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