gdb/coff-pe-read.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  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.

  4.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  5.    This file is part of GDB.

  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.

  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.

  14.    You should have received a copy of the GNU General Public License
  15.    along with this program.  If not, see <http://www.gnu.org/licenses/>.

  16.    Contributed by Raoul M. Gough (RaoulGough@yahoo.co.uk).  */

  17. #include "defs.h"

  18. #include "coff-pe-read.h"

  19. #include "bfd.h"
  20. #include "gdbtypes.h"

  21. #include "command.h"
  22. #include "gdbcmd.h"
  23. #include "symtab.h"
  24. #include "symfile.h"
  25. #include "objfiles.h"
  26. #include "common/common-utils.h"
  27. #include "coff/internal.h"

  28. #include <ctype.h>

  29. /* Internal section information */

  30. /* Coff PE read debugging flag:
  31.    default value is 0,
  32.    value 1 outputs problems encountered while parsing PE file,
  33.    value above 1 also lists all generated minimal symbols.  */
  34. static unsigned int debug_coff_pe_read;

  35. struct read_pe_section_data
  36. {
  37.   CORE_ADDR vma_offset;                /* Offset to loaded address of section.  */
  38.   unsigned long rva_start;        /* Start offset within the pe.  */
  39.   unsigned long rva_end;        /* End offset within the pe.  */
  40.   enum minimal_symbol_type ms_type;        /* Type to assign symbols in
  41.                                            section.  */
  42.   unsigned int index;                /* BFD section number.  */
  43.   char *section_name;                /* Recorded section name.  */
  44. };

  45. #define IMAGE_SCN_CNT_CODE 0x20
  46. #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x40
  47. #define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x80
  48. #define PE_SECTION_INDEX_TEXT     0
  49. #define PE_SECTION_INDEX_DATA     1
  50. #define PE_SECTION_INDEX_BSS      2
  51. #define PE_SECTION_TABLE_SIZE     3
  52. #define PE_SECTION_INDEX_INVALID -1

  53. /* Get the index of the named section in our own array, which contains
  54.    text, data and bss in that order.  Return PE_SECTION_INDEX_INVALID
  55.    if passed an unrecognised section name.  */

  56. static int
  57. read_pe_section_index (const char *section_name)
  58. {
  59.   if (strcmp (section_name, ".text") == 0)
  60.     {
  61.       return PE_SECTION_INDEX_TEXT;
  62.     }

  63.   else if (strcmp (section_name, ".data") == 0)
  64.     {
  65.       return PE_SECTION_INDEX_DATA;
  66.     }

  67.   else if (strcmp (section_name, ".bss") == 0)
  68.     {
  69.       return PE_SECTION_INDEX_BSS;
  70.     }

  71.   else
  72.     {
  73.       return PE_SECTION_INDEX_INVALID;
  74.     }
  75. }

  76. /* Get the index of the named section in our own full array.
  77.    text, data and bss in that order.  Return PE_SECTION_INDEX_INVALID
  78.    if passed an unrecognised section name.  */

  79. static int
  80. get_pe_section_index (const char *section_name,
  81.                       struct read_pe_section_data *sections,
  82.                       int nb_sections)
  83. {
  84.   int i;

  85.   for (i = 0; i < nb_sections; i++)
  86.     if (strcmp (sections[i].section_name, section_name) == 0)
  87.       return i;
  88.   return PE_SECTION_INDEX_INVALID;
  89. }

  90. /* Structure used by get_section_vmas function below
  91.    to access section_data array and the size of the array
  92.    stored in nb_sections field.  */
  93. struct pe_sections_info
  94. {
  95.   int nb_sections;
  96.   struct read_pe_section_data *sections;
  97. };

  98. /* Record the virtual memory address of a section.  */

  99. static void
  100. get_section_vmas (bfd *abfd, asection *sectp, void *context)
  101. {
  102.   struct pe_sections_info *data = context;
  103.   struct read_pe_section_data *sections = data->sections;
  104.   int sectix = get_pe_section_index (sectp->name, sections,
  105.                                      data->nb_sections);

  106.   if (sectix != PE_SECTION_INDEX_INVALID)
  107.     {
  108.       /* Data within the section start at rva_start in the pe and at
  109.          bfd_get_section_vma() within memory.  Store the offset.  */

  110.       sections[sectix].vma_offset
  111.         = bfd_get_section_vma (abfd, sectp) - sections[sectix].rva_start;
  112.     }
  113. }

  114. /* Create a minimal symbol entry for an exported symbol.
  115.    SYM_NAME contains the exported name or NULL if exported by ordinal,
  116.    FUNC_RVA contains the Relative Virtual Address of the symbol,
  117.    ORDINAL is the ordinal index value of the symbol,
  118.    SECTION_DATA contains information about the section in which the
  119.    symbol is declared,
  120.    DLL_NAME is the internal name of the DLL file,
  121.    OBJFILE is the objfile struct of DLL_NAME.  */

  122. static void
  123. add_pe_exported_sym (const char *sym_name,
  124.                      unsigned long func_rva,
  125.                      int ordinal,
  126.                      const struct read_pe_section_data *section_data,
  127.                      const char *dll_name, struct objfile *objfile)
  128. {
  129.   char *qualified_name, *bare_name;
  130.   /* Add the stored offset to get the loaded address of the symbol.  */
  131.   CORE_ADDR vma = func_rva + section_data->vma_offset;

  132.   /* Generate a (hopefully unique) qualified name using the first part
  133.      of the dll name, e.g. KERNEL32!AddAtomA.  This matches the style
  134.      used by windbg from the "Microsoft Debugging Tools for Windows".  */

  135.   if (sym_name == NULL || *sym_name == '\0')
  136.     bare_name = xstrprintf ("#%d", ordinal);
  137.   else
  138.     bare_name = xstrdup (sym_name);

  139.   qualified_name = xstrprintf ("%s!%s", dll_name, bare_name);

  140.   if ((section_data->ms_type == mst_unknown) && debug_coff_pe_read)
  141.     fprintf_unfiltered (gdb_stdlog , _("Unknown section type for \"%s\""
  142.                         " for entry \"%s\" in dll \"%s\"\n"),
  143.                         section_data->section_name, sym_name, dll_name);

  144.   prim_record_minimal_symbol_and_info (qualified_name, vma,
  145.                                        section_data->ms_type,
  146.                                        section_data->index, objfile);

  147.   /* Enter the plain name as well, which might not be unique.  */
  148.   prim_record_minimal_symbol_and_info (bare_name, vma, section_data->ms_type,
  149.                                        section_data->index, objfile);
  150.   if (debug_coff_pe_read > 1)
  151.     fprintf_unfiltered (gdb_stdlog, _("Adding exported symbol \"%s\""
  152.                         " in dll \"%s\"\n"), sym_name, dll_name);
  153.   xfree (qualified_name);
  154.   xfree (bare_name);
  155. }

  156. /* Create a minimal symbol entry for an exported forward symbol.
  157.    Return 1 if the forwarded function was found 0 otherwise.
  158.    SYM_NAME contains the exported name or NULL if exported by ordinal,
  159.    FORWARD_DLL_NAME is the name of the DLL in which the target symobl resides,
  160.    FORWARD_FUNC_NAME is the name of the target symbol in that DLL,
  161.    ORDINAL is the ordinal index value of the symbol,
  162.    DLL_NAME is the internal name of the DLL file,
  163.    OBJFILE is the objfile struct of DLL_NAME.  */

  164. static int
  165. add_pe_forwarded_sym (const char *sym_name, const char *forward_dll_name,
  166.                       const char *forward_func_name, int ordinal,
  167.                       const char *dll_name, struct objfile *objfile)
  168. {
  169.   CORE_ADDR vma, baseaddr;
  170.   struct bound_minimal_symbol msymbol;
  171.   enum minimal_symbol_type msymtype;
  172.   char *qualified_name, *bare_name;
  173.   int forward_dll_name_len = strlen (forward_dll_name);
  174.   int forward_func_name_len = strlen (forward_func_name);
  175.   int forward_len = forward_dll_name_len + forward_func_name_len + 2;
  176.   char *forward_qualified_name = alloca (forward_len);
  177.   short section;

  178.   xsnprintf (forward_qualified_name, forward_len, "%s!%s", forward_dll_name,
  179.              forward_func_name);


  180.   msymbol = lookup_minimal_symbol_and_objfile (forward_qualified_name);

  181.   if (!msymbol.minsym)
  182.     {
  183.       int i;

  184.       for (i = 0; i < forward_dll_name_len; i++)
  185.         forward_qualified_name[i] = tolower (forward_qualified_name[i]);
  186.       msymbol = lookup_minimal_symbol_and_objfile (forward_qualified_name);
  187.     }

  188.   if (!msymbol.minsym)
  189.     {
  190.       if (debug_coff_pe_read)
  191.         fprintf_unfiltered (gdb_stdlog, _("Unable to find function \"%s\" in"
  192.                             " dll \"%s\", forward of \"%s\" in dll \"%s\"\n"),
  193.                             forward_func_name, forward_dll_name, sym_name,
  194.                             dll_name);
  195.       return 0;
  196.     }

  197.   if (debug_coff_pe_read > 1)
  198.     fprintf_unfiltered (gdb_stdlog, _("Adding forwarded exported symbol"
  199.                         " \"%s\" in dll \"%s\", pointing to \"%s\"\n"),
  200.                         sym_name, dll_name, forward_qualified_name);

  201.   vma = BMSYMBOL_VALUE_ADDRESS (msymbol);
  202.   msymtype = MSYMBOL_TYPE (msymbol.minsym);
  203.   section = MSYMBOL_SECTION (msymbol.minsym);

  204.   /* Generate a (hopefully unique) qualified name using the first part
  205.      of the dll name, e.g. KERNEL32!AddAtomA.  This matches the style
  206.      used by windbg from the "Microsoft Debugging Tools for Windows".  */

  207.   if (sym_name == NULL || *sym_name == '\0')
  208.     bare_name = xstrprintf ("#%d", ordinal);
  209.   else
  210.     bare_name = xstrdup (sym_name);

  211.   qualified_name = xstrprintf ("%s!%s", dll_name, bare_name);

  212.   /* Note that this code makes a minimal symbol whose value may point
  213.      outside of any section in this objfile.  These symbols can't
  214.      really be relocated properly, but nevertheless we make a stab at
  215.      it, choosing an approach consistent with the history of this
  216.      code.  */
  217.   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  218.   prim_record_minimal_symbol_and_info (qualified_name, vma - baseaddr,
  219.                                        msymtype, section, objfile);

  220.   /* Enter the plain name as well, which might not be unique.  */
  221.   prim_record_minimal_symbol_and_info (bare_name, vma - baseaddr, msymtype,
  222.                                        section, objfile);
  223.   xfree (qualified_name);
  224.   xfree (bare_name);

  225.   return 1;
  226. }

  227. /* Truncate a dll_name at the last dot character.  */

  228. static void
  229. read_pe_truncate_name (char *dll_name)
  230. {
  231.   char *last_point = strrchr (dll_name, '.');

  232.   if (last_point != NULL)
  233.     *last_point = '\0';
  234. }

  235. /* Low-level support functions, direct from the ld module pe-dll.c.  */
  236. static unsigned int
  237. pe_get16 (bfd *abfd, int where)
  238. {
  239.   unsigned char b[2];

  240.   bfd_seek (abfd, (file_ptr) where, SEEK_SET);
  241.   bfd_bread (b, (bfd_size_type) 2, abfd);
  242.   return b[0] + (b[1] << 8);
  243. }

  244. static unsigned int
  245. pe_get32 (bfd *abfd, int where)
  246. {
  247.   unsigned char b[4];

  248.   bfd_seek (abfd, (file_ptr) where, SEEK_SET);
  249.   bfd_bread (b, (bfd_size_type) 4, abfd);
  250.   return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
  251. }

  252. static unsigned int
  253. pe_as16 (void *ptr)
  254. {
  255.   unsigned char *b = ptr;

  256.   return b[0] + (b[1] << 8);
  257. }

  258. static unsigned int
  259. pe_as32 (void *ptr)
  260. {
  261.   unsigned char *b = ptr;

  262.   return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
  263. }

  264. /* Read the (non-debug) export symbol table from a portable
  265.    executable.  Code originally lifted from the ld function
  266.    pe_implied_import_dll in pe-dll.c.  */

  267. void
  268. read_pe_exported_syms (struct objfile *objfile)
  269. {
  270.   bfd *dll = objfile->obfd;
  271.   unsigned long nbnormal, nbforward;
  272.   unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
  273.   unsigned long export_opthdrrva, export_opthdrsize;
  274.   unsigned long export_rva, export_size, nsections, secptr, expptr;
  275.   unsigned long exp_funcbase;
  276.   unsigned char *expdata, *erva;
  277.   unsigned long name_rvas, ordinals, nexp, ordbase;
  278.   char *dll_name = (char *) dll->filename;
  279.   int otherix = PE_SECTION_TABLE_SIZE;
  280.   int is_pe64 = 0;
  281.   int is_pe32 = 0;

  282.   /* Array elements are for text, data and bss in that order
  283.      Initialization with RVA_START > RVA_END guarantees that
  284.      unused sections won't be matched.  */
  285.   struct read_pe_section_data *section_data;
  286.   struct pe_sections_info pe_sections_info;

  287.   struct cleanup *back_to = make_cleanup (null_cleanup, 0);

  288.   char const *target = bfd_get_target (objfile->obfd);

  289.   section_data = xzalloc (PE_SECTION_TABLE_SIZE
  290.                          * sizeof (struct read_pe_section_data));

  291.   make_cleanup (free_current_contents, &section_data);

  292.   for (i=0; i < PE_SECTION_TABLE_SIZE; i++)
  293.     {
  294.       section_data[i].vma_offset = 0;
  295.       section_data[i].rva_start = 1;
  296.       section_data[i].rva_end = 0;
  297.     };
  298.   section_data[PE_SECTION_INDEX_TEXT].ms_type = mst_text;
  299.   section_data[PE_SECTION_INDEX_TEXT].section_name = ".text";
  300.   section_data[PE_SECTION_INDEX_DATA].ms_type = mst_data;
  301.   section_data[PE_SECTION_INDEX_DATA].section_name = ".data";
  302.   section_data[PE_SECTION_INDEX_BSS].ms_type = mst_bss;
  303.   section_data[PE_SECTION_INDEX_BSS].section_name = ".bss";

  304.   is_pe64 = (strcmp (target, "pe-x86-64") == 0
  305.              || strcmp (target, "pei-x86-64") == 0);
  306.   is_pe32 = (strcmp (target, "pe-i386") == 0
  307.              || strcmp (target, "pei-i386") == 0
  308.              || strcmp (target, "pe-arm-wince-little") == 0
  309.              || strcmp (target, "pei-arm-wince-little") == 0);
  310.   if (!is_pe32 && !is_pe64)
  311.     {
  312.       /* This is not a recognized PE format file.  Abort now, because
  313.          the code is untested on anything else.  *FIXME* test on
  314.          further architectures and loosen or remove this test.  */
  315.       do_cleanups (back_to);
  316.       return;
  317.     }

  318.   /* Get pe_header, optional header and numbers of export entries.  */
  319.   pe_header_offset = pe_get32 (dll, 0x3c);
  320.   opthdr_ofs = pe_header_offset + 4 + 20;
  321.   if (is_pe64)
  322.     num_entries = pe_get32 (dll, opthdr_ofs + 108);
  323.   else
  324.     num_entries = pe_get32 (dll, opthdr_ofs + 92);

  325.   if (num_entries < 1)                /* No exports.  */
  326.     {
  327.       do_cleanups (back_to);
  328.       return;
  329.     }
  330.   if (is_pe64)
  331.     {
  332.       export_opthdrrva = pe_get32 (dll, opthdr_ofs + 112);
  333.       export_opthdrsize = pe_get32 (dll, opthdr_ofs + 116);
  334.     }
  335.   else
  336.     {
  337.       export_opthdrrva = pe_get32 (dll, opthdr_ofs + 96);
  338.       export_opthdrsize = pe_get32 (dll, opthdr_ofs + 100);
  339.     }
  340.   nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
  341.   secptr = (pe_header_offset + 4 + 20 +
  342.             pe_get16 (dll, pe_header_offset + 4 + 16));
  343.   expptr = 0;
  344.   export_size = 0;

  345.   /* Get the rva and size of the export section.  */
  346.   for (i = 0; i < nsections; i++)
  347.     {
  348.       char sname[8];
  349.       unsigned long secptr1 = secptr + 40 * i;
  350.       unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
  351.       unsigned long vsize = pe_get32 (dll, secptr1 + 16);
  352.       unsigned long fptr = pe_get32 (dll, secptr1 + 20);

  353.       bfd_seek (dll, (file_ptr) secptr1, SEEK_SET);
  354.       bfd_bread (sname, (bfd_size_type) sizeof (sname), dll);

  355.       if ((strcmp (sname, ".edata") == 0)
  356.           || (vaddr <= export_opthdrrva && export_opthdrrva < vaddr + vsize))
  357.         {
  358.           if (strcmp (sname, ".edata") != 0)
  359.             {
  360.               if (debug_coff_pe_read)
  361.                 fprintf_unfiltered (gdb_stdlog, _("Export RVA for dll "
  362.                                     "\"%s\" is in section \"%s\"\n"),
  363.                                     dll_name, sname);
  364.             }
  365.           else if (export_opthdrrva != vaddr && debug_coff_pe_read)
  366.             fprintf_unfiltered (gdb_stdlog, _("Wrong value of export RVA"
  367.                                 " for dll \"%s\": 0x%lx instead of 0x%lx\n"),
  368.                                 dll_name, export_opthdrrva, vaddr);
  369.           expptr = fptr + (export_opthdrrva - vaddr);
  370.           break;
  371.         }
  372.     }

  373.   export_rva = export_opthdrrva;
  374.   export_size = export_opthdrsize;

  375.   if (export_size == 0)
  376.     {
  377.       /* Empty export table.  */
  378.       do_cleanups (back_to);
  379.       return;
  380.     }

  381.   /* Scan sections and store the base and size of the relevant
  382.      sections.  */
  383.   for (i = 0; i < nsections; i++)
  384.     {
  385.       unsigned long secptr1 = secptr + 40 * i;
  386.       unsigned long vsize = pe_get32 (dll, secptr1 + 8);
  387.       unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
  388.       unsigned long characteristics = pe_get32 (dll, secptr1 + 36);
  389.       char sec_name[SCNNMLEN + 1];
  390.       int sectix;
  391.       unsigned int bfd_section_index;
  392.       asection *section;

  393.       bfd_seek (dll, (file_ptr) secptr1 + 0, SEEK_SET);
  394.       bfd_bread (sec_name, (bfd_size_type) SCNNMLEN, dll);
  395.       sec_name[SCNNMLEN] = '\0';

  396.       sectix = read_pe_section_index (sec_name);
  397.       section = bfd_get_section_by_name (dll, sec_name);
  398.       if (section)
  399.         bfd_section_index = section->index;
  400.       else
  401.         bfd_section_index = -1;

  402.       if (sectix != PE_SECTION_INDEX_INVALID)
  403.         {
  404.           section_data[sectix].rva_start = vaddr;
  405.           section_data[sectix].rva_end = vaddr + vsize;
  406.           section_data[sectix].index = bfd_section_index;
  407.         }
  408.       else
  409.         {
  410.           char *name;

  411.           section_data = xrealloc (section_data, (otherix + 1)
  412.                                    * sizeof (struct read_pe_section_data));
  413.           name = xstrdup (sec_name);
  414.           section_data[otherix].section_name = name;
  415.           make_cleanup (xfree, name);
  416.           section_data[otherix].rva_start = vaddr;
  417.           section_data[otherix].rva_end = vaddr + vsize;
  418.           section_data[otherix].vma_offset = 0;
  419.           section_data[otherix].index = bfd_section_index;
  420.           if (characteristics & IMAGE_SCN_CNT_CODE)
  421.             section_data[otherix].ms_type = mst_text;
  422.           else if (characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
  423.             section_data[otherix].ms_type = mst_data;
  424.           else if (characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
  425.             section_data[otherix].ms_type = mst_bss;
  426.           else
  427.             section_data[otherix].ms_type = mst_unknown;
  428.           otherix++;
  429.         }
  430.     }

  431.   expdata = (unsigned char *) xmalloc (export_size);
  432.   make_cleanup (xfree, expdata);

  433.   bfd_seek (dll, (file_ptr) expptr, SEEK_SET);
  434.   bfd_bread (expdata, (bfd_size_type) export_size, dll);
  435.   erva = expdata - export_rva;

  436.   nexp = pe_as32 (expdata + 24);
  437.   name_rvas = pe_as32 (expdata + 32);
  438.   ordinals = pe_as32 (expdata + 36);
  439.   ordbase = pe_as32 (expdata + 16);
  440.   exp_funcbase = pe_as32 (expdata + 28);

  441.   /* Use internal dll name instead of full pathname.  */
  442.   dll_name = (char *) (pe_as32 (expdata + 12) + erva);

  443.   pe_sections_info.nb_sections = otherix;
  444.   pe_sections_info.sections = section_data;

  445.   bfd_map_over_sections (dll, get_section_vmas, &pe_sections_info);

  446.   /* Truncate name at first dot. Should maybe also convert to all
  447.      lower case for convenience on Windows.  */
  448.   read_pe_truncate_name (dll_name);

  449.   if (debug_coff_pe_read)
  450.     fprintf_unfiltered (gdb_stdlog, _("DLL \"%s\" has %ld export entries,"
  451.                         " base=%ld\n"), dll_name, nexp, ordbase);
  452.   nbforward = 0;
  453.   nbnormal = 0;
  454.   /* Iterate through the list of symbols.  */
  455.   for (i = 0; i < nexp; i++)
  456.     {
  457.       /* Pointer to the names vector.  */
  458.       unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4);
  459.       /* Retrieve ordinal value.  */

  460.       unsigned long ordinal = pe_as16 (erva + ordinals + i * 2);


  461.       /* Pointer to the function address vector.  */
  462.       /* This is relatived to ordinal value. */
  463.       unsigned long func_rva = pe_as32 (erva + exp_funcbase +
  464.                                         ordinal * 4);

  465.       /* Find this symbol's section in our own array.  */
  466.       int sectix = 0;
  467.       int section_found = 0;

  468.       /* First handle forward cases.  */
  469.       if (func_rva >= export_rva && func_rva < export_rva + export_size)
  470.         {
  471.           char *forward_name = (char *) (erva + func_rva);
  472.           char *funcname = (char *) (erva + name_rva);
  473.           char *forward_dll_name = forward_name;
  474.           char *forward_func_name = forward_name;
  475.           char *sep = strrchr (forward_name, '.');

  476.           if (sep)
  477.             {
  478.               int len = (int) (sep - forward_name);

  479.               forward_dll_name = alloca (len + 1);
  480.               strncpy (forward_dll_name, forward_name, len);
  481.               forward_dll_name[len] = '\0';
  482.               forward_func_name = ++sep;
  483.             }
  484.           if (add_pe_forwarded_sym (funcname, forward_dll_name,
  485.                                     forward_func_name, ordinal,
  486.                                     dll_name, objfile) != 0)
  487.             ++nbforward;
  488.           continue;
  489.         }

  490.       for (sectix = 0; sectix < otherix; ++sectix)
  491.         {
  492.           if ((func_rva >= section_data[sectix].rva_start)
  493.               && (func_rva < section_data[sectix].rva_end))
  494.             {
  495.               char *sym_name = (char *) (erva + name_rva);

  496.               section_found = 1;
  497.               add_pe_exported_sym (sym_name, func_rva, ordinal,
  498.                                    section_data + sectix, dll_name, objfile);
  499.               ++nbnormal;
  500.               break;
  501.             }
  502.         }
  503.       if (!section_found)
  504.         {
  505.           char *funcname = (char *) (erva + name_rva);

  506.           if (name_rva == 0)
  507.             {
  508.               add_pe_exported_sym (NULL, func_rva, ordinal,
  509.                                    section_data, dll_name, objfile);
  510.               ++nbnormal;
  511.             }
  512.           else if (debug_coff_pe_read)
  513.             fprintf_unfiltered (gdb_stdlog, _("Export name \"%s\" ord. %lu,"
  514.                                 " RVA 0x%lx in dll \"%s\" not handled\n"),
  515.                                 funcname, ordinal, func_rva, dll_name);
  516.         }
  517.     }

  518.   if (debug_coff_pe_read)
  519.     fprintf_unfiltered (gdb_stdlog, _("Finished reading \"%s\", exports %ld,"
  520.                         " forwards %ld, total %ld/%ld.\n"), dll_name, nbnormal,
  521.                         nbforward, nbnormal + nbforward, nexp);
  522.   /* Discard expdata and section_data.  */
  523.   do_cleanups (back_to);
  524. }

  525. /* Extract from ABFD the offset of the .text section.
  526.    This offset is mainly related to the offset within the file.
  527.    The value was previously expected to be 0x1000 for all files,
  528.    but some Windows OS core DLLs seem to use 0x10000 section alignement
  529.    which modified the return value of that function.
  530.    Still return default 0x1000 value if ABFD is NULL or
  531.    if '.text' section is not found, but that should not happen...  */

  532. #define DEFAULT_COFF_PE_TEXT_SECTION_OFFSET 0x1000

  533. CORE_ADDR
  534. pe_text_section_offset (struct bfd *abfd)

  535. {
  536.   unsigned long pe_header_offset, i;
  537.   unsigned long nsections, secptr;
  538.   int is_pe64 = 0;
  539.   int is_pe32 = 0;
  540.   char const *target;

  541.   if (!abfd)
  542.     return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;

  543.   target = bfd_get_target (abfd);

  544.   is_pe64 = (strcmp (target, "pe-x86-64") == 0
  545.              || strcmp (target, "pei-x86-64") == 0);
  546.   is_pe32 = (strcmp (target, "pe-i386") == 0
  547.              || strcmp (target, "pei-i386") == 0
  548.              || strcmp (target, "pe-arm-wince-little") == 0
  549.              || strcmp (target, "pei-arm-wince-little") == 0);

  550.   if (!is_pe32 && !is_pe64)
  551.     {
  552.       /* This is not a recognized PE format file.  Abort now, because
  553.          the code is untested on anything else.  *FIXME* test on
  554.          further architectures and loosen or remove this test.  */
  555.       return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
  556.     }

  557.   /* Get pe_header, optional header and numbers of sections.  */
  558.   pe_header_offset = pe_get32 (abfd, 0x3c);
  559.   nsections = pe_get16 (abfd, pe_header_offset + 4 + 2);
  560.   secptr = (pe_header_offset + 4 + 20 +
  561.             pe_get16 (abfd, pe_header_offset + 4 + 16));

  562.   /* Get the rva and size of the export section.  */
  563.   for (i = 0; i < nsections; i++)
  564.     {
  565.       char sname[SCNNMLEN + 1];
  566.       unsigned long secptr1 = secptr + 40 * i;
  567.       unsigned long vaddr = pe_get32 (abfd, secptr1 + 12);

  568.       bfd_seek (abfd, (file_ptr) secptr1, SEEK_SET);
  569.       bfd_bread (sname, (bfd_size_type) SCNNMLEN, abfd);
  570.       sname[SCNNMLEN] = '\0';
  571.       if (strcmp (sname, ".text") == 0)
  572.         return vaddr;
  573.     }

  574.   return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
  575. }

  576. /* Implements "show debug coff_pe_read" command.  */

  577. static void
  578. show_debug_coff_pe_read (struct ui_file *file, int from_tty,
  579.                          struct cmd_list_element *c, const char *value)
  580. {
  581.   fprintf_filtered (file, _("Coff PE read debugging is %s.\n"), value);
  582. }

  583. /* Provide a prototype to silence -Wmissing-prototypes.  */

  584. void _initialize_coff_pe_read (void);

  585. /* Adds "Set/show debug coff_pe_read" commands.  */

  586. void
  587. _initialize_coff_pe_read (void)
  588. {
  589.   add_setshow_zuinteger_cmd ("coff-pe-read", class_maintenance,
  590.                              &debug_coff_pe_read,
  591.                              _("Set coff PE read debugging."),
  592.                              _("Show coff PE read debugging."),
  593.                              _("When set, debugging messages for coff reading "
  594.                                "of exported symbols are displayed."),
  595.                              NULL, show_debug_coff_pe_read,
  596.                              &setdebuglist, &showdebuglist);
  597. }