1 /* Support for the generic parts of PE/PEI, for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Written by Cygnus Solutions.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 Most of this hacked by Steve Chamberlain,
26 PE/PEI rearrangement (and code added): Donn Terry
30 /* Hey look, some documentation [and in a place you expect to find it]!
32 The main reference for the pei format is "Microsoft Portable Executable
33 and Common Object File Format Specification 4.1". Get it if you need to
34 do some serious hacking on this code.
37 "Peering Inside the PE: A Tour of the Win32 Portable Executable
38 File Format", MSJ 1994, Volume 9.
40 The *sole* difference between the pe format and the pei format is that the
41 latter has an MSDOS 2.0 .exe header on the front that prints the message
42 "This app must be run under Windows." (or some such).
43 (FIXME: Whether that statement is *really* true or not is unknown.
44 Are there more subtle differences between pe and pei formats?
45 For now assume there aren't. If you find one, then for God sakes
48 The Microsoft docs use the word "image" instead of "executable" because
49 the former can also refer to a DLL (shared library). Confusion can arise
50 because the `i' in `pei' also refers to "image". The `pe' format can
51 also create images (i.e. executables), it's just that to run on a win32
52 system you need to use the pei format.
54 FIXME: Please add more docs here so the next poor fool that has to hack
55 on this code has a chance of getting something accomplished without
56 wasting too much time.
61 static bfd_boolean (*pe_saved_coff_bfd_print_private_bfd_data
)
62 PARAMS ((bfd
*, PTR
)) =
63 #ifndef coff_bfd_print_private_bfd_data
66 coff_bfd_print_private_bfd_data
;
67 #undef coff_bfd_print_private_bfd_data
70 static bfd_boolean pe_print_private_bfd_data
PARAMS ((bfd
*, PTR
));
71 #define coff_bfd_print_private_bfd_data pe_print_private_bfd_data
73 static bfd_boolean (*pe_saved_coff_bfd_copy_private_bfd_data
)
74 PARAMS ((bfd
*, bfd
*)) =
75 #ifndef coff_bfd_copy_private_bfd_data
78 coff_bfd_copy_private_bfd_data
;
79 #undef coff_bfd_copy_private_bfd_data
82 static bfd_boolean pe_bfd_copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
83 #define coff_bfd_copy_private_bfd_data pe_bfd_copy_private_bfd_data
85 #define coff_mkobject pe_mkobject
86 #define coff_mkobject_hook pe_mkobject_hook
88 #ifndef NO_COFF_RELOCS
89 static void coff_swap_reloc_in
PARAMS ((bfd
*, PTR
, PTR
));
90 static unsigned int coff_swap_reloc_out
PARAMS ((bfd
*, PTR
, PTR
));
92 static void coff_swap_filehdr_in
PARAMS ((bfd
*, PTR
, PTR
));
93 static void coff_swap_scnhdr_in
PARAMS ((bfd
*, PTR
, PTR
));
94 static bfd_boolean pe_mkobject
PARAMS ((bfd
*));
95 static PTR pe_mkobject_hook
PARAMS ((bfd
*, PTR
, PTR
));
97 #ifdef COFF_IMAGE_WITH_PE
98 /* This structure contains static variables used by the ILF code. */
99 typedef asection
* asection_ptr
;
105 struct bfd_in_memory
* bim
;
106 unsigned short magic
;
109 unsigned int relcount
;
111 coff_symbol_type
* sym_cache
;
112 coff_symbol_type
* sym_ptr
;
113 unsigned int sym_index
;
115 unsigned int * sym_table
;
116 unsigned int * table_ptr
;
118 combined_entry_type
* native_syms
;
119 combined_entry_type
* native_ptr
;
121 coff_symbol_type
** sym_ptr_table
;
122 coff_symbol_type
** sym_ptr_ptr
;
124 unsigned int sec_index
;
128 char * end_string_ptr
;
133 struct internal_reloc
* int_reltab
;
137 static asection_ptr pe_ILF_make_a_section
PARAMS ((pe_ILF_vars
*, const char *, unsigned int, flagword
));
138 static void pe_ILF_make_a_reloc
PARAMS ((pe_ILF_vars
*, bfd_vma
, bfd_reloc_code_real_type
, asection_ptr
));
139 static void pe_ILF_make_a_symbol
PARAMS ((pe_ILF_vars
*, const char *, const char *, asection_ptr
, flagword
));
140 static void pe_ILF_save_relocs
PARAMS ((pe_ILF_vars
*, asection_ptr
));
141 static void pe_ILF_make_a_symbol_reloc
PARAMS ((pe_ILF_vars
*, bfd_vma
, bfd_reloc_code_real_type
, struct symbol_cache_entry
**, unsigned int));
142 static bfd_boolean pe_ILF_build_a_bfd
PARAMS ((bfd
*, unsigned int, bfd_byte
*, bfd_byte
*, unsigned int, unsigned int));
143 static const bfd_target
* pe_ILF_object_p
PARAMS ((bfd
*));
144 static const bfd_target
* pe_bfd_object_p
PARAMS ((bfd
*));
145 #endif /* COFF_IMAGE_WITH_PE */
147 /**********************************************************************/
149 #ifndef NO_COFF_RELOCS
151 coff_swap_reloc_in (abfd
, src
, dst
)
156 RELOC
*reloc_src
= (RELOC
*) src
;
157 struct internal_reloc
*reloc_dst
= (struct internal_reloc
*) dst
;
159 reloc_dst
->r_vaddr
= H_GET_32 (abfd
, reloc_src
->r_vaddr
);
160 reloc_dst
->r_symndx
= H_GET_S32 (abfd
, reloc_src
->r_symndx
);
162 reloc_dst
->r_type
= H_GET_16 (abfd
, reloc_src
->r_type
);
164 #ifdef SWAP_IN_RELOC_OFFSET
165 reloc_dst
->r_offset
= SWAP_IN_RELOC_OFFSET (abfd
, reloc_src
->r_offset
);
170 coff_swap_reloc_out (abfd
, src
, dst
)
175 struct internal_reloc
*reloc_src
= (struct internal_reloc
*)src
;
176 struct external_reloc
*reloc_dst
= (struct external_reloc
*)dst
;
177 H_PUT_32 (abfd
, reloc_src
->r_vaddr
, reloc_dst
->r_vaddr
);
178 H_PUT_32 (abfd
, reloc_src
->r_symndx
, reloc_dst
->r_symndx
);
180 H_PUT_16 (abfd
, reloc_src
->r_type
, reloc_dst
->r_type
);
182 #ifdef SWAP_OUT_RELOC_OFFSET
183 SWAP_OUT_RELOC_OFFSET (abfd
, reloc_src
->r_offset
, reloc_dst
->r_offset
);
185 #ifdef SWAP_OUT_RELOC_EXTRA
186 SWAP_OUT_RELOC_EXTRA(abfd
, reloc_src
, reloc_dst
);
190 #endif /* not NO_COFF_RELOCS */
193 coff_swap_filehdr_in (abfd
, src
, dst
)
198 FILHDR
*filehdr_src
= (FILHDR
*) src
;
199 struct internal_filehdr
*filehdr_dst
= (struct internal_filehdr
*) dst
;
200 filehdr_dst
->f_magic
= H_GET_16 (abfd
, filehdr_src
->f_magic
);
201 filehdr_dst
->f_nscns
= H_GET_16 (abfd
, filehdr_src
-> f_nscns
);
202 filehdr_dst
->f_timdat
= H_GET_32 (abfd
, filehdr_src
-> f_timdat
);
204 filehdr_dst
->f_nsyms
= H_GET_32 (abfd
, filehdr_src
-> f_nsyms
);
205 filehdr_dst
->f_flags
= H_GET_16 (abfd
, filehdr_src
-> f_flags
);
206 filehdr_dst
->f_symptr
= H_GET_32 (abfd
, filehdr_src
->f_symptr
);
208 /* Other people's tools sometimes generate headers with an nsyms but
210 if (filehdr_dst
->f_nsyms
!= 0 && filehdr_dst
->f_symptr
== 0)
212 filehdr_dst
->f_nsyms
= 0;
213 filehdr_dst
->f_flags
|= F_LSYMS
;
216 filehdr_dst
->f_opthdr
= H_GET_16 (abfd
, filehdr_src
-> f_opthdr
);
219 #ifdef COFF_IMAGE_WITH_PE
220 # define coff_swap_filehdr_out _bfd_XXi_only_swap_filehdr_out
222 # define coff_swap_filehdr_out _bfd_pe_only_swap_filehdr_out
226 coff_swap_scnhdr_in (abfd
, ext
, in
)
231 SCNHDR
*scnhdr_ext
= (SCNHDR
*) ext
;
232 struct internal_scnhdr
*scnhdr_int
= (struct internal_scnhdr
*) in
;
234 memcpy(scnhdr_int
->s_name
, scnhdr_ext
->s_name
, sizeof (scnhdr_int
->s_name
));
235 scnhdr_int
->s_vaddr
= GET_SCNHDR_VADDR (abfd
, scnhdr_ext
->s_vaddr
);
236 scnhdr_int
->s_paddr
= GET_SCNHDR_PADDR (abfd
, scnhdr_ext
->s_paddr
);
237 scnhdr_int
->s_size
= GET_SCNHDR_SIZE (abfd
, scnhdr_ext
->s_size
);
238 scnhdr_int
->s_scnptr
= GET_SCNHDR_SCNPTR (abfd
, scnhdr_ext
->s_scnptr
);
239 scnhdr_int
->s_relptr
= GET_SCNHDR_RELPTR (abfd
, scnhdr_ext
->s_relptr
);
240 scnhdr_int
->s_lnnoptr
= GET_SCNHDR_LNNOPTR (abfd
, scnhdr_ext
->s_lnnoptr
);
241 scnhdr_int
->s_flags
= H_GET_32 (abfd
, scnhdr_ext
->s_flags
);
243 /* MS handles overflow of line numbers by carrying into the reloc
244 field (it appears). Since it's supposed to be zero for PE
245 *IMAGE* format, that's safe. This is still a bit iffy. */
246 #ifdef COFF_IMAGE_WITH_PE
247 scnhdr_int
->s_nlnno
= (H_GET_16 (abfd
, scnhdr_ext
->s_nlnno
)
248 + (H_GET_16 (abfd
, scnhdr_ext
->s_nreloc
) << 16));
249 scnhdr_int
->s_nreloc
= 0;
251 scnhdr_int
->s_nreloc
= H_GET_16 (abfd
, scnhdr_ext
->s_nreloc
);
252 scnhdr_int
->s_nlnno
= H_GET_16 (abfd
, scnhdr_ext
->s_nlnno
);
255 if (scnhdr_int
->s_vaddr
!= 0)
257 scnhdr_int
->s_vaddr
+= pe_data (abfd
)->pe_opthdr
.ImageBase
;
258 scnhdr_int
->s_vaddr
&= 0xffffffff;
261 #ifndef COFF_NO_HACK_SCNHDR_SIZE
262 /* If this section holds uninitialized data, use the virtual size
263 (stored in s_paddr) instead of the physical size. */
264 if ((scnhdr_int
->s_flags
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
) != 0
265 && (scnhdr_int
->s_paddr
> 0))
267 scnhdr_int
->s_size
= scnhdr_int
->s_paddr
;
268 /* This code used to set scnhdr_int->s_paddr to 0. However,
269 coff_set_alignment_hook stores s_paddr in virt_size, which
270 only works if it correctly holds the virtual size of the
281 bfd_size_type amt
= sizeof (pe_data_type
);
283 abfd
->tdata
.pe_obj_data
= (struct pe_tdata
*) bfd_zalloc (abfd
, amt
);
285 if (abfd
->tdata
.pe_obj_data
== 0)
292 /* in_reloc_p is architecture dependent. */
293 pe
->in_reloc_p
= in_reloc_p
;
295 #ifdef PEI_FORCE_MINIMUM_ALIGNMENT
296 pe
->force_minimum_alignment
= 1;
298 #ifdef PEI_TARGET_SUBSYSTEM
299 pe
->target_subsystem
= PEI_TARGET_SUBSYSTEM
;
305 /* Create the COFF backend specific information. */
307 pe_mkobject_hook (abfd
, filehdr
, aouthdr
)
310 PTR aouthdr ATTRIBUTE_UNUSED
;
312 struct internal_filehdr
*internal_f
= (struct internal_filehdr
*) filehdr
;
315 if (! pe_mkobject (abfd
))
319 pe
->coff
.sym_filepos
= internal_f
->f_symptr
;
320 /* These members communicate important constants about the symbol
321 table to GDB's symbol-reading code. These `constants'
322 unfortunately vary among coff implementations... */
323 pe
->coff
.local_n_btmask
= N_BTMASK
;
324 pe
->coff
.local_n_btshft
= N_BTSHFT
;
325 pe
->coff
.local_n_tmask
= N_TMASK
;
326 pe
->coff
.local_n_tshift
= N_TSHIFT
;
327 pe
->coff
.local_symesz
= SYMESZ
;
328 pe
->coff
.local_auxesz
= AUXESZ
;
329 pe
->coff
.local_linesz
= LINESZ
;
331 pe
->coff
.timestamp
= internal_f
->f_timdat
;
333 obj_raw_syment_count (abfd
) =
334 obj_conv_table_size (abfd
) =
337 pe
->real_flags
= internal_f
->f_flags
;
339 if ((internal_f
->f_flags
& F_DLL
) != 0)
342 if ((internal_f
->f_flags
& IMAGE_FILE_DEBUG_STRIPPED
) == 0)
343 abfd
->flags
|= HAS_DEBUG
;
345 #ifdef COFF_IMAGE_WITH_PE
347 pe
->pe_opthdr
= ((struct internal_aouthdr
*)aouthdr
)->pe
;
351 if (! _bfd_coff_arm_set_private_flags (abfd
, internal_f
->f_flags
))
352 coff_data (abfd
) ->flags
= 0;
359 pe_print_private_bfd_data (abfd
, vfile
)
363 FILE *file
= (FILE *) vfile
;
365 if (!_bfd_XX_print_private_bfd_data_common (abfd
, vfile
))
368 if (pe_saved_coff_bfd_print_private_bfd_data
!= NULL
)
372 return pe_saved_coff_bfd_print_private_bfd_data (abfd
, vfile
);
378 /* Copy any private info we understand from the input bfd
379 to the output bfd. */
382 pe_bfd_copy_private_bfd_data (ibfd
, obfd
)
385 if (!_bfd_XX_bfd_copy_private_bfd_data_common (ibfd
, obfd
))
388 if (pe_saved_coff_bfd_copy_private_bfd_data
)
389 return pe_saved_coff_bfd_copy_private_bfd_data (ibfd
, obfd
);
394 #define coff_bfd_copy_private_section_data \
395 _bfd_XX_bfd_copy_private_section_data
397 #define coff_get_symbol_info _bfd_XX_get_symbol_info
399 #ifdef COFF_IMAGE_WITH_PE
401 /* Code to handle Microsoft's Image Library Format.
402 Also known as LINK6 format.
403 Documentation about this format can be found at:
405 http://msdn.microsoft.com/library/specs/pecoff_section8.htm */
407 /* The following constants specify the sizes of the various data
408 structures that we have to create in order to build a bfd describing
409 an ILF object file. The final "+ 1" in the definitions of SIZEOF_IDATA6
410 and SIZEOF_IDATA7 below is to allow for the possibility that we might
411 need a padding byte in order to ensure 16 bit alignment for the section's
414 The value for SIZEOF_ILF_STRINGS is computed as follows:
416 There will be NUM_ILF_SECTIONS section symbols. Allow 9 characters
417 per symbol for their names (longest section name is .idata$x).
419 There will be two symbols for the imported value, one the symbol name
420 and one with _imp__ prefixed. Allowing for the terminating nul's this
421 is strlen (symbol_name) * 2 + 8 + 21 + strlen (source_dll).
423 The strings in the string table must start STRING__SIZE_SIZE bytes into
424 the table in order to for the string lookup code in coffgen/coffcode to
426 #define NUM_ILF_RELOCS 8
427 #define NUM_ILF_SECTIONS 6
428 #define NUM_ILF_SYMS (2 + NUM_ILF_SECTIONS)
430 #define SIZEOF_ILF_SYMS (NUM_ILF_SYMS * sizeof (* vars.sym_cache))
431 #define SIZEOF_ILF_SYM_TABLE (NUM_ILF_SYMS * sizeof (* vars.sym_table))
432 #define SIZEOF_ILF_NATIVE_SYMS (NUM_ILF_SYMS * sizeof (* vars.native_syms))
433 #define SIZEOF_ILF_SYM_PTR_TABLE (NUM_ILF_SYMS * sizeof (* vars.sym_ptr_table))
434 #define SIZEOF_ILF_EXT_SYMS (NUM_ILF_SYMS * sizeof (* vars.esym_table))
435 #define SIZEOF_ILF_RELOCS (NUM_ILF_RELOCS * sizeof (* vars.reltab))
436 #define SIZEOF_ILF_INT_RELOCS (NUM_ILF_RELOCS * sizeof (* vars.int_reltab))
437 #define SIZEOF_ILF_STRINGS (strlen (symbol_name) * 2 + 8 \
438 + 21 + strlen (source_dll) \
439 + NUM_ILF_SECTIONS * 9 \
441 #define SIZEOF_IDATA2 (5 * 4)
442 #define SIZEOF_IDATA4 (1 * 4)
443 #define SIZEOF_IDATA5 (1 * 4)
444 #define SIZEOF_IDATA6 (2 + strlen (symbol_name) + 1 + 1)
445 #define SIZEOF_IDATA7 (strlen (source_dll) + 1 + 1)
446 #define SIZEOF_ILF_SECTIONS (NUM_ILF_SECTIONS * sizeof (struct coff_section_tdata))
448 #define ILF_DATA_SIZE \
449 sizeof (* vars.bim) \
451 + SIZEOF_ILF_SYM_TABLE \
452 + SIZEOF_ILF_NATIVE_SYMS \
453 + SIZEOF_ILF_SYM_PTR_TABLE \
454 + SIZEOF_ILF_EXT_SYMS \
455 + SIZEOF_ILF_RELOCS \
456 + SIZEOF_ILF_INT_RELOCS \
457 + SIZEOF_ILF_STRINGS \
463 + SIZEOF_ILF_SECTIONS \
464 + MAX_TEXT_SECTION_SIZE
466 /* Create an empty relocation against the given symbol. */
468 pe_ILF_make_a_symbol_reloc (pe_ILF_vars
* vars
,
470 bfd_reloc_code_real_type reloc
,
471 struct symbol_cache_entry
** sym
,
472 unsigned int sym_index
)
475 struct internal_reloc
* internal
;
477 entry
= vars
->reltab
+ vars
->relcount
;
478 internal
= vars
->int_reltab
+ vars
->relcount
;
480 entry
->address
= address
;
482 entry
->howto
= bfd_reloc_type_lookup (vars
->abfd
, reloc
);
483 entry
->sym_ptr_ptr
= sym
;
485 internal
->r_vaddr
= address
;
486 internal
->r_symndx
= sym_index
;
487 internal
->r_type
= entry
->howto
->type
;
488 #if 0 /* These fields do not need to be initialised. */
489 internal
->r_size
= 0;
490 internal
->r_extern
= 0;
491 internal
->r_offset
= 0;
496 BFD_ASSERT (vars
->relcount
<= NUM_ILF_RELOCS
);
499 /* Create an empty relocation against the given section. */
501 pe_ILF_make_a_reloc (pe_ILF_vars
* vars
,
503 bfd_reloc_code_real_type reloc
,
506 pe_ILF_make_a_symbol_reloc (vars
, address
, reloc
, sec
->symbol_ptr_ptr
,
507 coff_section_data (vars
->abfd
, sec
)->i
);
510 /* Move the queued relocs into the given section. */
512 pe_ILF_save_relocs (pe_ILF_vars
* vars
,
515 /* Make sure that there is somewhere to store the internal relocs. */
516 if (coff_section_data (vars
->abfd
, sec
) == NULL
)
517 /* We should probably return an error indication here. */
520 coff_section_data (vars
->abfd
, sec
)->relocs
= vars
->int_reltab
;
521 coff_section_data (vars
->abfd
, sec
)->keep_relocs
= TRUE
;
523 sec
->relocation
= vars
->reltab
;
524 sec
->reloc_count
= vars
->relcount
;
525 sec
->flags
|= SEC_RELOC
;
527 vars
->reltab
+= vars
->relcount
;
528 vars
->int_reltab
+= vars
->relcount
;
531 BFD_ASSERT ((bfd_byte
*) vars
->int_reltab
< (bfd_byte
*) vars
->string_table
);
534 /* Create a global symbol and add it to the relevant tables. */
536 pe_ILF_make_a_symbol (pe_ILF_vars
* vars
,
538 const char * symbol_name
,
539 asection_ptr section
,
540 flagword extra_flags
)
542 coff_symbol_type
* sym
;
543 combined_entry_type
* ent
;
545 unsigned short sclass
;
547 if (extra_flags
& BSF_LOCAL
)
553 if (vars
->magic
== THUMBPEMAGIC
)
555 if (extra_flags
& BSF_FUNCTION
)
556 sclass
= C_THUMBEXTFUNC
;
557 else if (extra_flags
& BSF_LOCAL
)
558 sclass
= C_THUMBSTAT
;
564 BFD_ASSERT (vars
->sym_index
< NUM_ILF_SYMS
);
567 ent
= vars
->native_ptr
;
568 esym
= vars
->esym_ptr
;
570 /* Copy the symbol's name into the string table. */
571 sprintf (vars
->string_ptr
, "%s%s", prefix
, symbol_name
);
574 section
= (asection_ptr
) & bfd_und_section
;
576 /* Initialise the external symbol. */
577 H_PUT_32 (vars
->abfd
, vars
->string_ptr
- vars
->string_table
,
579 H_PUT_16 (vars
->abfd
, section
->target_index
, esym
->e_scnum
);
580 esym
->e_sclass
[0] = sclass
;
582 /* The following initialisations are unnecessary - the memory is
583 zero initialised. They are just kept here as reminders. */
585 esym
->e
.e
.e_zeroes
= 0;
587 esym
->e_type
= T_NULL
;
591 /* Initialise the internal symbol structure. */
592 ent
->u
.syment
.n_sclass
= sclass
;
593 ent
->u
.syment
.n_scnum
= section
->target_index
;
594 ent
->u
.syment
._n
._n_n
._n_offset
= (long) sym
;
596 #if 0 /* See comment above. */
597 ent
->u
.syment
.n_value
= 0;
598 ent
->u
.syment
.n_flags
= 0;
599 ent
->u
.syment
.n_type
= T_NULL
;
600 ent
->u
.syment
.n_numaux
= 0;
604 sym
->symbol
.the_bfd
= vars
->abfd
;
605 sym
->symbol
.name
= vars
->string_ptr
;
606 sym
->symbol
.flags
= BSF_EXPORT
| BSF_GLOBAL
| extra_flags
;
607 sym
->symbol
.section
= section
;
610 #if 0 /* See comment above. */
611 sym
->symbol
.value
= 0;
612 sym
->symbol
.udata
.i
= 0;
613 sym
->done_lineno
= FALSE
;
617 * vars
->table_ptr
= vars
->sym_index
;
618 * vars
->sym_ptr_ptr
= sym
;
620 /* Adjust pointers for the next symbol. */
623 vars
->sym_ptr_ptr
++;
627 vars
->string_ptr
+= strlen (symbol_name
) + strlen (prefix
) + 1;
629 BFD_ASSERT (vars
->string_ptr
< vars
->end_string_ptr
);
632 /* Create a section. */
634 pe_ILF_make_a_section (pe_ILF_vars
* vars
,
637 flagword extra_flags
)
642 sec
= bfd_make_section_old_way (vars
->abfd
, name
);
646 flags
= SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_KEEP
| SEC_IN_MEMORY
;
648 bfd_set_section_flags (vars
->abfd
, sec
, flags
| extra_flags
);
650 bfd_set_section_alignment (vars
->abfd
, sec
, 2);
652 /* Check that we will not run out of space. */
653 BFD_ASSERT (vars
->data
+ size
< vars
->bim
->buffer
+ vars
->bim
->size
);
655 /* Set the section size and contents. The actual
656 contents are filled in by our parent. */
657 bfd_set_section_size (vars
->abfd
, sec
, (bfd_size_type
) size
);
658 sec
->contents
= vars
->data
;
659 sec
->target_index
= vars
->sec_index
++;
661 /* Advance data pointer in the vars structure. */
664 /* Skip the padding byte if it was not needed.
665 The logic here is that if the string length is odd,
666 then the entire string length, including the null byte,
667 is even and so the extra, padding byte, is not needed. */
671 /* Create a coff_section_tdata structure for our use. */
672 sec
->used_by_bfd
= (struct coff_section_tdata
*) vars
->data
;
673 vars
->data
+= sizeof (struct coff_section_tdata
);
675 BFD_ASSERT (vars
->data
<= vars
->bim
->buffer
+ vars
->bim
->size
);
677 /* Create a symbol to refer to this section. */
678 pe_ILF_make_a_symbol (vars
, "", name
, sec
, BSF_LOCAL
);
680 /* Cache the index to the symbol in the coff_section_data structure. */
681 coff_section_data (vars
->abfd
, sec
)->i
= vars
->sym_index
- 1;
686 /* This structure contains the code that goes into the .text section
687 in order to perform a jump into the DLL lookup table. The entries
688 in the table are index by the magic number used to represent the
689 machine type in the PE file. The contents of the data[] arrays in
690 these entries are stolen from the jtab[] arrays in ld/pe-dll.c.
691 The SIZE field says how many bytes in the DATA array are actually
692 used. The OFFSET field says where in the data array the address
693 of the .idata$5 section should be placed. */
694 #define MAX_TEXT_SECTION_SIZE 32
698 unsigned short magic
;
699 unsigned char data
[MAX_TEXT_SECTION_SIZE
];
705 static jump_table jtab
[] =
709 { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 },
715 { MC68MAGIC
, { /* XXX fill me in */ }, 0, 0 },
717 #ifdef MIPS_ARCH_MAGIC_WINCE
718 { MIPS_ARCH_MAGIC_WINCE
,
719 { 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
720 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 },
725 #ifdef SH_ARCH_MAGIC_WINCE
726 { SH_ARCH_MAGIC_WINCE
,
727 { 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40,
728 0x09, 0x00, 0x00, 0x00, 0x00, 0x00 },
735 { 0x00, 0xc0, 0x9f, 0xe5, 0x00, 0xf0,
736 0x9c, 0xe5, 0x00, 0x00, 0x00, 0x00},
743 { 0x40, 0xb4, 0x02, 0x4e, 0x36, 0x68, 0xb4, 0x46,
744 0x40, 0xbc, 0x60, 0x47, 0x00, 0x00, 0x00, 0x00 },
752 #define NUM_ENTRIES(a) (sizeof (a) / sizeof (a)[0])
755 /* Build a full BFD from the information supplied in a ILF object. */
757 pe_ILF_build_a_bfd (bfd
* abfd
,
759 bfd_byte
* symbol_name
,
760 bfd_byte
* source_dll
,
761 unsigned int ordinal
,
766 struct internal_filehdr internal_f
;
767 unsigned int import_type
;
768 unsigned int import_name_type
;
769 asection_ptr id4
, id5
, id6
= NULL
, text
= NULL
;
770 coff_symbol_type
** imp_sym
;
771 unsigned int imp_index
;
773 /* Decode and verify the types field of the ILF structure. */
774 import_type
= types
& 0x3;
775 import_name_type
= (types
& 0x1c) >> 2;
784 /* XXX code yet to be written. */
785 _bfd_error_handler (_("%s: Unhandled import type; %x"),
786 bfd_archive_filename (abfd
), import_type
);
790 _bfd_error_handler (_("%s: Unrecognised import type; %x"),
791 bfd_archive_filename (abfd
), import_type
);
795 switch (import_name_type
)
799 case IMPORT_NAME_NOPREFIX
:
800 case IMPORT_NAME_UNDECORATE
:
804 _bfd_error_handler (_("%s: Unrecognised import name type; %x"),
805 bfd_archive_filename (abfd
), import_name_type
);
809 /* Initialise local variables.
811 Note these are kept in a structure rather than being
812 declared as statics since bfd frowns on global variables.
814 We are going to construct the contents of the BFD in memory,
815 so allocate all the space that we will need right now. */
816 ptr
= bfd_zalloc (abfd
, (bfd_size_type
) ILF_DATA_SIZE
);
820 /* Create a bfd_in_memory structure. */
821 vars
.bim
= (struct bfd_in_memory
*) ptr
;
822 vars
.bim
->buffer
= ptr
;
823 vars
.bim
->size
= ILF_DATA_SIZE
;
824 ptr
+= sizeof (* vars
.bim
);
826 /* Initialise the pointers to regions of the memory and the
827 other contents of the pe_ILF_vars structure as well. */
828 vars
.sym_cache
= (coff_symbol_type
*) ptr
;
829 vars
.sym_ptr
= (coff_symbol_type
*) ptr
;
831 ptr
+= SIZEOF_ILF_SYMS
;
833 vars
.sym_table
= (unsigned int *) ptr
;
834 vars
.table_ptr
= (unsigned int *) ptr
;
835 ptr
+= SIZEOF_ILF_SYM_TABLE
;
837 vars
.native_syms
= (combined_entry_type
*) ptr
;
838 vars
.native_ptr
= (combined_entry_type
*) ptr
;
839 ptr
+= SIZEOF_ILF_NATIVE_SYMS
;
841 vars
.sym_ptr_table
= (coff_symbol_type
**) ptr
;
842 vars
.sym_ptr_ptr
= (coff_symbol_type
**) ptr
;
843 ptr
+= SIZEOF_ILF_SYM_PTR_TABLE
;
845 vars
.esym_table
= (SYMENT
*) ptr
;
846 vars
.esym_ptr
= (SYMENT
*) ptr
;
847 ptr
+= SIZEOF_ILF_EXT_SYMS
;
849 vars
.reltab
= (arelent
*) ptr
;
851 ptr
+= SIZEOF_ILF_RELOCS
;
853 vars
.int_reltab
= (struct internal_reloc
*) ptr
;
854 ptr
+= SIZEOF_ILF_INT_RELOCS
;
856 vars
.string_table
= ptr
;
857 vars
.string_ptr
= ptr
+ STRING_SIZE_SIZE
;
858 ptr
+= SIZEOF_ILF_STRINGS
;
859 vars
.end_string_ptr
= ptr
;
861 /* The remaining space in bim->buffer is used
862 by the pe_ILF_make_a_section() function. */
868 /* Create the initial .idata$<n> sections:
869 [.idata$2: Import Directory Table -- not needed]
870 .idata$4: Import Lookup Table
871 .idata$5: Import Address Table
873 Note we do not create a .idata$3 section as this is
874 created for us by the linker script. */
875 id4
= pe_ILF_make_a_section (& vars
, ".idata$4", SIZEOF_IDATA4
, 0);
876 id5
= pe_ILF_make_a_section (& vars
, ".idata$5", SIZEOF_IDATA5
, 0);
877 if (id4
== NULL
|| id5
== NULL
)
880 /* Fill in the contents of these sections. */
881 if (import_name_type
== IMPORT_ORDINAL
)
884 /* XXX - treat as IMPORT_NAME ??? */
887 * (unsigned int *) id4
->contents
= ordinal
| 0x80000000;
888 * (unsigned int *) id5
->contents
= ordinal
| 0x80000000;
894 /* Create .idata$6 - the Hint Name Table. */
895 id6
= pe_ILF_make_a_section (& vars
, ".idata$6", SIZEOF_IDATA6
, 0);
899 /* If necessary, trim the import symbol name. */
900 symbol
= symbol_name
;
902 if (import_name_type
!= IMPORT_NAME
)
903 /* Skip any prefix in symbol_name. */
904 while (*symbol
== '@' || * symbol
== '?' || * symbol
== '_')
907 if (import_name_type
== IMPORT_NAME_UNDECORATE
)
909 /* Truncate at the first '@' */
910 while (* symbol
!= 0 && * symbol
!= '@')
916 id6
->contents
[0] = ordinal
& 0xff;
917 id6
->contents
[1] = ordinal
>> 8;
919 strcpy (id6
->contents
+ 2, symbol
);
922 if (import_name_type
!= IMPORT_ORDINAL
)
924 pe_ILF_make_a_reloc (&vars
, (bfd_vma
) 0, BFD_RELOC_RVA
, id6
);
925 pe_ILF_save_relocs (&vars
, id4
);
927 pe_ILF_make_a_reloc (&vars
, (bfd_vma
) 0, BFD_RELOC_RVA
, id6
);
928 pe_ILF_save_relocs (&vars
, id5
);
931 /* Create extra sections depending upon the type of import we are dealing with. */
937 /* Create a .text section.
938 First we need to look up its contents in the jump table. */
939 for (i
= NUM_ENTRIES (jtab
); i
--;)
941 if (jtab
[i
].size
== 0)
943 if (jtab
[i
].magic
== magic
)
946 /* If we did not find a matching entry something is wrong. */
950 /* Create the .text section. */
951 text
= pe_ILF_make_a_section (& vars
, ".text", jtab
[i
].size
, SEC_CODE
);
955 /* Copy in the jump code. */
956 memcpy (text
->contents
, jtab
[i
].data
, jtab
[i
].size
);
958 /* Create an import symbol. */
959 pe_ILF_make_a_symbol (& vars
, "__imp_", symbol_name
, id5
, 0);
960 imp_sym
= vars
.sym_ptr_ptr
- 1;
961 imp_index
= vars
.sym_index
- 1;
963 /* Create a reloc for the data in the text section. */
964 #ifdef MIPS_ARCH_MAGIC_WINCE
965 if (magic
== MIPS_ARCH_MAGIC_WINCE
)
967 pe_ILF_make_a_symbol_reloc (&vars
, (bfd_vma
) 0, BFD_RELOC_HI16_S
,
968 (struct symbol_cache_entry
**) imp_sym
,
970 pe_ILF_make_a_reloc (&vars
, (bfd_vma
) 0, BFD_RELOC_LO16
, text
);
971 pe_ILF_make_a_symbol_reloc (&vars
, (bfd_vma
) 4, BFD_RELOC_LO16
,
972 (struct symbol_cache_entry
**) imp_sym
,
977 pe_ILF_make_a_symbol_reloc (&vars
, (bfd_vma
) jtab
[i
].offset
,
978 BFD_RELOC_32
, (asymbol
**) imp_sym
,
981 pe_ILF_save_relocs (& vars
, text
);
988 /* XXX code not yet written. */
992 /* Initialise the bfd. */
993 memset (& internal_f
, 0, sizeof (internal_f
));
995 internal_f
.f_magic
= magic
;
996 internal_f
.f_symptr
= 0;
997 internal_f
.f_nsyms
= 0;
998 internal_f
.f_flags
= F_AR32WR
| F_LNNO
; /* XXX is this correct ? */
1000 if ( ! bfd_set_start_address (abfd
, (bfd_vma
) 0)
1001 || ! bfd_coff_set_arch_mach_hook (abfd
, & internal_f
))
1004 if (bfd_coff_mkobject_hook (abfd
, (PTR
) & internal_f
, NULL
) == NULL
)
1007 coff_data (abfd
)->pe
= 1;
1009 if (vars
.magic
== THUMBPEMAGIC
)
1010 /* Stop some linker warnings about thumb code not supporting interworking. */
1011 coff_data (abfd
)->flags
|= F_INTERWORK
| F_INTERWORK_SET
;
1014 /* Switch from file contents to memory contents. */
1015 bfd_cache_close (abfd
);
1017 abfd
->iostream
= (PTR
) vars
.bim
;
1018 abfd
->flags
|= BFD_IN_MEMORY
/* | HAS_LOCALS */;
1020 obj_sym_filepos (abfd
) = 0;
1022 /* Now create a symbol describing the imported value. */
1023 switch (import_type
)
1026 pe_ILF_make_a_symbol (& vars
, "", symbol_name
, text
,
1027 BSF_NOT_AT_END
| BSF_FUNCTION
);
1029 /* Create an import symbol for the DLL, without the
1031 ptr
= strrchr (source_dll
, '.');
1034 pe_ILF_make_a_symbol (& vars
, "__IMPORT_DESCRIPTOR_", source_dll
, NULL
, 0);
1040 /* Nothing to do here. */
1044 /* XXX code not yet written. */
1048 /* Point the bfd at the symbol table. */
1049 obj_symbols (abfd
) = vars
.sym_cache
;
1050 bfd_get_symcount (abfd
) = vars
.sym_index
;
1052 obj_raw_syments (abfd
) = vars
.native_syms
;
1053 obj_raw_syment_count (abfd
) = vars
.sym_index
;
1055 obj_coff_external_syms (abfd
) = (PTR
) vars
.esym_table
;
1056 obj_coff_keep_syms (abfd
) = TRUE
;
1058 obj_convert (abfd
) = vars
.sym_table
;
1059 obj_conv_table_size (abfd
) = vars
.sym_index
;
1061 obj_coff_strings (abfd
) = vars
.string_table
;
1062 obj_coff_keep_strings (abfd
) = TRUE
;
1064 abfd
->flags
|= HAS_SYMS
;
1069 /* We have detected a Image Library Format archive element.
1070 Decode the element and return the appropriate target. */
1071 static const bfd_target
*
1072 pe_ILF_object_p (bfd
* abfd
)
1074 bfd_byte buffer
[16];
1076 bfd_byte
* symbol_name
;
1077 bfd_byte
* source_dll
;
1078 unsigned int machine
;
1080 unsigned int ordinal
;
1084 /* Upon entry the first four buyes of the ILF header have
1085 already been read. Now read the rest of the header. */
1086 if (bfd_bread (buffer
, (bfd_size_type
) 16, abfd
) != 16)
1091 /* We do not bother to check the version number.
1092 version = H_GET_16 (abfd, ptr); */
1095 machine
= H_GET_16 (abfd
, ptr
);
1098 /* Check that the machine type is recognised. */
1103 case IMAGE_FILE_MACHINE_UNKNOWN
:
1104 case IMAGE_FILE_MACHINE_ALPHA
:
1105 case IMAGE_FILE_MACHINE_ALPHA64
:
1106 case IMAGE_FILE_MACHINE_IA64
:
1109 case IMAGE_FILE_MACHINE_I386
:
1115 case IMAGE_FILE_MACHINE_M68K
:
1121 case IMAGE_FILE_MACHINE_R3000
:
1122 case IMAGE_FILE_MACHINE_R4000
:
1123 case IMAGE_FILE_MACHINE_R10000
:
1125 case IMAGE_FILE_MACHINE_MIPS16
:
1126 case IMAGE_FILE_MACHINE_MIPSFPU
:
1127 case IMAGE_FILE_MACHINE_MIPSFPU16
:
1128 #ifdef MIPS_ARCH_MAGIC_WINCE
1129 magic
= MIPS_ARCH_MAGIC_WINCE
;
1133 case IMAGE_FILE_MACHINE_SH3
:
1134 case IMAGE_FILE_MACHINE_SH4
:
1135 #ifdef SH_ARCH_MAGIC_WINCE
1136 magic
= SH_ARCH_MAGIC_WINCE
;
1140 case IMAGE_FILE_MACHINE_ARM
:
1146 case IMAGE_FILE_MACHINE_THUMB
:
1149 extern const bfd_target TARGET_LITTLE_SYM
;
1151 if (abfd
->xvec
== & TARGET_LITTLE_SYM
)
1152 magic
= THUMBPEMAGIC
;
1157 case IMAGE_FILE_MACHINE_POWERPC
:
1158 /* We no longer support PowerPC. */
1162 _("%s: Unrecognised machine type (0x%x) in Import Library Format archive"),
1163 bfd_archive_filename (abfd
), machine
);
1164 bfd_set_error (bfd_error_malformed_archive
);
1174 _("%s: Recognised but unhandled machine type (0x%x) in Import Library Format archive"),
1175 bfd_archive_filename (abfd
), machine
);
1176 bfd_set_error (bfd_error_wrong_format
);
1181 /* We do not bother to check the date.
1182 date = H_GET_32 (abfd, ptr); */
1185 size
= H_GET_32 (abfd
, ptr
);
1191 (_("%s: size field is zero in Import Library Format header"),
1192 bfd_archive_filename (abfd
));
1193 bfd_set_error (bfd_error_malformed_archive
);
1198 ordinal
= H_GET_16 (abfd
, ptr
);
1201 types
= H_GET_16 (abfd
, ptr
);
1204 /* Now read in the two strings that follow. */
1205 ptr
= bfd_alloc (abfd
, size
);
1209 if (bfd_bread (ptr
, size
, abfd
) != size
)
1211 bfd_release (abfd
, ptr
);
1216 source_dll
= ptr
+ strlen (ptr
) + 1;
1218 /* Verify that the strings are null terminated. */
1219 if (ptr
[size
- 1] != 0 || ((unsigned long) (source_dll
- ptr
) >= size
))
1222 (_("%s: string not null terminated in ILF object file."),
1223 bfd_archive_filename (abfd
));
1224 bfd_set_error (bfd_error_malformed_archive
);
1225 bfd_release (abfd
, ptr
);
1229 /* Now construct the bfd. */
1230 if (! pe_ILF_build_a_bfd (abfd
, magic
, symbol_name
,
1231 source_dll
, ordinal
, types
))
1233 bfd_release (abfd
, ptr
);
1240 static const bfd_target
*
1241 pe_bfd_object_p (bfd
* abfd
)
1244 struct external_PEI_DOS_hdr dos_hdr
;
1245 struct external_PEI_IMAGE_hdr image_hdr
;
1248 /* Detect if this a Microsoft Import Library Format element. */
1249 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0
1250 || bfd_bread (buffer
, (bfd_size_type
) 4, abfd
) != 4)
1252 if (bfd_get_error () != bfd_error_system_call
)
1253 bfd_set_error (bfd_error_wrong_format
);
1257 if (H_GET_32 (abfd
, buffer
) == 0xffff0000)
1258 return pe_ILF_object_p (abfd
);
1260 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0
1261 || bfd_bread (&dos_hdr
, (bfd_size_type
) sizeof (dos_hdr
), abfd
)
1262 != sizeof (dos_hdr
))
1264 if (bfd_get_error () != bfd_error_system_call
)
1265 bfd_set_error (bfd_error_wrong_format
);
1269 /* There are really two magic numbers involved; the magic number
1270 that says this is a NT executable (PEI) and the magic number that
1271 determines the architecture. The former is DOSMAGIC, stored in
1272 the e_magic field. The latter is stored in the f_magic field.
1273 If the NT magic number isn't valid, the architecture magic number
1274 could be mimicked by some other field (specifically, the number
1275 of relocs in section 3). Since this routine can only be called
1276 correctly for a PEI file, check the e_magic number here, and, if
1277 it doesn't match, clobber the f_magic number so that we don't get
1279 if (H_GET_16 (abfd
, dos_hdr
.e_magic
) != DOSMAGIC
)
1281 bfd_set_error (bfd_error_wrong_format
);
1285 offset
= H_GET_32 (abfd
, dos_hdr
.e_lfanew
);
1286 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0
1287 || (bfd_bread (&image_hdr
, (bfd_size_type
) sizeof (image_hdr
), abfd
)
1288 != sizeof (image_hdr
)))
1290 if (bfd_get_error () != bfd_error_system_call
)
1291 bfd_set_error (bfd_error_wrong_format
);
1295 if (H_GET_32 (abfd
, image_hdr
.nt_signature
) != 0x4550)
1297 bfd_set_error (bfd_error_wrong_format
);
1301 /* Here is the hack. coff_object_p wants to read filhsz bytes to
1302 pick up the COFF header for PE, see "struct external_PEI_filehdr"
1303 in include/coff/pe.h. We adjust so that that will work. */
1304 if (bfd_seek (abfd
, (file_ptr
) (offset
- sizeof (dos_hdr
)), SEEK_SET
) != 0)
1306 if (bfd_get_error () != bfd_error_system_call
)
1307 bfd_set_error (bfd_error_wrong_format
);
1311 return coff_object_p (abfd
);
1314 #define coff_object_p pe_bfd_object_p
1315 #endif /* COFF_IMAGE_WITH_PE */