2009-10-21 Paul Pluzhnikov <ppluzhnikov@google.com>
[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>.
b569affa
DK
6// This file also contains borrowed and adapted code from
7// bfd/elf32-arm.c.
4a657b0d
DK
8
9// This file is part of gold.
10
11// This program is free software; you can redistribute it and/or modify
12// it under the terms of the GNU General Public License as published by
13// the Free Software Foundation; either version 3 of the License, or
14// (at your option) any later version.
15
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24// MA 02110-1301, USA.
25
26#include "gold.h"
27
28#include <cstring>
29#include <limits>
30#include <cstdio>
31#include <string>
32
33#include "elfcpp.h"
34#include "parameters.h"
35#include "reloc.h"
36#include "arm.h"
37#include "object.h"
38#include "symtab.h"
39#include "layout.h"
40#include "output.h"
41#include "copy-relocs.h"
42#include "target.h"
43#include "target-reloc.h"
44#include "target-select.h"
45#include "tls.h"
46#include "defstd.h"
f345227a 47#include "gc.h"
4a657b0d
DK
48
49namespace
50{
51
52using namespace gold;
53
94cdfcff
DK
54template<bool big_endian>
55class Output_data_plt_arm;
56
b569affa
DK
57template<bool big_endian>
58class Target_arm;
59
60// For convenience.
61typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
62
63// Maximum branch offsets for ARM, THUMB and THUMB2.
64const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
65const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
66const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
67const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
68const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
69const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
70
4a657b0d
DK
71// The arm target class.
72//
73// This is a very simple port of gold for ARM-EABI. It is intended for
74// supporting Android only for the time being. Only these relocation types
75// are supported.
76//
77// R_ARM_NONE
78// R_ARM_ABS32
be8fcb75
ILT
79// R_ARM_ABS32_NOI
80// R_ARM_ABS16
81// R_ARM_ABS12
82// R_ARM_ABS8
83// R_ARM_THM_ABS5
84// R_ARM_BASE_ABS
4a657b0d
DK
85// R_ARM_REL32
86// R_ARM_THM_CALL
87// R_ARM_COPY
88// R_ARM_GLOB_DAT
89// R_ARM_BASE_PREL
90// R_ARM_JUMP_SLOT
91// R_ARM_RELATIVE
92// R_ARM_GOTOFF32
93// R_ARM_GOT_BREL
7f5309a5 94// R_ARM_GOT_PREL
4a657b0d
DK
95// R_ARM_PLT32
96// R_ARM_CALL
97// R_ARM_JUMP24
98// R_ARM_TARGET1
99// R_ARM_PREL31
7f5309a5 100// R_ARM_ABS8
fd3c5f0b
ILT
101// R_ARM_MOVW_ABS_NC
102// R_ARM_MOVT_ABS
103// R_ARM_THM_MOVW_ABS_NC
c2a122b6
ILT
104// R_ARM_THM_MOVT_ABS
105// R_ARM_MOVW_PREL_NC
106// R_ARM_MOVT_PREL
107// R_ARM_THM_MOVW_PREL_NC
108// R_ARM_THM_MOVT_PREL
4a657b0d 109//
4a657b0d 110// TODOs:
11af873f
DK
111// - Generate various branch stubs.
112// - Support interworking.
113// - Define section symbols __exidx_start and __exidx_stop.
4a657b0d 114// - Support more relocation types as needed.
94cdfcff
DK
115// - Make PLTs more flexible for different architecture features like
116// Thumb-2 and BE8.
11af873f 117// There are probably a lot more.
4a657b0d 118
b569affa
DK
119// Instruction template class. This class is similar to the insn_sequence
120// struct in bfd/elf32-arm.c.
121
122class Insn_template
123{
124 public:
125 // Types of instruction templates.
126 enum Type
127 {
128 THUMB16_TYPE = 1,
129 THUMB32_TYPE,
130 ARM_TYPE,
131 DATA_TYPE
132 };
133
134 // Factory methods to create instrunction templates in different formats.
135
136 static const Insn_template
137 thumb16_insn(uint32_t data)
138 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
139
140 // A bit of a hack. A Thumb conditional branch, in which the proper
141 // condition is inserted when we build the stub.
142 static const Insn_template
143 thumb16_bcond_insn(uint32_t data)
144 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 1); }
145
146 static const Insn_template
147 thumb32_insn(uint32_t data)
148 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
149
150 static const Insn_template
151 thumb32_b_insn(uint32_t data, int reloc_addend)
152 {
153 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
154 reloc_addend);
155 }
156
157 static const Insn_template
158 arm_insn(uint32_t data)
159 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
160
161 static const Insn_template
162 arm_rel_insn(unsigned data, int reloc_addend)
163 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
164
165 static const Insn_template
166 data_word(unsigned data, unsigned int r_type, int reloc_addend)
167 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
168
169 // Accessors. This class is used for read-only objects so no modifiers
170 // are provided.
171
172 uint32_t
173 data() const
174 { return this->data_; }
175
176 // Return the instruction sequence type of this.
177 Type
178 type() const
179 { return this->type_; }
180
181 // Return the ARM relocation type of this.
182 unsigned int
183 r_type() const
184 { return this->r_type_; }
185
186 int32_t
187 reloc_addend() const
188 { return this->reloc_addend_; }
189
190 // Return size of instrunction template in bytes.
191 size_t
192 size() const;
193
194 // Return byte-alignment of instrunction template.
195 unsigned
196 alignment() const;
197
198 private:
199 // We make the constructor private to ensure that only the factory
200 // methods are used.
201 inline
202 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
203 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
204 { }
205
206 // Instruction specific data. This is used to store information like
207 // some of the instruction bits.
208 uint32_t data_;
209 // Instruction template type.
210 Type type_;
211 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
212 unsigned int r_type_;
213 // Relocation addend.
214 int32_t reloc_addend_;
215};
216
217// Macro for generating code to stub types. One entry per long/short
218// branch stub
219
220#define DEF_STUBS \
221 DEF_STUB(long_branch_any_any) \
222 DEF_STUB(long_branch_v4t_arm_thumb) \
223 DEF_STUB(long_branch_thumb_only) \
224 DEF_STUB(long_branch_v4t_thumb_thumb) \
225 DEF_STUB(long_branch_v4t_thumb_arm) \
226 DEF_STUB(short_branch_v4t_thumb_arm) \
227 DEF_STUB(long_branch_any_arm_pic) \
228 DEF_STUB(long_branch_any_thumb_pic) \
229 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
230 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
231 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
232 DEF_STUB(long_branch_thumb_only_pic) \
233 DEF_STUB(a8_veneer_b_cond) \
234 DEF_STUB(a8_veneer_b) \
235 DEF_STUB(a8_veneer_bl) \
236 DEF_STUB(a8_veneer_blx)
237
238// Stub types.
239
240#define DEF_STUB(x) arm_stub_##x,
241typedef enum
242 {
243 arm_stub_none,
244 DEF_STUBS
245
246 // First reloc stub type.
247 arm_stub_reloc_first = arm_stub_long_branch_any_any,
248 // Last reloc stub type.
249 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
250
251 // First Cortex-A8 stub type.
252 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
253 // Last Cortex-A8 stub type.
254 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
255
256 // Last stub type.
257 arm_stub_type_last = arm_stub_a8_veneer_blx
258 } Stub_type;
259#undef DEF_STUB
260
261// Stub template class. Templates are meant to be read-only objects.
262// A stub template for a stub type contains all read-only attributes
263// common to all stubs of the same type.
264
265class Stub_template
266{
267 public:
268 Stub_template(Stub_type, const Insn_template*, size_t);
269
270 ~Stub_template()
271 { }
272
273 // Return stub type.
274 Stub_type
275 type() const
276 { return this->type_; }
277
278 // Return an array of instruction templates.
279 const Insn_template*
280 insns() const
281 { return this->insns_; }
282
283 // Return size of template in number of instructions.
284 size_t
285 insn_count() const
286 { return this->insn_count_; }
287
288 // Return size of template in bytes.
289 size_t
290 size() const
291 { return this->size_; }
292
293 // Return alignment of the stub template.
294 unsigned
295 alignment() const
296 { return this->alignment_; }
297
298 // Return whether entry point is in thumb mode.
299 bool
300 entry_in_thumb_mode() const
301 { return this->entry_in_thumb_mode_; }
302
303 // Return number of relocations in this template.
304 size_t
305 reloc_count() const
306 { return this->relocs_.size(); }
307
308 // Return index of the I-th instruction with relocation.
309 size_t
310 reloc_insn_index(size_t i) const
311 {
312 gold_assert(i < this->relocs_.size());
313 return this->relocs_[i].first;
314 }
315
316 // Return the offset of the I-th instruction with relocation from the
317 // beginning of the stub.
318 section_size_type
319 reloc_offset(size_t i) const
320 {
321 gold_assert(i < this->relocs_.size());
322 return this->relocs_[i].second;
323 }
324
325 private:
326 // This contains information about an instruction template with a relocation
327 // and its offset from start of stub.
328 typedef std::pair<size_t, section_size_type> Reloc;
329
330 // A Stub_template may not be copied. We want to share templates as much
331 // as possible.
332 Stub_template(const Stub_template&);
333 Stub_template& operator=(const Stub_template&);
334
335 // Stub type.
336 Stub_type type_;
337 // Points to an array of Insn_templates.
338 const Insn_template* insns_;
339 // Number of Insn_templates in insns_[].
340 size_t insn_count_;
341 // Size of templated instructions in bytes.
342 size_t size_;
343 // Alignment of templated instructions.
344 unsigned alignment_;
345 // Flag to indicate if entry is in thumb mode.
346 bool entry_in_thumb_mode_;
347 // A table of reloc instruction indices and offsets. We can find these by
348 // looking at the instruction templates but we pre-compute and then stash
349 // them here for speed.
350 std::vector<Reloc> relocs_;
351};
352
353//
354// A class for code stubs. This is a base class for different type of
355// stubs used in the ARM target.
356//
357
358class Stub
359{
360 private:
361 static const section_offset_type invalid_offset =
362 static_cast<section_offset_type>(-1);
363
364 public:
365 Stub(const Stub_template* stub_template)
366 : stub_template_(stub_template), offset_(invalid_offset)
367 { }
368
369 virtual
370 ~Stub()
371 { }
372
373 // Return the stub template.
374 const Stub_template*
375 stub_template() const
376 { return this->stub_template_; }
377
378 // Return offset of code stub from beginning of its containing stub table.
379 section_offset_type
380 offset() const
381 {
382 gold_assert(this->offset_ != invalid_offset);
383 return this->offset_;
384 }
385
386 // Set offset of code stub from beginning of its containing stub table.
387 void
388 set_offset(section_offset_type offset)
389 { this->offset_ = offset; }
390
391 // Return the relocation target address of the i-th relocation in the
392 // stub. This must be defined in a child class.
393 Arm_address
394 reloc_target(size_t i)
395 { return this->do_reloc_target(i); }
396
397 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
398 void
399 write(unsigned char* view, section_size_type view_size, bool big_endian)
400 { this->do_write(view, view_size, big_endian); }
401
402 protected:
403 // This must be defined in the child class.
404 virtual Arm_address
405 do_reloc_target(size_t) = 0;
406
407 // This must be defined in the child class.
408 virtual void
409 do_write(unsigned char*, section_size_type, bool) = 0;
410
411 private:
412 // Its template.
413 const Stub_template* stub_template_;
414 // Offset within the section of containing this stub.
415 section_offset_type offset_;
416};
417
418// Reloc stub class. These are stubs we use to fix up relocation because
419// of limited branch ranges.
420
421class Reloc_stub : public Stub
422{
423 public:
424 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
425 // We assume we never jump to this address.
426 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
427
428 // Return destination address.
429 Arm_address
430 destination_address() const
431 {
432 gold_assert(this->destination_address_ != this->invalid_address);
433 return this->destination_address_;
434 }
435
436 // Set destination address.
437 void
438 set_destination_address(Arm_address address)
439 {
440 gold_assert(address != this->invalid_address);
441 this->destination_address_ = address;
442 }
443
444 // Reset destination address.
445 void
446 reset_destination_address()
447 { this->destination_address_ = this->invalid_address; }
448
449 // Determine stub type for a branch of a relocation of R_TYPE going
450 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
451 // the branch target is a thumb instruction. TARGET is used for look
452 // up ARM-specific linker settings.
453 static Stub_type
454 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
455 Arm_address branch_target, bool target_is_thumb);
456
457 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
458 // and an addend. Since we treat global and local symbol differently, we
459 // use a Symbol object for a global symbol and a object-index pair for
460 // a local symbol.
461 class Key
462 {
463 public:
464 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
465 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
466 // and R_SYM must not be invalid_index.
467 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
468 unsigned int r_sym, int32_t addend)
469 : stub_type_(stub_type), addend_(addend)
470 {
471 if (symbol != NULL)
472 {
473 this->r_sym_ = Reloc_stub::invalid_index;
474 this->u_.symbol = symbol;
475 }
476 else
477 {
478 gold_assert(relobj != NULL && r_sym != invalid_index);
479 this->r_sym_ = r_sym;
480 this->u_.relobj = relobj;
481 }
482 }
483
484 ~Key()
485 { }
486
487 // Accessors: Keys are meant to be read-only object so no modifiers are
488 // provided.
489
490 // Return stub type.
491 Stub_type
492 stub_type() const
493 { return this->stub_type_; }
494
495 // Return the local symbol index or invalid_index.
496 unsigned int
497 r_sym() const
498 { return this->r_sym_; }
499
500 // Return the symbol if there is one.
501 const Symbol*
502 symbol() const
503 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
504
505 // Return the relobj if there is one.
506 const Relobj*
507 relobj() const
508 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
509
510 // Whether this equals to another key k.
511 bool
512 eq(const Key& k) const
513 {
514 return ((this->stub_type_ == k.stub_type_)
515 && (this->r_sym_ == k.r_sym_)
516 && ((this->r_sym_ != Reloc_stub::invalid_index)
517 ? (this->u_.relobj == k.u_.relobj)
518 : (this->u_.symbol == k.u_.symbol))
519 && (this->addend_ == k.addend_));
520 }
521
522 // Return a hash value.
523 size_t
524 hash_value() const
525 {
526 return (this->stub_type_
527 ^ this->r_sym_
528 ^ gold::string_hash<char>(
529 (this->r_sym_ != Reloc_stub::invalid_index)
530 ? this->u_.relobj->name().c_str()
531 : this->u_.symbol->name())
532 ^ this->addend_);
533 }
534
535 // Functors for STL associative containers.
536 struct hash
537 {
538 size_t
539 operator()(const Key& k) const
540 { return k.hash_value(); }
541 };
542
543 struct equal_to
544 {
545 bool
546 operator()(const Key& k1, const Key& k2) const
547 { return k1.eq(k2); }
548 };
549
550 // Name of key. This is mainly for debugging.
551 std::string
552 name() const;
553
554 private:
555 // Stub type.
556 Stub_type stub_type_;
557 // If this is a local symbol, this is the index in the defining object.
558 // Otherwise, it is invalid_index for a global symbol.
559 unsigned int r_sym_;
560 // If r_sym_ is invalid index. This points to a global symbol.
561 // Otherwise, this points a relobj. We used the unsized and target
562 // independent Symbol and Relobj classes instead of Arm_symbol and
563 // Arm_relobj. This is done to avoid making the stub class a template
564 // as most of the stub machinery is endianity-neutral. However, it
565 // may require a bit of casting done by users of this class.
566 union
567 {
568 const Symbol* symbol;
569 const Relobj* relobj;
570 } u_;
571 // Addend associated with a reloc.
572 int32_t addend_;
573 };
574
575 protected:
576 // Reloc_stubs are created via a stub factory. So these are protected.
577 Reloc_stub(const Stub_template* stub_template)
578 : Stub(stub_template), destination_address_(invalid_address)
579 { }
580
581 ~Reloc_stub()
582 { }
583
584 friend class Stub_factory;
585
586 private:
587 // Return the relocation target address of the i-th relocation in the
588 // stub.
589 Arm_address
590 do_reloc_target(size_t i)
591 {
592 // All reloc stub have only one relocation.
593 gold_assert(i == 0);
594 return this->destination_address_;
595 }
596
597 // A template to implement do_write below.
598 template<bool big_endian>
599 void inline
600 do_fixed_endian_write(unsigned char*, section_size_type);
601
602 // Write a stub.
603 void
604 do_write(unsigned char* view, section_size_type view_size, bool big_endian);
605
606 // Address of destination.
607 Arm_address destination_address_;
608};
609
610// Stub factory class.
611
612class Stub_factory
613{
614 public:
615 // Return the unique instance of this class.
616 static const Stub_factory&
617 get_instance()
618 {
619 static Stub_factory singleton;
620 return singleton;
621 }
622
623 // Make a relocation stub.
624 Reloc_stub*
625 make_reloc_stub(Stub_type stub_type) const
626 {
627 gold_assert(stub_type >= arm_stub_reloc_first
628 && stub_type <= arm_stub_reloc_last);
629 return new Reloc_stub(this->stub_templates_[stub_type]);
630 }
631
632 private:
633 // Constructor and destructor are protected since we only return a single
634 // instance created in Stub_factory::get_instance().
635
636 Stub_factory();
637
638 // A Stub_factory may not be copied since it is a singleton.
639 Stub_factory(const Stub_factory&);
640 Stub_factory& operator=(Stub_factory&);
641
642 // Stub templates. These are initialized in the constructor.
643 const Stub_template* stub_templates_[arm_stub_type_last+1];
644};
645
c121c671
DK
646// Utilities for manipulating integers of up to 32-bits
647
648namespace utils
649{
650 // Sign extend an n-bit unsigned integer stored in an uint32_t into
651 // an int32_t. NO_BITS must be between 1 to 32.
652 template<int no_bits>
653 static inline int32_t
654 sign_extend(uint32_t bits)
655 {
96d49306 656 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
657 if (no_bits == 32)
658 return static_cast<int32_t>(bits);
659 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
660 bits &= mask;
661 uint32_t top_bit = 1U << (no_bits - 1);
662 int32_t as_signed = static_cast<int32_t>(bits);
663 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
664 }
665
666 // Detects overflow of an NO_BITS integer stored in a uint32_t.
667 template<int no_bits>
668 static inline bool
669 has_overflow(uint32_t bits)
670 {
96d49306 671 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
672 if (no_bits == 32)
673 return false;
674 int32_t max = (1 << (no_bits - 1)) - 1;
675 int32_t min = -(1 << (no_bits - 1));
676 int32_t as_signed = static_cast<int32_t>(bits);
677 return as_signed > max || as_signed < min;
678 }
679
5e445df6
ILT
680 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
681 // fits in the given number of bits as either a signed or unsigned value.
682 // For example, has_signed_unsigned_overflow<8> would check
683 // -128 <= bits <= 255
684 template<int no_bits>
685 static inline bool
686 has_signed_unsigned_overflow(uint32_t bits)
687 {
688 gold_assert(no_bits >= 2 && no_bits <= 32);
689 if (no_bits == 32)
690 return false;
691 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
692 int32_t min = -(1 << (no_bits - 1));
693 int32_t as_signed = static_cast<int32_t>(bits);
694 return as_signed > max || as_signed < min;
695 }
696
c121c671
DK
697 // Select bits from A and B using bits in MASK. For each n in [0..31],
698 // the n-th bit in the result is chosen from the n-th bits of A and B.
699 // A zero selects A and a one selects B.
700 static inline uint32_t
701 bit_select(uint32_t a, uint32_t b, uint32_t mask)
702 { return (a & ~mask) | (b & mask); }
703};
704
4a657b0d
DK
705template<bool big_endian>
706class Target_arm : public Sized_target<32, big_endian>
707{
708 public:
709 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
710 Reloc_section;
711
712 Target_arm()
94cdfcff
DK
713 : Sized_target<32, big_endian>(&arm_info),
714 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
b569affa
DK
715 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
716 may_use_blx_(true), should_force_pic_veneer_(false)
4a657b0d
DK
717 { }
718
b569affa
DK
719 // Whether we can use BLX.
720 bool
721 may_use_blx() const
722 { return this->may_use_blx_; }
723
724 // Set use-BLX flag.
725 void
726 set_may_use_blx(bool value)
727 { this->may_use_blx_ = value; }
728
729 // Whether we force PCI branch veneers.
730 bool
731 should_force_pic_veneer() const
732 { return this->should_force_pic_veneer_; }
733
734 // Set PIC veneer flag.
735 void
736 set_should_force_pic_veneer(bool value)
737 { this->should_force_pic_veneer_ = value; }
738
739 // Whether we use THUMB-2 instructions.
740 bool
741 using_thumb2() const
742 {
743 // FIXME: This should not hard-coded.
744 return false;
745 }
746
747 // Whether we use THUMB/THUMB-2 instructions only.
748 bool
749 using_thumb_only() const
750 {
751 // FIXME: This should not hard-coded.
752 return false;
753 }
754
4a657b0d
DK
755 // Process the relocations to determine unreferenced sections for
756 // garbage collection.
757 void
758 gc_process_relocs(const General_options& options,
759 Symbol_table* symtab,
760 Layout* layout,
761 Sized_relobj<32, big_endian>* object,
762 unsigned int data_shndx,
763 unsigned int sh_type,
764 const unsigned char* prelocs,
765 size_t reloc_count,
766 Output_section* output_section,
767 bool needs_special_offset_handling,
768 size_t local_symbol_count,
769 const unsigned char* plocal_symbols);
770
771 // Scan the relocations to look for symbol adjustments.
772 void
773 scan_relocs(const General_options& options,
774 Symbol_table* symtab,
775 Layout* layout,
776 Sized_relobj<32, big_endian>* object,
777 unsigned int data_shndx,
778 unsigned int sh_type,
779 const unsigned char* prelocs,
780 size_t reloc_count,
781 Output_section* output_section,
782 bool needs_special_offset_handling,
783 size_t local_symbol_count,
784 const unsigned char* plocal_symbols);
785
786 // Finalize the sections.
787 void
788 do_finalize_sections(Layout*);
789
94cdfcff 790 // Return the value to use for a dynamic symbol which requires special
4a657b0d
DK
791 // treatment.
792 uint64_t
793 do_dynsym_value(const Symbol*) const;
794
795 // Relocate a section.
796 void
797 relocate_section(const Relocate_info<32, big_endian>*,
798 unsigned int sh_type,
799 const unsigned char* prelocs,
800 size_t reloc_count,
801 Output_section* output_section,
802 bool needs_special_offset_handling,
803 unsigned char* view,
804 elfcpp::Elf_types<32>::Elf_Addr view_address,
364c7fa5
ILT
805 section_size_type view_size,
806 const Reloc_symbol_changes*);
4a657b0d
DK
807
808 // Scan the relocs during a relocatable link.
809 void
810 scan_relocatable_relocs(const General_options& options,
811 Symbol_table* symtab,
812 Layout* layout,
813 Sized_relobj<32, big_endian>* object,
814 unsigned int data_shndx,
815 unsigned int sh_type,
816 const unsigned char* prelocs,
817 size_t reloc_count,
818 Output_section* output_section,
819 bool needs_special_offset_handling,
820 size_t local_symbol_count,
821 const unsigned char* plocal_symbols,
822 Relocatable_relocs*);
823
824 // Relocate a section during a relocatable link.
825 void
826 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
827 unsigned int sh_type,
828 const unsigned char* prelocs,
829 size_t reloc_count,
830 Output_section* output_section,
831 off_t offset_in_output_section,
832 const Relocatable_relocs*,
833 unsigned char* view,
834 elfcpp::Elf_types<32>::Elf_Addr view_address,
835 section_size_type view_size,
836 unsigned char* reloc_view,
837 section_size_type reloc_view_size);
838
839 // Return whether SYM is defined by the ABI.
840 bool
841 do_is_defined_by_abi(Symbol* sym) const
842 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
843
94cdfcff
DK
844 // Return the size of the GOT section.
845 section_size_type
846 got_size()
847 {
848 gold_assert(this->got_ != NULL);
849 return this->got_->data_size();
850 }
851
4a657b0d
DK
852 // Map platform-specific reloc types
853 static unsigned int
854 get_real_reloc_type (unsigned int r_type);
855
b569affa
DK
856 // Get the default ARM target.
857 static const Target_arm<big_endian>&
858 default_target()
859 {
860 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
861 && parameters->target().is_big_endian() == big_endian);
862 return static_cast<const Target_arm<big_endian>&>(parameters->target());
863 }
864
4a657b0d
DK
865 private:
866 // The class which scans relocations.
867 class Scan
868 {
869 public:
870 Scan()
bec53400 871 : issued_non_pic_error_(false)
4a657b0d
DK
872 { }
873
874 inline void
875 local(const General_options& options, Symbol_table* symtab,
876 Layout* layout, Target_arm* target,
877 Sized_relobj<32, big_endian>* object,
878 unsigned int data_shndx,
879 Output_section* output_section,
880 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
881 const elfcpp::Sym<32, big_endian>& lsym);
882
883 inline void
884 global(const General_options& options, Symbol_table* symtab,
885 Layout* layout, Target_arm* target,
886 Sized_relobj<32, big_endian>* object,
887 unsigned int data_shndx,
888 Output_section* output_section,
889 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
890 Symbol* gsym);
891
892 private:
893 static void
894 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
895 unsigned int r_type);
896
897 static void
898 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
899 unsigned int r_type, Symbol*);
bec53400
DK
900
901 void
902 check_non_pic(Relobj*, unsigned int r_type);
903
904 // Almost identical to Symbol::needs_plt_entry except that it also
905 // handles STT_ARM_TFUNC.
906 static bool
907 symbol_needs_plt_entry(const Symbol* sym)
908 {
909 // An undefined symbol from an executable does not need a PLT entry.
910 if (sym->is_undefined() && !parameters->options().shared())
911 return false;
912
913 return (!parameters->doing_static_link()
914 && (sym->type() == elfcpp::STT_FUNC
915 || sym->type() == elfcpp::STT_ARM_TFUNC)
916 && (sym->is_from_dynobj()
917 || sym->is_undefined()
918 || sym->is_preemptible()));
919 }
920
921 // Whether we have issued an error about a non-PIC compilation.
922 bool issued_non_pic_error_;
4a657b0d
DK
923 };
924
925 // The class which implements relocation.
926 class Relocate
927 {
928 public:
929 Relocate()
930 { }
931
932 ~Relocate()
933 { }
934
bec53400
DK
935 // Return whether the static relocation needs to be applied.
936 inline bool
937 should_apply_static_reloc(const Sized_symbol<32>* gsym,
938 int ref_flags,
939 bool is_32bit,
940 Output_section* output_section);
941
4a657b0d
DK
942 // Do a relocation. Return false if the caller should not issue
943 // any warnings about this relocation.
944 inline bool
945 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
946 Output_section*, size_t relnum,
947 const elfcpp::Rel<32, big_endian>&,
948 unsigned int r_type, const Sized_symbol<32>*,
949 const Symbol_value<32>*,
950 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
951 section_size_type);
c121c671
DK
952
953 // Return whether we want to pass flag NON_PIC_REF for this
954 // reloc.
955 static inline bool
956 reloc_is_non_pic (unsigned int r_type)
957 {
958 switch (r_type)
959 {
960 case elfcpp::R_ARM_REL32:
961 case elfcpp::R_ARM_THM_CALL:
962 case elfcpp::R_ARM_CALL:
963 case elfcpp::R_ARM_JUMP24:
964 case elfcpp::R_ARM_PREL31:
be8fcb75
ILT
965 case elfcpp::R_ARM_THM_ABS5:
966 case elfcpp::R_ARM_ABS8:
967 case elfcpp::R_ARM_ABS12:
968 case elfcpp::R_ARM_ABS16:
969 case elfcpp::R_ARM_BASE_ABS:
c121c671
DK
970 return true;
971 default:
972 return false;
973 }
974 }
4a657b0d
DK
975 };
976
977 // A class which returns the size required for a relocation type,
978 // used while scanning relocs during a relocatable link.
979 class Relocatable_size_for_reloc
980 {
981 public:
982 unsigned int
983 get_size_for_reloc(unsigned int, Relobj*);
984 };
985
94cdfcff
DK
986 // Get the GOT section, creating it if necessary.
987 Output_data_got<32, big_endian>*
988 got_section(Symbol_table*, Layout*);
989
990 // Get the GOT PLT section.
991 Output_data_space*
992 got_plt_section() const
993 {
994 gold_assert(this->got_plt_ != NULL);
995 return this->got_plt_;
996 }
997
998 // Create a PLT entry for a global symbol.
999 void
1000 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1001
1002 // Get the PLT section.
1003 const Output_data_plt_arm<big_endian>*
1004 plt_section() const
1005 {
1006 gold_assert(this->plt_ != NULL);
1007 return this->plt_;
1008 }
1009
1010 // Get the dynamic reloc section, creating it if necessary.
1011 Reloc_section*
1012 rel_dyn_section(Layout*);
1013
1014 // Return true if the symbol may need a COPY relocation.
1015 // References from an executable object to non-function symbols
1016 // defined in a dynamic object may need a COPY relocation.
1017 bool
1018 may_need_copy_reloc(Symbol* gsym)
1019 {
966d4097
DK
1020 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1021 && gsym->may_need_copy_reloc());
94cdfcff
DK
1022 }
1023
1024 // Add a potential copy relocation.
1025 void
1026 copy_reloc(Symbol_table* symtab, Layout* layout,
1027 Sized_relobj<32, big_endian>* object,
1028 unsigned int shndx, Output_section* output_section,
1029 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1030 {
1031 this->copy_relocs_.copy_reloc(symtab, layout,
1032 symtab->get_sized_symbol<32>(sym),
1033 object, shndx, output_section, reloc,
1034 this->rel_dyn_section(layout));
1035 }
1036
4a657b0d
DK
1037 // Information about this specific target which we pass to the
1038 // general Target structure.
1039 static const Target::Target_info arm_info;
94cdfcff
DK
1040
1041 // The types of GOT entries needed for this platform.
1042 enum Got_type
1043 {
1044 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
1045 };
1046
1047 // The GOT section.
1048 Output_data_got<32, big_endian>* got_;
1049 // The PLT section.
1050 Output_data_plt_arm<big_endian>* plt_;
1051 // The GOT PLT section.
1052 Output_data_space* got_plt_;
1053 // The dynamic reloc section.
1054 Reloc_section* rel_dyn_;
1055 // Relocs saved to avoid a COPY reloc.
1056 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1057 // Space for variables copied with a COPY reloc.
1058 Output_data_space* dynbss_;
b569affa
DK
1059 // Whether we can use BLX.
1060 bool may_use_blx_;
1061 // Whether we force PIC branch veneers.
1062 bool should_force_pic_veneer_;
4a657b0d
DK
1063};
1064
1065template<bool big_endian>
1066const Target::Target_info Target_arm<big_endian>::arm_info =
1067{
1068 32, // size
1069 big_endian, // is_big_endian
1070 elfcpp::EM_ARM, // machine_code
1071 false, // has_make_symbol
1072 false, // has_resolve
1073 false, // has_code_fill
1074 true, // is_default_stack_executable
1075 '\0', // wrap_char
1076 "/usr/lib/libc.so.1", // dynamic_linker
1077 0x8000, // default_text_segment_address
1078 0x1000, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
1079 0x1000, // common_pagesize (overridable by -z common-page-size)
1080 elfcpp::SHN_UNDEF, // small_common_shndx
1081 elfcpp::SHN_UNDEF, // large_common_shndx
1082 0, // small_common_section_flags
1083 0 // large_common_section_flags
4a657b0d
DK
1084};
1085
c121c671
DK
1086// Arm relocate functions class
1087//
1088
1089template<bool big_endian>
1090class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1091{
1092 public:
1093 typedef enum
1094 {
1095 STATUS_OKAY, // No error during relocation.
1096 STATUS_OVERFLOW, // Relocation oveflow.
1097 STATUS_BAD_RELOC // Relocation cannot be applied.
1098 } Status;
1099
1100 private:
1101 typedef Relocate_functions<32, big_endian> Base;
1102 typedef Arm_relocate_functions<big_endian> This;
1103
1104 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
1105 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
1106 // to distinguish ARM and THUMB functions and it is treated specially.
1107 static inline Symbol_value<32>::Value
1108 arm_symbol_value (const Sized_relobj<32, big_endian> *object,
1109 const Symbol_value<32>* psymval,
1110 Symbol_value<32>::Value addend,
1111 bool has_thumb_bit)
1112 {
1113 typedef Symbol_value<32>::Value Valtype;
1114
1115 if (has_thumb_bit)
1116 {
1117 Valtype raw = psymval->value(object, 0);
1118 Valtype thumb_bit = raw & 1;
1119 return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
1120 }
1121 else
1122 return psymval->value(object, addend);
1123 }
1124
fd3c5f0b
ILT
1125 // Encoding of imm16 argument for movt and movw ARM instructions
1126 // from ARM ARM:
1127 //
1128 // imm16 := imm4 | imm12
1129 //
1130 // 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
1131 // +-------+---------------+-------+-------+-----------------------+
1132 // | | |imm4 | |imm12 |
1133 // +-------+---------------+-------+-------+-----------------------+
1134
1135 // Extract the relocation addend from VAL based on the ARM
1136 // instruction encoding described above.
1137 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1138 extract_arm_movw_movt_addend(
1139 typename elfcpp::Swap<32, big_endian>::Valtype val)
1140 {
1141 // According to the Elf ABI for ARM Architecture the immediate
1142 // field is sign-extended to form the addend.
1143 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1144 }
1145
1146 // Insert X into VAL based on the ARM instruction encoding described
1147 // above.
1148 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1149 insert_val_arm_movw_movt(
1150 typename elfcpp::Swap<32, big_endian>::Valtype val,
1151 typename elfcpp::Swap<32, big_endian>::Valtype x)
1152 {
1153 val &= 0xfff0f000;
1154 val |= x & 0x0fff;
1155 val |= (x & 0xf000) << 4;
1156 return val;
1157 }
1158
1159 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1160 // from ARM ARM:
1161 //
1162 // imm16 := imm4 | i | imm3 | imm8
1163 //
1164 // 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
1165 // +---------+-+-----------+-------++-+-----+-------+---------------+
1166 // | |i| |imm4 || |imm3 | |imm8 |
1167 // +---------+-+-----------+-------++-+-----+-------+---------------+
1168
1169 // Extract the relocation addend from VAL based on the Thumb2
1170 // instruction encoding described above.
1171 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1172 extract_thumb_movw_movt_addend(
1173 typename elfcpp::Swap<32, big_endian>::Valtype val)
1174 {
1175 // According to the Elf ABI for ARM Architecture the immediate
1176 // field is sign-extended to form the addend.
1177 return utils::sign_extend<16>(((val >> 4) & 0xf000)
1178 | ((val >> 15) & 0x0800)
1179 | ((val >> 4) & 0x0700)
1180 | (val & 0x00ff));
1181 }
1182
1183 // Insert X into VAL based on the Thumb2 instruction encoding
1184 // described above.
1185 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1186 insert_val_thumb_movw_movt(
1187 typename elfcpp::Swap<32, big_endian>::Valtype val,
1188 typename elfcpp::Swap<32, big_endian>::Valtype x)
1189 {
1190 val &= 0xfbf08f00;
1191 val |= (x & 0xf000) << 4;
1192 val |= (x & 0x0800) << 15;
1193 val |= (x & 0x0700) << 4;
1194 val |= (x & 0x00ff);
1195 return val;
1196 }
1197
c121c671
DK
1198 // FIXME: This probably only works for Android on ARM v5te. We should
1199 // following GNU ld for the general case.
1200 template<unsigned r_type>
1201 static inline typename This::Status
1202 arm_branch_common(unsigned char *view,
1203 const Sized_relobj<32, big_endian>* object,
1204 const Symbol_value<32>* psymval,
1205 elfcpp::Elf_types<32>::Elf_Addr address,
1206 bool has_thumb_bit)
1207 {
1208 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1209 Valtype* wv = reinterpret_cast<Valtype*>(view);
1210 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1211
1212 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
1213 && ((val & 0x0f000000UL) == 0x0a000000UL);
1214 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
1215 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
1216 && ((val & 0x0f000000UL) == 0x0b000000UL);
1217 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
1218 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
1219
1220 if (r_type == elfcpp::R_ARM_CALL)
1221 {
1222 if (!insn_is_uncond_bl && !insn_is_blx)
1223 return This::STATUS_BAD_RELOC;
1224 }
1225 else if (r_type == elfcpp::R_ARM_JUMP24)
1226 {
1227 if (!insn_is_b && !insn_is_cond_bl)
1228 return This::STATUS_BAD_RELOC;
1229 }
1230 else if (r_type == elfcpp::R_ARM_PLT32)
1231 {
1232 if (!insn_is_any_branch)
1233 return This::STATUS_BAD_RELOC;
1234 }
1235 else
1236 gold_unreachable();
1237
1238 Valtype addend = utils::sign_extend<26>(val << 2);
1239 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1240 - address);
1241
1242 // If target has thumb bit set, we need to either turn the BL
1243 // into a BLX (for ARMv5 or above) or generate a stub.
1244 if (x & 1)
1245 {
1246 // Turn BL to BLX.
1247 if (insn_is_uncond_bl)
1248 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
1249 else
1250 return This::STATUS_BAD_RELOC;
1251 }
1252 else
1253 gold_assert(!insn_is_blx);
1254
1255 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
1256 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1257 return (utils::has_overflow<26>(x)
1258 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
1259 }
1260
1261 public:
5e445df6
ILT
1262
1263 // R_ARM_ABS8: S + A
1264 static inline typename This::Status
1265 abs8(unsigned char *view,
1266 const Sized_relobj<32, big_endian>* object,
be8fcb75 1267 const Symbol_value<32>* psymval)
5e445df6
ILT
1268 {
1269 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
1270 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1271 Valtype* wv = reinterpret_cast<Valtype*>(view);
1272 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
1273 Reltype addend = utils::sign_extend<8>(val);
be8fcb75 1274 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
5e445df6
ILT
1275 val = utils::bit_select(val, x, 0xffU);
1276 elfcpp::Swap<8, big_endian>::writeval(wv, val);
1277 return (utils::has_signed_unsigned_overflow<8>(x)
1278 ? This::STATUS_OVERFLOW
1279 : This::STATUS_OKAY);
1280 }
1281
be8fcb75
ILT
1282 // R_ARM_THM_ABS5: S + A
1283 static inline typename This::Status
1284 thm_abs5(unsigned char *view,
1285 const Sized_relobj<32, big_endian>* object,
1286 const Symbol_value<32>* psymval)
1287 {
1288 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1289 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1290 Valtype* wv = reinterpret_cast<Valtype*>(view);
1291 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1292 Reltype addend = (val & 0x7e0U) >> 6;
1293 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1294 val = utils::bit_select(val, x << 6, 0x7e0U);
1295 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1296 return (utils::has_overflow<5>(x)
1297 ? This::STATUS_OVERFLOW
1298 : This::STATUS_OKAY);
1299 }
1300
1301 // R_ARM_ABS12: S + A
1302 static inline typename This::Status
1303 abs12(unsigned char *view,
1304 const Sized_relobj<32, big_endian>* object,
1305 const Symbol_value<32>* psymval)
1306 {
1307 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1308 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1309 Valtype* wv = reinterpret_cast<Valtype*>(view);
1310 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1311 Reltype addend = val & 0x0fffU;
1312 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1313 val = utils::bit_select(val, x, 0x0fffU);
1314 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1315 return (utils::has_overflow<12>(x)
1316 ? This::STATUS_OVERFLOW
1317 : This::STATUS_OKAY);
1318 }
1319
1320 // R_ARM_ABS16: S + A
1321 static inline typename This::Status
1322 abs16(unsigned char *view,
1323 const Sized_relobj<32, big_endian>* object,
1324 const Symbol_value<32>* psymval)
1325 {
1326 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1327 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1328 Valtype* wv = reinterpret_cast<Valtype*>(view);
1329 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1330 Reltype addend = utils::sign_extend<16>(val);
1331 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1332 val = utils::bit_select(val, x, 0xffffU);
1333 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1334 return (utils::has_signed_unsigned_overflow<16>(x)
1335 ? This::STATUS_OVERFLOW
1336 : This::STATUS_OKAY);
1337 }
1338
c121c671
DK
1339 // R_ARM_ABS32: (S + A) | T
1340 static inline typename This::Status
1341 abs32(unsigned char *view,
1342 const Sized_relobj<32, big_endian>* object,
1343 const Symbol_value<32>* psymval,
1344 bool has_thumb_bit)
1345 {
1346 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1347 Valtype* wv = reinterpret_cast<Valtype*>(view);
1348 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1349 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1350 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1351 return This::STATUS_OKAY;
1352 }
1353
1354 // R_ARM_REL32: (S + A) | T - P
1355 static inline typename This::Status
1356 rel32(unsigned char *view,
1357 const Sized_relobj<32, big_endian>* object,
1358 const Symbol_value<32>* psymval,
1359 elfcpp::Elf_types<32>::Elf_Addr address,
1360 bool has_thumb_bit)
1361 {
1362 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1363 Valtype* wv = reinterpret_cast<Valtype*>(view);
1364 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1365 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1366 - address);
1367 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1368 return This::STATUS_OKAY;
1369 }
1370
1371 // R_ARM_THM_CALL: (S + A) | T - P
1372 static inline typename This::Status
1373 thm_call(unsigned char *view,
1374 const Sized_relobj<32, big_endian>* object,
1375 const Symbol_value<32>* psymval,
1376 elfcpp::Elf_types<32>::Elf_Addr address,
1377 bool has_thumb_bit)
1378 {
1379 // A thumb call consists of two instructions.
1380 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1381 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1382 Valtype* wv = reinterpret_cast<Valtype*>(view);
1383 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
1384 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
1385 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
1386 gold_assert((lo & 0xf800) == 0xf800);
1387 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
1388 | ((lo & 0x7ff) << 1));
1389 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1390 - address);
1391
1392 // If target has no thumb bit set, we need to either turn the BL
1393 // into a BLX (for ARMv5 or above) or generate a stub.
1394 if ((x & 1) == 0)
1395 {
1396 // This only works for ARMv5 and above with interworking enabled.
1397 lo &= 0xefff;
1398 }
1399 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
1400 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
1401 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
1402 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
1403 return (utils::has_overflow<23>(x)
1404 ? This::STATUS_OVERFLOW
1405 : This::STATUS_OKAY);
1406 }
1407
1408 // R_ARM_BASE_PREL: B(S) + A - P
1409 static inline typename This::Status
1410 base_prel(unsigned char* view,
1411 elfcpp::Elf_types<32>::Elf_Addr origin,
1412 elfcpp::Elf_types<32>::Elf_Addr address)
1413 {
1414 Base::rel32(view, origin - address);
1415 return STATUS_OKAY;
1416 }
1417
be8fcb75
ILT
1418 // R_ARM_BASE_ABS: B(S) + A
1419 static inline typename This::Status
1420 base_abs(unsigned char* view,
1421 elfcpp::Elf_types<32>::Elf_Addr origin)
1422 {
1423 Base::rel32(view, origin);
1424 return STATUS_OKAY;
1425 }
1426
c121c671
DK
1427 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1428 static inline typename This::Status
1429 got_brel(unsigned char* view,
1430 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
1431 {
1432 Base::rel32(view, got_offset);
1433 return This::STATUS_OKAY;
1434 }
1435
7f5309a5
ILT
1436 // R_ARM_GOT_PREL: GOT(S) + A – P
1437 static inline typename This::Status
1438 got_prel(unsigned char* view,
1439 typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
1440 elfcpp::Elf_types<32>::Elf_Addr address)
1441 {
1442 Base::rel32(view, got_offset - address);
1443 return This::STATUS_OKAY;
1444 }
1445
c121c671
DK
1446 // R_ARM_PLT32: (S + A) | T - P
1447 static inline typename This::Status
1448 plt32(unsigned char *view,
1449 const Sized_relobj<32, big_endian>* object,
1450 const Symbol_value<32>* psymval,
1451 elfcpp::Elf_types<32>::Elf_Addr address,
1452 bool has_thumb_bit)
1453 {
1454 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
1455 address, has_thumb_bit);
1456 }
1457
1458 // R_ARM_CALL: (S + A) | T - P
1459 static inline typename This::Status
1460 call(unsigned char *view,
1461 const Sized_relobj<32, big_endian>* object,
1462 const Symbol_value<32>* psymval,
1463 elfcpp::Elf_types<32>::Elf_Addr address,
1464 bool has_thumb_bit)
1465 {
1466 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
1467 address, has_thumb_bit);
1468 }
1469
1470 // R_ARM_JUMP24: (S + A) | T - P
1471 static inline typename This::Status
1472 jump24(unsigned char *view,
1473 const Sized_relobj<32, big_endian>* object,
1474 const Symbol_value<32>* psymval,
1475 elfcpp::Elf_types<32>::Elf_Addr address,
1476 bool has_thumb_bit)
1477 {
1478 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
1479 address, has_thumb_bit);
1480 }
1481
1482 // R_ARM_PREL: (S + A) | T - P
1483 static inline typename This::Status
1484 prel31(unsigned char *view,
1485 const Sized_relobj<32, big_endian>* object,
1486 const Symbol_value<32>* psymval,
1487 elfcpp::Elf_types<32>::Elf_Addr address,
1488 bool has_thumb_bit)
1489 {
1490 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1491 Valtype* wv = reinterpret_cast<Valtype*>(view);
1492 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1493 Valtype addend = utils::sign_extend<31>(val);
1494 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1495 - address);
1496 val = utils::bit_select(val, x, 0x7fffffffU);
1497 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1498 return (utils::has_overflow<31>(x) ?
1499 This::STATUS_OVERFLOW : This::STATUS_OKAY);
1500 }
fd3c5f0b
ILT
1501
1502 // R_ARM_MOVW_ABS_NC: (S + A) | T
1503 static inline typename This::Status
1504 movw_abs_nc(unsigned char *view,
1505 const Sized_relobj<32, big_endian>* object,
1506 const Symbol_value<32>* psymval,
1507 bool has_thumb_bit)
1508 {
1509 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1510 Valtype* wv = reinterpret_cast<Valtype*>(view);
1511 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1512 Valtype addend = This::extract_arm_movw_movt_addend(val);
1513 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1514 val = This::insert_val_arm_movw_movt(val, x);
1515 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1516 return This::STATUS_OKAY;
1517 }
1518
1519 // R_ARM_MOVT_ABS: S + A
1520 static inline typename This::Status
1521 movt_abs(unsigned char *view,
1522 const Sized_relobj<32, big_endian>* object,
1523 const Symbol_value<32>* psymval)
1524 {
1525 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1526 Valtype* wv = reinterpret_cast<Valtype*>(view);
1527 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1528 Valtype addend = This::extract_arm_movw_movt_addend(val);
1529 Valtype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
1530 val = This::insert_val_arm_movw_movt(val, x);
1531 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1532 return This::STATUS_OKAY;
1533 }
1534
1535 // R_ARM_THM_MOVW_ABS_NC: S + A | T
1536 static inline typename This::Status
1537 thm_movw_abs_nc(unsigned char *view,
1538 const Sized_relobj<32, big_endian>* object,
1539 const Symbol_value<32>* psymval,
1540 bool has_thumb_bit)
1541 {
1542 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1543 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1544 Valtype* wv = reinterpret_cast<Valtype*>(view);
1545 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1546 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
1547 Reltype addend = extract_thumb_movw_movt_addend(val);
1548 Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1549 val = This::insert_val_thumb_movw_movt(val, x);
1550 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1551 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1552 return This::STATUS_OKAY;
1553 }
1554
1555 // R_ARM_THM_MOVT_ABS: S + A
1556 static inline typename This::Status
1557 thm_movt_abs(unsigned char *view,
1558 const Sized_relobj<32, big_endian>* object,
1559 const Symbol_value<32>* psymval)
1560 {
1561 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1562 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1563 Valtype* wv = reinterpret_cast<Valtype*>(view);
1564 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1565 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
1566 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1567 Reltype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
1568 val = This::insert_val_thumb_movw_movt(val, x);
1569 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1570 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1571 return This::STATUS_OKAY;
1572 }
1573
c2a122b6
ILT
1574 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
1575 static inline typename This::Status
1576 movw_prel_nc(unsigned char *view,
1577 const Sized_relobj<32, big_endian>* object,
1578 const Symbol_value<32>* psymval,
1579 elfcpp::Elf_types<32>::Elf_Addr address,
1580 bool has_thumb_bit)
1581 {
1582 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1583 Valtype* wv = reinterpret_cast<Valtype*>(view);
1584 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1585 Valtype addend = This::extract_arm_movw_movt_addend(val);
1586 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1587 - address);
1588 val = This::insert_val_arm_movw_movt(val, x);
1589 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1590 return This::STATUS_OKAY;
1591 }
1592
1593 // R_ARM_MOVT_PREL: S + A - P
1594 static inline typename This::Status
1595 movt_prel(unsigned char *view,
1596 const Sized_relobj<32, big_endian>* object,
1597 const Symbol_value<32>* psymval,
1598 elfcpp::Elf_types<32>::Elf_Addr address)
1599 {
1600 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1601 Valtype* wv = reinterpret_cast<Valtype*>(view);
1602 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1603 Valtype addend = This::extract_arm_movw_movt_addend(val);
1604 Valtype x = (This::arm_symbol_value(object, psymval, addend, 0)
1605 - address) >> 16;
1606 val = This::insert_val_arm_movw_movt(val, x);
1607 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1608 return This::STATUS_OKAY;
1609 }
1610
1611 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
1612 static inline typename This::Status
1613 thm_movw_prel_nc(unsigned char *view,
1614 const Sized_relobj<32, big_endian>* object,
1615 const Symbol_value<32>* psymval,
1616 elfcpp::Elf_types<32>::Elf_Addr address,
1617 bool has_thumb_bit)
1618 {
1619 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1620 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1621 Valtype* wv = reinterpret_cast<Valtype*>(view);
1622 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1623 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1624 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1625 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1626 - address);
1627 val = This::insert_val_thumb_movw_movt(val, x);
1628 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1629 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1630 return This::STATUS_OKAY;
1631 }
1632
1633 // R_ARM_THM_MOVT_PREL: S + A - P
1634 static inline typename This::Status
1635 thm_movt_prel(unsigned char *view,
1636 const Sized_relobj<32, big_endian>* object,
1637 const Symbol_value<32>* psymval,
1638 elfcpp::Elf_types<32>::Elf_Addr address)
1639 {
1640 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1641 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1642 Valtype* wv = reinterpret_cast<Valtype*>(view);
1643 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1644 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1645 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1646 Reltype x = (This::arm_symbol_value(object, psymval, addend, 0)
1647 - address) >> 16;
1648 val = This::insert_val_thumb_movw_movt(val, x);
1649 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1650 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1651 return This::STATUS_OKAY;
1652 }
c121c671
DK
1653};
1654
94cdfcff
DK
1655// Get the GOT section, creating it if necessary.
1656
1657template<bool big_endian>
1658Output_data_got<32, big_endian>*
1659Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
1660{
1661 if (this->got_ == NULL)
1662 {
1663 gold_assert(symtab != NULL && layout != NULL);
1664
1665 this->got_ = new Output_data_got<32, big_endian>();
1666
1667 Output_section* os;
1668 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1669 (elfcpp::SHF_ALLOC
1670 | elfcpp::SHF_WRITE),
1671 this->got_);
1672 os->set_is_relro();
1673
1674 // The old GNU linker creates a .got.plt section. We just
1675 // create another set of data in the .got section. Note that we
1676 // always create a PLT if we create a GOT, although the PLT
1677 // might be empty.
1678 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
1679 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1680 (elfcpp::SHF_ALLOC
1681 | elfcpp::SHF_WRITE),
1682 this->got_plt_);
1683 os->set_is_relro();
1684
1685 // The first three entries are reserved.
1686 this->got_plt_->set_current_data_size(3 * 4);
1687
1688 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1689 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1690 this->got_plt_,
1691 0, 0, elfcpp::STT_OBJECT,
1692 elfcpp::STB_LOCAL,
1693 elfcpp::STV_HIDDEN, 0,
1694 false, false);
1695 }
1696 return this->got_;
1697}
1698
1699// Get the dynamic reloc section, creating it if necessary.
1700
1701template<bool big_endian>
1702typename Target_arm<big_endian>::Reloc_section*
1703Target_arm<big_endian>::rel_dyn_section(Layout* layout)
1704{
1705 if (this->rel_dyn_ == NULL)
1706 {
1707 gold_assert(layout != NULL);
1708 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
1709 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1710 elfcpp::SHF_ALLOC, this->rel_dyn_);
1711 }
1712 return this->rel_dyn_;
1713}
1714
b569affa
DK
1715// Insn_template methods.
1716
1717// Return byte size of an instruction template.
1718
1719size_t
1720Insn_template::size() const
1721{
1722 switch (this->type())
1723 {
1724 case THUMB16_TYPE:
1725 return 2;
1726 case ARM_TYPE:
1727 case THUMB32_TYPE:
1728 case DATA_TYPE:
1729 return 4;
1730 default:
1731 gold_unreachable();
1732 }
1733}
1734
1735// Return alignment of an instruction template.
1736
1737unsigned
1738Insn_template::alignment() const
1739{
1740 switch (this->type())
1741 {
1742 case THUMB16_TYPE:
1743 case THUMB32_TYPE:
1744 return 2;
1745 case ARM_TYPE:
1746 case DATA_TYPE:
1747 return 4;
1748 default:
1749 gold_unreachable();
1750 }
1751}
1752
1753// Stub_template methods.
1754
1755Stub_template::Stub_template(
1756 Stub_type type, const Insn_template* insns,
1757 size_t insn_count)
1758 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
1759 entry_in_thumb_mode_(false), relocs_()
1760{
1761 off_t offset = 0;
1762
1763 // Compute byte size and alignment of stub template.
1764 for (size_t i = 0; i < insn_count; i++)
1765 {
1766 unsigned insn_alignment = insns[i].alignment();
1767 size_t insn_size = insns[i].size();
1768 gold_assert((offset & (insn_alignment - 1)) == 0);
1769 this->alignment_ = std::max(this->alignment_, insn_alignment);
1770 switch (insns[i].type())
1771 {
1772 case Insn_template::THUMB16_TYPE:
1773 if (i == 0)
1774 this->entry_in_thumb_mode_ = true;
1775 break;
1776
1777 case Insn_template::THUMB32_TYPE:
1778 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
1779 this->relocs_.push_back(Reloc(i, offset));
1780 if (i == 0)
1781 this->entry_in_thumb_mode_ = true;
1782 break;
1783
1784 case Insn_template::ARM_TYPE:
1785 // Handle cases where the target is encoded within the
1786 // instruction.
1787 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
1788 this->relocs_.push_back(Reloc(i, offset));
1789 break;
1790
1791 case Insn_template::DATA_TYPE:
1792 // Entry point cannot be data.
1793 gold_assert(i != 0);
1794 this->relocs_.push_back(Reloc(i, offset));
1795 break;
1796
1797 default:
1798 gold_unreachable();
1799 }
1800 offset += insn_size;
1801 }
1802 this->size_ = offset;
1803}
1804
1805// Reloc_stub::Key methods.
1806
1807// Dump a Key as a string for debugging.
1808
1809std::string
1810Reloc_stub::Key::name() const
1811{
1812 if (this->r_sym_ == invalid_index)
1813 {
1814 // Global symbol key name
1815 // <stub-type>:<symbol name>:<addend>.
1816 const std::string sym_name = this->u_.symbol->name();
1817 // We need to print two hex number and two colons. So just add 100 bytes
1818 // to the symbol name size.
1819 size_t len = sym_name.size() + 100;
1820 char* buffer = new char[len];
1821 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
1822 sym_name.c_str(), this->addend_);
1823 gold_assert(c > 0 && c < static_cast<int>(len));
1824 delete[] buffer;
1825 return std::string(buffer);
1826 }
1827 else
1828 {
1829 // local symbol key name
1830 // <stub-type>:<object>:<r_sym>:<addend>.
1831 const size_t len = 200;
1832 char buffer[len];
1833 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
1834 this->u_.relobj, this->r_sym_, this->addend_);
1835 gold_assert(c > 0 && c < static_cast<int>(len));
1836 return std::string(buffer);
1837 }
1838}
1839
1840// Reloc_stub methods.
1841
1842// Determine the type of stub needed, if any, for a relocation of R_TYPE at
1843// LOCATION to DESTINATION.
1844// This code is based on the arm_type_of_stub function in
1845// bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
1846// class simple.
1847
1848Stub_type
1849Reloc_stub::stub_type_for_reloc(
1850 unsigned int r_type,
1851 Arm_address location,
1852 Arm_address destination,
1853 bool target_is_thumb)
1854{
1855 Stub_type stub_type = arm_stub_none;
1856
1857 // This is a bit ugly but we want to avoid using a templated class for
1858 // big and little endianities.
1859 bool may_use_blx;
1860 bool should_force_pic_veneer;
1861 bool thumb2;
1862 bool thumb_only;
1863 if (parameters->target().is_big_endian())
1864 {
1865 const Target_arm<true>& big_endian_target =
1866 Target_arm<true>::default_target();
1867 may_use_blx = big_endian_target.may_use_blx();
1868 should_force_pic_veneer = big_endian_target.should_force_pic_veneer();
1869 thumb2 = big_endian_target.using_thumb2();
1870 thumb_only = big_endian_target.using_thumb_only();
1871 }
1872 else
1873 {
1874 const Target_arm<false>& little_endian_target =
1875 Target_arm<false>::default_target();
1876 may_use_blx = little_endian_target.may_use_blx();
1877 should_force_pic_veneer = little_endian_target.should_force_pic_veneer();
1878 thumb2 = little_endian_target.using_thumb2();
1879 thumb_only = little_endian_target.using_thumb_only();
1880 }
1881
1882 int64_t branch_offset = (int64_t)destination - location;
1883
1884 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
1885 {
1886 // Handle cases where:
1887 // - this call goes too far (different Thumb/Thumb2 max
1888 // distance)
1889 // - it's a Thumb->Arm call and blx is not available, or it's a
1890 // Thumb->Arm branch (not bl). A stub is needed in this case.
1891 if ((!thumb2
1892 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
1893 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
1894 || (thumb2
1895 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
1896 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
1897 || ((!target_is_thumb)
1898 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
1899 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
1900 {
1901 if (target_is_thumb)
1902 {
1903 // Thumb to thumb.
1904 if (!thumb_only)
1905 {
1906 stub_type = (parameters->options().shared() | should_force_pic_veneer)
1907 // PIC stubs.
1908 ? ((may_use_blx
1909 && (r_type == elfcpp::R_ARM_THM_CALL))
1910 // V5T and above. Stub starts with ARM code, so
1911 // we must be able to switch mode before
1912 // reaching it, which is only possible for 'bl'
1913 // (ie R_ARM_THM_CALL relocation).
1914 ? arm_stub_long_branch_any_thumb_pic
1915 // On V4T, use Thumb code only.
1916 : arm_stub_long_branch_v4t_thumb_thumb_pic)
1917
1918 // non-PIC stubs.
1919 : ((may_use_blx
1920 && (r_type == elfcpp::R_ARM_THM_CALL))
1921 ? arm_stub_long_branch_any_any // V5T and above.
1922 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
1923 }
1924 else
1925 {
1926 stub_type = (parameters->options().shared() | should_force_pic_veneer)
1927 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
1928 : arm_stub_long_branch_thumb_only; // non-PIC stub.
1929 }
1930 }
1931 else
1932 {
1933 // Thumb to arm.
1934
1935 // FIXME: We should check that the input section is from an
1936 // object that has interwork enabled.
1937
1938 stub_type = (parameters->options().shared()
1939 || should_force_pic_veneer)
1940 // PIC stubs.
1941 ? ((may_use_blx
1942 && (r_type == elfcpp::R_ARM_THM_CALL))
1943 ? arm_stub_long_branch_any_arm_pic // V5T and above.
1944 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
1945
1946 // non-PIC stubs.
1947 : ((may_use_blx
1948 && (r_type == elfcpp::R_ARM_THM_CALL))
1949 ? arm_stub_long_branch_any_any // V5T and above.
1950 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
1951
1952 // Handle v4t short branches.
1953 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
1954 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
1955 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
1956 stub_type = arm_stub_short_branch_v4t_thumb_arm;
1957 }
1958 }
1959 }
1960 else if (r_type == elfcpp::R_ARM_CALL
1961 || r_type == elfcpp::R_ARM_JUMP24
1962 || r_type == elfcpp::R_ARM_PLT32)
1963 {
1964 if (target_is_thumb)
1965 {
1966 // Arm to thumb.
1967
1968 // FIXME: We should check that the input section is from an
1969 // object that has interwork enabled.
1970
1971 // We have an extra 2-bytes reach because of
1972 // the mode change (bit 24 (H) of BLX encoding).
1973 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
1974 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
1975 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
1976 || (r_type == elfcpp::R_ARM_JUMP24)
1977 || (r_type == elfcpp::R_ARM_PLT32))
1978 {
1979 stub_type = (parameters->options().shared()
1980 || should_force_pic_veneer)
1981 // PIC stubs.
1982 ? (may_use_blx
1983 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
1984 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
1985
1986 // non-PIC stubs.
1987 : (may_use_blx
1988 ? arm_stub_long_branch_any_any // V5T and above.
1989 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
1990 }
1991 }
1992 else
1993 {
1994 // Arm to arm.
1995 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
1996 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
1997 {
1998 stub_type = (parameters->options().shared()
1999 || should_force_pic_veneer)
2000 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
2001 : arm_stub_long_branch_any_any; /// non-PIC.
2002 }
2003 }
2004 }
2005
2006 return stub_type;
2007}
2008
2009// Template to implement do_write for a specific target endianity.
2010
2011template<bool big_endian>
2012void inline
2013Reloc_stub::do_fixed_endian_write(unsigned char* view,
2014 section_size_type view_size)
2015{
2016 const Stub_template* stub_template = this->stub_template();
2017 const Insn_template* insns = stub_template->insns();
2018
2019 // FIXME: We do not handle BE8 encoding yet.
2020 unsigned char* pov = view;
2021 for (size_t i = 0; i < stub_template->insn_count(); i++)
2022 {
2023 switch (insns[i].type())
2024 {
2025 case Insn_template::THUMB16_TYPE:
2026 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2027 gold_assert(insns[i].reloc_addend() == 0);
2028 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2029 break;
2030 case Insn_template::THUMB32_TYPE:
2031 {
2032 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2033 uint32_t lo = insns[i].data() & 0xffff;
2034 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2035 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2036 }
2037 break;
2038 case Insn_template::ARM_TYPE:
2039 case Insn_template::DATA_TYPE:
2040 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2041 break;
2042 default:
2043 gold_unreachable();
2044 }
2045 pov += insns[i].size();
2046 }
2047 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2048}
2049
2050// Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2051
2052void
2053Reloc_stub::do_write(unsigned char* view, section_size_type view_size,
2054 bool big_endian)
2055{
2056 if (big_endian)
2057 this->do_fixed_endian_write<true>(view, view_size);
2058 else
2059 this->do_fixed_endian_write<false>(view, view_size);
2060}
2061
2062// Stub_factory methods.
2063
2064Stub_factory::Stub_factory()
2065{
2066 // The instruction template sequences are declared as static
2067 // objects and initialized first time the constructor runs.
2068
2069 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2070 // to reach the stub if necessary.
2071 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
2072 {
2073 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2074 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2075 // dcd R_ARM_ABS32(X)
2076 };
2077
2078 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2079 // available.
2080 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2081 {
2082 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2083 Insn_template::arm_insn(0xe12fff1c), // bx ip
2084 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2085 // dcd R_ARM_ABS32(X)
2086 };
2087
2088 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2089 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
2090 {
2091 Insn_template::thumb16_insn(0xb401), // push {r0}
2092 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2093 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2094 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2095 Insn_template::thumb16_insn(0x4760), // bx ip
2096 Insn_template::thumb16_insn(0xbf00), // nop
2097 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2098 // dcd R_ARM_ABS32(X)
2099 };
2100
2101 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2102 // allowed.
2103 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2104 {
2105 Insn_template::thumb16_insn(0x4778), // bx pc
2106 Insn_template::thumb16_insn(0x46c0), // nop
2107 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2108 Insn_template::arm_insn(0xe12fff1c), // bx ip
2109 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2110 // dcd R_ARM_ABS32(X)
2111 };
2112
2113 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2114 // available.
2115 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2116 {
2117 Insn_template::thumb16_insn(0x4778), // bx pc
2118 Insn_template::thumb16_insn(0x46c0), // nop
2119 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2120 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2121 // dcd R_ARM_ABS32(X)
2122 };
2123
2124 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2125 // one, when the destination is close enough.
2126 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2127 {
2128 Insn_template::thumb16_insn(0x4778), // bx pc
2129 Insn_template::thumb16_insn(0x46c0), // nop
2130 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2131 };
2132
2133 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2134 // blx to reach the stub if necessary.
2135 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
2136 {
2137 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2138 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2139 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2140 // dcd R_ARM_REL32(X-4)
2141 };
2142
2143 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2144 // blx to reach the stub if necessary. We can not add into pc;
2145 // it is not guaranteed to mode switch (different in ARMv6 and
2146 // ARMv7).
2147 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
2148 {
2149 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2150 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2151 Insn_template::arm_insn(0xe12fff1c), // bx ip
2152 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2153 // dcd R_ARM_REL32(X)
2154 };
2155
2156 // V4T ARM -> ARM long branch stub, PIC.
2157 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2158 {
2159 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2160 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2161 Insn_template::arm_insn(0xe12fff1c), // bx ip
2162 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2163 // dcd R_ARM_REL32(X)
2164 };
2165
2166 // V4T Thumb -> ARM long branch stub, PIC.
2167 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2168 {
2169 Insn_template::thumb16_insn(0x4778), // bx pc
2170 Insn_template::thumb16_insn(0x46c0), // nop
2171 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2172 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
2173 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2174 // dcd R_ARM_REL32(X)
2175 };
2176
2177 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
2178 // architectures.
2179 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
2180 {
2181 Insn_template::thumb16_insn(0xb401), // push {r0}
2182 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2183 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
2184 Insn_template::thumb16_insn(0x4484), // add ip, r0
2185 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2186 Insn_template::thumb16_insn(0x4760), // bx ip
2187 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
2188 // dcd R_ARM_REL32(X)
2189 };
2190
2191 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2192 // allowed.
2193 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2194 {
2195 Insn_template::thumb16_insn(0x4778), // bx pc
2196 Insn_template::thumb16_insn(0x46c0), // nop
2197 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2198 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2199 Insn_template::arm_insn(0xe12fff1c), // bx ip
2200 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2201 // dcd R_ARM_REL32(X)
2202 };
2203
2204 // Cortex-A8 erratum-workaround stubs.
2205
2206 // Stub used for conditional branches (which may be beyond +/-1MB away,
2207 // so we can't use a conditional branch to reach this stub).
2208
2209 // original code:
2210 //
2211 // b<cond> X
2212 // after:
2213 //
2214 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
2215 {
2216 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
2217 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
2218 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
2219 // b.w X
2220 };
2221
2222 // Stub used for b.w and bl.w instructions.
2223
2224 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
2225 {
2226 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2227 };
2228
2229 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
2230 {
2231 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2232 };
2233
2234 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
2235 // instruction (which switches to ARM mode) to point to this stub. Jump to
2236 // the real destination using an ARM-mode branch.
2237 const Insn_template elf32_arm_stub_a8_veneer_blx[] =
2238 {
2239 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
2240 };
2241
2242 // Fill in the stub template look-up table. Stub templates are constructed
2243 // per instance of Stub_factory for fast look-up without locking
2244 // in a thread-enabled environment.
2245
2246 this->stub_templates_[arm_stub_none] =
2247 new Stub_template(arm_stub_none, NULL, 0);
2248
2249#define DEF_STUB(x) \
2250 do \
2251 { \
2252 size_t array_size \
2253 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
2254 Stub_type type = arm_stub_##x; \
2255 this->stub_templates_[type] = \
2256 new Stub_template(type, elf32_arm_stub_##x, array_size); \
2257 } \
2258 while (0);
2259
2260 DEF_STUBS
2261#undef DEF_STUB
2262}
2263
94cdfcff
DK
2264// A class to handle the PLT data.
2265
2266template<bool big_endian>
2267class Output_data_plt_arm : public Output_section_data
2268{
2269 public:
2270 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
2271 Reloc_section;
2272
2273 Output_data_plt_arm(Layout*, Output_data_space*);
2274
2275 // Add an entry to the PLT.
2276 void
2277 add_entry(Symbol* gsym);
2278
2279 // Return the .rel.plt section data.
2280 const Reloc_section*
2281 rel_plt() const
2282 { return this->rel_; }
2283
2284 protected:
2285 void
2286 do_adjust_output_section(Output_section* os);
2287
2288 // Write to a map file.
2289 void
2290 do_print_to_mapfile(Mapfile* mapfile) const
2291 { mapfile->print_output_data(this, _("** PLT")); }
2292
2293 private:
2294 // Template for the first PLT entry.
2295 static const uint32_t first_plt_entry[5];
2296
2297 // Template for subsequent PLT entries.
2298 static const uint32_t plt_entry[3];
2299
2300 // Set the final size.
2301 void
2302 set_final_data_size()
2303 {
2304 this->set_data_size(sizeof(first_plt_entry)
2305 + this->count_ * sizeof(plt_entry));
2306 }
2307
2308 // Write out the PLT data.
2309 void
2310 do_write(Output_file*);
2311
2312 // The reloc section.
2313 Reloc_section* rel_;
2314 // The .got.plt section.
2315 Output_data_space* got_plt_;
2316 // The number of PLT entries.
2317 unsigned int count_;
2318};
2319
2320// Create the PLT section. The ordinary .got section is an argument,
2321// since we need to refer to the start. We also create our own .got
2322// section just for PLT entries.
2323
2324template<bool big_endian>
2325Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
2326 Output_data_space* got_plt)
2327 : Output_section_data(4), got_plt_(got_plt), count_(0)
2328{
2329 this->rel_ = new Reloc_section(false);
2330 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2331 elfcpp::SHF_ALLOC, this->rel_);
2332}
2333
2334template<bool big_endian>
2335void
2336Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
2337{
2338 os->set_entsize(0);
2339}
2340
2341// Add an entry to the PLT.
2342
2343template<bool big_endian>
2344void
2345Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
2346{
2347 gold_assert(!gsym->has_plt_offset());
2348
2349 // Note that when setting the PLT offset we skip the initial
2350 // reserved PLT entry.
2351 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
2352 + sizeof(first_plt_entry));
2353
2354 ++this->count_;
2355
2356 section_offset_type got_offset = this->got_plt_->current_data_size();
2357
2358 // Every PLT entry needs a GOT entry which points back to the PLT
2359 // entry (this will be changed by the dynamic linker, normally
2360 // lazily when the function is called).
2361 this->got_plt_->set_current_data_size(got_offset + 4);
2362
2363 // Every PLT entry needs a reloc.
2364 gsym->set_needs_dynsym_entry();
2365 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
2366 got_offset);
2367
2368 // Note that we don't need to save the symbol. The contents of the
2369 // PLT are independent of which symbols are used. The symbols only
2370 // appear in the relocations.
2371}
2372
2373// ARM PLTs.
2374// FIXME: This is not very flexible. Right now this has only been tested
2375// on armv5te. If we are to support additional architecture features like
2376// Thumb-2 or BE8, we need to make this more flexible like GNU ld.
2377
2378// The first entry in the PLT.
2379template<bool big_endian>
2380const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
2381{
2382 0xe52de004, // str lr, [sp, #-4]!
2383 0xe59fe004, // ldr lr, [pc, #4]
2384 0xe08fe00e, // add lr, pc, lr
2385 0xe5bef008, // ldr pc, [lr, #8]!
2386 0x00000000, // &GOT[0] - .
2387};
2388
2389// Subsequent entries in the PLT.
2390
2391template<bool big_endian>
2392const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
2393{
2394 0xe28fc600, // add ip, pc, #0xNN00000
2395 0xe28cca00, // add ip, ip, #0xNN000
2396 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
2397};
2398
2399// Write out the PLT. This uses the hand-coded instructions above,
2400// and adjusts them as needed. This is all specified by the arm ELF
2401// Processor Supplement.
2402
2403template<bool big_endian>
2404void
2405Output_data_plt_arm<big_endian>::do_write(Output_file* of)
2406{
2407 const off_t offset = this->offset();
2408 const section_size_type oview_size =
2409 convert_to_section_size_type(this->data_size());
2410 unsigned char* const oview = of->get_output_view(offset, oview_size);
2411
2412 const off_t got_file_offset = this->got_plt_->offset();
2413 const section_size_type got_size =
2414 convert_to_section_size_type(this->got_plt_->data_size());
2415 unsigned char* const got_view = of->get_output_view(got_file_offset,
2416 got_size);
2417 unsigned char* pov = oview;
2418
2419 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
2420 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
2421
2422 // Write first PLT entry. All but the last word are constants.
2423 const size_t num_first_plt_words = (sizeof(first_plt_entry)
2424 / sizeof(plt_entry[0]));
2425 for (size_t i = 0; i < num_first_plt_words - 1; i++)
2426 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
2427 // Last word in first PLT entry is &GOT[0] - .
2428 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
2429 got_address - (plt_address + 16));
2430 pov += sizeof(first_plt_entry);
2431
2432 unsigned char* got_pov = got_view;
2433
2434 memset(got_pov, 0, 12);
2435 got_pov += 12;
2436
2437 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
2438 unsigned int plt_offset = sizeof(first_plt_entry);
2439 unsigned int plt_rel_offset = 0;
2440 unsigned int got_offset = 12;
2441 const unsigned int count = this->count_;
2442 for (unsigned int i = 0;
2443 i < count;
2444 ++i,
2445 pov += sizeof(plt_entry),
2446 got_pov += 4,
2447 plt_offset += sizeof(plt_entry),
2448 plt_rel_offset += rel_size,
2449 got_offset += 4)
2450 {
2451 // Set and adjust the PLT entry itself.
2452 int32_t offset = ((got_address + got_offset)
2453 - (plt_address + plt_offset + 8));
2454
2455 gold_assert(offset >= 0 && offset < 0x0fffffff);
2456 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
2457 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
2458 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
2459 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
2460 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
2461 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
2462
2463 // Set the entry in the GOT.
2464 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
2465 }
2466
2467 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2468 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2469
2470 of->write_output_view(offset, oview_size, oview);
2471 of->write_output_view(got_file_offset, got_size, got_view);
2472}
2473
2474// Create a PLT entry for a global symbol.
2475
2476template<bool big_endian>
2477void
2478Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
2479 Symbol* gsym)
2480{
2481 if (gsym->has_plt_offset())
2482 return;
2483
2484 if (this->plt_ == NULL)
2485 {
2486 // Create the GOT sections first.
2487 this->got_section(symtab, layout);
2488
2489 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
2490 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2491 (elfcpp::SHF_ALLOC
2492 | elfcpp::SHF_EXECINSTR),
2493 this->plt_);
2494 }
2495 this->plt_->add_entry(gsym);
2496}
2497
4a657b0d
DK
2498// Report an unsupported relocation against a local symbol.
2499
2500template<bool big_endian>
2501void
2502Target_arm<big_endian>::Scan::unsupported_reloc_local(
2503 Sized_relobj<32, big_endian>* object,
2504 unsigned int r_type)
2505{
2506 gold_error(_("%s: unsupported reloc %u against local symbol"),
2507 object->name().c_str(), r_type);
2508}
2509
bec53400
DK
2510// We are about to emit a dynamic relocation of type R_TYPE. If the
2511// dynamic linker does not support it, issue an error. The GNU linker
2512// only issues a non-PIC error for an allocated read-only section.
2513// Here we know the section is allocated, but we don't know that it is
2514// read-only. But we check for all the relocation types which the
2515// glibc dynamic linker supports, so it seems appropriate to issue an
2516// error even if the section is not read-only.
2517
2518template<bool big_endian>
2519void
2520Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
2521 unsigned int r_type)
2522{
2523 switch (r_type)
2524 {
2525 // These are the relocation types supported by glibc for ARM.
2526 case elfcpp::R_ARM_RELATIVE:
2527 case elfcpp::R_ARM_COPY:
2528 case elfcpp::R_ARM_GLOB_DAT:
2529 case elfcpp::R_ARM_JUMP_SLOT:
2530 case elfcpp::R_ARM_ABS32:
be8fcb75 2531 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
2532 case elfcpp::R_ARM_PC24:
2533 // FIXME: The following 3 types are not supported by Android's dynamic
2534 // linker.
2535 case elfcpp::R_ARM_TLS_DTPMOD32:
2536 case elfcpp::R_ARM_TLS_DTPOFF32:
2537 case elfcpp::R_ARM_TLS_TPOFF32:
2538 return;
2539
2540 default:
2541 // This prevents us from issuing more than one error per reloc
2542 // section. But we can still wind up issuing more than one
2543 // error per object file.
2544 if (this->issued_non_pic_error_)
2545 return;
2546 object->error(_("requires unsupported dynamic reloc; "
2547 "recompile with -fPIC"));
2548 this->issued_non_pic_error_ = true;
2549 return;
2550
2551 case elfcpp::R_ARM_NONE:
2552 gold_unreachable();
2553 }
2554}
2555
4a657b0d 2556// Scan a relocation for a local symbol.
bec53400
DK
2557// FIXME: This only handles a subset of relocation types used by Android
2558// on ARM v5te devices.
4a657b0d
DK
2559
2560template<bool big_endian>
2561inline void
2562Target_arm<big_endian>::Scan::local(const General_options&,
bec53400
DK
2563 Symbol_table* symtab,
2564 Layout* layout,
2565 Target_arm* target,
4a657b0d 2566 Sized_relobj<32, big_endian>* object,
bec53400
DK
2567 unsigned int data_shndx,
2568 Output_section* output_section,
2569 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
2570 unsigned int r_type,
2571 const elfcpp::Sym<32, big_endian>&)
2572{
2573 r_type = get_real_reloc_type(r_type);
2574 switch (r_type)
2575 {
2576 case elfcpp::R_ARM_NONE:
2577 break;
2578
bec53400 2579 case elfcpp::R_ARM_ABS32:
be8fcb75 2580 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
2581 // If building a shared library (or a position-independent
2582 // executable), we need to create a dynamic relocation for
2583 // this location. The relocation applied at link time will
2584 // apply the link-time value, so we flag the location with
2585 // an R_ARM_RELATIVE relocation so the dynamic loader can
2586 // relocate it easily.
2587 if (parameters->options().output_is_position_independent())
2588 {
2589 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2590 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
2591 // If we are to add more other reloc types than R_ARM_ABS32,
2592 // we need to add check_non_pic(object, r_type) here.
2593 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
2594 output_section, data_shndx,
2595 reloc.get_r_offset());
2596 }
2597 break;
2598
2599 case elfcpp::R_ARM_REL32:
2600 case elfcpp::R_ARM_THM_CALL:
2601 case elfcpp::R_ARM_CALL:
2602 case elfcpp::R_ARM_PREL31:
2603 case elfcpp::R_ARM_JUMP24:
2604 case elfcpp::R_ARM_PLT32:
be8fcb75
ILT
2605 case elfcpp::R_ARM_THM_ABS5:
2606 case elfcpp::R_ARM_ABS8:
2607 case elfcpp::R_ARM_ABS12:
2608 case elfcpp::R_ARM_ABS16:
2609 case elfcpp::R_ARM_BASE_ABS:
fd3c5f0b
ILT
2610 case elfcpp::R_ARM_MOVW_ABS_NC:
2611 case elfcpp::R_ARM_MOVT_ABS:
2612 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
2613 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
2614 case elfcpp::R_ARM_MOVW_PREL_NC:
2615 case elfcpp::R_ARM_MOVT_PREL:
2616 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
2617 case elfcpp::R_ARM_THM_MOVT_PREL:
bec53400
DK
2618 break;
2619
2620 case elfcpp::R_ARM_GOTOFF32:
2621 // We need a GOT section:
2622 target->got_section(symtab, layout);
2623 break;
2624
2625 case elfcpp::R_ARM_BASE_PREL:
2626 // FIXME: What about this?
2627 break;
2628
2629 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 2630 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
2631 {
2632 // The symbol requires a GOT entry.
2633 Output_data_got<32, big_endian>* got =
2634 target->got_section(symtab, layout);
2635 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
2636 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
2637 {
2638 // If we are generating a shared object, we need to add a
2639 // dynamic RELATIVE relocation for this symbol's GOT entry.
2640 if (parameters->options().output_is_position_independent())
2641 {
2642 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2643 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
2644 rel_dyn->add_local_relative(
2645 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
2646 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
2647 }
2648 }
2649 }
2650 break;
2651
2652 case elfcpp::R_ARM_TARGET1:
2653 // This should have been mapped to another type already.
2654 // Fall through.
2655 case elfcpp::R_ARM_COPY:
2656 case elfcpp::R_ARM_GLOB_DAT:
2657 case elfcpp::R_ARM_JUMP_SLOT:
2658 case elfcpp::R_ARM_RELATIVE:
2659 // These are relocations which should only be seen by the
2660 // dynamic linker, and should never be seen here.
2661 gold_error(_("%s: unexpected reloc %u in object file"),
2662 object->name().c_str(), r_type);
2663 break;
2664
4a657b0d
DK
2665 default:
2666 unsupported_reloc_local(object, r_type);
2667 break;
2668 }
2669}
2670
2671// Report an unsupported relocation against a global symbol.
2672
2673template<bool big_endian>
2674void
2675Target_arm<big_endian>::Scan::unsupported_reloc_global(
2676 Sized_relobj<32, big_endian>* object,
2677 unsigned int r_type,
2678 Symbol* gsym)
2679{
2680 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2681 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2682}
2683
2684// Scan a relocation for a global symbol.
bec53400
DK
2685// FIXME: This only handles a subset of relocation types used by Android
2686// on ARM v5te devices.
4a657b0d
DK
2687
2688template<bool big_endian>
2689inline void
2690Target_arm<big_endian>::Scan::global(const General_options&,
bec53400
DK
2691 Symbol_table* symtab,
2692 Layout* layout,
2693 Target_arm* target,
4a657b0d 2694 Sized_relobj<32, big_endian>* object,
bec53400
DK
2695 unsigned int data_shndx,
2696 Output_section* output_section,
2697 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
2698 unsigned int r_type,
2699 Symbol* gsym)
2700{
2701 r_type = get_real_reloc_type(r_type);
2702 switch (r_type)
2703 {
2704 case elfcpp::R_ARM_NONE:
2705 break;
2706
bec53400 2707 case elfcpp::R_ARM_ABS32:
be8fcb75 2708 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
2709 {
2710 // Make a dynamic relocation if necessary.
2711 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
2712 {
2713 if (target->may_need_copy_reloc(gsym))
2714 {
2715 target->copy_reloc(symtab, layout, object,
2716 data_shndx, output_section, gsym, reloc);
2717 }
2718 else if (gsym->can_use_relative_reloc(false))
2719 {
2720 // If we are to add more other reloc types than R_ARM_ABS32,
2721 // we need to add check_non_pic(object, r_type) here.
2722 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2723 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
2724 output_section, object,
2725 data_shndx, reloc.get_r_offset());
2726 }
2727 else
2728 {
2729 // If we are to add more other reloc types than R_ARM_ABS32,
2730 // we need to add check_non_pic(object, r_type) here.
2731 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2732 rel_dyn->add_global(gsym, r_type, output_section, object,
2733 data_shndx, reloc.get_r_offset());
2734 }
2735 }
2736 }
2737 break;
2738
fd3c5f0b
ILT
2739 case elfcpp::R_ARM_MOVW_ABS_NC:
2740 case elfcpp::R_ARM_MOVT_ABS:
2741 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
2742 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
2743 case elfcpp::R_ARM_MOVW_PREL_NC:
2744 case elfcpp::R_ARM_MOVT_PREL:
2745 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
2746 case elfcpp::R_ARM_THM_MOVT_PREL:
fd3c5f0b
ILT
2747 break;
2748
be8fcb75
ILT
2749 case elfcpp::R_ARM_THM_ABS5:
2750 case elfcpp::R_ARM_ABS8:
2751 case elfcpp::R_ARM_ABS12:
2752 case elfcpp::R_ARM_ABS16:
2753 case elfcpp::R_ARM_BASE_ABS:
2754 {
2755 // No dynamic relocs of this kinds.
2756 // Report the error in case of PIC.
2757 int flags = Symbol::NON_PIC_REF;
2758 if (gsym->type() == elfcpp::STT_FUNC
2759 || gsym->type() == elfcpp::STT_ARM_TFUNC)
2760 flags |= Symbol::FUNCTION_CALL;
2761 if (gsym->needs_dynamic_reloc(flags))
2762 check_non_pic(object, r_type);
2763 }
2764 break;
2765
bec53400
DK
2766 case elfcpp::R_ARM_REL32:
2767 case elfcpp::R_ARM_PREL31:
2768 {
2769 // Make a dynamic relocation if necessary.
2770 int flags = Symbol::NON_PIC_REF;
2771 if (gsym->needs_dynamic_reloc(flags))
2772 {
2773 if (target->may_need_copy_reloc(gsym))
2774 {
2775 target->copy_reloc(symtab, layout, object,
2776 data_shndx, output_section, gsym, reloc);
2777 }
2778 else
2779 {
2780 check_non_pic(object, r_type);
2781 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2782 rel_dyn->add_global(gsym, r_type, output_section, object,
2783 data_shndx, reloc.get_r_offset());
2784 }
2785 }
2786 }
2787 break;
2788
2789 case elfcpp::R_ARM_JUMP24:
2790 case elfcpp::R_ARM_THM_CALL:
2791 case elfcpp::R_ARM_CALL:
2792 {
2793 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
2794 target->make_plt_entry(symtab, layout, gsym);
2795 // Make a dynamic relocation if necessary.
2796 int flags = Symbol::NON_PIC_REF;
2797 if (gsym->type() == elfcpp::STT_FUNC
07800fab 2798 || gsym->type() == elfcpp::STT_ARM_TFUNC)
bec53400
DK
2799 flags |= Symbol::FUNCTION_CALL;
2800 if (gsym->needs_dynamic_reloc(flags))
2801 {
2802 if (target->may_need_copy_reloc(gsym))
2803 {
2804 target->copy_reloc(symtab, layout, object,
2805 data_shndx, output_section, gsym,
2806 reloc);
2807 }
2808 else
2809 {
2810 check_non_pic(object, r_type);
2811 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2812 rel_dyn->add_global(gsym, r_type, output_section, object,
2813 data_shndx, reloc.get_r_offset());
2814 }
2815 }
2816 }
2817 break;
2818
2819 case elfcpp::R_ARM_PLT32:
2820 // If the symbol is fully resolved, this is just a relative
2821 // local reloc. Otherwise we need a PLT entry.
2822 if (gsym->final_value_is_known())
2823 break;
2824 // If building a shared library, we can also skip the PLT entry
2825 // if the symbol is defined in the output file and is protected
2826 // or hidden.
2827 if (gsym->is_defined()
2828 && !gsym->is_from_dynobj()
2829 && !gsym->is_preemptible())
2830 break;
2831 target->make_plt_entry(symtab, layout, gsym);
2832 break;
2833
2834 case elfcpp::R_ARM_GOTOFF32:
2835 // We need a GOT section.
2836 target->got_section(symtab, layout);
2837 break;
2838
2839 case elfcpp::R_ARM_BASE_PREL:
2840 // FIXME: What about this?
2841 break;
2842
2843 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 2844 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
2845 {
2846 // The symbol requires a GOT entry.
2847 Output_data_got<32, big_endian>* got =
2848 target->got_section(symtab, layout);
2849 if (gsym->final_value_is_known())
2850 got->add_global(gsym, GOT_TYPE_STANDARD);
2851 else
2852 {
2853 // If this symbol is not fully resolved, we need to add a
2854 // GOT entry with a dynamic relocation.
2855 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2856 if (gsym->is_from_dynobj()
2857 || gsym->is_undefined()
2858 || gsym->is_preemptible())
2859 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
2860 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
2861 else
2862 {
2863 if (got->add_global(gsym, GOT_TYPE_STANDARD))
2864 rel_dyn->add_global_relative(
2865 gsym, elfcpp::R_ARM_RELATIVE, got,
2866 gsym->got_offset(GOT_TYPE_STANDARD));
2867 }
2868 }
2869 }
2870 break;
2871
2872 case elfcpp::R_ARM_TARGET1:
2873 // This should have been mapped to another type already.
2874 // Fall through.
2875 case elfcpp::R_ARM_COPY:
2876 case elfcpp::R_ARM_GLOB_DAT:
2877 case elfcpp::R_ARM_JUMP_SLOT:
2878 case elfcpp::R_ARM_RELATIVE:
2879 // These are relocations which should only be seen by the
2880 // dynamic linker, and should never be seen here.
2881 gold_error(_("%s: unexpected reloc %u in object file"),
2882 object->name().c_str(), r_type);
2883 break;
2884
4a657b0d
DK
2885 default:
2886 unsupported_reloc_global(object, r_type, gsym);
2887 break;
2888 }
2889}
2890
2891// Process relocations for gc.
2892
2893template<bool big_endian>
2894void
2895Target_arm<big_endian>::gc_process_relocs(const General_options& options,
2896 Symbol_table* symtab,
2897 Layout* layout,
2898 Sized_relobj<32, big_endian>* object,
2899 unsigned int data_shndx,
2900 unsigned int,
2901 const unsigned char* prelocs,
2902 size_t reloc_count,
2903 Output_section* output_section,
2904 bool needs_special_offset_handling,
2905 size_t local_symbol_count,
2906 const unsigned char* plocal_symbols)
2907{
2908 typedef Target_arm<big_endian> Arm;
2909 typedef typename Target_arm<big_endian>::Scan Scan;
2910
2911 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
2912 options,
2913 symtab,
2914 layout,
2915 this,
2916 object,
2917 data_shndx,
2918 prelocs,
2919 reloc_count,
2920 output_section,
2921 needs_special_offset_handling,
2922 local_symbol_count,
2923 plocal_symbols);
2924}
2925
2926// Scan relocations for a section.
2927
2928template<bool big_endian>
2929void
2930Target_arm<big_endian>::scan_relocs(const General_options& options,
2931 Symbol_table* symtab,
2932 Layout* layout,
2933 Sized_relobj<32, big_endian>* object,
2934 unsigned int data_shndx,
2935 unsigned int sh_type,
2936 const unsigned char* prelocs,
2937 size_t reloc_count,
2938 Output_section* output_section,
2939 bool needs_special_offset_handling,
2940 size_t local_symbol_count,
2941 const unsigned char* plocal_symbols)
2942{
2943 typedef typename Target_arm<big_endian>::Scan Scan;
2944 if (sh_type == elfcpp::SHT_RELA)
2945 {
2946 gold_error(_("%s: unsupported RELA reloc section"),
2947 object->name().c_str());
2948 return;
2949 }
2950
2951 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
2952 options,
2953 symtab,
2954 layout,
2955 this,
2956 object,
2957 data_shndx,
2958 prelocs,
2959 reloc_count,
2960 output_section,
2961 needs_special_offset_handling,
2962 local_symbol_count,
2963 plocal_symbols);
2964}
2965
2966// Finalize the sections.
2967
2968template<bool big_endian>
2969void
94cdfcff 2970Target_arm<big_endian>::do_finalize_sections(Layout* layout)
4a657b0d 2971{
94cdfcff
DK
2972 // Fill in some more dynamic tags.
2973 Output_data_dynamic* const odyn = layout->dynamic_data();
2974 if (odyn != NULL)
2975 {
2976 if (this->got_plt_ != NULL)
2977 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
2978
2979 if (this->plt_ != NULL)
2980 {
2981 const Output_data* od = this->plt_->rel_plt();
2982 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
2983 odyn->add_section_address(elfcpp::DT_JMPREL, od);
2984 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
2985 }
2986
2987 if (this->rel_dyn_ != NULL)
2988 {
2989 const Output_data* od = this->rel_dyn_;
2990 odyn->add_section_address(elfcpp::DT_REL, od);
2991 odyn->add_section_size(elfcpp::DT_RELSZ, od);
2992 odyn->add_constant(elfcpp::DT_RELENT,
2993 elfcpp::Elf_sizes<32>::rel_size);
2994 }
2995
2996 if (!parameters->options().shared())
2997 {
2998 // The value of the DT_DEBUG tag is filled in by the dynamic
2999 // linker at run time, and used by the debugger.
3000 odyn->add_constant(elfcpp::DT_DEBUG, 0);
3001 }
3002 }
3003
3004 // Emit any relocs we saved in an attempt to avoid generating COPY
3005 // relocs.
3006 if (this->copy_relocs_.any_saved_relocs())
3007 this->copy_relocs_.emit(this->rel_dyn_section(layout));
11af873f
DK
3008
3009 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
3010 // the .ARM.exidx section.
3011 if (!layout->script_options()->saw_phdrs_clause()
3012 && !parameters->options().relocatable())
3013 {
3014 Output_section* exidx_section =
3015 layout->find_output_section(".ARM.exidx");
3016
3017 if (exidx_section != NULL
3018 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
3019 {
3020 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
3021 == NULL);
3022 Output_segment* exidx_segment =
3023 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
3024 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
3025 }
3026 }
4a657b0d
DK
3027}
3028
bec53400
DK
3029// Return whether a direct absolute static relocation needs to be applied.
3030// In cases where Scan::local() or Scan::global() has created
3031// a dynamic relocation other than R_ARM_RELATIVE, the addend
3032// of the relocation is carried in the data, and we must not
3033// apply the static relocation.
3034
3035template<bool big_endian>
3036inline bool
3037Target_arm<big_endian>::Relocate::should_apply_static_reloc(
3038 const Sized_symbol<32>* gsym,
3039 int ref_flags,
3040 bool is_32bit,
3041 Output_section* output_section)
3042{
3043 // If the output section is not allocated, then we didn't call
3044 // scan_relocs, we didn't create a dynamic reloc, and we must apply
3045 // the reloc here.
3046 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
3047 return true;
3048
3049 // For local symbols, we will have created a non-RELATIVE dynamic
3050 // relocation only if (a) the output is position independent,
3051 // (b) the relocation is absolute (not pc- or segment-relative), and
3052 // (c) the relocation is not 32 bits wide.
3053 if (gsym == NULL)
3054 return !(parameters->options().output_is_position_independent()
3055 && (ref_flags & Symbol::ABSOLUTE_REF)
3056 && !is_32bit);
3057
3058 // For global symbols, we use the same helper routines used in the
3059 // scan pass. If we did not create a dynamic relocation, or if we
3060 // created a RELATIVE dynamic relocation, we should apply the static
3061 // relocation.
3062 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
3063 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
3064 && gsym->can_use_relative_reloc(ref_flags
3065 & Symbol::FUNCTION_CALL);
3066 return !has_dyn || is_rel;
3067}
3068
4a657b0d
DK
3069// Perform a relocation.
3070
3071template<bool big_endian>
3072inline bool
3073Target_arm<big_endian>::Relocate::relocate(
c121c671
DK
3074 const Relocate_info<32, big_endian>* relinfo,
3075 Target_arm* target,
3076 Output_section *output_section,
3077 size_t relnum,
3078 const elfcpp::Rel<32, big_endian>& rel,
4a657b0d 3079 unsigned int r_type,
c121c671
DK
3080 const Sized_symbol<32>* gsym,
3081 const Symbol_value<32>* psymval,
3082 unsigned char* view,
3083 elfcpp::Elf_types<32>::Elf_Addr address,
4a657b0d
DK
3084 section_size_type /* view_size */ )
3085{
c121c671
DK
3086 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
3087
3088 r_type = get_real_reloc_type(r_type);
3089
3090 // If this the symbol may be a Thumb function, set thumb bit to 1.
3091 bool has_thumb_bit = ((gsym != NULL)
3092 && (gsym->type() == elfcpp::STT_FUNC
3093 || gsym->type() == elfcpp::STT_ARM_TFUNC));
3094
3095 // Pick the value to use for symbols defined in shared objects.
3096 Symbol_value<32> symval;
3097 if (gsym != NULL
3098 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
3099 {
3100 symval.set_output_value(target->plt_section()->address()
3101 + gsym->plt_offset());
3102 psymval = &symval;
3103 has_thumb_bit = 0;
3104 }
3105
3106 const Sized_relobj<32, big_endian>* object = relinfo->object;
3107
3108 // Get the GOT offset if needed.
3109 // The GOT pointer points to the end of the GOT section.
3110 // We need to subtract the size of the GOT section to get
3111 // the actual offset to use in the relocation.
3112 bool have_got_offset = false;
3113 unsigned int got_offset = 0;
3114 switch (r_type)
3115 {
3116 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 3117 case elfcpp::R_ARM_GOT_PREL:
c121c671
DK
3118 if (gsym != NULL)
3119 {
3120 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3121 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
3122 - target->got_size());
3123 }
3124 else
3125 {
3126 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
3127 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3128 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3129 - target->got_size());
3130 }
3131 have_got_offset = true;
3132 break;
3133
3134 default:
3135 break;
3136 }
3137
3138 typename Arm_relocate_functions::Status reloc_status =
3139 Arm_relocate_functions::STATUS_OKAY;
4a657b0d
DK
3140 switch (r_type)
3141 {
3142 case elfcpp::R_ARM_NONE:
3143 break;
3144
5e445df6
ILT
3145 case elfcpp::R_ARM_ABS8:
3146 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3147 output_section))
be8fcb75
ILT
3148 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
3149 break;
3150
3151 case elfcpp::R_ARM_ABS12:
3152 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3153 output_section))
3154 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
3155 break;
3156
3157 case elfcpp::R_ARM_ABS16:
3158 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3159 output_section))
3160 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5e445df6
ILT
3161 break;
3162
c121c671
DK
3163 case elfcpp::R_ARM_ABS32:
3164 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3165 output_section))
3166 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
3167 has_thumb_bit);
3168 break;
3169
be8fcb75
ILT
3170 case elfcpp::R_ARM_ABS32_NOI:
3171 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3172 output_section))
3173 // No thumb bit for this relocation: (S + A)
3174 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
3175 false);
3176 break;
3177
fd3c5f0b
ILT
3178 case elfcpp::R_ARM_MOVW_ABS_NC:
3179 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3180 output_section))
3181 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
3182 psymval,
3183 has_thumb_bit);
3184 else
3185 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
3186 "a shared object; recompile with -fPIC"));
3187 break;
3188
3189 case elfcpp::R_ARM_MOVT_ABS:
3190 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3191 output_section))
3192 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
3193 else
3194 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
3195 "a shared object; recompile with -fPIC"));
3196 break;
3197
3198 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3199 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3200 output_section))
3201 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
3202 psymval,
3203 has_thumb_bit);
3204 else
3205 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
3206 "making a shared object; recompile with -fPIC"));
3207 break;
3208
3209 case elfcpp::R_ARM_THM_MOVT_ABS:
3210 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3211 output_section))
3212 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
3213 psymval);
3214 else
3215 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
3216 "making a shared object; recompile with -fPIC"));
3217 break;
3218
c2a122b6
ILT
3219 case elfcpp::R_ARM_MOVW_PREL_NC:
3220 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
3221 psymval, address,
3222 has_thumb_bit);
3223 break;
3224
3225 case elfcpp::R_ARM_MOVT_PREL:
3226 reloc_status = Arm_relocate_functions::movt_prel(view, object,
3227 psymval, address);
3228 break;
3229
3230 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3231 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
3232 psymval, address,
3233 has_thumb_bit);
3234 break;
3235
3236 case elfcpp::R_ARM_THM_MOVT_PREL:
3237 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
3238 psymval, address);
3239 break;
3240
c121c671
DK
3241 case elfcpp::R_ARM_REL32:
3242 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
3243 address, has_thumb_bit);
3244 break;
3245
be8fcb75
ILT
3246 case elfcpp::R_ARM_THM_ABS5:
3247 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3248 output_section))
3249 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
3250 break;
3251
c121c671
DK
3252 case elfcpp::R_ARM_THM_CALL:
3253 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
3254 address, has_thumb_bit);
3255 break;
3256
3257 case elfcpp::R_ARM_GOTOFF32:
3258 {
3259 elfcpp::Elf_types<32>::Elf_Addr got_origin;
3260 got_origin = target->got_plt_section()->address();
3261 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
3262 got_origin, has_thumb_bit);
3263 }
3264 break;
3265
3266 case elfcpp::R_ARM_BASE_PREL:
3267 {
3268 uint32_t origin;
3269 // Get the addressing origin of the output segment defining the
3270 // symbol gsym (AAELF 4.6.1.2 Relocation types)
3271 gold_assert(gsym != NULL);
3272 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
3273 origin = gsym->output_segment()->vaddr();
3274 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
3275 origin = gsym->output_data()->address();
3276 else
3277 {
3278 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3279 _("cannot find origin of R_ARM_BASE_PREL"));
3280 return true;
3281 }
3282 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
3283 }
3284 break;
3285
be8fcb75
ILT
3286 case elfcpp::R_ARM_BASE_ABS:
3287 {
3288 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3289 output_section))
3290 break;
3291
3292 uint32_t origin;
3293 // Get the addressing origin of the output segment defining
3294 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
3295 if (gsym == NULL)
3296 // R_ARM_BASE_ABS with the NULL symbol will give the
3297 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
3298 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
3299 origin = target->got_plt_section()->address();
3300 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
3301 origin = gsym->output_segment()->vaddr();
3302 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
3303 origin = gsym->output_data()->address();
3304 else
3305 {
3306 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3307 _("cannot find origin of R_ARM_BASE_ABS"));
3308 return true;
3309 }
3310
3311 reloc_status = Arm_relocate_functions::base_abs(view, origin);
3312 }
3313 break;
3314
c121c671
DK
3315 case elfcpp::R_ARM_GOT_BREL:
3316 gold_assert(have_got_offset);
3317 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
3318 break;
3319
7f5309a5
ILT
3320 case elfcpp::R_ARM_GOT_PREL:
3321 gold_assert(have_got_offset);
3322 // Get the address origin for GOT PLT, which is allocated right
3323 // after the GOT section, to calculate an absolute address of
3324 // the symbol GOT entry (got_origin + got_offset).
3325 elfcpp::Elf_types<32>::Elf_Addr got_origin;
3326 got_origin = target->got_plt_section()->address();
3327 reloc_status = Arm_relocate_functions::got_prel(view,
3328 got_origin + got_offset,
3329 address);
3330 break;
3331
c121c671
DK
3332 case elfcpp::R_ARM_PLT32:
3333 gold_assert(gsym == NULL
3334 || gsym->has_plt_offset()
3335 || gsym->final_value_is_known()
3336 || (gsym->is_defined()
3337 && !gsym->is_from_dynobj()
3338 && !gsym->is_preemptible()));
3339 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
3340 address, has_thumb_bit);
3341 break;
3342
3343 case elfcpp::R_ARM_CALL:
3344 reloc_status = Arm_relocate_functions::call(view, object, psymval,
3345 address, has_thumb_bit);
3346 break;
3347
3348 case elfcpp::R_ARM_JUMP24:
3349 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
3350 address, has_thumb_bit);
3351 break;
3352
3353 case elfcpp::R_ARM_PREL31:
3354 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
3355 address, has_thumb_bit);
3356 break;
3357
3358 case elfcpp::R_ARM_TARGET1:
3359 // This should have been mapped to another type already.
3360 // Fall through.
3361 case elfcpp::R_ARM_COPY:
3362 case elfcpp::R_ARM_GLOB_DAT:
3363 case elfcpp::R_ARM_JUMP_SLOT:
3364 case elfcpp::R_ARM_RELATIVE:
3365 // These are relocations which should only be seen by the
3366 // dynamic linker, and should never be seen here.
3367 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3368 _("unexpected reloc %u in object file"),
3369 r_type);
3370 break;
3371
3372 default:
3373 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3374 _("unsupported reloc %u"),
3375 r_type);
3376 break;
3377 }
3378
3379 // Report any errors.
3380 switch (reloc_status)
3381 {
3382 case Arm_relocate_functions::STATUS_OKAY:
3383 break;
3384 case Arm_relocate_functions::STATUS_OVERFLOW:
3385 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3386 _("relocation overflow in relocation %u"),
3387 r_type);
3388 break;
3389 case Arm_relocate_functions::STATUS_BAD_RELOC:
3390 gold_error_at_location(
3391 relinfo,
3392 relnum,
3393 rel.get_r_offset(),
3394 _("unexpected opcode while processing relocation %u"),
3395 r_type);
3396 break;
4a657b0d
DK
3397 default:
3398 gold_unreachable();
3399 }
3400
3401 return true;
3402}
3403
3404// Relocate section data.
3405
3406template<bool big_endian>
3407void
3408Target_arm<big_endian>::relocate_section(
3409 const Relocate_info<32, big_endian>* relinfo,
3410 unsigned int sh_type,
3411 const unsigned char* prelocs,
3412 size_t reloc_count,
3413 Output_section* output_section,
3414 bool needs_special_offset_handling,
3415 unsigned char* view,
3416 elfcpp::Elf_types<32>::Elf_Addr address,
364c7fa5
ILT
3417 section_size_type view_size,
3418 const Reloc_symbol_changes* reloc_symbol_changes)
4a657b0d
DK
3419{
3420 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
3421 gold_assert(sh_type == elfcpp::SHT_REL);
3422
3423 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
3424 Arm_relocate>(
3425 relinfo,
3426 this,
3427 prelocs,
3428 reloc_count,
3429 output_section,
3430 needs_special_offset_handling,
3431 view,
3432 address,
364c7fa5
ILT
3433 view_size,
3434 reloc_symbol_changes);
4a657b0d
DK
3435}
3436
3437// Return the size of a relocation while scanning during a relocatable
3438// link.
3439
3440template<bool big_endian>
3441unsigned int
3442Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
3443 unsigned int r_type,
3444 Relobj* object)
3445{
3446 r_type = get_real_reloc_type(r_type);
3447 switch (r_type)
3448 {
3449 case elfcpp::R_ARM_NONE:
3450 return 0;
3451
5e445df6
ILT
3452 case elfcpp::R_ARM_ABS8:
3453 return 1;
3454
be8fcb75
ILT
3455 case elfcpp::R_ARM_ABS16:
3456 case elfcpp::R_ARM_THM_ABS5:
3457 return 2;
3458
4a657b0d 3459 case elfcpp::R_ARM_ABS32:
be8fcb75
ILT
3460 case elfcpp::R_ARM_ABS32_NOI:
3461 case elfcpp::R_ARM_ABS12:
3462 case elfcpp::R_ARM_BASE_ABS:
4a657b0d
DK
3463 case elfcpp::R_ARM_REL32:
3464 case elfcpp::R_ARM_THM_CALL:
3465 case elfcpp::R_ARM_GOTOFF32:
3466 case elfcpp::R_ARM_BASE_PREL:
3467 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 3468 case elfcpp::R_ARM_GOT_PREL:
4a657b0d
DK
3469 case elfcpp::R_ARM_PLT32:
3470 case elfcpp::R_ARM_CALL:
3471 case elfcpp::R_ARM_JUMP24:
3472 case elfcpp::R_ARM_PREL31:
fd3c5f0b
ILT
3473 case elfcpp::R_ARM_MOVW_ABS_NC:
3474 case elfcpp::R_ARM_MOVT_ABS:
3475 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3476 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
3477 case elfcpp::R_ARM_MOVW_PREL_NC:
3478 case elfcpp::R_ARM_MOVT_PREL:
3479 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3480 case elfcpp::R_ARM_THM_MOVT_PREL:
4a657b0d
DK
3481 return 4;
3482
3483 case elfcpp::R_ARM_TARGET1:
3484 // This should have been mapped to another type already.
3485 // Fall through.
3486 case elfcpp::R_ARM_COPY:
3487 case elfcpp::R_ARM_GLOB_DAT:
3488 case elfcpp::R_ARM_JUMP_SLOT:
3489 case elfcpp::R_ARM_RELATIVE:
3490 // These are relocations which should only be seen by the
3491 // dynamic linker, and should never be seen here.
3492 gold_error(_("%s: unexpected reloc %u in object file"),
3493 object->name().c_str(), r_type);
3494 return 0;
3495
3496 default:
3497 object->error(_("unsupported reloc %u in object file"), r_type);
3498 return 0;
3499 }
3500}
3501
3502// Scan the relocs during a relocatable link.
3503
3504template<bool big_endian>
3505void
3506Target_arm<big_endian>::scan_relocatable_relocs(
3507 const General_options& options,
3508 Symbol_table* symtab,
3509 Layout* layout,
3510 Sized_relobj<32, big_endian>* object,
3511 unsigned int data_shndx,
3512 unsigned int sh_type,
3513 const unsigned char* prelocs,
3514 size_t reloc_count,
3515 Output_section* output_section,
3516 bool needs_special_offset_handling,
3517 size_t local_symbol_count,
3518 const unsigned char* plocal_symbols,
3519 Relocatable_relocs* rr)
3520{
3521 gold_assert(sh_type == elfcpp::SHT_REL);
3522
3523 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3524 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3525
3526 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
3527 Scan_relocatable_relocs>(
3528 options,
3529 symtab,
3530 layout,
3531 object,
3532 data_shndx,
3533 prelocs,
3534 reloc_count,
3535 output_section,
3536 needs_special_offset_handling,
3537 local_symbol_count,
3538 plocal_symbols,
3539 rr);
3540}
3541
3542// Relocate a section during a relocatable link.
3543
3544template<bool big_endian>
3545void
3546Target_arm<big_endian>::relocate_for_relocatable(
3547 const Relocate_info<32, big_endian>* relinfo,
3548 unsigned int sh_type,
3549 const unsigned char* prelocs,
3550 size_t reloc_count,
3551 Output_section* output_section,
3552 off_t offset_in_output_section,
3553 const Relocatable_relocs* rr,
3554 unsigned char* view,
3555 elfcpp::Elf_types<32>::Elf_Addr view_address,
3556 section_size_type view_size,
3557 unsigned char* reloc_view,
3558 section_size_type reloc_view_size)
3559{
3560 gold_assert(sh_type == elfcpp::SHT_REL);
3561
3562 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
3563 relinfo,
3564 prelocs,
3565 reloc_count,
3566 output_section,
3567 offset_in_output_section,
3568 rr,
3569 view,
3570 view_address,
3571 view_size,
3572 reloc_view,
3573 reloc_view_size);
3574}
3575
94cdfcff
DK
3576// Return the value to use for a dynamic symbol which requires special
3577// treatment. This is how we support equality comparisons of function
3578// pointers across shared library boundaries, as described in the
3579// processor specific ABI supplement.
3580
4a657b0d
DK
3581template<bool big_endian>
3582uint64_t
94cdfcff 3583Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4a657b0d 3584{
94cdfcff
DK
3585 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3586 return this->plt_section()->address() + gsym->plt_offset();
4a657b0d
DK
3587}
3588
3589// Map platform-specific relocs to real relocs
3590//
3591template<bool big_endian>
3592unsigned int
3593Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
3594{
3595 switch (r_type)
3596 {
3597 case elfcpp::R_ARM_TARGET1:
3598 // This is either R_ARM_ABS32 or R_ARM_REL32;
3599 return elfcpp::R_ARM_ABS32;
3600
3601 case elfcpp::R_ARM_TARGET2:
3602 // This can be any reloc type but ususally is R_ARM_GOT_PREL
3603 return elfcpp::R_ARM_GOT_PREL;
3604
3605 default:
3606 return r_type;
3607 }
3608}
3609
3610// The selector for arm object files.
3611
3612template<bool big_endian>
3613class Target_selector_arm : public Target_selector
3614{
3615 public:
3616 Target_selector_arm()
3617 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
3618 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
3619 { }
3620
3621 Target*
3622 do_instantiate_target()
3623 { return new Target_arm<big_endian>(); }
3624};
3625
3626Target_selector_arm<false> target_selector_arm;
3627Target_selector_arm<true> target_selector_armbe;
3628
3629} // End anonymous namespace.
This page took 0.178955 seconds and 4 git commands to generate.