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