*** empty log message ***
[deliverable/binutils-gdb.git] / gold / arm.cc
CommitLineData
4a657b0d
DK
1// arm.cc -- arm target support for gold.
2
b10d2873 3// Copyright 2009, 2010 Free Software Foundation, Inc.
4a657b0d
DK
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>
41263c05
DK
33#include <map>
34#include <utility>
2b328d4e 35#include <set>
4a657b0d
DK
36
37#include "elfcpp.h"
38#include "parameters.h"
39#include "reloc.h"
40#include "arm.h"
41#include "object.h"
42#include "symtab.h"
43#include "layout.h"
44#include "output.h"
45#include "copy-relocs.h"
46#include "target.h"
47#include "target-reloc.h"
48#include "target-select.h"
49#include "tls.h"
50#include "defstd.h"
f345227a 51#include "gc.h"
a0351a69 52#include "attributes.h"
0d31c79d 53#include "arm-reloc-property.h"
4a657b0d
DK
54
55namespace
56{
57
58using namespace gold;
59
94cdfcff
DK
60template<bool big_endian>
61class Output_data_plt_arm;
62
56ee5e00
DK
63template<bool big_endian>
64class Stub_table;
65
66template<bool big_endian>
67class Arm_input_section;
68
af2cdeae
DK
69class Arm_exidx_cantunwind;
70
71class Arm_exidx_merged_section;
72
80d0d023
DK
73class Arm_exidx_fixup;
74
07f508a2
DK
75template<bool big_endian>
76class Arm_output_section;
77
993d07c1
DK
78class Arm_exidx_input_section;
79
07f508a2
DK
80template<bool big_endian>
81class Arm_relobj;
82
f96accdf
DK
83template<bool big_endian>
84class Arm_relocate_functions;
85
b569affa
DK
86template<bool big_endian>
87class Target_arm;
88
89// For convenience.
90typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
91
92// Maximum branch offsets for ARM, THUMB and THUMB2.
93const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
94const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
95const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
96const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
97const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
98const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
99
4a657b0d
DK
100// The arm target class.
101//
102// This is a very simple port of gold for ARM-EABI. It is intended for
b10d2873 103// supporting Android only for the time being.
4a657b0d 104//
4a657b0d 105// TODOs:
0d31c79d 106// - Implement all static relocation types documented in arm-reloc.def.
94cdfcff
DK
107// - Make PLTs more flexible for different architecture features like
108// Thumb-2 and BE8.
11af873f 109// There are probably a lot more.
4a657b0d 110
0d31c79d
DK
111// Ideally we would like to avoid using global variables but this is used
112// very in many places and sometimes in loops. If we use a function
113// returning a static instance of Arm_reloc_property_table, it will very
114// slow in an threaded environment since the static instance needs to be
115// locked. The pointer is below initialized in the
116// Target::do_select_as_default_target() hook so that we do not spend time
117// building the table if we are not linking ARM objects.
118//
119// An alternative is to to process the information in arm-reloc.def in
120// compilation time and generate a representation of it in PODs only. That
121// way we can avoid initialization when the linker starts.
122
123Arm_reloc_property_table *arm_reloc_property_table = NULL;
124
b569affa
DK
125// Instruction template class. This class is similar to the insn_sequence
126// struct in bfd/elf32-arm.c.
127
128class Insn_template
129{
130 public:
131 // Types of instruction templates.
132 enum Type
133 {
134 THUMB16_TYPE = 1,
bb0d3eb0
DK
135 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
136 // templates with class-specific semantics. Currently this is used
137 // only by the Cortex_a8_stub class for handling condition codes in
138 // conditional branches.
139 THUMB16_SPECIAL_TYPE,
b569affa
DK
140 THUMB32_TYPE,
141 ARM_TYPE,
142 DATA_TYPE
143 };
144
bb0d3eb0 145 // Factory methods to create instruction templates in different formats.
b569affa
DK
146
147 static const Insn_template
148 thumb16_insn(uint32_t data)
149 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
150
bb0d3eb0
DK
151 // A Thumb conditional branch, in which the proper condition is inserted
152 // when we build the stub.
b569affa
DK
153 static const Insn_template
154 thumb16_bcond_insn(uint32_t data)
bb0d3eb0 155 { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); }
b569affa
DK
156
157 static const Insn_template
158 thumb32_insn(uint32_t data)
159 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
160
161 static const Insn_template
162 thumb32_b_insn(uint32_t data, int reloc_addend)
163 {
164 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
165 reloc_addend);
166 }
167
168 static const Insn_template
169 arm_insn(uint32_t data)
170 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
171
172 static const Insn_template
173 arm_rel_insn(unsigned data, int reloc_addend)
174 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
175
176 static const Insn_template
177 data_word(unsigned data, unsigned int r_type, int reloc_addend)
178 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
179
180 // Accessors. This class is used for read-only objects so no modifiers
181 // are provided.
182
183 uint32_t
184 data() const
185 { return this->data_; }
186
187 // Return the instruction sequence type of this.
188 Type
189 type() const
190 { return this->type_; }
191
192 // Return the ARM relocation type of this.
193 unsigned int
194 r_type() const
195 { return this->r_type_; }
196
197 int32_t
198 reloc_addend() const
199 { return this->reloc_addend_; }
200
bb0d3eb0 201 // Return size of instruction template in bytes.
b569affa
DK
202 size_t
203 size() const;
204
bb0d3eb0 205 // Return byte-alignment of instruction template.
b569affa
DK
206 unsigned
207 alignment() const;
208
209 private:
210 // We make the constructor private to ensure that only the factory
211 // methods are used.
212 inline
2ea97941
ILT
213 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
214 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
b569affa
DK
215 { }
216
217 // Instruction specific data. This is used to store information like
218 // some of the instruction bits.
219 uint32_t data_;
220 // Instruction template type.
221 Type type_;
222 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
223 unsigned int r_type_;
224 // Relocation addend.
225 int32_t reloc_addend_;
226};
227
228// Macro for generating code to stub types. One entry per long/short
229// branch stub
230
231#define DEF_STUBS \
232 DEF_STUB(long_branch_any_any) \
233 DEF_STUB(long_branch_v4t_arm_thumb) \
234 DEF_STUB(long_branch_thumb_only) \
235 DEF_STUB(long_branch_v4t_thumb_thumb) \
236 DEF_STUB(long_branch_v4t_thumb_arm) \
237 DEF_STUB(short_branch_v4t_thumb_arm) \
238 DEF_STUB(long_branch_any_arm_pic) \
239 DEF_STUB(long_branch_any_thumb_pic) \
240 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
241 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
242 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
243 DEF_STUB(long_branch_thumb_only_pic) \
244 DEF_STUB(a8_veneer_b_cond) \
245 DEF_STUB(a8_veneer_b) \
246 DEF_STUB(a8_veneer_bl) \
a2162063
ILT
247 DEF_STUB(a8_veneer_blx) \
248 DEF_STUB(v4_veneer_bx)
b569affa
DK
249
250// Stub types.
251
252#define DEF_STUB(x) arm_stub_##x,
253typedef enum
254 {
255 arm_stub_none,
256 DEF_STUBS
257
258 // First reloc stub type.
259 arm_stub_reloc_first = arm_stub_long_branch_any_any,
260 // Last reloc stub type.
261 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
262
263 // First Cortex-A8 stub type.
264 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
265 // Last Cortex-A8 stub type.
266 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
267
268 // Last stub type.
a2162063 269 arm_stub_type_last = arm_stub_v4_veneer_bx
b569affa
DK
270 } Stub_type;
271#undef DEF_STUB
272
273// Stub template class. Templates are meant to be read-only objects.
274// A stub template for a stub type contains all read-only attributes
275// common to all stubs of the same type.
276
277class Stub_template
278{
279 public:
280 Stub_template(Stub_type, const Insn_template*, size_t);
281
282 ~Stub_template()
283 { }
284
285 // Return stub type.
286 Stub_type
287 type() const
288 { return this->type_; }
289
290 // Return an array of instruction templates.
291 const Insn_template*
292 insns() const
293 { return this->insns_; }
294
295 // Return size of template in number of instructions.
296 size_t
297 insn_count() const
298 { return this->insn_count_; }
299
300 // Return size of template in bytes.
301 size_t
302 size() const
303 { return this->size_; }
304
305 // Return alignment of the stub template.
306 unsigned
307 alignment() const
308 { return this->alignment_; }
309
310 // Return whether entry point is in thumb mode.
311 bool
312 entry_in_thumb_mode() const
313 { return this->entry_in_thumb_mode_; }
314
315 // Return number of relocations in this template.
316 size_t
317 reloc_count() const
318 { return this->relocs_.size(); }
319
320 // Return index of the I-th instruction with relocation.
321 size_t
322 reloc_insn_index(size_t i) const
323 {
324 gold_assert(i < this->relocs_.size());
325 return this->relocs_[i].first;
326 }
327
328 // Return the offset of the I-th instruction with relocation from the
329 // beginning of the stub.
330 section_size_type
331 reloc_offset(size_t i) const
332 {
333 gold_assert(i < this->relocs_.size());
334 return this->relocs_[i].second;
335 }
336
337 private:
338 // This contains information about an instruction template with a relocation
339 // and its offset from start of stub.
340 typedef std::pair<size_t, section_size_type> Reloc;
341
342 // A Stub_template may not be copied. We want to share templates as much
343 // as possible.
344 Stub_template(const Stub_template&);
345 Stub_template& operator=(const Stub_template&);
346
347 // Stub type.
348 Stub_type type_;
349 // Points to an array of Insn_templates.
350 const Insn_template* insns_;
351 // Number of Insn_templates in insns_[].
352 size_t insn_count_;
353 // Size of templated instructions in bytes.
354 size_t size_;
355 // Alignment of templated instructions.
356 unsigned alignment_;
357 // Flag to indicate if entry is in thumb mode.
358 bool entry_in_thumb_mode_;
359 // A table of reloc instruction indices and offsets. We can find these by
360 // looking at the instruction templates but we pre-compute and then stash
361 // them here for speed.
362 std::vector<Reloc> relocs_;
363};
364
365//
366// A class for code stubs. This is a base class for different type of
367// stubs used in the ARM target.
368//
369
370class Stub
371{
372 private:
373 static const section_offset_type invalid_offset =
374 static_cast<section_offset_type>(-1);
375
376 public:
2ea97941
ILT
377 Stub(const Stub_template* stub_template)
378 : stub_template_(stub_template), offset_(invalid_offset)
b569affa
DK
379 { }
380
381 virtual
382 ~Stub()
383 { }
384
385 // Return the stub template.
386 const Stub_template*
387 stub_template() const
388 { return this->stub_template_; }
389
390 // Return offset of code stub from beginning of its containing stub table.
391 section_offset_type
392 offset() const
393 {
394 gold_assert(this->offset_ != invalid_offset);
395 return this->offset_;
396 }
397
398 // Set offset of code stub from beginning of its containing stub table.
399 void
2ea97941
ILT
400 set_offset(section_offset_type offset)
401 { this->offset_ = offset; }
b569affa
DK
402
403 // Return the relocation target address of the i-th relocation in the
404 // stub. This must be defined in a child class.
405 Arm_address
406 reloc_target(size_t i)
407 { return this->do_reloc_target(i); }
408
409 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
410 void
411 write(unsigned char* view, section_size_type view_size, bool big_endian)
412 { this->do_write(view, view_size, big_endian); }
413
bb0d3eb0
DK
414 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
415 // for the i-th instruction.
416 uint16_t
417 thumb16_special(size_t i)
418 { return this->do_thumb16_special(i); }
419
b569affa
DK
420 protected:
421 // This must be defined in the child class.
422 virtual Arm_address
423 do_reloc_target(size_t) = 0;
424
bb0d3eb0 425 // This may be overridden in the child class.
b569affa 426 virtual void
bb0d3eb0
DK
427 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
428 {
429 if (big_endian)
430 this->do_fixed_endian_write<true>(view, view_size);
431 else
432 this->do_fixed_endian_write<false>(view, view_size);
433 }
b569affa 434
bb0d3eb0
DK
435 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
436 // instruction template.
437 virtual uint16_t
438 do_thumb16_special(size_t)
439 { gold_unreachable(); }
440
b569affa 441 private:
bb0d3eb0
DK
442 // A template to implement do_write.
443 template<bool big_endian>
444 void inline
445 do_fixed_endian_write(unsigned char*, section_size_type);
446
b569affa
DK
447 // Its template.
448 const Stub_template* stub_template_;
449 // Offset within the section of containing this stub.
450 section_offset_type offset_;
451};
452
453// Reloc stub class. These are stubs we use to fix up relocation because
454// of limited branch ranges.
455
456class Reloc_stub : public Stub
457{
458 public:
459 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
460 // We assume we never jump to this address.
461 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
462
463 // Return destination address.
464 Arm_address
465 destination_address() const
466 {
467 gold_assert(this->destination_address_ != this->invalid_address);
468 return this->destination_address_;
469 }
470
471 // Set destination address.
472 void
473 set_destination_address(Arm_address address)
474 {
475 gold_assert(address != this->invalid_address);
476 this->destination_address_ = address;
477 }
478
479 // Reset destination address.
480 void
481 reset_destination_address()
482 { this->destination_address_ = this->invalid_address; }
483
484 // Determine stub type for a branch of a relocation of R_TYPE going
485 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
486 // the branch target is a thumb instruction. TARGET is used for look
487 // up ARM-specific linker settings.
488 static Stub_type
489 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
490 Arm_address branch_target, bool target_is_thumb);
491
492 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
493 // and an addend. Since we treat global and local symbol differently, we
494 // use a Symbol object for a global symbol and a object-index pair for
495 // a local symbol.
496 class Key
497 {
498 public:
499 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
500 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
501 // and R_SYM must not be invalid_index.
2ea97941
ILT
502 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
503 unsigned int r_sym, int32_t addend)
504 : stub_type_(stub_type), addend_(addend)
b569affa 505 {
2ea97941 506 if (symbol != NULL)
b569affa
DK
507 {
508 this->r_sym_ = Reloc_stub::invalid_index;
2ea97941 509 this->u_.symbol = symbol;
b569affa
DK
510 }
511 else
512 {
2ea97941
ILT
513 gold_assert(relobj != NULL && r_sym != invalid_index);
514 this->r_sym_ = r_sym;
515 this->u_.relobj = relobj;
b569affa
DK
516 }
517 }
518
519 ~Key()
520 { }
521
522 // Accessors: Keys are meant to be read-only object so no modifiers are
523 // provided.
524
525 // Return stub type.
526 Stub_type
527 stub_type() const
528 { return this->stub_type_; }
529
530 // Return the local symbol index or invalid_index.
531 unsigned int
532 r_sym() const
533 { return this->r_sym_; }
534
535 // Return the symbol if there is one.
536 const Symbol*
537 symbol() const
538 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
539
540 // Return the relobj if there is one.
541 const Relobj*
542 relobj() const
543 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
544
545 // Whether this equals to another key k.
546 bool
547 eq(const Key& k) const
548 {
549 return ((this->stub_type_ == k.stub_type_)
550 && (this->r_sym_ == k.r_sym_)
551 && ((this->r_sym_ != Reloc_stub::invalid_index)
552 ? (this->u_.relobj == k.u_.relobj)
553 : (this->u_.symbol == k.u_.symbol))
554 && (this->addend_ == k.addend_));
555 }
556
557 // Return a hash value.
558 size_t
559 hash_value() const
560 {
561 return (this->stub_type_
562 ^ this->r_sym_
563 ^ gold::string_hash<char>(
564 (this->r_sym_ != Reloc_stub::invalid_index)
565 ? this->u_.relobj->name().c_str()
566 : this->u_.symbol->name())
567 ^ this->addend_);
568 }
569
570 // Functors for STL associative containers.
571 struct hash
572 {
573 size_t
574 operator()(const Key& k) const
575 { return k.hash_value(); }
576 };
577
578 struct equal_to
579 {
580 bool
581 operator()(const Key& k1, const Key& k2) const
582 { return k1.eq(k2); }
583 };
584
585 // Name of key. This is mainly for debugging.
586 std::string
587 name() const;
588
589 private:
590 // Stub type.
591 Stub_type stub_type_;
592 // If this is a local symbol, this is the index in the defining object.
593 // Otherwise, it is invalid_index for a global symbol.
594 unsigned int r_sym_;
595 // If r_sym_ is invalid index. This points to a global symbol.
596 // Otherwise, this points a relobj. We used the unsized and target
eb44217c 597 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
b569affa
DK
598 // Arm_relobj. This is done to avoid making the stub class a template
599 // as most of the stub machinery is endianity-neutral. However, it
600 // may require a bit of casting done by users of this class.
601 union
602 {
603 const Symbol* symbol;
604 const Relobj* relobj;
605 } u_;
606 // Addend associated with a reloc.
607 int32_t addend_;
608 };
609
610 protected:
611 // Reloc_stubs are created via a stub factory. So these are protected.
2ea97941
ILT
612 Reloc_stub(const Stub_template* stub_template)
613 : Stub(stub_template), destination_address_(invalid_address)
b569affa
DK
614 { }
615
616 ~Reloc_stub()
617 { }
618
619 friend class Stub_factory;
620
b569affa
DK
621 // Return the relocation target address of the i-th relocation in the
622 // stub.
623 Arm_address
624 do_reloc_target(size_t i)
625 {
626 // All reloc stub have only one relocation.
627 gold_assert(i == 0);
628 return this->destination_address_;
629 }
630
bb0d3eb0
DK
631 private:
632 // Address of destination.
633 Arm_address destination_address_;
634};
b569affa 635
bb0d3eb0
DK
636// Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
637// THUMB branch that meets the following conditions:
638//
639// 1. The branch straddles across a page boundary. i.e. lower 12-bit of
640// branch address is 0xffe.
641// 2. The branch target address is in the same page as the first word of the
642// branch.
643// 3. The branch follows a 32-bit instruction which is not a branch.
644//
645// To do the fix up, we need to store the address of the branch instruction
646// and its target at least. We also need to store the original branch
647// instruction bits for the condition code in a conditional branch. The
648// condition code is used in a special instruction template. We also want
649// to identify input sections needing Cortex-A8 workaround quickly. We store
650// extra information about object and section index of the code section
651// containing a branch being fixed up. The information is used to mark
652// the code section when we finalize the Cortex-A8 stubs.
653//
b569affa 654
bb0d3eb0
DK
655class Cortex_a8_stub : public Stub
656{
657 public:
658 ~Cortex_a8_stub()
659 { }
660
661 // Return the object of the code section containing the branch being fixed
662 // up.
663 Relobj*
664 relobj() const
665 { return this->relobj_; }
666
667 // Return the section index of the code section containing the branch being
668 // fixed up.
669 unsigned int
670 shndx() const
671 { return this->shndx_; }
672
673 // Return the source address of stub. This is the address of the original
674 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
675 // instruction.
676 Arm_address
677 source_address() const
678 { return this->source_address_; }
679
680 // Return the destination address of the stub. This is the branch taken
681 // address of the original branch instruction. LSB is 1 if it is a THUMB
682 // instruction address.
683 Arm_address
684 destination_address() const
685 { return this->destination_address_; }
686
687 // Return the instruction being fixed up.
688 uint32_t
689 original_insn() const
690 { return this->original_insn_; }
691
692 protected:
693 // Cortex_a8_stubs are created via a stub factory. So these are protected.
694 Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
695 unsigned int shndx, Arm_address source_address,
696 Arm_address destination_address, uint32_t original_insn)
697 : Stub(stub_template), relobj_(relobj), shndx_(shndx),
698 source_address_(source_address | 1U),
699 destination_address_(destination_address),
700 original_insn_(original_insn)
701 { }
702
703 friend class Stub_factory;
704
705 // Return the relocation target address of the i-th relocation in the
706 // stub.
707 Arm_address
708 do_reloc_target(size_t i)
709 {
710 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
711 {
712 // The conditional branch veneer has two relocations.
713 gold_assert(i < 2);
714 return i == 0 ? this->source_address_ + 4 : this->destination_address_;
715 }
716 else
717 {
718 // All other Cortex-A8 stubs have only one relocation.
719 gold_assert(i == 0);
720 return this->destination_address_;
721 }
722 }
723
724 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
725 uint16_t
726 do_thumb16_special(size_t);
727
728 private:
729 // Object of the code section containing the branch being fixed up.
730 Relobj* relobj_;
731 // Section index of the code section containing the branch begin fixed up.
732 unsigned int shndx_;
733 // Source address of original branch.
734 Arm_address source_address_;
735 // Destination address of the original branch.
b569affa 736 Arm_address destination_address_;
bb0d3eb0
DK
737 // Original branch instruction. This is needed for copying the condition
738 // code from a condition branch to its stub.
739 uint32_t original_insn_;
b569affa
DK
740};
741
a2162063
ILT
742// ARMv4 BX Rx branch relocation stub class.
743class Arm_v4bx_stub : public Stub
744{
745 public:
746 ~Arm_v4bx_stub()
747 { }
748
749 // Return the associated register.
750 uint32_t
751 reg() const
752 { return this->reg_; }
753
754 protected:
755 // Arm V4BX stubs are created via a stub factory. So these are protected.
756 Arm_v4bx_stub(const Stub_template* stub_template, const uint32_t reg)
757 : Stub(stub_template), reg_(reg)
758 { }
759
760 friend class Stub_factory;
761
762 // Return the relocation target address of the i-th relocation in the
763 // stub.
764 Arm_address
765 do_reloc_target(size_t)
766 { gold_unreachable(); }
767
768 // This may be overridden in the child class.
769 virtual void
770 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
771 {
772 if (big_endian)
773 this->do_fixed_endian_v4bx_write<true>(view, view_size);
774 else
775 this->do_fixed_endian_v4bx_write<false>(view, view_size);
776 }
777
778 private:
779 // A template to implement do_write.
780 template<bool big_endian>
781 void inline
782 do_fixed_endian_v4bx_write(unsigned char* view, section_size_type)
783 {
784 const Insn_template* insns = this->stub_template()->insns();
785 elfcpp::Swap<32, big_endian>::writeval(view,
786 (insns[0].data()
787 + (this->reg_ << 16)));
788 view += insns[0].size();
789 elfcpp::Swap<32, big_endian>::writeval(view,
790 (insns[1].data() + this->reg_));
791 view += insns[1].size();
792 elfcpp::Swap<32, big_endian>::writeval(view,
793 (insns[2].data() + this->reg_));
794 }
795
796 // A register index (r0-r14), which is associated with the stub.
797 uint32_t reg_;
798};
799
b569affa
DK
800// Stub factory class.
801
802class Stub_factory
803{
804 public:
805 // Return the unique instance of this class.
806 static const Stub_factory&
807 get_instance()
808 {
809 static Stub_factory singleton;
810 return singleton;
811 }
812
813 // Make a relocation stub.
814 Reloc_stub*
815 make_reloc_stub(Stub_type stub_type) const
816 {
817 gold_assert(stub_type >= arm_stub_reloc_first
818 && stub_type <= arm_stub_reloc_last);
819 return new Reloc_stub(this->stub_templates_[stub_type]);
820 }
821
bb0d3eb0
DK
822 // Make a Cortex-A8 stub.
823 Cortex_a8_stub*
824 make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
825 Arm_address source, Arm_address destination,
826 uint32_t original_insn) const
827 {
828 gold_assert(stub_type >= arm_stub_cortex_a8_first
829 && stub_type <= arm_stub_cortex_a8_last);
830 return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
831 source, destination, original_insn);
832 }
833
a2162063
ILT
834 // Make an ARM V4BX relocation stub.
835 // This method creates a stub from the arm_stub_v4_veneer_bx template only.
836 Arm_v4bx_stub*
837 make_arm_v4bx_stub(uint32_t reg) const
838 {
839 gold_assert(reg < 0xf);
840 return new Arm_v4bx_stub(this->stub_templates_[arm_stub_v4_veneer_bx],
841 reg);
842 }
843
b569affa
DK
844 private:
845 // Constructor and destructor are protected since we only return a single
846 // instance created in Stub_factory::get_instance().
847
848 Stub_factory();
849
850 // A Stub_factory may not be copied since it is a singleton.
851 Stub_factory(const Stub_factory&);
852 Stub_factory& operator=(Stub_factory&);
853
854 // Stub templates. These are initialized in the constructor.
855 const Stub_template* stub_templates_[arm_stub_type_last+1];
856};
857
56ee5e00
DK
858// A class to hold stubs for the ARM target.
859
860template<bool big_endian>
861class Stub_table : public Output_data
862{
863 public:
2ea97941 864 Stub_table(Arm_input_section<big_endian>* owner)
2fb7225c 865 : Output_data(), owner_(owner), reloc_stubs_(), cortex_a8_stubs_(),
a2162063 866 arm_v4bx_stubs_(0xf), prev_data_size_(0), prev_addralign_(1)
56ee5e00
DK
867 { }
868
869 ~Stub_table()
870 { }
871
872 // Owner of this stub table.
873 Arm_input_section<big_endian>*
874 owner() const
875 { return this->owner_; }
876
877 // Whether this stub table is empty.
878 bool
879 empty() const
a2162063
ILT
880 {
881 return (this->reloc_stubs_.empty()
882 && this->cortex_a8_stubs_.empty()
883 && this->arm_v4bx_stubs_.empty());
884 }
56ee5e00
DK
885
886 // Return the current data size.
887 off_t
888 current_data_size() const
889 { return this->current_data_size_for_child(); }
890
891 // Add a STUB with using KEY. Caller is reponsible for avoid adding
892 // if already a STUB with the same key has been added.
893 void
2fb7225c
DK
894 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
895 {
896 const Stub_template* stub_template = stub->stub_template();
897 gold_assert(stub_template->type() == key.stub_type());
898 this->reloc_stubs_[key] = stub;
899 }
900
901 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
902 // Caller is reponsible for avoid adding if already a STUB with the same
903 // address has been added.
904 void
905 add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
906 {
907 std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
908 this->cortex_a8_stubs_.insert(value);
909 }
910
a2162063
ILT
911 // Add an ARM V4BX relocation stub. A register index will be retrieved
912 // from the stub.
913 void
914 add_arm_v4bx_stub(Arm_v4bx_stub* stub)
915 {
916 gold_assert(stub != NULL && this->arm_v4bx_stubs_[stub->reg()] == NULL);
917 this->arm_v4bx_stubs_[stub->reg()] = stub;
918 }
919
2fb7225c
DK
920 // Remove all Cortex-A8 stubs.
921 void
922 remove_all_cortex_a8_stubs();
56ee5e00
DK
923
924 // Look up a relocation stub using KEY. Return NULL if there is none.
925 Reloc_stub*
926 find_reloc_stub(const Reloc_stub::Key& key) const
927 {
928 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
929 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
930 }
931
a2162063
ILT
932 // Look up an arm v4bx relocation stub using the register index.
933 // Return NULL if there is none.
934 Arm_v4bx_stub*
935 find_arm_v4bx_stub(const uint32_t reg) const
936 {
937 gold_assert(reg < 0xf);
938 return this->arm_v4bx_stubs_[reg];
939 }
940
56ee5e00
DK
941 // Relocate stubs in this stub table.
942 void
943 relocate_stubs(const Relocate_info<32, big_endian>*,
944 Target_arm<big_endian>*, Output_section*,
945 unsigned char*, Arm_address, section_size_type);
946
2fb7225c
DK
947 // Update data size and alignment at the end of a relaxation pass. Return
948 // true if either data size or alignment is different from that of the
949 // previous relaxation pass.
950 bool
951 update_data_size_and_addralign();
952
953 // Finalize stubs. Set the offsets of all stubs and mark input sections
954 // needing the Cortex-A8 workaround.
955 void
956 finalize_stubs();
957
958 // Apply Cortex-A8 workaround to an address range.
959 void
960 apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
961 unsigned char*, Arm_address,
962 section_size_type);
963
56ee5e00
DK
964 protected:
965 // Write out section contents.
966 void
967 do_write(Output_file*);
968
969 // Return the required alignment.
970 uint64_t
971 do_addralign() const
2fb7225c 972 { return this->prev_addralign_; }
56ee5e00
DK
973
974 // Reset address and file offset.
975 void
2fb7225c
DK
976 do_reset_address_and_file_offset()
977 { this->set_current_data_size_for_child(this->prev_data_size_); }
56ee5e00 978
2fb7225c
DK
979 // Set final data size.
980 void
981 set_final_data_size()
982 { this->set_data_size(this->current_data_size()); }
983
56ee5e00 984 private:
2fb7225c
DK
985 // Relocate one stub.
986 void
987 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
988 Target_arm<big_endian>*, Output_section*,
989 unsigned char*, Arm_address, section_size_type);
990
991 // Unordered map of relocation stubs.
56ee5e00
DK
992 typedef
993 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
994 Reloc_stub::Key::equal_to>
995 Reloc_stub_map;
996
2fb7225c
DK
997 // List of Cortex-A8 stubs ordered by addresses of branches being
998 // fixed up in output.
999 typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
a2162063
ILT
1000 // List of Arm V4BX relocation stubs ordered by associated registers.
1001 typedef std::vector<Arm_v4bx_stub*> Arm_v4bx_stub_list;
2fb7225c 1002
56ee5e00
DK
1003 // Owner of this stub table.
1004 Arm_input_section<big_endian>* owner_;
56ee5e00
DK
1005 // The relocation stubs.
1006 Reloc_stub_map reloc_stubs_;
2fb7225c
DK
1007 // The cortex_a8_stubs.
1008 Cortex_a8_stub_list cortex_a8_stubs_;
a2162063
ILT
1009 // The Arm V4BX relocation stubs.
1010 Arm_v4bx_stub_list arm_v4bx_stubs_;
2fb7225c
DK
1011 // data size of this in the previous pass.
1012 off_t prev_data_size_;
1013 // address alignment of this in the previous pass.
1014 uint64_t prev_addralign_;
56ee5e00
DK
1015};
1016
af2cdeae
DK
1017// Arm_exidx_cantunwind class. This represents an EXIDX_CANTUNWIND entry
1018// we add to the end of an EXIDX input section that goes into the output.
1019
1020class Arm_exidx_cantunwind : public Output_section_data
1021{
1022 public:
1023 Arm_exidx_cantunwind(Relobj* relobj, unsigned int shndx)
1024 : Output_section_data(8, 4, true), relobj_(relobj), shndx_(shndx)
1025 { }
1026
1027 // Return the object containing the section pointed by this.
1028 Relobj*
1029 relobj() const
1030 { return this->relobj_; }
1031
1032 // Return the section index of the section pointed by this.
1033 unsigned int
1034 shndx() const
1035 { return this->shndx_; }
1036
1037 protected:
1038 void
1039 do_write(Output_file* of)
1040 {
1041 if (parameters->target().is_big_endian())
1042 this->do_fixed_endian_write<true>(of);
1043 else
1044 this->do_fixed_endian_write<false>(of);
1045 }
1046
1047 private:
1048 // Implement do_write for a given endianity.
1049 template<bool big_endian>
1050 void inline
1051 do_fixed_endian_write(Output_file*);
1052
1053 // The object containing the section pointed by this.
1054 Relobj* relobj_;
1055 // The section index of the section pointed by this.
1056 unsigned int shndx_;
1057};
1058
1059// During EXIDX coverage fix-up, we compact an EXIDX section. The
1060// Offset map is used to map input section offset within the EXIDX section
1061// to the output offset from the start of this EXIDX section.
1062
1063typedef std::map<section_offset_type, section_offset_type>
1064 Arm_exidx_section_offset_map;
1065
1066// Arm_exidx_merged_section class. This represents an EXIDX input section
1067// with some of its entries merged.
1068
1069class Arm_exidx_merged_section : public Output_relaxed_input_section
1070{
1071 public:
1072 // Constructor for Arm_exidx_merged_section.
1073 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1074 // SECTION_OFFSET_MAP points to a section offset map describing how
1075 // parts of the input section are mapped to output. DELETED_BYTES is
1076 // the number of bytes deleted from the EXIDX input section.
1077 Arm_exidx_merged_section(
1078 const Arm_exidx_input_section& exidx_input_section,
1079 const Arm_exidx_section_offset_map& section_offset_map,
1080 uint32_t deleted_bytes);
1081
1082 // Return the original EXIDX input section.
1083 const Arm_exidx_input_section&
1084 exidx_input_section() const
1085 { return this->exidx_input_section_; }
1086
1087 // Return the section offset map.
1088 const Arm_exidx_section_offset_map&
1089 section_offset_map() const
1090 { return this->section_offset_map_; }
1091
1092 protected:
1093 // Write merged section into file OF.
1094 void
1095 do_write(Output_file* of);
1096
1097 bool
1098 do_output_offset(const Relobj*, unsigned int, section_offset_type,
1099 section_offset_type*) const;
1100
1101 private:
1102 // Original EXIDX input section.
1103 const Arm_exidx_input_section& exidx_input_section_;
1104 // Section offset map.
1105 const Arm_exidx_section_offset_map& section_offset_map_;
1106};
1107
10ad9fe5
DK
1108// A class to wrap an ordinary input section containing executable code.
1109
1110template<bool big_endian>
1111class Arm_input_section : public Output_relaxed_input_section
1112{
1113 public:
2ea97941
ILT
1114 Arm_input_section(Relobj* relobj, unsigned int shndx)
1115 : Output_relaxed_input_section(relobj, shndx, 1),
10ad9fe5
DK
1116 original_addralign_(1), original_size_(0), stub_table_(NULL)
1117 { }
1118
1119 ~Arm_input_section()
1120 { }
1121
1122 // Initialize.
1123 void
1124 init();
1125
1126 // Whether this is a stub table owner.
1127 bool
1128 is_stub_table_owner() const
1129 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1130
1131 // Return the stub table.
1132 Stub_table<big_endian>*
1133 stub_table() const
1134 { return this->stub_table_; }
1135
1136 // Set the stub_table.
1137 void
2ea97941
ILT
1138 set_stub_table(Stub_table<big_endian>* stub_table)
1139 { this->stub_table_ = stub_table; }
10ad9fe5 1140
07f508a2
DK
1141 // Downcast a base pointer to an Arm_input_section pointer. This is
1142 // not type-safe but we only use Arm_input_section not the base class.
1143 static Arm_input_section<big_endian>*
1144 as_arm_input_section(Output_relaxed_input_section* poris)
1145 { return static_cast<Arm_input_section<big_endian>*>(poris); }
1146
10ad9fe5
DK
1147 protected:
1148 // Write data to output file.
1149 void
1150 do_write(Output_file*);
1151
1152 // Return required alignment of this.
1153 uint64_t
1154 do_addralign() const
1155 {
1156 if (this->is_stub_table_owner())
1157 return std::max(this->stub_table_->addralign(),
1158 this->original_addralign_);
1159 else
1160 return this->original_addralign_;
1161 }
1162
1163 // Finalize data size.
1164 void
1165 set_final_data_size();
1166
1167 // Reset address and file offset.
1168 void
1169 do_reset_address_and_file_offset();
1170
1171 // Output offset.
1172 bool
2ea97941
ILT
1173 do_output_offset(const Relobj* object, unsigned int shndx,
1174 section_offset_type offset,
10ad9fe5
DK
1175 section_offset_type* poutput) const
1176 {
1177 if ((object == this->relobj())
2ea97941
ILT
1178 && (shndx == this->shndx())
1179 && (offset >= 0)
1180 && (convert_types<uint64_t, section_offset_type>(offset)
10ad9fe5
DK
1181 <= this->original_size_))
1182 {
2ea97941 1183 *poutput = offset;
10ad9fe5
DK
1184 return true;
1185 }
1186 else
1187 return false;
1188 }
1189
1190 private:
1191 // Copying is not allowed.
1192 Arm_input_section(const Arm_input_section&);
1193 Arm_input_section& operator=(const Arm_input_section&);
1194
1195 // Address alignment of the original input section.
1196 uint64_t original_addralign_;
1197 // Section size of the original input section.
1198 uint64_t original_size_;
1199 // Stub table.
1200 Stub_table<big_endian>* stub_table_;
1201};
1202
80d0d023
DK
1203// Arm_exidx_fixup class. This is used to define a number of methods
1204// and keep states for fixing up EXIDX coverage.
1205
1206class Arm_exidx_fixup
1207{
1208 public:
1209 Arm_exidx_fixup(Output_section* exidx_output_section)
1210 : exidx_output_section_(exidx_output_section), last_unwind_type_(UT_NONE),
1211 last_inlined_entry_(0), last_input_section_(NULL),
546c7457 1212 section_offset_map_(NULL), first_output_text_section_(NULL)
80d0d023
DK
1213 { }
1214
1215 ~Arm_exidx_fixup()
1216 { delete this->section_offset_map_; }
1217
1218 // Process an EXIDX section for entry merging. Return number of bytes to
1219 // be deleted in output. If parts of the input EXIDX section are merged
1220 // a heap allocated Arm_exidx_section_offset_map is store in the located
1221 // PSECTION_OFFSET_MAP. The caller owns the map and is reponsible for
1222 // releasing it.
1223 template<bool big_endian>
1224 uint32_t
1225 process_exidx_section(const Arm_exidx_input_section* exidx_input_section,
1226 Arm_exidx_section_offset_map** psection_offset_map);
1227
1228 // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1229 // input section, if there is not one already.
1230 void
1231 add_exidx_cantunwind_as_needed();
1232
546c7457
DK
1233 // Return the output section for the text section which is linked to the
1234 // first exidx input in output.
1235 Output_section*
1236 first_output_text_section() const
1237 { return this->first_output_text_section_; }
1238
80d0d023
DK
1239 private:
1240 // Copying is not allowed.
1241 Arm_exidx_fixup(const Arm_exidx_fixup&);
1242 Arm_exidx_fixup& operator=(const Arm_exidx_fixup&);
1243
1244 // Type of EXIDX unwind entry.
1245 enum Unwind_type
1246 {
1247 // No type.
1248 UT_NONE,
1249 // EXIDX_CANTUNWIND.
1250 UT_EXIDX_CANTUNWIND,
1251 // Inlined entry.
1252 UT_INLINED_ENTRY,
1253 // Normal entry.
1254 UT_NORMAL_ENTRY,
1255 };
1256
1257 // Process an EXIDX entry. We only care about the second word of the
1258 // entry. Return true if the entry can be deleted.
1259 bool
1260 process_exidx_entry(uint32_t second_word);
1261
1262 // Update the current section offset map during EXIDX section fix-up.
1263 // If there is no map, create one. INPUT_OFFSET is the offset of a
1264 // reference point, DELETED_BYTES is the number of deleted by in the
1265 // section so far. If DELETE_ENTRY is true, the reference point and
1266 // all offsets after the previous reference point are discarded.
1267 void
1268 update_offset_map(section_offset_type input_offset,
1269 section_size_type deleted_bytes, bool delete_entry);
1270
1271 // EXIDX output section.
1272 Output_section* exidx_output_section_;
1273 // Unwind type of the last EXIDX entry processed.
1274 Unwind_type last_unwind_type_;
1275 // Last seen inlined EXIDX entry.
1276 uint32_t last_inlined_entry_;
1277 // Last processed EXIDX input section.
2b328d4e 1278 const Arm_exidx_input_section* last_input_section_;
80d0d023
DK
1279 // Section offset map created in process_exidx_section.
1280 Arm_exidx_section_offset_map* section_offset_map_;
546c7457
DK
1281 // Output section for the text section which is linked to the first exidx
1282 // input in output.
1283 Output_section* first_output_text_section_;
80d0d023
DK
1284};
1285
07f508a2
DK
1286// Arm output section class. This is defined mainly to add a number of
1287// stub generation methods.
1288
1289template<bool big_endian>
1290class Arm_output_section : public Output_section
1291{
1292 public:
2b328d4e
DK
1293 typedef std::vector<std::pair<Relobj*, unsigned int> > Text_section_list;
1294
2ea97941
ILT
1295 Arm_output_section(const char* name, elfcpp::Elf_Word type,
1296 elfcpp::Elf_Xword flags)
1297 : Output_section(name, type, flags)
07f508a2
DK
1298 { }
1299
1300 ~Arm_output_section()
1301 { }
1302
1303 // Group input sections for stub generation.
1304 void
1305 group_sections(section_size_type, bool, Target_arm<big_endian>*);
1306
1307 // Downcast a base pointer to an Arm_output_section pointer. This is
1308 // not type-safe but we only use Arm_output_section not the base class.
1309 static Arm_output_section<big_endian>*
1310 as_arm_output_section(Output_section* os)
1311 { return static_cast<Arm_output_section<big_endian>*>(os); }
1312
2b328d4e
DK
1313 // Append all input text sections in this into LIST.
1314 void
1315 append_text_sections_to_list(Text_section_list* list);
1316
1317 // Fix EXIDX coverage of this EXIDX output section. SORTED_TEXT_SECTION
1318 // is a list of text input sections sorted in ascending order of their
1319 // output addresses.
1320 void
1321 fix_exidx_coverage(const Text_section_list& sorted_text_section,
1322 Symbol_table* symtab);
1323
07f508a2
DK
1324 private:
1325 // For convenience.
1326 typedef Output_section::Input_section Input_section;
1327 typedef Output_section::Input_section_list Input_section_list;
1328
1329 // Create a stub group.
1330 void create_stub_group(Input_section_list::const_iterator,
1331 Input_section_list::const_iterator,
1332 Input_section_list::const_iterator,
1333 Target_arm<big_endian>*,
1334 std::vector<Output_relaxed_input_section*>*);
1335};
1336
993d07c1
DK
1337// Arm_exidx_input_section class. This represents an EXIDX input section.
1338
1339class Arm_exidx_input_section
1340{
1341 public:
1342 static const section_offset_type invalid_offset =
1343 static_cast<section_offset_type>(-1);
1344
1345 Arm_exidx_input_section(Relobj* relobj, unsigned int shndx,
1346 unsigned int link, uint32_t size, uint32_t addralign)
1347 : relobj_(relobj), shndx_(shndx), link_(link), size_(size),
1348 addralign_(addralign)
1349 { }
1350
1351 ~Arm_exidx_input_section()
1352 { }
1353
1354 // Accessors: This is a read-only class.
1355
1356 // Return the object containing this EXIDX input section.
1357 Relobj*
1358 relobj() const
1359 { return this->relobj_; }
1360
1361 // Return the section index of this EXIDX input section.
1362 unsigned int
1363 shndx() const
1364 { return this->shndx_; }
1365
1366 // Return the section index of linked text section in the same object.
1367 unsigned int
1368 link() const
1369 { return this->link_; }
1370
1371 // Return size of the EXIDX input section.
1372 uint32_t
1373 size() const
1374 { return this->size_; }
1375
1376 // Reutnr address alignment of EXIDX input section.
1377 uint32_t
1378 addralign() const
1379 { return this->addralign_; }
1380
1381 private:
1382 // Object containing this.
1383 Relobj* relobj_;
1384 // Section index of this.
1385 unsigned int shndx_;
1386 // text section linked to this in the same object.
1387 unsigned int link_;
1388 // Size of this. For ARM 32-bit is sufficient.
1389 uint32_t size_;
1390 // Address alignment of this. For ARM 32-bit is sufficient.
1391 uint32_t addralign_;
1392};
1393
8ffa3667
DK
1394// Arm_relobj class.
1395
1396template<bool big_endian>
1397class Arm_relobj : public Sized_relobj<32, big_endian>
1398{
1399 public:
1400 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1401
2ea97941 1402 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
8ffa3667 1403 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
2ea97941 1404 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
a0351a69 1405 stub_tables_(), local_symbol_is_thumb_function_(),
20138696 1406 attributes_section_data_(NULL), mapping_symbols_info_(),
e7eca48c
DK
1407 section_has_cortex_a8_workaround_(NULL), exidx_section_map_(),
1408 output_local_symbol_count_needs_update_(false)
8ffa3667
DK
1409 { }
1410
1411 ~Arm_relobj()
a0351a69 1412 { delete this->attributes_section_data_; }
8ffa3667
DK
1413
1414 // Return the stub table of the SHNDX-th section if there is one.
1415 Stub_table<big_endian>*
2ea97941 1416 stub_table(unsigned int shndx) const
8ffa3667 1417 {
2ea97941
ILT
1418 gold_assert(shndx < this->stub_tables_.size());
1419 return this->stub_tables_[shndx];
8ffa3667
DK
1420 }
1421
1422 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1423 void
2ea97941 1424 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
8ffa3667 1425 {
2ea97941
ILT
1426 gold_assert(shndx < this->stub_tables_.size());
1427 this->stub_tables_[shndx] = stub_table;
8ffa3667
DK
1428 }
1429
1430 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1431 // index. This is only valid after do_count_local_symbol is called.
1432 bool
1433 local_symbol_is_thumb_function(unsigned int r_sym) const
1434 {
1435 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1436 return this->local_symbol_is_thumb_function_[r_sym];
1437 }
1438
1439 // Scan all relocation sections for stub generation.
1440 void
1441 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1442 const Layout*);
1443
1444 // Convert regular input section with index SHNDX to a relaxed section.
1445 void
2ea97941 1446 convert_input_section_to_relaxed_section(unsigned shndx)
8ffa3667
DK
1447 {
1448 // The stubs have relocations and we need to process them after writing
1449 // out the stubs. So relocation now must follow section write.
2b328d4e 1450 this->set_section_offset(shndx, -1ULL);
8ffa3667
DK
1451 this->set_relocs_must_follow_section_writes();
1452 }
1453
1454 // Downcast a base pointer to an Arm_relobj pointer. This is
1455 // not type-safe but we only use Arm_relobj not the base class.
1456 static Arm_relobj<big_endian>*
2ea97941
ILT
1457 as_arm_relobj(Relobj* relobj)
1458 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
8ffa3667 1459
d5b40221
DK
1460 // Processor-specific flags in ELF file header. This is valid only after
1461 // reading symbols.
1462 elfcpp::Elf_Word
1463 processor_specific_flags() const
1464 { return this->processor_specific_flags_; }
1465
a0351a69
DK
1466 // Attribute section data This is the contents of the .ARM.attribute section
1467 // if there is one.
1468 const Attributes_section_data*
1469 attributes_section_data() const
1470 { return this->attributes_section_data_; }
1471
20138696
DK
1472 // Mapping symbol location.
1473 typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1474
1475 // Functor for STL container.
1476 struct Mapping_symbol_position_less
1477 {
1478 bool
1479 operator()(const Mapping_symbol_position& p1,
1480 const Mapping_symbol_position& p2) const
1481 {
1482 return (p1.first < p2.first
1483 || (p1.first == p2.first && p1.second < p2.second));
1484 }
1485 };
1486
1487 // We only care about the first character of a mapping symbol, so
1488 // we only store that instead of the whole symbol name.
1489 typedef std::map<Mapping_symbol_position, char,
1490 Mapping_symbol_position_less> Mapping_symbols_info;
1491
2fb7225c
DK
1492 // Whether a section contains any Cortex-A8 workaround.
1493 bool
1494 section_has_cortex_a8_workaround(unsigned int shndx) const
1495 {
1496 return (this->section_has_cortex_a8_workaround_ != NULL
1497 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1498 }
1499
1500 // Mark a section that has Cortex-A8 workaround.
1501 void
1502 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1503 {
1504 if (this->section_has_cortex_a8_workaround_ == NULL)
1505 this->section_has_cortex_a8_workaround_ =
1506 new std::vector<bool>(this->shnum(), false);
1507 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1508 }
1509
993d07c1
DK
1510 // Return the EXIDX section of an text section with index SHNDX or NULL
1511 // if the text section has no associated EXIDX section.
1512 const Arm_exidx_input_section*
1513 exidx_input_section_by_link(unsigned int shndx) const
1514 {
1515 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1516 return ((p != this->exidx_section_map_.end()
1517 && p->second->link() == shndx)
1518 ? p->second
1519 : NULL);
1520 }
1521
1522 // Return the EXIDX section with index SHNDX or NULL if there is none.
1523 const Arm_exidx_input_section*
1524 exidx_input_section_by_shndx(unsigned shndx) const
1525 {
1526 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1527 return ((p != this->exidx_section_map_.end()
1528 && p->second->shndx() == shndx)
1529 ? p->second
1530 : NULL);
1531 }
1532
e7eca48c
DK
1533 // Whether output local symbol count needs updating.
1534 bool
1535 output_local_symbol_count_needs_update() const
1536 { return this->output_local_symbol_count_needs_update_; }
1537
1538 // Set output_local_symbol_count_needs_update flag to be true.
1539 void
1540 set_output_local_symbol_count_needs_update()
1541 { this->output_local_symbol_count_needs_update_ = true; }
1542
1543 // Update output local symbol count at the end of relaxation.
1544 void
1545 update_output_local_symbol_count();
1546
8ffa3667
DK
1547 protected:
1548 // Post constructor setup.
1549 void
1550 do_setup()
1551 {
1552 // Call parent's setup method.
1553 Sized_relobj<32, big_endian>::do_setup();
1554
1555 // Initialize look-up tables.
1556 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1557 this->stub_tables_.swap(empty_stub_table_list);
1558 }
1559
1560 // Count the local symbols.
1561 void
1562 do_count_local_symbols(Stringpool_template<char>*,
1563 Stringpool_template<char>*);
1564
1565 void
43d12afe 1566 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
8ffa3667
DK
1567 const unsigned char* pshdrs,
1568 typename Sized_relobj<32, big_endian>::Views* pivews);
1569
d5b40221
DK
1570 // Read the symbol information.
1571 void
1572 do_read_symbols(Read_symbols_data* sd);
1573
99e5bff2
DK
1574 // Process relocs for garbage collection.
1575 void
1576 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1577
8ffa3667 1578 private:
44272192
DK
1579
1580 // Whether a section needs to be scanned for relocation stubs.
1581 bool
1582 section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1583 const Relobj::Output_sections&,
2b328d4e 1584 const Symbol_table *, const unsigned char*);
44272192 1585
cf846138
DK
1586 // Whether a section is a scannable text section.
1587 bool
1588 section_is_scannable(const elfcpp::Shdr<32, big_endian>&, unsigned int,
1589 const Output_section*, const Symbol_table *);
1590
44272192
DK
1591 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1592 bool
1593 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1594 unsigned int, Output_section*,
1595 const Symbol_table *);
1596
1597 // Scan a section for the Cortex-A8 erratum.
1598 void
1599 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
1600 unsigned int, Output_section*,
1601 Target_arm<big_endian>*);
1602
c8761b9a
DK
1603 // Find the linked text section of an EXIDX section by looking at the
1604 // first reloction of the EXIDX section. PSHDR points to the section
1605 // headers of a relocation section and PSYMS points to the local symbols.
1606 // PSHNDX points to a location storing the text section index if found.
1607 // Return whether we can find the linked section.
1608 bool
1609 find_linked_text_section(const unsigned char* pshdr,
1610 const unsigned char* psyms, unsigned int* pshndx);
1611
1612 //
993d07c1 1613 // Make a new Arm_exidx_input_section object for EXIDX section with
c8761b9a
DK
1614 // index SHNDX and section header SHDR. TEXT_SHNDX is the section
1615 // index of the linked text section.
993d07c1
DK
1616 void
1617 make_exidx_input_section(unsigned int shndx,
c8761b9a
DK
1618 const elfcpp::Shdr<32, big_endian>& shdr,
1619 unsigned int text_shndx);
993d07c1 1620
cb1be87e
DK
1621 // Return the output address of either a plain input section or a
1622 // relaxed input section. SHNDX is the section index.
1623 Arm_address
1624 simple_input_section_output_address(unsigned int, Output_section*);
1625
8ffa3667 1626 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
993d07c1
DK
1627 typedef Unordered_map<unsigned int, const Arm_exidx_input_section*>
1628 Exidx_section_map;
1629
1630 // List of stub tables.
8ffa3667
DK
1631 Stub_table_list stub_tables_;
1632 // Bit vector to tell if a local symbol is a thumb function or not.
1633 // This is only valid after do_count_local_symbol is called.
1634 std::vector<bool> local_symbol_is_thumb_function_;
d5b40221
DK
1635 // processor-specific flags in ELF file header.
1636 elfcpp::Elf_Word processor_specific_flags_;
a0351a69
DK
1637 // Object attributes if there is an .ARM.attributes section or NULL.
1638 Attributes_section_data* attributes_section_data_;
20138696
DK
1639 // Mapping symbols information.
1640 Mapping_symbols_info mapping_symbols_info_;
2fb7225c
DK
1641 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1642 std::vector<bool>* section_has_cortex_a8_workaround_;
993d07c1
DK
1643 // Map a text section to its associated .ARM.exidx section, if there is one.
1644 Exidx_section_map exidx_section_map_;
e7eca48c
DK
1645 // Whether output local symbol count needs updating.
1646 bool output_local_symbol_count_needs_update_;
d5b40221
DK
1647};
1648
1649// Arm_dynobj class.
1650
1651template<bool big_endian>
1652class Arm_dynobj : public Sized_dynobj<32, big_endian>
1653{
1654 public:
2ea97941 1655 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
d5b40221 1656 const elfcpp::Ehdr<32, big_endian>& ehdr)
2ea97941
ILT
1657 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1658 processor_specific_flags_(0), attributes_section_data_(NULL)
d5b40221
DK
1659 { }
1660
1661 ~Arm_dynobj()
a0351a69 1662 { delete this->attributes_section_data_; }
d5b40221
DK
1663
1664 // Downcast a base pointer to an Arm_relobj pointer. This is
1665 // not type-safe but we only use Arm_relobj not the base class.
1666 static Arm_dynobj<big_endian>*
1667 as_arm_dynobj(Dynobj* dynobj)
1668 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1669
1670 // Processor-specific flags in ELF file header. This is valid only after
1671 // reading symbols.
1672 elfcpp::Elf_Word
1673 processor_specific_flags() const
1674 { return this->processor_specific_flags_; }
1675
a0351a69
DK
1676 // Attributes section data.
1677 const Attributes_section_data*
1678 attributes_section_data() const
1679 { return this->attributes_section_data_; }
1680
d5b40221
DK
1681 protected:
1682 // Read the symbol information.
1683 void
1684 do_read_symbols(Read_symbols_data* sd);
1685
1686 private:
1687 // processor-specific flags in ELF file header.
1688 elfcpp::Elf_Word processor_specific_flags_;
a0351a69
DK
1689 // Object attributes if there is an .ARM.attributes section or NULL.
1690 Attributes_section_data* attributes_section_data_;
8ffa3667
DK
1691};
1692
e9bbb538
DK
1693// Functor to read reloc addends during stub generation.
1694
1695template<int sh_type, bool big_endian>
1696struct Stub_addend_reader
1697{
1698 // Return the addend for a relocation of a particular type. Depending
1699 // on whether this is a REL or RELA relocation, read the addend from a
1700 // view or from a Reloc object.
1701 elfcpp::Elf_types<32>::Elf_Swxword
1702 operator()(
1703 unsigned int /* r_type */,
1704 const unsigned char* /* view */,
1705 const typename Reloc_types<sh_type,
ebd95253 1706 32, big_endian>::Reloc& /* reloc */) const;
e9bbb538
DK
1707};
1708
1709// Specialized Stub_addend_reader for SHT_REL type relocation sections.
1710
1711template<bool big_endian>
1712struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1713{
1714 elfcpp::Elf_types<32>::Elf_Swxword
1715 operator()(
1716 unsigned int,
1717 const unsigned char*,
1718 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1719};
1720
1721// Specialized Stub_addend_reader for RELA type relocation sections.
1722// We currently do not handle RELA type relocation sections but it is trivial
1723// to implement the addend reader. This is provided for completeness and to
1724// make it easier to add support for RELA relocation sections in the future.
1725
1726template<bool big_endian>
1727struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1728{
1729 elfcpp::Elf_types<32>::Elf_Swxword
1730 operator()(
1731 unsigned int,
1732 const unsigned char*,
1733 const typename Reloc_types<elfcpp::SHT_RELA, 32,
ebd95253
DK
1734 big_endian>::Reloc& reloc) const
1735 { return reloc.get_r_addend(); }
e9bbb538
DK
1736};
1737
a120bc7f
DK
1738// Cortex_a8_reloc class. We keep record of relocation that may need
1739// the Cortex-A8 erratum workaround.
1740
1741class Cortex_a8_reloc
1742{
1743 public:
1744 Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1745 Arm_address destination)
1746 : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1747 { }
1748
1749 ~Cortex_a8_reloc()
1750 { }
1751
1752 // Accessors: This is a read-only class.
1753
1754 // Return the relocation stub associated with this relocation if there is
1755 // one.
1756 const Reloc_stub*
1757 reloc_stub() const
1758 { return this->reloc_stub_; }
1759
1760 // Return the relocation type.
1761 unsigned int
1762 r_type() const
1763 { return this->r_type_; }
1764
1765 // Return the destination address of the relocation. LSB stores the THUMB
1766 // bit.
1767 Arm_address
1768 destination() const
1769 { return this->destination_; }
1770
1771 private:
1772 // Associated relocation stub if there is one, or NULL.
1773 const Reloc_stub* reloc_stub_;
1774 // Relocation type.
1775 unsigned int r_type_;
1776 // Destination address of this relocation. LSB is used to distinguish
1777 // ARM/THUMB mode.
1778 Arm_address destination_;
1779};
1780
c121c671
DK
1781// Utilities for manipulating integers of up to 32-bits
1782
1783namespace utils
1784{
1785 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1786 // an int32_t. NO_BITS must be between 1 to 32.
1787 template<int no_bits>
1788 static inline int32_t
1789 sign_extend(uint32_t bits)
1790 {
96d49306 1791 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
1792 if (no_bits == 32)
1793 return static_cast<int32_t>(bits);
1794 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1795 bits &= mask;
1796 uint32_t top_bit = 1U << (no_bits - 1);
1797 int32_t as_signed = static_cast<int32_t>(bits);
1798 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1799 }
1800
1801 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1802 template<int no_bits>
1803 static inline bool
1804 has_overflow(uint32_t bits)
1805 {
96d49306 1806 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
1807 if (no_bits == 32)
1808 return false;
1809 int32_t max = (1 << (no_bits - 1)) - 1;
1810 int32_t min = -(1 << (no_bits - 1));
1811 int32_t as_signed = static_cast<int32_t>(bits);
1812 return as_signed > max || as_signed < min;
1813 }
1814
5e445df6
ILT
1815 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1816 // fits in the given number of bits as either a signed or unsigned value.
1817 // For example, has_signed_unsigned_overflow<8> would check
1818 // -128 <= bits <= 255
1819 template<int no_bits>
1820 static inline bool
1821 has_signed_unsigned_overflow(uint32_t bits)
1822 {
1823 gold_assert(no_bits >= 2 && no_bits <= 32);
1824 if (no_bits == 32)
1825 return false;
1826 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1827 int32_t min = -(1 << (no_bits - 1));
1828 int32_t as_signed = static_cast<int32_t>(bits);
1829 return as_signed > max || as_signed < min;
1830 }
1831
c121c671
DK
1832 // Select bits from A and B using bits in MASK. For each n in [0..31],
1833 // the n-th bit in the result is chosen from the n-th bits of A and B.
1834 // A zero selects A and a one selects B.
1835 static inline uint32_t
1836 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1837 { return (a & ~mask) | (b & mask); }
1838};
1839
4a657b0d
DK
1840template<bool big_endian>
1841class Target_arm : public Sized_target<32, big_endian>
1842{
1843 public:
1844 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1845 Reloc_section;
1846
2daedcd6
DK
1847 // When were are relocating a stub, we pass this as the relocation number.
1848 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1849
a6d1ef57
DK
1850 Target_arm()
1851 : Sized_target<32, big_endian>(&arm_info),
1852 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
f96accdf
DK
1853 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
1854 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
1855 stub_tables_(), stub_factory_(Stub_factory::get_instance()),
1856 may_use_blx_(false), should_force_pic_veneer_(false),
1857 arm_input_section_map_(), attributes_section_data_(NULL),
1858 fix_cortex_a8_(false), cortex_a8_relocs_info_()
a6d1ef57 1859 { }
4a657b0d 1860
b569affa
DK
1861 // Whether we can use BLX.
1862 bool
1863 may_use_blx() const
1864 { return this->may_use_blx_; }
1865
1866 // Set use-BLX flag.
1867 void
1868 set_may_use_blx(bool value)
1869 { this->may_use_blx_ = value; }
1870
1871 // Whether we force PCI branch veneers.
1872 bool
1873 should_force_pic_veneer() const
1874 { return this->should_force_pic_veneer_; }
1875
1876 // Set PIC veneer flag.
1877 void
1878 set_should_force_pic_veneer(bool value)
1879 { this->should_force_pic_veneer_ = value; }
1880
1881 // Whether we use THUMB-2 instructions.
1882 bool
1883 using_thumb2() const
1884 {
a0351a69
DK
1885 Object_attribute* attr =
1886 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1887 int arch = attr->int_value();
1888 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
b569affa
DK
1889 }
1890
1891 // Whether we use THUMB/THUMB-2 instructions only.
1892 bool
1893 using_thumb_only() const
1894 {
a0351a69
DK
1895 Object_attribute* attr =
1896 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1897 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
1898 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
1899 return false;
1900 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
1901 return attr->int_value() == 'M';
b569affa
DK
1902 }
1903
d204b6e9
DK
1904 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1905 bool
1906 may_use_arm_nop() const
1907 {
a0351a69
DK
1908 Object_attribute* attr =
1909 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1910 int arch = attr->int_value();
1911 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1912 || arch == elfcpp::TAG_CPU_ARCH_V6K
1913 || arch == elfcpp::TAG_CPU_ARCH_V7
1914 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
d204b6e9
DK
1915 }
1916
51938283
DK
1917 // Whether we have THUMB-2 NOP.W instruction.
1918 bool
1919 may_use_thumb2_nop() const
1920 {
a0351a69
DK
1921 Object_attribute* attr =
1922 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1923 int arch = attr->int_value();
1924 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1925 || arch == elfcpp::TAG_CPU_ARCH_V7
1926 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
51938283
DK
1927 }
1928
4a657b0d
DK
1929 // Process the relocations to determine unreferenced sections for
1930 // garbage collection.
1931 void
ad0f2072 1932 gc_process_relocs(Symbol_table* symtab,
4a657b0d
DK
1933 Layout* layout,
1934 Sized_relobj<32, big_endian>* object,
1935 unsigned int data_shndx,
1936 unsigned int sh_type,
1937 const unsigned char* prelocs,
1938 size_t reloc_count,
1939 Output_section* output_section,
1940 bool needs_special_offset_handling,
1941 size_t local_symbol_count,
1942 const unsigned char* plocal_symbols);
1943
1944 // Scan the relocations to look for symbol adjustments.
1945 void
ad0f2072 1946 scan_relocs(Symbol_table* symtab,
4a657b0d
DK
1947 Layout* layout,
1948 Sized_relobj<32, big_endian>* object,
1949 unsigned int data_shndx,
1950 unsigned int sh_type,
1951 const unsigned char* prelocs,
1952 size_t reloc_count,
1953 Output_section* output_section,
1954 bool needs_special_offset_handling,
1955 size_t local_symbol_count,
1956 const unsigned char* plocal_symbols);
1957
1958 // Finalize the sections.
1959 void
f59f41f3 1960 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
4a657b0d 1961
94cdfcff 1962 // Return the value to use for a dynamic symbol which requires special
4a657b0d
DK
1963 // treatment.
1964 uint64_t
1965 do_dynsym_value(const Symbol*) const;
1966
1967 // Relocate a section.
1968 void
1969 relocate_section(const Relocate_info<32, big_endian>*,
1970 unsigned int sh_type,
1971 const unsigned char* prelocs,
1972 size_t reloc_count,
1973 Output_section* output_section,
1974 bool needs_special_offset_handling,
1975 unsigned char* view,
ebabffbd 1976 Arm_address view_address,
364c7fa5
ILT
1977 section_size_type view_size,
1978 const Reloc_symbol_changes*);
4a657b0d
DK
1979
1980 // Scan the relocs during a relocatable link.
1981 void
ad0f2072 1982 scan_relocatable_relocs(Symbol_table* symtab,
4a657b0d
DK
1983 Layout* layout,
1984 Sized_relobj<32, big_endian>* object,
1985 unsigned int data_shndx,
1986 unsigned int sh_type,
1987 const unsigned char* prelocs,
1988 size_t reloc_count,
1989 Output_section* output_section,
1990 bool needs_special_offset_handling,
1991 size_t local_symbol_count,
1992 const unsigned char* plocal_symbols,
1993 Relocatable_relocs*);
1994
1995 // Relocate a section during a relocatable link.
1996 void
1997 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1998 unsigned int sh_type,
1999 const unsigned char* prelocs,
2000 size_t reloc_count,
2001 Output_section* output_section,
2002 off_t offset_in_output_section,
2003 const Relocatable_relocs*,
2004 unsigned char* view,
ebabffbd 2005 Arm_address view_address,
4a657b0d
DK
2006 section_size_type view_size,
2007 unsigned char* reloc_view,
2008 section_size_type reloc_view_size);
2009
2010 // Return whether SYM is defined by the ABI.
2011 bool
2012 do_is_defined_by_abi(Symbol* sym) const
2013 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
2014
c8761b9a
DK
2015 // Return whether there is a GOT section.
2016 bool
2017 has_got_section() const
2018 { return this->got_ != NULL; }
2019
94cdfcff
DK
2020 // Return the size of the GOT section.
2021 section_size_type
2022 got_size()
2023 {
2024 gold_assert(this->got_ != NULL);
2025 return this->got_->data_size();
2026 }
2027
4a657b0d 2028 // Map platform-specific reloc types
a6d1ef57
DK
2029 static unsigned int
2030 get_real_reloc_type (unsigned int r_type);
4a657b0d 2031
55da9579
DK
2032 //
2033 // Methods to support stub-generations.
2034 //
2035
2036 // Return the stub factory
2037 const Stub_factory&
2038 stub_factory() const
2039 { return this->stub_factory_; }
2040
2041 // Make a new Arm_input_section object.
2042 Arm_input_section<big_endian>*
2043 new_arm_input_section(Relobj*, unsigned int);
2044
2045 // Find the Arm_input_section object corresponding to the SHNDX-th input
2046 // section of RELOBJ.
2047 Arm_input_section<big_endian>*
2ea97941 2048 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
55da9579
DK
2049
2050 // Make a new Stub_table
2051 Stub_table<big_endian>*
2052 new_stub_table(Arm_input_section<big_endian>*);
2053
eb44217c
DK
2054 // Scan a section for stub generation.
2055 void
2056 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
2057 const unsigned char*, size_t, Output_section*,
2058 bool, const unsigned char*, Arm_address,
2059 section_size_type);
2060
43d12afe
DK
2061 // Relocate a stub.
2062 void
2fb7225c 2063 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
43d12afe
DK
2064 Output_section*, unsigned char*, Arm_address,
2065 section_size_type);
2066
b569affa 2067 // Get the default ARM target.
43d12afe 2068 static Target_arm<big_endian>*
b569affa
DK
2069 default_target()
2070 {
2071 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
2072 && parameters->target().is_big_endian() == big_endian);
43d12afe
DK
2073 return static_cast<Target_arm<big_endian>*>(
2074 parameters->sized_target<32, big_endian>());
b569affa
DK
2075 }
2076
20138696
DK
2077 // Whether NAME belongs to a mapping symbol.
2078 static bool
2079 is_mapping_symbol_name(const char* name)
2080 {
2081 return (name
2082 && name[0] == '$'
2083 && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
2084 && (name[2] == '\0' || name[2] == '.'));
2085 }
2086
a120bc7f
DK
2087 // Whether we work around the Cortex-A8 erratum.
2088 bool
2089 fix_cortex_a8() const
2090 { return this->fix_cortex_a8_; }
2091
a2162063
ILT
2092 // Whether we fix R_ARM_V4BX relocation.
2093 // 0 - do not fix
2094 // 1 - replace with MOV instruction (armv4 target)
2095 // 2 - make interworking veneer (>= armv4t targets only)
9b2fd367 2096 General_options::Fix_v4bx
a2162063 2097 fix_v4bx() const
9b2fd367 2098 { return parameters->options().fix_v4bx(); }
a2162063 2099
44272192
DK
2100 // Scan a span of THUMB code section for Cortex-A8 erratum.
2101 void
2102 scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
2103 section_size_type, section_size_type,
2104 const unsigned char*, Arm_address);
2105
41263c05
DK
2106 // Apply Cortex-A8 workaround to a branch.
2107 void
2108 apply_cortex_a8_workaround(const Cortex_a8_stub*, Arm_address,
2109 unsigned char*, Arm_address);
2110
d5b40221 2111 protected:
eb44217c
DK
2112 // Make an ELF object.
2113 Object*
2114 do_make_elf_object(const std::string&, Input_file*, off_t,
2115 const elfcpp::Ehdr<32, big_endian>& ehdr);
2116
2117 Object*
2118 do_make_elf_object(const std::string&, Input_file*, off_t,
2119 const elfcpp::Ehdr<32, !big_endian>&)
2120 { gold_unreachable(); }
2121
2122 Object*
2123 do_make_elf_object(const std::string&, Input_file*, off_t,
2124 const elfcpp::Ehdr<64, false>&)
2125 { gold_unreachable(); }
2126
2127 Object*
2128 do_make_elf_object(const std::string&, Input_file*, off_t,
2129 const elfcpp::Ehdr<64, true>&)
2130 { gold_unreachable(); }
2131
2132 // Make an output section.
2133 Output_section*
2134 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2135 elfcpp::Elf_Xword flags)
2136 { return new Arm_output_section<big_endian>(name, type, flags); }
2137
d5b40221
DK
2138 void
2139 do_adjust_elf_header(unsigned char* view, int len) const;
2140
eb44217c
DK
2141 // We only need to generate stubs, and hence perform relaxation if we are
2142 // not doing relocatable linking.
2143 bool
2144 do_may_relax() const
2145 { return !parameters->options().relocatable(); }
2146
2147 bool
2148 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
2149
a0351a69
DK
2150 // Determine whether an object attribute tag takes an integer, a
2151 // string or both.
2152 int
2153 do_attribute_arg_type(int tag) const;
2154
2155 // Reorder tags during output.
2156 int
2157 do_attributes_order(int num) const;
2158
0d31c79d
DK
2159 // This is called when the target is selected as the default.
2160 void
2161 do_select_as_default_target()
2162 {
2163 // No locking is required since there should only be one default target.
2164 // We cannot have both the big-endian and little-endian ARM targets
2165 // as the default.
2166 gold_assert(arm_reloc_property_table == NULL);
2167 arm_reloc_property_table = new Arm_reloc_property_table();
2168 }
2169
4a657b0d
DK
2170 private:
2171 // The class which scans relocations.
2172 class Scan
2173 {
2174 public:
2175 Scan()
bec53400 2176 : issued_non_pic_error_(false)
4a657b0d
DK
2177 { }
2178
2179 inline void
ad0f2072 2180 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
4a657b0d
DK
2181 Sized_relobj<32, big_endian>* object,
2182 unsigned int data_shndx,
2183 Output_section* output_section,
2184 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2185 const elfcpp::Sym<32, big_endian>& lsym);
2186
2187 inline void
ad0f2072 2188 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
4a657b0d
DK
2189 Sized_relobj<32, big_endian>* object,
2190 unsigned int data_shndx,
2191 Output_section* output_section,
2192 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2193 Symbol* gsym);
2194
21bb3914
ST
2195 inline bool
2196 local_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2197 Sized_relobj<32, big_endian>* ,
2198 unsigned int ,
2199 Output_section* ,
2200 const elfcpp::Rel<32, big_endian>& ,
2201 unsigned int ,
2202 const elfcpp::Sym<32, big_endian>&)
2203 { return false; }
2204
2205 inline bool
2206 global_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2207 Sized_relobj<32, big_endian>* ,
2208 unsigned int ,
2209 Output_section* ,
2210 const elfcpp::Rel<32, big_endian>& ,
2211 unsigned int , Symbol*)
2212 { return false; }
2213
4a657b0d
DK
2214 private:
2215 static void
2216 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
2217 unsigned int r_type);
2218
2219 static void
2220 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
2221 unsigned int r_type, Symbol*);
bec53400
DK
2222
2223 void
2224 check_non_pic(Relobj*, unsigned int r_type);
2225
2226 // Almost identical to Symbol::needs_plt_entry except that it also
2227 // handles STT_ARM_TFUNC.
2228 static bool
2229 symbol_needs_plt_entry(const Symbol* sym)
2230 {
2231 // An undefined symbol from an executable does not need a PLT entry.
2232 if (sym->is_undefined() && !parameters->options().shared())
2233 return false;
2234
2235 return (!parameters->doing_static_link()
2236 && (sym->type() == elfcpp::STT_FUNC
2237 || sym->type() == elfcpp::STT_ARM_TFUNC)
2238 && (sym->is_from_dynobj()
2239 || sym->is_undefined()
2240 || sym->is_preemptible()));
2241 }
2242
2243 // Whether we have issued an error about a non-PIC compilation.
2244 bool issued_non_pic_error_;
4a657b0d
DK
2245 };
2246
2247 // The class which implements relocation.
2248 class Relocate
2249 {
2250 public:
2251 Relocate()
2252 { }
2253
2254 ~Relocate()
2255 { }
2256
bec53400
DK
2257 // Return whether the static relocation needs to be applied.
2258 inline bool
2259 should_apply_static_reloc(const Sized_symbol<32>* gsym,
2260 int ref_flags,
2261 bool is_32bit,
2262 Output_section* output_section);
2263
4a657b0d
DK
2264 // Do a relocation. Return false if the caller should not issue
2265 // any warnings about this relocation.
2266 inline bool
2267 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
2268 Output_section*, size_t relnum,
2269 const elfcpp::Rel<32, big_endian>&,
2270 unsigned int r_type, const Sized_symbol<32>*,
2271 const Symbol_value<32>*,
ebabffbd 2272 unsigned char*, Arm_address,
4a657b0d 2273 section_size_type);
c121c671
DK
2274
2275 // Return whether we want to pass flag NON_PIC_REF for this
f4e5969c
DK
2276 // reloc. This means the relocation type accesses a symbol not via
2277 // GOT or PLT.
c121c671
DK
2278 static inline bool
2279 reloc_is_non_pic (unsigned int r_type)
2280 {
2281 switch (r_type)
2282 {
f4e5969c
DK
2283 // These relocation types reference GOT or PLT entries explicitly.
2284 case elfcpp::R_ARM_GOT_BREL:
2285 case elfcpp::R_ARM_GOT_ABS:
2286 case elfcpp::R_ARM_GOT_PREL:
2287 case elfcpp::R_ARM_GOT_BREL12:
2288 case elfcpp::R_ARM_PLT32_ABS:
2289 case elfcpp::R_ARM_TLS_GD32:
2290 case elfcpp::R_ARM_TLS_LDM32:
2291 case elfcpp::R_ARM_TLS_IE32:
2292 case elfcpp::R_ARM_TLS_IE12GP:
2293
2294 // These relocate types may use PLT entries.
c121c671 2295 case elfcpp::R_ARM_CALL:
f4e5969c 2296 case elfcpp::R_ARM_THM_CALL:
c121c671 2297 case elfcpp::R_ARM_JUMP24:
f4e5969c
DK
2298 case elfcpp::R_ARM_THM_JUMP24:
2299 case elfcpp::R_ARM_THM_JUMP19:
2300 case elfcpp::R_ARM_PLT32:
2301 case elfcpp::R_ARM_THM_XPC22:
c121c671 2302 return false;
f4e5969c
DK
2303
2304 default:
2305 return true;
c121c671
DK
2306 }
2307 }
f96accdf
DK
2308
2309 private:
2310 // Do a TLS relocation.
2311 inline typename Arm_relocate_functions<big_endian>::Status
2312 relocate_tls(const Relocate_info<32, big_endian>*, Target_arm<big_endian>*,
2313 size_t, const elfcpp::Rel<32, big_endian>&, unsigned int,
2314 const Sized_symbol<32>*, const Symbol_value<32>*,
2315 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
2316 section_size_type);
2317
4a657b0d
DK
2318 };
2319
2320 // A class which returns the size required for a relocation type,
2321 // used while scanning relocs during a relocatable link.
2322 class Relocatable_size_for_reloc
2323 {
2324 public:
2325 unsigned int
2326 get_size_for_reloc(unsigned int, Relobj*);
2327 };
2328
f96accdf
DK
2329 // Adjust TLS relocation type based on the options and whether this
2330 // is a local symbol.
2331 static tls::Tls_optimization
2332 optimize_tls_reloc(bool is_final, int r_type);
2333
94cdfcff
DK
2334 // Get the GOT section, creating it if necessary.
2335 Output_data_got<32, big_endian>*
2336 got_section(Symbol_table*, Layout*);
2337
2338 // Get the GOT PLT section.
2339 Output_data_space*
2340 got_plt_section() const
2341 {
2342 gold_assert(this->got_plt_ != NULL);
2343 return this->got_plt_;
2344 }
2345
2346 // Create a PLT entry for a global symbol.
2347 void
2348 make_plt_entry(Symbol_table*, Layout*, Symbol*);
2349
f96accdf
DK
2350 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2351 void
2352 define_tls_base_symbol(Symbol_table*, Layout*);
2353
2354 // Create a GOT entry for the TLS module index.
2355 unsigned int
2356 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2357 Sized_relobj<32, big_endian>* object);
2358
94cdfcff
DK
2359 // Get the PLT section.
2360 const Output_data_plt_arm<big_endian>*
2361 plt_section() const
2362 {
2363 gold_assert(this->plt_ != NULL);
2364 return this->plt_;
2365 }
2366
2367 // Get the dynamic reloc section, creating it if necessary.
2368 Reloc_section*
2369 rel_dyn_section(Layout*);
2370
f96accdf
DK
2371 // Get the section to use for TLS_DESC relocations.
2372 Reloc_section*
2373 rel_tls_desc_section(Layout*) const;
2374
94cdfcff
DK
2375 // Return true if the symbol may need a COPY relocation.
2376 // References from an executable object to non-function symbols
2377 // defined in a dynamic object may need a COPY relocation.
2378 bool
2379 may_need_copy_reloc(Symbol* gsym)
2380 {
966d4097
DK
2381 return (gsym->type() != elfcpp::STT_ARM_TFUNC
2382 && gsym->may_need_copy_reloc());
94cdfcff
DK
2383 }
2384
2385 // Add a potential copy relocation.
2386 void
2387 copy_reloc(Symbol_table* symtab, Layout* layout,
2388 Sized_relobj<32, big_endian>* object,
2ea97941 2389 unsigned int shndx, Output_section* output_section,
94cdfcff
DK
2390 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
2391 {
2392 this->copy_relocs_.copy_reloc(symtab, layout,
2393 symtab->get_sized_symbol<32>(sym),
2ea97941 2394 object, shndx, output_section, reloc,
94cdfcff
DK
2395 this->rel_dyn_section(layout));
2396 }
2397
d5b40221
DK
2398 // Whether two EABI versions are compatible.
2399 static bool
2400 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
2401
2402 // Merge processor-specific flags from input object and those in the ELF
2403 // header of the output.
2404 void
2405 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
2406
a0351a69
DK
2407 // Get the secondary compatible architecture.
2408 static int
2409 get_secondary_compatible_arch(const Attributes_section_data*);
2410
2411 // Set the secondary compatible architecture.
2412 static void
2413 set_secondary_compatible_arch(Attributes_section_data*, int);
2414
2415 static int
2416 tag_cpu_arch_combine(const char*, int, int*, int, int);
2417
2418 // Helper to print AEABI enum tag value.
2419 static std::string
2420 aeabi_enum_name(unsigned int);
2421
2422 // Return string value for TAG_CPU_name.
2423 static std::string
2424 tag_cpu_name_value(unsigned int);
2425
2426 // Merge object attributes from input object and those in the output.
2427 void
2428 merge_object_attributes(const char*, const Attributes_section_data*);
2429
2430 // Helper to get an AEABI object attribute
2431 Object_attribute*
2432 get_aeabi_object_attribute(int tag) const
2433 {
2434 Attributes_section_data* pasd = this->attributes_section_data_;
2435 gold_assert(pasd != NULL);
2436 Object_attribute* attr =
2437 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
2438 gold_assert(attr != NULL);
2439 return attr;
2440 }
2441
eb44217c
DK
2442 //
2443 // Methods to support stub-generations.
2444 //
d5b40221 2445
eb44217c
DK
2446 // Group input sections for stub generation.
2447 void
2448 group_sections(Layout*, section_size_type, bool);
d5b40221 2449
eb44217c
DK
2450 // Scan a relocation for stub generation.
2451 void
2452 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
2453 const Sized_symbol<32>*, unsigned int,
2454 const Symbol_value<32>*,
2455 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
d5b40221 2456
eb44217c
DK
2457 // Scan a relocation section for stub.
2458 template<int sh_type>
2459 void
2460 scan_reloc_section_for_stubs(
2461 const Relocate_info<32, big_endian>* relinfo,
2462 const unsigned char* prelocs,
2463 size_t reloc_count,
2464 Output_section* output_section,
2465 bool needs_special_offset_handling,
2466 const unsigned char* view,
2467 elfcpp::Elf_types<32>::Elf_Addr view_address,
2468 section_size_type);
d5b40221 2469
2b328d4e
DK
2470 // Fix .ARM.exidx section coverage.
2471 void
2472 fix_exidx_coverage(Layout*, Arm_output_section<big_endian>*, Symbol_table*);
2473
2474 // Functors for STL set.
2475 struct output_section_address_less_than
2476 {
2477 bool
2478 operator()(const Output_section* s1, const Output_section* s2) const
2479 { return s1->address() < s2->address(); }
2480 };
2481
4a657b0d
DK
2482 // Information about this specific target which we pass to the
2483 // general Target structure.
2484 static const Target::Target_info arm_info;
94cdfcff
DK
2485
2486 // The types of GOT entries needed for this platform.
2487 enum Got_type
2488 {
f96accdf
DK
2489 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
2490 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
2491 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
2492 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
2493 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
94cdfcff
DK
2494 };
2495
55da9579
DK
2496 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
2497
2498 // Map input section to Arm_input_section.
5ac169d4 2499 typedef Unordered_map<Section_id,
55da9579 2500 Arm_input_section<big_endian>*,
5ac169d4 2501 Section_id_hash>
55da9579
DK
2502 Arm_input_section_map;
2503
a120bc7f
DK
2504 // Map output addresses to relocs for Cortex-A8 erratum.
2505 typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
2506 Cortex_a8_relocs_info;
2507
94cdfcff
DK
2508 // The GOT section.
2509 Output_data_got<32, big_endian>* got_;
2510 // The PLT section.
2511 Output_data_plt_arm<big_endian>* plt_;
2512 // The GOT PLT section.
2513 Output_data_space* got_plt_;
2514 // The dynamic reloc section.
2515 Reloc_section* rel_dyn_;
2516 // Relocs saved to avoid a COPY reloc.
2517 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
2518 // Space for variables copied with a COPY reloc.
2519 Output_data_space* dynbss_;
f96accdf
DK
2520 // Offset of the GOT entry for the TLS module index.
2521 unsigned int got_mod_index_offset_;
2522 // True if the _TLS_MODULE_BASE_ symbol has been defined.
2523 bool tls_base_symbol_defined_;
55da9579
DK
2524 // Vector of Stub_tables created.
2525 Stub_table_list stub_tables_;
2526 // Stub factory.
2527 const Stub_factory &stub_factory_;
b569affa
DK
2528 // Whether we can use BLX.
2529 bool may_use_blx_;
2530 // Whether we force PIC branch veneers.
2531 bool should_force_pic_veneer_;
eb44217c
DK
2532 // Map for locating Arm_input_sections.
2533 Arm_input_section_map arm_input_section_map_;
a0351a69
DK
2534 // Attributes section data in output.
2535 Attributes_section_data* attributes_section_data_;
a120bc7f
DK
2536 // Whether we want to fix code for Cortex-A8 erratum.
2537 bool fix_cortex_a8_;
2538 // Map addresses to relocs for Cortex-A8 erratum.
2539 Cortex_a8_relocs_info cortex_a8_relocs_info_;
4a657b0d
DK
2540};
2541
2542template<bool big_endian>
2543const Target::Target_info Target_arm<big_endian>::arm_info =
2544{
2545 32, // size
2546 big_endian, // is_big_endian
2547 elfcpp::EM_ARM, // machine_code
2548 false, // has_make_symbol
2549 false, // has_resolve
2550 false, // has_code_fill
2551 true, // is_default_stack_executable
2552 '\0', // wrap_char
2553 "/usr/lib/libc.so.1", // dynamic_linker
2554 0x8000, // default_text_segment_address
2555 0x1000, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
2556 0x1000, // common_pagesize (overridable by -z common-page-size)
2557 elfcpp::SHN_UNDEF, // small_common_shndx
2558 elfcpp::SHN_UNDEF, // large_common_shndx
2559 0, // small_common_section_flags
05a352e6
DK
2560 0, // large_common_section_flags
2561 ".ARM.attributes", // attributes_section
2562 "aeabi" // attributes_vendor
4a657b0d
DK
2563};
2564
c121c671
DK
2565// Arm relocate functions class
2566//
2567
2568template<bool big_endian>
2569class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2570{
2571 public:
2572 typedef enum
2573 {
2574 STATUS_OKAY, // No error during relocation.
2575 STATUS_OVERFLOW, // Relocation oveflow.
2576 STATUS_BAD_RELOC // Relocation cannot be applied.
2577 } Status;
2578
2579 private:
2580 typedef Relocate_functions<32, big_endian> Base;
2581 typedef Arm_relocate_functions<big_endian> This;
2582
fd3c5f0b
ILT
2583 // Encoding of imm16 argument for movt and movw ARM instructions
2584 // from ARM ARM:
2585 //
2586 // imm16 := imm4 | imm12
2587 //
2588 // 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
2589 // +-------+---------------+-------+-------+-----------------------+
2590 // | | |imm4 | |imm12 |
2591 // +-------+---------------+-------+-------+-----------------------+
2592
2593 // Extract the relocation addend from VAL based on the ARM
2594 // instruction encoding described above.
2595 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2596 extract_arm_movw_movt_addend(
2597 typename elfcpp::Swap<32, big_endian>::Valtype val)
2598 {
2599 // According to the Elf ABI for ARM Architecture the immediate
2600 // field is sign-extended to form the addend.
2601 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2602 }
2603
2604 // Insert X into VAL based on the ARM instruction encoding described
2605 // above.
2606 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2607 insert_val_arm_movw_movt(
2608 typename elfcpp::Swap<32, big_endian>::Valtype val,
2609 typename elfcpp::Swap<32, big_endian>::Valtype x)
2610 {
2611 val &= 0xfff0f000;
2612 val |= x & 0x0fff;
2613 val |= (x & 0xf000) << 4;
2614 return val;
2615 }
2616
2617 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2618 // from ARM ARM:
2619 //
2620 // imm16 := imm4 | i | imm3 | imm8
2621 //
2622 // 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
2623 // +---------+-+-----------+-------++-+-----+-------+---------------+
2624 // | |i| |imm4 || |imm3 | |imm8 |
2625 // +---------+-+-----------+-------++-+-----+-------+---------------+
2626
2627 // Extract the relocation addend from VAL based on the Thumb2
2628 // instruction encoding described above.
2629 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2630 extract_thumb_movw_movt_addend(
2631 typename elfcpp::Swap<32, big_endian>::Valtype val)
2632 {
2633 // According to the Elf ABI for ARM Architecture the immediate
2634 // field is sign-extended to form the addend.
2635 return utils::sign_extend<16>(((val >> 4) & 0xf000)
2636 | ((val >> 15) & 0x0800)
2637 | ((val >> 4) & 0x0700)
2638 | (val & 0x00ff));
2639 }
2640
2641 // Insert X into VAL based on the Thumb2 instruction encoding
2642 // described above.
2643 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2644 insert_val_thumb_movw_movt(
2645 typename elfcpp::Swap<32, big_endian>::Valtype val,
2646 typename elfcpp::Swap<32, big_endian>::Valtype x)
2647 {
2648 val &= 0xfbf08f00;
2649 val |= (x & 0xf000) << 4;
2650 val |= (x & 0x0800) << 15;
2651 val |= (x & 0x0700) << 4;
2652 val |= (x & 0x00ff);
2653 return val;
2654 }
2655
b10d2873
ILT
2656 // Calculate the smallest constant Kn for the specified residual.
2657 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2658 static uint32_t
2659 calc_grp_kn(typename elfcpp::Swap<32, big_endian>::Valtype residual)
2660 {
2661 int32_t msb;
2662
2663 if (residual == 0)
2664 return 0;
2665 // Determine the most significant bit in the residual and
2666 // align the resulting value to a 2-bit boundary.
2667 for (msb = 30; (msb >= 0) && !(residual & (3 << msb)); msb -= 2)
2668 ;
2669 // The desired shift is now (msb - 6), or zero, whichever
2670 // is the greater.
2671 return (((msb - 6) < 0) ? 0 : (msb - 6));
2672 }
2673
2674 // Calculate the final residual for the specified group index.
2675 // If the passed group index is less than zero, the method will return
2676 // the value of the specified residual without any change.
2677 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2678 static typename elfcpp::Swap<32, big_endian>::Valtype
2679 calc_grp_residual(typename elfcpp::Swap<32, big_endian>::Valtype residual,
2680 const int group)
2681 {
2682 for (int n = 0; n <= group; n++)
2683 {
2684 // Calculate which part of the value to mask.
2685 uint32_t shift = calc_grp_kn(residual);
2686 // Calculate the residual for the next time around.
2687 residual &= ~(residual & (0xff << shift));
2688 }
2689
2690 return residual;
2691 }
2692
2693 // Calculate the value of Gn for the specified group index.
2694 // We return it in the form of an encoded constant-and-rotation.
2695 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2696 static typename elfcpp::Swap<32, big_endian>::Valtype
2697 calc_grp_gn(typename elfcpp::Swap<32, big_endian>::Valtype residual,
2698 const int group)
2699 {
2700 typename elfcpp::Swap<32, big_endian>::Valtype gn = 0;
2701 uint32_t shift = 0;
2702
2703 for (int n = 0; n <= group; n++)
2704 {
2705 // Calculate which part of the value to mask.
2706 shift = calc_grp_kn(residual);
2707 // Calculate Gn in 32-bit as well as encoded constant-and-rotation form.
2708 gn = residual & (0xff << shift);
2709 // Calculate the residual for the next time around.
2710 residual &= ~gn;
2711 }
2712 // Return Gn in the form of an encoded constant-and-rotation.
2713 return ((gn >> shift) | ((gn <= 0xff ? 0 : (32 - shift) / 2) << 8));
2714 }
2715
1521477a 2716 public:
d204b6e9
DK
2717 // Handle ARM long branches.
2718 static typename This::Status
2719 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2720 unsigned char *, const Sized_symbol<32>*,
2721 const Arm_relobj<big_endian>*, unsigned int,
2722 const Symbol_value<32>*, Arm_address, Arm_address, bool);
c121c671 2723
51938283
DK
2724 // Handle THUMB long branches.
2725 static typename This::Status
2726 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2727 unsigned char *, const Sized_symbol<32>*,
2728 const Arm_relobj<big_endian>*, unsigned int,
2729 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2730
5e445df6 2731
089d69dc
DK
2732 // Return the branch offset of a 32-bit THUMB branch.
2733 static inline int32_t
2734 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2735 {
2736 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2737 // involving the J1 and J2 bits.
2738 uint32_t s = (upper_insn & (1U << 10)) >> 10;
2739 uint32_t upper = upper_insn & 0x3ffU;
2740 uint32_t lower = lower_insn & 0x7ffU;
2741 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
2742 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
2743 uint32_t i1 = j1 ^ s ? 0 : 1;
2744 uint32_t i2 = j2 ^ s ? 0 : 1;
2745
2746 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
2747 | (upper << 12) | (lower << 1));
2748 }
2749
2750 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2751 // UPPER_INSN is the original upper instruction of the branch. Caller is
2752 // responsible for overflow checking and BLX offset adjustment.
2753 static inline uint16_t
2754 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
2755 {
2756 uint32_t s = offset < 0 ? 1 : 0;
2757 uint32_t bits = static_cast<uint32_t>(offset);
2758 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
2759 }
2760
2761 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2762 // LOWER_INSN is the original lower instruction of the branch. Caller is
2763 // responsible for overflow checking and BLX offset adjustment.
2764 static inline uint16_t
2765 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
2766 {
2767 uint32_t s = offset < 0 ? 1 : 0;
2768 uint32_t bits = static_cast<uint32_t>(offset);
2769 return ((lower_insn & ~0x2fffU)
2770 | ((((bits >> 23) & 1) ^ !s) << 13)
2771 | ((((bits >> 22) & 1) ^ !s) << 11)
2772 | ((bits >> 1) & 0x7ffU));
2773 }
2774
2775 // Return the branch offset of a 32-bit THUMB conditional branch.
2776 static inline int32_t
2777 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2778 {
2779 uint32_t s = (upper_insn & 0x0400U) >> 10;
2780 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
2781 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
2782 uint32_t lower = (lower_insn & 0x07ffU);
2783 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
2784
2785 return utils::sign_extend<21>((upper << 12) | (lower << 1));
2786 }
2787
2788 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2789 // instruction. UPPER_INSN is the original upper instruction of the branch.
2790 // Caller is responsible for overflow checking.
2791 static inline uint16_t
2792 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
2793 {
2794 uint32_t s = offset < 0 ? 1 : 0;
2795 uint32_t bits = static_cast<uint32_t>(offset);
2796 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
2797 }
2798
2799 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2800 // instruction. LOWER_INSN is the original lower instruction of the branch.
2801 // Caller is reponsible for overflow checking.
2802 static inline uint16_t
2803 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
2804 {
2805 uint32_t bits = static_cast<uint32_t>(offset);
2806 uint32_t j2 = (bits & 0x00080000U) >> 19;
2807 uint32_t j1 = (bits & 0x00040000U) >> 18;
2808 uint32_t lo = (bits & 0x00000ffeU) >> 1;
2809
2810 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
2811 }
2812
5e445df6
ILT
2813 // R_ARM_ABS8: S + A
2814 static inline typename This::Status
2815 abs8(unsigned char *view,
2816 const Sized_relobj<32, big_endian>* object,
be8fcb75 2817 const Symbol_value<32>* psymval)
5e445df6
ILT
2818 {
2819 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
2820 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2821 Valtype* wv = reinterpret_cast<Valtype*>(view);
2822 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
2823 Reltype addend = utils::sign_extend<8>(val);
2daedcd6 2824 Reltype x = psymval->value(object, addend);
5e445df6
ILT
2825 val = utils::bit_select(val, x, 0xffU);
2826 elfcpp::Swap<8, big_endian>::writeval(wv, val);
2827 return (utils::has_signed_unsigned_overflow<8>(x)
2828 ? This::STATUS_OVERFLOW
2829 : This::STATUS_OKAY);
2830 }
2831
be8fcb75
ILT
2832 // R_ARM_THM_ABS5: S + A
2833 static inline typename This::Status
2834 thm_abs5(unsigned char *view,
2835 const Sized_relobj<32, big_endian>* object,
2836 const Symbol_value<32>* psymval)
2837 {
2838 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2839 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2840 Valtype* wv = reinterpret_cast<Valtype*>(view);
2841 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2842 Reltype addend = (val & 0x7e0U) >> 6;
2daedcd6 2843 Reltype x = psymval->value(object, addend);
be8fcb75
ILT
2844 val = utils::bit_select(val, x << 6, 0x7e0U);
2845 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2846 return (utils::has_overflow<5>(x)
2847 ? This::STATUS_OVERFLOW
2848 : This::STATUS_OKAY);
2849 }
2850
2851 // R_ARM_ABS12: S + A
2852 static inline typename This::Status
2853 abs12(unsigned char *view,
51938283
DK
2854 const Sized_relobj<32, big_endian>* object,
2855 const Symbol_value<32>* psymval)
be8fcb75
ILT
2856 {
2857 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2858 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2859 Valtype* wv = reinterpret_cast<Valtype*>(view);
2860 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2861 Reltype addend = val & 0x0fffU;
2daedcd6 2862 Reltype x = psymval->value(object, addend);
be8fcb75
ILT
2863 val = utils::bit_select(val, x, 0x0fffU);
2864 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2865 return (utils::has_overflow<12>(x)
2866 ? This::STATUS_OVERFLOW
2867 : This::STATUS_OKAY);
2868 }
2869
2870 // R_ARM_ABS16: S + A
2871 static inline typename This::Status
2872 abs16(unsigned char *view,
51938283
DK
2873 const Sized_relobj<32, big_endian>* object,
2874 const Symbol_value<32>* psymval)
be8fcb75
ILT
2875 {
2876 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2877 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2878 Valtype* wv = reinterpret_cast<Valtype*>(view);
2879 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2880 Reltype addend = utils::sign_extend<16>(val);
2daedcd6 2881 Reltype x = psymval->value(object, addend);
be8fcb75
ILT
2882 val = utils::bit_select(val, x, 0xffffU);
2883 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2884 return (utils::has_signed_unsigned_overflow<16>(x)
2885 ? This::STATUS_OVERFLOW
2886 : This::STATUS_OKAY);
2887 }
2888
c121c671
DK
2889 // R_ARM_ABS32: (S + A) | T
2890 static inline typename This::Status
2891 abs32(unsigned char *view,
2892 const Sized_relobj<32, big_endian>* object,
2893 const Symbol_value<32>* psymval,
2daedcd6 2894 Arm_address thumb_bit)
c121c671
DK
2895 {
2896 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2897 Valtype* wv = reinterpret_cast<Valtype*>(view);
2898 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2daedcd6 2899 Valtype x = psymval->value(object, addend) | thumb_bit;
c121c671
DK
2900 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2901 return This::STATUS_OKAY;
2902 }
2903
2904 // R_ARM_REL32: (S + A) | T - P
2905 static inline typename This::Status
2906 rel32(unsigned char *view,
2907 const Sized_relobj<32, big_endian>* object,
2908 const Symbol_value<32>* psymval,
ebabffbd 2909 Arm_address address,
2daedcd6 2910 Arm_address thumb_bit)
c121c671
DK
2911 {
2912 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2913 Valtype* wv = reinterpret_cast<Valtype*>(view);
2914 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2daedcd6 2915 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
c121c671
DK
2916 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2917 return This::STATUS_OKAY;
2918 }
2919
089d69dc
DK
2920 // R_ARM_THM_JUMP24: (S + A) | T - P
2921 static typename This::Status
2922 thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
2923 const Symbol_value<32>* psymval, Arm_address address,
2924 Arm_address thumb_bit);
2925
800d0f56
ILT
2926 // R_ARM_THM_JUMP6: S + A – P
2927 static inline typename This::Status
2928 thm_jump6(unsigned char *view,
2929 const Sized_relobj<32, big_endian>* object,
2930 const Symbol_value<32>* psymval,
2931 Arm_address address)
2932 {
2933 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2934 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2935 Valtype* wv = reinterpret_cast<Valtype*>(view);
2936 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2937 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
2938 Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
2939 Reltype x = (psymval->value(object, addend) - address);
2940 val = (val & 0xfd07) | ((x & 0x0040) << 3) | ((val & 0x003e) << 2);
2941 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2942 // CZB does only forward jumps.
2943 return ((x > 0x007e)
2944 ? This::STATUS_OVERFLOW
2945 : This::STATUS_OKAY);
2946 }
2947
2948 // R_ARM_THM_JUMP8: S + A – P
2949 static inline typename This::Status
2950 thm_jump8(unsigned char *view,
2951 const Sized_relobj<32, big_endian>* object,
2952 const Symbol_value<32>* psymval,
2953 Arm_address address)
2954 {
2955 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2956 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2957 Valtype* wv = reinterpret_cast<Valtype*>(view);
2958 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2959 Reltype addend = utils::sign_extend<8>((val & 0x00ff) << 1);
2960 Reltype x = (psymval->value(object, addend) - address);
2961 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xff00) | ((x & 0x01fe) >> 1));
2962 return (utils::has_overflow<8>(x)
2963 ? This::STATUS_OVERFLOW
2964 : This::STATUS_OKAY);
2965 }
2966
2967 // R_ARM_THM_JUMP11: S + A – P
2968 static inline typename This::Status
2969 thm_jump11(unsigned char *view,
2970 const Sized_relobj<32, big_endian>* object,
2971 const Symbol_value<32>* psymval,
2972 Arm_address address)
2973 {
2974 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2975 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2976 Valtype* wv = reinterpret_cast<Valtype*>(view);
2977 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2978 Reltype addend = utils::sign_extend<11>((val & 0x07ff) << 1);
2979 Reltype x = (psymval->value(object, addend) - address);
2980 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xf800) | ((x & 0x0ffe) >> 1));
2981 return (utils::has_overflow<11>(x)
2982 ? This::STATUS_OVERFLOW
2983 : This::STATUS_OKAY);
2984 }
2985
c121c671
DK
2986 // R_ARM_BASE_PREL: B(S) + A - P
2987 static inline typename This::Status
2988 base_prel(unsigned char* view,
ebabffbd
DK
2989 Arm_address origin,
2990 Arm_address address)
c121c671
DK
2991 {
2992 Base::rel32(view, origin - address);
2993 return STATUS_OKAY;
2994 }
2995
be8fcb75
ILT
2996 // R_ARM_BASE_ABS: B(S) + A
2997 static inline typename This::Status
2998 base_abs(unsigned char* view,
f4e5969c 2999 Arm_address origin)
be8fcb75
ILT
3000 {
3001 Base::rel32(view, origin);
3002 return STATUS_OKAY;
3003 }
3004
c121c671
DK
3005 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
3006 static inline typename This::Status
3007 got_brel(unsigned char* view,
3008 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
3009 {
3010 Base::rel32(view, got_offset);
3011 return This::STATUS_OKAY;
3012 }
3013
f4e5969c 3014 // R_ARM_GOT_PREL: GOT(S) + A - P
7f5309a5 3015 static inline typename This::Status
f4e5969c
DK
3016 got_prel(unsigned char *view,
3017 Arm_address got_entry,
ebabffbd 3018 Arm_address address)
7f5309a5 3019 {
f4e5969c 3020 Base::rel32(view, got_entry - address);
7f5309a5
ILT
3021 return This::STATUS_OKAY;
3022 }
3023
c121c671
DK
3024 // R_ARM_PREL: (S + A) | T - P
3025 static inline typename This::Status
3026 prel31(unsigned char *view,
3027 const Sized_relobj<32, big_endian>* object,
3028 const Symbol_value<32>* psymval,
ebabffbd 3029 Arm_address address,
2daedcd6 3030 Arm_address thumb_bit)
c121c671
DK
3031 {
3032 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3033 Valtype* wv = reinterpret_cast<Valtype*>(view);
3034 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3035 Valtype addend = utils::sign_extend<31>(val);
2daedcd6 3036 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
c121c671
DK
3037 val = utils::bit_select(val, x, 0x7fffffffU);
3038 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3039 return (utils::has_overflow<31>(x) ?
3040 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3041 }
fd3c5f0b 3042
5c57f1be 3043 // R_ARM_MOVW_ABS_NC: (S + A) | T (relative address base is )
c2a122b6 3044 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
5c57f1be
DK
3045 // R_ARM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3046 // R_ARM_MOVW_BREL: ((S + A) | T) - B(S)
02961d7e 3047 static inline typename This::Status
5c57f1be
DK
3048 movw(unsigned char* view,
3049 const Sized_relobj<32, big_endian>* object,
3050 const Symbol_value<32>* psymval,
3051 Arm_address relative_address_base,
3052 Arm_address thumb_bit,
3053 bool check_overflow)
02961d7e
ILT
3054 {
3055 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3056 Valtype* wv = reinterpret_cast<Valtype*>(view);
3057 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3058 Valtype addend = This::extract_arm_movw_movt_addend(val);
5c57f1be
DK
3059 Valtype x = ((psymval->value(object, addend) | thumb_bit)
3060 - relative_address_base);
02961d7e
ILT
3061 val = This::insert_val_arm_movw_movt(val, x);
3062 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5c57f1be
DK
3063 return ((check_overflow && utils::has_overflow<16>(x))
3064 ? This::STATUS_OVERFLOW
3065 : This::STATUS_OKAY);
02961d7e
ILT
3066 }
3067
5c57f1be 3068 // R_ARM_MOVT_ABS: S + A (relative address base is 0)
c2a122b6 3069 // R_ARM_MOVT_PREL: S + A - P
5c57f1be 3070 // R_ARM_MOVT_BREL: S + A - B(S)
c2a122b6 3071 static inline typename This::Status
5c57f1be
DK
3072 movt(unsigned char* view,
3073 const Sized_relobj<32, big_endian>* object,
3074 const Symbol_value<32>* psymval,
3075 Arm_address relative_address_base)
c2a122b6
ILT
3076 {
3077 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3078 Valtype* wv = reinterpret_cast<Valtype*>(view);
3079 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3080 Valtype addend = This::extract_arm_movw_movt_addend(val);
5c57f1be 3081 Valtype x = (psymval->value(object, addend) - relative_address_base) >> 16;
c2a122b6
ILT
3082 val = This::insert_val_arm_movw_movt(val, x);
3083 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5c57f1be 3084 // FIXME: IHI0044D says that we should check for overflow.
c2a122b6
ILT
3085 return This::STATUS_OKAY;
3086 }
3087
5c57f1be 3088 // R_ARM_THM_MOVW_ABS_NC: S + A | T (relative_address_base is 0)
c2a122b6 3089 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
5c57f1be
DK
3090 // R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3091 // R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
02961d7e 3092 static inline typename This::Status
5c57f1be
DK
3093 thm_movw(unsigned char *view,
3094 const Sized_relobj<32, big_endian>* object,
3095 const Symbol_value<32>* psymval,
3096 Arm_address relative_address_base,
3097 Arm_address thumb_bit,
3098 bool check_overflow)
02961d7e
ILT
3099 {
3100 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3101 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3102 Valtype* wv = reinterpret_cast<Valtype*>(view);
3103 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3104 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3105 Reltype addend = This::extract_thumb_movw_movt_addend(val);
5c57f1be
DK
3106 Reltype x =
3107 (psymval->value(object, addend) | thumb_bit) - relative_address_base;
02961d7e
ILT
3108 val = This::insert_val_thumb_movw_movt(val, x);
3109 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3110 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
5c57f1be
DK
3111 return ((check_overflow && utils::has_overflow<16>(x))
3112 ? This::STATUS_OVERFLOW
3113 : This::STATUS_OKAY);
02961d7e
ILT
3114 }
3115
5c57f1be 3116 // R_ARM_THM_MOVT_ABS: S + A (relative address base is 0)
c2a122b6 3117 // R_ARM_THM_MOVT_PREL: S + A - P
5c57f1be 3118 // R_ARM_THM_MOVT_BREL: S + A - B(S)
c2a122b6 3119 static inline typename This::Status
5c57f1be
DK
3120 thm_movt(unsigned char* view,
3121 const Sized_relobj<32, big_endian>* object,
3122 const Symbol_value<32>* psymval,
3123 Arm_address relative_address_base)
c2a122b6
ILT
3124 {
3125 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3126 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3127 Valtype* wv = reinterpret_cast<Valtype*>(view);
3128 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3129 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3130 Reltype addend = This::extract_thumb_movw_movt_addend(val);
5c57f1be 3131 Reltype x = (psymval->value(object, addend) - relative_address_base) >> 16;
c2a122b6
ILT
3132 val = This::insert_val_thumb_movw_movt(val, x);
3133 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3134 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3135 return This::STATUS_OKAY;
3136 }
a2162063 3137
11b861d5
DK
3138 // R_ARM_THM_ALU_PREL_11_0: ((S + A) | T) - Pa (Thumb32)
3139 static inline typename This::Status
3140 thm_alu11(unsigned char* view,
3141 const Sized_relobj<32, big_endian>* object,
3142 const Symbol_value<32>* psymval,
3143 Arm_address address,
3144 Arm_address thumb_bit)
3145 {
3146 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3147 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3148 Valtype* wv = reinterpret_cast<Valtype*>(view);
3149 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3150 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3151
3152 // 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
3153 // -----------------------------------------------------------------------
3154 // ADD{S} 1 1 1 1 0|i|0|1 0 0 0|S|1 1 0 1||0|imm3 |Rd |imm8
3155 // ADDW 1 1 1 1 0|i|1|0 0 0 0|0|1 1 0 1||0|imm3 |Rd |imm8
3156 // ADR[+] 1 1 1 1 0|i|1|0 0 0 0|0|1 1 1 1||0|imm3 |Rd |imm8
3157 // SUB{S} 1 1 1 1 0|i|0|1 1 0 1|S|1 1 0 1||0|imm3 |Rd |imm8
3158 // SUBW 1 1 1 1 0|i|1|0 1 0 1|0|1 1 0 1||0|imm3 |Rd |imm8
3159 // ADR[-] 1 1 1 1 0|i|1|0 1 0 1|0|1 1 1 1||0|imm3 |Rd |imm8
3160
3161 // Determine a sign for the addend.
3162 const int sign = ((insn & 0xf8ef0000) == 0xf0ad0000
3163 || (insn & 0xf8ef0000) == 0xf0af0000) ? -1 : 1;
3164 // Thumb2 addend encoding:
3165 // imm12 := i | imm3 | imm8
3166 int32_t addend = (insn & 0xff)
3167 | ((insn & 0x00007000) >> 4)
3168 | ((insn & 0x04000000) >> 15);
3169 // Apply a sign to the added.
3170 addend *= sign;
3171
3172 int32_t x = (psymval->value(object, addend) | thumb_bit)
3173 - (address & 0xfffffffc);
3174 Reltype val = abs(x);
3175 // Mask out the value and a distinct part of the ADD/SUB opcode
3176 // (bits 7:5 of opword).
3177 insn = (insn & 0xfb0f8f00)
3178 | (val & 0xff)
3179 | ((val & 0x700) << 4)
3180 | ((val & 0x800) << 15);
3181 // Set the opcode according to whether the value to go in the
3182 // place is negative.
3183 if (x < 0)
3184 insn |= 0x00a00000;
3185
3186 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3187 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3188 return ((val > 0xfff) ?
3189 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3190 }
3191
3192 // R_ARM_THM_PC8: S + A - Pa (Thumb)
3193 static inline typename This::Status
3194 thm_pc8(unsigned char* view,
3195 const Sized_relobj<32, big_endian>* object,
3196 const Symbol_value<32>* psymval,
3197 Arm_address address)
3198 {
3199 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3200 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3201 Valtype* wv = reinterpret_cast<Valtype*>(view);
3202 Valtype insn = elfcpp::Swap<16, big_endian>::readval(wv);
3203 Reltype addend = ((insn & 0x00ff) << 2);
3204 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3205 Reltype val = abs(x);
3206 insn = (insn & 0xff00) | ((val & 0x03fc) >> 2);
3207
3208 elfcpp::Swap<16, big_endian>::writeval(wv, insn);
3209 return ((val > 0x03fc)
3210 ? This::STATUS_OVERFLOW
3211 : This::STATUS_OKAY);
3212 }
3213
3214 // R_ARM_THM_PC12: S + A - Pa (Thumb32)
3215 static inline typename This::Status
3216 thm_pc12(unsigned char* view,
3217 const Sized_relobj<32, big_endian>* object,
3218 const Symbol_value<32>* psymval,
3219 Arm_address address)
3220 {
3221 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3222 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3223 Valtype* wv = reinterpret_cast<Valtype*>(view);
3224 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3225 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3226 // Determine a sign for the addend (positive if the U bit is 1).
3227 const int sign = (insn & 0x00800000) ? 1 : -1;
3228 int32_t addend = (insn & 0xfff);
3229 // Apply a sign to the added.
3230 addend *= sign;
3231
3232 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3233 Reltype val = abs(x);
3234 // Mask out and apply the value and the U bit.
3235 insn = (insn & 0xff7ff000) | (val & 0xfff);
3236 // Set the U bit according to whether the value to go in the
3237 // place is positive.
3238 if (x >= 0)
3239 insn |= 0x00800000;
3240
3241 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3242 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3243 return ((val > 0xfff) ?
3244 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3245 }
3246
a2162063
ILT
3247 // R_ARM_V4BX
3248 static inline typename This::Status
3249 v4bx(const Relocate_info<32, big_endian>* relinfo,
3250 unsigned char *view,
3251 const Arm_relobj<big_endian>* object,
3252 const Arm_address address,
3253 const bool is_interworking)
3254 {
3255
3256 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3257 Valtype* wv = reinterpret_cast<Valtype*>(view);
3258 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3259
3260 // Ensure that we have a BX instruction.
3261 gold_assert((val & 0x0ffffff0) == 0x012fff10);
3262 const uint32_t reg = (val & 0xf);
3263 if (is_interworking && reg != 0xf)
3264 {
3265 Stub_table<big_endian>* stub_table =
3266 object->stub_table(relinfo->data_shndx);
3267 gold_assert(stub_table != NULL);
3268
3269 Arm_v4bx_stub* stub = stub_table->find_arm_v4bx_stub(reg);
3270 gold_assert(stub != NULL);
3271
3272 int32_t veneer_address =
3273 stub_table->address() + stub->offset() - 8 - address;
3274 gold_assert((veneer_address <= ARM_MAX_FWD_BRANCH_OFFSET)
3275 && (veneer_address >= ARM_MAX_BWD_BRANCH_OFFSET));
3276 // Replace with a branch to veneer (B <addr>)
3277 val = (val & 0xf0000000) | 0x0a000000
3278 | ((veneer_address >> 2) & 0x00ffffff);
3279 }
3280 else
3281 {
3282 // Preserve Rm (lowest four bits) and the condition code
3283 // (highest four bits). Other bits encode MOV PC,Rm.
3284 val = (val & 0xf000000f) | 0x01a0f000;
3285 }
3286 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3287 return This::STATUS_OKAY;
3288 }
b10d2873
ILT
3289
3290 // R_ARM_ALU_PC_G0_NC: ((S + A) | T) - P
3291 // R_ARM_ALU_PC_G0: ((S + A) | T) - P
3292 // R_ARM_ALU_PC_G1_NC: ((S + A) | T) - P
3293 // R_ARM_ALU_PC_G1: ((S + A) | T) - P
3294 // R_ARM_ALU_PC_G2: ((S + A) | T) - P
3295 // R_ARM_ALU_SB_G0_NC: ((S + A) | T) - B(S)
3296 // R_ARM_ALU_SB_G0: ((S + A) | T) - B(S)
3297 // R_ARM_ALU_SB_G1_NC: ((S + A) | T) - B(S)
3298 // R_ARM_ALU_SB_G1: ((S + A) | T) - B(S)
3299 // R_ARM_ALU_SB_G2: ((S + A) | T) - B(S)
3300 static inline typename This::Status
3301 arm_grp_alu(unsigned char* view,
3302 const Sized_relobj<32, big_endian>* object,
3303 const Symbol_value<32>* psymval,
3304 const int group,
3305 Arm_address address,
3306 Arm_address thumb_bit,
3307 bool check_overflow)
3308 {
5c57f1be 3309 gold_assert(group >= 0 && group < 3);
b10d2873
ILT
3310 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3311 Valtype* wv = reinterpret_cast<Valtype*>(view);
3312 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3313
3314 // ALU group relocations are allowed only for the ADD/SUB instructions.
3315 // (0x00800000 - ADD, 0x00400000 - SUB)
3316 const Valtype opcode = insn & 0x01e00000;
3317 if (opcode != 0x00800000 && opcode != 0x00400000)
3318 return This::STATUS_BAD_RELOC;
3319
3320 // Determine a sign for the addend.
3321 const int sign = (opcode == 0x00800000) ? 1 : -1;
3322 // shifter = rotate_imm * 2
3323 const uint32_t shifter = (insn & 0xf00) >> 7;
3324 // Initial addend value.
3325 int32_t addend = insn & 0xff;
3326 // Rotate addend right by shifter.
3327 addend = (addend >> shifter) | (addend << (32 - shifter));
3328 // Apply a sign to the added.
3329 addend *= sign;
3330
3331 int32_t x = ((psymval->value(object, addend) | thumb_bit) - address);
3332 Valtype gn = Arm_relocate_functions::calc_grp_gn(abs(x), group);
3333 // Check for overflow if required
3334 if (check_overflow
3335 && (Arm_relocate_functions::calc_grp_residual(abs(x), group) != 0))
3336 return This::STATUS_OVERFLOW;
3337
3338 // Mask out the value and the ADD/SUB part of the opcode; take care
3339 // not to destroy the S bit.
3340 insn &= 0xff1ff000;
3341 // Set the opcode according to whether the value to go in the
3342 // place is negative.
3343 insn |= ((x < 0) ? 0x00400000 : 0x00800000);
3344 // Encode the offset (encoded Gn).
3345 insn |= gn;
3346
3347 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3348 return This::STATUS_OKAY;
3349 }
3350
3351 // R_ARM_LDR_PC_G0: S + A - P
3352 // R_ARM_LDR_PC_G1: S + A - P
3353 // R_ARM_LDR_PC_G2: S + A - P
3354 // R_ARM_LDR_SB_G0: S + A - B(S)
3355 // R_ARM_LDR_SB_G1: S + A - B(S)
3356 // R_ARM_LDR_SB_G2: S + A - B(S)
3357 static inline typename This::Status
3358 arm_grp_ldr(unsigned char* view,
3359 const Sized_relobj<32, big_endian>* object,
3360 const Symbol_value<32>* psymval,
3361 const int group,
3362 Arm_address address)
3363 {
5c57f1be 3364 gold_assert(group >= 0 && group < 3);
b10d2873
ILT
3365 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3366 Valtype* wv = reinterpret_cast<Valtype*>(view);
3367 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3368
3369 const int sign = (insn & 0x00800000) ? 1 : -1;
3370 int32_t addend = (insn & 0xfff) * sign;
3371 int32_t x = (psymval->value(object, addend) - address);
3372 // Calculate the relevant G(n-1) value to obtain this stage residual.
3373 Valtype residual =
3374 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3375 if (residual >= 0x1000)
3376 return This::STATUS_OVERFLOW;
3377
3378 // Mask out the value and U bit.
3379 insn &= 0xff7ff000;
3380 // Set the U bit for non-negative values.
3381 if (x >= 0)
3382 insn |= 0x00800000;
3383 insn |= residual;
3384
3385 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3386 return This::STATUS_OKAY;
3387 }
3388
3389 // R_ARM_LDRS_PC_G0: S + A - P
3390 // R_ARM_LDRS_PC_G1: S + A - P
3391 // R_ARM_LDRS_PC_G2: S + A - P
3392 // R_ARM_LDRS_SB_G0: S + A - B(S)
3393 // R_ARM_LDRS_SB_G1: S + A - B(S)
3394 // R_ARM_LDRS_SB_G2: S + A - B(S)
3395 static inline typename This::Status
3396 arm_grp_ldrs(unsigned char* view,
3397 const Sized_relobj<32, big_endian>* object,
3398 const Symbol_value<32>* psymval,
3399 const int group,
3400 Arm_address address)
3401 {
5c57f1be 3402 gold_assert(group >= 0 && group < 3);
b10d2873
ILT
3403 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3404 Valtype* wv = reinterpret_cast<Valtype*>(view);
3405 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3406
3407 const int sign = (insn & 0x00800000) ? 1 : -1;
3408 int32_t addend = (((insn & 0xf00) >> 4) + (insn & 0xf)) * sign;
3409 int32_t x = (psymval->value(object, addend) - address);
3410 // Calculate the relevant G(n-1) value to obtain this stage residual.
3411 Valtype residual =
3412 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3413 if (residual >= 0x100)
3414 return This::STATUS_OVERFLOW;
3415
3416 // Mask out the value and U bit.
3417 insn &= 0xff7ff0f0;
3418 // Set the U bit for non-negative values.
3419 if (x >= 0)
3420 insn |= 0x00800000;
3421 insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
3422
3423 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3424 return This::STATUS_OKAY;
3425 }
3426
3427 // R_ARM_LDC_PC_G0: S + A - P
3428 // R_ARM_LDC_PC_G1: S + A - P
3429 // R_ARM_LDC_PC_G2: S + A - P
3430 // R_ARM_LDC_SB_G0: S + A - B(S)
3431 // R_ARM_LDC_SB_G1: S + A - B(S)
3432 // R_ARM_LDC_SB_G2: S + A - B(S)
3433 static inline typename This::Status
3434 arm_grp_ldc(unsigned char* view,
3435 const Sized_relobj<32, big_endian>* object,
3436 const Symbol_value<32>* psymval,
3437 const int group,
3438 Arm_address address)
3439 {
5c57f1be 3440 gold_assert(group >= 0 && group < 3);
b10d2873
ILT
3441 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3442 Valtype* wv = reinterpret_cast<Valtype*>(view);
3443 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3444
3445 const int sign = (insn & 0x00800000) ? 1 : -1;
3446 int32_t addend = ((insn & 0xff) << 2) * sign;
3447 int32_t x = (psymval->value(object, addend) - address);
3448 // Calculate the relevant G(n-1) value to obtain this stage residual.
3449 Valtype residual =
3450 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3451 if ((residual & 0x3) != 0 || residual >= 0x400)
3452 return This::STATUS_OVERFLOW;
3453
3454 // Mask out the value and U bit.
3455 insn &= 0xff7fff00;
3456 // Set the U bit for non-negative values.
3457 if (x >= 0)
3458 insn |= 0x00800000;
3459 insn |= (residual >> 2);
3460
3461 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3462 return This::STATUS_OKAY;
3463 }
c121c671
DK
3464};
3465
d204b6e9
DK
3466// Relocate ARM long branches. This handles relocation types
3467// R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3468// If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3469// undefined and we do not use PLT in this relocation. In such a case,
3470// the branch is converted into an NOP.
3471
3472template<bool big_endian>
3473typename Arm_relocate_functions<big_endian>::Status
3474Arm_relocate_functions<big_endian>::arm_branch_common(
3475 unsigned int r_type,
3476 const Relocate_info<32, big_endian>* relinfo,
3477 unsigned char *view,
3478 const Sized_symbol<32>* gsym,
3479 const Arm_relobj<big_endian>* object,
3480 unsigned int r_sym,
3481 const Symbol_value<32>* psymval,
3482 Arm_address address,
3483 Arm_address thumb_bit,
3484 bool is_weakly_undefined_without_plt)
3485{
3486 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3487 Valtype* wv = reinterpret_cast<Valtype*>(view);
3488 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3489
3490 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
3491 && ((val & 0x0f000000UL) == 0x0a000000UL);
3492 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
3493 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
3494 && ((val & 0x0f000000UL) == 0x0b000000UL);
3495 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
3496 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
3497
3498 // Check that the instruction is valid.
3499 if (r_type == elfcpp::R_ARM_CALL)
3500 {
3501 if (!insn_is_uncond_bl && !insn_is_blx)
3502 return This::STATUS_BAD_RELOC;
3503 }
3504 else if (r_type == elfcpp::R_ARM_JUMP24)
3505 {
3506 if (!insn_is_b && !insn_is_cond_bl)
3507 return This::STATUS_BAD_RELOC;
3508 }
3509 else if (r_type == elfcpp::R_ARM_PLT32)
3510 {
3511 if (!insn_is_any_branch)
3512 return This::STATUS_BAD_RELOC;
3513 }
3514 else if (r_type == elfcpp::R_ARM_XPC25)
3515 {
3516 // FIXME: AAELF document IH0044C does not say much about it other
3517 // than it being obsolete.
3518 if (!insn_is_any_branch)
3519 return This::STATUS_BAD_RELOC;
3520 }
3521 else
3522 gold_unreachable();
3523
3524 // A branch to an undefined weak symbol is turned into a jump to
3525 // the next instruction unless a PLT entry will be created.
3526 // Do the same for local undefined symbols.
3527 // The jump to the next instruction is optimized as a NOP depending
3528 // on the architecture.
3529 const Target_arm<big_endian>* arm_target =
3530 Target_arm<big_endian>::default_target();
3531 if (is_weakly_undefined_without_plt)
3532 {
3533 Valtype cond = val & 0xf0000000U;
3534 if (arm_target->may_use_arm_nop())
3535 val = cond | 0x0320f000;
3536 else
3537 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3538 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3539 return This::STATUS_OKAY;
3540 }
3541
3542 Valtype addend = utils::sign_extend<26>(val << 2);
3543 Valtype branch_target = psymval->value(object, addend);
3544 int32_t branch_offset = branch_target - address;
3545
3546 // We need a stub if the branch offset is too large or if we need
3547 // to switch mode.
3548 bool may_use_blx = arm_target->may_use_blx();
3549 Reloc_stub* stub = NULL;
3550 if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
3551 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3552 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
3553 {
3554 Stub_type stub_type =
3555 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
3556 (thumb_bit != 0));
3557 if (stub_type != arm_stub_none)
3558 {
2ea97941 3559 Stub_table<big_endian>* stub_table =
d204b6e9 3560 object->stub_table(relinfo->data_shndx);
2ea97941 3561 gold_assert(stub_table != NULL);
d204b6e9
DK
3562
3563 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2ea97941 3564 stub = stub_table->find_reloc_stub(stub_key);
d204b6e9
DK
3565 gold_assert(stub != NULL);
3566 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2ea97941 3567 branch_target = stub_table->address() + stub->offset() + addend;
d204b6e9
DK
3568 branch_offset = branch_target - address;
3569 gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
3570 && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
3571 }
3572 }
3573
3574 // At this point, if we still need to switch mode, the instruction
3575 // must either be a BLX or a BL that can be converted to a BLX.
3576 if (thumb_bit != 0)
3577 {
3578 // Turn BL to BLX.
3579 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
3580 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
3581 }
3582
3583 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
3584 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3585 return (utils::has_overflow<26>(branch_offset)
3586 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
3587}
3588
51938283
DK
3589// Relocate THUMB long branches. This handles relocation types
3590// R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3591// If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3592// undefined and we do not use PLT in this relocation. In such a case,
3593// the branch is converted into an NOP.
3594
3595template<bool big_endian>
3596typename Arm_relocate_functions<big_endian>::Status
3597Arm_relocate_functions<big_endian>::thumb_branch_common(
3598 unsigned int r_type,
3599 const Relocate_info<32, big_endian>* relinfo,
3600 unsigned char *view,
3601 const Sized_symbol<32>* gsym,
3602 const Arm_relobj<big_endian>* object,
3603 unsigned int r_sym,
3604 const Symbol_value<32>* psymval,
3605 Arm_address address,
3606 Arm_address thumb_bit,
3607 bool is_weakly_undefined_without_plt)
3608{
3609 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3610 Valtype* wv = reinterpret_cast<Valtype*>(view);
3611 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3612 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3613
3614 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3615 // into account.
3616 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
3617 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
3618
3619 // Check that the instruction is valid.
3620 if (r_type == elfcpp::R_ARM_THM_CALL)
3621 {
3622 if (!is_bl_insn && !is_blx_insn)
3623 return This::STATUS_BAD_RELOC;
3624 }
3625 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
3626 {
3627 // This cannot be a BLX.
3628 if (!is_bl_insn)
3629 return This::STATUS_BAD_RELOC;
3630 }
3631 else if (r_type == elfcpp::R_ARM_THM_XPC22)
3632 {
3633 // Check for Thumb to Thumb call.
3634 if (!is_blx_insn)
3635 return This::STATUS_BAD_RELOC;
3636 if (thumb_bit != 0)
3637 {
3638 gold_warning(_("%s: Thumb BLX instruction targets "
3639 "thumb function '%s'."),
3640 object->name().c_str(),
3641 (gsym ? gsym->name() : "(local)"));
3642 // Convert BLX to BL.
3643 lower_insn |= 0x1000U;
3644 }
3645 }
3646 else
3647 gold_unreachable();
3648
3649 // A branch to an undefined weak symbol is turned into a jump to
3650 // the next instruction unless a PLT entry will be created.
3651 // The jump to the next instruction is optimized as a NOP.W for
3652 // Thumb-2 enabled architectures.
3653 const Target_arm<big_endian>* arm_target =
3654 Target_arm<big_endian>::default_target();
3655 if (is_weakly_undefined_without_plt)
3656 {
3657 if (arm_target->may_use_thumb2_nop())
3658 {
3659 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
3660 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
3661 }
3662 else
3663 {
3664 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
3665 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
3666 }
3667 return This::STATUS_OKAY;
3668 }
3669
089d69dc 3670 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
51938283
DK
3671 Arm_address branch_target = psymval->value(object, addend);
3672 int32_t branch_offset = branch_target - address;
3673
3674 // We need a stub if the branch offset is too large or if we need
3675 // to switch mode.
3676 bool may_use_blx = arm_target->may_use_blx();
3677 bool thumb2 = arm_target->using_thumb2();
3678 if ((!thumb2
3679 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3680 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3681 || (thumb2
3682 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3683 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3684 || ((thumb_bit == 0)
3685 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3686 || r_type == elfcpp::R_ARM_THM_JUMP24)))
3687 {
3688 Stub_type stub_type =
3689 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
3690 (thumb_bit != 0));
3691 if (stub_type != arm_stub_none)
3692 {
2ea97941 3693 Stub_table<big_endian>* stub_table =
51938283 3694 object->stub_table(relinfo->data_shndx);
2ea97941 3695 gold_assert(stub_table != NULL);
51938283
DK
3696
3697 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2ea97941 3698 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
51938283
DK
3699 gold_assert(stub != NULL);
3700 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2ea97941 3701 branch_target = stub_table->address() + stub->offset() + addend;
51938283
DK
3702 branch_offset = branch_target - address;
3703 }
3704 }
3705
3706 // At this point, if we still need to switch mode, the instruction
3707 // must either be a BLX or a BL that can be converted to a BLX.
3708 if (thumb_bit == 0)
3709 {
3710 gold_assert(may_use_blx
3711 && (r_type == elfcpp::R_ARM_THM_CALL
3712 || r_type == elfcpp::R_ARM_THM_XPC22));
3713 // Make sure this is a BLX.
3714 lower_insn &= ~0x1000U;
3715 }
3716 else
3717 {
3718 // Make sure this is a BL.
3719 lower_insn |= 0x1000U;
3720 }
3721
51938283
DK
3722 if ((lower_insn & 0x5000U) == 0x4000U)
3723 // For a BLX instruction, make sure that the relocation is rounded up
3724 // to a word boundary. This follows the semantics of the instruction
3725 // which specifies that bit 1 of the target address will come from bit
3726 // 1 of the base address.
089d69dc 3727 branch_offset = (branch_offset + 2) & ~3;
51938283
DK
3728
3729 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3730 // We use the Thumb-2 encoding, which is safe even if dealing with
3731 // a Thumb-1 instruction by virtue of our overflow check above. */
089d69dc
DK
3732 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
3733 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
51938283
DK
3734
3735 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3736 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3737
3738 return ((thumb2
089d69dc
DK
3739 ? utils::has_overflow<25>(branch_offset)
3740 : utils::has_overflow<23>(branch_offset))
3741 ? This::STATUS_OVERFLOW
3742 : This::STATUS_OKAY);
3743}
3744
3745// Relocate THUMB-2 long conditional branches.
3746// If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3747// undefined and we do not use PLT in this relocation. In such a case,
3748// the branch is converted into an NOP.
3749
3750template<bool big_endian>
3751typename Arm_relocate_functions<big_endian>::Status
3752Arm_relocate_functions<big_endian>::thm_jump19(
3753 unsigned char *view,
3754 const Arm_relobj<big_endian>* object,
3755 const Symbol_value<32>* psymval,
3756 Arm_address address,
3757 Arm_address thumb_bit)
3758{
3759 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3760 Valtype* wv = reinterpret_cast<Valtype*>(view);
3761 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3762 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3763 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
3764
3765 Arm_address branch_target = psymval->value(object, addend);
3766 int32_t branch_offset = branch_target - address;
3767
3768 // ??? Should handle interworking? GCC might someday try to
3769 // use this for tail calls.
3770 // FIXME: We do support thumb entry to PLT yet.
3771 if (thumb_bit == 0)
3772 {
3773 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3774 return This::STATUS_BAD_RELOC;
3775 }
3776
3777 // Put RELOCATION back into the insn.
3778 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
3779 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
3780
3781 // Put the relocated value back in the object file:
3782 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3783 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3784
3785 return (utils::has_overflow<21>(branch_offset)
51938283
DK
3786 ? This::STATUS_OVERFLOW
3787 : This::STATUS_OKAY);
3788}
3789
94cdfcff
DK
3790// Get the GOT section, creating it if necessary.
3791
3792template<bool big_endian>
3793Output_data_got<32, big_endian>*
3794Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
3795{
3796 if (this->got_ == NULL)
3797 {
3798 gold_assert(symtab != NULL && layout != NULL);
3799
3800 this->got_ = new Output_data_got<32, big_endian>();
3801
3802 Output_section* os;
3803 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3804 (elfcpp::SHF_ALLOC
3805 | elfcpp::SHF_WRITE),
1a2dff53
ILT
3806 this->got_, false, true, true,
3807 false);
94cdfcff
DK
3808
3809 // The old GNU linker creates a .got.plt section. We just
3810 // create another set of data in the .got section. Note that we
3811 // always create a PLT if we create a GOT, although the PLT
3812 // might be empty.
3813 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
3814 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3815 (elfcpp::SHF_ALLOC
3816 | elfcpp::SHF_WRITE),
1a2dff53
ILT
3817 this->got_plt_, false, false,
3818 false, true);
94cdfcff
DK
3819
3820 // The first three entries are reserved.
3821 this->got_plt_->set_current_data_size(3 * 4);
3822
3823 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3824 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
99fff23b 3825 Symbol_table::PREDEFINED,
94cdfcff
DK
3826 this->got_plt_,
3827 0, 0, elfcpp::STT_OBJECT,
3828 elfcpp::STB_LOCAL,
3829 elfcpp::STV_HIDDEN, 0,
3830 false, false);
3831 }
3832 return this->got_;
3833}
3834
3835// Get the dynamic reloc section, creating it if necessary.
3836
3837template<bool big_endian>
3838typename Target_arm<big_endian>::Reloc_section*
3839Target_arm<big_endian>::rel_dyn_section(Layout* layout)
3840{
3841 if (this->rel_dyn_ == NULL)
3842 {
3843 gold_assert(layout != NULL);
3844 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
3845 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1a2dff53
ILT
3846 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
3847 false, false, false);
94cdfcff
DK
3848 }
3849 return this->rel_dyn_;
3850}
3851
b569affa
DK
3852// Insn_template methods.
3853
3854// Return byte size of an instruction template.
3855
3856size_t
3857Insn_template::size() const
3858{
3859 switch (this->type())
3860 {
3861 case THUMB16_TYPE:
2fb7225c 3862 case THUMB16_SPECIAL_TYPE:
b569affa
DK
3863 return 2;
3864 case ARM_TYPE:
3865 case THUMB32_TYPE:
3866 case DATA_TYPE:
3867 return 4;
3868 default:
3869 gold_unreachable();
3870 }
3871}
3872
3873// Return alignment of an instruction template.
3874
3875unsigned
3876Insn_template::alignment() const
3877{
3878 switch (this->type())
3879 {
3880 case THUMB16_TYPE:
2fb7225c 3881 case THUMB16_SPECIAL_TYPE:
b569affa
DK
3882 case THUMB32_TYPE:
3883 return 2;
3884 case ARM_TYPE:
3885 case DATA_TYPE:
3886 return 4;
3887 default:
3888 gold_unreachable();
3889 }
3890}
3891
3892// Stub_template methods.
3893
3894Stub_template::Stub_template(
2ea97941
ILT
3895 Stub_type type, const Insn_template* insns,
3896 size_t insn_count)
3897 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
b569affa
DK
3898 entry_in_thumb_mode_(false), relocs_()
3899{
2ea97941 3900 off_t offset = 0;
b569affa
DK
3901
3902 // Compute byte size and alignment of stub template.
2ea97941 3903 for (size_t i = 0; i < insn_count; i++)
b569affa 3904 {
2ea97941
ILT
3905 unsigned insn_alignment = insns[i].alignment();
3906 size_t insn_size = insns[i].size();
3907 gold_assert((offset & (insn_alignment - 1)) == 0);
b569affa 3908 this->alignment_ = std::max(this->alignment_, insn_alignment);
2ea97941 3909 switch (insns[i].type())
b569affa
DK
3910 {
3911 case Insn_template::THUMB16_TYPE:
089d69dc 3912 case Insn_template::THUMB16_SPECIAL_TYPE:
b569affa
DK
3913 if (i == 0)
3914 this->entry_in_thumb_mode_ = true;
3915 break;
3916
3917 case Insn_template::THUMB32_TYPE:
2ea97941
ILT
3918 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
3919 this->relocs_.push_back(Reloc(i, offset));
b569affa
DK
3920 if (i == 0)
3921 this->entry_in_thumb_mode_ = true;
3922 break;
3923
3924 case Insn_template::ARM_TYPE:
3925 // Handle cases where the target is encoded within the
3926 // instruction.
2ea97941
ILT
3927 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
3928 this->relocs_.push_back(Reloc(i, offset));
b569affa
DK
3929 break;
3930
3931 case Insn_template::DATA_TYPE:
3932 // Entry point cannot be data.
3933 gold_assert(i != 0);
2ea97941 3934 this->relocs_.push_back(Reloc(i, offset));
b569affa
DK
3935 break;
3936
3937 default:
3938 gold_unreachable();
3939 }
2ea97941 3940 offset += insn_size;
b569affa 3941 }
2ea97941 3942 this->size_ = offset;
b569affa
DK
3943}
3944
bb0d3eb0
DK
3945// Stub methods.
3946
3947// Template to implement do_write for a specific target endianity.
3948
3949template<bool big_endian>
3950void inline
3951Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
3952{
3953 const Stub_template* stub_template = this->stub_template();
3954 const Insn_template* insns = stub_template->insns();
3955
3956 // FIXME: We do not handle BE8 encoding yet.
3957 unsigned char* pov = view;
3958 for (size_t i = 0; i < stub_template->insn_count(); i++)
3959 {
3960 switch (insns[i].type())
3961 {
3962 case Insn_template::THUMB16_TYPE:
3963 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
3964 break;
3965 case Insn_template::THUMB16_SPECIAL_TYPE:
3966 elfcpp::Swap<16, big_endian>::writeval(
3967 pov,
3968 this->thumb16_special(i));
3969 break;
3970 case Insn_template::THUMB32_TYPE:
3971 {
3972 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
3973 uint32_t lo = insns[i].data() & 0xffff;
3974 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
3975 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
3976 }
3977 break;
3978 case Insn_template::ARM_TYPE:
3979 case Insn_template::DATA_TYPE:
3980 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
3981 break;
3982 default:
3983 gold_unreachable();
3984 }
3985 pov += insns[i].size();
3986 }
3987 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
3988}
3989
b569affa
DK
3990// Reloc_stub::Key methods.
3991
3992// Dump a Key as a string for debugging.
3993
3994std::string
3995Reloc_stub::Key::name() const
3996{
3997 if (this->r_sym_ == invalid_index)
3998 {
3999 // Global symbol key name
4000 // <stub-type>:<symbol name>:<addend>.
4001 const std::string sym_name = this->u_.symbol->name();
4002 // We need to print two hex number and two colons. So just add 100 bytes
4003 // to the symbol name size.
4004 size_t len = sym_name.size() + 100;
4005 char* buffer = new char[len];
4006 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
4007 sym_name.c_str(), this->addend_);
4008 gold_assert(c > 0 && c < static_cast<int>(len));
4009 delete[] buffer;
4010 return std::string(buffer);
4011 }
4012 else
4013 {
4014 // local symbol key name
4015 // <stub-type>:<object>:<r_sym>:<addend>.
4016 const size_t len = 200;
4017 char buffer[len];
4018 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
4019 this->u_.relobj, this->r_sym_, this->addend_);
4020 gold_assert(c > 0 && c < static_cast<int>(len));
4021 return std::string(buffer);
4022 }
4023}
4024
4025// Reloc_stub methods.
4026
4027// Determine the type of stub needed, if any, for a relocation of R_TYPE at
4028// LOCATION to DESTINATION.
4029// This code is based on the arm_type_of_stub function in
4030// bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
4031// class simple.
4032
4033Stub_type
4034Reloc_stub::stub_type_for_reloc(
4035 unsigned int r_type,
4036 Arm_address location,
4037 Arm_address destination,
4038 bool target_is_thumb)
4039{
4040 Stub_type stub_type = arm_stub_none;
4041
4042 // This is a bit ugly but we want to avoid using a templated class for
4043 // big and little endianities.
4044 bool may_use_blx;
4045 bool should_force_pic_veneer;
4046 bool thumb2;
4047 bool thumb_only;
4048 if (parameters->target().is_big_endian())
4049 {
43d12afe 4050 const Target_arm<true>* big_endian_target =
b569affa 4051 Target_arm<true>::default_target();
43d12afe
DK
4052 may_use_blx = big_endian_target->may_use_blx();
4053 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
4054 thumb2 = big_endian_target->using_thumb2();
4055 thumb_only = big_endian_target->using_thumb_only();
b569affa
DK
4056 }
4057 else
4058 {
43d12afe 4059 const Target_arm<false>* little_endian_target =
b569affa 4060 Target_arm<false>::default_target();
43d12afe
DK
4061 may_use_blx = little_endian_target->may_use_blx();
4062 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
4063 thumb2 = little_endian_target->using_thumb2();
4064 thumb_only = little_endian_target->using_thumb_only();
b569affa
DK
4065 }
4066
4067 int64_t branch_offset = (int64_t)destination - location;
4068
4069 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
4070 {
4071 // Handle cases where:
4072 // - this call goes too far (different Thumb/Thumb2 max
4073 // distance)
4074 // - it's a Thumb->Arm call and blx is not available, or it's a
4075 // Thumb->Arm branch (not bl). A stub is needed in this case.
4076 if ((!thumb2
4077 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
4078 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
4079 || (thumb2
4080 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
4081 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
4082 || ((!target_is_thumb)
4083 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
4084 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
4085 {
4086 if (target_is_thumb)
4087 {
4088 // Thumb to thumb.
4089 if (!thumb_only)
4090 {
51938283
DK
4091 stub_type = (parameters->options().shared()
4092 || should_force_pic_veneer)
b569affa
DK
4093 // PIC stubs.
4094 ? ((may_use_blx
4095 && (r_type == elfcpp::R_ARM_THM_CALL))
4096 // V5T and above. Stub starts with ARM code, so
4097 // we must be able to switch mode before
4098 // reaching it, which is only possible for 'bl'
4099 // (ie R_ARM_THM_CALL relocation).
4100 ? arm_stub_long_branch_any_thumb_pic
4101 // On V4T, use Thumb code only.
4102 : arm_stub_long_branch_v4t_thumb_thumb_pic)
4103
4104 // non-PIC stubs.
4105 : ((may_use_blx
4106 && (r_type == elfcpp::R_ARM_THM_CALL))
4107 ? arm_stub_long_branch_any_any // V5T and above.
4108 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
4109 }
4110 else
4111 {
51938283
DK
4112 stub_type = (parameters->options().shared()
4113 || should_force_pic_veneer)
b569affa
DK
4114 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
4115 : arm_stub_long_branch_thumb_only; // non-PIC stub.
4116 }
4117 }
4118 else
4119 {
4120 // Thumb to arm.
4121
4122 // FIXME: We should check that the input section is from an
4123 // object that has interwork enabled.
4124
4125 stub_type = (parameters->options().shared()
4126 || should_force_pic_veneer)
4127 // PIC stubs.
4128 ? ((may_use_blx
4129 && (r_type == elfcpp::R_ARM_THM_CALL))
4130 ? arm_stub_long_branch_any_arm_pic // V5T and above.
4131 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
4132
4133 // non-PIC stubs.
4134 : ((may_use_blx
4135 && (r_type == elfcpp::R_ARM_THM_CALL))
4136 ? arm_stub_long_branch_any_any // V5T and above.
4137 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
4138
4139 // Handle v4t short branches.
4140 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
4141 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
4142 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
4143 stub_type = arm_stub_short_branch_v4t_thumb_arm;
4144 }
4145 }
4146 }
4147 else if (r_type == elfcpp::R_ARM_CALL
4148 || r_type == elfcpp::R_ARM_JUMP24
4149 || r_type == elfcpp::R_ARM_PLT32)
4150 {
4151 if (target_is_thumb)
4152 {
4153 // Arm to thumb.
4154
4155 // FIXME: We should check that the input section is from an
4156 // object that has interwork enabled.
4157
4158 // We have an extra 2-bytes reach because of
4159 // the mode change (bit 24 (H) of BLX encoding).
4160 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
4161 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
4162 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
4163 || (r_type == elfcpp::R_ARM_JUMP24)
4164 || (r_type == elfcpp::R_ARM_PLT32))
4165 {
4166 stub_type = (parameters->options().shared()
4167 || should_force_pic_veneer)
4168 // PIC stubs.
4169 ? (may_use_blx
4170 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
4171 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
4172
4173 // non-PIC stubs.
4174 : (may_use_blx
4175 ? arm_stub_long_branch_any_any // V5T and above.
4176 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
4177 }
4178 }
4179 else
4180 {
4181 // Arm to arm.
4182 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4183 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4184 {
4185 stub_type = (parameters->options().shared()
4186 || should_force_pic_veneer)
4187 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
4188 : arm_stub_long_branch_any_any; /// non-PIC.
4189 }
4190 }
4191 }
4192
4193 return stub_type;
4194}
4195
bb0d3eb0 4196// Cortex_a8_stub methods.
b569affa 4197
bb0d3eb0
DK
4198// Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
4199// I is the position of the instruction template in the stub template.
b569affa 4200
bb0d3eb0
DK
4201uint16_t
4202Cortex_a8_stub::do_thumb16_special(size_t i)
b569affa 4203{
bb0d3eb0
DK
4204 // The only use of this is to copy condition code from a conditional
4205 // branch being worked around to the corresponding conditional branch in
4206 // to the stub.
4207 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
4208 && i == 0);
4209 uint16_t data = this->stub_template()->insns()[i].data();
4210 gold_assert((data & 0xff00U) == 0xd000U);
4211 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
4212 return data;
b569affa
DK
4213}
4214
4215// Stub_factory methods.
4216
4217Stub_factory::Stub_factory()
4218{
4219 // The instruction template sequences are declared as static
4220 // objects and initialized first time the constructor runs.
4221
4222 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
4223 // to reach the stub if necessary.
4224 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
4225 {
4226 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4227 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4228 // dcd R_ARM_ABS32(X)
4229 };
4230
4231 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
4232 // available.
4233 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
4234 {
4235 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4236 Insn_template::arm_insn(0xe12fff1c), // bx ip
4237 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4238 // dcd R_ARM_ABS32(X)
4239 };
4240
4241 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
4242 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
4243 {
4244 Insn_template::thumb16_insn(0xb401), // push {r0}
4245 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4246 Insn_template::thumb16_insn(0x4684), // mov ip, r0
4247 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4248 Insn_template::thumb16_insn(0x4760), // bx ip
4249 Insn_template::thumb16_insn(0xbf00), // nop
4250 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4251 // dcd R_ARM_ABS32(X)
4252 };
4253
4254 // V4T Thumb -> Thumb long branch stub. Using the stack is not
4255 // allowed.
4256 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
4257 {
4258 Insn_template::thumb16_insn(0x4778), // bx pc
4259 Insn_template::thumb16_insn(0x46c0), // nop
4260 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4261 Insn_template::arm_insn(0xe12fff1c), // bx ip
4262 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4263 // dcd R_ARM_ABS32(X)
4264 };
4265
4266 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
4267 // available.
4268 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
4269 {
4270 Insn_template::thumb16_insn(0x4778), // bx pc
4271 Insn_template::thumb16_insn(0x46c0), // nop
4272 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4273 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4274 // dcd R_ARM_ABS32(X)
4275 };
4276
4277 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
4278 // one, when the destination is close enough.
4279 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
4280 {
4281 Insn_template::thumb16_insn(0x4778), // bx pc
4282 Insn_template::thumb16_insn(0x46c0), // nop
4283 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
4284 };
4285
4286 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
4287 // blx to reach the stub if necessary.
4288 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
4289 {
4290 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
4291 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
4292 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4293 // dcd R_ARM_REL32(X-4)
4294 };
4295
4296 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
4297 // blx to reach the stub if necessary. We can not add into pc;
4298 // it is not guaranteed to mode switch (different in ARMv6 and
4299 // ARMv7).
4300 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
4301 {
4302 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
4303 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4304 Insn_template::arm_insn(0xe12fff1c), // bx ip
4305 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4306 // dcd R_ARM_REL32(X)
4307 };
4308
4309 // V4T ARM -> ARM long branch stub, PIC.
4310 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
4311 {
4312 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4313 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4314 Insn_template::arm_insn(0xe12fff1c), // bx ip
4315 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4316 // dcd R_ARM_REL32(X)
4317 };
4318
4319 // V4T Thumb -> ARM long branch stub, PIC.
4320 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
4321 {
4322 Insn_template::thumb16_insn(0x4778), // bx pc
4323 Insn_template::thumb16_insn(0x46c0), // nop
4324 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4325 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4326 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4327 // dcd R_ARM_REL32(X)
4328 };
4329
4330 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4331 // architectures.
4332 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
4333 {
4334 Insn_template::thumb16_insn(0xb401), // push {r0}
4335 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4336 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4337 Insn_template::thumb16_insn(0x4484), // add ip, r0
4338 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4339 Insn_template::thumb16_insn(0x4760), // bx ip
4340 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
4341 // dcd R_ARM_REL32(X)
4342 };
4343
4344 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4345 // allowed.
4346 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
4347 {
4348 Insn_template::thumb16_insn(0x4778), // bx pc
4349 Insn_template::thumb16_insn(0x46c0), // nop
4350 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4351 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4352 Insn_template::arm_insn(0xe12fff1c), // bx ip
4353 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4354 // dcd R_ARM_REL32(X)
4355 };
4356
4357 // Cortex-A8 erratum-workaround stubs.
4358
4359 // Stub used for conditional branches (which may be beyond +/-1MB away,
4360 // so we can't use a conditional branch to reach this stub).
4361
4362 // original code:
4363 //
4364 // b<cond> X
4365 // after:
4366 //
4367 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
4368 {
4369 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4370 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4371 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4372 // b.w X
4373 };
4374
4375 // Stub used for b.w and bl.w instructions.
4376
4377 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
4378 {
4379 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4380 };
4381
4382 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
4383 {
4384 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4385 };
4386
4387 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4388 // instruction (which switches to ARM mode) to point to this stub. Jump to
4389 // the real destination using an ARM-mode branch.
bb0d3eb0 4390 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
b569affa
DK
4391 {
4392 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4393 };
4394
a2162063
ILT
4395 // Stub used to provide an interworking for R_ARM_V4BX relocation
4396 // (bx r[n] instruction).
4397 static const Insn_template elf32_arm_stub_v4_veneer_bx[] =
4398 {
4399 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4400 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4401 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4402 };
4403
b569affa
DK
4404 // Fill in the stub template look-up table. Stub templates are constructed
4405 // per instance of Stub_factory for fast look-up without locking
4406 // in a thread-enabled environment.
4407
4408 this->stub_templates_[arm_stub_none] =
4409 new Stub_template(arm_stub_none, NULL, 0);
4410
4411#define DEF_STUB(x) \
4412 do \
4413 { \
4414 size_t array_size \
4415 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4416 Stub_type type = arm_stub_##x; \
4417 this->stub_templates_[type] = \
4418 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4419 } \
4420 while (0);
4421
4422 DEF_STUBS
4423#undef DEF_STUB
4424}
4425
56ee5e00
DK
4426// Stub_table methods.
4427
2fb7225c 4428// Removel all Cortex-A8 stub.
56ee5e00
DK
4429
4430template<bool big_endian>
4431void
2fb7225c
DK
4432Stub_table<big_endian>::remove_all_cortex_a8_stubs()
4433{
4434 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4435 p != this->cortex_a8_stubs_.end();
4436 ++p)
4437 delete p->second;
4438 this->cortex_a8_stubs_.clear();
4439}
4440
4441// Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4442
4443template<bool big_endian>
4444void
4445Stub_table<big_endian>::relocate_stub(
4446 Stub* stub,
4447 const Relocate_info<32, big_endian>* relinfo,
4448 Target_arm<big_endian>* arm_target,
4449 Output_section* output_section,
4450 unsigned char* view,
4451 Arm_address address,
4452 section_size_type view_size)
56ee5e00 4453{
2ea97941 4454 const Stub_template* stub_template = stub->stub_template();
2fb7225c
DK
4455 if (stub_template->reloc_count() != 0)
4456 {
4457 // Adjust view to cover the stub only.
4458 section_size_type offset = stub->offset();
4459 section_size_type stub_size = stub_template->size();
4460 gold_assert(offset + stub_size <= view_size);
4461
4462 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
4463 address + offset, stub_size);
4464 }
56ee5e00
DK
4465}
4466
2fb7225c
DK
4467// Relocate all stubs in this stub table.
4468
56ee5e00
DK
4469template<bool big_endian>
4470void
4471Stub_table<big_endian>::relocate_stubs(
4472 const Relocate_info<32, big_endian>* relinfo,
4473 Target_arm<big_endian>* arm_target,
2ea97941 4474 Output_section* output_section,
56ee5e00 4475 unsigned char* view,
2ea97941 4476 Arm_address address,
56ee5e00
DK
4477 section_size_type view_size)
4478{
4479 // If we are passed a view bigger than the stub table's. we need to
4480 // adjust the view.
2ea97941 4481 gold_assert(address == this->address()
56ee5e00
DK
4482 && (view_size
4483 == static_cast<section_size_type>(this->data_size())));
4484
2fb7225c
DK
4485 // Relocate all relocation stubs.
4486 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4487 p != this->reloc_stubs_.end();
4488 ++p)
4489 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4490 address, view_size);
4491
4492 // Relocate all Cortex-A8 stubs.
4493 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4494 p != this->cortex_a8_stubs_.end();
4495 ++p)
4496 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4497 address, view_size);
a2162063
ILT
4498
4499 // Relocate all ARM V4BX stubs.
4500 for (Arm_v4bx_stub_list::iterator p = this->arm_v4bx_stubs_.begin();
4501 p != this->arm_v4bx_stubs_.end();
4502 ++p)
4503 {
4504 if (*p != NULL)
4505 this->relocate_stub(*p, relinfo, arm_target, output_section, view,
4506 address, view_size);
4507 }
2fb7225c
DK
4508}
4509
4510// Write out the stubs to file.
4511
4512template<bool big_endian>
4513void
4514Stub_table<big_endian>::do_write(Output_file* of)
4515{
4516 off_t offset = this->offset();
4517 const section_size_type oview_size =
4518 convert_to_section_size_type(this->data_size());
4519 unsigned char* const oview = of->get_output_view(offset, oview_size);
4520
4521 // Write relocation stubs.
56ee5e00
DK
4522 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4523 p != this->reloc_stubs_.end();
4524 ++p)
4525 {
4526 Reloc_stub* stub = p->second;
2fb7225c
DK
4527 Arm_address address = this->address() + stub->offset();
4528 gold_assert(address
4529 == align_address(address,
4530 stub->stub_template()->alignment()));
4531 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4532 big_endian);
56ee5e00 4533 }
2fb7225c
DK
4534
4535 // Write Cortex-A8 stubs.
4536 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4537 p != this->cortex_a8_stubs_.end();
4538 ++p)
4539 {
4540 Cortex_a8_stub* stub = p->second;
4541 Arm_address address = this->address() + stub->offset();
4542 gold_assert(address
4543 == align_address(address,
4544 stub->stub_template()->alignment()));
4545 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4546 big_endian);
4547 }
4548
a2162063
ILT
4549 // Write ARM V4BX relocation stubs.
4550 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4551 p != this->arm_v4bx_stubs_.end();
4552 ++p)
4553 {
4554 if (*p == NULL)
4555 continue;
4556
4557 Arm_address address = this->address() + (*p)->offset();
4558 gold_assert(address
4559 == align_address(address,
4560 (*p)->stub_template()->alignment()));
4561 (*p)->write(oview + (*p)->offset(), (*p)->stub_template()->size(),
4562 big_endian);
4563 }
4564
2fb7225c 4565 of->write_output_view(this->offset(), oview_size, oview);
56ee5e00
DK
4566}
4567
2fb7225c
DK
4568// Update the data size and address alignment of the stub table at the end
4569// of a relaxation pass. Return true if either the data size or the
4570// alignment changed in this relaxation pass.
4571
4572template<bool big_endian>
4573bool
4574Stub_table<big_endian>::update_data_size_and_addralign()
4575{
4576 off_t size = 0;
4577 unsigned addralign = 1;
4578
4579 // Go over all stubs in table to compute data size and address alignment.
4580
4581 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4582 p != this->reloc_stubs_.end();
4583 ++p)
4584 {
4585 const Stub_template* stub_template = p->second->stub_template();
4586 addralign = std::max(addralign, stub_template->alignment());
4587 size = (align_address(size, stub_template->alignment())
4588 + stub_template->size());
4589 }
4590
4591 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4592 p != this->cortex_a8_stubs_.end();
4593 ++p)
4594 {
4595 const Stub_template* stub_template = p->second->stub_template();
4596 addralign = std::max(addralign, stub_template->alignment());
4597 size = (align_address(size, stub_template->alignment())
4598 + stub_template->size());
4599 }
4600
a2162063
ILT
4601 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4602 p != this->arm_v4bx_stubs_.end();
4603 ++p)
4604 {
4605 if (*p == NULL)
4606 continue;
4607
4608 const Stub_template* stub_template = (*p)->stub_template();
4609 addralign = std::max(addralign, stub_template->alignment());
4610 size = (align_address(size, stub_template->alignment())
4611 + stub_template->size());
4612 }
4613
2fb7225c
DK
4614 // Check if either data size or alignment changed in this pass.
4615 // Update prev_data_size_ and prev_addralign_. These will be used
4616 // as the current data size and address alignment for the next pass.
4617 bool changed = size != this->prev_data_size_;
4618 this->prev_data_size_ = size;
4619
4620 if (addralign != this->prev_addralign_)
4621 changed = true;
4622 this->prev_addralign_ = addralign;
4623
4624 return changed;
4625}
4626
4627// Finalize the stubs. This sets the offsets of the stubs within the stub
4628// table. It also marks all input sections needing Cortex-A8 workaround.
56ee5e00
DK
4629
4630template<bool big_endian>
4631void
2fb7225c 4632Stub_table<big_endian>::finalize_stubs()
56ee5e00
DK
4633{
4634 off_t off = 0;
56ee5e00
DK
4635 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4636 p != this->reloc_stubs_.end();
4637 ++p)
4638 {
4639 Reloc_stub* stub = p->second;
2ea97941
ILT
4640 const Stub_template* stub_template = stub->stub_template();
4641 uint64_t stub_addralign = stub_template->alignment();
56ee5e00
DK
4642 off = align_address(off, stub_addralign);
4643 stub->set_offset(off);
2ea97941 4644 off += stub_template->size();
56ee5e00
DK
4645 }
4646
2fb7225c
DK
4647 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4648 p != this->cortex_a8_stubs_.end();
4649 ++p)
4650 {
4651 Cortex_a8_stub* stub = p->second;
4652 const Stub_template* stub_template = stub->stub_template();
4653 uint64_t stub_addralign = stub_template->alignment();
4654 off = align_address(off, stub_addralign);
4655 stub->set_offset(off);
4656 off += stub_template->size();
4657
4658 // Mark input section so that we can determine later if a code section
4659 // needs the Cortex-A8 workaround quickly.
4660 Arm_relobj<big_endian>* arm_relobj =
4661 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
4662 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
4663 }
4664
a2162063
ILT
4665 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4666 p != this->arm_v4bx_stubs_.end();
4667 ++p)
4668 {
4669 if (*p == NULL)
4670 continue;
4671
4672 const Stub_template* stub_template = (*p)->stub_template();
4673 uint64_t stub_addralign = stub_template->alignment();
4674 off = align_address(off, stub_addralign);
4675 (*p)->set_offset(off);
4676 off += stub_template->size();
4677 }
4678
2fb7225c 4679 gold_assert(off <= this->prev_data_size_);
56ee5e00
DK
4680}
4681
2fb7225c
DK
4682// Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
4683// and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
4684// of the address range seen by the linker.
56ee5e00
DK
4685
4686template<bool big_endian>
4687void
2fb7225c
DK
4688Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
4689 Target_arm<big_endian>* arm_target,
4690 unsigned char* view,
4691 Arm_address view_address,
4692 section_size_type view_size)
56ee5e00 4693{
2fb7225c
DK
4694 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
4695 for (Cortex_a8_stub_list::const_iterator p =
4696 this->cortex_a8_stubs_.lower_bound(view_address);
4697 ((p != this->cortex_a8_stubs_.end())
4698 && (p->first < (view_address + view_size)));
4699 ++p)
56ee5e00 4700 {
2fb7225c
DK
4701 // We do not store the THUMB bit in the LSB of either the branch address
4702 // or the stub offset. There is no need to strip the LSB.
4703 Arm_address branch_address = p->first;
4704 const Cortex_a8_stub* stub = p->second;
4705 Arm_address stub_address = this->address() + stub->offset();
4706
4707 // Offset of the branch instruction relative to this view.
4708 section_size_type offset =
4709 convert_to_section_size_type(branch_address - view_address);
4710 gold_assert((offset + 4) <= view_size);
4711
4712 arm_target->apply_cortex_a8_workaround(stub, stub_address,
4713 view + offset, branch_address);
4714 }
56ee5e00
DK
4715}
4716
10ad9fe5
DK
4717// Arm_input_section methods.
4718
4719// Initialize an Arm_input_section.
4720
4721template<bool big_endian>
4722void
4723Arm_input_section<big_endian>::init()
4724{
2ea97941
ILT
4725 Relobj* relobj = this->relobj();
4726 unsigned int shndx = this->shndx();
10ad9fe5
DK
4727
4728 // Cache these to speed up size and alignment queries. It is too slow
4729 // to call section_addraglin and section_size every time.
2ea97941
ILT
4730 this->original_addralign_ = relobj->section_addralign(shndx);
4731 this->original_size_ = relobj->section_size(shndx);
10ad9fe5
DK
4732
4733 // We want to make this look like the original input section after
4734 // output sections are finalized.
2ea97941
ILT
4735 Output_section* os = relobj->output_section(shndx);
4736 off_t offset = relobj->output_section_offset(shndx);
4737 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
4738 this->set_address(os->address() + offset);
4739 this->set_file_offset(os->offset() + offset);
10ad9fe5
DK
4740
4741 this->set_current_data_size(this->original_size_);
4742 this->finalize_data_size();
4743}
4744
4745template<bool big_endian>
4746void
4747Arm_input_section<big_endian>::do_write(Output_file* of)
4748{
4749 // We have to write out the original section content.
4750 section_size_type section_size;
4751 const unsigned char* section_contents =
4752 this->relobj()->section_contents(this->shndx(), &section_size, false);
4753 of->write(this->offset(), section_contents, section_size);
4754
4755 // If this owns a stub table and it is not empty, write it.
4756 if (this->is_stub_table_owner() && !this->stub_table_->empty())
4757 this->stub_table_->write(of);
4758}
4759
4760// Finalize data size.
4761
4762template<bool big_endian>
4763void
4764Arm_input_section<big_endian>::set_final_data_size()
4765{
4766 // If this owns a stub table, finalize its data size as well.
4767 if (this->is_stub_table_owner())
4768 {
2ea97941 4769 uint64_t address = this->address();
10ad9fe5
DK
4770
4771 // The stub table comes after the original section contents.
2ea97941
ILT
4772 address += this->original_size_;
4773 address = align_address(address, this->stub_table_->addralign());
4774 off_t offset = this->offset() + (address - this->address());
4775 this->stub_table_->set_address_and_file_offset(address, offset);
4776 address += this->stub_table_->data_size();
4777 gold_assert(address == this->address() + this->current_data_size());
10ad9fe5
DK
4778 }
4779
4780 this->set_data_size(this->current_data_size());
4781}
4782
4783// Reset address and file offset.
4784
4785template<bool big_endian>
4786void
4787Arm_input_section<big_endian>::do_reset_address_and_file_offset()
4788{
4789 // Size of the original input section contents.
4790 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
4791
4792 // If this is a stub table owner, account for the stub table size.
4793 if (this->is_stub_table_owner())
4794 {
2ea97941 4795 Stub_table<big_endian>* stub_table = this->stub_table_;
10ad9fe5
DK
4796
4797 // Reset the stub table's address and file offset. The
4798 // current data size for child will be updated after that.
4799 stub_table_->reset_address_and_file_offset();
4800 off = align_address(off, stub_table_->addralign());
2ea97941 4801 off += stub_table->current_data_size();
10ad9fe5
DK
4802 }
4803
4804 this->set_current_data_size(off);
4805}
4806
af2cdeae
DK
4807// Arm_exidx_cantunwind methods.
4808
4809// Write this to Output file OF for a fixed endianity.
4810
4811template<bool big_endian>
4812void
4813Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
4814{
4815 off_t offset = this->offset();
4816 const section_size_type oview_size = 8;
4817 unsigned char* const oview = of->get_output_view(offset, oview_size);
4818
4819 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4820 Valtype* wv = reinterpret_cast<Valtype*>(oview);
4821
4822 Output_section* os = this->relobj_->output_section(this->shndx_);
4823 gold_assert(os != NULL);
4824
4825 Arm_relobj<big_endian>* arm_relobj =
4826 Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
4827 Arm_address output_offset =
4828 arm_relobj->get_output_section_offset(this->shndx_);
4829 Arm_address section_start;
4830 if(output_offset != Arm_relobj<big_endian>::invalid_address)
4831 section_start = os->address() + output_offset;
4832 else
4833 {
4834 // Currently this only happens for a relaxed section.
4835 const Output_relaxed_input_section* poris =
4836 os->find_relaxed_input_section(this->relobj_, this->shndx_);
4837 gold_assert(poris != NULL);
4838 section_start = poris->address();
4839 }
4840
4841 // We always append this to the end of an EXIDX section.
4842 Arm_address output_address =
4843 section_start + this->relobj_->section_size(this->shndx_);
4844
4845 // Write out the entry. The first word either points to the beginning
4846 // or after the end of a text section. The second word is the special
4847 // EXIDX_CANTUNWIND value.
e7eca48c
DK
4848 uint32_t prel31_offset = output_address - this->address();
4849 if (utils::has_overflow<31>(offset))
4850 gold_error(_("PREL31 overflow in EXIDX_CANTUNWIND entry"));
4851 elfcpp::Swap<32, big_endian>::writeval(wv, prel31_offset & 0x7fffffffU);
af2cdeae
DK
4852 elfcpp::Swap<32, big_endian>::writeval(wv + 1, elfcpp::EXIDX_CANTUNWIND);
4853
4854 of->write_output_view(this->offset(), oview_size, oview);
4855}
4856
4857// Arm_exidx_merged_section methods.
4858
4859// Constructor for Arm_exidx_merged_section.
4860// EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
4861// SECTION_OFFSET_MAP points to a section offset map describing how
4862// parts of the input section are mapped to output. DELETED_BYTES is
4863// the number of bytes deleted from the EXIDX input section.
4864
4865Arm_exidx_merged_section::Arm_exidx_merged_section(
4866 const Arm_exidx_input_section& exidx_input_section,
4867 const Arm_exidx_section_offset_map& section_offset_map,
4868 uint32_t deleted_bytes)
4869 : Output_relaxed_input_section(exidx_input_section.relobj(),
4870 exidx_input_section.shndx(),
4871 exidx_input_section.addralign()),
4872 exidx_input_section_(exidx_input_section),
4873 section_offset_map_(section_offset_map)
4874{
4875 // Fix size here so that we do not need to implement set_final_data_size.
4876 this->set_data_size(exidx_input_section.size() - deleted_bytes);
4877 this->fix_data_size();
4878}
4879
4880// Given an input OBJECT, an input section index SHNDX within that
4881// object, and an OFFSET relative to the start of that input
4882// section, return whether or not the corresponding offset within
4883// the output section is known. If this function returns true, it
4884// sets *POUTPUT to the output offset. The value -1 indicates that
4885// this input offset is being discarded.
4886
4887bool
4888Arm_exidx_merged_section::do_output_offset(
4889 const Relobj* relobj,
4890 unsigned int shndx,
4891 section_offset_type offset,
4892 section_offset_type* poutput) const
4893{
4894 // We only handle offsets for the original EXIDX input section.
4895 if (relobj != this->exidx_input_section_.relobj()
4896 || shndx != this->exidx_input_section_.shndx())
4897 return false;
4898
c7f3c371
DK
4899 section_offset_type section_size =
4900 convert_types<section_offset_type>(this->exidx_input_section_.size());
4901 if (offset < 0 || offset >= section_size)
af2cdeae
DK
4902 // Input offset is out of valid range.
4903 *poutput = -1;
4904 else
4905 {
4906 // We need to look up the section offset map to determine the output
4907 // offset. Find the reference point in map that is first offset
4908 // bigger than or equal to this offset.
4909 Arm_exidx_section_offset_map::const_iterator p =
4910 this->section_offset_map_.lower_bound(offset);
4911
4912 // The section offset maps are build such that this should not happen if
4913 // input offset is in the valid range.
4914 gold_assert(p != this->section_offset_map_.end());
4915
4916 // We need to check if this is dropped.
4917 section_offset_type ref = p->first;
4918 section_offset_type mapped_ref = p->second;
4919
4920 if (mapped_ref != Arm_exidx_input_section::invalid_offset)
4921 // Offset is present in output.
4922 *poutput = mapped_ref + (offset - ref);
4923 else
4924 // Offset is discarded owing to EXIDX entry merging.
4925 *poutput = -1;
4926 }
4927
4928 return true;
4929}
4930
4931// Write this to output file OF.
4932
4933void
4934Arm_exidx_merged_section::do_write(Output_file* of)
4935{
4936 // If we retain or discard the whole EXIDX input section, we would
4937 // not be here.
4938 gold_assert(this->data_size() != this->exidx_input_section_.size()
4939 && this->data_size() != 0);
4940
4941 off_t offset = this->offset();
4942 const section_size_type oview_size = this->data_size();
4943 unsigned char* const oview = of->get_output_view(offset, oview_size);
4944
4945 Output_section* os = this->relobj()->output_section(this->shndx());
4946 gold_assert(os != NULL);
4947
4948 // Get contents of EXIDX input section.
4949 section_size_type section_size;
4950 const unsigned char* section_contents =
4951 this->relobj()->section_contents(this->shndx(), &section_size, false);
4952 gold_assert(section_size == this->exidx_input_section_.size());
4953
4954 // Go over spans of input offsets and write only those that are not
4955 // discarded.
4956 section_offset_type in_start = 0;
4957 section_offset_type out_start = 0;
4958 for(Arm_exidx_section_offset_map::const_iterator p =
4959 this->section_offset_map_.begin();
4960 p != this->section_offset_map_.end();
4961 ++p)
4962 {
4963 section_offset_type in_end = p->first;
4964 gold_assert(in_end >= in_start);
4965 section_offset_type out_end = p->second;
4966 size_t in_chunk_size = convert_types<size_t>(in_end - in_start + 1);
4967 if (out_end != -1)
4968 {
4969 size_t out_chunk_size =
4970 convert_types<size_t>(out_end - out_start + 1);
4971 gold_assert(out_chunk_size == in_chunk_size);
4972 memcpy(oview + out_start, section_contents + in_start,
4973 out_chunk_size);
4974 out_start += out_chunk_size;
4975 }
4976 in_start += in_chunk_size;
4977 }
4978
4979 gold_assert(convert_to_section_size_type(out_start) == oview_size);
4980 of->write_output_view(this->offset(), oview_size, oview);
4981}
4982
80d0d023
DK
4983// Arm_exidx_fixup methods.
4984
4985// Append an EXIDX_CANTUNWIND in the current output section if the last entry
4986// is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
4987// points to the end of the last seen EXIDX section.
4988
4989void
4990Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
4991{
4992 if (this->last_unwind_type_ != UT_EXIDX_CANTUNWIND
4993 && this->last_input_section_ != NULL)
4994 {
4995 Relobj* relobj = this->last_input_section_->relobj();
2b328d4e 4996 unsigned int text_shndx = this->last_input_section_->link();
80d0d023 4997 Arm_exidx_cantunwind* cantunwind =
2b328d4e 4998 new Arm_exidx_cantunwind(relobj, text_shndx);
80d0d023
DK
4999 this->exidx_output_section_->add_output_section_data(cantunwind);
5000 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5001 }
5002}
5003
5004// Process an EXIDX section entry in input. Return whether this entry
5005// can be deleted in the output. SECOND_WORD in the second word of the
5006// EXIDX entry.
5007
5008bool
5009Arm_exidx_fixup::process_exidx_entry(uint32_t second_word)
5010{
5011 bool delete_entry;
5012 if (second_word == elfcpp::EXIDX_CANTUNWIND)
5013 {
5014 // Merge if previous entry is also an EXIDX_CANTUNWIND.
5015 delete_entry = this->last_unwind_type_ == UT_EXIDX_CANTUNWIND;
5016 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5017 }
5018 else if ((second_word & 0x80000000) != 0)
5019 {
5020 // Inlined unwinding data. Merge if equal to previous.
5021 delete_entry = (this->last_unwind_type_ == UT_INLINED_ENTRY
5022 && this->last_inlined_entry_ == second_word);
5023 this->last_unwind_type_ = UT_INLINED_ENTRY;
5024 this->last_inlined_entry_ = second_word;
5025 }
5026 else
5027 {
5028 // Normal table entry. In theory we could merge these too,
5029 // but duplicate entries are likely to be much less common.
5030 delete_entry = false;
5031 this->last_unwind_type_ = UT_NORMAL_ENTRY;
5032 }
5033 return delete_entry;
5034}
5035
5036// Update the current section offset map during EXIDX section fix-up.
5037// If there is no map, create one. INPUT_OFFSET is the offset of a
5038// reference point, DELETED_BYTES is the number of deleted by in the
5039// section so far. If DELETE_ENTRY is true, the reference point and
5040// all offsets after the previous reference point are discarded.
5041
5042void
5043Arm_exidx_fixup::update_offset_map(
5044 section_offset_type input_offset,
5045 section_size_type deleted_bytes,
5046 bool delete_entry)
5047{
5048 if (this->section_offset_map_ == NULL)
5049 this->section_offset_map_ = new Arm_exidx_section_offset_map();
5050 section_offset_type output_offset = (delete_entry
5051 ? -1
5052 : input_offset - deleted_bytes);
5053 (*this->section_offset_map_)[input_offset] = output_offset;
5054}
5055
5056// Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
5057// bytes deleted. If some entries are merged, also store a pointer to a newly
5058// created Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The
5059// caller owns the map and is responsible for releasing it after use.
5060
5061template<bool big_endian>
5062uint32_t
5063Arm_exidx_fixup::process_exidx_section(
5064 const Arm_exidx_input_section* exidx_input_section,
5065 Arm_exidx_section_offset_map** psection_offset_map)
5066{
5067 Relobj* relobj = exidx_input_section->relobj();
5068 unsigned shndx = exidx_input_section->shndx();
5069 section_size_type section_size;
5070 const unsigned char* section_contents =
5071 relobj->section_contents(shndx, &section_size, false);
5072
5073 if ((section_size % 8) != 0)
5074 {
5075 // Something is wrong with this section. Better not touch it.
5076 gold_error(_("uneven .ARM.exidx section size in %s section %u"),
5077 relobj->name().c_str(), shndx);
5078 this->last_input_section_ = exidx_input_section;
5079 this->last_unwind_type_ = UT_NONE;
5080 return 0;
5081 }
5082
5083 uint32_t deleted_bytes = 0;
5084 bool prev_delete_entry = false;
5085 gold_assert(this->section_offset_map_ == NULL);
5086
5087 for (section_size_type i = 0; i < section_size; i += 8)
5088 {
5089 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5090 const Valtype* wv =
5091 reinterpret_cast<const Valtype*>(section_contents + i + 4);
5092 uint32_t second_word = elfcpp::Swap<32, big_endian>::readval(wv);
5093
5094 bool delete_entry = this->process_exidx_entry(second_word);
5095
5096 // Entry deletion causes changes in output offsets. We use a std::map
5097 // to record these. And entry (x, y) means input offset x
5098 // is mapped to output offset y. If y is invalid_offset, then x is
5099 // dropped in the output. Because of the way std::map::lower_bound
5100 // works, we record the last offset in a region w.r.t to keeping or
5101 // dropping. If there is no entry (x0, y0) for an input offset x0,
5102 // the output offset y0 of it is determined by the output offset y1 of
5103 // the smallest input offset x1 > x0 that there is an (x1, y1) entry
5104 // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Othewise, y1
5105 // y0 is also -1.
5106 if (delete_entry != prev_delete_entry && i != 0)
5107 this->update_offset_map(i - 1, deleted_bytes, prev_delete_entry);
5108
5109 // Update total deleted bytes for this entry.
5110 if (delete_entry)
5111 deleted_bytes += 8;
5112
5113 prev_delete_entry = delete_entry;
5114 }
5115
5116 // If section offset map is not NULL, make an entry for the end of
5117 // section.
5118 if (this->section_offset_map_ != NULL)
5119 update_offset_map(section_size - 1, deleted_bytes, prev_delete_entry);
5120
5121 *psection_offset_map = this->section_offset_map_;
5122 this->section_offset_map_ = NULL;
5123 this->last_input_section_ = exidx_input_section;
5124
546c7457
DK
5125 // Set the first output text section so that we can link the EXIDX output
5126 // section to it. Ignore any EXIDX input section that is completely merged.
5127 if (this->first_output_text_section_ == NULL
5128 && deleted_bytes != section_size)
5129 {
5130 unsigned int link = exidx_input_section->link();
5131 Output_section* os = relobj->output_section(link);
5132 gold_assert(os != NULL);
5133 this->first_output_text_section_ = os;
5134 }
5135
80d0d023
DK
5136 return deleted_bytes;
5137}
5138
07f508a2
DK
5139// Arm_output_section methods.
5140
5141// Create a stub group for input sections from BEGIN to END. OWNER
5142// points to the input section to be the owner a new stub table.
5143
5144template<bool big_endian>
5145void
5146Arm_output_section<big_endian>::create_stub_group(
5147 Input_section_list::const_iterator begin,
5148 Input_section_list::const_iterator end,
5149 Input_section_list::const_iterator owner,
5150 Target_arm<big_endian>* target,
5151 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
5152{
2b328d4e
DK
5153 // We use a different kind of relaxed section in an EXIDX section.
5154 // The static casting from Output_relaxed_input_section to
5155 // Arm_input_section is invalid in an EXIDX section. We are okay
5156 // because we should not be calling this for an EXIDX section.
5157 gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX);
5158
07f508a2
DK
5159 // Currently we convert ordinary input sections into relaxed sections only
5160 // at this point but we may want to support creating relaxed input section
5161 // very early. So we check here to see if owner is already a relaxed
5162 // section.
5163
5164 Arm_input_section<big_endian>* arm_input_section;
5165 if (owner->is_relaxed_input_section())
5166 {
5167 arm_input_section =
5168 Arm_input_section<big_endian>::as_arm_input_section(
5169 owner->relaxed_input_section());
5170 }
5171 else
5172 {
5173 gold_assert(owner->is_input_section());
5174 // Create a new relaxed input section.
5175 arm_input_section =
5176 target->new_arm_input_section(owner->relobj(), owner->shndx());
5177 new_relaxed_sections->push_back(arm_input_section);
5178 }
5179
5180 // Create a stub table.
2ea97941 5181 Stub_table<big_endian>* stub_table =
07f508a2
DK
5182 target->new_stub_table(arm_input_section);
5183
2ea97941 5184 arm_input_section->set_stub_table(stub_table);
07f508a2
DK
5185
5186 Input_section_list::const_iterator p = begin;
5187 Input_section_list::const_iterator prev_p;
5188
5189 // Look for input sections or relaxed input sections in [begin ... end].
5190 do
5191 {
5192 if (p->is_input_section() || p->is_relaxed_input_section())
5193 {
5194 // The stub table information for input sections live
5195 // in their objects.
5196 Arm_relobj<big_endian>* arm_relobj =
5197 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
2ea97941 5198 arm_relobj->set_stub_table(p->shndx(), stub_table);
07f508a2
DK
5199 }
5200 prev_p = p++;
5201 }
5202 while (prev_p != end);
5203}
5204
5205// Group input sections for stub generation. GROUP_SIZE is roughly the limit
5206// of stub groups. We grow a stub group by adding input section until the
5207// size is just below GROUP_SIZE. The last input section will be converted
5208// into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
5209// input section after the stub table, effectively double the group size.
5210//
5211// This is similar to the group_sections() function in elf32-arm.c but is
5212// implemented differently.
5213
5214template<bool big_endian>
5215void
5216Arm_output_section<big_endian>::group_sections(
5217 section_size_type group_size,
5218 bool stubs_always_after_branch,
5219 Target_arm<big_endian>* target)
5220{
5221 // We only care about sections containing code.
5222 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5223 return;
5224
5225 // States for grouping.
5226 typedef enum
5227 {
5228 // No group is being built.
5229 NO_GROUP,
5230 // A group is being built but the stub table is not found yet.
5231 // We keep group a stub group until the size is just under GROUP_SIZE.
5232 // The last input section in the group will be used as the stub table.
5233 FINDING_STUB_SECTION,
5234 // A group is being built and we have already found a stub table.
5235 // We enter this state to grow a stub group by adding input section
5236 // after the stub table. This effectively doubles the group size.
5237 HAS_STUB_SECTION
5238 } State;
5239
5240 // Any newly created relaxed sections are stored here.
5241 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
5242
5243 State state = NO_GROUP;
5244 section_size_type off = 0;
5245 section_size_type group_begin_offset = 0;
5246 section_size_type group_end_offset = 0;
5247 section_size_type stub_table_end_offset = 0;
5248 Input_section_list::const_iterator group_begin =
5249 this->input_sections().end();
2ea97941 5250 Input_section_list::const_iterator stub_table =
07f508a2
DK
5251 this->input_sections().end();
5252 Input_section_list::const_iterator group_end = this->input_sections().end();
5253 for (Input_section_list::const_iterator p = this->input_sections().begin();
5254 p != this->input_sections().end();
5255 ++p)
5256 {
5257 section_size_type section_begin_offset =
5258 align_address(off, p->addralign());
5259 section_size_type section_end_offset =
5260 section_begin_offset + p->data_size();
5261
5262 // Check to see if we should group the previously seens sections.
e9bbb538 5263 switch (state)
07f508a2
DK
5264 {
5265 case NO_GROUP:
5266 break;
5267
5268 case FINDING_STUB_SECTION:
5269 // Adding this section makes the group larger than GROUP_SIZE.
5270 if (section_end_offset - group_begin_offset >= group_size)
5271 {
5272 if (stubs_always_after_branch)
5273 {
5274 gold_assert(group_end != this->input_sections().end());
5275 this->create_stub_group(group_begin, group_end, group_end,
5276 target, &new_relaxed_sections);
5277 state = NO_GROUP;
5278 }
5279 else
5280 {
5281 // But wait, there's more! Input sections up to
5282 // stub_group_size bytes after the stub table can be
5283 // handled by it too.
5284 state = HAS_STUB_SECTION;
2ea97941 5285 stub_table = group_end;
07f508a2
DK
5286 stub_table_end_offset = group_end_offset;
5287 }
5288 }
5289 break;
5290
5291 case HAS_STUB_SECTION:
5292 // Adding this section makes the post stub-section group larger
5293 // than GROUP_SIZE.
5294 if (section_end_offset - stub_table_end_offset >= group_size)
5295 {
5296 gold_assert(group_end != this->input_sections().end());
2ea97941 5297 this->create_stub_group(group_begin, group_end, stub_table,
07f508a2
DK
5298 target, &new_relaxed_sections);
5299 state = NO_GROUP;
5300 }
5301 break;
5302
5303 default:
5304 gold_unreachable();
5305 }
5306
5307 // If we see an input section and currently there is no group, start
5308 // a new one. Skip any empty sections.
5309 if ((p->is_input_section() || p->is_relaxed_input_section())
5310 && (p->relobj()->section_size(p->shndx()) != 0))
5311 {
5312 if (state == NO_GROUP)
5313 {
5314 state = FINDING_STUB_SECTION;
5315 group_begin = p;
5316 group_begin_offset = section_begin_offset;
5317 }
5318
5319 // Keep track of the last input section seen.
5320 group_end = p;
5321 group_end_offset = section_end_offset;
5322 }
5323
5324 off = section_end_offset;
5325 }
5326
5327 // Create a stub group for any ungrouped sections.
5328 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
5329 {
5330 gold_assert(group_end != this->input_sections().end());
5331 this->create_stub_group(group_begin, group_end,
5332 (state == FINDING_STUB_SECTION
5333 ? group_end
2ea97941 5334 : stub_table),
07f508a2
DK
5335 target, &new_relaxed_sections);
5336 }
5337
5338 // Convert input section into relaxed input section in a batch.
5339 if (!new_relaxed_sections.empty())
5340 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
5341
5342 // Update the section offsets
5343 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
5344 {
5345 Arm_relobj<big_endian>* arm_relobj =
5346 Arm_relobj<big_endian>::as_arm_relobj(
5347 new_relaxed_sections[i]->relobj());
2ea97941 5348 unsigned int shndx = new_relaxed_sections[i]->shndx();
07f508a2 5349 // Tell Arm_relobj that this input section is converted.
2ea97941 5350 arm_relobj->convert_input_section_to_relaxed_section(shndx);
07f508a2
DK
5351 }
5352}
5353
2b328d4e
DK
5354// Append non empty text sections in this to LIST in ascending
5355// order of their position in this.
5356
5357template<bool big_endian>
5358void
5359Arm_output_section<big_endian>::append_text_sections_to_list(
5360 Text_section_list* list)
5361{
5362 // We only care about text sections.
5363 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5364 return;
5365
5366 gold_assert((this->flags() & elfcpp::SHF_ALLOC) != 0);
5367
5368 for (Input_section_list::const_iterator p = this->input_sections().begin();
5369 p != this->input_sections().end();
5370 ++p)
5371 {
5372 // We only care about plain or relaxed input sections. We also
5373 // ignore any merged sections.
5374 if ((p->is_input_section() || p->is_relaxed_input_section())
5375 && p->data_size() != 0)
5376 list->push_back(Text_section_list::value_type(p->relobj(),
5377 p->shndx()));
5378 }
5379}
5380
5381template<bool big_endian>
5382void
5383Arm_output_section<big_endian>::fix_exidx_coverage(
5384 const Text_section_list& sorted_text_sections,
5385 Symbol_table* symtab)
5386{
5387 // We should only do this for the EXIDX output section.
5388 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
5389
5390 // We don't want the relaxation loop to undo these changes, so we discard
5391 // the current saved states and take another one after the fix-up.
5392 this->discard_states();
5393
5394 // Remove all input sections.
5395 uint64_t address = this->address();
5396 typedef std::list<Simple_input_section> Simple_input_section_list;
5397 Simple_input_section_list input_sections;
5398 this->reset_address_and_file_offset();
5399 this->get_input_sections(address, std::string(""), &input_sections);
5400
5401 if (!this->input_sections().empty())
5402 gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
5403
5404 // Go through all the known input sections and record them.
5405 typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
5406 Section_id_set known_input_sections;
5407 for (Simple_input_section_list::const_iterator p = input_sections.begin();
5408 p != input_sections.end();
5409 ++p)
5410 {
5411 // This should never happen. At this point, we should only see
5412 // plain EXIDX input sections.
5413 gold_assert(!p->is_relaxed_input_section());
5414 known_input_sections.insert(Section_id(p->relobj(), p->shndx()));
5415 }
5416
5417 Arm_exidx_fixup exidx_fixup(this);
5418
5419 // Go over the sorted text sections.
5420 Section_id_set processed_input_sections;
5421 for (Text_section_list::const_iterator p = sorted_text_sections.begin();
5422 p != sorted_text_sections.end();
5423 ++p)
5424 {
5425 Relobj* relobj = p->first;
5426 unsigned int shndx = p->second;
5427
5428 Arm_relobj<big_endian>* arm_relobj =
5429 Arm_relobj<big_endian>::as_arm_relobj(relobj);
5430 const Arm_exidx_input_section* exidx_input_section =
5431 arm_relobj->exidx_input_section_by_link(shndx);
5432
5433 // If this text section has no EXIDX section, force an EXIDX_CANTUNWIND
5434 // entry pointing to the end of the last seen EXIDX section.
5435 if (exidx_input_section == NULL)
5436 {
5437 exidx_fixup.add_exidx_cantunwind_as_needed();
5438 continue;
5439 }
5440
5441 Relobj* exidx_relobj = exidx_input_section->relobj();
5442 unsigned int exidx_shndx = exidx_input_section->shndx();
5443 Section_id sid(exidx_relobj, exidx_shndx);
5444 if (known_input_sections.find(sid) == known_input_sections.end())
5445 {
5446 // This is odd. We have not seen this EXIDX input section before.
5447 // We cannot do fix-up.
5448 gold_error(_("EXIDX section %u of %s is not in EXIDX output section"),
5449 exidx_shndx, exidx_relobj->name().c_str());
5450 exidx_fixup.add_exidx_cantunwind_as_needed();
5451 continue;
5452 }
5453
5454 // Fix up coverage and append input section to output data list.
5455 Arm_exidx_section_offset_map* section_offset_map = NULL;
5456 uint32_t deleted_bytes =
5457 exidx_fixup.process_exidx_section<big_endian>(exidx_input_section,
5458 &section_offset_map);
5459
5460 if (deleted_bytes == exidx_input_section->size())
5461 {
5462 // The whole EXIDX section got merged. Remove it from output.
5463 gold_assert(section_offset_map == NULL);
5464 exidx_relobj->set_output_section(exidx_shndx, NULL);
e7eca48c
DK
5465
5466 // All local symbols defined in this input section will be dropped.
5467 // We need to adjust output local symbol count.
5468 arm_relobj->set_output_local_symbol_count_needs_update();
2b328d4e
DK
5469 }
5470 else if (deleted_bytes > 0)
5471 {
5472 // Some entries are merged. We need to convert this EXIDX input
5473 // section into a relaxed section.
5474 gold_assert(section_offset_map != NULL);
5475 Arm_exidx_merged_section* merged_section =
5476 new Arm_exidx_merged_section(*exidx_input_section,
5477 *section_offset_map, deleted_bytes);
5478 this->add_relaxed_input_section(merged_section);
5479 arm_relobj->convert_input_section_to_relaxed_section(exidx_shndx);
e7eca48c
DK
5480
5481 // All local symbols defined in discarded portions of this input
5482 // section will be dropped. We need to adjust output local symbol
5483 // count.
5484 arm_relobj->set_output_local_symbol_count_needs_update();
2b328d4e
DK
5485 }
5486 else
5487 {
5488 // Just add back the EXIDX input section.
5489 gold_assert(section_offset_map == NULL);
5490 Output_section::Simple_input_section sis(exidx_relobj, exidx_shndx);
5491 this->add_simple_input_section(sis, exidx_input_section->size(),
5492 exidx_input_section->addralign());
5493 }
5494
5495 processed_input_sections.insert(Section_id(exidx_relobj, exidx_shndx));
5496 }
5497
5498 // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
5499 exidx_fixup.add_exidx_cantunwind_as_needed();
5500
5501 // Remove any known EXIDX input sections that are not processed.
5502 for (Simple_input_section_list::const_iterator p = input_sections.begin();
5503 p != input_sections.end();
5504 ++p)
5505 {
5506 if (processed_input_sections.find(Section_id(p->relobj(), p->shndx()))
5507 == processed_input_sections.end())
5508 {
5509 // We only discard a known EXIDX section because its linked
5510 // text section has been folded by ICF.
5511 Arm_relobj<big_endian>* arm_relobj =
5512 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5513 const Arm_exidx_input_section* exidx_input_section =
5514 arm_relobj->exidx_input_section_by_shndx(p->shndx());
5515 gold_assert(exidx_input_section != NULL);
5516 unsigned int text_shndx = exidx_input_section->link();
5517 gold_assert(symtab->is_section_folded(p->relobj(), text_shndx));
5518
5519 // Remove this from link.
5520 p->relobj()->set_output_section(p->shndx(), NULL);
5521 }
5522 }
5523
546c7457
DK
5524 // Link exidx output section to the first seen output section and
5525 // set correct entry size.
5526 this->set_link_section(exidx_fixup.first_output_text_section());
5527 this->set_entsize(8);
5528
2b328d4e
DK
5529 // Make changes permanent.
5530 this->save_states();
5531 this->set_section_offsets_need_adjustment();
5532}
5533
8ffa3667
DK
5534// Arm_relobj methods.
5535
cf846138
DK
5536// Determine if an input section is scannable for stub processing. SHDR is
5537// the header of the section and SHNDX is the section index. OS is the output
5538// section for the input section and SYMTAB is the global symbol table used to
5539// look up ICF information.
5540
5541template<bool big_endian>
5542bool
5543Arm_relobj<big_endian>::section_is_scannable(
5544 const elfcpp::Shdr<32, big_endian>& shdr,
5545 unsigned int shndx,
5546 const Output_section* os,
5547 const Symbol_table *symtab)
5548{
5549 // Skip any empty sections, unallocated sections or sections whose
5550 // type are not SHT_PROGBITS.
5551 if (shdr.get_sh_size() == 0
5552 || (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
5553 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
5554 return false;
5555
5556 // Skip any discarded or ICF'ed sections.
5557 if (os == NULL || symtab->is_section_folded(this, shndx))
5558 return false;
5559
5560 // If this requires special offset handling, check to see if it is
5561 // a relaxed section. If this is not, then it is a merged section that
5562 // we cannot handle.
5563 if (this->is_output_section_offset_invalid(shndx))
5564 {
5565 const Output_relaxed_input_section* poris =
5566 os->find_relaxed_input_section(this, shndx);
5567 if (poris == NULL)
5568 return false;
5569 }
5570
5571 return true;
5572}
5573
44272192
DK
5574// Determine if we want to scan the SHNDX-th section for relocation stubs.
5575// This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5576
5577template<bool big_endian>
5578bool
5579Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
5580 const elfcpp::Shdr<32, big_endian>& shdr,
5581 const Relobj::Output_sections& out_sections,
2b328d4e
DK
5582 const Symbol_table *symtab,
5583 const unsigned char* pshdrs)
44272192
DK
5584{
5585 unsigned int sh_type = shdr.get_sh_type();
5586 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
5587 return false;
5588
5589 // Ignore empty section.
5590 off_t sh_size = shdr.get_sh_size();
5591 if (sh_size == 0)
5592 return false;
5593
44272192
DK
5594 // Ignore reloc section with unexpected symbol table. The
5595 // error will be reported in the final link.
5596 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
5597 return false;
5598
b521dfe4
DK
5599 unsigned int reloc_size;
5600 if (sh_type == elfcpp::SHT_REL)
5601 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5602 else
5603 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
44272192
DK
5604
5605 // Ignore reloc section with unexpected entsize or uneven size.
5606 // The error will be reported in the final link.
5607 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
5608 return false;
5609
cf846138
DK
5610 // Ignore reloc section with bad info. This error will be
5611 // reported in the final link.
5612 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5613 if (index >= this->shnum())
5614 return false;
5615
5616 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5617 const elfcpp::Shdr<32, big_endian> text_shdr(pshdrs + index * shdr_size);
5618 return this->section_is_scannable(text_shdr, index,
5619 out_sections[index], symtab);
44272192
DK
5620}
5621
cb1be87e
DK
5622// Return the output address of either a plain input section or a relaxed
5623// input section. SHNDX is the section index. We define and use this
5624// instead of calling Output_section::output_address because that is slow
5625// for large output.
5626
5627template<bool big_endian>
5628Arm_address
5629Arm_relobj<big_endian>::simple_input_section_output_address(
5630 unsigned int shndx,
5631 Output_section* os)
5632{
5633 if (this->is_output_section_offset_invalid(shndx))
5634 {
5635 const Output_relaxed_input_section* poris =
5636 os->find_relaxed_input_section(this, shndx);
5637 // We do not handle merged sections here.
5638 gold_assert(poris != NULL);
5639 return poris->address();
5640 }
5641 else
5642 return os->address() + this->get_output_section_offset(shndx);
5643}
5644
44272192
DK
5645// Determine if we want to scan the SHNDX-th section for non-relocation stubs.
5646// This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5647
5648template<bool big_endian>
5649bool
5650Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
5651 const elfcpp::Shdr<32, big_endian>& shdr,
5652 unsigned int shndx,
5653 Output_section* os,
5654 const Symbol_table* symtab)
5655{
cf846138 5656 if (!this->section_is_scannable(shdr, shndx, os, symtab))
44272192
DK
5657 return false;
5658
44272192
DK
5659 // If the section does not cross any 4K-boundaries, it does not need to
5660 // be scanned.
cb1be87e 5661 Arm_address address = this->simple_input_section_output_address(shndx, os);
44272192
DK
5662 if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
5663 return false;
5664
5665 return true;
5666}
5667
5668// Scan a section for Cortex-A8 workaround.
5669
5670template<bool big_endian>
5671void
5672Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
5673 const elfcpp::Shdr<32, big_endian>& shdr,
5674 unsigned int shndx,
5675 Output_section* os,
5676 Target_arm<big_endian>* arm_target)
5677{
c8761b9a
DK
5678 // Look for the first mapping symbol in this section. It should be
5679 // at (shndx, 0).
5680 Mapping_symbol_position section_start(shndx, 0);
5681 typename Mapping_symbols_info::const_iterator p =
5682 this->mapping_symbols_info_.lower_bound(section_start);
5683
5684 // There are no mapping symbols for this section. Treat it as a data-only
5685 // section.
5686 if (p == this->mapping_symbols_info_.end() || p->first.first != shndx)
5687 return;
5688
cb1be87e
DK
5689 Arm_address output_address =
5690 this->simple_input_section_output_address(shndx, os);
44272192
DK
5691
5692 // Get the section contents.
5693 section_size_type input_view_size = 0;
5694 const unsigned char* input_view =
5695 this->section_contents(shndx, &input_view_size, false);
5696
5697 // We need to go through the mapping symbols to determine what to
5698 // scan. There are two reasons. First, we should look at THUMB code and
5699 // THUMB code only. Second, we only want to look at the 4K-page boundary
5700 // to speed up the scanning.
5701
44272192
DK
5702 while (p != this->mapping_symbols_info_.end()
5703 && p->first.first == shndx)
5704 {
5705 typename Mapping_symbols_info::const_iterator next =
5706 this->mapping_symbols_info_.upper_bound(p->first);
5707
5708 // Only scan part of a section with THUMB code.
5709 if (p->second == 't')
5710 {
5711 // Determine the end of this range.
5712 section_size_type span_start =
5713 convert_to_section_size_type(p->first.second);
5714 section_size_type span_end;
5715 if (next != this->mapping_symbols_info_.end()
5716 && next->first.first == shndx)
5717 span_end = convert_to_section_size_type(next->first.second);
5718 else
5719 span_end = convert_to_section_size_type(shdr.get_sh_size());
5720
5721 if (((span_start + output_address) & ~0xfffUL)
5722 != ((span_end + output_address - 1) & ~0xfffUL))
5723 {
5724 arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
5725 span_start, span_end,
5726 input_view,
5727 output_address);
5728 }
5729 }
5730
5731 p = next;
5732 }
5733}
5734
8ffa3667
DK
5735// Scan relocations for stub generation.
5736
5737template<bool big_endian>
5738void
5739Arm_relobj<big_endian>::scan_sections_for_stubs(
5740 Target_arm<big_endian>* arm_target,
5741 const Symbol_table* symtab,
2ea97941 5742 const Layout* layout)
8ffa3667 5743{
2ea97941
ILT
5744 unsigned int shnum = this->shnum();
5745 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
8ffa3667
DK
5746
5747 // Read the section headers.
5748 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
2ea97941 5749 shnum * shdr_size,
8ffa3667
DK
5750 true, true);
5751
5752 // To speed up processing, we set up hash tables for fast lookup of
5753 // input offsets to output addresses.
5754 this->initialize_input_to_output_maps();
5755
5756 const Relobj::Output_sections& out_sections(this->output_sections());
5757
5758 Relocate_info<32, big_endian> relinfo;
8ffa3667 5759 relinfo.symtab = symtab;
2ea97941 5760 relinfo.layout = layout;
8ffa3667
DK
5761 relinfo.object = this;
5762
44272192 5763 // Do relocation stubs scanning.
2ea97941
ILT
5764 const unsigned char* p = pshdrs + shdr_size;
5765 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
8ffa3667 5766 {
44272192 5767 const elfcpp::Shdr<32, big_endian> shdr(p);
2b328d4e
DK
5768 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
5769 pshdrs))
8ffa3667 5770 {
44272192
DK
5771 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5772 Arm_address output_offset = this->get_output_section_offset(index);
5773 Arm_address output_address;
5774 if(output_offset != invalid_address)
5775 output_address = out_sections[index]->address() + output_offset;
5776 else
5777 {
5778 // Currently this only happens for a relaxed section.
5779 const Output_relaxed_input_section* poris =
5780 out_sections[index]->find_relaxed_input_section(this, index);
5781 gold_assert(poris != NULL);
5782 output_address = poris->address();
5783 }
8ffa3667 5784
44272192
DK
5785 // Get the relocations.
5786 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
5787 shdr.get_sh_size(),
5788 true, false);
5789
5790 // Get the section contents. This does work for the case in which
5791 // we modify the contents of an input section. We need to pass the
5792 // output view under such circumstances.
5793 section_size_type input_view_size = 0;
5794 const unsigned char* input_view =
5795 this->section_contents(index, &input_view_size, false);
5796
5797 relinfo.reloc_shndx = i;
5798 relinfo.data_shndx = index;
5799 unsigned int sh_type = shdr.get_sh_type();
b521dfe4
DK
5800 unsigned int reloc_size;
5801 if (sh_type == elfcpp::SHT_REL)
5802 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5803 else
5804 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
44272192
DK
5805
5806 Output_section* os = out_sections[index];
5807 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
5808 shdr.get_sh_size() / reloc_size,
5809 os,
5810 output_offset == invalid_address,
5811 input_view, output_address,
5812 input_view_size);
8ffa3667 5813 }
44272192 5814 }
8ffa3667 5815
44272192
DK
5816 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
5817 // after its relocation section, if there is one, is processed for
5818 // relocation stubs. Merging this loop with the one above would have been
5819 // complicated since we would have had to make sure that relocation stub
5820 // scanning is done first.
5821 if (arm_target->fix_cortex_a8())
5822 {
5823 const unsigned char* p = pshdrs + shdr_size;
5824 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
8ffa3667 5825 {
44272192
DK
5826 const elfcpp::Shdr<32, big_endian> shdr(p);
5827 if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
5828 out_sections[i],
5829 symtab))
5830 this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
5831 arm_target);
8ffa3667 5832 }
8ffa3667
DK
5833 }
5834
5835 // After we've done the relocations, we release the hash tables,
5836 // since we no longer need them.
5837 this->free_input_to_output_maps();
5838}
5839
5840// Count the local symbols. The ARM backend needs to know if a symbol
5841// is a THUMB function or not. For global symbols, it is easy because
5842// the Symbol object keeps the ELF symbol type. For local symbol it is
5843// harder because we cannot access this information. So we override the
5844// do_count_local_symbol in parent and scan local symbols to mark
5845// THUMB functions. This is not the most efficient way but I do not want to
5846// slow down other ports by calling a per symbol targer hook inside
5847// Sized_relobj<size, big_endian>::do_count_local_symbols.
5848
5849template<bool big_endian>
5850void
5851Arm_relobj<big_endian>::do_count_local_symbols(
5852 Stringpool_template<char>* pool,
5853 Stringpool_template<char>* dynpool)
5854{
5855 // We need to fix-up the values of any local symbols whose type are
5856 // STT_ARM_TFUNC.
5857
5858 // Ask parent to count the local symbols.
5859 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
5860 const unsigned int loccount = this->local_symbol_count();
5861 if (loccount == 0)
5862 return;
5863
5864 // Intialize the thumb function bit-vector.
5865 std::vector<bool> empty_vector(loccount, false);
5866 this->local_symbol_is_thumb_function_.swap(empty_vector);
5867
5868 // Read the symbol table section header.
2ea97941 5869 const unsigned int symtab_shndx = this->symtab_shndx();
8ffa3667 5870 elfcpp::Shdr<32, big_endian>
2ea97941 5871 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
8ffa3667
DK
5872 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
5873
5874 // Read the local symbols.
2ea97941 5875 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
8ffa3667 5876 gold_assert(loccount == symtabshdr.get_sh_info());
2ea97941 5877 off_t locsize = loccount * sym_size;
8ffa3667
DK
5878 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
5879 locsize, true, true);
5880
20138696
DK
5881 // For mapping symbol processing, we need to read the symbol names.
5882 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
5883 if (strtab_shndx >= this->shnum())
5884 {
5885 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
5886 return;
5887 }
5888
5889 elfcpp::Shdr<32, big_endian>
5890 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
5891 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
5892 {
5893 this->error(_("symbol table name section has wrong type: %u"),
5894 static_cast<unsigned int>(strtabshdr.get_sh_type()));
5895 return;
5896 }
5897 const char* pnames =
5898 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
5899 strtabshdr.get_sh_size(),
5900 false, false));
5901
8ffa3667
DK
5902 // Loop over the local symbols and mark any local symbols pointing
5903 // to THUMB functions.
5904
5905 // Skip the first dummy symbol.
2ea97941 5906 psyms += sym_size;
8ffa3667
DK
5907 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
5908 this->local_values();
2ea97941 5909 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
8ffa3667
DK
5910 {
5911 elfcpp::Sym<32, big_endian> sym(psyms);
5912 elfcpp::STT st_type = sym.get_st_type();
5913 Symbol_value<32>& lv((*plocal_values)[i]);
5914 Arm_address input_value = lv.input_value();
5915
20138696
DK
5916 // Check to see if this is a mapping symbol.
5917 const char* sym_name = pnames + sym.get_st_name();
5918 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
5919 {
5920 unsigned int input_shndx = sym.get_st_shndx();
5921
5922 // Strip of LSB in case this is a THUMB symbol.
5923 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
5924 this->mapping_symbols_info_[msp] = sym_name[1];
5925 }
5926
8ffa3667
DK
5927 if (st_type == elfcpp::STT_ARM_TFUNC
5928 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
5929 {
5930 // This is a THUMB function. Mark this and canonicalize the
5931 // symbol value by setting LSB.
5932 this->local_symbol_is_thumb_function_[i] = true;
5933 if ((input_value & 1) == 0)
5934 lv.set_input_value(input_value | 1);
5935 }
5936 }
5937}
5938
5939// Relocate sections.
5940template<bool big_endian>
5941void
5942Arm_relobj<big_endian>::do_relocate_sections(
8ffa3667 5943 const Symbol_table* symtab,
2ea97941 5944 const Layout* layout,
8ffa3667
DK
5945 const unsigned char* pshdrs,
5946 typename Sized_relobj<32, big_endian>::Views* pviews)
5947{
5948 // Call parent to relocate sections.
2ea97941 5949 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
43d12afe 5950 pviews);
8ffa3667
DK
5951
5952 // We do not generate stubs if doing a relocatable link.
5953 if (parameters->options().relocatable())
5954 return;
5955
5956 // Relocate stub tables.
2ea97941 5957 unsigned int shnum = this->shnum();
8ffa3667
DK
5958
5959 Target_arm<big_endian>* arm_target =
5960 Target_arm<big_endian>::default_target();
5961
5962 Relocate_info<32, big_endian> relinfo;
8ffa3667 5963 relinfo.symtab = symtab;
2ea97941 5964 relinfo.layout = layout;
8ffa3667
DK
5965 relinfo.object = this;
5966
2ea97941 5967 for (unsigned int i = 1; i < shnum; ++i)
8ffa3667
DK
5968 {
5969 Arm_input_section<big_endian>* arm_input_section =
5970 arm_target->find_arm_input_section(this, i);
5971
41263c05
DK
5972 if (arm_input_section != NULL
5973 && arm_input_section->is_stub_table_owner()
5974 && !arm_input_section->stub_table()->empty())
5975 {
5976 // We cannot discard a section if it owns a stub table.
5977 Output_section* os = this->output_section(i);
5978 gold_assert(os != NULL);
5979
5980 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
5981 relinfo.reloc_shdr = NULL;
5982 relinfo.data_shndx = i;
5983 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
5984
5985 gold_assert((*pviews)[i].view != NULL);
5986
5987 // We are passed the output section view. Adjust it to cover the
5988 // stub table only.
5989 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
5990 gold_assert((stub_table->address() >= (*pviews)[i].address)
5991 && ((stub_table->address() + stub_table->data_size())
5992 <= (*pviews)[i].address + (*pviews)[i].view_size));
5993
5994 off_t offset = stub_table->address() - (*pviews)[i].address;
5995 unsigned char* view = (*pviews)[i].view + offset;
5996 Arm_address address = stub_table->address();
5997 section_size_type view_size = stub_table->data_size();
8ffa3667 5998
41263c05
DK
5999 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
6000 view_size);
6001 }
6002
6003 // Apply Cortex A8 workaround if applicable.
6004 if (this->section_has_cortex_a8_workaround(i))
6005 {
6006 unsigned char* view = (*pviews)[i].view;
6007 Arm_address view_address = (*pviews)[i].address;
6008 section_size_type view_size = (*pviews)[i].view_size;
6009 Stub_table<big_endian>* stub_table = this->stub_tables_[i];
6010
6011 // Adjust view to cover section.
6012 Output_section* os = this->output_section(i);
6013 gold_assert(os != NULL);
cb1be87e
DK
6014 Arm_address section_address =
6015 this->simple_input_section_output_address(i, os);
41263c05
DK
6016 uint64_t section_size = this->section_size(i);
6017
6018 gold_assert(section_address >= view_address
6019 && ((section_address + section_size)
6020 <= (view_address + view_size)));
6021
6022 unsigned char* section_view = view + (section_address - view_address);
6023
6024 // Apply the Cortex-A8 workaround to the output address range
6025 // corresponding to this input section.
6026 stub_table->apply_cortex_a8_workaround_to_address_range(
6027 arm_target,
6028 section_view,
6029 section_address,
6030 section_size);
6031 }
8ffa3667
DK
6032 }
6033}
6034
c8761b9a
DK
6035// Find the linked text section of an EXIDX section by looking the the first
6036// relocation. 4.4.1 of the EHABI specifications says that an EXIDX section
6037// must be linked to to its associated code section via the sh_link field of
6038// its section header. However, some tools are broken and the link is not
6039// always set. LD just drops such an EXIDX section silently, causing the
6040// associated code not unwindabled. Here we try a little bit harder to
6041// discover the linked code section.
6042//
6043// PSHDR points to the section header of a relocation section of an EXIDX
6044// section. If we can find a linked text section, return true and
6045// store the text section index in the location PSHNDX. Otherwise
6046// return false.
a0351a69
DK
6047
6048template<bool big_endian>
c8761b9a
DK
6049bool
6050Arm_relobj<big_endian>::find_linked_text_section(
6051 const unsigned char* pshdr,
6052 const unsigned char* psyms,
6053 unsigned int* pshndx)
a0351a69 6054{
c8761b9a
DK
6055 elfcpp::Shdr<32, big_endian> shdr(pshdr);
6056
6057 // If there is no relocation, we cannot find the linked text section.
6058 size_t reloc_size;
6059 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6060 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6061 else
6062 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6063 size_t reloc_count = shdr.get_sh_size() / reloc_size;
6064
6065 // Get the relocations.
6066 const unsigned char* prelocs =
6067 this->get_view(shdr.get_sh_offset(), shdr.get_sh_size(), true, false);
993d07c1 6068
c8761b9a
DK
6069 // Find the REL31 relocation for the first word of the first EXIDX entry.
6070 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
a0351a69 6071 {
c8761b9a
DK
6072 Arm_address r_offset;
6073 typename elfcpp::Elf_types<32>::Elf_WXword r_info;
6074 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6075 {
6076 typename elfcpp::Rel<32, big_endian> reloc(prelocs);
6077 r_info = reloc.get_r_info();
6078 r_offset = reloc.get_r_offset();
6079 }
6080 else
6081 {
6082 typename elfcpp::Rela<32, big_endian> reloc(prelocs);
6083 r_info = reloc.get_r_info();
6084 r_offset = reloc.get_r_offset();
6085 }
6086
6087 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
6088 if (r_type != elfcpp::R_ARM_PREL31 && r_type != elfcpp::R_ARM_SBREL31)
6089 continue;
6090
6091 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6092 if (r_sym == 0
6093 || r_sym >= this->local_symbol_count()
6094 || r_offset != 0)
6095 continue;
6096
6097 // This is the relocation for the first word of the first EXIDX entry.
6098 // We expect to see a local section symbol.
6099 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6100 elfcpp::Sym<32, big_endian> sym(psyms + r_sym * sym_size);
6101 if (sym.get_st_type() == elfcpp::STT_SECTION)
6102 {
6103 *pshndx = this->adjust_shndx(sym.get_st_shndx());
6104 return true;
6105 }
6106 else
6107 return false;
993d07c1 6108 }
c8761b9a
DK
6109
6110 return false;
6111}
6112
6113// Make an EXIDX input section object for an EXIDX section whose index is
6114// SHNDX. SHDR is the section header of the EXIDX section and TEXT_SHNDX
6115// is the section index of the linked text section.
6116
6117template<bool big_endian>
6118void
6119Arm_relobj<big_endian>::make_exidx_input_section(
6120 unsigned int shndx,
6121 const elfcpp::Shdr<32, big_endian>& shdr,
6122 unsigned int text_shndx)
6123{
993d07c1
DK
6124 // Issue an error and ignore this EXIDX section if it points to a text
6125 // section already has an EXIDX section.
6126 if (this->exidx_section_map_[text_shndx] != NULL)
6127 {
6128 gold_error(_("EXIDX sections %u and %u both link to text section %u "
6129 "in %s"),
6130 shndx, this->exidx_section_map_[text_shndx]->shndx(),
6131 text_shndx, this->name().c_str());
6132 return;
a0351a69 6133 }
993d07c1
DK
6134
6135 // Create an Arm_exidx_input_section object for this EXIDX section.
6136 Arm_exidx_input_section* exidx_input_section =
6137 new Arm_exidx_input_section(this, shndx, text_shndx, shdr.get_sh_size(),
6138 shdr.get_sh_addralign());
6139 this->exidx_section_map_[text_shndx] = exidx_input_section;
6140
6141 // Also map the EXIDX section index to this.
6142 gold_assert(this->exidx_section_map_[shndx] == NULL);
6143 this->exidx_section_map_[shndx] = exidx_input_section;
a0351a69
DK
6144}
6145
d5b40221
DK
6146// Read the symbol information.
6147
6148template<bool big_endian>
6149void
6150Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6151{
6152 // Call parent class to read symbol information.
6153 Sized_relobj<32, big_endian>::do_read_symbols(sd);
6154
6155 // Read processor-specific flags in ELF file header.
6156 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6157 elfcpp::Elf_sizes<32>::ehdr_size,
6158 true, false);
6159 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6160 this->processor_specific_flags_ = ehdr.get_e_flags();
993d07c1
DK
6161
6162 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
6163 // sections.
c8761b9a 6164 std::vector<unsigned int> deferred_exidx_sections;
993d07c1 6165 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
c8761b9a
DK
6166 const unsigned char* pshdrs = sd->section_headers->data();
6167 const unsigned char *ps = pshdrs + shdr_size;
993d07c1
DK
6168 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6169 {
6170 elfcpp::Shdr<32, big_endian> shdr(ps);
6171 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6172 {
6173 gold_assert(this->attributes_section_data_ == NULL);
6174 section_offset_type section_offset = shdr.get_sh_offset();
6175 section_size_type section_size =
6176 convert_to_section_size_type(shdr.get_sh_size());
6177 File_view* view = this->get_lasting_view(section_offset,
6178 section_size, true, false);
6179 this->attributes_section_data_ =
6180 new Attributes_section_data(view->data(), section_size);
6181 }
6182 else if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
c8761b9a
DK
6183 {
6184 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6185 if (text_shndx >= this->shnum())
6186 gold_error(_("EXIDX section %u linked to invalid section %u"),
6187 i, text_shndx);
6188 else if (text_shndx == elfcpp::SHN_UNDEF)
6189 deferred_exidx_sections.push_back(i);
6190 else
6191 this->make_exidx_input_section(i, shdr, text_shndx);
6192 }
6193 }
6194
6195 // Some tools are broken and they do not set the link of EXIDX sections.
6196 // We look at the first relocation to figure out the linked sections.
6197 if (!deferred_exidx_sections.empty())
6198 {
6199 // We need to go over the section headers again to find the mapping
6200 // from sections being relocated to their relocation sections. This is
6201 // a bit inefficient as we could do that in the loop above. However,
6202 // we do not expect any deferred EXIDX sections normally. So we do not
6203 // want to slow down the most common path.
6204 typedef Unordered_map<unsigned int, unsigned int> Reloc_map;
6205 Reloc_map reloc_map;
6206 ps = pshdrs + shdr_size;
6207 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6208 {
6209 elfcpp::Shdr<32, big_endian> shdr(ps);
6210 elfcpp::Elf_Word sh_type = shdr.get_sh_type();
6211 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
6212 {
6213 unsigned int info_shndx = this->adjust_shndx(shdr.get_sh_info());
6214 if (info_shndx >= this->shnum())
6215 gold_error(_("relocation section %u has invalid info %u"),
6216 i, info_shndx);
6217 Reloc_map::value_type value(info_shndx, i);
6218 std::pair<Reloc_map::iterator, bool> result =
6219 reloc_map.insert(value);
6220 if (!result.second)
6221 gold_error(_("section %u has multiple relocation sections "
6222 "%u and %u"),
6223 info_shndx, i, reloc_map[info_shndx]);
6224 }
6225 }
6226
6227 // Read the symbol table section header.
6228 const unsigned int symtab_shndx = this->symtab_shndx();
6229 elfcpp::Shdr<32, big_endian>
6230 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6231 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6232
6233 // Read the local symbols.
6234 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6235 const unsigned int loccount = this->local_symbol_count();
6236 gold_assert(loccount == symtabshdr.get_sh_info());
6237 off_t locsize = loccount * sym_size;
6238 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6239 locsize, true, true);
6240
6241 // Process the deferred EXIDX sections.
6242 for(unsigned int i = 0; i < deferred_exidx_sections.size(); ++i)
6243 {
6244 unsigned int shndx = deferred_exidx_sections[i];
6245 elfcpp::Shdr<32, big_endian> shdr(pshdrs + shndx * shdr_size);
6246 unsigned int text_shndx;
6247 Reloc_map::const_iterator it = reloc_map.find(shndx);
6248 if (it != reloc_map.end()
6249 && find_linked_text_section(pshdrs + it->second * shdr_size,
6250 psyms, &text_shndx))
6251 this->make_exidx_input_section(shndx, shdr, text_shndx);
6252 else
6253 gold_error(_("EXIDX section %u has no linked text section."),
6254 shndx);
6255 }
993d07c1 6256 }
d5b40221
DK
6257}
6258
99e5bff2
DK
6259// Process relocations for garbage collection. The ARM target uses .ARM.exidx
6260// sections for unwinding. These sections are referenced implicitly by
6261// text sections linked in the section headers. If we ignore these implict
6262// references, the .ARM.exidx sections and any .ARM.extab sections they use
6263// will be garbage-collected incorrectly. Hence we override the same function
6264// in the base class to handle these implicit references.
6265
6266template<bool big_endian>
6267void
6268Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
6269 Layout* layout,
6270 Read_relocs_data* rd)
6271{
6272 // First, call base class method to process relocations in this object.
6273 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
6274
6275 unsigned int shnum = this->shnum();
6276 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6277 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
6278 shnum * shdr_size,
6279 true, true);
6280
6281 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
6282 // to these from the linked text sections.
6283 const unsigned char* ps = pshdrs + shdr_size;
6284 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
6285 {
6286 elfcpp::Shdr<32, big_endian> shdr(ps);
6287 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6288 {
6289 // Found an .ARM.exidx section, add it to the set of reachable
6290 // sections from its linked text section.
6291 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6292 symtab->gc()->add_reference(this, text_shndx, this, i);
6293 }
6294 }
6295}
6296
e7eca48c
DK
6297// Update output local symbol count. Owing to EXIDX entry merging, some local
6298// symbols will be removed in output. Adjust output local symbol count
6299// accordingly. We can only changed the static output local symbol count. It
6300// is too late to change the dynamic symbols.
6301
6302template<bool big_endian>
6303void
6304Arm_relobj<big_endian>::update_output_local_symbol_count()
6305{
6306 // Caller should check that this needs updating. We want caller checking
6307 // because output_local_symbol_count_needs_update() is most likely inlined.
6308 gold_assert(this->output_local_symbol_count_needs_update_);
6309
6310 gold_assert(this->symtab_shndx() != -1U);
6311 if (this->symtab_shndx() == 0)
6312 {
6313 // This object has no symbols. Weird but legal.
6314 return;
6315 }
6316
6317 // Read the symbol table section header.
6318 const unsigned int symtab_shndx = this->symtab_shndx();
6319 elfcpp::Shdr<32, big_endian>
6320 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6321 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6322
6323 // Read the local symbols.
6324 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6325 const unsigned int loccount = this->local_symbol_count();
6326 gold_assert(loccount == symtabshdr.get_sh_info());
6327 off_t locsize = loccount * sym_size;
6328 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6329 locsize, true, true);
6330
6331 // Loop over the local symbols.
6332
6333 typedef typename Sized_relobj<32, big_endian>::Output_sections
6334 Output_sections;
6335 const Output_sections& out_sections(this->output_sections());
6336 unsigned int shnum = this->shnum();
6337 unsigned int count = 0;
6338 // Skip the first, dummy, symbol.
6339 psyms += sym_size;
6340 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6341 {
6342 elfcpp::Sym<32, big_endian> sym(psyms);
6343
6344 Symbol_value<32>& lv((*this->local_values())[i]);
6345
6346 // This local symbol was already discarded by do_count_local_symbols.
6347 if (!lv.needs_output_symtab_entry())
6348 continue;
6349
6350 bool is_ordinary;
6351 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
6352 &is_ordinary);
6353
6354 if (shndx < shnum)
6355 {
6356 Output_section* os = out_sections[shndx];
6357
6358 // This local symbol no longer has an output section. Discard it.
6359 if (os == NULL)
6360 {
6361 lv.set_no_output_symtab_entry();
6362 continue;
6363 }
6364
6365 // Currently we only discard parts of EXIDX input sections.
6366 // We explicitly check for a merged EXIDX input section to avoid
6367 // calling Output_section_data::output_offset unless necessary.
6368 if ((this->get_output_section_offset(shndx) == invalid_address)
6369 && (this->exidx_input_section_by_shndx(shndx) != NULL))
6370 {
6371 section_offset_type output_offset =
6372 os->output_offset(this, shndx, lv.input_value());
6373 if (output_offset == -1)
6374 {
6375 // This symbol is defined in a part of an EXIDX input section
6376 // that is discarded due to entry merging.
6377 lv.set_no_output_symtab_entry();
6378 continue;
6379 }
6380 }
6381 }
6382
6383 ++count;
6384 }
6385
6386 this->set_output_local_symbol_count(count);
6387 this->output_local_symbol_count_needs_update_ = false;
6388}
6389
d5b40221
DK
6390// Arm_dynobj methods.
6391
6392// Read the symbol information.
6393
6394template<bool big_endian>
6395void
6396Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6397{
6398 // Call parent class to read symbol information.
6399 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
6400
6401 // Read processor-specific flags in ELF file header.
6402 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6403 elfcpp::Elf_sizes<32>::ehdr_size,
6404 true, false);
6405 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6406 this->processor_specific_flags_ = ehdr.get_e_flags();
993d07c1
DK
6407
6408 // Read the attributes section if there is one.
6409 // We read from the end because gas seems to put it near the end of
6410 // the section headers.
6411 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6412 const unsigned char *ps =
6413 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
6414 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
6415 {
6416 elfcpp::Shdr<32, big_endian> shdr(ps);
6417 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6418 {
6419 section_offset_type section_offset = shdr.get_sh_offset();
6420 section_size_type section_size =
6421 convert_to_section_size_type(shdr.get_sh_size());
6422 File_view* view = this->get_lasting_view(section_offset,
6423 section_size, true, false);
6424 this->attributes_section_data_ =
6425 new Attributes_section_data(view->data(), section_size);
6426 break;
6427 }
6428 }
d5b40221
DK
6429}
6430
e9bbb538
DK
6431// Stub_addend_reader methods.
6432
6433// Read the addend of a REL relocation of type R_TYPE at VIEW.
6434
6435template<bool big_endian>
6436elfcpp::Elf_types<32>::Elf_Swxword
6437Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
6438 unsigned int r_type,
6439 const unsigned char* view,
6440 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
6441{
089d69dc
DK
6442 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
6443
e9bbb538
DK
6444 switch (r_type)
6445 {
6446 case elfcpp::R_ARM_CALL:
6447 case elfcpp::R_ARM_JUMP24:
6448 case elfcpp::R_ARM_PLT32:
6449 {
6450 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6451 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6452 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
6453 return utils::sign_extend<26>(val << 2);
6454 }
6455
6456 case elfcpp::R_ARM_THM_CALL:
6457 case elfcpp::R_ARM_THM_JUMP24:
6458 case elfcpp::R_ARM_THM_XPC22:
6459 {
e9bbb538
DK
6460 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6461 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6462 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6463 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
089d69dc 6464 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
e9bbb538
DK
6465 }
6466
6467 case elfcpp::R_ARM_THM_JUMP19:
6468 {
6469 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6470 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6471 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6472 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
089d69dc 6473 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
e9bbb538
DK
6474 }
6475
6476 default:
6477 gold_unreachable();
6478 }
6479}
6480
94cdfcff
DK
6481// A class to handle the PLT data.
6482
6483template<bool big_endian>
6484class Output_data_plt_arm : public Output_section_data
6485{
6486 public:
6487 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
6488 Reloc_section;
6489
6490 Output_data_plt_arm(Layout*, Output_data_space*);
6491
6492 // Add an entry to the PLT.
6493 void
6494 add_entry(Symbol* gsym);
6495
6496 // Return the .rel.plt section data.
6497 const Reloc_section*
6498 rel_plt() const
6499 { return this->rel_; }
6500
6501 protected:
6502 void
6503 do_adjust_output_section(Output_section* os);
6504
6505 // Write to a map file.
6506 void
6507 do_print_to_mapfile(Mapfile* mapfile) const
6508 { mapfile->print_output_data(this, _("** PLT")); }
6509
6510 private:
6511 // Template for the first PLT entry.
6512 static const uint32_t first_plt_entry[5];
6513
6514 // Template for subsequent PLT entries.
6515 static const uint32_t plt_entry[3];
6516
6517 // Set the final size.
6518 void
6519 set_final_data_size()
6520 {
6521 this->set_data_size(sizeof(first_plt_entry)
6522 + this->count_ * sizeof(plt_entry));
6523 }
6524
6525 // Write out the PLT data.
6526 void
6527 do_write(Output_file*);
6528
6529 // The reloc section.
6530 Reloc_section* rel_;
6531 // The .got.plt section.
6532 Output_data_space* got_plt_;
6533 // The number of PLT entries.
6534 unsigned int count_;
6535};
6536
6537// Create the PLT section. The ordinary .got section is an argument,
6538// since we need to refer to the start. We also create our own .got
6539// section just for PLT entries.
6540
6541template<bool big_endian>
2ea97941 6542Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
94cdfcff
DK
6543 Output_data_space* got_plt)
6544 : Output_section_data(4), got_plt_(got_plt), count_(0)
6545{
6546 this->rel_ = new Reloc_section(false);
2ea97941 6547 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1a2dff53
ILT
6548 elfcpp::SHF_ALLOC, this->rel_, true, false,
6549 false, false);
94cdfcff
DK
6550}
6551
6552template<bool big_endian>
6553void
6554Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
6555{
6556 os->set_entsize(0);
6557}
6558
6559// Add an entry to the PLT.
6560
6561template<bool big_endian>
6562void
6563Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
6564{
6565 gold_assert(!gsym->has_plt_offset());
6566
6567 // Note that when setting the PLT offset we skip the initial
6568 // reserved PLT entry.
6569 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
6570 + sizeof(first_plt_entry));
6571
6572 ++this->count_;
6573
6574 section_offset_type got_offset = this->got_plt_->current_data_size();
6575
6576 // Every PLT entry needs a GOT entry which points back to the PLT
6577 // entry (this will be changed by the dynamic linker, normally
6578 // lazily when the function is called).
6579 this->got_plt_->set_current_data_size(got_offset + 4);
6580
6581 // Every PLT entry needs a reloc.
6582 gsym->set_needs_dynsym_entry();
6583 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
6584 got_offset);
6585
6586 // Note that we don't need to save the symbol. The contents of the
6587 // PLT are independent of which symbols are used. The symbols only
6588 // appear in the relocations.
6589}
6590
6591// ARM PLTs.
6592// FIXME: This is not very flexible. Right now this has only been tested
6593// on armv5te. If we are to support additional architecture features like
6594// Thumb-2 or BE8, we need to make this more flexible like GNU ld.
6595
6596// The first entry in the PLT.
6597template<bool big_endian>
6598const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
6599{
6600 0xe52de004, // str lr, [sp, #-4]!
6601 0xe59fe004, // ldr lr, [pc, #4]
6602 0xe08fe00e, // add lr, pc, lr
6603 0xe5bef008, // ldr pc, [lr, #8]!
6604 0x00000000, // &GOT[0] - .
6605};
6606
6607// Subsequent entries in the PLT.
6608
6609template<bool big_endian>
6610const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
6611{
6612 0xe28fc600, // add ip, pc, #0xNN00000
6613 0xe28cca00, // add ip, ip, #0xNN000
6614 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
6615};
6616
6617// Write out the PLT. This uses the hand-coded instructions above,
6618// and adjusts them as needed. This is all specified by the arm ELF
6619// Processor Supplement.
6620
6621template<bool big_endian>
6622void
6623Output_data_plt_arm<big_endian>::do_write(Output_file* of)
6624{
2ea97941 6625 const off_t offset = this->offset();
94cdfcff
DK
6626 const section_size_type oview_size =
6627 convert_to_section_size_type(this->data_size());
2ea97941 6628 unsigned char* const oview = of->get_output_view(offset, oview_size);
94cdfcff
DK
6629
6630 const off_t got_file_offset = this->got_plt_->offset();
6631 const section_size_type got_size =
6632 convert_to_section_size_type(this->got_plt_->data_size());
6633 unsigned char* const got_view = of->get_output_view(got_file_offset,
6634 got_size);
6635 unsigned char* pov = oview;
6636
ebabffbd
DK
6637 Arm_address plt_address = this->address();
6638 Arm_address got_address = this->got_plt_->address();
94cdfcff
DK
6639
6640 // Write first PLT entry. All but the last word are constants.
6641 const size_t num_first_plt_words = (sizeof(first_plt_entry)
6642 / sizeof(plt_entry[0]));
6643 for (size_t i = 0; i < num_first_plt_words - 1; i++)
6644 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
6645 // Last word in first PLT entry is &GOT[0] - .
6646 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
6647 got_address - (plt_address + 16));
6648 pov += sizeof(first_plt_entry);
6649
6650 unsigned char* got_pov = got_view;
6651
6652 memset(got_pov, 0, 12);
6653 got_pov += 12;
6654
6655 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
6656 unsigned int plt_offset = sizeof(first_plt_entry);
6657 unsigned int plt_rel_offset = 0;
6658 unsigned int got_offset = 12;
6659 const unsigned int count = this->count_;
6660 for (unsigned int i = 0;
6661 i < count;
6662 ++i,
6663 pov += sizeof(plt_entry),
6664 got_pov += 4,
6665 plt_offset += sizeof(plt_entry),
6666 plt_rel_offset += rel_size,
6667 got_offset += 4)
6668 {
6669 // Set and adjust the PLT entry itself.
2ea97941
ILT
6670 int32_t offset = ((got_address + got_offset)
6671 - (plt_address + plt_offset + 8));
94cdfcff 6672
2ea97941
ILT
6673 gold_assert(offset >= 0 && offset < 0x0fffffff);
6674 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
94cdfcff 6675 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
2ea97941 6676 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
94cdfcff 6677 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
2ea97941 6678 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
94cdfcff
DK
6679 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
6680
6681 // Set the entry in the GOT.
6682 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
6683 }
6684
6685 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
6686 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
6687
2ea97941 6688 of->write_output_view(offset, oview_size, oview);
94cdfcff
DK
6689 of->write_output_view(got_file_offset, got_size, got_view);
6690}
6691
6692// Create a PLT entry for a global symbol.
6693
6694template<bool big_endian>
6695void
2ea97941 6696Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
94cdfcff
DK
6697 Symbol* gsym)
6698{
6699 if (gsym->has_plt_offset())
6700 return;
6701
6702 if (this->plt_ == NULL)
6703 {
6704 // Create the GOT sections first.
2ea97941 6705 this->got_section(symtab, layout);
94cdfcff 6706
2ea97941
ILT
6707 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
6708 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
6709 (elfcpp::SHF_ALLOC
6710 | elfcpp::SHF_EXECINSTR),
1a2dff53 6711 this->plt_, false, false, false, false);
94cdfcff
DK
6712 }
6713 this->plt_->add_entry(gsym);
6714}
6715
f96accdf
DK
6716// Get the section to use for TLS_DESC relocations.
6717
6718template<bool big_endian>
6719typename Target_arm<big_endian>::Reloc_section*
6720Target_arm<big_endian>::rel_tls_desc_section(Layout* layout) const
6721{
6722 return this->plt_section()->rel_tls_desc(layout);
6723}
6724
6725// Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
6726
6727template<bool big_endian>
6728void
6729Target_arm<big_endian>::define_tls_base_symbol(
6730 Symbol_table* symtab,
6731 Layout* layout)
6732{
6733 if (this->tls_base_symbol_defined_)
6734 return;
6735
6736 Output_segment* tls_segment = layout->tls_segment();
6737 if (tls_segment != NULL)
6738 {
6739 bool is_exec = parameters->options().output_is_executable();
6740 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
6741 Symbol_table::PREDEFINED,
6742 tls_segment, 0, 0,
6743 elfcpp::STT_TLS,
6744 elfcpp::STB_LOCAL,
6745 elfcpp::STV_HIDDEN, 0,
6746 (is_exec
6747 ? Symbol::SEGMENT_END
6748 : Symbol::SEGMENT_START),
6749 true);
6750 }
6751 this->tls_base_symbol_defined_ = true;
6752}
6753
6754// Create a GOT entry for the TLS module index.
6755
6756template<bool big_endian>
6757unsigned int
6758Target_arm<big_endian>::got_mod_index_entry(
6759 Symbol_table* symtab,
6760 Layout* layout,
6761 Sized_relobj<32, big_endian>* object)
6762{
6763 if (this->got_mod_index_offset_ == -1U)
6764 {
6765 gold_assert(symtab != NULL && layout != NULL && object != NULL);
6766 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
6767 Output_data_got<32, big_endian>* got = this->got_section(symtab, layout);
6768 unsigned int got_offset = got->add_constant(0);
6769 rel_dyn->add_local(object, 0, elfcpp::R_ARM_TLS_DTPMOD32, got,
6770 got_offset);
6771 got->add_constant(0);
6772 this->got_mod_index_offset_ = got_offset;
6773 }
6774 return this->got_mod_index_offset_;
6775}
6776
6777// Optimize the TLS relocation type based on what we know about the
6778// symbol. IS_FINAL is true if the final address of this symbol is
6779// known at link time.
6780
6781template<bool big_endian>
6782tls::Tls_optimization
6783Target_arm<big_endian>::optimize_tls_reloc(bool, int)
6784{
6785 // FIXME: Currently we do not do any TLS optimization.
6786 return tls::TLSOPT_NONE;
6787}
6788
4a657b0d
DK
6789// Report an unsupported relocation against a local symbol.
6790
6791template<bool big_endian>
6792void
6793Target_arm<big_endian>::Scan::unsupported_reloc_local(
6794 Sized_relobj<32, big_endian>* object,
6795 unsigned int r_type)
6796{
6797 gold_error(_("%s: unsupported reloc %u against local symbol"),
6798 object->name().c_str(), r_type);
6799}
6800
bec53400
DK
6801// We are about to emit a dynamic relocation of type R_TYPE. If the
6802// dynamic linker does not support it, issue an error. The GNU linker
6803// only issues a non-PIC error for an allocated read-only section.
6804// Here we know the section is allocated, but we don't know that it is
6805// read-only. But we check for all the relocation types which the
6806// glibc dynamic linker supports, so it seems appropriate to issue an
6807// error even if the section is not read-only.
6808
6809template<bool big_endian>
6810void
6811Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
6812 unsigned int r_type)
6813{
6814 switch (r_type)
6815 {
6816 // These are the relocation types supported by glibc for ARM.
6817 case elfcpp::R_ARM_RELATIVE:
6818 case elfcpp::R_ARM_COPY:
6819 case elfcpp::R_ARM_GLOB_DAT:
6820 case elfcpp::R_ARM_JUMP_SLOT:
6821 case elfcpp::R_ARM_ABS32:
be8fcb75 6822 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
6823 case elfcpp::R_ARM_PC24:
6824 // FIXME: The following 3 types are not supported by Android's dynamic
6825 // linker.
6826 case elfcpp::R_ARM_TLS_DTPMOD32:
6827 case elfcpp::R_ARM_TLS_DTPOFF32:
6828 case elfcpp::R_ARM_TLS_TPOFF32:
6829 return;
6830
6831 default:
c8761b9a
DK
6832 {
6833 // This prevents us from issuing more than one error per reloc
6834 // section. But we can still wind up issuing more than one
6835 // error per object file.
6836 if (this->issued_non_pic_error_)
6837 return;
6838 const Arm_reloc_property* reloc_property =
6839 arm_reloc_property_table->get_reloc_property(r_type);
6840 gold_assert(reloc_property != NULL);
6841 object->error(_("requires unsupported dynamic reloc %s; "
6842 "recompile with -fPIC"),
6843 reloc_property->name().c_str());
6844 this->issued_non_pic_error_ = true;
bec53400 6845 return;
c8761b9a 6846 }
bec53400
DK
6847
6848 case elfcpp::R_ARM_NONE:
6849 gold_unreachable();
6850 }
6851}
6852
4a657b0d 6853// Scan a relocation for a local symbol.
bec53400
DK
6854// FIXME: This only handles a subset of relocation types used by Android
6855// on ARM v5te devices.
4a657b0d
DK
6856
6857template<bool big_endian>
6858inline void
ad0f2072 6859Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
2ea97941 6860 Layout* layout,
bec53400 6861 Target_arm* target,
4a657b0d 6862 Sized_relobj<32, big_endian>* object,
bec53400
DK
6863 unsigned int data_shndx,
6864 Output_section* output_section,
6865 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d 6866 unsigned int r_type,
e4782e83 6867 const elfcpp::Sym<32, big_endian>& lsym)
4a657b0d 6868{
a6d1ef57 6869 r_type = get_real_reloc_type(r_type);
4a657b0d
DK
6870 switch (r_type)
6871 {
6872 case elfcpp::R_ARM_NONE:
e4782e83
DK
6873 case elfcpp::R_ARM_V4BX:
6874 case elfcpp::R_ARM_GNU_VTENTRY:
6875 case elfcpp::R_ARM_GNU_VTINHERIT:
4a657b0d
DK
6876 break;
6877
bec53400 6878 case elfcpp::R_ARM_ABS32:
be8fcb75 6879 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
6880 // If building a shared library (or a position-independent
6881 // executable), we need to create a dynamic relocation for
6882 // this location. The relocation applied at link time will
6883 // apply the link-time value, so we flag the location with
6884 // an R_ARM_RELATIVE relocation so the dynamic loader can
6885 // relocate it easily.
6886 if (parameters->options().output_is_position_independent())
6887 {
2ea97941 6888 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
bec53400
DK
6889 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
6890 // If we are to add more other reloc types than R_ARM_ABS32,
6891 // we need to add check_non_pic(object, r_type) here.
6892 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
6893 output_section, data_shndx,
6894 reloc.get_r_offset());
6895 }
6896 break;
6897
e4782e83
DK
6898 case elfcpp::R_ARM_ABS16:
6899 case elfcpp::R_ARM_ABS12:
be8fcb75
ILT
6900 case elfcpp::R_ARM_THM_ABS5:
6901 case elfcpp::R_ARM_ABS8:
be8fcb75 6902 case elfcpp::R_ARM_BASE_ABS:
fd3c5f0b
ILT
6903 case elfcpp::R_ARM_MOVW_ABS_NC:
6904 case elfcpp::R_ARM_MOVT_ABS:
6905 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6906 case elfcpp::R_ARM_THM_MOVT_ABS:
e4782e83
DK
6907 // If building a shared library (or a position-independent
6908 // executable), we need to create a dynamic relocation for
6909 // this location. Because the addend needs to remain in the
6910 // data section, we need to be careful not to apply this
6911 // relocation statically.
6912 if (parameters->options().output_is_position_independent())
6913 {
6914 check_non_pic(object, r_type);
6915 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6916 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
6917 if (lsym.get_st_type() != elfcpp::STT_SECTION)
6918 rel_dyn->add_local(object, r_sym, r_type, output_section,
6919 data_shndx, reloc.get_r_offset());
6920 else
6921 {
6922 gold_assert(lsym.get_st_value() == 0);
6923 unsigned int shndx = lsym.get_st_shndx();
6924 bool is_ordinary;
6925 shndx = object->adjust_sym_shndx(r_sym, shndx,
6926 &is_ordinary);
6927 if (!is_ordinary)
6928 object->error(_("section symbol %u has bad shndx %u"),
6929 r_sym, shndx);
6930 else
6931 rel_dyn->add_local_section(object, shndx,
6932 r_type, output_section,
6933 data_shndx, reloc.get_r_offset());
6934 }
6935 }
6936 break;
6937
6938 case elfcpp::R_ARM_PC24:
6939 case elfcpp::R_ARM_REL32:
6940 case elfcpp::R_ARM_LDR_PC_G0:
6941 case elfcpp::R_ARM_SBREL32:
6942 case elfcpp::R_ARM_THM_CALL:
6943 case elfcpp::R_ARM_THM_PC8:
6944 case elfcpp::R_ARM_BASE_PREL:
6945 case elfcpp::R_ARM_PLT32:
6946 case elfcpp::R_ARM_CALL:
6947 case elfcpp::R_ARM_JUMP24:
6948 case elfcpp::R_ARM_THM_JUMP24:
6949 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
6950 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
6951 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
6952 case elfcpp::R_ARM_SBREL31:
6953 case elfcpp::R_ARM_PREL31:
c2a122b6
ILT
6954 case elfcpp::R_ARM_MOVW_PREL_NC:
6955 case elfcpp::R_ARM_MOVT_PREL:
6956 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6957 case elfcpp::R_ARM_THM_MOVT_PREL:
e4782e83 6958 case elfcpp::R_ARM_THM_JUMP19:
800d0f56 6959 case elfcpp::R_ARM_THM_JUMP6:
11b861d5 6960 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
e4782e83
DK
6961 case elfcpp::R_ARM_THM_PC12:
6962 case elfcpp::R_ARM_REL32_NOI:
b10d2873
ILT
6963 case elfcpp::R_ARM_ALU_PC_G0_NC:
6964 case elfcpp::R_ARM_ALU_PC_G0:
6965 case elfcpp::R_ARM_ALU_PC_G1_NC:
6966 case elfcpp::R_ARM_ALU_PC_G1:
6967 case elfcpp::R_ARM_ALU_PC_G2:
e4782e83
DK
6968 case elfcpp::R_ARM_LDR_PC_G1:
6969 case elfcpp::R_ARM_LDR_PC_G2:
6970 case elfcpp::R_ARM_LDRS_PC_G0:
6971 case elfcpp::R_ARM_LDRS_PC_G1:
6972 case elfcpp::R_ARM_LDRS_PC_G2:
6973 case elfcpp::R_ARM_LDC_PC_G0:
6974 case elfcpp::R_ARM_LDC_PC_G1:
6975 case elfcpp::R_ARM_LDC_PC_G2:
b10d2873
ILT
6976 case elfcpp::R_ARM_ALU_SB_G0_NC:
6977 case elfcpp::R_ARM_ALU_SB_G0:
6978 case elfcpp::R_ARM_ALU_SB_G1_NC:
6979 case elfcpp::R_ARM_ALU_SB_G1:
6980 case elfcpp::R_ARM_ALU_SB_G2:
b10d2873
ILT
6981 case elfcpp::R_ARM_LDR_SB_G0:
6982 case elfcpp::R_ARM_LDR_SB_G1:
6983 case elfcpp::R_ARM_LDR_SB_G2:
b10d2873
ILT
6984 case elfcpp::R_ARM_LDRS_SB_G0:
6985 case elfcpp::R_ARM_LDRS_SB_G1:
6986 case elfcpp::R_ARM_LDRS_SB_G2:
b10d2873
ILT
6987 case elfcpp::R_ARM_LDC_SB_G0:
6988 case elfcpp::R_ARM_LDC_SB_G1:
6989 case elfcpp::R_ARM_LDC_SB_G2:
e4782e83
DK
6990 case elfcpp::R_ARM_MOVW_BREL_NC:
6991 case elfcpp::R_ARM_MOVT_BREL:
6992 case elfcpp::R_ARM_MOVW_BREL:
6993 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
6994 case elfcpp::R_ARM_THM_MOVT_BREL:
6995 case elfcpp::R_ARM_THM_MOVW_BREL:
6996 case elfcpp::R_ARM_THM_JUMP11:
6997 case elfcpp::R_ARM_THM_JUMP8:
6998 // We don't need to do anything for a relative addressing relocation
6999 // against a local symbol if it does not reference the GOT.
bec53400
DK
7000 break;
7001
7002 case elfcpp::R_ARM_GOTOFF32:
e4782e83 7003 case elfcpp::R_ARM_GOTOFF12:
bec53400 7004 // We need a GOT section:
2ea97941 7005 target->got_section(symtab, layout);
bec53400
DK
7006 break;
7007
bec53400 7008 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 7009 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
7010 {
7011 // The symbol requires a GOT entry.
7012 Output_data_got<32, big_endian>* got =
2ea97941 7013 target->got_section(symtab, layout);
bec53400
DK
7014 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7015 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
7016 {
7017 // If we are generating a shared object, we need to add a
7018 // dynamic RELATIVE relocation for this symbol's GOT entry.
7019 if (parameters->options().output_is_position_independent())
7020 {
2ea97941
ILT
7021 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7022 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
bec53400 7023 rel_dyn->add_local_relative(
2ea97941
ILT
7024 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
7025 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
bec53400
DK
7026 }
7027 }
7028 }
7029 break;
7030
7031 case elfcpp::R_ARM_TARGET1:
e4782e83 7032 case elfcpp::R_ARM_TARGET2:
bec53400
DK
7033 // This should have been mapped to another type already.
7034 // Fall through.
7035 case elfcpp::R_ARM_COPY:
7036 case elfcpp::R_ARM_GLOB_DAT:
7037 case elfcpp::R_ARM_JUMP_SLOT:
7038 case elfcpp::R_ARM_RELATIVE:
7039 // These are relocations which should only be seen by the
7040 // dynamic linker, and should never be seen here.
7041 gold_error(_("%s: unexpected reloc %u in object file"),
7042 object->name().c_str(), r_type);
7043 break;
7044
f96accdf
DK
7045
7046 // These are initial TLS relocs, which are expected when
7047 // linking.
7048 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7049 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7050 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7051 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7052 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7053 {
7054 bool output_is_shared = parameters->options().shared();
7055 const tls::Tls_optimization optimized_type
7056 = Target_arm<big_endian>::optimize_tls_reloc(!output_is_shared,
7057 r_type);
7058 switch (r_type)
7059 {
7060 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7061 if (optimized_type == tls::TLSOPT_NONE)
7062 {
7063 // Create a pair of GOT entries for the module index and
7064 // dtv-relative offset.
7065 Output_data_got<32, big_endian>* got
7066 = target->got_section(symtab, layout);
7067 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7068 unsigned int shndx = lsym.get_st_shndx();
7069 bool is_ordinary;
7070 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7071 if (!is_ordinary)
7072 object->error(_("local symbol %u has bad shndx %u"),
7073 r_sym, shndx);
7074 else
7075 got->add_local_pair_with_rel(object, r_sym, shndx,
7076 GOT_TYPE_TLS_PAIR,
7077 target->rel_dyn_section(layout),
7078 elfcpp::R_ARM_TLS_DTPMOD32, 0);
7079 }
7080 else
7081 // FIXME: TLS optimization not supported yet.
7082 gold_unreachable();
7083 break;
7084
7085 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7086 if (optimized_type == tls::TLSOPT_NONE)
7087 {
7088 // Create a GOT entry for the module index.
7089 target->got_mod_index_entry(symtab, layout, object);
7090 }
7091 else
7092 // FIXME: TLS optimization not supported yet.
7093 gold_unreachable();
7094 break;
7095
7096 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7097 break;
7098
7099 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7100 layout->set_has_static_tls();
7101 if (optimized_type == tls::TLSOPT_NONE)
7102 {
7103 // Create a GOT entry for the tp-relative offset.
7104 Output_data_got<32, big_endian>* got
7105 = target->got_section(symtab, layout);
7106 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7107 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
7108 target->rel_dyn_section(layout),
7109 elfcpp::R_ARM_TLS_TPOFF32);
7110 }
7111 else
7112 // FIXME: TLS optimization not supported yet.
7113 gold_unreachable();
7114 break;
7115
7116 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7117 layout->set_has_static_tls();
7118 if (output_is_shared)
7119 {
7120 // We need to create a dynamic relocation.
7121 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
7122 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7123 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7124 rel_dyn->add_local(object, r_sym, elfcpp::R_ARM_TLS_TPOFF32,
7125 output_section, data_shndx,
7126 reloc.get_r_offset());
7127 }
7128 break;
7129
7130 default:
7131 gold_unreachable();
7132 }
7133 }
7134 break;
7135
4a657b0d
DK
7136 default:
7137 unsupported_reloc_local(object, r_type);
7138 break;
7139 }
7140}
7141
7142// Report an unsupported relocation against a global symbol.
7143
7144template<bool big_endian>
7145void
7146Target_arm<big_endian>::Scan::unsupported_reloc_global(
7147 Sized_relobj<32, big_endian>* object,
7148 unsigned int r_type,
7149 Symbol* gsym)
7150{
7151 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7152 object->name().c_str(), r_type, gsym->demangled_name().c_str());
7153}
7154
7155// Scan a relocation for a global symbol.
7156
7157template<bool big_endian>
7158inline void
ad0f2072 7159Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
2ea97941 7160 Layout* layout,
bec53400 7161 Target_arm* target,
4a657b0d 7162 Sized_relobj<32, big_endian>* object,
bec53400
DK
7163 unsigned int data_shndx,
7164 Output_section* output_section,
7165 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
7166 unsigned int r_type,
7167 Symbol* gsym)
7168{
c8761b9a
DK
7169 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
7170 // section. We check here to avoid creating a dynamic reloc against
7171 // _GLOBAL_OFFSET_TABLE_.
7172 if (!target->has_got_section()
7173 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7174 target->got_section(symtab, layout);
7175
a6d1ef57 7176 r_type = get_real_reloc_type(r_type);
4a657b0d
DK
7177 switch (r_type)
7178 {
7179 case elfcpp::R_ARM_NONE:
e4782e83
DK
7180 case elfcpp::R_ARM_V4BX:
7181 case elfcpp::R_ARM_GNU_VTENTRY:
7182 case elfcpp::R_ARM_GNU_VTINHERIT:
4a657b0d
DK
7183 break;
7184
bec53400 7185 case elfcpp::R_ARM_ABS32:
e4782e83
DK
7186 case elfcpp::R_ARM_ABS16:
7187 case elfcpp::R_ARM_ABS12:
7188 case elfcpp::R_ARM_THM_ABS5:
7189 case elfcpp::R_ARM_ABS8:
7190 case elfcpp::R_ARM_BASE_ABS:
7191 case elfcpp::R_ARM_MOVW_ABS_NC:
7192 case elfcpp::R_ARM_MOVT_ABS:
7193 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7194 case elfcpp::R_ARM_THM_MOVT_ABS:
be8fcb75 7195 case elfcpp::R_ARM_ABS32_NOI:
e4782e83 7196 // Absolute addressing relocations.
bec53400 7197 {
e4782e83
DK
7198 // Make a PLT entry if necessary.
7199 if (this->symbol_needs_plt_entry(gsym))
7200 {
7201 target->make_plt_entry(symtab, layout, gsym);
7202 // Since this is not a PC-relative relocation, we may be
7203 // taking the address of a function. In that case we need to
7204 // set the entry in the dynamic symbol table to the address of
7205 // the PLT entry.
7206 if (gsym->is_from_dynobj() && !parameters->options().shared())
7207 gsym->set_needs_dynsym_value();
7208 }
7209 // Make a dynamic relocation if necessary.
7210 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
7211 {
7212 if (gsym->may_need_copy_reloc())
7213 {
7214 target->copy_reloc(symtab, layout, object,
7215 data_shndx, output_section, gsym, reloc);
7216 }
7217 else if ((r_type == elfcpp::R_ARM_ABS32
7218 || r_type == elfcpp::R_ARM_ABS32_NOI)
7219 && gsym->can_use_relative_reloc(false))
7220 {
7221 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7222 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
7223 output_section, object,
7224 data_shndx, reloc.get_r_offset());
7225 }
7226 else
7227 {
7228 check_non_pic(object, r_type);
7229 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7230 rel_dyn->add_global(gsym, r_type, output_section, object,
7231 data_shndx, reloc.get_r_offset());
7232 }
7233 }
bec53400
DK
7234 }
7235 break;
7236
e4782e83
DK
7237 case elfcpp::R_ARM_GOTOFF32:
7238 case elfcpp::R_ARM_GOTOFF12:
7239 // We need a GOT section.
7240 target->got_section(symtab, layout);
7241 break;
7242
7243 case elfcpp::R_ARM_REL32:
7244 case elfcpp::R_ARM_LDR_PC_G0:
7245 case elfcpp::R_ARM_SBREL32:
7246 case elfcpp::R_ARM_THM_PC8:
7247 case elfcpp::R_ARM_BASE_PREL:
7248 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7249 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7250 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
c2a122b6
ILT
7251 case elfcpp::R_ARM_MOVW_PREL_NC:
7252 case elfcpp::R_ARM_MOVT_PREL:
7253 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7254 case elfcpp::R_ARM_THM_MOVT_PREL:
11b861d5 7255 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
e4782e83
DK
7256 case elfcpp::R_ARM_THM_PC12:
7257 case elfcpp::R_ARM_REL32_NOI:
b10d2873
ILT
7258 case elfcpp::R_ARM_ALU_PC_G0_NC:
7259 case elfcpp::R_ARM_ALU_PC_G0:
7260 case elfcpp::R_ARM_ALU_PC_G1_NC:
7261 case elfcpp::R_ARM_ALU_PC_G1:
7262 case elfcpp::R_ARM_ALU_PC_G2:
e4782e83
DK
7263 case elfcpp::R_ARM_LDR_PC_G1:
7264 case elfcpp::R_ARM_LDR_PC_G2:
7265 case elfcpp::R_ARM_LDRS_PC_G0:
7266 case elfcpp::R_ARM_LDRS_PC_G1:
7267 case elfcpp::R_ARM_LDRS_PC_G2:
7268 case elfcpp::R_ARM_LDC_PC_G0:
7269 case elfcpp::R_ARM_LDC_PC_G1:
7270 case elfcpp::R_ARM_LDC_PC_G2:
b10d2873
ILT
7271 case elfcpp::R_ARM_ALU_SB_G0_NC:
7272 case elfcpp::R_ARM_ALU_SB_G0:
7273 case elfcpp::R_ARM_ALU_SB_G1_NC:
7274 case elfcpp::R_ARM_ALU_SB_G1:
7275 case elfcpp::R_ARM_ALU_SB_G2:
b10d2873
ILT
7276 case elfcpp::R_ARM_LDR_SB_G0:
7277 case elfcpp::R_ARM_LDR_SB_G1:
7278 case elfcpp::R_ARM_LDR_SB_G2:
b10d2873
ILT
7279 case elfcpp::R_ARM_LDRS_SB_G0:
7280 case elfcpp::R_ARM_LDRS_SB_G1:
7281 case elfcpp::R_ARM_LDRS_SB_G2:
b10d2873
ILT
7282 case elfcpp::R_ARM_LDC_SB_G0:
7283 case elfcpp::R_ARM_LDC_SB_G1:
7284 case elfcpp::R_ARM_LDC_SB_G2:
e4782e83
DK
7285 case elfcpp::R_ARM_MOVW_BREL_NC:
7286 case elfcpp::R_ARM_MOVT_BREL:
7287 case elfcpp::R_ARM_MOVW_BREL:
7288 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7289 case elfcpp::R_ARM_THM_MOVT_BREL:
7290 case elfcpp::R_ARM_THM_MOVW_BREL:
7291 // Relative addressing relocations.
bec53400
DK
7292 {
7293 // Make a dynamic relocation if necessary.
7294 int flags = Symbol::NON_PIC_REF;
7295 if (gsym->needs_dynamic_reloc(flags))
7296 {
7297 if (target->may_need_copy_reloc(gsym))
7298 {
2ea97941 7299 target->copy_reloc(symtab, layout, object,
bec53400
DK
7300 data_shndx, output_section, gsym, reloc);
7301 }
7302 else
7303 {
7304 check_non_pic(object, r_type);
2ea97941 7305 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
bec53400
DK
7306 rel_dyn->add_global(gsym, r_type, output_section, object,
7307 data_shndx, reloc.get_r_offset());
7308 }
7309 }
7310 }
7311 break;
7312
e4782e83 7313 case elfcpp::R_ARM_PC24:
f4e5969c 7314 case elfcpp::R_ARM_THM_CALL:
bec53400 7315 case elfcpp::R_ARM_PLT32:
e4782e83
DK
7316 case elfcpp::R_ARM_CALL:
7317 case elfcpp::R_ARM_JUMP24:
7318 case elfcpp::R_ARM_THM_JUMP24:
7319 case elfcpp::R_ARM_SBREL31:
c9a2c125 7320 case elfcpp::R_ARM_PREL31:
e4782e83
DK
7321 case elfcpp::R_ARM_THM_JUMP19:
7322 case elfcpp::R_ARM_THM_JUMP6:
7323 case elfcpp::R_ARM_THM_JUMP11:
7324 case elfcpp::R_ARM_THM_JUMP8:
7325 // All the relocation above are branches except for the PREL31 ones.
7326 // A PREL31 relocation can point to a personality function in a shared
7327 // library. In that case we want to use a PLT because we want to
7328 // call the personality routine and the dyanmic linkers we care about
7329 // do not support dynamic PREL31 relocations. An REL31 relocation may
7330 // point to a function whose unwinding behaviour is being described but
7331 // we will not mistakenly generate a PLT for that because we should use
7332 // a local section symbol.
7333
bec53400
DK
7334 // If the symbol is fully resolved, this is just a relative
7335 // local reloc. Otherwise we need a PLT entry.
7336 if (gsym->final_value_is_known())
7337 break;
7338 // If building a shared library, we can also skip the PLT entry
7339 // if the symbol is defined in the output file and is protected
7340 // or hidden.
7341 if (gsym->is_defined()
7342 && !gsym->is_from_dynobj()
7343 && !gsym->is_preemptible())
7344 break;
2ea97941 7345 target->make_plt_entry(symtab, layout, gsym);
bec53400
DK
7346 break;
7347
bec53400 7348 case elfcpp::R_ARM_GOT_BREL:
e4782e83 7349 case elfcpp::R_ARM_GOT_ABS:
7f5309a5 7350 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
7351 {
7352 // The symbol requires a GOT entry.
7353 Output_data_got<32, big_endian>* got =
2ea97941 7354 target->got_section(symtab, layout);
bec53400
DK
7355 if (gsym->final_value_is_known())
7356 got->add_global(gsym, GOT_TYPE_STANDARD);
7357 else
7358 {
7359 // If this symbol is not fully resolved, we need to add a
7360 // GOT entry with a dynamic relocation.
2ea97941 7361 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
bec53400
DK
7362 if (gsym->is_from_dynobj()
7363 || gsym->is_undefined()
7364 || gsym->is_preemptible())
7365 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
7366 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
7367 else
7368 {
7369 if (got->add_global(gsym, GOT_TYPE_STANDARD))
7370 rel_dyn->add_global_relative(
7371 gsym, elfcpp::R_ARM_RELATIVE, got,
7372 gsym->got_offset(GOT_TYPE_STANDARD));
7373 }
7374 }
7375 }
7376 break;
7377
7378 case elfcpp::R_ARM_TARGET1:
e4782e83
DK
7379 case elfcpp::R_ARM_TARGET2:
7380 // These should have been mapped to other types already.
bec53400
DK
7381 // Fall through.
7382 case elfcpp::R_ARM_COPY:
7383 case elfcpp::R_ARM_GLOB_DAT:
7384 case elfcpp::R_ARM_JUMP_SLOT:
7385 case elfcpp::R_ARM_RELATIVE:
7386 // These are relocations which should only be seen by the
7387 // dynamic linker, and should never be seen here.
7388 gold_error(_("%s: unexpected reloc %u in object file"),
7389 object->name().c_str(), r_type);
7390 break;
7391
f96accdf
DK
7392 // These are initial tls relocs, which are expected when
7393 // linking.
7394 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7395 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7396 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7397 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7398 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7399 {
7400 const bool is_final = gsym->final_value_is_known();
7401 const tls::Tls_optimization optimized_type
7402 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
7403 switch (r_type)
7404 {
7405 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7406 if (optimized_type == tls::TLSOPT_NONE)
7407 {
7408 // Create a pair of GOT entries for the module index and
7409 // dtv-relative offset.
7410 Output_data_got<32, big_endian>* got
7411 = target->got_section(symtab, layout);
7412 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
7413 target->rel_dyn_section(layout),
7414 elfcpp::R_ARM_TLS_DTPMOD32,
7415 elfcpp::R_ARM_TLS_DTPOFF32);
7416 }
7417 else
7418 // FIXME: TLS optimization not supported yet.
7419 gold_unreachable();
7420 break;
7421
7422 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7423 if (optimized_type == tls::TLSOPT_NONE)
7424 {
7425 // Create a GOT entry for the module index.
7426 target->got_mod_index_entry(symtab, layout, object);
7427 }
7428 else
7429 // FIXME: TLS optimization not supported yet.
7430 gold_unreachable();
7431 break;
7432
7433 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7434 break;
7435
7436 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7437 layout->set_has_static_tls();
7438 if (optimized_type == tls::TLSOPT_NONE)
7439 {
7440 // Create a GOT entry for the tp-relative offset.
7441 Output_data_got<32, big_endian>* got
7442 = target->got_section(symtab, layout);
7443 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
7444 target->rel_dyn_section(layout),
7445 elfcpp::R_ARM_TLS_TPOFF32);
7446 }
7447 else
7448 // FIXME: TLS optimization not supported yet.
7449 gold_unreachable();
7450 break;
7451
7452 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7453 layout->set_has_static_tls();
7454 if (parameters->options().shared())
7455 {
7456 // We need to create a dynamic relocation.
7457 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7458 rel_dyn->add_global(gsym, elfcpp::R_ARM_TLS_TPOFF32,
7459 output_section, object,
7460 data_shndx, reloc.get_r_offset());
7461 }
7462 break;
7463
7464 default:
7465 gold_unreachable();
7466 }
7467 }
7468 break;
7469
4a657b0d
DK
7470 default:
7471 unsupported_reloc_global(object, r_type, gsym);
7472 break;
7473 }
7474}
7475
7476// Process relocations for gc.
7477
7478template<bool big_endian>
7479void
ad0f2072 7480Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
2ea97941 7481 Layout* layout,
4a657b0d
DK
7482 Sized_relobj<32, big_endian>* object,
7483 unsigned int data_shndx,
7484 unsigned int,
7485 const unsigned char* prelocs,
7486 size_t reloc_count,
7487 Output_section* output_section,
7488 bool needs_special_offset_handling,
7489 size_t local_symbol_count,
7490 const unsigned char* plocal_symbols)
7491{
7492 typedef Target_arm<big_endian> Arm;
2ea97941 7493 typedef typename Target_arm<big_endian>::Scan Scan;
4a657b0d 7494
2ea97941 7495 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
4a657b0d 7496 symtab,
2ea97941 7497 layout,
4a657b0d
DK
7498 this,
7499 object,
7500 data_shndx,
7501 prelocs,
7502 reloc_count,
7503 output_section,
7504 needs_special_offset_handling,
7505 local_symbol_count,
7506 plocal_symbols);
7507}
7508
7509// Scan relocations for a section.
7510
7511template<bool big_endian>
7512void
ad0f2072 7513Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
2ea97941 7514 Layout* layout,
4a657b0d
DK
7515 Sized_relobj<32, big_endian>* object,
7516 unsigned int data_shndx,
7517 unsigned int sh_type,
7518 const unsigned char* prelocs,
7519 size_t reloc_count,
7520 Output_section* output_section,
7521 bool needs_special_offset_handling,
7522 size_t local_symbol_count,
7523 const unsigned char* plocal_symbols)
7524{
2ea97941 7525 typedef typename Target_arm<big_endian>::Scan Scan;
4a657b0d
DK
7526 if (sh_type == elfcpp::SHT_RELA)
7527 {
7528 gold_error(_("%s: unsupported RELA reloc section"),
7529 object->name().c_str());
7530 return;
7531 }
7532
2ea97941 7533 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
4a657b0d 7534 symtab,
2ea97941 7535 layout,
4a657b0d
DK
7536 this,
7537 object,
7538 data_shndx,
7539 prelocs,
7540 reloc_count,
7541 output_section,
7542 needs_special_offset_handling,
7543 local_symbol_count,
7544 plocal_symbols);
7545}
7546
7547// Finalize the sections.
7548
7549template<bool big_endian>
7550void
d5b40221 7551Target_arm<big_endian>::do_finalize_sections(
2ea97941 7552 Layout* layout,
f59f41f3
DK
7553 const Input_objects* input_objects,
7554 Symbol_table* symtab)
4a657b0d 7555{
d5b40221
DK
7556 // Merge processor-specific flags.
7557 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
7558 p != input_objects->relobj_end();
7559 ++p)
7560 {
7561 Arm_relobj<big_endian>* arm_relobj =
7562 Arm_relobj<big_endian>::as_arm_relobj(*p);
7563 this->merge_processor_specific_flags(
7564 arm_relobj->name(),
7565 arm_relobj->processor_specific_flags());
a0351a69
DK
7566 this->merge_object_attributes(arm_relobj->name().c_str(),
7567 arm_relobj->attributes_section_data());
7568
d5b40221
DK
7569 }
7570
7571 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
7572 p != input_objects->dynobj_end();
7573 ++p)
7574 {
7575 Arm_dynobj<big_endian>* arm_dynobj =
7576 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
7577 this->merge_processor_specific_flags(
7578 arm_dynobj->name(),
7579 arm_dynobj->processor_specific_flags());
a0351a69
DK
7580 this->merge_object_attributes(arm_dynobj->name().c_str(),
7581 arm_dynobj->attributes_section_data());
d5b40221
DK
7582 }
7583
a0351a69 7584 // Check BLX use.
41263c05 7585 const Object_attribute* cpu_arch_attr =
a0351a69 7586 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
41263c05 7587 if (cpu_arch_attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
a0351a69
DK
7588 this->set_may_use_blx(true);
7589
41263c05
DK
7590 // Check if we need to use Cortex-A8 workaround.
7591 if (parameters->options().user_set_fix_cortex_a8())
7592 this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
7593 else
7594 {
7595 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
7596 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
7597 // profile.
7598 const Object_attribute* cpu_arch_profile_attr =
7599 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
7600 this->fix_cortex_a8_ =
7601 (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
7602 && (cpu_arch_profile_attr->int_value() == 'A'
7603 || cpu_arch_profile_attr->int_value() == 0));
7604 }
7605
a2162063
ILT
7606 // Check if we can use V4BX interworking.
7607 // The V4BX interworking stub contains BX instruction,
7608 // which is not specified for some profiles.
9b2fd367
DK
7609 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
7610 && !this->may_use_blx())
a2162063
ILT
7611 gold_error(_("unable to provide V4BX reloc interworking fix up; "
7612 "the target profile does not support BX instruction"));
7613
94cdfcff 7614 // Fill in some more dynamic tags.
ea715a34
ILT
7615 const Reloc_section* rel_plt = (this->plt_ == NULL
7616 ? NULL
7617 : this->plt_->rel_plt());
7618 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
612a8d3d 7619 this->rel_dyn_, true, false);
94cdfcff
DK
7620
7621 // Emit any relocs we saved in an attempt to avoid generating COPY
7622 // relocs.
7623 if (this->copy_relocs_.any_saved_relocs())
2ea97941 7624 this->copy_relocs_.emit(this->rel_dyn_section(layout));
11af873f 7625
f59f41f3 7626 // Handle the .ARM.exidx section.
2ea97941 7627 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
f59f41f3
DK
7628 if (exidx_section != NULL
7629 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
11af873f
DK
7630 && !parameters->options().relocatable())
7631 {
f59f41f3 7632 // Create __exidx_start and __exdix_end symbols.
99fff23b
ILT
7633 symtab->define_in_output_data("__exidx_start", NULL,
7634 Symbol_table::PREDEFINED,
7635 exidx_section, 0, 0, elfcpp::STT_OBJECT,
a0351a69 7636 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
99e5bff2 7637 false, true);
99fff23b
ILT
7638 symtab->define_in_output_data("__exidx_end", NULL,
7639 Symbol_table::PREDEFINED,
7640 exidx_section, 0, 0, elfcpp::STT_OBJECT,
a0351a69 7641 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
99e5bff2 7642 true, true);
11af873f 7643
f59f41f3
DK
7644 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
7645 // the .ARM.exidx section.
2ea97941 7646 if (!layout->script_options()->saw_phdrs_clause())
11af873f 7647 {
2ea97941 7648 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
11af873f
DK
7649 == NULL);
7650 Output_segment* exidx_segment =
2ea97941 7651 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
f5c870d2
ILT
7652 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
7653 false);
11af873f
DK
7654 }
7655 }
a0351a69
DK
7656
7657 // Create an .ARM.attributes section if there is not one already.
2ea97941 7658 Output_attributes_section_data* attributes_section =
a0351a69 7659 new Output_attributes_section_data(*this->attributes_section_data_);
2ea97941
ILT
7660 layout->add_output_section_data(".ARM.attributes",
7661 elfcpp::SHT_ARM_ATTRIBUTES, 0,
1a2dff53
ILT
7662 attributes_section, false, false, false,
7663 false);
4a657b0d
DK
7664}
7665
bec53400
DK
7666// Return whether a direct absolute static relocation needs to be applied.
7667// In cases where Scan::local() or Scan::global() has created
7668// a dynamic relocation other than R_ARM_RELATIVE, the addend
7669// of the relocation is carried in the data, and we must not
7670// apply the static relocation.
7671
7672template<bool big_endian>
7673inline bool
7674Target_arm<big_endian>::Relocate::should_apply_static_reloc(
7675 const Sized_symbol<32>* gsym,
7676 int ref_flags,
7677 bool is_32bit,
7678 Output_section* output_section)
7679{
7680 // If the output section is not allocated, then we didn't call
7681 // scan_relocs, we didn't create a dynamic reloc, and we must apply
7682 // the reloc here.
7683 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
7684 return true;
7685
7686 // For local symbols, we will have created a non-RELATIVE dynamic
7687 // relocation only if (a) the output is position independent,
7688 // (b) the relocation is absolute (not pc- or segment-relative), and
7689 // (c) the relocation is not 32 bits wide.
7690 if (gsym == NULL)
7691 return !(parameters->options().output_is_position_independent()
7692 && (ref_flags & Symbol::ABSOLUTE_REF)
7693 && !is_32bit);
7694
7695 // For global symbols, we use the same helper routines used in the
7696 // scan pass. If we did not create a dynamic relocation, or if we
7697 // created a RELATIVE dynamic relocation, we should apply the static
7698 // relocation.
7699 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
7700 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
7701 && gsym->can_use_relative_reloc(ref_flags
7702 & Symbol::FUNCTION_CALL);
7703 return !has_dyn || is_rel;
7704}
7705
4a657b0d
DK
7706// Perform a relocation.
7707
7708template<bool big_endian>
7709inline bool
7710Target_arm<big_endian>::Relocate::relocate(
c121c671
DK
7711 const Relocate_info<32, big_endian>* relinfo,
7712 Target_arm* target,
7713 Output_section *output_section,
7714 size_t relnum,
7715 const elfcpp::Rel<32, big_endian>& rel,
4a657b0d 7716 unsigned int r_type,
c121c671
DK
7717 const Sized_symbol<32>* gsym,
7718 const Symbol_value<32>* psymval,
7719 unsigned char* view,
ebabffbd 7720 Arm_address address,
f96accdf 7721 section_size_type view_size)
4a657b0d 7722{
c121c671
DK
7723 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
7724
a6d1ef57 7725 r_type = get_real_reloc_type(r_type);
5c57f1be
DK
7726 const Arm_reloc_property* reloc_property =
7727 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
7728 if (reloc_property == NULL)
7729 {
7730 std::string reloc_name =
7731 arm_reloc_property_table->reloc_name_in_error_message(r_type);
7732 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
7733 _("cannot relocate %s in object file"),
7734 reloc_name.c_str());
7735 return true;
7736 }
c121c671 7737
2daedcd6
DK
7738 const Arm_relobj<big_endian>* object =
7739 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
c121c671 7740
2daedcd6
DK
7741 // If the final branch target of a relocation is THUMB instruction, this
7742 // is 1. Otherwise it is 0.
7743 Arm_address thumb_bit = 0;
c121c671 7744 Symbol_value<32> symval;
d204b6e9 7745 bool is_weakly_undefined_without_plt = false;
2daedcd6 7746 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
c121c671 7747 {
2daedcd6
DK
7748 if (gsym != NULL)
7749 {
7750 // This is a global symbol. Determine if we use PLT and if the
7751 // final target is THUMB.
7752 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
7753 {
7754 // This uses a PLT, change the symbol value.
7755 symval.set_output_value(target->plt_section()->address()
7756 + gsym->plt_offset());
7757 psymval = &symval;
7758 }
d204b6e9
DK
7759 else if (gsym->is_weak_undefined())
7760 {
7761 // This is a weakly undefined symbol and we do not use PLT
7762 // for this relocation. A branch targeting this symbol will
7763 // be converted into an NOP.
7764 is_weakly_undefined_without_plt = true;
7765 }
2daedcd6
DK
7766 else
7767 {
7768 // Set thumb bit if symbol:
7769 // -Has type STT_ARM_TFUNC or
7770 // -Has type STT_FUNC, is defined and with LSB in value set.
7771 thumb_bit =
7772 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
7773 || (gsym->type() == elfcpp::STT_FUNC
7774 && !gsym->is_undefined()
7775 && ((psymval->value(object, 0) & 1) != 0)))
7776 ? 1
7777 : 0);
7778 }
7779 }
7780 else
7781 {
7782 // This is a local symbol. Determine if the final target is THUMB.
7783 // We saved this information when all the local symbols were read.
7784 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
7785 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
7786 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
7787 }
7788 }
7789 else
7790 {
7791 // This is a fake relocation synthesized for a stub. It does not have
7792 // a real symbol. We just look at the LSB of the symbol value to
7793 // determine if the target is THUMB or not.
7794 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
c121c671
DK
7795 }
7796
2daedcd6
DK
7797 // Strip LSB if this points to a THUMB target.
7798 if (thumb_bit != 0
5c57f1be 7799 && reloc_property->uses_thumb_bit()
2daedcd6
DK
7800 && ((psymval->value(object, 0) & 1) != 0))
7801 {
7802 Arm_address stripped_value =
7803 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
7804 symval.set_output_value(stripped_value);
7805 psymval = &symval;
7806 }
7807
c121c671
DK
7808 // Get the GOT offset if needed.
7809 // The GOT pointer points to the end of the GOT section.
7810 // We need to subtract the size of the GOT section to get
7811 // the actual offset to use in the relocation.
7812 bool have_got_offset = false;
7813 unsigned int got_offset = 0;
7814 switch (r_type)
7815 {
7816 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 7817 case elfcpp::R_ARM_GOT_PREL:
c121c671
DK
7818 if (gsym != NULL)
7819 {
7820 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
7821 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
7822 - target->got_size());
7823 }
7824 else
7825 {
7826 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
7827 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
7828 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
7829 - target->got_size());
7830 }
7831 have_got_offset = true;
7832 break;
7833
7834 default:
7835 break;
7836 }
7837
d204b6e9
DK
7838 // To look up relocation stubs, we need to pass the symbol table index of
7839 // a local symbol.
7840 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
7841
b10d2873
ILT
7842 // Get the addressing origin of the output segment defining the
7843 // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
7844 Arm_address sym_origin = 0;
5c57f1be 7845 if (reloc_property->uses_symbol_base())
b10d2873
ILT
7846 {
7847 if (r_type == elfcpp::R_ARM_BASE_ABS && gsym == NULL)
7848 // R_ARM_BASE_ABS with the NULL symbol will give the
7849 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
7850 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
7851 sym_origin = target->got_plt_section()->address();
7852 else if (gsym == NULL)
7853 sym_origin = 0;
7854 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
7855 sym_origin = gsym->output_segment()->vaddr();
7856 else if (gsym->source() == Symbol::IN_OUTPUT_DATA)
7857 sym_origin = gsym->output_data()->address();
7858
7859 // TODO: Assumes the segment base to be zero for the global symbols
7860 // till the proper support for the segment-base-relative addressing
7861 // will be implemented. This is consistent with GNU ld.
7862 }
7863
5c57f1be
DK
7864 // For relative addressing relocation, find out the relative address base.
7865 Arm_address relative_address_base = 0;
7866 switch(reloc_property->relative_address_base())
7867 {
7868 case Arm_reloc_property::RAB_NONE:
f96accdf
DK
7869 // Relocations with relative address bases RAB_TLS and RAB_tp are
7870 // handled by relocate_tls. So we do not need to do anything here.
7871 case Arm_reloc_property::RAB_TLS:
7872 case Arm_reloc_property::RAB_tp:
5c57f1be
DK
7873 break;
7874 case Arm_reloc_property::RAB_B_S:
7875 relative_address_base = sym_origin;
7876 break;
7877 case Arm_reloc_property::RAB_GOT_ORG:
7878 relative_address_base = target->got_plt_section()->address();
7879 break;
7880 case Arm_reloc_property::RAB_P:
7881 relative_address_base = address;
7882 break;
7883 case Arm_reloc_property::RAB_Pa:
7884 relative_address_base = address & 0xfffffffcU;
7885 break;
7886 default:
7887 gold_unreachable();
7888 }
7889
c121c671
DK
7890 typename Arm_relocate_functions::Status reloc_status =
7891 Arm_relocate_functions::STATUS_OKAY;
5c57f1be 7892 bool check_overflow = reloc_property->checks_overflow();
4a657b0d
DK
7893 switch (r_type)
7894 {
7895 case elfcpp::R_ARM_NONE:
7896 break;
7897
5e445df6
ILT
7898 case elfcpp::R_ARM_ABS8:
7899 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
7900 output_section))
be8fcb75
ILT
7901 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
7902 break;
7903
7904 case elfcpp::R_ARM_ABS12:
7905 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
7906 output_section))
7907 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
7908 break;
7909
7910 case elfcpp::R_ARM_ABS16:
7911 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
7912 output_section))
7913 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5e445df6
ILT
7914 break;
7915
c121c671
DK
7916 case elfcpp::R_ARM_ABS32:
7917 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
7918 output_section))
7919 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
2daedcd6 7920 thumb_bit);
c121c671
DK
7921 break;
7922
be8fcb75
ILT
7923 case elfcpp::R_ARM_ABS32_NOI:
7924 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
7925 output_section))
7926 // No thumb bit for this relocation: (S + A)
7927 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
f4e5969c 7928 0);
be8fcb75
ILT
7929 break;
7930
fd3c5f0b 7931 case elfcpp::R_ARM_MOVW_ABS_NC:
e4782e83 7932 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
fd3c5f0b 7933 output_section))
5c57f1be
DK
7934 reloc_status = Arm_relocate_functions::movw(view, object, psymval,
7935 0, thumb_bit,
7936 check_overflow);
fd3c5f0b
ILT
7937 break;
7938
7939 case elfcpp::R_ARM_MOVT_ABS:
e4782e83 7940 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
fd3c5f0b 7941 output_section))
5c57f1be 7942 reloc_status = Arm_relocate_functions::movt(view, object, psymval, 0);
fd3c5f0b
ILT
7943 break;
7944
7945 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
e4782e83 7946 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
fd3c5f0b 7947 output_section))
5c57f1be
DK
7948 reloc_status = Arm_relocate_functions::thm_movw(view, object, psymval,
7949 0, thumb_bit, false);
fd3c5f0b
ILT
7950 break;
7951
7952 case elfcpp::R_ARM_THM_MOVT_ABS:
e4782e83 7953 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
fd3c5f0b 7954 output_section))
5c57f1be
DK
7955 reloc_status = Arm_relocate_functions::thm_movt(view, object,
7956 psymval, 0);
fd3c5f0b
ILT
7957 break;
7958
c2a122b6 7959 case elfcpp::R_ARM_MOVW_PREL_NC:
02961d7e 7960 case elfcpp::R_ARM_MOVW_BREL_NC:
02961d7e 7961 case elfcpp::R_ARM_MOVW_BREL:
5c57f1be
DK
7962 reloc_status =
7963 Arm_relocate_functions::movw(view, object, psymval,
7964 relative_address_base, thumb_bit,
7965 check_overflow);
c2a122b6
ILT
7966 break;
7967
7968 case elfcpp::R_ARM_MOVT_PREL:
02961d7e 7969 case elfcpp::R_ARM_MOVT_BREL:
5c57f1be
DK
7970 reloc_status =
7971 Arm_relocate_functions::movt(view, object, psymval,
7972 relative_address_base);
c2a122b6
ILT
7973 break;
7974
7975 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
02961d7e 7976 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
02961d7e 7977 case elfcpp::R_ARM_THM_MOVW_BREL:
5c57f1be
DK
7978 reloc_status =
7979 Arm_relocate_functions::thm_movw(view, object, psymval,
7980 relative_address_base,
7981 thumb_bit, check_overflow);
c2a122b6
ILT
7982 break;
7983
7984 case elfcpp::R_ARM_THM_MOVT_PREL:
02961d7e 7985 case elfcpp::R_ARM_THM_MOVT_BREL:
5c57f1be
DK
7986 reloc_status =
7987 Arm_relocate_functions::thm_movt(view, object, psymval,
7988 relative_address_base);
02961d7e 7989 break;
5c57f1be 7990
c121c671
DK
7991 case elfcpp::R_ARM_REL32:
7992 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2daedcd6 7993 address, thumb_bit);
c121c671
DK
7994 break;
7995
be8fcb75
ILT
7996 case elfcpp::R_ARM_THM_ABS5:
7997 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
7998 output_section))
7999 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
8000 break;
8001
1521477a 8002 // Thumb long branches.
c121c671 8003 case elfcpp::R_ARM_THM_CALL:
51938283 8004 case elfcpp::R_ARM_THM_XPC22:
1521477a 8005 case elfcpp::R_ARM_THM_JUMP24:
51938283 8006 reloc_status =
1521477a
DK
8007 Arm_relocate_functions::thumb_branch_common(
8008 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8009 thumb_bit, is_weakly_undefined_without_plt);
51938283
DK
8010 break;
8011
c121c671
DK
8012 case elfcpp::R_ARM_GOTOFF32:
8013 {
ebabffbd 8014 Arm_address got_origin;
c121c671
DK
8015 got_origin = target->got_plt_section()->address();
8016 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2daedcd6 8017 got_origin, thumb_bit);
c121c671
DK
8018 }
8019 break;
8020
8021 case elfcpp::R_ARM_BASE_PREL:
b10d2873
ILT
8022 gold_assert(gsym != NULL);
8023 reloc_status =
8024 Arm_relocate_functions::base_prel(view, sym_origin, address);
c121c671
DK
8025 break;
8026
be8fcb75
ILT
8027 case elfcpp::R_ARM_BASE_ABS:
8028 {
e4782e83 8029 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
be8fcb75
ILT
8030 output_section))
8031 break;
8032
b10d2873 8033 reloc_status = Arm_relocate_functions::base_abs(view, sym_origin);
be8fcb75
ILT
8034 }
8035 break;
8036
c121c671
DK
8037 case elfcpp::R_ARM_GOT_BREL:
8038 gold_assert(have_got_offset);
8039 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
8040 break;
8041
7f5309a5
ILT
8042 case elfcpp::R_ARM_GOT_PREL:
8043 gold_assert(have_got_offset);
8044 // Get the address origin for GOT PLT, which is allocated right
8045 // after the GOT section, to calculate an absolute address of
8046 // the symbol GOT entry (got_origin + got_offset).
ebabffbd 8047 Arm_address got_origin;
7f5309a5
ILT
8048 got_origin = target->got_plt_section()->address();
8049 reloc_status = Arm_relocate_functions::got_prel(view,
8050 got_origin + got_offset,
8051 address);
8052 break;
8053
c121c671 8054 case elfcpp::R_ARM_PLT32:
1521477a
DK
8055 case elfcpp::R_ARM_CALL:
8056 case elfcpp::R_ARM_JUMP24:
8057 case elfcpp::R_ARM_XPC25:
c121c671
DK
8058 gold_assert(gsym == NULL
8059 || gsym->has_plt_offset()
8060 || gsym->final_value_is_known()
8061 || (gsym->is_defined()
8062 && !gsym->is_from_dynobj()
8063 && !gsym->is_preemptible()));
d204b6e9 8064 reloc_status =
1521477a
DK
8065 Arm_relocate_functions::arm_branch_common(
8066 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8067 thumb_bit, is_weakly_undefined_without_plt);
51938283
DK
8068 break;
8069
41263c05
DK
8070 case elfcpp::R_ARM_THM_JUMP19:
8071 reloc_status =
8072 Arm_relocate_functions::thm_jump19(view, object, psymval, address,
8073 thumb_bit);
8074 break;
8075
800d0f56
ILT
8076 case elfcpp::R_ARM_THM_JUMP6:
8077 reloc_status =
8078 Arm_relocate_functions::thm_jump6(view, object, psymval, address);
8079 break;
8080
8081 case elfcpp::R_ARM_THM_JUMP8:
8082 reloc_status =
8083 Arm_relocate_functions::thm_jump8(view, object, psymval, address);
8084 break;
8085
8086 case elfcpp::R_ARM_THM_JUMP11:
8087 reloc_status =
8088 Arm_relocate_functions::thm_jump11(view, object, psymval, address);
8089 break;
8090
c121c671
DK
8091 case elfcpp::R_ARM_PREL31:
8092 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
2daedcd6 8093 address, thumb_bit);
c121c671
DK
8094 break;
8095
a2162063 8096 case elfcpp::R_ARM_V4BX:
9b2fd367
DK
8097 if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
8098 {
8099 const bool is_v4bx_interworking =
8100 (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
8101 reloc_status =
8102 Arm_relocate_functions::v4bx(relinfo, view, object, address,
8103 is_v4bx_interworking);
8104 }
a2162063
ILT
8105 break;
8106
11b861d5
DK
8107 case elfcpp::R_ARM_THM_PC8:
8108 reloc_status =
8109 Arm_relocate_functions::thm_pc8(view, object, psymval, address);
8110 break;
8111
8112 case elfcpp::R_ARM_THM_PC12:
8113 reloc_status =
8114 Arm_relocate_functions::thm_pc12(view, object, psymval, address);
8115 break;
8116
8117 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8118 reloc_status =
8119 Arm_relocate_functions::thm_alu11(view, object, psymval, address,
8120 thumb_bit);
8121 break;
8122
b10d2873 8123 case elfcpp::R_ARM_ALU_PC_G0_NC:
b10d2873 8124 case elfcpp::R_ARM_ALU_PC_G0:
b10d2873 8125 case elfcpp::R_ARM_ALU_PC_G1_NC:
b10d2873 8126 case elfcpp::R_ARM_ALU_PC_G1:
b10d2873 8127 case elfcpp::R_ARM_ALU_PC_G2:
b10d2873 8128 case elfcpp::R_ARM_ALU_SB_G0_NC:
b10d2873 8129 case elfcpp::R_ARM_ALU_SB_G0:
b10d2873 8130 case elfcpp::R_ARM_ALU_SB_G1_NC:
b10d2873 8131 case elfcpp::R_ARM_ALU_SB_G1:
b10d2873
ILT
8132 case elfcpp::R_ARM_ALU_SB_G2:
8133 reloc_status =
5c57f1be
DK
8134 Arm_relocate_functions::arm_grp_alu(view, object, psymval,
8135 reloc_property->group_index(),
8136 relative_address_base,
8137 thumb_bit, check_overflow);
b10d2873
ILT
8138 break;
8139
8140 case elfcpp::R_ARM_LDR_PC_G0:
b10d2873 8141 case elfcpp::R_ARM_LDR_PC_G1:
b10d2873 8142 case elfcpp::R_ARM_LDR_PC_G2:
b10d2873 8143 case elfcpp::R_ARM_LDR_SB_G0:
b10d2873 8144 case elfcpp::R_ARM_LDR_SB_G1:
b10d2873
ILT
8145 case elfcpp::R_ARM_LDR_SB_G2:
8146 reloc_status =
5c57f1be
DK
8147 Arm_relocate_functions::arm_grp_ldr(view, object, psymval,
8148 reloc_property->group_index(),
8149 relative_address_base);
b10d2873
ILT
8150 break;
8151
8152 case elfcpp::R_ARM_LDRS_PC_G0:
b10d2873 8153 case elfcpp::R_ARM_LDRS_PC_G1:
b10d2873 8154 case elfcpp::R_ARM_LDRS_PC_G2:
b10d2873 8155 case elfcpp::R_ARM_LDRS_SB_G0:
b10d2873 8156 case elfcpp::R_ARM_LDRS_SB_G1:
b10d2873
ILT
8157 case elfcpp::R_ARM_LDRS_SB_G2:
8158 reloc_status =
5c57f1be
DK
8159 Arm_relocate_functions::arm_grp_ldrs(view, object, psymval,
8160 reloc_property->group_index(),
8161 relative_address_base);
b10d2873
ILT
8162 break;
8163
8164 case elfcpp::R_ARM_LDC_PC_G0:
b10d2873 8165 case elfcpp::R_ARM_LDC_PC_G1:
b10d2873 8166 case elfcpp::R_ARM_LDC_PC_G2:
b10d2873 8167 case elfcpp::R_ARM_LDC_SB_G0:
b10d2873 8168 case elfcpp::R_ARM_LDC_SB_G1:
b10d2873
ILT
8169 case elfcpp::R_ARM_LDC_SB_G2:
8170 reloc_status =
5c57f1be
DK
8171 Arm_relocate_functions::arm_grp_ldc(view, object, psymval,
8172 reloc_property->group_index(),
8173 relative_address_base);
c121c671
DK
8174 break;
8175
f96accdf
DK
8176 // These are initial tls relocs, which are expected when
8177 // linking.
8178 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8179 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8180 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8181 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8182 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8183 reloc_status =
8184 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
8185 view, address, view_size);
8186 break;
8187
c121c671 8188 default:
5c57f1be 8189 gold_unreachable();
c121c671
DK
8190 }
8191
8192 // Report any errors.
8193 switch (reloc_status)
8194 {
8195 case Arm_relocate_functions::STATUS_OKAY:
8196 break;
8197 case Arm_relocate_functions::STATUS_OVERFLOW:
8198 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8199 _("relocation overflow in relocation %u"),
8200 r_type);
8201 break;
8202 case Arm_relocate_functions::STATUS_BAD_RELOC:
8203 gold_error_at_location(
8204 relinfo,
8205 relnum,
8206 rel.get_r_offset(),
8207 _("unexpected opcode while processing relocation %u"),
8208 r_type);
8209 break;
4a657b0d
DK
8210 default:
8211 gold_unreachable();
8212 }
8213
8214 return true;
8215}
8216
f96accdf
DK
8217// Perform a TLS relocation.
8218
8219template<bool big_endian>
8220inline typename Arm_relocate_functions<big_endian>::Status
8221Target_arm<big_endian>::Relocate::relocate_tls(
8222 const Relocate_info<32, big_endian>* relinfo,
8223 Target_arm<big_endian>* target,
8224 size_t relnum,
8225 const elfcpp::Rel<32, big_endian>& rel,
8226 unsigned int r_type,
8227 const Sized_symbol<32>* gsym,
8228 const Symbol_value<32>* psymval,
8229 unsigned char* view,
8230 elfcpp::Elf_types<32>::Elf_Addr,
8231 section_size_type /*view_size*/ )
8232{
8233 typedef Arm_relocate_functions<big_endian> ArmRelocFuncs;
8234 Output_segment* tls_segment = relinfo->layout->tls_segment();
8235
8236 const Sized_relobj<32, big_endian>* object = relinfo->object;
8237
8238 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
8239
8240 const bool is_final = (gsym == NULL
8241 ? !parameters->options().shared()
8242 : gsym->final_value_is_known());
8243 const tls::Tls_optimization optimized_type
8244 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
8245 switch (r_type)
8246 {
8247 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8248 {
8249 unsigned int got_type = GOT_TYPE_TLS_PAIR;
8250 unsigned int got_offset;
8251 if (gsym != NULL)
8252 {
8253 gold_assert(gsym->has_got_offset(got_type));
8254 got_offset = gsym->got_offset(got_type) - target->got_size();
8255 }
8256 else
8257 {
8258 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8259 gold_assert(object->local_has_got_offset(r_sym, got_type));
8260 got_offset = (object->local_got_offset(r_sym, got_type)
8261 - target->got_size());
8262 }
8263 if (optimized_type == tls::TLSOPT_NONE)
8264 {
8265 // Relocate the field with the offset of the pair of GOT
8266 // entries.
8267 Relocate_functions<32, big_endian>::rel32(view, got_offset);
8268 return ArmRelocFuncs::STATUS_OKAY;
8269 }
8270 }
8271 break;
8272
8273 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8274 if (optimized_type == tls::TLSOPT_NONE)
8275 {
8276 // Relocate the field with the offset of the GOT entry for
8277 // the module index.
8278 unsigned int got_offset;
8279 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
8280 - target->got_size());
8281 Relocate_functions<32, big_endian>::rel32(view, got_offset);
8282 return ArmRelocFuncs::STATUS_OKAY;
8283 }
8284 break;
8285
8286 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8287 Relocate_functions<32, big_endian>::rel32(view, value);
8288 return ArmRelocFuncs::STATUS_OKAY;
8289
8290 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8291 if (optimized_type == tls::TLSOPT_NONE)
8292 {
8293 // Relocate the field with the offset of the GOT entry for
8294 // the tp-relative offset of the symbol.
8295 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
8296 unsigned int got_offset;
8297 if (gsym != NULL)
8298 {
8299 gold_assert(gsym->has_got_offset(got_type));
8300 got_offset = gsym->got_offset(got_type);
8301 }
8302 else
8303 {
8304 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8305 gold_assert(object->local_has_got_offset(r_sym, got_type));
8306 got_offset = object->local_got_offset(r_sym, got_type);
8307 }
8308 // All GOT offsets are relative to the end of the GOT.
8309 got_offset -= target->got_size();
8310 Relocate_functions<32, big_endian>::rel32(view, got_offset);
8311 return ArmRelocFuncs::STATUS_OKAY;
8312 }
8313 break;
8314
8315 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8316 // If we're creating a shared library, a dynamic relocation will
8317 // have been created for this location, so do not apply it now.
8318 if (!parameters->options().shared())
8319 {
8320 gold_assert(tls_segment != NULL);
8321 value = tls_segment->memsz() - value;
8322 Relocate_functions<32, false>::rel32(view, value);
8323 }
8324 return ArmRelocFuncs::STATUS_OKAY;
8325
8326 default:
8327 gold_unreachable();
8328 }
8329
8330 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8331 _("unsupported reloc %u"),
8332 r_type);
8333 return ArmRelocFuncs::STATUS_BAD_RELOC;
8334}
8335
4a657b0d
DK
8336// Relocate section data.
8337
8338template<bool big_endian>
8339void
8340Target_arm<big_endian>::relocate_section(
8341 const Relocate_info<32, big_endian>* relinfo,
8342 unsigned int sh_type,
8343 const unsigned char* prelocs,
8344 size_t reloc_count,
8345 Output_section* output_section,
8346 bool needs_special_offset_handling,
8347 unsigned char* view,
ebabffbd 8348 Arm_address address,
364c7fa5
ILT
8349 section_size_type view_size,
8350 const Reloc_symbol_changes* reloc_symbol_changes)
4a657b0d
DK
8351{
8352 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
8353 gold_assert(sh_type == elfcpp::SHT_REL);
8354
218c5831
DK
8355 // See if we are relocating a relaxed input section. If so, the view
8356 // covers the whole output section and we need to adjust accordingly.
8357 if (needs_special_offset_handling)
43d12afe 8358 {
218c5831
DK
8359 const Output_relaxed_input_section* poris =
8360 output_section->find_relaxed_input_section(relinfo->object,
8361 relinfo->data_shndx);
8362 if (poris != NULL)
8363 {
8364 Arm_address section_address = poris->address();
8365 section_size_type section_size = poris->data_size();
8366
8367 gold_assert((section_address >= address)
8368 && ((section_address + section_size)
8369 <= (address + view_size)));
8370
8371 off_t offset = section_address - address;
8372 view += offset;
8373 address += offset;
8374 view_size = section_size;
8375 }
43d12afe
DK
8376 }
8377
4a657b0d
DK
8378 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
8379 Arm_relocate>(
8380 relinfo,
8381 this,
8382 prelocs,
8383 reloc_count,
8384 output_section,
8385 needs_special_offset_handling,
8386 view,
8387 address,
364c7fa5
ILT
8388 view_size,
8389 reloc_symbol_changes);
4a657b0d
DK
8390}
8391
8392// Return the size of a relocation while scanning during a relocatable
8393// link.
8394
8395template<bool big_endian>
8396unsigned int
8397Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
8398 unsigned int r_type,
8399 Relobj* object)
8400{
a6d1ef57 8401 r_type = get_real_reloc_type(r_type);
5c57f1be
DK
8402 const Arm_reloc_property* arp =
8403 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
8404 if (arp != NULL)
8405 return arp->size();
8406 else
4a657b0d 8407 {
5c57f1be
DK
8408 std::string reloc_name =
8409 arm_reloc_property_table->reloc_name_in_error_message(r_type);
8410 gold_error(_("%s: unexpected %s in object file"),
8411 object->name().c_str(), reloc_name.c_str());
4a657b0d
DK
8412 return 0;
8413 }
8414}
8415
8416// Scan the relocs during a relocatable link.
8417
8418template<bool big_endian>
8419void
8420Target_arm<big_endian>::scan_relocatable_relocs(
4a657b0d 8421 Symbol_table* symtab,
2ea97941 8422 Layout* layout,
4a657b0d
DK
8423 Sized_relobj<32, big_endian>* object,
8424 unsigned int data_shndx,
8425 unsigned int sh_type,
8426 const unsigned char* prelocs,
8427 size_t reloc_count,
8428 Output_section* output_section,
8429 bool needs_special_offset_handling,
8430 size_t local_symbol_count,
8431 const unsigned char* plocal_symbols,
8432 Relocatable_relocs* rr)
8433{
8434 gold_assert(sh_type == elfcpp::SHT_REL);
8435
8436 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
8437 Relocatable_size_for_reloc> Scan_relocatable_relocs;
8438
8439 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
8440 Scan_relocatable_relocs>(
4a657b0d 8441 symtab,
2ea97941 8442 layout,
4a657b0d
DK
8443 object,
8444 data_shndx,
8445 prelocs,
8446 reloc_count,
8447 output_section,
8448 needs_special_offset_handling,
8449 local_symbol_count,
8450 plocal_symbols,
8451 rr);
8452}
8453
8454// Relocate a section during a relocatable link.
8455
8456template<bool big_endian>
8457void
8458Target_arm<big_endian>::relocate_for_relocatable(
8459 const Relocate_info<32, big_endian>* relinfo,
8460 unsigned int sh_type,
8461 const unsigned char* prelocs,
8462 size_t reloc_count,
8463 Output_section* output_section,
8464 off_t offset_in_output_section,
8465 const Relocatable_relocs* rr,
8466 unsigned char* view,
ebabffbd 8467 Arm_address view_address,
4a657b0d
DK
8468 section_size_type view_size,
8469 unsigned char* reloc_view,
8470 section_size_type reloc_view_size)
8471{
8472 gold_assert(sh_type == elfcpp::SHT_REL);
8473
8474 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
8475 relinfo,
8476 prelocs,
8477 reloc_count,
8478 output_section,
8479 offset_in_output_section,
8480 rr,
8481 view,
8482 view_address,
8483 view_size,
8484 reloc_view,
8485 reloc_view_size);
8486}
8487
94cdfcff
DK
8488// Return the value to use for a dynamic symbol which requires special
8489// treatment. This is how we support equality comparisons of function
8490// pointers across shared library boundaries, as described in the
8491// processor specific ABI supplement.
8492
4a657b0d
DK
8493template<bool big_endian>
8494uint64_t
94cdfcff 8495Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4a657b0d 8496{
94cdfcff
DK
8497 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
8498 return this->plt_section()->address() + gsym->plt_offset();
4a657b0d
DK
8499}
8500
8501// Map platform-specific relocs to real relocs
8502//
8503template<bool big_endian>
8504unsigned int
a6d1ef57 8505Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
4a657b0d
DK
8506{
8507 switch (r_type)
8508 {
8509 case elfcpp::R_ARM_TARGET1:
a6d1ef57
DK
8510 // This is either R_ARM_ABS32 or R_ARM_REL32;
8511 return elfcpp::R_ARM_ABS32;
4a657b0d
DK
8512
8513 case elfcpp::R_ARM_TARGET2:
a6d1ef57
DK
8514 // This can be any reloc type but ususally is R_ARM_GOT_PREL
8515 return elfcpp::R_ARM_GOT_PREL;
4a657b0d
DK
8516
8517 default:
8518 return r_type;
8519 }
8520}
8521
d5b40221
DK
8522// Whether if two EABI versions V1 and V2 are compatible.
8523
8524template<bool big_endian>
8525bool
8526Target_arm<big_endian>::are_eabi_versions_compatible(
8527 elfcpp::Elf_Word v1,
8528 elfcpp::Elf_Word v2)
8529{
8530 // v4 and v5 are the same spec before and after it was released,
8531 // so allow mixing them.
8532 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
8533 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
8534 return true;
8535
8536 return v1 == v2;
8537}
8538
8539// Combine FLAGS from an input object called NAME and the processor-specific
8540// flags in the ELF header of the output. Much of this is adapted from the
8541// processor-specific flags merging code in elf32_arm_merge_private_bfd_data
8542// in bfd/elf32-arm.c.
8543
8544template<bool big_endian>
8545void
8546Target_arm<big_endian>::merge_processor_specific_flags(
8547 const std::string& name,
8548 elfcpp::Elf_Word flags)
8549{
8550 if (this->are_processor_specific_flags_set())
8551 {
8552 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
8553
8554 // Nothing to merge if flags equal to those in output.
8555 if (flags == out_flags)
8556 return;
8557
8558 // Complain about various flag mismatches.
8559 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
8560 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
8561 if (!this->are_eabi_versions_compatible(version1, version2))
8562 gold_error(_("Source object %s has EABI version %d but output has "
8563 "EABI version %d."),
8564 name.c_str(),
8565 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
8566 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
8567 }
8568 else
8569 {
8570 // If the input is the default architecture and had the default
8571 // flags then do not bother setting the flags for the output
8572 // architecture, instead allow future merges to do this. If no
8573 // future merges ever set these flags then they will retain their
8574 // uninitialised values, which surprise surprise, correspond
8575 // to the default values.
8576 if (flags == 0)
8577 return;
8578
8579 // This is the first time, just copy the flags.
8580 // We only copy the EABI version for now.
8581 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
8582 }
8583}
8584
8585// Adjust ELF file header.
8586template<bool big_endian>
8587void
8588Target_arm<big_endian>::do_adjust_elf_header(
8589 unsigned char* view,
8590 int len) const
8591{
8592 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
8593
8594 elfcpp::Ehdr<32, big_endian> ehdr(view);
8595 unsigned char e_ident[elfcpp::EI_NIDENT];
8596 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
8597
8598 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
8599 == elfcpp::EF_ARM_EABI_UNKNOWN)
8600 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
8601 else
8602 e_ident[elfcpp::EI_OSABI] = 0;
8603 e_ident[elfcpp::EI_ABIVERSION] = 0;
8604
8605 // FIXME: Do EF_ARM_BE8 adjustment.
8606
8607 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
8608 oehdr.put_e_ident(e_ident);
8609}
8610
8611// do_make_elf_object to override the same function in the base class.
8612// We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
8613// to store ARM specific information. Hence we need to have our own
8614// ELF object creation.
8615
8616template<bool big_endian>
8617Object*
8618Target_arm<big_endian>::do_make_elf_object(
8619 const std::string& name,
8620 Input_file* input_file,
2ea97941 8621 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
d5b40221
DK
8622{
8623 int et = ehdr.get_e_type();
8624 if (et == elfcpp::ET_REL)
8625 {
8626 Arm_relobj<big_endian>* obj =
2ea97941 8627 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
d5b40221
DK
8628 obj->setup();
8629 return obj;
8630 }
8631 else if (et == elfcpp::ET_DYN)
8632 {
8633 Sized_dynobj<32, big_endian>* obj =
2ea97941 8634 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
d5b40221
DK
8635 obj->setup();
8636 return obj;
8637 }
8638 else
8639 {
8640 gold_error(_("%s: unsupported ELF file type %d"),
8641 name.c_str(), et);
8642 return NULL;
8643 }
8644}
8645
a0351a69
DK
8646// Read the architecture from the Tag_also_compatible_with attribute, if any.
8647// Returns -1 if no architecture could be read.
8648// This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
8649
8650template<bool big_endian>
8651int
8652Target_arm<big_endian>::get_secondary_compatible_arch(
8653 const Attributes_section_data* pasd)
8654{
8655 const Object_attribute *known_attributes =
8656 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
8657
8658 // Note: the tag and its argument below are uleb128 values, though
8659 // currently-defined values fit in one byte for each.
8660 const std::string& sv =
8661 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
8662 if (sv.size() == 2
8663 && sv.data()[0] == elfcpp::Tag_CPU_arch
8664 && (sv.data()[1] & 128) != 128)
8665 return sv.data()[1];
8666
8667 // This tag is "safely ignorable", so don't complain if it looks funny.
8668 return -1;
8669}
8670
8671// Set, or unset, the architecture of the Tag_also_compatible_with attribute.
8672// The tag is removed if ARCH is -1.
8673// This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
8674
8675template<bool big_endian>
8676void
8677Target_arm<big_endian>::set_secondary_compatible_arch(
8678 Attributes_section_data* pasd,
8679 int arch)
8680{
8681 Object_attribute *known_attributes =
8682 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
8683
8684 if (arch == -1)
8685 {
8686 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
8687 return;
8688 }
8689
8690 // Note: the tag and its argument below are uleb128 values, though
8691 // currently-defined values fit in one byte for each.
8692 char sv[3];
8693 sv[0] = elfcpp::Tag_CPU_arch;
8694 gold_assert(arch != 0);
8695 sv[1] = arch;
8696 sv[2] = '\0';
8697
8698 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
8699}
8700
8701// Combine two values for Tag_CPU_arch, taking secondary compatibility tags
8702// into account.
8703// This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
8704
8705template<bool big_endian>
8706int
8707Target_arm<big_endian>::tag_cpu_arch_combine(
8708 const char* name,
8709 int oldtag,
8710 int* secondary_compat_out,
8711 int newtag,
8712 int secondary_compat)
8713{
8714#define T(X) elfcpp::TAG_CPU_ARCH_##X
8715 static const int v6t2[] =
8716 {
8717 T(V6T2), // PRE_V4.
8718 T(V6T2), // V4.
8719 T(V6T2), // V4T.
8720 T(V6T2), // V5T.
8721 T(V6T2), // V5TE.
8722 T(V6T2), // V5TEJ.
8723 T(V6T2), // V6.
8724 T(V7), // V6KZ.
8725 T(V6T2) // V6T2.
8726 };
8727 static const int v6k[] =
8728 {
8729 T(V6K), // PRE_V4.
8730 T(V6K), // V4.
8731 T(V6K), // V4T.
8732 T(V6K), // V5T.
8733 T(V6K), // V5TE.
8734 T(V6K), // V5TEJ.
8735 T(V6K), // V6.
8736 T(V6KZ), // V6KZ.
8737 T(V7), // V6T2.
8738 T(V6K) // V6K.
8739 };
8740 static const int v7[] =
8741 {
8742 T(V7), // PRE_V4.
8743 T(V7), // V4.
8744 T(V7), // V4T.
8745 T(V7), // V5T.
8746 T(V7), // V5TE.
8747 T(V7), // V5TEJ.
8748 T(V7), // V6.
8749 T(V7), // V6KZ.
8750 T(V7), // V6T2.
8751 T(V7), // V6K.
8752 T(V7) // V7.
8753 };
8754 static const int v6_m[] =
8755 {
8756 -1, // PRE_V4.
8757 -1, // V4.
8758 T(V6K), // V4T.
8759 T(V6K), // V5T.
8760 T(V6K), // V5TE.
8761 T(V6K), // V5TEJ.
8762 T(V6K), // V6.
8763 T(V6KZ), // V6KZ.
8764 T(V7), // V6T2.
8765 T(V6K), // V6K.
8766 T(V7), // V7.
8767 T(V6_M) // V6_M.
8768 };
8769 static const int v6s_m[] =
8770 {
8771 -1, // PRE_V4.
8772 -1, // V4.
8773 T(V6K), // V4T.
8774 T(V6K), // V5T.
8775 T(V6K), // V5TE.
8776 T(V6K), // V5TEJ.
8777 T(V6K), // V6.
8778 T(V6KZ), // V6KZ.
8779 T(V7), // V6T2.
8780 T(V6K), // V6K.
8781 T(V7), // V7.
8782 T(V6S_M), // V6_M.
8783 T(V6S_M) // V6S_M.
8784 };
8785 static const int v7e_m[] =
8786 {
8787 -1, // PRE_V4.
8788 -1, // V4.
8789 T(V7E_M), // V4T.
8790 T(V7E_M), // V5T.
8791 T(V7E_M), // V5TE.
8792 T(V7E_M), // V5TEJ.
8793 T(V7E_M), // V6.
8794 T(V7E_M), // V6KZ.
8795 T(V7E_M), // V6T2.
8796 T(V7E_M), // V6K.
8797 T(V7E_M), // V7.
8798 T(V7E_M), // V6_M.
8799 T(V7E_M), // V6S_M.
8800 T(V7E_M) // V7E_M.
8801 };
8802 static const int v4t_plus_v6_m[] =
8803 {
8804 -1, // PRE_V4.
8805 -1, // V4.
8806 T(V4T), // V4T.
8807 T(V5T), // V5T.
8808 T(V5TE), // V5TE.
8809 T(V5TEJ), // V5TEJ.
8810 T(V6), // V6.
8811 T(V6KZ), // V6KZ.
8812 T(V6T2), // V6T2.
8813 T(V6K), // V6K.
8814 T(V7), // V7.
8815 T(V6_M), // V6_M.
8816 T(V6S_M), // V6S_M.
8817 T(V7E_M), // V7E_M.
8818 T(V4T_PLUS_V6_M) // V4T plus V6_M.
8819 };
8820 static const int *comb[] =
8821 {
8822 v6t2,
8823 v6k,
8824 v7,
8825 v6_m,
8826 v6s_m,
8827 v7e_m,
8828 // Pseudo-architecture.
8829 v4t_plus_v6_m
8830 };
8831
8832 // Check we've not got a higher architecture than we know about.
8833
8834 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
8835 {
8836 gold_error(_("%s: unknown CPU architecture"), name);
8837 return -1;
8838 }
8839
8840 // Override old tag if we have a Tag_also_compatible_with on the output.
8841
8842 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
8843 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
8844 oldtag = T(V4T_PLUS_V6_M);
8845
8846 // And override the new tag if we have a Tag_also_compatible_with on the
8847 // input.
8848
8849 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
8850 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
8851 newtag = T(V4T_PLUS_V6_M);
8852
8853 // Architectures before V6KZ add features monotonically.
8854 int tagh = std::max(oldtag, newtag);
8855 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
8856 return tagh;
8857
8858 int tagl = std::min(oldtag, newtag);
8859 int result = comb[tagh - T(V6T2)][tagl];
8860
8861 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
8862 // as the canonical version.
8863 if (result == T(V4T_PLUS_V6_M))
8864 {
8865 result = T(V4T);
8866 *secondary_compat_out = T(V6_M);
8867 }
8868 else
8869 *secondary_compat_out = -1;
8870
8871 if (result == -1)
8872 {
8873 gold_error(_("%s: conflicting CPU architectures %d/%d"),
8874 name, oldtag, newtag);
8875 return -1;
8876 }
8877
8878 return result;
8879#undef T
8880}
8881
8882// Helper to print AEABI enum tag value.
8883
8884template<bool big_endian>
8885std::string
8886Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
8887{
8888 static const char *aeabi_enum_names[] =
8889 { "", "variable-size", "32-bit", "" };
8890 const size_t aeabi_enum_names_size =
8891 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
8892
8893 if (value < aeabi_enum_names_size)
8894 return std::string(aeabi_enum_names[value]);
8895 else
8896 {
8897 char buffer[100];
8898 sprintf(buffer, "<unknown value %u>", value);
8899 return std::string(buffer);
8900 }
8901}
8902
8903// Return the string value to store in TAG_CPU_name.
8904
8905template<bool big_endian>
8906std::string
8907Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
8908{
8909 static const char *name_table[] = {
8910 // These aren't real CPU names, but we can't guess
8911 // that from the architecture version alone.
8912 "Pre v4",
8913 "ARM v4",
8914 "ARM v4T",
8915 "ARM v5T",
8916 "ARM v5TE",
8917 "ARM v5TEJ",
8918 "ARM v6",
8919 "ARM v6KZ",
8920 "ARM v6T2",
8921 "ARM v6K",
8922 "ARM v7",
8923 "ARM v6-M",
8924 "ARM v6S-M",
8925 "ARM v7E-M"
8926 };
8927 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
8928
8929 if (value < name_table_size)
8930 return std::string(name_table[value]);
8931 else
8932 {
8933 char buffer[100];
8934 sprintf(buffer, "<unknown CPU value %u>", value);
8935 return std::string(buffer);
8936 }
8937}
8938
8939// Merge object attributes from input file called NAME with those of the
8940// output. The input object attributes are in the object pointed by PASD.
8941
8942template<bool big_endian>
8943void
8944Target_arm<big_endian>::merge_object_attributes(
8945 const char* name,
8946 const Attributes_section_data* pasd)
8947{
8948 // Return if there is no attributes section data.
8949 if (pasd == NULL)
8950 return;
8951
8952 // If output has no object attributes, just copy.
8953 if (this->attributes_section_data_ == NULL)
8954 {
8955 this->attributes_section_data_ = new Attributes_section_data(*pasd);
8956 return;
8957 }
8958
8959 const int vendor = Object_attribute::OBJ_ATTR_PROC;
8960 const Object_attribute* in_attr = pasd->known_attributes(vendor);
8961 Object_attribute* out_attr =
8962 this->attributes_section_data_->known_attributes(vendor);
8963
8964 // This needs to happen before Tag_ABI_FP_number_model is merged. */
8965 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
8966 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
8967 {
8968 // Ignore mismatches if the object doesn't use floating point. */
8969 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
8970 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
8971 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
8972 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
8973 gold_error(_("%s uses VFP register arguments, output does not"),
8974 name);
8975 }
8976
8977 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
8978 {
8979 // Merge this attribute with existing attributes.
8980 switch (i)
8981 {
8982 case elfcpp::Tag_CPU_raw_name:
8983 case elfcpp::Tag_CPU_name:
8984 // These are merged after Tag_CPU_arch.
8985 break;
8986
8987 case elfcpp::Tag_ABI_optimization_goals:
8988 case elfcpp::Tag_ABI_FP_optimization_goals:
8989 // Use the first value seen.
8990 break;
8991
8992 case elfcpp::Tag_CPU_arch:
8993 {
8994 unsigned int saved_out_attr = out_attr->int_value();
8995 // Merge Tag_CPU_arch and Tag_also_compatible_with.
8996 int secondary_compat =
8997 this->get_secondary_compatible_arch(pasd);
8998 int secondary_compat_out =
8999 this->get_secondary_compatible_arch(
9000 this->attributes_section_data_);
9001 out_attr[i].set_int_value(
9002 tag_cpu_arch_combine(name, out_attr[i].int_value(),
9003 &secondary_compat_out,
9004 in_attr[i].int_value(),
9005 secondary_compat));
9006 this->set_secondary_compatible_arch(this->attributes_section_data_,
9007 secondary_compat_out);
9008
9009 // Merge Tag_CPU_name and Tag_CPU_raw_name.
9010 if (out_attr[i].int_value() == saved_out_attr)
9011 ; // Leave the names alone.
9012 else if (out_attr[i].int_value() == in_attr[i].int_value())
9013 {
9014 // The output architecture has been changed to match the
9015 // input architecture. Use the input names.
9016 out_attr[elfcpp::Tag_CPU_name].set_string_value(
9017 in_attr[elfcpp::Tag_CPU_name].string_value());
9018 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
9019 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
9020 }
9021 else
9022 {
9023 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
9024 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
9025 }
9026
9027 // If we still don't have a value for Tag_CPU_name,
9028 // make one up now. Tag_CPU_raw_name remains blank.
9029 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
9030 {
9031 const std::string cpu_name =
9032 this->tag_cpu_name_value(out_attr[i].int_value());
9033 // FIXME: If we see an unknown CPU, this will be set
9034 // to "<unknown CPU n>", where n is the attribute value.
9035 // This is different from BFD, which leaves the name alone.
9036 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
9037 }
9038 }
9039 break;
9040
9041 case elfcpp::Tag_ARM_ISA_use:
9042 case elfcpp::Tag_THUMB_ISA_use:
9043 case elfcpp::Tag_WMMX_arch:
9044 case elfcpp::Tag_Advanced_SIMD_arch:
9045 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
9046 case elfcpp::Tag_ABI_FP_rounding:
9047 case elfcpp::Tag_ABI_FP_exceptions:
9048 case elfcpp::Tag_ABI_FP_user_exceptions:
9049 case elfcpp::Tag_ABI_FP_number_model:
9050 case elfcpp::Tag_VFP_HP_extension:
9051 case elfcpp::Tag_CPU_unaligned_access:
9052 case elfcpp::Tag_T2EE_use:
9053 case elfcpp::Tag_Virtualization_use:
9054 case elfcpp::Tag_MPextension_use:
9055 // Use the largest value specified.
9056 if (in_attr[i].int_value() > out_attr[i].int_value())
9057 out_attr[i].set_int_value(in_attr[i].int_value());
9058 break;
9059
9060 case elfcpp::Tag_ABI_align8_preserved:
9061 case elfcpp::Tag_ABI_PCS_RO_data:
9062 // Use the smallest value specified.
9063 if (in_attr[i].int_value() < out_attr[i].int_value())
9064 out_attr[i].set_int_value(in_attr[i].int_value());
9065 break;
9066
9067 case elfcpp::Tag_ABI_align8_needed:
9068 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
9069 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
9070 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
9071 == 0)))
9072 {
9073 // This error message should be enabled once all non-conformant
9074 // binaries in the toolchain have had the attributes set
9075 // properly.
9076 // gold_error(_("output 8-byte data alignment conflicts with %s"),
9077 // name);
9078 }
9079 // Fall through.
9080 case elfcpp::Tag_ABI_FP_denormal:
9081 case elfcpp::Tag_ABI_PCS_GOT_use:
9082 {
9083 // These tags have 0 = don't care, 1 = strong requirement,
9084 // 2 = weak requirement.
9085 static const int order_021[3] = {0, 2, 1};
9086
9087 // Use the "greatest" from the sequence 0, 2, 1, or the largest
9088 // value if greater than 2 (for future-proofing).
9089 if ((in_attr[i].int_value() > 2
9090 && in_attr[i].int_value() > out_attr[i].int_value())
9091 || (in_attr[i].int_value() <= 2
9092 && out_attr[i].int_value() <= 2
9093 && (order_021[in_attr[i].int_value()]
9094 > order_021[out_attr[i].int_value()])))
9095 out_attr[i].set_int_value(in_attr[i].int_value());
9096 }
9097 break;
9098
9099 case elfcpp::Tag_CPU_arch_profile:
9100 if (out_attr[i].int_value() != in_attr[i].int_value())
9101 {
9102 // 0 will merge with anything.
9103 // 'A' and 'S' merge to 'A'.
9104 // 'R' and 'S' merge to 'R'.
9105 // 'M' and 'A|R|S' is an error.
9106 if (out_attr[i].int_value() == 0
9107 || (out_attr[i].int_value() == 'S'
9108 && (in_attr[i].int_value() == 'A'
9109 || in_attr[i].int_value() == 'R')))
9110 out_attr[i].set_int_value(in_attr[i].int_value());
9111 else if (in_attr[i].int_value() == 0
9112 || (in_attr[i].int_value() == 'S'
9113 && (out_attr[i].int_value() == 'A'
9114 || out_attr[i].int_value() == 'R')))
9115 ; // Do nothing.
9116 else
9117 {
9118 gold_error
9119 (_("conflicting architecture profiles %c/%c"),
9120 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
9121 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
9122 }
9123 }
9124 break;
9125 case elfcpp::Tag_VFP_arch:
9126 {
9127 static const struct
9128 {
9129 int ver;
9130 int regs;
9131 } vfp_versions[7] =
9132 {
9133 {0, 0},
9134 {1, 16},
9135 {2, 16},
9136 {3, 32},
9137 {3, 16},
9138 {4, 32},
9139 {4, 16}
9140 };
9141
9142 // Values greater than 6 aren't defined, so just pick the
9143 // biggest.
9144 if (in_attr[i].int_value() > 6
9145 && in_attr[i].int_value() > out_attr[i].int_value())
9146 {
9147 *out_attr = *in_attr;
9148 break;
9149 }
9150 // The output uses the superset of input features
9151 // (ISA version) and registers.
9152 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
9153 vfp_versions[out_attr[i].int_value()].ver);
9154 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
9155 vfp_versions[out_attr[i].int_value()].regs);
9156 // This assumes all possible supersets are also a valid
9157 // options.
9158 int newval;
9159 for (newval = 6; newval > 0; newval--)
9160 {
9161 if (regs == vfp_versions[newval].regs
9162 && ver == vfp_versions[newval].ver)
9163 break;
9164 }
9165 out_attr[i].set_int_value(newval);
9166 }
9167 break;
9168 case elfcpp::Tag_PCS_config:
9169 if (out_attr[i].int_value() == 0)
9170 out_attr[i].set_int_value(in_attr[i].int_value());
9171 else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
9172 {
9173 // It's sometimes ok to mix different configs, so this is only
9174 // a warning.
9175 gold_warning(_("%s: conflicting platform configuration"), name);
9176 }
9177 break;
9178 case elfcpp::Tag_ABI_PCS_R9_use:
9179 if (in_attr[i].int_value() != out_attr[i].int_value()
9180 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
9181 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
9182 {
9183 gold_error(_("%s: conflicting use of R9"), name);
9184 }
9185 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
9186 out_attr[i].set_int_value(in_attr[i].int_value());
9187 break;
9188 case elfcpp::Tag_ABI_PCS_RW_data:
9189 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
9190 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
9191 != elfcpp::AEABI_R9_SB)
9192 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
9193 != elfcpp::AEABI_R9_unused))
9194 {
9195 gold_error(_("%s: SB relative addressing conflicts with use "
9196 "of R9"),
9197 name);
9198 }
9199 // Use the smallest value specified.
9200 if (in_attr[i].int_value() < out_attr[i].int_value())
9201 out_attr[i].set_int_value(in_attr[i].int_value());
9202 break;
9203 case elfcpp::Tag_ABI_PCS_wchar_t:
9204 // FIXME: Make it possible to turn off this warning.
9205 if (out_attr[i].int_value()
9206 && in_attr[i].int_value()
9207 && out_attr[i].int_value() != in_attr[i].int_value())
9208 {
9209 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
9210 "use %u-byte wchar_t; use of wchar_t values "
9211 "across objects may fail"),
9212 name, in_attr[i].int_value(),
9213 out_attr[i].int_value());
9214 }
9215 else if (in_attr[i].int_value() && !out_attr[i].int_value())
9216 out_attr[i].set_int_value(in_attr[i].int_value());
9217 break;
9218 case elfcpp::Tag_ABI_enum_size:
9219 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
9220 {
9221 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
9222 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
9223 {
9224 // The existing object is compatible with anything.
9225 // Use whatever requirements the new object has.
9226 out_attr[i].set_int_value(in_attr[i].int_value());
9227 }
9228 // FIXME: Make it possible to turn off this warning.
9229 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
9230 && out_attr[i].int_value() != in_attr[i].int_value())
9231 {
9232 unsigned int in_value = in_attr[i].int_value();
9233 unsigned int out_value = out_attr[i].int_value();
9234 gold_warning(_("%s uses %s enums yet the output is to use "
9235 "%s enums; use of enum values across objects "
9236 "may fail"),
9237 name,
9238 this->aeabi_enum_name(in_value).c_str(),
9239 this->aeabi_enum_name(out_value).c_str());
9240 }
9241 }
9242 break;
9243 case elfcpp::Tag_ABI_VFP_args:
9244 // Aready done.
9245 break;
9246 case elfcpp::Tag_ABI_WMMX_args:
9247 if (in_attr[i].int_value() != out_attr[i].int_value())
9248 {
9249 gold_error(_("%s uses iWMMXt register arguments, output does "
9250 "not"),
9251 name);
9252 }
9253 break;
9254 case Object_attribute::Tag_compatibility:
9255 // Merged in target-independent code.
9256 break;
9257 case elfcpp::Tag_ABI_HardFP_use:
9258 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
9259 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
9260 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
9261 out_attr[i].set_int_value(3);
9262 else if (in_attr[i].int_value() > out_attr[i].int_value())
9263 out_attr[i].set_int_value(in_attr[i].int_value());
9264 break;
9265 case elfcpp::Tag_ABI_FP_16bit_format:
9266 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
9267 {
9268 if (in_attr[i].int_value() != out_attr[i].int_value())
9269 gold_error(_("fp16 format mismatch between %s and output"),
9270 name);
9271 }
9272 if (in_attr[i].int_value() != 0)
9273 out_attr[i].set_int_value(in_attr[i].int_value());
9274 break;
9275
9276 case elfcpp::Tag_nodefaults:
9277 // This tag is set if it exists, but the value is unused (and is
9278 // typically zero). We don't actually need to do anything here -
9279 // the merge happens automatically when the type flags are merged
9280 // below.
9281 break;
9282 case elfcpp::Tag_also_compatible_with:
9283 // Already done in Tag_CPU_arch.
9284 break;
9285 case elfcpp::Tag_conformance:
9286 // Keep the attribute if it matches. Throw it away otherwise.
9287 // No attribute means no claim to conform.
9288 if (in_attr[i].string_value() != out_attr[i].string_value())
9289 out_attr[i].set_string_value("");
9290 break;
9291
9292 default:
9293 {
9294 const char* err_object = NULL;
9295
9296 // The "known_obj_attributes" table does contain some undefined
9297 // attributes. Ensure that there are unused.
9298 if (out_attr[i].int_value() != 0
9299 || out_attr[i].string_value() != "")
9300 err_object = "output";
9301 else if (in_attr[i].int_value() != 0
9302 || in_attr[i].string_value() != "")
9303 err_object = name;
9304
9305 if (err_object != NULL)
9306 {
9307 // Attribute numbers >=64 (mod 128) can be safely ignored.
9308 if ((i & 127) < 64)
9309 gold_error(_("%s: unknown mandatory EABI object attribute "
9310 "%d"),
9311 err_object, i);
9312 else
9313 gold_warning(_("%s: unknown EABI object attribute %d"),
9314 err_object, i);
9315 }
9316
9317 // Only pass on attributes that match in both inputs.
9318 if (!in_attr[i].matches(out_attr[i]))
9319 {
9320 out_attr[i].set_int_value(0);
9321 out_attr[i].set_string_value("");
9322 }
9323 }
9324 }
9325
9326 // If out_attr was copied from in_attr then it won't have a type yet.
9327 if (in_attr[i].type() && !out_attr[i].type())
9328 out_attr[i].set_type(in_attr[i].type());
9329 }
9330
9331 // Merge Tag_compatibility attributes and any common GNU ones.
9332 this->attributes_section_data_->merge(name, pasd);
9333
9334 // Check for any attributes not known on ARM.
9335 typedef Vendor_object_attributes::Other_attributes Other_attributes;
9336 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
9337 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
9338 Other_attributes* out_other_attributes =
9339 this->attributes_section_data_->other_attributes(vendor);
9340 Other_attributes::iterator out_iter = out_other_attributes->begin();
9341
9342 while (in_iter != in_other_attributes->end()
9343 || out_iter != out_other_attributes->end())
9344 {
9345 const char* err_object = NULL;
9346 int err_tag = 0;
9347
9348 // The tags for each list are in numerical order.
9349 // If the tags are equal, then merge.
9350 if (out_iter != out_other_attributes->end()
9351 && (in_iter == in_other_attributes->end()
9352 || in_iter->first > out_iter->first))
9353 {
9354 // This attribute only exists in output. We can't merge, and we
9355 // don't know what the tag means, so delete it.
9356 err_object = "output";
9357 err_tag = out_iter->first;
9358 int saved_tag = out_iter->first;
9359 delete out_iter->second;
9360 out_other_attributes->erase(out_iter);
9361 out_iter = out_other_attributes->upper_bound(saved_tag);
9362 }
9363 else if (in_iter != in_other_attributes->end()
9364 && (out_iter != out_other_attributes->end()
9365 || in_iter->first < out_iter->first))
9366 {
9367 // This attribute only exists in input. We can't merge, and we
9368 // don't know what the tag means, so ignore it.
9369 err_object = name;
9370 err_tag = in_iter->first;
9371 ++in_iter;
9372 }
9373 else // The tags are equal.
9374 {
9375 // As present, all attributes in the list are unknown, and
9376 // therefore can't be merged meaningfully.
9377 err_object = "output";
9378 err_tag = out_iter->first;
9379
9380 // Only pass on attributes that match in both inputs.
9381 if (!in_iter->second->matches(*(out_iter->second)))
9382 {
9383 // No match. Delete the attribute.
9384 int saved_tag = out_iter->first;
9385 delete out_iter->second;
9386 out_other_attributes->erase(out_iter);
9387 out_iter = out_other_attributes->upper_bound(saved_tag);
9388 }
9389 else
9390 {
9391 // Matched. Keep the attribute and move to the next.
9392 ++out_iter;
9393 ++in_iter;
9394 }
9395 }
9396
9397 if (err_object)
9398 {
9399 // Attribute numbers >=64 (mod 128) can be safely ignored. */
9400 if ((err_tag & 127) < 64)
9401 {
9402 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
9403 err_object, err_tag);
9404 }
9405 else
9406 {
9407 gold_warning(_("%s: unknown EABI object attribute %d"),
9408 err_object, err_tag);
9409 }
9410 }
9411 }
9412}
9413
55da9579
DK
9414// Stub-generation methods for Target_arm.
9415
9416// Make a new Arm_input_section object.
9417
9418template<bool big_endian>
9419Arm_input_section<big_endian>*
9420Target_arm<big_endian>::new_arm_input_section(
2ea97941
ILT
9421 Relobj* relobj,
9422 unsigned int shndx)
55da9579 9423{
5ac169d4 9424 Section_id sid(relobj, shndx);
55da9579
DK
9425
9426 Arm_input_section<big_endian>* arm_input_section =
2ea97941 9427 new Arm_input_section<big_endian>(relobj, shndx);
55da9579
DK
9428 arm_input_section->init();
9429
9430 // Register new Arm_input_section in map for look-up.
9431 std::pair<typename Arm_input_section_map::iterator, bool> ins =
5ac169d4 9432 this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
55da9579
DK
9433
9434 // Make sure that it we have not created another Arm_input_section
9435 // for this input section already.
9436 gold_assert(ins.second);
9437
9438 return arm_input_section;
9439}
9440
9441// Find the Arm_input_section object corresponding to the SHNDX-th input
9442// section of RELOBJ.
9443
9444template<bool big_endian>
9445Arm_input_section<big_endian>*
9446Target_arm<big_endian>::find_arm_input_section(
2ea97941
ILT
9447 Relobj* relobj,
9448 unsigned int shndx) const
55da9579 9449{
5ac169d4 9450 Section_id sid(relobj, shndx);
55da9579 9451 typename Arm_input_section_map::const_iterator p =
5ac169d4 9452 this->arm_input_section_map_.find(sid);
55da9579
DK
9453 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
9454}
9455
9456// Make a new stub table.
9457
9458template<bool big_endian>
9459Stub_table<big_endian>*
9460Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
9461{
2ea97941 9462 Stub_table<big_endian>* stub_table =
55da9579 9463 new Stub_table<big_endian>(owner);
2ea97941 9464 this->stub_tables_.push_back(stub_table);
55da9579 9465
2ea97941
ILT
9466 stub_table->set_address(owner->address() + owner->data_size());
9467 stub_table->set_file_offset(owner->offset() + owner->data_size());
9468 stub_table->finalize_data_size();
55da9579 9469
2ea97941 9470 return stub_table;
55da9579
DK
9471}
9472
eb44217c
DK
9473// Scan a relocation for stub generation.
9474
9475template<bool big_endian>
9476void
9477Target_arm<big_endian>::scan_reloc_for_stub(
9478 const Relocate_info<32, big_endian>* relinfo,
9479 unsigned int r_type,
9480 const Sized_symbol<32>* gsym,
9481 unsigned int r_sym,
9482 const Symbol_value<32>* psymval,
9483 elfcpp::Elf_types<32>::Elf_Swxword addend,
9484 Arm_address address)
9485{
2ea97941 9486 typedef typename Target_arm<big_endian>::Relocate Relocate;
eb44217c
DK
9487
9488 const Arm_relobj<big_endian>* arm_relobj =
9489 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
9490
a2162063
ILT
9491 if (r_type == elfcpp::R_ARM_V4BX)
9492 {
9493 const uint32_t reg = (addend & 0xf);
9b2fd367
DK
9494 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
9495 && reg < 0xf)
a2162063
ILT
9496 {
9497 // Try looking up an existing stub from a stub table.
9498 Stub_table<big_endian>* stub_table =
9499 arm_relobj->stub_table(relinfo->data_shndx);
9500 gold_assert(stub_table != NULL);
9501
9502 if (stub_table->find_arm_v4bx_stub(reg) == NULL)
9503 {
9504 // create a new stub and add it to stub table.
9505 Arm_v4bx_stub* stub =
9506 this->stub_factory().make_arm_v4bx_stub(reg);
9507 gold_assert(stub != NULL);
9508 stub_table->add_arm_v4bx_stub(stub);
9509 }
9510 }
9511
9512 return;
9513 }
9514
eb44217c
DK
9515 bool target_is_thumb;
9516 Symbol_value<32> symval;
9517 if (gsym != NULL)
9518 {
9519 // This is a global symbol. Determine if we use PLT and if the
9520 // final target is THUMB.
2ea97941 9521 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
eb44217c
DK
9522 {
9523 // This uses a PLT, change the symbol value.
9524 symval.set_output_value(this->plt_section()->address()
9525 + gsym->plt_offset());
9526 psymval = &symval;
9527 target_is_thumb = false;
9528 }
9529 else if (gsym->is_undefined())
9530 // There is no need to generate a stub symbol is undefined.
9531 return;
9532 else
9533 {
9534 target_is_thumb =
9535 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
9536 || (gsym->type() == elfcpp::STT_FUNC
9537 && !gsym->is_undefined()
9538 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
9539 }
9540 }
9541 else
9542 {
9543 // This is a local symbol. Determine if the final target is THUMB.
9544 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
9545 }
9546
9547 // Strip LSB if this points to a THUMB target.
5c57f1be
DK
9548 const Arm_reloc_property* reloc_property =
9549 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
9550 gold_assert(reloc_property != NULL);
eb44217c 9551 if (target_is_thumb
5c57f1be 9552 && reloc_property->uses_thumb_bit()
eb44217c
DK
9553 && ((psymval->value(arm_relobj, 0) & 1) != 0))
9554 {
9555 Arm_address stripped_value =
9556 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
9557 symval.set_output_value(stripped_value);
9558 psymval = &symval;
9559 }
9560
9561 // Get the symbol value.
9562 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
9563
9564 // Owing to pipelining, the PC relative branches below actually skip
9565 // two instructions when the branch offset is 0.
9566 Arm_address destination;
9567 switch (r_type)
9568 {
9569 case elfcpp::R_ARM_CALL:
9570 case elfcpp::R_ARM_JUMP24:
9571 case elfcpp::R_ARM_PLT32:
9572 // ARM branches.
9573 destination = value + addend + 8;
9574 break;
9575 case elfcpp::R_ARM_THM_CALL:
9576 case elfcpp::R_ARM_THM_XPC22:
9577 case elfcpp::R_ARM_THM_JUMP24:
9578 case elfcpp::R_ARM_THM_JUMP19:
9579 // THUMB branches.
9580 destination = value + addend + 4;
9581 break;
9582 default:
9583 gold_unreachable();
9584 }
9585
a120bc7f 9586 Reloc_stub* stub = NULL;
eb44217c
DK
9587 Stub_type stub_type =
9588 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
9589 target_is_thumb);
a120bc7f
DK
9590 if (stub_type != arm_stub_none)
9591 {
9592 // Try looking up an existing stub from a stub table.
9593 Stub_table<big_endian>* stub_table =
9594 arm_relobj->stub_table(relinfo->data_shndx);
9595 gold_assert(stub_table != NULL);
eb44217c 9596
a120bc7f
DK
9597 // Locate stub by destination.
9598 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
eb44217c 9599
a120bc7f
DK
9600 // Create a stub if there is not one already
9601 stub = stub_table->find_reloc_stub(stub_key);
9602 if (stub == NULL)
9603 {
9604 // create a new stub and add it to stub table.
9605 stub = this->stub_factory().make_reloc_stub(stub_type);
9606 stub_table->add_reloc_stub(stub, stub_key);
9607 }
9608
9609 // Record the destination address.
9610 stub->set_destination_address(destination
9611 | (target_is_thumb ? 1 : 0));
eb44217c
DK
9612 }
9613
a120bc7f
DK
9614 // For Cortex-A8, we need to record a relocation at 4K page boundary.
9615 if (this->fix_cortex_a8_
9616 && (r_type == elfcpp::R_ARM_THM_JUMP24
9617 || r_type == elfcpp::R_ARM_THM_JUMP19
9618 || r_type == elfcpp::R_ARM_THM_CALL
9619 || r_type == elfcpp::R_ARM_THM_XPC22)
9620 && (address & 0xfffU) == 0xffeU)
9621 {
9622 // Found a candidate. Note we haven't checked the destination is
9623 // within 4K here: if we do so (and don't create a record) we can't
9624 // tell that a branch should have been relocated when scanning later.
9625 this->cortex_a8_relocs_info_[address] =
9626 new Cortex_a8_reloc(stub, r_type,
9627 destination | (target_is_thumb ? 1 : 0));
9628 }
eb44217c
DK
9629}
9630
9631// This function scans a relocation sections for stub generation.
9632// The template parameter Relocate must be a class type which provides
9633// a single function, relocate(), which implements the machine
9634// specific part of a relocation.
9635
9636// BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
9637// SHT_REL or SHT_RELA.
9638
9639// PRELOCS points to the relocation data. RELOC_COUNT is the number
9640// of relocs. OUTPUT_SECTION is the output section.
9641// NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
9642// mapped to output offsets.
9643
9644// VIEW is the section data, VIEW_ADDRESS is its memory address, and
9645// VIEW_SIZE is the size. These refer to the input section, unless
9646// NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
9647// the output section.
9648
9649template<bool big_endian>
9650template<int sh_type>
9651void inline
9652Target_arm<big_endian>::scan_reloc_section_for_stubs(
9653 const Relocate_info<32, big_endian>* relinfo,
9654 const unsigned char* prelocs,
9655 size_t reloc_count,
9656 Output_section* output_section,
9657 bool needs_special_offset_handling,
9658 const unsigned char* view,
9659 elfcpp::Elf_types<32>::Elf_Addr view_address,
9660 section_size_type)
9661{
9662 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
9663 const int reloc_size =
9664 Reloc_types<sh_type, 32, big_endian>::reloc_size;
9665
9666 Arm_relobj<big_endian>* arm_object =
9667 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
9668 unsigned int local_count = arm_object->local_symbol_count();
9669
9670 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
9671
9672 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
9673 {
9674 Reltype reloc(prelocs);
9675
9676 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
9677 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
9678 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
9679
9680 r_type = this->get_real_reloc_type(r_type);
9681
9682 // Only a few relocation types need stubs.
9683 if ((r_type != elfcpp::R_ARM_CALL)
9684 && (r_type != elfcpp::R_ARM_JUMP24)
9685 && (r_type != elfcpp::R_ARM_PLT32)
9686 && (r_type != elfcpp::R_ARM_THM_CALL)
9687 && (r_type != elfcpp::R_ARM_THM_XPC22)
9688 && (r_type != elfcpp::R_ARM_THM_JUMP24)
a2162063
ILT
9689 && (r_type != elfcpp::R_ARM_THM_JUMP19)
9690 && (r_type != elfcpp::R_ARM_V4BX))
eb44217c
DK
9691 continue;
9692
2ea97941 9693 section_offset_type offset =
eb44217c
DK
9694 convert_to_section_size_type(reloc.get_r_offset());
9695
9696 if (needs_special_offset_handling)
9697 {
2ea97941
ILT
9698 offset = output_section->output_offset(relinfo->object,
9699 relinfo->data_shndx,
9700 offset);
9701 if (offset == -1)
eb44217c
DK
9702 continue;
9703 }
9704
a2162063
ILT
9705 if (r_type == elfcpp::R_ARM_V4BX)
9706 {
9707 // Get the BX instruction.
9708 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
9709 const Valtype* wv = reinterpret_cast<const Valtype*>(view + offset);
9710 elfcpp::Elf_types<32>::Elf_Swxword insn =
9711 elfcpp::Swap<32, big_endian>::readval(wv);
9712 this->scan_reloc_for_stub(relinfo, r_type, NULL, 0, NULL,
9713 insn, NULL);
9714 continue;
9715 }
9716
eb44217c
DK
9717 // Get the addend.
9718 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
9719 elfcpp::Elf_types<32>::Elf_Swxword addend =
2ea97941 9720 stub_addend_reader(r_type, view + offset, reloc);
eb44217c
DK
9721
9722 const Sized_symbol<32>* sym;
9723
9724 Symbol_value<32> symval;
9725 const Symbol_value<32> *psymval;
9726 if (r_sym < local_count)
9727 {
9728 sym = NULL;
9729 psymval = arm_object->local_symbol(r_sym);
9730
9731 // If the local symbol belongs to a section we are discarding,
9732 // and that section is a debug section, try to find the
9733 // corresponding kept section and map this symbol to its
9734 // counterpart in the kept section. The symbol must not
9735 // correspond to a section we are folding.
9736 bool is_ordinary;
2ea97941 9737 unsigned int shndx = psymval->input_shndx(&is_ordinary);
eb44217c 9738 if (is_ordinary
2ea97941
ILT
9739 && shndx != elfcpp::SHN_UNDEF
9740 && !arm_object->is_section_included(shndx)
9741 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
eb44217c
DK
9742 {
9743 if (comdat_behavior == CB_UNDETERMINED)
9744 {
9745 std::string name =
9746 arm_object->section_name(relinfo->data_shndx);
9747 comdat_behavior = get_comdat_behavior(name.c_str());
9748 }
9749 if (comdat_behavior == CB_PRETEND)
9750 {
9751 bool found;
9752 typename elfcpp::Elf_types<32>::Elf_Addr value =
2ea97941 9753 arm_object->map_to_kept_section(shndx, &found);
eb44217c
DK
9754 if (found)
9755 symval.set_output_value(value + psymval->input_value());
9756 else
9757 symval.set_output_value(0);
9758 }
9759 else
9760 {
9761 symval.set_output_value(0);
9762 }
9763 symval.set_no_output_symtab_entry();
9764 psymval = &symval;
9765 }
9766 }
9767 else
9768 {
9769 const Symbol* gsym = arm_object->global_symbol(r_sym);
9770 gold_assert(gsym != NULL);
9771 if (gsym->is_forwarder())
9772 gsym = relinfo->symtab->resolve_forwards(gsym);
9773
9774 sym = static_cast<const Sized_symbol<32>*>(gsym);
9775 if (sym->has_symtab_index())
9776 symval.set_output_symtab_index(sym->symtab_index());
9777 else
9778 symval.set_no_output_symtab_entry();
9779
9780 // We need to compute the would-be final value of this global
9781 // symbol.
9782 const Symbol_table* symtab = relinfo->symtab;
9783 const Sized_symbol<32>* sized_symbol =
9784 symtab->get_sized_symbol<32>(gsym);
9785 Symbol_table::Compute_final_value_status status;
9786 Arm_address value =
9787 symtab->compute_final_value<32>(sized_symbol, &status);
9788
9789 // Skip this if the symbol has not output section.
9790 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
9791 continue;
9792
9793 symval.set_output_value(value);
9794 psymval = &symval;
9795 }
9796
9797 // If symbol is a section symbol, we don't know the actual type of
9798 // destination. Give up.
9799 if (psymval->is_section_symbol())
9800 continue;
9801
9802 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
2ea97941 9803 addend, view_address + offset);
eb44217c
DK
9804 }
9805}
9806
9807// Scan an input section for stub generation.
9808
9809template<bool big_endian>
9810void
9811Target_arm<big_endian>::scan_section_for_stubs(
9812 const Relocate_info<32, big_endian>* relinfo,
9813 unsigned int sh_type,
9814 const unsigned char* prelocs,
9815 size_t reloc_count,
9816 Output_section* output_section,
9817 bool needs_special_offset_handling,
9818 const unsigned char* view,
9819 Arm_address view_address,
9820 section_size_type view_size)
9821{
9822 if (sh_type == elfcpp::SHT_REL)
9823 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
9824 relinfo,
9825 prelocs,
9826 reloc_count,
9827 output_section,
9828 needs_special_offset_handling,
9829 view,
9830 view_address,
9831 view_size);
9832 else if (sh_type == elfcpp::SHT_RELA)
9833 // We do not support RELA type relocations yet. This is provided for
9834 // completeness.
9835 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
9836 relinfo,
9837 prelocs,
9838 reloc_count,
9839 output_section,
9840 needs_special_offset_handling,
9841 view,
9842 view_address,
9843 view_size);
9844 else
9845 gold_unreachable();
9846}
9847
9848// Group input sections for stub generation.
9849//
9850// We goup input sections in an output sections so that the total size,
9851// including any padding space due to alignment is smaller than GROUP_SIZE
9852// unless the only input section in group is bigger than GROUP_SIZE already.
9853// Then an ARM stub table is created to follow the last input section
9854// in group. For each group an ARM stub table is created an is placed
9855// after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
9856// extend the group after the stub table.
9857
9858template<bool big_endian>
9859void
9860Target_arm<big_endian>::group_sections(
2ea97941 9861 Layout* layout,
eb44217c
DK
9862 section_size_type group_size,
9863 bool stubs_always_after_branch)
9864{
9865 // Group input sections and insert stub table
9866 Layout::Section_list section_list;
2ea97941 9867 layout->get_allocated_sections(&section_list);
eb44217c
DK
9868 for (Layout::Section_list::const_iterator p = section_list.begin();
9869 p != section_list.end();
9870 ++p)
9871 {
9872 Arm_output_section<big_endian>* output_section =
9873 Arm_output_section<big_endian>::as_arm_output_section(*p);
9874 output_section->group_sections(group_size, stubs_always_after_branch,
9875 this);
9876 }
9877}
9878
9879// Relaxation hook. This is where we do stub generation.
9880
9881template<bool big_endian>
9882bool
9883Target_arm<big_endian>::do_relax(
9884 int pass,
9885 const Input_objects* input_objects,
9886 Symbol_table* symtab,
2ea97941 9887 Layout* layout)
eb44217c
DK
9888{
9889 // No need to generate stubs if this is a relocatable link.
9890 gold_assert(!parameters->options().relocatable());
9891
9892 // If this is the first pass, we need to group input sections into
9893 // stub groups.
2b328d4e 9894 bool done_exidx_fixup = false;
eb44217c
DK
9895 if (pass == 1)
9896 {
9897 // Determine the stub group size. The group size is the absolute
9898 // value of the parameter --stub-group-size. If --stub-group-size
9899 // is passed a negative value, we restict stubs to be always after
9900 // the stubbed branches.
9901 int32_t stub_group_size_param =
9902 parameters->options().stub_group_size();
9903 bool stubs_always_after_branch = stub_group_size_param < 0;
9904 section_size_type stub_group_size = abs(stub_group_size_param);
9905
44272192
DK
9906 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
9907 // page as the first half of a 32-bit branch straddling two 4K pages.
9908 // This is a crude way of enforcing that.
9909 if (this->fix_cortex_a8_)
9910 stubs_always_after_branch = true;
9911
eb44217c
DK
9912 if (stub_group_size == 1)
9913 {
9914 // Default value.
9915 // Thumb branch range is +-4MB has to be used as the default
9916 // maximum size (a given section can contain both ARM and Thumb
9917 // code, so the worst case has to be taken into account).
9918 //
9919 // This value is 24K less than that, which allows for 2025
9920 // 12-byte stubs. If we exceed that, then we will fail to link.
9921 // The user will have to relink with an explicit group size
9922 // option.
9923 stub_group_size = 4170000;
9924 }
9925
2ea97941 9926 group_sections(layout, stub_group_size, stubs_always_after_branch);
2b328d4e
DK
9927
9928 // Also fix .ARM.exidx section coverage.
9929 Output_section* os = layout->find_output_section(".ARM.exidx");
9930 if (os != NULL && os->type() == elfcpp::SHT_ARM_EXIDX)
9931 {
9932 Arm_output_section<big_endian>* exidx_output_section =
9933 Arm_output_section<big_endian>::as_arm_output_section(os);
9934 this->fix_exidx_coverage(layout, exidx_output_section, symtab);
9935 done_exidx_fixup = true;
9936 }
eb44217c
DK
9937 }
9938
44272192
DK
9939 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
9940 // beginning of each relaxation pass, just blow away all the stubs.
9941 // Alternatively, we could selectively remove only the stubs and reloc
9942 // information for code sections that have moved since the last pass.
9943 // That would require more book-keeping.
eb44217c 9944 typedef typename Stub_table_list::iterator Stub_table_iterator;
a120bc7f
DK
9945 if (this->fix_cortex_a8_)
9946 {
9947 // Clear all Cortex-A8 reloc information.
9948 for (typename Cortex_a8_relocs_info::const_iterator p =
9949 this->cortex_a8_relocs_info_.begin();
9950 p != this->cortex_a8_relocs_info_.end();
9951 ++p)
9952 delete p->second;
9953 this->cortex_a8_relocs_info_.clear();
44272192
DK
9954
9955 // Remove all Cortex-A8 stubs.
9956 for (Stub_table_iterator sp = this->stub_tables_.begin();
9957 sp != this->stub_tables_.end();
9958 ++sp)
9959 (*sp)->remove_all_cortex_a8_stubs();
a120bc7f
DK
9960 }
9961
44272192 9962 // Scan relocs for relocation stubs
eb44217c
DK
9963 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
9964 op != input_objects->relobj_end();
9965 ++op)
9966 {
9967 Arm_relobj<big_endian>* arm_relobj =
9968 Arm_relobj<big_endian>::as_arm_relobj(*op);
2ea97941 9969 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
eb44217c
DK
9970 }
9971
2fb7225c
DK
9972 // Check all stub tables to see if any of them have their data sizes
9973 // or addresses alignments changed. These are the only things that
9974 // matter.
eb44217c 9975 bool any_stub_table_changed = false;
8923b24c 9976 Unordered_set<const Output_section*> sections_needing_adjustment;
eb44217c
DK
9977 for (Stub_table_iterator sp = this->stub_tables_.begin();
9978 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
9979 ++sp)
9980 {
2fb7225c 9981 if ((*sp)->update_data_size_and_addralign())
8923b24c
DK
9982 {
9983 // Update data size of stub table owner.
9984 Arm_input_section<big_endian>* owner = (*sp)->owner();
9985 uint64_t address = owner->address();
9986 off_t offset = owner->offset();
9987 owner->reset_address_and_file_offset();
9988 owner->set_address_and_file_offset(address, offset);
9989
9990 sections_needing_adjustment.insert(owner->output_section());
9991 any_stub_table_changed = true;
9992 }
9993 }
9994
9995 // Output_section_data::output_section() returns a const pointer but we
9996 // need to update output sections, so we record all output sections needing
9997 // update above and scan the sections here to find out what sections need
9998 // to be updated.
9999 for(Layout::Section_list::const_iterator p = layout->section_list().begin();
10000 p != layout->section_list().end();
10001 ++p)
10002 {
10003 if (sections_needing_adjustment.find(*p)
10004 != sections_needing_adjustment.end())
10005 (*p)->set_section_offsets_need_adjustment();
eb44217c
DK
10006 }
10007
2b328d4e
DK
10008 // Stop relaxation if no EXIDX fix-up and no stub table change.
10009 bool continue_relaxation = done_exidx_fixup || any_stub_table_changed;
10010
2fb7225c 10011 // Finalize the stubs in the last relaxation pass.
2b328d4e 10012 if (!continue_relaxation)
e7eca48c
DK
10013 {
10014 for (Stub_table_iterator sp = this->stub_tables_.begin();
10015 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
10016 ++sp)
10017 (*sp)->finalize_stubs();
10018
10019 // Update output local symbol counts of objects if necessary.
10020 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
10021 op != input_objects->relobj_end();
10022 ++op)
10023 {
10024 Arm_relobj<big_endian>* arm_relobj =
10025 Arm_relobj<big_endian>::as_arm_relobj(*op);
10026
10027 // Update output local symbol counts. We need to discard local
10028 // symbols defined in parts of input sections that are discarded by
10029 // relaxation.
10030 if (arm_relobj->output_local_symbol_count_needs_update())
10031 arm_relobj->update_output_local_symbol_count();
10032 }
10033 }
2fb7225c 10034
2b328d4e 10035 return continue_relaxation;
eb44217c
DK
10036}
10037
43d12afe
DK
10038// Relocate a stub.
10039
10040template<bool big_endian>
10041void
10042Target_arm<big_endian>::relocate_stub(
2fb7225c 10043 Stub* stub,
43d12afe
DK
10044 const Relocate_info<32, big_endian>* relinfo,
10045 Output_section* output_section,
10046 unsigned char* view,
10047 Arm_address address,
10048 section_size_type view_size)
10049{
10050 Relocate relocate;
2ea97941
ILT
10051 const Stub_template* stub_template = stub->stub_template();
10052 for (size_t i = 0; i < stub_template->reloc_count(); i++)
43d12afe 10053 {
2ea97941
ILT
10054 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
10055 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
43d12afe
DK
10056
10057 unsigned int r_type = insn->r_type();
2ea97941 10058 section_size_type reloc_offset = stub_template->reloc_offset(i);
43d12afe
DK
10059 section_size_type reloc_size = insn->size();
10060 gold_assert(reloc_offset + reloc_size <= view_size);
10061
10062 // This is the address of the stub destination.
41263c05 10063 Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
43d12afe
DK
10064 Symbol_value<32> symval;
10065 symval.set_output_value(target);
10066
10067 // Synthesize a fake reloc just in case. We don't have a symbol so
10068 // we use 0.
10069 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
10070 memset(reloc_buffer, 0, sizeof(reloc_buffer));
10071 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
10072 reloc_write.put_r_offset(reloc_offset);
10073 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
10074 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
10075
10076 relocate.relocate(relinfo, this, output_section,
10077 this->fake_relnum_for_stubs, rel, r_type,
10078 NULL, &symval, view + reloc_offset,
10079 address + reloc_offset, reloc_size);
10080 }
10081}
10082
a0351a69
DK
10083// Determine whether an object attribute tag takes an integer, a
10084// string or both.
10085
10086template<bool big_endian>
10087int
10088Target_arm<big_endian>::do_attribute_arg_type(int tag) const
10089{
10090 if (tag == Object_attribute::Tag_compatibility)
10091 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10092 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
10093 else if (tag == elfcpp::Tag_nodefaults)
10094 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10095 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
10096 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
10097 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
10098 else if (tag < 32)
10099 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
10100 else
10101 return ((tag & 1) != 0
10102 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
10103 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10104}
10105
10106// Reorder attributes.
10107//
10108// The ABI defines that Tag_conformance should be emitted first, and that
10109// Tag_nodefaults should be second (if either is defined). This sets those
10110// two positions, and bumps up the position of all the remaining tags to
10111// compensate.
10112
10113template<bool big_endian>
10114int
10115Target_arm<big_endian>::do_attributes_order(int num) const
10116{
10117 // Reorder the known object attributes in output. We want to move
10118 // Tag_conformance to position 4 and Tag_conformance to position 5
10119 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
10120 if (num == 4)
10121 return elfcpp::Tag_conformance;
10122 if (num == 5)
10123 return elfcpp::Tag_nodefaults;
10124 if ((num - 2) < elfcpp::Tag_nodefaults)
10125 return num - 2;
10126 if ((num - 1) < elfcpp::Tag_conformance)
10127 return num - 1;
10128 return num;
10129}
4a657b0d 10130
44272192
DK
10131// Scan a span of THUMB code for Cortex-A8 erratum.
10132
10133template<bool big_endian>
10134void
10135Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
10136 Arm_relobj<big_endian>* arm_relobj,
10137 unsigned int shndx,
10138 section_size_type span_start,
10139 section_size_type span_end,
10140 const unsigned char* view,
10141 Arm_address address)
10142{
10143 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
10144 //
10145 // The opcode is BLX.W, BL.W, B.W, Bcc.W
10146 // The branch target is in the same 4KB region as the
10147 // first half of the branch.
10148 // The instruction before the branch is a 32-bit
10149 // length non-branch instruction.
10150 section_size_type i = span_start;
10151 bool last_was_32bit = false;
10152 bool last_was_branch = false;
10153 while (i < span_end)
10154 {
10155 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
10156 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
10157 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
10158 bool is_blx = false, is_b = false;
10159 bool is_bl = false, is_bcc = false;
10160
10161 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
10162 if (insn_32bit)
10163 {
10164 // Load the rest of the insn (in manual-friendly order).
10165 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
10166
10167 // Encoding T4: B<c>.W.
10168 is_b = (insn & 0xf800d000U) == 0xf0009000U;
10169 // Encoding T1: BL<c>.W.
10170 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
10171 // Encoding T2: BLX<c>.W.
10172 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
10173 // Encoding T3: B<c>.W (not permitted in IT block).
10174 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
10175 && (insn & 0x07f00000U) != 0x03800000U);
10176 }
10177
10178 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
10179
10180 // If this instruction is a 32-bit THUMB branch that crosses a 4K
10181 // page boundary and it follows 32-bit non-branch instruction,
10182 // we need to work around.
10183 if (is_32bit_branch
10184 && ((address + i) & 0xfffU) == 0xffeU
10185 && last_was_32bit
10186 && !last_was_branch)
10187 {
10188 // Check to see if there is a relocation stub for this branch.
10189 bool force_target_arm = false;
10190 bool force_target_thumb = false;
10191 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
10192 Cortex_a8_relocs_info::const_iterator p =
10193 this->cortex_a8_relocs_info_.find(address + i);
10194
10195 if (p != this->cortex_a8_relocs_info_.end())
10196 {
10197 cortex_a8_reloc = p->second;
10198 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
10199
10200 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
10201 && !target_is_thumb)
10202 force_target_arm = true;
10203 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
10204 && target_is_thumb)
10205 force_target_thumb = true;
10206 }
10207
10208 off_t offset;
10209 Stub_type stub_type = arm_stub_none;
10210
10211 // Check if we have an offending branch instruction.
10212 uint16_t upper_insn = (insn >> 16) & 0xffffU;
10213 uint16_t lower_insn = insn & 0xffffU;
10214 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
10215
10216 if (cortex_a8_reloc != NULL
10217 && cortex_a8_reloc->reloc_stub() != NULL)
10218 // We've already made a stub for this instruction, e.g.
10219 // it's a long branch or a Thumb->ARM stub. Assume that
10220 // stub will suffice to work around the A8 erratum (see
10221 // setting of always_after_branch above).
10222 ;
10223 else if (is_bcc)
10224 {
10225 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
10226 lower_insn);
10227 stub_type = arm_stub_a8_veneer_b_cond;
10228 }
10229 else if (is_b || is_bl || is_blx)
10230 {
10231 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
10232 lower_insn);
10233 if (is_blx)
10234 offset &= ~3;
10235
10236 stub_type = (is_blx
10237 ? arm_stub_a8_veneer_blx
10238 : (is_bl
10239 ? arm_stub_a8_veneer_bl
10240 : arm_stub_a8_veneer_b));
10241 }
10242
10243 if (stub_type != arm_stub_none)
10244 {
10245 Arm_address pc_for_insn = address + i + 4;
10246
10247 // The original instruction is a BL, but the target is
10248 // an ARM instruction. If we were not making a stub,
10249 // the BL would have been converted to a BLX. Use the
10250 // BLX stub instead in that case.
10251 if (this->may_use_blx() && force_target_arm
10252 && stub_type == arm_stub_a8_veneer_bl)
10253 {
10254 stub_type = arm_stub_a8_veneer_blx;
10255 is_blx = true;
10256 is_bl = false;
10257 }
10258 // Conversely, if the original instruction was
10259 // BLX but the target is Thumb mode, use the BL stub.
10260 else if (force_target_thumb
10261 && stub_type == arm_stub_a8_veneer_blx)
10262 {
10263 stub_type = arm_stub_a8_veneer_bl;
10264 is_blx = false;
10265 is_bl = true;
10266 }
10267
10268 if (is_blx)
10269 pc_for_insn &= ~3;
10270
10271 // If we found a relocation, use the proper destination,
10272 // not the offset in the (unrelocated) instruction.
10273 // Note this is always done if we switched the stub type above.
10274 if (cortex_a8_reloc != NULL)
10275 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
10276
10277 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
10278
10279 // Add a new stub if destination address in in the same page.
10280 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
10281 {
10282 Cortex_a8_stub* stub =
10283 this->stub_factory_.make_cortex_a8_stub(stub_type,
10284 arm_relobj, shndx,
10285 address + i,
10286 target, insn);
10287 Stub_table<big_endian>* stub_table =
10288 arm_relobj->stub_table(shndx);
10289 gold_assert(stub_table != NULL);
10290 stub_table->add_cortex_a8_stub(address + i, stub);
10291 }
10292 }
10293 }
10294
10295 i += insn_32bit ? 4 : 2;
10296 last_was_32bit = insn_32bit;
10297 last_was_branch = is_32bit_branch;
10298 }
10299}
10300
41263c05
DK
10301// Apply the Cortex-A8 workaround.
10302
10303template<bool big_endian>
10304void
10305Target_arm<big_endian>::apply_cortex_a8_workaround(
10306 const Cortex_a8_stub* stub,
10307 Arm_address stub_address,
10308 unsigned char* insn_view,
10309 Arm_address insn_address)
10310{
10311 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
10312 Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
10313 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
10314 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
10315 off_t branch_offset = stub_address - (insn_address + 4);
10316
10317 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
10318 switch (stub->stub_template()->type())
10319 {
10320 case arm_stub_a8_veneer_b_cond:
10321 gold_assert(!utils::has_overflow<21>(branch_offset));
10322 upper_insn = RelocFuncs::thumb32_cond_branch_upper(upper_insn,
10323 branch_offset);
10324 lower_insn = RelocFuncs::thumb32_cond_branch_lower(lower_insn,
10325 branch_offset);
10326 break;
10327
10328 case arm_stub_a8_veneer_b:
10329 case arm_stub_a8_veneer_bl:
10330 case arm_stub_a8_veneer_blx:
10331 if ((lower_insn & 0x5000U) == 0x4000U)
10332 // For a BLX instruction, make sure that the relocation is
10333 // rounded up to a word boundary. This follows the semantics of
10334 // the instruction which specifies that bit 1 of the target
10335 // address will come from bit 1 of the base address.
10336 branch_offset = (branch_offset + 2) & ~3;
10337
10338 // Put BRANCH_OFFSET back into the insn.
10339 gold_assert(!utils::has_overflow<25>(branch_offset));
10340 upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
10341 lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
10342 break;
10343
10344 default:
10345 gold_unreachable();
10346 }
10347
10348 // Put the relocated value back in the object file:
10349 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
10350 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
10351}
10352
4a657b0d
DK
10353template<bool big_endian>
10354class Target_selector_arm : public Target_selector
10355{
10356 public:
10357 Target_selector_arm()
10358 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
10359 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
10360 { }
10361
10362 Target*
10363 do_instantiate_target()
10364 { return new Target_arm<big_endian>(); }
10365};
10366
2b328d4e
DK
10367// Fix .ARM.exidx section coverage.
10368
10369template<bool big_endian>
10370void
10371Target_arm<big_endian>::fix_exidx_coverage(
10372 Layout* layout,
10373 Arm_output_section<big_endian>* exidx_section,
10374 Symbol_table* symtab)
10375{
10376 // We need to look at all the input sections in output in ascending
10377 // order of of output address. We do that by building a sorted list
10378 // of output sections by addresses. Then we looks at the output sections
10379 // in order. The input sections in an output section are already sorted
10380 // by addresses within the output section.
10381
10382 typedef std::set<Output_section*, output_section_address_less_than>
10383 Sorted_output_section_list;
10384 Sorted_output_section_list sorted_output_sections;
10385 Layout::Section_list section_list;
10386 layout->get_allocated_sections(&section_list);
10387 for (Layout::Section_list::const_iterator p = section_list.begin();
10388 p != section_list.end();
10389 ++p)
10390 {
10391 // We only care about output sections that contain executable code.
10392 if (((*p)->flags() & elfcpp::SHF_EXECINSTR) != 0)
10393 sorted_output_sections.insert(*p);
10394 }
10395
10396 // Go over the output sections in ascending order of output addresses.
10397 typedef typename Arm_output_section<big_endian>::Text_section_list
10398 Text_section_list;
10399 Text_section_list sorted_text_sections;
10400 for(typename Sorted_output_section_list::iterator p =
10401 sorted_output_sections.begin();
10402 p != sorted_output_sections.end();
10403 ++p)
10404 {
10405 Arm_output_section<big_endian>* arm_output_section =
10406 Arm_output_section<big_endian>::as_arm_output_section(*p);
10407 arm_output_section->append_text_sections_to_list(&sorted_text_sections);
10408 }
10409
10410 exidx_section->fix_exidx_coverage(sorted_text_sections, symtab);
10411}
10412
4a657b0d
DK
10413Target_selector_arm<false> target_selector_arm;
10414Target_selector_arm<true> target_selector_armbe;
10415
10416} // End anonymous namespace.
This page took 0.647202 seconds and 4 git commands to generate.