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