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