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