1 /* start-sanitize-armelf */
2 /* 32-bit ELF support for ARM
3 Copyright 1993, 1995, 1998 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 static reloc_howto_type
*elf32_arm_reloc_type_lookup
29 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
30 static void elf32_arm_info_to_howto
31 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rela
*));
34 #define TARGET_UNDERSCORE '_'
36 static reloc_howto_type elf32_arm_howto_table
[] =
39 HOWTO (R_ARM_NONE
, /* type */
41 0, /* size (0 = byte, 1 = short, 2 = long) */
43 false, /* pc_relative */
45 complain_overflow_dont
, /* complain_on_overflow */
46 bfd_elf_generic_reloc
, /* special_function */
47 "R_ARM_NONE", /* name */
48 false, /* partial_inplace */
51 false), /* pcrel_offset */
53 HOWTO (R_ARM_PC24
, /* type */
55 2, /* size (0 = byte, 1 = short, 2 = long) */
57 true, /* pc_relative */
59 complain_overflow_signed
, /* complain_on_overflow */
60 bfd_elf_generic_reloc
, /* special_function */
61 "R_ARM_PC24", /* name */
62 false, /* partial_inplace */
63 0x00ffffff, /* src_mask */
64 0x00ffffff, /* dst_mask */
65 true), /* pcrel_offset */
68 HOWTO (R_ARM_ABS32
, /* type */
70 2, /* size (0 = byte, 1 = short, 2 = long) */
72 false, /* pc_relative */
74 complain_overflow_bitfield
, /* complain_on_overflow */
75 bfd_elf_generic_reloc
, /* special_function */
76 "R_ARM_ABS32", /* name */
77 false, /* partial_inplace */
78 0xffffffff, /* src_mask */
79 0xffffffff, /* dst_mask */
80 false), /* pcrel_offset */
82 /* standard 32bit pc-relative reloc */
83 HOWTO (R_ARM_REL32
, /* type */
85 2, /* size (0 = byte, 1 = short, 2 = long) */
87 true, /* pc_relative */
89 complain_overflow_bitfield
, /* complain_on_overflow */
90 bfd_elf_generic_reloc
, /* special_function */
91 "R_ARM_REL32", /* name */
92 false, /* partial_inplace */
93 0xffffffff, /* src_mask */
94 0xffffffff, /* dst_mask */
95 true), /* pcrel_offset */
98 HOWTO (R_ARM_ABS8
, /* type */
100 0, /* size (0 = byte, 1 = short, 2 = long) */
102 false, /* pc_relative */
104 complain_overflow_bitfield
, /* complain_on_overflow */
105 bfd_elf_generic_reloc
, /* special_function */
106 "R_ARM_ABS8", /* name */
107 false, /* partial_inplace */
108 0x000000ff, /* src_mask */
109 0x000000ff, /* dst_mask */
110 false), /* pcrel_offset */
112 /* 16 bit absolute */
113 HOWTO (R_ARM_ABS16
, /* type */
115 1, /* size (0 = byte, 1 = short, 2 = long) */
117 false, /* pc_relative */
119 complain_overflow_bitfield
, /* complain_on_overflow */
120 bfd_elf_generic_reloc
, /* special_function */
121 "R_ARM_ABS16", /* name */
122 false, /* partial_inplace */
125 false), /* pcrel_offset */
127 /* 12 bit absolute */
128 HOWTO (R_ARM_ABS12
, /* type */
130 2, /* size (0 = byte, 1 = short, 2 = long) */
132 false, /* pc_relative */
134 complain_overflow_bitfield
, /* complain_on_overflow */
135 bfd_elf_generic_reloc
, /* special_function */
136 "R_ARM_ABS12", /* name */
137 false, /* partial_inplace */
138 0x000008ff, /* src_mask */
139 0x000008ff, /* dst_mask */
140 false), /* pcrel_offset */
142 HOWTO (R_ARM_THM_ABS5
, /* type */
144 2, /* size (0 = byte, 1 = short, 2 = long) */
146 false, /* pc_relative */
148 complain_overflow_bitfield
, /* complain_on_overflow */
149 bfd_elf_generic_reloc
, /* special_function */
150 "R_ARM_THM_ABS5", /* name */
151 false, /* partial_inplace */
152 0x000007e0, /* src_mask */
153 0x000007e0, /* dst_mask */
154 false), /* pcrel_offset */
156 HOWTO (R_ARM_THM_PC22
, /* type */
158 2, /* size (0 = byte, 1 = short, 2 = long) */
160 true, /* pc_relative */
162 complain_overflow_signed
, /* complain_on_overflow */
163 bfd_elf_generic_reloc
, /* special_function */
164 "R_ARM_THM_PC22", /* name */
165 false, /* partial_inplace */
166 0x07ff07ff, /* src_mask */
167 0x07ff07ff, /* dst_mask */
168 true), /* pcrel_offset */
170 HOWTO (R_ARM_SBREL32
, /* type */
172 0, /* size (0 = byte, 1 = short, 2 = long) */
174 false, /* pc_relative */
176 complain_overflow_dont
, /* complain_on_overflow */
177 bfd_elf_generic_reloc
, /* special_function */
178 "R_ARM_SBREL32", /* name */
179 false, /* partial_inplace */
182 false), /* pcrel_offset */
184 HOWTO (R_ARM_AMP_VCALL9
, /* type */
186 1, /* size (0 = byte, 1 = short, 2 = long) */
188 true, /* pc_relative */
190 complain_overflow_signed
, /* complain_on_overflow */
191 bfd_elf_generic_reloc
, /* special_function */
192 "R_ARM_AMP_VCALL9", /* name */
193 false, /* partial_inplace */
194 0x000000ff, /* src_mask */
195 0x000000ff, /* dst_mask */
196 true), /* pcrel_offset */
198 /* 12 bit pc relative */
199 HOWTO (R_ARM_THM_PC11
, /* type */
201 1, /* size (0 = byte, 1 = short, 2 = long) */
203 true, /* pc_relative */
205 complain_overflow_signed
, /* complain_on_overflow */
206 bfd_elf_generic_reloc
, /* special_function */
207 "R_ARM_THM_PC11", /* name */
208 false, /* partial_inplace */
209 0x000007ff, /* src_mask */
210 0x000007ff, /* dst_mask */
211 true), /* pcrel_offset */
213 /* 12 bit pc relative */
214 HOWTO (R_ARM_THM_PC9
, /* type */
216 1, /* size (0 = byte, 1 = short, 2 = long) */
218 true, /* pc_relative */
220 complain_overflow_signed
, /* complain_on_overflow */
221 bfd_elf_generic_reloc
, /* special_function */
222 "R_ARM_THM_PC9", /* name */
223 false, /* partial_inplace */
224 0x000000ff, /* src_mask */
225 0x000000ff, /* dst_mask */
226 true), /* pcrel_offset */
228 /* FILL ME IN (#13-249) */
231 HOWTO (R_ARM_RREL32
, /* type */
233 0, /* size (0 = byte, 1 = short, 2 = long) */
235 false, /* pc_relative */
237 complain_overflow_dont
, /* complain_on_overflow */
238 bfd_elf_generic_reloc
, /* special_function */
239 "R_ARM_RREL32", /* name */
240 false, /* partial_inplace */
243 false), /* pcrel_offset */
245 HOWTO (R_ARM_RABS32
, /* type */
247 0, /* size (0 = byte, 1 = short, 2 = long) */
249 false, /* pc_relative */
251 complain_overflow_dont
, /* complain_on_overflow */
252 bfd_elf_generic_reloc
, /* special_function */
253 "R_ARM_RABS32", /* name */
254 false, /* partial_inplace */
257 false), /* pcrel_offset */
259 HOWTO (R_ARM_RPC24
, /* type */
261 0, /* size (0 = byte, 1 = short, 2 = long) */
263 false, /* pc_relative */
265 complain_overflow_dont
, /* complain_on_overflow */
266 bfd_elf_generic_reloc
, /* special_function */
267 "R_ARM_RPC24", /* name */
268 false, /* partial_inplace */
271 false), /* pcrel_offset */
273 HOWTO (R_ARM_RBASE
, /* type */
275 0, /* size (0 = byte, 1 = short, 2 = long) */
277 false, /* pc_relative */
279 complain_overflow_dont
, /* complain_on_overflow */
280 bfd_elf_generic_reloc
, /* special_function */
281 "R_ARM_RBASE", /* name */
282 false, /* partial_inplace */
285 false), /* pcrel_offset */
288 struct elf32_arm_reloc_map
290 unsigned char bfd_reloc_val
;
291 unsigned char elf_reloc_val
;
294 static const struct elf32_arm_reloc_map elf32_arm_reloc_map
[] =
296 { BFD_RELOC_NONE
, R_ARM_NONE
, },
297 { BFD_RELOC_ARM_PCREL_BRANCH
, R_ARM_PC24
, },
298 { BFD_RELOC_32
, R_ARM_ABS32
, },
299 { BFD_RELOC_32_PCREL
, R_ARM_REL32
, },
300 { BFD_RELOC_8
, R_ARM_ABS8
, },
301 { BFD_RELOC_16
, R_ARM_ABS16
, },
302 { BFD_RELOC_ARM_OFFSET_IMM
, R_ARM_ABS12
, },
303 { BFD_RELOC_ARM_THUMB_OFFSET
, R_ARM_THM_ABS5
, },
304 { BFD_RELOC_THUMB_PCREL_BRANCH23
, R_ARM_THM_PC22
, },
305 { BFD_RELOC_NONE
, R_ARM_SBREL32
, },
306 { BFD_RELOC_NONE
, R_ARM_AMP_VCALL9
, },
307 { BFD_RELOC_THUMB_PCREL_BRANCH12
, R_ARM_THM_PC11
, },
308 { BFD_RELOC_THUMB_PCREL_BRANCH9
, R_ARM_THM_PC9
, }
311 static reloc_howto_type
*
312 elf32_arm_reloc_type_lookup (abfd
, code
)
314 bfd_reloc_code_real_type code
;
319 i
< sizeof (elf32_arm_reloc_map
) / sizeof (struct elf32_arm_reloc_map
);
322 if (elf32_arm_reloc_map
[i
].bfd_reloc_val
== code
)
323 return &elf32_arm_howto_table
[elf32_arm_reloc_map
[i
].elf_reloc_val
];
330 elf32_arm_info_to_howto (abfd
, bfd_reloc
, elf_reloc
)
333 Elf32_Internal_Rela
*elf_reloc
;
337 r_type
= ELF32_R_TYPE (elf_reloc
->r_info
);
338 /* fixme: need range test */
339 /* BFD_ASSERT (r_type < (unsigned int) R_ELF32_ARM_MAX); */
340 bfd_reloc
->howto
= &elf32_arm_howto_table
[r_type
];
343 /* Perform a relocation as part of a final link. */
344 static bfd_reloc_status_type
345 elf32_arm_final_link_relocate (howto
, input_bfd
, output_bfd
,
346 input_section
, contents
, offset
, value
,
347 addend
, info
, sym_sec
, is_local
)
348 reloc_howto_type
*howto
;
351 asection
*input_section
;
356 struct bfd_link_info
*info
;
360 unsigned long r_type
= howto
->type
;
361 bfd_byte
*hit_data
= contents
+ offset
;
370 value
-= (input_section
->output_offset
+ offset
+ 8);
374 /* if ((long) value > 0xffffff || (long) value < -0x1000000)
375 return bfd_reloc_overflow; */
378 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
379 bfd_put_32 (input_bfd
, value
, hit_data
);
384 bfd_put_32 (input_bfd
, value
, hit_data
);
388 value
-= (input_section
->output_section
->vma
389 + input_section
->output_offset
);
392 bfd_put_32 (input_bfd
, value
, hit_data
);
398 if ((long) value
> 0x7f || (long) value
< -0x80)
399 return bfd_reloc_overflow
;
401 bfd_put_8 (input_bfd
, value
, hit_data
);
407 if ((long) value
> 0x7fff || (long) value
< -0x8000)
408 return bfd_reloc_overflow
;
410 bfd_put_16 (input_bfd
, value
, hit_data
);
414 /* Support ldr and str instruction for the arm */
415 /* Also thumb b (unconditional branch) */
418 if ((long) value
> 0x7ff || (long) value
< -0x800)
419 return bfd_reloc_overflow
;
421 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xfffff000);
422 bfd_put_32 (input_bfd
, value
, hit_data
);
426 /* Support ldr and str instructions for the thumb. */
429 if ((long) value
> 0x1f || (long) value
< -0x10)
430 return bfd_reloc_overflow
;
432 value
|= bfd_get_16 (input_bfd
, hit_data
) & 0xf82f;
433 bfd_put_16 (input_bfd
, value
, hit_data
);
437 #define LOW_HI_ORDER 0xF800F000
438 #define HI_LOW_ORDER 0xF000F800
440 /* thumb BL (branch long instruction). */
443 unsigned int low_bits
;
444 unsigned int high_bits
;
447 value
-= (input_section
->output_offset
+ offset
+ 8);
451 low_bits
= value
& 0x000007ff;
452 high_bits
= (value
>> 11) & 0x000007ff;
453 insn
= bfd_get_32 (input_bfd
, hit_data
);
455 if ((insn
& LOW_HI_ORDER
) == LOW_HI_ORDER
)
456 insn
= LOW_HI_ORDER
| (low_bits
<< 16) | high_bits
;
457 else if ((insn
& HI_LOW_ORDER
) == HI_LOW_ORDER
)
458 insn
= HI_LOW_ORDER
| (high_bits
<< 16) | low_bits
;
460 abort(); /* error - not a valid branch instruction form */
462 bfd_put_32 (input_bfd
, insn
, hit_data
);
470 case R_ARM_AMP_VCALL9
:
471 return bfd_reloc_notsupported
;
474 return bfd_reloc_notsupported
;
476 case R_ARM_THM_RPC22
:
477 return bfd_reloc_notsupported
;
480 return bfd_reloc_notsupported
;
483 return bfd_reloc_notsupported
;
486 return bfd_reloc_notsupported
;
489 return bfd_reloc_notsupported
;
492 return bfd_reloc_notsupported
;
495 /* Relocate an ARM ELF section. */
497 elf32_arm_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
498 contents
, relocs
, local_syms
, local_sections
)
500 struct bfd_link_info
*info
;
502 asection
*input_section
;
504 Elf_Internal_Rela
*relocs
;
505 Elf_Internal_Sym
*local_syms
;
506 asection
**local_sections
;
508 Elf_Internal_Shdr
*symtab_hdr
;
509 struct elf_link_hash_entry
**sym_hashes
;
510 Elf_Internal_Rela
*rel
, *relend
;
512 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
513 sym_hashes
= elf_sym_hashes (input_bfd
);
516 relend
= relocs
+ input_section
->reloc_count
;
517 for (; rel
< relend
; rel
++)
520 reloc_howto_type
*howto
;
521 unsigned long r_symndx
;
522 Elf_Internal_Sym
*sym
;
524 struct elf_link_hash_entry
*h
;
526 bfd_reloc_status_type r
;
528 r_symndx
= ELF32_R_SYM (rel
->r_info
);
529 r_type
= ELF32_R_TYPE (rel
->r_info
);
530 howto
= elf32_arm_howto_table
+ r_type
;
532 if (info
->relocateable
)
534 /* This is a relocateable link. We don't have to change
535 anything, unless the reloc is against a section symbol,
536 in which case we have to adjust according to where the
537 section symbol winds up in the output section. */
538 if (r_symndx
< symtab_hdr
->sh_info
)
540 sym
= local_syms
+ r_symndx
;
541 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
543 sec
= local_sections
[r_symndx
];
544 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
551 /* This is a final link. */
555 if (r_symndx
< symtab_hdr
->sh_info
)
557 sym
= local_syms
+ r_symndx
;
558 sec
= local_sections
[r_symndx
];
559 relocation
= (sec
->output_section
->vma
565 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
566 while (h
->root
.type
== bfd_link_hash_indirect
567 || h
->root
.type
== bfd_link_hash_warning
)
568 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
569 if (h
->root
.type
== bfd_link_hash_defined
570 || h
->root
.type
== bfd_link_hash_defweak
)
572 sec
= h
->root
.u
.def
.section
;
573 relocation
= (h
->root
.u
.def
.value
574 + sec
->output_section
->vma
575 + sec
->output_offset
);
577 else if (h
->root
.type
== bfd_link_hash_undefweak
)
581 if (! ((*info
->callbacks
->undefined_symbol
)
582 (info
, h
->root
.root
.string
, input_bfd
,
583 input_section
, rel
->r_offset
)))
589 r
= elf32_arm_final_link_relocate (howto
, input_bfd
, output_bfd
,
591 contents
, rel
->r_offset
,
592 relocation
, rel
->r_addend
,
593 info
, sec
, h
== NULL
);
596 if (r
!= bfd_reloc_ok
)
599 const char *msg
= (const char *)0;
602 name
= h
->root
.root
.string
;
605 name
= (bfd_elf_string_from_elf_section
606 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
607 if (name
== NULL
|| *name
== '\0')
608 name
= bfd_section_name (input_bfd
, sec
);
613 case bfd_reloc_overflow
:
614 if (! ((*info
->callbacks
->reloc_overflow
)
615 (info
, name
, howto
->name
, (bfd_vma
) 0,
616 input_bfd
, input_section
, rel
->r_offset
)))
620 case bfd_reloc_undefined
:
621 if (! ((*info
->callbacks
->undefined_symbol
)
622 (info
, name
, input_bfd
, input_section
,
627 case bfd_reloc_outofrange
:
628 msg
= _("internal error: out of range error");
631 case bfd_reloc_notsupported
:
632 msg
= _("internal error: unsupported relocation error");
635 case bfd_reloc_dangerous
:
636 msg
= _("internal error: dangerous error");
640 msg
= _("internal error: unknown error");
644 if (!((*info
->callbacks
->warning
)
645 (info
, msg
, name
, input_bfd
, input_section
,
656 #define TARGET_LITTLE_SYM bfd_elf32_littlearm_vec
657 #define TARGET_LITTLE_NAME "elf32-littlearm"
658 #define TARGET_BIG_SYM bfd_elf32_bigarm_vec
659 #define TARGET_BIG_NAME "elf32-bigarm"
660 #define ELF_ARCH bfd_arch_arm
661 #define ELF_MACHINE_CODE EM_ARM
663 #define bfd_elf32_bfd_reloc_type_lookup elf32_arm_reloc_type_lookup
664 #define elf_info_to_howto elf32_arm_info_to_howto
665 #define elf_info_to_howto_rel 0
666 #define elf_backend_relocate_section elf32_arm_relocate_section
668 #define elf_symbol_leading_char '_'
670 #include "elf32-target.h"
671 /* end-sanitize-armelf */