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