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