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