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