*** empty log message ***
[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
07f508a2
DK
64template<bool big_endian>
65class Arm_output_section;
66
67template<bool big_endian>
68class Arm_relobj;
69
b569affa
DK
70template<bool big_endian>
71class Target_arm;
72
73// For convenience.
74typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
75
76// Maximum branch offsets for ARM, THUMB and THUMB2.
77const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
78const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
79const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
80const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
81const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
82const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
83
4a657b0d
DK
84// The arm target class.
85//
86// This is a very simple port of gold for ARM-EABI. It is intended for
87// supporting Android only for the time being. Only these relocation types
88// are supported.
89//
90// R_ARM_NONE
91// R_ARM_ABS32
be8fcb75
ILT
92// R_ARM_ABS32_NOI
93// R_ARM_ABS16
94// R_ARM_ABS12
95// R_ARM_ABS8
96// R_ARM_THM_ABS5
97// R_ARM_BASE_ABS
4a657b0d
DK
98// R_ARM_REL32
99// R_ARM_THM_CALL
100// R_ARM_COPY
101// R_ARM_GLOB_DAT
102// R_ARM_BASE_PREL
103// R_ARM_JUMP_SLOT
104// R_ARM_RELATIVE
105// R_ARM_GOTOFF32
106// R_ARM_GOT_BREL
7f5309a5 107// R_ARM_GOT_PREL
4a657b0d
DK
108// R_ARM_PLT32
109// R_ARM_CALL
110// R_ARM_JUMP24
111// R_ARM_TARGET1
112// R_ARM_PREL31
7f5309a5 113// R_ARM_ABS8
fd3c5f0b
ILT
114// R_ARM_MOVW_ABS_NC
115// R_ARM_MOVT_ABS
116// R_ARM_THM_MOVW_ABS_NC
c2a122b6
ILT
117// R_ARM_THM_MOVT_ABS
118// R_ARM_MOVW_PREL_NC
119// R_ARM_MOVT_PREL
120// R_ARM_THM_MOVW_PREL_NC
121// R_ARM_THM_MOVT_PREL
4a657b0d 122//
4a657b0d 123// TODOs:
11af873f
DK
124// - Generate various branch stubs.
125// - Support interworking.
126// - Define section symbols __exidx_start and __exidx_stop.
4a657b0d 127// - Support more relocation types as needed.
94cdfcff
DK
128// - Make PLTs more flexible for different architecture features like
129// Thumb-2 and BE8.
11af873f 130// There are probably a lot more.
4a657b0d 131
b569affa
DK
132// Instruction template class. This class is similar to the insn_sequence
133// struct in bfd/elf32-arm.c.
134
135class Insn_template
136{
137 public:
138 // Types of instruction templates.
139 enum Type
140 {
141 THUMB16_TYPE = 1,
142 THUMB32_TYPE,
143 ARM_TYPE,
144 DATA_TYPE
145 };
146
147 // Factory methods to create instrunction templates in different formats.
148
149 static const Insn_template
150 thumb16_insn(uint32_t data)
151 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
152
153 // A bit of a hack. A Thumb conditional branch, in which the proper
154 // condition is inserted when we build the stub.
155 static const Insn_template
156 thumb16_bcond_insn(uint32_t data)
157 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 1); }
158
159 static const Insn_template
160 thumb32_insn(uint32_t data)
161 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
162
163 static const Insn_template
164 thumb32_b_insn(uint32_t data, int reloc_addend)
165 {
166 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
167 reloc_addend);
168 }
169
170 static const Insn_template
171 arm_insn(uint32_t data)
172 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
173
174 static const Insn_template
175 arm_rel_insn(unsigned data, int reloc_addend)
176 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
177
178 static const Insn_template
179 data_word(unsigned data, unsigned int r_type, int reloc_addend)
180 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
181
182 // Accessors. This class is used for read-only objects so no modifiers
183 // are provided.
184
185 uint32_t
186 data() const
187 { return this->data_; }
188
189 // Return the instruction sequence type of this.
190 Type
191 type() const
192 { return this->type_; }
193
194 // Return the ARM relocation type of this.
195 unsigned int
196 r_type() const
197 { return this->r_type_; }
198
199 int32_t
200 reloc_addend() const
201 { return this->reloc_addend_; }
202
203 // Return size of instrunction template in bytes.
204 size_t
205 size() const;
206
207 // Return byte-alignment of instrunction template.
208 unsigned
209 alignment() const;
210
211 private:
212 // We make the constructor private to ensure that only the factory
213 // methods are used.
214 inline
215 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
216 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
217 { }
218
219 // Instruction specific data. This is used to store information like
220 // some of the instruction bits.
221 uint32_t data_;
222 // Instruction template type.
223 Type type_;
224 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
225 unsigned int r_type_;
226 // Relocation addend.
227 int32_t reloc_addend_;
228};
229
230// Macro for generating code to stub types. One entry per long/short
231// branch stub
232
233#define DEF_STUBS \
234 DEF_STUB(long_branch_any_any) \
235 DEF_STUB(long_branch_v4t_arm_thumb) \
236 DEF_STUB(long_branch_thumb_only) \
237 DEF_STUB(long_branch_v4t_thumb_thumb) \
238 DEF_STUB(long_branch_v4t_thumb_arm) \
239 DEF_STUB(short_branch_v4t_thumb_arm) \
240 DEF_STUB(long_branch_any_arm_pic) \
241 DEF_STUB(long_branch_any_thumb_pic) \
242 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
243 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
244 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
245 DEF_STUB(long_branch_thumb_only_pic) \
246 DEF_STUB(a8_veneer_b_cond) \
247 DEF_STUB(a8_veneer_b) \
248 DEF_STUB(a8_veneer_bl) \
249 DEF_STUB(a8_veneer_blx)
250
251// Stub types.
252
253#define DEF_STUB(x) arm_stub_##x,
254typedef enum
255 {
256 arm_stub_none,
257 DEF_STUBS
258
259 // First reloc stub type.
260 arm_stub_reloc_first = arm_stub_long_branch_any_any,
261 // Last reloc stub type.
262 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
263
264 // First Cortex-A8 stub type.
265 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
266 // Last Cortex-A8 stub type.
267 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
268
269 // Last stub type.
270 arm_stub_type_last = arm_stub_a8_veneer_blx
271 } Stub_type;
272#undef DEF_STUB
273
274// Stub template class. Templates are meant to be read-only objects.
275// A stub template for a stub type contains all read-only attributes
276// common to all stubs of the same type.
277
278class Stub_template
279{
280 public:
281 Stub_template(Stub_type, const Insn_template*, size_t);
282
283 ~Stub_template()
284 { }
285
286 // Return stub type.
287 Stub_type
288 type() const
289 { return this->type_; }
290
291 // Return an array of instruction templates.
292 const Insn_template*
293 insns() const
294 { return this->insns_; }
295
296 // Return size of template in number of instructions.
297 size_t
298 insn_count() const
299 { return this->insn_count_; }
300
301 // Return size of template in bytes.
302 size_t
303 size() const
304 { return this->size_; }
305
306 // Return alignment of the stub template.
307 unsigned
308 alignment() const
309 { return this->alignment_; }
310
311 // Return whether entry point is in thumb mode.
312 bool
313 entry_in_thumb_mode() const
314 { return this->entry_in_thumb_mode_; }
315
316 // Return number of relocations in this template.
317 size_t
318 reloc_count() const
319 { return this->relocs_.size(); }
320
321 // Return index of the I-th instruction with relocation.
322 size_t
323 reloc_insn_index(size_t i) const
324 {
325 gold_assert(i < this->relocs_.size());
326 return this->relocs_[i].first;
327 }
328
329 // Return the offset of the I-th instruction with relocation from the
330 // beginning of the stub.
331 section_size_type
332 reloc_offset(size_t i) const
333 {
334 gold_assert(i < this->relocs_.size());
335 return this->relocs_[i].second;
336 }
337
338 private:
339 // This contains information about an instruction template with a relocation
340 // and its offset from start of stub.
341 typedef std::pair<size_t, section_size_type> Reloc;
342
343 // A Stub_template may not be copied. We want to share templates as much
344 // as possible.
345 Stub_template(const Stub_template&);
346 Stub_template& operator=(const Stub_template&);
347
348 // Stub type.
349 Stub_type type_;
350 // Points to an array of Insn_templates.
351 const Insn_template* insns_;
352 // Number of Insn_templates in insns_[].
353 size_t insn_count_;
354 // Size of templated instructions in bytes.
355 size_t size_;
356 // Alignment of templated instructions.
357 unsigned alignment_;
358 // Flag to indicate if entry is in thumb mode.
359 bool entry_in_thumb_mode_;
360 // A table of reloc instruction indices and offsets. We can find these by
361 // looking at the instruction templates but we pre-compute and then stash
362 // them here for speed.
363 std::vector<Reloc> relocs_;
364};
365
366//
367// A class for code stubs. This is a base class for different type of
368// stubs used in the ARM target.
369//
370
371class Stub
372{
373 private:
374 static const section_offset_type invalid_offset =
375 static_cast<section_offset_type>(-1);
376
377 public:
378 Stub(const Stub_template* stub_template)
379 : stub_template_(stub_template), offset_(invalid_offset)
380 { }
381
382 virtual
383 ~Stub()
384 { }
385
386 // Return the stub template.
387 const Stub_template*
388 stub_template() const
389 { return this->stub_template_; }
390
391 // Return offset of code stub from beginning of its containing stub table.
392 section_offset_type
393 offset() const
394 {
395 gold_assert(this->offset_ != invalid_offset);
396 return this->offset_;
397 }
398
399 // Set offset of code stub from beginning of its containing stub table.
400 void
401 set_offset(section_offset_type offset)
402 { this->offset_ = offset; }
403
404 // Return the relocation target address of the i-th relocation in the
405 // stub. This must be defined in a child class.
406 Arm_address
407 reloc_target(size_t i)
408 { return this->do_reloc_target(i); }
409
410 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
411 void
412 write(unsigned char* view, section_size_type view_size, bool big_endian)
413 { this->do_write(view, view_size, big_endian); }
414
415 protected:
416 // This must be defined in the child class.
417 virtual Arm_address
418 do_reloc_target(size_t) = 0;
419
420 // This must be defined in the child class.
421 virtual void
422 do_write(unsigned char*, section_size_type, bool) = 0;
423
424 private:
425 // Its template.
426 const Stub_template* stub_template_;
427 // Offset within the section of containing this stub.
428 section_offset_type offset_;
429};
430
431// Reloc stub class. These are stubs we use to fix up relocation because
432// of limited branch ranges.
433
434class Reloc_stub : public Stub
435{
436 public:
437 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
438 // We assume we never jump to this address.
439 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
440
441 // Return destination address.
442 Arm_address
443 destination_address() const
444 {
445 gold_assert(this->destination_address_ != this->invalid_address);
446 return this->destination_address_;
447 }
448
449 // Set destination address.
450 void
451 set_destination_address(Arm_address address)
452 {
453 gold_assert(address != this->invalid_address);
454 this->destination_address_ = address;
455 }
456
457 // Reset destination address.
458 void
459 reset_destination_address()
460 { this->destination_address_ = this->invalid_address; }
461
462 // Determine stub type for a branch of a relocation of R_TYPE going
463 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
464 // the branch target is a thumb instruction. TARGET is used for look
465 // up ARM-specific linker settings.
466 static Stub_type
467 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
468 Arm_address branch_target, bool target_is_thumb);
469
470 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
471 // and an addend. Since we treat global and local symbol differently, we
472 // use a Symbol object for a global symbol and a object-index pair for
473 // a local symbol.
474 class Key
475 {
476 public:
477 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
478 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
479 // and R_SYM must not be invalid_index.
480 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
481 unsigned int r_sym, int32_t addend)
482 : stub_type_(stub_type), addend_(addend)
483 {
484 if (symbol != NULL)
485 {
486 this->r_sym_ = Reloc_stub::invalid_index;
487 this->u_.symbol = symbol;
488 }
489 else
490 {
491 gold_assert(relobj != NULL && r_sym != invalid_index);
492 this->r_sym_ = r_sym;
493 this->u_.relobj = relobj;
494 }
495 }
496
497 ~Key()
498 { }
499
500 // Accessors: Keys are meant to be read-only object so no modifiers are
501 // provided.
502
503 // Return stub type.
504 Stub_type
505 stub_type() const
506 { return this->stub_type_; }
507
508 // Return the local symbol index or invalid_index.
509 unsigned int
510 r_sym() const
511 { return this->r_sym_; }
512
513 // Return the symbol if there is one.
514 const Symbol*
515 symbol() const
516 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
517
518 // Return the relobj if there is one.
519 const Relobj*
520 relobj() const
521 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
522
523 // Whether this equals to another key k.
524 bool
525 eq(const Key& k) const
526 {
527 return ((this->stub_type_ == k.stub_type_)
528 && (this->r_sym_ == k.r_sym_)
529 && ((this->r_sym_ != Reloc_stub::invalid_index)
530 ? (this->u_.relobj == k.u_.relobj)
531 : (this->u_.symbol == k.u_.symbol))
532 && (this->addend_ == k.addend_));
533 }
534
535 // Return a hash value.
536 size_t
537 hash_value() const
538 {
539 return (this->stub_type_
540 ^ this->r_sym_
541 ^ gold::string_hash<char>(
542 (this->r_sym_ != Reloc_stub::invalid_index)
543 ? this->u_.relobj->name().c_str()
544 : this->u_.symbol->name())
545 ^ this->addend_);
546 }
547
548 // Functors for STL associative containers.
549 struct hash
550 {
551 size_t
552 operator()(const Key& k) const
553 { return k.hash_value(); }
554 };
555
556 struct equal_to
557 {
558 bool
559 operator()(const Key& k1, const Key& k2) const
560 { return k1.eq(k2); }
561 };
562
563 // Name of key. This is mainly for debugging.
564 std::string
565 name() const;
566
567 private:
568 // Stub type.
569 Stub_type stub_type_;
570 // If this is a local symbol, this is the index in the defining object.
571 // Otherwise, it is invalid_index for a global symbol.
572 unsigned int r_sym_;
573 // If r_sym_ is invalid index. This points to a global symbol.
574 // Otherwise, this points a relobj. We used the unsized and target
eb44217c 575 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
b569affa
DK
576 // Arm_relobj. This is done to avoid making the stub class a template
577 // as most of the stub machinery is endianity-neutral. However, it
578 // may require a bit of casting done by users of this class.
579 union
580 {
581 const Symbol* symbol;
582 const Relobj* relobj;
583 } u_;
584 // Addend associated with a reloc.
585 int32_t addend_;
586 };
587
588 protected:
589 // Reloc_stubs are created via a stub factory. So these are protected.
590 Reloc_stub(const Stub_template* stub_template)
591 : Stub(stub_template), destination_address_(invalid_address)
592 { }
593
594 ~Reloc_stub()
595 { }
596
597 friend class Stub_factory;
598
599 private:
600 // Return the relocation target address of the i-th relocation in the
601 // stub.
602 Arm_address
603 do_reloc_target(size_t i)
604 {
605 // All reloc stub have only one relocation.
606 gold_assert(i == 0);
607 return this->destination_address_;
608 }
609
610 // A template to implement do_write below.
611 template<bool big_endian>
612 void inline
613 do_fixed_endian_write(unsigned char*, section_size_type);
614
615 // Write a stub.
616 void
617 do_write(unsigned char* view, section_size_type view_size, bool big_endian);
618
619 // Address of destination.
620 Arm_address destination_address_;
621};
622
623// Stub factory class.
624
625class Stub_factory
626{
627 public:
628 // Return the unique instance of this class.
629 static const Stub_factory&
630 get_instance()
631 {
632 static Stub_factory singleton;
633 return singleton;
634 }
635
636 // Make a relocation stub.
637 Reloc_stub*
638 make_reloc_stub(Stub_type stub_type) const
639 {
640 gold_assert(stub_type >= arm_stub_reloc_first
641 && stub_type <= arm_stub_reloc_last);
642 return new Reloc_stub(this->stub_templates_[stub_type]);
643 }
644
645 private:
646 // Constructor and destructor are protected since we only return a single
647 // instance created in Stub_factory::get_instance().
648
649 Stub_factory();
650
651 // A Stub_factory may not be copied since it is a singleton.
652 Stub_factory(const Stub_factory&);
653 Stub_factory& operator=(Stub_factory&);
654
655 // Stub templates. These are initialized in the constructor.
656 const Stub_template* stub_templates_[arm_stub_type_last+1];
657};
658
56ee5e00
DK
659// A class to hold stubs for the ARM target.
660
661template<bool big_endian>
662class Stub_table : public Output_data
663{
664 public:
665 Stub_table(Arm_input_section<big_endian>* owner)
666 : Output_data(), addralign_(1), owner_(owner), has_been_changed_(false),
667 reloc_stubs_()
668 { }
669
670 ~Stub_table()
671 { }
672
673 // Owner of this stub table.
674 Arm_input_section<big_endian>*
675 owner() const
676 { return this->owner_; }
677
678 // Whether this stub table is empty.
679 bool
680 empty() const
681 { return this->reloc_stubs_.empty(); }
682
683 // Whether this has been changed.
684 bool
685 has_been_changed() const
686 { return this->has_been_changed_; }
687
688 // Set the has-been-changed flag.
689 void
690 set_has_been_changed(bool value)
691 { this->has_been_changed_ = value; }
692
693 // Return the current data size.
694 off_t
695 current_data_size() const
696 { return this->current_data_size_for_child(); }
697
698 // Add a STUB with using KEY. Caller is reponsible for avoid adding
699 // if already a STUB with the same key has been added.
700 void
701 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key);
702
703 // Look up a relocation stub using KEY. Return NULL if there is none.
704 Reloc_stub*
705 find_reloc_stub(const Reloc_stub::Key& key) const
706 {
707 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
708 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
709 }
710
711 // Relocate stubs in this stub table.
712 void
713 relocate_stubs(const Relocate_info<32, big_endian>*,
714 Target_arm<big_endian>*, Output_section*,
715 unsigned char*, Arm_address, section_size_type);
716
717 protected:
718 // Write out section contents.
719 void
720 do_write(Output_file*);
721
722 // Return the required alignment.
723 uint64_t
724 do_addralign() const
725 { return this->addralign_; }
726
727 // Finalize data size.
728 void
729 set_final_data_size()
730 { this->set_data_size(this->current_data_size_for_child()); }
731
732 // Reset address and file offset.
733 void
734 do_reset_address_and_file_offset();
735
736 private:
737 // Unordered map of stubs.
738 typedef
739 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
740 Reloc_stub::Key::equal_to>
741 Reloc_stub_map;
742
743 // Address alignment
744 uint64_t addralign_;
745 // Owner of this stub table.
746 Arm_input_section<big_endian>* owner_;
747 // This is set to true during relaxiong if the size of the stub table
748 // has been changed.
749 bool has_been_changed_;
750 // The relocation stubs.
751 Reloc_stub_map reloc_stubs_;
752};
753
10ad9fe5
DK
754// A class to wrap an ordinary input section containing executable code.
755
756template<bool big_endian>
757class Arm_input_section : public Output_relaxed_input_section
758{
759 public:
760 Arm_input_section(Relobj* relobj, unsigned int shndx)
761 : Output_relaxed_input_section(relobj, shndx, 1),
762 original_addralign_(1), original_size_(0), stub_table_(NULL)
763 { }
764
765 ~Arm_input_section()
766 { }
767
768 // Initialize.
769 void
770 init();
771
772 // Whether this is a stub table owner.
773 bool
774 is_stub_table_owner() const
775 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
776
777 // Return the stub table.
778 Stub_table<big_endian>*
779 stub_table() const
780 { return this->stub_table_; }
781
782 // Set the stub_table.
783 void
784 set_stub_table(Stub_table<big_endian>* stub_table)
785 { this->stub_table_ = stub_table; }
786
07f508a2
DK
787 // Downcast a base pointer to an Arm_input_section pointer. This is
788 // not type-safe but we only use Arm_input_section not the base class.
789 static Arm_input_section<big_endian>*
790 as_arm_input_section(Output_relaxed_input_section* poris)
791 { return static_cast<Arm_input_section<big_endian>*>(poris); }
792
10ad9fe5
DK
793 protected:
794 // Write data to output file.
795 void
796 do_write(Output_file*);
797
798 // Return required alignment of this.
799 uint64_t
800 do_addralign() const
801 {
802 if (this->is_stub_table_owner())
803 return std::max(this->stub_table_->addralign(),
804 this->original_addralign_);
805 else
806 return this->original_addralign_;
807 }
808
809 // Finalize data size.
810 void
811 set_final_data_size();
812
813 // Reset address and file offset.
814 void
815 do_reset_address_and_file_offset();
816
817 // Output offset.
818 bool
819 do_output_offset(const Relobj* object, unsigned int shndx,
820 section_offset_type offset,
821 section_offset_type* poutput) const
822 {
823 if ((object == this->relobj())
824 && (shndx == this->shndx())
825 && (offset >= 0)
826 && (convert_types<uint64_t, section_offset_type>(offset)
827 <= this->original_size_))
828 {
829 *poutput = offset;
830 return true;
831 }
832 else
833 return false;
834 }
835
836 private:
837 // Copying is not allowed.
838 Arm_input_section(const Arm_input_section&);
839 Arm_input_section& operator=(const Arm_input_section&);
840
841 // Address alignment of the original input section.
842 uint64_t original_addralign_;
843 // Section size of the original input section.
844 uint64_t original_size_;
845 // Stub table.
846 Stub_table<big_endian>* stub_table_;
847};
848
07f508a2
DK
849// Arm output section class. This is defined mainly to add a number of
850// stub generation methods.
851
852template<bool big_endian>
853class Arm_output_section : public Output_section
854{
855 public:
856 Arm_output_section(const char* name, elfcpp::Elf_Word type,
857 elfcpp::Elf_Xword flags)
858 : Output_section(name, type, flags)
859 { }
860
861 ~Arm_output_section()
862 { }
863
864 // Group input sections for stub generation.
865 void
866 group_sections(section_size_type, bool, Target_arm<big_endian>*);
867
868 // Downcast a base pointer to an Arm_output_section pointer. This is
869 // not type-safe but we only use Arm_output_section not the base class.
870 static Arm_output_section<big_endian>*
871 as_arm_output_section(Output_section* os)
872 { return static_cast<Arm_output_section<big_endian>*>(os); }
873
874 private:
875 // For convenience.
876 typedef Output_section::Input_section Input_section;
877 typedef Output_section::Input_section_list Input_section_list;
878
879 // Create a stub group.
880 void create_stub_group(Input_section_list::const_iterator,
881 Input_section_list::const_iterator,
882 Input_section_list::const_iterator,
883 Target_arm<big_endian>*,
884 std::vector<Output_relaxed_input_section*>*);
885};
886
8ffa3667
DK
887// Arm_relobj class.
888
889template<bool big_endian>
890class Arm_relobj : public Sized_relobj<32, big_endian>
891{
892 public:
893 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
894
895 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
896 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
897 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
898 stub_tables_(), local_symbol_is_thumb_function_()
899 { }
900
901 ~Arm_relobj()
902 { }
903
904 // Return the stub table of the SHNDX-th section if there is one.
905 Stub_table<big_endian>*
906 stub_table(unsigned int shndx) const
907 {
908 gold_assert(shndx < this->stub_tables_.size());
909 return this->stub_tables_[shndx];
910 }
911
912 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
913 void
914 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
915 {
916 gold_assert(shndx < this->stub_tables_.size());
917 this->stub_tables_[shndx] = stub_table;
918 }
919
920 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
921 // index. This is only valid after do_count_local_symbol is called.
922 bool
923 local_symbol_is_thumb_function(unsigned int r_sym) const
924 {
925 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
926 return this->local_symbol_is_thumb_function_[r_sym];
927 }
928
929 // Scan all relocation sections for stub generation.
930 void
931 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
932 const Layout*);
933
934 // Convert regular input section with index SHNDX to a relaxed section.
935 void
936 convert_input_section_to_relaxed_section(unsigned shndx)
937 {
938 // The stubs have relocations and we need to process them after writing
939 // out the stubs. So relocation now must follow section write.
940 this->invalidate_section_offset(shndx);
941 this->set_relocs_must_follow_section_writes();
942 }
943
944 // Downcast a base pointer to an Arm_relobj pointer. This is
945 // not type-safe but we only use Arm_relobj not the base class.
946 static Arm_relobj<big_endian>*
947 as_arm_relobj(Relobj* relobj)
948 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
949
d5b40221
DK
950 // Processor-specific flags in ELF file header. This is valid only after
951 // reading symbols.
952 elfcpp::Elf_Word
953 processor_specific_flags() const
954 { return this->processor_specific_flags_; }
955
8ffa3667
DK
956 protected:
957 // Post constructor setup.
958 void
959 do_setup()
960 {
961 // Call parent's setup method.
962 Sized_relobj<32, big_endian>::do_setup();
963
964 // Initialize look-up tables.
965 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
966 this->stub_tables_.swap(empty_stub_table_list);
967 }
968
969 // Count the local symbols.
970 void
971 do_count_local_symbols(Stringpool_template<char>*,
972 Stringpool_template<char>*);
973
974 void
43d12afe 975 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
8ffa3667
DK
976 const unsigned char* pshdrs,
977 typename Sized_relobj<32, big_endian>::Views* pivews);
978
d5b40221
DK
979 // Read the symbol information.
980 void
981 do_read_symbols(Read_symbols_data* sd);
982
8ffa3667
DK
983 private:
984 // List of stub tables.
985 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
986 Stub_table_list stub_tables_;
987 // Bit vector to tell if a local symbol is a thumb function or not.
988 // This is only valid after do_count_local_symbol is called.
989 std::vector<bool> local_symbol_is_thumb_function_;
d5b40221
DK
990 // processor-specific flags in ELF file header.
991 elfcpp::Elf_Word processor_specific_flags_;
992};
993
994// Arm_dynobj class.
995
996template<bool big_endian>
997class Arm_dynobj : public Sized_dynobj<32, big_endian>
998{
999 public:
1000 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1001 const elfcpp::Ehdr<32, big_endian>& ehdr)
1002 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1003 processor_specific_flags_(0)
1004 { }
1005
1006 ~Arm_dynobj()
1007 { }
1008
1009 // Downcast a base pointer to an Arm_relobj pointer. This is
1010 // not type-safe but we only use Arm_relobj not the base class.
1011 static Arm_dynobj<big_endian>*
1012 as_arm_dynobj(Dynobj* dynobj)
1013 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1014
1015 // Processor-specific flags in ELF file header. This is valid only after
1016 // reading symbols.
1017 elfcpp::Elf_Word
1018 processor_specific_flags() const
1019 { return this->processor_specific_flags_; }
1020
1021 protected:
1022 // Read the symbol information.
1023 void
1024 do_read_symbols(Read_symbols_data* sd);
1025
1026 private:
1027 // processor-specific flags in ELF file header.
1028 elfcpp::Elf_Word processor_specific_flags_;
8ffa3667
DK
1029};
1030
e9bbb538
DK
1031// Functor to read reloc addends during stub generation.
1032
1033template<int sh_type, bool big_endian>
1034struct Stub_addend_reader
1035{
1036 // Return the addend for a relocation of a particular type. Depending
1037 // on whether this is a REL or RELA relocation, read the addend from a
1038 // view or from a Reloc object.
1039 elfcpp::Elf_types<32>::Elf_Swxword
1040 operator()(
1041 unsigned int /* r_type */,
1042 const unsigned char* /* view */,
1043 const typename Reloc_types<sh_type,
ebd95253 1044 32, big_endian>::Reloc& /* reloc */) const;
e9bbb538
DK
1045};
1046
1047// Specialized Stub_addend_reader for SHT_REL type relocation sections.
1048
1049template<bool big_endian>
1050struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1051{
1052 elfcpp::Elf_types<32>::Elf_Swxword
1053 operator()(
1054 unsigned int,
1055 const unsigned char*,
1056 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1057};
1058
1059// Specialized Stub_addend_reader for RELA type relocation sections.
1060// We currently do not handle RELA type relocation sections but it is trivial
1061// to implement the addend reader. This is provided for completeness and to
1062// make it easier to add support for RELA relocation sections in the future.
1063
1064template<bool big_endian>
1065struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1066{
1067 elfcpp::Elf_types<32>::Elf_Swxword
1068 operator()(
1069 unsigned int,
1070 const unsigned char*,
1071 const typename Reloc_types<elfcpp::SHT_RELA, 32,
ebd95253
DK
1072 big_endian>::Reloc& reloc) const
1073 { return reloc.get_r_addend(); }
e9bbb538
DK
1074};
1075
c121c671
DK
1076// Utilities for manipulating integers of up to 32-bits
1077
1078namespace utils
1079{
1080 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1081 // an int32_t. NO_BITS must be between 1 to 32.
1082 template<int no_bits>
1083 static inline int32_t
1084 sign_extend(uint32_t bits)
1085 {
96d49306 1086 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
1087 if (no_bits == 32)
1088 return static_cast<int32_t>(bits);
1089 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1090 bits &= mask;
1091 uint32_t top_bit = 1U << (no_bits - 1);
1092 int32_t as_signed = static_cast<int32_t>(bits);
1093 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1094 }
1095
1096 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1097 template<int no_bits>
1098 static inline bool
1099 has_overflow(uint32_t bits)
1100 {
96d49306 1101 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
1102 if (no_bits == 32)
1103 return false;
1104 int32_t max = (1 << (no_bits - 1)) - 1;
1105 int32_t min = -(1 << (no_bits - 1));
1106 int32_t as_signed = static_cast<int32_t>(bits);
1107 return as_signed > max || as_signed < min;
1108 }
1109
5e445df6
ILT
1110 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1111 // fits in the given number of bits as either a signed or unsigned value.
1112 // For example, has_signed_unsigned_overflow<8> would check
1113 // -128 <= bits <= 255
1114 template<int no_bits>
1115 static inline bool
1116 has_signed_unsigned_overflow(uint32_t bits)
1117 {
1118 gold_assert(no_bits >= 2 && no_bits <= 32);
1119 if (no_bits == 32)
1120 return false;
1121 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1122 int32_t min = -(1 << (no_bits - 1));
1123 int32_t as_signed = static_cast<int32_t>(bits);
1124 return as_signed > max || as_signed < min;
1125 }
1126
c121c671
DK
1127 // Select bits from A and B using bits in MASK. For each n in [0..31],
1128 // the n-th bit in the result is chosen from the n-th bits of A and B.
1129 // A zero selects A and a one selects B.
1130 static inline uint32_t
1131 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1132 { return (a & ~mask) | (b & mask); }
1133};
1134
4a657b0d
DK
1135template<bool big_endian>
1136class Target_arm : public Sized_target<32, big_endian>
1137{
1138 public:
1139 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1140 Reloc_section;
1141
2daedcd6
DK
1142 // When were are relocating a stub, we pass this as the relocation number.
1143 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1144
4a657b0d 1145 Target_arm()
94cdfcff
DK
1146 : Sized_target<32, big_endian>(&arm_info),
1147 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
55da9579
DK
1148 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
1149 stub_factory_(Stub_factory::get_instance()),
eb44217c
DK
1150 may_use_blx_(true), should_force_pic_veneer_(false),
1151 arm_input_section_map_()
4a657b0d
DK
1152 { }
1153
b569affa
DK
1154 // Whether we can use BLX.
1155 bool
1156 may_use_blx() const
1157 { return this->may_use_blx_; }
1158
1159 // Set use-BLX flag.
1160 void
1161 set_may_use_blx(bool value)
1162 { this->may_use_blx_ = value; }
1163
1164 // Whether we force PCI branch veneers.
1165 bool
1166 should_force_pic_veneer() const
1167 { return this->should_force_pic_veneer_; }
1168
1169 // Set PIC veneer flag.
1170 void
1171 set_should_force_pic_veneer(bool value)
1172 { this->should_force_pic_veneer_ = value; }
1173
1174 // Whether we use THUMB-2 instructions.
1175 bool
1176 using_thumb2() const
1177 {
1178 // FIXME: This should not hard-coded.
1179 return false;
1180 }
1181
1182 // Whether we use THUMB/THUMB-2 instructions only.
1183 bool
1184 using_thumb_only() const
1185 {
1186 // FIXME: This should not hard-coded.
1187 return false;
1188 }
1189
4a657b0d
DK
1190 // Process the relocations to determine unreferenced sections for
1191 // garbage collection.
1192 void
ad0f2072 1193 gc_process_relocs(Symbol_table* symtab,
4a657b0d
DK
1194 Layout* layout,
1195 Sized_relobj<32, big_endian>* object,
1196 unsigned int data_shndx,
1197 unsigned int sh_type,
1198 const unsigned char* prelocs,
1199 size_t reloc_count,
1200 Output_section* output_section,
1201 bool needs_special_offset_handling,
1202 size_t local_symbol_count,
1203 const unsigned char* plocal_symbols);
1204
1205 // Scan the relocations to look for symbol adjustments.
1206 void
ad0f2072 1207 scan_relocs(Symbol_table* symtab,
4a657b0d
DK
1208 Layout* layout,
1209 Sized_relobj<32, big_endian>* object,
1210 unsigned int data_shndx,
1211 unsigned int sh_type,
1212 const unsigned char* prelocs,
1213 size_t reloc_count,
1214 Output_section* output_section,
1215 bool needs_special_offset_handling,
1216 size_t local_symbol_count,
1217 const unsigned char* plocal_symbols);
1218
1219 // Finalize the sections.
1220 void
d5b40221 1221 do_finalize_sections(Layout*, const Input_objects*);
4a657b0d 1222
94cdfcff 1223 // Return the value to use for a dynamic symbol which requires special
4a657b0d
DK
1224 // treatment.
1225 uint64_t
1226 do_dynsym_value(const Symbol*) const;
1227
1228 // Relocate a section.
1229 void
1230 relocate_section(const Relocate_info<32, big_endian>*,
1231 unsigned int sh_type,
1232 const unsigned char* prelocs,
1233 size_t reloc_count,
1234 Output_section* output_section,
1235 bool needs_special_offset_handling,
1236 unsigned char* view,
ebabffbd 1237 Arm_address view_address,
364c7fa5
ILT
1238 section_size_type view_size,
1239 const Reloc_symbol_changes*);
4a657b0d
DK
1240
1241 // Scan the relocs during a relocatable link.
1242 void
ad0f2072 1243 scan_relocatable_relocs(Symbol_table* symtab,
4a657b0d
DK
1244 Layout* layout,
1245 Sized_relobj<32, big_endian>* object,
1246 unsigned int data_shndx,
1247 unsigned int sh_type,
1248 const unsigned char* prelocs,
1249 size_t reloc_count,
1250 Output_section* output_section,
1251 bool needs_special_offset_handling,
1252 size_t local_symbol_count,
1253 const unsigned char* plocal_symbols,
1254 Relocatable_relocs*);
1255
1256 // Relocate a section during a relocatable link.
1257 void
1258 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1259 unsigned int sh_type,
1260 const unsigned char* prelocs,
1261 size_t reloc_count,
1262 Output_section* output_section,
1263 off_t offset_in_output_section,
1264 const Relocatable_relocs*,
1265 unsigned char* view,
ebabffbd 1266 Arm_address view_address,
4a657b0d
DK
1267 section_size_type view_size,
1268 unsigned char* reloc_view,
1269 section_size_type reloc_view_size);
1270
1271 // Return whether SYM is defined by the ABI.
1272 bool
1273 do_is_defined_by_abi(Symbol* sym) const
1274 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1275
94cdfcff
DK
1276 // Return the size of the GOT section.
1277 section_size_type
1278 got_size()
1279 {
1280 gold_assert(this->got_ != NULL);
1281 return this->got_->data_size();
1282 }
1283
4a657b0d
DK
1284 // Map platform-specific reloc types
1285 static unsigned int
1286 get_real_reloc_type (unsigned int r_type);
1287
55da9579
DK
1288 //
1289 // Methods to support stub-generations.
1290 //
1291
1292 // Return the stub factory
1293 const Stub_factory&
1294 stub_factory() const
1295 { return this->stub_factory_; }
1296
1297 // Make a new Arm_input_section object.
1298 Arm_input_section<big_endian>*
1299 new_arm_input_section(Relobj*, unsigned int);
1300
1301 // Find the Arm_input_section object corresponding to the SHNDX-th input
1302 // section of RELOBJ.
1303 Arm_input_section<big_endian>*
1304 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
1305
1306 // Make a new Stub_table
1307 Stub_table<big_endian>*
1308 new_stub_table(Arm_input_section<big_endian>*);
1309
eb44217c
DK
1310 // Scan a section for stub generation.
1311 void
1312 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
1313 const unsigned char*, size_t, Output_section*,
1314 bool, const unsigned char*, Arm_address,
1315 section_size_type);
1316
43d12afe
DK
1317 // Relocate a stub.
1318 void
1319 relocate_stub(Reloc_stub*, const Relocate_info<32, big_endian>*,
1320 Output_section*, unsigned char*, Arm_address,
1321 section_size_type);
1322
b569affa 1323 // Get the default ARM target.
43d12afe 1324 static Target_arm<big_endian>*
b569affa
DK
1325 default_target()
1326 {
1327 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1328 && parameters->target().is_big_endian() == big_endian);
43d12afe
DK
1329 return static_cast<Target_arm<big_endian>*>(
1330 parameters->sized_target<32, big_endian>());
b569affa
DK
1331 }
1332
55da9579
DK
1333 // Whether relocation type uses LSB to distinguish THUMB addresses.
1334 static bool
1335 reloc_uses_thumb_bit(unsigned int r_type);
1336
d5b40221 1337 protected:
eb44217c
DK
1338 // Make an ELF object.
1339 Object*
1340 do_make_elf_object(const std::string&, Input_file*, off_t,
1341 const elfcpp::Ehdr<32, big_endian>& ehdr);
1342
1343 Object*
1344 do_make_elf_object(const std::string&, Input_file*, off_t,
1345 const elfcpp::Ehdr<32, !big_endian>&)
1346 { gold_unreachable(); }
1347
1348 Object*
1349 do_make_elf_object(const std::string&, Input_file*, off_t,
1350 const elfcpp::Ehdr<64, false>&)
1351 { gold_unreachable(); }
1352
1353 Object*
1354 do_make_elf_object(const std::string&, Input_file*, off_t,
1355 const elfcpp::Ehdr<64, true>&)
1356 { gold_unreachable(); }
1357
1358 // Make an output section.
1359 Output_section*
1360 do_make_output_section(const char* name, elfcpp::Elf_Word type,
1361 elfcpp::Elf_Xword flags)
1362 { return new Arm_output_section<big_endian>(name, type, flags); }
1363
d5b40221
DK
1364 void
1365 do_adjust_elf_header(unsigned char* view, int len) const;
1366
eb44217c
DK
1367 // We only need to generate stubs, and hence perform relaxation if we are
1368 // not doing relocatable linking.
1369 bool
1370 do_may_relax() const
1371 { return !parameters->options().relocatable(); }
1372
1373 bool
1374 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
1375
4a657b0d
DK
1376 private:
1377 // The class which scans relocations.
1378 class Scan
1379 {
1380 public:
1381 Scan()
bec53400 1382 : issued_non_pic_error_(false)
4a657b0d
DK
1383 { }
1384
1385 inline void
ad0f2072 1386 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
4a657b0d
DK
1387 Sized_relobj<32, big_endian>* object,
1388 unsigned int data_shndx,
1389 Output_section* output_section,
1390 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1391 const elfcpp::Sym<32, big_endian>& lsym);
1392
1393 inline void
ad0f2072 1394 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
4a657b0d
DK
1395 Sized_relobj<32, big_endian>* object,
1396 unsigned int data_shndx,
1397 Output_section* output_section,
1398 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1399 Symbol* gsym);
1400
1401 private:
1402 static void
1403 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1404 unsigned int r_type);
1405
1406 static void
1407 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1408 unsigned int r_type, Symbol*);
bec53400
DK
1409
1410 void
1411 check_non_pic(Relobj*, unsigned int r_type);
1412
1413 // Almost identical to Symbol::needs_plt_entry except that it also
1414 // handles STT_ARM_TFUNC.
1415 static bool
1416 symbol_needs_plt_entry(const Symbol* sym)
1417 {
1418 // An undefined symbol from an executable does not need a PLT entry.
1419 if (sym->is_undefined() && !parameters->options().shared())
1420 return false;
1421
1422 return (!parameters->doing_static_link()
1423 && (sym->type() == elfcpp::STT_FUNC
1424 || sym->type() == elfcpp::STT_ARM_TFUNC)
1425 && (sym->is_from_dynobj()
1426 || sym->is_undefined()
1427 || sym->is_preemptible()));
1428 }
1429
1430 // Whether we have issued an error about a non-PIC compilation.
1431 bool issued_non_pic_error_;
4a657b0d
DK
1432 };
1433
1434 // The class which implements relocation.
1435 class Relocate
1436 {
1437 public:
1438 Relocate()
1439 { }
1440
1441 ~Relocate()
1442 { }
1443
bec53400
DK
1444 // Return whether the static relocation needs to be applied.
1445 inline bool
1446 should_apply_static_reloc(const Sized_symbol<32>* gsym,
1447 int ref_flags,
1448 bool is_32bit,
1449 Output_section* output_section);
1450
4a657b0d
DK
1451 // Do a relocation. Return false if the caller should not issue
1452 // any warnings about this relocation.
1453 inline bool
1454 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1455 Output_section*, size_t relnum,
1456 const elfcpp::Rel<32, big_endian>&,
1457 unsigned int r_type, const Sized_symbol<32>*,
1458 const Symbol_value<32>*,
ebabffbd 1459 unsigned char*, Arm_address,
4a657b0d 1460 section_size_type);
c121c671
DK
1461
1462 // Return whether we want to pass flag NON_PIC_REF for this
1463 // reloc.
1464 static inline bool
1465 reloc_is_non_pic (unsigned int r_type)
1466 {
1467 switch (r_type)
1468 {
1469 case elfcpp::R_ARM_REL32:
1470 case elfcpp::R_ARM_THM_CALL:
1471 case elfcpp::R_ARM_CALL:
1472 case elfcpp::R_ARM_JUMP24:
1473 case elfcpp::R_ARM_PREL31:
be8fcb75
ILT
1474 case elfcpp::R_ARM_THM_ABS5:
1475 case elfcpp::R_ARM_ABS8:
1476 case elfcpp::R_ARM_ABS12:
1477 case elfcpp::R_ARM_ABS16:
1478 case elfcpp::R_ARM_BASE_ABS:
c121c671
DK
1479 return true;
1480 default:
1481 return false;
1482 }
1483 }
4a657b0d
DK
1484 };
1485
1486 // A class which returns the size required for a relocation type,
1487 // used while scanning relocs during a relocatable link.
1488 class Relocatable_size_for_reloc
1489 {
1490 public:
1491 unsigned int
1492 get_size_for_reloc(unsigned int, Relobj*);
1493 };
1494
94cdfcff
DK
1495 // Get the GOT section, creating it if necessary.
1496 Output_data_got<32, big_endian>*
1497 got_section(Symbol_table*, Layout*);
1498
1499 // Get the GOT PLT section.
1500 Output_data_space*
1501 got_plt_section() const
1502 {
1503 gold_assert(this->got_plt_ != NULL);
1504 return this->got_plt_;
1505 }
1506
1507 // Create a PLT entry for a global symbol.
1508 void
1509 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1510
1511 // Get the PLT section.
1512 const Output_data_plt_arm<big_endian>*
1513 plt_section() const
1514 {
1515 gold_assert(this->plt_ != NULL);
1516 return this->plt_;
1517 }
1518
1519 // Get the dynamic reloc section, creating it if necessary.
1520 Reloc_section*
1521 rel_dyn_section(Layout*);
1522
1523 // Return true if the symbol may need a COPY relocation.
1524 // References from an executable object to non-function symbols
1525 // defined in a dynamic object may need a COPY relocation.
1526 bool
1527 may_need_copy_reloc(Symbol* gsym)
1528 {
966d4097
DK
1529 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1530 && gsym->may_need_copy_reloc());
94cdfcff
DK
1531 }
1532
1533 // Add a potential copy relocation.
1534 void
1535 copy_reloc(Symbol_table* symtab, Layout* layout,
1536 Sized_relobj<32, big_endian>* object,
1537 unsigned int shndx, Output_section* output_section,
1538 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1539 {
1540 this->copy_relocs_.copy_reloc(symtab, layout,
1541 symtab->get_sized_symbol<32>(sym),
1542 object, shndx, output_section, reloc,
1543 this->rel_dyn_section(layout));
1544 }
1545
d5b40221
DK
1546 // Whether two EABI versions are compatible.
1547 static bool
1548 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
1549
1550 // Merge processor-specific flags from input object and those in the ELF
1551 // header of the output.
1552 void
1553 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
1554
eb44217c
DK
1555 //
1556 // Methods to support stub-generations.
1557 //
d5b40221 1558
eb44217c
DK
1559 // Group input sections for stub generation.
1560 void
1561 group_sections(Layout*, section_size_type, bool);
d5b40221 1562
eb44217c
DK
1563 // Scan a relocation for stub generation.
1564 void
1565 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
1566 const Sized_symbol<32>*, unsigned int,
1567 const Symbol_value<32>*,
1568 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
d5b40221 1569
eb44217c
DK
1570 // Scan a relocation section for stub.
1571 template<int sh_type>
1572 void
1573 scan_reloc_section_for_stubs(
1574 const Relocate_info<32, big_endian>* relinfo,
1575 const unsigned char* prelocs,
1576 size_t reloc_count,
1577 Output_section* output_section,
1578 bool needs_special_offset_handling,
1579 const unsigned char* view,
1580 elfcpp::Elf_types<32>::Elf_Addr view_address,
1581 section_size_type);
d5b40221 1582
4a657b0d
DK
1583 // Information about this specific target which we pass to the
1584 // general Target structure.
1585 static const Target::Target_info arm_info;
94cdfcff
DK
1586
1587 // The types of GOT entries needed for this platform.
1588 enum Got_type
1589 {
1590 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
1591 };
1592
55da9579
DK
1593 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
1594
1595 // Map input section to Arm_input_section.
1596 typedef Unordered_map<Input_section_specifier,
1597 Arm_input_section<big_endian>*,
1598 Input_section_specifier::hash,
1599 Input_section_specifier::equal_to>
1600 Arm_input_section_map;
1601
94cdfcff
DK
1602 // The GOT section.
1603 Output_data_got<32, big_endian>* got_;
1604 // The PLT section.
1605 Output_data_plt_arm<big_endian>* plt_;
1606 // The GOT PLT section.
1607 Output_data_space* got_plt_;
1608 // The dynamic reloc section.
1609 Reloc_section* rel_dyn_;
1610 // Relocs saved to avoid a COPY reloc.
1611 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1612 // Space for variables copied with a COPY reloc.
1613 Output_data_space* dynbss_;
55da9579
DK
1614 // Vector of Stub_tables created.
1615 Stub_table_list stub_tables_;
1616 // Stub factory.
1617 const Stub_factory &stub_factory_;
b569affa
DK
1618 // Whether we can use BLX.
1619 bool may_use_blx_;
1620 // Whether we force PIC branch veneers.
1621 bool should_force_pic_veneer_;
eb44217c
DK
1622 // Map for locating Arm_input_sections.
1623 Arm_input_section_map arm_input_section_map_;
4a657b0d
DK
1624};
1625
1626template<bool big_endian>
1627const Target::Target_info Target_arm<big_endian>::arm_info =
1628{
1629 32, // size
1630 big_endian, // is_big_endian
1631 elfcpp::EM_ARM, // machine_code
1632 false, // has_make_symbol
1633 false, // has_resolve
1634 false, // has_code_fill
1635 true, // is_default_stack_executable
1636 '\0', // wrap_char
1637 "/usr/lib/libc.so.1", // dynamic_linker
1638 0x8000, // default_text_segment_address
1639 0x1000, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
1640 0x1000, // common_pagesize (overridable by -z common-page-size)
1641 elfcpp::SHN_UNDEF, // small_common_shndx
1642 elfcpp::SHN_UNDEF, // large_common_shndx
1643 0, // small_common_section_flags
1644 0 // large_common_section_flags
4a657b0d
DK
1645};
1646
c121c671
DK
1647// Arm relocate functions class
1648//
1649
1650template<bool big_endian>
1651class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1652{
1653 public:
1654 typedef enum
1655 {
1656 STATUS_OKAY, // No error during relocation.
1657 STATUS_OVERFLOW, // Relocation oveflow.
1658 STATUS_BAD_RELOC // Relocation cannot be applied.
1659 } Status;
1660
1661 private:
1662 typedef Relocate_functions<32, big_endian> Base;
1663 typedef Arm_relocate_functions<big_endian> This;
1664
fd3c5f0b
ILT
1665 // Encoding of imm16 argument for movt and movw ARM instructions
1666 // from ARM ARM:
1667 //
1668 // imm16 := imm4 | imm12
1669 //
1670 // 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
1671 // +-------+---------------+-------+-------+-----------------------+
1672 // | | |imm4 | |imm12 |
1673 // +-------+---------------+-------+-------+-----------------------+
1674
1675 // Extract the relocation addend from VAL based on the ARM
1676 // instruction encoding described above.
1677 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1678 extract_arm_movw_movt_addend(
1679 typename elfcpp::Swap<32, big_endian>::Valtype val)
1680 {
1681 // According to the Elf ABI for ARM Architecture the immediate
1682 // field is sign-extended to form the addend.
1683 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1684 }
1685
1686 // Insert X into VAL based on the ARM instruction encoding described
1687 // above.
1688 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1689 insert_val_arm_movw_movt(
1690 typename elfcpp::Swap<32, big_endian>::Valtype val,
1691 typename elfcpp::Swap<32, big_endian>::Valtype x)
1692 {
1693 val &= 0xfff0f000;
1694 val |= x & 0x0fff;
1695 val |= (x & 0xf000) << 4;
1696 return val;
1697 }
1698
1699 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1700 // from ARM ARM:
1701 //
1702 // imm16 := imm4 | i | imm3 | imm8
1703 //
1704 // 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
1705 // +---------+-+-----------+-------++-+-----+-------+---------------+
1706 // | |i| |imm4 || |imm3 | |imm8 |
1707 // +---------+-+-----------+-------++-+-----+-------+---------------+
1708
1709 // Extract the relocation addend from VAL based on the Thumb2
1710 // instruction encoding described above.
1711 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1712 extract_thumb_movw_movt_addend(
1713 typename elfcpp::Swap<32, big_endian>::Valtype val)
1714 {
1715 // According to the Elf ABI for ARM Architecture the immediate
1716 // field is sign-extended to form the addend.
1717 return utils::sign_extend<16>(((val >> 4) & 0xf000)
1718 | ((val >> 15) & 0x0800)
1719 | ((val >> 4) & 0x0700)
1720 | (val & 0x00ff));
1721 }
1722
1723 // Insert X into VAL based on the Thumb2 instruction encoding
1724 // described above.
1725 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1726 insert_val_thumb_movw_movt(
1727 typename elfcpp::Swap<32, big_endian>::Valtype val,
1728 typename elfcpp::Swap<32, big_endian>::Valtype x)
1729 {
1730 val &= 0xfbf08f00;
1731 val |= (x & 0xf000) << 4;
1732 val |= (x & 0x0800) << 15;
1733 val |= (x & 0x0700) << 4;
1734 val |= (x & 0x00ff);
1735 return val;
1736 }
1737
c121c671
DK
1738 // FIXME: This probably only works for Android on ARM v5te. We should
1739 // following GNU ld for the general case.
1740 template<unsigned r_type>
1741 static inline typename This::Status
1742 arm_branch_common(unsigned char *view,
1743 const Sized_relobj<32, big_endian>* object,
1744 const Symbol_value<32>* psymval,
ebabffbd 1745 Arm_address address,
2daedcd6 1746 Arm_address thumb_bit)
c121c671
DK
1747 {
1748 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1749 Valtype* wv = reinterpret_cast<Valtype*>(view);
1750 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1751
1752 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
1753 && ((val & 0x0f000000UL) == 0x0a000000UL);
1754 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
1755 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
1756 && ((val & 0x0f000000UL) == 0x0b000000UL);
1757 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
1758 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
1759
1760 if (r_type == elfcpp::R_ARM_CALL)
1761 {
1762 if (!insn_is_uncond_bl && !insn_is_blx)
1763 return This::STATUS_BAD_RELOC;
1764 }
1765 else if (r_type == elfcpp::R_ARM_JUMP24)
1766 {
1767 if (!insn_is_b && !insn_is_cond_bl)
1768 return This::STATUS_BAD_RELOC;
1769 }
1770 else if (r_type == elfcpp::R_ARM_PLT32)
1771 {
1772 if (!insn_is_any_branch)
1773 return This::STATUS_BAD_RELOC;
1774 }
1775 else
1776 gold_unreachable();
1777
1778 Valtype addend = utils::sign_extend<26>(val << 2);
2daedcd6 1779 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
c121c671
DK
1780
1781 // If target has thumb bit set, we need to either turn the BL
1782 // into a BLX (for ARMv5 or above) or generate a stub.
1783 if (x & 1)
1784 {
1785 // Turn BL to BLX.
1786 if (insn_is_uncond_bl)
1787 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
1788 else
1789 return This::STATUS_BAD_RELOC;
1790 }
1791 else
1792 gold_assert(!insn_is_blx);
1793
1794 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
1795 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1796 return (utils::has_overflow<26>(x)
1797 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
1798 }
1799
1800 public:
5e445df6
ILT
1801
1802 // R_ARM_ABS8: S + A
1803 static inline typename This::Status
1804 abs8(unsigned char *view,
1805 const Sized_relobj<32, big_endian>* object,
be8fcb75 1806 const Symbol_value<32>* psymval)
5e445df6
ILT
1807 {
1808 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
1809 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1810 Valtype* wv = reinterpret_cast<Valtype*>(view);
1811 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
1812 Reltype addend = utils::sign_extend<8>(val);
2daedcd6 1813 Reltype x = psymval->value(object, addend);
5e445df6
ILT
1814 val = utils::bit_select(val, x, 0xffU);
1815 elfcpp::Swap<8, big_endian>::writeval(wv, val);
1816 return (utils::has_signed_unsigned_overflow<8>(x)
1817 ? This::STATUS_OVERFLOW
1818 : This::STATUS_OKAY);
1819 }
1820
be8fcb75
ILT
1821 // R_ARM_THM_ABS5: S + A
1822 static inline typename This::Status
1823 thm_abs5(unsigned char *view,
1824 const Sized_relobj<32, big_endian>* object,
1825 const Symbol_value<32>* psymval)
1826 {
1827 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1828 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1829 Valtype* wv = reinterpret_cast<Valtype*>(view);
1830 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1831 Reltype addend = (val & 0x7e0U) >> 6;
2daedcd6 1832 Reltype x = psymval->value(object, addend);
be8fcb75
ILT
1833 val = utils::bit_select(val, x << 6, 0x7e0U);
1834 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1835 return (utils::has_overflow<5>(x)
1836 ? This::STATUS_OVERFLOW
1837 : This::STATUS_OKAY);
1838 }
1839
1840 // R_ARM_ABS12: S + A
1841 static inline typename This::Status
1842 abs12(unsigned char *view,
1843 const Sized_relobj<32, big_endian>* object,
1844 const Symbol_value<32>* psymval)
1845 {
1846 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1847 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1848 Valtype* wv = reinterpret_cast<Valtype*>(view);
1849 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1850 Reltype addend = val & 0x0fffU;
2daedcd6 1851 Reltype x = psymval->value(object, addend);
be8fcb75
ILT
1852 val = utils::bit_select(val, x, 0x0fffU);
1853 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1854 return (utils::has_overflow<12>(x)
1855 ? This::STATUS_OVERFLOW
1856 : This::STATUS_OKAY);
1857 }
1858
1859 // R_ARM_ABS16: S + A
1860 static inline typename This::Status
1861 abs16(unsigned char *view,
1862 const Sized_relobj<32, big_endian>* object,
1863 const Symbol_value<32>* psymval)
1864 {
1865 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1866 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1867 Valtype* wv = reinterpret_cast<Valtype*>(view);
1868 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1869 Reltype addend = utils::sign_extend<16>(val);
2daedcd6 1870 Reltype x = psymval->value(object, addend);
be8fcb75
ILT
1871 val = utils::bit_select(val, x, 0xffffU);
1872 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1873 return (utils::has_signed_unsigned_overflow<16>(x)
1874 ? This::STATUS_OVERFLOW
1875 : This::STATUS_OKAY);
1876 }
1877
c121c671
DK
1878 // R_ARM_ABS32: (S + A) | T
1879 static inline typename This::Status
1880 abs32(unsigned char *view,
1881 const Sized_relobj<32, big_endian>* object,
1882 const Symbol_value<32>* psymval,
2daedcd6 1883 Arm_address thumb_bit)
c121c671
DK
1884 {
1885 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1886 Valtype* wv = reinterpret_cast<Valtype*>(view);
1887 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2daedcd6 1888 Valtype x = psymval->value(object, addend) | thumb_bit;
c121c671
DK
1889 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1890 return This::STATUS_OKAY;
1891 }
1892
1893 // R_ARM_REL32: (S + A) | T - P
1894 static inline typename This::Status
1895 rel32(unsigned char *view,
1896 const Sized_relobj<32, big_endian>* object,
1897 const Symbol_value<32>* psymval,
ebabffbd 1898 Arm_address address,
2daedcd6 1899 Arm_address thumb_bit)
c121c671
DK
1900 {
1901 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1902 Valtype* wv = reinterpret_cast<Valtype*>(view);
1903 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2daedcd6 1904 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
c121c671
DK
1905 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1906 return This::STATUS_OKAY;
1907 }
1908
1909 // R_ARM_THM_CALL: (S + A) | T - P
1910 static inline typename This::Status
1911 thm_call(unsigned char *view,
1912 const Sized_relobj<32, big_endian>* object,
1913 const Symbol_value<32>* psymval,
ebabffbd 1914 Arm_address address,
2daedcd6 1915 Arm_address thumb_bit)
c121c671
DK
1916 {
1917 // A thumb call consists of two instructions.
1918 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1919 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1920 Valtype* wv = reinterpret_cast<Valtype*>(view);
1921 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
1922 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
1923 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
1924 gold_assert((lo & 0xf800) == 0xf800);
1925 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
1926 | ((lo & 0x7ff) << 1));
2daedcd6 1927 Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
c121c671
DK
1928
1929 // If target has no thumb bit set, we need to either turn the BL
1930 // into a BLX (for ARMv5 or above) or generate a stub.
1931 if ((x & 1) == 0)
1932 {
1933 // This only works for ARMv5 and above with interworking enabled.
1934 lo &= 0xefff;
1935 }
1936 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
1937 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
1938 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
1939 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
1940 return (utils::has_overflow<23>(x)
1941 ? This::STATUS_OVERFLOW
1942 : This::STATUS_OKAY);
1943 }
1944
1945 // R_ARM_BASE_PREL: B(S) + A - P
1946 static inline typename This::Status
1947 base_prel(unsigned char* view,
ebabffbd
DK
1948 Arm_address origin,
1949 Arm_address address)
c121c671
DK
1950 {
1951 Base::rel32(view, origin - address);
1952 return STATUS_OKAY;
1953 }
1954
be8fcb75
ILT
1955 // R_ARM_BASE_ABS: B(S) + A
1956 static inline typename This::Status
1957 base_abs(unsigned char* view,
ebabffbd 1958 Arm_address origin)
be8fcb75
ILT
1959 {
1960 Base::rel32(view, origin);
1961 return STATUS_OKAY;
1962 }
1963
c121c671
DK
1964 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1965 static inline typename This::Status
1966 got_brel(unsigned char* view,
1967 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
1968 {
1969 Base::rel32(view, got_offset);
1970 return This::STATUS_OKAY;
1971 }
1972
7f5309a5
ILT
1973 // R_ARM_GOT_PREL: GOT(S) + A – P
1974 static inline typename This::Status
1975 got_prel(unsigned char* view,
1976 typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
ebabffbd 1977 Arm_address address)
7f5309a5
ILT
1978 {
1979 Base::rel32(view, got_offset - address);
1980 return This::STATUS_OKAY;
1981 }
1982
c121c671
DK
1983 // R_ARM_PLT32: (S + A) | T - P
1984 static inline typename This::Status
1985 plt32(unsigned char *view,
1986 const Sized_relobj<32, big_endian>* object,
1987 const Symbol_value<32>* psymval,
ebabffbd 1988 Arm_address address,
2daedcd6 1989 Arm_address thumb_bit)
c121c671
DK
1990 {
1991 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
2daedcd6 1992 address, thumb_bit);
c121c671
DK
1993 }
1994
1995 // R_ARM_CALL: (S + A) | T - P
1996 static inline typename This::Status
1997 call(unsigned char *view,
1998 const Sized_relobj<32, big_endian>* object,
1999 const Symbol_value<32>* psymval,
ebabffbd 2000 Arm_address address,
2daedcd6 2001 Arm_address thumb_bit)
c121c671
DK
2002 {
2003 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
2daedcd6 2004 address, thumb_bit);
c121c671
DK
2005 }
2006
2007 // R_ARM_JUMP24: (S + A) | T - P
2008 static inline typename This::Status
2009 jump24(unsigned char *view,
2010 const Sized_relobj<32, big_endian>* object,
2011 const Symbol_value<32>* psymval,
ebabffbd 2012 Arm_address address,
2daedcd6 2013 Arm_address thumb_bit)
c121c671
DK
2014 {
2015 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
2daedcd6 2016 address, thumb_bit);
c121c671
DK
2017 }
2018
2019 // R_ARM_PREL: (S + A) | T - P
2020 static inline typename This::Status
2021 prel31(unsigned char *view,
2022 const Sized_relobj<32, big_endian>* object,
2023 const Symbol_value<32>* psymval,
ebabffbd 2024 Arm_address address,
2daedcd6 2025 Arm_address thumb_bit)
c121c671
DK
2026 {
2027 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2028 Valtype* wv = reinterpret_cast<Valtype*>(view);
2029 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2030 Valtype addend = utils::sign_extend<31>(val);
2daedcd6 2031 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
c121c671
DK
2032 val = utils::bit_select(val, x, 0x7fffffffU);
2033 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2034 return (utils::has_overflow<31>(x) ?
2035 This::STATUS_OVERFLOW : This::STATUS_OKAY);
2036 }
fd3c5f0b
ILT
2037
2038 // R_ARM_MOVW_ABS_NC: (S + A) | T
2039 static inline typename This::Status
2040 movw_abs_nc(unsigned char *view,
2041 const Sized_relobj<32, big_endian>* object,
2042 const Symbol_value<32>* psymval,
2daedcd6 2043 Arm_address thumb_bit)
fd3c5f0b
ILT
2044 {
2045 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2046 Valtype* wv = reinterpret_cast<Valtype*>(view);
2047 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2048 Valtype addend = This::extract_arm_movw_movt_addend(val);
2daedcd6 2049 Valtype x = psymval->value(object, addend) | thumb_bit;
fd3c5f0b
ILT
2050 val = This::insert_val_arm_movw_movt(val, x);
2051 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2052 return This::STATUS_OKAY;
2053 }
2054
2055 // R_ARM_MOVT_ABS: S + A
2056 static inline typename This::Status
2057 movt_abs(unsigned char *view,
2058 const Sized_relobj<32, big_endian>* object,
2059 const Symbol_value<32>* psymval)
2060 {
2061 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2062 Valtype* wv = reinterpret_cast<Valtype*>(view);
2063 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2064 Valtype addend = This::extract_arm_movw_movt_addend(val);
2daedcd6 2065 Valtype x = psymval->value(object, addend) >> 16;
fd3c5f0b
ILT
2066 val = This::insert_val_arm_movw_movt(val, x);
2067 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2068 return This::STATUS_OKAY;
2069 }
2070
2071 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2072 static inline typename This::Status
2073 thm_movw_abs_nc(unsigned char *view,
2074 const Sized_relobj<32, big_endian>* object,
2075 const Symbol_value<32>* psymval,
2daedcd6 2076 Arm_address thumb_bit)
fd3c5f0b
ILT
2077 {
2078 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2079 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2080 Valtype* wv = reinterpret_cast<Valtype*>(view);
2081 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2082 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2083 Reltype addend = extract_thumb_movw_movt_addend(val);
2daedcd6 2084 Reltype x = psymval->value(object, addend) | thumb_bit;
fd3c5f0b
ILT
2085 val = This::insert_val_thumb_movw_movt(val, x);
2086 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2087 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2088 return This::STATUS_OKAY;
2089 }
2090
2091 // R_ARM_THM_MOVT_ABS: S + A
2092 static inline typename This::Status
2093 thm_movt_abs(unsigned char *view,
2094 const Sized_relobj<32, big_endian>* object,
2095 const Symbol_value<32>* psymval)
2096 {
2097 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2098 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2099 Valtype* wv = reinterpret_cast<Valtype*>(view);
2100 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2101 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2102 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2daedcd6 2103 Reltype x = psymval->value(object, addend) >> 16;
fd3c5f0b
ILT
2104 val = This::insert_val_thumb_movw_movt(val, x);
2105 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2106 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2107 return This::STATUS_OKAY;
2108 }
2109
c2a122b6
ILT
2110 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2111 static inline typename This::Status
2112 movw_prel_nc(unsigned char *view,
2113 const Sized_relobj<32, big_endian>* object,
2114 const Symbol_value<32>* psymval,
ebabffbd 2115 Arm_address address,
2daedcd6 2116 Arm_address thumb_bit)
c2a122b6
ILT
2117 {
2118 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2119 Valtype* wv = reinterpret_cast<Valtype*>(view);
2120 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2121 Valtype addend = This::extract_arm_movw_movt_addend(val);
2daedcd6 2122 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
c2a122b6
ILT
2123 val = This::insert_val_arm_movw_movt(val, x);
2124 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2125 return This::STATUS_OKAY;
2126 }
2127
2128 // R_ARM_MOVT_PREL: S + A - P
2129 static inline typename This::Status
2130 movt_prel(unsigned char *view,
2131 const Sized_relobj<32, big_endian>* object,
2132 const Symbol_value<32>* psymval,
ebabffbd 2133 Arm_address address)
c2a122b6
ILT
2134 {
2135 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2136 Valtype* wv = reinterpret_cast<Valtype*>(view);
2137 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2138 Valtype addend = This::extract_arm_movw_movt_addend(val);
2daedcd6 2139 Valtype x = (psymval->value(object, addend) - address) >> 16;
c2a122b6
ILT
2140 val = This::insert_val_arm_movw_movt(val, x);
2141 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2142 return This::STATUS_OKAY;
2143 }
2144
2145 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2146 static inline typename This::Status
2147 thm_movw_prel_nc(unsigned char *view,
2148 const Sized_relobj<32, big_endian>* object,
2149 const Symbol_value<32>* psymval,
ebabffbd 2150 Arm_address address,
2daedcd6 2151 Arm_address thumb_bit)
c2a122b6
ILT
2152 {
2153 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2154 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2155 Valtype* wv = reinterpret_cast<Valtype*>(view);
2156 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2157 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2158 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2daedcd6 2159 Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
c2a122b6
ILT
2160 val = This::insert_val_thumb_movw_movt(val, x);
2161 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2162 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2163 return This::STATUS_OKAY;
2164 }
2165
2166 // R_ARM_THM_MOVT_PREL: S + A - P
2167 static inline typename This::Status
2168 thm_movt_prel(unsigned char *view,
2169 const Sized_relobj<32, big_endian>* object,
2170 const Symbol_value<32>* psymval,
ebabffbd 2171 Arm_address address)
c2a122b6
ILT
2172 {
2173 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2174 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2175 Valtype* wv = reinterpret_cast<Valtype*>(view);
2176 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2177 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2178 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2daedcd6 2179 Reltype x = (psymval->value(object, addend) - address) >> 16;
c2a122b6
ILT
2180 val = This::insert_val_thumb_movw_movt(val, x);
2181 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2182 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2183 return This::STATUS_OKAY;
2184 }
c121c671
DK
2185};
2186
94cdfcff
DK
2187// Get the GOT section, creating it if necessary.
2188
2189template<bool big_endian>
2190Output_data_got<32, big_endian>*
2191Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
2192{
2193 if (this->got_ == NULL)
2194 {
2195 gold_assert(symtab != NULL && layout != NULL);
2196
2197 this->got_ = new Output_data_got<32, big_endian>();
2198
2199 Output_section* os;
2200 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2201 (elfcpp::SHF_ALLOC
2202 | elfcpp::SHF_WRITE),
f5c870d2 2203 this->got_, false);
94cdfcff
DK
2204 os->set_is_relro();
2205
2206 // The old GNU linker creates a .got.plt section. We just
2207 // create another set of data in the .got section. Note that we
2208 // always create a PLT if we create a GOT, although the PLT
2209 // might be empty.
2210 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
2211 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2212 (elfcpp::SHF_ALLOC
2213 | elfcpp::SHF_WRITE),
f5c870d2 2214 this->got_plt_, false);
94cdfcff
DK
2215 os->set_is_relro();
2216
2217 // The first three entries are reserved.
2218 this->got_plt_->set_current_data_size(3 * 4);
2219
2220 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2221 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2222 this->got_plt_,
2223 0, 0, elfcpp::STT_OBJECT,
2224 elfcpp::STB_LOCAL,
2225 elfcpp::STV_HIDDEN, 0,
2226 false, false);
2227 }
2228 return this->got_;
2229}
2230
2231// Get the dynamic reloc section, creating it if necessary.
2232
2233template<bool big_endian>
2234typename Target_arm<big_endian>::Reloc_section*
2235Target_arm<big_endian>::rel_dyn_section(Layout* layout)
2236{
2237 if (this->rel_dyn_ == NULL)
2238 {
2239 gold_assert(layout != NULL);
2240 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
2241 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
f5c870d2 2242 elfcpp::SHF_ALLOC, this->rel_dyn_, true);
94cdfcff
DK
2243 }
2244 return this->rel_dyn_;
2245}
2246
b569affa
DK
2247// Insn_template methods.
2248
2249// Return byte size of an instruction template.
2250
2251size_t
2252Insn_template::size() const
2253{
2254 switch (this->type())
2255 {
2256 case THUMB16_TYPE:
2257 return 2;
2258 case ARM_TYPE:
2259 case THUMB32_TYPE:
2260 case DATA_TYPE:
2261 return 4;
2262 default:
2263 gold_unreachable();
2264 }
2265}
2266
2267// Return alignment of an instruction template.
2268
2269unsigned
2270Insn_template::alignment() const
2271{
2272 switch (this->type())
2273 {
2274 case THUMB16_TYPE:
2275 case THUMB32_TYPE:
2276 return 2;
2277 case ARM_TYPE:
2278 case DATA_TYPE:
2279 return 4;
2280 default:
2281 gold_unreachable();
2282 }
2283}
2284
2285// Stub_template methods.
2286
2287Stub_template::Stub_template(
2288 Stub_type type, const Insn_template* insns,
2289 size_t insn_count)
2290 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
2291 entry_in_thumb_mode_(false), relocs_()
2292{
2293 off_t offset = 0;
2294
2295 // Compute byte size and alignment of stub template.
2296 for (size_t i = 0; i < insn_count; i++)
2297 {
2298 unsigned insn_alignment = insns[i].alignment();
2299 size_t insn_size = insns[i].size();
2300 gold_assert((offset & (insn_alignment - 1)) == 0);
2301 this->alignment_ = std::max(this->alignment_, insn_alignment);
2302 switch (insns[i].type())
2303 {
2304 case Insn_template::THUMB16_TYPE:
2305 if (i == 0)
2306 this->entry_in_thumb_mode_ = true;
2307 break;
2308
2309 case Insn_template::THUMB32_TYPE:
2310 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
2311 this->relocs_.push_back(Reloc(i, offset));
2312 if (i == 0)
2313 this->entry_in_thumb_mode_ = true;
2314 break;
2315
2316 case Insn_template::ARM_TYPE:
2317 // Handle cases where the target is encoded within the
2318 // instruction.
2319 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
2320 this->relocs_.push_back(Reloc(i, offset));
2321 break;
2322
2323 case Insn_template::DATA_TYPE:
2324 // Entry point cannot be data.
2325 gold_assert(i != 0);
2326 this->relocs_.push_back(Reloc(i, offset));
2327 break;
2328
2329 default:
2330 gold_unreachable();
2331 }
2332 offset += insn_size;
2333 }
2334 this->size_ = offset;
2335}
2336
2337// Reloc_stub::Key methods.
2338
2339// Dump a Key as a string for debugging.
2340
2341std::string
2342Reloc_stub::Key::name() const
2343{
2344 if (this->r_sym_ == invalid_index)
2345 {
2346 // Global symbol key name
2347 // <stub-type>:<symbol name>:<addend>.
2348 const std::string sym_name = this->u_.symbol->name();
2349 // We need to print two hex number and two colons. So just add 100 bytes
2350 // to the symbol name size.
2351 size_t len = sym_name.size() + 100;
2352 char* buffer = new char[len];
2353 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
2354 sym_name.c_str(), this->addend_);
2355 gold_assert(c > 0 && c < static_cast<int>(len));
2356 delete[] buffer;
2357 return std::string(buffer);
2358 }
2359 else
2360 {
2361 // local symbol key name
2362 // <stub-type>:<object>:<r_sym>:<addend>.
2363 const size_t len = 200;
2364 char buffer[len];
2365 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
2366 this->u_.relobj, this->r_sym_, this->addend_);
2367 gold_assert(c > 0 && c < static_cast<int>(len));
2368 return std::string(buffer);
2369 }
2370}
2371
2372// Reloc_stub methods.
2373
2374// Determine the type of stub needed, if any, for a relocation of R_TYPE at
2375// LOCATION to DESTINATION.
2376// This code is based on the arm_type_of_stub function in
2377// bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2378// class simple.
2379
2380Stub_type
2381Reloc_stub::stub_type_for_reloc(
2382 unsigned int r_type,
2383 Arm_address location,
2384 Arm_address destination,
2385 bool target_is_thumb)
2386{
2387 Stub_type stub_type = arm_stub_none;
2388
2389 // This is a bit ugly but we want to avoid using a templated class for
2390 // big and little endianities.
2391 bool may_use_blx;
2392 bool should_force_pic_veneer;
2393 bool thumb2;
2394 bool thumb_only;
2395 if (parameters->target().is_big_endian())
2396 {
43d12afe 2397 const Target_arm<true>* big_endian_target =
b569affa 2398 Target_arm<true>::default_target();
43d12afe
DK
2399 may_use_blx = big_endian_target->may_use_blx();
2400 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
2401 thumb2 = big_endian_target->using_thumb2();
2402 thumb_only = big_endian_target->using_thumb_only();
b569affa
DK
2403 }
2404 else
2405 {
43d12afe 2406 const Target_arm<false>* little_endian_target =
b569affa 2407 Target_arm<false>::default_target();
43d12afe
DK
2408 may_use_blx = little_endian_target->may_use_blx();
2409 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
2410 thumb2 = little_endian_target->using_thumb2();
2411 thumb_only = little_endian_target->using_thumb_only();
b569affa
DK
2412 }
2413
2414 int64_t branch_offset = (int64_t)destination - location;
2415
2416 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
2417 {
2418 // Handle cases where:
2419 // - this call goes too far (different Thumb/Thumb2 max
2420 // distance)
2421 // - it's a Thumb->Arm call and blx is not available, or it's a
2422 // Thumb->Arm branch (not bl). A stub is needed in this case.
2423 if ((!thumb2
2424 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2425 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2426 || (thumb2
2427 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2428 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2429 || ((!target_is_thumb)
2430 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2431 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
2432 {
2433 if (target_is_thumb)
2434 {
2435 // Thumb to thumb.
2436 if (!thumb_only)
2437 {
2438 stub_type = (parameters->options().shared() | should_force_pic_veneer)
2439 // PIC stubs.
2440 ? ((may_use_blx
2441 && (r_type == elfcpp::R_ARM_THM_CALL))
2442 // V5T and above. Stub starts with ARM code, so
2443 // we must be able to switch mode before
2444 // reaching it, which is only possible for 'bl'
2445 // (ie R_ARM_THM_CALL relocation).
2446 ? arm_stub_long_branch_any_thumb_pic
2447 // On V4T, use Thumb code only.
2448 : arm_stub_long_branch_v4t_thumb_thumb_pic)
2449
2450 // non-PIC stubs.
2451 : ((may_use_blx
2452 && (r_type == elfcpp::R_ARM_THM_CALL))
2453 ? arm_stub_long_branch_any_any // V5T and above.
2454 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
2455 }
2456 else
2457 {
2458 stub_type = (parameters->options().shared() | should_force_pic_veneer)
2459 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
2460 : arm_stub_long_branch_thumb_only; // non-PIC stub.
2461 }
2462 }
2463 else
2464 {
2465 // Thumb to arm.
2466
2467 // FIXME: We should check that the input section is from an
2468 // object that has interwork enabled.
2469
2470 stub_type = (parameters->options().shared()
2471 || should_force_pic_veneer)
2472 // PIC stubs.
2473 ? ((may_use_blx
2474 && (r_type == elfcpp::R_ARM_THM_CALL))
2475 ? arm_stub_long_branch_any_arm_pic // V5T and above.
2476 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
2477
2478 // non-PIC stubs.
2479 : ((may_use_blx
2480 && (r_type == elfcpp::R_ARM_THM_CALL))
2481 ? arm_stub_long_branch_any_any // V5T and above.
2482 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
2483
2484 // Handle v4t short branches.
2485 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
2486 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
2487 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
2488 stub_type = arm_stub_short_branch_v4t_thumb_arm;
2489 }
2490 }
2491 }
2492 else if (r_type == elfcpp::R_ARM_CALL
2493 || r_type == elfcpp::R_ARM_JUMP24
2494 || r_type == elfcpp::R_ARM_PLT32)
2495 {
2496 if (target_is_thumb)
2497 {
2498 // Arm to thumb.
2499
2500 // FIXME: We should check that the input section is from an
2501 // object that has interwork enabled.
2502
2503 // We have an extra 2-bytes reach because of
2504 // the mode change (bit 24 (H) of BLX encoding).
2505 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
2506 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2507 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
2508 || (r_type == elfcpp::R_ARM_JUMP24)
2509 || (r_type == elfcpp::R_ARM_PLT32))
2510 {
2511 stub_type = (parameters->options().shared()
2512 || should_force_pic_veneer)
2513 // PIC stubs.
2514 ? (may_use_blx
2515 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
2516 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
2517
2518 // non-PIC stubs.
2519 : (may_use_blx
2520 ? arm_stub_long_branch_any_any // V5T and above.
2521 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
2522 }
2523 }
2524 else
2525 {
2526 // Arm to arm.
2527 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
2528 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
2529 {
2530 stub_type = (parameters->options().shared()
2531 || should_force_pic_veneer)
2532 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
2533 : arm_stub_long_branch_any_any; /// non-PIC.
2534 }
2535 }
2536 }
2537
2538 return stub_type;
2539}
2540
2541// Template to implement do_write for a specific target endianity.
2542
2543template<bool big_endian>
2544void inline
2545Reloc_stub::do_fixed_endian_write(unsigned char* view,
2546 section_size_type view_size)
2547{
2548 const Stub_template* stub_template = this->stub_template();
2549 const Insn_template* insns = stub_template->insns();
2550
2551 // FIXME: We do not handle BE8 encoding yet.
2552 unsigned char* pov = view;
2553 for (size_t i = 0; i < stub_template->insn_count(); i++)
2554 {
2555 switch (insns[i].type())
2556 {
2557 case Insn_template::THUMB16_TYPE:
2558 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2559 gold_assert(insns[i].reloc_addend() == 0);
2560 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2561 break;
2562 case Insn_template::THUMB32_TYPE:
2563 {
2564 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2565 uint32_t lo = insns[i].data() & 0xffff;
2566 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2567 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2568 }
2569 break;
2570 case Insn_template::ARM_TYPE:
2571 case Insn_template::DATA_TYPE:
2572 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2573 break;
2574 default:
2575 gold_unreachable();
2576 }
2577 pov += insns[i].size();
2578 }
2579 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2580}
2581
2582// Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2583
2584void
2585Reloc_stub::do_write(unsigned char* view, section_size_type view_size,
2586 bool big_endian)
2587{
2588 if (big_endian)
2589 this->do_fixed_endian_write<true>(view, view_size);
2590 else
2591 this->do_fixed_endian_write<false>(view, view_size);
2592}
2593
2594// Stub_factory methods.
2595
2596Stub_factory::Stub_factory()
2597{
2598 // The instruction template sequences are declared as static
2599 // objects and initialized first time the constructor runs.
2600
2601 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2602 // to reach the stub if necessary.
2603 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
2604 {
2605 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2606 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2607 // dcd R_ARM_ABS32(X)
2608 };
2609
2610 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2611 // available.
2612 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2613 {
2614 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2615 Insn_template::arm_insn(0xe12fff1c), // bx ip
2616 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2617 // dcd R_ARM_ABS32(X)
2618 };
2619
2620 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2621 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
2622 {
2623 Insn_template::thumb16_insn(0xb401), // push {r0}
2624 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2625 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2626 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2627 Insn_template::thumb16_insn(0x4760), // bx ip
2628 Insn_template::thumb16_insn(0xbf00), // nop
2629 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2630 // dcd R_ARM_ABS32(X)
2631 };
2632
2633 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2634 // allowed.
2635 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2636 {
2637 Insn_template::thumb16_insn(0x4778), // bx pc
2638 Insn_template::thumb16_insn(0x46c0), // nop
2639 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2640 Insn_template::arm_insn(0xe12fff1c), // bx ip
2641 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2642 // dcd R_ARM_ABS32(X)
2643 };
2644
2645 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2646 // available.
2647 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2648 {
2649 Insn_template::thumb16_insn(0x4778), // bx pc
2650 Insn_template::thumb16_insn(0x46c0), // nop
2651 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2652 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2653 // dcd R_ARM_ABS32(X)
2654 };
2655
2656 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2657 // one, when the destination is close enough.
2658 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2659 {
2660 Insn_template::thumb16_insn(0x4778), // bx pc
2661 Insn_template::thumb16_insn(0x46c0), // nop
2662 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2663 };
2664
2665 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2666 // blx to reach the stub if necessary.
2667 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
2668 {
2669 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2670 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2671 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2672 // dcd R_ARM_REL32(X-4)
2673 };
2674
2675 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2676 // blx to reach the stub if necessary. We can not add into pc;
2677 // it is not guaranteed to mode switch (different in ARMv6 and
2678 // ARMv7).
2679 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
2680 {
2681 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2682 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2683 Insn_template::arm_insn(0xe12fff1c), // bx ip
2684 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2685 // dcd R_ARM_REL32(X)
2686 };
2687
2688 // V4T ARM -> ARM long branch stub, PIC.
2689 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2690 {
2691 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2692 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2693 Insn_template::arm_insn(0xe12fff1c), // bx ip
2694 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2695 // dcd R_ARM_REL32(X)
2696 };
2697
2698 // V4T Thumb -> ARM long branch stub, PIC.
2699 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2700 {
2701 Insn_template::thumb16_insn(0x4778), // bx pc
2702 Insn_template::thumb16_insn(0x46c0), // nop
2703 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2704 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
2705 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2706 // dcd R_ARM_REL32(X)
2707 };
2708
2709 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
2710 // architectures.
2711 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
2712 {
2713 Insn_template::thumb16_insn(0xb401), // push {r0}
2714 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2715 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
2716 Insn_template::thumb16_insn(0x4484), // add ip, r0
2717 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2718 Insn_template::thumb16_insn(0x4760), // bx ip
2719 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
2720 // dcd R_ARM_REL32(X)
2721 };
2722
2723 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2724 // allowed.
2725 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2726 {
2727 Insn_template::thumb16_insn(0x4778), // bx pc
2728 Insn_template::thumb16_insn(0x46c0), // nop
2729 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2730 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2731 Insn_template::arm_insn(0xe12fff1c), // bx ip
2732 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2733 // dcd R_ARM_REL32(X)
2734 };
2735
2736 // Cortex-A8 erratum-workaround stubs.
2737
2738 // Stub used for conditional branches (which may be beyond +/-1MB away,
2739 // so we can't use a conditional branch to reach this stub).
2740
2741 // original code:
2742 //
2743 // b<cond> X
2744 // after:
2745 //
2746 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
2747 {
2748 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
2749 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
2750 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
2751 // b.w X
2752 };
2753
2754 // Stub used for b.w and bl.w instructions.
2755
2756 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
2757 {
2758 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2759 };
2760
2761 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
2762 {
2763 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2764 };
2765
2766 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
2767 // instruction (which switches to ARM mode) to point to this stub. Jump to
2768 // the real destination using an ARM-mode branch.
2769 const Insn_template elf32_arm_stub_a8_veneer_blx[] =
2770 {
2771 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
2772 };
2773
2774 // Fill in the stub template look-up table. Stub templates are constructed
2775 // per instance of Stub_factory for fast look-up without locking
2776 // in a thread-enabled environment.
2777
2778 this->stub_templates_[arm_stub_none] =
2779 new Stub_template(arm_stub_none, NULL, 0);
2780
2781#define DEF_STUB(x) \
2782 do \
2783 { \
2784 size_t array_size \
2785 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
2786 Stub_type type = arm_stub_##x; \
2787 this->stub_templates_[type] = \
2788 new Stub_template(type, elf32_arm_stub_##x, array_size); \
2789 } \
2790 while (0);
2791
2792 DEF_STUBS
2793#undef DEF_STUB
2794}
2795
56ee5e00
DK
2796// Stub_table methods.
2797
2798// Add a STUB with using KEY. Caller is reponsible for avoid adding
2799// if already a STUB with the same key has been added.
2800
2801template<bool big_endian>
2802void
2803Stub_table<big_endian>::add_reloc_stub(
2804 Reloc_stub* stub,
2805 const Reloc_stub::Key& key)
2806{
2807 const Stub_template* stub_template = stub->stub_template();
2808 gold_assert(stub_template->type() == key.stub_type());
2809 this->reloc_stubs_[key] = stub;
2810 if (this->addralign_ < stub_template->alignment())
2811 this->addralign_ = stub_template->alignment();
2812 this->has_been_changed_ = true;
2813}
2814
2815template<bool big_endian>
2816void
2817Stub_table<big_endian>::relocate_stubs(
2818 const Relocate_info<32, big_endian>* relinfo,
2819 Target_arm<big_endian>* arm_target,
2820 Output_section* output_section,
2821 unsigned char* view,
2822 Arm_address address,
2823 section_size_type view_size)
2824{
2825 // If we are passed a view bigger than the stub table's. we need to
2826 // adjust the view.
2827 gold_assert(address == this->address()
2828 && (view_size
2829 == static_cast<section_size_type>(this->data_size())));
2830
2831 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2832 p != this->reloc_stubs_.end();
2833 ++p)
2834 {
2835 Reloc_stub* stub = p->second;
2836 const Stub_template* stub_template = stub->stub_template();
2837 if (stub_template->reloc_count() != 0)
2838 {
2839 // Adjust view to cover the stub only.
2840 section_size_type offset = stub->offset();
2841 section_size_type stub_size = stub_template->size();
2842 gold_assert(offset + stub_size <= view_size);
2843
2844 arm_target->relocate_stub(stub, relinfo, output_section,
2845 view + offset, address + offset,
2846 stub_size);
2847 }
2848 }
2849}
2850
2851// Reset address and file offset.
2852
2853template<bool big_endian>
2854void
2855Stub_table<big_endian>::do_reset_address_and_file_offset()
2856{
2857 off_t off = 0;
2858 uint64_t max_addralign = 1;
2859 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2860 p != this->reloc_stubs_.end();
2861 ++p)
2862 {
2863 Reloc_stub* stub = p->second;
2864 const Stub_template* stub_template = stub->stub_template();
2865 uint64_t stub_addralign = stub_template->alignment();
2866 max_addralign = std::max(max_addralign, stub_addralign);
2867 off = align_address(off, stub_addralign);
2868 stub->set_offset(off);
2869 stub->reset_destination_address();
2870 off += stub_template->size();
2871 }
2872
2873 this->addralign_ = max_addralign;
2874 this->set_current_data_size_for_child(off);
2875}
2876
2877// Write out the stubs to file.
2878
2879template<bool big_endian>
2880void
2881Stub_table<big_endian>::do_write(Output_file* of)
2882{
2883 off_t offset = this->offset();
2884 const section_size_type oview_size =
2885 convert_to_section_size_type(this->data_size());
2886 unsigned char* const oview = of->get_output_view(offset, oview_size);
2887
2888 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2889 p != this->reloc_stubs_.end();
2890 ++p)
2891 {
2892 Reloc_stub* stub = p->second;
2893 Arm_address address = this->address() + stub->offset();
2894 gold_assert(address
2895 == align_address(address,
2896 stub->stub_template()->alignment()));
2897 stub->write(oview + stub->offset(), stub->stub_template()->size(),
2898 big_endian);
2899 }
2900 of->write_output_view(this->offset(), oview_size, oview);
2901}
2902
10ad9fe5
DK
2903// Arm_input_section methods.
2904
2905// Initialize an Arm_input_section.
2906
2907template<bool big_endian>
2908void
2909Arm_input_section<big_endian>::init()
2910{
2911 Relobj* relobj = this->relobj();
2912 unsigned int shndx = this->shndx();
2913
2914 // Cache these to speed up size and alignment queries. It is too slow
2915 // to call section_addraglin and section_size every time.
2916 this->original_addralign_ = relobj->section_addralign(shndx);
2917 this->original_size_ = relobj->section_size(shndx);
2918
2919 // We want to make this look like the original input section after
2920 // output sections are finalized.
2921 Output_section* os = relobj->output_section(shndx);
2922 off_t offset = relobj->output_section_offset(shndx);
2923 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2924 this->set_address(os->address() + offset);
2925 this->set_file_offset(os->offset() + offset);
2926
2927 this->set_current_data_size(this->original_size_);
2928 this->finalize_data_size();
2929}
2930
2931template<bool big_endian>
2932void
2933Arm_input_section<big_endian>::do_write(Output_file* of)
2934{
2935 // We have to write out the original section content.
2936 section_size_type section_size;
2937 const unsigned char* section_contents =
2938 this->relobj()->section_contents(this->shndx(), &section_size, false);
2939 of->write(this->offset(), section_contents, section_size);
2940
2941 // If this owns a stub table and it is not empty, write it.
2942 if (this->is_stub_table_owner() && !this->stub_table_->empty())
2943 this->stub_table_->write(of);
2944}
2945
2946// Finalize data size.
2947
2948template<bool big_endian>
2949void
2950Arm_input_section<big_endian>::set_final_data_size()
2951{
2952 // If this owns a stub table, finalize its data size as well.
2953 if (this->is_stub_table_owner())
2954 {
2955 uint64_t address = this->address();
2956
2957 // The stub table comes after the original section contents.
2958 address += this->original_size_;
2959 address = align_address(address, this->stub_table_->addralign());
2960 off_t offset = this->offset() + (address - this->address());
2961 this->stub_table_->set_address_and_file_offset(address, offset);
2962 address += this->stub_table_->data_size();
2963 gold_assert(address == this->address() + this->current_data_size());
2964 }
2965
2966 this->set_data_size(this->current_data_size());
2967}
2968
2969// Reset address and file offset.
2970
2971template<bool big_endian>
2972void
2973Arm_input_section<big_endian>::do_reset_address_and_file_offset()
2974{
2975 // Size of the original input section contents.
2976 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2977
2978 // If this is a stub table owner, account for the stub table size.
2979 if (this->is_stub_table_owner())
2980 {
2981 Stub_table<big_endian>* stub_table = this->stub_table_;
2982
2983 // Reset the stub table's address and file offset. The
2984 // current data size for child will be updated after that.
2985 stub_table_->reset_address_and_file_offset();
2986 off = align_address(off, stub_table_->addralign());
2987 off += stub_table->current_data_size();
2988 }
2989
2990 this->set_current_data_size(off);
2991}
2992
07f508a2
DK
2993// Arm_output_section methods.
2994
2995// Create a stub group for input sections from BEGIN to END. OWNER
2996// points to the input section to be the owner a new stub table.
2997
2998template<bool big_endian>
2999void
3000Arm_output_section<big_endian>::create_stub_group(
3001 Input_section_list::const_iterator begin,
3002 Input_section_list::const_iterator end,
3003 Input_section_list::const_iterator owner,
3004 Target_arm<big_endian>* target,
3005 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
3006{
3007 // Currently we convert ordinary input sections into relaxed sections only
3008 // at this point but we may want to support creating relaxed input section
3009 // very early. So we check here to see if owner is already a relaxed
3010 // section.
3011
3012 Arm_input_section<big_endian>* arm_input_section;
3013 if (owner->is_relaxed_input_section())
3014 {
3015 arm_input_section =
3016 Arm_input_section<big_endian>::as_arm_input_section(
3017 owner->relaxed_input_section());
3018 }
3019 else
3020 {
3021 gold_assert(owner->is_input_section());
3022 // Create a new relaxed input section.
3023 arm_input_section =
3024 target->new_arm_input_section(owner->relobj(), owner->shndx());
3025 new_relaxed_sections->push_back(arm_input_section);
3026 }
3027
3028 // Create a stub table.
3029 Stub_table<big_endian>* stub_table =
3030 target->new_stub_table(arm_input_section);
3031
3032 arm_input_section->set_stub_table(stub_table);
3033
3034 Input_section_list::const_iterator p = begin;
3035 Input_section_list::const_iterator prev_p;
3036
3037 // Look for input sections or relaxed input sections in [begin ... end].
3038 do
3039 {
3040 if (p->is_input_section() || p->is_relaxed_input_section())
3041 {
3042 // The stub table information for input sections live
3043 // in their objects.
3044 Arm_relobj<big_endian>* arm_relobj =
3045 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
3046 arm_relobj->set_stub_table(p->shndx(), stub_table);
3047 }
3048 prev_p = p++;
3049 }
3050 while (prev_p != end);
3051}
3052
3053// Group input sections for stub generation. GROUP_SIZE is roughly the limit
3054// of stub groups. We grow a stub group by adding input section until the
3055// size is just below GROUP_SIZE. The last input section will be converted
3056// into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3057// input section after the stub table, effectively double the group size.
3058//
3059// This is similar to the group_sections() function in elf32-arm.c but is
3060// implemented differently.
3061
3062template<bool big_endian>
3063void
3064Arm_output_section<big_endian>::group_sections(
3065 section_size_type group_size,
3066 bool stubs_always_after_branch,
3067 Target_arm<big_endian>* target)
3068{
3069 // We only care about sections containing code.
3070 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
3071 return;
3072
3073 // States for grouping.
3074 typedef enum
3075 {
3076 // No group is being built.
3077 NO_GROUP,
3078 // A group is being built but the stub table is not found yet.
3079 // We keep group a stub group until the size is just under GROUP_SIZE.
3080 // The last input section in the group will be used as the stub table.
3081 FINDING_STUB_SECTION,
3082 // A group is being built and we have already found a stub table.
3083 // We enter this state to grow a stub group by adding input section
3084 // after the stub table. This effectively doubles the group size.
3085 HAS_STUB_SECTION
3086 } State;
3087
3088 // Any newly created relaxed sections are stored here.
3089 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
3090
3091 State state = NO_GROUP;
3092 section_size_type off = 0;
3093 section_size_type group_begin_offset = 0;
3094 section_size_type group_end_offset = 0;
3095 section_size_type stub_table_end_offset = 0;
3096 Input_section_list::const_iterator group_begin =
3097 this->input_sections().end();
3098 Input_section_list::const_iterator stub_table =
3099 this->input_sections().end();
3100 Input_section_list::const_iterator group_end = this->input_sections().end();
3101 for (Input_section_list::const_iterator p = this->input_sections().begin();
3102 p != this->input_sections().end();
3103 ++p)
3104 {
3105 section_size_type section_begin_offset =
3106 align_address(off, p->addralign());
3107 section_size_type section_end_offset =
3108 section_begin_offset + p->data_size();
3109
3110 // Check to see if we should group the previously seens sections.
e9bbb538 3111 switch (state)
07f508a2
DK
3112 {
3113 case NO_GROUP:
3114 break;
3115
3116 case FINDING_STUB_SECTION:
3117 // Adding this section makes the group larger than GROUP_SIZE.
3118 if (section_end_offset - group_begin_offset >= group_size)
3119 {
3120 if (stubs_always_after_branch)
3121 {
3122 gold_assert(group_end != this->input_sections().end());
3123 this->create_stub_group(group_begin, group_end, group_end,
3124 target, &new_relaxed_sections);
3125 state = NO_GROUP;
3126 }
3127 else
3128 {
3129 // But wait, there's more! Input sections up to
3130 // stub_group_size bytes after the stub table can be
3131 // handled by it too.
3132 state = HAS_STUB_SECTION;
3133 stub_table = group_end;
3134 stub_table_end_offset = group_end_offset;
3135 }
3136 }
3137 break;
3138
3139 case HAS_STUB_SECTION:
3140 // Adding this section makes the post stub-section group larger
3141 // than GROUP_SIZE.
3142 if (section_end_offset - stub_table_end_offset >= group_size)
3143 {
3144 gold_assert(group_end != this->input_sections().end());
3145 this->create_stub_group(group_begin, group_end, stub_table,
3146 target, &new_relaxed_sections);
3147 state = NO_GROUP;
3148 }
3149 break;
3150
3151 default:
3152 gold_unreachable();
3153 }
3154
3155 // If we see an input section and currently there is no group, start
3156 // a new one. Skip any empty sections.
3157 if ((p->is_input_section() || p->is_relaxed_input_section())
3158 && (p->relobj()->section_size(p->shndx()) != 0))
3159 {
3160 if (state == NO_GROUP)
3161 {
3162 state = FINDING_STUB_SECTION;
3163 group_begin = p;
3164 group_begin_offset = section_begin_offset;
3165 }
3166
3167 // Keep track of the last input section seen.
3168 group_end = p;
3169 group_end_offset = section_end_offset;
3170 }
3171
3172 off = section_end_offset;
3173 }
3174
3175 // Create a stub group for any ungrouped sections.
3176 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
3177 {
3178 gold_assert(group_end != this->input_sections().end());
3179 this->create_stub_group(group_begin, group_end,
3180 (state == FINDING_STUB_SECTION
3181 ? group_end
3182 : stub_table),
3183 target, &new_relaxed_sections);
3184 }
3185
3186 // Convert input section into relaxed input section in a batch.
3187 if (!new_relaxed_sections.empty())
3188 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
3189
3190 // Update the section offsets
3191 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
3192 {
3193 Arm_relobj<big_endian>* arm_relobj =
3194 Arm_relobj<big_endian>::as_arm_relobj(
3195 new_relaxed_sections[i]->relobj());
3196 unsigned int shndx = new_relaxed_sections[i]->shndx();
3197 // Tell Arm_relobj that this input section is converted.
3198 arm_relobj->convert_input_section_to_relaxed_section(shndx);
3199 }
3200}
3201
8ffa3667
DK
3202// Arm_relobj methods.
3203
3204// Scan relocations for stub generation.
3205
3206template<bool big_endian>
3207void
3208Arm_relobj<big_endian>::scan_sections_for_stubs(
3209 Target_arm<big_endian>* arm_target,
3210 const Symbol_table* symtab,
3211 const Layout* layout)
3212{
3213 unsigned int shnum = this->shnum();
3214 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
3215
3216 // Read the section headers.
3217 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
3218 shnum * shdr_size,
3219 true, true);
3220
3221 // To speed up processing, we set up hash tables for fast lookup of
3222 // input offsets to output addresses.
3223 this->initialize_input_to_output_maps();
3224
3225 const Relobj::Output_sections& out_sections(this->output_sections());
3226
3227 Relocate_info<32, big_endian> relinfo;
8ffa3667
DK
3228 relinfo.symtab = symtab;
3229 relinfo.layout = layout;
3230 relinfo.object = this;
3231
3232 const unsigned char* p = pshdrs + shdr_size;
3233 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
3234 {
3235 typename elfcpp::Shdr<32, big_endian> shdr(p);
3236
3237 unsigned int sh_type = shdr.get_sh_type();
3238 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
3239 continue;
3240
3241 off_t sh_size = shdr.get_sh_size();
3242 if (sh_size == 0)
3243 continue;
3244
3245 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
3246 if (index >= this->shnum())
3247 {
3248 // Ignore reloc section with bad info. This error will be
3249 // reported in the final link.
3250 continue;
3251 }
3252
3253 Output_section* os = out_sections[index];
3254 if (os == NULL)
3255 {
3256 // This relocation section is against a section which we
3257 // discarded.
3258 continue;
3259 }
3260 Arm_address output_offset = this->get_output_section_offset(index);
3261
3262 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
3263 {
3264 // Ignore reloc section with unexpected symbol table. The
3265 // error will be reported in the final link.
3266 continue;
3267 }
3268
3269 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
3270 sh_size, true, false);
3271
3272 unsigned int reloc_size;
3273 if (sh_type == elfcpp::SHT_REL)
3274 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
3275 else
3276 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
3277
3278 if (reloc_size != shdr.get_sh_entsize())
3279 {
3280 // Ignore reloc section with unexpected entsize. The error
3281 // will be reported in the final link.
3282 continue;
3283 }
3284
3285 size_t reloc_count = sh_size / reloc_size;
3286 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
3287 {
3288 // Ignore reloc section with uneven size. The error will be
3289 // reported in the final link.
3290 continue;
3291 }
3292
3293 gold_assert(output_offset != invalid_address
3294 || this->relocs_must_follow_section_writes());
3295
3296 // Get the section contents. This does work for the case in which
3297 // we modify the contents of an input section. We need to pass the
3298 // output view under such circumstances.
3299 section_size_type input_view_size = 0;
3300 const unsigned char* input_view =
3301 this->section_contents(index, &input_view_size, false);
3302
3303 relinfo.reloc_shndx = i;
3304 relinfo.data_shndx = index;
3305 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
3306 reloc_count, os,
3307 output_offset == invalid_address,
3308 input_view,
3309 os->address(),
3310 input_view_size);
3311 }
3312
3313 // After we've done the relocations, we release the hash tables,
3314 // since we no longer need them.
3315 this->free_input_to_output_maps();
3316}
3317
3318// Count the local symbols. The ARM backend needs to know if a symbol
3319// is a THUMB function or not. For global symbols, it is easy because
3320// the Symbol object keeps the ELF symbol type. For local symbol it is
3321// harder because we cannot access this information. So we override the
3322// do_count_local_symbol in parent and scan local symbols to mark
3323// THUMB functions. This is not the most efficient way but I do not want to
3324// slow down other ports by calling a per symbol targer hook inside
3325// Sized_relobj<size, big_endian>::do_count_local_symbols.
3326
3327template<bool big_endian>
3328void
3329Arm_relobj<big_endian>::do_count_local_symbols(
3330 Stringpool_template<char>* pool,
3331 Stringpool_template<char>* dynpool)
3332{
3333 // We need to fix-up the values of any local symbols whose type are
3334 // STT_ARM_TFUNC.
3335
3336 // Ask parent to count the local symbols.
3337 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
3338 const unsigned int loccount = this->local_symbol_count();
3339 if (loccount == 0)
3340 return;
3341
3342 // Intialize the thumb function bit-vector.
3343 std::vector<bool> empty_vector(loccount, false);
3344 this->local_symbol_is_thumb_function_.swap(empty_vector);
3345
3346 // Read the symbol table section header.
3347 const unsigned int symtab_shndx = this->symtab_shndx();
3348 elfcpp::Shdr<32, big_endian>
3349 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
3350 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
3351
3352 // Read the local symbols.
3353 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
3354 gold_assert(loccount == symtabshdr.get_sh_info());
3355 off_t locsize = loccount * sym_size;
3356 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
3357 locsize, true, true);
3358
3359 // Loop over the local symbols and mark any local symbols pointing
3360 // to THUMB functions.
3361
3362 // Skip the first dummy symbol.
3363 psyms += sym_size;
3364 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
3365 this->local_values();
3366 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
3367 {
3368 elfcpp::Sym<32, big_endian> sym(psyms);
3369 elfcpp::STT st_type = sym.get_st_type();
3370 Symbol_value<32>& lv((*plocal_values)[i]);
3371 Arm_address input_value = lv.input_value();
3372
3373 if (st_type == elfcpp::STT_ARM_TFUNC
3374 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
3375 {
3376 // This is a THUMB function. Mark this and canonicalize the
3377 // symbol value by setting LSB.
3378 this->local_symbol_is_thumb_function_[i] = true;
3379 if ((input_value & 1) == 0)
3380 lv.set_input_value(input_value | 1);
3381 }
3382 }
3383}
3384
3385// Relocate sections.
3386template<bool big_endian>
3387void
3388Arm_relobj<big_endian>::do_relocate_sections(
8ffa3667
DK
3389 const Symbol_table* symtab,
3390 const Layout* layout,
3391 const unsigned char* pshdrs,
3392 typename Sized_relobj<32, big_endian>::Views* pviews)
3393{
3394 // Call parent to relocate sections.
43d12afe
DK
3395 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
3396 pviews);
8ffa3667
DK
3397
3398 // We do not generate stubs if doing a relocatable link.
3399 if (parameters->options().relocatable())
3400 return;
3401
3402 // Relocate stub tables.
3403 unsigned int shnum = this->shnum();
3404
3405 Target_arm<big_endian>* arm_target =
3406 Target_arm<big_endian>::default_target();
3407
3408 Relocate_info<32, big_endian> relinfo;
8ffa3667
DK
3409 relinfo.symtab = symtab;
3410 relinfo.layout = layout;
3411 relinfo.object = this;
3412
3413 for (unsigned int i = 1; i < shnum; ++i)
3414 {
3415 Arm_input_section<big_endian>* arm_input_section =
3416 arm_target->find_arm_input_section(this, i);
3417
3418 if (arm_input_section == NULL
3419 || !arm_input_section->is_stub_table_owner()
3420 || arm_input_section->stub_table()->empty())
3421 continue;
3422
3423 // We cannot discard a section if it owns a stub table.
3424 Output_section* os = this->output_section(i);
3425 gold_assert(os != NULL);
3426
3427 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
3428 relinfo.reloc_shdr = NULL;
3429 relinfo.data_shndx = i;
3430 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
3431
3432 gold_assert((*pviews)[i].view != NULL);
3433
3434 // We are passed the output section view. Adjust it to cover the
3435 // stub table only.
3436 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
3437 gold_assert((stub_table->address() >= (*pviews)[i].address)
3438 && ((stub_table->address() + stub_table->data_size())
3439 <= (*pviews)[i].address + (*pviews)[i].view_size));
3440
3441 off_t offset = stub_table->address() - (*pviews)[i].address;
3442 unsigned char* view = (*pviews)[i].view + offset;
3443 Arm_address address = stub_table->address();
3444 section_size_type view_size = stub_table->data_size();
3445
3446 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
3447 view_size);
3448 }
3449}
3450
d5b40221
DK
3451// Read the symbol information.
3452
3453template<bool big_endian>
3454void
3455Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
3456{
3457 // Call parent class to read symbol information.
3458 Sized_relobj<32, big_endian>::do_read_symbols(sd);
3459
3460 // Read processor-specific flags in ELF file header.
3461 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
3462 elfcpp::Elf_sizes<32>::ehdr_size,
3463 true, false);
3464 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
3465 this->processor_specific_flags_ = ehdr.get_e_flags();
3466}
3467
3468// Arm_dynobj methods.
3469
3470// Read the symbol information.
3471
3472template<bool big_endian>
3473void
3474Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
3475{
3476 // Call parent class to read symbol information.
3477 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
3478
3479 // Read processor-specific flags in ELF file header.
3480 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
3481 elfcpp::Elf_sizes<32>::ehdr_size,
3482 true, false);
3483 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
3484 this->processor_specific_flags_ = ehdr.get_e_flags();
3485}
3486
e9bbb538
DK
3487// Stub_addend_reader methods.
3488
3489// Read the addend of a REL relocation of type R_TYPE at VIEW.
3490
3491template<bool big_endian>
3492elfcpp::Elf_types<32>::Elf_Swxword
3493Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
3494 unsigned int r_type,
3495 const unsigned char* view,
3496 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
3497{
3498 switch (r_type)
3499 {
3500 case elfcpp::R_ARM_CALL:
3501 case elfcpp::R_ARM_JUMP24:
3502 case elfcpp::R_ARM_PLT32:
3503 {
3504 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3505 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3506 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3507 return utils::sign_extend<26>(val << 2);
3508 }
3509
3510 case elfcpp::R_ARM_THM_CALL:
3511 case elfcpp::R_ARM_THM_JUMP24:
3512 case elfcpp::R_ARM_THM_XPC22:
3513 {
3514 // Fetch the addend. We use the Thumb-2 encoding (backwards
3515 // compatible with Thumb-1) involving the J1 and J2 bits.
3516 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3517 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3518 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3519 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3520
3521 uint32_t s = (upper_insn & (1 << 10)) >> 10;
3522 uint32_t upper = upper_insn & 0x3ff;
3523 uint32_t lower = lower_insn & 0x7ff;
3524 uint32_t j1 = (lower_insn & (1 << 13)) >> 13;
3525 uint32_t j2 = (lower_insn & (1 << 11)) >> 11;
3526 uint32_t i1 = j1 ^ s ? 0 : 1;
3527 uint32_t i2 = j2 ^ s ? 0 : 1;
3528
3529 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
3530 | (upper << 12) | (lower << 1));
3531 }
3532
3533 case elfcpp::R_ARM_THM_JUMP19:
3534 {
3535 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3536 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3537 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3538 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3539
3540 // Reconstruct the top three bits and squish the two 11 bit pieces
3541 // together.
3542 uint32_t S = (upper_insn & 0x0400) >> 10;
3543 uint32_t J1 = (lower_insn & 0x2000) >> 13;
3544 uint32_t J2 = (lower_insn & 0x0800) >> 11;
3545 uint32_t upper =
3546 (S << 8) | (J2 << 7) | (J1 << 6) | (upper_insn & 0x003f);
3547 uint32_t lower = (lower_insn & 0x07ff);
3548 return utils::sign_extend<23>((upper << 12) | (lower << 1));
3549 }
3550
3551 default:
3552 gold_unreachable();
3553 }
3554}
3555
94cdfcff
DK
3556// A class to handle the PLT data.
3557
3558template<bool big_endian>
3559class Output_data_plt_arm : public Output_section_data
3560{
3561 public:
3562 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
3563 Reloc_section;
3564
3565 Output_data_plt_arm(Layout*, Output_data_space*);
3566
3567 // Add an entry to the PLT.
3568 void
3569 add_entry(Symbol* gsym);
3570
3571 // Return the .rel.plt section data.
3572 const Reloc_section*
3573 rel_plt() const
3574 { return this->rel_; }
3575
3576 protected:
3577 void
3578 do_adjust_output_section(Output_section* os);
3579
3580 // Write to a map file.
3581 void
3582 do_print_to_mapfile(Mapfile* mapfile) const
3583 { mapfile->print_output_data(this, _("** PLT")); }
3584
3585 private:
3586 // Template for the first PLT entry.
3587 static const uint32_t first_plt_entry[5];
3588
3589 // Template for subsequent PLT entries.
3590 static const uint32_t plt_entry[3];
3591
3592 // Set the final size.
3593 void
3594 set_final_data_size()
3595 {
3596 this->set_data_size(sizeof(first_plt_entry)
3597 + this->count_ * sizeof(plt_entry));
3598 }
3599
3600 // Write out the PLT data.
3601 void
3602 do_write(Output_file*);
3603
3604 // The reloc section.
3605 Reloc_section* rel_;
3606 // The .got.plt section.
3607 Output_data_space* got_plt_;
3608 // The number of PLT entries.
3609 unsigned int count_;
3610};
3611
3612// Create the PLT section. The ordinary .got section is an argument,
3613// since we need to refer to the start. We also create our own .got
3614// section just for PLT entries.
3615
3616template<bool big_endian>
3617Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
3618 Output_data_space* got_plt)
3619 : Output_section_data(4), got_plt_(got_plt), count_(0)
3620{
3621 this->rel_ = new Reloc_section(false);
3622 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
f5c870d2 3623 elfcpp::SHF_ALLOC, this->rel_, true);
94cdfcff
DK
3624}
3625
3626template<bool big_endian>
3627void
3628Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
3629{
3630 os->set_entsize(0);
3631}
3632
3633// Add an entry to the PLT.
3634
3635template<bool big_endian>
3636void
3637Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
3638{
3639 gold_assert(!gsym->has_plt_offset());
3640
3641 // Note that when setting the PLT offset we skip the initial
3642 // reserved PLT entry.
3643 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
3644 + sizeof(first_plt_entry));
3645
3646 ++this->count_;
3647
3648 section_offset_type got_offset = this->got_plt_->current_data_size();
3649
3650 // Every PLT entry needs a GOT entry which points back to the PLT
3651 // entry (this will be changed by the dynamic linker, normally
3652 // lazily when the function is called).
3653 this->got_plt_->set_current_data_size(got_offset + 4);
3654
3655 // Every PLT entry needs a reloc.
3656 gsym->set_needs_dynsym_entry();
3657 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
3658 got_offset);
3659
3660 // Note that we don't need to save the symbol. The contents of the
3661 // PLT are independent of which symbols are used. The symbols only
3662 // appear in the relocations.
3663}
3664
3665// ARM PLTs.
3666// FIXME: This is not very flexible. Right now this has only been tested
3667// on armv5te. If we are to support additional architecture features like
3668// Thumb-2 or BE8, we need to make this more flexible like GNU ld.
3669
3670// The first entry in the PLT.
3671template<bool big_endian>
3672const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
3673{
3674 0xe52de004, // str lr, [sp, #-4]!
3675 0xe59fe004, // ldr lr, [pc, #4]
3676 0xe08fe00e, // add lr, pc, lr
3677 0xe5bef008, // ldr pc, [lr, #8]!
3678 0x00000000, // &GOT[0] - .
3679};
3680
3681// Subsequent entries in the PLT.
3682
3683template<bool big_endian>
3684const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
3685{
3686 0xe28fc600, // add ip, pc, #0xNN00000
3687 0xe28cca00, // add ip, ip, #0xNN000
3688 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
3689};
3690
3691// Write out the PLT. This uses the hand-coded instructions above,
3692// and adjusts them as needed. This is all specified by the arm ELF
3693// Processor Supplement.
3694
3695template<bool big_endian>
3696void
3697Output_data_plt_arm<big_endian>::do_write(Output_file* of)
3698{
3699 const off_t offset = this->offset();
3700 const section_size_type oview_size =
3701 convert_to_section_size_type(this->data_size());
3702 unsigned char* const oview = of->get_output_view(offset, oview_size);
3703
3704 const off_t got_file_offset = this->got_plt_->offset();
3705 const section_size_type got_size =
3706 convert_to_section_size_type(this->got_plt_->data_size());
3707 unsigned char* const got_view = of->get_output_view(got_file_offset,
3708 got_size);
3709 unsigned char* pov = oview;
3710
ebabffbd
DK
3711 Arm_address plt_address = this->address();
3712 Arm_address got_address = this->got_plt_->address();
94cdfcff
DK
3713
3714 // Write first PLT entry. All but the last word are constants.
3715 const size_t num_first_plt_words = (sizeof(first_plt_entry)
3716 / sizeof(plt_entry[0]));
3717 for (size_t i = 0; i < num_first_plt_words - 1; i++)
3718 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
3719 // Last word in first PLT entry is &GOT[0] - .
3720 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
3721 got_address - (plt_address + 16));
3722 pov += sizeof(first_plt_entry);
3723
3724 unsigned char* got_pov = got_view;
3725
3726 memset(got_pov, 0, 12);
3727 got_pov += 12;
3728
3729 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
3730 unsigned int plt_offset = sizeof(first_plt_entry);
3731 unsigned int plt_rel_offset = 0;
3732 unsigned int got_offset = 12;
3733 const unsigned int count = this->count_;
3734 for (unsigned int i = 0;
3735 i < count;
3736 ++i,
3737 pov += sizeof(plt_entry),
3738 got_pov += 4,
3739 plt_offset += sizeof(plt_entry),
3740 plt_rel_offset += rel_size,
3741 got_offset += 4)
3742 {
3743 // Set and adjust the PLT entry itself.
3744 int32_t offset = ((got_address + got_offset)
3745 - (plt_address + plt_offset + 8));
3746
3747 gold_assert(offset >= 0 && offset < 0x0fffffff);
3748 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
3749 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
3750 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
3751 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
3752 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
3753 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
3754
3755 // Set the entry in the GOT.
3756 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
3757 }
3758
3759 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
3760 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
3761
3762 of->write_output_view(offset, oview_size, oview);
3763 of->write_output_view(got_file_offset, got_size, got_view);
3764}
3765
3766// Create a PLT entry for a global symbol.
3767
3768template<bool big_endian>
3769void
3770Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
3771 Symbol* gsym)
3772{
3773 if (gsym->has_plt_offset())
3774 return;
3775
3776 if (this->plt_ == NULL)
3777 {
3778 // Create the GOT sections first.
3779 this->got_section(symtab, layout);
3780
3781 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
3782 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
3783 (elfcpp::SHF_ALLOC
3784 | elfcpp::SHF_EXECINSTR),
f5c870d2 3785 this->plt_, false);
94cdfcff
DK
3786 }
3787 this->plt_->add_entry(gsym);
3788}
3789
4a657b0d
DK
3790// Report an unsupported relocation against a local symbol.
3791
3792template<bool big_endian>
3793void
3794Target_arm<big_endian>::Scan::unsupported_reloc_local(
3795 Sized_relobj<32, big_endian>* object,
3796 unsigned int r_type)
3797{
3798 gold_error(_("%s: unsupported reloc %u against local symbol"),
3799 object->name().c_str(), r_type);
3800}
3801
bec53400
DK
3802// We are about to emit a dynamic relocation of type R_TYPE. If the
3803// dynamic linker does not support it, issue an error. The GNU linker
3804// only issues a non-PIC error for an allocated read-only section.
3805// Here we know the section is allocated, but we don't know that it is
3806// read-only. But we check for all the relocation types which the
3807// glibc dynamic linker supports, so it seems appropriate to issue an
3808// error even if the section is not read-only.
3809
3810template<bool big_endian>
3811void
3812Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
3813 unsigned int r_type)
3814{
3815 switch (r_type)
3816 {
3817 // These are the relocation types supported by glibc for ARM.
3818 case elfcpp::R_ARM_RELATIVE:
3819 case elfcpp::R_ARM_COPY:
3820 case elfcpp::R_ARM_GLOB_DAT:
3821 case elfcpp::R_ARM_JUMP_SLOT:
3822 case elfcpp::R_ARM_ABS32:
be8fcb75 3823 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
3824 case elfcpp::R_ARM_PC24:
3825 // FIXME: The following 3 types are not supported by Android's dynamic
3826 // linker.
3827 case elfcpp::R_ARM_TLS_DTPMOD32:
3828 case elfcpp::R_ARM_TLS_DTPOFF32:
3829 case elfcpp::R_ARM_TLS_TPOFF32:
3830 return;
3831
3832 default:
3833 // This prevents us from issuing more than one error per reloc
3834 // section. But we can still wind up issuing more than one
3835 // error per object file.
3836 if (this->issued_non_pic_error_)
3837 return;
3838 object->error(_("requires unsupported dynamic reloc; "
3839 "recompile with -fPIC"));
3840 this->issued_non_pic_error_ = true;
3841 return;
3842
3843 case elfcpp::R_ARM_NONE:
3844 gold_unreachable();
3845 }
3846}
3847
4a657b0d 3848// Scan a relocation for a local symbol.
bec53400
DK
3849// FIXME: This only handles a subset of relocation types used by Android
3850// on ARM v5te devices.
4a657b0d
DK
3851
3852template<bool big_endian>
3853inline void
ad0f2072 3854Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
bec53400
DK
3855 Layout* layout,
3856 Target_arm* target,
4a657b0d 3857 Sized_relobj<32, big_endian>* object,
bec53400
DK
3858 unsigned int data_shndx,
3859 Output_section* output_section,
3860 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
3861 unsigned int r_type,
3862 const elfcpp::Sym<32, big_endian>&)
3863{
3864 r_type = get_real_reloc_type(r_type);
3865 switch (r_type)
3866 {
3867 case elfcpp::R_ARM_NONE:
3868 break;
3869
bec53400 3870 case elfcpp::R_ARM_ABS32:
be8fcb75 3871 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
3872 // If building a shared library (or a position-independent
3873 // executable), we need to create a dynamic relocation for
3874 // this location. The relocation applied at link time will
3875 // apply the link-time value, so we flag the location with
3876 // an R_ARM_RELATIVE relocation so the dynamic loader can
3877 // relocate it easily.
3878 if (parameters->options().output_is_position_independent())
3879 {
3880 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3881 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3882 // If we are to add more other reloc types than R_ARM_ABS32,
3883 // we need to add check_non_pic(object, r_type) here.
3884 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
3885 output_section, data_shndx,
3886 reloc.get_r_offset());
3887 }
3888 break;
3889
3890 case elfcpp::R_ARM_REL32:
3891 case elfcpp::R_ARM_THM_CALL:
3892 case elfcpp::R_ARM_CALL:
3893 case elfcpp::R_ARM_PREL31:
3894 case elfcpp::R_ARM_JUMP24:
3895 case elfcpp::R_ARM_PLT32:
be8fcb75
ILT
3896 case elfcpp::R_ARM_THM_ABS5:
3897 case elfcpp::R_ARM_ABS8:
3898 case elfcpp::R_ARM_ABS12:
3899 case elfcpp::R_ARM_ABS16:
3900 case elfcpp::R_ARM_BASE_ABS:
fd3c5f0b
ILT
3901 case elfcpp::R_ARM_MOVW_ABS_NC:
3902 case elfcpp::R_ARM_MOVT_ABS:
3903 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3904 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
3905 case elfcpp::R_ARM_MOVW_PREL_NC:
3906 case elfcpp::R_ARM_MOVT_PREL:
3907 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3908 case elfcpp::R_ARM_THM_MOVT_PREL:
bec53400
DK
3909 break;
3910
3911 case elfcpp::R_ARM_GOTOFF32:
3912 // We need a GOT section:
3913 target->got_section(symtab, layout);
3914 break;
3915
3916 case elfcpp::R_ARM_BASE_PREL:
3917 // FIXME: What about this?
3918 break;
3919
3920 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 3921 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
3922 {
3923 // The symbol requires a GOT entry.
3924 Output_data_got<32, big_endian>* got =
3925 target->got_section(symtab, layout);
3926 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3927 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
3928 {
3929 // If we are generating a shared object, we need to add a
3930 // dynamic RELATIVE relocation for this symbol's GOT entry.
3931 if (parameters->options().output_is_position_independent())
3932 {
3933 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3934 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3935 rel_dyn->add_local_relative(
3936 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
3937 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
3938 }
3939 }
3940 }
3941 break;
3942
3943 case elfcpp::R_ARM_TARGET1:
3944 // This should have been mapped to another type already.
3945 // Fall through.
3946 case elfcpp::R_ARM_COPY:
3947 case elfcpp::R_ARM_GLOB_DAT:
3948 case elfcpp::R_ARM_JUMP_SLOT:
3949 case elfcpp::R_ARM_RELATIVE:
3950 // These are relocations which should only be seen by the
3951 // dynamic linker, and should never be seen here.
3952 gold_error(_("%s: unexpected reloc %u in object file"),
3953 object->name().c_str(), r_type);
3954 break;
3955
4a657b0d
DK
3956 default:
3957 unsupported_reloc_local(object, r_type);
3958 break;
3959 }
3960}
3961
3962// Report an unsupported relocation against a global symbol.
3963
3964template<bool big_endian>
3965void
3966Target_arm<big_endian>::Scan::unsupported_reloc_global(
3967 Sized_relobj<32, big_endian>* object,
3968 unsigned int r_type,
3969 Symbol* gsym)
3970{
3971 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3972 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3973}
3974
3975// Scan a relocation for a global symbol.
bec53400
DK
3976// FIXME: This only handles a subset of relocation types used by Android
3977// on ARM v5te devices.
4a657b0d
DK
3978
3979template<bool big_endian>
3980inline void
ad0f2072 3981Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
bec53400
DK
3982 Layout* layout,
3983 Target_arm* target,
4a657b0d 3984 Sized_relobj<32, big_endian>* object,
bec53400
DK
3985 unsigned int data_shndx,
3986 Output_section* output_section,
3987 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
3988 unsigned int r_type,
3989 Symbol* gsym)
3990{
3991 r_type = get_real_reloc_type(r_type);
3992 switch (r_type)
3993 {
3994 case elfcpp::R_ARM_NONE:
3995 break;
3996
bec53400 3997 case elfcpp::R_ARM_ABS32:
be8fcb75 3998 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
3999 {
4000 // Make a dynamic relocation if necessary.
4001 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
4002 {
4003 if (target->may_need_copy_reloc(gsym))
4004 {
4005 target->copy_reloc(symtab, layout, object,
4006 data_shndx, output_section, gsym, reloc);
4007 }
4008 else if (gsym->can_use_relative_reloc(false))
4009 {
4010 // If we are to add more other reloc types than R_ARM_ABS32,
4011 // we need to add check_non_pic(object, r_type) here.
4012 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4013 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
4014 output_section, object,
4015 data_shndx, reloc.get_r_offset());
4016 }
4017 else
4018 {
4019 // If we are to add more other reloc types than R_ARM_ABS32,
4020 // we need to add check_non_pic(object, r_type) here.
4021 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4022 rel_dyn->add_global(gsym, r_type, output_section, object,
4023 data_shndx, reloc.get_r_offset());
4024 }
4025 }
4026 }
4027 break;
4028
fd3c5f0b
ILT
4029 case elfcpp::R_ARM_MOVW_ABS_NC:
4030 case elfcpp::R_ARM_MOVT_ABS:
4031 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4032 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
4033 case elfcpp::R_ARM_MOVW_PREL_NC:
4034 case elfcpp::R_ARM_MOVT_PREL:
4035 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4036 case elfcpp::R_ARM_THM_MOVT_PREL:
fd3c5f0b
ILT
4037 break;
4038
be8fcb75
ILT
4039 case elfcpp::R_ARM_THM_ABS5:
4040 case elfcpp::R_ARM_ABS8:
4041 case elfcpp::R_ARM_ABS12:
4042 case elfcpp::R_ARM_ABS16:
4043 case elfcpp::R_ARM_BASE_ABS:
4044 {
4045 // No dynamic relocs of this kinds.
4046 // Report the error in case of PIC.
4047 int flags = Symbol::NON_PIC_REF;
4048 if (gsym->type() == elfcpp::STT_FUNC
4049 || gsym->type() == elfcpp::STT_ARM_TFUNC)
4050 flags |= Symbol::FUNCTION_CALL;
4051 if (gsym->needs_dynamic_reloc(flags))
4052 check_non_pic(object, r_type);
4053 }
4054 break;
4055
bec53400
DK
4056 case elfcpp::R_ARM_REL32:
4057 case elfcpp::R_ARM_PREL31:
4058 {
4059 // Make a dynamic relocation if necessary.
4060 int flags = Symbol::NON_PIC_REF;
4061 if (gsym->needs_dynamic_reloc(flags))
4062 {
4063 if (target->may_need_copy_reloc(gsym))
4064 {
4065 target->copy_reloc(symtab, layout, object,
4066 data_shndx, output_section, gsym, reloc);
4067 }
4068 else
4069 {
4070 check_non_pic(object, r_type);
4071 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4072 rel_dyn->add_global(gsym, r_type, output_section, object,
4073 data_shndx, reloc.get_r_offset());
4074 }
4075 }
4076 }
4077 break;
4078
4079 case elfcpp::R_ARM_JUMP24:
4080 case elfcpp::R_ARM_THM_CALL:
4081 case elfcpp::R_ARM_CALL:
4082 {
4083 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
4084 target->make_plt_entry(symtab, layout, gsym);
4085 // Make a dynamic relocation if necessary.
4086 int flags = Symbol::NON_PIC_REF;
4087 if (gsym->type() == elfcpp::STT_FUNC
07800fab 4088 || gsym->type() == elfcpp::STT_ARM_TFUNC)
bec53400
DK
4089 flags |= Symbol::FUNCTION_CALL;
4090 if (gsym->needs_dynamic_reloc(flags))
4091 {
4092 if (target->may_need_copy_reloc(gsym))
4093 {
4094 target->copy_reloc(symtab, layout, object,
4095 data_shndx, output_section, gsym,
4096 reloc);
4097 }
4098 else
4099 {
4100 check_non_pic(object, r_type);
4101 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4102 rel_dyn->add_global(gsym, r_type, output_section, object,
4103 data_shndx, reloc.get_r_offset());
4104 }
4105 }
4106 }
4107 break;
4108
4109 case elfcpp::R_ARM_PLT32:
4110 // If the symbol is fully resolved, this is just a relative
4111 // local reloc. Otherwise we need a PLT entry.
4112 if (gsym->final_value_is_known())
4113 break;
4114 // If building a shared library, we can also skip the PLT entry
4115 // if the symbol is defined in the output file and is protected
4116 // or hidden.
4117 if (gsym->is_defined()
4118 && !gsym->is_from_dynobj()
4119 && !gsym->is_preemptible())
4120 break;
4121 target->make_plt_entry(symtab, layout, gsym);
4122 break;
4123
4124 case elfcpp::R_ARM_GOTOFF32:
4125 // We need a GOT section.
4126 target->got_section(symtab, layout);
4127 break;
4128
4129 case elfcpp::R_ARM_BASE_PREL:
4130 // FIXME: What about this?
4131 break;
4132
4133 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 4134 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
4135 {
4136 // The symbol requires a GOT entry.
4137 Output_data_got<32, big_endian>* got =
4138 target->got_section(symtab, layout);
4139 if (gsym->final_value_is_known())
4140 got->add_global(gsym, GOT_TYPE_STANDARD);
4141 else
4142 {
4143 // If this symbol is not fully resolved, we need to add a
4144 // GOT entry with a dynamic relocation.
4145 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4146 if (gsym->is_from_dynobj()
4147 || gsym->is_undefined()
4148 || gsym->is_preemptible())
4149 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
4150 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
4151 else
4152 {
4153 if (got->add_global(gsym, GOT_TYPE_STANDARD))
4154 rel_dyn->add_global_relative(
4155 gsym, elfcpp::R_ARM_RELATIVE, got,
4156 gsym->got_offset(GOT_TYPE_STANDARD));
4157 }
4158 }
4159 }
4160 break;
4161
4162 case elfcpp::R_ARM_TARGET1:
4163 // This should have been mapped to another type already.
4164 // Fall through.
4165 case elfcpp::R_ARM_COPY:
4166 case elfcpp::R_ARM_GLOB_DAT:
4167 case elfcpp::R_ARM_JUMP_SLOT:
4168 case elfcpp::R_ARM_RELATIVE:
4169 // These are relocations which should only be seen by the
4170 // dynamic linker, and should never be seen here.
4171 gold_error(_("%s: unexpected reloc %u in object file"),
4172 object->name().c_str(), r_type);
4173 break;
4174
4a657b0d
DK
4175 default:
4176 unsupported_reloc_global(object, r_type, gsym);
4177 break;
4178 }
4179}
4180
4181// Process relocations for gc.
4182
4183template<bool big_endian>
4184void
ad0f2072 4185Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
4a657b0d
DK
4186 Layout* layout,
4187 Sized_relobj<32, big_endian>* object,
4188 unsigned int data_shndx,
4189 unsigned int,
4190 const unsigned char* prelocs,
4191 size_t reloc_count,
4192 Output_section* output_section,
4193 bool needs_special_offset_handling,
4194 size_t local_symbol_count,
4195 const unsigned char* plocal_symbols)
4196{
4197 typedef Target_arm<big_endian> Arm;
4198 typedef typename Target_arm<big_endian>::Scan Scan;
4199
4200 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
4a657b0d
DK
4201 symtab,
4202 layout,
4203 this,
4204 object,
4205 data_shndx,
4206 prelocs,
4207 reloc_count,
4208 output_section,
4209 needs_special_offset_handling,
4210 local_symbol_count,
4211 plocal_symbols);
4212}
4213
4214// Scan relocations for a section.
4215
4216template<bool big_endian>
4217void
ad0f2072 4218Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
4a657b0d
DK
4219 Layout* layout,
4220 Sized_relobj<32, big_endian>* object,
4221 unsigned int data_shndx,
4222 unsigned int sh_type,
4223 const unsigned char* prelocs,
4224 size_t reloc_count,
4225 Output_section* output_section,
4226 bool needs_special_offset_handling,
4227 size_t local_symbol_count,
4228 const unsigned char* plocal_symbols)
4229{
4230 typedef typename Target_arm<big_endian>::Scan Scan;
4231 if (sh_type == elfcpp::SHT_RELA)
4232 {
4233 gold_error(_("%s: unsupported RELA reloc section"),
4234 object->name().c_str());
4235 return;
4236 }
4237
4238 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
4a657b0d
DK
4239 symtab,
4240 layout,
4241 this,
4242 object,
4243 data_shndx,
4244 prelocs,
4245 reloc_count,
4246 output_section,
4247 needs_special_offset_handling,
4248 local_symbol_count,
4249 plocal_symbols);
4250}
4251
4252// Finalize the sections.
4253
4254template<bool big_endian>
4255void
d5b40221
DK
4256Target_arm<big_endian>::do_finalize_sections(
4257 Layout* layout,
4258 const Input_objects* input_objects)
4a657b0d 4259{
d5b40221
DK
4260 // Merge processor-specific flags.
4261 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4262 p != input_objects->relobj_end();
4263 ++p)
4264 {
4265 Arm_relobj<big_endian>* arm_relobj =
4266 Arm_relobj<big_endian>::as_arm_relobj(*p);
4267 this->merge_processor_specific_flags(
4268 arm_relobj->name(),
4269 arm_relobj->processor_specific_flags());
4270 }
4271
4272 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
4273 p != input_objects->dynobj_end();
4274 ++p)
4275 {
4276 Arm_dynobj<big_endian>* arm_dynobj =
4277 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
4278 this->merge_processor_specific_flags(
4279 arm_dynobj->name(),
4280 arm_dynobj->processor_specific_flags());
4281 }
4282
94cdfcff
DK
4283 // Fill in some more dynamic tags.
4284 Output_data_dynamic* const odyn = layout->dynamic_data();
4285 if (odyn != NULL)
4286 {
22b127cc
ILT
4287 if (this->got_plt_ != NULL
4288 && this->got_plt_->output_section() != NULL)
94cdfcff
DK
4289 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
4290
22b127cc
ILT
4291 if (this->plt_ != NULL
4292 && this->plt_->output_section() != NULL)
94cdfcff
DK
4293 {
4294 const Output_data* od = this->plt_->rel_plt();
4295 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
4296 odyn->add_section_address(elfcpp::DT_JMPREL, od);
4297 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
4298 }
4299
22b127cc
ILT
4300 if (this->rel_dyn_ != NULL
4301 && this->rel_dyn_->output_section() != NULL)
94cdfcff
DK
4302 {
4303 const Output_data* od = this->rel_dyn_;
4304 odyn->add_section_address(elfcpp::DT_REL, od);
4305 odyn->add_section_size(elfcpp::DT_RELSZ, od);
4306 odyn->add_constant(elfcpp::DT_RELENT,
4307 elfcpp::Elf_sizes<32>::rel_size);
4308 }
4309
4310 if (!parameters->options().shared())
4311 {
4312 // The value of the DT_DEBUG tag is filled in by the dynamic
4313 // linker at run time, and used by the debugger.
4314 odyn->add_constant(elfcpp::DT_DEBUG, 0);
4315 }
4316 }
4317
4318 // Emit any relocs we saved in an attempt to avoid generating COPY
4319 // relocs.
4320 if (this->copy_relocs_.any_saved_relocs())
4321 this->copy_relocs_.emit(this->rel_dyn_section(layout));
11af873f
DK
4322
4323 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4324 // the .ARM.exidx section.
4325 if (!layout->script_options()->saw_phdrs_clause()
4326 && !parameters->options().relocatable())
4327 {
4328 Output_section* exidx_section =
4329 layout->find_output_section(".ARM.exidx");
4330
4331 if (exidx_section != NULL
4332 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
4333 {
4334 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
4335 == NULL);
4336 Output_segment* exidx_segment =
4337 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
f5c870d2
ILT
4338 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
4339 false);
11af873f
DK
4340 }
4341 }
4a657b0d
DK
4342}
4343
bec53400
DK
4344// Return whether a direct absolute static relocation needs to be applied.
4345// In cases where Scan::local() or Scan::global() has created
4346// a dynamic relocation other than R_ARM_RELATIVE, the addend
4347// of the relocation is carried in the data, and we must not
4348// apply the static relocation.
4349
4350template<bool big_endian>
4351inline bool
4352Target_arm<big_endian>::Relocate::should_apply_static_reloc(
4353 const Sized_symbol<32>* gsym,
4354 int ref_flags,
4355 bool is_32bit,
4356 Output_section* output_section)
4357{
4358 // If the output section is not allocated, then we didn't call
4359 // scan_relocs, we didn't create a dynamic reloc, and we must apply
4360 // the reloc here.
4361 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
4362 return true;
4363
4364 // For local symbols, we will have created a non-RELATIVE dynamic
4365 // relocation only if (a) the output is position independent,
4366 // (b) the relocation is absolute (not pc- or segment-relative), and
4367 // (c) the relocation is not 32 bits wide.
4368 if (gsym == NULL)
4369 return !(parameters->options().output_is_position_independent()
4370 && (ref_flags & Symbol::ABSOLUTE_REF)
4371 && !is_32bit);
4372
4373 // For global symbols, we use the same helper routines used in the
4374 // scan pass. If we did not create a dynamic relocation, or if we
4375 // created a RELATIVE dynamic relocation, we should apply the static
4376 // relocation.
4377 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
4378 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
4379 && gsym->can_use_relative_reloc(ref_flags
4380 & Symbol::FUNCTION_CALL);
4381 return !has_dyn || is_rel;
4382}
4383
4a657b0d
DK
4384// Perform a relocation.
4385
4386template<bool big_endian>
4387inline bool
4388Target_arm<big_endian>::Relocate::relocate(
c121c671
DK
4389 const Relocate_info<32, big_endian>* relinfo,
4390 Target_arm* target,
4391 Output_section *output_section,
4392 size_t relnum,
4393 const elfcpp::Rel<32, big_endian>& rel,
4a657b0d 4394 unsigned int r_type,
c121c671
DK
4395 const Sized_symbol<32>* gsym,
4396 const Symbol_value<32>* psymval,
4397 unsigned char* view,
ebabffbd 4398 Arm_address address,
4a657b0d
DK
4399 section_size_type /* view_size */ )
4400{
c121c671
DK
4401 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
4402
4403 r_type = get_real_reloc_type(r_type);
4404
2daedcd6
DK
4405 const Arm_relobj<big_endian>* object =
4406 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
c121c671 4407
2daedcd6
DK
4408 // If the final branch target of a relocation is THUMB instruction, this
4409 // is 1. Otherwise it is 0.
4410 Arm_address thumb_bit = 0;
c121c671 4411 Symbol_value<32> symval;
2daedcd6 4412 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
c121c671 4413 {
2daedcd6
DK
4414 if (gsym != NULL)
4415 {
4416 // This is a global symbol. Determine if we use PLT and if the
4417 // final target is THUMB.
4418 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
4419 {
4420 // This uses a PLT, change the symbol value.
4421 symval.set_output_value(target->plt_section()->address()
4422 + gsym->plt_offset());
4423 psymval = &symval;
4424 }
4425 else
4426 {
4427 // Set thumb bit if symbol:
4428 // -Has type STT_ARM_TFUNC or
4429 // -Has type STT_FUNC, is defined and with LSB in value set.
4430 thumb_bit =
4431 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
4432 || (gsym->type() == elfcpp::STT_FUNC
4433 && !gsym->is_undefined()
4434 && ((psymval->value(object, 0) & 1) != 0)))
4435 ? 1
4436 : 0);
4437 }
4438 }
4439 else
4440 {
4441 // This is a local symbol. Determine if the final target is THUMB.
4442 // We saved this information when all the local symbols were read.
4443 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
4444 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
4445 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
4446 }
4447 }
4448 else
4449 {
4450 // This is a fake relocation synthesized for a stub. It does not have
4451 // a real symbol. We just look at the LSB of the symbol value to
4452 // determine if the target is THUMB or not.
4453 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
c121c671
DK
4454 }
4455
2daedcd6
DK
4456 // Strip LSB if this points to a THUMB target.
4457 if (thumb_bit != 0
4458 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
4459 && ((psymval->value(object, 0) & 1) != 0))
4460 {
4461 Arm_address stripped_value =
4462 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
4463 symval.set_output_value(stripped_value);
4464 psymval = &symval;
4465 }
4466
c121c671
DK
4467 // Get the GOT offset if needed.
4468 // The GOT pointer points to the end of the GOT section.
4469 // We need to subtract the size of the GOT section to get
4470 // the actual offset to use in the relocation.
4471 bool have_got_offset = false;
4472 unsigned int got_offset = 0;
4473 switch (r_type)
4474 {
4475 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 4476 case elfcpp::R_ARM_GOT_PREL:
c121c671
DK
4477 if (gsym != NULL)
4478 {
4479 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4480 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4481 - target->got_size());
4482 }
4483 else
4484 {
4485 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
4486 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4487 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4488 - target->got_size());
4489 }
4490 have_got_offset = true;
4491 break;
4492
4493 default:
4494 break;
4495 }
4496
4497 typename Arm_relocate_functions::Status reloc_status =
4498 Arm_relocate_functions::STATUS_OKAY;
4a657b0d
DK
4499 switch (r_type)
4500 {
4501 case elfcpp::R_ARM_NONE:
4502 break;
4503
5e445df6
ILT
4504 case elfcpp::R_ARM_ABS8:
4505 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4506 output_section))
be8fcb75
ILT
4507 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
4508 break;
4509
4510 case elfcpp::R_ARM_ABS12:
4511 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4512 output_section))
4513 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
4514 break;
4515
4516 case elfcpp::R_ARM_ABS16:
4517 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4518 output_section))
4519 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5e445df6
ILT
4520 break;
4521
c121c671
DK
4522 case elfcpp::R_ARM_ABS32:
4523 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4524 output_section))
4525 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
2daedcd6 4526 thumb_bit);
c121c671
DK
4527 break;
4528
be8fcb75
ILT
4529 case elfcpp::R_ARM_ABS32_NOI:
4530 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4531 output_section))
4532 // No thumb bit for this relocation: (S + A)
4533 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
4534 false);
4535 break;
4536
fd3c5f0b
ILT
4537 case elfcpp::R_ARM_MOVW_ABS_NC:
4538 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4539 output_section))
4540 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
4541 psymval,
2daedcd6 4542 thumb_bit);
fd3c5f0b
ILT
4543 else
4544 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
4545 "a shared object; recompile with -fPIC"));
4546 break;
4547
4548 case elfcpp::R_ARM_MOVT_ABS:
4549 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4550 output_section))
4551 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
4552 else
4553 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
4554 "a shared object; recompile with -fPIC"));
4555 break;
4556
4557 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4558 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4559 output_section))
4560 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
4561 psymval,
2daedcd6 4562 thumb_bit);
fd3c5f0b
ILT
4563 else
4564 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
4565 "making a shared object; recompile with -fPIC"));
4566 break;
4567
4568 case elfcpp::R_ARM_THM_MOVT_ABS:
4569 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4570 output_section))
4571 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
4572 psymval);
4573 else
4574 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
4575 "making a shared object; recompile with -fPIC"));
4576 break;
4577
c2a122b6
ILT
4578 case elfcpp::R_ARM_MOVW_PREL_NC:
4579 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
4580 psymval, address,
2daedcd6 4581 thumb_bit);
c2a122b6
ILT
4582 break;
4583
4584 case elfcpp::R_ARM_MOVT_PREL:
4585 reloc_status = Arm_relocate_functions::movt_prel(view, object,
4586 psymval, address);
4587 break;
4588
4589 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4590 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
4591 psymval, address,
2daedcd6 4592 thumb_bit);
c2a122b6
ILT
4593 break;
4594
4595 case elfcpp::R_ARM_THM_MOVT_PREL:
4596 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
4597 psymval, address);
4598 break;
4599
c121c671
DK
4600 case elfcpp::R_ARM_REL32:
4601 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2daedcd6 4602 address, thumb_bit);
c121c671
DK
4603 break;
4604
be8fcb75
ILT
4605 case elfcpp::R_ARM_THM_ABS5:
4606 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4607 output_section))
4608 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
4609 break;
4610
c121c671
DK
4611 case elfcpp::R_ARM_THM_CALL:
4612 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
2daedcd6 4613 address, thumb_bit);
c121c671
DK
4614 break;
4615
4616 case elfcpp::R_ARM_GOTOFF32:
4617 {
ebabffbd 4618 Arm_address got_origin;
c121c671
DK
4619 got_origin = target->got_plt_section()->address();
4620 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2daedcd6 4621 got_origin, thumb_bit);
c121c671
DK
4622 }
4623 break;
4624
4625 case elfcpp::R_ARM_BASE_PREL:
4626 {
4627 uint32_t origin;
4628 // Get the addressing origin of the output segment defining the
4629 // symbol gsym (AAELF 4.6.1.2 Relocation types)
4630 gold_assert(gsym != NULL);
4631 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
4632 origin = gsym->output_segment()->vaddr();
4633 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
4634 origin = gsym->output_data()->address();
4635 else
4636 {
4637 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4638 _("cannot find origin of R_ARM_BASE_PREL"));
4639 return true;
4640 }
4641 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
4642 }
4643 break;
4644
be8fcb75
ILT
4645 case elfcpp::R_ARM_BASE_ABS:
4646 {
4647 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4648 output_section))
4649 break;
4650
4651 uint32_t origin;
4652 // Get the addressing origin of the output segment defining
4653 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
4654 if (gsym == NULL)
4655 // R_ARM_BASE_ABS with the NULL symbol will give the
4656 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
4657 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
4658 origin = target->got_plt_section()->address();
4659 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
4660 origin = gsym->output_segment()->vaddr();
4661 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
4662 origin = gsym->output_data()->address();
4663 else
4664 {
4665 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4666 _("cannot find origin of R_ARM_BASE_ABS"));
4667 return true;
4668 }
4669
4670 reloc_status = Arm_relocate_functions::base_abs(view, origin);
4671 }
4672 break;
4673
c121c671
DK
4674 case elfcpp::R_ARM_GOT_BREL:
4675 gold_assert(have_got_offset);
4676 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
4677 break;
4678
7f5309a5
ILT
4679 case elfcpp::R_ARM_GOT_PREL:
4680 gold_assert(have_got_offset);
4681 // Get the address origin for GOT PLT, which is allocated right
4682 // after the GOT section, to calculate an absolute address of
4683 // the symbol GOT entry (got_origin + got_offset).
ebabffbd 4684 Arm_address got_origin;
7f5309a5
ILT
4685 got_origin = target->got_plt_section()->address();
4686 reloc_status = Arm_relocate_functions::got_prel(view,
4687 got_origin + got_offset,
4688 address);
4689 break;
4690
c121c671
DK
4691 case elfcpp::R_ARM_PLT32:
4692 gold_assert(gsym == NULL
4693 || gsym->has_plt_offset()
4694 || gsym->final_value_is_known()
4695 || (gsym->is_defined()
4696 && !gsym->is_from_dynobj()
4697 && !gsym->is_preemptible()));
4698 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
2daedcd6 4699 address, thumb_bit);
c121c671
DK
4700 break;
4701
4702 case elfcpp::R_ARM_CALL:
4703 reloc_status = Arm_relocate_functions::call(view, object, psymval,
2daedcd6 4704 address, thumb_bit);
c121c671
DK
4705 break;
4706
4707 case elfcpp::R_ARM_JUMP24:
4708 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
2daedcd6 4709 address, thumb_bit);
c121c671
DK
4710 break;
4711
4712 case elfcpp::R_ARM_PREL31:
4713 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
2daedcd6 4714 address, thumb_bit);
c121c671
DK
4715 break;
4716
4717 case elfcpp::R_ARM_TARGET1:
4718 // This should have been mapped to another type already.
4719 // Fall through.
4720 case elfcpp::R_ARM_COPY:
4721 case elfcpp::R_ARM_GLOB_DAT:
4722 case elfcpp::R_ARM_JUMP_SLOT:
4723 case elfcpp::R_ARM_RELATIVE:
4724 // These are relocations which should only be seen by the
4725 // dynamic linker, and should never be seen here.
4726 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4727 _("unexpected reloc %u in object file"),
4728 r_type);
4729 break;
4730
4731 default:
4732 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4733 _("unsupported reloc %u"),
4734 r_type);
4735 break;
4736 }
4737
4738 // Report any errors.
4739 switch (reloc_status)
4740 {
4741 case Arm_relocate_functions::STATUS_OKAY:
4742 break;
4743 case Arm_relocate_functions::STATUS_OVERFLOW:
4744 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4745 _("relocation overflow in relocation %u"),
4746 r_type);
4747 break;
4748 case Arm_relocate_functions::STATUS_BAD_RELOC:
4749 gold_error_at_location(
4750 relinfo,
4751 relnum,
4752 rel.get_r_offset(),
4753 _("unexpected opcode while processing relocation %u"),
4754 r_type);
4755 break;
4a657b0d
DK
4756 default:
4757 gold_unreachable();
4758 }
4759
4760 return true;
4761}
4762
4763// Relocate section data.
4764
4765template<bool big_endian>
4766void
4767Target_arm<big_endian>::relocate_section(
4768 const Relocate_info<32, big_endian>* relinfo,
4769 unsigned int sh_type,
4770 const unsigned char* prelocs,
4771 size_t reloc_count,
4772 Output_section* output_section,
4773 bool needs_special_offset_handling,
4774 unsigned char* view,
ebabffbd 4775 Arm_address address,
364c7fa5
ILT
4776 section_size_type view_size,
4777 const Reloc_symbol_changes* reloc_symbol_changes)
4a657b0d
DK
4778{
4779 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
4780 gold_assert(sh_type == elfcpp::SHT_REL);
4781
43d12afe
DK
4782 Arm_input_section<big_endian>* arm_input_section =
4783 this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
4784
4785 // This is an ARM input section and the view covers the whole output
4786 // section.
4787 if (arm_input_section != NULL)
4788 {
4789 gold_assert(needs_special_offset_handling);
4790 Arm_address section_address = arm_input_section->address();
4791 section_size_type section_size = arm_input_section->data_size();
4792
4793 gold_assert((arm_input_section->address() >= address)
4794 && ((arm_input_section->address()
4795 + arm_input_section->data_size())
4796 <= (address + view_size)));
4797
4798 off_t offset = section_address - address;
4799 view += offset;
4800 address += offset;
4801 view_size = section_size;
4802 }
4803
4a657b0d
DK
4804 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
4805 Arm_relocate>(
4806 relinfo,
4807 this,
4808 prelocs,
4809 reloc_count,
4810 output_section,
4811 needs_special_offset_handling,
4812 view,
4813 address,
364c7fa5
ILT
4814 view_size,
4815 reloc_symbol_changes);
4a657b0d
DK
4816}
4817
4818// Return the size of a relocation while scanning during a relocatable
4819// link.
4820
4821template<bool big_endian>
4822unsigned int
4823Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
4824 unsigned int r_type,
4825 Relobj* object)
4826{
4827 r_type = get_real_reloc_type(r_type);
4828 switch (r_type)
4829 {
4830 case elfcpp::R_ARM_NONE:
4831 return 0;
4832
5e445df6
ILT
4833 case elfcpp::R_ARM_ABS8:
4834 return 1;
4835
be8fcb75
ILT
4836 case elfcpp::R_ARM_ABS16:
4837 case elfcpp::R_ARM_THM_ABS5:
4838 return 2;
4839
4a657b0d 4840 case elfcpp::R_ARM_ABS32:
be8fcb75
ILT
4841 case elfcpp::R_ARM_ABS32_NOI:
4842 case elfcpp::R_ARM_ABS12:
4843 case elfcpp::R_ARM_BASE_ABS:
4a657b0d
DK
4844 case elfcpp::R_ARM_REL32:
4845 case elfcpp::R_ARM_THM_CALL:
4846 case elfcpp::R_ARM_GOTOFF32:
4847 case elfcpp::R_ARM_BASE_PREL:
4848 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 4849 case elfcpp::R_ARM_GOT_PREL:
4a657b0d
DK
4850 case elfcpp::R_ARM_PLT32:
4851 case elfcpp::R_ARM_CALL:
4852 case elfcpp::R_ARM_JUMP24:
4853 case elfcpp::R_ARM_PREL31:
fd3c5f0b
ILT
4854 case elfcpp::R_ARM_MOVW_ABS_NC:
4855 case elfcpp::R_ARM_MOVT_ABS:
4856 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4857 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
4858 case elfcpp::R_ARM_MOVW_PREL_NC:
4859 case elfcpp::R_ARM_MOVT_PREL:
4860 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4861 case elfcpp::R_ARM_THM_MOVT_PREL:
4a657b0d
DK
4862 return 4;
4863
4864 case elfcpp::R_ARM_TARGET1:
4865 // This should have been mapped to another type already.
4866 // Fall through.
4867 case elfcpp::R_ARM_COPY:
4868 case elfcpp::R_ARM_GLOB_DAT:
4869 case elfcpp::R_ARM_JUMP_SLOT:
4870 case elfcpp::R_ARM_RELATIVE:
4871 // These are relocations which should only be seen by the
4872 // dynamic linker, and should never be seen here.
4873 gold_error(_("%s: unexpected reloc %u in object file"),
4874 object->name().c_str(), r_type);
4875 return 0;
4876
4877 default:
4878 object->error(_("unsupported reloc %u in object file"), r_type);
4879 return 0;
4880 }
4881}
4882
4883// Scan the relocs during a relocatable link.
4884
4885template<bool big_endian>
4886void
4887Target_arm<big_endian>::scan_relocatable_relocs(
4a657b0d
DK
4888 Symbol_table* symtab,
4889 Layout* layout,
4890 Sized_relobj<32, big_endian>* object,
4891 unsigned int data_shndx,
4892 unsigned int sh_type,
4893 const unsigned char* prelocs,
4894 size_t reloc_count,
4895 Output_section* output_section,
4896 bool needs_special_offset_handling,
4897 size_t local_symbol_count,
4898 const unsigned char* plocal_symbols,
4899 Relocatable_relocs* rr)
4900{
4901 gold_assert(sh_type == elfcpp::SHT_REL);
4902
4903 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
4904 Relocatable_size_for_reloc> Scan_relocatable_relocs;
4905
4906 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
4907 Scan_relocatable_relocs>(
4a657b0d
DK
4908 symtab,
4909 layout,
4910 object,
4911 data_shndx,
4912 prelocs,
4913 reloc_count,
4914 output_section,
4915 needs_special_offset_handling,
4916 local_symbol_count,
4917 plocal_symbols,
4918 rr);
4919}
4920
4921// Relocate a section during a relocatable link.
4922
4923template<bool big_endian>
4924void
4925Target_arm<big_endian>::relocate_for_relocatable(
4926 const Relocate_info<32, big_endian>* relinfo,
4927 unsigned int sh_type,
4928 const unsigned char* prelocs,
4929 size_t reloc_count,
4930 Output_section* output_section,
4931 off_t offset_in_output_section,
4932 const Relocatable_relocs* rr,
4933 unsigned char* view,
ebabffbd 4934 Arm_address view_address,
4a657b0d
DK
4935 section_size_type view_size,
4936 unsigned char* reloc_view,
4937 section_size_type reloc_view_size)
4938{
4939 gold_assert(sh_type == elfcpp::SHT_REL);
4940
4941 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
4942 relinfo,
4943 prelocs,
4944 reloc_count,
4945 output_section,
4946 offset_in_output_section,
4947 rr,
4948 view,
4949 view_address,
4950 view_size,
4951 reloc_view,
4952 reloc_view_size);
4953}
4954
94cdfcff
DK
4955// Return the value to use for a dynamic symbol which requires special
4956// treatment. This is how we support equality comparisons of function
4957// pointers across shared library boundaries, as described in the
4958// processor specific ABI supplement.
4959
4a657b0d
DK
4960template<bool big_endian>
4961uint64_t
94cdfcff 4962Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4a657b0d 4963{
94cdfcff
DK
4964 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4965 return this->plt_section()->address() + gsym->plt_offset();
4a657b0d
DK
4966}
4967
4968// Map platform-specific relocs to real relocs
4969//
4970template<bool big_endian>
4971unsigned int
4972Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
4973{
4974 switch (r_type)
4975 {
4976 case elfcpp::R_ARM_TARGET1:
4977 // This is either R_ARM_ABS32 or R_ARM_REL32;
4978 return elfcpp::R_ARM_ABS32;
4979
4980 case elfcpp::R_ARM_TARGET2:
4981 // This can be any reloc type but ususally is R_ARM_GOT_PREL
4982 return elfcpp::R_ARM_GOT_PREL;
4983
4984 default:
4985 return r_type;
4986 }
4987}
4988
d5b40221
DK
4989// Whether if two EABI versions V1 and V2 are compatible.
4990
4991template<bool big_endian>
4992bool
4993Target_arm<big_endian>::are_eabi_versions_compatible(
4994 elfcpp::Elf_Word v1,
4995 elfcpp::Elf_Word v2)
4996{
4997 // v4 and v5 are the same spec before and after it was released,
4998 // so allow mixing them.
4999 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
5000 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
5001 return true;
5002
5003 return v1 == v2;
5004}
5005
5006// Combine FLAGS from an input object called NAME and the processor-specific
5007// flags in the ELF header of the output. Much of this is adapted from the
5008// processor-specific flags merging code in elf32_arm_merge_private_bfd_data
5009// in bfd/elf32-arm.c.
5010
5011template<bool big_endian>
5012void
5013Target_arm<big_endian>::merge_processor_specific_flags(
5014 const std::string& name,
5015 elfcpp::Elf_Word flags)
5016{
5017 if (this->are_processor_specific_flags_set())
5018 {
5019 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
5020
5021 // Nothing to merge if flags equal to those in output.
5022 if (flags == out_flags)
5023 return;
5024
5025 // Complain about various flag mismatches.
5026 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
5027 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
5028 if (!this->are_eabi_versions_compatible(version1, version2))
5029 gold_error(_("Source object %s has EABI version %d but output has "
5030 "EABI version %d."),
5031 name.c_str(),
5032 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
5033 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
5034 }
5035 else
5036 {
5037 // If the input is the default architecture and had the default
5038 // flags then do not bother setting the flags for the output
5039 // architecture, instead allow future merges to do this. If no
5040 // future merges ever set these flags then they will retain their
5041 // uninitialised values, which surprise surprise, correspond
5042 // to the default values.
5043 if (flags == 0)
5044 return;
5045
5046 // This is the first time, just copy the flags.
5047 // We only copy the EABI version for now.
5048 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
5049 }
5050}
5051
5052// Adjust ELF file header.
5053template<bool big_endian>
5054void
5055Target_arm<big_endian>::do_adjust_elf_header(
5056 unsigned char* view,
5057 int len) const
5058{
5059 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
5060
5061 elfcpp::Ehdr<32, big_endian> ehdr(view);
5062 unsigned char e_ident[elfcpp::EI_NIDENT];
5063 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
5064
5065 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
5066 == elfcpp::EF_ARM_EABI_UNKNOWN)
5067 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
5068 else
5069 e_ident[elfcpp::EI_OSABI] = 0;
5070 e_ident[elfcpp::EI_ABIVERSION] = 0;
5071
5072 // FIXME: Do EF_ARM_BE8 adjustment.
5073
5074 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
5075 oehdr.put_e_ident(e_ident);
5076}
5077
5078// do_make_elf_object to override the same function in the base class.
5079// We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
5080// to store ARM specific information. Hence we need to have our own
5081// ELF object creation.
5082
5083template<bool big_endian>
5084Object*
5085Target_arm<big_endian>::do_make_elf_object(
5086 const std::string& name,
5087 Input_file* input_file,
5088 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
5089{
5090 int et = ehdr.get_e_type();
5091 if (et == elfcpp::ET_REL)
5092 {
5093 Arm_relobj<big_endian>* obj =
5094 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
5095 obj->setup();
5096 return obj;
5097 }
5098 else if (et == elfcpp::ET_DYN)
5099 {
5100 Sized_dynobj<32, big_endian>* obj =
5101 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
5102 obj->setup();
5103 return obj;
5104 }
5105 else
5106 {
5107 gold_error(_("%s: unsupported ELF file type %d"),
5108 name.c_str(), et);
5109 return NULL;
5110 }
5111}
5112
55da9579
DK
5113// Return whether a relocation type used the LSB to distinguish THUMB
5114// addresses.
5115template<bool big_endian>
5116bool
5117Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
5118{
5119 switch (r_type)
5120 {
5121 case elfcpp::R_ARM_PC24:
5122 case elfcpp::R_ARM_ABS32:
5123 case elfcpp::R_ARM_REL32:
5124 case elfcpp::R_ARM_SBREL32:
5125 case elfcpp::R_ARM_THM_CALL:
5126 case elfcpp::R_ARM_GLOB_DAT:
5127 case elfcpp::R_ARM_JUMP_SLOT:
5128 case elfcpp::R_ARM_GOTOFF32:
5129 case elfcpp::R_ARM_PLT32:
5130 case elfcpp::R_ARM_CALL:
5131 case elfcpp::R_ARM_JUMP24:
5132 case elfcpp::R_ARM_THM_JUMP24:
5133 case elfcpp::R_ARM_SBREL31:
5134 case elfcpp::R_ARM_PREL31:
5135 case elfcpp::R_ARM_MOVW_ABS_NC:
5136 case elfcpp::R_ARM_MOVW_PREL_NC:
5137 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5138 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5139 case elfcpp::R_ARM_THM_JUMP19:
5140 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
5141 case elfcpp::R_ARM_ALU_PC_G0_NC:
5142 case elfcpp::R_ARM_ALU_PC_G0:
5143 case elfcpp::R_ARM_ALU_PC_G1_NC:
5144 case elfcpp::R_ARM_ALU_PC_G1:
5145 case elfcpp::R_ARM_ALU_PC_G2:
5146 case elfcpp::R_ARM_ALU_SB_G0_NC:
5147 case elfcpp::R_ARM_ALU_SB_G0:
5148 case elfcpp::R_ARM_ALU_SB_G1_NC:
5149 case elfcpp::R_ARM_ALU_SB_G1:
5150 case elfcpp::R_ARM_ALU_SB_G2:
5151 case elfcpp::R_ARM_MOVW_BREL_NC:
5152 case elfcpp::R_ARM_MOVW_BREL:
5153 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
5154 case elfcpp::R_ARM_THM_MOVW_BREL:
5155 return true;
5156 default:
5157 return false;
5158 }
5159}
5160
5161// Stub-generation methods for Target_arm.
5162
5163// Make a new Arm_input_section object.
5164
5165template<bool big_endian>
5166Arm_input_section<big_endian>*
5167Target_arm<big_endian>::new_arm_input_section(
5168 Relobj* relobj,
5169 unsigned int shndx)
5170{
5171 Input_section_specifier iss(relobj, shndx);
5172
5173 Arm_input_section<big_endian>* arm_input_section =
5174 new Arm_input_section<big_endian>(relobj, shndx);
5175 arm_input_section->init();
5176
5177 // Register new Arm_input_section in map for look-up.
5178 std::pair<typename Arm_input_section_map::iterator, bool> ins =
5179 this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section));
5180
5181 // Make sure that it we have not created another Arm_input_section
5182 // for this input section already.
5183 gold_assert(ins.second);
5184
5185 return arm_input_section;
5186}
5187
5188// Find the Arm_input_section object corresponding to the SHNDX-th input
5189// section of RELOBJ.
5190
5191template<bool big_endian>
5192Arm_input_section<big_endian>*
5193Target_arm<big_endian>::find_arm_input_section(
5194 Relobj* relobj,
5195 unsigned int shndx) const
5196{
5197 Input_section_specifier iss(relobj, shndx);
5198 typename Arm_input_section_map::const_iterator p =
5199 this->arm_input_section_map_.find(iss);
5200 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
5201}
5202
5203// Make a new stub table.
5204
5205template<bool big_endian>
5206Stub_table<big_endian>*
5207Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
5208{
5209 Stub_table<big_endian>* stub_table =
5210 new Stub_table<big_endian>(owner);
5211 this->stub_tables_.push_back(stub_table);
5212
5213 stub_table->set_address(owner->address() + owner->data_size());
5214 stub_table->set_file_offset(owner->offset() + owner->data_size());
5215 stub_table->finalize_data_size();
5216
5217 return stub_table;
5218}
5219
eb44217c
DK
5220// Scan a relocation for stub generation.
5221
5222template<bool big_endian>
5223void
5224Target_arm<big_endian>::scan_reloc_for_stub(
5225 const Relocate_info<32, big_endian>* relinfo,
5226 unsigned int r_type,
5227 const Sized_symbol<32>* gsym,
5228 unsigned int r_sym,
5229 const Symbol_value<32>* psymval,
5230 elfcpp::Elf_types<32>::Elf_Swxword addend,
5231 Arm_address address)
5232{
5233 typedef typename Target_arm<big_endian>::Relocate Relocate;
5234
5235 const Arm_relobj<big_endian>* arm_relobj =
5236 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
5237
5238 bool target_is_thumb;
5239 Symbol_value<32> symval;
5240 if (gsym != NULL)
5241 {
5242 // This is a global symbol. Determine if we use PLT and if the
5243 // final target is THUMB.
5244 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
5245 {
5246 // This uses a PLT, change the symbol value.
5247 symval.set_output_value(this->plt_section()->address()
5248 + gsym->plt_offset());
5249 psymval = &symval;
5250 target_is_thumb = false;
5251 }
5252 else if (gsym->is_undefined())
5253 // There is no need to generate a stub symbol is undefined.
5254 return;
5255 else
5256 {
5257 target_is_thumb =
5258 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
5259 || (gsym->type() == elfcpp::STT_FUNC
5260 && !gsym->is_undefined()
5261 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
5262 }
5263 }
5264 else
5265 {
5266 // This is a local symbol. Determine if the final target is THUMB.
5267 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
5268 }
5269
5270 // Strip LSB if this points to a THUMB target.
5271 if (target_is_thumb
5272 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
5273 && ((psymval->value(arm_relobj, 0) & 1) != 0))
5274 {
5275 Arm_address stripped_value =
5276 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
5277 symval.set_output_value(stripped_value);
5278 psymval = &symval;
5279 }
5280
5281 // Get the symbol value.
5282 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
5283
5284 // Owing to pipelining, the PC relative branches below actually skip
5285 // two instructions when the branch offset is 0.
5286 Arm_address destination;
5287 switch (r_type)
5288 {
5289 case elfcpp::R_ARM_CALL:
5290 case elfcpp::R_ARM_JUMP24:
5291 case elfcpp::R_ARM_PLT32:
5292 // ARM branches.
5293 destination = value + addend + 8;
5294 break;
5295 case elfcpp::R_ARM_THM_CALL:
5296 case elfcpp::R_ARM_THM_XPC22:
5297 case elfcpp::R_ARM_THM_JUMP24:
5298 case elfcpp::R_ARM_THM_JUMP19:
5299 // THUMB branches.
5300 destination = value + addend + 4;
5301 break;
5302 default:
5303 gold_unreachable();
5304 }
5305
5306 Stub_type stub_type =
5307 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
5308 target_is_thumb);
5309
5310 // This reloc does not need a stub.
5311 if (stub_type == arm_stub_none)
5312 return;
5313
5314 // Try looking up an existing stub from a stub table.
5315 Stub_table<big_endian>* stub_table =
5316 arm_relobj->stub_table(relinfo->data_shndx);
5317 gold_assert(stub_table != NULL);
5318
5319 // Locate stub by destination.
5320 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
5321
5322 // Create a stub if there is not one already
5323 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
5324 if (stub == NULL)
5325 {
5326 // create a new stub and add it to stub table.
5327 stub = this->stub_factory().make_reloc_stub(stub_type);
5328 stub_table->add_reloc_stub(stub, stub_key);
5329 }
5330
5331 // Record the destination address.
5332 stub->set_destination_address(destination
5333 | (target_is_thumb ? 1 : 0));
5334}
5335
5336// This function scans a relocation sections for stub generation.
5337// The template parameter Relocate must be a class type which provides
5338// a single function, relocate(), which implements the machine
5339// specific part of a relocation.
5340
5341// BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
5342// SHT_REL or SHT_RELA.
5343
5344// PRELOCS points to the relocation data. RELOC_COUNT is the number
5345// of relocs. OUTPUT_SECTION is the output section.
5346// NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
5347// mapped to output offsets.
5348
5349// VIEW is the section data, VIEW_ADDRESS is its memory address, and
5350// VIEW_SIZE is the size. These refer to the input section, unless
5351// NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
5352// the output section.
5353
5354template<bool big_endian>
5355template<int sh_type>
5356void inline
5357Target_arm<big_endian>::scan_reloc_section_for_stubs(
5358 const Relocate_info<32, big_endian>* relinfo,
5359 const unsigned char* prelocs,
5360 size_t reloc_count,
5361 Output_section* output_section,
5362 bool needs_special_offset_handling,
5363 const unsigned char* view,
5364 elfcpp::Elf_types<32>::Elf_Addr view_address,
5365 section_size_type)
5366{
5367 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
5368 const int reloc_size =
5369 Reloc_types<sh_type, 32, big_endian>::reloc_size;
5370
5371 Arm_relobj<big_endian>* arm_object =
5372 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
5373 unsigned int local_count = arm_object->local_symbol_count();
5374
5375 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
5376
5377 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
5378 {
5379 Reltype reloc(prelocs);
5380
5381 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
5382 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
5383 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
5384
5385 r_type = this->get_real_reloc_type(r_type);
5386
5387 // Only a few relocation types need stubs.
5388 if ((r_type != elfcpp::R_ARM_CALL)
5389 && (r_type != elfcpp::R_ARM_JUMP24)
5390 && (r_type != elfcpp::R_ARM_PLT32)
5391 && (r_type != elfcpp::R_ARM_THM_CALL)
5392 && (r_type != elfcpp::R_ARM_THM_XPC22)
5393 && (r_type != elfcpp::R_ARM_THM_JUMP24)
5394 && (r_type != elfcpp::R_ARM_THM_JUMP19))
5395 continue;
5396
5397 section_offset_type offset =
5398 convert_to_section_size_type(reloc.get_r_offset());
5399
5400 if (needs_special_offset_handling)
5401 {
5402 offset = output_section->output_offset(relinfo->object,
5403 relinfo->data_shndx,
5404 offset);
5405 if (offset == -1)
5406 continue;
5407 }
5408
5409 // Get the addend.
5410 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
5411 elfcpp::Elf_types<32>::Elf_Swxword addend =
5412 stub_addend_reader(r_type, view + offset, reloc);
5413
5414 const Sized_symbol<32>* sym;
5415
5416 Symbol_value<32> symval;
5417 const Symbol_value<32> *psymval;
5418 if (r_sym < local_count)
5419 {
5420 sym = NULL;
5421 psymval = arm_object->local_symbol(r_sym);
5422
5423 // If the local symbol belongs to a section we are discarding,
5424 // and that section is a debug section, try to find the
5425 // corresponding kept section and map this symbol to its
5426 // counterpart in the kept section. The symbol must not
5427 // correspond to a section we are folding.
5428 bool is_ordinary;
5429 unsigned int shndx = psymval->input_shndx(&is_ordinary);
5430 if (is_ordinary
5431 && shndx != elfcpp::SHN_UNDEF
5432 && !arm_object->is_section_included(shndx)
5433 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
5434 {
5435 if (comdat_behavior == CB_UNDETERMINED)
5436 {
5437 std::string name =
5438 arm_object->section_name(relinfo->data_shndx);
5439 comdat_behavior = get_comdat_behavior(name.c_str());
5440 }
5441 if (comdat_behavior == CB_PRETEND)
5442 {
5443 bool found;
5444 typename elfcpp::Elf_types<32>::Elf_Addr value =
5445 arm_object->map_to_kept_section(shndx, &found);
5446 if (found)
5447 symval.set_output_value(value + psymval->input_value());
5448 else
5449 symval.set_output_value(0);
5450 }
5451 else
5452 {
5453 symval.set_output_value(0);
5454 }
5455 symval.set_no_output_symtab_entry();
5456 psymval = &symval;
5457 }
5458 }
5459 else
5460 {
5461 const Symbol* gsym = arm_object->global_symbol(r_sym);
5462 gold_assert(gsym != NULL);
5463 if (gsym->is_forwarder())
5464 gsym = relinfo->symtab->resolve_forwards(gsym);
5465
5466 sym = static_cast<const Sized_symbol<32>*>(gsym);
5467 if (sym->has_symtab_index())
5468 symval.set_output_symtab_index(sym->symtab_index());
5469 else
5470 symval.set_no_output_symtab_entry();
5471
5472 // We need to compute the would-be final value of this global
5473 // symbol.
5474 const Symbol_table* symtab = relinfo->symtab;
5475 const Sized_symbol<32>* sized_symbol =
5476 symtab->get_sized_symbol<32>(gsym);
5477 Symbol_table::Compute_final_value_status status;
5478 Arm_address value =
5479 symtab->compute_final_value<32>(sized_symbol, &status);
5480
5481 // Skip this if the symbol has not output section.
5482 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
5483 continue;
5484
5485 symval.set_output_value(value);
5486 psymval = &symval;
5487 }
5488
5489 // If symbol is a section symbol, we don't know the actual type of
5490 // destination. Give up.
5491 if (psymval->is_section_symbol())
5492 continue;
5493
5494 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
5495 addend, view_address + offset);
5496 }
5497}
5498
5499// Scan an input section for stub generation.
5500
5501template<bool big_endian>
5502void
5503Target_arm<big_endian>::scan_section_for_stubs(
5504 const Relocate_info<32, big_endian>* relinfo,
5505 unsigned int sh_type,
5506 const unsigned char* prelocs,
5507 size_t reloc_count,
5508 Output_section* output_section,
5509 bool needs_special_offset_handling,
5510 const unsigned char* view,
5511 Arm_address view_address,
5512 section_size_type view_size)
5513{
5514 if (sh_type == elfcpp::SHT_REL)
5515 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
5516 relinfo,
5517 prelocs,
5518 reloc_count,
5519 output_section,
5520 needs_special_offset_handling,
5521 view,
5522 view_address,
5523 view_size);
5524 else if (sh_type == elfcpp::SHT_RELA)
5525 // We do not support RELA type relocations yet. This is provided for
5526 // completeness.
5527 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
5528 relinfo,
5529 prelocs,
5530 reloc_count,
5531 output_section,
5532 needs_special_offset_handling,
5533 view,
5534 view_address,
5535 view_size);
5536 else
5537 gold_unreachable();
5538}
5539
5540// Group input sections for stub generation.
5541//
5542// We goup input sections in an output sections so that the total size,
5543// including any padding space due to alignment is smaller than GROUP_SIZE
5544// unless the only input section in group is bigger than GROUP_SIZE already.
5545// Then an ARM stub table is created to follow the last input section
5546// in group. For each group an ARM stub table is created an is placed
5547// after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
5548// extend the group after the stub table.
5549
5550template<bool big_endian>
5551void
5552Target_arm<big_endian>::group_sections(
5553 Layout* layout,
5554 section_size_type group_size,
5555 bool stubs_always_after_branch)
5556{
5557 // Group input sections and insert stub table
5558 Layout::Section_list section_list;
5559 layout->get_allocated_sections(&section_list);
5560 for (Layout::Section_list::const_iterator p = section_list.begin();
5561 p != section_list.end();
5562 ++p)
5563 {
5564 Arm_output_section<big_endian>* output_section =
5565 Arm_output_section<big_endian>::as_arm_output_section(*p);
5566 output_section->group_sections(group_size, stubs_always_after_branch,
5567 this);
5568 }
5569}
5570
5571// Relaxation hook. This is where we do stub generation.
5572
5573template<bool big_endian>
5574bool
5575Target_arm<big_endian>::do_relax(
5576 int pass,
5577 const Input_objects* input_objects,
5578 Symbol_table* symtab,
5579 Layout* layout)
5580{
5581 // No need to generate stubs if this is a relocatable link.
5582 gold_assert(!parameters->options().relocatable());
5583
5584 // If this is the first pass, we need to group input sections into
5585 // stub groups.
5586 if (pass == 1)
5587 {
5588 // Determine the stub group size. The group size is the absolute
5589 // value of the parameter --stub-group-size. If --stub-group-size
5590 // is passed a negative value, we restict stubs to be always after
5591 // the stubbed branches.
5592 int32_t stub_group_size_param =
5593 parameters->options().stub_group_size();
5594 bool stubs_always_after_branch = stub_group_size_param < 0;
5595 section_size_type stub_group_size = abs(stub_group_size_param);
5596
5597 if (stub_group_size == 1)
5598 {
5599 // Default value.
5600 // Thumb branch range is +-4MB has to be used as the default
5601 // maximum size (a given section can contain both ARM and Thumb
5602 // code, so the worst case has to be taken into account).
5603 //
5604 // This value is 24K less than that, which allows for 2025
5605 // 12-byte stubs. If we exceed that, then we will fail to link.
5606 // The user will have to relink with an explicit group size
5607 // option.
5608 stub_group_size = 4170000;
5609 }
5610
5611 group_sections(layout, stub_group_size, stubs_always_after_branch);
5612 }
5613
5614 // clear changed flags for all stub_tables
5615 typedef typename Stub_table_list::iterator Stub_table_iterator;
5616 for (Stub_table_iterator sp = this->stub_tables_.begin();
5617 sp != this->stub_tables_.end();
5618 ++sp)
5619 (*sp)->set_has_been_changed(false);
5620
5621 // scan relocs for stubs
5622 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
5623 op != input_objects->relobj_end();
5624 ++op)
5625 {
5626 Arm_relobj<big_endian>* arm_relobj =
5627 Arm_relobj<big_endian>::as_arm_relobj(*op);
5628 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
5629 }
5630
5631 bool any_stub_table_changed = false;
5632 for (Stub_table_iterator sp = this->stub_tables_.begin();
5633 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
5634 ++sp)
5635 {
5636 if ((*sp)->has_been_changed())
5637 any_stub_table_changed = true;
5638 }
5639
5640 return any_stub_table_changed;
5641}
5642
43d12afe
DK
5643// Relocate a stub.
5644
5645template<bool big_endian>
5646void
5647Target_arm<big_endian>::relocate_stub(
5648 Reloc_stub* stub,
5649 const Relocate_info<32, big_endian>* relinfo,
5650 Output_section* output_section,
5651 unsigned char* view,
5652 Arm_address address,
5653 section_size_type view_size)
5654{
5655 Relocate relocate;
5656 const Stub_template* stub_template = stub->stub_template();
5657 for (size_t i = 0; i < stub_template->reloc_count(); i++)
5658 {
5659 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
5660 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
5661
5662 unsigned int r_type = insn->r_type();
5663 section_size_type reloc_offset = stub_template->reloc_offset(i);
5664 section_size_type reloc_size = insn->size();
5665 gold_assert(reloc_offset + reloc_size <= view_size);
5666
5667 // This is the address of the stub destination.
5668 Arm_address target = stub->reloc_target(i);
5669 Symbol_value<32> symval;
5670 symval.set_output_value(target);
5671
5672 // Synthesize a fake reloc just in case. We don't have a symbol so
5673 // we use 0.
5674 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
5675 memset(reloc_buffer, 0, sizeof(reloc_buffer));
5676 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
5677 reloc_write.put_r_offset(reloc_offset);
5678 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
5679 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
5680
5681 relocate.relocate(relinfo, this, output_section,
5682 this->fake_relnum_for_stubs, rel, r_type,
5683 NULL, &symval, view + reloc_offset,
5684 address + reloc_offset, reloc_size);
5685 }
5686}
5687
4a657b0d
DK
5688// The selector for arm object files.
5689
5690template<bool big_endian>
5691class Target_selector_arm : public Target_selector
5692{
5693 public:
5694 Target_selector_arm()
5695 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
5696 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
5697 { }
5698
5699 Target*
5700 do_instantiate_target()
5701 { return new Target_arm<big_endian>(); }
5702};
5703
5704Target_selector_arm<false> target_selector_arm;
5705Target_selector_arm<true> target_selector_armbe;
5706
5707} // End anonymous namespace.
This page took 0.271687 seconds and 4 git commands to generate.