OSDN Git Service

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