peicode.h revision 1.1.1.6 1 /* Support for the generic parts of PE/PEI, for BFD.
2 Copyright (C) 1995-2019 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 (_("%pB: unhandled import type; %x"),
778 abfd, import_type);
779 return FALSE;
780
781 default:
782 /* xgettext:c-format */
783 _bfd_error_handler (_("%pB: unrecognized 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 (_("%pB: unrecognized 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_R3000:
1173 case IMAGE_FILE_MACHINE_R4000:
1174 case IMAGE_FILE_MACHINE_R10000:
1175
1176 case IMAGE_FILE_MACHINE_MIPS16:
1177 case IMAGE_FILE_MACHINE_MIPSFPU:
1178 case IMAGE_FILE_MACHINE_MIPSFPU16:
1179 #ifdef MIPS_ARCH_MAGIC_WINCE
1180 magic = MIPS_ARCH_MAGIC_WINCE;
1181 #endif
1182 break;
1183
1184 case IMAGE_FILE_MACHINE_SH3:
1185 case IMAGE_FILE_MACHINE_SH4:
1186 #ifdef SH_ARCH_MAGIC_WINCE
1187 magic = SH_ARCH_MAGIC_WINCE;
1188 #endif
1189 break;
1190
1191 case IMAGE_FILE_MACHINE_ARM:
1192 #ifdef ARMPEMAGIC
1193 magic = ARMPEMAGIC;
1194 #endif
1195 break;
1196
1197 case IMAGE_FILE_MACHINE_THUMB:
1198 #ifdef THUMBPEMAGIC
1199 {
1200 extern const bfd_target TARGET_LITTLE_SYM;
1201
1202 if (abfd->xvec == & TARGET_LITTLE_SYM)
1203 magic = THUMBPEMAGIC;
1204 }
1205 #endif
1206 break;
1207
1208 case IMAGE_FILE_MACHINE_POWERPC:
1209 /* We no longer support PowerPC. */
1210 default:
1211 _bfd_error_handler
1212 /* xgettext:c-format */
1213 (_("%pB: unrecognised machine type (0x%x)"
1214 " in Import Library Format archive"),
1215 abfd, machine);
1216 bfd_set_error (bfd_error_malformed_archive);
1217
1218 return NULL;
1219 break;
1220 }
1221
1222 if (magic == 0)
1223 {
1224 _bfd_error_handler
1225 /* xgettext:c-format */
1226 (_("%pB: recognised but unhandled machine type (0x%x)"
1227 " in Import Library Format archive"),
1228 abfd, machine);
1229 bfd_set_error (bfd_error_wrong_format);
1230
1231 return NULL;
1232 }
1233
1234 /* We do not bother to check the date.
1235 date = H_GET_32 (abfd, ptr); */
1236 ptr += 4;
1237
1238 size = H_GET_32 (abfd, ptr);
1239 ptr += 4;
1240
1241 if (size == 0)
1242 {
1243 _bfd_error_handler
1244 (_("%pB: size field is zero in Import Library Format header"), abfd);
1245 bfd_set_error (bfd_error_malformed_archive);
1246
1247 return NULL;
1248 }
1249
1250 ordinal = H_GET_16 (abfd, ptr);
1251 ptr += 2;
1252
1253 types = H_GET_16 (abfd, ptr);
1254 /* ptr += 2; */
1255
1256 /* Now read in the two strings that follow. */
1257 ptr = (bfd_byte *) bfd_alloc (abfd, size);
1258 if (ptr == NULL)
1259 return NULL;
1260
1261 if (bfd_bread (ptr, size, abfd) != size)
1262 {
1263 bfd_release (abfd, ptr);
1264 return NULL;
1265 }
1266
1267 symbol_name = (char *) ptr;
1268 /* See PR 20905 for an example of where the strnlen is necessary. */
1269 source_dll = symbol_name + strnlen (symbol_name, size - 1) + 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 (_("%pB: 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
1293 static void
1294 pe_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 bfd_vma addr = extra->DataDirectory[PE_DEBUG_DATA].VirtualAddress;
1303 bfd_size_type size = extra->DataDirectory[PE_DEBUG_DATA].Size;
1304
1305 if (size == 0)
1306 return;
1307
1308 addr += extra->ImageBase;
1309
1310 /* Search for the section containing the DebugDirectory. */
1311 for (section = abfd->sections; section != NULL; section = section->next)
1312 {
1313 if ((addr >= section->vma) && (addr < (section->vma + section->size)))
1314 break;
1315 }
1316
1317 if (section == NULL)
1318 return;
1319
1320 if (!(section->flags & SEC_HAS_CONTENTS))
1321 return;
1322
1323 dataoff = addr - section->vma;
1324
1325 /* PR 20605 and 22373: Make sure that the data is really there.
1326 Note - since we are dealing with unsigned quantities we have
1327 to be careful to check for potential overflows. */
1328 if (dataoff >= section->size
1329 || size > section->size - dataoff)
1330 {
1331 _bfd_error_handler
1332 (_("%pB: error: debug data ends beyond end of debug directory"),
1333 abfd);
1334 return;
1335 }
1336
1337 /* Read the whole section. */
1338 if (!bfd_malloc_and_get_section (abfd, section, &data))
1339 {
1340 if (data != NULL)
1341 free (data);
1342 return;
1343 }
1344
1345 /* Search for a CodeView entry in the DebugDirectory */
1346 for (i = 0; i < size / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
1347 {
1348 struct external_IMAGE_DEBUG_DIRECTORY *ext
1349 = &((struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff))[i];
1350 struct internal_IMAGE_DEBUG_DIRECTORY idd;
1351
1352 _bfd_XXi_swap_debugdir_in (abfd, ext, &idd);
1353
1354 if (idd.Type == PE_IMAGE_DEBUG_TYPE_CODEVIEW)
1355 {
1356 char buffer[256 + 1];
1357 CODEVIEW_INFO *cvinfo = (CODEVIEW_INFO *) buffer;
1358
1359 /*
1360 The debug entry doesn't have to have to be in a section, in which
1361 case AddressOfRawData is 0, so always use PointerToRawData.
1362 */
1363 if (_bfd_XXi_slurp_codeview_record (abfd,
1364 (file_ptr) idd.PointerToRawData,
1365 idd.SizeOfData, cvinfo))
1366 {
1367 struct bfd_build_id* build_id = bfd_alloc (abfd,
1368 sizeof (struct bfd_build_id) + cvinfo->SignatureLength);
1369 if (build_id)
1370 {
1371 build_id->size = cvinfo->SignatureLength;
1372 memcpy(build_id->data, cvinfo->Signature,
1373 cvinfo->SignatureLength);
1374 abfd->build_id = build_id;
1375 }
1376 }
1377 break;
1378 }
1379 }
1380 }
1381
1382 static const bfd_target *
1383 pe_bfd_object_p (bfd * abfd)
1384 {
1385 bfd_byte buffer[6];
1386 struct external_DOS_hdr dos_hdr;
1387 struct external_PEI_IMAGE_hdr image_hdr;
1388 struct internal_filehdr internal_f;
1389 struct internal_aouthdr internal_a;
1390 file_ptr opt_hdr_size;
1391 file_ptr offset;
1392 const bfd_target *result;
1393
1394 /* Detect if this a Microsoft Import Library Format element. */
1395 /* First read the beginning of the header. */
1396 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1397 || bfd_bread (buffer, (bfd_size_type) 6, abfd) != 6)
1398 {
1399 if (bfd_get_error () != bfd_error_system_call)
1400 bfd_set_error (bfd_error_wrong_format);
1401 return NULL;
1402 }
1403
1404 /* Then check the magic and the version (only 0 is supported). */
1405 if (H_GET_32 (abfd, buffer) == 0xffff0000
1406 && H_GET_16 (abfd, buffer + 4) == 0)
1407 return pe_ILF_object_p (abfd);
1408
1409 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1410 || bfd_bread (&dos_hdr, (bfd_size_type) sizeof (dos_hdr), abfd)
1411 != sizeof (dos_hdr))
1412 {
1413 if (bfd_get_error () != bfd_error_system_call)
1414 bfd_set_error (bfd_error_wrong_format);
1415 return NULL;
1416 }
1417
1418 /* There are really two magic numbers involved; the magic number
1419 that says this is a NT executable (PEI) and the magic number that
1420 determines the architecture. The former is IMAGE_DOS_SIGNATURE, stored in
1421 the e_magic field. The latter is stored in the f_magic field.
1422 If the NT magic number isn't valid, the architecture magic number
1423 could be mimicked by some other field (specifically, the number
1424 of relocs in section 3). Since this routine can only be called
1425 correctly for a PEI file, check the e_magic number here, and, if
1426 it doesn't match, clobber the f_magic number so that we don't get
1427 a false match. */
1428 if (H_GET_16 (abfd, dos_hdr.e_magic) != IMAGE_DOS_SIGNATURE)
1429 {
1430 bfd_set_error (bfd_error_wrong_format);
1431 return NULL;
1432 }
1433
1434 offset = H_GET_32 (abfd, dos_hdr.e_lfanew);
1435 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1436 || (bfd_bread (&image_hdr, (bfd_size_type) sizeof (image_hdr), abfd)
1437 != sizeof (image_hdr)))
1438 {
1439 if (bfd_get_error () != bfd_error_system_call)
1440 bfd_set_error (bfd_error_wrong_format);
1441 return NULL;
1442 }
1443
1444 if (H_GET_32 (abfd, image_hdr.nt_signature) != 0x4550)
1445 {
1446 bfd_set_error (bfd_error_wrong_format);
1447 return NULL;
1448 }
1449
1450 /* Swap file header, so that we get the location for calling
1451 real_object_p. */
1452 bfd_coff_swap_filehdr_in (abfd, &image_hdr, &internal_f);
1453
1454 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
1455 || internal_f.f_opthdr > bfd_coff_aoutsz (abfd))
1456 {
1457 bfd_set_error (bfd_error_wrong_format);
1458 return NULL;
1459 }
1460
1461 /* Read the optional header, which has variable size. */
1462 opt_hdr_size = internal_f.f_opthdr;
1463
1464 if (opt_hdr_size != 0)
1465 {
1466 bfd_size_type amt = opt_hdr_size;
1467 void * opthdr;
1468
1469 /* PR 17521 file: 230-131433-0.004. */
1470 if (amt < sizeof (PEAOUTHDR))
1471 amt = sizeof (PEAOUTHDR);
1472
1473 opthdr = bfd_zalloc (abfd, amt);
1474 if (opthdr == NULL)
1475 return NULL;
1476 if (bfd_bread (opthdr, opt_hdr_size, abfd)
1477 != (bfd_size_type) opt_hdr_size)
1478 return NULL;
1479
1480 bfd_set_error (bfd_error_no_error);
1481 bfd_coff_swap_aouthdr_in (abfd, opthdr, & internal_a);
1482 if (bfd_get_error () != bfd_error_no_error)
1483 return NULL;
1484 }
1485
1486
1487 result = coff_real_object_p (abfd, internal_f.f_nscns, &internal_f,
1488 (opt_hdr_size != 0
1489 ? &internal_a
1490 : (struct internal_aouthdr *) NULL));
1491
1492
1493 if (result)
1494 {
1495 /* Now the whole header has been processed, see if there is a build-id */
1496 pe_bfd_read_buildid(abfd);
1497 }
1498
1499 return result;
1500 }
1501
1502 #define coff_object_p pe_bfd_object_p
1503 #endif /* COFF_IMAGE_WITH_PE */
1504