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