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