wininet: Support the Cache-Control max-age directive for setting url cache entry...
[wine/testsucceed.git] / dlls / dbghelp / coff.c
blob3a9f2a26b8566b8fbb9882e684fe1193b49e7b8d
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 if (coff_files->files)
113 coff_files->nfiles_alloc *= 2;
114 coff_files->files = HeapReAlloc(GetProcessHeap(), 0, coff_files->files,
115 coff_files->nfiles_alloc * sizeof(struct CoffFile));
117 else
119 coff_files->nfiles_alloc = 16;
120 coff_files->files = HeapAlloc(GetProcessHeap(), 0,
121 coff_files->nfiles_alloc * sizeof(struct CoffFile));
124 file = coff_files->files + coff_files->nfiles;
125 file->startaddr = 0xffffffff;
126 file->endaddr = 0;
127 file->compiland = symt_new_compiland(module, 0,
128 source_new(module, NULL, filename));
129 file->linetab_offset = -1;
130 file->linecnt = 0;
131 file->entries = NULL;
132 file->neps = file->neps_alloc = 0;
134 return coff_files->nfiles++;
137 static void coff_add_symbol(struct CoffFile* coff_file, struct symt* sym)
139 if (coff_file->neps + 1 >= coff_file->neps_alloc)
141 coff_file->neps_alloc *= 2;
142 coff_file->entries = (coff_file->entries) ?
143 HeapReAlloc(GetProcessHeap(), 0, coff_file->entries,
144 coff_file->neps_alloc * sizeof(struct symt*)) :
145 HeapAlloc(GetProcessHeap(), 0,
146 coff_file->neps_alloc * sizeof(struct symt*));
148 coff_file->entries[coff_file->neps++] = sym;
151 BOOL coff_process_info(const struct msc_debug_info* msc_dbg)
153 const IMAGE_AUX_SYMBOL* aux;
154 const IMAGE_COFF_SYMBOLS_HEADER* coff;
155 const IMAGE_LINENUMBER* coff_linetab;
156 const IMAGE_LINENUMBER* linepnt;
157 const char* coff_strtab;
158 const IMAGE_SYMBOL* coff_sym;
159 const IMAGE_SYMBOL* coff_symbols;
160 struct CoffFileSet coff_files;
161 int curr_file_idx = -1;
162 unsigned int i;
163 int j;
164 int k;
165 int l;
166 int linetab_indx;
167 const char* nampnt;
168 int naux;
169 BOOL ret = FALSE;
170 DWORD addr;
172 TRACE("Processing COFF symbols...\n");
174 assert(sizeof(IMAGE_SYMBOL) == IMAGE_SIZEOF_SYMBOL);
175 assert(sizeof(IMAGE_LINENUMBER) == IMAGE_SIZEOF_LINENUMBER);
177 coff_files.files = NULL;
178 coff_files.nfiles = coff_files.nfiles_alloc = 0;
180 coff = (const IMAGE_COFF_SYMBOLS_HEADER*)msc_dbg->root;
182 coff_symbols = (const IMAGE_SYMBOL*)((const char *)coff + coff->LvaToFirstSymbol);
183 coff_linetab = (const IMAGE_LINENUMBER*)((const char *)coff + coff->LvaToFirstLinenumber);
184 coff_strtab = (const char*)(coff_symbols + coff->NumberOfSymbols);
186 linetab_indx = 0;
188 for (i = 0; i < coff->NumberOfSymbols; i++)
190 coff_sym = coff_symbols + i;
191 naux = coff_sym->NumberOfAuxSymbols;
193 if (coff_sym->StorageClass == IMAGE_SYM_CLASS_FILE)
195 curr_file_idx = coff_add_file(&coff_files, msc_dbg->module,
196 (const char*)(coff_sym + 1));
197 TRACE("New file %s\n", (const char*)(coff_sym + 1));
198 i += naux;
199 continue;
202 if (curr_file_idx < 0)
204 assert(coff_files.nfiles == 0 && coff_files.nfiles_alloc == 0);
205 curr_file_idx = coff_add_file(&coff_files, msc_dbg->module, "<none>");
206 TRACE("New file <none>\n");
210 * This guy marks the size and location of the text section
211 * for the current file. We need to keep track of this so
212 * we can figure out what file the different global functions
213 * go with.
215 if (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC &&
216 naux != 0 && coff_sym->Type == 0 && coff_sym->SectionNumber == 1)
218 aux = (const IMAGE_AUX_SYMBOL*) (coff_sym + 1);
220 if (coff_files.files[curr_file_idx].linetab_offset != -1)
223 * Save this so we can still get the old name.
225 const char* fn;
227 fn = source_get(msc_dbg->module,
228 coff_files.files[curr_file_idx].compiland->source);
230 TRACE("Duplicating sect from %s: %x %x %x %d %d\n",
231 fn, aux->Section.Length,
232 aux->Section.NumberOfRelocations,
233 aux->Section.NumberOfLinenumbers,
234 aux->Section.Number, aux->Section.Selection);
235 TRACE("More sect %d %s %08x %d %d %d\n",
236 coff_sym->SectionNumber,
237 coff_get_name(coff_sym, coff_strtab),
238 coff_sym->Value, coff_sym->Type,
239 coff_sym->StorageClass, coff_sym->NumberOfAuxSymbols);
242 * Duplicate the file entry. We have no way to describe
243 * multiple text sections in our current way of handling things.
245 coff_add_file(&coff_files, msc_dbg->module, fn);
247 else
249 TRACE("New text sect from %s: %x %x %x %d %d\n",
250 source_get(msc_dbg->module, coff_files.files[curr_file_idx].compiland->source),
251 aux->Section.Length,
252 aux->Section.NumberOfRelocations,
253 aux->Section.NumberOfLinenumbers,
254 aux->Section.Number, aux->Section.Selection);
257 if (coff_files.files[curr_file_idx].startaddr > coff_sym->Value)
259 coff_files.files[curr_file_idx].startaddr = coff_sym->Value;
262 if (coff_files.files[curr_file_idx].endaddr < coff_sym->Value + aux->Section.Length)
264 coff_files.files[curr_file_idx].endaddr = coff_sym->Value + aux->Section.Length;
267 coff_files.files[curr_file_idx].linetab_offset = linetab_indx;
268 coff_files.files[curr_file_idx].linecnt = aux->Section.NumberOfLinenumbers;
269 linetab_indx += aux->Section.NumberOfLinenumbers;
270 i += naux;
271 continue;
274 if (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC && naux == 0 &&
275 coff_sym->SectionNumber == 1)
277 DWORD base = msc_dbg->sectp[coff_sym->SectionNumber - 1].VirtualAddress;
279 * This is a normal static function when naux == 0.
280 * Just register it. The current file is the correct
281 * one in this instance.
283 nampnt = coff_get_name(coff_sym, coff_strtab);
285 TRACE("\tAdding static symbol %s\n", nampnt);
287 /* FIXME: was adding symbol to this_file ??? */
288 coff_add_symbol(&coff_files.files[curr_file_idx],
289 &symt_new_function(msc_dbg->module,
290 coff_files.files[curr_file_idx].compiland,
291 nampnt,
292 msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
293 0 /* FIXME */,
294 NULL /* FIXME */)->symt);
295 i += naux;
296 continue;
299 if (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
300 ISFCN(coff_sym->Type) && coff_sym->SectionNumber > 0)
302 struct symt_compiland* compiland = NULL;
303 DWORD base = msc_dbg->sectp[coff_sym->SectionNumber - 1].VirtualAddress;
304 nampnt = coff_get_name(coff_sym, coff_strtab);
306 TRACE("%d: %s %s\n",
307 i, wine_dbgstr_longlong(msc_dbg->module->module.BaseOfImage + base + coff_sym->Value),
308 nampnt);
309 TRACE("\tAdding global symbol %s (sect=%s)\n",
310 nampnt, msc_dbg->sectp[coff_sym->SectionNumber - 1].Name);
313 * Now we need to figure out which file this guy belongs to.
315 for (j = 0; j < coff_files.nfiles; j++)
317 if (coff_files.files[j].startaddr <= base + coff_sym->Value
318 && coff_files.files[j].endaddr > base + coff_sym->Value)
320 compiland = coff_files.files[j].compiland;
321 break;
324 if (j < coff_files.nfiles)
326 coff_add_symbol(&coff_files.files[j],
327 &symt_new_function(msc_dbg->module, compiland, nampnt,
328 msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
329 0 /* FIXME */, NULL /* FIXME */)->symt);
331 else
333 symt_new_function(msc_dbg->module, NULL, nampnt,
334 msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
335 0 /* FIXME */, NULL /* FIXME */);
337 i += naux;
338 continue;
341 if (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
342 coff_sym->SectionNumber > 0)
344 DWORD base = msc_dbg->sectp[coff_sym->SectionNumber - 1].VirtualAddress;
345 struct location loc;
348 * Similar to above, but for the case of data symbols.
349 * These aren't treated as entrypoints.
351 nampnt = coff_get_name(coff_sym, coff_strtab);
353 TRACE("%d: %s %s\n",
354 i, wine_dbgstr_longlong(msc_dbg->module->module.BaseOfImage + base + coff_sym->Value),
355 nampnt);
356 TRACE("\tAdding global data symbol %s\n", nampnt);
359 * Now we need to figure out which file this guy belongs to.
361 loc.kind = loc_absolute;
362 loc.reg = 0;
363 loc.offset = msc_dbg->module->module.BaseOfImage + base + coff_sym->Value;
364 symt_new_global_variable(msc_dbg->module, NULL, nampnt, TRUE /* FIXME */,
365 loc, 0 /* FIXME */, NULL /* FIXME */);
366 i += naux;
367 continue;
370 if (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC && naux == 0)
373 * Ignore these. They don't have anything to do with
374 * reality.
376 i += naux;
377 continue;
380 TRACE("Skipping unknown entry '%s' %d %d %d\n",
381 coff_get_name(coff_sym, coff_strtab),
382 coff_sym->StorageClass, coff_sym->SectionNumber, naux);
385 * For now, skip past the aux entries.
387 i += naux;
390 if (coff_files.files != NULL)
393 * OK, we now should have a list of files, and we should have a list
394 * of entrypoints. We need to sort the entrypoints so that we are
395 * able to tie the line numbers with the given functions within the
396 * file.
398 for (j = 0; j < coff_files.nfiles; j++)
400 if (coff_files.files[j].entries != NULL)
402 symt_cmp_addr_module = msc_dbg->module;
403 qsort(coff_files.files[j].entries, coff_files.files[j].neps,
404 sizeof(struct symt*), symt_cmp_addr);
409 * Now pick apart the line number tables, and attach the entries
410 * to the given functions.
412 for (j = 0; j < coff_files.nfiles; j++)
414 l = 0;
415 if (coff_files.files[j].neps != 0)
417 for (k = 0; k < coff_files.files[j].linecnt; k++)
419 linepnt = coff_linetab + coff_files.files[j].linetab_offset + k;
421 * If we have spilled onto the next entrypoint, then
422 * bump the counter..
424 for (;;)
426 if (l+1 >= coff_files.files[j].neps) break;
427 symt_get_info(msc_dbg->module, coff_files.files[j].entries[l+1], TI_GET_ADDRESS, &addr);
428 if (((msc_dbg->module->module.BaseOfImage + linepnt->Type.VirtualAddress) < addr))
429 break;
430 l++;
433 if (coff_files.files[j].entries[l+1]->tag == SymTagFunction)
436 * Add the line number. This is always relative to the
437 * start of the function, so we need to subtract that offset
438 * first.
440 symt_get_info(msc_dbg->module, coff_files.files[j].entries[l+1], TI_GET_ADDRESS, &addr);
441 symt_add_func_line(msc_dbg->module, (struct symt_function*)coff_files.files[j].entries[l+1],
442 coff_files.files[j].compiland->source, linepnt->Linenumber,
443 msc_dbg->module->module.BaseOfImage + linepnt->Type.VirtualAddress - addr);
449 for (j = 0; j < coff_files.nfiles; j++)
451 HeapFree(GetProcessHeap(), 0, coff_files.files[j].entries);
453 HeapFree(GetProcessHeap(), 0, coff_files.files);
454 msc_dbg->module->module.SymType = SymCoff;
455 /* FIXME: we could have a finer grain here */
456 msc_dbg->module->module.LineNumbers = TRUE;
457 msc_dbg->module->module.GlobalSymbols = TRUE;
458 msc_dbg->module->module.TypeInfo = FALSE;
459 msc_dbg->module->module.SourceIndexed = TRUE;
460 msc_dbg->module->module.Publics = TRUE;
461 ret = TRUE;
464 return ret;