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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [bfd/] [coff-ppc.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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