1 // arm.cc -- arm target support for gold.
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
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 3 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., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
32 #include "parameters.h"
39 #include "copy-relocs.h"
41 #include "target-reloc.h"
42 #include "target-select.h"
51 template<bool big_endian>
52 class Output_data_plt_arm;
54 // The arm target class.
56 // This is a very simple port of gold for ARM-EABI. It is intended for
57 // supporting Android only for the time being. Only these relocation types
86 // R_ARM_THM_MOVW_ABS_NC
90 // R_ARM_THM_MOVW_PREL_NC
91 // R_ARM_THM_MOVT_PREL
94 // - Generate various branch stubs.
95 // - Support interworking.
96 // - Define section symbols __exidx_start and __exidx_stop.
97 // - Support more relocation types as needed.
98 // - Make PLTs more flexible for different architecture features like
100 // There are probably a lot more.
102 // Utilities for manipulating integers of up to 32-bits
106 // Sign extend an n-bit unsigned integer stored in an uint32_t into
107 // an int32_t. NO_BITS must be between 1 to 32.
108 template<int no_bits>
109 static inline int32_t
110 sign_extend(uint32_t bits)
112 gold_assert(no_bits >= 0 && no_bits <= 32);
114 return static_cast<int32_t>(bits);
115 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
117 uint32_t top_bit = 1U << (no_bits - 1);
118 int32_t as_signed = static_cast<int32_t>(bits);
119 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
122 // Detects overflow of an NO_BITS integer stored in a uint32_t.
123 template<int no_bits>
125 has_overflow(uint32_t bits)
127 gold_assert(no_bits >= 0 && no_bits <= 32);
130 int32_t max = (1 << (no_bits - 1)) - 1;
131 int32_t min = -(1 << (no_bits - 1));
132 int32_t as_signed = static_cast<int32_t>(bits);
133 return as_signed > max || as_signed < min;
136 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
137 // fits in the given number of bits as either a signed or unsigned value.
138 // For example, has_signed_unsigned_overflow<8> would check
139 // -128 <= bits <= 255
140 template<int no_bits>
142 has_signed_unsigned_overflow(uint32_t bits)
144 gold_assert(no_bits >= 2 && no_bits <= 32);
147 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
148 int32_t min = -(1 << (no_bits - 1));
149 int32_t as_signed = static_cast<int32_t>(bits);
150 return as_signed > max || as_signed < min;
153 // Select bits from A and B using bits in MASK. For each n in [0..31],
154 // the n-th bit in the result is chosen from the n-th bits of A and B.
155 // A zero selects A and a one selects B.
156 static inline uint32_t
157 bit_select(uint32_t a, uint32_t b, uint32_t mask)
158 { return (a & ~mask) | (b & mask); }
161 template<bool big_endian>
162 class Target_arm : public Sized_target<32, big_endian>
165 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
169 : Sized_target<32, big_endian>(&arm_info),
170 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
171 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL)
174 // Process the relocations to determine unreferenced sections for
175 // garbage collection.
177 gc_process_relocs(const General_options& options,
178 Symbol_table* symtab,
180 Sized_relobj<32, big_endian>* object,
181 unsigned int data_shndx,
182 unsigned int sh_type,
183 const unsigned char* prelocs,
185 Output_section* output_section,
186 bool needs_special_offset_handling,
187 size_t local_symbol_count,
188 const unsigned char* plocal_symbols);
190 // Scan the relocations to look for symbol adjustments.
192 scan_relocs(const General_options& options,
193 Symbol_table* symtab,
195 Sized_relobj<32, big_endian>* object,
196 unsigned int data_shndx,
197 unsigned int sh_type,
198 const unsigned char* prelocs,
200 Output_section* output_section,
201 bool needs_special_offset_handling,
202 size_t local_symbol_count,
203 const unsigned char* plocal_symbols);
205 // Finalize the sections.
207 do_finalize_sections(Layout*);
209 // Return the value to use for a dynamic symbol which requires special
212 do_dynsym_value(const Symbol*) const;
214 // Relocate a section.
216 relocate_section(const Relocate_info<32, big_endian>*,
217 unsigned int sh_type,
218 const unsigned char* prelocs,
220 Output_section* output_section,
221 bool needs_special_offset_handling,
223 elfcpp::Elf_types<32>::Elf_Addr view_address,
224 section_size_type view_size,
225 const Reloc_symbol_changes*);
227 // Scan the relocs during a relocatable link.
229 scan_relocatable_relocs(const General_options& options,
230 Symbol_table* symtab,
232 Sized_relobj<32, big_endian>* object,
233 unsigned int data_shndx,
234 unsigned int sh_type,
235 const unsigned char* prelocs,
237 Output_section* output_section,
238 bool needs_special_offset_handling,
239 size_t local_symbol_count,
240 const unsigned char* plocal_symbols,
241 Relocatable_relocs*);
243 // Relocate a section during a relocatable link.
245 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
246 unsigned int sh_type,
247 const unsigned char* prelocs,
249 Output_section* output_section,
250 off_t offset_in_output_section,
251 const Relocatable_relocs*,
253 elfcpp::Elf_types<32>::Elf_Addr view_address,
254 section_size_type view_size,
255 unsigned char* reloc_view,
256 section_size_type reloc_view_size);
258 // Return whether SYM is defined by the ABI.
260 do_is_defined_by_abi(Symbol* sym) const
261 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
263 // Return the size of the GOT section.
267 gold_assert(this->got_ != NULL);
268 return this->got_->data_size();
271 // Map platform-specific reloc types
273 get_real_reloc_type (unsigned int r_type);
276 // The class which scans relocations.
281 : issued_non_pic_error_(false)
285 local(const General_options& options, Symbol_table* symtab,
286 Layout* layout, Target_arm* target,
287 Sized_relobj<32, big_endian>* object,
288 unsigned int data_shndx,
289 Output_section* output_section,
290 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
291 const elfcpp::Sym<32, big_endian>& lsym);
294 global(const General_options& options, Symbol_table* symtab,
295 Layout* layout, Target_arm* target,
296 Sized_relobj<32, big_endian>* object,
297 unsigned int data_shndx,
298 Output_section* output_section,
299 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
304 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
305 unsigned int r_type);
308 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
309 unsigned int r_type, Symbol*);
312 check_non_pic(Relobj*, unsigned int r_type);
314 // Almost identical to Symbol::needs_plt_entry except that it also
315 // handles STT_ARM_TFUNC.
317 symbol_needs_plt_entry(const Symbol* sym)
319 // An undefined symbol from an executable does not need a PLT entry.
320 if (sym->is_undefined() && !parameters->options().shared())
323 return (!parameters->doing_static_link()
324 && (sym->type() == elfcpp::STT_FUNC
325 || sym->type() == elfcpp::STT_ARM_TFUNC)
326 && (sym->is_from_dynobj()
327 || sym->is_undefined()
328 || sym->is_preemptible()));
331 // Whether we have issued an error about a non-PIC compilation.
332 bool issued_non_pic_error_;
335 // The class which implements relocation.
345 // Return whether the static relocation needs to be applied.
347 should_apply_static_reloc(const Sized_symbol<32>* gsym,
350 Output_section* output_section);
352 // Do a relocation. Return false if the caller should not issue
353 // any warnings about this relocation.
355 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
356 Output_section*, size_t relnum,
357 const elfcpp::Rel<32, big_endian>&,
358 unsigned int r_type, const Sized_symbol<32>*,
359 const Symbol_value<32>*,
360 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
363 // Return whether we want to pass flag NON_PIC_REF for this
366 reloc_is_non_pic (unsigned int r_type)
370 case elfcpp::R_ARM_REL32:
371 case elfcpp::R_ARM_THM_CALL:
372 case elfcpp::R_ARM_CALL:
373 case elfcpp::R_ARM_JUMP24:
374 case elfcpp::R_ARM_PREL31:
375 case elfcpp::R_ARM_THM_ABS5:
376 case elfcpp::R_ARM_ABS8:
377 case elfcpp::R_ARM_ABS12:
378 case elfcpp::R_ARM_ABS16:
379 case elfcpp::R_ARM_BASE_ABS:
387 // A class which returns the size required for a relocation type,
388 // used while scanning relocs during a relocatable link.
389 class Relocatable_size_for_reloc
393 get_size_for_reloc(unsigned int, Relobj*);
396 // Get the GOT section, creating it if necessary.
397 Output_data_got<32, big_endian>*
398 got_section(Symbol_table*, Layout*);
400 // Get the GOT PLT section.
402 got_plt_section() const
404 gold_assert(this->got_plt_ != NULL);
405 return this->got_plt_;
408 // Create a PLT entry for a global symbol.
410 make_plt_entry(Symbol_table*, Layout*, Symbol*);
412 // Get the PLT section.
413 const Output_data_plt_arm<big_endian>*
416 gold_assert(this->plt_ != NULL);
420 // Get the dynamic reloc section, creating it if necessary.
422 rel_dyn_section(Layout*);
424 // Return true if the symbol may need a COPY relocation.
425 // References from an executable object to non-function symbols
426 // defined in a dynamic object may need a COPY relocation.
428 may_need_copy_reloc(Symbol* gsym)
430 return (gsym->type() != elfcpp::STT_ARM_TFUNC
431 && gsym->may_need_copy_reloc());
434 // Add a potential copy relocation.
436 copy_reloc(Symbol_table* symtab, Layout* layout,
437 Sized_relobj<32, big_endian>* object,
438 unsigned int shndx, Output_section* output_section,
439 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
441 this->copy_relocs_.copy_reloc(symtab, layout,
442 symtab->get_sized_symbol<32>(sym),
443 object, shndx, output_section, reloc,
444 this->rel_dyn_section(layout));
447 // Information about this specific target which we pass to the
448 // general Target structure.
449 static const Target::Target_info arm_info;
451 // The types of GOT entries needed for this platform.
454 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
458 Output_data_got<32, big_endian>* got_;
460 Output_data_plt_arm<big_endian>* plt_;
461 // The GOT PLT section.
462 Output_data_space* got_plt_;
463 // The dynamic reloc section.
464 Reloc_section* rel_dyn_;
465 // Relocs saved to avoid a COPY reloc.
466 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
467 // Space for variables copied with a COPY reloc.
468 Output_data_space* dynbss_;
471 template<bool big_endian>
472 const Target::Target_info Target_arm<big_endian>::arm_info =
475 big_endian, // is_big_endian
476 elfcpp::EM_ARM, // machine_code
477 false, // has_make_symbol
478 false, // has_resolve
479 false, // has_code_fill
480 true, // is_default_stack_executable
482 "/usr/lib/libc.so.1", // dynamic_linker
483 0x8000, // default_text_segment_address
484 0x1000, // abi_pagesize (overridable by -z max-page-size)
485 0x1000, // common_pagesize (overridable by -z common-page-size)
486 elfcpp::SHN_UNDEF, // small_common_shndx
487 elfcpp::SHN_UNDEF, // large_common_shndx
488 0, // small_common_section_flags
489 0 // large_common_section_flags
492 // Arm relocate functions class
495 template<bool big_endian>
496 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
501 STATUS_OKAY, // No error during relocation.
502 STATUS_OVERFLOW, // Relocation oveflow.
503 STATUS_BAD_RELOC // Relocation cannot be applied.
507 typedef Relocate_functions<32, big_endian> Base;
508 typedef Arm_relocate_functions<big_endian> This;
510 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
511 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
512 // to distinguish ARM and THUMB functions and it is treated specially.
513 static inline Symbol_value<32>::Value
514 arm_symbol_value (const Sized_relobj<32, big_endian> *object,
515 const Symbol_value<32>* psymval,
516 Symbol_value<32>::Value addend,
519 typedef Symbol_value<32>::Value Valtype;
523 Valtype raw = psymval->value(object, 0);
524 Valtype thumb_bit = raw & 1;
525 return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
528 return psymval->value(object, addend);
531 // Encoding of imm16 argument for movt and movw ARM instructions
534 // imm16 := imm4 | imm12
536 // f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0
537 // +-------+---------------+-------+-------+-----------------------+
538 // | | |imm4 | |imm12 |
539 // +-------+---------------+-------+-------+-----------------------+
541 // Extract the relocation addend from VAL based on the ARM
542 // instruction encoding described above.
543 static inline typename elfcpp::Swap<32, big_endian>::Valtype
544 extract_arm_movw_movt_addend(
545 typename elfcpp::Swap<32, big_endian>::Valtype val)
547 // According to the Elf ABI for ARM Architecture the immediate
548 // field is sign-extended to form the addend.
549 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
552 // Insert X into VAL based on the ARM instruction encoding described
554 static inline typename elfcpp::Swap<32, big_endian>::Valtype
555 insert_val_arm_movw_movt(
556 typename elfcpp::Swap<32, big_endian>::Valtype val,
557 typename elfcpp::Swap<32, big_endian>::Valtype x)
561 val |= (x & 0xf000) << 4;
565 // Encoding of imm16 argument for movt and movw Thumb2 instructions
568 // imm16 := imm4 | i | imm3 | imm8
570 // f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0
571 // +---------+-+-----------+-------++-+-----+-------+---------------+
572 // | |i| |imm4 || |imm3 | |imm8 |
573 // +---------+-+-----------+-------++-+-----+-------+---------------+
575 // Extract the relocation addend from VAL based on the Thumb2
576 // instruction encoding described above.
577 static inline typename elfcpp::Swap<32, big_endian>::Valtype
578 extract_thumb_movw_movt_addend(
579 typename elfcpp::Swap<32, big_endian>::Valtype val)
581 // According to the Elf ABI for ARM Architecture the immediate
582 // field is sign-extended to form the addend.
583 return utils::sign_extend<16>(((val >> 4) & 0xf000)
584 | ((val >> 15) & 0x0800)
585 | ((val >> 4) & 0x0700)
589 // Insert X into VAL based on the Thumb2 instruction encoding
591 static inline typename elfcpp::Swap<32, big_endian>::Valtype
592 insert_val_thumb_movw_movt(
593 typename elfcpp::Swap<32, big_endian>::Valtype val,
594 typename elfcpp::Swap<32, big_endian>::Valtype x)
597 val |= (x & 0xf000) << 4;
598 val |= (x & 0x0800) << 15;
599 val |= (x & 0x0700) << 4;
604 // FIXME: This probably only works for Android on ARM v5te. We should
605 // following GNU ld for the general case.
606 template<unsigned r_type>
607 static inline typename This::Status
608 arm_branch_common(unsigned char *view,
609 const Sized_relobj<32, big_endian>* object,
610 const Symbol_value<32>* psymval,
611 elfcpp::Elf_types<32>::Elf_Addr address,
614 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
615 Valtype* wv = reinterpret_cast<Valtype*>(view);
616 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
618 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
619 && ((val & 0x0f000000UL) == 0x0a000000UL);
620 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
621 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
622 && ((val & 0x0f000000UL) == 0x0b000000UL);
623 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
624 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
626 if (r_type == elfcpp::R_ARM_CALL)
628 if (!insn_is_uncond_bl && !insn_is_blx)
629 return This::STATUS_BAD_RELOC;
631 else if (r_type == elfcpp::R_ARM_JUMP24)
633 if (!insn_is_b && !insn_is_cond_bl)
634 return This::STATUS_BAD_RELOC;
636 else if (r_type == elfcpp::R_ARM_PLT32)
638 if (!insn_is_any_branch)
639 return This::STATUS_BAD_RELOC;
644 Valtype addend = utils::sign_extend<26>(val << 2);
645 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
648 // If target has thumb bit set, we need to either turn the BL
649 // into a BLX (for ARMv5 or above) or generate a stub.
653 if (insn_is_uncond_bl)
654 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
656 return This::STATUS_BAD_RELOC;
659 gold_assert(!insn_is_blx);
661 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
662 elfcpp::Swap<32, big_endian>::writeval(wv, val);
663 return (utils::has_overflow<26>(x)
664 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
670 static inline typename This::Status
671 abs8(unsigned char *view,
672 const Sized_relobj<32, big_endian>* object,
673 const Symbol_value<32>* psymval)
675 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
676 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
677 Valtype* wv = reinterpret_cast<Valtype*>(view);
678 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
679 Reltype addend = utils::sign_extend<8>(val);
680 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
681 val = utils::bit_select(val, x, 0xffU);
682 elfcpp::Swap<8, big_endian>::writeval(wv, val);
683 return (utils::has_signed_unsigned_overflow<8>(x)
684 ? This::STATUS_OVERFLOW
685 : This::STATUS_OKAY);
688 // R_ARM_THM_ABS5: S + A
689 static inline typename This::Status
690 thm_abs5(unsigned char *view,
691 const Sized_relobj<32, big_endian>* object,
692 const Symbol_value<32>* psymval)
694 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
695 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
696 Valtype* wv = reinterpret_cast<Valtype*>(view);
697 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
698 Reltype addend = (val & 0x7e0U) >> 6;
699 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
700 val = utils::bit_select(val, x << 6, 0x7e0U);
701 elfcpp::Swap<16, big_endian>::writeval(wv, val);
702 return (utils::has_overflow<5>(x)
703 ? This::STATUS_OVERFLOW
704 : This::STATUS_OKAY);
707 // R_ARM_ABS12: S + A
708 static inline typename This::Status
709 abs12(unsigned char *view,
710 const Sized_relobj<32, big_endian>* object,
711 const Symbol_value<32>* psymval)
713 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
714 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
715 Valtype* wv = reinterpret_cast<Valtype*>(view);
716 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
717 Reltype addend = val & 0x0fffU;
718 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
719 val = utils::bit_select(val, x, 0x0fffU);
720 elfcpp::Swap<32, big_endian>::writeval(wv, val);
721 return (utils::has_overflow<12>(x)
722 ? This::STATUS_OVERFLOW
723 : This::STATUS_OKAY);
726 // R_ARM_ABS16: S + A
727 static inline typename This::Status
728 abs16(unsigned char *view,
729 const Sized_relobj<32, big_endian>* object,
730 const Symbol_value<32>* psymval)
732 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
733 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
734 Valtype* wv = reinterpret_cast<Valtype*>(view);
735 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
736 Reltype addend = utils::sign_extend<16>(val);
737 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
738 val = utils::bit_select(val, x, 0xffffU);
739 elfcpp::Swap<16, big_endian>::writeval(wv, val);
740 return (utils::has_signed_unsigned_overflow<16>(x)
741 ? This::STATUS_OVERFLOW
742 : This::STATUS_OKAY);
745 // R_ARM_ABS32: (S + A) | T
746 static inline typename This::Status
747 abs32(unsigned char *view,
748 const Sized_relobj<32, big_endian>* object,
749 const Symbol_value<32>* psymval,
752 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
753 Valtype* wv = reinterpret_cast<Valtype*>(view);
754 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
755 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
756 elfcpp::Swap<32, big_endian>::writeval(wv, x);
757 return This::STATUS_OKAY;
760 // R_ARM_REL32: (S + A) | T - P
761 static inline typename This::Status
762 rel32(unsigned char *view,
763 const Sized_relobj<32, big_endian>* object,
764 const Symbol_value<32>* psymval,
765 elfcpp::Elf_types<32>::Elf_Addr address,
768 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
769 Valtype* wv = reinterpret_cast<Valtype*>(view);
770 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
771 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
773 elfcpp::Swap<32, big_endian>::writeval(wv, x);
774 return This::STATUS_OKAY;
777 // R_ARM_THM_CALL: (S + A) | T - P
778 static inline typename This::Status
779 thm_call(unsigned char *view,
780 const Sized_relobj<32, big_endian>* object,
781 const Symbol_value<32>* psymval,
782 elfcpp::Elf_types<32>::Elf_Addr address,
785 // A thumb call consists of two instructions.
786 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
787 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
788 Valtype* wv = reinterpret_cast<Valtype*>(view);
789 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
790 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
791 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
792 gold_assert((lo & 0xf800) == 0xf800);
793 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
794 | ((lo & 0x7ff) << 1));
795 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
798 // If target has no thumb bit set, we need to either turn the BL
799 // into a BLX (for ARMv5 or above) or generate a stub.
802 // This only works for ARMv5 and above with interworking enabled.
805 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
806 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
807 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
808 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
809 return (utils::has_overflow<23>(x)
810 ? This::STATUS_OVERFLOW
811 : This::STATUS_OKAY);
814 // R_ARM_BASE_PREL: B(S) + A - P
815 static inline typename This::Status
816 base_prel(unsigned char* view,
817 elfcpp::Elf_types<32>::Elf_Addr origin,
818 elfcpp::Elf_types<32>::Elf_Addr address)
820 Base::rel32(view, origin - address);
824 // R_ARM_BASE_ABS: B(S) + A
825 static inline typename This::Status
826 base_abs(unsigned char* view,
827 elfcpp::Elf_types<32>::Elf_Addr origin)
829 Base::rel32(view, origin);
833 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
834 static inline typename This::Status
835 got_brel(unsigned char* view,
836 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
838 Base::rel32(view, got_offset);
839 return This::STATUS_OKAY;
842 // R_ARM_GOT_PREL: GOT(S) + A – P
843 static inline typename This::Status
844 got_prel(unsigned char* view,
845 typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
846 elfcpp::Elf_types<32>::Elf_Addr address)
848 Base::rel32(view, got_offset - address);
849 return This::STATUS_OKAY;
852 // R_ARM_PLT32: (S + A) | T - P
853 static inline typename This::Status
854 plt32(unsigned char *view,
855 const Sized_relobj<32, big_endian>* object,
856 const Symbol_value<32>* psymval,
857 elfcpp::Elf_types<32>::Elf_Addr address,
860 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
861 address, has_thumb_bit);
864 // R_ARM_CALL: (S + A) | T - P
865 static inline typename This::Status
866 call(unsigned char *view,
867 const Sized_relobj<32, big_endian>* object,
868 const Symbol_value<32>* psymval,
869 elfcpp::Elf_types<32>::Elf_Addr address,
872 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
873 address, has_thumb_bit);
876 // R_ARM_JUMP24: (S + A) | T - P
877 static inline typename This::Status
878 jump24(unsigned char *view,
879 const Sized_relobj<32, big_endian>* object,
880 const Symbol_value<32>* psymval,
881 elfcpp::Elf_types<32>::Elf_Addr address,
884 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
885 address, has_thumb_bit);
888 // R_ARM_PREL: (S + A) | T - P
889 static inline typename This::Status
890 prel31(unsigned char *view,
891 const Sized_relobj<32, big_endian>* object,
892 const Symbol_value<32>* psymval,
893 elfcpp::Elf_types<32>::Elf_Addr address,
896 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
897 Valtype* wv = reinterpret_cast<Valtype*>(view);
898 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
899 Valtype addend = utils::sign_extend<31>(val);
900 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
902 val = utils::bit_select(val, x, 0x7fffffffU);
903 elfcpp::Swap<32, big_endian>::writeval(wv, val);
904 return (utils::has_overflow<31>(x) ?
905 This::STATUS_OVERFLOW : This::STATUS_OKAY);
908 // R_ARM_MOVW_ABS_NC: (S + A) | T
909 static inline typename This::Status
910 movw_abs_nc(unsigned char *view,
911 const Sized_relobj<32, big_endian>* object,
912 const Symbol_value<32>* psymval,
915 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
916 Valtype* wv = reinterpret_cast<Valtype*>(view);
917 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
918 Valtype addend = This::extract_arm_movw_movt_addend(val);
919 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
920 val = This::insert_val_arm_movw_movt(val, x);
921 elfcpp::Swap<32, big_endian>::writeval(wv, val);
922 return This::STATUS_OKAY;
925 // R_ARM_MOVT_ABS: S + A
926 static inline typename This::Status
927 movt_abs(unsigned char *view,
928 const Sized_relobj<32, big_endian>* object,
929 const Symbol_value<32>* psymval)
931 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
932 Valtype* wv = reinterpret_cast<Valtype*>(view);
933 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
934 Valtype addend = This::extract_arm_movw_movt_addend(val);
935 Valtype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
936 val = This::insert_val_arm_movw_movt(val, x);
937 elfcpp::Swap<32, big_endian>::writeval(wv, val);
938 return This::STATUS_OKAY;
941 // R_ARM_THM_MOVW_ABS_NC: S + A | T
942 static inline typename This::Status
943 thm_movw_abs_nc(unsigned char *view,
944 const Sized_relobj<32, big_endian>* object,
945 const Symbol_value<32>* psymval,
948 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
949 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
950 Valtype* wv = reinterpret_cast<Valtype*>(view);
951 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
952 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
953 Reltype addend = extract_thumb_movw_movt_addend(val);
954 Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
955 val = This::insert_val_thumb_movw_movt(val, x);
956 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
957 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
958 return This::STATUS_OKAY;
961 // R_ARM_THM_MOVT_ABS: S + A
962 static inline typename This::Status
963 thm_movt_abs(unsigned char *view,
964 const Sized_relobj<32, big_endian>* object,
965 const Symbol_value<32>* psymval)
967 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
968 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
969 Valtype* wv = reinterpret_cast<Valtype*>(view);
970 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
971 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
972 Reltype addend = This::extract_thumb_movw_movt_addend(val);
973 Reltype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
974 val = This::insert_val_thumb_movw_movt(val, x);
975 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
976 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
977 return This::STATUS_OKAY;
980 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
981 static inline typename This::Status
982 movw_prel_nc(unsigned char *view,
983 const Sized_relobj<32, big_endian>* object,
984 const Symbol_value<32>* psymval,
985 elfcpp::Elf_types<32>::Elf_Addr address,
988 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
989 Valtype* wv = reinterpret_cast<Valtype*>(view);
990 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
991 Valtype addend = This::extract_arm_movw_movt_addend(val);
992 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
994 val = This::insert_val_arm_movw_movt(val, x);
995 elfcpp::Swap<32, big_endian>::writeval(wv, val);
996 return This::STATUS_OKAY;
999 // R_ARM_MOVT_PREL: S + A - P
1000 static inline typename This::Status
1001 movt_prel(unsigned char *view,
1002 const Sized_relobj<32, big_endian>* object,
1003 const Symbol_value<32>* psymval,
1004 elfcpp::Elf_types<32>::Elf_Addr address)
1006 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1007 Valtype* wv = reinterpret_cast<Valtype*>(view);
1008 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1009 Valtype addend = This::extract_arm_movw_movt_addend(val);
1010 Valtype x = (This::arm_symbol_value(object, psymval, addend, 0)
1012 val = This::insert_val_arm_movw_movt(val, x);
1013 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1014 return This::STATUS_OKAY;
1017 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
1018 static inline typename This::Status
1019 thm_movw_prel_nc(unsigned char *view,
1020 const Sized_relobj<32, big_endian>* object,
1021 const Symbol_value<32>* psymval,
1022 elfcpp::Elf_types<32>::Elf_Addr address,
1025 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1026 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1027 Valtype* wv = reinterpret_cast<Valtype*>(view);
1028 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1029 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1030 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1031 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1033 val = This::insert_val_thumb_movw_movt(val, x);
1034 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1035 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1036 return This::STATUS_OKAY;
1039 // R_ARM_THM_MOVT_PREL: S + A - P
1040 static inline typename This::Status
1041 thm_movt_prel(unsigned char *view,
1042 const Sized_relobj<32, big_endian>* object,
1043 const Symbol_value<32>* psymval,
1044 elfcpp::Elf_types<32>::Elf_Addr address)
1046 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1047 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1048 Valtype* wv = reinterpret_cast<Valtype*>(view);
1049 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1050 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1051 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1052 Reltype x = (This::arm_symbol_value(object, psymval, addend, 0)
1054 val = This::insert_val_thumb_movw_movt(val, x);
1055 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1056 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1057 return This::STATUS_OKAY;
1061 // Get the GOT section, creating it if necessary.
1063 template<bool big_endian>
1064 Output_data_got<32, big_endian>*
1065 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
1067 if (this->got_ == NULL)
1069 gold_assert(symtab != NULL && layout != NULL);
1071 this->got_ = new Output_data_got<32, big_endian>();
1074 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1076 | elfcpp::SHF_WRITE),
1080 // The old GNU linker creates a .got.plt section. We just
1081 // create another set of data in the .got section. Note that we
1082 // always create a PLT if we create a GOT, although the PLT
1084 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
1085 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1087 | elfcpp::SHF_WRITE),
1091 // The first three entries are reserved.
1092 this->got_plt_->set_current_data_size(3 * 4);
1094 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1095 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1097 0, 0, elfcpp::STT_OBJECT,
1099 elfcpp::STV_HIDDEN, 0,
1105 // Get the dynamic reloc section, creating it if necessary.
1107 template<bool big_endian>
1108 typename Target_arm<big_endian>::Reloc_section*
1109 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
1111 if (this->rel_dyn_ == NULL)
1113 gold_assert(layout != NULL);
1114 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
1115 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1116 elfcpp::SHF_ALLOC, this->rel_dyn_);
1118 return this->rel_dyn_;
1121 // A class to handle the PLT data.
1123 template<bool big_endian>
1124 class Output_data_plt_arm : public Output_section_data
1127 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1130 Output_data_plt_arm(Layout*, Output_data_space*);
1132 // Add an entry to the PLT.
1134 add_entry(Symbol* gsym);
1136 // Return the .rel.plt section data.
1137 const Reloc_section*
1139 { return this->rel_; }
1143 do_adjust_output_section(Output_section* os);
1145 // Write to a map file.
1147 do_print_to_mapfile(Mapfile* mapfile) const
1148 { mapfile->print_output_data(this, _("** PLT")); }
1151 // Template for the first PLT entry.
1152 static const uint32_t first_plt_entry[5];
1154 // Template for subsequent PLT entries.
1155 static const uint32_t plt_entry[3];
1157 // Set the final size.
1159 set_final_data_size()
1161 this->set_data_size(sizeof(first_plt_entry)
1162 + this->count_ * sizeof(plt_entry));
1165 // Write out the PLT data.
1167 do_write(Output_file*);
1169 // The reloc section.
1170 Reloc_section* rel_;
1171 // The .got.plt section.
1172 Output_data_space* got_plt_;
1173 // The number of PLT entries.
1174 unsigned int count_;
1177 // Create the PLT section. The ordinary .got section is an argument,
1178 // since we need to refer to the start. We also create our own .got
1179 // section just for PLT entries.
1181 template<bool big_endian>
1182 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
1183 Output_data_space* got_plt)
1184 : Output_section_data(4), got_plt_(got_plt), count_(0)
1186 this->rel_ = new Reloc_section(false);
1187 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1188 elfcpp::SHF_ALLOC, this->rel_);
1191 template<bool big_endian>
1193 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
1198 // Add an entry to the PLT.
1200 template<bool big_endian>
1202 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
1204 gold_assert(!gsym->has_plt_offset());
1206 // Note that when setting the PLT offset we skip the initial
1207 // reserved PLT entry.
1208 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
1209 + sizeof(first_plt_entry));
1213 section_offset_type got_offset = this->got_plt_->current_data_size();
1215 // Every PLT entry needs a GOT entry which points back to the PLT
1216 // entry (this will be changed by the dynamic linker, normally
1217 // lazily when the function is called).
1218 this->got_plt_->set_current_data_size(got_offset + 4);
1220 // Every PLT entry needs a reloc.
1221 gsym->set_needs_dynsym_entry();
1222 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
1225 // Note that we don't need to save the symbol. The contents of the
1226 // PLT are independent of which symbols are used. The symbols only
1227 // appear in the relocations.
1231 // FIXME: This is not very flexible. Right now this has only been tested
1232 // on armv5te. If we are to support additional architecture features like
1233 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
1235 // The first entry in the PLT.
1236 template<bool big_endian>
1237 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
1239 0xe52de004, // str lr, [sp, #-4]!
1240 0xe59fe004, // ldr lr, [pc, #4]
1241 0xe08fe00e, // add lr, pc, lr
1242 0xe5bef008, // ldr pc, [lr, #8]!
1243 0x00000000, // &GOT[0] - .
1246 // Subsequent entries in the PLT.
1248 template<bool big_endian>
1249 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
1251 0xe28fc600, // add ip, pc, #0xNN00000
1252 0xe28cca00, // add ip, ip, #0xNN000
1253 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
1256 // Write out the PLT. This uses the hand-coded instructions above,
1257 // and adjusts them as needed. This is all specified by the arm ELF
1258 // Processor Supplement.
1260 template<bool big_endian>
1262 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
1264 const off_t offset = this->offset();
1265 const section_size_type oview_size =
1266 convert_to_section_size_type(this->data_size());
1267 unsigned char* const oview = of->get_output_view(offset, oview_size);
1269 const off_t got_file_offset = this->got_plt_->offset();
1270 const section_size_type got_size =
1271 convert_to_section_size_type(this->got_plt_->data_size());
1272 unsigned char* const got_view = of->get_output_view(got_file_offset,
1274 unsigned char* pov = oview;
1276 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
1277 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
1279 // Write first PLT entry. All but the last word are constants.
1280 const size_t num_first_plt_words = (sizeof(first_plt_entry)
1281 / sizeof(plt_entry[0]));
1282 for (size_t i = 0; i < num_first_plt_words - 1; i++)
1283 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
1284 // Last word in first PLT entry is &GOT[0] - .
1285 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
1286 got_address - (plt_address + 16));
1287 pov += sizeof(first_plt_entry);
1289 unsigned char* got_pov = got_view;
1291 memset(got_pov, 0, 12);
1294 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
1295 unsigned int plt_offset = sizeof(first_plt_entry);
1296 unsigned int plt_rel_offset = 0;
1297 unsigned int got_offset = 12;
1298 const unsigned int count = this->count_;
1299 for (unsigned int i = 0;
1302 pov += sizeof(plt_entry),
1304 plt_offset += sizeof(plt_entry),
1305 plt_rel_offset += rel_size,
1308 // Set and adjust the PLT entry itself.
1309 int32_t offset = ((got_address + got_offset)
1310 - (plt_address + plt_offset + 8));
1312 gold_assert(offset >= 0 && offset < 0x0fffffff);
1313 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
1314 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
1315 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
1316 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
1317 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
1318 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
1320 // Set the entry in the GOT.
1321 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
1324 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1325 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1327 of->write_output_view(offset, oview_size, oview);
1328 of->write_output_view(got_file_offset, got_size, got_view);
1331 // Create a PLT entry for a global symbol.
1333 template<bool big_endian>
1335 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
1338 if (gsym->has_plt_offset())
1341 if (this->plt_ == NULL)
1343 // Create the GOT sections first.
1344 this->got_section(symtab, layout);
1346 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
1347 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1349 | elfcpp::SHF_EXECINSTR),
1352 this->plt_->add_entry(gsym);
1355 // Report an unsupported relocation against a local symbol.
1357 template<bool big_endian>
1359 Target_arm<big_endian>::Scan::unsupported_reloc_local(
1360 Sized_relobj<32, big_endian>* object,
1361 unsigned int r_type)
1363 gold_error(_("%s: unsupported reloc %u against local symbol"),
1364 object->name().c_str(), r_type);
1367 // We are about to emit a dynamic relocation of type R_TYPE. If the
1368 // dynamic linker does not support it, issue an error. The GNU linker
1369 // only issues a non-PIC error for an allocated read-only section.
1370 // Here we know the section is allocated, but we don't know that it is
1371 // read-only. But we check for all the relocation types which the
1372 // glibc dynamic linker supports, so it seems appropriate to issue an
1373 // error even if the section is not read-only.
1375 template<bool big_endian>
1377 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
1378 unsigned int r_type)
1382 // These are the relocation types supported by glibc for ARM.
1383 case elfcpp::R_ARM_RELATIVE:
1384 case elfcpp::R_ARM_COPY:
1385 case elfcpp::R_ARM_GLOB_DAT:
1386 case elfcpp::R_ARM_JUMP_SLOT:
1387 case elfcpp::R_ARM_ABS32:
1388 case elfcpp::R_ARM_ABS32_NOI:
1389 case elfcpp::R_ARM_PC24:
1390 // FIXME: The following 3 types are not supported by Android's dynamic
1392 case elfcpp::R_ARM_TLS_DTPMOD32:
1393 case elfcpp::R_ARM_TLS_DTPOFF32:
1394 case elfcpp::R_ARM_TLS_TPOFF32:
1398 // This prevents us from issuing more than one error per reloc
1399 // section. But we can still wind up issuing more than one
1400 // error per object file.
1401 if (this->issued_non_pic_error_)
1403 object->error(_("requires unsupported dynamic reloc; "
1404 "recompile with -fPIC"));
1405 this->issued_non_pic_error_ = true;
1408 case elfcpp::R_ARM_NONE:
1413 // Scan a relocation for a local symbol.
1414 // FIXME: This only handles a subset of relocation types used by Android
1415 // on ARM v5te devices.
1417 template<bool big_endian>
1419 Target_arm<big_endian>::Scan::local(const General_options&,
1420 Symbol_table* symtab,
1423 Sized_relobj<32, big_endian>* object,
1424 unsigned int data_shndx,
1425 Output_section* output_section,
1426 const elfcpp::Rel<32, big_endian>& reloc,
1427 unsigned int r_type,
1428 const elfcpp::Sym<32, big_endian>&)
1430 r_type = get_real_reloc_type(r_type);
1433 case elfcpp::R_ARM_NONE:
1436 case elfcpp::R_ARM_ABS32:
1437 case elfcpp::R_ARM_ABS32_NOI:
1438 // If building a shared library (or a position-independent
1439 // executable), we need to create a dynamic relocation for
1440 // this location. The relocation applied at link time will
1441 // apply the link-time value, so we flag the location with
1442 // an R_ARM_RELATIVE relocation so the dynamic loader can
1443 // relocate it easily.
1444 if (parameters->options().output_is_position_independent())
1446 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1447 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1448 // If we are to add more other reloc types than R_ARM_ABS32,
1449 // we need to add check_non_pic(object, r_type) here.
1450 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
1451 output_section, data_shndx,
1452 reloc.get_r_offset());
1456 case elfcpp::R_ARM_REL32:
1457 case elfcpp::R_ARM_THM_CALL:
1458 case elfcpp::R_ARM_CALL:
1459 case elfcpp::R_ARM_PREL31:
1460 case elfcpp::R_ARM_JUMP24:
1461 case elfcpp::R_ARM_PLT32:
1462 case elfcpp::R_ARM_THM_ABS5:
1463 case elfcpp::R_ARM_ABS8:
1464 case elfcpp::R_ARM_ABS12:
1465 case elfcpp::R_ARM_ABS16:
1466 case elfcpp::R_ARM_BASE_ABS:
1467 case elfcpp::R_ARM_MOVW_ABS_NC:
1468 case elfcpp::R_ARM_MOVT_ABS:
1469 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
1470 case elfcpp::R_ARM_THM_MOVT_ABS:
1471 case elfcpp::R_ARM_MOVW_PREL_NC:
1472 case elfcpp::R_ARM_MOVT_PREL:
1473 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
1474 case elfcpp::R_ARM_THM_MOVT_PREL:
1477 case elfcpp::R_ARM_GOTOFF32:
1478 // We need a GOT section:
1479 target->got_section(symtab, layout);
1482 case elfcpp::R_ARM_BASE_PREL:
1483 // FIXME: What about this?
1486 case elfcpp::R_ARM_GOT_BREL:
1487 case elfcpp::R_ARM_GOT_PREL:
1489 // The symbol requires a GOT entry.
1490 Output_data_got<32, big_endian>* got =
1491 target->got_section(symtab, layout);
1492 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1493 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1495 // If we are generating a shared object, we need to add a
1496 // dynamic RELATIVE relocation for this symbol's GOT entry.
1497 if (parameters->options().output_is_position_independent())
1499 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1500 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1501 rel_dyn->add_local_relative(
1502 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
1503 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
1509 case elfcpp::R_ARM_TARGET1:
1510 // This should have been mapped to another type already.
1512 case elfcpp::R_ARM_COPY:
1513 case elfcpp::R_ARM_GLOB_DAT:
1514 case elfcpp::R_ARM_JUMP_SLOT:
1515 case elfcpp::R_ARM_RELATIVE:
1516 // These are relocations which should only be seen by the
1517 // dynamic linker, and should never be seen here.
1518 gold_error(_("%s: unexpected reloc %u in object file"),
1519 object->name().c_str(), r_type);
1523 unsupported_reloc_local(object, r_type);
1528 // Report an unsupported relocation against a global symbol.
1530 template<bool big_endian>
1532 Target_arm<big_endian>::Scan::unsupported_reloc_global(
1533 Sized_relobj<32, big_endian>* object,
1534 unsigned int r_type,
1537 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1538 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1541 // Scan a relocation for a global symbol.
1542 // FIXME: This only handles a subset of relocation types used by Android
1543 // on ARM v5te devices.
1545 template<bool big_endian>
1547 Target_arm<big_endian>::Scan::global(const General_options&,
1548 Symbol_table* symtab,
1551 Sized_relobj<32, big_endian>* object,
1552 unsigned int data_shndx,
1553 Output_section* output_section,
1554 const elfcpp::Rel<32, big_endian>& reloc,
1555 unsigned int r_type,
1558 r_type = get_real_reloc_type(r_type);
1561 case elfcpp::R_ARM_NONE:
1564 case elfcpp::R_ARM_ABS32:
1565 case elfcpp::R_ARM_ABS32_NOI:
1567 // Make a dynamic relocation if necessary.
1568 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1570 if (target->may_need_copy_reloc(gsym))
1572 target->copy_reloc(symtab, layout, object,
1573 data_shndx, output_section, gsym, reloc);
1575 else if (gsym->can_use_relative_reloc(false))
1577 // If we are to add more other reloc types than R_ARM_ABS32,
1578 // we need to add check_non_pic(object, r_type) here.
1579 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1580 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
1581 output_section, object,
1582 data_shndx, reloc.get_r_offset());
1586 // If we are to add more other reloc types than R_ARM_ABS32,
1587 // we need to add check_non_pic(object, r_type) here.
1588 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1589 rel_dyn->add_global(gsym, r_type, output_section, object,
1590 data_shndx, reloc.get_r_offset());
1596 case elfcpp::R_ARM_MOVW_ABS_NC:
1597 case elfcpp::R_ARM_MOVT_ABS:
1598 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
1599 case elfcpp::R_ARM_THM_MOVT_ABS:
1600 case elfcpp::R_ARM_MOVW_PREL_NC:
1601 case elfcpp::R_ARM_MOVT_PREL:
1602 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
1603 case elfcpp::R_ARM_THM_MOVT_PREL:
1606 case elfcpp::R_ARM_THM_ABS5:
1607 case elfcpp::R_ARM_ABS8:
1608 case elfcpp::R_ARM_ABS12:
1609 case elfcpp::R_ARM_ABS16:
1610 case elfcpp::R_ARM_BASE_ABS:
1612 // No dynamic relocs of this kinds.
1613 // Report the error in case of PIC.
1614 int flags = Symbol::NON_PIC_REF;
1615 if (gsym->type() == elfcpp::STT_FUNC
1616 || gsym->type() == elfcpp::STT_ARM_TFUNC)
1617 flags |= Symbol::FUNCTION_CALL;
1618 if (gsym->needs_dynamic_reloc(flags))
1619 check_non_pic(object, r_type);
1623 case elfcpp::R_ARM_REL32:
1624 case elfcpp::R_ARM_PREL31:
1626 // Make a dynamic relocation if necessary.
1627 int flags = Symbol::NON_PIC_REF;
1628 if (gsym->needs_dynamic_reloc(flags))
1630 if (target->may_need_copy_reloc(gsym))
1632 target->copy_reloc(symtab, layout, object,
1633 data_shndx, output_section, gsym, reloc);
1637 check_non_pic(object, r_type);
1638 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1639 rel_dyn->add_global(gsym, r_type, output_section, object,
1640 data_shndx, reloc.get_r_offset());
1646 case elfcpp::R_ARM_JUMP24:
1647 case elfcpp::R_ARM_THM_CALL:
1648 case elfcpp::R_ARM_CALL:
1650 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
1651 target->make_plt_entry(symtab, layout, gsym);
1652 // Make a dynamic relocation if necessary.
1653 int flags = Symbol::NON_PIC_REF;
1654 if (gsym->type() == elfcpp::STT_FUNC
1655 || gsym->type() == elfcpp::STT_ARM_TFUNC)
1656 flags |= Symbol::FUNCTION_CALL;
1657 if (gsym->needs_dynamic_reloc(flags))
1659 if (target->may_need_copy_reloc(gsym))
1661 target->copy_reloc(symtab, layout, object,
1662 data_shndx, output_section, gsym,
1667 check_non_pic(object, r_type);
1668 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1669 rel_dyn->add_global(gsym, r_type, output_section, object,
1670 data_shndx, reloc.get_r_offset());
1676 case elfcpp::R_ARM_PLT32:
1677 // If the symbol is fully resolved, this is just a relative
1678 // local reloc. Otherwise we need a PLT entry.
1679 if (gsym->final_value_is_known())
1681 // If building a shared library, we can also skip the PLT entry
1682 // if the symbol is defined in the output file and is protected
1684 if (gsym->is_defined()
1685 && !gsym->is_from_dynobj()
1686 && !gsym->is_preemptible())
1688 target->make_plt_entry(symtab, layout, gsym);
1691 case elfcpp::R_ARM_GOTOFF32:
1692 // We need a GOT section.
1693 target->got_section(symtab, layout);
1696 case elfcpp::R_ARM_BASE_PREL:
1697 // FIXME: What about this?
1700 case elfcpp::R_ARM_GOT_BREL:
1701 case elfcpp::R_ARM_GOT_PREL:
1703 // The symbol requires a GOT entry.
1704 Output_data_got<32, big_endian>* got =
1705 target->got_section(symtab, layout);
1706 if (gsym->final_value_is_known())
1707 got->add_global(gsym, GOT_TYPE_STANDARD);
1710 // If this symbol is not fully resolved, we need to add a
1711 // GOT entry with a dynamic relocation.
1712 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1713 if (gsym->is_from_dynobj()
1714 || gsym->is_undefined()
1715 || gsym->is_preemptible())
1716 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1717 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
1720 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1721 rel_dyn->add_global_relative(
1722 gsym, elfcpp::R_ARM_RELATIVE, got,
1723 gsym->got_offset(GOT_TYPE_STANDARD));
1729 case elfcpp::R_ARM_TARGET1:
1730 // This should have been mapped to another type already.
1732 case elfcpp::R_ARM_COPY:
1733 case elfcpp::R_ARM_GLOB_DAT:
1734 case elfcpp::R_ARM_JUMP_SLOT:
1735 case elfcpp::R_ARM_RELATIVE:
1736 // These are relocations which should only be seen by the
1737 // dynamic linker, and should never be seen here.
1738 gold_error(_("%s: unexpected reloc %u in object file"),
1739 object->name().c_str(), r_type);
1743 unsupported_reloc_global(object, r_type, gsym);
1748 // Process relocations for gc.
1750 template<bool big_endian>
1752 Target_arm<big_endian>::gc_process_relocs(const General_options& options,
1753 Symbol_table* symtab,
1755 Sized_relobj<32, big_endian>* object,
1756 unsigned int data_shndx,
1758 const unsigned char* prelocs,
1760 Output_section* output_section,
1761 bool needs_special_offset_handling,
1762 size_t local_symbol_count,
1763 const unsigned char* plocal_symbols)
1765 typedef Target_arm<big_endian> Arm;
1766 typedef typename Target_arm<big_endian>::Scan Scan;
1768 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
1778 needs_special_offset_handling,
1783 // Scan relocations for a section.
1785 template<bool big_endian>
1787 Target_arm<big_endian>::scan_relocs(const General_options& options,
1788 Symbol_table* symtab,
1790 Sized_relobj<32, big_endian>* object,
1791 unsigned int data_shndx,
1792 unsigned int sh_type,
1793 const unsigned char* prelocs,
1795 Output_section* output_section,
1796 bool needs_special_offset_handling,
1797 size_t local_symbol_count,
1798 const unsigned char* plocal_symbols)
1800 typedef typename Target_arm<big_endian>::Scan Scan;
1801 if (sh_type == elfcpp::SHT_RELA)
1803 gold_error(_("%s: unsupported RELA reloc section"),
1804 object->name().c_str());
1808 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
1818 needs_special_offset_handling,
1823 // Finalize the sections.
1825 template<bool big_endian>
1827 Target_arm<big_endian>::do_finalize_sections(Layout* layout)
1829 // Fill in some more dynamic tags.
1830 Output_data_dynamic* const odyn = layout->dynamic_data();
1833 if (this->got_plt_ != NULL)
1834 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1836 if (this->plt_ != NULL)
1838 const Output_data* od = this->plt_->rel_plt();
1839 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1840 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1841 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1844 if (this->rel_dyn_ != NULL)
1846 const Output_data* od = this->rel_dyn_;
1847 odyn->add_section_address(elfcpp::DT_REL, od);
1848 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1849 odyn->add_constant(elfcpp::DT_RELENT,
1850 elfcpp::Elf_sizes<32>::rel_size);
1853 if (!parameters->options().shared())
1855 // The value of the DT_DEBUG tag is filled in by the dynamic
1856 // linker at run time, and used by the debugger.
1857 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1861 // Emit any relocs we saved in an attempt to avoid generating COPY
1863 if (this->copy_relocs_.any_saved_relocs())
1864 this->copy_relocs_.emit(this->rel_dyn_section(layout));
1866 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
1867 // the .ARM.exidx section.
1868 if (!layout->script_options()->saw_phdrs_clause()
1869 && !parameters->options().relocatable())
1871 Output_section* exidx_section =
1872 layout->find_output_section(".ARM.exidx");
1874 if (exidx_section != NULL
1875 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
1877 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
1879 Output_segment* exidx_segment =
1880 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
1881 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
1886 // Return whether a direct absolute static relocation needs to be applied.
1887 // In cases where Scan::local() or Scan::global() has created
1888 // a dynamic relocation other than R_ARM_RELATIVE, the addend
1889 // of the relocation is carried in the data, and we must not
1890 // apply the static relocation.
1892 template<bool big_endian>
1894 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
1895 const Sized_symbol<32>* gsym,
1898 Output_section* output_section)
1900 // If the output section is not allocated, then we didn't call
1901 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1903 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1906 // For local symbols, we will have created a non-RELATIVE dynamic
1907 // relocation only if (a) the output is position independent,
1908 // (b) the relocation is absolute (not pc- or segment-relative), and
1909 // (c) the relocation is not 32 bits wide.
1911 return !(parameters->options().output_is_position_independent()
1912 && (ref_flags & Symbol::ABSOLUTE_REF)
1915 // For global symbols, we use the same helper routines used in the
1916 // scan pass. If we did not create a dynamic relocation, or if we
1917 // created a RELATIVE dynamic relocation, we should apply the static
1919 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1920 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1921 && gsym->can_use_relative_reloc(ref_flags
1922 & Symbol::FUNCTION_CALL);
1923 return !has_dyn || is_rel;
1926 // Perform a relocation.
1928 template<bool big_endian>
1930 Target_arm<big_endian>::Relocate::relocate(
1931 const Relocate_info<32, big_endian>* relinfo,
1933 Output_section *output_section,
1935 const elfcpp::Rel<32, big_endian>& rel,
1936 unsigned int r_type,
1937 const Sized_symbol<32>* gsym,
1938 const Symbol_value<32>* psymval,
1939 unsigned char* view,
1940 elfcpp::Elf_types<32>::Elf_Addr address,
1941 section_size_type /* view_size */ )
1943 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
1945 r_type = get_real_reloc_type(r_type);
1947 // If this the symbol may be a Thumb function, set thumb bit to 1.
1948 bool has_thumb_bit = ((gsym != NULL)
1949 && (gsym->type() == elfcpp::STT_FUNC
1950 || gsym->type() == elfcpp::STT_ARM_TFUNC));
1952 // Pick the value to use for symbols defined in shared objects.
1953 Symbol_value<32> symval;
1955 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
1957 symval.set_output_value(target->plt_section()->address()
1958 + gsym->plt_offset());
1963 const Sized_relobj<32, big_endian>* object = relinfo->object;
1965 // Get the GOT offset if needed.
1966 // The GOT pointer points to the end of the GOT section.
1967 // We need to subtract the size of the GOT section to get
1968 // the actual offset to use in the relocation.
1969 bool have_got_offset = false;
1970 unsigned int got_offset = 0;
1973 case elfcpp::R_ARM_GOT_BREL:
1974 case elfcpp::R_ARM_GOT_PREL:
1977 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1978 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1979 - target->got_size());
1983 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1984 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1985 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1986 - target->got_size());
1988 have_got_offset = true;
1995 typename Arm_relocate_functions::Status reloc_status =
1996 Arm_relocate_functions::STATUS_OKAY;
1999 case elfcpp::R_ARM_NONE:
2002 case elfcpp::R_ARM_ABS8:
2003 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2005 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
2008 case elfcpp::R_ARM_ABS12:
2009 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2011 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
2014 case elfcpp::R_ARM_ABS16:
2015 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2017 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
2020 case elfcpp::R_ARM_ABS32:
2021 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2023 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
2027 case elfcpp::R_ARM_ABS32_NOI:
2028 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2030 // No thumb bit for this relocation: (S + A)
2031 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
2035 case elfcpp::R_ARM_MOVW_ABS_NC:
2036 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2038 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
2042 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
2043 "a shared object; recompile with -fPIC"));
2046 case elfcpp::R_ARM_MOVT_ABS:
2047 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2049 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
2051 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
2052 "a shared object; recompile with -fPIC"));
2055 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
2056 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2058 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
2062 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
2063 "making a shared object; recompile with -fPIC"));
2066 case elfcpp::R_ARM_THM_MOVT_ABS:
2067 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2069 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
2072 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
2073 "making a shared object; recompile with -fPIC"));
2076 case elfcpp::R_ARM_MOVW_PREL_NC:
2077 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
2082 case elfcpp::R_ARM_MOVT_PREL:
2083 reloc_status = Arm_relocate_functions::movt_prel(view, object,
2087 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
2088 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
2093 case elfcpp::R_ARM_THM_MOVT_PREL:
2094 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
2098 case elfcpp::R_ARM_REL32:
2099 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2100 address, has_thumb_bit);
2103 case elfcpp::R_ARM_THM_ABS5:
2104 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2106 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
2109 case elfcpp::R_ARM_THM_CALL:
2110 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
2111 address, has_thumb_bit);
2114 case elfcpp::R_ARM_GOTOFF32:
2116 elfcpp::Elf_types<32>::Elf_Addr got_origin;
2117 got_origin = target->got_plt_section()->address();
2118 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2119 got_origin, has_thumb_bit);
2123 case elfcpp::R_ARM_BASE_PREL:
2126 // Get the addressing origin of the output segment defining the
2127 // symbol gsym (AAELF 4.6.1.2 Relocation types)
2128 gold_assert(gsym != NULL);
2129 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
2130 origin = gsym->output_segment()->vaddr();
2131 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
2132 origin = gsym->output_data()->address();
2135 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2136 _("cannot find origin of R_ARM_BASE_PREL"));
2139 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
2143 case elfcpp::R_ARM_BASE_ABS:
2145 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2150 // Get the addressing origin of the output segment defining
2151 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
2153 // R_ARM_BASE_ABS with the NULL symbol will give the
2154 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
2155 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
2156 origin = target->got_plt_section()->address();
2157 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
2158 origin = gsym->output_segment()->vaddr();
2159 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
2160 origin = gsym->output_data()->address();
2163 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2164 _("cannot find origin of R_ARM_BASE_ABS"));
2168 reloc_status = Arm_relocate_functions::base_abs(view, origin);
2172 case elfcpp::R_ARM_GOT_BREL:
2173 gold_assert(have_got_offset);
2174 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
2177 case elfcpp::R_ARM_GOT_PREL:
2178 gold_assert(have_got_offset);
2179 // Get the address origin for GOT PLT, which is allocated right
2180 // after the GOT section, to calculate an absolute address of
2181 // the symbol GOT entry (got_origin + got_offset).
2182 elfcpp::Elf_types<32>::Elf_Addr got_origin;
2183 got_origin = target->got_plt_section()->address();
2184 reloc_status = Arm_relocate_functions::got_prel(view,
2185 got_origin + got_offset,
2189 case elfcpp::R_ARM_PLT32:
2190 gold_assert(gsym == NULL
2191 || gsym->has_plt_offset()
2192 || gsym->final_value_is_known()
2193 || (gsym->is_defined()
2194 && !gsym->is_from_dynobj()
2195 && !gsym->is_preemptible()));
2196 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
2197 address, has_thumb_bit);
2200 case elfcpp::R_ARM_CALL:
2201 reloc_status = Arm_relocate_functions::call(view, object, psymval,
2202 address, has_thumb_bit);
2205 case elfcpp::R_ARM_JUMP24:
2206 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
2207 address, has_thumb_bit);
2210 case elfcpp::R_ARM_PREL31:
2211 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
2212 address, has_thumb_bit);
2215 case elfcpp::R_ARM_TARGET1:
2216 // This should have been mapped to another type already.
2218 case elfcpp::R_ARM_COPY:
2219 case elfcpp::R_ARM_GLOB_DAT:
2220 case elfcpp::R_ARM_JUMP_SLOT:
2221 case elfcpp::R_ARM_RELATIVE:
2222 // These are relocations which should only be seen by the
2223 // dynamic linker, and should never be seen here.
2224 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2225 _("unexpected reloc %u in object file"),
2230 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2231 _("unsupported reloc %u"),
2236 // Report any errors.
2237 switch (reloc_status)
2239 case Arm_relocate_functions::STATUS_OKAY:
2241 case Arm_relocate_functions::STATUS_OVERFLOW:
2242 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2243 _("relocation overflow in relocation %u"),
2246 case Arm_relocate_functions::STATUS_BAD_RELOC:
2247 gold_error_at_location(
2251 _("unexpected opcode while processing relocation %u"),
2261 // Relocate section data.
2263 template<bool big_endian>
2265 Target_arm<big_endian>::relocate_section(
2266 const Relocate_info<32, big_endian>* relinfo,
2267 unsigned int sh_type,
2268 const unsigned char* prelocs,
2270 Output_section* output_section,
2271 bool needs_special_offset_handling,
2272 unsigned char* view,
2273 elfcpp::Elf_types<32>::Elf_Addr address,
2274 section_size_type view_size,
2275 const Reloc_symbol_changes* reloc_symbol_changes)
2277 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
2278 gold_assert(sh_type == elfcpp::SHT_REL);
2280 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
2287 needs_special_offset_handling,
2291 reloc_symbol_changes);
2294 // Return the size of a relocation while scanning during a relocatable
2297 template<bool big_endian>
2299 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
2300 unsigned int r_type,
2303 r_type = get_real_reloc_type(r_type);
2306 case elfcpp::R_ARM_NONE:
2309 case elfcpp::R_ARM_ABS8:
2312 case elfcpp::R_ARM_ABS16:
2313 case elfcpp::R_ARM_THM_ABS5:
2316 case elfcpp::R_ARM_ABS32:
2317 case elfcpp::R_ARM_ABS32_NOI:
2318 case elfcpp::R_ARM_ABS12:
2319 case elfcpp::R_ARM_BASE_ABS:
2320 case elfcpp::R_ARM_REL32:
2321 case elfcpp::R_ARM_THM_CALL:
2322 case elfcpp::R_ARM_GOTOFF32:
2323 case elfcpp::R_ARM_BASE_PREL:
2324 case elfcpp::R_ARM_GOT_BREL:
2325 case elfcpp::R_ARM_GOT_PREL:
2326 case elfcpp::R_ARM_PLT32:
2327 case elfcpp::R_ARM_CALL:
2328 case elfcpp::R_ARM_JUMP24:
2329 case elfcpp::R_ARM_PREL31:
2330 case elfcpp::R_ARM_MOVW_ABS_NC:
2331 case elfcpp::R_ARM_MOVT_ABS:
2332 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
2333 case elfcpp::R_ARM_THM_MOVT_ABS:
2334 case elfcpp::R_ARM_MOVW_PREL_NC:
2335 case elfcpp::R_ARM_MOVT_PREL:
2336 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
2337 case elfcpp::R_ARM_THM_MOVT_PREL:
2340 case elfcpp::R_ARM_TARGET1:
2341 // This should have been mapped to another type already.
2343 case elfcpp::R_ARM_COPY:
2344 case elfcpp::R_ARM_GLOB_DAT:
2345 case elfcpp::R_ARM_JUMP_SLOT:
2346 case elfcpp::R_ARM_RELATIVE:
2347 // These are relocations which should only be seen by the
2348 // dynamic linker, and should never be seen here.
2349 gold_error(_("%s: unexpected reloc %u in object file"),
2350 object->name().c_str(), r_type);
2354 object->error(_("unsupported reloc %u in object file"), r_type);
2359 // Scan the relocs during a relocatable link.
2361 template<bool big_endian>
2363 Target_arm<big_endian>::scan_relocatable_relocs(
2364 const General_options& options,
2365 Symbol_table* symtab,
2367 Sized_relobj<32, big_endian>* object,
2368 unsigned int data_shndx,
2369 unsigned int sh_type,
2370 const unsigned char* prelocs,
2372 Output_section* output_section,
2373 bool needs_special_offset_handling,
2374 size_t local_symbol_count,
2375 const unsigned char* plocal_symbols,
2376 Relocatable_relocs* rr)
2378 gold_assert(sh_type == elfcpp::SHT_REL);
2380 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2381 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2383 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
2384 Scan_relocatable_relocs>(
2393 needs_special_offset_handling,
2399 // Relocate a section during a relocatable link.
2401 template<bool big_endian>
2403 Target_arm<big_endian>::relocate_for_relocatable(
2404 const Relocate_info<32, big_endian>* relinfo,
2405 unsigned int sh_type,
2406 const unsigned char* prelocs,
2408 Output_section* output_section,
2409 off_t offset_in_output_section,
2410 const Relocatable_relocs* rr,
2411 unsigned char* view,
2412 elfcpp::Elf_types<32>::Elf_Addr view_address,
2413 section_size_type view_size,
2414 unsigned char* reloc_view,
2415 section_size_type reloc_view_size)
2417 gold_assert(sh_type == elfcpp::SHT_REL);
2419 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
2424 offset_in_output_section,
2433 // Return the value to use for a dynamic symbol which requires special
2434 // treatment. This is how we support equality comparisons of function
2435 // pointers across shared library boundaries, as described in the
2436 // processor specific ABI supplement.
2438 template<bool big_endian>
2440 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
2442 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2443 return this->plt_section()->address() + gsym->plt_offset();
2446 // Map platform-specific relocs to real relocs
2448 template<bool big_endian>
2450 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
2454 case elfcpp::R_ARM_TARGET1:
2455 // This is either R_ARM_ABS32 or R_ARM_REL32;
2456 return elfcpp::R_ARM_ABS32;
2458 case elfcpp::R_ARM_TARGET2:
2459 // This can be any reloc type but ususally is R_ARM_GOT_PREL
2460 return elfcpp::R_ARM_GOT_PREL;
2467 // The selector for arm object files.
2469 template<bool big_endian>
2470 class Target_selector_arm : public Target_selector
2473 Target_selector_arm()
2474 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
2475 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
2479 do_instantiate_target()
2480 { return new Target_arm<big_endian>(); }
2483 Target_selector_arm<false> target_selector_arm;
2484 Target_selector_arm<true> target_selector_armbe;
2486 } // End anonymous namespace.