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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-7.2/] [bfd/] [coff-ppc.c] - Blame information for rev 359

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

Line No. Rev Author Line
1 330 jeremybenn
/* BFD back-end for PowerPC Microsoft Portable Executable files.
2
   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3
   2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4
   Free Software Foundation, Inc.
5
 
6
   Original version pieced together by Kim Knuttila (krk@cygnus.com)
7
 
8
   There is nothing new under the sun. This file draws a lot on other
9
   coff files, in particular, those for the rs/6000, alpha, mips, and
10
   intel backends, and the PE work for the arm.
11
 
12
   This file is part of BFD, the Binary File Descriptor library.
13
 
14
   This program is free software; you can redistribute it and/or modify
15
   it under the terms of the GNU General Public License as published by
16
   the Free Software Foundation; either version 3 of the License, or
17
   (at your option) any later version.
18
 
19
   This program is distributed in the hope that it will be useful,
20
   but WITHOUT ANY WARRANTY; without even the implied warranty of
21
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
   GNU General Public License for more details.
23
 
24
   You should have received a copy of the GNU General Public License
25
   along with this program; if not, write to the Free Software
26
   Foundation, 51 Franklin Street - Fifth Floor,
27
   Boston, MA 02110-1301, USA.  */
28
 
29
/* Current State:
30
   - objdump works
31
   - relocs generated by gas
32
   - ld will link files, but they do not run.
33
   - dlltool will not produce correct output in some .reloc cases, and will
34
     not produce the right glue code for dll function calls.  */
35
 
36
#include "sysdep.h"
37
#include "bfd.h"
38
#include "libbfd.h"
39
 
40
#include "coff/powerpc.h"
41
#include "coff/internal.h"
42
 
43
#include "coff/pe.h"
44
 
45
#ifdef BADMAG
46
#undef BADMAG
47
#endif
48
 
49
#define BADMAG(x) PPCBADMAG(x)
50
 
51
#include "libcoff.h"
52
 
53
/* This file is compiled more than once, but we only compile the
54
   final_link routine once.  */
55
extern bfd_boolean ppc_bfd_coff_final_link
56
  PARAMS ((bfd *, struct bfd_link_info *));
57
extern void dump_toc PARAMS ((PTR));
58
 
59
/* The toc is a set of bfd_vma fields. We use the fact that valid
60
   addresses are even (i.e. the bit representing "1" is off) to allow
61
   us to encode a little extra information in the field
62
   - Unallocated addresses are initialized to 1.
63
   - Allocated addresses are even numbers.
64
   The first time we actually write a reference to the toc in the bfd,
65
   we want to record that fact in a fixup file (if it is asked for), so
66
   we keep track of whether or not an address has been written by marking
67
   the low order bit with a "1" upon writing.  */
68
 
69
#define SET_UNALLOCATED(x)  ((x) = 1)
70
#define IS_UNALLOCATED(x)   ((x) == 1)
71
 
72
#define IS_WRITTEN(x)       ((x) & 1)
73
#define MARK_AS_WRITTEN(x)  ((x) |= 1)
74
#define MAKE_ADDR_AGAIN(x)  ((x) &= ~1)
75
 
76
/* Turn on this check if you suspect something amiss in the hash tables.  */
77
#ifdef DEBUG_HASH
78
 
79
/* Need a 7 char string for an eye catcher.  */
80
#define EYE "krkjunk"
81
 
82
#define HASH_CHECK_DCL char eye_catcher[8];
83
#define HASH_CHECK_INIT(ret)      strcpy(ret->eye_catcher, EYE)
84
#define HASH_CHECK(addr) \
85
 if (strcmp(addr->eye_catcher, EYE) != 0) \
86
  { \
87
    fprintf (stderr,\
88
    _("File %s, line %d, Hash check failure, bad eye %8s\n"), \
89
    __FILE__, __LINE__, addr->eye_catcher); \
90
    abort (); \
91
 }
92
 
93
#else
94
 
95
#define HASH_CHECK_DCL
96
#define HASH_CHECK_INIT(ret)
97
#define HASH_CHECK(addr)
98
 
99
#endif
100
 
101
/* In order not to add an int to every hash table item for every coff
102
   linker, we define our own hash table, derived from the coff one.  */
103
 
104
/* PE linker hash table entries.  */
105
 
106
struct ppc_coff_link_hash_entry
107
{
108
  struct coff_link_hash_entry root; /* First entry, as required.  */
109
 
110
  /* As we wonder around the relocs, we'll keep the assigned toc_offset
111
     here.  */
112
  bfd_vma toc_offset;               /* Our addition, as required.  */
113
  int symbol_is_glue;
114
  unsigned long int glue_insn;
115
 
116
  HASH_CHECK_DCL
117
};
118
 
119
/* PE linker hash table.  */
120
 
121
struct ppc_coff_link_hash_table
122
{
123
  struct coff_link_hash_table root; /* First entry, as required.  */
124
};
125
 
126
static struct bfd_hash_entry *ppc_coff_link_hash_newfunc
127
  PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
128
           const char *));
129
static struct bfd_link_hash_table *ppc_coff_link_hash_table_create
130
  PARAMS ((bfd *));
131
static bfd_boolean coff_ppc_relocate_section
132
  PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
133
           struct internal_reloc *, struct internal_syment *, asection **));
134
static reloc_howto_type *coff_ppc_rtype_to_howto
135
  PARAMS ((bfd *, asection *, struct internal_reloc *,
136
           struct coff_link_hash_entry *, struct internal_syment *,
137
           bfd_vma *));
138
 
139
/* Routine to create an entry in the link hash table.  */
140
 
141
static struct bfd_hash_entry *
142
ppc_coff_link_hash_newfunc (entry, table, string)
143
     struct bfd_hash_entry *entry;
144
     struct bfd_hash_table *table;
145
     const char *string;
146
{
147
  struct ppc_coff_link_hash_entry *ret =
148
    (struct ppc_coff_link_hash_entry *) entry;
149
 
150
  /* Allocate the structure if it has not already been allocated by a
151
     subclass.  */
152
  if (ret == (struct ppc_coff_link_hash_entry *) NULL)
153
    ret = (struct ppc_coff_link_hash_entry *)
154
      bfd_hash_allocate (table,
155
                         sizeof (struct ppc_coff_link_hash_entry));
156
 
157
  if (ret == (struct ppc_coff_link_hash_entry *) NULL)
158
    return NULL;
159
 
160
  /* Call the allocation method of the superclass.  */
161
  ret = ((struct ppc_coff_link_hash_entry *)
162
         _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry *) ret,
163
                                      table, string));
164
 
165
  if (ret)
166
    {
167
      /* Initialize the local fields.  */
168
      SET_UNALLOCATED (ret->toc_offset);
169
      ret->symbol_is_glue = 0;
170
      ret->glue_insn = 0;
171
 
172
      HASH_CHECK_INIT (ret);
173
    }
174
 
175
  return (struct bfd_hash_entry *) ret;
176
}
177
 
178
/* Initialize a PE linker hash table.  */
179
 
180
static bfd_boolean
181
ppc_coff_link_hash_table_init (struct ppc_coff_link_hash_table *table,
182
                               bfd *abfd,
183
                               struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
184
                                                                  struct bfd_hash_table *,
185
                                                                  const char *),
186
                               unsigned int entsize)
187
{
188
  return _bfd_coff_link_hash_table_init (&table->root, abfd, newfunc, entsize);
189
}
190
 
191
/* Create a PE linker hash table.  */
192
 
193
static struct bfd_link_hash_table *
194
ppc_coff_link_hash_table_create (abfd)
195
     bfd *abfd;
196
{
197
  struct ppc_coff_link_hash_table *ret;
198
  bfd_size_type amt = sizeof (struct ppc_coff_link_hash_table);
199
 
200
  ret = (struct ppc_coff_link_hash_table *) bfd_malloc (amt);
201
  if (ret == NULL)
202
    return NULL;
203
  if (!ppc_coff_link_hash_table_init (ret, abfd,
204
                                      ppc_coff_link_hash_newfunc,
205
                                      sizeof (struct ppc_coff_link_hash_entry)))
206
    {
207
      free (ret);
208
      return (struct bfd_link_hash_table *) NULL;
209
    }
210
  return &ret->root.root;
211
}
212
 
213
/* Now, tailor coffcode.h to use our hash stuff.  */
214
 
215
#define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
216
 
217
/* The nt loader points the toc register to &toc + 32768, in order to
218
   use the complete range of a 16-bit displacement. We have to adjust
219
   for this when we fix up loads displaced off the toc reg.  */
220
#define TOC_LOAD_ADJUSTMENT (-32768)
221
#define TOC_SECTION_NAME ".private.toc"
222
 
223
/* The main body of code is in coffcode.h.  */
224
 
225
#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
226
 
227
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
228
   from smaller values.  Start with zero, widen, *then* decrement.  */
229
#define MINUS_ONE       (((bfd_vma)0) - 1)
230
 
231
/* These should definitely go in a header file somewhere...  */
232
 
233
/* NOP */
234
#define IMAGE_REL_PPC_ABSOLUTE          0x0000
235
 
236
/* 64-bit address */
237
#define IMAGE_REL_PPC_ADDR64            0x0001
238
 
239
/* 32-bit address */
240
#define IMAGE_REL_PPC_ADDR32            0x0002
241
 
242
/* 26-bit address, shifted left 2 (branch absolute) */
243
#define IMAGE_REL_PPC_ADDR24            0x0003
244
 
245
/* 16-bit address */
246
#define IMAGE_REL_PPC_ADDR16            0x0004
247
 
248
/* 16-bit address, shifted left 2 (load doubleword) */
249
#define IMAGE_REL_PPC_ADDR14            0x0005
250
 
251
/* 26-bit PC-relative offset, shifted left 2 (branch relative) */
252
#define IMAGE_REL_PPC_REL24             0x0006
253
 
254
/* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
255
#define IMAGE_REL_PPC_REL14             0x0007
256
 
257
/* 16-bit offset from TOC base */
258
#define IMAGE_REL_PPC_TOCREL16          0x0008
259
 
260
/* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
261
#define IMAGE_REL_PPC_TOCREL14          0x0009
262
 
263
/* 32-bit addr w/o image base */
264
#define IMAGE_REL_PPC_ADDR32NB          0x000A
265
 
266
/* va of containing section (as in an image sectionhdr) */
267
#define IMAGE_REL_PPC_SECREL            0x000B
268
 
269
/* sectionheader number */
270
#define IMAGE_REL_PPC_SECTION           0x000C
271
 
272
/* substitute TOC restore instruction iff symbol is glue code */
273
#define IMAGE_REL_PPC_IFGLUE            0x000D
274
 
275
/* symbol is glue code; virtual address is TOC restore instruction */
276
#define IMAGE_REL_PPC_IMGLUE            0x000E
277
 
278
/* va of containing section (limited to 16 bits) */
279
#define IMAGE_REL_PPC_SECREL16          0x000F
280
 
281
/* Stuff to handle immediate data when the number of bits in the
282
   data is greater than the number of bits in the immediate field
283
   We need to do (usually) 32 bit arithmetic on 16 bit chunks.  */
284
#define IMAGE_REL_PPC_REFHI             0x0010
285
#define IMAGE_REL_PPC_REFLO             0x0011
286
#define IMAGE_REL_PPC_PAIR              0x0012
287
 
288
/* This is essentially the same as tocrel16, with TOCDEFN assumed.  */
289
#define IMAGE_REL_PPC_TOCREL16_DEFN     0x0013
290
 
291
/* Flag bits in IMAGE_RELOCATION.TYPE.  */
292
 
293
/* Subtract reloc value rather than adding it.  */
294
#define IMAGE_REL_PPC_NEG               0x0100
295
 
296
/* Fix branch prediction bit to predict branch taken.  */
297
#define IMAGE_REL_PPC_BRTAKEN           0x0200
298
 
299
/* Fix branch prediction bit to predict branch not taken.  */
300
#define IMAGE_REL_PPC_BRNTAKEN          0x0400
301
 
302
/* TOC slot defined in file (or, data in toc).  */
303
#define IMAGE_REL_PPC_TOCDEFN           0x0800
304
 
305
/* Masks to isolate above values in IMAGE_RELOCATION.Type.  */
306
#define IMAGE_REL_PPC_TYPEMASK          0x00FF
307
#define IMAGE_REL_PPC_FLAGMASK          0x0F00
308
 
309
#define EXTRACT_TYPE(x)                 ((x) & IMAGE_REL_PPC_TYPEMASK)
310
#define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
311
#define EXTRACT_JUNK(x)  \
312
           ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
313
 
314
/* Static helper functions to make relocation work.  */
315
/* (Work In Progress) */
316
 
317
static bfd_reloc_status_type ppc_refhi_reloc PARAMS ((bfd *abfd,
318
                                                      arelent *reloc,
319
                                                      asymbol *symbol,
320
                                                      PTR data,
321
                                                      asection *section,
322
                                                      bfd *output_bfd,
323
                                                      char **error));
324
static bfd_reloc_status_type ppc_pair_reloc PARAMS ((bfd *abfd,
325
                                                     arelent *reloc,
326
                                                     asymbol *symbol,
327
                                                     PTR data,
328
                                                     asection *section,
329
                                                     bfd *output_bfd,
330
                                                     char **error));
331
 
332
static bfd_reloc_status_type ppc_toc16_reloc PARAMS ((bfd *abfd,
333
                                                      arelent *reloc,
334
                                                      asymbol *symbol,
335
                                                      PTR data,
336
                                                      asection *section,
337
                                                      bfd *output_bfd,
338
                                                      char **error));
339
 
340
static bfd_reloc_status_type ppc_section_reloc PARAMS ((bfd *abfd,
341
                                                        arelent *reloc,
342
                                                        asymbol *symbol,
343
                                                        PTR data,
344
                                                        asection *section,
345
                                                        bfd *output_bfd,
346
                                                        char **error));
347
 
348
static bfd_reloc_status_type ppc_secrel_reloc PARAMS ((bfd *abfd,
349
                                                       arelent *reloc,
350
                                                       asymbol *symbol,
351
                                                       PTR data,
352
                                                       asection *section,
353
                                                       bfd *output_bfd,
354
                                                       char **error));
355
 
356
static bfd_reloc_status_type ppc_imglue_reloc PARAMS ((bfd *abfd,
357
                                                       arelent *reloc,
358
                                                       asymbol *symbol,
359
                                                       PTR data,
360
                                                       asection *section,
361
                                                       bfd *output_bfd,
362
                                                       char **error));
363
 
364
static bfd_boolean in_reloc_p PARAMS((bfd *abfd, reloc_howto_type *howto));
365
 
366
/* FIXME: It'll take a while to get through all of these. I only need a few to
367
   get us started, so those I'll make sure work. Those marked FIXME are either
368
   completely unverified or have a specific unknown marked in the comment.  */
369
 
370
/* Relocation entries for Windows/NT on PowerPC.
371
 
372
   From the document "" we find the following listed as used relocs:
373
 
374
     ABSOLUTE       : The noop
375
     ADDR[64|32|16] : fields that hold addresses in data fields or the
376
                      16 bit displacement field on a load/store.
377
     ADDR[24|14]    : fields that hold addresses in branch and cond
378
                      branches. These represent [26|16] bit addresses.
379
                      The low order 2 bits are preserved.
380
     REL[24|14]     : branches relative to the Instruction Address
381
                      register. These represent [26|16] bit addresses,
382
                      as before. The instruction field will be zero, and
383
                      the address of the SYM will be inserted at link time.
384
     TOCREL16       : 16 bit displacement field referring to a slot in
385
                      toc.
386
     TOCREL14       : 16 bit displacement field, similar to REL14 or ADDR14.
387
     ADDR32NB       : 32 bit address relative to the virtual origin.
388
                      (On the alpha, this is always a linker generated thunk)
389
                      (i.e. 32bit addr relative to the image base)
390
     SECREL         : The value is relative to the start of the section
391
                      containing the symbol.
392
     SECTION        : access to the header containing the item. Supports the
393
                      codeview debugger.
394
 
395
   In particular, note that the document does not indicate that the
396
   relocations listed in the header file are used.  */
397
 
398
 
399
static reloc_howto_type ppc_coff_howto_table[] =
400
{
401
  /* IMAGE_REL_PPC_ABSOLUTE 0x0000   NOP */
402
  /* Unused: */
403
  HOWTO (IMAGE_REL_PPC_ABSOLUTE, /* type */
404
         0,                       /* rightshift */
405
         0,                       /* size (0 = byte, 1 = short, 2 = long) */
406
         0,                       /* bitsize */
407
         FALSE,                  /* pc_relative */
408
         0,                       /* bitpos */
409
         complain_overflow_dont, /* dont complain_on_overflow */
410
         0,                       /* special_function */
411
         "ABSOLUTE",             /* name */
412
         FALSE,                  /* partial_inplace */
413
         0x00,                   /* src_mask */
414
         0x00,                   /* dst_mask */
415
         FALSE),                 /* pcrel_offset */
416
 
417
  /* IMAGE_REL_PPC_ADDR64 0x0001  64-bit address */
418
  /* Unused: */
419
  HOWTO(IMAGE_REL_PPC_ADDR64,    /* type */
420
        0,                        /* rightshift */
421
        3,                       /* size (0 = byte, 1 = short, 2 = long) */
422
        64,                      /* bitsize */
423
        FALSE,                   /* pc_relative */
424
        0,                        /* bitpos */
425
        complain_overflow_bitfield,      /* complain_on_overflow */
426
        0,                        /* special_function */
427
        "ADDR64",               /* name */
428
        TRUE,                    /* partial_inplace */
429
        MINUS_ONE,               /* src_mask */
430
        MINUS_ONE,               /* dst_mask */
431
        FALSE),                 /* pcrel_offset */
432
 
433
  /* IMAGE_REL_PPC_ADDR32 0x0002  32-bit address */
434
  /* Used: */
435
  HOWTO (IMAGE_REL_PPC_ADDR32,  /* type */
436
         0,                      /* rightshift */
437
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
438
         32,                    /* bitsize */
439
         FALSE,                 /* pc_relative */
440
         0,                      /* bitpos */
441
         complain_overflow_bitfield, /* complain_on_overflow */
442
         0,                      /* special_function */
443
         "ADDR32",              /* name */
444
         TRUE,                  /* partial_inplace */
445
         0xffffffff,            /* src_mask */
446
         0xffffffff,            /* dst_mask */
447
         FALSE),                /* pcrel_offset */
448
 
449
  /* IMAGE_REL_PPC_ADDR24 0x0003  26-bit address, shifted left 2 (branch absolute) */
450
  /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
451
  /* Of course, That's the IBM approved bit numbering, which is not what */
452
  /* anyone else uses.... The li field is in bit 2 thru 25 */
453
  /* Used: */
454
  HOWTO (IMAGE_REL_PPC_ADDR24,  /* type */
455
         0,                      /* rightshift */
456
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
457
         26,                    /* bitsize */
458
         FALSE,                 /* pc_relative */
459
         0,                      /* bitpos */
460
         complain_overflow_bitfield, /* complain_on_overflow */
461
         0,                      /* special_function */
462
         "ADDR24",              /* name */
463
         TRUE,                  /* partial_inplace */
464
         0x07fffffc,            /* src_mask */
465
         0x07fffffc,            /* dst_mask */
466
         FALSE),                /* pcrel_offset */
467
 
468
  /* IMAGE_REL_PPC_ADDR16 0x0004  16-bit address */
469
  /* Used: */
470
  HOWTO (IMAGE_REL_PPC_ADDR16,  /* type */
471
         0,                      /* rightshift */
472
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
473
         16,                    /* bitsize */
474
         FALSE,                 /* pc_relative */
475
         0,                      /* bitpos */
476
         complain_overflow_signed, /* complain_on_overflow */
477
         0,                      /* special_function */
478
         "ADDR16",              /* name */
479
         TRUE,                  /* partial_inplace */
480
         0xffff,                /* src_mask */
481
         0xffff,                /* dst_mask */
482
         FALSE),                /* pcrel_offset */
483
 
484
  /* IMAGE_REL_PPC_ADDR14 0x0005 */
485
  /*  16-bit address, shifted left 2 (load doubleword) */
486
  /* FIXME: the mask is likely wrong, and the bit position may be as well */
487
  /* Unused: */
488
  HOWTO (IMAGE_REL_PPC_ADDR14,  /* type */
489
         1,                     /* rightshift */
490
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
491
         16,                    /* bitsize */
492
         FALSE,                 /* pc_relative */
493
         0,                      /* bitpos */
494
         complain_overflow_signed, /* complain_on_overflow */
495
         0,                      /* special_function */
496
         "ADDR16",              /* name */
497
         TRUE,                  /* partial_inplace */
498
         0xffff,                /* src_mask */
499
         0xffff,                /* dst_mask */
500
         FALSE),                /* pcrel_offset */
501
 
502
  /* IMAGE_REL_PPC_REL24 0x0006 */
503
  /*   26-bit PC-relative offset, shifted left 2 (branch relative) */
504
  /* Used: */
505
  HOWTO (IMAGE_REL_PPC_REL24,   /* type */
506
         0,                      /* rightshift */
507
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
508
         26,                    /* bitsize */
509
         TRUE,                  /* pc_relative */
510
         0,                      /* bitpos */
511
         complain_overflow_signed, /* complain_on_overflow */
512
         0,                      /* special_function */
513
         "REL24",               /* name */
514
         TRUE,                  /* partial_inplace */
515
         0x3fffffc,             /* src_mask */
516
         0x3fffffc,             /* dst_mask */
517
         FALSE),                /* pcrel_offset */
518
 
519
  /* IMAGE_REL_PPC_REL14 0x0007 */
520
  /*   16-bit PC-relative offset, shifted left 2 (br cond relative) */
521
  /* FIXME: the mask is likely wrong, and the bit position may be as well */
522
  /* FIXME: how does it know how far to shift? */
523
  /* Unused: */
524
  HOWTO (IMAGE_REL_PPC_ADDR14,  /* type */
525
         1,                     /* rightshift */
526
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
527
         16,                    /* bitsize */
528
         FALSE,                 /* pc_relative */
529
         0,                      /* bitpos */
530
         complain_overflow_signed, /* complain_on_overflow */
531
         0,                      /* special_function */
532
         "ADDR16",              /* name */
533
         TRUE,                  /* partial_inplace */
534
         0xffff,                /* src_mask */
535
         0xffff,                /* dst_mask */
536
         TRUE),                 /* pcrel_offset */
537
 
538
  /* IMAGE_REL_PPC_TOCREL16 0x0008 */
539
  /*   16-bit offset from TOC base */
540
  /* Used: */
541
  HOWTO (IMAGE_REL_PPC_TOCREL16,/* type */
542
         0,                      /* rightshift */
543
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
544
         16,                    /* bitsize */
545
         FALSE,                 /* pc_relative */
546
         0,                      /* bitpos */
547
         complain_overflow_dont, /* complain_on_overflow */
548
         ppc_toc16_reloc,       /* special_function */
549
         "TOCREL16",            /* name */
550
         FALSE,                 /* partial_inplace */
551
         0xffff,                /* src_mask */
552
         0xffff,                /* dst_mask */
553
         FALSE),                /* pcrel_offset */
554
 
555
  /* IMAGE_REL_PPC_TOCREL14 0x0009 */
556
  /*   16-bit offset from TOC base, shifted left 2 (load doubleword) */
557
  /* Unused: */
558
  HOWTO (IMAGE_REL_PPC_TOCREL14,/* type */
559
         1,                     /* rightshift */
560
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
561
         16,                    /* bitsize */
562
         FALSE,                 /* pc_relative */
563
         0,                      /* bitpos */
564
         complain_overflow_signed, /* complain_on_overflow */
565
         0,                      /* special_function */
566
         "TOCREL14",            /* name */
567
         FALSE,                 /* partial_inplace */
568
         0xffff,                /* src_mask */
569
         0xffff,                /* dst_mask */
570
         FALSE),                /* pcrel_offset */
571
 
572
  /* IMAGE_REL_PPC_ADDR32NB 0x000A */
573
  /*   32-bit addr w/ image base */
574
  /* Unused: */
575
  HOWTO (IMAGE_REL_PPC_ADDR32NB,/* type */
576
         0,                      /* rightshift */
577
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
578
         32,                    /* bitsize */
579
         FALSE,                 /* pc_relative */
580
         0,                      /* bitpos */
581
         complain_overflow_signed, /* complain_on_overflow */
582
         0,                     /* special_function */
583
         "ADDR32NB",            /* name */
584
         TRUE,                  /* partial_inplace */
585
         0xffffffff,            /* src_mask */
586
         0xffffffff,            /* dst_mask */
587
         FALSE),                 /* pcrel_offset */
588
 
589
  /* IMAGE_REL_PPC_SECREL 0x000B */
590
  /*   va of containing section (as in an image sectionhdr) */
591
  /* Unused: */
592
  HOWTO (IMAGE_REL_PPC_SECREL,/* type */
593
         0,                      /* rightshift */
594
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
595
         32,                    /* bitsize */
596
         FALSE,                 /* pc_relative */
597
         0,                      /* bitpos */
598
         complain_overflow_signed, /* complain_on_overflow */
599
         ppc_secrel_reloc,      /* special_function */
600
         "SECREL",              /* name */
601
         TRUE,                  /* partial_inplace */
602
         0xffffffff,            /* src_mask */
603
         0xffffffff,            /* dst_mask */
604
         TRUE),                 /* pcrel_offset */
605
 
606
  /* IMAGE_REL_PPC_SECTION 0x000C */
607
  /*   sectionheader number */
608
  /* Unused: */
609
  HOWTO (IMAGE_REL_PPC_SECTION,/* type */
610
         0,                      /* rightshift */
611
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
612
         32,                    /* bitsize */
613
         FALSE,                 /* pc_relative */
614
         0,                      /* bitpos */
615
         complain_overflow_signed, /* complain_on_overflow */
616
         ppc_section_reloc,     /* special_function */
617
         "SECTION",             /* name */
618
         TRUE,                  /* partial_inplace */
619
         0xffffffff,            /* src_mask */
620
         0xffffffff,            /* dst_mask */
621
         TRUE),                 /* pcrel_offset */
622
 
623
  /* IMAGE_REL_PPC_IFGLUE 0x000D */
624
  /*   substitute TOC restore instruction iff symbol is glue code */
625
  /* Used: */
626
  HOWTO (IMAGE_REL_PPC_IFGLUE,/* type */
627
         0,                      /* rightshift */
628
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
629
         32,                    /* bitsize */
630
         FALSE,                 /* pc_relative */
631
         0,                      /* bitpos */
632
         complain_overflow_signed, /* complain_on_overflow */
633
         0,                      /* special_function */
634
         "IFGLUE",              /* name */
635
         TRUE,                  /* partial_inplace */
636
         0xffffffff,            /* src_mask */
637
         0xffffffff,            /* dst_mask */
638
         FALSE),                /* pcrel_offset */
639
 
640
  /* IMAGE_REL_PPC_IMGLUE 0x000E */
641
  /*   symbol is glue code; virtual address is TOC restore instruction */
642
  /* Unused: */
643
  HOWTO (IMAGE_REL_PPC_IMGLUE,/* type */
644
         0,                      /* rightshift */
645
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
646
         32,                    /* bitsize */
647
         FALSE,                 /* pc_relative */
648
         0,                      /* bitpos */
649
         complain_overflow_dont, /* complain_on_overflow */
650
         ppc_imglue_reloc,      /* special_function */
651
         "IMGLUE",              /* name */
652
         FALSE,                 /* partial_inplace */
653
         0xffffffff,            /* src_mask */
654
         0xffffffff,            /* dst_mask */
655
         FALSE),                 /* pcrel_offset */
656
 
657
  /* IMAGE_REL_PPC_SECREL16 0x000F */
658
  /*   va of containing section (limited to 16 bits) */
659
  /* Unused: */
660
  HOWTO (IMAGE_REL_PPC_SECREL16,/* type */
661
         0,                      /* rightshift */
662
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
663
         16,                    /* bitsize */
664
         FALSE,                 /* pc_relative */
665
         0,                      /* bitpos */
666
         complain_overflow_signed, /* complain_on_overflow */
667
         0,                      /* special_function */
668
         "SECREL16",            /* name */
669
         TRUE,                  /* partial_inplace */
670
         0xffff,                /* src_mask */
671
         0xffff,                /* dst_mask */
672
         TRUE),                 /* pcrel_offset */
673
 
674
  /* IMAGE_REL_PPC_REFHI             0x0010 */
675
  /* Unused: */
676
  HOWTO (IMAGE_REL_PPC_REFHI,   /* type */
677
         0,                      /* rightshift */
678
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
679
         16,                    /* bitsize */
680
         FALSE,                 /* pc_relative */
681
         0,                      /* bitpos */
682
         complain_overflow_signed, /* complain_on_overflow */
683
         ppc_refhi_reloc,       /* special_function */
684
         "REFHI",               /* name */
685
         TRUE,                  /* partial_inplace */
686
         0xffffffff,            /* src_mask */
687
         0xffffffff,            /* dst_mask */
688
         FALSE),                 /* pcrel_offset */
689
 
690
  /* IMAGE_REL_PPC_REFLO             0x0011 */
691
  /* Unused: */
692
  HOWTO (IMAGE_REL_PPC_REFLO,   /* type */
693
         0,                      /* rightshift */
694
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
695
         16,                    /* bitsize */
696
         FALSE,                 /* pc_relative */
697
         0,                      /* bitpos */
698
         complain_overflow_signed, /* complain_on_overflow */
699
         ppc_refhi_reloc,       /* special_function */
700
         "REFLO",               /* name */
701
         TRUE,                  /* partial_inplace */
702
         0xffffffff,            /* src_mask */
703
         0xffffffff,            /* dst_mask */
704
         FALSE),                /* pcrel_offset */
705
 
706
  /* IMAGE_REL_PPC_PAIR              0x0012 */
707
  /* Unused: */
708
  HOWTO (IMAGE_REL_PPC_PAIR,    /* type */
709
         0,                      /* rightshift */
710
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
711
         16,                    /* bitsize */
712
         FALSE,                 /* pc_relative */
713
         0,                      /* bitpos */
714
         complain_overflow_signed, /* complain_on_overflow */
715
         ppc_pair_reloc,        /* special_function */
716
         "PAIR",                /* name */
717
         TRUE,                  /* partial_inplace */
718
         0xffffffff,            /* src_mask */
719
         0xffffffff,            /* dst_mask */
720
         FALSE),                /* pcrel_offset */
721
 
722
  /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
723
  /*   16-bit offset from TOC base, without causing a definition */
724
  /* Used: */
725
  HOWTO ( (IMAGE_REL_PPC_TOCREL16 | IMAGE_REL_PPC_TOCDEFN), /* type */
726
         0,                      /* rightshift */
727
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
728
         16,                    /* bitsize */
729
         FALSE,                 /* pc_relative */
730
         0,                      /* bitpos */
731
         complain_overflow_dont, /* complain_on_overflow */
732
         0,                     /* special_function */
733
         "TOCREL16, TOCDEFN",   /* name */
734
         FALSE,                 /* partial_inplace */
735
         0xffff,                /* src_mask */
736
         0xffff,                /* dst_mask */
737
         FALSE),                /* pcrel_offset */
738
 
739
};
740
 
741
/* Some really cheezy macros that can be turned on to test stderr :-)  */
742
 
743
#ifdef DEBUG_RELOC
744
#define UN_IMPL(x)                                           \
745
{                                                            \
746
   static int i;                                             \
747
   if (i == 0)                                               \
748
     {                                                       \
749
       i = 1;                                                \
750
       fprintf (stderr,_("Unimplemented Relocation -- %s\n"),x); \
751
     }                                                       \
752
}
753
 
754
#define DUMP_RELOC(n,r)                              \
755
{                                                    \
756
   fprintf (stderr,"%s sym %d, addr %d, addend %d\n", \
757
           n, (*(r->sym_ptr_ptr))->name,             \
758
           r->address, r->addend);                   \
759
}
760
 
761
/* Given a reloc name, n, and a pointer to an internal_reloc,
762
   dump out interesting information on the contents
763
 
764
#define n_name          _n._n_name
765
#define n_zeroes        _n._n_n._n_zeroes
766
#define n_offset        _n._n_n._n_offset  */
767
 
768
#define DUMP_RELOC2(n,r)                                \
769
{                                                       \
770
   fprintf (stderr,"%s sym %d, r_vaddr %d %s\n",        \
771
           n, r->r_symndx, r->r_vaddr,                  \
772
           (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
773
           ?" ":" TOCDEFN"  );                          \
774
}
775
 
776
#else
777
#define UN_IMPL(x)
778
#define DUMP_RELOC(n,r)
779
#define DUMP_RELOC2(n,r)
780
#endif
781
 
782
/* TOC construction and management routines.  */
783
 
784
/* This file is compiled twice, and these variables are defined in one
785
   of the compilations.  FIXME: This is confusing and weird.  Also,
786
   BFD should not use global variables.  */
787
extern bfd *    bfd_of_toc_owner;
788
extern long int global_toc_size;
789
extern long int import_table_size;
790
extern long int first_thunk_address;
791
extern long int thunk_size;
792
 
793
enum toc_type
794
{
795
  default_toc,
796
  toc_32,
797
  toc_64
798
};
799
 
800
enum ref_category
801
{
802
  priv,
803
  pub,
804
  tocdata
805
};
806
 
807
struct list_ele
808
{
809
  struct list_ele *next;
810
  bfd_vma addr;
811
  enum ref_category cat;
812
  int offset;
813
  const char *name;
814
};
815
 
816
extern struct list_ele *head;
817
extern struct list_ele *tail;
818
 
819
static void record_toc
820
  PARAMS ((asection *, bfd_signed_vma, enum ref_category, const char *));
821
 
822
static void
823
record_toc (toc_section, our_toc_offset, cat, name)
824
     asection *toc_section;
825
     bfd_signed_vma our_toc_offset;
826
     enum ref_category cat;
827
     const char *name;
828
{
829
  /* Add this entry to our toc addr-offset-name list.  */
830
  bfd_size_type amt = sizeof (struct list_ele);
831
  struct list_ele *t = (struct list_ele *) bfd_malloc (amt);
832
 
833
  if (t == NULL)
834
    abort ();
835
  t->next = 0;
836
  t->offset = our_toc_offset;
837
  t->name = name;
838
  t->cat = cat;
839
  t->addr = toc_section->output_offset + our_toc_offset;
840
 
841
  if (head == 0)
842
    {
843
      head = t;
844
      tail = t;
845
    }
846
  else
847
    {
848
      tail->next = t;
849
      tail = t;
850
    }
851
}
852
 
853
#ifdef COFF_IMAGE_WITH_PE
854
 
855
static bfd_boolean ppc_record_toc_entry
856
  PARAMS ((bfd *, struct bfd_link_info *, asection *, int, enum toc_type));
857
static void ppc_mark_symbol_as_glue
858
  PARAMS ((bfd *, int, struct internal_reloc *));
859
 
860
/* Record a toc offset against a symbol.  */
861
static bfd_boolean
862
ppc_record_toc_entry(abfd, info, sec, sym, toc_kind)
863
     bfd *abfd;
864
     struct bfd_link_info *info ATTRIBUTE_UNUSED;
865
     asection *sec ATTRIBUTE_UNUSED;
866
     int sym;
867
     enum toc_type toc_kind ATTRIBUTE_UNUSED;
868
{
869
  struct ppc_coff_link_hash_entry *h;
870
  int *local_syms;
871
 
872
  h = 0;
873
 
874
  h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
875
  if (h != 0)
876
    {
877
      HASH_CHECK(h);
878
    }
879
 
880
  if (h == 0)
881
    {
882
      local_syms = obj_coff_local_toc_table(abfd);
883
 
884
      if (local_syms == 0)
885
        {
886
          unsigned int i;
887
          bfd_size_type amt;
888
 
889
          /* allocate a table */
890
          amt = (bfd_size_type) obj_raw_syment_count (abfd) * sizeof (int);
891
          local_syms = (int *) bfd_zalloc (abfd, amt);
892
          if (local_syms == 0)
893
            return FALSE;
894
          obj_coff_local_toc_table (abfd) = local_syms;
895
 
896
          for (i = 0; i < obj_raw_syment_count (abfd); ++i)
897
            {
898
              SET_UNALLOCATED (local_syms[i]);
899
            }
900
        }
901
 
902
      if (IS_UNALLOCATED(local_syms[sym]))
903
        {
904
          local_syms[sym] = global_toc_size;
905
          global_toc_size += 4;
906
 
907
          /* The size must fit in a 16-bit displacement.  */
908
          if (global_toc_size > 65535)
909
            {
910
              (*_bfd_error_handler) (_("TOC overflow"));
911
              bfd_set_error (bfd_error_file_too_big);
912
              return FALSE;
913
            }
914
        }
915
    }
916
  else
917
    {
918
      /* Check to see if there's a toc slot allocated. If not, do it
919
         here. It will be used in relocate_section.  */
920
      if (IS_UNALLOCATED(h->toc_offset))
921
        {
922
          h->toc_offset = global_toc_size;
923
          global_toc_size += 4;
924
 
925
          /* The size must fit in a 16-bit displacement.  */
926
          if (global_toc_size >= 65535)
927
            {
928
              (*_bfd_error_handler) (_("TOC overflow"));
929
              bfd_set_error (bfd_error_file_too_big);
930
              return FALSE;
931
            }
932
        }
933
    }
934
 
935
  return TRUE;
936
}
937
 
938
/* Record a toc offset against a symbol.  */
939
static void
940
ppc_mark_symbol_as_glue(abfd, sym, rel)
941
     bfd *abfd;
942
     int sym;
943
     struct internal_reloc *rel;
944
{
945
  struct ppc_coff_link_hash_entry *h;
946
 
947
  h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
948
 
949
  HASH_CHECK(h);
950
 
951
  h->symbol_is_glue = 1;
952
  h->glue_insn = bfd_get_32 (abfd, (bfd_byte *) &rel->r_vaddr);
953
 
954
  return;
955
}
956
 
957
#endif /* COFF_IMAGE_WITH_PE */
958
 
959
/* Return TRUE if this relocation should
960
   appear in the output .reloc section.  */
961
 
962
static bfd_boolean in_reloc_p(abfd, howto)
963
     bfd * abfd ATTRIBUTE_UNUSED;
964
     reloc_howto_type *howto;
965
{
966
  return
967
    (! howto->pc_relative)
968
      && (howto->type != IMAGE_REL_PPC_ADDR32NB)
969
      && (howto->type != IMAGE_REL_PPC_TOCREL16)
970
      && (howto->type != IMAGE_REL_PPC_IMGLUE)
971
      && (howto->type != IMAGE_REL_PPC_IFGLUE)
972
      && (howto->type != IMAGE_REL_PPC_SECREL)
973
      && (howto->type != IMAGE_REL_PPC_SECTION)
974
      && (howto->type != IMAGE_REL_PPC_SECREL16)
975
      && (howto->type != IMAGE_REL_PPC_REFHI)
976
      && (howto->type != IMAGE_REL_PPC_REFLO)
977
      && (howto->type != IMAGE_REL_PPC_PAIR)
978
      && (howto->type != IMAGE_REL_PPC_TOCREL16_DEFN) ;
979
}
980
 
981
static bfd_boolean
982
write_base_file_entry (bfd *obfd, struct bfd_link_info *info, bfd_vma addr)
983
{
984
  if (coff_data (obfd)->pe)
985
     addr -= pe_data (obfd)->pe_opthdr.ImageBase;
986
  if (fwrite (&addr, sizeof (addr), 1, (FILE *) info->base_file) == 1)
987
    return TRUE;
988
 
989
  bfd_set_error (bfd_error_system_call);
990
  return FALSE;
991
}
992
 
993
/* The reloc processing routine for the optimized COFF linker.  */
994
 
995
static bfd_boolean
996
coff_ppc_relocate_section (output_bfd, info, input_bfd, input_section,
997
                           contents, relocs, syms, sections)
998
     bfd *output_bfd;
999
     struct bfd_link_info *info;
1000
     bfd *input_bfd;
1001
     asection *input_section;
1002
     bfd_byte *contents;
1003
     struct internal_reloc *relocs;
1004
     struct internal_syment *syms;
1005
     asection **sections;
1006
{
1007
  struct internal_reloc *rel;
1008
  struct internal_reloc *relend;
1009
  asection *toc_section = 0;
1010
  bfd_vma relocation;
1011
  reloc_howto_type *howto = 0;
1012
 
1013
  /* If we are performing a relocatable link, we don't need to do a
1014
     thing.  The caller will take care of adjusting the reloc
1015
     addresses and symbol indices.  */
1016
  if (info->relocatable)
1017
    return TRUE;
1018
 
1019
  rel = relocs;
1020
  relend = rel + input_section->reloc_count;
1021
  for (; rel < relend; rel++)
1022
    {
1023
      long symndx;
1024
      struct ppc_coff_link_hash_entry *h;
1025
      struct internal_syment *sym;
1026
      bfd_vma val;
1027
 
1028
      asection *sec;
1029
      bfd_reloc_status_type rstat;
1030
      bfd_byte *loc;
1031
 
1032
      unsigned short r_type  = EXTRACT_TYPE (rel->r_type);
1033
      unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
1034
 
1035
      symndx = rel->r_symndx;
1036
      loc = contents + rel->r_vaddr - input_section->vma;
1037
 
1038
      /* FIXME: check bounds on r_type */
1039
      howto = ppc_coff_howto_table + r_type;
1040
 
1041
      if (symndx == -1)
1042
        {
1043
          h = NULL;
1044
          sym = NULL;
1045
        }
1046
      else
1047
        {
1048
          h = (struct ppc_coff_link_hash_entry *)
1049
            (obj_coff_sym_hashes (input_bfd)[symndx]);
1050
          if (h != 0)
1051
            {
1052
              HASH_CHECK(h);
1053
            }
1054
 
1055
          sym = syms + symndx;
1056
        }
1057
 
1058
      if (r_type == IMAGE_REL_PPC_IMGLUE && h == 0)
1059
        {
1060
          /* An IMGLUE reloc must have a name. Something is very wrong.  */
1061
          abort ();
1062
        }
1063
 
1064
      sec = NULL;
1065
      val = 0;
1066
 
1067
      /* FIXME: PAIR unsupported in the following code.  */
1068
      if (h == NULL)
1069
        {
1070
          if (symndx == -1)
1071
            sec = bfd_abs_section_ptr;
1072
          else
1073
            {
1074
              sec = sections[symndx];
1075
              val = (sec->output_section->vma
1076
                     + sec->output_offset
1077
                     + sym->n_value);
1078
              if (! obj_pe (output_bfd))
1079
                val -= sec->vma;
1080
            }
1081
        }
1082
      else
1083
        {
1084
          HASH_CHECK(h);
1085
 
1086
          if (h->root.root.type == bfd_link_hash_defined
1087
              || h->root.root.type == bfd_link_hash_defweak)
1088
            {
1089
              sec = h->root.root.u.def.section;
1090
              val = (h->root.root.u.def.value
1091
                     + sec->output_section->vma
1092
                     + sec->output_offset);
1093
            }
1094
          else
1095
            {
1096
              if (! ((*info->callbacks->undefined_symbol)
1097
                     (info, h->root.root.root.string, input_bfd, input_section,
1098
                      rel->r_vaddr - input_section->vma, TRUE)))
1099
                return FALSE;
1100
            }
1101
        }
1102
 
1103
      rstat = bfd_reloc_ok;
1104
 
1105
      /* Each case must do its own relocation, setting rstat appropriately.  */
1106
      switch (r_type)
1107
        {
1108
        default:
1109
          (*_bfd_error_handler)
1110
            (_("%B: unsupported relocation type 0x%02x"), input_bfd, r_type);
1111
          bfd_set_error (bfd_error_bad_value);
1112
          return FALSE;
1113
        case IMAGE_REL_PPC_TOCREL16:
1114
          {
1115
            bfd_signed_vma our_toc_offset;
1116
            int fixit;
1117
 
1118
            DUMP_RELOC2(howto->name, rel);
1119
 
1120
            if (toc_section == 0)
1121
              {
1122
                toc_section = bfd_get_section_by_name (bfd_of_toc_owner,
1123
                                                       TOC_SECTION_NAME);
1124
 
1125
                if ( toc_section == NULL )
1126
                  {
1127
                    /* There is no toc section. Something is very wrong.  */
1128
                    abort ();
1129
                  }
1130
              }
1131
 
1132
            /* Amazing bit tricks present. As we may have seen earlier, we
1133
               use the 1 bit to tell us whether or not a toc offset has been
1134
               allocated. Now that they've all been allocated, we will use
1135
               the 1 bit to tell us if we've written this particular toc
1136
               entry out.  */
1137
            fixit = FALSE;
1138
            if (h == 0)
1139
              {
1140
                /* It is a file local symbol.  */
1141
                int *local_toc_table;
1142
                const char *name;
1143
 
1144
                sym = syms + symndx;
1145
                name = sym->_n._n_name;
1146
 
1147
                local_toc_table = obj_coff_local_toc_table(input_bfd);
1148
                our_toc_offset = local_toc_table[symndx];
1149
 
1150
                if (IS_WRITTEN(our_toc_offset))
1151
                  {
1152
                    /* If it has been written out, it is marked with the
1153
                       1 bit. Fix up our offset, but do not write it out
1154
                       again.  */
1155
                    MAKE_ADDR_AGAIN(our_toc_offset);
1156
                  }
1157
                else
1158
                  {
1159
                    /* Write out the toc entry.  */
1160
                    record_toc (toc_section, our_toc_offset, priv,
1161
                                strdup (name));
1162
 
1163
                    bfd_put_32 (output_bfd, val,
1164
                               toc_section->contents + our_toc_offset);
1165
 
1166
                    MARK_AS_WRITTEN(local_toc_table[symndx]);
1167
                    fixit = TRUE;
1168
                  }
1169
              }
1170
            else
1171
              {
1172
                const char *name = h->root.root.root.string;
1173
                our_toc_offset = h->toc_offset;
1174
 
1175
                if ((r_flags & IMAGE_REL_PPC_TOCDEFN)
1176
                    == IMAGE_REL_PPC_TOCDEFN )
1177
                  {
1178
                    /* This is unbelievable cheese. Some knowledgable asm
1179
                       hacker has decided to use r2 as a base for loading
1180
                       a value. He/She does this by setting the tocdefn bit,
1181
                       and not supplying a toc definition. The behaviour is
1182
                       then to use the difference between the value of the
1183
                       symbol and the actual location of the toc as the toc
1184
                       index.
1185
 
1186
                       In fact, what is usually happening is, because the
1187
                       Import Address Table is mapped immediately following
1188
                       the toc, some trippy library code trying for speed on
1189
                       dll linkage, takes advantage of that and considers
1190
                       the IAT to be part of the toc, thus saving a load.  */
1191
 
1192
                    our_toc_offset = val - (toc_section->output_section->vma
1193
                                            + toc_section->output_offset);
1194
 
1195
                    /* The size must still fit in a 16-bit displacement.  */
1196
                    if ((bfd_vma) our_toc_offset >= 65535)
1197
                      {
1198
                        (*_bfd_error_handler)
1199
                          (_("%B: Relocation for %s of %lx exceeds Toc size limit"),
1200
                           input_bfd, name,
1201
                           (unsigned long) our_toc_offset);
1202
                        bfd_set_error (bfd_error_bad_value);
1203
                        return FALSE;
1204
                      }
1205
 
1206
                    record_toc (toc_section, our_toc_offset, pub,
1207
                                strdup (name));
1208
                  }
1209
                else if (IS_WRITTEN (our_toc_offset))
1210
                  {
1211
                    /* If it has been written out, it is marked with the
1212
                       1 bit. Fix up our offset, but do not write it out
1213
                       again.  */
1214
                    MAKE_ADDR_AGAIN(our_toc_offset);
1215
                  }
1216
                else
1217
                  {
1218
                    record_toc(toc_section, our_toc_offset, pub,
1219
                               strdup (name));
1220
 
1221
                    /* Write out the toc entry.  */
1222
                    bfd_put_32 (output_bfd, val,
1223
                               toc_section->contents + our_toc_offset);
1224
 
1225
                    MARK_AS_WRITTEN(h->toc_offset);
1226
                    /* The tricky part is that this is the address that
1227
                       needs a .reloc entry for it.  */
1228
                    fixit = TRUE;
1229
                  }
1230
              }
1231
 
1232
            if (fixit && info->base_file)
1233
              {
1234
                /* So if this is non pcrelative, and is referenced
1235
                   to a section or a common symbol, then it needs a reloc.  */
1236
 
1237
                /* Relocation to a symbol in a section which
1238
                   isn't absolute - we output the address here
1239
                   to a file.  */
1240
                bfd_vma addr = (toc_section->output_section->vma
1241
                                + toc_section->output_offset + our_toc_offset);
1242
 
1243
                if (!write_base_file_entry (output_bfd, info, addr))
1244
                  return FALSE;
1245
              }
1246
 
1247
            /* FIXME: this test is conservative.  */
1248
            if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN
1249
                && (bfd_vma) our_toc_offset > toc_section->size)
1250
              {
1251
                (*_bfd_error_handler)
1252
                  (_("%B: Relocation exceeds allocated TOC (%lx)"),
1253
                   input_bfd, (unsigned long) toc_section->size);
1254
                bfd_set_error (bfd_error_bad_value);
1255
                return FALSE;
1256
              }
1257
 
1258
            /* Now we know the relocation for this toc reference.  */
1259
            relocation =  our_toc_offset + TOC_LOAD_ADJUSTMENT;
1260
            rstat = _bfd_relocate_contents (howto, input_bfd, relocation, loc);
1261
          }
1262
          break;
1263
        case IMAGE_REL_PPC_IFGLUE:
1264
          {
1265
            /* To solve this, we need to know whether or not the symbol
1266
               appearing on the call instruction is a glue function or not.
1267
               A glue function must announce itself via a IMGLUE reloc, and
1268
               the reloc contains the required toc restore instruction.  */
1269
            DUMP_RELOC2 (howto->name, rel);
1270
 
1271
            if (h != 0)
1272
              {
1273
                if (h->symbol_is_glue == 1)
1274
                  {
1275
                    bfd_put_32 (input_bfd, (bfd_vma) h->glue_insn, loc);
1276
                  }
1277
              }
1278
          }
1279
          break;
1280
        case IMAGE_REL_PPC_SECREL:
1281
          /* Unimplemented: codeview debugging information.  */
1282
          /* For fast access to the header of the section
1283
             containing the item.  */
1284
          break;
1285
        case IMAGE_REL_PPC_SECTION:
1286
          /* Unimplemented: codeview debugging information.  */
1287
          /* Is used to indicate that the value should be relative
1288
             to the beginning of the section that contains the
1289
             symbol.  */
1290
          break;
1291
        case IMAGE_REL_PPC_ABSOLUTE:
1292
          {
1293
            const char *my_name;
1294
 
1295
            if (h == 0)
1296
              my_name = (syms+symndx)->_n._n_name;
1297
            else
1298
              my_name = h->root.root.root.string;
1299
 
1300
            (*_bfd_error_handler)
1301
              (_("Warning: unsupported reloc %s <file %B, section %A>\n"
1302
                 "sym %ld (%s), r_vaddr %ld (%lx)"),
1303
               input_bfd, input_section, howto->name,
1304
               rel->r_symndx, my_name, (long) rel->r_vaddr,
1305
               (unsigned long) rel->r_vaddr);
1306
          }
1307
          break;
1308
        case IMAGE_REL_PPC_IMGLUE:
1309
          {
1310
            /* There is nothing to do now. This reloc was noted in the first
1311
               pass over the relocs, and the glue instruction extracted.  */
1312
            const char *my_name;
1313
 
1314
            if (h->symbol_is_glue == 1)
1315
              break;
1316
            my_name = h->root.root.root.string;
1317
 
1318
            (*_bfd_error_handler)
1319
              (_("%B: Out of order IMGLUE reloc for %s"), input_bfd, my_name);
1320
            bfd_set_error (bfd_error_bad_value);
1321
            return FALSE;
1322
          }
1323
 
1324
        case IMAGE_REL_PPC_ADDR32NB:
1325
          {
1326
            const char *name = 0;
1327
 
1328
            DUMP_RELOC2 (howto->name, rel);
1329
 
1330
            if (CONST_STRNEQ (input_section->name, ".idata$2") && first_thunk_address == 0)
1331
              {
1332
                /* Set magic values.  */
1333
                int idata5offset;
1334
                struct coff_link_hash_entry *myh;
1335
 
1336
                myh = coff_link_hash_lookup (coff_hash_table (info),
1337
                                             "__idata5_magic__",
1338
                                             FALSE, FALSE, TRUE);
1339
                first_thunk_address = myh->root.u.def.value +
1340
                  sec->output_section->vma +
1341
                    sec->output_offset -
1342
                      pe_data(output_bfd)->pe_opthdr.ImageBase;
1343
 
1344
                idata5offset = myh->root.u.def.value;
1345
                myh = coff_link_hash_lookup (coff_hash_table (info),
1346
                                             "__idata6_magic__",
1347
                                             FALSE, FALSE, TRUE);
1348
 
1349
                thunk_size = myh->root.u.def.value - idata5offset;
1350
                myh = coff_link_hash_lookup (coff_hash_table (info),
1351
                                             "__idata4_magic__",
1352
                                             FALSE, FALSE, TRUE);
1353
                import_table_size = myh->root.u.def.value;
1354
              }
1355
 
1356
            if (h == 0)
1357
              {
1358
                /* It is a file local symbol.  */
1359
                sym = syms + symndx;
1360
                name = sym->_n._n_name;
1361
              }
1362
            else
1363
              {
1364
                char *target = 0;
1365
 
1366
                name = h->root.root.root.string;
1367
                if (strcmp (".idata$2", name) == 0)
1368
                  target = "__idata2_magic__";
1369
                else if (strcmp (".idata$4", name) == 0)
1370
                  target = "__idata4_magic__";
1371
                else if (strcmp (".idata$5", name) == 0)
1372
                  target = "__idata5_magic__";
1373
 
1374
                if (target != 0)
1375
                  {
1376
                    struct coff_link_hash_entry *myh;
1377
 
1378
                    myh = coff_link_hash_lookup (coff_hash_table (info),
1379
                                                 target,
1380
                                                 FALSE, FALSE, TRUE);
1381
                    if (myh == 0)
1382
                      {
1383
                        /* Missing magic cookies. Something is very wrong.  */
1384
                        abort ();
1385
                      }
1386
 
1387
                    val = myh->root.u.def.value +
1388
                      sec->output_section->vma + sec->output_offset;
1389
                    if (first_thunk_address == 0)
1390
                      {
1391
                        int idata5offset;
1392
                        myh = coff_link_hash_lookup (coff_hash_table (info),
1393
                                                     "__idata5_magic__",
1394
                                                     FALSE, FALSE, TRUE);
1395
                        first_thunk_address = myh->root.u.def.value +
1396
                          sec->output_section->vma +
1397
                            sec->output_offset -
1398
                              pe_data(output_bfd)->pe_opthdr.ImageBase;
1399
 
1400
                        idata5offset = myh->root.u.def.value;
1401
                        myh = coff_link_hash_lookup (coff_hash_table (info),
1402
                                                     "__idata6_magic__",
1403
                                                     FALSE, FALSE, TRUE);
1404
 
1405
                        thunk_size = myh->root.u.def.value - idata5offset;
1406
                        myh = coff_link_hash_lookup (coff_hash_table (info),
1407
                                                     "__idata4_magic__",
1408
                                                     FALSE, FALSE, TRUE);
1409
                        import_table_size = myh->root.u.def.value;
1410
                      }
1411
                  }
1412
              }
1413
 
1414
            rstat = _bfd_relocate_contents (howto,
1415
                                            input_bfd,
1416
                                            val -
1417
                                            pe_data (output_bfd)->pe_opthdr.ImageBase,
1418
                                            loc);
1419
          }
1420
          break;
1421
 
1422
        case IMAGE_REL_PPC_REL24:
1423
          DUMP_RELOC2(howto->name, rel);
1424
          val -= (input_section->output_section->vma
1425
                  + input_section->output_offset);
1426
 
1427
          rstat = _bfd_relocate_contents (howto,
1428
                                          input_bfd,
1429
                                          val,
1430
                                          loc);
1431
          break;
1432
        case IMAGE_REL_PPC_ADDR16:
1433
        case IMAGE_REL_PPC_ADDR24:
1434
        case IMAGE_REL_PPC_ADDR32:
1435
          DUMP_RELOC2(howto->name, rel);
1436
          rstat = _bfd_relocate_contents (howto,
1437
                                          input_bfd,
1438
                                          val,
1439
                                          loc);
1440
          break;
1441
        }
1442
 
1443
      if (info->base_file)
1444
        {
1445
          /* So if this is non pcrelative, and is referenced
1446
             to a section or a common symbol, then it needs a reloc.  */
1447
          if (sym && pe_data(output_bfd)->in_reloc_p (output_bfd, howto))
1448
            {
1449
              /* Relocation to a symbol in a section which
1450
                 isn't absolute - we output the address here
1451
                 to a file.  */
1452
              bfd_vma addr = (rel->r_vaddr
1453
                              - input_section->vma
1454
                              + input_section->output_offset
1455
                              + input_section->output_section->vma);
1456
 
1457
              if (!write_base_file_entry (output_bfd, info, addr))
1458
                return FALSE;
1459
            }
1460
        }
1461
 
1462
      switch (rstat)
1463
        {
1464
        default:
1465
          abort ();
1466
        case bfd_reloc_ok:
1467
          break;
1468
        case bfd_reloc_overflow:
1469
          {
1470
            const char *name;
1471
            char buf[SYMNMLEN + 1];
1472
 
1473
            if (symndx == -1)
1474
              name = "*ABS*";
1475
            else if (h != NULL)
1476
              name = NULL;
1477
            else if (sym == NULL)
1478
              name = "*unknown*";
1479
            else if (sym->_n._n_n._n_zeroes == 0
1480
                     && sym->_n._n_n._n_offset != 0)
1481
              name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
1482
            else
1483
              {
1484
                strncpy (buf, sym->_n._n_name, SYMNMLEN);
1485
                buf[SYMNMLEN] = '\0';
1486
                name = buf;
1487
              }
1488
 
1489
            if (! ((*info->callbacks->reloc_overflow)
1490
                   (info, (h ? &h->root.root : NULL), name, howto->name,
1491
                    (bfd_vma) 0, input_bfd,
1492
                    input_section, rel->r_vaddr - input_section->vma)))
1493
              return FALSE;
1494
          }
1495
        }
1496
    }
1497
 
1498
  return TRUE;
1499
}
1500
 
1501
#ifdef COFF_IMAGE_WITH_PE
1502
 
1503
/* FIXME: BFD should not use global variables.  This file is compiled
1504
   twice, and these variables are shared.  This is confusing and
1505
   weird.  */
1506
 
1507
long int global_toc_size = 4;
1508
 
1509
bfd* bfd_of_toc_owner = 0;
1510
 
1511
long int import_table_size;
1512
long int first_thunk_address;
1513
long int thunk_size;
1514
 
1515
struct list_ele *head;
1516
struct list_ele *tail;
1517
 
1518
static char *
1519
h1 = N_("\n\t\t\tTOC MAPPING\n\n");
1520
static char *
1521
h2 = N_(" TOC    disassembly  Comments       Name\n");
1522
static char *
1523
h3 = N_(" Offset  spelling                   (if present)\n");
1524
 
1525
void
1526
dump_toc (vfile)
1527
     PTR vfile;
1528
{
1529
  FILE *file = (FILE *) vfile;
1530
  struct list_ele *t;
1531
 
1532
  fputs (_(h1), file);
1533
  fputs (_(h2), file);
1534
  fputs (_(h3), file);
1535
 
1536
  for (t = head; t != 0; t=t->next)
1537
    {
1538
      const char *cat = "";
1539
 
1540
      if (t->cat == priv)
1541
        cat = _("private       ");
1542
      else if (t->cat == pub)
1543
        cat = _("public        ");
1544
      else if (t->cat == tocdata)
1545
        cat = _("data-in-toc   ");
1546
 
1547
      if (t->offset > global_toc_size)
1548
        {
1549
          if (t->offset <= global_toc_size + thunk_size)
1550
            cat = _("IAT reference ");
1551
          else
1552
            {
1553
              fprintf (file,
1554
                      _("**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n"),
1555
                       global_toc_size, (unsigned long) global_toc_size,
1556
                       thunk_size, (unsigned long) thunk_size);
1557
              cat = _("Out of bounds!");
1558
            }
1559
        }
1560
 
1561
      fprintf (file,
1562
              " %04lx    (%d)", (unsigned long) t->offset, t->offset - 32768);
1563
      fprintf (file,
1564
              "    %s %s\n",
1565
              cat, t->name);
1566
 
1567
    }
1568
 
1569
  fprintf (file, "\n");
1570
}
1571
 
1572
bfd_boolean
1573
ppc_allocate_toc_section (info)
1574
     struct bfd_link_info *info ATTRIBUTE_UNUSED;
1575
{
1576
  asection *s;
1577
  bfd_byte *foo;
1578
  bfd_size_type amt;
1579
  static char test_char = '1';
1580
 
1581
  if ( global_toc_size == 0 ) /* FIXME: does this get me in trouble?  */
1582
    return TRUE;
1583
 
1584
  if (bfd_of_toc_owner == 0)
1585
    /* No toc owner? Something is very wrong.  */
1586
    abort ();
1587
 
1588
  s = bfd_get_section_by_name ( bfd_of_toc_owner , TOC_SECTION_NAME);
1589
  if (s == NULL)
1590
    /* No toc section? Something is very wrong.  */
1591
    abort ();
1592
 
1593
  amt = global_toc_size;
1594
  foo = (bfd_byte *) bfd_alloc (bfd_of_toc_owner, amt);
1595
  memset(foo, test_char, (size_t) global_toc_size);
1596
 
1597
  s->size = global_toc_size;
1598
  s->contents = foo;
1599
 
1600
  return TRUE;
1601
}
1602
 
1603
bfd_boolean
1604
ppc_process_before_allocation (abfd, info)
1605
     bfd *abfd;
1606
     struct bfd_link_info *info;
1607
{
1608
  asection *sec;
1609
  struct internal_reloc *i, *rel;
1610
 
1611
  /* Here we have a bfd that is to be included on the link. We have a hook
1612
     to do reloc rummaging, before section sizes are nailed down.  */
1613
  _bfd_coff_get_external_symbols (abfd);
1614
 
1615
  /* Rummage around all the relocs and map the toc.  */
1616
  sec = abfd->sections;
1617
 
1618
  if (sec == 0)
1619
    return TRUE;
1620
 
1621
  for (; sec != 0; sec = sec->next)
1622
    {
1623
      if (sec->reloc_count == 0)
1624
        continue;
1625
 
1626
      /* load the relocs */
1627
      /* FIXME: there may be a storage leak here */
1628
      i=_bfd_coff_read_internal_relocs(abfd,sec,1,0,0,0);
1629
 
1630
      if (i == 0)
1631
        abort ();
1632
 
1633
      for (rel = i; rel < i + sec->reloc_count; ++rel)
1634
        {
1635
          unsigned short r_type  = EXTRACT_TYPE  (rel->r_type);
1636
          unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
1637
          bfd_boolean ok = TRUE;
1638
 
1639
          DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, rel);
1640
 
1641
          switch(r_type)
1642
            {
1643
            case IMAGE_REL_PPC_TOCREL16:
1644
              /* If TOCDEFN is on, ignore as someone else has allocated the
1645
                 toc entry.  */
1646
              if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN)
1647
                ok = ppc_record_toc_entry(abfd, info, sec,
1648
                                          rel->r_symndx, default_toc);
1649
              if (!ok)
1650
                return FALSE;
1651
              break;
1652
            case IMAGE_REL_PPC_IMGLUE:
1653
              ppc_mark_symbol_as_glue (abfd, rel->r_symndx, rel);
1654
              break;
1655
            default:
1656
              break;
1657
            }
1658
        }
1659
    }
1660
 
1661
  return TRUE;
1662
}
1663
 
1664
#endif
1665
 
1666
static bfd_reloc_status_type
1667
ppc_refhi_reloc (abfd, reloc_entry, symbol, data,
1668
                 input_section, output_bfd, error_message)
1669
     bfd *abfd ATTRIBUTE_UNUSED;
1670
     arelent *reloc_entry ATTRIBUTE_UNUSED;
1671
     asymbol *symbol ATTRIBUTE_UNUSED;
1672
     PTR data ATTRIBUTE_UNUSED;
1673
     asection *input_section ATTRIBUTE_UNUSED;
1674
     bfd *output_bfd;
1675
     char **error_message ATTRIBUTE_UNUSED;
1676
{
1677
  UN_IMPL("REFHI");
1678
  DUMP_RELOC("REFHI",reloc_entry);
1679
 
1680
  if (output_bfd == (bfd *) NULL)
1681
    return bfd_reloc_continue;
1682
 
1683
  return bfd_reloc_undefined;
1684
}
1685
 
1686
static bfd_reloc_status_type
1687
ppc_pair_reloc (abfd, reloc_entry, symbol, data,
1688
                input_section, output_bfd, error_message)
1689
     bfd *abfd ATTRIBUTE_UNUSED;
1690
     arelent *reloc_entry ATTRIBUTE_UNUSED;
1691
     asymbol *symbol ATTRIBUTE_UNUSED;
1692
     PTR data ATTRIBUTE_UNUSED;
1693
     asection *input_section ATTRIBUTE_UNUSED;
1694
     bfd *output_bfd;
1695
     char **error_message ATTRIBUTE_UNUSED;
1696
{
1697
  UN_IMPL("PAIR");
1698
  DUMP_RELOC("PAIR",reloc_entry);
1699
 
1700
  if (output_bfd == (bfd *) NULL)
1701
    return bfd_reloc_continue;
1702
 
1703
  return bfd_reloc_undefined;
1704
}
1705
 
1706
static bfd_reloc_status_type
1707
ppc_toc16_reloc (abfd, reloc_entry, symbol, data,
1708
                 input_section, output_bfd, error_message)
1709
     bfd *abfd ATTRIBUTE_UNUSED;
1710
     arelent *reloc_entry ATTRIBUTE_UNUSED;
1711
     asymbol *symbol ATTRIBUTE_UNUSED;
1712
     PTR data ATTRIBUTE_UNUSED;
1713
     asection *input_section ATTRIBUTE_UNUSED;
1714
     bfd *output_bfd;
1715
     char **error_message ATTRIBUTE_UNUSED;
1716
{
1717
  UN_IMPL ("TOCREL16");
1718
  DUMP_RELOC ("TOCREL16",reloc_entry);
1719
 
1720
  if (output_bfd == (bfd *) NULL)
1721
    return bfd_reloc_continue;
1722
 
1723
  return bfd_reloc_ok;
1724
}
1725
 
1726
static bfd_reloc_status_type
1727
ppc_secrel_reloc (abfd, reloc_entry, symbol, data,
1728
                  input_section, output_bfd, error_message)
1729
     bfd *abfd ATTRIBUTE_UNUSED;
1730
     arelent *reloc_entry ATTRIBUTE_UNUSED;
1731
     asymbol *symbol ATTRIBUTE_UNUSED;
1732
     PTR data ATTRIBUTE_UNUSED;
1733
     asection *input_section ATTRIBUTE_UNUSED;
1734
     bfd *output_bfd;
1735
     char **error_message ATTRIBUTE_UNUSED;
1736
{
1737
  UN_IMPL("SECREL");
1738
  DUMP_RELOC("SECREL",reloc_entry);
1739
 
1740
  if (output_bfd == (bfd *) NULL)
1741
    return bfd_reloc_continue;
1742
 
1743
  return bfd_reloc_ok;
1744
}
1745
 
1746
static bfd_reloc_status_type
1747
ppc_section_reloc (abfd, reloc_entry, symbol, data,
1748
                   input_section, output_bfd, error_message)
1749
     bfd *abfd ATTRIBUTE_UNUSED;
1750
     arelent *reloc_entry ATTRIBUTE_UNUSED;
1751
     asymbol *symbol ATTRIBUTE_UNUSED;
1752
     PTR data ATTRIBUTE_UNUSED;
1753
     asection *input_section ATTRIBUTE_UNUSED;
1754
     bfd *output_bfd;
1755
     char **error_message ATTRIBUTE_UNUSED;
1756
{
1757
  UN_IMPL("SECTION");
1758
  DUMP_RELOC("SECTION",reloc_entry);
1759
 
1760
  if (output_bfd == (bfd *) NULL)
1761
    return bfd_reloc_continue;
1762
 
1763
  return bfd_reloc_ok;
1764
}
1765
 
1766
static bfd_reloc_status_type
1767
ppc_imglue_reloc (abfd, reloc_entry, symbol, data,
1768
                  input_section, output_bfd, error_message)
1769
     bfd *abfd ATTRIBUTE_UNUSED;
1770
     arelent *reloc_entry ATTRIBUTE_UNUSED;
1771
     asymbol *symbol ATTRIBUTE_UNUSED;
1772
     PTR data ATTRIBUTE_UNUSED;
1773
     asection *input_section ATTRIBUTE_UNUSED;
1774
     bfd *output_bfd;
1775
     char **error_message ATTRIBUTE_UNUSED;
1776
{
1777
  UN_IMPL("IMGLUE");
1778
  DUMP_RELOC("IMGLUE",reloc_entry);
1779
 
1780
  if (output_bfd == (bfd *) NULL)
1781
    return bfd_reloc_continue;
1782
 
1783
  return bfd_reloc_ok;
1784
}
1785
 
1786
#define MAX_RELOC_INDEX  \
1787
      (sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]) - 1)
1788
 
1789
/* FIXME: There is a possibility that when we read in a reloc from a file,
1790
          that there are some bits encoded in the upper portion of the
1791
          type field. Not yet implemented.  */
1792
static void ppc_coff_rtype2howto PARAMS ((arelent *, struct internal_reloc *));
1793
 
1794
static void
1795
ppc_coff_rtype2howto (relent, internal)
1796
     arelent *relent;
1797
     struct internal_reloc *internal;
1798
{
1799
  /* We can encode one of three things in the type field, aside from the
1800
     type:
1801
     1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1802
        value, rather than an addition value
1803
     2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1804
        the branch is expected to be taken or not.
1805
     3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1806
     For now, we just strip this stuff to find the type, and ignore it other
1807
     than that.  */
1808
  reloc_howto_type *howto;
1809
  unsigned short r_type  = EXTRACT_TYPE (internal->r_type);
1810
  unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
1811
  unsigned short junk    = EXTRACT_JUNK (internal->r_type);
1812
 
1813
  /* The masking process only slices off the bottom byte for r_type.  */
1814
  if ( r_type > MAX_RELOC_INDEX )
1815
    abort ();
1816
 
1817
  /* Check for absolute crap.  */
1818
  if (junk != 0)
1819
    abort ();
1820
 
1821
  switch(r_type)
1822
    {
1823
    case IMAGE_REL_PPC_ADDR16:
1824
    case IMAGE_REL_PPC_REL24:
1825
    case IMAGE_REL_PPC_ADDR24:
1826
    case IMAGE_REL_PPC_ADDR32:
1827
    case IMAGE_REL_PPC_IFGLUE:
1828
    case IMAGE_REL_PPC_ADDR32NB:
1829
    case IMAGE_REL_PPC_SECTION:
1830
    case IMAGE_REL_PPC_SECREL:
1831
      DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1832
      howto = ppc_coff_howto_table + r_type;
1833
      break;
1834
    case IMAGE_REL_PPC_IMGLUE:
1835
      DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1836
      howto = ppc_coff_howto_table + r_type;
1837
      break;
1838
    case IMAGE_REL_PPC_TOCREL16:
1839
      DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1840
      if (r_flags & IMAGE_REL_PPC_TOCDEFN)
1841
        howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
1842
      else
1843
        howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
1844
      break;
1845
    default:
1846
      fprintf (stderr,
1847
              _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
1848
              ppc_coff_howto_table[r_type].name,
1849
              r_type);
1850
      howto = ppc_coff_howto_table + r_type;
1851
      break;
1852
    }
1853
 
1854
  relent->howto = howto;
1855
}
1856
 
1857
static reloc_howto_type *
1858
coff_ppc_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
1859
     bfd *abfd ATTRIBUTE_UNUSED;
1860
     asection *sec;
1861
     struct internal_reloc *rel;
1862
     struct coff_link_hash_entry *h ATTRIBUTE_UNUSED;
1863
     struct internal_syment *sym ATTRIBUTE_UNUSED;
1864
     bfd_vma *addendp;
1865
{
1866
  reloc_howto_type *howto;
1867
 
1868
  /* We can encode one of three things in the type field, aside from the
1869
     type:
1870
     1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1871
        value, rather than an addition value
1872
     2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1873
        the branch is expected to be taken or not.
1874
     3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1875
     For now, we just strip this stuff to find the type, and ignore it other
1876
     than that.  */
1877
 
1878
  unsigned short r_type  = EXTRACT_TYPE  (rel->r_type);
1879
  unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
1880
  unsigned short junk    = EXTRACT_JUNK  (rel->r_type);
1881
 
1882
  /* The masking process only slices off the bottom byte for r_type.  */
1883
  if (r_type > MAX_RELOC_INDEX)
1884
    abort ();
1885
 
1886
  /* Check for absolute crap.  */
1887
  if (junk != 0)
1888
    abort ();
1889
 
1890
  switch(r_type)
1891
    {
1892
    case IMAGE_REL_PPC_ADDR32NB:
1893
      DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1894
      *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
1895
      howto = ppc_coff_howto_table + r_type;
1896
      break;
1897
    case IMAGE_REL_PPC_TOCREL16:
1898
      DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1899
      if (r_flags & IMAGE_REL_PPC_TOCDEFN)
1900
        howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
1901
      else
1902
        howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
1903
      break;
1904
    case IMAGE_REL_PPC_ADDR16:
1905
    case IMAGE_REL_PPC_REL24:
1906
    case IMAGE_REL_PPC_ADDR24:
1907
    case IMAGE_REL_PPC_ADDR32:
1908
    case IMAGE_REL_PPC_IFGLUE:
1909
    case IMAGE_REL_PPC_SECTION:
1910
    case IMAGE_REL_PPC_SECREL:
1911
      DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1912
      howto = ppc_coff_howto_table + r_type;
1913
      break;
1914
    case IMAGE_REL_PPC_IMGLUE:
1915
      DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1916
      howto = ppc_coff_howto_table + r_type;
1917
      break;
1918
    default:
1919
      fprintf (stderr,
1920
              _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
1921
              ppc_coff_howto_table[r_type].name,
1922
              r_type);
1923
      howto = ppc_coff_howto_table + r_type;
1924
      break;
1925
    }
1926
 
1927
  return howto;
1928
}
1929
 
1930
/* A cheesy little macro to make the code a little more readable.  */
1931
#define HOW2MAP(bfd_rtype,ppc_rtype)  \
1932
 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
1933
 
1934
static reloc_howto_type *ppc_coff_reloc_type_lookup
1935
PARAMS ((bfd *, bfd_reloc_code_real_type));
1936
 
1937
static reloc_howto_type *
1938
ppc_coff_reloc_type_lookup (abfd, code)
1939
     bfd *abfd ATTRIBUTE_UNUSED;
1940
     bfd_reloc_code_real_type code;
1941
{
1942
  switch (code)
1943
    {
1944
      HOW2MAP(BFD_RELOC_32_GOTOFF,    IMAGE_REL_PPC_IMGLUE);
1945
      HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
1946
      HOW2MAP(BFD_RELOC_16,           IMAGE_REL_PPC_ADDR16);
1947
      HOW2MAP(BFD_RELOC_PPC_B26,      IMAGE_REL_PPC_REL24);
1948
      HOW2MAP(BFD_RELOC_PPC_BA26,     IMAGE_REL_PPC_ADDR24);
1949
      HOW2MAP(BFD_RELOC_PPC_TOC16,    IMAGE_REL_PPC_TOCREL16);
1950
      HOW2MAP(BFD_RELOC_16_GOTOFF,    IMAGE_REL_PPC_TOCREL16_DEFN);
1951
      HOW2MAP(BFD_RELOC_32,           IMAGE_REL_PPC_ADDR32);
1952
      HOW2MAP(BFD_RELOC_RVA,          IMAGE_REL_PPC_ADDR32NB);
1953
    default:
1954
      return NULL;
1955
    }
1956
}
1957
#undef HOW2MAP
1958
 
1959
static reloc_howto_type *
1960
ppc_coff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1961
                            const char *r_name)
1962
{
1963
  unsigned int i;
1964
 
1965
  for (i = 0;
1966
       i < sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]);
1967
       i++)
1968
    if (ppc_coff_howto_table[i].name != NULL
1969
        && strcasecmp (ppc_coff_howto_table[i].name, r_name) == 0)
1970
      return &ppc_coff_howto_table[i];
1971
 
1972
  return NULL;
1973
}
1974
 
1975
/* Tailor coffcode.h -- macro heaven.  */
1976
 
1977
#define RTYPE2HOWTO(cache_ptr, dst)  ppc_coff_rtype2howto (cache_ptr, dst)
1978
 
1979
/* We use the special COFF backend linker, with our own special touch.  */
1980
 
1981
#define coff_bfd_reloc_type_lookup   ppc_coff_reloc_type_lookup
1982
#define coff_bfd_reloc_name_lookup ppc_coff_reloc_name_lookup
1983
#define coff_rtype_to_howto          coff_ppc_rtype_to_howto
1984
#define coff_relocate_section        coff_ppc_relocate_section
1985
#define coff_bfd_final_link          ppc_bfd_coff_final_link
1986
 
1987
#ifndef COFF_IMAGE_WITH_PE
1988
#endif
1989
 
1990
#define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
1991
 
1992
#define COFF_PAGE_SIZE                       0x1000
1993
 
1994
/* FIXME: This controls some code that used to be in peicode.h and is
1995
   now in peigen.c.  It will not control the code in peigen.c.  If
1996
   anybody wants to get this working, you will need to fix that.  */
1997
#define POWERPC_LE_PE
1998
 
1999
#define COFF_SECTION_ALIGNMENT_ENTRIES \
2000
{ COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
2001
  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2002
{ COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
2003
  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2004
{ COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
2005
  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2006
{ COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
2007
  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2008
{ COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
2009
  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
2010
{ COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
2011
  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
2012
 
2013
#include "coffcode.h"
2014
 
2015
#ifndef COFF_IMAGE_WITH_PE
2016
 
2017
static bfd_boolean ppc_do_last PARAMS ((bfd *));
2018
static bfd *ppc_get_last PARAMS ((void));
2019
 
2020
static bfd_boolean
2021
ppc_do_last (abfd)
2022
     bfd *abfd;
2023
{
2024
  if (abfd == bfd_of_toc_owner)
2025
    return TRUE;
2026
  else
2027
    return FALSE;
2028
}
2029
 
2030
static bfd *
2031
ppc_get_last()
2032
{
2033
  return bfd_of_toc_owner;
2034
}
2035
 
2036
/* This piece of machinery exists only to guarantee that the bfd that holds
2037
   the toc section is written last.
2038
 
2039
   This does depend on bfd_make_section attaching a new section to the
2040
   end of the section list for the bfd.
2041
 
2042
   This is otherwise intended to be functionally the same as
2043
   cofflink.c:_bfd_coff_final_link(). It is specifically different only
2044
   where the POWERPC_LE_PE macro modifies the code. It is left in as a
2045
   precise form of comment. krk@cygnus.com  */
2046
 
2047
/* Do the final link step.  */
2048
 
2049
bfd_boolean
2050
ppc_bfd_coff_final_link (abfd, info)
2051
     bfd *abfd;
2052
     struct bfd_link_info *info;
2053
{
2054
  bfd_size_type symesz;
2055
  struct coff_final_link_info finfo;
2056
  bfd_boolean debug_merge_allocated;
2057
  asection *o;
2058
  struct bfd_link_order *p;
2059
  bfd_size_type max_sym_count;
2060
  bfd_size_type max_lineno_count;
2061
  bfd_size_type max_reloc_count;
2062
  bfd_size_type max_output_reloc_count;
2063
  bfd_size_type max_contents_size;
2064
  file_ptr rel_filepos;
2065
  unsigned int relsz;
2066
  file_ptr line_filepos;
2067
  unsigned int linesz;
2068
  bfd *sub;
2069
  bfd_byte *external_relocs = NULL;
2070
  char strbuf[STRING_SIZE_SIZE];
2071
  bfd_size_type amt;
2072
 
2073
  symesz = bfd_coff_symesz (abfd);
2074
 
2075
  finfo.info = info;
2076
  finfo.output_bfd = abfd;
2077
  finfo.strtab = NULL;
2078
  finfo.section_info = NULL;
2079
  finfo.last_file_index = -1;
2080
  finfo.last_bf_index = -1;
2081
  finfo.internal_syms = NULL;
2082
  finfo.sec_ptrs = NULL;
2083
  finfo.sym_indices = NULL;
2084
  finfo.outsyms = NULL;
2085
  finfo.linenos = NULL;
2086
  finfo.contents = NULL;
2087
  finfo.external_relocs = NULL;
2088
  finfo.internal_relocs = NULL;
2089
  debug_merge_allocated = FALSE;
2090
 
2091
  coff_data (abfd)->link_info = info;
2092
 
2093
  finfo.strtab = _bfd_stringtab_init ();
2094
  if (finfo.strtab == NULL)
2095
    goto error_return;
2096
 
2097
  if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
2098
    goto error_return;
2099
  debug_merge_allocated = TRUE;
2100
 
2101
  /* Compute the file positions for all the sections.  */
2102
  if (! abfd->output_has_begun)
2103
    {
2104
      if (! bfd_coff_compute_section_file_positions (abfd))
2105
        return FALSE;
2106
    }
2107
 
2108
  /* Count the line numbers and relocation entries required for the
2109
     output file.  Set the file positions for the relocs.  */
2110
  rel_filepos = obj_relocbase (abfd);
2111
  relsz = bfd_coff_relsz (abfd);
2112
  max_contents_size = 0;
2113
  max_lineno_count = 0;
2114
  max_reloc_count = 0;
2115
 
2116
  for (o = abfd->sections; o != NULL; o = o->next)
2117
    {
2118
      o->reloc_count = 0;
2119
      o->lineno_count = 0;
2120
 
2121
      for (p = o->map_head.link_order; p != NULL; p = p->next)
2122
        {
2123
          if (p->type == bfd_indirect_link_order)
2124
            {
2125
              asection *sec;
2126
 
2127
              sec = p->u.indirect.section;
2128
 
2129
              /* Mark all sections which are to be included in the
2130
                 link.  This will normally be every section.  We need
2131
                 to do this so that we can identify any sections which
2132
                 the linker has decided to not include.  */
2133
              sec->linker_mark = TRUE;
2134
 
2135
              if (info->strip == strip_none
2136
                  || info->strip == strip_some)
2137
                o->lineno_count += sec->lineno_count;
2138
 
2139
              if (info->relocatable)
2140
                o->reloc_count += sec->reloc_count;
2141
 
2142
              if (sec->rawsize > max_contents_size)
2143
                max_contents_size = sec->rawsize;
2144
              if (sec->size > max_contents_size)
2145
                max_contents_size = sec->size;
2146
              if (sec->lineno_count > max_lineno_count)
2147
                max_lineno_count = sec->lineno_count;
2148
              if (sec->reloc_count > max_reloc_count)
2149
                max_reloc_count = sec->reloc_count;
2150
            }
2151
          else if (info->relocatable
2152
                   && (p->type == bfd_section_reloc_link_order
2153
                       || p->type == bfd_symbol_reloc_link_order))
2154
            ++o->reloc_count;
2155
        }
2156
      if (o->reloc_count == 0)
2157
        o->rel_filepos = 0;
2158
      else
2159
        {
2160
          o->flags |= SEC_RELOC;
2161
          o->rel_filepos = rel_filepos;
2162
          rel_filepos += o->reloc_count * relsz;
2163
        }
2164
    }
2165
 
2166
  /* If doing a relocatable link, allocate space for the pointers we
2167
     need to keep.  */
2168
  if (info->relocatable)
2169
    {
2170
      unsigned int i;
2171
 
2172
      /* We use section_count + 1, rather than section_count, because
2173
         the target_index fields are 1 based.  */
2174
      amt = abfd->section_count + 1;
2175
      amt *= sizeof (struct coff_link_section_info);
2176
      finfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
2177
 
2178
      if (finfo.section_info == NULL)
2179
        goto error_return;
2180
 
2181
      for (i = 0; i <= abfd->section_count; i++)
2182
        {
2183
          finfo.section_info[i].relocs = NULL;
2184
          finfo.section_info[i].rel_hashes = NULL;
2185
        }
2186
    }
2187
 
2188
  /* We now know the size of the relocs, so we can determine the file
2189
     positions of the line numbers.  */
2190
  line_filepos = rel_filepos;
2191
  linesz = bfd_coff_linesz (abfd);
2192
  max_output_reloc_count = 0;
2193
 
2194
  for (o = abfd->sections; o != NULL; o = o->next)
2195
    {
2196
      if (o->lineno_count == 0)
2197
        o->line_filepos = 0;
2198
      else
2199
        {
2200
          o->line_filepos = line_filepos;
2201
          line_filepos += o->lineno_count * linesz;
2202
        }
2203
 
2204
      if (o->reloc_count != 0)
2205
        {
2206
          /* We don't know the indices of global symbols until we have
2207
             written out all the local symbols.  For each section in
2208
             the output file, we keep an array of pointers to hash
2209
             table entries.  Each entry in the array corresponds to a
2210
             reloc.  When we find a reloc against a global symbol, we
2211
             set the corresponding entry in this array so that we can
2212
             fix up the symbol index after we have written out all the
2213
             local symbols.
2214
 
2215
             Because of this problem, we also keep the relocs in
2216
             memory until the end of the link.  This wastes memory,
2217
             but only when doing a relocatable link, which is not the
2218
             common case.  */
2219
          BFD_ASSERT (info->relocatable);
2220
          amt = o->reloc_count;
2221
          amt *= sizeof (struct internal_reloc);
2222
          finfo.section_info[o->target_index].relocs =
2223
            (struct internal_reloc *) bfd_malloc (amt);
2224
          amt = o->reloc_count;
2225
          amt *= sizeof (struct coff_link_hash_entry *);
2226
          finfo.section_info[o->target_index].rel_hashes =
2227
            (struct coff_link_hash_entry **) bfd_malloc (amt);
2228
          if (finfo.section_info[o->target_index].relocs == NULL
2229
              || finfo.section_info[o->target_index].rel_hashes == NULL)
2230
            goto error_return;
2231
 
2232
          if (o->reloc_count > max_output_reloc_count)
2233
            max_output_reloc_count = o->reloc_count;
2234
        }
2235
 
2236
      /* Reset the reloc and lineno counts, so that we can use them to
2237
         count the number of entries we have output so far.  */
2238
      o->reloc_count = 0;
2239
      o->lineno_count = 0;
2240
    }
2241
 
2242
  obj_sym_filepos (abfd) = line_filepos;
2243
 
2244
  /* Figure out the largest number of symbols in an input BFD.  Take
2245
     the opportunity to clear the output_has_begun fields of all the
2246
     input BFD's.  */
2247
  max_sym_count = 0;
2248
  for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2249
    {
2250
      bfd_size_type sz;
2251
 
2252
      sub->output_has_begun = FALSE;
2253
      sz = obj_raw_syment_count (sub);
2254
      if (sz > max_sym_count)
2255
        max_sym_count = sz;
2256
    }
2257
 
2258
  /* Allocate some buffers used while linking.  */
2259
  amt = max_sym_count * sizeof (struct internal_syment);
2260
  finfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
2261
  amt = max_sym_count * sizeof (asection *);
2262
  finfo.sec_ptrs = (asection **) bfd_malloc (amt);
2263
  amt = max_sym_count * sizeof (long);
2264
  finfo.sym_indices = (long *) bfd_malloc (amt);
2265
  amt = (max_sym_count + 1) * symesz;
2266
  finfo.outsyms = (bfd_byte *) bfd_malloc (amt);
2267
  amt = max_lineno_count * bfd_coff_linesz (abfd);
2268
  finfo.linenos = (bfd_byte *) bfd_malloc (amt);
2269
  finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
2270
  finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
2271
  if (! info->relocatable)
2272
    {
2273
      amt = max_reloc_count * sizeof (struct internal_reloc);
2274
      finfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
2275
    }
2276
  if ((finfo.internal_syms == NULL && max_sym_count > 0)
2277
      || (finfo.sec_ptrs == NULL && max_sym_count > 0)
2278
      || (finfo.sym_indices == NULL && max_sym_count > 0)
2279
      || finfo.outsyms == NULL
2280
      || (finfo.linenos == NULL && max_lineno_count > 0)
2281
      || (finfo.contents == NULL && max_contents_size > 0)
2282
      || (finfo.external_relocs == NULL && max_reloc_count > 0)
2283
      || (! info->relocatable
2284
          && finfo.internal_relocs == NULL
2285
          && max_reloc_count > 0))
2286
    goto error_return;
2287
 
2288
  /* We now know the position of everything in the file, except that
2289
     we don't know the size of the symbol table and therefore we don't
2290
     know where the string table starts.  We just build the string
2291
     table in memory as we go along.  We process all the relocations
2292
     for a single input file at once.  */
2293
  obj_raw_syment_count (abfd) = 0;
2294
 
2295
  if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
2296
    {
2297
      if (! bfd_coff_start_final_link (abfd, info))
2298
        goto error_return;
2299
    }
2300
 
2301
  for (o = abfd->sections; o != NULL; o = o->next)
2302
    {
2303
      for (p = o->map_head.link_order; p != NULL; p = p->next)
2304
        {
2305
          if (p->type == bfd_indirect_link_order
2306
              && (bfd_get_flavour (p->u.indirect.section->owner)
2307
                  == bfd_target_coff_flavour))
2308
            {
2309
              sub = p->u.indirect.section->owner;
2310
#ifdef POWERPC_LE_PE
2311
              if (! sub->output_has_begun && !ppc_do_last(sub))
2312
#else
2313
              if (! sub->output_has_begun)
2314
#endif
2315
                {
2316
                  if (! _bfd_coff_link_input_bfd (&finfo, sub))
2317
                    goto error_return;
2318
                  sub->output_has_begun = TRUE;
2319
                }
2320
            }
2321
          else if (p->type == bfd_section_reloc_link_order
2322
                   || p->type == bfd_symbol_reloc_link_order)
2323
            {
2324
              if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
2325
                goto error_return;
2326
            }
2327
          else
2328
            {
2329
              if (! _bfd_default_link_order (abfd, info, o, p))
2330
                goto error_return;
2331
            }
2332
        }
2333
    }
2334
 
2335
#ifdef POWERPC_LE_PE
2336
  {
2337
    bfd* last_one = ppc_get_last();
2338
    if (last_one)
2339
      {
2340
        if (! _bfd_coff_link_input_bfd (&finfo, last_one))
2341
          goto error_return;
2342
      }
2343
    last_one->output_has_begun = TRUE;
2344
  }
2345
#endif
2346
 
2347
  /* Free up the buffers used by _bfd_coff_link_input_bfd.  */
2348
  coff_debug_merge_hash_table_free (&finfo.debug_merge);
2349
  debug_merge_allocated = FALSE;
2350
 
2351
  if (finfo.internal_syms != NULL)
2352
    {
2353
      free (finfo.internal_syms);
2354
      finfo.internal_syms = NULL;
2355
    }
2356
  if (finfo.sec_ptrs != NULL)
2357
    {
2358
      free (finfo.sec_ptrs);
2359
      finfo.sec_ptrs = NULL;
2360
    }
2361
  if (finfo.sym_indices != NULL)
2362
    {
2363
      free (finfo.sym_indices);
2364
      finfo.sym_indices = NULL;
2365
    }
2366
  if (finfo.linenos != NULL)
2367
    {
2368
      free (finfo.linenos);
2369
      finfo.linenos = NULL;
2370
    }
2371
  if (finfo.contents != NULL)
2372
    {
2373
      free (finfo.contents);
2374
      finfo.contents = NULL;
2375
    }
2376
  if (finfo.external_relocs != NULL)
2377
    {
2378
      free (finfo.external_relocs);
2379
      finfo.external_relocs = NULL;
2380
    }
2381
  if (finfo.internal_relocs != NULL)
2382
    {
2383
      free (finfo.internal_relocs);
2384
      finfo.internal_relocs = NULL;
2385
    }
2386
 
2387
  /* The value of the last C_FILE symbol is supposed to be the symbol
2388
     index of the first external symbol.  Write it out again if
2389
     necessary.  */
2390
  if (finfo.last_file_index != -1
2391
      && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
2392
    {
2393
      file_ptr pos;
2394
 
2395
      finfo.last_file.n_value = obj_raw_syment_count (abfd);
2396
      bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
2397
                             (PTR) finfo.outsyms);
2398
      pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
2399
      if (bfd_seek (abfd, pos, SEEK_SET) != 0
2400
          || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
2401
        return FALSE;
2402
    }
2403
 
2404
  /* Write out the global symbols.  */
2405
  finfo.failed = FALSE;
2406
  coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_global_sym,
2407
                           (PTR) &finfo);
2408
  if (finfo.failed)
2409
    goto error_return;
2410
 
2411
  /* The outsyms buffer is used by _bfd_coff_write_global_sym.  */
2412
  if (finfo.outsyms != NULL)
2413
    {
2414
      free (finfo.outsyms);
2415
      finfo.outsyms = NULL;
2416
    }
2417
 
2418
  if (info->relocatable)
2419
    {
2420
      /* Now that we have written out all the global symbols, we know
2421
         the symbol indices to use for relocs against them, and we can
2422
         finally write out the relocs.  */
2423
      amt = max_output_reloc_count * relsz;
2424
      external_relocs = (bfd_byte *) bfd_malloc (amt);
2425
      if (external_relocs == NULL)
2426
        goto error_return;
2427
 
2428
      for (o = abfd->sections; o != NULL; o = o->next)
2429
        {
2430
          struct internal_reloc *irel;
2431
          struct internal_reloc *irelend;
2432
          struct coff_link_hash_entry **rel_hash;
2433
          bfd_byte *erel;
2434
 
2435
          if (o->reloc_count == 0)
2436
            continue;
2437
 
2438
          irel = finfo.section_info[o->target_index].relocs;
2439
          irelend = irel + o->reloc_count;
2440
          rel_hash = finfo.section_info[o->target_index].rel_hashes;
2441
          erel = external_relocs;
2442
          for (; irel < irelend; irel++, rel_hash++, erel += relsz)
2443
            {
2444
              if (*rel_hash != NULL)
2445
                {
2446
                  BFD_ASSERT ((*rel_hash)->indx >= 0);
2447
                  irel->r_symndx = (*rel_hash)->indx;
2448
                }
2449
              bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
2450
            }
2451
 
2452
          amt = relsz * o->reloc_count;
2453
          if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
2454
              || bfd_bwrite ((PTR) external_relocs, amt, abfd) != amt)
2455
            goto error_return;
2456
        }
2457
 
2458
      free (external_relocs);
2459
      external_relocs = NULL;
2460
    }
2461
 
2462
  /* Free up the section information.  */
2463
  if (finfo.section_info != NULL)
2464
    {
2465
      unsigned int i;
2466
 
2467
      for (i = 0; i < abfd->section_count; i++)
2468
        {
2469
          if (finfo.section_info[i].relocs != NULL)
2470
            free (finfo.section_info[i].relocs);
2471
          if (finfo.section_info[i].rel_hashes != NULL)
2472
            free (finfo.section_info[i].rel_hashes);
2473
        }
2474
      free (finfo.section_info);
2475
      finfo.section_info = NULL;
2476
    }
2477
 
2478
  /* If we have optimized stabs strings, output them.  */
2479
  if (coff_hash_table (info)->stab_info.stabstr != NULL)
2480
    {
2481
      if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
2482
        return FALSE;
2483
    }
2484
 
2485
  /* Write out the string table.  */
2486
  if (obj_raw_syment_count (abfd) != 0)
2487
    {
2488
      file_ptr pos;
2489
 
2490
      pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
2491
      if (bfd_seek (abfd, pos, SEEK_SET) != 0)
2492
        return FALSE;
2493
 
2494
#if STRING_SIZE_SIZE == 4
2495
      H_PUT_32 (abfd,
2496
                _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
2497
                strbuf);
2498
#else
2499
 #error Change H_PUT_32 above
2500
#endif
2501
 
2502
      if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
2503
          != STRING_SIZE_SIZE)
2504
        return FALSE;
2505
 
2506
      if (! _bfd_stringtab_emit (abfd, finfo.strtab))
2507
        return FALSE;
2508
    }
2509
 
2510
  _bfd_stringtab_free (finfo.strtab);
2511
 
2512
  /* Setting bfd_get_symcount to 0 will cause write_object_contents to
2513
     not try to write out the symbols.  */
2514
  bfd_get_symcount (abfd) = 0;
2515
 
2516
  return TRUE;
2517
 
2518
 error_return:
2519
  if (debug_merge_allocated)
2520
    coff_debug_merge_hash_table_free (&finfo.debug_merge);
2521
  if (finfo.strtab != NULL)
2522
    _bfd_stringtab_free (finfo.strtab);
2523
  if (finfo.section_info != NULL)
2524
    {
2525
      unsigned int i;
2526
 
2527
      for (i = 0; i < abfd->section_count; i++)
2528
        {
2529
          if (finfo.section_info[i].relocs != NULL)
2530
            free (finfo.section_info[i].relocs);
2531
          if (finfo.section_info[i].rel_hashes != NULL)
2532
            free (finfo.section_info[i].rel_hashes);
2533
        }
2534
      free (finfo.section_info);
2535
    }
2536
  if (finfo.internal_syms != NULL)
2537
    free (finfo.internal_syms);
2538
  if (finfo.sec_ptrs != NULL)
2539
    free (finfo.sec_ptrs);
2540
  if (finfo.sym_indices != NULL)
2541
    free (finfo.sym_indices);
2542
  if (finfo.outsyms != NULL)
2543
    free (finfo.outsyms);
2544
  if (finfo.linenos != NULL)
2545
    free (finfo.linenos);
2546
  if (finfo.contents != NULL)
2547
    free (finfo.contents);
2548
  if (finfo.external_relocs != NULL)
2549
    free (finfo.external_relocs);
2550
  if (finfo.internal_relocs != NULL)
2551
    free (finfo.internal_relocs);
2552
  if (external_relocs != NULL)
2553
    free (external_relocs);
2554
  return FALSE;
2555
}
2556
#endif
2557
 
2558
/* Forward declaration for use by alternative_target field.  */
2559
#ifdef TARGET_BIG_SYM
2560
extern const bfd_target TARGET_BIG_SYM;
2561
#endif
2562
 
2563
/* The transfer vectors that lead the outside world to all of the above.  */
2564
 
2565
#ifdef TARGET_LITTLE_SYM
2566
const bfd_target TARGET_LITTLE_SYM =
2567
{
2568
  TARGET_LITTLE_NAME,           /* name or coff-arm-little */
2569
  bfd_target_coff_flavour,
2570
  BFD_ENDIAN_LITTLE,            /* data byte order is little */
2571
  BFD_ENDIAN_LITTLE,            /* header byte order is little */
2572
 
2573
  (HAS_RELOC | EXEC_P |         /* FIXME: object flags */
2574
   HAS_LINENO | HAS_DEBUG |
2575
   HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2576
 
2577
#ifndef COFF_WITH_PE
2578
  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2579
#else
2580
  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
2581
   | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2582
#endif
2583
 
2584
  0,                             /* leading char */
2585
  '/',                          /* ar_pad_char */
2586
  15,                           /* ar_max_namelen??? FIXMEmgo */
2587
 
2588
  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2589
  bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2590
  bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2591
 
2592
  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2593
  bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2594
  bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2595
 
2596
  {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
2597
     bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2598
  {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2599
     bfd_false},
2600
  {bfd_false, coff_write_object_contents,       /* bfd_write_contents */
2601
     _bfd_write_archive_contents, bfd_false},
2602
 
2603
  BFD_JUMP_TABLE_GENERIC (coff),
2604
  BFD_JUMP_TABLE_COPY (coff),
2605
  BFD_JUMP_TABLE_CORE (_bfd_nocore),
2606
  BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2607
  BFD_JUMP_TABLE_SYMBOLS (coff),
2608
  BFD_JUMP_TABLE_RELOCS (coff),
2609
  BFD_JUMP_TABLE_WRITE (coff),
2610
  BFD_JUMP_TABLE_LINK (coff),
2611
  BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2612
 
2613
  /* Alternative_target.  */
2614
#ifdef TARGET_BIG_SYM
2615
  & TARGET_BIG_SYM,
2616
#else
2617
  NULL,
2618
#endif
2619
 
2620
  COFF_SWAP_TABLE
2621
};
2622
#endif
2623
 
2624
#ifdef TARGET_BIG_SYM
2625
const bfd_target TARGET_BIG_SYM =
2626
{
2627
  TARGET_BIG_NAME,
2628
  bfd_target_coff_flavour,
2629
  BFD_ENDIAN_BIG,               /* data byte order is big */
2630
  BFD_ENDIAN_BIG,               /* header byte order is big */
2631
 
2632
  (HAS_RELOC | EXEC_P |         /* FIXME: object flags */
2633
   HAS_LINENO | HAS_DEBUG |
2634
   HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2635
 
2636
#ifndef COFF_WITH_PE
2637
  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2638
#else
2639
  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
2640
   | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2641
#endif
2642
 
2643
  0,                             /* leading char */
2644
  '/',                          /* ar_pad_char */
2645
  15,                           /* ar_max_namelen??? FIXMEmgo */
2646
 
2647
  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2648
  bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2649
  bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
2650
 
2651
  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2652
  bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2653
  bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
2654
 
2655
  {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
2656
     bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2657
  {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2658
     bfd_false},
2659
  {bfd_false, coff_write_object_contents,       /* bfd_write_contents */
2660
     _bfd_write_archive_contents, bfd_false},
2661
 
2662
  BFD_JUMP_TABLE_GENERIC (coff),
2663
  BFD_JUMP_TABLE_COPY (coff),
2664
  BFD_JUMP_TABLE_CORE (_bfd_nocore),
2665
  BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2666
  BFD_JUMP_TABLE_SYMBOLS (coff),
2667
  BFD_JUMP_TABLE_RELOCS (coff),
2668
  BFD_JUMP_TABLE_WRITE (coff),
2669
  BFD_JUMP_TABLE_LINK (coff),
2670
  BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2671
 
2672
  /* Alternative_target.  */
2673
#ifdef TARGET_LITTLE_SYM
2674
  & TARGET_LITTLE_SYM,
2675
#else
2676
  NULL,
2677
#endif
2678
 
2679
  COFF_SWAP_TABLE
2680
};
2681
 
2682
#endif

powered by: WebSVN 2.1.0

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