1 /* Read the export table symbols from a portable executable and
2 convert to internal format, for GDB. Used as a last resort if no
3 debugging symbols recognized.
5 Copyright (C) 2003-2013 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program 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
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 Contributed by Raoul M. Gough (RaoulGough@yahoo.co.uk). */
26 #include "coff-pe-read.h"
36 #include "common/common-utils.h"
37 #include "coff/internal.h"
41 /* Internal section information */
43 /* Coff PE read debugging flag:
45 value 1 outputs problems encountered while parsing PE file,
46 value above 1 also lists all generated minimal symbols. */
47 static unsigned int debug_coff_pe_read;
49 struct read_pe_section_data
51 CORE_ADDR vma_offset; /* Offset to loaded address of section. */
52 unsigned long rva_start; /* Start offset within the pe. */
53 unsigned long rva_end; /* End offset within the pe. */
54 enum minimal_symbol_type ms_type; /* Type to assign symbols in
56 char *section_name; /* Recorded section name. */
59 #define IMAGE_SCN_CNT_CODE 0x20
60 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x40
61 #define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x80
62 #define PE_SECTION_INDEX_TEXT 0
63 #define PE_SECTION_INDEX_DATA 1
64 #define PE_SECTION_INDEX_BSS 2
65 #define PE_SECTION_TABLE_SIZE 3
66 #define PE_SECTION_INDEX_INVALID -1
68 /* Get the index of the named section in our own array, which contains
69 text, data and bss in that order. Return PE_SECTION_INDEX_INVALID
70 if passed an unrecognised section name. */
73 read_pe_section_index (const char *section_name)
75 if (strcmp (section_name, ".text") == 0)
77 return PE_SECTION_INDEX_TEXT;
80 else if (strcmp (section_name, ".data") == 0)
82 return PE_SECTION_INDEX_DATA;
85 else if (strcmp (section_name, ".bss") == 0)
87 return PE_SECTION_INDEX_BSS;
92 return PE_SECTION_INDEX_INVALID;
96 /* Get the index of the named section in our own full arrayi.
97 text, data and bss in that order. Return PE_SECTION_INDEX_INVALID
98 if passed an unrecognised section name. */
101 get_pe_section_index (const char *section_name,
102 struct read_pe_section_data *sections,
107 for (i = 0; i < nb_sections; i++)
108 if (strcmp (sections[i].section_name, section_name) == 0)
110 return PE_SECTION_INDEX_INVALID;
113 /* Structure used by get_section_vmas function below
114 to access section_data array and the size of the array
115 stored in nb_sections field. */
116 struct pe_sections_info
119 struct read_pe_section_data *sections;
122 /* Record the virtual memory address of a section. */
125 get_section_vmas (bfd *abfd, asection *sectp, void *context)
127 struct pe_sections_info *data = context;
128 struct read_pe_section_data *sections = data->sections;
129 int sectix = get_pe_section_index (sectp->name, sections,
132 if (sectix != PE_SECTION_INDEX_INVALID)
134 /* Data within the section start at rva_start in the pe and at
135 bfd_get_section_vma() within memory. Store the offset. */
137 sections[sectix].vma_offset
138 = bfd_get_section_vma (abfd, sectp) - sections[sectix].rva_start;
142 /* Create a minimal symbol entry for an exported symbol.
143 SYM_NAME contains the exported name or NULL if exported by ordinal,
144 FUNC_RVA contains the Relative Virtual Address of the symbol,
145 ORDINAL is the ordinal index value of the symbol,
146 SECTION_DATA contains information about the section in which the
148 DLL_NAME is the internal name of the DLL file,
149 OBJFILE is the objfile struct of DLL_NAME. */
152 add_pe_exported_sym (const char *sym_name,
153 unsigned long func_rva,
155 const struct read_pe_section_data *section_data,
156 const char *dll_name, struct objfile *objfile)
158 char *qualified_name, *bare_name;
159 /* Add the stored offset to get the loaded address of the symbol. */
160 CORE_ADDR vma = func_rva + section_data->vma_offset;
162 /* Generate a (hopefully unique) qualified name using the first part
163 of the dll name, e.g. KERNEL32!AddAtomA. This matches the style
164 used by windbg from the "Microsoft Debugging Tools for Windows". */
166 if (sym_name == NULL || *sym_name == '\0')
167 bare_name = xstrprintf ("#%d", ordinal);
169 bare_name = xstrdup (sym_name);
171 qualified_name = xstrprintf ("%s!%s", dll_name, bare_name);
173 if ((section_data->ms_type == mst_unknown) && debug_coff_pe_read)
174 fprintf_unfiltered (gdb_stdlog , _("Unknown section type for \"%s\""
175 " for entry \"%s\" in dll \"%s\"\n"),
176 section_data->section_name, sym_name, dll_name);
178 prim_record_minimal_symbol (qualified_name, vma,
179 section_data->ms_type, objfile);
181 /* Enter the plain name as well, which might not be unique. */
182 prim_record_minimal_symbol (bare_name, vma, section_data->ms_type, objfile);
183 if (debug_coff_pe_read > 1)
184 fprintf_unfiltered (gdb_stdlog, _("Adding exported symbol \"%s\""
185 " in dll \"%s\"\n"), sym_name, dll_name);
186 xfree (qualified_name);
190 /* Create a minimal symbol entry for an exported forward symbol.
191 Return 1 if the forwarded function was found 0 otherwise.
192 SYM_NAME contains the exported name or NULL if exported by ordinal,
193 FORWARD_DLL_NAME is the name of the DLL in which the target symobl resides,
194 FORWARD_FUNC_NAME is the name of the target symbol in that DLL,
195 ORDINAL is the ordinal index value of the symbol,
196 DLL_NAME is the internal name of the DLL file,
197 OBJFILE is the objfile struct of DLL_NAME. */
200 add_pe_forwarded_sym (const char *sym_name, const char *forward_dll_name,
201 const char *forward_func_name, int ordinal,
202 const char *dll_name, struct objfile *objfile)
205 struct objfile *forward_objfile;
206 struct minimal_symbol *msymbol;
207 enum minimal_symbol_type msymtype;
208 char *qualified_name, *bare_name;
209 int forward_dll_name_len = strlen (forward_dll_name);
210 int forward_func_name_len = strlen (forward_func_name);
211 int forward_len = forward_dll_name_len + forward_func_name_len + 2;
212 char *forward_qualified_name = alloca (forward_len);
214 xsnprintf (forward_qualified_name, forward_len, "%s!%s", forward_dll_name,
218 msymbol = lookup_minimal_symbol_and_objfile (forward_qualified_name,
225 for (i = 0; i < forward_dll_name_len; i++)
226 forward_qualified_name[i] = tolower (forward_qualified_name[i]);
227 msymbol = lookup_minimal_symbol_and_objfile (forward_qualified_name,
233 if (debug_coff_pe_read)
234 fprintf_unfiltered (gdb_stdlog, _("Unable to find function \"%s\" in"
235 " dll \"%s\", forward of \"%s\" in dll \"%s\"\n"),
236 forward_func_name, forward_dll_name, sym_name,
241 if (debug_coff_pe_read > 1)
242 fprintf_unfiltered (gdb_stdlog, _("Adding forwarded exported symbol"
243 " \"%s\" in dll \"%s\", pointing to \"%s\"\n"),
244 sym_name, dll_name, forward_qualified_name);
246 vma = SYMBOL_VALUE_ADDRESS (msymbol);
247 msymtype = MSYMBOL_TYPE (msymbol);
249 /* Generate a (hopefully unique) qualified name using the first part
250 of the dll name, e.g. KERNEL32!AddAtomA. This matches the style
251 used by windbg from the "Microsoft Debugging Tools for Windows". */
253 if (sym_name == NULL || *sym_name == '\0')
254 bare_name = xstrprintf ("#%d", ordinal);
256 bare_name = xstrdup (sym_name);
258 qualified_name = xstrprintf ("%s!%s", dll_name, bare_name);
260 prim_record_minimal_symbol (qualified_name, vma, msymtype, objfile);
262 /* Enter the plain name as well, which might not be unique. */
263 prim_record_minimal_symbol (bare_name, vma, msymtype, objfile);
264 xfree (qualified_name);
270 /* Truncate a dll_name at the last dot character. */
273 read_pe_truncate_name (char *dll_name)
275 char *last_point = strrchr (dll_name, '.');
277 if (last_point != NULL)
281 /* Low-level support functions, direct from the ld module pe-dll.c. */
283 pe_get16 (bfd *abfd, int where)
287 bfd_seek (abfd, (file_ptr) where, SEEK_SET);
288 bfd_bread (b, (bfd_size_type) 2, abfd);
289 return b[0] + (b[1] << 8);
293 pe_get32 (bfd *abfd, int where)
297 bfd_seek (abfd, (file_ptr) where, SEEK_SET);
298 bfd_bread (b, (bfd_size_type) 4, abfd);
299 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
305 unsigned char *b = ptr;
307 return b[0] + (b[1] << 8);
313 unsigned char *b = ptr;
315 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
318 /* Read the (non-debug) export symbol table from a portable
319 executable. Code originally lifted from the ld function
320 pe_implied_import_dll in pe-dll.c. */
323 read_pe_exported_syms (struct objfile *objfile)
325 bfd *dll = objfile->obfd;
326 unsigned long nbnormal, nbforward;
327 unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
328 unsigned long export_opthdrrva, export_opthdrsize;
329 unsigned long export_rva, export_size, nsections, secptr, expptr;
330 unsigned long exp_funcbase;
331 unsigned char *expdata, *erva;
332 unsigned long name_rvas, ordinals, nexp, ordbase;
333 char *dll_name = (char *) dll->filename;
334 int otherix = PE_SECTION_TABLE_SIZE;
338 /* Array elements are for text, data and bss in that order
339 Initialization with RVA_START > RVA_END guarantees that
340 unused sections won't be matched. */
341 struct read_pe_section_data *section_data;
342 struct pe_sections_info pe_sections_info;
344 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
346 char const *target = bfd_get_target (objfile->obfd);
348 section_data = xzalloc (PE_SECTION_TABLE_SIZE
349 * sizeof (struct read_pe_section_data));
351 make_cleanup (free_current_contents, §ion_data);
353 for (i=0; i < PE_SECTION_TABLE_SIZE; i++)
355 section_data[i].vma_offset = 0;
356 section_data[i].rva_start = 1;
357 section_data[i].rva_end = 0;
359 section_data[PE_SECTION_INDEX_TEXT].ms_type = mst_text;
360 section_data[PE_SECTION_INDEX_TEXT].section_name = ".text";
361 section_data[PE_SECTION_INDEX_DATA].ms_type = mst_data;
362 section_data[PE_SECTION_INDEX_DATA].section_name = ".data";
363 section_data[PE_SECTION_INDEX_BSS].ms_type = mst_bss;
364 section_data[PE_SECTION_INDEX_BSS].section_name = ".bss";
366 is_pe64 = (strcmp (target, "pe-x86-64") == 0
367 || strcmp (target, "pei-x86-64") == 0);
368 is_pe32 = (strcmp (target, "pe-i386") == 0
369 || strcmp (target, "pei-i386") == 0
370 || strcmp (target, "pe-arm-wince-little") == 0
371 || strcmp (target, "pei-arm-wince-little") == 0);
372 if (!is_pe32 && !is_pe64)
374 /* This is not a recognized PE format file. Abort now, because
375 the code is untested on anything else. *FIXME* test on
376 further architectures and loosen or remove this test. */
377 do_cleanups (back_to);
381 /* Get pe_header, optional header and numbers of export entries. */
382 pe_header_offset = pe_get32 (dll, 0x3c);
383 opthdr_ofs = pe_header_offset + 4 + 20;
385 num_entries = pe_get32 (dll, opthdr_ofs + 108);
387 num_entries = pe_get32 (dll, opthdr_ofs + 92);
389 if (num_entries < 1) /* No exports. */
391 do_cleanups (back_to);
396 export_opthdrrva = pe_get32 (dll, opthdr_ofs + 112);
397 export_opthdrsize = pe_get32 (dll, opthdr_ofs + 116);
401 export_opthdrrva = pe_get32 (dll, opthdr_ofs + 96);
402 export_opthdrsize = pe_get32 (dll, opthdr_ofs + 100);
404 nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
405 secptr = (pe_header_offset + 4 + 20 +
406 pe_get16 (dll, pe_header_offset + 4 + 16));
410 /* Get the rva and size of the export section. */
411 for (i = 0; i < nsections; i++)
414 unsigned long secptr1 = secptr + 40 * i;
415 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
416 unsigned long vsize = pe_get32 (dll, secptr1 + 16);
417 unsigned long fptr = pe_get32 (dll, secptr1 + 20);
419 bfd_seek (dll, (file_ptr) secptr1, SEEK_SET);
420 bfd_bread (sname, (bfd_size_type) sizeof (sname), dll);
422 if ((strcmp (sname, ".edata") == 0)
423 || (vaddr <= export_opthdrrva && export_opthdrrva < vaddr + vsize))
425 if (strcmp (sname, ".edata") != 0)
427 if (debug_coff_pe_read)
428 fprintf_unfiltered (gdb_stdlog, _("Export RVA for dll "
429 "\"%s\" is in section \"%s\"\n"),
432 else if (export_opthdrrva != vaddr && debug_coff_pe_read)
433 fprintf_unfiltered (gdb_stdlog, _("Wrong value of export RVA"
434 " for dll \"%s\": 0x%lx instead of 0x%lx\n"),
435 dll_name, export_opthdrrva, vaddr);
436 expptr = fptr + (export_opthdrrva - vaddr);
441 export_rva = export_opthdrrva;
442 export_size = export_opthdrsize;
444 if (export_size == 0)
446 /* Empty export table. */
447 do_cleanups (back_to);
451 /* Scan sections and store the base and size of the relevant
453 for (i = 0; i < nsections; i++)
455 unsigned long secptr1 = secptr + 40 * i;
456 unsigned long vsize = pe_get32 (dll, secptr1 + 8);
457 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
458 unsigned long characteristics = pe_get32 (dll, secptr1 + 36);
459 char sec_name[SCNNMLEN + 1];
462 bfd_seek (dll, (file_ptr) secptr1 + 0, SEEK_SET);
463 bfd_bread (sec_name, (bfd_size_type) SCNNMLEN, dll);
464 sec_name[SCNNMLEN] = '\0';
466 sectix = read_pe_section_index (sec_name);
468 if (sectix != PE_SECTION_INDEX_INVALID)
470 section_data[sectix].rva_start = vaddr;
471 section_data[sectix].rva_end = vaddr + vsize;
477 section_data = xrealloc (section_data, (otherix + 1)
478 * sizeof (struct read_pe_section_data));
479 name = xstrdup (sec_name);
480 section_data[otherix].section_name = name;
481 make_cleanup (xfree, name);
482 section_data[otherix].rva_start = vaddr;
483 section_data[otherix].rva_end = vaddr + vsize;
484 section_data[otherix].vma_offset = 0;
485 if (characteristics & IMAGE_SCN_CNT_CODE)
486 section_data[otherix].ms_type = mst_text;
487 else if (characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
488 section_data[otherix].ms_type = mst_data;
489 else if (characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
490 section_data[otherix].ms_type = mst_bss;
492 section_data[otherix].ms_type = mst_unknown;
497 expdata = (unsigned char *) xmalloc (export_size);
498 make_cleanup (xfree, expdata);
500 bfd_seek (dll, (file_ptr) expptr, SEEK_SET);
501 bfd_bread (expdata, (bfd_size_type) export_size, dll);
502 erva = expdata - export_rva;
504 nexp = pe_as32 (expdata + 24);
505 name_rvas = pe_as32 (expdata + 32);
506 ordinals = pe_as32 (expdata + 36);
507 ordbase = pe_as32 (expdata + 16);
508 exp_funcbase = pe_as32 (expdata + 28);
510 /* Use internal dll name instead of full pathname. */
511 dll_name = pe_as32 (expdata + 12) + erva;
513 pe_sections_info.nb_sections = otherix;
514 pe_sections_info.sections = section_data;
516 bfd_map_over_sections (dll, get_section_vmas, &pe_sections_info);
518 /* Adjust the vma_offsets in case this PE got relocated. This
519 assumes that *all* sections share the same relocation offset
520 as the text section. */
521 for (i = 0; i < otherix; i++)
523 section_data[i].vma_offset
524 += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
527 /* Truncate name at first dot. Should maybe also convert to all
528 lower case for convenience on Windows. */
529 read_pe_truncate_name (dll_name);
531 if (debug_coff_pe_read)
532 fprintf_unfiltered (gdb_stdlog, _("DLL \"%s\" has %ld export entries,"
533 " base=%ld\n"), dll_name, nexp, ordbase);
536 /* Iterate through the list of symbols. */
537 for (i = 0; i < nexp; i++)
539 /* Pointer to the names vector. */
540 unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4);
541 /* Retrieve ordinal value. */
543 unsigned long ordinal = pe_as16 (erva + ordinals + i * 2);
546 /* Pointer to the function address vector. */
547 /* This is relatived to ordinal value. */
548 unsigned long func_rva = pe_as32 (erva + exp_funcbase +
551 /* Find this symbol's section in our own array. */
553 int section_found = 0;
555 /* First handle forward cases. */
556 if (func_rva >= export_rva && func_rva < export_rva + export_size)
558 char *forward_name = (char *) (erva + func_rva);
559 char *funcname = (char *) (erva + name_rva);
560 char *forward_dll_name = forward_name;
561 char *forward_func_name = forward_name;
562 char *sep = strrchr (forward_name, '.');
566 int len = (int) (sep - forward_name);
568 forward_dll_name = alloca (len + 1);
569 strncpy (forward_dll_name, forward_name, len);
570 forward_dll_name[len] = '\0';
571 forward_func_name = ++sep;
573 if (add_pe_forwarded_sym (funcname, forward_dll_name,
574 forward_func_name, ordinal,
575 dll_name, objfile) != 0)
580 for (sectix = 0; sectix < otherix; ++sectix)
582 if ((func_rva >= section_data[sectix].rva_start)
583 && (func_rva < section_data[sectix].rva_end))
586 add_pe_exported_sym (erva + name_rva,
588 section_data + sectix, dll_name, objfile);
595 char *funcname = (char *) (erva + name_rva);
599 add_pe_exported_sym (NULL, func_rva, ordinal,
600 section_data, dll_name, objfile);
603 else if (debug_coff_pe_read)
604 fprintf_unfiltered (gdb_stdlog, _("Export name \"%s\" ord. %lu,"
605 " RVA 0x%lx in dll \"%s\" not handled\n"),
606 funcname, ordinal, func_rva, dll_name);
610 if (debug_coff_pe_read)
611 fprintf_unfiltered (gdb_stdlog, _("Finished reading \"%s\", exports %ld,"
612 " forwards %ld, total %ld/%ld.\n"), dll_name, nbnormal,
613 nbforward, nbnormal + nbforward, nexp);
614 /* Discard expdata and section_data. */
615 do_cleanups (back_to);
618 /* Extract from ABFD the offset of the .text section.
619 This offset is mainly related to the offset within the file.
620 The value was previously expected to be 0x1000 for all files,
621 but some Windows OS core DLLs seem to use 0x10000 section alignement
622 which modified the return value of that function.
623 Still return default 0x1000 value if ABFD is NULL or
624 if '.text' section is not found, but that should not happen... */
626 #define DEFAULT_COFF_PE_TEXT_SECTION_OFFSET 0x1000
629 pe_text_section_offset (struct bfd *abfd)
632 unsigned long pe_header_offset, i;
633 unsigned long nsections, secptr;
639 return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
641 target = bfd_get_target (abfd);
643 is_pe64 = (strcmp (target, "pe-x86-64") == 0
644 || strcmp (target, "pei-x86-64") == 0);
645 is_pe32 = (strcmp (target, "pe-i386") == 0
646 || strcmp (target, "pei-i386") == 0
647 || strcmp (target, "pe-arm-wince-little") == 0
648 || strcmp (target, "pei-arm-wince-little") == 0);
650 if (!is_pe32 && !is_pe64)
652 /* This is not a recognized PE format file. Abort now, because
653 the code is untested on anything else. *FIXME* test on
654 further architectures and loosen or remove this test. */
655 return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
658 /* Get pe_header, optional header and numbers of sections. */
659 pe_header_offset = pe_get32 (abfd, 0x3c);
660 nsections = pe_get16 (abfd, pe_header_offset + 4 + 2);
661 secptr = (pe_header_offset + 4 + 20 +
662 pe_get16 (abfd, pe_header_offset + 4 + 16));
664 /* Get the rva and size of the export section. */
665 for (i = 0; i < nsections; i++)
667 char sname[SCNNMLEN + 1];
668 unsigned long secptr1 = secptr + 40 * i;
669 unsigned long vaddr = pe_get32 (abfd, secptr1 + 12);
671 bfd_seek (abfd, (file_ptr) secptr1, SEEK_SET);
672 bfd_bread (sname, (bfd_size_type) SCNNMLEN, abfd);
673 sname[SCNNMLEN] = '\0';
674 if (strcmp (sname, ".text") == 0)
678 return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
681 /* Implements "show debug coff_pe_read" command. */
684 show_debug_coff_pe_read (struct ui_file *file, int from_tty,
685 struct cmd_list_element *c, const char *value)
687 fprintf_filtered (file, _("Coff PE read debugging is %s.\n"), value);
690 /* Provide a prototype to silence -Wmissing-prototypes. */
692 void _initialize_coff_pe_read (void);
694 /* Adds "Set/show debug coff_pe_read" commands. */
697 _initialize_coff_pe_read (void)
699 add_setshow_zuinteger_cmd ("coff-pe-read", class_maintenance,
701 _("Set coff PE read debugging."),
702 _("Show coff PE read debugging."),
703 _("When set, debugging messages for coff reading "
704 "of exported symbols are displayed."),
705 NULL, show_debug_coff_pe_read,
706 &setdebuglist, &showdebuglist);