* Makefile.am: Remove spurious sanitization marker.
[binutils.git] / gold / arm.cc
blobdd5f67dca23cb624a353916e689938e434446d5e
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.
24 #include "gold.h"
26 #include <cstring>
27 #include <limits>
28 #include <cstdio>
29 #include <string>
31 #include "elfcpp.h"
32 #include "parameters.h"
33 #include "reloc.h"
34 #include "arm.h"
35 #include "object.h"
36 #include "symtab.h"
37 #include "layout.h"
38 #include "output.h"
39 #include "copy-relocs.h"
40 #include "target.h"
41 #include "target-reloc.h"
42 #include "target-select.h"
43 #include "tls.h"
44 #include "defstd.h"
45 #include "gc.h"
47 namespace
50 using namespace gold;
52 template<bool big_endian>
53 class Output_data_plt_arm;
55 // The arm target class.
57 // This is a very simple port of gold for ARM-EABI. It is intended for
58 // supporting Android only for the time being. Only these relocation types
59 // are supported.
61 // R_ARM_NONE
62 // R_ARM_ABS32
63 // R_ARM_ABS32_NOI
64 // R_ARM_ABS16
65 // R_ARM_ABS12
66 // R_ARM_ABS8
67 // R_ARM_THM_ABS5
68 // R_ARM_BASE_ABS
69 // R_ARM_REL32
70 // R_ARM_THM_CALL
71 // R_ARM_COPY
72 // R_ARM_GLOB_DAT
73 // R_ARM_BASE_PREL
74 // R_ARM_JUMP_SLOT
75 // R_ARM_RELATIVE
76 // R_ARM_GOTOFF32
77 // R_ARM_GOT_BREL
78 // R_ARM_GOT_PREL
79 // R_ARM_PLT32
80 // R_ARM_CALL
81 // R_ARM_JUMP24
82 // R_ARM_TARGET1
83 // R_ARM_PREL31
84 // R_ARM_ABS8
85 // R_ARM_MOVW_ABS_NC
86 // R_ARM_MOVT_ABS
87 // R_ARM_THM_MOVW_ABS_NC
88 // R_ARM_THM_MOVT_ABS
89 // R_ARM_MOVW_PREL_NC
90 // R_ARM_MOVT_PREL
91 // R_ARM_THM_MOVW_PREL_NC
92 // R_ARM_THM_MOVT_PREL
93 //
94 // TODOs:
95 // - Generate various branch stubs.
96 // - Support interworking.
97 // - Define section symbols __exidx_start and __exidx_stop.
98 // - Support more relocation types as needed.
99 // - Make PLTs more flexible for different architecture features like
100 // Thumb-2 and BE8.
101 // There are probably a lot more.
103 // Utilities for manipulating integers of up to 32-bits
105 namespace utils
107 // Sign extend an n-bit unsigned integer stored in an uint32_t into
108 // an int32_t. NO_BITS must be between 1 to 32.
109 template<int no_bits>
110 static inline int32_t
111 sign_extend(uint32_t bits)
113 gold_assert(no_bits >= 0 && no_bits <= 32);
114 if (no_bits == 32)
115 return static_cast<int32_t>(bits);
116 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
117 bits &= mask;
118 uint32_t top_bit = 1U << (no_bits - 1);
119 int32_t as_signed = static_cast<int32_t>(bits);
120 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
123 // Detects overflow of an NO_BITS integer stored in a uint32_t.
124 template<int no_bits>
125 static inline bool
126 has_overflow(uint32_t bits)
128 gold_assert(no_bits >= 0 && no_bits <= 32);
129 if (no_bits == 32)
130 return false;
131 int32_t max = (1 << (no_bits - 1)) - 1;
132 int32_t min = -(1 << (no_bits - 1));
133 int32_t as_signed = static_cast<int32_t>(bits);
134 return as_signed > max || as_signed < min;
137 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
138 // fits in the given number of bits as either a signed or unsigned value.
139 // For example, has_signed_unsigned_overflow<8> would check
140 // -128 <= bits <= 255
141 template<int no_bits>
142 static inline bool
143 has_signed_unsigned_overflow(uint32_t bits)
145 gold_assert(no_bits >= 2 && no_bits <= 32);
146 if (no_bits == 32)
147 return false;
148 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
149 int32_t min = -(1 << (no_bits - 1));
150 int32_t as_signed = static_cast<int32_t>(bits);
151 return as_signed > max || as_signed < min;
154 // Select bits from A and B using bits in MASK. For each n in [0..31],
155 // the n-th bit in the result is chosen from the n-th bits of A and B.
156 // A zero selects A and a one selects B.
157 static inline uint32_t
158 bit_select(uint32_t a, uint32_t b, uint32_t mask)
159 { return (a & ~mask) | (b & mask); }
162 template<bool big_endian>
163 class Target_arm : public Sized_target<32, big_endian>
165 public:
166 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
167 Reloc_section;
169 Target_arm()
170 : Sized_target<32, big_endian>(&arm_info),
171 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
172 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL)
175 // Process the relocations to determine unreferenced sections for
176 // garbage collection.
177 void
178 gc_process_relocs(const General_options& options,
179 Symbol_table* symtab,
180 Layout* layout,
181 Sized_relobj<32, big_endian>* object,
182 unsigned int data_shndx,
183 unsigned int sh_type,
184 const unsigned char* prelocs,
185 size_t reloc_count,
186 Output_section* output_section,
187 bool needs_special_offset_handling,
188 size_t local_symbol_count,
189 const unsigned char* plocal_symbols);
191 // Scan the relocations to look for symbol adjustments.
192 void
193 scan_relocs(const General_options& options,
194 Symbol_table* symtab,
195 Layout* layout,
196 Sized_relobj<32, big_endian>* object,
197 unsigned int data_shndx,
198 unsigned int sh_type,
199 const unsigned char* prelocs,
200 size_t reloc_count,
201 Output_section* output_section,
202 bool needs_special_offset_handling,
203 size_t local_symbol_count,
204 const unsigned char* plocal_symbols);
206 // Finalize the sections.
207 void
208 do_finalize_sections(Layout*);
210 // Return the value to use for a dynamic symbol which requires special
211 // treatment.
212 uint64_t
213 do_dynsym_value(const Symbol*) const;
215 // Relocate a section.
216 void
217 relocate_section(const Relocate_info<32, big_endian>*,
218 unsigned int sh_type,
219 const unsigned char* prelocs,
220 size_t reloc_count,
221 Output_section* output_section,
222 bool needs_special_offset_handling,
223 unsigned char* view,
224 elfcpp::Elf_types<32>::Elf_Addr view_address,
225 section_size_type view_size,
226 const Reloc_symbol_changes*);
228 // Scan the relocs during a relocatable link.
229 void
230 scan_relocatable_relocs(const General_options& options,
231 Symbol_table* symtab,
232 Layout* layout,
233 Sized_relobj<32, big_endian>* object,
234 unsigned int data_shndx,
235 unsigned int sh_type,
236 const unsigned char* prelocs,
237 size_t reloc_count,
238 Output_section* output_section,
239 bool needs_special_offset_handling,
240 size_t local_symbol_count,
241 const unsigned char* plocal_symbols,
242 Relocatable_relocs*);
244 // Relocate a section during a relocatable link.
245 void
246 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
247 unsigned int sh_type,
248 const unsigned char* prelocs,
249 size_t reloc_count,
250 Output_section* output_section,
251 off_t offset_in_output_section,
252 const Relocatable_relocs*,
253 unsigned char* view,
254 elfcpp::Elf_types<32>::Elf_Addr view_address,
255 section_size_type view_size,
256 unsigned char* reloc_view,
257 section_size_type reloc_view_size);
259 // Return whether SYM is defined by the ABI.
260 bool
261 do_is_defined_by_abi(Symbol* sym) const
262 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
264 // Return the size of the GOT section.
265 section_size_type
266 got_size()
268 gold_assert(this->got_ != NULL);
269 return this->got_->data_size();
272 // Map platform-specific reloc types
273 static unsigned int
274 get_real_reloc_type (unsigned int r_type);
276 private:
277 // The class which scans relocations.
278 class Scan
280 public:
281 Scan()
282 : issued_non_pic_error_(false)
285 inline void
286 local(const General_options& options, Symbol_table* symtab,
287 Layout* layout, Target_arm* target,
288 Sized_relobj<32, big_endian>* object,
289 unsigned int data_shndx,
290 Output_section* output_section,
291 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
292 const elfcpp::Sym<32, big_endian>& lsym);
294 inline void
295 global(const General_options& options, Symbol_table* symtab,
296 Layout* layout, Target_arm* target,
297 Sized_relobj<32, big_endian>* object,
298 unsigned int data_shndx,
299 Output_section* output_section,
300 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
301 Symbol* gsym);
303 private:
304 static void
305 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
306 unsigned int r_type);
308 static void
309 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
310 unsigned int r_type, Symbol*);
312 void
313 check_non_pic(Relobj*, unsigned int r_type);
315 // Almost identical to Symbol::needs_plt_entry except that it also
316 // handles STT_ARM_TFUNC.
317 static bool
318 symbol_needs_plt_entry(const Symbol* sym)
320 // An undefined symbol from an executable does not need a PLT entry.
321 if (sym->is_undefined() && !parameters->options().shared())
322 return false;
324 return (!parameters->doing_static_link()
325 && (sym->type() == elfcpp::STT_FUNC
326 || sym->type() == elfcpp::STT_ARM_TFUNC)
327 && (sym->is_from_dynobj()
328 || sym->is_undefined()
329 || sym->is_preemptible()));
332 // Whether we have issued an error about a non-PIC compilation.
333 bool issued_non_pic_error_;
336 // The class which implements relocation.
337 class Relocate
339 public:
340 Relocate()
343 ~Relocate()
346 // Return whether the static relocation needs to be applied.
347 inline bool
348 should_apply_static_reloc(const Sized_symbol<32>* gsym,
349 int ref_flags,
350 bool is_32bit,
351 Output_section* output_section);
353 // Do a relocation. Return false if the caller should not issue
354 // any warnings about this relocation.
355 inline bool
356 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
357 Output_section*, size_t relnum,
358 const elfcpp::Rel<32, big_endian>&,
359 unsigned int r_type, const Sized_symbol<32>*,
360 const Symbol_value<32>*,
361 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
362 section_size_type);
364 // Return whether we want to pass flag NON_PIC_REF for this
365 // reloc.
366 static inline bool
367 reloc_is_non_pic (unsigned int r_type)
369 switch (r_type)
371 case elfcpp::R_ARM_REL32:
372 case elfcpp::R_ARM_THM_CALL:
373 case elfcpp::R_ARM_CALL:
374 case elfcpp::R_ARM_JUMP24:
375 case elfcpp::R_ARM_PREL31:
376 case elfcpp::R_ARM_THM_ABS5:
377 case elfcpp::R_ARM_ABS8:
378 case elfcpp::R_ARM_ABS12:
379 case elfcpp::R_ARM_ABS16:
380 case elfcpp::R_ARM_BASE_ABS:
381 return true;
382 default:
383 return false;
388 // A class which returns the size required for a relocation type,
389 // used while scanning relocs during a relocatable link.
390 class Relocatable_size_for_reloc
392 public:
393 unsigned int
394 get_size_for_reloc(unsigned int, Relobj*);
397 // Get the GOT section, creating it if necessary.
398 Output_data_got<32, big_endian>*
399 got_section(Symbol_table*, Layout*);
401 // Get the GOT PLT section.
402 Output_data_space*
403 got_plt_section() const
405 gold_assert(this->got_plt_ != NULL);
406 return this->got_plt_;
409 // Create a PLT entry for a global symbol.
410 void
411 make_plt_entry(Symbol_table*, Layout*, Symbol*);
413 // Get the PLT section.
414 const Output_data_plt_arm<big_endian>*
415 plt_section() const
417 gold_assert(this->plt_ != NULL);
418 return this->plt_;
421 // Get the dynamic reloc section, creating it if necessary.
422 Reloc_section*
423 rel_dyn_section(Layout*);
425 // Return true if the symbol may need a COPY relocation.
426 // References from an executable object to non-function symbols
427 // defined in a dynamic object may need a COPY relocation.
428 bool
429 may_need_copy_reloc(Symbol* gsym)
431 return (gsym->type() != elfcpp::STT_ARM_TFUNC
432 && gsym->may_need_copy_reloc());
435 // Add a potential copy relocation.
436 void
437 copy_reloc(Symbol_table* symtab, Layout* layout,
438 Sized_relobj<32, big_endian>* object,
439 unsigned int shndx, Output_section* output_section,
440 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
442 this->copy_relocs_.copy_reloc(symtab, layout,
443 symtab->get_sized_symbol<32>(sym),
444 object, shndx, output_section, reloc,
445 this->rel_dyn_section(layout));
448 // Information about this specific target which we pass to the
449 // general Target structure.
450 static const Target::Target_info arm_info;
452 // The types of GOT entries needed for this platform.
453 enum Got_type
455 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
458 // The GOT section.
459 Output_data_got<32, big_endian>* got_;
460 // The PLT section.
461 Output_data_plt_arm<big_endian>* plt_;
462 // The GOT PLT section.
463 Output_data_space* got_plt_;
464 // The dynamic reloc section.
465 Reloc_section* rel_dyn_;
466 // Relocs saved to avoid a COPY reloc.
467 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
468 // Space for variables copied with a COPY reloc.
469 Output_data_space* dynbss_;
472 template<bool big_endian>
473 const Target::Target_info Target_arm<big_endian>::arm_info =
475 32, // size
476 big_endian, // is_big_endian
477 elfcpp::EM_ARM, // machine_code
478 false, // has_make_symbol
479 false, // has_resolve
480 false, // has_code_fill
481 true, // is_default_stack_executable
482 '\0', // wrap_char
483 "/usr/lib/libc.so.1", // dynamic_linker
484 0x8000, // default_text_segment_address
485 0x1000, // abi_pagesize (overridable by -z max-page-size)
486 0x1000, // common_pagesize (overridable by -z common-page-size)
487 elfcpp::SHN_UNDEF, // small_common_shndx
488 elfcpp::SHN_UNDEF, // large_common_shndx
489 0, // small_common_section_flags
490 0 // large_common_section_flags
493 // Arm relocate functions class
496 template<bool big_endian>
497 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
499 public:
500 typedef enum
502 STATUS_OKAY, // No error during relocation.
503 STATUS_OVERFLOW, // Relocation oveflow.
504 STATUS_BAD_RELOC // Relocation cannot be applied.
505 } Status;
507 private:
508 typedef Relocate_functions<32, big_endian> Base;
509 typedef Arm_relocate_functions<big_endian> This;
511 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
512 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
513 // to distinguish ARM and THUMB functions and it is treated specially.
514 static inline Symbol_value<32>::Value
515 arm_symbol_value (const Sized_relobj<32, big_endian> *object,
516 const Symbol_value<32>* psymval,
517 Symbol_value<32>::Value addend,
518 bool has_thumb_bit)
520 typedef Symbol_value<32>::Value Valtype;
522 if (has_thumb_bit)
524 Valtype raw = psymval->value(object, 0);
525 Valtype thumb_bit = raw & 1;
526 return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
528 else
529 return psymval->value(object, addend);
532 // Encoding of imm16 argument for movt and movw ARM instructions
533 // from ARM ARM:
535 // imm16 := imm4 | imm12
537 // 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
538 // +-------+---------------+-------+-------+-----------------------+
539 // | | |imm4 | |imm12 |
540 // +-------+---------------+-------+-------+-----------------------+
542 // Extract the relocation addend from VAL based on the ARM
543 // instruction encoding described above.
544 static inline typename elfcpp::Swap<32, big_endian>::Valtype
545 extract_arm_movw_movt_addend(
546 typename elfcpp::Swap<32, big_endian>::Valtype val)
548 // According to the Elf ABI for ARM Architecture the immediate
549 // field is sign-extended to form the addend.
550 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
553 // Insert X into VAL based on the ARM instruction encoding described
554 // above.
555 static inline typename elfcpp::Swap<32, big_endian>::Valtype
556 insert_val_arm_movw_movt(
557 typename elfcpp::Swap<32, big_endian>::Valtype val,
558 typename elfcpp::Swap<32, big_endian>::Valtype x)
560 val &= 0xfff0f000;
561 val |= x & 0x0fff;
562 val |= (x & 0xf000) << 4;
563 return val;
566 // Encoding of imm16 argument for movt and movw Thumb2 instructions
567 // from ARM ARM:
569 // imm16 := imm4 | i | imm3 | imm8
571 // 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
572 // +---------+-+-----------+-------++-+-----+-------+---------------+
573 // | |i| |imm4 || |imm3 | |imm8 |
574 // +---------+-+-----------+-------++-+-----+-------+---------------+
576 // Extract the relocation addend from VAL based on the Thumb2
577 // instruction encoding described above.
578 static inline typename elfcpp::Swap<32, big_endian>::Valtype
579 extract_thumb_movw_movt_addend(
580 typename elfcpp::Swap<32, big_endian>::Valtype val)
582 // According to the Elf ABI for ARM Architecture the immediate
583 // field is sign-extended to form the addend.
584 return utils::sign_extend<16>(((val >> 4) & 0xf000)
585 | ((val >> 15) & 0x0800)
586 | ((val >> 4) & 0x0700)
587 | (val & 0x00ff));
590 // Insert X into VAL based on the Thumb2 instruction encoding
591 // described above.
592 static inline typename elfcpp::Swap<32, big_endian>::Valtype
593 insert_val_thumb_movw_movt(
594 typename elfcpp::Swap<32, big_endian>::Valtype val,
595 typename elfcpp::Swap<32, big_endian>::Valtype x)
597 val &= 0xfbf08f00;
598 val |= (x & 0xf000) << 4;
599 val |= (x & 0x0800) << 15;
600 val |= (x & 0x0700) << 4;
601 val |= (x & 0x00ff);
602 return val;
605 // FIXME: This probably only works for Android on ARM v5te. We should
606 // following GNU ld for the general case.
607 template<unsigned r_type>
608 static inline typename This::Status
609 arm_branch_common(unsigned char *view,
610 const Sized_relobj<32, big_endian>* object,
611 const Symbol_value<32>* psymval,
612 elfcpp::Elf_types<32>::Elf_Addr address,
613 bool has_thumb_bit)
615 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
616 Valtype* wv = reinterpret_cast<Valtype*>(view);
617 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
619 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
620 && ((val & 0x0f000000UL) == 0x0a000000UL);
621 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
622 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
623 && ((val & 0x0f000000UL) == 0x0b000000UL);
624 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
625 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
627 if (r_type == elfcpp::R_ARM_CALL)
629 if (!insn_is_uncond_bl && !insn_is_blx)
630 return This::STATUS_BAD_RELOC;
632 else if (r_type == elfcpp::R_ARM_JUMP24)
634 if (!insn_is_b && !insn_is_cond_bl)
635 return This::STATUS_BAD_RELOC;
637 else if (r_type == elfcpp::R_ARM_PLT32)
639 if (!insn_is_any_branch)
640 return This::STATUS_BAD_RELOC;
642 else
643 gold_unreachable();
645 Valtype addend = utils::sign_extend<26>(val << 2);
646 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
647 - address);
649 // If target has thumb bit set, we need to either turn the BL
650 // into a BLX (for ARMv5 or above) or generate a stub.
651 if (x & 1)
653 // Turn BL to BLX.
654 if (insn_is_uncond_bl)
655 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
656 else
657 return This::STATUS_BAD_RELOC;
659 else
660 gold_assert(!insn_is_blx);
662 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
663 elfcpp::Swap<32, big_endian>::writeval(wv, val);
664 return (utils::has_overflow<26>(x)
665 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
668 public:
670 // R_ARM_ABS8: S + A
671 static inline typename This::Status
672 abs8(unsigned char *view,
673 const Sized_relobj<32, big_endian>* object,
674 const Symbol_value<32>* psymval)
676 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
677 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
678 Valtype* wv = reinterpret_cast<Valtype*>(view);
679 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
680 Reltype addend = utils::sign_extend<8>(val);
681 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
682 val = utils::bit_select(val, x, 0xffU);
683 elfcpp::Swap<8, big_endian>::writeval(wv, val);
684 return (utils::has_signed_unsigned_overflow<8>(x)
685 ? This::STATUS_OVERFLOW
686 : This::STATUS_OKAY);
689 // R_ARM_THM_ABS5: S + A
690 static inline typename This::Status
691 thm_abs5(unsigned char *view,
692 const Sized_relobj<32, big_endian>* object,
693 const Symbol_value<32>* psymval)
695 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
696 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
697 Valtype* wv = reinterpret_cast<Valtype*>(view);
698 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
699 Reltype addend = (val & 0x7e0U) >> 6;
700 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
701 val = utils::bit_select(val, x << 6, 0x7e0U);
702 elfcpp::Swap<16, big_endian>::writeval(wv, val);
703 return (utils::has_overflow<5>(x)
704 ? This::STATUS_OVERFLOW
705 : This::STATUS_OKAY);
708 // R_ARM_ABS12: S + A
709 static inline typename This::Status
710 abs12(unsigned char *view,
711 const Sized_relobj<32, big_endian>* object,
712 const Symbol_value<32>* psymval)
714 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
715 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
716 Valtype* wv = reinterpret_cast<Valtype*>(view);
717 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
718 Reltype addend = val & 0x0fffU;
719 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
720 val = utils::bit_select(val, x, 0x0fffU);
721 elfcpp::Swap<32, big_endian>::writeval(wv, val);
722 return (utils::has_overflow<12>(x)
723 ? This::STATUS_OVERFLOW
724 : This::STATUS_OKAY);
727 // R_ARM_ABS16: S + A
728 static inline typename This::Status
729 abs16(unsigned char *view,
730 const Sized_relobj<32, big_endian>* object,
731 const Symbol_value<32>* psymval)
733 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
734 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
735 Valtype* wv = reinterpret_cast<Valtype*>(view);
736 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
737 Reltype addend = utils::sign_extend<16>(val);
738 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
739 val = utils::bit_select(val, x, 0xffffU);
740 elfcpp::Swap<16, big_endian>::writeval(wv, val);
741 return (utils::has_signed_unsigned_overflow<16>(x)
742 ? This::STATUS_OVERFLOW
743 : This::STATUS_OKAY);
746 // R_ARM_ABS32: (S + A) | T
747 static inline typename This::Status
748 abs32(unsigned char *view,
749 const Sized_relobj<32, big_endian>* object,
750 const Symbol_value<32>* psymval,
751 bool has_thumb_bit)
753 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
754 Valtype* wv = reinterpret_cast<Valtype*>(view);
755 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
756 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
757 elfcpp::Swap<32, big_endian>::writeval(wv, x);
758 return This::STATUS_OKAY;
761 // R_ARM_REL32: (S + A) | T - P
762 static inline typename This::Status
763 rel32(unsigned char *view,
764 const Sized_relobj<32, big_endian>* object,
765 const Symbol_value<32>* psymval,
766 elfcpp::Elf_types<32>::Elf_Addr address,
767 bool has_thumb_bit)
769 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
770 Valtype* wv = reinterpret_cast<Valtype*>(view);
771 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
772 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
773 - address);
774 elfcpp::Swap<32, big_endian>::writeval(wv, x);
775 return This::STATUS_OKAY;
778 // R_ARM_THM_CALL: (S + A) | T - P
779 static inline typename This::Status
780 thm_call(unsigned char *view,
781 const Sized_relobj<32, big_endian>* object,
782 const Symbol_value<32>* psymval,
783 elfcpp::Elf_types<32>::Elf_Addr address,
784 bool has_thumb_bit)
786 // A thumb call consists of two instructions.
787 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
788 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
789 Valtype* wv = reinterpret_cast<Valtype*>(view);
790 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
791 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
792 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
793 gold_assert((lo & 0xf800) == 0xf800);
794 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
795 | ((lo & 0x7ff) << 1));
796 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
797 - address);
799 // If target has no thumb bit set, we need to either turn the BL
800 // into a BLX (for ARMv5 or above) or generate a stub.
801 if ((x & 1) == 0)
803 // This only works for ARMv5 and above with interworking enabled.
804 lo &= 0xefff;
806 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
807 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
808 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
809 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
810 return (utils::has_overflow<23>(x)
811 ? This::STATUS_OVERFLOW
812 : This::STATUS_OKAY);
815 // R_ARM_BASE_PREL: B(S) + A - P
816 static inline typename This::Status
817 base_prel(unsigned char* view,
818 elfcpp::Elf_types<32>::Elf_Addr origin,
819 elfcpp::Elf_types<32>::Elf_Addr address)
821 Base::rel32(view, origin - address);
822 return STATUS_OKAY;
825 // R_ARM_BASE_ABS: B(S) + A
826 static inline typename This::Status
827 base_abs(unsigned char* view,
828 elfcpp::Elf_types<32>::Elf_Addr origin)
830 Base::rel32(view, origin);
831 return STATUS_OKAY;
834 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
835 static inline typename This::Status
836 got_brel(unsigned char* view,
837 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
839 Base::rel32(view, got_offset);
840 return This::STATUS_OKAY;
843 // R_ARM_GOT_PREL: GOT(S) + A – P
844 static inline typename This::Status
845 got_prel(unsigned char* view,
846 typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
847 elfcpp::Elf_types<32>::Elf_Addr address)
849 Base::rel32(view, got_offset - address);
850 return This::STATUS_OKAY;
853 // R_ARM_PLT32: (S + A) | T - P
854 static inline typename This::Status
855 plt32(unsigned char *view,
856 const Sized_relobj<32, big_endian>* object,
857 const Symbol_value<32>* psymval,
858 elfcpp::Elf_types<32>::Elf_Addr address,
859 bool has_thumb_bit)
861 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
862 address, has_thumb_bit);
865 // R_ARM_CALL: (S + A) | T - P
866 static inline typename This::Status
867 call(unsigned char *view,
868 const Sized_relobj<32, big_endian>* object,
869 const Symbol_value<32>* psymval,
870 elfcpp::Elf_types<32>::Elf_Addr address,
871 bool has_thumb_bit)
873 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
874 address, has_thumb_bit);
877 // R_ARM_JUMP24: (S + A) | T - P
878 static inline typename This::Status
879 jump24(unsigned char *view,
880 const Sized_relobj<32, big_endian>* object,
881 const Symbol_value<32>* psymval,
882 elfcpp::Elf_types<32>::Elf_Addr address,
883 bool has_thumb_bit)
885 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
886 address, has_thumb_bit);
889 // R_ARM_PREL: (S + A) | T - P
890 static inline typename This::Status
891 prel31(unsigned char *view,
892 const Sized_relobj<32, big_endian>* object,
893 const Symbol_value<32>* psymval,
894 elfcpp::Elf_types<32>::Elf_Addr address,
895 bool has_thumb_bit)
897 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
898 Valtype* wv = reinterpret_cast<Valtype*>(view);
899 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
900 Valtype addend = utils::sign_extend<31>(val);
901 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
902 - address);
903 val = utils::bit_select(val, x, 0x7fffffffU);
904 elfcpp::Swap<32, big_endian>::writeval(wv, val);
905 return (utils::has_overflow<31>(x) ?
906 This::STATUS_OVERFLOW : This::STATUS_OKAY);
909 // R_ARM_MOVW_ABS_NC: (S + A) | T
910 static inline typename This::Status
911 movw_abs_nc(unsigned char *view,
912 const Sized_relobj<32, big_endian>* object,
913 const Symbol_value<32>* psymval,
914 bool has_thumb_bit)
916 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
917 Valtype* wv = reinterpret_cast<Valtype*>(view);
918 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
919 Valtype addend = This::extract_arm_movw_movt_addend(val);
920 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
921 val = This::insert_val_arm_movw_movt(val, x);
922 elfcpp::Swap<32, big_endian>::writeval(wv, val);
923 return This::STATUS_OKAY;
926 // R_ARM_MOVT_ABS: S + A
927 static inline typename This::Status
928 movt_abs(unsigned char *view,
929 const Sized_relobj<32, big_endian>* object,
930 const Symbol_value<32>* psymval)
932 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
933 Valtype* wv = reinterpret_cast<Valtype*>(view);
934 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
935 Valtype addend = This::extract_arm_movw_movt_addend(val);
936 Valtype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
937 val = This::insert_val_arm_movw_movt(val, x);
938 elfcpp::Swap<32, big_endian>::writeval(wv, val);
939 return This::STATUS_OKAY;
942 // R_ARM_THM_MOVW_ABS_NC: S + A | T
943 static inline typename This::Status
944 thm_movw_abs_nc(unsigned char *view,
945 const Sized_relobj<32, big_endian>* object,
946 const Symbol_value<32>* psymval,
947 bool has_thumb_bit)
949 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
950 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
951 Valtype* wv = reinterpret_cast<Valtype*>(view);
952 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
953 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
954 Reltype addend = extract_thumb_movw_movt_addend(val);
955 Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
956 val = This::insert_val_thumb_movw_movt(val, x);
957 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
958 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
959 return This::STATUS_OKAY;
962 // R_ARM_THM_MOVT_ABS: S + A
963 static inline typename This::Status
964 thm_movt_abs(unsigned char *view,
965 const Sized_relobj<32, big_endian>* object,
966 const Symbol_value<32>* psymval)
968 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
969 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
970 Valtype* wv = reinterpret_cast<Valtype*>(view);
971 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
972 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
973 Reltype addend = This::extract_thumb_movw_movt_addend(val);
974 Reltype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
975 val = This::insert_val_thumb_movw_movt(val, x);
976 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
977 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
978 return This::STATUS_OKAY;
981 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
982 static inline typename This::Status
983 movw_prel_nc(unsigned char *view,
984 const Sized_relobj<32, big_endian>* object,
985 const Symbol_value<32>* psymval,
986 elfcpp::Elf_types<32>::Elf_Addr address,
987 bool has_thumb_bit)
989 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
990 Valtype* wv = reinterpret_cast<Valtype*>(view);
991 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
992 Valtype addend = This::extract_arm_movw_movt_addend(val);
993 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
994 - address);
995 val = This::insert_val_arm_movw_movt(val, x);
996 elfcpp::Swap<32, big_endian>::writeval(wv, val);
997 return This::STATUS_OKAY;
1000 // R_ARM_MOVT_PREL: S + A - P
1001 static inline typename This::Status
1002 movt_prel(unsigned char *view,
1003 const Sized_relobj<32, big_endian>* object,
1004 const Symbol_value<32>* psymval,
1005 elfcpp::Elf_types<32>::Elf_Addr address)
1007 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1008 Valtype* wv = reinterpret_cast<Valtype*>(view);
1009 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1010 Valtype addend = This::extract_arm_movw_movt_addend(val);
1011 Valtype x = (This::arm_symbol_value(object, psymval, addend, 0)
1012 - address) >> 16;
1013 val = This::insert_val_arm_movw_movt(val, x);
1014 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1015 return This::STATUS_OKAY;
1018 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
1019 static inline typename This::Status
1020 thm_movw_prel_nc(unsigned char *view,
1021 const Sized_relobj<32, big_endian>* object,
1022 const Symbol_value<32>* psymval,
1023 elfcpp::Elf_types<32>::Elf_Addr address,
1024 bool has_thumb_bit)
1026 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1027 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1028 Valtype* wv = reinterpret_cast<Valtype*>(view);
1029 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1030 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1031 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1032 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1033 - address);
1034 val = This::insert_val_thumb_movw_movt(val, x);
1035 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1036 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1037 return This::STATUS_OKAY;
1040 // R_ARM_THM_MOVT_PREL: S + A - P
1041 static inline typename This::Status
1042 thm_movt_prel(unsigned char *view,
1043 const Sized_relobj<32, big_endian>* object,
1044 const Symbol_value<32>* psymval,
1045 elfcpp::Elf_types<32>::Elf_Addr address)
1047 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1048 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1049 Valtype* wv = reinterpret_cast<Valtype*>(view);
1050 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1051 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1052 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1053 Reltype x = (This::arm_symbol_value(object, psymval, addend, 0)
1054 - address) >> 16;
1055 val = This::insert_val_thumb_movw_movt(val, x);
1056 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1057 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1058 return This::STATUS_OKAY;
1062 // Get the GOT section, creating it if necessary.
1064 template<bool big_endian>
1065 Output_data_got<32, big_endian>*
1066 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
1068 if (this->got_ == NULL)
1070 gold_assert(symtab != NULL && layout != NULL);
1072 this->got_ = new Output_data_got<32, big_endian>();
1074 Output_section* os;
1075 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1076 (elfcpp::SHF_ALLOC
1077 | elfcpp::SHF_WRITE),
1078 this->got_);
1079 os->set_is_relro();
1081 // The old GNU linker creates a .got.plt section. We just
1082 // create another set of data in the .got section. Note that we
1083 // always create a PLT if we create a GOT, although the PLT
1084 // might be empty.
1085 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
1086 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1087 (elfcpp::SHF_ALLOC
1088 | elfcpp::SHF_WRITE),
1089 this->got_plt_);
1090 os->set_is_relro();
1092 // The first three entries are reserved.
1093 this->got_plt_->set_current_data_size(3 * 4);
1095 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1096 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1097 this->got_plt_,
1098 0, 0, elfcpp::STT_OBJECT,
1099 elfcpp::STB_LOCAL,
1100 elfcpp::STV_HIDDEN, 0,
1101 false, false);
1103 return this->got_;
1106 // Get the dynamic reloc section, creating it if necessary.
1108 template<bool big_endian>
1109 typename Target_arm<big_endian>::Reloc_section*
1110 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
1112 if (this->rel_dyn_ == NULL)
1114 gold_assert(layout != NULL);
1115 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
1116 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1117 elfcpp::SHF_ALLOC, this->rel_dyn_);
1119 return this->rel_dyn_;
1122 // A class to handle the PLT data.
1124 template<bool big_endian>
1125 class Output_data_plt_arm : public Output_section_data
1127 public:
1128 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1129 Reloc_section;
1131 Output_data_plt_arm(Layout*, Output_data_space*);
1133 // Add an entry to the PLT.
1134 void
1135 add_entry(Symbol* gsym);
1137 // Return the .rel.plt section data.
1138 const Reloc_section*
1139 rel_plt() const
1140 { return this->rel_; }
1142 protected:
1143 void
1144 do_adjust_output_section(Output_section* os);
1146 // Write to a map file.
1147 void
1148 do_print_to_mapfile(Mapfile* mapfile) const
1149 { mapfile->print_output_data(this, _("** PLT")); }
1151 private:
1152 // Template for the first PLT entry.
1153 static const uint32_t first_plt_entry[5];
1155 // Template for subsequent PLT entries.
1156 static const uint32_t plt_entry[3];
1158 // Set the final size.
1159 void
1160 set_final_data_size()
1162 this->set_data_size(sizeof(first_plt_entry)
1163 + this->count_ * sizeof(plt_entry));
1166 // Write out the PLT data.
1167 void
1168 do_write(Output_file*);
1170 // The reloc section.
1171 Reloc_section* rel_;
1172 // The .got.plt section.
1173 Output_data_space* got_plt_;
1174 // The number of PLT entries.
1175 unsigned int count_;
1178 // Create the PLT section. The ordinary .got section is an argument,
1179 // since we need to refer to the start. We also create our own .got
1180 // section just for PLT entries.
1182 template<bool big_endian>
1183 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
1184 Output_data_space* got_plt)
1185 : Output_section_data(4), got_plt_(got_plt), count_(0)
1187 this->rel_ = new Reloc_section(false);
1188 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1189 elfcpp::SHF_ALLOC, this->rel_);
1192 template<bool big_endian>
1193 void
1194 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
1196 os->set_entsize(0);
1199 // Add an entry to the PLT.
1201 template<bool big_endian>
1202 void
1203 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
1205 gold_assert(!gsym->has_plt_offset());
1207 // Note that when setting the PLT offset we skip the initial
1208 // reserved PLT entry.
1209 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
1210 + sizeof(first_plt_entry));
1212 ++this->count_;
1214 section_offset_type got_offset = this->got_plt_->current_data_size();
1216 // Every PLT entry needs a GOT entry which points back to the PLT
1217 // entry (this will be changed by the dynamic linker, normally
1218 // lazily when the function is called).
1219 this->got_plt_->set_current_data_size(got_offset + 4);
1221 // Every PLT entry needs a reloc.
1222 gsym->set_needs_dynsym_entry();
1223 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
1224 got_offset);
1226 // Note that we don't need to save the symbol. The contents of the
1227 // PLT are independent of which symbols are used. The symbols only
1228 // appear in the relocations.
1231 // ARM PLTs.
1232 // FIXME: This is not very flexible. Right now this has only been tested
1233 // on armv5te. If we are to support additional architecture features like
1234 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
1236 // The first entry in the PLT.
1237 template<bool big_endian>
1238 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
1240 0xe52de004, // str lr, [sp, #-4]!
1241 0xe59fe004, // ldr lr, [pc, #4]
1242 0xe08fe00e, // add lr, pc, lr
1243 0xe5bef008, // ldr pc, [lr, #8]!
1244 0x00000000, // &GOT[0] - .
1247 // Subsequent entries in the PLT.
1249 template<bool big_endian>
1250 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
1252 0xe28fc600, // add ip, pc, #0xNN00000
1253 0xe28cca00, // add ip, ip, #0xNN000
1254 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
1257 // Write out the PLT. This uses the hand-coded instructions above,
1258 // and adjusts them as needed. This is all specified by the arm ELF
1259 // Processor Supplement.
1261 template<bool big_endian>
1262 void
1263 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
1265 const off_t offset = this->offset();
1266 const section_size_type oview_size =
1267 convert_to_section_size_type(this->data_size());
1268 unsigned char* const oview = of->get_output_view(offset, oview_size);
1270 const off_t got_file_offset = this->got_plt_->offset();
1271 const section_size_type got_size =
1272 convert_to_section_size_type(this->got_plt_->data_size());
1273 unsigned char* const got_view = of->get_output_view(got_file_offset,
1274 got_size);
1275 unsigned char* pov = oview;
1277 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
1278 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
1280 // Write first PLT entry. All but the last word are constants.
1281 const size_t num_first_plt_words = (sizeof(first_plt_entry)
1282 / sizeof(plt_entry[0]));
1283 for (size_t i = 0; i < num_first_plt_words - 1; i++)
1284 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
1285 // Last word in first PLT entry is &GOT[0] - .
1286 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
1287 got_address - (plt_address + 16));
1288 pov += sizeof(first_plt_entry);
1290 unsigned char* got_pov = got_view;
1292 memset(got_pov, 0, 12);
1293 got_pov += 12;
1295 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
1296 unsigned int plt_offset = sizeof(first_plt_entry);
1297 unsigned int plt_rel_offset = 0;
1298 unsigned int got_offset = 12;
1299 const unsigned int count = this->count_;
1300 for (unsigned int i = 0;
1301 i < count;
1302 ++i,
1303 pov += sizeof(plt_entry),
1304 got_pov += 4,
1305 plt_offset += sizeof(plt_entry),
1306 plt_rel_offset += rel_size,
1307 got_offset += 4)
1309 // Set and adjust the PLT entry itself.
1310 int32_t offset = ((got_address + got_offset)
1311 - (plt_address + plt_offset + 8));
1313 gold_assert(offset >= 0 && offset < 0x0fffffff);
1314 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
1315 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
1316 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
1317 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
1318 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
1319 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
1321 // Set the entry in the GOT.
1322 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
1325 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1326 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1328 of->write_output_view(offset, oview_size, oview);
1329 of->write_output_view(got_file_offset, got_size, got_view);
1332 // Create a PLT entry for a global symbol.
1334 template<bool big_endian>
1335 void
1336 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
1337 Symbol* gsym)
1339 if (gsym->has_plt_offset())
1340 return;
1342 if (this->plt_ == NULL)
1344 // Create the GOT sections first.
1345 this->got_section(symtab, layout);
1347 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
1348 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1349 (elfcpp::SHF_ALLOC
1350 | elfcpp::SHF_EXECINSTR),
1351 this->plt_);
1353 this->plt_->add_entry(gsym);
1356 // Report an unsupported relocation against a local symbol.
1358 template<bool big_endian>
1359 void
1360 Target_arm<big_endian>::Scan::unsupported_reloc_local(
1361 Sized_relobj<32, big_endian>* object,
1362 unsigned int r_type)
1364 gold_error(_("%s: unsupported reloc %u against local symbol"),
1365 object->name().c_str(), r_type);
1368 // We are about to emit a dynamic relocation of type R_TYPE. If the
1369 // dynamic linker does not support it, issue an error. The GNU linker
1370 // only issues a non-PIC error for an allocated read-only section.
1371 // Here we know the section is allocated, but we don't know that it is
1372 // read-only. But we check for all the relocation types which the
1373 // glibc dynamic linker supports, so it seems appropriate to issue an
1374 // error even if the section is not read-only.
1376 template<bool big_endian>
1377 void
1378 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
1379 unsigned int r_type)
1381 switch (r_type)
1383 // These are the relocation types supported by glibc for ARM.
1384 case elfcpp::R_ARM_RELATIVE:
1385 case elfcpp::R_ARM_COPY:
1386 case elfcpp::R_ARM_GLOB_DAT:
1387 case elfcpp::R_ARM_JUMP_SLOT:
1388 case elfcpp::R_ARM_ABS32:
1389 case elfcpp::R_ARM_ABS32_NOI:
1390 case elfcpp::R_ARM_PC24:
1391 // FIXME: The following 3 types are not supported by Android's dynamic
1392 // linker.
1393 case elfcpp::R_ARM_TLS_DTPMOD32:
1394 case elfcpp::R_ARM_TLS_DTPOFF32:
1395 case elfcpp::R_ARM_TLS_TPOFF32:
1396 return;
1398 default:
1399 // This prevents us from issuing more than one error per reloc
1400 // section. But we can still wind up issuing more than one
1401 // error per object file.
1402 if (this->issued_non_pic_error_)
1403 return;
1404 object->error(_("requires unsupported dynamic reloc; "
1405 "recompile with -fPIC"));
1406 this->issued_non_pic_error_ = true;
1407 return;
1409 case elfcpp::R_ARM_NONE:
1410 gold_unreachable();
1414 // Scan a relocation for a local symbol.
1415 // FIXME: This only handles a subset of relocation types used by Android
1416 // on ARM v5te devices.
1418 template<bool big_endian>
1419 inline void
1420 Target_arm<big_endian>::Scan::local(const General_options&,
1421 Symbol_table* symtab,
1422 Layout* layout,
1423 Target_arm* target,
1424 Sized_relobj<32, big_endian>* object,
1425 unsigned int data_shndx,
1426 Output_section* output_section,
1427 const elfcpp::Rel<32, big_endian>& reloc,
1428 unsigned int r_type,
1429 const elfcpp::Sym<32, big_endian>&)
1431 r_type = get_real_reloc_type(r_type);
1432 switch (r_type)
1434 case elfcpp::R_ARM_NONE:
1435 break;
1437 case elfcpp::R_ARM_ABS32:
1438 case elfcpp::R_ARM_ABS32_NOI:
1439 // If building a shared library (or a position-independent
1440 // executable), we need to create a dynamic relocation for
1441 // this location. The relocation applied at link time will
1442 // apply the link-time value, so we flag the location with
1443 // an R_ARM_RELATIVE relocation so the dynamic loader can
1444 // relocate it easily.
1445 if (parameters->options().output_is_position_independent())
1447 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1448 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1449 // If we are to add more other reloc types than R_ARM_ABS32,
1450 // we need to add check_non_pic(object, r_type) here.
1451 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
1452 output_section, data_shndx,
1453 reloc.get_r_offset());
1455 break;
1457 case elfcpp::R_ARM_REL32:
1458 case elfcpp::R_ARM_THM_CALL:
1459 case elfcpp::R_ARM_CALL:
1460 case elfcpp::R_ARM_PREL31:
1461 case elfcpp::R_ARM_JUMP24:
1462 case elfcpp::R_ARM_PLT32:
1463 case elfcpp::R_ARM_THM_ABS5:
1464 case elfcpp::R_ARM_ABS8:
1465 case elfcpp::R_ARM_ABS12:
1466 case elfcpp::R_ARM_ABS16:
1467 case elfcpp::R_ARM_BASE_ABS:
1468 case elfcpp::R_ARM_MOVW_ABS_NC:
1469 case elfcpp::R_ARM_MOVT_ABS:
1470 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
1471 case elfcpp::R_ARM_THM_MOVT_ABS:
1472 case elfcpp::R_ARM_MOVW_PREL_NC:
1473 case elfcpp::R_ARM_MOVT_PREL:
1474 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
1475 case elfcpp::R_ARM_THM_MOVT_PREL:
1476 break;
1478 case elfcpp::R_ARM_GOTOFF32:
1479 // We need a GOT section:
1480 target->got_section(symtab, layout);
1481 break;
1483 case elfcpp::R_ARM_BASE_PREL:
1484 // FIXME: What about this?
1485 break;
1487 case elfcpp::R_ARM_GOT_BREL:
1488 case elfcpp::R_ARM_GOT_PREL:
1490 // The symbol requires a GOT entry.
1491 Output_data_got<32, big_endian>* got =
1492 target->got_section(symtab, layout);
1493 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1494 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1496 // If we are generating a shared object, we need to add a
1497 // dynamic RELATIVE relocation for this symbol's GOT entry.
1498 if (parameters->options().output_is_position_independent())
1500 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1501 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1502 rel_dyn->add_local_relative(
1503 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
1504 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
1508 break;
1510 case elfcpp::R_ARM_TARGET1:
1511 // This should have been mapped to another type already.
1512 // Fall through.
1513 case elfcpp::R_ARM_COPY:
1514 case elfcpp::R_ARM_GLOB_DAT:
1515 case elfcpp::R_ARM_JUMP_SLOT:
1516 case elfcpp::R_ARM_RELATIVE:
1517 // These are relocations which should only be seen by the
1518 // dynamic linker, and should never be seen here.
1519 gold_error(_("%s: unexpected reloc %u in object file"),
1520 object->name().c_str(), r_type);
1521 break;
1523 default:
1524 unsupported_reloc_local(object, r_type);
1525 break;
1529 // Report an unsupported relocation against a global symbol.
1531 template<bool big_endian>
1532 void
1533 Target_arm<big_endian>::Scan::unsupported_reloc_global(
1534 Sized_relobj<32, big_endian>* object,
1535 unsigned int r_type,
1536 Symbol* gsym)
1538 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1539 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1542 // Scan a relocation for a global symbol.
1543 // FIXME: This only handles a subset of relocation types used by Android
1544 // on ARM v5te devices.
1546 template<bool big_endian>
1547 inline void
1548 Target_arm<big_endian>::Scan::global(const General_options&,
1549 Symbol_table* symtab,
1550 Layout* layout,
1551 Target_arm* target,
1552 Sized_relobj<32, big_endian>* object,
1553 unsigned int data_shndx,
1554 Output_section* output_section,
1555 const elfcpp::Rel<32, big_endian>& reloc,
1556 unsigned int r_type,
1557 Symbol* gsym)
1559 r_type = get_real_reloc_type(r_type);
1560 switch (r_type)
1562 case elfcpp::R_ARM_NONE:
1563 break;
1565 case elfcpp::R_ARM_ABS32:
1566 case elfcpp::R_ARM_ABS32_NOI:
1568 // Make a dynamic relocation if necessary.
1569 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1571 if (target->may_need_copy_reloc(gsym))
1573 target->copy_reloc(symtab, layout, object,
1574 data_shndx, output_section, gsym, reloc);
1576 else if (gsym->can_use_relative_reloc(false))
1578 // If we are to add more other reloc types than R_ARM_ABS32,
1579 // we need to add check_non_pic(object, r_type) here.
1580 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1581 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
1582 output_section, object,
1583 data_shndx, reloc.get_r_offset());
1585 else
1587 // If we are to add more other reloc types than R_ARM_ABS32,
1588 // we need to add check_non_pic(object, r_type) here.
1589 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1590 rel_dyn->add_global(gsym, r_type, output_section, object,
1591 data_shndx, reloc.get_r_offset());
1595 break;
1597 case elfcpp::R_ARM_MOVW_ABS_NC:
1598 case elfcpp::R_ARM_MOVT_ABS:
1599 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
1600 case elfcpp::R_ARM_THM_MOVT_ABS:
1601 case elfcpp::R_ARM_MOVW_PREL_NC:
1602 case elfcpp::R_ARM_MOVT_PREL:
1603 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
1604 case elfcpp::R_ARM_THM_MOVT_PREL:
1605 break;
1607 case elfcpp::R_ARM_THM_ABS5:
1608 case elfcpp::R_ARM_ABS8:
1609 case elfcpp::R_ARM_ABS12:
1610 case elfcpp::R_ARM_ABS16:
1611 case elfcpp::R_ARM_BASE_ABS:
1613 // No dynamic relocs of this kinds.
1614 // Report the error in case of PIC.
1615 int flags = Symbol::NON_PIC_REF;
1616 if (gsym->type() == elfcpp::STT_FUNC
1617 || gsym->type() == elfcpp::STT_ARM_TFUNC)
1618 flags |= Symbol::FUNCTION_CALL;
1619 if (gsym->needs_dynamic_reloc(flags))
1620 check_non_pic(object, r_type);
1622 break;
1624 case elfcpp::R_ARM_REL32:
1625 case elfcpp::R_ARM_PREL31:
1627 // Make a dynamic relocation if necessary.
1628 int flags = Symbol::NON_PIC_REF;
1629 if (gsym->needs_dynamic_reloc(flags))
1631 if (target->may_need_copy_reloc(gsym))
1633 target->copy_reloc(symtab, layout, object,
1634 data_shndx, output_section, gsym, reloc);
1636 else
1638 check_non_pic(object, r_type);
1639 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1640 rel_dyn->add_global(gsym, r_type, output_section, object,
1641 data_shndx, reloc.get_r_offset());
1645 break;
1647 case elfcpp::R_ARM_JUMP24:
1648 case elfcpp::R_ARM_THM_CALL:
1649 case elfcpp::R_ARM_CALL:
1651 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
1652 target->make_plt_entry(symtab, layout, gsym);
1653 // Make a dynamic relocation if necessary.
1654 int flags = Symbol::NON_PIC_REF;
1655 if (gsym->type() == elfcpp::STT_FUNC
1656 || gsym->type() == elfcpp::STT_ARM_TFUNC)
1657 flags |= Symbol::FUNCTION_CALL;
1658 if (gsym->needs_dynamic_reloc(flags))
1660 if (target->may_need_copy_reloc(gsym))
1662 target->copy_reloc(symtab, layout, object,
1663 data_shndx, output_section, gsym,
1664 reloc);
1666 else
1668 check_non_pic(object, r_type);
1669 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1670 rel_dyn->add_global(gsym, r_type, output_section, object,
1671 data_shndx, reloc.get_r_offset());
1675 break;
1677 case elfcpp::R_ARM_PLT32:
1678 // If the symbol is fully resolved, this is just a relative
1679 // local reloc. Otherwise we need a PLT entry.
1680 if (gsym->final_value_is_known())
1681 break;
1682 // If building a shared library, we can also skip the PLT entry
1683 // if the symbol is defined in the output file and is protected
1684 // or hidden.
1685 if (gsym->is_defined()
1686 && !gsym->is_from_dynobj()
1687 && !gsym->is_preemptible())
1688 break;
1689 target->make_plt_entry(symtab, layout, gsym);
1690 break;
1692 case elfcpp::R_ARM_GOTOFF32:
1693 // We need a GOT section.
1694 target->got_section(symtab, layout);
1695 break;
1697 case elfcpp::R_ARM_BASE_PREL:
1698 // FIXME: What about this?
1699 break;
1701 case elfcpp::R_ARM_GOT_BREL:
1702 case elfcpp::R_ARM_GOT_PREL:
1704 // The symbol requires a GOT entry.
1705 Output_data_got<32, big_endian>* got =
1706 target->got_section(symtab, layout);
1707 if (gsym->final_value_is_known())
1708 got->add_global(gsym, GOT_TYPE_STANDARD);
1709 else
1711 // If this symbol is not fully resolved, we need to add a
1712 // GOT entry with a dynamic relocation.
1713 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1714 if (gsym->is_from_dynobj()
1715 || gsym->is_undefined()
1716 || gsym->is_preemptible())
1717 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1718 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
1719 else
1721 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1722 rel_dyn->add_global_relative(
1723 gsym, elfcpp::R_ARM_RELATIVE, got,
1724 gsym->got_offset(GOT_TYPE_STANDARD));
1728 break;
1730 case elfcpp::R_ARM_TARGET1:
1731 // This should have been mapped to another type already.
1732 // Fall through.
1733 case elfcpp::R_ARM_COPY:
1734 case elfcpp::R_ARM_GLOB_DAT:
1735 case elfcpp::R_ARM_JUMP_SLOT:
1736 case elfcpp::R_ARM_RELATIVE:
1737 // These are relocations which should only be seen by the
1738 // dynamic linker, and should never be seen here.
1739 gold_error(_("%s: unexpected reloc %u in object file"),
1740 object->name().c_str(), r_type);
1741 break;
1743 default:
1744 unsupported_reloc_global(object, r_type, gsym);
1745 break;
1749 // Process relocations for gc.
1751 template<bool big_endian>
1752 void
1753 Target_arm<big_endian>::gc_process_relocs(const General_options& options,
1754 Symbol_table* symtab,
1755 Layout* layout,
1756 Sized_relobj<32, big_endian>* object,
1757 unsigned int data_shndx,
1758 unsigned int,
1759 const unsigned char* prelocs,
1760 size_t reloc_count,
1761 Output_section* output_section,
1762 bool needs_special_offset_handling,
1763 size_t local_symbol_count,
1764 const unsigned char* plocal_symbols)
1766 typedef Target_arm<big_endian> Arm;
1767 typedef typename Target_arm<big_endian>::Scan Scan;
1769 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
1770 options,
1771 symtab,
1772 layout,
1773 this,
1774 object,
1775 data_shndx,
1776 prelocs,
1777 reloc_count,
1778 output_section,
1779 needs_special_offset_handling,
1780 local_symbol_count,
1781 plocal_symbols);
1784 // Scan relocations for a section.
1786 template<bool big_endian>
1787 void
1788 Target_arm<big_endian>::scan_relocs(const General_options& options,
1789 Symbol_table* symtab,
1790 Layout* layout,
1791 Sized_relobj<32, big_endian>* object,
1792 unsigned int data_shndx,
1793 unsigned int sh_type,
1794 const unsigned char* prelocs,
1795 size_t reloc_count,
1796 Output_section* output_section,
1797 bool needs_special_offset_handling,
1798 size_t local_symbol_count,
1799 const unsigned char* plocal_symbols)
1801 typedef typename Target_arm<big_endian>::Scan Scan;
1802 if (sh_type == elfcpp::SHT_RELA)
1804 gold_error(_("%s: unsupported RELA reloc section"),
1805 object->name().c_str());
1806 return;
1809 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
1810 options,
1811 symtab,
1812 layout,
1813 this,
1814 object,
1815 data_shndx,
1816 prelocs,
1817 reloc_count,
1818 output_section,
1819 needs_special_offset_handling,
1820 local_symbol_count,
1821 plocal_symbols);
1824 // Finalize the sections.
1826 template<bool big_endian>
1827 void
1828 Target_arm<big_endian>::do_finalize_sections(Layout* layout)
1830 // Fill in some more dynamic tags.
1831 Output_data_dynamic* const odyn = layout->dynamic_data();
1832 if (odyn != NULL)
1834 if (this->got_plt_ != NULL)
1835 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1837 if (this->plt_ != NULL)
1839 const Output_data* od = this->plt_->rel_plt();
1840 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1841 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1842 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1845 if (this->rel_dyn_ != NULL)
1847 const Output_data* od = this->rel_dyn_;
1848 odyn->add_section_address(elfcpp::DT_REL, od);
1849 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1850 odyn->add_constant(elfcpp::DT_RELENT,
1851 elfcpp::Elf_sizes<32>::rel_size);
1854 if (!parameters->options().shared())
1856 // The value of the DT_DEBUG tag is filled in by the dynamic
1857 // linker at run time, and used by the debugger.
1858 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1862 // Emit any relocs we saved in an attempt to avoid generating COPY
1863 // relocs.
1864 if (this->copy_relocs_.any_saved_relocs())
1865 this->copy_relocs_.emit(this->rel_dyn_section(layout));
1867 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
1868 // the .ARM.exidx section.
1869 if (!layout->script_options()->saw_phdrs_clause()
1870 && !parameters->options().relocatable())
1872 Output_section* exidx_section =
1873 layout->find_output_section(".ARM.exidx");
1875 if (exidx_section != NULL
1876 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
1878 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
1879 == NULL);
1880 Output_segment* exidx_segment =
1881 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
1882 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
1887 // Return whether a direct absolute static relocation needs to be applied.
1888 // In cases where Scan::local() or Scan::global() has created
1889 // a dynamic relocation other than R_ARM_RELATIVE, the addend
1890 // of the relocation is carried in the data, and we must not
1891 // apply the static relocation.
1893 template<bool big_endian>
1894 inline bool
1895 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
1896 const Sized_symbol<32>* gsym,
1897 int ref_flags,
1898 bool is_32bit,
1899 Output_section* output_section)
1901 // If the output section is not allocated, then we didn't call
1902 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1903 // the reloc here.
1904 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1905 return true;
1907 // For local symbols, we will have created a non-RELATIVE dynamic
1908 // relocation only if (a) the output is position independent,
1909 // (b) the relocation is absolute (not pc- or segment-relative), and
1910 // (c) the relocation is not 32 bits wide.
1911 if (gsym == NULL)
1912 return !(parameters->options().output_is_position_independent()
1913 && (ref_flags & Symbol::ABSOLUTE_REF)
1914 && !is_32bit);
1916 // For global symbols, we use the same helper routines used in the
1917 // scan pass. If we did not create a dynamic relocation, or if we
1918 // created a RELATIVE dynamic relocation, we should apply the static
1919 // relocation.
1920 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1921 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1922 && gsym->can_use_relative_reloc(ref_flags
1923 & Symbol::FUNCTION_CALL);
1924 return !has_dyn || is_rel;
1927 // Perform a relocation.
1929 template<bool big_endian>
1930 inline bool
1931 Target_arm<big_endian>::Relocate::relocate(
1932 const Relocate_info<32, big_endian>* relinfo,
1933 Target_arm* target,
1934 Output_section *output_section,
1935 size_t relnum,
1936 const elfcpp::Rel<32, big_endian>& rel,
1937 unsigned int r_type,
1938 const Sized_symbol<32>* gsym,
1939 const Symbol_value<32>* psymval,
1940 unsigned char* view,
1941 elfcpp::Elf_types<32>::Elf_Addr address,
1942 section_size_type /* view_size */ )
1944 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
1946 r_type = get_real_reloc_type(r_type);
1948 // If this the symbol may be a Thumb function, set thumb bit to 1.
1949 bool has_thumb_bit = ((gsym != NULL)
1950 && (gsym->type() == elfcpp::STT_FUNC
1951 || gsym->type() == elfcpp::STT_ARM_TFUNC));
1953 // Pick the value to use for symbols defined in shared objects.
1954 Symbol_value<32> symval;
1955 if (gsym != NULL
1956 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
1958 symval.set_output_value(target->plt_section()->address()
1959 + gsym->plt_offset());
1960 psymval = &symval;
1961 has_thumb_bit = 0;
1964 const Sized_relobj<32, big_endian>* object = relinfo->object;
1966 // Get the GOT offset if needed.
1967 // The GOT pointer points to the end of the GOT section.
1968 // We need to subtract the size of the GOT section to get
1969 // the actual offset to use in the relocation.
1970 bool have_got_offset = false;
1971 unsigned int got_offset = 0;
1972 switch (r_type)
1974 case elfcpp::R_ARM_GOT_BREL:
1975 case elfcpp::R_ARM_GOT_PREL:
1976 if (gsym != NULL)
1978 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1979 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1980 - target->got_size());
1982 else
1984 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1985 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1986 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1987 - target->got_size());
1989 have_got_offset = true;
1990 break;
1992 default:
1993 break;
1996 typename Arm_relocate_functions::Status reloc_status =
1997 Arm_relocate_functions::STATUS_OKAY;
1998 switch (r_type)
2000 case elfcpp::R_ARM_NONE:
2001 break;
2003 case elfcpp::R_ARM_ABS8:
2004 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2005 output_section))
2006 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
2007 break;
2009 case elfcpp::R_ARM_ABS12:
2010 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2011 output_section))
2012 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
2013 break;
2015 case elfcpp::R_ARM_ABS16:
2016 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2017 output_section))
2018 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
2019 break;
2021 case elfcpp::R_ARM_ABS32:
2022 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2023 output_section))
2024 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
2025 has_thumb_bit);
2026 break;
2028 case elfcpp::R_ARM_ABS32_NOI:
2029 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2030 output_section))
2031 // No thumb bit for this relocation: (S + A)
2032 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
2033 false);
2034 break;
2036 case elfcpp::R_ARM_MOVW_ABS_NC:
2037 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2038 output_section))
2039 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
2040 psymval,
2041 has_thumb_bit);
2042 else
2043 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
2044 "a shared object; recompile with -fPIC"));
2045 break;
2047 case elfcpp::R_ARM_MOVT_ABS:
2048 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2049 output_section))
2050 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
2051 else
2052 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
2053 "a shared object; recompile with -fPIC"));
2054 break;
2056 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
2057 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2058 output_section))
2059 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
2060 psymval,
2061 has_thumb_bit);
2062 else
2063 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
2064 "making a shared object; recompile with -fPIC"));
2065 break;
2067 case elfcpp::R_ARM_THM_MOVT_ABS:
2068 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2069 output_section))
2070 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
2071 psymval);
2072 else
2073 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
2074 "making a shared object; recompile with -fPIC"));
2075 break;
2077 case elfcpp::R_ARM_MOVW_PREL_NC:
2078 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
2079 psymval, address,
2080 has_thumb_bit);
2081 break;
2083 case elfcpp::R_ARM_MOVT_PREL:
2084 reloc_status = Arm_relocate_functions::movt_prel(view, object,
2085 psymval, address);
2086 break;
2088 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
2089 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
2090 psymval, address,
2091 has_thumb_bit);
2092 break;
2094 case elfcpp::R_ARM_THM_MOVT_PREL:
2095 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
2096 psymval, address);
2097 break;
2099 case elfcpp::R_ARM_REL32:
2100 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2101 address, has_thumb_bit);
2102 break;
2104 case elfcpp::R_ARM_THM_ABS5:
2105 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2106 output_section))
2107 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
2108 break;
2110 case elfcpp::R_ARM_THM_CALL:
2111 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
2112 address, has_thumb_bit);
2113 break;
2115 case elfcpp::R_ARM_GOTOFF32:
2117 elfcpp::Elf_types<32>::Elf_Addr got_origin;
2118 got_origin = target->got_plt_section()->address();
2119 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2120 got_origin, has_thumb_bit);
2122 break;
2124 case elfcpp::R_ARM_BASE_PREL:
2126 uint32_t origin;
2127 // Get the addressing origin of the output segment defining the
2128 // symbol gsym (AAELF 4.6.1.2 Relocation types)
2129 gold_assert(gsym != NULL);
2130 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
2131 origin = gsym->output_segment()->vaddr();
2132 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
2133 origin = gsym->output_data()->address();
2134 else
2136 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2137 _("cannot find origin of R_ARM_BASE_PREL"));
2138 return true;
2140 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
2142 break;
2144 case elfcpp::R_ARM_BASE_ABS:
2146 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2147 output_section))
2148 break;
2150 uint32_t origin;
2151 // Get the addressing origin of the output segment defining
2152 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
2153 if (gsym == NULL)
2154 // R_ARM_BASE_ABS with the NULL symbol will give the
2155 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
2156 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
2157 origin = target->got_plt_section()->address();
2158 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
2159 origin = gsym->output_segment()->vaddr();
2160 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
2161 origin = gsym->output_data()->address();
2162 else
2164 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2165 _("cannot find origin of R_ARM_BASE_ABS"));
2166 return true;
2169 reloc_status = Arm_relocate_functions::base_abs(view, origin);
2171 break;
2173 case elfcpp::R_ARM_GOT_BREL:
2174 gold_assert(have_got_offset);
2175 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
2176 break;
2178 case elfcpp::R_ARM_GOT_PREL:
2179 gold_assert(have_got_offset);
2180 // Get the address origin for GOT PLT, which is allocated right
2181 // after the GOT section, to calculate an absolute address of
2182 // the symbol GOT entry (got_origin + got_offset).
2183 elfcpp::Elf_types<32>::Elf_Addr got_origin;
2184 got_origin = target->got_plt_section()->address();
2185 reloc_status = Arm_relocate_functions::got_prel(view,
2186 got_origin + got_offset,
2187 address);
2188 break;
2190 case elfcpp::R_ARM_PLT32:
2191 gold_assert(gsym == NULL
2192 || gsym->has_plt_offset()
2193 || gsym->final_value_is_known()
2194 || (gsym->is_defined()
2195 && !gsym->is_from_dynobj()
2196 && !gsym->is_preemptible()));
2197 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
2198 address, has_thumb_bit);
2199 break;
2201 case elfcpp::R_ARM_CALL:
2202 reloc_status = Arm_relocate_functions::call(view, object, psymval,
2203 address, has_thumb_bit);
2204 break;
2206 case elfcpp::R_ARM_JUMP24:
2207 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
2208 address, has_thumb_bit);
2209 break;
2211 case elfcpp::R_ARM_PREL31:
2212 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
2213 address, has_thumb_bit);
2214 break;
2216 case elfcpp::R_ARM_TARGET1:
2217 // This should have been mapped to another type already.
2218 // Fall through.
2219 case elfcpp::R_ARM_COPY:
2220 case elfcpp::R_ARM_GLOB_DAT:
2221 case elfcpp::R_ARM_JUMP_SLOT:
2222 case elfcpp::R_ARM_RELATIVE:
2223 // These are relocations which should only be seen by the
2224 // dynamic linker, and should never be seen here.
2225 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2226 _("unexpected reloc %u in object file"),
2227 r_type);
2228 break;
2230 default:
2231 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2232 _("unsupported reloc %u"),
2233 r_type);
2234 break;
2237 // Report any errors.
2238 switch (reloc_status)
2240 case Arm_relocate_functions::STATUS_OKAY:
2241 break;
2242 case Arm_relocate_functions::STATUS_OVERFLOW:
2243 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2244 _("relocation overflow in relocation %u"),
2245 r_type);
2246 break;
2247 case Arm_relocate_functions::STATUS_BAD_RELOC:
2248 gold_error_at_location(
2249 relinfo,
2250 relnum,
2251 rel.get_r_offset(),
2252 _("unexpected opcode while processing relocation %u"),
2253 r_type);
2254 break;
2255 default:
2256 gold_unreachable();
2259 return true;
2262 // Relocate section data.
2264 template<bool big_endian>
2265 void
2266 Target_arm<big_endian>::relocate_section(
2267 const Relocate_info<32, big_endian>* relinfo,
2268 unsigned int sh_type,
2269 const unsigned char* prelocs,
2270 size_t reloc_count,
2271 Output_section* output_section,
2272 bool needs_special_offset_handling,
2273 unsigned char* view,
2274 elfcpp::Elf_types<32>::Elf_Addr address,
2275 section_size_type view_size,
2276 const Reloc_symbol_changes* reloc_symbol_changes)
2278 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
2279 gold_assert(sh_type == elfcpp::SHT_REL);
2281 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
2282 Arm_relocate>(
2283 relinfo,
2284 this,
2285 prelocs,
2286 reloc_count,
2287 output_section,
2288 needs_special_offset_handling,
2289 view,
2290 address,
2291 view_size,
2292 reloc_symbol_changes);
2295 // Return the size of a relocation while scanning during a relocatable
2296 // link.
2298 template<bool big_endian>
2299 unsigned int
2300 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
2301 unsigned int r_type,
2302 Relobj* object)
2304 r_type = get_real_reloc_type(r_type);
2305 switch (r_type)
2307 case elfcpp::R_ARM_NONE:
2308 return 0;
2310 case elfcpp::R_ARM_ABS8:
2311 return 1;
2313 case elfcpp::R_ARM_ABS16:
2314 case elfcpp::R_ARM_THM_ABS5:
2315 return 2;
2317 case elfcpp::R_ARM_ABS32:
2318 case elfcpp::R_ARM_ABS32_NOI:
2319 case elfcpp::R_ARM_ABS12:
2320 case elfcpp::R_ARM_BASE_ABS:
2321 case elfcpp::R_ARM_REL32:
2322 case elfcpp::R_ARM_THM_CALL:
2323 case elfcpp::R_ARM_GOTOFF32:
2324 case elfcpp::R_ARM_BASE_PREL:
2325 case elfcpp::R_ARM_GOT_BREL:
2326 case elfcpp::R_ARM_GOT_PREL:
2327 case elfcpp::R_ARM_PLT32:
2328 case elfcpp::R_ARM_CALL:
2329 case elfcpp::R_ARM_JUMP24:
2330 case elfcpp::R_ARM_PREL31:
2331 case elfcpp::R_ARM_MOVW_ABS_NC:
2332 case elfcpp::R_ARM_MOVT_ABS:
2333 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
2334 case elfcpp::R_ARM_THM_MOVT_ABS:
2335 case elfcpp::R_ARM_MOVW_PREL_NC:
2336 case elfcpp::R_ARM_MOVT_PREL:
2337 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
2338 case elfcpp::R_ARM_THM_MOVT_PREL:
2339 return 4;
2341 case elfcpp::R_ARM_TARGET1:
2342 // This should have been mapped to another type already.
2343 // Fall through.
2344 case elfcpp::R_ARM_COPY:
2345 case elfcpp::R_ARM_GLOB_DAT:
2346 case elfcpp::R_ARM_JUMP_SLOT:
2347 case elfcpp::R_ARM_RELATIVE:
2348 // These are relocations which should only be seen by the
2349 // dynamic linker, and should never be seen here.
2350 gold_error(_("%s: unexpected reloc %u in object file"),
2351 object->name().c_str(), r_type);
2352 return 0;
2354 default:
2355 object->error(_("unsupported reloc %u in object file"), r_type);
2356 return 0;
2360 // Scan the relocs during a relocatable link.
2362 template<bool big_endian>
2363 void
2364 Target_arm<big_endian>::scan_relocatable_relocs(
2365 const General_options& options,
2366 Symbol_table* symtab,
2367 Layout* layout,
2368 Sized_relobj<32, big_endian>* object,
2369 unsigned int data_shndx,
2370 unsigned int sh_type,
2371 const unsigned char* prelocs,
2372 size_t reloc_count,
2373 Output_section* output_section,
2374 bool needs_special_offset_handling,
2375 size_t local_symbol_count,
2376 const unsigned char* plocal_symbols,
2377 Relocatable_relocs* rr)
2379 gold_assert(sh_type == elfcpp::SHT_REL);
2381 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2382 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2384 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
2385 Scan_relocatable_relocs>(
2386 options,
2387 symtab,
2388 layout,
2389 object,
2390 data_shndx,
2391 prelocs,
2392 reloc_count,
2393 output_section,
2394 needs_special_offset_handling,
2395 local_symbol_count,
2396 plocal_symbols,
2397 rr);
2400 // Relocate a section during a relocatable link.
2402 template<bool big_endian>
2403 void
2404 Target_arm<big_endian>::relocate_for_relocatable(
2405 const Relocate_info<32, big_endian>* relinfo,
2406 unsigned int sh_type,
2407 const unsigned char* prelocs,
2408 size_t reloc_count,
2409 Output_section* output_section,
2410 off_t offset_in_output_section,
2411 const Relocatable_relocs* rr,
2412 unsigned char* view,
2413 elfcpp::Elf_types<32>::Elf_Addr view_address,
2414 section_size_type view_size,
2415 unsigned char* reloc_view,
2416 section_size_type reloc_view_size)
2418 gold_assert(sh_type == elfcpp::SHT_REL);
2420 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
2421 relinfo,
2422 prelocs,
2423 reloc_count,
2424 output_section,
2425 offset_in_output_section,
2427 view,
2428 view_address,
2429 view_size,
2430 reloc_view,
2431 reloc_view_size);
2434 // Return the value to use for a dynamic symbol which requires special
2435 // treatment. This is how we support equality comparisons of function
2436 // pointers across shared library boundaries, as described in the
2437 // processor specific ABI supplement.
2439 template<bool big_endian>
2440 uint64_t
2441 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
2443 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2444 return this->plt_section()->address() + gsym->plt_offset();
2447 // Map platform-specific relocs to real relocs
2449 template<bool big_endian>
2450 unsigned int
2451 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
2453 switch (r_type)
2455 case elfcpp::R_ARM_TARGET1:
2456 // This is either R_ARM_ABS32 or R_ARM_REL32;
2457 return elfcpp::R_ARM_ABS32;
2459 case elfcpp::R_ARM_TARGET2:
2460 // This can be any reloc type but ususally is R_ARM_GOT_PREL
2461 return elfcpp::R_ARM_GOT_PREL;
2463 default:
2464 return r_type;
2468 // The selector for arm object files.
2470 template<bool big_endian>
2471 class Target_selector_arm : public Target_selector
2473 public:
2474 Target_selector_arm()
2475 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
2476 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
2479 Target*
2480 do_instantiate_target()
2481 { return new Target_arm<big_endian>(); }
2484 Target_selector_arm<false> target_selector_arm;
2485 Target_selector_arm<true> target_selector_armbe;
2487 } // End anonymous namespace.