peicode.h revision 1.1.1.3 1 /* Support for the generic parts of PE/PEI, for BFD.
2 Copyright (C) 1995-2015 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 (at) 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
59 static 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
67 static bfd_boolean pe_print_private_bfd_data (bfd *, void *);
68 #define coff_bfd_print_private_bfd_data pe_print_private_bfd_data
69
70 static 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
78 static 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. */
86 typedef asection * asection_ptr;
87
88 typedef 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 }
122 pe_ILF_vars;
123 #endif /* COFF_IMAGE_WITH_PE */
124
125 const bfd_target *coff_real_object_p
126 (bfd *, unsigned, struct internal_filehdr *, struct internal_aouthdr *);
127
128 #ifndef NO_COFF_RELOCS
130 static void
131 coff_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
144 static unsigned int
145 coff_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
169 static void
170 coff_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
203 static void
204 coff_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
257 static bfd_boolean
258 pe_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
281 static void *
282 pe_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
332 static bfd_boolean
333 pe_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
351 static bfd_boolean
352 pe_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 /* 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
453 static void
454 pe_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
482 static void
483 pe_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
494 static void
495 pe_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
519 static void
520 pe_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
598 static asection_ptr
599 pe_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 /* Create a coff_section_tdata structure for our use. */
637 sec->used_by_bfd = (struct coff_section_tdata *) vars->data;
638 vars->data += sizeof (struct coff_section_tdata);
639
640 BFD_ASSERT (vars->data <= vars->bim->buffer + vars->bim->size);
641
642 /* Create a symbol to refer to this section. */
643 pe_ILF_make_a_symbol (vars, "", name, sec, BSF_LOCAL);
644
645 /* Cache the index to the symbol in the coff_section_data structure. */
646 coff_section_data (vars->abfd, sec)->i = vars->sym_index - 1;
647
648 return sec;
649 }
650
651 /* This structure contains the code that goes into the .text section
652 in order to perform a jump into the DLL lookup table. The entries
653 in the table are index by the magic number used to represent the
654 machine type in the PE file. The contents of the data[] arrays in
655 these entries are stolen from the jtab[] arrays in ld/pe-dll.c.
656 The SIZE field says how many bytes in the DATA array are actually
657 used. The OFFSET field says where in the data array the address
658 of the .idata$5 section should be placed. */
659 #define MAX_TEXT_SECTION_SIZE 32
660
661 typedef struct
662 {
663 unsigned short magic;
664 unsigned char data[MAX_TEXT_SECTION_SIZE];
665 unsigned int size;
666 unsigned int offset;
667 }
668 jump_table;
669
670 static jump_table jtab[] =
671 {
672 #ifdef I386MAGIC
673 { I386MAGIC,
674 { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 },
675 8, 2
676 },
677 #endif
678
679 #ifdef AMD64MAGIC
680 { AMD64MAGIC,
681 { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 },
682 8, 2
683 },
684 #endif
685
686 #ifdef MC68MAGIC
687 { MC68MAGIC,
688 { /* XXX fill me in */ },
689 0, 0
690 },
691 #endif
692
693 #ifdef MIPS_ARCH_MAGIC_WINCE
694 { MIPS_ARCH_MAGIC_WINCE,
695 { 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
696 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 },
697 16, 0
698 },
699 #endif
700
701 #ifdef SH_ARCH_MAGIC_WINCE
702 { SH_ARCH_MAGIC_WINCE,
703 { 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40,
704 0x09, 0x00, 0x00, 0x00, 0x00, 0x00 },
705 12, 8
706 },
707 #endif
708
709 #ifdef ARMPEMAGIC
710 { ARMPEMAGIC,
711 { 0x00, 0xc0, 0x9f, 0xe5, 0x00, 0xf0,
712 0x9c, 0xe5, 0x00, 0x00, 0x00, 0x00},
713 12, 8
714 },
715 #endif
716
717 #ifdef THUMBPEMAGIC
718 { THUMBPEMAGIC,
719 { 0x40, 0xb4, 0x02, 0x4e, 0x36, 0x68, 0xb4, 0x46,
720 0x40, 0xbc, 0x60, 0x47, 0x00, 0x00, 0x00, 0x00 },
721 16, 12
722 },
723 #endif
724 { 0, { 0 }, 0, 0 }
725 };
726
727 #ifndef NUM_ENTRIES
728 #define NUM_ENTRIES(a) (sizeof (a) / sizeof (a)[0])
729 #endif
730
731 /* Build a full BFD from the information supplied in a ILF object. */
732
733 static bfd_boolean
734 pe_ILF_build_a_bfd (bfd * abfd,
735 unsigned int magic,
736 char * symbol_name,
737 char * source_dll,
738 unsigned int ordinal,
739 unsigned int types)
740 {
741 bfd_byte * ptr;
742 pe_ILF_vars vars;
743 struct internal_filehdr internal_f;
744 unsigned int import_type;
745 unsigned int import_name_type;
746 asection_ptr id4, id5, id6 = NULL, text = NULL;
747 coff_symbol_type ** imp_sym;
748 unsigned int imp_index;
749
750 /* Decode and verify the types field of the ILF structure. */
751 import_type = types & 0x3;
752 import_name_type = (types & 0x1c) >> 2;
753
754 switch (import_type)
755 {
756 case IMPORT_CODE:
757 case IMPORT_DATA:
758 break;
759
760 case IMPORT_CONST:
761 /* XXX code yet to be written. */
762 _bfd_error_handler (_("%B: Unhandled import type; %x"),
763 abfd, import_type);
764 return FALSE;
765
766 default:
767 _bfd_error_handler (_("%B: Unrecognised import type; %x"),
768 abfd, import_type);
769 return FALSE;
770 }
771
772 switch (import_name_type)
773 {
774 case IMPORT_ORDINAL:
775 case IMPORT_NAME:
776 case IMPORT_NAME_NOPREFIX:
777 case IMPORT_NAME_UNDECORATE:
778 break;
779
780 default:
781 _bfd_error_handler (_("%B: Unrecognised import name type; %x"),
782 abfd, import_name_type);
783 return FALSE;
784 }
785
786 /* Initialise local variables.
787
788 Note these are kept in a structure rather than being
789 declared as statics since bfd frowns on global variables.
790
791 We are going to construct the contents of the BFD in memory,
792 so allocate all the space that we will need right now. */
793 vars.bim
794 = (struct bfd_in_memory *) bfd_malloc ((bfd_size_type) sizeof (*vars.bim));
795 if (vars.bim == NULL)
796 return FALSE;
797
798 ptr = (bfd_byte *) bfd_zmalloc ((bfd_size_type) ILF_DATA_SIZE);
799 vars.bim->buffer = ptr;
800 vars.bim->size = ILF_DATA_SIZE;
801 if (ptr == NULL)
802 goto error_return;
803
804 /* Initialise the pointers to regions of the memory and the
805 other contents of the pe_ILF_vars structure as well. */
806 vars.sym_cache = (coff_symbol_type *) ptr;
807 vars.sym_ptr = (coff_symbol_type *) ptr;
808 vars.sym_index = 0;
809 ptr += SIZEOF_ILF_SYMS;
810
811 vars.sym_table = (unsigned int *) ptr;
812 vars.table_ptr = (unsigned int *) ptr;
813 ptr += SIZEOF_ILF_SYM_TABLE;
814
815 vars.native_syms = (combined_entry_type *) ptr;
816 vars.native_ptr = (combined_entry_type *) ptr;
817 ptr += SIZEOF_ILF_NATIVE_SYMS;
818
819 vars.sym_ptr_table = (coff_symbol_type **) ptr;
820 vars.sym_ptr_ptr = (coff_symbol_type **) ptr;
821 ptr += SIZEOF_ILF_SYM_PTR_TABLE;
822
823 vars.esym_table = (SYMENT *) ptr;
824 vars.esym_ptr = (SYMENT *) ptr;
825 ptr += SIZEOF_ILF_EXT_SYMS;
826
827 vars.reltab = (arelent *) ptr;
828 vars.relcount = 0;
829 ptr += SIZEOF_ILF_RELOCS;
830
831 vars.int_reltab = (struct internal_reloc *) ptr;
832 ptr += SIZEOF_ILF_INT_RELOCS;
833
834 vars.string_table = (char *) ptr;
835 vars.string_ptr = (char *) ptr + STRING_SIZE_SIZE;
836 ptr += SIZEOF_ILF_STRINGS;
837 vars.end_string_ptr = (char *) ptr;
838
839 /* The remaining space in bim->buffer is used
840 by the pe_ILF_make_a_section() function. */
841 vars.data = ptr;
842 vars.abfd = abfd;
843 vars.sec_index = 0;
844 vars.magic = magic;
845
846 /* Create the initial .idata$<n> sections:
847 [.idata$2: Import Directory Table -- not needed]
848 .idata$4: Import Lookup Table
849 .idata$5: Import Address Table
850
851 Note we do not create a .idata$3 section as this is
852 created for us by the linker script. */
853 id4 = pe_ILF_make_a_section (& vars, ".idata$4", SIZEOF_IDATA4, 0);
854 id5 = pe_ILF_make_a_section (& vars, ".idata$5", SIZEOF_IDATA5, 0);
855 if (id4 == NULL || id5 == NULL)
856 goto error_return;
857
858 /* Fill in the contents of these sections. */
859 if (import_name_type == IMPORT_ORDINAL)
860 {
861 if (ordinal == 0)
862 /* XXX - treat as IMPORT_NAME ??? */
863 abort ();
864
865 #ifdef COFF_WITH_pex64
866 ((unsigned int *) id4->contents)[0] = ordinal;
867 ((unsigned int *) id4->contents)[1] = 0x80000000;
868 ((unsigned int *) id5->contents)[0] = ordinal;
869 ((unsigned int *) id5->contents)[1] = 0x80000000;
870 #else
871 * (unsigned int *) id4->contents = ordinal | 0x80000000;
872 * (unsigned int *) id5->contents = ordinal | 0x80000000;
873 #endif
874 }
875 else
876 {
877 char * symbol;
878 unsigned int len;
879
880 /* Create .idata$6 - the Hint Name Table. */
881 id6 = pe_ILF_make_a_section (& vars, ".idata$6", SIZEOF_IDATA6, 0);
882 if (id6 == NULL)
883 goto error_return;
884
885 /* If necessary, trim the import symbol name. */
886 symbol = symbol_name;
887
888 /* As used by MS compiler, '_', '@', and '?' are alternative
889 forms of USER_LABEL_PREFIX, with '?' for c++ mangled names,
890 '@' used for fastcall (in C), '_' everywhere else. Only one
891 of these is used for a symbol. We strip this leading char for
892 IMPORT_NAME_NOPREFIX and IMPORT_NAME_UNDECORATE as per the
893 PE COFF 6.0 spec (section 8.3, Import Name Type). */
894
895 if (import_name_type != IMPORT_NAME)
896 {
897 char c = symbol[0];
898
899 /* Check that we don't remove for targets with empty
900 USER_LABEL_PREFIX the leading underscore. */
901 if ((c == '_' && abfd->xvec->symbol_leading_char != 0)
902 || c == '@' || c == '?')
903 symbol++;
904 }
905
906 len = strlen (symbol);
907 if (import_name_type == IMPORT_NAME_UNDECORATE)
908 {
909 /* Truncate at the first '@'. */
910 char *at = strchr (symbol, '@');
911
912 if (at != NULL)
913 len = at - symbol;
914 }
915
916 id6->contents[0] = ordinal & 0xff;
917 id6->contents[1] = ordinal >> 8;
918
919 memcpy ((char *) id6->contents + 2, symbol, len);
920 id6->contents[len + 2] = '\0';
921 }
922
923 if (import_name_type != IMPORT_ORDINAL)
924 {
925 pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
926 pe_ILF_save_relocs (&vars, id4);
927
928 pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
929 pe_ILF_save_relocs (&vars, id5);
930 }
931
932 /* Create extra sections depending upon the type of import we are dealing with. */
933 switch (import_type)
934 {
935 int i;
936
937 case IMPORT_CODE:
938 /* Create a .text section.
939 First we need to look up its contents in the jump table. */
940 for (i = NUM_ENTRIES (jtab); i--;)
941 {
942 if (jtab[i].size == 0)
943 continue;
944 if (jtab[i].magic == magic)
945 break;
946 }
947 /* If we did not find a matching entry something is wrong. */
948 if (i < 0)
949 abort ();
950
951 /* Create the .text section. */
952 text = pe_ILF_make_a_section (& vars, ".text", jtab[i].size, SEC_CODE);
953 if (text == NULL)
954 goto error_return;
955
956 /* Copy in the jump code. */
957 memcpy (text->contents, jtab[i].data, jtab[i].size);
958
959 /* Create an import symbol. */
960 pe_ILF_make_a_symbol (& vars, "__imp_", symbol_name, id5, 0);
961 imp_sym = vars.sym_ptr_ptr - 1;
962 imp_index = vars.sym_index - 1;
963
964 /* Create a reloc for the data in the text section. */
965 #ifdef MIPS_ARCH_MAGIC_WINCE
966 if (magic == MIPS_ARCH_MAGIC_WINCE)
967 {
968 pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) 0, BFD_RELOC_HI16_S,
969 (struct bfd_symbol **) imp_sym,
970 imp_index);
971 pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_LO16, text);
972 pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) 4, BFD_RELOC_LO16,
973 (struct bfd_symbol **) imp_sym,
974 imp_index);
975 }
976 else
977 #endif
978 #ifdef AMD64MAGIC
979 if (magic == AMD64MAGIC)
980 {
981 pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) jtab[i].offset,
982 BFD_RELOC_32_PCREL, (asymbol **) imp_sym,
983 imp_index);
984 }
985 else
986 #endif
987 pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) jtab[i].offset,
988 BFD_RELOC_32, (asymbol **) imp_sym,
989 imp_index);
990
991 pe_ILF_save_relocs (& vars, text);
992 break;
993
994 case IMPORT_DATA:
995 break;
996
997 default:
998 /* XXX code not yet written. */
999 abort ();
1000 }
1001
1002 /* Initialise the bfd. */
1003 memset (& internal_f, 0, sizeof (internal_f));
1004
1005 internal_f.f_magic = magic;
1006 internal_f.f_symptr = 0;
1007 internal_f.f_nsyms = 0;
1008 internal_f.f_flags = F_AR32WR | F_LNNO; /* XXX is this correct ? */
1009
1010 if ( ! bfd_set_start_address (abfd, (bfd_vma) 0)
1011 || ! bfd_coff_set_arch_mach_hook (abfd, & internal_f))
1012 goto error_return;
1013
1014 if (bfd_coff_mkobject_hook (abfd, (void *) & internal_f, NULL) == NULL)
1015 goto error_return;
1016
1017 coff_data (abfd)->pe = 1;
1018 #ifdef THUMBPEMAGIC
1019 if (vars.magic == THUMBPEMAGIC)
1020 /* Stop some linker warnings about thumb code not supporting interworking. */
1021 coff_data (abfd)->flags |= F_INTERWORK | F_INTERWORK_SET;
1022 #endif
1023
1024 /* Switch from file contents to memory contents. */
1025 bfd_cache_close (abfd);
1026
1027 abfd->iostream = (void *) vars.bim;
1028 abfd->flags |= BFD_IN_MEMORY /* | HAS_LOCALS */;
1029 abfd->iovec = &_bfd_memory_iovec;
1030 abfd->where = 0;
1031 abfd->origin = 0;
1032 obj_sym_filepos (abfd) = 0;
1033
1034 /* Now create a symbol describing the imported value. */
1035 switch (import_type)
1036 {
1037 case IMPORT_CODE:
1038 pe_ILF_make_a_symbol (& vars, "", symbol_name, text,
1039 BSF_NOT_AT_END | BSF_FUNCTION);
1040
1041 /* Create an import symbol for the DLL, without the
1042 .dll suffix. */
1043 ptr = (bfd_byte *) strrchr (source_dll, '.');
1044 if (ptr)
1045 * ptr = 0;
1046 pe_ILF_make_a_symbol (& vars, "__IMPORT_DESCRIPTOR_", source_dll, NULL, 0);
1047 if (ptr)
1048 * ptr = '.';
1049 break;
1050
1051 case IMPORT_DATA:
1052 /* Nothing to do here. */
1053 break;
1054
1055 default:
1056 /* XXX code not yet written. */
1057 abort ();
1058 }
1059
1060 /* Point the bfd at the symbol table. */
1061 obj_symbols (abfd) = vars.sym_cache;
1062 bfd_get_symcount (abfd) = vars.sym_index;
1063
1064 obj_raw_syments (abfd) = vars.native_syms;
1065 obj_raw_syment_count (abfd) = vars.sym_index;
1066
1067 obj_coff_external_syms (abfd) = (void *) vars.esym_table;
1068 obj_coff_keep_syms (abfd) = TRUE;
1069
1070 obj_convert (abfd) = vars.sym_table;
1071 obj_conv_table_size (abfd) = vars.sym_index;
1072
1073 obj_coff_strings (abfd) = vars.string_table;
1074 obj_coff_keep_strings (abfd) = TRUE;
1075
1076 abfd->flags |= HAS_SYMS;
1077
1078 return TRUE;
1079
1080 error_return:
1081 if (vars.bim->buffer != NULL)
1082 free (vars.bim->buffer);
1083 free (vars.bim);
1084 return FALSE;
1085 }
1086
1087 /* We have detected a Image Library Format archive element.
1088 Decode the element and return the appropriate target. */
1089
1090 static const bfd_target *
1091 pe_ILF_object_p (bfd * abfd)
1092 {
1093 bfd_byte buffer[14];
1094 bfd_byte * ptr;
1095 char * symbol_name;
1096 char * source_dll;
1097 unsigned int machine;
1098 bfd_size_type size;
1099 unsigned int ordinal;
1100 unsigned int types;
1101 unsigned int magic;
1102
1103 /* Upon entry the first six bytes of the ILF header have
1104 already been read. Now read the rest of the header. */
1105 if (bfd_bread (buffer, (bfd_size_type) 14, abfd) != 14)
1106 return NULL;
1107
1108 ptr = buffer;
1109
1110 machine = H_GET_16 (abfd, ptr);
1111 ptr += 2;
1112
1113 /* Check that the machine type is recognised. */
1114 magic = 0;
1115
1116 switch (machine)
1117 {
1118 case IMAGE_FILE_MACHINE_UNKNOWN:
1119 case IMAGE_FILE_MACHINE_ALPHA:
1120 case IMAGE_FILE_MACHINE_ALPHA64:
1121 case IMAGE_FILE_MACHINE_IA64:
1122 break;
1123
1124 case IMAGE_FILE_MACHINE_I386:
1125 #ifdef I386MAGIC
1126 magic = I386MAGIC;
1127 #endif
1128 break;
1129
1130 case IMAGE_FILE_MACHINE_AMD64:
1131 #ifdef AMD64MAGIC
1132 magic = AMD64MAGIC;
1133 #endif
1134 break;
1135
1136 case IMAGE_FILE_MACHINE_M68K:
1137 #ifdef MC68AGIC
1138 magic = MC68MAGIC;
1139 #endif
1140 break;
1141
1142 case IMAGE_FILE_MACHINE_R3000:
1143 case IMAGE_FILE_MACHINE_R4000:
1144 case IMAGE_FILE_MACHINE_R10000:
1145
1146 case IMAGE_FILE_MACHINE_MIPS16:
1147 case IMAGE_FILE_MACHINE_MIPSFPU:
1148 case IMAGE_FILE_MACHINE_MIPSFPU16:
1149 #ifdef MIPS_ARCH_MAGIC_WINCE
1150 magic = MIPS_ARCH_MAGIC_WINCE;
1151 #endif
1152 break;
1153
1154 case IMAGE_FILE_MACHINE_SH3:
1155 case IMAGE_FILE_MACHINE_SH4:
1156 #ifdef SH_ARCH_MAGIC_WINCE
1157 magic = SH_ARCH_MAGIC_WINCE;
1158 #endif
1159 break;
1160
1161 case IMAGE_FILE_MACHINE_ARM:
1162 #ifdef ARMPEMAGIC
1163 magic = ARMPEMAGIC;
1164 #endif
1165 break;
1166
1167 case IMAGE_FILE_MACHINE_THUMB:
1168 #ifdef THUMBPEMAGIC
1169 {
1170 extern const bfd_target TARGET_LITTLE_SYM;
1171
1172 if (abfd->xvec == & TARGET_LITTLE_SYM)
1173 magic = THUMBPEMAGIC;
1174 }
1175 #endif
1176 break;
1177
1178 case IMAGE_FILE_MACHINE_POWERPC:
1179 /* We no longer support PowerPC. */
1180 default:
1181 _bfd_error_handler
1182 (_("%B: Unrecognised machine type (0x%x)"
1183 " in Import Library Format archive"),
1184 abfd, machine);
1185 bfd_set_error (bfd_error_malformed_archive);
1186
1187 return NULL;
1188 break;
1189 }
1190
1191 if (magic == 0)
1192 {
1193 _bfd_error_handler
1194 (_("%B: Recognised but unhandled machine type (0x%x)"
1195 " in Import Library Format archive"),
1196 abfd, machine);
1197 bfd_set_error (bfd_error_wrong_format);
1198
1199 return NULL;
1200 }
1201
1202 /* We do not bother to check the date.
1203 date = H_GET_32 (abfd, ptr); */
1204 ptr += 4;
1205
1206 size = H_GET_32 (abfd, ptr);
1207 ptr += 4;
1208
1209 if (size == 0)
1210 {
1211 _bfd_error_handler
1212 (_("%B: size field is zero in Import Library Format header"), abfd);
1213 bfd_set_error (bfd_error_malformed_archive);
1214
1215 return NULL;
1216 }
1217
1218 ordinal = H_GET_16 (abfd, ptr);
1219 ptr += 2;
1220
1221 types = H_GET_16 (abfd, ptr);
1222 /* ptr += 2; */
1223
1224 /* Now read in the two strings that follow. */
1225 ptr = (bfd_byte *) bfd_alloc (abfd, size);
1226 if (ptr == NULL)
1227 return NULL;
1228
1229 if (bfd_bread (ptr, size, abfd) != size)
1230 {
1231 bfd_release (abfd, ptr);
1232 return NULL;
1233 }
1234
1235 symbol_name = (char *) ptr;
1236 source_dll = symbol_name + strlen (symbol_name) + 1;
1237
1238 /* Verify that the strings are null terminated. */
1239 if (ptr[size - 1] != 0
1240 || (bfd_size_type) ((bfd_byte *) source_dll - ptr) >= size)
1241 {
1242 _bfd_error_handler
1243 (_("%B: string not null terminated in ILF object file."), abfd);
1244 bfd_set_error (bfd_error_malformed_archive);
1245 bfd_release (abfd, ptr);
1246 return NULL;
1247 }
1248
1249 /* Now construct the bfd. */
1250 if (! pe_ILF_build_a_bfd (abfd, magic, symbol_name,
1251 source_dll, ordinal, types))
1252 {
1253 bfd_release (abfd, ptr);
1254 return NULL;
1255 }
1256
1257 return abfd->xvec;
1258 }
1259
1260 static void
1261 pe_bfd_read_buildid(bfd *abfd)
1262 {
1263 pe_data_type *pe = pe_data (abfd);
1264 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1265 asection *section;
1266 bfd_byte *data = 0;
1267 bfd_size_type dataoff;
1268 unsigned int i;
1269
1270 bfd_vma addr = extra->DataDirectory[PE_DEBUG_DATA].VirtualAddress;
1271 bfd_size_type size = extra->DataDirectory[PE_DEBUG_DATA].Size;
1272
1273 if (size == 0)
1274 return;
1275
1276 addr += extra->ImageBase;
1277
1278 /* Search for the section containing the DebugDirectory */
1279 for (section = abfd->sections; section != NULL; section = section->next)
1280 {
1281 if ((addr >= section->vma) && (addr < (section->vma + section->size)))
1282 break;
1283 }
1284
1285 if (section == NULL)
1286 {
1287 return;
1288 }
1289 else if (!(section->flags & SEC_HAS_CONTENTS))
1290 {
1291 return;
1292 }
1293
1294 dataoff = addr - section->vma;
1295
1296 /* Read the whole section. */
1297 if (!bfd_malloc_and_get_section (abfd, section, &data))
1298 {
1299 if (data != NULL)
1300 free (data);
1301 return;
1302 }
1303
1304 /* Search for a CodeView entry in the DebugDirectory */
1305 for (i = 0; i < size / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
1306 {
1307 struct external_IMAGE_DEBUG_DIRECTORY *ext
1308 = &((struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff))[i];
1309 struct internal_IMAGE_DEBUG_DIRECTORY idd;
1310
1311 _bfd_XXi_swap_debugdir_in (abfd, ext, &idd);
1312
1313 if (idd.Type == PE_IMAGE_DEBUG_TYPE_CODEVIEW)
1314 {
1315 char buffer[256 + 1];
1316 CODEVIEW_INFO *cvinfo = (CODEVIEW_INFO *) buffer;
1317
1318 /*
1319 The debug entry doesn't have to have to be in a section, in which
1320 case AddressOfRawData is 0, so always use PointerToRawData.
1321 */
1322 if (_bfd_XXi_slurp_codeview_record (abfd,
1323 (file_ptr) idd.PointerToRawData,
1324 idd.SizeOfData, cvinfo))
1325 {
1326 struct bfd_build_id* build_id = bfd_alloc(abfd,
1327 sizeof(struct bfd_build_id) + cvinfo->SignatureLength);
1328 if (build_id)
1329 {
1330 build_id->size = cvinfo->SignatureLength;
1331 memcpy(build_id->data, cvinfo->Signature,
1332 cvinfo->SignatureLength);
1333 abfd->build_id = build_id;
1334 }
1335 }
1336 break;
1337 }
1338 }
1339 }
1340
1341 static const bfd_target *
1342 pe_bfd_object_p (bfd * abfd)
1343 {
1344 bfd_byte buffer[6];
1345 struct external_PEI_DOS_hdr dos_hdr;
1346 struct external_PEI_IMAGE_hdr image_hdr;
1347 struct internal_filehdr internal_f;
1348 struct internal_aouthdr internal_a;
1349 file_ptr opt_hdr_size;
1350 file_ptr offset;
1351 const bfd_target *result;
1352
1353 /* Detect if this a Microsoft Import Library Format element. */
1354 /* First read the beginning of the header. */
1355 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1356 || bfd_bread (buffer, (bfd_size_type) 6, abfd) != 6)
1357 {
1358 if (bfd_get_error () != bfd_error_system_call)
1359 bfd_set_error (bfd_error_wrong_format);
1360 return NULL;
1361 }
1362
1363 /* Then check the magic and the version (only 0 is supported). */
1364 if (H_GET_32 (abfd, buffer) == 0xffff0000
1365 && H_GET_16 (abfd, buffer + 4) == 0)
1366 return pe_ILF_object_p (abfd);
1367
1368 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1369 || bfd_bread (&dos_hdr, (bfd_size_type) sizeof (dos_hdr), abfd)
1370 != sizeof (dos_hdr))
1371 {
1372 if (bfd_get_error () != bfd_error_system_call)
1373 bfd_set_error (bfd_error_wrong_format);
1374 return NULL;
1375 }
1376
1377 /* There are really two magic numbers involved; the magic number
1378 that says this is a NT executable (PEI) and the magic number that
1379 determines the architecture. The former is DOSMAGIC, stored in
1380 the e_magic field. The latter is stored in the f_magic field.
1381 If the NT magic number isn't valid, the architecture magic number
1382 could be mimicked by some other field (specifically, the number
1383 of relocs in section 3). Since this routine can only be called
1384 correctly for a PEI file, check the e_magic number here, and, if
1385 it doesn't match, clobber the f_magic number so that we don't get
1386 a false match. */
1387 if (H_GET_16 (abfd, dos_hdr.e_magic) != DOSMAGIC)
1388 {
1389 bfd_set_error (bfd_error_wrong_format);
1390 return NULL;
1391 }
1392
1393 offset = H_GET_32 (abfd, dos_hdr.e_lfanew);
1394 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1395 || (bfd_bread (&image_hdr, (bfd_size_type) sizeof (image_hdr), abfd)
1396 != sizeof (image_hdr)))
1397 {
1398 if (bfd_get_error () != bfd_error_system_call)
1399 bfd_set_error (bfd_error_wrong_format);
1400 return NULL;
1401 }
1402
1403 if (H_GET_32 (abfd, image_hdr.nt_signature) != 0x4550)
1404 {
1405 bfd_set_error (bfd_error_wrong_format);
1406 return NULL;
1407 }
1408
1409 /* Swap file header, so that we get the location for calling
1410 real_object_p. */
1411 bfd_coff_swap_filehdr_in (abfd, &image_hdr, &internal_f);
1412
1413 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
1414 || internal_f.f_opthdr > bfd_coff_aoutsz (abfd))
1415 {
1416 bfd_set_error (bfd_error_wrong_format);
1417 return NULL;
1418 }
1419
1420 /* Read the optional header, which has variable size. */
1421 opt_hdr_size = internal_f.f_opthdr;
1422
1423 if (opt_hdr_size != 0)
1424 {
1425 bfd_size_type amt = opt_hdr_size;
1426 void * opthdr;
1427
1428 /* PR 17521 file: 230-131433-0.004. */
1429 if (amt < sizeof (PEAOUTHDR))
1430 amt = sizeof (PEAOUTHDR);
1431
1432 opthdr = bfd_zalloc (abfd, amt);
1433 if (opthdr == NULL)
1434 return NULL;
1435 if (bfd_bread (opthdr, opt_hdr_size, abfd)
1436 != (bfd_size_type) opt_hdr_size)
1437 return NULL;
1438
1439 bfd_set_error (bfd_error_no_error);
1440 bfd_coff_swap_aouthdr_in (abfd, opthdr, & internal_a);
1441 if (bfd_get_error () != bfd_error_no_error)
1442 return NULL;
1443 }
1444
1445
1446 result = coff_real_object_p (abfd, internal_f.f_nscns, &internal_f,
1447 (opt_hdr_size != 0
1448 ? &internal_a
1449 : (struct internal_aouthdr *) NULL));
1450
1451
1452 if (result)
1453 {
1454 /* Now the whole header has been processed, see if there is a build-id */
1455 pe_bfd_read_buildid(abfd);
1456 }
1457
1458 return result;
1459 }
1460
1461 #define coff_object_p pe_bfd_object_p
1462 #endif /* COFF_IMAGE_WITH_PE */
1463