OSDN Git Service

Touches most files in bfd/, so likely will be blamed for everything..
[pf3gnuchains/pf3gnuchains3x.git] / bfd / coff-ppc.c
1 /* 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   bfd_size_type amt = sizeof (struct ppc_coff_link_hash_table);
206
207   ret = (struct ppc_coff_link_hash_table *) bfd_alloc (abfd, amt);
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 \f
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 \f
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 \f
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 \f
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 \f
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 \f
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   tocdata
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 *, bfd_signed_vma, enum ref_category, const char *));
853
854 static void
855 record_toc (toc_section, our_toc_offset, cat, name)
856      asection *toc_section;
857      bfd_signed_vma 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   bfd_size_type amt = sizeof (struct list_ele);
863   struct list_ele *t = (struct list_ele *) bfd_malloc (amt);
864
865   if (t == NULL)
866     abort ();
867   t->next = 0;
868   t->offset = our_toc_offset;
869   t->name = name;
870   t->cat = cat;
871   t->addr = toc_section->output_offset + our_toc_offset;
872
873   if (head == 0)
874     {
875       head = t;
876       tail = t;
877     }
878   else
879     {
880       tail->next = t;
881       tail = t;
882     }
883 }
884
885 #ifdef COFF_IMAGE_WITH_PE
886
887 static boolean ppc_record_toc_entry
888   PARAMS ((bfd *, struct bfd_link_info *, asection *, int, enum toc_type));
889 static void ppc_mark_symbol_as_glue
890   PARAMS ((bfd *, int, struct internal_reloc *));
891
892 /* record a toc offset against a symbol */
893 static boolean
894 ppc_record_toc_entry(abfd, info, sec, sym, toc_kind)
895      bfd *abfd;
896      struct bfd_link_info *info ATTRIBUTE_UNUSED;
897      asection *sec ATTRIBUTE_UNUSED;
898      int sym;
899      enum toc_type toc_kind ATTRIBUTE_UNUSED;
900 {
901   struct ppc_coff_link_hash_entry *h;
902   const char *name;
903
904   int *local_syms;
905
906   h = 0;
907
908   h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
909   if (h != 0)
910     {
911       HASH_CHECK(h);
912     }
913
914   if (h == 0)
915     {
916       local_syms = obj_coff_local_toc_table(abfd);
917       if (local_syms == 0)
918         {
919           unsigned int i;
920           bfd_size_type amt;
921           /* allocate a table */
922           amt = (bfd_size_type) obj_raw_syment_count (abfd) * sizeof (int);
923           local_syms = (int *) bfd_zalloc (abfd, amt);
924           if (local_syms == 0)
925             return false;
926           obj_coff_local_toc_table (abfd) = local_syms;
927           for (i = 0; i < obj_raw_syment_count (abfd); ++i)
928             {
929               SET_UNALLOCATED (local_syms[i]);
930             }
931         }
932
933       if (IS_UNALLOCATED(local_syms[sym]))
934         {
935           local_syms[sym] = global_toc_size;
936           global_toc_size += 4;
937
938           /* The size must fit in a 16bit displacment */
939           if (global_toc_size > 65535)
940             {
941               (*_bfd_error_handler) (_("TOC overflow"));
942               bfd_set_error (bfd_error_file_too_big);
943               return false;
944             }
945         }
946     }
947   else
948     {
949       name = h->root.root.root.string;
950
951       /* check to see if there's a toc slot allocated. If not, do it
952          here. It will be used in relocate_section */
953       if (IS_UNALLOCATED(h->toc_offset))
954         {
955           h->toc_offset = global_toc_size;
956           global_toc_size += 4;
957
958           /* The size must fit in a 16bit displacment */
959           if (global_toc_size >= 65535)
960             {
961               (*_bfd_error_handler) (_("TOC overflow"));
962               bfd_set_error (bfd_error_file_too_big);
963               return false;
964             }
965         }
966     }
967
968   return true;
969 }
970
971 /* record a toc offset against a symbol */
972 static void
973 ppc_mark_symbol_as_glue(abfd, sym, rel)
974      bfd *abfd;
975      int sym;
976      struct internal_reloc *rel;
977 {
978   struct ppc_coff_link_hash_entry *h;
979
980   h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
981
982   HASH_CHECK(h);
983
984   h->symbol_is_glue = 1;
985   h->glue_insn = bfd_get_32 (abfd, (bfd_byte *) &rel->r_vaddr);
986
987   return;
988 }
989
990 #endif /* COFF_IMAGE_WITH_PE */
991 \f
992 /* Return true if this relocation should
993    appear in the output .reloc section.  */
994
995 static boolean in_reloc_p(abfd, howto)
996      bfd * abfd ATTRIBUTE_UNUSED;
997      reloc_howto_type *howto;
998 {
999   return
1000     (! howto->pc_relative)
1001       && (howto->type != IMAGE_REL_PPC_ADDR32NB)
1002       && (howto->type != IMAGE_REL_PPC_TOCREL16)
1003       && (howto->type != IMAGE_REL_PPC_IMGLUE)
1004       && (howto->type != IMAGE_REL_PPC_IFGLUE)
1005       && (howto->type != IMAGE_REL_PPC_SECREL)
1006       && (howto->type != IMAGE_REL_PPC_SECTION)
1007       && (howto->type != IMAGE_REL_PPC_SECREL16)
1008       && (howto->type != IMAGE_REL_PPC_REFHI)
1009       && (howto->type != IMAGE_REL_PPC_REFLO)
1010       && (howto->type != IMAGE_REL_PPC_PAIR)
1011       && (howto->type != IMAGE_REL_PPC_TOCREL16_DEFN) ;
1012 }
1013
1014 #if 0
1015
1016 /* this function is in charge of performing all the ppc PE relocations */
1017 /* Don't yet know if we want to do this this particular way ... (krk)  */
1018 /* FIXME: (it is not yet enabled) */
1019
1020 static bfd_reloc_status_type
1021 pe_ppc_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
1022               error_message)
1023      bfd *abfd;
1024      arelent *reloc_entry;
1025      asymbol *symbol_in;
1026      PTR data;
1027      asection *input_section;
1028      bfd *output_bfd;
1029      char **error_message;
1030 {
1031   /* the consth relocation comes in two parts, we have to remember
1032      the state between calls, in these variables */
1033   static boolean part1_consth_active = false;
1034   static unsigned long part1_consth_value;
1035
1036   unsigned long sym_value;
1037   unsigned short r_type;
1038   unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
1039
1040   r_type = reloc_entry->howto->type;
1041
1042   if (output_bfd)
1043     {
1044       /* Partial linking - do nothing */
1045       reloc_entry->address += input_section->output_offset;
1046       return bfd_reloc_ok;
1047     }
1048
1049   if (symbol_in != NULL
1050       && bfd_is_und_section (symbol_in->section))
1051     {
1052       /* Keep the state machine happy in case we're called again */
1053       if (r_type == IMAGE_REL_PPC_REFHI)
1054         {
1055           part1_consth_active = true;
1056           part1_consth_value  = 0;
1057         }
1058       return(bfd_reloc_undefined);
1059     }
1060
1061   if ((part1_consth_active) && (r_type != IMAGE_REL_PPC_PAIR))
1062     {
1063       part1_consth_active = false;
1064       *error_message = (char *) _("Missing PAIR");
1065       return(bfd_reloc_dangerous);
1066     }
1067
1068   sym_value = get_symbol_value(symbol_in);
1069
1070   return(bfd_reloc_ok);
1071 }
1072
1073 #endif /* 0 */
1074
1075 /* The reloc processing routine for the optimized COFF linker.  */
1076
1077 static boolean
1078 coff_ppc_relocate_section (output_bfd, info, input_bfd, input_section,
1079                            contents, relocs, syms, sections)
1080      bfd *output_bfd;
1081      struct bfd_link_info *info;
1082      bfd *input_bfd;
1083      asection *input_section;
1084      bfd_byte *contents;
1085      struct internal_reloc *relocs;
1086      struct internal_syment *syms;
1087      asection **sections;
1088 {
1089   struct internal_reloc *rel;
1090   struct internal_reloc *relend;
1091   boolean hihalf;
1092   bfd_vma hihalf_val;
1093   asection *toc_section = 0;
1094   bfd_vma relocation;
1095   reloc_howto_type *howto = 0;
1096
1097   /* If we are performing a relocateable link, we don't need to do a
1098      thing.  The caller will take care of adjusting the reloc
1099      addresses and symbol indices.  */
1100   if (info->relocateable)
1101     return true;
1102
1103   hihalf = false;
1104   hihalf_val = 0;
1105
1106   rel = relocs;
1107   relend = rel + input_section->reloc_count;
1108   for (; rel < relend; rel++)
1109     {
1110       long symndx;
1111       struct ppc_coff_link_hash_entry *h;
1112       struct internal_syment *sym;
1113       bfd_vma val;
1114
1115       asection *sec;
1116       bfd_reloc_status_type rstat;
1117       bfd_byte *loc;
1118
1119       unsigned short r_type  = EXTRACT_TYPE (rel->r_type);
1120       unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
1121
1122       symndx = rel->r_symndx;
1123       loc = contents + rel->r_vaddr - input_section->vma;
1124
1125       /* FIXME: check bounds on r_type */
1126       howto = ppc_coff_howto_table + r_type;
1127
1128       if (symndx == -1)
1129         {
1130           h = NULL;
1131           sym = NULL;
1132         }
1133       else
1134         {
1135           h = (struct ppc_coff_link_hash_entry *)
1136             (obj_coff_sym_hashes (input_bfd)[symndx]);
1137           if (h != 0)
1138             {
1139               HASH_CHECK(h);
1140             }
1141
1142           sym = syms + symndx;
1143         }
1144
1145       if (r_type == IMAGE_REL_PPC_IMGLUE && h == 0)
1146         {
1147           /* An IMGLUE reloc must have a name. Something is very wrong.  */
1148           abort ();
1149         }
1150
1151       sec = NULL;
1152       val = 0;
1153
1154       /* FIXME: PAIR unsupported in the following code */
1155       if (h == NULL)
1156         {
1157           if (symndx == -1)
1158             sec = bfd_abs_section_ptr;
1159           else
1160             {
1161               sec = sections[symndx];
1162               val = (sec->output_section->vma
1163                      + sec->output_offset
1164                      + sym->n_value);
1165               if (! obj_pe (output_bfd))
1166                 val -= sec->vma;
1167             }
1168         }
1169       else
1170         {
1171           HASH_CHECK(h);
1172
1173           if (h->root.root.type == bfd_link_hash_defined
1174               || h->root.root.type == bfd_link_hash_defweak)
1175             {
1176               sec = h->root.root.u.def.section;
1177               val = (h->root.root.u.def.value
1178                      + sec->output_section->vma
1179                      + sec->output_offset);
1180             }
1181           else
1182             {
1183               if (! ((*info->callbacks->undefined_symbol)
1184                      (info, h->root.root.root.string, input_bfd, input_section,
1185                       rel->r_vaddr - input_section->vma, true)))
1186                 return false;
1187             }
1188         }
1189
1190       rstat = bfd_reloc_ok;
1191
1192       /* Each case must do its own relocation, setting rstat appropriately */
1193       switch (r_type)
1194         {
1195         default:
1196           (*_bfd_error_handler)
1197             (_("%s: unsupported relocation type 0x%02x"),
1198              bfd_get_filename (input_bfd), r_type);
1199           bfd_set_error (bfd_error_bad_value);
1200           return false;
1201         case IMAGE_REL_PPC_TOCREL16:
1202           {
1203             bfd_signed_vma our_toc_offset;
1204             int fixit;
1205
1206             DUMP_RELOC2(howto->name, rel);
1207
1208             if (toc_section == 0)
1209               {
1210                 toc_section = bfd_get_section_by_name (bfd_of_toc_owner,
1211                                                        TOC_SECTION_NAME);
1212
1213                 if ( toc_section == NULL )
1214                   {
1215                     /* There is no toc section. Something is very wrong.  */
1216                     abort ();
1217                   }
1218               }
1219
1220             /*
1221              *  Amazing bit tricks present. As we may have seen earlier, we
1222              *  use the 1 bit to tell us whether or not a toc offset has been
1223              *  allocated. Now that they've all been allocated, we will use
1224              *  the 1 bit to tell us if we've written this particular toc
1225              *  entry out.
1226              */
1227             fixit = false;
1228             if (h == 0)
1229               { /* it is a file local symbol */
1230                 int *local_toc_table;
1231                 const char *name;
1232
1233                 sym = syms + symndx;
1234                 name = sym->_n._n_name;
1235
1236                 local_toc_table = obj_coff_local_toc_table(input_bfd);
1237                 our_toc_offset = local_toc_table[symndx];
1238
1239                 if (IS_WRITTEN(our_toc_offset))
1240                   {
1241                     /* if it has been written out, it is marked with the
1242                        1 bit. Fix up our offset, but do not write it out
1243                        again.
1244                      */
1245                     MAKE_ADDR_AGAIN(our_toc_offset);
1246                   }
1247                 else
1248                   {
1249                     /* write out the toc entry */
1250                     record_toc (toc_section, our_toc_offset, priv,
1251                                 strdup (name));
1252
1253                     bfd_put_32 (output_bfd, val,
1254                                toc_section->contents + our_toc_offset);
1255
1256                     MARK_AS_WRITTEN(local_toc_table[symndx]);
1257                     fixit = true;
1258                   }
1259               }
1260             else
1261               {
1262                 const char *name = h->root.root.root.string;
1263                 our_toc_offset = h->toc_offset;
1264
1265                 if ((r_flags & IMAGE_REL_PPC_TOCDEFN)
1266                     == IMAGE_REL_PPC_TOCDEFN )
1267                   {
1268                     /* This is unbelievable cheese. Some knowledgable asm
1269                        hacker has decided to use r2 as a base for loading
1270                        a value. He/She does this by setting the tocdefn bit,
1271                        and not supplying a toc definition. The behaviour is
1272                        then to use the difference between the value of the
1273                        symbol and the actual location of the toc as the toc
1274                        index.
1275
1276                        In fact, what is usually happening is, because the
1277                        Import Address Table is mapped immediately following
1278                        the toc, some trippy library code trying for speed on
1279                        dll linkage, takes advantage of that and considers
1280                        the IAT to be part of the toc, thus saving a load.
1281                     */
1282
1283                     our_toc_offset = val - (toc_section->output_section->vma
1284                                             + toc_section->output_offset);
1285
1286                     /* The size must still fit in a 16bit displacment */
1287                     if ((bfd_vma) our_toc_offset >= 65535)
1288                       {
1289                         (*_bfd_error_handler)
1290                           (_("%s: Relocation for %s of %lx exceeds Toc size limit"),
1291                            bfd_get_filename (input_bfd), name,
1292                            (unsigned long) our_toc_offset);
1293                         bfd_set_error (bfd_error_bad_value);
1294                         return false;
1295                       }
1296
1297                     record_toc (toc_section, our_toc_offset, pub,
1298                                 strdup (name));
1299                   }
1300                 else if (IS_WRITTEN(our_toc_offset))
1301                   {
1302                     /* if it has been written out, it is marked with the
1303                        1 bit. Fix up our offset, but do not write it out
1304                        again.
1305                      */
1306                     MAKE_ADDR_AGAIN(our_toc_offset);
1307                   }
1308                 else
1309                   {
1310                     record_toc(toc_section, our_toc_offset, pub,
1311                                strdup (name));
1312
1313                     /* write out the toc entry */
1314                     bfd_put_32 (output_bfd, val,
1315                                toc_section->contents + our_toc_offset);
1316
1317                     MARK_AS_WRITTEN(h->toc_offset);
1318                     /* The tricky part is that this is the address that */
1319                     /* needs a .reloc entry for it */
1320                     fixit = true;
1321                   }
1322               }
1323
1324             if (fixit && info->base_file)
1325               {
1326                 /* So if this is non pcrelative, and is referenced
1327                    to a section or a common symbol, then it needs a reloc */
1328
1329                 /* relocation to a symbol in a section which
1330                    isn't absolute - we output the address here
1331                    to a file */
1332
1333                 bfd_vma addr = (toc_section->output_section->vma
1334                                 + toc_section->output_offset + our_toc_offset);
1335
1336                 if (coff_data(output_bfd)->pe)
1337                   addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
1338
1339                 fwrite (&addr, 1,4, (FILE *) info->base_file);
1340               }
1341
1342             /* FIXME: this test is conservative */
1343             if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN
1344                 && (bfd_vma) our_toc_offset > toc_section->_raw_size)
1345               {
1346                 (*_bfd_error_handler)
1347                   (_("%s: Relocation exceeds allocated TOC (%lx)"),
1348                    bfd_get_filename (input_bfd),
1349                    (unsigned long) toc_section->_raw_size);
1350                 bfd_set_error (bfd_error_bad_value);
1351                 return false;
1352               }
1353
1354             /* Now we know the relocation for this toc reference */
1355             relocation =  our_toc_offset + TOC_LOAD_ADJUSTMENT;
1356             rstat = _bfd_relocate_contents (howto, input_bfd, relocation, loc);
1357           }
1358           break;
1359         case IMAGE_REL_PPC_IFGLUE:
1360           {
1361             /* To solve this, we need to know whether or not the symbol */
1362             /* appearing on the call instruction is a glue function or not.  */
1363             /* A glue function must announce itself via a IMGLUE reloc, and */
1364             /* the reloc contains the required toc restore instruction */
1365
1366             bfd_vma x;
1367             const char *my_name;
1368             DUMP_RELOC2(howto->name, rel);
1369
1370             if (h != 0)
1371               {
1372                 my_name = h->root.root.root.string;
1373                 if (h->symbol_is_glue == 1)
1374                   {
1375                     x = bfd_get_32 (input_bfd, loc);
1376                     bfd_put_32 (input_bfd, (bfd_vma) h->glue_insn, loc);
1377                   }
1378               }
1379           }
1380           break;
1381         case IMAGE_REL_PPC_SECREL:
1382           /* Unimplemented: codeview debugging information */
1383           /* For fast access to the header of the section
1384              containing the item.  */
1385           break;
1386         case IMAGE_REL_PPC_SECTION:
1387           /* Unimplemented: codeview debugging information */
1388           /* Is used to indicate that the value should be relative
1389              to the beginning of the section that contains the
1390              symbol */
1391           break;
1392         case IMAGE_REL_PPC_ABSOLUTE:
1393           {
1394             const char *my_name;
1395             if (h == 0)
1396                 my_name = (syms+symndx)->_n._n_name;
1397             else
1398               {
1399                 my_name = h->root.root.root.string;
1400               }
1401
1402             fprintf (stderr,
1403                     _("Warning: unsupported reloc %s <file %s, section %s>\n"),
1404                     howto->name,
1405                     bfd_get_filename(input_bfd),
1406                     input_section->name);
1407
1408             fprintf (stderr,"sym %ld (%s), r_vaddr %ld (%lx)\n",
1409                     rel->r_symndx, my_name, (long) rel->r_vaddr,
1410                     (unsigned long) rel->r_vaddr);
1411           }
1412           break;
1413         case IMAGE_REL_PPC_IMGLUE:
1414           {
1415             /* There is nothing to do now. This reloc was noted in the first
1416                pass over the relocs, and the glue instruction extracted */
1417             const char *my_name;
1418             if (h->symbol_is_glue == 1)
1419               break;
1420             my_name = h->root.root.root.string;
1421
1422             (*_bfd_error_handler)
1423               (_("%s: Out of order IMGLUE reloc for %s"),
1424                bfd_get_filename (input_bfd), my_name);
1425             bfd_set_error (bfd_error_bad_value);
1426             return false;
1427           }
1428
1429         case IMAGE_REL_PPC_ADDR32NB:
1430           {
1431             const char *name = 0;
1432             DUMP_RELOC2(howto->name, rel);
1433
1434             if (strncmp(".idata$2",input_section->name,8) == 0 && first_thunk_address == 0)
1435               {
1436                 /* set magic values */
1437                 int idata5offset;
1438                 struct coff_link_hash_entry *myh;
1439                 myh = coff_link_hash_lookup (coff_hash_table (info),
1440                                              "__idata5_magic__",
1441                                              false, false, true);
1442                 first_thunk_address = myh->root.u.def.value +
1443                   sec->output_section->vma +
1444                     sec->output_offset -
1445                       pe_data(output_bfd)->pe_opthdr.ImageBase;
1446
1447                 idata5offset = myh->root.u.def.value;
1448                 myh = coff_link_hash_lookup (coff_hash_table (info),
1449                                              "__idata6_magic__",
1450                                              false, false, true);
1451
1452                 thunk_size = myh->root.u.def.value - idata5offset;
1453                 myh = coff_link_hash_lookup (coff_hash_table (info),
1454                                              "__idata4_magic__",
1455                                              false, false, true);
1456                 import_table_size = myh->root.u.def.value;
1457               }
1458
1459             if (h == 0)
1460               { /* it is a file local symbol */
1461                 sym = syms + symndx;
1462                 name = sym->_n._n_name;
1463               }
1464             else
1465               {
1466                 char *target = 0;
1467
1468                 name = h->root.root.root.string;
1469                 if (strcmp(".idata$2", name) == 0)
1470                   target = "__idata2_magic__";
1471                 else if (strcmp(".idata$4", name) == 0)
1472                   target = "__idata4_magic__";
1473                 else if (strcmp(".idata$5", name) == 0)
1474                   target = "__idata5_magic__";
1475
1476                 if (target != 0)
1477                   {
1478                     struct coff_link_hash_entry *myh;
1479
1480                     myh = coff_link_hash_lookup (coff_hash_table (info),
1481                                                  target,
1482                                                  false, false, true);
1483                     if (myh == 0)
1484                       {
1485                         /* Missing magic cookies. Something is very wrong.  */
1486                         abort ();
1487                       }
1488
1489                     val = myh->root.u.def.value +
1490                       sec->output_section->vma + sec->output_offset;
1491                     if (first_thunk_address == 0)
1492                       {
1493                         int idata5offset;
1494                         myh = coff_link_hash_lookup (coff_hash_table (info),
1495                                                      "__idata5_magic__",
1496                                                      false, false, true);
1497                         first_thunk_address = myh->root.u.def.value +
1498                           sec->output_section->vma +
1499                             sec->output_offset -
1500                               pe_data(output_bfd)->pe_opthdr.ImageBase;
1501
1502                         idata5offset = myh->root.u.def.value;
1503                         myh = coff_link_hash_lookup (coff_hash_table (info),
1504                                                      "__idata6_magic__",
1505                                                      false, false, true);
1506
1507                         thunk_size = myh->root.u.def.value - idata5offset;
1508                         myh = coff_link_hash_lookup (coff_hash_table (info),
1509                                                      "__idata4_magic__",
1510                                                      false, false, true);
1511                         import_table_size = myh->root.u.def.value;
1512                       }
1513                   }
1514               }
1515
1516             rstat = _bfd_relocate_contents (howto,
1517                               input_bfd,
1518                               val -
1519                               pe_data(output_bfd)->pe_opthdr.ImageBase,
1520                               loc);
1521           }
1522           break;
1523
1524         case IMAGE_REL_PPC_REL24:
1525           DUMP_RELOC2(howto->name, rel);
1526           val -= (input_section->output_section->vma
1527                   + input_section->output_offset);
1528
1529           rstat = _bfd_relocate_contents (howto,
1530                                           input_bfd,
1531                                           val,
1532                                           loc);
1533           break;
1534         case IMAGE_REL_PPC_ADDR16:
1535         case IMAGE_REL_PPC_ADDR24:
1536         case IMAGE_REL_PPC_ADDR32:
1537           DUMP_RELOC2(howto->name, rel);
1538           rstat = _bfd_relocate_contents (howto,
1539                                           input_bfd,
1540                                           val,
1541                                           loc);
1542           break;
1543         }
1544
1545       if ( info->base_file )
1546         {
1547           /* So if this is non pcrelative, and is referenced
1548              to a section or a common symbol, then it needs a reloc */
1549           if (sym && pe_data(output_bfd)->in_reloc_p(output_bfd, howto))
1550             {
1551               /* relocation to a symbol in a section which
1552                  isn't absolute - we output the address here
1553                  to a file */
1554               bfd_vma addr = rel->r_vaddr
1555                 - input_section->vma
1556                 + input_section->output_offset
1557                   + input_section->output_section->vma;
1558
1559               if (coff_data(output_bfd)->pe)
1560                 {
1561                   addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
1562                 }
1563               fwrite (&addr, 1,4, (FILE *) info->base_file);
1564             }
1565         }
1566
1567       switch (rstat)
1568         {
1569         default:
1570           abort ();
1571         case bfd_reloc_ok:
1572           break;
1573         case bfd_reloc_overflow:
1574           {
1575             const char *name;
1576             char buf[SYMNMLEN + 1];
1577
1578             if (symndx == -1)
1579               name = "*ABS*";
1580             else if (h != NULL)
1581               name = h->root.root.root.string;
1582             else if (sym == NULL)
1583               name = "*unknown*";
1584             else if (sym->_n._n_n._n_zeroes == 0
1585                      && sym->_n._n_n._n_offset != 0)
1586               name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
1587             else
1588               {
1589                 strncpy (buf, sym->_n._n_name, SYMNMLEN);
1590                 buf[SYMNMLEN] = '\0';
1591                 name = buf;
1592               }
1593
1594             if (! ((*info->callbacks->reloc_overflow)
1595                    (info, name, howto->name,
1596                     (bfd_vma) 0, input_bfd,
1597                     input_section, rel->r_vaddr - input_section->vma)))
1598               {
1599                 return false;
1600               }
1601           }
1602         }
1603
1604     }
1605
1606   return true;
1607 }
1608
1609 #ifdef COFF_IMAGE_WITH_PE
1610
1611 /* FIXME: BFD should not use global variables.  This file is compiled
1612    twice, and these variables are shared.  This is confusing and
1613    weird.  */
1614
1615 long int global_toc_size = 4;
1616
1617 bfd* bfd_of_toc_owner = 0;
1618
1619 long int import_table_size;
1620 long int first_thunk_address;
1621 long int thunk_size;
1622
1623 struct list_ele *head;
1624 struct list_ele *tail;
1625
1626 static char *
1627 h1 = N_("\n\t\t\tTOC MAPPING\n\n");
1628 static char *
1629 h2 = N_(" TOC    disassembly  Comments       Name\n");
1630 static char *
1631 h3 = N_(" Offset  spelling                   (if present)\n");
1632
1633 void
1634 dump_toc (vfile)
1635      PTR vfile;
1636 {
1637   FILE *file = (FILE *) vfile;
1638   struct list_ele *t;
1639
1640   fprintf (file, _(h1));
1641   fprintf (file, _(h2));
1642   fprintf (file, _(h3));
1643
1644   for (t = head; t != 0; t=t->next)
1645     {
1646       const char *cat = "";
1647
1648       if (t->cat == priv)
1649         cat = _("private       ");
1650       else if (t->cat == pub)
1651         cat = _("public        ");
1652       else if (t->cat == tocdata)
1653         cat = _("data-in-toc   ");
1654
1655       if (t->offset > global_toc_size)
1656         {
1657           if (t->offset <= global_toc_size + thunk_size)
1658             cat = _("IAT reference ");
1659           else
1660             {
1661               fprintf (file,
1662                       _("**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n"),
1663                        global_toc_size, global_toc_size,
1664                        thunk_size, thunk_size);
1665               cat = _("Out of bounds!");
1666             }
1667         }
1668
1669       fprintf (file,
1670               " %04lx    (%d)", (unsigned long) t->offset, t->offset - 32768);
1671       fprintf (file,
1672               "    %s %s\n",
1673               cat, t->name);
1674
1675     }
1676
1677   fprintf (file, "\n");
1678 }
1679
1680 boolean
1681 ppc_allocate_toc_section (info)
1682      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1683 {
1684   asection *s;
1685   bfd_byte *foo;
1686   bfd_size_type amt;
1687   static char test_char = '1';
1688
1689   if ( global_toc_size == 0 ) /* FIXME: does this get me in trouble? */
1690     return true;
1691
1692   if (bfd_of_toc_owner == 0)
1693     {
1694       /* No toc owner? Something is very wrong.  */
1695       abort ();
1696     }
1697
1698   s = bfd_get_section_by_name ( bfd_of_toc_owner , TOC_SECTION_NAME);
1699   if (s == NULL)
1700     {
1701       /* No toc section? Something is very wrong.  */
1702       abort ();
1703     }
1704
1705   amt = global_toc_size;
1706   foo = (bfd_byte *) bfd_alloc (bfd_of_toc_owner, amt);
1707   memset(foo, test_char, (size_t) global_toc_size);
1708
1709   s->_raw_size = s->_cooked_size = global_toc_size;
1710   s->contents = foo;
1711
1712   return true;
1713 }
1714
1715 boolean
1716 ppc_process_before_allocation (abfd, info)
1717      bfd *abfd;
1718      struct bfd_link_info *info;
1719 {
1720   asection *sec;
1721   struct internal_reloc *i, *rel;
1722
1723   /* here we have a bfd that is to be included on the link. We have a hook
1724      to do reloc rummaging, before section sizes are nailed down.  */
1725
1726   _bfd_coff_get_external_symbols(abfd);
1727
1728   /* rummage around all the relocs and map the toc */
1729   sec = abfd->sections;
1730
1731   if (sec == 0)
1732     {
1733       return true;
1734     }
1735
1736   for (; sec != 0; sec = sec->next)
1737   {
1738     if (sec->reloc_count == 0)
1739       continue;
1740
1741     /* load the relocs */
1742     /* FIXME: there may be a storage leak here */
1743     i=_bfd_coff_read_internal_relocs(abfd,sec,1,0,0,0);
1744
1745     if (i == 0)
1746       abort ();
1747
1748     for (rel=i;rel<i+sec->reloc_count;++rel)
1749       {
1750         unsigned short r_type  = EXTRACT_TYPE (rel->r_type);
1751         unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
1752         boolean ok = true;
1753
1754         DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1755
1756         switch(r_type)
1757           {
1758           case IMAGE_REL_PPC_TOCREL16:
1759             /* if TOCDEFN is on, ignore as someone else has allocated the
1760                toc entry */
1761             if ( (r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN )
1762               ok = ppc_record_toc_entry(abfd, info, sec,
1763                                         rel->r_symndx, default_toc);
1764             if (!ok)
1765               return false;
1766             break;
1767           case IMAGE_REL_PPC_IMGLUE:
1768             ppc_mark_symbol_as_glue(abfd, rel->r_symndx, rel);
1769             break;
1770           default:
1771             break;
1772           }
1773       }
1774   }
1775
1776   return true;
1777 }
1778
1779 #endif
1780
1781 static bfd_reloc_status_type
1782 ppc_refhi_reloc (abfd,
1783                  reloc_entry,
1784                  symbol,
1785                  data,
1786                  input_section,
1787                  output_bfd,
1788                  error_message)
1789      bfd *abfd ATTRIBUTE_UNUSED;
1790      arelent *reloc_entry ATTRIBUTE_UNUSED;
1791      asymbol *symbol ATTRIBUTE_UNUSED;
1792      PTR data ATTRIBUTE_UNUSED;
1793      asection *input_section ATTRIBUTE_UNUSED;
1794      bfd *output_bfd;
1795      char **error_message ATTRIBUTE_UNUSED;
1796 {
1797   UN_IMPL("REFHI");
1798   DUMP_RELOC("REFHI",reloc_entry);
1799
1800   if (output_bfd == (bfd *) NULL)
1801     return bfd_reloc_continue;
1802
1803   return bfd_reloc_undefined;
1804 }
1805
1806 #if 0
1807
1808 static bfd_reloc_status_type
1809 ppc_reflo_reloc (abfd,
1810                  reloc_entry,
1811                  symbol,
1812                  data,
1813                  input_section,
1814                  output_bfd,
1815                  error_message)
1816      bfd *abfd;
1817      arelent *reloc_entry;
1818      asymbol *symbol;
1819      PTR data;
1820      asection *input_section;
1821      bfd *output_bfd;
1822      char **error_message;
1823 {
1824   UN_IMPL("REFLO");
1825   DUMP_RELOC("REFLO",reloc_entry);
1826
1827   if (output_bfd == (bfd *) NULL)
1828     return bfd_reloc_continue;
1829
1830   return bfd_reloc_undefined;
1831 }
1832
1833 #endif
1834
1835 static bfd_reloc_status_type
1836 ppc_pair_reloc (abfd,
1837                 reloc_entry,
1838                 symbol,
1839                 data,
1840                 input_section,
1841                 output_bfd,
1842                 error_message)
1843      bfd *abfd ATTRIBUTE_UNUSED;
1844      arelent *reloc_entry ATTRIBUTE_UNUSED;
1845      asymbol *symbol ATTRIBUTE_UNUSED;
1846      PTR data ATTRIBUTE_UNUSED;
1847      asection *input_section ATTRIBUTE_UNUSED;
1848      bfd *output_bfd;
1849      char **error_message ATTRIBUTE_UNUSED;
1850 {
1851   UN_IMPL("PAIR");
1852   DUMP_RELOC("PAIR",reloc_entry);
1853
1854   if (output_bfd == (bfd *) NULL)
1855     return bfd_reloc_continue;
1856
1857   return bfd_reloc_undefined;
1858 }
1859 \f
1860 static bfd_reloc_status_type
1861 ppc_toc16_reloc (abfd,
1862                  reloc_entry,
1863                  symbol,
1864                  data,
1865                  input_section,
1866                  output_bfd,
1867                  error_message)
1868      bfd *abfd ATTRIBUTE_UNUSED;
1869      arelent *reloc_entry ATTRIBUTE_UNUSED;
1870      asymbol *symbol ATTRIBUTE_UNUSED;
1871      PTR data ATTRIBUTE_UNUSED;
1872      asection *input_section ATTRIBUTE_UNUSED;
1873      bfd *output_bfd;
1874      char **error_message ATTRIBUTE_UNUSED;
1875 {
1876   UN_IMPL("TOCREL16");
1877   DUMP_RELOC("TOCREL16",reloc_entry);
1878
1879   if (output_bfd == (bfd *) NULL)
1880     {
1881       return bfd_reloc_continue;
1882     }
1883
1884   return bfd_reloc_ok;
1885 }
1886
1887 #if 0
1888
1889 /* ADDR32NB : 32 bit address relative to the virtual origin.         */
1890 /*            (On the alpha, this is always a linker generated thunk)*/
1891 /*            (i.e. 32bit addr relative to the image base)           */
1892 /*                                                                   */
1893 /*                                                                   */
1894
1895 static bfd_reloc_status_type
1896 ppc_addr32nb_reloc (abfd,
1897                     reloc_entry,
1898                     symbol,
1899                     data,
1900                     input_section,
1901                     output_bfd,
1902                     error_message)
1903      bfd *abfd;
1904      arelent *reloc_entry;
1905      asymbol *symbol;
1906      PTR data;
1907      asection *input_section;
1908      bfd *output_bfd;
1909      char **error_message;
1910 {
1911   UN_IMPL("ADDR32NB");
1912   DUMP_RELOC("ADDR32NB",reloc_entry);
1913
1914   return bfd_reloc_ok;
1915 }
1916
1917 #endif
1918
1919 static bfd_reloc_status_type
1920 ppc_secrel_reloc (abfd,
1921                   reloc_entry,
1922                   symbol,
1923                   data,
1924                   input_section,
1925                   output_bfd,
1926                   error_message)
1927      bfd *abfd ATTRIBUTE_UNUSED;
1928      arelent *reloc_entry ATTRIBUTE_UNUSED;
1929      asymbol *symbol ATTRIBUTE_UNUSED;
1930      PTR data ATTRIBUTE_UNUSED;
1931      asection *input_section ATTRIBUTE_UNUSED;
1932      bfd *output_bfd;
1933      char **error_message ATTRIBUTE_UNUSED;
1934 {
1935   UN_IMPL("SECREL");
1936   DUMP_RELOC("SECREL",reloc_entry);
1937
1938   if (output_bfd == (bfd *) NULL)
1939     return bfd_reloc_continue;
1940
1941   return bfd_reloc_ok;
1942 }
1943
1944 static bfd_reloc_status_type
1945 ppc_section_reloc (abfd,
1946                    reloc_entry,
1947                    symbol,
1948                    data,
1949                    input_section,
1950                    output_bfd,
1951                    error_message)
1952      bfd *abfd ATTRIBUTE_UNUSED;
1953      arelent *reloc_entry ATTRIBUTE_UNUSED;
1954      asymbol *symbol ATTRIBUTE_UNUSED;
1955      PTR data ATTRIBUTE_UNUSED;
1956      asection *input_section ATTRIBUTE_UNUSED;
1957      bfd *output_bfd;
1958      char **error_message ATTRIBUTE_UNUSED;
1959 {
1960   UN_IMPL("SECTION");
1961   DUMP_RELOC("SECTION",reloc_entry);
1962
1963   if (output_bfd == (bfd *) NULL)
1964     return bfd_reloc_continue;
1965
1966   return bfd_reloc_ok;
1967 }
1968
1969 static bfd_reloc_status_type
1970 ppc_imglue_reloc (abfd,
1971                   reloc_entry,
1972                   symbol,
1973                   data,
1974                   input_section,
1975                   output_bfd,
1976                   error_message)
1977      bfd *abfd ATTRIBUTE_UNUSED;
1978      arelent *reloc_entry ATTRIBUTE_UNUSED;
1979      asymbol *symbol ATTRIBUTE_UNUSED;
1980      PTR data ATTRIBUTE_UNUSED;
1981      asection *input_section ATTRIBUTE_UNUSED;
1982      bfd *output_bfd;
1983      char **error_message ATTRIBUTE_UNUSED;
1984 {
1985   UN_IMPL("IMGLUE");
1986   DUMP_RELOC("IMGLUE",reloc_entry);
1987
1988   if (output_bfd == (bfd *) NULL)
1989     return bfd_reloc_continue;
1990
1991   return bfd_reloc_ok;
1992 }
1993 \f
1994 #define MAX_RELOC_INDEX  \
1995       (sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]) - 1)
1996
1997 /* FIXME: There is a possiblity that when we read in a reloc from a file,
1998           that there are some bits encoded in the upper portion of the
1999           type field. Not yet implemented.
2000 */
2001 static void ppc_coff_rtype2howto PARAMS ((arelent *relent,
2002                                           struct internal_reloc *internal));
2003
2004 static void
2005 ppc_coff_rtype2howto (relent, internal)
2006      arelent *relent;
2007      struct internal_reloc *internal;
2008 {
2009
2010   /* We can encode one of three things in the type field, aside from the
2011      type:
2012      1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2013         value, rather than an addition value
2014      2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2015         the branch is expected to be taken or not.
2016      3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2017      For now, we just strip this stuff to find the type, and ignore it other
2018      than that.
2019   */
2020   reloc_howto_type *howto;
2021   unsigned short r_type  = EXTRACT_TYPE (internal->r_type);
2022   unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
2023   unsigned short junk    = EXTRACT_JUNK (internal->r_type);
2024
2025   /* the masking process only slices off the bottom byte for r_type.  */
2026   if ( r_type > MAX_RELOC_INDEX )
2027     abort ();
2028
2029   /* check for absolute crap */
2030   if ( junk != 0 )
2031     abort ();
2032
2033   switch(r_type)
2034     {
2035     case IMAGE_REL_PPC_ADDR16:
2036     case IMAGE_REL_PPC_REL24:
2037     case IMAGE_REL_PPC_ADDR24:
2038     case IMAGE_REL_PPC_ADDR32:
2039     case IMAGE_REL_PPC_IFGLUE:
2040     case IMAGE_REL_PPC_ADDR32NB:
2041     case IMAGE_REL_PPC_SECTION:
2042     case IMAGE_REL_PPC_SECREL:
2043       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2044       howto = ppc_coff_howto_table + r_type;
2045       break;
2046     case IMAGE_REL_PPC_IMGLUE:
2047       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2048       howto = ppc_coff_howto_table + r_type;
2049       break;
2050     case IMAGE_REL_PPC_TOCREL16:
2051       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2052       if (r_flags & IMAGE_REL_PPC_TOCDEFN)
2053         howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
2054       else
2055         howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
2056       break;
2057     default:
2058       fprintf (stderr,
2059               _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
2060               ppc_coff_howto_table[r_type].name,
2061               r_type);
2062       howto = ppc_coff_howto_table + r_type;
2063       break;
2064     }
2065
2066   relent->howto = howto;
2067
2068 }
2069
2070 static reloc_howto_type *
2071 coff_ppc_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
2072      bfd *abfd ATTRIBUTE_UNUSED;
2073      asection *sec;
2074      struct internal_reloc *rel;
2075      struct coff_link_hash_entry *h ATTRIBUTE_UNUSED;
2076      struct internal_syment *sym ATTRIBUTE_UNUSED;
2077      bfd_vma *addendp;
2078 {
2079   reloc_howto_type *howto;
2080
2081   /* We can encode one of three things in the type field, aside from the
2082      type:
2083      1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2084         value, rather than an addition value
2085      2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2086         the branch is expected to be taken or not.
2087      3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2088      For now, we just strip this stuff to find the type, and ignore it other
2089      than that.
2090   */
2091
2092   unsigned short r_type  = EXTRACT_TYPE (rel->r_type);
2093   unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
2094   unsigned short junk    = EXTRACT_JUNK (rel->r_type);
2095
2096   /* the masking process only slices off the bottom byte for r_type.  */
2097   if ( r_type > MAX_RELOC_INDEX )
2098     abort ();
2099
2100   /* check for absolute crap */
2101   if ( junk != 0 )
2102     abort ();
2103
2104   switch(r_type)
2105     {
2106     case IMAGE_REL_PPC_ADDR32NB:
2107       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2108       *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
2109       howto = ppc_coff_howto_table + r_type;
2110       break;
2111     case IMAGE_REL_PPC_TOCREL16:
2112       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2113       if (r_flags & IMAGE_REL_PPC_TOCDEFN)
2114         howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
2115       else
2116         howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
2117       break;
2118     case IMAGE_REL_PPC_ADDR16:
2119     case IMAGE_REL_PPC_REL24:
2120     case IMAGE_REL_PPC_ADDR24:
2121     case IMAGE_REL_PPC_ADDR32:
2122     case IMAGE_REL_PPC_IFGLUE:
2123     case IMAGE_REL_PPC_SECTION:
2124     case IMAGE_REL_PPC_SECREL:
2125       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2126       howto = ppc_coff_howto_table + r_type;
2127       break;
2128     case IMAGE_REL_PPC_IMGLUE:
2129       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2130       howto = ppc_coff_howto_table + r_type;
2131       break;
2132     default:
2133       fprintf (stderr,
2134               _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
2135               ppc_coff_howto_table[r_type].name,
2136               r_type);
2137       howto = ppc_coff_howto_table + r_type;
2138       break;
2139     }
2140
2141   return howto;
2142 }
2143
2144 /* a cheesy little macro to make the code a little more readable */
2145 #define HOW2MAP(bfd_rtype,ppc_rtype)  \
2146  case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
2147
2148 static reloc_howto_type *ppc_coff_reloc_type_lookup
2149 PARAMS ((bfd *, bfd_reloc_code_real_type));
2150
2151 static reloc_howto_type *
2152 ppc_coff_reloc_type_lookup (abfd, code)
2153      bfd *abfd ATTRIBUTE_UNUSED;
2154      bfd_reloc_code_real_type code;
2155 {
2156   switch (code)
2157     {
2158       HOW2MAP(BFD_RELOC_32_GOTOFF,    IMAGE_REL_PPC_IMGLUE);
2159       HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
2160       HOW2MAP(BFD_RELOC_16,           IMAGE_REL_PPC_ADDR16);
2161       HOW2MAP(BFD_RELOC_PPC_B26,      IMAGE_REL_PPC_REL24);
2162       HOW2MAP(BFD_RELOC_PPC_BA26,     IMAGE_REL_PPC_ADDR24);
2163       HOW2MAP(BFD_RELOC_PPC_TOC16,    IMAGE_REL_PPC_TOCREL16);
2164       HOW2MAP(BFD_RELOC_16_GOTOFF,    IMAGE_REL_PPC_TOCREL16_DEFN);
2165       HOW2MAP(BFD_RELOC_32,           IMAGE_REL_PPC_ADDR32);
2166       HOW2MAP(BFD_RELOC_RVA,          IMAGE_REL_PPC_ADDR32NB);
2167     default:
2168       return NULL;
2169     }
2170   /*NOTREACHED*/
2171 }
2172
2173 #undef HOW2MAP
2174 \f
2175 /* Tailor coffcode.h -- macro heaven.  */
2176
2177 #define RTYPE2HOWTO(cache_ptr, dst)  ppc_coff_rtype2howto (cache_ptr, dst)
2178
2179 #ifndef COFF_IMAGE_WITH_PE
2180 static void ppc_coff_swap_sym_in_hook PARAMS ((bfd *, PTR, PTR));
2181 #endif
2182
2183 /* We use the special COFF backend linker, with our own special touch.  */
2184
2185 #define coff_bfd_reloc_type_lookup   ppc_coff_reloc_type_lookup
2186 #define coff_rtype_to_howto          coff_ppc_rtype_to_howto
2187 #define coff_relocate_section        coff_ppc_relocate_section
2188 #define coff_bfd_final_link          ppc_bfd_coff_final_link
2189
2190 #ifndef COFF_IMAGE_WITH_PE
2191 /* FIXME: This no longer works.  */
2192 #define coff_swap_sym_in_hook        ppc_coff_swap_sym_in_hook
2193 #endif
2194
2195 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
2196
2197 #define COFF_PAGE_SIZE                       0x1000
2198
2199 /* FIXME: This controls some code that used to be in peicode.h and is
2200    now in peigen.c.  It will not control the code in peigen.c.  If
2201    anybody wants to get this working, you will need to fix that.  */
2202 #define POWERPC_LE_PE
2203
2204 #define COFF_SECTION_ALIGNMENT_ENTRIES \
2205 { COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
2206   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2207 { COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
2208   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2209 { COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
2210   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2211 { COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
2212   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2213 { COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
2214   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
2215 { COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
2216   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
2217
2218 #include "coffcode.h"
2219 \f
2220 #ifndef COFF_IMAGE_WITH_PE
2221 /* FIXME:
2222    What we're trying to do here is allocate a toc section (early), and attach
2223    it to the last bfd to be processed. This avoids the problem of having a toc
2224    written out before all files have been processed. This code allocates
2225    a toc section for every file, and records the last one seen. There are
2226    at least two problems with this approach:
2227    1. We allocate whole bunches of toc sections that are ignored, but at
2228       at least we will not allocate a toc if no .toc is present.
2229    2. It's not clear to me that being the last bfd read necessarily means
2230       that you are the last bfd closed.
2231    3. Doing it on a "swap in" hook depends on when the "swap in" is called,
2232       and how often, etc. It's not clear to me that there isn't a hole here.
2233 */
2234
2235 static void
2236 ppc_coff_swap_sym_in_hook (abfd, ext1, in1)
2237      bfd            *abfd;
2238      PTR ext1 ATTRIBUTE_UNUSED;
2239      PTR in1;
2240 {
2241   struct internal_syment      *in = (struct internal_syment *)in1;
2242
2243   if (bfd_of_toc_owner != 0) /* we already have a toc, so go home */
2244     return;
2245
2246   if (strcmp(in->_n._n_name, ".toc") == 0)
2247     {
2248       flagword flags;
2249       register asection *s;
2250
2251       s = bfd_get_section_by_name ( abfd , TOC_SECTION_NAME);
2252       if (s != NULL)
2253         {
2254           return;
2255         }
2256
2257       flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY ;
2258
2259       s = bfd_make_section (abfd, TOC_SECTION_NAME);
2260
2261       if (s == NULL
2262           || !bfd_set_section_flags (abfd, s, flags)
2263           || !bfd_set_section_alignment (abfd, s, 2))
2264         {
2265           /* FIXME: set appropriate bfd error */
2266           abort ();
2267         }
2268
2269       /* save the bfd for later allocation */
2270       bfd_of_toc_owner = abfd;
2271     }
2272
2273   return;
2274 }
2275 #endif
2276
2277 #ifndef COFF_IMAGE_WITH_PE
2278
2279 static boolean ppc_do_last PARAMS ((bfd *));
2280 static bfd *ppc_get_last PARAMS ((void));
2281
2282 static boolean
2283 ppc_do_last (abfd)
2284      bfd *abfd;
2285 {
2286   if (abfd == bfd_of_toc_owner)
2287     return true;
2288   else
2289     return false;
2290 }
2291
2292 static bfd *
2293 ppc_get_last()
2294 {
2295   return bfd_of_toc_owner;
2296 }
2297
2298 /* this piece of machinery exists only to guarantee that the bfd that holds
2299    the toc section is written last.
2300
2301    This does depend on bfd_make_section attaching a new section to the
2302    end of the section list for the bfd.
2303
2304    This is otherwise intended to be functionally the same as
2305    cofflink.c:_bfd_coff_final_link(). It is specifically different only
2306    where the POWERPC_LE_PE macro modifies the code. It is left in as a
2307    precise form of comment. krk@cygnus.com
2308 */
2309
2310 /* Do the final link step.  */
2311
2312 boolean
2313 ppc_bfd_coff_final_link (abfd, info)
2314      bfd *abfd;
2315      struct bfd_link_info *info;
2316 {
2317   bfd_size_type symesz;
2318   struct coff_final_link_info finfo;
2319   boolean debug_merge_allocated;
2320   asection *o;
2321   struct bfd_link_order *p;
2322   bfd_size_type max_sym_count;
2323   bfd_size_type max_lineno_count;
2324   bfd_size_type max_reloc_count;
2325   bfd_size_type max_output_reloc_count;
2326   bfd_size_type max_contents_size;
2327   file_ptr rel_filepos;
2328   unsigned int relsz;
2329   file_ptr line_filepos;
2330   unsigned int linesz;
2331   bfd *sub;
2332   bfd_byte *external_relocs = NULL;
2333   char strbuf[STRING_SIZE_SIZE];
2334   bfd_size_type amt;
2335
2336   symesz = bfd_coff_symesz (abfd);
2337
2338   finfo.info = info;
2339   finfo.output_bfd = abfd;
2340   finfo.strtab = NULL;
2341   finfo.section_info = NULL;
2342   finfo.last_file_index = -1;
2343   finfo.last_bf_index = -1;
2344   finfo.internal_syms = NULL;
2345   finfo.sec_ptrs = NULL;
2346   finfo.sym_indices = NULL;
2347   finfo.outsyms = NULL;
2348   finfo.linenos = NULL;
2349   finfo.contents = NULL;
2350   finfo.external_relocs = NULL;
2351   finfo.internal_relocs = NULL;
2352   debug_merge_allocated = false;
2353
2354   coff_data (abfd)->link_info = info;
2355
2356   finfo.strtab = _bfd_stringtab_init ();
2357   if (finfo.strtab == NULL)
2358     goto error_return;
2359
2360   if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
2361     goto error_return;
2362   debug_merge_allocated = true;
2363
2364   /* Compute the file positions for all the sections.  */
2365   if (! abfd->output_has_begun)
2366     {
2367       if (! bfd_coff_compute_section_file_positions (abfd))
2368         return false;
2369     }
2370
2371   /* Count the line numbers and relocation entries required for the
2372      output file.  Set the file positions for the relocs.  */
2373   rel_filepos = obj_relocbase (abfd);
2374   relsz = bfd_coff_relsz (abfd);
2375   max_contents_size = 0;
2376   max_lineno_count = 0;
2377   max_reloc_count = 0;
2378
2379   for (o = abfd->sections; o != NULL; o = o->next)
2380     {
2381       o->reloc_count = 0;
2382       o->lineno_count = 0;
2383       for (p = o->link_order_head; p != NULL; p = p->next)
2384         {
2385
2386           if (p->type == bfd_indirect_link_order)
2387             {
2388               asection *sec;
2389
2390               sec = p->u.indirect.section;
2391
2392               /* Mark all sections which are to be included in the
2393                  link.  This will normally be every section.  We need
2394                  to do this so that we can identify any sections which
2395                  the linker has decided to not include.  */
2396               sec->linker_mark = true;
2397
2398               if (info->strip == strip_none
2399                   || info->strip == strip_some)
2400                 o->lineno_count += sec->lineno_count;
2401
2402               if (info->relocateable)
2403                 o->reloc_count += sec->reloc_count;
2404
2405               if (sec->_raw_size > max_contents_size)
2406                 max_contents_size = sec->_raw_size;
2407               if (sec->lineno_count > max_lineno_count)
2408                 max_lineno_count = sec->lineno_count;
2409               if (sec->reloc_count > max_reloc_count)
2410                 max_reloc_count = sec->reloc_count;
2411             }
2412           else if (info->relocateable
2413                    && (p->type == bfd_section_reloc_link_order
2414                        || p->type == bfd_symbol_reloc_link_order))
2415             ++o->reloc_count;
2416         }
2417       if (o->reloc_count == 0)
2418         o->rel_filepos = 0;
2419       else
2420         {
2421           o->flags |= SEC_RELOC;
2422           o->rel_filepos = rel_filepos;
2423           rel_filepos += o->reloc_count * relsz;
2424         }
2425     }
2426
2427   /* If doing a relocateable link, allocate space for the pointers we
2428      need to keep.  */
2429   if (info->relocateable)
2430     {
2431       unsigned int i;
2432
2433       /* We use section_count + 1, rather than section_count, because
2434          the target_index fields are 1 based.  */
2435       amt = abfd->section_count + 1;
2436       amt *= sizeof (struct coff_link_section_info);
2437       finfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
2438       if (finfo.section_info == NULL)
2439         goto error_return;
2440       for (i = 0; i <= abfd->section_count; i++)
2441         {
2442           finfo.section_info[i].relocs = NULL;
2443           finfo.section_info[i].rel_hashes = NULL;
2444         }
2445     }
2446
2447   /* We now know the size of the relocs, so we can determine the file
2448      positions of the line numbers.  */
2449   line_filepos = rel_filepos;
2450   linesz = bfd_coff_linesz (abfd);
2451   max_output_reloc_count = 0;
2452   for (o = abfd->sections; o != NULL; o = o->next)
2453     {
2454       if (o->lineno_count == 0)
2455         o->line_filepos = 0;
2456       else
2457         {
2458           o->line_filepos = line_filepos;
2459           line_filepos += o->lineno_count * linesz;
2460         }
2461
2462       if (o->reloc_count != 0)
2463         {
2464           /* We don't know the indices of global symbols until we have
2465              written out all the local symbols.  For each section in
2466              the output file, we keep an array of pointers to hash
2467              table entries.  Each entry in the array corresponds to a
2468              reloc.  When we find a reloc against a global symbol, we
2469              set the corresponding entry in this array so that we can
2470              fix up the symbol index after we have written out all the
2471              local symbols.
2472
2473              Because of this problem, we also keep the relocs in
2474              memory until the end of the link.  This wastes memory,
2475              but only when doing a relocateable link, which is not the
2476              common case.  */
2477           BFD_ASSERT (info->relocateable);
2478           amt = o->reloc_count;
2479           amt *= sizeof (struct internal_reloc);
2480           finfo.section_info[o->target_index].relocs =
2481             (struct internal_reloc *) bfd_malloc (amt);
2482           amt = o->reloc_count;
2483           amt *= sizeof (struct coff_link_hash_entry *);
2484           finfo.section_info[o->target_index].rel_hashes =
2485             (struct coff_link_hash_entry **) bfd_malloc (amt);
2486           if (finfo.section_info[o->target_index].relocs == NULL
2487               || finfo.section_info[o->target_index].rel_hashes == NULL)
2488             goto error_return;
2489
2490           if (o->reloc_count > max_output_reloc_count)
2491             max_output_reloc_count = o->reloc_count;
2492         }
2493
2494       /* Reset the reloc and lineno counts, so that we can use them to
2495          count the number of entries we have output so far.  */
2496       o->reloc_count = 0;
2497       o->lineno_count = 0;
2498     }
2499
2500   obj_sym_filepos (abfd) = line_filepos;
2501
2502   /* Figure out the largest number of symbols in an input BFD.  Take
2503      the opportunity to clear the output_has_begun fields of all the
2504      input BFD's.  */
2505   max_sym_count = 0;
2506   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2507     {
2508       bfd_size_type sz;
2509
2510       sub->output_has_begun = false;
2511       sz = obj_raw_syment_count (sub);
2512       if (sz > max_sym_count)
2513         max_sym_count = sz;
2514     }
2515
2516   /* Allocate some buffers used while linking.  */
2517   amt = max_sym_count * sizeof (struct internal_syment);
2518   finfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
2519   amt = max_sym_count * sizeof (asection *);
2520   finfo.sec_ptrs = (asection **) bfd_malloc (amt);
2521   amt = max_sym_count * sizeof (long);
2522   finfo.sym_indices = (long *) bfd_malloc (amt);
2523   amt = (max_sym_count + 1) * symesz;
2524   finfo.outsyms = (bfd_byte *) bfd_malloc (amt);
2525   amt = max_lineno_count * bfd_coff_linesz (abfd);
2526   finfo.linenos = (bfd_byte *) bfd_malloc (amt);
2527   finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
2528   finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
2529   if (! info->relocateable)
2530     {
2531       amt = max_reloc_count * sizeof (struct internal_reloc);
2532       finfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
2533     }
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       file_ptr pos;
2653
2654       finfo.last_file.n_value = obj_raw_syment_count (abfd);
2655       bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
2656                              (PTR) finfo.outsyms);
2657       pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
2658       if (bfd_seek (abfd, pos, SEEK_SET) != 0
2659           || bfd_bwrite (finfo.outsyms, symesz, 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       amt = max_output_reloc_count * relsz;
2683       external_relocs = (bfd_byte *) bfd_malloc (amt);
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           amt = relsz * o->reloc_count;
2712           if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
2713               || bfd_bwrite ((PTR) external_relocs, amt, abfd) != amt)
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       file_ptr pos;
2748
2749       pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
2750       if (bfd_seek (abfd, pos, SEEK_SET) != 0)
2751         return false;
2752
2753 #if STRING_SIZE_SIZE == 4
2754       H_PUT_32 (abfd,
2755                 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
2756                 strbuf);
2757 #else
2758  #error Change H_PUT_32 above
2759 #endif
2760
2761       if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
2762           != STRING_SIZE_SIZE)
2763         return false;
2764
2765       if (! _bfd_stringtab_emit (abfd, finfo.strtab))
2766         return false;
2767     }
2768
2769   _bfd_stringtab_free (finfo.strtab);
2770
2771   /* Setting bfd_get_symcount to 0 will cause write_object_contents to
2772      not try to write out the symbols.  */
2773   bfd_get_symcount (abfd) = 0;
2774
2775   return true;
2776
2777  error_return:
2778   if (debug_merge_allocated)
2779     coff_debug_merge_hash_table_free (&finfo.debug_merge);
2780   if (finfo.strtab != NULL)
2781     _bfd_stringtab_free (finfo.strtab);
2782   if (finfo.section_info != NULL)
2783     {
2784       unsigned int i;
2785
2786       for (i = 0; i < abfd->section_count; i++)
2787         {
2788           if (finfo.section_info[i].relocs != NULL)
2789             free (finfo.section_info[i].relocs);
2790           if (finfo.section_info[i].rel_hashes != NULL)
2791             free (finfo.section_info[i].rel_hashes);
2792         }
2793       free (finfo.section_info);
2794     }
2795   if (finfo.internal_syms != NULL)
2796     free (finfo.internal_syms);
2797   if (finfo.sec_ptrs != NULL)
2798     free (finfo.sec_ptrs);
2799   if (finfo.sym_indices != NULL)
2800     free (finfo.sym_indices);
2801   if (finfo.outsyms != NULL)
2802     free (finfo.outsyms);
2803   if (finfo.linenos != NULL)
2804     free (finfo.linenos);
2805   if (finfo.contents != NULL)
2806     free (finfo.contents);
2807   if (finfo.external_relocs != NULL)
2808     free (finfo.external_relocs);
2809   if (finfo.internal_relocs != NULL)
2810     free (finfo.internal_relocs);
2811   if (external_relocs != NULL)
2812     free (external_relocs);
2813   return false;
2814 }
2815 #endif
2816 \f
2817 /* Forward declaration for use by alternative_target field.  */
2818 #ifdef TARGET_BIG_SYM
2819 extern const bfd_target TARGET_BIG_SYM;
2820 #endif
2821
2822 /* The transfer vectors that lead the outside world to all of the above.  */
2823
2824 #ifdef TARGET_LITTLE_SYM
2825 const bfd_target TARGET_LITTLE_SYM =
2826 {
2827   TARGET_LITTLE_NAME,           /* name or coff-arm-little */
2828   bfd_target_coff_flavour,
2829   BFD_ENDIAN_LITTLE,            /* data byte order is little */
2830   BFD_ENDIAN_LITTLE,            /* header byte order is little */
2831
2832   (HAS_RELOC | EXEC_P |         /* FIXME: object flags */
2833    HAS_LINENO | HAS_DEBUG |
2834    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2835
2836 #ifndef COFF_WITH_PE
2837   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2838 #else
2839   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
2840    | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2841 #endif
2842
2843   0,                            /* leading char */
2844   '/',                          /* ar_pad_char */
2845   15,                           /* ar_max_namelen??? FIXMEmgo */
2846
2847   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2848   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2849   bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2850
2851   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2852   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2853   bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2854
2855   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
2856      bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2857   {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2858      bfd_false},
2859   {bfd_false, coff_write_object_contents,       /* bfd_write_contents */
2860      _bfd_write_archive_contents, bfd_false},
2861
2862   BFD_JUMP_TABLE_GENERIC (coff),
2863   BFD_JUMP_TABLE_COPY (coff),
2864   BFD_JUMP_TABLE_CORE (_bfd_nocore),
2865   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2866   BFD_JUMP_TABLE_SYMBOLS (coff),
2867   BFD_JUMP_TABLE_RELOCS (coff),
2868   BFD_JUMP_TABLE_WRITE (coff),
2869   BFD_JUMP_TABLE_LINK (coff),
2870   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2871
2872   /* Alternative_target.  */
2873 #ifdef TARGET_BIG_SYM
2874   & TARGET_BIG_SYM,
2875 #else
2876   NULL,
2877 #endif
2878
2879   COFF_SWAP_TABLE
2880 };
2881 #endif
2882
2883 #ifdef TARGET_BIG_SYM
2884 const bfd_target TARGET_BIG_SYM =
2885 {
2886   TARGET_BIG_NAME,
2887   bfd_target_coff_flavour,
2888   BFD_ENDIAN_BIG,               /* data byte order is big */
2889   BFD_ENDIAN_BIG,               /* header byte order is big */
2890
2891   (HAS_RELOC | EXEC_P |         /* FIXME: object flags */
2892    HAS_LINENO | HAS_DEBUG |
2893    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2894
2895 #ifndef COFF_WITH_PE
2896   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2897 #else
2898   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
2899    | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2900 #endif
2901
2902   0,                            /* leading char */
2903   '/',                          /* ar_pad_char */
2904   15,                           /* ar_max_namelen??? FIXMEmgo */
2905
2906   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2907   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2908   bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
2909
2910   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2911   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2912   bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
2913
2914   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
2915      bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2916   {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2917      bfd_false},
2918   {bfd_false, coff_write_object_contents,       /* bfd_write_contents */
2919      _bfd_write_archive_contents, bfd_false},
2920
2921   BFD_JUMP_TABLE_GENERIC (coff),
2922   BFD_JUMP_TABLE_COPY (coff),
2923   BFD_JUMP_TABLE_CORE (_bfd_nocore),
2924   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2925   BFD_JUMP_TABLE_SYMBOLS (coff),
2926   BFD_JUMP_TABLE_RELOCS (coff),
2927   BFD_JUMP_TABLE_WRITE (coff),
2928   BFD_JUMP_TABLE_LINK (coff),
2929   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2930
2931   /* Alternative_target.  */
2932 #ifdef TARGET_LITTLE_SYM
2933   & TARGET_LITTLE_SYM,
2934 #else
2935   NULL,
2936 #endif
2937
2938   COFF_SWAP_TABLE
2939 };
2940
2941 #endif