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

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

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 85.4 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
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
12This file is part of BFD, the Binary File Descriptor library.
13
14This program is free software; you can redistribute it and/or modify
15it under the terms of the GNU General Public License as published by
16the Free Software Foundation; either version 2 of the License, or
17(at your option) any later version.
18
19This program is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22GNU General Public License for more details.
23
24You should have received a copy of the GNU General Public License
25along with this program; if not, write to the Free Software
26Foundation, 59 Temple Place - Suite 330,
27Boston, 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
37#include "bfd.h"
38#include "sysdep.h"
39
40#include "libbfd.h"
41
42#include "coff/powerpc.h"
43#include "coff/internal.h"
44
45#include "coff/pe.h"
46
47#ifdef BADMAG
48#undef BADMAG
49#endif
50
51#define BADMAG(x) PPCBADMAG(x)
52
53#include "libcoff.h"
54
55/* This file is compiled more than once, but we only compile the
56 final_link routine once. */
57extern boolean ppc_bfd_coff_final_link
58 PARAMS ((bfd *, struct bfd_link_info *));
59extern void dump_toc PARAMS ((PTR));
60
61/* The toc is a set of bfd_vma fields. We use the fact that valid */
62/* addresses are even (i.e. the bit representing "1" is off) to allow */
63/* us to encode a little extra information in the field */
64/* - Unallocated addresses are intialized to 1. */
65/* - Allocated addresses are even numbers. */
66/* The first time we actually write a reference to the toc in the bfd, */
67/* we want to record that fact in a fixup file (if it is asked for), so */
68/* we keep track of whether or not an address has been written by marking */
69/* the low order bit with a "1" upon writing */
70
71#define SET_UNALLOCATED(x) ((x) = 1)
72#define IS_UNALLOCATED(x) ((x) == 1)
73
74#define IS_WRITTEN(x) ((x) & 1)
75#define MARK_AS_WRITTEN(x) ((x) |= 1)
76#define MAKE_ADDR_AGAIN(x) ((x) &= ~1)
77
78/* Turn on this check if you suspect something amiss in the hash tables */
79#ifdef DEBUG_HASH
80
81/* Need a 7 char string for an eye catcher */
82#define EYE "krkjunk"
83
84#define HASH_CHECK_DCL char eye_catcher[8];
85#define HASH_CHECK_INIT(ret) strcpy(ret->eye_catcher, EYE)
86#define HASH_CHECK(addr) \
87 if (strcmp(addr->eye_catcher, EYE) != 0) \
88 { \
89 fprintf (stderr,\
90 _("File %s, line %d, Hash check failure, bad eye %8s\n"), \
91 __FILE__, __LINE__, addr->eye_catcher); \
92 abort (); \
93 }
94
95#else
96
97#define HASH_CHECK_DCL
98#define HASH_CHECK_INIT(ret)
99#define HASH_CHECK(addr)
100
101#endif
102
103/* In order not to add an int to every hash table item for every coff
104 linker, we define our own hash table, derived from the coff one */
105
106/* PE linker hash table entries. */
107
108struct ppc_coff_link_hash_entry
109{
110 struct coff_link_hash_entry root; /* First entry, as required */
111
112 /* As we wonder around the relocs, we'll keep the assigned toc_offset
113 here */
114 bfd_vma toc_offset; /* Our addition, as required */
115 int symbol_is_glue;
116 unsigned long int glue_insn;
117
118 HASH_CHECK_DCL
119};
120
121/* PE linker hash table. */
122
123struct ppc_coff_link_hash_table
124{
125 struct coff_link_hash_table root; /* First entry, as required */
126};
127
128static struct bfd_hash_entry *ppc_coff_link_hash_newfunc
129 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
130 const char *));
131static boolean ppc_coff_link_hash_table_init
132 PARAMS ((struct ppc_coff_link_hash_table *, bfd *,
133 struct bfd_hash_entry *(*) (struct bfd_hash_entry *,
134 struct bfd_hash_table *,
135 const char *)));
136static struct bfd_link_hash_table *ppc_coff_link_hash_table_create
137 PARAMS ((bfd *));
138static boolean coff_ppc_relocate_section
139 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
140 struct internal_reloc *, struct internal_syment *, asection **));
141static reloc_howto_type *coff_ppc_rtype_to_howto
142 PARAMS ((bfd *, asection *, struct internal_reloc *,
143 struct coff_link_hash_entry *, struct internal_syment *,
144 bfd_vma *));
145
146/* Routine to create an entry in the link hash table. */
147
148static struct bfd_hash_entry *
149ppc_coff_link_hash_newfunc (entry, table, string)
150 struct bfd_hash_entry *entry;
151 struct bfd_hash_table *table;
152 const char *string;
153{
154 struct ppc_coff_link_hash_entry *ret =
155 (struct ppc_coff_link_hash_entry *) entry;
156
157 /* Allocate the structure if it has not already been allocated by a
158 subclass. */
159 if (ret == (struct ppc_coff_link_hash_entry *) NULL)
160 ret = (struct ppc_coff_link_hash_entry *)
161 bfd_hash_allocate (table,
162 sizeof (struct ppc_coff_link_hash_entry));
163
164 if (ret == (struct ppc_coff_link_hash_entry *) NULL)
165 return NULL;
166
167 /* Call the allocation method of the superclass. */
168 ret = ((struct ppc_coff_link_hash_entry *)
169 _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry *) ret,
170 table, string));
171
172 if (ret)
173 {
174 /* Initialize the local fields. */
175 SET_UNALLOCATED(ret->toc_offset);
176 ret->symbol_is_glue = 0;
177 ret->glue_insn = 0;
178
179 HASH_CHECK_INIT(ret);
180 }
181
182 return (struct bfd_hash_entry *) ret;
183}
184
185/* Initialize a PE linker hash table. */
186
187static boolean
188ppc_coff_link_hash_table_init (table, abfd, newfunc)
189 struct ppc_coff_link_hash_table *table;
190 bfd *abfd;
191 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
192 struct bfd_hash_table *,
193 const char *));
194{
195 return _bfd_coff_link_hash_table_init (&table->root, abfd, newfunc);
196}
197
198/* Create a PE linker hash table. */
199
200static struct bfd_link_hash_table *
201ppc_coff_link_hash_table_create (abfd)
202 bfd *abfd;
203{
204 struct ppc_coff_link_hash_table *ret;
205
206 ret = ((struct ppc_coff_link_hash_table *)
207 bfd_alloc (abfd, sizeof (struct ppc_coff_link_hash_table)));
208 if (ret == NULL)
209 return NULL;
210 if (! ppc_coff_link_hash_table_init (ret, abfd,
211 ppc_coff_link_hash_newfunc))
212 {
213 bfd_release (abfd, ret);
214 return (struct bfd_link_hash_table *) NULL;
215 }
216 return &ret->root.root;
217}
218
219/* Now, tailor coffcode.h to use our hash stuff */
220
221#define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
222
223
224/* The nt loader points the toc register to &toc + 32768, in order to */
225/* use the complete range of a 16-bit displacement. We have to adjust */
226/* for this when we fix up loads displaced off the toc reg. */
227#define TOC_LOAD_ADJUSTMENT (-32768)
228#define TOC_SECTION_NAME ".private.toc"
229
230/* The main body of code is in coffcode.h. */
231
232#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
233
234/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
235 from smaller values. Start with zero, widen, *then* decrement. */
236#define MINUS_ONE (((bfd_vma)0) - 1)
237
238/* these should definitely go in a header file somewhere... */
239
240/* NOP */
241#define IMAGE_REL_PPC_ABSOLUTE 0x0000
242
243/* 64-bit address */
244#define IMAGE_REL_PPC_ADDR64 0x0001
245
246/* 32-bit address */
247#define IMAGE_REL_PPC_ADDR32 0x0002
248
249/* 26-bit address, shifted left 2 (branch absolute) */
250#define IMAGE_REL_PPC_ADDR24 0x0003
251
252/* 16-bit address */
253#define IMAGE_REL_PPC_ADDR16 0x0004
254
255/* 16-bit address, shifted left 2 (load doubleword) */
256#define IMAGE_REL_PPC_ADDR14 0x0005
257
258/* 26-bit PC-relative offset, shifted left 2 (branch relative) */
259#define IMAGE_REL_PPC_REL24 0x0006
260
261/* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
262#define IMAGE_REL_PPC_REL14 0x0007
263
264/* 16-bit offset from TOC base */
265#define IMAGE_REL_PPC_TOCREL16 0x0008
266
267/* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
268#define IMAGE_REL_PPC_TOCREL14 0x0009
269
270/* 32-bit addr w/o image base */
271#define IMAGE_REL_PPC_ADDR32NB 0x000A
272
273/* va of containing section (as in an image sectionhdr) */
274#define IMAGE_REL_PPC_SECREL 0x000B
275
276/* sectionheader number */
277#define IMAGE_REL_PPC_SECTION 0x000C
278
279/* substitute TOC restore instruction iff symbol is glue code */
280#define IMAGE_REL_PPC_IFGLUE 0x000D
281
282/* symbol is glue code; virtual address is TOC restore instruction */
283#define IMAGE_REL_PPC_IMGLUE 0x000E
284
285/* va of containing section (limited to 16 bits) */
286#define IMAGE_REL_PPC_SECREL16 0x000F
287
288/* stuff to handle immediate data when the number of bits in the */
289/* data is greater than the number of bits in the immediate field */
290/* We need to do (usually) 32 bit arithmetic on 16 bit chunks */
291#define IMAGE_REL_PPC_REFHI 0x0010
292#define IMAGE_REL_PPC_REFLO 0x0011
293#define IMAGE_REL_PPC_PAIR 0x0012
294
295/* This is essentially the same as tocrel16, with TOCDEFN assumed */
296#define IMAGE_REL_PPC_TOCREL16_DEFN 0x0013
297
298/* Flag bits in IMAGE_RELOCATION.TYPE */
299
300/* subtract reloc value rather than adding it */
301#define IMAGE_REL_PPC_NEG 0x0100
302
303/* fix branch prediction bit to predict branch taken */
304#define IMAGE_REL_PPC_BRTAKEN 0x0200
305
306/* fix branch prediction bit to predict branch not taken */
307#define IMAGE_REL_PPC_BRNTAKEN 0x0400
308
309/* toc slot defined in file (or, data in toc) */
310#define IMAGE_REL_PPC_TOCDEFN 0x0800
311
312/* masks to isolate above values in IMAGE_RELOCATION.Type */
313#define IMAGE_REL_PPC_TYPEMASK 0x00FF
314#define IMAGE_REL_PPC_FLAGMASK 0x0F00
315
316#define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
317#define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
318#define EXTRACT_JUNK(x) \
319 ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
320
321
322/* static helper functions to make relocation work */
323/* (Work In Progress) */
324
325static bfd_reloc_status_type ppc_refhi_reloc PARAMS ((bfd *abfd,
326 arelent *reloc,
327 asymbol *symbol,
328 PTR data,
329 asection *section,
330 bfd *output_bfd,
331 char **error));
332#if 0
333static bfd_reloc_status_type ppc_reflo_reloc PARAMS ((bfd *abfd,
334 arelent *reloc,
335 asymbol *symbol,
336 PTR data,
337 asection *section,
338 bfd *output_bfd,
339 char **error));
340#endif
341static bfd_reloc_status_type ppc_pair_reloc PARAMS ((bfd *abfd,
342 arelent *reloc,
343 asymbol *symbol,
344 PTR data,
345 asection *section,
346 bfd *output_bfd,
347 char **error));
348
349
350static bfd_reloc_status_type ppc_toc16_reloc PARAMS ((bfd *abfd,
351 arelent *reloc,
352 asymbol *symbol,
353 PTR data,
354 asection *section,
355 bfd *output_bfd,
356 char **error));
357
358#if 0
359static bfd_reloc_status_type ppc_addr32nb_reloc PARAMS ((bfd *abfd,
360 arelent *reloc,
361 asymbol *symbol,
362 PTR data,
363 asection *section,
364 bfd *output_bfd,
365 char **error));
366#endif
367static bfd_reloc_status_type ppc_section_reloc PARAMS ((bfd *abfd,
368 arelent *reloc,
369 asymbol *symbol,
370 PTR data,
371 asection *section,
372 bfd *output_bfd,
373 char **error));
374
375static bfd_reloc_status_type ppc_secrel_reloc PARAMS ((bfd *abfd,
376 arelent *reloc,
377 asymbol *symbol,
378 PTR data,
379 asection *section,
380 bfd *output_bfd,
381 char **error));
382
383static bfd_reloc_status_type ppc_imglue_reloc PARAMS ((bfd *abfd,
384 arelent *reloc,
385 asymbol *symbol,
386 PTR data,
387 asection *section,
388 bfd *output_bfd,
389 char **error));
390
391static boolean in_reloc_p PARAMS((bfd *abfd, reloc_howto_type *howto));
392
393
394/* FIXME: It'll take a while to get through all of these. I only need a few to
395 get us started, so those I'll make sure work. Those marked FIXME are either
396 completely unverified or have a specific unknown marked in the comment */
397
398/*---------------------------------------------------------------------------*/
399/* */
400/* Relocation entries for Windows/NT on PowerPC. */
401/* */
402/* From the document "" we find the following listed as used relocs: */
403/* */
404/* ABSOLUTE : The noop */
405/* ADDR[64|32|16] : fields that hold addresses in data fields or the */
406/* 16 bit displacement field on a load/store. */
407/* ADDR[24|14] : fields that hold addresses in branch and cond */
408/* branches. These represent [26|16] bit addresses. */
409/* The low order 2 bits are preserved. */
410/* REL[24|14] : branches relative to the Instruction Address */
411/* register. These represent [26|16] bit addresses, */
412/* as before. The instruction field will be zero, and */
413/* the address of the SYM will be inserted at link time. */
414/* TOCREL16 : 16 bit displacement field referring to a slot in */
415/* toc. */
416/* TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14. */
417/* ADDR32NB : 32 bit address relative to the virtual origin. */
418/* (On the alpha, this is always a linker generated thunk)*/
419/* (i.e. 32bit addr relative to the image base) */
420/* SECREL : The value is relative to the start of the section */
421/* containing the symbol. */
422/* SECTION : access to the header containing the item. Supports the */
423/* codeview debugger. */
424/* */
425/* In particular, note that the document does not indicate that the */
426/* relocations listed in the header file are used. */
427/* */
428/* */
429/* */
430/*---------------------------------------------------------------------------*/
431
432static reloc_howto_type ppc_coff_howto_table[] =
433{
434 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
435 /* Unused: */
436 HOWTO (IMAGE_REL_PPC_ABSOLUTE, /* type */
437 0, /* rightshift */
438 0, /* size (0 = byte, 1 = short, 2 = long) */
439 0, /* bitsize */
440 false, /* pc_relative */
441 0, /* bitpos */
442 complain_overflow_dont, /* dont complain_on_overflow */
443 0, /* special_function */
444 "ABSOLUTE", /* name */
445 false, /* partial_inplace */
446 0x00, /* src_mask */
447 0x00, /* dst_mask */
448 false), /* pcrel_offset */
449
450 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
451 /* Unused: */
452 HOWTO(IMAGE_REL_PPC_ADDR64, /* type */
453 0, /* rightshift */
454 3, /* size (0 = byte, 1 = short, 2 = long) */
455 64, /* bitsize */
456 false, /* pc_relative */
457 0, /* bitpos */
458 complain_overflow_bitfield, /* complain_on_overflow */
459 0, /* special_function */
460 "ADDR64", /* name */
461 true, /* partial_inplace */
462 MINUS_ONE, /* src_mask */
463 MINUS_ONE, /* dst_mask */
464 false), /* pcrel_offset */
465
466 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
467 /* Used: */
468 HOWTO (IMAGE_REL_PPC_ADDR32, /* type */
469 0, /* rightshift */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
471 32, /* bitsize */
472 false, /* pc_relative */
473 0, /* bitpos */
474 complain_overflow_bitfield, /* complain_on_overflow */
475 0, /* special_function */
476 "ADDR32", /* name */
477 true, /* partial_inplace */
478 0xffffffff, /* src_mask */
479 0xffffffff, /* dst_mask */
480 false), /* pcrel_offset */
481
482 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
483 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
484 /* Of course, That's the IBM approved bit numbering, which is not what */
485 /* anyone else uses.... The li field is in bit 2 thru 25 */
486 /* Used: */
487 HOWTO (IMAGE_REL_PPC_ADDR24, /* type */
488 0, /* rightshift */
489 2, /* size (0 = byte, 1 = short, 2 = long) */
490 26, /* bitsize */
491 false, /* pc_relative */
492 0, /* bitpos */
493 complain_overflow_bitfield, /* complain_on_overflow */
494 0, /* special_function */
495 "ADDR24", /* name */
496 true, /* partial_inplace */
497 0x07fffffc, /* src_mask */
498 0x07fffffc, /* dst_mask */
499 false), /* pcrel_offset */
500
501 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
502 /* Used: */
503 HOWTO (IMAGE_REL_PPC_ADDR16, /* type */
504 0, /* rightshift */
505 1, /* size (0 = byte, 1 = short, 2 = long) */
506 16, /* bitsize */
507 false, /* pc_relative */
508 0, /* bitpos */
509 complain_overflow_signed, /* complain_on_overflow */
510 0, /* special_function */
511 "ADDR16", /* name */
512 true, /* partial_inplace */
513 0xffff, /* src_mask */
514 0xffff, /* dst_mask */
515 false), /* pcrel_offset */
516
517 /* IMAGE_REL_PPC_ADDR14 0x0005 */
518 /* 16-bit address, shifted left 2 (load doubleword) */
519 /* FIXME: the mask is likely wrong, and the bit position may be as well */
520 /* Unused: */
521 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
522 1, /* rightshift */
523 1, /* size (0 = byte, 1 = short, 2 = long) */
524 16, /* bitsize */
525 false, /* pc_relative */
526 0, /* bitpos */
527 complain_overflow_signed, /* complain_on_overflow */
528 0, /* special_function */
529 "ADDR16", /* name */
530 true, /* partial_inplace */
531 0xffff, /* src_mask */
532 0xffff, /* dst_mask */
533 false), /* pcrel_offset */
534
535 /* IMAGE_REL_PPC_REL24 0x0006 */
536 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
537 /* Used: */
538 HOWTO (IMAGE_REL_PPC_REL24, /* type */
539 0, /* rightshift */
540 2, /* size (0 = byte, 1 = short, 2 = long) */
541 26, /* bitsize */
542 true, /* pc_relative */
543 0, /* bitpos */
544 complain_overflow_signed, /* complain_on_overflow */
545 0, /* special_function */
546 "REL24", /* name */
547 true, /* partial_inplace */
548 0x3fffffc, /* src_mask */
549 0x3fffffc, /* dst_mask */
550 false), /* pcrel_offset */
551
552 /* IMAGE_REL_PPC_REL14 0x0007 */
553 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
554 /* FIXME: the mask is likely wrong, and the bit position may be as well */
555 /* FIXME: how does it know how far to shift? */
556 /* Unused: */
557 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
558 1, /* rightshift */
559 1, /* size (0 = byte, 1 = short, 2 = long) */
560 16, /* bitsize */
561 false, /* pc_relative */
562 0, /* bitpos */
563 complain_overflow_signed, /* complain_on_overflow */
564 0, /* special_function */
565 "ADDR16", /* name */
566 true, /* partial_inplace */
567 0xffff, /* src_mask */
568 0xffff, /* dst_mask */
569 true), /* pcrel_offset */
570
571 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
572 /* 16-bit offset from TOC base */
573 /* Used: */
574 HOWTO (IMAGE_REL_PPC_TOCREL16,/* type */
575 0, /* rightshift */
576 1, /* size (0 = byte, 1 = short, 2 = long) */
577 16, /* bitsize */
578 false, /* pc_relative */
579 0, /* bitpos */
580 complain_overflow_dont, /* complain_on_overflow */
581 ppc_toc16_reloc, /* special_function */
582 "TOCREL16", /* name */
583 false, /* partial_inplace */
584 0xffff, /* src_mask */
585 0xffff, /* dst_mask */
586 false), /* pcrel_offset */
587
588 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
589 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
590 /* Unused: */
591 HOWTO (IMAGE_REL_PPC_TOCREL14,/* type */
592 1, /* rightshift */
593 1, /* size (0 = byte, 1 = short, 2 = long) */
594 16, /* bitsize */
595 false, /* pc_relative */
596 0, /* bitpos */
597 complain_overflow_signed, /* complain_on_overflow */
598 0, /* special_function */
599 "TOCREL14", /* name */
600 false, /* partial_inplace */
601 0xffff, /* src_mask */
602 0xffff, /* dst_mask */
603 false), /* pcrel_offset */
604
605 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
606 /* 32-bit addr w/ image base */
607 /* Unused: */
608 HOWTO (IMAGE_REL_PPC_ADDR32NB,/* type */
609 0, /* rightshift */
610 2, /* size (0 = byte, 1 = short, 2 = long) */
611 32, /* bitsize */
612 false, /* pc_relative */
613 0, /* bitpos */
614 complain_overflow_signed, /* complain_on_overflow */
615 0, /* special_function */
616 "ADDR32NB", /* name */
617 true, /* partial_inplace */
618 0xffffffff, /* src_mask */
619 0xffffffff, /* dst_mask */
620 false), /* pcrel_offset */
621
622 /* IMAGE_REL_PPC_SECREL 0x000B */
623 /* va of containing section (as in an image sectionhdr) */
624 /* Unused: */
625 HOWTO (IMAGE_REL_PPC_SECREL,/* type */
626 0, /* rightshift */
627 2, /* size (0 = byte, 1 = short, 2 = long) */
628 32, /* bitsize */
629 false, /* pc_relative */
630 0, /* bitpos */
631 complain_overflow_signed, /* complain_on_overflow */
632 ppc_secrel_reloc, /* special_function */
633 "SECREL", /* name */
634 true, /* partial_inplace */
635 0xffffffff, /* src_mask */
636 0xffffffff, /* dst_mask */
637 true), /* pcrel_offset */
638
639 /* IMAGE_REL_PPC_SECTION 0x000C */
640 /* sectionheader number */
641 /* Unused: */
642 HOWTO (IMAGE_REL_PPC_SECTION,/* type */
643 0, /* rightshift */
644 2, /* size (0 = byte, 1 = short, 2 = long) */
645 32, /* bitsize */
646 false, /* pc_relative */
647 0, /* bitpos */
648 complain_overflow_signed, /* complain_on_overflow */
649 ppc_section_reloc, /* special_function */
650 "SECTION", /* name */
651 true, /* partial_inplace */
652 0xffffffff, /* src_mask */
653 0xffffffff, /* dst_mask */
654 true), /* pcrel_offset */
655
656 /* IMAGE_REL_PPC_IFGLUE 0x000D */
657 /* substitute TOC restore instruction iff symbol is glue code */
658 /* Used: */
659 HOWTO (IMAGE_REL_PPC_IFGLUE,/* type */
660 0, /* rightshift */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
662 32, /* bitsize */
663 false, /* pc_relative */
664 0, /* bitpos */
665 complain_overflow_signed, /* complain_on_overflow */
666 0, /* special_function */
667 "IFGLUE", /* name */
668 true, /* partial_inplace */
669 0xffffffff, /* src_mask */
670 0xffffffff, /* dst_mask */
671 false), /* pcrel_offset */
672
673 /* IMAGE_REL_PPC_IMGLUE 0x000E */
674 /* symbol is glue code; virtual address is TOC restore instruction */
675 /* Unused: */
676 HOWTO (IMAGE_REL_PPC_IMGLUE,/* type */
677 0, /* rightshift */
678 2, /* size (0 = byte, 1 = short, 2 = long) */
679 32, /* bitsize */
680 false, /* pc_relative */
681 0, /* bitpos */
682 complain_overflow_dont, /* complain_on_overflow */
683 ppc_imglue_reloc, /* special_function */
684 "IMGLUE", /* name */
685 false, /* partial_inplace */
686 0xffffffff, /* src_mask */
687 0xffffffff, /* dst_mask */
688 false), /* pcrel_offset */
689
690 /* IMAGE_REL_PPC_SECREL16 0x000F */
691 /* va of containing section (limited to 16 bits) */
692 /* Unused: */
693 HOWTO (IMAGE_REL_PPC_SECREL16,/* type */
694 0, /* rightshift */
695 1, /* size (0 = byte, 1 = short, 2 = long) */
696 16, /* bitsize */
697 false, /* pc_relative */
698 0, /* bitpos */
699 complain_overflow_signed, /* complain_on_overflow */
700 0, /* special_function */
701 "SECREL16", /* name */
702 true, /* partial_inplace */
703 0xffff, /* src_mask */
704 0xffff, /* dst_mask */
705 true), /* pcrel_offset */
706
707 /* IMAGE_REL_PPC_REFHI 0x0010 */
708 /* Unused: */
709 HOWTO (IMAGE_REL_PPC_REFHI, /* type */
710 0, /* rightshift */
711 1, /* size (0 = byte, 1 = short, 2 = long) */
712 16, /* bitsize */
713 false, /* pc_relative */
714 0, /* bitpos */
715 complain_overflow_signed, /* complain_on_overflow */
716 ppc_refhi_reloc, /* special_function */
717 "REFHI", /* name */
718 true, /* partial_inplace */
719 0xffffffff, /* src_mask */
720 0xffffffff, /* dst_mask */
721 false), /* pcrel_offset */
722
723 /* IMAGE_REL_PPC_REFLO 0x0011 */
724 /* Unused: */
725 HOWTO (IMAGE_REL_PPC_REFLO, /* type */
726 0, /* rightshift */
727 1, /* size (0 = byte, 1 = short, 2 = long) */
728 16, /* bitsize */
729 false, /* pc_relative */
730 0, /* bitpos */
731 complain_overflow_signed, /* complain_on_overflow */
732 ppc_refhi_reloc, /* special_function */
733 "REFLO", /* name */
734 true, /* partial_inplace */
735 0xffffffff, /* src_mask */
736 0xffffffff, /* dst_mask */
737 false), /* pcrel_offset */
738
739 /* IMAGE_REL_PPC_PAIR 0x0012 */
740 /* Unused: */
741 HOWTO (IMAGE_REL_PPC_PAIR, /* type */
742 0, /* rightshift */
743 1, /* size (0 = byte, 1 = short, 2 = long) */
744 16, /* bitsize */
745 false, /* pc_relative */
746 0, /* bitpos */
747 complain_overflow_signed, /* complain_on_overflow */
748 ppc_pair_reloc, /* special_function */
749 "PAIR", /* name */
750 true, /* partial_inplace */
751 0xffffffff, /* src_mask */
752 0xffffffff, /* dst_mask */
753 false), /* pcrel_offset */
754
755 /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
756 /* 16-bit offset from TOC base, without causing a definition */
757 /* Used: */
758 HOWTO ( (IMAGE_REL_PPC_TOCREL16 | IMAGE_REL_PPC_TOCDEFN), /* type */
759 0, /* rightshift */
760 1, /* size (0 = byte, 1 = short, 2 = long) */
761 16, /* bitsize */
762 false, /* pc_relative */
763 0, /* bitpos */
764 complain_overflow_dont, /* complain_on_overflow */
765 0, /* special_function */
766 "TOCREL16, TOCDEFN", /* name */
767 false, /* partial_inplace */
768 0xffff, /* src_mask */
769 0xffff, /* dst_mask */
770 false), /* pcrel_offset */
771
772};
773
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
795/* Given a reloc name, n, and a pointer to an internal_reloc,
796 dump out interesting information on the contents
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
818
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;
901 struct bfd_link_info *info ATTRIBUTE_UNUSED;
902 asection *sec ATTRIBUTE_UNUSED;
903 int sym;
904 enum toc_type toc_kind ATTRIBUTE_UNUSED;
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
919 if (h == 0)
920 {
921 local_syms = obj_coff_local_toc_table(abfd);
922 if (local_syms == 0)
923 {
924 unsigned int i;
925 /* allocate a table */
926 local_syms =
927 (int *) bfd_zalloc (abfd,
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
938 if (IS_UNALLOCATED(local_syms[sym]))
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
997
998/* Return true if this relocation should
999 appear in the output .reloc section. */
1000
1001static boolean in_reloc_p(abfd, howto)
1002 bfd * abfd ATTRIBUTE_UNUSED;
1003 reloc_howto_type *howto;
1004{
1005 return
1006 (! howto->pc_relative)
1007 && (howto->type != IMAGE_REL_PPC_ADDR32NB)
1008 && (howto->type != IMAGE_REL_PPC_TOCREL16)
1009 && (howto->type != IMAGE_REL_PPC_IMGLUE)
1010 && (howto->type != IMAGE_REL_PPC_IFGLUE)
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) ;
1018}
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*/
1045
1046 r_type = reloc_entry->howto->type;
1047
1048 if (output_bfd)
1049 {
1050 /* Partial linking - do nothing */
1051 reloc_entry->address += input_section->output_offset;
1052 return bfd_reloc_ok;
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 */
1059 if (r_type == IMAGE_REL_PPC_REFHI)
1060 {
1061 part1_consth_active = true;
1062 part1_consth_value = 0;
1063 }
1064 return(bfd_reloc_undefined);
1065 }
1066
1067 if ((part1_consth_active) && (r_type != IMAGE_REL_PPC_PAIR))
1068 {
1069 part1_consth_active = false;
1070 *error_message = (char *) _("Missing PAIR");
1071 return(bfd_reloc_dangerous);
1072 }
1073
1074 sym_value = get_symbol_value(symbol_in);
1075
1076 return(bfd_reloc_ok);
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;
1102
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;
1108
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);
1127
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 {
1141 h = (struct ppc_coff_link_hash_entry *)
1142 (obj_coff_sym_hashes (input_bfd)[symndx]);
1143 if (h != 0)
1144 {
1145 HASH_CHECK(h);
1146 }
1147
1148 sym = syms + symndx;
1149 }
1150
1151 if (r_type == IMAGE_REL_PPC_IMGLUE && h == 0)
1152 {
1153 /* An IMGLUE reloc must have a name. Something is very wrong. */
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,
1191 rel->r_vaddr - input_section->vma, true)))
1192 return false;
1193 }
1194 }
1195
1196 rstat = bfd_reloc_ok;
1197
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
1214 if (toc_section == 0)
1215 {
1216 toc_section = bfd_get_section_by_name (bfd_of_toc_owner,
1217 TOC_SECTION_NAME);
1218
1219 if ( toc_section == NULL )
1220 {
1221 /* There is no toc section. Something is very wrong. */
1222 abort ();
1223 }
1224 }
1225
1226 /*
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 {
1247 /* if it has been written out, it is marked with the
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 */
1256 record_toc(toc_section,
1257 our_toc_offset,
1258 priv,
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
1274 if ((r_flags & IMAGE_REL_PPC_TOCDEFN)
1275 == IMAGE_REL_PPC_TOCDEFN )
1276 {
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.
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
1288 dll linkage, takes advantage of that and considers
1289 the IAT to be part of the toc, thus saving a load.
1290 */
1291
1292 our_toc_offset = val -
1293 (toc_section->output_section->vma +
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)
1300 (_("%s: Relocation for %s of %x exceeds Toc size limit"),
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 {
1310 /* if it has been written out, it is marked with the
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
1332 if (fixit && info->base_file)
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
1338 isn't absolute - we output the address here
1339 to a file */
1340
1341 bfd_vma addr = toc_section->output_section->vma
1342 + toc_section->output_offset + our_toc_offset;
1343
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
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)
1355 (_("%s: Relocation exceeds allocated TOC (%x)"),
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,
1365 input_bfd,
1366 relocation,
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 */
1373 /* appearing on the call instruction is a glue function or not. */
1374 /* A glue function must announce itself via a IMGLUE reloc, and */
1375 /* the reloc contains the required toc restore instruction */
1376
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;
1384 if (h->symbol_is_glue == 1)
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 */
1394 /* For fast access to the header of the section
1395 containing the item. */
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
1413 fprintf (stderr,
1414 _("Warning: unsupported reloc %s <file %s, section %s>\n"),
1415 howto->name,
1416 bfd_get_filename(input_bfd),
1417 input_section->name);
1418
1419 fprintf (stderr,"sym %ld (%s), r_vaddr %ld (%lx)\n",
1420 rel->r_symndx, my_name, (long) rel->r_vaddr,
1421 (unsigned long) rel->r_vaddr);
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;
1429 if (h->symbol_is_glue == 1)
1430 break;
1431 my_name = h->root.root.root.string;
1432
1433 (*_bfd_error_handler)
1434 (_("%s: Out of order IMGLUE reloc for %s"),
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);
1454 first_thunk_address = myh->root.u.def.value +
1455 sec->output_section->vma +
1456 sec->output_offset -
1457 pe_data(output_bfd)->pe_opthdr.ImageBase;
1458
1459 idata5offset = myh->root.u.def.value;
1460 myh = coff_link_hash_lookup (coff_hash_table (info),
1461 "__idata6_magic__",
1462 false, false, true);
1463
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);
1495 if (myh == 0)
1496 {
1497 /* Missing magic cookies. Something is very wrong. */
1498 abort ();
1499 }
1500
1501 val = myh->root.u.def.value +
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);
1509 first_thunk_address = myh->root.u.def.value +
1510 sec->output_section->vma +
1511 sec->output_offset -
1512 pe_data(output_bfd)->pe_opthdr.ImageBase;
1513
1514 idata5offset = myh->root.u.def.value;
1515 myh = coff_link_hash_lookup (coff_hash_table (info),
1516 "__idata6_magic__",
1517 false, false, true);
1518
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,
1529 input_bfd,
1530 val -
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,
1542 input_bfd,
1543 val,
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,
1551 input_bfd,
1552 val,
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
1564 isn't absolute - we output the address here
1565 to a file */
1566 bfd_vma addr = rel->r_vaddr
1567 - input_section->vma
1568 + input_section->output_offset
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)
1607 (info, name, howto->name,
1608 (bfd_vma) 0, input_bfd,
1609 input_section, rel->r_vaddr - input_section->vma)))
1610 {
1611 return false;
1612 }
1613 }
1614 }
1615
1616 }
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
1692ppc_allocate_toc_section (info)
1693 struct bfd_link_info *info ATTRIBUTE_UNUSED;
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 {
1704 /* No toc owner? Something is very wrong. */
1705 abort ();
1706 }
1707
1708 s = bfd_get_section_by_name ( bfd_of_toc_owner , TOC_SECTION_NAME);
1709 if (s == NULL)
1710 {
1711 /* No toc section? Something is very wrong. */
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
1733 to do reloc rummaging, before section sizes are nailed down. */
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 {
1747 if (sec->reloc_count == 0)
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);
1753
1754 if (i == 0)
1755 abort ();
1756
1757 for (rel=i;rel<i+sec->reloc_count;++rel)
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
1765 switch(r_type)
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 )
1771 ok = ppc_record_toc_entry(abfd, info, sec,
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
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)
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;
1803 bfd *output_bfd;
1804 char **error_message ATTRIBUTE_UNUSED;
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)
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;
1857 bfd *output_bfd;
1858 char **error_message ATTRIBUTE_UNUSED;
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
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)
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;
1883 bfd *output_bfd;
1884 char **error_message ATTRIBUTE_UNUSED;
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)
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;
1942 bfd *output_bfd;
1943 char **error_message ATTRIBUTE_UNUSED;
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)
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;
1967 bfd *output_bfd;
1968 char **error_message ATTRIBUTE_UNUSED;
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)
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;
1992 bfd *output_bfd;
1993 char **error_message ATTRIBUTE_UNUSED;
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
2005#define MAX_RELOC_INDEX \
2006 (sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]) - 1)
2007
2008/* FIXME: There is a possiblity that when we read in a reloc from a file,
2009 that there are some bits encoded in the upper portion of the
2010 type field. Not yet implemented.
2011*/
2012static void ppc_coff_rtype2howto PARAMS ((arelent *relent,
2013 struct internal_reloc *internal));
2014
2015static void
2016ppc_coff_rtype2howto (relent, internal)
2017 arelent *relent;
2018 struct internal_reloc *internal;
2019{
2020
2021 /* We can encode one of three things in the type field, aside from the
2022 type:
2023 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2024 value, rather than an addition value
2025 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2026 the branch is expected to be taken or not.
2027 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2028 For now, we just strip this stuff to find the type, and ignore it other
2029 than that.
2030 */
2031 reloc_howto_type *howto;
2032 unsigned short r_type = EXTRACT_TYPE (internal->r_type);
2033 unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
2034 unsigned short junk = EXTRACT_JUNK (internal->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_ADDR16:
2047 case IMAGE_REL_PPC_REL24:
2048 case IMAGE_REL_PPC_ADDR24:
2049 case IMAGE_REL_PPC_ADDR32:
2050 case IMAGE_REL_PPC_IFGLUE:
2051 case IMAGE_REL_PPC_ADDR32NB:
2052 case IMAGE_REL_PPC_SECTION:
2053 case IMAGE_REL_PPC_SECREL:
2054 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2055 howto = ppc_coff_howto_table + r_type;
2056 break;
2057 case IMAGE_REL_PPC_IMGLUE:
2058 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2059 howto = ppc_coff_howto_table + r_type;
2060 break;
2061 case IMAGE_REL_PPC_TOCREL16:
2062 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2063 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
2064 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
2065 else
2066 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
2067 break;
2068 default:
2069 fprintf (stderr,
2070 _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
2071 ppc_coff_howto_table[r_type].name,
2072 r_type);
2073 howto = ppc_coff_howto_table + r_type;
2074 break;
2075 }
2076
2077 relent->howto = howto;
2078
2079}
2080
2081static reloc_howto_type *
2082coff_ppc_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
2083 bfd *abfd ATTRIBUTE_UNUSED;
2084 asection *sec;
2085 struct internal_reloc *rel;
2086 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED;
2087 struct internal_syment *sym ATTRIBUTE_UNUSED;
2088 bfd_vma *addendp;
2089{
2090 reloc_howto_type *howto;
2091
2092 /* We can encode one of three things in the type field, aside from the
2093 type:
2094 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2095 value, rather than an addition value
2096 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2097 the branch is expected to be taken or not.
2098 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2099 For now, we just strip this stuff to find the type, and ignore it other
2100 than that.
2101 */
2102
2103 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
2104 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
2105 unsigned short junk = EXTRACT_JUNK (rel->r_type);
2106
2107 /* the masking process only slices off the bottom byte for r_type. */
2108 if ( r_type > MAX_RELOC_INDEX )
2109 abort ();
2110
2111 /* check for absolute crap */
2112 if ( junk != 0 )
2113 abort ();
2114
2115 switch(r_type)
2116 {
2117 case IMAGE_REL_PPC_ADDR32NB:
2118 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2119 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
2120 howto = ppc_coff_howto_table + r_type;
2121 break;
2122 case IMAGE_REL_PPC_TOCREL16:
2123 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2124 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
2125 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
2126 else
2127 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
2128 break;
2129 case IMAGE_REL_PPC_ADDR16:
2130 case IMAGE_REL_PPC_REL24:
2131 case IMAGE_REL_PPC_ADDR24:
2132 case IMAGE_REL_PPC_ADDR32:
2133 case IMAGE_REL_PPC_IFGLUE:
2134 case IMAGE_REL_PPC_SECTION:
2135 case IMAGE_REL_PPC_SECREL:
2136 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2137 howto = ppc_coff_howto_table + r_type;
2138 break;
2139 case IMAGE_REL_PPC_IMGLUE:
2140 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2141 howto = ppc_coff_howto_table + r_type;
2142 break;
2143 default:
2144 fprintf (stderr,
2145 _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
2146 ppc_coff_howto_table[r_type].name,
2147 r_type);
2148 howto = ppc_coff_howto_table + r_type;
2149 break;
2150 }
2151
2152 return howto;
2153}
2154
2155/* a cheesy little macro to make the code a little more readable */
2156#define HOW2MAP(bfd_rtype,ppc_rtype) \
2157 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
2158
2159static reloc_howto_type *ppc_coff_reloc_type_lookup
2160PARAMS ((bfd *, bfd_reloc_code_real_type));
2161
2162static reloc_howto_type *
2163ppc_coff_reloc_type_lookup (abfd, code)
2164 bfd *abfd ATTRIBUTE_UNUSED;
2165 bfd_reloc_code_real_type code;
2166{
2167 switch (code)
2168 {
2169 HOW2MAP(BFD_RELOC_32_GOTOFF, IMAGE_REL_PPC_IMGLUE);
2170 HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
2171 HOW2MAP(BFD_RELOC_16, IMAGE_REL_PPC_ADDR16);
2172 HOW2MAP(BFD_RELOC_PPC_B26, IMAGE_REL_PPC_REL24);
2173 HOW2MAP(BFD_RELOC_PPC_BA26, IMAGE_REL_PPC_ADDR24);
2174 HOW2MAP(BFD_RELOC_PPC_TOC16, IMAGE_REL_PPC_TOCREL16);
2175 HOW2MAP(BFD_RELOC_16_GOTOFF, IMAGE_REL_PPC_TOCREL16_DEFN);
2176 HOW2MAP(BFD_RELOC_32, IMAGE_REL_PPC_ADDR32);
2177 HOW2MAP(BFD_RELOC_RVA, IMAGE_REL_PPC_ADDR32NB);
2178 default:
2179 return NULL;
2180 }
2181 /*NOTREACHED*/
2182}
2183
2184#undef HOW2MAP
2185
2186
2187/* Tailor coffcode.h -- macro heaven. */
2188
2189#define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
2190
2191#ifndef COFF_IMAGE_WITH_PE
2192static void ppc_coff_swap_sym_in_hook PARAMS ((bfd *, PTR, PTR));
2193#endif
2194
2195/* We use the special COFF backend linker, with our own special touch. */
2196
2197#define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
2198#define coff_rtype_to_howto coff_ppc_rtype_to_howto
2199#define coff_relocate_section coff_ppc_relocate_section
2200#define coff_bfd_final_link ppc_bfd_coff_final_link
2201
2202#ifndef COFF_IMAGE_WITH_PE
2203/* FIXME: This no longer works. */
2204#define coff_swap_sym_in_hook ppc_coff_swap_sym_in_hook
2205#endif
2206
2207#define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
2208
2209#define COFF_PAGE_SIZE 0x1000
2210
2211/* FIXME: This controls some code that used to be in peicode.h and is
2212 now in peigen.c. It will not control the code in peigen.c. If
2213 anybody wants to get this working, you will need to fix that. */
2214#define POWERPC_LE_PE
2215
2216#define COFF_SECTION_ALIGNMENT_ENTRIES \
2217{ COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
2218 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2219{ COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
2220 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2221{ COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
2222 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2223{ COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
2224 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2225{ COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
2226 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
2227{ COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
2228 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
2229
2230#include "coffcode.h"
2231
2232
2233#ifndef COFF_IMAGE_WITH_PE
2234/* FIXME:
2235 What we're trying to do here is allocate a toc section (early), and attach
2236 it to the last bfd to be processed. This avoids the problem of having a toc
2237 written out before all files have been processed. This code allocates
2238 a toc section for every file, and records the last one seen. There are
2239 at least two problems with this approach:
2240 1. We allocate whole bunches of toc sections that are ignored, but at
2241 at least we will not allocate a toc if no .toc is present.
2242 2. It's not clear to me that being the last bfd read necessarily means
2243 that you are the last bfd closed.
2244 3. Doing it on a "swap in" hook depends on when the "swap in" is called,
2245 and how often, etc. It's not clear to me that there isn't a hole here.
2246*/
2247
2248static void
2249ppc_coff_swap_sym_in_hook (abfd, ext1, in1)
2250 bfd *abfd;
2251 PTR ext1 ATTRIBUTE_UNUSED;
2252 PTR in1;
2253{
2254 struct internal_syment *in = (struct internal_syment *)in1;
2255
2256 if (bfd_of_toc_owner != 0) /* we already have a toc, so go home */
2257 return;
2258
2259 if (strcmp(in->_n._n_name, ".toc") == 0)
2260 {
2261 flagword flags;
2262 register asection *s;
2263
2264 s = bfd_get_section_by_name ( abfd , TOC_SECTION_NAME);
2265 if (s != NULL)
2266 {
2267 return;
2268 }
2269
2270 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY ;
2271
2272 s = bfd_make_section (abfd, TOC_SECTION_NAME);
2273
2274 if (s == NULL
2275 || !bfd_set_section_flags (abfd, s, flags)
2276 || !bfd_set_section_alignment (abfd, s, 2))
2277 {
2278 /* FIXME: set appropriate bfd error */
2279 abort ();
2280 }
2281
2282 /* save the bfd for later allocation */
2283 bfd_of_toc_owner = abfd;
2284 }
2285
2286 return;
2287}
2288#endif
2289
2290#ifndef COFF_IMAGE_WITH_PE
2291
2292static boolean ppc_do_last PARAMS ((bfd *));
2293static bfd *ppc_get_last PARAMS ((void));
2294
2295static boolean
2296ppc_do_last (abfd)
2297 bfd *abfd;
2298{
2299 if (abfd == bfd_of_toc_owner)
2300 return true;
2301 else
2302 return false;
2303}
2304
2305static bfd *
2306ppc_get_last()
2307{
2308 return bfd_of_toc_owner;
2309}
2310
2311/* this piece of machinery exists only to guarantee that the bfd that holds
2312 the toc section is written last.
2313
2314 This does depend on bfd_make_section attaching a new section to the
2315 end of the section list for the bfd.
2316
2317 This is otherwise intended to be functionally the same as
2318 cofflink.c:_bfd_coff_final_link(). It is specifically different only
2319 where the POWERPC_LE_PE macro modifies the code. It is left in as a
2320 precise form of comment. krk@cygnus.com
2321*/
2322
2323/* Do the final link step. */
2324
2325boolean
2326ppc_bfd_coff_final_link (abfd, info)
2327 bfd *abfd;
2328 struct bfd_link_info *info;
2329{
2330 bfd_size_type symesz;
2331 struct coff_final_link_info finfo;
2332 boolean debug_merge_allocated;
2333 asection *o;
2334 struct bfd_link_order *p;
2335 size_t max_sym_count;
2336 size_t max_lineno_count;
2337 size_t max_reloc_count;
2338 size_t max_output_reloc_count;
2339 size_t max_contents_size;
2340 file_ptr rel_filepos;
2341 unsigned int relsz;
2342 file_ptr line_filepos;
2343 unsigned int linesz;
2344 bfd *sub;
2345 bfd_byte *external_relocs = NULL;
2346 char strbuf[STRING_SIZE_SIZE];
2347
2348 symesz = bfd_coff_symesz (abfd);
2349
2350 finfo.info = info;
2351 finfo.output_bfd = abfd;
2352 finfo.strtab = NULL;
2353 finfo.section_info = NULL;
2354 finfo.last_file_index = -1;
2355 finfo.last_bf_index = -1;
2356 finfo.internal_syms = NULL;
2357 finfo.sec_ptrs = NULL;
2358 finfo.sym_indices = NULL;
2359 finfo.outsyms = NULL;
2360 finfo.linenos = NULL;
2361 finfo.contents = NULL;
2362 finfo.external_relocs = NULL;
2363 finfo.internal_relocs = NULL;
2364 debug_merge_allocated = false;
2365
2366 coff_data (abfd)->link_info = info;
2367
2368 finfo.strtab = _bfd_stringtab_init ();
2369 if (finfo.strtab == NULL)
2370 goto error_return;
2371
2372 if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
2373 goto error_return;
2374 debug_merge_allocated = true;
2375
2376 /* Compute the file positions for all the sections. */
2377 if (! abfd->output_has_begun)
2378 {
2379 if (! bfd_coff_compute_section_file_positions (abfd))
2380 return false;
2381 }
2382
2383 /* Count the line numbers and relocation entries required for the
2384 output file. Set the file positions for the relocs. */
2385 rel_filepos = obj_relocbase (abfd);
2386 relsz = bfd_coff_relsz (abfd);
2387 max_contents_size = 0;
2388 max_lineno_count = 0;
2389 max_reloc_count = 0;
2390
2391 for (o = abfd->sections; o != NULL; o = o->next)
2392 {
2393 o->reloc_count = 0;
2394 o->lineno_count = 0;
2395 for (p = o->link_order_head; p != NULL; p = p->next)
2396 {
2397
2398 if (p->type == bfd_indirect_link_order)
2399 {
2400 asection *sec;
2401
2402 sec = p->u.indirect.section;
2403
2404 /* Mark all sections which are to be included in the
2405 link. This will normally be every section. We need
2406 to do this so that we can identify any sections which
2407 the linker has decided to not include. */
2408 sec->linker_mark = true;
2409
2410 if (info->strip == strip_none
2411 || info->strip == strip_some)
2412 o->lineno_count += sec->lineno_count;
2413
2414 if (info->relocateable)
2415 o->reloc_count += sec->reloc_count;
2416
2417 if (sec->_raw_size > max_contents_size)
2418 max_contents_size = sec->_raw_size;
2419 if (sec->lineno_count > max_lineno_count)
2420 max_lineno_count = sec->lineno_count;
2421 if (sec->reloc_count > max_reloc_count)
2422 max_reloc_count = sec->reloc_count;
2423 }
2424 else if (info->relocateable
2425 && (p->type == bfd_section_reloc_link_order
2426 || p->type == bfd_symbol_reloc_link_order))
2427 ++o->reloc_count;
2428 }
2429 if (o->reloc_count == 0)
2430 o->rel_filepos = 0;
2431 else
2432 {
2433 o->flags |= SEC_RELOC;
2434 o->rel_filepos = rel_filepos;
2435 rel_filepos += o->reloc_count * relsz;
2436 }
2437 }
2438
2439 /* If doing a relocateable link, allocate space for the pointers we
2440 need to keep. */
2441 if (info->relocateable)
2442 {
2443 unsigned int i;
2444
2445 /* We use section_count + 1, rather than section_count, because
2446 the target_index fields are 1 based. */
2447 finfo.section_info =
2448 ((struct coff_link_section_info *)
2449 bfd_malloc ((abfd->section_count + 1)
2450 * sizeof (struct coff_link_section_info)));
2451 if (finfo.section_info == NULL)
2452 goto error_return;
2453 for (i = 0; i <= abfd->section_count; i++)
2454 {
2455 finfo.section_info[i].relocs = NULL;
2456 finfo.section_info[i].rel_hashes = NULL;
2457 }
2458 }
2459
2460 /* We now know the size of the relocs, so we can determine the file
2461 positions of the line numbers. */
2462 line_filepos = rel_filepos;
2463 linesz = bfd_coff_linesz (abfd);
2464 max_output_reloc_count = 0;
2465 for (o = abfd->sections; o != NULL; o = o->next)
2466 {
2467 if (o->lineno_count == 0)
2468 o->line_filepos = 0;
2469 else
2470 {
2471 o->line_filepos = line_filepos;
2472 line_filepos += o->lineno_count * linesz;
2473 }
2474
2475 if (o->reloc_count != 0)
2476 {
2477 /* We don't know the indices of global symbols until we have
2478 written out all the local symbols. For each section in
2479 the output file, we keep an array of pointers to hash
2480 table entries. Each entry in the array corresponds to a
2481 reloc. When we find a reloc against a global symbol, we
2482 set the corresponding entry in this array so that we can
2483 fix up the symbol index after we have written out all the
2484 local symbols.
2485
2486 Because of this problem, we also keep the relocs in
2487 memory until the end of the link. This wastes memory,
2488 but only when doing a relocateable link, which is not the
2489 common case. */
2490 BFD_ASSERT (info->relocateable);
2491 finfo.section_info[o->target_index].relocs =
2492 ((struct internal_reloc *)
2493 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
2494 finfo.section_info[o->target_index].rel_hashes =
2495 ((struct coff_link_hash_entry **)
2496 bfd_malloc (o->reloc_count
2497 * sizeof (struct coff_link_hash_entry *)));
2498 if (finfo.section_info[o->target_index].relocs == NULL
2499 || finfo.section_info[o->target_index].rel_hashes == NULL)
2500 goto error_return;
2501
2502 if (o->reloc_count > max_output_reloc_count)
2503 max_output_reloc_count = o->reloc_count;
2504 }
2505
2506 /* Reset the reloc and lineno counts, so that we can use them to
2507 count the number of entries we have output so far. */
2508 o->reloc_count = 0;
2509 o->lineno_count = 0;
2510 }
2511
2512 obj_sym_filepos (abfd) = line_filepos;
2513
2514 /* Figure out the largest number of symbols in an input BFD. Take
2515 the opportunity to clear the output_has_begun fields of all the
2516 input BFD's. */
2517 max_sym_count = 0;
2518 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2519 {
2520 size_t sz;
2521
2522 sub->output_has_begun = false;
2523 sz = obj_raw_syment_count (sub);
2524 if (sz > max_sym_count)
2525 max_sym_count = sz;
2526 }
2527
2528 /* Allocate some buffers used while linking. */
2529 finfo.internal_syms = ((struct internal_syment *)
2530 bfd_malloc (max_sym_count
2531 * sizeof (struct internal_syment)));
2532 finfo.sec_ptrs = (asection **) bfd_malloc (max_sym_count
2533 * sizeof (asection *));
2534 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
2535 finfo.outsyms = ((bfd_byte *)
2536 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
2537 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
2538 * bfd_coff_linesz (abfd));
2539 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
2540 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
2541 if (! info->relocateable)
2542 finfo.internal_relocs = ((struct internal_reloc *)
2543 bfd_malloc (max_reloc_count
2544 * sizeof (struct internal_reloc)));
2545 if ((finfo.internal_syms == NULL && max_sym_count > 0)
2546 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
2547 || (finfo.sym_indices == NULL && max_sym_count > 0)
2548 || finfo.outsyms == NULL
2549 || (finfo.linenos == NULL && max_lineno_count > 0)
2550 || (finfo.contents == NULL && max_contents_size > 0)
2551 || (finfo.external_relocs == NULL && max_reloc_count > 0)
2552 || (! info->relocateable
2553 && finfo.internal_relocs == NULL
2554 && max_reloc_count > 0))
2555 goto error_return;
2556
2557 /* We now know the position of everything in the file, except that
2558 we don't know the size of the symbol table and therefore we don't
2559 know where the string table starts. We just build the string
2560 table in memory as we go along. We process all the relocations
2561 for a single input file at once. */
2562 obj_raw_syment_count (abfd) = 0;
2563
2564 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
2565 {
2566 if (! bfd_coff_start_final_link (abfd, info))
2567 goto error_return;
2568 }
2569
2570 for (o = abfd->sections; o != NULL; o = o->next)
2571 {
2572 for (p = o->link_order_head; p != NULL; p = p->next)
2573 {
2574 if (p->type == bfd_indirect_link_order
2575 && (bfd_get_flavour (p->u.indirect.section->owner)
2576 == bfd_target_coff_flavour))
2577 {
2578 sub = p->u.indirect.section->owner;
2579#ifdef POWERPC_LE_PE
2580 if (! sub->output_has_begun && !ppc_do_last(sub))
2581#else
2582 if (! sub->output_has_begun)
2583#endif
2584 {
2585 if (! _bfd_coff_link_input_bfd (&finfo, sub))
2586 goto error_return;
2587 sub->output_has_begun = true;
2588 }
2589 }
2590 else if (p->type == bfd_section_reloc_link_order
2591 || p->type == bfd_symbol_reloc_link_order)
2592 {
2593 if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
2594 goto error_return;
2595 }
2596 else
2597 {
2598 if (! _bfd_default_link_order (abfd, info, o, p))
2599 goto error_return;
2600 }
2601 }
2602 }
2603
2604#ifdef POWERPC_LE_PE
2605 {
2606 bfd* last_one = ppc_get_last();
2607 if (last_one)
2608 {
2609 if (! _bfd_coff_link_input_bfd (&finfo, last_one))
2610 goto error_return;
2611 }
2612 last_one->output_has_begun = true;
2613 }
2614#endif
2615
2616 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
2617
2618 coff_debug_merge_hash_table_free (&finfo.debug_merge);
2619 debug_merge_allocated = false;
2620
2621 if (finfo.internal_syms != NULL)
2622 {
2623 free (finfo.internal_syms);
2624 finfo.internal_syms = NULL;
2625 }
2626 if (finfo.sec_ptrs != NULL)
2627 {
2628 free (finfo.sec_ptrs);
2629 finfo.sec_ptrs = NULL;
2630 }
2631 if (finfo.sym_indices != NULL)
2632 {
2633 free (finfo.sym_indices);
2634 finfo.sym_indices = NULL;
2635 }
2636 if (finfo.linenos != NULL)
2637 {
2638 free (finfo.linenos);
2639 finfo.linenos = NULL;
2640 }
2641 if (finfo.contents != NULL)
2642 {
2643 free (finfo.contents);
2644 finfo.contents = NULL;
2645 }
2646 if (finfo.external_relocs != NULL)
2647 {
2648 free (finfo.external_relocs);
2649 finfo.external_relocs = NULL;
2650 }
2651 if (finfo.internal_relocs != NULL)
2652 {
2653 free (finfo.internal_relocs);
2654 finfo.internal_relocs = NULL;
2655 }
2656
2657 /* The value of the last C_FILE symbol is supposed to be the symbol
2658 index of the first external symbol. Write it out again if
2659 necessary. */
2660 if (finfo.last_file_index != -1
2661 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
2662 {
2663 finfo.last_file.n_value = obj_raw_syment_count (abfd);
2664 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
2665 (PTR) finfo.outsyms);
2666 if (bfd_seek (abfd,
2667 (obj_sym_filepos (abfd)
2668 + finfo.last_file_index * symesz),
2669 SEEK_SET) != 0
2670 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
2671 return false;
2672 }
2673
2674 /* Write out the global symbols. */
2675 finfo.failed = false;
2676 coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_global_sym,
2677 (PTR) &finfo);
2678 if (finfo.failed)
2679 goto error_return;
2680
2681 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
2682 if (finfo.outsyms != NULL)
2683 {
2684 free (finfo.outsyms);
2685 finfo.outsyms = NULL;
2686 }
2687
2688 if (info->relocateable)
2689 {
2690 /* Now that we have written out all the global symbols, we know
2691 the symbol indices to use for relocs against them, and we can
2692 finally write out the relocs. */
2693 external_relocs = ((bfd_byte *)
2694 bfd_malloc (max_output_reloc_count * relsz));
2695 if (external_relocs == NULL)
2696 goto error_return;
2697
2698 for (o = abfd->sections; o != NULL; o = o->next)
2699 {
2700 struct internal_reloc *irel;
2701 struct internal_reloc *irelend;
2702 struct coff_link_hash_entry **rel_hash;
2703 bfd_byte *erel;
2704
2705 if (o->reloc_count == 0)
2706 continue;
2707
2708 irel = finfo.section_info[o->target_index].relocs;
2709 irelend = irel + o->reloc_count;
2710 rel_hash = finfo.section_info[o->target_index].rel_hashes;
2711 erel = external_relocs;
2712 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
2713 {
2714 if (*rel_hash != NULL)
2715 {
2716 BFD_ASSERT ((*rel_hash)->indx >= 0);
2717 irel->r_symndx = (*rel_hash)->indx;
2718 }
2719 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
2720 }
2721
2722 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
2723 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
2724 abfd) != relsz * o->reloc_count)
2725 goto error_return;
2726 }
2727
2728 free (external_relocs);
2729 external_relocs = NULL;
2730 }
2731
2732 /* Free up the section information. */
2733 if (finfo.section_info != NULL)
2734 {
2735 unsigned int i;
2736
2737 for (i = 0; i < abfd->section_count; i++)
2738 {
2739 if (finfo.section_info[i].relocs != NULL)
2740 free (finfo.section_info[i].relocs);
2741 if (finfo.section_info[i].rel_hashes != NULL)
2742 free (finfo.section_info[i].rel_hashes);
2743 }
2744 free (finfo.section_info);
2745 finfo.section_info = NULL;
2746 }
2747
2748 /* If we have optimized stabs strings, output them. */
2749 if (coff_hash_table (info)->stab_info != NULL)
2750 {
2751 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
2752 return false;
2753 }
2754
2755 /* Write out the string table. */
2756 if (obj_raw_syment_count (abfd) != 0)
2757 {
2758 if (bfd_seek (abfd,
2759 (obj_sym_filepos (abfd)
2760 + obj_raw_syment_count (abfd) * symesz),
2761 SEEK_SET) != 0)
2762 return false;
2763
2764#if STRING_SIZE_SIZE == 4
2765 bfd_h_put_32 (abfd,
2766 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
2767 (bfd_byte *) strbuf);
2768#else
2769 #error Change bfd_h_put_32
2770#endif
2771
2772 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
2773 return false;
2774
2775 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
2776 return false;
2777 }
2778
2779 _bfd_stringtab_free (finfo.strtab);
2780
2781 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
2782 not try to write out the symbols. */
2783 bfd_get_symcount (abfd) = 0;
2784
2785 return true;
2786
2787 error_return:
2788 if (debug_merge_allocated)
2789 coff_debug_merge_hash_table_free (&finfo.debug_merge);
2790 if (finfo.strtab != NULL)
2791 _bfd_stringtab_free (finfo.strtab);
2792 if (finfo.section_info != NULL)
2793 {
2794 unsigned int i;
2795
2796 for (i = 0; i < abfd->section_count; i++)
2797 {
2798 if (finfo.section_info[i].relocs != NULL)
2799 free (finfo.section_info[i].relocs);
2800 if (finfo.section_info[i].rel_hashes != NULL)
2801 free (finfo.section_info[i].rel_hashes);
2802 }
2803 free (finfo.section_info);
2804 }
2805 if (finfo.internal_syms != NULL)
2806 free (finfo.internal_syms);
2807 if (finfo.sec_ptrs != NULL)
2808 free (finfo.sec_ptrs);
2809 if (finfo.sym_indices != NULL)
2810 free (finfo.sym_indices);
2811 if (finfo.outsyms != NULL)
2812 free (finfo.outsyms);
2813 if (finfo.linenos != NULL)
2814 free (finfo.linenos);
2815 if (finfo.contents != NULL)
2816 free (finfo.contents);
2817 if (finfo.external_relocs != NULL)
2818 free (finfo.external_relocs);
2819 if (finfo.internal_relocs != NULL)
2820 free (finfo.internal_relocs);
2821 if (external_relocs != NULL)
2822 free (external_relocs);
2823 return false;
2824}
2825#endif
2826
2827
2828/* Forward declaration for use by alternative_target field. */
2829#ifdef TARGET_BIG_SYM
2830extern const bfd_target TARGET_BIG_SYM;
2831#endif
2832
2833/* The transfer vectors that lead the outside world to all of the above. */
2834
2835#ifdef TARGET_LITTLE_SYM
2836const bfd_target TARGET_LITTLE_SYM =
2837{
2838 TARGET_LITTLE_NAME, /* name or coff-arm-little */
2839 bfd_target_coff_flavour,
2840 BFD_ENDIAN_LITTLE, /* data byte order is little */
2841 BFD_ENDIAN_LITTLE, /* header byte order is little */
2842
2843 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
2844 HAS_LINENO | HAS_DEBUG |
2845 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2846
2847#ifndef COFF_WITH_PE
2848 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2849#else
2850 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
2851 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2852#endif
2853
2854 0, /* leading char */
2855 '/', /* ar_pad_char */
2856 15, /* ar_max_namelen??? FIXMEmgo */
2857
2858 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2859 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2860 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2861
2862 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2863 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2864 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2865
2866 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2867 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2868 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2869 bfd_false},
2870 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
2871 _bfd_write_archive_contents, bfd_false},
2872
2873 BFD_JUMP_TABLE_GENERIC (coff),
2874 BFD_JUMP_TABLE_COPY (coff),
2875 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2876 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2877 BFD_JUMP_TABLE_SYMBOLS (coff),
2878 BFD_JUMP_TABLE_RELOCS (coff),
2879 BFD_JUMP_TABLE_WRITE (coff),
2880 BFD_JUMP_TABLE_LINK (coff),
2881 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2882
2883 /* Alternative_target. */
2884#ifdef TARGET_BIG_SYM
2885 & TARGET_BIG_SYM,
2886#else
2887 NULL,
2888#endif
2889
2890 COFF_SWAP_TABLE
2891};
2892#endif
2893
2894#ifdef TARGET_BIG_SYM
2895const bfd_target TARGET_BIG_SYM =
2896{
2897 TARGET_BIG_NAME,
2898 bfd_target_coff_flavour,
2899 BFD_ENDIAN_BIG, /* data byte order is big */
2900 BFD_ENDIAN_BIG, /* header byte order is big */
2901
2902 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
2903 HAS_LINENO | HAS_DEBUG |
2904 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2905
2906#ifndef COFF_WITH_PE
2907 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2908#else
2909 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
2910 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2911#endif
2912
2913 0, /* leading char */
2914 '/', /* ar_pad_char */
2915 15, /* ar_max_namelen??? FIXMEmgo */
2916
2917 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2918 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2919 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
2920
2921 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2922 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2923 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
2924
2925 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2926 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2927 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2928 bfd_false},
2929 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
2930 _bfd_write_archive_contents, bfd_false},
2931
2932 BFD_JUMP_TABLE_GENERIC (coff),
2933 BFD_JUMP_TABLE_COPY (coff),
2934 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2935 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2936 BFD_JUMP_TABLE_SYMBOLS (coff),
2937 BFD_JUMP_TABLE_RELOCS (coff),
2938 BFD_JUMP_TABLE_WRITE (coff),
2939 BFD_JUMP_TABLE_LINK (coff),
2940 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2941
2942 /* Alternative_target. */
2943#ifdef TARGET_LITTLE_SYM
2944 & TARGET_LITTLE_SYM,
2945#else
2946 NULL,
2947#endif
2948
2949 COFF_SWAP_TABLE
2950};
2951
2952#endif
Note: See TracBrowser for help on using the repository browser.