]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/arm.cc
cpu/
[thirdparty/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)
452 0x1000 // common_pagesize (overridable by -z common-page-size)
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));
4a657b0d
DK
1462}
1463
bec53400
DK
1464// Return whether a direct absolute static relocation needs to be applied.
1465// In cases where Scan::local() or Scan::global() has created
1466// a dynamic relocation other than R_ARM_RELATIVE, the addend
1467// of the relocation is carried in the data, and we must not
1468// apply the static relocation.
1469
1470template<bool big_endian>
1471inline bool
1472Target_arm<big_endian>::Relocate::should_apply_static_reloc(
1473 const Sized_symbol<32>* gsym,
1474 int ref_flags,
1475 bool is_32bit,
1476 Output_section* output_section)
1477{
1478 // If the output section is not allocated, then we didn't call
1479 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1480 // the reloc here.
1481 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1482 return true;
1483
1484 // For local symbols, we will have created a non-RELATIVE dynamic
1485 // relocation only if (a) the output is position independent,
1486 // (b) the relocation is absolute (not pc- or segment-relative), and
1487 // (c) the relocation is not 32 bits wide.
1488 if (gsym == NULL)
1489 return !(parameters->options().output_is_position_independent()
1490 && (ref_flags & Symbol::ABSOLUTE_REF)
1491 && !is_32bit);
1492
1493 // For global symbols, we use the same helper routines used in the
1494 // scan pass. If we did not create a dynamic relocation, or if we
1495 // created a RELATIVE dynamic relocation, we should apply the static
1496 // relocation.
1497 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1498 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1499 && gsym->can_use_relative_reloc(ref_flags
1500 & Symbol::FUNCTION_CALL);
1501 return !has_dyn || is_rel;
1502}
1503
4a657b0d
DK
1504// Perform a relocation.
1505
1506template<bool big_endian>
1507inline bool
1508Target_arm<big_endian>::Relocate::relocate(
c121c671
DK
1509 const Relocate_info<32, big_endian>* relinfo,
1510 Target_arm* target,
1511 Output_section *output_section,
1512 size_t relnum,
1513 const elfcpp::Rel<32, big_endian>& rel,
4a657b0d 1514 unsigned int r_type,
c121c671
DK
1515 const Sized_symbol<32>* gsym,
1516 const Symbol_value<32>* psymval,
1517 unsigned char* view,
1518 elfcpp::Elf_types<32>::Elf_Addr address,
4a657b0d
DK
1519 section_size_type /* view_size */ )
1520{
c121c671
DK
1521 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
1522
1523 r_type = get_real_reloc_type(r_type);
1524
1525 // If this the symbol may be a Thumb function, set thumb bit to 1.
1526 bool has_thumb_bit = ((gsym != NULL)
1527 && (gsym->type() == elfcpp::STT_FUNC
1528 || gsym->type() == elfcpp::STT_ARM_TFUNC));
1529
1530 // Pick the value to use for symbols defined in shared objects.
1531 Symbol_value<32> symval;
1532 if (gsym != NULL
1533 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
1534 {
1535 symval.set_output_value(target->plt_section()->address()
1536 + gsym->plt_offset());
1537 psymval = &symval;
1538 has_thumb_bit = 0;
1539 }
1540
1541 const Sized_relobj<32, big_endian>* object = relinfo->object;
1542
1543 // Get the GOT offset if needed.
1544 // The GOT pointer points to the end of the GOT section.
1545 // We need to subtract the size of the GOT section to get
1546 // the actual offset to use in the relocation.
1547 bool have_got_offset = false;
1548 unsigned int got_offset = 0;
1549 switch (r_type)
1550 {
1551 case elfcpp::R_ARM_GOT_BREL:
1552 if (gsym != NULL)
1553 {
1554 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1555 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1556 - target->got_size());
1557 }
1558 else
1559 {
1560 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1561 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1562 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1563 - target->got_size());
1564 }
1565 have_got_offset = true;
1566 break;
1567
1568 default:
1569 break;
1570 }
1571
1572 typename Arm_relocate_functions::Status reloc_status =
1573 Arm_relocate_functions::STATUS_OKAY;
4a657b0d
DK
1574 switch (r_type)
1575 {
1576 case elfcpp::R_ARM_NONE:
1577 break;
1578
c121c671
DK
1579 case elfcpp::R_ARM_ABS32:
1580 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
1581 output_section))
1582 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
1583 has_thumb_bit);
1584 break;
1585
1586 case elfcpp::R_ARM_REL32:
1587 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
1588 address, has_thumb_bit);
1589 break;
1590
1591 case elfcpp::R_ARM_THM_CALL:
1592 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
1593 address, has_thumb_bit);
1594 break;
1595
1596 case elfcpp::R_ARM_GOTOFF32:
1597 {
1598 elfcpp::Elf_types<32>::Elf_Addr got_origin;
1599 got_origin = target->got_plt_section()->address();
1600 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
1601 got_origin, has_thumb_bit);
1602 }
1603 break;
1604
1605 case elfcpp::R_ARM_BASE_PREL:
1606 {
1607 uint32_t origin;
1608 // Get the addressing origin of the output segment defining the
1609 // symbol gsym (AAELF 4.6.1.2 Relocation types)
1610 gold_assert(gsym != NULL);
1611 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
1612 origin = gsym->output_segment()->vaddr();
1613 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
1614 origin = gsym->output_data()->address();
1615 else
1616 {
1617 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1618 _("cannot find origin of R_ARM_BASE_PREL"));
1619 return true;
1620 }
1621 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
1622 }
1623 break;
1624
1625 case elfcpp::R_ARM_GOT_BREL:
1626 gold_assert(have_got_offset);
1627 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
1628 break;
1629
1630 case elfcpp::R_ARM_PLT32:
1631 gold_assert(gsym == NULL
1632 || gsym->has_plt_offset()
1633 || gsym->final_value_is_known()
1634 || (gsym->is_defined()
1635 && !gsym->is_from_dynobj()
1636 && !gsym->is_preemptible()));
1637 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
1638 address, has_thumb_bit);
1639 break;
1640
1641 case elfcpp::R_ARM_CALL:
1642 reloc_status = Arm_relocate_functions::call(view, object, psymval,
1643 address, has_thumb_bit);
1644 break;
1645
1646 case elfcpp::R_ARM_JUMP24:
1647 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
1648 address, has_thumb_bit);
1649 break;
1650
1651 case elfcpp::R_ARM_PREL31:
1652 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
1653 address, has_thumb_bit);
1654 break;
1655
1656 case elfcpp::R_ARM_TARGET1:
1657 // This should have been mapped to another type already.
1658 // Fall through.
1659 case elfcpp::R_ARM_COPY:
1660 case elfcpp::R_ARM_GLOB_DAT:
1661 case elfcpp::R_ARM_JUMP_SLOT:
1662 case elfcpp::R_ARM_RELATIVE:
1663 // These are relocations which should only be seen by the
1664 // dynamic linker, and should never be seen here.
1665 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1666 _("unexpected reloc %u in object file"),
1667 r_type);
1668 break;
1669
1670 default:
1671 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1672 _("unsupported reloc %u"),
1673 r_type);
1674 break;
1675 }
1676
1677 // Report any errors.
1678 switch (reloc_status)
1679 {
1680 case Arm_relocate_functions::STATUS_OKAY:
1681 break;
1682 case Arm_relocate_functions::STATUS_OVERFLOW:
1683 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1684 _("relocation overflow in relocation %u"),
1685 r_type);
1686 break;
1687 case Arm_relocate_functions::STATUS_BAD_RELOC:
1688 gold_error_at_location(
1689 relinfo,
1690 relnum,
1691 rel.get_r_offset(),
1692 _("unexpected opcode while processing relocation %u"),
1693 r_type);
1694 break;
4a657b0d
DK
1695 default:
1696 gold_unreachable();
1697 }
1698
1699 return true;
1700}
1701
1702// Relocate section data.
1703
1704template<bool big_endian>
1705void
1706Target_arm<big_endian>::relocate_section(
1707 const Relocate_info<32, big_endian>* relinfo,
1708 unsigned int sh_type,
1709 const unsigned char* prelocs,
1710 size_t reloc_count,
1711 Output_section* output_section,
1712 bool needs_special_offset_handling,
1713 unsigned char* view,
1714 elfcpp::Elf_types<32>::Elf_Addr address,
1715 section_size_type view_size)
1716{
1717 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
1718 gold_assert(sh_type == elfcpp::SHT_REL);
1719
1720 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
1721 Arm_relocate>(
1722 relinfo,
1723 this,
1724 prelocs,
1725 reloc_count,
1726 output_section,
1727 needs_special_offset_handling,
1728 view,
1729 address,
1730 view_size);
1731}
1732
1733// Return the size of a relocation while scanning during a relocatable
1734// link.
1735
1736template<bool big_endian>
1737unsigned int
1738Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1739 unsigned int r_type,
1740 Relobj* object)
1741{
1742 r_type = get_real_reloc_type(r_type);
1743 switch (r_type)
1744 {
1745 case elfcpp::R_ARM_NONE:
1746 return 0;
1747
1748 case elfcpp::R_ARM_ABS32:
1749 case elfcpp::R_ARM_REL32:
1750 case elfcpp::R_ARM_THM_CALL:
1751 case elfcpp::R_ARM_GOTOFF32:
1752 case elfcpp::R_ARM_BASE_PREL:
1753 case elfcpp::R_ARM_GOT_BREL:
1754 case elfcpp::R_ARM_PLT32:
1755 case elfcpp::R_ARM_CALL:
1756 case elfcpp::R_ARM_JUMP24:
1757 case elfcpp::R_ARM_PREL31:
1758 return 4;
1759
1760 case elfcpp::R_ARM_TARGET1:
1761 // This should have been mapped to another type already.
1762 // Fall through.
1763 case elfcpp::R_ARM_COPY:
1764 case elfcpp::R_ARM_GLOB_DAT:
1765 case elfcpp::R_ARM_JUMP_SLOT:
1766 case elfcpp::R_ARM_RELATIVE:
1767 // These are relocations which should only be seen by the
1768 // dynamic linker, and should never be seen here.
1769 gold_error(_("%s: unexpected reloc %u in object file"),
1770 object->name().c_str(), r_type);
1771 return 0;
1772
1773 default:
1774 object->error(_("unsupported reloc %u in object file"), r_type);
1775 return 0;
1776 }
1777}
1778
1779// Scan the relocs during a relocatable link.
1780
1781template<bool big_endian>
1782void
1783Target_arm<big_endian>::scan_relocatable_relocs(
1784 const General_options& options,
1785 Symbol_table* symtab,
1786 Layout* layout,
1787 Sized_relobj<32, big_endian>* object,
1788 unsigned int data_shndx,
1789 unsigned int sh_type,
1790 const unsigned char* prelocs,
1791 size_t reloc_count,
1792 Output_section* output_section,
1793 bool needs_special_offset_handling,
1794 size_t local_symbol_count,
1795 const unsigned char* plocal_symbols,
1796 Relocatable_relocs* rr)
1797{
1798 gold_assert(sh_type == elfcpp::SHT_REL);
1799
1800 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
1801 Relocatable_size_for_reloc> Scan_relocatable_relocs;
1802
1803 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
1804 Scan_relocatable_relocs>(
1805 options,
1806 symtab,
1807 layout,
1808 object,
1809 data_shndx,
1810 prelocs,
1811 reloc_count,
1812 output_section,
1813 needs_special_offset_handling,
1814 local_symbol_count,
1815 plocal_symbols,
1816 rr);
1817}
1818
1819// Relocate a section during a relocatable link.
1820
1821template<bool big_endian>
1822void
1823Target_arm<big_endian>::relocate_for_relocatable(
1824 const Relocate_info<32, big_endian>* relinfo,
1825 unsigned int sh_type,
1826 const unsigned char* prelocs,
1827 size_t reloc_count,
1828 Output_section* output_section,
1829 off_t offset_in_output_section,
1830 const Relocatable_relocs* rr,
1831 unsigned char* view,
1832 elfcpp::Elf_types<32>::Elf_Addr view_address,
1833 section_size_type view_size,
1834 unsigned char* reloc_view,
1835 section_size_type reloc_view_size)
1836{
1837 gold_assert(sh_type == elfcpp::SHT_REL);
1838
1839 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
1840 relinfo,
1841 prelocs,
1842 reloc_count,
1843 output_section,
1844 offset_in_output_section,
1845 rr,
1846 view,
1847 view_address,
1848 view_size,
1849 reloc_view,
1850 reloc_view_size);
1851}
1852
94cdfcff
DK
1853// Return the value to use for a dynamic symbol which requires special
1854// treatment. This is how we support equality comparisons of function
1855// pointers across shared library boundaries, as described in the
1856// processor specific ABI supplement.
1857
4a657b0d
DK
1858template<bool big_endian>
1859uint64_t
94cdfcff 1860Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4a657b0d 1861{
94cdfcff
DK
1862 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1863 return this->plt_section()->address() + gsym->plt_offset();
4a657b0d
DK
1864}
1865
1866// Map platform-specific relocs to real relocs
1867//
1868template<bool big_endian>
1869unsigned int
1870Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
1871{
1872 switch (r_type)
1873 {
1874 case elfcpp::R_ARM_TARGET1:
1875 // This is either R_ARM_ABS32 or R_ARM_REL32;
1876 return elfcpp::R_ARM_ABS32;
1877
1878 case elfcpp::R_ARM_TARGET2:
1879 // This can be any reloc type but ususally is R_ARM_GOT_PREL
1880 return elfcpp::R_ARM_GOT_PREL;
1881
1882 default:
1883 return r_type;
1884 }
1885}
1886
1887// The selector for arm object files.
1888
1889template<bool big_endian>
1890class Target_selector_arm : public Target_selector
1891{
1892 public:
1893 Target_selector_arm()
1894 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
1895 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
1896 { }
1897
1898 Target*
1899 do_instantiate_target()
1900 { return new Target_arm<big_endian>(); }
1901};
1902
1903Target_selector_arm<false> target_selector_arm;
1904Target_selector_arm<true> target_selector_armbe;
1905
1906} // End anonymous namespace.