* object.cc (Sized_relobj::do_layout): Change to call layout_gnu_stack
[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
b521dfe4
DK
4206 unsigned int reloc_size;
4207 if (sh_type == elfcpp::SHT_REL)
4208 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
4209 else
4210 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
44272192
DK
4211
4212 // Ignore reloc section with unexpected entsize or uneven size.
4213 // The error will be reported in the final link.
4214 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
4215 return false;
4216
4217 return true;
4218}
4219
4220// Determine if we want to scan the SHNDX-th section for non-relocation stubs.
4221// This is a helper for Arm_relobj::scan_sections_for_stubs() below.
4222
4223template<bool big_endian>
4224bool
4225Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
4226 const elfcpp::Shdr<32, big_endian>& shdr,
4227 unsigned int shndx,
4228 Output_section* os,
4229 const Symbol_table* symtab)
4230{
4231 // We only scan non-empty code sections.
4232 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) == 0
4233 || shdr.get_sh_size() == 0)
4234 return false;
4235
4236 // Ignore discarded or ICF'ed sections.
4237 if (os == NULL || symtab->is_section_folded(this, shndx))
4238 return false;
4239
4240 // Find output address of section.
4241 Arm_address address = os->output_address(this, shndx, 0);
4242
4243 // If the section does not cross any 4K-boundaries, it does not need to
4244 // be scanned.
4245 if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
4246 return false;
4247
4248 return true;
4249}
4250
4251// Scan a section for Cortex-A8 workaround.
4252
4253template<bool big_endian>
4254void
4255Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
4256 const elfcpp::Shdr<32, big_endian>& shdr,
4257 unsigned int shndx,
4258 Output_section* os,
4259 Target_arm<big_endian>* arm_target)
4260{
4261 Arm_address output_address = os->output_address(this, shndx, 0);
4262
4263 // Get the section contents.
4264 section_size_type input_view_size = 0;
4265 const unsigned char* input_view =
4266 this->section_contents(shndx, &input_view_size, false);
4267
4268 // We need to go through the mapping symbols to determine what to
4269 // scan. There are two reasons. First, we should look at THUMB code and
4270 // THUMB code only. Second, we only want to look at the 4K-page boundary
4271 // to speed up the scanning.
4272
4273 // Look for the first mapping symbol in this section. It should be
4274 // at (shndx, 0).
4275 Mapping_symbol_position section_start(shndx, 0);
4276 typename Mapping_symbols_info::const_iterator p =
4277 this->mapping_symbols_info_.lower_bound(section_start);
4278
4279 if (p == this->mapping_symbols_info_.end()
4280 || p->first != section_start)
4281 {
4282 gold_warning(_("Cortex-A8 erratum scanning failed because there "
4283 "is no mapping symbols for section %u of %s"),
4284 shndx, this->name().c_str());
4285 return;
4286 }
4287
4288 while (p != this->mapping_symbols_info_.end()
4289 && p->first.first == shndx)
4290 {
4291 typename Mapping_symbols_info::const_iterator next =
4292 this->mapping_symbols_info_.upper_bound(p->first);
4293
4294 // Only scan part of a section with THUMB code.
4295 if (p->second == 't')
4296 {
4297 // Determine the end of this range.
4298 section_size_type span_start =
4299 convert_to_section_size_type(p->first.second);
4300 section_size_type span_end;
4301 if (next != this->mapping_symbols_info_.end()
4302 && next->first.first == shndx)
4303 span_end = convert_to_section_size_type(next->first.second);
4304 else
4305 span_end = convert_to_section_size_type(shdr.get_sh_size());
4306
4307 if (((span_start + output_address) & ~0xfffUL)
4308 != ((span_end + output_address - 1) & ~0xfffUL))
4309 {
4310 arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
4311 span_start, span_end,
4312 input_view,
4313 output_address);
4314 }
4315 }
4316
4317 p = next;
4318 }
4319}
4320
8ffa3667
DK
4321// Scan relocations for stub generation.
4322
4323template<bool big_endian>
4324void
4325Arm_relobj<big_endian>::scan_sections_for_stubs(
4326 Target_arm<big_endian>* arm_target,
4327 const Symbol_table* symtab,
2ea97941 4328 const Layout* layout)
8ffa3667 4329{
2ea97941
ILT
4330 unsigned int shnum = this->shnum();
4331 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
8ffa3667
DK
4332
4333 // Read the section headers.
4334 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
2ea97941 4335 shnum * shdr_size,
8ffa3667
DK
4336 true, true);
4337
4338 // To speed up processing, we set up hash tables for fast lookup of
4339 // input offsets to output addresses.
4340 this->initialize_input_to_output_maps();
4341
4342 const Relobj::Output_sections& out_sections(this->output_sections());
4343
4344 Relocate_info<32, big_endian> relinfo;
8ffa3667 4345 relinfo.symtab = symtab;
2ea97941 4346 relinfo.layout = layout;
8ffa3667
DK
4347 relinfo.object = this;
4348
44272192 4349 // Do relocation stubs scanning.
2ea97941
ILT
4350 const unsigned char* p = pshdrs + shdr_size;
4351 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
8ffa3667 4352 {
44272192
DK
4353 const elfcpp::Shdr<32, big_endian> shdr(p);
4354 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab))
8ffa3667 4355 {
44272192
DK
4356 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
4357 Arm_address output_offset = this->get_output_section_offset(index);
4358 Arm_address output_address;
4359 if(output_offset != invalid_address)
4360 output_address = out_sections[index]->address() + output_offset;
4361 else
4362 {
4363 // Currently this only happens for a relaxed section.
4364 const Output_relaxed_input_section* poris =
4365 out_sections[index]->find_relaxed_input_section(this, index);
4366 gold_assert(poris != NULL);
4367 output_address = poris->address();
4368 }
8ffa3667 4369
44272192
DK
4370 // Get the relocations.
4371 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
4372 shdr.get_sh_size(),
4373 true, false);
4374
4375 // Get the section contents. This does work for the case in which
4376 // we modify the contents of an input section. We need to pass the
4377 // output view under such circumstances.
4378 section_size_type input_view_size = 0;
4379 const unsigned char* input_view =
4380 this->section_contents(index, &input_view_size, false);
4381
4382 relinfo.reloc_shndx = i;
4383 relinfo.data_shndx = index;
4384 unsigned int sh_type = shdr.get_sh_type();
b521dfe4
DK
4385 unsigned int reloc_size;
4386 if (sh_type == elfcpp::SHT_REL)
4387 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
4388 else
4389 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
44272192
DK
4390
4391 Output_section* os = out_sections[index];
4392 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
4393 shdr.get_sh_size() / reloc_size,
4394 os,
4395 output_offset == invalid_address,
4396 input_view, output_address,
4397 input_view_size);
8ffa3667 4398 }
44272192 4399 }
8ffa3667 4400
44272192
DK
4401 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
4402 // after its relocation section, if there is one, is processed for
4403 // relocation stubs. Merging this loop with the one above would have been
4404 // complicated since we would have had to make sure that relocation stub
4405 // scanning is done first.
4406 if (arm_target->fix_cortex_a8())
4407 {
4408 const unsigned char* p = pshdrs + shdr_size;
4409 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
8ffa3667 4410 {
44272192
DK
4411 const elfcpp::Shdr<32, big_endian> shdr(p);
4412 if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
4413 out_sections[i],
4414 symtab))
4415 this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
4416 arm_target);
8ffa3667 4417 }
8ffa3667
DK
4418 }
4419
4420 // After we've done the relocations, we release the hash tables,
4421 // since we no longer need them.
4422 this->free_input_to_output_maps();
4423}
4424
4425// Count the local symbols. The ARM backend needs to know if a symbol
4426// is a THUMB function or not. For global symbols, it is easy because
4427// the Symbol object keeps the ELF symbol type. For local symbol it is
4428// harder because we cannot access this information. So we override the
4429// do_count_local_symbol in parent and scan local symbols to mark
4430// THUMB functions. This is not the most efficient way but I do not want to
4431// slow down other ports by calling a per symbol targer hook inside
4432// Sized_relobj<size, big_endian>::do_count_local_symbols.
4433
4434template<bool big_endian>
4435void
4436Arm_relobj<big_endian>::do_count_local_symbols(
4437 Stringpool_template<char>* pool,
4438 Stringpool_template<char>* dynpool)
4439{
4440 // We need to fix-up the values of any local symbols whose type are
4441 // STT_ARM_TFUNC.
4442
4443 // Ask parent to count the local symbols.
4444 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
4445 const unsigned int loccount = this->local_symbol_count();
4446 if (loccount == 0)
4447 return;
4448
4449 // Intialize the thumb function bit-vector.
4450 std::vector<bool> empty_vector(loccount, false);
4451 this->local_symbol_is_thumb_function_.swap(empty_vector);
4452
4453 // Read the symbol table section header.
2ea97941 4454 const unsigned int symtab_shndx = this->symtab_shndx();
8ffa3667 4455 elfcpp::Shdr<32, big_endian>
2ea97941 4456 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
8ffa3667
DK
4457 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
4458
4459 // Read the local symbols.
2ea97941 4460 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
8ffa3667 4461 gold_assert(loccount == symtabshdr.get_sh_info());
2ea97941 4462 off_t locsize = loccount * sym_size;
8ffa3667
DK
4463 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
4464 locsize, true, true);
4465
20138696
DK
4466 // For mapping symbol processing, we need to read the symbol names.
4467 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
4468 if (strtab_shndx >= this->shnum())
4469 {
4470 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
4471 return;
4472 }
4473
4474 elfcpp::Shdr<32, big_endian>
4475 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
4476 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
4477 {
4478 this->error(_("symbol table name section has wrong type: %u"),
4479 static_cast<unsigned int>(strtabshdr.get_sh_type()));
4480 return;
4481 }
4482 const char* pnames =
4483 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
4484 strtabshdr.get_sh_size(),
4485 false, false));
4486
8ffa3667
DK
4487 // Loop over the local symbols and mark any local symbols pointing
4488 // to THUMB functions.
4489
4490 // Skip the first dummy symbol.
2ea97941 4491 psyms += sym_size;
8ffa3667
DK
4492 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
4493 this->local_values();
2ea97941 4494 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
8ffa3667
DK
4495 {
4496 elfcpp::Sym<32, big_endian> sym(psyms);
4497 elfcpp::STT st_type = sym.get_st_type();
4498 Symbol_value<32>& lv((*plocal_values)[i]);
4499 Arm_address input_value = lv.input_value();
4500
20138696
DK
4501 // Check to see if this is a mapping symbol.
4502 const char* sym_name = pnames + sym.get_st_name();
4503 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
4504 {
4505 unsigned int input_shndx = sym.get_st_shndx();
4506
4507 // Strip of LSB in case this is a THUMB symbol.
4508 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
4509 this->mapping_symbols_info_[msp] = sym_name[1];
4510 }
4511
8ffa3667
DK
4512 if (st_type == elfcpp::STT_ARM_TFUNC
4513 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
4514 {
4515 // This is a THUMB function. Mark this and canonicalize the
4516 // symbol value by setting LSB.
4517 this->local_symbol_is_thumb_function_[i] = true;
4518 if ((input_value & 1) == 0)
4519 lv.set_input_value(input_value | 1);
4520 }
4521 }
4522}
4523
4524// Relocate sections.
4525template<bool big_endian>
4526void
4527Arm_relobj<big_endian>::do_relocate_sections(
8ffa3667 4528 const Symbol_table* symtab,
2ea97941 4529 const Layout* layout,
8ffa3667
DK
4530 const unsigned char* pshdrs,
4531 typename Sized_relobj<32, big_endian>::Views* pviews)
4532{
4533 // Call parent to relocate sections.
2ea97941 4534 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
43d12afe 4535 pviews);
8ffa3667
DK
4536
4537 // We do not generate stubs if doing a relocatable link.
4538 if (parameters->options().relocatable())
4539 return;
4540
4541 // Relocate stub tables.
2ea97941 4542 unsigned int shnum = this->shnum();
8ffa3667
DK
4543
4544 Target_arm<big_endian>* arm_target =
4545 Target_arm<big_endian>::default_target();
4546
4547 Relocate_info<32, big_endian> relinfo;
8ffa3667 4548 relinfo.symtab = symtab;
2ea97941 4549 relinfo.layout = layout;
8ffa3667
DK
4550 relinfo.object = this;
4551
2ea97941 4552 for (unsigned int i = 1; i < shnum; ++i)
8ffa3667
DK
4553 {
4554 Arm_input_section<big_endian>* arm_input_section =
4555 arm_target->find_arm_input_section(this, i);
4556
4557 if (arm_input_section == NULL
4558 || !arm_input_section->is_stub_table_owner()
4559 || arm_input_section->stub_table()->empty())
4560 continue;
4561
4562 // We cannot discard a section if it owns a stub table.
4563 Output_section* os = this->output_section(i);
4564 gold_assert(os != NULL);
4565
4566 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
4567 relinfo.reloc_shdr = NULL;
4568 relinfo.data_shndx = i;
4569 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
4570
4571 gold_assert((*pviews)[i].view != NULL);
4572
4573 // We are passed the output section view. Adjust it to cover the
4574 // stub table only.
2ea97941
ILT
4575 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
4576 gold_assert((stub_table->address() >= (*pviews)[i].address)
4577 && ((stub_table->address() + stub_table->data_size())
8ffa3667
DK
4578 <= (*pviews)[i].address + (*pviews)[i].view_size));
4579
2ea97941
ILT
4580 off_t offset = stub_table->address() - (*pviews)[i].address;
4581 unsigned char* view = (*pviews)[i].view + offset;
4582 Arm_address address = stub_table->address();
4583 section_size_type view_size = stub_table->data_size();
8ffa3667 4584
2ea97941
ILT
4585 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
4586 view_size);
8ffa3667
DK
4587 }
4588}
4589
a0351a69
DK
4590// Helper functions for both Arm_relobj and Arm_dynobj to read ARM
4591// ABI information.
4592
4593template<bool big_endian>
4594Attributes_section_data*
4595read_arm_attributes_section(
4596 Object* object,
4597 Read_symbols_data *sd)
4598{
4599 // Read the attributes section if there is one.
4600 // We read from the end because gas seems to put it near the end of
4601 // the section headers.
4602 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4603 const unsigned char *ps =
4604 sd->section_headers->data() + shdr_size * (object->shnum() - 1);
4605 for (unsigned int i = object->shnum(); i > 0; --i, ps -= shdr_size)
4606 {
4607 elfcpp::Shdr<32, big_endian> shdr(ps);
4608 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
4609 {
4610 section_offset_type section_offset = shdr.get_sh_offset();
4611 section_size_type section_size =
4612 convert_to_section_size_type(shdr.get_sh_size());
4613 File_view* view = object->get_lasting_view(section_offset,
4614 section_size, true, false);
4615 return new Attributes_section_data(view->data(), section_size);
4616 }
4617 }
4618 return NULL;
4619}
4620
d5b40221
DK
4621// Read the symbol information.
4622
4623template<bool big_endian>
4624void
4625Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4626{
4627 // Call parent class to read symbol information.
4628 Sized_relobj<32, big_endian>::do_read_symbols(sd);
4629
4630 // Read processor-specific flags in ELF file header.
4631 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4632 elfcpp::Elf_sizes<32>::ehdr_size,
4633 true, false);
4634 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4635 this->processor_specific_flags_ = ehdr.get_e_flags();
a0351a69
DK
4636 this->attributes_section_data_ =
4637 read_arm_attributes_section<big_endian>(this, sd);
d5b40221
DK
4638}
4639
99e5bff2
DK
4640// Process relocations for garbage collection. The ARM target uses .ARM.exidx
4641// sections for unwinding. These sections are referenced implicitly by
4642// text sections linked in the section headers. If we ignore these implict
4643// references, the .ARM.exidx sections and any .ARM.extab sections they use
4644// will be garbage-collected incorrectly. Hence we override the same function
4645// in the base class to handle these implicit references.
4646
4647template<bool big_endian>
4648void
4649Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
4650 Layout* layout,
4651 Read_relocs_data* rd)
4652{
4653 // First, call base class method to process relocations in this object.
4654 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
4655
4656 unsigned int shnum = this->shnum();
4657 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4658 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
4659 shnum * shdr_size,
4660 true, true);
4661
4662 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
4663 // to these from the linked text sections.
4664 const unsigned char* ps = pshdrs + shdr_size;
4665 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
4666 {
4667 elfcpp::Shdr<32, big_endian> shdr(ps);
4668 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
4669 {
4670 // Found an .ARM.exidx section, add it to the set of reachable
4671 // sections from its linked text section.
4672 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
4673 symtab->gc()->add_reference(this, text_shndx, this, i);
4674 }
4675 }
4676}
4677
d5b40221
DK
4678// Arm_dynobj methods.
4679
4680// Read the symbol information.
4681
4682template<bool big_endian>
4683void
4684Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4685{
4686 // Call parent class to read symbol information.
4687 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
4688
4689 // Read processor-specific flags in ELF file header.
4690 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4691 elfcpp::Elf_sizes<32>::ehdr_size,
4692 true, false);
4693 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4694 this->processor_specific_flags_ = ehdr.get_e_flags();
a0351a69
DK
4695 this->attributes_section_data_ =
4696 read_arm_attributes_section<big_endian>(this, sd);
d5b40221
DK
4697}
4698
e9bbb538
DK
4699// Stub_addend_reader methods.
4700
4701// Read the addend of a REL relocation of type R_TYPE at VIEW.
4702
4703template<bool big_endian>
4704elfcpp::Elf_types<32>::Elf_Swxword
4705Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
4706 unsigned int r_type,
4707 const unsigned char* view,
4708 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
4709{
089d69dc
DK
4710 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
4711
e9bbb538
DK
4712 switch (r_type)
4713 {
4714 case elfcpp::R_ARM_CALL:
4715 case elfcpp::R_ARM_JUMP24:
4716 case elfcpp::R_ARM_PLT32:
4717 {
4718 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4719 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4720 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
4721 return utils::sign_extend<26>(val << 2);
4722 }
4723
4724 case elfcpp::R_ARM_THM_CALL:
4725 case elfcpp::R_ARM_THM_JUMP24:
4726 case elfcpp::R_ARM_THM_XPC22:
4727 {
e9bbb538
DK
4728 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4729 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4730 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4731 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
089d69dc 4732 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
e9bbb538
DK
4733 }
4734
4735 case elfcpp::R_ARM_THM_JUMP19:
4736 {
4737 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4738 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4739 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4740 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
089d69dc 4741 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
e9bbb538
DK
4742 }
4743
4744 default:
4745 gold_unreachable();
4746 }
4747}
4748
94cdfcff
DK
4749// A class to handle the PLT data.
4750
4751template<bool big_endian>
4752class Output_data_plt_arm : public Output_section_data
4753{
4754 public:
4755 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
4756 Reloc_section;
4757
4758 Output_data_plt_arm(Layout*, Output_data_space*);
4759
4760 // Add an entry to the PLT.
4761 void
4762 add_entry(Symbol* gsym);
4763
4764 // Return the .rel.plt section data.
4765 const Reloc_section*
4766 rel_plt() const
4767 { return this->rel_; }
4768
4769 protected:
4770 void
4771 do_adjust_output_section(Output_section* os);
4772
4773 // Write to a map file.
4774 void
4775 do_print_to_mapfile(Mapfile* mapfile) const
4776 { mapfile->print_output_data(this, _("** PLT")); }
4777
4778 private:
4779 // Template for the first PLT entry.
4780 static const uint32_t first_plt_entry[5];
4781
4782 // Template for subsequent PLT entries.
4783 static const uint32_t plt_entry[3];
4784
4785 // Set the final size.
4786 void
4787 set_final_data_size()
4788 {
4789 this->set_data_size(sizeof(first_plt_entry)
4790 + this->count_ * sizeof(plt_entry));
4791 }
4792
4793 // Write out the PLT data.
4794 void
4795 do_write(Output_file*);
4796
4797 // The reloc section.
4798 Reloc_section* rel_;
4799 // The .got.plt section.
4800 Output_data_space* got_plt_;
4801 // The number of PLT entries.
4802 unsigned int count_;
4803};
4804
4805// Create the PLT section. The ordinary .got section is an argument,
4806// since we need to refer to the start. We also create our own .got
4807// section just for PLT entries.
4808
4809template<bool big_endian>
2ea97941 4810Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
94cdfcff
DK
4811 Output_data_space* got_plt)
4812 : Output_section_data(4), got_plt_(got_plt), count_(0)
4813{
4814 this->rel_ = new Reloc_section(false);
2ea97941 4815 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1a2dff53
ILT
4816 elfcpp::SHF_ALLOC, this->rel_, true, false,
4817 false, false);
94cdfcff
DK
4818}
4819
4820template<bool big_endian>
4821void
4822Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
4823{
4824 os->set_entsize(0);
4825}
4826
4827// Add an entry to the PLT.
4828
4829template<bool big_endian>
4830void
4831Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
4832{
4833 gold_assert(!gsym->has_plt_offset());
4834
4835 // Note that when setting the PLT offset we skip the initial
4836 // reserved PLT entry.
4837 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
4838 + sizeof(first_plt_entry));
4839
4840 ++this->count_;
4841
4842 section_offset_type got_offset = this->got_plt_->current_data_size();
4843
4844 // Every PLT entry needs a GOT entry which points back to the PLT
4845 // entry (this will be changed by the dynamic linker, normally
4846 // lazily when the function is called).
4847 this->got_plt_->set_current_data_size(got_offset + 4);
4848
4849 // Every PLT entry needs a reloc.
4850 gsym->set_needs_dynsym_entry();
4851 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
4852 got_offset);
4853
4854 // Note that we don't need to save the symbol. The contents of the
4855 // PLT are independent of which symbols are used. The symbols only
4856 // appear in the relocations.
4857}
4858
4859// ARM PLTs.
4860// FIXME: This is not very flexible. Right now this has only been tested
4861// on armv5te. If we are to support additional architecture features like
4862// Thumb-2 or BE8, we need to make this more flexible like GNU ld.
4863
4864// The first entry in the PLT.
4865template<bool big_endian>
4866const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
4867{
4868 0xe52de004, // str lr, [sp, #-4]!
4869 0xe59fe004, // ldr lr, [pc, #4]
4870 0xe08fe00e, // add lr, pc, lr
4871 0xe5bef008, // ldr pc, [lr, #8]!
4872 0x00000000, // &GOT[0] - .
4873};
4874
4875// Subsequent entries in the PLT.
4876
4877template<bool big_endian>
4878const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
4879{
4880 0xe28fc600, // add ip, pc, #0xNN00000
4881 0xe28cca00, // add ip, ip, #0xNN000
4882 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4883};
4884
4885// Write out the PLT. This uses the hand-coded instructions above,
4886// and adjusts them as needed. This is all specified by the arm ELF
4887// Processor Supplement.
4888
4889template<bool big_endian>
4890void
4891Output_data_plt_arm<big_endian>::do_write(Output_file* of)
4892{
2ea97941 4893 const off_t offset = this->offset();
94cdfcff
DK
4894 const section_size_type oview_size =
4895 convert_to_section_size_type(this->data_size());
2ea97941 4896 unsigned char* const oview = of->get_output_view(offset, oview_size);
94cdfcff
DK
4897
4898 const off_t got_file_offset = this->got_plt_->offset();
4899 const section_size_type got_size =
4900 convert_to_section_size_type(this->got_plt_->data_size());
4901 unsigned char* const got_view = of->get_output_view(got_file_offset,
4902 got_size);
4903 unsigned char* pov = oview;
4904
ebabffbd
DK
4905 Arm_address plt_address = this->address();
4906 Arm_address got_address = this->got_plt_->address();
94cdfcff
DK
4907
4908 // Write first PLT entry. All but the last word are constants.
4909 const size_t num_first_plt_words = (sizeof(first_plt_entry)
4910 / sizeof(plt_entry[0]));
4911 for (size_t i = 0; i < num_first_plt_words - 1; i++)
4912 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
4913 // Last word in first PLT entry is &GOT[0] - .
4914 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
4915 got_address - (plt_address + 16));
4916 pov += sizeof(first_plt_entry);
4917
4918 unsigned char* got_pov = got_view;
4919
4920 memset(got_pov, 0, 12);
4921 got_pov += 12;
4922
4923 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
4924 unsigned int plt_offset = sizeof(first_plt_entry);
4925 unsigned int plt_rel_offset = 0;
4926 unsigned int got_offset = 12;
4927 const unsigned int count = this->count_;
4928 for (unsigned int i = 0;
4929 i < count;
4930 ++i,
4931 pov += sizeof(plt_entry),
4932 got_pov += 4,
4933 plt_offset += sizeof(plt_entry),
4934 plt_rel_offset += rel_size,
4935 got_offset += 4)
4936 {
4937 // Set and adjust the PLT entry itself.
2ea97941
ILT
4938 int32_t offset = ((got_address + got_offset)
4939 - (plt_address + plt_offset + 8));
94cdfcff 4940
2ea97941
ILT
4941 gold_assert(offset >= 0 && offset < 0x0fffffff);
4942 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
94cdfcff 4943 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
2ea97941 4944 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
94cdfcff 4945 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
2ea97941 4946 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
94cdfcff
DK
4947 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
4948
4949 // Set the entry in the GOT.
4950 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
4951 }
4952
4953 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
4954 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
4955
2ea97941 4956 of->write_output_view(offset, oview_size, oview);
94cdfcff
DK
4957 of->write_output_view(got_file_offset, got_size, got_view);
4958}
4959
4960// Create a PLT entry for a global symbol.
4961
4962template<bool big_endian>
4963void
2ea97941 4964Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
94cdfcff
DK
4965 Symbol* gsym)
4966{
4967 if (gsym->has_plt_offset())
4968 return;
4969
4970 if (this->plt_ == NULL)
4971 {
4972 // Create the GOT sections first.
2ea97941 4973 this->got_section(symtab, layout);
94cdfcff 4974
2ea97941
ILT
4975 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
4976 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
4977 (elfcpp::SHF_ALLOC
4978 | elfcpp::SHF_EXECINSTR),
1a2dff53 4979 this->plt_, false, false, false, false);
94cdfcff
DK
4980 }
4981 this->plt_->add_entry(gsym);
4982}
4983
4a657b0d
DK
4984// Report an unsupported relocation against a local symbol.
4985
4986template<bool big_endian>
4987void
4988Target_arm<big_endian>::Scan::unsupported_reloc_local(
4989 Sized_relobj<32, big_endian>* object,
4990 unsigned int r_type)
4991{
4992 gold_error(_("%s: unsupported reloc %u against local symbol"),
4993 object->name().c_str(), r_type);
4994}
4995
bec53400
DK
4996// We are about to emit a dynamic relocation of type R_TYPE. If the
4997// dynamic linker does not support it, issue an error. The GNU linker
4998// only issues a non-PIC error for an allocated read-only section.
4999// Here we know the section is allocated, but we don't know that it is
5000// read-only. But we check for all the relocation types which the
5001// glibc dynamic linker supports, so it seems appropriate to issue an
5002// error even if the section is not read-only.
5003
5004template<bool big_endian>
5005void
5006Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
5007 unsigned int r_type)
5008{
5009 switch (r_type)
5010 {
5011 // These are the relocation types supported by glibc for ARM.
5012 case elfcpp::R_ARM_RELATIVE:
5013 case elfcpp::R_ARM_COPY:
5014 case elfcpp::R_ARM_GLOB_DAT:
5015 case elfcpp::R_ARM_JUMP_SLOT:
5016 case elfcpp::R_ARM_ABS32:
be8fcb75 5017 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
5018 case elfcpp::R_ARM_PC24:
5019 // FIXME: The following 3 types are not supported by Android's dynamic
5020 // linker.
5021 case elfcpp::R_ARM_TLS_DTPMOD32:
5022 case elfcpp::R_ARM_TLS_DTPOFF32:
5023 case elfcpp::R_ARM_TLS_TPOFF32:
5024 return;
5025
5026 default:
5027 // This prevents us from issuing more than one error per reloc
5028 // section. But we can still wind up issuing more than one
5029 // error per object file.
5030 if (this->issued_non_pic_error_)
5031 return;
5032 object->error(_("requires unsupported dynamic reloc; "
5033 "recompile with -fPIC"));
5034 this->issued_non_pic_error_ = true;
5035 return;
5036
5037 case elfcpp::R_ARM_NONE:
5038 gold_unreachable();
5039 }
5040}
5041
4a657b0d 5042// Scan a relocation for a local symbol.
bec53400
DK
5043// FIXME: This only handles a subset of relocation types used by Android
5044// on ARM v5te devices.
4a657b0d
DK
5045
5046template<bool big_endian>
5047inline void
ad0f2072 5048Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
2ea97941 5049 Layout* layout,
bec53400 5050 Target_arm* target,
4a657b0d 5051 Sized_relobj<32, big_endian>* object,
bec53400
DK
5052 unsigned int data_shndx,
5053 Output_section* output_section,
5054 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
5055 unsigned int r_type,
5056 const elfcpp::Sym<32, big_endian>&)
5057{
a6d1ef57 5058 r_type = get_real_reloc_type(r_type);
4a657b0d
DK
5059 switch (r_type)
5060 {
5061 case elfcpp::R_ARM_NONE:
5062 break;
5063
bec53400 5064 case elfcpp::R_ARM_ABS32:
be8fcb75 5065 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
5066 // If building a shared library (or a position-independent
5067 // executable), we need to create a dynamic relocation for
5068 // this location. The relocation applied at link time will
5069 // apply the link-time value, so we flag the location with
5070 // an R_ARM_RELATIVE relocation so the dynamic loader can
5071 // relocate it easily.
5072 if (parameters->options().output_is_position_independent())
5073 {
2ea97941 5074 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
bec53400
DK
5075 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
5076 // If we are to add more other reloc types than R_ARM_ABS32,
5077 // we need to add check_non_pic(object, r_type) here.
5078 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
5079 output_section, data_shndx,
5080 reloc.get_r_offset());
5081 }
5082 break;
5083
5084 case elfcpp::R_ARM_REL32:
5085 case elfcpp::R_ARM_THM_CALL:
5086 case elfcpp::R_ARM_CALL:
5087 case elfcpp::R_ARM_PREL31:
5088 case elfcpp::R_ARM_JUMP24:
5089 case elfcpp::R_ARM_PLT32:
be8fcb75
ILT
5090 case elfcpp::R_ARM_THM_ABS5:
5091 case elfcpp::R_ARM_ABS8:
5092 case elfcpp::R_ARM_ABS12:
5093 case elfcpp::R_ARM_ABS16:
5094 case elfcpp::R_ARM_BASE_ABS:
fd3c5f0b
ILT
5095 case elfcpp::R_ARM_MOVW_ABS_NC:
5096 case elfcpp::R_ARM_MOVT_ABS:
5097 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5098 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
5099 case elfcpp::R_ARM_MOVW_PREL_NC:
5100 case elfcpp::R_ARM_MOVT_PREL:
5101 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5102 case elfcpp::R_ARM_THM_MOVT_PREL:
bec53400
DK
5103 break;
5104
5105 case elfcpp::R_ARM_GOTOFF32:
5106 // We need a GOT section:
2ea97941 5107 target->got_section(symtab, layout);
bec53400
DK
5108 break;
5109
5110 case elfcpp::R_ARM_BASE_PREL:
5111 // FIXME: What about this?
5112 break;
5113
5114 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 5115 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
5116 {
5117 // The symbol requires a GOT entry.
5118 Output_data_got<32, big_endian>* got =
2ea97941 5119 target->got_section(symtab, layout);
bec53400
DK
5120 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
5121 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
5122 {
5123 // If we are generating a shared object, we need to add a
5124 // dynamic RELATIVE relocation for this symbol's GOT entry.
5125 if (parameters->options().output_is_position_independent())
5126 {
2ea97941
ILT
5127 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5128 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
bec53400 5129 rel_dyn->add_local_relative(
2ea97941
ILT
5130 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
5131 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
bec53400
DK
5132 }
5133 }
5134 }
5135 break;
5136
5137 case elfcpp::R_ARM_TARGET1:
5138 // This should have been mapped to another type already.
5139 // Fall through.
5140 case elfcpp::R_ARM_COPY:
5141 case elfcpp::R_ARM_GLOB_DAT:
5142 case elfcpp::R_ARM_JUMP_SLOT:
5143 case elfcpp::R_ARM_RELATIVE:
5144 // These are relocations which should only be seen by the
5145 // dynamic linker, and should never be seen here.
5146 gold_error(_("%s: unexpected reloc %u in object file"),
5147 object->name().c_str(), r_type);
5148 break;
5149
4a657b0d
DK
5150 default:
5151 unsupported_reloc_local(object, r_type);
5152 break;
5153 }
5154}
5155
5156// Report an unsupported relocation against a global symbol.
5157
5158template<bool big_endian>
5159void
5160Target_arm<big_endian>::Scan::unsupported_reloc_global(
5161 Sized_relobj<32, big_endian>* object,
5162 unsigned int r_type,
5163 Symbol* gsym)
5164{
5165 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5166 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5167}
5168
5169// Scan a relocation for a global symbol.
bec53400
DK
5170// FIXME: This only handles a subset of relocation types used by Android
5171// on ARM v5te devices.
4a657b0d
DK
5172
5173template<bool big_endian>
5174inline void
ad0f2072 5175Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
2ea97941 5176 Layout* layout,
bec53400 5177 Target_arm* target,
4a657b0d 5178 Sized_relobj<32, big_endian>* object,
bec53400
DK
5179 unsigned int data_shndx,
5180 Output_section* output_section,
5181 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
5182 unsigned int r_type,
5183 Symbol* gsym)
5184{
a6d1ef57 5185 r_type = get_real_reloc_type(r_type);
4a657b0d
DK
5186 switch (r_type)
5187 {
5188 case elfcpp::R_ARM_NONE:
5189 break;
5190
bec53400 5191 case elfcpp::R_ARM_ABS32:
be8fcb75 5192 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
5193 {
5194 // Make a dynamic relocation if necessary.
5195 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
5196 {
5197 if (target->may_need_copy_reloc(gsym))
5198 {
2ea97941 5199 target->copy_reloc(symtab, layout, object,
bec53400
DK
5200 data_shndx, output_section, gsym, reloc);
5201 }
5202 else if (gsym->can_use_relative_reloc(false))
5203 {
5204 // If we are to add more other reloc types than R_ARM_ABS32,
5205 // we need to add check_non_pic(object, r_type) here.
2ea97941 5206 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
bec53400
DK
5207 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
5208 output_section, object,
5209 data_shndx, reloc.get_r_offset());
5210 }
5211 else
5212 {
5213 // If we are to add more other reloc types than R_ARM_ABS32,
5214 // we need to add check_non_pic(object, r_type) here.
2ea97941 5215 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
bec53400
DK
5216 rel_dyn->add_global(gsym, r_type, output_section, object,
5217 data_shndx, reloc.get_r_offset());
5218 }
5219 }
5220 }
5221 break;
5222
fd3c5f0b
ILT
5223 case elfcpp::R_ARM_MOVW_ABS_NC:
5224 case elfcpp::R_ARM_MOVT_ABS:
5225 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5226 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
5227 case elfcpp::R_ARM_MOVW_PREL_NC:
5228 case elfcpp::R_ARM_MOVT_PREL:
5229 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5230 case elfcpp::R_ARM_THM_MOVT_PREL:
fd3c5f0b
ILT
5231 break;
5232
be8fcb75
ILT
5233 case elfcpp::R_ARM_THM_ABS5:
5234 case elfcpp::R_ARM_ABS8:
5235 case elfcpp::R_ARM_ABS12:
5236 case elfcpp::R_ARM_ABS16:
5237 case elfcpp::R_ARM_BASE_ABS:
5238 {
5239 // No dynamic relocs of this kinds.
5240 // Report the error in case of PIC.
5241 int flags = Symbol::NON_PIC_REF;
5242 if (gsym->type() == elfcpp::STT_FUNC
5243 || gsym->type() == elfcpp::STT_ARM_TFUNC)
5244 flags |= Symbol::FUNCTION_CALL;
5245 if (gsym->needs_dynamic_reloc(flags))
5246 check_non_pic(object, r_type);
5247 }
5248 break;
5249
bec53400
DK
5250 case elfcpp::R_ARM_REL32:
5251 case elfcpp::R_ARM_PREL31:
5252 {
5253 // Make a dynamic relocation if necessary.
5254 int flags = Symbol::NON_PIC_REF;
5255 if (gsym->needs_dynamic_reloc(flags))
5256 {
5257 if (target->may_need_copy_reloc(gsym))
5258 {
2ea97941 5259 target->copy_reloc(symtab, layout, object,
bec53400
DK
5260 data_shndx, output_section, gsym, reloc);
5261 }
5262 else
5263 {
5264 check_non_pic(object, r_type);
2ea97941 5265 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
bec53400
DK
5266 rel_dyn->add_global(gsym, r_type, output_section, object,
5267 data_shndx, reloc.get_r_offset());
5268 }
5269 }
5270 }
5271 break;
5272
5273 case elfcpp::R_ARM_JUMP24:
f4e5969c 5274 case elfcpp::R_ARM_THM_JUMP24:
bec53400 5275 case elfcpp::R_ARM_CALL:
f4e5969c
DK
5276 case elfcpp::R_ARM_THM_CALL:
5277
5278 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
2ea97941 5279 target->make_plt_entry(symtab, layout, gsym);
f4e5969c
DK
5280 else
5281 {
5282 // Check to see if this is a function that would need a PLT
5283 // but does not get one because the function symbol is untyped.
5284 // This happens in assembly code missing a proper .type directive.
5285 if ((!gsym->is_undefined() || parameters->options().shared())
5286 && !parameters->doing_static_link()
5287 && gsym->type() == elfcpp::STT_NOTYPE
5288 && (gsym->is_from_dynobj()
5289 || gsym->is_undefined()
5290 || gsym->is_preemptible()))
5291 gold_error(_("%s is not a function."),
5292 gsym->demangled_name().c_str());
5293 }
bec53400
DK
5294 break;
5295
5296 case elfcpp::R_ARM_PLT32:
5297 // If the symbol is fully resolved, this is just a relative
5298 // local reloc. Otherwise we need a PLT entry.
5299 if (gsym->final_value_is_known())
5300 break;
5301 // If building a shared library, we can also skip the PLT entry
5302 // if the symbol is defined in the output file and is protected
5303 // or hidden.
5304 if (gsym->is_defined()
5305 && !gsym->is_from_dynobj()
5306 && !gsym->is_preemptible())
5307 break;
2ea97941 5308 target->make_plt_entry(symtab, layout, gsym);
bec53400
DK
5309 break;
5310
5311 case elfcpp::R_ARM_GOTOFF32:
5312 // We need a GOT section.
2ea97941 5313 target->got_section(symtab, layout);
bec53400
DK
5314 break;
5315
5316 case elfcpp::R_ARM_BASE_PREL:
5317 // FIXME: What about this?
5318 break;
5319
5320 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 5321 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
5322 {
5323 // The symbol requires a GOT entry.
5324 Output_data_got<32, big_endian>* got =
2ea97941 5325 target->got_section(symtab, layout);
bec53400
DK
5326 if (gsym->final_value_is_known())
5327 got->add_global(gsym, GOT_TYPE_STANDARD);
5328 else
5329 {
5330 // If this symbol is not fully resolved, we need to add a
5331 // GOT entry with a dynamic relocation.
2ea97941 5332 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
bec53400
DK
5333 if (gsym->is_from_dynobj()
5334 || gsym->is_undefined()
5335 || gsym->is_preemptible())
5336 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
5337 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
5338 else
5339 {
5340 if (got->add_global(gsym, GOT_TYPE_STANDARD))
5341 rel_dyn->add_global_relative(
5342 gsym, elfcpp::R_ARM_RELATIVE, got,
5343 gsym->got_offset(GOT_TYPE_STANDARD));
5344 }
5345 }
5346 }
5347 break;
5348
5349 case elfcpp::R_ARM_TARGET1:
5350 // This should have been mapped to another type already.
5351 // Fall through.
5352 case elfcpp::R_ARM_COPY:
5353 case elfcpp::R_ARM_GLOB_DAT:
5354 case elfcpp::R_ARM_JUMP_SLOT:
5355 case elfcpp::R_ARM_RELATIVE:
5356 // These are relocations which should only be seen by the
5357 // dynamic linker, and should never be seen here.
5358 gold_error(_("%s: unexpected reloc %u in object file"),
5359 object->name().c_str(), r_type);
5360 break;
5361
4a657b0d
DK
5362 default:
5363 unsupported_reloc_global(object, r_type, gsym);
5364 break;
5365 }
5366}
5367
5368// Process relocations for gc.
5369
5370template<bool big_endian>
5371void
ad0f2072 5372Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
2ea97941 5373 Layout* layout,
4a657b0d
DK
5374 Sized_relobj<32, big_endian>* object,
5375 unsigned int data_shndx,
5376 unsigned int,
5377 const unsigned char* prelocs,
5378 size_t reloc_count,
5379 Output_section* output_section,
5380 bool needs_special_offset_handling,
5381 size_t local_symbol_count,
5382 const unsigned char* plocal_symbols)
5383{
5384 typedef Target_arm<big_endian> Arm;
2ea97941 5385 typedef typename Target_arm<big_endian>::Scan Scan;
4a657b0d 5386
2ea97941 5387 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
4a657b0d 5388 symtab,
2ea97941 5389 layout,
4a657b0d
DK
5390 this,
5391 object,
5392 data_shndx,
5393 prelocs,
5394 reloc_count,
5395 output_section,
5396 needs_special_offset_handling,
5397 local_symbol_count,
5398 plocal_symbols);
5399}
5400
5401// Scan relocations for a section.
5402
5403template<bool big_endian>
5404void
ad0f2072 5405Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
2ea97941 5406 Layout* layout,
4a657b0d
DK
5407 Sized_relobj<32, big_endian>* object,
5408 unsigned int data_shndx,
5409 unsigned int sh_type,
5410 const unsigned char* prelocs,
5411 size_t reloc_count,
5412 Output_section* output_section,
5413 bool needs_special_offset_handling,
5414 size_t local_symbol_count,
5415 const unsigned char* plocal_symbols)
5416{
2ea97941 5417 typedef typename Target_arm<big_endian>::Scan Scan;
4a657b0d
DK
5418 if (sh_type == elfcpp::SHT_RELA)
5419 {
5420 gold_error(_("%s: unsupported RELA reloc section"),
5421 object->name().c_str());
5422 return;
5423 }
5424
2ea97941 5425 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
4a657b0d 5426 symtab,
2ea97941 5427 layout,
4a657b0d
DK
5428 this,
5429 object,
5430 data_shndx,
5431 prelocs,
5432 reloc_count,
5433 output_section,
5434 needs_special_offset_handling,
5435 local_symbol_count,
5436 plocal_symbols);
5437}
5438
5439// Finalize the sections.
5440
5441template<bool big_endian>
5442void
d5b40221 5443Target_arm<big_endian>::do_finalize_sections(
2ea97941 5444 Layout* layout,
f59f41f3
DK
5445 const Input_objects* input_objects,
5446 Symbol_table* symtab)
4a657b0d 5447{
d5b40221
DK
5448 // Merge processor-specific flags.
5449 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
5450 p != input_objects->relobj_end();
5451 ++p)
5452 {
5453 Arm_relobj<big_endian>* arm_relobj =
5454 Arm_relobj<big_endian>::as_arm_relobj(*p);
5455 this->merge_processor_specific_flags(
5456 arm_relobj->name(),
5457 arm_relobj->processor_specific_flags());
a0351a69
DK
5458 this->merge_object_attributes(arm_relobj->name().c_str(),
5459 arm_relobj->attributes_section_data());
5460
d5b40221
DK
5461 }
5462
5463 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
5464 p != input_objects->dynobj_end();
5465 ++p)
5466 {
5467 Arm_dynobj<big_endian>* arm_dynobj =
5468 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
5469 this->merge_processor_specific_flags(
5470 arm_dynobj->name(),
5471 arm_dynobj->processor_specific_flags());
a0351a69
DK
5472 this->merge_object_attributes(arm_dynobj->name().c_str(),
5473 arm_dynobj->attributes_section_data());
d5b40221
DK
5474 }
5475
a0351a69
DK
5476 // Check BLX use.
5477 Object_attribute* attr =
5478 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
5479 if (attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
5480 this->set_may_use_blx(true);
5481
94cdfcff 5482 // Fill in some more dynamic tags.
ea715a34
ILT
5483 const Reloc_section* rel_plt = (this->plt_ == NULL
5484 ? NULL
5485 : this->plt_->rel_plt());
5486 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
5487 this->rel_dyn_, true);
94cdfcff
DK
5488
5489 // Emit any relocs we saved in an attempt to avoid generating COPY
5490 // relocs.
5491 if (this->copy_relocs_.any_saved_relocs())
2ea97941 5492 this->copy_relocs_.emit(this->rel_dyn_section(layout));
11af873f 5493
f59f41f3 5494 // Handle the .ARM.exidx section.
2ea97941 5495 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
f59f41f3
DK
5496 if (exidx_section != NULL
5497 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
11af873f
DK
5498 && !parameters->options().relocatable())
5499 {
f59f41f3 5500 // Create __exidx_start and __exdix_end symbols.
99fff23b
ILT
5501 symtab->define_in_output_data("__exidx_start", NULL,
5502 Symbol_table::PREDEFINED,
5503 exidx_section, 0, 0, elfcpp::STT_OBJECT,
a0351a69 5504 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
99e5bff2 5505 false, true);
99fff23b
ILT
5506 symtab->define_in_output_data("__exidx_end", NULL,
5507 Symbol_table::PREDEFINED,
5508 exidx_section, 0, 0, elfcpp::STT_OBJECT,
a0351a69 5509 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
99e5bff2 5510 true, true);
11af873f 5511
f59f41f3
DK
5512 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
5513 // the .ARM.exidx section.
2ea97941 5514 if (!layout->script_options()->saw_phdrs_clause())
11af873f 5515 {
2ea97941 5516 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
11af873f
DK
5517 == NULL);
5518 Output_segment* exidx_segment =
2ea97941 5519 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
f5c870d2
ILT
5520 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
5521 false);
11af873f
DK
5522 }
5523 }
a0351a69
DK
5524
5525 // Create an .ARM.attributes section if there is not one already.
2ea97941 5526 Output_attributes_section_data* attributes_section =
a0351a69 5527 new Output_attributes_section_data(*this->attributes_section_data_);
2ea97941
ILT
5528 layout->add_output_section_data(".ARM.attributes",
5529 elfcpp::SHT_ARM_ATTRIBUTES, 0,
1a2dff53
ILT
5530 attributes_section, false, false, false,
5531 false);
4a657b0d
DK
5532}
5533
bec53400
DK
5534// Return whether a direct absolute static relocation needs to be applied.
5535// In cases where Scan::local() or Scan::global() has created
5536// a dynamic relocation other than R_ARM_RELATIVE, the addend
5537// of the relocation is carried in the data, and we must not
5538// apply the static relocation.
5539
5540template<bool big_endian>
5541inline bool
5542Target_arm<big_endian>::Relocate::should_apply_static_reloc(
5543 const Sized_symbol<32>* gsym,
5544 int ref_flags,
5545 bool is_32bit,
5546 Output_section* output_section)
5547{
5548 // If the output section is not allocated, then we didn't call
5549 // scan_relocs, we didn't create a dynamic reloc, and we must apply
5550 // the reloc here.
5551 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
5552 return true;
5553
5554 // For local symbols, we will have created a non-RELATIVE dynamic
5555 // relocation only if (a) the output is position independent,
5556 // (b) the relocation is absolute (not pc- or segment-relative), and
5557 // (c) the relocation is not 32 bits wide.
5558 if (gsym == NULL)
5559 return !(parameters->options().output_is_position_independent()
5560 && (ref_flags & Symbol::ABSOLUTE_REF)
5561 && !is_32bit);
5562
5563 // For global symbols, we use the same helper routines used in the
5564 // scan pass. If we did not create a dynamic relocation, or if we
5565 // created a RELATIVE dynamic relocation, we should apply the static
5566 // relocation.
5567 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
5568 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
5569 && gsym->can_use_relative_reloc(ref_flags
5570 & Symbol::FUNCTION_CALL);
5571 return !has_dyn || is_rel;
5572}
5573
4a657b0d
DK
5574// Perform a relocation.
5575
5576template<bool big_endian>
5577inline bool
5578Target_arm<big_endian>::Relocate::relocate(
c121c671
DK
5579 const Relocate_info<32, big_endian>* relinfo,
5580 Target_arm* target,
5581 Output_section *output_section,
5582 size_t relnum,
5583 const elfcpp::Rel<32, big_endian>& rel,
4a657b0d 5584 unsigned int r_type,
c121c671
DK
5585 const Sized_symbol<32>* gsym,
5586 const Symbol_value<32>* psymval,
5587 unsigned char* view,
ebabffbd 5588 Arm_address address,
4a657b0d
DK
5589 section_size_type /* view_size */ )
5590{
c121c671
DK
5591 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
5592
a6d1ef57 5593 r_type = get_real_reloc_type(r_type);
c121c671 5594
2daedcd6
DK
5595 const Arm_relobj<big_endian>* object =
5596 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
c121c671 5597
2daedcd6
DK
5598 // If the final branch target of a relocation is THUMB instruction, this
5599 // is 1. Otherwise it is 0.
5600 Arm_address thumb_bit = 0;
c121c671 5601 Symbol_value<32> symval;
d204b6e9 5602 bool is_weakly_undefined_without_plt = false;
2daedcd6 5603 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
c121c671 5604 {
2daedcd6
DK
5605 if (gsym != NULL)
5606 {
5607 // This is a global symbol. Determine if we use PLT and if the
5608 // final target is THUMB.
5609 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
5610 {
5611 // This uses a PLT, change the symbol value.
5612 symval.set_output_value(target->plt_section()->address()
5613 + gsym->plt_offset());
5614 psymval = &symval;
5615 }
d204b6e9
DK
5616 else if (gsym->is_weak_undefined())
5617 {
5618 // This is a weakly undefined symbol and we do not use PLT
5619 // for this relocation. A branch targeting this symbol will
5620 // be converted into an NOP.
5621 is_weakly_undefined_without_plt = true;
5622 }
2daedcd6
DK
5623 else
5624 {
5625 // Set thumb bit if symbol:
5626 // -Has type STT_ARM_TFUNC or
5627 // -Has type STT_FUNC, is defined and with LSB in value set.
5628 thumb_bit =
5629 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
5630 || (gsym->type() == elfcpp::STT_FUNC
5631 && !gsym->is_undefined()
5632 && ((psymval->value(object, 0) & 1) != 0)))
5633 ? 1
5634 : 0);
5635 }
5636 }
5637 else
5638 {
5639 // This is a local symbol. Determine if the final target is THUMB.
5640 // We saved this information when all the local symbols were read.
5641 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
5642 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
5643 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
5644 }
5645 }
5646 else
5647 {
5648 // This is a fake relocation synthesized for a stub. It does not have
5649 // a real symbol. We just look at the LSB of the symbol value to
5650 // determine if the target is THUMB or not.
5651 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
c121c671
DK
5652 }
5653
2daedcd6
DK
5654 // Strip LSB if this points to a THUMB target.
5655 if (thumb_bit != 0
5656 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
5657 && ((psymval->value(object, 0) & 1) != 0))
5658 {
5659 Arm_address stripped_value =
5660 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
5661 symval.set_output_value(stripped_value);
5662 psymval = &symval;
5663 }
5664
c121c671
DK
5665 // Get the GOT offset if needed.
5666 // The GOT pointer points to the end of the GOT section.
5667 // We need to subtract the size of the GOT section to get
5668 // the actual offset to use in the relocation.
5669 bool have_got_offset = false;
5670 unsigned int got_offset = 0;
5671 switch (r_type)
5672 {
5673 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 5674 case elfcpp::R_ARM_GOT_PREL:
c121c671
DK
5675 if (gsym != NULL)
5676 {
5677 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
5678 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
5679 - target->got_size());
5680 }
5681 else
5682 {
5683 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5684 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
5685 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
5686 - target->got_size());
5687 }
5688 have_got_offset = true;
5689 break;
5690
5691 default:
5692 break;
5693 }
5694
d204b6e9
DK
5695 // To look up relocation stubs, we need to pass the symbol table index of
5696 // a local symbol.
5697 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5698
c121c671
DK
5699 typename Arm_relocate_functions::Status reloc_status =
5700 Arm_relocate_functions::STATUS_OKAY;
4a657b0d
DK
5701 switch (r_type)
5702 {
5703 case elfcpp::R_ARM_NONE:
5704 break;
5705
5e445df6
ILT
5706 case elfcpp::R_ARM_ABS8:
5707 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5708 output_section))
be8fcb75
ILT
5709 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
5710 break;
5711
5712 case elfcpp::R_ARM_ABS12:
5713 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5714 output_section))
5715 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
5716 break;
5717
5718 case elfcpp::R_ARM_ABS16:
5719 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5720 output_section))
5721 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5e445df6
ILT
5722 break;
5723
c121c671
DK
5724 case elfcpp::R_ARM_ABS32:
5725 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5726 output_section))
5727 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
2daedcd6 5728 thumb_bit);
c121c671
DK
5729 break;
5730
be8fcb75
ILT
5731 case elfcpp::R_ARM_ABS32_NOI:
5732 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5733 output_section))
5734 // No thumb bit for this relocation: (S + A)
5735 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
f4e5969c 5736 0);
be8fcb75
ILT
5737 break;
5738
fd3c5f0b
ILT
5739 case elfcpp::R_ARM_MOVW_ABS_NC:
5740 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5741 output_section))
5742 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
5743 psymval,
2daedcd6 5744 thumb_bit);
fd3c5f0b
ILT
5745 else
5746 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
5747 "a shared object; recompile with -fPIC"));
5748 break;
5749
5750 case elfcpp::R_ARM_MOVT_ABS:
5751 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5752 output_section))
5753 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
5754 else
5755 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
5756 "a shared object; recompile with -fPIC"));
5757 break;
5758
5759 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5760 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5761 output_section))
5762 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
5763 psymval,
2daedcd6 5764 thumb_bit);
fd3c5f0b
ILT
5765 else
5766 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
5767 "making a shared object; recompile with -fPIC"));
5768 break;
5769
5770 case elfcpp::R_ARM_THM_MOVT_ABS:
5771 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5772 output_section))
5773 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
5774 psymval);
5775 else
5776 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
5777 "making a shared object; recompile with -fPIC"));
5778 break;
5779
c2a122b6
ILT
5780 case elfcpp::R_ARM_MOVW_PREL_NC:
5781 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
5782 psymval, address,
2daedcd6 5783 thumb_bit);
c2a122b6
ILT
5784 break;
5785
5786 case elfcpp::R_ARM_MOVT_PREL:
5787 reloc_status = Arm_relocate_functions::movt_prel(view, object,
5788 psymval, address);
5789 break;
5790
5791 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5792 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
5793 psymval, address,
2daedcd6 5794 thumb_bit);
c2a122b6
ILT
5795 break;
5796
5797 case elfcpp::R_ARM_THM_MOVT_PREL:
5798 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
5799 psymval, address);
5800 break;
5801
c121c671
DK
5802 case elfcpp::R_ARM_REL32:
5803 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2daedcd6 5804 address, thumb_bit);
c121c671
DK
5805 break;
5806
be8fcb75
ILT
5807 case elfcpp::R_ARM_THM_ABS5:
5808 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5809 output_section))
5810 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
5811 break;
5812
c121c671 5813 case elfcpp::R_ARM_THM_CALL:
51938283
DK
5814 reloc_status =
5815 Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym,
5816 psymval, address, thumb_bit,
5817 is_weakly_undefined_without_plt);
c121c671
DK
5818 break;
5819
d204b6e9
DK
5820 case elfcpp::R_ARM_XPC25:
5821 reloc_status =
5822 Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym,
5823 psymval, address, thumb_bit,
5824 is_weakly_undefined_without_plt);
5825 break;
5826
51938283
DK
5827 case elfcpp::R_ARM_THM_XPC22:
5828 reloc_status =
5829 Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym,
5830 psymval, address, thumb_bit,
5831 is_weakly_undefined_without_plt);
5832 break;
5833
c121c671
DK
5834 case elfcpp::R_ARM_GOTOFF32:
5835 {
ebabffbd 5836 Arm_address got_origin;
c121c671
DK
5837 got_origin = target->got_plt_section()->address();
5838 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2daedcd6 5839 got_origin, thumb_bit);
c121c671
DK
5840 }
5841 break;
5842
5843 case elfcpp::R_ARM_BASE_PREL:
5844 {
5845 uint32_t origin;
5846 // Get the addressing origin of the output segment defining the
5847 // symbol gsym (AAELF 4.6.1.2 Relocation types)
5848 gold_assert(gsym != NULL);
5849 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5850 origin = gsym->output_segment()->vaddr();
5851 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5852 origin = gsym->output_data()->address();
5853 else
5854 {
5855 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5856 _("cannot find origin of R_ARM_BASE_PREL"));
5857 return true;
5858 }
5859 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
5860 }
5861 break;
5862
be8fcb75
ILT
5863 case elfcpp::R_ARM_BASE_ABS:
5864 {
5865 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5866 output_section))
5867 break;
5868
5869 uint32_t origin;
5870 // Get the addressing origin of the output segment defining
5871 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5872 if (gsym == NULL)
5873 // R_ARM_BASE_ABS with the NULL symbol will give the
5874 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5875 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5876 origin = target->got_plt_section()->address();
5877 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5878 origin = gsym->output_segment()->vaddr();
5879 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5880 origin = gsym->output_data()->address();
5881 else
5882 {
5883 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5884 _("cannot find origin of R_ARM_BASE_ABS"));
5885 return true;
5886 }
5887
5888 reloc_status = Arm_relocate_functions::base_abs(view, origin);
5889 }
5890 break;
5891
c121c671
DK
5892 case elfcpp::R_ARM_GOT_BREL:
5893 gold_assert(have_got_offset);
5894 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
5895 break;
5896
7f5309a5
ILT
5897 case elfcpp::R_ARM_GOT_PREL:
5898 gold_assert(have_got_offset);
5899 // Get the address origin for GOT PLT, which is allocated right
5900 // after the GOT section, to calculate an absolute address of
5901 // the symbol GOT entry (got_origin + got_offset).
ebabffbd 5902 Arm_address got_origin;
7f5309a5
ILT
5903 got_origin = target->got_plt_section()->address();
5904 reloc_status = Arm_relocate_functions::got_prel(view,
5905 got_origin + got_offset,
5906 address);
5907 break;
5908
c121c671
DK
5909 case elfcpp::R_ARM_PLT32:
5910 gold_assert(gsym == NULL
5911 || gsym->has_plt_offset()
5912 || gsym->final_value_is_known()
5913 || (gsym->is_defined()
5914 && !gsym->is_from_dynobj()
5915 && !gsym->is_preemptible()));
d204b6e9
DK
5916 reloc_status =
5917 Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym,
5918 psymval, address, thumb_bit,
5919 is_weakly_undefined_without_plt);
c121c671
DK
5920 break;
5921
5922 case elfcpp::R_ARM_CALL:
d204b6e9
DK
5923 reloc_status =
5924 Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym,
5925 psymval, address, thumb_bit,
5926 is_weakly_undefined_without_plt);
c121c671
DK
5927 break;
5928
5929 case elfcpp::R_ARM_JUMP24:
d204b6e9
DK
5930 reloc_status =
5931 Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym,
5932 psymval, address, thumb_bit,
5933 is_weakly_undefined_without_plt);
c121c671
DK
5934 break;
5935
51938283
DK
5936 case elfcpp::R_ARM_THM_JUMP24:
5937 reloc_status =
5938 Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym,
5939 psymval, address, thumb_bit,
5940 is_weakly_undefined_without_plt);
5941 break;
5942
c121c671
DK
5943 case elfcpp::R_ARM_PREL31:
5944 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
2daedcd6 5945 address, thumb_bit);
c121c671
DK
5946 break;
5947
5948 case elfcpp::R_ARM_TARGET1:
5949 // This should have been mapped to another type already.
5950 // Fall through.
5951 case elfcpp::R_ARM_COPY:
5952 case elfcpp::R_ARM_GLOB_DAT:
5953 case elfcpp::R_ARM_JUMP_SLOT:
5954 case elfcpp::R_ARM_RELATIVE:
5955 // These are relocations which should only be seen by the
5956 // dynamic linker, and should never be seen here.
5957 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5958 _("unexpected reloc %u in object file"),
5959 r_type);
5960 break;
5961
5962 default:
5963 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5964 _("unsupported reloc %u"),
5965 r_type);
5966 break;
5967 }
5968
5969 // Report any errors.
5970 switch (reloc_status)
5971 {
5972 case Arm_relocate_functions::STATUS_OKAY:
5973 break;
5974 case Arm_relocate_functions::STATUS_OVERFLOW:
5975 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5976 _("relocation overflow in relocation %u"),
5977 r_type);
5978 break;
5979 case Arm_relocate_functions::STATUS_BAD_RELOC:
5980 gold_error_at_location(
5981 relinfo,
5982 relnum,
5983 rel.get_r_offset(),
5984 _("unexpected opcode while processing relocation %u"),
5985 r_type);
5986 break;
4a657b0d
DK
5987 default:
5988 gold_unreachable();
5989 }
5990
5991 return true;
5992}
5993
5994// Relocate section data.
5995
5996template<bool big_endian>
5997void
5998Target_arm<big_endian>::relocate_section(
5999 const Relocate_info<32, big_endian>* relinfo,
6000 unsigned int sh_type,
6001 const unsigned char* prelocs,
6002 size_t reloc_count,
6003 Output_section* output_section,
6004 bool needs_special_offset_handling,
6005 unsigned char* view,
ebabffbd 6006 Arm_address address,
364c7fa5
ILT
6007 section_size_type view_size,
6008 const Reloc_symbol_changes* reloc_symbol_changes)
4a657b0d
DK
6009{
6010 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
6011 gold_assert(sh_type == elfcpp::SHT_REL);
6012
43d12afe
DK
6013 Arm_input_section<big_endian>* arm_input_section =
6014 this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
6015
6016 // This is an ARM input section and the view covers the whole output
6017 // section.
6018 if (arm_input_section != NULL)
6019 {
6020 gold_assert(needs_special_offset_handling);
6021 Arm_address section_address = arm_input_section->address();
6022 section_size_type section_size = arm_input_section->data_size();
6023
6024 gold_assert((arm_input_section->address() >= address)
6025 && ((arm_input_section->address()
6026 + arm_input_section->data_size())
6027 <= (address + view_size)));
6028
2ea97941
ILT
6029 off_t offset = section_address - address;
6030 view += offset;
6031 address += offset;
43d12afe
DK
6032 view_size = section_size;
6033 }
6034
4a657b0d
DK
6035 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
6036 Arm_relocate>(
6037 relinfo,
6038 this,
6039 prelocs,
6040 reloc_count,
6041 output_section,
6042 needs_special_offset_handling,
6043 view,
6044 address,
364c7fa5
ILT
6045 view_size,
6046 reloc_symbol_changes);
4a657b0d
DK
6047}
6048
6049// Return the size of a relocation while scanning during a relocatable
6050// link.
6051
6052template<bool big_endian>
6053unsigned int
6054Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
6055 unsigned int r_type,
6056 Relobj* object)
6057{
a6d1ef57 6058 r_type = get_real_reloc_type(r_type);
4a657b0d
DK
6059 switch (r_type)
6060 {
6061 case elfcpp::R_ARM_NONE:
6062 return 0;
6063
5e445df6
ILT
6064 case elfcpp::R_ARM_ABS8:
6065 return 1;
6066
be8fcb75
ILT
6067 case elfcpp::R_ARM_ABS16:
6068 case elfcpp::R_ARM_THM_ABS5:
6069 return 2;
6070
4a657b0d 6071 case elfcpp::R_ARM_ABS32:
be8fcb75
ILT
6072 case elfcpp::R_ARM_ABS32_NOI:
6073 case elfcpp::R_ARM_ABS12:
6074 case elfcpp::R_ARM_BASE_ABS:
4a657b0d
DK
6075 case elfcpp::R_ARM_REL32:
6076 case elfcpp::R_ARM_THM_CALL:
6077 case elfcpp::R_ARM_GOTOFF32:
6078 case elfcpp::R_ARM_BASE_PREL:
6079 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 6080 case elfcpp::R_ARM_GOT_PREL:
4a657b0d
DK
6081 case elfcpp::R_ARM_PLT32:
6082 case elfcpp::R_ARM_CALL:
6083 case elfcpp::R_ARM_JUMP24:
6084 case elfcpp::R_ARM_PREL31:
fd3c5f0b
ILT
6085 case elfcpp::R_ARM_MOVW_ABS_NC:
6086 case elfcpp::R_ARM_MOVT_ABS:
6087 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6088 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
6089 case elfcpp::R_ARM_MOVW_PREL_NC:
6090 case elfcpp::R_ARM_MOVT_PREL:
6091 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6092 case elfcpp::R_ARM_THM_MOVT_PREL:
4a657b0d
DK
6093 return 4;
6094
6095 case elfcpp::R_ARM_TARGET1:
6096 // This should have been mapped to another type already.
6097 // Fall through.
6098 case elfcpp::R_ARM_COPY:
6099 case elfcpp::R_ARM_GLOB_DAT:
6100 case elfcpp::R_ARM_JUMP_SLOT:
6101 case elfcpp::R_ARM_RELATIVE:
6102 // These are relocations which should only be seen by the
6103 // dynamic linker, and should never be seen here.
6104 gold_error(_("%s: unexpected reloc %u in object file"),
6105 object->name().c_str(), r_type);
6106 return 0;
6107
6108 default:
6109 object->error(_("unsupported reloc %u in object file"), r_type);
6110 return 0;
6111 }
6112}
6113
6114// Scan the relocs during a relocatable link.
6115
6116template<bool big_endian>
6117void
6118Target_arm<big_endian>::scan_relocatable_relocs(
4a657b0d 6119 Symbol_table* symtab,
2ea97941 6120 Layout* layout,
4a657b0d
DK
6121 Sized_relobj<32, big_endian>* object,
6122 unsigned int data_shndx,
6123 unsigned int sh_type,
6124 const unsigned char* prelocs,
6125 size_t reloc_count,
6126 Output_section* output_section,
6127 bool needs_special_offset_handling,
6128 size_t local_symbol_count,
6129 const unsigned char* plocal_symbols,
6130 Relocatable_relocs* rr)
6131{
6132 gold_assert(sh_type == elfcpp::SHT_REL);
6133
6134 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
6135 Relocatable_size_for_reloc> Scan_relocatable_relocs;
6136
6137 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
6138 Scan_relocatable_relocs>(
4a657b0d 6139 symtab,
2ea97941 6140 layout,
4a657b0d
DK
6141 object,
6142 data_shndx,
6143 prelocs,
6144 reloc_count,
6145 output_section,
6146 needs_special_offset_handling,
6147 local_symbol_count,
6148 plocal_symbols,
6149 rr);
6150}
6151
6152// Relocate a section during a relocatable link.
6153
6154template<bool big_endian>
6155void
6156Target_arm<big_endian>::relocate_for_relocatable(
6157 const Relocate_info<32, big_endian>* relinfo,
6158 unsigned int sh_type,
6159 const unsigned char* prelocs,
6160 size_t reloc_count,
6161 Output_section* output_section,
6162 off_t offset_in_output_section,
6163 const Relocatable_relocs* rr,
6164 unsigned char* view,
ebabffbd 6165 Arm_address view_address,
4a657b0d
DK
6166 section_size_type view_size,
6167 unsigned char* reloc_view,
6168 section_size_type reloc_view_size)
6169{
6170 gold_assert(sh_type == elfcpp::SHT_REL);
6171
6172 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
6173 relinfo,
6174 prelocs,
6175 reloc_count,
6176 output_section,
6177 offset_in_output_section,
6178 rr,
6179 view,
6180 view_address,
6181 view_size,
6182 reloc_view,
6183 reloc_view_size);
6184}
6185
94cdfcff
DK
6186// Return the value to use for a dynamic symbol which requires special
6187// treatment. This is how we support equality comparisons of function
6188// pointers across shared library boundaries, as described in the
6189// processor specific ABI supplement.
6190
4a657b0d
DK
6191template<bool big_endian>
6192uint64_t
94cdfcff 6193Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4a657b0d 6194{
94cdfcff
DK
6195 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
6196 return this->plt_section()->address() + gsym->plt_offset();
4a657b0d
DK
6197}
6198
6199// Map platform-specific relocs to real relocs
6200//
6201template<bool big_endian>
6202unsigned int
a6d1ef57 6203Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
4a657b0d
DK
6204{
6205 switch (r_type)
6206 {
6207 case elfcpp::R_ARM_TARGET1:
a6d1ef57
DK
6208 // This is either R_ARM_ABS32 or R_ARM_REL32;
6209 return elfcpp::R_ARM_ABS32;
4a657b0d
DK
6210
6211 case elfcpp::R_ARM_TARGET2:
a6d1ef57
DK
6212 // This can be any reloc type but ususally is R_ARM_GOT_PREL
6213 return elfcpp::R_ARM_GOT_PREL;
4a657b0d
DK
6214
6215 default:
6216 return r_type;
6217 }
6218}
6219
d5b40221
DK
6220// Whether if two EABI versions V1 and V2 are compatible.
6221
6222template<bool big_endian>
6223bool
6224Target_arm<big_endian>::are_eabi_versions_compatible(
6225 elfcpp::Elf_Word v1,
6226 elfcpp::Elf_Word v2)
6227{
6228 // v4 and v5 are the same spec before and after it was released,
6229 // so allow mixing them.
6230 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
6231 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
6232 return true;
6233
6234 return v1 == v2;
6235}
6236
6237// Combine FLAGS from an input object called NAME and the processor-specific
6238// flags in the ELF header of the output. Much of this is adapted from the
6239// processor-specific flags merging code in elf32_arm_merge_private_bfd_data
6240// in bfd/elf32-arm.c.
6241
6242template<bool big_endian>
6243void
6244Target_arm<big_endian>::merge_processor_specific_flags(
6245 const std::string& name,
6246 elfcpp::Elf_Word flags)
6247{
6248 if (this->are_processor_specific_flags_set())
6249 {
6250 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
6251
6252 // Nothing to merge if flags equal to those in output.
6253 if (flags == out_flags)
6254 return;
6255
6256 // Complain about various flag mismatches.
6257 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
6258 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
6259 if (!this->are_eabi_versions_compatible(version1, version2))
6260 gold_error(_("Source object %s has EABI version %d but output has "
6261 "EABI version %d."),
6262 name.c_str(),
6263 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
6264 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
6265 }
6266 else
6267 {
6268 // If the input is the default architecture and had the default
6269 // flags then do not bother setting the flags for the output
6270 // architecture, instead allow future merges to do this. If no
6271 // future merges ever set these flags then they will retain their
6272 // uninitialised values, which surprise surprise, correspond
6273 // to the default values.
6274 if (flags == 0)
6275 return;
6276
6277 // This is the first time, just copy the flags.
6278 // We only copy the EABI version for now.
6279 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
6280 }
6281}
6282
6283// Adjust ELF file header.
6284template<bool big_endian>
6285void
6286Target_arm<big_endian>::do_adjust_elf_header(
6287 unsigned char* view,
6288 int len) const
6289{
6290 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
6291
6292 elfcpp::Ehdr<32, big_endian> ehdr(view);
6293 unsigned char e_ident[elfcpp::EI_NIDENT];
6294 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
6295
6296 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
6297 == elfcpp::EF_ARM_EABI_UNKNOWN)
6298 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
6299 else
6300 e_ident[elfcpp::EI_OSABI] = 0;
6301 e_ident[elfcpp::EI_ABIVERSION] = 0;
6302
6303 // FIXME: Do EF_ARM_BE8 adjustment.
6304
6305 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
6306 oehdr.put_e_ident(e_ident);
6307}
6308
6309// do_make_elf_object to override the same function in the base class.
6310// We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
6311// to store ARM specific information. Hence we need to have our own
6312// ELF object creation.
6313
6314template<bool big_endian>
6315Object*
6316Target_arm<big_endian>::do_make_elf_object(
6317 const std::string& name,
6318 Input_file* input_file,
2ea97941 6319 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
d5b40221
DK
6320{
6321 int et = ehdr.get_e_type();
6322 if (et == elfcpp::ET_REL)
6323 {
6324 Arm_relobj<big_endian>* obj =
2ea97941 6325 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
d5b40221
DK
6326 obj->setup();
6327 return obj;
6328 }
6329 else if (et == elfcpp::ET_DYN)
6330 {
6331 Sized_dynobj<32, big_endian>* obj =
2ea97941 6332 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
d5b40221
DK
6333 obj->setup();
6334 return obj;
6335 }
6336 else
6337 {
6338 gold_error(_("%s: unsupported ELF file type %d"),
6339 name.c_str(), et);
6340 return NULL;
6341 }
6342}
6343
a0351a69
DK
6344// Read the architecture from the Tag_also_compatible_with attribute, if any.
6345// Returns -1 if no architecture could be read.
6346// This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
6347
6348template<bool big_endian>
6349int
6350Target_arm<big_endian>::get_secondary_compatible_arch(
6351 const Attributes_section_data* pasd)
6352{
6353 const Object_attribute *known_attributes =
6354 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
6355
6356 // Note: the tag and its argument below are uleb128 values, though
6357 // currently-defined values fit in one byte for each.
6358 const std::string& sv =
6359 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
6360 if (sv.size() == 2
6361 && sv.data()[0] == elfcpp::Tag_CPU_arch
6362 && (sv.data()[1] & 128) != 128)
6363 return sv.data()[1];
6364
6365 // This tag is "safely ignorable", so don't complain if it looks funny.
6366 return -1;
6367}
6368
6369// Set, or unset, the architecture of the Tag_also_compatible_with attribute.
6370// The tag is removed if ARCH is -1.
6371// This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
6372
6373template<bool big_endian>
6374void
6375Target_arm<big_endian>::set_secondary_compatible_arch(
6376 Attributes_section_data* pasd,
6377 int arch)
6378{
6379 Object_attribute *known_attributes =
6380 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
6381
6382 if (arch == -1)
6383 {
6384 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
6385 return;
6386 }
6387
6388 // Note: the tag and its argument below are uleb128 values, though
6389 // currently-defined values fit in one byte for each.
6390 char sv[3];
6391 sv[0] = elfcpp::Tag_CPU_arch;
6392 gold_assert(arch != 0);
6393 sv[1] = arch;
6394 sv[2] = '\0';
6395
6396 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
6397}
6398
6399// Combine two values for Tag_CPU_arch, taking secondary compatibility tags
6400// into account.
6401// This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
6402
6403template<bool big_endian>
6404int
6405Target_arm<big_endian>::tag_cpu_arch_combine(
6406 const char* name,
6407 int oldtag,
6408 int* secondary_compat_out,
6409 int newtag,
6410 int secondary_compat)
6411{
6412#define T(X) elfcpp::TAG_CPU_ARCH_##X
6413 static const int v6t2[] =
6414 {
6415 T(V6T2), // PRE_V4.
6416 T(V6T2), // V4.
6417 T(V6T2), // V4T.
6418 T(V6T2), // V5T.
6419 T(V6T2), // V5TE.
6420 T(V6T2), // V5TEJ.
6421 T(V6T2), // V6.
6422 T(V7), // V6KZ.
6423 T(V6T2) // V6T2.
6424 };
6425 static const int v6k[] =
6426 {
6427 T(V6K), // PRE_V4.
6428 T(V6K), // V4.
6429 T(V6K), // V4T.
6430 T(V6K), // V5T.
6431 T(V6K), // V5TE.
6432 T(V6K), // V5TEJ.
6433 T(V6K), // V6.
6434 T(V6KZ), // V6KZ.
6435 T(V7), // V6T2.
6436 T(V6K) // V6K.
6437 };
6438 static const int v7[] =
6439 {
6440 T(V7), // PRE_V4.
6441 T(V7), // V4.
6442 T(V7), // V4T.
6443 T(V7), // V5T.
6444 T(V7), // V5TE.
6445 T(V7), // V5TEJ.
6446 T(V7), // V6.
6447 T(V7), // V6KZ.
6448 T(V7), // V6T2.
6449 T(V7), // V6K.
6450 T(V7) // V7.
6451 };
6452 static const int v6_m[] =
6453 {
6454 -1, // PRE_V4.
6455 -1, // V4.
6456 T(V6K), // V4T.
6457 T(V6K), // V5T.
6458 T(V6K), // V5TE.
6459 T(V6K), // V5TEJ.
6460 T(V6K), // V6.
6461 T(V6KZ), // V6KZ.
6462 T(V7), // V6T2.
6463 T(V6K), // V6K.
6464 T(V7), // V7.
6465 T(V6_M) // V6_M.
6466 };
6467 static const int v6s_m[] =
6468 {
6469 -1, // PRE_V4.
6470 -1, // V4.
6471 T(V6K), // V4T.
6472 T(V6K), // V5T.
6473 T(V6K), // V5TE.
6474 T(V6K), // V5TEJ.
6475 T(V6K), // V6.
6476 T(V6KZ), // V6KZ.
6477 T(V7), // V6T2.
6478 T(V6K), // V6K.
6479 T(V7), // V7.
6480 T(V6S_M), // V6_M.
6481 T(V6S_M) // V6S_M.
6482 };
6483 static const int v7e_m[] =
6484 {
6485 -1, // PRE_V4.
6486 -1, // V4.
6487 T(V7E_M), // V4T.
6488 T(V7E_M), // V5T.
6489 T(V7E_M), // V5TE.
6490 T(V7E_M), // V5TEJ.
6491 T(V7E_M), // V6.
6492 T(V7E_M), // V6KZ.
6493 T(V7E_M), // V6T2.
6494 T(V7E_M), // V6K.
6495 T(V7E_M), // V7.
6496 T(V7E_M), // V6_M.
6497 T(V7E_M), // V6S_M.
6498 T(V7E_M) // V7E_M.
6499 };
6500 static const int v4t_plus_v6_m[] =
6501 {
6502 -1, // PRE_V4.
6503 -1, // V4.
6504 T(V4T), // V4T.
6505 T(V5T), // V5T.
6506 T(V5TE), // V5TE.
6507 T(V5TEJ), // V5TEJ.
6508 T(V6), // V6.
6509 T(V6KZ), // V6KZ.
6510 T(V6T2), // V6T2.
6511 T(V6K), // V6K.
6512 T(V7), // V7.
6513 T(V6_M), // V6_M.
6514 T(V6S_M), // V6S_M.
6515 T(V7E_M), // V7E_M.
6516 T(V4T_PLUS_V6_M) // V4T plus V6_M.
6517 };
6518 static const int *comb[] =
6519 {
6520 v6t2,
6521 v6k,
6522 v7,
6523 v6_m,
6524 v6s_m,
6525 v7e_m,
6526 // Pseudo-architecture.
6527 v4t_plus_v6_m
6528 };
6529
6530 // Check we've not got a higher architecture than we know about.
6531
6532 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
6533 {
6534 gold_error(_("%s: unknown CPU architecture"), name);
6535 return -1;
6536 }
6537
6538 // Override old tag if we have a Tag_also_compatible_with on the output.
6539
6540 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
6541 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
6542 oldtag = T(V4T_PLUS_V6_M);
6543
6544 // And override the new tag if we have a Tag_also_compatible_with on the
6545 // input.
6546
6547 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
6548 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
6549 newtag = T(V4T_PLUS_V6_M);
6550
6551 // Architectures before V6KZ add features monotonically.
6552 int tagh = std::max(oldtag, newtag);
6553 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
6554 return tagh;
6555
6556 int tagl = std::min(oldtag, newtag);
6557 int result = comb[tagh - T(V6T2)][tagl];
6558
6559 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
6560 // as the canonical version.
6561 if (result == T(V4T_PLUS_V6_M))
6562 {
6563 result = T(V4T);
6564 *secondary_compat_out = T(V6_M);
6565 }
6566 else
6567 *secondary_compat_out = -1;
6568
6569 if (result == -1)
6570 {
6571 gold_error(_("%s: conflicting CPU architectures %d/%d"),
6572 name, oldtag, newtag);
6573 return -1;
6574 }
6575
6576 return result;
6577#undef T
6578}
6579
6580// Helper to print AEABI enum tag value.
6581
6582template<bool big_endian>
6583std::string
6584Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
6585{
6586 static const char *aeabi_enum_names[] =
6587 { "", "variable-size", "32-bit", "" };
6588 const size_t aeabi_enum_names_size =
6589 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
6590
6591 if (value < aeabi_enum_names_size)
6592 return std::string(aeabi_enum_names[value]);
6593 else
6594 {
6595 char buffer[100];
6596 sprintf(buffer, "<unknown value %u>", value);
6597 return std::string(buffer);
6598 }
6599}
6600
6601// Return the string value to store in TAG_CPU_name.
6602
6603template<bool big_endian>
6604std::string
6605Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
6606{
6607 static const char *name_table[] = {
6608 // These aren't real CPU names, but we can't guess
6609 // that from the architecture version alone.
6610 "Pre v4",
6611 "ARM v4",
6612 "ARM v4T",
6613 "ARM v5T",
6614 "ARM v5TE",
6615 "ARM v5TEJ",
6616 "ARM v6",
6617 "ARM v6KZ",
6618 "ARM v6T2",
6619 "ARM v6K",
6620 "ARM v7",
6621 "ARM v6-M",
6622 "ARM v6S-M",
6623 "ARM v7E-M"
6624 };
6625 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
6626
6627 if (value < name_table_size)
6628 return std::string(name_table[value]);
6629 else
6630 {
6631 char buffer[100];
6632 sprintf(buffer, "<unknown CPU value %u>", value);
6633 return std::string(buffer);
6634 }
6635}
6636
6637// Merge object attributes from input file called NAME with those of the
6638// output. The input object attributes are in the object pointed by PASD.
6639
6640template<bool big_endian>
6641void
6642Target_arm<big_endian>::merge_object_attributes(
6643 const char* name,
6644 const Attributes_section_data* pasd)
6645{
6646 // Return if there is no attributes section data.
6647 if (pasd == NULL)
6648 return;
6649
6650 // If output has no object attributes, just copy.
6651 if (this->attributes_section_data_ == NULL)
6652 {
6653 this->attributes_section_data_ = new Attributes_section_data(*pasd);
6654 return;
6655 }
6656
6657 const int vendor = Object_attribute::OBJ_ATTR_PROC;
6658 const Object_attribute* in_attr = pasd->known_attributes(vendor);
6659 Object_attribute* out_attr =
6660 this->attributes_section_data_->known_attributes(vendor);
6661
6662 // This needs to happen before Tag_ABI_FP_number_model is merged. */
6663 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
6664 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
6665 {
6666 // Ignore mismatches if the object doesn't use floating point. */
6667 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
6668 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
6669 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
6670 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
6671 gold_error(_("%s uses VFP register arguments, output does not"),
6672 name);
6673 }
6674
6675 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
6676 {
6677 // Merge this attribute with existing attributes.
6678 switch (i)
6679 {
6680 case elfcpp::Tag_CPU_raw_name:
6681 case elfcpp::Tag_CPU_name:
6682 // These are merged after Tag_CPU_arch.
6683 break;
6684
6685 case elfcpp::Tag_ABI_optimization_goals:
6686 case elfcpp::Tag_ABI_FP_optimization_goals:
6687 // Use the first value seen.
6688 break;
6689
6690 case elfcpp::Tag_CPU_arch:
6691 {
6692 unsigned int saved_out_attr = out_attr->int_value();
6693 // Merge Tag_CPU_arch and Tag_also_compatible_with.
6694 int secondary_compat =
6695 this->get_secondary_compatible_arch(pasd);
6696 int secondary_compat_out =
6697 this->get_secondary_compatible_arch(
6698 this->attributes_section_data_);
6699 out_attr[i].set_int_value(
6700 tag_cpu_arch_combine(name, out_attr[i].int_value(),
6701 &secondary_compat_out,
6702 in_attr[i].int_value(),
6703 secondary_compat));
6704 this->set_secondary_compatible_arch(this->attributes_section_data_,
6705 secondary_compat_out);
6706
6707 // Merge Tag_CPU_name and Tag_CPU_raw_name.
6708 if (out_attr[i].int_value() == saved_out_attr)
6709 ; // Leave the names alone.
6710 else if (out_attr[i].int_value() == in_attr[i].int_value())
6711 {
6712 // The output architecture has been changed to match the
6713 // input architecture. Use the input names.
6714 out_attr[elfcpp::Tag_CPU_name].set_string_value(
6715 in_attr[elfcpp::Tag_CPU_name].string_value());
6716 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
6717 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
6718 }
6719 else
6720 {
6721 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
6722 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
6723 }
6724
6725 // If we still don't have a value for Tag_CPU_name,
6726 // make one up now. Tag_CPU_raw_name remains blank.
6727 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
6728 {
6729 const std::string cpu_name =
6730 this->tag_cpu_name_value(out_attr[i].int_value());
6731 // FIXME: If we see an unknown CPU, this will be set
6732 // to "<unknown CPU n>", where n is the attribute value.
6733 // This is different from BFD, which leaves the name alone.
6734 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
6735 }
6736 }
6737 break;
6738
6739 case elfcpp::Tag_ARM_ISA_use:
6740 case elfcpp::Tag_THUMB_ISA_use:
6741 case elfcpp::Tag_WMMX_arch:
6742 case elfcpp::Tag_Advanced_SIMD_arch:
6743 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
6744 case elfcpp::Tag_ABI_FP_rounding:
6745 case elfcpp::Tag_ABI_FP_exceptions:
6746 case elfcpp::Tag_ABI_FP_user_exceptions:
6747 case elfcpp::Tag_ABI_FP_number_model:
6748 case elfcpp::Tag_VFP_HP_extension:
6749 case elfcpp::Tag_CPU_unaligned_access:
6750 case elfcpp::Tag_T2EE_use:
6751 case elfcpp::Tag_Virtualization_use:
6752 case elfcpp::Tag_MPextension_use:
6753 // Use the largest value specified.
6754 if (in_attr[i].int_value() > out_attr[i].int_value())
6755 out_attr[i].set_int_value(in_attr[i].int_value());
6756 break;
6757
6758 case elfcpp::Tag_ABI_align8_preserved:
6759 case elfcpp::Tag_ABI_PCS_RO_data:
6760 // Use the smallest value specified.
6761 if (in_attr[i].int_value() < out_attr[i].int_value())
6762 out_attr[i].set_int_value(in_attr[i].int_value());
6763 break;
6764
6765 case elfcpp::Tag_ABI_align8_needed:
6766 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
6767 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
6768 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
6769 == 0)))
6770 {
6771 // This error message should be enabled once all non-conformant
6772 // binaries in the toolchain have had the attributes set
6773 // properly.
6774 // gold_error(_("output 8-byte data alignment conflicts with %s"),
6775 // name);
6776 }
6777 // Fall through.
6778 case elfcpp::Tag_ABI_FP_denormal:
6779 case elfcpp::Tag_ABI_PCS_GOT_use:
6780 {
6781 // These tags have 0 = don't care, 1 = strong requirement,
6782 // 2 = weak requirement.
6783 static const int order_021[3] = {0, 2, 1};
6784
6785 // Use the "greatest" from the sequence 0, 2, 1, or the largest
6786 // value if greater than 2 (for future-proofing).
6787 if ((in_attr[i].int_value() > 2
6788 && in_attr[i].int_value() > out_attr[i].int_value())
6789 || (in_attr[i].int_value() <= 2
6790 && out_attr[i].int_value() <= 2
6791 && (order_021[in_attr[i].int_value()]
6792 > order_021[out_attr[i].int_value()])))
6793 out_attr[i].set_int_value(in_attr[i].int_value());
6794 }
6795 break;
6796
6797 case elfcpp::Tag_CPU_arch_profile:
6798 if (out_attr[i].int_value() != in_attr[i].int_value())
6799 {
6800 // 0 will merge with anything.
6801 // 'A' and 'S' merge to 'A'.
6802 // 'R' and 'S' merge to 'R'.
6803 // 'M' and 'A|R|S' is an error.
6804 if (out_attr[i].int_value() == 0
6805 || (out_attr[i].int_value() == 'S'
6806 && (in_attr[i].int_value() == 'A'
6807 || in_attr[i].int_value() == 'R')))
6808 out_attr[i].set_int_value(in_attr[i].int_value());
6809 else if (in_attr[i].int_value() == 0
6810 || (in_attr[i].int_value() == 'S'
6811 && (out_attr[i].int_value() == 'A'
6812 || out_attr[i].int_value() == 'R')))
6813 ; // Do nothing.
6814 else
6815 {
6816 gold_error
6817 (_("conflicting architecture profiles %c/%c"),
6818 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
6819 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
6820 }
6821 }
6822 break;
6823 case elfcpp::Tag_VFP_arch:
6824 {
6825 static const struct
6826 {
6827 int ver;
6828 int regs;
6829 } vfp_versions[7] =
6830 {
6831 {0, 0},
6832 {1, 16},
6833 {2, 16},
6834 {3, 32},
6835 {3, 16},
6836 {4, 32},
6837 {4, 16}
6838 };
6839
6840 // Values greater than 6 aren't defined, so just pick the
6841 // biggest.
6842 if (in_attr[i].int_value() > 6
6843 && in_attr[i].int_value() > out_attr[i].int_value())
6844 {
6845 *out_attr = *in_attr;
6846 break;
6847 }
6848 // The output uses the superset of input features
6849 // (ISA version) and registers.
6850 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
6851 vfp_versions[out_attr[i].int_value()].ver);
6852 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
6853 vfp_versions[out_attr[i].int_value()].regs);
6854 // This assumes all possible supersets are also a valid
6855 // options.
6856 int newval;
6857 for (newval = 6; newval > 0; newval--)
6858 {
6859 if (regs == vfp_versions[newval].regs
6860 && ver == vfp_versions[newval].ver)
6861 break;
6862 }
6863 out_attr[i].set_int_value(newval);
6864 }
6865 break;
6866 case elfcpp::Tag_PCS_config:
6867 if (out_attr[i].int_value() == 0)
6868 out_attr[i].set_int_value(in_attr[i].int_value());
6869 else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6870 {
6871 // It's sometimes ok to mix different configs, so this is only
6872 // a warning.
6873 gold_warning(_("%s: conflicting platform configuration"), name);
6874 }
6875 break;
6876 case elfcpp::Tag_ABI_PCS_R9_use:
6877 if (in_attr[i].int_value() != out_attr[i].int_value()
6878 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
6879 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
6880 {
6881 gold_error(_("%s: conflicting use of R9"), name);
6882 }
6883 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
6884 out_attr[i].set_int_value(in_attr[i].int_value());
6885 break;
6886 case elfcpp::Tag_ABI_PCS_RW_data:
6887 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
6888 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6889 != elfcpp::AEABI_R9_SB)
6890 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6891 != elfcpp::AEABI_R9_unused))
6892 {
6893 gold_error(_("%s: SB relative addressing conflicts with use "
6894 "of R9"),
6895 name);
6896 }
6897 // Use the smallest value specified.
6898 if (in_attr[i].int_value() < out_attr[i].int_value())
6899 out_attr[i].set_int_value(in_attr[i].int_value());
6900 break;
6901 case elfcpp::Tag_ABI_PCS_wchar_t:
6902 // FIXME: Make it possible to turn off this warning.
6903 if (out_attr[i].int_value()
6904 && in_attr[i].int_value()
6905 && out_attr[i].int_value() != in_attr[i].int_value())
6906 {
6907 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
6908 "use %u-byte wchar_t; use of wchar_t values "
6909 "across objects may fail"),
6910 name, in_attr[i].int_value(),
6911 out_attr[i].int_value());
6912 }
6913 else if (in_attr[i].int_value() && !out_attr[i].int_value())
6914 out_attr[i].set_int_value(in_attr[i].int_value());
6915 break;
6916 case elfcpp::Tag_ABI_enum_size:
6917 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
6918 {
6919 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
6920 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
6921 {
6922 // The existing object is compatible with anything.
6923 // Use whatever requirements the new object has.
6924 out_attr[i].set_int_value(in_attr[i].int_value());
6925 }
6926 // FIXME: Make it possible to turn off this warning.
6927 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
6928 && out_attr[i].int_value() != in_attr[i].int_value())
6929 {
6930 unsigned int in_value = in_attr[i].int_value();
6931 unsigned int out_value = out_attr[i].int_value();
6932 gold_warning(_("%s uses %s enums yet the output is to use "
6933 "%s enums; use of enum values across objects "
6934 "may fail"),
6935 name,
6936 this->aeabi_enum_name(in_value).c_str(),
6937 this->aeabi_enum_name(out_value).c_str());
6938 }
6939 }
6940 break;
6941 case elfcpp::Tag_ABI_VFP_args:
6942 // Aready done.
6943 break;
6944 case elfcpp::Tag_ABI_WMMX_args:
6945 if (in_attr[i].int_value() != out_attr[i].int_value())
6946 {
6947 gold_error(_("%s uses iWMMXt register arguments, output does "
6948 "not"),
6949 name);
6950 }
6951 break;
6952 case Object_attribute::Tag_compatibility:
6953 // Merged in target-independent code.
6954 break;
6955 case elfcpp::Tag_ABI_HardFP_use:
6956 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
6957 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
6958 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
6959 out_attr[i].set_int_value(3);
6960 else if (in_attr[i].int_value() > out_attr[i].int_value())
6961 out_attr[i].set_int_value(in_attr[i].int_value());
6962 break;
6963 case elfcpp::Tag_ABI_FP_16bit_format:
6964 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6965 {
6966 if (in_attr[i].int_value() != out_attr[i].int_value())
6967 gold_error(_("fp16 format mismatch between %s and output"),
6968 name);
6969 }
6970 if (in_attr[i].int_value() != 0)
6971 out_attr[i].set_int_value(in_attr[i].int_value());
6972 break;
6973
6974 case elfcpp::Tag_nodefaults:
6975 // This tag is set if it exists, but the value is unused (and is
6976 // typically zero). We don't actually need to do anything here -
6977 // the merge happens automatically when the type flags are merged
6978 // below.
6979 break;
6980 case elfcpp::Tag_also_compatible_with:
6981 // Already done in Tag_CPU_arch.
6982 break;
6983 case elfcpp::Tag_conformance:
6984 // Keep the attribute if it matches. Throw it away otherwise.
6985 // No attribute means no claim to conform.
6986 if (in_attr[i].string_value() != out_attr[i].string_value())
6987 out_attr[i].set_string_value("");
6988 break;
6989
6990 default:
6991 {
6992 const char* err_object = NULL;
6993
6994 // The "known_obj_attributes" table does contain some undefined
6995 // attributes. Ensure that there are unused.
6996 if (out_attr[i].int_value() != 0
6997 || out_attr[i].string_value() != "")
6998 err_object = "output";
6999 else if (in_attr[i].int_value() != 0
7000 || in_attr[i].string_value() != "")
7001 err_object = name;
7002
7003 if (err_object != NULL)
7004 {
7005 // Attribute numbers >=64 (mod 128) can be safely ignored.
7006 if ((i & 127) < 64)
7007 gold_error(_("%s: unknown mandatory EABI object attribute "
7008 "%d"),
7009 err_object, i);
7010 else
7011 gold_warning(_("%s: unknown EABI object attribute %d"),
7012 err_object, i);
7013 }
7014
7015 // Only pass on attributes that match in both inputs.
7016 if (!in_attr[i].matches(out_attr[i]))
7017 {
7018 out_attr[i].set_int_value(0);
7019 out_attr[i].set_string_value("");
7020 }
7021 }
7022 }
7023
7024 // If out_attr was copied from in_attr then it won't have a type yet.
7025 if (in_attr[i].type() && !out_attr[i].type())
7026 out_attr[i].set_type(in_attr[i].type());
7027 }
7028
7029 // Merge Tag_compatibility attributes and any common GNU ones.
7030 this->attributes_section_data_->merge(name, pasd);
7031
7032 // Check for any attributes not known on ARM.
7033 typedef Vendor_object_attributes::Other_attributes Other_attributes;
7034 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
7035 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
7036 Other_attributes* out_other_attributes =
7037 this->attributes_section_data_->other_attributes(vendor);
7038 Other_attributes::iterator out_iter = out_other_attributes->begin();
7039
7040 while (in_iter != in_other_attributes->end()
7041 || out_iter != out_other_attributes->end())
7042 {
7043 const char* err_object = NULL;
7044 int err_tag = 0;
7045
7046 // The tags for each list are in numerical order.
7047 // If the tags are equal, then merge.
7048 if (out_iter != out_other_attributes->end()
7049 && (in_iter == in_other_attributes->end()
7050 || in_iter->first > out_iter->first))
7051 {
7052 // This attribute only exists in output. We can't merge, and we
7053 // don't know what the tag means, so delete it.
7054 err_object = "output";
7055 err_tag = out_iter->first;
7056 int saved_tag = out_iter->first;
7057 delete out_iter->second;
7058 out_other_attributes->erase(out_iter);
7059 out_iter = out_other_attributes->upper_bound(saved_tag);
7060 }
7061 else if (in_iter != in_other_attributes->end()
7062 && (out_iter != out_other_attributes->end()
7063 || in_iter->first < out_iter->first))
7064 {
7065 // This attribute only exists in input. We can't merge, and we
7066 // don't know what the tag means, so ignore it.
7067 err_object = name;
7068 err_tag = in_iter->first;
7069 ++in_iter;
7070 }
7071 else // The tags are equal.
7072 {
7073 // As present, all attributes in the list are unknown, and
7074 // therefore can't be merged meaningfully.
7075 err_object = "output";
7076 err_tag = out_iter->first;
7077
7078 // Only pass on attributes that match in both inputs.
7079 if (!in_iter->second->matches(*(out_iter->second)))
7080 {
7081 // No match. Delete the attribute.
7082 int saved_tag = out_iter->first;
7083 delete out_iter->second;
7084 out_other_attributes->erase(out_iter);
7085 out_iter = out_other_attributes->upper_bound(saved_tag);
7086 }
7087 else
7088 {
7089 // Matched. Keep the attribute and move to the next.
7090 ++out_iter;
7091 ++in_iter;
7092 }
7093 }
7094
7095 if (err_object)
7096 {
7097 // Attribute numbers >=64 (mod 128) can be safely ignored. */
7098 if ((err_tag & 127) < 64)
7099 {
7100 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
7101 err_object, err_tag);
7102 }
7103 else
7104 {
7105 gold_warning(_("%s: unknown EABI object attribute %d"),
7106 err_object, err_tag);
7107 }
7108 }
7109 }
7110}
7111
55da9579
DK
7112// Return whether a relocation type used the LSB to distinguish THUMB
7113// addresses.
7114template<bool big_endian>
7115bool
7116Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
7117{
7118 switch (r_type)
7119 {
7120 case elfcpp::R_ARM_PC24:
7121 case elfcpp::R_ARM_ABS32:
7122 case elfcpp::R_ARM_REL32:
7123 case elfcpp::R_ARM_SBREL32:
7124 case elfcpp::R_ARM_THM_CALL:
7125 case elfcpp::R_ARM_GLOB_DAT:
7126 case elfcpp::R_ARM_JUMP_SLOT:
7127 case elfcpp::R_ARM_GOTOFF32:
7128 case elfcpp::R_ARM_PLT32:
7129 case elfcpp::R_ARM_CALL:
7130 case elfcpp::R_ARM_JUMP24:
7131 case elfcpp::R_ARM_THM_JUMP24:
7132 case elfcpp::R_ARM_SBREL31:
7133 case elfcpp::R_ARM_PREL31:
7134 case elfcpp::R_ARM_MOVW_ABS_NC:
7135 case elfcpp::R_ARM_MOVW_PREL_NC:
7136 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7137 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7138 case elfcpp::R_ARM_THM_JUMP19:
7139 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7140 case elfcpp::R_ARM_ALU_PC_G0_NC:
7141 case elfcpp::R_ARM_ALU_PC_G0:
7142 case elfcpp::R_ARM_ALU_PC_G1_NC:
7143 case elfcpp::R_ARM_ALU_PC_G1:
7144 case elfcpp::R_ARM_ALU_PC_G2:
7145 case elfcpp::R_ARM_ALU_SB_G0_NC:
7146 case elfcpp::R_ARM_ALU_SB_G0:
7147 case elfcpp::R_ARM_ALU_SB_G1_NC:
7148 case elfcpp::R_ARM_ALU_SB_G1:
7149 case elfcpp::R_ARM_ALU_SB_G2:
7150 case elfcpp::R_ARM_MOVW_BREL_NC:
7151 case elfcpp::R_ARM_MOVW_BREL:
7152 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7153 case elfcpp::R_ARM_THM_MOVW_BREL:
7154 return true;
7155 default:
7156 return false;
7157 }
7158}
7159
7160// Stub-generation methods for Target_arm.
7161
7162// Make a new Arm_input_section object.
7163
7164template<bool big_endian>
7165Arm_input_section<big_endian>*
7166Target_arm<big_endian>::new_arm_input_section(
2ea97941
ILT
7167 Relobj* relobj,
7168 unsigned int shndx)
55da9579 7169{
2ea97941 7170 Input_section_specifier iss(relobj, shndx);
55da9579
DK
7171
7172 Arm_input_section<big_endian>* arm_input_section =
2ea97941 7173 new Arm_input_section<big_endian>(relobj, shndx);
55da9579
DK
7174 arm_input_section->init();
7175
7176 // Register new Arm_input_section in map for look-up.
7177 std::pair<typename Arm_input_section_map::iterator, bool> ins =
7178 this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section));
7179
7180 // Make sure that it we have not created another Arm_input_section
7181 // for this input section already.
7182 gold_assert(ins.second);
7183
7184 return arm_input_section;
7185}
7186
7187// Find the Arm_input_section object corresponding to the SHNDX-th input
7188// section of RELOBJ.
7189
7190template<bool big_endian>
7191Arm_input_section<big_endian>*
7192Target_arm<big_endian>::find_arm_input_section(
2ea97941
ILT
7193 Relobj* relobj,
7194 unsigned int shndx) const
55da9579 7195{
2ea97941 7196 Input_section_specifier iss(relobj, shndx);
55da9579
DK
7197 typename Arm_input_section_map::const_iterator p =
7198 this->arm_input_section_map_.find(iss);
7199 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
7200}
7201
7202// Make a new stub table.
7203
7204template<bool big_endian>
7205Stub_table<big_endian>*
7206Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
7207{
2ea97941 7208 Stub_table<big_endian>* stub_table =
55da9579 7209 new Stub_table<big_endian>(owner);
2ea97941 7210 this->stub_tables_.push_back(stub_table);
55da9579 7211
2ea97941
ILT
7212 stub_table->set_address(owner->address() + owner->data_size());
7213 stub_table->set_file_offset(owner->offset() + owner->data_size());
7214 stub_table->finalize_data_size();
55da9579 7215
2ea97941 7216 return stub_table;
55da9579
DK
7217}
7218
eb44217c
DK
7219// Scan a relocation for stub generation.
7220
7221template<bool big_endian>
7222void
7223Target_arm<big_endian>::scan_reloc_for_stub(
7224 const Relocate_info<32, big_endian>* relinfo,
7225 unsigned int r_type,
7226 const Sized_symbol<32>* gsym,
7227 unsigned int r_sym,
7228 const Symbol_value<32>* psymval,
7229 elfcpp::Elf_types<32>::Elf_Swxword addend,
7230 Arm_address address)
7231{
2ea97941 7232 typedef typename Target_arm<big_endian>::Relocate Relocate;
eb44217c
DK
7233
7234 const Arm_relobj<big_endian>* arm_relobj =
7235 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
7236
7237 bool target_is_thumb;
7238 Symbol_value<32> symval;
7239 if (gsym != NULL)
7240 {
7241 // This is a global symbol. Determine if we use PLT and if the
7242 // final target is THUMB.
2ea97941 7243 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
eb44217c
DK
7244 {
7245 // This uses a PLT, change the symbol value.
7246 symval.set_output_value(this->plt_section()->address()
7247 + gsym->plt_offset());
7248 psymval = &symval;
7249 target_is_thumb = false;
7250 }
7251 else if (gsym->is_undefined())
7252 // There is no need to generate a stub symbol is undefined.
7253 return;
7254 else
7255 {
7256 target_is_thumb =
7257 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
7258 || (gsym->type() == elfcpp::STT_FUNC
7259 && !gsym->is_undefined()
7260 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
7261 }
7262 }
7263 else
7264 {
7265 // This is a local symbol. Determine if the final target is THUMB.
7266 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
7267 }
7268
7269 // Strip LSB if this points to a THUMB target.
7270 if (target_is_thumb
7271 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
7272 && ((psymval->value(arm_relobj, 0) & 1) != 0))
7273 {
7274 Arm_address stripped_value =
7275 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
7276 symval.set_output_value(stripped_value);
7277 psymval = &symval;
7278 }
7279
7280 // Get the symbol value.
7281 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
7282
7283 // Owing to pipelining, the PC relative branches below actually skip
7284 // two instructions when the branch offset is 0.
7285 Arm_address destination;
7286 switch (r_type)
7287 {
7288 case elfcpp::R_ARM_CALL:
7289 case elfcpp::R_ARM_JUMP24:
7290 case elfcpp::R_ARM_PLT32:
7291 // ARM branches.
7292 destination = value + addend + 8;
7293 break;
7294 case elfcpp::R_ARM_THM_CALL:
7295 case elfcpp::R_ARM_THM_XPC22:
7296 case elfcpp::R_ARM_THM_JUMP24:
7297 case elfcpp::R_ARM_THM_JUMP19:
7298 // THUMB branches.
7299 destination = value + addend + 4;
7300 break;
7301 default:
7302 gold_unreachable();
7303 }
7304
a120bc7f 7305 Reloc_stub* stub = NULL;
eb44217c
DK
7306 Stub_type stub_type =
7307 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
7308 target_is_thumb);
a120bc7f
DK
7309 if (stub_type != arm_stub_none)
7310 {
7311 // Try looking up an existing stub from a stub table.
7312 Stub_table<big_endian>* stub_table =
7313 arm_relobj->stub_table(relinfo->data_shndx);
7314 gold_assert(stub_table != NULL);
eb44217c 7315
a120bc7f
DK
7316 // Locate stub by destination.
7317 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
eb44217c 7318
a120bc7f
DK
7319 // Create a stub if there is not one already
7320 stub = stub_table->find_reloc_stub(stub_key);
7321 if (stub == NULL)
7322 {
7323 // create a new stub and add it to stub table.
7324 stub = this->stub_factory().make_reloc_stub(stub_type);
7325 stub_table->add_reloc_stub(stub, stub_key);
7326 }
7327
7328 // Record the destination address.
7329 stub->set_destination_address(destination
7330 | (target_is_thumb ? 1 : 0));
eb44217c
DK
7331 }
7332
a120bc7f
DK
7333 // For Cortex-A8, we need to record a relocation at 4K page boundary.
7334 if (this->fix_cortex_a8_
7335 && (r_type == elfcpp::R_ARM_THM_JUMP24
7336 || r_type == elfcpp::R_ARM_THM_JUMP19
7337 || r_type == elfcpp::R_ARM_THM_CALL
7338 || r_type == elfcpp::R_ARM_THM_XPC22)
7339 && (address & 0xfffU) == 0xffeU)
7340 {
7341 // Found a candidate. Note we haven't checked the destination is
7342 // within 4K here: if we do so (and don't create a record) we can't
7343 // tell that a branch should have been relocated when scanning later.
7344 this->cortex_a8_relocs_info_[address] =
7345 new Cortex_a8_reloc(stub, r_type,
7346 destination | (target_is_thumb ? 1 : 0));
7347 }
eb44217c
DK
7348}
7349
7350// This function scans a relocation sections for stub generation.
7351// The template parameter Relocate must be a class type which provides
7352// a single function, relocate(), which implements the machine
7353// specific part of a relocation.
7354
7355// BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
7356// SHT_REL or SHT_RELA.
7357
7358// PRELOCS points to the relocation data. RELOC_COUNT is the number
7359// of relocs. OUTPUT_SECTION is the output section.
7360// NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
7361// mapped to output offsets.
7362
7363// VIEW is the section data, VIEW_ADDRESS is its memory address, and
7364// VIEW_SIZE is the size. These refer to the input section, unless
7365// NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
7366// the output section.
7367
7368template<bool big_endian>
7369template<int sh_type>
7370void inline
7371Target_arm<big_endian>::scan_reloc_section_for_stubs(
7372 const Relocate_info<32, big_endian>* relinfo,
7373 const unsigned char* prelocs,
7374 size_t reloc_count,
7375 Output_section* output_section,
7376 bool needs_special_offset_handling,
7377 const unsigned char* view,
7378 elfcpp::Elf_types<32>::Elf_Addr view_address,
7379 section_size_type)
7380{
7381 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
7382 const int reloc_size =
7383 Reloc_types<sh_type, 32, big_endian>::reloc_size;
7384
7385 Arm_relobj<big_endian>* arm_object =
7386 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
7387 unsigned int local_count = arm_object->local_symbol_count();
7388
7389 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
7390
7391 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
7392 {
7393 Reltype reloc(prelocs);
7394
7395 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
7396 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
7397 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
7398
7399 r_type = this->get_real_reloc_type(r_type);
7400
7401 // Only a few relocation types need stubs.
7402 if ((r_type != elfcpp::R_ARM_CALL)
7403 && (r_type != elfcpp::R_ARM_JUMP24)
7404 && (r_type != elfcpp::R_ARM_PLT32)
7405 && (r_type != elfcpp::R_ARM_THM_CALL)
7406 && (r_type != elfcpp::R_ARM_THM_XPC22)
7407 && (r_type != elfcpp::R_ARM_THM_JUMP24)
7408 && (r_type != elfcpp::R_ARM_THM_JUMP19))
7409 continue;
7410
2ea97941 7411 section_offset_type offset =
eb44217c
DK
7412 convert_to_section_size_type(reloc.get_r_offset());
7413
7414 if (needs_special_offset_handling)
7415 {
2ea97941
ILT
7416 offset = output_section->output_offset(relinfo->object,
7417 relinfo->data_shndx,
7418 offset);
7419 if (offset == -1)
eb44217c
DK
7420 continue;
7421 }
7422
7423 // Get the addend.
7424 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
7425 elfcpp::Elf_types<32>::Elf_Swxword addend =
2ea97941 7426 stub_addend_reader(r_type, view + offset, reloc);
eb44217c
DK
7427
7428 const Sized_symbol<32>* sym;
7429
7430 Symbol_value<32> symval;
7431 const Symbol_value<32> *psymval;
7432 if (r_sym < local_count)
7433 {
7434 sym = NULL;
7435 psymval = arm_object->local_symbol(r_sym);
7436
7437 // If the local symbol belongs to a section we are discarding,
7438 // and that section is a debug section, try to find the
7439 // corresponding kept section and map this symbol to its
7440 // counterpart in the kept section. The symbol must not
7441 // correspond to a section we are folding.
7442 bool is_ordinary;
2ea97941 7443 unsigned int shndx = psymval->input_shndx(&is_ordinary);
eb44217c 7444 if (is_ordinary
2ea97941
ILT
7445 && shndx != elfcpp::SHN_UNDEF
7446 && !arm_object->is_section_included(shndx)
7447 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
eb44217c
DK
7448 {
7449 if (comdat_behavior == CB_UNDETERMINED)
7450 {
7451 std::string name =
7452 arm_object->section_name(relinfo->data_shndx);
7453 comdat_behavior = get_comdat_behavior(name.c_str());
7454 }
7455 if (comdat_behavior == CB_PRETEND)
7456 {
7457 bool found;
7458 typename elfcpp::Elf_types<32>::Elf_Addr value =
2ea97941 7459 arm_object->map_to_kept_section(shndx, &found);
eb44217c
DK
7460 if (found)
7461 symval.set_output_value(value + psymval->input_value());
7462 else
7463 symval.set_output_value(0);
7464 }
7465 else
7466 {
7467 symval.set_output_value(0);
7468 }
7469 symval.set_no_output_symtab_entry();
7470 psymval = &symval;
7471 }
7472 }
7473 else
7474 {
7475 const Symbol* gsym = arm_object->global_symbol(r_sym);
7476 gold_assert(gsym != NULL);
7477 if (gsym->is_forwarder())
7478 gsym = relinfo->symtab->resolve_forwards(gsym);
7479
7480 sym = static_cast<const Sized_symbol<32>*>(gsym);
7481 if (sym->has_symtab_index())
7482 symval.set_output_symtab_index(sym->symtab_index());
7483 else
7484 symval.set_no_output_symtab_entry();
7485
7486 // We need to compute the would-be final value of this global
7487 // symbol.
7488 const Symbol_table* symtab = relinfo->symtab;
7489 const Sized_symbol<32>* sized_symbol =
7490 symtab->get_sized_symbol<32>(gsym);
7491 Symbol_table::Compute_final_value_status status;
7492 Arm_address value =
7493 symtab->compute_final_value<32>(sized_symbol, &status);
7494
7495 // Skip this if the symbol has not output section.
7496 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
7497 continue;
7498
7499 symval.set_output_value(value);
7500 psymval = &symval;
7501 }
7502
7503 // If symbol is a section symbol, we don't know the actual type of
7504 // destination. Give up.
7505 if (psymval->is_section_symbol())
7506 continue;
7507
7508 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
2ea97941 7509 addend, view_address + offset);
eb44217c
DK
7510 }
7511}
7512
7513// Scan an input section for stub generation.
7514
7515template<bool big_endian>
7516void
7517Target_arm<big_endian>::scan_section_for_stubs(
7518 const Relocate_info<32, big_endian>* relinfo,
7519 unsigned int sh_type,
7520 const unsigned char* prelocs,
7521 size_t reloc_count,
7522 Output_section* output_section,
7523 bool needs_special_offset_handling,
7524 const unsigned char* view,
7525 Arm_address view_address,
7526 section_size_type view_size)
7527{
7528 if (sh_type == elfcpp::SHT_REL)
7529 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
7530 relinfo,
7531 prelocs,
7532 reloc_count,
7533 output_section,
7534 needs_special_offset_handling,
7535 view,
7536 view_address,
7537 view_size);
7538 else if (sh_type == elfcpp::SHT_RELA)
7539 // We do not support RELA type relocations yet. This is provided for
7540 // completeness.
7541 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
7542 relinfo,
7543 prelocs,
7544 reloc_count,
7545 output_section,
7546 needs_special_offset_handling,
7547 view,
7548 view_address,
7549 view_size);
7550 else
7551 gold_unreachable();
7552}
7553
7554// Group input sections for stub generation.
7555//
7556// We goup input sections in an output sections so that the total size,
7557// including any padding space due to alignment is smaller than GROUP_SIZE
7558// unless the only input section in group is bigger than GROUP_SIZE already.
7559// Then an ARM stub table is created to follow the last input section
7560// in group. For each group an ARM stub table is created an is placed
7561// after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
7562// extend the group after the stub table.
7563
7564template<bool big_endian>
7565void
7566Target_arm<big_endian>::group_sections(
2ea97941 7567 Layout* layout,
eb44217c
DK
7568 section_size_type group_size,
7569 bool stubs_always_after_branch)
7570{
7571 // Group input sections and insert stub table
7572 Layout::Section_list section_list;
2ea97941 7573 layout->get_allocated_sections(&section_list);
eb44217c
DK
7574 for (Layout::Section_list::const_iterator p = section_list.begin();
7575 p != section_list.end();
7576 ++p)
7577 {
7578 Arm_output_section<big_endian>* output_section =
7579 Arm_output_section<big_endian>::as_arm_output_section(*p);
7580 output_section->group_sections(group_size, stubs_always_after_branch,
7581 this);
7582 }
7583}
7584
7585// Relaxation hook. This is where we do stub generation.
7586
7587template<bool big_endian>
7588bool
7589Target_arm<big_endian>::do_relax(
7590 int pass,
7591 const Input_objects* input_objects,
7592 Symbol_table* symtab,
2ea97941 7593 Layout* layout)
eb44217c
DK
7594{
7595 // No need to generate stubs if this is a relocatable link.
7596 gold_assert(!parameters->options().relocatable());
7597
7598 // If this is the first pass, we need to group input sections into
7599 // stub groups.
7600 if (pass == 1)
7601 {
7602 // Determine the stub group size. The group size is the absolute
7603 // value of the parameter --stub-group-size. If --stub-group-size
7604 // is passed a negative value, we restict stubs to be always after
7605 // the stubbed branches.
7606 int32_t stub_group_size_param =
7607 parameters->options().stub_group_size();
7608 bool stubs_always_after_branch = stub_group_size_param < 0;
7609 section_size_type stub_group_size = abs(stub_group_size_param);
7610
44272192
DK
7611 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
7612 // page as the first half of a 32-bit branch straddling two 4K pages.
7613 // This is a crude way of enforcing that.
7614 if (this->fix_cortex_a8_)
7615 stubs_always_after_branch = true;
7616
eb44217c
DK
7617 if (stub_group_size == 1)
7618 {
7619 // Default value.
7620 // Thumb branch range is +-4MB has to be used as the default
7621 // maximum size (a given section can contain both ARM and Thumb
7622 // code, so the worst case has to be taken into account).
7623 //
7624 // This value is 24K less than that, which allows for 2025
7625 // 12-byte stubs. If we exceed that, then we will fail to link.
7626 // The user will have to relink with an explicit group size
7627 // option.
7628 stub_group_size = 4170000;
7629 }
7630
2ea97941 7631 group_sections(layout, stub_group_size, stubs_always_after_branch);
eb44217c
DK
7632 }
7633
44272192
DK
7634 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
7635 // beginning of each relaxation pass, just blow away all the stubs.
7636 // Alternatively, we could selectively remove only the stubs and reloc
7637 // information for code sections that have moved since the last pass.
7638 // That would require more book-keeping.
eb44217c 7639 typedef typename Stub_table_list::iterator Stub_table_iterator;
a120bc7f
DK
7640 if (this->fix_cortex_a8_)
7641 {
7642 // Clear all Cortex-A8 reloc information.
7643 for (typename Cortex_a8_relocs_info::const_iterator p =
7644 this->cortex_a8_relocs_info_.begin();
7645 p != this->cortex_a8_relocs_info_.end();
7646 ++p)
7647 delete p->second;
7648 this->cortex_a8_relocs_info_.clear();
44272192
DK
7649
7650 // Remove all Cortex-A8 stubs.
7651 for (Stub_table_iterator sp = this->stub_tables_.begin();
7652 sp != this->stub_tables_.end();
7653 ++sp)
7654 (*sp)->remove_all_cortex_a8_stubs();
a120bc7f
DK
7655 }
7656
44272192 7657 // Scan relocs for relocation stubs
eb44217c
DK
7658 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
7659 op != input_objects->relobj_end();
7660 ++op)
7661 {
7662 Arm_relobj<big_endian>* arm_relobj =
7663 Arm_relobj<big_endian>::as_arm_relobj(*op);
2ea97941 7664 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
eb44217c
DK
7665 }
7666
2fb7225c
DK
7667 // Check all stub tables to see if any of them have their data sizes
7668 // or addresses alignments changed. These are the only things that
7669 // matter.
eb44217c
DK
7670 bool any_stub_table_changed = false;
7671 for (Stub_table_iterator sp = this->stub_tables_.begin();
7672 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
7673 ++sp)
7674 {
2fb7225c 7675 if ((*sp)->update_data_size_and_addralign())
eb44217c
DK
7676 any_stub_table_changed = true;
7677 }
7678
2fb7225c
DK
7679 // Finalize the stubs in the last relaxation pass.
7680 if (!any_stub_table_changed)
7681 for (Stub_table_iterator sp = this->stub_tables_.begin();
7682 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
7683 ++sp)
7684 (*sp)->finalize_stubs();
7685
eb44217c
DK
7686 return any_stub_table_changed;
7687}
7688
43d12afe
DK
7689// Relocate a stub.
7690
7691template<bool big_endian>
7692void
7693Target_arm<big_endian>::relocate_stub(
2fb7225c 7694 Stub* stub,
43d12afe
DK
7695 const Relocate_info<32, big_endian>* relinfo,
7696 Output_section* output_section,
7697 unsigned char* view,
7698 Arm_address address,
7699 section_size_type view_size)
7700{
7701 Relocate relocate;
2ea97941
ILT
7702 const Stub_template* stub_template = stub->stub_template();
7703 for (size_t i = 0; i < stub_template->reloc_count(); i++)
43d12afe 7704 {
2ea97941
ILT
7705 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
7706 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
43d12afe
DK
7707
7708 unsigned int r_type = insn->r_type();
2ea97941 7709 section_size_type reloc_offset = stub_template->reloc_offset(i);
43d12afe
DK
7710 section_size_type reloc_size = insn->size();
7711 gold_assert(reloc_offset + reloc_size <= view_size);
7712
7713 // This is the address of the stub destination.
7714 Arm_address target = stub->reloc_target(i);
7715 Symbol_value<32> symval;
7716 symval.set_output_value(target);
7717
7718 // Synthesize a fake reloc just in case. We don't have a symbol so
7719 // we use 0.
7720 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
7721 memset(reloc_buffer, 0, sizeof(reloc_buffer));
7722 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
7723 reloc_write.put_r_offset(reloc_offset);
7724 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
7725 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
7726
7727 relocate.relocate(relinfo, this, output_section,
7728 this->fake_relnum_for_stubs, rel, r_type,
7729 NULL, &symval, view + reloc_offset,
7730 address + reloc_offset, reloc_size);
7731 }
7732}
7733
a0351a69
DK
7734// Determine whether an object attribute tag takes an integer, a
7735// string or both.
7736
7737template<bool big_endian>
7738int
7739Target_arm<big_endian>::do_attribute_arg_type(int tag) const
7740{
7741 if (tag == Object_attribute::Tag_compatibility)
7742 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7743 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
7744 else if (tag == elfcpp::Tag_nodefaults)
7745 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7746 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
7747 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
7748 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
7749 else if (tag < 32)
7750 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
7751 else
7752 return ((tag & 1) != 0
7753 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
7754 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
7755}
7756
7757// Reorder attributes.
7758//
7759// The ABI defines that Tag_conformance should be emitted first, and that
7760// Tag_nodefaults should be second (if either is defined). This sets those
7761// two positions, and bumps up the position of all the remaining tags to
7762// compensate.
7763
7764template<bool big_endian>
7765int
7766Target_arm<big_endian>::do_attributes_order(int num) const
7767{
7768 // Reorder the known object attributes in output. We want to move
7769 // Tag_conformance to position 4 and Tag_conformance to position 5
7770 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
7771 if (num == 4)
7772 return elfcpp::Tag_conformance;
7773 if (num == 5)
7774 return elfcpp::Tag_nodefaults;
7775 if ((num - 2) < elfcpp::Tag_nodefaults)
7776 return num - 2;
7777 if ((num - 1) < elfcpp::Tag_conformance)
7778 return num - 1;
7779 return num;
7780}
4a657b0d 7781
44272192
DK
7782// Scan a span of THUMB code for Cortex-A8 erratum.
7783
7784template<bool big_endian>
7785void
7786Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
7787 Arm_relobj<big_endian>* arm_relobj,
7788 unsigned int shndx,
7789 section_size_type span_start,
7790 section_size_type span_end,
7791 const unsigned char* view,
7792 Arm_address address)
7793{
7794 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
7795 //
7796 // The opcode is BLX.W, BL.W, B.W, Bcc.W
7797 // The branch target is in the same 4KB region as the
7798 // first half of the branch.
7799 // The instruction before the branch is a 32-bit
7800 // length non-branch instruction.
7801 section_size_type i = span_start;
7802 bool last_was_32bit = false;
7803 bool last_was_branch = false;
7804 while (i < span_end)
7805 {
7806 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
7807 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
7808 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
7809 bool is_blx = false, is_b = false;
7810 bool is_bl = false, is_bcc = false;
7811
7812 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
7813 if (insn_32bit)
7814 {
7815 // Load the rest of the insn (in manual-friendly order).
7816 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
7817
7818 // Encoding T4: B<c>.W.
7819 is_b = (insn & 0xf800d000U) == 0xf0009000U;
7820 // Encoding T1: BL<c>.W.
7821 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
7822 // Encoding T2: BLX<c>.W.
7823 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
7824 // Encoding T3: B<c>.W (not permitted in IT block).
7825 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
7826 && (insn & 0x07f00000U) != 0x03800000U);
7827 }
7828
7829 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
7830
7831 // If this instruction is a 32-bit THUMB branch that crosses a 4K
7832 // page boundary and it follows 32-bit non-branch instruction,
7833 // we need to work around.
7834 if (is_32bit_branch
7835 && ((address + i) & 0xfffU) == 0xffeU
7836 && last_was_32bit
7837 && !last_was_branch)
7838 {
7839 // Check to see if there is a relocation stub for this branch.
7840 bool force_target_arm = false;
7841 bool force_target_thumb = false;
7842 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
7843 Cortex_a8_relocs_info::const_iterator p =
7844 this->cortex_a8_relocs_info_.find(address + i);
7845
7846 if (p != this->cortex_a8_relocs_info_.end())
7847 {
7848 cortex_a8_reloc = p->second;
7849 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
7850
7851 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
7852 && !target_is_thumb)
7853 force_target_arm = true;
7854 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
7855 && target_is_thumb)
7856 force_target_thumb = true;
7857 }
7858
7859 off_t offset;
7860 Stub_type stub_type = arm_stub_none;
7861
7862 // Check if we have an offending branch instruction.
7863 uint16_t upper_insn = (insn >> 16) & 0xffffU;
7864 uint16_t lower_insn = insn & 0xffffU;
7865 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
7866
7867 if (cortex_a8_reloc != NULL
7868 && cortex_a8_reloc->reloc_stub() != NULL)
7869 // We've already made a stub for this instruction, e.g.
7870 // it's a long branch or a Thumb->ARM stub. Assume that
7871 // stub will suffice to work around the A8 erratum (see
7872 // setting of always_after_branch above).
7873 ;
7874 else if (is_bcc)
7875 {
7876 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
7877 lower_insn);
7878 stub_type = arm_stub_a8_veneer_b_cond;
7879 }
7880 else if (is_b || is_bl || is_blx)
7881 {
7882 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
7883 lower_insn);
7884 if (is_blx)
7885 offset &= ~3;
7886
7887 stub_type = (is_blx
7888 ? arm_stub_a8_veneer_blx
7889 : (is_bl
7890 ? arm_stub_a8_veneer_bl
7891 : arm_stub_a8_veneer_b));
7892 }
7893
7894 if (stub_type != arm_stub_none)
7895 {
7896 Arm_address pc_for_insn = address + i + 4;
7897
7898 // The original instruction is a BL, but the target is
7899 // an ARM instruction. If we were not making a stub,
7900 // the BL would have been converted to a BLX. Use the
7901 // BLX stub instead in that case.
7902 if (this->may_use_blx() && force_target_arm
7903 && stub_type == arm_stub_a8_veneer_bl)
7904 {
7905 stub_type = arm_stub_a8_veneer_blx;
7906 is_blx = true;
7907 is_bl = false;
7908 }
7909 // Conversely, if the original instruction was
7910 // BLX but the target is Thumb mode, use the BL stub.
7911 else if (force_target_thumb
7912 && stub_type == arm_stub_a8_veneer_blx)
7913 {
7914 stub_type = arm_stub_a8_veneer_bl;
7915 is_blx = false;
7916 is_bl = true;
7917 }
7918
7919 if (is_blx)
7920 pc_for_insn &= ~3;
7921
7922 // If we found a relocation, use the proper destination,
7923 // not the offset in the (unrelocated) instruction.
7924 // Note this is always done if we switched the stub type above.
7925 if (cortex_a8_reloc != NULL)
7926 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
7927
7928 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
7929
7930 // Add a new stub if destination address in in the same page.
7931 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
7932 {
7933 Cortex_a8_stub* stub =
7934 this->stub_factory_.make_cortex_a8_stub(stub_type,
7935 arm_relobj, shndx,
7936 address + i,
7937 target, insn);
7938 Stub_table<big_endian>* stub_table =
7939 arm_relobj->stub_table(shndx);
7940 gold_assert(stub_table != NULL);
7941 stub_table->add_cortex_a8_stub(address + i, stub);
7942 }
7943 }
7944 }
7945
7946 i += insn_32bit ? 4 : 2;
7947 last_was_32bit = insn_32bit;
7948 last_was_branch = is_32bit_branch;
7949 }
7950}
7951
4a657b0d
DK
7952template<bool big_endian>
7953class Target_selector_arm : public Target_selector
7954{
7955 public:
7956 Target_selector_arm()
7957 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
7958 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
7959 { }
7960
7961 Target*
7962 do_instantiate_target()
7963 { return new Target_arm<big_endian>(); }
7964};
7965
7966Target_selector_arm<false> target_selector_arm;
7967Target_selector_arm<true> target_selector_armbe;
7968
7969} // End anonymous namespace.
This page took 0.391927 seconds and 4 git commands to generate.