2009-10-30 Doug Kwan <dougkwan@google.com>
[deliverable/binutils-gdb.git] / gold / arm.cc
1 // arm.cc -- arm target support for gold.
2
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
6 // This file also contains borrowed and adapted code from
7 // bfd/elf32-arm.c.
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>
32 #include <algorithm>
33
34 #include "elfcpp.h"
35 #include "parameters.h"
36 #include "reloc.h"
37 #include "arm.h"
38 #include "object.h"
39 #include "symtab.h"
40 #include "layout.h"
41 #include "output.h"
42 #include "copy-relocs.h"
43 #include "target.h"
44 #include "target-reloc.h"
45 #include "target-select.h"
46 #include "tls.h"
47 #include "defstd.h"
48 #include "gc.h"
49
50 namespace
51 {
52
53 using namespace gold;
54
55 template<bool big_endian>
56 class Output_data_plt_arm;
57
58 template<bool big_endian>
59 class Stub_table;
60
61 template<bool big_endian>
62 class Arm_input_section;
63
64 template<bool big_endian>
65 class Arm_output_section;
66
67 template<bool big_endian>
68 class Arm_relobj;
69
70 template<bool big_endian>
71 class Target_arm;
72
73 // For convenience.
74 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
75
76 // Maximum branch offsets for ARM, THUMB and THUMB2.
77 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
78 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
79 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
80 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
81 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
82 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
83
84 // The arm target class.
85 //
86 // This is a very simple port of gold for ARM-EABI. It is intended for
87 // supporting Android only for the time being. Only these relocation types
88 // are supported.
89 //
90 // R_ARM_NONE
91 // R_ARM_ABS32
92 // R_ARM_ABS32_NOI
93 // R_ARM_ABS16
94 // R_ARM_ABS12
95 // R_ARM_ABS8
96 // R_ARM_THM_ABS5
97 // R_ARM_BASE_ABS
98 // R_ARM_REL32
99 // R_ARM_THM_CALL
100 // R_ARM_COPY
101 // R_ARM_GLOB_DAT
102 // R_ARM_BASE_PREL
103 // R_ARM_JUMP_SLOT
104 // R_ARM_RELATIVE
105 // R_ARM_GOTOFF32
106 // R_ARM_GOT_BREL
107 // R_ARM_GOT_PREL
108 // R_ARM_PLT32
109 // R_ARM_CALL
110 // R_ARM_JUMP24
111 // R_ARM_TARGET1
112 // R_ARM_PREL31
113 // R_ARM_ABS8
114 // R_ARM_MOVW_ABS_NC
115 // R_ARM_MOVT_ABS
116 // R_ARM_THM_MOVW_ABS_NC
117 // R_ARM_THM_MOVT_ABS
118 // R_ARM_MOVW_PREL_NC
119 // R_ARM_MOVT_PREL
120 // R_ARM_THM_MOVW_PREL_NC
121 // R_ARM_THM_MOVT_PREL
122 //
123 // TODOs:
124 // - Generate various branch stubs.
125 // - Support interworking.
126 // - Define section symbols __exidx_start and __exidx_stop.
127 // - Support more relocation types as needed.
128 // - Make PLTs more flexible for different architecture features like
129 // Thumb-2 and BE8.
130 // There are probably a lot more.
131
132 // Instruction template class. This class is similar to the insn_sequence
133 // struct in bfd/elf32-arm.c.
134
135 class Insn_template
136 {
137 public:
138 // Types of instruction templates.
139 enum Type
140 {
141 THUMB16_TYPE = 1,
142 THUMB32_TYPE,
143 ARM_TYPE,
144 DATA_TYPE
145 };
146
147 // Factory methods to create instrunction templates in different formats.
148
149 static const Insn_template
150 thumb16_insn(uint32_t data)
151 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
152
153 // A bit of a hack. A Thumb conditional branch, in which the proper
154 // condition is inserted when we build the stub.
155 static const Insn_template
156 thumb16_bcond_insn(uint32_t data)
157 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 1); }
158
159 static const Insn_template
160 thumb32_insn(uint32_t data)
161 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
162
163 static const Insn_template
164 thumb32_b_insn(uint32_t data, int reloc_addend)
165 {
166 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
167 reloc_addend);
168 }
169
170 static const Insn_template
171 arm_insn(uint32_t data)
172 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
173
174 static const Insn_template
175 arm_rel_insn(unsigned data, int reloc_addend)
176 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
177
178 static const Insn_template
179 data_word(unsigned data, unsigned int r_type, int reloc_addend)
180 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
181
182 // Accessors. This class is used for read-only objects so no modifiers
183 // are provided.
184
185 uint32_t
186 data() const
187 { return this->data_; }
188
189 // Return the instruction sequence type of this.
190 Type
191 type() const
192 { return this->type_; }
193
194 // Return the ARM relocation type of this.
195 unsigned int
196 r_type() const
197 { return this->r_type_; }
198
199 int32_t
200 reloc_addend() const
201 { return this->reloc_addend_; }
202
203 // Return size of instrunction template in bytes.
204 size_t
205 size() const;
206
207 // Return byte-alignment of instrunction template.
208 unsigned
209 alignment() const;
210
211 private:
212 // We make the constructor private to ensure that only the factory
213 // methods are used.
214 inline
215 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
216 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
217 { }
218
219 // Instruction specific data. This is used to store information like
220 // some of the instruction bits.
221 uint32_t data_;
222 // Instruction template type.
223 Type type_;
224 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
225 unsigned int r_type_;
226 // Relocation addend.
227 int32_t reloc_addend_;
228 };
229
230 // Macro for generating code to stub types. One entry per long/short
231 // branch stub
232
233 #define DEF_STUBS \
234 DEF_STUB(long_branch_any_any) \
235 DEF_STUB(long_branch_v4t_arm_thumb) \
236 DEF_STUB(long_branch_thumb_only) \
237 DEF_STUB(long_branch_v4t_thumb_thumb) \
238 DEF_STUB(long_branch_v4t_thumb_arm) \
239 DEF_STUB(short_branch_v4t_thumb_arm) \
240 DEF_STUB(long_branch_any_arm_pic) \
241 DEF_STUB(long_branch_any_thumb_pic) \
242 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
243 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
244 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
245 DEF_STUB(long_branch_thumb_only_pic) \
246 DEF_STUB(a8_veneer_b_cond) \
247 DEF_STUB(a8_veneer_b) \
248 DEF_STUB(a8_veneer_bl) \
249 DEF_STUB(a8_veneer_blx)
250
251 // Stub types.
252
253 #define DEF_STUB(x) arm_stub_##x,
254 typedef enum
255 {
256 arm_stub_none,
257 DEF_STUBS
258
259 // First reloc stub type.
260 arm_stub_reloc_first = arm_stub_long_branch_any_any,
261 // Last reloc stub type.
262 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
263
264 // First Cortex-A8 stub type.
265 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
266 // Last Cortex-A8 stub type.
267 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
268
269 // Last stub type.
270 arm_stub_type_last = arm_stub_a8_veneer_blx
271 } Stub_type;
272 #undef DEF_STUB
273
274 // Stub template class. Templates are meant to be read-only objects.
275 // A stub template for a stub type contains all read-only attributes
276 // common to all stubs of the same type.
277
278 class Stub_template
279 {
280 public:
281 Stub_template(Stub_type, const Insn_template*, size_t);
282
283 ~Stub_template()
284 { }
285
286 // Return stub type.
287 Stub_type
288 type() const
289 { return this->type_; }
290
291 // Return an array of instruction templates.
292 const Insn_template*
293 insns() const
294 { return this->insns_; }
295
296 // Return size of template in number of instructions.
297 size_t
298 insn_count() const
299 { return this->insn_count_; }
300
301 // Return size of template in bytes.
302 size_t
303 size() const
304 { return this->size_; }
305
306 // Return alignment of the stub template.
307 unsigned
308 alignment() const
309 { return this->alignment_; }
310
311 // Return whether entry point is in thumb mode.
312 bool
313 entry_in_thumb_mode() const
314 { return this->entry_in_thumb_mode_; }
315
316 // Return number of relocations in this template.
317 size_t
318 reloc_count() const
319 { return this->relocs_.size(); }
320
321 // Return index of the I-th instruction with relocation.
322 size_t
323 reloc_insn_index(size_t i) const
324 {
325 gold_assert(i < this->relocs_.size());
326 return this->relocs_[i].first;
327 }
328
329 // Return the offset of the I-th instruction with relocation from the
330 // beginning of the stub.
331 section_size_type
332 reloc_offset(size_t i) const
333 {
334 gold_assert(i < this->relocs_.size());
335 return this->relocs_[i].second;
336 }
337
338 private:
339 // This contains information about an instruction template with a relocation
340 // and its offset from start of stub.
341 typedef std::pair<size_t, section_size_type> Reloc;
342
343 // A Stub_template may not be copied. We want to share templates as much
344 // as possible.
345 Stub_template(const Stub_template&);
346 Stub_template& operator=(const Stub_template&);
347
348 // Stub type.
349 Stub_type type_;
350 // Points to an array of Insn_templates.
351 const Insn_template* insns_;
352 // Number of Insn_templates in insns_[].
353 size_t insn_count_;
354 // Size of templated instructions in bytes.
355 size_t size_;
356 // Alignment of templated instructions.
357 unsigned alignment_;
358 // Flag to indicate if entry is in thumb mode.
359 bool entry_in_thumb_mode_;
360 // A table of reloc instruction indices and offsets. We can find these by
361 // looking at the instruction templates but we pre-compute and then stash
362 // them here for speed.
363 std::vector<Reloc> relocs_;
364 };
365
366 //
367 // A class for code stubs. This is a base class for different type of
368 // stubs used in the ARM target.
369 //
370
371 class Stub
372 {
373 private:
374 static const section_offset_type invalid_offset =
375 static_cast<section_offset_type>(-1);
376
377 public:
378 Stub(const Stub_template* stub_template)
379 : stub_template_(stub_template), offset_(invalid_offset)
380 { }
381
382 virtual
383 ~Stub()
384 { }
385
386 // Return the stub template.
387 const Stub_template*
388 stub_template() const
389 { return this->stub_template_; }
390
391 // Return offset of code stub from beginning of its containing stub table.
392 section_offset_type
393 offset() const
394 {
395 gold_assert(this->offset_ != invalid_offset);
396 return this->offset_;
397 }
398
399 // Set offset of code stub from beginning of its containing stub table.
400 void
401 set_offset(section_offset_type offset)
402 { this->offset_ = offset; }
403
404 // Return the relocation target address of the i-th relocation in the
405 // stub. This must be defined in a child class.
406 Arm_address
407 reloc_target(size_t i)
408 { return this->do_reloc_target(i); }
409
410 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
411 void
412 write(unsigned char* view, section_size_type view_size, bool big_endian)
413 { this->do_write(view, view_size, big_endian); }
414
415 protected:
416 // This must be defined in the child class.
417 virtual Arm_address
418 do_reloc_target(size_t) = 0;
419
420 // This must be defined in the child class.
421 virtual void
422 do_write(unsigned char*, section_size_type, bool) = 0;
423
424 private:
425 // Its template.
426 const Stub_template* stub_template_;
427 // Offset within the section of containing this stub.
428 section_offset_type offset_;
429 };
430
431 // Reloc stub class. These are stubs we use to fix up relocation because
432 // of limited branch ranges.
433
434 class Reloc_stub : public Stub
435 {
436 public:
437 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
438 // We assume we never jump to this address.
439 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
440
441 // Return destination address.
442 Arm_address
443 destination_address() const
444 {
445 gold_assert(this->destination_address_ != this->invalid_address);
446 return this->destination_address_;
447 }
448
449 // Set destination address.
450 void
451 set_destination_address(Arm_address address)
452 {
453 gold_assert(address != this->invalid_address);
454 this->destination_address_ = address;
455 }
456
457 // Reset destination address.
458 void
459 reset_destination_address()
460 { this->destination_address_ = this->invalid_address; }
461
462 // Determine stub type for a branch of a relocation of R_TYPE going
463 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
464 // the branch target is a thumb instruction. TARGET is used for look
465 // up ARM-specific linker settings.
466 static Stub_type
467 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
468 Arm_address branch_target, bool target_is_thumb);
469
470 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
471 // and an addend. Since we treat global and local symbol differently, we
472 // use a Symbol object for a global symbol and a object-index pair for
473 // a local symbol.
474 class Key
475 {
476 public:
477 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
478 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
479 // and R_SYM must not be invalid_index.
480 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
481 unsigned int r_sym, int32_t addend)
482 : stub_type_(stub_type), addend_(addend)
483 {
484 if (symbol != NULL)
485 {
486 this->r_sym_ = Reloc_stub::invalid_index;
487 this->u_.symbol = symbol;
488 }
489 else
490 {
491 gold_assert(relobj != NULL && r_sym != invalid_index);
492 this->r_sym_ = r_sym;
493 this->u_.relobj = relobj;
494 }
495 }
496
497 ~Key()
498 { }
499
500 // Accessors: Keys are meant to be read-only object so no modifiers are
501 // provided.
502
503 // Return stub type.
504 Stub_type
505 stub_type() const
506 { return this->stub_type_; }
507
508 // Return the local symbol index or invalid_index.
509 unsigned int
510 r_sym() const
511 { return this->r_sym_; }
512
513 // Return the symbol if there is one.
514 const Symbol*
515 symbol() const
516 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
517
518 // Return the relobj if there is one.
519 const Relobj*
520 relobj() const
521 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
522
523 // Whether this equals to another key k.
524 bool
525 eq(const Key& k) const
526 {
527 return ((this->stub_type_ == k.stub_type_)
528 && (this->r_sym_ == k.r_sym_)
529 && ((this->r_sym_ != Reloc_stub::invalid_index)
530 ? (this->u_.relobj == k.u_.relobj)
531 : (this->u_.symbol == k.u_.symbol))
532 && (this->addend_ == k.addend_));
533 }
534
535 // Return a hash value.
536 size_t
537 hash_value() const
538 {
539 return (this->stub_type_
540 ^ this->r_sym_
541 ^ gold::string_hash<char>(
542 (this->r_sym_ != Reloc_stub::invalid_index)
543 ? this->u_.relobj->name().c_str()
544 : this->u_.symbol->name())
545 ^ this->addend_);
546 }
547
548 // Functors for STL associative containers.
549 struct hash
550 {
551 size_t
552 operator()(const Key& k) const
553 { return k.hash_value(); }
554 };
555
556 struct equal_to
557 {
558 bool
559 operator()(const Key& k1, const Key& k2) const
560 { return k1.eq(k2); }
561 };
562
563 // Name of key. This is mainly for debugging.
564 std::string
565 name() const;
566
567 private:
568 // Stub type.
569 Stub_type stub_type_;
570 // If this is a local symbol, this is the index in the defining object.
571 // Otherwise, it is invalid_index for a global symbol.
572 unsigned int r_sym_;
573 // If r_sym_ is invalid index. This points to a global symbol.
574 // Otherwise, this points a relobj. We used the unsized and target
575 // independent Symbol and Relobj classes instead of Arm_symbol and
576 // Arm_relobj. This is done to avoid making the stub class a template
577 // as most of the stub machinery is endianity-neutral. However, it
578 // may require a bit of casting done by users of this class.
579 union
580 {
581 const Symbol* symbol;
582 const Relobj* relobj;
583 } u_;
584 // Addend associated with a reloc.
585 int32_t addend_;
586 };
587
588 protected:
589 // Reloc_stubs are created via a stub factory. So these are protected.
590 Reloc_stub(const Stub_template* stub_template)
591 : Stub(stub_template), destination_address_(invalid_address)
592 { }
593
594 ~Reloc_stub()
595 { }
596
597 friend class Stub_factory;
598
599 private:
600 // Return the relocation target address of the i-th relocation in the
601 // stub.
602 Arm_address
603 do_reloc_target(size_t i)
604 {
605 // All reloc stub have only one relocation.
606 gold_assert(i == 0);
607 return this->destination_address_;
608 }
609
610 // A template to implement do_write below.
611 template<bool big_endian>
612 void inline
613 do_fixed_endian_write(unsigned char*, section_size_type);
614
615 // Write a stub.
616 void
617 do_write(unsigned char* view, section_size_type view_size, bool big_endian);
618
619 // Address of destination.
620 Arm_address destination_address_;
621 };
622
623 // Stub factory class.
624
625 class Stub_factory
626 {
627 public:
628 // Return the unique instance of this class.
629 static const Stub_factory&
630 get_instance()
631 {
632 static Stub_factory singleton;
633 return singleton;
634 }
635
636 // Make a relocation stub.
637 Reloc_stub*
638 make_reloc_stub(Stub_type stub_type) const
639 {
640 gold_assert(stub_type >= arm_stub_reloc_first
641 && stub_type <= arm_stub_reloc_last);
642 return new Reloc_stub(this->stub_templates_[stub_type]);
643 }
644
645 private:
646 // Constructor and destructor are protected since we only return a single
647 // instance created in Stub_factory::get_instance().
648
649 Stub_factory();
650
651 // A Stub_factory may not be copied since it is a singleton.
652 Stub_factory(const Stub_factory&);
653 Stub_factory& operator=(Stub_factory&);
654
655 // Stub templates. These are initialized in the constructor.
656 const Stub_template* stub_templates_[arm_stub_type_last+1];
657 };
658
659 // A class to hold stubs for the ARM target.
660
661 template<bool big_endian>
662 class Stub_table : public Output_data
663 {
664 public:
665 Stub_table(Arm_input_section<big_endian>* owner)
666 : Output_data(), addralign_(1), owner_(owner), has_been_changed_(false),
667 reloc_stubs_()
668 { }
669
670 ~Stub_table()
671 { }
672
673 // Owner of this stub table.
674 Arm_input_section<big_endian>*
675 owner() const
676 { return this->owner_; }
677
678 // Whether this stub table is empty.
679 bool
680 empty() const
681 { return this->reloc_stubs_.empty(); }
682
683 // Whether this has been changed.
684 bool
685 has_been_changed() const
686 { return this->has_been_changed_; }
687
688 // Set the has-been-changed flag.
689 void
690 set_has_been_changed(bool value)
691 { this->has_been_changed_ = value; }
692
693 // Return the current data size.
694 off_t
695 current_data_size() const
696 { return this->current_data_size_for_child(); }
697
698 // Add a STUB with using KEY. Caller is reponsible for avoid adding
699 // if already a STUB with the same key has been added.
700 void
701 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key);
702
703 // Look up a relocation stub using KEY. Return NULL if there is none.
704 Reloc_stub*
705 find_reloc_stub(const Reloc_stub::Key& key) const
706 {
707 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
708 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
709 }
710
711 // Relocate stubs in this stub table.
712 void
713 relocate_stubs(const Relocate_info<32, big_endian>*,
714 Target_arm<big_endian>*, Output_section*,
715 unsigned char*, Arm_address, section_size_type);
716
717 protected:
718 // Write out section contents.
719 void
720 do_write(Output_file*);
721
722 // Return the required alignment.
723 uint64_t
724 do_addralign() const
725 { return this->addralign_; }
726
727 // Finalize data size.
728 void
729 set_final_data_size()
730 { this->set_data_size(this->current_data_size_for_child()); }
731
732 // Reset address and file offset.
733 void
734 do_reset_address_and_file_offset();
735
736 private:
737 // Unordered map of stubs.
738 typedef
739 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
740 Reloc_stub::Key::equal_to>
741 Reloc_stub_map;
742
743 // Address alignment
744 uint64_t addralign_;
745 // Owner of this stub table.
746 Arm_input_section<big_endian>* owner_;
747 // This is set to true during relaxiong if the size of the stub table
748 // has been changed.
749 bool has_been_changed_;
750 // The relocation stubs.
751 Reloc_stub_map reloc_stubs_;
752 };
753
754 // A class to wrap an ordinary input section containing executable code.
755
756 template<bool big_endian>
757 class Arm_input_section : public Output_relaxed_input_section
758 {
759 public:
760 Arm_input_section(Relobj* relobj, unsigned int shndx)
761 : Output_relaxed_input_section(relobj, shndx, 1),
762 original_addralign_(1), original_size_(0), stub_table_(NULL)
763 { }
764
765 ~Arm_input_section()
766 { }
767
768 // Initialize.
769 void
770 init();
771
772 // Whether this is a stub table owner.
773 bool
774 is_stub_table_owner() const
775 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
776
777 // Return the stub table.
778 Stub_table<big_endian>*
779 stub_table() const
780 { return this->stub_table_; }
781
782 // Set the stub_table.
783 void
784 set_stub_table(Stub_table<big_endian>* stub_table)
785 { this->stub_table_ = stub_table; }
786
787 // Downcast a base pointer to an Arm_input_section pointer. This is
788 // not type-safe but we only use Arm_input_section not the base class.
789 static Arm_input_section<big_endian>*
790 as_arm_input_section(Output_relaxed_input_section* poris)
791 { return static_cast<Arm_input_section<big_endian>*>(poris); }
792
793 protected:
794 // Write data to output file.
795 void
796 do_write(Output_file*);
797
798 // Return required alignment of this.
799 uint64_t
800 do_addralign() const
801 {
802 if (this->is_stub_table_owner())
803 return std::max(this->stub_table_->addralign(),
804 this->original_addralign_);
805 else
806 return this->original_addralign_;
807 }
808
809 // Finalize data size.
810 void
811 set_final_data_size();
812
813 // Reset address and file offset.
814 void
815 do_reset_address_and_file_offset();
816
817 // Output offset.
818 bool
819 do_output_offset(const Relobj* object, unsigned int shndx,
820 section_offset_type offset,
821 section_offset_type* poutput) const
822 {
823 if ((object == this->relobj())
824 && (shndx == this->shndx())
825 && (offset >= 0)
826 && (convert_types<uint64_t, section_offset_type>(offset)
827 <= this->original_size_))
828 {
829 *poutput = offset;
830 return true;
831 }
832 else
833 return false;
834 }
835
836 private:
837 // Copying is not allowed.
838 Arm_input_section(const Arm_input_section&);
839 Arm_input_section& operator=(const Arm_input_section&);
840
841 // Address alignment of the original input section.
842 uint64_t original_addralign_;
843 // Section size of the original input section.
844 uint64_t original_size_;
845 // Stub table.
846 Stub_table<big_endian>* stub_table_;
847 };
848
849 // Arm output section class. This is defined mainly to add a number of
850 // stub generation methods.
851
852 template<bool big_endian>
853 class Arm_output_section : public Output_section
854 {
855 public:
856 Arm_output_section(const char* name, elfcpp::Elf_Word type,
857 elfcpp::Elf_Xword flags)
858 : Output_section(name, type, flags)
859 { }
860
861 ~Arm_output_section()
862 { }
863
864 // Group input sections for stub generation.
865 void
866 group_sections(section_size_type, bool, Target_arm<big_endian>*);
867
868 // Downcast a base pointer to an Arm_output_section pointer. This is
869 // not type-safe but we only use Arm_output_section not the base class.
870 static Arm_output_section<big_endian>*
871 as_arm_output_section(Output_section* os)
872 { return static_cast<Arm_output_section<big_endian>*>(os); }
873
874 private:
875 // For convenience.
876 typedef Output_section::Input_section Input_section;
877 typedef Output_section::Input_section_list Input_section_list;
878
879 // Create a stub group.
880 void create_stub_group(Input_section_list::const_iterator,
881 Input_section_list::const_iterator,
882 Input_section_list::const_iterator,
883 Target_arm<big_endian>*,
884 std::vector<Output_relaxed_input_section*>*);
885 };
886
887 // Arm_relobj class.
888
889 template<bool big_endian>
890 class Arm_relobj : public Sized_relobj<32, big_endian>
891 {
892 public:
893 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
894
895 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
896 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
897 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
898 stub_tables_(), local_symbol_is_thumb_function_()
899 { }
900
901 ~Arm_relobj()
902 { }
903
904 // Return the stub table of the SHNDX-th section if there is one.
905 Stub_table<big_endian>*
906 stub_table(unsigned int shndx) const
907 {
908 gold_assert(shndx < this->stub_tables_.size());
909 return this->stub_tables_[shndx];
910 }
911
912 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
913 void
914 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
915 {
916 gold_assert(shndx < this->stub_tables_.size());
917 this->stub_tables_[shndx] = stub_table;
918 }
919
920 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
921 // index. This is only valid after do_count_local_symbol is called.
922 bool
923 local_symbol_is_thumb_function(unsigned int r_sym) const
924 {
925 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
926 return this->local_symbol_is_thumb_function_[r_sym];
927 }
928
929 // Scan all relocation sections for stub generation.
930 void
931 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
932 const Layout*);
933
934 // Convert regular input section with index SHNDX to a relaxed section.
935 void
936 convert_input_section_to_relaxed_section(unsigned shndx)
937 {
938 // The stubs have relocations and we need to process them after writing
939 // out the stubs. So relocation now must follow section write.
940 this->invalidate_section_offset(shndx);
941 this->set_relocs_must_follow_section_writes();
942 }
943
944 // Downcast a base pointer to an Arm_relobj pointer. This is
945 // not type-safe but we only use Arm_relobj not the base class.
946 static Arm_relobj<big_endian>*
947 as_arm_relobj(Relobj* relobj)
948 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
949
950 // Processor-specific flags in ELF file header. This is valid only after
951 // reading symbols.
952 elfcpp::Elf_Word
953 processor_specific_flags() const
954 { return this->processor_specific_flags_; }
955
956 protected:
957 // Post constructor setup.
958 void
959 do_setup()
960 {
961 // Call parent's setup method.
962 Sized_relobj<32, big_endian>::do_setup();
963
964 // Initialize look-up tables.
965 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
966 this->stub_tables_.swap(empty_stub_table_list);
967 }
968
969 // Count the local symbols.
970 void
971 do_count_local_symbols(Stringpool_template<char>*,
972 Stringpool_template<char>*);
973
974 void
975 do_relocate_sections(const General_options& options,
976 const Symbol_table* symtab, const Layout* layout,
977 const unsigned char* pshdrs,
978 typename Sized_relobj<32, big_endian>::Views* pivews);
979
980 // Read the symbol information.
981 void
982 do_read_symbols(Read_symbols_data* sd);
983
984 private:
985 // List of stub tables.
986 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
987 Stub_table_list stub_tables_;
988 // Bit vector to tell if a local symbol is a thumb function or not.
989 // This is only valid after do_count_local_symbol is called.
990 std::vector<bool> local_symbol_is_thumb_function_;
991 // processor-specific flags in ELF file header.
992 elfcpp::Elf_Word processor_specific_flags_;
993 };
994
995 // Arm_dynobj class.
996
997 template<bool big_endian>
998 class Arm_dynobj : public Sized_dynobj<32, big_endian>
999 {
1000 public:
1001 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1002 const elfcpp::Ehdr<32, big_endian>& ehdr)
1003 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1004 processor_specific_flags_(0)
1005 { }
1006
1007 ~Arm_dynobj()
1008 { }
1009
1010 // Downcast a base pointer to an Arm_relobj pointer. This is
1011 // not type-safe but we only use Arm_relobj not the base class.
1012 static Arm_dynobj<big_endian>*
1013 as_arm_dynobj(Dynobj* dynobj)
1014 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1015
1016 // Processor-specific flags in ELF file header. This is valid only after
1017 // reading symbols.
1018 elfcpp::Elf_Word
1019 processor_specific_flags() const
1020 { return this->processor_specific_flags_; }
1021
1022 protected:
1023 // Read the symbol information.
1024 void
1025 do_read_symbols(Read_symbols_data* sd);
1026
1027 private:
1028 // processor-specific flags in ELF file header.
1029 elfcpp::Elf_Word processor_specific_flags_;
1030 };
1031
1032 // Functor to read reloc addends during stub generation.
1033
1034 template<int sh_type, bool big_endian>
1035 struct Stub_addend_reader
1036 {
1037 // Return the addend for a relocation of a particular type. Depending
1038 // on whether this is a REL or RELA relocation, read the addend from a
1039 // view or from a Reloc object.
1040 elfcpp::Elf_types<32>::Elf_Swxword
1041 operator()(
1042 unsigned int /* r_type */,
1043 const unsigned char* /* view */,
1044 const typename Reloc_types<sh_type,
1045 32, big_endian>::Reloc& /* reloc */) const
1046 { gold_unreachable(); }
1047 };
1048
1049 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1050
1051 template<bool big_endian>
1052 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1053 {
1054 elfcpp::Elf_types<32>::Elf_Swxword
1055 operator()(
1056 unsigned int,
1057 const unsigned char*,
1058 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1059 };
1060
1061 // Specialized Stub_addend_reader for RELA type relocation sections.
1062 // We currently do not handle RELA type relocation sections but it is trivial
1063 // to implement the addend reader. This is provided for completeness and to
1064 // make it easier to add support for RELA relocation sections in the future.
1065
1066 template<bool big_endian>
1067 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1068 {
1069 elfcpp::Elf_types<32>::Elf_Swxword
1070 operator()(
1071 unsigned int,
1072 const unsigned char*,
1073 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1074 big_endian>::Reloc& reloc) const;
1075 };
1076
1077 // Utilities for manipulating integers of up to 32-bits
1078
1079 namespace utils
1080 {
1081 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1082 // an int32_t. NO_BITS must be between 1 to 32.
1083 template<int no_bits>
1084 static inline int32_t
1085 sign_extend(uint32_t bits)
1086 {
1087 gold_assert(no_bits >= 0 && no_bits <= 32);
1088 if (no_bits == 32)
1089 return static_cast<int32_t>(bits);
1090 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1091 bits &= mask;
1092 uint32_t top_bit = 1U << (no_bits - 1);
1093 int32_t as_signed = static_cast<int32_t>(bits);
1094 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1095 }
1096
1097 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1098 template<int no_bits>
1099 static inline bool
1100 has_overflow(uint32_t bits)
1101 {
1102 gold_assert(no_bits >= 0 && no_bits <= 32);
1103 if (no_bits == 32)
1104 return false;
1105 int32_t max = (1 << (no_bits - 1)) - 1;
1106 int32_t min = -(1 << (no_bits - 1));
1107 int32_t as_signed = static_cast<int32_t>(bits);
1108 return as_signed > max || as_signed < min;
1109 }
1110
1111 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1112 // fits in the given number of bits as either a signed or unsigned value.
1113 // For example, has_signed_unsigned_overflow<8> would check
1114 // -128 <= bits <= 255
1115 template<int no_bits>
1116 static inline bool
1117 has_signed_unsigned_overflow(uint32_t bits)
1118 {
1119 gold_assert(no_bits >= 2 && no_bits <= 32);
1120 if (no_bits == 32)
1121 return false;
1122 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1123 int32_t min = -(1 << (no_bits - 1));
1124 int32_t as_signed = static_cast<int32_t>(bits);
1125 return as_signed > max || as_signed < min;
1126 }
1127
1128 // Select bits from A and B using bits in MASK. For each n in [0..31],
1129 // the n-th bit in the result is chosen from the n-th bits of A and B.
1130 // A zero selects A and a one selects B.
1131 static inline uint32_t
1132 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1133 { return (a & ~mask) | (b & mask); }
1134 };
1135
1136 template<bool big_endian>
1137 class Target_arm : public Sized_target<32, big_endian>
1138 {
1139 public:
1140 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1141 Reloc_section;
1142
1143 Target_arm()
1144 : Sized_target<32, big_endian>(&arm_info),
1145 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1146 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
1147 may_use_blx_(true), should_force_pic_veneer_(false)
1148 { }
1149
1150 // Whether we can use BLX.
1151 bool
1152 may_use_blx() const
1153 { return this->may_use_blx_; }
1154
1155 // Set use-BLX flag.
1156 void
1157 set_may_use_blx(bool value)
1158 { this->may_use_blx_ = value; }
1159
1160 // Whether we force PCI branch veneers.
1161 bool
1162 should_force_pic_veneer() const
1163 { return this->should_force_pic_veneer_; }
1164
1165 // Set PIC veneer flag.
1166 void
1167 set_should_force_pic_veneer(bool value)
1168 { this->should_force_pic_veneer_ = value; }
1169
1170 // Whether we use THUMB-2 instructions.
1171 bool
1172 using_thumb2() const
1173 {
1174 // FIXME: This should not hard-coded.
1175 return false;
1176 }
1177
1178 // Whether we use THUMB/THUMB-2 instructions only.
1179 bool
1180 using_thumb_only() const
1181 {
1182 // FIXME: This should not hard-coded.
1183 return false;
1184 }
1185
1186 // Process the relocations to determine unreferenced sections for
1187 // garbage collection.
1188 void
1189 gc_process_relocs(Symbol_table* symtab,
1190 Layout* layout,
1191 Sized_relobj<32, big_endian>* object,
1192 unsigned int data_shndx,
1193 unsigned int sh_type,
1194 const unsigned char* prelocs,
1195 size_t reloc_count,
1196 Output_section* output_section,
1197 bool needs_special_offset_handling,
1198 size_t local_symbol_count,
1199 const unsigned char* plocal_symbols);
1200
1201 // Scan the relocations to look for symbol adjustments.
1202 void
1203 scan_relocs(Symbol_table* symtab,
1204 Layout* layout,
1205 Sized_relobj<32, big_endian>* object,
1206 unsigned int data_shndx,
1207 unsigned int sh_type,
1208 const unsigned char* prelocs,
1209 size_t reloc_count,
1210 Output_section* output_section,
1211 bool needs_special_offset_handling,
1212 size_t local_symbol_count,
1213 const unsigned char* plocal_symbols);
1214
1215 // Finalize the sections.
1216 void
1217 do_finalize_sections(Layout*, const Input_objects*);
1218
1219 // Return the value to use for a dynamic symbol which requires special
1220 // treatment.
1221 uint64_t
1222 do_dynsym_value(const Symbol*) const;
1223
1224 // Relocate a section.
1225 void
1226 relocate_section(const Relocate_info<32, big_endian>*,
1227 unsigned int sh_type,
1228 const unsigned char* prelocs,
1229 size_t reloc_count,
1230 Output_section* output_section,
1231 bool needs_special_offset_handling,
1232 unsigned char* view,
1233 Arm_address view_address,
1234 section_size_type view_size,
1235 const Reloc_symbol_changes*);
1236
1237 // Scan the relocs during a relocatable link.
1238 void
1239 scan_relocatable_relocs(Symbol_table* symtab,
1240 Layout* layout,
1241 Sized_relobj<32, big_endian>* object,
1242 unsigned int data_shndx,
1243 unsigned int sh_type,
1244 const unsigned char* prelocs,
1245 size_t reloc_count,
1246 Output_section* output_section,
1247 bool needs_special_offset_handling,
1248 size_t local_symbol_count,
1249 const unsigned char* plocal_symbols,
1250 Relocatable_relocs*);
1251
1252 // Relocate a section during a relocatable link.
1253 void
1254 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1255 unsigned int sh_type,
1256 const unsigned char* prelocs,
1257 size_t reloc_count,
1258 Output_section* output_section,
1259 off_t offset_in_output_section,
1260 const Relocatable_relocs*,
1261 unsigned char* view,
1262 Arm_address view_address,
1263 section_size_type view_size,
1264 unsigned char* reloc_view,
1265 section_size_type reloc_view_size);
1266
1267 // Return whether SYM is defined by the ABI.
1268 bool
1269 do_is_defined_by_abi(Symbol* sym) const
1270 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1271
1272 // Return the size of the GOT section.
1273 section_size_type
1274 got_size()
1275 {
1276 gold_assert(this->got_ != NULL);
1277 return this->got_->data_size();
1278 }
1279
1280 // Map platform-specific reloc types
1281 static unsigned int
1282 get_real_reloc_type (unsigned int r_type);
1283
1284 // Get the default ARM target.
1285 static const Target_arm<big_endian>&
1286 default_target()
1287 {
1288 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1289 && parameters->target().is_big_endian() == big_endian);
1290 return static_cast<const Target_arm<big_endian>&>(parameters->target());
1291 }
1292
1293 protected:
1294 void
1295 do_adjust_elf_header(unsigned char* view, int len) const;
1296
1297 private:
1298 // The class which scans relocations.
1299 class Scan
1300 {
1301 public:
1302 Scan()
1303 : issued_non_pic_error_(false)
1304 { }
1305
1306 inline void
1307 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
1308 Sized_relobj<32, big_endian>* object,
1309 unsigned int data_shndx,
1310 Output_section* output_section,
1311 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1312 const elfcpp::Sym<32, big_endian>& lsym);
1313
1314 inline void
1315 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
1316 Sized_relobj<32, big_endian>* object,
1317 unsigned int data_shndx,
1318 Output_section* output_section,
1319 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1320 Symbol* gsym);
1321
1322 private:
1323 static void
1324 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1325 unsigned int r_type);
1326
1327 static void
1328 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1329 unsigned int r_type, Symbol*);
1330
1331 void
1332 check_non_pic(Relobj*, unsigned int r_type);
1333
1334 // Almost identical to Symbol::needs_plt_entry except that it also
1335 // handles STT_ARM_TFUNC.
1336 static bool
1337 symbol_needs_plt_entry(const Symbol* sym)
1338 {
1339 // An undefined symbol from an executable does not need a PLT entry.
1340 if (sym->is_undefined() && !parameters->options().shared())
1341 return false;
1342
1343 return (!parameters->doing_static_link()
1344 && (sym->type() == elfcpp::STT_FUNC
1345 || sym->type() == elfcpp::STT_ARM_TFUNC)
1346 && (sym->is_from_dynobj()
1347 || sym->is_undefined()
1348 || sym->is_preemptible()));
1349 }
1350
1351 // Whether we have issued an error about a non-PIC compilation.
1352 bool issued_non_pic_error_;
1353 };
1354
1355 // The class which implements relocation.
1356 class Relocate
1357 {
1358 public:
1359 Relocate()
1360 { }
1361
1362 ~Relocate()
1363 { }
1364
1365 // Return whether the static relocation needs to be applied.
1366 inline bool
1367 should_apply_static_reloc(const Sized_symbol<32>* gsym,
1368 int ref_flags,
1369 bool is_32bit,
1370 Output_section* output_section);
1371
1372 // Do a relocation. Return false if the caller should not issue
1373 // any warnings about this relocation.
1374 inline bool
1375 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1376 Output_section*, size_t relnum,
1377 const elfcpp::Rel<32, big_endian>&,
1378 unsigned int r_type, const Sized_symbol<32>*,
1379 const Symbol_value<32>*,
1380 unsigned char*, Arm_address,
1381 section_size_type);
1382
1383 // Return whether we want to pass flag NON_PIC_REF for this
1384 // reloc.
1385 static inline bool
1386 reloc_is_non_pic (unsigned int r_type)
1387 {
1388 switch (r_type)
1389 {
1390 case elfcpp::R_ARM_REL32:
1391 case elfcpp::R_ARM_THM_CALL:
1392 case elfcpp::R_ARM_CALL:
1393 case elfcpp::R_ARM_JUMP24:
1394 case elfcpp::R_ARM_PREL31:
1395 case elfcpp::R_ARM_THM_ABS5:
1396 case elfcpp::R_ARM_ABS8:
1397 case elfcpp::R_ARM_ABS12:
1398 case elfcpp::R_ARM_ABS16:
1399 case elfcpp::R_ARM_BASE_ABS:
1400 return true;
1401 default:
1402 return false;
1403 }
1404 }
1405 };
1406
1407 // A class which returns the size required for a relocation type,
1408 // used while scanning relocs during a relocatable link.
1409 class Relocatable_size_for_reloc
1410 {
1411 public:
1412 unsigned int
1413 get_size_for_reloc(unsigned int, Relobj*);
1414 };
1415
1416 // Get the GOT section, creating it if necessary.
1417 Output_data_got<32, big_endian>*
1418 got_section(Symbol_table*, Layout*);
1419
1420 // Get the GOT PLT section.
1421 Output_data_space*
1422 got_plt_section() const
1423 {
1424 gold_assert(this->got_plt_ != NULL);
1425 return this->got_plt_;
1426 }
1427
1428 // Create a PLT entry for a global symbol.
1429 void
1430 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1431
1432 // Get the PLT section.
1433 const Output_data_plt_arm<big_endian>*
1434 plt_section() const
1435 {
1436 gold_assert(this->plt_ != NULL);
1437 return this->plt_;
1438 }
1439
1440 // Get the dynamic reloc section, creating it if necessary.
1441 Reloc_section*
1442 rel_dyn_section(Layout*);
1443
1444 // Return true if the symbol may need a COPY relocation.
1445 // References from an executable object to non-function symbols
1446 // defined in a dynamic object may need a COPY relocation.
1447 bool
1448 may_need_copy_reloc(Symbol* gsym)
1449 {
1450 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1451 && gsym->may_need_copy_reloc());
1452 }
1453
1454 // Add a potential copy relocation.
1455 void
1456 copy_reloc(Symbol_table* symtab, Layout* layout,
1457 Sized_relobj<32, big_endian>* object,
1458 unsigned int shndx, Output_section* output_section,
1459 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1460 {
1461 this->copy_relocs_.copy_reloc(symtab, layout,
1462 symtab->get_sized_symbol<32>(sym),
1463 object, shndx, output_section, reloc,
1464 this->rel_dyn_section(layout));
1465 }
1466
1467 // Whether two EABI versions are compatible.
1468 static bool
1469 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
1470
1471 // Merge processor-specific flags from input object and those in the ELF
1472 // header of the output.
1473 void
1474 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
1475
1476 Object*
1477 do_make_elf_object(const std::string&, Input_file*, off_t,
1478 const elfcpp::Ehdr<32, big_endian>& ehdr);
1479
1480 Object*
1481 do_make_elf_object(const std::string&, Input_file*, off_t,
1482 const elfcpp::Ehdr<32, !big_endian>&)
1483 { gold_unreachable(); }
1484
1485 Object*
1486 do_make_elf_object(const std::string&, Input_file*, off_t,
1487 const elfcpp::Ehdr<64, false>&)
1488 { gold_unreachable(); }
1489
1490 Object*
1491 do_make_elf_object(const std::string&, Input_file*, off_t,
1492 const elfcpp::Ehdr<64, true>&)
1493 { gold_unreachable(); }
1494
1495 // Information about this specific target which we pass to the
1496 // general Target structure.
1497 static const Target::Target_info arm_info;
1498
1499 // The types of GOT entries needed for this platform.
1500 enum Got_type
1501 {
1502 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
1503 };
1504
1505 // The GOT section.
1506 Output_data_got<32, big_endian>* got_;
1507 // The PLT section.
1508 Output_data_plt_arm<big_endian>* plt_;
1509 // The GOT PLT section.
1510 Output_data_space* got_plt_;
1511 // The dynamic reloc section.
1512 Reloc_section* rel_dyn_;
1513 // Relocs saved to avoid a COPY reloc.
1514 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1515 // Space for variables copied with a COPY reloc.
1516 Output_data_space* dynbss_;
1517 // Whether we can use BLX.
1518 bool may_use_blx_;
1519 // Whether we force PIC branch veneers.
1520 bool should_force_pic_veneer_;
1521 };
1522
1523 template<bool big_endian>
1524 const Target::Target_info Target_arm<big_endian>::arm_info =
1525 {
1526 32, // size
1527 big_endian, // is_big_endian
1528 elfcpp::EM_ARM, // machine_code
1529 false, // has_make_symbol
1530 false, // has_resolve
1531 false, // has_code_fill
1532 true, // is_default_stack_executable
1533 '\0', // wrap_char
1534 "/usr/lib/libc.so.1", // dynamic_linker
1535 0x8000, // default_text_segment_address
1536 0x1000, // abi_pagesize (overridable by -z max-page-size)
1537 0x1000, // common_pagesize (overridable by -z common-page-size)
1538 elfcpp::SHN_UNDEF, // small_common_shndx
1539 elfcpp::SHN_UNDEF, // large_common_shndx
1540 0, // small_common_section_flags
1541 0 // large_common_section_flags
1542 };
1543
1544 // Arm relocate functions class
1545 //
1546
1547 template<bool big_endian>
1548 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1549 {
1550 public:
1551 typedef enum
1552 {
1553 STATUS_OKAY, // No error during relocation.
1554 STATUS_OVERFLOW, // Relocation oveflow.
1555 STATUS_BAD_RELOC // Relocation cannot be applied.
1556 } Status;
1557
1558 private:
1559 typedef Relocate_functions<32, big_endian> Base;
1560 typedef Arm_relocate_functions<big_endian> This;
1561
1562 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
1563 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
1564 // to distinguish ARM and THUMB functions and it is treated specially.
1565 static inline Symbol_value<32>::Value
1566 arm_symbol_value (const Sized_relobj<32, big_endian> *object,
1567 const Symbol_value<32>* psymval,
1568 Symbol_value<32>::Value addend,
1569 bool has_thumb_bit)
1570 {
1571 typedef Symbol_value<32>::Value Valtype;
1572
1573 if (has_thumb_bit)
1574 {
1575 Valtype raw = psymval->value(object, 0);
1576 Valtype thumb_bit = raw & 1;
1577 return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
1578 }
1579 else
1580 return psymval->value(object, addend);
1581 }
1582
1583 // Encoding of imm16 argument for movt and movw ARM instructions
1584 // from ARM ARM:
1585 //
1586 // imm16 := imm4 | imm12
1587 //
1588 // 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
1589 // +-------+---------------+-------+-------+-----------------------+
1590 // | | |imm4 | |imm12 |
1591 // +-------+---------------+-------+-------+-----------------------+
1592
1593 // Extract the relocation addend from VAL based on the ARM
1594 // instruction encoding described above.
1595 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1596 extract_arm_movw_movt_addend(
1597 typename elfcpp::Swap<32, big_endian>::Valtype val)
1598 {
1599 // According to the Elf ABI for ARM Architecture the immediate
1600 // field is sign-extended to form the addend.
1601 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1602 }
1603
1604 // Insert X into VAL based on the ARM instruction encoding described
1605 // above.
1606 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1607 insert_val_arm_movw_movt(
1608 typename elfcpp::Swap<32, big_endian>::Valtype val,
1609 typename elfcpp::Swap<32, big_endian>::Valtype x)
1610 {
1611 val &= 0xfff0f000;
1612 val |= x & 0x0fff;
1613 val |= (x & 0xf000) << 4;
1614 return val;
1615 }
1616
1617 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1618 // from ARM ARM:
1619 //
1620 // imm16 := imm4 | i | imm3 | imm8
1621 //
1622 // 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
1623 // +---------+-+-----------+-------++-+-----+-------+---------------+
1624 // | |i| |imm4 || |imm3 | |imm8 |
1625 // +---------+-+-----------+-------++-+-----+-------+---------------+
1626
1627 // Extract the relocation addend from VAL based on the Thumb2
1628 // instruction encoding described above.
1629 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1630 extract_thumb_movw_movt_addend(
1631 typename elfcpp::Swap<32, big_endian>::Valtype val)
1632 {
1633 // According to the Elf ABI for ARM Architecture the immediate
1634 // field is sign-extended to form the addend.
1635 return utils::sign_extend<16>(((val >> 4) & 0xf000)
1636 | ((val >> 15) & 0x0800)
1637 | ((val >> 4) & 0x0700)
1638 | (val & 0x00ff));
1639 }
1640
1641 // Insert X into VAL based on the Thumb2 instruction encoding
1642 // described above.
1643 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1644 insert_val_thumb_movw_movt(
1645 typename elfcpp::Swap<32, big_endian>::Valtype val,
1646 typename elfcpp::Swap<32, big_endian>::Valtype x)
1647 {
1648 val &= 0xfbf08f00;
1649 val |= (x & 0xf000) << 4;
1650 val |= (x & 0x0800) << 15;
1651 val |= (x & 0x0700) << 4;
1652 val |= (x & 0x00ff);
1653 return val;
1654 }
1655
1656 // FIXME: This probably only works for Android on ARM v5te. We should
1657 // following GNU ld for the general case.
1658 template<unsigned r_type>
1659 static inline typename This::Status
1660 arm_branch_common(unsigned char *view,
1661 const Sized_relobj<32, big_endian>* object,
1662 const Symbol_value<32>* psymval,
1663 Arm_address address,
1664 bool has_thumb_bit)
1665 {
1666 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1667 Valtype* wv = reinterpret_cast<Valtype*>(view);
1668 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1669
1670 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
1671 && ((val & 0x0f000000UL) == 0x0a000000UL);
1672 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
1673 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
1674 && ((val & 0x0f000000UL) == 0x0b000000UL);
1675 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
1676 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
1677
1678 if (r_type == elfcpp::R_ARM_CALL)
1679 {
1680 if (!insn_is_uncond_bl && !insn_is_blx)
1681 return This::STATUS_BAD_RELOC;
1682 }
1683 else if (r_type == elfcpp::R_ARM_JUMP24)
1684 {
1685 if (!insn_is_b && !insn_is_cond_bl)
1686 return This::STATUS_BAD_RELOC;
1687 }
1688 else if (r_type == elfcpp::R_ARM_PLT32)
1689 {
1690 if (!insn_is_any_branch)
1691 return This::STATUS_BAD_RELOC;
1692 }
1693 else
1694 gold_unreachable();
1695
1696 Valtype addend = utils::sign_extend<26>(val << 2);
1697 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1698 - address);
1699
1700 // If target has thumb bit set, we need to either turn the BL
1701 // into a BLX (for ARMv5 or above) or generate a stub.
1702 if (x & 1)
1703 {
1704 // Turn BL to BLX.
1705 if (insn_is_uncond_bl)
1706 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
1707 else
1708 return This::STATUS_BAD_RELOC;
1709 }
1710 else
1711 gold_assert(!insn_is_blx);
1712
1713 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
1714 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1715 return (utils::has_overflow<26>(x)
1716 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
1717 }
1718
1719 public:
1720
1721 // R_ARM_ABS8: S + A
1722 static inline typename This::Status
1723 abs8(unsigned char *view,
1724 const Sized_relobj<32, big_endian>* object,
1725 const Symbol_value<32>* psymval)
1726 {
1727 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
1728 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1729 Valtype* wv = reinterpret_cast<Valtype*>(view);
1730 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
1731 Reltype addend = utils::sign_extend<8>(val);
1732 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1733 val = utils::bit_select(val, x, 0xffU);
1734 elfcpp::Swap<8, big_endian>::writeval(wv, val);
1735 return (utils::has_signed_unsigned_overflow<8>(x)
1736 ? This::STATUS_OVERFLOW
1737 : This::STATUS_OKAY);
1738 }
1739
1740 // R_ARM_THM_ABS5: S + A
1741 static inline typename This::Status
1742 thm_abs5(unsigned char *view,
1743 const Sized_relobj<32, big_endian>* object,
1744 const Symbol_value<32>* psymval)
1745 {
1746 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1747 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1748 Valtype* wv = reinterpret_cast<Valtype*>(view);
1749 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1750 Reltype addend = (val & 0x7e0U) >> 6;
1751 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1752 val = utils::bit_select(val, x << 6, 0x7e0U);
1753 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1754 return (utils::has_overflow<5>(x)
1755 ? This::STATUS_OVERFLOW
1756 : This::STATUS_OKAY);
1757 }
1758
1759 // R_ARM_ABS12: S + A
1760 static inline typename This::Status
1761 abs12(unsigned char *view,
1762 const Sized_relobj<32, big_endian>* object,
1763 const Symbol_value<32>* psymval)
1764 {
1765 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1766 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1767 Valtype* wv = reinterpret_cast<Valtype*>(view);
1768 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1769 Reltype addend = val & 0x0fffU;
1770 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1771 val = utils::bit_select(val, x, 0x0fffU);
1772 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1773 return (utils::has_overflow<12>(x)
1774 ? This::STATUS_OVERFLOW
1775 : This::STATUS_OKAY);
1776 }
1777
1778 // R_ARM_ABS16: S + A
1779 static inline typename This::Status
1780 abs16(unsigned char *view,
1781 const Sized_relobj<32, big_endian>* object,
1782 const Symbol_value<32>* psymval)
1783 {
1784 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1785 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1786 Valtype* wv = reinterpret_cast<Valtype*>(view);
1787 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1788 Reltype addend = utils::sign_extend<16>(val);
1789 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1790 val = utils::bit_select(val, x, 0xffffU);
1791 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1792 return (utils::has_signed_unsigned_overflow<16>(x)
1793 ? This::STATUS_OVERFLOW
1794 : This::STATUS_OKAY);
1795 }
1796
1797 // R_ARM_ABS32: (S + A) | T
1798 static inline typename This::Status
1799 abs32(unsigned char *view,
1800 const Sized_relobj<32, big_endian>* object,
1801 const Symbol_value<32>* psymval,
1802 bool has_thumb_bit)
1803 {
1804 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1805 Valtype* wv = reinterpret_cast<Valtype*>(view);
1806 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1807 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1808 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1809 return This::STATUS_OKAY;
1810 }
1811
1812 // R_ARM_REL32: (S + A) | T - P
1813 static inline typename This::Status
1814 rel32(unsigned char *view,
1815 const Sized_relobj<32, big_endian>* object,
1816 const Symbol_value<32>* psymval,
1817 Arm_address address,
1818 bool has_thumb_bit)
1819 {
1820 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1821 Valtype* wv = reinterpret_cast<Valtype*>(view);
1822 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1823 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1824 - address);
1825 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1826 return This::STATUS_OKAY;
1827 }
1828
1829 // R_ARM_THM_CALL: (S + A) | T - P
1830 static inline typename This::Status
1831 thm_call(unsigned char *view,
1832 const Sized_relobj<32, big_endian>* object,
1833 const Symbol_value<32>* psymval,
1834 Arm_address address,
1835 bool has_thumb_bit)
1836 {
1837 // A thumb call consists of two instructions.
1838 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1839 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1840 Valtype* wv = reinterpret_cast<Valtype*>(view);
1841 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
1842 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
1843 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
1844 gold_assert((lo & 0xf800) == 0xf800);
1845 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
1846 | ((lo & 0x7ff) << 1));
1847 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1848 - address);
1849
1850 // If target has no thumb bit set, we need to either turn the BL
1851 // into a BLX (for ARMv5 or above) or generate a stub.
1852 if ((x & 1) == 0)
1853 {
1854 // This only works for ARMv5 and above with interworking enabled.
1855 lo &= 0xefff;
1856 }
1857 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
1858 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
1859 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
1860 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
1861 return (utils::has_overflow<23>(x)
1862 ? This::STATUS_OVERFLOW
1863 : This::STATUS_OKAY);
1864 }
1865
1866 // R_ARM_BASE_PREL: B(S) + A - P
1867 static inline typename This::Status
1868 base_prel(unsigned char* view,
1869 Arm_address origin,
1870 Arm_address address)
1871 {
1872 Base::rel32(view, origin - address);
1873 return STATUS_OKAY;
1874 }
1875
1876 // R_ARM_BASE_ABS: B(S) + A
1877 static inline typename This::Status
1878 base_abs(unsigned char* view,
1879 Arm_address origin)
1880 {
1881 Base::rel32(view, origin);
1882 return STATUS_OKAY;
1883 }
1884
1885 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1886 static inline typename This::Status
1887 got_brel(unsigned char* view,
1888 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
1889 {
1890 Base::rel32(view, got_offset);
1891 return This::STATUS_OKAY;
1892 }
1893
1894 // R_ARM_GOT_PREL: GOT(S) + A – P
1895 static inline typename This::Status
1896 got_prel(unsigned char* view,
1897 typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
1898 Arm_address address)
1899 {
1900 Base::rel32(view, got_offset - address);
1901 return This::STATUS_OKAY;
1902 }
1903
1904 // R_ARM_PLT32: (S + A) | T - P
1905 static inline typename This::Status
1906 plt32(unsigned char *view,
1907 const Sized_relobj<32, big_endian>* object,
1908 const Symbol_value<32>* psymval,
1909 Arm_address address,
1910 bool has_thumb_bit)
1911 {
1912 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
1913 address, has_thumb_bit);
1914 }
1915
1916 // R_ARM_CALL: (S + A) | T - P
1917 static inline typename This::Status
1918 call(unsigned char *view,
1919 const Sized_relobj<32, big_endian>* object,
1920 const Symbol_value<32>* psymval,
1921 Arm_address address,
1922 bool has_thumb_bit)
1923 {
1924 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
1925 address, has_thumb_bit);
1926 }
1927
1928 // R_ARM_JUMP24: (S + A) | T - P
1929 static inline typename This::Status
1930 jump24(unsigned char *view,
1931 const Sized_relobj<32, big_endian>* object,
1932 const Symbol_value<32>* psymval,
1933 Arm_address address,
1934 bool has_thumb_bit)
1935 {
1936 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
1937 address, has_thumb_bit);
1938 }
1939
1940 // R_ARM_PREL: (S + A) | T - P
1941 static inline typename This::Status
1942 prel31(unsigned char *view,
1943 const Sized_relobj<32, big_endian>* object,
1944 const Symbol_value<32>* psymval,
1945 Arm_address address,
1946 bool has_thumb_bit)
1947 {
1948 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1949 Valtype* wv = reinterpret_cast<Valtype*>(view);
1950 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1951 Valtype addend = utils::sign_extend<31>(val);
1952 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1953 - address);
1954 val = utils::bit_select(val, x, 0x7fffffffU);
1955 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1956 return (utils::has_overflow<31>(x) ?
1957 This::STATUS_OVERFLOW : This::STATUS_OKAY);
1958 }
1959
1960 // R_ARM_MOVW_ABS_NC: (S + A) | T
1961 static inline typename This::Status
1962 movw_abs_nc(unsigned char *view,
1963 const Sized_relobj<32, big_endian>* object,
1964 const Symbol_value<32>* psymval,
1965 bool has_thumb_bit)
1966 {
1967 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1968 Valtype* wv = reinterpret_cast<Valtype*>(view);
1969 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1970 Valtype addend = This::extract_arm_movw_movt_addend(val);
1971 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1972 val = This::insert_val_arm_movw_movt(val, x);
1973 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1974 return This::STATUS_OKAY;
1975 }
1976
1977 // R_ARM_MOVT_ABS: S + A
1978 static inline typename This::Status
1979 movt_abs(unsigned char *view,
1980 const Sized_relobj<32, big_endian>* object,
1981 const Symbol_value<32>* psymval)
1982 {
1983 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1984 Valtype* wv = reinterpret_cast<Valtype*>(view);
1985 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1986 Valtype addend = This::extract_arm_movw_movt_addend(val);
1987 Valtype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
1988 val = This::insert_val_arm_movw_movt(val, x);
1989 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1990 return This::STATUS_OKAY;
1991 }
1992
1993 // R_ARM_THM_MOVW_ABS_NC: S + A | T
1994 static inline typename This::Status
1995 thm_movw_abs_nc(unsigned char *view,
1996 const Sized_relobj<32, big_endian>* object,
1997 const Symbol_value<32>* psymval,
1998 bool has_thumb_bit)
1999 {
2000 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2001 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2002 Valtype* wv = reinterpret_cast<Valtype*>(view);
2003 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2004 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2005 Reltype addend = extract_thumb_movw_movt_addend(val);
2006 Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
2007 val = This::insert_val_thumb_movw_movt(val, x);
2008 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2009 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2010 return This::STATUS_OKAY;
2011 }
2012
2013 // R_ARM_THM_MOVT_ABS: S + A
2014 static inline typename This::Status
2015 thm_movt_abs(unsigned char *view,
2016 const Sized_relobj<32, big_endian>* object,
2017 const Symbol_value<32>* psymval)
2018 {
2019 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2020 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2021 Valtype* wv = reinterpret_cast<Valtype*>(view);
2022 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2023 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2024 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2025 Reltype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
2026 val = This::insert_val_thumb_movw_movt(val, x);
2027 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2028 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2029 return This::STATUS_OKAY;
2030 }
2031
2032 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2033 static inline typename This::Status
2034 movw_prel_nc(unsigned char *view,
2035 const Sized_relobj<32, big_endian>* object,
2036 const Symbol_value<32>* psymval,
2037 Arm_address address,
2038 bool has_thumb_bit)
2039 {
2040 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2041 Valtype* wv = reinterpret_cast<Valtype*>(view);
2042 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2043 Valtype addend = This::extract_arm_movw_movt_addend(val);
2044 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
2045 - address);
2046 val = This::insert_val_arm_movw_movt(val, x);
2047 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2048 return This::STATUS_OKAY;
2049 }
2050
2051 // R_ARM_MOVT_PREL: S + A - P
2052 static inline typename This::Status
2053 movt_prel(unsigned char *view,
2054 const Sized_relobj<32, big_endian>* object,
2055 const Symbol_value<32>* psymval,
2056 Arm_address address)
2057 {
2058 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2059 Valtype* wv = reinterpret_cast<Valtype*>(view);
2060 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2061 Valtype addend = This::extract_arm_movw_movt_addend(val);
2062 Valtype x = (This::arm_symbol_value(object, psymval, addend, 0)
2063 - address) >> 16;
2064 val = This::insert_val_arm_movw_movt(val, x);
2065 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2066 return This::STATUS_OKAY;
2067 }
2068
2069 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2070 static inline typename This::Status
2071 thm_movw_prel_nc(unsigned char *view,
2072 const Sized_relobj<32, big_endian>* object,
2073 const Symbol_value<32>* psymval,
2074 Arm_address address,
2075 bool has_thumb_bit)
2076 {
2077 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2078 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2079 Valtype* wv = reinterpret_cast<Valtype*>(view);
2080 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2081 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2082 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2083 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
2084 - address);
2085 val = This::insert_val_thumb_movw_movt(val, x);
2086 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2087 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2088 return This::STATUS_OKAY;
2089 }
2090
2091 // R_ARM_THM_MOVT_PREL: S + A - P
2092 static inline typename This::Status
2093 thm_movt_prel(unsigned char *view,
2094 const Sized_relobj<32, big_endian>* object,
2095 const Symbol_value<32>* psymval,
2096 Arm_address address)
2097 {
2098 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2099 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2100 Valtype* wv = reinterpret_cast<Valtype*>(view);
2101 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2102 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2103 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2104 Reltype x = (This::arm_symbol_value(object, psymval, addend, 0)
2105 - address) >> 16;
2106 val = This::insert_val_thumb_movw_movt(val, x);
2107 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2108 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2109 return This::STATUS_OKAY;
2110 }
2111 };
2112
2113 // Get the GOT section, creating it if necessary.
2114
2115 template<bool big_endian>
2116 Output_data_got<32, big_endian>*
2117 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
2118 {
2119 if (this->got_ == NULL)
2120 {
2121 gold_assert(symtab != NULL && layout != NULL);
2122
2123 this->got_ = new Output_data_got<32, big_endian>();
2124
2125 Output_section* os;
2126 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2127 (elfcpp::SHF_ALLOC
2128 | elfcpp::SHF_WRITE),
2129 this->got_);
2130 os->set_is_relro();
2131
2132 // The old GNU linker creates a .got.plt section. We just
2133 // create another set of data in the .got section. Note that we
2134 // always create a PLT if we create a GOT, although the PLT
2135 // might be empty.
2136 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
2137 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2138 (elfcpp::SHF_ALLOC
2139 | elfcpp::SHF_WRITE),
2140 this->got_plt_);
2141 os->set_is_relro();
2142
2143 // The first three entries are reserved.
2144 this->got_plt_->set_current_data_size(3 * 4);
2145
2146 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2147 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2148 this->got_plt_,
2149 0, 0, elfcpp::STT_OBJECT,
2150 elfcpp::STB_LOCAL,
2151 elfcpp::STV_HIDDEN, 0,
2152 false, false);
2153 }
2154 return this->got_;
2155 }
2156
2157 // Get the dynamic reloc section, creating it if necessary.
2158
2159 template<bool big_endian>
2160 typename Target_arm<big_endian>::Reloc_section*
2161 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
2162 {
2163 if (this->rel_dyn_ == NULL)
2164 {
2165 gold_assert(layout != NULL);
2166 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
2167 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
2168 elfcpp::SHF_ALLOC, this->rel_dyn_);
2169 }
2170 return this->rel_dyn_;
2171 }
2172
2173 // Insn_template methods.
2174
2175 // Return byte size of an instruction template.
2176
2177 size_t
2178 Insn_template::size() const
2179 {
2180 switch (this->type())
2181 {
2182 case THUMB16_TYPE:
2183 return 2;
2184 case ARM_TYPE:
2185 case THUMB32_TYPE:
2186 case DATA_TYPE:
2187 return 4;
2188 default:
2189 gold_unreachable();
2190 }
2191 }
2192
2193 // Return alignment of an instruction template.
2194
2195 unsigned
2196 Insn_template::alignment() const
2197 {
2198 switch (this->type())
2199 {
2200 case THUMB16_TYPE:
2201 case THUMB32_TYPE:
2202 return 2;
2203 case ARM_TYPE:
2204 case DATA_TYPE:
2205 return 4;
2206 default:
2207 gold_unreachable();
2208 }
2209 }
2210
2211 // Stub_template methods.
2212
2213 Stub_template::Stub_template(
2214 Stub_type type, const Insn_template* insns,
2215 size_t insn_count)
2216 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
2217 entry_in_thumb_mode_(false), relocs_()
2218 {
2219 off_t offset = 0;
2220
2221 // Compute byte size and alignment of stub template.
2222 for (size_t i = 0; i < insn_count; i++)
2223 {
2224 unsigned insn_alignment = insns[i].alignment();
2225 size_t insn_size = insns[i].size();
2226 gold_assert((offset & (insn_alignment - 1)) == 0);
2227 this->alignment_ = std::max(this->alignment_, insn_alignment);
2228 switch (insns[i].type())
2229 {
2230 case Insn_template::THUMB16_TYPE:
2231 if (i == 0)
2232 this->entry_in_thumb_mode_ = true;
2233 break;
2234
2235 case Insn_template::THUMB32_TYPE:
2236 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
2237 this->relocs_.push_back(Reloc(i, offset));
2238 if (i == 0)
2239 this->entry_in_thumb_mode_ = true;
2240 break;
2241
2242 case Insn_template::ARM_TYPE:
2243 // Handle cases where the target is encoded within the
2244 // instruction.
2245 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
2246 this->relocs_.push_back(Reloc(i, offset));
2247 break;
2248
2249 case Insn_template::DATA_TYPE:
2250 // Entry point cannot be data.
2251 gold_assert(i != 0);
2252 this->relocs_.push_back(Reloc(i, offset));
2253 break;
2254
2255 default:
2256 gold_unreachable();
2257 }
2258 offset += insn_size;
2259 }
2260 this->size_ = offset;
2261 }
2262
2263 // Reloc_stub::Key methods.
2264
2265 // Dump a Key as a string for debugging.
2266
2267 std::string
2268 Reloc_stub::Key::name() const
2269 {
2270 if (this->r_sym_ == invalid_index)
2271 {
2272 // Global symbol key name
2273 // <stub-type>:<symbol name>:<addend>.
2274 const std::string sym_name = this->u_.symbol->name();
2275 // We need to print two hex number and two colons. So just add 100 bytes
2276 // to the symbol name size.
2277 size_t len = sym_name.size() + 100;
2278 char* buffer = new char[len];
2279 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
2280 sym_name.c_str(), this->addend_);
2281 gold_assert(c > 0 && c < static_cast<int>(len));
2282 delete[] buffer;
2283 return std::string(buffer);
2284 }
2285 else
2286 {
2287 // local symbol key name
2288 // <stub-type>:<object>:<r_sym>:<addend>.
2289 const size_t len = 200;
2290 char buffer[len];
2291 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
2292 this->u_.relobj, this->r_sym_, this->addend_);
2293 gold_assert(c > 0 && c < static_cast<int>(len));
2294 return std::string(buffer);
2295 }
2296 }
2297
2298 // Reloc_stub methods.
2299
2300 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2301 // LOCATION to DESTINATION.
2302 // This code is based on the arm_type_of_stub function in
2303 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2304 // class simple.
2305
2306 Stub_type
2307 Reloc_stub::stub_type_for_reloc(
2308 unsigned int r_type,
2309 Arm_address location,
2310 Arm_address destination,
2311 bool target_is_thumb)
2312 {
2313 Stub_type stub_type = arm_stub_none;
2314
2315 // This is a bit ugly but we want to avoid using a templated class for
2316 // big and little endianities.
2317 bool may_use_blx;
2318 bool should_force_pic_veneer;
2319 bool thumb2;
2320 bool thumb_only;
2321 if (parameters->target().is_big_endian())
2322 {
2323 const Target_arm<true>& big_endian_target =
2324 Target_arm<true>::default_target();
2325 may_use_blx = big_endian_target.may_use_blx();
2326 should_force_pic_veneer = big_endian_target.should_force_pic_veneer();
2327 thumb2 = big_endian_target.using_thumb2();
2328 thumb_only = big_endian_target.using_thumb_only();
2329 }
2330 else
2331 {
2332 const Target_arm<false>& little_endian_target =
2333 Target_arm<false>::default_target();
2334 may_use_blx = little_endian_target.may_use_blx();
2335 should_force_pic_veneer = little_endian_target.should_force_pic_veneer();
2336 thumb2 = little_endian_target.using_thumb2();
2337 thumb_only = little_endian_target.using_thumb_only();
2338 }
2339
2340 int64_t branch_offset = (int64_t)destination - location;
2341
2342 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
2343 {
2344 // Handle cases where:
2345 // - this call goes too far (different Thumb/Thumb2 max
2346 // distance)
2347 // - it's a Thumb->Arm call and blx is not available, or it's a
2348 // Thumb->Arm branch (not bl). A stub is needed in this case.
2349 if ((!thumb2
2350 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2351 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2352 || (thumb2
2353 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2354 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2355 || ((!target_is_thumb)
2356 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2357 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
2358 {
2359 if (target_is_thumb)
2360 {
2361 // Thumb to thumb.
2362 if (!thumb_only)
2363 {
2364 stub_type = (parameters->options().shared() | should_force_pic_veneer)
2365 // PIC stubs.
2366 ? ((may_use_blx
2367 && (r_type == elfcpp::R_ARM_THM_CALL))
2368 // V5T and above. Stub starts with ARM code, so
2369 // we must be able to switch mode before
2370 // reaching it, which is only possible for 'bl'
2371 // (ie R_ARM_THM_CALL relocation).
2372 ? arm_stub_long_branch_any_thumb_pic
2373 // On V4T, use Thumb code only.
2374 : arm_stub_long_branch_v4t_thumb_thumb_pic)
2375
2376 // non-PIC stubs.
2377 : ((may_use_blx
2378 && (r_type == elfcpp::R_ARM_THM_CALL))
2379 ? arm_stub_long_branch_any_any // V5T and above.
2380 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
2381 }
2382 else
2383 {
2384 stub_type = (parameters->options().shared() | should_force_pic_veneer)
2385 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
2386 : arm_stub_long_branch_thumb_only; // non-PIC stub.
2387 }
2388 }
2389 else
2390 {
2391 // Thumb to arm.
2392
2393 // FIXME: We should check that the input section is from an
2394 // object that has interwork enabled.
2395
2396 stub_type = (parameters->options().shared()
2397 || should_force_pic_veneer)
2398 // PIC stubs.
2399 ? ((may_use_blx
2400 && (r_type == elfcpp::R_ARM_THM_CALL))
2401 ? arm_stub_long_branch_any_arm_pic // V5T and above.
2402 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
2403
2404 // non-PIC stubs.
2405 : ((may_use_blx
2406 && (r_type == elfcpp::R_ARM_THM_CALL))
2407 ? arm_stub_long_branch_any_any // V5T and above.
2408 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
2409
2410 // Handle v4t short branches.
2411 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
2412 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
2413 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
2414 stub_type = arm_stub_short_branch_v4t_thumb_arm;
2415 }
2416 }
2417 }
2418 else if (r_type == elfcpp::R_ARM_CALL
2419 || r_type == elfcpp::R_ARM_JUMP24
2420 || r_type == elfcpp::R_ARM_PLT32)
2421 {
2422 if (target_is_thumb)
2423 {
2424 // Arm to thumb.
2425
2426 // FIXME: We should check that the input section is from an
2427 // object that has interwork enabled.
2428
2429 // We have an extra 2-bytes reach because of
2430 // the mode change (bit 24 (H) of BLX encoding).
2431 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
2432 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2433 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
2434 || (r_type == elfcpp::R_ARM_JUMP24)
2435 || (r_type == elfcpp::R_ARM_PLT32))
2436 {
2437 stub_type = (parameters->options().shared()
2438 || should_force_pic_veneer)
2439 // PIC stubs.
2440 ? (may_use_blx
2441 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
2442 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
2443
2444 // non-PIC stubs.
2445 : (may_use_blx
2446 ? arm_stub_long_branch_any_any // V5T and above.
2447 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
2448 }
2449 }
2450 else
2451 {
2452 // Arm to arm.
2453 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
2454 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
2455 {
2456 stub_type = (parameters->options().shared()
2457 || should_force_pic_veneer)
2458 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
2459 : arm_stub_long_branch_any_any; /// non-PIC.
2460 }
2461 }
2462 }
2463
2464 return stub_type;
2465 }
2466
2467 // Template to implement do_write for a specific target endianity.
2468
2469 template<bool big_endian>
2470 void inline
2471 Reloc_stub::do_fixed_endian_write(unsigned char* view,
2472 section_size_type view_size)
2473 {
2474 const Stub_template* stub_template = this->stub_template();
2475 const Insn_template* insns = stub_template->insns();
2476
2477 // FIXME: We do not handle BE8 encoding yet.
2478 unsigned char* pov = view;
2479 for (size_t i = 0; i < stub_template->insn_count(); i++)
2480 {
2481 switch (insns[i].type())
2482 {
2483 case Insn_template::THUMB16_TYPE:
2484 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2485 gold_assert(insns[i].reloc_addend() == 0);
2486 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2487 break;
2488 case Insn_template::THUMB32_TYPE:
2489 {
2490 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2491 uint32_t lo = insns[i].data() & 0xffff;
2492 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2493 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2494 }
2495 break;
2496 case Insn_template::ARM_TYPE:
2497 case Insn_template::DATA_TYPE:
2498 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2499 break;
2500 default:
2501 gold_unreachable();
2502 }
2503 pov += insns[i].size();
2504 }
2505 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2506 }
2507
2508 // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2509
2510 void
2511 Reloc_stub::do_write(unsigned char* view, section_size_type view_size,
2512 bool big_endian)
2513 {
2514 if (big_endian)
2515 this->do_fixed_endian_write<true>(view, view_size);
2516 else
2517 this->do_fixed_endian_write<false>(view, view_size);
2518 }
2519
2520 // Stub_factory methods.
2521
2522 Stub_factory::Stub_factory()
2523 {
2524 // The instruction template sequences are declared as static
2525 // objects and initialized first time the constructor runs.
2526
2527 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2528 // to reach the stub if necessary.
2529 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
2530 {
2531 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2532 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2533 // dcd R_ARM_ABS32(X)
2534 };
2535
2536 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2537 // available.
2538 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2539 {
2540 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2541 Insn_template::arm_insn(0xe12fff1c), // bx ip
2542 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2543 // dcd R_ARM_ABS32(X)
2544 };
2545
2546 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2547 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
2548 {
2549 Insn_template::thumb16_insn(0xb401), // push {r0}
2550 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2551 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2552 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2553 Insn_template::thumb16_insn(0x4760), // bx ip
2554 Insn_template::thumb16_insn(0xbf00), // nop
2555 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2556 // dcd R_ARM_ABS32(X)
2557 };
2558
2559 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2560 // allowed.
2561 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2562 {
2563 Insn_template::thumb16_insn(0x4778), // bx pc
2564 Insn_template::thumb16_insn(0x46c0), // nop
2565 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2566 Insn_template::arm_insn(0xe12fff1c), // bx ip
2567 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2568 // dcd R_ARM_ABS32(X)
2569 };
2570
2571 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2572 // available.
2573 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2574 {
2575 Insn_template::thumb16_insn(0x4778), // bx pc
2576 Insn_template::thumb16_insn(0x46c0), // nop
2577 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2578 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2579 // dcd R_ARM_ABS32(X)
2580 };
2581
2582 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2583 // one, when the destination is close enough.
2584 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2585 {
2586 Insn_template::thumb16_insn(0x4778), // bx pc
2587 Insn_template::thumb16_insn(0x46c0), // nop
2588 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2589 };
2590
2591 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2592 // blx to reach the stub if necessary.
2593 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
2594 {
2595 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2596 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2597 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2598 // dcd R_ARM_REL32(X-4)
2599 };
2600
2601 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2602 // blx to reach the stub if necessary. We can not add into pc;
2603 // it is not guaranteed to mode switch (different in ARMv6 and
2604 // ARMv7).
2605 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
2606 {
2607 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2608 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2609 Insn_template::arm_insn(0xe12fff1c), // bx ip
2610 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2611 // dcd R_ARM_REL32(X)
2612 };
2613
2614 // V4T ARM -> ARM long branch stub, PIC.
2615 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2616 {
2617 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2618 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2619 Insn_template::arm_insn(0xe12fff1c), // bx ip
2620 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2621 // dcd R_ARM_REL32(X)
2622 };
2623
2624 // V4T Thumb -> ARM long branch stub, PIC.
2625 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2626 {
2627 Insn_template::thumb16_insn(0x4778), // bx pc
2628 Insn_template::thumb16_insn(0x46c0), // nop
2629 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2630 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
2631 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2632 // dcd R_ARM_REL32(X)
2633 };
2634
2635 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
2636 // architectures.
2637 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
2638 {
2639 Insn_template::thumb16_insn(0xb401), // push {r0}
2640 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2641 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
2642 Insn_template::thumb16_insn(0x4484), // add ip, r0
2643 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2644 Insn_template::thumb16_insn(0x4760), // bx ip
2645 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
2646 // dcd R_ARM_REL32(X)
2647 };
2648
2649 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2650 // allowed.
2651 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2652 {
2653 Insn_template::thumb16_insn(0x4778), // bx pc
2654 Insn_template::thumb16_insn(0x46c0), // nop
2655 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2656 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2657 Insn_template::arm_insn(0xe12fff1c), // bx ip
2658 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2659 // dcd R_ARM_REL32(X)
2660 };
2661
2662 // Cortex-A8 erratum-workaround stubs.
2663
2664 // Stub used for conditional branches (which may be beyond +/-1MB away,
2665 // so we can't use a conditional branch to reach this stub).
2666
2667 // original code:
2668 //
2669 // b<cond> X
2670 // after:
2671 //
2672 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
2673 {
2674 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
2675 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
2676 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
2677 // b.w X
2678 };
2679
2680 // Stub used for b.w and bl.w instructions.
2681
2682 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
2683 {
2684 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2685 };
2686
2687 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
2688 {
2689 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2690 };
2691
2692 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
2693 // instruction (which switches to ARM mode) to point to this stub. Jump to
2694 // the real destination using an ARM-mode branch.
2695 const Insn_template elf32_arm_stub_a8_veneer_blx[] =
2696 {
2697 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
2698 };
2699
2700 // Fill in the stub template look-up table. Stub templates are constructed
2701 // per instance of Stub_factory for fast look-up without locking
2702 // in a thread-enabled environment.
2703
2704 this->stub_templates_[arm_stub_none] =
2705 new Stub_template(arm_stub_none, NULL, 0);
2706
2707 #define DEF_STUB(x) \
2708 do \
2709 { \
2710 size_t array_size \
2711 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
2712 Stub_type type = arm_stub_##x; \
2713 this->stub_templates_[type] = \
2714 new Stub_template(type, elf32_arm_stub_##x, array_size); \
2715 } \
2716 while (0);
2717
2718 DEF_STUBS
2719 #undef DEF_STUB
2720 }
2721
2722 // Stub_table methods.
2723
2724 // Add a STUB with using KEY. Caller is reponsible for avoid adding
2725 // if already a STUB with the same key has been added.
2726
2727 template<bool big_endian>
2728 void
2729 Stub_table<big_endian>::add_reloc_stub(
2730 Reloc_stub* stub,
2731 const Reloc_stub::Key& key)
2732 {
2733 const Stub_template* stub_template = stub->stub_template();
2734 gold_assert(stub_template->type() == key.stub_type());
2735 this->reloc_stubs_[key] = stub;
2736 if (this->addralign_ < stub_template->alignment())
2737 this->addralign_ = stub_template->alignment();
2738 this->has_been_changed_ = true;
2739 }
2740
2741 template<bool big_endian>
2742 void
2743 Stub_table<big_endian>::relocate_stubs(
2744 const Relocate_info<32, big_endian>* relinfo,
2745 Target_arm<big_endian>* arm_target,
2746 Output_section* output_section,
2747 unsigned char* view,
2748 Arm_address address,
2749 section_size_type view_size)
2750 {
2751 // If we are passed a view bigger than the stub table's. we need to
2752 // adjust the view.
2753 gold_assert(address == this->address()
2754 && (view_size
2755 == static_cast<section_size_type>(this->data_size())));
2756
2757 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2758 p != this->reloc_stubs_.end();
2759 ++p)
2760 {
2761 Reloc_stub* stub = p->second;
2762 const Stub_template* stub_template = stub->stub_template();
2763 if (stub_template->reloc_count() != 0)
2764 {
2765 // Adjust view to cover the stub only.
2766 section_size_type offset = stub->offset();
2767 section_size_type stub_size = stub_template->size();
2768 gold_assert(offset + stub_size <= view_size);
2769
2770 arm_target->relocate_stub(stub, relinfo, output_section,
2771 view + offset, address + offset,
2772 stub_size);
2773 }
2774 }
2775 }
2776
2777 // Reset address and file offset.
2778
2779 template<bool big_endian>
2780 void
2781 Stub_table<big_endian>::do_reset_address_and_file_offset()
2782 {
2783 off_t off = 0;
2784 uint64_t max_addralign = 1;
2785 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2786 p != this->reloc_stubs_.end();
2787 ++p)
2788 {
2789 Reloc_stub* stub = p->second;
2790 const Stub_template* stub_template = stub->stub_template();
2791 uint64_t stub_addralign = stub_template->alignment();
2792 max_addralign = std::max(max_addralign, stub_addralign);
2793 off = align_address(off, stub_addralign);
2794 stub->set_offset(off);
2795 stub->reset_destination_address();
2796 off += stub_template->size();
2797 }
2798
2799 this->addralign_ = max_addralign;
2800 this->set_current_data_size_for_child(off);
2801 }
2802
2803 // Write out the stubs to file.
2804
2805 template<bool big_endian>
2806 void
2807 Stub_table<big_endian>::do_write(Output_file* of)
2808 {
2809 off_t offset = this->offset();
2810 const section_size_type oview_size =
2811 convert_to_section_size_type(this->data_size());
2812 unsigned char* const oview = of->get_output_view(offset, oview_size);
2813
2814 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2815 p != this->reloc_stubs_.end();
2816 ++p)
2817 {
2818 Reloc_stub* stub = p->second;
2819 Arm_address address = this->address() + stub->offset();
2820 gold_assert(address
2821 == align_address(address,
2822 stub->stub_template()->alignment()));
2823 stub->write(oview + stub->offset(), stub->stub_template()->size(),
2824 big_endian);
2825 }
2826 of->write_output_view(this->offset(), oview_size, oview);
2827 }
2828
2829 // Arm_input_section methods.
2830
2831 // Initialize an Arm_input_section.
2832
2833 template<bool big_endian>
2834 void
2835 Arm_input_section<big_endian>::init()
2836 {
2837 Relobj* relobj = this->relobj();
2838 unsigned int shndx = this->shndx();
2839
2840 // Cache these to speed up size and alignment queries. It is too slow
2841 // to call section_addraglin and section_size every time.
2842 this->original_addralign_ = relobj->section_addralign(shndx);
2843 this->original_size_ = relobj->section_size(shndx);
2844
2845 // We want to make this look like the original input section after
2846 // output sections are finalized.
2847 Output_section* os = relobj->output_section(shndx);
2848 off_t offset = relobj->output_section_offset(shndx);
2849 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2850 this->set_address(os->address() + offset);
2851 this->set_file_offset(os->offset() + offset);
2852
2853 this->set_current_data_size(this->original_size_);
2854 this->finalize_data_size();
2855 }
2856
2857 template<bool big_endian>
2858 void
2859 Arm_input_section<big_endian>::do_write(Output_file* of)
2860 {
2861 // We have to write out the original section content.
2862 section_size_type section_size;
2863 const unsigned char* section_contents =
2864 this->relobj()->section_contents(this->shndx(), &section_size, false);
2865 of->write(this->offset(), section_contents, section_size);
2866
2867 // If this owns a stub table and it is not empty, write it.
2868 if (this->is_stub_table_owner() && !this->stub_table_->empty())
2869 this->stub_table_->write(of);
2870 }
2871
2872 // Finalize data size.
2873
2874 template<bool big_endian>
2875 void
2876 Arm_input_section<big_endian>::set_final_data_size()
2877 {
2878 // If this owns a stub table, finalize its data size as well.
2879 if (this->is_stub_table_owner())
2880 {
2881 uint64_t address = this->address();
2882
2883 // The stub table comes after the original section contents.
2884 address += this->original_size_;
2885 address = align_address(address, this->stub_table_->addralign());
2886 off_t offset = this->offset() + (address - this->address());
2887 this->stub_table_->set_address_and_file_offset(address, offset);
2888 address += this->stub_table_->data_size();
2889 gold_assert(address == this->address() + this->current_data_size());
2890 }
2891
2892 this->set_data_size(this->current_data_size());
2893 }
2894
2895 // Reset address and file offset.
2896
2897 template<bool big_endian>
2898 void
2899 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
2900 {
2901 // Size of the original input section contents.
2902 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2903
2904 // If this is a stub table owner, account for the stub table size.
2905 if (this->is_stub_table_owner())
2906 {
2907 Stub_table<big_endian>* stub_table = this->stub_table_;
2908
2909 // Reset the stub table's address and file offset. The
2910 // current data size for child will be updated after that.
2911 stub_table_->reset_address_and_file_offset();
2912 off = align_address(off, stub_table_->addralign());
2913 off += stub_table->current_data_size();
2914 }
2915
2916 this->set_current_data_size(off);
2917 }
2918
2919 // Arm_output_section methods.
2920
2921 // Create a stub group for input sections from BEGIN to END. OWNER
2922 // points to the input section to be the owner a new stub table.
2923
2924 template<bool big_endian>
2925 void
2926 Arm_output_section<big_endian>::create_stub_group(
2927 Input_section_list::const_iterator begin,
2928 Input_section_list::const_iterator end,
2929 Input_section_list::const_iterator owner,
2930 Target_arm<big_endian>* target,
2931 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
2932 {
2933 // Currently we convert ordinary input sections into relaxed sections only
2934 // at this point but we may want to support creating relaxed input section
2935 // very early. So we check here to see if owner is already a relaxed
2936 // section.
2937
2938 Arm_input_section<big_endian>* arm_input_section;
2939 if (owner->is_relaxed_input_section())
2940 {
2941 arm_input_section =
2942 Arm_input_section<big_endian>::as_arm_input_section(
2943 owner->relaxed_input_section());
2944 }
2945 else
2946 {
2947 gold_assert(owner->is_input_section());
2948 // Create a new relaxed input section.
2949 arm_input_section =
2950 target->new_arm_input_section(owner->relobj(), owner->shndx());
2951 new_relaxed_sections->push_back(arm_input_section);
2952 }
2953
2954 // Create a stub table.
2955 Stub_table<big_endian>* stub_table =
2956 target->new_stub_table(arm_input_section);
2957
2958 arm_input_section->set_stub_table(stub_table);
2959
2960 Input_section_list::const_iterator p = begin;
2961 Input_section_list::const_iterator prev_p;
2962
2963 // Look for input sections or relaxed input sections in [begin ... end].
2964 do
2965 {
2966 if (p->is_input_section() || p->is_relaxed_input_section())
2967 {
2968 // The stub table information for input sections live
2969 // in their objects.
2970 Arm_relobj<big_endian>* arm_relobj =
2971 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
2972 arm_relobj->set_stub_table(p->shndx(), stub_table);
2973 }
2974 prev_p = p++;
2975 }
2976 while (prev_p != end);
2977 }
2978
2979 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
2980 // of stub groups. We grow a stub group by adding input section until the
2981 // size is just below GROUP_SIZE. The last input section will be converted
2982 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
2983 // input section after the stub table, effectively double the group size.
2984 //
2985 // This is similar to the group_sections() function in elf32-arm.c but is
2986 // implemented differently.
2987
2988 template<bool big_endian>
2989 void
2990 Arm_output_section<big_endian>::group_sections(
2991 section_size_type group_size,
2992 bool stubs_always_after_branch,
2993 Target_arm<big_endian>* target)
2994 {
2995 // We only care about sections containing code.
2996 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
2997 return;
2998
2999 // States for grouping.
3000 typedef enum
3001 {
3002 // No group is being built.
3003 NO_GROUP,
3004 // A group is being built but the stub table is not found yet.
3005 // We keep group a stub group until the size is just under GROUP_SIZE.
3006 // The last input section in the group will be used as the stub table.
3007 FINDING_STUB_SECTION,
3008 // A group is being built and we have already found a stub table.
3009 // We enter this state to grow a stub group by adding input section
3010 // after the stub table. This effectively doubles the group size.
3011 HAS_STUB_SECTION
3012 } State;
3013
3014 // Any newly created relaxed sections are stored here.
3015 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
3016
3017 State state = NO_GROUP;
3018 section_size_type off = 0;
3019 section_size_type group_begin_offset = 0;
3020 section_size_type group_end_offset = 0;
3021 section_size_type stub_table_end_offset = 0;
3022 Input_section_list::const_iterator group_begin =
3023 this->input_sections().end();
3024 Input_section_list::const_iterator stub_table =
3025 this->input_sections().end();
3026 Input_section_list::const_iterator group_end = this->input_sections().end();
3027 for (Input_section_list::const_iterator p = this->input_sections().begin();
3028 p != this->input_sections().end();
3029 ++p)
3030 {
3031 section_size_type section_begin_offset =
3032 align_address(off, p->addralign());
3033 section_size_type section_end_offset =
3034 section_begin_offset + p->data_size();
3035
3036 // Check to see if we should group the previously seens sections.
3037 switch (state)
3038 {
3039 case NO_GROUP:
3040 break;
3041
3042 case FINDING_STUB_SECTION:
3043 // Adding this section makes the group larger than GROUP_SIZE.
3044 if (section_end_offset - group_begin_offset >= group_size)
3045 {
3046 if (stubs_always_after_branch)
3047 {
3048 gold_assert(group_end != this->input_sections().end());
3049 this->create_stub_group(group_begin, group_end, group_end,
3050 target, &new_relaxed_sections);
3051 state = NO_GROUP;
3052 }
3053 else
3054 {
3055 // But wait, there's more! Input sections up to
3056 // stub_group_size bytes after the stub table can be
3057 // handled by it too.
3058 state = HAS_STUB_SECTION;
3059 stub_table = group_end;
3060 stub_table_end_offset = group_end_offset;
3061 }
3062 }
3063 break;
3064
3065 case HAS_STUB_SECTION:
3066 // Adding this section makes the post stub-section group larger
3067 // than GROUP_SIZE.
3068 if (section_end_offset - stub_table_end_offset >= group_size)
3069 {
3070 gold_assert(group_end != this->input_sections().end());
3071 this->create_stub_group(group_begin, group_end, stub_table,
3072 target, &new_relaxed_sections);
3073 state = NO_GROUP;
3074 }
3075 break;
3076
3077 default:
3078 gold_unreachable();
3079 }
3080
3081 // If we see an input section and currently there is no group, start
3082 // a new one. Skip any empty sections.
3083 if ((p->is_input_section() || p->is_relaxed_input_section())
3084 && (p->relobj()->section_size(p->shndx()) != 0))
3085 {
3086 if (state == NO_GROUP)
3087 {
3088 state = FINDING_STUB_SECTION;
3089 group_begin = p;
3090 group_begin_offset = section_begin_offset;
3091 }
3092
3093 // Keep track of the last input section seen.
3094 group_end = p;
3095 group_end_offset = section_end_offset;
3096 }
3097
3098 off = section_end_offset;
3099 }
3100
3101 // Create a stub group for any ungrouped sections.
3102 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
3103 {
3104 gold_assert(group_end != this->input_sections().end());
3105 this->create_stub_group(group_begin, group_end,
3106 (state == FINDING_STUB_SECTION
3107 ? group_end
3108 : stub_table),
3109 target, &new_relaxed_sections);
3110 }
3111
3112 // Convert input section into relaxed input section in a batch.
3113 if (!new_relaxed_sections.empty())
3114 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
3115
3116 // Update the section offsets
3117 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
3118 {
3119 Arm_relobj<big_endian>* arm_relobj =
3120 Arm_relobj<big_endian>::as_arm_relobj(
3121 new_relaxed_sections[i]->relobj());
3122 unsigned int shndx = new_relaxed_sections[i]->shndx();
3123 // Tell Arm_relobj that this input section is converted.
3124 arm_relobj->convert_input_section_to_relaxed_section(shndx);
3125 }
3126 }
3127
3128 // Arm_relobj methods.
3129
3130 // Scan relocations for stub generation.
3131
3132 template<bool big_endian>
3133 void
3134 Arm_relobj<big_endian>::scan_sections_for_stubs(
3135 Target_arm<big_endian>* arm_target,
3136 const Symbol_table* symtab,
3137 const Layout* layout)
3138 {
3139 unsigned int shnum = this->shnum();
3140 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
3141
3142 // Read the section headers.
3143 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
3144 shnum * shdr_size,
3145 true, true);
3146
3147 // To speed up processing, we set up hash tables for fast lookup of
3148 // input offsets to output addresses.
3149 this->initialize_input_to_output_maps();
3150
3151 const Relobj::Output_sections& out_sections(this->output_sections());
3152
3153 Relocate_info<32, big_endian> relinfo;
3154 relinfo.symtab = symtab;
3155 relinfo.layout = layout;
3156 relinfo.object = this;
3157
3158 const unsigned char* p = pshdrs + shdr_size;
3159 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
3160 {
3161 typename elfcpp::Shdr<32, big_endian> shdr(p);
3162
3163 unsigned int sh_type = shdr.get_sh_type();
3164 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
3165 continue;
3166
3167 off_t sh_size = shdr.get_sh_size();
3168 if (sh_size == 0)
3169 continue;
3170
3171 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
3172 if (index >= this->shnum())
3173 {
3174 // Ignore reloc section with bad info. This error will be
3175 // reported in the final link.
3176 continue;
3177 }
3178
3179 Output_section* os = out_sections[index];
3180 if (os == NULL)
3181 {
3182 // This relocation section is against a section which we
3183 // discarded.
3184 continue;
3185 }
3186 Arm_address output_offset = this->get_output_section_offset(index);
3187
3188 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
3189 {
3190 // Ignore reloc section with unexpected symbol table. The
3191 // error will be reported in the final link.
3192 continue;
3193 }
3194
3195 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
3196 sh_size, true, false);
3197
3198 unsigned int reloc_size;
3199 if (sh_type == elfcpp::SHT_REL)
3200 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
3201 else
3202 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
3203
3204 if (reloc_size != shdr.get_sh_entsize())
3205 {
3206 // Ignore reloc section with unexpected entsize. The error
3207 // will be reported in the final link.
3208 continue;
3209 }
3210
3211 size_t reloc_count = sh_size / reloc_size;
3212 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
3213 {
3214 // Ignore reloc section with uneven size. The error will be
3215 // reported in the final link.
3216 continue;
3217 }
3218
3219 gold_assert(output_offset != invalid_address
3220 || this->relocs_must_follow_section_writes());
3221
3222 // Get the section contents. This does work for the case in which
3223 // we modify the contents of an input section. We need to pass the
3224 // output view under such circumstances.
3225 section_size_type input_view_size = 0;
3226 const unsigned char* input_view =
3227 this->section_contents(index, &input_view_size, false);
3228
3229 relinfo.reloc_shndx = i;
3230 relinfo.data_shndx = index;
3231 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
3232 reloc_count, os,
3233 output_offset == invalid_address,
3234 input_view,
3235 os->address(),
3236 input_view_size);
3237 }
3238
3239 // After we've done the relocations, we release the hash tables,
3240 // since we no longer need them.
3241 this->free_input_to_output_maps();
3242 }
3243
3244 // Count the local symbols. The ARM backend needs to know if a symbol
3245 // is a THUMB function or not. For global symbols, it is easy because
3246 // the Symbol object keeps the ELF symbol type. For local symbol it is
3247 // harder because we cannot access this information. So we override the
3248 // do_count_local_symbol in parent and scan local symbols to mark
3249 // THUMB functions. This is not the most efficient way but I do not want to
3250 // slow down other ports by calling a per symbol targer hook inside
3251 // Sized_relobj<size, big_endian>::do_count_local_symbols.
3252
3253 template<bool big_endian>
3254 void
3255 Arm_relobj<big_endian>::do_count_local_symbols(
3256 Stringpool_template<char>* pool,
3257 Stringpool_template<char>* dynpool)
3258 {
3259 // We need to fix-up the values of any local symbols whose type are
3260 // STT_ARM_TFUNC.
3261
3262 // Ask parent to count the local symbols.
3263 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
3264 const unsigned int loccount = this->local_symbol_count();
3265 if (loccount == 0)
3266 return;
3267
3268 // Intialize the thumb function bit-vector.
3269 std::vector<bool> empty_vector(loccount, false);
3270 this->local_symbol_is_thumb_function_.swap(empty_vector);
3271
3272 // Read the symbol table section header.
3273 const unsigned int symtab_shndx = this->symtab_shndx();
3274 elfcpp::Shdr<32, big_endian>
3275 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
3276 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
3277
3278 // Read the local symbols.
3279 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
3280 gold_assert(loccount == symtabshdr.get_sh_info());
3281 off_t locsize = loccount * sym_size;
3282 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
3283 locsize, true, true);
3284
3285 // Loop over the local symbols and mark any local symbols pointing
3286 // to THUMB functions.
3287
3288 // Skip the first dummy symbol.
3289 psyms += sym_size;
3290 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
3291 this->local_values();
3292 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
3293 {
3294 elfcpp::Sym<32, big_endian> sym(psyms);
3295 elfcpp::STT st_type = sym.get_st_type();
3296 Symbol_value<32>& lv((*plocal_values)[i]);
3297 Arm_address input_value = lv.input_value();
3298
3299 if (st_type == elfcpp::STT_ARM_TFUNC
3300 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
3301 {
3302 // This is a THUMB function. Mark this and canonicalize the
3303 // symbol value by setting LSB.
3304 this->local_symbol_is_thumb_function_[i] = true;
3305 if ((input_value & 1) == 0)
3306 lv.set_input_value(input_value | 1);
3307 }
3308 }
3309 }
3310
3311 // Relocate sections.
3312 template<bool big_endian>
3313 void
3314 Arm_relobj<big_endian>::do_relocate_sections(
3315 const General_options& options,
3316 const Symbol_table* symtab,
3317 const Layout* layout,
3318 const unsigned char* pshdrs,
3319 typename Sized_relobj<32, big_endian>::Views* pviews)
3320 {
3321 // Call parent to relocate sections.
3322 Sized_relobj<32, big_endian>::do_relocate_sections(options, symtab, layout,
3323 pshdrs, pviews);
3324
3325 // We do not generate stubs if doing a relocatable link.
3326 if (parameters->options().relocatable())
3327 return;
3328
3329 // Relocate stub tables.
3330 unsigned int shnum = this->shnum();
3331
3332 Target_arm<big_endian>* arm_target =
3333 Target_arm<big_endian>::default_target();
3334
3335 Relocate_info<32, big_endian> relinfo;
3336 relinfo.options = &options;
3337 relinfo.symtab = symtab;
3338 relinfo.layout = layout;
3339 relinfo.object = this;
3340
3341 for (unsigned int i = 1; i < shnum; ++i)
3342 {
3343 Arm_input_section<big_endian>* arm_input_section =
3344 arm_target->find_arm_input_section(this, i);
3345
3346 if (arm_input_section == NULL
3347 || !arm_input_section->is_stub_table_owner()
3348 || arm_input_section->stub_table()->empty())
3349 continue;
3350
3351 // We cannot discard a section if it owns a stub table.
3352 Output_section* os = this->output_section(i);
3353 gold_assert(os != NULL);
3354
3355 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
3356 relinfo.reloc_shdr = NULL;
3357 relinfo.data_shndx = i;
3358 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
3359
3360 gold_assert((*pviews)[i].view != NULL);
3361
3362 // We are passed the output section view. Adjust it to cover the
3363 // stub table only.
3364 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
3365 gold_assert((stub_table->address() >= (*pviews)[i].address)
3366 && ((stub_table->address() + stub_table->data_size())
3367 <= (*pviews)[i].address + (*pviews)[i].view_size));
3368
3369 off_t offset = stub_table->address() - (*pviews)[i].address;
3370 unsigned char* view = (*pviews)[i].view + offset;
3371 Arm_address address = stub_table->address();
3372 section_size_type view_size = stub_table->data_size();
3373
3374 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
3375 view_size);
3376 }
3377 }
3378
3379 // Read the symbol information.
3380
3381 template<bool big_endian>
3382 void
3383 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
3384 {
3385 // Call parent class to read symbol information.
3386 Sized_relobj<32, big_endian>::do_read_symbols(sd);
3387
3388 // Read processor-specific flags in ELF file header.
3389 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
3390 elfcpp::Elf_sizes<32>::ehdr_size,
3391 true, false);
3392 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
3393 this->processor_specific_flags_ = ehdr.get_e_flags();
3394 }
3395
3396 // Arm_dynobj methods.
3397
3398 // Read the symbol information.
3399
3400 template<bool big_endian>
3401 void
3402 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
3403 {
3404 // Call parent class to read symbol information.
3405 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
3406
3407 // Read processor-specific flags in ELF file header.
3408 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
3409 elfcpp::Elf_sizes<32>::ehdr_size,
3410 true, false);
3411 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
3412 this->processor_specific_flags_ = ehdr.get_e_flags();
3413 }
3414
3415 // Stub_addend_reader methods.
3416
3417 // Read the addend of a REL relocation of type R_TYPE at VIEW.
3418
3419 template<bool big_endian>
3420 elfcpp::Elf_types<32>::Elf_Swxword
3421 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
3422 unsigned int r_type,
3423 const unsigned char* view,
3424 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
3425 {
3426 switch (r_type)
3427 {
3428 case elfcpp::R_ARM_CALL:
3429 case elfcpp::R_ARM_JUMP24:
3430 case elfcpp::R_ARM_PLT32:
3431 {
3432 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3433 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3434 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3435 return utils::sign_extend<26>(val << 2);
3436 }
3437
3438 case elfcpp::R_ARM_THM_CALL:
3439 case elfcpp::R_ARM_THM_JUMP24:
3440 case elfcpp::R_ARM_THM_XPC22:
3441 {
3442 // Fetch the addend. We use the Thumb-2 encoding (backwards
3443 // compatible with Thumb-1) involving the J1 and J2 bits.
3444 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3445 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3446 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3447 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3448
3449 uint32_t s = (upper_insn & (1 << 10)) >> 10;
3450 uint32_t upper = upper_insn & 0x3ff;
3451 uint32_t lower = lower_insn & 0x7ff;
3452 uint32_t j1 = (lower_insn & (1 << 13)) >> 13;
3453 uint32_t j2 = (lower_insn & (1 << 11)) >> 11;
3454 uint32_t i1 = j1 ^ s ? 0 : 1;
3455 uint32_t i2 = j2 ^ s ? 0 : 1;
3456
3457 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
3458 | (upper << 12) | (lower << 1));
3459 }
3460
3461 case elfcpp::R_ARM_THM_JUMP19:
3462 {
3463 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3464 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3465 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3466 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3467
3468 // Reconstruct the top three bits and squish the two 11 bit pieces
3469 // together.
3470 uint32_t S = (upper_insn & 0x0400) >> 10;
3471 uint32_t J1 = (lower_insn & 0x2000) >> 13;
3472 uint32_t J2 = (lower_insn & 0x0800) >> 11;
3473 uint32_t upper =
3474 (S << 8) | (J2 << 7) | (J1 << 6) | (upper_insn & 0x003f);
3475 uint32_t lower = (lower_insn & 0x07ff);
3476 return utils::sign_extend<23>((upper << 12) | (lower << 1));
3477 }
3478
3479 default:
3480 gold_unreachable();
3481 }
3482 }
3483
3484 // A class to handle the PLT data.
3485
3486 template<bool big_endian>
3487 class Output_data_plt_arm : public Output_section_data
3488 {
3489 public:
3490 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
3491 Reloc_section;
3492
3493 Output_data_plt_arm(Layout*, Output_data_space*);
3494
3495 // Add an entry to the PLT.
3496 void
3497 add_entry(Symbol* gsym);
3498
3499 // Return the .rel.plt section data.
3500 const Reloc_section*
3501 rel_plt() const
3502 { return this->rel_; }
3503
3504 protected:
3505 void
3506 do_adjust_output_section(Output_section* os);
3507
3508 // Write to a map file.
3509 void
3510 do_print_to_mapfile(Mapfile* mapfile) const
3511 { mapfile->print_output_data(this, _("** PLT")); }
3512
3513 private:
3514 // Template for the first PLT entry.
3515 static const uint32_t first_plt_entry[5];
3516
3517 // Template for subsequent PLT entries.
3518 static const uint32_t plt_entry[3];
3519
3520 // Set the final size.
3521 void
3522 set_final_data_size()
3523 {
3524 this->set_data_size(sizeof(first_plt_entry)
3525 + this->count_ * sizeof(plt_entry));
3526 }
3527
3528 // Write out the PLT data.
3529 void
3530 do_write(Output_file*);
3531
3532 // The reloc section.
3533 Reloc_section* rel_;
3534 // The .got.plt section.
3535 Output_data_space* got_plt_;
3536 // The number of PLT entries.
3537 unsigned int count_;
3538 };
3539
3540 // Create the PLT section. The ordinary .got section is an argument,
3541 // since we need to refer to the start. We also create our own .got
3542 // section just for PLT entries.
3543
3544 template<bool big_endian>
3545 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
3546 Output_data_space* got_plt)
3547 : Output_section_data(4), got_plt_(got_plt), count_(0)
3548 {
3549 this->rel_ = new Reloc_section(false);
3550 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
3551 elfcpp::SHF_ALLOC, this->rel_);
3552 }
3553
3554 template<bool big_endian>
3555 void
3556 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
3557 {
3558 os->set_entsize(0);
3559 }
3560
3561 // Add an entry to the PLT.
3562
3563 template<bool big_endian>
3564 void
3565 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
3566 {
3567 gold_assert(!gsym->has_plt_offset());
3568
3569 // Note that when setting the PLT offset we skip the initial
3570 // reserved PLT entry.
3571 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
3572 + sizeof(first_plt_entry));
3573
3574 ++this->count_;
3575
3576 section_offset_type got_offset = this->got_plt_->current_data_size();
3577
3578 // Every PLT entry needs a GOT entry which points back to the PLT
3579 // entry (this will be changed by the dynamic linker, normally
3580 // lazily when the function is called).
3581 this->got_plt_->set_current_data_size(got_offset + 4);
3582
3583 // Every PLT entry needs a reloc.
3584 gsym->set_needs_dynsym_entry();
3585 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
3586 got_offset);
3587
3588 // Note that we don't need to save the symbol. The contents of the
3589 // PLT are independent of which symbols are used. The symbols only
3590 // appear in the relocations.
3591 }
3592
3593 // ARM PLTs.
3594 // FIXME: This is not very flexible. Right now this has only been tested
3595 // on armv5te. If we are to support additional architecture features like
3596 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
3597
3598 // The first entry in the PLT.
3599 template<bool big_endian>
3600 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
3601 {
3602 0xe52de004, // str lr, [sp, #-4]!
3603 0xe59fe004, // ldr lr, [pc, #4]
3604 0xe08fe00e, // add lr, pc, lr
3605 0xe5bef008, // ldr pc, [lr, #8]!
3606 0x00000000, // &GOT[0] - .
3607 };
3608
3609 // Subsequent entries in the PLT.
3610
3611 template<bool big_endian>
3612 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
3613 {
3614 0xe28fc600, // add ip, pc, #0xNN00000
3615 0xe28cca00, // add ip, ip, #0xNN000
3616 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
3617 };
3618
3619 // Write out the PLT. This uses the hand-coded instructions above,
3620 // and adjusts them as needed. This is all specified by the arm ELF
3621 // Processor Supplement.
3622
3623 template<bool big_endian>
3624 void
3625 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
3626 {
3627 const off_t offset = this->offset();
3628 const section_size_type oview_size =
3629 convert_to_section_size_type(this->data_size());
3630 unsigned char* const oview = of->get_output_view(offset, oview_size);
3631
3632 const off_t got_file_offset = this->got_plt_->offset();
3633 const section_size_type got_size =
3634 convert_to_section_size_type(this->got_plt_->data_size());
3635 unsigned char* const got_view = of->get_output_view(got_file_offset,
3636 got_size);
3637 unsigned char* pov = oview;
3638
3639 Arm_address plt_address = this->address();
3640 Arm_address got_address = this->got_plt_->address();
3641
3642 // Write first PLT entry. All but the last word are constants.
3643 const size_t num_first_plt_words = (sizeof(first_plt_entry)
3644 / sizeof(plt_entry[0]));
3645 for (size_t i = 0; i < num_first_plt_words - 1; i++)
3646 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
3647 // Last word in first PLT entry is &GOT[0] - .
3648 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
3649 got_address - (plt_address + 16));
3650 pov += sizeof(first_plt_entry);
3651
3652 unsigned char* got_pov = got_view;
3653
3654 memset(got_pov, 0, 12);
3655 got_pov += 12;
3656
3657 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
3658 unsigned int plt_offset = sizeof(first_plt_entry);
3659 unsigned int plt_rel_offset = 0;
3660 unsigned int got_offset = 12;
3661 const unsigned int count = this->count_;
3662 for (unsigned int i = 0;
3663 i < count;
3664 ++i,
3665 pov += sizeof(plt_entry),
3666 got_pov += 4,
3667 plt_offset += sizeof(plt_entry),
3668 plt_rel_offset += rel_size,
3669 got_offset += 4)
3670 {
3671 // Set and adjust the PLT entry itself.
3672 int32_t offset = ((got_address + got_offset)
3673 - (plt_address + plt_offset + 8));
3674
3675 gold_assert(offset >= 0 && offset < 0x0fffffff);
3676 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
3677 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
3678 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
3679 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
3680 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
3681 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
3682
3683 // Set the entry in the GOT.
3684 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
3685 }
3686
3687 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
3688 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
3689
3690 of->write_output_view(offset, oview_size, oview);
3691 of->write_output_view(got_file_offset, got_size, got_view);
3692 }
3693
3694 // Create a PLT entry for a global symbol.
3695
3696 template<bool big_endian>
3697 void
3698 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
3699 Symbol* gsym)
3700 {
3701 if (gsym->has_plt_offset())
3702 return;
3703
3704 if (this->plt_ == NULL)
3705 {
3706 // Create the GOT sections first.
3707 this->got_section(symtab, layout);
3708
3709 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
3710 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
3711 (elfcpp::SHF_ALLOC
3712 | elfcpp::SHF_EXECINSTR),
3713 this->plt_);
3714 }
3715 this->plt_->add_entry(gsym);
3716 }
3717
3718 // Report an unsupported relocation against a local symbol.
3719
3720 template<bool big_endian>
3721 void
3722 Target_arm<big_endian>::Scan::unsupported_reloc_local(
3723 Sized_relobj<32, big_endian>* object,
3724 unsigned int r_type)
3725 {
3726 gold_error(_("%s: unsupported reloc %u against local symbol"),
3727 object->name().c_str(), r_type);
3728 }
3729
3730 // We are about to emit a dynamic relocation of type R_TYPE. If the
3731 // dynamic linker does not support it, issue an error. The GNU linker
3732 // only issues a non-PIC error for an allocated read-only section.
3733 // Here we know the section is allocated, but we don't know that it is
3734 // read-only. But we check for all the relocation types which the
3735 // glibc dynamic linker supports, so it seems appropriate to issue an
3736 // error even if the section is not read-only.
3737
3738 template<bool big_endian>
3739 void
3740 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
3741 unsigned int r_type)
3742 {
3743 switch (r_type)
3744 {
3745 // These are the relocation types supported by glibc for ARM.
3746 case elfcpp::R_ARM_RELATIVE:
3747 case elfcpp::R_ARM_COPY:
3748 case elfcpp::R_ARM_GLOB_DAT:
3749 case elfcpp::R_ARM_JUMP_SLOT:
3750 case elfcpp::R_ARM_ABS32:
3751 case elfcpp::R_ARM_ABS32_NOI:
3752 case elfcpp::R_ARM_PC24:
3753 // FIXME: The following 3 types are not supported by Android's dynamic
3754 // linker.
3755 case elfcpp::R_ARM_TLS_DTPMOD32:
3756 case elfcpp::R_ARM_TLS_DTPOFF32:
3757 case elfcpp::R_ARM_TLS_TPOFF32:
3758 return;
3759
3760 default:
3761 // This prevents us from issuing more than one error per reloc
3762 // section. But we can still wind up issuing more than one
3763 // error per object file.
3764 if (this->issued_non_pic_error_)
3765 return;
3766 object->error(_("requires unsupported dynamic reloc; "
3767 "recompile with -fPIC"));
3768 this->issued_non_pic_error_ = true;
3769 return;
3770
3771 case elfcpp::R_ARM_NONE:
3772 gold_unreachable();
3773 }
3774 }
3775
3776 // Scan a relocation for a local symbol.
3777 // FIXME: This only handles a subset of relocation types used by Android
3778 // on ARM v5te devices.
3779
3780 template<bool big_endian>
3781 inline void
3782 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
3783 Layout* layout,
3784 Target_arm* target,
3785 Sized_relobj<32, big_endian>* object,
3786 unsigned int data_shndx,
3787 Output_section* output_section,
3788 const elfcpp::Rel<32, big_endian>& reloc,
3789 unsigned int r_type,
3790 const elfcpp::Sym<32, big_endian>&)
3791 {
3792 r_type = get_real_reloc_type(r_type);
3793 switch (r_type)
3794 {
3795 case elfcpp::R_ARM_NONE:
3796 break;
3797
3798 case elfcpp::R_ARM_ABS32:
3799 case elfcpp::R_ARM_ABS32_NOI:
3800 // If building a shared library (or a position-independent
3801 // executable), we need to create a dynamic relocation for
3802 // this location. The relocation applied at link time will
3803 // apply the link-time value, so we flag the location with
3804 // an R_ARM_RELATIVE relocation so the dynamic loader can
3805 // relocate it easily.
3806 if (parameters->options().output_is_position_independent())
3807 {
3808 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3809 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3810 // If we are to add more other reloc types than R_ARM_ABS32,
3811 // we need to add check_non_pic(object, r_type) here.
3812 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
3813 output_section, data_shndx,
3814 reloc.get_r_offset());
3815 }
3816 break;
3817
3818 case elfcpp::R_ARM_REL32:
3819 case elfcpp::R_ARM_THM_CALL:
3820 case elfcpp::R_ARM_CALL:
3821 case elfcpp::R_ARM_PREL31:
3822 case elfcpp::R_ARM_JUMP24:
3823 case elfcpp::R_ARM_PLT32:
3824 case elfcpp::R_ARM_THM_ABS5:
3825 case elfcpp::R_ARM_ABS8:
3826 case elfcpp::R_ARM_ABS12:
3827 case elfcpp::R_ARM_ABS16:
3828 case elfcpp::R_ARM_BASE_ABS:
3829 case elfcpp::R_ARM_MOVW_ABS_NC:
3830 case elfcpp::R_ARM_MOVT_ABS:
3831 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3832 case elfcpp::R_ARM_THM_MOVT_ABS:
3833 case elfcpp::R_ARM_MOVW_PREL_NC:
3834 case elfcpp::R_ARM_MOVT_PREL:
3835 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3836 case elfcpp::R_ARM_THM_MOVT_PREL:
3837 break;
3838
3839 case elfcpp::R_ARM_GOTOFF32:
3840 // We need a GOT section:
3841 target->got_section(symtab, layout);
3842 break;
3843
3844 case elfcpp::R_ARM_BASE_PREL:
3845 // FIXME: What about this?
3846 break;
3847
3848 case elfcpp::R_ARM_GOT_BREL:
3849 case elfcpp::R_ARM_GOT_PREL:
3850 {
3851 // The symbol requires a GOT entry.
3852 Output_data_got<32, big_endian>* got =
3853 target->got_section(symtab, layout);
3854 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3855 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
3856 {
3857 // If we are generating a shared object, we need to add a
3858 // dynamic RELATIVE relocation for this symbol's GOT entry.
3859 if (parameters->options().output_is_position_independent())
3860 {
3861 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3862 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3863 rel_dyn->add_local_relative(
3864 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
3865 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
3866 }
3867 }
3868 }
3869 break;
3870
3871 case elfcpp::R_ARM_TARGET1:
3872 // This should have been mapped to another type already.
3873 // Fall through.
3874 case elfcpp::R_ARM_COPY:
3875 case elfcpp::R_ARM_GLOB_DAT:
3876 case elfcpp::R_ARM_JUMP_SLOT:
3877 case elfcpp::R_ARM_RELATIVE:
3878 // These are relocations which should only be seen by the
3879 // dynamic linker, and should never be seen here.
3880 gold_error(_("%s: unexpected reloc %u in object file"),
3881 object->name().c_str(), r_type);
3882 break;
3883
3884 default:
3885 unsupported_reloc_local(object, r_type);
3886 break;
3887 }
3888 }
3889
3890 // Report an unsupported relocation against a global symbol.
3891
3892 template<bool big_endian>
3893 void
3894 Target_arm<big_endian>::Scan::unsupported_reloc_global(
3895 Sized_relobj<32, big_endian>* object,
3896 unsigned int r_type,
3897 Symbol* gsym)
3898 {
3899 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3900 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3901 }
3902
3903 // Scan a relocation for a global symbol.
3904 // FIXME: This only handles a subset of relocation types used by Android
3905 // on ARM v5te devices.
3906
3907 template<bool big_endian>
3908 inline void
3909 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
3910 Layout* layout,
3911 Target_arm* target,
3912 Sized_relobj<32, big_endian>* object,
3913 unsigned int data_shndx,
3914 Output_section* output_section,
3915 const elfcpp::Rel<32, big_endian>& reloc,
3916 unsigned int r_type,
3917 Symbol* gsym)
3918 {
3919 r_type = get_real_reloc_type(r_type);
3920 switch (r_type)
3921 {
3922 case elfcpp::R_ARM_NONE:
3923 break;
3924
3925 case elfcpp::R_ARM_ABS32:
3926 case elfcpp::R_ARM_ABS32_NOI:
3927 {
3928 // Make a dynamic relocation if necessary.
3929 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
3930 {
3931 if (target->may_need_copy_reloc(gsym))
3932 {
3933 target->copy_reloc(symtab, layout, object,
3934 data_shndx, output_section, gsym, reloc);
3935 }
3936 else if (gsym->can_use_relative_reloc(false))
3937 {
3938 // If we are to add more other reloc types than R_ARM_ABS32,
3939 // we need to add check_non_pic(object, r_type) here.
3940 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3941 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
3942 output_section, object,
3943 data_shndx, reloc.get_r_offset());
3944 }
3945 else
3946 {
3947 // If we are to add more other reloc types than R_ARM_ABS32,
3948 // we need to add check_non_pic(object, r_type) here.
3949 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3950 rel_dyn->add_global(gsym, r_type, output_section, object,
3951 data_shndx, reloc.get_r_offset());
3952 }
3953 }
3954 }
3955 break;
3956
3957 case elfcpp::R_ARM_MOVW_ABS_NC:
3958 case elfcpp::R_ARM_MOVT_ABS:
3959 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3960 case elfcpp::R_ARM_THM_MOVT_ABS:
3961 case elfcpp::R_ARM_MOVW_PREL_NC:
3962 case elfcpp::R_ARM_MOVT_PREL:
3963 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3964 case elfcpp::R_ARM_THM_MOVT_PREL:
3965 break;
3966
3967 case elfcpp::R_ARM_THM_ABS5:
3968 case elfcpp::R_ARM_ABS8:
3969 case elfcpp::R_ARM_ABS12:
3970 case elfcpp::R_ARM_ABS16:
3971 case elfcpp::R_ARM_BASE_ABS:
3972 {
3973 // No dynamic relocs of this kinds.
3974 // Report the error in case of PIC.
3975 int flags = Symbol::NON_PIC_REF;
3976 if (gsym->type() == elfcpp::STT_FUNC
3977 || gsym->type() == elfcpp::STT_ARM_TFUNC)
3978 flags |= Symbol::FUNCTION_CALL;
3979 if (gsym->needs_dynamic_reloc(flags))
3980 check_non_pic(object, r_type);
3981 }
3982 break;
3983
3984 case elfcpp::R_ARM_REL32:
3985 case elfcpp::R_ARM_PREL31:
3986 {
3987 // Make a dynamic relocation if necessary.
3988 int flags = Symbol::NON_PIC_REF;
3989 if (gsym->needs_dynamic_reloc(flags))
3990 {
3991 if (target->may_need_copy_reloc(gsym))
3992 {
3993 target->copy_reloc(symtab, layout, object,
3994 data_shndx, output_section, gsym, reloc);
3995 }
3996 else
3997 {
3998 check_non_pic(object, r_type);
3999 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4000 rel_dyn->add_global(gsym, r_type, output_section, object,
4001 data_shndx, reloc.get_r_offset());
4002 }
4003 }
4004 }
4005 break;
4006
4007 case elfcpp::R_ARM_JUMP24:
4008 case elfcpp::R_ARM_THM_CALL:
4009 case elfcpp::R_ARM_CALL:
4010 {
4011 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
4012 target->make_plt_entry(symtab, layout, gsym);
4013 // Make a dynamic relocation if necessary.
4014 int flags = Symbol::NON_PIC_REF;
4015 if (gsym->type() == elfcpp::STT_FUNC
4016 || gsym->type() == elfcpp::STT_ARM_TFUNC)
4017 flags |= Symbol::FUNCTION_CALL;
4018 if (gsym->needs_dynamic_reloc(flags))
4019 {
4020 if (target->may_need_copy_reloc(gsym))
4021 {
4022 target->copy_reloc(symtab, layout, object,
4023 data_shndx, output_section, gsym,
4024 reloc);
4025 }
4026 else
4027 {
4028 check_non_pic(object, r_type);
4029 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4030 rel_dyn->add_global(gsym, r_type, output_section, object,
4031 data_shndx, reloc.get_r_offset());
4032 }
4033 }
4034 }
4035 break;
4036
4037 case elfcpp::R_ARM_PLT32:
4038 // If the symbol is fully resolved, this is just a relative
4039 // local reloc. Otherwise we need a PLT entry.
4040 if (gsym->final_value_is_known())
4041 break;
4042 // If building a shared library, we can also skip the PLT entry
4043 // if the symbol is defined in the output file and is protected
4044 // or hidden.
4045 if (gsym->is_defined()
4046 && !gsym->is_from_dynobj()
4047 && !gsym->is_preemptible())
4048 break;
4049 target->make_plt_entry(symtab, layout, gsym);
4050 break;
4051
4052 case elfcpp::R_ARM_GOTOFF32:
4053 // We need a GOT section.
4054 target->got_section(symtab, layout);
4055 break;
4056
4057 case elfcpp::R_ARM_BASE_PREL:
4058 // FIXME: What about this?
4059 break;
4060
4061 case elfcpp::R_ARM_GOT_BREL:
4062 case elfcpp::R_ARM_GOT_PREL:
4063 {
4064 // The symbol requires a GOT entry.
4065 Output_data_got<32, big_endian>* got =
4066 target->got_section(symtab, layout);
4067 if (gsym->final_value_is_known())
4068 got->add_global(gsym, GOT_TYPE_STANDARD);
4069 else
4070 {
4071 // If this symbol is not fully resolved, we need to add a
4072 // GOT entry with a dynamic relocation.
4073 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4074 if (gsym->is_from_dynobj()
4075 || gsym->is_undefined()
4076 || gsym->is_preemptible())
4077 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
4078 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
4079 else
4080 {
4081 if (got->add_global(gsym, GOT_TYPE_STANDARD))
4082 rel_dyn->add_global_relative(
4083 gsym, elfcpp::R_ARM_RELATIVE, got,
4084 gsym->got_offset(GOT_TYPE_STANDARD));
4085 }
4086 }
4087 }
4088 break;
4089
4090 case elfcpp::R_ARM_TARGET1:
4091 // This should have been mapped to another type already.
4092 // Fall through.
4093 case elfcpp::R_ARM_COPY:
4094 case elfcpp::R_ARM_GLOB_DAT:
4095 case elfcpp::R_ARM_JUMP_SLOT:
4096 case elfcpp::R_ARM_RELATIVE:
4097 // These are relocations which should only be seen by the
4098 // dynamic linker, and should never be seen here.
4099 gold_error(_("%s: unexpected reloc %u in object file"),
4100 object->name().c_str(), r_type);
4101 break;
4102
4103 default:
4104 unsupported_reloc_global(object, r_type, gsym);
4105 break;
4106 }
4107 }
4108
4109 // Process relocations for gc.
4110
4111 template<bool big_endian>
4112 void
4113 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
4114 Layout* layout,
4115 Sized_relobj<32, big_endian>* object,
4116 unsigned int data_shndx,
4117 unsigned int,
4118 const unsigned char* prelocs,
4119 size_t reloc_count,
4120 Output_section* output_section,
4121 bool needs_special_offset_handling,
4122 size_t local_symbol_count,
4123 const unsigned char* plocal_symbols)
4124 {
4125 typedef Target_arm<big_endian> Arm;
4126 typedef typename Target_arm<big_endian>::Scan Scan;
4127
4128 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
4129 symtab,
4130 layout,
4131 this,
4132 object,
4133 data_shndx,
4134 prelocs,
4135 reloc_count,
4136 output_section,
4137 needs_special_offset_handling,
4138 local_symbol_count,
4139 plocal_symbols);
4140 }
4141
4142 // Scan relocations for a section.
4143
4144 template<bool big_endian>
4145 void
4146 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
4147 Layout* layout,
4148 Sized_relobj<32, big_endian>* object,
4149 unsigned int data_shndx,
4150 unsigned int sh_type,
4151 const unsigned char* prelocs,
4152 size_t reloc_count,
4153 Output_section* output_section,
4154 bool needs_special_offset_handling,
4155 size_t local_symbol_count,
4156 const unsigned char* plocal_symbols)
4157 {
4158 typedef typename Target_arm<big_endian>::Scan Scan;
4159 if (sh_type == elfcpp::SHT_RELA)
4160 {
4161 gold_error(_("%s: unsupported RELA reloc section"),
4162 object->name().c_str());
4163 return;
4164 }
4165
4166 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
4167 symtab,
4168 layout,
4169 this,
4170 object,
4171 data_shndx,
4172 prelocs,
4173 reloc_count,
4174 output_section,
4175 needs_special_offset_handling,
4176 local_symbol_count,
4177 plocal_symbols);
4178 }
4179
4180 // Finalize the sections.
4181
4182 template<bool big_endian>
4183 void
4184 Target_arm<big_endian>::do_finalize_sections(
4185 Layout* layout,
4186 const Input_objects* input_objects)
4187 {
4188 // Merge processor-specific flags.
4189 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4190 p != input_objects->relobj_end();
4191 ++p)
4192 {
4193 Arm_relobj<big_endian>* arm_relobj =
4194 Arm_relobj<big_endian>::as_arm_relobj(*p);
4195 this->merge_processor_specific_flags(
4196 arm_relobj->name(),
4197 arm_relobj->processor_specific_flags());
4198 }
4199
4200 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
4201 p != input_objects->dynobj_end();
4202 ++p)
4203 {
4204 Arm_dynobj<big_endian>* arm_dynobj =
4205 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
4206 this->merge_processor_specific_flags(
4207 arm_dynobj->name(),
4208 arm_dynobj->processor_specific_flags());
4209 }
4210
4211 // Fill in some more dynamic tags.
4212 Output_data_dynamic* const odyn = layout->dynamic_data();
4213 if (odyn != NULL)
4214 {
4215 if (this->got_plt_ != NULL)
4216 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
4217
4218 if (this->plt_ != NULL)
4219 {
4220 const Output_data* od = this->plt_->rel_plt();
4221 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
4222 odyn->add_section_address(elfcpp::DT_JMPREL, od);
4223 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
4224 }
4225
4226 if (this->rel_dyn_ != NULL)
4227 {
4228 const Output_data* od = this->rel_dyn_;
4229 odyn->add_section_address(elfcpp::DT_REL, od);
4230 odyn->add_section_size(elfcpp::DT_RELSZ, od);
4231 odyn->add_constant(elfcpp::DT_RELENT,
4232 elfcpp::Elf_sizes<32>::rel_size);
4233 }
4234
4235 if (!parameters->options().shared())
4236 {
4237 // The value of the DT_DEBUG tag is filled in by the dynamic
4238 // linker at run time, and used by the debugger.
4239 odyn->add_constant(elfcpp::DT_DEBUG, 0);
4240 }
4241 }
4242
4243 // Emit any relocs we saved in an attempt to avoid generating COPY
4244 // relocs.
4245 if (this->copy_relocs_.any_saved_relocs())
4246 this->copy_relocs_.emit(this->rel_dyn_section(layout));
4247
4248 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4249 // the .ARM.exidx section.
4250 if (!layout->script_options()->saw_phdrs_clause()
4251 && !parameters->options().relocatable())
4252 {
4253 Output_section* exidx_section =
4254 layout->find_output_section(".ARM.exidx");
4255
4256 if (exidx_section != NULL
4257 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
4258 {
4259 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
4260 == NULL);
4261 Output_segment* exidx_segment =
4262 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
4263 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
4264 }
4265 }
4266 }
4267
4268 // Return whether a direct absolute static relocation needs to be applied.
4269 // In cases where Scan::local() or Scan::global() has created
4270 // a dynamic relocation other than R_ARM_RELATIVE, the addend
4271 // of the relocation is carried in the data, and we must not
4272 // apply the static relocation.
4273
4274 template<bool big_endian>
4275 inline bool
4276 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
4277 const Sized_symbol<32>* gsym,
4278 int ref_flags,
4279 bool is_32bit,
4280 Output_section* output_section)
4281 {
4282 // If the output section is not allocated, then we didn't call
4283 // scan_relocs, we didn't create a dynamic reloc, and we must apply
4284 // the reloc here.
4285 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
4286 return true;
4287
4288 // For local symbols, we will have created a non-RELATIVE dynamic
4289 // relocation only if (a) the output is position independent,
4290 // (b) the relocation is absolute (not pc- or segment-relative), and
4291 // (c) the relocation is not 32 bits wide.
4292 if (gsym == NULL)
4293 return !(parameters->options().output_is_position_independent()
4294 && (ref_flags & Symbol::ABSOLUTE_REF)
4295 && !is_32bit);
4296
4297 // For global symbols, we use the same helper routines used in the
4298 // scan pass. If we did not create a dynamic relocation, or if we
4299 // created a RELATIVE dynamic relocation, we should apply the static
4300 // relocation.
4301 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
4302 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
4303 && gsym->can_use_relative_reloc(ref_flags
4304 & Symbol::FUNCTION_CALL);
4305 return !has_dyn || is_rel;
4306 }
4307
4308 // Perform a relocation.
4309
4310 template<bool big_endian>
4311 inline bool
4312 Target_arm<big_endian>::Relocate::relocate(
4313 const Relocate_info<32, big_endian>* relinfo,
4314 Target_arm* target,
4315 Output_section *output_section,
4316 size_t relnum,
4317 const elfcpp::Rel<32, big_endian>& rel,
4318 unsigned int r_type,
4319 const Sized_symbol<32>* gsym,
4320 const Symbol_value<32>* psymval,
4321 unsigned char* view,
4322 Arm_address address,
4323 section_size_type /* view_size */ )
4324 {
4325 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
4326
4327 r_type = get_real_reloc_type(r_type);
4328
4329 // If this the symbol may be a Thumb function, set thumb bit to 1.
4330 bool has_thumb_bit = ((gsym != NULL)
4331 && (gsym->type() == elfcpp::STT_FUNC
4332 || gsym->type() == elfcpp::STT_ARM_TFUNC));
4333
4334 // Pick the value to use for symbols defined in shared objects.
4335 Symbol_value<32> symval;
4336 if (gsym != NULL
4337 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
4338 {
4339 symval.set_output_value(target->plt_section()->address()
4340 + gsym->plt_offset());
4341 psymval = &symval;
4342 has_thumb_bit = 0;
4343 }
4344
4345 const Sized_relobj<32, big_endian>* object = relinfo->object;
4346
4347 // Get the GOT offset if needed.
4348 // The GOT pointer points to the end of the GOT section.
4349 // We need to subtract the size of the GOT section to get
4350 // the actual offset to use in the relocation.
4351 bool have_got_offset = false;
4352 unsigned int got_offset = 0;
4353 switch (r_type)
4354 {
4355 case elfcpp::R_ARM_GOT_BREL:
4356 case elfcpp::R_ARM_GOT_PREL:
4357 if (gsym != NULL)
4358 {
4359 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4360 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4361 - target->got_size());
4362 }
4363 else
4364 {
4365 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
4366 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4367 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4368 - target->got_size());
4369 }
4370 have_got_offset = true;
4371 break;
4372
4373 default:
4374 break;
4375 }
4376
4377 typename Arm_relocate_functions::Status reloc_status =
4378 Arm_relocate_functions::STATUS_OKAY;
4379 switch (r_type)
4380 {
4381 case elfcpp::R_ARM_NONE:
4382 break;
4383
4384 case elfcpp::R_ARM_ABS8:
4385 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4386 output_section))
4387 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
4388 break;
4389
4390 case elfcpp::R_ARM_ABS12:
4391 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4392 output_section))
4393 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
4394 break;
4395
4396 case elfcpp::R_ARM_ABS16:
4397 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4398 output_section))
4399 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
4400 break;
4401
4402 case elfcpp::R_ARM_ABS32:
4403 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4404 output_section))
4405 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
4406 has_thumb_bit);
4407 break;
4408
4409 case elfcpp::R_ARM_ABS32_NOI:
4410 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4411 output_section))
4412 // No thumb bit for this relocation: (S + A)
4413 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
4414 false);
4415 break;
4416
4417 case elfcpp::R_ARM_MOVW_ABS_NC:
4418 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4419 output_section))
4420 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
4421 psymval,
4422 has_thumb_bit);
4423 else
4424 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
4425 "a shared object; recompile with -fPIC"));
4426 break;
4427
4428 case elfcpp::R_ARM_MOVT_ABS:
4429 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4430 output_section))
4431 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
4432 else
4433 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
4434 "a shared object; recompile with -fPIC"));
4435 break;
4436
4437 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4438 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4439 output_section))
4440 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
4441 psymval,
4442 has_thumb_bit);
4443 else
4444 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
4445 "making a shared object; recompile with -fPIC"));
4446 break;
4447
4448 case elfcpp::R_ARM_THM_MOVT_ABS:
4449 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4450 output_section))
4451 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
4452 psymval);
4453 else
4454 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
4455 "making a shared object; recompile with -fPIC"));
4456 break;
4457
4458 case elfcpp::R_ARM_MOVW_PREL_NC:
4459 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
4460 psymval, address,
4461 has_thumb_bit);
4462 break;
4463
4464 case elfcpp::R_ARM_MOVT_PREL:
4465 reloc_status = Arm_relocate_functions::movt_prel(view, object,
4466 psymval, address);
4467 break;
4468
4469 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4470 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
4471 psymval, address,
4472 has_thumb_bit);
4473 break;
4474
4475 case elfcpp::R_ARM_THM_MOVT_PREL:
4476 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
4477 psymval, address);
4478 break;
4479
4480 case elfcpp::R_ARM_REL32:
4481 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
4482 address, has_thumb_bit);
4483 break;
4484
4485 case elfcpp::R_ARM_THM_ABS5:
4486 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4487 output_section))
4488 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
4489 break;
4490
4491 case elfcpp::R_ARM_THM_CALL:
4492 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
4493 address, has_thumb_bit);
4494 break;
4495
4496 case elfcpp::R_ARM_GOTOFF32:
4497 {
4498 Arm_address got_origin;
4499 got_origin = target->got_plt_section()->address();
4500 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
4501 got_origin, has_thumb_bit);
4502 }
4503 break;
4504
4505 case elfcpp::R_ARM_BASE_PREL:
4506 {
4507 uint32_t origin;
4508 // Get the addressing origin of the output segment defining the
4509 // symbol gsym (AAELF 4.6.1.2 Relocation types)
4510 gold_assert(gsym != NULL);
4511 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
4512 origin = gsym->output_segment()->vaddr();
4513 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
4514 origin = gsym->output_data()->address();
4515 else
4516 {
4517 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4518 _("cannot find origin of R_ARM_BASE_PREL"));
4519 return true;
4520 }
4521 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
4522 }
4523 break;
4524
4525 case elfcpp::R_ARM_BASE_ABS:
4526 {
4527 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4528 output_section))
4529 break;
4530
4531 uint32_t origin;
4532 // Get the addressing origin of the output segment defining
4533 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
4534 if (gsym == NULL)
4535 // R_ARM_BASE_ABS with the NULL symbol will give the
4536 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
4537 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
4538 origin = target->got_plt_section()->address();
4539 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
4540 origin = gsym->output_segment()->vaddr();
4541 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
4542 origin = gsym->output_data()->address();
4543 else
4544 {
4545 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4546 _("cannot find origin of R_ARM_BASE_ABS"));
4547 return true;
4548 }
4549
4550 reloc_status = Arm_relocate_functions::base_abs(view, origin);
4551 }
4552 break;
4553
4554 case elfcpp::R_ARM_GOT_BREL:
4555 gold_assert(have_got_offset);
4556 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
4557 break;
4558
4559 case elfcpp::R_ARM_GOT_PREL:
4560 gold_assert(have_got_offset);
4561 // Get the address origin for GOT PLT, which is allocated right
4562 // after the GOT section, to calculate an absolute address of
4563 // the symbol GOT entry (got_origin + got_offset).
4564 Arm_address got_origin;
4565 got_origin = target->got_plt_section()->address();
4566 reloc_status = Arm_relocate_functions::got_prel(view,
4567 got_origin + got_offset,
4568 address);
4569 break;
4570
4571 case elfcpp::R_ARM_PLT32:
4572 gold_assert(gsym == NULL
4573 || gsym->has_plt_offset()
4574 || gsym->final_value_is_known()
4575 || (gsym->is_defined()
4576 && !gsym->is_from_dynobj()
4577 && !gsym->is_preemptible()));
4578 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
4579 address, has_thumb_bit);
4580 break;
4581
4582 case elfcpp::R_ARM_CALL:
4583 reloc_status = Arm_relocate_functions::call(view, object, psymval,
4584 address, has_thumb_bit);
4585 break;
4586
4587 case elfcpp::R_ARM_JUMP24:
4588 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
4589 address, has_thumb_bit);
4590 break;
4591
4592 case elfcpp::R_ARM_PREL31:
4593 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
4594 address, has_thumb_bit);
4595 break;
4596
4597 case elfcpp::R_ARM_TARGET1:
4598 // This should have been mapped to another type already.
4599 // Fall through.
4600 case elfcpp::R_ARM_COPY:
4601 case elfcpp::R_ARM_GLOB_DAT:
4602 case elfcpp::R_ARM_JUMP_SLOT:
4603 case elfcpp::R_ARM_RELATIVE:
4604 // These are relocations which should only be seen by the
4605 // dynamic linker, and should never be seen here.
4606 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4607 _("unexpected reloc %u in object file"),
4608 r_type);
4609 break;
4610
4611 default:
4612 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4613 _("unsupported reloc %u"),
4614 r_type);
4615 break;
4616 }
4617
4618 // Report any errors.
4619 switch (reloc_status)
4620 {
4621 case Arm_relocate_functions::STATUS_OKAY:
4622 break;
4623 case Arm_relocate_functions::STATUS_OVERFLOW:
4624 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4625 _("relocation overflow in relocation %u"),
4626 r_type);
4627 break;
4628 case Arm_relocate_functions::STATUS_BAD_RELOC:
4629 gold_error_at_location(
4630 relinfo,
4631 relnum,
4632 rel.get_r_offset(),
4633 _("unexpected opcode while processing relocation %u"),
4634 r_type);
4635 break;
4636 default:
4637 gold_unreachable();
4638 }
4639
4640 return true;
4641 }
4642
4643 // Relocate section data.
4644
4645 template<bool big_endian>
4646 void
4647 Target_arm<big_endian>::relocate_section(
4648 const Relocate_info<32, big_endian>* relinfo,
4649 unsigned int sh_type,
4650 const unsigned char* prelocs,
4651 size_t reloc_count,
4652 Output_section* output_section,
4653 bool needs_special_offset_handling,
4654 unsigned char* view,
4655 Arm_address address,
4656 section_size_type view_size,
4657 const Reloc_symbol_changes* reloc_symbol_changes)
4658 {
4659 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
4660 gold_assert(sh_type == elfcpp::SHT_REL);
4661
4662 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
4663 Arm_relocate>(
4664 relinfo,
4665 this,
4666 prelocs,
4667 reloc_count,
4668 output_section,
4669 needs_special_offset_handling,
4670 view,
4671 address,
4672 view_size,
4673 reloc_symbol_changes);
4674 }
4675
4676 // Return the size of a relocation while scanning during a relocatable
4677 // link.
4678
4679 template<bool big_endian>
4680 unsigned int
4681 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
4682 unsigned int r_type,
4683 Relobj* object)
4684 {
4685 r_type = get_real_reloc_type(r_type);
4686 switch (r_type)
4687 {
4688 case elfcpp::R_ARM_NONE:
4689 return 0;
4690
4691 case elfcpp::R_ARM_ABS8:
4692 return 1;
4693
4694 case elfcpp::R_ARM_ABS16:
4695 case elfcpp::R_ARM_THM_ABS5:
4696 return 2;
4697
4698 case elfcpp::R_ARM_ABS32:
4699 case elfcpp::R_ARM_ABS32_NOI:
4700 case elfcpp::R_ARM_ABS12:
4701 case elfcpp::R_ARM_BASE_ABS:
4702 case elfcpp::R_ARM_REL32:
4703 case elfcpp::R_ARM_THM_CALL:
4704 case elfcpp::R_ARM_GOTOFF32:
4705 case elfcpp::R_ARM_BASE_PREL:
4706 case elfcpp::R_ARM_GOT_BREL:
4707 case elfcpp::R_ARM_GOT_PREL:
4708 case elfcpp::R_ARM_PLT32:
4709 case elfcpp::R_ARM_CALL:
4710 case elfcpp::R_ARM_JUMP24:
4711 case elfcpp::R_ARM_PREL31:
4712 case elfcpp::R_ARM_MOVW_ABS_NC:
4713 case elfcpp::R_ARM_MOVT_ABS:
4714 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4715 case elfcpp::R_ARM_THM_MOVT_ABS:
4716 case elfcpp::R_ARM_MOVW_PREL_NC:
4717 case elfcpp::R_ARM_MOVT_PREL:
4718 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4719 case elfcpp::R_ARM_THM_MOVT_PREL:
4720 return 4;
4721
4722 case elfcpp::R_ARM_TARGET1:
4723 // This should have been mapped to another type already.
4724 // Fall through.
4725 case elfcpp::R_ARM_COPY:
4726 case elfcpp::R_ARM_GLOB_DAT:
4727 case elfcpp::R_ARM_JUMP_SLOT:
4728 case elfcpp::R_ARM_RELATIVE:
4729 // These are relocations which should only be seen by the
4730 // dynamic linker, and should never be seen here.
4731 gold_error(_("%s: unexpected reloc %u in object file"),
4732 object->name().c_str(), r_type);
4733 return 0;
4734
4735 default:
4736 object->error(_("unsupported reloc %u in object file"), r_type);
4737 return 0;
4738 }
4739 }
4740
4741 // Scan the relocs during a relocatable link.
4742
4743 template<bool big_endian>
4744 void
4745 Target_arm<big_endian>::scan_relocatable_relocs(
4746 Symbol_table* symtab,
4747 Layout* layout,
4748 Sized_relobj<32, big_endian>* object,
4749 unsigned int data_shndx,
4750 unsigned int sh_type,
4751 const unsigned char* prelocs,
4752 size_t reloc_count,
4753 Output_section* output_section,
4754 bool needs_special_offset_handling,
4755 size_t local_symbol_count,
4756 const unsigned char* plocal_symbols,
4757 Relocatable_relocs* rr)
4758 {
4759 gold_assert(sh_type == elfcpp::SHT_REL);
4760
4761 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
4762 Relocatable_size_for_reloc> Scan_relocatable_relocs;
4763
4764 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
4765 Scan_relocatable_relocs>(
4766 symtab,
4767 layout,
4768 object,
4769 data_shndx,
4770 prelocs,
4771 reloc_count,
4772 output_section,
4773 needs_special_offset_handling,
4774 local_symbol_count,
4775 plocal_symbols,
4776 rr);
4777 }
4778
4779 // Relocate a section during a relocatable link.
4780
4781 template<bool big_endian>
4782 void
4783 Target_arm<big_endian>::relocate_for_relocatable(
4784 const Relocate_info<32, big_endian>* relinfo,
4785 unsigned int sh_type,
4786 const unsigned char* prelocs,
4787 size_t reloc_count,
4788 Output_section* output_section,
4789 off_t offset_in_output_section,
4790 const Relocatable_relocs* rr,
4791 unsigned char* view,
4792 Arm_address view_address,
4793 section_size_type view_size,
4794 unsigned char* reloc_view,
4795 section_size_type reloc_view_size)
4796 {
4797 gold_assert(sh_type == elfcpp::SHT_REL);
4798
4799 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
4800 relinfo,
4801 prelocs,
4802 reloc_count,
4803 output_section,
4804 offset_in_output_section,
4805 rr,
4806 view,
4807 view_address,
4808 view_size,
4809 reloc_view,
4810 reloc_view_size);
4811 }
4812
4813 // Return the value to use for a dynamic symbol which requires special
4814 // treatment. This is how we support equality comparisons of function
4815 // pointers across shared library boundaries, as described in the
4816 // processor specific ABI supplement.
4817
4818 template<bool big_endian>
4819 uint64_t
4820 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4821 {
4822 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4823 return this->plt_section()->address() + gsym->plt_offset();
4824 }
4825
4826 // Map platform-specific relocs to real relocs
4827 //
4828 template<bool big_endian>
4829 unsigned int
4830 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
4831 {
4832 switch (r_type)
4833 {
4834 case elfcpp::R_ARM_TARGET1:
4835 // This is either R_ARM_ABS32 or R_ARM_REL32;
4836 return elfcpp::R_ARM_ABS32;
4837
4838 case elfcpp::R_ARM_TARGET2:
4839 // This can be any reloc type but ususally is R_ARM_GOT_PREL
4840 return elfcpp::R_ARM_GOT_PREL;
4841
4842 default:
4843 return r_type;
4844 }
4845 }
4846
4847 // Whether if two EABI versions V1 and V2 are compatible.
4848
4849 template<bool big_endian>
4850 bool
4851 Target_arm<big_endian>::are_eabi_versions_compatible(
4852 elfcpp::Elf_Word v1,
4853 elfcpp::Elf_Word v2)
4854 {
4855 // v4 and v5 are the same spec before and after it was released,
4856 // so allow mixing them.
4857 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
4858 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
4859 return true;
4860
4861 return v1 == v2;
4862 }
4863
4864 // Combine FLAGS from an input object called NAME and the processor-specific
4865 // flags in the ELF header of the output. Much of this is adapted from the
4866 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
4867 // in bfd/elf32-arm.c.
4868
4869 template<bool big_endian>
4870 void
4871 Target_arm<big_endian>::merge_processor_specific_flags(
4872 const std::string& name,
4873 elfcpp::Elf_Word flags)
4874 {
4875 if (this->are_processor_specific_flags_set())
4876 {
4877 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
4878
4879 // Nothing to merge if flags equal to those in output.
4880 if (flags == out_flags)
4881 return;
4882
4883 // Complain about various flag mismatches.
4884 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
4885 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
4886 if (!this->are_eabi_versions_compatible(version1, version2))
4887 gold_error(_("Source object %s has EABI version %d but output has "
4888 "EABI version %d."),
4889 name.c_str(),
4890 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
4891 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
4892 }
4893 else
4894 {
4895 // If the input is the default architecture and had the default
4896 // flags then do not bother setting the flags for the output
4897 // architecture, instead allow future merges to do this. If no
4898 // future merges ever set these flags then they will retain their
4899 // uninitialised values, which surprise surprise, correspond
4900 // to the default values.
4901 if (flags == 0)
4902 return;
4903
4904 // This is the first time, just copy the flags.
4905 // We only copy the EABI version for now.
4906 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
4907 }
4908 }
4909
4910 // Adjust ELF file header.
4911 template<bool big_endian>
4912 void
4913 Target_arm<big_endian>::do_adjust_elf_header(
4914 unsigned char* view,
4915 int len) const
4916 {
4917 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
4918
4919 elfcpp::Ehdr<32, big_endian> ehdr(view);
4920 unsigned char e_ident[elfcpp::EI_NIDENT];
4921 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
4922
4923 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
4924 == elfcpp::EF_ARM_EABI_UNKNOWN)
4925 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
4926 else
4927 e_ident[elfcpp::EI_OSABI] = 0;
4928 e_ident[elfcpp::EI_ABIVERSION] = 0;
4929
4930 // FIXME: Do EF_ARM_BE8 adjustment.
4931
4932 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
4933 oehdr.put_e_ident(e_ident);
4934 }
4935
4936 // do_make_elf_object to override the same function in the base class.
4937 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
4938 // to store ARM specific information. Hence we need to have our own
4939 // ELF object creation.
4940
4941 template<bool big_endian>
4942 Object*
4943 Target_arm<big_endian>::do_make_elf_object(
4944 const std::string& name,
4945 Input_file* input_file,
4946 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
4947 {
4948 int et = ehdr.get_e_type();
4949 if (et == elfcpp::ET_REL)
4950 {
4951 Arm_relobj<big_endian>* obj =
4952 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
4953 obj->setup();
4954 return obj;
4955 }
4956 else if (et == elfcpp::ET_DYN)
4957 {
4958 Sized_dynobj<32, big_endian>* obj =
4959 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
4960 obj->setup();
4961 return obj;
4962 }
4963 else
4964 {
4965 gold_error(_("%s: unsupported ELF file type %d"),
4966 name.c_str(), et);
4967 return NULL;
4968 }
4969 }
4970
4971 // The selector for arm object files.
4972
4973 template<bool big_endian>
4974 class Target_selector_arm : public Target_selector
4975 {
4976 public:
4977 Target_selector_arm()
4978 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
4979 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
4980 { }
4981
4982 Target*
4983 do_instantiate_target()
4984 { return new Target_arm<big_endian>(); }
4985 };
4986
4987 Target_selector_arm<false> target_selector_arm;
4988 Target_selector_arm<true> target_selector_armbe;
4989
4990 } // End anonymous namespace.
This page took 0.131302 seconds and 4 git commands to generate.