1 /* BFD back-end for ARM COFF files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 #include "coff/internal.h"
37 /* Macros for manipulation the bits in the flags field of the coff data
39 #define APCS_26_FLAG(abfd) \
40 (coff_data (abfd)->flags & F_APCS_26)
42 #define APCS_FLOAT_FLAG(abfd) \
43 (coff_data (abfd)->flags & F_APCS_FLOAT)
45 #define PIC_FLAG(abfd) \
46 (coff_data (abfd)->flags & F_PIC)
48 #define APCS_SET(abfd) \
49 (coff_data (abfd)->flags & F_APCS_SET)
51 #define SET_APCS_FLAGS(abfd, flgs) \
54 coff_data (abfd)->flags &= ~(F_APCS_26 | F_APCS_FLOAT | F_PIC); \
55 coff_data (abfd)->flags |= (flgs) | F_APCS_SET; \
59 #define INTERWORK_FLAG(abfd) \
60 (coff_data (abfd)->flags & F_INTERWORK)
62 #define INTERWORK_SET(abfd) \
63 (coff_data (abfd)->flags & F_INTERWORK_SET)
65 #define SET_INTERWORK_FLAG(abfd, flg) \
68 coff_data (abfd)->flags &= ~F_INTERWORK; \
69 coff_data (abfd)->flags |= (flg) | F_INTERWORK_SET; \
74 #define NUM_ELEM(a) ((sizeof (a)) / sizeof ((a)[0]))
77 typedef enum {bunknown, b9, b12, b23} thumb_pcrel_branchtype;
78 /* some typedefs for holding instructions */
79 typedef unsigned long int insn32;
80 typedef unsigned short int insn16;
82 /* Forward declarations for stupid compilers. */
83 static boolean coff_arm_relocate_section
84 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
85 struct internal_reloc *, struct internal_syment *, asection **));
86 static bfd_reloc_status_type aoutarm_fix_pcrel_26_done
87 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
88 static bfd_reloc_status_type aoutarm_fix_pcrel_26
89 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
90 static bfd_reloc_status_type coff_thumb_pcrel_23
91 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
92 static bfd_reloc_status_type coff_thumb_pcrel_12
93 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
94 static bfd_reloc_status_type coff_thumb_pcrel_9
95 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
96 static bfd_reloc_status_type coff_arm_reloc
97 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
98 static boolean coff_arm_adjust_symndx
99 PARAMS ((bfd *, struct bfd_link_info *, bfd *,
100 asection *, struct internal_reloc *, boolean *));
101 static reloc_howto_type * coff_arm_rtype_to_howto
102 PARAMS ((bfd *, asection *, struct internal_reloc *,
103 struct coff_link_hash_entry *, struct internal_syment *,
105 static bfd_reloc_status_type coff_thumb_pcrel_common
106 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **,
107 thumb_pcrel_branchtype));
108 static const struct reloc_howto_struct * coff_arm_reloc_type_lookup
109 PARAMS ((bfd *, bfd_reloc_code_real_type));
110 static struct bfd_link_hash_table * coff_arm_link_hash_table_create
112 static insn32 insert_thumb_branch
113 PARAMS ((insn32, int));
114 static struct coff_link_hash_entry * find_thumb_glue
115 PARAMS ((struct bfd_link_info *, const char *, bfd *));
116 static struct coff_link_hash_entry * find_arm_glue
117 PARAMS ((struct bfd_link_info *, const char *, bfd *));
118 #ifndef COFF_IMAGE_WITH_PE
119 static void record_arm_to_thumb_glue
120 PARAMS ((struct bfd_link_info *, struct coff_link_hash_entry *));
121 static void record_thumb_to_arm_glue
122 PARAMS ((struct bfd_link_info *, struct coff_link_hash_entry *));
124 static boolean coff_arm_merge_private_bfd_data
125 PARAMS ((bfd *, bfd *));
126 static boolean coff_arm_print_private_bfd_data
127 PARAMS ((bfd *, PTR));
128 static boolean _bfd_coff_arm_set_private_flags
129 PARAMS ((bfd *, flagword));
130 static boolean coff_arm_copy_private_bfd_data
131 PARAMS ((bfd *, bfd *));
132 static boolean coff_arm_is_local_label_name
133 PARAMS ((bfd *, const char *));
134 static boolean coff_arm_link_output_has_begun
135 PARAMS ((bfd *, struct coff_final_link_info *));
136 static boolean coff_arm_final_link_postscript
137 PARAMS ((bfd *, struct coff_final_link_info *));
138 static void arm_emit_base_file_entry
139 PARAMS ((struct bfd_link_info *, bfd *, asection *, bfd_vma));
141 /* The linker script knows the section names for placement.
142 The entry_names are used to do simple name mangling on the stubs.
143 Given a function name, and its type, the stub can be found. The
144 name can be changed. The only requirement is the %s be present. */
146 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
147 #define THUMB2ARM_GLUE_ENTRY_NAME "__%s_from_thumb"
149 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
150 #define ARM2THUMB_GLUE_ENTRY_NAME "__%s_from_arm"
152 /* Used by the assembler. */
154 static bfd_reloc_status_type
155 coff_arm_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
158 arelent *reloc_entry;
159 asymbol *symbol ATTRIBUTE_UNUSED;
161 asection *input_section ATTRIBUTE_UNUSED;
163 char **error_message ATTRIBUTE_UNUSED;
166 if (output_bfd == (bfd *) NULL)
167 return bfd_reloc_continue;
169 diff = reloc_entry->addend;
172 x = ((x & ~howto->dst_mask) \
173 | (((x & howto->src_mask) + diff) & howto->dst_mask))
177 reloc_howto_type *howto = reloc_entry->howto;
178 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
184 char x = bfd_get_8 (abfd, addr);
186 bfd_put_8 (abfd, x, addr);
192 short x = bfd_get_16 (abfd, addr);
194 bfd_put_16 (abfd, (bfd_vma) x, addr);
200 long x = bfd_get_32 (abfd, addr);
202 bfd_put_32 (abfd, (bfd_vma) x, addr);
211 /* Now let bfd_perform_relocation finish everything up. */
212 return bfd_reloc_continue;
215 /* If USER_LABEL_PREFIX is defined as "_" (see coff_arm_is_local_label_name()
216 in this file), then TARGET_UNDERSCORE should be defined, otherwise it
218 #ifndef TARGET_UNDERSCORE
219 #define TARGET_UNDERSCORE '_'
223 #define PCRELOFFSET true
226 /* These most certainly belong somewhere else. Just had to get rid of
227 the manifest constants in the code. */
240 #define ARM_THUMB9 12
241 #define ARM_THUMB12 13
242 #define ARM_THUMB23 14
254 #define ARM_THUMB12 4
256 #define ARM_SECTION 14
257 #define ARM_SECREL 15
260 static reloc_howto_type aoutarm_std_reloc_howto[] =
270 complain_overflow_bitfield,
283 complain_overflow_bitfield,
296 complain_overflow_signed,
297 aoutarm_fix_pcrel_26 ,
309 complain_overflow_signed,
310 coff_thumb_pcrel_12 ,
322 complain_overflow_dont,
323 aoutarm_fix_pcrel_26_done,
343 complain_overflow_bitfield,
356 complain_overflow_bitfield,
363 #else /* not ARM_WINCE */
364 HOWTO (ARM_8, /* type */
368 false, /* pc_relative */
370 complain_overflow_bitfield, /* complain_on_overflow */
371 coff_arm_reloc, /* special_function */
373 true, /* partial_inplace */
374 0x000000ff, /* src_mask */
375 0x000000ff, /* dst_mask */
376 PCRELOFFSET /* pcrel_offset */),
383 complain_overflow_bitfield,
396 complain_overflow_bitfield,
409 complain_overflow_signed,
410 aoutarm_fix_pcrel_26 ,
422 complain_overflow_signed,
435 complain_overflow_signed,
448 complain_overflow_signed,
461 complain_overflow_dont,
462 aoutarm_fix_pcrel_26_done,
476 complain_overflow_bitfield,
489 complain_overflow_bitfield,
502 complain_overflow_bitfield,
515 complain_overflow_signed,
528 complain_overflow_signed,
529 coff_thumb_pcrel_12 ,
541 complain_overflow_signed,
542 coff_thumb_pcrel_23 ,
548 #endif /* not ARM_WINCE */
551 #define NUM_RELOCS NUM_ELEM (aoutarm_std_reloc_howto)
554 static boolean in_reloc_p PARAMS ((bfd *, reloc_howto_type *));
555 /* Return true if this relocation should
556 appear in the output .reloc section. */
559 in_reloc_p (abfd, howto)
560 bfd * abfd ATTRIBUTE_UNUSED;
561 reloc_howto_type * howto;
563 return !howto->pc_relative && howto->type != ARM_RVA32;
567 #define RTYPE2HOWTO(cache_ptr, dst) \
568 (cache_ptr)->howto = \
569 (dst)->r_type < NUM_RELOCS \
570 ? aoutarm_std_reloc_howto + (dst)->r_type \
573 #define coff_rtype_to_howto coff_arm_rtype_to_howto
575 static reloc_howto_type *
576 coff_arm_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
577 bfd *abfd ATTRIBUTE_UNUSED;
579 struct internal_reloc *rel;
580 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED;
581 struct internal_syment *sym ATTRIBUTE_UNUSED;
584 reloc_howto_type * howto;
586 if (rel->r_type >= NUM_RELOCS)
589 howto = aoutarm_std_reloc_howto + rel->r_type;
591 if (rel->r_type == ARM_RVA32)
592 *addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
597 /* Used by the assembler. */
599 static bfd_reloc_status_type
600 aoutarm_fix_pcrel_26_done (abfd, reloc_entry, symbol, data, input_section,
601 output_bfd, error_message)
602 bfd *abfd ATTRIBUTE_UNUSED;
603 arelent *reloc_entry ATTRIBUTE_UNUSED;
604 asymbol *symbol ATTRIBUTE_UNUSED;
605 PTR data ATTRIBUTE_UNUSED;
606 asection *input_section ATTRIBUTE_UNUSED;
607 bfd *output_bfd ATTRIBUTE_UNUSED;
608 char **error_message ATTRIBUTE_UNUSED;
610 /* This is dead simple at present. */
614 /* Used by the assembler. */
616 static bfd_reloc_status_type
617 aoutarm_fix_pcrel_26 (abfd, reloc_entry, symbol, data, input_section,
618 output_bfd, error_message)
620 arelent *reloc_entry;
623 asection *input_section;
625 char **error_message ATTRIBUTE_UNUSED;
628 bfd_size_type addr = reloc_entry->address;
629 long target = bfd_get_32 (abfd, (bfd_byte *) data + addr);
630 bfd_reloc_status_type flag = bfd_reloc_ok;
632 /* If this is an undefined symbol, return error. */
633 if (symbol->section == &bfd_und_section
634 && (symbol->flags & BSF_WEAK) == 0)
635 return output_bfd ? bfd_reloc_continue : bfd_reloc_undefined;
637 /* If the sections are different, and we are doing a partial relocation,
638 just ignore it for now. */
639 if (symbol->section->name != input_section->name
640 && output_bfd != (bfd *)NULL)
641 return bfd_reloc_continue;
643 relocation = (target & 0x00ffffff) << 2;
644 relocation = (relocation ^ 0x02000000) - 0x02000000; /* Sign extend. */
645 relocation += symbol->value;
646 relocation += symbol->section->output_section->vma;
647 relocation += symbol->section->output_offset;
648 relocation += reloc_entry->addend;
649 relocation -= input_section->output_section->vma;
650 relocation -= input_section->output_offset;
654 return bfd_reloc_overflow;
656 /* Check for overflow. */
657 if (relocation & 0x02000000)
659 if ((relocation & ~ (bfd_vma) 0x03ffffff) != ~ (bfd_vma) 0x03ffffff)
660 flag = bfd_reloc_overflow;
662 else if (relocation & ~(bfd_vma) 0x03ffffff)
663 flag = bfd_reloc_overflow;
665 target &= ~0x00ffffff;
666 target |= (relocation >> 2) & 0x00ffffff;
667 bfd_put_32 (abfd, (bfd_vma) target, (bfd_byte *) data + addr);
669 /* Now the ARM magic... Change the reloc type so that it is marked as done.
670 Strictly this is only necessary if we are doing a partial relocation. */
671 reloc_entry->howto = &aoutarm_std_reloc_howto[ARM_26D];
676 static bfd_reloc_status_type
677 coff_thumb_pcrel_common (abfd, reloc_entry, symbol, data, input_section,
678 output_bfd, error_message, btype)
680 arelent *reloc_entry;
683 asection *input_section;
685 char **error_message ATTRIBUTE_UNUSED;
686 thumb_pcrel_branchtype btype;
688 bfd_vma relocation = 0;
689 bfd_size_type addr = reloc_entry->address;
690 long target = bfd_get_32 (abfd, (bfd_byte *) data + addr);
691 bfd_reloc_status_type flag = bfd_reloc_ok;
696 /* NOTE: This routine is currently used by GAS, but not by the link
704 signbit = 0x00000100;
710 signbit = 0x00000800;
716 signbit = 0x00400000;
723 /* If this is an undefined symbol, return error. */
724 if (symbol->section == &bfd_und_section
725 && (symbol->flags & BSF_WEAK) == 0)
726 return output_bfd ? bfd_reloc_continue : bfd_reloc_undefined;
728 /* If the sections are different, and we are doing a partial relocation,
729 just ignore it for now. */
730 if (symbol->section->name != input_section->name
731 && output_bfd != (bfd *)NULL)
732 return bfd_reloc_continue;
738 relocation = ((target & dstmsk) << 1);
742 if (bfd_big_endian (abfd))
743 relocation = ((target & 0x7ff) << 1) | ((target & 0x07ff0000) >> 4);
745 relocation = ((target & 0x7ff) << 12) | ((target & 0x07ff0000) >> 15);
752 relocation = (relocation ^ signbit) - signbit; /* Sign extend. */
753 relocation += symbol->value;
754 relocation += symbol->section->output_section->vma;
755 relocation += symbol->section->output_offset;
756 relocation += reloc_entry->addend;
757 relocation -= input_section->output_section->vma;
758 relocation -= input_section->output_offset;
762 return bfd_reloc_overflow;
764 /* Check for overflow. */
765 if (relocation & signbit)
767 if ((relocation & ~offmsk) != ~offmsk)
768 flag = bfd_reloc_overflow;
770 else if (relocation & ~offmsk)
771 flag = bfd_reloc_overflow;
778 target |= (relocation >> 1);
782 if (bfd_big_endian (abfd))
783 target |= (((relocation & 0xfff) >> 1)
784 | ((relocation << 4) & 0x07ff0000));
786 target |= (((relocation & 0xffe) << 15)
787 | ((relocation >> 12) & 0x7ff));
794 bfd_put_32 (abfd, (bfd_vma) target, (bfd_byte *) data + addr);
796 /* Now the ARM magic... Change the reloc type so that it is marked as done.
797 Strictly this is only necessary if we are doing a partial relocation. */
798 reloc_entry->howto = & aoutarm_std_reloc_howto [ARM_26D];
800 /* TODO: We should possibly have DONE entries for the THUMB PCREL relocations. */
804 static bfd_reloc_status_type
805 coff_thumb_pcrel_23 (abfd, reloc_entry, symbol, data, input_section,
806 output_bfd, error_message)
808 arelent *reloc_entry;
811 asection *input_section;
813 char **error_message;
815 return coff_thumb_pcrel_common (abfd, reloc_entry, symbol, data,
816 input_section, output_bfd, error_message,
820 static bfd_reloc_status_type
821 coff_thumb_pcrel_12 (abfd, reloc_entry, symbol, data, input_section,
822 output_bfd, error_message)
824 arelent *reloc_entry;
827 asection *input_section;
829 char **error_message;
831 return coff_thumb_pcrel_common (abfd, reloc_entry, symbol, data,
832 input_section, output_bfd, error_message,
836 static bfd_reloc_status_type
837 coff_thumb_pcrel_9 (abfd, reloc_entry, symbol, data, input_section,
838 output_bfd, error_message)
840 arelent *reloc_entry;
843 asection *input_section;
845 char **error_message;
847 return coff_thumb_pcrel_common (abfd, reloc_entry, symbol, data,
848 input_section, output_bfd, error_message,
852 static const struct reloc_howto_struct *
853 coff_arm_reloc_type_lookup (abfd, code)
855 bfd_reloc_code_real_type code;
857 #define ASTD(i,j) case i: return aoutarm_std_reloc_howto + j
859 if (code == BFD_RELOC_CTOR)
860 switch (bfd_get_arch_info (abfd)->bits_per_address)
866 return (const struct reloc_howto_struct *) 0;
872 ASTD (BFD_RELOC_32, ARM_32);
873 ASTD (BFD_RELOC_RVA, ARM_RVA32);
874 ASTD (BFD_RELOC_ARM_PCREL_BRANCH, ARM_26);
875 ASTD (BFD_RELOC_THUMB_PCREL_BRANCH12, ARM_THUMB12);
877 ASTD (BFD_RELOC_8, ARM_8);
878 ASTD (BFD_RELOC_16, ARM_16);
879 ASTD (BFD_RELOC_32, ARM_32);
880 ASTD (BFD_RELOC_ARM_PCREL_BRANCH, ARM_26);
881 ASTD (BFD_RELOC_ARM_PCREL_BLX, ARM_26);
882 ASTD (BFD_RELOC_8_PCREL, ARM_DISP8);
883 ASTD (BFD_RELOC_16_PCREL, ARM_DISP16);
884 ASTD (BFD_RELOC_32_PCREL, ARM_DISP32);
885 ASTD (BFD_RELOC_RVA, ARM_RVA32);
886 ASTD (BFD_RELOC_THUMB_PCREL_BRANCH9, ARM_THUMB9);
887 ASTD (BFD_RELOC_THUMB_PCREL_BRANCH12, ARM_THUMB12);
888 ASTD (BFD_RELOC_THUMB_PCREL_BRANCH23, ARM_THUMB23);
889 ASTD (BFD_RELOC_THUMB_PCREL_BLX, ARM_THUMB23);
891 default: return (const struct reloc_howto_struct *) 0;
895 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
896 #define COFF_PAGE_SIZE 0x1000
897 /* Turn a howto into a reloc nunmber */
899 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
900 #define BADMAG(x) ARMBADMAG(x)
901 #define ARM 1 /* Customize coffcode.h */
903 /* Make sure that the 'r_offset' field is copied properly
904 so that identical binaries will compare the same. */
905 #define SWAP_IN_RELOC_OFFSET H_GET_32
906 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
908 /* Extend the coff_link_hash_table structure with a few ARM specific fields.
909 This allows us to store global data here without actually creating any
910 global variables, which is a no-no in the BFD world. */
911 struct coff_arm_link_hash_table
913 /* The original coff_link_hash_table structure. MUST be first field. */
914 struct coff_link_hash_table root;
916 /* The size in bytes of the section containg the Thumb-to-ARM glue. */
917 bfd_size_type thumb_glue_size;
919 /* The size in bytes of the section containg the ARM-to-Thumb glue. */
920 bfd_size_type arm_glue_size;
922 /* An arbitary input BFD chosen to hold the glue sections. */
923 bfd * bfd_of_glue_owner;
925 /* Support interworking with old, non-interworking aware ARM code. */
926 int support_old_code;
929 /* Get the ARM coff linker hash table from a link_info structure. */
930 #define coff_arm_hash_table(info) \
931 ((struct coff_arm_link_hash_table *) ((info)->hash))
933 /* Create an ARM coff linker hash table. */
935 static struct bfd_link_hash_table *
936 coff_arm_link_hash_table_create (abfd)
939 struct coff_arm_link_hash_table * ret;
940 bfd_size_type amt = sizeof (struct coff_arm_link_hash_table);
942 ret = (struct coff_arm_link_hash_table *) bfd_malloc (amt);
943 if (ret == (struct coff_arm_link_hash_table *) NULL)
946 if (! _bfd_coff_link_hash_table_init
947 (& ret->root, abfd, _bfd_coff_link_hash_newfunc))
950 return (struct bfd_link_hash_table *) NULL;
953 ret->thumb_glue_size = 0;
954 ret->arm_glue_size = 0;
955 ret->bfd_of_glue_owner = NULL;
957 return & ret->root.root;
961 arm_emit_base_file_entry (info, output_bfd, input_section, reloc_offset)
962 struct bfd_link_info *info;
964 asection *input_section;
965 bfd_vma reloc_offset;
967 bfd_vma addr = reloc_offset
969 + input_section->output_offset
970 + input_section->output_section->vma;
972 if (coff_data (output_bfd)->pe)
973 addr -= pe_data (output_bfd)->pe_opthdr.ImageBase;
974 fwrite (& addr, 1, sizeof (addr), (FILE *) info->base_file);
978 /* The thumb form of a long branch is a bit finicky, because the offset
979 encoding is split over two fields, each in it's own instruction. They
980 can occur in any order. So given a thumb form of long branch, and an
981 offset, insert the offset into the thumb branch and return finished
984 It takes two thumb instructions to encode the target address. Each has
985 11 bits to invest. The upper 11 bits are stored in one (identifed by
986 H-0.. see below), the lower 11 bits are stored in the other (identified
989 Combine together and shifted left by 1 (it's a half word address) and
993 H-0, upper address-0 = 000
995 H-1, lower address-0 = 800
997 They can be ordered either way, but the arm tools I've seen always put
998 the lower one first. It probably doesn't matter. krk@cygnus.com
1000 XXX: Actually the order does matter. The second instruction (H-1)
1001 moves the computed address into the PC, so it must be the second one
1002 in the sequence. The problem, however is that whilst little endian code
1003 stores the instructions in HI then LOW order, big endian code does the
1004 reverse. nickc@cygnus.com. */
1006 #define LOW_HI_ORDER 0xF800F000
1007 #define HI_LOW_ORDER 0xF000F800
1010 insert_thumb_branch (br_insn, rel_off)
1014 unsigned int low_bits;
1015 unsigned int high_bits;
1017 BFD_ASSERT((rel_off & 1) != 1);
1019 rel_off >>= 1; /* half word aligned address */
1020 low_bits = rel_off & 0x000007FF; /* the bottom 11 bits */
1021 high_bits = (rel_off >> 11) & 0x000007FF; /* the top 11 bits */
1023 if ((br_insn & LOW_HI_ORDER) == LOW_HI_ORDER)
1024 br_insn = LOW_HI_ORDER | (low_bits << 16) | high_bits;
1025 else if ((br_insn & HI_LOW_ORDER) == HI_LOW_ORDER)
1026 br_insn = HI_LOW_ORDER | (high_bits << 16) | low_bits;
1028 /* FIXME: the BFD library should never abort except for internal errors
1029 - it should return an error status. */
1030 abort (); /* Error - not a valid branch instruction form. */
1035 static struct coff_link_hash_entry *
1036 find_thumb_glue (info, name, input_bfd)
1037 struct bfd_link_info *info;
1042 struct coff_link_hash_entry *myh;
1043 bfd_size_type amt = strlen (name) + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1;
1045 tmp_name = (char *) bfd_malloc (amt);
1047 BFD_ASSERT (tmp_name);
1049 sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
1051 myh = coff_link_hash_lookup
1052 (coff_hash_table (info), tmp_name, false, false, true);
1055 /* xgettext:c-format */
1056 _bfd_error_handler (_("%s: unable to find THUMB glue '%s' for `%s'"),
1057 bfd_archive_filename (input_bfd), tmp_name, name);
1064 static struct coff_link_hash_entry *
1065 find_arm_glue (info, name, input_bfd)
1066 struct bfd_link_info *info;
1071 struct coff_link_hash_entry * myh;
1072 bfd_size_type amt = strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1;
1074 tmp_name = (char *) bfd_malloc (amt);
1076 BFD_ASSERT (tmp_name);
1078 sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
1080 myh = coff_link_hash_lookup
1081 (coff_hash_table (info), tmp_name, false, false, true);
1084 /* xgettext:c-format */
1085 _bfd_error_handler (_("%s: unable to find ARM glue '%s' for `%s'"),
1086 bfd_archive_filename (input_bfd), tmp_name, name);
1098 ldr r12, __func_addr
1101 .word func @ behave as if you saw a ARM_32 reloc
1104 #define ARM2THUMB_GLUE_SIZE 12
1105 static const insn32 a2t1_ldr_insn = 0xe59fc000;
1106 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
1107 static const insn32 a2t3_func_addr_insn = 0x00000001;
1110 Thumb->ARM: Thumb->(non-interworking aware) ARM
1114 __func_from_thumb: __func_from_thumb:
1116 nop ldr r6, __func_addr
1118 __func_change_to_arm: bx r6
1120 __func_back_to_thumb:
1127 #define THUMB2ARM_GLUE_SIZE (globals->support_old_code ? 20 : 8)
1128 static const insn16 t2a1_bx_pc_insn = 0x4778;
1129 static const insn16 t2a2_noop_insn = 0x46c0;
1130 static const insn32 t2a3_b_insn = 0xea000000;
1132 static const insn16 t2a1_push_insn = 0xb540;
1133 static const insn16 t2a2_ldr_insn = 0x4e03;
1134 static const insn16 t2a3_mov_insn = 0x46fe;
1135 static const insn16 t2a4_bx_insn = 0x4730;
1136 static const insn32 t2a5_pop_insn = 0xe8bd4040;
1137 static const insn32 t2a6_bx_insn = 0xe12fff1e;
1140 We should really create new local (static) symbols in destination
1141 object for each stub we create. We should also create local
1142 (static) symbols within the stubs when switching between ARM and
1143 Thumb code. This will ensure that the debugger and disassembler
1144 can present a better view of stubs.
1146 We can treat stubs like literal sections, and for the THUMB9 ones
1147 (short addressing range) we should be able to insert the stubs
1148 between sections. i.e. the simplest approach (since relocations
1149 are done on a section basis) is to dump the stubs at the end of
1150 processing a section. That way we can always try and minimise the
1151 offset to and from a stub. However, this does not map well onto
1152 the way that the linker/BFD does its work: mapping all input
1153 sections to output sections via the linker script before doing
1156 Unfortunately it may be easier to just to disallow short range
1157 Thumb->ARM stubs (i.e. no conditional inter-working branches,
1158 only branch-and-link (BL) calls. This will simplify the processing
1159 since we can then put all of the stubs into their own section.
1162 On a different subject, rather than complaining when a
1163 branch cannot fit in the number of bits available for the
1164 instruction we should generate a trampoline stub (needed to
1165 address the complete 32bit address space). */
1167 /* The standard COFF backend linker does not cope with the special
1168 Thumb BRANCH23 relocation. The alternative would be to split the
1169 BRANCH23 into seperate HI23 and LO23 relocations. However, it is a
1170 bit simpler simply providing our own relocation driver. */
1172 /* The reloc processing routine for the ARM/Thumb COFF linker. NOTE:
1173 This code is a very slightly modified copy of
1174 _bfd_coff_generic_relocate_section. It would be a much more
1175 maintainable solution to have a MACRO that could be expanded within
1176 _bfd_coff_generic_relocate_section that would only be provided for
1177 ARM/Thumb builds. It is only the code marked THUMBEXTENSION that
1178 is different from the original. */
1181 coff_arm_relocate_section (output_bfd, info, input_bfd, input_section,
1182 contents, relocs, syms, sections)
1184 struct bfd_link_info *info;
1186 asection *input_section;
1188 struct internal_reloc *relocs;
1189 struct internal_syment *syms;
1190 asection **sections;
1192 struct internal_reloc * rel;
1193 struct internal_reloc * relend;
1196 relend = rel + input_section->reloc_count;
1198 for (; rel < relend; rel++)
1202 struct coff_link_hash_entry * h;
1203 struct internal_syment * sym;
1206 reloc_howto_type * howto;
1207 bfd_reloc_status_type rstat;
1210 symndx = rel->r_symndx;
1219 h = obj_coff_sym_hashes (input_bfd)[symndx];
1220 sym = syms + symndx;
1223 /* COFF treats common symbols in one of two ways. Either the
1224 size of the symbol is included in the section contents, or it
1225 is not. We assume that the size is not included, and force
1226 the rtype_to_howto function to adjust the addend as needed. */
1228 if (sym != NULL && sym->n_scnum != 0)
1229 addend = - sym->n_value;
1233 howto = coff_rtype_to_howto (input_bfd, input_section, rel, h,
1238 /* The relocation_section function will skip pcrel_offset relocs
1239 when doing a relocateable link. However, we want to convert
1240 ARM26 to ARM26D relocs if possible. We return a fake howto in
1241 this case without pcrel_offset set, and adjust the addend to
1243 if (rel->r_type == ARM_26
1245 && info->relocateable
1246 && (h->root.type == bfd_link_hash_defined
1247 || h->root.type == bfd_link_hash_defweak)
1248 && (h->root.u.def.section->output_section
1249 == input_section->output_section))
1251 static reloc_howto_type fake_arm26_reloc =
1258 complain_overflow_signed,
1259 aoutarm_fix_pcrel_26 ,
1266 addend -= rel->r_vaddr - input_section->vma;
1267 howto = &fake_arm26_reloc;
1271 /* MS ARM-CE makes the reloc relative to the opcode's pc, not
1272 the next opcode's pc, so is off by one. */
1273 if (howto->pc_relative && !info->relocateable)
1277 /* If we are doing a relocateable link, then we can just ignore
1278 a PC relative reloc that is pcrel_offset. It will already
1279 have the correct value. If this is not a relocateable link,
1280 then we should ignore the symbol value. */
1281 if (howto->pc_relative && howto->pcrel_offset)
1283 if (info->relocateable)
1285 /* FIXME - it is not clear which targets need this next test
1286 and which do not. It is known that it is needed for the
1287 VxWorks and EPOC-PE targets, but it is also known that it
1288 was supressed for other ARM targets. This ought to be
1289 sorted out one day. */
1290 #ifdef ARM_COFF_BUGFIX
1291 /* We must not ignore the symbol value. If the symbol is
1292 within the same section, the relocation should have already
1293 been fixed, but if it is not, we'll be handed a reloc into
1294 the beginning of the symbol's section, so we must not cancel
1295 out the symbol's value, otherwise we'll be adding it in
1297 if (sym != NULL && sym->n_scnum != 0)
1298 addend += sym->n_value;
1310 sec = bfd_abs_section_ptr;
1315 sec = sections[symndx];
1316 val = (sec->output_section->vma
1317 + sec->output_offset
1324 #if 1 /* THUMBEXTENSION */
1325 /* We don't output the stubs if we are generating a
1326 relocatable output file, since we may as well leave the
1327 stub generation to the final linker pass. If we fail to
1328 verify that the name is defined, we'll try to build stubs
1329 for an undefined name... */
1330 if (! info->relocateable
1331 && ( h->root.type == bfd_link_hash_defined
1332 || h->root.type == bfd_link_hash_defweak))
1334 asection * h_sec = h->root.u.def.section;
1335 const char * name = h->root.root.string;
1337 /* h locates the symbol referenced in the reloc. */
1338 h_val = (h->root.u.def.value
1339 + h_sec->output_section->vma
1340 + h_sec->output_offset);
1342 if (howto->type == ARM_26)
1344 if ( h->class == C_THUMBSTATFUNC
1345 || h->class == C_THUMBEXTFUNC)
1347 /* Arm code calling a Thumb function. */
1348 unsigned long int tmp;
1351 long int ret_offset;
1352 struct coff_link_hash_entry * myh;
1353 struct coff_arm_link_hash_table * globals;
1355 myh = find_arm_glue (info, name, input_bfd);
1359 globals = coff_arm_hash_table (info);
1361 BFD_ASSERT (globals != NULL);
1362 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1364 my_offset = myh->root.u.def.value;
1366 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
1367 ARM2THUMB_GLUE_SECTION_NAME);
1368 BFD_ASSERT (s != NULL);
1369 BFD_ASSERT (s->contents != NULL);
1370 BFD_ASSERT (s->output_section != NULL);
1372 if ((my_offset & 0x01) == 0x01)
1374 if (h_sec->owner != NULL
1375 && INTERWORK_SET (h_sec->owner)
1376 && ! INTERWORK_FLAG (h_sec->owner))
1379 /* xgettext:c-format */
1380 (_("%s(%s): warning: interworking not enabled."),
1381 bfd_archive_filename (h_sec->owner), name);
1383 /* xgettext:c-format */
1384 (_(" first occurrence: %s: arm call to thumb"),
1385 bfd_archive_filename (input_bfd));
1389 myh->root.u.def.value = my_offset;
1391 bfd_put_32 (output_bfd, (bfd_vma) a2t1_ldr_insn,
1392 s->contents + my_offset);
1394 bfd_put_32 (output_bfd, (bfd_vma) a2t2_bx_r12_insn,
1395 s->contents + my_offset + 4);
1397 /* It's a thumb address. Add the low order bit. */
1398 bfd_put_32 (output_bfd, h_val | a2t3_func_addr_insn,
1399 s->contents + my_offset + 8);
1401 if (info->base_file)
1402 arm_emit_base_file_entry (info, output_bfd, s,
1407 BFD_ASSERT (my_offset <= globals->arm_glue_size);
1409 tmp = bfd_get_32 (input_bfd, contents + rel->r_vaddr
1410 - input_section->vma);
1412 tmp = tmp & 0xFF000000;
1414 /* Somehow these are both 4 too far, so subtract 8. */
1418 + s->output_section->vma
1419 - (input_section->output_offset
1420 + input_section->output_section->vma
1424 tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
1426 bfd_put_32 (output_bfd, (bfd_vma) tmp,
1427 contents + rel->r_vaddr - input_section->vma);
1433 /* Note: We used to check for ARM_THUMB9 and ARM_THUMB12. */
1434 else if (howto->type == ARM_THUMB23)
1436 if ( h->class == C_EXT
1437 || h->class == C_STAT
1438 || h->class == C_LABEL)
1440 /* Thumb code calling an ARM function */
1443 unsigned long int tmp;
1444 long int ret_offset;
1445 struct coff_link_hash_entry * myh;
1446 struct coff_arm_link_hash_table * globals;
1448 myh = find_thumb_glue (info, name, input_bfd);
1452 globals = coff_arm_hash_table (info);
1454 BFD_ASSERT (globals != NULL);
1455 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1457 my_offset = myh->root.u.def.value;
1459 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
1460 THUMB2ARM_GLUE_SECTION_NAME);
1462 BFD_ASSERT (s != NULL);
1463 BFD_ASSERT (s->contents != NULL);
1464 BFD_ASSERT (s->output_section != NULL);
1466 if ((my_offset & 0x01) == 0x01)
1468 if (h_sec->owner != NULL
1469 && INTERWORK_SET (h_sec->owner)
1470 && ! INTERWORK_FLAG (h_sec->owner)
1471 && ! globals->support_old_code)
1474 /* xgettext:c-format */
1475 (_("%s(%s): warning: interworking not enabled."),
1476 bfd_archive_filename (h_sec->owner), name);
1478 /* xgettext:c-format */
1479 (_(" first occurrence: %s: thumb call to arm"),
1480 bfd_archive_filename (input_bfd));
1482 (_(" consider relinking with --support-old-code enabled"));
1486 myh->root.u.def.value = my_offset;
1488 if (globals->support_old_code)
1490 bfd_put_16 (output_bfd, (bfd_vma) t2a1_push_insn,
1491 s->contents + my_offset);
1493 bfd_put_16 (output_bfd, (bfd_vma) t2a2_ldr_insn,
1494 s->contents + my_offset + 2);
1496 bfd_put_16 (output_bfd, (bfd_vma) t2a3_mov_insn,
1497 s->contents + my_offset + 4);
1499 bfd_put_16 (output_bfd, (bfd_vma) t2a4_bx_insn,
1500 s->contents + my_offset + 6);
1502 bfd_put_32 (output_bfd, (bfd_vma) t2a5_pop_insn,
1503 s->contents + my_offset + 8);
1505 bfd_put_32 (output_bfd, (bfd_vma) t2a6_bx_insn,
1506 s->contents + my_offset + 12);
1508 /* Store the address of the function in the last word of the stub. */
1509 bfd_put_32 (output_bfd, h_val,
1510 s->contents + my_offset + 16);
1512 if (info->base_file)
1513 arm_emit_base_file_entry (info, output_bfd, s,
1518 bfd_put_16 (output_bfd, (bfd_vma) t2a1_bx_pc_insn,
1519 s->contents + my_offset);
1521 bfd_put_16 (output_bfd, (bfd_vma) t2a2_noop_insn,
1522 s->contents + my_offset + 2);
1525 ((bfd_signed_vma) h_val) /* Address of destination of the stub. */
1527 (s->output_offset /* Offset from the start of the current section to the start of the stubs. */
1528 + my_offset /* Offset of the start of this stub from the start of the stubs. */
1529 + s->output_section->vma) /* Address of the start of the current section. */
1530 + 4 /* The branch instruction is 4 bytes into the stub. */
1531 + 8); /* ARM branches work from the pc of the instruction + 8. */
1533 bfd_put_32 (output_bfd,
1534 (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
1535 s->contents + my_offset + 4);
1540 BFD_ASSERT (my_offset <= globals->thumb_glue_size);
1542 /* Now go back and fix up the original BL insn to point
1547 - (input_section->output_offset
1551 tmp = bfd_get_32 (input_bfd, contents + rel->r_vaddr
1552 - input_section->vma);
1554 bfd_put_32 (output_bfd,
1555 (bfd_vma) insert_thumb_branch (tmp,
1557 contents + rel->r_vaddr - input_section->vma);
1565 /* If the relocation type and destination symbol does not
1566 fall into one of the above categories, then we can just
1567 perform a direct link. */
1570 rstat = bfd_reloc_ok;
1572 #endif /* THUMBEXTENSION */
1573 if ( h->root.type == bfd_link_hash_defined
1574 || h->root.type == bfd_link_hash_defweak)
1578 sec = h->root.u.def.section;
1579 val = (h->root.u.def.value
1580 + sec->output_section->vma
1581 + sec->output_offset);
1584 else if (! info->relocateable)
1586 if (! ((*info->callbacks->undefined_symbol)
1587 (info, h->root.root.string, input_bfd, input_section,
1588 rel->r_vaddr - input_section->vma, true)))
1593 if (info->base_file)
1595 /* Emit a reloc if the backend thinks it needs it. */
1596 if (sym && pe_data(output_bfd)->in_reloc_p(output_bfd, howto))
1597 arm_emit_base_file_entry (info, output_bfd, input_section,
1601 #if 1 /* THUMBEXTENSION */
1603 rstat = bfd_reloc_ok;
1605 /* Only perform this fix during the final link, not a relocatable link. nickc@cygnus.com */
1606 else if (! info->relocateable
1607 && howto->type == ARM_THUMB23)
1609 /* This is pretty much a copy of what the default
1610 _bfd_final_link_relocate and _bfd_relocate_contents
1611 routines do to perform a relocation, with special
1612 processing for the split addressing of the Thumb BL
1613 instruction. Again, it would probably be simpler adding a
1614 ThumbBRANCH23 specific macro expansion into the default
1617 bfd_vma address = rel->r_vaddr - input_section->vma;
1619 if (address > input_section->_raw_size)
1620 rstat = bfd_reloc_outofrange;
1623 bfd_vma relocation = val + addend;
1624 int size = bfd_get_reloc_size (howto);
1625 boolean overflow = false;
1626 bfd_byte * location = contents + address;
1627 bfd_vma x = bfd_get_32 (input_bfd, location);
1628 bfd_vma src_mask = 0x007FFFFE;
1629 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
1630 bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
1632 bfd_signed_vma signed_check;
1634 bfd_signed_vma signed_add;
1636 BFD_ASSERT (size == 4);
1638 /* howto->pc_relative should be TRUE for type 14 BRANCH23. */
1639 relocation -= (input_section->output_section->vma
1640 + input_section->output_offset);
1642 /* howto->pcrel_offset should be TRUE for type 14 BRANCH23. */
1643 relocation -= address;
1645 /* No need to negate the relocation with BRANCH23. */
1646 /* howto->complain_on_overflow == complain_overflow_signed for BRANCH23. */
1647 /* howto->rightshift == 1 */
1649 /* Drop unwanted bits from the value we are relocating to. */
1650 check = relocation >> howto->rightshift;
1652 /* If this is a signed value, the rightshift just dropped
1653 leading 1 bits (assuming twos complement). */
1654 if ((bfd_signed_vma) relocation >= 0)
1655 signed_check = check;
1657 signed_check = (check
1659 & ~((bfd_vma) - 1 >> howto->rightshift)));
1661 /* Get the value from the object file. */
1662 if (bfd_big_endian (input_bfd))
1663 add = (((x) & 0x07ff0000) >> 4) | (((x) & 0x7ff) << 1);
1665 add = ((((x) & 0x7ff) << 12) | (((x) & 0x07ff0000) >> 15));
1667 /* Get the value from the object file with an appropriate sign.
1668 The expression involving howto->src_mask isolates the upper
1669 bit of src_mask. If that bit is set in the value we are
1670 adding, it is negative, and we subtract out that number times
1671 two. If src_mask includes the highest possible bit, then we
1672 can not get the upper bit, but that does not matter since
1673 signed_add needs no adjustment to become negative in that
1677 if ((add & (((~ src_mask) >> 1) & src_mask)) != 0)
1678 signed_add -= (((~ src_mask) >> 1) & src_mask) << 1;
1680 /* howto->bitpos == 0 */
1681 /* Add the value from the object file, shifted so that it is a
1683 signed_check += signed_add;
1684 relocation += signed_add;
1686 BFD_ASSERT (howto->complain_on_overflow == complain_overflow_signed);
1688 /* Assumes two's complement. */
1689 if ( signed_check > reloc_signed_max
1690 || signed_check < reloc_signed_min)
1693 /* For the BLX(1) instruction remove bit 0 of the adjusted offset.
1694 Bit 0 can only be set if the upper insn is at a half-word boundary,
1695 since the destination address, an ARM instruction, must always be
1696 on a word boundary. The semantics of the BLX (1) instruction,
1697 however, are that bit 0 in the offset must always be 0, and the
1698 corresponding bit 1 in the target address will be set from bit
1699 1 of the source address. */
1700 if ((x & 0x18000000) == 0x08000000)
1703 /* Put the relocation into the correct bits. */
1704 if (bfd_big_endian (input_bfd))
1705 relocation = (((relocation & 0xffe) >> 1) | ((relocation << 4) & 0x07ff0000));
1707 relocation = (((relocation & 0xffe) << 15) | ((relocation >> 12) & 0x7ff));
1709 /* Add the relocation to the correct bits of X. */
1710 x = ((x & ~howto->dst_mask) | relocation);
1712 /* Put the relocated value back in the object file. */
1713 bfd_put_32 (input_bfd, x, location);
1715 rstat = overflow ? bfd_reloc_overflow : bfd_reloc_ok;
1720 #endif /* THUMBEXTENSION */
1721 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
1723 rel->r_vaddr - input_section->vma,
1725 #if 1 /* THUMBEXTENSION */
1727 Is this the best way to fix up thumb addresses? krk@cygnus.com
1728 Probably not, but it works, and if it works it don't need fixing! nickc@cygnus.com */
1729 /* Only perform this fix during the final link, not a relocatable link. nickc@cygnus.com */
1730 if (! info->relocateable
1731 && (rel->r_type == ARM_32 || rel->r_type == ARM_RVA32))
1733 /* Determine if we need to set the bottom bit of a relocated address
1734 because the address is the address of a Thumb code symbol. */
1736 int patchit = false;
1739 && ( h->class == C_THUMBSTATFUNC
1740 || h->class == C_THUMBEXTFUNC))
1744 else if (sym != NULL
1745 && sym->n_scnum > N_UNDEF)
1747 /* No hash entry - use the symbol instead. */
1749 if ( sym->n_sclass == C_THUMBSTATFUNC
1750 || sym->n_sclass == C_THUMBEXTFUNC)
1756 bfd_byte * location = contents + rel->r_vaddr - input_section->vma;
1757 bfd_vma x = bfd_get_32 (input_bfd, location);
1759 bfd_put_32 (input_bfd, x | 1, location);
1762 #endif /* THUMBEXTENSION */
1770 case bfd_reloc_outofrange:
1771 (*_bfd_error_handler)
1772 (_("%s: bad reloc address 0x%lx in section `%s'"),
1773 bfd_archive_filename (input_bfd),
1774 (unsigned long) rel->r_vaddr,
1775 bfd_get_section_name (input_bfd, input_section));
1777 case bfd_reloc_overflow:
1780 char buf[SYMNMLEN + 1];
1785 name = h->root.root.string;
1788 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
1793 if (! ((*info->callbacks->reloc_overflow)
1794 (info, name, howto->name, (bfd_vma) 0, input_bfd,
1795 input_section, rel->r_vaddr - input_section->vma)))
1804 #ifndef COFF_IMAGE_WITH_PE
1807 bfd_arm_allocate_interworking_sections (info)
1808 struct bfd_link_info * info;
1812 struct coff_arm_link_hash_table * globals;
1814 static char test_char = '1';
1817 globals = coff_arm_hash_table (info);
1819 BFD_ASSERT (globals != NULL);
1821 if (globals->arm_glue_size != 0)
1823 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1825 s = bfd_get_section_by_name
1826 (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
1828 BFD_ASSERT (s != NULL);
1830 foo = (bfd_byte *) bfd_alloc (globals->bfd_of_glue_owner,
1831 globals->arm_glue_size);
1833 memset (foo, test_char, (size_t) globals->arm_glue_size);
1836 s->_raw_size = s->_cooked_size = globals->arm_glue_size;
1840 if (globals->thumb_glue_size != 0)
1842 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1844 s = bfd_get_section_by_name
1845 (globals->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
1847 BFD_ASSERT (s != NULL);
1849 foo = (bfd_byte *) bfd_alloc (globals->bfd_of_glue_owner,
1850 globals->thumb_glue_size);
1852 memset (foo, test_char, (size_t) globals->thumb_glue_size);
1855 s->_raw_size = s->_cooked_size = globals->thumb_glue_size;
1863 record_arm_to_thumb_glue (info, h)
1864 struct bfd_link_info * info;
1865 struct coff_link_hash_entry * h;
1867 const char * name = h->root.root.string;
1868 register asection * s;
1870 struct coff_link_hash_entry * myh;
1871 struct coff_arm_link_hash_table * globals;
1875 globals = coff_arm_hash_table (info);
1877 BFD_ASSERT (globals != NULL);
1878 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1880 s = bfd_get_section_by_name
1881 (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
1883 BFD_ASSERT (s != NULL);
1885 amt = strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1;
1886 tmp_name = (char *) bfd_malloc (amt);
1888 BFD_ASSERT (tmp_name);
1890 sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
1892 myh = coff_link_hash_lookup
1893 (coff_hash_table (info), tmp_name, false, false, true);
1898 return; /* we've already seen this guy */
1901 /* The only trick here is using globals->arm_glue_size as the value. Even
1902 though the section isn't allocated yet, this is where we will be putting
1905 val = globals->arm_glue_size + 1;
1906 bfd_coff_link_add_one_symbol (info, globals->bfd_of_glue_owner, tmp_name,
1907 BSF_GLOBAL, s, val, NULL, true, false,
1908 (struct bfd_link_hash_entry **) & myh);
1912 globals->arm_glue_size += ARM2THUMB_GLUE_SIZE;
1918 record_thumb_to_arm_glue (info, h)
1919 struct bfd_link_info * info;
1920 struct coff_link_hash_entry * h;
1922 const char * name = h->root.root.string;
1923 register asection * s;
1925 struct coff_link_hash_entry * myh;
1926 struct coff_arm_link_hash_table * globals;
1930 globals = coff_arm_hash_table (info);
1932 BFD_ASSERT (globals != NULL);
1933 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1935 s = bfd_get_section_by_name
1936 (globals->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
1938 BFD_ASSERT (s != NULL);
1940 amt = strlen (name) + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1;
1941 tmp_name = (char *) bfd_malloc (amt);
1943 BFD_ASSERT (tmp_name);
1945 sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
1947 myh = coff_link_hash_lookup
1948 (coff_hash_table (info), tmp_name, false, false, true);
1953 return; /* we've already seen this guy */
1956 val = globals->thumb_glue_size + 1;
1957 bfd_coff_link_add_one_symbol (info, globals->bfd_of_glue_owner, tmp_name,
1958 BSF_GLOBAL, s, val, NULL, true, false,
1959 (struct bfd_link_hash_entry **) &myh);
1961 /* If we mark it 'thumb', the disassembler will do a better job. */
1962 myh->class = C_THUMBEXTFUNC;
1966 /* Allocate another symbol to mark where we switch to arm mode. */
1968 #define CHANGE_TO_ARM "__%s_change_to_arm"
1969 #define BACK_FROM_ARM "__%s_back_from_arm"
1971 amt = strlen (name) + strlen (CHANGE_TO_ARM) + 1;
1972 tmp_name = (char *) bfd_malloc (amt);
1974 BFD_ASSERT (tmp_name);
1976 sprintf (tmp_name, globals->support_old_code ? BACK_FROM_ARM : CHANGE_TO_ARM, name);
1979 val = globals->thumb_glue_size + (globals->support_old_code ? 8 : 4);
1980 bfd_coff_link_add_one_symbol (info, globals->bfd_of_glue_owner, tmp_name,
1981 BSF_LOCAL, s, val, NULL, true, false,
1982 (struct bfd_link_hash_entry **) & myh);
1986 globals->thumb_glue_size += THUMB2ARM_GLUE_SIZE;
1991 /* Select a BFD to be used to hold the sections used by the glue code.
1992 This function is called from the linker scripts in ld/emultempl/
1996 bfd_arm_get_bfd_for_interworking (abfd, info)
1998 struct bfd_link_info * info;
2000 struct coff_arm_link_hash_table * globals;
2004 /* If we are only performing a partial link do not bother
2005 getting a bfd to hold the glue. */
2006 if (info->relocateable)
2009 globals = coff_arm_hash_table (info);
2011 BFD_ASSERT (globals != NULL);
2013 if (globals->bfd_of_glue_owner != NULL)
2016 sec = bfd_get_section_by_name (abfd, ARM2THUMB_GLUE_SECTION_NAME);
2020 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE | SEC_READONLY;
2022 sec = bfd_make_section (abfd, ARM2THUMB_GLUE_SECTION_NAME);
2025 || ! bfd_set_section_flags (abfd, sec, flags)
2026 || ! bfd_set_section_alignment (abfd, sec, 2))
2030 sec = bfd_get_section_by_name (abfd, THUMB2ARM_GLUE_SECTION_NAME);
2034 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE | SEC_READONLY;
2036 sec = bfd_make_section (abfd, THUMB2ARM_GLUE_SECTION_NAME);
2039 || ! bfd_set_section_flags (abfd, sec, flags)
2040 || ! bfd_set_section_alignment (abfd, sec, 2))
2044 /* Save the bfd for later use. */
2045 globals->bfd_of_glue_owner = abfd;
2051 bfd_arm_process_before_allocation (abfd, info, support_old_code)
2053 struct bfd_link_info * info;
2054 int support_old_code;
2057 struct coff_arm_link_hash_table * globals;
2059 /* If we are only performing a partial link do not bother
2060 to construct any glue. */
2061 if (info->relocateable)
2064 /* Here we have a bfd that is to be included on the link. We have a hook
2065 to do reloc rummaging, before section sizes are nailed down. */
2067 _bfd_coff_get_external_symbols (abfd);
2069 globals = coff_arm_hash_table (info);
2071 BFD_ASSERT (globals != NULL);
2072 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2074 globals->support_old_code = support_old_code;
2076 /* Rummage around all the relocs and map the glue vectors. */
2077 sec = abfd->sections;
2082 for (; sec != NULL; sec = sec->next)
2084 struct internal_reloc * i;
2085 struct internal_reloc * rel;
2087 if (sec->reloc_count == 0)
2090 /* Load the relocs. */
2091 /* FIXME: there may be a storage leak here. */
2093 i = _bfd_coff_read_internal_relocs (abfd, sec, 1, 0, 0, 0);
2095 BFD_ASSERT (i != 0);
2097 for (rel = i; rel < i + sec->reloc_count; ++rel)
2099 unsigned short r_type = rel->r_type;
2101 struct coff_link_hash_entry * h;
2103 symndx = rel->r_symndx;
2105 /* If the relocation is not against a symbol it cannot concern us. */
2109 /* If the index is outside of the range of our table, something has gone wrong. */
2110 if (symndx >= obj_conv_table_size (abfd))
2112 _bfd_error_handler (_("%s: illegal symbol index in reloc: %d"),
2113 bfd_archive_filename (abfd), symndx);
2117 h = obj_coff_sym_hashes (abfd)[symndx];
2119 /* If the relocation is against a static symbol it must be within
2120 the current section and so cannot be a cross ARM/Thumb relocation. */
2127 /* This one is a call from arm code. We need to look up
2128 the target of the call. If it is a thumb target, we
2131 if (h->class == C_THUMBEXTFUNC)
2132 record_arm_to_thumb_glue (info, h);
2137 /* This one is a call from thumb code. We used to look
2138 for ARM_THUMB9 and ARM_THUMB12 as well. We need to look
2139 up the target of the call. If it is an arm target, we
2140 insert glue. If the symbol does not exist it will be
2141 given a class of C_EXT and so we will generate a stub
2142 for it. This is not really a problem, since the link
2143 is doomed anyway. */
2150 record_thumb_to_arm_glue (info, h);
2167 #endif /* ! defined (COFF_IMAGE_WITH_PE) */
2169 #define coff_bfd_reloc_type_lookup coff_arm_reloc_type_lookup
2170 #define coff_relocate_section coff_arm_relocate_section
2171 #define coff_bfd_is_local_label_name coff_arm_is_local_label_name
2172 #define coff_adjust_symndx coff_arm_adjust_symndx
2173 #define coff_link_output_has_begun coff_arm_link_output_has_begun
2174 #define coff_final_link_postscript coff_arm_final_link_postscript
2175 #define coff_bfd_merge_private_bfd_data coff_arm_merge_private_bfd_data
2176 #define coff_bfd_print_private_bfd_data coff_arm_print_private_bfd_data
2177 #define coff_bfd_set_private_flags _bfd_coff_arm_set_private_flags
2178 #define coff_bfd_copy_private_bfd_data coff_arm_copy_private_bfd_data
2179 #define coff_bfd_link_hash_table_create coff_arm_link_hash_table_create
2181 /* When doing a relocateable link, we want to convert ARM26 relocs
2182 into ARM26D relocs. */
2185 coff_arm_adjust_symndx (obfd, info, ibfd, sec, irel, adjustedp)
2186 bfd *obfd ATTRIBUTE_UNUSED;
2187 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2190 struct internal_reloc *irel;
2193 if (irel->r_type == 3)
2195 struct coff_link_hash_entry *h;
2197 h = obj_coff_sym_hashes (ibfd)[irel->r_symndx];
2199 && (h->root.type == bfd_link_hash_defined
2200 || h->root.type == bfd_link_hash_defweak)
2201 && h->root.u.def.section->output_section == sec->output_section)
2208 /* Called when merging the private data areas of two BFDs.
2209 This is important as it allows us to detect if we are
2210 attempting to merge binaries compiled for different ARM
2211 targets, eg different CPUs or differents APCS's. */
2214 coff_arm_merge_private_bfd_data (ibfd, obfd)
2218 BFD_ASSERT (ibfd != NULL && obfd != NULL);
2223 /* If the two formats are different we cannot merge anything.
2224 This is not an error, since it is permissable to change the
2225 input and output formats. */
2226 if ( ibfd->xvec->flavour != bfd_target_coff_flavour
2227 || obfd->xvec->flavour != bfd_target_coff_flavour)
2230 /* Verify that the APCS is the same for the two BFDs */
2231 if (APCS_SET (ibfd))
2233 if (APCS_SET (obfd))
2235 /* If the src and dest have different APCS flag bits set, fail. */
2236 if (APCS_26_FLAG (obfd) != APCS_26_FLAG (ibfd))
2239 /* xgettext: c-format */
2240 (_("ERROR: %s is compiled for APCS-%d, whereas %s is compiled for APCS-%d"),
2241 bfd_archive_filename (ibfd), APCS_26_FLAG (ibfd) ? 26 : 32,
2242 bfd_get_filename (obfd), APCS_26_FLAG (obfd) ? 26 : 32
2245 bfd_set_error (bfd_error_wrong_format);
2249 if (APCS_FLOAT_FLAG (obfd) != APCS_FLOAT_FLAG (ibfd))
2253 if (APCS_FLOAT_FLAG (ibfd))
2254 /* xgettext: c-format */
2255 msg = _("ERROR: %s passes floats in float registers, whereas %s passes them in integer registers");
2257 /* xgettext: c-format */
2258 msg = _("ERROR: %s passes floats in integer registers, whereas %s passes them in float registers");
2260 _bfd_error_handler (msg, bfd_archive_filename (ibfd),
2261 bfd_get_filename (obfd));
2263 bfd_set_error (bfd_error_wrong_format);
2267 if (PIC_FLAG (obfd) != PIC_FLAG (ibfd))
2271 if (PIC_FLAG (ibfd))
2272 /* xgettext: c-format */
2273 msg = _("ERROR: %s is compiled as position independent code, whereas target %s is absolute position");
2275 /* xgettext: c-format */
2276 msg = _("ERROR: %s is compiled as absolute position code, whereas target %s is position independent");
2277 _bfd_error_handler (msg, bfd_archive_filename (ibfd),
2278 bfd_get_filename (obfd));
2280 bfd_set_error (bfd_error_wrong_format);
2286 SET_APCS_FLAGS (obfd, APCS_26_FLAG (ibfd) | APCS_FLOAT_FLAG (ibfd) | PIC_FLAG (ibfd));
2288 /* Set up the arch and fields as well as these are probably wrong. */
2289 bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
2293 /* Check the interworking support. */
2294 if (INTERWORK_SET (ibfd))
2296 if (INTERWORK_SET (obfd))
2298 /* If the src and dest differ in their interworking issue a warning. */
2299 if (INTERWORK_FLAG (obfd) != INTERWORK_FLAG (ibfd))
2303 if (INTERWORK_FLAG (ibfd))
2304 /* xgettext: c-format */
2305 msg = _("Warning: %s supports interworking, whereas %s does not");
2307 /* xgettext: c-format */
2308 msg = _("Warning: %s does not support interworking, whereas %s does");
2310 _bfd_error_handler (msg, bfd_archive_filename (ibfd),
2311 bfd_get_filename (obfd));
2316 SET_INTERWORK_FLAG (obfd, INTERWORK_FLAG (ibfd));
2323 /* Display the flags field. */
2326 coff_arm_print_private_bfd_data (abfd, ptr)
2330 FILE * file = (FILE *) ptr;
2332 BFD_ASSERT (abfd != NULL && ptr != NULL);
2334 /* xgettext:c-format */
2335 fprintf (file, _("private flags = %x:"), coff_data (abfd)->flags);
2337 if (APCS_SET (abfd))
2339 /* xgettext: APCS is ARM Prodecure Call Standard, it should not be translated. */
2340 fprintf (file, " [APCS-%d]", APCS_26_FLAG (abfd) ? 26 : 32);
2342 if (APCS_FLOAT_FLAG (abfd))
2343 fprintf (file, _(" [floats passed in float registers]"));
2345 fprintf (file, _(" [floats passed in integer registers]"));
2347 if (PIC_FLAG (abfd))
2348 fprintf (file, _(" [position independent]"));
2350 fprintf (file, _(" [absolute position]"));
2353 if (! INTERWORK_SET (abfd))
2354 fprintf (file, _(" [interworking flag not initialised]"));
2355 else if (INTERWORK_FLAG (abfd))
2356 fprintf (file, _(" [interworking supported]"));
2358 fprintf (file, _(" [interworking not supported]"));
2365 /* Copies the given flags into the coff_tdata.flags field.
2366 Typically these flags come from the f_flags[] field of
2367 the COFF filehdr structure, which contains important,
2368 target specific information.
2369 Note: Although this function is static, it is explicitly
2370 called from both coffcode.h and peicode.h. */
2373 _bfd_coff_arm_set_private_flags (abfd, flags)
2379 BFD_ASSERT (abfd != NULL);
2381 flag = (flags & F_APCS26) ? F_APCS_26 : 0;
2383 /* Make sure that the APCS field has not been initialised to the opposite
2386 && ( (APCS_26_FLAG (abfd) != flag)
2387 || (APCS_FLOAT_FLAG (abfd) != (flags & F_APCS_FLOAT))
2388 || (PIC_FLAG (abfd) != (flags & F_PIC))
2392 flag |= (flags & (F_APCS_FLOAT | F_PIC));
2394 SET_APCS_FLAGS (abfd, flag);
2396 flag = (flags & F_INTERWORK);
2398 /* If the BFD has already had its interworking flag set, but it
2399 is different from the value that we have been asked to set,
2400 then assume that that merged code will not support interworking
2401 and set the flag accordingly. */
2402 if (INTERWORK_SET (abfd) && (INTERWORK_FLAG (abfd) != flag))
2405 /* xgettext: c-format */
2406 _bfd_error_handler (_("Warning: Not setting interworking flag of %s since it has already been specified as non-interworking"),
2407 bfd_archive_filename (abfd));
2409 /* xgettext: c-format */
2410 _bfd_error_handler (_("Warning: Clearing the interworking flag of %s due to outside request"),
2411 bfd_archive_filename (abfd));
2415 SET_INTERWORK_FLAG (abfd, flag);
2420 /* Copy the important parts of the target specific data
2421 from one instance of a BFD to another. */
2424 coff_arm_copy_private_bfd_data (src, dest)
2428 BFD_ASSERT (src != NULL && dest != NULL);
2433 /* If the destination is not in the same format as the source, do not do
2435 if (src->xvec != dest->xvec)
2438 /* copy the flags field */
2441 if (APCS_SET (dest))
2443 /* If the src and dest have different APCS flag bits set, fail. */
2444 if (APCS_26_FLAG (dest) != APCS_26_FLAG (src))
2447 if (APCS_FLOAT_FLAG (dest) != APCS_FLOAT_FLAG (src))
2450 if (PIC_FLAG (dest) != PIC_FLAG (src))
2454 SET_APCS_FLAGS (dest, APCS_26_FLAG (src) | APCS_FLOAT_FLAG (src)
2458 if (INTERWORK_SET (src))
2460 if (INTERWORK_SET (dest))
2462 /* If the src and dest have different interworking flags then turn
2463 off the interworking bit. */
2464 if (INTERWORK_FLAG (dest) != INTERWORK_FLAG (src))
2466 if (INTERWORK_FLAG (dest))
2468 /* xgettext:c-format */
2469 _bfd_error_handler (("\
2470 Warning: Clearing the interworking flag of %s because non-interworking code in %s has been linked with it"),
2471 bfd_get_filename (dest),
2472 bfd_archive_filename (src));
2475 SET_INTERWORK_FLAG (dest, 0);
2480 SET_INTERWORK_FLAG (dest, INTERWORK_FLAG (src));
2487 /* Note: the definitions here of LOCAL_LABEL_PREFIX and USER_LABEL_PREIFX
2488 *must* match the definitions in gcc/config/arm/{coff|semi|aout}.h. */
2489 #define LOCAL_LABEL_PREFIX ""
2490 #ifndef USER_LABEL_PREFIX
2491 #define USER_LABEL_PREFIX "_"
2494 /* Like _bfd_coff_is_local_label_name, but
2495 a) test against USER_LABEL_PREFIX, to avoid stripping labels known to be
2497 b) Allow other prefixes than ".", e.g. an empty prefix would cause all
2498 labels of the form Lxxx to be stripped. */
2500 coff_arm_is_local_label_name (abfd, name)
2501 bfd * abfd ATTRIBUTE_UNUSED;
2504 #ifdef USER_LABEL_PREFIX
2505 if (USER_LABEL_PREFIX[0] != 0)
2507 if (strncmp (name, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)) == 0)
2512 #ifdef LOCAL_LABEL_PREFIX
2513 /* If there is a prefix for local labels then look for this.
2514 If the prefix exists, but it is empty, then ignore the test. */
2516 if (LOCAL_LABEL_PREFIX[0] != 0)
2518 size_t len = strlen (LOCAL_LABEL_PREFIX);
2520 if (strncmp (name, LOCAL_LABEL_PREFIX, len) != 0)
2523 /* Perform the checks below for the rest of the name. */
2528 return name[0] == 'L';
2531 /* This piece of machinery exists only to guarantee that the bfd that holds
2532 the glue section is written last.
2534 This does depend on bfd_make_section attaching a new section to the
2535 end of the section list for the bfd.
2540 coff_arm_link_output_has_begun (sub, info)
2542 struct coff_final_link_info * info;
2544 return (sub->output_has_begun
2545 || sub == coff_arm_hash_table (info->info)->bfd_of_glue_owner);
2549 coff_arm_final_link_postscript (abfd, pfinfo)
2550 bfd * abfd ATTRIBUTE_UNUSED;
2551 struct coff_final_link_info * pfinfo;
2553 struct coff_arm_link_hash_table * globals;
2555 globals = coff_arm_hash_table (pfinfo->info);
2557 BFD_ASSERT (globals != NULL);
2559 if (globals->bfd_of_glue_owner != NULL)
2561 if (! _bfd_coff_link_input_bfd (pfinfo, globals->bfd_of_glue_owner))
2564 globals->bfd_of_glue_owner->output_has_begun = true;
2570 #include "coffcode.h"
2572 #ifndef TARGET_LITTLE_SYM
2573 #define TARGET_LITTLE_SYM armcoff_little_vec
2575 #ifndef TARGET_LITTLE_NAME
2576 #define TARGET_LITTLE_NAME "coff-arm-little"
2578 #ifndef TARGET_BIG_SYM
2579 #define TARGET_BIG_SYM armcoff_big_vec
2581 #ifndef TARGET_BIG_NAME
2582 #define TARGET_BIG_NAME "coff-arm-big"
2585 #ifndef TARGET_UNDERSCORE
2586 #define TARGET_UNDERSCORE 0
2589 #ifndef EXTRA_S_FLAGS
2591 #define EXTRA_S_FLAGS (SEC_LINK_ONCE | SEC_LINK_DUPLICATES)
2593 #define EXTRA_S_FLAGS 0
2597 /* Forward declaration for use initialising alternative_target field. */
2598 extern const bfd_target TARGET_BIG_SYM ;
2600 /* Target vectors. */
2601 CREATE_LITTLE_COFF_TARGET_VEC (TARGET_LITTLE_SYM, TARGET_LITTLE_NAME, D_PAGED, EXTRA_S_FLAGS, TARGET_UNDERSCORE, & TARGET_BIG_SYM)
2602 CREATE_BIG_COFF_TARGET_VEC (TARGET_BIG_SYM, TARGET_BIG_NAME, D_PAGED, EXTRA_S_FLAGS, TARGET_UNDERSCORE, & TARGET_LITTLE_SYM)