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