comctl32: Fix read of uninitialized data in LISTVIEW_GetItemExtT when LVIF_TEXT is...
[wine] / dlls / dbghelp / symbol.c
1 /*
2  * File symbol.c - management of symbols (lexical tree)
3  *
4  * Copyright (C) 1993, Eric Youngdale.
5  *               2004, Eric Pouech
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24
25 #include "config.h"
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <sys/types.h>
32 #include <assert.h>
33 #ifdef HAVE_REGEX_H
34 # include <regex.h>
35 #endif
36
37 #include "wine/debug.h"
38 #include "dbghelp_private.h"
39 #include "winnls.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
42 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
43
44 static inline int cmp_addr(ULONG64 a1, ULONG64 a2)
45 {
46     if (a1 > a2) return 1;
47     if (a1 < a2) return -1;
48     return 0;
49 }
50
51 static inline int cmp_sorttab_addr(struct module* module, int idx, ULONG64 addr)
52 {
53     ULONG64     ref;
54
55     symt_get_info(module, &module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
56     return cmp_addr(ref, addr);
57 }
58
59 struct module*  symt_cmp_addr_module = NULL;
60
61 int symt_cmp_addr(const void* p1, const void* p2)
62 {
63     const struct symt*  sym1 = *(const struct symt* const *)p1;
64     const struct symt*  sym2 = *(const struct symt* const *)p2;
65     ULONG64     a1, a2;
66
67     symt_get_info(symt_cmp_addr_module, sym1, TI_GET_ADDRESS, &a1);
68     symt_get_info(symt_cmp_addr_module, sym2, TI_GET_ADDRESS, &a2);
69     return cmp_addr(a1, a2);
70 }
71
72 DWORD             symt_ptr2index(struct module* module, const struct symt* sym)
73 {
74 #ifdef _WIN64
75     const struct symt** c;
76     int                 len = vector_length(&module->vsymt), i;
77
78     /* FIXME: this is inefficient */
79     for (i = 0; i < len; i++)
80     {
81         if (*(struct symt**)vector_at(&module->vsymt, i) == sym)
82             return i + 1;
83     }
84     /* not found */
85     c = vector_add(&module->vsymt, &module->pool);
86     if (c) *c = sym;
87     return len + 1;
88 #else
89     return (DWORD)sym;
90 #endif
91 }
92
93 struct symt*      symt_index2ptr(struct module* module, DWORD id)
94 {
95 #ifdef _WIN64
96     if (!id-- || id >= vector_length(&module->vsymt)) return NULL;
97     return *(struct symt**)vector_at(&module->vsymt, id);
98 #else
99     return (struct symt*)id;
100 #endif
101 }
102
103 static BOOL symt_grow_sorttab(struct module* module, unsigned sz)
104 {
105     struct symt_ht**    new;
106     unsigned int size;
107
108     if (sz <= module->sorttab_size) return TRUE;
109     if (module->addr_sorttab)
110     {
111         size = module->sorttab_size * 2;
112         new = HeapReAlloc(GetProcessHeap(), 0, module->addr_sorttab,
113                           size * sizeof(struct symt_ht*));
114     }
115     else
116     {
117         size = 64;
118         new = HeapAlloc(GetProcessHeap(), 0, size * sizeof(struct symt_ht*));
119     }
120     if (!new) return FALSE;
121     module->sorttab_size = size;
122     module->addr_sorttab = new;
123     return TRUE;
124 }
125
126 static void symt_add_module_ht(struct module* module, struct symt_ht* ht)
127 {
128     ULONG64             addr;
129
130     hash_table_add(&module->ht_symbols, &ht->hash_elt);
131     /* Don't store in sorttab a symbol without address, they are of
132      * no use here (e.g. constant values)
133      */
134     if (symt_get_info(module, &ht->symt, TI_GET_ADDRESS, &addr) &&
135         symt_grow_sorttab(module, module->num_symbols + 1))
136     {
137         module->addr_sorttab[module->num_symbols++] = ht;
138         module->sortlist_valid = FALSE;
139     }
140 }
141
142 #ifdef HAVE_REGEX_H
143
144 /* transforms a dbghelp's regular expression into a POSIX one
145  * Here are the valid dbghelp reg ex characters:
146  *      *       0 or more characters
147  *      ?       a single character
148  *      []      list
149  *      #       0 or more of preceding char
150  *      +       1 or more of preceding char
151  *      escapes \ on #, ?, [, ], *, +. don't work on -
152  */
153 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
154 {
155     char *mask, *p;
156     BOOL        in_escape = FALSE;
157     unsigned    flags = REG_NOSUB;
158
159     if (numchar == -1) numchar = strlen( str );
160
161     p = mask = HeapAlloc( GetProcessHeap(), 0, 2 * numchar + 3 );
162     *p++ = '^';
163
164     while (*str && numchar--)
165     {
166         /* FIXME: this shouldn't be valid on '-' */
167         if (in_escape)
168         {
169             *p++ = '\\';
170             *p++ = *str;
171             in_escape = FALSE;
172         }
173         else switch (*str)
174         {
175         case '\\': in_escape = TRUE; break;
176         case '*':  *p++ = '.'; *p++ = '*'; break;
177         case '?':  *p++ = '.'; break;
178         case '#':  *p++ = '*'; break;
179         /* escape some valid characters in dbghelp reg exp:s */
180         case '$':  *p++ = '\\'; *p++ = '$'; break;
181         /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
182         default:   *p++ = *str; break;
183         }
184         str++;
185     }
186     if (in_escape)
187     {
188         *p++ = '\\';
189         *p++ = '\\';
190     }
191     *p++ = '$';
192     *p = 0;
193     if (_case) flags |= REG_ICASE;
194     if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
195     HeapFree(GetProcessHeap(), 0, mask);
196 }
197
198 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
199 {
200     char *mask, *p;
201     BOOL ret;
202
203     if (!srcfile || !*srcfile) return regcomp(re, ".*", REG_NOSUB);
204
205     p = mask = HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile) + 4);
206     *p++ = '^';
207     while (*srcfile)
208     {
209         switch (*srcfile)
210         {
211         case '\\':
212         case '/':
213             *p++ = '[';
214             *p++ = '\\';
215             *p++ = '\\';
216             *p++ = '/';
217             *p++ = ']';
218             break;
219         case '.':
220             *p++ = '\\';
221             *p++ = '.';
222             break;
223         default:
224             *p++ = *srcfile;
225             break;
226         }
227         srcfile++;
228     }
229     *p++ = '$';
230     *p = 0;
231     ret = !regcomp(re, mask, REG_NOSUB);
232     HeapFree(GetProcessHeap(), 0, mask);
233     if (!ret)
234     {
235         FIXME("Couldn't compile %s\n", mask);
236         SetLastError(ERROR_INVALID_PARAMETER);
237     }
238     return ret;
239 }
240
241 static int match_regexp( const regex_t *re, const char *str )
242 {
243     return !regexec( re, str, 0, NULL, 0 );
244 }
245
246 #else /* HAVE_REGEX_H */
247
248 /* if we don't have regexp support, fall back to a simple string comparison */
249
250 typedef struct
251 {
252     char *str;
253     BOOL  icase;
254 } regex_t;
255
256 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
257 {
258     if (numchar == -1) numchar = strlen( str );
259
260     re->str = HeapAlloc( GetProcessHeap(), 0, numchar + 1 );
261     memcpy( re->str, str, numchar );
262     re->str[numchar] = 0;
263     re->icase = _case;
264 }
265
266 static BOOL compile_file_regex(regex_t* re, const char* srcfile)
267 {
268     if (!srcfile || !*srcfile) re->str = NULL;
269     else compile_regex( srcfile, -1, re, FALSE );
270     return TRUE;
271 }
272
273 static int match_regexp( const regex_t *re, const char *str )
274 {
275     if (!re->str) return 1;
276     if (re->icase) return !lstrcmpiA( re->str, str );
277     return !strcmp( re->str, str );
278 }
279
280 static void regfree( regex_t *re )
281 {
282     HeapFree( GetProcessHeap(), 0, re->str );
283 }
284
285 #endif /* HAVE_REGEX_H */
286
287 struct symt_compiland* symt_new_compiland(struct module* module, 
288                                           unsigned long address, unsigned src_idx)
289 {
290     struct symt_compiland*    sym;
291
292     TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
293                          debugstr_w(module->module.ModuleName), source_get(module, src_idx));
294     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
295     {
296         sym->symt.tag = SymTagCompiland;
297         sym->address  = address;
298         sym->source   = src_idx;
299         vector_init(&sym->vchildren, sizeof(struct symt*), 32);
300     }
301     return sym;
302 }
303
304 struct symt_public* symt_new_public(struct module* module, 
305                                     struct symt_compiland* compiland,
306                                     const char* name,
307                                     unsigned long address, unsigned size)
308 {
309     struct symt_public* sym;
310     struct symt**       p;
311
312     TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
313                          debugstr_w(module->module.ModuleName), name, address);
314     if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
315         symt_find_nearest(module, address) != NULL)
316         return NULL;
317     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
318     {
319         sym->symt.tag      = SymTagPublicSymbol;
320         sym->hash_elt.name = pool_strdup(&module->pool, name);
321         sym->container     = compiland ? &compiland->symt : NULL;
322         sym->address       = address;
323         sym->size          = size;
324         symt_add_module_ht(module, (struct symt_ht*)sym);
325         if (compiland)
326         {
327             p = vector_add(&compiland->vchildren, &module->pool);
328             *p = &sym->symt;
329         }
330     }
331     return sym;
332 }
333
334 struct symt_data* symt_new_global_variable(struct module* module, 
335                                            struct symt_compiland* compiland, 
336                                            const char* name, unsigned is_static,
337                                            struct location loc, unsigned long size,
338                                            struct symt* type)
339 {
340     struct symt_data*   sym;
341     struct symt**       p;
342     DWORD64             tsz;
343
344     TRACE_(dbghelp_symt)("Adding global symbol %s:%s %d@%lx %p\n",
345                          debugstr_w(module->module.ModuleName), name, loc.kind, loc.offset, type);
346     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
347     {
348         sym->symt.tag      = SymTagData;
349         sym->hash_elt.name = pool_strdup(&module->pool, name);
350         sym->kind          = is_static ? DataIsFileStatic : DataIsGlobal;
351         sym->container     = compiland ? &compiland->symt : NULL;
352         sym->type          = type;
353         sym->u.var         = loc;
354         if (type && size && symt_get_info(module, type, TI_GET_LENGTH, &tsz))
355         {
356             if (tsz != size)
357                 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
358                       debugstr_w(module->module.ModuleName), name,
359                       wine_dbgstr_longlong(tsz), size);
360         }
361         symt_add_module_ht(module, (struct symt_ht*)sym);
362         if (compiland)
363         {
364             p = vector_add(&compiland->vchildren, &module->pool);
365             *p = &sym->symt;
366         }
367     }
368     return sym;
369 }
370
371 struct symt_function* symt_new_function(struct module* module, 
372                                         struct symt_compiland* compiland, 
373                                         const char* name,
374                                         unsigned long addr, unsigned long size,
375                                         struct symt* sig_type)
376 {
377     struct symt_function*       sym;
378     struct symt**               p;
379
380     TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
381                          debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
382
383     assert(!sig_type || sig_type->tag == SymTagFunctionType);
384     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
385     {
386         sym->symt.tag  = SymTagFunction;
387         sym->hash_elt.name = pool_strdup(&module->pool, name);
388         sym->container = &compiland->symt;
389         sym->address   = addr;
390         sym->type      = sig_type;
391         sym->size      = size;
392         vector_init(&sym->vlines,  sizeof(struct line_info), 64);
393         vector_init(&sym->vchildren, sizeof(struct symt*), 8);
394         symt_add_module_ht(module, (struct symt_ht*)sym);
395         if (compiland)
396         {
397             p = vector_add(&compiland->vchildren, &module->pool);
398             *p = &sym->symt;
399         }
400     }
401     return sym;
402 }
403
404 void symt_add_func_line(struct module* module, struct symt_function* func,
405                         unsigned source_idx, int line_num, unsigned long offset)
406 {
407     struct line_info*   dli;
408     BOOL                last_matches = FALSE;
409     int                 i;
410
411     if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
412
413     TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n", 
414                          func, func->hash_elt.name, offset, 
415                          source_get(module, source_idx), line_num);
416
417     assert(func->symt.tag == SymTagFunction);
418
419     for (i=vector_length(&func->vlines)-1; i>=0; i--)
420     {
421         dli = vector_at(&func->vlines, i);
422         if (dli->is_source_file)
423         {
424             last_matches = (source_idx == dli->u.source_file);
425             break;
426         }
427     }
428
429     if (!last_matches)
430     {
431         /* we shouldn't have line changes on first line of function */
432         dli = vector_add(&func->vlines, &module->pool);
433         dli->is_source_file = 1;
434         dli->is_first       = dli->is_last = 0;
435         dli->line_number    = 0;
436         dli->u.source_file  = source_idx;
437     }
438     dli = vector_add(&func->vlines, &module->pool);
439     dli->is_source_file = 0;
440     dli->is_first       = dli->is_last = 0;
441     dli->line_number    = line_num;
442     dli->u.pc_offset    = func->address + offset;
443 }
444
445 /******************************************************************
446  *             symt_add_func_local
447  *
448  * Adds a new local/parameter to a given function:
449  * In any cases, dt tells whether it's a local variable or a parameter
450  * If regno it's not 0:
451  *      - then variable is stored in a register
452  *      - otherwise, value is referenced by register + offset
453  * Otherwise, the variable is stored on the stack:
454  *      - offset is then the offset from the frame register
455  */
456 struct symt_data* symt_add_func_local(struct module* module, 
457                                       struct symt_function* func, 
458                                       enum DataKind dt,
459                                       const struct location* loc,
460                                       struct symt_block* block, 
461                                       struct symt* type, const char* name)
462 {
463     struct symt_data*   locsym;
464     struct symt**       p;
465
466     TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
467                          debugstr_w(module->module.ModuleName), func->hash_elt.name,
468                          name, type);
469
470     assert(func);
471     assert(func->symt.tag == SymTagFunction);
472     assert(dt == DataIsParam || dt == DataIsLocal);
473
474     locsym = pool_alloc(&module->pool, sizeof(*locsym));
475     locsym->symt.tag      = SymTagData;
476     locsym->hash_elt.name = pool_strdup(&module->pool, name);
477     locsym->hash_elt.next = NULL;
478     locsym->kind          = dt;
479     locsym->container     = block ? &block->symt : &func->symt;
480     locsym->type          = type;
481     locsym->u.var         = *loc;
482     if (block)
483         p = vector_add(&block->vchildren, &module->pool);
484     else
485         p = vector_add(&func->vchildren, &module->pool);
486     *p = &locsym->symt;
487     return locsym;
488 }
489
490
491 struct symt_block* symt_open_func_block(struct module* module, 
492                                         struct symt_function* func,
493                                         struct symt_block* parent_block, 
494                                         unsigned pc, unsigned len)
495 {
496     struct symt_block*  block;
497     struct symt**       p;
498
499     assert(func);
500     assert(func->symt.tag == SymTagFunction);
501
502     assert(!parent_block || parent_block->symt.tag == SymTagBlock);
503     block = pool_alloc(&module->pool, sizeof(*block));
504     block->symt.tag = SymTagBlock;
505     block->address  = func->address + pc;
506     block->size     = len;
507     block->container = parent_block ? &parent_block->symt : &func->symt;
508     vector_init(&block->vchildren, sizeof(struct symt*), 4);
509     if (parent_block)
510         p = vector_add(&parent_block->vchildren, &module->pool);
511     else
512         p = vector_add(&func->vchildren, &module->pool);
513     *p = &block->symt;
514
515     return block;
516 }
517
518 struct symt_block* symt_close_func_block(struct module* module, 
519                                          const struct symt_function* func,
520                                          struct symt_block* block, unsigned pc)
521 {
522     assert(func);
523     assert(func->symt.tag == SymTagFunction);
524
525     if (pc) block->size = func->address + pc - block->address;
526     return (block->container->tag == SymTagBlock) ? 
527         GET_ENTRY(block->container, struct symt_block, symt) : NULL;
528 }
529
530 struct symt_hierarchy_point* symt_add_function_point(struct module* module,
531                                                      struct symt_function* func,
532                                                      enum SymTagEnum point,
533                                                      const struct location* loc,
534                                                      const char* name)
535 {
536     struct symt_hierarchy_point*sym;
537     struct symt**               p;
538
539     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
540     {
541         sym->symt.tag = point;
542         sym->parent   = &func->symt;
543         sym->loc      = *loc;
544         sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
545         p = vector_add(&func->vchildren, &module->pool);
546         *p = &sym->symt;
547     }
548     return sym;
549 }
550
551 BOOL symt_normalize_function(struct module* module, const struct symt_function* func)
552 {
553     unsigned            len;
554     struct line_info*   dli;
555
556     assert(func);
557     /* We aren't adding any more locals or line numbers to this function.
558      * Free any spare memory that we might have allocated.
559      */
560     assert(func->symt.tag == SymTagFunction);
561
562 /* EPP     vector_pool_normalize(&func->vlines,    &module->pool); */
563 /* EPP     vector_pool_normalize(&func->vchildren, &module->pool); */
564
565     len = vector_length(&func->vlines);
566     if (len--)
567     {
568         dli = vector_at(&func->vlines,   0);  dli->is_first = 1;
569         dli = vector_at(&func->vlines, len);  dli->is_last  = 1;
570     }
571     return TRUE;
572 }
573
574 struct symt_thunk* symt_new_thunk(struct module* module, 
575                                   struct symt_compiland* compiland, 
576                                   const char* name, THUNK_ORDINAL ord,
577                                   unsigned long addr, unsigned long size)
578 {
579     struct symt_thunk*  sym;
580
581     TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
582                          debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
583
584     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
585     {
586         sym->symt.tag  = SymTagThunk;
587         sym->hash_elt.name = pool_strdup(&module->pool, name);
588         sym->container = &compiland->symt;
589         sym->address   = addr;
590         sym->size      = size;
591         sym->ordinal   = ord;
592         symt_add_module_ht(module, (struct symt_ht*)sym);
593         if (compiland)
594         {
595             struct symt**       p;
596             p = vector_add(&compiland->vchildren, &module->pool);
597             *p = &sym->symt;
598         }
599     }
600     return sym;
601 }
602
603 struct symt_data* symt_new_constant(struct module* module,
604                                     struct symt_compiland* compiland,
605                                     const char* name, struct symt* type,
606                                     const VARIANT* v)
607 {
608     struct symt_data*  sym;
609
610     TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
611                          debugstr_w(module->module.ModuleName), name);
612
613     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
614     {
615         sym->symt.tag      = SymTagData;
616         sym->hash_elt.name = pool_strdup(&module->pool, name);
617         sym->kind          = DataIsConstant;
618         sym->container     = compiland ? &compiland->symt : NULL;
619         sym->type          = type;
620         sym->u.value       = *v;
621         symt_add_module_ht(module, (struct symt_ht*)sym);
622         if (compiland)
623         {
624             struct symt**       p;
625             p = vector_add(&compiland->vchildren, &module->pool);
626             *p = &sym->symt;
627         }
628     }
629     return sym;
630 }
631
632 struct symt_hierarchy_point* symt_new_label(struct module* module,
633                                             struct symt_compiland* compiland,
634                                             const char* name, unsigned long address)
635 {
636     struct symt_hierarchy_point*        sym;
637
638     TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
639                          debugstr_w(module->module.ModuleName), name);
640
641     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
642     {
643         sym->symt.tag      = SymTagLabel;
644         sym->hash_elt.name = pool_strdup(&module->pool, name);
645         sym->loc.kind      = loc_absolute;
646         sym->loc.offset    = address;
647         sym->parent        = compiland ? &compiland->symt : NULL;
648         symt_add_module_ht(module, (struct symt_ht*)sym);
649         if (compiland)
650         {
651             struct symt**       p;
652             p = vector_add(&compiland->vchildren, &module->pool);
653             *p = &sym->symt;
654         }
655     }
656     return sym;
657 }
658
659 /* expect sym_info->MaxNameLen to be set before being called */
660 static void symt_fill_sym_info(struct module_pair* pair,
661                                const struct symt_function* func,
662                                const struct symt* sym, SYMBOL_INFO* sym_info)
663 {
664     const char* name;
665     DWORD64 size;
666
667     if (!symt_get_info(pair->effective, sym, TI_GET_TYPE, &sym_info->TypeIndex))
668         sym_info->TypeIndex = 0;
669     sym_info->info = symt_ptr2index(pair->effective, sym);
670     sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
671     if (!symt_get_info(pair->effective, sym, TI_GET_LENGTH, &size) &&
672         (!sym_info->TypeIndex ||
673          !symt_get_info(pair->effective, symt_index2ptr(pair->effective, sym_info->TypeIndex),
674                          TI_GET_LENGTH, &size)))
675         size = 0;
676     sym_info->Size = (DWORD)size;
677     sym_info->ModBase = pair->requested->module.BaseOfImage;
678     sym_info->Flags = 0;
679     sym_info->Value = 0;
680
681     switch (sym->tag)
682     {
683     case SymTagData:
684         {
685             const struct symt_data*  data = (const struct symt_data*)sym;
686             switch (data->kind)
687             {
688             case DataIsParam:
689                 sym_info->Flags |= SYMFLAG_PARAMETER;
690                 /* fall through */
691             case DataIsLocal:
692                 {
693                     struct location loc = data->u.var;
694
695                     if (loc.kind >= loc_user)
696                     {
697                         unsigned                i;
698                         struct module_format*   modfmt;
699
700                         for (i = 0; i < DFI_LAST; i++)
701                         {
702                             modfmt = pair->effective->format_info[i];
703                             if (modfmt && modfmt->loc_compute)
704                             {
705                                 modfmt->loc_compute(pair->pcs, modfmt, func, &loc);
706                                 break;
707                             }
708                         }
709                     }
710                     switch (loc.kind)
711                     {
712                     case loc_error:
713                         /* for now we report error cases as a negative register number */
714                         sym_info->Flags |= SYMFLAG_LOCAL;
715                         /* fall through */
716                     case loc_register:
717                         sym_info->Flags |= SYMFLAG_REGISTER;
718                         sym_info->Register = loc.reg;
719                         sym_info->Address = 0;
720                         break;
721                     case loc_regrel:
722                         sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
723                         /* FIXME: it's i386 dependent !!! */
724                         sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
725                         sym_info->Address = loc.offset;
726                         break;
727                     case loc_absolute:
728                         sym_info->Flags |= SYMFLAG_VALUEPRESENT;
729                         sym_info->Value = loc.offset;
730                         break;
731                     default:
732                         FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
733                         assert(0);
734                     }
735                 }
736                 break;
737             case DataIsGlobal:
738             case DataIsFileStatic:
739                 switch (data->u.var.kind)
740                 {
741                 case loc_tlsrel:
742                     sym_info->Flags |= SYMFLAG_TLSREL;
743                     /* fall through */
744                 case loc_absolute:
745                     symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
746                     sym_info->Register = 0;
747                     break;
748                 default:
749                     FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", data->u.var.kind);
750                     assert(0);
751                 }
752                 break;
753             case DataIsConstant:
754                 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
755                 switch (data->u.value.n1.n2.vt)
756                 {
757                 case VT_I4:  sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
758                 case VT_I2:  sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
759                 case VT_I1:  sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
760                 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
761                 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
762                 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
763                 case VT_I1 | VT_BYREF: sym_info->Value = (ULONG64)(DWORD_PTR)data->u.value.n1.n2.n3.byref; break;
764                 case VT_EMPTY: sym_info->Value = 0; break;
765                 default:
766                     FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
767                     sym_info->Value = 0;
768                     break;
769                 }
770                 break;
771             default:
772                 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
773             }
774         }
775         break;
776     case SymTagPublicSymbol:
777         sym_info->Flags |= SYMFLAG_EXPORT;
778         symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
779         break;
780     case SymTagFunction:
781         sym_info->Flags |= SYMFLAG_FUNCTION;
782         symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
783         break;
784     case SymTagThunk:
785         sym_info->Flags |= SYMFLAG_THUNK;
786         symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
787         break;
788     default:
789         symt_get_info(pair->effective, sym, TI_GET_ADDRESS, &sym_info->Address);
790         sym_info->Register = 0;
791         break;
792     }
793     sym_info->Scope = 0; /* FIXME */
794     sym_info->Tag = sym->tag;
795     name = symt_get_name(sym);
796     if (sym_info->MaxNameLen)
797     {
798         if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
799             (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
800                                                       sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
801         {
802             sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
803             memcpy(sym_info->Name, name, sym_info->NameLen);
804             sym_info->Name[sym_info->NameLen] = '\0';
805         }
806     }
807     TRACE_(dbghelp_symt)("%p => %s %u %s\n",
808                          sym, sym_info->Name, sym_info->Size,
809                          wine_dbgstr_longlong(sym_info->Address));
810 }
811
812 struct sym_enum
813 {
814     PSYM_ENUMERATESYMBOLS_CALLBACK      cb;
815     PVOID                               user;
816     SYMBOL_INFO*                        sym_info;
817     DWORD                               index;
818     DWORD                               tag;
819     DWORD64                             addr;
820     char                                buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
821 };
822
823 static BOOL send_symbol(const struct sym_enum* se, struct module_pair* pair,
824                         const struct symt_function* func, const struct symt* sym)
825 {
826     symt_fill_sym_info(pair, func, sym, se->sym_info);
827     if (se->index && se->sym_info->info != se->index) return FALSE;
828     if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
829     if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
830     return !se->cb(se->sym_info, se->sym_info->Size, se->user);
831 }
832
833 static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
834                              const struct sym_enum* se)
835 {
836     void*                       ptr;
837     struct symt_ht*             sym = NULL;
838     struct hash_table_iter      hti;
839
840     hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
841     while ((ptr = hash_table_iter_up(&hti)))
842     {
843         sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
844         if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
845         {
846             se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
847             se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
848             if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
849         }
850     }   
851     return FALSE;
852 }
853
854 static inline unsigned where_to_insert(struct module* module, unsigned high, const struct symt_ht* elt)
855 {
856     unsigned    low = 0, mid = high / 2;
857     ULONG64     addr;
858
859     if (!high) return 0;
860     symt_get_info(module, &elt->symt, TI_GET_ADDRESS, &addr);
861     do
862     {
863         switch (cmp_sorttab_addr(module, mid, addr))
864         {
865         case 0: return mid;
866         case -1: low = mid + 1; break;
867         case 1: high = mid; break;
868         }
869         mid = low + (high - low) / 2;
870     } while (low < high);
871     return mid;
872 }
873
874 /***********************************************************************
875  *              resort_symbols
876  *
877  * Rebuild sorted list of symbols for a module.
878  */
879 static BOOL resort_symbols(struct module* module)
880 {
881     if (!(module->module.NumSyms = module->num_symbols))
882         return FALSE;
883
884     /* FIXME: what's the optimal value here ??? */
885     if (module->num_sorttab && module->num_symbols <= module->num_sorttab + 30)
886     {
887         int     i, delta, ins_idx = module->num_sorttab, prev_ins_idx;
888         struct symt_ht* tmp[30];
889
890         delta = module->num_symbols - module->num_sorttab;
891         memcpy(tmp, &module->addr_sorttab[module->num_sorttab], delta * sizeof(struct symt_ht*));
892         symt_cmp_addr_module = module;
893         qsort(tmp, delta, sizeof(struct symt_ht*), symt_cmp_addr);
894
895         for (i = delta - 1; i >= 0; i--)
896         {
897             prev_ins_idx = ins_idx;
898             ins_idx = where_to_insert(module, prev_ins_idx = ins_idx, tmp[i]);
899             memmove(&module->addr_sorttab[ins_idx + i + 1],
900                     &module->addr_sorttab[ins_idx],
901                     (prev_ins_idx - ins_idx) * sizeof(struct symt_ht*));
902             module->addr_sorttab[ins_idx + i] = tmp[i];
903         }
904     }
905     else
906     {
907         symt_cmp_addr_module = module;
908         qsort(module->addr_sorttab, module->num_symbols, sizeof(struct symt_ht*), symt_cmp_addr);
909     }
910     module->num_sorttab = module->num_symbols;
911     return module->sortlist_valid = TRUE;
912 }
913
914 static void symt_get_length(struct module* module, const struct symt* symt, ULONG64* size)
915 {
916     DWORD       type_index;
917
918     if (symt_get_info(module,  symt, TI_GET_LENGTH, size) && *size)
919         return;
920
921     if (symt_get_info(module, symt, TI_GET_TYPE, &type_index) &&
922         symt_get_info(module, symt_index2ptr(module, type_index), TI_GET_LENGTH, size)) return;
923     *size = 0x1000; /* arbitrary value */
924 }
925
926 /* assume addr is in module */
927 struct symt_ht* symt_find_nearest(struct module* module, DWORD_PTR addr)
928 {
929     int         mid, high, low;
930     ULONG64     ref_addr, ref_size;
931
932     if (!module->sortlist_valid || !module->addr_sorttab)
933     {
934         if (!resort_symbols(module)) return NULL;
935     }
936
937     /*
938      * Binary search to find closest symbol.
939      */
940     low = 0;
941     high = module->num_sorttab;
942
943     symt_get_info(module, &module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
944     if (addr < ref_addr) return NULL;
945     if (high)
946     {
947         symt_get_info(module, &module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
948         symt_get_length(module, &module->addr_sorttab[high - 1]->symt, &ref_size);
949         if (addr >= ref_addr + ref_size) return NULL;
950     }
951     
952     while (high > low + 1)
953     {
954         mid = (high + low) / 2;
955         if (cmp_sorttab_addr(module, mid, addr) < 0)
956             low = mid;
957         else
958             high = mid;
959     }
960     if (low != high && high != module->num_sorttab &&
961         cmp_sorttab_addr(module, high, addr) <= 0)
962         low = high;
963
964     /* If found symbol is a public symbol, check if there are any other entries that
965      * might also have the same address, but would get better information
966      */
967     if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
968     {   
969         symt_get_info(module, &module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
970         if (low > 0 &&
971             module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
972             !cmp_sorttab_addr(module, low - 1, ref_addr))
973             low--;
974         else if (low < module->num_sorttab - 1 &&
975                  module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
976                  !cmp_sorttab_addr(module, low + 1, ref_addr))
977             low++;
978     }
979     /* finally check that we fit into the found symbol */
980     symt_get_info(module, &module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
981     if (addr < ref_addr) return NULL;
982     symt_get_length(module, &module->addr_sorttab[low]->symt, &ref_size);
983     if (addr >= ref_addr + ref_size) return NULL;
984
985     return module->addr_sorttab[low];
986 }
987
988 static BOOL symt_enum_locals_helper(struct module_pair* pair,
989                                     regex_t* preg, const struct sym_enum* se,
990                                     struct symt_function* func, const struct vector* v)
991 {
992     struct symt*        lsym = NULL;
993     DWORD               pc = pair->pcs->ctx_frame.InstructionOffset;
994     unsigned int        i;
995
996     for (i=0; i<vector_length(v); i++)
997     {
998         lsym = *(struct symt**)vector_at(v, i);
999         switch (lsym->tag)
1000         {
1001         case SymTagBlock:
1002             {
1003                 struct symt_block*  block = (struct symt_block*)lsym;
1004                 if (pc < block->address || block->address + block->size <= pc)
1005                     continue;
1006                 if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
1007                     return FALSE;
1008             }
1009             break;
1010         case SymTagData:
1011             if (match_regexp(preg, symt_get_name(lsym)))
1012             {
1013                 if (send_symbol(se, pair, func, lsym)) return FALSE;
1014             }
1015             break;
1016         case SymTagLabel:
1017         case SymTagFuncDebugStart:
1018         case SymTagFuncDebugEnd:
1019         case SymTagCustom:
1020             break;
1021         default:
1022             FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
1023             assert(0);
1024         }
1025     }
1026     return TRUE;
1027 }
1028
1029 static BOOL symt_enum_locals(struct process* pcs, const char* mask, 
1030                              const struct sym_enum* se)
1031 {
1032     struct module_pair  pair;
1033     struct symt_ht*     sym;
1034     DWORD_PTR           pc = pcs->ctx_frame.InstructionOffset;
1035
1036     se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
1037     se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
1038
1039     pair.pcs = pcs;
1040     pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
1041     if (!module_get_debug(&pair)) return FALSE;
1042     if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
1043
1044     if (sym->symt.tag == SymTagFunction)
1045     {
1046         BOOL            ret;
1047         regex_t         preg;
1048
1049         compile_regex(mask ? mask : "*", -1, &preg,
1050                       dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1051         ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
1052                                       &((struct symt_function*)sym)->vchildren);
1053         regfree(&preg);
1054         return ret;
1055     }
1056     return FALSE;
1057 }
1058
1059 /******************************************************************
1060  *              copy_symbolW
1061  *
1062  * Helper for transforming an ANSI symbol info into a UNICODE one.
1063  * Assume that MaxNameLen is the same for both version (A & W).
1064  */
1065 void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
1066 {
1067     siw->SizeOfStruct = si->SizeOfStruct;
1068     siw->TypeIndex = si->TypeIndex; 
1069     siw->Reserved[0] = si->Reserved[0];
1070     siw->Reserved[1] = si->Reserved[1];
1071     siw->Index = si->info; /* FIXME: see dbghelp.h */
1072     siw->Size = si->Size;
1073     siw->ModBase = si->ModBase;
1074     siw->Flags = si->Flags;
1075     siw->Value = si->Value;
1076     siw->Address = si->Address;
1077     siw->Register = si->Register;
1078     siw->Scope = si->Scope;
1079     siw->Tag = si->Tag;
1080     siw->NameLen = si->NameLen;
1081     siw->MaxNameLen = si->MaxNameLen;
1082     MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
1083 }
1084
1085 /******************************************************************
1086  *              sym_enum
1087  *
1088  * Core routine for most of the enumeration of symbols
1089  */
1090 static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1091                      const struct sym_enum* se)
1092 {
1093     struct module_pair  pair;
1094     const char*         bang;
1095     regex_t             mod_regex, sym_regex;
1096
1097     pair.pcs = process_find_by_handle(hProcess);
1098     if (!pair.pcs) return FALSE;
1099     if (BaseOfDll == 0)
1100     {
1101         /* do local variables ? */
1102         if (!Mask || !(bang = strchr(Mask, '!')))
1103             return symt_enum_locals(pair.pcs, Mask, se);
1104
1105         if (bang == Mask) return FALSE;
1106
1107         compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
1108         compile_regex(bang + 1, -1, &sym_regex, 
1109                       dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1110         
1111         for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1112         {
1113             if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1114             {
1115                 if (match_regexp(&mod_regex, pair.requested->module_name) &&
1116                     symt_enum_module(&pair, &sym_regex, se))
1117                     break;
1118             }
1119         }
1120         /* not found in PE modules, retry on the ELF ones
1121          */
1122         if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1123         {
1124             for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1125             {
1126                 if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1127                     !module_get_containee(pair.pcs, pair.requested) &&
1128                     module_get_debug(&pair))
1129                 {
1130                     if (match_regexp(&mod_regex, pair.requested->module_name) &&
1131                         symt_enum_module(&pair, &sym_regex, se))
1132                     break;
1133                 }
1134             }
1135         }
1136         regfree(&mod_regex);
1137         regfree(&sym_regex);
1138         return TRUE;
1139     }
1140     pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1141     if (!module_get_debug(&pair))
1142         return FALSE;
1143
1144     /* we always ignore module name from Mask when BaseOfDll is defined */
1145     if (Mask && (bang = strchr(Mask, '!')))
1146     {
1147         if (bang == Mask) return FALSE;
1148         Mask = bang + 1;
1149     }
1150
1151     compile_regex(Mask ? Mask : "*", -1, &sym_regex, 
1152                   dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1153     symt_enum_module(&pair, &sym_regex, se);
1154     regfree(&sym_regex);
1155
1156     return TRUE;
1157 }
1158
1159 /******************************************************************
1160  *              SymEnumSymbols (DBGHELP.@)
1161  *
1162  * cases BaseOfDll = 0
1163  *      !foo fails always (despite what MSDN states)
1164  *      RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
1165  *      no ! in Mask, lookup in local Context
1166  * cases BaseOfDll != 0
1167  *      !foo fails always (despite what MSDN states)
1168  *      RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
1169  */
1170 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
1171                            PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1172                            PVOID UserContext)
1173 {
1174     struct sym_enum     se;
1175
1176     TRACE("(%p %s %s %p %p)\n", 
1177           hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
1178           EnumSymbolsCallback, UserContext);
1179
1180     se.cb = EnumSymbolsCallback;
1181     se.user = UserContext;
1182     se.index = 0;
1183     se.tag = 0;
1184     se.addr = 0;
1185     se.sym_info = (PSYMBOL_INFO)se.buffer;
1186
1187     return sym_enum(hProcess, BaseOfDll, Mask, &se);
1188 }
1189
1190 struct sym_enumW
1191 {
1192     PSYM_ENUMERATESYMBOLS_CALLBACKW     cb;
1193     void*                               ctx;
1194     PSYMBOL_INFOW                       sym_info;
1195     char                                buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];
1196
1197 };
1198     
1199 static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
1200 {
1201     struct sym_enumW*   sew = ctx;
1202
1203     copy_symbolW(sew->sym_info, si);
1204
1205     return (sew->cb)(sew->sym_info, size, sew->ctx);
1206 }
1207
1208 /******************************************************************
1209  *              SymEnumSymbolsW (DBGHELP.@)
1210  *
1211  */
1212 BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
1213                             PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1214                             PVOID UserContext)
1215 {
1216     struct sym_enumW    sew;
1217     BOOL                ret = FALSE;
1218     char*               maskA = NULL;
1219
1220     sew.ctx = UserContext;
1221     sew.cb = EnumSymbolsCallback;
1222     sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1223
1224     if (Mask)
1225     {
1226         unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1227         maskA = HeapAlloc(GetProcessHeap(), 0, len);
1228         if (!maskA) return FALSE;
1229         WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1230     }
1231     ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
1232     HeapFree(GetProcessHeap(), 0, maskA);
1233
1234     return ret;
1235 }
1236
1237 struct sym_enumerate
1238 {
1239     void*                       ctx;
1240     PSYM_ENUMSYMBOLS_CALLBACK   cb;
1241 };
1242
1243 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1244 {
1245     struct sym_enumerate*       se = ctx;
1246     return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1247 }
1248
1249 /***********************************************************************
1250  *              SymEnumerateSymbols (DBGHELP.@)
1251  */
1252 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
1253                                 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback, 
1254                                 PVOID UserContext)
1255 {
1256     struct sym_enumerate        se;
1257
1258     se.ctx = UserContext;
1259     se.cb  = EnumSymbolsCallback;
1260     
1261     return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
1262 }
1263
1264 struct sym_enumerate64
1265 {
1266     void*                       ctx;
1267     PSYM_ENUMSYMBOLS_CALLBACK64 cb;
1268 };
1269
1270 static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
1271 {
1272     struct sym_enumerate64*     se = ctx;
1273     return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
1274 }
1275
1276 /***********************************************************************
1277  *              SymEnumerateSymbols64 (DBGHELP.@)
1278  */
1279 BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
1280                                   PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
1281                                   PVOID UserContext)
1282 {
1283     struct sym_enumerate64      se;
1284
1285     se.ctx = UserContext;
1286     se.cb  = EnumSymbolsCallback;
1287
1288     return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
1289 }
1290
1291 /******************************************************************
1292  *              SymFromAddr (DBGHELP.@)
1293  *
1294  */
1295 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address, 
1296                         DWORD64* Displacement, PSYMBOL_INFO Symbol)
1297 {
1298     struct module_pair  pair;
1299     struct symt_ht*     sym;
1300
1301     pair.pcs = process_find_by_handle(hProcess);
1302     if (!pair.pcs) return FALSE;
1303     pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
1304     if (!module_get_debug(&pair)) return FALSE;
1305     if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1306
1307     symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1308     *Displacement = Address - Symbol->Address;
1309     return TRUE;
1310 }
1311
1312 /******************************************************************
1313  *              SymFromAddrW (DBGHELP.@)
1314  *
1315  */
1316 BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address, 
1317                          DWORD64* Displacement, PSYMBOL_INFOW Symbol)
1318 {
1319     PSYMBOL_INFO        si;
1320     unsigned            len;
1321     BOOL                ret;
1322
1323     len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
1324     si = HeapAlloc(GetProcessHeap(), 0, len);
1325     if (!si) return FALSE;
1326
1327     si->SizeOfStruct = sizeof(*si);
1328     si->MaxNameLen = Symbol->MaxNameLen;
1329     if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
1330     {
1331         copy_symbolW(Symbol, si);
1332     }
1333     HeapFree(GetProcessHeap(), 0, si);
1334     return ret;
1335 }
1336
1337 /******************************************************************
1338  *              SymGetSymFromAddr (DBGHELP.@)
1339  *
1340  */
1341 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
1342                               PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
1343 {
1344     char        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1345     SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1346     size_t      len;
1347     DWORD64     Displacement64;
1348
1349     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1350     si->SizeOfStruct = sizeof(*si);
1351     si->MaxNameLen = MAX_SYM_NAME;
1352     if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1353         return FALSE;
1354
1355     if (Displacement)
1356         *Displacement = Displacement64;
1357     Symbol->Address = si->Address;
1358     Symbol->Size    = si->Size;
1359     Symbol->Flags   = si->Flags;
1360     len = min(Symbol->MaxNameLength, si->MaxNameLen);
1361     lstrcpynA(Symbol->Name, si->Name, len);
1362     return TRUE;
1363 }
1364
1365 /******************************************************************
1366  *              SymGetSymFromAddr64 (DBGHELP.@)
1367  *
1368  */
1369 BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
1370                                 PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
1371 {
1372     char        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1373     SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1374     size_t      len;
1375     DWORD64     Displacement64;
1376
1377     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1378     si->SizeOfStruct = sizeof(*si);
1379     si->MaxNameLen = MAX_SYM_NAME;
1380     if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1381         return FALSE;
1382
1383     if (Displacement)
1384         *Displacement = Displacement64;
1385     Symbol->Address = si->Address;
1386     Symbol->Size    = si->Size;
1387     Symbol->Flags   = si->Flags;
1388     len = min(Symbol->MaxNameLength, si->MaxNameLen);
1389     lstrcpynA(Symbol->Name, si->Name, len);
1390     return TRUE;
1391 }
1392
1393 static BOOL find_name(struct process* pcs, struct module* module, const char* name,
1394                       SYMBOL_INFO* symbol)
1395 {
1396     struct hash_table_iter      hti;
1397     void*                       ptr;
1398     struct symt_ht*             sym = NULL;
1399     struct module_pair          pair;
1400
1401     pair.pcs = pcs;
1402     if (!(pair.requested = module)) return FALSE;
1403     if (!module_get_debug(&pair)) return FALSE;
1404
1405     hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
1406     while ((ptr = hash_table_iter_up(&hti)))
1407     {
1408         sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1409
1410         if (!strcmp(sym->hash_elt.name, name))
1411         {
1412             symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
1413             return TRUE;
1414         }
1415     }
1416     return FALSE;
1417
1418 }
1419 /******************************************************************
1420  *              SymFromName (DBGHELP.@)
1421  *
1422  */
1423 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1424 {
1425     struct process*             pcs = process_find_by_handle(hProcess);
1426     struct module*              module;
1427     const char*                 name;
1428
1429     TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
1430     if (!pcs) return FALSE;
1431     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1432     name = strchr(Name, '!');
1433     if (name)
1434     {
1435         char    tmp[128];
1436         assert(name - Name < sizeof(tmp));
1437         memcpy(tmp, Name, name - Name);
1438         tmp[name - Name] = '\0';
1439         module = module_find_by_nameA(pcs, tmp);
1440         return find_name(pcs, module, name + 1, Symbol);
1441     }
1442     for (module = pcs->lmodules; module; module = module->next)
1443     {
1444         if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
1445             return TRUE;
1446     }
1447     /* not found in PE modules, retry on the ELF ones
1448      */
1449     if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
1450     {
1451         for (module = pcs->lmodules; module; module = module->next)
1452         {
1453             if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
1454                 !module_get_containee(pcs, module) &&
1455                 find_name(pcs, module, Name, Symbol))
1456                 return TRUE;
1457         }
1458     }
1459     return FALSE;
1460 }
1461
1462 /***********************************************************************
1463  *              SymGetSymFromName64 (DBGHELP.@)
1464  */
1465 BOOL WINAPI SymGetSymFromName64(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL64 Symbol)
1466 {
1467     char        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1468     SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1469     size_t      len;
1470
1471     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1472     si->SizeOfStruct = sizeof(*si);
1473     si->MaxNameLen = MAX_SYM_NAME;
1474     if (!SymFromName(hProcess, Name, si)) return FALSE;
1475
1476     Symbol->Address = si->Address;
1477     Symbol->Size    = si->Size;
1478     Symbol->Flags   = si->Flags;
1479     len = min(Symbol->MaxNameLength, si->MaxNameLen);
1480     lstrcpynA(Symbol->Name, si->Name, len);
1481     return TRUE;
1482 }
1483
1484 /***********************************************************************
1485  *              SymGetSymFromName (DBGHELP.@)
1486  */
1487 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1488 {
1489     char        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1490     SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1491     size_t      len;
1492
1493     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1494     si->SizeOfStruct = sizeof(*si);
1495     si->MaxNameLen = MAX_SYM_NAME;
1496     if (!SymFromName(hProcess, Name, si)) return FALSE;
1497
1498     Symbol->Address = si->Address;
1499     Symbol->Size    = si->Size;
1500     Symbol->Flags   = si->Flags;
1501     len = min(Symbol->MaxNameLength, si->MaxNameLen);
1502     lstrcpynA(Symbol->Name, si->Name, len);
1503     return TRUE;
1504 }
1505
1506 /******************************************************************
1507  *              sym_fill_func_line_info
1508  *
1509  * fills information about a file
1510  */
1511 BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1512                               DWORD64 addr, IMAGEHLP_LINE64* line)
1513 {
1514     struct line_info*   dli = NULL;
1515     BOOL                found = FALSE;
1516     int                 i;
1517
1518     assert(func->symt.tag == SymTagFunction);
1519
1520     for (i=vector_length(&func->vlines)-1; i>=0; i--)
1521     {
1522         dli = vector_at(&func->vlines, i);
1523         if (!dli->is_source_file)
1524         {
1525             if (found || dli->u.pc_offset > addr) continue;
1526             line->LineNumber = dli->line_number;
1527             line->Address    = dli->u.pc_offset;
1528             line->Key        = dli;
1529             found = TRUE;
1530             continue;
1531         }
1532         if (found)
1533         {
1534             line->FileName = (char*)source_get(module, dli->u.source_file);
1535             return TRUE;
1536         }
1537     }
1538     return FALSE;
1539 }
1540
1541 /***********************************************************************
1542  *              SymGetSymNext64 (DBGHELP.@)
1543  */
1544 BOOL WINAPI SymGetSymNext64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1545 {
1546     /* algo:
1547      * get module from Symbol.Address
1548      * get index in module.addr_sorttab of Symbol.Address
1549      * increment index
1550      * if out of module bounds, move to next module in process address space
1551      */
1552     FIXME("(%p, %p): stub\n", hProcess, Symbol);
1553     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1554     return FALSE;
1555 }
1556
1557 /***********************************************************************
1558  *              SymGetSymNext (DBGHELP.@)
1559  */
1560 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1561 {
1562     FIXME("(%p, %p): stub\n", hProcess, Symbol);
1563     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1564     return FALSE;
1565 }
1566
1567 /***********************************************************************
1568  *              SymGetSymPrev64 (DBGHELP.@)
1569  */
1570 BOOL WINAPI SymGetSymPrev64(HANDLE hProcess, PIMAGEHLP_SYMBOL64 Symbol)
1571 {
1572     FIXME("(%p, %p): stub\n", hProcess, Symbol);
1573     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1574     return FALSE;
1575 }
1576
1577 /***********************************************************************
1578  *              SymGetSymPrev (DBGHELP.@)
1579  */
1580 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1581 {
1582     FIXME("(%p, %p): stub\n", hProcess, Symbol);
1583     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1584     return FALSE;
1585 }
1586
1587 /******************************************************************
1588  *              copy_line_64_from_32 (internal)
1589  *
1590  */
1591 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1592
1593 {
1594     l64->Key = l32->Key;
1595     l64->LineNumber = l32->LineNumber;
1596     l64->FileName = l32->FileName;
1597     l64->Address = l32->Address;
1598 }
1599
1600 /******************************************************************
1601  *              copy_line_W64_from_32 (internal)
1602  *
1603  */
1604 static void copy_line_W64_from_64(struct process* pcs, IMAGEHLP_LINEW64* l64w, const IMAGEHLP_LINE64* l64)
1605 {
1606     unsigned len;
1607
1608     l64w->Key = l64->Key;
1609     l64w->LineNumber = l64->LineNumber;
1610     len = MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, NULL, 0);
1611     if ((l64w->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
1612         MultiByteToWideChar(CP_ACP, 0, l64->FileName, -1, l64w->FileName, len);
1613     l64w->Address = l64->Address;
1614 }
1615
1616 /******************************************************************
1617  *              copy_line_32_from_64 (internal)
1618  *
1619  */
1620 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1621
1622 {
1623     l32->Key = l64->Key;
1624     l32->LineNumber = l64->LineNumber;
1625     l32->FileName = l64->FileName;
1626     l32->Address = l64->Address;
1627 }
1628
1629 /******************************************************************
1630  *              SymGetLineFromAddr (DBGHELP.@)
1631  *
1632  */
1633 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr,
1634                                PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1635 {
1636     IMAGEHLP_LINE64     il64;
1637
1638     il64.SizeOfStruct = sizeof(il64);
1639     if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1640         return FALSE;
1641     copy_line_32_from_64(Line, &il64);
1642     return TRUE;
1643 }
1644
1645 /******************************************************************
1646  *              SymGetLineFromAddr64 (DBGHELP.@)
1647  *
1648  */
1649 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr, 
1650                                  PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1651 {
1652     struct module_pair  pair;
1653     struct symt_ht*     symt;
1654
1655     TRACE("%p %s %p %p\n", hProcess, wine_dbgstr_longlong(dwAddr), pdwDisplacement, Line);
1656
1657     if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1658
1659     pair.pcs = process_find_by_handle(hProcess);
1660     if (!pair.pcs) return FALSE;
1661     pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
1662     if (!module_get_debug(&pair)) return FALSE;
1663     if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1664
1665     if (symt->symt.tag != SymTagFunction) return FALSE;
1666     if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1667                                   dwAddr, Line)) return FALSE;
1668     *pdwDisplacement = dwAddr - Line->Address;
1669     return TRUE;
1670 }
1671
1672 /******************************************************************
1673  *              SymGetLineFromAddrW64 (DBGHELP.@)
1674  *
1675  */
1676 BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr, 
1677                                   PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
1678 {
1679     IMAGEHLP_LINE64     il64;
1680
1681     il64.SizeOfStruct = sizeof(il64);
1682     if (!SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, &il64))
1683         return FALSE;
1684     copy_line_W64_from_64(process_find_by_handle(hProcess), Line, &il64);
1685     return TRUE;
1686 }
1687
1688 /******************************************************************
1689  *              SymGetLinePrev64 (DBGHELP.@)
1690  *
1691  */
1692 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1693 {
1694     struct module_pair  pair;
1695     struct line_info*   li;
1696     BOOL                in_search = FALSE;
1697
1698     TRACE("(%p %p)\n", hProcess, Line);
1699
1700     if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1701
1702     pair.pcs = process_find_by_handle(hProcess);
1703     if (!pair.pcs) return FALSE;
1704     pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1705     if (!module_get_debug(&pair)) return FALSE;
1706
1707     if (Line->Key == 0) return FALSE;
1708     li = Line->Key;
1709     /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1710      * element we have to go back until we find the prev one to get the real
1711      * source file name for the DLIT_OFFSET element just before 
1712      * the first DLIT_SOURCEFILE
1713      */
1714     while (!li->is_first)
1715     {
1716         li--;
1717         if (!li->is_source_file)
1718         {
1719             Line->LineNumber = li->line_number;
1720             Line->Address    = li->u.pc_offset;
1721             Line->Key        = li;
1722             if (!in_search) return TRUE;
1723         }
1724         else
1725         {
1726             if (in_search)
1727             {
1728                 Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1729                 return TRUE;
1730             }
1731             in_search = TRUE;
1732         }
1733     }
1734     SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1735     return FALSE;
1736 }
1737
1738 /******************************************************************
1739  *              SymGetLinePrev (DBGHELP.@)
1740  *
1741  */
1742 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1743 {
1744     IMAGEHLP_LINE64     line64;
1745
1746     line64.SizeOfStruct = sizeof(line64);
1747     copy_line_64_from_32(&line64, Line);
1748     if (!SymGetLinePrev64(hProcess, &line64)) return FALSE;
1749     copy_line_32_from_64(Line, &line64);
1750     return TRUE;
1751 }
1752
1753 BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE64 line)
1754 {
1755     struct line_info*   li;
1756
1757     if (line->Key == 0) return FALSE;
1758     li = line->Key;
1759     while (!li->is_last)
1760     {
1761         li++;
1762         if (!li->is_source_file)
1763         {
1764             line->LineNumber = li->line_number;
1765             line->Address    = li->u.pc_offset;
1766             line->Key        = li;
1767             return TRUE;
1768         }
1769         line->FileName = (char*)source_get(module, li->u.source_file);
1770     }
1771     return FALSE;
1772 }
1773
1774 /******************************************************************
1775  *              SymGetLineNext64 (DBGHELP.@)
1776  *
1777  */
1778 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1779 {
1780     struct module_pair  pair;
1781
1782     TRACE("(%p %p)\n", hProcess, Line);
1783
1784     if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1785     pair.pcs = process_find_by_handle(hProcess);
1786     if (!pair.pcs) return FALSE;
1787     pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
1788     if (!module_get_debug(&pair)) return FALSE;
1789
1790     if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1791     SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1792     return FALSE;
1793 }
1794
1795 /******************************************************************
1796  *              SymGetLineNext (DBGHELP.@)
1797  *
1798  */
1799 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1800 {
1801     IMAGEHLP_LINE64     line64;
1802
1803     line64.SizeOfStruct = sizeof(line64);
1804     copy_line_64_from_32(&line64, Line);
1805     if (!SymGetLineNext64(hProcess, &line64)) return FALSE;
1806     copy_line_32_from_64(Line, &line64);
1807     return TRUE;
1808 }
1809
1810 /***********************************************************************
1811  *              SymUnDName (DBGHELP.@)
1812  */
1813 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1814 {
1815     return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1816                                 UNDNAME_COMPLETE) != 0;
1817 }
1818
1819 /***********************************************************************
1820  *              SymUnDName64 (DBGHELP.@)
1821  */
1822 BOOL WINAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym, PSTR UnDecName, DWORD UnDecNameLength)
1823 {
1824     return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1825                                 UNDNAME_COMPLETE) != 0;
1826 }
1827
1828 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1829 static void  und_free (void* ptr)  { HeapFree(GetProcessHeap(), 0, ptr); }
1830
1831 /***********************************************************************
1832  *              UnDecorateSymbolName (DBGHELP.@)
1833  */
1834 DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1835                                   DWORD UndecoratedLength, DWORD Flags)
1836 {
1837     /* undocumented from msvcrt */
1838     static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1839     static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1840
1841     TRACE("(%s, %p, %d, 0x%08x)\n",
1842           debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1843
1844     if (!p_undname)
1845     {
1846         if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1847         if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1848         if (!p_undname) return 0;
1849     }
1850
1851     if (!UnDecoratedName) return 0;
1852     if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength, 
1853                    und_alloc, und_free, Flags))
1854         return 0;
1855     return strlen(UnDecoratedName);
1856 }
1857
1858 /******************************************************************
1859  *              SymMatchString (DBGHELP.@)
1860  *
1861  */
1862 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1863 {
1864     regex_t     preg;
1865     BOOL        ret;
1866
1867     TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1868
1869     compile_regex(re, -1, &preg, _case);
1870     ret = match_regexp(&preg, string);
1871     regfree(&preg);
1872     return ret;
1873 }
1874
1875 /******************************************************************
1876  *              SymSearch (DBGHELP.@)
1877  */
1878 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1879                       DWORD SymTag, PCSTR Mask, DWORD64 Address,
1880                       PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1881                       PVOID UserContext, DWORD Options)
1882 {
1883     struct sym_enum     se;
1884
1885     TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1886           hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1887           wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1888           UserContext, Options);
1889
1890     if (Options != SYMSEARCH_GLOBALSONLY)
1891     {
1892         FIXME("Unsupported searching with options (%x)\n", Options);
1893         SetLastError(ERROR_INVALID_PARAMETER);
1894         return FALSE;
1895     }
1896
1897     se.cb = EnumSymbolsCallback;
1898     se.user = UserContext;
1899     se.index = Index;
1900     se.tag = SymTag;
1901     se.addr = Address;
1902     se.sym_info = (PSYMBOL_INFO)se.buffer;
1903
1904     return sym_enum(hProcess, BaseOfDll, Mask, &se);
1905 }
1906
1907 /******************************************************************
1908  *              SymSearchW (DBGHELP.@)
1909  */
1910 BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1911                        DWORD SymTag, PCWSTR Mask, DWORD64 Address,
1912                        PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
1913                        PVOID UserContext, DWORD Options)
1914 {
1915     struct sym_enumW    sew;
1916     BOOL                ret = FALSE;
1917     char*               maskA = NULL;
1918
1919     TRACE("(%p %s %u %u %s %s %p %p %x)\n",
1920           hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1921           wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1922           UserContext, Options);
1923
1924     sew.ctx = UserContext;
1925     sew.cb = EnumSymbolsCallback;
1926     sew.sym_info = (PSYMBOL_INFOW)sew.buffer;
1927
1928     if (Mask)
1929     {
1930         unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
1931         maskA = HeapAlloc(GetProcessHeap(), 0, len);
1932         if (!maskA) return FALSE;
1933         WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
1934     }
1935     ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
1936                     sym_enumW, &sew, Options);
1937     HeapFree(GetProcessHeap(), 0, maskA);
1938
1939     return ret;
1940 }
1941
1942 /******************************************************************
1943  *              SymAddSymbol (DBGHELP.@)
1944  *
1945  */
1946 BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
1947                          DWORD64 addr, DWORD size, DWORD flags)
1948 {
1949     WCHAR       nameW[MAX_SYM_NAME];
1950
1951     MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
1952     return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
1953 }
1954
1955 /******************************************************************
1956  *              SymAddSymbolW (DBGHELP.@)
1957  *
1958  */
1959 BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
1960                           DWORD64 addr, DWORD size, DWORD flags)
1961 {
1962     struct module_pair  pair;
1963
1964     TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);
1965
1966     pair.pcs = process_find_by_handle(hProcess);
1967     if (!pair.pcs) return FALSE;
1968     pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
1969     if (!module_get_debug(&pair)) return FALSE;
1970
1971     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1972     return FALSE;
1973 }
1974
1975 /******************************************************************
1976  *              SymSetScopeFromAddr (DBGHELP.@)
1977  */
1978 BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
1979 {
1980     struct process*     pcs;
1981
1982     FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));
1983
1984     if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1985     return TRUE;
1986 }
1987
1988 /******************************************************************
1989  *              SymEnumLines (DBGHELP.@)
1990  *
1991  */
1992 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
1993                          PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
1994 {
1995     struct module_pair          pair;
1996     struct hash_table_iter      hti;
1997     struct symt_ht*             sym;
1998     regex_t                     re;
1999     struct line_info*           dli;
2000     void*                       ptr;
2001     SRCCODEINFO                 sci;
2002     const char*                 file;
2003
2004     if (!cb) return FALSE;
2005     if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
2006
2007     pair.pcs = process_find_by_handle(hProcess);
2008     if (!pair.pcs) return FALSE;
2009     if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
2010     pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
2011     if (!module_get_debug(&pair)) return FALSE;
2012     if (!compile_file_regex(&re, srcfile)) return FALSE;
2013
2014     sci.SizeOfStruct = sizeof(sci);
2015     sci.ModBase      = base;
2016
2017     hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
2018     while ((ptr = hash_table_iter_up(&hti)))
2019     {
2020         unsigned int    i;
2021
2022         sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
2023         if (sym->symt.tag != SymTagFunction) continue;
2024
2025         sci.FileName[0] = '\0';
2026         for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
2027         {
2028             dli = vector_at(&((struct symt_function*)sym)->vlines, i);
2029             if (dli->is_source_file)
2030             {
2031                 file = source_get(pair.effective, dli->u.source_file);
2032                 if (!match_regexp(&re, file)) file = "";
2033                 strcpy(sci.FileName, file);
2034             }
2035             else if (sci.FileName[0])
2036             {
2037                 sci.Key = dli;
2038                 sci.Obj[0] = '\0'; /* FIXME */
2039                 sci.LineNumber = dli->line_number;
2040                 sci.Address = dli->u.pc_offset;
2041                 if (!cb(&sci, user)) break;
2042             }
2043         }
2044     }
2045     regfree(&re);
2046     return TRUE;
2047 }
2048
2049 BOOL WINAPI SymGetLineFromName(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2050                 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINE Line)
2051 {
2052     FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2053                 dwLineNumber, plDisplacement, Line);
2054     return FALSE;
2055 }
2056
2057 BOOL WINAPI SymGetLineFromName64(HANDLE hProcess, PCSTR ModuleName, PCSTR FileName,
2058                 DWORD dwLineNumber, PLONG lpDisplacement, PIMAGEHLP_LINE64 Line)
2059 {
2060     FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, ModuleName, FileName,
2061                 dwLineNumber, lpDisplacement, Line);
2062     return FALSE;
2063 }
2064
2065 BOOL WINAPI SymGetLineFromNameW64(HANDLE hProcess, PCWSTR ModuleName, PCWSTR FileName,
2066                 DWORD dwLineNumber, PLONG plDisplacement, PIMAGEHLP_LINEW64 Line)
2067 {
2068     FIXME("(%p) (%s, %s, %d %p %p): stub\n", hProcess, debugstr_w(ModuleName), debugstr_w(FileName),
2069                 dwLineNumber, plDisplacement, Line);
2070     return FALSE;
2071 }