*** empty log message ***
[deliverable/binutils-gdb.git] / gold / arm.cc
CommitLineData
4a657b0d
DK
1// arm.cc -- arm target support for gold.
2
3// Copyright 2009 Free Software Foundation, Inc.
4// Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5// by Ian Lance Taylor <iant@google.com>.
b569affa
DK
6// This file also contains borrowed and adapted code from
7// bfd/elf32-arm.c.
4a657b0d
DK
8
9// This file is part of gold.
10
11// This program is free software; you can redistribute it and/or modify
12// it under the terms of the GNU General Public License as published by
13// the Free Software Foundation; either version 3 of the License, or
14// (at your option) any later version.
15
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24// MA 02110-1301, USA.
25
26#include "gold.h"
27
28#include <cstring>
29#include <limits>
30#include <cstdio>
31#include <string>
56ee5e00 32#include <algorithm>
4a657b0d
DK
33
34#include "elfcpp.h"
35#include "parameters.h"
36#include "reloc.h"
37#include "arm.h"
38#include "object.h"
39#include "symtab.h"
40#include "layout.h"
41#include "output.h"
42#include "copy-relocs.h"
43#include "target.h"
44#include "target-reloc.h"
45#include "target-select.h"
46#include "tls.h"
47#include "defstd.h"
f345227a 48#include "gc.h"
4a657b0d
DK
49
50namespace
51{
52
53using namespace gold;
54
94cdfcff
DK
55template<bool big_endian>
56class Output_data_plt_arm;
57
56ee5e00
DK
58template<bool big_endian>
59class Stub_table;
60
61template<bool big_endian>
62class Arm_input_section;
63
07f508a2
DK
64template<bool big_endian>
65class Arm_output_section;
66
67template<bool big_endian>
68class Arm_relobj;
69
b569affa
DK
70template<bool big_endian>
71class Target_arm;
72
73// For convenience.
74typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
75
76// Maximum branch offsets for ARM, THUMB and THUMB2.
77const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
78const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
79const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
80const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
81const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
82const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
83
4a657b0d
DK
84// The arm target class.
85//
86// This is a very simple port of gold for ARM-EABI. It is intended for
87// supporting Android only for the time being. Only these relocation types
88// are supported.
89//
90// R_ARM_NONE
91// R_ARM_ABS32
be8fcb75
ILT
92// R_ARM_ABS32_NOI
93// R_ARM_ABS16
94// R_ARM_ABS12
95// R_ARM_ABS8
96// R_ARM_THM_ABS5
97// R_ARM_BASE_ABS
4a657b0d
DK
98// R_ARM_REL32
99// R_ARM_THM_CALL
100// R_ARM_COPY
101// R_ARM_GLOB_DAT
102// R_ARM_BASE_PREL
103// R_ARM_JUMP_SLOT
104// R_ARM_RELATIVE
105// R_ARM_GOTOFF32
106// R_ARM_GOT_BREL
7f5309a5 107// R_ARM_GOT_PREL
4a657b0d
DK
108// R_ARM_PLT32
109// R_ARM_CALL
110// R_ARM_JUMP24
111// R_ARM_TARGET1
112// R_ARM_PREL31
7f5309a5 113// R_ARM_ABS8
fd3c5f0b
ILT
114// R_ARM_MOVW_ABS_NC
115// R_ARM_MOVT_ABS
116// R_ARM_THM_MOVW_ABS_NC
c2a122b6
ILT
117// R_ARM_THM_MOVT_ABS
118// R_ARM_MOVW_PREL_NC
119// R_ARM_MOVT_PREL
120// R_ARM_THM_MOVW_PREL_NC
121// R_ARM_THM_MOVT_PREL
4a657b0d 122//
4a657b0d 123// TODOs:
11af873f
DK
124// - Generate various branch stubs.
125// - Support interworking.
126// - Define section symbols __exidx_start and __exidx_stop.
4a657b0d 127// - Support more relocation types as needed.
94cdfcff
DK
128// - Make PLTs more flexible for different architecture features like
129// Thumb-2 and BE8.
11af873f 130// There are probably a lot more.
4a657b0d 131
b569affa
DK
132// Instruction template class. This class is similar to the insn_sequence
133// struct in bfd/elf32-arm.c.
134
135class Insn_template
136{
137 public:
138 // Types of instruction templates.
139 enum Type
140 {
141 THUMB16_TYPE = 1,
142 THUMB32_TYPE,
143 ARM_TYPE,
144 DATA_TYPE
145 };
146
147 // Factory methods to create instrunction templates in different formats.
148
149 static const Insn_template
150 thumb16_insn(uint32_t data)
151 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
152
153 // A bit of a hack. A Thumb conditional branch, in which the proper
154 // condition is inserted when we build the stub.
155 static const Insn_template
156 thumb16_bcond_insn(uint32_t data)
157 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 1); }
158
159 static const Insn_template
160 thumb32_insn(uint32_t data)
161 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
162
163 static const Insn_template
164 thumb32_b_insn(uint32_t data, int reloc_addend)
165 {
166 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
167 reloc_addend);
168 }
169
170 static const Insn_template
171 arm_insn(uint32_t data)
172 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
173
174 static const Insn_template
175 arm_rel_insn(unsigned data, int reloc_addend)
176 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
177
178 static const Insn_template
179 data_word(unsigned data, unsigned int r_type, int reloc_addend)
180 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
181
182 // Accessors. This class is used for read-only objects so no modifiers
183 // are provided.
184
185 uint32_t
186 data() const
187 { return this->data_; }
188
189 // Return the instruction sequence type of this.
190 Type
191 type() const
192 { return this->type_; }
193
194 // Return the ARM relocation type of this.
195 unsigned int
196 r_type() const
197 { return this->r_type_; }
198
199 int32_t
200 reloc_addend() const
201 { return this->reloc_addend_; }
202
203 // Return size of instrunction template in bytes.
204 size_t
205 size() const;
206
207 // Return byte-alignment of instrunction template.
208 unsigned
209 alignment() const;
210
211 private:
212 // We make the constructor private to ensure that only the factory
213 // methods are used.
214 inline
215 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
216 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
217 { }
218
219 // Instruction specific data. This is used to store information like
220 // some of the instruction bits.
221 uint32_t data_;
222 // Instruction template type.
223 Type type_;
224 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
225 unsigned int r_type_;
226 // Relocation addend.
227 int32_t reloc_addend_;
228};
229
230// Macro for generating code to stub types. One entry per long/short
231// branch stub
232
233#define DEF_STUBS \
234 DEF_STUB(long_branch_any_any) \
235 DEF_STUB(long_branch_v4t_arm_thumb) \
236 DEF_STUB(long_branch_thumb_only) \
237 DEF_STUB(long_branch_v4t_thumb_thumb) \
238 DEF_STUB(long_branch_v4t_thumb_arm) \
239 DEF_STUB(short_branch_v4t_thumb_arm) \
240 DEF_STUB(long_branch_any_arm_pic) \
241 DEF_STUB(long_branch_any_thumb_pic) \
242 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
243 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
244 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
245 DEF_STUB(long_branch_thumb_only_pic) \
246 DEF_STUB(a8_veneer_b_cond) \
247 DEF_STUB(a8_veneer_b) \
248 DEF_STUB(a8_veneer_bl) \
249 DEF_STUB(a8_veneer_blx)
250
251// Stub types.
252
253#define DEF_STUB(x) arm_stub_##x,
254typedef enum
255 {
256 arm_stub_none,
257 DEF_STUBS
258
259 // First reloc stub type.
260 arm_stub_reloc_first = arm_stub_long_branch_any_any,
261 // Last reloc stub type.
262 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
263
264 // First Cortex-A8 stub type.
265 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
266 // Last Cortex-A8 stub type.
267 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
268
269 // Last stub type.
270 arm_stub_type_last = arm_stub_a8_veneer_blx
271 } Stub_type;
272#undef DEF_STUB
273
274// Stub template class. Templates are meant to be read-only objects.
275// A stub template for a stub type contains all read-only attributes
276// common to all stubs of the same type.
277
278class Stub_template
279{
280 public:
281 Stub_template(Stub_type, const Insn_template*, size_t);
282
283 ~Stub_template()
284 { }
285
286 // Return stub type.
287 Stub_type
288 type() const
289 { return this->type_; }
290
291 // Return an array of instruction templates.
292 const Insn_template*
293 insns() const
294 { return this->insns_; }
295
296 // Return size of template in number of instructions.
297 size_t
298 insn_count() const
299 { return this->insn_count_; }
300
301 // Return size of template in bytes.
302 size_t
303 size() const
304 { return this->size_; }
305
306 // Return alignment of the stub template.
307 unsigned
308 alignment() const
309 { return this->alignment_; }
310
311 // Return whether entry point is in thumb mode.
312 bool
313 entry_in_thumb_mode() const
314 { return this->entry_in_thumb_mode_; }
315
316 // Return number of relocations in this template.
317 size_t
318 reloc_count() const
319 { return this->relocs_.size(); }
320
321 // Return index of the I-th instruction with relocation.
322 size_t
323 reloc_insn_index(size_t i) const
324 {
325 gold_assert(i < this->relocs_.size());
326 return this->relocs_[i].first;
327 }
328
329 // Return the offset of the I-th instruction with relocation from the
330 // beginning of the stub.
331 section_size_type
332 reloc_offset(size_t i) const
333 {
334 gold_assert(i < this->relocs_.size());
335 return this->relocs_[i].second;
336 }
337
338 private:
339 // This contains information about an instruction template with a relocation
340 // and its offset from start of stub.
341 typedef std::pair<size_t, section_size_type> Reloc;
342
343 // A Stub_template may not be copied. We want to share templates as much
344 // as possible.
345 Stub_template(const Stub_template&);
346 Stub_template& operator=(const Stub_template&);
347
348 // Stub type.
349 Stub_type type_;
350 // Points to an array of Insn_templates.
351 const Insn_template* insns_;
352 // Number of Insn_templates in insns_[].
353 size_t insn_count_;
354 // Size of templated instructions in bytes.
355 size_t size_;
356 // Alignment of templated instructions.
357 unsigned alignment_;
358 // Flag to indicate if entry is in thumb mode.
359 bool entry_in_thumb_mode_;
360 // A table of reloc instruction indices and offsets. We can find these by
361 // looking at the instruction templates but we pre-compute and then stash
362 // them here for speed.
363 std::vector<Reloc> relocs_;
364};
365
366//
367// A class for code stubs. This is a base class for different type of
368// stubs used in the ARM target.
369//
370
371class Stub
372{
373 private:
374 static const section_offset_type invalid_offset =
375 static_cast<section_offset_type>(-1);
376
377 public:
378 Stub(const Stub_template* stub_template)
379 : stub_template_(stub_template), offset_(invalid_offset)
380 { }
381
382 virtual
383 ~Stub()
384 { }
385
386 // Return the stub template.
387 const Stub_template*
388 stub_template() const
389 { return this->stub_template_; }
390
391 // Return offset of code stub from beginning of its containing stub table.
392 section_offset_type
393 offset() const
394 {
395 gold_assert(this->offset_ != invalid_offset);
396 return this->offset_;
397 }
398
399 // Set offset of code stub from beginning of its containing stub table.
400 void
401 set_offset(section_offset_type offset)
402 { this->offset_ = offset; }
403
404 // Return the relocation target address of the i-th relocation in the
405 // stub. This must be defined in a child class.
406 Arm_address
407 reloc_target(size_t i)
408 { return this->do_reloc_target(i); }
409
410 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
411 void
412 write(unsigned char* view, section_size_type view_size, bool big_endian)
413 { this->do_write(view, view_size, big_endian); }
414
415 protected:
416 // This must be defined in the child class.
417 virtual Arm_address
418 do_reloc_target(size_t) = 0;
419
420 // This must be defined in the child class.
421 virtual void
422 do_write(unsigned char*, section_size_type, bool) = 0;
423
424 private:
425 // Its template.
426 const Stub_template* stub_template_;
427 // Offset within the section of containing this stub.
428 section_offset_type offset_;
429};
430
431// Reloc stub class. These are stubs we use to fix up relocation because
432// of limited branch ranges.
433
434class Reloc_stub : public Stub
435{
436 public:
437 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
438 // We assume we never jump to this address.
439 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
440
441 // Return destination address.
442 Arm_address
443 destination_address() const
444 {
445 gold_assert(this->destination_address_ != this->invalid_address);
446 return this->destination_address_;
447 }
448
449 // Set destination address.
450 void
451 set_destination_address(Arm_address address)
452 {
453 gold_assert(address != this->invalid_address);
454 this->destination_address_ = address;
455 }
456
457 // Reset destination address.
458 void
459 reset_destination_address()
460 { this->destination_address_ = this->invalid_address; }
461
462 // Determine stub type for a branch of a relocation of R_TYPE going
463 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
464 // the branch target is a thumb instruction. TARGET is used for look
465 // up ARM-specific linker settings.
466 static Stub_type
467 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
468 Arm_address branch_target, bool target_is_thumb);
469
470 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
471 // and an addend. Since we treat global and local symbol differently, we
472 // use a Symbol object for a global symbol and a object-index pair for
473 // a local symbol.
474 class Key
475 {
476 public:
477 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
478 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
479 // and R_SYM must not be invalid_index.
480 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
481 unsigned int r_sym, int32_t addend)
482 : stub_type_(stub_type), addend_(addend)
483 {
484 if (symbol != NULL)
485 {
486 this->r_sym_ = Reloc_stub::invalid_index;
487 this->u_.symbol = symbol;
488 }
489 else
490 {
491 gold_assert(relobj != NULL && r_sym != invalid_index);
492 this->r_sym_ = r_sym;
493 this->u_.relobj = relobj;
494 }
495 }
496
497 ~Key()
498 { }
499
500 // Accessors: Keys are meant to be read-only object so no modifiers are
501 // provided.
502
503 // Return stub type.
504 Stub_type
505 stub_type() const
506 { return this->stub_type_; }
507
508 // Return the local symbol index or invalid_index.
509 unsigned int
510 r_sym() const
511 { return this->r_sym_; }
512
513 // Return the symbol if there is one.
514 const Symbol*
515 symbol() const
516 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
517
518 // Return the relobj if there is one.
519 const Relobj*
520 relobj() const
521 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
522
523 // Whether this equals to another key k.
524 bool
525 eq(const Key& k) const
526 {
527 return ((this->stub_type_ == k.stub_type_)
528 && (this->r_sym_ == k.r_sym_)
529 && ((this->r_sym_ != Reloc_stub::invalid_index)
530 ? (this->u_.relobj == k.u_.relobj)
531 : (this->u_.symbol == k.u_.symbol))
532 && (this->addend_ == k.addend_));
533 }
534
535 // Return a hash value.
536 size_t
537 hash_value() const
538 {
539 return (this->stub_type_
540 ^ this->r_sym_
541 ^ gold::string_hash<char>(
542 (this->r_sym_ != Reloc_stub::invalid_index)
543 ? this->u_.relobj->name().c_str()
544 : this->u_.symbol->name())
545 ^ this->addend_);
546 }
547
548 // Functors for STL associative containers.
549 struct hash
550 {
551 size_t
552 operator()(const Key& k) const
553 { return k.hash_value(); }
554 };
555
556 struct equal_to
557 {
558 bool
559 operator()(const Key& k1, const Key& k2) const
560 { return k1.eq(k2); }
561 };
562
563 // Name of key. This is mainly for debugging.
564 std::string
565 name() const;
566
567 private:
568 // Stub type.
569 Stub_type stub_type_;
570 // If this is a local symbol, this is the index in the defining object.
571 // Otherwise, it is invalid_index for a global symbol.
572 unsigned int r_sym_;
573 // If r_sym_ is invalid index. This points to a global symbol.
574 // Otherwise, this points a relobj. We used the unsized and target
575 // independent Symbol and Relobj classes instead of Arm_symbol and
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
975 do_relocate_sections(const General_options& options,
976 const Symbol_table* symtab, const Layout* layout,
977 const unsigned char* pshdrs,
978 typename Sized_relobj<32, big_endian>::Views* pivews);
979
d5b40221
DK
980 // Read the symbol information.
981 void
982 do_read_symbols(Read_symbols_data* sd);
983
8ffa3667
DK
984 private:
985 // List of stub tables.
986 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
987 Stub_table_list stub_tables_;
988 // Bit vector to tell if a local symbol is a thumb function or not.
989 // This is only valid after do_count_local_symbol is called.
990 std::vector<bool> local_symbol_is_thumb_function_;
d5b40221
DK
991 // processor-specific flags in ELF file header.
992 elfcpp::Elf_Word processor_specific_flags_;
993};
994
995// Arm_dynobj class.
996
997template<bool big_endian>
998class Arm_dynobj : public Sized_dynobj<32, big_endian>
999{
1000 public:
1001 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1002 const elfcpp::Ehdr<32, big_endian>& ehdr)
1003 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1004 processor_specific_flags_(0)
1005 { }
1006
1007 ~Arm_dynobj()
1008 { }
1009
1010 // Downcast a base pointer to an Arm_relobj pointer. This is
1011 // not type-safe but we only use Arm_relobj not the base class.
1012 static Arm_dynobj<big_endian>*
1013 as_arm_dynobj(Dynobj* dynobj)
1014 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1015
1016 // Processor-specific flags in ELF file header. This is valid only after
1017 // reading symbols.
1018 elfcpp::Elf_Word
1019 processor_specific_flags() const
1020 { return this->processor_specific_flags_; }
1021
1022 protected:
1023 // Read the symbol information.
1024 void
1025 do_read_symbols(Read_symbols_data* sd);
1026
1027 private:
1028 // processor-specific flags in ELF file header.
1029 elfcpp::Elf_Word processor_specific_flags_;
8ffa3667
DK
1030};
1031
c121c671
DK
1032// Utilities for manipulating integers of up to 32-bits
1033
1034namespace utils
1035{
1036 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1037 // an int32_t. NO_BITS must be between 1 to 32.
1038 template<int no_bits>
1039 static inline int32_t
1040 sign_extend(uint32_t bits)
1041 {
96d49306 1042 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
1043 if (no_bits == 32)
1044 return static_cast<int32_t>(bits);
1045 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1046 bits &= mask;
1047 uint32_t top_bit = 1U << (no_bits - 1);
1048 int32_t as_signed = static_cast<int32_t>(bits);
1049 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1050 }
1051
1052 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1053 template<int no_bits>
1054 static inline bool
1055 has_overflow(uint32_t bits)
1056 {
96d49306 1057 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
1058 if (no_bits == 32)
1059 return false;
1060 int32_t max = (1 << (no_bits - 1)) - 1;
1061 int32_t min = -(1 << (no_bits - 1));
1062 int32_t as_signed = static_cast<int32_t>(bits);
1063 return as_signed > max || as_signed < min;
1064 }
1065
5e445df6
ILT
1066 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1067 // fits in the given number of bits as either a signed or unsigned value.
1068 // For example, has_signed_unsigned_overflow<8> would check
1069 // -128 <= bits <= 255
1070 template<int no_bits>
1071 static inline bool
1072 has_signed_unsigned_overflow(uint32_t bits)
1073 {
1074 gold_assert(no_bits >= 2 && no_bits <= 32);
1075 if (no_bits == 32)
1076 return false;
1077 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1078 int32_t min = -(1 << (no_bits - 1));
1079 int32_t as_signed = static_cast<int32_t>(bits);
1080 return as_signed > max || as_signed < min;
1081 }
1082
c121c671
DK
1083 // Select bits from A and B using bits in MASK. For each n in [0..31],
1084 // the n-th bit in the result is chosen from the n-th bits of A and B.
1085 // A zero selects A and a one selects B.
1086 static inline uint32_t
1087 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1088 { return (a & ~mask) | (b & mask); }
1089};
1090
4a657b0d
DK
1091template<bool big_endian>
1092class Target_arm : public Sized_target<32, big_endian>
1093{
1094 public:
1095 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1096 Reloc_section;
1097
1098 Target_arm()
94cdfcff
DK
1099 : Sized_target<32, big_endian>(&arm_info),
1100 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
b569affa
DK
1101 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
1102 may_use_blx_(true), should_force_pic_veneer_(false)
4a657b0d
DK
1103 { }
1104
b569affa
DK
1105 // Whether we can use BLX.
1106 bool
1107 may_use_blx() const
1108 { return this->may_use_blx_; }
1109
1110 // Set use-BLX flag.
1111 void
1112 set_may_use_blx(bool value)
1113 { this->may_use_blx_ = value; }
1114
1115 // Whether we force PCI branch veneers.
1116 bool
1117 should_force_pic_veneer() const
1118 { return this->should_force_pic_veneer_; }
1119
1120 // Set PIC veneer flag.
1121 void
1122 set_should_force_pic_veneer(bool value)
1123 { this->should_force_pic_veneer_ = value; }
1124
1125 // Whether we use THUMB-2 instructions.
1126 bool
1127 using_thumb2() const
1128 {
1129 // FIXME: This should not hard-coded.
1130 return false;
1131 }
1132
1133 // Whether we use THUMB/THUMB-2 instructions only.
1134 bool
1135 using_thumb_only() const
1136 {
1137 // FIXME: This should not hard-coded.
1138 return false;
1139 }
1140
4a657b0d
DK
1141 // Process the relocations to determine unreferenced sections for
1142 // garbage collection.
1143 void
ad0f2072 1144 gc_process_relocs(Symbol_table* symtab,
4a657b0d
DK
1145 Layout* layout,
1146 Sized_relobj<32, big_endian>* object,
1147 unsigned int data_shndx,
1148 unsigned int sh_type,
1149 const unsigned char* prelocs,
1150 size_t reloc_count,
1151 Output_section* output_section,
1152 bool needs_special_offset_handling,
1153 size_t local_symbol_count,
1154 const unsigned char* plocal_symbols);
1155
1156 // Scan the relocations to look for symbol adjustments.
1157 void
ad0f2072 1158 scan_relocs(Symbol_table* symtab,
4a657b0d
DK
1159 Layout* layout,
1160 Sized_relobj<32, big_endian>* object,
1161 unsigned int data_shndx,
1162 unsigned int sh_type,
1163 const unsigned char* prelocs,
1164 size_t reloc_count,
1165 Output_section* output_section,
1166 bool needs_special_offset_handling,
1167 size_t local_symbol_count,
1168 const unsigned char* plocal_symbols);
1169
1170 // Finalize the sections.
1171 void
d5b40221 1172 do_finalize_sections(Layout*, const Input_objects*);
4a657b0d 1173
94cdfcff 1174 // Return the value to use for a dynamic symbol which requires special
4a657b0d
DK
1175 // treatment.
1176 uint64_t
1177 do_dynsym_value(const Symbol*) const;
1178
1179 // Relocate a section.
1180 void
1181 relocate_section(const Relocate_info<32, big_endian>*,
1182 unsigned int sh_type,
1183 const unsigned char* prelocs,
1184 size_t reloc_count,
1185 Output_section* output_section,
1186 bool needs_special_offset_handling,
1187 unsigned char* view,
ebabffbd 1188 Arm_address view_address,
364c7fa5
ILT
1189 section_size_type view_size,
1190 const Reloc_symbol_changes*);
4a657b0d
DK
1191
1192 // Scan the relocs during a relocatable link.
1193 void
ad0f2072 1194 scan_relocatable_relocs(Symbol_table* symtab,
4a657b0d
DK
1195 Layout* layout,
1196 Sized_relobj<32, big_endian>* object,
1197 unsigned int data_shndx,
1198 unsigned int sh_type,
1199 const unsigned char* prelocs,
1200 size_t reloc_count,
1201 Output_section* output_section,
1202 bool needs_special_offset_handling,
1203 size_t local_symbol_count,
1204 const unsigned char* plocal_symbols,
1205 Relocatable_relocs*);
1206
1207 // Relocate a section during a relocatable link.
1208 void
1209 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1210 unsigned int sh_type,
1211 const unsigned char* prelocs,
1212 size_t reloc_count,
1213 Output_section* output_section,
1214 off_t offset_in_output_section,
1215 const Relocatable_relocs*,
1216 unsigned char* view,
ebabffbd 1217 Arm_address view_address,
4a657b0d
DK
1218 section_size_type view_size,
1219 unsigned char* reloc_view,
1220 section_size_type reloc_view_size);
1221
1222 // Return whether SYM is defined by the ABI.
1223 bool
1224 do_is_defined_by_abi(Symbol* sym) const
1225 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1226
94cdfcff
DK
1227 // Return the size of the GOT section.
1228 section_size_type
1229 got_size()
1230 {
1231 gold_assert(this->got_ != NULL);
1232 return this->got_->data_size();
1233 }
1234
4a657b0d
DK
1235 // Map platform-specific reloc types
1236 static unsigned int
1237 get_real_reloc_type (unsigned int r_type);
1238
b569affa
DK
1239 // Get the default ARM target.
1240 static const Target_arm<big_endian>&
1241 default_target()
1242 {
1243 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1244 && parameters->target().is_big_endian() == big_endian);
1245 return static_cast<const Target_arm<big_endian>&>(parameters->target());
1246 }
1247
d5b40221
DK
1248 protected:
1249 void
1250 do_adjust_elf_header(unsigned char* view, int len) const;
1251
4a657b0d
DK
1252 private:
1253 // The class which scans relocations.
1254 class Scan
1255 {
1256 public:
1257 Scan()
bec53400 1258 : issued_non_pic_error_(false)
4a657b0d
DK
1259 { }
1260
1261 inline void
ad0f2072 1262 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
4a657b0d
DK
1263 Sized_relobj<32, big_endian>* object,
1264 unsigned int data_shndx,
1265 Output_section* output_section,
1266 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1267 const elfcpp::Sym<32, big_endian>& lsym);
1268
1269 inline void
ad0f2072 1270 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
4a657b0d
DK
1271 Sized_relobj<32, big_endian>* object,
1272 unsigned int data_shndx,
1273 Output_section* output_section,
1274 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1275 Symbol* gsym);
1276
1277 private:
1278 static void
1279 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1280 unsigned int r_type);
1281
1282 static void
1283 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1284 unsigned int r_type, Symbol*);
bec53400
DK
1285
1286 void
1287 check_non_pic(Relobj*, unsigned int r_type);
1288
1289 // Almost identical to Symbol::needs_plt_entry except that it also
1290 // handles STT_ARM_TFUNC.
1291 static bool
1292 symbol_needs_plt_entry(const Symbol* sym)
1293 {
1294 // An undefined symbol from an executable does not need a PLT entry.
1295 if (sym->is_undefined() && !parameters->options().shared())
1296 return false;
1297
1298 return (!parameters->doing_static_link()
1299 && (sym->type() == elfcpp::STT_FUNC
1300 || sym->type() == elfcpp::STT_ARM_TFUNC)
1301 && (sym->is_from_dynobj()
1302 || sym->is_undefined()
1303 || sym->is_preemptible()));
1304 }
1305
1306 // Whether we have issued an error about a non-PIC compilation.
1307 bool issued_non_pic_error_;
4a657b0d
DK
1308 };
1309
1310 // The class which implements relocation.
1311 class Relocate
1312 {
1313 public:
1314 Relocate()
1315 { }
1316
1317 ~Relocate()
1318 { }
1319
bec53400
DK
1320 // Return whether the static relocation needs to be applied.
1321 inline bool
1322 should_apply_static_reloc(const Sized_symbol<32>* gsym,
1323 int ref_flags,
1324 bool is_32bit,
1325 Output_section* output_section);
1326
4a657b0d
DK
1327 // Do a relocation. Return false if the caller should not issue
1328 // any warnings about this relocation.
1329 inline bool
1330 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1331 Output_section*, size_t relnum,
1332 const elfcpp::Rel<32, big_endian>&,
1333 unsigned int r_type, const Sized_symbol<32>*,
1334 const Symbol_value<32>*,
ebabffbd 1335 unsigned char*, Arm_address,
4a657b0d 1336 section_size_type);
c121c671
DK
1337
1338 // Return whether we want to pass flag NON_PIC_REF for this
1339 // reloc.
1340 static inline bool
1341 reloc_is_non_pic (unsigned int r_type)
1342 {
1343 switch (r_type)
1344 {
1345 case elfcpp::R_ARM_REL32:
1346 case elfcpp::R_ARM_THM_CALL:
1347 case elfcpp::R_ARM_CALL:
1348 case elfcpp::R_ARM_JUMP24:
1349 case elfcpp::R_ARM_PREL31:
be8fcb75
ILT
1350 case elfcpp::R_ARM_THM_ABS5:
1351 case elfcpp::R_ARM_ABS8:
1352 case elfcpp::R_ARM_ABS12:
1353 case elfcpp::R_ARM_ABS16:
1354 case elfcpp::R_ARM_BASE_ABS:
c121c671
DK
1355 return true;
1356 default:
1357 return false;
1358 }
1359 }
4a657b0d
DK
1360 };
1361
1362 // A class which returns the size required for a relocation type,
1363 // used while scanning relocs during a relocatable link.
1364 class Relocatable_size_for_reloc
1365 {
1366 public:
1367 unsigned int
1368 get_size_for_reloc(unsigned int, Relobj*);
1369 };
1370
94cdfcff
DK
1371 // Get the GOT section, creating it if necessary.
1372 Output_data_got<32, big_endian>*
1373 got_section(Symbol_table*, Layout*);
1374
1375 // Get the GOT PLT section.
1376 Output_data_space*
1377 got_plt_section() const
1378 {
1379 gold_assert(this->got_plt_ != NULL);
1380 return this->got_plt_;
1381 }
1382
1383 // Create a PLT entry for a global symbol.
1384 void
1385 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1386
1387 // Get the PLT section.
1388 const Output_data_plt_arm<big_endian>*
1389 plt_section() const
1390 {
1391 gold_assert(this->plt_ != NULL);
1392 return this->plt_;
1393 }
1394
1395 // Get the dynamic reloc section, creating it if necessary.
1396 Reloc_section*
1397 rel_dyn_section(Layout*);
1398
1399 // Return true if the symbol may need a COPY relocation.
1400 // References from an executable object to non-function symbols
1401 // defined in a dynamic object may need a COPY relocation.
1402 bool
1403 may_need_copy_reloc(Symbol* gsym)
1404 {
966d4097
DK
1405 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1406 && gsym->may_need_copy_reloc());
94cdfcff
DK
1407 }
1408
1409 // Add a potential copy relocation.
1410 void
1411 copy_reloc(Symbol_table* symtab, Layout* layout,
1412 Sized_relobj<32, big_endian>* object,
1413 unsigned int shndx, Output_section* output_section,
1414 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1415 {
1416 this->copy_relocs_.copy_reloc(symtab, layout,
1417 symtab->get_sized_symbol<32>(sym),
1418 object, shndx, output_section, reloc,
1419 this->rel_dyn_section(layout));
1420 }
1421
d5b40221
DK
1422 // Whether two EABI versions are compatible.
1423 static bool
1424 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
1425
1426 // Merge processor-specific flags from input object and those in the ELF
1427 // header of the output.
1428 void
1429 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
1430
1431 Object*
1432 do_make_elf_object(const std::string&, Input_file*, off_t,
1433 const elfcpp::Ehdr<32, big_endian>& ehdr);
1434
1435 Object*
1436 do_make_elf_object(const std::string&, Input_file*, off_t,
1437 const elfcpp::Ehdr<32, !big_endian>&)
1438 { gold_unreachable(); }
1439
1440 Object*
1441 do_make_elf_object(const std::string&, Input_file*, off_t,
1442 const elfcpp::Ehdr<64, false>&)
1443 { gold_unreachable(); }
1444
1445 Object*
1446 do_make_elf_object(const std::string&, Input_file*, off_t,
1447 const elfcpp::Ehdr<64, true>&)
1448 { gold_unreachable(); }
1449
4a657b0d
DK
1450 // Information about this specific target which we pass to the
1451 // general Target structure.
1452 static const Target::Target_info arm_info;
94cdfcff
DK
1453
1454 // The types of GOT entries needed for this platform.
1455 enum Got_type
1456 {
1457 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
1458 };
1459
1460 // The GOT section.
1461 Output_data_got<32, big_endian>* got_;
1462 // The PLT section.
1463 Output_data_plt_arm<big_endian>* plt_;
1464 // The GOT PLT section.
1465 Output_data_space* got_plt_;
1466 // The dynamic reloc section.
1467 Reloc_section* rel_dyn_;
1468 // Relocs saved to avoid a COPY reloc.
1469 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1470 // Space for variables copied with a COPY reloc.
1471 Output_data_space* dynbss_;
b569affa
DK
1472 // Whether we can use BLX.
1473 bool may_use_blx_;
1474 // Whether we force PIC branch veneers.
1475 bool should_force_pic_veneer_;
4a657b0d
DK
1476};
1477
1478template<bool big_endian>
1479const Target::Target_info Target_arm<big_endian>::arm_info =
1480{
1481 32, // size
1482 big_endian, // is_big_endian
1483 elfcpp::EM_ARM, // machine_code
1484 false, // has_make_symbol
1485 false, // has_resolve
1486 false, // has_code_fill
1487 true, // is_default_stack_executable
1488 '\0', // wrap_char
1489 "/usr/lib/libc.so.1", // dynamic_linker
1490 0x8000, // default_text_segment_address
1491 0x1000, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
1492 0x1000, // common_pagesize (overridable by -z common-page-size)
1493 elfcpp::SHN_UNDEF, // small_common_shndx
1494 elfcpp::SHN_UNDEF, // large_common_shndx
1495 0, // small_common_section_flags
1496 0 // large_common_section_flags
4a657b0d
DK
1497};
1498
c121c671
DK
1499// Arm relocate functions class
1500//
1501
1502template<bool big_endian>
1503class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1504{
1505 public:
1506 typedef enum
1507 {
1508 STATUS_OKAY, // No error during relocation.
1509 STATUS_OVERFLOW, // Relocation oveflow.
1510 STATUS_BAD_RELOC // Relocation cannot be applied.
1511 } Status;
1512
1513 private:
1514 typedef Relocate_functions<32, big_endian> Base;
1515 typedef Arm_relocate_functions<big_endian> This;
1516
1517 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
1518 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
1519 // to distinguish ARM and THUMB functions and it is treated specially.
1520 static inline Symbol_value<32>::Value
1521 arm_symbol_value (const Sized_relobj<32, big_endian> *object,
1522 const Symbol_value<32>* psymval,
1523 Symbol_value<32>::Value addend,
1524 bool has_thumb_bit)
1525 {
1526 typedef Symbol_value<32>::Value Valtype;
1527
1528 if (has_thumb_bit)
1529 {
1530 Valtype raw = psymval->value(object, 0);
1531 Valtype thumb_bit = raw & 1;
1532 return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
1533 }
1534 else
1535 return psymval->value(object, addend);
1536 }
1537
fd3c5f0b
ILT
1538 // Encoding of imm16 argument for movt and movw ARM instructions
1539 // from ARM ARM:
1540 //
1541 // imm16 := imm4 | imm12
1542 //
1543 // 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
1544 // +-------+---------------+-------+-------+-----------------------+
1545 // | | |imm4 | |imm12 |
1546 // +-------+---------------+-------+-------+-----------------------+
1547
1548 // Extract the relocation addend from VAL based on the ARM
1549 // instruction encoding described above.
1550 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1551 extract_arm_movw_movt_addend(
1552 typename elfcpp::Swap<32, big_endian>::Valtype val)
1553 {
1554 // According to the Elf ABI for ARM Architecture the immediate
1555 // field is sign-extended to form the addend.
1556 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1557 }
1558
1559 // Insert X into VAL based on the ARM instruction encoding described
1560 // above.
1561 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1562 insert_val_arm_movw_movt(
1563 typename elfcpp::Swap<32, big_endian>::Valtype val,
1564 typename elfcpp::Swap<32, big_endian>::Valtype x)
1565 {
1566 val &= 0xfff0f000;
1567 val |= x & 0x0fff;
1568 val |= (x & 0xf000) << 4;
1569 return val;
1570 }
1571
1572 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1573 // from ARM ARM:
1574 //
1575 // imm16 := imm4 | i | imm3 | imm8
1576 //
1577 // 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
1578 // +---------+-+-----------+-------++-+-----+-------+---------------+
1579 // | |i| |imm4 || |imm3 | |imm8 |
1580 // +---------+-+-----------+-------++-+-----+-------+---------------+
1581
1582 // Extract the relocation addend from VAL based on the Thumb2
1583 // instruction encoding described above.
1584 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1585 extract_thumb_movw_movt_addend(
1586 typename elfcpp::Swap<32, big_endian>::Valtype val)
1587 {
1588 // According to the Elf ABI for ARM Architecture the immediate
1589 // field is sign-extended to form the addend.
1590 return utils::sign_extend<16>(((val >> 4) & 0xf000)
1591 | ((val >> 15) & 0x0800)
1592 | ((val >> 4) & 0x0700)
1593 | (val & 0x00ff));
1594 }
1595
1596 // Insert X into VAL based on the Thumb2 instruction encoding
1597 // described above.
1598 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1599 insert_val_thumb_movw_movt(
1600 typename elfcpp::Swap<32, big_endian>::Valtype val,
1601 typename elfcpp::Swap<32, big_endian>::Valtype x)
1602 {
1603 val &= 0xfbf08f00;
1604 val |= (x & 0xf000) << 4;
1605 val |= (x & 0x0800) << 15;
1606 val |= (x & 0x0700) << 4;
1607 val |= (x & 0x00ff);
1608 return val;
1609 }
1610
c121c671
DK
1611 // FIXME: This probably only works for Android on ARM v5te. We should
1612 // following GNU ld for the general case.
1613 template<unsigned r_type>
1614 static inline typename This::Status
1615 arm_branch_common(unsigned char *view,
1616 const Sized_relobj<32, big_endian>* object,
1617 const Symbol_value<32>* psymval,
ebabffbd 1618 Arm_address address,
c121c671
DK
1619 bool has_thumb_bit)
1620 {
1621 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1622 Valtype* wv = reinterpret_cast<Valtype*>(view);
1623 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1624
1625 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
1626 && ((val & 0x0f000000UL) == 0x0a000000UL);
1627 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
1628 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
1629 && ((val & 0x0f000000UL) == 0x0b000000UL);
1630 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
1631 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
1632
1633 if (r_type == elfcpp::R_ARM_CALL)
1634 {
1635 if (!insn_is_uncond_bl && !insn_is_blx)
1636 return This::STATUS_BAD_RELOC;
1637 }
1638 else if (r_type == elfcpp::R_ARM_JUMP24)
1639 {
1640 if (!insn_is_b && !insn_is_cond_bl)
1641 return This::STATUS_BAD_RELOC;
1642 }
1643 else if (r_type == elfcpp::R_ARM_PLT32)
1644 {
1645 if (!insn_is_any_branch)
1646 return This::STATUS_BAD_RELOC;
1647 }
1648 else
1649 gold_unreachable();
1650
1651 Valtype addend = utils::sign_extend<26>(val << 2);
1652 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1653 - address);
1654
1655 // If target has thumb bit set, we need to either turn the BL
1656 // into a BLX (for ARMv5 or above) or generate a stub.
1657 if (x & 1)
1658 {
1659 // Turn BL to BLX.
1660 if (insn_is_uncond_bl)
1661 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
1662 else
1663 return This::STATUS_BAD_RELOC;
1664 }
1665 else
1666 gold_assert(!insn_is_blx);
1667
1668 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
1669 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1670 return (utils::has_overflow<26>(x)
1671 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
1672 }
1673
1674 public:
5e445df6
ILT
1675
1676 // R_ARM_ABS8: S + A
1677 static inline typename This::Status
1678 abs8(unsigned char *view,
1679 const Sized_relobj<32, big_endian>* object,
be8fcb75 1680 const Symbol_value<32>* psymval)
5e445df6
ILT
1681 {
1682 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
1683 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1684 Valtype* wv = reinterpret_cast<Valtype*>(view);
1685 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
1686 Reltype addend = utils::sign_extend<8>(val);
be8fcb75 1687 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
5e445df6
ILT
1688 val = utils::bit_select(val, x, 0xffU);
1689 elfcpp::Swap<8, big_endian>::writeval(wv, val);
1690 return (utils::has_signed_unsigned_overflow<8>(x)
1691 ? This::STATUS_OVERFLOW
1692 : This::STATUS_OKAY);
1693 }
1694
be8fcb75
ILT
1695 // R_ARM_THM_ABS5: S + A
1696 static inline typename This::Status
1697 thm_abs5(unsigned char *view,
1698 const Sized_relobj<32, big_endian>* object,
1699 const Symbol_value<32>* psymval)
1700 {
1701 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1702 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1703 Valtype* wv = reinterpret_cast<Valtype*>(view);
1704 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1705 Reltype addend = (val & 0x7e0U) >> 6;
1706 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1707 val = utils::bit_select(val, x << 6, 0x7e0U);
1708 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1709 return (utils::has_overflow<5>(x)
1710 ? This::STATUS_OVERFLOW
1711 : This::STATUS_OKAY);
1712 }
1713
1714 // R_ARM_ABS12: S + A
1715 static inline typename This::Status
1716 abs12(unsigned char *view,
1717 const Sized_relobj<32, big_endian>* object,
1718 const Symbol_value<32>* psymval)
1719 {
1720 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1721 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1722 Valtype* wv = reinterpret_cast<Valtype*>(view);
1723 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1724 Reltype addend = val & 0x0fffU;
1725 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1726 val = utils::bit_select(val, x, 0x0fffU);
1727 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1728 return (utils::has_overflow<12>(x)
1729 ? This::STATUS_OVERFLOW
1730 : This::STATUS_OKAY);
1731 }
1732
1733 // R_ARM_ABS16: S + A
1734 static inline typename This::Status
1735 abs16(unsigned char *view,
1736 const Sized_relobj<32, big_endian>* object,
1737 const Symbol_value<32>* psymval)
1738 {
1739 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1740 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1741 Valtype* wv = reinterpret_cast<Valtype*>(view);
1742 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1743 Reltype addend = utils::sign_extend<16>(val);
1744 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1745 val = utils::bit_select(val, x, 0xffffU);
1746 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1747 return (utils::has_signed_unsigned_overflow<16>(x)
1748 ? This::STATUS_OVERFLOW
1749 : This::STATUS_OKAY);
1750 }
1751
c121c671
DK
1752 // R_ARM_ABS32: (S + A) | T
1753 static inline typename This::Status
1754 abs32(unsigned char *view,
1755 const Sized_relobj<32, big_endian>* object,
1756 const Symbol_value<32>* psymval,
1757 bool has_thumb_bit)
1758 {
1759 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1760 Valtype* wv = reinterpret_cast<Valtype*>(view);
1761 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1762 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1763 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1764 return This::STATUS_OKAY;
1765 }
1766
1767 // R_ARM_REL32: (S + A) | T - P
1768 static inline typename This::Status
1769 rel32(unsigned char *view,
1770 const Sized_relobj<32, big_endian>* object,
1771 const Symbol_value<32>* psymval,
ebabffbd 1772 Arm_address address,
c121c671
DK
1773 bool has_thumb_bit)
1774 {
1775 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1776 Valtype* wv = reinterpret_cast<Valtype*>(view);
1777 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1778 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1779 - address);
1780 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1781 return This::STATUS_OKAY;
1782 }
1783
1784 // R_ARM_THM_CALL: (S + A) | T - P
1785 static inline typename This::Status
1786 thm_call(unsigned char *view,
1787 const Sized_relobj<32, big_endian>* object,
1788 const Symbol_value<32>* psymval,
ebabffbd 1789 Arm_address address,
c121c671
DK
1790 bool has_thumb_bit)
1791 {
1792 // A thumb call consists of two instructions.
1793 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1794 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1795 Valtype* wv = reinterpret_cast<Valtype*>(view);
1796 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
1797 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
1798 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
1799 gold_assert((lo & 0xf800) == 0xf800);
1800 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
1801 | ((lo & 0x7ff) << 1));
1802 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1803 - address);
1804
1805 // If target has no thumb bit set, we need to either turn the BL
1806 // into a BLX (for ARMv5 or above) or generate a stub.
1807 if ((x & 1) == 0)
1808 {
1809 // This only works for ARMv5 and above with interworking enabled.
1810 lo &= 0xefff;
1811 }
1812 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
1813 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
1814 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
1815 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
1816 return (utils::has_overflow<23>(x)
1817 ? This::STATUS_OVERFLOW
1818 : This::STATUS_OKAY);
1819 }
1820
1821 // R_ARM_BASE_PREL: B(S) + A - P
1822 static inline typename This::Status
1823 base_prel(unsigned char* view,
ebabffbd
DK
1824 Arm_address origin,
1825 Arm_address address)
c121c671
DK
1826 {
1827 Base::rel32(view, origin - address);
1828 return STATUS_OKAY;
1829 }
1830
be8fcb75
ILT
1831 // R_ARM_BASE_ABS: B(S) + A
1832 static inline typename This::Status
1833 base_abs(unsigned char* view,
ebabffbd 1834 Arm_address origin)
be8fcb75
ILT
1835 {
1836 Base::rel32(view, origin);
1837 return STATUS_OKAY;
1838 }
1839
c121c671
DK
1840 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1841 static inline typename This::Status
1842 got_brel(unsigned char* view,
1843 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
1844 {
1845 Base::rel32(view, got_offset);
1846 return This::STATUS_OKAY;
1847 }
1848
7f5309a5
ILT
1849 // R_ARM_GOT_PREL: GOT(S) + A – P
1850 static inline typename This::Status
1851 got_prel(unsigned char* view,
1852 typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
ebabffbd 1853 Arm_address address)
7f5309a5
ILT
1854 {
1855 Base::rel32(view, got_offset - address);
1856 return This::STATUS_OKAY;
1857 }
1858
c121c671
DK
1859 // R_ARM_PLT32: (S + A) | T - P
1860 static inline typename This::Status
1861 plt32(unsigned char *view,
1862 const Sized_relobj<32, big_endian>* object,
1863 const Symbol_value<32>* psymval,
ebabffbd 1864 Arm_address address,
c121c671
DK
1865 bool has_thumb_bit)
1866 {
1867 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
1868 address, has_thumb_bit);
1869 }
1870
1871 // R_ARM_CALL: (S + A) | T - P
1872 static inline typename This::Status
1873 call(unsigned char *view,
1874 const Sized_relobj<32, big_endian>* object,
1875 const Symbol_value<32>* psymval,
ebabffbd 1876 Arm_address address,
c121c671
DK
1877 bool has_thumb_bit)
1878 {
1879 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
1880 address, has_thumb_bit);
1881 }
1882
1883 // R_ARM_JUMP24: (S + A) | T - P
1884 static inline typename This::Status
1885 jump24(unsigned char *view,
1886 const Sized_relobj<32, big_endian>* object,
1887 const Symbol_value<32>* psymval,
ebabffbd 1888 Arm_address address,
c121c671
DK
1889 bool has_thumb_bit)
1890 {
1891 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
1892 address, has_thumb_bit);
1893 }
1894
1895 // R_ARM_PREL: (S + A) | T - P
1896 static inline typename This::Status
1897 prel31(unsigned char *view,
1898 const Sized_relobj<32, big_endian>* object,
1899 const Symbol_value<32>* psymval,
ebabffbd 1900 Arm_address address,
c121c671
DK
1901 bool has_thumb_bit)
1902 {
1903 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1904 Valtype* wv = reinterpret_cast<Valtype*>(view);
1905 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1906 Valtype addend = utils::sign_extend<31>(val);
1907 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1908 - address);
1909 val = utils::bit_select(val, x, 0x7fffffffU);
1910 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1911 return (utils::has_overflow<31>(x) ?
1912 This::STATUS_OVERFLOW : This::STATUS_OKAY);
1913 }
fd3c5f0b
ILT
1914
1915 // R_ARM_MOVW_ABS_NC: (S + A) | T
1916 static inline typename This::Status
1917 movw_abs_nc(unsigned char *view,
1918 const Sized_relobj<32, big_endian>* object,
1919 const Symbol_value<32>* psymval,
1920 bool has_thumb_bit)
1921 {
1922 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1923 Valtype* wv = reinterpret_cast<Valtype*>(view);
1924 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1925 Valtype addend = This::extract_arm_movw_movt_addend(val);
1926 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1927 val = This::insert_val_arm_movw_movt(val, x);
1928 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1929 return This::STATUS_OKAY;
1930 }
1931
1932 // R_ARM_MOVT_ABS: S + A
1933 static inline typename This::Status
1934 movt_abs(unsigned char *view,
1935 const Sized_relobj<32, big_endian>* object,
1936 const Symbol_value<32>* psymval)
1937 {
1938 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1939 Valtype* wv = reinterpret_cast<Valtype*>(view);
1940 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1941 Valtype addend = This::extract_arm_movw_movt_addend(val);
1942 Valtype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
1943 val = This::insert_val_arm_movw_movt(val, x);
1944 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1945 return This::STATUS_OKAY;
1946 }
1947
1948 // R_ARM_THM_MOVW_ABS_NC: S + A | T
1949 static inline typename This::Status
1950 thm_movw_abs_nc(unsigned char *view,
1951 const Sized_relobj<32, big_endian>* object,
1952 const Symbol_value<32>* psymval,
1953 bool has_thumb_bit)
1954 {
1955 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1956 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1957 Valtype* wv = reinterpret_cast<Valtype*>(view);
1958 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1959 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
1960 Reltype addend = extract_thumb_movw_movt_addend(val);
1961 Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1962 val = This::insert_val_thumb_movw_movt(val, x);
1963 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1964 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1965 return This::STATUS_OKAY;
1966 }
1967
1968 // R_ARM_THM_MOVT_ABS: S + A
1969 static inline typename This::Status
1970 thm_movt_abs(unsigned char *view,
1971 const Sized_relobj<32, big_endian>* object,
1972 const Symbol_value<32>* psymval)
1973 {
1974 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1975 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1976 Valtype* wv = reinterpret_cast<Valtype*>(view);
1977 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1978 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
1979 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1980 Reltype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
1981 val = This::insert_val_thumb_movw_movt(val, x);
1982 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1983 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1984 return This::STATUS_OKAY;
1985 }
1986
c2a122b6
ILT
1987 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
1988 static inline typename This::Status
1989 movw_prel_nc(unsigned char *view,
1990 const Sized_relobj<32, big_endian>* object,
1991 const Symbol_value<32>* psymval,
ebabffbd 1992 Arm_address address,
c2a122b6
ILT
1993 bool has_thumb_bit)
1994 {
1995 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1996 Valtype* wv = reinterpret_cast<Valtype*>(view);
1997 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1998 Valtype addend = This::extract_arm_movw_movt_addend(val);
1999 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
2000 - address);
2001 val = This::insert_val_arm_movw_movt(val, x);
2002 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2003 return This::STATUS_OKAY;
2004 }
2005
2006 // R_ARM_MOVT_PREL: S + A - P
2007 static inline typename This::Status
2008 movt_prel(unsigned char *view,
2009 const Sized_relobj<32, big_endian>* object,
2010 const Symbol_value<32>* psymval,
ebabffbd 2011 Arm_address address)
c2a122b6
ILT
2012 {
2013 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2014 Valtype* wv = reinterpret_cast<Valtype*>(view);
2015 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2016 Valtype addend = This::extract_arm_movw_movt_addend(val);
2017 Valtype x = (This::arm_symbol_value(object, psymval, addend, 0)
2018 - address) >> 16;
2019 val = This::insert_val_arm_movw_movt(val, x);
2020 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2021 return This::STATUS_OKAY;
2022 }
2023
2024 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2025 static inline typename This::Status
2026 thm_movw_prel_nc(unsigned char *view,
2027 const Sized_relobj<32, big_endian>* object,
2028 const Symbol_value<32>* psymval,
ebabffbd 2029 Arm_address address,
c2a122b6
ILT
2030 bool has_thumb_bit)
2031 {
2032 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2033 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2034 Valtype* wv = reinterpret_cast<Valtype*>(view);
2035 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2036 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2037 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2038 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
2039 - address);
2040 val = This::insert_val_thumb_movw_movt(val, x);
2041 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2042 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2043 return This::STATUS_OKAY;
2044 }
2045
2046 // R_ARM_THM_MOVT_PREL: S + A - P
2047 static inline typename This::Status
2048 thm_movt_prel(unsigned char *view,
2049 const Sized_relobj<32, big_endian>* object,
2050 const Symbol_value<32>* psymval,
ebabffbd 2051 Arm_address address)
c2a122b6
ILT
2052 {
2053 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2054 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2055 Valtype* wv = reinterpret_cast<Valtype*>(view);
2056 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2057 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2058 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2059 Reltype x = (This::arm_symbol_value(object, psymval, addend, 0)
2060 - address) >> 16;
2061 val = This::insert_val_thumb_movw_movt(val, x);
2062 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2063 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2064 return This::STATUS_OKAY;
2065 }
c121c671
DK
2066};
2067
94cdfcff
DK
2068// Get the GOT section, creating it if necessary.
2069
2070template<bool big_endian>
2071Output_data_got<32, big_endian>*
2072Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
2073{
2074 if (this->got_ == NULL)
2075 {
2076 gold_assert(symtab != NULL && layout != NULL);
2077
2078 this->got_ = new Output_data_got<32, big_endian>();
2079
2080 Output_section* os;
2081 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2082 (elfcpp::SHF_ALLOC
2083 | elfcpp::SHF_WRITE),
2084 this->got_);
2085 os->set_is_relro();
2086
2087 // The old GNU linker creates a .got.plt section. We just
2088 // create another set of data in the .got section. Note that we
2089 // always create a PLT if we create a GOT, although the PLT
2090 // might be empty.
2091 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
2092 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2093 (elfcpp::SHF_ALLOC
2094 | elfcpp::SHF_WRITE),
2095 this->got_plt_);
2096 os->set_is_relro();
2097
2098 // The first three entries are reserved.
2099 this->got_plt_->set_current_data_size(3 * 4);
2100
2101 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2102 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2103 this->got_plt_,
2104 0, 0, elfcpp::STT_OBJECT,
2105 elfcpp::STB_LOCAL,
2106 elfcpp::STV_HIDDEN, 0,
2107 false, false);
2108 }
2109 return this->got_;
2110}
2111
2112// Get the dynamic reloc section, creating it if necessary.
2113
2114template<bool big_endian>
2115typename Target_arm<big_endian>::Reloc_section*
2116Target_arm<big_endian>::rel_dyn_section(Layout* layout)
2117{
2118 if (this->rel_dyn_ == NULL)
2119 {
2120 gold_assert(layout != NULL);
2121 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
2122 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
2123 elfcpp::SHF_ALLOC, this->rel_dyn_);
2124 }
2125 return this->rel_dyn_;
2126}
2127
b569affa
DK
2128// Insn_template methods.
2129
2130// Return byte size of an instruction template.
2131
2132size_t
2133Insn_template::size() const
2134{
2135 switch (this->type())
2136 {
2137 case THUMB16_TYPE:
2138 return 2;
2139 case ARM_TYPE:
2140 case THUMB32_TYPE:
2141 case DATA_TYPE:
2142 return 4;
2143 default:
2144 gold_unreachable();
2145 }
2146}
2147
2148// Return alignment of an instruction template.
2149
2150unsigned
2151Insn_template::alignment() const
2152{
2153 switch (this->type())
2154 {
2155 case THUMB16_TYPE:
2156 case THUMB32_TYPE:
2157 return 2;
2158 case ARM_TYPE:
2159 case DATA_TYPE:
2160 return 4;
2161 default:
2162 gold_unreachable();
2163 }
2164}
2165
2166// Stub_template methods.
2167
2168Stub_template::Stub_template(
2169 Stub_type type, const Insn_template* insns,
2170 size_t insn_count)
2171 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
2172 entry_in_thumb_mode_(false), relocs_()
2173{
2174 off_t offset = 0;
2175
2176 // Compute byte size and alignment of stub template.
2177 for (size_t i = 0; i < insn_count; i++)
2178 {
2179 unsigned insn_alignment = insns[i].alignment();
2180 size_t insn_size = insns[i].size();
2181 gold_assert((offset & (insn_alignment - 1)) == 0);
2182 this->alignment_ = std::max(this->alignment_, insn_alignment);
2183 switch (insns[i].type())
2184 {
2185 case Insn_template::THUMB16_TYPE:
2186 if (i == 0)
2187 this->entry_in_thumb_mode_ = true;
2188 break;
2189
2190 case Insn_template::THUMB32_TYPE:
2191 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
2192 this->relocs_.push_back(Reloc(i, offset));
2193 if (i == 0)
2194 this->entry_in_thumb_mode_ = true;
2195 break;
2196
2197 case Insn_template::ARM_TYPE:
2198 // Handle cases where the target is encoded within the
2199 // instruction.
2200 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
2201 this->relocs_.push_back(Reloc(i, offset));
2202 break;
2203
2204 case Insn_template::DATA_TYPE:
2205 // Entry point cannot be data.
2206 gold_assert(i != 0);
2207 this->relocs_.push_back(Reloc(i, offset));
2208 break;
2209
2210 default:
2211 gold_unreachable();
2212 }
2213 offset += insn_size;
2214 }
2215 this->size_ = offset;
2216}
2217
2218// Reloc_stub::Key methods.
2219
2220// Dump a Key as a string for debugging.
2221
2222std::string
2223Reloc_stub::Key::name() const
2224{
2225 if (this->r_sym_ == invalid_index)
2226 {
2227 // Global symbol key name
2228 // <stub-type>:<symbol name>:<addend>.
2229 const std::string sym_name = this->u_.symbol->name();
2230 // We need to print two hex number and two colons. So just add 100 bytes
2231 // to the symbol name size.
2232 size_t len = sym_name.size() + 100;
2233 char* buffer = new char[len];
2234 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
2235 sym_name.c_str(), this->addend_);
2236 gold_assert(c > 0 && c < static_cast<int>(len));
2237 delete[] buffer;
2238 return std::string(buffer);
2239 }
2240 else
2241 {
2242 // local symbol key name
2243 // <stub-type>:<object>:<r_sym>:<addend>.
2244 const size_t len = 200;
2245 char buffer[len];
2246 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
2247 this->u_.relobj, this->r_sym_, this->addend_);
2248 gold_assert(c > 0 && c < static_cast<int>(len));
2249 return std::string(buffer);
2250 }
2251}
2252
2253// Reloc_stub methods.
2254
2255// Determine the type of stub needed, if any, for a relocation of R_TYPE at
2256// LOCATION to DESTINATION.
2257// This code is based on the arm_type_of_stub function in
2258// bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2259// class simple.
2260
2261Stub_type
2262Reloc_stub::stub_type_for_reloc(
2263 unsigned int r_type,
2264 Arm_address location,
2265 Arm_address destination,
2266 bool target_is_thumb)
2267{
2268 Stub_type stub_type = arm_stub_none;
2269
2270 // This is a bit ugly but we want to avoid using a templated class for
2271 // big and little endianities.
2272 bool may_use_blx;
2273 bool should_force_pic_veneer;
2274 bool thumb2;
2275 bool thumb_only;
2276 if (parameters->target().is_big_endian())
2277 {
2278 const Target_arm<true>& big_endian_target =
2279 Target_arm<true>::default_target();
2280 may_use_blx = big_endian_target.may_use_blx();
2281 should_force_pic_veneer = big_endian_target.should_force_pic_veneer();
2282 thumb2 = big_endian_target.using_thumb2();
2283 thumb_only = big_endian_target.using_thumb_only();
2284 }
2285 else
2286 {
2287 const Target_arm<false>& little_endian_target =
2288 Target_arm<false>::default_target();
2289 may_use_blx = little_endian_target.may_use_blx();
2290 should_force_pic_veneer = little_endian_target.should_force_pic_veneer();
2291 thumb2 = little_endian_target.using_thumb2();
2292 thumb_only = little_endian_target.using_thumb_only();
2293 }
2294
2295 int64_t branch_offset = (int64_t)destination - location;
2296
2297 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
2298 {
2299 // Handle cases where:
2300 // - this call goes too far (different Thumb/Thumb2 max
2301 // distance)
2302 // - it's a Thumb->Arm call and blx is not available, or it's a
2303 // Thumb->Arm branch (not bl). A stub is needed in this case.
2304 if ((!thumb2
2305 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2306 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2307 || (thumb2
2308 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2309 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2310 || ((!target_is_thumb)
2311 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2312 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
2313 {
2314 if (target_is_thumb)
2315 {
2316 // Thumb to thumb.
2317 if (!thumb_only)
2318 {
2319 stub_type = (parameters->options().shared() | should_force_pic_veneer)
2320 // PIC stubs.
2321 ? ((may_use_blx
2322 && (r_type == elfcpp::R_ARM_THM_CALL))
2323 // V5T and above. Stub starts with ARM code, so
2324 // we must be able to switch mode before
2325 // reaching it, which is only possible for 'bl'
2326 // (ie R_ARM_THM_CALL relocation).
2327 ? arm_stub_long_branch_any_thumb_pic
2328 // On V4T, use Thumb code only.
2329 : arm_stub_long_branch_v4t_thumb_thumb_pic)
2330
2331 // non-PIC stubs.
2332 : ((may_use_blx
2333 && (r_type == elfcpp::R_ARM_THM_CALL))
2334 ? arm_stub_long_branch_any_any // V5T and above.
2335 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
2336 }
2337 else
2338 {
2339 stub_type = (parameters->options().shared() | should_force_pic_veneer)
2340 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
2341 : arm_stub_long_branch_thumb_only; // non-PIC stub.
2342 }
2343 }
2344 else
2345 {
2346 // Thumb to arm.
2347
2348 // FIXME: We should check that the input section is from an
2349 // object that has interwork enabled.
2350
2351 stub_type = (parameters->options().shared()
2352 || should_force_pic_veneer)
2353 // PIC stubs.
2354 ? ((may_use_blx
2355 && (r_type == elfcpp::R_ARM_THM_CALL))
2356 ? arm_stub_long_branch_any_arm_pic // V5T and above.
2357 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
2358
2359 // non-PIC stubs.
2360 : ((may_use_blx
2361 && (r_type == elfcpp::R_ARM_THM_CALL))
2362 ? arm_stub_long_branch_any_any // V5T and above.
2363 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
2364
2365 // Handle v4t short branches.
2366 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
2367 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
2368 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
2369 stub_type = arm_stub_short_branch_v4t_thumb_arm;
2370 }
2371 }
2372 }
2373 else if (r_type == elfcpp::R_ARM_CALL
2374 || r_type == elfcpp::R_ARM_JUMP24
2375 || r_type == elfcpp::R_ARM_PLT32)
2376 {
2377 if (target_is_thumb)
2378 {
2379 // Arm to thumb.
2380
2381 // FIXME: We should check that the input section is from an
2382 // object that has interwork enabled.
2383
2384 // We have an extra 2-bytes reach because of
2385 // the mode change (bit 24 (H) of BLX encoding).
2386 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
2387 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2388 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
2389 || (r_type == elfcpp::R_ARM_JUMP24)
2390 || (r_type == elfcpp::R_ARM_PLT32))
2391 {
2392 stub_type = (parameters->options().shared()
2393 || should_force_pic_veneer)
2394 // PIC stubs.
2395 ? (may_use_blx
2396 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
2397 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
2398
2399 // non-PIC stubs.
2400 : (may_use_blx
2401 ? arm_stub_long_branch_any_any // V5T and above.
2402 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
2403 }
2404 }
2405 else
2406 {
2407 // Arm to arm.
2408 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
2409 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
2410 {
2411 stub_type = (parameters->options().shared()
2412 || should_force_pic_veneer)
2413 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
2414 : arm_stub_long_branch_any_any; /// non-PIC.
2415 }
2416 }
2417 }
2418
2419 return stub_type;
2420}
2421
2422// Template to implement do_write for a specific target endianity.
2423
2424template<bool big_endian>
2425void inline
2426Reloc_stub::do_fixed_endian_write(unsigned char* view,
2427 section_size_type view_size)
2428{
2429 const Stub_template* stub_template = this->stub_template();
2430 const Insn_template* insns = stub_template->insns();
2431
2432 // FIXME: We do not handle BE8 encoding yet.
2433 unsigned char* pov = view;
2434 for (size_t i = 0; i < stub_template->insn_count(); i++)
2435 {
2436 switch (insns[i].type())
2437 {
2438 case Insn_template::THUMB16_TYPE:
2439 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2440 gold_assert(insns[i].reloc_addend() == 0);
2441 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2442 break;
2443 case Insn_template::THUMB32_TYPE:
2444 {
2445 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2446 uint32_t lo = insns[i].data() & 0xffff;
2447 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2448 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2449 }
2450 break;
2451 case Insn_template::ARM_TYPE:
2452 case Insn_template::DATA_TYPE:
2453 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2454 break;
2455 default:
2456 gold_unreachable();
2457 }
2458 pov += insns[i].size();
2459 }
2460 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2461}
2462
2463// Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2464
2465void
2466Reloc_stub::do_write(unsigned char* view, section_size_type view_size,
2467 bool big_endian)
2468{
2469 if (big_endian)
2470 this->do_fixed_endian_write<true>(view, view_size);
2471 else
2472 this->do_fixed_endian_write<false>(view, view_size);
2473}
2474
2475// Stub_factory methods.
2476
2477Stub_factory::Stub_factory()
2478{
2479 // The instruction template sequences are declared as static
2480 // objects and initialized first time the constructor runs.
2481
2482 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2483 // to reach the stub if necessary.
2484 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
2485 {
2486 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2487 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2488 // dcd R_ARM_ABS32(X)
2489 };
2490
2491 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2492 // available.
2493 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2494 {
2495 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2496 Insn_template::arm_insn(0xe12fff1c), // bx ip
2497 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2498 // dcd R_ARM_ABS32(X)
2499 };
2500
2501 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2502 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
2503 {
2504 Insn_template::thumb16_insn(0xb401), // push {r0}
2505 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2506 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2507 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2508 Insn_template::thumb16_insn(0x4760), // bx ip
2509 Insn_template::thumb16_insn(0xbf00), // nop
2510 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2511 // dcd R_ARM_ABS32(X)
2512 };
2513
2514 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2515 // allowed.
2516 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2517 {
2518 Insn_template::thumb16_insn(0x4778), // bx pc
2519 Insn_template::thumb16_insn(0x46c0), // nop
2520 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2521 Insn_template::arm_insn(0xe12fff1c), // bx ip
2522 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2523 // dcd R_ARM_ABS32(X)
2524 };
2525
2526 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2527 // available.
2528 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2529 {
2530 Insn_template::thumb16_insn(0x4778), // bx pc
2531 Insn_template::thumb16_insn(0x46c0), // nop
2532 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2533 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2534 // dcd R_ARM_ABS32(X)
2535 };
2536
2537 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2538 // one, when the destination is close enough.
2539 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2540 {
2541 Insn_template::thumb16_insn(0x4778), // bx pc
2542 Insn_template::thumb16_insn(0x46c0), // nop
2543 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2544 };
2545
2546 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2547 // blx to reach the stub if necessary.
2548 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
2549 {
2550 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2551 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2552 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2553 // dcd R_ARM_REL32(X-4)
2554 };
2555
2556 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2557 // blx to reach the stub if necessary. We can not add into pc;
2558 // it is not guaranteed to mode switch (different in ARMv6 and
2559 // ARMv7).
2560 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
2561 {
2562 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2563 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2564 Insn_template::arm_insn(0xe12fff1c), // bx ip
2565 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2566 // dcd R_ARM_REL32(X)
2567 };
2568
2569 // V4T ARM -> ARM long branch stub, PIC.
2570 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2571 {
2572 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2573 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2574 Insn_template::arm_insn(0xe12fff1c), // bx ip
2575 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2576 // dcd R_ARM_REL32(X)
2577 };
2578
2579 // V4T Thumb -> ARM long branch stub, PIC.
2580 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2581 {
2582 Insn_template::thumb16_insn(0x4778), // bx pc
2583 Insn_template::thumb16_insn(0x46c0), // nop
2584 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2585 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
2586 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2587 // dcd R_ARM_REL32(X)
2588 };
2589
2590 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
2591 // architectures.
2592 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
2593 {
2594 Insn_template::thumb16_insn(0xb401), // push {r0}
2595 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2596 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
2597 Insn_template::thumb16_insn(0x4484), // add ip, r0
2598 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2599 Insn_template::thumb16_insn(0x4760), // bx ip
2600 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
2601 // dcd R_ARM_REL32(X)
2602 };
2603
2604 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2605 // allowed.
2606 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2607 {
2608 Insn_template::thumb16_insn(0x4778), // bx pc
2609 Insn_template::thumb16_insn(0x46c0), // nop
2610 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2611 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2612 Insn_template::arm_insn(0xe12fff1c), // bx ip
2613 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2614 // dcd R_ARM_REL32(X)
2615 };
2616
2617 // Cortex-A8 erratum-workaround stubs.
2618
2619 // Stub used for conditional branches (which may be beyond +/-1MB away,
2620 // so we can't use a conditional branch to reach this stub).
2621
2622 // original code:
2623 //
2624 // b<cond> X
2625 // after:
2626 //
2627 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
2628 {
2629 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
2630 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
2631 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
2632 // b.w X
2633 };
2634
2635 // Stub used for b.w and bl.w instructions.
2636
2637 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
2638 {
2639 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2640 };
2641
2642 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
2643 {
2644 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2645 };
2646
2647 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
2648 // instruction (which switches to ARM mode) to point to this stub. Jump to
2649 // the real destination using an ARM-mode branch.
2650 const Insn_template elf32_arm_stub_a8_veneer_blx[] =
2651 {
2652 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
2653 };
2654
2655 // Fill in the stub template look-up table. Stub templates are constructed
2656 // per instance of Stub_factory for fast look-up without locking
2657 // in a thread-enabled environment.
2658
2659 this->stub_templates_[arm_stub_none] =
2660 new Stub_template(arm_stub_none, NULL, 0);
2661
2662#define DEF_STUB(x) \
2663 do \
2664 { \
2665 size_t array_size \
2666 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
2667 Stub_type type = arm_stub_##x; \
2668 this->stub_templates_[type] = \
2669 new Stub_template(type, elf32_arm_stub_##x, array_size); \
2670 } \
2671 while (0);
2672
2673 DEF_STUBS
2674#undef DEF_STUB
2675}
2676
56ee5e00
DK
2677// Stub_table methods.
2678
2679// Add a STUB with using KEY. Caller is reponsible for avoid adding
2680// if already a STUB with the same key has been added.
2681
2682template<bool big_endian>
2683void
2684Stub_table<big_endian>::add_reloc_stub(
2685 Reloc_stub* stub,
2686 const Reloc_stub::Key& key)
2687{
2688 const Stub_template* stub_template = stub->stub_template();
2689 gold_assert(stub_template->type() == key.stub_type());
2690 this->reloc_stubs_[key] = stub;
2691 if (this->addralign_ < stub_template->alignment())
2692 this->addralign_ = stub_template->alignment();
2693 this->has_been_changed_ = true;
2694}
2695
2696template<bool big_endian>
2697void
2698Stub_table<big_endian>::relocate_stubs(
2699 const Relocate_info<32, big_endian>* relinfo,
2700 Target_arm<big_endian>* arm_target,
2701 Output_section* output_section,
2702 unsigned char* view,
2703 Arm_address address,
2704 section_size_type view_size)
2705{
2706 // If we are passed a view bigger than the stub table's. we need to
2707 // adjust the view.
2708 gold_assert(address == this->address()
2709 && (view_size
2710 == static_cast<section_size_type>(this->data_size())));
2711
2712 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2713 p != this->reloc_stubs_.end();
2714 ++p)
2715 {
2716 Reloc_stub* stub = p->second;
2717 const Stub_template* stub_template = stub->stub_template();
2718 if (stub_template->reloc_count() != 0)
2719 {
2720 // Adjust view to cover the stub only.
2721 section_size_type offset = stub->offset();
2722 section_size_type stub_size = stub_template->size();
2723 gold_assert(offset + stub_size <= view_size);
2724
2725 arm_target->relocate_stub(stub, relinfo, output_section,
2726 view + offset, address + offset,
2727 stub_size);
2728 }
2729 }
2730}
2731
2732// Reset address and file offset.
2733
2734template<bool big_endian>
2735void
2736Stub_table<big_endian>::do_reset_address_and_file_offset()
2737{
2738 off_t off = 0;
2739 uint64_t max_addralign = 1;
2740 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2741 p != this->reloc_stubs_.end();
2742 ++p)
2743 {
2744 Reloc_stub* stub = p->second;
2745 const Stub_template* stub_template = stub->stub_template();
2746 uint64_t stub_addralign = stub_template->alignment();
2747 max_addralign = std::max(max_addralign, stub_addralign);
2748 off = align_address(off, stub_addralign);
2749 stub->set_offset(off);
2750 stub->reset_destination_address();
2751 off += stub_template->size();
2752 }
2753
2754 this->addralign_ = max_addralign;
2755 this->set_current_data_size_for_child(off);
2756}
2757
2758// Write out the stubs to file.
2759
2760template<bool big_endian>
2761void
2762Stub_table<big_endian>::do_write(Output_file* of)
2763{
2764 off_t offset = this->offset();
2765 const section_size_type oview_size =
2766 convert_to_section_size_type(this->data_size());
2767 unsigned char* const oview = of->get_output_view(offset, oview_size);
2768
2769 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2770 p != this->reloc_stubs_.end();
2771 ++p)
2772 {
2773 Reloc_stub* stub = p->second;
2774 Arm_address address = this->address() + stub->offset();
2775 gold_assert(address
2776 == align_address(address,
2777 stub->stub_template()->alignment()));
2778 stub->write(oview + stub->offset(), stub->stub_template()->size(),
2779 big_endian);
2780 }
2781 of->write_output_view(this->offset(), oview_size, oview);
2782}
2783
10ad9fe5
DK
2784// Arm_input_section methods.
2785
2786// Initialize an Arm_input_section.
2787
2788template<bool big_endian>
2789void
2790Arm_input_section<big_endian>::init()
2791{
2792 Relobj* relobj = this->relobj();
2793 unsigned int shndx = this->shndx();
2794
2795 // Cache these to speed up size and alignment queries. It is too slow
2796 // to call section_addraglin and section_size every time.
2797 this->original_addralign_ = relobj->section_addralign(shndx);
2798 this->original_size_ = relobj->section_size(shndx);
2799
2800 // We want to make this look like the original input section after
2801 // output sections are finalized.
2802 Output_section* os = relobj->output_section(shndx);
2803 off_t offset = relobj->output_section_offset(shndx);
2804 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2805 this->set_address(os->address() + offset);
2806 this->set_file_offset(os->offset() + offset);
2807
2808 this->set_current_data_size(this->original_size_);
2809 this->finalize_data_size();
2810}
2811
2812template<bool big_endian>
2813void
2814Arm_input_section<big_endian>::do_write(Output_file* of)
2815{
2816 // We have to write out the original section content.
2817 section_size_type section_size;
2818 const unsigned char* section_contents =
2819 this->relobj()->section_contents(this->shndx(), &section_size, false);
2820 of->write(this->offset(), section_contents, section_size);
2821
2822 // If this owns a stub table and it is not empty, write it.
2823 if (this->is_stub_table_owner() && !this->stub_table_->empty())
2824 this->stub_table_->write(of);
2825}
2826
2827// Finalize data size.
2828
2829template<bool big_endian>
2830void
2831Arm_input_section<big_endian>::set_final_data_size()
2832{
2833 // If this owns a stub table, finalize its data size as well.
2834 if (this->is_stub_table_owner())
2835 {
2836 uint64_t address = this->address();
2837
2838 // The stub table comes after the original section contents.
2839 address += this->original_size_;
2840 address = align_address(address, this->stub_table_->addralign());
2841 off_t offset = this->offset() + (address - this->address());
2842 this->stub_table_->set_address_and_file_offset(address, offset);
2843 address += this->stub_table_->data_size();
2844 gold_assert(address == this->address() + this->current_data_size());
2845 }
2846
2847 this->set_data_size(this->current_data_size());
2848}
2849
2850// Reset address and file offset.
2851
2852template<bool big_endian>
2853void
2854Arm_input_section<big_endian>::do_reset_address_and_file_offset()
2855{
2856 // Size of the original input section contents.
2857 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2858
2859 // If this is a stub table owner, account for the stub table size.
2860 if (this->is_stub_table_owner())
2861 {
2862 Stub_table<big_endian>* stub_table = this->stub_table_;
2863
2864 // Reset the stub table's address and file offset. The
2865 // current data size for child will be updated after that.
2866 stub_table_->reset_address_and_file_offset();
2867 off = align_address(off, stub_table_->addralign());
2868 off += stub_table->current_data_size();
2869 }
2870
2871 this->set_current_data_size(off);
2872}
2873
07f508a2
DK
2874// Arm_output_section methods.
2875
2876// Create a stub group for input sections from BEGIN to END. OWNER
2877// points to the input section to be the owner a new stub table.
2878
2879template<bool big_endian>
2880void
2881Arm_output_section<big_endian>::create_stub_group(
2882 Input_section_list::const_iterator begin,
2883 Input_section_list::const_iterator end,
2884 Input_section_list::const_iterator owner,
2885 Target_arm<big_endian>* target,
2886 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
2887{
2888 // Currently we convert ordinary input sections into relaxed sections only
2889 // at this point but we may want to support creating relaxed input section
2890 // very early. So we check here to see if owner is already a relaxed
2891 // section.
2892
2893 Arm_input_section<big_endian>* arm_input_section;
2894 if (owner->is_relaxed_input_section())
2895 {
2896 arm_input_section =
2897 Arm_input_section<big_endian>::as_arm_input_section(
2898 owner->relaxed_input_section());
2899 }
2900 else
2901 {
2902 gold_assert(owner->is_input_section());
2903 // Create a new relaxed input section.
2904 arm_input_section =
2905 target->new_arm_input_section(owner->relobj(), owner->shndx());
2906 new_relaxed_sections->push_back(arm_input_section);
2907 }
2908
2909 // Create a stub table.
2910 Stub_table<big_endian>* stub_table =
2911 target->new_stub_table(arm_input_section);
2912
2913 arm_input_section->set_stub_table(stub_table);
2914
2915 Input_section_list::const_iterator p = begin;
2916 Input_section_list::const_iterator prev_p;
2917
2918 // Look for input sections or relaxed input sections in [begin ... end].
2919 do
2920 {
2921 if (p->is_input_section() || p->is_relaxed_input_section())
2922 {
2923 // The stub table information for input sections live
2924 // in their objects.
2925 Arm_relobj<big_endian>* arm_relobj =
2926 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
2927 arm_relobj->set_stub_table(p->shndx(), stub_table);
2928 }
2929 prev_p = p++;
2930 }
2931 while (prev_p != end);
2932}
2933
2934// Group input sections for stub generation. GROUP_SIZE is roughly the limit
2935// of stub groups. We grow a stub group by adding input section until the
2936// size is just below GROUP_SIZE. The last input section will be converted
2937// into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
2938// input section after the stub table, effectively double the group size.
2939//
2940// This is similar to the group_sections() function in elf32-arm.c but is
2941// implemented differently.
2942
2943template<bool big_endian>
2944void
2945Arm_output_section<big_endian>::group_sections(
2946 section_size_type group_size,
2947 bool stubs_always_after_branch,
2948 Target_arm<big_endian>* target)
2949{
2950 // We only care about sections containing code.
2951 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
2952 return;
2953
2954 // States for grouping.
2955 typedef enum
2956 {
2957 // No group is being built.
2958 NO_GROUP,
2959 // A group is being built but the stub table is not found yet.
2960 // We keep group a stub group until the size is just under GROUP_SIZE.
2961 // The last input section in the group will be used as the stub table.
2962 FINDING_STUB_SECTION,
2963 // A group is being built and we have already found a stub table.
2964 // We enter this state to grow a stub group by adding input section
2965 // after the stub table. This effectively doubles the group size.
2966 HAS_STUB_SECTION
2967 } State;
2968
2969 // Any newly created relaxed sections are stored here.
2970 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
2971
2972 State state = NO_GROUP;
2973 section_size_type off = 0;
2974 section_size_type group_begin_offset = 0;
2975 section_size_type group_end_offset = 0;
2976 section_size_type stub_table_end_offset = 0;
2977 Input_section_list::const_iterator group_begin =
2978 this->input_sections().end();
2979 Input_section_list::const_iterator stub_table =
2980 this->input_sections().end();
2981 Input_section_list::const_iterator group_end = this->input_sections().end();
2982 for (Input_section_list::const_iterator p = this->input_sections().begin();
2983 p != this->input_sections().end();
2984 ++p)
2985 {
2986 section_size_type section_begin_offset =
2987 align_address(off, p->addralign());
2988 section_size_type section_end_offset =
2989 section_begin_offset + p->data_size();
2990
2991 // Check to see if we should group the previously seens sections.
2992 switch(state)
2993 {
2994 case NO_GROUP:
2995 break;
2996
2997 case FINDING_STUB_SECTION:
2998 // Adding this section makes the group larger than GROUP_SIZE.
2999 if (section_end_offset - group_begin_offset >= group_size)
3000 {
3001 if (stubs_always_after_branch)
3002 {
3003 gold_assert(group_end != this->input_sections().end());
3004 this->create_stub_group(group_begin, group_end, group_end,
3005 target, &new_relaxed_sections);
3006 state = NO_GROUP;
3007 }
3008 else
3009 {
3010 // But wait, there's more! Input sections up to
3011 // stub_group_size bytes after the stub table can be
3012 // handled by it too.
3013 state = HAS_STUB_SECTION;
3014 stub_table = group_end;
3015 stub_table_end_offset = group_end_offset;
3016 }
3017 }
3018 break;
3019
3020 case HAS_STUB_SECTION:
3021 // Adding this section makes the post stub-section group larger
3022 // than GROUP_SIZE.
3023 if (section_end_offset - stub_table_end_offset >= group_size)
3024 {
3025 gold_assert(group_end != this->input_sections().end());
3026 this->create_stub_group(group_begin, group_end, stub_table,
3027 target, &new_relaxed_sections);
3028 state = NO_GROUP;
3029 }
3030 break;
3031
3032 default:
3033 gold_unreachable();
3034 }
3035
3036 // If we see an input section and currently there is no group, start
3037 // a new one. Skip any empty sections.
3038 if ((p->is_input_section() || p->is_relaxed_input_section())
3039 && (p->relobj()->section_size(p->shndx()) != 0))
3040 {
3041 if (state == NO_GROUP)
3042 {
3043 state = FINDING_STUB_SECTION;
3044 group_begin = p;
3045 group_begin_offset = section_begin_offset;
3046 }
3047
3048 // Keep track of the last input section seen.
3049 group_end = p;
3050 group_end_offset = section_end_offset;
3051 }
3052
3053 off = section_end_offset;
3054 }
3055
3056 // Create a stub group for any ungrouped sections.
3057 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
3058 {
3059 gold_assert(group_end != this->input_sections().end());
3060 this->create_stub_group(group_begin, group_end,
3061 (state == FINDING_STUB_SECTION
3062 ? group_end
3063 : stub_table),
3064 target, &new_relaxed_sections);
3065 }
3066
3067 // Convert input section into relaxed input section in a batch.
3068 if (!new_relaxed_sections.empty())
3069 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
3070
3071 // Update the section offsets
3072 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
3073 {
3074 Arm_relobj<big_endian>* arm_relobj =
3075 Arm_relobj<big_endian>::as_arm_relobj(
3076 new_relaxed_sections[i]->relobj());
3077 unsigned int shndx = new_relaxed_sections[i]->shndx();
3078 // Tell Arm_relobj that this input section is converted.
3079 arm_relobj->convert_input_section_to_relaxed_section(shndx);
3080 }
3081}
3082
8ffa3667
DK
3083// Arm_relobj methods.
3084
3085// Scan relocations for stub generation.
3086
3087template<bool big_endian>
3088void
3089Arm_relobj<big_endian>::scan_sections_for_stubs(
3090 Target_arm<big_endian>* arm_target,
3091 const Symbol_table* symtab,
3092 const Layout* layout)
3093{
3094 unsigned int shnum = this->shnum();
3095 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
3096
3097 // Read the section headers.
3098 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
3099 shnum * shdr_size,
3100 true, true);
3101
3102 // To speed up processing, we set up hash tables for fast lookup of
3103 // input offsets to output addresses.
3104 this->initialize_input_to_output_maps();
3105
3106 const Relobj::Output_sections& out_sections(this->output_sections());
3107
3108 Relocate_info<32, big_endian> relinfo;
8ffa3667
DK
3109 relinfo.symtab = symtab;
3110 relinfo.layout = layout;
3111 relinfo.object = this;
3112
3113 const unsigned char* p = pshdrs + shdr_size;
3114 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
3115 {
3116 typename elfcpp::Shdr<32, big_endian> shdr(p);
3117
3118 unsigned int sh_type = shdr.get_sh_type();
3119 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
3120 continue;
3121
3122 off_t sh_size = shdr.get_sh_size();
3123 if (sh_size == 0)
3124 continue;
3125
3126 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
3127 if (index >= this->shnum())
3128 {
3129 // Ignore reloc section with bad info. This error will be
3130 // reported in the final link.
3131 continue;
3132 }
3133
3134 Output_section* os = out_sections[index];
3135 if (os == NULL)
3136 {
3137 // This relocation section is against a section which we
3138 // discarded.
3139 continue;
3140 }
3141 Arm_address output_offset = this->get_output_section_offset(index);
3142
3143 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
3144 {
3145 // Ignore reloc section with unexpected symbol table. The
3146 // error will be reported in the final link.
3147 continue;
3148 }
3149
3150 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
3151 sh_size, true, false);
3152
3153 unsigned int reloc_size;
3154 if (sh_type == elfcpp::SHT_REL)
3155 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
3156 else
3157 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
3158
3159 if (reloc_size != shdr.get_sh_entsize())
3160 {
3161 // Ignore reloc section with unexpected entsize. The error
3162 // will be reported in the final link.
3163 continue;
3164 }
3165
3166 size_t reloc_count = sh_size / reloc_size;
3167 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
3168 {
3169 // Ignore reloc section with uneven size. The error will be
3170 // reported in the final link.
3171 continue;
3172 }
3173
3174 gold_assert(output_offset != invalid_address
3175 || this->relocs_must_follow_section_writes());
3176
3177 // Get the section contents. This does work for the case in which
3178 // we modify the contents of an input section. We need to pass the
3179 // output view under such circumstances.
3180 section_size_type input_view_size = 0;
3181 const unsigned char* input_view =
3182 this->section_contents(index, &input_view_size, false);
3183
3184 relinfo.reloc_shndx = i;
3185 relinfo.data_shndx = index;
3186 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
3187 reloc_count, os,
3188 output_offset == invalid_address,
3189 input_view,
3190 os->address(),
3191 input_view_size);
3192 }
3193
3194 // After we've done the relocations, we release the hash tables,
3195 // since we no longer need them.
3196 this->free_input_to_output_maps();
3197}
3198
3199// Count the local symbols. The ARM backend needs to know if a symbol
3200// is a THUMB function or not. For global symbols, it is easy because
3201// the Symbol object keeps the ELF symbol type. For local symbol it is
3202// harder because we cannot access this information. So we override the
3203// do_count_local_symbol in parent and scan local symbols to mark
3204// THUMB functions. This is not the most efficient way but I do not want to
3205// slow down other ports by calling a per symbol targer hook inside
3206// Sized_relobj<size, big_endian>::do_count_local_symbols.
3207
3208template<bool big_endian>
3209void
3210Arm_relobj<big_endian>::do_count_local_symbols(
3211 Stringpool_template<char>* pool,
3212 Stringpool_template<char>* dynpool)
3213{
3214 // We need to fix-up the values of any local symbols whose type are
3215 // STT_ARM_TFUNC.
3216
3217 // Ask parent to count the local symbols.
3218 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
3219 const unsigned int loccount = this->local_symbol_count();
3220 if (loccount == 0)
3221 return;
3222
3223 // Intialize the thumb function bit-vector.
3224 std::vector<bool> empty_vector(loccount, false);
3225 this->local_symbol_is_thumb_function_.swap(empty_vector);
3226
3227 // Read the symbol table section header.
3228 const unsigned int symtab_shndx = this->symtab_shndx();
3229 elfcpp::Shdr<32, big_endian>
3230 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
3231 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
3232
3233 // Read the local symbols.
3234 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
3235 gold_assert(loccount == symtabshdr.get_sh_info());
3236 off_t locsize = loccount * sym_size;
3237 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
3238 locsize, true, true);
3239
3240 // Loop over the local symbols and mark any local symbols pointing
3241 // to THUMB functions.
3242
3243 // Skip the first dummy symbol.
3244 psyms += sym_size;
3245 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
3246 this->local_values();
3247 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
3248 {
3249 elfcpp::Sym<32, big_endian> sym(psyms);
3250 elfcpp::STT st_type = sym.get_st_type();
3251 Symbol_value<32>& lv((*plocal_values)[i]);
3252 Arm_address input_value = lv.input_value();
3253
3254 if (st_type == elfcpp::STT_ARM_TFUNC
3255 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
3256 {
3257 // This is a THUMB function. Mark this and canonicalize the
3258 // symbol value by setting LSB.
3259 this->local_symbol_is_thumb_function_[i] = true;
3260 if ((input_value & 1) == 0)
3261 lv.set_input_value(input_value | 1);
3262 }
3263 }
3264}
3265
3266// Relocate sections.
3267template<bool big_endian>
3268void
3269Arm_relobj<big_endian>::do_relocate_sections(
3270 const General_options& options,
3271 const Symbol_table* symtab,
3272 const Layout* layout,
3273 const unsigned char* pshdrs,
3274 typename Sized_relobj<32, big_endian>::Views* pviews)
3275{
3276 // Call parent to relocate sections.
3277 Sized_relobj<32, big_endian>::do_relocate_sections(options, symtab, layout,
3278 pshdrs, pviews);
3279
3280 // We do not generate stubs if doing a relocatable link.
3281 if (parameters->options().relocatable())
3282 return;
3283
3284 // Relocate stub tables.
3285 unsigned int shnum = this->shnum();
3286
3287 Target_arm<big_endian>* arm_target =
3288 Target_arm<big_endian>::default_target();
3289
3290 Relocate_info<32, big_endian> relinfo;
3291 relinfo.options = &options;
3292 relinfo.symtab = symtab;
3293 relinfo.layout = layout;
3294 relinfo.object = this;
3295
3296 for (unsigned int i = 1; i < shnum; ++i)
3297 {
3298 Arm_input_section<big_endian>* arm_input_section =
3299 arm_target->find_arm_input_section(this, i);
3300
3301 if (arm_input_section == NULL
3302 || !arm_input_section->is_stub_table_owner()
3303 || arm_input_section->stub_table()->empty())
3304 continue;
3305
3306 // We cannot discard a section if it owns a stub table.
3307 Output_section* os = this->output_section(i);
3308 gold_assert(os != NULL);
3309
3310 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
3311 relinfo.reloc_shdr = NULL;
3312 relinfo.data_shndx = i;
3313 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
3314
3315 gold_assert((*pviews)[i].view != NULL);
3316
3317 // We are passed the output section view. Adjust it to cover the
3318 // stub table only.
3319 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
3320 gold_assert((stub_table->address() >= (*pviews)[i].address)
3321 && ((stub_table->address() + stub_table->data_size())
3322 <= (*pviews)[i].address + (*pviews)[i].view_size));
3323
3324 off_t offset = stub_table->address() - (*pviews)[i].address;
3325 unsigned char* view = (*pviews)[i].view + offset;
3326 Arm_address address = stub_table->address();
3327 section_size_type view_size = stub_table->data_size();
3328
3329 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
3330 view_size);
3331 }
3332}
3333
d5b40221
DK
3334// Read the symbol information.
3335
3336template<bool big_endian>
3337void
3338Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
3339{
3340 // Call parent class to read symbol information.
3341 Sized_relobj<32, big_endian>::do_read_symbols(sd);
3342
3343 // Read processor-specific flags in ELF file header.
3344 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
3345 elfcpp::Elf_sizes<32>::ehdr_size,
3346 true, false);
3347 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
3348 this->processor_specific_flags_ = ehdr.get_e_flags();
3349}
3350
3351// Arm_dynobj methods.
3352
3353// Read the symbol information.
3354
3355template<bool big_endian>
3356void
3357Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
3358{
3359 // Call parent class to read symbol information.
3360 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
3361
3362 // Read processor-specific flags in ELF file header.
3363 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
3364 elfcpp::Elf_sizes<32>::ehdr_size,
3365 true, false);
3366 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
3367 this->processor_specific_flags_ = ehdr.get_e_flags();
3368}
3369
94cdfcff
DK
3370// A class to handle the PLT data.
3371
3372template<bool big_endian>
3373class Output_data_plt_arm : public Output_section_data
3374{
3375 public:
3376 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
3377 Reloc_section;
3378
3379 Output_data_plt_arm(Layout*, Output_data_space*);
3380
3381 // Add an entry to the PLT.
3382 void
3383 add_entry(Symbol* gsym);
3384
3385 // Return the .rel.plt section data.
3386 const Reloc_section*
3387 rel_plt() const
3388 { return this->rel_; }
3389
3390 protected:
3391 void
3392 do_adjust_output_section(Output_section* os);
3393
3394 // Write to a map file.
3395 void
3396 do_print_to_mapfile(Mapfile* mapfile) const
3397 { mapfile->print_output_data(this, _("** PLT")); }
3398
3399 private:
3400 // Template for the first PLT entry.
3401 static const uint32_t first_plt_entry[5];
3402
3403 // Template for subsequent PLT entries.
3404 static const uint32_t plt_entry[3];
3405
3406 // Set the final size.
3407 void
3408 set_final_data_size()
3409 {
3410 this->set_data_size(sizeof(first_plt_entry)
3411 + this->count_ * sizeof(plt_entry));
3412 }
3413
3414 // Write out the PLT data.
3415 void
3416 do_write(Output_file*);
3417
3418 // The reloc section.
3419 Reloc_section* rel_;
3420 // The .got.plt section.
3421 Output_data_space* got_plt_;
3422 // The number of PLT entries.
3423 unsigned int count_;
3424};
3425
3426// Create the PLT section. The ordinary .got section is an argument,
3427// since we need to refer to the start. We also create our own .got
3428// section just for PLT entries.
3429
3430template<bool big_endian>
3431Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
3432 Output_data_space* got_plt)
3433 : Output_section_data(4), got_plt_(got_plt), count_(0)
3434{
3435 this->rel_ = new Reloc_section(false);
3436 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
3437 elfcpp::SHF_ALLOC, this->rel_);
3438}
3439
3440template<bool big_endian>
3441void
3442Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
3443{
3444 os->set_entsize(0);
3445}
3446
3447// Add an entry to the PLT.
3448
3449template<bool big_endian>
3450void
3451Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
3452{
3453 gold_assert(!gsym->has_plt_offset());
3454
3455 // Note that when setting the PLT offset we skip the initial
3456 // reserved PLT entry.
3457 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
3458 + sizeof(first_plt_entry));
3459
3460 ++this->count_;
3461
3462 section_offset_type got_offset = this->got_plt_->current_data_size();
3463
3464 // Every PLT entry needs a GOT entry which points back to the PLT
3465 // entry (this will be changed by the dynamic linker, normally
3466 // lazily when the function is called).
3467 this->got_plt_->set_current_data_size(got_offset + 4);
3468
3469 // Every PLT entry needs a reloc.
3470 gsym->set_needs_dynsym_entry();
3471 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
3472 got_offset);
3473
3474 // Note that we don't need to save the symbol. The contents of the
3475 // PLT are independent of which symbols are used. The symbols only
3476 // appear in the relocations.
3477}
3478
3479// ARM PLTs.
3480// FIXME: This is not very flexible. Right now this has only been tested
3481// on armv5te. If we are to support additional architecture features like
3482// Thumb-2 or BE8, we need to make this more flexible like GNU ld.
3483
3484// The first entry in the PLT.
3485template<bool big_endian>
3486const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
3487{
3488 0xe52de004, // str lr, [sp, #-4]!
3489 0xe59fe004, // ldr lr, [pc, #4]
3490 0xe08fe00e, // add lr, pc, lr
3491 0xe5bef008, // ldr pc, [lr, #8]!
3492 0x00000000, // &GOT[0] - .
3493};
3494
3495// Subsequent entries in the PLT.
3496
3497template<bool big_endian>
3498const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
3499{
3500 0xe28fc600, // add ip, pc, #0xNN00000
3501 0xe28cca00, // add ip, ip, #0xNN000
3502 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
3503};
3504
3505// Write out the PLT. This uses the hand-coded instructions above,
3506// and adjusts them as needed. This is all specified by the arm ELF
3507// Processor Supplement.
3508
3509template<bool big_endian>
3510void
3511Output_data_plt_arm<big_endian>::do_write(Output_file* of)
3512{
3513 const off_t offset = this->offset();
3514 const section_size_type oview_size =
3515 convert_to_section_size_type(this->data_size());
3516 unsigned char* const oview = of->get_output_view(offset, oview_size);
3517
3518 const off_t got_file_offset = this->got_plt_->offset();
3519 const section_size_type got_size =
3520 convert_to_section_size_type(this->got_plt_->data_size());
3521 unsigned char* const got_view = of->get_output_view(got_file_offset,
3522 got_size);
3523 unsigned char* pov = oview;
3524
ebabffbd
DK
3525 Arm_address plt_address = this->address();
3526 Arm_address got_address = this->got_plt_->address();
94cdfcff
DK
3527
3528 // Write first PLT entry. All but the last word are constants.
3529 const size_t num_first_plt_words = (sizeof(first_plt_entry)
3530 / sizeof(plt_entry[0]));
3531 for (size_t i = 0; i < num_first_plt_words - 1; i++)
3532 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
3533 // Last word in first PLT entry is &GOT[0] - .
3534 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
3535 got_address - (plt_address + 16));
3536 pov += sizeof(first_plt_entry);
3537
3538 unsigned char* got_pov = got_view;
3539
3540 memset(got_pov, 0, 12);
3541 got_pov += 12;
3542
3543 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
3544 unsigned int plt_offset = sizeof(first_plt_entry);
3545 unsigned int plt_rel_offset = 0;
3546 unsigned int got_offset = 12;
3547 const unsigned int count = this->count_;
3548 for (unsigned int i = 0;
3549 i < count;
3550 ++i,
3551 pov += sizeof(plt_entry),
3552 got_pov += 4,
3553 plt_offset += sizeof(plt_entry),
3554 plt_rel_offset += rel_size,
3555 got_offset += 4)
3556 {
3557 // Set and adjust the PLT entry itself.
3558 int32_t offset = ((got_address + got_offset)
3559 - (plt_address + plt_offset + 8));
3560
3561 gold_assert(offset >= 0 && offset < 0x0fffffff);
3562 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
3563 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
3564 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
3565 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
3566 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
3567 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
3568
3569 // Set the entry in the GOT.
3570 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
3571 }
3572
3573 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
3574 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
3575
3576 of->write_output_view(offset, oview_size, oview);
3577 of->write_output_view(got_file_offset, got_size, got_view);
3578}
3579
3580// Create a PLT entry for a global symbol.
3581
3582template<bool big_endian>
3583void
3584Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
3585 Symbol* gsym)
3586{
3587 if (gsym->has_plt_offset())
3588 return;
3589
3590 if (this->plt_ == NULL)
3591 {
3592 // Create the GOT sections first.
3593 this->got_section(symtab, layout);
3594
3595 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
3596 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
3597 (elfcpp::SHF_ALLOC
3598 | elfcpp::SHF_EXECINSTR),
3599 this->plt_);
3600 }
3601 this->plt_->add_entry(gsym);
3602}
3603
4a657b0d
DK
3604// Report an unsupported relocation against a local symbol.
3605
3606template<bool big_endian>
3607void
3608Target_arm<big_endian>::Scan::unsupported_reloc_local(
3609 Sized_relobj<32, big_endian>* object,
3610 unsigned int r_type)
3611{
3612 gold_error(_("%s: unsupported reloc %u against local symbol"),
3613 object->name().c_str(), r_type);
3614}
3615
bec53400
DK
3616// We are about to emit a dynamic relocation of type R_TYPE. If the
3617// dynamic linker does not support it, issue an error. The GNU linker
3618// only issues a non-PIC error for an allocated read-only section.
3619// Here we know the section is allocated, but we don't know that it is
3620// read-only. But we check for all the relocation types which the
3621// glibc dynamic linker supports, so it seems appropriate to issue an
3622// error even if the section is not read-only.
3623
3624template<bool big_endian>
3625void
3626Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
3627 unsigned int r_type)
3628{
3629 switch (r_type)
3630 {
3631 // These are the relocation types supported by glibc for ARM.
3632 case elfcpp::R_ARM_RELATIVE:
3633 case elfcpp::R_ARM_COPY:
3634 case elfcpp::R_ARM_GLOB_DAT:
3635 case elfcpp::R_ARM_JUMP_SLOT:
3636 case elfcpp::R_ARM_ABS32:
be8fcb75 3637 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
3638 case elfcpp::R_ARM_PC24:
3639 // FIXME: The following 3 types are not supported by Android's dynamic
3640 // linker.
3641 case elfcpp::R_ARM_TLS_DTPMOD32:
3642 case elfcpp::R_ARM_TLS_DTPOFF32:
3643 case elfcpp::R_ARM_TLS_TPOFF32:
3644 return;
3645
3646 default:
3647 // This prevents us from issuing more than one error per reloc
3648 // section. But we can still wind up issuing more than one
3649 // error per object file.
3650 if (this->issued_non_pic_error_)
3651 return;
3652 object->error(_("requires unsupported dynamic reloc; "
3653 "recompile with -fPIC"));
3654 this->issued_non_pic_error_ = true;
3655 return;
3656
3657 case elfcpp::R_ARM_NONE:
3658 gold_unreachable();
3659 }
3660}
3661
4a657b0d 3662// Scan a relocation for a local symbol.
bec53400
DK
3663// FIXME: This only handles a subset of relocation types used by Android
3664// on ARM v5te devices.
4a657b0d
DK
3665
3666template<bool big_endian>
3667inline void
ad0f2072 3668Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
bec53400
DK
3669 Layout* layout,
3670 Target_arm* target,
4a657b0d 3671 Sized_relobj<32, big_endian>* object,
bec53400
DK
3672 unsigned int data_shndx,
3673 Output_section* output_section,
3674 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
3675 unsigned int r_type,
3676 const elfcpp::Sym<32, big_endian>&)
3677{
3678 r_type = get_real_reloc_type(r_type);
3679 switch (r_type)
3680 {
3681 case elfcpp::R_ARM_NONE:
3682 break;
3683
bec53400 3684 case elfcpp::R_ARM_ABS32:
be8fcb75 3685 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
3686 // If building a shared library (or a position-independent
3687 // executable), we need to create a dynamic relocation for
3688 // this location. The relocation applied at link time will
3689 // apply the link-time value, so we flag the location with
3690 // an R_ARM_RELATIVE relocation so the dynamic loader can
3691 // relocate it easily.
3692 if (parameters->options().output_is_position_independent())
3693 {
3694 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3695 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3696 // If we are to add more other reloc types than R_ARM_ABS32,
3697 // we need to add check_non_pic(object, r_type) here.
3698 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
3699 output_section, data_shndx,
3700 reloc.get_r_offset());
3701 }
3702 break;
3703
3704 case elfcpp::R_ARM_REL32:
3705 case elfcpp::R_ARM_THM_CALL:
3706 case elfcpp::R_ARM_CALL:
3707 case elfcpp::R_ARM_PREL31:
3708 case elfcpp::R_ARM_JUMP24:
3709 case elfcpp::R_ARM_PLT32:
be8fcb75
ILT
3710 case elfcpp::R_ARM_THM_ABS5:
3711 case elfcpp::R_ARM_ABS8:
3712 case elfcpp::R_ARM_ABS12:
3713 case elfcpp::R_ARM_ABS16:
3714 case elfcpp::R_ARM_BASE_ABS:
fd3c5f0b
ILT
3715 case elfcpp::R_ARM_MOVW_ABS_NC:
3716 case elfcpp::R_ARM_MOVT_ABS:
3717 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3718 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
3719 case elfcpp::R_ARM_MOVW_PREL_NC:
3720 case elfcpp::R_ARM_MOVT_PREL:
3721 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3722 case elfcpp::R_ARM_THM_MOVT_PREL:
bec53400
DK
3723 break;
3724
3725 case elfcpp::R_ARM_GOTOFF32:
3726 // We need a GOT section:
3727 target->got_section(symtab, layout);
3728 break;
3729
3730 case elfcpp::R_ARM_BASE_PREL:
3731 // FIXME: What about this?
3732 break;
3733
3734 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 3735 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
3736 {
3737 // The symbol requires a GOT entry.
3738 Output_data_got<32, big_endian>* got =
3739 target->got_section(symtab, layout);
3740 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3741 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
3742 {
3743 // If we are generating a shared object, we need to add a
3744 // dynamic RELATIVE relocation for this symbol's GOT entry.
3745 if (parameters->options().output_is_position_independent())
3746 {
3747 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3748 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3749 rel_dyn->add_local_relative(
3750 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
3751 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
3752 }
3753 }
3754 }
3755 break;
3756
3757 case elfcpp::R_ARM_TARGET1:
3758 // This should have been mapped to another type already.
3759 // Fall through.
3760 case elfcpp::R_ARM_COPY:
3761 case elfcpp::R_ARM_GLOB_DAT:
3762 case elfcpp::R_ARM_JUMP_SLOT:
3763 case elfcpp::R_ARM_RELATIVE:
3764 // These are relocations which should only be seen by the
3765 // dynamic linker, and should never be seen here.
3766 gold_error(_("%s: unexpected reloc %u in object file"),
3767 object->name().c_str(), r_type);
3768 break;
3769
4a657b0d
DK
3770 default:
3771 unsupported_reloc_local(object, r_type);
3772 break;
3773 }
3774}
3775
3776// Report an unsupported relocation against a global symbol.
3777
3778template<bool big_endian>
3779void
3780Target_arm<big_endian>::Scan::unsupported_reloc_global(
3781 Sized_relobj<32, big_endian>* object,
3782 unsigned int r_type,
3783 Symbol* gsym)
3784{
3785 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3786 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3787}
3788
3789// Scan a relocation for a global symbol.
bec53400
DK
3790// FIXME: This only handles a subset of relocation types used by Android
3791// on ARM v5te devices.
4a657b0d
DK
3792
3793template<bool big_endian>
3794inline void
ad0f2072 3795Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
bec53400
DK
3796 Layout* layout,
3797 Target_arm* target,
4a657b0d 3798 Sized_relobj<32, big_endian>* object,
bec53400
DK
3799 unsigned int data_shndx,
3800 Output_section* output_section,
3801 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
3802 unsigned int r_type,
3803 Symbol* gsym)
3804{
3805 r_type = get_real_reloc_type(r_type);
3806 switch (r_type)
3807 {
3808 case elfcpp::R_ARM_NONE:
3809 break;
3810
bec53400 3811 case elfcpp::R_ARM_ABS32:
be8fcb75 3812 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
3813 {
3814 // Make a dynamic relocation if necessary.
3815 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
3816 {
3817 if (target->may_need_copy_reloc(gsym))
3818 {
3819 target->copy_reloc(symtab, layout, object,
3820 data_shndx, output_section, gsym, reloc);
3821 }
3822 else if (gsym->can_use_relative_reloc(false))
3823 {
3824 // If we are to add more other reloc types than R_ARM_ABS32,
3825 // we need to add check_non_pic(object, r_type) here.
3826 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3827 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
3828 output_section, object,
3829 data_shndx, reloc.get_r_offset());
3830 }
3831 else
3832 {
3833 // If we are to add more other reloc types than R_ARM_ABS32,
3834 // we need to add check_non_pic(object, r_type) here.
3835 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3836 rel_dyn->add_global(gsym, r_type, output_section, object,
3837 data_shndx, reloc.get_r_offset());
3838 }
3839 }
3840 }
3841 break;
3842
fd3c5f0b
ILT
3843 case elfcpp::R_ARM_MOVW_ABS_NC:
3844 case elfcpp::R_ARM_MOVT_ABS:
3845 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3846 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
3847 case elfcpp::R_ARM_MOVW_PREL_NC:
3848 case elfcpp::R_ARM_MOVT_PREL:
3849 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3850 case elfcpp::R_ARM_THM_MOVT_PREL:
fd3c5f0b
ILT
3851 break;
3852
be8fcb75
ILT
3853 case elfcpp::R_ARM_THM_ABS5:
3854 case elfcpp::R_ARM_ABS8:
3855 case elfcpp::R_ARM_ABS12:
3856 case elfcpp::R_ARM_ABS16:
3857 case elfcpp::R_ARM_BASE_ABS:
3858 {
3859 // No dynamic relocs of this kinds.
3860 // Report the error in case of PIC.
3861 int flags = Symbol::NON_PIC_REF;
3862 if (gsym->type() == elfcpp::STT_FUNC
3863 || gsym->type() == elfcpp::STT_ARM_TFUNC)
3864 flags |= Symbol::FUNCTION_CALL;
3865 if (gsym->needs_dynamic_reloc(flags))
3866 check_non_pic(object, r_type);
3867 }
3868 break;
3869
bec53400
DK
3870 case elfcpp::R_ARM_REL32:
3871 case elfcpp::R_ARM_PREL31:
3872 {
3873 // Make a dynamic relocation if necessary.
3874 int flags = Symbol::NON_PIC_REF;
3875 if (gsym->needs_dynamic_reloc(flags))
3876 {
3877 if (target->may_need_copy_reloc(gsym))
3878 {
3879 target->copy_reloc(symtab, layout, object,
3880 data_shndx, output_section, gsym, reloc);
3881 }
3882 else
3883 {
3884 check_non_pic(object, r_type);
3885 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3886 rel_dyn->add_global(gsym, r_type, output_section, object,
3887 data_shndx, reloc.get_r_offset());
3888 }
3889 }
3890 }
3891 break;
3892
3893 case elfcpp::R_ARM_JUMP24:
3894 case elfcpp::R_ARM_THM_CALL:
3895 case elfcpp::R_ARM_CALL:
3896 {
3897 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
3898 target->make_plt_entry(symtab, layout, gsym);
3899 // Make a dynamic relocation if necessary.
3900 int flags = Symbol::NON_PIC_REF;
3901 if (gsym->type() == elfcpp::STT_FUNC
07800fab 3902 || gsym->type() == elfcpp::STT_ARM_TFUNC)
bec53400
DK
3903 flags |= Symbol::FUNCTION_CALL;
3904 if (gsym->needs_dynamic_reloc(flags))
3905 {
3906 if (target->may_need_copy_reloc(gsym))
3907 {
3908 target->copy_reloc(symtab, layout, object,
3909 data_shndx, output_section, gsym,
3910 reloc);
3911 }
3912 else
3913 {
3914 check_non_pic(object, r_type);
3915 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3916 rel_dyn->add_global(gsym, r_type, output_section, object,
3917 data_shndx, reloc.get_r_offset());
3918 }
3919 }
3920 }
3921 break;
3922
3923 case elfcpp::R_ARM_PLT32:
3924 // If the symbol is fully resolved, this is just a relative
3925 // local reloc. Otherwise we need a PLT entry.
3926 if (gsym->final_value_is_known())
3927 break;
3928 // If building a shared library, we can also skip the PLT entry
3929 // if the symbol is defined in the output file and is protected
3930 // or hidden.
3931 if (gsym->is_defined()
3932 && !gsym->is_from_dynobj()
3933 && !gsym->is_preemptible())
3934 break;
3935 target->make_plt_entry(symtab, layout, gsym);
3936 break;
3937
3938 case elfcpp::R_ARM_GOTOFF32:
3939 // We need a GOT section.
3940 target->got_section(symtab, layout);
3941 break;
3942
3943 case elfcpp::R_ARM_BASE_PREL:
3944 // FIXME: What about this?
3945 break;
3946
3947 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 3948 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
3949 {
3950 // The symbol requires a GOT entry.
3951 Output_data_got<32, big_endian>* got =
3952 target->got_section(symtab, layout);
3953 if (gsym->final_value_is_known())
3954 got->add_global(gsym, GOT_TYPE_STANDARD);
3955 else
3956 {
3957 // If this symbol is not fully resolved, we need to add a
3958 // GOT entry with a dynamic relocation.
3959 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3960 if (gsym->is_from_dynobj()
3961 || gsym->is_undefined()
3962 || gsym->is_preemptible())
3963 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
3964 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
3965 else
3966 {
3967 if (got->add_global(gsym, GOT_TYPE_STANDARD))
3968 rel_dyn->add_global_relative(
3969 gsym, elfcpp::R_ARM_RELATIVE, got,
3970 gsym->got_offset(GOT_TYPE_STANDARD));
3971 }
3972 }
3973 }
3974 break;
3975
3976 case elfcpp::R_ARM_TARGET1:
3977 // This should have been mapped to another type already.
3978 // Fall through.
3979 case elfcpp::R_ARM_COPY:
3980 case elfcpp::R_ARM_GLOB_DAT:
3981 case elfcpp::R_ARM_JUMP_SLOT:
3982 case elfcpp::R_ARM_RELATIVE:
3983 // These are relocations which should only be seen by the
3984 // dynamic linker, and should never be seen here.
3985 gold_error(_("%s: unexpected reloc %u in object file"),
3986 object->name().c_str(), r_type);
3987 break;
3988
4a657b0d
DK
3989 default:
3990 unsupported_reloc_global(object, r_type, gsym);
3991 break;
3992 }
3993}
3994
3995// Process relocations for gc.
3996
3997template<bool big_endian>
3998void
ad0f2072 3999Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
4a657b0d
DK
4000 Layout* layout,
4001 Sized_relobj<32, big_endian>* object,
4002 unsigned int data_shndx,
4003 unsigned int,
4004 const unsigned char* prelocs,
4005 size_t reloc_count,
4006 Output_section* output_section,
4007 bool needs_special_offset_handling,
4008 size_t local_symbol_count,
4009 const unsigned char* plocal_symbols)
4010{
4011 typedef Target_arm<big_endian> Arm;
4012 typedef typename Target_arm<big_endian>::Scan Scan;
4013
4014 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
4a657b0d
DK
4015 symtab,
4016 layout,
4017 this,
4018 object,
4019 data_shndx,
4020 prelocs,
4021 reloc_count,
4022 output_section,
4023 needs_special_offset_handling,
4024 local_symbol_count,
4025 plocal_symbols);
4026}
4027
4028// Scan relocations for a section.
4029
4030template<bool big_endian>
4031void
ad0f2072 4032Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
4a657b0d
DK
4033 Layout* layout,
4034 Sized_relobj<32, big_endian>* object,
4035 unsigned int data_shndx,
4036 unsigned int sh_type,
4037 const unsigned char* prelocs,
4038 size_t reloc_count,
4039 Output_section* output_section,
4040 bool needs_special_offset_handling,
4041 size_t local_symbol_count,
4042 const unsigned char* plocal_symbols)
4043{
4044 typedef typename Target_arm<big_endian>::Scan Scan;
4045 if (sh_type == elfcpp::SHT_RELA)
4046 {
4047 gold_error(_("%s: unsupported RELA reloc section"),
4048 object->name().c_str());
4049 return;
4050 }
4051
4052 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
4a657b0d
DK
4053 symtab,
4054 layout,
4055 this,
4056 object,
4057 data_shndx,
4058 prelocs,
4059 reloc_count,
4060 output_section,
4061 needs_special_offset_handling,
4062 local_symbol_count,
4063 plocal_symbols);
4064}
4065
4066// Finalize the sections.
4067
4068template<bool big_endian>
4069void
d5b40221
DK
4070Target_arm<big_endian>::do_finalize_sections(
4071 Layout* layout,
4072 const Input_objects* input_objects)
4a657b0d 4073{
d5b40221
DK
4074 // Merge processor-specific flags.
4075 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4076 p != input_objects->relobj_end();
4077 ++p)
4078 {
4079 Arm_relobj<big_endian>* arm_relobj =
4080 Arm_relobj<big_endian>::as_arm_relobj(*p);
4081 this->merge_processor_specific_flags(
4082 arm_relobj->name(),
4083 arm_relobj->processor_specific_flags());
4084 }
4085
4086 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
4087 p != input_objects->dynobj_end();
4088 ++p)
4089 {
4090 Arm_dynobj<big_endian>* arm_dynobj =
4091 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
4092 this->merge_processor_specific_flags(
4093 arm_dynobj->name(),
4094 arm_dynobj->processor_specific_flags());
4095 }
4096
94cdfcff
DK
4097 // Fill in some more dynamic tags.
4098 Output_data_dynamic* const odyn = layout->dynamic_data();
4099 if (odyn != NULL)
4100 {
4101 if (this->got_plt_ != NULL)
4102 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
4103
4104 if (this->plt_ != NULL)
4105 {
4106 const Output_data* od = this->plt_->rel_plt();
4107 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
4108 odyn->add_section_address(elfcpp::DT_JMPREL, od);
4109 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
4110 }
4111
4112 if (this->rel_dyn_ != NULL)
4113 {
4114 const Output_data* od = this->rel_dyn_;
4115 odyn->add_section_address(elfcpp::DT_REL, od);
4116 odyn->add_section_size(elfcpp::DT_RELSZ, od);
4117 odyn->add_constant(elfcpp::DT_RELENT,
4118 elfcpp::Elf_sizes<32>::rel_size);
4119 }
4120
4121 if (!parameters->options().shared())
4122 {
4123 // The value of the DT_DEBUG tag is filled in by the dynamic
4124 // linker at run time, and used by the debugger.
4125 odyn->add_constant(elfcpp::DT_DEBUG, 0);
4126 }
4127 }
4128
4129 // Emit any relocs we saved in an attempt to avoid generating COPY
4130 // relocs.
4131 if (this->copy_relocs_.any_saved_relocs())
4132 this->copy_relocs_.emit(this->rel_dyn_section(layout));
11af873f
DK
4133
4134 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4135 // the .ARM.exidx section.
4136 if (!layout->script_options()->saw_phdrs_clause()
4137 && !parameters->options().relocatable())
4138 {
4139 Output_section* exidx_section =
4140 layout->find_output_section(".ARM.exidx");
4141
4142 if (exidx_section != NULL
4143 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
4144 {
4145 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
4146 == NULL);
4147 Output_segment* exidx_segment =
4148 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
4149 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
4150 }
4151 }
4a657b0d
DK
4152}
4153
bec53400
DK
4154// Return whether a direct absolute static relocation needs to be applied.
4155// In cases where Scan::local() or Scan::global() has created
4156// a dynamic relocation other than R_ARM_RELATIVE, the addend
4157// of the relocation is carried in the data, and we must not
4158// apply the static relocation.
4159
4160template<bool big_endian>
4161inline bool
4162Target_arm<big_endian>::Relocate::should_apply_static_reloc(
4163 const Sized_symbol<32>* gsym,
4164 int ref_flags,
4165 bool is_32bit,
4166 Output_section* output_section)
4167{
4168 // If the output section is not allocated, then we didn't call
4169 // scan_relocs, we didn't create a dynamic reloc, and we must apply
4170 // the reloc here.
4171 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
4172 return true;
4173
4174 // For local symbols, we will have created a non-RELATIVE dynamic
4175 // relocation only if (a) the output is position independent,
4176 // (b) the relocation is absolute (not pc- or segment-relative), and
4177 // (c) the relocation is not 32 bits wide.
4178 if (gsym == NULL)
4179 return !(parameters->options().output_is_position_independent()
4180 && (ref_flags & Symbol::ABSOLUTE_REF)
4181 && !is_32bit);
4182
4183 // For global symbols, we use the same helper routines used in the
4184 // scan pass. If we did not create a dynamic relocation, or if we
4185 // created a RELATIVE dynamic relocation, we should apply the static
4186 // relocation.
4187 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
4188 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
4189 && gsym->can_use_relative_reloc(ref_flags
4190 & Symbol::FUNCTION_CALL);
4191 return !has_dyn || is_rel;
4192}
4193
4a657b0d
DK
4194// Perform a relocation.
4195
4196template<bool big_endian>
4197inline bool
4198Target_arm<big_endian>::Relocate::relocate(
c121c671
DK
4199 const Relocate_info<32, big_endian>* relinfo,
4200 Target_arm* target,
4201 Output_section *output_section,
4202 size_t relnum,
4203 const elfcpp::Rel<32, big_endian>& rel,
4a657b0d 4204 unsigned int r_type,
c121c671
DK
4205 const Sized_symbol<32>* gsym,
4206 const Symbol_value<32>* psymval,
4207 unsigned char* view,
ebabffbd 4208 Arm_address address,
4a657b0d
DK
4209 section_size_type /* view_size */ )
4210{
c121c671
DK
4211 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
4212
4213 r_type = get_real_reloc_type(r_type);
4214
4215 // If this the symbol may be a Thumb function, set thumb bit to 1.
4216 bool has_thumb_bit = ((gsym != NULL)
4217 && (gsym->type() == elfcpp::STT_FUNC
4218 || gsym->type() == elfcpp::STT_ARM_TFUNC));
4219
4220 // Pick the value to use for symbols defined in shared objects.
4221 Symbol_value<32> symval;
4222 if (gsym != NULL
4223 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
4224 {
4225 symval.set_output_value(target->plt_section()->address()
4226 + gsym->plt_offset());
4227 psymval = &symval;
4228 has_thumb_bit = 0;
4229 }
4230
4231 const Sized_relobj<32, big_endian>* object = relinfo->object;
4232
4233 // Get the GOT offset if needed.
4234 // The GOT pointer points to the end of the GOT section.
4235 // We need to subtract the size of the GOT section to get
4236 // the actual offset to use in the relocation.
4237 bool have_got_offset = false;
4238 unsigned int got_offset = 0;
4239 switch (r_type)
4240 {
4241 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 4242 case elfcpp::R_ARM_GOT_PREL:
c121c671
DK
4243 if (gsym != NULL)
4244 {
4245 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4246 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4247 - target->got_size());
4248 }
4249 else
4250 {
4251 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
4252 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4253 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4254 - target->got_size());
4255 }
4256 have_got_offset = true;
4257 break;
4258
4259 default:
4260 break;
4261 }
4262
4263 typename Arm_relocate_functions::Status reloc_status =
4264 Arm_relocate_functions::STATUS_OKAY;
4a657b0d
DK
4265 switch (r_type)
4266 {
4267 case elfcpp::R_ARM_NONE:
4268 break;
4269
5e445df6
ILT
4270 case elfcpp::R_ARM_ABS8:
4271 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4272 output_section))
be8fcb75
ILT
4273 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
4274 break;
4275
4276 case elfcpp::R_ARM_ABS12:
4277 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4278 output_section))
4279 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
4280 break;
4281
4282 case elfcpp::R_ARM_ABS16:
4283 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4284 output_section))
4285 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5e445df6
ILT
4286 break;
4287
c121c671
DK
4288 case elfcpp::R_ARM_ABS32:
4289 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4290 output_section))
4291 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
4292 has_thumb_bit);
4293 break;
4294
be8fcb75
ILT
4295 case elfcpp::R_ARM_ABS32_NOI:
4296 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4297 output_section))
4298 // No thumb bit for this relocation: (S + A)
4299 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
4300 false);
4301 break;
4302
fd3c5f0b
ILT
4303 case elfcpp::R_ARM_MOVW_ABS_NC:
4304 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4305 output_section))
4306 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
4307 psymval,
4308 has_thumb_bit);
4309 else
4310 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
4311 "a shared object; recompile with -fPIC"));
4312 break;
4313
4314 case elfcpp::R_ARM_MOVT_ABS:
4315 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4316 output_section))
4317 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
4318 else
4319 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
4320 "a shared object; recompile with -fPIC"));
4321 break;
4322
4323 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4324 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4325 output_section))
4326 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
4327 psymval,
4328 has_thumb_bit);
4329 else
4330 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
4331 "making a shared object; recompile with -fPIC"));
4332 break;
4333
4334 case elfcpp::R_ARM_THM_MOVT_ABS:
4335 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4336 output_section))
4337 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
4338 psymval);
4339 else
4340 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
4341 "making a shared object; recompile with -fPIC"));
4342 break;
4343
c2a122b6
ILT
4344 case elfcpp::R_ARM_MOVW_PREL_NC:
4345 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
4346 psymval, address,
4347 has_thumb_bit);
4348 break;
4349
4350 case elfcpp::R_ARM_MOVT_PREL:
4351 reloc_status = Arm_relocate_functions::movt_prel(view, object,
4352 psymval, address);
4353 break;
4354
4355 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4356 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
4357 psymval, address,
4358 has_thumb_bit);
4359 break;
4360
4361 case elfcpp::R_ARM_THM_MOVT_PREL:
4362 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
4363 psymval, address);
4364 break;
4365
c121c671
DK
4366 case elfcpp::R_ARM_REL32:
4367 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
4368 address, has_thumb_bit);
4369 break;
4370
be8fcb75
ILT
4371 case elfcpp::R_ARM_THM_ABS5:
4372 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4373 output_section))
4374 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
4375 break;
4376
c121c671
DK
4377 case elfcpp::R_ARM_THM_CALL:
4378 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
4379 address, has_thumb_bit);
4380 break;
4381
4382 case elfcpp::R_ARM_GOTOFF32:
4383 {
ebabffbd 4384 Arm_address got_origin;
c121c671
DK
4385 got_origin = target->got_plt_section()->address();
4386 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
4387 got_origin, has_thumb_bit);
4388 }
4389 break;
4390
4391 case elfcpp::R_ARM_BASE_PREL:
4392 {
4393 uint32_t origin;
4394 // Get the addressing origin of the output segment defining the
4395 // symbol gsym (AAELF 4.6.1.2 Relocation types)
4396 gold_assert(gsym != NULL);
4397 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
4398 origin = gsym->output_segment()->vaddr();
4399 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
4400 origin = gsym->output_data()->address();
4401 else
4402 {
4403 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4404 _("cannot find origin of R_ARM_BASE_PREL"));
4405 return true;
4406 }
4407 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
4408 }
4409 break;
4410
be8fcb75
ILT
4411 case elfcpp::R_ARM_BASE_ABS:
4412 {
4413 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4414 output_section))
4415 break;
4416
4417 uint32_t origin;
4418 // Get the addressing origin of the output segment defining
4419 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
4420 if (gsym == NULL)
4421 // R_ARM_BASE_ABS with the NULL symbol will give the
4422 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
4423 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
4424 origin = target->got_plt_section()->address();
4425 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
4426 origin = gsym->output_segment()->vaddr();
4427 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
4428 origin = gsym->output_data()->address();
4429 else
4430 {
4431 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4432 _("cannot find origin of R_ARM_BASE_ABS"));
4433 return true;
4434 }
4435
4436 reloc_status = Arm_relocate_functions::base_abs(view, origin);
4437 }
4438 break;
4439
c121c671
DK
4440 case elfcpp::R_ARM_GOT_BREL:
4441 gold_assert(have_got_offset);
4442 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
4443 break;
4444
7f5309a5
ILT
4445 case elfcpp::R_ARM_GOT_PREL:
4446 gold_assert(have_got_offset);
4447 // Get the address origin for GOT PLT, which is allocated right
4448 // after the GOT section, to calculate an absolute address of
4449 // the symbol GOT entry (got_origin + got_offset).
ebabffbd 4450 Arm_address got_origin;
7f5309a5
ILT
4451 got_origin = target->got_plt_section()->address();
4452 reloc_status = Arm_relocate_functions::got_prel(view,
4453 got_origin + got_offset,
4454 address);
4455 break;
4456
c121c671
DK
4457 case elfcpp::R_ARM_PLT32:
4458 gold_assert(gsym == NULL
4459 || gsym->has_plt_offset()
4460 || gsym->final_value_is_known()
4461 || (gsym->is_defined()
4462 && !gsym->is_from_dynobj()
4463 && !gsym->is_preemptible()));
4464 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
4465 address, has_thumb_bit);
4466 break;
4467
4468 case elfcpp::R_ARM_CALL:
4469 reloc_status = Arm_relocate_functions::call(view, object, psymval,
4470 address, has_thumb_bit);
4471 break;
4472
4473 case elfcpp::R_ARM_JUMP24:
4474 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
4475 address, has_thumb_bit);
4476 break;
4477
4478 case elfcpp::R_ARM_PREL31:
4479 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
4480 address, has_thumb_bit);
4481 break;
4482
4483 case elfcpp::R_ARM_TARGET1:
4484 // This should have been mapped to another type already.
4485 // Fall through.
4486 case elfcpp::R_ARM_COPY:
4487 case elfcpp::R_ARM_GLOB_DAT:
4488 case elfcpp::R_ARM_JUMP_SLOT:
4489 case elfcpp::R_ARM_RELATIVE:
4490 // These are relocations which should only be seen by the
4491 // dynamic linker, and should never be seen here.
4492 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4493 _("unexpected reloc %u in object file"),
4494 r_type);
4495 break;
4496
4497 default:
4498 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4499 _("unsupported reloc %u"),
4500 r_type);
4501 break;
4502 }
4503
4504 // Report any errors.
4505 switch (reloc_status)
4506 {
4507 case Arm_relocate_functions::STATUS_OKAY:
4508 break;
4509 case Arm_relocate_functions::STATUS_OVERFLOW:
4510 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4511 _("relocation overflow in relocation %u"),
4512 r_type);
4513 break;
4514 case Arm_relocate_functions::STATUS_BAD_RELOC:
4515 gold_error_at_location(
4516 relinfo,
4517 relnum,
4518 rel.get_r_offset(),
4519 _("unexpected opcode while processing relocation %u"),
4520 r_type);
4521 break;
4a657b0d
DK
4522 default:
4523 gold_unreachable();
4524 }
4525
4526 return true;
4527}
4528
4529// Relocate section data.
4530
4531template<bool big_endian>
4532void
4533Target_arm<big_endian>::relocate_section(
4534 const Relocate_info<32, big_endian>* relinfo,
4535 unsigned int sh_type,
4536 const unsigned char* prelocs,
4537 size_t reloc_count,
4538 Output_section* output_section,
4539 bool needs_special_offset_handling,
4540 unsigned char* view,
ebabffbd 4541 Arm_address address,
364c7fa5
ILT
4542 section_size_type view_size,
4543 const Reloc_symbol_changes* reloc_symbol_changes)
4a657b0d
DK
4544{
4545 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
4546 gold_assert(sh_type == elfcpp::SHT_REL);
4547
4548 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
4549 Arm_relocate>(
4550 relinfo,
4551 this,
4552 prelocs,
4553 reloc_count,
4554 output_section,
4555 needs_special_offset_handling,
4556 view,
4557 address,
364c7fa5
ILT
4558 view_size,
4559 reloc_symbol_changes);
4a657b0d
DK
4560}
4561
4562// Return the size of a relocation while scanning during a relocatable
4563// link.
4564
4565template<bool big_endian>
4566unsigned int
4567Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
4568 unsigned int r_type,
4569 Relobj* object)
4570{
4571 r_type = get_real_reloc_type(r_type);
4572 switch (r_type)
4573 {
4574 case elfcpp::R_ARM_NONE:
4575 return 0;
4576
5e445df6
ILT
4577 case elfcpp::R_ARM_ABS8:
4578 return 1;
4579
be8fcb75
ILT
4580 case elfcpp::R_ARM_ABS16:
4581 case elfcpp::R_ARM_THM_ABS5:
4582 return 2;
4583
4a657b0d 4584 case elfcpp::R_ARM_ABS32:
be8fcb75
ILT
4585 case elfcpp::R_ARM_ABS32_NOI:
4586 case elfcpp::R_ARM_ABS12:
4587 case elfcpp::R_ARM_BASE_ABS:
4a657b0d
DK
4588 case elfcpp::R_ARM_REL32:
4589 case elfcpp::R_ARM_THM_CALL:
4590 case elfcpp::R_ARM_GOTOFF32:
4591 case elfcpp::R_ARM_BASE_PREL:
4592 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 4593 case elfcpp::R_ARM_GOT_PREL:
4a657b0d
DK
4594 case elfcpp::R_ARM_PLT32:
4595 case elfcpp::R_ARM_CALL:
4596 case elfcpp::R_ARM_JUMP24:
4597 case elfcpp::R_ARM_PREL31:
fd3c5f0b
ILT
4598 case elfcpp::R_ARM_MOVW_ABS_NC:
4599 case elfcpp::R_ARM_MOVT_ABS:
4600 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4601 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
4602 case elfcpp::R_ARM_MOVW_PREL_NC:
4603 case elfcpp::R_ARM_MOVT_PREL:
4604 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4605 case elfcpp::R_ARM_THM_MOVT_PREL:
4a657b0d
DK
4606 return 4;
4607
4608 case elfcpp::R_ARM_TARGET1:
4609 // This should have been mapped to another type already.
4610 // Fall through.
4611 case elfcpp::R_ARM_COPY:
4612 case elfcpp::R_ARM_GLOB_DAT:
4613 case elfcpp::R_ARM_JUMP_SLOT:
4614 case elfcpp::R_ARM_RELATIVE:
4615 // These are relocations which should only be seen by the
4616 // dynamic linker, and should never be seen here.
4617 gold_error(_("%s: unexpected reloc %u in object file"),
4618 object->name().c_str(), r_type);
4619 return 0;
4620
4621 default:
4622 object->error(_("unsupported reloc %u in object file"), r_type);
4623 return 0;
4624 }
4625}
4626
4627// Scan the relocs during a relocatable link.
4628
4629template<bool big_endian>
4630void
4631Target_arm<big_endian>::scan_relocatable_relocs(
4a657b0d
DK
4632 Symbol_table* symtab,
4633 Layout* layout,
4634 Sized_relobj<32, big_endian>* object,
4635 unsigned int data_shndx,
4636 unsigned int sh_type,
4637 const unsigned char* prelocs,
4638 size_t reloc_count,
4639 Output_section* output_section,
4640 bool needs_special_offset_handling,
4641 size_t local_symbol_count,
4642 const unsigned char* plocal_symbols,
4643 Relocatable_relocs* rr)
4644{
4645 gold_assert(sh_type == elfcpp::SHT_REL);
4646
4647 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
4648 Relocatable_size_for_reloc> Scan_relocatable_relocs;
4649
4650 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
4651 Scan_relocatable_relocs>(
4a657b0d
DK
4652 symtab,
4653 layout,
4654 object,
4655 data_shndx,
4656 prelocs,
4657 reloc_count,
4658 output_section,
4659 needs_special_offset_handling,
4660 local_symbol_count,
4661 plocal_symbols,
4662 rr);
4663}
4664
4665// Relocate a section during a relocatable link.
4666
4667template<bool big_endian>
4668void
4669Target_arm<big_endian>::relocate_for_relocatable(
4670 const Relocate_info<32, big_endian>* relinfo,
4671 unsigned int sh_type,
4672 const unsigned char* prelocs,
4673 size_t reloc_count,
4674 Output_section* output_section,
4675 off_t offset_in_output_section,
4676 const Relocatable_relocs* rr,
4677 unsigned char* view,
ebabffbd 4678 Arm_address view_address,
4a657b0d
DK
4679 section_size_type view_size,
4680 unsigned char* reloc_view,
4681 section_size_type reloc_view_size)
4682{
4683 gold_assert(sh_type == elfcpp::SHT_REL);
4684
4685 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
4686 relinfo,
4687 prelocs,
4688 reloc_count,
4689 output_section,
4690 offset_in_output_section,
4691 rr,
4692 view,
4693 view_address,
4694 view_size,
4695 reloc_view,
4696 reloc_view_size);
4697}
4698
94cdfcff
DK
4699// Return the value to use for a dynamic symbol which requires special
4700// treatment. This is how we support equality comparisons of function
4701// pointers across shared library boundaries, as described in the
4702// processor specific ABI supplement.
4703
4a657b0d
DK
4704template<bool big_endian>
4705uint64_t
94cdfcff 4706Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4a657b0d 4707{
94cdfcff
DK
4708 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4709 return this->plt_section()->address() + gsym->plt_offset();
4a657b0d
DK
4710}
4711
4712// Map platform-specific relocs to real relocs
4713//
4714template<bool big_endian>
4715unsigned int
4716Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
4717{
4718 switch (r_type)
4719 {
4720 case elfcpp::R_ARM_TARGET1:
4721 // This is either R_ARM_ABS32 or R_ARM_REL32;
4722 return elfcpp::R_ARM_ABS32;
4723
4724 case elfcpp::R_ARM_TARGET2:
4725 // This can be any reloc type but ususally is R_ARM_GOT_PREL
4726 return elfcpp::R_ARM_GOT_PREL;
4727
4728 default:
4729 return r_type;
4730 }
4731}
4732
d5b40221
DK
4733// Whether if two EABI versions V1 and V2 are compatible.
4734
4735template<bool big_endian>
4736bool
4737Target_arm<big_endian>::are_eabi_versions_compatible(
4738 elfcpp::Elf_Word v1,
4739 elfcpp::Elf_Word v2)
4740{
4741 // v4 and v5 are the same spec before and after it was released,
4742 // so allow mixing them.
4743 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
4744 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
4745 return true;
4746
4747 return v1 == v2;
4748}
4749
4750// Combine FLAGS from an input object called NAME and the processor-specific
4751// flags in the ELF header of the output. Much of this is adapted from the
4752// processor-specific flags merging code in elf32_arm_merge_private_bfd_data
4753// in bfd/elf32-arm.c.
4754
4755template<bool big_endian>
4756void
4757Target_arm<big_endian>::merge_processor_specific_flags(
4758 const std::string& name,
4759 elfcpp::Elf_Word flags)
4760{
4761 if (this->are_processor_specific_flags_set())
4762 {
4763 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
4764
4765 // Nothing to merge if flags equal to those in output.
4766 if (flags == out_flags)
4767 return;
4768
4769 // Complain about various flag mismatches.
4770 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
4771 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
4772 if (!this->are_eabi_versions_compatible(version1, version2))
4773 gold_error(_("Source object %s has EABI version %d but output has "
4774 "EABI version %d."),
4775 name.c_str(),
4776 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
4777 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
4778 }
4779 else
4780 {
4781 // If the input is the default architecture and had the default
4782 // flags then do not bother setting the flags for the output
4783 // architecture, instead allow future merges to do this. If no
4784 // future merges ever set these flags then they will retain their
4785 // uninitialised values, which surprise surprise, correspond
4786 // to the default values.
4787 if (flags == 0)
4788 return;
4789
4790 // This is the first time, just copy the flags.
4791 // We only copy the EABI version for now.
4792 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
4793 }
4794}
4795
4796// Adjust ELF file header.
4797template<bool big_endian>
4798void
4799Target_arm<big_endian>::do_adjust_elf_header(
4800 unsigned char* view,
4801 int len) const
4802{
4803 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
4804
4805 elfcpp::Ehdr<32, big_endian> ehdr(view);
4806 unsigned char e_ident[elfcpp::EI_NIDENT];
4807 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
4808
4809 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
4810 == elfcpp::EF_ARM_EABI_UNKNOWN)
4811 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
4812 else
4813 e_ident[elfcpp::EI_OSABI] = 0;
4814 e_ident[elfcpp::EI_ABIVERSION] = 0;
4815
4816 // FIXME: Do EF_ARM_BE8 adjustment.
4817
4818 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
4819 oehdr.put_e_ident(e_ident);
4820}
4821
4822// do_make_elf_object to override the same function in the base class.
4823// We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
4824// to store ARM specific information. Hence we need to have our own
4825// ELF object creation.
4826
4827template<bool big_endian>
4828Object*
4829Target_arm<big_endian>::do_make_elf_object(
4830 const std::string& name,
4831 Input_file* input_file,
4832 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
4833{
4834 int et = ehdr.get_e_type();
4835 if (et == elfcpp::ET_REL)
4836 {
4837 Arm_relobj<big_endian>* obj =
4838 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
4839 obj->setup();
4840 return obj;
4841 }
4842 else if (et == elfcpp::ET_DYN)
4843 {
4844 Sized_dynobj<32, big_endian>* obj =
4845 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
4846 obj->setup();
4847 return obj;
4848 }
4849 else
4850 {
4851 gold_error(_("%s: unsupported ELF file type %d"),
4852 name.c_str(), et);
4853 return NULL;
4854 }
4855}
4856
4a657b0d
DK
4857// The selector for arm object files.
4858
4859template<bool big_endian>
4860class Target_selector_arm : public Target_selector
4861{
4862 public:
4863 Target_selector_arm()
4864 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
4865 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
4866 { }
4867
4868 Target*
4869 do_instantiate_target()
4870 { return new Target_arm<big_endian>(); }
4871};
4872
4873Target_selector_arm<false> target_selector_arm;
4874Target_selector_arm<true> target_selector_armbe;
4875
4876} // End anonymous namespace.
This page took 0.235643 seconds and 4 git commands to generate.