winecfg: Fix the Bulgarian translation.
[wine.git] / dlls / dbghelp / coff.c
blobef8632b0f09b8013667034b09823ed3313ff7b50
1 /*
2 * Read VC++ debug information from COFF and eventually
3 * from PDB files.
5 * Copyright (C) 1996, Eric Youngdale.
6 * Copyright (C) 1999-2000, Ulrich Weigand.
7 * Copyright (C) 2004, Eric Pouech.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * Note - this handles reading debug information for 32 bit applications
26 * that run under Windows-NT for example. I doubt that this would work well
27 * for 16 bit applications, but I don't think it really matters since the
28 * file format is different, and we should never get in here in such cases.
30 * TODO:
31 * Get 16 bit CV stuff working.
32 * Add symbol size to internal symbol table.
35 #include "config.h"
36 #include "wine/port.h"
38 #include <assert.h>
39 #include <stdlib.h>
41 #include <string.h>
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45 #ifndef PATH_MAX
46 #define PATH_MAX MAX_PATH
47 #endif
48 #include <stdarg.h>
49 #include "windef.h"
50 #include "winbase.h"
51 #include "winternl.h"
53 #include "wine/exception.h"
54 #include "wine/debug.h"
55 #include "dbghelp_private.h"
56 #include "wine/mscvpdb.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_coff);
60 /*========================================================================
61 * Process COFF debug information.
64 struct CoffFile
66 unsigned int startaddr;
67 unsigned int endaddr;
68 struct symt_compiland* compiland;
69 int linetab_offset;
70 int linecnt;
71 struct symt** entries;
72 int neps;
73 int neps_alloc;
76 struct CoffFileSet
78 struct CoffFile* files;
79 int nfiles;
80 int nfiles_alloc;
83 static const char* coff_get_name(const IMAGE_SYMBOL* coff_sym,
84 const char* coff_strtab)
86 static char namebuff[9];
87 const char* nampnt;
89 if (coff_sym->N.Name.Short)
91 memcpy(namebuff, coff_sym->N.ShortName, 8);
92 namebuff[8] = '\0';
93 nampnt = &namebuff[0];
95 else
97 nampnt = coff_strtab + coff_sym->N.Name.Long;
100 if (nampnt[0] == '_') nampnt++;
101 return nampnt;
104 static int coff_add_file(struct CoffFileSet* coff_files, struct module* module,
105 const char* filename)
107 struct CoffFile* file;
109 if (coff_files->nfiles + 1 >= coff_files->nfiles_alloc)
111 coff_files->nfiles_alloc += 10;
112 coff_files->files = (coff_files->files) ?
113 HeapReAlloc(GetProcessHeap(), 0, coff_files->files,
114 coff_files->nfiles_alloc * sizeof(struct CoffFile)) :
115 HeapAlloc(GetProcessHeap(), 0,
116 coff_files->nfiles_alloc * sizeof(struct CoffFile));
118 file = coff_files->files + coff_files->nfiles;
119 file->startaddr = 0xffffffff;
120 file->endaddr = 0;
121 file->compiland = symt_new_compiland(module, 0,
122 source_new(module, NULL, filename));
123 file->linetab_offset = -1;
124 file->linecnt = 0;
125 file->entries = NULL;
126 file->neps = file->neps_alloc = 0;
128 return coff_files->nfiles++;
131 static void coff_add_symbol(struct CoffFile* coff_file, struct symt* sym)
133 if (coff_file->neps + 1 >= coff_file->neps_alloc)
135 coff_file->neps_alloc += 10;
136 coff_file->entries = (coff_file->entries) ?
137 HeapReAlloc(GetProcessHeap(), 0, coff_file->entries,
138 coff_file->neps_alloc * sizeof(struct symt*)) :
139 HeapAlloc(GetProcessHeap(), 0,
140 coff_file->neps_alloc * sizeof(struct symt*));
142 coff_file->entries[coff_file->neps++] = sym;
145 BOOL coff_process_info(const struct msc_debug_info* msc_dbg)
147 const IMAGE_AUX_SYMBOL* aux;
148 const IMAGE_COFF_SYMBOLS_HEADER* coff;
149 const IMAGE_LINENUMBER* coff_linetab;
150 const IMAGE_LINENUMBER* linepnt;
151 const char* coff_strtab;
152 const IMAGE_SYMBOL* coff_sym;
153 const IMAGE_SYMBOL* coff_symbols;
154 struct CoffFileSet coff_files;
155 int curr_file_idx = -1;
156 unsigned int i;
157 int j;
158 int k;
159 int l;
160 int linetab_indx;
161 const char* nampnt;
162 int naux;
163 BOOL ret = FALSE;
164 DWORD addr;
166 TRACE("Processing COFF symbols...\n");
168 assert(sizeof(IMAGE_SYMBOL) == IMAGE_SIZEOF_SYMBOL);
169 assert(sizeof(IMAGE_LINENUMBER) == IMAGE_SIZEOF_LINENUMBER);
171 coff_files.files = NULL;
172 coff_files.nfiles = coff_files.nfiles_alloc = 0;
174 coff = (const IMAGE_COFF_SYMBOLS_HEADER*)msc_dbg->root;
176 coff_symbols = (const IMAGE_SYMBOL*)((const char *)coff + coff->LvaToFirstSymbol);
177 coff_linetab = (const IMAGE_LINENUMBER*)((const char *)coff + coff->LvaToFirstLinenumber);
178 coff_strtab = (const char*)(coff_symbols + coff->NumberOfSymbols);
180 linetab_indx = 0;
182 for (i = 0; i < coff->NumberOfSymbols; i++)
184 coff_sym = coff_symbols + i;
185 naux = coff_sym->NumberOfAuxSymbols;
187 if (coff_sym->StorageClass == IMAGE_SYM_CLASS_FILE)
189 curr_file_idx = coff_add_file(&coff_files, msc_dbg->module,
190 (const char*)(coff_sym + 1));
191 TRACE("New file %s\n", (const char*)(coff_sym + 1));
192 i += naux;
193 continue;
196 if (curr_file_idx < 0)
198 assert(coff_files.nfiles == 0 && coff_files.nfiles_alloc == 0);
199 curr_file_idx = coff_add_file(&coff_files, msc_dbg->module, "<none>");
200 TRACE("New file <none>\n");
204 * This guy marks the size and location of the text section
205 * for the current file. We need to keep track of this so
206 * we can figure out what file the different global functions
207 * go with.
209 if (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC &&
210 naux != 0 && coff_sym->Type == 0 && coff_sym->SectionNumber == 1)
212 aux = (const IMAGE_AUX_SYMBOL*) (coff_sym + 1);
214 if (coff_files.files[curr_file_idx].linetab_offset != -1)
217 * Save this so we can still get the old name.
219 const char* fn;
221 fn = source_get(msc_dbg->module,
222 coff_files.files[curr_file_idx].compiland->source);
224 TRACE("Duplicating sect from %s: %x %x %x %d %d\n",
225 fn, aux->Section.Length,
226 aux->Section.NumberOfRelocations,
227 aux->Section.NumberOfLinenumbers,
228 aux->Section.Number, aux->Section.Selection);
229 TRACE("More sect %d %s %08x %d %d %d\n",
230 coff_sym->SectionNumber,
231 coff_get_name(coff_sym, coff_strtab),
232 coff_sym->Value, coff_sym->Type,
233 coff_sym->StorageClass, coff_sym->NumberOfAuxSymbols);
236 * Duplicate the file entry. We have no way to describe
237 * multiple text sections in our current way of handling things.
239 coff_add_file(&coff_files, msc_dbg->module, fn);
241 else
243 TRACE("New text sect from %s: %x %x %x %d %d\n",
244 source_get(msc_dbg->module, coff_files.files[curr_file_idx].compiland->source),
245 aux->Section.Length,
246 aux->Section.NumberOfRelocations,
247 aux->Section.NumberOfLinenumbers,
248 aux->Section.Number, aux->Section.Selection);
251 if (coff_files.files[curr_file_idx].startaddr > coff_sym->Value)
253 coff_files.files[curr_file_idx].startaddr = coff_sym->Value;
256 if (coff_files.files[curr_file_idx].endaddr < coff_sym->Value + aux->Section.Length)
258 coff_files.files[curr_file_idx].endaddr = coff_sym->Value + aux->Section.Length;
261 coff_files.files[curr_file_idx].linetab_offset = linetab_indx;
262 coff_files.files[curr_file_idx].linecnt = aux->Section.NumberOfLinenumbers;
263 linetab_indx += aux->Section.NumberOfLinenumbers;
264 i += naux;
265 continue;
268 if (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC && naux == 0 &&
269 coff_sym->SectionNumber == 1)
271 DWORD base = msc_dbg->sectp[coff_sym->SectionNumber - 1].VirtualAddress;
273 * This is a normal static function when naux == 0.
274 * Just register it. The current file is the correct
275 * one in this instance.
277 nampnt = coff_get_name(coff_sym, coff_strtab);
279 TRACE("\tAdding static symbol %s\n", nampnt);
281 /* FIXME: was adding symbol to this_file ??? */
282 coff_add_symbol(&coff_files.files[curr_file_idx],
283 &symt_new_function(msc_dbg->module,
284 coff_files.files[curr_file_idx].compiland,
285 nampnt,
286 msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
287 0 /* FIXME */,
288 NULL /* FIXME */)->symt);
289 i += naux;
290 continue;
293 if (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
294 ISFCN(coff_sym->Type) && coff_sym->SectionNumber > 0)
296 struct symt_compiland* compiland = NULL;
297 DWORD base = msc_dbg->sectp[coff_sym->SectionNumber - 1].VirtualAddress;
298 nampnt = coff_get_name(coff_sym, coff_strtab);
300 TRACE("%d: %s %s\n",
301 i, wine_dbgstr_longlong(msc_dbg->module->module.BaseOfImage + base + coff_sym->Value),
302 nampnt);
303 TRACE("\tAdding global symbol %s (sect=%s)\n",
304 nampnt, msc_dbg->sectp[coff_sym->SectionNumber - 1].Name);
307 * Now we need to figure out which file this guy belongs to.
309 for (j = 0; j < coff_files.nfiles; j++)
311 if (coff_files.files[j].startaddr <= base + coff_sym->Value
312 && coff_files.files[j].endaddr > base + coff_sym->Value)
314 compiland = coff_files.files[j].compiland;
315 break;
318 if (j < coff_files.nfiles)
320 coff_add_symbol(&coff_files.files[j],
321 &symt_new_function(msc_dbg->module, compiland, nampnt,
322 msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
323 0 /* FIXME */, NULL /* FIXME */)->symt);
325 else
327 symt_new_function(msc_dbg->module, NULL, nampnt,
328 msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
329 0 /* FIXME */, NULL /* FIXME */);
331 i += naux;
332 continue;
335 if (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
336 coff_sym->SectionNumber > 0)
338 DWORD base = msc_dbg->sectp[coff_sym->SectionNumber - 1].VirtualAddress;
340 * Similar to above, but for the case of data symbols.
341 * These aren't treated as entrypoints.
343 nampnt = coff_get_name(coff_sym, coff_strtab);
345 TRACE("%d: %s %s\n",
346 i, wine_dbgstr_longlong(msc_dbg->module->module.BaseOfImage + base + coff_sym->Value),
347 nampnt);
348 TRACE("\tAdding global data symbol %s\n", nampnt);
351 * Now we need to figure out which file this guy belongs to.
353 symt_new_global_variable(msc_dbg->module, NULL, nampnt, TRUE /* FIXME */,
354 msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
355 0 /* FIXME */, NULL /* FIXME */);
356 i += naux;
357 continue;
360 if (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC && naux == 0)
363 * Ignore these. They don't have anything to do with
364 * reality.
366 i += naux;
367 continue;
370 TRACE("Skipping unknown entry '%s' %d %d %d\n",
371 coff_get_name(coff_sym, coff_strtab),
372 coff_sym->StorageClass, coff_sym->SectionNumber, naux);
375 * For now, skip past the aux entries.
377 i += naux;
380 if (coff_files.files != NULL)
383 * OK, we now should have a list of files, and we should have a list
384 * of entrypoints. We need to sort the entrypoints so that we are
385 * able to tie the line numbers with the given functions within the
386 * file.
388 for (j = 0; j < coff_files.nfiles; j++)
390 if (coff_files.files[j].entries != NULL)
392 qsort(coff_files.files[j].entries, coff_files.files[j].neps,
393 sizeof(struct symt*), symt_cmp_addr);
398 * Now pick apart the line number tables, and attach the entries
399 * to the given functions.
401 for (j = 0; j < coff_files.nfiles; j++)
403 l = 0;
404 if (coff_files.files[j].neps != 0)
406 for (k = 0; k < coff_files.files[j].linecnt; k++)
408 linepnt = coff_linetab + coff_files.files[j].linetab_offset + k;
410 * If we have spilled onto the next entrypoint, then
411 * bump the counter..
413 for (;;)
415 if (l+1 >= coff_files.files[j].neps) break;
416 symt_get_info(coff_files.files[j].entries[l+1], TI_GET_ADDRESS, &addr);
417 if (((msc_dbg->module->module.BaseOfImage + linepnt->Type.VirtualAddress) < addr))
418 break;
419 l++;
422 if (coff_files.files[j].entries[l+1]->tag == SymTagFunction)
425 * Add the line number. This is always relative to the
426 * start of the function, so we need to subtract that offset
427 * first.
429 symt_get_info(coff_files.files[j].entries[l+1], TI_GET_ADDRESS, &addr);
430 symt_add_func_line(msc_dbg->module, (struct symt_function*)coff_files.files[j].entries[l+1],
431 coff_files.files[j].compiland->source, linepnt->Linenumber,
432 msc_dbg->module->module.BaseOfImage + linepnt->Type.VirtualAddress - addr);
438 for (j = 0; j < coff_files.nfiles; j++)
440 HeapFree(GetProcessHeap(), 0, coff_files.files[j].entries);
442 HeapFree(GetProcessHeap(), 0, coff_files.files);
443 msc_dbg->module->module.SymType = SymCoff;
444 /* FIXME: we could have a finer grain here */
445 msc_dbg->module->module.LineNumbers = TRUE;
446 msc_dbg->module->module.GlobalSymbols = TRUE;
447 msc_dbg->module->module.TypeInfo = FALSE;
448 msc_dbg->module->module.SourceIndexed = TRUE;
449 msc_dbg->module->module.Publics = TRUE;
450 ret = TRUE;
453 return ret;