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