OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.3/] [bfd/] [peicode.h] - Blame information for rev 1782

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1181 sfurman
/* Support for the generic parts of PE/PEI, for BFD.
2
   Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3
   Free Software Foundation, Inc.
4
   Written by Cygnus Solutions.
5
 
6
This file is part of BFD, the Binary File Descriptor library.
7
 
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
 
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
 
22
/*
23
Most of this hacked by  Steve Chamberlain,
24
                        sac@cygnus.com
25
 
26
PE/PEI rearrangement (and code added): Donn Terry
27
                                       Softway Systems, Inc.
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
 
59
#include "libpei.h"
60
 
61
static boolean (*pe_saved_coff_bfd_print_private_bfd_data)
62
    PARAMS ((bfd *, PTR)) =
63
#ifndef coff_bfd_print_private_bfd_data
64
     NULL;
65
#else
66
     coff_bfd_print_private_bfd_data;
67
#undef coff_bfd_print_private_bfd_data
68
#endif
69
 
70
static boolean pe_print_private_bfd_data PARAMS ((bfd *, PTR));
71
#define coff_bfd_print_private_bfd_data pe_print_private_bfd_data
72
 
73
static 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
80
#endif
81
 
82
static boolean pe_bfd_copy_private_bfd_data PARAMS ((bfd *, bfd *));
83
#define coff_bfd_copy_private_bfd_data pe_bfd_copy_private_bfd_data
84
 
85
#define coff_mkobject      pe_mkobject
86
#define coff_mkobject_hook pe_mkobject_hook
87
 
88
#ifndef NO_COFF_RELOCS
89
static void coff_swap_reloc_in PARAMS ((bfd *, PTR, PTR));
90
static unsigned int coff_swap_reloc_out PARAMS ((bfd *, PTR, PTR));
91
#endif
92
static void coff_swap_filehdr_in PARAMS ((bfd *, PTR, PTR));
93
static void coff_swap_scnhdr_in PARAMS ((bfd *, PTR, PTR));
94
static boolean pe_mkobject PARAMS ((bfd *));
95
static PTR pe_mkobject_hook PARAMS ((bfd *, PTR, PTR));
96
 
97
#ifdef COFF_IMAGE_WITH_PE
98
/* This structure contains static variables used by the ILF code.  */
99
typedef asection * asection_ptr;
100
 
101
typedef struct
102
{
103
  bfd *                 abfd;
104
  bfd_byte *            data;
105
  struct bfd_in_memory * bim;
106
  unsigned short        magic;
107
 
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;
114
 
115
  unsigned int *        sym_table;
116
  unsigned int *        table_ptr;
117
 
118
  combined_entry_type * native_syms;
119
  combined_entry_type * native_ptr;
120
 
121
  coff_symbol_type **   sym_ptr_table;
122
  coff_symbol_type **   sym_ptr_ptr;
123
 
124
  unsigned int          sec_index;
125
 
126
  char *                string_table;
127
  char *                string_ptr;
128
  char *                end_string_ptr;
129
 
130
  SYMENT *              esym_table;
131
  SYMENT *              esym_ptr;
132
 
133
  struct internal_reloc * int_reltab;
134
}
135
pe_ILF_vars;
136
 
137
static asection_ptr       pe_ILF_make_a_section   PARAMS ((pe_ILF_vars *, const char *, unsigned int, flagword));
138
static void               pe_ILF_make_a_reloc     PARAMS ((pe_ILF_vars *, bfd_vma, bfd_reloc_code_real_type, asection_ptr));
139
static void               pe_ILF_make_a_symbol    PARAMS ((pe_ILF_vars *, const char *, const char *, asection_ptr, flagword));
140
static void               pe_ILF_save_relocs      PARAMS ((pe_ILF_vars *, asection_ptr));
141
static void               pe_ILF_make_a_symbol_reloc  PARAMS ((pe_ILF_vars *, bfd_vma, bfd_reloc_code_real_type, struct symbol_cache_entry **, unsigned int));
142
static boolean            pe_ILF_build_a_bfd      PARAMS ((bfd *, unsigned int, bfd_byte *, bfd_byte *, unsigned int, unsigned int));
143
static const bfd_target * pe_ILF_object_p         PARAMS ((bfd *));
144
static const bfd_target * pe_bfd_object_p         PARAMS ((bfd *));
145
#endif /* COFF_IMAGE_WITH_PE */
146
 
147
/**********************************************************************/
148
 
149
#ifndef NO_COFF_RELOCS
150
static void
151
coff_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
 
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);
161
 
162
  reloc_dst->r_type = H_GET_16 (abfd, reloc_src->r_type);
163
 
164
#ifdef SWAP_IN_RELOC_OFFSET
165
  reloc_dst->r_offset = SWAP_IN_RELOC_OFFSET (abfd, reloc_src->r_offset);
166
#endif
167
}
168
 
169
static unsigned int
170
coff_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;
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);
179
 
180
  H_PUT_16 (abfd, reloc_src->r_type, reloc_dst->r_type);
181
 
182
#ifdef SWAP_OUT_RELOC_OFFSET
183
  SWAP_OUT_RELOC_OFFSET (abfd, reloc_src->r_offset, reloc_dst->r_offset);
184
#endif
185
#ifdef SWAP_OUT_RELOC_EXTRA
186
  SWAP_OUT_RELOC_EXTRA(abfd, reloc_src, reloc_dst);
187
#endif
188
  return RELSZ;
189
}
190
#endif /* not NO_COFF_RELOCS */
191
 
192
static void
193
coff_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;
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);
203
 
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);
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
 
216
  filehdr_dst->f_opthdr = H_GET_16 (abfd, filehdr_src-> f_opthdr);
217
}
218
 
219
#ifdef COFF_IMAGE_WITH_PE
220
# define coff_swap_filehdr_out _bfd_XXi_only_swap_filehdr_out
221
#else
222
# define coff_swap_filehdr_out _bfd_pe_only_swap_filehdr_out
223
#endif
224
 
225
static void
226
coff_swap_scnhdr_in (abfd, ext, in)
227
     bfd            *abfd;
228
     PTR             ext;
229
     PTR             in;
230
{
231
  SCNHDR *scnhdr_ext = (SCNHDR *) ext;
232
  struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
233
 
234
  memcpy(scnhdr_int->s_name, scnhdr_ext->s_name, sizeof (scnhdr_int->s_name));
235
  scnhdr_int->s_vaddr = GET_SCNHDR_VADDR (abfd, scnhdr_ext->s_vaddr);
236
  scnhdr_int->s_paddr = GET_SCNHDR_PADDR (abfd, scnhdr_ext->s_paddr);
237
  scnhdr_int->s_size = GET_SCNHDR_SIZE (abfd, scnhdr_ext->s_size);
238
  scnhdr_int->s_scnptr = GET_SCNHDR_SCNPTR (abfd, scnhdr_ext->s_scnptr);
239
  scnhdr_int->s_relptr = GET_SCNHDR_RELPTR (abfd, scnhdr_ext->s_relptr);
240
  scnhdr_int->s_lnnoptr = GET_SCNHDR_LNNOPTR (abfd, scnhdr_ext->s_lnnoptr);
241
  scnhdr_int->s_flags = H_GET_32 (abfd, scnhdr_ext->s_flags);
242
 
243
  /* MS handles overflow of line numbers by carrying into the reloc
244
     field (it appears).  Since it's supposed to be zero for PE
245
     *IMAGE* format, that's safe.  This is still a bit iffy.  */
246
#ifdef COFF_IMAGE_WITH_PE
247
  scnhdr_int->s_nlnno = (H_GET_16 (abfd, scnhdr_ext->s_nlnno)
248
                         + (H_GET_16 (abfd, scnhdr_ext->s_nreloc) << 16));
249
  scnhdr_int->s_nreloc = 0;
250
#else
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);
253
#endif
254
 
255
  if (scnhdr_int->s_vaddr != 0)
256
    {
257
      scnhdr_int->s_vaddr += pe_data (abfd)->pe_opthdr.ImageBase;
258
      scnhdr_int->s_vaddr &= 0xffffffff;
259
    }
260
 
261
#ifndef COFF_NO_HACK_SCNHDR_SIZE
262
  /* If this section holds uninitialized data, use the virtual size
263
     (stored in s_paddr) instead of the physical size.  */
264
  if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0
265
      && (scnhdr_int->s_paddr > 0))
266
    {
267
      scnhdr_int->s_size = scnhdr_int->s_paddr;
268
      /* This code used to set scnhdr_int->s_paddr to 0.  However,
269
         coff_set_alignment_hook stores s_paddr in virt_size, which
270
         only works if it correctly holds the virtual size of the
271
         section.  */
272
    }
273
#endif
274
}
275
 
276
static boolean
277
pe_mkobject (abfd)
278
     bfd * abfd;
279
{
280
  pe_data_type *pe;
281
  bfd_size_type amt = sizeof (pe_data_type);
282
 
283
  abfd->tdata.pe_obj_data = (struct pe_tdata *) bfd_zalloc (abfd, amt);
284
 
285
  if (abfd->tdata.pe_obj_data == 0)
286
    return false;
287
 
288
  pe = pe_data (abfd);
289
 
290
  pe->coff.pe = 1;
291
 
292
  /* in_reloc_p is architecture dependent.  */
293
  pe->in_reloc_p = in_reloc_p;
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
 
302
  return true;
303
}
304
 
305
/* Create the COFF backend specific information.  */
306
static PTR
307
pe_mkobject_hook (abfd, filehdr, aouthdr)
308
     bfd * abfd;
309
     PTR filehdr;
310
     PTR aouthdr ATTRIBUTE_UNUSED;
311
{
312
  struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
313
  pe_data_type *pe;
314
 
315
  if (! pe_mkobject (abfd))
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
 
331
  pe->coff.timestamp = internal_f->f_timdat;
332
 
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
 
342
  if ((internal_f->f_flags & IMAGE_FILE_DEBUG_STRIPPED) == 0)
343
    abfd->flags |= HAS_DEBUG;
344
 
345
#ifdef COFF_IMAGE_WITH_PE
346
  if (aouthdr)
347
    pe->pe_opthdr = ((struct internal_aouthdr *)aouthdr)->pe;
348
#endif
349
 
350
#ifdef ARM
351
  if (! _bfd_coff_arm_set_private_flags (abfd, internal_f->f_flags))
352
    coff_data (abfd) ->flags = 0;
353
#endif
354
 
355
  return (PTR) pe;
356
}
357
 
358
static boolean
359
pe_print_private_bfd_data (abfd, vfile)
360
     bfd *abfd;
361
     PTR vfile;
362
{
363
  FILE *file = (FILE *) vfile;
364
 
365
  if (!_bfd_XX_print_private_bfd_data_common (abfd, vfile))
366
    return false;
367
 
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
}
377
 
378
/* Copy any private info we understand from the input bfd
379
   to the output bfd.  */
380
 
381
static boolean
382
pe_bfd_copy_private_bfd_data (ibfd, obfd)
383
     bfd *ibfd, *obfd;
384
{
385
  if (!_bfd_XX_bfd_copy_private_bfd_data_common (ibfd, obfd))
386
    return false;
387
 
388
  if (pe_saved_coff_bfd_copy_private_bfd_data)
389
    return pe_saved_coff_bfd_copy_private_bfd_data (ibfd, obfd);
390
 
391
  return true;
392
}
393
 
394
#define coff_bfd_copy_private_section_data \
395
  _bfd_XX_bfd_copy_private_section_data
396
 
397
#define coff_get_symbol_info _bfd_XX_get_symbol_info
398
 
399
#ifdef COFF_IMAGE_WITH_PE
400
 
401
/* Code to handle Microsoft's Image Library Format.
402
   Also known as LINK6 format.
403
   Documentation about this format can be found at:
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
417
      per symbol for their names (longest section name is .idata$x).
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
421
      is strlen (symbol_name) * 2 + 8 + 21 + strlen (source_dll).
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)
429
 
430
#define SIZEOF_ILF_SYMS         (NUM_ILF_SYMS * sizeof (* vars.sym_cache))
431
#define SIZEOF_ILF_SYM_TABLE    (NUM_ILF_SYMS * sizeof (* vars.sym_table))
432
#define SIZEOF_ILF_NATIVE_SYMS  (NUM_ILF_SYMS * sizeof (* vars.native_syms))
433
#define SIZEOF_ILF_SYM_PTR_TABLE (NUM_ILF_SYMS * sizeof (* vars.sym_ptr_table))
434
#define SIZEOF_ILF_EXT_SYMS     (NUM_ILF_SYMS * sizeof (* vars.esym_table))
435
#define SIZEOF_ILF_RELOCS       (NUM_ILF_RELOCS * sizeof (* vars.reltab))
436
#define SIZEOF_ILF_INT_RELOCS   (NUM_ILF_RELOCS * sizeof (* vars.int_reltab))
437
#define SIZEOF_ILF_STRINGS      (strlen (symbol_name) * 2 + 8 \
438
                                        + 21 + strlen (source_dll) \
439
                                        + NUM_ILF_SECTIONS * 9 \
440
                                        + STRING_SIZE_SIZE)
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))
447
 
448
#define ILF_DATA_SIZE                           \
449
      sizeof (* vars.bim)                       \
450
    + SIZEOF_ILF_SYMS                           \
451
    + SIZEOF_ILF_SYM_TABLE                      \
452
    + SIZEOF_ILF_NATIVE_SYMS                    \
453
    + SIZEOF_ILF_SYM_PTR_TABLE                  \
454
    + SIZEOF_ILF_EXT_SYMS                       \
455
    + SIZEOF_ILF_RELOCS                         \
456
    + SIZEOF_ILF_INT_RELOCS                     \
457
    + SIZEOF_ILF_STRINGS                        \
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
 
466
/* Create an empty relocation against the given symbol.  */
467
static void
468
pe_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)
473
{
474
  arelent * entry;
475
  struct internal_reloc * internal;
476
 
477
  entry = vars->reltab + vars->relcount;
478
  internal = vars->int_reltab + vars->relcount;
479
 
480
  entry->address     = address;
481
  entry->addend      = 0;
482
  entry->howto       = bfd_reloc_type_lookup (vars->abfd, reloc);
483
  entry->sym_ptr_ptr = sym;
484
 
485
  internal->r_vaddr  = address;
486
  internal->r_symndx = sym_index;
487
  internal->r_type   = entry->howto->type;
488
#if 0  /* These fields do not need to be initialised.  */
489
  internal->r_size   = 0;
490
  internal->r_extern = 0;
491
  internal->r_offset = 0;
492
#endif
493
 
494
  vars->relcount ++;
495
 
496
  BFD_ASSERT (vars->relcount <= NUM_ILF_RELOCS);
497
}
498
 
499
/* Create an empty relocation against the given section.  */
500
static void
501
pe_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
 
510
/* Move the queued relocs into the given section.  */
511
static void
512
pe_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
 
531
  BFD_ASSERT ((bfd_byte *) vars->int_reltab < (bfd_byte *) vars->string_table);
532
}
533
 
534
/* Create a global symbol and add it to the relevant tables.  */
535
static void
536
pe_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)
548
    sclass = C_STAT;
549
  else
550
    sclass = C_EXT;
551
 
552
#ifdef THUMBPEMAGIC
553
  if (vars->magic == THUMBPEMAGIC)
554
    {
555
      if (extra_flags & BSF_FUNCTION)
556
        sclass = C_THUMBEXTFUNC;
557
      else if (extra_flags & BSF_LOCAL)
558
        sclass = C_THUMBSTAT;
559
      else
560
        sclass = C_THUMBEXT;
561
    }
562
#endif
563
 
564
  BFD_ASSERT (vars->sym_index < NUM_ILF_SYMS);
565
 
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
 
573
  if (section == NULL)
574
    section = (asection_ptr) & bfd_und_section;
575
 
576
  /* Initialise the external symbol.  */
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);
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
590
 
591
  /* Initialise the internal symbol structure.  */
592
  ent->u.syment.n_sclass          = sclass;
593
  ent->u.syment.n_scnum           = section->target_index;
594
  ent->u.syment._n._n_n._n_offset = (long) sym;
595
 
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
603
 
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;
609
 
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
616
 
617
  * vars->table_ptr = vars->sym_index;
618
  * vars->sym_ptr_ptr = sym;
619
 
620
  /* Adjust pointers for the next symbol.  */
621
  vars->sym_index ++;
622
  vars->sym_ptr ++;
623
  vars->sym_ptr_ptr ++;
624
  vars->table_ptr ++;
625
  vars->native_ptr ++;
626
  vars->esym_ptr ++;
627
  vars->string_ptr += strlen (symbol_name) + strlen (prefix) + 1;
628
 
629
  BFD_ASSERT (vars->string_ptr < vars->end_string_ptr);
630
}
631
 
632
/* Create a section.  */
633
static asection_ptr
634
pe_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;
641
 
642
  sec = bfd_make_section_old_way (vars->abfd, name);
643
  if (sec == NULL)
644
    return NULL;
645
 
646
  flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_IN_MEMORY;
647
 
648
  bfd_set_section_flags (vars->abfd, sec, flags | extra_flags);
649
 
650
  bfd_set_section_alignment (vars->abfd, sec, 2);
651
 
652
  /* Check that we will not run out of space.  */
653
  BFD_ASSERT (vars->data + size < vars->bim->buffer + vars->bim->size);
654
 
655
  /* Set the section size and contents.  The actual
656
     contents are filled in by our parent.  */
657
  bfd_set_section_size (vars->abfd, sec, (bfd_size_type) size);
658
  sec->contents = vars->data;
659
  sec->target_index = vars->sec_index ++;
660
 
661
  /* Advance data pointer in the vars structure.  */
662
  vars->data += size;
663
 
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 --;
670
 
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);
676
 
677
  /* Create a symbol to refer to this section.  */
678
  pe_ILF_make_a_symbol (vars, "", name, sec, BSF_LOCAL);
679
 
680
  /* Cache the index to the symbol in the coff_section_data structure.  */
681
  coff_section_data (vars->abfd, sec)->i = vars->sym_index - 1;
682
 
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
 
696
typedef struct
697
{
698
  unsigned short magic;
699
  unsigned char  data[MAX_TEXT_SECTION_SIZE];
700
  unsigned int   size;
701
  unsigned int   offset;
702
}
703
jump_table;
704
 
705
static jump_table jtab[] =
706
{
707
#ifdef I386MAGIC
708
  { I386MAGIC,
709
    { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 },
710
    8, 2
711
  },
712
#endif
713
 
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
724
 
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
732
 
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
740
 
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.  */
756
static boolean
757
pe_ILF_build_a_bfd (bfd *           abfd,
758
                    unsigned int    magic,
759
                    bfd_byte *      symbol_name,
760
                    bfd_byte *      source_dll,
761
                    unsigned int    ordinal,
762
                    unsigned int    types)
763
{
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;
769
  asection_ptr             id4, id5, id6 = NULL, text = NULL;
770
  coff_symbol_type **      imp_sym;
771
  unsigned int             imp_index;
772
 
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;
782
 
783
    case IMPORT_CONST:
784
      /* XXX code yet to be written.  */
785
      _bfd_error_handler (_("%s: Unhandled import type; %x"),
786
                          bfd_archive_filename (abfd), import_type);
787
      return false;
788
 
789
    default:
790
      _bfd_error_handler (_("%s: Unrecognised import type; %x"),
791
                          bfd_archive_filename (abfd), import_type);
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;
802
 
803
    default:
804
      _bfd_error_handler (_("%s: Unrecognised import name type; %x"),
805
                          bfd_archive_filename (abfd), import_name_type);
806
      return false;
807
    }
808
 
809
  /* Initialise local variables.
810
 
811
     Note these are kept in a structure rather than being
812
     declared as statics since bfd frowns on global variables.
813
 
814
     We are going to construct the contents of the BFD in memory,
815
     so allocate all the space that we will need right now.  */
816
  ptr = bfd_zalloc (abfd, (bfd_size_type) ILF_DATA_SIZE);
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);
825
 
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;
832
 
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;
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;
844
 
845
  vars.esym_table = (SYMENT *) ptr;
846
  vars.esym_ptr   = (SYMENT *) ptr;
847
  ptr += SIZEOF_ILF_EXT_SYMS;
848
 
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;
860
 
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;
867
 
868
  /* Create the initial .idata$<n> sections:
869
     [.idata$2:  Import Directory Table -- not needed]
870
     .idata$4:  Import Lookup Table
871
     .idata$5:  Import Address Table
872
 
873
     Note we do not create a .idata$3 section as this is
874
     created for us by the linker script.  */
875
  id4 = pe_ILF_make_a_section (& vars, ".idata$4", SIZEOF_IDATA4, 0);
876
  id5 = pe_ILF_make_a_section (& vars, ".idata$5", SIZEOF_IDATA5, 0);
877
  if (id4 == NULL || id5 == NULL)
878
    return false;
879
 
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 ();
886
 
887
      * (unsigned int *) id4->contents = ordinal | 0x80000000;
888
      * (unsigned int *) id5->contents = ordinal | 0x80000000;
889
    }
890
  else
891
    {
892
      char * symbol;
893
 
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
        }
915
 
916
      id6->contents[0] = ordinal & 0xff;
917
      id6->contents[1] = ordinal >> 8;
918
 
919
      strcpy (id6->contents + 2, symbol);
920
    }
921
 
922
  if (import_name_type != IMPORT_ORDINAL)
923
    {
924
      pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
925
      pe_ILF_save_relocs (&vars, id4);
926
 
927
      pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
928
      pe_ILF_save_relocs (&vars, id5);
929
    }
930
 
931
  /* Create extra sections depending upon the type of import we are dealing with.  */
932
  switch (import_type)
933
    {
934
      int i;
935
 
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
 
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;
962
 
963
      /* Create a reloc for the data in the text section.  */
964
#ifdef MIPS_ARCH_MAGIC_WINCE
965
      if (magic == MIPS_ARCH_MAGIC_WINCE)
966
        {
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);
974
        }
975
      else
976
#endif
977
        pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) jtab[i].offset,
978
                                    BFD_RELOC_32, (asymbol **) imp_sym,
979
                                    imp_index);
980
 
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
    }
991
 
992
  /* Initialise the bfd.  */
993
  memset (& internal_f, 0, sizeof (internal_f));
994
 
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 ?  */
999
 
1000
  if (   ! bfd_set_start_address (abfd, (bfd_vma) 0)
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;
1008
#ifdef THUMBPEMAGIC
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
1013
 
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);
1028
 
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 = '.';
1037
      break;
1038
 
1039
    case IMPORT_DATA:
1040
      /* Nothing to do here.  */
1041
      break;
1042
 
1043
    default:
1044
      /* XXX code not yet written.  */
1045
      abort ();
1046
    }
1047
 
1048
  /* Point the bfd at the symbol table.  */
1049
  obj_symbols (abfd) = vars.sym_cache;
1050
  bfd_get_symcount (abfd) = vars.sym_index;
1051
 
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;
1057
 
1058
  obj_convert (abfd) = vars.sym_table;
1059
  obj_conv_table_size (abfd) = vars.sym_index;
1060
 
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.  */
1071
static const bfd_target *
1072
pe_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;
1079
  bfd_size_type   size;
1080
  unsigned int    ordinal;
1081
  unsigned int    types;
1082
  unsigned int    magic;
1083
 
1084
  /* Upon entry the first four buyes of the ILF header have
1085
      already been read.  Now read the rest of the header.  */
1086
  if (bfd_bread (buffer, (bfd_size_type) 16, abfd) != 16)
1087
    return NULL;
1088
 
1089
  ptr = buffer;
1090
 
1091
  /*  We do not bother to check the version number.
1092
      version = H_GET_16 (abfd, ptr);  */
1093
  ptr += 2;
1094
 
1095
  machine = H_GET_16 (abfd, ptr);
1096
  ptr += 2;
1097
 
1098
  /* Check that the machine type is recognised.  */
1099
  magic = 0;
1100
 
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;
1108
 
1109
    case IMAGE_FILE_MACHINE_I386:
1110
#ifdef I386MAGIC
1111
      magic = I386MAGIC;
1112
#endif
1113
      break;
1114
 
1115
    case IMAGE_FILE_MACHINE_M68K:
1116
#ifdef MC68AGIC
1117
      magic = MC68MAGIC;
1118
#endif
1119
      break;
1120
 
1121
    case IMAGE_FILE_MACHINE_R3000:
1122
    case IMAGE_FILE_MACHINE_R4000:
1123
    case IMAGE_FILE_MACHINE_R10000:
1124
 
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;
1132
 
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;
1139
 
1140
    case IMAGE_FILE_MACHINE_ARM:
1141
#ifdef ARMPEMAGIC
1142
      magic = ARMPEMAGIC;
1143
#endif
1144
      break;
1145
 
1146
    case IMAGE_FILE_MACHINE_THUMB:
1147
#ifdef THUMBPEMAGIC
1148
      {
1149
        extern const bfd_target TARGET_LITTLE_SYM;
1150
 
1151
        if (abfd->xvec == & TARGET_LITTLE_SYM)
1152
          magic = THUMBPEMAGIC;
1153
      }
1154
#endif
1155
      break;
1156
 
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"),
1163
         bfd_archive_filename (abfd), machine);
1164
      bfd_set_error (bfd_error_malformed_archive);
1165
 
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"),
1175
         bfd_archive_filename (abfd), machine);
1176
      bfd_set_error (bfd_error_wrong_format);
1177
 
1178
      return NULL;
1179
    }
1180
 
1181
  /* We do not bother to check the date.
1182
     date = H_GET_32 (abfd, ptr);  */
1183
  ptr += 4;
1184
 
1185
  size = H_GET_32 (abfd, ptr);
1186
  ptr += 4;
1187
 
1188
  if (size == 0)
1189
    {
1190
      _bfd_error_handler
1191
        (_("%s: size field is zero in Import Library Format header"),
1192
         bfd_archive_filename (abfd));
1193
      bfd_set_error (bfd_error_malformed_archive);
1194
 
1195
      return NULL;
1196
    }
1197
 
1198
  ordinal = H_GET_16 (abfd, ptr);
1199
  ptr += 2;
1200
 
1201
  types = H_GET_16 (abfd, ptr);
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;
1208
 
1209
  if (bfd_bread (ptr, size, abfd) != size)
1210
    {
1211
      bfd_release (abfd, ptr);
1212
      return NULL;
1213
    }
1214
 
1215
  symbol_name = ptr;
1216
  source_dll  = ptr + strlen (ptr) + 1;
1217
 
1218
  /* Verify that the strings are null terminated.  */
1219
  if (ptr[size - 1] != 0 || ((unsigned long) (source_dll - ptr) >= size))
1220
    {
1221
      _bfd_error_handler
1222
        (_("%s: string not null terminated in ILF object file."),
1223
         bfd_archive_filename (abfd));
1224
      bfd_set_error (bfd_error_malformed_archive);
1225
      bfd_release (abfd, ptr);
1226
      return NULL;
1227
    }
1228
 
1229
  /* Now construct the bfd.  */
1230
  if (! pe_ILF_build_a_bfd (abfd, magic, symbol_name,
1231
                            source_dll, ordinal, types))
1232
    {
1233
      bfd_release (abfd, ptr);
1234
      return NULL;
1235
    }
1236
 
1237
  return abfd->xvec;
1238
}
1239
 
1240
static const bfd_target *
1241
pe_bfd_object_p (bfd * abfd)
1242
{
1243
  bfd_byte buffer[4];
1244
  struct external_PEI_DOS_hdr dos_hdr;
1245
  struct external_PEI_IMAGE_hdr image_hdr;
1246
  file_ptr offset;
1247
 
1248
  /* Detect if this a Microsoft Import Library Format element.  */
1249
  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1250
      || bfd_bread (buffer, (bfd_size_type) 4, abfd) != 4)
1251
    {
1252
      if (bfd_get_error () != bfd_error_system_call)
1253
        bfd_set_error (bfd_error_wrong_format);
1254
      return NULL;
1255
    }
1256
 
1257
  if (H_GET_32 (abfd, buffer) == 0xffff0000)
1258
    return pe_ILF_object_p (abfd);
1259
 
1260
  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1261
      || bfd_bread (&dos_hdr, (bfd_size_type) sizeof (dos_hdr), abfd)
1262
         != sizeof (dos_hdr))
1263
    {
1264
      if (bfd_get_error () != bfd_error_system_call)
1265
        bfd_set_error (bfd_error_wrong_format);
1266
      return NULL;
1267
    }
1268
 
1269
  /* There are really two magic numbers involved; the magic number
1270
     that says this is a NT executable (PEI) and the magic number that
1271
     determines the architecture.  The former is DOSMAGIC, stored in
1272
     the e_magic field.  The latter is stored in the f_magic field.
1273
     If the NT magic number isn't valid, the architecture magic number
1274
     could be mimicked by some other field (specifically, the number
1275
     of relocs in section 3).  Since this routine can only be called
1276
     correctly for a PEI file, check the e_magic number here, and, if
1277
     it doesn't match, clobber the f_magic number so that we don't get
1278
     a false match.  */
1279
  if (H_GET_16 (abfd, dos_hdr.e_magic) != DOSMAGIC)
1280
    {
1281
      bfd_set_error (bfd_error_wrong_format);
1282
      return NULL;
1283
    }
1284
 
1285
  offset = H_GET_32 (abfd, dos_hdr.e_lfanew);
1286
  if (bfd_seek (abfd, offset, SEEK_SET) != 0
1287
      || (bfd_bread (&image_hdr, (bfd_size_type) sizeof (image_hdr), abfd)
1288
          != sizeof (image_hdr)))
1289
    {
1290
      if (bfd_get_error () != bfd_error_system_call)
1291
        bfd_set_error (bfd_error_wrong_format);
1292
      return NULL;
1293
    }
1294
 
1295
  if (H_GET_32 (abfd, image_hdr.nt_signature) != 0x4550)
1296
    {
1297
      bfd_set_error (bfd_error_wrong_format);
1298
      return NULL;
1299
    }
1300
 
1301
  /* Here is the hack.  coff_object_p wants to read filhsz bytes to
1302
     pick up the COFF header for PE, see "struct external_PEI_filehdr"
1303
     in include/coff/pe.h.  We adjust so that that will work. */
1304
  if (bfd_seek (abfd, (file_ptr) (offset - sizeof (dos_hdr)), SEEK_SET) != 0)
1305
    {
1306
      if (bfd_get_error () != bfd_error_system_call)
1307
        bfd_set_error (bfd_error_wrong_format);
1308
      return NULL;
1309
    }
1310
 
1311
  return coff_object_p (abfd);
1312
}
1313
 
1314
#define coff_object_p pe_bfd_object_p
1315
#endif /* COFF_IMAGE_WITH_PE */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.