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