1 /* BFD back-end for PowerPC Microsoft Portable Executable files.
2 Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 Original version pieced together by Kim Knuttila (krk@cygnus.com)
6 There is nothing new under the sun. This file draws a lot on other
7 coff files, in particular, those for the rs/6000, alpha, mips, and
8 intel backends, and the PE work for the arm.
10 This file is part of BFD, the Binary File Descriptor library.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 - relocs generated by gas
37 #include "coff/powerpc.h"
38 #include "coff/internal.h"
46 #define BADMAG(x) PPCBADMAG(x)
50 /* The main body of code is in coffcode.h. */
52 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
54 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
55 from smaller values. Start with zero, widen, *then* decrement. */
56 #define MINUS_ONE (((bfd_vma)0) - 1)
58 /* these should definitely go in a header file somewhere... */
61 #define IMAGE_REL_PPC_ABSOLUTE 0x0000
64 #define IMAGE_REL_PPC_ADDR64 0x0001
67 #define IMAGE_REL_PPC_ADDR32 0x0002
69 /* 26-bit address, shifted left 2 (branch absolute) */
70 #define IMAGE_REL_PPC_ADDR24 0x0003
73 #define IMAGE_REL_PPC_ADDR16 0x0004
75 /* 16-bit address, shifted left 2 (load doubleword) */
76 #define IMAGE_REL_PPC_ADDR14 0x0005
78 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
79 #define IMAGE_REL_PPC_REL24 0x0006
81 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
82 #define IMAGE_REL_PPC_REL14 0x0007
84 /* 16-bit offset from TOC base */
85 #define IMAGE_REL_PPC_TOCREL16 0x0008
87 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
88 #define IMAGE_REL_PPC_TOCREL14 0x0009
90 /* 32-bit addr w/o image base */
91 #define IMAGE_REL_PPC_ADDR32NB 0x000A
93 /* va of containing section (as in an image sectionhdr) */
94 #define IMAGE_REL_PPC_SECREL 0x000B
96 /* sectionheader number */
97 #define IMAGE_REL_PPC_SECTION 0x000C
99 /* substitute TOC restore instruction iff symbol is glue code */
100 #define IMAGE_REL_PPC_IFGLUE 0x000D
102 /* symbol is glue code; virtual address is TOC restore instruction */
103 #define IMAGE_REL_PPC_IMGLUE 0x000E
105 /* va of containing section (limited to 16 bits) */
106 #define IMAGE_REL_PPC_SECREL16 0x000F
108 /* stuff to handle immediate data when the number of bits in the */
109 /* data is greater than the number of bits in the immediate field */
110 /* We need to do (usually) 32 bit arithmetic on 16 bit chunks */
111 #define IMAGE_REL_PPC_REFHI 0x0010
112 #define IMAGE_REL_PPC_REFLO 0x0011
113 #define IMAGE_REL_PPC_PAIR 0x0012
116 /* Flag bits in IMAGE_RELOCATION.TYPE */
118 /* subtract reloc value rather than adding it */
119 #define IMAGE_REL_PPC_NEG 0x0100
121 /* fix branch prediction bit to predict branch taken */
122 #define IMAGE_REL_PPC_BRTAKEN 0x0200
124 /* fix branch prediction bit to predict branch not taken */
125 #define IMAGE_REL_PPC_BRNTAKEN 0x0400
127 /* toc slot defined in file (or, data in toc) */
128 #define IMAGE_REL_PPC_TOCDEFN 0x0800
130 /* masks to isolate above values in IMAGE_RELOCATION.Type */
131 #define IMAGE_REL_PPC_TYPEMASK 0x00FF
132 #define IMAGE_REL_PPC_FLAGMASK 0x0F00
134 #define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
135 #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
136 #define EXTRACT_JUNK(x) \
137 ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
140 /* static helper functions to make relocation work */
141 /* (Work In Progress) */
143 static bfd_reloc_status_type ppc_refhi_reloc PARAMS ((bfd *abfd,
150 static bfd_reloc_status_type ppc_reflo_reloc PARAMS ((bfd *abfd,
157 static bfd_reloc_status_type ppc_pair_reloc PARAMS ((bfd *abfd,
166 static bfd_reloc_status_type ppc_toc16_reloc PARAMS ((bfd *abfd,
174 static bfd_reloc_status_type ppc_addr32nb_reloc PARAMS ((bfd *abfd,
182 static bfd_reloc_status_type ppc_section_reloc PARAMS ((bfd *abfd,
190 static bfd_reloc_status_type ppc_secrel_reloc PARAMS ((bfd *abfd,
198 static bfd_reloc_status_type ppc_imglue_reloc PARAMS ((bfd *abfd,
210 /* FIXME: It'll take a while to get through all of these. I only need a few to
211 get us started, so those I'll make sure work. Those marked FIXME are either
212 completely unverified or have a specific unknown marked in the comment */
214 /*---------------------------------------------------------------------------*/
216 /* Relocation entries for Windows/NT on PowerPC. */
218 /* From the document "" we find the following listed as used relocs: */
220 /* ABSOLUTE : The noop */
221 /* ADDR[64|32|16] : fields that hold addresses in data fields or the */
222 /* 16 bit displacement field on a load/store. */
223 /* ADDR[24|14] : fields that hold addresses in branch and cond */
224 /* branches. These represent [26|16] bit addresses. */
225 /* The low order 2 bits are preserved. */
226 /* REL[24|14] : branches relative to the Instruction Address */
227 /* register. These represent [26|16] bit addresses, */
228 /* as before. The instruction field will be zero, and */
229 /* the address of the SYM will be inserted at link time. */
230 /* TOCREL16 : 16 bit displacement field referring to a slot in */
232 /* TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14. */
233 /* ADDR32NB : 32 bit address relative to the virtual origin. */
234 /* (On the alpha, this is always a linker generated thunk)*/
235 /* (i.e. 32bit addr relative to the image base) */
236 /* SECREL : The value is relative to the start of the section */
237 /* containing the symbol. */
238 /* SECTION : access to the header containing the item. Supports the */
239 /* codeview debugger. */
241 /* In particular, note that the document does not indicate that the */
242 /* relocations listed in the header file are used. */
246 /*---------------------------------------------------------------------------*/
248 static reloc_howto_type ppc_coff_howto_table[] =
250 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
252 HOWTO (IMAGE_REL_PPC_ABSOLUTE, /* type */
254 0, /* size (0 = byte, 1 = short, 2 = long) */
256 false, /* pc_relative */
258 complain_overflow_dont, /* dont complain_on_overflow */
259 0, /* special_function */
260 "ABSOLUTE", /* name */
261 false, /* partial_inplace */
264 false), /* pcrel_offset */
266 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
268 HOWTO(IMAGE_REL_PPC_ADDR64, /* type */
270 3, /* size (0 = byte, 1 = short, 2 = long) */
272 false, /* pc_relative */
274 complain_overflow_bitfield, /* complain_on_overflow */
275 0, /* special_function */
277 true, /* partial_inplace */
278 MINUS_ONE, /* src_mask */
279 MINUS_ONE, /* dst_mask */
280 false), /* pcrel_offset */
282 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
284 HOWTO (IMAGE_REL_PPC_ADDR32, /* type */
286 2, /* size (0 = byte, 1 = short, 2 = long) */
288 false, /* pc_relative */
290 complain_overflow_bitfield, /* complain_on_overflow */
291 0, /* special_function */
293 true, /* partial_inplace */
294 0xffffffff, /* src_mask */
295 0xffffffff, /* dst_mask */
296 false), /* pcrel_offset */
298 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
299 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
300 /* Of course, That's the IBM approved bit numbering, which is not what
301 /* anyone else uses.... The li field is in bit 2 thru 25 */
303 HOWTO (IMAGE_REL_PPC_ADDR24, /* type */
305 2, /* size (0 = byte, 1 = short, 2 = long) */
307 false, /* pc_relative */
309 complain_overflow_bitfield, /* complain_on_overflow */
310 0, /* special_function */
312 true, /* partial_inplace */
313 0x07fffffc, /* src_mask */
314 0x07fffffc, /* dst_mask */
315 false), /* pcrel_offset */
317 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
319 HOWTO (IMAGE_REL_PPC_ADDR16, /* type */
321 1, /* size (0 = byte, 1 = short, 2 = long) */
323 false, /* pc_relative */
325 complain_overflow_signed, /* complain_on_overflow */
326 0, /* special_function */
328 true, /* partial_inplace */
329 0xffff, /* src_mask */
330 0xffff, /* dst_mask */
331 false), /* pcrel_offset */
333 /* IMAGE_REL_PPC_ADDR14 0x0005 */
334 /* 16-bit address, shifted left 2 (load doubleword) */
335 /* FIXME: the mask is likely wrong, and the bit position may be as well */
337 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
339 1, /* size (0 = byte, 1 = short, 2 = long) */
341 false, /* pc_relative */
343 complain_overflow_signed, /* complain_on_overflow */
344 0, /* special_function */
346 true, /* partial_inplace */
347 0xffff, /* src_mask */
348 0xffff, /* dst_mask */
349 false), /* pcrel_offset */
351 /* IMAGE_REL_PPC_REL24 0x0006 */
352 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
354 HOWTO (IMAGE_REL_PPC_REL24, /* type */
356 2, /* size (0 = byte, 1 = short, 2 = long) */
358 true, /* pc_relative */
360 complain_overflow_signed, /* complain_on_overflow */
361 0, /* special_function */
363 true, /* partial_inplace */
364 0x3fffffc, /* src_mask */
365 0x3fffffc, /* dst_mask */
366 false), /* pcrel_offset */
368 /* IMAGE_REL_PPC_REL14 0x0007 */
369 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
370 /* FIXME: the mask is likely wrong, and the bit position may be as well */
371 /* FIXME: how does it know how far to shift? */
373 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
375 1, /* size (0 = byte, 1 = short, 2 = long) */
377 false, /* pc_relative */
379 complain_overflow_signed, /* complain_on_overflow */
380 0, /* special_function */
382 true, /* partial_inplace */
383 0xffff, /* src_mask */
384 0xffff, /* dst_mask */
385 true), /* pcrel_offset */
387 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
388 /* 16-bit offset from TOC base */
390 HOWTO (IMAGE_REL_PPC_TOCREL16,/* type */
392 1, /* size (0 = byte, 1 = short, 2 = long) */
394 false, /* pc_relative */
396 complain_overflow_dont, /* complain_on_overflow */
397 ppc_toc16_reloc, /* special_function */
398 "TOCREL16", /* name */
399 false, /* partial_inplace */
400 0xffff, /* src_mask */
401 0xffff, /* dst_mask */
402 false), /* pcrel_offset */
404 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
405 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
407 HOWTO (IMAGE_REL_PPC_TOCREL14,/* type */
409 1, /* size (0 = byte, 1 = short, 2 = long) */
411 false, /* pc_relative */
413 complain_overflow_signed, /* complain_on_overflow */
414 0, /* special_function */
415 "TOCREL14", /* name */
416 false, /* partial_inplace */
417 0xffff, /* src_mask */
418 0xffff, /* dst_mask */
419 true), /* pcrel_offset */
421 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
422 /* 32-bit addr w/ image base */
424 HOWTO (IMAGE_REL_PPC_ADDR32NB,/* type */
426 2, /* size (0 = byte, 1 = short, 2 = long) */
428 false, /* pc_relative */
430 complain_overflow_signed, /* complain_on_overflow */
431 ppc_addr32nb_reloc, /* special_function */
432 "ADDR32NB", /* name */
433 true, /* partial_inplace */
434 0xffffffff, /* src_mask */
435 0xffffffff, /* dst_mask */
436 true), /* pcrel_offset */
438 /* IMAGE_REL_PPC_SECREL 0x000B */
439 /* va of containing section (as in an image sectionhdr) */
441 HOWTO (IMAGE_REL_PPC_SECREL,/* type */
443 2, /* size (0 = byte, 1 = short, 2 = long) */
445 false, /* pc_relative */
447 complain_overflow_signed, /* complain_on_overflow */
448 ppc_secrel_reloc, /* special_function */
450 true, /* partial_inplace */
451 0xffffffff, /* src_mask */
452 0xffffffff, /* dst_mask */
453 true), /* pcrel_offset */
455 /* IMAGE_REL_PPC_SECTION 0x000C */
456 /* sectionheader number */
458 HOWTO (IMAGE_REL_PPC_SECTION,/* type */
460 2, /* size (0 = byte, 1 = short, 2 = long) */
462 false, /* pc_relative */
464 complain_overflow_signed, /* complain_on_overflow */
465 ppc_section_reloc, /* special_function */
466 "SECTION", /* name */
467 true, /* partial_inplace */
468 0xffffffff, /* src_mask */
469 0xffffffff, /* dst_mask */
470 true), /* pcrel_offset */
472 /* IMAGE_REL_PPC_IFGLUE 0x000D */
473 /* substitute TOC restore instruction iff symbol is glue code */
475 HOWTO (IMAGE_REL_PPC_IFGLUE,/* type */
477 2, /* size (0 = byte, 1 = short, 2 = long) */
479 false, /* pc_relative */
481 complain_overflow_signed, /* complain_on_overflow */
482 0, /* special_function */
484 true, /* partial_inplace */
485 0xffffffff, /* src_mask */
486 0xffffffff, /* dst_mask */
487 true), /* pcrel_offset */
489 /* IMAGE_REL_PPC_IMGLUE 0x000E */
490 /* symbol is glue code; virtual address is TOC restore instruction */
492 HOWTO (IMAGE_REL_PPC_IMGLUE,/* type */
494 2, /* size (0 = byte, 1 = short, 2 = long) */
496 false, /* pc_relative */
498 complain_overflow_dont, /* complain_on_overflow */
499 ppc_imglue_reloc, /* special_function */
501 false, /* partial_inplace */
502 0xffffffff, /* src_mask */
503 0xffffffff, /* dst_mask */
504 false), /* pcrel_offset */
506 /* IMAGE_REL_PPC_SECREL16 0x000F */
507 /* va of containing section (limited to 16 bits) */
509 HOWTO (IMAGE_REL_PPC_SECREL16,/* type */
511 1, /* size (0 = byte, 1 = short, 2 = long) */
513 false, /* pc_relative */
515 complain_overflow_signed, /* complain_on_overflow */
516 0, /* special_function */
517 "SECREL16", /* name */
518 true, /* partial_inplace */
519 0xffff, /* src_mask */
520 0xffff, /* dst_mask */
521 true), /* pcrel_offset */
523 /* IMAGE_REL_PPC_REFHI 0x0010 */
525 HOWTO (IMAGE_REL_PPC_REFHI, /* type */
527 1, /* size (0 = byte, 1 = short, 2 = long) */
529 false, /* pc_relative */
531 complain_overflow_signed, /* complain_on_overflow */
532 ppc_refhi_reloc, /* special_function */
534 true, /* partial_inplace */
535 0xffffffff, /* src_mask */
536 0xffffffff, /* dst_mask */
537 false), /* pcrel_offset */
539 /* IMAGE_REL_PPC_REFLO 0x0011 */
541 HOWTO (IMAGE_REL_PPC_REFLO, /* type */
543 1, /* size (0 = byte, 1 = short, 2 = long) */
545 false, /* pc_relative */
547 complain_overflow_signed, /* complain_on_overflow */
548 ppc_refhi_reloc, /* special_function */
550 true, /* partial_inplace */
551 0xffffffff, /* src_mask */
552 0xffffffff, /* dst_mask */
553 false), /* pcrel_offset */
555 /* IMAGE_REL_PPC_PAIR 0x0012 */
557 HOWTO (IMAGE_REL_PPC_PAIR, /* type */
559 1, /* size (0 = byte, 1 = short, 2 = long) */
561 false, /* pc_relative */
563 complain_overflow_signed, /* complain_on_overflow */
564 ppc_pair_reloc, /* special_function */
566 true, /* partial_inplace */
567 0xffffffff, /* src_mask */
568 0xffffffff, /* dst_mask */
569 false) /* pcrel_offset */
573 /* Provided the symbol, returns the value reffed */
574 static long get_symbol_value PARAMS ((asymbol *));
577 get_symbol_value (symbol)
582 if (bfd_is_com_section (symbol->section))
588 relocation = symbol->value +
589 symbol->section->output_section->vma +
590 symbol->section->output_offset;
596 /* this function is in charge of performing all the ppc PE relocations */
597 /* Don't yet know if we want to do this this particular way ... (krk) */
598 /* (it is not yet enabled) */
600 static bfd_reloc_status_type
601 pe_ppc_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
604 arelent *reloc_entry;
607 asection *input_section;
609 char **error_message;
611 /* the consth relocation comes in two parts, we have to remember
612 the state between calls, in these variables */
613 static boolean part1_consth_active = false;
614 static unsigned long part1_consth_value;
617 unsigned long sym_value;
618 unsigned long unsigned_value;
619 unsigned short r_type;
622 unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
623 bfd_byte *hit_data =addr + (bfd_byte *)(data);
625 r_type = reloc_entry->howto->type;
628 /* Partial linking - do nothing */
629 reloc_entry->address += input_section->output_offset;
634 if (symbol_in != NULL
635 && bfd_is_und_section (symbol_in->section))
637 /* Keep the state machine happy in case we're called again */
638 if (r_type == IMAGE_REL_PPC_REFHI)
640 part1_consth_active = true;
641 part1_consth_value = 0;
643 return(bfd_reloc_undefined);
646 if ((part1_consth_active) && (r_type != IMAGE_REL_PPC_PAIR))
648 part1_consth_active = false;
649 *error_message = (char *) "Missing PAIR";
650 return(bfd_reloc_dangerous);
654 sym_value = get_symbol_value(symbol_in);
661 insn = bfd_get_32(abfd, hit_data);
662 /* Take the value in the field and sign extend it */
663 signed_value = EXTRACT_HWORD(insn);
664 signed_value = SIGN_EXTEND_HWORD(signed_value);
666 signed_value += sym_value + reloc_entry->addend;
667 if (((signed_value + reloc_entry->address) & ~0x3ffff) == 0)
668 { /* Absolute jmp/call */
669 insn |= (1<<24); /* Make it absolute */
670 signed_value += reloc_entry->address;
671 /* FIXME: Should we change r_type to R_IABS */
675 /* Relative jmp/call, so subtract from the value the
676 address of the place we're coming from */
677 signed_value -= (input_section->output_section->vma
678 + input_section->output_offset);
679 if (signed_value>0x1ffff || signed_value<-0x20000)
680 return(bfd_reloc_overflow);
683 insn = INSERT_HWORD(insn, signed_value);
684 bfd_put_32(abfd, insn ,hit_data);
687 insn = bfd_get_32(abfd, hit_data);
688 unsigned_value = EXTRACT_HWORD(insn);
689 unsigned_value += sym_value + reloc_entry->addend;
690 insn = INSERT_HWORD(insn, unsigned_value);
691 bfd_put_32(abfd, insn, hit_data);
694 insn = bfd_get_32(abfd, hit_data);
696 Just get the symbol value that is referenced */
697 part1_consth_active = true;
698 part1_consth_value = sym_value + reloc_entry->addend;
699 /* Don't modify insn until R_IHCONST */
702 insn = bfd_get_32(abfd, hit_data);
704 Now relocate the reference */
705 if (part1_consth_active == false) {
706 *error_message = (char *) "Missing IHIHALF";
707 return(bfd_reloc_dangerous);
709 /* sym_ptr_ptr = r_symndx, in coff_slurp_reloc_table() */
710 unsigned_value = 0; /*EXTRACT_HWORD(insn) << 16;*/
711 unsigned_value += reloc_entry->addend; /* r_symndx */
712 unsigned_value += part1_consth_value;
713 unsigned_value = unsigned_value >> 16;
714 insn = INSERT_HWORD(insn, unsigned_value);
715 part1_consth_active = false;
716 bfd_put_32(abfd, insn, hit_data);
719 insn = bfd_get_8(abfd, hit_data);
720 unsigned_value = insn + sym_value + reloc_entry->addend;
721 if (unsigned_value & 0xffffff00) {
722 fprintf(stderr,"Relocation problem : ");
723 fprintf(stderr,"byte value too large in module %s\n",
725 return(bfd_reloc_overflow);
727 bfd_put_8(abfd, unsigned_value, hit_data);
730 insn = bfd_get_16(abfd, hit_data);
731 unsigned_value = insn + sym_value + reloc_entry->addend;
732 if (unsigned_value & 0xffff0000) {
733 fprintf(stderr,"Relocation problem : ");
734 fprintf(stderr,"hword value too large in module %s\n",
736 return(bfd_reloc_overflow);
739 bfd_put_16(abfd, insn, hit_data);
742 insn = bfd_get_32(abfd, hit_data);
743 insn += sym_value + reloc_entry->addend;
744 bfd_put_32(abfd, insn, hit_data);
747 *error_message = "Unrecognized reloc";
748 return (bfd_reloc_dangerous);
753 return(bfd_reloc_ok);
756 /* The reloc processing routine for the optimized COFF linker. */
759 coff_ppc_relocate_section (output_bfd, info, input_bfd, input_section,
760 contents, relocs, syms, sections)
762 struct bfd_link_info *info;
764 asection *input_section;
766 struct internal_reloc *relocs;
767 struct internal_syment *syms;
770 struct internal_reloc *rel;
771 struct internal_reloc *relend;
775 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
776 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
777 unsigned short junk = EXTRACT_JUNK (rel->r_type);
780 /* If we are performing a relocateable link, we don't need to do a
781 thing. The caller will take care of adjusting the reloc
782 addresses and symbol indices. */
784 if (info->relocateable)
791 relend = rel + input_section->reloc_count;
792 for (; rel < relend; rel++)
796 struct coff_link_hash_entry *h;
797 struct internal_syment *sym;
803 unsigned long unsigned_value;
804 bfd_reloc_status_type rstat;
806 symndx = rel->r_symndx;
807 loc = contents + rel->r_vaddr - input_section->vma;
812 h = obj_coff_sym_hashes (input_bfd)[symndx];
823 fprintf( stderr, "unsupported reloc %s\n",
824 ppc_coff_howto_table[r_type].name);
825 bfd_set_error (bfd_error_bad_value);
827 case IMAGE_REL_PPC_TOCREL16:
828 case IMAGE_REL_PPC_IFGLUE:
829 case IMAGE_REL_PPC_SECTION:
830 case IMAGE_REL_PPC_SECREL:
831 case IMAGE_REL_PPC_IMGLUE:
832 case IMAGE_REL_PPC_ABSOLUTE:
835 case IMAGE_REL_PPC_ADDR16:
836 case IMAGE_REL_PPC_REL24:
837 case IMAGE_REL_PPC_ADDR24:
838 case IMAGE_REL_PPC_ADDR32:
839 case IMAGE_REL_PPC_ADDR32NB:
840 rstat = _bfd_relocate_contents (ppc_coff_howto_table + r_type,
841 input_bfd, val, loc);
842 if (rstat == bfd_reloc_overflow)
844 else if (rstat != bfd_reloc_ok)
852 char buf[SYMNMLEN + 1];
857 name = h->root.root.string;
858 else if (sym == NULL)
860 else if (sym->_n._n_n._n_zeroes == 0
861 && sym->_n._n_n._n_offset != 0)
862 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
865 strncpy (buf, sym->_n._n_name, SYMNMLEN);
866 buf[SYMNMLEN] = '\0';
870 if (! ((*info->callbacks->reloc_overflow)
871 (info, name, ppc_coff_howto_table[r_type].name, (bfd_vma) 0,
872 input_bfd, input_section,
873 rel->r_vaddr - input_section->vma)))
883 /* Some really cheezy macros that can be turned on to test stderr :-) */
892 fprintf(stderr,"Unimplemented Relocation -- %s\n",x); \
896 #define DUMP_RELOC(n,r) \
898 fprintf(stderr,"%s sym %d, addr %d, addend %d\n", \
899 n, (*(r->sym_ptr_ptr))->name, \
900 r->address, r->addend); \
903 #define DUMP_RELOC2(n,r) \
905 fprintf(stderr,"%s sym %d, r_vaddr %d\n", \
906 n, r->r_symndx, r->r_vaddr); \
911 #define DUMP_RELOC(n,r)
912 #define DUMP_RELOC2(n,r)
916 static bfd_reloc_status_type
917 ppc_refhi_reloc (abfd,
925 arelent *reloc_entry;
928 asection *input_section;
930 char **error_message;
933 DUMP_RELOC("REFHI",reloc_entry);
935 if (output_bfd == (bfd *) NULL)
936 return bfd_reloc_continue;
938 return bfd_reloc_undefined;
941 static bfd_reloc_status_type
942 ppc_reflo_reloc (abfd,
950 arelent *reloc_entry;
953 asection *input_section;
955 char **error_message;
958 DUMP_RELOC("REFLO",reloc_entry);
960 if (output_bfd == (bfd *) NULL)
961 return bfd_reloc_continue;
963 return bfd_reloc_undefined;
966 static bfd_reloc_status_type
967 ppc_pair_reloc (abfd,
975 arelent *reloc_entry;
978 asection *input_section;
980 char **error_message;
983 DUMP_RELOC("PAIR",reloc_entry);
985 if (output_bfd == (bfd *) NULL)
986 return bfd_reloc_continue;
988 return bfd_reloc_undefined;
992 static bfd_reloc_status_type
993 ppc_toc16_reloc (abfd,
1001 arelent *reloc_entry;
1004 asection *input_section;
1006 char **error_message;
1008 UN_IMPL("TOCREL16");
1009 DUMP_RELOC("TOCREL16",reloc_entry);
1011 if (output_bfd == (bfd *) NULL)
1013 return bfd_reloc_continue;
1016 return bfd_reloc_ok;
1019 /* ADDR32NB : 32 bit address relative to the virtual origin. */
1020 /* (On the alpha, this is always a linker generated thunk)*/
1021 /* (i.e. 32bit addr relative to the image base) */
1025 static bfd_reloc_status_type
1026 ppc_addr32nb_reloc (abfd,
1034 arelent *reloc_entry;
1037 asection *input_section;
1039 char **error_message;
1041 UN_IMPL("ADDR32NB");
1042 DUMP_RELOC("ADDR32NB",reloc_entry);
1044 return bfd_reloc_ok;
1047 static bfd_reloc_status_type
1048 ppc_secrel_reloc (abfd,
1056 arelent *reloc_entry;
1059 asection *input_section;
1061 char **error_message;
1064 DUMP_RELOC("SECREL",reloc_entry);
1066 if (output_bfd == (bfd *) NULL)
1067 return bfd_reloc_continue;
1069 return bfd_reloc_ok;
1072 static bfd_reloc_status_type
1073 ppc_section_reloc (abfd,
1081 arelent *reloc_entry;
1084 asection *input_section;
1086 char **error_message;
1089 DUMP_RELOC("SECTION",reloc_entry);
1091 if (output_bfd == (bfd *) NULL)
1092 return bfd_reloc_continue;
1094 return bfd_reloc_ok;
1097 static bfd_reloc_status_type
1098 ppc_imglue_reloc (abfd,
1106 arelent *reloc_entry;
1109 asection *input_section;
1111 char **error_message;
1114 DUMP_RELOC("IMGLUE",reloc_entry);
1116 if (output_bfd == (bfd *) NULL)
1117 return bfd_reloc_continue;
1119 return bfd_reloc_ok;
1124 #define MAX_RELOC_INDEX \
1125 (sizeof(ppc_coff_howto_table) / sizeof(ppc_coff_howto_table[0]) - 1)
1128 /* FIXME: There is a possiblity that when we read in a reloc from a file,
1129 that there are some bits encoded in the upper portion of the
1130 type field. Not yet implemented.
1132 static void ppc_coff_rtype2howto PARAMS ((arelent *relent,
1133 struct internal_reloc *internal));
1136 ppc_coff_rtype2howto (relent, internal)
1138 struct internal_reloc *internal;
1141 /* We can encode one of three things in the type field, aside from the
1143 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1144 value, rather than an addition value
1145 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1146 the branch is expected to be taken or not.
1147 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1148 For now, we just strip this stuff to find the type, and ignore it other
1152 unsigned short r_type = EXTRACT_TYPE (internal->r_type);
1153 unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
1154 unsigned short junk = EXTRACT_JUNK (internal->r_type);
1156 /* the masking process only slices off the bottom byte for r_type. */
1157 if ( r_type > MAX_RELOC_INDEX )
1160 "ppc_coff_rtype2howto: reloc index %d out of range [%d, %d]\n",
1161 internal->r_type, 0, MAX_RELOC_INDEX);
1165 /* check for absolute crap */
1169 "ppc_coff_rtype2howto: reloc index %d contains junk %d\n",
1170 internal->r_type, junk);
1175 /* now examine flags */
1178 fprintf (stderr, "Reloc with flags found!");
1179 if ( r_flags & IMAGE_REL_PPC_NEG )
1180 fprintf (stderr, " NEG");
1181 if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
1182 fprintf (stderr, " BRTAKEN");
1183 if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
1184 fprintf (stderr, " BRNTAKEN");
1185 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
1186 fprintf (stderr, " TOCDEFN");
1187 fprintf(stderr, "\n");
1193 case IMAGE_REL_PPC_ADDR16:
1194 case IMAGE_REL_PPC_REL24:
1195 case IMAGE_REL_PPC_ADDR24:
1196 case IMAGE_REL_PPC_TOCREL16:
1197 case IMAGE_REL_PPC_ADDR32:
1198 case IMAGE_REL_PPC_IFGLUE:
1199 case IMAGE_REL_PPC_ADDR32NB:
1200 case IMAGE_REL_PPC_SECTION:
1201 case IMAGE_REL_PPC_SECREL:
1202 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
1204 case IMAGE_REL_PPC_IMGLUE:
1205 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
1206 /* IMGLUE relocs have big numbers in them. Don't know what for yet. */
1207 internal->r_vaddr = 0; /* make it zero for now */
1211 "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
1212 ppc_coff_howto_table[r_type].name,
1217 relent->howto = ppc_coff_howto_table + r_type;
1221 static reloc_howto_type *
1222 coff_ppc_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
1225 struct internal_reloc *rel;
1226 struct coff_link_hash_entry *h;
1227 struct internal_syment *sym;
1230 reloc_howto_type *howto;
1232 /* We can encode one of three things in the type field, aside from the
1234 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1235 value, rather than an addition value
1236 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1237 the branch is expected to be taken or not.
1238 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1239 For now, we just strip this stuff to find the type, and ignore it other
1243 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
1244 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
1245 unsigned short junk = EXTRACT_JUNK (rel->r_type);
1247 /* the masking process only slices off the bottom byte for r_type. */
1248 if ( r_type > MAX_RELOC_INDEX )
1251 "coff_ppc_rtype_to_howto: index %d out of range [%d, %d]\n",
1252 r_type, 0, MAX_RELOC_INDEX);
1256 /* check for absolute crap */
1260 "coff_ppc_rtype_to_howto: reloc index %d contains junk %d\n",
1266 /* now examine flags */
1269 fprintf (stderr, "Reloc with flags found!");
1270 if ( r_flags & IMAGE_REL_PPC_NEG )
1271 fprintf (stderr, " NEG");
1272 if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
1273 fprintf (stderr, " BRTAKEN");
1274 if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
1275 fprintf (stderr, " BRNTAKEN");
1276 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
1277 fprintf (stderr, " TOCDEFN");
1278 fprintf(stderr, "\n");
1284 case IMAGE_REL_PPC_ADDR32NB:
1285 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1286 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
1288 case IMAGE_REL_PPC_ADDR16:
1289 case IMAGE_REL_PPC_REL24:
1290 case IMAGE_REL_PPC_ADDR24:
1291 case IMAGE_REL_PPC_TOCREL16:
1292 case IMAGE_REL_PPC_ADDR32:
1293 case IMAGE_REL_PPC_IFGLUE:
1294 case IMAGE_REL_PPC_SECTION:
1295 case IMAGE_REL_PPC_SECREL:
1296 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1298 case IMAGE_REL_PPC_IMGLUE:
1299 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1300 /* IMGLUE relocs have big numbers in them. Don't know what for yet. */
1301 rel->r_vaddr = 0; /* make it zero for now */
1305 "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
1306 ppc_coff_howto_table[r_type].name,
1311 howto = ppc_coff_howto_table + r_type;
1316 /* a cheesy little macro to make the code a little more readable */
1317 #define HOW2MAP(bfd_rtype,ppc_rtype) \
1318 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
1320 static reloc_howto_type *ppc_coff_reloc_type_lookup
1321 PARAMS ((bfd *, bfd_reloc_code_real_type));
1323 static reloc_howto_type *
1324 ppc_coff_reloc_type_lookup (abfd, code)
1326 bfd_reloc_code_real_type code;
1330 fprintf(stderr, "ppc_coff_reloc_type_lookup for %s\n",
1331 bfd_get_reloc_code_name(code));
1336 HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
1337 HOW2MAP(BFD_RELOC_16, IMAGE_REL_PPC_ADDR16);
1338 HOW2MAP(BFD_RELOC_PPC_B26, IMAGE_REL_PPC_REL24);
1339 HOW2MAP(BFD_RELOC_PPC_BA26, IMAGE_REL_PPC_ADDR24);
1340 HOW2MAP(BFD_RELOC_PPC_TOC16, IMAGE_REL_PPC_TOCREL16);
1341 HOW2MAP(BFD_RELOC_32, IMAGE_REL_PPC_ADDR32);
1342 default: return NULL;
1351 /* Tailor coffcode.h -- macro heaven. */
1353 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
1355 /* We use the special COFF backend linker. */
1356 /* #define coff_relocate_section _bfd_coff_generic_relocate_section */
1357 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
1358 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
1359 #define coff_relocate_section coff_ppc_relocate_section
1361 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
1363 #define COFF_PAGE_SIZE 0x1000
1365 #include "coffcode.h"
1367 /* The transfer vectors that lead the outside world to all of the above. */
1369 #ifdef TARGET_LITTLE_SYM
1373 TARGET_LITTLE_NAME, /* name or coff-arm-little */
1374 bfd_target_coff_flavour,
1375 false, /* data byte order is little */
1376 false, /* header byte order is little */
1378 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
1379 HAS_LINENO | HAS_DEBUG |
1380 HAS_SYMS | HAS_LOCALS | WP_TEXT),
1382 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1383 0, /* leading char */
1384 '/', /* ar_pad_char */
1385 15, /* ar_max_namelen??? FIXMEmgo */
1387 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1388 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1389 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
1390 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1391 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1392 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
1394 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
1395 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
1396 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
1398 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
1399 _bfd_write_archive_contents, bfd_false},
1401 BFD_JUMP_TABLE_GENERIC (coff),
1402 BFD_JUMP_TABLE_COPY (coff),
1403 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1404 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
1405 BFD_JUMP_TABLE_SYMBOLS (coff),
1406 BFD_JUMP_TABLE_RELOCS (coff),
1407 BFD_JUMP_TABLE_WRITE (coff),
1408 BFD_JUMP_TABLE_LINK (coff),
1409 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1415 #ifdef TARGET_BIG_SYM
1420 bfd_target_coff_flavour,
1421 true, /* data byte order is big */
1422 true, /* header byte order is big */
1424 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
1425 HAS_LINENO | HAS_DEBUG |
1426 HAS_SYMS | HAS_LOCALS | WP_TEXT),
1428 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1429 0, /* leading char */
1430 '/', /* ar_pad_char */
1431 15, /* ar_max_namelen??? FIXMEmgo */
1433 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1434 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1435 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1436 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1437 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1438 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1440 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
1441 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
1442 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
1444 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
1445 _bfd_write_archive_contents, bfd_false},
1447 BFD_JUMP_TABLE_GENERIC (coff),
1448 BFD_JUMP_TABLE_COPY (coff),
1449 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1450 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
1451 BFD_JUMP_TABLE_SYMBOLS (coff),
1452 BFD_JUMP_TABLE_RELOCS (coff),
1453 BFD_JUMP_TABLE_WRITE (coff),
1454 BFD_JUMP_TABLE_LINK (coff),
1455 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),