dbghelp: Now moving the ELF loading code to Unicode.
[wine] / dlls / dbghelp / dbghelp_private.h
1 /*
2  * File dbghelp_private.h - dbghelp internal definitions
3  *
4  * Copyright (C) 1995, Alexandre Julliard
5  * Copyright (C) 1996, Eric Youngdale.
6  * Copyright (C) 1999-2000, Ulrich Weigand.
7  * Copyright (C) 2004-2007, Eric Pouech.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winver.h"
28 #include "dbghelp.h"
29 #include "objbase.h"
30 #include "oaidl.h"
31 #include "winnls.h"
32 #include "wine/unicode.h"
33
34 #include "cvconst.h"
35
36 /* #define USE_STATS */
37
38 struct pool /* poor's man */
39 {
40     struct pool_arena*  first;
41     unsigned            arena_size;
42 };
43
44 void     pool_init(struct pool* a, unsigned arena_size);
45 void     pool_destroy(struct pool* a);
46 void*    pool_alloc(struct pool* a, unsigned len);
47 /* void*    pool_realloc(struct pool* a, void* p,
48    unsigned old_size, unsigned new_size); */
49 char*    pool_strdup(struct pool* a, const char* str);
50
51 struct vector
52 {
53     void**      buckets;
54     unsigned    elt_size;
55     unsigned    shift;
56     unsigned    num_elts;
57     unsigned    num_buckets;
58 };
59
60 void     vector_init(struct vector* v, unsigned elt_sz, unsigned bucket_sz);
61 unsigned vector_length(const struct vector* v);
62 void*    vector_at(const struct vector* v, unsigned pos);
63 void*    vector_add(struct vector* v, struct pool* pool);
64 /*void     vector_pool_normalize(struct vector* v, struct pool* pool); */
65 void*    vector_iter_up(const struct vector* v, void* elt);
66 void*    vector_iter_down(const struct vector* v, void* elt);
67
68 struct sparse_array
69 {
70     struct vector               key2index;
71     struct vector               elements;
72 };
73
74 void     sparse_array_init(struct sparse_array* sa, unsigned elt_sz, unsigned bucket_sz);
75 void*    sparse_array_find(const struct sparse_array* sa, unsigned long idx);
76 void*    sparse_array_add(struct sparse_array* sa, unsigned long key, struct pool* pool);
77 unsigned sparse_array_length(const struct sparse_array* sa);
78
79 struct hash_table_elt
80 {
81     const char*                 name;
82     struct hash_table_elt*      next;
83 };
84
85 struct hash_table
86 {
87     unsigned                    num_elts;
88     unsigned                    num_buckets;
89     struct hash_table_elt**     buckets;
90 };
91
92 void     hash_table_init(struct pool* pool, struct hash_table* ht,
93                          unsigned num_buckets);
94 void     hash_table_destroy(struct hash_table* ht);
95 void     hash_table_add(struct hash_table* ht, struct hash_table_elt* elt);
96 void*    hash_table_find(const struct hash_table* ht, const char* name);
97 unsigned hash_table_hash(const char* name, unsigned num_buckets);
98
99 struct hash_table_iter
100 {
101     const struct hash_table*    ht;
102     struct hash_table_elt*      element;
103     int                         index;
104     int                         last;
105 };
106
107 void     hash_table_iter_init(const struct hash_table* ht,
108                               struct hash_table_iter* hti, const char* name);
109 void*    hash_table_iter_up(struct hash_table_iter* hti);
110
111 #define GET_ENTRY(__i, __t, __f) \
112     ((__t*)((char*)(__i) - (unsigned int)(&((__t*)0)->__f)))
113
114
115 extern unsigned dbghelp_options;
116 /* some more Wine extensions */
117 #define SYMOPT_WINE_WITH_ELF_MODULES 0x40000000
118
119 enum location_kind {loc_error,          /* reg is the error code */
120                     loc_absolute,       /* offset is the location */
121                     loc_register,       /* reg is the location */
122                     loc_regrel,         /* [reg+offset] is the location */
123                     loc_user,           /* value is debug information dependent,
124                                            reg & offset can be used ad libidem */
125 };
126
127 enum location_error {loc_err_internal = -1,     /* internal while computing */
128                      loc_err_too_complex = -2,  /* couldn't compute location (even at runtime) */
129                      loc_err_out_of_scope = -3, /* variable isn't available at current address */
130                      loc_err_cant_read = -4,    /* couldn't read memory at given address */
131 };
132
133 struct location
134 {
135     unsigned            kind : 8,
136                         reg;
137     unsigned long       offset;
138 };
139
140 struct symt
141 {
142     enum SymTagEnum             tag;
143 };
144
145 struct symt_ht
146 {
147     struct symt                 symt;
148     struct hash_table_elt       hash_elt;        /* if global symbol or type */
149 };
150
151 /* lexical tree */
152 struct symt_block
153 {
154     struct symt                 symt;
155     unsigned long               address;
156     unsigned long               size;
157     struct symt*                container;      /* block, or func */
158     struct vector               vchildren;      /* sub-blocks & local variables */
159 };
160
161 struct symt_compiland
162 {
163     struct symt                 symt;
164     unsigned long               address;
165     unsigned                    source;
166     struct vector               vchildren;      /* global variables & functions */
167 };
168
169 struct symt_data
170 {
171     struct symt                 symt;
172     struct hash_table_elt       hash_elt;       /* if global symbol */
173     enum DataKind               kind;
174     struct symt*                container;
175     struct symt*                type;
176     union                                       /* depends on kind */
177     {
178         /* DataIs{Global, FileStatic}:
179          *      loc.kind is loc_absolute
180          *      loc.offset is address
181          * DataIs{Local,Param}:
182          *      with loc.kind
183          *              loc_absolute    not supported
184          *              loc_register    location is in register loc.reg
185          *              loc_regrel      location is at address loc.reg + loc.offset
186          *              >= loc_user     ask debug info provider for resolution
187          */
188         struct location         var;
189         /* DataIs{Member} (all values are in bits, not bytes) */
190         struct
191         {
192             long                        offset;
193             unsigned long               length;
194         } member;
195         /* DataIsConstant */
196         VARIANT                 value;
197     } u;
198 };
199
200 struct symt_function
201 {
202     struct symt                 symt;
203     struct hash_table_elt       hash_elt;       /* if global symbol */
204     unsigned long               address;
205     struct symt*                container;      /* compiland */
206     struct symt*                type;           /* points to function_signature */
207     unsigned long               size;
208     struct vector               vlines;
209     struct vector               vchildren;      /* locals, params, blocks, start/end, labels */
210 };
211
212 struct symt_function_point
213 {
214     struct symt                 symt;           /* either SymTagFunctionDebugStart, SymTagFunctionDebugEnd, SymTagLabel */
215     struct symt_function*       parent;
216     struct location             loc;
217     const char*                 name;           /* for labels */
218 };
219
220 struct symt_public
221 {
222     struct symt                 symt;
223     struct hash_table_elt       hash_elt;
224     struct symt*                container;      /* compiland */
225     unsigned long               address;
226     unsigned long               size;
227     unsigned                    in_code : 1,
228                                 is_function : 1;
229 };
230
231 struct symt_thunk
232 {
233     struct symt                 symt;
234     struct hash_table_elt       hash_elt;
235     struct symt*                container;      /* compiland */
236     unsigned long               address;
237     unsigned long               size;
238     THUNK_ORDINAL               ordinal;        /* FIXME: doesn't seem to be accessible */
239 };
240
241 /* class tree */
242 struct symt_array
243 {
244     struct symt                 symt;
245     int                         start;
246     int                         end;
247     struct symt*                base_type;
248     struct symt*                index_type;
249 };
250
251 struct symt_basic
252 {
253     struct symt                 symt;
254     struct hash_table_elt       hash_elt;
255     enum BasicType              bt;
256     unsigned long               size;
257 };
258
259 struct symt_enum
260 {
261     struct symt                 symt;
262     const char*                 name;
263     struct vector               vchildren;
264 };
265
266 struct symt_function_signature
267 {
268     struct symt                 symt;
269     struct symt*                rettype;
270     struct vector               vchildren;
271     enum CV_call_e              call_conv;
272 };
273
274 struct symt_function_arg_type
275 {
276     struct symt                 symt;
277     struct symt*                arg_type;
278     struct symt*                container;
279 };
280
281 struct symt_pointer
282 {
283     struct symt                 symt;
284     struct symt*                pointsto;
285 };
286
287 struct symt_typedef
288 {
289     struct symt                 symt;
290     struct hash_table_elt       hash_elt;
291     struct symt*                type;
292 };
293
294 struct symt_udt
295 {
296     struct symt                 symt;
297     struct hash_table_elt       hash_elt;
298     enum UdtKind                kind;
299     int                         size;
300     struct vector               vchildren;
301 };
302
303 enum module_type
304 {
305     DMT_UNKNOWN,        /* for lookup, not actually used for a module */
306     DMT_ELF,            /* a real ELF shared module */
307     DMT_PE,             /* a native or builtin PE module */
308     DMT_PDB,            /* PDB file */
309 };
310
311 struct process;
312
313 struct module
314 {
315     IMAGEHLP_MODULEW64          module;
316     /* ANSI copy of module.ModuleName for efficiency */
317     char                        module_name[MAX_PATH];
318     struct module*              next;
319     enum module_type            type : 16;
320     unsigned short              is_virtual : 1;
321
322     /* specific information for debug types */
323     struct elf_module_info*     elf_info;
324     struct dwarf2_module_info_s*dwarf2_info;
325
326     /* memory allocation pool */
327     struct pool                 pool;
328
329     /* symbols & symbol tables */
330     int                         sortlist_valid;
331     unsigned                    num_sorttab;    /* number of symbols with addresses */
332     struct symt_ht**            addr_sorttab;
333     struct hash_table           ht_symbols;
334     void                        (*loc_compute)(struct process* pcs,
335                                                const struct module* module,
336                                                const struct symt_function* func,
337                                                struct location* loc);
338
339     /* types */
340     struct hash_table           ht_types;
341     struct vector               vtypes;
342
343     /* source files */
344     unsigned                    sources_used;
345     unsigned                    sources_alloc;
346     char*                       sources;
347 };
348
349 struct process 
350 {
351     struct process*             next;
352     HANDLE                      handle;
353     WCHAR*                      search_path;
354     
355     PSYMBOL_REGISTERED_CALLBACK64       reg_cb;
356     BOOL                        reg_is_unicode;
357     DWORD64                     reg_user;
358
359     struct module*              lmodules;
360     unsigned long               dbg_hdr_addr;
361
362     IMAGEHLP_STACK_FRAME        ctx_frame;
363
364     unsigned                    buffer_size;
365     void*                       buffer;
366 };
367
368 struct line_info
369 {
370     unsigned long               is_first : 1,
371                                 is_last : 1,
372                                 is_source_file : 1,
373                                 line_number;
374     union
375     {
376         unsigned long               pc_offset;   /* if is_source_file isn't set */
377         unsigned                    source_file; /* if is_source_file is set */
378     } u;
379 };
380
381 struct module_pair
382 {
383     struct process*             pcs;
384     struct module*              requested; /* in:  to module_get_debug() */
385     struct module*              effective; /* out: module with debug info */
386 };
387
388 enum pdb_kind {PDB_JG, PDB_DS};
389
390 struct pdb_lookup
391 {
392     const char*                 filename;
393     DWORD                       age;
394     enum pdb_kind               kind;
395     union
396     {
397         struct
398         {
399             DWORD               timestamp;
400             struct PDB_JG_TOC*  toc;
401         } jg;
402         struct
403         {
404             GUID                guid;
405             struct PDB_DS_TOC*  toc;
406         } ds;
407     } u;
408 };
409
410 /* dbghelp.c */
411 extern struct process* process_find_by_handle(HANDLE hProcess);
412 extern HANDLE hMsvcrt;
413 extern BOOL         validate_addr64(DWORD64 addr);
414 extern BOOL         pcs_callback(const struct process* pcs, ULONG action, void* data);
415 extern void*        fetch_buffer(struct process* pcs, unsigned size);
416
417 /* elf_module.c */
418 #define ELF_NO_MAP      ((const void*)0xffffffff)
419 typedef BOOL (*elf_enum_modules_cb)(const WCHAR*, unsigned long addr, void* user);
420 extern BOOL         elf_enum_modules(HANDLE hProc, elf_enum_modules_cb, void*);
421 extern BOOL         elf_fetch_file_info(const WCHAR* name, DWORD* base, DWORD* size, DWORD* checksum);
422 struct elf_file_map;
423 extern BOOL         elf_load_debug_info(struct module* module, struct elf_file_map* fmap);
424 extern struct module*
425                     elf_load_module(struct process* pcs, const WCHAR* name, unsigned long);
426 extern BOOL         elf_read_wine_loader_dbg_info(struct process* pcs);
427 extern BOOL         elf_synchronize_module_list(struct process* pcs);
428 struct elf_thunk_area;
429 extern int          elf_is_in_thunk_area(unsigned long addr, const struct elf_thunk_area* thunks);
430 extern DWORD WINAPI addr_to_linear(HANDLE hProcess, HANDLE hThread, ADDRESS* addr);
431
432 /* module.c */
433 extern const WCHAR      S_ElfW[];
434 extern const WCHAR      S_WineLoaderW[];
435 extern const WCHAR      S_WinePThreadW[];
436 extern const WCHAR      S_WineKThreadW[];
437 extern const WCHAR      S_SlashW[];
438
439 extern struct module*
440                     module_find_by_addr(const struct process* pcs, unsigned long addr,
441                                         enum module_type type);
442 extern struct module*
443                     module_find_by_name(const struct process* pcs,
444                                         const WCHAR* name, enum module_type type);
445 extern struct module*
446                     module_find_by_nameA(const struct process* pcs,
447                                          const char* name, enum module_type type);
448 extern BOOL         module_get_debug(struct module_pair*);
449 extern struct module*
450                     module_new(struct process* pcs, const WCHAR* name,
451                                enum module_type type, BOOL virtual,
452                                unsigned long addr, unsigned long size,
453                                unsigned long stamp, unsigned long checksum);
454 extern struct module*
455                     module_get_container(const struct process* pcs,
456                                          const struct module* inner);
457 extern struct module*
458                     module_get_containee(const struct process* pcs,
459                                          const struct module* inner);
460 extern enum module_type
461                     module_get_type_by_name(const WCHAR* name);
462 extern void         module_reset_debug_info(struct module* module);
463 extern BOOL         module_remove(struct process* pcs,
464                                   struct module* module);
465 extern void         module_set_module(struct module* module, const WCHAR* name);
466
467 /* msc.c */
468 extern BOOL         pe_load_debug_directory(const struct process* pcs,
469                                             struct module* module, 
470                                             const BYTE* mapping,
471                                             const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
472                                             const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg);
473 extern BOOL         pdb_fetch_file_info(struct pdb_lookup* pdb_lookup);
474
475 /* pe_module.c */
476 extern BOOL         pe_load_nt_header(HANDLE hProc, DWORD base, IMAGE_NT_HEADERS* nth);
477 extern struct module*
478                     pe_load_module(struct process* pcs, const WCHAR* name,
479                                    HANDLE hFile, DWORD base, DWORD size);
480 extern struct module*
481                     pe_load_module_from_pcs(struct process* pcs, const WCHAR* name,
482                                             const WCHAR* mod_name, DWORD base, DWORD size);
483 extern BOOL         pe_load_debug_info(const struct process* pcs,
484                                        struct module* module);
485 /* source.c */
486 extern unsigned     source_new(struct module* module, const char* basedir, const char* source);
487 extern const char*  source_get(const struct module* module, unsigned idx);
488
489 /* stabs.c */
490 extern BOOL         stabs_parse(struct module* module, unsigned long load_offset,
491                                 const void* stabs, int stablen,
492                                 const char* strs, int strtablen);
493
494 /* dwarf.c */
495 extern BOOL         dwarf2_parse(struct module* module, unsigned long load_offset,
496                                  const struct elf_thunk_area* thunks,
497                                  const unsigned char* debug, unsigned int debug_size, 
498                                  const unsigned char* abbrev, unsigned int abbrev_size, 
499                                  const unsigned char* str, unsigned int str_size,
500                                  const unsigned char* line, unsigned int line_size,
501                                  const unsigned char* loclist, unsigned int loclist_size);
502
503 /* symbol.c */
504 extern const char*  symt_get_name(const struct symt* sym);
505 extern int          symt_cmp_addr(const void* p1, const void* p2);
506 extern struct symt_ht*
507                     symt_find_nearest(struct module* module, DWORD addr);
508 extern struct symt_compiland*
509                     symt_new_compiland(struct module* module, unsigned long address,
510                                        unsigned src_idx);
511 extern struct symt_public*
512                     symt_new_public(struct module* module, 
513                                     struct symt_compiland* parent, 
514                                     const char* typename,
515                                     unsigned long address, unsigned size,
516                                     BOOL in_code, BOOL is_func);
517 extern struct symt_data*
518                     symt_new_global_variable(struct module* module, 
519                                              struct symt_compiland* parent,
520                                              const char* name, unsigned is_static,
521                                              unsigned long addr, unsigned long size, 
522                                              struct symt* type);
523 extern struct symt_function*
524                     symt_new_function(struct module* module,
525                                       struct symt_compiland* parent,
526                                       const char* name,
527                                       unsigned long addr, unsigned long size,
528                                       struct symt* type);
529 extern BOOL         symt_normalize_function(struct module* module, 
530                                             struct symt_function* func);
531 extern void         symt_add_func_line(struct module* module,
532                                        struct symt_function* func, 
533                                        unsigned source_idx, int line_num, 
534                                        unsigned long offset);
535 extern struct symt_data*
536                     symt_add_func_local(struct module* module, 
537                                         struct symt_function* func, 
538                                         enum DataKind dt, const struct location* loc,
539                                         struct symt_block* block,
540                                         struct symt* type, const char* name);
541 extern struct symt_block*
542                     symt_open_func_block(struct module* module, 
543                                          struct symt_function* func,
544                                          struct symt_block* block, 
545                                          unsigned pc, unsigned len);
546 extern struct symt_block*
547                     symt_close_func_block(struct module* module, 
548                                           struct symt_function* func,
549                                           struct symt_block* block, unsigned pc);
550 extern struct symt_function_point*
551                     symt_add_function_point(struct module* module, 
552                                             struct symt_function* func,
553                                             enum SymTagEnum point, 
554                                             const struct location* loc,
555                                             const char* name);
556 extern BOOL         symt_fill_func_line_info(struct module* module,
557                                              struct symt_function* func, 
558                                              DWORD addr, IMAGEHLP_LINE* line);
559 extern BOOL         symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line);
560 extern struct symt_thunk*
561                     symt_new_thunk(struct module* module, 
562                                    struct symt_compiland* parent,
563                                    const char* name, THUNK_ORDINAL ord,
564                                    unsigned long addr, unsigned long size);
565 extern struct symt_data*
566                     symt_new_constant(struct module* module,
567                                       struct symt_compiland* parent,
568                                       const char* name, struct symt* type,
569                                       const VARIANT* v);
570
571 /* type.c */
572 extern void         symt_init_basic(struct module* module);
573 extern BOOL         symt_get_info(const struct symt* type,
574                                   IMAGEHLP_SYMBOL_TYPE_INFO req, void* pInfo);
575 extern struct symt_basic*
576                     symt_new_basic(struct module* module, enum BasicType, 
577                                    const char* typename, unsigned size);
578 extern struct symt_udt*
579                     symt_new_udt(struct module* module, const char* typename,
580                                  unsigned size, enum UdtKind kind);
581 extern BOOL         symt_set_udt_size(struct module* module,
582                                       struct symt_udt* type, unsigned size);
583 extern BOOL         symt_add_udt_element(struct module* module, 
584                                          struct symt_udt* udt_type, 
585                                          const char* name,
586                                          struct symt* elt_type, unsigned offset, 
587                                          unsigned size);
588 extern struct symt_enum*
589                     symt_new_enum(struct module* module, const char* typename);
590 extern BOOL         symt_add_enum_element(struct module* module, 
591                                           struct symt_enum* enum_type, 
592                                           const char* name, int value);
593 extern struct symt_array*
594                     symt_new_array(struct module* module, int min, int max, 
595                                    struct symt* base, struct symt* index);
596 extern struct symt_function_signature*
597                     symt_new_function_signature(struct module* module, 
598                                                 struct symt* ret_type,
599                                                 enum CV_call_e call_conv);
600 extern BOOL         symt_add_function_signature_parameter(struct module* module,
601                                                           struct symt_function_signature* sig,
602                                                           struct symt* param);
603 extern struct symt_pointer*
604                     symt_new_pointer(struct module* module, 
605                                      struct symt* ref_type);
606 extern struct symt_typedef*
607                     symt_new_typedef(struct module* module, struct symt* ref, 
608                                      const char* name);