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