comctl32: A couple fixes for tab icon offsets.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
40 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
41 WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
42
43 inline static int cmp_addr(ULONG64 a1, ULONG64 a2)
44 {
45     if (a1 > a2) return 1;
46     if (a1 < a2) return -1;
47     return 0;
48 }
49
50 inline static int cmp_sorttab_addr(const struct module* module, int idx, ULONG64 addr)
51 {
52     ULONG64     ref;
53
54     symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
55     return cmp_addr(ref, addr);
56 }
57
58 int symt_cmp_addr(const void* p1, const void* p2)
59 {
60     const struct symt*  sym1 = *(const struct symt* const *)p1;
61     const struct symt*  sym2 = *(const struct symt* const *)p2;
62     ULONG64     a1, a2;
63
64     symt_get_info(sym1, TI_GET_ADDRESS, &a1);
65     symt_get_info(sym2, TI_GET_ADDRESS, &a2);
66     return cmp_addr(a1, a2);
67 }
68
69 static inline void re_append(char** mask, unsigned* len, char ch)
70 {
71     *mask = HeapReAlloc(GetProcessHeap(), 0, *mask, ++(*len));
72     (*mask)[*len - 2] = ch;
73 }
74
75 /* transforms a dbghelp's regular expression into a POSIX one
76  * Here are the valid dbghelp reg ex characters:
77  *      *       0 or more characters
78  *      ?       a single character
79  *      []      list
80  *      #       0 or more of preceding char
81  *      +       1 or more of preceding char
82  *      escapes \ on #, ?, [, ], *, +. don't work on -
83  */
84 static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
85 {
86     char*       mask = HeapAlloc(GetProcessHeap(), 0, 1);
87     unsigned    len = 1;
88     BOOL        in_escape = FALSE;
89     unsigned    flags = REG_NOSUB;
90
91     re_append(&mask, &len, '^');
92
93     while (*str && numchar--)
94     {
95         /* FIXME: this shouldn't be valid on '-' */
96         if (in_escape)
97         {
98             re_append(&mask, &len, '\\');
99             re_append(&mask, &len, *str);
100             in_escape = FALSE;
101         }
102         else switch (*str)
103         {
104         case '\\': in_escape = TRUE; break;
105         case '*':  re_append(&mask, &len, '.'); re_append(&mask, &len, '*'); break;
106         case '?':  re_append(&mask, &len, '.'); break;
107         case '#':  re_append(&mask, &len, '*'); break;
108         /* escape some valid characters in dbghelp reg exp:s */
109         case '$':  re_append(&mask, &len, '\\'); re_append(&mask, &len, '$'); break;
110         /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
111         default:   re_append(&mask, &len, *str); break;
112         }
113         str++;
114     }
115     if (in_escape)
116     {
117         re_append(&mask, &len, '\\');
118         re_append(&mask, &len, '\\');
119     }
120     re_append(&mask, &len, '$');
121     mask[len - 1] = '\0';
122     if (_case) flags |= REG_ICASE;
123     if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
124     HeapFree(GetProcessHeap(), 0, mask);
125 }
126
127 struct symt_compiland* symt_new_compiland(struct module* module, const char* name)
128 {
129     struct symt_compiland*    sym;
130
131     TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n", 
132                          module->module.ModuleName, name);
133     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
134     {
135         sym->symt.tag = SymTagCompiland;
136         sym->source   = source_new(module, name);
137         vector_init(&sym->vchildren, sizeof(struct symt*), 32);
138     }
139     return sym;
140 }
141
142 struct symt_public* symt_new_public(struct module* module, 
143                                     struct symt_compiland* compiland,
144                                     const char* name,
145                                     unsigned long address, unsigned size,
146                                     BOOL in_code, BOOL is_func)
147 {
148     struct symt_public* sym;
149     struct symt**       p;
150
151     TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n", 
152                          module->module.ModuleName, name, address);
153     if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) && 
154         symt_find_nearest(module, address) != -1)
155         return NULL;
156     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
157     {
158         sym->symt.tag      = SymTagPublicSymbol;
159         sym->hash_elt.name = pool_strdup(&module->pool, name);
160         hash_table_add(&module->ht_symbols, &sym->hash_elt);
161         module->sortlist_valid = FALSE;
162         sym->container     = compiland ? &compiland->symt : NULL;
163         sym->address       = address;
164         sym->size          = size;
165         sym->in_code       = in_code;
166         sym->is_function   = is_func;
167         if (compiland)
168         {
169             p = vector_add(&compiland->vchildren, &module->pool);
170             *p = &sym->symt;
171         }
172     }
173     return sym;
174 }
175
176 struct symt_data* symt_new_global_variable(struct module* module, 
177                                            struct symt_compiland* compiland, 
178                                            const char* name, unsigned is_static,
179                                            unsigned long addr, unsigned long size,
180                                            struct symt* type)
181 {
182     struct symt_data*   sym;
183     struct symt**       p;
184     DWORD64             tsz;
185
186     TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n", 
187                          module->module.ModuleName, name, addr, type);
188     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
189     {
190         sym->symt.tag      = SymTagData;
191         sym->hash_elt.name = pool_strdup(&module->pool, name);
192         hash_table_add(&module->ht_symbols, &sym->hash_elt);
193         module->sortlist_valid = FALSE;
194         sym->kind          = is_static ? DataIsFileStatic : DataIsGlobal;
195         sym->container     = compiland ? &compiland->symt : NULL;
196         sym->type          = type;
197         sym->u.address     = addr;
198         if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
199         {
200             if (tsz != size)
201                 FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
202                       module->module.ModuleName, name, 
203                       wine_dbgstr_longlong(tsz), size);
204         }
205         if (compiland)
206         {
207             p = vector_add(&compiland->vchildren, &module->pool);
208             *p = &sym->symt;
209         }
210     }
211     return sym;
212 }
213
214 struct symt_function* symt_new_function(struct module* module, 
215                                         struct symt_compiland* compiland, 
216                                         const char* name,
217                                         unsigned long addr, unsigned long size,
218                                         struct symt* sig_type)
219 {
220     struct symt_function*       sym;
221     struct symt**               p;
222
223     TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n", 
224                          module->module.ModuleName, name, addr, addr + size - 1);
225
226     assert(!sig_type || sig_type->tag == SymTagFunctionType);
227     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
228     {
229         sym->symt.tag  = SymTagFunction;
230         sym->hash_elt.name = pool_strdup(&module->pool, name);
231         hash_table_add(&module->ht_symbols, &sym->hash_elt);
232         module->sortlist_valid = FALSE;
233         sym->container = &compiland->symt;
234         sym->address   = addr;
235         sym->type      = sig_type;
236         sym->size      = size;
237         vector_init(&sym->vlines,  sizeof(struct line_info), 64);
238         vector_init(&sym->vchildren, sizeof(struct symt*), 8);
239         if (compiland)
240         {
241             p = vector_add(&compiland->vchildren, &module->pool);
242             *p = &sym->symt;
243         }
244     }
245     return sym;
246 }
247
248 void symt_add_func_line(struct module* module, struct symt_function* func,
249                         unsigned source_idx, int line_num, unsigned long offset)
250 {
251     struct line_info*   dli;
252     BOOL                last_matches = FALSE;
253
254     if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;
255
256     TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n", 
257                          func, func->hash_elt.name, offset, 
258                          source_get(module, source_idx), line_num);
259
260     assert(func->symt.tag == SymTagFunction);
261
262     dli = NULL;
263     while ((dli = vector_iter_down(&func->vlines, dli)))
264     {
265         if (dli->is_source_file)
266         {
267             last_matches = (source_idx == dli->u.source_file);
268             break;
269         }
270     }
271
272     if (!last_matches)
273     {
274         /* we shouldn't have line changes on first line of function */
275         dli = vector_add(&func->vlines, &module->pool);
276         dli->is_source_file = 1;
277         dli->is_first       = dli->is_last = 0;
278         dli->line_number    = 0;
279         dli->u.source_file  = source_idx;
280     }
281     dli = vector_add(&func->vlines, &module->pool);
282     dli->is_source_file = 0;
283     dli->is_first       = dli->is_last = 0;
284     dli->line_number    = line_num;
285     dli->u.pc_offset    = func->address + offset;
286 }
287
288 /******************************************************************
289  *              symt_add_func_local
290  *
291  * Adds a new local/parameter to a given function:
292  * If regno it's not 0:
293  *      - then variable is stored in a register
294  *      - if offset is > 0, then it's a parameter to the function (in a register)
295  *      - if offset is = 0, then it's a local variable (in a register)
296  * Otherwise, the variable is stored on the stack:
297  *      - if offset is > 0, then it's a parameter to the function
298  *      - otherwise, it's a local variable
299  * FIXME: this is too i386 centric
300  */
301 struct symt_data* symt_add_func_local(struct module* module, 
302                                       struct symt_function* func, 
303                                       int regno, int offset, 
304                                       struct symt_block* block, 
305                                       struct symt* type, const char* name)
306 {
307     struct symt_data*   locsym;
308     struct symt**       p;
309
310     assert(func);
311     assert(func->symt.tag == SymTagFunction);
312
313     TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n", 
314                          module->module.ModuleName, func->hash_elt.name, 
315                          name, type);
316     locsym = pool_alloc(&module->pool, sizeof(*locsym));
317     locsym->symt.tag      = SymTagData;
318     locsym->hash_elt.name = pool_strdup(&module->pool, name);
319     locsym->hash_elt.next = NULL;
320     locsym->kind          = (offset > 0) ? DataIsParam : DataIsLocal;
321     locsym->container     = &block->symt;
322     locsym->type          = type;
323     if (regno)
324     {
325         locsym->u.s.reg_id = regno;
326         locsym->u.s.offset = 0;
327         locsym->u.s.length = 0;
328     }
329     else
330     {
331         locsym->u.s.reg_id = 0;
332         locsym->u.s.offset = offset * 8;
333         locsym->u.s.length = 0;
334     }
335     if (block)
336         p = vector_add(&block->vchildren, &module->pool);
337     else
338         p = vector_add(&func->vchildren, &module->pool);
339     *p = &locsym->symt;
340     return locsym;
341 }
342
343 struct symt_block* symt_open_func_block(struct module* module, 
344                                         struct symt_function* func,
345                                         struct symt_block* parent_block, 
346                                         unsigned pc, unsigned len)
347 {
348     struct symt_block*  block;
349     struct symt**       p;
350
351     assert(func);
352     assert(func->symt.tag == SymTagFunction);
353
354     assert(!parent_block || parent_block->symt.tag == SymTagBlock);
355     block = pool_alloc(&module->pool, sizeof(*block));
356     block->symt.tag = SymTagBlock;
357     block->address  = func->address + pc;
358     block->size     = len;
359     block->container = parent_block ? &parent_block->symt : &func->symt;
360     vector_init(&block->vchildren, sizeof(struct symt*), 4);
361     if (parent_block)
362         p = vector_add(&parent_block->vchildren, &module->pool);
363     else
364         p = vector_add(&func->vchildren, &module->pool);
365     *p = &block->symt;
366
367     return block;
368 }
369
370 struct symt_block* symt_close_func_block(struct module* module, 
371                                          struct symt_function* func,
372                                          struct symt_block* block, unsigned pc)
373 {
374     assert(func->symt.tag == SymTagFunction);
375
376     if (pc) block->size = func->address + pc - block->address;
377     return (block->container->tag == SymTagBlock) ? 
378         GET_ENTRY(block->container, struct symt_block, symt) : NULL;
379 }
380
381 struct symt_function_point* symt_add_function_point(struct module* module, 
382                                                     struct symt_function* func,
383                                                     enum SymTagEnum point, 
384                                                     unsigned offset, const char* name)
385 {
386     struct symt_function_point* sym;
387     struct symt**               p;
388
389     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
390     {
391         sym->symt.tag = point;
392         sym->parent   = func;
393         sym->offset   = offset;
394         sym->name     = name ? pool_strdup(&module->pool, name) : NULL;
395         p = vector_add(&func->vchildren, &module->pool);
396         *p = &sym->symt;
397     }
398     return sym;
399 }
400
401 BOOL symt_normalize_function(struct module* module, struct symt_function* func)
402 {
403     unsigned            len;
404     struct line_info*   dli;
405
406     assert(func);
407     /* We aren't adding any more locals or line numbers to this function.
408      * Free any spare memory that we might have allocated.
409      */
410     assert(func->symt.tag == SymTagFunction);
411
412 /* EPP     vector_pool_normalize(&func->vlines,    &module->pool); */
413 /* EPP     vector_pool_normalize(&func->vchildren, &module->pool); */
414
415     len = vector_length(&func->vlines);
416     if (len--)
417     {
418         dli = vector_at(&func->vlines,   0);  dli->is_first = 1;
419         dli = vector_at(&func->vlines, len);  dli->is_last  = 1;
420     }
421     return TRUE;
422 }
423
424 struct symt_thunk* symt_new_thunk(struct module* module, 
425                                   struct symt_compiland* compiland, 
426                                   const char* name, THUNK_ORDINAL ord,
427                                   unsigned long addr, unsigned long size)
428 {
429     struct symt_thunk*  sym;
430
431     TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n", 
432                          module->module.ModuleName, name, addr, addr + size - 1);
433
434     if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
435     {
436         sym->symt.tag  = SymTagThunk;
437         sym->hash_elt.name = pool_strdup(&module->pool, name);
438         hash_table_add(&module->ht_symbols, &sym->hash_elt);
439         module->sortlist_valid = FALSE;
440         sym->container = &compiland->symt;
441         sym->address   = addr;
442         sym->size      = size;
443         sym->ordinal   = ord;
444         if (compiland)
445         {
446             struct symt**       p;
447             p = vector_add(&compiland->vchildren, &module->pool);
448             *p = &sym->symt;
449         }
450     }
451     return sym;
452 }
453
454 /* expect sym_info->MaxNameLen to be set before being called */
455 static void symt_fill_sym_info(const struct module* module, 
456                                const struct symt* sym, SYMBOL_INFO* sym_info)
457 {
458     const char* name;
459     DWORD64 size;
460
461     if (!symt_get_info(sym, TI_GET_TYPE, &sym_info->TypeIndex))
462         sym_info->TypeIndex = 0;
463     sym_info->info = (DWORD)sym;
464     sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
465     if (!symt_get_info(sym, TI_GET_LENGTH, &size) &&
466         (!sym_info->TypeIndex ||
467          !symt_get_info((struct symt*)sym_info->TypeIndex, TI_GET_LENGTH, &size)))
468         size = 0;
469     sym_info->Size = (DWORD)size;
470     sym_info->ModBase = module->module.BaseOfImage;
471     sym_info->Flags = 0;
472     sym_info->Value = 0;
473
474     switch (sym->tag)
475     {
476     case SymTagData:
477         {
478             const struct symt_data*  data = (const struct symt_data*)sym;
479             switch (data->kind)
480             {
481             case DataIsParam:
482                 sym_info->Flags |= SYMFLAG_PARAMETER;
483                 /* fall through */
484             case DataIsLocal: 
485                 if (data->u.s.reg_id)
486                 {
487                     sym_info->Flags |= SYMFLAG_REGISTER;
488                     sym_info->Register = data->u.s.reg_id;
489                     sym_info->Address = 0;
490                 }
491                 else
492                 {
493                     sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
494                     /* FIXME: needed ? moreover, it's i386 dependent !!! */
495                     sym_info->Register = CV_REG_EBP;
496                     sym_info->Address = data->u.s.offset / 8;
497                 }
498                 break;
499             case DataIsGlobal:
500             case DataIsFileStatic:
501                 symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
502                 sym_info->Register = 0;
503                 break;
504             case DataIsConstant:
505                 sym_info->Flags |= SYMFLAG_VALUEPRESENT;
506                 switch (data->u.value.n1.n2.vt)
507                 {
508                 case VT_I4:  sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
509                 case VT_I2:  sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
510                 case VT_I1:  sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
511                 case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
512                 case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
513                 case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
514                 default:        
515                     FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
516                 }
517                 break;
518             default:
519                 FIXME("Unhandled kind (%u) in sym data\n", data->kind);
520             }
521         }
522         break;
523     case SymTagPublicSymbol:
524         sym_info->Flags |= SYMFLAG_EXPORT;
525         symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
526         break;
527     case SymTagFunction:
528         sym_info->Flags |= SYMFLAG_FUNCTION;
529         symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
530         break;
531     case SymTagThunk:
532         sym_info->Flags |= SYMFLAG_THUNK;
533         symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
534         break;
535     default:
536         symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
537         sym_info->Register = 0;
538         break;
539     }
540     sym_info->Scope = 0; /* FIXME */
541     sym_info->Tag = sym->tag;
542     name = symt_get_name(sym);
543     if (sym_info->MaxNameLen)
544     {
545         if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
546             (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name, 
547                                                       sym_info->MaxNameLen, UNDNAME_COMPLETE) == 0))
548         {
549             sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
550             memcpy(sym_info->Name, name, sym_info->NameLen);
551             sym_info->Name[sym_info->NameLen] = '\0';
552         }
553     }
554     TRACE_(dbghelp_symt)("%p => %s %lu %s\n",
555                          sym, sym_info->Name, sym_info->Size,
556                          wine_dbgstr_longlong(sym_info->Address));
557 }
558
559 static BOOL symt_enum_module(struct module* module, regex_t* regex,
560                              PSYM_ENUMERATESYMBOLS_CALLBACK cb, PVOID user)
561 {
562     char                        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
563     SYMBOL_INFO*                sym_info = (SYMBOL_INFO*)buffer;
564     void*                       ptr;
565     struct symt_ht*             sym = NULL;
566     struct hash_table_iter      hti;
567
568     hash_table_iter_init(&module->ht_symbols, &hti, NULL);
569     while ((ptr = hash_table_iter_up(&hti)))
570     {
571         sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
572         if (sym->hash_elt.name &&
573             regexec(regex, sym->hash_elt.name, 0, NULL, 0) == 0)
574         {
575             sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
576             sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
577             symt_fill_sym_info(module, &sym->symt, sym_info);
578             if (!cb(sym_info, sym_info->Size, user)) return TRUE;
579         }
580     }   
581     return FALSE;
582 }
583
584 /***********************************************************************
585  *              resort_symbols
586  *
587  * Rebuild sorted list of symbols for a module.
588  */
589 static BOOL resort_symbols(struct module* module)
590 {
591     int                         nsym;
592     void*                       ptr;
593     struct symt_ht*             sym;
594     struct hash_table_iter      hti;
595
596     if (!module_compute_num_syms(module)) return FALSE;
597     
598     if (module->addr_sorttab)
599         module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
600                                            module->addr_sorttab, 
601                                            module->module.NumSyms * sizeof(struct symt_ht*));
602     else
603         module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
604                                          module->module.NumSyms * sizeof(struct symt_ht*));
605     if (!module->addr_sorttab) return FALSE;
606
607     nsym = 0;
608     hash_table_iter_init(&module->ht_symbols, &hti, NULL);
609     while ((ptr = hash_table_iter_up(&hti)))
610     {
611         sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
612         assert(sym);
613         module->addr_sorttab[nsym++] = sym;
614     }
615     
616     qsort(module->addr_sorttab, nsym, sizeof(struct symt_ht*), symt_cmp_addr);
617     return module->sortlist_valid = TRUE;
618 }
619
620 /* assume addr is in module */
621 int symt_find_nearest(struct module* module, DWORD addr)
622 {
623     int         mid, high, low;
624     ULONG64     ref_addr, ref_size;
625
626     if (!module->sortlist_valid || !module->addr_sorttab)
627     {
628         if (!resort_symbols(module)) return -1;
629     }
630
631     /*
632      * Binary search to find closest symbol.
633      */
634     low = 0;
635     high = module->module.NumSyms;
636
637     symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
638     if (addr < ref_addr) return -1;
639     if (high)
640     {
641         symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
642         if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
643             ref_size = 0x1000; /* arbitrary value */
644         if (addr >= ref_addr + ref_size) return -1;
645     }
646     
647     while (high > low + 1)
648     {
649         mid = (high + low) / 2;
650         if (cmp_sorttab_addr(module, mid, addr) < 0)
651             low = mid;
652         else
653             high = mid;
654     }
655     if (low != high && high != module->module.NumSyms && 
656         cmp_sorttab_addr(module, high, addr) <= 0)
657         low = high;
658
659     /* If found symbol is a public symbol, check if there are any other entries that
660      * might also have the same address, but would get better information
661      */
662     if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
663     {   
664         symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
665         if (low > 0 &&
666             module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
667             !cmp_sorttab_addr(module, low - 1, ref_addr))
668             low--;
669         else if (low < module->module.NumSyms - 1 && 
670                  module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
671                  !cmp_sorttab_addr(module, low + 1, ref_addr))
672             low++;
673     }
674     /* finally check that we fit into the found symbol */
675     symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
676     if (addr < ref_addr) return -1;
677     if (!symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_LENGTH, &ref_size) || !ref_size)
678         ref_size = 0x1000; /* arbitrary value */
679     if (addr >= ref_addr + ref_size) return -1;
680
681     return low;
682 }
683
684 static BOOL symt_enum_locals_helper(struct process* pcs, struct module* module,
685                                     regex_t* preg, PSYM_ENUMERATESYMBOLS_CALLBACK cb,
686                                     PVOID user, SYMBOL_INFO* sym_info,
687                                     struct vector* v)
688 {
689     struct symt**       plsym = NULL;
690     struct symt*        lsym = NULL;
691     DWORD               pc = pcs->ctx_frame.InstructionOffset;
692
693     while ((plsym = vector_iter_up(v, plsym)))
694     {
695         lsym = *plsym;
696         switch (lsym->tag)
697         {
698         case SymTagBlock:
699             {
700                 struct symt_block*  block = (struct symt_block*)lsym;
701                 if (pc < block->address || block->address + block->size <= pc)
702                     continue;
703                 if (!symt_enum_locals_helper(pcs, module, preg, cb, user, 
704                                              sym_info, &block->vchildren))
705                     return FALSE;
706             }
707             break;
708         case SymTagData:
709             if (regexec(preg, symt_get_name(lsym), 0, NULL, 0) == 0)
710             {
711                 symt_fill_sym_info(module, lsym, sym_info);
712                 if (!cb(sym_info, sym_info->Size, user))
713                     return FALSE;
714             }
715             break;
716         case SymTagLabel:
717         case SymTagFuncDebugStart:
718         case SymTagFuncDebugEnd:
719             break;
720         default:
721             FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
722             assert(0);
723         }
724     }
725     return TRUE;
726 }
727
728 static BOOL symt_enum_locals(struct process* pcs, const char* mask,
729                              PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
730                              PVOID UserContext)
731 {
732     struct module*      module;
733     struct symt_ht*     sym;
734     char                buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
735     SYMBOL_INFO*        sym_info = (SYMBOL_INFO*)buffer;
736     DWORD               pc = pcs->ctx_frame.InstructionOffset;
737     int                 idx;
738
739     sym_info->SizeOfStruct = sizeof(*sym_info);
740     sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
741
742     module = module_find_by_addr(pcs, pc, DMT_UNKNOWN);
743     if (!(module = module_get_debug(pcs, module))) return FALSE;
744     if ((idx = symt_find_nearest(module, pc)) == -1) return FALSE;
745
746     sym = module->addr_sorttab[idx];
747     if (sym->symt.tag == SymTagFunction)
748     {
749         BOOL            ret;
750         regex_t         preg;
751
752         compile_regex(mask ? mask : "*", -1, &preg,
753                       dbghelp_options & SYMOPT_CASE_INSENSITIVE);
754         ret = symt_enum_locals_helper(pcs, module, &preg, EnumSymbolsCallback, 
755                                       UserContext, sym_info, 
756                                       &((struct symt_function*)sym)->vchildren);
757         regfree(&preg);
758         return ret;
759         
760     }
761     symt_fill_sym_info(module, &sym->symt, sym_info);
762     return EnumSymbolsCallback(sym_info, sym_info->Size, UserContext);
763 }
764
765 /******************************************************************
766  *              SymEnumSymbols (DBGHELP.@)
767  *
768  * cases BaseOfDll = 0
769  *      !foo fails always (despite what MSDN states)
770  *      RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
771  *      no ! in Mask, lookup in local Context
772  * cases BaseOfDll != 0
773  *      !foo fails always (despite what MSDN states)
774  *      RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
775  */
776 BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
777                            PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
778                            PVOID UserContext)
779 {
780     struct process*     pcs = process_find_by_handle(hProcess);
781     struct module*      module;
782     struct module*      dbg_module;
783     const char*         bang;
784     regex_t             mod_regex, sym_regex;
785
786     TRACE("(%p %s %s %p %p)\n", 
787           hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
788           EnumSymbolsCallback, UserContext);
789
790     if (!pcs) return FALSE;
791
792     if (BaseOfDll == 0)
793     {
794         /* do local variables ? */
795         if (!Mask || !(bang = strchr(Mask, '!')))
796             return symt_enum_locals(pcs, Mask, EnumSymbolsCallback, UserContext);
797
798         if (bang == Mask) return FALSE;
799
800         compile_regex(Mask, bang - Mask, &mod_regex, 
801                       dbghelp_options & SYMOPT_CASE_INSENSITIVE);
802         compile_regex(bang + 1, -1, &sym_regex, 
803                       dbghelp_options & SYMOPT_CASE_INSENSITIVE);
804         
805         for (module = pcs->lmodules; module; module = module->next)
806         {
807             if (module->type == DMT_PE && (dbg_module = module_get_debug(pcs, module)))
808             {
809                 if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
810                     symt_enum_module(dbg_module, &sym_regex, 
811                                      EnumSymbolsCallback, UserContext))
812                     break;
813             }
814         }
815         /* not found in PE modules, retry on the ELF ones
816          */
817         if (!module && (dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES))
818         {
819             for (module = pcs->lmodules; module; module = module->next)
820             {
821                 if (module->type == DMT_ELF &&
822                     !module_get_containee(pcs, module) &&
823                     (dbg_module = module_get_debug(pcs, module)))
824                 {
825                     if (regexec(&mod_regex, module->module.ModuleName, 0, NULL, 0) == 0 &&
826                         symt_enum_module(dbg_module, &sym_regex, EnumSymbolsCallback, UserContext))
827                     break;
828                 }
829             }
830         }
831         regfree(&mod_regex);
832         regfree(&sym_regex);
833         return TRUE;
834     }
835     module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
836     if (!(module = module_get_debug(pcs, module)))
837         return FALSE;
838
839     /* we always ignore module name from Mask when BaseOfDll is defined */
840     if (Mask && (bang = strchr(Mask, '!')))
841     {
842         if (bang == Mask) return FALSE;
843         Mask = bang + 1;
844     }
845
846     compile_regex(Mask ? Mask : "*", -1, &sym_regex, 
847                       dbghelp_options & SYMOPT_CASE_INSENSITIVE);
848     symt_enum_module(module, &sym_regex, EnumSymbolsCallback, UserContext);
849     regfree(&sym_regex);
850
851     return TRUE;
852 }
853
854 struct sym_enumerate
855 {
856     void*                       ctx;
857     PSYM_ENUMSYMBOLS_CALLBACK   cb;
858 };
859
860 static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
861 {
862     struct sym_enumerate*       se = (struct sym_enumerate*)ctx;
863     return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
864 }
865
866 /***********************************************************************
867  *              SymEnumerateSymbols (DBGHELP.@)
868  */
869 BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
870                                 PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback, 
871                                 PVOID UserContext)
872 {
873     struct sym_enumerate        se;
874
875     se.ctx = UserContext;
876     se.cb  = EnumSymbolsCallback;
877     
878     return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
879 }
880
881 /******************************************************************
882  *              SymFromAddr (DBGHELP.@)
883  *
884  */
885 BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address, 
886                         DWORD64* Displacement, PSYMBOL_INFO Symbol)
887 {
888     struct process*     pcs = process_find_by_handle(hProcess);
889     struct module*      module;
890     struct symt_ht*     sym;
891     int                 idx;
892
893     if (!pcs) return FALSE;
894     module = module_find_by_addr(pcs, Address, DMT_UNKNOWN);
895     if (!(module = module_get_debug(pcs, module))) return FALSE;
896     if ((idx = symt_find_nearest(module, Address)) == -1) return FALSE;
897
898     sym = module->addr_sorttab[idx];
899
900     symt_fill_sym_info(module, &sym->symt, Symbol);
901     *Displacement = Address - Symbol->Address;
902     return TRUE;
903 }
904
905 /******************************************************************
906  *              SymGetSymFromAddr (DBGHELP.@)
907  *
908  */
909 BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
910                               PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
911 {
912     char        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
913     SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
914     size_t      len;
915     DWORD64     Displacement64;
916
917     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
918     si->SizeOfStruct = sizeof(*si);
919     si->MaxNameLen = MAX_SYM_NAME;
920     if (!SymFromAddr(hProcess, Address, &Displacement64, si))
921         return FALSE;
922
923     if (Displacement)
924         *Displacement = Displacement64;
925     Symbol->Address = si->Address;
926     Symbol->Size    = si->Size;
927     Symbol->Flags   = si->Flags;
928     len = min(Symbol->MaxNameLength, si->MaxNameLen);
929     lstrcpynA(Symbol->Name, si->Name, len);
930     return TRUE;
931 }
932
933 /******************************************************************
934  *              SymFromName (DBGHELP.@)
935  *
936  */
937 BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
938 {
939     struct process*             pcs = process_find_by_handle(hProcess);
940     struct module*              module;
941     struct hash_table_iter      hti;
942     void*                       ptr;
943     struct symt_ht*             sym = NULL;
944     const char*                 name;
945
946     TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
947     if (!pcs) return FALSE;
948     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
949     name = strchr(Name, '!');
950     if (name)
951     {
952         char    tmp[128];
953         assert(name - Name < sizeof(tmp));
954         memcpy(tmp, Name, name - Name);
955         tmp[name - Name] = '\0';
956         module = module_find_by_name(pcs, tmp, DMT_UNKNOWN);
957         if (!module) return FALSE;
958         Name = (char*)(name + 1);
959     }
960     else module = pcs->lmodules;
961
962     /* FIXME: Name could be made out of a regular expression */
963     for (; module; module = (name) ? NULL : module->next)
964     {
965         if (module->module.SymType == SymNone) continue;
966         if (module->module.SymType == SymDeferred)
967         {
968             struct module*      xmodule = module_get_debug(pcs, module);
969             if (!xmodule || xmodule != module) continue;
970         }
971         hash_table_iter_init(&module->ht_symbols, &hti, Name);
972         while ((ptr = hash_table_iter_up(&hti)))
973         {
974             sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
975
976             if (!strcmp(sym->hash_elt.name, Name))
977             {
978                 symt_fill_sym_info(module, &sym->symt, Symbol);
979                 return TRUE;
980             }
981         }
982     }
983     return FALSE;
984 }
985
986 /***********************************************************************
987  *              SymGetSymFromName (DBGHELP.@)
988  */
989 BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
990 {
991     char        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
992     SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
993     size_t      len;
994
995     if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
996     si->SizeOfStruct = sizeof(*si);
997     si->MaxNameLen = MAX_SYM_NAME;
998     if (!SymFromName(hProcess, Name, si)) return FALSE;
999
1000     Symbol->Address = si->Address;
1001     Symbol->Size    = si->Size;
1002     Symbol->Flags   = si->Flags;
1003     len = min(Symbol->MaxNameLength, si->MaxNameLen);
1004     lstrcpynA(Symbol->Name, si->Name, len);
1005     return TRUE;
1006 }
1007
1008 /******************************************************************
1009  *              sym_fill_func_line_info
1010  *
1011  * fills information about a file
1012  */
1013 BOOL symt_fill_func_line_info(struct module* module, struct symt_function* func, 
1014                               DWORD addr, IMAGEHLP_LINE* line)
1015 {
1016     struct line_info*   dli = NULL;
1017     BOOL                found = FALSE;
1018
1019     assert(func->symt.tag == SymTagFunction);
1020
1021     while ((dli = vector_iter_down(&func->vlines, dli)))
1022     {
1023         if (!dli->is_source_file)
1024         {
1025             if (found || dli->u.pc_offset > addr) continue;
1026             line->LineNumber = dli->line_number;
1027             line->Address    = dli->u.pc_offset;
1028             line->Key        = dli;
1029             found = TRUE;
1030             continue;
1031         }
1032         if (found)
1033         {
1034             line->FileName = (char*)source_get(module, dli->u.source_file);
1035             return TRUE;
1036         }
1037     }
1038     return FALSE;
1039 }
1040
1041 /***********************************************************************
1042  *              SymGetSymNext (DBGHELP.@)
1043  */
1044 BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1045 {
1046     /* algo:
1047      * get module from Symbol.Address
1048      * get index in module.addr_sorttab of Symbol.Address
1049      * increment index
1050      * if out of module bounds, move to next module in process address space
1051      */
1052     FIXME("(%p, %p): stub\n", hProcess, Symbol);
1053     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1054     return FALSE;
1055 }
1056
1057 /***********************************************************************
1058  *              SymGetSymPrev (DBGHELP.@)
1059  */
1060
1061 BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
1062 {
1063     FIXME("(%p, %p): stub\n", hProcess, Symbol);
1064     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1065     return FALSE;
1066 }
1067
1068 /******************************************************************
1069  *              SymGetLineFromAddr (DBGHELP.@)
1070  *
1071  */
1072 BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr, 
1073                                PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
1074 {
1075     struct process*     pcs = process_find_by_handle(hProcess);
1076     struct module*      module;
1077     int                 idx;
1078
1079     TRACE("%p %08lx %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1080
1081     if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1082
1083     if (!pcs) return FALSE;
1084     module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1085     if (!(module = module_get_debug(pcs, module))) return FALSE;
1086     if ((idx = symt_find_nearest(module, dwAddr)) == -1) return FALSE;
1087
1088     if (module->addr_sorttab[idx]->symt.tag != SymTagFunction) return FALSE;
1089     if (!symt_fill_func_line_info(module, 
1090                                   (struct symt_function*)module->addr_sorttab[idx],
1091                                   dwAddr, Line)) return FALSE;
1092     *pdwDisplacement = dwAddr - Line->Address;
1093     return TRUE;
1094 }
1095
1096 /******************************************************************
1097  *              copy_line_64_from_32 (internal)
1098  *
1099  */
1100 static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)
1101
1102 {
1103     l64->Key = l32->Key;
1104     l64->LineNumber = l32->LineNumber;
1105     l64->FileName = l32->FileName;
1106     l64->Address = l32->Address;
1107 }
1108
1109 /******************************************************************
1110  *              copy_line_32_from_64 (internal)
1111  *
1112  */
1113 static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)
1114
1115 {
1116     l32->Key = l64->Key;
1117     l32->LineNumber = l64->LineNumber;
1118     l32->FileName = l64->FileName;
1119     l32->Address = l64->Address;
1120 }
1121
1122 /******************************************************************
1123  *              SymGetLineFromAddr64 (DBGHELP.@)
1124  *
1125  */
1126 BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr, 
1127                                  PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
1128 {
1129     IMAGEHLP_LINE       line32;
1130
1131     if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1132     if (!validate_addr64(dwAddr)) return FALSE;
1133     line32.SizeOfStruct = sizeof(line32);
1134     if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
1135         return FALSE;
1136     copy_line_64_from_32(Line, &line32);
1137     return TRUE;
1138 }
1139
1140 /******************************************************************
1141  *              SymGetLinePrev (DBGHELP.@)
1142  *
1143  */
1144 BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
1145 {
1146     struct process*     pcs = process_find_by_handle(hProcess);
1147     struct module*      module;
1148     struct line_info*   li;
1149     BOOL                in_search = FALSE;
1150
1151     TRACE("(%p %p)\n", hProcess, Line);
1152
1153     if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1154
1155     if (!pcs) return FALSE;
1156     module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1157     if (!(module = module_get_debug(pcs, module))) return FALSE;
1158
1159     if (Line->Key == 0) return FALSE;
1160     li = (struct line_info*)Line->Key;
1161     /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
1162      * element we have to go back until we find the prev one to get the real
1163      * source file name for the DLIT_OFFSET element just before 
1164      * the first DLIT_SOURCEFILE
1165      */
1166     while (!li->is_first)
1167     {
1168         li--;
1169         if (!li->is_source_file)
1170         {
1171             Line->LineNumber = li->line_number;
1172             Line->Address    = li->u.pc_offset;
1173             Line->Key        = li;
1174             if (!in_search) return TRUE;
1175         }
1176         else
1177         {
1178             if (in_search)
1179             {
1180                 Line->FileName = (char*)source_get(module, li->u.source_file);
1181                 return TRUE;
1182             }
1183             in_search = TRUE;
1184         }
1185     }
1186     SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1187     return FALSE;
1188 }
1189
1190 /******************************************************************
1191  *              SymGetLinePrev64 (DBGHELP.@)
1192  *
1193  */
1194 BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1195 {
1196     IMAGEHLP_LINE       line32;
1197
1198     line32.SizeOfStruct = sizeof(line32);
1199     copy_line_32_from_64(&line32, Line);
1200     if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
1201     copy_line_64_from_32(Line, &line32);
1202     return TRUE;
1203 }
1204     
1205 BOOL symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line)
1206 {
1207     struct line_info*   li;
1208
1209     if (line->Key == 0) return FALSE;
1210     li = (struct line_info*)line->Key;
1211     while (!li->is_last)
1212     {
1213         li++;
1214         if (!li->is_source_file)
1215         {
1216             line->LineNumber = li->line_number;
1217             line->Address    = li->u.pc_offset;
1218             line->Key        = li;
1219             return TRUE;
1220         }
1221         line->FileName = (char*)source_get(module, li->u.source_file);
1222     }
1223     return FALSE;
1224 }
1225
1226 /******************************************************************
1227  *              SymGetLineNext (DBGHELP.@)
1228  *
1229  */
1230 BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
1231 {
1232     struct process*     pcs = process_find_by_handle(hProcess);
1233     struct module*      module;
1234
1235     TRACE("(%p %p)\n", hProcess, Line);
1236
1237     if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1238     if (!pcs) return FALSE;
1239     module = module_find_by_addr(pcs, Line->Address, DMT_UNKNOWN);
1240     if (!(module = module_get_debug(pcs, module))) return FALSE;
1241
1242     if (symt_get_func_line_next(module, Line)) return TRUE;
1243     SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
1244     return FALSE;
1245 }
1246
1247 /******************************************************************
1248  *              SymGetLineNext64 (DBGHELP.@)
1249  *
1250  */
1251 BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
1252 {
1253     IMAGEHLP_LINE       line32;
1254
1255     line32.SizeOfStruct = sizeof(line32);
1256     copy_line_32_from_64(&line32, Line);
1257     if (!SymGetLineNext(hProcess, &line32)) return FALSE;
1258     copy_line_64_from_32(Line, &line32);
1259     return TRUE;
1260 }
1261     
1262 /***********************************************************************
1263  *              SymFunctionTableAccess (DBGHELP.@)
1264  */
1265 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1266 {
1267     WARN("(%p, 0x%08lx): stub\n", hProcess, AddrBase);
1268     return NULL;
1269 }
1270
1271 /***********************************************************************
1272  *              SymFunctionTableAccess64 (DBGHELP.@)
1273  */
1274 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1275 {
1276     WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
1277     return NULL;
1278 }
1279
1280 /***********************************************************************
1281  *              SymUnDName (DBGHELP.@)
1282  */
1283 BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, LPSTR UnDecName, DWORD UnDecNameLength)
1284 {
1285     TRACE("(%p %s %lu)\n", sym, UnDecName, UnDecNameLength);
1286     return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength, 
1287                                 UNDNAME_COMPLETE) != 0;
1288 }
1289
1290 static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
1291 static void  und_free (void* ptr)  { HeapFree(GetProcessHeap(), 0, ptr); }
1292
1293 /***********************************************************************
1294  *              UnDecorateSymbolName (DBGHELP.@)
1295  */
1296 DWORD WINAPI UnDecorateSymbolName(LPCSTR DecoratedName, LPSTR UnDecoratedName,
1297                                   DWORD UndecoratedLength, DWORD Flags)
1298 {
1299     /* undocumented from msvcrt */
1300     static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1301     static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1302
1303     TRACE("(%s, %p, %ld, 0x%08lx)\n",
1304           debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);
1305
1306     if (!p_undname)
1307     {
1308         if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
1309         if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
1310         if (!p_undname) return 0;
1311     }
1312
1313     if (!UnDecoratedName) return 0;
1314     if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength, 
1315                    und_alloc, und_free, Flags))
1316         return 0;
1317     return strlen(UnDecoratedName);
1318 }
1319
1320 /******************************************************************
1321  *              SymMatchString (DBGHELP.@)
1322  *
1323  */
1324 BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
1325 {
1326     regex_t     preg;
1327     BOOL        ret;
1328
1329     TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');
1330
1331     compile_regex(re, -1, &preg, _case);
1332     ret = regexec(&preg, string, 0, NULL, 0) == 0;
1333     regfree(&preg);
1334     return ret;
1335 }
1336
1337 /******************************************************************
1338  *              SymSearch (DBGHELP.@)
1339  */
1340 BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
1341                       DWORD SymTag, PCSTR Mask, DWORD64 Address,
1342                       PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
1343                       PVOID UserContext, DWORD Options)
1344 {
1345     TRACE("(%p %s %lu %lu %s %s %p %p %lx)\n",
1346           hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask, 
1347           wine_dbgstr_longlong(Address), EnumSymbolsCallback,
1348           UserContext, Options);
1349
1350     if (Index != 0)
1351     {
1352         FIXME("Unsupported searching for a given Index (%lu)\n", Index);
1353         SetLastError(ERROR_INVALID_PARAMETER);
1354         return FALSE;
1355     }
1356     if (SymTag != 0)
1357     {
1358         FIXME("Unsupported searching for a given SymTag (%lu)\n", SymTag);
1359         SetLastError(ERROR_INVALID_PARAMETER);
1360         return FALSE;
1361     }
1362     if (Address != 0)
1363     {
1364         FIXME("Unsupported searching for a given Address (%s)\n", wine_dbgstr_longlong(Address));
1365         SetLastError(ERROR_INVALID_PARAMETER);
1366         return FALSE;
1367     }
1368     if (Options != SYMSEARCH_GLOBALSONLY)
1369     {
1370         FIXME("Unsupported searching with options (%lx)\n", Options);
1371         SetLastError(ERROR_INVALID_PARAMETER);
1372         return FALSE;
1373     }
1374     return SymEnumSymbols(hProcess, BaseOfDll, Mask, EnumSymbolsCallback, UserContext);
1375 }