Merge some FRV FDPIC changes into the Blackfin code.
[deliverable/binutils-gdb.git] / gold / arm.cc
CommitLineData
4a657b0d
DK
1// arm.cc -- arm target support for gold.
2
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>.
6
7// This file is part of gold.
8
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.
13
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.
18
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.
23
24#include "gold.h"
25
26#include <cstring>
27#include <limits>
28#include <cstdio>
29#include <string>
30
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
46namespace
47{
48
49using namespace gold;
50
94cdfcff
DK
51template<bool big_endian>
52class Output_data_plt_arm;
53
4a657b0d
DK
54// The arm target class.
55//
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
58// are supported.
59//
60// R_ARM_NONE
61// R_ARM_ABS32
62// R_ARM_REL32
63// R_ARM_THM_CALL
64// R_ARM_COPY
65// R_ARM_GLOB_DAT
66// R_ARM_BASE_PREL
67// R_ARM_JUMP_SLOT
68// R_ARM_RELATIVE
69// R_ARM_GOTOFF32
70// R_ARM_GOT_BREL
71// R_ARM_PLT32
72// R_ARM_CALL
73// R_ARM_JUMP24
74// R_ARM_TARGET1
75// R_ARM_PREL31
76//
77// Coming soon (pending patches):
4a657b0d
DK
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.
82//
83// TODOs:
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.
94cdfcff
DK
87// - Make PLTs more flexible for different architecture features like
88// Thumb-2 and BE8.
4a657b0d 89
c121c671
DK
90// Utilities for manipulating integers of up to 32-bits
91
92namespace utils
93{
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.
96 template<int no_bits>
97 static inline int32_t
98 sign_extend(uint32_t bits)
99 {
96d49306 100 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
101 if (no_bits == 32)
102 return static_cast<int32_t>(bits);
103 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
104 bits &= mask;
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;
108 }
109
110 // Detects overflow of an NO_BITS integer stored in a uint32_t.
111 template<int no_bits>
112 static inline bool
113 has_overflow(uint32_t bits)
114 {
96d49306 115 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
116 if (no_bits == 32)
117 return false;
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;
122 }
123
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); }
130};
131
4a657b0d
DK
132template<bool big_endian>
133class Target_arm : public Sized_target<32, big_endian>
134{
135 public:
136 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
137 Reloc_section;
138
139 Target_arm()
94cdfcff
DK
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)
4a657b0d
DK
143 { }
144
145 // Process the relocations to determine unreferenced sections for
146 // garbage collection.
147 void
148 gc_process_relocs(const General_options& options,
149 Symbol_table* symtab,
150 Layout* layout,
151 Sized_relobj<32, big_endian>* object,
152 unsigned int data_shndx,
153 unsigned int sh_type,
154 const unsigned char* prelocs,
155 size_t reloc_count,
156 Output_section* output_section,
157 bool needs_special_offset_handling,
158 size_t local_symbol_count,
159 const unsigned char* plocal_symbols);
160
161 // Scan the relocations to look for symbol adjustments.
162 void
163 scan_relocs(const General_options& options,
164 Symbol_table* symtab,
165 Layout* layout,
166 Sized_relobj<32, big_endian>* object,
167 unsigned int data_shndx,
168 unsigned int sh_type,
169 const unsigned char* prelocs,
170 size_t reloc_count,
171 Output_section* output_section,
172 bool needs_special_offset_handling,
173 size_t local_symbol_count,
174 const unsigned char* plocal_symbols);
175
176 // Finalize the sections.
177 void
178 do_finalize_sections(Layout*);
179
94cdfcff 180 // Return the value to use for a dynamic symbol which requires special
4a657b0d
DK
181 // treatment.
182 uint64_t
183 do_dynsym_value(const Symbol*) const;
184
185 // Relocate a section.
186 void
187 relocate_section(const Relocate_info<32, big_endian>*,
188 unsigned int sh_type,
189 const unsigned char* prelocs,
190 size_t reloc_count,
191 Output_section* output_section,
192 bool needs_special_offset_handling,
193 unsigned char* view,
194 elfcpp::Elf_types<32>::Elf_Addr view_address,
195 section_size_type view_size);
196
197 // Scan the relocs during a relocatable link.
198 void
199 scan_relocatable_relocs(const General_options& options,
200 Symbol_table* symtab,
201 Layout* layout,
202 Sized_relobj<32, big_endian>* object,
203 unsigned int data_shndx,
204 unsigned int sh_type,
205 const unsigned char* prelocs,
206 size_t reloc_count,
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*);
212
213 // Relocate a section during a relocatable link.
214 void
215 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
216 unsigned int sh_type,
217 const unsigned char* prelocs,
218 size_t reloc_count,
219 Output_section* output_section,
220 off_t offset_in_output_section,
221 const Relocatable_relocs*,
222 unsigned char* view,
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);
227
228 // Return whether SYM is defined by the ABI.
229 bool
230 do_is_defined_by_abi(Symbol* sym) const
231 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
232
94cdfcff
DK
233 // Return the size of the GOT section.
234 section_size_type
235 got_size()
236 {
237 gold_assert(this->got_ != NULL);
238 return this->got_->data_size();
239 }
240
4a657b0d
DK
241 // Map platform-specific reloc types
242 static unsigned int
243 get_real_reloc_type (unsigned int r_type);
244
245 private:
246 // The class which scans relocations.
247 class Scan
248 {
249 public:
250 Scan()
bec53400 251 : issued_non_pic_error_(false)
4a657b0d
DK
252 { }
253
254 inline void
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);
262
263 inline void
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,
270 Symbol* gsym);
271
272 private:
273 static void
274 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
275 unsigned int r_type);
276
277 static void
278 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
279 unsigned int r_type, Symbol*);
bec53400
DK
280
281 void
282 check_non_pic(Relobj*, unsigned int r_type);
283
284 // Almost identical to Symbol::needs_plt_entry except that it also
285 // handles STT_ARM_TFUNC.
286 static bool
287 symbol_needs_plt_entry(const Symbol* sym)
288 {
289 // An undefined symbol from an executable does not need a PLT entry.
290 if (sym->is_undefined() && !parameters->options().shared())
291 return false;
292
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()));
299 }
300
301 // Whether we have issued an error about a non-PIC compilation.
302 bool issued_non_pic_error_;
4a657b0d
DK
303 };
304
305 // The class which implements relocation.
306 class Relocate
307 {
308 public:
309 Relocate()
310 { }
311
312 ~Relocate()
313 { }
314
bec53400
DK
315 // Return whether the static relocation needs to be applied.
316 inline bool
317 should_apply_static_reloc(const Sized_symbol<32>* gsym,
318 int ref_flags,
319 bool is_32bit,
320 Output_section* output_section);
321
4a657b0d
DK
322 // Do a relocation. Return false if the caller should not issue
323 // any warnings about this relocation.
324 inline bool
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,
331 section_size_type);
c121c671
DK
332
333 // Return whether we want to pass flag NON_PIC_REF for this
334 // reloc.
335 static inline bool
336 reloc_is_non_pic (unsigned int r_type)
337 {
338 switch (r_type)
339 {
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:
345 return true;
346 default:
347 return false;
348 }
349 }
4a657b0d
DK
350 };
351
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
355 {
356 public:
357 unsigned int
358 get_size_for_reloc(unsigned int, Relobj*);
359 };
360
94cdfcff
DK
361 // Get the GOT section, creating it if necessary.
362 Output_data_got<32, big_endian>*
363 got_section(Symbol_table*, Layout*);
364
365 // Get the GOT PLT section.
366 Output_data_space*
367 got_plt_section() const
368 {
369 gold_assert(this->got_plt_ != NULL);
370 return this->got_plt_;
371 }
372
373 // Create a PLT entry for a global symbol.
374 void
375 make_plt_entry(Symbol_table*, Layout*, Symbol*);
376
377 // Get the PLT section.
378 const Output_data_plt_arm<big_endian>*
379 plt_section() const
380 {
381 gold_assert(this->plt_ != NULL);
382 return this->plt_;
383 }
384
385 // Get the dynamic reloc section, creating it if necessary.
386 Reloc_section*
387 rel_dyn_section(Layout*);
388
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.
392 bool
393 may_need_copy_reloc(Symbol* gsym)
394 {
395 return (!parameters->options().shared()
396 && gsym->is_from_dynobj()
bec53400
DK
397 && gsym->type() != elfcpp::STT_FUNC
398 && gsym->type() != elfcpp::STT_ARM_TFUNC);
94cdfcff
DK
399 }
400
401 // Add a potential copy relocation.
402 void
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)
407 {
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));
412 }
413
4a657b0d
DK
414 // Information about this specific target which we pass to the
415 // general Target structure.
416 static const Target::Target_info arm_info;
94cdfcff
DK
417
418 // The types of GOT entries needed for this platform.
419 enum Got_type
420 {
421 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
422 };
423
424 // The GOT section.
425 Output_data_got<32, big_endian>* got_;
426 // The PLT section.
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_;
4a657b0d
DK
436};
437
438template<bool big_endian>
439const Target::Target_info Target_arm<big_endian>::arm_info =
440{
441 32, // size
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
448 '\0', // wrap_char
449 "/usr/lib/libc.so.1", // dynamic_linker
450 0x8000, // default_text_segment_address
451 0x1000, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
452 0x1000, // common_pagesize (overridable by -z common-page-size)
453 elfcpp::SHN_UNDEF, // small_common_shndx
454 elfcpp::SHN_UNDEF, // large_common_shndx
455 0, // small_common_section_flags
456 0 // large_common_section_flags
4a657b0d
DK
457};
458
c121c671
DK
459// Arm relocate functions class
460//
461
462template<bool big_endian>
463class Arm_relocate_functions : public Relocate_functions<32, big_endian>
464{
465 public:
466 typedef enum
467 {
468 STATUS_OKAY, // No error during relocation.
469 STATUS_OVERFLOW, // Relocation oveflow.
470 STATUS_BAD_RELOC // Relocation cannot be applied.
471 } Status;
472
473 private:
474 typedef Relocate_functions<32, big_endian> Base;
475 typedef Arm_relocate_functions<big_endian> This;
476
477 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
478 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
479 // to distinguish ARM and THUMB functions and it is treated specially.
480 static inline Symbol_value<32>::Value
481 arm_symbol_value (const Sized_relobj<32, big_endian> *object,
482 const Symbol_value<32>* psymval,
483 Symbol_value<32>::Value addend,
484 bool has_thumb_bit)
485 {
486 typedef Symbol_value<32>::Value Valtype;
487
488 if (has_thumb_bit)
489 {
490 Valtype raw = psymval->value(object, 0);
491 Valtype thumb_bit = raw & 1;
492 return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
493 }
494 else
495 return psymval->value(object, addend);
496 }
497
498 // FIXME: This probably only works for Android on ARM v5te. We should
499 // following GNU ld for the general case.
500 template<unsigned r_type>
501 static inline typename This::Status
502 arm_branch_common(unsigned char *view,
503 const Sized_relobj<32, big_endian>* object,
504 const Symbol_value<32>* psymval,
505 elfcpp::Elf_types<32>::Elf_Addr address,
506 bool has_thumb_bit)
507 {
508 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
509 Valtype* wv = reinterpret_cast<Valtype*>(view);
510 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
511
512 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
513 && ((val & 0x0f000000UL) == 0x0a000000UL);
514 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
515 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
516 && ((val & 0x0f000000UL) == 0x0b000000UL);
517 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
518 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
519
520 if (r_type == elfcpp::R_ARM_CALL)
521 {
522 if (!insn_is_uncond_bl && !insn_is_blx)
523 return This::STATUS_BAD_RELOC;
524 }
525 else if (r_type == elfcpp::R_ARM_JUMP24)
526 {
527 if (!insn_is_b && !insn_is_cond_bl)
528 return This::STATUS_BAD_RELOC;
529 }
530 else if (r_type == elfcpp::R_ARM_PLT32)
531 {
532 if (!insn_is_any_branch)
533 return This::STATUS_BAD_RELOC;
534 }
535 else
536 gold_unreachable();
537
538 Valtype addend = utils::sign_extend<26>(val << 2);
539 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
540 - address);
541
542 // If target has thumb bit set, we need to either turn the BL
543 // into a BLX (for ARMv5 or above) or generate a stub.
544 if (x & 1)
545 {
546 // Turn BL to BLX.
547 if (insn_is_uncond_bl)
548 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
549 else
550 return This::STATUS_BAD_RELOC;
551 }
552 else
553 gold_assert(!insn_is_blx);
554
555 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
556 elfcpp::Swap<32, big_endian>::writeval(wv, val);
557 return (utils::has_overflow<26>(x)
558 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
559 }
560
561 public:
562 // R_ARM_ABS32: (S + A) | T
563 static inline typename This::Status
564 abs32(unsigned char *view,
565 const Sized_relobj<32, big_endian>* object,
566 const Symbol_value<32>* psymval,
567 bool has_thumb_bit)
568 {
569 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
570 Valtype* wv = reinterpret_cast<Valtype*>(view);
571 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
572 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
573 elfcpp::Swap<32, big_endian>::writeval(wv, x);
574 return This::STATUS_OKAY;
575 }
576
577 // R_ARM_REL32: (S + A) | T - P
578 static inline typename This::Status
579 rel32(unsigned char *view,
580 const Sized_relobj<32, big_endian>* object,
581 const Symbol_value<32>* psymval,
582 elfcpp::Elf_types<32>::Elf_Addr address,
583 bool has_thumb_bit)
584 {
585 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
586 Valtype* wv = reinterpret_cast<Valtype*>(view);
587 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
588 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
589 - address);
590 elfcpp::Swap<32, big_endian>::writeval(wv, x);
591 return This::STATUS_OKAY;
592 }
593
594 // R_ARM_THM_CALL: (S + A) | T - P
595 static inline typename This::Status
596 thm_call(unsigned char *view,
597 const Sized_relobj<32, big_endian>* object,
598 const Symbol_value<32>* psymval,
599 elfcpp::Elf_types<32>::Elf_Addr address,
600 bool has_thumb_bit)
601 {
602 // A thumb call consists of two instructions.
603 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
604 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
605 Valtype* wv = reinterpret_cast<Valtype*>(view);
606 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
607 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
608 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
609 gold_assert((lo & 0xf800) == 0xf800);
610 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
611 | ((lo & 0x7ff) << 1));
612 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
613 - address);
614
615 // If target has no thumb bit set, we need to either turn the BL
616 // into a BLX (for ARMv5 or above) or generate a stub.
617 if ((x & 1) == 0)
618 {
619 // This only works for ARMv5 and above with interworking enabled.
620 lo &= 0xefff;
621 }
622 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
623 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
624 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
625 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
626 return (utils::has_overflow<23>(x)
627 ? This::STATUS_OVERFLOW
628 : This::STATUS_OKAY);
629 }
630
631 // R_ARM_BASE_PREL: B(S) + A - P
632 static inline typename This::Status
633 base_prel(unsigned char* view,
634 elfcpp::Elf_types<32>::Elf_Addr origin,
635 elfcpp::Elf_types<32>::Elf_Addr address)
636 {
637 Base::rel32(view, origin - address);
638 return STATUS_OKAY;
639 }
640
641 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
642 static inline typename This::Status
643 got_brel(unsigned char* view,
644 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
645 {
646 Base::rel32(view, got_offset);
647 return This::STATUS_OKAY;
648 }
649
650 // R_ARM_PLT32: (S + A) | T - P
651 static inline typename This::Status
652 plt32(unsigned char *view,
653 const Sized_relobj<32, big_endian>* object,
654 const Symbol_value<32>* psymval,
655 elfcpp::Elf_types<32>::Elf_Addr address,
656 bool has_thumb_bit)
657 {
658 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
659 address, has_thumb_bit);
660 }
661
662 // R_ARM_CALL: (S + A) | T - P
663 static inline typename This::Status
664 call(unsigned char *view,
665 const Sized_relobj<32, big_endian>* object,
666 const Symbol_value<32>* psymval,
667 elfcpp::Elf_types<32>::Elf_Addr address,
668 bool has_thumb_bit)
669 {
670 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
671 address, has_thumb_bit);
672 }
673
674 // R_ARM_JUMP24: (S + A) | T - P
675 static inline typename This::Status
676 jump24(unsigned char *view,
677 const Sized_relobj<32, big_endian>* object,
678 const Symbol_value<32>* psymval,
679 elfcpp::Elf_types<32>::Elf_Addr address,
680 bool has_thumb_bit)
681 {
682 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
683 address, has_thumb_bit);
684 }
685
686 // R_ARM_PREL: (S + A) | T - P
687 static inline typename This::Status
688 prel31(unsigned char *view,
689 const Sized_relobj<32, big_endian>* object,
690 const Symbol_value<32>* psymval,
691 elfcpp::Elf_types<32>::Elf_Addr address,
692 bool has_thumb_bit)
693 {
694 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
695 Valtype* wv = reinterpret_cast<Valtype*>(view);
696 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
697 Valtype addend = utils::sign_extend<31>(val);
698 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
699 - address);
700 val = utils::bit_select(val, x, 0x7fffffffU);
701 elfcpp::Swap<32, big_endian>::writeval(wv, val);
702 return (utils::has_overflow<31>(x) ?
703 This::STATUS_OVERFLOW : This::STATUS_OKAY);
704 }
705};
706
94cdfcff
DK
707// Get the GOT section, creating it if necessary.
708
709template<bool big_endian>
710Output_data_got<32, big_endian>*
711Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
712{
713 if (this->got_ == NULL)
714 {
715 gold_assert(symtab != NULL && layout != NULL);
716
717 this->got_ = new Output_data_got<32, big_endian>();
718
719 Output_section* os;
720 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
721 (elfcpp::SHF_ALLOC
722 | elfcpp::SHF_WRITE),
723 this->got_);
724 os->set_is_relro();
725
726 // The old GNU linker creates a .got.plt section. We just
727 // create another set of data in the .got section. Note that we
728 // always create a PLT if we create a GOT, although the PLT
729 // might be empty.
730 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
731 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
732 (elfcpp::SHF_ALLOC
733 | elfcpp::SHF_WRITE),
734 this->got_plt_);
735 os->set_is_relro();
736
737 // The first three entries are reserved.
738 this->got_plt_->set_current_data_size(3 * 4);
739
740 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
741 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
742 this->got_plt_,
743 0, 0, elfcpp::STT_OBJECT,
744 elfcpp::STB_LOCAL,
745 elfcpp::STV_HIDDEN, 0,
746 false, false);
747 }
748 return this->got_;
749}
750
751// Get the dynamic reloc section, creating it if necessary.
752
753template<bool big_endian>
754typename Target_arm<big_endian>::Reloc_section*
755Target_arm<big_endian>::rel_dyn_section(Layout* layout)
756{
757 if (this->rel_dyn_ == NULL)
758 {
759 gold_assert(layout != NULL);
760 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
761 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
762 elfcpp::SHF_ALLOC, this->rel_dyn_);
763 }
764 return this->rel_dyn_;
765}
766
767// A class to handle the PLT data.
768
769template<bool big_endian>
770class Output_data_plt_arm : public Output_section_data
771{
772 public:
773 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
774 Reloc_section;
775
776 Output_data_plt_arm(Layout*, Output_data_space*);
777
778 // Add an entry to the PLT.
779 void
780 add_entry(Symbol* gsym);
781
782 // Return the .rel.plt section data.
783 const Reloc_section*
784 rel_plt() const
785 { return this->rel_; }
786
787 protected:
788 void
789 do_adjust_output_section(Output_section* os);
790
791 // Write to a map file.
792 void
793 do_print_to_mapfile(Mapfile* mapfile) const
794 { mapfile->print_output_data(this, _("** PLT")); }
795
796 private:
797 // Template for the first PLT entry.
798 static const uint32_t first_plt_entry[5];
799
800 // Template for subsequent PLT entries.
801 static const uint32_t plt_entry[3];
802
803 // Set the final size.
804 void
805 set_final_data_size()
806 {
807 this->set_data_size(sizeof(first_plt_entry)
808 + this->count_ * sizeof(plt_entry));
809 }
810
811 // Write out the PLT data.
812 void
813 do_write(Output_file*);
814
815 // The reloc section.
816 Reloc_section* rel_;
817 // The .got.plt section.
818 Output_data_space* got_plt_;
819 // The number of PLT entries.
820 unsigned int count_;
821};
822
823// Create the PLT section. The ordinary .got section is an argument,
824// since we need to refer to the start. We also create our own .got
825// section just for PLT entries.
826
827template<bool big_endian>
828Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
829 Output_data_space* got_plt)
830 : Output_section_data(4), got_plt_(got_plt), count_(0)
831{
832 this->rel_ = new Reloc_section(false);
833 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
834 elfcpp::SHF_ALLOC, this->rel_);
835}
836
837template<bool big_endian>
838void
839Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
840{
841 os->set_entsize(0);
842}
843
844// Add an entry to the PLT.
845
846template<bool big_endian>
847void
848Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
849{
850 gold_assert(!gsym->has_plt_offset());
851
852 // Note that when setting the PLT offset we skip the initial
853 // reserved PLT entry.
854 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
855 + sizeof(first_plt_entry));
856
857 ++this->count_;
858
859 section_offset_type got_offset = this->got_plt_->current_data_size();
860
861 // Every PLT entry needs a GOT entry which points back to the PLT
862 // entry (this will be changed by the dynamic linker, normally
863 // lazily when the function is called).
864 this->got_plt_->set_current_data_size(got_offset + 4);
865
866 // Every PLT entry needs a reloc.
867 gsym->set_needs_dynsym_entry();
868 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
869 got_offset);
870
871 // Note that we don't need to save the symbol. The contents of the
872 // PLT are independent of which symbols are used. The symbols only
873 // appear in the relocations.
874}
875
876// ARM PLTs.
877// FIXME: This is not very flexible. Right now this has only been tested
878// on armv5te. If we are to support additional architecture features like
879// Thumb-2 or BE8, we need to make this more flexible like GNU ld.
880
881// The first entry in the PLT.
882template<bool big_endian>
883const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
884{
885 0xe52de004, // str lr, [sp, #-4]!
886 0xe59fe004, // ldr lr, [pc, #4]
887 0xe08fe00e, // add lr, pc, lr
888 0xe5bef008, // ldr pc, [lr, #8]!
889 0x00000000, // &GOT[0] - .
890};
891
892// Subsequent entries in the PLT.
893
894template<bool big_endian>
895const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
896{
897 0xe28fc600, // add ip, pc, #0xNN00000
898 0xe28cca00, // add ip, ip, #0xNN000
899 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
900};
901
902// Write out the PLT. This uses the hand-coded instructions above,
903// and adjusts them as needed. This is all specified by the arm ELF
904// Processor Supplement.
905
906template<bool big_endian>
907void
908Output_data_plt_arm<big_endian>::do_write(Output_file* of)
909{
910 const off_t offset = this->offset();
911 const section_size_type oview_size =
912 convert_to_section_size_type(this->data_size());
913 unsigned char* const oview = of->get_output_view(offset, oview_size);
914
915 const off_t got_file_offset = this->got_plt_->offset();
916 const section_size_type got_size =
917 convert_to_section_size_type(this->got_plt_->data_size());
918 unsigned char* const got_view = of->get_output_view(got_file_offset,
919 got_size);
920 unsigned char* pov = oview;
921
922 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
923 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
924
925 // Write first PLT entry. All but the last word are constants.
926 const size_t num_first_plt_words = (sizeof(first_plt_entry)
927 / sizeof(plt_entry[0]));
928 for (size_t i = 0; i < num_first_plt_words - 1; i++)
929 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
930 // Last word in first PLT entry is &GOT[0] - .
931 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
932 got_address - (plt_address + 16));
933 pov += sizeof(first_plt_entry);
934
935 unsigned char* got_pov = got_view;
936
937 memset(got_pov, 0, 12);
938 got_pov += 12;
939
940 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
941 unsigned int plt_offset = sizeof(first_plt_entry);
942 unsigned int plt_rel_offset = 0;
943 unsigned int got_offset = 12;
944 const unsigned int count = this->count_;
945 for (unsigned int i = 0;
946 i < count;
947 ++i,
948 pov += sizeof(plt_entry),
949 got_pov += 4,
950 plt_offset += sizeof(plt_entry),
951 plt_rel_offset += rel_size,
952 got_offset += 4)
953 {
954 // Set and adjust the PLT entry itself.
955 int32_t offset = ((got_address + got_offset)
956 - (plt_address + plt_offset + 8));
957
958 gold_assert(offset >= 0 && offset < 0x0fffffff);
959 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
960 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
961 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
962 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
963 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
964 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
965
966 // Set the entry in the GOT.
967 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
968 }
969
970 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
971 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
972
973 of->write_output_view(offset, oview_size, oview);
974 of->write_output_view(got_file_offset, got_size, got_view);
975}
976
977// Create a PLT entry for a global symbol.
978
979template<bool big_endian>
980void
981Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
982 Symbol* gsym)
983{
984 if (gsym->has_plt_offset())
985 return;
986
987 if (this->plt_ == NULL)
988 {
989 // Create the GOT sections first.
990 this->got_section(symtab, layout);
991
992 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
993 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
994 (elfcpp::SHF_ALLOC
995 | elfcpp::SHF_EXECINSTR),
996 this->plt_);
997 }
998 this->plt_->add_entry(gsym);
999}
1000
4a657b0d
DK
1001// Report an unsupported relocation against a local symbol.
1002
1003template<bool big_endian>
1004void
1005Target_arm<big_endian>::Scan::unsupported_reloc_local(
1006 Sized_relobj<32, big_endian>* object,
1007 unsigned int r_type)
1008{
1009 gold_error(_("%s: unsupported reloc %u against local symbol"),
1010 object->name().c_str(), r_type);
1011}
1012
bec53400
DK
1013// We are about to emit a dynamic relocation of type R_TYPE. If the
1014// dynamic linker does not support it, issue an error. The GNU linker
1015// only issues a non-PIC error for an allocated read-only section.
1016// Here we know the section is allocated, but we don't know that it is
1017// read-only. But we check for all the relocation types which the
1018// glibc dynamic linker supports, so it seems appropriate to issue an
1019// error even if the section is not read-only.
1020
1021template<bool big_endian>
1022void
1023Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
1024 unsigned int r_type)
1025{
1026 switch (r_type)
1027 {
1028 // These are the relocation types supported by glibc for ARM.
1029 case elfcpp::R_ARM_RELATIVE:
1030 case elfcpp::R_ARM_COPY:
1031 case elfcpp::R_ARM_GLOB_DAT:
1032 case elfcpp::R_ARM_JUMP_SLOT:
1033 case elfcpp::R_ARM_ABS32:
1034 case elfcpp::R_ARM_PC24:
1035 // FIXME: The following 3 types are not supported by Android's dynamic
1036 // linker.
1037 case elfcpp::R_ARM_TLS_DTPMOD32:
1038 case elfcpp::R_ARM_TLS_DTPOFF32:
1039 case elfcpp::R_ARM_TLS_TPOFF32:
1040 return;
1041
1042 default:
1043 // This prevents us from issuing more than one error per reloc
1044 // section. But we can still wind up issuing more than one
1045 // error per object file.
1046 if (this->issued_non_pic_error_)
1047 return;
1048 object->error(_("requires unsupported dynamic reloc; "
1049 "recompile with -fPIC"));
1050 this->issued_non_pic_error_ = true;
1051 return;
1052
1053 case elfcpp::R_ARM_NONE:
1054 gold_unreachable();
1055 }
1056}
1057
4a657b0d 1058// Scan a relocation for a local symbol.
bec53400
DK
1059// FIXME: This only handles a subset of relocation types used by Android
1060// on ARM v5te devices.
4a657b0d
DK
1061
1062template<bool big_endian>
1063inline void
1064Target_arm<big_endian>::Scan::local(const General_options&,
bec53400
DK
1065 Symbol_table* symtab,
1066 Layout* layout,
1067 Target_arm* target,
4a657b0d 1068 Sized_relobj<32, big_endian>* object,
bec53400
DK
1069 unsigned int data_shndx,
1070 Output_section* output_section,
1071 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
1072 unsigned int r_type,
1073 const elfcpp::Sym<32, big_endian>&)
1074{
1075 r_type = get_real_reloc_type(r_type);
1076 switch (r_type)
1077 {
1078 case elfcpp::R_ARM_NONE:
1079 break;
1080
bec53400
DK
1081 case elfcpp::R_ARM_ABS32:
1082 // If building a shared library (or a position-independent
1083 // executable), we need to create a dynamic relocation for
1084 // this location. The relocation applied at link time will
1085 // apply the link-time value, so we flag the location with
1086 // an R_ARM_RELATIVE relocation so the dynamic loader can
1087 // relocate it easily.
1088 if (parameters->options().output_is_position_independent())
1089 {
1090 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1091 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1092 // If we are to add more other reloc types than R_ARM_ABS32,
1093 // we need to add check_non_pic(object, r_type) here.
1094 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
1095 output_section, data_shndx,
1096 reloc.get_r_offset());
1097 }
1098 break;
1099
1100 case elfcpp::R_ARM_REL32:
1101 case elfcpp::R_ARM_THM_CALL:
1102 case elfcpp::R_ARM_CALL:
1103 case elfcpp::R_ARM_PREL31:
1104 case elfcpp::R_ARM_JUMP24:
1105 case elfcpp::R_ARM_PLT32:
1106 break;
1107
1108 case elfcpp::R_ARM_GOTOFF32:
1109 // We need a GOT section:
1110 target->got_section(symtab, layout);
1111 break;
1112
1113 case elfcpp::R_ARM_BASE_PREL:
1114 // FIXME: What about this?
1115 break;
1116
1117 case elfcpp::R_ARM_GOT_BREL:
1118 {
1119 // The symbol requires a GOT entry.
1120 Output_data_got<32, big_endian>* got =
1121 target->got_section(symtab, layout);
1122 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1123 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1124 {
1125 // If we are generating a shared object, we need to add a
1126 // dynamic RELATIVE relocation for this symbol's GOT entry.
1127 if (parameters->options().output_is_position_independent())
1128 {
1129 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1130 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1131 rel_dyn->add_local_relative(
1132 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
1133 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
1134 }
1135 }
1136 }
1137 break;
1138
1139 case elfcpp::R_ARM_TARGET1:
1140 // This should have been mapped to another type already.
1141 // Fall through.
1142 case elfcpp::R_ARM_COPY:
1143 case elfcpp::R_ARM_GLOB_DAT:
1144 case elfcpp::R_ARM_JUMP_SLOT:
1145 case elfcpp::R_ARM_RELATIVE:
1146 // These are relocations which should only be seen by the
1147 // dynamic linker, and should never be seen here.
1148 gold_error(_("%s: unexpected reloc %u in object file"),
1149 object->name().c_str(), r_type);
1150 break;
1151
4a657b0d
DK
1152 default:
1153 unsupported_reloc_local(object, r_type);
1154 break;
1155 }
1156}
1157
1158// Report an unsupported relocation against a global symbol.
1159
1160template<bool big_endian>
1161void
1162Target_arm<big_endian>::Scan::unsupported_reloc_global(
1163 Sized_relobj<32, big_endian>* object,
1164 unsigned int r_type,
1165 Symbol* gsym)
1166{
1167 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1168 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1169}
1170
1171// Scan a relocation for a global symbol.
bec53400
DK
1172// FIXME: This only handles a subset of relocation types used by Android
1173// on ARM v5te devices.
4a657b0d
DK
1174
1175template<bool big_endian>
1176inline void
1177Target_arm<big_endian>::Scan::global(const General_options&,
bec53400
DK
1178 Symbol_table* symtab,
1179 Layout* layout,
1180 Target_arm* target,
4a657b0d 1181 Sized_relobj<32, big_endian>* object,
bec53400
DK
1182 unsigned int data_shndx,
1183 Output_section* output_section,
1184 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
1185 unsigned int r_type,
1186 Symbol* gsym)
1187{
1188 r_type = get_real_reloc_type(r_type);
1189 switch (r_type)
1190 {
1191 case elfcpp::R_ARM_NONE:
1192 break;
1193
bec53400
DK
1194 case elfcpp::R_ARM_ABS32:
1195 {
1196 // Make a dynamic relocation if necessary.
1197 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1198 {
1199 if (target->may_need_copy_reloc(gsym))
1200 {
1201 target->copy_reloc(symtab, layout, object,
1202 data_shndx, output_section, gsym, reloc);
1203 }
1204 else if (gsym->can_use_relative_reloc(false))
1205 {
1206 // If we are to add more other reloc types than R_ARM_ABS32,
1207 // we need to add check_non_pic(object, r_type) here.
1208 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1209 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
1210 output_section, object,
1211 data_shndx, reloc.get_r_offset());
1212 }
1213 else
1214 {
1215 // If we are to add more other reloc types than R_ARM_ABS32,
1216 // we need to add check_non_pic(object, r_type) here.
1217 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1218 rel_dyn->add_global(gsym, r_type, output_section, object,
1219 data_shndx, reloc.get_r_offset());
1220 }
1221 }
1222 }
1223 break;
1224
1225 case elfcpp::R_ARM_REL32:
1226 case elfcpp::R_ARM_PREL31:
1227 {
1228 // Make a dynamic relocation if necessary.
1229 int flags = Symbol::NON_PIC_REF;
1230 if (gsym->needs_dynamic_reloc(flags))
1231 {
1232 if (target->may_need_copy_reloc(gsym))
1233 {
1234 target->copy_reloc(symtab, layout, object,
1235 data_shndx, output_section, gsym, reloc);
1236 }
1237 else
1238 {
1239 check_non_pic(object, r_type);
1240 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1241 rel_dyn->add_global(gsym, r_type, output_section, object,
1242 data_shndx, reloc.get_r_offset());
1243 }
1244 }
1245 }
1246 break;
1247
1248 case elfcpp::R_ARM_JUMP24:
1249 case elfcpp::R_ARM_THM_CALL:
1250 case elfcpp::R_ARM_CALL:
1251 {
1252 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
1253 target->make_plt_entry(symtab, layout, gsym);
1254 // Make a dynamic relocation if necessary.
1255 int flags = Symbol::NON_PIC_REF;
1256 if (gsym->type() == elfcpp::STT_FUNC
07800fab 1257 || gsym->type() == elfcpp::STT_ARM_TFUNC)
bec53400
DK
1258 flags |= Symbol::FUNCTION_CALL;
1259 if (gsym->needs_dynamic_reloc(flags))
1260 {
1261 if (target->may_need_copy_reloc(gsym))
1262 {
1263 target->copy_reloc(symtab, layout, object,
1264 data_shndx, output_section, gsym,
1265 reloc);
1266 }
1267 else
1268 {
1269 check_non_pic(object, r_type);
1270 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1271 rel_dyn->add_global(gsym, r_type, output_section, object,
1272 data_shndx, reloc.get_r_offset());
1273 }
1274 }
1275 }
1276 break;
1277
1278 case elfcpp::R_ARM_PLT32:
1279 // If the symbol is fully resolved, this is just a relative
1280 // local reloc. Otherwise we need a PLT entry.
1281 if (gsym->final_value_is_known())
1282 break;
1283 // If building a shared library, we can also skip the PLT entry
1284 // if the symbol is defined in the output file and is protected
1285 // or hidden.
1286 if (gsym->is_defined()
1287 && !gsym->is_from_dynobj()
1288 && !gsym->is_preemptible())
1289 break;
1290 target->make_plt_entry(symtab, layout, gsym);
1291 break;
1292
1293 case elfcpp::R_ARM_GOTOFF32:
1294 // We need a GOT section.
1295 target->got_section(symtab, layout);
1296 break;
1297
1298 case elfcpp::R_ARM_BASE_PREL:
1299 // FIXME: What about this?
1300 break;
1301
1302 case elfcpp::R_ARM_GOT_BREL:
1303 {
1304 // The symbol requires a GOT entry.
1305 Output_data_got<32, big_endian>* got =
1306 target->got_section(symtab, layout);
1307 if (gsym->final_value_is_known())
1308 got->add_global(gsym, GOT_TYPE_STANDARD);
1309 else
1310 {
1311 // If this symbol is not fully resolved, we need to add a
1312 // GOT entry with a dynamic relocation.
1313 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1314 if (gsym->is_from_dynobj()
1315 || gsym->is_undefined()
1316 || gsym->is_preemptible())
1317 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1318 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
1319 else
1320 {
1321 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1322 rel_dyn->add_global_relative(
1323 gsym, elfcpp::R_ARM_RELATIVE, got,
1324 gsym->got_offset(GOT_TYPE_STANDARD));
1325 }
1326 }
1327 }
1328 break;
1329
1330 case elfcpp::R_ARM_TARGET1:
1331 // This should have been mapped to another type already.
1332 // Fall through.
1333 case elfcpp::R_ARM_COPY:
1334 case elfcpp::R_ARM_GLOB_DAT:
1335 case elfcpp::R_ARM_JUMP_SLOT:
1336 case elfcpp::R_ARM_RELATIVE:
1337 // These are relocations which should only be seen by the
1338 // dynamic linker, and should never be seen here.
1339 gold_error(_("%s: unexpected reloc %u in object file"),
1340 object->name().c_str(), r_type);
1341 break;
1342
4a657b0d
DK
1343 default:
1344 unsupported_reloc_global(object, r_type, gsym);
1345 break;
1346 }
1347}
1348
1349// Process relocations for gc.
1350
1351template<bool big_endian>
1352void
1353Target_arm<big_endian>::gc_process_relocs(const General_options& options,
1354 Symbol_table* symtab,
1355 Layout* layout,
1356 Sized_relobj<32, big_endian>* object,
1357 unsigned int data_shndx,
1358 unsigned int,
1359 const unsigned char* prelocs,
1360 size_t reloc_count,
1361 Output_section* output_section,
1362 bool needs_special_offset_handling,
1363 size_t local_symbol_count,
1364 const unsigned char* plocal_symbols)
1365{
1366 typedef Target_arm<big_endian> Arm;
1367 typedef typename Target_arm<big_endian>::Scan Scan;
1368
1369 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
1370 options,
1371 symtab,
1372 layout,
1373 this,
1374 object,
1375 data_shndx,
1376 prelocs,
1377 reloc_count,
1378 output_section,
1379 needs_special_offset_handling,
1380 local_symbol_count,
1381 plocal_symbols);
1382}
1383
1384// Scan relocations for a section.
1385
1386template<bool big_endian>
1387void
1388Target_arm<big_endian>::scan_relocs(const General_options& options,
1389 Symbol_table* symtab,
1390 Layout* layout,
1391 Sized_relobj<32, big_endian>* object,
1392 unsigned int data_shndx,
1393 unsigned int sh_type,
1394 const unsigned char* prelocs,
1395 size_t reloc_count,
1396 Output_section* output_section,
1397 bool needs_special_offset_handling,
1398 size_t local_symbol_count,
1399 const unsigned char* plocal_symbols)
1400{
1401 typedef typename Target_arm<big_endian>::Scan Scan;
1402 if (sh_type == elfcpp::SHT_RELA)
1403 {
1404 gold_error(_("%s: unsupported RELA reloc section"),
1405 object->name().c_str());
1406 return;
1407 }
1408
1409 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
1410 options,
1411 symtab,
1412 layout,
1413 this,
1414 object,
1415 data_shndx,
1416 prelocs,
1417 reloc_count,
1418 output_section,
1419 needs_special_offset_handling,
1420 local_symbol_count,
1421 plocal_symbols);
1422}
1423
1424// Finalize the sections.
1425
1426template<bool big_endian>
1427void
94cdfcff 1428Target_arm<big_endian>::do_finalize_sections(Layout* layout)
4a657b0d 1429{
94cdfcff
DK
1430 // Fill in some more dynamic tags.
1431 Output_data_dynamic* const odyn = layout->dynamic_data();
1432 if (odyn != NULL)
1433 {
1434 if (this->got_plt_ != NULL)
1435 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1436
1437 if (this->plt_ != NULL)
1438 {
1439 const Output_data* od = this->plt_->rel_plt();
1440 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1441 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1442 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1443 }
1444
1445 if (this->rel_dyn_ != NULL)
1446 {
1447 const Output_data* od = this->rel_dyn_;
1448 odyn->add_section_address(elfcpp::DT_REL, od);
1449 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1450 odyn->add_constant(elfcpp::DT_RELENT,
1451 elfcpp::Elf_sizes<32>::rel_size);
1452 }
1453
1454 if (!parameters->options().shared())
1455 {
1456 // The value of the DT_DEBUG tag is filled in by the dynamic
1457 // linker at run time, and used by the debugger.
1458 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1459 }
1460 }
1461
1462 // Emit any relocs we saved in an attempt to avoid generating COPY
1463 // relocs.
1464 if (this->copy_relocs_.any_saved_relocs())
1465 this->copy_relocs_.emit(this->rel_dyn_section(layout));
4a657b0d
DK
1466}
1467
bec53400
DK
1468// Return whether a direct absolute static relocation needs to be applied.
1469// In cases where Scan::local() or Scan::global() has created
1470// a dynamic relocation other than R_ARM_RELATIVE, the addend
1471// of the relocation is carried in the data, and we must not
1472// apply the static relocation.
1473
1474template<bool big_endian>
1475inline bool
1476Target_arm<big_endian>::Relocate::should_apply_static_reloc(
1477 const Sized_symbol<32>* gsym,
1478 int ref_flags,
1479 bool is_32bit,
1480 Output_section* output_section)
1481{
1482 // If the output section is not allocated, then we didn't call
1483 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1484 // the reloc here.
1485 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1486 return true;
1487
1488 // For local symbols, we will have created a non-RELATIVE dynamic
1489 // relocation only if (a) the output is position independent,
1490 // (b) the relocation is absolute (not pc- or segment-relative), and
1491 // (c) the relocation is not 32 bits wide.
1492 if (gsym == NULL)
1493 return !(parameters->options().output_is_position_independent()
1494 && (ref_flags & Symbol::ABSOLUTE_REF)
1495 && !is_32bit);
1496
1497 // For global symbols, we use the same helper routines used in the
1498 // scan pass. If we did not create a dynamic relocation, or if we
1499 // created a RELATIVE dynamic relocation, we should apply the static
1500 // relocation.
1501 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1502 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1503 && gsym->can_use_relative_reloc(ref_flags
1504 & Symbol::FUNCTION_CALL);
1505 return !has_dyn || is_rel;
1506}
1507
4a657b0d
DK
1508// Perform a relocation.
1509
1510template<bool big_endian>
1511inline bool
1512Target_arm<big_endian>::Relocate::relocate(
c121c671
DK
1513 const Relocate_info<32, big_endian>* relinfo,
1514 Target_arm* target,
1515 Output_section *output_section,
1516 size_t relnum,
1517 const elfcpp::Rel<32, big_endian>& rel,
4a657b0d 1518 unsigned int r_type,
c121c671
DK
1519 const Sized_symbol<32>* gsym,
1520 const Symbol_value<32>* psymval,
1521 unsigned char* view,
1522 elfcpp::Elf_types<32>::Elf_Addr address,
4a657b0d
DK
1523 section_size_type /* view_size */ )
1524{
c121c671
DK
1525 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
1526
1527 r_type = get_real_reloc_type(r_type);
1528
1529 // If this the symbol may be a Thumb function, set thumb bit to 1.
1530 bool has_thumb_bit = ((gsym != NULL)
1531 && (gsym->type() == elfcpp::STT_FUNC
1532 || gsym->type() == elfcpp::STT_ARM_TFUNC));
1533
1534 // Pick the value to use for symbols defined in shared objects.
1535 Symbol_value<32> symval;
1536 if (gsym != NULL
1537 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
1538 {
1539 symval.set_output_value(target->plt_section()->address()
1540 + gsym->plt_offset());
1541 psymval = &symval;
1542 has_thumb_bit = 0;
1543 }
1544
1545 const Sized_relobj<32, big_endian>* object = relinfo->object;
1546
1547 // Get the GOT offset if needed.
1548 // The GOT pointer points to the end of the GOT section.
1549 // We need to subtract the size of the GOT section to get
1550 // the actual offset to use in the relocation.
1551 bool have_got_offset = false;
1552 unsigned int got_offset = 0;
1553 switch (r_type)
1554 {
1555 case elfcpp::R_ARM_GOT_BREL:
1556 if (gsym != NULL)
1557 {
1558 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1559 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1560 - target->got_size());
1561 }
1562 else
1563 {
1564 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1565 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1566 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1567 - target->got_size());
1568 }
1569 have_got_offset = true;
1570 break;
1571
1572 default:
1573 break;
1574 }
1575
1576 typename Arm_relocate_functions::Status reloc_status =
1577 Arm_relocate_functions::STATUS_OKAY;
4a657b0d
DK
1578 switch (r_type)
1579 {
1580 case elfcpp::R_ARM_NONE:
1581 break;
1582
c121c671
DK
1583 case elfcpp::R_ARM_ABS32:
1584 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
1585 output_section))
1586 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
1587 has_thumb_bit);
1588 break;
1589
1590 case elfcpp::R_ARM_REL32:
1591 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
1592 address, has_thumb_bit);
1593 break;
1594
1595 case elfcpp::R_ARM_THM_CALL:
1596 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
1597 address, has_thumb_bit);
1598 break;
1599
1600 case elfcpp::R_ARM_GOTOFF32:
1601 {
1602 elfcpp::Elf_types<32>::Elf_Addr got_origin;
1603 got_origin = target->got_plt_section()->address();
1604 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
1605 got_origin, has_thumb_bit);
1606 }
1607 break;
1608
1609 case elfcpp::R_ARM_BASE_PREL:
1610 {
1611 uint32_t origin;
1612 // Get the addressing origin of the output segment defining the
1613 // symbol gsym (AAELF 4.6.1.2 Relocation types)
1614 gold_assert(gsym != NULL);
1615 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
1616 origin = gsym->output_segment()->vaddr();
1617 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
1618 origin = gsym->output_data()->address();
1619 else
1620 {
1621 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1622 _("cannot find origin of R_ARM_BASE_PREL"));
1623 return true;
1624 }
1625 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
1626 }
1627 break;
1628
1629 case elfcpp::R_ARM_GOT_BREL:
1630 gold_assert(have_got_offset);
1631 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
1632 break;
1633
1634 case elfcpp::R_ARM_PLT32:
1635 gold_assert(gsym == NULL
1636 || gsym->has_plt_offset()
1637 || gsym->final_value_is_known()
1638 || (gsym->is_defined()
1639 && !gsym->is_from_dynobj()
1640 && !gsym->is_preemptible()));
1641 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
1642 address, has_thumb_bit);
1643 break;
1644
1645 case elfcpp::R_ARM_CALL:
1646 reloc_status = Arm_relocate_functions::call(view, object, psymval,
1647 address, has_thumb_bit);
1648 break;
1649
1650 case elfcpp::R_ARM_JUMP24:
1651 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
1652 address, has_thumb_bit);
1653 break;
1654
1655 case elfcpp::R_ARM_PREL31:
1656 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
1657 address, has_thumb_bit);
1658 break;
1659
1660 case elfcpp::R_ARM_TARGET1:
1661 // This should have been mapped to another type already.
1662 // Fall through.
1663 case elfcpp::R_ARM_COPY:
1664 case elfcpp::R_ARM_GLOB_DAT:
1665 case elfcpp::R_ARM_JUMP_SLOT:
1666 case elfcpp::R_ARM_RELATIVE:
1667 // These are relocations which should only be seen by the
1668 // dynamic linker, and should never be seen here.
1669 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1670 _("unexpected reloc %u in object file"),
1671 r_type);
1672 break;
1673
1674 default:
1675 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1676 _("unsupported reloc %u"),
1677 r_type);
1678 break;
1679 }
1680
1681 // Report any errors.
1682 switch (reloc_status)
1683 {
1684 case Arm_relocate_functions::STATUS_OKAY:
1685 break;
1686 case Arm_relocate_functions::STATUS_OVERFLOW:
1687 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1688 _("relocation overflow in relocation %u"),
1689 r_type);
1690 break;
1691 case Arm_relocate_functions::STATUS_BAD_RELOC:
1692 gold_error_at_location(
1693 relinfo,
1694 relnum,
1695 rel.get_r_offset(),
1696 _("unexpected opcode while processing relocation %u"),
1697 r_type);
1698 break;
4a657b0d
DK
1699 default:
1700 gold_unreachable();
1701 }
1702
1703 return true;
1704}
1705
1706// Relocate section data.
1707
1708template<bool big_endian>
1709void
1710Target_arm<big_endian>::relocate_section(
1711 const Relocate_info<32, big_endian>* relinfo,
1712 unsigned int sh_type,
1713 const unsigned char* prelocs,
1714 size_t reloc_count,
1715 Output_section* output_section,
1716 bool needs_special_offset_handling,
1717 unsigned char* view,
1718 elfcpp::Elf_types<32>::Elf_Addr address,
1719 section_size_type view_size)
1720{
1721 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
1722 gold_assert(sh_type == elfcpp::SHT_REL);
1723
1724 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
1725 Arm_relocate>(
1726 relinfo,
1727 this,
1728 prelocs,
1729 reloc_count,
1730 output_section,
1731 needs_special_offset_handling,
1732 view,
1733 address,
1734 view_size);
1735}
1736
1737// Return the size of a relocation while scanning during a relocatable
1738// link.
1739
1740template<bool big_endian>
1741unsigned int
1742Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1743 unsigned int r_type,
1744 Relobj* object)
1745{
1746 r_type = get_real_reloc_type(r_type);
1747 switch (r_type)
1748 {
1749 case elfcpp::R_ARM_NONE:
1750 return 0;
1751
1752 case elfcpp::R_ARM_ABS32:
1753 case elfcpp::R_ARM_REL32:
1754 case elfcpp::R_ARM_THM_CALL:
1755 case elfcpp::R_ARM_GOTOFF32:
1756 case elfcpp::R_ARM_BASE_PREL:
1757 case elfcpp::R_ARM_GOT_BREL:
1758 case elfcpp::R_ARM_PLT32:
1759 case elfcpp::R_ARM_CALL:
1760 case elfcpp::R_ARM_JUMP24:
1761 case elfcpp::R_ARM_PREL31:
1762 return 4;
1763
1764 case elfcpp::R_ARM_TARGET1:
1765 // This should have been mapped to another type already.
1766 // Fall through.
1767 case elfcpp::R_ARM_COPY:
1768 case elfcpp::R_ARM_GLOB_DAT:
1769 case elfcpp::R_ARM_JUMP_SLOT:
1770 case elfcpp::R_ARM_RELATIVE:
1771 // These are relocations which should only be seen by the
1772 // dynamic linker, and should never be seen here.
1773 gold_error(_("%s: unexpected reloc %u in object file"),
1774 object->name().c_str(), r_type);
1775 return 0;
1776
1777 default:
1778 object->error(_("unsupported reloc %u in object file"), r_type);
1779 return 0;
1780 }
1781}
1782
1783// Scan the relocs during a relocatable link.
1784
1785template<bool big_endian>
1786void
1787Target_arm<big_endian>::scan_relocatable_relocs(
1788 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,
1800 Relocatable_relocs* rr)
1801{
1802 gold_assert(sh_type == elfcpp::SHT_REL);
1803
1804 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
1805 Relocatable_size_for_reloc> Scan_relocatable_relocs;
1806
1807 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
1808 Scan_relocatable_relocs>(
1809 options,
1810 symtab,
1811 layout,
1812 object,
1813 data_shndx,
1814 prelocs,
1815 reloc_count,
1816 output_section,
1817 needs_special_offset_handling,
1818 local_symbol_count,
1819 plocal_symbols,
1820 rr);
1821}
1822
1823// Relocate a section during a relocatable link.
1824
1825template<bool big_endian>
1826void
1827Target_arm<big_endian>::relocate_for_relocatable(
1828 const Relocate_info<32, big_endian>* relinfo,
1829 unsigned int sh_type,
1830 const unsigned char* prelocs,
1831 size_t reloc_count,
1832 Output_section* output_section,
1833 off_t offset_in_output_section,
1834 const Relocatable_relocs* rr,
1835 unsigned char* view,
1836 elfcpp::Elf_types<32>::Elf_Addr view_address,
1837 section_size_type view_size,
1838 unsigned char* reloc_view,
1839 section_size_type reloc_view_size)
1840{
1841 gold_assert(sh_type == elfcpp::SHT_REL);
1842
1843 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
1844 relinfo,
1845 prelocs,
1846 reloc_count,
1847 output_section,
1848 offset_in_output_section,
1849 rr,
1850 view,
1851 view_address,
1852 view_size,
1853 reloc_view,
1854 reloc_view_size);
1855}
1856
94cdfcff
DK
1857// Return the value to use for a dynamic symbol which requires special
1858// treatment. This is how we support equality comparisons of function
1859// pointers across shared library boundaries, as described in the
1860// processor specific ABI supplement.
1861
4a657b0d
DK
1862template<bool big_endian>
1863uint64_t
94cdfcff 1864Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4a657b0d 1865{
94cdfcff
DK
1866 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1867 return this->plt_section()->address() + gsym->plt_offset();
4a657b0d
DK
1868}
1869
1870// Map platform-specific relocs to real relocs
1871//
1872template<bool big_endian>
1873unsigned int
1874Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
1875{
1876 switch (r_type)
1877 {
1878 case elfcpp::R_ARM_TARGET1:
1879 // This is either R_ARM_ABS32 or R_ARM_REL32;
1880 return elfcpp::R_ARM_ABS32;
1881
1882 case elfcpp::R_ARM_TARGET2:
1883 // This can be any reloc type but ususally is R_ARM_GOT_PREL
1884 return elfcpp::R_ARM_GOT_PREL;
1885
1886 default:
1887 return r_type;
1888 }
1889}
1890
1891// The selector for arm object files.
1892
1893template<bool big_endian>
1894class Target_selector_arm : public Target_selector
1895{
1896 public:
1897 Target_selector_arm()
1898 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
1899 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
1900 { }
1901
1902 Target*
1903 do_instantiate_target()
1904 { return new Target_arm<big_endian>(); }
1905};
1906
1907Target_selector_arm<false> target_selector_arm;
1908Target_selector_arm<true> target_selector_armbe;
1909
1910} // End anonymous namespace.
This page took 0.104316 seconds and 4 git commands to generate.