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