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