Home | History | Annotate | Line # | Download | only in bfd
      1       1.1     skrll /* Support for the generic parts of PE/PEI, for BFD.
      2  1.1.1.12  christos    Copyright (C) 1995-2026 Free Software Foundation, Inc.
      3       1.1     skrll    Written by Cygnus Solutions.
      4       1.1     skrll 
      5       1.1     skrll    This file is part of BFD, the Binary File Descriptor library.
      6       1.1     skrll 
      7       1.1     skrll    This program is free software; you can redistribute it and/or modify
      8       1.1     skrll    it under the terms of the GNU General Public License as published by
      9       1.1     skrll    the Free Software Foundation; either version 3 of the License, or
     10       1.1     skrll    (at your option) any later version.
     11       1.1     skrll 
     12       1.1     skrll    This program is distributed in the hope that it will be useful,
     13       1.1     skrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14       1.1     skrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15       1.1     skrll    GNU General Public License for more details.
     16       1.1     skrll 
     17       1.1     skrll    You should have received a copy of the GNU General Public License
     18       1.1     skrll    along with this program; if not, write to the Free Software
     19       1.1     skrll    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20       1.1     skrll    MA 02110-1301, USA.  */
     21       1.1     skrll 
     22       1.1     skrll 
     23       1.1     skrll /* Most of this hacked by  Steve Chamberlain,
     24       1.1     skrll 			sac (at) cygnus.com
     25       1.1     skrll 
     26       1.1     skrll    PE/PEI rearrangement (and code added): Donn Terry
     27   1.1.1.6  christos 				       Softway Systems, Inc.  */
     28       1.1     skrll 
     29       1.1     skrll /* Hey look, some documentation [and in a place you expect to find it]!
     30       1.1     skrll 
     31       1.1     skrll    The main reference for the pei format is "Microsoft Portable Executable
     32       1.1     skrll    and Common Object File Format Specification 4.1".  Get it if you need to
     33       1.1     skrll    do some serious hacking on this code.
     34       1.1     skrll 
     35       1.1     skrll    Another reference:
     36       1.1     skrll    "Peering Inside the PE: A Tour of the Win32 Portable Executable
     37       1.1     skrll    File Format", MSJ 1994, Volume 9.
     38       1.1     skrll 
     39       1.1     skrll    The *sole* difference between the pe format and the pei format is that the
     40       1.1     skrll    latter has an MSDOS 2.0 .exe header on the front that prints the message
     41       1.1     skrll    "This app must be run under Windows." (or some such).
     42       1.1     skrll    (FIXME: Whether that statement is *really* true or not is unknown.
     43       1.1     skrll    Are there more subtle differences between pe and pei formats?
     44       1.1     skrll    For now assume there aren't.  If you find one, then for God sakes
     45       1.1     skrll    document it here!)
     46       1.1     skrll 
     47       1.1     skrll    The Microsoft docs use the word "image" instead of "executable" because
     48       1.1     skrll    the former can also refer to a DLL (shared library).  Confusion can arise
     49       1.1     skrll    because the `i' in `pei' also refers to "image".  The `pe' format can
     50       1.1     skrll    also create images (i.e. executables), it's just that to run on a win32
     51       1.1     skrll    system you need to use the pei format.
     52       1.1     skrll 
     53       1.1     skrll    FIXME: Please add more docs here so the next poor fool that has to hack
     54       1.1     skrll    on this code has a chance of getting something accomplished without
     55       1.1     skrll    wasting too much time.  */
     56       1.1     skrll 
     57       1.1     skrll #include "libpei.h"
     58       1.1     skrll 
     59   1.1.1.9  christos static bool (*pe_saved_coff_bfd_print_private_bfd_data) (bfd *, void *) =
     60       1.1     skrll #ifndef coff_bfd_print_private_bfd_data
     61       1.1     skrll      NULL;
     62       1.1     skrll #else
     63       1.1     skrll      coff_bfd_print_private_bfd_data;
     64       1.1     skrll #undef coff_bfd_print_private_bfd_data
     65       1.1     skrll #endif
     66       1.1     skrll 
     67   1.1.1.9  christos static bool pe_print_private_bfd_data (bfd *, void *);
     68       1.1     skrll #define coff_bfd_print_private_bfd_data pe_print_private_bfd_data
     69       1.1     skrll 
     70   1.1.1.9  christos static bool (*pe_saved_coff_bfd_copy_private_bfd_data) (bfd *, bfd *) =
     71       1.1     skrll #ifndef coff_bfd_copy_private_bfd_data
     72       1.1     skrll      NULL;
     73       1.1     skrll #else
     74       1.1     skrll      coff_bfd_copy_private_bfd_data;
     75       1.1     skrll #undef coff_bfd_copy_private_bfd_data
     76       1.1     skrll #endif
     77       1.1     skrll 
     78   1.1.1.9  christos static bool pe_bfd_copy_private_bfd_data (bfd *, bfd *);
     79       1.1     skrll #define coff_bfd_copy_private_bfd_data pe_bfd_copy_private_bfd_data
     80       1.1     skrll 
     81   1.1.1.6  christos #define coff_mkobject	   pe_mkobject
     82       1.1     skrll #define coff_mkobject_hook pe_mkobject_hook
     83       1.1     skrll 
     84       1.1     skrll #ifdef COFF_IMAGE_WITH_PE
     85  1.1.1.12  christos 
     86  1.1.1.12  christos /* For the case of linking ELF objects into a PE binary.  */
     87  1.1.1.12  christos #undef TARGET_MERGE_SECTIONS
     88  1.1.1.12  christos #define TARGET_MERGE_SECTIONS true
     89  1.1.1.12  christos 
     90       1.1     skrll /* This structure contains static variables used by the ILF code.  */
     91       1.1     skrll typedef asection * asection_ptr;
     92       1.1     skrll 
     93       1.1     skrll typedef struct
     94       1.1     skrll {
     95       1.1     skrll   bfd *			abfd;
     96       1.1     skrll   bfd_byte *		data;
     97       1.1     skrll   struct bfd_in_memory * bim;
     98   1.1.1.6  christos   unsigned short	magic;
     99       1.1     skrll 
    100       1.1     skrll   arelent *		reltab;
    101   1.1.1.6  christos   unsigned int		relcount;
    102       1.1     skrll 
    103   1.1.1.6  christos   coff_symbol_type *	sym_cache;
    104   1.1.1.6  christos   coff_symbol_type *	sym_ptr;
    105   1.1.1.6  christos   unsigned int		sym_index;
    106       1.1     skrll 
    107   1.1.1.6  christos   unsigned int *	sym_table;
    108   1.1.1.6  christos   unsigned int *	table_ptr;
    109       1.1     skrll 
    110       1.1     skrll   combined_entry_type * native_syms;
    111       1.1     skrll   combined_entry_type * native_ptr;
    112       1.1     skrll 
    113       1.1     skrll   coff_symbol_type **	sym_ptr_table;
    114       1.1     skrll   coff_symbol_type **	sym_ptr_ptr;
    115       1.1     skrll 
    116       1.1     skrll   unsigned int		sec_index;
    117       1.1     skrll 
    118   1.1.1.6  christos   char *		string_table;
    119   1.1.1.6  christos   char *		string_ptr;
    120       1.1     skrll   char *		end_string_ptr;
    121       1.1     skrll 
    122   1.1.1.6  christos   SYMENT *		esym_table;
    123   1.1.1.6  christos   SYMENT *		esym_ptr;
    124       1.1     skrll 
    125       1.1     skrll   struct internal_reloc * int_reltab;
    126       1.1     skrll }
    127       1.1     skrll pe_ILF_vars;
    128       1.1     skrll #endif /* COFF_IMAGE_WITH_PE */
    129   1.1.1.4  christos 
    130   1.1.1.9  christos bfd_cleanup coff_real_object_p
    131   1.1.1.4  christos   (bfd *, unsigned, struct internal_filehdr *, struct internal_aouthdr *);
    132       1.1     skrll 
    133       1.1     skrll #ifndef NO_COFF_RELOCS
    135  1.1.1.11  christos static void
    136       1.1     skrll coff_swap_reloc_in (bfd *abfd, void *src, void *dst)
    137       1.1     skrll {
    138       1.1     skrll   RELOC *reloc_src = (RELOC *) src;
    139       1.1     skrll   struct internal_reloc *reloc_dst = (struct internal_reloc *) dst;
    140       1.1     skrll 
    141       1.1     skrll   reloc_dst->r_vaddr  = H_GET_32 (abfd, reloc_src->r_vaddr);
    142       1.1     skrll   reloc_dst->r_symndx = H_GET_S32 (abfd, reloc_src->r_symndx);
    143       1.1     skrll   reloc_dst->r_type   = H_GET_16 (abfd, reloc_src->r_type);
    144       1.1     skrll #ifdef SWAP_IN_RELOC_OFFSET
    145       1.1     skrll   reloc_dst->r_offset = SWAP_IN_RELOC_OFFSET (abfd, reloc_src->r_offset);
    146       1.1     skrll #endif
    147       1.1     skrll }
    148       1.1     skrll 
    149  1.1.1.11  christos static unsigned int
    150       1.1     skrll coff_swap_reloc_out (bfd *abfd, void *src, void *dst)
    151       1.1     skrll {
    152       1.1     skrll   struct internal_reloc *reloc_src = (struct internal_reloc *) src;
    153       1.1     skrll   struct external_reloc *reloc_dst = (struct external_reloc *) dst;
    154       1.1     skrll 
    155       1.1     skrll   H_PUT_32 (abfd, reloc_src->r_vaddr, reloc_dst->r_vaddr);
    156       1.1     skrll   H_PUT_32 (abfd, reloc_src->r_symndx, reloc_dst->r_symndx);
    157       1.1     skrll   H_PUT_16 (abfd, reloc_src->r_type, reloc_dst->r_type);
    158   1.1.1.4  christos 
    159       1.1     skrll #ifdef SWAP_OUT_RELOC_OFFSET
    160       1.1     skrll   SWAP_OUT_RELOC_OFFSET (abfd, reloc_src->r_offset, reloc_dst->r_offset);
    161       1.1     skrll #endif
    162       1.1     skrll #ifdef SWAP_OUT_RELOC_EXTRA
    163       1.1     skrll   SWAP_OUT_RELOC_EXTRA (abfd, reloc_src, reloc_dst);
    164       1.1     skrll #endif
    165       1.1     skrll   return RELSZ;
    166       1.1     skrll }
    167       1.1     skrll #endif /* not NO_COFF_RELOCS */
    168   1.1.1.4  christos 
    169   1.1.1.4  christos #ifdef COFF_IMAGE_WITH_PE
    170   1.1.1.4  christos #undef FILHDR
    171   1.1.1.4  christos #define FILHDR struct external_PEI_IMAGE_hdr
    172   1.1.1.4  christos #endif
    173       1.1     skrll 
    174  1.1.1.11  christos static void
    175       1.1     skrll coff_swap_filehdr_in (bfd *abfd, void *src, void *dst)
    176       1.1     skrll {
    177       1.1     skrll   FILHDR *filehdr_src = (FILHDR *) src;
    178       1.1     skrll   struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
    179       1.1     skrll 
    180       1.1     skrll   filehdr_dst->f_magic  = H_GET_16 (abfd, filehdr_src->f_magic);
    181       1.1     skrll   filehdr_dst->f_nscns  = H_GET_16 (abfd, filehdr_src->f_nscns);
    182       1.1     skrll   filehdr_dst->f_timdat = H_GET_32 (abfd, filehdr_src->f_timdat);
    183       1.1     skrll   filehdr_dst->f_nsyms  = H_GET_32 (abfd, filehdr_src->f_nsyms);
    184       1.1     skrll   filehdr_dst->f_flags  = H_GET_16 (abfd, filehdr_src->f_flags);
    185       1.1     skrll   filehdr_dst->f_symptr = H_GET_32 (abfd, filehdr_src->f_symptr);
    186       1.1     skrll 
    187       1.1     skrll   /* Other people's tools sometimes generate headers with an nsyms but
    188       1.1     skrll      a zero symptr.  */
    189       1.1     skrll   if (filehdr_dst->f_nsyms != 0 && filehdr_dst->f_symptr == 0)
    190       1.1     skrll     {
    191       1.1     skrll       filehdr_dst->f_nsyms = 0;
    192       1.1     skrll       filehdr_dst->f_flags |= F_LSYMS;
    193       1.1     skrll     }
    194       1.1     skrll 
    195       1.1     skrll   filehdr_dst->f_opthdr = H_GET_16 (abfd, filehdr_src-> f_opthdr);
    196       1.1     skrll }
    197       1.1     skrll 
    198       1.1     skrll #ifdef COFF_IMAGE_WITH_PE
    199  1.1.1.10  christos # define coff_swap_filehdr_out _bfd_XXi_only_swap_filehdr_out
    200  1.1.1.10  christos #elif defined COFF_WITH_peAArch64
    201       1.1     skrll # define coff_swap_filehdr_out _bfd_XX_only_swap_filehdr_out
    202       1.1     skrll #elif defined COFF_WITH_pex64
    203       1.1     skrll # define coff_swap_filehdr_out _bfd_pex64_only_swap_filehdr_out
    204       1.1     skrll #elif defined COFF_WITH_pep
    205       1.1     skrll # define coff_swap_filehdr_out _bfd_pep_only_swap_filehdr_out
    206       1.1     skrll #else
    207       1.1     skrll # define coff_swap_filehdr_out _bfd_pe_only_swap_filehdr_out
    208       1.1     skrll #endif
    209       1.1     skrll 
    210  1.1.1.11  christos static void
    211       1.1     skrll coff_swap_scnhdr_in (bfd *abfd, void *ext, void *in)
    212       1.1     skrll {
    213       1.1     skrll   SCNHDR *scnhdr_ext = (SCNHDR *) ext;
    214       1.1     skrll   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
    215       1.1     skrll 
    216       1.1     skrll   memcpy (scnhdr_int->s_name, scnhdr_ext->s_name, sizeof (scnhdr_int->s_name));
    217       1.1     skrll 
    218       1.1     skrll   scnhdr_int->s_vaddr   = GET_SCNHDR_VADDR (abfd, scnhdr_ext->s_vaddr);
    219       1.1     skrll   scnhdr_int->s_paddr   = GET_SCNHDR_PADDR (abfd, scnhdr_ext->s_paddr);
    220       1.1     skrll   scnhdr_int->s_size    = GET_SCNHDR_SIZE (abfd, scnhdr_ext->s_size);
    221       1.1     skrll   scnhdr_int->s_scnptr  = GET_SCNHDR_SCNPTR (abfd, scnhdr_ext->s_scnptr);
    222       1.1     skrll   scnhdr_int->s_relptr  = GET_SCNHDR_RELPTR (abfd, scnhdr_ext->s_relptr);
    223       1.1     skrll   scnhdr_int->s_lnnoptr = GET_SCNHDR_LNNOPTR (abfd, scnhdr_ext->s_lnnoptr);
    224       1.1     skrll   scnhdr_int->s_flags   = H_GET_32 (abfd, scnhdr_ext->s_flags);
    225       1.1     skrll 
    226       1.1     skrll   /* MS handles overflow of line numbers by carrying into the reloc
    227       1.1     skrll      field (it appears).  Since it's supposed to be zero for PE
    228       1.1     skrll      *IMAGE* format, that's safe.  This is still a bit iffy.  */
    229       1.1     skrll #ifdef COFF_IMAGE_WITH_PE
    230       1.1     skrll   scnhdr_int->s_nlnno = (H_GET_16 (abfd, scnhdr_ext->s_nlnno)
    231       1.1     skrll 			 + (H_GET_16 (abfd, scnhdr_ext->s_nreloc) << 16));
    232       1.1     skrll   scnhdr_int->s_nreloc = 0;
    233       1.1     skrll #else
    234       1.1     skrll   scnhdr_int->s_nreloc = H_GET_16 (abfd, scnhdr_ext->s_nreloc);
    235       1.1     skrll   scnhdr_int->s_nlnno = H_GET_16 (abfd, scnhdr_ext->s_nlnno);
    236       1.1     skrll #endif
    237       1.1     skrll 
    238       1.1     skrll   if (scnhdr_int->s_vaddr != 0)
    239       1.1     skrll     {
    240       1.1     skrll       scnhdr_int->s_vaddr += pe_data (abfd)->pe_opthdr.ImageBase;
    241  1.1.1.11  christos       /* Do not cut upper 32-bits for 64-bit vma.  */
    242  1.1.1.11  christos #if (!defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64) \
    243       1.1     skrll      && !defined(COFF_WITH_peLoongArch64) && !defined(COFF_WITH_peRiscV64))
    244       1.1     skrll       scnhdr_int->s_vaddr &= 0xffffffff;
    245       1.1     skrll #endif
    246       1.1     skrll     }
    247       1.1     skrll 
    248       1.1     skrll #ifndef COFF_NO_HACK_SCNHDR_SIZE
    249       1.1     skrll   /* If this section holds uninitialized data and is from an object file
    250       1.1     skrll      or from an executable image that has not initialized the field,
    251       1.1     skrll      or if the image is an executable file and the physical size is padded,
    252       1.1     skrll      use the virtual size (stored in s_paddr) instead.  */
    253       1.1     skrll   if (scnhdr_int->s_paddr > 0
    254   1.1.1.2  christos       && (((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0
    255   1.1.1.6  christos 	   && (! bfd_pei_p (abfd) || scnhdr_int->s_size == 0))
    256       1.1     skrll 	  || (bfd_pei_p (abfd) && (scnhdr_int->s_size > scnhdr_int->s_paddr))))
    257       1.1     skrll   /* This code used to set scnhdr_int->s_paddr to 0.  However,
    258       1.1     skrll      coff_set_alignment_hook stores s_paddr in virt_size, which
    259       1.1     skrll      only works if it correctly holds the virtual size of the
    260       1.1     skrll      section.  */
    261       1.1     skrll     scnhdr_int->s_size = scnhdr_int->s_paddr;
    262       1.1     skrll #endif
    263       1.1     skrll }
    264   1.1.1.9  christos 
    265  1.1.1.11  christos static bool
    266       1.1     skrll pe_mkobject (bfd *abfd)
    267  1.1.1.10  christos {
    268  1.1.1.10  christos   /* Some x86 code followed by an ascii string.  */
    269  1.1.1.10  christos   static const char default_dos_message[64] = {
    270  1.1.1.10  christos     0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd,
    271  1.1.1.10  christos     0x21, 0xb8, 0x01, 0x4c, 0xcd, 0x21, 0x54, 0x68,
    272  1.1.1.10  christos     0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72,
    273  1.1.1.10  christos     0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f,
    274  1.1.1.10  christos     0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6e,
    275  1.1.1.10  christos     0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20,
    276  1.1.1.10  christos     0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x0d, 0x0d, 0x0a,
    277  1.1.1.10  christos     0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    278  1.1.1.10  christos 
    279  1.1.1.10  christos   pe_data_type *pe = bfd_zalloc (abfd, sizeof (*pe));
    280  1.1.1.10  christos   abfd->tdata.pe_obj_data = pe;
    281   1.1.1.9  christos   if (pe == NULL)
    282       1.1     skrll     return false;
    283       1.1     skrll 
    284       1.1     skrll   pe->coff.pe = 1;
    285       1.1     skrll 
    286       1.1     skrll   /* in_reloc_p is architecture dependent.  */
    287       1.1     skrll   pe->in_reloc_p = in_reloc_p;
    288  1.1.1.10  christos 
    289  1.1.1.10  christos   memcpy (pe->dos_message, default_dos_message, sizeof (pe->dos_message));
    290  1.1.1.10  christos 
    291  1.1.1.10  christos   bfd_coff_long_section_names (abfd)
    292   1.1.1.8  christos     = coff_backend_info (abfd)->_bfd_coff_long_section_names;
    293   1.1.1.9  christos 
    294       1.1     skrll   return true;
    295       1.1     skrll }
    296       1.1     skrll 
    297       1.1     skrll /* Create the COFF backend specific information.  */
    298       1.1     skrll 
    299  1.1.1.11  christos static void *
    300  1.1.1.11  christos pe_mkobject_hook (bfd *abfd,
    301  1.1.1.11  christos 		  void *filehdr,
    302       1.1     skrll 		  void *aouthdr ATTRIBUTE_UNUSED)
    303       1.1     skrll {
    304       1.1     skrll   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
    305       1.1     skrll   pe_data_type *pe;
    306       1.1     skrll 
    307       1.1     skrll   if (! pe_mkobject (abfd))
    308       1.1     skrll     return NULL;
    309       1.1     skrll 
    310       1.1     skrll   pe = pe_data (abfd);
    311       1.1     skrll   pe->coff.sym_filepos = internal_f->f_symptr;
    312       1.1     skrll   /* These members communicate important constants about the symbol
    313       1.1     skrll      table to GDB's symbol-reading code.  These `constants'
    314       1.1     skrll      unfortunately vary among coff implementations...  */
    315       1.1     skrll   pe->coff.local_n_btmask = N_BTMASK;
    316       1.1     skrll   pe->coff.local_n_btshft = N_BTSHFT;
    317       1.1     skrll   pe->coff.local_n_tmask = N_TMASK;
    318       1.1     skrll   pe->coff.local_n_tshift = N_TSHIFT;
    319       1.1     skrll   pe->coff.local_symesz = SYMESZ;
    320       1.1     skrll   pe->coff.local_auxesz = AUXESZ;
    321       1.1     skrll   pe->coff.local_linesz = LINESZ;
    322       1.1     skrll 
    323       1.1     skrll   pe->coff.timestamp = internal_f->f_timdat;
    324       1.1     skrll 
    325       1.1     skrll   obj_raw_syment_count (abfd) =
    326       1.1     skrll     obj_conv_table_size (abfd) =
    327       1.1     skrll       internal_f->f_nsyms;
    328       1.1     skrll 
    329       1.1     skrll   pe->real_flags = internal_f->f_flags;
    330       1.1     skrll 
    331       1.1     skrll   if ((internal_f->f_flags & F_DLL) != 0)
    332       1.1     skrll     pe->dll = 1;
    333       1.1     skrll 
    334       1.1     skrll   if ((internal_f->f_flags & IMAGE_FILE_DEBUG_STRIPPED) == 0)
    335       1.1     skrll     abfd->flags |= HAS_DEBUG;
    336       1.1     skrll 
    337       1.1     skrll #ifdef COFF_IMAGE_WITH_PE
    338       1.1     skrll   if (aouthdr)
    339       1.1     skrll     pe->pe_opthdr = ((struct internal_aouthdr *) aouthdr)->pe;
    340       1.1     skrll #endif
    341       1.1     skrll 
    342       1.1     skrll #ifdef ARM
    343       1.1     skrll   if (! _bfd_coff_arm_set_private_flags (abfd, internal_f->f_flags))
    344       1.1     skrll     coff_data (abfd) ->flags = 0;
    345       1.1     skrll #endif
    346   1.1.1.8  christos 
    347   1.1.1.8  christos   memcpy (pe->dos_message, internal_f->pe.dos_message,
    348   1.1.1.8  christos 	  sizeof (pe->dos_message));
    349       1.1     skrll 
    350       1.1     skrll   return (void *) pe;
    351       1.1     skrll }
    352   1.1.1.9  christos 
    353  1.1.1.11  christos static bool
    354       1.1     skrll pe_print_private_bfd_data (bfd *abfd, void *vfile)
    355       1.1     skrll {
    356       1.1     skrll   FILE *file = (FILE *) vfile;
    357       1.1     skrll 
    358   1.1.1.9  christos   if (!_bfd_XX_print_private_bfd_data_common (abfd, vfile))
    359       1.1     skrll     return false;
    360       1.1     skrll 
    361   1.1.1.9  christos   if (pe_saved_coff_bfd_print_private_bfd_data == NULL)
    362       1.1     skrll     return true;
    363       1.1     skrll 
    364       1.1     skrll   fputc ('\n', file);
    365       1.1     skrll 
    366       1.1     skrll   return pe_saved_coff_bfd_print_private_bfd_data (abfd, vfile);
    367       1.1     skrll }
    368       1.1     skrll 
    369       1.1     skrll /* Copy any private info we understand from the input bfd
    370       1.1     skrll    to the output bfd.  */
    371   1.1.1.9  christos 
    372       1.1     skrll static bool
    373       1.1     skrll pe_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
    374       1.1     skrll {
    375       1.1     skrll   /* PR binutils/716: Copy the large address aware flag.
    376       1.1     skrll      XXX: Should we be copying other flags or other fields in the pe_data()
    377       1.1     skrll      structure ?  */
    378       1.1     skrll   if (pe_data (obfd) != NULL
    379       1.1     skrll       && pe_data (ibfd) != NULL
    380       1.1     skrll       && pe_data (ibfd)->real_flags & IMAGE_FILE_LARGE_ADDRESS_AWARE)
    381   1.1.1.4  christos     pe_data (obfd)->real_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
    382       1.1     skrll 
    383   1.1.1.9  christos   if (!_bfd_XX_bfd_copy_private_bfd_data_common (ibfd, obfd))
    384       1.1     skrll     return false;
    385       1.1     skrll 
    386       1.1     skrll   if (pe_saved_coff_bfd_copy_private_bfd_data)
    387       1.1     skrll     return pe_saved_coff_bfd_copy_private_bfd_data (ibfd, obfd);
    388   1.1.1.9  christos 
    389       1.1     skrll   return true;
    390       1.1     skrll }
    391       1.1     skrll 
    392       1.1     skrll #define coff_bfd_copy_private_section_data \
    393       1.1     skrll   _bfd_XX_bfd_copy_private_section_data
    394       1.1     skrll 
    395       1.1     skrll #define coff_get_symbol_info _bfd_XX_get_symbol_info
    396       1.1     skrll 
    397       1.1     skrll #ifdef COFF_IMAGE_WITH_PE
    398  1.1.1.10  christos 
    399       1.1     skrll /* Code to handle Microsoft's Import Library Format.
    401       1.1     skrll    Also known as LINK6 format.
    402  1.1.1.10  christos    Documentation about this format can be found at:
    403       1.1     skrll 
    404       1.1     skrll    https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#import-library-format  */
    405       1.1     skrll 
    406       1.1     skrll /* The following constants specify the sizes of the various data
    407       1.1     skrll    structures that we have to create in order to build a bfd describing
    408       1.1     skrll    an ILF object file.  The final "+ 1" in the definitions of SIZEOF_IDATA6
    409       1.1     skrll    and SIZEOF_IDATA7 below is to allow for the possibility that we might
    410       1.1     skrll    need a padding byte in order to ensure 16 bit alignment for the section's
    411       1.1     skrll    contents.
    412       1.1     skrll 
    413       1.1     skrll    The value for SIZEOF_ILF_STRINGS is computed as follows:
    414       1.1     skrll 
    415       1.1     skrll       There will be NUM_ILF_SECTIONS section symbols.  Allow 9 characters
    416       1.1     skrll       per symbol for their names (longest section name is .idata$x).
    417       1.1     skrll 
    418       1.1     skrll       There will be two symbols for the imported value, one the symbol name
    419       1.1     skrll       and one with _imp__ prefixed.  Allowing for the terminating nul's this
    420       1.1     skrll       is strlen (symbol_name) * 2 + 8 + 21 + strlen (source_dll).
    421       1.1     skrll 
    422       1.1     skrll       The strings in the string table must start STRING__SIZE_SIZE bytes into
    423       1.1     skrll       the table in order to for the string lookup code in coffgen/coffcode to
    424   1.1.1.6  christos       work.  */
    425   1.1.1.6  christos #define NUM_ILF_RELOCS		8
    426       1.1     skrll #define NUM_ILF_SECTIONS	6
    427  1.1.1.11  christos #define NUM_ILF_SYMS		(2 + NUM_ILF_SECTIONS)
    428  1.1.1.11  christos 
    429  1.1.1.11  christos #define SIZEOF_ILF_SYMS		 (NUM_ILF_SYMS * sizeof (*vars.sym_cache))
    430  1.1.1.11  christos #define SIZEOF_ILF_SYM_TABLE	 (NUM_ILF_SYMS * sizeof (*vars.sym_table))
    431  1.1.1.11  christos #define SIZEOF_ILF_NATIVE_SYMS	 (NUM_ILF_SYMS * sizeof (*vars.native_syms))
    432  1.1.1.11  christos #define SIZEOF_ILF_SYM_PTR_TABLE (NUM_ILF_SYMS * sizeof (*vars.sym_ptr_table))
    433  1.1.1.11  christos #define SIZEOF_ILF_EXT_SYMS	 (NUM_ILF_SYMS * sizeof (*vars.esym_table))
    434       1.1     skrll #define SIZEOF_ILF_RELOCS	 (NUM_ILF_RELOCS * sizeof (*vars.reltab))
    435  1.1.1.11  christos #define SIZEOF_ILF_INT_RELOCS	 (NUM_ILF_RELOCS * sizeof (*vars.int_reltab))
    436  1.1.1.11  christos #define SIZEOF_ILF_STRINGS	 (strlen (symbol_name) * 2 + 8 \
    437  1.1.1.11  christos 				  + 21 + strlen (source_dll)   \
    438       1.1     skrll 				  + NUM_ILF_SECTIONS * 9       \
    439       1.1     skrll 				  + STRING_SIZE_SIZE)
    440       1.1     skrll #define SIZEOF_IDATA2		(5 * 4)
    441  1.1.1.10  christos 
    442       1.1     skrll /* For PEx64 idata4 & 5 have thumb size of 8 bytes.  */
    443       1.1     skrll #if defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64)
    444       1.1     skrll #define SIZEOF_IDATA4		(2 * 4)
    445       1.1     skrll #define SIZEOF_IDATA5		(2 * 4)
    446       1.1     skrll #else
    447       1.1     skrll #define SIZEOF_IDATA4		(1 * 4)
    448       1.1     skrll #define SIZEOF_IDATA5		(1 * 4)
    449  1.1.1.11  christos #endif
    450       1.1     skrll 
    451  1.1.1.11  christos #define SIZEOF_IDATA6		(2 + strlen (import_name) + 1 + 1)
    452  1.1.1.11  christos #define SIZEOF_IDATA7		(strlen (source_dll) + 1 + 1)
    453       1.1     skrll #define SIZEOF_ILF_SECTIONS	(NUM_ILF_SECTIONS \
    454       1.1     skrll 				 * sizeof (struct coff_section_tdata))
    455       1.1     skrll 
    456       1.1     skrll #define ILF_DATA_SIZE				\
    457       1.1     skrll     + SIZEOF_ILF_SYMS				\
    458       1.1     skrll     + SIZEOF_ILF_SYM_TABLE			\
    459       1.1     skrll     + SIZEOF_ILF_NATIVE_SYMS			\
    460       1.1     skrll     + SIZEOF_ILF_SYM_PTR_TABLE			\
    461       1.1     skrll     + SIZEOF_ILF_EXT_SYMS			\
    462       1.1     skrll     + SIZEOF_ILF_RELOCS				\
    463       1.1     skrll     + SIZEOF_ILF_INT_RELOCS			\
    464       1.1     skrll     + SIZEOF_ILF_STRINGS			\
    465       1.1     skrll     + SIZEOF_IDATA2				\
    466       1.1     skrll     + SIZEOF_IDATA4				\
    467       1.1     skrll     + SIZEOF_IDATA5				\
    468       1.1     skrll     + SIZEOF_IDATA6				\
    469       1.1     skrll     + SIZEOF_IDATA7				\
    470       1.1     skrll     + SIZEOF_ILF_SECTIONS			\
    471       1.1     skrll     + MAX_TEXT_SECTION_SIZE
    472       1.1     skrll 
    473       1.1     skrll /* Create an empty relocation against the given symbol.  */
    474   1.1.1.6  christos 
    475   1.1.1.6  christos static void
    476   1.1.1.6  christos pe_ILF_make_a_symbol_reloc (pe_ILF_vars *		vars,
    477   1.1.1.6  christos 			    bfd_vma			address,
    478   1.1.1.6  christos 			    bfd_reloc_code_real_type	reloc,
    479       1.1     skrll 			    struct bfd_symbol **	sym,
    480  1.1.1.11  christos 			    unsigned int		sym_index)
    481  1.1.1.11  christos {
    482       1.1     skrll   arelent *entry;
    483       1.1     skrll   struct internal_reloc *internal;
    484       1.1     skrll 
    485       1.1     skrll   entry = vars->reltab + vars->relcount;
    486       1.1     skrll   internal = vars->int_reltab + vars->relcount;
    487       1.1     skrll 
    488       1.1     skrll   entry->address     = address;
    489       1.1     skrll   entry->addend      = 0;
    490       1.1     skrll   entry->howto       = bfd_reloc_type_lookup (vars->abfd, reloc);
    491       1.1     skrll   entry->sym_ptr_ptr = sym;
    492       1.1     skrll 
    493   1.1.1.9  christos   internal->r_vaddr  = address;
    494       1.1     skrll   internal->r_symndx = sym_index;
    495       1.1     skrll   internal->r_type   = entry->howto ? entry->howto->type : 0;
    496       1.1     skrll 
    497       1.1     skrll   vars->relcount ++;
    498       1.1     skrll 
    499       1.1     skrll   BFD_ASSERT (vars->relcount <= NUM_ILF_RELOCS);
    500       1.1     skrll }
    501       1.1     skrll 
    502       1.1     skrll /* Create an empty relocation against the given section.  */
    503   1.1.1.6  christos 
    504   1.1.1.6  christos static void
    505       1.1     skrll pe_ILF_make_a_reloc (pe_ILF_vars *	       vars,
    506   1.1.1.6  christos 		     bfd_vma		       address,
    507       1.1     skrll 		     bfd_reloc_code_real_type  reloc,
    508  1.1.1.11  christos 		     asection_ptr	       sec)
    509       1.1     skrll {
    510       1.1     skrll   pe_ILF_make_a_symbol_reloc (vars, address, reloc, &sec->symbol,
    511       1.1     skrll 			      coff_section_data (vars->abfd, sec)->i);
    512       1.1     skrll }
    513       1.1     skrll 
    514       1.1     skrll /* Move the queued relocs into the given section.  */
    515  1.1.1.11  christos 
    516  1.1.1.11  christos static void
    517       1.1     skrll pe_ILF_save_relocs (pe_ILF_vars *vars,
    518       1.1     skrll 		    asection_ptr sec)
    519       1.1     skrll {
    520       1.1     skrll   /* Make sure that there is somewhere to store the internal relocs.  */
    521       1.1     skrll   if (coff_section_data (vars->abfd, sec) == NULL)
    522       1.1     skrll     /* We should probably return an error indication here.  */
    523       1.1     skrll     abort ();
    524       1.1     skrll 
    525       1.1     skrll   coff_section_data (vars->abfd, sec)->relocs = vars->int_reltab;
    526       1.1     skrll 
    527       1.1     skrll   sec->relocation  = vars->reltab;
    528       1.1     skrll   sec->reloc_count = vars->relcount;
    529       1.1     skrll   sec->flags      |= SEC_RELOC;
    530       1.1     skrll 
    531       1.1     skrll   vars->reltab     += vars->relcount;
    532       1.1     skrll   vars->int_reltab += vars->relcount;
    533       1.1     skrll   vars->relcount   = 0;
    534       1.1     skrll 
    535       1.1     skrll   BFD_ASSERT ((bfd_byte *) vars->int_reltab < (bfd_byte *) vars->string_table);
    536       1.1     skrll }
    537       1.1     skrll 
    538       1.1     skrll /* Create a global symbol and add it to the relevant tables.  */
    539       1.1     skrll 
    540       1.1     skrll static void
    541       1.1     skrll pe_ILF_make_a_symbol (pe_ILF_vars *  vars,
    542       1.1     skrll 		      const char *   prefix,
    543       1.1     skrll 		      const char *   symbol_name,
    544       1.1     skrll 		      asection_ptr   section,
    545  1.1.1.11  christos 		      flagword       extra_flags)
    546  1.1.1.11  christos {
    547  1.1.1.11  christos   coff_symbol_type *sym;
    548       1.1     skrll   combined_entry_type *ent;
    549       1.1     skrll   SYMENT *esym;
    550       1.1     skrll   unsigned short sclass;
    551       1.1     skrll 
    552       1.1     skrll   if (extra_flags & BSF_LOCAL)
    553       1.1     skrll     sclass = C_STAT;
    554       1.1     skrll   else
    555       1.1     skrll     sclass = C_EXT;
    556       1.1     skrll 
    557       1.1     skrll #ifdef THUMBPEMAGIC
    558       1.1     skrll   if (vars->magic == THUMBPEMAGIC)
    559       1.1     skrll     {
    560       1.1     skrll       if (extra_flags & BSF_FUNCTION)
    561       1.1     skrll 	sclass = C_THUMBEXTFUNC;
    562       1.1     skrll       else if (extra_flags & BSF_LOCAL)
    563       1.1     skrll 	sclass = C_THUMBSTAT;
    564       1.1     skrll       else
    565       1.1     skrll 	sclass = C_THUMBEXT;
    566       1.1     skrll     }
    567       1.1     skrll #endif
    568       1.1     skrll 
    569       1.1     skrll   BFD_ASSERT (vars->sym_index < NUM_ILF_SYMS);
    570       1.1     skrll 
    571       1.1     skrll   sym = vars->sym_ptr;
    572       1.1     skrll   ent = vars->native_ptr;
    573       1.1     skrll   esym = vars->esym_ptr;
    574  1.1.1.10  christos 
    575       1.1     skrll   /* Copy the symbol's name into the string table.  */
    576       1.1     skrll   int len = sprintf (vars->string_ptr, "%s%s", prefix, symbol_name);
    577   1.1.1.3  christos 
    578       1.1     skrll   if (section == NULL)
    579       1.1     skrll     section = bfd_und_section_ptr;
    580       1.1     skrll 
    581       1.1     skrll   /* Initialise the external symbol.  */
    582       1.1     skrll   H_PUT_32 (vars->abfd, vars->string_ptr - vars->string_table,
    583       1.1     skrll 	    esym->e.e.e_offset);
    584       1.1     skrll   H_PUT_16 (vars->abfd, section->target_index, esym->e_scnum);
    585       1.1     skrll   esym->e_sclass[0] = sclass;
    586       1.1     skrll 
    587       1.1     skrll   /* The following initialisations are unnecessary - the memory is
    588       1.1     skrll      zero initialised.  They are just kept here as reminders.  */
    589   1.1.1.6  christos 
    590   1.1.1.6  christos   /* Initialise the internal symbol structure.  */
    591   1.1.1.9  christos   ent->u.syment.n_sclass	  = sclass;
    592   1.1.1.9  christos   ent->u.syment.n_scnum		  = section->target_index;
    593       1.1     skrll   ent->u.syment._n._n_n._n_offset = (uintptr_t) sym;
    594       1.1     skrll   ent->is_sym = true;
    595       1.1     skrll 
    596       1.1     skrll   sym->symbol.the_bfd = vars->abfd;
    597       1.1     skrll   sym->symbol.name    = vars->string_ptr;
    598   1.1.1.6  christos   sym->symbol.flags   = BSF_EXPORT | BSF_GLOBAL | extra_flags;
    599       1.1     skrll   sym->symbol.section = section;
    600  1.1.1.11  christos   sym->native	      = ent;
    601  1.1.1.11  christos 
    602       1.1     skrll   *vars->table_ptr = vars->sym_index;
    603       1.1     skrll   *vars->sym_ptr_ptr = sym;
    604       1.1     skrll 
    605       1.1     skrll   /* Adjust pointers for the next symbol.  */
    606       1.1     skrll   vars->sym_index ++;
    607       1.1     skrll   vars->sym_ptr ++;
    608       1.1     skrll   vars->sym_ptr_ptr ++;
    609       1.1     skrll   vars->table_ptr ++;
    610  1.1.1.10  christos   vars->native_ptr ++;
    611       1.1     skrll   vars->esym_ptr ++;
    612       1.1     skrll   vars->string_ptr += len + 1;
    613       1.1     skrll 
    614       1.1     skrll   BFD_ASSERT (vars->string_ptr < vars->end_string_ptr);
    615       1.1     skrll }
    616       1.1     skrll 
    617       1.1     skrll /* Create a section.  */
    618       1.1     skrll 
    619       1.1     skrll static asection_ptr
    620       1.1     skrll pe_ILF_make_a_section (pe_ILF_vars * vars,
    621       1.1     skrll 		       const char *  name,
    622       1.1     skrll 		       unsigned int  size,
    623       1.1     skrll 		       flagword      extra_flags)
    624       1.1     skrll {
    625   1.1.1.9  christos   asection_ptr sec;
    626       1.1     skrll   flagword     flags;
    627       1.1     skrll   intptr_t alignment;
    628       1.1     skrll 
    629       1.1     skrll   sec = bfd_make_section_old_way (vars->abfd, name);
    630       1.1     skrll   if (sec == NULL)
    631       1.1     skrll     return NULL;
    632       1.1     skrll 
    633   1.1.1.8  christos   flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_IN_MEMORY;
    634       1.1     skrll 
    635   1.1.1.8  christos   bfd_set_section_flags (sec, flags | extra_flags);
    636       1.1     skrll 
    637       1.1     skrll   bfd_set_section_alignment (sec, 2);
    638       1.1     skrll 
    639       1.1     skrll   /* Check that we will not run out of space.  */
    640       1.1     skrll   BFD_ASSERT (vars->data + size < vars->bim->buffer + vars->bim->size);
    641       1.1     skrll 
    642   1.1.1.8  christos   /* Set the section size and contents.  The actual
    643       1.1     skrll      contents are filled in by our parent.  */
    644       1.1     skrll   bfd_set_section_size (sec, (bfd_size_type) size);
    645       1.1     skrll   sec->contents = vars->data;
    646       1.1     skrll   sec->target_index = vars->sec_index ++;
    647       1.1     skrll 
    648       1.1     skrll   /* Advance data pointer in the vars structure.  */
    649       1.1     skrll   vars->data += size;
    650       1.1     skrll 
    651       1.1     skrll   /* Skip the padding byte if it was not needed.
    652       1.1     skrll      The logic here is that if the string length is odd,
    653       1.1     skrll      then the entire string length, including the null byte,
    654       1.1     skrll      is even and so the extra, padding byte, is not needed.  */
    655       1.1     skrll   if (size & 1)
    656   1.1.1.4  christos     vars->data --;
    657   1.1.1.9  christos 
    658   1.1.1.9  christos   /* PR 18758: See note in pe_ILF_buid_a_bfd.  We must make sure that we
    659   1.1.1.9  christos      preserve host alignment requirements.  The BFD_ASSERTs in this
    660   1.1.1.9  christos      functions will warn us if we run out of room, but we should
    661   1.1.1.9  christos      already have enough padding built in to ILF_DATA_SIZE.  */
    662   1.1.1.9  christos #if GCC_VERSION >= 3000
    663   1.1.1.9  christos   alignment = __alignof__ (struct coff_section_tdata);
    664   1.1.1.4  christos #else
    665   1.1.1.9  christos   alignment = 8;
    666   1.1.1.9  christos #endif
    667   1.1.1.9  christos   vars->data
    668       1.1     skrll     = (bfd_byte *) (((intptr_t) vars->data + alignment - 1) & -alignment);
    669       1.1     skrll 
    670       1.1     skrll   /* Create a coff_section_tdata structure for our use.  */
    671       1.1     skrll   sec->used_by_bfd = (struct coff_section_tdata *) vars->data;
    672       1.1     skrll   vars->data += sizeof (struct coff_section_tdata);
    673       1.1     skrll 
    674       1.1     skrll   BFD_ASSERT (vars->data <= vars->bim->buffer + vars->bim->size);
    675       1.1     skrll 
    676       1.1     skrll   /* Create a symbol to refer to this section.  */
    677       1.1     skrll   pe_ILF_make_a_symbol (vars, "", name, sec, BSF_LOCAL);
    678       1.1     skrll 
    679       1.1     skrll   /* Cache the index to the symbol in the coff_section_data structure.  */
    680       1.1     skrll   coff_section_data (vars->abfd, sec)->i = vars->sym_index - 1;
    681       1.1     skrll 
    682       1.1     skrll   return sec;
    683       1.1     skrll }
    684       1.1     skrll 
    685       1.1     skrll /* This structure contains the code that goes into the .text section
    686       1.1     skrll    in order to perform a jump into the DLL lookup table.  The entries
    687       1.1     skrll    in the table are index by the magic number used to represent the
    688       1.1     skrll    machine type in the PE file.  The contents of the data[] arrays in
    689       1.1     skrll    these entries are stolen from the jtab[] arrays in ld/pe-dll.c.
    690       1.1     skrll    The SIZE field says how many bytes in the DATA array are actually
    691       1.1     skrll    used.  The OFFSET field says where in the data array the address
    692       1.1     skrll    of the .idata$5 section should be placed.  */
    693       1.1     skrll #define MAX_TEXT_SECTION_SIZE 32
    694       1.1     skrll 
    695       1.1     skrll typedef struct
    696       1.1     skrll {
    697       1.1     skrll   unsigned short magic;
    698       1.1     skrll   unsigned char  data[MAX_TEXT_SECTION_SIZE];
    699       1.1     skrll   unsigned int   size;
    700       1.1     skrll   unsigned int   offset;
    701       1.1     skrll }
    702   1.1.1.9  christos jump_table;
    703       1.1     skrll 
    704       1.1     skrll static const jump_table jtab[] =
    705       1.1     skrll {
    706       1.1     skrll #ifdef I386MAGIC
    707       1.1     skrll   { I386MAGIC,
    708       1.1     skrll     { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 },
    709       1.1     skrll     8, 2
    710       1.1     skrll   },
    711       1.1     skrll #endif
    712       1.1     skrll 
    713       1.1     skrll #ifdef AMD64MAGIC
    714       1.1     skrll   { AMD64MAGIC,
    715       1.1     skrll     { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 },
    716       1.1     skrll     8, 2
    717       1.1     skrll   },
    718       1.1     skrll #endif
    719       1.1     skrll 
    720       1.1     skrll #ifdef  MC68MAGIC
    721       1.1     skrll   { MC68MAGIC,
    722       1.1     skrll     { /* XXX fill me in */ },
    723       1.1     skrll     0, 0
    724       1.1     skrll   },
    725       1.1     skrll #endif
    726       1.1     skrll 
    727       1.1     skrll #ifdef  MIPS_ARCH_MAGIC_WINCE
    728       1.1     skrll   { MIPS_ARCH_MAGIC_WINCE,
    729       1.1     skrll     { 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
    730       1.1     skrll       0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 },
    731       1.1     skrll     16, 0
    732       1.1     skrll   },
    733       1.1     skrll #endif
    734       1.1     skrll 
    735       1.1     skrll #ifdef  SH_ARCH_MAGIC_WINCE
    736       1.1     skrll   { SH_ARCH_MAGIC_WINCE,
    737       1.1     skrll     { 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40,
    738       1.1     skrll       0x09, 0x00, 0x00, 0x00, 0x00, 0x00 },
    739       1.1     skrll     12, 8
    740       1.1     skrll   },
    741   1.1.1.9  christos #endif
    742   1.1.1.9  christos 
    743   1.1.1.9  christos #ifdef AARCH64MAGIC
    744   1.1.1.9  christos /* We don't currently support jumping to DLLs, so if
    745   1.1.1.9  christos    someone does try emit a runtime trap.  Through UDF #0.  */
    746   1.1.1.9  christos   { AARCH64MAGIC,
    747   1.1.1.9  christos     { 0x00, 0x00, 0x00, 0x00 },
    748   1.1.1.9  christos     4, 0
    749   1.1.1.9  christos   },
    750   1.1.1.9  christos 
    751       1.1     skrll #endif
    752       1.1     skrll 
    753       1.1     skrll #ifdef  ARMPEMAGIC
    754       1.1     skrll   { ARMPEMAGIC,
    755       1.1     skrll     { 0x00, 0xc0, 0x9f, 0xe5, 0x00, 0xf0,
    756       1.1     skrll       0x9c, 0xe5, 0x00, 0x00, 0x00, 0x00},
    757       1.1     skrll     12, 8
    758       1.1     skrll   },
    759       1.1     skrll #endif
    760       1.1     skrll 
    761       1.1     skrll #ifdef  THUMBPEMAGIC
    762       1.1     skrll   { THUMBPEMAGIC,
    763       1.1     skrll     { 0x40, 0xb4, 0x02, 0x4e, 0x36, 0x68, 0xb4, 0x46,
    764       1.1     skrll       0x40, 0xbc, 0x60, 0x47, 0x00, 0x00, 0x00, 0x00 },
    765       1.1     skrll     16, 12
    766  1.1.1.10  christos   },
    767  1.1.1.10  christos #endif
    768  1.1.1.10  christos 
    769  1.1.1.10  christos #ifdef LOONGARCH64MAGIC
    770  1.1.1.10  christos /* We don't currently support jumping to DLLs, so if
    771  1.1.1.10  christos    someone does try emit a runtime trap.  Through BREAK 0.  */
    772  1.1.1.10  christos   { LOONGARCH64MAGIC,
    773  1.1.1.10  christos     { 0x00, 0x00, 0x2a, 0x00 },
    774  1.1.1.10  christos     4, 0
    775  1.1.1.10  christos   },
    776  1.1.1.10  christos 
    777  1.1.1.10  christos #endif
    778  1.1.1.10  christos 
    779  1.1.1.10  christos #ifdef RISCV64MAGIC
    780  1.1.1.10  christos   /* We don't currently support jumping to DLLs, so if
    781  1.1.1.10  christos      someone does try emit a runtime trap.  Through EBREAK.  */
    782  1.1.1.10  christos   { RISCV64MAGIC,
    783  1.1.1.10  christos     { 0x73, 0x00, 0x10, 0x00 },
    784  1.1.1.10  christos     4, 0
    785  1.1.1.10  christos   },
    786  1.1.1.10  christos 
    787       1.1     skrll #endif
    788       1.1     skrll 
    789       1.1     skrll   { 0, { 0 }, 0, 0 }
    790       1.1     skrll };
    791       1.1     skrll 
    792       1.1     skrll #ifndef NUM_ENTRIES
    793       1.1     skrll #define NUM_ENTRIES(a) (sizeof (a) / sizeof (a)[0])
    794       1.1     skrll #endif
    795       1.1     skrll 
    796   1.1.1.9  christos /* Build a full BFD from the information supplied in a ILF object.  */
    797   1.1.1.6  christos 
    798       1.1     skrll static bool
    799   1.1.1.6  christos pe_ILF_build_a_bfd (bfd *	    abfd,
    800   1.1.1.6  christos 		    unsigned int    magic,
    801       1.1     skrll 		    char *	    symbol_name,
    802  1.1.1.11  christos 		    char *	    source_dll,
    803  1.1.1.11  christos 		    unsigned int    ordinal,
    804       1.1     skrll 		    unsigned int    types,
    805   1.1.1.6  christos 		    char *	    import_name)
    806   1.1.1.6  christos {
    807       1.1     skrll   bfd_byte *		   ptr;
    808   1.1.1.6  christos   pe_ILF_vars		   vars;
    809   1.1.1.6  christos   struct internal_filehdr  internal_f;
    810   1.1.1.6  christos   unsigned int		   import_type;
    811   1.1.1.6  christos   unsigned int		   import_name_type;
    812   1.1.1.6  christos   asection_ptr		   id4, id5, id6 = NULL, text = NULL;
    813   1.1.1.9  christos   coff_symbol_type **	   imp_sym;
    814       1.1     skrll   unsigned int		   imp_index;
    815       1.1     skrll   intptr_t alignment;
    816       1.1     skrll 
    817       1.1     skrll   /* Decode and verify the types field of the ILF structure.  */
    818       1.1     skrll   import_type = types & 0x3;
    819       1.1     skrll   import_name_type = (types & 0x1c) >> 2;
    820       1.1     skrll 
    821       1.1     skrll   switch (import_type)
    822       1.1     skrll     {
    823       1.1     skrll     case IMPORT_CODE:
    824  1.1.1.11  christos     case IMPORT_DATA:
    825       1.1     skrll     case IMPORT_CONST:
    826       1.1     skrll       break;
    827   1.1.1.6  christos 
    828   1.1.1.7  christos     default:
    829       1.1     skrll       /* xgettext:c-format */
    830   1.1.1.9  christos       _bfd_error_handler (_("%pB: unrecognized import type; %x"),
    831       1.1     skrll 			  abfd, import_type);
    832       1.1     skrll       return false;
    833       1.1     skrll     }
    834       1.1     skrll 
    835       1.1     skrll   switch (import_name_type)
    836       1.1     skrll     {
    837       1.1     skrll     case IMPORT_ORDINAL:
    838       1.1     skrll     case IMPORT_NAME:
    839  1.1.1.11  christos     case IMPORT_NAME_NOPREFIX:
    840  1.1.1.11  christos     case IMPORT_NAME_UNDECORATE:
    841  1.1.1.11  christos       import_name = symbol_name;
    842  1.1.1.11  christos       break;
    843  1.1.1.11  christos 
    844  1.1.1.11  christos     case IMPORT_NAME_EXPORTAS:
    845  1.1.1.11  christos       if (!import_name || !import_name[0])
    846  1.1.1.11  christos 	{
    847  1.1.1.11  christos 	  _bfd_error_handler (_("%pB: missing import name for "
    848  1.1.1.11  christos 				"IMPORT_NAME_EXPORTAS for %s"),
    849  1.1.1.11  christos 			      abfd, symbol_name);
    850       1.1     skrll 	  return false;
    851       1.1     skrll 	}
    852       1.1     skrll       break;
    853   1.1.1.6  christos 
    854   1.1.1.7  christos     default:
    855       1.1     skrll       /* xgettext:c-format */
    856   1.1.1.9  christos       _bfd_error_handler (_("%pB: unrecognized import name type; %x"),
    857       1.1     skrll 			  abfd, import_name_type);
    858       1.1     skrll       return false;
    859       1.1     skrll     }
    860       1.1     skrll 
    861       1.1     skrll   /* Initialise local variables.
    862       1.1     skrll 
    863       1.1     skrll      Note these are kept in a structure rather than being
    864       1.1     skrll      declared as statics since bfd frowns on global variables.
    865       1.1     skrll 
    866   1.1.1.2  christos      We are going to construct the contents of the BFD in memory,
    867   1.1.1.2  christos      so allocate all the space that we will need right now.  */
    868   1.1.1.2  christos   vars.bim
    869   1.1.1.9  christos     = (struct bfd_in_memory *) bfd_malloc ((bfd_size_type) sizeof (*vars.bim));
    870       1.1     skrll   if (vars.bim == NULL)
    871   1.1.1.2  christos     return false;
    872       1.1     skrll 
    873       1.1     skrll   ptr = (bfd_byte *) bfd_zmalloc ((bfd_size_type) ILF_DATA_SIZE);
    874   1.1.1.2  christos   vars.bim->buffer = ptr;
    875   1.1.1.2  christos   vars.bim->size   = ILF_DATA_SIZE;
    876       1.1     skrll   if (ptr == NULL)
    877       1.1     skrll     goto error_return;
    878       1.1     skrll 
    879       1.1     skrll   /* Initialise the pointers to regions of the memory and the
    880       1.1     skrll      other contents of the pe_ILF_vars structure as well.  */
    881       1.1     skrll   vars.sym_cache = (coff_symbol_type *) ptr;
    882       1.1     skrll   vars.sym_ptr   = (coff_symbol_type *) ptr;
    883       1.1     skrll   vars.sym_index = 0;
    884       1.1     skrll   ptr += SIZEOF_ILF_SYMS;
    885       1.1     skrll 
    886       1.1     skrll   vars.sym_table = (unsigned int *) ptr;
    887       1.1     skrll   vars.table_ptr = (unsigned int *) ptr;
    888       1.1     skrll   ptr += SIZEOF_ILF_SYM_TABLE;
    889       1.1     skrll 
    890       1.1     skrll   vars.native_syms = (combined_entry_type *) ptr;
    891       1.1     skrll   vars.native_ptr  = (combined_entry_type *) ptr;
    892       1.1     skrll   ptr += SIZEOF_ILF_NATIVE_SYMS;
    893       1.1     skrll 
    894       1.1     skrll   vars.sym_ptr_table = (coff_symbol_type **) ptr;
    895       1.1     skrll   vars.sym_ptr_ptr   = (coff_symbol_type **) ptr;
    896       1.1     skrll   ptr += SIZEOF_ILF_SYM_PTR_TABLE;
    897       1.1     skrll 
    898       1.1     skrll   vars.esym_table = (SYMENT *) ptr;
    899       1.1     skrll   vars.esym_ptr   = (SYMENT *) ptr;
    900       1.1     skrll   ptr += SIZEOF_ILF_EXT_SYMS;
    901       1.1     skrll 
    902       1.1     skrll   vars.reltab   = (arelent *) ptr;
    903       1.1     skrll   vars.relcount = 0;
    904       1.1     skrll   ptr += SIZEOF_ILF_RELOCS;
    905       1.1     skrll 
    906       1.1     skrll   vars.int_reltab  = (struct internal_reloc *) ptr;
    907       1.1     skrll   ptr += SIZEOF_ILF_INT_RELOCS;
    908       1.1     skrll 
    909       1.1     skrll   vars.string_table = (char *) ptr;
    910       1.1     skrll   vars.string_ptr   = (char *) ptr + STRING_SIZE_SIZE;
    911       1.1     skrll   ptr += SIZEOF_ILF_STRINGS;
    912       1.1     skrll   vars.end_string_ptr = (char *) ptr;
    913       1.1     skrll 
    914   1.1.1.9  christos   /* The remaining space in bim->buffer is used
    915   1.1.1.4  christos      by the pe_ILF_make_a_section() function.  */
    916   1.1.1.9  christos 
    917   1.1.1.9  christos   /* PR 18758: Make sure that the data area is sufficiently aligned for
    918   1.1.1.9  christos      struct coff_section_tdata.  __alignof__ is a gcc extension, hence
    919   1.1.1.9  christos      the test of GCC_VERSION.  For other compilers we assume 8 byte
    920   1.1.1.9  christos      alignment.  */
    921   1.1.1.9  christos #if GCC_VERSION >= 3000
    922   1.1.1.9  christos   alignment = __alignof__ (struct coff_section_tdata);
    923   1.1.1.4  christos #else
    924   1.1.1.9  christos   alignment = 8;
    925   1.1.1.4  christos #endif
    926       1.1     skrll   ptr = (bfd_byte *) (((intptr_t) ptr + alignment - 1) & -alignment);
    927       1.1     skrll 
    928       1.1     skrll   vars.data = ptr;
    929       1.1     skrll   vars.abfd = abfd;
    930       1.1     skrll   vars.sec_index = 0;
    931       1.1     skrll   vars.magic = magic;
    932       1.1     skrll 
    933       1.1     skrll   /* Create the initial .idata$<n> sections:
    934       1.1     skrll      [.idata$2:  Import Directory Table -- not needed]
    935       1.1     skrll      .idata$4:  Import Lookup Table
    936       1.1     skrll      .idata$5:  Import Address Table
    937       1.1     skrll 
    938  1.1.1.11  christos      Note we do not create a .idata$3 section as this is
    939  1.1.1.11  christos      created for us by the linker script.  */
    940       1.1     skrll   id4 = pe_ILF_make_a_section (&vars, ".idata$4", SIZEOF_IDATA4, 0);
    941   1.1.1.2  christos   id5 = pe_ILF_make_a_section (&vars, ".idata$5", SIZEOF_IDATA5, 0);
    942       1.1     skrll   if (id4 == NULL || id5 == NULL)
    943       1.1     skrll     goto error_return;
    944       1.1     skrll 
    945       1.1     skrll   /* Fill in the contents of these sections.  */
    946       1.1     skrll   if (import_name_type == IMPORT_ORDINAL)
    947   1.1.1.6  christos     {
    948   1.1.1.6  christos       if (ordinal == 0)
    949       1.1     skrll 	/* See PR 20907 for a reproducer.  */
    950  1.1.1.11  christos 	goto error_return;
    951  1.1.1.11  christos 
    952       1.1     skrll #if (defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64) \
    953       1.1     skrll      || defined(COFF_WITH_peLoongArch64) || defined (COFF_WITH_peRiscV64))
    954       1.1     skrll       ((unsigned int *) id4->contents)[0] = ordinal;
    955       1.1     skrll       ((unsigned int *) id4->contents)[1] = 0x80000000;
    956       1.1     skrll       ((unsigned int *) id5->contents)[0] = ordinal;
    957  1.1.1.11  christos       ((unsigned int *) id5->contents)[1] = 0x80000000;
    958  1.1.1.11  christos #else
    959       1.1     skrll       ((unsigned int *) id4->contents)[0] = ordinal | 0x80000000;
    960       1.1     skrll       ((unsigned int *) id5->contents)[0] = ordinal | 0x80000000;
    961       1.1     skrll #endif
    962       1.1     skrll     }
    963  1.1.1.11  christos   else
    964       1.1     skrll     {
    965       1.1     skrll       char *symbol;
    966       1.1     skrll       unsigned int len;
    967  1.1.1.11  christos 
    968       1.1     skrll       /* Create .idata$6 - the Hint Name Table.  */
    969   1.1.1.2  christos       id6 = pe_ILF_make_a_section (&vars, ".idata$6", SIZEOF_IDATA6, 0);
    970       1.1     skrll       if (id6 == NULL)
    971       1.1     skrll 	goto error_return;
    972  1.1.1.11  christos 
    973       1.1     skrll       /* If necessary, trim the import symbol name.  */
    974       1.1     skrll       symbol = import_name;
    975       1.1     skrll 
    976       1.1     skrll       /* As used by MS compiler, '_', '@', and '?' are alternative
    977       1.1     skrll 	 forms of USER_LABEL_PREFIX, with '?' for c++ mangled names,
    978       1.1     skrll 	 '@' used for fastcall (in C),  '_' everywhere else.  Only one
    979       1.1     skrll 	 of these is used for a symbol.  We strip this leading char for
    980       1.1     skrll 	 IMPORT_NAME_NOPREFIX and IMPORT_NAME_UNDECORATE as per the
    981  1.1.1.11  christos 	 PE COFF 6.0 spec (section 8.3, Import Name Type).  */
    982  1.1.1.11  christos 
    983       1.1     skrll       if (import_name_type != IMPORT_NAME
    984       1.1     skrll 	  && import_name_type != IMPORT_NAME_EXPORTAS)
    985   1.1.1.4  christos 	{
    986   1.1.1.3  christos 	  char c = symbol[0];
    987   1.1.1.3  christos 
    988   1.1.1.3  christos 	  /* Check that we don't remove for targets with empty
    989   1.1.1.3  christos 	     USER_LABEL_PREFIX the leading underscore.  */
    990       1.1     skrll 	  if ((c == '_' && abfd->xvec->symbol_leading_char != 0)
    991       1.1     skrll 	      || c == '@' || c == '?')
    992   1.1.1.4  christos 	    symbol++;
    993       1.1     skrll 	}
    994       1.1     skrll 
    995       1.1     skrll       len = strlen (symbol);
    996       1.1     skrll       if (import_name_type == IMPORT_NAME_UNDECORATE)
    997       1.1     skrll 	{
    998       1.1     skrll 	  /* Truncate at the first '@'.  */
    999       1.1     skrll 	  char *at = strchr (symbol, '@');
   1000       1.1     skrll 
   1001       1.1     skrll 	  if (at != NULL)
   1002       1.1     skrll 	    len = at - symbol;
   1003       1.1     skrll 	}
   1004       1.1     skrll 
   1005       1.1     skrll       id6->contents[0] = ordinal & 0xff;
   1006       1.1     skrll       id6->contents[1] = ordinal >> 8;
   1007       1.1     skrll 
   1008       1.1     skrll       memcpy ((char *) id6->contents + 2, symbol, len);
   1009       1.1     skrll       id6->contents[len + 2] = '\0';
   1010       1.1     skrll     }
   1011       1.1     skrll 
   1012       1.1     skrll   if (import_name_type != IMPORT_ORDINAL)
   1013       1.1     skrll     {
   1014       1.1     skrll       pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
   1015       1.1     skrll       pe_ILF_save_relocs (&vars, id4);
   1016       1.1     skrll 
   1017       1.1     skrll       pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
   1018       1.1     skrll       pe_ILF_save_relocs (&vars, id5);
   1019   1.1.1.5  christos     }
   1020  1.1.1.11  christos 
   1021   1.1.1.5  christos   /* Create an import symbol.  */
   1022   1.1.1.5  christos   pe_ILF_make_a_symbol (&vars, "__imp_", symbol_name, id5, 0);
   1023   1.1.1.5  christos   imp_sym   = vars.sym_ptr_ptr - 1;
   1024  1.1.1.11  christos   imp_index = vars.sym_index - 1;
   1025  1.1.1.11  christos 
   1026       1.1     skrll   /* Create extra sections depending upon the type of import we are
   1027       1.1     skrll      dealing with.  */
   1028       1.1     skrll   switch (import_type)
   1029       1.1     skrll     {
   1030       1.1     skrll       int i;
   1031   1.1.1.5  christos 
   1032   1.1.1.6  christos     case IMPORT_CODE:
   1033       1.1     skrll       /* CODE functions are special, in that they get a trampoline that
   1034       1.1     skrll 	 jumps to the main import symbol.  Create a .text section to hold it.
   1035       1.1     skrll 	 First we need to look up its contents in the jump table.  */
   1036       1.1     skrll       for (i = NUM_ENTRIES (jtab); i--;)
   1037       1.1     skrll 	{
   1038       1.1     skrll 	  if (jtab[i].size == 0)
   1039       1.1     skrll 	    continue;
   1040       1.1     skrll 	  if (jtab[i].magic == magic)
   1041       1.1     skrll 	    break;
   1042       1.1     skrll 	}
   1043       1.1     skrll       /* If we did not find a matching entry something is wrong.  */
   1044       1.1     skrll       if (i < 0)
   1045       1.1     skrll 	abort ();
   1046  1.1.1.11  christos 
   1047       1.1     skrll       /* Create the .text section.  */
   1048   1.1.1.2  christos       text = pe_ILF_make_a_section (&vars, ".text", jtab[i].size, SEC_CODE);
   1049       1.1     skrll       if (text == NULL)
   1050       1.1     skrll 	goto error_return;
   1051       1.1     skrll 
   1052       1.1     skrll       /* Copy in the jump code.  */
   1053       1.1     skrll       memcpy (text->contents, jtab[i].data, jtab[i].size);
   1054       1.1     skrll 
   1055       1.1     skrll       /* Create a reloc for the data in the text section.  */
   1056       1.1     skrll #ifdef MIPS_ARCH_MAGIC_WINCE
   1057       1.1     skrll       if (magic == MIPS_ARCH_MAGIC_WINCE)
   1058       1.1     skrll 	{
   1059       1.1     skrll 	  pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) 0, BFD_RELOC_HI16_S,
   1060       1.1     skrll 				      (struct bfd_symbol **) imp_sym,
   1061       1.1     skrll 				      imp_index);
   1062       1.1     skrll 	  pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_LO16, text);
   1063       1.1     skrll 	  pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) 4, BFD_RELOC_LO16,
   1064       1.1     skrll 				      (struct bfd_symbol **) imp_sym,
   1065       1.1     skrll 				      imp_index);
   1066       1.1     skrll 	}
   1067   1.1.1.4  christos       else
   1068   1.1.1.4  christos #endif
   1069   1.1.1.4  christos #ifdef AMD64MAGIC
   1070   1.1.1.4  christos       if (magic == AMD64MAGIC)
   1071   1.1.1.4  christos 	{
   1072   1.1.1.4  christos 	  pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) jtab[i].offset,
   1073   1.1.1.4  christos 				      BFD_RELOC_32_PCREL, (asymbol **) imp_sym,
   1074   1.1.1.4  christos 				      imp_index);
   1075   1.1.1.4  christos 	}
   1076       1.1     skrll       else
   1077       1.1     skrll #endif
   1078       1.1     skrll 	pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) jtab[i].offset,
   1079       1.1     skrll 				    BFD_RELOC_32, (asymbol **) imp_sym,
   1080  1.1.1.11  christos 				    imp_index);
   1081       1.1     skrll 
   1082       1.1     skrll       pe_ILF_save_relocs (&vars, text);
   1083       1.1     skrll       break;
   1084  1.1.1.11  christos 
   1085       1.1     skrll     case IMPORT_DATA:
   1086       1.1     skrll     case IMPORT_CONST:
   1087       1.1     skrll       break;
   1088       1.1     skrll 
   1089       1.1     skrll     default:
   1090       1.1     skrll       /* XXX code not yet written.  */
   1091       1.1     skrll       abort ();
   1092  1.1.1.10  christos     }
   1093  1.1.1.10  christos 
   1094  1.1.1.10  christos   /* Now create a symbol describing the imported value.  */
   1095  1.1.1.10  christos   switch (import_type)
   1096  1.1.1.11  christos     {
   1097  1.1.1.10  christos     case IMPORT_CODE:
   1098  1.1.1.10  christos       pe_ILF_make_a_symbol (&vars, "", symbol_name, text,
   1099  1.1.1.10  christos 			    BSF_NOT_AT_END | BSF_FUNCTION);
   1100  1.1.1.10  christos 
   1101  1.1.1.10  christos       break;
   1102  1.1.1.10  christos 
   1103  1.1.1.10  christos     case IMPORT_DATA:
   1104  1.1.1.10  christos       /* Nothing to do here.  */
   1105  1.1.1.11  christos       break;
   1106  1.1.1.11  christos 
   1107  1.1.1.11  christos     case IMPORT_CONST:
   1108  1.1.1.11  christos       pe_ILF_make_a_symbol (&vars, "", symbol_name, id5, 0);
   1109  1.1.1.10  christos       break;
   1110  1.1.1.10  christos 
   1111  1.1.1.10  christos     default:
   1112  1.1.1.10  christos       /* XXX code not yet written.  */
   1113  1.1.1.10  christos       abort ();
   1114  1.1.1.10  christos     }
   1115  1.1.1.10  christos 
   1116  1.1.1.10  christos   /* Create an import symbol for the DLL, without the .dll suffix.  */
   1117  1.1.1.11  christos   ptr = (bfd_byte *) strrchr (source_dll, '.');
   1118  1.1.1.11  christos   if (ptr)
   1119  1.1.1.10  christos     *ptr = 0;
   1120  1.1.1.11  christos   pe_ILF_make_a_symbol (&vars, "__IMPORT_DESCRIPTOR_", source_dll, NULL, 0);
   1121  1.1.1.10  christos   if (ptr)
   1122       1.1     skrll     *ptr = '.';
   1123  1.1.1.11  christos 
   1124       1.1     skrll   /* Initialise the bfd.  */
   1125       1.1     skrll   memset (&internal_f, 0, sizeof (internal_f));
   1126       1.1     skrll 
   1127       1.1     skrll   internal_f.f_magic  = magic;
   1128       1.1     skrll   internal_f.f_symptr = 0;
   1129       1.1     skrll   internal_f.f_nsyms  = 0;
   1130  1.1.1.11  christos   internal_f.f_flags  = F_AR32WR | F_LNNO; /* XXX is this correct ?  */
   1131  1.1.1.11  christos 
   1132   1.1.1.2  christos   if (!bfd_set_start_address (abfd, (bfd_vma) 0)
   1133       1.1     skrll       || !bfd_coff_set_arch_mach_hook (abfd, &internal_f))
   1134  1.1.1.11  christos     goto error_return;
   1135   1.1.1.2  christos 
   1136       1.1     skrll   if (bfd_coff_mkobject_hook (abfd, (void *) &internal_f, NULL) == NULL)
   1137  1.1.1.10  christos     goto error_return;
   1138       1.1     skrll 
   1139       1.1     skrll   obj_pe (abfd) = true;
   1140  1.1.1.11  christos #ifdef THUMBPEMAGIC
   1141  1.1.1.11  christos   if (vars.magic == THUMBPEMAGIC)
   1142       1.1     skrll     /* Stop some linker warnings about thumb code not supporting
   1143       1.1     skrll        interworking.  */
   1144       1.1     skrll     coff_data (abfd)->flags |= F_INTERWORK | F_INTERWORK_SET;
   1145       1.1     skrll #endif
   1146       1.1     skrll 
   1147       1.1     skrll   /* Switch from file contents to memory contents.  */
   1148       1.1     skrll   bfd_cache_close (abfd);
   1149  1.1.1.10  christos 
   1150   1.1.1.2  christos   abfd->iostream = (void *) vars.bim;
   1151       1.1     skrll   abfd->flags |= BFD_IN_MEMORY | HAS_SYMS;
   1152   1.1.1.2  christos   abfd->iovec = &_bfd_memory_iovec;
   1153  1.1.1.10  christos   abfd->where = 0;
   1154       1.1     skrll   abfd->origin = 0;
   1155       1.1     skrll   abfd->size = 0;
   1156       1.1     skrll   obj_sym_filepos (abfd) = 0;
   1157       1.1     skrll 
   1158   1.1.1.8  christos   /* Point the bfd at the symbol table.  */
   1159       1.1     skrll   obj_symbols (abfd) = vars.sym_cache;
   1160       1.1     skrll   abfd->symcount = vars.sym_index;
   1161       1.1     skrll 
   1162  1.1.1.11  christos   obj_raw_syments (abfd) = vars.native_syms;
   1163       1.1     skrll   obj_raw_syment_count (abfd) = vars.sym_index;
   1164       1.1     skrll   obj_coff_keep_raw_syms (abfd) = true;
   1165   1.1.1.9  christos 
   1166       1.1     skrll   obj_coff_external_syms (abfd) = (void *) vars.esym_table;
   1167       1.1     skrll   obj_coff_keep_syms (abfd) = true;
   1168       1.1     skrll 
   1169       1.1     skrll   obj_convert (abfd) = vars.sym_table;
   1170       1.1     skrll   obj_conv_table_size (abfd) = vars.sym_index;
   1171  1.1.1.10  christos 
   1172   1.1.1.9  christos   obj_coff_strings (abfd) = vars.string_table;
   1173       1.1     skrll   obj_coff_strings_len (abfd) = vars.string_ptr - vars.string_table;
   1174   1.1.1.9  christos   obj_coff_keep_strings (abfd) = true;
   1175   1.1.1.2  christos 
   1176   1.1.1.2  christos   return true;
   1177   1.1.1.9  christos 
   1178   1.1.1.2  christos  error_return:
   1179   1.1.1.9  christos   free (vars.bim->buffer);
   1180       1.1     skrll   free (vars.bim);
   1181       1.1     skrll   return false;
   1182  1.1.1.10  christos }
   1183  1.1.1.10  christos 
   1184  1.1.1.10  christos /* Cleanup function, returned from check_format hook.  */
   1185  1.1.1.10  christos 
   1186  1.1.1.10  christos static void
   1187  1.1.1.10  christos pe_ILF_cleanup (bfd *abfd)
   1188  1.1.1.10  christos {
   1189  1.1.1.10  christos   coff_object_cleanup (abfd);
   1190  1.1.1.10  christos 
   1191  1.1.1.10  christos   struct bfd_in_memory *bim = abfd->iostream;
   1192  1.1.1.10  christos   free (bim->buffer);
   1193  1.1.1.10  christos   free (bim);
   1194  1.1.1.10  christos   abfd->iostream = NULL;
   1195  1.1.1.10  christos }
   1196       1.1     skrll 
   1197       1.1     skrll /* We have detected an Import Library Format archive element.
   1198   1.1.1.9  christos    Decode the element and return the appropriate target.  */
   1199  1.1.1.11  christos 
   1200       1.1     skrll static bfd_cleanup
   1201   1.1.1.6  christos pe_ILF_object_p (bfd *abfd)
   1202   1.1.1.6  christos {
   1203   1.1.1.6  christos   bfd_byte	  buffer[14];
   1204   1.1.1.6  christos   bfd_byte *	  ptr;
   1205  1.1.1.11  christos   char *	  symbol_name;
   1206   1.1.1.6  christos   char *	  source_dll;
   1207   1.1.1.6  christos   char *	  import_name;
   1208   1.1.1.6  christos   unsigned int	  machine;
   1209   1.1.1.6  christos   bfd_size_type	  size;
   1210   1.1.1.6  christos   unsigned int	  ordinal;
   1211       1.1     skrll   unsigned int	  types;
   1212   1.1.1.4  christos   unsigned int	  magic;
   1213  1.1.1.10  christos 
   1214  1.1.1.10  christos   /* Upon entry the first six bytes of the ILF header have
   1215       1.1     skrll      already been read.  Now read the rest of the header.  */
   1216       1.1     skrll   if (bfd_read (buffer, 14, abfd) != 14)
   1217       1.1     skrll     return NULL;
   1218       1.1     skrll 
   1219       1.1     skrll   ptr = buffer;
   1220       1.1     skrll 
   1221       1.1     skrll   machine = H_GET_16 (abfd, ptr);
   1222       1.1     skrll   ptr += 2;
   1223       1.1     skrll 
   1224       1.1     skrll   /* Check that the machine type is recognised.  */
   1225       1.1     skrll   magic = 0;
   1226       1.1     skrll 
   1227       1.1     skrll   switch (machine)
   1228       1.1     skrll     {
   1229       1.1     skrll     case IMAGE_FILE_MACHINE_UNKNOWN:
   1230       1.1     skrll     case IMAGE_FILE_MACHINE_ALPHA:
   1231       1.1     skrll     case IMAGE_FILE_MACHINE_ALPHA64:
   1232       1.1     skrll     case IMAGE_FILE_MACHINE_IA64:
   1233       1.1     skrll       break;
   1234       1.1     skrll 
   1235       1.1     skrll     case IMAGE_FILE_MACHINE_I386:
   1236       1.1     skrll #ifdef I386MAGIC
   1237       1.1     skrll       magic = I386MAGIC;
   1238       1.1     skrll #endif
   1239       1.1     skrll       break;
   1240       1.1     skrll 
   1241       1.1     skrll     case IMAGE_FILE_MACHINE_AMD64:
   1242       1.1     skrll #ifdef AMD64MAGIC
   1243       1.1     skrll       magic = AMD64MAGIC;
   1244       1.1     skrll #endif
   1245       1.1     skrll       break;
   1246       1.1     skrll 
   1247       1.1     skrll     case IMAGE_FILE_MACHINE_R3000:
   1248       1.1     skrll     case IMAGE_FILE_MACHINE_R4000:
   1249       1.1     skrll     case IMAGE_FILE_MACHINE_R10000:
   1250       1.1     skrll 
   1251       1.1     skrll     case IMAGE_FILE_MACHINE_MIPS16:
   1252       1.1     skrll     case IMAGE_FILE_MACHINE_MIPSFPU:
   1253       1.1     skrll     case IMAGE_FILE_MACHINE_MIPSFPU16:
   1254       1.1     skrll #ifdef MIPS_ARCH_MAGIC_WINCE
   1255       1.1     skrll       magic = MIPS_ARCH_MAGIC_WINCE;
   1256       1.1     skrll #endif
   1257       1.1     skrll       break;
   1258       1.1     skrll 
   1259       1.1     skrll     case IMAGE_FILE_MACHINE_SH3:
   1260       1.1     skrll     case IMAGE_FILE_MACHINE_SH4:
   1261       1.1     skrll #ifdef SH_ARCH_MAGIC_WINCE
   1262       1.1     skrll       magic = SH_ARCH_MAGIC_WINCE;
   1263       1.1     skrll #endif
   1264       1.1     skrll       break;
   1265       1.1     skrll 
   1266       1.1     skrll     case IMAGE_FILE_MACHINE_ARM:
   1267       1.1     skrll #ifdef ARMPEMAGIC
   1268       1.1     skrll       magic = ARMPEMAGIC;
   1269       1.1     skrll #endif
   1270   1.1.1.9  christos       break;
   1271   1.1.1.9  christos 
   1272   1.1.1.9  christos     case IMAGE_FILE_MACHINE_ARM64:
   1273   1.1.1.9  christos #ifdef AARCH64MAGIC
   1274   1.1.1.9  christos       magic = AARCH64MAGIC;
   1275   1.1.1.9  christos #endif
   1276  1.1.1.10  christos       break;
   1277  1.1.1.10  christos 
   1278  1.1.1.10  christos     case IMAGE_FILE_MACHINE_LOONGARCH64:
   1279  1.1.1.10  christos #ifdef LOONGARCH64MAGIC
   1280  1.1.1.10  christos       magic = LOONGARCH64MAGIC;
   1281  1.1.1.10  christos #endif
   1282  1.1.1.10  christos       break;
   1283  1.1.1.10  christos 
   1284  1.1.1.10  christos     case IMAGE_FILE_MACHINE_RISCV64:
   1285  1.1.1.10  christos #ifdef RISCV64MAGIC
   1286  1.1.1.10  christos       magic = RISCV64MAGIC;
   1287  1.1.1.10  christos #endif
   1288       1.1     skrll       break;
   1289       1.1     skrll 
   1290       1.1     skrll     case IMAGE_FILE_MACHINE_THUMB:
   1291       1.1     skrll #ifdef THUMBPEMAGIC
   1292       1.1     skrll       {
   1293  1.1.1.11  christos 	extern const bfd_target TARGET_LITTLE_SYM;
   1294       1.1     skrll 
   1295       1.1     skrll 	if (abfd->xvec == &TARGET_LITTLE_SYM)
   1296       1.1     skrll 	  magic = THUMBPEMAGIC;
   1297       1.1     skrll       }
   1298       1.1     skrll #endif
   1299       1.1     skrll       break;
   1300       1.1     skrll 
   1301       1.1     skrll     case IMAGE_FILE_MACHINE_POWERPC:
   1302       1.1     skrll       /* We no longer support PowerPC.  */
   1303   1.1.1.6  christos     default:
   1304   1.1.1.7  christos       _bfd_error_handler
   1305       1.1     skrll 	/* xgettext:c-format */
   1306       1.1     skrll 	(_("%pB: unrecognised machine type (0x%x)"
   1307       1.1     skrll 	   " in Import Library Format archive"),
   1308       1.1     skrll 	 abfd, machine);
   1309       1.1     skrll       bfd_set_error (bfd_error_malformed_archive);
   1310       1.1     skrll 
   1311       1.1     skrll       return NULL;
   1312       1.1     skrll       break;
   1313       1.1     skrll     }
   1314       1.1     skrll 
   1315       1.1     skrll   if (magic == 0)
   1316   1.1.1.6  christos     {
   1317   1.1.1.7  christos       _bfd_error_handler
   1318       1.1     skrll 	/* xgettext:c-format */
   1319       1.1     skrll 	(_("%pB: recognised but unhandled machine type (0x%x)"
   1320       1.1     skrll 	   " in Import Library Format archive"),
   1321       1.1     skrll 	 abfd, machine);
   1322       1.1     skrll       bfd_set_error (bfd_error_wrong_format);
   1323       1.1     skrll 
   1324       1.1     skrll       return NULL;
   1325       1.1     skrll     }
   1326       1.1     skrll 
   1327       1.1     skrll   /* We do not bother to check the date.
   1328       1.1     skrll      date = H_GET_32 (abfd, ptr);  */
   1329       1.1     skrll   ptr += 4;
   1330       1.1     skrll 
   1331       1.1     skrll   size = H_GET_32 (abfd, ptr);
   1332       1.1     skrll   ptr += 4;
   1333       1.1     skrll 
   1334       1.1     skrll   if (size == 0)
   1335   1.1.1.7  christos     {
   1336       1.1     skrll       _bfd_error_handler
   1337       1.1     skrll 	(_("%pB: size field is zero in Import Library Format header"), abfd);
   1338       1.1     skrll       bfd_set_error (bfd_error_malformed_archive);
   1339       1.1     skrll 
   1340       1.1     skrll       return NULL;
   1341       1.1     skrll     }
   1342       1.1     skrll 
   1343       1.1     skrll   ordinal = H_GET_16 (abfd, ptr);
   1344       1.1     skrll   ptr += 2;
   1345       1.1     skrll 
   1346       1.1     skrll   types = H_GET_16 (abfd, ptr);
   1347       1.1     skrll   /* ptr += 2; */
   1348   1.1.1.9  christos 
   1349       1.1     skrll   /* Now read in the two strings that follow.  */
   1350       1.1     skrll   ptr = (bfd_byte *) _bfd_alloc_and_read (abfd, size, size);
   1351       1.1     skrll   if (ptr == NULL)
   1352       1.1     skrll     return NULL;
   1353   1.1.1.6  christos 
   1354   1.1.1.6  christos   symbol_name = (char *) ptr;
   1355       1.1     skrll   /* See PR 20905 for an example of where the strnlen is necessary.  */
   1356       1.1     skrll   source_dll  = symbol_name + strnlen (symbol_name, size - 1) + 1;
   1357       1.1     skrll 
   1358       1.1     skrll   /* Verify that the strings are null terminated.  */
   1359       1.1     skrll   if (ptr[size - 1] != 0
   1360       1.1     skrll       || (bfd_size_type) ((bfd_byte *) source_dll - ptr) >= size)
   1361   1.1.1.7  christos     {
   1362       1.1     skrll       _bfd_error_handler
   1363       1.1     skrll 	(_("%pB: string not null terminated in ILF object file"), abfd);
   1364       1.1     skrll       bfd_set_error (bfd_error_malformed_archive);
   1365       1.1     skrll       bfd_release (abfd, ptr);
   1366       1.1     skrll       return NULL;
   1367  1.1.1.11  christos     }
   1368  1.1.1.11  christos 
   1369  1.1.1.11  christos   /* An ILF file may contain a third string, after source_dll; this is
   1370  1.1.1.11  christos      used for IMPORT_NAME_EXPORTAS. We know from above that the whole
   1371  1.1.1.11  christos      block of data is null terminated, ptr[size-1]==0, but we don't
   1372  1.1.1.11  christos      know how many individual null terminated strings we have in there.
   1373  1.1.1.11  christos 
   1374  1.1.1.11  christos      First find the end of source_dll.  */
   1375  1.1.1.11  christos   import_name = source_dll + strlen (source_dll) + 1;
   1376  1.1.1.11  christos   if ((bfd_byte *) import_name >= ptr + size)
   1377  1.1.1.11  christos     {
   1378  1.1.1.11  christos       /* If this points at the end of the ptr+size block, we only had
   1379  1.1.1.11  christos 	 two strings. */
   1380  1.1.1.11  christos       import_name = NULL;
   1381       1.1     skrll     }
   1382       1.1     skrll 
   1383  1.1.1.11  christos   /* Now construct the bfd.  */
   1384  1.1.1.11  christos   if (! pe_ILF_build_a_bfd (abfd, magic, symbol_name,
   1385       1.1     skrll 			    source_dll, ordinal, types,
   1386       1.1     skrll 			    import_name))
   1387       1.1     skrll     {
   1388       1.1     skrll       bfd_release (abfd, ptr);
   1389       1.1     skrll       return NULL;
   1390  1.1.1.10  christos     }
   1391       1.1     skrll 
   1392       1.1     skrll   return pe_ILF_cleanup;
   1393   1.1.1.4  christos }
   1394   1.1.1.6  christos 
   1395   1.1.1.4  christos static void
   1396   1.1.1.4  christos pe_bfd_read_buildid (bfd *abfd)
   1397   1.1.1.4  christos {
   1398   1.1.1.4  christos   pe_data_type *pe = pe_data (abfd);
   1399   1.1.1.4  christos   struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
   1400   1.1.1.4  christos   asection *section;
   1401   1.1.1.4  christos   bfd_byte *data = 0;
   1402   1.1.1.4  christos   bfd_size_type dataoff;
   1403   1.1.1.4  christos   unsigned int i;
   1404   1.1.1.4  christos   bfd_vma addr = extra->DataDirectory[PE_DEBUG_DATA].VirtualAddress;
   1405   1.1.1.4  christos   bfd_size_type size = extra->DataDirectory[PE_DEBUG_DATA].Size;
   1406   1.1.1.4  christos 
   1407   1.1.1.4  christos   if (size == 0)
   1408   1.1.1.4  christos     return;
   1409   1.1.1.4  christos 
   1410   1.1.1.6  christos   addr += extra->ImageBase;
   1411   1.1.1.4  christos 
   1412   1.1.1.4  christos   /* Search for the section containing the DebugDirectory.  */
   1413   1.1.1.4  christos   for (section = abfd->sections; section != NULL; section = section->next)
   1414   1.1.1.6  christos     {
   1415   1.1.1.4  christos       if ((addr >= section->vma) && (addr < (section->vma + section->size)))
   1416   1.1.1.4  christos 	break;
   1417   1.1.1.4  christos     }
   1418   1.1.1.6  christos 
   1419   1.1.1.6  christos   if (section == NULL)
   1420   1.1.1.6  christos     return;
   1421   1.1.1.6  christos 
   1422   1.1.1.6  christos   if (!(section->flags & SEC_HAS_CONTENTS))
   1423   1.1.1.6  christos     return;
   1424   1.1.1.6  christos 
   1425   1.1.1.6  christos   dataoff = addr - section->vma;
   1426   1.1.1.6  christos 
   1427   1.1.1.6  christos   /* PR 20605 and 22373: Make sure that the data is really there.
   1428   1.1.1.6  christos      Note - since we are dealing with unsigned quantities we have
   1429   1.1.1.6  christos      to be careful to check for potential overflows.  */
   1430   1.1.1.4  christos   if (dataoff >= section->size
   1431   1.1.1.7  christos       || size > section->size - dataoff)
   1432   1.1.1.7  christos     {
   1433   1.1.1.7  christos       _bfd_error_handler
   1434   1.1.1.4  christos 	(_("%pB: error: debug data ends beyond end of debug directory"),
   1435   1.1.1.4  christos 	 abfd);
   1436   1.1.1.4  christos       return;
   1437   1.1.1.4  christos     }
   1438   1.1.1.4  christos 
   1439   1.1.1.4  christos   /* Read the whole section. */
   1440   1.1.1.9  christos   if (!bfd_malloc_and_get_section (abfd, section, &data))
   1441   1.1.1.4  christos     {
   1442   1.1.1.4  christos       free (data);
   1443   1.1.1.4  christos       return;
   1444   1.1.1.4  christos     }
   1445   1.1.1.4  christos 
   1446   1.1.1.4  christos   /* Search for a CodeView entry in the DebugDirectory */
   1447   1.1.1.4  christos   for (i = 0; i < size / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
   1448   1.1.1.4  christos     {
   1449   1.1.1.4  christos       struct external_IMAGE_DEBUG_DIRECTORY *ext
   1450   1.1.1.4  christos 	= &((struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff))[i];
   1451   1.1.1.4  christos       struct internal_IMAGE_DEBUG_DIRECTORY idd;
   1452   1.1.1.4  christos 
   1453   1.1.1.4  christos       _bfd_XXi_swap_debugdir_in (abfd, ext, &idd);
   1454   1.1.1.6  christos 
   1455   1.1.1.6  christos       if (idd.Type == PE_IMAGE_DEBUG_TYPE_CODEVIEW)
   1456   1.1.1.6  christos 	{
   1457   1.1.1.6  christos 	  char buffer[256 + 1];
   1458   1.1.1.6  christos 	  CODEVIEW_INFO *cvinfo = (CODEVIEW_INFO *) buffer;
   1459   1.1.1.6  christos 
   1460   1.1.1.6  christos 	  /*
   1461   1.1.1.6  christos 	    The debug entry doesn't have to have to be in a section, in which
   1462   1.1.1.6  christos 	    case AddressOfRawData is 0, so always use PointerToRawData.
   1463   1.1.1.6  christos 	  */
   1464  1.1.1.10  christos 	  if (_bfd_XXi_slurp_codeview_record (abfd,
   1465   1.1.1.6  christos 					      (file_ptr) idd.PointerToRawData,
   1466  1.1.1.11  christos 					      idd.SizeOfData, cvinfo, NULL))
   1467  1.1.1.11  christos 	    {
   1468  1.1.1.11  christos 	      struct bfd_build_id *build_id;
   1469  1.1.1.11  christos 	      size_t bidlen = sizeof (*build_id) + cvinfo->SignatureLength;
   1470   1.1.1.6  christos 
   1471   1.1.1.6  christos 	      build_id = bfd_alloc (abfd, bidlen);
   1472   1.1.1.6  christos 	      if (build_id)
   1473  1.1.1.11  christos 		{
   1474   1.1.1.6  christos 		  build_id->size = cvinfo->SignatureLength;
   1475   1.1.1.6  christos 		  memcpy(build_id->data, cvinfo->Signature,
   1476   1.1.1.6  christos 			 cvinfo->SignatureLength);
   1477   1.1.1.6  christos 		  abfd->build_id = build_id;
   1478   1.1.1.6  christos 		}
   1479   1.1.1.6  christos 	    }
   1480   1.1.1.4  christos 	  break;
   1481   1.1.1.8  christos 	}
   1482   1.1.1.8  christos     }
   1483   1.1.1.4  christos 
   1484   1.1.1.4  christos   free (data);
   1485   1.1.1.9  christos }
   1486  1.1.1.11  christos 
   1487       1.1     skrll static bfd_cleanup
   1488   1.1.1.4  christos pe_bfd_object_p (bfd *abfd)
   1489   1.1.1.7  christos {
   1490       1.1     skrll   bfd_byte buffer[6];
   1491   1.1.1.4  christos   struct external_DOS_hdr dos_hdr;
   1492   1.1.1.4  christos   struct external_PEI_IMAGE_hdr image_hdr;
   1493   1.1.1.9  christos   struct internal_filehdr internal_f;
   1494       1.1     skrll   struct internal_aouthdr internal_a;
   1495   1.1.1.9  christos   bfd_size_type opt_hdr_size;
   1496       1.1     skrll   file_ptr offset;
   1497       1.1     skrll   bfd_cleanup result;
   1498   1.1.1.4  christos 
   1499  1.1.1.10  christos   /* Detect if this a Microsoft Import Library Format element.  */
   1500  1.1.1.10  christos   /* First read the beginning of the header.  */
   1501       1.1     skrll   if (bfd_seek (abfd, 0, SEEK_SET) != 0
   1502       1.1     skrll       || bfd_read (buffer, 6, abfd) != 6)
   1503       1.1     skrll     {
   1504       1.1     skrll       if (bfd_get_error () != bfd_error_system_call)
   1505       1.1     skrll 	bfd_set_error (bfd_error_wrong_format);
   1506       1.1     skrll       return NULL;
   1507   1.1.1.4  christos     }
   1508   1.1.1.4  christos 
   1509   1.1.1.4  christos   /* Then check the magic and the version (only 0 is supported).  */
   1510       1.1     skrll   if (H_GET_32 (abfd, buffer) == 0xffff0000
   1511       1.1     skrll       && H_GET_16 (abfd, buffer + 4) == 0)
   1512  1.1.1.10  christos     return pe_ILF_object_p (abfd);
   1513  1.1.1.10  christos 
   1514       1.1     skrll   if (bfd_seek (abfd, 0, SEEK_SET) != 0
   1515       1.1     skrll       || bfd_read (&dos_hdr, sizeof (dos_hdr), abfd) != sizeof (dos_hdr))
   1516       1.1     skrll     {
   1517       1.1     skrll       if (bfd_get_error () != bfd_error_system_call)
   1518       1.1     skrll 	bfd_set_error (bfd_error_wrong_format);
   1519       1.1     skrll       return NULL;
   1520       1.1     skrll     }
   1521       1.1     skrll 
   1522   1.1.1.7  christos   /* There are really two magic numbers involved; the magic number
   1523       1.1     skrll      that says this is a NT executable (PEI) and the magic number that
   1524       1.1     skrll      determines the architecture.  The former is IMAGE_DOS_SIGNATURE, stored in
   1525       1.1     skrll      the e_magic field.  The latter is stored in the f_magic field.
   1526       1.1     skrll      If the NT magic number isn't valid, the architecture magic number
   1527       1.1     skrll      could be mimicked by some other field (specifically, the number
   1528       1.1     skrll      of relocs in section 3).  Since this routine can only be called
   1529       1.1     skrll      correctly for a PEI file, check the e_magic number here, and, if
   1530   1.1.1.7  christos      it doesn't match, clobber the f_magic number so that we don't get
   1531       1.1     skrll      a false match.  */
   1532       1.1     skrll   if (H_GET_16 (abfd, dos_hdr.e_magic) != IMAGE_DOS_SIGNATURE)
   1533       1.1     skrll     {
   1534       1.1     skrll       bfd_set_error (bfd_error_wrong_format);
   1535       1.1     skrll       return NULL;
   1536       1.1     skrll     }
   1537       1.1     skrll 
   1538  1.1.1.10  christos   offset = H_GET_32 (abfd, dos_hdr.e_lfanew);
   1539       1.1     skrll   if (bfd_seek (abfd, offset, SEEK_SET) != 0
   1540       1.1     skrll       || bfd_read (&image_hdr, sizeof (image_hdr), abfd) != sizeof (image_hdr))
   1541       1.1     skrll     {
   1542       1.1     skrll       if (bfd_get_error () != bfd_error_system_call)
   1543       1.1     skrll 	bfd_set_error (bfd_error_wrong_format);
   1544       1.1     skrll       return NULL;
   1545       1.1     skrll     }
   1546       1.1     skrll 
   1547       1.1     skrll   if (H_GET_32 (abfd, image_hdr.nt_signature) != 0x4550)
   1548       1.1     skrll     {
   1549       1.1     skrll       bfd_set_error (bfd_error_wrong_format);
   1550       1.1     skrll       return NULL;
   1551   1.1.1.4  christos     }
   1552   1.1.1.4  christos 
   1553   1.1.1.4  christos   /* Swap file header, so that we get the location for calling
   1554   1.1.1.4  christos      real_object_p.  */
   1555   1.1.1.4  christos   bfd_coff_swap_filehdr_in (abfd, &image_hdr, &internal_f);
   1556   1.1.1.4  christos 
   1557       1.1     skrll   if (! bfd_coff_bad_format_hook (abfd, &internal_f)
   1558   1.1.1.4  christos       || internal_f.f_opthdr > bfd_coff_aoutsz (abfd))
   1559       1.1     skrll     {
   1560       1.1     skrll       bfd_set_error (bfd_error_wrong_format);
   1561       1.1     skrll       return NULL;
   1562   1.1.1.8  christos     }
   1563   1.1.1.8  christos 
   1564   1.1.1.8  christos   memcpy (internal_f.pe.dos_message, dos_hdr.dos_message,
   1565   1.1.1.4  christos 	  sizeof (internal_f.pe.dos_message));
   1566   1.1.1.4  christos 
   1567   1.1.1.4  christos   /* Read the optional header, which has variable size.  */
   1568   1.1.1.4  christos   opt_hdr_size = internal_f.f_opthdr;
   1569   1.1.1.4  christos 
   1570   1.1.1.4  christos   if (opt_hdr_size != 0)
   1571  1.1.1.11  christos     {
   1572   1.1.1.4  christos       bfd_size_type amt = opt_hdr_size;
   1573   1.1.1.4  christos       bfd_byte *opthdr;
   1574   1.1.1.4  christos 
   1575   1.1.1.4  christos       /* PR 17521 file: 230-131433-0.004.  */
   1576   1.1.1.4  christos       if (amt < sizeof (PEAOUTHDR))
   1577   1.1.1.9  christos 	amt = sizeof (PEAOUTHDR);
   1578   1.1.1.4  christos 
   1579   1.1.1.4  christos       opthdr = _bfd_alloc_and_read (abfd, amt, opt_hdr_size);
   1580   1.1.1.9  christos       if (opthdr == NULL)
   1581   1.1.1.9  christos 	return NULL;
   1582   1.1.1.4  christos       if (amt > opt_hdr_size)
   1583  1.1.1.10  christos 	memset (opthdr + opt_hdr_size, 0, amt - opt_hdr_size);
   1584   1.1.1.4  christos 
   1585  1.1.1.10  christos       bfd_coff_swap_aouthdr_in (abfd, opthdr, &internal_a);
   1586  1.1.1.10  christos 
   1587  1.1.1.10  christos       struct internal_extra_pe_aouthdr *a = &internal_a.pe;
   1588  1.1.1.10  christos 
   1589  1.1.1.10  christos #ifdef ARM
   1590  1.1.1.10  christos       /* Use Subsystem to distinguish between pei-arm-little and
   1591  1.1.1.10  christos 	 pei-arm-wince-little.  */
   1592  1.1.1.10  christos #ifdef WINCE
   1593  1.1.1.10  christos       if (a->Subsystem != IMAGE_SUBSYSTEM_WINDOWS_CE_GUI)
   1594  1.1.1.10  christos #else
   1595  1.1.1.10  christos       if (a->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CE_GUI)
   1596  1.1.1.10  christos #endif
   1597  1.1.1.10  christos 	{
   1598  1.1.1.10  christos 	  bfd_set_error (bfd_error_wrong_format);
   1599  1.1.1.10  christos 	  return NULL;
   1600  1.1.1.10  christos 	}
   1601  1.1.1.10  christos #endif
   1602  1.1.1.10  christos 
   1603  1.1.1.10  christos       if ((a->SectionAlignment & -a->SectionAlignment) != a->SectionAlignment
   1604  1.1.1.10  christos 	  || a->SectionAlignment >= 0x80000000)
   1605  1.1.1.11  christos 	{
   1606  1.1.1.10  christos 	  _bfd_error_handler (_("%pB: adjusting invalid SectionAlignment"),
   1607  1.1.1.10  christos 			      abfd);
   1608  1.1.1.10  christos 	  a->SectionAlignment &= -a->SectionAlignment;
   1609  1.1.1.10  christos 	  if (a->SectionAlignment >= 0x80000000)
   1610  1.1.1.10  christos 	    a->SectionAlignment = 0x40000000;
   1611  1.1.1.10  christos 	}
   1612  1.1.1.10  christos 
   1613  1.1.1.10  christos       if ((a->FileAlignment & -a->FileAlignment) != a->FileAlignment
   1614  1.1.1.10  christos 	  || a->FileAlignment > a->SectionAlignment)
   1615  1.1.1.10  christos 	{
   1616  1.1.1.10  christos 	  _bfd_error_handler (_("%pB: adjusting invalid FileAlignment"),
   1617  1.1.1.10  christos 			      abfd);
   1618  1.1.1.10  christos 	  a->FileAlignment &= -a->FileAlignment;
   1619  1.1.1.10  christos 	  if (a->FileAlignment > a->SectionAlignment)
   1620  1.1.1.10  christos 	    a->FileAlignment = a->SectionAlignment;
   1621  1.1.1.10  christos 	}
   1622  1.1.1.10  christos 
   1623  1.1.1.10  christos       if (a->NumberOfRvaAndSizes > IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
   1624   1.1.1.4  christos 	_bfd_error_handler (_("%pB: invalid NumberOfRvaAndSizes"), abfd);
   1625   1.1.1.4  christos     }
   1626   1.1.1.6  christos 
   1627   1.1.1.6  christos   result = coff_real_object_p (abfd, internal_f.f_nscns, &internal_f,
   1628   1.1.1.6  christos 			       (opt_hdr_size != 0
   1629   1.1.1.4  christos 				? &internal_a
   1630   1.1.1.4  christos 				: (struct internal_aouthdr *) NULL));
   1631   1.1.1.4  christos 
   1632   1.1.1.4  christos   if (result)
   1633   1.1.1.4  christos     {
   1634   1.1.1.4  christos       /* Now the whole header has been processed, see if there is a build-id */
   1635   1.1.1.4  christos       pe_bfd_read_buildid(abfd);
   1636   1.1.1.4  christos     }
   1637       1.1     skrll 
   1638       1.1     skrll   return result;
   1639       1.1     skrll }
   1640       1.1     skrll 
   1641                     #define coff_object_p pe_bfd_object_p
   1642                     #endif /* COFF_IMAGE_WITH_PE */
   1643