OSDN Git Service

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