* cris/arch.c: Regenerate.
[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
10ad9fe5
DK
748// A class to wrap an ordinary input section containing executable code.
749
750template<bool big_endian>
751class Arm_input_section : public Output_relaxed_input_section
752{
753 public:
754 Arm_input_section(Relobj* relobj, unsigned int shndx)
755 : Output_relaxed_input_section(relobj, shndx, 1),
756 original_addralign_(1), original_size_(0), stub_table_(NULL)
757 { }
758
759 ~Arm_input_section()
760 { }
761
762 // Initialize.
763 void
764 init();
765
766 // Whether this is a stub table owner.
767 bool
768 is_stub_table_owner() const
769 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
770
771 // Return the stub table.
772 Stub_table<big_endian>*
773 stub_table() const
774 { return this->stub_table_; }
775
776 // Set the stub_table.
777 void
778 set_stub_table(Stub_table<big_endian>* stub_table)
779 { this->stub_table_ = stub_table; }
780
781 protected:
782 // Write data to output file.
783 void
784 do_write(Output_file*);
785
786 // Return required alignment of this.
787 uint64_t
788 do_addralign() const
789 {
790 if (this->is_stub_table_owner())
791 return std::max(this->stub_table_->addralign(),
792 this->original_addralign_);
793 else
794 return this->original_addralign_;
795 }
796
797 // Finalize data size.
798 void
799 set_final_data_size();
800
801 // Reset address and file offset.
802 void
803 do_reset_address_and_file_offset();
804
805 // Output offset.
806 bool
807 do_output_offset(const Relobj* object, unsigned int shndx,
808 section_offset_type offset,
809 section_offset_type* poutput) const
810 {
811 if ((object == this->relobj())
812 && (shndx == this->shndx())
813 && (offset >= 0)
814 && (convert_types<uint64_t, section_offset_type>(offset)
815 <= this->original_size_))
816 {
817 *poutput = offset;
818 return true;
819 }
820 else
821 return false;
822 }
823
824 private:
825 // Copying is not allowed.
826 Arm_input_section(const Arm_input_section&);
827 Arm_input_section& operator=(const Arm_input_section&);
828
829 // Address alignment of the original input section.
830 uint64_t original_addralign_;
831 // Section size of the original input section.
832 uint64_t original_size_;
833 // Stub table.
834 Stub_table<big_endian>* stub_table_;
835};
836
c121c671
DK
837// Utilities for manipulating integers of up to 32-bits
838
839namespace utils
840{
841 // Sign extend an n-bit unsigned integer stored in an uint32_t into
842 // an int32_t. NO_BITS must be between 1 to 32.
843 template<int no_bits>
844 static inline int32_t
845 sign_extend(uint32_t bits)
846 {
96d49306 847 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
848 if (no_bits == 32)
849 return static_cast<int32_t>(bits);
850 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
851 bits &= mask;
852 uint32_t top_bit = 1U << (no_bits - 1);
853 int32_t as_signed = static_cast<int32_t>(bits);
854 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
855 }
856
857 // Detects overflow of an NO_BITS integer stored in a uint32_t.
858 template<int no_bits>
859 static inline bool
860 has_overflow(uint32_t bits)
861 {
96d49306 862 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
863 if (no_bits == 32)
864 return false;
865 int32_t max = (1 << (no_bits - 1)) - 1;
866 int32_t min = -(1 << (no_bits - 1));
867 int32_t as_signed = static_cast<int32_t>(bits);
868 return as_signed > max || as_signed < min;
869 }
870
5e445df6
ILT
871 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
872 // fits in the given number of bits as either a signed or unsigned value.
873 // For example, has_signed_unsigned_overflow<8> would check
874 // -128 <= bits <= 255
875 template<int no_bits>
876 static inline bool
877 has_signed_unsigned_overflow(uint32_t bits)
878 {
879 gold_assert(no_bits >= 2 && no_bits <= 32);
880 if (no_bits == 32)
881 return false;
882 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
883 int32_t min = -(1 << (no_bits - 1));
884 int32_t as_signed = static_cast<int32_t>(bits);
885 return as_signed > max || as_signed < min;
886 }
887
c121c671
DK
888 // Select bits from A and B using bits in MASK. For each n in [0..31],
889 // the n-th bit in the result is chosen from the n-th bits of A and B.
890 // A zero selects A and a one selects B.
891 static inline uint32_t
892 bit_select(uint32_t a, uint32_t b, uint32_t mask)
893 { return (a & ~mask) | (b & mask); }
894};
895
4a657b0d
DK
896template<bool big_endian>
897class Target_arm : public Sized_target<32, big_endian>
898{
899 public:
900 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
901 Reloc_section;
902
903 Target_arm()
94cdfcff
DK
904 : Sized_target<32, big_endian>(&arm_info),
905 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
b569affa
DK
906 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
907 may_use_blx_(true), should_force_pic_veneer_(false)
4a657b0d
DK
908 { }
909
b569affa
DK
910 // Whether we can use BLX.
911 bool
912 may_use_blx() const
913 { return this->may_use_blx_; }
914
915 // Set use-BLX flag.
916 void
917 set_may_use_blx(bool value)
918 { this->may_use_blx_ = value; }
919
920 // Whether we force PCI branch veneers.
921 bool
922 should_force_pic_veneer() const
923 { return this->should_force_pic_veneer_; }
924
925 // Set PIC veneer flag.
926 void
927 set_should_force_pic_veneer(bool value)
928 { this->should_force_pic_veneer_ = value; }
929
930 // Whether we use THUMB-2 instructions.
931 bool
932 using_thumb2() const
933 {
934 // FIXME: This should not hard-coded.
935 return false;
936 }
937
938 // Whether we use THUMB/THUMB-2 instructions only.
939 bool
940 using_thumb_only() const
941 {
942 // FIXME: This should not hard-coded.
943 return false;
944 }
945
4a657b0d
DK
946 // Process the relocations to determine unreferenced sections for
947 // garbage collection.
948 void
949 gc_process_relocs(const General_options& options,
950 Symbol_table* symtab,
951 Layout* layout,
952 Sized_relobj<32, big_endian>* object,
953 unsigned int data_shndx,
954 unsigned int sh_type,
955 const unsigned char* prelocs,
956 size_t reloc_count,
957 Output_section* output_section,
958 bool needs_special_offset_handling,
959 size_t local_symbol_count,
960 const unsigned char* plocal_symbols);
961
962 // Scan the relocations to look for symbol adjustments.
963 void
964 scan_relocs(const General_options& options,
965 Symbol_table* symtab,
966 Layout* layout,
967 Sized_relobj<32, big_endian>* object,
968 unsigned int data_shndx,
969 unsigned int sh_type,
970 const unsigned char* prelocs,
971 size_t reloc_count,
972 Output_section* output_section,
973 bool needs_special_offset_handling,
974 size_t local_symbol_count,
975 const unsigned char* plocal_symbols);
976
977 // Finalize the sections.
978 void
979 do_finalize_sections(Layout*);
980
94cdfcff 981 // Return the value to use for a dynamic symbol which requires special
4a657b0d
DK
982 // treatment.
983 uint64_t
984 do_dynsym_value(const Symbol*) const;
985
986 // Relocate a section.
987 void
988 relocate_section(const Relocate_info<32, big_endian>*,
989 unsigned int sh_type,
990 const unsigned char* prelocs,
991 size_t reloc_count,
992 Output_section* output_section,
993 bool needs_special_offset_handling,
994 unsigned char* view,
995 elfcpp::Elf_types<32>::Elf_Addr view_address,
364c7fa5
ILT
996 section_size_type view_size,
997 const Reloc_symbol_changes*);
4a657b0d
DK
998
999 // Scan the relocs during a relocatable link.
1000 void
1001 scan_relocatable_relocs(const General_options& options,
1002 Symbol_table* symtab,
1003 Layout* layout,
1004 Sized_relobj<32, big_endian>* object,
1005 unsigned int data_shndx,
1006 unsigned int sh_type,
1007 const unsigned char* prelocs,
1008 size_t reloc_count,
1009 Output_section* output_section,
1010 bool needs_special_offset_handling,
1011 size_t local_symbol_count,
1012 const unsigned char* plocal_symbols,
1013 Relocatable_relocs*);
1014
1015 // Relocate a section during a relocatable link.
1016 void
1017 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1018 unsigned int sh_type,
1019 const unsigned char* prelocs,
1020 size_t reloc_count,
1021 Output_section* output_section,
1022 off_t offset_in_output_section,
1023 const Relocatable_relocs*,
1024 unsigned char* view,
1025 elfcpp::Elf_types<32>::Elf_Addr view_address,
1026 section_size_type view_size,
1027 unsigned char* reloc_view,
1028 section_size_type reloc_view_size);
1029
1030 // Return whether SYM is defined by the ABI.
1031 bool
1032 do_is_defined_by_abi(Symbol* sym) const
1033 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1034
94cdfcff
DK
1035 // Return the size of the GOT section.
1036 section_size_type
1037 got_size()
1038 {
1039 gold_assert(this->got_ != NULL);
1040 return this->got_->data_size();
1041 }
1042
4a657b0d
DK
1043 // Map platform-specific reloc types
1044 static unsigned int
1045 get_real_reloc_type (unsigned int r_type);
1046
b569affa
DK
1047 // Get the default ARM target.
1048 static const Target_arm<big_endian>&
1049 default_target()
1050 {
1051 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1052 && parameters->target().is_big_endian() == big_endian);
1053 return static_cast<const Target_arm<big_endian>&>(parameters->target());
1054 }
1055
4a657b0d
DK
1056 private:
1057 // The class which scans relocations.
1058 class Scan
1059 {
1060 public:
1061 Scan()
bec53400 1062 : issued_non_pic_error_(false)
4a657b0d
DK
1063 { }
1064
1065 inline void
1066 local(const General_options& options, Symbol_table* symtab,
1067 Layout* layout, Target_arm* target,
1068 Sized_relobj<32, big_endian>* object,
1069 unsigned int data_shndx,
1070 Output_section* output_section,
1071 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1072 const elfcpp::Sym<32, big_endian>& lsym);
1073
1074 inline void
1075 global(const General_options& options, Symbol_table* symtab,
1076 Layout* layout, Target_arm* target,
1077 Sized_relobj<32, big_endian>* object,
1078 unsigned int data_shndx,
1079 Output_section* output_section,
1080 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1081 Symbol* gsym);
1082
1083 private:
1084 static void
1085 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1086 unsigned int r_type);
1087
1088 static void
1089 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1090 unsigned int r_type, Symbol*);
bec53400
DK
1091
1092 void
1093 check_non_pic(Relobj*, unsigned int r_type);
1094
1095 // Almost identical to Symbol::needs_plt_entry except that it also
1096 // handles STT_ARM_TFUNC.
1097 static bool
1098 symbol_needs_plt_entry(const Symbol* sym)
1099 {
1100 // An undefined symbol from an executable does not need a PLT entry.
1101 if (sym->is_undefined() && !parameters->options().shared())
1102 return false;
1103
1104 return (!parameters->doing_static_link()
1105 && (sym->type() == elfcpp::STT_FUNC
1106 || sym->type() == elfcpp::STT_ARM_TFUNC)
1107 && (sym->is_from_dynobj()
1108 || sym->is_undefined()
1109 || sym->is_preemptible()));
1110 }
1111
1112 // Whether we have issued an error about a non-PIC compilation.
1113 bool issued_non_pic_error_;
4a657b0d
DK
1114 };
1115
1116 // The class which implements relocation.
1117 class Relocate
1118 {
1119 public:
1120 Relocate()
1121 { }
1122
1123 ~Relocate()
1124 { }
1125
bec53400
DK
1126 // Return whether the static relocation needs to be applied.
1127 inline bool
1128 should_apply_static_reloc(const Sized_symbol<32>* gsym,
1129 int ref_flags,
1130 bool is_32bit,
1131 Output_section* output_section);
1132
4a657b0d
DK
1133 // Do a relocation. Return false if the caller should not issue
1134 // any warnings about this relocation.
1135 inline bool
1136 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1137 Output_section*, size_t relnum,
1138 const elfcpp::Rel<32, big_endian>&,
1139 unsigned int r_type, const Sized_symbol<32>*,
1140 const Symbol_value<32>*,
1141 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
1142 section_size_type);
c121c671
DK
1143
1144 // Return whether we want to pass flag NON_PIC_REF for this
1145 // reloc.
1146 static inline bool
1147 reloc_is_non_pic (unsigned int r_type)
1148 {
1149 switch (r_type)
1150 {
1151 case elfcpp::R_ARM_REL32:
1152 case elfcpp::R_ARM_THM_CALL:
1153 case elfcpp::R_ARM_CALL:
1154 case elfcpp::R_ARM_JUMP24:
1155 case elfcpp::R_ARM_PREL31:
be8fcb75
ILT
1156 case elfcpp::R_ARM_THM_ABS5:
1157 case elfcpp::R_ARM_ABS8:
1158 case elfcpp::R_ARM_ABS12:
1159 case elfcpp::R_ARM_ABS16:
1160 case elfcpp::R_ARM_BASE_ABS:
c121c671
DK
1161 return true;
1162 default:
1163 return false;
1164 }
1165 }
4a657b0d
DK
1166 };
1167
1168 // A class which returns the size required for a relocation type,
1169 // used while scanning relocs during a relocatable link.
1170 class Relocatable_size_for_reloc
1171 {
1172 public:
1173 unsigned int
1174 get_size_for_reloc(unsigned int, Relobj*);
1175 };
1176
94cdfcff
DK
1177 // Get the GOT section, creating it if necessary.
1178 Output_data_got<32, big_endian>*
1179 got_section(Symbol_table*, Layout*);
1180
1181 // Get the GOT PLT section.
1182 Output_data_space*
1183 got_plt_section() const
1184 {
1185 gold_assert(this->got_plt_ != NULL);
1186 return this->got_plt_;
1187 }
1188
1189 // Create a PLT entry for a global symbol.
1190 void
1191 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1192
1193 // Get the PLT section.
1194 const Output_data_plt_arm<big_endian>*
1195 plt_section() const
1196 {
1197 gold_assert(this->plt_ != NULL);
1198 return this->plt_;
1199 }
1200
1201 // Get the dynamic reloc section, creating it if necessary.
1202 Reloc_section*
1203 rel_dyn_section(Layout*);
1204
1205 // Return true if the symbol may need a COPY relocation.
1206 // References from an executable object to non-function symbols
1207 // defined in a dynamic object may need a COPY relocation.
1208 bool
1209 may_need_copy_reloc(Symbol* gsym)
1210 {
966d4097
DK
1211 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1212 && gsym->may_need_copy_reloc());
94cdfcff
DK
1213 }
1214
1215 // Add a potential copy relocation.
1216 void
1217 copy_reloc(Symbol_table* symtab, Layout* layout,
1218 Sized_relobj<32, big_endian>* object,
1219 unsigned int shndx, Output_section* output_section,
1220 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1221 {
1222 this->copy_relocs_.copy_reloc(symtab, layout,
1223 symtab->get_sized_symbol<32>(sym),
1224 object, shndx, output_section, reloc,
1225 this->rel_dyn_section(layout));
1226 }
1227
4a657b0d
DK
1228 // Information about this specific target which we pass to the
1229 // general Target structure.
1230 static const Target::Target_info arm_info;
94cdfcff
DK
1231
1232 // The types of GOT entries needed for this platform.
1233 enum Got_type
1234 {
1235 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
1236 };
1237
1238 // The GOT section.
1239 Output_data_got<32, big_endian>* got_;
1240 // The PLT section.
1241 Output_data_plt_arm<big_endian>* plt_;
1242 // The GOT PLT section.
1243 Output_data_space* got_plt_;
1244 // The dynamic reloc section.
1245 Reloc_section* rel_dyn_;
1246 // Relocs saved to avoid a COPY reloc.
1247 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1248 // Space for variables copied with a COPY reloc.
1249 Output_data_space* dynbss_;
b569affa
DK
1250 // Whether we can use BLX.
1251 bool may_use_blx_;
1252 // Whether we force PIC branch veneers.
1253 bool should_force_pic_veneer_;
4a657b0d
DK
1254};
1255
1256template<bool big_endian>
1257const Target::Target_info Target_arm<big_endian>::arm_info =
1258{
1259 32, // size
1260 big_endian, // is_big_endian
1261 elfcpp::EM_ARM, // machine_code
1262 false, // has_make_symbol
1263 false, // has_resolve
1264 false, // has_code_fill
1265 true, // is_default_stack_executable
1266 '\0', // wrap_char
1267 "/usr/lib/libc.so.1", // dynamic_linker
1268 0x8000, // default_text_segment_address
1269 0x1000, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
1270 0x1000, // common_pagesize (overridable by -z common-page-size)
1271 elfcpp::SHN_UNDEF, // small_common_shndx
1272 elfcpp::SHN_UNDEF, // large_common_shndx
1273 0, // small_common_section_flags
1274 0 // large_common_section_flags
4a657b0d
DK
1275};
1276
c121c671
DK
1277// Arm relocate functions class
1278//
1279
1280template<bool big_endian>
1281class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1282{
1283 public:
1284 typedef enum
1285 {
1286 STATUS_OKAY, // No error during relocation.
1287 STATUS_OVERFLOW, // Relocation oveflow.
1288 STATUS_BAD_RELOC // Relocation cannot be applied.
1289 } Status;
1290
1291 private:
1292 typedef Relocate_functions<32, big_endian> Base;
1293 typedef Arm_relocate_functions<big_endian> This;
1294
1295 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
1296 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
1297 // to distinguish ARM and THUMB functions and it is treated specially.
1298 static inline Symbol_value<32>::Value
1299 arm_symbol_value (const Sized_relobj<32, big_endian> *object,
1300 const Symbol_value<32>* psymval,
1301 Symbol_value<32>::Value addend,
1302 bool has_thumb_bit)
1303 {
1304 typedef Symbol_value<32>::Value Valtype;
1305
1306 if (has_thumb_bit)
1307 {
1308 Valtype raw = psymval->value(object, 0);
1309 Valtype thumb_bit = raw & 1;
1310 return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
1311 }
1312 else
1313 return psymval->value(object, addend);
1314 }
1315
fd3c5f0b
ILT
1316 // Encoding of imm16 argument for movt and movw ARM instructions
1317 // from ARM ARM:
1318 //
1319 // imm16 := imm4 | imm12
1320 //
1321 // 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
1322 // +-------+---------------+-------+-------+-----------------------+
1323 // | | |imm4 | |imm12 |
1324 // +-------+---------------+-------+-------+-----------------------+
1325
1326 // Extract the relocation addend from VAL based on the ARM
1327 // instruction encoding described above.
1328 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1329 extract_arm_movw_movt_addend(
1330 typename elfcpp::Swap<32, big_endian>::Valtype val)
1331 {
1332 // According to the Elf ABI for ARM Architecture the immediate
1333 // field is sign-extended to form the addend.
1334 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1335 }
1336
1337 // Insert X into VAL based on the ARM instruction encoding described
1338 // above.
1339 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1340 insert_val_arm_movw_movt(
1341 typename elfcpp::Swap<32, big_endian>::Valtype val,
1342 typename elfcpp::Swap<32, big_endian>::Valtype x)
1343 {
1344 val &= 0xfff0f000;
1345 val |= x & 0x0fff;
1346 val |= (x & 0xf000) << 4;
1347 return val;
1348 }
1349
1350 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1351 // from ARM ARM:
1352 //
1353 // imm16 := imm4 | i | imm3 | imm8
1354 //
1355 // 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
1356 // +---------+-+-----------+-------++-+-----+-------+---------------+
1357 // | |i| |imm4 || |imm3 | |imm8 |
1358 // +---------+-+-----------+-------++-+-----+-------+---------------+
1359
1360 // Extract the relocation addend from VAL based on the Thumb2
1361 // instruction encoding described above.
1362 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1363 extract_thumb_movw_movt_addend(
1364 typename elfcpp::Swap<32, big_endian>::Valtype val)
1365 {
1366 // According to the Elf ABI for ARM Architecture the immediate
1367 // field is sign-extended to form the addend.
1368 return utils::sign_extend<16>(((val >> 4) & 0xf000)
1369 | ((val >> 15) & 0x0800)
1370 | ((val >> 4) & 0x0700)
1371 | (val & 0x00ff));
1372 }
1373
1374 // Insert X into VAL based on the Thumb2 instruction encoding
1375 // described above.
1376 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1377 insert_val_thumb_movw_movt(
1378 typename elfcpp::Swap<32, big_endian>::Valtype val,
1379 typename elfcpp::Swap<32, big_endian>::Valtype x)
1380 {
1381 val &= 0xfbf08f00;
1382 val |= (x & 0xf000) << 4;
1383 val |= (x & 0x0800) << 15;
1384 val |= (x & 0x0700) << 4;
1385 val |= (x & 0x00ff);
1386 return val;
1387 }
1388
c121c671
DK
1389 // FIXME: This probably only works for Android on ARM v5te. We should
1390 // following GNU ld for the general case.
1391 template<unsigned r_type>
1392 static inline typename This::Status
1393 arm_branch_common(unsigned char *view,
1394 const Sized_relobj<32, big_endian>* object,
1395 const Symbol_value<32>* psymval,
1396 elfcpp::Elf_types<32>::Elf_Addr address,
1397 bool has_thumb_bit)
1398 {
1399 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1400 Valtype* wv = reinterpret_cast<Valtype*>(view);
1401 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1402
1403 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
1404 && ((val & 0x0f000000UL) == 0x0a000000UL);
1405 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
1406 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
1407 && ((val & 0x0f000000UL) == 0x0b000000UL);
1408 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
1409 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
1410
1411 if (r_type == elfcpp::R_ARM_CALL)
1412 {
1413 if (!insn_is_uncond_bl && !insn_is_blx)
1414 return This::STATUS_BAD_RELOC;
1415 }
1416 else if (r_type == elfcpp::R_ARM_JUMP24)
1417 {
1418 if (!insn_is_b && !insn_is_cond_bl)
1419 return This::STATUS_BAD_RELOC;
1420 }
1421 else if (r_type == elfcpp::R_ARM_PLT32)
1422 {
1423 if (!insn_is_any_branch)
1424 return This::STATUS_BAD_RELOC;
1425 }
1426 else
1427 gold_unreachable();
1428
1429 Valtype addend = utils::sign_extend<26>(val << 2);
1430 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1431 - address);
1432
1433 // If target has thumb bit set, we need to either turn the BL
1434 // into a BLX (for ARMv5 or above) or generate a stub.
1435 if (x & 1)
1436 {
1437 // Turn BL to BLX.
1438 if (insn_is_uncond_bl)
1439 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
1440 else
1441 return This::STATUS_BAD_RELOC;
1442 }
1443 else
1444 gold_assert(!insn_is_blx);
1445
1446 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
1447 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1448 return (utils::has_overflow<26>(x)
1449 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
1450 }
1451
1452 public:
5e445df6
ILT
1453
1454 // R_ARM_ABS8: S + A
1455 static inline typename This::Status
1456 abs8(unsigned char *view,
1457 const Sized_relobj<32, big_endian>* object,
be8fcb75 1458 const Symbol_value<32>* psymval)
5e445df6
ILT
1459 {
1460 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
1461 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1462 Valtype* wv = reinterpret_cast<Valtype*>(view);
1463 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
1464 Reltype addend = utils::sign_extend<8>(val);
be8fcb75 1465 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
5e445df6
ILT
1466 val = utils::bit_select(val, x, 0xffU);
1467 elfcpp::Swap<8, big_endian>::writeval(wv, val);
1468 return (utils::has_signed_unsigned_overflow<8>(x)
1469 ? This::STATUS_OVERFLOW
1470 : This::STATUS_OKAY);
1471 }
1472
be8fcb75
ILT
1473 // R_ARM_THM_ABS5: S + A
1474 static inline typename This::Status
1475 thm_abs5(unsigned char *view,
1476 const Sized_relobj<32, big_endian>* object,
1477 const Symbol_value<32>* psymval)
1478 {
1479 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1480 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1481 Valtype* wv = reinterpret_cast<Valtype*>(view);
1482 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1483 Reltype addend = (val & 0x7e0U) >> 6;
1484 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1485 val = utils::bit_select(val, x << 6, 0x7e0U);
1486 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1487 return (utils::has_overflow<5>(x)
1488 ? This::STATUS_OVERFLOW
1489 : This::STATUS_OKAY);
1490 }
1491
1492 // R_ARM_ABS12: S + A
1493 static inline typename This::Status
1494 abs12(unsigned char *view,
1495 const Sized_relobj<32, big_endian>* object,
1496 const Symbol_value<32>* psymval)
1497 {
1498 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1499 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1500 Valtype* wv = reinterpret_cast<Valtype*>(view);
1501 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1502 Reltype addend = val & 0x0fffU;
1503 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1504 val = utils::bit_select(val, x, 0x0fffU);
1505 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1506 return (utils::has_overflow<12>(x)
1507 ? This::STATUS_OVERFLOW
1508 : This::STATUS_OKAY);
1509 }
1510
1511 // R_ARM_ABS16: S + A
1512 static inline typename This::Status
1513 abs16(unsigned char *view,
1514 const Sized_relobj<32, big_endian>* object,
1515 const Symbol_value<32>* psymval)
1516 {
1517 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1518 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1519 Valtype* wv = reinterpret_cast<Valtype*>(view);
1520 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1521 Reltype addend = utils::sign_extend<16>(val);
1522 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1523 val = utils::bit_select(val, x, 0xffffU);
1524 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1525 return (utils::has_signed_unsigned_overflow<16>(x)
1526 ? This::STATUS_OVERFLOW
1527 : This::STATUS_OKAY);
1528 }
1529
c121c671
DK
1530 // R_ARM_ABS32: (S + A) | T
1531 static inline typename This::Status
1532 abs32(unsigned char *view,
1533 const Sized_relobj<32, big_endian>* object,
1534 const Symbol_value<32>* psymval,
1535 bool has_thumb_bit)
1536 {
1537 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1538 Valtype* wv = reinterpret_cast<Valtype*>(view);
1539 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1540 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1541 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1542 return This::STATUS_OKAY;
1543 }
1544
1545 // R_ARM_REL32: (S + A) | T - P
1546 static inline typename This::Status
1547 rel32(unsigned char *view,
1548 const Sized_relobj<32, big_endian>* object,
1549 const Symbol_value<32>* psymval,
1550 elfcpp::Elf_types<32>::Elf_Addr address,
1551 bool has_thumb_bit)
1552 {
1553 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1554 Valtype* wv = reinterpret_cast<Valtype*>(view);
1555 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1556 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1557 - address);
1558 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1559 return This::STATUS_OKAY;
1560 }
1561
1562 // R_ARM_THM_CALL: (S + A) | T - P
1563 static inline typename This::Status
1564 thm_call(unsigned char *view,
1565 const Sized_relobj<32, big_endian>* object,
1566 const Symbol_value<32>* psymval,
1567 elfcpp::Elf_types<32>::Elf_Addr address,
1568 bool has_thumb_bit)
1569 {
1570 // A thumb call consists of two instructions.
1571 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1572 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1573 Valtype* wv = reinterpret_cast<Valtype*>(view);
1574 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
1575 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
1576 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
1577 gold_assert((lo & 0xf800) == 0xf800);
1578 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
1579 | ((lo & 0x7ff) << 1));
1580 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1581 - address);
1582
1583 // If target has no thumb bit set, we need to either turn the BL
1584 // into a BLX (for ARMv5 or above) or generate a stub.
1585 if ((x & 1) == 0)
1586 {
1587 // This only works for ARMv5 and above with interworking enabled.
1588 lo &= 0xefff;
1589 }
1590 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
1591 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
1592 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
1593 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
1594 return (utils::has_overflow<23>(x)
1595 ? This::STATUS_OVERFLOW
1596 : This::STATUS_OKAY);
1597 }
1598
1599 // R_ARM_BASE_PREL: B(S) + A - P
1600 static inline typename This::Status
1601 base_prel(unsigned char* view,
1602 elfcpp::Elf_types<32>::Elf_Addr origin,
1603 elfcpp::Elf_types<32>::Elf_Addr address)
1604 {
1605 Base::rel32(view, origin - address);
1606 return STATUS_OKAY;
1607 }
1608
be8fcb75
ILT
1609 // R_ARM_BASE_ABS: B(S) + A
1610 static inline typename This::Status
1611 base_abs(unsigned char* view,
1612 elfcpp::Elf_types<32>::Elf_Addr origin)
1613 {
1614 Base::rel32(view, origin);
1615 return STATUS_OKAY;
1616 }
1617
c121c671
DK
1618 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1619 static inline typename This::Status
1620 got_brel(unsigned char* view,
1621 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
1622 {
1623 Base::rel32(view, got_offset);
1624 return This::STATUS_OKAY;
1625 }
1626
7f5309a5
ILT
1627 // R_ARM_GOT_PREL: GOT(S) + A – P
1628 static inline typename This::Status
1629 got_prel(unsigned char* view,
1630 typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
1631 elfcpp::Elf_types<32>::Elf_Addr address)
1632 {
1633 Base::rel32(view, got_offset - address);
1634 return This::STATUS_OKAY;
1635 }
1636
c121c671
DK
1637 // R_ARM_PLT32: (S + A) | T - P
1638 static inline typename This::Status
1639 plt32(unsigned char *view,
1640 const Sized_relobj<32, big_endian>* object,
1641 const Symbol_value<32>* psymval,
1642 elfcpp::Elf_types<32>::Elf_Addr address,
1643 bool has_thumb_bit)
1644 {
1645 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
1646 address, has_thumb_bit);
1647 }
1648
1649 // R_ARM_CALL: (S + A) | T - P
1650 static inline typename This::Status
1651 call(unsigned char *view,
1652 const Sized_relobj<32, big_endian>* object,
1653 const Symbol_value<32>* psymval,
1654 elfcpp::Elf_types<32>::Elf_Addr address,
1655 bool has_thumb_bit)
1656 {
1657 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
1658 address, has_thumb_bit);
1659 }
1660
1661 // R_ARM_JUMP24: (S + A) | T - P
1662 static inline typename This::Status
1663 jump24(unsigned char *view,
1664 const Sized_relobj<32, big_endian>* object,
1665 const Symbol_value<32>* psymval,
1666 elfcpp::Elf_types<32>::Elf_Addr address,
1667 bool has_thumb_bit)
1668 {
1669 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
1670 address, has_thumb_bit);
1671 }
1672
1673 // R_ARM_PREL: (S + A) | T - P
1674 static inline typename This::Status
1675 prel31(unsigned char *view,
1676 const Sized_relobj<32, big_endian>* object,
1677 const Symbol_value<32>* psymval,
1678 elfcpp::Elf_types<32>::Elf_Addr address,
1679 bool has_thumb_bit)
1680 {
1681 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1682 Valtype* wv = reinterpret_cast<Valtype*>(view);
1683 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1684 Valtype addend = utils::sign_extend<31>(val);
1685 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1686 - address);
1687 val = utils::bit_select(val, x, 0x7fffffffU);
1688 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1689 return (utils::has_overflow<31>(x) ?
1690 This::STATUS_OVERFLOW : This::STATUS_OKAY);
1691 }
fd3c5f0b
ILT
1692
1693 // R_ARM_MOVW_ABS_NC: (S + A) | T
1694 static inline typename This::Status
1695 movw_abs_nc(unsigned char *view,
1696 const Sized_relobj<32, big_endian>* object,
1697 const Symbol_value<32>* psymval,
1698 bool has_thumb_bit)
1699 {
1700 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1701 Valtype* wv = reinterpret_cast<Valtype*>(view);
1702 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1703 Valtype addend = This::extract_arm_movw_movt_addend(val);
1704 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1705 val = This::insert_val_arm_movw_movt(val, x);
1706 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1707 return This::STATUS_OKAY;
1708 }
1709
1710 // R_ARM_MOVT_ABS: S + A
1711 static inline typename This::Status
1712 movt_abs(unsigned char *view,
1713 const Sized_relobj<32, big_endian>* object,
1714 const Symbol_value<32>* psymval)
1715 {
1716 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1717 Valtype* wv = reinterpret_cast<Valtype*>(view);
1718 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1719 Valtype addend = This::extract_arm_movw_movt_addend(val);
1720 Valtype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
1721 val = This::insert_val_arm_movw_movt(val, x);
1722 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1723 return This::STATUS_OKAY;
1724 }
1725
1726 // R_ARM_THM_MOVW_ABS_NC: S + A | T
1727 static inline typename This::Status
1728 thm_movw_abs_nc(unsigned char *view,
1729 const Sized_relobj<32, big_endian>* object,
1730 const Symbol_value<32>* psymval,
1731 bool has_thumb_bit)
1732 {
1733 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1734 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1735 Valtype* wv = reinterpret_cast<Valtype*>(view);
1736 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1737 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
1738 Reltype addend = extract_thumb_movw_movt_addend(val);
1739 Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1740 val = This::insert_val_thumb_movw_movt(val, x);
1741 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1742 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1743 return This::STATUS_OKAY;
1744 }
1745
1746 // R_ARM_THM_MOVT_ABS: S + A
1747 static inline typename This::Status
1748 thm_movt_abs(unsigned char *view,
1749 const Sized_relobj<32, big_endian>* object,
1750 const Symbol_value<32>* psymval)
1751 {
1752 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1753 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1754 Valtype* wv = reinterpret_cast<Valtype*>(view);
1755 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1756 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
1757 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1758 Reltype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
1759 val = This::insert_val_thumb_movw_movt(val, x);
1760 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1761 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1762 return This::STATUS_OKAY;
1763 }
1764
c2a122b6
ILT
1765 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
1766 static inline typename This::Status
1767 movw_prel_nc(unsigned char *view,
1768 const Sized_relobj<32, big_endian>* object,
1769 const Symbol_value<32>* psymval,
1770 elfcpp::Elf_types<32>::Elf_Addr address,
1771 bool has_thumb_bit)
1772 {
1773 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1774 Valtype* wv = reinterpret_cast<Valtype*>(view);
1775 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1776 Valtype addend = This::extract_arm_movw_movt_addend(val);
1777 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1778 - address);
1779 val = This::insert_val_arm_movw_movt(val, x);
1780 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1781 return This::STATUS_OKAY;
1782 }
1783
1784 // R_ARM_MOVT_PREL: S + A - P
1785 static inline typename This::Status
1786 movt_prel(unsigned char *view,
1787 const Sized_relobj<32, big_endian>* object,
1788 const Symbol_value<32>* psymval,
1789 elfcpp::Elf_types<32>::Elf_Addr address)
1790 {
1791 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1792 Valtype* wv = reinterpret_cast<Valtype*>(view);
1793 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1794 Valtype addend = This::extract_arm_movw_movt_addend(val);
1795 Valtype x = (This::arm_symbol_value(object, psymval, addend, 0)
1796 - address) >> 16;
1797 val = This::insert_val_arm_movw_movt(val, x);
1798 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1799 return This::STATUS_OKAY;
1800 }
1801
1802 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
1803 static inline typename This::Status
1804 thm_movw_prel_nc(unsigned char *view,
1805 const Sized_relobj<32, big_endian>* object,
1806 const Symbol_value<32>* psymval,
1807 elfcpp::Elf_types<32>::Elf_Addr address,
1808 bool has_thumb_bit)
1809 {
1810 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1811 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1812 Valtype* wv = reinterpret_cast<Valtype*>(view);
1813 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1814 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1815 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1816 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1817 - address);
1818 val = This::insert_val_thumb_movw_movt(val, x);
1819 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1820 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1821 return This::STATUS_OKAY;
1822 }
1823
1824 // R_ARM_THM_MOVT_PREL: S + A - P
1825 static inline typename This::Status
1826 thm_movt_prel(unsigned char *view,
1827 const Sized_relobj<32, big_endian>* object,
1828 const Symbol_value<32>* psymval,
1829 elfcpp::Elf_types<32>::Elf_Addr address)
1830 {
1831 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1832 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1833 Valtype* wv = reinterpret_cast<Valtype*>(view);
1834 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1835 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1836 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1837 Reltype x = (This::arm_symbol_value(object, psymval, addend, 0)
1838 - address) >> 16;
1839 val = This::insert_val_thumb_movw_movt(val, x);
1840 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1841 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1842 return This::STATUS_OKAY;
1843 }
c121c671
DK
1844};
1845
94cdfcff
DK
1846// Get the GOT section, creating it if necessary.
1847
1848template<bool big_endian>
1849Output_data_got<32, big_endian>*
1850Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
1851{
1852 if (this->got_ == NULL)
1853 {
1854 gold_assert(symtab != NULL && layout != NULL);
1855
1856 this->got_ = new Output_data_got<32, big_endian>();
1857
1858 Output_section* os;
1859 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1860 (elfcpp::SHF_ALLOC
1861 | elfcpp::SHF_WRITE),
1862 this->got_);
1863 os->set_is_relro();
1864
1865 // The old GNU linker creates a .got.plt section. We just
1866 // create another set of data in the .got section. Note that we
1867 // always create a PLT if we create a GOT, although the PLT
1868 // might be empty.
1869 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
1870 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1871 (elfcpp::SHF_ALLOC
1872 | elfcpp::SHF_WRITE),
1873 this->got_plt_);
1874 os->set_is_relro();
1875
1876 // The first three entries are reserved.
1877 this->got_plt_->set_current_data_size(3 * 4);
1878
1879 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1880 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1881 this->got_plt_,
1882 0, 0, elfcpp::STT_OBJECT,
1883 elfcpp::STB_LOCAL,
1884 elfcpp::STV_HIDDEN, 0,
1885 false, false);
1886 }
1887 return this->got_;
1888}
1889
1890// Get the dynamic reloc section, creating it if necessary.
1891
1892template<bool big_endian>
1893typename Target_arm<big_endian>::Reloc_section*
1894Target_arm<big_endian>::rel_dyn_section(Layout* layout)
1895{
1896 if (this->rel_dyn_ == NULL)
1897 {
1898 gold_assert(layout != NULL);
1899 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
1900 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1901 elfcpp::SHF_ALLOC, this->rel_dyn_);
1902 }
1903 return this->rel_dyn_;
1904}
1905
b569affa
DK
1906// Insn_template methods.
1907
1908// Return byte size of an instruction template.
1909
1910size_t
1911Insn_template::size() const
1912{
1913 switch (this->type())
1914 {
1915 case THUMB16_TYPE:
1916 return 2;
1917 case ARM_TYPE:
1918 case THUMB32_TYPE:
1919 case DATA_TYPE:
1920 return 4;
1921 default:
1922 gold_unreachable();
1923 }
1924}
1925
1926// Return alignment of an instruction template.
1927
1928unsigned
1929Insn_template::alignment() const
1930{
1931 switch (this->type())
1932 {
1933 case THUMB16_TYPE:
1934 case THUMB32_TYPE:
1935 return 2;
1936 case ARM_TYPE:
1937 case DATA_TYPE:
1938 return 4;
1939 default:
1940 gold_unreachable();
1941 }
1942}
1943
1944// Stub_template methods.
1945
1946Stub_template::Stub_template(
1947 Stub_type type, const Insn_template* insns,
1948 size_t insn_count)
1949 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
1950 entry_in_thumb_mode_(false), relocs_()
1951{
1952 off_t offset = 0;
1953
1954 // Compute byte size and alignment of stub template.
1955 for (size_t i = 0; i < insn_count; i++)
1956 {
1957 unsigned insn_alignment = insns[i].alignment();
1958 size_t insn_size = insns[i].size();
1959 gold_assert((offset & (insn_alignment - 1)) == 0);
1960 this->alignment_ = std::max(this->alignment_, insn_alignment);
1961 switch (insns[i].type())
1962 {
1963 case Insn_template::THUMB16_TYPE:
1964 if (i == 0)
1965 this->entry_in_thumb_mode_ = true;
1966 break;
1967
1968 case Insn_template::THUMB32_TYPE:
1969 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
1970 this->relocs_.push_back(Reloc(i, offset));
1971 if (i == 0)
1972 this->entry_in_thumb_mode_ = true;
1973 break;
1974
1975 case Insn_template::ARM_TYPE:
1976 // Handle cases where the target is encoded within the
1977 // instruction.
1978 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
1979 this->relocs_.push_back(Reloc(i, offset));
1980 break;
1981
1982 case Insn_template::DATA_TYPE:
1983 // Entry point cannot be data.
1984 gold_assert(i != 0);
1985 this->relocs_.push_back(Reloc(i, offset));
1986 break;
1987
1988 default:
1989 gold_unreachable();
1990 }
1991 offset += insn_size;
1992 }
1993 this->size_ = offset;
1994}
1995
1996// Reloc_stub::Key methods.
1997
1998// Dump a Key as a string for debugging.
1999
2000std::string
2001Reloc_stub::Key::name() const
2002{
2003 if (this->r_sym_ == invalid_index)
2004 {
2005 // Global symbol key name
2006 // <stub-type>:<symbol name>:<addend>.
2007 const std::string sym_name = this->u_.symbol->name();
2008 // We need to print two hex number and two colons. So just add 100 bytes
2009 // to the symbol name size.
2010 size_t len = sym_name.size() + 100;
2011 char* buffer = new char[len];
2012 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
2013 sym_name.c_str(), this->addend_);
2014 gold_assert(c > 0 && c < static_cast<int>(len));
2015 delete[] buffer;
2016 return std::string(buffer);
2017 }
2018 else
2019 {
2020 // local symbol key name
2021 // <stub-type>:<object>:<r_sym>:<addend>.
2022 const size_t len = 200;
2023 char buffer[len];
2024 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
2025 this->u_.relobj, this->r_sym_, this->addend_);
2026 gold_assert(c > 0 && c < static_cast<int>(len));
2027 return std::string(buffer);
2028 }
2029}
2030
2031// Reloc_stub methods.
2032
2033// Determine the type of stub needed, if any, for a relocation of R_TYPE at
2034// LOCATION to DESTINATION.
2035// This code is based on the arm_type_of_stub function in
2036// bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2037// class simple.
2038
2039Stub_type
2040Reloc_stub::stub_type_for_reloc(
2041 unsigned int r_type,
2042 Arm_address location,
2043 Arm_address destination,
2044 bool target_is_thumb)
2045{
2046 Stub_type stub_type = arm_stub_none;
2047
2048 // This is a bit ugly but we want to avoid using a templated class for
2049 // big and little endianities.
2050 bool may_use_blx;
2051 bool should_force_pic_veneer;
2052 bool thumb2;
2053 bool thumb_only;
2054 if (parameters->target().is_big_endian())
2055 {
2056 const Target_arm<true>& big_endian_target =
2057 Target_arm<true>::default_target();
2058 may_use_blx = big_endian_target.may_use_blx();
2059 should_force_pic_veneer = big_endian_target.should_force_pic_veneer();
2060 thumb2 = big_endian_target.using_thumb2();
2061 thumb_only = big_endian_target.using_thumb_only();
2062 }
2063 else
2064 {
2065 const Target_arm<false>& little_endian_target =
2066 Target_arm<false>::default_target();
2067 may_use_blx = little_endian_target.may_use_blx();
2068 should_force_pic_veneer = little_endian_target.should_force_pic_veneer();
2069 thumb2 = little_endian_target.using_thumb2();
2070 thumb_only = little_endian_target.using_thumb_only();
2071 }
2072
2073 int64_t branch_offset = (int64_t)destination - location;
2074
2075 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
2076 {
2077 // Handle cases where:
2078 // - this call goes too far (different Thumb/Thumb2 max
2079 // distance)
2080 // - it's a Thumb->Arm call and blx is not available, or it's a
2081 // Thumb->Arm branch (not bl). A stub is needed in this case.
2082 if ((!thumb2
2083 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2084 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2085 || (thumb2
2086 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2087 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2088 || ((!target_is_thumb)
2089 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2090 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
2091 {
2092 if (target_is_thumb)
2093 {
2094 // Thumb to thumb.
2095 if (!thumb_only)
2096 {
2097 stub_type = (parameters->options().shared() | should_force_pic_veneer)
2098 // PIC stubs.
2099 ? ((may_use_blx
2100 && (r_type == elfcpp::R_ARM_THM_CALL))
2101 // V5T and above. Stub starts with ARM code, so
2102 // we must be able to switch mode before
2103 // reaching it, which is only possible for 'bl'
2104 // (ie R_ARM_THM_CALL relocation).
2105 ? arm_stub_long_branch_any_thumb_pic
2106 // On V4T, use Thumb code only.
2107 : arm_stub_long_branch_v4t_thumb_thumb_pic)
2108
2109 // non-PIC stubs.
2110 : ((may_use_blx
2111 && (r_type == elfcpp::R_ARM_THM_CALL))
2112 ? arm_stub_long_branch_any_any // V5T and above.
2113 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
2114 }
2115 else
2116 {
2117 stub_type = (parameters->options().shared() | should_force_pic_veneer)
2118 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
2119 : arm_stub_long_branch_thumb_only; // non-PIC stub.
2120 }
2121 }
2122 else
2123 {
2124 // Thumb to arm.
2125
2126 // FIXME: We should check that the input section is from an
2127 // object that has interwork enabled.
2128
2129 stub_type = (parameters->options().shared()
2130 || should_force_pic_veneer)
2131 // PIC stubs.
2132 ? ((may_use_blx
2133 && (r_type == elfcpp::R_ARM_THM_CALL))
2134 ? arm_stub_long_branch_any_arm_pic // V5T and above.
2135 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
2136
2137 // non-PIC stubs.
2138 : ((may_use_blx
2139 && (r_type == elfcpp::R_ARM_THM_CALL))
2140 ? arm_stub_long_branch_any_any // V5T and above.
2141 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
2142
2143 // Handle v4t short branches.
2144 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
2145 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
2146 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
2147 stub_type = arm_stub_short_branch_v4t_thumb_arm;
2148 }
2149 }
2150 }
2151 else if (r_type == elfcpp::R_ARM_CALL
2152 || r_type == elfcpp::R_ARM_JUMP24
2153 || r_type == elfcpp::R_ARM_PLT32)
2154 {
2155 if (target_is_thumb)
2156 {
2157 // Arm to thumb.
2158
2159 // FIXME: We should check that the input section is from an
2160 // object that has interwork enabled.
2161
2162 // We have an extra 2-bytes reach because of
2163 // the mode change (bit 24 (H) of BLX encoding).
2164 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
2165 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2166 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
2167 || (r_type == elfcpp::R_ARM_JUMP24)
2168 || (r_type == elfcpp::R_ARM_PLT32))
2169 {
2170 stub_type = (parameters->options().shared()
2171 || should_force_pic_veneer)
2172 // PIC stubs.
2173 ? (may_use_blx
2174 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
2175 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
2176
2177 // non-PIC stubs.
2178 : (may_use_blx
2179 ? arm_stub_long_branch_any_any // V5T and above.
2180 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
2181 }
2182 }
2183 else
2184 {
2185 // Arm to arm.
2186 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
2187 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
2188 {
2189 stub_type = (parameters->options().shared()
2190 || should_force_pic_veneer)
2191 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
2192 : arm_stub_long_branch_any_any; /// non-PIC.
2193 }
2194 }
2195 }
2196
2197 return stub_type;
2198}
2199
2200// Template to implement do_write for a specific target endianity.
2201
2202template<bool big_endian>
2203void inline
2204Reloc_stub::do_fixed_endian_write(unsigned char* view,
2205 section_size_type view_size)
2206{
2207 const Stub_template* stub_template = this->stub_template();
2208 const Insn_template* insns = stub_template->insns();
2209
2210 // FIXME: We do not handle BE8 encoding yet.
2211 unsigned char* pov = view;
2212 for (size_t i = 0; i < stub_template->insn_count(); i++)
2213 {
2214 switch (insns[i].type())
2215 {
2216 case Insn_template::THUMB16_TYPE:
2217 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2218 gold_assert(insns[i].reloc_addend() == 0);
2219 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2220 break;
2221 case Insn_template::THUMB32_TYPE:
2222 {
2223 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2224 uint32_t lo = insns[i].data() & 0xffff;
2225 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2226 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2227 }
2228 break;
2229 case Insn_template::ARM_TYPE:
2230 case Insn_template::DATA_TYPE:
2231 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2232 break;
2233 default:
2234 gold_unreachable();
2235 }
2236 pov += insns[i].size();
2237 }
2238 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2239}
2240
2241// Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2242
2243void
2244Reloc_stub::do_write(unsigned char* view, section_size_type view_size,
2245 bool big_endian)
2246{
2247 if (big_endian)
2248 this->do_fixed_endian_write<true>(view, view_size);
2249 else
2250 this->do_fixed_endian_write<false>(view, view_size);
2251}
2252
2253// Stub_factory methods.
2254
2255Stub_factory::Stub_factory()
2256{
2257 // The instruction template sequences are declared as static
2258 // objects and initialized first time the constructor runs.
2259
2260 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2261 // to reach the stub if necessary.
2262 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
2263 {
2264 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2265 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2266 // dcd R_ARM_ABS32(X)
2267 };
2268
2269 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2270 // available.
2271 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2272 {
2273 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2274 Insn_template::arm_insn(0xe12fff1c), // bx ip
2275 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2276 // dcd R_ARM_ABS32(X)
2277 };
2278
2279 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2280 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
2281 {
2282 Insn_template::thumb16_insn(0xb401), // push {r0}
2283 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2284 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2285 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2286 Insn_template::thumb16_insn(0x4760), // bx ip
2287 Insn_template::thumb16_insn(0xbf00), // nop
2288 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2289 // dcd R_ARM_ABS32(X)
2290 };
2291
2292 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2293 // allowed.
2294 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2295 {
2296 Insn_template::thumb16_insn(0x4778), // bx pc
2297 Insn_template::thumb16_insn(0x46c0), // nop
2298 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2299 Insn_template::arm_insn(0xe12fff1c), // bx ip
2300 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2301 // dcd R_ARM_ABS32(X)
2302 };
2303
2304 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2305 // available.
2306 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2307 {
2308 Insn_template::thumb16_insn(0x4778), // bx pc
2309 Insn_template::thumb16_insn(0x46c0), // nop
2310 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2311 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2312 // dcd R_ARM_ABS32(X)
2313 };
2314
2315 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2316 // one, when the destination is close enough.
2317 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2318 {
2319 Insn_template::thumb16_insn(0x4778), // bx pc
2320 Insn_template::thumb16_insn(0x46c0), // nop
2321 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2322 };
2323
2324 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2325 // blx to reach the stub if necessary.
2326 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
2327 {
2328 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2329 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2330 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2331 // dcd R_ARM_REL32(X-4)
2332 };
2333
2334 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2335 // blx to reach the stub if necessary. We can not add into pc;
2336 // it is not guaranteed to mode switch (different in ARMv6 and
2337 // ARMv7).
2338 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
2339 {
2340 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2341 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2342 Insn_template::arm_insn(0xe12fff1c), // bx ip
2343 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2344 // dcd R_ARM_REL32(X)
2345 };
2346
2347 // V4T ARM -> ARM long branch stub, PIC.
2348 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2349 {
2350 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2351 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2352 Insn_template::arm_insn(0xe12fff1c), // bx ip
2353 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2354 // dcd R_ARM_REL32(X)
2355 };
2356
2357 // V4T Thumb -> ARM long branch stub, PIC.
2358 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2359 {
2360 Insn_template::thumb16_insn(0x4778), // bx pc
2361 Insn_template::thumb16_insn(0x46c0), // nop
2362 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2363 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
2364 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2365 // dcd R_ARM_REL32(X)
2366 };
2367
2368 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
2369 // architectures.
2370 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
2371 {
2372 Insn_template::thumb16_insn(0xb401), // push {r0}
2373 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2374 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
2375 Insn_template::thumb16_insn(0x4484), // add ip, r0
2376 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2377 Insn_template::thumb16_insn(0x4760), // bx ip
2378 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
2379 // dcd R_ARM_REL32(X)
2380 };
2381
2382 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2383 // allowed.
2384 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2385 {
2386 Insn_template::thumb16_insn(0x4778), // bx pc
2387 Insn_template::thumb16_insn(0x46c0), // nop
2388 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2389 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2390 Insn_template::arm_insn(0xe12fff1c), // bx ip
2391 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2392 // dcd R_ARM_REL32(X)
2393 };
2394
2395 // Cortex-A8 erratum-workaround stubs.
2396
2397 // Stub used for conditional branches (which may be beyond +/-1MB away,
2398 // so we can't use a conditional branch to reach this stub).
2399
2400 // original code:
2401 //
2402 // b<cond> X
2403 // after:
2404 //
2405 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
2406 {
2407 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
2408 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
2409 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
2410 // b.w X
2411 };
2412
2413 // Stub used for b.w and bl.w instructions.
2414
2415 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
2416 {
2417 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2418 };
2419
2420 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
2421 {
2422 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2423 };
2424
2425 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
2426 // instruction (which switches to ARM mode) to point to this stub. Jump to
2427 // the real destination using an ARM-mode branch.
2428 const Insn_template elf32_arm_stub_a8_veneer_blx[] =
2429 {
2430 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
2431 };
2432
2433 // Fill in the stub template look-up table. Stub templates are constructed
2434 // per instance of Stub_factory for fast look-up without locking
2435 // in a thread-enabled environment.
2436
2437 this->stub_templates_[arm_stub_none] =
2438 new Stub_template(arm_stub_none, NULL, 0);
2439
2440#define DEF_STUB(x) \
2441 do \
2442 { \
2443 size_t array_size \
2444 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
2445 Stub_type type = arm_stub_##x; \
2446 this->stub_templates_[type] = \
2447 new Stub_template(type, elf32_arm_stub_##x, array_size); \
2448 } \
2449 while (0);
2450
2451 DEF_STUBS
2452#undef DEF_STUB
2453}
2454
56ee5e00
DK
2455// Stub_table methods.
2456
2457// Add a STUB with using KEY. Caller is reponsible for avoid adding
2458// if already a STUB with the same key has been added.
2459
2460template<bool big_endian>
2461void
2462Stub_table<big_endian>::add_reloc_stub(
2463 Reloc_stub* stub,
2464 const Reloc_stub::Key& key)
2465{
2466 const Stub_template* stub_template = stub->stub_template();
2467 gold_assert(stub_template->type() == key.stub_type());
2468 this->reloc_stubs_[key] = stub;
2469 if (this->addralign_ < stub_template->alignment())
2470 this->addralign_ = stub_template->alignment();
2471 this->has_been_changed_ = true;
2472}
2473
2474template<bool big_endian>
2475void
2476Stub_table<big_endian>::relocate_stubs(
2477 const Relocate_info<32, big_endian>* relinfo,
2478 Target_arm<big_endian>* arm_target,
2479 Output_section* output_section,
2480 unsigned char* view,
2481 Arm_address address,
2482 section_size_type view_size)
2483{
2484 // If we are passed a view bigger than the stub table's. we need to
2485 // adjust the view.
2486 gold_assert(address == this->address()
2487 && (view_size
2488 == static_cast<section_size_type>(this->data_size())));
2489
2490 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2491 p != this->reloc_stubs_.end();
2492 ++p)
2493 {
2494 Reloc_stub* stub = p->second;
2495 const Stub_template* stub_template = stub->stub_template();
2496 if (stub_template->reloc_count() != 0)
2497 {
2498 // Adjust view to cover the stub only.
2499 section_size_type offset = stub->offset();
2500 section_size_type stub_size = stub_template->size();
2501 gold_assert(offset + stub_size <= view_size);
2502
2503 arm_target->relocate_stub(stub, relinfo, output_section,
2504 view + offset, address + offset,
2505 stub_size);
2506 }
2507 }
2508}
2509
2510// Reset address and file offset.
2511
2512template<bool big_endian>
2513void
2514Stub_table<big_endian>::do_reset_address_and_file_offset()
2515{
2516 off_t off = 0;
2517 uint64_t max_addralign = 1;
2518 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2519 p != this->reloc_stubs_.end();
2520 ++p)
2521 {
2522 Reloc_stub* stub = p->second;
2523 const Stub_template* stub_template = stub->stub_template();
2524 uint64_t stub_addralign = stub_template->alignment();
2525 max_addralign = std::max(max_addralign, stub_addralign);
2526 off = align_address(off, stub_addralign);
2527 stub->set_offset(off);
2528 stub->reset_destination_address();
2529 off += stub_template->size();
2530 }
2531
2532 this->addralign_ = max_addralign;
2533 this->set_current_data_size_for_child(off);
2534}
2535
2536// Write out the stubs to file.
2537
2538template<bool big_endian>
2539void
2540Stub_table<big_endian>::do_write(Output_file* of)
2541{
2542 off_t offset = this->offset();
2543 const section_size_type oview_size =
2544 convert_to_section_size_type(this->data_size());
2545 unsigned char* const oview = of->get_output_view(offset, oview_size);
2546
2547 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2548 p != this->reloc_stubs_.end();
2549 ++p)
2550 {
2551 Reloc_stub* stub = p->second;
2552 Arm_address address = this->address() + stub->offset();
2553 gold_assert(address
2554 == align_address(address,
2555 stub->stub_template()->alignment()));
2556 stub->write(oview + stub->offset(), stub->stub_template()->size(),
2557 big_endian);
2558 }
2559 of->write_output_view(this->offset(), oview_size, oview);
2560}
2561
10ad9fe5
DK
2562// Arm_input_section methods.
2563
2564// Initialize an Arm_input_section.
2565
2566template<bool big_endian>
2567void
2568Arm_input_section<big_endian>::init()
2569{
2570 Relobj* relobj = this->relobj();
2571 unsigned int shndx = this->shndx();
2572
2573 // Cache these to speed up size and alignment queries. It is too slow
2574 // to call section_addraglin and section_size every time.
2575 this->original_addralign_ = relobj->section_addralign(shndx);
2576 this->original_size_ = relobj->section_size(shndx);
2577
2578 // We want to make this look like the original input section after
2579 // output sections are finalized.
2580 Output_section* os = relobj->output_section(shndx);
2581 off_t offset = relobj->output_section_offset(shndx);
2582 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2583 this->set_address(os->address() + offset);
2584 this->set_file_offset(os->offset() + offset);
2585
2586 this->set_current_data_size(this->original_size_);
2587 this->finalize_data_size();
2588}
2589
2590template<bool big_endian>
2591void
2592Arm_input_section<big_endian>::do_write(Output_file* of)
2593{
2594 // We have to write out the original section content.
2595 section_size_type section_size;
2596 const unsigned char* section_contents =
2597 this->relobj()->section_contents(this->shndx(), &section_size, false);
2598 of->write(this->offset(), section_contents, section_size);
2599
2600 // If this owns a stub table and it is not empty, write it.
2601 if (this->is_stub_table_owner() && !this->stub_table_->empty())
2602 this->stub_table_->write(of);
2603}
2604
2605// Finalize data size.
2606
2607template<bool big_endian>
2608void
2609Arm_input_section<big_endian>::set_final_data_size()
2610{
2611 // If this owns a stub table, finalize its data size as well.
2612 if (this->is_stub_table_owner())
2613 {
2614 uint64_t address = this->address();
2615
2616 // The stub table comes after the original section contents.
2617 address += this->original_size_;
2618 address = align_address(address, this->stub_table_->addralign());
2619 off_t offset = this->offset() + (address - this->address());
2620 this->stub_table_->set_address_and_file_offset(address, offset);
2621 address += this->stub_table_->data_size();
2622 gold_assert(address == this->address() + this->current_data_size());
2623 }
2624
2625 this->set_data_size(this->current_data_size());
2626}
2627
2628// Reset address and file offset.
2629
2630template<bool big_endian>
2631void
2632Arm_input_section<big_endian>::do_reset_address_and_file_offset()
2633{
2634 // Size of the original input section contents.
2635 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2636
2637 // If this is a stub table owner, account for the stub table size.
2638 if (this->is_stub_table_owner())
2639 {
2640 Stub_table<big_endian>* stub_table = this->stub_table_;
2641
2642 // Reset the stub table's address and file offset. The
2643 // current data size for child will be updated after that.
2644 stub_table_->reset_address_and_file_offset();
2645 off = align_address(off, stub_table_->addralign());
2646 off += stub_table->current_data_size();
2647 }
2648
2649 this->set_current_data_size(off);
2650}
2651
94cdfcff
DK
2652// A class to handle the PLT data.
2653
2654template<bool big_endian>
2655class Output_data_plt_arm : public Output_section_data
2656{
2657 public:
2658 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
2659 Reloc_section;
2660
2661 Output_data_plt_arm(Layout*, Output_data_space*);
2662
2663 // Add an entry to the PLT.
2664 void
2665 add_entry(Symbol* gsym);
2666
2667 // Return the .rel.plt section data.
2668 const Reloc_section*
2669 rel_plt() const
2670 { return this->rel_; }
2671
2672 protected:
2673 void
2674 do_adjust_output_section(Output_section* os);
2675
2676 // Write to a map file.
2677 void
2678 do_print_to_mapfile(Mapfile* mapfile) const
2679 { mapfile->print_output_data(this, _("** PLT")); }
2680
2681 private:
2682 // Template for the first PLT entry.
2683 static const uint32_t first_plt_entry[5];
2684
2685 // Template for subsequent PLT entries.
2686 static const uint32_t plt_entry[3];
2687
2688 // Set the final size.
2689 void
2690 set_final_data_size()
2691 {
2692 this->set_data_size(sizeof(first_plt_entry)
2693 + this->count_ * sizeof(plt_entry));
2694 }
2695
2696 // Write out the PLT data.
2697 void
2698 do_write(Output_file*);
2699
2700 // The reloc section.
2701 Reloc_section* rel_;
2702 // The .got.plt section.
2703 Output_data_space* got_plt_;
2704 // The number of PLT entries.
2705 unsigned int count_;
2706};
2707
2708// Create the PLT section. The ordinary .got section is an argument,
2709// since we need to refer to the start. We also create our own .got
2710// section just for PLT entries.
2711
2712template<bool big_endian>
2713Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
2714 Output_data_space* got_plt)
2715 : Output_section_data(4), got_plt_(got_plt), count_(0)
2716{
2717 this->rel_ = new Reloc_section(false);
2718 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2719 elfcpp::SHF_ALLOC, this->rel_);
2720}
2721
2722template<bool big_endian>
2723void
2724Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
2725{
2726 os->set_entsize(0);
2727}
2728
2729// Add an entry to the PLT.
2730
2731template<bool big_endian>
2732void
2733Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
2734{
2735 gold_assert(!gsym->has_plt_offset());
2736
2737 // Note that when setting the PLT offset we skip the initial
2738 // reserved PLT entry.
2739 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
2740 + sizeof(first_plt_entry));
2741
2742 ++this->count_;
2743
2744 section_offset_type got_offset = this->got_plt_->current_data_size();
2745
2746 // Every PLT entry needs a GOT entry which points back to the PLT
2747 // entry (this will be changed by the dynamic linker, normally
2748 // lazily when the function is called).
2749 this->got_plt_->set_current_data_size(got_offset + 4);
2750
2751 // Every PLT entry needs a reloc.
2752 gsym->set_needs_dynsym_entry();
2753 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
2754 got_offset);
2755
2756 // Note that we don't need to save the symbol. The contents of the
2757 // PLT are independent of which symbols are used. The symbols only
2758 // appear in the relocations.
2759}
2760
2761// ARM PLTs.
2762// FIXME: This is not very flexible. Right now this has only been tested
2763// on armv5te. If we are to support additional architecture features like
2764// Thumb-2 or BE8, we need to make this more flexible like GNU ld.
2765
2766// The first entry in the PLT.
2767template<bool big_endian>
2768const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
2769{
2770 0xe52de004, // str lr, [sp, #-4]!
2771 0xe59fe004, // ldr lr, [pc, #4]
2772 0xe08fe00e, // add lr, pc, lr
2773 0xe5bef008, // ldr pc, [lr, #8]!
2774 0x00000000, // &GOT[0] - .
2775};
2776
2777// Subsequent entries in the PLT.
2778
2779template<bool big_endian>
2780const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
2781{
2782 0xe28fc600, // add ip, pc, #0xNN00000
2783 0xe28cca00, // add ip, ip, #0xNN000
2784 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
2785};
2786
2787// Write out the PLT. This uses the hand-coded instructions above,
2788// and adjusts them as needed. This is all specified by the arm ELF
2789// Processor Supplement.
2790
2791template<bool big_endian>
2792void
2793Output_data_plt_arm<big_endian>::do_write(Output_file* of)
2794{
2795 const off_t offset = this->offset();
2796 const section_size_type oview_size =
2797 convert_to_section_size_type(this->data_size());
2798 unsigned char* const oview = of->get_output_view(offset, oview_size);
2799
2800 const off_t got_file_offset = this->got_plt_->offset();
2801 const section_size_type got_size =
2802 convert_to_section_size_type(this->got_plt_->data_size());
2803 unsigned char* const got_view = of->get_output_view(got_file_offset,
2804 got_size);
2805 unsigned char* pov = oview;
2806
2807 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
2808 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
2809
2810 // Write first PLT entry. All but the last word are constants.
2811 const size_t num_first_plt_words = (sizeof(first_plt_entry)
2812 / sizeof(plt_entry[0]));
2813 for (size_t i = 0; i < num_first_plt_words - 1; i++)
2814 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
2815 // Last word in first PLT entry is &GOT[0] - .
2816 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
2817 got_address - (plt_address + 16));
2818 pov += sizeof(first_plt_entry);
2819
2820 unsigned char* got_pov = got_view;
2821
2822 memset(got_pov, 0, 12);
2823 got_pov += 12;
2824
2825 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
2826 unsigned int plt_offset = sizeof(first_plt_entry);
2827 unsigned int plt_rel_offset = 0;
2828 unsigned int got_offset = 12;
2829 const unsigned int count = this->count_;
2830 for (unsigned int i = 0;
2831 i < count;
2832 ++i,
2833 pov += sizeof(plt_entry),
2834 got_pov += 4,
2835 plt_offset += sizeof(plt_entry),
2836 plt_rel_offset += rel_size,
2837 got_offset += 4)
2838 {
2839 // Set and adjust the PLT entry itself.
2840 int32_t offset = ((got_address + got_offset)
2841 - (plt_address + plt_offset + 8));
2842
2843 gold_assert(offset >= 0 && offset < 0x0fffffff);
2844 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
2845 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
2846 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
2847 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
2848 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
2849 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
2850
2851 // Set the entry in the GOT.
2852 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
2853 }
2854
2855 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2856 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2857
2858 of->write_output_view(offset, oview_size, oview);
2859 of->write_output_view(got_file_offset, got_size, got_view);
2860}
2861
2862// Create a PLT entry for a global symbol.
2863
2864template<bool big_endian>
2865void
2866Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
2867 Symbol* gsym)
2868{
2869 if (gsym->has_plt_offset())
2870 return;
2871
2872 if (this->plt_ == NULL)
2873 {
2874 // Create the GOT sections first.
2875 this->got_section(symtab, layout);
2876
2877 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
2878 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2879 (elfcpp::SHF_ALLOC
2880 | elfcpp::SHF_EXECINSTR),
2881 this->plt_);
2882 }
2883 this->plt_->add_entry(gsym);
2884}
2885
4a657b0d
DK
2886// Report an unsupported relocation against a local symbol.
2887
2888template<bool big_endian>
2889void
2890Target_arm<big_endian>::Scan::unsupported_reloc_local(
2891 Sized_relobj<32, big_endian>* object,
2892 unsigned int r_type)
2893{
2894 gold_error(_("%s: unsupported reloc %u against local symbol"),
2895 object->name().c_str(), r_type);
2896}
2897
bec53400
DK
2898// We are about to emit a dynamic relocation of type R_TYPE. If the
2899// dynamic linker does not support it, issue an error. The GNU linker
2900// only issues a non-PIC error for an allocated read-only section.
2901// Here we know the section is allocated, but we don't know that it is
2902// read-only. But we check for all the relocation types which the
2903// glibc dynamic linker supports, so it seems appropriate to issue an
2904// error even if the section is not read-only.
2905
2906template<bool big_endian>
2907void
2908Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
2909 unsigned int r_type)
2910{
2911 switch (r_type)
2912 {
2913 // These are the relocation types supported by glibc for ARM.
2914 case elfcpp::R_ARM_RELATIVE:
2915 case elfcpp::R_ARM_COPY:
2916 case elfcpp::R_ARM_GLOB_DAT:
2917 case elfcpp::R_ARM_JUMP_SLOT:
2918 case elfcpp::R_ARM_ABS32:
be8fcb75 2919 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
2920 case elfcpp::R_ARM_PC24:
2921 // FIXME: The following 3 types are not supported by Android's dynamic
2922 // linker.
2923 case elfcpp::R_ARM_TLS_DTPMOD32:
2924 case elfcpp::R_ARM_TLS_DTPOFF32:
2925 case elfcpp::R_ARM_TLS_TPOFF32:
2926 return;
2927
2928 default:
2929 // This prevents us from issuing more than one error per reloc
2930 // section. But we can still wind up issuing more than one
2931 // error per object file.
2932 if (this->issued_non_pic_error_)
2933 return;
2934 object->error(_("requires unsupported dynamic reloc; "
2935 "recompile with -fPIC"));
2936 this->issued_non_pic_error_ = true;
2937 return;
2938
2939 case elfcpp::R_ARM_NONE:
2940 gold_unreachable();
2941 }
2942}
2943
4a657b0d 2944// Scan a relocation for a local symbol.
bec53400
DK
2945// FIXME: This only handles a subset of relocation types used by Android
2946// on ARM v5te devices.
4a657b0d
DK
2947
2948template<bool big_endian>
2949inline void
2950Target_arm<big_endian>::Scan::local(const General_options&,
bec53400
DK
2951 Symbol_table* symtab,
2952 Layout* layout,
2953 Target_arm* target,
4a657b0d 2954 Sized_relobj<32, big_endian>* object,
bec53400
DK
2955 unsigned int data_shndx,
2956 Output_section* output_section,
2957 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
2958 unsigned int r_type,
2959 const elfcpp::Sym<32, big_endian>&)
2960{
2961 r_type = get_real_reloc_type(r_type);
2962 switch (r_type)
2963 {
2964 case elfcpp::R_ARM_NONE:
2965 break;
2966
bec53400 2967 case elfcpp::R_ARM_ABS32:
be8fcb75 2968 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
2969 // If building a shared library (or a position-independent
2970 // executable), we need to create a dynamic relocation for
2971 // this location. The relocation applied at link time will
2972 // apply the link-time value, so we flag the location with
2973 // an R_ARM_RELATIVE relocation so the dynamic loader can
2974 // relocate it easily.
2975 if (parameters->options().output_is_position_independent())
2976 {
2977 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2978 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
2979 // If we are to add more other reloc types than R_ARM_ABS32,
2980 // we need to add check_non_pic(object, r_type) here.
2981 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
2982 output_section, data_shndx,
2983 reloc.get_r_offset());
2984 }
2985 break;
2986
2987 case elfcpp::R_ARM_REL32:
2988 case elfcpp::R_ARM_THM_CALL:
2989 case elfcpp::R_ARM_CALL:
2990 case elfcpp::R_ARM_PREL31:
2991 case elfcpp::R_ARM_JUMP24:
2992 case elfcpp::R_ARM_PLT32:
be8fcb75
ILT
2993 case elfcpp::R_ARM_THM_ABS5:
2994 case elfcpp::R_ARM_ABS8:
2995 case elfcpp::R_ARM_ABS12:
2996 case elfcpp::R_ARM_ABS16:
2997 case elfcpp::R_ARM_BASE_ABS:
fd3c5f0b
ILT
2998 case elfcpp::R_ARM_MOVW_ABS_NC:
2999 case elfcpp::R_ARM_MOVT_ABS:
3000 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3001 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
3002 case elfcpp::R_ARM_MOVW_PREL_NC:
3003 case elfcpp::R_ARM_MOVT_PREL:
3004 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3005 case elfcpp::R_ARM_THM_MOVT_PREL:
bec53400
DK
3006 break;
3007
3008 case elfcpp::R_ARM_GOTOFF32:
3009 // We need a GOT section:
3010 target->got_section(symtab, layout);
3011 break;
3012
3013 case elfcpp::R_ARM_BASE_PREL:
3014 // FIXME: What about this?
3015 break;
3016
3017 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 3018 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
3019 {
3020 // The symbol requires a GOT entry.
3021 Output_data_got<32, big_endian>* got =
3022 target->got_section(symtab, layout);
3023 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3024 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
3025 {
3026 // If we are generating a shared object, we need to add a
3027 // dynamic RELATIVE relocation for this symbol's GOT entry.
3028 if (parameters->options().output_is_position_independent())
3029 {
3030 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3031 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3032 rel_dyn->add_local_relative(
3033 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
3034 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
3035 }
3036 }
3037 }
3038 break;
3039
3040 case elfcpp::R_ARM_TARGET1:
3041 // This should have been mapped to another type already.
3042 // Fall through.
3043 case elfcpp::R_ARM_COPY:
3044 case elfcpp::R_ARM_GLOB_DAT:
3045 case elfcpp::R_ARM_JUMP_SLOT:
3046 case elfcpp::R_ARM_RELATIVE:
3047 // These are relocations which should only be seen by the
3048 // dynamic linker, and should never be seen here.
3049 gold_error(_("%s: unexpected reloc %u in object file"),
3050 object->name().c_str(), r_type);
3051 break;
3052
4a657b0d
DK
3053 default:
3054 unsupported_reloc_local(object, r_type);
3055 break;
3056 }
3057}
3058
3059// Report an unsupported relocation against a global symbol.
3060
3061template<bool big_endian>
3062void
3063Target_arm<big_endian>::Scan::unsupported_reloc_global(
3064 Sized_relobj<32, big_endian>* object,
3065 unsigned int r_type,
3066 Symbol* gsym)
3067{
3068 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3069 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3070}
3071
3072// Scan a relocation for a global symbol.
bec53400
DK
3073// FIXME: This only handles a subset of relocation types used by Android
3074// on ARM v5te devices.
4a657b0d
DK
3075
3076template<bool big_endian>
3077inline void
3078Target_arm<big_endian>::Scan::global(const General_options&,
bec53400
DK
3079 Symbol_table* symtab,
3080 Layout* layout,
3081 Target_arm* target,
4a657b0d 3082 Sized_relobj<32, big_endian>* object,
bec53400
DK
3083 unsigned int data_shndx,
3084 Output_section* output_section,
3085 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
3086 unsigned int r_type,
3087 Symbol* gsym)
3088{
3089 r_type = get_real_reloc_type(r_type);
3090 switch (r_type)
3091 {
3092 case elfcpp::R_ARM_NONE:
3093 break;
3094
bec53400 3095 case elfcpp::R_ARM_ABS32:
be8fcb75 3096 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
3097 {
3098 // Make a dynamic relocation if necessary.
3099 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
3100 {
3101 if (target->may_need_copy_reloc(gsym))
3102 {
3103 target->copy_reloc(symtab, layout, object,
3104 data_shndx, output_section, gsym, reloc);
3105 }
3106 else if (gsym->can_use_relative_reloc(false))
3107 {
3108 // If we are to add more other reloc types than R_ARM_ABS32,
3109 // we need to add check_non_pic(object, r_type) here.
3110 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3111 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
3112 output_section, object,
3113 data_shndx, reloc.get_r_offset());
3114 }
3115 else
3116 {
3117 // If we are to add more other reloc types than R_ARM_ABS32,
3118 // we need to add check_non_pic(object, r_type) here.
3119 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3120 rel_dyn->add_global(gsym, r_type, output_section, object,
3121 data_shndx, reloc.get_r_offset());
3122 }
3123 }
3124 }
3125 break;
3126
fd3c5f0b
ILT
3127 case elfcpp::R_ARM_MOVW_ABS_NC:
3128 case elfcpp::R_ARM_MOVT_ABS:
3129 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3130 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
3131 case elfcpp::R_ARM_MOVW_PREL_NC:
3132 case elfcpp::R_ARM_MOVT_PREL:
3133 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3134 case elfcpp::R_ARM_THM_MOVT_PREL:
fd3c5f0b
ILT
3135 break;
3136
be8fcb75
ILT
3137 case elfcpp::R_ARM_THM_ABS5:
3138 case elfcpp::R_ARM_ABS8:
3139 case elfcpp::R_ARM_ABS12:
3140 case elfcpp::R_ARM_ABS16:
3141 case elfcpp::R_ARM_BASE_ABS:
3142 {
3143 // No dynamic relocs of this kinds.
3144 // Report the error in case of PIC.
3145 int flags = Symbol::NON_PIC_REF;
3146 if (gsym->type() == elfcpp::STT_FUNC
3147 || gsym->type() == elfcpp::STT_ARM_TFUNC)
3148 flags |= Symbol::FUNCTION_CALL;
3149 if (gsym->needs_dynamic_reloc(flags))
3150 check_non_pic(object, r_type);
3151 }
3152 break;
3153
bec53400
DK
3154 case elfcpp::R_ARM_REL32:
3155 case elfcpp::R_ARM_PREL31:
3156 {
3157 // Make a dynamic relocation if necessary.
3158 int flags = Symbol::NON_PIC_REF;
3159 if (gsym->needs_dynamic_reloc(flags))
3160 {
3161 if (target->may_need_copy_reloc(gsym))
3162 {
3163 target->copy_reloc(symtab, layout, object,
3164 data_shndx, output_section, gsym, reloc);
3165 }
3166 else
3167 {
3168 check_non_pic(object, r_type);
3169 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3170 rel_dyn->add_global(gsym, r_type, output_section, object,
3171 data_shndx, reloc.get_r_offset());
3172 }
3173 }
3174 }
3175 break;
3176
3177 case elfcpp::R_ARM_JUMP24:
3178 case elfcpp::R_ARM_THM_CALL:
3179 case elfcpp::R_ARM_CALL:
3180 {
3181 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
3182 target->make_plt_entry(symtab, layout, gsym);
3183 // Make a dynamic relocation if necessary.
3184 int flags = Symbol::NON_PIC_REF;
3185 if (gsym->type() == elfcpp::STT_FUNC
07800fab 3186 || gsym->type() == elfcpp::STT_ARM_TFUNC)
bec53400
DK
3187 flags |= Symbol::FUNCTION_CALL;
3188 if (gsym->needs_dynamic_reloc(flags))
3189 {
3190 if (target->may_need_copy_reloc(gsym))
3191 {
3192 target->copy_reloc(symtab, layout, object,
3193 data_shndx, output_section, gsym,
3194 reloc);
3195 }
3196 else
3197 {
3198 check_non_pic(object, r_type);
3199 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3200 rel_dyn->add_global(gsym, r_type, output_section, object,
3201 data_shndx, reloc.get_r_offset());
3202 }
3203 }
3204 }
3205 break;
3206
3207 case elfcpp::R_ARM_PLT32:
3208 // If the symbol is fully resolved, this is just a relative
3209 // local reloc. Otherwise we need a PLT entry.
3210 if (gsym->final_value_is_known())
3211 break;
3212 // If building a shared library, we can also skip the PLT entry
3213 // if the symbol is defined in the output file and is protected
3214 // or hidden.
3215 if (gsym->is_defined()
3216 && !gsym->is_from_dynobj()
3217 && !gsym->is_preemptible())
3218 break;
3219 target->make_plt_entry(symtab, layout, gsym);
3220 break;
3221
3222 case elfcpp::R_ARM_GOTOFF32:
3223 // We need a GOT section.
3224 target->got_section(symtab, layout);
3225 break;
3226
3227 case elfcpp::R_ARM_BASE_PREL:
3228 // FIXME: What about this?
3229 break;
3230
3231 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 3232 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
3233 {
3234 // The symbol requires a GOT entry.
3235 Output_data_got<32, big_endian>* got =
3236 target->got_section(symtab, layout);
3237 if (gsym->final_value_is_known())
3238 got->add_global(gsym, GOT_TYPE_STANDARD);
3239 else
3240 {
3241 // If this symbol is not fully resolved, we need to add a
3242 // GOT entry with a dynamic relocation.
3243 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3244 if (gsym->is_from_dynobj()
3245 || gsym->is_undefined()
3246 || gsym->is_preemptible())
3247 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
3248 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
3249 else
3250 {
3251 if (got->add_global(gsym, GOT_TYPE_STANDARD))
3252 rel_dyn->add_global_relative(
3253 gsym, elfcpp::R_ARM_RELATIVE, got,
3254 gsym->got_offset(GOT_TYPE_STANDARD));
3255 }
3256 }
3257 }
3258 break;
3259
3260 case elfcpp::R_ARM_TARGET1:
3261 // This should have been mapped to another type already.
3262 // Fall through.
3263 case elfcpp::R_ARM_COPY:
3264 case elfcpp::R_ARM_GLOB_DAT:
3265 case elfcpp::R_ARM_JUMP_SLOT:
3266 case elfcpp::R_ARM_RELATIVE:
3267 // These are relocations which should only be seen by the
3268 // dynamic linker, and should never be seen here.
3269 gold_error(_("%s: unexpected reloc %u in object file"),
3270 object->name().c_str(), r_type);
3271 break;
3272
4a657b0d
DK
3273 default:
3274 unsupported_reloc_global(object, r_type, gsym);
3275 break;
3276 }
3277}
3278
3279// Process relocations for gc.
3280
3281template<bool big_endian>
3282void
3283Target_arm<big_endian>::gc_process_relocs(const General_options& options,
3284 Symbol_table* symtab,
3285 Layout* layout,
3286 Sized_relobj<32, big_endian>* object,
3287 unsigned int data_shndx,
3288 unsigned int,
3289 const unsigned char* prelocs,
3290 size_t reloc_count,
3291 Output_section* output_section,
3292 bool needs_special_offset_handling,
3293 size_t local_symbol_count,
3294 const unsigned char* plocal_symbols)
3295{
3296 typedef Target_arm<big_endian> Arm;
3297 typedef typename Target_arm<big_endian>::Scan Scan;
3298
3299 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
3300 options,
3301 symtab,
3302 layout,
3303 this,
3304 object,
3305 data_shndx,
3306 prelocs,
3307 reloc_count,
3308 output_section,
3309 needs_special_offset_handling,
3310 local_symbol_count,
3311 plocal_symbols);
3312}
3313
3314// Scan relocations for a section.
3315
3316template<bool big_endian>
3317void
3318Target_arm<big_endian>::scan_relocs(const General_options& options,
3319 Symbol_table* symtab,
3320 Layout* layout,
3321 Sized_relobj<32, big_endian>* object,
3322 unsigned int data_shndx,
3323 unsigned int sh_type,
3324 const unsigned char* prelocs,
3325 size_t reloc_count,
3326 Output_section* output_section,
3327 bool needs_special_offset_handling,
3328 size_t local_symbol_count,
3329 const unsigned char* plocal_symbols)
3330{
3331 typedef typename Target_arm<big_endian>::Scan Scan;
3332 if (sh_type == elfcpp::SHT_RELA)
3333 {
3334 gold_error(_("%s: unsupported RELA reloc section"),
3335 object->name().c_str());
3336 return;
3337 }
3338
3339 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
3340 options,
3341 symtab,
3342 layout,
3343 this,
3344 object,
3345 data_shndx,
3346 prelocs,
3347 reloc_count,
3348 output_section,
3349 needs_special_offset_handling,
3350 local_symbol_count,
3351 plocal_symbols);
3352}
3353
3354// Finalize the sections.
3355
3356template<bool big_endian>
3357void
94cdfcff 3358Target_arm<big_endian>::do_finalize_sections(Layout* layout)
4a657b0d 3359{
94cdfcff
DK
3360 // Fill in some more dynamic tags.
3361 Output_data_dynamic* const odyn = layout->dynamic_data();
3362 if (odyn != NULL)
3363 {
3364 if (this->got_plt_ != NULL)
3365 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
3366
3367 if (this->plt_ != NULL)
3368 {
3369 const Output_data* od = this->plt_->rel_plt();
3370 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
3371 odyn->add_section_address(elfcpp::DT_JMPREL, od);
3372 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
3373 }
3374
3375 if (this->rel_dyn_ != NULL)
3376 {
3377 const Output_data* od = this->rel_dyn_;
3378 odyn->add_section_address(elfcpp::DT_REL, od);
3379 odyn->add_section_size(elfcpp::DT_RELSZ, od);
3380 odyn->add_constant(elfcpp::DT_RELENT,
3381 elfcpp::Elf_sizes<32>::rel_size);
3382 }
3383
3384 if (!parameters->options().shared())
3385 {
3386 // The value of the DT_DEBUG tag is filled in by the dynamic
3387 // linker at run time, and used by the debugger.
3388 odyn->add_constant(elfcpp::DT_DEBUG, 0);
3389 }
3390 }
3391
3392 // Emit any relocs we saved in an attempt to avoid generating COPY
3393 // relocs.
3394 if (this->copy_relocs_.any_saved_relocs())
3395 this->copy_relocs_.emit(this->rel_dyn_section(layout));
11af873f
DK
3396
3397 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
3398 // the .ARM.exidx section.
3399 if (!layout->script_options()->saw_phdrs_clause()
3400 && !parameters->options().relocatable())
3401 {
3402 Output_section* exidx_section =
3403 layout->find_output_section(".ARM.exidx");
3404
3405 if (exidx_section != NULL
3406 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
3407 {
3408 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
3409 == NULL);
3410 Output_segment* exidx_segment =
3411 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
3412 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
3413 }
3414 }
4a657b0d
DK
3415}
3416
bec53400
DK
3417// Return whether a direct absolute static relocation needs to be applied.
3418// In cases where Scan::local() or Scan::global() has created
3419// a dynamic relocation other than R_ARM_RELATIVE, the addend
3420// of the relocation is carried in the data, and we must not
3421// apply the static relocation.
3422
3423template<bool big_endian>
3424inline bool
3425Target_arm<big_endian>::Relocate::should_apply_static_reloc(
3426 const Sized_symbol<32>* gsym,
3427 int ref_flags,
3428 bool is_32bit,
3429 Output_section* output_section)
3430{
3431 // If the output section is not allocated, then we didn't call
3432 // scan_relocs, we didn't create a dynamic reloc, and we must apply
3433 // the reloc here.
3434 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
3435 return true;
3436
3437 // For local symbols, we will have created a non-RELATIVE dynamic
3438 // relocation only if (a) the output is position independent,
3439 // (b) the relocation is absolute (not pc- or segment-relative), and
3440 // (c) the relocation is not 32 bits wide.
3441 if (gsym == NULL)
3442 return !(parameters->options().output_is_position_independent()
3443 && (ref_flags & Symbol::ABSOLUTE_REF)
3444 && !is_32bit);
3445
3446 // For global symbols, we use the same helper routines used in the
3447 // scan pass. If we did not create a dynamic relocation, or if we
3448 // created a RELATIVE dynamic relocation, we should apply the static
3449 // relocation.
3450 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
3451 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
3452 && gsym->can_use_relative_reloc(ref_flags
3453 & Symbol::FUNCTION_CALL);
3454 return !has_dyn || is_rel;
3455}
3456
4a657b0d
DK
3457// Perform a relocation.
3458
3459template<bool big_endian>
3460inline bool
3461Target_arm<big_endian>::Relocate::relocate(
c121c671
DK
3462 const Relocate_info<32, big_endian>* relinfo,
3463 Target_arm* target,
3464 Output_section *output_section,
3465 size_t relnum,
3466 const elfcpp::Rel<32, big_endian>& rel,
4a657b0d 3467 unsigned int r_type,
c121c671
DK
3468 const Sized_symbol<32>* gsym,
3469 const Symbol_value<32>* psymval,
3470 unsigned char* view,
3471 elfcpp::Elf_types<32>::Elf_Addr address,
4a657b0d
DK
3472 section_size_type /* view_size */ )
3473{
c121c671
DK
3474 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
3475
3476 r_type = get_real_reloc_type(r_type);
3477
3478 // If this the symbol may be a Thumb function, set thumb bit to 1.
3479 bool has_thumb_bit = ((gsym != NULL)
3480 && (gsym->type() == elfcpp::STT_FUNC
3481 || gsym->type() == elfcpp::STT_ARM_TFUNC));
3482
3483 // Pick the value to use for symbols defined in shared objects.
3484 Symbol_value<32> symval;
3485 if (gsym != NULL
3486 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
3487 {
3488 symval.set_output_value(target->plt_section()->address()
3489 + gsym->plt_offset());
3490 psymval = &symval;
3491 has_thumb_bit = 0;
3492 }
3493
3494 const Sized_relobj<32, big_endian>* object = relinfo->object;
3495
3496 // Get the GOT offset if needed.
3497 // The GOT pointer points to the end of the GOT section.
3498 // We need to subtract the size of the GOT section to get
3499 // the actual offset to use in the relocation.
3500 bool have_got_offset = false;
3501 unsigned int got_offset = 0;
3502 switch (r_type)
3503 {
3504 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 3505 case elfcpp::R_ARM_GOT_PREL:
c121c671
DK
3506 if (gsym != NULL)
3507 {
3508 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3509 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
3510 - target->got_size());
3511 }
3512 else
3513 {
3514 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
3515 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3516 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3517 - target->got_size());
3518 }
3519 have_got_offset = true;
3520 break;
3521
3522 default:
3523 break;
3524 }
3525
3526 typename Arm_relocate_functions::Status reloc_status =
3527 Arm_relocate_functions::STATUS_OKAY;
4a657b0d
DK
3528 switch (r_type)
3529 {
3530 case elfcpp::R_ARM_NONE:
3531 break;
3532
5e445df6
ILT
3533 case elfcpp::R_ARM_ABS8:
3534 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3535 output_section))
be8fcb75
ILT
3536 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
3537 break;
3538
3539 case elfcpp::R_ARM_ABS12:
3540 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3541 output_section))
3542 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
3543 break;
3544
3545 case elfcpp::R_ARM_ABS16:
3546 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3547 output_section))
3548 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5e445df6
ILT
3549 break;
3550
c121c671
DK
3551 case elfcpp::R_ARM_ABS32:
3552 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3553 output_section))
3554 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
3555 has_thumb_bit);
3556 break;
3557
be8fcb75
ILT
3558 case elfcpp::R_ARM_ABS32_NOI:
3559 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3560 output_section))
3561 // No thumb bit for this relocation: (S + A)
3562 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
3563 false);
3564 break;
3565
fd3c5f0b
ILT
3566 case elfcpp::R_ARM_MOVW_ABS_NC:
3567 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3568 output_section))
3569 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
3570 psymval,
3571 has_thumb_bit);
3572 else
3573 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
3574 "a shared object; recompile with -fPIC"));
3575 break;
3576
3577 case elfcpp::R_ARM_MOVT_ABS:
3578 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3579 output_section))
3580 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
3581 else
3582 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
3583 "a shared object; recompile with -fPIC"));
3584 break;
3585
3586 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3587 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3588 output_section))
3589 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
3590 psymval,
3591 has_thumb_bit);
3592 else
3593 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
3594 "making a shared object; recompile with -fPIC"));
3595 break;
3596
3597 case elfcpp::R_ARM_THM_MOVT_ABS:
3598 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3599 output_section))
3600 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
3601 psymval);
3602 else
3603 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
3604 "making a shared object; recompile with -fPIC"));
3605 break;
3606
c2a122b6
ILT
3607 case elfcpp::R_ARM_MOVW_PREL_NC:
3608 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
3609 psymval, address,
3610 has_thumb_bit);
3611 break;
3612
3613 case elfcpp::R_ARM_MOVT_PREL:
3614 reloc_status = Arm_relocate_functions::movt_prel(view, object,
3615 psymval, address);
3616 break;
3617
3618 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3619 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
3620 psymval, address,
3621 has_thumb_bit);
3622 break;
3623
3624 case elfcpp::R_ARM_THM_MOVT_PREL:
3625 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
3626 psymval, address);
3627 break;
3628
c121c671
DK
3629 case elfcpp::R_ARM_REL32:
3630 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
3631 address, has_thumb_bit);
3632 break;
3633
be8fcb75
ILT
3634 case elfcpp::R_ARM_THM_ABS5:
3635 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3636 output_section))
3637 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
3638 break;
3639
c121c671
DK
3640 case elfcpp::R_ARM_THM_CALL:
3641 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
3642 address, has_thumb_bit);
3643 break;
3644
3645 case elfcpp::R_ARM_GOTOFF32:
3646 {
3647 elfcpp::Elf_types<32>::Elf_Addr got_origin;
3648 got_origin = target->got_plt_section()->address();
3649 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
3650 got_origin, has_thumb_bit);
3651 }
3652 break;
3653
3654 case elfcpp::R_ARM_BASE_PREL:
3655 {
3656 uint32_t origin;
3657 // Get the addressing origin of the output segment defining the
3658 // symbol gsym (AAELF 4.6.1.2 Relocation types)
3659 gold_assert(gsym != NULL);
3660 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
3661 origin = gsym->output_segment()->vaddr();
3662 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
3663 origin = gsym->output_data()->address();
3664 else
3665 {
3666 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3667 _("cannot find origin of R_ARM_BASE_PREL"));
3668 return true;
3669 }
3670 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
3671 }
3672 break;
3673
be8fcb75
ILT
3674 case elfcpp::R_ARM_BASE_ABS:
3675 {
3676 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3677 output_section))
3678 break;
3679
3680 uint32_t origin;
3681 // Get the addressing origin of the output segment defining
3682 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
3683 if (gsym == NULL)
3684 // R_ARM_BASE_ABS with the NULL symbol will give the
3685 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
3686 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
3687 origin = target->got_plt_section()->address();
3688 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
3689 origin = gsym->output_segment()->vaddr();
3690 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
3691 origin = gsym->output_data()->address();
3692 else
3693 {
3694 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3695 _("cannot find origin of R_ARM_BASE_ABS"));
3696 return true;
3697 }
3698
3699 reloc_status = Arm_relocate_functions::base_abs(view, origin);
3700 }
3701 break;
3702
c121c671
DK
3703 case elfcpp::R_ARM_GOT_BREL:
3704 gold_assert(have_got_offset);
3705 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
3706 break;
3707
7f5309a5
ILT
3708 case elfcpp::R_ARM_GOT_PREL:
3709 gold_assert(have_got_offset);
3710 // Get the address origin for GOT PLT, which is allocated right
3711 // after the GOT section, to calculate an absolute address of
3712 // the symbol GOT entry (got_origin + got_offset).
3713 elfcpp::Elf_types<32>::Elf_Addr got_origin;
3714 got_origin = target->got_plt_section()->address();
3715 reloc_status = Arm_relocate_functions::got_prel(view,
3716 got_origin + got_offset,
3717 address);
3718 break;
3719
c121c671
DK
3720 case elfcpp::R_ARM_PLT32:
3721 gold_assert(gsym == NULL
3722 || gsym->has_plt_offset()
3723 || gsym->final_value_is_known()
3724 || (gsym->is_defined()
3725 && !gsym->is_from_dynobj()
3726 && !gsym->is_preemptible()));
3727 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
3728 address, has_thumb_bit);
3729 break;
3730
3731 case elfcpp::R_ARM_CALL:
3732 reloc_status = Arm_relocate_functions::call(view, object, psymval,
3733 address, has_thumb_bit);
3734 break;
3735
3736 case elfcpp::R_ARM_JUMP24:
3737 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
3738 address, has_thumb_bit);
3739 break;
3740
3741 case elfcpp::R_ARM_PREL31:
3742 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
3743 address, has_thumb_bit);
3744 break;
3745
3746 case elfcpp::R_ARM_TARGET1:
3747 // This should have been mapped to another type already.
3748 // Fall through.
3749 case elfcpp::R_ARM_COPY:
3750 case elfcpp::R_ARM_GLOB_DAT:
3751 case elfcpp::R_ARM_JUMP_SLOT:
3752 case elfcpp::R_ARM_RELATIVE:
3753 // These are relocations which should only be seen by the
3754 // dynamic linker, and should never be seen here.
3755 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3756 _("unexpected reloc %u in object file"),
3757 r_type);
3758 break;
3759
3760 default:
3761 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3762 _("unsupported reloc %u"),
3763 r_type);
3764 break;
3765 }
3766
3767 // Report any errors.
3768 switch (reloc_status)
3769 {
3770 case Arm_relocate_functions::STATUS_OKAY:
3771 break;
3772 case Arm_relocate_functions::STATUS_OVERFLOW:
3773 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3774 _("relocation overflow in relocation %u"),
3775 r_type);
3776 break;
3777 case Arm_relocate_functions::STATUS_BAD_RELOC:
3778 gold_error_at_location(
3779 relinfo,
3780 relnum,
3781 rel.get_r_offset(),
3782 _("unexpected opcode while processing relocation %u"),
3783 r_type);
3784 break;
4a657b0d
DK
3785 default:
3786 gold_unreachable();
3787 }
3788
3789 return true;
3790}
3791
3792// Relocate section data.
3793
3794template<bool big_endian>
3795void
3796Target_arm<big_endian>::relocate_section(
3797 const Relocate_info<32, big_endian>* relinfo,
3798 unsigned int sh_type,
3799 const unsigned char* prelocs,
3800 size_t reloc_count,
3801 Output_section* output_section,
3802 bool needs_special_offset_handling,
3803 unsigned char* view,
3804 elfcpp::Elf_types<32>::Elf_Addr address,
364c7fa5
ILT
3805 section_size_type view_size,
3806 const Reloc_symbol_changes* reloc_symbol_changes)
4a657b0d
DK
3807{
3808 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
3809 gold_assert(sh_type == elfcpp::SHT_REL);
3810
3811 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
3812 Arm_relocate>(
3813 relinfo,
3814 this,
3815 prelocs,
3816 reloc_count,
3817 output_section,
3818 needs_special_offset_handling,
3819 view,
3820 address,
364c7fa5
ILT
3821 view_size,
3822 reloc_symbol_changes);
4a657b0d
DK
3823}
3824
3825// Return the size of a relocation while scanning during a relocatable
3826// link.
3827
3828template<bool big_endian>
3829unsigned int
3830Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
3831 unsigned int r_type,
3832 Relobj* object)
3833{
3834 r_type = get_real_reloc_type(r_type);
3835 switch (r_type)
3836 {
3837 case elfcpp::R_ARM_NONE:
3838 return 0;
3839
5e445df6
ILT
3840 case elfcpp::R_ARM_ABS8:
3841 return 1;
3842
be8fcb75
ILT
3843 case elfcpp::R_ARM_ABS16:
3844 case elfcpp::R_ARM_THM_ABS5:
3845 return 2;
3846
4a657b0d 3847 case elfcpp::R_ARM_ABS32:
be8fcb75
ILT
3848 case elfcpp::R_ARM_ABS32_NOI:
3849 case elfcpp::R_ARM_ABS12:
3850 case elfcpp::R_ARM_BASE_ABS:
4a657b0d
DK
3851 case elfcpp::R_ARM_REL32:
3852 case elfcpp::R_ARM_THM_CALL:
3853 case elfcpp::R_ARM_GOTOFF32:
3854 case elfcpp::R_ARM_BASE_PREL:
3855 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 3856 case elfcpp::R_ARM_GOT_PREL:
4a657b0d
DK
3857 case elfcpp::R_ARM_PLT32:
3858 case elfcpp::R_ARM_CALL:
3859 case elfcpp::R_ARM_JUMP24:
3860 case elfcpp::R_ARM_PREL31:
fd3c5f0b
ILT
3861 case elfcpp::R_ARM_MOVW_ABS_NC:
3862 case elfcpp::R_ARM_MOVT_ABS:
3863 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3864 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
3865 case elfcpp::R_ARM_MOVW_PREL_NC:
3866 case elfcpp::R_ARM_MOVT_PREL:
3867 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3868 case elfcpp::R_ARM_THM_MOVT_PREL:
4a657b0d
DK
3869 return 4;
3870
3871 case elfcpp::R_ARM_TARGET1:
3872 // This should have been mapped to another type already.
3873 // Fall through.
3874 case elfcpp::R_ARM_COPY:
3875 case elfcpp::R_ARM_GLOB_DAT:
3876 case elfcpp::R_ARM_JUMP_SLOT:
3877 case elfcpp::R_ARM_RELATIVE:
3878 // These are relocations which should only be seen by the
3879 // dynamic linker, and should never be seen here.
3880 gold_error(_("%s: unexpected reloc %u in object file"),
3881 object->name().c_str(), r_type);
3882 return 0;
3883
3884 default:
3885 object->error(_("unsupported reloc %u in object file"), r_type);
3886 return 0;
3887 }
3888}
3889
3890// Scan the relocs during a relocatable link.
3891
3892template<bool big_endian>
3893void
3894Target_arm<big_endian>::scan_relocatable_relocs(
3895 const General_options& options,
3896 Symbol_table* symtab,
3897 Layout* layout,
3898 Sized_relobj<32, big_endian>* object,
3899 unsigned int data_shndx,
3900 unsigned int sh_type,
3901 const unsigned char* prelocs,
3902 size_t reloc_count,
3903 Output_section* output_section,
3904 bool needs_special_offset_handling,
3905 size_t local_symbol_count,
3906 const unsigned char* plocal_symbols,
3907 Relocatable_relocs* rr)
3908{
3909 gold_assert(sh_type == elfcpp::SHT_REL);
3910
3911 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3912 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3913
3914 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
3915 Scan_relocatable_relocs>(
3916 options,
3917 symtab,
3918 layout,
3919 object,
3920 data_shndx,
3921 prelocs,
3922 reloc_count,
3923 output_section,
3924 needs_special_offset_handling,
3925 local_symbol_count,
3926 plocal_symbols,
3927 rr);
3928}
3929
3930// Relocate a section during a relocatable link.
3931
3932template<bool big_endian>
3933void
3934Target_arm<big_endian>::relocate_for_relocatable(
3935 const Relocate_info<32, big_endian>* relinfo,
3936 unsigned int sh_type,
3937 const unsigned char* prelocs,
3938 size_t reloc_count,
3939 Output_section* output_section,
3940 off_t offset_in_output_section,
3941 const Relocatable_relocs* rr,
3942 unsigned char* view,
3943 elfcpp::Elf_types<32>::Elf_Addr view_address,
3944 section_size_type view_size,
3945 unsigned char* reloc_view,
3946 section_size_type reloc_view_size)
3947{
3948 gold_assert(sh_type == elfcpp::SHT_REL);
3949
3950 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
3951 relinfo,
3952 prelocs,
3953 reloc_count,
3954 output_section,
3955 offset_in_output_section,
3956 rr,
3957 view,
3958 view_address,
3959 view_size,
3960 reloc_view,
3961 reloc_view_size);
3962}
3963
94cdfcff
DK
3964// Return the value to use for a dynamic symbol which requires special
3965// treatment. This is how we support equality comparisons of function
3966// pointers across shared library boundaries, as described in the
3967// processor specific ABI supplement.
3968
4a657b0d
DK
3969template<bool big_endian>
3970uint64_t
94cdfcff 3971Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4a657b0d 3972{
94cdfcff
DK
3973 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3974 return this->plt_section()->address() + gsym->plt_offset();
4a657b0d
DK
3975}
3976
3977// Map platform-specific relocs to real relocs
3978//
3979template<bool big_endian>
3980unsigned int
3981Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
3982{
3983 switch (r_type)
3984 {
3985 case elfcpp::R_ARM_TARGET1:
3986 // This is either R_ARM_ABS32 or R_ARM_REL32;
3987 return elfcpp::R_ARM_ABS32;
3988
3989 case elfcpp::R_ARM_TARGET2:
3990 // This can be any reloc type but ususally is R_ARM_GOT_PREL
3991 return elfcpp::R_ARM_GOT_PREL;
3992
3993 default:
3994 return r_type;
3995 }
3996}
3997
3998// The selector for arm object files.
3999
4000template<bool big_endian>
4001class Target_selector_arm : public Target_selector
4002{
4003 public:
4004 Target_selector_arm()
4005 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
4006 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
4007 { }
4008
4009 Target*
4010 do_instantiate_target()
4011 { return new Target_arm<big_endian>(); }
4012};
4013
4014Target_selector_arm<false> target_selector_arm;
4015Target_selector_arm<true> target_selector_armbe;
4016
4017} // End anonymous namespace.
This page took 0.198239 seconds and 4 git commands to generate.