2 * Read VC++ debug information from COFF and eventually
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.
31 * Get 16 bit CV stuff working.
32 * Add symbol size to internal symbol table.
44 #include "wine/exception.h"
45 #include "wine/debug.h"
46 #include "dbghelp_private.h"
47 #include "wine/mscvpdb.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_coff
);
51 /*========================================================================
52 * Process COFF debug information.
57 unsigned int startaddr
;
59 struct symt_compiland
* compiland
;
62 struct symt
** entries
;
69 struct CoffFile
* files
;
74 static const char* coff_get_name(const IMAGE_SYMBOL
* coff_sym
,
75 const char* coff_strtab
)
77 static char namebuff
[9];
80 if (coff_sym
->N
.Name
.Short
)
82 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
84 nampnt
= &namebuff
[0];
88 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.Long
;
91 if (nampnt
[0] == '_') nampnt
++;
95 static int coff_add_file(struct CoffFileSet
* coff_files
, struct module
* module
,
98 struct CoffFile
* file
;
100 if (coff_files
->nfiles
+ 1 >= coff_files
->nfiles_alloc
)
102 if (coff_files
->files
)
104 coff_files
->nfiles_alloc
*= 2;
105 coff_files
->files
= HeapReAlloc(GetProcessHeap(), 0, coff_files
->files
,
106 coff_files
->nfiles_alloc
* sizeof(struct CoffFile
));
110 coff_files
->nfiles_alloc
= 16;
111 coff_files
->files
= HeapAlloc(GetProcessHeap(), 0,
112 coff_files
->nfiles_alloc
* sizeof(struct CoffFile
));
115 file
= coff_files
->files
+ coff_files
->nfiles
;
116 file
->startaddr
= 0xffffffff;
118 file
->compiland
= symt_new_compiland(module
, 0,
119 source_new(module
, NULL
, filename
));
120 file
->linetab_offset
= -1;
122 file
->entries
= NULL
;
123 file
->neps
= file
->neps_alloc
= 0;
125 return coff_files
->nfiles
++;
128 static void coff_add_symbol(struct CoffFile
* coff_file
, struct symt
* sym
)
130 if (coff_file
->neps
+ 1 >= coff_file
->neps_alloc
)
132 if (coff_file
->entries
)
134 coff_file
->neps_alloc
*= 2;
135 coff_file
->entries
= HeapReAlloc(GetProcessHeap(), 0, coff_file
->entries
,
136 coff_file
->neps_alloc
* sizeof(struct symt
*));
140 coff_file
->neps_alloc
= 32;
141 coff_file
->entries
= HeapAlloc(GetProcessHeap(), 0,
142 coff_file
->neps_alloc
* sizeof(struct symt
*));
145 coff_file
->entries
[coff_file
->neps
++] = sym
;
148 DECLSPEC_HIDDEN BOOL
coff_process_info(const struct msc_debug_info
* msc_dbg
)
150 const IMAGE_AUX_SYMBOL
* aux
;
151 const IMAGE_COFF_SYMBOLS_HEADER
* coff
;
152 const IMAGE_LINENUMBER
* coff_linetab
;
153 const IMAGE_LINENUMBER
* linepnt
;
154 const char* coff_strtab
;
155 const IMAGE_SYMBOL
* coff_sym
;
156 const IMAGE_SYMBOL
* coff_symbols
;
157 struct CoffFileSet coff_files
;
158 int curr_file_idx
= -1;
169 TRACE("Processing COFF symbols...\n");
171 assert(sizeof(IMAGE_SYMBOL
) == IMAGE_SIZEOF_SYMBOL
);
172 assert(sizeof(IMAGE_LINENUMBER
) == IMAGE_SIZEOF_LINENUMBER
);
174 coff_files
.files
= NULL
;
175 coff_files
.nfiles
= coff_files
.nfiles_alloc
= 0;
177 coff
= (const IMAGE_COFF_SYMBOLS_HEADER
*)msc_dbg
->root
;
179 coff_symbols
= (const IMAGE_SYMBOL
*)((const char *)coff
+ coff
->LvaToFirstSymbol
);
180 coff_linetab
= (const IMAGE_LINENUMBER
*)((const char *)coff
+ coff
->LvaToFirstLinenumber
);
181 coff_strtab
= (const char*)(coff_symbols
+ coff
->NumberOfSymbols
);
185 for (i
= 0; i
< coff
->NumberOfSymbols
; i
++)
187 coff_sym
= coff_symbols
+ i
;
188 naux
= coff_sym
->NumberOfAuxSymbols
;
190 if (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_FILE
)
192 curr_file_idx
= coff_add_file(&coff_files
, msc_dbg
->module
,
193 (const char*)(coff_sym
+ 1));
194 TRACE("New file %s\n", (const char*)(coff_sym
+ 1));
199 if (curr_file_idx
< 0)
201 assert(coff_files
.nfiles
== 0 && coff_files
.nfiles_alloc
== 0);
202 curr_file_idx
= coff_add_file(&coff_files
, msc_dbg
->module
, "<none>");
203 TRACE("New file <none>\n");
207 * This guy marks the size and location of the text section
208 * for the current file. We need to keep track of this so
209 * we can figure out what file the different global functions
212 if (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
&&
213 naux
!= 0 && coff_sym
->Type
== 0 && coff_sym
->SectionNumber
== 1)
215 aux
= (const IMAGE_AUX_SYMBOL
*) (coff_sym
+ 1);
217 if (coff_files
.files
[curr_file_idx
].linetab_offset
!= -1)
220 * Save this so we can still get the old name.
224 fn
= source_get(msc_dbg
->module
,
225 coff_files
.files
[curr_file_idx
].compiland
->source
);
227 TRACE("Duplicating sect from %s: %x %x %x %d %d\n",
228 fn
, aux
->Section
.Length
,
229 aux
->Section
.NumberOfRelocations
,
230 aux
->Section
.NumberOfLinenumbers
,
231 aux
->Section
.Number
, aux
->Section
.Selection
);
232 TRACE("More sect %d %s %08x %d %d %d\n",
233 coff_sym
->SectionNumber
,
234 coff_get_name(coff_sym
, coff_strtab
),
235 coff_sym
->Value
, coff_sym
->Type
,
236 coff_sym
->StorageClass
, coff_sym
->NumberOfAuxSymbols
);
239 * Duplicate the file entry. We have no way to describe
240 * multiple text sections in our current way of handling things.
242 coff_add_file(&coff_files
, msc_dbg
->module
, fn
);
246 TRACE("New text sect from %s: %x %x %x %d %d\n",
247 source_get(msc_dbg
->module
, coff_files
.files
[curr_file_idx
].compiland
->source
),
249 aux
->Section
.NumberOfRelocations
,
250 aux
->Section
.NumberOfLinenumbers
,
251 aux
->Section
.Number
, aux
->Section
.Selection
);
254 if (coff_files
.files
[curr_file_idx
].startaddr
> coff_sym
->Value
)
256 coff_files
.files
[curr_file_idx
].startaddr
= coff_sym
->Value
;
259 if (coff_files
.files
[curr_file_idx
].endaddr
< coff_sym
->Value
+ aux
->Section
.Length
)
261 coff_files
.files
[curr_file_idx
].endaddr
= coff_sym
->Value
+ aux
->Section
.Length
;
264 coff_files
.files
[curr_file_idx
].linetab_offset
= linetab_indx
;
265 coff_files
.files
[curr_file_idx
].linecnt
= aux
->Section
.NumberOfLinenumbers
;
266 linetab_indx
+= aux
->Section
.NumberOfLinenumbers
;
271 if (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
&& naux
== 0 &&
272 coff_sym
->SectionNumber
== 1)
274 DWORD base
= msc_dbg
->sectp
[coff_sym
->SectionNumber
- 1].VirtualAddress
;
276 * This is a normal static function when naux == 0.
277 * Just register it. The current file is the correct
278 * one in this instance.
280 nampnt
= coff_get_name(coff_sym
, coff_strtab
);
282 TRACE("\tAdding static symbol %s\n", nampnt
);
284 /* FIXME: was adding symbol to this_file ??? */
285 coff_add_symbol(&coff_files
.files
[curr_file_idx
],
286 &symt_new_function(msc_dbg
->module
,
287 coff_files
.files
[curr_file_idx
].compiland
,
289 msc_dbg
->module
->module
.BaseOfImage
+ base
+ coff_sym
->Value
,
291 NULL
/* FIXME */)->symt
);
295 if (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
&&
296 ISFCN(coff_sym
->Type
) && coff_sym
->SectionNumber
> 0)
298 struct symt_compiland
* compiland
= NULL
;
299 DWORD base
= msc_dbg
->sectp
[coff_sym
->SectionNumber
- 1].VirtualAddress
;
300 nampnt
= coff_get_name(coff_sym
, coff_strtab
);
303 i
, wine_dbgstr_longlong(msc_dbg
->module
->module
.BaseOfImage
+ base
+ coff_sym
->Value
),
305 TRACE("\tAdding global symbol %s (sect=%s)\n",
306 nampnt
, msc_dbg
->sectp
[coff_sym
->SectionNumber
- 1].Name
);
309 * Now we need to figure out which file this guy belongs to.
311 for (j
= 0; j
< coff_files
.nfiles
; j
++)
313 if (coff_files
.files
[j
].startaddr
<= base
+ coff_sym
->Value
314 && coff_files
.files
[j
].endaddr
> base
+ coff_sym
->Value
)
316 compiland
= coff_files
.files
[j
].compiland
;
320 if (j
< coff_files
.nfiles
)
322 coff_add_symbol(&coff_files
.files
[j
],
323 &symt_new_function(msc_dbg
->module
, compiland
, nampnt
,
324 msc_dbg
->module
->module
.BaseOfImage
+ base
+ coff_sym
->Value
,
325 0 /* FIXME */, NULL
/* FIXME */)->symt
);
329 symt_new_function(msc_dbg
->module
, NULL
, nampnt
,
330 msc_dbg
->module
->module
.BaseOfImage
+ base
+ coff_sym
->Value
,
331 0 /* FIXME */, NULL
/* FIXME */);
337 if (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
&&
338 coff_sym
->SectionNumber
> 0)
340 DWORD base
= msc_dbg
->sectp
[coff_sym
->SectionNumber
- 1].VirtualAddress
;
344 * Similar to above, but for the case of data symbols.
345 * These aren't treated as entrypoints.
347 nampnt
= coff_get_name(coff_sym
, coff_strtab
);
350 i
, wine_dbgstr_longlong(msc_dbg
->module
->module
.BaseOfImage
+ base
+ coff_sym
->Value
),
352 TRACE("\tAdding global data symbol %s\n", nampnt
);
355 * Now we need to figure out which file this guy belongs to.
357 loc
.kind
= loc_absolute
;
359 loc
.offset
= msc_dbg
->module
->module
.BaseOfImage
+ base
+ coff_sym
->Value
;
360 symt_new_global_variable(msc_dbg
->module
, NULL
, nampnt
, TRUE
/* FIXME */,
361 loc
, 0 /* FIXME */, NULL
/* FIXME */);
366 if (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
&& naux
== 0)
369 * Ignore these. They don't have anything to do with
375 TRACE("Skipping unknown entry '%s' %d %d %d\n",
376 coff_get_name(coff_sym
, coff_strtab
),
377 coff_sym
->StorageClass
, coff_sym
->SectionNumber
, naux
);
380 * For now, skip past the aux entries.
385 if (coff_files
.files
!= NULL
)
388 * OK, we now should have a list of files, and we should have a list
389 * of entrypoints. We need to sort the entrypoints so that we are
390 * able to tie the line numbers with the given functions within the
393 for (j
= 0; j
< coff_files
.nfiles
; j
++)
395 if (coff_files
.files
[j
].entries
!= NULL
)
397 qsort(coff_files
.files
[j
].entries
, coff_files
.files
[j
].neps
,
398 sizeof(struct symt
*), symt_cmp_addr
);
403 * Now pick apart the line number tables, and attach the entries
404 * to the given functions.
406 for (j
= 0; j
< coff_files
.nfiles
; j
++)
409 if (coff_files
.files
[j
].neps
!= 0)
411 for (k
= 0; k
< coff_files
.files
[j
].linecnt
; k
++)
413 linepnt
= coff_linetab
+ coff_files
.files
[j
].linetab_offset
+ k
;
415 * If we have spilled onto the next entrypoint, then
418 for (; l
+1 < coff_files
.files
[j
].neps
; l
++)
420 if (symt_get_address(coff_files
.files
[j
].entries
[l
+1], &addr
) &&
421 msc_dbg
->module
->module
.BaseOfImage
+ linepnt
->Type
.VirtualAddress
< addr
)
423 if (coff_files
.files
[j
].entries
[l
+1]->tag
== SymTagFunction
)
426 * Add the line number. This is always relative to the
427 * start of the function, so we need to subtract that offset
430 symt_add_func_line(msc_dbg
->module
,
431 (struct symt_function
*)coff_files
.files
[j
].entries
[l
+1],
432 coff_files
.files
[j
].compiland
->source
,
434 msc_dbg
->module
->module
.BaseOfImage
+ linepnt
->Type
.VirtualAddress
- addr
);
443 for (j
= 0; j
< coff_files
.nfiles
; j
++)
445 HeapFree(GetProcessHeap(), 0, coff_files
.files
[j
].entries
);
447 HeapFree(GetProcessHeap(), 0, coff_files
.files
);
448 msc_dbg
->module
->module
.SymType
= SymCoff
;
449 /* FIXME: we could have a finer grain here */
450 msc_dbg
->module
->module
.LineNumbers
= TRUE
;
451 msc_dbg
->module
->module
.GlobalSymbols
= TRUE
;
452 msc_dbg
->module
->module
.TypeInfo
= FALSE
;
453 msc_dbg
->module
->module
.SourceIndexed
= TRUE
;
454 msc_dbg
->module
->module
.Publics
= TRUE
;