source: binutils/vendor/current/bfd/peicode.h@ 1968

Last change on this file since 1968 was 1968, checked in by Silvan Scherrer, 8 years ago

binutils: update vendor to version 2.27. some files are missing, due to a svn bug with @ in the name. But as it's only in the testsuite I don't care atm

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