[gold] Rename '--fix-cortex-a53' to '--fix-cortex-a53-843419'.
[deliverable/binutils-gdb.git] / gold / aarch64.cc
1 // aarch64.cc -- aarch64 target support for gold.
2
3 // Copyright (C) 2014-2015 Free Software Foundation, Inc.
4 // Written by Jing Yu <jingyu@google.com> and Han Shen <shenhan@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstring>
26 #include <map>
27
28 #include "elfcpp.h"
29 #include "dwarf.h"
30 #include "parameters.h"
31 #include "reloc.h"
32 #include "aarch64.h"
33 #include "object.h"
34 #include "symtab.h"
35 #include "layout.h"
36 #include "output.h"
37 #include "copy-relocs.h"
38 #include "target.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
41 #include "tls.h"
42 #include "freebsd.h"
43 #include "nacl.h"
44 #include "gc.h"
45 #include "icf.h"
46 #include "aarch64-reloc-property.h"
47
48 // The first three .got.plt entries are reserved.
49 const int32_t AARCH64_GOTPLT_RESERVE_COUNT = 3;
50
51
52 namespace
53 {
54
55 using namespace gold;
56
57 template<int size, bool big_endian>
58 class Output_data_plt_aarch64;
59
60 template<int size, bool big_endian>
61 class Output_data_plt_aarch64_standard;
62
63 template<int size, bool big_endian>
64 class Target_aarch64;
65
66 template<int size, bool big_endian>
67 class AArch64_relocate_functions;
68
69 // Utility class dealing with insns. This is ported from macros in
70 // bfd/elfnn-aarch64.cc, but wrapped inside a class as static members. This
71 // class is used in erratum sequence scanning.
72
73 template<bool big_endian>
74 class AArch64_insn_utilities
75 {
76 public:
77 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
78
79 static const int BYTES_PER_INSN = 4;
80
81 static unsigned int
82 aarch64_bit(Insntype insn, int pos)
83 { return ((1 << pos) & insn) >> pos; }
84
85 static unsigned int
86 aarch64_bits(Insntype insn, int pos, int l)
87 { return (insn >> pos) & ((1 << l) - 1); }
88
89 static bool
90 is_adrp(const Insntype insn)
91 { return (insn & 0x9F000000) == 0x90000000; }
92
93 static unsigned int
94 aarch64_rm(const Insntype insn)
95 { return aarch64_bits(insn, 16, 5); }
96
97 static unsigned int
98 aarch64_rn(const Insntype insn)
99 { return aarch64_bits(insn, 5, 5); }
100
101 static unsigned int
102 aarch64_rd(const Insntype insn)
103 { return aarch64_bits(insn, 0, 5); }
104
105 static unsigned int
106 aarch64_rt(const Insntype insn)
107 { return aarch64_bits(insn, 0, 5); }
108
109 static unsigned int
110 aarch64_rt2(const Insntype insn)
111 { return aarch64_bits(insn, 10, 5); }
112
113 static bool
114 aarch64_b(const Insntype insn)
115 { return (insn & 0xFC000000) == 0x14000000; }
116
117 static bool
118 aarch64_bl(const Insntype insn)
119 { return (insn & 0xFC000000) == 0x94000000; }
120
121 static bool
122 aarch64_blr(const Insntype insn)
123 { return (insn & 0xFFFFFC1F) == 0xD63F0000; }
124
125 static bool
126 aarch64_br(const Insntype insn)
127 { return (insn & 0xFFFFFC1F) == 0xD61F0000; }
128
129 // All ld/st ops. See C4-182 of the ARM ARM. The encoding space for
130 // LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops.
131 static bool
132 aarch64_ld(Insntype insn) { return aarch64_bit(insn, 22) == 1; }
133
134 static bool
135 aarch64_ldst(Insntype insn)
136 { return (insn & 0x0a000000) == 0x08000000; }
137
138 static bool
139 aarch64_ldst_ex(Insntype insn)
140 { return (insn & 0x3f000000) == 0x08000000; }
141
142 static bool
143 aarch64_ldst_pcrel(Insntype insn)
144 { return (insn & 0x3b000000) == 0x18000000; }
145
146 static bool
147 aarch64_ldst_nap(Insntype insn)
148 { return (insn & 0x3b800000) == 0x28000000; }
149
150 static bool
151 aarch64_ldstp_pi(Insntype insn)
152 { return (insn & 0x3b800000) == 0x28800000; }
153
154 static bool
155 aarch64_ldstp_o(Insntype insn)
156 { return (insn & 0x3b800000) == 0x29000000; }
157
158 static bool
159 aarch64_ldstp_pre(Insntype insn)
160 { return (insn & 0x3b800000) == 0x29800000; }
161
162 static bool
163 aarch64_ldst_ui(Insntype insn)
164 { return (insn & 0x3b200c00) == 0x38000000; }
165
166 static bool
167 aarch64_ldst_piimm(Insntype insn)
168 { return (insn & 0x3b200c00) == 0x38000400; }
169
170 static bool
171 aarch64_ldst_u(Insntype insn)
172 { return (insn & 0x3b200c00) == 0x38000800; }
173
174 static bool
175 aarch64_ldst_preimm(Insntype insn)
176 { return (insn & 0x3b200c00) == 0x38000c00; }
177
178 static bool
179 aarch64_ldst_ro(Insntype insn)
180 { return (insn & 0x3b200c00) == 0x38200800; }
181
182 static bool
183 aarch64_ldst_uimm(Insntype insn)
184 { return (insn & 0x3b000000) == 0x39000000; }
185
186 static bool
187 aarch64_ldst_simd_m(Insntype insn)
188 { return (insn & 0xbfbf0000) == 0x0c000000; }
189
190 static bool
191 aarch64_ldst_simd_m_pi(Insntype insn)
192 { return (insn & 0xbfa00000) == 0x0c800000; }
193
194 static bool
195 aarch64_ldst_simd_s(Insntype insn)
196 { return (insn & 0xbf9f0000) == 0x0d000000; }
197
198 static bool
199 aarch64_ldst_simd_s_pi(Insntype insn)
200 { return (insn & 0xbf800000) == 0x0d800000; }
201
202 // Classify an INSN if it is indeed a load/store. Return true if INSN is a
203 // LD/ST instruction otherwise return false. For scalar LD/ST instructions
204 // PAIR is FALSE, RT is returned and RT2 is set equal to RT. For LD/ST pair
205 // instructions PAIR is TRUE, RT and RT2 are returned.
206 static bool
207 aarch64_mem_op_p(Insntype insn, unsigned int *rt, unsigned int *rt2,
208 bool *pair, bool *load)
209 {
210 uint32_t opcode;
211 unsigned int r;
212 uint32_t opc = 0;
213 uint32_t v = 0;
214 uint32_t opc_v = 0;
215
216 /* Bail out quickly if INSN doesn't fall into the the load-store
217 encoding space. */
218 if (!aarch64_ldst (insn))
219 return false;
220
221 *pair = false;
222 *load = false;
223 if (aarch64_ldst_ex (insn))
224 {
225 *rt = aarch64_rt (insn);
226 *rt2 = *rt;
227 if (aarch64_bit (insn, 21) == 1)
228 {
229 *pair = true;
230 *rt2 = aarch64_rt2 (insn);
231 }
232 *load = aarch64_ld (insn);
233 return true;
234 }
235 else if (aarch64_ldst_nap (insn)
236 || aarch64_ldstp_pi (insn)
237 || aarch64_ldstp_o (insn)
238 || aarch64_ldstp_pre (insn))
239 {
240 *pair = true;
241 *rt = aarch64_rt (insn);
242 *rt2 = aarch64_rt2 (insn);
243 *load = aarch64_ld (insn);
244 return true;
245 }
246 else if (aarch64_ldst_pcrel (insn)
247 || aarch64_ldst_ui (insn)
248 || aarch64_ldst_piimm (insn)
249 || aarch64_ldst_u (insn)
250 || aarch64_ldst_preimm (insn)
251 || aarch64_ldst_ro (insn)
252 || aarch64_ldst_uimm (insn))
253 {
254 *rt = aarch64_rt (insn);
255 *rt2 = *rt;
256 if (aarch64_ldst_pcrel (insn))
257 *load = true;
258 opc = aarch64_bits (insn, 22, 2);
259 v = aarch64_bit (insn, 26);
260 opc_v = opc | (v << 2);
261 *load = (opc_v == 1 || opc_v == 2 || opc_v == 3
262 || opc_v == 5 || opc_v == 7);
263 return true;
264 }
265 else if (aarch64_ldst_simd_m (insn)
266 || aarch64_ldst_simd_m_pi (insn))
267 {
268 *rt = aarch64_rt (insn);
269 *load = aarch64_bit (insn, 22);
270 opcode = (insn >> 12) & 0xf;
271 switch (opcode)
272 {
273 case 0:
274 case 2:
275 *rt2 = *rt + 3;
276 break;
277
278 case 4:
279 case 6:
280 *rt2 = *rt + 2;
281 break;
282
283 case 7:
284 *rt2 = *rt;
285 break;
286
287 case 8:
288 case 10:
289 *rt2 = *rt + 1;
290 break;
291
292 default:
293 return false;
294 }
295 return true;
296 }
297 else if (aarch64_ldst_simd_s (insn)
298 || aarch64_ldst_simd_s_pi (insn))
299 {
300 *rt = aarch64_rt (insn);
301 r = (insn >> 21) & 1;
302 *load = aarch64_bit (insn, 22);
303 opcode = (insn >> 13) & 0x7;
304 switch (opcode)
305 {
306 case 0:
307 case 2:
308 case 4:
309 *rt2 = *rt + r;
310 break;
311
312 case 1:
313 case 3:
314 case 5:
315 *rt2 = *rt + (r == 0 ? 2 : 3);
316 break;
317
318 case 6:
319 *rt2 = *rt + r;
320 break;
321
322 case 7:
323 *rt2 = *rt + (r == 0 ? 2 : 3);
324 break;
325
326 default:
327 return false;
328 }
329 return true;
330 }
331 return false;
332 }
333 };
334
335
336 // Output_data_got_aarch64 class.
337
338 template<int size, bool big_endian>
339 class Output_data_got_aarch64 : public Output_data_got<size, big_endian>
340 {
341 public:
342 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
343 Output_data_got_aarch64(Symbol_table* symtab, Layout* layout)
344 : Output_data_got<size, big_endian>(),
345 symbol_table_(symtab), layout_(layout)
346 { }
347
348 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
349 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
350 // applied in a static link.
351 void
352 add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
353 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
354
355
356 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
357 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
358 // relocation that needs to be applied in a static link.
359 void
360 add_static_reloc(unsigned int got_offset, unsigned int r_type,
361 Sized_relobj_file<size, big_endian>* relobj,
362 unsigned int index)
363 {
364 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
365 index));
366 }
367
368
369 protected:
370 // Write out the GOT table.
371 void
372 do_write(Output_file* of) {
373 // The first entry in the GOT is the address of the .dynamic section.
374 gold_assert(this->data_size() >= size / 8);
375 Output_section* dynamic = this->layout_->dynamic_section();
376 Valtype dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
377 this->replace_constant(0, dynamic_addr);
378 Output_data_got<size, big_endian>::do_write(of);
379
380 // Handling static relocs
381 if (this->static_relocs_.empty())
382 return;
383
384 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
385
386 gold_assert(parameters->doing_static_link());
387 const off_t offset = this->offset();
388 const section_size_type oview_size =
389 convert_to_section_size_type(this->data_size());
390 unsigned char* const oview = of->get_output_view(offset, oview_size);
391
392 Output_segment* tls_segment = this->layout_->tls_segment();
393 gold_assert(tls_segment != NULL);
394
395 AArch64_address aligned_tcb_address =
396 align_address(Target_aarch64<size, big_endian>::TCB_SIZE,
397 tls_segment->maximum_alignment());
398
399 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
400 {
401 Static_reloc& reloc(this->static_relocs_[i]);
402 AArch64_address value;
403
404 if (!reloc.symbol_is_global())
405 {
406 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
407 const Symbol_value<size>* psymval =
408 reloc.relobj()->local_symbol(reloc.index());
409
410 // We are doing static linking. Issue an error and skip this
411 // relocation if the symbol is undefined or in a discarded_section.
412 bool is_ordinary;
413 unsigned int shndx = psymval->input_shndx(&is_ordinary);
414 if ((shndx == elfcpp::SHN_UNDEF)
415 || (is_ordinary
416 && shndx != elfcpp::SHN_UNDEF
417 && !object->is_section_included(shndx)
418 && !this->symbol_table_->is_section_folded(object, shndx)))
419 {
420 gold_error(_("undefined or discarded local symbol %u from "
421 " object %s in GOT"),
422 reloc.index(), reloc.relobj()->name().c_str());
423 continue;
424 }
425 value = psymval->value(object, 0);
426 }
427 else
428 {
429 const Symbol* gsym = reloc.symbol();
430 gold_assert(gsym != NULL);
431 if (gsym->is_forwarder())
432 gsym = this->symbol_table_->resolve_forwards(gsym);
433
434 // We are doing static linking. Issue an error and skip this
435 // relocation if the symbol is undefined or in a discarded_section
436 // unless it is a weakly_undefined symbol.
437 if ((gsym->is_defined_in_discarded_section()
438 || gsym->is_undefined())
439 && !gsym->is_weak_undefined())
440 {
441 gold_error(_("undefined or discarded symbol %s in GOT"),
442 gsym->name());
443 continue;
444 }
445
446 if (!gsym->is_weak_undefined())
447 {
448 const Sized_symbol<size>* sym =
449 static_cast<const Sized_symbol<size>*>(gsym);
450 value = sym->value();
451 }
452 else
453 value = 0;
454 }
455
456 unsigned got_offset = reloc.got_offset();
457 gold_assert(got_offset < oview_size);
458
459 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
460 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
461 Valtype x;
462 switch (reloc.r_type())
463 {
464 case elfcpp::R_AARCH64_TLS_DTPREL64:
465 x = value;
466 break;
467 case elfcpp::R_AARCH64_TLS_TPREL64:
468 x = value + aligned_tcb_address;
469 break;
470 default:
471 gold_unreachable();
472 }
473 elfcpp::Swap<size, big_endian>::writeval(wv, x);
474 }
475
476 of->write_output_view(offset, oview_size, oview);
477 }
478
479 private:
480 // Symbol table of the output object.
481 Symbol_table* symbol_table_;
482 // A pointer to the Layout class, so that we can find the .dynamic
483 // section when we write out the GOT section.
484 Layout* layout_;
485
486 // This class represent dynamic relocations that need to be applied by
487 // gold because we are using TLS relocations in a static link.
488 class Static_reloc
489 {
490 public:
491 Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
492 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
493 { this->u_.global.symbol = gsym; }
494
495 Static_reloc(unsigned int got_offset, unsigned int r_type,
496 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
497 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
498 {
499 this->u_.local.relobj = relobj;
500 this->u_.local.index = index;
501 }
502
503 // Return the GOT offset.
504 unsigned int
505 got_offset() const
506 { return this->got_offset_; }
507
508 // Relocation type.
509 unsigned int
510 r_type() const
511 { return this->r_type_; }
512
513 // Whether the symbol is global or not.
514 bool
515 symbol_is_global() const
516 { return this->symbol_is_global_; }
517
518 // For a relocation against a global symbol, the global symbol.
519 Symbol*
520 symbol() const
521 {
522 gold_assert(this->symbol_is_global_);
523 return this->u_.global.symbol;
524 }
525
526 // For a relocation against a local symbol, the defining object.
527 Sized_relobj_file<size, big_endian>*
528 relobj() const
529 {
530 gold_assert(!this->symbol_is_global_);
531 return this->u_.local.relobj;
532 }
533
534 // For a relocation against a local symbol, the local symbol index.
535 unsigned int
536 index() const
537 {
538 gold_assert(!this->symbol_is_global_);
539 return this->u_.local.index;
540 }
541
542 private:
543 // GOT offset of the entry to which this relocation is applied.
544 unsigned int got_offset_;
545 // Type of relocation.
546 unsigned int r_type_;
547 // Whether this relocation is against a global symbol.
548 bool symbol_is_global_;
549 // A global or local symbol.
550 union
551 {
552 struct
553 {
554 // For a global symbol, the symbol itself.
555 Symbol* symbol;
556 } global;
557 struct
558 {
559 // For a local symbol, the object defining the symbol.
560 Sized_relobj_file<size, big_endian>* relobj;
561 // For a local symbol, the symbol index.
562 unsigned int index;
563 } local;
564 } u_;
565 }; // End of inner class Static_reloc
566
567 std::vector<Static_reloc> static_relocs_;
568 }; // End of Output_data_got_aarch64
569
570
571 template<int size, bool big_endian>
572 class AArch64_input_section;
573
574
575 template<int size, bool big_endian>
576 class AArch64_output_section;
577
578
579 // Reloc stub class.
580
581 template<int size, bool big_endian>
582 class Reloc_stub
583 {
584 public:
585 typedef Reloc_stub<size, big_endian> This;
586 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
587
588 // Do not change the value of the enums, they are used to index into
589 // stub_insns array.
590 typedef enum
591 {
592 ST_NONE = 0,
593
594 // Using adrp/add pair, 4 insns (including alignment) without mem access,
595 // the fastest stub. This has a limited jump distance, which is tested by
596 // aarch64_valid_for_adrp_p.
597 ST_ADRP_BRANCH = 1,
598
599 // Using ldr-absolute-address/br-register, 4 insns with 1 mem access,
600 // unlimited in jump distance.
601 ST_LONG_BRANCH_ABS = 2,
602
603 // Using ldr/calculate-pcrel/jump, 8 insns (including alignment) with 1 mem
604 // access, slowest one. Only used in position independent executables.
605 ST_LONG_BRANCH_PCREL = 3,
606
607 } Stub_type;
608
609 // Branch range. This is used to calculate the section group size, as well as
610 // determine whether a stub is needed.
611 static const int MAX_BRANCH_OFFSET = ((1 << 25) - 1) << 2;
612 static const int MIN_BRANCH_OFFSET = -((1 << 25) << 2);
613
614 // Constant used to determine if an offset fits in the adrp instruction
615 // encoding.
616 static const int MAX_ADRP_IMM = (1 << 20) - 1;
617 static const int MIN_ADRP_IMM = -(1 << 20);
618
619 static const int BYTES_PER_INSN = 4;
620 static const int STUB_ADDR_ALIGN = 4;
621
622 // Determine whether the offset fits in the jump/branch instruction.
623 static bool
624 aarch64_valid_branch_offset_p(int64_t offset)
625 { return offset >= MIN_BRANCH_OFFSET && offset <= MAX_BRANCH_OFFSET; }
626
627 // Determine whether the offset fits in the adrp immediate field.
628 static bool
629 aarch64_valid_for_adrp_p(AArch64_address location, AArch64_address dest)
630 {
631 typedef AArch64_relocate_functions<size, big_endian> Reloc;
632 int64_t adrp_imm = (Reloc::Page(dest) - Reloc::Page(location)) >> 12;
633 return adrp_imm >= MIN_ADRP_IMM && adrp_imm <= MAX_ADRP_IMM;
634 }
635
636 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
637 // needed.
638 static Stub_type
639 stub_type_for_reloc(unsigned int r_type, AArch64_address address,
640 AArch64_address target);
641
642 Reloc_stub(Stub_type stub_type)
643 : stub_type_(stub_type), offset_(invalid_offset),
644 destination_address_(invalid_address)
645 { }
646
647 ~Reloc_stub()
648 { }
649
650 // Return offset of code stub from beginning of its containing stub table.
651 section_offset_type
652 offset() const
653 {
654 gold_assert(this->offset_ != invalid_offset);
655 return this->offset_;
656 }
657
658 // Set offset of code stub from beginning of its containing stub table.
659 void
660 set_offset(section_offset_type offset)
661 { this->offset_ = offset; }
662
663 // Return destination address.
664 AArch64_address
665 destination_address() const
666 {
667 gold_assert(this->destination_address_ != this->invalid_address);
668 return this->destination_address_;
669 }
670
671 // Set destination address.
672 void
673 set_destination_address(AArch64_address address)
674 {
675 gold_assert(address != this->invalid_address);
676 this->destination_address_ = address;
677 }
678
679 // Reset the destination address.
680 void
681 reset_destination_address()
682 { this->destination_address_ = this->invalid_address; }
683
684 // Return the stub type.
685 Stub_type
686 stub_type() const
687 { return stub_type_; }
688
689 // Return the stub size.
690 uint32_t
691 stub_size() const
692 { return this->stub_insn_number() * BYTES_PER_INSN; }
693
694 // Return the instruction number of this stub instance.
695 int
696 stub_insn_number() const
697 { return stub_insns_[this->stub_type_][0]; }
698
699 // Note the first "insn" is the number of total insns in this array.
700 const uint32_t*
701 stub_insns() const
702 { return stub_insns_[this->stub_type_]; }
703
704 // Write stub to output file.
705 void
706 write(unsigned char* view, section_size_type view_size)
707 { this->do_write(view, view_size); }
708
709 // The key class used to index the stub instance in the stub table's stub map.
710 class Key
711 {
712 public:
713 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
714 unsigned int r_sym, int32_t addend)
715 : stub_type_(stub_type), addend_(addend)
716 {
717 if (symbol != NULL)
718 {
719 this->r_sym_ = Reloc_stub::invalid_index;
720 this->u_.symbol = symbol;
721 }
722 else
723 {
724 gold_assert(relobj != NULL && r_sym != invalid_index);
725 this->r_sym_ = r_sym;
726 this->u_.relobj = relobj;
727 }
728 }
729
730 ~Key()
731 { }
732
733 // Return stub type.
734 Stub_type
735 stub_type() const
736 { return this->stub_type_; }
737
738 // Return the local symbol index or invalid_index.
739 unsigned int
740 r_sym() const
741 { return this->r_sym_; }
742
743 // Return the symbol if there is one.
744 const Symbol*
745 symbol() const
746 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
747
748 // Return the relobj if there is one.
749 const Relobj*
750 relobj() const
751 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
752
753 // Whether this equals to another key k.
754 bool
755 eq(const Key& k) const
756 {
757 return ((this->stub_type_ == k.stub_type_)
758 && (this->r_sym_ == k.r_sym_)
759 && ((this->r_sym_ != Reloc_stub::invalid_index)
760 ? (this->u_.relobj == k.u_.relobj)
761 : (this->u_.symbol == k.u_.symbol))
762 && (this->addend_ == k.addend_));
763 }
764
765 // Return a hash value.
766 size_t
767 hash_value() const
768 {
769 size_t name_hash_value = gold::string_hash<char>(
770 (this->r_sym_ != Reloc_stub::invalid_index)
771 ? this->u_.relobj->name().c_str()
772 : this->u_.symbol->name());
773 // We only have 4 stub types.
774 size_t stub_type_hash_value = 0x03 & this->stub_type_;
775 return (name_hash_value
776 ^ stub_type_hash_value
777 ^ ((this->r_sym_ & 0x3fff) << 2)
778 ^ ((this->addend_ & 0xffff) << 16));
779 }
780
781 // Functors for STL associative containers.
782 struct hash
783 {
784 size_t
785 operator()(const Key& k) const
786 { return k.hash_value(); }
787 };
788
789 struct equal_to
790 {
791 bool
792 operator()(const Key& k1, const Key& k2) const
793 { return k1.eq(k2); }
794 };
795
796 private:
797 // Stub type.
798 const Stub_type stub_type_;
799 // If this is a local symbol, this is the index in the defining object.
800 // Otherwise, it is invalid_index for a global symbol.
801 unsigned int r_sym_;
802 // If r_sym_ is an invalid index, this points to a global symbol.
803 // Otherwise, it points to a relobj. We used the unsized and target
804 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
805 // Arm_relobj, in order to avoid making the stub class a template
806 // as most of the stub machinery is endianness-neutral. However, it
807 // may require a bit of casting done by users of this class.
808 union
809 {
810 const Symbol* symbol;
811 const Relobj* relobj;
812 } u_;
813 // Addend associated with a reloc.
814 int32_t addend_;
815 }; // End of inner class Reloc_stub::Key
816
817 protected:
818 // This may be overridden in the child class.
819 virtual void
820 do_write(unsigned char*, section_size_type);
821
822 private:
823 static const section_offset_type invalid_offset =
824 static_cast<section_offset_type>(-1);
825 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
826 static const AArch64_address invalid_address =
827 static_cast<AArch64_address>(-1);
828
829 static const uint32_t stub_insns_[][10];
830
831 const Stub_type stub_type_;
832 section_offset_type offset_;
833 AArch64_address destination_address_;
834 }; // End of Reloc_stub
835
836
837 // Write data to output file.
838
839 template<int size, bool big_endian>
840 void
841 Reloc_stub<size, big_endian>::
842 do_write(unsigned char* view, section_size_type)
843 {
844 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
845 const uint32_t* insns = this->stub_insns();
846 uint32_t num_insns = this->stub_insn_number();
847 Insntype* ip = reinterpret_cast<Insntype*>(view);
848 for (uint32_t i = 1; i <= num_insns; ++i)
849 elfcpp::Swap<32, big_endian>::writeval(ip + i - 1, insns[i]);
850 }
851
852
853 // Stubs instructions definition.
854
855 template<int size, bool big_endian>
856 const uint32_t
857 Reloc_stub<size, big_endian>::stub_insns_[][10] =
858 {
859 // The first element of each group is the num of the insns.
860
861 // ST_NONE
862 {0, 0},
863
864 // ST_ADRP_BRANCH
865 {
866 4,
867 0x90000010, /* adrp ip0, X */
868 /* ADR_PREL_PG_HI21(X) */
869 0x91000210, /* add ip0, ip0, :lo12:X */
870 /* ADD_ABS_LO12_NC(X) */
871 0xd61f0200, /* br ip0 */
872 0x00000000, /* alignment padding */
873 },
874
875 // ST_LONG_BRANCH_ABS
876 {
877 4,
878 0x58000050, /* ldr ip0, 0x8 */
879 0xd61f0200, /* br ip0 */
880 0x00000000, /* address field */
881 0x00000000, /* address fields */
882 },
883
884 // ST_LONG_BRANCH_PCREL
885 {
886 8,
887 0x58000090, /* ldr ip0, 0x10 */
888 0x10000011, /* adr ip1, #0 */
889 0x8b110210, /* add ip0, ip0, ip1 */
890 0xd61f0200, /* br ip0 */
891 0x00000000, /* address field */
892 0x00000000, /* address field */
893 0x00000000, /* alignment padding */
894 0x00000000, /* alignment padding */
895 }
896 };
897
898
899 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
900 // needed.
901
902 template<int size, bool big_endian>
903 inline
904 typename Reloc_stub<size, big_endian>::Stub_type
905 Reloc_stub<size, big_endian>::stub_type_for_reloc(
906 unsigned int r_type, AArch64_address location, AArch64_address dest)
907 {
908 int64_t branch_offset = 0;
909 switch(r_type)
910 {
911 case elfcpp::R_AARCH64_CALL26:
912 case elfcpp::R_AARCH64_JUMP26:
913 branch_offset = dest - location;
914 break;
915 default:
916 gold_unreachable();
917 }
918
919 if (aarch64_valid_branch_offset_p(branch_offset))
920 return ST_NONE;
921
922 if (aarch64_valid_for_adrp_p(location, dest))
923 return ST_ADRP_BRANCH;
924
925 if (parameters->options().output_is_position_independent()
926 && parameters->options().output_is_executable())
927 return ST_LONG_BRANCH_PCREL;
928
929 return ST_LONG_BRANCH_ABS;
930 }
931
932 // A class to hold stubs for the ARM target.
933
934 template<int size, bool big_endian>
935 class Stub_table : public Output_data
936 {
937 public:
938 typedef Target_aarch64<size, big_endian> The_target_aarch64;
939 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
940 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
941 typedef Reloc_stub<size, big_endian> The_reloc_stub;
942 typedef typename The_reloc_stub::Key The_reloc_stub_key;
943 typedef typename The_reloc_stub_key::hash The_reloc_stub_key_hash;
944 typedef typename The_reloc_stub_key::equal_to The_reloc_stub_key_equal_to;
945 typedef Stub_table<size, big_endian> The_stub_table;
946 typedef Unordered_map<The_reloc_stub_key, The_reloc_stub*,
947 The_reloc_stub_key_hash, The_reloc_stub_key_equal_to>
948 Reloc_stub_map;
949 typedef typename Reloc_stub_map::const_iterator Reloc_stub_map_const_iter;
950 typedef Relocate_info<size, big_endian> The_relocate_info;
951
952 Stub_table(The_aarch64_input_section* owner)
953 : Output_data(), owner_(owner), reloc_stubs_size_(0), prev_data_size_(0)
954 { }
955
956 ~Stub_table()
957 { }
958
959 The_aarch64_input_section*
960 owner() const
961 { return owner_; }
962
963 // Whether this stub table is empty.
964 bool
965 empty() const
966 { return reloc_stubs_.empty(); }
967
968 // Return the current data size.
969 off_t
970 current_data_size() const
971 { return this->current_data_size_for_child(); }
972
973 // Add a STUB using KEY. The caller is responsible for avoiding addition
974 // if a STUB with the same key has already been added.
975 void
976 add_reloc_stub(The_reloc_stub* stub, const The_reloc_stub_key& key);
977
978 // Finalize stubs. No-op here, just for completeness.
979 void
980 finalize_stubs()
981 { }
982
983 // Look up a relocation stub using KEY. Return NULL if there is none.
984 The_reloc_stub*
985 find_reloc_stub(The_reloc_stub_key& key)
986 {
987 Reloc_stub_map_const_iter p = this->reloc_stubs_.find(key);
988 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
989 }
990
991 // Relocate stubs in this stub table.
992 void
993 relocate_stubs(const The_relocate_info*,
994 The_target_aarch64*,
995 Output_section*,
996 unsigned char*,
997 AArch64_address,
998 section_size_type);
999
1000 // Update data size at the end of a relaxation pass. Return true if data size
1001 // is different from that of the previous relaxation pass.
1002 bool
1003 update_data_size_changed_p()
1004 {
1005 // No addralign changed here.
1006 off_t s = this->reloc_stubs_size_;
1007 bool changed = (s != this->prev_data_size_);
1008 this->prev_data_size_ = s;
1009 return changed;
1010 }
1011
1012 protected:
1013 // Write out section contents.
1014 void
1015 do_write(Output_file*);
1016
1017 // Return the required alignment.
1018 uint64_t
1019 do_addralign() const
1020 { return The_reloc_stub::STUB_ADDR_ALIGN; }
1021
1022 // Reset address and file offset.
1023 void
1024 do_reset_address_and_file_offset()
1025 { this->set_current_data_size_for_child(this->prev_data_size_); }
1026
1027 // Set final data size.
1028 void
1029 set_final_data_size()
1030 { this->set_data_size(this->current_data_size()); }
1031
1032 private:
1033 // Relocate one stub.
1034 void
1035 relocate_stub(The_reloc_stub*,
1036 const The_relocate_info*,
1037 The_target_aarch64*,
1038 Output_section*,
1039 unsigned char*,
1040 AArch64_address,
1041 section_size_type);
1042
1043 private:
1044 // Owner of this stub table.
1045 The_aarch64_input_section* owner_;
1046 // The relocation stubs.
1047 Reloc_stub_map reloc_stubs_;
1048 // Size of reloc stubs.
1049 off_t reloc_stubs_size_;
1050 // data size of this in the previous pass.
1051 off_t prev_data_size_;
1052 }; // End of Stub_table
1053
1054
1055 // Add a STUB using KEY. The caller is responsible for avoiding addition
1056 // if a STUB with the same key has already been added.
1057
1058 template<int size, bool big_endian>
1059 void
1060 Stub_table<size, big_endian>::add_reloc_stub(
1061 The_reloc_stub* stub, const The_reloc_stub_key& key)
1062 {
1063 gold_assert(stub->stub_type() == key.stub_type());
1064 this->reloc_stubs_[key] = stub;
1065
1066 // Assign stub offset early. We can do this because we never remove
1067 // reloc stubs and they are in the beginning of the stub table.
1068 this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_,
1069 The_reloc_stub::STUB_ADDR_ALIGN);
1070 stub->set_offset(this->reloc_stubs_size_);
1071 this->reloc_stubs_size_ += stub->stub_size();
1072 }
1073
1074
1075 // Relocate all stubs in this stub table.
1076
1077 template<int size, bool big_endian>
1078 void
1079 Stub_table<size, big_endian>::
1080 relocate_stubs(const The_relocate_info* relinfo,
1081 The_target_aarch64* target_aarch64,
1082 Output_section* output_section,
1083 unsigned char* view,
1084 AArch64_address address,
1085 section_size_type view_size)
1086 {
1087 // "view_size" is the total size of the stub_table.
1088 gold_assert(address == this->address() &&
1089 view_size == static_cast<section_size_type>(this->data_size()));
1090 for(Reloc_stub_map_const_iter p = this->reloc_stubs_.begin();
1091 p != this->reloc_stubs_.end(); ++p)
1092 relocate_stub(p->second, relinfo, target_aarch64, output_section,
1093 view, address, view_size);
1094 }
1095
1096
1097 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
1098
1099 template<int size, bool big_endian>
1100 void
1101 Stub_table<size, big_endian>::
1102 relocate_stub(The_reloc_stub* stub,
1103 const The_relocate_info* relinfo,
1104 The_target_aarch64* target_aarch64,
1105 Output_section* output_section,
1106 unsigned char* view,
1107 AArch64_address address,
1108 section_size_type view_size)
1109 {
1110 // "offset" is the offset from the beginning of the stub_table.
1111 section_size_type offset = stub->offset();
1112 section_size_type stub_size = stub->stub_size();
1113 // "view_size" is the total size of the stub_table.
1114 gold_assert(offset + stub_size <= view_size);
1115
1116 target_aarch64->relocate_stub(stub, relinfo, output_section,
1117 view + offset, address + offset, view_size);
1118 }
1119
1120
1121 // Write out the stubs to file.
1122
1123 template<int size, bool big_endian>
1124 void
1125 Stub_table<size, big_endian>::do_write(Output_file* of)
1126 {
1127 off_t offset = this->offset();
1128 const section_size_type oview_size =
1129 convert_to_section_size_type(this->data_size());
1130 unsigned char* const oview = of->get_output_view(offset, oview_size);
1131
1132 // Write relocation stubs.
1133 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
1134 p != this->reloc_stubs_.end(); ++p)
1135 {
1136 The_reloc_stub* stub = p->second;
1137 AArch64_address address = this->address() + stub->offset();
1138 gold_assert(address ==
1139 align_address(address, The_reloc_stub::STUB_ADDR_ALIGN));
1140 stub->write(oview + stub->offset(), stub->stub_size());
1141 }
1142
1143 of->write_output_view(this->offset(), oview_size, oview);
1144 }
1145
1146
1147 // AArch64_relobj class.
1148
1149 template<int size, bool big_endian>
1150 class AArch64_relobj : public Sized_relobj_file<size, big_endian>
1151 {
1152 public:
1153 typedef AArch64_relobj<size, big_endian> This;
1154 typedef Target_aarch64<size, big_endian> The_target_aarch64;
1155 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1156 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
1157 typedef Stub_table<size, big_endian> The_stub_table;
1158 typedef std::vector<The_stub_table*> Stub_table_list;
1159 static const AArch64_address invalid_address =
1160 static_cast<AArch64_address>(-1);
1161
1162 AArch64_relobj(const std::string& name, Input_file* input_file, off_t offset,
1163 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1164 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1165 stub_tables_()
1166 { }
1167
1168 ~AArch64_relobj()
1169 { }
1170
1171 // Return the stub table of the SHNDX-th section if there is one.
1172 The_stub_table*
1173 stub_table(unsigned int shndx) const
1174 {
1175 gold_assert(shndx < this->stub_tables_.size());
1176 return this->stub_tables_[shndx];
1177 }
1178
1179 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1180 void
1181 set_stub_table(unsigned int shndx, The_stub_table* stub_table)
1182 {
1183 gold_assert(shndx < this->stub_tables_.size());
1184 this->stub_tables_[shndx] = stub_table;
1185 }
1186
1187 // Entrance to erratum_843419 scanning.
1188 void
1189 scan_erratum_843419(unsigned int shndx,
1190 const elfcpp::Shdr<size, big_endian>&,
1191 Output_section*, const Symbol_table*,
1192 The_target_aarch64*);
1193
1194 // Scan all relocation sections for stub generation.
1195 void
1196 scan_sections_for_stubs(The_target_aarch64*, const Symbol_table*,
1197 const Layout*);
1198
1199 // Whether a section is a scannable text section.
1200 bool
1201 text_section_is_scannable(const elfcpp::Shdr<size, big_endian>&, unsigned int,
1202 const Output_section*, const Symbol_table*);
1203
1204 // Convert regular input section with index SHNDX to a relaxed section.
1205 void
1206 convert_input_section_to_relaxed_section(unsigned /* shndx */)
1207 {
1208 // The stubs have relocations and we need to process them after writing
1209 // out the stubs. So relocation now must follow section write.
1210 this->set_relocs_must_follow_section_writes();
1211 }
1212
1213 // Structure for mapping symbol position.
1214 struct Mapping_symbol_position
1215 {
1216 Mapping_symbol_position(unsigned int shndx, AArch64_address offset):
1217 shndx_(shndx), offset_(offset)
1218 {}
1219
1220 // "<" comparator used in ordered_map container.
1221 bool
1222 operator<(const Mapping_symbol_position& p) const
1223 {
1224 return (this->shndx_ < p.shndx_
1225 || (this->shndx_ == p.shndx_ && this->offset_ < p.offset_));
1226 }
1227
1228 // Section index.
1229 unsigned int shndx_;
1230
1231 // Section offset.
1232 AArch64_address offset_;
1233 };
1234
1235 typedef std::map<Mapping_symbol_position, char> Mapping_symbol_info;
1236
1237 protected:
1238 // Post constructor setup.
1239 void
1240 do_setup()
1241 {
1242 // Call parent's setup method.
1243 Sized_relobj_file<size, big_endian>::do_setup();
1244
1245 // Initialize look-up tables.
1246 this->stub_tables_.resize(this->shnum());
1247 }
1248
1249 virtual void
1250 do_relocate_sections(
1251 const Symbol_table* symtab, const Layout* layout,
1252 const unsigned char* pshdrs, Output_file* of,
1253 typename Sized_relobj_file<size, big_endian>::Views* pviews);
1254
1255 // Count local symbols and (optionally) record mapping info.
1256 virtual void
1257 do_count_local_symbols(Stringpool_template<char>*,
1258 Stringpool_template<char>*);
1259
1260 private:
1261 // Whether a section needs to be scanned for relocation stubs.
1262 bool
1263 section_needs_reloc_stub_scanning(const elfcpp::Shdr<size, big_endian>&,
1264 const Relobj::Output_sections&,
1265 const Symbol_table*, const unsigned char*);
1266
1267 // List of stub tables.
1268 Stub_table_list stub_tables_;
1269
1270 // Mapping symbol information sorted by (section index, section_offset).
1271 Mapping_symbol_info mapping_symbol_info_;
1272 }; // End of AArch64_relobj
1273
1274
1275 // Override to record mapping symbol information.
1276 template<int size, bool big_endian>
1277 void
1278 AArch64_relobj<size, big_endian>::do_count_local_symbols(
1279 Stringpool_template<char>* pool, Stringpool_template<char>* dynpool)
1280 {
1281 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
1282
1283 // Only erratum-fixing work needs mapping symbols, so skip this time consuming
1284 // processing if not fixing erratum.
1285 if (!parameters->options().fix_cortex_a53_843419())
1286 return;
1287
1288 const unsigned int loccount = this->local_symbol_count();
1289 if (loccount == 0)
1290 return;
1291
1292 // Read the symbol table section header.
1293 const unsigned int symtab_shndx = this->symtab_shndx();
1294 elfcpp::Shdr<size, big_endian>
1295 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
1296 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1297
1298 // Read the local symbols.
1299 const int sym_size =elfcpp::Elf_sizes<size>::sym_size;
1300 gold_assert(loccount == symtabshdr.get_sh_info());
1301 off_t locsize = loccount * sym_size;
1302 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1303 locsize, true, true);
1304
1305 // For mapping symbol processing, we need to read the symbol names.
1306 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
1307 if (strtab_shndx >= this->shnum())
1308 {
1309 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
1310 return;
1311 }
1312
1313 elfcpp::Shdr<size, big_endian>
1314 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
1315 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
1316 {
1317 this->error(_("symbol table name section has wrong type: %u"),
1318 static_cast<unsigned int>(strtabshdr.get_sh_type()));
1319 return;
1320 }
1321
1322 const char* pnames =
1323 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
1324 strtabshdr.get_sh_size(),
1325 false, false));
1326
1327 // Skip the first dummy symbol.
1328 psyms += sym_size;
1329 typename Sized_relobj_file<size, big_endian>::Local_values*
1330 plocal_values = this->local_values();
1331 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1332 {
1333 elfcpp::Sym<size, big_endian> sym(psyms);
1334 Symbol_value<size>& lv((*plocal_values)[i]);
1335 AArch64_address input_value = lv.input_value();
1336
1337 // Check to see if this is a mapping symbol.
1338 const char* sym_name = pnames + sym.get_st_name();
1339 if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd')
1340 && sym_name[2] == '\0')
1341 {
1342 bool is_ordinary;
1343 unsigned int input_shndx =
1344 this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
1345 gold_assert(is_ordinary);
1346
1347 Mapping_symbol_position msp(input_shndx, input_value);
1348 // Insert mapping_symbol_info into map whose ordering is defined by
1349 // (shndx, offset_within_section).
1350 this->mapping_symbol_info_[msp] = sym_name[1];
1351 }
1352 }
1353 }
1354
1355
1356 // Relocate sections.
1357
1358 template<int size, bool big_endian>
1359 void
1360 AArch64_relobj<size, big_endian>::do_relocate_sections(
1361 const Symbol_table* symtab, const Layout* layout,
1362 const unsigned char* pshdrs, Output_file* of,
1363 typename Sized_relobj_file<size, big_endian>::Views* pviews)
1364 {
1365 // Call parent to relocate sections.
1366 Sized_relobj_file<size, big_endian>::do_relocate_sections(symtab, layout,
1367 pshdrs, of, pviews);
1368
1369 // We do not generate stubs if doing a relocatable link.
1370 if (parameters->options().relocatable())
1371 return;
1372
1373 Relocate_info<size, big_endian> relinfo;
1374 relinfo.symtab = symtab;
1375 relinfo.layout = layout;
1376 relinfo.object = this;
1377
1378 // Relocate stub tables.
1379 unsigned int shnum = this->shnum();
1380 The_target_aarch64* target = The_target_aarch64::current_target();
1381
1382 for (unsigned int i = 1; i < shnum; ++i)
1383 {
1384 The_aarch64_input_section* aarch64_input_section =
1385 target->find_aarch64_input_section(this, i);
1386 if (aarch64_input_section != NULL
1387 && aarch64_input_section->is_stub_table_owner()
1388 && !aarch64_input_section->stub_table()->empty())
1389 {
1390 Output_section* os = this->output_section(i);
1391 gold_assert(os != NULL);
1392
1393 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
1394 relinfo.reloc_shdr = NULL;
1395 relinfo.data_shndx = i;
1396 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<size>::shdr_size;
1397
1398 typename Sized_relobj_file<size, big_endian>::View_size&
1399 view_struct = (*pviews)[i];
1400 gold_assert(view_struct.view != NULL);
1401
1402 The_stub_table* stub_table = aarch64_input_section->stub_table();
1403 off_t offset = stub_table->address() - view_struct.address;
1404 unsigned char* view = view_struct.view + offset;
1405 AArch64_address address = stub_table->address();
1406 section_size_type view_size = stub_table->data_size();
1407 stub_table->relocate_stubs(&relinfo, target, os, view, address,
1408 view_size);
1409 }
1410 }
1411 }
1412
1413
1414 // Determine if an input section is scannable for stub processing. SHDR is
1415 // the header of the section and SHNDX is the section index. OS is the output
1416 // section for the input section and SYMTAB is the global symbol table used to
1417 // look up ICF information.
1418
1419 template<int size, bool big_endian>
1420 bool
1421 AArch64_relobj<size, big_endian>::text_section_is_scannable(
1422 const elfcpp::Shdr<size, big_endian>& text_shdr,
1423 unsigned int text_shndx,
1424 const Output_section* os,
1425 const Symbol_table* symtab)
1426 {
1427 // Skip any empty sections, unallocated sections or sections whose
1428 // type are not SHT_PROGBITS.
1429 if (text_shdr.get_sh_size() == 0
1430 || (text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
1431 || text_shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
1432 return false;
1433
1434 // Skip any discarded or ICF'ed sections.
1435 if (os == NULL || symtab->is_section_folded(this, text_shndx))
1436 return false;
1437
1438 // Skip exception frame.
1439 if (strcmp(os->name(), ".eh_frame") == 0)
1440 return false ;
1441
1442 gold_assert(!this->is_output_section_offset_invalid(text_shndx) ||
1443 os->find_relaxed_input_section(this, text_shndx) != NULL);
1444
1445 return true;
1446 }
1447
1448
1449 // Determine if we want to scan the SHNDX-th section for relocation stubs.
1450 // This is a helper for AArch64_relobj::scan_sections_for_stubs().
1451
1452 template<int size, bool big_endian>
1453 bool
1454 AArch64_relobj<size, big_endian>::section_needs_reloc_stub_scanning(
1455 const elfcpp::Shdr<size, big_endian>& shdr,
1456 const Relobj::Output_sections& out_sections,
1457 const Symbol_table* symtab,
1458 const unsigned char* pshdrs)
1459 {
1460 unsigned int sh_type = shdr.get_sh_type();
1461 if (sh_type != elfcpp::SHT_RELA)
1462 return false;
1463
1464 // Ignore empty section.
1465 off_t sh_size = shdr.get_sh_size();
1466 if (sh_size == 0)
1467 return false;
1468
1469 // Ignore reloc section with unexpected symbol table. The
1470 // error will be reported in the final link.
1471 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
1472 return false;
1473
1474 gold_assert(sh_type == elfcpp::SHT_RELA);
1475 unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
1476
1477 // Ignore reloc section with unexpected entsize or uneven size.
1478 // The error will be reported in the final link.
1479 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
1480 return false;
1481
1482 // Ignore reloc section with bad info. This error will be
1483 // reported in the final link.
1484 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_info());
1485 if (text_shndx >= this->shnum())
1486 return false;
1487
1488 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1489 const elfcpp::Shdr<size, big_endian> text_shdr(pshdrs +
1490 text_shndx * shdr_size);
1491 return this->text_section_is_scannable(text_shdr, text_shndx,
1492 out_sections[text_shndx], symtab);
1493 }
1494
1495
1496 // Scan section SHNDX for erratum 843419.
1497
1498 template<int size, bool big_endian>
1499 void
1500 AArch64_relobj<size, big_endian>::scan_erratum_843419(
1501 unsigned int shndx, const elfcpp::Shdr<size, big_endian>& shdr,
1502 Output_section* os, const Symbol_table* symtab,
1503 The_target_aarch64* target)
1504 {
1505 if (shdr.get_sh_size() == 0
1506 || (shdr.get_sh_flags() &
1507 (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR)) == 0
1508 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
1509 return;
1510
1511 if (!os || symtab->is_section_folded(this, shndx)) return;
1512
1513 AArch64_address output_offset = this->get_output_section_offset(shndx);
1514 AArch64_address output_address;
1515 if (output_offset != invalid_address)
1516 output_address = os->address() + output_offset;
1517 else
1518 {
1519 const Output_relaxed_input_section* poris =
1520 os->find_relaxed_input_section(this, shndx);
1521 if (!poris) return;
1522 output_address = poris->address();
1523 }
1524
1525 section_size_type input_view_size = 0;
1526 const unsigned char* input_view =
1527 this->section_contents(shndx, &input_view_size, false);
1528
1529 Mapping_symbol_position section_start(shndx, 0);
1530 // Find the first mapping symbol record within section shndx.
1531 typename Mapping_symbol_info::const_iterator p =
1532 this->mapping_symbol_info_.lower_bound(section_start);
1533 if (p == this->mapping_symbol_info_.end() || p->first.shndx_ != shndx)
1534 gold_warning(_("cannot scan executable section %u of %s for Cortex-A53 "
1535 "erratum because it has no mapping symbols."),
1536 shndx, this->name().c_str());
1537 while (p != this->mapping_symbol_info_.end() &&
1538 p->first.shndx_ == shndx)
1539 {
1540 typename Mapping_symbol_info::const_iterator prev = p;
1541 ++p;
1542 if (prev->second == 'x')
1543 {
1544 section_size_type span_start =
1545 convert_to_section_size_type(prev->first.offset_);
1546 section_size_type span_end;
1547 if (p != this->mapping_symbol_info_.end()
1548 && p->first.shndx_ == shndx)
1549 span_end = convert_to_section_size_type(p->first.offset_);
1550 else
1551 span_end = convert_to_section_size_type(shdr.get_sh_size());
1552 target->scan_erratum_843419_span(
1553 this, shndx, span_start, span_end,
1554 const_cast<unsigned char*>(input_view), output_address);
1555 }
1556 }
1557 }
1558
1559
1560 // Scan relocations for stub generation.
1561
1562 template<int size, bool big_endian>
1563 void
1564 AArch64_relobj<size, big_endian>::scan_sections_for_stubs(
1565 The_target_aarch64* target,
1566 const Symbol_table* symtab,
1567 const Layout* layout)
1568 {
1569 unsigned int shnum = this->shnum();
1570 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1571
1572 // Read the section headers.
1573 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
1574 shnum * shdr_size,
1575 true, true);
1576
1577 // To speed up processing, we set up hash tables for fast lookup of
1578 // input offsets to output addresses.
1579 this->initialize_input_to_output_maps();
1580
1581 const Relobj::Output_sections& out_sections(this->output_sections());
1582
1583 Relocate_info<size, big_endian> relinfo;
1584 relinfo.symtab = symtab;
1585 relinfo.layout = layout;
1586 relinfo.object = this;
1587
1588 // Do relocation stubs scanning.
1589 const unsigned char* p = pshdrs + shdr_size;
1590 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
1591 {
1592 const elfcpp::Shdr<size, big_endian> shdr(p);
1593 if (parameters->options().fix_cortex_a53_843419())
1594 scan_erratum_843419(i, shdr, out_sections[i], symtab, target);
1595 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
1596 pshdrs))
1597 {
1598 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
1599 AArch64_address output_offset =
1600 this->get_output_section_offset(index);
1601 AArch64_address output_address;
1602 if (output_offset != invalid_address)
1603 {
1604 output_address = out_sections[index]->address() + output_offset;
1605 }
1606 else
1607 {
1608 // Currently this only happens for a relaxed section.
1609 const Output_relaxed_input_section* poris =
1610 out_sections[index]->find_relaxed_input_section(this, index);
1611 gold_assert(poris != NULL);
1612 output_address = poris->address();
1613 }
1614
1615 // Get the relocations.
1616 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
1617 shdr.get_sh_size(),
1618 true, false);
1619
1620 // Get the section contents.
1621 section_size_type input_view_size = 0;
1622 const unsigned char* input_view =
1623 this->section_contents(index, &input_view_size, false);
1624
1625 relinfo.reloc_shndx = i;
1626 relinfo.data_shndx = index;
1627 unsigned int sh_type = shdr.get_sh_type();
1628 unsigned int reloc_size;
1629 gold_assert (sh_type == elfcpp::SHT_RELA);
1630 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
1631
1632 Output_section* os = out_sections[index];
1633 target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
1634 shdr.get_sh_size() / reloc_size,
1635 os,
1636 output_offset == invalid_address,
1637 input_view, output_address,
1638 input_view_size);
1639 }
1640 }
1641 }
1642
1643
1644 // A class to wrap an ordinary input section containing executable code.
1645
1646 template<int size, bool big_endian>
1647 class AArch64_input_section : public Output_relaxed_input_section
1648 {
1649 public:
1650 typedef Stub_table<size, big_endian> The_stub_table;
1651
1652 AArch64_input_section(Relobj* relobj, unsigned int shndx)
1653 : Output_relaxed_input_section(relobj, shndx, 1),
1654 stub_table_(NULL),
1655 original_contents_(NULL), original_size_(0),
1656 original_addralign_(1)
1657 { }
1658
1659 ~AArch64_input_section()
1660 { delete[] this->original_contents_; }
1661
1662 // Initialize.
1663 void
1664 init();
1665
1666 // Set the stub_table.
1667 void
1668 set_stub_table(The_stub_table* st)
1669 { this->stub_table_ = st; }
1670
1671 // Whether this is a stub table owner.
1672 bool
1673 is_stub_table_owner() const
1674 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1675
1676 // Return the original size of the section.
1677 uint32_t
1678 original_size() const
1679 { return this->original_size_; }
1680
1681 // Return the stub table.
1682 The_stub_table*
1683 stub_table()
1684 { return stub_table_; }
1685
1686 protected:
1687 // Write out this input section.
1688 void
1689 do_write(Output_file*);
1690
1691 // Return required alignment of this.
1692 uint64_t
1693 do_addralign() const
1694 {
1695 if (this->is_stub_table_owner())
1696 return std::max(this->stub_table_->addralign(),
1697 static_cast<uint64_t>(this->original_addralign_));
1698 else
1699 return this->original_addralign_;
1700 }
1701
1702 // Finalize data size.
1703 void
1704 set_final_data_size();
1705
1706 // Reset address and file offset.
1707 void
1708 do_reset_address_and_file_offset();
1709
1710 // Output offset.
1711 bool
1712 do_output_offset(const Relobj* object, unsigned int shndx,
1713 section_offset_type offset,
1714 section_offset_type* poutput) const
1715 {
1716 if ((object == this->relobj())
1717 && (shndx == this->shndx())
1718 && (offset >= 0)
1719 && (offset <=
1720 convert_types<section_offset_type, uint32_t>(this->original_size_)))
1721 {
1722 *poutput = offset;
1723 return true;
1724 }
1725 else
1726 return false;
1727 }
1728
1729 private:
1730 // Copying is not allowed.
1731 AArch64_input_section(const AArch64_input_section&);
1732 AArch64_input_section& operator=(const AArch64_input_section&);
1733
1734 // The relocation stubs.
1735 The_stub_table* stub_table_;
1736 // Original section contents. We have to make a copy here since the file
1737 // containing the original section may not be locked when we need to access
1738 // the contents.
1739 unsigned char* original_contents_;
1740 // Section size of the original input section.
1741 uint32_t original_size_;
1742 // Address alignment of the original input section.
1743 uint32_t original_addralign_;
1744 }; // End of AArch64_input_section
1745
1746
1747 // Finalize data size.
1748
1749 template<int size, bool big_endian>
1750 void
1751 AArch64_input_section<size, big_endian>::set_final_data_size()
1752 {
1753 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
1754
1755 if (this->is_stub_table_owner())
1756 {
1757 this->stub_table_->finalize_data_size();
1758 off = align_address(off, this->stub_table_->addralign());
1759 off += this->stub_table_->data_size();
1760 }
1761 this->set_data_size(off);
1762 }
1763
1764
1765 // Reset address and file offset.
1766
1767 template<int size, bool big_endian>
1768 void
1769 AArch64_input_section<size, big_endian>::do_reset_address_and_file_offset()
1770 {
1771 // Size of the original input section contents.
1772 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
1773
1774 // If this is a stub table owner, account for the stub table size.
1775 if (this->is_stub_table_owner())
1776 {
1777 The_stub_table* stub_table = this->stub_table_;
1778
1779 // Reset the stub table's address and file offset. The
1780 // current data size for child will be updated after that.
1781 stub_table_->reset_address_and_file_offset();
1782 off = align_address(off, stub_table_->addralign());
1783 off += stub_table->current_data_size();
1784 }
1785
1786 this->set_current_data_size(off);
1787 }
1788
1789
1790 // Initialize an Arm_input_section.
1791
1792 template<int size, bool big_endian>
1793 void
1794 AArch64_input_section<size, big_endian>::init()
1795 {
1796 Relobj* relobj = this->relobj();
1797 unsigned int shndx = this->shndx();
1798
1799 // We have to cache original size, alignment and contents to avoid locking
1800 // the original file.
1801 this->original_addralign_ =
1802 convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
1803
1804 // This is not efficient but we expect only a small number of relaxed
1805 // input sections for stubs.
1806 section_size_type section_size;
1807 const unsigned char* section_contents =
1808 relobj->section_contents(shndx, &section_size, false);
1809 this->original_size_ =
1810 convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
1811
1812 gold_assert(this->original_contents_ == NULL);
1813 this->original_contents_ = new unsigned char[section_size];
1814 memcpy(this->original_contents_, section_contents, section_size);
1815
1816 // We want to make this look like the original input section after
1817 // output sections are finalized.
1818 Output_section* os = relobj->output_section(shndx);
1819 off_t offset = relobj->output_section_offset(shndx);
1820 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
1821 this->set_address(os->address() + offset);
1822 this->set_file_offset(os->offset() + offset);
1823 this->set_current_data_size(this->original_size_);
1824 this->finalize_data_size();
1825 }
1826
1827
1828 // Write data to output file.
1829
1830 template<int size, bool big_endian>
1831 void
1832 AArch64_input_section<size, big_endian>::do_write(Output_file* of)
1833 {
1834 // We have to write out the original section content.
1835 gold_assert(this->original_contents_ != NULL);
1836 of->write(this->offset(), this->original_contents_,
1837 this->original_size_);
1838
1839 // If this owns a stub table and it is not empty, write it.
1840 if (this->is_stub_table_owner() && !this->stub_table_->empty())
1841 this->stub_table_->write(of);
1842 }
1843
1844
1845 // Arm output section class. This is defined mainly to add a number of stub
1846 // generation methods.
1847
1848 template<int size, bool big_endian>
1849 class AArch64_output_section : public Output_section
1850 {
1851 public:
1852 typedef Target_aarch64<size, big_endian> The_target_aarch64;
1853 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
1854 typedef Stub_table<size, big_endian> The_stub_table;
1855 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1856
1857 public:
1858 AArch64_output_section(const char* name, elfcpp::Elf_Word type,
1859 elfcpp::Elf_Xword flags)
1860 : Output_section(name, type, flags)
1861 { }
1862
1863 ~AArch64_output_section() {}
1864
1865 // Group input sections for stub generation.
1866 void
1867 group_sections(section_size_type, bool, Target_aarch64<size, big_endian>*,
1868 const Task*);
1869
1870 private:
1871 typedef Output_section::Input_section Input_section;
1872 typedef Output_section::Input_section_list Input_section_list;
1873
1874 // Create a stub group.
1875 void
1876 create_stub_group(Input_section_list::const_iterator,
1877 Input_section_list::const_iterator,
1878 Input_section_list::const_iterator,
1879 The_target_aarch64*,
1880 std::vector<Output_relaxed_input_section*>&,
1881 const Task*);
1882 }; // End of AArch64_output_section
1883
1884
1885 // Create a stub group for input sections from FIRST to LAST. OWNER points to
1886 // the input section that will be the owner of the stub table.
1887
1888 template<int size, bool big_endian> void
1889 AArch64_output_section<size, big_endian>::create_stub_group(
1890 Input_section_list::const_iterator first,
1891 Input_section_list::const_iterator last,
1892 Input_section_list::const_iterator owner,
1893 The_target_aarch64* target,
1894 std::vector<Output_relaxed_input_section*>& new_relaxed_sections,
1895 const Task* task)
1896 {
1897 // Currently we convert ordinary input sections into relaxed sections only
1898 // at this point.
1899 The_aarch64_input_section* input_section;
1900 if (owner->is_relaxed_input_section())
1901 gold_unreachable();
1902 else
1903 {
1904 gold_assert(owner->is_input_section());
1905 // Create a new relaxed input section. We need to lock the original
1906 // file.
1907 Task_lock_obj<Object> tl(task, owner->relobj());
1908 input_section =
1909 target->new_aarch64_input_section(owner->relobj(), owner->shndx());
1910 new_relaxed_sections.push_back(input_section);
1911 }
1912
1913 // Create a stub table.
1914 The_stub_table* stub_table =
1915 target->new_stub_table(input_section);
1916
1917 input_section->set_stub_table(stub_table);
1918
1919 Input_section_list::const_iterator p = first;
1920 // Look for input sections or relaxed input sections in [first ... last].
1921 do
1922 {
1923 if (p->is_input_section() || p->is_relaxed_input_section())
1924 {
1925 // The stub table information for input sections live
1926 // in their objects.
1927 The_aarch64_relobj* aarch64_relobj =
1928 static_cast<The_aarch64_relobj*>(p->relobj());
1929 aarch64_relobj->set_stub_table(p->shndx(), stub_table);
1930 }
1931 }
1932 while (p++ != last);
1933 }
1934
1935
1936 // Group input sections for stub generation. GROUP_SIZE is roughly the limit of
1937 // stub groups. We grow a stub group by adding input section until the size is
1938 // just below GROUP_SIZE. The last input section will be converted into a stub
1939 // table owner. If STUB_ALWAYS_AFTER_BRANCH is false, we also add input sectiond
1940 // after the stub table, effectively doubling the group size.
1941 //
1942 // This is similar to the group_sections() function in elf32-arm.c but is
1943 // implemented differently.
1944
1945 template<int size, bool big_endian>
1946 void AArch64_output_section<size, big_endian>::group_sections(
1947 section_size_type group_size,
1948 bool stubs_always_after_branch,
1949 Target_aarch64<size, big_endian>* target,
1950 const Task* task)
1951 {
1952 typedef enum
1953 {
1954 NO_GROUP,
1955 FINDING_STUB_SECTION,
1956 HAS_STUB_SECTION
1957 } State;
1958
1959 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
1960
1961 State state = NO_GROUP;
1962 section_size_type off = 0;
1963 section_size_type group_begin_offset = 0;
1964 section_size_type group_end_offset = 0;
1965 section_size_type stub_table_end_offset = 0;
1966 Input_section_list::const_iterator group_begin =
1967 this->input_sections().end();
1968 Input_section_list::const_iterator stub_table =
1969 this->input_sections().end();
1970 Input_section_list::const_iterator group_end = this->input_sections().end();
1971 for (Input_section_list::const_iterator p = this->input_sections().begin();
1972 p != this->input_sections().end();
1973 ++p)
1974 {
1975 section_size_type section_begin_offset =
1976 align_address(off, p->addralign());
1977 section_size_type section_end_offset =
1978 section_begin_offset + p->data_size();
1979
1980 // Check to see if we should group the previously seen sections.
1981 switch (state)
1982 {
1983 case NO_GROUP:
1984 break;
1985
1986 case FINDING_STUB_SECTION:
1987 // Adding this section makes the group larger than GROUP_SIZE.
1988 if (section_end_offset - group_begin_offset >= group_size)
1989 {
1990 if (stubs_always_after_branch)
1991 {
1992 gold_assert(group_end != this->input_sections().end());
1993 this->create_stub_group(group_begin, group_end, group_end,
1994 target, new_relaxed_sections,
1995 task);
1996 state = NO_GROUP;
1997 }
1998 else
1999 {
2000 // Input sections up to stub_group_size bytes after the stub
2001 // table can be handled by it too.
2002 state = HAS_STUB_SECTION;
2003 stub_table = group_end;
2004 stub_table_end_offset = group_end_offset;
2005 }
2006 }
2007 break;
2008
2009 case HAS_STUB_SECTION:
2010 // Adding this section makes the post stub-section group larger
2011 // than GROUP_SIZE.
2012 gold_unreachable();
2013 // NOT SUPPORTED YET. For completeness only.
2014 if (section_end_offset - stub_table_end_offset >= group_size)
2015 {
2016 gold_assert(group_end != this->input_sections().end());
2017 this->create_stub_group(group_begin, group_end, stub_table,
2018 target, new_relaxed_sections, task);
2019 state = NO_GROUP;
2020 }
2021 break;
2022
2023 default:
2024 gold_unreachable();
2025 }
2026
2027 // If we see an input section and currently there is no group, start
2028 // a new one. Skip any empty sections. We look at the data size
2029 // instead of calling p->relobj()->section_size() to avoid locking.
2030 if ((p->is_input_section() || p->is_relaxed_input_section())
2031 && (p->data_size() != 0))
2032 {
2033 if (state == NO_GROUP)
2034 {
2035 state = FINDING_STUB_SECTION;
2036 group_begin = p;
2037 group_begin_offset = section_begin_offset;
2038 }
2039
2040 // Keep track of the last input section seen.
2041 group_end = p;
2042 group_end_offset = section_end_offset;
2043 }
2044
2045 off = section_end_offset;
2046 }
2047
2048 // Create a stub group for any ungrouped sections.
2049 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
2050 {
2051 gold_assert(group_end != this->input_sections().end());
2052 this->create_stub_group(group_begin, group_end,
2053 (state == FINDING_STUB_SECTION
2054 ? group_end
2055 : stub_table),
2056 target, new_relaxed_sections, task);
2057 }
2058
2059 if (!new_relaxed_sections.empty())
2060 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
2061
2062 // Update the section offsets
2063 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
2064 {
2065 The_aarch64_relobj* relobj = static_cast<The_aarch64_relobj*>(
2066 new_relaxed_sections[i]->relobj());
2067 unsigned int shndx = new_relaxed_sections[i]->shndx();
2068 // Tell AArch64_relobj that this input section is converted.
2069 relobj->convert_input_section_to_relaxed_section(shndx);
2070 }
2071 } // End of AArch64_output_section::group_sections
2072
2073
2074 AArch64_reloc_property_table* aarch64_reloc_property_table = NULL;
2075
2076
2077 // The aarch64 target class.
2078 // See the ABI at
2079 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
2080 template<int size, bool big_endian>
2081 class Target_aarch64 : public Sized_target<size, big_endian>
2082 {
2083 public:
2084 typedef Target_aarch64<size, big_endian> This;
2085 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
2086 Reloc_section;
2087 typedef Relocate_info<size, big_endian> The_relocate_info;
2088 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2089 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2090 typedef Reloc_stub<size, big_endian> The_reloc_stub;
2091 typedef typename The_reloc_stub::Stub_type The_reloc_stub_type;
2092 typedef typename Reloc_stub<size, big_endian>::Key The_reloc_stub_key;
2093 typedef Stub_table<size, big_endian> The_stub_table;
2094 typedef std::vector<The_stub_table*> Stub_table_list;
2095 typedef typename Stub_table_list::iterator Stub_table_iterator;
2096 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2097 typedef AArch64_output_section<size, big_endian> The_aarch64_output_section;
2098 typedef Unordered_map<Section_id,
2099 AArch64_input_section<size, big_endian>*,
2100 Section_id_hash> AArch64_input_section_map;
2101 typedef AArch64_insn_utilities<big_endian> Insn_utilities;
2102 const static int TCB_SIZE = size / 8 * 2;
2103
2104 Target_aarch64(const Target::Target_info* info = &aarch64_info)
2105 : Sized_target<size, big_endian>(info),
2106 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
2107 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
2108 rela_irelative_(NULL), copy_relocs_(elfcpp::R_AARCH64_COPY),
2109 got_mod_index_offset_(-1U),
2110 tlsdesc_reloc_info_(), tls_base_symbol_defined_(false),
2111 stub_tables_(), stub_group_size_(0), aarch64_input_section_map_()
2112 { }
2113
2114 // Scan the relocations to determine unreferenced sections for
2115 // garbage collection.
2116 void
2117 gc_process_relocs(Symbol_table* symtab,
2118 Layout* layout,
2119 Sized_relobj_file<size, big_endian>* object,
2120 unsigned int data_shndx,
2121 unsigned int sh_type,
2122 const unsigned char* prelocs,
2123 size_t reloc_count,
2124 Output_section* output_section,
2125 bool needs_special_offset_handling,
2126 size_t local_symbol_count,
2127 const unsigned char* plocal_symbols);
2128
2129 // Scan the relocations to look for symbol adjustments.
2130 void
2131 scan_relocs(Symbol_table* symtab,
2132 Layout* layout,
2133 Sized_relobj_file<size, big_endian>* object,
2134 unsigned int data_shndx,
2135 unsigned int sh_type,
2136 const unsigned char* prelocs,
2137 size_t reloc_count,
2138 Output_section* output_section,
2139 bool needs_special_offset_handling,
2140 size_t local_symbol_count,
2141 const unsigned char* plocal_symbols);
2142
2143 // Finalize the sections.
2144 void
2145 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2146
2147 // Return the value to use for a dynamic which requires special
2148 // treatment.
2149 uint64_t
2150 do_dynsym_value(const Symbol*) const;
2151
2152 // Relocate a section.
2153 void
2154 relocate_section(const Relocate_info<size, big_endian>*,
2155 unsigned int sh_type,
2156 const unsigned char* prelocs,
2157 size_t reloc_count,
2158 Output_section* output_section,
2159 bool needs_special_offset_handling,
2160 unsigned char* view,
2161 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2162 section_size_type view_size,
2163 const Reloc_symbol_changes*);
2164
2165 // Scan the relocs during a relocatable link.
2166 void
2167 scan_relocatable_relocs(Symbol_table* symtab,
2168 Layout* layout,
2169 Sized_relobj_file<size, big_endian>* object,
2170 unsigned int data_shndx,
2171 unsigned int sh_type,
2172 const unsigned char* prelocs,
2173 size_t reloc_count,
2174 Output_section* output_section,
2175 bool needs_special_offset_handling,
2176 size_t local_symbol_count,
2177 const unsigned char* plocal_symbols,
2178 Relocatable_relocs*);
2179
2180 // Relocate a section during a relocatable link.
2181 void
2182 relocate_relocs(
2183 const Relocate_info<size, big_endian>*,
2184 unsigned int sh_type,
2185 const unsigned char* prelocs,
2186 size_t reloc_count,
2187 Output_section* output_section,
2188 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
2189 const Relocatable_relocs*,
2190 unsigned char* view,
2191 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2192 section_size_type view_size,
2193 unsigned char* reloc_view,
2194 section_size_type reloc_view_size);
2195
2196 // Return the symbol index to use for a target specific relocation.
2197 // The only target specific relocation is R_AARCH64_TLSDESC for a
2198 // local symbol, which is an absolute reloc.
2199 unsigned int
2200 do_reloc_symbol_index(void*, unsigned int r_type) const
2201 {
2202 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
2203 return 0;
2204 }
2205
2206 // Return the addend to use for a target specific relocation.
2207 uint64_t
2208 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
2209
2210 // Return the PLT section.
2211 uint64_t
2212 do_plt_address_for_global(const Symbol* gsym) const
2213 { return this->plt_section()->address_for_global(gsym); }
2214
2215 uint64_t
2216 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
2217 { return this->plt_section()->address_for_local(relobj, symndx); }
2218
2219 // This function should be defined in targets that can use relocation
2220 // types to determine (implemented in local_reloc_may_be_function_pointer
2221 // and global_reloc_may_be_function_pointer)
2222 // if a function's pointer is taken. ICF uses this in safe mode to only
2223 // fold those functions whose pointer is defintely not taken.
2224 bool
2225 do_can_check_for_function_pointers() const
2226 { return true; }
2227
2228 // Return the number of entries in the PLT.
2229 unsigned int
2230 plt_entry_count() const;
2231
2232 //Return the offset of the first non-reserved PLT entry.
2233 unsigned int
2234 first_plt_entry_offset() const;
2235
2236 // Return the size of each PLT entry.
2237 unsigned int
2238 plt_entry_size() const;
2239
2240 // Create a stub table.
2241 The_stub_table*
2242 new_stub_table(The_aarch64_input_section*);
2243
2244 // Create an aarch64 input section.
2245 The_aarch64_input_section*
2246 new_aarch64_input_section(Relobj*, unsigned int);
2247
2248 // Find an aarch64 input section instance for a given OBJ and SHNDX.
2249 The_aarch64_input_section*
2250 find_aarch64_input_section(Relobj*, unsigned int) const;
2251
2252 // Return the thread control block size.
2253 unsigned int
2254 tcb_size() const { return This::TCB_SIZE; }
2255
2256 // Scan a section for stub generation.
2257 void
2258 scan_section_for_stubs(const Relocate_info<size, big_endian>*, unsigned int,
2259 const unsigned char*, size_t, Output_section*,
2260 bool, const unsigned char*,
2261 Address,
2262 section_size_type);
2263
2264 // Scan a relocation section for stub.
2265 template<int sh_type>
2266 void
2267 scan_reloc_section_for_stubs(
2268 const The_relocate_info* relinfo,
2269 const unsigned char* prelocs,
2270 size_t reloc_count,
2271 Output_section* output_section,
2272 bool needs_special_offset_handling,
2273 const unsigned char* view,
2274 Address view_address,
2275 section_size_type);
2276
2277 // Relocate a single stub.
2278 void
2279 relocate_stub(The_reloc_stub*, const Relocate_info<size, big_endian>*,
2280 Output_section*, unsigned char*, Address,
2281 section_size_type);
2282
2283 // Get the default AArch64 target.
2284 static This*
2285 current_target()
2286 {
2287 gold_assert(parameters->target().machine_code() == elfcpp::EM_AARCH64
2288 && parameters->target().get_size() == size
2289 && parameters->target().is_big_endian() == big_endian);
2290 return static_cast<This*>(parameters->sized_target<size, big_endian>());
2291 }
2292
2293
2294 // Scan erratum for a part of a section.
2295 void
2296 scan_erratum_843419_span(
2297 AArch64_relobj<size, big_endian>*,
2298 unsigned int shndx,
2299 const section_size_type,
2300 const section_size_type,
2301 unsigned char*,
2302 Address);
2303
2304 protected:
2305 void
2306 do_select_as_default_target()
2307 {
2308 gold_assert(aarch64_reloc_property_table == NULL);
2309 aarch64_reloc_property_table = new AArch64_reloc_property_table();
2310 }
2311
2312 // Add a new reloc argument, returning the index in the vector.
2313 size_t
2314 add_tlsdesc_info(Sized_relobj_file<size, big_endian>* object,
2315 unsigned int r_sym)
2316 {
2317 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
2318 return this->tlsdesc_reloc_info_.size() - 1;
2319 }
2320
2321 virtual Output_data_plt_aarch64<size, big_endian>*
2322 do_make_data_plt(Layout* layout,
2323 Output_data_got_aarch64<size, big_endian>* got,
2324 Output_data_space* got_plt,
2325 Output_data_space* got_irelative)
2326 {
2327 return new Output_data_plt_aarch64_standard<size, big_endian>(
2328 layout, got, got_plt, got_irelative);
2329 }
2330
2331
2332 // do_make_elf_object to override the same function in the base class.
2333 Object*
2334 do_make_elf_object(const std::string&, Input_file*, off_t,
2335 const elfcpp::Ehdr<size, big_endian>&);
2336
2337 Output_data_plt_aarch64<size, big_endian>*
2338 make_data_plt(Layout* layout,
2339 Output_data_got_aarch64<size, big_endian>* got,
2340 Output_data_space* got_plt,
2341 Output_data_space* got_irelative)
2342 {
2343 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
2344 }
2345
2346 // We only need to generate stubs, and hence perform relaxation if we are
2347 // not doing relocatable linking.
2348 virtual bool
2349 do_may_relax() const
2350 { return !parameters->options().relocatable(); }
2351
2352 // Relaxation hook. This is where we do stub generation.
2353 virtual bool
2354 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
2355
2356 void
2357 group_sections(Layout* layout,
2358 section_size_type group_size,
2359 bool stubs_always_after_branch,
2360 const Task* task);
2361
2362 void
2363 scan_reloc_for_stub(const The_relocate_info*, unsigned int,
2364 const Sized_symbol<size>*, unsigned int,
2365 const Symbol_value<size>*,
2366 typename elfcpp::Elf_types<size>::Elf_Swxword,
2367 Address Elf_Addr);
2368
2369 // Make an output section.
2370 Output_section*
2371 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2372 elfcpp::Elf_Xword flags)
2373 { return new The_aarch64_output_section(name, type, flags); }
2374
2375 private:
2376 // The class which scans relocations.
2377 class Scan
2378 {
2379 public:
2380 Scan()
2381 : issued_non_pic_error_(false)
2382 { }
2383
2384 inline void
2385 local(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
2386 Sized_relobj_file<size, big_endian>* object,
2387 unsigned int data_shndx,
2388 Output_section* output_section,
2389 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
2390 const elfcpp::Sym<size, big_endian>& lsym,
2391 bool is_discarded);
2392
2393 inline void
2394 global(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
2395 Sized_relobj_file<size, big_endian>* object,
2396 unsigned int data_shndx,
2397 Output_section* output_section,
2398 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
2399 Symbol* gsym);
2400
2401 inline bool
2402 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
2403 Target_aarch64<size, big_endian>* ,
2404 Sized_relobj_file<size, big_endian>* ,
2405 unsigned int ,
2406 Output_section* ,
2407 const elfcpp::Rela<size, big_endian>& ,
2408 unsigned int r_type,
2409 const elfcpp::Sym<size, big_endian>&);
2410
2411 inline bool
2412 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
2413 Target_aarch64<size, big_endian>* ,
2414 Sized_relobj_file<size, big_endian>* ,
2415 unsigned int ,
2416 Output_section* ,
2417 const elfcpp::Rela<size, big_endian>& ,
2418 unsigned int r_type,
2419 Symbol* gsym);
2420
2421 private:
2422 static void
2423 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
2424 unsigned int r_type);
2425
2426 static void
2427 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
2428 unsigned int r_type, Symbol*);
2429
2430 inline bool
2431 possible_function_pointer_reloc(unsigned int r_type);
2432
2433 void
2434 check_non_pic(Relobj*, unsigned int r_type);
2435
2436 bool
2437 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
2438 unsigned int r_type);
2439
2440 // Whether we have issued an error about a non-PIC compilation.
2441 bool issued_non_pic_error_;
2442 };
2443
2444 // The class which implements relocation.
2445 class Relocate
2446 {
2447 public:
2448 Relocate()
2449 : skip_call_tls_get_addr_(false)
2450 { }
2451
2452 ~Relocate()
2453 { }
2454
2455 // Do a relocation. Return false if the caller should not issue
2456 // any warnings about this relocation.
2457 inline bool
2458 relocate(const Relocate_info<size, big_endian>*, Target_aarch64*,
2459 Output_section*,
2460 size_t relnum, const elfcpp::Rela<size, big_endian>&,
2461 unsigned int r_type, const Sized_symbol<size>*,
2462 const Symbol_value<size>*,
2463 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
2464 section_size_type);
2465
2466 private:
2467 inline typename AArch64_relocate_functions<size, big_endian>::Status
2468 relocate_tls(const Relocate_info<size, big_endian>*,
2469 Target_aarch64<size, big_endian>*,
2470 size_t,
2471 const elfcpp::Rela<size, big_endian>&,
2472 unsigned int r_type, const Sized_symbol<size>*,
2473 const Symbol_value<size>*,
2474 unsigned char*,
2475 typename elfcpp::Elf_types<size>::Elf_Addr);
2476
2477 inline typename AArch64_relocate_functions<size, big_endian>::Status
2478 tls_gd_to_le(
2479 const Relocate_info<size, big_endian>*,
2480 Target_aarch64<size, big_endian>*,
2481 const elfcpp::Rela<size, big_endian>&,
2482 unsigned int,
2483 unsigned char*,
2484 const Symbol_value<size>*);
2485
2486 inline typename AArch64_relocate_functions<size, big_endian>::Status
2487 tls_ld_to_le(
2488 const Relocate_info<size, big_endian>*,
2489 Target_aarch64<size, big_endian>*,
2490 const elfcpp::Rela<size, big_endian>&,
2491 unsigned int,
2492 unsigned char*,
2493 const Symbol_value<size>*);
2494
2495 inline typename AArch64_relocate_functions<size, big_endian>::Status
2496 tls_ie_to_le(
2497 const Relocate_info<size, big_endian>*,
2498 Target_aarch64<size, big_endian>*,
2499 const elfcpp::Rela<size, big_endian>&,
2500 unsigned int,
2501 unsigned char*,
2502 const Symbol_value<size>*);
2503
2504 inline typename AArch64_relocate_functions<size, big_endian>::Status
2505 tls_desc_gd_to_le(
2506 const Relocate_info<size, big_endian>*,
2507 Target_aarch64<size, big_endian>*,
2508 const elfcpp::Rela<size, big_endian>&,
2509 unsigned int,
2510 unsigned char*,
2511 const Symbol_value<size>*);
2512
2513 inline typename AArch64_relocate_functions<size, big_endian>::Status
2514 tls_desc_gd_to_ie(
2515 const Relocate_info<size, big_endian>*,
2516 Target_aarch64<size, big_endian>*,
2517 const elfcpp::Rela<size, big_endian>&,
2518 unsigned int,
2519 unsigned char*,
2520 const Symbol_value<size>*,
2521 typename elfcpp::Elf_types<size>::Elf_Addr,
2522 typename elfcpp::Elf_types<size>::Elf_Addr);
2523
2524 bool skip_call_tls_get_addr_;
2525
2526 }; // End of class Relocate
2527
2528 // A class which returns the size required for a relocation type,
2529 // used while scanning relocs during a relocatable link.
2530 class Relocatable_size_for_reloc
2531 {
2532 public:
2533 unsigned int
2534 get_size_for_reloc(unsigned int, Relobj*);
2535 };
2536
2537 // Adjust TLS relocation type based on the options and whether this
2538 // is a local symbol.
2539 static tls::Tls_optimization
2540 optimize_tls_reloc(bool is_final, int r_type);
2541
2542 // Get the GOT section, creating it if necessary.
2543 Output_data_got_aarch64<size, big_endian>*
2544 got_section(Symbol_table*, Layout*);
2545
2546 // Get the GOT PLT section.
2547 Output_data_space*
2548 got_plt_section() const
2549 {
2550 gold_assert(this->got_plt_ != NULL);
2551 return this->got_plt_;
2552 }
2553
2554 // Get the GOT section for TLSDESC entries.
2555 Output_data_got<size, big_endian>*
2556 got_tlsdesc_section() const
2557 {
2558 gold_assert(this->got_tlsdesc_ != NULL);
2559 return this->got_tlsdesc_;
2560 }
2561
2562 // Create the PLT section.
2563 void
2564 make_plt_section(Symbol_table* symtab, Layout* layout);
2565
2566 // Create a PLT entry for a global symbol.
2567 void
2568 make_plt_entry(Symbol_table*, Layout*, Symbol*);
2569
2570 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
2571 void
2572 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
2573 Sized_relobj_file<size, big_endian>* relobj,
2574 unsigned int local_sym_index);
2575
2576 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2577 void
2578 define_tls_base_symbol(Symbol_table*, Layout*);
2579
2580 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2581 void
2582 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
2583
2584 // Create a GOT entry for the TLS module index.
2585 unsigned int
2586 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2587 Sized_relobj_file<size, big_endian>* object);
2588
2589 // Get the PLT section.
2590 Output_data_plt_aarch64<size, big_endian>*
2591 plt_section() const
2592 {
2593 gold_assert(this->plt_ != NULL);
2594 return this->plt_;
2595 }
2596
2597 // Return whether this is a 3-insn erratum sequence.
2598 bool is_erratum_843419_sequence(
2599 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
2600 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
2601 typename elfcpp::Swap<32,big_endian>::Valtype insn3);
2602
2603 // Get the dynamic reloc section, creating it if necessary.
2604 Reloc_section*
2605 rela_dyn_section(Layout*);
2606
2607 // Get the section to use for TLSDESC relocations.
2608 Reloc_section*
2609 rela_tlsdesc_section(Layout*) const;
2610
2611 // Get the section to use for IRELATIVE relocations.
2612 Reloc_section*
2613 rela_irelative_section(Layout*);
2614
2615 // Add a potential copy relocation.
2616 void
2617 copy_reloc(Symbol_table* symtab, Layout* layout,
2618 Sized_relobj_file<size, big_endian>* object,
2619 unsigned int shndx, Output_section* output_section,
2620 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
2621 {
2622 this->copy_relocs_.copy_reloc(symtab, layout,
2623 symtab->get_sized_symbol<size>(sym),
2624 object, shndx, output_section,
2625 reloc, this->rela_dyn_section(layout));
2626 }
2627
2628 // Information about this specific target which we pass to the
2629 // general Target structure.
2630 static const Target::Target_info aarch64_info;
2631
2632 // The types of GOT entries needed for this platform.
2633 // These values are exposed to the ABI in an incremental link.
2634 // Do not renumber existing values without changing the version
2635 // number of the .gnu_incremental_inputs section.
2636 enum Got_type
2637 {
2638 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
2639 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
2640 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
2641 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
2642 };
2643
2644 // This type is used as the argument to the target specific
2645 // relocation routines. The only target specific reloc is
2646 // R_AARCh64_TLSDESC against a local symbol.
2647 struct Tlsdesc_info
2648 {
2649 Tlsdesc_info(Sized_relobj_file<size, big_endian>* a_object,
2650 unsigned int a_r_sym)
2651 : object(a_object), r_sym(a_r_sym)
2652 { }
2653
2654 // The object in which the local symbol is defined.
2655 Sized_relobj_file<size, big_endian>* object;
2656 // The local symbol index in the object.
2657 unsigned int r_sym;
2658 };
2659
2660 // The GOT section.
2661 Output_data_got_aarch64<size, big_endian>* got_;
2662 // The PLT section.
2663 Output_data_plt_aarch64<size, big_endian>* plt_;
2664 // The GOT PLT section.
2665 Output_data_space* got_plt_;
2666 // The GOT section for IRELATIVE relocations.
2667 Output_data_space* got_irelative_;
2668 // The GOT section for TLSDESC relocations.
2669 Output_data_got<size, big_endian>* got_tlsdesc_;
2670 // The _GLOBAL_OFFSET_TABLE_ symbol.
2671 Symbol* global_offset_table_;
2672 // The dynamic reloc section.
2673 Reloc_section* rela_dyn_;
2674 // The section to use for IRELATIVE relocs.
2675 Reloc_section* rela_irelative_;
2676 // Relocs saved to avoid a COPY reloc.
2677 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
2678 // Offset of the GOT entry for the TLS module index.
2679 unsigned int got_mod_index_offset_;
2680 // We handle R_AARCH64_TLSDESC against a local symbol as a target
2681 // specific relocation. Here we store the object and local symbol
2682 // index for the relocation.
2683 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
2684 // True if the _TLS_MODULE_BASE_ symbol has been defined.
2685 bool tls_base_symbol_defined_;
2686 // List of stub_tables
2687 Stub_table_list stub_tables_;
2688 // Actual stub group size
2689 section_size_type stub_group_size_;
2690 AArch64_input_section_map aarch64_input_section_map_;
2691 }; // End of Target_aarch64
2692
2693
2694 template<>
2695 const Target::Target_info Target_aarch64<64, false>::aarch64_info =
2696 {
2697 64, // size
2698 false, // is_big_endian
2699 elfcpp::EM_AARCH64, // machine_code
2700 false, // has_make_symbol
2701 false, // has_resolve
2702 false, // has_code_fill
2703 true, // is_default_stack_executable
2704 true, // can_icf_inline_merge_sections
2705 '\0', // wrap_char
2706 "/lib/ld.so.1", // program interpreter
2707 0x400000, // default_text_segment_address
2708 0x1000, // abi_pagesize (overridable by -z max-page-size)
2709 0x1000, // common_pagesize (overridable by -z common-page-size)
2710 false, // isolate_execinstr
2711 0, // rosegment_gap
2712 elfcpp::SHN_UNDEF, // small_common_shndx
2713 elfcpp::SHN_UNDEF, // large_common_shndx
2714 0, // small_common_section_flags
2715 0, // large_common_section_flags
2716 NULL, // attributes_section
2717 NULL, // attributes_vendor
2718 "_start" // entry_symbol_name
2719 };
2720
2721 template<>
2722 const Target::Target_info Target_aarch64<32, false>::aarch64_info =
2723 {
2724 32, // size
2725 false, // is_big_endian
2726 elfcpp::EM_AARCH64, // machine_code
2727 false, // has_make_symbol
2728 false, // has_resolve
2729 false, // has_code_fill
2730 true, // is_default_stack_executable
2731 false, // can_icf_inline_merge_sections
2732 '\0', // wrap_char
2733 "/lib/ld.so.1", // program interpreter
2734 0x400000, // default_text_segment_address
2735 0x1000, // abi_pagesize (overridable by -z max-page-size)
2736 0x1000, // common_pagesize (overridable by -z common-page-size)
2737 false, // isolate_execinstr
2738 0, // rosegment_gap
2739 elfcpp::SHN_UNDEF, // small_common_shndx
2740 elfcpp::SHN_UNDEF, // large_common_shndx
2741 0, // small_common_section_flags
2742 0, // large_common_section_flags
2743 NULL, // attributes_section
2744 NULL, // attributes_vendor
2745 "_start" // entry_symbol_name
2746 };
2747
2748 template<>
2749 const Target::Target_info Target_aarch64<64, true>::aarch64_info =
2750 {
2751 64, // size
2752 true, // is_big_endian
2753 elfcpp::EM_AARCH64, // machine_code
2754 false, // has_make_symbol
2755 false, // has_resolve
2756 false, // has_code_fill
2757 true, // is_default_stack_executable
2758 true, // can_icf_inline_merge_sections
2759 '\0', // wrap_char
2760 "/lib/ld.so.1", // program interpreter
2761 0x400000, // default_text_segment_address
2762 0x1000, // abi_pagesize (overridable by -z max-page-size)
2763 0x1000, // common_pagesize (overridable by -z common-page-size)
2764 false, // isolate_execinstr
2765 0, // rosegment_gap
2766 elfcpp::SHN_UNDEF, // small_common_shndx
2767 elfcpp::SHN_UNDEF, // large_common_shndx
2768 0, // small_common_section_flags
2769 0, // large_common_section_flags
2770 NULL, // attributes_section
2771 NULL, // attributes_vendor
2772 "_start" // entry_symbol_name
2773 };
2774
2775 template<>
2776 const Target::Target_info Target_aarch64<32, true>::aarch64_info =
2777 {
2778 32, // size
2779 true, // is_big_endian
2780 elfcpp::EM_AARCH64, // machine_code
2781 false, // has_make_symbol
2782 false, // has_resolve
2783 false, // has_code_fill
2784 true, // is_default_stack_executable
2785 false, // can_icf_inline_merge_sections
2786 '\0', // wrap_char
2787 "/lib/ld.so.1", // program interpreter
2788 0x400000, // default_text_segment_address
2789 0x1000, // abi_pagesize (overridable by -z max-page-size)
2790 0x1000, // common_pagesize (overridable by -z common-page-size)
2791 false, // isolate_execinstr
2792 0, // rosegment_gap
2793 elfcpp::SHN_UNDEF, // small_common_shndx
2794 elfcpp::SHN_UNDEF, // large_common_shndx
2795 0, // small_common_section_flags
2796 0, // large_common_section_flags
2797 NULL, // attributes_section
2798 NULL, // attributes_vendor
2799 "_start" // entry_symbol_name
2800 };
2801
2802 // Get the GOT section, creating it if necessary.
2803
2804 template<int size, bool big_endian>
2805 Output_data_got_aarch64<size, big_endian>*
2806 Target_aarch64<size, big_endian>::got_section(Symbol_table* symtab,
2807 Layout* layout)
2808 {
2809 if (this->got_ == NULL)
2810 {
2811 gold_assert(symtab != NULL && layout != NULL);
2812
2813 // When using -z now, we can treat .got.plt as a relro section.
2814 // Without -z now, it is modified after program startup by lazy
2815 // PLT relocations.
2816 bool is_got_plt_relro = parameters->options().now();
2817 Output_section_order got_order = (is_got_plt_relro
2818 ? ORDER_RELRO
2819 : ORDER_RELRO_LAST);
2820 Output_section_order got_plt_order = (is_got_plt_relro
2821 ? ORDER_RELRO
2822 : ORDER_NON_RELRO_FIRST);
2823
2824 // Layout of .got and .got.plt sections.
2825 // .got[0] &_DYNAMIC <-_GLOBAL_OFFSET_TABLE_
2826 // ...
2827 // .gotplt[0] reserved for ld.so (&linkmap) <--DT_PLTGOT
2828 // .gotplt[1] reserved for ld.so (resolver)
2829 // .gotplt[2] reserved
2830
2831 // Generate .got section.
2832 this->got_ = new Output_data_got_aarch64<size, big_endian>(symtab,
2833 layout);
2834 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2835 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
2836 this->got_, got_order, true);
2837 // The first word of GOT is reserved for the address of .dynamic.
2838 // We put 0 here now. The value will be replaced later in
2839 // Output_data_got_aarch64::do_write.
2840 this->got_->add_constant(0);
2841
2842 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2843 // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
2844 // even if there is a .got.plt section.
2845 this->global_offset_table_ =
2846 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2847 Symbol_table::PREDEFINED,
2848 this->got_,
2849 0, 0, elfcpp::STT_OBJECT,
2850 elfcpp::STB_LOCAL,
2851 elfcpp::STV_HIDDEN, 0,
2852 false, false);
2853
2854 // Generate .got.plt section.
2855 this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
2856 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2857 (elfcpp::SHF_ALLOC
2858 | elfcpp::SHF_WRITE),
2859 this->got_plt_, got_plt_order,
2860 is_got_plt_relro);
2861
2862 // The first three entries are reserved.
2863 this->got_plt_->set_current_data_size(
2864 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
2865
2866 // If there are any IRELATIVE relocations, they get GOT entries
2867 // in .got.plt after the jump slot entries.
2868 this->got_irelative_ = new Output_data_space(size / 8,
2869 "** GOT IRELATIVE PLT");
2870 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2871 (elfcpp::SHF_ALLOC
2872 | elfcpp::SHF_WRITE),
2873 this->got_irelative_,
2874 got_plt_order,
2875 is_got_plt_relro);
2876
2877 // If there are any TLSDESC relocations, they get GOT entries in
2878 // .got.plt after the jump slot and IRELATIVE entries.
2879 this->got_tlsdesc_ = new Output_data_got<size, big_endian>();
2880 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2881 (elfcpp::SHF_ALLOC
2882 | elfcpp::SHF_WRITE),
2883 this->got_tlsdesc_,
2884 got_plt_order,
2885 is_got_plt_relro);
2886
2887 if (!is_got_plt_relro)
2888 {
2889 // Those bytes can go into the relro segment.
2890 layout->increase_relro(
2891 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
2892 }
2893
2894 }
2895 return this->got_;
2896 }
2897
2898 // Get the dynamic reloc section, creating it if necessary.
2899
2900 template<int size, bool big_endian>
2901 typename Target_aarch64<size, big_endian>::Reloc_section*
2902 Target_aarch64<size, big_endian>::rela_dyn_section(Layout* layout)
2903 {
2904 if (this->rela_dyn_ == NULL)
2905 {
2906 gold_assert(layout != NULL);
2907 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2908 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2909 elfcpp::SHF_ALLOC, this->rela_dyn_,
2910 ORDER_DYNAMIC_RELOCS, false);
2911 }
2912 return this->rela_dyn_;
2913 }
2914
2915 // Get the section to use for IRELATIVE relocs, creating it if
2916 // necessary. These go in .rela.dyn, but only after all other dynamic
2917 // relocations. They need to follow the other dynamic relocations so
2918 // that they can refer to global variables initialized by those
2919 // relocs.
2920
2921 template<int size, bool big_endian>
2922 typename Target_aarch64<size, big_endian>::Reloc_section*
2923 Target_aarch64<size, big_endian>::rela_irelative_section(Layout* layout)
2924 {
2925 if (this->rela_irelative_ == NULL)
2926 {
2927 // Make sure we have already created the dynamic reloc section.
2928 this->rela_dyn_section(layout);
2929 this->rela_irelative_ = new Reloc_section(false);
2930 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2931 elfcpp::SHF_ALLOC, this->rela_irelative_,
2932 ORDER_DYNAMIC_RELOCS, false);
2933 gold_assert(this->rela_dyn_->output_section()
2934 == this->rela_irelative_->output_section());
2935 }
2936 return this->rela_irelative_;
2937 }
2938
2939
2940 // do_make_elf_object to override the same function in the base class. We need
2941 // to use a target-specific sub-class of Sized_relobj_file<size, big_endian> to
2942 // store backend specific information. Hence we need to have our own ELF object
2943 // creation.
2944
2945 template<int size, bool big_endian>
2946 Object*
2947 Target_aarch64<size, big_endian>::do_make_elf_object(
2948 const std::string& name,
2949 Input_file* input_file,
2950 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2951 {
2952 int et = ehdr.get_e_type();
2953 // ET_EXEC files are valid input for --just-symbols/-R,
2954 // and we treat them as relocatable objects.
2955 if (et == elfcpp::ET_EXEC && input_file->just_symbols())
2956 return Sized_target<size, big_endian>::do_make_elf_object(
2957 name, input_file, offset, ehdr);
2958 else if (et == elfcpp::ET_REL)
2959 {
2960 AArch64_relobj<size, big_endian>* obj =
2961 new AArch64_relobj<size, big_endian>(name, input_file, offset, ehdr);
2962 obj->setup();
2963 return obj;
2964 }
2965 else if (et == elfcpp::ET_DYN)
2966 {
2967 // Keep base implementation.
2968 Sized_dynobj<size, big_endian>* obj =
2969 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2970 obj->setup();
2971 return obj;
2972 }
2973 else
2974 {
2975 gold_error(_("%s: unsupported ELF file type %d"),
2976 name.c_str(), et);
2977 return NULL;
2978 }
2979 }
2980
2981
2982 // Scan a relocation for stub generation.
2983
2984 template<int size, bool big_endian>
2985 void
2986 Target_aarch64<size, big_endian>::scan_reloc_for_stub(
2987 const Relocate_info<size, big_endian>* relinfo,
2988 unsigned int r_type,
2989 const Sized_symbol<size>* gsym,
2990 unsigned int r_sym,
2991 const Symbol_value<size>* psymval,
2992 typename elfcpp::Elf_types<size>::Elf_Swxword addend,
2993 Address address)
2994 {
2995 const AArch64_relobj<size, big_endian>* aarch64_relobj =
2996 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
2997
2998 Symbol_value<size> symval;
2999 if (gsym != NULL)
3000 {
3001 const AArch64_reloc_property* arp = aarch64_reloc_property_table->
3002 get_reloc_property(r_type);
3003 if (gsym->use_plt_offset(arp->reference_flags()))
3004 {
3005 // This uses a PLT, change the symbol value.
3006 symval.set_output_value(this->plt_section()->address()
3007 + gsym->plt_offset());
3008 psymval = &symval;
3009 }
3010 else if (gsym->is_undefined())
3011 // There is no need to generate a stub symbol is undefined.
3012 return;
3013 }
3014
3015 // Get the symbol value.
3016 typename Symbol_value<size>::Value value = psymval->value(aarch64_relobj, 0);
3017
3018 // Owing to pipelining, the PC relative branches below actually skip
3019 // two instructions when the branch offset is 0.
3020 Address destination = static_cast<Address>(-1);
3021 switch (r_type)
3022 {
3023 case elfcpp::R_AARCH64_CALL26:
3024 case elfcpp::R_AARCH64_JUMP26:
3025 destination = value + addend;
3026 break;
3027 default:
3028 gold_unreachable();
3029 }
3030
3031 typename The_reloc_stub::Stub_type stub_type = The_reloc_stub::
3032 stub_type_for_reloc(r_type, address, destination);
3033 if (stub_type == The_reloc_stub::ST_NONE)
3034 return;
3035
3036 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
3037 gold_assert(stub_table != NULL);
3038
3039 The_reloc_stub_key key(stub_type, gsym, aarch64_relobj, r_sym, addend);
3040 The_reloc_stub* stub = stub_table->find_reloc_stub(key);
3041 if (stub == NULL)
3042 {
3043 stub = new The_reloc_stub(stub_type);
3044 stub_table->add_reloc_stub(stub, key);
3045 }
3046 stub->set_destination_address(destination);
3047 } // End of Target_aarch64::scan_reloc_for_stub
3048
3049
3050 // This function scans a relocation section for stub generation.
3051 // The template parameter Relocate must be a class type which provides
3052 // a single function, relocate(), which implements the machine
3053 // specific part of a relocation.
3054
3055 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
3056 // SHT_REL or SHT_RELA.
3057
3058 // PRELOCS points to the relocation data. RELOC_COUNT is the number
3059 // of relocs. OUTPUT_SECTION is the output section.
3060 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
3061 // mapped to output offsets.
3062
3063 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
3064 // VIEW_SIZE is the size. These refer to the input section, unless
3065 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
3066 // the output section.
3067
3068 template<int size, bool big_endian>
3069 template<int sh_type>
3070 void inline
3071 Target_aarch64<size, big_endian>::scan_reloc_section_for_stubs(
3072 const Relocate_info<size, big_endian>* relinfo,
3073 const unsigned char* prelocs,
3074 size_t reloc_count,
3075 Output_section* /*output_section*/,
3076 bool /*needs_special_offset_handling*/,
3077 const unsigned char* /*view*/,
3078 Address view_address,
3079 section_size_type)
3080 {
3081 typedef typename Reloc_types<sh_type,size,big_endian>::Reloc Reltype;
3082
3083 const int reloc_size =
3084 Reloc_types<sh_type,size,big_endian>::reloc_size;
3085 AArch64_relobj<size, big_endian>* object =
3086 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3087 unsigned int local_count = object->local_symbol_count();
3088
3089 gold::Default_comdat_behavior default_comdat_behavior;
3090 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
3091
3092 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
3093 {
3094 Reltype reloc(prelocs);
3095 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
3096 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3097 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
3098 if (r_type != elfcpp::R_AARCH64_CALL26
3099 && r_type != elfcpp::R_AARCH64_JUMP26)
3100 continue;
3101
3102 section_offset_type offset =
3103 convert_to_section_size_type(reloc.get_r_offset());
3104
3105 // Get the addend.
3106 typename elfcpp::Elf_types<size>::Elf_Swxword addend =
3107 reloc.get_r_addend();
3108
3109 const Sized_symbol<size>* sym;
3110 Symbol_value<size> symval;
3111 const Symbol_value<size> *psymval;
3112 bool is_defined_in_discarded_section;
3113 unsigned int shndx;
3114 if (r_sym < local_count)
3115 {
3116 sym = NULL;
3117 psymval = object->local_symbol(r_sym);
3118
3119 // If the local symbol belongs to a section we are discarding,
3120 // and that section is a debug section, try to find the
3121 // corresponding kept section and map this symbol to its
3122 // counterpart in the kept section. The symbol must not
3123 // correspond to a section we are folding.
3124 bool is_ordinary;
3125 shndx = psymval->input_shndx(&is_ordinary);
3126 is_defined_in_discarded_section =
3127 (is_ordinary
3128 && shndx != elfcpp::SHN_UNDEF
3129 && !object->is_section_included(shndx)
3130 && !relinfo->symtab->is_section_folded(object, shndx));
3131
3132 // We need to compute the would-be final value of this local
3133 // symbol.
3134 if (!is_defined_in_discarded_section)
3135 {
3136 typedef Sized_relobj_file<size, big_endian> ObjType;
3137 typename ObjType::Compute_final_local_value_status status =
3138 object->compute_final_local_value(r_sym, psymval, &symval,
3139 relinfo->symtab);
3140 if (status == ObjType::CFLV_OK)
3141 {
3142 // Currently we cannot handle a branch to a target in
3143 // a merged section. If this is the case, issue an error
3144 // and also free the merge symbol value.
3145 if (!symval.has_output_value())
3146 {
3147 const std::string& section_name =
3148 object->section_name(shndx);
3149 object->error(_("cannot handle branch to local %u "
3150 "in a merged section %s"),
3151 r_sym, section_name.c_str());
3152 }
3153 psymval = &symval;
3154 }
3155 else
3156 {
3157 // We cannot determine the final value.
3158 continue;
3159 }
3160 }
3161 }
3162 else
3163 {
3164 const Symbol* gsym;
3165 gsym = object->global_symbol(r_sym);
3166 gold_assert(gsym != NULL);
3167 if (gsym->is_forwarder())
3168 gsym = relinfo->symtab->resolve_forwards(gsym);
3169
3170 sym = static_cast<const Sized_symbol<size>*>(gsym);
3171 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
3172 symval.set_output_symtab_index(sym->symtab_index());
3173 else
3174 symval.set_no_output_symtab_entry();
3175
3176 // We need to compute the would-be final value of this global
3177 // symbol.
3178 const Symbol_table* symtab = relinfo->symtab;
3179 const Sized_symbol<size>* sized_symbol =
3180 symtab->get_sized_symbol<size>(gsym);
3181 Symbol_table::Compute_final_value_status status;
3182 typename elfcpp::Elf_types<size>::Elf_Addr value =
3183 symtab->compute_final_value<size>(sized_symbol, &status);
3184
3185 // Skip this if the symbol has not output section.
3186 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
3187 continue;
3188 symval.set_output_value(value);
3189
3190 if (gsym->type() == elfcpp::STT_TLS)
3191 symval.set_is_tls_symbol();
3192 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3193 symval.set_is_ifunc_symbol();
3194 psymval = &symval;
3195
3196 is_defined_in_discarded_section =
3197 (gsym->is_defined_in_discarded_section()
3198 && gsym->is_undefined());
3199 shndx = 0;
3200 }
3201
3202 Symbol_value<size> symval2;
3203 if (is_defined_in_discarded_section)
3204 {
3205 if (comdat_behavior == CB_UNDETERMINED)
3206 {
3207 std::string name = object->section_name(relinfo->data_shndx);
3208 comdat_behavior = default_comdat_behavior.get(name.c_str());
3209 }
3210 if (comdat_behavior == CB_PRETEND)
3211 {
3212 bool found;
3213 typename elfcpp::Elf_types<size>::Elf_Addr value =
3214 object->map_to_kept_section(shndx, &found);
3215 if (found)
3216 symval2.set_output_value(value + psymval->input_value());
3217 else
3218 symval2.set_output_value(0);
3219 }
3220 else
3221 {
3222 if (comdat_behavior == CB_WARNING)
3223 gold_warning_at_location(relinfo, i, offset,
3224 _("relocation refers to discarded "
3225 "section"));
3226 symval2.set_output_value(0);
3227 }
3228 symval2.set_no_output_symtab_entry();
3229 psymval = &symval2;
3230 }
3231
3232 // If symbol is a section symbol, we don't know the actual type of
3233 // destination. Give up.
3234 if (psymval->is_section_symbol())
3235 continue;
3236
3237 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
3238 addend, view_address + offset);
3239 } // End of iterating relocs in a section
3240 } // End of Target_aarch64::scan_reloc_section_for_stubs
3241
3242
3243 // Scan an input section for stub generation.
3244
3245 template<int size, bool big_endian>
3246 void
3247 Target_aarch64<size, big_endian>::scan_section_for_stubs(
3248 const Relocate_info<size, big_endian>* relinfo,
3249 unsigned int sh_type,
3250 const unsigned char* prelocs,
3251 size_t reloc_count,
3252 Output_section* output_section,
3253 bool needs_special_offset_handling,
3254 const unsigned char* view,
3255 Address view_address,
3256 section_size_type view_size)
3257 {
3258 gold_assert(sh_type == elfcpp::SHT_RELA);
3259 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
3260 relinfo,
3261 prelocs,
3262 reloc_count,
3263 output_section,
3264 needs_special_offset_handling,
3265 view,
3266 view_address,
3267 view_size);
3268 }
3269
3270
3271 // Relocate a single stub.
3272
3273 template<int size, bool big_endian>
3274 void Target_aarch64<size, big_endian>::
3275 relocate_stub(The_reloc_stub* stub,
3276 const The_relocate_info*,
3277 Output_section*,
3278 unsigned char* view,
3279 Address address,
3280 section_size_type)
3281 {
3282 typedef AArch64_relocate_functions<size, big_endian> The_reloc_functions;
3283 typedef typename The_reloc_functions::Status The_reloc_functions_status;
3284 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
3285
3286 Insntype* ip = reinterpret_cast<Insntype*>(view);
3287 int insn_number = stub->stub_insn_number();
3288 const uint32_t* insns = stub->stub_insns();
3289 // Check the insns are really those stub insns.
3290 for (int i = 0; i < insn_number; ++i)
3291 {
3292 Insntype insn = elfcpp::Swap<32,big_endian>::readval(ip + i);
3293 gold_assert(((uint32_t)insn == insns[i+1]));
3294 }
3295
3296 Address dest = stub->destination_address();
3297
3298 switch(stub->stub_type())
3299 {
3300 case The_reloc_stub::ST_ADRP_BRANCH:
3301 {
3302 // 1st reloc is ADR_PREL_PG_HI21
3303 The_reloc_functions_status status =
3304 The_reloc_functions::adrp(view, dest, address);
3305 // An error should never arise in the above step. If so, please
3306 // check 'aarch64_valid_for_adrp_p'.
3307 gold_assert(status == The_reloc_functions::STATUS_OKAY);
3308
3309 // 2nd reloc is ADD_ABS_LO12_NC
3310 const AArch64_reloc_property* arp =
3311 aarch64_reloc_property_table->get_reloc_property(
3312 elfcpp::R_AARCH64_ADD_ABS_LO12_NC);
3313 gold_assert(arp != NULL);
3314 status = The_reloc_functions::template
3315 rela_general<32>(view + 4, dest, 0, arp);
3316 // An error should never arise, it is an "_NC" relocation.
3317 gold_assert(status == The_reloc_functions::STATUS_OKAY);
3318 }
3319 break;
3320
3321 case The_reloc_stub::ST_LONG_BRANCH_ABS:
3322 // 1st reloc is R_AARCH64_PREL64, at offset 8
3323 elfcpp::Swap<64,big_endian>::writeval(view + 8, dest);
3324 break;
3325
3326 case The_reloc_stub::ST_LONG_BRANCH_PCREL:
3327 {
3328 // "PC" calculation is the 2nd insn in the stub.
3329 uint64_t offset = dest - (address + 4);
3330 // Offset is placed at offset 4 and 5.
3331 elfcpp::Swap<64,big_endian>::writeval(view + 16, offset);
3332 }
3333 break;
3334
3335 default:
3336 gold_unreachable();
3337 }
3338 }
3339
3340
3341 // A class to handle the PLT data.
3342 // This is an abstract base class that handles most of the linker details
3343 // but does not know the actual contents of PLT entries. The derived
3344 // classes below fill in those details.
3345
3346 template<int size, bool big_endian>
3347 class Output_data_plt_aarch64 : public Output_section_data
3348 {
3349 public:
3350 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
3351 Reloc_section;
3352 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3353
3354 Output_data_plt_aarch64(Layout* layout,
3355 uint64_t addralign,
3356 Output_data_got_aarch64<size, big_endian>* got,
3357 Output_data_space* got_plt,
3358 Output_data_space* got_irelative)
3359 : Output_section_data(addralign), tlsdesc_rel_(NULL), irelative_rel_(NULL),
3360 got_(got), got_plt_(got_plt), got_irelative_(got_irelative),
3361 count_(0), irelative_count_(0), tlsdesc_got_offset_(-1U)
3362 { this->init(layout); }
3363
3364 // Initialize the PLT section.
3365 void
3366 init(Layout* layout);
3367
3368 // Add an entry to the PLT.
3369 void
3370 add_entry(Symbol_table*, Layout*, Symbol* gsym);
3371
3372 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
3373 unsigned int
3374 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
3375 Sized_relobj_file<size, big_endian>* relobj,
3376 unsigned int local_sym_index);
3377
3378 // Add the relocation for a PLT entry.
3379 void
3380 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
3381 unsigned int got_offset);
3382
3383 // Add the reserved TLSDESC_PLT entry to the PLT.
3384 void
3385 reserve_tlsdesc_entry(unsigned int got_offset)
3386 { this->tlsdesc_got_offset_ = got_offset; }
3387
3388 // Return true if a TLSDESC_PLT entry has been reserved.
3389 bool
3390 has_tlsdesc_entry() const
3391 { return this->tlsdesc_got_offset_ != -1U; }
3392
3393 // Return the GOT offset for the reserved TLSDESC_PLT entry.
3394 unsigned int
3395 get_tlsdesc_got_offset() const
3396 { return this->tlsdesc_got_offset_; }
3397
3398 // Return the PLT offset of the reserved TLSDESC_PLT entry.
3399 unsigned int
3400 get_tlsdesc_plt_offset() const
3401 {
3402 return (this->first_plt_entry_offset() +
3403 (this->count_ + this->irelative_count_)
3404 * this->get_plt_entry_size());
3405 }
3406
3407 // Return the .rela.plt section data.
3408 Reloc_section*
3409 rela_plt()
3410 { return this->rel_; }
3411
3412 // Return where the TLSDESC relocations should go.
3413 Reloc_section*
3414 rela_tlsdesc(Layout*);
3415
3416 // Return where the IRELATIVE relocations should go in the PLT
3417 // relocations.
3418 Reloc_section*
3419 rela_irelative(Symbol_table*, Layout*);
3420
3421 // Return whether we created a section for IRELATIVE relocations.
3422 bool
3423 has_irelative_section() const
3424 { return this->irelative_rel_ != NULL; }
3425
3426 // Return the number of PLT entries.
3427 unsigned int
3428 entry_count() const
3429 { return this->count_ + this->irelative_count_; }
3430
3431 // Return the offset of the first non-reserved PLT entry.
3432 unsigned int
3433 first_plt_entry_offset() const
3434 { return this->do_first_plt_entry_offset(); }
3435
3436 // Return the size of a PLT entry.
3437 unsigned int
3438 get_plt_entry_size() const
3439 { return this->do_get_plt_entry_size(); }
3440
3441 // Return the reserved tlsdesc entry size.
3442 unsigned int
3443 get_plt_tlsdesc_entry_size() const
3444 { return this->do_get_plt_tlsdesc_entry_size(); }
3445
3446 // Return the PLT address to use for a global symbol.
3447 uint64_t
3448 address_for_global(const Symbol*);
3449
3450 // Return the PLT address to use for a local symbol.
3451 uint64_t
3452 address_for_local(const Relobj*, unsigned int symndx);
3453
3454 protected:
3455 // Fill in the first PLT entry.
3456 void
3457 fill_first_plt_entry(unsigned char* pov,
3458 Address got_address,
3459 Address plt_address)
3460 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
3461
3462 // Fill in a normal PLT entry.
3463 void
3464 fill_plt_entry(unsigned char* pov,
3465 Address got_address,
3466 Address plt_address,
3467 unsigned int got_offset,
3468 unsigned int plt_offset)
3469 {
3470 this->do_fill_plt_entry(pov, got_address, plt_address,
3471 got_offset, plt_offset);
3472 }
3473
3474 // Fill in the reserved TLSDESC PLT entry.
3475 void
3476 fill_tlsdesc_entry(unsigned char* pov,
3477 Address gotplt_address,
3478 Address plt_address,
3479 Address got_base,
3480 unsigned int tlsdesc_got_offset,
3481 unsigned int plt_offset)
3482 {
3483 this->do_fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
3484 tlsdesc_got_offset, plt_offset);
3485 }
3486
3487 virtual unsigned int
3488 do_first_plt_entry_offset() const = 0;
3489
3490 virtual unsigned int
3491 do_get_plt_entry_size() const = 0;
3492
3493 virtual unsigned int
3494 do_get_plt_tlsdesc_entry_size() const = 0;
3495
3496 virtual void
3497 do_fill_first_plt_entry(unsigned char* pov,
3498 Address got_addr,
3499 Address plt_addr) = 0;
3500
3501 virtual void
3502 do_fill_plt_entry(unsigned char* pov,
3503 Address got_address,
3504 Address plt_address,
3505 unsigned int got_offset,
3506 unsigned int plt_offset) = 0;
3507
3508 virtual void
3509 do_fill_tlsdesc_entry(unsigned char* pov,
3510 Address gotplt_address,
3511 Address plt_address,
3512 Address got_base,
3513 unsigned int tlsdesc_got_offset,
3514 unsigned int plt_offset) = 0;
3515
3516 void
3517 do_adjust_output_section(Output_section* os);
3518
3519 // Write to a map file.
3520 void
3521 do_print_to_mapfile(Mapfile* mapfile) const
3522 { mapfile->print_output_data(this, _("** PLT")); }
3523
3524 private:
3525 // Set the final size.
3526 void
3527 set_final_data_size();
3528
3529 // Write out the PLT data.
3530 void
3531 do_write(Output_file*);
3532
3533 // The reloc section.
3534 Reloc_section* rel_;
3535
3536 // The TLSDESC relocs, if necessary. These must follow the regular
3537 // PLT relocs.
3538 Reloc_section* tlsdesc_rel_;
3539
3540 // The IRELATIVE relocs, if necessary. These must follow the
3541 // regular PLT relocations.
3542 Reloc_section* irelative_rel_;
3543
3544 // The .got section.
3545 Output_data_got_aarch64<size, big_endian>* got_;
3546
3547 // The .got.plt section.
3548 Output_data_space* got_plt_;
3549
3550 // The part of the .got.plt section used for IRELATIVE relocs.
3551 Output_data_space* got_irelative_;
3552
3553 // The number of PLT entries.
3554 unsigned int count_;
3555
3556 // Number of PLT entries with R_AARCH64_IRELATIVE relocs. These
3557 // follow the regular PLT entries.
3558 unsigned int irelative_count_;
3559
3560 // GOT offset of the reserved TLSDESC_GOT entry for the lazy trampoline.
3561 // Communicated to the loader via DT_TLSDESC_GOT. The magic value -1
3562 // indicates an offset is not allocated.
3563 unsigned int tlsdesc_got_offset_;
3564 };
3565
3566 // Initialize the PLT section.
3567
3568 template<int size, bool big_endian>
3569 void
3570 Output_data_plt_aarch64<size, big_endian>::init(Layout* layout)
3571 {
3572 this->rel_ = new Reloc_section(false);
3573 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3574 elfcpp::SHF_ALLOC, this->rel_,
3575 ORDER_DYNAMIC_PLT_RELOCS, false);
3576 }
3577
3578 template<int size, bool big_endian>
3579 void
3580 Output_data_plt_aarch64<size, big_endian>::do_adjust_output_section(
3581 Output_section* os)
3582 {
3583 os->set_entsize(this->get_plt_entry_size());
3584 }
3585
3586 // Add an entry to the PLT.
3587
3588 template<int size, bool big_endian>
3589 void
3590 Output_data_plt_aarch64<size, big_endian>::add_entry(Symbol_table* symtab,
3591 Layout* layout, Symbol* gsym)
3592 {
3593 gold_assert(!gsym->has_plt_offset());
3594
3595 unsigned int* pcount;
3596 unsigned int plt_reserved;
3597 Output_section_data_build* got;
3598
3599 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3600 && gsym->can_use_relative_reloc(false))
3601 {
3602 pcount = &this->irelative_count_;
3603 plt_reserved = 0;
3604 got = this->got_irelative_;
3605 }
3606 else
3607 {
3608 pcount = &this->count_;
3609 plt_reserved = this->first_plt_entry_offset();
3610 got = this->got_plt_;
3611 }
3612
3613 gsym->set_plt_offset((*pcount) * this->get_plt_entry_size()
3614 + plt_reserved);
3615
3616 ++*pcount;
3617
3618 section_offset_type got_offset = got->current_data_size();
3619
3620 // Every PLT entry needs a GOT entry which points back to the PLT
3621 // entry (this will be changed by the dynamic linker, normally
3622 // lazily when the function is called).
3623 got->set_current_data_size(got_offset + size / 8);
3624
3625 // Every PLT entry needs a reloc.
3626 this->add_relocation(symtab, layout, gsym, got_offset);
3627
3628 // Note that we don't need to save the symbol. The contents of the
3629 // PLT are independent of which symbols are used. The symbols only
3630 // appear in the relocations.
3631 }
3632
3633 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
3634 // the PLT offset.
3635
3636 template<int size, bool big_endian>
3637 unsigned int
3638 Output_data_plt_aarch64<size, big_endian>::add_local_ifunc_entry(
3639 Symbol_table* symtab,
3640 Layout* layout,
3641 Sized_relobj_file<size, big_endian>* relobj,
3642 unsigned int local_sym_index)
3643 {
3644 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
3645 ++this->irelative_count_;
3646
3647 section_offset_type got_offset = this->got_irelative_->current_data_size();
3648
3649 // Every PLT entry needs a GOT entry which points back to the PLT
3650 // entry.
3651 this->got_irelative_->set_current_data_size(got_offset + size / 8);
3652
3653 // Every PLT entry needs a reloc.
3654 Reloc_section* rela = this->rela_irelative(symtab, layout);
3655 rela->add_symbolless_local_addend(relobj, local_sym_index,
3656 elfcpp::R_AARCH64_IRELATIVE,
3657 this->got_irelative_, got_offset, 0);
3658
3659 return plt_offset;
3660 }
3661
3662 // Add the relocation for a PLT entry.
3663
3664 template<int size, bool big_endian>
3665 void
3666 Output_data_plt_aarch64<size, big_endian>::add_relocation(
3667 Symbol_table* symtab, Layout* layout, Symbol* gsym, unsigned int got_offset)
3668 {
3669 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3670 && gsym->can_use_relative_reloc(false))
3671 {
3672 Reloc_section* rela = this->rela_irelative(symtab, layout);
3673 rela->add_symbolless_global_addend(gsym, elfcpp::R_AARCH64_IRELATIVE,
3674 this->got_irelative_, got_offset, 0);
3675 }
3676 else
3677 {
3678 gsym->set_needs_dynsym_entry();
3679 this->rel_->add_global(gsym, elfcpp::R_AARCH64_JUMP_SLOT, this->got_plt_,
3680 got_offset, 0);
3681 }
3682 }
3683
3684 // Return where the TLSDESC relocations should go, creating it if
3685 // necessary. These follow the JUMP_SLOT relocations.
3686
3687 template<int size, bool big_endian>
3688 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
3689 Output_data_plt_aarch64<size, big_endian>::rela_tlsdesc(Layout* layout)
3690 {
3691 if (this->tlsdesc_rel_ == NULL)
3692 {
3693 this->tlsdesc_rel_ = new Reloc_section(false);
3694 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3695 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
3696 ORDER_DYNAMIC_PLT_RELOCS, false);
3697 gold_assert(this->tlsdesc_rel_->output_section()
3698 == this->rel_->output_section());
3699 }
3700 return this->tlsdesc_rel_;
3701 }
3702
3703 // Return where the IRELATIVE relocations should go in the PLT. These
3704 // follow the JUMP_SLOT and the TLSDESC relocations.
3705
3706 template<int size, bool big_endian>
3707 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
3708 Output_data_plt_aarch64<size, big_endian>::rela_irelative(Symbol_table* symtab,
3709 Layout* layout)
3710 {
3711 if (this->irelative_rel_ == NULL)
3712 {
3713 // Make sure we have a place for the TLSDESC relocations, in
3714 // case we see any later on.
3715 this->rela_tlsdesc(layout);
3716 this->irelative_rel_ = new Reloc_section(false);
3717 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3718 elfcpp::SHF_ALLOC, this->irelative_rel_,
3719 ORDER_DYNAMIC_PLT_RELOCS, false);
3720 gold_assert(this->irelative_rel_->output_section()
3721 == this->rel_->output_section());
3722
3723 if (parameters->doing_static_link())
3724 {
3725 // A statically linked executable will only have a .rela.plt
3726 // section to hold R_AARCH64_IRELATIVE relocs for
3727 // STT_GNU_IFUNC symbols. The library will use these
3728 // symbols to locate the IRELATIVE relocs at program startup
3729 // time.
3730 symtab->define_in_output_data("__rela_iplt_start", NULL,
3731 Symbol_table::PREDEFINED,
3732 this->irelative_rel_, 0, 0,
3733 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
3734 elfcpp::STV_HIDDEN, 0, false, true);
3735 symtab->define_in_output_data("__rela_iplt_end", NULL,
3736 Symbol_table::PREDEFINED,
3737 this->irelative_rel_, 0, 0,
3738 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
3739 elfcpp::STV_HIDDEN, 0, true, true);
3740 }
3741 }
3742 return this->irelative_rel_;
3743 }
3744
3745 // Return the PLT address to use for a global symbol.
3746
3747 template<int size, bool big_endian>
3748 uint64_t
3749 Output_data_plt_aarch64<size, big_endian>::address_for_global(
3750 const Symbol* gsym)
3751 {
3752 uint64_t offset = 0;
3753 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3754 && gsym->can_use_relative_reloc(false))
3755 offset = (this->first_plt_entry_offset() +
3756 this->count_ * this->get_plt_entry_size());
3757 return this->address() + offset + gsym->plt_offset();
3758 }
3759
3760 // Return the PLT address to use for a local symbol. These are always
3761 // IRELATIVE relocs.
3762
3763 template<int size, bool big_endian>
3764 uint64_t
3765 Output_data_plt_aarch64<size, big_endian>::address_for_local(
3766 const Relobj* object,
3767 unsigned int r_sym)
3768 {
3769 return (this->address()
3770 + this->first_plt_entry_offset()
3771 + this->count_ * this->get_plt_entry_size()
3772 + object->local_plt_offset(r_sym));
3773 }
3774
3775 // Set the final size.
3776
3777 template<int size, bool big_endian>
3778 void
3779 Output_data_plt_aarch64<size, big_endian>::set_final_data_size()
3780 {
3781 unsigned int count = this->count_ + this->irelative_count_;
3782 unsigned int extra_size = 0;
3783 if (this->has_tlsdesc_entry())
3784 extra_size += this->get_plt_tlsdesc_entry_size();
3785 this->set_data_size(this->first_plt_entry_offset()
3786 + count * this->get_plt_entry_size()
3787 + extra_size);
3788 }
3789
3790 template<int size, bool big_endian>
3791 class Output_data_plt_aarch64_standard :
3792 public Output_data_plt_aarch64<size, big_endian>
3793 {
3794 public:
3795 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3796 Output_data_plt_aarch64_standard(
3797 Layout* layout,
3798 Output_data_got_aarch64<size, big_endian>* got,
3799 Output_data_space* got_plt,
3800 Output_data_space* got_irelative)
3801 : Output_data_plt_aarch64<size, big_endian>(layout,
3802 size == 32 ? 4 : 8,
3803 got, got_plt,
3804 got_irelative)
3805 { }
3806
3807 protected:
3808 // Return the offset of the first non-reserved PLT entry.
3809 virtual unsigned int
3810 do_first_plt_entry_offset() const
3811 { return this->first_plt_entry_size; }
3812
3813 // Return the size of a PLT entry
3814 virtual unsigned int
3815 do_get_plt_entry_size() const
3816 { return this->plt_entry_size; }
3817
3818 // Return the size of a tlsdesc entry
3819 virtual unsigned int
3820 do_get_plt_tlsdesc_entry_size() const
3821 { return this->plt_tlsdesc_entry_size; }
3822
3823 virtual void
3824 do_fill_first_plt_entry(unsigned char* pov,
3825 Address got_address,
3826 Address plt_address);
3827
3828 virtual void
3829 do_fill_plt_entry(unsigned char* pov,
3830 Address got_address,
3831 Address plt_address,
3832 unsigned int got_offset,
3833 unsigned int plt_offset);
3834
3835 virtual void
3836 do_fill_tlsdesc_entry(unsigned char* pov,
3837 Address gotplt_address,
3838 Address plt_address,
3839 Address got_base,
3840 unsigned int tlsdesc_got_offset,
3841 unsigned int plt_offset);
3842
3843 private:
3844 // The size of the first plt entry size.
3845 static const int first_plt_entry_size = 32;
3846 // The size of the plt entry size.
3847 static const int plt_entry_size = 16;
3848 // The size of the plt tlsdesc entry size.
3849 static const int plt_tlsdesc_entry_size = 32;
3850 // Template for the first PLT entry.
3851 static const uint32_t first_plt_entry[first_plt_entry_size / 4];
3852 // Template for subsequent PLT entries.
3853 static const uint32_t plt_entry[plt_entry_size / 4];
3854 // The reserved TLSDESC entry in the PLT for an executable.
3855 static const uint32_t tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4];
3856 };
3857
3858 // The first entry in the PLT for an executable.
3859
3860 template<>
3861 const uint32_t
3862 Output_data_plt_aarch64_standard<32, false>::
3863 first_plt_entry[first_plt_entry_size / 4] =
3864 {
3865 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
3866 0x90000010, /* adrp x16, PLT_GOT+0x8 */
3867 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
3868 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
3869 0xd61f0220, /* br x17 */
3870 0xd503201f, /* nop */
3871 0xd503201f, /* nop */
3872 0xd503201f, /* nop */
3873 };
3874
3875
3876 template<>
3877 const uint32_t
3878 Output_data_plt_aarch64_standard<32, true>::
3879 first_plt_entry[first_plt_entry_size / 4] =
3880 {
3881 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
3882 0x90000010, /* adrp x16, PLT_GOT+0x8 */
3883 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
3884 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
3885 0xd61f0220, /* br x17 */
3886 0xd503201f, /* nop */
3887 0xd503201f, /* nop */
3888 0xd503201f, /* nop */
3889 };
3890
3891
3892 template<>
3893 const uint32_t
3894 Output_data_plt_aarch64_standard<64, false>::
3895 first_plt_entry[first_plt_entry_size / 4] =
3896 {
3897 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
3898 0x90000010, /* adrp x16, PLT_GOT+16 */
3899 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
3900 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
3901 0xd61f0220, /* br x17 */
3902 0xd503201f, /* nop */
3903 0xd503201f, /* nop */
3904 0xd503201f, /* nop */
3905 };
3906
3907
3908 template<>
3909 const uint32_t
3910 Output_data_plt_aarch64_standard<64, true>::
3911 first_plt_entry[first_plt_entry_size / 4] =
3912 {
3913 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
3914 0x90000010, /* adrp x16, PLT_GOT+16 */
3915 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
3916 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
3917 0xd61f0220, /* br x17 */
3918 0xd503201f, /* nop */
3919 0xd503201f, /* nop */
3920 0xd503201f, /* nop */
3921 };
3922
3923
3924 template<>
3925 const uint32_t
3926 Output_data_plt_aarch64_standard<32, false>::
3927 plt_entry[plt_entry_size / 4] =
3928 {
3929 0x90000010, /* adrp x16, PLTGOT + n * 4 */
3930 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
3931 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
3932 0xd61f0220, /* br x17. */
3933 };
3934
3935
3936 template<>
3937 const uint32_t
3938 Output_data_plt_aarch64_standard<32, true>::
3939 plt_entry[plt_entry_size / 4] =
3940 {
3941 0x90000010, /* adrp x16, PLTGOT + n * 4 */
3942 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
3943 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
3944 0xd61f0220, /* br x17. */
3945 };
3946
3947
3948 template<>
3949 const uint32_t
3950 Output_data_plt_aarch64_standard<64, false>::
3951 plt_entry[plt_entry_size / 4] =
3952 {
3953 0x90000010, /* adrp x16, PLTGOT + n * 8 */
3954 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
3955 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
3956 0xd61f0220, /* br x17. */
3957 };
3958
3959
3960 template<>
3961 const uint32_t
3962 Output_data_plt_aarch64_standard<64, true>::
3963 plt_entry[plt_entry_size / 4] =
3964 {
3965 0x90000010, /* adrp x16, PLTGOT + n * 8 */
3966 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
3967 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
3968 0xd61f0220, /* br x17. */
3969 };
3970
3971
3972 template<int size, bool big_endian>
3973 void
3974 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_first_plt_entry(
3975 unsigned char* pov,
3976 Address got_address,
3977 Address plt_address)
3978 {
3979 // PLT0 of the small PLT looks like this in ELF64 -
3980 // stp x16, x30, [sp, #-16]! Save the reloc and lr on stack.
3981 // adrp x16, PLT_GOT + 16 Get the page base of the GOTPLT
3982 // ldr x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
3983 // symbol resolver
3984 // add x16, x16, #:lo12:PLT_GOT+16 Load the lo12 bits of the
3985 // GOTPLT entry for this.
3986 // br x17
3987 // PLT0 will be slightly different in ELF32 due to different got entry
3988 // size.
3989 memcpy(pov, this->first_plt_entry, this->first_plt_entry_size);
3990 Address gotplt_2nd_ent = got_address + (size / 8) * 2;
3991
3992 // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
3993 // ADRP: (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
3994 // FIXME: This only works for 64bit
3995 AArch64_relocate_functions<size, big_endian>::adrp(pov + 4,
3996 gotplt_2nd_ent, plt_address + 4);
3997
3998 // Fill in R_AARCH64_LDST8_LO12
3999 elfcpp::Swap<32, big_endian>::writeval(
4000 pov + 8,
4001 ((this->first_plt_entry[2] & 0xffc003ff)
4002 | ((gotplt_2nd_ent & 0xff8) << 7)));
4003
4004 // Fill in R_AARCH64_ADD_ABS_LO12
4005 elfcpp::Swap<32, big_endian>::writeval(
4006 pov + 12,
4007 ((this->first_plt_entry[3] & 0xffc003ff)
4008 | ((gotplt_2nd_ent & 0xfff) << 10)));
4009 }
4010
4011
4012 // Subsequent entries in the PLT for an executable.
4013 // FIXME: This only works for 64bit
4014
4015 template<int size, bool big_endian>
4016 void
4017 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_plt_entry(
4018 unsigned char* pov,
4019 Address got_address,
4020 Address plt_address,
4021 unsigned int got_offset,
4022 unsigned int plt_offset)
4023 {
4024 memcpy(pov, this->plt_entry, this->plt_entry_size);
4025
4026 Address gotplt_entry_address = got_address + got_offset;
4027 Address plt_entry_address = plt_address + plt_offset;
4028
4029 // Fill in R_AARCH64_PCREL_ADR_HI21
4030 AArch64_relocate_functions<size, big_endian>::adrp(
4031 pov,
4032 gotplt_entry_address,
4033 plt_entry_address);
4034
4035 // Fill in R_AARCH64_LDST64_ABS_LO12
4036 elfcpp::Swap<32, big_endian>::writeval(
4037 pov + 4,
4038 ((this->plt_entry[1] & 0xffc003ff)
4039 | ((gotplt_entry_address & 0xff8) << 7)));
4040
4041 // Fill in R_AARCH64_ADD_ABS_LO12
4042 elfcpp::Swap<32, big_endian>::writeval(
4043 pov + 8,
4044 ((this->plt_entry[2] & 0xffc003ff)
4045 | ((gotplt_entry_address & 0xfff) <<10)));
4046
4047 }
4048
4049
4050 template<>
4051 const uint32_t
4052 Output_data_plt_aarch64_standard<32, false>::
4053 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4054 {
4055 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4056 0x90000002, /* adrp x2, 0 */
4057 0x90000003, /* adrp x3, 0 */
4058 0xb9400042, /* ldr w2, [w2, #0] */
4059 0x11000063, /* add w3, w3, 0 */
4060 0xd61f0040, /* br x2 */
4061 0xd503201f, /* nop */
4062 0xd503201f, /* nop */
4063 };
4064
4065 template<>
4066 const uint32_t
4067 Output_data_plt_aarch64_standard<32, true>::
4068 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4069 {
4070 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4071 0x90000002, /* adrp x2, 0 */
4072 0x90000003, /* adrp x3, 0 */
4073 0xb9400042, /* ldr w2, [w2, #0] */
4074 0x11000063, /* add w3, w3, 0 */
4075 0xd61f0040, /* br x2 */
4076 0xd503201f, /* nop */
4077 0xd503201f, /* nop */
4078 };
4079
4080 template<>
4081 const uint32_t
4082 Output_data_plt_aarch64_standard<64, false>::
4083 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4084 {
4085 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4086 0x90000002, /* adrp x2, 0 */
4087 0x90000003, /* adrp x3, 0 */
4088 0xf9400042, /* ldr x2, [x2, #0] */
4089 0x91000063, /* add x3, x3, 0 */
4090 0xd61f0040, /* br x2 */
4091 0xd503201f, /* nop */
4092 0xd503201f, /* nop */
4093 };
4094
4095 template<>
4096 const uint32_t
4097 Output_data_plt_aarch64_standard<64, true>::
4098 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4099 {
4100 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4101 0x90000002, /* adrp x2, 0 */
4102 0x90000003, /* adrp x3, 0 */
4103 0xf9400042, /* ldr x2, [x2, #0] */
4104 0x91000063, /* add x3, x3, 0 */
4105 0xd61f0040, /* br x2 */
4106 0xd503201f, /* nop */
4107 0xd503201f, /* nop */
4108 };
4109
4110 template<int size, bool big_endian>
4111 void
4112 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_tlsdesc_entry(
4113 unsigned char* pov,
4114 Address gotplt_address,
4115 Address plt_address,
4116 Address got_base,
4117 unsigned int tlsdesc_got_offset,
4118 unsigned int plt_offset)
4119 {
4120 memcpy(pov, tlsdesc_plt_entry, plt_tlsdesc_entry_size);
4121
4122 // move DT_TLSDESC_GOT address into x2
4123 // move .got.plt address into x3
4124 Address tlsdesc_got_entry = got_base + tlsdesc_got_offset;
4125 Address plt_entry_address = plt_address + plt_offset;
4126
4127 // R_AARCH64_ADR_PREL_PG_HI21
4128 AArch64_relocate_functions<size, big_endian>::adrp(
4129 pov + 4,
4130 tlsdesc_got_entry,
4131 plt_entry_address + 4);
4132
4133 // R_AARCH64_ADR_PREL_PG_HI21
4134 AArch64_relocate_functions<size, big_endian>::adrp(
4135 pov + 8,
4136 gotplt_address,
4137 plt_entry_address + 8);
4138
4139 // R_AARCH64_LDST64_ABS_LO12
4140 elfcpp::Swap<32, big_endian>::writeval(
4141 pov + 12,
4142 ((this->tlsdesc_plt_entry[3] & 0xffc003ff)
4143 | ((tlsdesc_got_entry & 0xff8) << 7)));
4144
4145 // R_AARCH64_ADD_ABS_LO12
4146 elfcpp::Swap<32, big_endian>::writeval(
4147 pov + 16,
4148 ((this->tlsdesc_plt_entry[4] & 0xffc003ff)
4149 | ((gotplt_address & 0xfff) << 10)));
4150 }
4151
4152 // Write out the PLT. This uses the hand-coded instructions above,
4153 // and adjusts them as needed. This is specified by the AMD64 ABI.
4154
4155 template<int size, bool big_endian>
4156 void
4157 Output_data_plt_aarch64<size, big_endian>::do_write(Output_file* of)
4158 {
4159 const off_t offset = this->offset();
4160 const section_size_type oview_size =
4161 convert_to_section_size_type(this->data_size());
4162 unsigned char* const oview = of->get_output_view(offset, oview_size);
4163
4164 const off_t got_file_offset = this->got_plt_->offset();
4165 gold_assert(got_file_offset + this->got_plt_->data_size()
4166 == this->got_irelative_->offset());
4167
4168 const section_size_type got_size =
4169 convert_to_section_size_type(this->got_plt_->data_size()
4170 + this->got_irelative_->data_size());
4171 unsigned char* const got_view = of->get_output_view(got_file_offset,
4172 got_size);
4173
4174 unsigned char* pov = oview;
4175
4176 // The base address of the .plt section.
4177 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
4178 // The base address of the PLT portion of the .got section.
4179 typename elfcpp::Elf_types<size>::Elf_Addr gotplt_address
4180 = this->got_plt_->address();
4181
4182 this->fill_first_plt_entry(pov, gotplt_address, plt_address);
4183 pov += this->first_plt_entry_offset();
4184
4185 // The first three entries in .got.plt are reserved.
4186 unsigned char* got_pov = got_view;
4187 memset(got_pov, 0, size / 8 * AARCH64_GOTPLT_RESERVE_COUNT);
4188 got_pov += (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
4189
4190 unsigned int plt_offset = this->first_plt_entry_offset();
4191 unsigned int got_offset = (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
4192 const unsigned int count = this->count_ + this->irelative_count_;
4193 for (unsigned int plt_index = 0;
4194 plt_index < count;
4195 ++plt_index,
4196 pov += this->get_plt_entry_size(),
4197 got_pov += size / 8,
4198 plt_offset += this->get_plt_entry_size(),
4199 got_offset += size / 8)
4200 {
4201 // Set and adjust the PLT entry itself.
4202 this->fill_plt_entry(pov, gotplt_address, plt_address,
4203 got_offset, plt_offset);
4204
4205 // Set the entry in the GOT, which points to plt0.
4206 elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
4207 }
4208
4209 if (this->has_tlsdesc_entry())
4210 {
4211 // Set and adjust the reserved TLSDESC PLT entry.
4212 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
4213 // The base address of the .base section.
4214 typename elfcpp::Elf_types<size>::Elf_Addr got_base =
4215 this->got_->address();
4216 this->fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
4217 tlsdesc_got_offset, plt_offset);
4218 pov += this->get_plt_tlsdesc_entry_size();
4219 }
4220
4221 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
4222 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
4223
4224 of->write_output_view(offset, oview_size, oview);
4225 of->write_output_view(got_file_offset, got_size, got_view);
4226 }
4227
4228 // Telling how to update the immediate field of an instruction.
4229 struct AArch64_howto
4230 {
4231 // The immediate field mask.
4232 elfcpp::Elf_Xword dst_mask;
4233
4234 // The offset to apply relocation immediate
4235 int doffset;
4236
4237 // The second part offset, if the immediate field has two parts.
4238 // -1 if the immediate field has only one part.
4239 int doffset2;
4240 };
4241
4242 static const AArch64_howto aarch64_howto[AArch64_reloc_property::INST_NUM] =
4243 {
4244 {0, -1, -1}, // DATA
4245 {0x1fffe0, 5, -1}, // MOVW [20:5]-imm16
4246 {0xffffe0, 5, -1}, // LD [23:5]-imm19
4247 {0x60ffffe0, 29, 5}, // ADR [30:29]-immlo [23:5]-immhi
4248 {0x60ffffe0, 29, 5}, // ADRP [30:29]-immlo [23:5]-immhi
4249 {0x3ffc00, 10, -1}, // ADD [21:10]-imm12
4250 {0x3ffc00, 10, -1}, // LDST [21:10]-imm12
4251 {0x7ffe0, 5, -1}, // TBZNZ [18:5]-imm14
4252 {0xffffe0, 5, -1}, // CONDB [23:5]-imm19
4253 {0x3ffffff, 0, -1}, // B [25:0]-imm26
4254 {0x3ffffff, 0, -1}, // CALL [25:0]-imm26
4255 };
4256
4257 // AArch64 relocate function class
4258
4259 template<int size, bool big_endian>
4260 class AArch64_relocate_functions
4261 {
4262 public:
4263 typedef enum
4264 {
4265 STATUS_OKAY, // No error during relocation.
4266 STATUS_OVERFLOW, // Relocation overflow.
4267 STATUS_BAD_RELOC, // Relocation cannot be applied.
4268 } Status;
4269
4270 typedef AArch64_relocate_functions<size, big_endian> This;
4271 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4272 typedef Relocate_info<size, big_endian> The_relocate_info;
4273 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
4274 typedef Reloc_stub<size, big_endian> The_reloc_stub;
4275 typedef typename The_reloc_stub::Stub_type The_reloc_stub_type;
4276 typedef Stub_table<size, big_endian> The_stub_table;
4277 typedef elfcpp::Rela<size, big_endian> The_rela;
4278 typedef typename elfcpp::Swap<size, big_endian>::Valtype AArch64_valtype;
4279
4280 // Return the page address of the address.
4281 // Page(address) = address & ~0xFFF
4282
4283 static inline AArch64_valtype
4284 Page(Address address)
4285 {
4286 return (address & (~static_cast<Address>(0xFFF)));
4287 }
4288
4289 private:
4290 // Update instruction (pointed by view) with selected bits (immed).
4291 // val = (val & ~dst_mask) | (immed << doffset)
4292
4293 template<int valsize>
4294 static inline void
4295 update_view(unsigned char* view,
4296 AArch64_valtype immed,
4297 elfcpp::Elf_Xword doffset,
4298 elfcpp::Elf_Xword dst_mask)
4299 {
4300 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4301 Valtype* wv = reinterpret_cast<Valtype*>(view);
4302 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
4303
4304 // Clear immediate fields.
4305 val &= ~dst_mask;
4306 elfcpp::Swap<valsize, big_endian>::writeval(wv,
4307 static_cast<Valtype>(val | (immed << doffset)));
4308 }
4309
4310 // Update two parts of an instruction (pointed by view) with selected
4311 // bits (immed1 and immed2).
4312 // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
4313
4314 template<int valsize>
4315 static inline void
4316 update_view_two_parts(
4317 unsigned char* view,
4318 AArch64_valtype immed1,
4319 AArch64_valtype immed2,
4320 elfcpp::Elf_Xword doffset1,
4321 elfcpp::Elf_Xword doffset2,
4322 elfcpp::Elf_Xword dst_mask)
4323 {
4324 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4325 Valtype* wv = reinterpret_cast<Valtype*>(view);
4326 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
4327 val &= ~dst_mask;
4328 elfcpp::Swap<valsize, big_endian>::writeval(wv,
4329 static_cast<Valtype>(val | (immed1 << doffset1) |
4330 (immed2 << doffset2)));
4331 }
4332
4333 // Update adr or adrp instruction with immed.
4334 // In adr and adrp: [30:29] immlo [23:5] immhi
4335
4336 static inline void
4337 update_adr(unsigned char* view, AArch64_valtype immed)
4338 {
4339 elfcpp::Elf_Xword dst_mask = (0x3 << 29) | (0x7ffff << 5);
4340 This::template update_view_two_parts<32>(
4341 view,
4342 immed & 0x3,
4343 (immed & 0x1ffffc) >> 2,
4344 29,
4345 5,
4346 dst_mask);
4347 }
4348
4349 // Update movz/movn instruction with bits immed.
4350 // Set instruction to movz if is_movz is true, otherwise set instruction
4351 // to movn.
4352
4353 static inline void
4354 update_movnz(unsigned char* view,
4355 AArch64_valtype immed,
4356 bool is_movz)
4357 {
4358 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4359 Valtype* wv = reinterpret_cast<Valtype*>(view);
4360 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
4361
4362 const elfcpp::Elf_Xword doffset =
4363 aarch64_howto[AArch64_reloc_property::INST_MOVW].doffset;
4364 const elfcpp::Elf_Xword dst_mask =
4365 aarch64_howto[AArch64_reloc_property::INST_MOVW].dst_mask;
4366
4367 // Clear immediate fields and opc code.
4368 val &= ~(dst_mask | (0x3 << 29));
4369
4370 // Set instruction to movz or movn.
4371 // movz: [30:29] is 10 movn: [30:29] is 00
4372 if (is_movz)
4373 val |= (0x2 << 29);
4374
4375 elfcpp::Swap<32, big_endian>::writeval(wv,
4376 static_cast<Valtype>(val | (immed << doffset)));
4377 }
4378
4379 // Update selected bits in text.
4380
4381 template<int valsize>
4382 static inline typename This::Status
4383 reloc_common(unsigned char* view, Address x,
4384 const AArch64_reloc_property* reloc_property)
4385 {
4386 // Select bits from X.
4387 Address immed = reloc_property->select_x_value(x);
4388
4389 // Update view.
4390 const AArch64_reloc_property::Reloc_inst inst =
4391 reloc_property->reloc_inst();
4392 // If it is a data relocation or instruction has 2 parts of immediate
4393 // fields, you should not call pcrela_general.
4394 gold_assert(aarch64_howto[inst].doffset2 == -1 &&
4395 aarch64_howto[inst].doffset != -1);
4396 This::template update_view<valsize>(view, immed,
4397 aarch64_howto[inst].doffset,
4398 aarch64_howto[inst].dst_mask);
4399
4400 // Do check overflow or alignment if needed.
4401 return (reloc_property->checkup_x_value(x)
4402 ? This::STATUS_OKAY
4403 : This::STATUS_OVERFLOW);
4404 }
4405
4406 public:
4407
4408 // Do a simple rela relocation at unaligned addresses.
4409
4410 template<int valsize>
4411 static inline typename This::Status
4412 rela_ua(unsigned char* view,
4413 const Sized_relobj_file<size, big_endian>* object,
4414 const Symbol_value<size>* psymval,
4415 AArch64_valtype addend,
4416 const AArch64_reloc_property* reloc_property)
4417 {
4418 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
4419 Valtype;
4420 typename elfcpp::Elf_types<size>::Elf_Addr x =
4421 psymval->value(object, addend);
4422 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
4423 static_cast<Valtype>(x));
4424 return (reloc_property->checkup_x_value(x)
4425 ? This::STATUS_OKAY
4426 : This::STATUS_OVERFLOW);
4427 }
4428
4429 // Do a simple pc-relative relocation at unaligned addresses.
4430
4431 template<int valsize>
4432 static inline typename This::Status
4433 pcrela_ua(unsigned char* view,
4434 const Sized_relobj_file<size, big_endian>* object,
4435 const Symbol_value<size>* psymval,
4436 AArch64_valtype addend,
4437 Address address,
4438 const AArch64_reloc_property* reloc_property)
4439 {
4440 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
4441 Valtype;
4442 Address x = psymval->value(object, addend) - address;
4443 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
4444 static_cast<Valtype>(x));
4445 return (reloc_property->checkup_x_value(x)
4446 ? This::STATUS_OKAY
4447 : This::STATUS_OVERFLOW);
4448 }
4449
4450 // Do a simple rela relocation at aligned addresses.
4451
4452 template<int valsize>
4453 static inline typename This::Status
4454 rela(
4455 unsigned char* view,
4456 const Sized_relobj_file<size, big_endian>* object,
4457 const Symbol_value<size>* psymval,
4458 AArch64_valtype addend,
4459 const AArch64_reloc_property* reloc_property)
4460 {
4461 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4462 Valtype* wv = reinterpret_cast<Valtype*>(view);
4463 Address x = psymval->value(object, addend);
4464 elfcpp::Swap<valsize, big_endian>::writeval(wv,static_cast<Valtype>(x));
4465 return (reloc_property->checkup_x_value(x)
4466 ? This::STATUS_OKAY
4467 : This::STATUS_OVERFLOW);
4468 }
4469
4470 // Do relocate. Update selected bits in text.
4471 // new_val = (val & ~dst_mask) | (immed << doffset)
4472
4473 template<int valsize>
4474 static inline typename This::Status
4475 rela_general(unsigned char* view,
4476 const Sized_relobj_file<size, big_endian>* object,
4477 const Symbol_value<size>* psymval,
4478 AArch64_valtype addend,
4479 const AArch64_reloc_property* reloc_property)
4480 {
4481 // Calculate relocation.
4482 Address x = psymval->value(object, addend);
4483 return This::template reloc_common<valsize>(view, x, reloc_property);
4484 }
4485
4486 // Do relocate. Update selected bits in text.
4487 // new val = (val & ~dst_mask) | (immed << doffset)
4488
4489 template<int valsize>
4490 static inline typename This::Status
4491 rela_general(
4492 unsigned char* view,
4493 AArch64_valtype s,
4494 AArch64_valtype addend,
4495 const AArch64_reloc_property* reloc_property)
4496 {
4497 // Calculate relocation.
4498 Address x = s + addend;
4499 return This::template reloc_common<valsize>(view, x, reloc_property);
4500 }
4501
4502 // Do address relative relocate. Update selected bits in text.
4503 // new val = (val & ~dst_mask) | (immed << doffset)
4504
4505 template<int valsize>
4506 static inline typename This::Status
4507 pcrela_general(
4508 unsigned char* view,
4509 const Sized_relobj_file<size, big_endian>* object,
4510 const Symbol_value<size>* psymval,
4511 AArch64_valtype addend,
4512 Address address,
4513 const AArch64_reloc_property* reloc_property)
4514 {
4515 // Calculate relocation.
4516 Address x = psymval->value(object, addend) - address;
4517 return This::template reloc_common<valsize>(view, x, reloc_property);
4518 }
4519
4520
4521 // Calculate (S + A) - address, update adr instruction.
4522
4523 static inline typename This::Status
4524 adr(unsigned char* view,
4525 const Sized_relobj_file<size, big_endian>* object,
4526 const Symbol_value<size>* psymval,
4527 Address addend,
4528 Address address,
4529 const AArch64_reloc_property* /* reloc_property */)
4530 {
4531 AArch64_valtype x = psymval->value(object, addend) - address;
4532 // Pick bits [20:0] of X.
4533 AArch64_valtype immed = x & 0x1fffff;
4534 update_adr(view, immed);
4535 // Check -2^20 <= X < 2^20
4536 return (size == 64 && Bits<21>::has_overflow((x))
4537 ? This::STATUS_OVERFLOW
4538 : This::STATUS_OKAY);
4539 }
4540
4541 // Calculate PG(S+A) - PG(address), update adrp instruction.
4542 // R_AARCH64_ADR_PREL_PG_HI21
4543
4544 static inline typename This::Status
4545 adrp(
4546 unsigned char* view,
4547 Address sa,
4548 Address address)
4549 {
4550 AArch64_valtype x = This::Page(sa) - This::Page(address);
4551 // Pick [32:12] of X.
4552 AArch64_valtype immed = (x >> 12) & 0x1fffff;
4553 update_adr(view, immed);
4554 // Check -2^32 <= X < 2^32
4555 return (size == 64 && Bits<33>::has_overflow((x))
4556 ? This::STATUS_OVERFLOW
4557 : This::STATUS_OKAY);
4558 }
4559
4560 // Calculate PG(S+A) - PG(address), update adrp instruction.
4561 // R_AARCH64_ADR_PREL_PG_HI21
4562
4563 static inline typename This::Status
4564 adrp(unsigned char* view,
4565 const Sized_relobj_file<size, big_endian>* object,
4566 const Symbol_value<size>* psymval,
4567 Address addend,
4568 Address address,
4569 const AArch64_reloc_property* reloc_property)
4570 {
4571 Address sa = psymval->value(object, addend);
4572 AArch64_valtype x = This::Page(sa) - This::Page(address);
4573 // Pick [32:12] of X.
4574 AArch64_valtype immed = (x >> 12) & 0x1fffff;
4575 update_adr(view, immed);
4576 return (reloc_property->checkup_x_value(x)
4577 ? This::STATUS_OKAY
4578 : This::STATUS_OVERFLOW);
4579 }
4580
4581 // Update mov[n/z] instruction. Check overflow if needed.
4582 // If X >=0, set the instruction to movz and its immediate value to the
4583 // selected bits S.
4584 // If X < 0, set the instruction to movn and its immediate value to
4585 // NOT (selected bits of).
4586
4587 static inline typename This::Status
4588 movnz(unsigned char* view,
4589 AArch64_valtype x,
4590 const AArch64_reloc_property* reloc_property)
4591 {
4592 // Select bits from X.
4593 Address immed;
4594 bool is_movz;
4595 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedW;
4596 if (static_cast<SignedW>(x) >= 0)
4597 {
4598 immed = reloc_property->select_x_value(x);
4599 is_movz = true;
4600 }
4601 else
4602 {
4603 immed = reloc_property->select_x_value(~x);;
4604 is_movz = false;
4605 }
4606
4607 // Update movnz instruction.
4608 update_movnz(view, immed, is_movz);
4609
4610 // Do check overflow or alignment if needed.
4611 return (reloc_property->checkup_x_value(x)
4612 ? This::STATUS_OKAY
4613 : This::STATUS_OVERFLOW);
4614 }
4615
4616 static inline bool
4617 maybe_apply_stub(unsigned int,
4618 const The_relocate_info*,
4619 const The_rela&,
4620 unsigned char*,
4621 Address,
4622 const Sized_symbol<size>*,
4623 const Symbol_value<size>*,
4624 const Sized_relobj_file<size, big_endian>*,
4625 section_size_type);
4626
4627 }; // End of AArch64_relocate_functions
4628
4629
4630 // For a certain relocation type (usually jump/branch), test to see if the
4631 // destination needs a stub to fulfil. If so, re-route the destination of the
4632 // original instruction to the stub, note, at this time, the stub has already
4633 // been generated.
4634
4635 template<int size, bool big_endian>
4636 bool
4637 AArch64_relocate_functions<size, big_endian>::
4638 maybe_apply_stub(unsigned int r_type,
4639 const The_relocate_info* relinfo,
4640 const The_rela& rela,
4641 unsigned char* view,
4642 Address address,
4643 const Sized_symbol<size>* gsym,
4644 const Symbol_value<size>* psymval,
4645 const Sized_relobj_file<size, big_endian>* object,
4646 section_size_type current_group_size)
4647 {
4648 if (parameters->options().relocatable())
4649 return false;
4650
4651 typename elfcpp::Elf_types<size>::Elf_Swxword addend = rela.get_r_addend();
4652 Address branch_target = psymval->value(object, 0) + addend;
4653 The_reloc_stub_type stub_type = The_reloc_stub::
4654 stub_type_for_reloc(r_type, address, branch_target);
4655 if (stub_type == The_reloc_stub::ST_NONE)
4656 return false;
4657
4658 const The_aarch64_relobj* aarch64_relobj =
4659 static_cast<const The_aarch64_relobj*>(object);
4660 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
4661 gold_assert(stub_table != NULL);
4662
4663 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4664 typename The_reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
4665 The_reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
4666 gold_assert(stub != NULL);
4667
4668 Address new_branch_target = stub_table->address() + stub->offset();
4669 typename elfcpp::Swap<size, big_endian>::Valtype branch_offset =
4670 new_branch_target - address;
4671 const AArch64_reloc_property* arp =
4672 aarch64_reloc_property_table->get_reloc_property(r_type);
4673 gold_assert(arp != NULL);
4674 typename This::Status status = This::template
4675 rela_general<32>(view, branch_offset, 0, arp);
4676 if (status != This::STATUS_OKAY)
4677 gold_error(_("Stub is too far away, try a smaller value "
4678 "for '--stub-group-size'. The current value is 0x%lx."),
4679 static_cast<unsigned long>(current_group_size));
4680 return true;
4681 }
4682
4683
4684 // Group input sections for stub generation.
4685 //
4686 // We group input sections in an output section so that the total size,
4687 // including any padding space due to alignment is smaller than GROUP_SIZE
4688 // unless the only input section in group is bigger than GROUP_SIZE already.
4689 // Then an ARM stub table is created to follow the last input section
4690 // in group. For each group an ARM stub table is created an is placed
4691 // after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
4692 // extend the group after the stub table.
4693
4694 template<int size, bool big_endian>
4695 void
4696 Target_aarch64<size, big_endian>::group_sections(
4697 Layout* layout,
4698 section_size_type group_size,
4699 bool stubs_always_after_branch,
4700 const Task* task)
4701 {
4702 // Group input sections and insert stub table
4703 Layout::Section_list section_list;
4704 layout->get_executable_sections(&section_list);
4705 for (Layout::Section_list::const_iterator p = section_list.begin();
4706 p != section_list.end();
4707 ++p)
4708 {
4709 AArch64_output_section<size, big_endian>* output_section =
4710 static_cast<AArch64_output_section<size, big_endian>*>(*p);
4711 output_section->group_sections(group_size, stubs_always_after_branch,
4712 this, task);
4713 }
4714 }
4715
4716
4717 // Find the AArch64_input_section object corresponding to the SHNDX-th input
4718 // section of RELOBJ.
4719
4720 template<int size, bool big_endian>
4721 AArch64_input_section<size, big_endian>*
4722 Target_aarch64<size, big_endian>::find_aarch64_input_section(
4723 Relobj* relobj, unsigned int shndx) const
4724 {
4725 Section_id sid(relobj, shndx);
4726 typename AArch64_input_section_map::const_iterator p =
4727 this->aarch64_input_section_map_.find(sid);
4728 return (p != this->aarch64_input_section_map_.end()) ? p->second : NULL;
4729 }
4730
4731
4732 // Make a new AArch64_input_section object.
4733
4734 template<int size, bool big_endian>
4735 AArch64_input_section<size, big_endian>*
4736 Target_aarch64<size, big_endian>::new_aarch64_input_section(
4737 Relobj* relobj, unsigned int shndx)
4738 {
4739 Section_id sid(relobj, shndx);
4740
4741 AArch64_input_section<size, big_endian>* input_section =
4742 new AArch64_input_section<size, big_endian>(relobj, shndx);
4743 input_section->init();
4744
4745 // Register new AArch64_input_section in map for look-up.
4746 std::pair<typename AArch64_input_section_map::iterator,bool> ins =
4747 this->aarch64_input_section_map_.insert(
4748 std::make_pair(sid, input_section));
4749
4750 // Make sure that it we have not created another AArch64_input_section
4751 // for this input section already.
4752 gold_assert(ins.second);
4753
4754 return input_section;
4755 }
4756
4757
4758 // Relaxation hook. This is where we do stub generation.
4759
4760 template<int size, bool big_endian>
4761 bool
4762 Target_aarch64<size, big_endian>::do_relax(
4763 int pass,
4764 const Input_objects* input_objects,
4765 Symbol_table* symtab,
4766 Layout* layout ,
4767 const Task* task)
4768 {
4769 gold_assert(!parameters->options().relocatable());
4770 if (pass == 1)
4771 {
4772 // We don't handle negative stub_group_size right now.
4773 this->stub_group_size_ = abs(parameters->options().stub_group_size());
4774 if (this->stub_group_size_ == 1)
4775 {
4776 // Leave room for 4096 4-byte stub entries. If we exceed that, then we
4777 // will fail to link. The user will have to relink with an explicit
4778 // group size option.
4779 this->stub_group_size_ = The_reloc_stub::MAX_BRANCH_OFFSET -
4780 4096 * 4;
4781 }
4782 group_sections(layout, this->stub_group_size_, true, task);
4783 }
4784 else
4785 {
4786 // If this is not the first pass, addresses and file offsets have
4787 // been reset at this point, set them here.
4788 for (Stub_table_iterator sp = this->stub_tables_.begin();
4789 sp != this->stub_tables_.end(); ++sp)
4790 {
4791 The_stub_table* stt = *sp;
4792 The_aarch64_input_section* owner = stt->owner();
4793 off_t off = align_address(owner->original_size(),
4794 stt->addralign());
4795 stt->set_address_and_file_offset(owner->address() + off,
4796 owner->offset() + off);
4797 }
4798 }
4799
4800 // Scan relocs for relocation stubs
4801 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
4802 op != input_objects->relobj_end();
4803 ++op)
4804 {
4805 The_aarch64_relobj* aarch64_relobj =
4806 static_cast<The_aarch64_relobj*>(*op);
4807 // Lock the object so we can read from it. This is only called
4808 // single-threaded from Layout::finalize, so it is OK to lock.
4809 Task_lock_obj<Object> tl(task, aarch64_relobj);
4810 aarch64_relobj->scan_sections_for_stubs(this, symtab, layout);
4811 }
4812
4813 bool any_stub_table_changed = false;
4814 for (Stub_table_iterator siter = this->stub_tables_.begin();
4815 siter != this->stub_tables_.end() && !any_stub_table_changed; ++siter)
4816 {
4817 The_stub_table* stub_table = *siter;
4818 if (stub_table->update_data_size_changed_p())
4819 {
4820 The_aarch64_input_section* owner = stub_table->owner();
4821 uint64_t address = owner->address();
4822 off_t offset = owner->offset();
4823 owner->reset_address_and_file_offset();
4824 owner->set_address_and_file_offset(address, offset);
4825
4826 any_stub_table_changed = true;
4827 }
4828 }
4829
4830 // Do not continue relaxation.
4831 bool continue_relaxation = any_stub_table_changed;
4832 if (!continue_relaxation)
4833 for (Stub_table_iterator sp = this->stub_tables_.begin();
4834 (sp != this->stub_tables_.end());
4835 ++sp)
4836 (*sp)->finalize_stubs();
4837
4838 return continue_relaxation;
4839 }
4840
4841
4842 // Make a new Stub_table.
4843
4844 template<int size, bool big_endian>
4845 Stub_table<size, big_endian>*
4846 Target_aarch64<size, big_endian>::new_stub_table(
4847 AArch64_input_section<size, big_endian>* owner)
4848 {
4849 Stub_table<size, big_endian>* stub_table =
4850 new Stub_table<size, big_endian>(owner);
4851 stub_table->set_address(align_address(
4852 owner->address() + owner->data_size(), 8));
4853 stub_table->set_file_offset(owner->offset() + owner->data_size());
4854 stub_table->finalize_data_size();
4855
4856 this->stub_tables_.push_back(stub_table);
4857
4858 return stub_table;
4859 }
4860
4861
4862 template<int size, bool big_endian>
4863 uint64_t
4864 Target_aarch64<size, big_endian>::do_reloc_addend(
4865 void* arg, unsigned int r_type, uint64_t) const
4866 {
4867 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
4868 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
4869 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
4870 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
4871 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
4872 gold_assert(psymval->is_tls_symbol());
4873 // The value of a TLS symbol is the offset in the TLS segment.
4874 return psymval->value(ti.object, 0);
4875 }
4876
4877 // Return the number of entries in the PLT.
4878
4879 template<int size, bool big_endian>
4880 unsigned int
4881 Target_aarch64<size, big_endian>::plt_entry_count() const
4882 {
4883 if (this->plt_ == NULL)
4884 return 0;
4885 return this->plt_->entry_count();
4886 }
4887
4888 // Return the offset of the first non-reserved PLT entry.
4889
4890 template<int size, bool big_endian>
4891 unsigned int
4892 Target_aarch64<size, big_endian>::first_plt_entry_offset() const
4893 {
4894 return this->plt_->first_plt_entry_offset();
4895 }
4896
4897 // Return the size of each PLT entry.
4898
4899 template<int size, bool big_endian>
4900 unsigned int
4901 Target_aarch64<size, big_endian>::plt_entry_size() const
4902 {
4903 return this->plt_->get_plt_entry_size();
4904 }
4905
4906 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
4907
4908 template<int size, bool big_endian>
4909 void
4910 Target_aarch64<size, big_endian>::define_tls_base_symbol(
4911 Symbol_table* symtab, Layout* layout)
4912 {
4913 if (this->tls_base_symbol_defined_)
4914 return;
4915
4916 Output_segment* tls_segment = layout->tls_segment();
4917 if (tls_segment != NULL)
4918 {
4919 // _TLS_MODULE_BASE_ always points to the beginning of tls segment.
4920 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
4921 Symbol_table::PREDEFINED,
4922 tls_segment, 0, 0,
4923 elfcpp::STT_TLS,
4924 elfcpp::STB_LOCAL,
4925 elfcpp::STV_HIDDEN, 0,
4926 Symbol::SEGMENT_START,
4927 true);
4928 }
4929 this->tls_base_symbol_defined_ = true;
4930 }
4931
4932 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
4933
4934 template<int size, bool big_endian>
4935 void
4936 Target_aarch64<size, big_endian>::reserve_tlsdesc_entries(
4937 Symbol_table* symtab, Layout* layout)
4938 {
4939 if (this->plt_ == NULL)
4940 this->make_plt_section(symtab, layout);
4941
4942 if (!this->plt_->has_tlsdesc_entry())
4943 {
4944 // Allocate the TLSDESC_GOT entry.
4945 Output_data_got_aarch64<size, big_endian>* got =
4946 this->got_section(symtab, layout);
4947 unsigned int got_offset = got->add_constant(0);
4948
4949 // Allocate the TLSDESC_PLT entry.
4950 this->plt_->reserve_tlsdesc_entry(got_offset);
4951 }
4952 }
4953
4954 // Create a GOT entry for the TLS module index.
4955
4956 template<int size, bool big_endian>
4957 unsigned int
4958 Target_aarch64<size, big_endian>::got_mod_index_entry(
4959 Symbol_table* symtab, Layout* layout,
4960 Sized_relobj_file<size, big_endian>* object)
4961 {
4962 if (this->got_mod_index_offset_ == -1U)
4963 {
4964 gold_assert(symtab != NULL && layout != NULL && object != NULL);
4965 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
4966 Output_data_got_aarch64<size, big_endian>* got =
4967 this->got_section(symtab, layout);
4968 unsigned int got_offset = got->add_constant(0);
4969 rela_dyn->add_local(object, 0, elfcpp::R_AARCH64_TLS_DTPMOD64, got,
4970 got_offset, 0);
4971 got->add_constant(0);
4972 this->got_mod_index_offset_ = got_offset;
4973 }
4974 return this->got_mod_index_offset_;
4975 }
4976
4977 // Optimize the TLS relocation type based on what we know about the
4978 // symbol. IS_FINAL is true if the final address of this symbol is
4979 // known at link time.
4980
4981 template<int size, bool big_endian>
4982 tls::Tls_optimization
4983 Target_aarch64<size, big_endian>::optimize_tls_reloc(bool is_final,
4984 int r_type)
4985 {
4986 // If we are generating a shared library, then we can't do anything
4987 // in the linker
4988 if (parameters->options().shared())
4989 return tls::TLSOPT_NONE;
4990
4991 switch (r_type)
4992 {
4993 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
4994 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
4995 case elfcpp::R_AARCH64_TLSDESC_LD_PREL19:
4996 case elfcpp::R_AARCH64_TLSDESC_ADR_PREL21:
4997 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
4998 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
4999 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5000 case elfcpp::R_AARCH64_TLSDESC_OFF_G1:
5001 case elfcpp::R_AARCH64_TLSDESC_OFF_G0_NC:
5002 case elfcpp::R_AARCH64_TLSDESC_LDR:
5003 case elfcpp::R_AARCH64_TLSDESC_ADD:
5004 case elfcpp::R_AARCH64_TLSDESC_CALL:
5005 // These are General-Dynamic which permits fully general TLS
5006 // access. Since we know that we are generating an executable,
5007 // we can convert this to Initial-Exec. If we also know that
5008 // this is a local symbol, we can further switch to Local-Exec.
5009 if (is_final)
5010 return tls::TLSOPT_TO_LE;
5011 return tls::TLSOPT_TO_IE;
5012
5013 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5014 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5015 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5016 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5017 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5018 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5019 // These are Local-Dynamic, which refer to local symbols in the
5020 // dynamic TLS block. Since we know that we generating an
5021 // executable, we can switch to Local-Exec.
5022 return tls::TLSOPT_TO_LE;
5023
5024 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
5025 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
5026 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5027 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5028 case elfcpp::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5029 // These are Initial-Exec relocs which get the thread offset
5030 // from the GOT. If we know that we are linking against the
5031 // local symbol, we can switch to Local-Exec, which links the
5032 // thread offset into the instruction.
5033 if (is_final)
5034 return tls::TLSOPT_TO_LE;
5035 return tls::TLSOPT_NONE;
5036
5037 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5038 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5039 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5040 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5041 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5042 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5043 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5044 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5045 // When we already have Local-Exec, there is nothing further we
5046 // can do.
5047 return tls::TLSOPT_NONE;
5048
5049 default:
5050 gold_unreachable();
5051 }
5052 }
5053
5054 // Returns true if this relocation type could be that of a function pointer.
5055
5056 template<int size, bool big_endian>
5057 inline bool
5058 Target_aarch64<size, big_endian>::Scan::possible_function_pointer_reloc(
5059 unsigned int r_type)
5060 {
5061 switch (r_type)
5062 {
5063 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
5064 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
5065 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
5066 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5067 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5068 {
5069 return true;
5070 }
5071 }
5072 return false;
5073 }
5074
5075 // For safe ICF, scan a relocation for a local symbol to check if it
5076 // corresponds to a function pointer being taken. In that case mark
5077 // the function whose pointer was taken as not foldable.
5078
5079 template<int size, bool big_endian>
5080 inline bool
5081 Target_aarch64<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
5082 Symbol_table* ,
5083 Layout* ,
5084 Target_aarch64<size, big_endian>* ,
5085 Sized_relobj_file<size, big_endian>* ,
5086 unsigned int ,
5087 Output_section* ,
5088 const elfcpp::Rela<size, big_endian>& ,
5089 unsigned int r_type,
5090 const elfcpp::Sym<size, big_endian>&)
5091 {
5092 // When building a shared library, do not fold any local symbols.
5093 return (parameters->options().shared()
5094 || possible_function_pointer_reloc(r_type));
5095 }
5096
5097 // For safe ICF, scan a relocation for a global symbol to check if it
5098 // corresponds to a function pointer being taken. In that case mark
5099 // the function whose pointer was taken as not foldable.
5100
5101 template<int size, bool big_endian>
5102 inline bool
5103 Target_aarch64<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
5104 Symbol_table* ,
5105 Layout* ,
5106 Target_aarch64<size, big_endian>* ,
5107 Sized_relobj_file<size, big_endian>* ,
5108 unsigned int ,
5109 Output_section* ,
5110 const elfcpp::Rela<size, big_endian>& ,
5111 unsigned int r_type,
5112 Symbol* gsym)
5113 {
5114 // When building a shared library, do not fold symbols whose visibility
5115 // is hidden, internal or protected.
5116 return ((parameters->options().shared()
5117 && (gsym->visibility() == elfcpp::STV_INTERNAL
5118 || gsym->visibility() == elfcpp::STV_PROTECTED
5119 || gsym->visibility() == elfcpp::STV_HIDDEN))
5120 || possible_function_pointer_reloc(r_type));
5121 }
5122
5123 // Report an unsupported relocation against a local symbol.
5124
5125 template<int size, bool big_endian>
5126 void
5127 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_local(
5128 Sized_relobj_file<size, big_endian>* object,
5129 unsigned int r_type)
5130 {
5131 gold_error(_("%s: unsupported reloc %u against local symbol"),
5132 object->name().c_str(), r_type);
5133 }
5134
5135 // We are about to emit a dynamic relocation of type R_TYPE. If the
5136 // dynamic linker does not support it, issue an error.
5137
5138 template<int size, bool big_endian>
5139 void
5140 Target_aarch64<size, big_endian>::Scan::check_non_pic(Relobj* object,
5141 unsigned int r_type)
5142 {
5143 gold_assert(r_type != elfcpp::R_AARCH64_NONE);
5144
5145 switch (r_type)
5146 {
5147 // These are the relocation types supported by glibc for AARCH64.
5148 case elfcpp::R_AARCH64_NONE:
5149 case elfcpp::R_AARCH64_COPY:
5150 case elfcpp::R_AARCH64_GLOB_DAT:
5151 case elfcpp::R_AARCH64_JUMP_SLOT:
5152 case elfcpp::R_AARCH64_RELATIVE:
5153 case elfcpp::R_AARCH64_TLS_DTPREL64:
5154 case elfcpp::R_AARCH64_TLS_DTPMOD64:
5155 case elfcpp::R_AARCH64_TLS_TPREL64:
5156 case elfcpp::R_AARCH64_TLSDESC:
5157 case elfcpp::R_AARCH64_IRELATIVE:
5158 case elfcpp::R_AARCH64_ABS32:
5159 case elfcpp::R_AARCH64_ABS64:
5160 return;
5161
5162 default:
5163 break;
5164 }
5165
5166 // This prevents us from issuing more than one error per reloc
5167 // section. But we can still wind up issuing more than one
5168 // error per object file.
5169 if (this->issued_non_pic_error_)
5170 return;
5171 gold_assert(parameters->options().output_is_position_independent());
5172 object->error(_("requires unsupported dynamic reloc; "
5173 "recompile with -fPIC"));
5174 this->issued_non_pic_error_ = true;
5175 return;
5176 }
5177
5178 // Return whether we need to make a PLT entry for a relocation of the
5179 // given type against a STT_GNU_IFUNC symbol.
5180
5181 template<int size, bool big_endian>
5182 bool
5183 Target_aarch64<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
5184 Sized_relobj_file<size, big_endian>* object,
5185 unsigned int r_type)
5186 {
5187 const AArch64_reloc_property* arp =
5188 aarch64_reloc_property_table->get_reloc_property(r_type);
5189 gold_assert(arp != NULL);
5190
5191 int flags = arp->reference_flags();
5192 if (flags & Symbol::TLS_REF)
5193 {
5194 gold_error(_("%s: unsupported TLS reloc %s for IFUNC symbol"),
5195 object->name().c_str(), arp->name().c_str());
5196 return false;
5197 }
5198 return flags != 0;
5199 }
5200
5201 // Scan a relocation for a local symbol.
5202
5203 template<int size, bool big_endian>
5204 inline void
5205 Target_aarch64<size, big_endian>::Scan::local(
5206 Symbol_table* symtab,
5207 Layout* layout,
5208 Target_aarch64<size, big_endian>* target,
5209 Sized_relobj_file<size, big_endian>* object,
5210 unsigned int data_shndx,
5211 Output_section* output_section,
5212 const elfcpp::Rela<size, big_endian>& rela,
5213 unsigned int r_type,
5214 const elfcpp::Sym<size, big_endian>& lsym,
5215 bool is_discarded)
5216 {
5217 if (is_discarded)
5218 return;
5219
5220 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
5221 Reloc_section;
5222 Output_data_got_aarch64<size, big_endian>* got =
5223 target->got_section(symtab, layout);
5224 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5225
5226 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5227 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
5228 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
5229 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
5230
5231 switch (r_type)
5232 {
5233 case elfcpp::R_AARCH64_ABS32:
5234 case elfcpp::R_AARCH64_ABS16:
5235 if (parameters->options().output_is_position_independent())
5236 {
5237 gold_error(_("%s: unsupported reloc %u in pos independent link."),
5238 object->name().c_str(), r_type);
5239 }
5240 break;
5241
5242 case elfcpp::R_AARCH64_ABS64:
5243 // If building a shared library or pie, we need to mark this as a dynmic
5244 // reloction, so that the dynamic loader can relocate it.
5245 if (parameters->options().output_is_position_independent())
5246 {
5247 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5248 rela_dyn->add_local_relative(object, r_sym,
5249 elfcpp::R_AARCH64_RELATIVE,
5250 output_section,
5251 data_shndx,
5252 rela.get_r_offset(),
5253 rela.get_r_addend(),
5254 is_ifunc);
5255 }
5256 break;
5257
5258 case elfcpp::R_AARCH64_PREL64:
5259 case elfcpp::R_AARCH64_PREL32:
5260 case elfcpp::R_AARCH64_PREL16:
5261 break;
5262
5263 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
5264 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
5265 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
5266 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
5267 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
5268 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
5269 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
5270 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
5271 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
5272 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
5273 break;
5274
5275 // Control flow, pc-relative. We don't need to do anything for a relative
5276 // addressing relocation against a local symbol if it does not reference
5277 // the GOT.
5278 case elfcpp::R_AARCH64_TSTBR14:
5279 case elfcpp::R_AARCH64_CONDBR19:
5280 case elfcpp::R_AARCH64_JUMP26:
5281 case elfcpp::R_AARCH64_CALL26:
5282 break;
5283
5284 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5285 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5286 {
5287 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5288 optimize_tls_reloc(!parameters->options().shared(), r_type);
5289 if (tlsopt == tls::TLSOPT_TO_LE)
5290 break;
5291
5292 layout->set_has_static_tls();
5293 // Create a GOT entry for the tp-relative offset.
5294 if (!parameters->doing_static_link())
5295 {
5296 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
5297 target->rela_dyn_section(layout),
5298 elfcpp::R_AARCH64_TLS_TPREL64);
5299 }
5300 else if (!object->local_has_got_offset(r_sym,
5301 GOT_TYPE_TLS_OFFSET))
5302 {
5303 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
5304 unsigned int got_offset =
5305 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
5306 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5307 gold_assert(addend == 0);
5308 got->add_static_reloc(got_offset, elfcpp::R_AARCH64_TLS_TPREL64,
5309 object, r_sym);
5310 }
5311 }
5312 break;
5313
5314 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5315 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5316 {
5317 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5318 optimize_tls_reloc(!parameters->options().shared(), r_type);
5319 if (tlsopt == tls::TLSOPT_TO_LE)
5320 {
5321 layout->set_has_static_tls();
5322 break;
5323 }
5324 gold_assert(tlsopt == tls::TLSOPT_NONE);
5325
5326 got->add_local_pair_with_rel(object,r_sym, data_shndx,
5327 GOT_TYPE_TLS_PAIR,
5328 target->rela_dyn_section(layout),
5329 elfcpp::R_AARCH64_TLS_DTPMOD64);
5330 }
5331 break;
5332
5333 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5334 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5335 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5336 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5337 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5338 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5339 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5340 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5341 {
5342 layout->set_has_static_tls();
5343 bool output_is_shared = parameters->options().shared();
5344 if (output_is_shared)
5345 gold_error(_("%s: unsupported TLSLE reloc %u in shared code."),
5346 object->name().c_str(), r_type);
5347 }
5348 break;
5349
5350 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5351 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5352 {
5353 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5354 optimize_tls_reloc(!parameters->options().shared(), r_type);
5355 if (tlsopt == tls::TLSOPT_NONE)
5356 {
5357 // Create a GOT entry for the module index.
5358 target->got_mod_index_entry(symtab, layout, object);
5359 }
5360 else if (tlsopt != tls::TLSOPT_TO_LE)
5361 unsupported_reloc_local(object, r_type);
5362 }
5363 break;
5364
5365 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5366 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5367 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5368 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5369 break;
5370
5371 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5372 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5373 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5374 {
5375 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5376 optimize_tls_reloc(!parameters->options().shared(), r_type);
5377 target->define_tls_base_symbol(symtab, layout);
5378 if (tlsopt == tls::TLSOPT_NONE)
5379 {
5380 // Create reserved PLT and GOT entries for the resolver.
5381 target->reserve_tlsdesc_entries(symtab, layout);
5382
5383 // Generate a double GOT entry with an R_AARCH64_TLSDESC reloc.
5384 // The R_AARCH64_TLSDESC reloc is resolved lazily, so the GOT
5385 // entry needs to be in an area in .got.plt, not .got. Call
5386 // got_section to make sure the section has been created.
5387 target->got_section(symtab, layout);
5388 Output_data_got<size, big_endian>* got =
5389 target->got_tlsdesc_section();
5390 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5391 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
5392 {
5393 unsigned int got_offset = got->add_constant(0);
5394 got->add_constant(0);
5395 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
5396 got_offset);
5397 Reloc_section* rt = target->rela_tlsdesc_section(layout);
5398 // We store the arguments we need in a vector, and use
5399 // the index into the vector as the parameter to pass
5400 // to the target specific routines.
5401 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
5402 void* arg = reinterpret_cast<void*>(intarg);
5403 rt->add_target_specific(elfcpp::R_AARCH64_TLSDESC, arg,
5404 got, got_offset, 0);
5405 }
5406 }
5407 else if (tlsopt != tls::TLSOPT_TO_LE)
5408 unsupported_reloc_local(object, r_type);
5409 }
5410 break;
5411
5412 case elfcpp::R_AARCH64_TLSDESC_CALL:
5413 break;
5414
5415 default:
5416 unsupported_reloc_local(object, r_type);
5417 }
5418 }
5419
5420
5421 // Report an unsupported relocation against a global symbol.
5422
5423 template<int size, bool big_endian>
5424 void
5425 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_global(
5426 Sized_relobj_file<size, big_endian>* object,
5427 unsigned int r_type,
5428 Symbol* gsym)
5429 {
5430 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5431 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5432 }
5433
5434 template<int size, bool big_endian>
5435 inline void
5436 Target_aarch64<size, big_endian>::Scan::global(
5437 Symbol_table* symtab,
5438 Layout* layout,
5439 Target_aarch64<size, big_endian>* target,
5440 Sized_relobj_file<size, big_endian> * object,
5441 unsigned int data_shndx,
5442 Output_section* output_section,
5443 const elfcpp::Rela<size, big_endian>& rela,
5444 unsigned int r_type,
5445 Symbol* gsym)
5446 {
5447 // A STT_GNU_IFUNC symbol may require a PLT entry.
5448 if (gsym->type() == elfcpp::STT_GNU_IFUNC
5449 && this->reloc_needs_plt_for_ifunc(object, r_type))
5450 target->make_plt_entry(symtab, layout, gsym);
5451
5452 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
5453 Reloc_section;
5454 const AArch64_reloc_property* arp =
5455 aarch64_reloc_property_table->get_reloc_property(r_type);
5456 gold_assert(arp != NULL);
5457
5458 switch (r_type)
5459 {
5460 case elfcpp::R_AARCH64_ABS16:
5461 case elfcpp::R_AARCH64_ABS32:
5462 case elfcpp::R_AARCH64_ABS64:
5463 {
5464 // Make a PLT entry if necessary.
5465 if (gsym->needs_plt_entry())
5466 {
5467 target->make_plt_entry(symtab, layout, gsym);
5468 // Since this is not a PC-relative relocation, we may be
5469 // taking the address of a function. In that case we need to
5470 // set the entry in the dynamic symbol table to the address of
5471 // the PLT entry.
5472 if (gsym->is_from_dynobj() && !parameters->options().shared())
5473 gsym->set_needs_dynsym_value();
5474 }
5475 // Make a dynamic relocation if necessary.
5476 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
5477 {
5478 if (!parameters->options().output_is_position_independent()
5479 && gsym->may_need_copy_reloc())
5480 {
5481 target->copy_reloc(symtab, layout, object,
5482 data_shndx, output_section, gsym, rela);
5483 }
5484 else if (r_type == elfcpp::R_AARCH64_ABS64
5485 && gsym->type() == elfcpp::STT_GNU_IFUNC
5486 && gsym->can_use_relative_reloc(false)
5487 && !gsym->is_from_dynobj()
5488 && !gsym->is_undefined()
5489 && !gsym->is_preemptible())
5490 {
5491 // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
5492 // symbol. This makes a function address in a PIE executable
5493 // match the address in a shared library that it links against.
5494 Reloc_section* rela_dyn =
5495 target->rela_irelative_section(layout);
5496 unsigned int r_type = elfcpp::R_AARCH64_IRELATIVE;
5497 rela_dyn->add_symbolless_global_addend(gsym, r_type,
5498 output_section, object,
5499 data_shndx,
5500 rela.get_r_offset(),
5501 rela.get_r_addend());
5502 }
5503 else if (r_type == elfcpp::R_AARCH64_ABS64
5504 && gsym->can_use_relative_reloc(false))
5505 {
5506 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5507 rela_dyn->add_global_relative(gsym,
5508 elfcpp::R_AARCH64_RELATIVE,
5509 output_section,
5510 object,
5511 data_shndx,
5512 rela.get_r_offset(),
5513 rela.get_r_addend(),
5514 false);
5515 }
5516 else
5517 {
5518 check_non_pic(object, r_type);
5519 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*
5520 rela_dyn = target->rela_dyn_section(layout);
5521 rela_dyn->add_global(
5522 gsym, r_type, output_section, object,
5523 data_shndx, rela.get_r_offset(),rela.get_r_addend());
5524 }
5525 }
5526 }
5527 break;
5528
5529 case elfcpp::R_AARCH64_PREL16:
5530 case elfcpp::R_AARCH64_PREL32:
5531 case elfcpp::R_AARCH64_PREL64:
5532 // This is used to fill the GOT absolute address.
5533 if (gsym->needs_plt_entry())
5534 {
5535 target->make_plt_entry(symtab, layout, gsym);
5536 }
5537 break;
5538
5539 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
5540 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
5541 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
5542 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
5543 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
5544 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
5545 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
5546 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
5547 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
5548 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
5549 {
5550 if (gsym->needs_plt_entry())
5551 target->make_plt_entry(symtab, layout, gsym);
5552 // Make a dynamic relocation if necessary.
5553 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
5554 {
5555 if (parameters->options().output_is_executable()
5556 && gsym->may_need_copy_reloc())
5557 {
5558 target->copy_reloc(symtab, layout, object,
5559 data_shndx, output_section, gsym, rela);
5560 }
5561 }
5562 break;
5563 }
5564
5565 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5566 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5567 {
5568 // This pair of relocations is used to access a specific GOT entry.
5569 // Note a GOT entry is an *address* to a symbol.
5570 // The symbol requires a GOT entry
5571 Output_data_got_aarch64<size, big_endian>* got =
5572 target->got_section(symtab, layout);
5573 if (gsym->final_value_is_known())
5574 {
5575 // For a STT_GNU_IFUNC symbol we want the PLT address.
5576 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
5577 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
5578 else
5579 got->add_global(gsym, GOT_TYPE_STANDARD);
5580 }
5581 else
5582 {
5583 // If this symbol is not fully resolved, we need to add a dynamic
5584 // relocation for it.
5585 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5586
5587 // Use a GLOB_DAT rather than a RELATIVE reloc if:
5588 //
5589 // 1) The symbol may be defined in some other module.
5590 // 2) We are building a shared library and this is a protected
5591 // symbol; using GLOB_DAT means that the dynamic linker can use
5592 // the address of the PLT in the main executable when appropriate
5593 // so that function address comparisons work.
5594 // 3) This is a STT_GNU_IFUNC symbol in position dependent code,
5595 // again so that function address comparisons work.
5596 if (gsym->is_from_dynobj()
5597 || gsym->is_undefined()
5598 || gsym->is_preemptible()
5599 || (gsym->visibility() == elfcpp::STV_PROTECTED
5600 && parameters->options().shared())
5601 || (gsym->type() == elfcpp::STT_GNU_IFUNC
5602 && parameters->options().output_is_position_independent()))
5603 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
5604 rela_dyn, elfcpp::R_AARCH64_GLOB_DAT);
5605 else
5606 {
5607 // For a STT_GNU_IFUNC symbol we want to write the PLT
5608 // offset into the GOT, so that function pointer
5609 // comparisons work correctly.
5610 bool is_new;
5611 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
5612 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
5613 else
5614 {
5615 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
5616 // Tell the dynamic linker to use the PLT address
5617 // when resolving relocations.
5618 if (gsym->is_from_dynobj()
5619 && !parameters->options().shared())
5620 gsym->set_needs_dynsym_value();
5621 }
5622 if (is_new)
5623 {
5624 rela_dyn->add_global_relative(
5625 gsym, elfcpp::R_AARCH64_RELATIVE,
5626 got,
5627 gsym->got_offset(GOT_TYPE_STANDARD),
5628 0,
5629 false);
5630 }
5631 }
5632 }
5633 break;
5634 }
5635
5636 case elfcpp::R_AARCH64_TSTBR14:
5637 case elfcpp::R_AARCH64_CONDBR19:
5638 case elfcpp::R_AARCH64_JUMP26:
5639 case elfcpp::R_AARCH64_CALL26:
5640 {
5641 if (gsym->final_value_is_known())
5642 break;
5643
5644 if (gsym->is_defined() &&
5645 !gsym->is_from_dynobj() &&
5646 !gsym->is_preemptible())
5647 break;
5648
5649 // Make plt entry for function call.
5650 target->make_plt_entry(symtab, layout, gsym);
5651 break;
5652 }
5653
5654 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5655 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // General dynamic
5656 {
5657 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5658 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
5659 if (tlsopt == tls::TLSOPT_TO_LE)
5660 {
5661 layout->set_has_static_tls();
5662 break;
5663 }
5664 gold_assert(tlsopt == tls::TLSOPT_NONE);
5665
5666 // General dynamic.
5667 Output_data_got_aarch64<size, big_endian>* got =
5668 target->got_section(symtab, layout);
5669 // Create 2 consecutive entries for module index and offset.
5670 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
5671 target->rela_dyn_section(layout),
5672 elfcpp::R_AARCH64_TLS_DTPMOD64,
5673 elfcpp::R_AARCH64_TLS_DTPREL64);
5674 }
5675 break;
5676
5677 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5678 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local dynamic
5679 {
5680 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5681 optimize_tls_reloc(!parameters->options().shared(), r_type);
5682 if (tlsopt == tls::TLSOPT_NONE)
5683 {
5684 // Create a GOT entry for the module index.
5685 target->got_mod_index_entry(symtab, layout, object);
5686 }
5687 else if (tlsopt != tls::TLSOPT_TO_LE)
5688 unsupported_reloc_local(object, r_type);
5689 }
5690 break;
5691
5692 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5693 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5694 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5695 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local dynamic
5696 break;
5697
5698 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5699 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial executable
5700 {
5701 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5702 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
5703 if (tlsopt == tls::TLSOPT_TO_LE)
5704 break;
5705
5706 layout->set_has_static_tls();
5707 // Create a GOT entry for the tp-relative offset.
5708 Output_data_got_aarch64<size, big_endian>* got
5709 = target->got_section(symtab, layout);
5710 if (!parameters->doing_static_link())
5711 {
5712 got->add_global_with_rel(
5713 gsym, GOT_TYPE_TLS_OFFSET,
5714 target->rela_dyn_section(layout),
5715 elfcpp::R_AARCH64_TLS_TPREL64);
5716 }
5717 if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
5718 {
5719 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
5720 unsigned int got_offset =
5721 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
5722 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5723 gold_assert(addend == 0);
5724 got->add_static_reloc(got_offset,
5725 elfcpp::R_AARCH64_TLS_TPREL64, gsym);
5726 }
5727 }
5728 break;
5729
5730 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5731 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5732 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5733 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5734 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5735 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5736 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5737 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: // Local executable
5738 layout->set_has_static_tls();
5739 if (parameters->options().shared())
5740 gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
5741 object->name().c_str(), r_type);
5742 break;
5743
5744 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5745 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5746 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12: // TLS descriptor
5747 {
5748 target->define_tls_base_symbol(symtab, layout);
5749 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5750 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
5751 if (tlsopt == tls::TLSOPT_NONE)
5752 {
5753 // Create reserved PLT and GOT entries for the resolver.
5754 target->reserve_tlsdesc_entries(symtab, layout);
5755
5756 // Create a double GOT entry with an R_AARCH64_TLSDESC
5757 // relocation. The R_AARCH64_TLSDESC is resolved lazily, so the GOT
5758 // entry needs to be in an area in .got.plt, not .got. Call
5759 // got_section to make sure the section has been created.
5760 target->got_section(symtab, layout);
5761 Output_data_got<size, big_endian>* got =
5762 target->got_tlsdesc_section();
5763 Reloc_section* rt = target->rela_tlsdesc_section(layout);
5764 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
5765 elfcpp::R_AARCH64_TLSDESC, 0);
5766 }
5767 else if (tlsopt == tls::TLSOPT_TO_IE)
5768 {
5769 // Create a GOT entry for the tp-relative offset.
5770 Output_data_got<size, big_endian>* got
5771 = target->got_section(symtab, layout);
5772 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
5773 target->rela_dyn_section(layout),
5774 elfcpp::R_AARCH64_TLS_TPREL64);
5775 }
5776 else if (tlsopt != tls::TLSOPT_TO_LE)
5777 unsupported_reloc_global(object, r_type, gsym);
5778 }
5779 break;
5780
5781 case elfcpp::R_AARCH64_TLSDESC_CALL:
5782 break;
5783
5784 default:
5785 gold_error(_("%s: unsupported reloc type in global scan"),
5786 aarch64_reloc_property_table->
5787 reloc_name_in_error_message(r_type).c_str());
5788 }
5789 return;
5790 } // End of Scan::global
5791
5792
5793 // Create the PLT section.
5794 template<int size, bool big_endian>
5795 void
5796 Target_aarch64<size, big_endian>::make_plt_section(
5797 Symbol_table* symtab, Layout* layout)
5798 {
5799 if (this->plt_ == NULL)
5800 {
5801 // Create the GOT section first.
5802 this->got_section(symtab, layout);
5803
5804 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
5805 this->got_irelative_);
5806
5807 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
5808 (elfcpp::SHF_ALLOC
5809 | elfcpp::SHF_EXECINSTR),
5810 this->plt_, ORDER_PLT, false);
5811
5812 // Make the sh_info field of .rela.plt point to .plt.
5813 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
5814 rela_plt_os->set_info_section(this->plt_->output_section());
5815 }
5816 }
5817
5818 // Return the section for TLSDESC relocations.
5819
5820 template<int size, bool big_endian>
5821 typename Target_aarch64<size, big_endian>::Reloc_section*
5822 Target_aarch64<size, big_endian>::rela_tlsdesc_section(Layout* layout) const
5823 {
5824 return this->plt_section()->rela_tlsdesc(layout);
5825 }
5826
5827 // Create a PLT entry for a global symbol.
5828
5829 template<int size, bool big_endian>
5830 void
5831 Target_aarch64<size, big_endian>::make_plt_entry(
5832 Symbol_table* symtab,
5833 Layout* layout,
5834 Symbol* gsym)
5835 {
5836 if (gsym->has_plt_offset())
5837 return;
5838
5839 if (this->plt_ == NULL)
5840 this->make_plt_section(symtab, layout);
5841
5842 this->plt_->add_entry(symtab, layout, gsym);
5843 }
5844
5845 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5846
5847 template<int size, bool big_endian>
5848 void
5849 Target_aarch64<size, big_endian>::make_local_ifunc_plt_entry(
5850 Symbol_table* symtab, Layout* layout,
5851 Sized_relobj_file<size, big_endian>* relobj,
5852 unsigned int local_sym_index)
5853 {
5854 if (relobj->local_has_plt_offset(local_sym_index))
5855 return;
5856 if (this->plt_ == NULL)
5857 this->make_plt_section(symtab, layout);
5858 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
5859 relobj,
5860 local_sym_index);
5861 relobj->set_local_plt_offset(local_sym_index, plt_offset);
5862 }
5863
5864 template<int size, bool big_endian>
5865 void
5866 Target_aarch64<size, big_endian>::gc_process_relocs(
5867 Symbol_table* symtab,
5868 Layout* layout,
5869 Sized_relobj_file<size, big_endian>* object,
5870 unsigned int data_shndx,
5871 unsigned int sh_type,
5872 const unsigned char* prelocs,
5873 size_t reloc_count,
5874 Output_section* output_section,
5875 bool needs_special_offset_handling,
5876 size_t local_symbol_count,
5877 const unsigned char* plocal_symbols)
5878 {
5879 if (sh_type == elfcpp::SHT_REL)
5880 {
5881 return;
5882 }
5883
5884 gold::gc_process_relocs<
5885 size, big_endian,
5886 Target_aarch64<size, big_endian>,
5887 elfcpp::SHT_RELA,
5888 typename Target_aarch64<size, big_endian>::Scan,
5889 typename Target_aarch64<size, big_endian>::Relocatable_size_for_reloc>(
5890 symtab,
5891 layout,
5892 this,
5893 object,
5894 data_shndx,
5895 prelocs,
5896 reloc_count,
5897 output_section,
5898 needs_special_offset_handling,
5899 local_symbol_count,
5900 plocal_symbols);
5901 }
5902
5903 // Scan relocations for a section.
5904
5905 template<int size, bool big_endian>
5906 void
5907 Target_aarch64<size, big_endian>::scan_relocs(
5908 Symbol_table* symtab,
5909 Layout* layout,
5910 Sized_relobj_file<size, big_endian>* object,
5911 unsigned int data_shndx,
5912 unsigned int sh_type,
5913 const unsigned char* prelocs,
5914 size_t reloc_count,
5915 Output_section* output_section,
5916 bool needs_special_offset_handling,
5917 size_t local_symbol_count,
5918 const unsigned char* plocal_symbols)
5919 {
5920 if (sh_type == elfcpp::SHT_REL)
5921 {
5922 gold_error(_("%s: unsupported REL reloc section"),
5923 object->name().c_str());
5924 return;
5925 }
5926 gold::scan_relocs<size, big_endian, Target_aarch64, elfcpp::SHT_RELA, Scan>(
5927 symtab,
5928 layout,
5929 this,
5930 object,
5931 data_shndx,
5932 prelocs,
5933 reloc_count,
5934 output_section,
5935 needs_special_offset_handling,
5936 local_symbol_count,
5937 plocal_symbols);
5938 }
5939
5940 // Return the value to use for a dynamic which requires special
5941 // treatment. This is how we support equality comparisons of function
5942 // pointers across shared library boundaries, as described in the
5943 // processor specific ABI supplement.
5944
5945 template<int size, bool big_endian>
5946 uint64_t
5947 Target_aarch64<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
5948 {
5949 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5950 return this->plt_address_for_global(gsym);
5951 }
5952
5953
5954 // Finalize the sections.
5955
5956 template<int size, bool big_endian>
5957 void
5958 Target_aarch64<size, big_endian>::do_finalize_sections(
5959 Layout* layout,
5960 const Input_objects*,
5961 Symbol_table* symtab)
5962 {
5963 const Reloc_section* rel_plt = (this->plt_ == NULL
5964 ? NULL
5965 : this->plt_->rela_plt());
5966 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
5967 this->rela_dyn_, true, false);
5968
5969 // Emit any relocs we saved in an attempt to avoid generating COPY
5970 // relocs.
5971 if (this->copy_relocs_.any_saved_relocs())
5972 this->copy_relocs_.emit(this->rela_dyn_section(layout));
5973
5974 // Fill in some more dynamic tags.
5975 Output_data_dynamic* const odyn = layout->dynamic_data();
5976 if (odyn != NULL)
5977 {
5978 if (this->plt_ != NULL
5979 && this->plt_->output_section() != NULL
5980 && this->plt_ ->has_tlsdesc_entry())
5981 {
5982 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
5983 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
5984 this->got_->finalize_data_size();
5985 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
5986 this->plt_, plt_offset);
5987 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
5988 this->got_, got_offset);
5989 }
5990 }
5991
5992 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
5993 // the .got.plt section.
5994 Symbol* sym = this->global_offset_table_;
5995 if (sym != NULL)
5996 {
5997 uint64_t data_size = this->got_plt_->current_data_size();
5998 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
5999
6000 // If the .got section is more than 0x8000 bytes, we add
6001 // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
6002 // bit relocations have a greater chance of working.
6003 if (data_size >= 0x8000)
6004 symtab->get_sized_symbol<size>(sym)->set_value(
6005 symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
6006 }
6007
6008 if (parameters->doing_static_link()
6009 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
6010 {
6011 // If linking statically, make sure that the __rela_iplt symbols
6012 // were defined if necessary, even if we didn't create a PLT.
6013 static const Define_symbol_in_segment syms[] =
6014 {
6015 {
6016 "__rela_iplt_start", // name
6017 elfcpp::PT_LOAD, // segment_type
6018 elfcpp::PF_W, // segment_flags_set
6019 elfcpp::PF(0), // segment_flags_clear
6020 0, // value
6021 0, // size
6022 elfcpp::STT_NOTYPE, // type
6023 elfcpp::STB_GLOBAL, // binding
6024 elfcpp::STV_HIDDEN, // visibility
6025 0, // nonvis
6026 Symbol::SEGMENT_START, // offset_from_base
6027 true // only_if_ref
6028 },
6029 {
6030 "__rela_iplt_end", // name
6031 elfcpp::PT_LOAD, // segment_type
6032 elfcpp::PF_W, // segment_flags_set
6033 elfcpp::PF(0), // segment_flags_clear
6034 0, // value
6035 0, // size
6036 elfcpp::STT_NOTYPE, // type
6037 elfcpp::STB_GLOBAL, // binding
6038 elfcpp::STV_HIDDEN, // visibility
6039 0, // nonvis
6040 Symbol::SEGMENT_START, // offset_from_base
6041 true // only_if_ref
6042 }
6043 };
6044
6045 symtab->define_symbols(layout, 2, syms,
6046 layout->script_options()->saw_sections_clause());
6047 }
6048
6049 return;
6050 }
6051
6052 // Perform a relocation.
6053
6054 template<int size, bool big_endian>
6055 inline bool
6056 Target_aarch64<size, big_endian>::Relocate::relocate(
6057 const Relocate_info<size, big_endian>* relinfo,
6058 Target_aarch64<size, big_endian>* target,
6059 Output_section* ,
6060 size_t relnum,
6061 const elfcpp::Rela<size, big_endian>& rela,
6062 unsigned int r_type,
6063 const Sized_symbol<size>* gsym,
6064 const Symbol_value<size>* psymval,
6065 unsigned char* view,
6066 typename elfcpp::Elf_types<size>::Elf_Addr address,
6067 section_size_type /* view_size */)
6068 {
6069 if (view == NULL)
6070 return true;
6071
6072 typedef AArch64_relocate_functions<size, big_endian> Reloc;
6073
6074 const AArch64_reloc_property* reloc_property =
6075 aarch64_reloc_property_table->get_reloc_property(r_type);
6076
6077 if (reloc_property == NULL)
6078 {
6079 std::string reloc_name =
6080 aarch64_reloc_property_table->reloc_name_in_error_message(r_type);
6081 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6082 _("cannot relocate %s in object file"),
6083 reloc_name.c_str());
6084 return true;
6085 }
6086
6087 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
6088
6089 // Pick the value to use for symbols defined in the PLT.
6090 Symbol_value<size> symval;
6091 if (gsym != NULL
6092 && gsym->use_plt_offset(reloc_property->reference_flags()))
6093 {
6094 symval.set_output_value(target->plt_address_for_global(gsym));
6095 psymval = &symval;
6096 }
6097 else if (gsym == NULL && psymval->is_ifunc_symbol())
6098 {
6099 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6100 if (object->local_has_plt_offset(r_sym))
6101 {
6102 symval.set_output_value(target->plt_address_for_local(object, r_sym));
6103 psymval = &symval;
6104 }
6105 }
6106
6107 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6108
6109 // Get the GOT offset if needed.
6110 // For aarch64, the GOT pointer points to the start of the GOT section.
6111 bool have_got_offset = false;
6112 int got_offset = 0;
6113 int got_base = (target->got_ != NULL
6114 ? (target->got_->current_data_size() >= 0x8000
6115 ? 0x8000 : 0)
6116 : 0);
6117 switch (r_type)
6118 {
6119 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0:
6120 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC:
6121 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1:
6122 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC:
6123 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2:
6124 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC:
6125 case elfcpp::R_AARCH64_MOVW_GOTOFF_G3:
6126 case elfcpp::R_AARCH64_GOTREL64:
6127 case elfcpp::R_AARCH64_GOTREL32:
6128 case elfcpp::R_AARCH64_GOT_LD_PREL19:
6129 case elfcpp::R_AARCH64_LD64_GOTOFF_LO15:
6130 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6131 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6132 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
6133 if (gsym != NULL)
6134 {
6135 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6136 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - got_base;
6137 }
6138 else
6139 {
6140 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6141 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6142 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
6143 - got_base);
6144 }
6145 have_got_offset = true;
6146 break;
6147
6148 default:
6149 break;
6150 }
6151
6152 typename Reloc::Status reloc_status = Reloc::STATUS_OKAY;
6153 typename elfcpp::Elf_types<size>::Elf_Addr value;
6154 switch (r_type)
6155 {
6156 case elfcpp::R_AARCH64_NONE:
6157 break;
6158
6159 case elfcpp::R_AARCH64_ABS64:
6160 reloc_status = Reloc::template rela_ua<64>(
6161 view, object, psymval, addend, reloc_property);
6162 break;
6163
6164 case elfcpp::R_AARCH64_ABS32:
6165 reloc_status = Reloc::template rela_ua<32>(
6166 view, object, psymval, addend, reloc_property);
6167 break;
6168
6169 case elfcpp::R_AARCH64_ABS16:
6170 reloc_status = Reloc::template rela_ua<16>(
6171 view, object, psymval, addend, reloc_property);
6172 break;
6173
6174 case elfcpp::R_AARCH64_PREL64:
6175 reloc_status = Reloc::template pcrela_ua<64>(
6176 view, object, psymval, addend, address, reloc_property);
6177 break;
6178
6179 case elfcpp::R_AARCH64_PREL32:
6180 reloc_status = Reloc::template pcrela_ua<32>(
6181 view, object, psymval, addend, address, reloc_property);
6182 break;
6183
6184 case elfcpp::R_AARCH64_PREL16:
6185 reloc_status = Reloc::template pcrela_ua<16>(
6186 view, object, psymval, addend, address, reloc_property);
6187 break;
6188
6189 case elfcpp::R_AARCH64_LD_PREL_LO19:
6190 reloc_status = Reloc::template pcrela_general<32>(
6191 view, object, psymval, addend, address, reloc_property);
6192 break;
6193
6194 case elfcpp::R_AARCH64_ADR_PREL_LO21:
6195 reloc_status = Reloc::adr(view, object, psymval, addend,
6196 address, reloc_property);
6197 break;
6198
6199 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
6200 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
6201 reloc_status = Reloc::adrp(view, object, psymval, addend, address,
6202 reloc_property);
6203 break;
6204
6205 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:
6206 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:
6207 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:
6208 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:
6209 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC:
6210 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
6211 reloc_status = Reloc::template rela_general<32>(
6212 view, object, psymval, addend, reloc_property);
6213 break;
6214
6215 case elfcpp::R_AARCH64_CALL26:
6216 if (this->skip_call_tls_get_addr_)
6217 {
6218 // Double check that the TLSGD insn has been optimized away.
6219 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6220 Insntype insn = elfcpp::Swap<32, big_endian>::readval(
6221 reinterpret_cast<Insntype*>(view));
6222 gold_assert((insn & 0xff000000) == 0x91000000);
6223
6224 reloc_status = Reloc::STATUS_OKAY;
6225 this->skip_call_tls_get_addr_ = false;
6226 // Return false to stop further processing this reloc.
6227 return false;
6228 }
6229 // Fallthrough
6230 case elfcpp::R_AARCH64_JUMP26:
6231 if (Reloc::maybe_apply_stub(r_type, relinfo, rela, view, address,
6232 gsym, psymval, object,
6233 target->stub_group_size_))
6234 break;
6235 // Fallthrough
6236 case elfcpp::R_AARCH64_TSTBR14:
6237 case elfcpp::R_AARCH64_CONDBR19:
6238 reloc_status = Reloc::template pcrela_general<32>(
6239 view, object, psymval, addend, address, reloc_property);
6240 break;
6241
6242 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6243 gold_assert(have_got_offset);
6244 value = target->got_->address() + got_base + got_offset;
6245 reloc_status = Reloc::adrp(view, value + addend, address);
6246 break;
6247
6248 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6249 gold_assert(have_got_offset);
6250 value = target->got_->address() + got_base + got_offset;
6251 reloc_status = Reloc::template rela_general<32>(
6252 view, value, addend, reloc_property);
6253 break;
6254
6255 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6256 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
6257 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6258 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
6259 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6260 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6261 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6262 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
6263 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6264 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6265 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6266 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6267 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6268 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6269 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6270 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6271 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6272 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6273 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6274 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6275 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6276 case elfcpp::R_AARCH64_TLSDESC_CALL:
6277 reloc_status = relocate_tls(relinfo, target, relnum, rela, r_type,
6278 gsym, psymval, view, address);
6279 break;
6280
6281 // These are dynamic relocations, which are unexpected when linking.
6282 case elfcpp::R_AARCH64_COPY:
6283 case elfcpp::R_AARCH64_GLOB_DAT:
6284 case elfcpp::R_AARCH64_JUMP_SLOT:
6285 case elfcpp::R_AARCH64_RELATIVE:
6286 case elfcpp::R_AARCH64_IRELATIVE:
6287 case elfcpp::R_AARCH64_TLS_DTPREL64:
6288 case elfcpp::R_AARCH64_TLS_DTPMOD64:
6289 case elfcpp::R_AARCH64_TLS_TPREL64:
6290 case elfcpp::R_AARCH64_TLSDESC:
6291 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6292 _("unexpected reloc %u in object file"),
6293 r_type);
6294 break;
6295
6296 default:
6297 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6298 _("unsupported reloc %s"),
6299 reloc_property->name().c_str());
6300 break;
6301 }
6302
6303 // Report any errors.
6304 switch (reloc_status)
6305 {
6306 case Reloc::STATUS_OKAY:
6307 break;
6308 case Reloc::STATUS_OVERFLOW:
6309 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6310 _("relocation overflow in %s"),
6311 reloc_property->name().c_str());
6312 break;
6313 case Reloc::STATUS_BAD_RELOC:
6314 gold_error_at_location(
6315 relinfo,
6316 relnum,
6317 rela.get_r_offset(),
6318 _("unexpected opcode while processing relocation %s"),
6319 reloc_property->name().c_str());
6320 break;
6321 default:
6322 gold_unreachable();
6323 }
6324
6325 return true;
6326 }
6327
6328
6329 template<int size, bool big_endian>
6330 inline
6331 typename AArch64_relocate_functions<size, big_endian>::Status
6332 Target_aarch64<size, big_endian>::Relocate::relocate_tls(
6333 const Relocate_info<size, big_endian>* relinfo,
6334 Target_aarch64<size, big_endian>* target,
6335 size_t relnum,
6336 const elfcpp::Rela<size, big_endian>& rela,
6337 unsigned int r_type, const Sized_symbol<size>* gsym,
6338 const Symbol_value<size>* psymval,
6339 unsigned char* view,
6340 typename elfcpp::Elf_types<size>::Elf_Addr address)
6341 {
6342 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6343 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6344
6345 Output_segment* tls_segment = relinfo->layout->tls_segment();
6346 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6347 const AArch64_reloc_property* reloc_property =
6348 aarch64_reloc_property_table->get_reloc_property(r_type);
6349 gold_assert(reloc_property != NULL);
6350
6351 const bool is_final = (gsym == NULL
6352 ? !parameters->options().shared()
6353 : gsym->final_value_is_known());
6354 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6355 optimize_tls_reloc(is_final, r_type);
6356
6357 Sized_relobj_file<size, big_endian>* object = relinfo->object;
6358 int tls_got_offset_type;
6359 switch (r_type)
6360 {
6361 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6362 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // Global-dynamic
6363 {
6364 if (tlsopt == tls::TLSOPT_TO_LE)
6365 {
6366 if (tls_segment == NULL)
6367 {
6368 gold_assert(parameters->errors()->error_count() > 0
6369 || issue_undefined_symbol_error(gsym));
6370 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6371 }
6372 return tls_gd_to_le(relinfo, target, rela, r_type, view,
6373 psymval);
6374 }
6375 else if (tlsopt == tls::TLSOPT_NONE)
6376 {
6377 tls_got_offset_type = GOT_TYPE_TLS_PAIR;
6378 // Firstly get the address for the got entry.
6379 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6380 if (gsym != NULL)
6381 {
6382 gold_assert(gsym->has_got_offset(tls_got_offset_type));
6383 got_entry_address = target->got_->address() +
6384 gsym->got_offset(tls_got_offset_type);
6385 }
6386 else
6387 {
6388 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6389 gold_assert(
6390 object->local_has_got_offset(r_sym, tls_got_offset_type));
6391 got_entry_address = target->got_->address() +
6392 object->local_got_offset(r_sym, tls_got_offset_type);
6393 }
6394
6395 // Relocate the address into adrp/ld, adrp/add pair.
6396 switch (r_type)
6397 {
6398 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6399 return aarch64_reloc_funcs::adrp(
6400 view, got_entry_address + addend, address);
6401
6402 break;
6403
6404 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
6405 return aarch64_reloc_funcs::template rela_general<32>(
6406 view, got_entry_address, addend, reloc_property);
6407 break;
6408
6409 default:
6410 gold_unreachable();
6411 }
6412 }
6413 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6414 _("unsupported gd_to_ie relaxation on %u"),
6415 r_type);
6416 }
6417 break;
6418
6419 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6420 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local-dynamic
6421 {
6422 if (tlsopt == tls::TLSOPT_TO_LE)
6423 {
6424 if (tls_segment == NULL)
6425 {
6426 gold_assert(parameters->errors()->error_count() > 0
6427 || issue_undefined_symbol_error(gsym));
6428 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6429 }
6430 return this->tls_ld_to_le(relinfo, target, rela, r_type, view,
6431 psymval);
6432 }
6433
6434 gold_assert(tlsopt == tls::TLSOPT_NONE);
6435 // Relocate the field with the offset of the GOT entry for
6436 // the module index.
6437 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6438 got_entry_address = (target->got_mod_index_entry(NULL, NULL, NULL) +
6439 target->got_->address());
6440
6441 switch (r_type)
6442 {
6443 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6444 return aarch64_reloc_funcs::adrp(
6445 view, got_entry_address + addend, address);
6446 break;
6447
6448 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
6449 return aarch64_reloc_funcs::template rela_general<32>(
6450 view, got_entry_address, addend, reloc_property);
6451 break;
6452
6453 default:
6454 gold_unreachable();
6455 }
6456 }
6457 break;
6458
6459 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6460 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6461 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6462 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local-dynamic
6463 {
6464 AArch64_address value = psymval->value(object, 0);
6465 if (tlsopt == tls::TLSOPT_TO_LE)
6466 {
6467 if (tls_segment == NULL)
6468 {
6469 gold_assert(parameters->errors()->error_count() > 0
6470 || issue_undefined_symbol_error(gsym));
6471 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6472 }
6473 }
6474 switch (r_type)
6475 {
6476 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6477 return aarch64_reloc_funcs::movnz(view, value + addend,
6478 reloc_property);
6479 break;
6480
6481 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6482 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6483 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
6484 return aarch64_reloc_funcs::template rela_general<32>(
6485 view, value, addend, reloc_property);
6486 break;
6487
6488 default:
6489 gold_unreachable();
6490 }
6491 // We should never reach here.
6492 }
6493 break;
6494
6495 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6496 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial-exec
6497 {
6498 if (tlsopt == tls::TLSOPT_TO_LE)
6499 {
6500 if (tls_segment == NULL)
6501 {
6502 gold_assert(parameters->errors()->error_count() > 0
6503 || issue_undefined_symbol_error(gsym));
6504 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6505 }
6506 return tls_ie_to_le(relinfo, target, rela, r_type, view,
6507 psymval);
6508 }
6509 tls_got_offset_type = GOT_TYPE_TLS_OFFSET;
6510
6511 // Firstly get the address for the got entry.
6512 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6513 if (gsym != NULL)
6514 {
6515 gold_assert(gsym->has_got_offset(tls_got_offset_type));
6516 got_entry_address = target->got_->address() +
6517 gsym->got_offset(tls_got_offset_type);
6518 }
6519 else
6520 {
6521 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6522 gold_assert(
6523 object->local_has_got_offset(r_sym, tls_got_offset_type));
6524 got_entry_address = target->got_->address() +
6525 object->local_got_offset(r_sym, tls_got_offset_type);
6526 }
6527 // Relocate the address into adrp/ld, adrp/add pair.
6528 switch (r_type)
6529 {
6530 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6531 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
6532 address);
6533 break;
6534 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6535 return aarch64_reloc_funcs::template rela_general<32>(
6536 view, got_entry_address, addend, reloc_property);
6537 default:
6538 gold_unreachable();
6539 }
6540 }
6541 // We shall never reach here.
6542 break;
6543
6544 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6545 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6546 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6547 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6548 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6549 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6550 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6551 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6552 {
6553 gold_assert(tls_segment != NULL);
6554 AArch64_address value = psymval->value(object, 0);
6555
6556 if (!parameters->options().shared())
6557 {
6558 AArch64_address aligned_tcb_size =
6559 align_address(target->tcb_size(),
6560 tls_segment->maximum_alignment());
6561 value += aligned_tcb_size;
6562 switch (r_type)
6563 {
6564 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6565 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6566 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6567 return aarch64_reloc_funcs::movnz(view, value + addend,
6568 reloc_property);
6569 default:
6570 return aarch64_reloc_funcs::template
6571 rela_general<32>(view,
6572 value,
6573 addend,
6574 reloc_property);
6575 }
6576 }
6577 else
6578 gold_error(_("%s: unsupported reloc %u "
6579 "in non-static TLSLE mode."),
6580 object->name().c_str(), r_type);
6581 }
6582 break;
6583
6584 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6585 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6586 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6587 case elfcpp::R_AARCH64_TLSDESC_CALL:
6588 {
6589 if (tlsopt == tls::TLSOPT_TO_LE)
6590 {
6591 if (tls_segment == NULL)
6592 {
6593 gold_assert(parameters->errors()->error_count() > 0
6594 || issue_undefined_symbol_error(gsym));
6595 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6596 }
6597 return tls_desc_gd_to_le(relinfo, target, rela, r_type,
6598 view, psymval);
6599 }
6600 else
6601 {
6602 tls_got_offset_type = (tlsopt == tls::TLSOPT_TO_IE
6603 ? GOT_TYPE_TLS_OFFSET
6604 : GOT_TYPE_TLS_DESC);
6605 unsigned int got_tlsdesc_offset = 0;
6606 if (r_type != elfcpp::R_AARCH64_TLSDESC_CALL
6607 && tlsopt == tls::TLSOPT_NONE)
6608 {
6609 // We created GOT entries in the .got.tlsdesc portion of the
6610 // .got.plt section, but the offset stored in the symbol is the
6611 // offset within .got.tlsdesc.
6612 got_tlsdesc_offset = (target->got_->data_size()
6613 + target->got_plt_section()->data_size());
6614 }
6615 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6616 if (gsym != NULL)
6617 {
6618 gold_assert(gsym->has_got_offset(tls_got_offset_type));
6619 got_entry_address = target->got_->address()
6620 + got_tlsdesc_offset
6621 + gsym->got_offset(tls_got_offset_type);
6622 }
6623 else
6624 {
6625 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6626 gold_assert(
6627 object->local_has_got_offset(r_sym, tls_got_offset_type));
6628 got_entry_address = target->got_->address() +
6629 got_tlsdesc_offset +
6630 object->local_got_offset(r_sym, tls_got_offset_type);
6631 }
6632 if (tlsopt == tls::TLSOPT_TO_IE)
6633 {
6634 if (tls_segment == NULL)
6635 {
6636 gold_assert(parameters->errors()->error_count() > 0
6637 || issue_undefined_symbol_error(gsym));
6638 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6639 }
6640 return tls_desc_gd_to_ie(relinfo, target, rela, r_type,
6641 view, psymval, got_entry_address,
6642 address);
6643 }
6644
6645 // Now do tlsdesc relocation.
6646 switch (r_type)
6647 {
6648 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6649 return aarch64_reloc_funcs::adrp(view,
6650 got_entry_address + addend,
6651 address);
6652 break;
6653 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6654 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6655 return aarch64_reloc_funcs::template rela_general<32>(
6656 view, got_entry_address, addend, reloc_property);
6657 break;
6658 case elfcpp::R_AARCH64_TLSDESC_CALL:
6659 return aarch64_reloc_funcs::STATUS_OKAY;
6660 break;
6661 default:
6662 gold_unreachable();
6663 }
6664 }
6665 }
6666 break;
6667
6668 default:
6669 gold_error(_("%s: unsupported TLS reloc %u."),
6670 object->name().c_str(), r_type);
6671 }
6672 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6673 } // End of relocate_tls.
6674
6675
6676 template<int size, bool big_endian>
6677 inline
6678 typename AArch64_relocate_functions<size, big_endian>::Status
6679 Target_aarch64<size, big_endian>::Relocate::tls_gd_to_le(
6680 const Relocate_info<size, big_endian>* relinfo,
6681 Target_aarch64<size, big_endian>* target,
6682 const elfcpp::Rela<size, big_endian>& rela,
6683 unsigned int r_type,
6684 unsigned char* view,
6685 const Symbol_value<size>* psymval)
6686 {
6687 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6688 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6689 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6690
6691 Insntype* ip = reinterpret_cast<Insntype*>(view);
6692 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
6693 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
6694 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
6695
6696 if (r_type == elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC)
6697 {
6698 // This is the 2nd relocs, optimization should already have been
6699 // done.
6700 gold_assert((insn1 & 0xfff00000) == 0x91400000);
6701 return aarch64_reloc_funcs::STATUS_OKAY;
6702 }
6703
6704 // The original sequence is -
6705 // 90000000 adrp x0, 0 <main>
6706 // 91000000 add x0, x0, #0x0
6707 // 94000000 bl 0 <__tls_get_addr>
6708 // optimized to sequence -
6709 // d53bd040 mrs x0, tpidr_el0
6710 // 91400000 add x0, x0, #0x0, lsl #12
6711 // 91000000 add x0, x0, #0x0
6712
6713 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
6714 // encounter the first relocation "R_AARCH64_TLSGD_ADR_PAGE21". Because we
6715 // have to change "bl tls_get_addr", which does not have a corresponding tls
6716 // relocation type. So before proceeding, we need to make sure compiler
6717 // does not change the sequence.
6718 if(!(insn1 == 0x90000000 // adrp x0,0
6719 && insn2 == 0x91000000 // add x0, x0, #0x0
6720 && insn3 == 0x94000000)) // bl 0
6721 {
6722 // Ideally we should give up gd_to_le relaxation and do gd access.
6723 // However the gd_to_le relaxation decision has been made early
6724 // in the scan stage, where we did not allocate any GOT entry for
6725 // this symbol. Therefore we have to exit and report error now.
6726 gold_error(_("unexpected reloc insn sequence while relaxing "
6727 "tls gd to le for reloc %u."), r_type);
6728 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6729 }
6730
6731 // Write new insns.
6732 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
6733 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
6734 insn3 = 0x91000000; // add x0, x0, #0x0
6735 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
6736 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
6737 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
6738
6739 // Calculate tprel value.
6740 Output_segment* tls_segment = relinfo->layout->tls_segment();
6741 gold_assert(tls_segment != NULL);
6742 AArch64_address value = psymval->value(relinfo->object, 0);
6743 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6744 AArch64_address aligned_tcb_size =
6745 align_address(target->tcb_size(), tls_segment->maximum_alignment());
6746 AArch64_address x = value + aligned_tcb_size;
6747
6748 // After new insns are written, apply TLSLE relocs.
6749 const AArch64_reloc_property* rp1 =
6750 aarch64_reloc_property_table->get_reloc_property(
6751 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
6752 const AArch64_reloc_property* rp2 =
6753 aarch64_reloc_property_table->get_reloc_property(
6754 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
6755 gold_assert(rp1 != NULL && rp2 != NULL);
6756
6757 typename aarch64_reloc_funcs::Status s1 =
6758 aarch64_reloc_funcs::template rela_general<32>(view + 4,
6759 x,
6760 addend,
6761 rp1);
6762 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
6763 return s1;
6764
6765 typename aarch64_reloc_funcs::Status s2 =
6766 aarch64_reloc_funcs::template rela_general<32>(view + 8,
6767 x,
6768 addend,
6769 rp2);
6770
6771 this->skip_call_tls_get_addr_ = true;
6772 return s2;
6773 } // End of tls_gd_to_le
6774
6775
6776 template<int size, bool big_endian>
6777 inline
6778 typename AArch64_relocate_functions<size, big_endian>::Status
6779 Target_aarch64<size, big_endian>::Relocate::tls_ld_to_le(
6780 const Relocate_info<size, big_endian>* relinfo,
6781 Target_aarch64<size, big_endian>* target,
6782 const elfcpp::Rela<size, big_endian>& rela,
6783 unsigned int r_type,
6784 unsigned char* view,
6785 const Symbol_value<size>* psymval)
6786 {
6787 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6788 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6789 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6790
6791 Insntype* ip = reinterpret_cast<Insntype*>(view);
6792 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
6793 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
6794 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
6795
6796 if (r_type == elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC)
6797 {
6798 // This is the 2nd relocs, optimization should already have been
6799 // done.
6800 gold_assert((insn1 & 0xfff00000) == 0x91400000);
6801 return aarch64_reloc_funcs::STATUS_OKAY;
6802 }
6803
6804 // The original sequence is -
6805 // 90000000 adrp x0, 0 <main>
6806 // 91000000 add x0, x0, #0x0
6807 // 94000000 bl 0 <__tls_get_addr>
6808 // optimized to sequence -
6809 // d53bd040 mrs x0, tpidr_el0
6810 // 91400000 add x0, x0, #0x0, lsl #12
6811 // 91000000 add x0, x0, #0x0
6812
6813 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
6814 // encounter the first relocation "R_AARCH64_TLSLD_ADR_PAGE21". Because we
6815 // have to change "bl tls_get_addr", which does not have a corresponding tls
6816 // relocation type. So before proceeding, we need to make sure compiler
6817 // does not change the sequence.
6818 if(!(insn1 == 0x90000000 // adrp x0,0
6819 && insn2 == 0x91000000 // add x0, x0, #0x0
6820 && insn3 == 0x94000000)) // bl 0
6821 {
6822 // Ideally we should give up gd_to_le relaxation and do gd access.
6823 // However the gd_to_le relaxation decision has been made early
6824 // in the scan stage, where we did not allocate any GOT entry for
6825 // this symbol. Therefore we have to exit and report error now.
6826 gold_error(_("unexpected reloc insn sequence while relaxing "
6827 "tls gd to le for reloc %u."), r_type);
6828 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6829 }
6830
6831 // Write new insns.
6832 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
6833 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
6834 insn3 = 0x91000000; // add x0, x0, #0x0
6835 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
6836 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
6837 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
6838
6839 // Calculate tprel value.
6840 Output_segment* tls_segment = relinfo->layout->tls_segment();
6841 gold_assert(tls_segment != NULL);
6842 AArch64_address value = psymval->value(relinfo->object, 0);
6843 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6844 AArch64_address aligned_tcb_size =
6845 align_address(target->tcb_size(), tls_segment->maximum_alignment());
6846 AArch64_address x = value + aligned_tcb_size;
6847
6848 // After new insns are written, apply TLSLE relocs.
6849 const AArch64_reloc_property* rp1 =
6850 aarch64_reloc_property_table->get_reloc_property(
6851 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
6852 const AArch64_reloc_property* rp2 =
6853 aarch64_reloc_property_table->get_reloc_property(
6854 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
6855 gold_assert(rp1 != NULL && rp2 != NULL);
6856
6857 typename aarch64_reloc_funcs::Status s1 =
6858 aarch64_reloc_funcs::template rela_general<32>(view + 4,
6859 x,
6860 addend,
6861 rp1);
6862 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
6863 return s1;
6864
6865 typename aarch64_reloc_funcs::Status s2 =
6866 aarch64_reloc_funcs::template rela_general<32>(view + 8,
6867 x,
6868 addend,
6869 rp2);
6870
6871 this->skip_call_tls_get_addr_ = true;
6872 return s2;
6873
6874 } // End of tls_ld_to_le
6875
6876 template<int size, bool big_endian>
6877 inline
6878 typename AArch64_relocate_functions<size, big_endian>::Status
6879 Target_aarch64<size, big_endian>::Relocate::tls_ie_to_le(
6880 const Relocate_info<size, big_endian>* relinfo,
6881 Target_aarch64<size, big_endian>* target,
6882 const elfcpp::Rela<size, big_endian>& rela,
6883 unsigned int r_type,
6884 unsigned char* view,
6885 const Symbol_value<size>* psymval)
6886 {
6887 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6888 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6889 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6890
6891 AArch64_address value = psymval->value(relinfo->object, 0);
6892 Output_segment* tls_segment = relinfo->layout->tls_segment();
6893 AArch64_address aligned_tcb_address =
6894 align_address(target->tcb_size(), tls_segment->maximum_alignment());
6895 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6896 AArch64_address x = value + addend + aligned_tcb_address;
6897 // "x" is the offset to tp, we can only do this if x is within
6898 // range [0, 2^32-1]
6899 if (!(size == 32 || (size == 64 && (static_cast<uint64_t>(x) >> 32) == 0)))
6900 {
6901 gold_error(_("TLS variable referred by reloc %u is too far from TP."),
6902 r_type);
6903 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6904 }
6905
6906 Insntype* ip = reinterpret_cast<Insntype*>(view);
6907 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
6908 unsigned int regno;
6909 Insntype newinsn;
6910 if (r_type == elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21)
6911 {
6912 // Generate movz.
6913 regno = (insn & 0x1f);
6914 newinsn = (0xd2a00000 | regno) | (((x >> 16) & 0xffff) << 5);
6915 }
6916 else if (r_type == elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
6917 {
6918 // Generate movk.
6919 regno = (insn & 0x1f);
6920 gold_assert(regno == ((insn >> 5) & 0x1f));
6921 newinsn = (0xf2800000 | regno) | ((x & 0xffff) << 5);
6922 }
6923 else
6924 gold_unreachable();
6925
6926 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
6927 return aarch64_reloc_funcs::STATUS_OKAY;
6928 } // End of tls_ie_to_le
6929
6930
6931 template<int size, bool big_endian>
6932 inline
6933 typename AArch64_relocate_functions<size, big_endian>::Status
6934 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_le(
6935 const Relocate_info<size, big_endian>* relinfo,
6936 Target_aarch64<size, big_endian>* target,
6937 const elfcpp::Rela<size, big_endian>& rela,
6938 unsigned int r_type,
6939 unsigned char* view,
6940 const Symbol_value<size>* psymval)
6941 {
6942 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6943 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6944 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6945
6946 // TLSDESC-GD sequence is like:
6947 // adrp x0, :tlsdesc:v1
6948 // ldr x1, [x0, #:tlsdesc_lo12:v1]
6949 // add x0, x0, :tlsdesc_lo12:v1
6950 // .tlsdesccall v1
6951 // blr x1
6952 // After desc_gd_to_le optimization, the sequence will be like:
6953 // movz x0, #0x0, lsl #16
6954 // movk x0, #0x10
6955 // nop
6956 // nop
6957
6958 // Calculate tprel value.
6959 Output_segment* tls_segment = relinfo->layout->tls_segment();
6960 gold_assert(tls_segment != NULL);
6961 Insntype* ip = reinterpret_cast<Insntype*>(view);
6962 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6963 AArch64_address value = psymval->value(relinfo->object, addend);
6964 AArch64_address aligned_tcb_size =
6965 align_address(target->tcb_size(), tls_segment->maximum_alignment());
6966 AArch64_address x = value + aligned_tcb_size;
6967 // x is the offset to tp, we can only do this if x is within range
6968 // [0, 2^32-1]. If x is out of range, fail and exit.
6969 if (size == 64 && (static_cast<uint64_t>(x) >> 32) != 0)
6970 {
6971 gold_error(_("TLS variable referred by reloc %u is too far from TP. "
6972 "We Can't do gd_to_le relaxation.\n"), r_type);
6973 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6974 }
6975 Insntype newinsn;
6976 switch (r_type)
6977 {
6978 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6979 case elfcpp::R_AARCH64_TLSDESC_CALL:
6980 // Change to nop
6981 newinsn = 0xd503201f;
6982 break;
6983
6984 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6985 // Change to movz.
6986 newinsn = 0xd2a00000 | (((x >> 16) & 0xffff) << 5);
6987 break;
6988
6989 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6990 // Change to movk.
6991 newinsn = 0xf2800000 | ((x & 0xffff) << 5);
6992 break;
6993
6994 default:
6995 gold_error(_("unsupported tlsdesc gd_to_le optimization on reloc %u"),
6996 r_type);
6997 gold_unreachable();
6998 }
6999 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7000 return aarch64_reloc_funcs::STATUS_OKAY;
7001 } // End of tls_desc_gd_to_le
7002
7003
7004 template<int size, bool big_endian>
7005 inline
7006 typename AArch64_relocate_functions<size, big_endian>::Status
7007 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_ie(
7008 const Relocate_info<size, big_endian>* /* relinfo */,
7009 Target_aarch64<size, big_endian>* /* target */,
7010 const elfcpp::Rela<size, big_endian>& rela,
7011 unsigned int r_type,
7012 unsigned char* view,
7013 const Symbol_value<size>* /* psymval */,
7014 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address,
7015 typename elfcpp::Elf_types<size>::Elf_Addr address)
7016 {
7017 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7018 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7019
7020 // TLSDESC-GD sequence is like:
7021 // adrp x0, :tlsdesc:v1
7022 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7023 // add x0, x0, :tlsdesc_lo12:v1
7024 // .tlsdesccall v1
7025 // blr x1
7026 // After desc_gd_to_ie optimization, the sequence will be like:
7027 // adrp x0, :tlsie:v1
7028 // ldr x0, [x0, :tlsie_lo12:v1]
7029 // nop
7030 // nop
7031
7032 Insntype* ip = reinterpret_cast<Insntype*>(view);
7033 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7034 Insntype newinsn;
7035 switch (r_type)
7036 {
7037 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7038 case elfcpp::R_AARCH64_TLSDESC_CALL:
7039 // Change to nop
7040 newinsn = 0xd503201f;
7041 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7042 break;
7043
7044 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7045 {
7046 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
7047 address);
7048 }
7049 break;
7050
7051 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7052 {
7053 // Set ldr target register to be x0.
7054 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
7055 insn &= 0xffffffe0;
7056 elfcpp::Swap<32, big_endian>::writeval(ip, insn);
7057 // Do relocation.
7058 const AArch64_reloc_property* reloc_property =
7059 aarch64_reloc_property_table->get_reloc_property(
7060 elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
7061 return aarch64_reloc_funcs::template rela_general<32>(
7062 view, got_entry_address, addend, reloc_property);
7063 }
7064 break;
7065
7066 default:
7067 gold_error(_("Don't support tlsdesc gd_to_ie optimization on reloc %u"),
7068 r_type);
7069 gold_unreachable();
7070 }
7071 return aarch64_reloc_funcs::STATUS_OKAY;
7072 } // End of tls_desc_gd_to_ie
7073
7074 // Relocate section data.
7075
7076 template<int size, bool big_endian>
7077 void
7078 Target_aarch64<size, big_endian>::relocate_section(
7079 const Relocate_info<size, big_endian>* relinfo,
7080 unsigned int sh_type,
7081 const unsigned char* prelocs,
7082 size_t reloc_count,
7083 Output_section* output_section,
7084 bool needs_special_offset_handling,
7085 unsigned char* view,
7086 typename elfcpp::Elf_types<size>::Elf_Addr address,
7087 section_size_type view_size,
7088 const Reloc_symbol_changes* reloc_symbol_changes)
7089 {
7090 gold_assert(sh_type == elfcpp::SHT_RELA);
7091 typedef typename Target_aarch64<size, big_endian>::Relocate AArch64_relocate;
7092 gold::relocate_section<size, big_endian, Target_aarch64, elfcpp::SHT_RELA,
7093 AArch64_relocate, gold::Default_comdat_behavior>(
7094 relinfo,
7095 this,
7096 prelocs,
7097 reloc_count,
7098 output_section,
7099 needs_special_offset_handling,
7100 view,
7101 address,
7102 view_size,
7103 reloc_symbol_changes);
7104 }
7105
7106 // Return the size of a relocation while scanning during a relocatable
7107 // link.
7108
7109 template<int size, bool big_endian>
7110 unsigned int
7111 Target_aarch64<size, big_endian>::Relocatable_size_for_reloc::
7112 get_size_for_reloc(
7113 unsigned int ,
7114 Relobj* )
7115 {
7116 // We will never support SHT_REL relocations.
7117 gold_unreachable();
7118 return 0;
7119 }
7120
7121 // Scan the relocs during a relocatable link.
7122
7123 template<int size, bool big_endian>
7124 void
7125 Target_aarch64<size, big_endian>::scan_relocatable_relocs(
7126 Symbol_table* symtab,
7127 Layout* layout,
7128 Sized_relobj_file<size, big_endian>* object,
7129 unsigned int data_shndx,
7130 unsigned int sh_type,
7131 const unsigned char* prelocs,
7132 size_t reloc_count,
7133 Output_section* output_section,
7134 bool needs_special_offset_handling,
7135 size_t local_symbol_count,
7136 const unsigned char* plocal_symbols,
7137 Relocatable_relocs* rr)
7138 {
7139 gold_assert(sh_type == elfcpp::SHT_RELA);
7140
7141 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
7142 Relocatable_size_for_reloc> Scan_relocatable_relocs;
7143
7144 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
7145 Scan_relocatable_relocs>(
7146 symtab,
7147 layout,
7148 object,
7149 data_shndx,
7150 prelocs,
7151 reloc_count,
7152 output_section,
7153 needs_special_offset_handling,
7154 local_symbol_count,
7155 plocal_symbols,
7156 rr);
7157 }
7158
7159 // Relocate a section during a relocatable link.
7160
7161 template<int size, bool big_endian>
7162 void
7163 Target_aarch64<size, big_endian>::relocate_relocs(
7164 const Relocate_info<size, big_endian>* relinfo,
7165 unsigned int sh_type,
7166 const unsigned char* prelocs,
7167 size_t reloc_count,
7168 Output_section* output_section,
7169 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
7170 const Relocatable_relocs* rr,
7171 unsigned char* view,
7172 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
7173 section_size_type view_size,
7174 unsigned char* reloc_view,
7175 section_size_type reloc_view_size)
7176 {
7177 gold_assert(sh_type == elfcpp::SHT_RELA);
7178
7179 gold::relocate_relocs<size, big_endian, elfcpp::SHT_RELA>(
7180 relinfo,
7181 prelocs,
7182 reloc_count,
7183 output_section,
7184 offset_in_output_section,
7185 rr,
7186 view,
7187 view_address,
7188 view_size,
7189 reloc_view,
7190 reloc_view_size);
7191 }
7192
7193
7194 // Return whether this is a 3-insn erratum sequence.
7195
7196 template<int size, bool big_endian>
7197 bool
7198 Target_aarch64<size, big_endian>::is_erratum_843419_sequence(
7199 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
7200 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
7201 typename elfcpp::Swap<32,big_endian>::Valtype insn3)
7202 {
7203 unsigned rt1, rt2;
7204 bool load, pair;
7205
7206 // The 2nd insn is a single register load or store; or register pair
7207 // store.
7208 if (Insn_utilities::aarch64_mem_op_p(insn2, &rt1, &rt2, &pair, &load)
7209 && (!pair || (pair && !load)))
7210 {
7211 // The 3rd insn is a load or store instruction from the "Load/store
7212 // register (unsigned immediate)" encoding class, using Rn as the
7213 // base address register.
7214 if (Insn_utilities::aarch64_ldst_uimm(insn3)
7215 && (Insn_utilities::aarch64_rn(insn3)
7216 == Insn_utilities::aarch64_rd(insn1)))
7217 return true;
7218 }
7219 return false;
7220 }
7221
7222
7223 // Scan erratum for section SHNDX range
7224 // [output_address + span_start, output_address + span_end).
7225
7226 template<int size, bool big_endian>
7227 void
7228 Target_aarch64<size, big_endian>::scan_erratum_843419_span(
7229 AArch64_relobj<size, big_endian>* relobj,
7230 unsigned int shndx,
7231 const section_size_type span_start,
7232 const section_size_type span_end,
7233 unsigned char* input_view,
7234 Address output_address)
7235 {
7236 typedef typename Insn_utilities::Insntype Insntype;
7237
7238 // Adjust output_address and view to the start of span.
7239 output_address += span_start;
7240 input_view += span_start;
7241
7242 if ((output_address & 0x03) != 0)
7243 return;
7244
7245 section_size_type offset = 0;
7246 section_size_type span_length = span_end - span_start;
7247 // The first instruction must be ending at 0xFF8 or 0xFFC.
7248 unsigned int page_offset = output_address & 0xFFF;
7249 // Make sure starting position, that is "output_address+offset",
7250 // starts at page position 0xff8 or 0xffc.
7251 if (page_offset < 0xff8)
7252 offset = 0xff8 - page_offset;
7253 while (offset + 3 * Insn_utilities::BYTES_PER_INSN <= span_length)
7254 {
7255 Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
7256 Insntype insn1 = ip[0];
7257 if (Insn_utilities::is_adrp(insn1))
7258 {
7259 Insntype insn2 = ip[1];
7260 Insntype insn3 = ip[2];
7261 bool do_report = false;
7262 if (is_erratum_843419_sequence(insn1, insn2, insn3))
7263 do_report = true;
7264 else if (offset + 4 * Insn_utilities::BYTES_PER_INSN <= span_length)
7265 {
7266 // Optionally we can have an insn between ins2 and ins3
7267 Insntype insn_opt = ip[2];
7268 // And insn_opt must not be a branch.
7269 if (!Insn_utilities::aarch64_b(insn_opt)
7270 && !Insn_utilities::aarch64_bl(insn_opt)
7271 && !Insn_utilities::aarch64_blr(insn_opt)
7272 && !Insn_utilities::aarch64_br(insn_opt))
7273 {
7274 // And insn_opt must not write to dest reg in insn1. However
7275 // we do a conservative scan, which means we may fix/report
7276 // more than necessary, but it doesn't hurt.
7277
7278 Insntype insn4 = ip[3];
7279 if (is_erratum_843419_sequence(insn1, insn2, insn4))
7280 do_report = true;
7281 }
7282 }
7283 if (do_report)
7284 {
7285 gold_error(_("Erratum 943419 found at \"%s\", section %d, "
7286 "offset 0x%08x."),
7287 relobj->name().c_str(), shndx,
7288 (unsigned int)(span_start + offset));
7289 }
7290 }
7291
7292 // Advance to next candidate instruction. We only consider instruction
7293 // sequences starting at a page offset of 0xff8 or 0xffc.
7294 page_offset = (output_address + offset) & 0xfff;
7295 if (page_offset == 0xff8)
7296 offset += 4;
7297 else // (page_offset == 0xffc), we move to next page's 0xff8.
7298 offset += 0xffc;
7299 }
7300 }
7301
7302
7303 // The selector for aarch64 object files.
7304
7305 template<int size, bool big_endian>
7306 class Target_selector_aarch64 : public Target_selector
7307 {
7308 public:
7309 Target_selector_aarch64();
7310
7311 virtual Target*
7312 do_instantiate_target()
7313 { return new Target_aarch64<size, big_endian>(); }
7314 };
7315
7316 template<>
7317 Target_selector_aarch64<32, true>::Target_selector_aarch64()
7318 : Target_selector(elfcpp::EM_AARCH64, 32, true,
7319 "elf32-bigaarch64", "aarch64_elf32_be_vec")
7320 { }
7321
7322 template<>
7323 Target_selector_aarch64<32, false>::Target_selector_aarch64()
7324 : Target_selector(elfcpp::EM_AARCH64, 32, false,
7325 "elf32-littleaarch64", "aarch64_elf32_le_vec")
7326 { }
7327
7328 template<>
7329 Target_selector_aarch64<64, true>::Target_selector_aarch64()
7330 : Target_selector(elfcpp::EM_AARCH64, 64, true,
7331 "elf64-bigaarch64", "aarch64_elf64_be_vec")
7332 { }
7333
7334 template<>
7335 Target_selector_aarch64<64, false>::Target_selector_aarch64()
7336 : Target_selector(elfcpp::EM_AARCH64, 64, false,
7337 "elf64-littleaarch64", "aarch64_elf64_le_vec")
7338 { }
7339
7340 Target_selector_aarch64<32, true> target_selector_aarch64elf32b;
7341 Target_selector_aarch64<32, false> target_selector_aarch64elf32;
7342 Target_selector_aarch64<64, true> target_selector_aarch64elfb;
7343 Target_selector_aarch64<64, false> target_selector_aarch64elf;
7344
7345 } // End anonymous namespace.
This page took 0.27198 seconds and 5 git commands to generate.