10b57cec5SDimitry Andric //===- AArch64.cpp --------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
95f757f3fSDimitry Andric #include "InputFiles.h"
10bdd1243dSDimitry Andric #include "OutputSections.h"
110b57cec5SDimitry Andric #include "Symbols.h"
120b57cec5SDimitry Andric #include "SyntheticSections.h"
130b57cec5SDimitry Andric #include "Target.h"
140b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h"
1581ad6265SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
160b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric using namespace llvm;
190b57cec5SDimitry Andric using namespace llvm::support::endian;
200b57cec5SDimitry Andric using namespace llvm::ELF;
215ffd83dbSDimitry Andric using namespace lld;
225ffd83dbSDimitry Andric using namespace lld::elf;
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric // Page(Expr) is the page address of the expression Expr, defined
250b57cec5SDimitry Andric // as (Expr & ~0xFFF). (This applies even if the machine page size
260b57cec5SDimitry Andric // supported by the platform has a different value.)
getAArch64Page(uint64_t expr)275ffd83dbSDimitry Andric uint64_t elf::getAArch64Page(uint64_t expr) {
280b57cec5SDimitry Andric return expr & ~static_cast<uint64_t>(0xFFF);
290b57cec5SDimitry Andric }
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric namespace {
320b57cec5SDimitry Andric class AArch64 : public TargetInfo {
330b57cec5SDimitry Andric public:
340b57cec5SDimitry Andric AArch64();
350b57cec5SDimitry Andric RelExpr getRelExpr(RelType type, const Symbol &s,
360b57cec5SDimitry Andric const uint8_t *loc) const override;
370b57cec5SDimitry Andric RelType getDynRel(RelType type) const override;
38fe6060f1SDimitry Andric int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
390b57cec5SDimitry Andric void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
40bdd1243dSDimitry Andric void writeIgotPlt(uint8_t *buf, const Symbol &s) const override;
410b57cec5SDimitry Andric void writePltHeader(uint8_t *buf) const override;
42480093f4SDimitry Andric void writePlt(uint8_t *buf, const Symbol &sym,
43480093f4SDimitry Andric uint64_t pltEntryAddr) const override;
440b57cec5SDimitry Andric bool needsThunk(RelExpr expr, RelType type, const InputFile *file,
45480093f4SDimitry Andric uint64_t branchAddr, const Symbol &s,
46480093f4SDimitry Andric int64_t a) const override;
470b57cec5SDimitry Andric uint32_t getThunkSectionSpacing() const override;
480b57cec5SDimitry Andric bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
490b57cec5SDimitry Andric bool usesOnlyLowPageBits(RelType type) const override;
505ffd83dbSDimitry Andric void relocate(uint8_t *loc, const Relocation &rel,
515ffd83dbSDimitry Andric uint64_t val) const override;
52e8d8bef9SDimitry Andric RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;
53bdd1243dSDimitry Andric void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
54bdd1243dSDimitry Andric
55bdd1243dSDimitry Andric private:
56bdd1243dSDimitry Andric void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
57bdd1243dSDimitry Andric void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
58bdd1243dSDimitry Andric void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
59bdd1243dSDimitry Andric };
60bdd1243dSDimitry Andric
61bdd1243dSDimitry Andric struct AArch64Relaxer {
62bdd1243dSDimitry Andric bool safeToRelaxAdrpLdr = false;
63bdd1243dSDimitry Andric
64bdd1243dSDimitry Andric AArch64Relaxer(ArrayRef<Relocation> relocs);
65bdd1243dSDimitry Andric bool tryRelaxAdrpAdd(const Relocation &adrpRel, const Relocation &addRel,
66bdd1243dSDimitry Andric uint64_t secAddr, uint8_t *buf) const;
67bdd1243dSDimitry Andric bool tryRelaxAdrpLdr(const Relocation &adrpRel, const Relocation &ldrRel,
68bdd1243dSDimitry Andric uint64_t secAddr, uint8_t *buf) const;
690b57cec5SDimitry Andric };
700b57cec5SDimitry Andric } // namespace
710b57cec5SDimitry Andric
72*0fca6ea1SDimitry Andric // Return the bits [Start, End] from Val shifted Start bits.
73*0fca6ea1SDimitry Andric // For instance, getBits(0xF0, 4, 8) returns 0xF.
getBits(uint64_t val,int start,int end)74*0fca6ea1SDimitry Andric static uint64_t getBits(uint64_t val, int start, int end) {
75*0fca6ea1SDimitry Andric uint64_t mask = ((uint64_t)1 << (end + 1 - start)) - 1;
76*0fca6ea1SDimitry Andric return (val >> start) & mask;
77*0fca6ea1SDimitry Andric }
78*0fca6ea1SDimitry Andric
AArch64()790b57cec5SDimitry Andric AArch64::AArch64() {
800b57cec5SDimitry Andric copyRel = R_AARCH64_COPY;
810b57cec5SDimitry Andric relativeRel = R_AARCH64_RELATIVE;
820b57cec5SDimitry Andric iRelativeRel = R_AARCH64_IRELATIVE;
830b57cec5SDimitry Andric gotRel = R_AARCH64_GLOB_DAT;
840b57cec5SDimitry Andric pltRel = R_AARCH64_JUMP_SLOT;
850b57cec5SDimitry Andric symbolicRel = R_AARCH64_ABS64;
860b57cec5SDimitry Andric tlsDescRel = R_AARCH64_TLSDESC;
870b57cec5SDimitry Andric tlsGotRel = R_AARCH64_TLS_TPREL64;
880b57cec5SDimitry Andric pltHeaderSize = 32;
89480093f4SDimitry Andric pltEntrySize = 16;
90480093f4SDimitry Andric ipltEntrySize = 16;
910b57cec5SDimitry Andric defaultMaxPageSize = 65536;
920b57cec5SDimitry Andric
930b57cec5SDimitry Andric // Align to the 2 MiB page size (known as a superpage or huge page).
940b57cec5SDimitry Andric // FreeBSD automatically promotes 2 MiB-aligned allocations.
950b57cec5SDimitry Andric defaultImageBase = 0x200000;
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric needsThunks = true;
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric
getRelExpr(RelType type,const Symbol & s,const uint8_t * loc) const1000b57cec5SDimitry Andric RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
1010b57cec5SDimitry Andric const uint8_t *loc) const {
1020b57cec5SDimitry Andric switch (type) {
10385868e8aSDimitry Andric case R_AARCH64_ABS16:
10485868e8aSDimitry Andric case R_AARCH64_ABS32:
10585868e8aSDimitry Andric case R_AARCH64_ABS64:
10685868e8aSDimitry Andric case R_AARCH64_ADD_ABS_LO12_NC:
10785868e8aSDimitry Andric case R_AARCH64_LDST128_ABS_LO12_NC:
10885868e8aSDimitry Andric case R_AARCH64_LDST16_ABS_LO12_NC:
10985868e8aSDimitry Andric case R_AARCH64_LDST32_ABS_LO12_NC:
11085868e8aSDimitry Andric case R_AARCH64_LDST64_ABS_LO12_NC:
11185868e8aSDimitry Andric case R_AARCH64_LDST8_ABS_LO12_NC:
11285868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G0:
11385868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G1:
11485868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G2:
11585868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G0:
11685868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G0_NC:
11785868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G1:
11885868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G1_NC:
11985868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G2:
12085868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G2_NC:
12185868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G3:
12285868e8aSDimitry Andric return R_ABS;
123*0fca6ea1SDimitry Andric case R_AARCH64_AUTH_ABS64:
124*0fca6ea1SDimitry Andric return R_AARCH64_AUTH;
1250b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21:
1260b57cec5SDimitry Andric return R_AARCH64_TLSDESC_PAGE;
1270b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12:
1280b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12:
1290b57cec5SDimitry Andric return R_TLSDESC;
1300b57cec5SDimitry Andric case R_AARCH64_TLSDESC_CALL:
1310b57cec5SDimitry Andric return R_TLSDESC_CALL;
1320b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1330b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1340b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
1350b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
1360b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
1370b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
1380b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC:
13985868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0:
14085868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
14185868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1:
14285868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
14385868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G2:
144e8d8bef9SDimitry Andric return R_TPREL;
1450b57cec5SDimitry Andric case R_AARCH64_CALL26:
1460b57cec5SDimitry Andric case R_AARCH64_CONDBR19:
1470b57cec5SDimitry Andric case R_AARCH64_JUMP26:
1480b57cec5SDimitry Andric case R_AARCH64_TSTBR14:
14906c3fb27SDimitry Andric return R_PLT_PC;
1505ffd83dbSDimitry Andric case R_AARCH64_PLT32:
15106c3fb27SDimitry Andric const_cast<Symbol &>(s).thunkAccessed = true;
1520b57cec5SDimitry Andric return R_PLT_PC;
1530b57cec5SDimitry Andric case R_AARCH64_PREL16:
1540b57cec5SDimitry Andric case R_AARCH64_PREL32:
1550b57cec5SDimitry Andric case R_AARCH64_PREL64:
1560b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_LO21:
1570b57cec5SDimitry Andric case R_AARCH64_LD_PREL_LO19:
15885868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0:
15985868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0_NC:
16085868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1:
16185868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1_NC:
16285868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2:
16385868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2_NC:
16485868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G3:
1650b57cec5SDimitry Andric return R_PC;
1660b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21:
1670b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21_NC:
1680b57cec5SDimitry Andric return R_AARCH64_PAGE_PC;
1690b57cec5SDimitry Andric case R_AARCH64_LD64_GOT_LO12_NC:
1700b57cec5SDimitry Andric case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1710b57cec5SDimitry Andric return R_GOT;
172e8d8bef9SDimitry Andric case R_AARCH64_LD64_GOTPAGE_LO15:
173e8d8bef9SDimitry Andric return R_AARCH64_GOT_PAGE;
1740b57cec5SDimitry Andric case R_AARCH64_ADR_GOT_PAGE:
1750b57cec5SDimitry Andric case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1760b57cec5SDimitry Andric return R_AARCH64_GOT_PAGE_PC;
177297eecfbSDimitry Andric case R_AARCH64_GOTPCREL32:
178*0fca6ea1SDimitry Andric case R_AARCH64_GOT_LD_PREL19:
179297eecfbSDimitry Andric return R_GOT_PC;
1800b57cec5SDimitry Andric case R_AARCH64_NONE:
1810b57cec5SDimitry Andric return R_NONE;
1820b57cec5SDimitry Andric default:
18385868e8aSDimitry Andric error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
18485868e8aSDimitry Andric ") against symbol " + toString(s));
18585868e8aSDimitry Andric return R_NONE;
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric
adjustTlsExpr(RelType type,RelExpr expr) const189e8d8bef9SDimitry Andric RelExpr AArch64::adjustTlsExpr(RelType type, RelExpr expr) const {
1900b57cec5SDimitry Andric if (expr == R_RELAX_TLS_GD_TO_IE) {
1910b57cec5SDimitry Andric if (type == R_AARCH64_TLSDESC_ADR_PAGE21)
1920b57cec5SDimitry Andric return R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC;
1930b57cec5SDimitry Andric return R_RELAX_TLS_GD_TO_IE_ABS;
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric return expr;
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric
usesOnlyLowPageBits(RelType type) const1980b57cec5SDimitry Andric bool AArch64::usesOnlyLowPageBits(RelType type) const {
1990b57cec5SDimitry Andric switch (type) {
2000b57cec5SDimitry Andric default:
2010b57cec5SDimitry Andric return false;
2020b57cec5SDimitry Andric case R_AARCH64_ADD_ABS_LO12_NC:
2030b57cec5SDimitry Andric case R_AARCH64_LD64_GOT_LO12_NC:
2040b57cec5SDimitry Andric case R_AARCH64_LDST128_ABS_LO12_NC:
2050b57cec5SDimitry Andric case R_AARCH64_LDST16_ABS_LO12_NC:
2060b57cec5SDimitry Andric case R_AARCH64_LDST32_ABS_LO12_NC:
2070b57cec5SDimitry Andric case R_AARCH64_LDST64_ABS_LO12_NC:
2080b57cec5SDimitry Andric case R_AARCH64_LDST8_ABS_LO12_NC:
2090b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12:
2100b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12:
2110b57cec5SDimitry Andric case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
2120b57cec5SDimitry Andric return true;
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric }
2150b57cec5SDimitry Andric
getDynRel(RelType type) const2160b57cec5SDimitry Andric RelType AArch64::getDynRel(RelType type) const {
217*0fca6ea1SDimitry Andric if (type == R_AARCH64_ABS64 || type == R_AARCH64_AUTH_ABS64)
2180b57cec5SDimitry Andric return type;
2190b57cec5SDimitry Andric return R_AARCH64_NONE;
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric
getImplicitAddend(const uint8_t * buf,RelType type) const222fe6060f1SDimitry Andric int64_t AArch64::getImplicitAddend(const uint8_t *buf, RelType type) const {
223fe6060f1SDimitry Andric switch (type) {
224fe6060f1SDimitry Andric case R_AARCH64_TLSDESC:
225fe6060f1SDimitry Andric return read64(buf + 8);
226298c3e8dSDimitry Andric case R_AARCH64_NONE:
227bdd1243dSDimitry Andric case R_AARCH64_GLOB_DAT:
228bdd1243dSDimitry Andric case R_AARCH64_JUMP_SLOT:
229298c3e8dSDimitry Andric return 0;
230*0fca6ea1SDimitry Andric case R_AARCH64_ABS16:
231*0fca6ea1SDimitry Andric case R_AARCH64_PREL16:
232*0fca6ea1SDimitry Andric return SignExtend64<16>(read16(buf));
233*0fca6ea1SDimitry Andric case R_AARCH64_ABS32:
234298c3e8dSDimitry Andric case R_AARCH64_PREL32:
235298c3e8dSDimitry Andric return SignExtend64<32>(read32(buf));
236298c3e8dSDimitry Andric case R_AARCH64_ABS64:
237298c3e8dSDimitry Andric case R_AARCH64_PREL64:
238bdd1243dSDimitry Andric case R_AARCH64_RELATIVE:
239bdd1243dSDimitry Andric case R_AARCH64_IRELATIVE:
240bdd1243dSDimitry Andric case R_AARCH64_TLS_TPREL64:
241298c3e8dSDimitry Andric return read64(buf);
242*0fca6ea1SDimitry Andric
243*0fca6ea1SDimitry Andric // The following relocation types all point at instructions, and
244*0fca6ea1SDimitry Andric // relocate an immediate field in the instruction.
245*0fca6ea1SDimitry Andric //
246*0fca6ea1SDimitry Andric // The general rule, from AAELF64 §5.7.2 "Addends and PC-bias",
247*0fca6ea1SDimitry Andric // says: "If the relocation relocates an instruction the immediate
248*0fca6ea1SDimitry Andric // field of the instruction is extracted, scaled as required by
249*0fca6ea1SDimitry Andric // the instruction field encoding, and sign-extended to 64 bits".
250*0fca6ea1SDimitry Andric
251*0fca6ea1SDimitry Andric // The R_AARCH64_MOVW family operates on wide MOV/MOVK/MOVZ
252*0fca6ea1SDimitry Andric // instructions, which have a 16-bit immediate field with its low
253*0fca6ea1SDimitry Andric // bit in bit 5 of the instruction encoding. When the immediate
254*0fca6ea1SDimitry Andric // field is used as an implicit addend for REL-type relocations,
255*0fca6ea1SDimitry Andric // it is treated as added to the low bits of the output value, not
256*0fca6ea1SDimitry Andric // shifted depending on the relocation type.
257*0fca6ea1SDimitry Andric //
258*0fca6ea1SDimitry Andric // This allows REL relocations to express the requirement 'please
259*0fca6ea1SDimitry Andric // add 12345 to this symbol value and give me the four 16-bit
260*0fca6ea1SDimitry Andric // chunks of the result', by putting the same addend 12345 in all
261*0fca6ea1SDimitry Andric // four instructions. Carries between the 16-bit chunks are
262*0fca6ea1SDimitry Andric // handled correctly, because the whole 64-bit addition is done
263*0fca6ea1SDimitry Andric // once per relocation.
264*0fca6ea1SDimitry Andric case R_AARCH64_MOVW_UABS_G0:
265*0fca6ea1SDimitry Andric case R_AARCH64_MOVW_UABS_G0_NC:
266*0fca6ea1SDimitry Andric case R_AARCH64_MOVW_UABS_G1:
267*0fca6ea1SDimitry Andric case R_AARCH64_MOVW_UABS_G1_NC:
268*0fca6ea1SDimitry Andric case R_AARCH64_MOVW_UABS_G2:
269*0fca6ea1SDimitry Andric case R_AARCH64_MOVW_UABS_G2_NC:
270*0fca6ea1SDimitry Andric case R_AARCH64_MOVW_UABS_G3:
271*0fca6ea1SDimitry Andric return SignExtend64<16>(getBits(read32(buf), 5, 20));
272*0fca6ea1SDimitry Andric
273*0fca6ea1SDimitry Andric // R_AARCH64_TSTBR14 points at a TBZ or TBNZ instruction, which
274*0fca6ea1SDimitry Andric // has a 14-bit offset measured in instructions, i.e. shifted left
275*0fca6ea1SDimitry Andric // by 2.
276*0fca6ea1SDimitry Andric case R_AARCH64_TSTBR14:
277*0fca6ea1SDimitry Andric return SignExtend64<16>(getBits(read32(buf), 5, 18) << 2);
278*0fca6ea1SDimitry Andric
279*0fca6ea1SDimitry Andric // R_AARCH64_CONDBR19 operates on the ordinary B.cond instruction,
280*0fca6ea1SDimitry Andric // which has a 19-bit offset measured in instructions.
281*0fca6ea1SDimitry Andric //
282*0fca6ea1SDimitry Andric // R_AARCH64_LD_PREL_LO19 operates on the LDR (literal)
283*0fca6ea1SDimitry Andric // instruction, which also has a 19-bit offset, measured in 4-byte
284*0fca6ea1SDimitry Andric // chunks. So the calculation is the same as for
285*0fca6ea1SDimitry Andric // R_AARCH64_CONDBR19.
286*0fca6ea1SDimitry Andric case R_AARCH64_CONDBR19:
287*0fca6ea1SDimitry Andric case R_AARCH64_LD_PREL_LO19:
288*0fca6ea1SDimitry Andric return SignExtend64<21>(getBits(read32(buf), 5, 23) << 2);
289*0fca6ea1SDimitry Andric
290*0fca6ea1SDimitry Andric // R_AARCH64_ADD_ABS_LO12_NC operates on ADD (immediate). The
291*0fca6ea1SDimitry Andric // immediate can optionally be shifted left by 12 bits, but this
292*0fca6ea1SDimitry Andric // relocation is intended for the case where it is not.
293*0fca6ea1SDimitry Andric case R_AARCH64_ADD_ABS_LO12_NC:
294*0fca6ea1SDimitry Andric return SignExtend64<12>(getBits(read32(buf), 10, 21));
295*0fca6ea1SDimitry Andric
296*0fca6ea1SDimitry Andric // R_AARCH64_ADR_PREL_LO21 operates on an ADR instruction, whose
297*0fca6ea1SDimitry Andric // 21-bit immediate is split between two bits high up in the word
298*0fca6ea1SDimitry Andric // (in fact the two _lowest_ order bits of the value) and 19 bits
299*0fca6ea1SDimitry Andric // lower down.
300*0fca6ea1SDimitry Andric //
301*0fca6ea1SDimitry Andric // R_AARCH64_ADR_PREL_PG_HI21[_NC] operate on an ADRP instruction,
302*0fca6ea1SDimitry Andric // which encodes the immediate in the same way, but will shift it
303*0fca6ea1SDimitry Andric // left by 12 bits when the instruction executes. For the same
304*0fca6ea1SDimitry Andric // reason as the MOVW family, we don't apply that left shift here.
305*0fca6ea1SDimitry Andric case R_AARCH64_ADR_PREL_LO21:
306*0fca6ea1SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21:
307*0fca6ea1SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21_NC:
308*0fca6ea1SDimitry Andric return SignExtend64<21>((getBits(read32(buf), 5, 23) << 2) |
309*0fca6ea1SDimitry Andric getBits(read32(buf), 29, 30));
310*0fca6ea1SDimitry Andric
311*0fca6ea1SDimitry Andric // R_AARCH64_{JUMP,CALL}26 operate on B and BL, which have a
312*0fca6ea1SDimitry Andric // 26-bit offset measured in instructions.
313*0fca6ea1SDimitry Andric case R_AARCH64_JUMP26:
314*0fca6ea1SDimitry Andric case R_AARCH64_CALL26:
315*0fca6ea1SDimitry Andric return SignExtend64<28>(getBits(read32(buf), 0, 25) << 2);
316*0fca6ea1SDimitry Andric
317fe6060f1SDimitry Andric default:
318fe6060f1SDimitry Andric internalLinkerError(getErrorLocation(buf),
319fe6060f1SDimitry Andric "cannot read addend for relocation " + toString(type));
320fe6060f1SDimitry Andric return 0;
321fe6060f1SDimitry Andric }
322fe6060f1SDimitry Andric }
323fe6060f1SDimitry Andric
writeGotPlt(uint8_t * buf,const Symbol &) const3240b57cec5SDimitry Andric void AArch64::writeGotPlt(uint8_t *buf, const Symbol &) const {
325fe6060f1SDimitry Andric write64(buf, in.plt->getVA());
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric
writeIgotPlt(uint8_t * buf,const Symbol & s) const328bdd1243dSDimitry Andric void AArch64::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
329bdd1243dSDimitry Andric if (config->writeAddends)
330bdd1243dSDimitry Andric write64(buf, s.getVA());
331bdd1243dSDimitry Andric }
332bdd1243dSDimitry Andric
writePltHeader(uint8_t * buf) const3330b57cec5SDimitry Andric void AArch64::writePltHeader(uint8_t *buf) const {
3340b57cec5SDimitry Andric const uint8_t pltData[] = {
3350b57cec5SDimitry Andric 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
336bdd1243dSDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.got.plt[2]))
337bdd1243dSDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.got.plt[2]))]
338bdd1243dSDimitry Andric 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.got.plt[2]))
3390b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6, // br x17
3400b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5, // nop
3410b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5, // nop
3420b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5 // nop
3430b57cec5SDimitry Andric };
3440b57cec5SDimitry Andric memcpy(buf, pltData, sizeof(pltData));
3450b57cec5SDimitry Andric
3460b57cec5SDimitry Andric uint64_t got = in.gotPlt->getVA();
3470b57cec5SDimitry Andric uint64_t plt = in.plt->getVA();
3485ffd83dbSDimitry Andric relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
3490b57cec5SDimitry Andric getAArch64Page(got + 16) - getAArch64Page(plt + 4));
3505ffd83dbSDimitry Andric relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16);
3515ffd83dbSDimitry Andric relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16);
3520b57cec5SDimitry Andric }
3530b57cec5SDimitry Andric
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr) const354480093f4SDimitry Andric void AArch64::writePlt(uint8_t *buf, const Symbol &sym,
355480093f4SDimitry Andric uint64_t pltEntryAddr) const {
3560b57cec5SDimitry Andric const uint8_t inst[] = {
357bdd1243dSDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.got.plt[n]))
358bdd1243dSDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.got.plt[n]))]
359bdd1243dSDimitry Andric 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.got.plt[n]))
3600b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6 // br x17
3610b57cec5SDimitry Andric };
3620b57cec5SDimitry Andric memcpy(buf, inst, sizeof(inst));
3630b57cec5SDimitry Andric
364480093f4SDimitry Andric uint64_t gotPltEntryAddr = sym.getGotPltVA();
3655ffd83dbSDimitry Andric relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
3660b57cec5SDimitry Andric getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr));
3675ffd83dbSDimitry Andric relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr);
3685ffd83dbSDimitry Andric relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr);
3690b57cec5SDimitry Andric }
3700b57cec5SDimitry Andric
needsThunk(RelExpr expr,RelType type,const InputFile * file,uint64_t branchAddr,const Symbol & s,int64_t a) const3710b57cec5SDimitry Andric bool AArch64::needsThunk(RelExpr expr, RelType type, const InputFile *file,
372480093f4SDimitry Andric uint64_t branchAddr, const Symbol &s,
373480093f4SDimitry Andric int64_t a) const {
3742a66634dSDimitry Andric // If s is an undefined weak symbol and does not have a PLT entry then it will
3752a66634dSDimitry Andric // be resolved as a branch to the next instruction. If it is hidden, its
3762a66634dSDimitry Andric // binding has been converted to local, so we just check isUndefined() here. A
3772a66634dSDimitry Andric // undefined non-weak symbol will have been errored.
3782a66634dSDimitry Andric if (s.isUndefined() && !s.isInPlt())
379480093f4SDimitry Andric return false;
3800b57cec5SDimitry Andric // ELF for the ARM 64-bit architecture, section Call and Jump relocations
3810b57cec5SDimitry Andric // only permits range extension thunks for R_AARCH64_CALL26 and
3820b57cec5SDimitry Andric // R_AARCH64_JUMP26 relocation types.
3835ffd83dbSDimitry Andric if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&
3845ffd83dbSDimitry Andric type != R_AARCH64_PLT32)
3850b57cec5SDimitry Andric return false;
386480093f4SDimitry Andric uint64_t dst = expr == R_PLT_PC ? s.getPltVA() : s.getVA(a);
3870b57cec5SDimitry Andric return !inBranchRange(type, branchAddr, dst);
3880b57cec5SDimitry Andric }
3890b57cec5SDimitry Andric
getThunkSectionSpacing() const3900b57cec5SDimitry Andric uint32_t AArch64::getThunkSectionSpacing() const {
3910b57cec5SDimitry Andric // See comment in Arch/ARM.cpp for a more detailed explanation of
3920b57cec5SDimitry Andric // getThunkSectionSpacing(). For AArch64 the only branches we are permitted to
3930b57cec5SDimitry Andric // Thunk have a range of +/- 128 MiB
3940b57cec5SDimitry Andric return (128 * 1024 * 1024) - 0x30000;
3950b57cec5SDimitry Andric }
3960b57cec5SDimitry Andric
inBranchRange(RelType type,uint64_t src,uint64_t dst) const3970b57cec5SDimitry Andric bool AArch64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
3985ffd83dbSDimitry Andric if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&
3995ffd83dbSDimitry Andric type != R_AARCH64_PLT32)
4000b57cec5SDimitry Andric return true;
4010b57cec5SDimitry Andric // The AArch64 call and unconditional branch instructions have a range of
4025ffd83dbSDimitry Andric // +/- 128 MiB. The PLT32 relocation supports a range up to +/- 2 GiB.
4035ffd83dbSDimitry Andric uint64_t range =
4045ffd83dbSDimitry Andric type == R_AARCH64_PLT32 ? (UINT64_C(1) << 31) : (128 * 1024 * 1024);
4050b57cec5SDimitry Andric if (dst > src) {
4060b57cec5SDimitry Andric // Immediate of branch is signed.
4070b57cec5SDimitry Andric range -= 4;
4080b57cec5SDimitry Andric return dst - src <= range;
4090b57cec5SDimitry Andric }
4100b57cec5SDimitry Andric return src - dst <= range;
4110b57cec5SDimitry Andric }
4120b57cec5SDimitry Andric
write32AArch64Addr(uint8_t * l,uint64_t imm)4130b57cec5SDimitry Andric static void write32AArch64Addr(uint8_t *l, uint64_t imm) {
4140b57cec5SDimitry Andric uint32_t immLo = (imm & 0x3) << 29;
4150b57cec5SDimitry Andric uint32_t immHi = (imm & 0x1FFFFC) << 3;
4160b57cec5SDimitry Andric uint64_t mask = (0x3 << 29) | (0x1FFFFC << 3);
4170b57cec5SDimitry Andric write32le(l, (read32le(l) & ~mask) | immLo | immHi);
4180b57cec5SDimitry Andric }
4190b57cec5SDimitry Andric
writeMaskedBits32le(uint8_t * p,int32_t v,uint32_t mask)420*0fca6ea1SDimitry Andric static void writeMaskedBits32le(uint8_t *p, int32_t v, uint32_t mask) {
421*0fca6ea1SDimitry Andric write32le(p, (read32le(p) & ~mask) | v);
4220b57cec5SDimitry Andric }
4230b57cec5SDimitry Andric
4240b57cec5SDimitry Andric // Update the immediate field in a AARCH64 ldr, str, and add instruction.
write32Imm12(uint8_t * l,uint64_t imm)425*0fca6ea1SDimitry Andric static void write32Imm12(uint8_t *l, uint64_t imm) {
426*0fca6ea1SDimitry Andric writeMaskedBits32le(l, (imm & 0xFFF) << 10, 0xFFF << 10);
4270b57cec5SDimitry Andric }
4280b57cec5SDimitry Andric
42985868e8aSDimitry Andric // Update the immediate field in an AArch64 movk, movn or movz instruction
43085868e8aSDimitry Andric // for a signed relocation, and update the opcode of a movn or movz instruction
43185868e8aSDimitry Andric // to match the sign of the operand.
writeSMovWImm(uint8_t * loc,uint32_t imm)43285868e8aSDimitry Andric static void writeSMovWImm(uint8_t *loc, uint32_t imm) {
43385868e8aSDimitry Andric uint32_t inst = read32le(loc);
43485868e8aSDimitry Andric // Opcode field is bits 30, 29, with 10 = movz, 00 = movn and 11 = movk.
43585868e8aSDimitry Andric if (!(inst & (1 << 29))) {
43685868e8aSDimitry Andric // movn or movz.
43785868e8aSDimitry Andric if (imm & 0x10000) {
43885868e8aSDimitry Andric // Change opcode to movn, which takes an inverted operand.
43985868e8aSDimitry Andric imm ^= 0xFFFF;
44085868e8aSDimitry Andric inst &= ~(1 << 30);
44185868e8aSDimitry Andric } else {
44285868e8aSDimitry Andric // Change opcode to movz.
44385868e8aSDimitry Andric inst |= 1 << 30;
44485868e8aSDimitry Andric }
44585868e8aSDimitry Andric }
44685868e8aSDimitry Andric write32le(loc, inst | ((imm & 0xFFFF) << 5));
44785868e8aSDimitry Andric }
44885868e8aSDimitry Andric
relocate(uint8_t * loc,const Relocation & rel,uint64_t val) const4495ffd83dbSDimitry Andric void AArch64::relocate(uint8_t *loc, const Relocation &rel,
4505ffd83dbSDimitry Andric uint64_t val) const {
4515ffd83dbSDimitry Andric switch (rel.type) {
4520b57cec5SDimitry Andric case R_AARCH64_ABS16:
4530b57cec5SDimitry Andric case R_AARCH64_PREL16:
4545ffd83dbSDimitry Andric checkIntUInt(loc, val, 16, rel);
455fe6060f1SDimitry Andric write16(loc, val);
4560b57cec5SDimitry Andric break;
4570b57cec5SDimitry Andric case R_AARCH64_ABS32:
4580b57cec5SDimitry Andric case R_AARCH64_PREL32:
4595ffd83dbSDimitry Andric checkIntUInt(loc, val, 32, rel);
460fe6060f1SDimitry Andric write32(loc, val);
4615ffd83dbSDimitry Andric break;
4625ffd83dbSDimitry Andric case R_AARCH64_PLT32:
463297eecfbSDimitry Andric case R_AARCH64_GOTPCREL32:
4645ffd83dbSDimitry Andric checkInt(loc, val, 32, rel);
465fe6060f1SDimitry Andric write32(loc, val);
4660b57cec5SDimitry Andric break;
4670b57cec5SDimitry Andric case R_AARCH64_ABS64:
4685f757f3fSDimitry Andric // AArch64 relocations to tagged symbols have extended semantics, as
4695f757f3fSDimitry Andric // described here:
4705f757f3fSDimitry Andric // https://github.com/ARM-software/abi-aa/blob/main/memtagabielf64/memtagabielf64.rst#841extended-semantics-of-r_aarch64_relative.
4715f757f3fSDimitry Andric // tl;dr: encode the symbol's special addend in the place, which is an
4725f757f3fSDimitry Andric // offset to the point where the logical tag is derived from. Quick hack, if
4735f757f3fSDimitry Andric // the addend is within the symbol's bounds, no need to encode the tag
4745f757f3fSDimitry Andric // derivation offset.
4755f757f3fSDimitry Andric if (rel.sym && rel.sym->isTagged() &&
4765f757f3fSDimitry Andric (rel.addend < 0 ||
4775f757f3fSDimitry Andric rel.addend >= static_cast<int64_t>(rel.sym->getSize())))
4785f757f3fSDimitry Andric write64(loc, -rel.addend);
4795f757f3fSDimitry Andric else
4805f757f3fSDimitry Andric write64(loc, val);
4815f757f3fSDimitry Andric break;
4820b57cec5SDimitry Andric case R_AARCH64_PREL64:
483fe6060f1SDimitry Andric write64(loc, val);
4840b57cec5SDimitry Andric break;
485*0fca6ea1SDimitry Andric case R_AARCH64_AUTH_ABS64:
486*0fca6ea1SDimitry Andric // If val is wider than 32 bits, the relocation must have been moved from
487*0fca6ea1SDimitry Andric // .relr.auth.dyn to .rela.dyn, and the addend write is not needed.
488*0fca6ea1SDimitry Andric //
489*0fca6ea1SDimitry Andric // If val fits in 32 bits, we have two potential scenarios:
490*0fca6ea1SDimitry Andric // * True RELR: Write the 32-bit `val`.
491*0fca6ea1SDimitry Andric // * RELA: Even if the value now fits in 32 bits, it might have been
492*0fca6ea1SDimitry Andric // converted from RELR during an iteration in
493*0fca6ea1SDimitry Andric // finalizeAddressDependentContent(). Writing the value is harmless
494*0fca6ea1SDimitry Andric // because dynamic linking ignores it.
495*0fca6ea1SDimitry Andric if (isInt<32>(val))
496*0fca6ea1SDimitry Andric write32(loc, val);
497*0fca6ea1SDimitry Andric break;
4980b57cec5SDimitry Andric case R_AARCH64_ADD_ABS_LO12_NC:
499*0fca6ea1SDimitry Andric write32Imm12(loc, val);
5000b57cec5SDimitry Andric break;
5010b57cec5SDimitry Andric case R_AARCH64_ADR_GOT_PAGE:
5020b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21:
5030b57cec5SDimitry Andric case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5040b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21:
5055ffd83dbSDimitry Andric checkInt(loc, val, 33, rel);
506bdd1243dSDimitry Andric [[fallthrough]];
5070b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21_NC:
5080b57cec5SDimitry Andric write32AArch64Addr(loc, val >> 12);
5090b57cec5SDimitry Andric break;
5100b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_LO21:
5115ffd83dbSDimitry Andric checkInt(loc, val, 21, rel);
5120b57cec5SDimitry Andric write32AArch64Addr(loc, val);
5130b57cec5SDimitry Andric break;
5140b57cec5SDimitry Andric case R_AARCH64_JUMP26:
5150b57cec5SDimitry Andric // Normally we would just write the bits of the immediate field, however
5160b57cec5SDimitry Andric // when patching instructions for the cpu errata fix -fix-cortex-a53-843419
5170b57cec5SDimitry Andric // we want to replace a non-branch instruction with a branch immediate
5180b57cec5SDimitry Andric // instruction. By writing all the bits of the instruction including the
5190b57cec5SDimitry Andric // opcode and the immediate (0 001 | 01 imm26) we can do this
5200b57cec5SDimitry Andric // transformation by placing a R_AARCH64_JUMP26 relocation at the offset of
5210b57cec5SDimitry Andric // the instruction we want to patch.
5220b57cec5SDimitry Andric write32le(loc, 0x14000000);
523bdd1243dSDimitry Andric [[fallthrough]];
5240b57cec5SDimitry Andric case R_AARCH64_CALL26:
5255ffd83dbSDimitry Andric checkInt(loc, val, 28, rel);
526*0fca6ea1SDimitry Andric writeMaskedBits32le(loc, (val & 0x0FFFFFFC) >> 2, 0x0FFFFFFC >> 2);
5270b57cec5SDimitry Andric break;
5280b57cec5SDimitry Andric case R_AARCH64_CONDBR19:
5290b57cec5SDimitry Andric case R_AARCH64_LD_PREL_LO19:
530*0fca6ea1SDimitry Andric case R_AARCH64_GOT_LD_PREL19:
5315ffd83dbSDimitry Andric checkAlignment(loc, val, 4, rel);
5325ffd83dbSDimitry Andric checkInt(loc, val, 21, rel);
533*0fca6ea1SDimitry Andric writeMaskedBits32le(loc, (val & 0x1FFFFC) << 3, 0x1FFFFC << 3);
5340b57cec5SDimitry Andric break;
5350b57cec5SDimitry Andric case R_AARCH64_LDST8_ABS_LO12_NC:
5360b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
537*0fca6ea1SDimitry Andric write32Imm12(loc, getBits(val, 0, 11));
5380b57cec5SDimitry Andric break;
5390b57cec5SDimitry Andric case R_AARCH64_LDST16_ABS_LO12_NC:
5400b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
5415ffd83dbSDimitry Andric checkAlignment(loc, val, 2, rel);
542*0fca6ea1SDimitry Andric write32Imm12(loc, getBits(val, 1, 11));
5430b57cec5SDimitry Andric break;
5440b57cec5SDimitry Andric case R_AARCH64_LDST32_ABS_LO12_NC:
5450b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
5465ffd83dbSDimitry Andric checkAlignment(loc, val, 4, rel);
547*0fca6ea1SDimitry Andric write32Imm12(loc, getBits(val, 2, 11));
5480b57cec5SDimitry Andric break;
5490b57cec5SDimitry Andric case R_AARCH64_LDST64_ABS_LO12_NC:
5500b57cec5SDimitry Andric case R_AARCH64_LD64_GOT_LO12_NC:
5510b57cec5SDimitry Andric case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5520b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
5530b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12:
5545ffd83dbSDimitry Andric checkAlignment(loc, val, 8, rel);
555*0fca6ea1SDimitry Andric write32Imm12(loc, getBits(val, 3, 11));
5560b57cec5SDimitry Andric break;
5570b57cec5SDimitry Andric case R_AARCH64_LDST128_ABS_LO12_NC:
5580b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC:
5595ffd83dbSDimitry Andric checkAlignment(loc, val, 16, rel);
560*0fca6ea1SDimitry Andric write32Imm12(loc, getBits(val, 4, 11));
5610b57cec5SDimitry Andric break;
562e8d8bef9SDimitry Andric case R_AARCH64_LD64_GOTPAGE_LO15:
563e8d8bef9SDimitry Andric checkAlignment(loc, val, 8, rel);
564*0fca6ea1SDimitry Andric write32Imm12(loc, getBits(val, 3, 14));
565e8d8bef9SDimitry Andric break;
56685868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G0:
5675ffd83dbSDimitry Andric checkUInt(loc, val, 16, rel);
568bdd1243dSDimitry Andric [[fallthrough]];
5690b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G0_NC:
570*0fca6ea1SDimitry Andric writeMaskedBits32le(loc, (val & 0xFFFF) << 5, 0xFFFF << 5);
5710b57cec5SDimitry Andric break;
57285868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G1:
5735ffd83dbSDimitry Andric checkUInt(loc, val, 32, rel);
574bdd1243dSDimitry Andric [[fallthrough]];
5750b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G1_NC:
576*0fca6ea1SDimitry Andric writeMaskedBits32le(loc, (val & 0xFFFF0000) >> 11, 0xFFFF0000 >> 11);
5770b57cec5SDimitry Andric break;
57885868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G2:
5795ffd83dbSDimitry Andric checkUInt(loc, val, 48, rel);
580bdd1243dSDimitry Andric [[fallthrough]];
5810b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G2_NC:
582*0fca6ea1SDimitry Andric writeMaskedBits32le(loc, (val & 0xFFFF00000000) >> 27,
583*0fca6ea1SDimitry Andric 0xFFFF00000000 >> 27);
5840b57cec5SDimitry Andric break;
5850b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G3:
586*0fca6ea1SDimitry Andric writeMaskedBits32le(loc, (val & 0xFFFF000000000000) >> 43,
587*0fca6ea1SDimitry Andric 0xFFFF000000000000 >> 43);
5880b57cec5SDimitry Andric break;
58985868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0:
59085868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G0:
59185868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0:
5925ffd83dbSDimitry Andric checkInt(loc, val, 17, rel);
593bdd1243dSDimitry Andric [[fallthrough]];
59485868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0_NC:
59585868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
59685868e8aSDimitry Andric writeSMovWImm(loc, val);
59785868e8aSDimitry Andric break;
59885868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1:
59985868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G1:
60085868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1:
6015ffd83dbSDimitry Andric checkInt(loc, val, 33, rel);
602bdd1243dSDimitry Andric [[fallthrough]];
60385868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1_NC:
60485868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
60585868e8aSDimitry Andric writeSMovWImm(loc, val >> 16);
60685868e8aSDimitry Andric break;
60785868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2:
60885868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G2:
60985868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G2:
6105ffd83dbSDimitry Andric checkInt(loc, val, 49, rel);
611bdd1243dSDimitry Andric [[fallthrough]];
61285868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2_NC:
61385868e8aSDimitry Andric writeSMovWImm(loc, val >> 32);
61485868e8aSDimitry Andric break;
61585868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G3:
61685868e8aSDimitry Andric writeSMovWImm(loc, val >> 48);
61785868e8aSDimitry Andric break;
6180b57cec5SDimitry Andric case R_AARCH64_TSTBR14:
6195ffd83dbSDimitry Andric checkInt(loc, val, 16, rel);
620*0fca6ea1SDimitry Andric writeMaskedBits32le(loc, (val & 0xFFFC) << 3, 0xFFFC << 3);
6210b57cec5SDimitry Andric break;
6220b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_HI12:
6235ffd83dbSDimitry Andric checkUInt(loc, val, 24, rel);
624*0fca6ea1SDimitry Andric write32Imm12(loc, val >> 12);
6250b57cec5SDimitry Andric break;
6260b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6270b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12:
628*0fca6ea1SDimitry Andric write32Imm12(loc, val);
6290b57cec5SDimitry Andric break;
630fe6060f1SDimitry Andric case R_AARCH64_TLSDESC:
631fe6060f1SDimitry Andric // For R_AARCH64_TLSDESC the addend is stored in the second 64-bit word.
632fe6060f1SDimitry Andric write64(loc + 8, val);
633fe6060f1SDimitry Andric break;
6340b57cec5SDimitry Andric default:
63585868e8aSDimitry Andric llvm_unreachable("unknown relocation");
6360b57cec5SDimitry Andric }
6370b57cec5SDimitry Andric }
6380b57cec5SDimitry Andric
relaxTlsGdToLe(uint8_t * loc,const Relocation & rel,uint64_t val) const6395ffd83dbSDimitry Andric void AArch64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
6405ffd83dbSDimitry Andric uint64_t val) const {
6410b57cec5SDimitry Andric // TLSDESC Global-Dynamic relocation are in the form:
6420b57cec5SDimitry Andric // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
6430b57cec5SDimitry Andric // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12]
6440b57cec5SDimitry Andric // add x0, x0, :tlsdesc_los:v [R_AARCH64_TLSDESC_ADD_LO12]
6450b57cec5SDimitry Andric // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
6460b57cec5SDimitry Andric // blr x1
6470b57cec5SDimitry Andric // And it can optimized to:
6480b57cec5SDimitry Andric // movz x0, #0x0, lsl #16
6490b57cec5SDimitry Andric // movk x0, #0x10
6500b57cec5SDimitry Andric // nop
6510b57cec5SDimitry Andric // nop
6525ffd83dbSDimitry Andric checkUInt(loc, val, 32, rel);
6530b57cec5SDimitry Andric
6545ffd83dbSDimitry Andric switch (rel.type) {
6550b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12:
6560b57cec5SDimitry Andric case R_AARCH64_TLSDESC_CALL:
6570b57cec5SDimitry Andric write32le(loc, 0xd503201f); // nop
6580b57cec5SDimitry Andric return;
6590b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21:
6600b57cec5SDimitry Andric write32le(loc, 0xd2a00000 | (((val >> 16) & 0xffff) << 5)); // movz
6610b57cec5SDimitry Andric return;
6620b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12:
6630b57cec5SDimitry Andric write32le(loc, 0xf2800000 | ((val & 0xffff) << 5)); // movk
6640b57cec5SDimitry Andric return;
6650b57cec5SDimitry Andric default:
6660b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
6670b57cec5SDimitry Andric }
6680b57cec5SDimitry Andric }
6690b57cec5SDimitry Andric
relaxTlsGdToIe(uint8_t * loc,const Relocation & rel,uint64_t val) const6705ffd83dbSDimitry Andric void AArch64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
6715ffd83dbSDimitry Andric uint64_t val) const {
6720b57cec5SDimitry Andric // TLSDESC Global-Dynamic relocation are in the form:
6730b57cec5SDimitry Andric // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
6740b57cec5SDimitry Andric // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12]
6750b57cec5SDimitry Andric // add x0, x0, :tlsdesc_los:v [R_AARCH64_TLSDESC_ADD_LO12]
6760b57cec5SDimitry Andric // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
6770b57cec5SDimitry Andric // blr x1
6780b57cec5SDimitry Andric // And it can optimized to:
6790b57cec5SDimitry Andric // adrp x0, :gottprel:v
6800b57cec5SDimitry Andric // ldr x0, [x0, :gottprel_lo12:v]
6810b57cec5SDimitry Andric // nop
6820b57cec5SDimitry Andric // nop
6830b57cec5SDimitry Andric
6845ffd83dbSDimitry Andric switch (rel.type) {
6850b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12:
6860b57cec5SDimitry Andric case R_AARCH64_TLSDESC_CALL:
6870b57cec5SDimitry Andric write32le(loc, 0xd503201f); // nop
6880b57cec5SDimitry Andric break;
6890b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21:
6900b57cec5SDimitry Andric write32le(loc, 0x90000000); // adrp
6915ffd83dbSDimitry Andric relocateNoSym(loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, val);
6920b57cec5SDimitry Andric break;
6930b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12:
6940b57cec5SDimitry Andric write32le(loc, 0xf9400000); // ldr
6955ffd83dbSDimitry Andric relocateNoSym(loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, val);
6960b57cec5SDimitry Andric break;
6970b57cec5SDimitry Andric default:
6980b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
6990b57cec5SDimitry Andric }
7000b57cec5SDimitry Andric }
7010b57cec5SDimitry Andric
relaxTlsIeToLe(uint8_t * loc,const Relocation & rel,uint64_t val) const7025ffd83dbSDimitry Andric void AArch64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
7035ffd83dbSDimitry Andric uint64_t val) const {
7045ffd83dbSDimitry Andric checkUInt(loc, val, 32, rel);
7050b57cec5SDimitry Andric
7065ffd83dbSDimitry Andric if (rel.type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
7070b57cec5SDimitry Andric // Generate MOVZ.
7080b57cec5SDimitry Andric uint32_t regNo = read32le(loc) & 0x1f;
7090b57cec5SDimitry Andric write32le(loc, (0xd2a00000 | regNo) | (((val >> 16) & 0xffff) << 5));
7100b57cec5SDimitry Andric return;
7110b57cec5SDimitry Andric }
7125ffd83dbSDimitry Andric if (rel.type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
7130b57cec5SDimitry Andric // Generate MOVK.
7140b57cec5SDimitry Andric uint32_t regNo = read32le(loc) & 0x1f;
7150b57cec5SDimitry Andric write32le(loc, (0xf2800000 | regNo) | ((val & 0xffff) << 5));
7160b57cec5SDimitry Andric return;
7170b57cec5SDimitry Andric }
7180b57cec5SDimitry Andric llvm_unreachable("invalid relocation for TLS IE to LE relaxation");
7190b57cec5SDimitry Andric }
7200b57cec5SDimitry Andric
AArch64Relaxer(ArrayRef<Relocation> relocs)72104eeddc0SDimitry Andric AArch64Relaxer::AArch64Relaxer(ArrayRef<Relocation> relocs) {
722bdd1243dSDimitry Andric if (!config->relax)
72304eeddc0SDimitry Andric return;
72404eeddc0SDimitry Andric // Check if R_AARCH64_ADR_GOT_PAGE and R_AARCH64_LD64_GOT_LO12_NC
72504eeddc0SDimitry Andric // always appear in pairs.
72604eeddc0SDimitry Andric size_t i = 0;
72704eeddc0SDimitry Andric const size_t size = relocs.size();
72804eeddc0SDimitry Andric for (; i != size; ++i) {
72904eeddc0SDimitry Andric if (relocs[i].type == R_AARCH64_ADR_GOT_PAGE) {
73004eeddc0SDimitry Andric if (i + 1 < size && relocs[i + 1].type == R_AARCH64_LD64_GOT_LO12_NC) {
73104eeddc0SDimitry Andric ++i;
73204eeddc0SDimitry Andric continue;
73304eeddc0SDimitry Andric }
73404eeddc0SDimitry Andric break;
73504eeddc0SDimitry Andric } else if (relocs[i].type == R_AARCH64_LD64_GOT_LO12_NC) {
73604eeddc0SDimitry Andric break;
73704eeddc0SDimitry Andric }
73804eeddc0SDimitry Andric }
73904eeddc0SDimitry Andric safeToRelaxAdrpLdr = i == size;
74004eeddc0SDimitry Andric }
74104eeddc0SDimitry Andric
tryRelaxAdrpAdd(const Relocation & adrpRel,const Relocation & addRel,uint64_t secAddr,uint8_t * buf) const7421fd87a68SDimitry Andric bool AArch64Relaxer::tryRelaxAdrpAdd(const Relocation &adrpRel,
7431fd87a68SDimitry Andric const Relocation &addRel, uint64_t secAddr,
7441fd87a68SDimitry Andric uint8_t *buf) const {
7451fd87a68SDimitry Andric // When the address of sym is within the range of ADR then
7461fd87a68SDimitry Andric // we may relax
7471fd87a68SDimitry Andric // ADRP xn, sym
7481fd87a68SDimitry Andric // ADD xn, xn, :lo12: sym
7491fd87a68SDimitry Andric // to
7501fd87a68SDimitry Andric // NOP
7511fd87a68SDimitry Andric // ADR xn, sym
7521fd87a68SDimitry Andric if (!config->relax || adrpRel.type != R_AARCH64_ADR_PREL_PG_HI21 ||
7531fd87a68SDimitry Andric addRel.type != R_AARCH64_ADD_ABS_LO12_NC)
7541fd87a68SDimitry Andric return false;
7551fd87a68SDimitry Andric // Check if the relocations apply to consecutive instructions.
7561fd87a68SDimitry Andric if (adrpRel.offset + 4 != addRel.offset)
7571fd87a68SDimitry Andric return false;
7581fd87a68SDimitry Andric if (adrpRel.sym != addRel.sym)
7591fd87a68SDimitry Andric return false;
7601fd87a68SDimitry Andric if (adrpRel.addend != 0 || addRel.addend != 0)
7611fd87a68SDimitry Andric return false;
7621fd87a68SDimitry Andric
7631fd87a68SDimitry Andric uint32_t adrpInstr = read32le(buf + adrpRel.offset);
7641fd87a68SDimitry Andric uint32_t addInstr = read32le(buf + addRel.offset);
7651fd87a68SDimitry Andric // Check if the first instruction is ADRP and the second instruction is ADD.
7661fd87a68SDimitry Andric if ((adrpInstr & 0x9f000000) != 0x90000000 ||
7671fd87a68SDimitry Andric (addInstr & 0xffc00000) != 0x91000000)
7681fd87a68SDimitry Andric return false;
7691fd87a68SDimitry Andric uint32_t adrpDestReg = adrpInstr & 0x1f;
7701fd87a68SDimitry Andric uint32_t addDestReg = addInstr & 0x1f;
7711fd87a68SDimitry Andric uint32_t addSrcReg = (addInstr >> 5) & 0x1f;
7721fd87a68SDimitry Andric if (adrpDestReg != addDestReg || adrpDestReg != addSrcReg)
7731fd87a68SDimitry Andric return false;
7741fd87a68SDimitry Andric
7751fd87a68SDimitry Andric Symbol &sym = *adrpRel.sym;
7761fd87a68SDimitry Andric // Check if the address difference is within 1MiB range.
7771fd87a68SDimitry Andric int64_t val = sym.getVA() - (secAddr + addRel.offset);
7781fd87a68SDimitry Andric if (val < -1024 * 1024 || val >= 1024 * 1024)
7791fd87a68SDimitry Andric return false;
7801fd87a68SDimitry Andric
7811fd87a68SDimitry Andric Relocation adrRel = {R_ABS, R_AARCH64_ADR_PREL_LO21, addRel.offset,
7821fd87a68SDimitry Andric /*addend=*/0, &sym};
7831fd87a68SDimitry Andric // nop
7841fd87a68SDimitry Andric write32le(buf + adrpRel.offset, 0xd503201f);
7851fd87a68SDimitry Andric // adr x_<dest_reg>
7861fd87a68SDimitry Andric write32le(buf + adrRel.offset, 0x10000000 | adrpDestReg);
7871fd87a68SDimitry Andric target->relocate(buf + adrRel.offset, adrRel, val);
7881fd87a68SDimitry Andric return true;
7891fd87a68SDimitry Andric }
7901fd87a68SDimitry Andric
tryRelaxAdrpLdr(const Relocation & adrpRel,const Relocation & ldrRel,uint64_t secAddr,uint8_t * buf) const79104eeddc0SDimitry Andric bool AArch64Relaxer::tryRelaxAdrpLdr(const Relocation &adrpRel,
79204eeddc0SDimitry Andric const Relocation &ldrRel, uint64_t secAddr,
79304eeddc0SDimitry Andric uint8_t *buf) const {
79404eeddc0SDimitry Andric if (!safeToRelaxAdrpLdr)
79504eeddc0SDimitry Andric return false;
79604eeddc0SDimitry Andric
79704eeddc0SDimitry Andric // When the definition of sym is not preemptible then we may
79804eeddc0SDimitry Andric // be able to relax
79904eeddc0SDimitry Andric // ADRP xn, :got: sym
80004eeddc0SDimitry Andric // LDR xn, [ xn :got_lo12: sym]
80104eeddc0SDimitry Andric // to
80204eeddc0SDimitry Andric // ADRP xn, sym
80304eeddc0SDimitry Andric // ADD xn, xn, :lo_12: sym
80404eeddc0SDimitry Andric
80504eeddc0SDimitry Andric if (adrpRel.type != R_AARCH64_ADR_GOT_PAGE ||
80604eeddc0SDimitry Andric ldrRel.type != R_AARCH64_LD64_GOT_LO12_NC)
80704eeddc0SDimitry Andric return false;
80804eeddc0SDimitry Andric // Check if the relocations apply to consecutive instructions.
80904eeddc0SDimitry Andric if (adrpRel.offset + 4 != ldrRel.offset)
81004eeddc0SDimitry Andric return false;
81104eeddc0SDimitry Andric // Check if the relocations reference the same symbol and
81204eeddc0SDimitry Andric // skip undefined, preemptible and STT_GNU_IFUNC symbols.
81304eeddc0SDimitry Andric if (!adrpRel.sym || adrpRel.sym != ldrRel.sym || !adrpRel.sym->isDefined() ||
81404eeddc0SDimitry Andric adrpRel.sym->isPreemptible || adrpRel.sym->isGnuIFunc())
81504eeddc0SDimitry Andric return false;
81604eeddc0SDimitry Andric // Check if the addends of the both relocations are zero.
81704eeddc0SDimitry Andric if (adrpRel.addend != 0 || ldrRel.addend != 0)
81804eeddc0SDimitry Andric return false;
81904eeddc0SDimitry Andric uint32_t adrpInstr = read32le(buf + adrpRel.offset);
82004eeddc0SDimitry Andric uint32_t ldrInstr = read32le(buf + ldrRel.offset);
82104eeddc0SDimitry Andric // Check if the first instruction is ADRP and the second instruction is LDR.
82204eeddc0SDimitry Andric if ((adrpInstr & 0x9f000000) != 0x90000000 ||
82304eeddc0SDimitry Andric (ldrInstr & 0x3b000000) != 0x39000000)
82404eeddc0SDimitry Andric return false;
82504eeddc0SDimitry Andric // Check the value of the sf bit.
82604eeddc0SDimitry Andric if (!(ldrInstr >> 31))
82704eeddc0SDimitry Andric return false;
82804eeddc0SDimitry Andric uint32_t adrpDestReg = adrpInstr & 0x1f;
82904eeddc0SDimitry Andric uint32_t ldrDestReg = ldrInstr & 0x1f;
83004eeddc0SDimitry Andric uint32_t ldrSrcReg = (ldrInstr >> 5) & 0x1f;
83104eeddc0SDimitry Andric // Check if ADPR and LDR use the same register.
83204eeddc0SDimitry Andric if (adrpDestReg != ldrDestReg || adrpDestReg != ldrSrcReg)
83304eeddc0SDimitry Andric return false;
83404eeddc0SDimitry Andric
83504eeddc0SDimitry Andric Symbol &sym = *adrpRel.sym;
83681ad6265SDimitry Andric // GOT references to absolute symbols can't be relaxed to use ADRP/ADD in
83781ad6265SDimitry Andric // position-independent code because these instructions produce a relative
83881ad6265SDimitry Andric // address.
83981ad6265SDimitry Andric if (config->isPic && !cast<Defined>(sym).section)
84081ad6265SDimitry Andric return false;
84104eeddc0SDimitry Andric // Check if the address difference is within 4GB range.
84204eeddc0SDimitry Andric int64_t val =
84304eeddc0SDimitry Andric getAArch64Page(sym.getVA()) - getAArch64Page(secAddr + adrpRel.offset);
84404eeddc0SDimitry Andric if (val != llvm::SignExtend64(val, 33))
84504eeddc0SDimitry Andric return false;
84604eeddc0SDimitry Andric
84704eeddc0SDimitry Andric Relocation adrpSymRel = {R_AARCH64_PAGE_PC, R_AARCH64_ADR_PREL_PG_HI21,
84804eeddc0SDimitry Andric adrpRel.offset, /*addend=*/0, &sym};
84904eeddc0SDimitry Andric Relocation addRel = {R_ABS, R_AARCH64_ADD_ABS_LO12_NC, ldrRel.offset,
85004eeddc0SDimitry Andric /*addend=*/0, &sym};
85104eeddc0SDimitry Andric
85204eeddc0SDimitry Andric // adrp x_<dest_reg>
85304eeddc0SDimitry Andric write32le(buf + adrpSymRel.offset, 0x90000000 | adrpDestReg);
85404eeddc0SDimitry Andric // add x_<dest reg>, x_<dest reg>
85504eeddc0SDimitry Andric write32le(buf + addRel.offset, 0x91000000 | adrpDestReg | (adrpDestReg << 5));
85604eeddc0SDimitry Andric
85704eeddc0SDimitry Andric target->relocate(buf + adrpSymRel.offset, adrpSymRel,
85804eeddc0SDimitry Andric SignExtend64(getAArch64Page(sym.getVA()) -
85904eeddc0SDimitry Andric getAArch64Page(secAddr + adrpSymRel.offset),
86004eeddc0SDimitry Andric 64));
86104eeddc0SDimitry Andric target->relocate(buf + addRel.offset, addRel, SignExtend64(sym.getVA(), 64));
8621fd87a68SDimitry Andric tryRelaxAdrpAdd(adrpSymRel, addRel, secAddr, buf);
86304eeddc0SDimitry Andric return true;
86404eeddc0SDimitry Andric }
86504eeddc0SDimitry Andric
8665f757f3fSDimitry Andric // Tagged symbols have upper address bits that are added by the dynamic loader,
8675f757f3fSDimitry Andric // and thus need the full 64-bit GOT entry. Do not relax such symbols.
needsGotForMemtag(const Relocation & rel)8685f757f3fSDimitry Andric static bool needsGotForMemtag(const Relocation &rel) {
8695f757f3fSDimitry Andric return rel.sym->isTagged() && needsGot(rel.expr);
8705f757f3fSDimitry Andric }
8715f757f3fSDimitry Andric
relocateAlloc(InputSectionBase & sec,uint8_t * buf) const872bdd1243dSDimitry Andric void AArch64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
873bdd1243dSDimitry Andric uint64_t secAddr = sec.getOutputSection()->addr;
874bdd1243dSDimitry Andric if (auto *s = dyn_cast<InputSection>(&sec))
875bdd1243dSDimitry Andric secAddr += s->outSecOff;
8765f757f3fSDimitry Andric else if (auto *ehIn = dyn_cast<EhInputSection>(&sec))
8775f757f3fSDimitry Andric secAddr += ehIn->getParent()->outSecOff;
878bdd1243dSDimitry Andric AArch64Relaxer relaxer(sec.relocs());
879bdd1243dSDimitry Andric for (size_t i = 0, size = sec.relocs().size(); i != size; ++i) {
880bdd1243dSDimitry Andric const Relocation &rel = sec.relocs()[i];
881bdd1243dSDimitry Andric uint8_t *loc = buf + rel.offset;
882bdd1243dSDimitry Andric const uint64_t val =
883bdd1243dSDimitry Andric sec.getRelocTargetVA(sec.file, rel.type, rel.addend,
884bdd1243dSDimitry Andric secAddr + rel.offset, *rel.sym, rel.expr);
8855f757f3fSDimitry Andric
8865f757f3fSDimitry Andric if (needsGotForMemtag(rel)) {
8875f757f3fSDimitry Andric relocate(loc, rel, val);
8885f757f3fSDimitry Andric continue;
8895f757f3fSDimitry Andric }
8905f757f3fSDimitry Andric
891bdd1243dSDimitry Andric switch (rel.expr) {
892bdd1243dSDimitry Andric case R_AARCH64_GOT_PAGE_PC:
893bdd1243dSDimitry Andric if (i + 1 < size &&
894bdd1243dSDimitry Andric relaxer.tryRelaxAdrpLdr(rel, sec.relocs()[i + 1], secAddr, buf)) {
895bdd1243dSDimitry Andric ++i;
896bdd1243dSDimitry Andric continue;
897bdd1243dSDimitry Andric }
898bdd1243dSDimitry Andric break;
899bdd1243dSDimitry Andric case R_AARCH64_PAGE_PC:
900bdd1243dSDimitry Andric if (i + 1 < size &&
901bdd1243dSDimitry Andric relaxer.tryRelaxAdrpAdd(rel, sec.relocs()[i + 1], secAddr, buf)) {
902bdd1243dSDimitry Andric ++i;
903bdd1243dSDimitry Andric continue;
904bdd1243dSDimitry Andric }
905bdd1243dSDimitry Andric break;
906bdd1243dSDimitry Andric case R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC:
907bdd1243dSDimitry Andric case R_RELAX_TLS_GD_TO_IE_ABS:
908bdd1243dSDimitry Andric relaxTlsGdToIe(loc, rel, val);
909bdd1243dSDimitry Andric continue;
910bdd1243dSDimitry Andric case R_RELAX_TLS_GD_TO_LE:
911bdd1243dSDimitry Andric relaxTlsGdToLe(loc, rel, val);
912bdd1243dSDimitry Andric continue;
913bdd1243dSDimitry Andric case R_RELAX_TLS_IE_TO_LE:
914bdd1243dSDimitry Andric relaxTlsIeToLe(loc, rel, val);
915bdd1243dSDimitry Andric continue;
916bdd1243dSDimitry Andric default:
917bdd1243dSDimitry Andric break;
918bdd1243dSDimitry Andric }
919bdd1243dSDimitry Andric relocate(loc, rel, val);
920bdd1243dSDimitry Andric }
921bdd1243dSDimitry Andric }
922bdd1243dSDimitry Andric
9230b57cec5SDimitry Andric // AArch64 may use security features in variant PLT sequences. These are:
9240b57cec5SDimitry Andric // Pointer Authentication (PAC), introduced in armv8.3-a and Branch Target
9250b57cec5SDimitry Andric // Indicator (BTI) introduced in armv8.5-a. The additional instructions used
9260b57cec5SDimitry Andric // in the variant Plt sequences are encoded in the Hint space so they can be
9270b57cec5SDimitry Andric // deployed on older architectures, which treat the instructions as a nop.
9280b57cec5SDimitry Andric // PAC and BTI can be combined leading to the following combinations:
9290b57cec5SDimitry Andric // writePltHeader
9300b57cec5SDimitry Andric // writePltHeaderBti (no PAC Header needed)
9310b57cec5SDimitry Andric // writePlt
9320b57cec5SDimitry Andric // writePltBti (BTI only)
9330b57cec5SDimitry Andric // writePltPac (PAC only)
9340b57cec5SDimitry Andric // writePltBtiPac (BTI and PAC)
9350b57cec5SDimitry Andric //
9360b57cec5SDimitry Andric // When PAC is enabled the dynamic loader encrypts the address that it places
9370b57cec5SDimitry Andric // in the .got.plt using the pacia1716 instruction which encrypts the value in
9380b57cec5SDimitry Andric // x17 using the modifier in x16. The static linker places autia1716 before the
9390b57cec5SDimitry Andric // indirect branch to x17 to authenticate the address in x17 with the modifier
9400b57cec5SDimitry Andric // in x16. This makes it more difficult for an attacker to modify the value in
9410b57cec5SDimitry Andric // the .got.plt.
9420b57cec5SDimitry Andric //
9430b57cec5SDimitry Andric // When BTI is enabled all indirect branches must land on a bti instruction.
9440b57cec5SDimitry Andric // The static linker must place a bti instruction at the start of any PLT entry
9450b57cec5SDimitry Andric // that may be the target of an indirect branch. As the PLT entries call the
9460b57cec5SDimitry Andric // lazy resolver indirectly this must have a bti instruction at start. In
9470b57cec5SDimitry Andric // general a bti instruction is not needed for a PLT entry as indirect calls
9480b57cec5SDimitry Andric // are resolved to the function address and not the PLT entry for the function.
9490b57cec5SDimitry Andric // There are a small number of cases where the PLT address can escape, such as
9500b57cec5SDimitry Andric // taking the address of a function or ifunc via a non got-generating
9510b57cec5SDimitry Andric // relocation, and a shared library refers to that symbol.
9520b57cec5SDimitry Andric //
9530b57cec5SDimitry Andric // We use the bti c variant of the instruction which permits indirect branches
9540b57cec5SDimitry Andric // (br) via x16/x17 and indirect function calls (blr) via any register. The ABI
9550b57cec5SDimitry Andric // guarantees that all indirect branches from code requiring BTI protection
9560b57cec5SDimitry Andric // will go via x16/x17
9570b57cec5SDimitry Andric
9580b57cec5SDimitry Andric namespace {
9590b57cec5SDimitry Andric class AArch64BtiPac final : public AArch64 {
9600b57cec5SDimitry Andric public:
9610b57cec5SDimitry Andric AArch64BtiPac();
9620b57cec5SDimitry Andric void writePltHeader(uint8_t *buf) const override;
963480093f4SDimitry Andric void writePlt(uint8_t *buf, const Symbol &sym,
964480093f4SDimitry Andric uint64_t pltEntryAddr) const override;
9650b57cec5SDimitry Andric
9660b57cec5SDimitry Andric private:
967349cc55cSDimitry Andric bool btiHeader; // bti instruction needed in PLT Header and Entry
9680b57cec5SDimitry Andric bool pacEntry; // autia1716 instruction needed in PLT Entry
9690b57cec5SDimitry Andric };
9700b57cec5SDimitry Andric } // namespace
9710b57cec5SDimitry Andric
AArch64BtiPac()9720b57cec5SDimitry Andric AArch64BtiPac::AArch64BtiPac() {
9730b57cec5SDimitry Andric btiHeader = (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI);
9740b57cec5SDimitry Andric // A BTI (Branch Target Indicator) Plt Entry is only required if the
9750b57cec5SDimitry Andric // address of the PLT entry can be taken by the program, which permits an
9760b57cec5SDimitry Andric // indirect jump to the PLT entry. This can happen when the address
9770b57cec5SDimitry Andric // of the PLT entry for a function is canonicalised due to the address of
978349cc55cSDimitry Andric // the function in an executable being taken by a shared library, or
979349cc55cSDimitry Andric // non-preemptible ifunc referenced by non-GOT-generating, non-PLT-generating
980349cc55cSDimitry Andric // relocations.
9815ffd83dbSDimitry Andric // The PAC PLT entries require dynamic loader support and this isn't known
9825ffd83dbSDimitry Andric // from properties in the objects, so we use the command line flag.
9835ffd83dbSDimitry Andric pacEntry = config->zPacPlt;
9840b57cec5SDimitry Andric
985349cc55cSDimitry Andric if (btiHeader || pacEntry) {
9860b57cec5SDimitry Andric pltEntrySize = 24;
987480093f4SDimitry Andric ipltEntrySize = 24;
988480093f4SDimitry Andric }
9890b57cec5SDimitry Andric }
9900b57cec5SDimitry Andric
writePltHeader(uint8_t * buf) const9910b57cec5SDimitry Andric void AArch64BtiPac::writePltHeader(uint8_t *buf) const {
9920b57cec5SDimitry Andric const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c
9930b57cec5SDimitry Andric const uint8_t pltData[] = {
9940b57cec5SDimitry Andric 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
995bdd1243dSDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.got.plt[2]))
996bdd1243dSDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.got.plt[2]))]
997bdd1243dSDimitry Andric 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.got.plt[2]))
9980b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6, // br x17
9990b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5, // nop
10000b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5 // nop
10010b57cec5SDimitry Andric };
10020b57cec5SDimitry Andric const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop
10030b57cec5SDimitry Andric
10040b57cec5SDimitry Andric uint64_t got = in.gotPlt->getVA();
10050b57cec5SDimitry Andric uint64_t plt = in.plt->getVA();
10060b57cec5SDimitry Andric
10070b57cec5SDimitry Andric if (btiHeader) {
10080b57cec5SDimitry Andric // PltHeader is called indirectly by plt[N]. Prefix pltData with a BTI C
10090b57cec5SDimitry Andric // instruction.
10100b57cec5SDimitry Andric memcpy(buf, btiData, sizeof(btiData));
10110b57cec5SDimitry Andric buf += sizeof(btiData);
10120b57cec5SDimitry Andric plt += sizeof(btiData);
10130b57cec5SDimitry Andric }
10140b57cec5SDimitry Andric memcpy(buf, pltData, sizeof(pltData));
10150b57cec5SDimitry Andric
10165ffd83dbSDimitry Andric relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
10170b57cec5SDimitry Andric getAArch64Page(got + 16) - getAArch64Page(plt + 8));
10185ffd83dbSDimitry Andric relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16);
10195ffd83dbSDimitry Andric relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16);
10200b57cec5SDimitry Andric if (!btiHeader)
10210b57cec5SDimitry Andric // We didn't add the BTI c instruction so round out size with NOP.
10220b57cec5SDimitry Andric memcpy(buf + sizeof(pltData), nopData, sizeof(nopData));
10230b57cec5SDimitry Andric }
10240b57cec5SDimitry Andric
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr) const1025480093f4SDimitry Andric void AArch64BtiPac::writePlt(uint8_t *buf, const Symbol &sym,
1026480093f4SDimitry Andric uint64_t pltEntryAddr) const {
10270b57cec5SDimitry Andric // The PLT entry is of the form:
10280b57cec5SDimitry Andric // [btiData] addrInst (pacBr | stdBr) [nopData]
10290b57cec5SDimitry Andric const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c
10300b57cec5SDimitry Andric const uint8_t addrInst[] = {
1031bdd1243dSDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.got.plt[n]))
1032bdd1243dSDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.got.plt[n]))]
1033bdd1243dSDimitry Andric 0x10, 0x02, 0x00, 0x91 // add x16, x16, Offset(&(.got.plt[n]))
10340b57cec5SDimitry Andric };
10350b57cec5SDimitry Andric const uint8_t pacBr[] = {
10360b57cec5SDimitry Andric 0x9f, 0x21, 0x03, 0xd5, // autia1716
10370b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6 // br x17
10380b57cec5SDimitry Andric };
10390b57cec5SDimitry Andric const uint8_t stdBr[] = {
10400b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6, // br x17
10410b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5 // nop
10420b57cec5SDimitry Andric };
10430b57cec5SDimitry Andric const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop
10440b57cec5SDimitry Andric
1045bdd1243dSDimitry Andric // NEEDS_COPY indicates a non-ifunc canonical PLT entry whose address may
1046349cc55cSDimitry Andric // escape to shared objects. isInIplt indicates a non-preemptible ifunc. Its
104706c3fb27SDimitry Andric // address may escape if referenced by a direct relocation. If relative
104806c3fb27SDimitry Andric // vtables are used then if the vtable is in a shared object the offsets will
104906c3fb27SDimitry Andric // be to the PLT entry. The condition is conservative.
105006c3fb27SDimitry Andric bool hasBti = btiHeader &&
105106c3fb27SDimitry Andric (sym.hasFlag(NEEDS_COPY) || sym.isInIplt || sym.thunkAccessed);
1052349cc55cSDimitry Andric if (hasBti) {
10530b57cec5SDimitry Andric memcpy(buf, btiData, sizeof(btiData));
10540b57cec5SDimitry Andric buf += sizeof(btiData);
10550b57cec5SDimitry Andric pltEntryAddr += sizeof(btiData);
10560b57cec5SDimitry Andric }
10570b57cec5SDimitry Andric
1058480093f4SDimitry Andric uint64_t gotPltEntryAddr = sym.getGotPltVA();
10590b57cec5SDimitry Andric memcpy(buf, addrInst, sizeof(addrInst));
10605ffd83dbSDimitry Andric relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
10615ffd83dbSDimitry Andric getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr));
10625ffd83dbSDimitry Andric relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr);
10635ffd83dbSDimitry Andric relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr);
10640b57cec5SDimitry Andric
10650b57cec5SDimitry Andric if (pacEntry)
10660b57cec5SDimitry Andric memcpy(buf + sizeof(addrInst), pacBr, sizeof(pacBr));
10670b57cec5SDimitry Andric else
10680b57cec5SDimitry Andric memcpy(buf + sizeof(addrInst), stdBr, sizeof(stdBr));
1069349cc55cSDimitry Andric if (!hasBti)
10700b57cec5SDimitry Andric // We didn't add the BTI c instruction so round out size with NOP.
10710b57cec5SDimitry Andric memcpy(buf + sizeof(addrInst) + sizeof(stdBr), nopData, sizeof(nopData));
10720b57cec5SDimitry Andric }
10730b57cec5SDimitry Andric
getTargetInfo()10740b57cec5SDimitry Andric static TargetInfo *getTargetInfo() {
107561cfbce3SDimitry Andric if ((config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) ||
107661cfbce3SDimitry Andric config->zPacPlt) {
10770b57cec5SDimitry Andric static AArch64BtiPac t;
10780b57cec5SDimitry Andric return &t;
10790b57cec5SDimitry Andric }
10800b57cec5SDimitry Andric static AArch64 t;
10810b57cec5SDimitry Andric return &t;
10820b57cec5SDimitry Andric }
10830b57cec5SDimitry Andric
getAArch64TargetInfo()10845ffd83dbSDimitry Andric TargetInfo *elf::getAArch64TargetInfo() { return getTargetInfo(); }
10855f757f3fSDimitry Andric
10865f757f3fSDimitry Andric template <class ELFT>
10875f757f3fSDimitry Andric static void
addTaggedSymbolReferences(InputSectionBase & sec,DenseMap<Symbol *,unsigned> & referenceCount)10885f757f3fSDimitry Andric addTaggedSymbolReferences(InputSectionBase &sec,
10895f757f3fSDimitry Andric DenseMap<Symbol *, unsigned> &referenceCount) {
10905f757f3fSDimitry Andric assert(sec.type == SHT_AARCH64_MEMTAG_GLOBALS_STATIC);
10915f757f3fSDimitry Andric
10925f757f3fSDimitry Andric const RelsOrRelas<ELFT> rels = sec.relsOrRelas<ELFT>();
10935f757f3fSDimitry Andric if (rels.areRelocsRel())
10945f757f3fSDimitry Andric error("non-RELA relocations are not allowed with memtag globals");
10955f757f3fSDimitry Andric
10965f757f3fSDimitry Andric for (const typename ELFT::Rela &rel : rels.relas) {
1097*0fca6ea1SDimitry Andric Symbol &sym = sec.file->getRelocTargetSym(rel);
10985f757f3fSDimitry Andric // Linker-synthesized symbols such as __executable_start may be referenced
10995f757f3fSDimitry Andric // as tagged in input objfiles, and we don't want them to be tagged. A
11005f757f3fSDimitry Andric // cheap way to exclude them is the type check, but their type is
11015f757f3fSDimitry Andric // STT_NOTYPE. In addition, this save us from checking untaggable symbols,
11025f757f3fSDimitry Andric // like functions or TLS symbols.
11035f757f3fSDimitry Andric if (sym.type != STT_OBJECT)
11045f757f3fSDimitry Andric continue;
11055f757f3fSDimitry Andric // STB_LOCAL symbols can't be referenced from outside the object file, and
11065f757f3fSDimitry Andric // thus don't need to be checked for references from other object files.
11075f757f3fSDimitry Andric if (sym.binding == STB_LOCAL) {
11085f757f3fSDimitry Andric sym.setIsTagged(true);
11095f757f3fSDimitry Andric continue;
11105f757f3fSDimitry Andric }
11115f757f3fSDimitry Andric ++referenceCount[&sym];
11125f757f3fSDimitry Andric }
11135f757f3fSDimitry Andric sec.markDead();
11145f757f3fSDimitry Andric }
11155f757f3fSDimitry Andric
11165f757f3fSDimitry Andric // A tagged symbol must be denoted as being tagged by all references and the
11175f757f3fSDimitry Andric // chosen definition. For simplicity, here, it must also be denoted as tagged
11185f757f3fSDimitry Andric // for all definitions. Otherwise:
11195f757f3fSDimitry Andric //
11205f757f3fSDimitry Andric // 1. A tagged definition can be used by an untagged declaration, in which case
11215f757f3fSDimitry Andric // the untagged access may be PC-relative, causing a tag mismatch at
11225f757f3fSDimitry Andric // runtime.
11235f757f3fSDimitry Andric // 2. An untagged definition can be used by a tagged declaration, where the
11245f757f3fSDimitry Andric // compiler has taken advantage of the increased alignment of the tagged
11255f757f3fSDimitry Andric // declaration, but the alignment at runtime is wrong, causing a fault.
11265f757f3fSDimitry Andric //
11275f757f3fSDimitry Andric // Ideally, this isn't a problem, as any TU that imports or exports tagged
11285f757f3fSDimitry Andric // symbols should also be built with tagging. But, to handle these cases, we
11295f757f3fSDimitry Andric // demote the symbol to be untagged.
createTaggedSymbols(const SmallVector<ELFFileBase *,0> & files)11305f757f3fSDimitry Andric void lld::elf::createTaggedSymbols(const SmallVector<ELFFileBase *, 0> &files) {
11311db9f3b2SDimitry Andric assert(hasMemtag());
11325f757f3fSDimitry Andric
11335f757f3fSDimitry Andric // First, collect all symbols that are marked as tagged, and count how many
11345f757f3fSDimitry Andric // times they're marked as tagged.
11355f757f3fSDimitry Andric DenseMap<Symbol *, unsigned> taggedSymbolReferenceCount;
11365f757f3fSDimitry Andric for (InputFile* file : files) {
11375f757f3fSDimitry Andric if (file->kind() != InputFile::ObjKind)
11385f757f3fSDimitry Andric continue;
11395f757f3fSDimitry Andric for (InputSectionBase *section : file->getSections()) {
11405f757f3fSDimitry Andric if (!section || section->type != SHT_AARCH64_MEMTAG_GLOBALS_STATIC ||
11415f757f3fSDimitry Andric section == &InputSection::discarded)
11425f757f3fSDimitry Andric continue;
11435f757f3fSDimitry Andric invokeELFT(addTaggedSymbolReferences, *section,
11445f757f3fSDimitry Andric taggedSymbolReferenceCount);
11455f757f3fSDimitry Andric }
11465f757f3fSDimitry Andric }
11475f757f3fSDimitry Andric
11485f757f3fSDimitry Andric // Now, go through all the symbols. If the number of declarations +
11495f757f3fSDimitry Andric // definitions to a symbol exceeds the amount of times they're marked as
11505f757f3fSDimitry Andric // tagged, it means we have an objfile that uses the untagged variant of the
11515f757f3fSDimitry Andric // symbol.
11525f757f3fSDimitry Andric for (InputFile *file : files) {
11535f757f3fSDimitry Andric if (file->kind() != InputFile::BinaryKind &&
11545f757f3fSDimitry Andric file->kind() != InputFile::ObjKind)
11555f757f3fSDimitry Andric continue;
11565f757f3fSDimitry Andric
11575f757f3fSDimitry Andric for (Symbol *symbol : file->getSymbols()) {
11585f757f3fSDimitry Andric // See `addTaggedSymbolReferences` for more details.
11595f757f3fSDimitry Andric if (symbol->type != STT_OBJECT ||
11605f757f3fSDimitry Andric symbol->binding == STB_LOCAL)
11615f757f3fSDimitry Andric continue;
11625f757f3fSDimitry Andric auto it = taggedSymbolReferenceCount.find(symbol);
11635f757f3fSDimitry Andric if (it == taggedSymbolReferenceCount.end()) continue;
11645f757f3fSDimitry Andric unsigned &remainingAllowedTaggedRefs = it->second;
11655f757f3fSDimitry Andric if (remainingAllowedTaggedRefs == 0) {
11665f757f3fSDimitry Andric taggedSymbolReferenceCount.erase(it);
11675f757f3fSDimitry Andric continue;
11685f757f3fSDimitry Andric }
11695f757f3fSDimitry Andric --remainingAllowedTaggedRefs;
11705f757f3fSDimitry Andric }
11715f757f3fSDimitry Andric }
11725f757f3fSDimitry Andric
11735f757f3fSDimitry Andric // `addTaggedSymbolReferences` has already checked that we have RELA
11745f757f3fSDimitry Andric // relocations, the only other way to get written addends is with
11755f757f3fSDimitry Andric // --apply-dynamic-relocs.
11765f757f3fSDimitry Andric if (!taggedSymbolReferenceCount.empty() && config->writeAddends)
11775f757f3fSDimitry Andric error("--apply-dynamic-relocs cannot be used with MTE globals");
11785f757f3fSDimitry Andric
11795f757f3fSDimitry Andric // Now, `taggedSymbolReferenceCount` should only contain symbols that are
11805f757f3fSDimitry Andric // defined as tagged exactly the same amount as it's referenced, meaning all
11815f757f3fSDimitry Andric // uses are tagged.
11825f757f3fSDimitry Andric for (auto &[symbol, remainingTaggedRefs] : taggedSymbolReferenceCount) {
11835f757f3fSDimitry Andric assert(remainingTaggedRefs == 0 &&
11845f757f3fSDimitry Andric "Symbol is defined as tagged more times than it's used");
11855f757f3fSDimitry Andric symbol->setIsTagged(true);
11865f757f3fSDimitry Andric }
11875f757f3fSDimitry Andric }
1188