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