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