source: trunk/src/binutils/bfd/coff-ppc.c@ 610

Last change on this file since 610 was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

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