peicode.h revision 1.1.1.5 1 /* Support for the generic parts of PE/PEI, for BFD.
2 Copyright (C) 1995-2017 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 # 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
675 typedef struct
676 {
677 unsigned short magic;
678 unsigned char data[MAX_TEXT_SECTION_SIZE];
679 unsigned int size;
680 unsigned int offset;
681 }
682 jump_table;
683
684 static 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
747 static bfd_boolean
748 pe_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 /* xgettext:c-format */
777 _bfd_error_handler (_("%B: Unhandled import type; %x"),
778 abfd, import_type);
779 return FALSE;
780
781 default:
782 /* xgettext:c-format */
783 _bfd_error_handler (_("%B: Unrecognised import type; %x"),
784 abfd, import_type);
785 return FALSE;
786 }
787
788 switch (import_name_type)
789 {
790 case IMPORT_ORDINAL:
791 case IMPORT_NAME:
792 case IMPORT_NAME_NOPREFIX:
793 case IMPORT_NAME_UNDECORATE:
794 break;
795
796 default:
797 /* xgettext:c-format */
798 _bfd_error_handler (_("%B: Unrecognised import name type; %x"),
799 abfd, import_name_type);
800 return FALSE;
801 }
802
803 /* Initialise local variables.
804
805 Note these are kept in a structure rather than being
806 declared as statics since bfd frowns on global variables.
807
808 We are going to construct the contents of the BFD in memory,
809 so allocate all the space that we will need right now. */
810 vars.bim
811 = (struct bfd_in_memory *) bfd_malloc ((bfd_size_type) sizeof (*vars.bim));
812 if (vars.bim == NULL)
813 return FALSE;
814
815 ptr = (bfd_byte *) bfd_zmalloc ((bfd_size_type) ILF_DATA_SIZE);
816 vars.bim->buffer = ptr;
817 vars.bim->size = ILF_DATA_SIZE;
818 if (ptr == NULL)
819 goto error_return;
820
821 /* Initialise the pointers to regions of the memory and the
822 other contents of the pe_ILF_vars structure as well. */
823 vars.sym_cache = (coff_symbol_type *) ptr;
824 vars.sym_ptr = (coff_symbol_type *) ptr;
825 vars.sym_index = 0;
826 ptr += SIZEOF_ILF_SYMS;
827
828 vars.sym_table = (unsigned int *) ptr;
829 vars.table_ptr = (unsigned int *) ptr;
830 ptr += SIZEOF_ILF_SYM_TABLE;
831
832 vars.native_syms = (combined_entry_type *) ptr;
833 vars.native_ptr = (combined_entry_type *) ptr;
834 ptr += SIZEOF_ILF_NATIVE_SYMS;
835
836 vars.sym_ptr_table = (coff_symbol_type **) ptr;
837 vars.sym_ptr_ptr = (coff_symbol_type **) ptr;
838 ptr += SIZEOF_ILF_SYM_PTR_TABLE;
839
840 vars.esym_table = (SYMENT *) ptr;
841 vars.esym_ptr = (SYMENT *) ptr;
842 ptr += SIZEOF_ILF_EXT_SYMS;
843
844 vars.reltab = (arelent *) ptr;
845 vars.relcount = 0;
846 ptr += SIZEOF_ILF_RELOCS;
847
848 vars.int_reltab = (struct internal_reloc *) ptr;
849 ptr += SIZEOF_ILF_INT_RELOCS;
850
851 vars.string_table = (char *) ptr;
852 vars.string_ptr = (char *) ptr + STRING_SIZE_SIZE;
853 ptr += SIZEOF_ILF_STRINGS;
854 vars.end_string_ptr = (char *) ptr;
855
856 /* The remaining space in bim->buffer is used
857 by the pe_ILF_make_a_section() function. */
858 # if (GCC_VERSION >= 3000)
859 /* PR 18758: Make sure that the data area is sufficiently aligned for
860 pointers on the host. __alignof__ is a gcc extension, hence the test
861 above. For other compilers we will have to assume that the alignment is
862 unimportant, or else extra code can be added here and in
863 pe_ILF_make_a_section.
864
865 Note - we cannot test 'ptr' directly as it is illegal to perform binary
866 arithmetic on pointers, but we know that the strings section is the only
867 one that might end on an unaligned boundary. */
868 {
869 unsigned int alignment = __alignof__ (char *);
870
871 if (SIZEOF_ILF_STRINGS & (alignment - 1))
872 ptr += alignment - (SIZEOF_ILF_STRINGS & (alignment - 1));
873 }
874 #endif
875
876 vars.data = ptr;
877 vars.abfd = abfd;
878 vars.sec_index = 0;
879 vars.magic = magic;
880
881 /* Create the initial .idata$<n> sections:
882 [.idata$2: Import Directory Table -- not needed]
883 .idata$4: Import Lookup Table
884 .idata$5: Import Address Table
885
886 Note we do not create a .idata$3 section as this is
887 created for us by the linker script. */
888 id4 = pe_ILF_make_a_section (& vars, ".idata$4", SIZEOF_IDATA4, 0);
889 id5 = pe_ILF_make_a_section (& vars, ".idata$5", SIZEOF_IDATA5, 0);
890 if (id4 == NULL || id5 == NULL)
891 goto error_return;
892
893 /* Fill in the contents of these sections. */
894 if (import_name_type == IMPORT_ORDINAL)
895 {
896 if (ordinal == 0)
897 /* See PR 20907 for a reproducer. */
898 goto error_return;
899
900 #ifdef COFF_WITH_pex64
901 ((unsigned int *) id4->contents)[0] = ordinal;
902 ((unsigned int *) id4->contents)[1] = 0x80000000;
903 ((unsigned int *) id5->contents)[0] = ordinal;
904 ((unsigned int *) id5->contents)[1] = 0x80000000;
905 #else
906 * (unsigned int *) id4->contents = ordinal | 0x80000000;
907 * (unsigned int *) id5->contents = ordinal | 0x80000000;
908 #endif
909 }
910 else
911 {
912 char * symbol;
913 unsigned int len;
914
915 /* Create .idata$6 - the Hint Name Table. */
916 id6 = pe_ILF_make_a_section (& vars, ".idata$6", SIZEOF_IDATA6, 0);
917 if (id6 == NULL)
918 goto error_return;
919
920 /* If necessary, trim the import symbol name. */
921 symbol = symbol_name;
922
923 /* As used by MS compiler, '_', '@', and '?' are alternative
924 forms of USER_LABEL_PREFIX, with '?' for c++ mangled names,
925 '@' used for fastcall (in C), '_' everywhere else. Only one
926 of these is used for a symbol. We strip this leading char for
927 IMPORT_NAME_NOPREFIX and IMPORT_NAME_UNDECORATE as per the
928 PE COFF 6.0 spec (section 8.3, Import Name Type). */
929
930 if (import_name_type != IMPORT_NAME)
931 {
932 char c = symbol[0];
933
934 /* Check that we don't remove for targets with empty
935 USER_LABEL_PREFIX the leading underscore. */
936 if ((c == '_' && abfd->xvec->symbol_leading_char != 0)
937 || c == '@' || c == '?')
938 symbol++;
939 }
940
941 len = strlen (symbol);
942 if (import_name_type == IMPORT_NAME_UNDECORATE)
943 {
944 /* Truncate at the first '@'. */
945 char *at = strchr (symbol, '@');
946
947 if (at != NULL)
948 len = at - symbol;
949 }
950
951 id6->contents[0] = ordinal & 0xff;
952 id6->contents[1] = ordinal >> 8;
953
954 memcpy ((char *) id6->contents + 2, symbol, len);
955 id6->contents[len + 2] = '\0';
956 }
957
958 if (import_name_type != IMPORT_ORDINAL)
959 {
960 pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
961 pe_ILF_save_relocs (&vars, id4);
962
963 pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
964 pe_ILF_save_relocs (&vars, id5);
965 }
966
967 /* Create an import symbol. */
968 pe_ILF_make_a_symbol (& vars, "__imp_", symbol_name, id5, 0);
969 imp_sym = vars.sym_ptr_ptr - 1;
970 imp_index = vars.sym_index - 1;
971
972 /* Create extra sections depending upon the type of import we are dealing with. */
973 switch (import_type)
974 {
975 int i;
976
977 case IMPORT_CODE:
978 /* CODE functions are special, in that they get a trampoline that
979 jumps to the main import symbol. Create a .text section to hold it.
980 First we need to look up its contents in the jump table. */
981 for (i = NUM_ENTRIES (jtab); i--;)
982 {
983 if (jtab[i].size == 0)
984 continue;
985 if (jtab[i].magic == magic)
986 break;
987 }
988 /* If we did not find a matching entry something is wrong. */
989 if (i < 0)
990 abort ();
991
992 /* Create the .text section. */
993 text = pe_ILF_make_a_section (& vars, ".text", jtab[i].size, SEC_CODE);
994 if (text == NULL)
995 goto error_return;
996
997 /* Copy in the jump code. */
998 memcpy (text->contents, jtab[i].data, jtab[i].size);
999
1000 /* Create a reloc for the data in the text section. */
1001 #ifdef MIPS_ARCH_MAGIC_WINCE
1002 if (magic == MIPS_ARCH_MAGIC_WINCE)
1003 {
1004 pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) 0, BFD_RELOC_HI16_S,
1005 (struct bfd_symbol **) imp_sym,
1006 imp_index);
1007 pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_LO16, text);
1008 pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) 4, BFD_RELOC_LO16,
1009 (struct bfd_symbol **) imp_sym,
1010 imp_index);
1011 }
1012 else
1013 #endif
1014 #ifdef AMD64MAGIC
1015 if (magic == AMD64MAGIC)
1016 {
1017 pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) jtab[i].offset,
1018 BFD_RELOC_32_PCREL, (asymbol **) imp_sym,
1019 imp_index);
1020 }
1021 else
1022 #endif
1023 pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) jtab[i].offset,
1024 BFD_RELOC_32, (asymbol **) imp_sym,
1025 imp_index);
1026
1027 pe_ILF_save_relocs (& vars, text);
1028 break;
1029
1030 case IMPORT_DATA:
1031 break;
1032
1033 default:
1034 /* XXX code not yet written. */
1035 abort ();
1036 }
1037
1038 /* Initialise the bfd. */
1039 memset (& internal_f, 0, sizeof (internal_f));
1040
1041 internal_f.f_magic = magic;
1042 internal_f.f_symptr = 0;
1043 internal_f.f_nsyms = 0;
1044 internal_f.f_flags = F_AR32WR | F_LNNO; /* XXX is this correct ? */
1045
1046 if ( ! bfd_set_start_address (abfd, (bfd_vma) 0)
1047 || ! bfd_coff_set_arch_mach_hook (abfd, & internal_f))
1048 goto error_return;
1049
1050 if (bfd_coff_mkobject_hook (abfd, (void *) & internal_f, NULL) == NULL)
1051 goto error_return;
1052
1053 coff_data (abfd)->pe = 1;
1054 #ifdef THUMBPEMAGIC
1055 if (vars.magic == THUMBPEMAGIC)
1056 /* Stop some linker warnings about thumb code not supporting interworking. */
1057 coff_data (abfd)->flags |= F_INTERWORK | F_INTERWORK_SET;
1058 #endif
1059
1060 /* Switch from file contents to memory contents. */
1061 bfd_cache_close (abfd);
1062
1063 abfd->iostream = (void *) vars.bim;
1064 abfd->flags |= BFD_IN_MEMORY /* | HAS_LOCALS */;
1065 abfd->iovec = &_bfd_memory_iovec;
1066 abfd->where = 0;
1067 abfd->origin = 0;
1068 obj_sym_filepos (abfd) = 0;
1069
1070 /* Now create a symbol describing the imported value. */
1071 switch (import_type)
1072 {
1073 case IMPORT_CODE:
1074 pe_ILF_make_a_symbol (& vars, "", symbol_name, text,
1075 BSF_NOT_AT_END | BSF_FUNCTION);
1076
1077 break;
1078
1079 case IMPORT_DATA:
1080 /* Nothing to do here. */
1081 break;
1082
1083 default:
1084 /* XXX code not yet written. */
1085 abort ();
1086 }
1087
1088 /* Create an import symbol for the DLL, without the .dll suffix. */
1089 ptr = (bfd_byte *) strrchr (source_dll, '.');
1090 if (ptr)
1091 * ptr = 0;
1092 pe_ILF_make_a_symbol (& vars, "__IMPORT_DESCRIPTOR_", source_dll, NULL, 0);
1093 if (ptr)
1094 * ptr = '.';
1095
1096 /* Point the bfd at the symbol table. */
1097 obj_symbols (abfd) = vars.sym_cache;
1098 bfd_get_symcount (abfd) = vars.sym_index;
1099
1100 obj_raw_syments (abfd) = vars.native_syms;
1101 obj_raw_syment_count (abfd) = vars.sym_index;
1102
1103 obj_coff_external_syms (abfd) = (void *) vars.esym_table;
1104 obj_coff_keep_syms (abfd) = TRUE;
1105
1106 obj_convert (abfd) = vars.sym_table;
1107 obj_conv_table_size (abfd) = vars.sym_index;
1108
1109 obj_coff_strings (abfd) = vars.string_table;
1110 obj_coff_keep_strings (abfd) = TRUE;
1111
1112 abfd->flags |= HAS_SYMS;
1113
1114 return TRUE;
1115
1116 error_return:
1117 if (vars.bim->buffer != NULL)
1118 free (vars.bim->buffer);
1119 free (vars.bim);
1120 return FALSE;
1121 }
1122
1123 /* We have detected a Image Library Format archive element.
1124 Decode the element and return the appropriate target. */
1125
1126 static const bfd_target *
1127 pe_ILF_object_p (bfd * abfd)
1128 {
1129 bfd_byte buffer[14];
1130 bfd_byte * ptr;
1131 char * symbol_name;
1132 char * source_dll;
1133 unsigned int machine;
1134 bfd_size_type size;
1135 unsigned int ordinal;
1136 unsigned int types;
1137 unsigned int magic;
1138
1139 /* Upon entry the first six bytes of the ILF header have
1140 already been read. Now read the rest of the header. */
1141 if (bfd_bread (buffer, (bfd_size_type) 14, abfd) != 14)
1142 return NULL;
1143
1144 ptr = buffer;
1145
1146 machine = H_GET_16 (abfd, ptr);
1147 ptr += 2;
1148
1149 /* Check that the machine type is recognised. */
1150 magic = 0;
1151
1152 switch (machine)
1153 {
1154 case IMAGE_FILE_MACHINE_UNKNOWN:
1155 case IMAGE_FILE_MACHINE_ALPHA:
1156 case IMAGE_FILE_MACHINE_ALPHA64:
1157 case IMAGE_FILE_MACHINE_IA64:
1158 break;
1159
1160 case IMAGE_FILE_MACHINE_I386:
1161 #ifdef I386MAGIC
1162 magic = I386MAGIC;
1163 #endif
1164 break;
1165
1166 case IMAGE_FILE_MACHINE_AMD64:
1167 #ifdef AMD64MAGIC
1168 magic = AMD64MAGIC;
1169 #endif
1170 break;
1171
1172 case IMAGE_FILE_MACHINE_M68K:
1173 #ifdef MC68AGIC
1174 magic = MC68MAGIC;
1175 #endif
1176 break;
1177
1178 case IMAGE_FILE_MACHINE_R3000:
1179 case IMAGE_FILE_MACHINE_R4000:
1180 case IMAGE_FILE_MACHINE_R10000:
1181
1182 case IMAGE_FILE_MACHINE_MIPS16:
1183 case IMAGE_FILE_MACHINE_MIPSFPU:
1184 case IMAGE_FILE_MACHINE_MIPSFPU16:
1185 #ifdef MIPS_ARCH_MAGIC_WINCE
1186 magic = MIPS_ARCH_MAGIC_WINCE;
1187 #endif
1188 break;
1189
1190 case IMAGE_FILE_MACHINE_SH3:
1191 case IMAGE_FILE_MACHINE_SH4:
1192 #ifdef SH_ARCH_MAGIC_WINCE
1193 magic = SH_ARCH_MAGIC_WINCE;
1194 #endif
1195 break;
1196
1197 case IMAGE_FILE_MACHINE_ARM:
1198 #ifdef ARMPEMAGIC
1199 magic = ARMPEMAGIC;
1200 #endif
1201 break;
1202
1203 case IMAGE_FILE_MACHINE_THUMB:
1204 #ifdef THUMBPEMAGIC
1205 {
1206 extern const bfd_target TARGET_LITTLE_SYM;
1207
1208 if (abfd->xvec == & TARGET_LITTLE_SYM)
1209 magic = THUMBPEMAGIC;
1210 }
1211 #endif
1212 break;
1213
1214 case IMAGE_FILE_MACHINE_POWERPC:
1215 /* We no longer support PowerPC. */
1216 default:
1217 _bfd_error_handler
1218 /* xgettext:c-format */
1219 (_("%B: Unrecognised machine type (0x%x)"
1220 " in Import Library Format archive"),
1221 abfd, machine);
1222 bfd_set_error (bfd_error_malformed_archive);
1223
1224 return NULL;
1225 break;
1226 }
1227
1228 if (magic == 0)
1229 {
1230 _bfd_error_handler
1231 /* xgettext:c-format */
1232 (_("%B: Recognised but unhandled machine type (0x%x)"
1233 " in Import Library Format archive"),
1234 abfd, machine);
1235 bfd_set_error (bfd_error_wrong_format);
1236
1237 return NULL;
1238 }
1239
1240 /* We do not bother to check the date.
1241 date = H_GET_32 (abfd, ptr); */
1242 ptr += 4;
1243
1244 size = H_GET_32 (abfd, ptr);
1245 ptr += 4;
1246
1247 if (size == 0)
1248 {
1249 _bfd_error_handler
1250 (_("%B: size field is zero in Import Library Format header"), abfd);
1251 bfd_set_error (bfd_error_malformed_archive);
1252
1253 return NULL;
1254 }
1255
1256 ordinal = H_GET_16 (abfd, ptr);
1257 ptr += 2;
1258
1259 types = H_GET_16 (abfd, ptr);
1260 /* ptr += 2; */
1261
1262 /* Now read in the two strings that follow. */
1263 ptr = (bfd_byte *) bfd_alloc (abfd, size);
1264 if (ptr == NULL)
1265 return NULL;
1266
1267 if (bfd_bread (ptr, size, abfd) != size)
1268 {
1269 bfd_release (abfd, ptr);
1270 return NULL;
1271 }
1272
1273 symbol_name = (char *) ptr;
1274 /* See PR 20905 for an example of where the strnlen is necessary. */
1275 source_dll = symbol_name + strnlen (symbol_name, size - 1) + 1;
1276
1277 /* Verify that the strings are null terminated. */
1278 if (ptr[size - 1] != 0
1279 || (bfd_size_type) ((bfd_byte *) source_dll - ptr) >= size)
1280 {
1281 _bfd_error_handler
1282 (_("%B: string not null terminated in ILF object file."), abfd);
1283 bfd_set_error (bfd_error_malformed_archive);
1284 bfd_release (abfd, ptr);
1285 return NULL;
1286 }
1287
1288 /* Now construct the bfd. */
1289 if (! pe_ILF_build_a_bfd (abfd, magic, symbol_name,
1290 source_dll, ordinal, types))
1291 {
1292 bfd_release (abfd, ptr);
1293 return NULL;
1294 }
1295
1296 return abfd->xvec;
1297 }
1298
1299 static void
1300 pe_bfd_read_buildid (bfd *abfd)
1301 {
1302 pe_data_type *pe = pe_data (abfd);
1303 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1304 asection *section;
1305 bfd_byte *data = 0;
1306 bfd_size_type dataoff;
1307 unsigned int i;
1308
1309 bfd_vma addr = extra->DataDirectory[PE_DEBUG_DATA].VirtualAddress;
1310 bfd_size_type size = extra->DataDirectory[PE_DEBUG_DATA].Size;
1311
1312 if (size == 0)
1313 return;
1314
1315 addr += extra->ImageBase;
1316
1317 /* Search for the section containing the DebugDirectory. */
1318 for (section = abfd->sections; section != NULL; section = section->next)
1319 {
1320 if ((addr >= section->vma) && (addr < (section->vma + section->size)))
1321 break;
1322 }
1323
1324 if (section == NULL)
1325 return;
1326
1327 if (!(section->flags & SEC_HAS_CONTENTS))
1328 return;
1329
1330 dataoff = addr - section->vma;
1331
1332 /* PR 20605: Make sure that the data is really there. */
1333 if (dataoff + size > section->size)
1334 {
1335 _bfd_error_handler (_("%B: Error: Debug Data ends beyond end of debug directory."),
1336 abfd);
1337 return;
1338 }
1339
1340 /* Read the whole section. */
1341 if (!bfd_malloc_and_get_section (abfd, section, &data))
1342 {
1343 if (data != NULL)
1344 free (data);
1345 return;
1346 }
1347
1348 /* Search for a CodeView entry in the DebugDirectory */
1349 for (i = 0; i < size / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
1350 {
1351 struct external_IMAGE_DEBUG_DIRECTORY *ext
1352 = &((struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff))[i];
1353 struct internal_IMAGE_DEBUG_DIRECTORY idd;
1354
1355 _bfd_XXi_swap_debugdir_in (abfd, ext, &idd);
1356
1357 if (idd.Type == PE_IMAGE_DEBUG_TYPE_CODEVIEW)
1358 {
1359 char buffer[256 + 1];
1360 CODEVIEW_INFO *cvinfo = (CODEVIEW_INFO *) buffer;
1361
1362 /*
1363 The debug entry doesn't have to have to be in a section, in which
1364 case AddressOfRawData is 0, so always use PointerToRawData.
1365 */
1366 if (_bfd_XXi_slurp_codeview_record (abfd,
1367 (file_ptr) idd.PointerToRawData,
1368 idd.SizeOfData, cvinfo))
1369 {
1370 struct bfd_build_id* build_id = bfd_alloc (abfd,
1371 sizeof (struct bfd_build_id) + cvinfo->SignatureLength);
1372 if (build_id)
1373 {
1374 build_id->size = cvinfo->SignatureLength;
1375 memcpy(build_id->data, cvinfo->Signature,
1376 cvinfo->SignatureLength);
1377 abfd->build_id = build_id;
1378 }
1379 }
1380 break;
1381 }
1382 }
1383 }
1384
1385 static const bfd_target *
1386 pe_bfd_object_p (bfd * abfd)
1387 {
1388 bfd_byte buffer[6];
1389 struct external_PEI_DOS_hdr dos_hdr;
1390 struct external_PEI_IMAGE_hdr image_hdr;
1391 struct internal_filehdr internal_f;
1392 struct internal_aouthdr internal_a;
1393 file_ptr opt_hdr_size;
1394 file_ptr offset;
1395 const bfd_target *result;
1396
1397 /* Detect if this a Microsoft Import Library Format element. */
1398 /* First read the beginning of the header. */
1399 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1400 || bfd_bread (buffer, (bfd_size_type) 6, abfd) != 6)
1401 {
1402 if (bfd_get_error () != bfd_error_system_call)
1403 bfd_set_error (bfd_error_wrong_format);
1404 return NULL;
1405 }
1406
1407 /* Then check the magic and the version (only 0 is supported). */
1408 if (H_GET_32 (abfd, buffer) == 0xffff0000
1409 && H_GET_16 (abfd, buffer + 4) == 0)
1410 return pe_ILF_object_p (abfd);
1411
1412 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1413 || bfd_bread (&dos_hdr, (bfd_size_type) sizeof (dos_hdr), abfd)
1414 != sizeof (dos_hdr))
1415 {
1416 if (bfd_get_error () != bfd_error_system_call)
1417 bfd_set_error (bfd_error_wrong_format);
1418 return NULL;
1419 }
1420
1421 /* There are really two magic numbers involved; the magic number
1422 that says this is a NT executable (PEI) and the magic number that
1423 determines the architecture. The former is DOSMAGIC, stored in
1424 the e_magic field. The latter is stored in the f_magic field.
1425 If the NT magic number isn't valid, the architecture magic number
1426 could be mimicked by some other field (specifically, the number
1427 of relocs in section 3). Since this routine can only be called
1428 correctly for a PEI file, check the e_magic number here, and, if
1429 it doesn't match, clobber the f_magic number so that we don't get
1430 a false match. */
1431 if (H_GET_16 (abfd, dos_hdr.e_magic) != DOSMAGIC)
1432 {
1433 bfd_set_error (bfd_error_wrong_format);
1434 return NULL;
1435 }
1436
1437 offset = H_GET_32 (abfd, dos_hdr.e_lfanew);
1438 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1439 || (bfd_bread (&image_hdr, (bfd_size_type) sizeof (image_hdr), abfd)
1440 != sizeof (image_hdr)))
1441 {
1442 if (bfd_get_error () != bfd_error_system_call)
1443 bfd_set_error (bfd_error_wrong_format);
1444 return NULL;
1445 }
1446
1447 if (H_GET_32 (abfd, image_hdr.nt_signature) != 0x4550)
1448 {
1449 bfd_set_error (bfd_error_wrong_format);
1450 return NULL;
1451 }
1452
1453 /* Swap file header, so that we get the location for calling
1454 real_object_p. */
1455 bfd_coff_swap_filehdr_in (abfd, &image_hdr, &internal_f);
1456
1457 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
1458 || internal_f.f_opthdr > bfd_coff_aoutsz (abfd))
1459 {
1460 bfd_set_error (bfd_error_wrong_format);
1461 return NULL;
1462 }
1463
1464 /* Read the optional header, which has variable size. */
1465 opt_hdr_size = internal_f.f_opthdr;
1466
1467 if (opt_hdr_size != 0)
1468 {
1469 bfd_size_type amt = opt_hdr_size;
1470 void * opthdr;
1471
1472 /* PR 17521 file: 230-131433-0.004. */
1473 if (amt < sizeof (PEAOUTHDR))
1474 amt = sizeof (PEAOUTHDR);
1475
1476 opthdr = bfd_zalloc (abfd, amt);
1477 if (opthdr == NULL)
1478 return NULL;
1479 if (bfd_bread (opthdr, opt_hdr_size, abfd)
1480 != (bfd_size_type) opt_hdr_size)
1481 return NULL;
1482
1483 bfd_set_error (bfd_error_no_error);
1484 bfd_coff_swap_aouthdr_in (abfd, opthdr, & internal_a);
1485 if (bfd_get_error () != bfd_error_no_error)
1486 return NULL;
1487 }
1488
1489
1490 result = coff_real_object_p (abfd, internal_f.f_nscns, &internal_f,
1491 (opt_hdr_size != 0
1492 ? &internal_a
1493 : (struct internal_aouthdr *) NULL));
1494
1495
1496 if (result)
1497 {
1498 /* Now the whole header has been processed, see if there is a build-id */
1499 pe_bfd_read_buildid(abfd);
1500 }
1501
1502 return result;
1503 }
1504
1505 #define coff_object_p pe_bfd_object_p
1506 #endif /* COFF_IMAGE_WITH_PE */
1507