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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [bfd/] [coff-ppc.c] - Blame information for rev 578

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

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

powered by: WebSVN 2.1.0

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