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