OSDN Git Service

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