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