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