Merge from vendor branch GCC:
[dragonfly.git] / contrib / binutils / 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_archive_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_archive_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_archive_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_archive_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_archive_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 /* We use the special COFF backend linker, with our own special touch.  */
2180
2181 #define coff_bfd_reloc_type_lookup   ppc_coff_reloc_type_lookup
2182 #define coff_rtype_to_howto          coff_ppc_rtype_to_howto
2183 #define coff_relocate_section        coff_ppc_relocate_section
2184 #define coff_bfd_final_link          ppc_bfd_coff_final_link
2185
2186 #ifndef COFF_IMAGE_WITH_PE
2187 /* FIXME: This no longer works.  */
2188 #if 0
2189 #define coff_swap_sym_in_hook        ppc_coff_swap_sym_in_hook
2190 #endif
2191 #endif
2192
2193 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
2194
2195 #define COFF_PAGE_SIZE                       0x1000
2196
2197 /* FIXME: This controls some code that used to be in peicode.h and is
2198    now in peigen.c.  It will not control the code in peigen.c.  If
2199    anybody wants to get this working, you will need to fix that.  */
2200 #define POWERPC_LE_PE
2201
2202 #define COFF_SECTION_ALIGNMENT_ENTRIES \
2203 { COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
2204   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2205 { COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
2206   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2207 { COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
2208   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2209 { COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
2210   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2211 { COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
2212   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
2213 { COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
2214   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
2215
2216 #include "coffcode.h"
2217 \f
2218 #ifndef COFF_IMAGE_WITH_PE
2219 /* FIXME: This no longer works.  */
2220 #if 0
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 static void ppc_coff_swap_sym_in_hook PARAMS ((bfd *, PTR, PTR));
2235
2236 static void
2237 ppc_coff_swap_sym_in_hook (abfd, ext1, in1)
2238      bfd            *abfd;
2239      PTR ext1 ATTRIBUTE_UNUSED;
2240      PTR in1;
2241 {
2242   struct internal_syment      *in = (struct internal_syment *)in1;
2243
2244   if (bfd_of_toc_owner != 0) /* we already have a toc, so go home */
2245     return;
2246
2247   if (strcmp(in->_n._n_name, ".toc") == 0)
2248     {
2249       flagword flags;
2250       register asection *s;
2251
2252       s = bfd_get_section_by_name ( abfd , TOC_SECTION_NAME);
2253       if (s != NULL)
2254         {
2255           return;
2256         }
2257
2258       flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY ;
2259
2260       s = bfd_make_section (abfd, TOC_SECTION_NAME);
2261
2262       if (s == NULL
2263           || !bfd_set_section_flags (abfd, s, flags)
2264           || !bfd_set_section_alignment (abfd, s, 2))
2265         {
2266           /* FIXME: set appropriate bfd error */
2267           abort ();
2268         }
2269
2270       /* save the bfd for later allocation */
2271       bfd_of_toc_owner = abfd;
2272     }
2273
2274   return;
2275 }
2276 #endif
2277 #endif
2278
2279 #ifndef COFF_IMAGE_WITH_PE
2280
2281 static boolean ppc_do_last PARAMS ((bfd *));
2282 static bfd *ppc_get_last PARAMS ((void));
2283
2284 static boolean
2285 ppc_do_last (abfd)
2286      bfd *abfd;
2287 {
2288   if (abfd == bfd_of_toc_owner)
2289     return true;
2290   else
2291     return false;
2292 }
2293
2294 static bfd *
2295 ppc_get_last()
2296 {
2297   return bfd_of_toc_owner;
2298 }
2299
2300 /* this piece of machinery exists only to guarantee that the bfd that holds
2301    the toc section is written last.
2302
2303    This does depend on bfd_make_section attaching a new section to the
2304    end of the section list for the bfd.
2305
2306    This is otherwise intended to be functionally the same as
2307    cofflink.c:_bfd_coff_final_link(). It is specifically different only
2308    where the POWERPC_LE_PE macro modifies the code. It is left in as a
2309    precise form of comment. krk@cygnus.com
2310 */
2311
2312 /* Do the final link step.  */
2313
2314 boolean
2315 ppc_bfd_coff_final_link (abfd, info)
2316      bfd *abfd;
2317      struct bfd_link_info *info;
2318 {
2319   bfd_size_type symesz;
2320   struct coff_final_link_info finfo;
2321   boolean debug_merge_allocated;
2322   asection *o;
2323   struct bfd_link_order *p;
2324   bfd_size_type max_sym_count;
2325   bfd_size_type max_lineno_count;
2326   bfd_size_type max_reloc_count;
2327   bfd_size_type max_output_reloc_count;
2328   bfd_size_type max_contents_size;
2329   file_ptr rel_filepos;
2330   unsigned int relsz;
2331   file_ptr line_filepos;
2332   unsigned int linesz;
2333   bfd *sub;
2334   bfd_byte *external_relocs = NULL;
2335   char strbuf[STRING_SIZE_SIZE];
2336   bfd_size_type amt;
2337
2338   symesz = bfd_coff_symesz (abfd);
2339
2340   finfo.info = info;
2341   finfo.output_bfd = abfd;
2342   finfo.strtab = NULL;
2343   finfo.section_info = NULL;
2344   finfo.last_file_index = -1;
2345   finfo.last_bf_index = -1;
2346   finfo.internal_syms = NULL;
2347   finfo.sec_ptrs = NULL;
2348   finfo.sym_indices = NULL;
2349   finfo.outsyms = NULL;
2350   finfo.linenos = NULL;
2351   finfo.contents = NULL;
2352   finfo.external_relocs = NULL;
2353   finfo.internal_relocs = NULL;
2354   debug_merge_allocated = false;
2355
2356   coff_data (abfd)->link_info = info;
2357
2358   finfo.strtab = _bfd_stringtab_init ();
2359   if (finfo.strtab == NULL)
2360     goto error_return;
2361
2362   if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
2363     goto error_return;
2364   debug_merge_allocated = true;
2365
2366   /* Compute the file positions for all the sections.  */
2367   if (! abfd->output_has_begun)
2368     {
2369       if (! bfd_coff_compute_section_file_positions (abfd))
2370         return false;
2371     }
2372
2373   /* Count the line numbers and relocation entries required for the
2374      output file.  Set the file positions for the relocs.  */
2375   rel_filepos = obj_relocbase (abfd);
2376   relsz = bfd_coff_relsz (abfd);
2377   max_contents_size = 0;
2378   max_lineno_count = 0;
2379   max_reloc_count = 0;
2380
2381   for (o = abfd->sections; o != NULL; o = o->next)
2382     {
2383       o->reloc_count = 0;
2384       o->lineno_count = 0;
2385       for (p = o->link_order_head; p != NULL; p = p->next)
2386         {
2387
2388           if (p->type == bfd_indirect_link_order)
2389             {
2390               asection *sec;
2391
2392               sec = p->u.indirect.section;
2393
2394               /* Mark all sections which are to be included in the
2395                  link.  This will normally be every section.  We need
2396                  to do this so that we can identify any sections which
2397                  the linker has decided to not include.  */
2398               sec->linker_mark = true;
2399
2400               if (info->strip == strip_none
2401                   || info->strip == strip_some)
2402                 o->lineno_count += sec->lineno_count;
2403
2404               if (info->relocateable)
2405                 o->reloc_count += sec->reloc_count;
2406
2407               if (sec->_raw_size > max_contents_size)
2408                 max_contents_size = sec->_raw_size;
2409               if (sec->lineno_count > max_lineno_count)
2410                 max_lineno_count = sec->lineno_count;
2411               if (sec->reloc_count > max_reloc_count)
2412                 max_reloc_count = sec->reloc_count;
2413             }
2414           else if (info->relocateable
2415                    && (p->type == bfd_section_reloc_link_order
2416                        || p->type == bfd_symbol_reloc_link_order))
2417             ++o->reloc_count;
2418         }
2419       if (o->reloc_count == 0)
2420         o->rel_filepos = 0;
2421       else
2422         {
2423           o->flags |= SEC_RELOC;
2424           o->rel_filepos = rel_filepos;
2425           rel_filepos += o->reloc_count * relsz;
2426         }
2427     }
2428
2429   /* If doing a relocateable link, allocate space for the pointers we
2430      need to keep.  */
2431   if (info->relocateable)
2432     {
2433       unsigned int i;
2434
2435       /* We use section_count + 1, rather than section_count, because
2436          the target_index fields are 1 based.  */
2437       amt = abfd->section_count + 1;
2438       amt *= sizeof (struct coff_link_section_info);
2439       finfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
2440       if (finfo.section_info == NULL)
2441         goto error_return;
2442       for (i = 0; i <= abfd->section_count; i++)
2443         {
2444           finfo.section_info[i].relocs = NULL;
2445           finfo.section_info[i].rel_hashes = NULL;
2446         }
2447     }
2448
2449   /* We now know the size of the relocs, so we can determine the file
2450      positions of the line numbers.  */
2451   line_filepos = rel_filepos;
2452   linesz = bfd_coff_linesz (abfd);
2453   max_output_reloc_count = 0;
2454   for (o = abfd->sections; o != NULL; o = o->next)
2455     {
2456       if (o->lineno_count == 0)
2457         o->line_filepos = 0;
2458       else
2459         {
2460           o->line_filepos = line_filepos;
2461           line_filepos += o->lineno_count * linesz;
2462         }
2463
2464       if (o->reloc_count != 0)
2465         {
2466           /* We don't know the indices of global symbols until we have
2467              written out all the local symbols.  For each section in
2468              the output file, we keep an array of pointers to hash
2469              table entries.  Each entry in the array corresponds to a
2470              reloc.  When we find a reloc against a global symbol, we
2471              set the corresponding entry in this array so that we can
2472              fix up the symbol index after we have written out all the
2473              local symbols.
2474
2475              Because of this problem, we also keep the relocs in
2476              memory until the end of the link.  This wastes memory,
2477              but only when doing a relocateable link, which is not the
2478              common case.  */
2479           BFD_ASSERT (info->relocateable);
2480           amt = o->reloc_count;
2481           amt *= sizeof (struct internal_reloc);
2482           finfo.section_info[o->target_index].relocs =
2483             (struct internal_reloc *) bfd_malloc (amt);
2484           amt = o->reloc_count;
2485           amt *= sizeof (struct coff_link_hash_entry *);
2486           finfo.section_info[o->target_index].rel_hashes =
2487             (struct coff_link_hash_entry **) bfd_malloc (amt);
2488           if (finfo.section_info[o->target_index].relocs == NULL
2489               || finfo.section_info[o->target_index].rel_hashes == NULL)
2490             goto error_return;
2491
2492           if (o->reloc_count > max_output_reloc_count)
2493             max_output_reloc_count = o->reloc_count;
2494         }
2495
2496       /* Reset the reloc and lineno counts, so that we can use them to
2497          count the number of entries we have output so far.  */
2498       o->reloc_count = 0;
2499       o->lineno_count = 0;
2500     }
2501
2502   obj_sym_filepos (abfd) = line_filepos;
2503
2504   /* Figure out the largest number of symbols in an input BFD.  Take
2505      the opportunity to clear the output_has_begun fields of all the
2506      input BFD's.  */
2507   max_sym_count = 0;
2508   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2509     {
2510       bfd_size_type sz;
2511
2512       sub->output_has_begun = false;
2513       sz = obj_raw_syment_count (sub);
2514       if (sz > max_sym_count)
2515         max_sym_count = sz;
2516     }
2517
2518   /* Allocate some buffers used while linking.  */
2519   amt = max_sym_count * sizeof (struct internal_syment);
2520   finfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
2521   amt = max_sym_count * sizeof (asection *);
2522   finfo.sec_ptrs = (asection **) bfd_malloc (amt);
2523   amt = max_sym_count * sizeof (long);
2524   finfo.sym_indices = (long *) bfd_malloc (amt);
2525   amt = (max_sym_count + 1) * symesz;
2526   finfo.outsyms = (bfd_byte *) bfd_malloc (amt);
2527   amt = max_lineno_count * bfd_coff_linesz (abfd);
2528   finfo.linenos = (bfd_byte *) bfd_malloc (amt);
2529   finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
2530   finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
2531   if (! info->relocateable)
2532     {
2533       amt = max_reloc_count * sizeof (struct internal_reloc);
2534       finfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
2535     }
2536   if ((finfo.internal_syms == NULL && max_sym_count > 0)
2537       || (finfo.sec_ptrs == NULL && max_sym_count > 0)
2538       || (finfo.sym_indices == NULL && max_sym_count > 0)
2539       || finfo.outsyms == NULL
2540       || (finfo.linenos == NULL && max_lineno_count > 0)
2541       || (finfo.contents == NULL && max_contents_size > 0)
2542       || (finfo.external_relocs == NULL && max_reloc_count > 0)
2543       || (! info->relocateable
2544           && finfo.internal_relocs == NULL
2545           && max_reloc_count > 0))
2546     goto error_return;
2547
2548   /* We now know the position of everything in the file, except that
2549      we don't know the size of the symbol table and therefore we don't
2550      know where the string table starts.  We just build the string
2551      table in memory as we go along.  We process all the relocations
2552      for a single input file at once.  */
2553   obj_raw_syment_count (abfd) = 0;
2554
2555   if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
2556     {
2557       if (! bfd_coff_start_final_link (abfd, info))
2558         goto error_return;
2559     }
2560
2561   for (o = abfd->sections; o != NULL; o = o->next)
2562     {
2563       for (p = o->link_order_head; p != NULL; p = p->next)
2564         {
2565           if (p->type == bfd_indirect_link_order
2566               && (bfd_get_flavour (p->u.indirect.section->owner)
2567                   == bfd_target_coff_flavour))
2568             {
2569               sub = p->u.indirect.section->owner;
2570 #ifdef POWERPC_LE_PE
2571               if (! sub->output_has_begun && !ppc_do_last(sub))
2572 #else
2573               if (! sub->output_has_begun)
2574 #endif
2575                 {
2576                   if (! _bfd_coff_link_input_bfd (&finfo, sub))
2577                     goto error_return;
2578                   sub->output_has_begun = true;
2579                 }
2580             }
2581           else if (p->type == bfd_section_reloc_link_order
2582                    || p->type == bfd_symbol_reloc_link_order)
2583             {
2584               if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
2585                 goto error_return;
2586             }
2587           else
2588             {
2589               if (! _bfd_default_link_order (abfd, info, o, p))
2590                 goto error_return;
2591             }
2592         }
2593     }
2594
2595 #ifdef POWERPC_LE_PE
2596   {
2597     bfd* last_one = ppc_get_last();
2598     if (last_one)
2599       {
2600         if (! _bfd_coff_link_input_bfd (&finfo, last_one))
2601           goto error_return;
2602       }
2603     last_one->output_has_begun = true;
2604   }
2605 #endif
2606
2607   /* Free up the buffers used by _bfd_coff_link_input_bfd.  */
2608
2609   coff_debug_merge_hash_table_free (&finfo.debug_merge);
2610   debug_merge_allocated = false;
2611
2612   if (finfo.internal_syms != NULL)
2613     {
2614       free (finfo.internal_syms);
2615       finfo.internal_syms = NULL;
2616     }
2617   if (finfo.sec_ptrs != NULL)
2618     {
2619       free (finfo.sec_ptrs);
2620       finfo.sec_ptrs = NULL;
2621     }
2622   if (finfo.sym_indices != NULL)
2623     {
2624       free (finfo.sym_indices);
2625       finfo.sym_indices = NULL;
2626     }
2627   if (finfo.linenos != NULL)
2628     {
2629       free (finfo.linenos);
2630       finfo.linenos = NULL;
2631     }
2632   if (finfo.contents != NULL)
2633     {
2634       free (finfo.contents);
2635       finfo.contents = NULL;
2636     }
2637   if (finfo.external_relocs != NULL)
2638     {
2639       free (finfo.external_relocs);
2640       finfo.external_relocs = NULL;
2641     }
2642   if (finfo.internal_relocs != NULL)
2643     {
2644       free (finfo.internal_relocs);
2645       finfo.internal_relocs = NULL;
2646     }
2647
2648   /* The value of the last C_FILE symbol is supposed to be the symbol
2649      index of the first external symbol.  Write it out again if
2650      necessary.  */
2651   if (finfo.last_file_index != -1
2652       && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
2653     {
2654       file_ptr pos;
2655
2656       finfo.last_file.n_value = obj_raw_syment_count (abfd);
2657       bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
2658                              (PTR) finfo.outsyms);
2659       pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
2660       if (bfd_seek (abfd, pos, SEEK_SET) != 0
2661           || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
2662         return false;
2663     }
2664
2665   /* Write out the global symbols.  */
2666   finfo.failed = false;
2667   coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_global_sym,
2668                            (PTR) &finfo);
2669   if (finfo.failed)
2670     goto error_return;
2671
2672   /* The outsyms buffer is used by _bfd_coff_write_global_sym.  */
2673   if (finfo.outsyms != NULL)
2674     {
2675       free (finfo.outsyms);
2676       finfo.outsyms = NULL;
2677     }
2678
2679   if (info->relocateable)
2680     {
2681       /* Now that we have written out all the global symbols, we know
2682          the symbol indices to use for relocs against them, and we can
2683          finally write out the relocs.  */
2684       amt = max_output_reloc_count * relsz;
2685       external_relocs = (bfd_byte *) bfd_malloc (amt);
2686       if (external_relocs == NULL)
2687         goto error_return;
2688
2689       for (o = abfd->sections; o != NULL; o = o->next)
2690         {
2691           struct internal_reloc *irel;
2692           struct internal_reloc *irelend;
2693           struct coff_link_hash_entry **rel_hash;
2694           bfd_byte *erel;
2695
2696           if (o->reloc_count == 0)
2697             continue;
2698
2699           irel = finfo.section_info[o->target_index].relocs;
2700           irelend = irel + o->reloc_count;
2701           rel_hash = finfo.section_info[o->target_index].rel_hashes;
2702           erel = external_relocs;
2703           for (; irel < irelend; irel++, rel_hash++, erel += relsz)
2704             {
2705               if (*rel_hash != NULL)
2706                 {
2707                   BFD_ASSERT ((*rel_hash)->indx >= 0);
2708                   irel->r_symndx = (*rel_hash)->indx;
2709                 }
2710               bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
2711             }
2712
2713           amt = relsz * o->reloc_count;
2714           if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
2715               || bfd_bwrite ((PTR) external_relocs, amt, abfd) != amt)
2716             goto error_return;
2717         }
2718
2719       free (external_relocs);
2720       external_relocs = NULL;
2721     }
2722
2723   /* Free up the section information.  */
2724   if (finfo.section_info != NULL)
2725     {
2726       unsigned int i;
2727
2728       for (i = 0; i < abfd->section_count; i++)
2729         {
2730           if (finfo.section_info[i].relocs != NULL)
2731             free (finfo.section_info[i].relocs);
2732           if (finfo.section_info[i].rel_hashes != NULL)
2733             free (finfo.section_info[i].rel_hashes);
2734         }
2735       free (finfo.section_info);
2736       finfo.section_info = NULL;
2737     }
2738
2739   /* If we have optimized stabs strings, output them.  */
2740   if (coff_hash_table (info)->stab_info != NULL)
2741     {
2742       if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
2743         return false;
2744     }
2745
2746   /* Write out the string table.  */
2747   if (obj_raw_syment_count (abfd) != 0)
2748     {
2749       file_ptr pos;
2750
2751       pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
2752       if (bfd_seek (abfd, pos, SEEK_SET) != 0)
2753         return false;
2754
2755 #if STRING_SIZE_SIZE == 4
2756       H_PUT_32 (abfd,
2757                 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
2758                 strbuf);
2759 #else
2760  #error Change H_PUT_32 above
2761 #endif
2762
2763       if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
2764           != STRING_SIZE_SIZE)
2765         return false;
2766
2767       if (! _bfd_stringtab_emit (abfd, finfo.strtab))
2768         return false;
2769     }
2770
2771   _bfd_stringtab_free (finfo.strtab);
2772
2773   /* Setting bfd_get_symcount to 0 will cause write_object_contents to
2774      not try to write out the symbols.  */
2775   bfd_get_symcount (abfd) = 0;
2776
2777   return true;
2778
2779  error_return:
2780   if (debug_merge_allocated)
2781     coff_debug_merge_hash_table_free (&finfo.debug_merge);
2782   if (finfo.strtab != NULL)
2783     _bfd_stringtab_free (finfo.strtab);
2784   if (finfo.section_info != NULL)
2785     {
2786       unsigned int i;
2787
2788       for (i = 0; i < abfd->section_count; i++)
2789         {
2790           if (finfo.section_info[i].relocs != NULL)
2791             free (finfo.section_info[i].relocs);
2792           if (finfo.section_info[i].rel_hashes != NULL)
2793             free (finfo.section_info[i].rel_hashes);
2794         }
2795       free (finfo.section_info);
2796     }
2797   if (finfo.internal_syms != NULL)
2798     free (finfo.internal_syms);
2799   if (finfo.sec_ptrs != NULL)
2800     free (finfo.sec_ptrs);
2801   if (finfo.sym_indices != NULL)
2802     free (finfo.sym_indices);
2803   if (finfo.outsyms != NULL)
2804     free (finfo.outsyms);
2805   if (finfo.linenos != NULL)
2806     free (finfo.linenos);
2807   if (finfo.contents != NULL)
2808     free (finfo.contents);
2809   if (finfo.external_relocs != NULL)
2810     free (finfo.external_relocs);
2811   if (finfo.internal_relocs != NULL)
2812     free (finfo.internal_relocs);
2813   if (external_relocs != NULL)
2814     free (external_relocs);
2815   return false;
2816 }
2817 #endif
2818 \f
2819 /* Forward declaration for use by alternative_target field.  */
2820 #ifdef TARGET_BIG_SYM
2821 extern const bfd_target TARGET_BIG_SYM;
2822 #endif
2823
2824 /* The transfer vectors that lead the outside world to all of the above.  */
2825
2826 #ifdef TARGET_LITTLE_SYM
2827 const bfd_target TARGET_LITTLE_SYM =
2828 {
2829   TARGET_LITTLE_NAME,           /* name or coff-arm-little */
2830   bfd_target_coff_flavour,
2831   BFD_ENDIAN_LITTLE,            /* data byte order is little */
2832   BFD_ENDIAN_LITTLE,            /* header byte order is little */
2833
2834   (HAS_RELOC | EXEC_P |         /* FIXME: object flags */
2835    HAS_LINENO | HAS_DEBUG |
2836    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2837
2838 #ifndef COFF_WITH_PE
2839   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2840 #else
2841   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
2842    | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2843 #endif
2844
2845   0,                            /* leading char */
2846   '/',                          /* ar_pad_char */
2847   15,                           /* ar_max_namelen??? FIXMEmgo */
2848
2849   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2850   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2851   bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2852
2853   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2854   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2855   bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2856
2857   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
2858      bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2859   {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2860      bfd_false},
2861   {bfd_false, coff_write_object_contents,       /* bfd_write_contents */
2862      _bfd_write_archive_contents, bfd_false},
2863
2864   BFD_JUMP_TABLE_GENERIC (coff),
2865   BFD_JUMP_TABLE_COPY (coff),
2866   BFD_JUMP_TABLE_CORE (_bfd_nocore),
2867   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2868   BFD_JUMP_TABLE_SYMBOLS (coff),
2869   BFD_JUMP_TABLE_RELOCS (coff),
2870   BFD_JUMP_TABLE_WRITE (coff),
2871   BFD_JUMP_TABLE_LINK (coff),
2872   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2873
2874   /* Alternative_target.  */
2875 #ifdef TARGET_BIG_SYM
2876   & TARGET_BIG_SYM,
2877 #else
2878   NULL,
2879 #endif
2880
2881   COFF_SWAP_TABLE
2882 };
2883 #endif
2884
2885 #ifdef TARGET_BIG_SYM
2886 const bfd_target TARGET_BIG_SYM =
2887 {
2888   TARGET_BIG_NAME,
2889   bfd_target_coff_flavour,
2890   BFD_ENDIAN_BIG,               /* data byte order is big */
2891   BFD_ENDIAN_BIG,               /* header byte order is big */
2892
2893   (HAS_RELOC | EXEC_P |         /* FIXME: object flags */
2894    HAS_LINENO | HAS_DEBUG |
2895    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2896
2897 #ifndef COFF_WITH_PE
2898   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2899 #else
2900   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
2901    | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2902 #endif
2903
2904   0,                            /* leading char */
2905   '/',                          /* ar_pad_char */
2906   15,                           /* ar_max_namelen??? FIXMEmgo */
2907
2908   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2909   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2910   bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
2911
2912   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2913   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2914   bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
2915
2916   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
2917      bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2918   {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2919      bfd_false},
2920   {bfd_false, coff_write_object_contents,       /* bfd_write_contents */
2921      _bfd_write_archive_contents, bfd_false},
2922
2923   BFD_JUMP_TABLE_GENERIC (coff),
2924   BFD_JUMP_TABLE_COPY (coff),
2925   BFD_JUMP_TABLE_CORE (_bfd_nocore),
2926   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2927   BFD_JUMP_TABLE_SYMBOLS (coff),
2928   BFD_JUMP_TABLE_RELOCS (coff),
2929   BFD_JUMP_TABLE_WRITE (coff),
2930   BFD_JUMP_TABLE_LINK (coff),
2931   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2932
2933   /* Alternative_target.  */
2934 #ifdef TARGET_LITTLE_SYM
2935   & TARGET_LITTLE_SYM,
2936 #else
2937   NULL,
2938 #endif
2939
2940   COFF_SWAP_TABLE
2941 };
2942
2943 #endif