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