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
77 // Coming soon (pending patches):
78 // - Defining section symbols __exidx_start and __exidx_stop.
79 // - Support interworking.
80 // - Mergeing all .ARM.xxx.yyy sections into .ARM.xxx. Currently, they
81 // are incorrectly merged into an .ARM section.
84 // - Create a PT_ARM_EXIDX program header for a shared object that
85 // might throw an exception.
86 // - Support more relocation types as needed.
87 // - Make PLTs more flexible for different architecture features like
90 // Utilities for manipulating integers of up to 32-bits
94 // Sign extend an n-bit unsigned integer stored in an uint32_t into
95 // an int32_t. NO_BITS must be between 1 to 32.
98 sign_extend(uint32_t bits
)
100 gold_assert(no_bits
>= 0 && no_bits
<= 32);
102 return static_cast<int32_t>(bits
);
103 uint32_t mask
= (~((uint32_t) 0)) >> (32 - no_bits
);
105 uint32_t top_bit
= 1U << (no_bits
- 1);
106 int32_t as_signed
= static_cast<int32_t>(bits
);
107 return (bits
& top_bit
) ? as_signed
+ (-top_bit
* 2) : as_signed
;
110 // Detects overflow of an NO_BITS integer stored in a uint32_t.
111 template<int no_bits
>
113 has_overflow(uint32_t bits
)
115 gold_assert(no_bits
>= 0 && no_bits
<= 32);
118 int32_t max
= (1 << (no_bits
- 1)) - 1;
119 int32_t min
= -(1 << (no_bits
- 1));
120 int32_t as_signed
= static_cast<int32_t>(bits
);
121 return as_signed
> max
|| as_signed
< min
;
124 // Select bits from A and B using bits in MASK. For each n in [0..31],
125 // the n-th bit in the result is chosen from the n-th bits of A and B.
126 // A zero selects A and a one selects B.
127 static inline uint32_t
128 bit_select(uint32_t a
, uint32_t b
, uint32_t mask
)
129 { return (a
& ~mask
) | (b
& mask
); }
132 template<bool big_endian
>
133 class Target_arm
: public Sized_target
<32, big_endian
>
136 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
140 : Sized_target
<32, big_endian
>(&arm_info
),
141 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
142 copy_relocs_(elfcpp::R_ARM_COPY
), dynbss_(NULL
)
145 // Process the relocations to determine unreferenced sections for
146 // garbage collection.
148 gc_process_relocs(const General_options
& options
,
149 Symbol_table
* symtab
,
151 Sized_relobj
<32, big_endian
>* object
,
152 unsigned int data_shndx
,
153 unsigned int sh_type
,
154 const unsigned char* prelocs
,
156 Output_section
* output_section
,
157 bool needs_special_offset_handling
,
158 size_t local_symbol_count
,
159 const unsigned char* plocal_symbols
);
161 // Scan the relocations to look for symbol adjustments.
163 scan_relocs(const General_options
& options
,
164 Symbol_table
* symtab
,
166 Sized_relobj
<32, big_endian
>* object
,
167 unsigned int data_shndx
,
168 unsigned int sh_type
,
169 const unsigned char* prelocs
,
171 Output_section
* output_section
,
172 bool needs_special_offset_handling
,
173 size_t local_symbol_count
,
174 const unsigned char* plocal_symbols
);
176 // Finalize the sections.
178 do_finalize_sections(Layout
*);
180 // Return the value to use for a dynamic symbol which requires special
183 do_dynsym_value(const Symbol
*) const;
185 // Relocate a section.
187 relocate_section(const Relocate_info
<32, big_endian
>*,
188 unsigned int sh_type
,
189 const unsigned char* prelocs
,
191 Output_section
* output_section
,
192 bool needs_special_offset_handling
,
194 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
195 section_size_type view_size
);
197 // Scan the relocs during a relocatable link.
199 scan_relocatable_relocs(const General_options
& options
,
200 Symbol_table
* symtab
,
202 Sized_relobj
<32, big_endian
>* object
,
203 unsigned int data_shndx
,
204 unsigned int sh_type
,
205 const unsigned char* prelocs
,
207 Output_section
* output_section
,
208 bool needs_special_offset_handling
,
209 size_t local_symbol_count
,
210 const unsigned char* plocal_symbols
,
211 Relocatable_relocs
*);
213 // Relocate a section during a relocatable link.
215 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
216 unsigned int sh_type
,
217 const unsigned char* prelocs
,
219 Output_section
* output_section
,
220 off_t offset_in_output_section
,
221 const Relocatable_relocs
*,
223 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
224 section_size_type view_size
,
225 unsigned char* reloc_view
,
226 section_size_type reloc_view_size
);
228 // Return whether SYM is defined by the ABI.
230 do_is_defined_by_abi(Symbol
* sym
) const
231 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
233 // Return the size of the GOT section.
237 gold_assert(this->got_
!= NULL
);
238 return this->got_
->data_size();
241 // Map platform-specific reloc types
243 get_real_reloc_type (unsigned int r_type
);
246 // The class which scans relocations.
251 : issued_non_pic_error_(false)
255 local(const General_options
& options
, Symbol_table
* symtab
,
256 Layout
* layout
, Target_arm
* target
,
257 Sized_relobj
<32, big_endian
>* object
,
258 unsigned int data_shndx
,
259 Output_section
* output_section
,
260 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
261 const elfcpp::Sym
<32, big_endian
>& lsym
);
264 global(const General_options
& options
, Symbol_table
* symtab
,
265 Layout
* layout
, Target_arm
* target
,
266 Sized_relobj
<32, big_endian
>* object
,
267 unsigned int data_shndx
,
268 Output_section
* output_section
,
269 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
274 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
275 unsigned int r_type
);
278 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
279 unsigned int r_type
, Symbol
*);
282 check_non_pic(Relobj
*, unsigned int r_type
);
284 // Almost identical to Symbol::needs_plt_entry except that it also
285 // handles STT_ARM_TFUNC.
287 symbol_needs_plt_entry(const Symbol
* sym
)
289 // An undefined symbol from an executable does not need a PLT entry.
290 if (sym
->is_undefined() && !parameters
->options().shared())
293 return (!parameters
->doing_static_link()
294 && (sym
->type() == elfcpp::STT_FUNC
295 || sym
->type() == elfcpp::STT_ARM_TFUNC
)
296 && (sym
->is_from_dynobj()
297 || sym
->is_undefined()
298 || sym
->is_preemptible()));
301 // Whether we have issued an error about a non-PIC compilation.
302 bool issued_non_pic_error_
;
305 // The class which implements relocation.
315 // Return whether the static relocation needs to be applied.
317 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
320 Output_section
* output_section
);
322 // Do a relocation. Return false if the caller should not issue
323 // any warnings about this relocation.
325 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
326 Output_section
*, size_t relnum
,
327 const elfcpp::Rel
<32, big_endian
>&,
328 unsigned int r_type
, const Sized_symbol
<32>*,
329 const Symbol_value
<32>*,
330 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
333 // Return whether we want to pass flag NON_PIC_REF for this
336 reloc_is_non_pic (unsigned int r_type
)
340 case elfcpp::R_ARM_REL32
:
341 case elfcpp::R_ARM_THM_CALL
:
342 case elfcpp::R_ARM_CALL
:
343 case elfcpp::R_ARM_JUMP24
:
344 case elfcpp::R_ARM_PREL31
:
352 // A class which returns the size required for a relocation type,
353 // used while scanning relocs during a relocatable link.
354 class Relocatable_size_for_reloc
358 get_size_for_reloc(unsigned int, Relobj
*);
361 // Get the GOT section, creating it if necessary.
362 Output_data_got
<32, big_endian
>*
363 got_section(Symbol_table
*, Layout
*);
365 // Get the GOT PLT section.
367 got_plt_section() const
369 gold_assert(this->got_plt_
!= NULL
);
370 return this->got_plt_
;
373 // Create a PLT entry for a global symbol.
375 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
377 // Get the PLT section.
378 const Output_data_plt_arm
<big_endian
>*
381 gold_assert(this->plt_
!= NULL
);
385 // Get the dynamic reloc section, creating it if necessary.
387 rel_dyn_section(Layout
*);
389 // Return true if the symbol may need a COPY relocation.
390 // References from an executable object to non-function symbols
391 // defined in a dynamic object may need a COPY relocation.
393 may_need_copy_reloc(Symbol
* gsym
)
395 return (!parameters
->options().shared()
396 && gsym
->is_from_dynobj()
397 && gsym
->type() != elfcpp::STT_FUNC
398 && gsym
->type() != elfcpp::STT_ARM_TFUNC
);
401 // Add a potential copy relocation.
403 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
404 Sized_relobj
<32, big_endian
>* object
,
405 unsigned int shndx
, Output_section
* output_section
,
406 Symbol
* sym
, const elfcpp::Rel
<32, big_endian
>& reloc
)
408 this->copy_relocs_
.copy_reloc(symtab
, layout
,
409 symtab
->get_sized_symbol
<32>(sym
),
410 object
, shndx
, output_section
, reloc
,
411 this->rel_dyn_section(layout
));
414 // Information about this specific target which we pass to the
415 // general Target structure.
416 static const Target::Target_info arm_info
;
418 // The types of GOT entries needed for this platform.
421 GOT_TYPE_STANDARD
= 0 // GOT entry for a regular symbol
425 Output_data_got
<32, big_endian
>* got_
;
427 Output_data_plt_arm
<big_endian
>* plt_
;
428 // The GOT PLT section.
429 Output_data_space
* got_plt_
;
430 // The dynamic reloc section.
431 Reloc_section
* rel_dyn_
;
432 // Relocs saved to avoid a COPY reloc.
433 Copy_relocs
<elfcpp::SHT_REL
, 32, big_endian
> copy_relocs_
;
434 // Space for variables copied with a COPY reloc.
435 Output_data_space
* dynbss_
;
438 template<bool big_endian
>
439 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
442 big_endian
, // is_big_endian
443 elfcpp::EM_ARM
, // machine_code
444 false, // has_make_symbol
445 false, // has_resolve
446 false, // has_code_fill
447 true, // is_default_stack_executable
449 "/usr/lib/libc.so.1", // dynamic_linker
450 0x8000, // default_text_segment_address
451 0x1000, // abi_pagesize (overridable by -z max-page-size)
452 0x1000 // common_pagesize (overridable by -z common-page-size)
455 // Arm relocate functions class
458 template<bool big_endian
>
459 class Arm_relocate_functions
: public Relocate_functions
<32, big_endian
>
464 STATUS_OKAY
, // No error during relocation.
465 STATUS_OVERFLOW
, // Relocation oveflow.
466 STATUS_BAD_RELOC
// Relocation cannot be applied.
470 typedef Relocate_functions
<32, big_endian
> Base
;
471 typedef Arm_relocate_functions
<big_endian
> This
;
473 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
474 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
475 // to distinguish ARM and THUMB functions and it is treated specially.
476 static inline Symbol_value
<32>::Value
477 arm_symbol_value (const Sized_relobj
<32, big_endian
> *object
,
478 const Symbol_value
<32>* psymval
,
479 Symbol_value
<32>::Value addend
,
482 typedef Symbol_value
<32>::Value Valtype
;
486 Valtype raw
= psymval
->value(object
, 0);
487 Valtype thumb_bit
= raw
& 1;
488 return ((raw
& ~((Valtype
) 1)) + addend
) | thumb_bit
;
491 return psymval
->value(object
, addend
);
494 // FIXME: This probably only works for Android on ARM v5te. We should
495 // following GNU ld for the general case.
496 template<unsigned r_type
>
497 static inline typename
This::Status
498 arm_branch_common(unsigned char *view
,
499 const Sized_relobj
<32, big_endian
>* object
,
500 const Symbol_value
<32>* psymval
,
501 elfcpp::Elf_types
<32>::Elf_Addr address
,
504 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
505 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
506 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
508 bool insn_is_b
= (((val
>> 28) & 0xf) <= 0xe)
509 && ((val
& 0x0f000000UL
) == 0x0a000000UL
);
510 bool insn_is_uncond_bl
= (val
& 0xff000000UL
) == 0xeb000000UL
;
511 bool insn_is_cond_bl
= (((val
>> 28) & 0xf) < 0xe)
512 && ((val
& 0x0f000000UL
) == 0x0b000000UL
);
513 bool insn_is_blx
= (val
& 0xfe000000UL
) == 0xfa000000UL
;
514 bool insn_is_any_branch
= (val
& 0x0e000000UL
) == 0x0a000000UL
;
516 if (r_type
== elfcpp::R_ARM_CALL
)
518 if (!insn_is_uncond_bl
&& !insn_is_blx
)
519 return This::STATUS_BAD_RELOC
;
521 else if (r_type
== elfcpp::R_ARM_JUMP24
)
523 if (!insn_is_b
&& !insn_is_cond_bl
)
524 return This::STATUS_BAD_RELOC
;
526 else if (r_type
== elfcpp::R_ARM_PLT32
)
528 if (!insn_is_any_branch
)
529 return This::STATUS_BAD_RELOC
;
534 Valtype addend
= utils::sign_extend
<26>(val
<< 2);
535 Valtype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
538 // If target has thumb bit set, we need to either turn the BL
539 // into a BLX (for ARMv5 or above) or generate a stub.
543 if (insn_is_uncond_bl
)
544 val
= (val
& 0xffffff) | 0xfa000000 | ((x
& 2) << 23);
546 return This::STATUS_BAD_RELOC
;
549 gold_assert(!insn_is_blx
);
551 val
= utils::bit_select(val
, (x
>> 2), 0xffffffUL
);
552 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
553 return (utils::has_overflow
<26>(x
)
554 ? This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
558 // R_ARM_ABS32: (S + A) | T
559 static inline typename
This::Status
560 abs32(unsigned char *view
,
561 const Sized_relobj
<32, big_endian
>* object
,
562 const Symbol_value
<32>* psymval
,
565 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
566 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
567 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
568 Valtype x
= This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
);
569 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
570 return This::STATUS_OKAY
;
573 // R_ARM_REL32: (S + A) | T - P
574 static inline typename
This::Status
575 rel32(unsigned char *view
,
576 const Sized_relobj
<32, big_endian
>* object
,
577 const Symbol_value
<32>* psymval
,
578 elfcpp::Elf_types
<32>::Elf_Addr address
,
581 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
582 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
583 Valtype addend
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
584 Valtype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
586 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
587 return This::STATUS_OKAY
;
590 // R_ARM_THM_CALL: (S + A) | T - P
591 static inline typename
This::Status
592 thm_call(unsigned char *view
,
593 const Sized_relobj
<32, big_endian
>* object
,
594 const Symbol_value
<32>* psymval
,
595 elfcpp::Elf_types
<32>::Elf_Addr address
,
598 // A thumb call consists of two instructions.
599 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype
;
600 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Reltype
;
601 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
602 Valtype hi
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
603 Valtype lo
= elfcpp::Swap
<16, big_endian
>::readval(wv
+ 1);
604 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
605 gold_assert((lo
& 0xf800) == 0xf800);
606 Reltype addend
= utils::sign_extend
<23>(((hi
& 0x7ff) << 12)
607 | ((lo
& 0x7ff) << 1));
608 Reltype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
611 // If target has no thumb bit set, we need to either turn the BL
612 // into a BLX (for ARMv5 or above) or generate a stub.
615 // This only works for ARMv5 and above with interworking enabled.
618 hi
= utils::bit_select(hi
, (x
>> 12), 0x7ffU
);
619 lo
= utils::bit_select(lo
, (x
>> 1), 0x7ffU
);
620 elfcpp::Swap
<16, big_endian
>::writeval(wv
, hi
);
621 elfcpp::Swap
<16, big_endian
>::writeval(wv
+ 1, lo
);
622 return (utils::has_overflow
<23>(x
)
623 ? This::STATUS_OVERFLOW
624 : This::STATUS_OKAY
);
627 // R_ARM_BASE_PREL: B(S) + A - P
628 static inline typename
This::Status
629 base_prel(unsigned char* view
,
630 elfcpp::Elf_types
<32>::Elf_Addr origin
,
631 elfcpp::Elf_types
<32>::Elf_Addr address
)
633 Base::rel32(view
, origin
- address
);
637 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
638 static inline typename
This::Status
639 got_brel(unsigned char* view
,
640 typename
elfcpp::Swap
<32, big_endian
>::Valtype got_offset
)
642 Base::rel32(view
, got_offset
);
643 return This::STATUS_OKAY
;
646 // R_ARM_PLT32: (S + A) | T - P
647 static inline typename
This::Status
648 plt32(unsigned char *view
,
649 const Sized_relobj
<32, big_endian
>* object
,
650 const Symbol_value
<32>* psymval
,
651 elfcpp::Elf_types
<32>::Elf_Addr address
,
654 return arm_branch_common
<elfcpp::R_ARM_PLT32
>(view
, object
, psymval
,
655 address
, has_thumb_bit
);
658 // R_ARM_CALL: (S + A) | T - P
659 static inline typename
This::Status
660 call(unsigned char *view
,
661 const Sized_relobj
<32, big_endian
>* object
,
662 const Symbol_value
<32>* psymval
,
663 elfcpp::Elf_types
<32>::Elf_Addr address
,
666 return arm_branch_common
<elfcpp::R_ARM_CALL
>(view
, object
, psymval
,
667 address
, has_thumb_bit
);
670 // R_ARM_JUMP24: (S + A) | T - P
671 static inline typename
This::Status
672 jump24(unsigned char *view
,
673 const Sized_relobj
<32, big_endian
>* object
,
674 const Symbol_value
<32>* psymval
,
675 elfcpp::Elf_types
<32>::Elf_Addr address
,
678 return arm_branch_common
<elfcpp::R_ARM_JUMP24
>(view
, object
, psymval
,
679 address
, has_thumb_bit
);
682 // R_ARM_PREL: (S + A) | T - P
683 static inline typename
This::Status
684 prel31(unsigned char *view
,
685 const Sized_relobj
<32, big_endian
>* object
,
686 const Symbol_value
<32>* psymval
,
687 elfcpp::Elf_types
<32>::Elf_Addr address
,
690 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
691 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
692 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
693 Valtype addend
= utils::sign_extend
<31>(val
);
694 Valtype x
= (This::arm_symbol_value(object
, psymval
, addend
, has_thumb_bit
)
696 val
= utils::bit_select(val
, x
, 0x7fffffffU
);
697 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
698 return (utils::has_overflow
<31>(x
) ?
699 This::STATUS_OVERFLOW
: This::STATUS_OKAY
);
703 // Get the GOT section, creating it if necessary.
705 template<bool big_endian
>
706 Output_data_got
<32, big_endian
>*
707 Target_arm
<big_endian
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
709 if (this->got_
== NULL
)
711 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
713 this->got_
= new Output_data_got
<32, big_endian
>();
716 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
718 | elfcpp::SHF_WRITE
),
722 // The old GNU linker creates a .got.plt section. We just
723 // create another set of data in the .got section. Note that we
724 // always create a PLT if we create a GOT, although the PLT
726 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
727 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
729 | elfcpp::SHF_WRITE
),
733 // The first three entries are reserved.
734 this->got_plt_
->set_current_data_size(3 * 4);
736 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
737 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
739 0, 0, elfcpp::STT_OBJECT
,
741 elfcpp::STV_HIDDEN
, 0,
747 // Get the dynamic reloc section, creating it if necessary.
749 template<bool big_endian
>
750 typename Target_arm
<big_endian
>::Reloc_section
*
751 Target_arm
<big_endian
>::rel_dyn_section(Layout
* layout
)
753 if (this->rel_dyn_
== NULL
)
755 gold_assert(layout
!= NULL
);
756 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
757 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
758 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
760 return this->rel_dyn_
;
763 // A class to handle the PLT data.
765 template<bool big_endian
>
766 class Output_data_plt_arm
: public Output_section_data
769 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
772 Output_data_plt_arm(Layout
*, Output_data_space
*);
774 // Add an entry to the PLT.
776 add_entry(Symbol
* gsym
);
778 // Return the .rel.plt section data.
781 { return this->rel_
; }
785 do_adjust_output_section(Output_section
* os
);
787 // Write to a map file.
789 do_print_to_mapfile(Mapfile
* mapfile
) const
790 { mapfile
->print_output_data(this, _("** PLT")); }
793 // Template for the first PLT entry.
794 static const uint32_t first_plt_entry
[5];
796 // Template for subsequent PLT entries.
797 static const uint32_t plt_entry
[3];
799 // Set the final size.
801 set_final_data_size()
803 this->set_data_size(sizeof(first_plt_entry
)
804 + this->count_
* sizeof(plt_entry
));
807 // Write out the PLT data.
809 do_write(Output_file
*);
811 // The reloc section.
813 // The .got.plt section.
814 Output_data_space
* got_plt_
;
815 // The number of PLT entries.
819 // Create the PLT section. The ordinary .got section is an argument,
820 // since we need to refer to the start. We also create our own .got
821 // section just for PLT entries.
823 template<bool big_endian
>
824 Output_data_plt_arm
<big_endian
>::Output_data_plt_arm(Layout
* layout
,
825 Output_data_space
* got_plt
)
826 : Output_section_data(4), got_plt_(got_plt
), count_(0)
828 this->rel_
= new Reloc_section(false);
829 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
830 elfcpp::SHF_ALLOC
, this->rel_
);
833 template<bool big_endian
>
835 Output_data_plt_arm
<big_endian
>::do_adjust_output_section(Output_section
* os
)
840 // Add an entry to the PLT.
842 template<bool big_endian
>
844 Output_data_plt_arm
<big_endian
>::add_entry(Symbol
* gsym
)
846 gold_assert(!gsym
->has_plt_offset());
848 // Note that when setting the PLT offset we skip the initial
849 // reserved PLT entry.
850 gsym
->set_plt_offset((this->count_
) * sizeof(plt_entry
)
851 + sizeof(first_plt_entry
));
855 section_offset_type got_offset
= this->got_plt_
->current_data_size();
857 // Every PLT entry needs a GOT entry which points back to the PLT
858 // entry (this will be changed by the dynamic linker, normally
859 // lazily when the function is called).
860 this->got_plt_
->set_current_data_size(got_offset
+ 4);
862 // Every PLT entry needs a reloc.
863 gsym
->set_needs_dynsym_entry();
864 this->rel_
->add_global(gsym
, elfcpp::R_ARM_JUMP_SLOT
, this->got_plt_
,
867 // Note that we don't need to save the symbol. The contents of the
868 // PLT are independent of which symbols are used. The symbols only
869 // appear in the relocations.
873 // FIXME: This is not very flexible. Right now this has only been tested
874 // on armv5te. If we are to support additional architecture features like
875 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
877 // The first entry in the PLT.
878 template<bool big_endian
>
879 const uint32_t Output_data_plt_arm
<big_endian
>::first_plt_entry
[5] =
881 0xe52de004, // str lr, [sp, #-4]!
882 0xe59fe004, // ldr lr, [pc, #4]
883 0xe08fe00e, // add lr, pc, lr
884 0xe5bef008, // ldr pc, [lr, #8]!
885 0x00000000, // &GOT[0] - .
888 // Subsequent entries in the PLT.
890 template<bool big_endian
>
891 const uint32_t Output_data_plt_arm
<big_endian
>::plt_entry
[3] =
893 0xe28fc600, // add ip, pc, #0xNN00000
894 0xe28cca00, // add ip, ip, #0xNN000
895 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
898 // Write out the PLT. This uses the hand-coded instructions above,
899 // and adjusts them as needed. This is all specified by the arm ELF
900 // Processor Supplement.
902 template<bool big_endian
>
904 Output_data_plt_arm
<big_endian
>::do_write(Output_file
* of
)
906 const off_t offset
= this->offset();
907 const section_size_type oview_size
=
908 convert_to_section_size_type(this->data_size());
909 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
911 const off_t got_file_offset
= this->got_plt_
->offset();
912 const section_size_type got_size
=
913 convert_to_section_size_type(this->got_plt_
->data_size());
914 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
916 unsigned char* pov
= oview
;
918 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
919 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
921 // Write first PLT entry. All but the last word are constants.
922 const size_t num_first_plt_words
= (sizeof(first_plt_entry
)
923 / sizeof(plt_entry
[0]));
924 for (size_t i
= 0; i
< num_first_plt_words
- 1; i
++)
925 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ i
* 4, first_plt_entry
[i
]);
926 // Last word in first PLT entry is &GOT[0] - .
927 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 16,
928 got_address
- (plt_address
+ 16));
929 pov
+= sizeof(first_plt_entry
);
931 unsigned char* got_pov
= got_view
;
933 memset(got_pov
, 0, 12);
936 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
937 unsigned int plt_offset
= sizeof(first_plt_entry
);
938 unsigned int plt_rel_offset
= 0;
939 unsigned int got_offset
= 12;
940 const unsigned int count
= this->count_
;
941 for (unsigned int i
= 0;
944 pov
+= sizeof(plt_entry
),
946 plt_offset
+= sizeof(plt_entry
),
947 plt_rel_offset
+= rel_size
,
950 // Set and adjust the PLT entry itself.
951 int32_t offset
= ((got_address
+ got_offset
)
952 - (plt_address
+ plt_offset
+ 8));
954 gold_assert(offset
>= 0 && offset
< 0x0fffffff);
955 uint32_t plt_insn0
= plt_entry
[0] | ((offset
>> 20) & 0xff);
956 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt_insn0
);
957 uint32_t plt_insn1
= plt_entry
[1] | ((offset
>> 12) & 0xff);
958 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, plt_insn1
);
959 uint32_t plt_insn2
= plt_entry
[2] | (offset
& 0xfff);
960 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, plt_insn2
);
962 // Set the entry in the GOT.
963 elfcpp::Swap
<32, big_endian
>::writeval(got_pov
, plt_address
);
966 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
967 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
969 of
->write_output_view(offset
, oview_size
, oview
);
970 of
->write_output_view(got_file_offset
, got_size
, got_view
);
973 // Create a PLT entry for a global symbol.
975 template<bool big_endian
>
977 Target_arm
<big_endian
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
980 if (gsym
->has_plt_offset())
983 if (this->plt_
== NULL
)
985 // Create the GOT sections first.
986 this->got_section(symtab
, layout
);
988 this->plt_
= new Output_data_plt_arm
<big_endian
>(layout
, this->got_plt_
);
989 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
991 | elfcpp::SHF_EXECINSTR
),
994 this->plt_
->add_entry(gsym
);
997 // Report an unsupported relocation against a local symbol.
999 template<bool big_endian
>
1001 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
1002 Sized_relobj
<32, big_endian
>* object
,
1003 unsigned int r_type
)
1005 gold_error(_("%s: unsupported reloc %u against local symbol"),
1006 object
->name().c_str(), r_type
);
1009 // We are about to emit a dynamic relocation of type R_TYPE. If the
1010 // dynamic linker does not support it, issue an error. The GNU linker
1011 // only issues a non-PIC error for an allocated read-only section.
1012 // Here we know the section is allocated, but we don't know that it is
1013 // read-only. But we check for all the relocation types which the
1014 // glibc dynamic linker supports, so it seems appropriate to issue an
1015 // error even if the section is not read-only.
1017 template<bool big_endian
>
1019 Target_arm
<big_endian
>::Scan::check_non_pic(Relobj
* object
,
1020 unsigned int r_type
)
1024 // These are the relocation types supported by glibc for ARM.
1025 case elfcpp::R_ARM_RELATIVE
:
1026 case elfcpp::R_ARM_COPY
:
1027 case elfcpp::R_ARM_GLOB_DAT
:
1028 case elfcpp::R_ARM_JUMP_SLOT
:
1029 case elfcpp::R_ARM_ABS32
:
1030 case elfcpp::R_ARM_PC24
:
1031 // FIXME: The following 3 types are not supported by Android's dynamic
1033 case elfcpp::R_ARM_TLS_DTPMOD32
:
1034 case elfcpp::R_ARM_TLS_DTPOFF32
:
1035 case elfcpp::R_ARM_TLS_TPOFF32
:
1039 // This prevents us from issuing more than one error per reloc
1040 // section. But we can still wind up issuing more than one
1041 // error per object file.
1042 if (this->issued_non_pic_error_
)
1044 object
->error(_("requires unsupported dynamic reloc; "
1045 "recompile with -fPIC"));
1046 this->issued_non_pic_error_
= true;
1049 case elfcpp::R_ARM_NONE
:
1054 // Scan a relocation for a local symbol.
1055 // FIXME: This only handles a subset of relocation types used by Android
1056 // on ARM v5te devices.
1058 template<bool big_endian
>
1060 Target_arm
<big_endian
>::Scan::local(const General_options
&,
1061 Symbol_table
* symtab
,
1064 Sized_relobj
<32, big_endian
>* object
,
1065 unsigned int data_shndx
,
1066 Output_section
* output_section
,
1067 const elfcpp::Rel
<32, big_endian
>& reloc
,
1068 unsigned int r_type
,
1069 const elfcpp::Sym
<32, big_endian
>&)
1071 r_type
= get_real_reloc_type(r_type
);
1074 case elfcpp::R_ARM_NONE
:
1077 case elfcpp::R_ARM_ABS32
:
1078 // If building a shared library (or a position-independent
1079 // executable), we need to create a dynamic relocation for
1080 // this location. The relocation applied at link time will
1081 // apply the link-time value, so we flag the location with
1082 // an R_ARM_RELATIVE relocation so the dynamic loader can
1083 // relocate it easily.
1084 if (parameters
->options().output_is_position_independent())
1086 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1087 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1088 // If we are to add more other reloc types than R_ARM_ABS32,
1089 // we need to add check_non_pic(object, r_type) here.
1090 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_ARM_RELATIVE
,
1091 output_section
, data_shndx
,
1092 reloc
.get_r_offset());
1096 case elfcpp::R_ARM_REL32
:
1097 case elfcpp::R_ARM_THM_CALL
:
1098 case elfcpp::R_ARM_CALL
:
1099 case elfcpp::R_ARM_PREL31
:
1100 case elfcpp::R_ARM_JUMP24
:
1101 case elfcpp::R_ARM_PLT32
:
1104 case elfcpp::R_ARM_GOTOFF32
:
1105 // We need a GOT section:
1106 target
->got_section(symtab
, layout
);
1109 case elfcpp::R_ARM_BASE_PREL
:
1110 // FIXME: What about this?
1113 case elfcpp::R_ARM_GOT_BREL
:
1115 // The symbol requires a GOT entry.
1116 Output_data_got
<32, big_endian
>* got
=
1117 target
->got_section(symtab
, layout
);
1118 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1119 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
1121 // If we are generating a shared object, we need to add a
1122 // dynamic RELATIVE relocation for this symbol's GOT entry.
1123 if (parameters
->options().output_is_position_independent())
1125 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1126 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1127 rel_dyn
->add_local_relative(
1128 object
, r_sym
, elfcpp::R_ARM_RELATIVE
, got
,
1129 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1135 case elfcpp::R_ARM_TARGET1
:
1136 // This should have been mapped to another type already.
1138 case elfcpp::R_ARM_COPY
:
1139 case elfcpp::R_ARM_GLOB_DAT
:
1140 case elfcpp::R_ARM_JUMP_SLOT
:
1141 case elfcpp::R_ARM_RELATIVE
:
1142 // These are relocations which should only be seen by the
1143 // dynamic linker, and should never be seen here.
1144 gold_error(_("%s: unexpected reloc %u in object file"),
1145 object
->name().c_str(), r_type
);
1149 unsupported_reloc_local(object
, r_type
);
1154 // Report an unsupported relocation against a global symbol.
1156 template<bool big_endian
>
1158 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
1159 Sized_relobj
<32, big_endian
>* object
,
1160 unsigned int r_type
,
1163 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1164 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1167 // Scan a relocation for a global symbol.
1168 // FIXME: This only handles a subset of relocation types used by Android
1169 // on ARM v5te devices.
1171 template<bool big_endian
>
1173 Target_arm
<big_endian
>::Scan::global(const General_options
&,
1174 Symbol_table
* symtab
,
1177 Sized_relobj
<32, big_endian
>* object
,
1178 unsigned int data_shndx
,
1179 Output_section
* output_section
,
1180 const elfcpp::Rel
<32, big_endian
>& reloc
,
1181 unsigned int r_type
,
1184 r_type
= get_real_reloc_type(r_type
);
1187 case elfcpp::R_ARM_NONE
:
1190 case elfcpp::R_ARM_ABS32
:
1192 // Make a dynamic relocation if necessary.
1193 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1195 if (target
->may_need_copy_reloc(gsym
))
1197 target
->copy_reloc(symtab
, layout
, object
,
1198 data_shndx
, output_section
, gsym
, reloc
);
1200 else if (gsym
->can_use_relative_reloc(false))
1202 // If we are to add more other reloc types than R_ARM_ABS32,
1203 // we need to add check_non_pic(object, r_type) here.
1204 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1205 rel_dyn
->add_global_relative(gsym
, elfcpp::R_ARM_RELATIVE
,
1206 output_section
, object
,
1207 data_shndx
, reloc
.get_r_offset());
1211 // If we are to add more other reloc types than R_ARM_ABS32,
1212 // we need to add check_non_pic(object, r_type) here.
1213 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1214 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1215 data_shndx
, reloc
.get_r_offset());
1221 case elfcpp::R_ARM_REL32
:
1222 case elfcpp::R_ARM_PREL31
:
1224 // Make a dynamic relocation if necessary.
1225 int flags
= Symbol::NON_PIC_REF
;
1226 if (gsym
->needs_dynamic_reloc(flags
))
1228 if (target
->may_need_copy_reloc(gsym
))
1230 target
->copy_reloc(symtab
, layout
, object
,
1231 data_shndx
, output_section
, gsym
, reloc
);
1235 check_non_pic(object
, r_type
);
1236 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1237 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1238 data_shndx
, reloc
.get_r_offset());
1244 case elfcpp::R_ARM_JUMP24
:
1245 case elfcpp::R_ARM_THM_CALL
:
1246 case elfcpp::R_ARM_CALL
:
1248 if (Target_arm
<big_endian
>::Scan::symbol_needs_plt_entry(gsym
))
1249 target
->make_plt_entry(symtab
, layout
, gsym
);
1250 // Make a dynamic relocation if necessary.
1251 int flags
= Symbol::NON_PIC_REF
;
1252 if (gsym
->type() == elfcpp::STT_FUNC
1253 || gsym
->type() == elfcpp::STT_ARM_TFUNC
)
1254 flags
|= Symbol::FUNCTION_CALL
;
1255 if (gsym
->needs_dynamic_reloc(flags
))
1257 if (target
->may_need_copy_reloc(gsym
))
1259 target
->copy_reloc(symtab
, layout
, object
,
1260 data_shndx
, output_section
, gsym
,
1265 check_non_pic(object
, r_type
);
1266 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1267 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1268 data_shndx
, reloc
.get_r_offset());
1274 case elfcpp::R_ARM_PLT32
:
1275 // If the symbol is fully resolved, this is just a relative
1276 // local reloc. Otherwise we need a PLT entry.
1277 if (gsym
->final_value_is_known())
1279 // If building a shared library, we can also skip the PLT entry
1280 // if the symbol is defined in the output file and is protected
1282 if (gsym
->is_defined()
1283 && !gsym
->is_from_dynobj()
1284 && !gsym
->is_preemptible())
1286 target
->make_plt_entry(symtab
, layout
, gsym
);
1289 case elfcpp::R_ARM_GOTOFF32
:
1290 // We need a GOT section.
1291 target
->got_section(symtab
, layout
);
1294 case elfcpp::R_ARM_BASE_PREL
:
1295 // FIXME: What about this?
1298 case elfcpp::R_ARM_GOT_BREL
:
1300 // The symbol requires a GOT entry.
1301 Output_data_got
<32, big_endian
>* got
=
1302 target
->got_section(symtab
, layout
);
1303 if (gsym
->final_value_is_known())
1304 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1307 // If this symbol is not fully resolved, we need to add a
1308 // GOT entry with a dynamic relocation.
1309 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1310 if (gsym
->is_from_dynobj()
1311 || gsym
->is_undefined()
1312 || gsym
->is_preemptible())
1313 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
1314 rel_dyn
, elfcpp::R_ARM_GLOB_DAT
);
1317 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1318 rel_dyn
->add_global_relative(
1319 gsym
, elfcpp::R_ARM_RELATIVE
, got
,
1320 gsym
->got_offset(GOT_TYPE_STANDARD
));
1326 case elfcpp::R_ARM_TARGET1
:
1327 // This should have been mapped to another type already.
1329 case elfcpp::R_ARM_COPY
:
1330 case elfcpp::R_ARM_GLOB_DAT
:
1331 case elfcpp::R_ARM_JUMP_SLOT
:
1332 case elfcpp::R_ARM_RELATIVE
:
1333 // These are relocations which should only be seen by the
1334 // dynamic linker, and should never be seen here.
1335 gold_error(_("%s: unexpected reloc %u in object file"),
1336 object
->name().c_str(), r_type
);
1340 unsupported_reloc_global(object
, r_type
, gsym
);
1345 // Process relocations for gc.
1347 template<bool big_endian
>
1349 Target_arm
<big_endian
>::gc_process_relocs(const General_options
& options
,
1350 Symbol_table
* symtab
,
1352 Sized_relobj
<32, big_endian
>* object
,
1353 unsigned int data_shndx
,
1355 const unsigned char* prelocs
,
1357 Output_section
* output_section
,
1358 bool needs_special_offset_handling
,
1359 size_t local_symbol_count
,
1360 const unsigned char* plocal_symbols
)
1362 typedef Target_arm
<big_endian
> Arm
;
1363 typedef typename Target_arm
<big_endian
>::Scan Scan
;
1365 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
1375 needs_special_offset_handling
,
1380 // Scan relocations for a section.
1382 template<bool big_endian
>
1384 Target_arm
<big_endian
>::scan_relocs(const General_options
& options
,
1385 Symbol_table
* symtab
,
1387 Sized_relobj
<32, big_endian
>* object
,
1388 unsigned int data_shndx
,
1389 unsigned int sh_type
,
1390 const unsigned char* prelocs
,
1392 Output_section
* output_section
,
1393 bool needs_special_offset_handling
,
1394 size_t local_symbol_count
,
1395 const unsigned char* plocal_symbols
)
1397 typedef typename Target_arm
<big_endian
>::Scan Scan
;
1398 if (sh_type
== elfcpp::SHT_RELA
)
1400 gold_error(_("%s: unsupported RELA reloc section"),
1401 object
->name().c_str());
1405 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
1415 needs_special_offset_handling
,
1420 // Finalize the sections.
1422 template<bool big_endian
>
1424 Target_arm
<big_endian
>::do_finalize_sections(Layout
* layout
)
1426 // Fill in some more dynamic tags.
1427 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1430 if (this->got_plt_
!= NULL
)
1431 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1433 if (this->plt_
!= NULL
)
1435 const Output_data
* od
= this->plt_
->rel_plt();
1436 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1437 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1438 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1441 if (this->rel_dyn_
!= NULL
)
1443 const Output_data
* od
= this->rel_dyn_
;
1444 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1445 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1446 odyn
->add_constant(elfcpp::DT_RELENT
,
1447 elfcpp::Elf_sizes
<32>::rel_size
);
1450 if (!parameters
->options().shared())
1452 // The value of the DT_DEBUG tag is filled in by the dynamic
1453 // linker at run time, and used by the debugger.
1454 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1458 // Emit any relocs we saved in an attempt to avoid generating COPY
1460 if (this->copy_relocs_
.any_saved_relocs())
1461 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
1464 // Return whether a direct absolute static relocation needs to be applied.
1465 // In cases where Scan::local() or Scan::global() has created
1466 // a dynamic relocation other than R_ARM_RELATIVE, the addend
1467 // of the relocation is carried in the data, and we must not
1468 // apply the static relocation.
1470 template<bool big_endian
>
1472 Target_arm
<big_endian
>::Relocate::should_apply_static_reloc(
1473 const Sized_symbol
<32>* gsym
,
1476 Output_section
* output_section
)
1478 // If the output section is not allocated, then we didn't call
1479 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1481 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
1484 // For local symbols, we will have created a non-RELATIVE dynamic
1485 // relocation only if (a) the output is position independent,
1486 // (b) the relocation is absolute (not pc- or segment-relative), and
1487 // (c) the relocation is not 32 bits wide.
1489 return !(parameters
->options().output_is_position_independent()
1490 && (ref_flags
& Symbol::ABSOLUTE_REF
)
1493 // For global symbols, we use the same helper routines used in the
1494 // scan pass. If we did not create a dynamic relocation, or if we
1495 // created a RELATIVE dynamic relocation, we should apply the static
1497 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
1498 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
1499 && gsym
->can_use_relative_reloc(ref_flags
1500 & Symbol::FUNCTION_CALL
);
1501 return !has_dyn
|| is_rel
;
1504 // Perform a relocation.
1506 template<bool big_endian
>
1508 Target_arm
<big_endian
>::Relocate::relocate(
1509 const Relocate_info
<32, big_endian
>* relinfo
,
1511 Output_section
*output_section
,
1513 const elfcpp::Rel
<32, big_endian
>& rel
,
1514 unsigned int r_type
,
1515 const Sized_symbol
<32>* gsym
,
1516 const Symbol_value
<32>* psymval
,
1517 unsigned char* view
,
1518 elfcpp::Elf_types
<32>::Elf_Addr address
,
1519 section_size_type
/* view_size */ )
1521 typedef Arm_relocate_functions
<big_endian
> Arm_relocate_functions
;
1523 r_type
= get_real_reloc_type(r_type
);
1525 // If this the symbol may be a Thumb function, set thumb bit to 1.
1526 bool has_thumb_bit
= ((gsym
!= NULL
)
1527 && (gsym
->type() == elfcpp::STT_FUNC
1528 || gsym
->type() == elfcpp::STT_ARM_TFUNC
));
1530 // Pick the value to use for symbols defined in shared objects.
1531 Symbol_value
<32> symval
;
1533 && gsym
->use_plt_offset(reloc_is_non_pic(r_type
)))
1535 symval
.set_output_value(target
->plt_section()->address()
1536 + gsym
->plt_offset());
1541 const Sized_relobj
<32, big_endian
>* object
= relinfo
->object
;
1543 // Get the GOT offset if needed.
1544 // The GOT pointer points to the end of the GOT section.
1545 // We need to subtract the size of the GOT section to get
1546 // the actual offset to use in the relocation.
1547 bool have_got_offset
= false;
1548 unsigned int got_offset
= 0;
1551 case elfcpp::R_ARM_GOT_BREL
:
1554 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1555 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
1556 - target
->got_size());
1560 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1561 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1562 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1563 - target
->got_size());
1565 have_got_offset
= true;
1572 typename
Arm_relocate_functions::Status reloc_status
=
1573 Arm_relocate_functions::STATUS_OKAY
;
1576 case elfcpp::R_ARM_NONE
:
1579 case elfcpp::R_ARM_ABS32
:
1580 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
1582 reloc_status
= Arm_relocate_functions::abs32(view
, object
, psymval
,
1586 case elfcpp::R_ARM_REL32
:
1587 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
1588 address
, has_thumb_bit
);
1591 case elfcpp::R_ARM_THM_CALL
:
1592 reloc_status
= Arm_relocate_functions::thm_call(view
, object
, psymval
,
1593 address
, has_thumb_bit
);
1596 case elfcpp::R_ARM_GOTOFF32
:
1598 elfcpp::Elf_types
<32>::Elf_Addr got_origin
;
1599 got_origin
= target
->got_plt_section()->address();
1600 reloc_status
= Arm_relocate_functions::rel32(view
, object
, psymval
,
1601 got_origin
, has_thumb_bit
);
1605 case elfcpp::R_ARM_BASE_PREL
:
1608 // Get the addressing origin of the output segment defining the
1609 // symbol gsym (AAELF 4.6.1.2 Relocation types)
1610 gold_assert(gsym
!= NULL
);
1611 if (gsym
->source() == Symbol::IN_OUTPUT_SEGMENT
)
1612 origin
= gsym
->output_segment()->vaddr();
1613 else if (gsym
->source () == Symbol::IN_OUTPUT_DATA
)
1614 origin
= gsym
->output_data()->address();
1617 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1618 _("cannot find origin of R_ARM_BASE_PREL"));
1621 reloc_status
= Arm_relocate_functions::base_prel(view
, origin
, address
);
1625 case elfcpp::R_ARM_GOT_BREL
:
1626 gold_assert(have_got_offset
);
1627 reloc_status
= Arm_relocate_functions::got_brel(view
, got_offset
);
1630 case elfcpp::R_ARM_PLT32
:
1631 gold_assert(gsym
== NULL
1632 || gsym
->has_plt_offset()
1633 || gsym
->final_value_is_known()
1634 || (gsym
->is_defined()
1635 && !gsym
->is_from_dynobj()
1636 && !gsym
->is_preemptible()));
1637 reloc_status
= Arm_relocate_functions::plt32(view
, object
, psymval
,
1638 address
, has_thumb_bit
);
1641 case elfcpp::R_ARM_CALL
:
1642 reloc_status
= Arm_relocate_functions::call(view
, object
, psymval
,
1643 address
, has_thumb_bit
);
1646 case elfcpp::R_ARM_JUMP24
:
1647 reloc_status
= Arm_relocate_functions::jump24(view
, object
, psymval
,
1648 address
, has_thumb_bit
);
1651 case elfcpp::R_ARM_PREL31
:
1652 reloc_status
= Arm_relocate_functions::prel31(view
, object
, psymval
,
1653 address
, has_thumb_bit
);
1656 case elfcpp::R_ARM_TARGET1
:
1657 // This should have been mapped to another type already.
1659 case elfcpp::R_ARM_COPY
:
1660 case elfcpp::R_ARM_GLOB_DAT
:
1661 case elfcpp::R_ARM_JUMP_SLOT
:
1662 case elfcpp::R_ARM_RELATIVE
:
1663 // These are relocations which should only be seen by the
1664 // dynamic linker, and should never be seen here.
1665 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1666 _("unexpected reloc %u in object file"),
1671 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1672 _("unsupported reloc %u"),
1677 // Report any errors.
1678 switch (reloc_status
)
1680 case Arm_relocate_functions::STATUS_OKAY
:
1682 case Arm_relocate_functions::STATUS_OVERFLOW
:
1683 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1684 _("relocation overflow in relocation %u"),
1687 case Arm_relocate_functions::STATUS_BAD_RELOC
:
1688 gold_error_at_location(
1692 _("unexpected opcode while processing relocation %u"),
1702 // Relocate section data.
1704 template<bool big_endian
>
1706 Target_arm
<big_endian
>::relocate_section(
1707 const Relocate_info
<32, big_endian
>* relinfo
,
1708 unsigned int sh_type
,
1709 const unsigned char* prelocs
,
1711 Output_section
* output_section
,
1712 bool needs_special_offset_handling
,
1713 unsigned char* view
,
1714 elfcpp::Elf_types
<32>::Elf_Addr address
,
1715 section_size_type view_size
)
1717 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
1718 gold_assert(sh_type
== elfcpp::SHT_REL
);
1720 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
1727 needs_special_offset_handling
,
1733 // Return the size of a relocation while scanning during a relocatable
1736 template<bool big_endian
>
1738 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
1739 unsigned int r_type
,
1742 r_type
= get_real_reloc_type(r_type
);
1745 case elfcpp::R_ARM_NONE
:
1748 case elfcpp::R_ARM_ABS32
:
1749 case elfcpp::R_ARM_REL32
:
1750 case elfcpp::R_ARM_THM_CALL
:
1751 case elfcpp::R_ARM_GOTOFF32
:
1752 case elfcpp::R_ARM_BASE_PREL
:
1753 case elfcpp::R_ARM_GOT_BREL
:
1754 case elfcpp::R_ARM_PLT32
:
1755 case elfcpp::R_ARM_CALL
:
1756 case elfcpp::R_ARM_JUMP24
:
1757 case elfcpp::R_ARM_PREL31
:
1760 case elfcpp::R_ARM_TARGET1
:
1761 // This should have been mapped to another type already.
1763 case elfcpp::R_ARM_COPY
:
1764 case elfcpp::R_ARM_GLOB_DAT
:
1765 case elfcpp::R_ARM_JUMP_SLOT
:
1766 case elfcpp::R_ARM_RELATIVE
:
1767 // These are relocations which should only be seen by the
1768 // dynamic linker, and should never be seen here.
1769 gold_error(_("%s: unexpected reloc %u in object file"),
1770 object
->name().c_str(), r_type
);
1774 object
->error(_("unsupported reloc %u in object file"), r_type
);
1779 // Scan the relocs during a relocatable link.
1781 template<bool big_endian
>
1783 Target_arm
<big_endian
>::scan_relocatable_relocs(
1784 const General_options
& options
,
1785 Symbol_table
* symtab
,
1787 Sized_relobj
<32, big_endian
>* object
,
1788 unsigned int data_shndx
,
1789 unsigned int sh_type
,
1790 const unsigned char* prelocs
,
1792 Output_section
* output_section
,
1793 bool needs_special_offset_handling
,
1794 size_t local_symbol_count
,
1795 const unsigned char* plocal_symbols
,
1796 Relocatable_relocs
* rr
)
1798 gold_assert(sh_type
== elfcpp::SHT_REL
);
1800 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
1801 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
1803 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
1804 Scan_relocatable_relocs
>(
1813 needs_special_offset_handling
,
1819 // Relocate a section during a relocatable link.
1821 template<bool big_endian
>
1823 Target_arm
<big_endian
>::relocate_for_relocatable(
1824 const Relocate_info
<32, big_endian
>* relinfo
,
1825 unsigned int sh_type
,
1826 const unsigned char* prelocs
,
1828 Output_section
* output_section
,
1829 off_t offset_in_output_section
,
1830 const Relocatable_relocs
* rr
,
1831 unsigned char* view
,
1832 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
1833 section_size_type view_size
,
1834 unsigned char* reloc_view
,
1835 section_size_type reloc_view_size
)
1837 gold_assert(sh_type
== elfcpp::SHT_REL
);
1839 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
1844 offset_in_output_section
,
1853 // Return the value to use for a dynamic symbol which requires special
1854 // treatment. This is how we support equality comparisons of function
1855 // pointers across shared library boundaries, as described in the
1856 // processor specific ABI supplement.
1858 template<bool big_endian
>
1860 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
1862 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1863 return this->plt_section()->address() + gsym
->plt_offset();
1866 // Map platform-specific relocs to real relocs
1868 template<bool big_endian
>
1870 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
1874 case elfcpp::R_ARM_TARGET1
:
1875 // This is either R_ARM_ABS32 or R_ARM_REL32;
1876 return elfcpp::R_ARM_ABS32
;
1878 case elfcpp::R_ARM_TARGET2
:
1879 // This can be any reloc type but ususally is R_ARM_GOT_PREL
1880 return elfcpp::R_ARM_GOT_PREL
;
1887 // The selector for arm object files.
1889 template<bool big_endian
>
1890 class Target_selector_arm
: public Target_selector
1893 Target_selector_arm()
1894 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
1895 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
1899 do_instantiate_target()
1900 { return new Target_arm
<big_endian
>(); }
1903 Target_selector_arm
<false> target_selector_arm
;
1904 Target_selector_arm
<true> target_selector_armbe
;
1906 } // End anonymous namespace.