Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* Support for the generic parts of PE/PEI; the common executable parts.
  2.    Copyright 1995-2013 Free Software Foundation, Inc.
  3.    Written by Cygnus Solutions.
  4.  
  5.    This file is part of BFD, the Binary File Descriptor library.
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3 of the License, or
  10.    (at your option) any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  20.    MA 02110-1301, USA.  */
  21.  
  22.  
  23. /* Most of this hacked by Steve Chamberlain <sac@cygnus.com>.
  24.  
  25.    PE/PEI rearrangement (and code added): Donn Terry
  26.                                           Softway Systems, Inc.  */
  27.  
  28. /* Hey look, some documentation [and in a place you expect to find it]!
  29.  
  30.    The main reference for the pei format is "Microsoft Portable Executable
  31.    and Common Object File Format Specification 4.1".  Get it if you need to
  32.    do some serious hacking on this code.
  33.  
  34.    Another reference:
  35.    "Peering Inside the PE: A Tour of the Win32 Portable Executable
  36.    File Format", MSJ 1994, Volume 9.
  37.  
  38.    The *sole* difference between the pe format and the pei format is that the
  39.    latter has an MSDOS 2.0 .exe header on the front that prints the message
  40.    "This app must be run under Windows." (or some such).
  41.    (FIXME: Whether that statement is *really* true or not is unknown.
  42.    Are there more subtle differences between pe and pei formats?
  43.    For now assume there aren't.  If you find one, then for God sakes
  44.    document it here!)
  45.  
  46.    The Microsoft docs use the word "image" instead of "executable" because
  47.    the former can also refer to a DLL (shared library).  Confusion can arise
  48.    because the `i' in `pei' also refers to "image".  The `pe' format can
  49.    also create images (i.e. executables), it's just that to run on a win32
  50.    system you need to use the pei format.
  51.  
  52.    FIXME: Please add more docs here so the next poor fool that has to hack
  53.    on this code has a chance of getting something accomplished without
  54.    wasting too much time.  */
  55.  
  56. /* This expands into COFF_WITH_pe, COFF_WITH_pep, or COFF_WITH_pex64
  57.    depending on whether we're compiling for straight PE or PE+.  */
  58. #define COFF_WITH_pe
  59.  
  60. #include "sysdep.h"
  61. #include "bfd.h"
  62. #include "libbfd.h"
  63. #include "coff/internal.h"
  64. #include "bfdver.h"
  65.  
  66. /* NOTE: it's strange to be including an architecture specific header
  67.    in what's supposed to be general (to PE/PEI) code.  However, that's
  68.    where the definitions are, and they don't vary per architecture
  69.    within PE/PEI, so we get them from there.  FIXME: The lack of
  70.    variance is an assumption which may prove to be incorrect if new
  71.    PE/PEI targets are created.  */
  72. #if defined COFF_WITH_pex64
  73. # include "coff/x86_64.h"
  74. #elif defined COFF_WITH_pep
  75. # include "coff/ia64.h"
  76. #else
  77. # include "coff/i386.h"
  78. #endif
  79.  
  80. #include "coff/pe.h"
  81. #include "libcoff.h"
  82. #include "libpei.h"
  83.  
  84. #if defined COFF_WITH_pep || defined COFF_WITH_pex64
  85. # undef AOUTSZ
  86. # define AOUTSZ         PEPAOUTSZ
  87. # define PEAOUTHDR      PEPAOUTHDR
  88. #endif
  89.  
  90. /* FIXME: This file has various tests of POWERPC_LE_PE.  Those tests
  91.    worked when the code was in peicode.h, but no longer work now that
  92.    the code is in peigen.c.  PowerPC NT is said to be dead.  If
  93.    anybody wants to revive the code, you will have to figure out how
  94.    to handle those issues.  */
  95. void
  96. _bfd_pei_swap_sym_in (bfd * abfd, void * ext1, void * in1)
  97. {
  98.   SYMENT *ext = (SYMENT *) ext1;
  99.   struct internal_syment *in = (struct internal_syment *) in1;
  100.  
  101.   if (ext->e.e_name[0] == 0)
  102.     {
  103.       in->_n._n_n._n_zeroes = 0;
  104.       in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
  105.     }
  106.   else
  107.     memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
  108.  
  109.   in->n_value = H_GET_32 (abfd, ext->e_value);
  110.   in->n_scnum = H_GET_16 (abfd, ext->e_scnum);
  111.  
  112.   if (sizeof (ext->e_type) == 2)
  113.     in->n_type = H_GET_16 (abfd, ext->e_type);
  114.   else
  115.     in->n_type = H_GET_32 (abfd, ext->e_type);
  116.  
  117.   in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
  118.   in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
  119.  
  120. #ifndef STRICT_PE_FORMAT
  121.   /* This is for Gnu-created DLLs.  */
  122.  
  123.   /* The section symbols for the .idata$ sections have class 0x68
  124.      (C_SECTION), which MS documentation indicates is a section
  125.      symbol.  Unfortunately, the value field in the symbol is simply a
  126.      copy of the .idata section's flags rather than something useful.
  127.      When these symbols are encountered, change the value to 0 so that
  128.      they will be handled somewhat correctly in the bfd code.  */
  129.   if (in->n_sclass == C_SECTION)
  130.     {
  131.       char namebuf[SYMNMLEN + 1];
  132.       const char *name = NULL;
  133.  
  134.       in->n_value = 0x0;
  135.  
  136.       /* Create synthetic empty sections as needed.  DJ */
  137.       if (in->n_scnum == 0)
  138.         {
  139.           asection *sec;
  140.  
  141.           name = _bfd_coff_internal_syment_name (abfd, in, namebuf);
  142.           if (name == NULL)
  143.             /* FIXME: Return error.  */
  144.             abort ();
  145.           sec = bfd_get_section_by_name (abfd, name);
  146.           if (sec != NULL)
  147.             in->n_scnum = sec->target_index;
  148.         }
  149.  
  150.       if (in->n_scnum == 0)
  151.         {
  152.           int unused_section_number = 0;
  153.           asection *sec;
  154.           flagword flags;
  155.  
  156.           for (sec = abfd->sections; sec; sec = sec->next)
  157.             if (unused_section_number <= sec->target_index)
  158.               unused_section_number = sec->target_index + 1;
  159.  
  160.           if (name == namebuf)
  161.             {
  162.               name = (const char *) bfd_alloc (abfd, strlen (namebuf) + 1);
  163.               if (name == NULL)
  164.                 /* FIXME: Return error.  */
  165.                 abort ();
  166.               strcpy ((char *) name, namebuf);
  167.             }
  168.           flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_DATA | SEC_LOAD;
  169.           sec = bfd_make_section_anyway_with_flags (abfd, name, flags);
  170.           if (sec == NULL)
  171.             /* FIXME: Return error.  */
  172.             abort ();
  173.  
  174.           sec->vma = 0;
  175.           sec->lma = 0;
  176.           sec->size = 0;
  177.           sec->filepos = 0;
  178.           sec->rel_filepos = 0;
  179.           sec->reloc_count = 0;
  180.           sec->line_filepos = 0;
  181.           sec->lineno_count = 0;
  182.           sec->userdata = NULL;
  183.           sec->next = NULL;
  184.           sec->alignment_power = 2;
  185.  
  186.           sec->target_index = unused_section_number;
  187.  
  188.           in->n_scnum = unused_section_number;
  189.         }
  190.       in->n_sclass = C_STAT;
  191.     }
  192. #endif
  193.  
  194. #ifdef coff_swap_sym_in_hook
  195.   /* This won't work in peigen.c, but since it's for PPC PE, it's not
  196.      worth fixing.  */
  197.   coff_swap_sym_in_hook (abfd, ext1, in1);
  198. #endif
  199. }
  200.  
  201. unsigned int
  202. _bfd_pei_swap_sym_out (bfd * abfd, void * inp, void * extp)
  203. {
  204.   struct internal_syment *in = (struct internal_syment *) inp;
  205.   SYMENT *ext = (SYMENT *) extp;
  206.  
  207.   if (in->_n._n_name[0] == 0)
  208.     {
  209.       H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
  210.       H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
  211.     }
  212.   else
  213.     memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
  214.  
  215.   H_PUT_32 (abfd, in->n_value, ext->e_value);
  216.   H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
  217.  
  218.   if (sizeof (ext->e_type) == 2)
  219.     H_PUT_16 (abfd, in->n_type, ext->e_type);
  220.   else
  221.     H_PUT_32 (abfd, in->n_type, ext->e_type);
  222.  
  223.   H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
  224.   H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
  225.  
  226.   return SYMESZ;
  227. }
  228.  
  229. void
  230. _bfd_pei_swap_aux_in (bfd *     abfd,
  231.                       void *    ext1,
  232.                       int       type,
  233.                       int       in_class,
  234.                       int       indx ATTRIBUTE_UNUSED,
  235.                       int       numaux ATTRIBUTE_UNUSED,
  236.                       void *    in1)
  237. {
  238.   AUXENT *ext = (AUXENT *) ext1;
  239.   union internal_auxent *in = (union internal_auxent *) in1;
  240.  
  241.   switch (in_class)
  242.     {
  243.     case C_FILE:
  244.       if (ext->x_file.x_fname[0] == 0)
  245.         {
  246.           in->x_file.x_n.x_zeroes = 0;
  247.           in->x_file.x_n.x_offset = H_GET_32 (abfd, ext->x_file.x_n.x_offset);
  248.         }
  249.       else
  250.         memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
  251.       return;
  252.  
  253.     case C_STAT:
  254.     case C_LEAFSTAT:
  255.     case C_HIDDEN:
  256.       if (type == T_NULL)
  257.         {
  258.           in->x_scn.x_scnlen = GET_SCN_SCNLEN (abfd, ext);
  259.           in->x_scn.x_nreloc = GET_SCN_NRELOC (abfd, ext);
  260.           in->x_scn.x_nlinno = GET_SCN_NLINNO (abfd, ext);
  261.           in->x_scn.x_checksum = H_GET_32 (abfd, ext->x_scn.x_checksum);
  262.           in->x_scn.x_associated = H_GET_16 (abfd, ext->x_scn.x_associated);
  263.           in->x_scn.x_comdat = H_GET_8 (abfd, ext->x_scn.x_comdat);
  264.           return;
  265.         }
  266.       break;
  267.     }
  268.  
  269.   in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->x_sym.x_tagndx);
  270.   in->x_sym.x_tvndx = H_GET_16 (abfd, ext->x_sym.x_tvndx);
  271.  
  272.   if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
  273.       || ISTAG (in_class))
  274.     {
  275.       in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext);
  276.       in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext);
  277.     }
  278.   else
  279.     {
  280.       in->x_sym.x_fcnary.x_ary.x_dimen[0] =
  281.         H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
  282.       in->x_sym.x_fcnary.x_ary.x_dimen[1] =
  283.         H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
  284.       in->x_sym.x_fcnary.x_ary.x_dimen[2] =
  285.         H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
  286.       in->x_sym.x_fcnary.x_ary.x_dimen[3] =
  287.         H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
  288.     }
  289.  
  290.   if (ISFCN (type))
  291.     {
  292.       in->x_sym.x_misc.x_fsize = H_GET_32 (abfd, ext->x_sym.x_misc.x_fsize);
  293.     }
  294.   else
  295.     {
  296.       in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO (abfd, ext);
  297.       in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE (abfd, ext);
  298.     }
  299. }
  300.  
  301. unsigned int
  302. _bfd_pei_swap_aux_out (bfd *  abfd,
  303.                        void * inp,
  304.                        int    type,
  305.                        int    in_class,
  306.                        int    indx ATTRIBUTE_UNUSED,
  307.                        int    numaux ATTRIBUTE_UNUSED,
  308.                        void * extp)
  309. {
  310.   union internal_auxent *in = (union internal_auxent *) inp;
  311.   AUXENT *ext = (AUXENT *) extp;
  312.  
  313.   memset (ext, 0, AUXESZ);
  314.  
  315.   switch (in_class)
  316.     {
  317.     case C_FILE:
  318.       if (in->x_file.x_fname[0] == 0)
  319.         {
  320.           H_PUT_32 (abfd, 0, ext->x_file.x_n.x_zeroes);
  321.           H_PUT_32 (abfd, in->x_file.x_n.x_offset, ext->x_file.x_n.x_offset);
  322.         }
  323.       else
  324.         memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
  325.  
  326.       return AUXESZ;
  327.  
  328.     case C_STAT:
  329.     case C_LEAFSTAT:
  330.     case C_HIDDEN:
  331.       if (type == T_NULL)
  332.         {
  333.           PUT_SCN_SCNLEN (abfd, in->x_scn.x_scnlen, ext);
  334.           PUT_SCN_NRELOC (abfd, in->x_scn.x_nreloc, ext);
  335.           PUT_SCN_NLINNO (abfd, in->x_scn.x_nlinno, ext);
  336.           H_PUT_32 (abfd, in->x_scn.x_checksum, ext->x_scn.x_checksum);
  337.           H_PUT_16 (abfd, in->x_scn.x_associated, ext->x_scn.x_associated);
  338.           H_PUT_8 (abfd, in->x_scn.x_comdat, ext->x_scn.x_comdat);
  339.           return AUXESZ;
  340.         }
  341.       break;
  342.     }
  343.  
  344.   H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->x_sym.x_tagndx);
  345.   H_PUT_16 (abfd, in->x_sym.x_tvndx, ext->x_sym.x_tvndx);
  346.  
  347.   if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
  348.       || ISTAG (in_class))
  349.     {
  350.       PUT_FCN_LNNOPTR (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,  ext);
  351.       PUT_FCN_ENDNDX  (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
  352.     }
  353.   else
  354.     {
  355.       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
  356.                 ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
  357.       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
  358.                 ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
  359.       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
  360.                 ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
  361.       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
  362.                 ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
  363.     }
  364.  
  365.   if (ISFCN (type))
  366.     H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_sym.x_misc.x_fsize);
  367.   else
  368.     {
  369.       PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
  370.       PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
  371.     }
  372.  
  373.   return AUXESZ;
  374. }
  375.  
  376. void
  377. _bfd_pei_swap_lineno_in (bfd * abfd, void * ext1, void * in1)
  378. {
  379.   LINENO *ext = (LINENO *) ext1;
  380.   struct internal_lineno *in = (struct internal_lineno *) in1;
  381.  
  382.   in->l_addr.l_symndx = H_GET_32 (abfd, ext->l_addr.l_symndx);
  383.   in->l_lnno = GET_LINENO_LNNO (abfd, ext);
  384. }
  385.  
  386. unsigned int
  387. _bfd_pei_swap_lineno_out (bfd * abfd, void * inp, void * outp)
  388. {
  389.   struct internal_lineno *in = (struct internal_lineno *) inp;
  390.   struct external_lineno *ext = (struct external_lineno *) outp;
  391.   H_PUT_32 (abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx);
  392.  
  393.   PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
  394.   return LINESZ;
  395. }
  396.  
  397. void
  398. _bfd_pei_swap_aouthdr_in (bfd * abfd,
  399.                           void * aouthdr_ext1,
  400.                           void * aouthdr_int1)
  401. {
  402.   PEAOUTHDR * src = (PEAOUTHDR *) aouthdr_ext1;
  403.   AOUTHDR * aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
  404.   struct internal_aouthdr *aouthdr_int
  405.     = (struct internal_aouthdr *) aouthdr_int1;
  406.   struct internal_extra_pe_aouthdr *a = &aouthdr_int->pe;
  407.  
  408.   aouthdr_int->magic = H_GET_16 (abfd, aouthdr_ext->magic);
  409.   aouthdr_int->vstamp = H_GET_16 (abfd, aouthdr_ext->vstamp);
  410.   aouthdr_int->tsize = GET_AOUTHDR_TSIZE (abfd, aouthdr_ext->tsize);
  411.   aouthdr_int->dsize = GET_AOUTHDR_DSIZE (abfd, aouthdr_ext->dsize);
  412.   aouthdr_int->bsize = GET_AOUTHDR_BSIZE (abfd, aouthdr_ext->bsize);
  413.   aouthdr_int->entry = GET_AOUTHDR_ENTRY (abfd, aouthdr_ext->entry);
  414.   aouthdr_int->text_start =
  415.     GET_AOUTHDR_TEXT_START (abfd, aouthdr_ext->text_start);
  416. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
  417.   /* PE32+ does not have data_start member!  */
  418.   aouthdr_int->data_start =
  419.     GET_AOUTHDR_DATA_START (abfd, aouthdr_ext->data_start);
  420.   a->BaseOfData = aouthdr_int->data_start;
  421. #endif
  422.  
  423.   a->Magic = aouthdr_int->magic;
  424.   a->MajorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp);
  425.   a->MinorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp + 1);
  426.   a->SizeOfCode = aouthdr_int->tsize ;
  427.   a->SizeOfInitializedData = aouthdr_int->dsize ;
  428.   a->SizeOfUninitializedData = aouthdr_int->bsize ;
  429.   a->AddressOfEntryPoint = aouthdr_int->entry;
  430.   a->BaseOfCode = aouthdr_int->text_start;
  431.   a->ImageBase = GET_OPTHDR_IMAGE_BASE (abfd, src->ImageBase);
  432.   a->SectionAlignment = H_GET_32 (abfd, src->SectionAlignment);
  433.   a->FileAlignment = H_GET_32 (abfd, src->FileAlignment);
  434.   a->MajorOperatingSystemVersion =
  435.     H_GET_16 (abfd, src->MajorOperatingSystemVersion);
  436.   a->MinorOperatingSystemVersion =
  437.     H_GET_16 (abfd, src->MinorOperatingSystemVersion);
  438.   a->MajorImageVersion = H_GET_16 (abfd, src->MajorImageVersion);
  439.   a->MinorImageVersion = H_GET_16 (abfd, src->MinorImageVersion);
  440.   a->MajorSubsystemVersion = H_GET_16 (abfd, src->MajorSubsystemVersion);
  441.   a->MinorSubsystemVersion = H_GET_16 (abfd, src->MinorSubsystemVersion);
  442.   a->Reserved1 = H_GET_32 (abfd, src->Reserved1);
  443.   a->SizeOfImage = H_GET_32 (abfd, src->SizeOfImage);
  444.   a->SizeOfHeaders = H_GET_32 (abfd, src->SizeOfHeaders);
  445.   a->CheckSum = H_GET_32 (abfd, src->CheckSum);
  446.   a->Subsystem = H_GET_16 (abfd, src->Subsystem);
  447.   a->DllCharacteristics = H_GET_16 (abfd, src->DllCharacteristics);
  448.   a->SizeOfStackReserve =
  449.     GET_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, src->SizeOfStackReserve);
  450.   a->SizeOfStackCommit =
  451.     GET_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, src->SizeOfStackCommit);
  452.   a->SizeOfHeapReserve =
  453.     GET_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, src->SizeOfHeapReserve);
  454.   a->SizeOfHeapCommit =
  455.     GET_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, src->SizeOfHeapCommit);
  456.   a->LoaderFlags = H_GET_32 (abfd, src->LoaderFlags);
  457.   a->NumberOfRvaAndSizes = H_GET_32 (abfd, src->NumberOfRvaAndSizes);
  458.  
  459.   {
  460.     int idx;
  461.  
  462.     for (idx = 0; idx < a->NumberOfRvaAndSizes; idx++)
  463.       {
  464.         /* If data directory is empty, rva also should be 0.  */
  465.         int size =
  466.           H_GET_32 (abfd, src->DataDirectory[idx][1]);
  467.  
  468.         a->DataDirectory[idx].Size = size;
  469.  
  470.         if (size)
  471.           a->DataDirectory[idx].VirtualAddress =
  472.             H_GET_32 (abfd, src->DataDirectory[idx][0]);
  473.         else
  474.           a->DataDirectory[idx].VirtualAddress = 0;
  475.       }
  476.   }
  477.  
  478.   if (aouthdr_int->entry)
  479.     {
  480.       aouthdr_int->entry += a->ImageBase;
  481. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
  482.       aouthdr_int->entry &= 0xffffffff;
  483. #endif
  484.     }
  485.  
  486.   if (aouthdr_int->tsize)
  487.     {
  488.       aouthdr_int->text_start += a->ImageBase;
  489. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
  490.       aouthdr_int->text_start &= 0xffffffff;
  491. #endif
  492.     }
  493.  
  494. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
  495.   /* PE32+ does not have data_start member!  */
  496.   if (aouthdr_int->dsize)
  497.     {
  498.       aouthdr_int->data_start += a->ImageBase;
  499.       aouthdr_int->data_start &= 0xffffffff;
  500.     }
  501. #endif
  502.  
  503. #ifdef POWERPC_LE_PE
  504.   /* These three fields are normally set up by ppc_relocate_section.
  505.      In the case of reading a file in, we can pick them up from the
  506.      DataDirectory.  */
  507.   first_thunk_address = a->DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress;
  508.   thunk_size = a->DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size;
  509.   import_table_size = a->DataDirectory[PE_IMPORT_TABLE].Size;
  510. #endif
  511. }
  512.  
  513. /* A support function for below.  */
  514.  
  515. static void
  516. add_data_entry (bfd * abfd,
  517.                 struct internal_extra_pe_aouthdr *aout,
  518.                 int idx,
  519.                 char *name,
  520.                 bfd_vma base)
  521. {
  522.   asection *sec = bfd_get_section_by_name (abfd, name);
  523.  
  524.   /* Add import directory information if it exists.  */
  525.   if ((sec != NULL)
  526.       && (coff_section_data (abfd, sec) != NULL)
  527.       && (pei_section_data (abfd, sec) != NULL))
  528.     {
  529.       /* If data directory is empty, rva also should be 0.  */
  530.       int size = pei_section_data (abfd, sec)->virt_size;
  531.       aout->DataDirectory[idx].Size = size;
  532.  
  533.       if (size)
  534.         {
  535.           aout->DataDirectory[idx].VirtualAddress =
  536.             (sec->vma - base) & 0xffffffff;
  537.           sec->flags |= SEC_DATA;
  538.         }
  539.     }
  540. }
  541.  
  542. unsigned int
  543. _bfd_pei_swap_aouthdr_out (bfd * abfd, void * in, void * out)
  544. {
  545.   struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *) in;
  546.   pe_data_type *pe = pe_data (abfd);
  547.   struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
  548.   PEAOUTHDR *aouthdr_out = (PEAOUTHDR *) out;
  549.   bfd_vma sa, fa, ib;
  550.   IMAGE_DATA_DIRECTORY idata2, idata5, tls;
  551.  
  552.   sa = extra->SectionAlignment;
  553.   fa = extra->FileAlignment;
  554.   ib = extra->ImageBase;
  555.  
  556.   idata2 = pe->pe_opthdr.DataDirectory[PE_IMPORT_TABLE];
  557.   idata5 = pe->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE];
  558.   tls = pe->pe_opthdr.DataDirectory[PE_TLS_TABLE];
  559.  
  560.   if (aouthdr_in->tsize)
  561.     {
  562.       aouthdr_in->text_start -= ib;
  563. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
  564.       aouthdr_in->text_start &= 0xffffffff;
  565. #endif
  566.     }
  567.  
  568.   if (aouthdr_in->dsize)
  569.     {
  570.       aouthdr_in->data_start -= ib;
  571. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
  572.       aouthdr_in->data_start &= 0xffffffff;
  573. #endif
  574.     }
  575.  
  576.   if (aouthdr_in->entry)
  577.     {
  578.       aouthdr_in->entry -= ib;
  579. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
  580.       aouthdr_in->entry &= 0xffffffff;
  581. #endif
  582.     }
  583.  
  584. #define FA(x) (((x) + fa -1 ) & (- fa))
  585. #define SA(x) (((x) + sa -1 ) & (- sa))
  586.  
  587.   /* We like to have the sizes aligned.  */
  588.   aouthdr_in->bsize = FA (aouthdr_in->bsize);
  589.  
  590.   extra->NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
  591.  
  592.   add_data_entry (abfd, extra, 0, ".edata", ib);
  593.   add_data_entry (abfd, extra, 2, ".rsrc", ib);
  594.   add_data_entry (abfd, extra, 3, ".pdata", ib);
  595.  
  596.   /* In theory we do not need to call add_data_entry for .idata$2 or
  597.      .idata$5.  It will be done in bfd_coff_final_link where all the
  598.      required information is available.  If however, we are not going
  599.      to perform a final link, eg because we have been invoked by objcopy
  600.      or strip, then we need to make sure that these Data Directory
  601.      entries are initialised properly.
  602.  
  603.      So - we copy the input values into the output values, and then, if
  604.      a final link is going to be performed, it can overwrite them.  */
  605.   extra->DataDirectory[PE_IMPORT_TABLE]  = idata2;
  606.   extra->DataDirectory[PE_IMPORT_ADDRESS_TABLE] = idata5;
  607.   extra->DataDirectory[PE_TLS_TABLE] = tls;
  608.  
  609.   if (extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress == 0)
  610.     /* Until other .idata fixes are made (pending patch), the entry for
  611.        .idata is needed for backwards compatibility.  FIXME.  */
  612.     add_data_entry (abfd, extra, 1, ".idata", ib);
  613.  
  614.   /* For some reason, the virtual size (which is what's set by
  615.      add_data_entry) for .reloc is not the same as the size recorded
  616.      in this slot by MSVC; it doesn't seem to cause problems (so far),
  617.      but since it's the best we've got, use it.  It does do the right
  618.      thing for .pdata.  */
  619.   if (pe->has_reloc_section)
  620.     add_data_entry (abfd, extra, 5, ".reloc", ib);
  621.  
  622.   {
  623.     asection *sec;
  624.     bfd_vma hsize = 0;
  625.     bfd_vma dsize = 0;
  626.     bfd_vma isize = 0;
  627.     bfd_vma tsize = 0;
  628.  
  629.     for (sec = abfd->sections; sec; sec = sec->next)
  630.       {
  631.         int rounded = FA (sec->size);
  632.  
  633.         /* The first non-zero section filepos is the header size.
  634.            Sections without contents will have a filepos of 0.  */
  635.         if (hsize == 0)
  636.           hsize = sec->filepos;
  637.         if (sec->flags & SEC_DATA)
  638.           dsize += rounded;
  639.         if (sec->flags & SEC_CODE)
  640.           tsize += rounded;
  641.         /* The image size is the total VIRTUAL size (which is what is
  642.            in the virt_size field).  Files have been seen (from MSVC
  643.            5.0 link.exe) where the file size of the .data segment is
  644.            quite small compared to the virtual size.  Without this
  645.            fix, strip munges the file.
  646.  
  647.            FIXME: We need to handle holes between sections, which may
  648.            happpen when we covert from another format.  We just use
  649.            the virtual address and virtual size of the last section
  650.            for the image size.  */
  651.         if (coff_section_data (abfd, sec) != NULL
  652.             && pei_section_data (abfd, sec) != NULL)
  653.           isize = (sec->vma - extra->ImageBase
  654.                    + SA (FA (pei_section_data (abfd, sec)->virt_size)));
  655.       }
  656.  
  657.     aouthdr_in->dsize = dsize;
  658.     aouthdr_in->tsize = tsize;
  659.     extra->SizeOfHeaders = hsize;
  660.     extra->SizeOfImage = isize;
  661.   }
  662.  
  663.   H_PUT_16 (abfd, aouthdr_in->magic, aouthdr_out->standard.magic);
  664.  
  665. /* e.g. 219510000 is linker version 2.19  */
  666. #define LINKER_VERSION ((short) (BFD_VERSION / 1000000))
  667.  
  668.   /* This piece of magic sets the "linker version" field to
  669.      LINKER_VERSION.  */
  670.   H_PUT_16 (abfd, (LINKER_VERSION / 100 + (LINKER_VERSION % 100) * 256),
  671.             aouthdr_out->standard.vstamp);
  672.  
  673.   PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, aouthdr_out->standard.tsize);
  674.   PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, aouthdr_out->standard.dsize);
  675.   PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, aouthdr_out->standard.bsize);
  676.   PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, aouthdr_out->standard.entry);
  677.   PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
  678.                           aouthdr_out->standard.text_start);
  679.  
  680. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
  681.   /* PE32+ does not have data_start member!  */
  682.   PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
  683.                           aouthdr_out->standard.data_start);
  684. #endif
  685.  
  686.   PUT_OPTHDR_IMAGE_BASE (abfd, extra->ImageBase, aouthdr_out->ImageBase);
  687.   H_PUT_32 (abfd, extra->SectionAlignment, aouthdr_out->SectionAlignment);
  688.   H_PUT_32 (abfd, extra->FileAlignment, aouthdr_out->FileAlignment);
  689.   H_PUT_16 (abfd, extra->MajorOperatingSystemVersion,
  690.             aouthdr_out->MajorOperatingSystemVersion);
  691.   H_PUT_16 (abfd, extra->MinorOperatingSystemVersion,
  692.             aouthdr_out->MinorOperatingSystemVersion);
  693.   H_PUT_16 (abfd, extra->MajorImageVersion, aouthdr_out->MajorImageVersion);
  694.   H_PUT_16 (abfd, extra->MinorImageVersion, aouthdr_out->MinorImageVersion);
  695.   H_PUT_16 (abfd, extra->MajorSubsystemVersion,
  696.             aouthdr_out->MajorSubsystemVersion);
  697.   H_PUT_16 (abfd, extra->MinorSubsystemVersion,
  698.             aouthdr_out->MinorSubsystemVersion);
  699.   H_PUT_32 (abfd, extra->Reserved1, aouthdr_out->Reserved1);
  700.   H_PUT_32 (abfd, extra->SizeOfImage, aouthdr_out->SizeOfImage);
  701.   H_PUT_32 (abfd, extra->SizeOfHeaders, aouthdr_out->SizeOfHeaders);
  702.   H_PUT_32 (abfd, extra->CheckSum, aouthdr_out->CheckSum);
  703.   H_PUT_16 (abfd, extra->Subsystem, aouthdr_out->Subsystem);
  704.   H_PUT_16 (abfd, extra->DllCharacteristics, aouthdr_out->DllCharacteristics);
  705.   PUT_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, extra->SizeOfStackReserve,
  706.                                     aouthdr_out->SizeOfStackReserve);
  707.   PUT_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, extra->SizeOfStackCommit,
  708.                                    aouthdr_out->SizeOfStackCommit);
  709.   PUT_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, extra->SizeOfHeapReserve,
  710.                                    aouthdr_out->SizeOfHeapReserve);
  711.   PUT_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, extra->SizeOfHeapCommit,
  712.                                   aouthdr_out->SizeOfHeapCommit);
  713.   H_PUT_32 (abfd, extra->LoaderFlags, aouthdr_out->LoaderFlags);
  714.   H_PUT_32 (abfd, extra->NumberOfRvaAndSizes,
  715.             aouthdr_out->NumberOfRvaAndSizes);
  716.   {
  717.     int idx;
  718.  
  719.     for (idx = 0; idx < 16; idx++)
  720.       {
  721.         H_PUT_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
  722.                   aouthdr_out->DataDirectory[idx][0]);
  723.         H_PUT_32 (abfd, extra->DataDirectory[idx].Size,
  724.                   aouthdr_out->DataDirectory[idx][1]);
  725.       }
  726.   }
  727.  
  728.   return AOUTSZ;
  729. }
  730.  
  731. unsigned int
  732. _bfd_pei_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
  733. {
  734.   int idx;
  735.   struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
  736.   struct external_PEI_filehdr *filehdr_out = (struct external_PEI_filehdr *) out;
  737.  
  738.   if (pe_data (abfd)->has_reloc_section
  739.       || pe_data (abfd)->dont_strip_reloc)
  740.     filehdr_in->f_flags &= ~F_RELFLG;
  741.  
  742.   if (pe_data (abfd)->dll)
  743.     filehdr_in->f_flags |= F_DLL;
  744.  
  745.   filehdr_in->pe.e_magic    = DOSMAGIC;
  746.   filehdr_in->pe.e_cblp     = 0x90;
  747.   filehdr_in->pe.e_cp       = 0x3;
  748.   filehdr_in->pe.e_crlc     = 0x0;
  749.   filehdr_in->pe.e_cparhdr  = 0x4;
  750.   filehdr_in->pe.e_minalloc = 0x0;
  751.   filehdr_in->pe.e_maxalloc = 0xffff;
  752.   filehdr_in->pe.e_ss       = 0x0;
  753.   filehdr_in->pe.e_sp       = 0xb8;
  754.   filehdr_in->pe.e_csum     = 0x0;
  755.   filehdr_in->pe.e_ip       = 0x0;
  756.   filehdr_in->pe.e_cs       = 0x0;
  757.   filehdr_in->pe.e_lfarlc   = 0x40;
  758.   filehdr_in->pe.e_ovno     = 0x0;
  759.  
  760.   for (idx = 0; idx < 4; idx++)
  761.     filehdr_in->pe.e_res[idx] = 0x0;
  762.  
  763.   filehdr_in->pe.e_oemid   = 0x0;
  764.   filehdr_in->pe.e_oeminfo = 0x0;
  765.  
  766.   for (idx = 0; idx < 10; idx++)
  767.     filehdr_in->pe.e_res2[idx] = 0x0;
  768.  
  769.   filehdr_in->pe.e_lfanew = 0x80;
  770.  
  771.   /* This next collection of data are mostly just characters.  It
  772.      appears to be constant within the headers put on NT exes.  */
  773.   filehdr_in->pe.dos_message[0]  = 0x0eba1f0e;
  774.   filehdr_in->pe.dos_message[1]  = 0xcd09b400;
  775.   filehdr_in->pe.dos_message[2]  = 0x4c01b821;
  776.   filehdr_in->pe.dos_message[3]  = 0x685421cd;
  777.   filehdr_in->pe.dos_message[4]  = 0x70207369;
  778.   filehdr_in->pe.dos_message[5]  = 0x72676f72;
  779.   filehdr_in->pe.dos_message[6]  = 0x63206d61;
  780.   filehdr_in->pe.dos_message[7]  = 0x6f6e6e61;
  781.   filehdr_in->pe.dos_message[8]  = 0x65622074;
  782.   filehdr_in->pe.dos_message[9]  = 0x6e757220;
  783.   filehdr_in->pe.dos_message[10] = 0x206e6920;
  784.   filehdr_in->pe.dos_message[11] = 0x20534f44;
  785.   filehdr_in->pe.dos_message[12] = 0x65646f6d;
  786.   filehdr_in->pe.dos_message[13] = 0x0a0d0d2e;
  787.   filehdr_in->pe.dos_message[14] = 0x24;
  788.   filehdr_in->pe.dos_message[15] = 0x0;
  789.   filehdr_in->pe.nt_signature = NT_SIGNATURE;
  790.  
  791.   H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
  792.   H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
  793.  
  794.   /* Only use a real timestamp if the option was chosen.  */
  795.   if ((pe_data (abfd)->insert_timestamp))
  796.     H_PUT_32 (abfd, time(0), filehdr_out->f_timdat);
  797.  
  798.   PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
  799.                       filehdr_out->f_symptr);
  800.   H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
  801.   H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
  802.   H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
  803.  
  804.   /* Put in extra dos header stuff.  This data remains essentially
  805.      constant, it just has to be tacked on to the beginning of all exes
  806.      for NT.  */
  807.   H_PUT_16 (abfd, filehdr_in->pe.e_magic, filehdr_out->e_magic);
  808.   H_PUT_16 (abfd, filehdr_in->pe.e_cblp, filehdr_out->e_cblp);
  809.   H_PUT_16 (abfd, filehdr_in->pe.e_cp, filehdr_out->e_cp);
  810.   H_PUT_16 (abfd, filehdr_in->pe.e_crlc, filehdr_out->e_crlc);
  811.   H_PUT_16 (abfd, filehdr_in->pe.e_cparhdr, filehdr_out->e_cparhdr);
  812.   H_PUT_16 (abfd, filehdr_in->pe.e_minalloc, filehdr_out->e_minalloc);
  813.   H_PUT_16 (abfd, filehdr_in->pe.e_maxalloc, filehdr_out->e_maxalloc);
  814.   H_PUT_16 (abfd, filehdr_in->pe.e_ss, filehdr_out->e_ss);
  815.   H_PUT_16 (abfd, filehdr_in->pe.e_sp, filehdr_out->e_sp);
  816.   H_PUT_16 (abfd, filehdr_in->pe.e_csum, filehdr_out->e_csum);
  817.   H_PUT_16 (abfd, filehdr_in->pe.e_ip, filehdr_out->e_ip);
  818.   H_PUT_16 (abfd, filehdr_in->pe.e_cs, filehdr_out->e_cs);
  819.   H_PUT_16 (abfd, filehdr_in->pe.e_lfarlc, filehdr_out->e_lfarlc);
  820.   H_PUT_16 (abfd, filehdr_in->pe.e_ovno, filehdr_out->e_ovno);
  821.  
  822.   for (idx = 0; idx < 4; idx++)
  823.     H_PUT_16 (abfd, filehdr_in->pe.e_res[idx], filehdr_out->e_res[idx]);
  824.  
  825.   H_PUT_16 (abfd, filehdr_in->pe.e_oemid, filehdr_out->e_oemid);
  826.   H_PUT_16 (abfd, filehdr_in->pe.e_oeminfo, filehdr_out->e_oeminfo);
  827.  
  828.   for (idx = 0; idx < 10; idx++)
  829.     H_PUT_16 (abfd, filehdr_in->pe.e_res2[idx], filehdr_out->e_res2[idx]);
  830.  
  831.   H_PUT_32 (abfd, filehdr_in->pe.e_lfanew, filehdr_out->e_lfanew);
  832.  
  833.   for (idx = 0; idx < 16; idx++)
  834.     H_PUT_32 (abfd, filehdr_in->pe.dos_message[idx],
  835.               filehdr_out->dos_message[idx]);
  836.  
  837.   /* Also put in the NT signature.  */
  838.   H_PUT_32 (abfd, filehdr_in->pe.nt_signature, filehdr_out->nt_signature);
  839.  
  840.   return FILHSZ;
  841. }
  842.  
  843. unsigned int
  844. _bfd_pe_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
  845. {
  846.   struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
  847.   FILHDR *filehdr_out = (FILHDR *) out;
  848.  
  849.   H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
  850.   H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
  851.   H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->f_timdat);
  852.   PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr, filehdr_out->f_symptr);
  853.   H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
  854.   H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
  855.   H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
  856.  
  857.   return FILHSZ;
  858. }
  859.  
  860. unsigned int
  861. _bfd_pei_swap_scnhdr_out (bfd * abfd, void * in, void * out)
  862. {
  863.   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
  864.   SCNHDR *scnhdr_ext = (SCNHDR *) out;
  865.   unsigned int ret = SCNHSZ;
  866.   bfd_vma ps;
  867.   bfd_vma ss;
  868.  
  869.   memcpy (scnhdr_ext->s_name, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
  870.  
  871.   PUT_SCNHDR_VADDR (abfd,
  872.                     ((scnhdr_int->s_vaddr
  873.                       - pe_data (abfd)->pe_opthdr.ImageBase)
  874.                      & 0xffffffff),
  875.                     scnhdr_ext->s_vaddr);
  876.  
  877.   /* NT wants the size data to be rounded up to the next
  878.      NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
  879.      sometimes).  */
  880.   if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0)
  881.     {
  882.       if (bfd_pei_p (abfd))
  883.         {
  884.           ps = scnhdr_int->s_size;
  885.           ss = 0;
  886.         }
  887.       else
  888.        {
  889.          ps = 0;
  890.          ss = scnhdr_int->s_size;
  891.        }
  892.     }
  893.   else
  894.     {
  895.       if (bfd_pei_p (abfd))
  896.         ps = scnhdr_int->s_paddr;
  897.       else
  898.         ps = 0;
  899.  
  900.       ss = scnhdr_int->s_size;
  901.     }
  902.  
  903.   PUT_SCNHDR_SIZE (abfd, ss,
  904.                    scnhdr_ext->s_size);
  905.  
  906.   /* s_paddr in PE is really the virtual size.  */
  907.   PUT_SCNHDR_PADDR (abfd, ps, scnhdr_ext->s_paddr);
  908.  
  909.   PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
  910.                      scnhdr_ext->s_scnptr);
  911.   PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
  912.                      scnhdr_ext->s_relptr);
  913.   PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
  914.                       scnhdr_ext->s_lnnoptr);
  915.  
  916.   {
  917.     /* Extra flags must be set when dealing with PE.  All sections should also
  918.        have the IMAGE_SCN_MEM_READ (0x40000000) flag set.  In addition, the
  919.        .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data
  920.        sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set
  921.        (this is especially important when dealing with the .idata section since
  922.        the addresses for routines from .dlls must be overwritten).  If .reloc
  923.        section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE
  924.        (0x02000000).  Also, the resource data should also be read and
  925.        writable.  */
  926.  
  927.     /* FIXME: Alignment is also encoded in this field, at least on PPC and
  928.        ARM-WINCE.  Although - how do we get the original alignment field
  929.        back ?  */
  930.  
  931.     typedef struct
  932.     {
  933.       const char *      section_name;
  934.       unsigned long     must_have;
  935.     }
  936.     pe_required_section_flags;
  937.  
  938.     pe_required_section_flags known_sections [] =
  939.       {
  940.         { ".arch",  IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_ALIGN_8BYTES },
  941.         { ".bss",   IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
  942.         { ".data",  IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
  943.         { ".edata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
  944.         { ".idata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
  945.         { ".pdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
  946.         { ".rdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
  947.         { ".reloc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE },
  948.         { ".rsrc",  IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
  949.         { ".text" , IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE },
  950.         { ".tls",   IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
  951.         { ".xdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
  952.         { NULL, 0}
  953.       };
  954.  
  955.     pe_required_section_flags * p;
  956.  
  957.     /* We have defaulted to adding the IMAGE_SCN_MEM_WRITE flag, but now
  958.        we know exactly what this specific section wants so we remove it
  959.        and then allow the must_have field to add it back in if necessary.
  960.        However, we don't remove IMAGE_SCN_MEM_WRITE flag from .text if the
  961.        default WP_TEXT file flag has been cleared.  WP_TEXT may be cleared
  962.        by ld --enable-auto-import (if auto-import is actually needed),
  963.        by ld --omagic, or by obcopy --writable-text.  */
  964.  
  965.     for (p = known_sections; p->section_name; p++)
  966.       if (strcmp (scnhdr_int->s_name, p->section_name) == 0)
  967.         {
  968.           if (strcmp (scnhdr_int->s_name, ".text")
  969.               || (bfd_get_file_flags (abfd) & WP_TEXT))
  970.             scnhdr_int->s_flags &= ~IMAGE_SCN_MEM_WRITE;
  971.           scnhdr_int->s_flags |= p->must_have;
  972.           break;
  973.         }
  974.  
  975.     H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
  976.   }
  977.  
  978.   if (coff_data (abfd)->link_info
  979.       && ! coff_data (abfd)->link_info->relocatable
  980.       && ! coff_data (abfd)->link_info->shared
  981.       && strcmp (scnhdr_int->s_name, ".text") == 0)
  982.     {
  983.       /* By inference from looking at MS output, the 32 bit field
  984.          which is the combination of the number_of_relocs and
  985.          number_of_linenos is used for the line number count in
  986.          executables.  A 16-bit field won't do for cc1.  The MS
  987.          document says that the number of relocs is zero for
  988.          executables, but the 17-th bit has been observed to be there.
  989.          Overflow is not an issue: a 4G-line program will overflow a
  990.          bunch of other fields long before this!  */
  991.       H_PUT_16 (abfd, (scnhdr_int->s_nlnno & 0xffff), scnhdr_ext->s_nlnno);
  992.       H_PUT_16 (abfd, (scnhdr_int->s_nlnno >> 16), scnhdr_ext->s_nreloc);
  993.     }
  994.   else
  995.     {
  996.       if (scnhdr_int->s_nlnno <= 0xffff)
  997.         H_PUT_16 (abfd, scnhdr_int->s_nlnno, scnhdr_ext->s_nlnno);
  998.       else
  999.         {
  1000.           (*_bfd_error_handler) (_("%s: line number overflow: 0x%lx > 0xffff"),
  1001.                                  bfd_get_filename (abfd),
  1002.                                  scnhdr_int->s_nlnno);
  1003.           bfd_set_error (bfd_error_file_truncated);
  1004.           H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nlnno);
  1005.           ret = 0;
  1006.         }
  1007.  
  1008.       /* Although we could encode 0xffff relocs here, we do not, to be
  1009.          consistent with other parts of bfd. Also it lets us warn, as
  1010.          we should never see 0xffff here w/o having the overflow flag
  1011.          set.  */
  1012.       if (scnhdr_int->s_nreloc < 0xffff)
  1013.         H_PUT_16 (abfd, scnhdr_int->s_nreloc, scnhdr_ext->s_nreloc);
  1014.       else
  1015.         {
  1016.           /* PE can deal with large #s of relocs, but not here.  */
  1017.           H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nreloc);
  1018.           scnhdr_int->s_flags |= IMAGE_SCN_LNK_NRELOC_OVFL;
  1019.           H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
  1020.         }
  1021.     }
  1022.   return ret;
  1023. }
  1024.  
  1025. static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
  1026. {
  1027.   N_("Export Directory [.edata (or where ever we found it)]"),
  1028.   N_("Import Directory [parts of .idata]"),
  1029.   N_("Resource Directory [.rsrc]"),
  1030.   N_("Exception Directory [.pdata]"),
  1031.   N_("Security Directory"),
  1032.   N_("Base Relocation Directory [.reloc]"),
  1033.   N_("Debug Directory"),
  1034.   N_("Description Directory"),
  1035.   N_("Special Directory"),
  1036.   N_("Thread Storage Directory [.tls]"),
  1037.   N_("Load Configuration Directory"),
  1038.   N_("Bound Import Directory"),
  1039.   N_("Import Address Table Directory"),
  1040.   N_("Delay Import Directory"),
  1041.   N_("CLR Runtime Header"),
  1042.   N_("Reserved")
  1043. };
  1044.  
  1045. #ifdef POWERPC_LE_PE
  1046. /* The code for the PPC really falls in the "architecture dependent"
  1047.    category.  However, it's not clear that anyone will ever care, so
  1048.    we're ignoring the issue for now; if/when PPC matters, some of this
  1049.    may need to go into peicode.h, or arguments passed to enable the
  1050.    PPC- specific code.  */
  1051. #endif
  1052.  
  1053. static bfd_boolean
  1054. pe_print_idata (bfd * abfd, void * vfile)
  1055. {
  1056.   FILE *file = (FILE *) vfile;
  1057.   bfd_byte *data;
  1058.   asection *section;
  1059.   bfd_signed_vma adj;
  1060.  
  1061. #ifdef POWERPC_LE_PE
  1062.   asection *rel_section = bfd_get_section_by_name (abfd, ".reldata");
  1063. #endif
  1064.  
  1065.   bfd_size_type datasize = 0;
  1066.   bfd_size_type dataoff;
  1067.   bfd_size_type i;
  1068.   int onaline = 20;
  1069.  
  1070.   pe_data_type *pe = pe_data (abfd);
  1071.   struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
  1072.  
  1073.   bfd_vma addr;
  1074.  
  1075.   addr = extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress;
  1076.  
  1077.   if (addr == 0 && extra->DataDirectory[PE_IMPORT_TABLE].Size == 0)
  1078.     {
  1079.       /* Maybe the extra header isn't there.  Look for the section.  */
  1080.       section = bfd_get_section_by_name (abfd, ".idata");
  1081.       if (section == NULL)
  1082.         return TRUE;
  1083.  
  1084.       addr = section->vma;
  1085.       datasize = section->size;
  1086.       if (datasize == 0)
  1087.         return TRUE;
  1088.     }
  1089.   else
  1090.     {
  1091.       addr += extra->ImageBase;
  1092.       for (section = abfd->sections; section != NULL; section = section->next)
  1093.         {
  1094.           datasize = section->size;
  1095.           if (addr >= section->vma && addr < section->vma + datasize)
  1096.             break;
  1097.         }
  1098.  
  1099.       if (section == NULL)
  1100.         {
  1101.           fprintf (file,
  1102.                    _("\nThere is an import table, but the section containing it could not be found\n"));
  1103.           return TRUE;
  1104.         }
  1105.     }
  1106.  
  1107.   fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
  1108.            section->name, (unsigned long) addr);
  1109.  
  1110.   dataoff = addr - section->vma;
  1111.  
  1112. #ifdef POWERPC_LE_PE
  1113.   if (rel_section != 0 && rel_section->size != 0)
  1114.     {
  1115.       /* The toc address can be found by taking the starting address,
  1116.          which on the PPC locates a function descriptor. The
  1117.          descriptor consists of the function code starting address
  1118.          followed by the address of the toc. The starting address we
  1119.          get from the bfd, and the descriptor is supposed to be in the
  1120.          .reldata section.  */
  1121.  
  1122.       bfd_vma loadable_toc_address;
  1123.       bfd_vma toc_address;
  1124.       bfd_vma start_address;
  1125.       bfd_byte *data;
  1126.       bfd_vma offset;
  1127.  
  1128.       if (!bfd_malloc_and_get_section (abfd, rel_section, &data))
  1129.         {
  1130.           if (data != NULL)
  1131.             free (data);
  1132.           return FALSE;
  1133.         }
  1134.  
  1135.       offset = abfd->start_address - rel_section->vma;
  1136.  
  1137.       if (offset >= rel_section->size || offset + 8 > rel_section->size)
  1138.         {
  1139.           if (data != NULL)
  1140.             free (data);
  1141.           return FALSE;
  1142.         }
  1143.  
  1144.       start_address = bfd_get_32 (abfd, data + offset);
  1145.       loadable_toc_address = bfd_get_32 (abfd, data + offset + 4);
  1146.       toc_address = loadable_toc_address - 32768;
  1147.  
  1148.       fprintf (file,
  1149.                _("\nFunction descriptor located at the start address: %04lx\n"),
  1150.                (unsigned long int) (abfd->start_address));
  1151.       fprintf (file,
  1152.                _("\tcode-base %08lx toc (loadable/actual) %08lx/%08lx\n"),
  1153.                start_address, loadable_toc_address, toc_address);
  1154.       if (data != NULL)
  1155.         free (data);
  1156.     }
  1157.   else
  1158.     {
  1159.       fprintf (file,
  1160.                _("\nNo reldata section! Function descriptor not decoded.\n"));
  1161.     }
  1162. #endif
  1163.  
  1164.   fprintf (file,
  1165.            _("\nThe Import Tables (interpreted %s section contents)\n"),
  1166.            section->name);
  1167.   fprintf (file,
  1168.            _("\
  1169. vma:            Hint    Time      Forward  DLL       First\n\
  1170.                 Table   Stamp     Chain    Name      Thunk\n"));
  1171.  
  1172.   /* Read the whole section.  Some of the fields might be before dataoff.  */
  1173.   if (!bfd_malloc_and_get_section (abfd, section, &data))
  1174.     {
  1175.       if (data != NULL)
  1176.         free (data);
  1177.       return FALSE;
  1178.     }
  1179.  
  1180.   adj = section->vma - extra->ImageBase;
  1181.  
  1182.   /* Print all image import descriptors.  */
  1183.   for (i = dataoff; i + onaline <= datasize; i += onaline)
  1184.     {
  1185.       bfd_vma hint_addr;
  1186.       bfd_vma time_stamp;
  1187.       bfd_vma forward_chain;
  1188.       bfd_vma dll_name;
  1189.       bfd_vma first_thunk;
  1190.       int idx = 0;
  1191.       bfd_size_type j;
  1192.       char *dll;
  1193.  
  1194.       /* Print (i + extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress).  */
  1195.       fprintf (file, " %08lx\t", (unsigned long) (i + adj));
  1196.       hint_addr = bfd_get_32 (abfd, data + i);
  1197.       time_stamp = bfd_get_32 (abfd, data + i + 4);
  1198.       forward_chain = bfd_get_32 (abfd, data + i + 8);
  1199.       dll_name = bfd_get_32 (abfd, data + i + 12);
  1200.       first_thunk = bfd_get_32 (abfd, data + i + 16);
  1201.  
  1202.       fprintf (file, "%08lx %08lx %08lx %08lx %08lx\n",
  1203.                (unsigned long) hint_addr,
  1204.                (unsigned long) time_stamp,
  1205.                (unsigned long) forward_chain,
  1206.                (unsigned long) dll_name,
  1207.                (unsigned long) first_thunk);
  1208.  
  1209.       if (hint_addr == 0 && first_thunk == 0)
  1210.         break;
  1211.  
  1212.       if (dll_name - adj >= section->size)
  1213.         break;
  1214.  
  1215.       dll = (char *) data + dll_name - adj;
  1216.       fprintf (file, _("\n\tDLL Name: %s\n"), dll);
  1217.  
  1218.       if (hint_addr != 0)
  1219.         {
  1220.           bfd_byte *ft_data;
  1221.           asection *ft_section;
  1222.           bfd_vma ft_addr;
  1223.           bfd_size_type ft_datasize;
  1224.           int ft_idx;
  1225.           int ft_allocated;
  1226.  
  1227.           fprintf (file, _("\tvma:  Hint/Ord Member-Name Bound-To\n"));
  1228.  
  1229.           idx = hint_addr - adj;
  1230.  
  1231.           ft_addr = first_thunk + extra->ImageBase;
  1232.           ft_idx = first_thunk - adj;
  1233.           ft_data = data + ft_idx;
  1234.           ft_datasize = datasize - ft_idx;
  1235.           ft_allocated = 0;
  1236.  
  1237.           if (first_thunk != hint_addr)
  1238.             {
  1239.               /* Find the section which contains the first thunk.  */
  1240.               for (ft_section = abfd->sections;
  1241.                    ft_section != NULL;
  1242.                    ft_section = ft_section->next)
  1243.                 {
  1244.                   if (ft_addr >= ft_section->vma
  1245.                       && ft_addr < ft_section->vma + ft_section->size)
  1246.                     break;
  1247.                 }
  1248.  
  1249.               if (ft_section == NULL)
  1250.                 {
  1251.                   fprintf (file,
  1252.                        _("\nThere is a first thunk, but the section containing it could not be found\n"));
  1253.                   continue;
  1254.                 }
  1255.  
  1256.               /* Now check to see if this section is the same as our current
  1257.                  section.  If it is not then we will have to load its data in.  */
  1258.               if (ft_section != section)
  1259.                 {
  1260.                   ft_idx = first_thunk - (ft_section->vma - extra->ImageBase);
  1261.                   ft_datasize = ft_section->size - ft_idx;
  1262.                   ft_data = (bfd_byte *) bfd_malloc (ft_datasize);
  1263.                   if (ft_data == NULL)
  1264.                     continue;
  1265.  
  1266.                   /* Read ft_datasize bytes starting at offset ft_idx.  */
  1267.                   if (!bfd_get_section_contents (abfd, ft_section, ft_data,
  1268.                                                  (bfd_vma) ft_idx, ft_datasize))
  1269.                     {
  1270.                       free (ft_data);
  1271.                       continue;
  1272.                     }
  1273.                   ft_allocated = 1;
  1274.                 }
  1275.             }
  1276.  
  1277.           /* Print HintName vector entries.  */
  1278. #ifdef COFF_WITH_pex64
  1279.           for (j = 0; idx + j + 8 <= datasize; j += 8)
  1280.             {
  1281.               unsigned long member = bfd_get_32 (abfd, data + idx + j);
  1282.               unsigned long member_high = bfd_get_32 (abfd, data + idx + j + 4);
  1283.  
  1284.               if (!member && !member_high)
  1285.                 break;
  1286.  
  1287.               if (member_high & 0x80000000)
  1288.                 fprintf (file, "\t%lx%08lx\t %4lx%08lx  <none>",
  1289.                          member_high,member, member_high & 0x7fffffff, member);
  1290.               else
  1291.                 {
  1292.                   int ordinal;
  1293.                   char *member_name;
  1294.  
  1295.                   ordinal = bfd_get_16 (abfd, data + member - adj);
  1296.                   member_name = (char *) data + member - adj + 2;
  1297.                   fprintf (file, "\t%04lx\t %4d  %s",member, ordinal, member_name);
  1298.                 }
  1299.  
  1300.               /* If the time stamp is not zero, the import address
  1301.                  table holds actual addresses.  */
  1302.               if (time_stamp != 0
  1303.                   && first_thunk != 0
  1304.                   && first_thunk != hint_addr
  1305.                   && j + 4 <= ft_datasize)
  1306.                 fprintf (file, "\t%04lx",
  1307.                          (unsigned long) bfd_get_32 (abfd, ft_data + j));
  1308.               fprintf (file, "\n");
  1309.             }
  1310. #else
  1311.           for (j = 0; idx + j + 4 <= datasize; j += 4)
  1312.             {
  1313.               unsigned long member = bfd_get_32 (abfd, data + idx + j);
  1314.  
  1315.               /* Print single IMAGE_IMPORT_BY_NAME vector.  */
  1316.               if (member == 0)
  1317.                 break;
  1318.  
  1319.               if (member & 0x80000000)
  1320.                 fprintf (file, "\t%04lx\t %4lu  <none>",
  1321.                          member, member & 0x7fffffff);
  1322.               else
  1323.                 {
  1324.                   int ordinal;
  1325.                   char *member_name;
  1326.  
  1327.                   ordinal = bfd_get_16 (abfd, data + member - adj);
  1328.                   member_name = (char *) data + member - adj + 2;
  1329.                   fprintf (file, "\t%04lx\t %4d  %s",
  1330.                            member, ordinal, member_name);
  1331.                 }
  1332.  
  1333.               /* If the time stamp is not zero, the import address
  1334.                  table holds actual addresses.  */
  1335.               if (time_stamp != 0
  1336.                   && first_thunk != 0
  1337.                   && first_thunk != hint_addr
  1338.                   && j + 4 <= ft_datasize)
  1339.                 fprintf (file, "\t%04lx",
  1340.                          (unsigned long) bfd_get_32 (abfd, ft_data + j));
  1341.  
  1342.               fprintf (file, "\n");
  1343.             }
  1344. #endif
  1345.           if (ft_allocated)
  1346.             free (ft_data);
  1347.         }
  1348.  
  1349.       fprintf (file, "\n");
  1350.     }
  1351.  
  1352.   free (data);
  1353.  
  1354.   return TRUE;
  1355. }
  1356.  
  1357. static bfd_boolean
  1358. pe_print_edata (bfd * abfd, void * vfile)
  1359. {
  1360.   FILE *file = (FILE *) vfile;
  1361.   bfd_byte *data;
  1362.   asection *section;
  1363.   bfd_size_type datasize = 0;
  1364.   bfd_size_type dataoff;
  1365.   bfd_size_type i;
  1366.   bfd_signed_vma adj;
  1367.   struct EDT_type
  1368.   {
  1369.     long export_flags;          /* Reserved - should be zero.  */
  1370.     long time_stamp;
  1371.     short major_ver;
  1372.     short minor_ver;
  1373.     bfd_vma name;               /* RVA - relative to image base.  */
  1374.     long base;                  /* Ordinal base.  */
  1375.     unsigned long num_functions;/* Number in the export address table.  */
  1376.     unsigned long num_names;    /* Number in the name pointer table.  */
  1377.     bfd_vma eat_addr;           /* RVA to the export address table.  */
  1378.     bfd_vma npt_addr;           /* RVA to the Export Name Pointer Table.  */
  1379.     bfd_vma ot_addr;            /* RVA to the Ordinal Table.  */
  1380.   } edt;
  1381.  
  1382.   pe_data_type *pe = pe_data (abfd);
  1383.   struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
  1384.  
  1385.   bfd_vma addr;
  1386.  
  1387.   addr = extra->DataDirectory[PE_EXPORT_TABLE].VirtualAddress;
  1388.  
  1389.   if (addr == 0 && extra->DataDirectory[PE_EXPORT_TABLE].Size == 0)
  1390.     {
  1391.       /* Maybe the extra header isn't there.  Look for the section.  */
  1392.       section = bfd_get_section_by_name (abfd, ".edata");
  1393.       if (section == NULL)
  1394.         return TRUE;
  1395.  
  1396.       addr = section->vma;
  1397.       dataoff = 0;
  1398.       datasize = section->size;
  1399.       if (datasize == 0)
  1400.         return TRUE;
  1401.     }
  1402.   else
  1403.     {
  1404.       addr += extra->ImageBase;
  1405.  
  1406.       for (section = abfd->sections; section != NULL; section = section->next)
  1407.         if (addr >= section->vma && addr < section->vma + section->size)
  1408.           break;
  1409.  
  1410.       if (section == NULL)
  1411.         {
  1412.           fprintf (file,
  1413.                    _("\nThere is an export table, but the section containing it could not be found\n"));
  1414.           return TRUE;
  1415.         }
  1416.  
  1417.       dataoff = addr - section->vma;
  1418.       datasize = extra->DataDirectory[PE_EXPORT_TABLE].Size;
  1419.       if (datasize > section->size - dataoff)
  1420.         {
  1421.           fprintf (file,
  1422.                    _("\nThere is an export table in %s, but it does not fit into that section\n"),
  1423.                    section->name);
  1424.           return TRUE;
  1425.         }
  1426.     }
  1427.  
  1428.   fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
  1429.            section->name, (unsigned long) addr);
  1430.  
  1431.   data = (bfd_byte *) bfd_malloc (datasize);
  1432.   if (data == NULL)
  1433.     return FALSE;
  1434.  
  1435.   if (! bfd_get_section_contents (abfd, section, data,
  1436.                                   (file_ptr) dataoff, datasize))
  1437.     return FALSE;
  1438.  
  1439.   /* Go get Export Directory Table.  */
  1440.   edt.export_flags   = bfd_get_32 (abfd, data +  0);
  1441.   edt.time_stamp     = bfd_get_32 (abfd, data +  4);
  1442.   edt.major_ver      = bfd_get_16 (abfd, data +  8);
  1443.   edt.minor_ver      = bfd_get_16 (abfd, data + 10);
  1444.   edt.name           = bfd_get_32 (abfd, data + 12);
  1445.   edt.base           = bfd_get_32 (abfd, data + 16);
  1446.   edt.num_functions  = bfd_get_32 (abfd, data + 20);
  1447.   edt.num_names      = bfd_get_32 (abfd, data + 24);
  1448.   edt.eat_addr       = bfd_get_32 (abfd, data + 28);
  1449.   edt.npt_addr       = bfd_get_32 (abfd, data + 32);
  1450.   edt.ot_addr        = bfd_get_32 (abfd, data + 36);
  1451.  
  1452.   adj = section->vma - extra->ImageBase + dataoff;
  1453.  
  1454.   /* Dump the EDT first.  */
  1455.   fprintf (file,
  1456.            _("\nThe Export Tables (interpreted %s section contents)\n\n"),
  1457.            section->name);
  1458.  
  1459.   fprintf (file,
  1460.            _("Export Flags \t\t\t%lx\n"), (unsigned long) edt.export_flags);
  1461.  
  1462.   fprintf (file,
  1463.            _("Time/Date stamp \t\t%lx\n"), (unsigned long) edt.time_stamp);
  1464.  
  1465.   fprintf (file,
  1466.            _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
  1467.  
  1468.   fprintf (file,
  1469.            _("Name \t\t\t\t"));
  1470.   bfd_fprintf_vma (abfd, file, edt.name);
  1471.   fprintf (file,
  1472.            " %s\n", data + edt.name - adj);
  1473.  
  1474.   fprintf (file,
  1475.            _("Ordinal Base \t\t\t%ld\n"), edt.base);
  1476.  
  1477.   fprintf (file,
  1478.            _("Number in:\n"));
  1479.  
  1480.   fprintf (file,
  1481.            _("\tExport Address Table \t\t%08lx\n"),
  1482.            edt.num_functions);
  1483.  
  1484.   fprintf (file,
  1485.            _("\t[Name Pointer/Ordinal] Table\t%08lx\n"), edt.num_names);
  1486.  
  1487.   fprintf (file,
  1488.            _("Table Addresses\n"));
  1489.  
  1490.   fprintf (file,
  1491.            _("\tExport Address Table \t\t"));
  1492.   bfd_fprintf_vma (abfd, file, edt.eat_addr);
  1493.   fprintf (file, "\n");
  1494.  
  1495.   fprintf (file,
  1496.            _("\tName Pointer Table \t\t"));
  1497.   bfd_fprintf_vma (abfd, file, edt.npt_addr);
  1498.   fprintf (file, "\n");
  1499.  
  1500.   fprintf (file,
  1501.            _("\tOrdinal Table \t\t\t"));
  1502.   bfd_fprintf_vma (abfd, file, edt.ot_addr);
  1503.   fprintf (file, "\n");
  1504.  
  1505.   /* The next table to find is the Export Address Table. It's basically
  1506.      a list of pointers that either locate a function in this dll, or
  1507.      forward the call to another dll. Something like:
  1508.       typedef union
  1509.       {
  1510.         long export_rva;
  1511.         long forwarder_rva;
  1512.       } export_address_table_entry;  */
  1513.  
  1514.   fprintf (file,
  1515.           _("\nExport Address Table -- Ordinal Base %ld\n"),
  1516.           edt.base);
  1517.  
  1518.   for (i = 0; i < edt.num_functions; ++i)
  1519.     {
  1520.       bfd_vma eat_member = bfd_get_32 (abfd,
  1521.                                        data + edt.eat_addr + (i * 4) - adj);
  1522.       if (eat_member == 0)
  1523.         continue;
  1524.  
  1525.       if (eat_member - adj <= datasize)
  1526.         {
  1527.           /* This rva is to a name (forwarding function) in our section.  */
  1528.           /* Should locate a function descriptor.  */
  1529.           fprintf (file,
  1530.                    "\t[%4ld] +base[%4ld] %04lx %s -- %s\n",
  1531.                    (long) i,
  1532.                    (long) (i + edt.base),
  1533.                    (unsigned long) eat_member,
  1534.                    _("Forwarder RVA"),
  1535.                    data + eat_member - adj);
  1536.         }
  1537.       else
  1538.         {
  1539.           /* Should locate a function descriptor in the reldata section.  */
  1540.           fprintf (file,
  1541.                    "\t[%4ld] +base[%4ld] %04lx %s\n",
  1542.                    (long) i,
  1543.                    (long) (i + edt.base),
  1544.                    (unsigned long) eat_member,
  1545.                    _("Export RVA"));
  1546.         }
  1547.     }
  1548.  
  1549.   /* The Export Name Pointer Table is paired with the Export Ordinal Table.  */
  1550.   /* Dump them in parallel for clarity.  */
  1551.   fprintf (file,
  1552.            _("\n[Ordinal/Name Pointer] Table\n"));
  1553.  
  1554.   for (i = 0; i < edt.num_names; ++i)
  1555.     {
  1556.       bfd_vma name_ptr = bfd_get_32 (abfd,
  1557.                                     data +
  1558.                                     edt.npt_addr
  1559.                                     + (i*4) - adj);
  1560.  
  1561.       char *name = (char *) data + name_ptr - adj;
  1562.  
  1563.       bfd_vma ord = bfd_get_16 (abfd,
  1564.                                     data +
  1565.                                     edt.ot_addr
  1566.                                     + (i*2) - adj);
  1567.       fprintf (file,
  1568.               "\t[%4ld] %s\n", (long) ord, name);
  1569.     }
  1570.  
  1571.   free (data);
  1572.  
  1573.   return TRUE;
  1574. }
  1575.  
  1576. /* This really is architecture dependent.  On IA-64, a .pdata entry
  1577.    consists of three dwords containing relative virtual addresses that
  1578.    specify the start and end address of the code range the entry
  1579.    covers and the address of the corresponding unwind info data.
  1580.  
  1581.    On ARM and SH-4, a compressed PDATA structure is used :
  1582.    _IMAGE_CE_RUNTIME_FUNCTION_ENTRY, whereas MIPS is documented to use
  1583.    _IMAGE_ALPHA_RUNTIME_FUNCTION_ENTRY.
  1584.    See http://msdn2.microsoft.com/en-us/library/ms253988(VS.80).aspx .
  1585.  
  1586.    This is the version for uncompressed data.  */
  1587.  
  1588. static bfd_boolean
  1589. pe_print_pdata (bfd * abfd, void * vfile)
  1590. {
  1591. #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
  1592. # define PDATA_ROW_SIZE (3 * 8)
  1593. #else
  1594. # define PDATA_ROW_SIZE (5 * 4)
  1595. #endif
  1596.   FILE *file = (FILE *) vfile;
  1597.   bfd_byte *data = 0;
  1598.   asection *section = bfd_get_section_by_name (abfd, ".pdata");
  1599.   bfd_size_type datasize = 0;
  1600.   bfd_size_type i;
  1601.   bfd_size_type start, stop;
  1602.   int onaline = PDATA_ROW_SIZE;
  1603.  
  1604.   if (section == NULL
  1605.       || coff_section_data (abfd, section) == NULL
  1606.       || pei_section_data (abfd, section) == NULL)
  1607.     return TRUE;
  1608.  
  1609.   stop = pei_section_data (abfd, section)->virt_size;
  1610.   if ((stop % onaline) != 0)
  1611.     fprintf (file,
  1612.              _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
  1613.              (long) stop, onaline);
  1614.  
  1615.   fprintf (file,
  1616.            _("\nThe Function Table (interpreted .pdata section contents)\n"));
  1617. #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
  1618.   fprintf (file,
  1619.            _(" vma:\t\t\tBegin Address    End Address      Unwind Info\n"));
  1620. #else
  1621.   fprintf (file, _("\
  1622. vma:\t\tBegin    End      EH       EH       PrologEnd  Exception\n\
  1623.     \t\tAddress  Address  Handler  Data     Address    Mask\n"));
  1624. #endif
  1625.  
  1626.   datasize = section->size;
  1627.   if (datasize == 0)
  1628.     return TRUE;
  1629.  
  1630.   if (! bfd_malloc_and_get_section (abfd, section, &data))
  1631.     {
  1632.       if (data != NULL)
  1633.         free (data);
  1634.       return FALSE;
  1635.     }
  1636.  
  1637.   start = 0;
  1638.  
  1639.   for (i = start; i < stop; i += onaline)
  1640.     {
  1641.       bfd_vma begin_addr;
  1642.       bfd_vma end_addr;
  1643.       bfd_vma eh_handler;
  1644.       bfd_vma eh_data;
  1645.       bfd_vma prolog_end_addr;
  1646. #if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
  1647.       int em_data;
  1648. #endif
  1649.  
  1650.       if (i + PDATA_ROW_SIZE > stop)
  1651.         break;
  1652.  
  1653.       begin_addr      = GET_PDATA_ENTRY (abfd, data + i     );
  1654.       end_addr        = GET_PDATA_ENTRY (abfd, data + i +  4);
  1655.       eh_handler      = GET_PDATA_ENTRY (abfd, data + i +  8);
  1656.       eh_data         = GET_PDATA_ENTRY (abfd, data + i + 12);
  1657.       prolog_end_addr = GET_PDATA_ENTRY (abfd, data + i + 16);
  1658.  
  1659.       if (begin_addr == 0 && end_addr == 0 && eh_handler == 0
  1660.           && eh_data == 0 && prolog_end_addr == 0)
  1661.         /* We are probably into the padding of the section now.  */
  1662.         break;
  1663.  
  1664. #if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
  1665.       em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
  1666. #endif
  1667.       eh_handler &= ~(bfd_vma) 0x3;
  1668.       prolog_end_addr &= ~(bfd_vma) 0x3;
  1669.  
  1670.       fputc (' ', file);
  1671.       bfd_fprintf_vma (abfd, file, i + section->vma); fputc ('\t', file);
  1672.       bfd_fprintf_vma (abfd, file, begin_addr); fputc (' ', file);
  1673.       bfd_fprintf_vma (abfd, file, end_addr); fputc (' ', file);
  1674.       bfd_fprintf_vma (abfd, file, eh_handler);
  1675. #if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
  1676.       fputc (' ', file);
  1677.       bfd_fprintf_vma (abfd, file, eh_data); fputc (' ', file);
  1678.       bfd_fprintf_vma (abfd, file, prolog_end_addr);
  1679.       fprintf (file, "   %x", em_data);
  1680. #endif
  1681.  
  1682. #ifdef POWERPC_LE_PE
  1683.       if (eh_handler == 0 && eh_data != 0)
  1684.         {
  1685.           /* Special bits here, although the meaning may be a little
  1686.              mysterious. The only one I know for sure is 0x03
  1687.              Code Significance
  1688.              0x00 None
  1689.              0x01 Register Save Millicode
  1690.              0x02 Register Restore Millicode
  1691.              0x03 Glue Code Sequence.  */
  1692.           switch (eh_data)
  1693.             {
  1694.             case 0x01:
  1695.               fprintf (file, _(" Register save millicode"));
  1696.               break;
  1697.             case 0x02:
  1698.               fprintf (file, _(" Register restore millicode"));
  1699.               break;
  1700.             case 0x03:
  1701.               fprintf (file, _(" Glue code sequence"));
  1702.               break;
  1703.             default:
  1704.               break;
  1705.             }
  1706.         }
  1707. #endif
  1708.       fprintf (file, "\n");
  1709.     }
  1710.  
  1711.   free (data);
  1712.  
  1713.   return TRUE;
  1714. #undef PDATA_ROW_SIZE
  1715. }
  1716.  
  1717. typedef struct sym_cache
  1718. {
  1719.   int        symcount;
  1720.   asymbol ** syms;
  1721. } sym_cache;
  1722.  
  1723. static asymbol **
  1724. slurp_symtab (bfd *abfd, sym_cache *psc)
  1725. {
  1726.   asymbol ** sy = NULL;
  1727.   long storage;
  1728.  
  1729.   if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
  1730.     {
  1731.       psc->symcount = 0;
  1732.       return NULL;
  1733.     }
  1734.  
  1735.   storage = bfd_get_symtab_upper_bound (abfd);
  1736.   if (storage < 0)
  1737.     return NULL;
  1738.   if (storage)
  1739.     sy = (asymbol **) bfd_malloc (storage);
  1740.  
  1741.   psc->symcount = bfd_canonicalize_symtab (abfd, sy);
  1742.   if (psc->symcount < 0)
  1743.     return NULL;
  1744.   return sy;
  1745. }
  1746.  
  1747. static const char *
  1748. my_symbol_for_address (bfd *abfd, bfd_vma func, sym_cache *psc)
  1749. {
  1750.   int i;
  1751.  
  1752.   if (psc->syms == 0)
  1753.     psc->syms = slurp_symtab (abfd, psc);
  1754.  
  1755.   for (i = 0; i < psc->symcount; i++)
  1756.     {
  1757.       if (psc->syms[i]->section->vma + psc->syms[i]->value == func)
  1758.         return psc->syms[i]->name;
  1759.     }
  1760.  
  1761.   return NULL;
  1762. }
  1763.  
  1764. static void
  1765. cleanup_syms (sym_cache *psc)
  1766. {
  1767.   psc->symcount = 0;
  1768.   free (psc->syms);
  1769.   psc->syms = NULL;
  1770. }
  1771.  
  1772. /* This is the version for "compressed" pdata.  */
  1773.  
  1774. bfd_boolean
  1775. _bfd_pe_print_ce_compressed_pdata (bfd * abfd, void * vfile)
  1776. {
  1777. # define PDATA_ROW_SIZE (2 * 4)
  1778.   FILE *file = (FILE *) vfile;
  1779.   bfd_byte *data = NULL;
  1780.   asection *section = bfd_get_section_by_name (abfd, ".pdata");
  1781.   bfd_size_type datasize = 0;
  1782.   bfd_size_type i;
  1783.   bfd_size_type start, stop;
  1784.   int onaline = PDATA_ROW_SIZE;
  1785.   struct sym_cache cache = {0, 0} ;
  1786.  
  1787.   if (section == NULL
  1788.       || coff_section_data (abfd, section) == NULL
  1789.       || pei_section_data (abfd, section) == NULL)
  1790.     return TRUE;
  1791.  
  1792.   stop = pei_section_data (abfd, section)->virt_size;
  1793.   if ((stop % onaline) != 0)
  1794.     fprintf (file,
  1795.              _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
  1796.              (long) stop, onaline);
  1797.  
  1798.   fprintf (file,
  1799.            _("\nThe Function Table (interpreted .pdata section contents)\n"));
  1800.  
  1801.   fprintf (file, _("\
  1802. vma:\t\tBegin    Prolog   Function Flags    Exception EH\n\
  1803.     \t\tAddress  Length   Length   32b exc  Handler   Data\n"));
  1804.  
  1805.   datasize = section->size;
  1806.   if (datasize == 0)
  1807.     return TRUE;
  1808.  
  1809.   if (! bfd_malloc_and_get_section (abfd, section, &data))
  1810.     {
  1811.       if (data != NULL)
  1812.         free (data);
  1813.       return FALSE;
  1814.     }
  1815.  
  1816.   start = 0;
  1817.  
  1818.   for (i = start; i < stop; i += onaline)
  1819.     {
  1820.       bfd_vma begin_addr;
  1821.       bfd_vma other_data;
  1822.       bfd_vma prolog_length, function_length;
  1823.       int flag32bit, exception_flag;
  1824.       asection *tsection;
  1825.  
  1826.       if (i + PDATA_ROW_SIZE > stop)
  1827.         break;
  1828.  
  1829.       begin_addr = GET_PDATA_ENTRY (abfd, data + i     );
  1830.       other_data = GET_PDATA_ENTRY (abfd, data + i +  4);
  1831.  
  1832.       if (begin_addr == 0 && other_data == 0)
  1833.         /* We are probably into the padding of the section now.  */
  1834.         break;
  1835.  
  1836.       prolog_length = (other_data & 0x000000FF);
  1837.       function_length = (other_data & 0x3FFFFF00) >> 8;
  1838.       flag32bit = (int)((other_data & 0x40000000) >> 30);
  1839.       exception_flag = (int)((other_data & 0x80000000) >> 31);
  1840.  
  1841.       fputc (' ', file);
  1842.       bfd_fprintf_vma (abfd, file, i + section->vma); fputc ('\t', file);
  1843.       bfd_fprintf_vma (abfd, file, begin_addr); fputc (' ', file);
  1844.       bfd_fprintf_vma (abfd, file, prolog_length); fputc (' ', file);
  1845.       bfd_fprintf_vma (abfd, file, function_length); fputc (' ', file);
  1846.       fprintf (file, "%2d  %2d   ", flag32bit, exception_flag);
  1847.  
  1848.       /* Get the exception handler's address and the data passed from the
  1849.          .text section. This is really the data that belongs with the .pdata
  1850.          but got "compressed" out for the ARM and SH4 architectures.  */
  1851.       tsection = bfd_get_section_by_name (abfd, ".text");
  1852.       if (tsection && coff_section_data (abfd, tsection)
  1853.           && pei_section_data (abfd, tsection))
  1854.         {
  1855.           bfd_vma eh_off = (begin_addr - 8) - tsection->vma;
  1856.           bfd_byte *tdata;
  1857.  
  1858.           tdata = (bfd_byte *) bfd_malloc (8);
  1859.           if (tdata)
  1860.             {
  1861.               if (bfd_get_section_contents (abfd, tsection, tdata, eh_off, 8))
  1862.                 {
  1863.                   bfd_vma eh, eh_data;
  1864.  
  1865.                   eh = bfd_get_32 (abfd, tdata);
  1866.                   eh_data = bfd_get_32 (abfd, tdata + 4);
  1867.                   fprintf (file, "%08x  ", (unsigned int) eh);
  1868.                   fprintf (file, "%08x", (unsigned int) eh_data);
  1869.                   if (eh != 0)
  1870.                     {
  1871.                       const char *s = my_symbol_for_address (abfd, eh, &cache);
  1872.  
  1873.                       if (s)
  1874.                         fprintf (file, " (%s) ", s);
  1875.                     }
  1876.                 }
  1877.               free (tdata);
  1878.             }
  1879.         }
  1880.  
  1881.       fprintf (file, "\n");
  1882.     }
  1883.  
  1884.   free (data);
  1885.  
  1886.   cleanup_syms (& cache);
  1887.  
  1888.   return TRUE;
  1889. #undef PDATA_ROW_SIZE
  1890. }
  1891.  
  1892. #define IMAGE_REL_BASED_HIGHADJ 4
  1893. static const char * const tbl[] =
  1894. {
  1895.   "ABSOLUTE",
  1896.   "HIGH",
  1897.   "LOW",
  1898.   "HIGHLOW",
  1899.   "HIGHADJ",
  1900.   "MIPS_JMPADDR",
  1901.   "SECTION",
  1902.   "REL32",
  1903.   "RESERVED1",
  1904.   "MIPS_JMPADDR16",
  1905.   "DIR64",
  1906.   "HIGH3ADJ",
  1907.   "UNKNOWN",   /* MUST be last.  */
  1908. };
  1909.  
  1910. static bfd_boolean
  1911. pe_print_reloc (bfd * abfd, void * vfile)
  1912. {
  1913.   FILE *file = (FILE *) vfile;
  1914.   bfd_byte *data = 0;
  1915.   asection *section = bfd_get_section_by_name (abfd, ".reloc");
  1916.   bfd_size_type i;
  1917.   bfd_size_type start, stop;
  1918.  
  1919.   if (section == NULL)
  1920.     return TRUE;
  1921.  
  1922.   if (section->size == 0)
  1923.     return TRUE;
  1924.  
  1925.   fprintf (file,
  1926.            _("\n\nPE File Base Relocations (interpreted .reloc section contents)\n"));
  1927.  
  1928.   if (! bfd_malloc_and_get_section (abfd, section, &data))
  1929.     {
  1930.       if (data != NULL)
  1931.         free (data);
  1932.       return FALSE;
  1933.     }
  1934.  
  1935.   start = 0;
  1936.  
  1937.   stop = section->size;
  1938.  
  1939.   for (i = start; i < stop;)
  1940.     {
  1941.       int j;
  1942.       bfd_vma virtual_address;
  1943.       long number, size;
  1944.  
  1945.       /* The .reloc section is a sequence of blocks, with a header consisting
  1946.          of two 32 bit quantities, followed by a number of 16 bit entries.  */
  1947.       virtual_address = bfd_get_32 (abfd, data+i);
  1948.       size = bfd_get_32 (abfd, data+i+4);
  1949.       number = (size - 8) / 2;
  1950.  
  1951.       if (size == 0)
  1952.         break;
  1953.  
  1954.       fprintf (file,
  1955.                _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
  1956.                (unsigned long) virtual_address, size, (unsigned long) size, number);
  1957.  
  1958.       for (j = 0; j < number; ++j)
  1959.         {
  1960.           unsigned short e = bfd_get_16 (abfd, data + i + 8 + j * 2);
  1961.           unsigned int t = (e & 0xF000) >> 12;
  1962.           int off = e & 0x0FFF;
  1963.  
  1964.           if (t >= sizeof (tbl) / sizeof (tbl[0]))
  1965.             t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
  1966.  
  1967.           fprintf (file,
  1968.                    _("\treloc %4d offset %4x [%4lx] %s"),
  1969.                    j, off, (unsigned long) (off + virtual_address), tbl[t]);
  1970.  
  1971.           /* HIGHADJ takes an argument, - the next record *is* the
  1972.              low 16 bits of addend.  */
  1973.           if (t == IMAGE_REL_BASED_HIGHADJ)
  1974.             {
  1975.               fprintf (file, " (%4x)",
  1976.                        ((unsigned int)
  1977.                         bfd_get_16 (abfd, data + i + 8 + j * 2 + 2)));
  1978.               j++;
  1979.             }
  1980.  
  1981.           fprintf (file, "\n");
  1982.         }
  1983.  
  1984.       i += size;
  1985.     }
  1986.  
  1987.   free (data);
  1988.  
  1989.   return TRUE;
  1990. }
  1991.  
  1992. /* Print out the program headers.  */
  1993.  
  1994. bfd_boolean
  1995. _bfd_pe_print_private_bfd_data_common (bfd * abfd, void * vfile)
  1996. {
  1997.   FILE *file = (FILE *) vfile;
  1998.   int j;
  1999.   pe_data_type *pe = pe_data (abfd);
  2000.   struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr;
  2001.   const char *subsystem_name = NULL;
  2002.   const char *name;
  2003.  
  2004.   /* The MS dumpbin program reportedly ands with 0xff0f before
  2005.      printing the characteristics field.  Not sure why.  No reason to
  2006.      emulate it here.  */
  2007.   fprintf (file, _("\nCharacteristics 0x%x\n"), pe->real_flags);
  2008. #undef PF
  2009. #define PF(x, y) if (pe->real_flags & x) { fprintf (file, "\t%s\n", y); }
  2010.   PF (IMAGE_FILE_RELOCS_STRIPPED, "relocations stripped");
  2011.   PF (IMAGE_FILE_EXECUTABLE_IMAGE, "executable");
  2012.   PF (IMAGE_FILE_LINE_NUMS_STRIPPED, "line numbers stripped");
  2013.   PF (IMAGE_FILE_LOCAL_SYMS_STRIPPED, "symbols stripped");
  2014.   PF (IMAGE_FILE_LARGE_ADDRESS_AWARE, "large address aware");
  2015.   PF (IMAGE_FILE_BYTES_REVERSED_LO, "little endian");
  2016.   PF (IMAGE_FILE_32BIT_MACHINE, "32 bit words");
  2017.   PF (IMAGE_FILE_DEBUG_STRIPPED, "debugging information removed");
  2018.   PF (IMAGE_FILE_SYSTEM, "system file");
  2019.   PF (IMAGE_FILE_DLL, "DLL");
  2020.   PF (IMAGE_FILE_BYTES_REVERSED_HI, "big endian");
  2021. #undef PF
  2022.  
  2023.   /* ctime implies '\n'.  */
  2024.   {
  2025.     time_t t = pe->coff.timestamp;
  2026.     fprintf (file, "\nTime/Date\t\t%s", ctime (&t));
  2027.   }
  2028.  
  2029. #ifndef IMAGE_NT_OPTIONAL_HDR_MAGIC
  2030. # define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b
  2031. #endif
  2032. #ifndef IMAGE_NT_OPTIONAL_HDR64_MAGIC
  2033. # define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
  2034. #endif
  2035. #ifndef IMAGE_NT_OPTIONAL_HDRROM_MAGIC
  2036. # define IMAGE_NT_OPTIONAL_HDRROM_MAGIC 0x107
  2037. #endif
  2038.  
  2039.   switch (i->Magic)
  2040.     {
  2041.     case IMAGE_NT_OPTIONAL_HDR_MAGIC:
  2042.       name = "PE32";
  2043.       break;
  2044.     case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
  2045.       name = "PE32+";
  2046.       break;
  2047.     case IMAGE_NT_OPTIONAL_HDRROM_MAGIC:
  2048.       name = "ROM";
  2049.       break;
  2050.     default:
  2051.       name = NULL;
  2052.       break;
  2053.     }
  2054.   fprintf (file, "Magic\t\t\t%04x", i->Magic);
  2055.   if (name)
  2056.     fprintf (file, "\t(%s)",name);
  2057.   fprintf (file, "\nMajorLinkerVersion\t%d\n", i->MajorLinkerVersion);
  2058.   fprintf (file, "MinorLinkerVersion\t%d\n", i->MinorLinkerVersion);
  2059.   fprintf (file, "SizeOfCode\t\t%08lx\n", (unsigned long) i->SizeOfCode);
  2060.   fprintf (file, "SizeOfInitializedData\t%08lx\n",
  2061.            (unsigned long) i->SizeOfInitializedData);
  2062.   fprintf (file, "SizeOfUninitializedData\t%08lx\n",
  2063.            (unsigned long) i->SizeOfUninitializedData);
  2064.   fprintf (file, "AddressOfEntryPoint\t");
  2065.   bfd_fprintf_vma (abfd, file, i->AddressOfEntryPoint);
  2066.   fprintf (file, "\nBaseOfCode\t\t");
  2067.   bfd_fprintf_vma (abfd, file, i->BaseOfCode);
  2068. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
  2069.   /* PE32+ does not have BaseOfData member!  */
  2070.   fprintf (file, "\nBaseOfData\t\t");
  2071.   bfd_fprintf_vma (abfd, file, i->BaseOfData);
  2072. #endif
  2073.  
  2074.   fprintf (file, "\nImageBase\t\t");
  2075.   bfd_fprintf_vma (abfd, file, i->ImageBase);
  2076.   fprintf (file, "\nSectionAlignment\t");
  2077.   bfd_fprintf_vma (abfd, file, i->SectionAlignment);
  2078.   fprintf (file, "\nFileAlignment\t\t");
  2079.   bfd_fprintf_vma (abfd, file, i->FileAlignment);
  2080.   fprintf (file, "\nMajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
  2081.   fprintf (file, "MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
  2082.   fprintf (file, "MajorImageVersion\t%d\n", i->MajorImageVersion);
  2083.   fprintf (file, "MinorImageVersion\t%d\n", i->MinorImageVersion);
  2084.   fprintf (file, "MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
  2085.   fprintf (file, "MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
  2086.   fprintf (file, "Win32Version\t\t%08lx\n", (unsigned long) i->Reserved1);
  2087.   fprintf (file, "SizeOfImage\t\t%08lx\n", (unsigned long) i->SizeOfImage);
  2088.   fprintf (file, "SizeOfHeaders\t\t%08lx\n", (unsigned long) i->SizeOfHeaders);
  2089.   fprintf (file, "CheckSum\t\t%08lx\n", (unsigned long) i->CheckSum);
  2090.  
  2091.   switch (i->Subsystem)
  2092.     {
  2093.     case IMAGE_SUBSYSTEM_UNKNOWN:
  2094.       subsystem_name = "unspecified";
  2095.       break;
  2096.     case IMAGE_SUBSYSTEM_NATIVE:
  2097.       subsystem_name = "NT native";
  2098.       break;
  2099.     case IMAGE_SUBSYSTEM_WINDOWS_GUI:
  2100.       subsystem_name = "Windows GUI";
  2101.       break;
  2102.     case IMAGE_SUBSYSTEM_WINDOWS_CUI:
  2103.       subsystem_name = "Windows CUI";
  2104.       break;
  2105.     case IMAGE_SUBSYSTEM_POSIX_CUI:
  2106.       subsystem_name = "POSIX CUI";
  2107.       break;
  2108.     case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
  2109.       subsystem_name = "Wince CUI";
  2110.       break;
  2111.     // These are from UEFI Platform Initialization Specification 1.1.
  2112.     case IMAGE_SUBSYSTEM_EFI_APPLICATION:
  2113.       subsystem_name = "EFI application";
  2114.       break;
  2115.     case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
  2116.       subsystem_name = "EFI boot service driver";
  2117.       break;
  2118.     case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
  2119.       subsystem_name = "EFI runtime driver";
  2120.       break;
  2121.     case IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
  2122.       subsystem_name = "SAL runtime driver";
  2123.       break;
  2124.     // This is from revision 8.0 of the MS PE/COFF spec
  2125.     case IMAGE_SUBSYSTEM_XBOX:
  2126.       subsystem_name = "XBOX";
  2127.       break;
  2128.     // Added default case for clarity - subsystem_name is NULL anyway.
  2129.     default:
  2130.       subsystem_name = NULL;
  2131.     }
  2132.  
  2133.   fprintf (file, "Subsystem\t\t%08x", i->Subsystem);
  2134.   if (subsystem_name)
  2135.     fprintf (file, "\t(%s)", subsystem_name);
  2136.   fprintf (file, "\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
  2137.   fprintf (file, "SizeOfStackReserve\t");
  2138.   bfd_fprintf_vma (abfd, file, i->SizeOfStackReserve);
  2139.   fprintf (file, "\nSizeOfStackCommit\t");
  2140.   bfd_fprintf_vma (abfd, file, i->SizeOfStackCommit);
  2141.   fprintf (file, "\nSizeOfHeapReserve\t");
  2142.   bfd_fprintf_vma (abfd, file, i->SizeOfHeapReserve);
  2143.   fprintf (file, "\nSizeOfHeapCommit\t");
  2144.   bfd_fprintf_vma (abfd, file, i->SizeOfHeapCommit);
  2145.   fprintf (file, "\nLoaderFlags\t\t%08lx\n", (unsigned long) i->LoaderFlags);
  2146.   fprintf (file, "NumberOfRvaAndSizes\t%08lx\n",
  2147.            (unsigned long) i->NumberOfRvaAndSizes);
  2148.  
  2149.   fprintf (file, "\nThe Data Directory\n");
  2150.   for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
  2151.     {
  2152.       fprintf (file, "Entry %1x ", j);
  2153.       bfd_fprintf_vma (abfd, file, i->DataDirectory[j].VirtualAddress);
  2154.       fprintf (file, " %08lx ", (unsigned long) i->DataDirectory[j].Size);
  2155.       fprintf (file, "%s\n", dir_names[j]);
  2156.     }
  2157.  
  2158.   pe_print_idata (abfd, vfile);
  2159.   pe_print_edata (abfd, vfile);
  2160.   if (bfd_coff_have_print_pdata (abfd))
  2161.     bfd_coff_print_pdata (abfd, vfile);
  2162.   else
  2163.     pe_print_pdata (abfd, vfile);
  2164.   pe_print_reloc (abfd, vfile);
  2165.  
  2166.   return TRUE;
  2167. }
  2168.  
  2169. /* Copy any private info we understand from the input bfd
  2170.    to the output bfd.  */
  2171.  
  2172. bfd_boolean
  2173. _bfd_pe_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
  2174. {
  2175.   pe_data_type *ipe, *ope;
  2176.  
  2177.   /* One day we may try to grok other private data.  */
  2178.   if (ibfd->xvec->flavour != bfd_target_coff_flavour
  2179.       || obfd->xvec->flavour != bfd_target_coff_flavour)
  2180.     return TRUE;
  2181.  
  2182.   ipe = pe_data (ibfd);
  2183.   ope = pe_data (obfd);
  2184.  
  2185.   /* pe_opthdr is copied in copy_object.  */
  2186.   ope->dll = ipe->dll;
  2187.  
  2188.   /* Don't copy input subsystem if output is different from input.  */
  2189.   if (obfd->xvec != ibfd->xvec)
  2190.     ope->pe_opthdr.Subsystem = IMAGE_SUBSYSTEM_UNKNOWN;
  2191.  
  2192.   /* For strip: if we removed .reloc, we'll make a real mess of things
  2193.      if we don't remove this entry as well.  */
  2194.   if (! pe_data (obfd)->has_reloc_section)
  2195.     {
  2196.       pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].VirtualAddress = 0;
  2197.       pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].Size = 0;
  2198.     }
  2199.  
  2200.   /* For PIE, if there is .reloc, we won't add IMAGE_FILE_RELOCS_STRIPPED.
  2201.      But there is no .reloc, we make sure that IMAGE_FILE_RELOCS_STRIPPED
  2202.      won't be added.  */
  2203.   if (! pe_data (ibfd)->has_reloc_section
  2204.       && ! (pe_data (ibfd)->real_flags & IMAGE_FILE_RELOCS_STRIPPED))
  2205.     pe_data (obfd)->dont_strip_reloc = 1;
  2206.  
  2207.   return TRUE;
  2208. }
  2209.  
  2210. /* Copy private section data.  */
  2211.  
  2212. bfd_boolean
  2213. _bfd_pe_bfd_copy_private_section_data (bfd *ibfd,
  2214.                                        asection *isec,
  2215.                                        bfd *obfd,
  2216.                                        asection *osec)
  2217. {
  2218.   if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
  2219.       || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
  2220.     return TRUE;
  2221.  
  2222.   if (coff_section_data (ibfd, isec) != NULL
  2223.       && pei_section_data (ibfd, isec) != NULL)
  2224.     {
  2225.       if (coff_section_data (obfd, osec) == NULL)
  2226.         {
  2227.           bfd_size_type amt = sizeof (struct coff_section_tdata);
  2228.           osec->used_by_bfd = bfd_zalloc (obfd, amt);
  2229.           if (osec->used_by_bfd == NULL)
  2230.             return FALSE;
  2231.         }
  2232.  
  2233.       if (pei_section_data (obfd, osec) == NULL)
  2234.         {
  2235.           bfd_size_type amt = sizeof (struct pei_section_tdata);
  2236.           coff_section_data (obfd, osec)->tdata = bfd_zalloc (obfd, amt);
  2237.           if (coff_section_data (obfd, osec)->tdata == NULL)
  2238.             return FALSE;
  2239.         }
  2240.  
  2241.       pei_section_data (obfd, osec)->virt_size =
  2242.         pei_section_data (ibfd, isec)->virt_size;
  2243.       pei_section_data (obfd, osec)->pe_flags =
  2244.         pei_section_data (ibfd, isec)->pe_flags;
  2245.     }
  2246.  
  2247.   return TRUE;
  2248. }
  2249.  
  2250. void
  2251. _bfd_pe_get_symbol_info (bfd * abfd, asymbol *symbol, symbol_info *ret)
  2252. {
  2253.   coff_get_symbol_info (abfd, symbol, ret);
  2254. }
  2255.  
  2256. #if !defined(COFF_WITH_pep) && defined(COFF_WITH_pex64)
  2257. static int
  2258. sort_x64_pdata (const void *l, const void *r)
  2259. {
  2260.   const char *lp = (const char *) l;
  2261.   const char *rp = (const char *) r;
  2262.   bfd_vma vl, vr;
  2263.   vl = bfd_getl32 (lp); vr = bfd_getl32 (rp);
  2264.   if (vl != vr)
  2265.     return (vl < vr ? -1 : 1);
  2266.   /* We compare just begin address.  */
  2267.   return 0;
  2268. }
  2269. #endif
  2270.  
  2271. /* Handle the .idata section and other things that need symbol table
  2272.    access.  */
  2273.  
  2274. bfd_boolean
  2275. _bfd_pei_final_link_postscript (bfd * abfd, struct coff_final_link_info *pfinfo)
  2276. {
  2277.   struct coff_link_hash_entry *h1;
  2278.   struct bfd_link_info *info = pfinfo->info;
  2279.   bfd_boolean result = TRUE;
  2280.  
  2281.   /* There are a few fields that need to be filled in now while we
  2282.      have symbol table access.
  2283.  
  2284.      The .idata subsections aren't directly available as sections, but
  2285.      they are in the symbol table, so get them from there.  */
  2286.  
  2287.   /* The import directory.  This is the address of .idata$2, with size
  2288.      of .idata$2 + .idata$3.  */
  2289.   h1 = coff_link_hash_lookup (coff_hash_table (info),
  2290.                               ".idata$2", FALSE, FALSE, TRUE);
  2291.   if (h1 != NULL)
  2292.     {
  2293.       /* PR ld/2729: We cannot rely upon all the output sections having been
  2294.          created properly, so check before referencing them.  Issue a warning
  2295.          message for any sections tht could not be found.  */
  2296.       if ((h1->root.type == bfd_link_hash_defined
  2297.            || h1->root.type == bfd_link_hash_defweak)
  2298.           && h1->root.u.def.section != NULL
  2299.           && h1->root.u.def.section->output_section != NULL)
  2300.         pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress =
  2301.           (h1->root.u.def.value
  2302.            + h1->root.u.def.section->output_section->vma
  2303.            + h1->root.u.def.section->output_offset);
  2304.       else
  2305.         {
  2306.           _bfd_error_handler
  2307.             (_("%B: unable to fill in DataDictionary[1] because .idata$2 is missing"),
  2308.              abfd);
  2309.           result = FALSE;
  2310.         }
  2311.  
  2312.       h1 = coff_link_hash_lookup (coff_hash_table (info),
  2313.                                   ".idata$4", FALSE, FALSE, TRUE);
  2314.       if (h1 != NULL
  2315.           && (h1->root.type == bfd_link_hash_defined
  2316.            || h1->root.type == bfd_link_hash_defweak)
  2317.           && h1->root.u.def.section != NULL
  2318.           && h1->root.u.def.section->output_section != NULL)
  2319.         pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].Size =
  2320.           ((h1->root.u.def.value
  2321.             + h1->root.u.def.section->output_section->vma
  2322.             + h1->root.u.def.section->output_offset)
  2323.            - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress);
  2324.       else
  2325.         {
  2326.           _bfd_error_handler
  2327.             (_("%B: unable to fill in DataDictionary[1] because .idata$4 is missing"),
  2328.              abfd);
  2329.           result = FALSE;
  2330.         }
  2331.  
  2332.       /* The import address table.  This is the size/address of
  2333.          .idata$5.  */
  2334.       h1 = coff_link_hash_lookup (coff_hash_table (info),
  2335.                                   ".idata$5", FALSE, FALSE, TRUE);
  2336.       if (h1 != NULL
  2337.           && (h1->root.type == bfd_link_hash_defined
  2338.            || h1->root.type == bfd_link_hash_defweak)
  2339.           && h1->root.u.def.section != NULL
  2340.           && h1->root.u.def.section->output_section != NULL)
  2341.         pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
  2342.           (h1->root.u.def.value
  2343.            + h1->root.u.def.section->output_section->vma
  2344.            + h1->root.u.def.section->output_offset);
  2345.       else
  2346.         {
  2347.           _bfd_error_handler
  2348.             (_("%B: unable to fill in DataDictionary[12] because .idata$5 is missing"),
  2349.              abfd);
  2350.           result = FALSE;
  2351.         }
  2352.  
  2353.       h1 = coff_link_hash_lookup (coff_hash_table (info),
  2354.                                   ".idata$6", FALSE, FALSE, TRUE);
  2355.       if (h1 != NULL
  2356.           && (h1->root.type == bfd_link_hash_defined
  2357.            || h1->root.type == bfd_link_hash_defweak)
  2358.           && h1->root.u.def.section != NULL
  2359.           && h1->root.u.def.section->output_section != NULL)
  2360.         pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
  2361.           ((h1->root.u.def.value
  2362.             + h1->root.u.def.section->output_section->vma
  2363.             + h1->root.u.def.section->output_offset)
  2364.            - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress);
  2365.       else
  2366.         {
  2367.           _bfd_error_handler
  2368.             (_("%B: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE (12)] because .idata$6 is missing"),
  2369.              abfd);
  2370.           result = FALSE;
  2371.         }
  2372.     }
  2373.   else
  2374.     {
  2375.       h1 = coff_link_hash_lookup (coff_hash_table (info),
  2376.                                   "__IAT_start__", FALSE, FALSE, TRUE);
  2377.       if (h1 != NULL
  2378.           && (h1->root.type == bfd_link_hash_defined
  2379.            || h1->root.type == bfd_link_hash_defweak)
  2380.           && h1->root.u.def.section != NULL
  2381.           && h1->root.u.def.section->output_section != NULL)
  2382.         {
  2383.           bfd_vma iat_va;
  2384.  
  2385.           iat_va =
  2386.             (h1->root.u.def.value
  2387.              + h1->root.u.def.section->output_section->vma
  2388.              + h1->root.u.def.section->output_offset);
  2389.  
  2390.           h1 = coff_link_hash_lookup (coff_hash_table (info),
  2391.                                       "__IAT_end__", FALSE, FALSE, TRUE);
  2392.           if (h1 != NULL
  2393.               && (h1->root.type == bfd_link_hash_defined
  2394.                || h1->root.type == bfd_link_hash_defweak)
  2395.               && h1->root.u.def.section != NULL
  2396.               && h1->root.u.def.section->output_section != NULL)
  2397.             {
  2398.               pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
  2399.                 ((h1->root.u.def.value
  2400.                   + h1->root.u.def.section->output_section->vma
  2401.                   + h1->root.u.def.section->output_offset)
  2402.                  - iat_va);
  2403.               if (pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size != 0)
  2404.                 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
  2405.                   iat_va - pe_data (abfd)->pe_opthdr.ImageBase;
  2406.             }
  2407.           else
  2408.             {
  2409.               _bfd_error_handler
  2410.                 (_("%B: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE(12)]"
  2411.                    " because .idata$6 is missing"), abfd);
  2412.               result = FALSE;
  2413.             }
  2414.         }
  2415.     }
  2416.  
  2417.   h1 = coff_link_hash_lookup (coff_hash_table (info),
  2418.                               (bfd_get_symbol_leading_char(abfd) != 0
  2419.                                ? "__tls_used" : "_tls_used"),
  2420.                               FALSE, FALSE, TRUE);
  2421.   if (h1 != NULL)
  2422.     {
  2423.       if ((h1->root.type == bfd_link_hash_defined
  2424.            || h1->root.type == bfd_link_hash_defweak)
  2425.           && h1->root.u.def.section != NULL
  2426.           && h1->root.u.def.section->output_section != NULL)
  2427.         pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].VirtualAddress =
  2428.           (h1->root.u.def.value
  2429.            + h1->root.u.def.section->output_section->vma
  2430.            + h1->root.u.def.section->output_offset
  2431.            - pe_data (abfd)->pe_opthdr.ImageBase);
  2432.       else
  2433.         {
  2434.           _bfd_error_handler
  2435.             (_("%B: unable to fill in DataDictionary[9] because __tls_used is missing"),
  2436.              abfd);
  2437.           result = FALSE;
  2438.         }
  2439.      /* According to PECOFF sepcifications by Microsoft version 8.2
  2440.         the TLS data directory consists of 4 pointers, followed
  2441.         by two 4-byte integer. This implies that the total size
  2442.         is different for 32-bit and 64-bit executables.  */
  2443. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
  2444.       pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x18;
  2445. #else
  2446.       pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x28;
  2447. #endif
  2448.     }
  2449.  
  2450. /* If there is a .pdata section and we have linked pdata finally, we
  2451.      need to sort the entries ascending.  */
  2452. #if !defined(COFF_WITH_pep) && defined(COFF_WITH_pex64)
  2453.   {
  2454.     asection *sec = bfd_get_section_by_name (abfd, ".pdata");
  2455.  
  2456.     if (sec)
  2457.       {
  2458.         bfd_size_type x = sec->rawsize;
  2459.         bfd_byte *tmp_data = NULL;
  2460.  
  2461.         if (x)
  2462.           tmp_data = bfd_malloc (x);
  2463.  
  2464.         if (tmp_data != NULL)
  2465.           {
  2466.             if (bfd_get_section_contents (abfd, sec, tmp_data, 0, x))
  2467.               {
  2468.                 qsort (tmp_data,
  2469.                        (size_t) (x / 12),
  2470.                        12, sort_x64_pdata);
  2471.                 bfd_set_section_contents (pfinfo->output_bfd, sec,
  2472.                                           tmp_data, 0, x);
  2473.               }
  2474.             free (tmp_data);
  2475.           }
  2476.       }
  2477.   }
  2478. #endif
  2479.  
  2480.   /* If we couldn't find idata$2, we either have an excessively
  2481.      trivial program or are in DEEP trouble; we have to assume trivial
  2482.      program....  */
  2483.   return result;
  2484. }
  2485.