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