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