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