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