xref: /openbsd-src/gnu/llvm/lld/ELF/Arch/ARM.cpp (revision dfe94b169149f14cc1aee2cf6dad58a8d9a1860c)
1ece8a530Spatrick //===- ARM.cpp ------------------------------------------------------------===//
2ece8a530Spatrick //
3ece8a530Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ece8a530Spatrick // See https://llvm.org/LICENSE.txt for license information.
5ece8a530Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ece8a530Spatrick //
7ece8a530Spatrick //===----------------------------------------------------------------------===//
8ece8a530Spatrick 
9ece8a530Spatrick #include "Symbols.h"
10ece8a530Spatrick #include "SyntheticSections.h"
11ece8a530Spatrick #include "Target.h"
12ece8a530Spatrick #include "lld/Common/ErrorHandler.h"
13*dfe94b16Srobert #include "llvm/BinaryFormat/ELF.h"
14ece8a530Spatrick #include "llvm/Support/Endian.h"
15ece8a530Spatrick 
16ece8a530Spatrick using namespace llvm;
17ece8a530Spatrick using namespace llvm::support::endian;
18ece8a530Spatrick using namespace llvm::ELF;
19bb684c34Spatrick using namespace lld;
20bb684c34Spatrick using namespace lld::elf;
21ece8a530Spatrick 
22ece8a530Spatrick namespace {
23ece8a530Spatrick class ARM final : public TargetInfo {
24ece8a530Spatrick public:
25ece8a530Spatrick   ARM();
26ece8a530Spatrick   uint32_t calcEFlags() const override;
27ece8a530Spatrick   RelExpr getRelExpr(RelType type, const Symbol &s,
28ece8a530Spatrick                      const uint8_t *loc) const override;
29ece8a530Spatrick   RelType getDynRel(RelType type) const override;
30ece8a530Spatrick   int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
31ece8a530Spatrick   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
32ece8a530Spatrick   void writeIgotPlt(uint8_t *buf, const Symbol &s) const override;
33ece8a530Spatrick   void writePltHeader(uint8_t *buf) const override;
34ece8a530Spatrick   void writePlt(uint8_t *buf, const Symbol &sym,
35ece8a530Spatrick                 uint64_t pltEntryAddr) const override;
36ece8a530Spatrick   void addPltSymbols(InputSection &isec, uint64_t off) const override;
37ece8a530Spatrick   void addPltHeaderSymbols(InputSection &isd) const override;
38ece8a530Spatrick   bool needsThunk(RelExpr expr, RelType type, const InputFile *file,
39ece8a530Spatrick                   uint64_t branchAddr, const Symbol &s,
40ece8a530Spatrick                   int64_t a) const override;
41ece8a530Spatrick   uint32_t getThunkSectionSpacing() const override;
42ece8a530Spatrick   bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
43bb684c34Spatrick   void relocate(uint8_t *loc, const Relocation &rel,
44bb684c34Spatrick                 uint64_t val) const override;
45ece8a530Spatrick };
46ece8a530Spatrick } // namespace
47ece8a530Spatrick 
ARM()48ece8a530Spatrick ARM::ARM() {
49ece8a530Spatrick   copyRel = R_ARM_COPY;
50ece8a530Spatrick   relativeRel = R_ARM_RELATIVE;
51ece8a530Spatrick   iRelativeRel = R_ARM_IRELATIVE;
52ece8a530Spatrick   gotRel = R_ARM_GLOB_DAT;
53ece8a530Spatrick   pltRel = R_ARM_JUMP_SLOT;
54ece8a530Spatrick   symbolicRel = R_ARM_ABS32;
55ece8a530Spatrick   tlsGotRel = R_ARM_TLS_TPOFF32;
56ece8a530Spatrick   tlsModuleIndexRel = R_ARM_TLS_DTPMOD32;
57ece8a530Spatrick   tlsOffsetRel = R_ARM_TLS_DTPOFF32;
58ece8a530Spatrick   pltHeaderSize = 32;
59ece8a530Spatrick   pltEntrySize = 16;
60ece8a530Spatrick   ipltEntrySize = 16;
61ece8a530Spatrick   trapInstr = {0xd4, 0xd4, 0xd4, 0xd4};
62ece8a530Spatrick   needsThunks = true;
63bb684c34Spatrick   defaultMaxPageSize = 65536;
64ece8a530Spatrick }
65ece8a530Spatrick 
calcEFlags() const66ece8a530Spatrick uint32_t ARM::calcEFlags() const {
67ece8a530Spatrick   // The ABIFloatType is used by loaders to detect the floating point calling
68ece8a530Spatrick   // convention.
69ece8a530Spatrick   uint32_t abiFloatType = 0;
70ece8a530Spatrick   if (config->armVFPArgs == ARMVFPArgKind::Base ||
71ece8a530Spatrick       config->armVFPArgs == ARMVFPArgKind::Default)
72ece8a530Spatrick     abiFloatType = EF_ARM_ABI_FLOAT_SOFT;
73ece8a530Spatrick   else if (config->armVFPArgs == ARMVFPArgKind::VFP)
74ece8a530Spatrick     abiFloatType = EF_ARM_ABI_FLOAT_HARD;
75ece8a530Spatrick 
76ece8a530Spatrick   // We don't currently use any features incompatible with EF_ARM_EABI_VER5,
77ece8a530Spatrick   // but we don't have any firm guarantees of conformance. Linux AArch64
78ece8a530Spatrick   // kernels (as of 2016) require an EABI version to be set.
79ece8a530Spatrick   return EF_ARM_EABI_VER5 | abiFloatType;
80ece8a530Spatrick }
81ece8a530Spatrick 
getRelExpr(RelType type,const Symbol & s,const uint8_t * loc) const82ece8a530Spatrick RelExpr ARM::getRelExpr(RelType type, const Symbol &s,
83ece8a530Spatrick                         const uint8_t *loc) const {
84ece8a530Spatrick   switch (type) {
85*dfe94b16Srobert   case R_ARM_ABS32:
86*dfe94b16Srobert   case R_ARM_MOVW_ABS_NC:
87*dfe94b16Srobert   case R_ARM_MOVT_ABS:
88*dfe94b16Srobert   case R_ARM_THM_MOVW_ABS_NC:
89*dfe94b16Srobert   case R_ARM_THM_MOVT_ABS:
90*dfe94b16Srobert     return R_ABS;
91*dfe94b16Srobert   case R_ARM_THM_JUMP8:
92ece8a530Spatrick   case R_ARM_THM_JUMP11:
93ece8a530Spatrick     return R_PC;
94ece8a530Spatrick   case R_ARM_CALL:
95ece8a530Spatrick   case R_ARM_JUMP24:
96ece8a530Spatrick   case R_ARM_PC24:
97ece8a530Spatrick   case R_ARM_PLT32:
98ece8a530Spatrick   case R_ARM_PREL31:
99ece8a530Spatrick   case R_ARM_THM_JUMP19:
100ece8a530Spatrick   case R_ARM_THM_JUMP24:
101ece8a530Spatrick   case R_ARM_THM_CALL:
102ece8a530Spatrick     return R_PLT_PC;
103ece8a530Spatrick   case R_ARM_GOTOFF32:
104ece8a530Spatrick     // (S + A) - GOT_ORG
105ece8a530Spatrick     return R_GOTREL;
106ece8a530Spatrick   case R_ARM_GOT_BREL:
107ece8a530Spatrick     // GOT(S) + A - GOT_ORG
108ece8a530Spatrick     return R_GOT_OFF;
109ece8a530Spatrick   case R_ARM_GOT_PREL:
110ece8a530Spatrick   case R_ARM_TLS_IE32:
111ece8a530Spatrick     // GOT(S) + A - P
112ece8a530Spatrick     return R_GOT_PC;
113ece8a530Spatrick   case R_ARM_SBREL32:
114ece8a530Spatrick     return R_ARM_SBREL;
115ece8a530Spatrick   case R_ARM_TARGET1:
116ece8a530Spatrick     return config->target1Rel ? R_PC : R_ABS;
117ece8a530Spatrick   case R_ARM_TARGET2:
118ece8a530Spatrick     if (config->target2 == Target2Policy::Rel)
119ece8a530Spatrick       return R_PC;
120ece8a530Spatrick     if (config->target2 == Target2Policy::Abs)
121ece8a530Spatrick       return R_ABS;
122ece8a530Spatrick     return R_GOT_PC;
123ece8a530Spatrick   case R_ARM_TLS_GD32:
124ece8a530Spatrick     return R_TLSGD_PC;
125ece8a530Spatrick   case R_ARM_TLS_LDM32:
126ece8a530Spatrick     return R_TLSLD_PC;
127bb684c34Spatrick   case R_ARM_TLS_LDO32:
128bb684c34Spatrick     return R_DTPREL;
129ece8a530Spatrick   case R_ARM_BASE_PREL:
130ece8a530Spatrick     // B(S) + A - P
131ece8a530Spatrick     // FIXME: currently B(S) assumed to be .got, this may not hold for all
132ece8a530Spatrick     // platforms.
133ece8a530Spatrick     return R_GOTONLY_PC;
134ece8a530Spatrick   case R_ARM_MOVW_PREL_NC:
135ece8a530Spatrick   case R_ARM_MOVT_PREL:
136ece8a530Spatrick   case R_ARM_REL32:
137ece8a530Spatrick   case R_ARM_THM_MOVW_PREL_NC:
138ece8a530Spatrick   case R_ARM_THM_MOVT_PREL:
139ece8a530Spatrick     return R_PC;
140bb684c34Spatrick   case R_ARM_ALU_PC_G0:
141*dfe94b16Srobert   case R_ARM_ALU_PC_G0_NC:
142*dfe94b16Srobert   case R_ARM_ALU_PC_G1:
143*dfe94b16Srobert   case R_ARM_ALU_PC_G1_NC:
144*dfe94b16Srobert   case R_ARM_ALU_PC_G2:
145bb684c34Spatrick   case R_ARM_LDR_PC_G0:
146*dfe94b16Srobert   case R_ARM_LDR_PC_G1:
147*dfe94b16Srobert   case R_ARM_LDR_PC_G2:
148*dfe94b16Srobert   case R_ARM_LDRS_PC_G0:
149*dfe94b16Srobert   case R_ARM_LDRS_PC_G1:
150*dfe94b16Srobert   case R_ARM_LDRS_PC_G2:
151bb684c34Spatrick   case R_ARM_THM_ALU_PREL_11_0:
152bb684c34Spatrick   case R_ARM_THM_PC8:
153bb684c34Spatrick   case R_ARM_THM_PC12:
154bb684c34Spatrick     return R_ARM_PCA;
155bb684c34Spatrick   case R_ARM_MOVW_BREL_NC:
156bb684c34Spatrick   case R_ARM_MOVW_BREL:
157bb684c34Spatrick   case R_ARM_MOVT_BREL:
158bb684c34Spatrick   case R_ARM_THM_MOVW_BREL_NC:
159bb684c34Spatrick   case R_ARM_THM_MOVW_BREL:
160bb684c34Spatrick   case R_ARM_THM_MOVT_BREL:
161bb684c34Spatrick     return R_ARM_SBREL;
162ece8a530Spatrick   case R_ARM_NONE:
163ece8a530Spatrick     return R_NONE;
164ece8a530Spatrick   case R_ARM_TLS_LE32:
1651cf9926bSpatrick     return R_TPREL;
166ece8a530Spatrick   case R_ARM_V4BX:
167ece8a530Spatrick     // V4BX is just a marker to indicate there's a "bx rN" instruction at the
168ece8a530Spatrick     // given address. It can be used to implement a special linker mode which
169ece8a530Spatrick     // rewrites ARMv4T inputs to ARMv4. Since we support only ARMv4 input and
170ece8a530Spatrick     // not ARMv4 output, we can just ignore it.
171ece8a530Spatrick     return R_NONE;
172ece8a530Spatrick   default:
173*dfe94b16Srobert     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
174*dfe94b16Srobert           ") against symbol " + toString(s));
175*dfe94b16Srobert     return R_NONE;
176ece8a530Spatrick   }
177ece8a530Spatrick }
178ece8a530Spatrick 
getDynRel(RelType type) const179ece8a530Spatrick RelType ARM::getDynRel(RelType type) const {
180ece8a530Spatrick   if ((type == R_ARM_ABS32) || (type == R_ARM_TARGET1 && !config->target1Rel))
181ece8a530Spatrick     return R_ARM_ABS32;
182ece8a530Spatrick   return R_ARM_NONE;
183ece8a530Spatrick }
184ece8a530Spatrick 
writeGotPlt(uint8_t * buf,const Symbol &) const185ece8a530Spatrick void ARM::writeGotPlt(uint8_t *buf, const Symbol &) const {
186ece8a530Spatrick   write32le(buf, in.plt->getVA());
187ece8a530Spatrick }
188ece8a530Spatrick 
writeIgotPlt(uint8_t * buf,const Symbol & s) const189ece8a530Spatrick void ARM::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
190ece8a530Spatrick   // An ARM entry is the address of the ifunc resolver function.
191ece8a530Spatrick   write32le(buf, s.getVA());
192ece8a530Spatrick }
193ece8a530Spatrick 
194ece8a530Spatrick // Long form PLT Header that does not have any restrictions on the displacement
195*dfe94b16Srobert // of the .plt from the .got.plt.
writePltHeaderLong(uint8_t * buf)196ece8a530Spatrick static void writePltHeaderLong(uint8_t *buf) {
197ece8a530Spatrick   const uint8_t pltData[] = {
198ece8a530Spatrick       0x04, 0xe0, 0x2d, 0xe5, //     str lr, [sp,#-4]!
199ece8a530Spatrick       0x04, 0xe0, 0x9f, 0xe5, //     ldr lr, L2
200ece8a530Spatrick       0x0e, 0xe0, 0x8f, 0xe0, // L1: add lr, pc, lr
201ece8a530Spatrick       0x08, 0xf0, 0xbe, 0xe5, //     ldr pc, [lr, #8]
202ece8a530Spatrick       0x00, 0x00, 0x00, 0x00, // L2: .word   &(.got.plt) - L1 - 8
203ece8a530Spatrick       0xd4, 0xd4, 0xd4, 0xd4, //     Pad to 32-byte boundary
204ece8a530Spatrick       0xd4, 0xd4, 0xd4, 0xd4, //     Pad to 32-byte boundary
205ece8a530Spatrick       0xd4, 0xd4, 0xd4, 0xd4};
206ece8a530Spatrick   memcpy(buf, pltData, sizeof(pltData));
207ece8a530Spatrick   uint64_t gotPlt = in.gotPlt->getVA();
208ece8a530Spatrick   uint64_t l1 = in.plt->getVA() + 8;
209ece8a530Spatrick   write32le(buf + 16, gotPlt - l1 - 8);
210ece8a530Spatrick }
211ece8a530Spatrick 
212*dfe94b16Srobert // The default PLT header requires the .got.plt to be within 128 Mb of the
213ece8a530Spatrick // .plt in the positive direction.
writePltHeader(uint8_t * buf) const214ece8a530Spatrick void ARM::writePltHeader(uint8_t *buf) const {
215ece8a530Spatrick   // Use a similar sequence to that in writePlt(), the difference is the calling
216ece8a530Spatrick   // conventions mean we use lr instead of ip. The PLT entry is responsible for
217ece8a530Spatrick   // saving lr on the stack, the dynamic loader is responsible for reloading
218ece8a530Spatrick   // it.
219ece8a530Spatrick   const uint32_t pltData[] = {
220ece8a530Spatrick       0xe52de004, // L1: str lr, [sp,#-4]!
221ece8a530Spatrick       0xe28fe600, //     add lr, pc,  #0x0NN00000 &(.got.plt - L1 - 4)
222ece8a530Spatrick       0xe28eea00, //     add lr, lr,  #0x000NN000 &(.got.plt - L1 - 4)
223ece8a530Spatrick       0xe5bef000, //     ldr pc, [lr, #0x00000NNN] &(.got.plt -L1 - 4)
224ece8a530Spatrick   };
225ece8a530Spatrick 
226ece8a530Spatrick   uint64_t offset = in.gotPlt->getVA() - in.plt->getVA() - 4;
227ece8a530Spatrick   if (!llvm::isUInt<27>(offset)) {
228ece8a530Spatrick     // We cannot encode the Offset, use the long form.
229ece8a530Spatrick     writePltHeaderLong(buf);
230ece8a530Spatrick     return;
231ece8a530Spatrick   }
232ece8a530Spatrick   write32le(buf + 0, pltData[0]);
233ece8a530Spatrick   write32le(buf + 4, pltData[1] | ((offset >> 20) & 0xff));
234ece8a530Spatrick   write32le(buf + 8, pltData[2] | ((offset >> 12) & 0xff));
235ece8a530Spatrick   write32le(buf + 12, pltData[3] | (offset & 0xfff));
236ece8a530Spatrick   memcpy(buf + 16, trapInstr.data(), 4); // Pad to 32-byte boundary
237ece8a530Spatrick   memcpy(buf + 20, trapInstr.data(), 4);
238ece8a530Spatrick   memcpy(buf + 24, trapInstr.data(), 4);
239ece8a530Spatrick   memcpy(buf + 28, trapInstr.data(), 4);
240ece8a530Spatrick }
241ece8a530Spatrick 
addPltHeaderSymbols(InputSection & isec) const242ece8a530Spatrick void ARM::addPltHeaderSymbols(InputSection &isec) const {
243ece8a530Spatrick   addSyntheticLocal("$a", STT_NOTYPE, 0, 0, isec);
244ece8a530Spatrick   addSyntheticLocal("$d", STT_NOTYPE, 16, 0, isec);
245ece8a530Spatrick }
246ece8a530Spatrick 
247ece8a530Spatrick // Long form PLT entries that do not have any restrictions on the displacement
248*dfe94b16Srobert // of the .plt from the .got.plt.
writePltLong(uint8_t * buf,uint64_t gotPltEntryAddr,uint64_t pltEntryAddr)249ece8a530Spatrick static void writePltLong(uint8_t *buf, uint64_t gotPltEntryAddr,
250ece8a530Spatrick                          uint64_t pltEntryAddr) {
251ece8a530Spatrick   const uint8_t pltData[] = {
252ece8a530Spatrick       0x04, 0xc0, 0x9f, 0xe5, //     ldr ip, L2
253ece8a530Spatrick       0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
254ece8a530Spatrick       0x00, 0xf0, 0x9c, 0xe5, //     ldr pc, [ip]
255*dfe94b16Srobert       0x00, 0x00, 0x00, 0x00, // L2: .word   Offset(&(.got.plt) - L1 - 8
256ece8a530Spatrick   };
257ece8a530Spatrick   memcpy(buf, pltData, sizeof(pltData));
258ece8a530Spatrick   uint64_t l1 = pltEntryAddr + 4;
259ece8a530Spatrick   write32le(buf + 12, gotPltEntryAddr - l1 - 8);
260ece8a530Spatrick }
261ece8a530Spatrick 
262*dfe94b16Srobert // The default PLT entries require the .got.plt to be within 128 Mb of the
263ece8a530Spatrick // .plt in the positive direction.
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr) const264ece8a530Spatrick void ARM::writePlt(uint8_t *buf, const Symbol &sym,
265ece8a530Spatrick                    uint64_t pltEntryAddr) const {
266ece8a530Spatrick   // The PLT entry is similar to the example given in Appendix A of ELF for
267ece8a530Spatrick   // the Arm Architecture. Instead of using the Group Relocations to find the
268ece8a530Spatrick   // optimal rotation for the 8-bit immediate used in the add instructions we
269ece8a530Spatrick   // hard code the most compact rotations for simplicity. This saves a load
270ece8a530Spatrick   // instruction over the long plt sequences.
271ece8a530Spatrick   const uint32_t pltData[] = {
272*dfe94b16Srobert       0xe28fc600, // L1: add ip, pc,  #0x0NN00000  Offset(&(.got.plt) - L1 - 8
273*dfe94b16Srobert       0xe28cca00, //     add ip, ip,  #0x000NN000  Offset(&(.got.plt) - L1 - 8
274*dfe94b16Srobert       0xe5bcf000, //     ldr pc, [ip, #0x00000NNN] Offset(&(.got.plt) - L1 - 8
275ece8a530Spatrick   };
276ece8a530Spatrick 
277ece8a530Spatrick   uint64_t offset = sym.getGotPltVA() - pltEntryAddr - 8;
278ece8a530Spatrick   if (!llvm::isUInt<27>(offset)) {
279ece8a530Spatrick     // We cannot encode the Offset, use the long form.
280ece8a530Spatrick     writePltLong(buf, sym.getGotPltVA(), pltEntryAddr);
281ece8a530Spatrick     return;
282ece8a530Spatrick   }
283ece8a530Spatrick   write32le(buf + 0, pltData[0] | ((offset >> 20) & 0xff));
284ece8a530Spatrick   write32le(buf + 4, pltData[1] | ((offset >> 12) & 0xff));
285ece8a530Spatrick   write32le(buf + 8, pltData[2] | (offset & 0xfff));
286ece8a530Spatrick   memcpy(buf + 12, trapInstr.data(), 4); // Pad to 16-byte boundary
287ece8a530Spatrick }
288ece8a530Spatrick 
addPltSymbols(InputSection & isec,uint64_t off) const289ece8a530Spatrick void ARM::addPltSymbols(InputSection &isec, uint64_t off) const {
290ece8a530Spatrick   addSyntheticLocal("$a", STT_NOTYPE, off, 0, isec);
291ece8a530Spatrick   addSyntheticLocal("$d", STT_NOTYPE, off + 12, 0, isec);
292ece8a530Spatrick }
293ece8a530Spatrick 
needsThunk(RelExpr expr,RelType type,const InputFile * file,uint64_t branchAddr,const Symbol & s,int64_t a) const294ece8a530Spatrick bool ARM::needsThunk(RelExpr expr, RelType type, const InputFile *file,
295bb684c34Spatrick                      uint64_t branchAddr, const Symbol &s,
2961cf9926bSpatrick                      int64_t a) const {
297*dfe94b16Srobert   // If s is an undefined weak symbol and does not have a PLT entry then it will
298*dfe94b16Srobert   // be resolved as a branch to the next instruction. If it is hidden, its
299*dfe94b16Srobert   // binding has been converted to local, so we just check isUndefined() here. A
300*dfe94b16Srobert   // undefined non-weak symbol will have been errored.
301*dfe94b16Srobert   if (s.isUndefined() && !s.isInPlt())
302ece8a530Spatrick     return false;
303ece8a530Spatrick   // A state change from ARM to Thumb and vice versa must go through an
304ece8a530Spatrick   // interworking thunk if the relocation type is not R_ARM_CALL or
305ece8a530Spatrick   // R_ARM_THM_CALL.
306ece8a530Spatrick   switch (type) {
307ece8a530Spatrick   case R_ARM_PC24:
308ece8a530Spatrick   case R_ARM_PLT32:
309ece8a530Spatrick   case R_ARM_JUMP24:
310ece8a530Spatrick     // Source is ARM, all PLT entries are ARM so no interworking required.
311ece8a530Spatrick     // Otherwise we need to interwork if STT_FUNC Symbol has bit 0 set (Thumb).
312ece8a530Spatrick     if (s.isFunc() && expr == R_PC && (s.getVA() & 1))
313ece8a530Spatrick       return true;
314*dfe94b16Srobert     [[fallthrough]];
315ece8a530Spatrick   case R_ARM_CALL: {
316ece8a530Spatrick     uint64_t dst = (expr == R_PLT_PC) ? s.getPltVA() : s.getVA();
317*dfe94b16Srobert     return !inBranchRange(type, branchAddr, dst + a) ||
318*dfe94b16Srobert         (!config->armHasBlx && (s.getVA() & 1));
319ece8a530Spatrick   }
320ece8a530Spatrick   case R_ARM_THM_JUMP19:
321ece8a530Spatrick   case R_ARM_THM_JUMP24:
322ece8a530Spatrick     // Source is Thumb, all PLT entries are ARM so interworking is required.
323ece8a530Spatrick     // Otherwise we need to interwork if STT_FUNC Symbol has bit 0 clear (ARM).
324ece8a530Spatrick     if (expr == R_PLT_PC || (s.isFunc() && (s.getVA() & 1) == 0))
325ece8a530Spatrick       return true;
326*dfe94b16Srobert     [[fallthrough]];
327ece8a530Spatrick   case R_ARM_THM_CALL: {
328ece8a530Spatrick     uint64_t dst = (expr == R_PLT_PC) ? s.getPltVA() : s.getVA();
329*dfe94b16Srobert     return !inBranchRange(type, branchAddr, dst + a) ||
330*dfe94b16Srobert         (!config->armHasBlx && (s.getVA() & 1) == 0);;
331ece8a530Spatrick   }
332ece8a530Spatrick   }
333ece8a530Spatrick   return false;
334ece8a530Spatrick }
335ece8a530Spatrick 
getThunkSectionSpacing() const336ece8a530Spatrick uint32_t ARM::getThunkSectionSpacing() const {
337ece8a530Spatrick   // The placing of pre-created ThunkSections is controlled by the value
338ece8a530Spatrick   // thunkSectionSpacing returned by getThunkSectionSpacing(). The aim is to
339ece8a530Spatrick   // place the ThunkSection such that all branches from the InputSections
340ece8a530Spatrick   // prior to the ThunkSection can reach a Thunk placed at the end of the
341ece8a530Spatrick   // ThunkSection. Graphically:
342ece8a530Spatrick   // | up to thunkSectionSpacing .text input sections |
343ece8a530Spatrick   // | ThunkSection                                   |
344ece8a530Spatrick   // | up to thunkSectionSpacing .text input sections |
345ece8a530Spatrick   // | ThunkSection                                   |
346ece8a530Spatrick 
347ece8a530Spatrick   // Pre-created ThunkSections are spaced roughly 16MiB apart on ARMv7. This
348ece8a530Spatrick   // is to match the most common expected case of a Thumb 2 encoded BL, BLX or
349ece8a530Spatrick   // B.W:
350ece8a530Spatrick   // ARM B, BL, BLX range +/- 32MiB
351ece8a530Spatrick   // Thumb B.W, BL, BLX range +/- 16MiB
352ece8a530Spatrick   // Thumb B<cc>.W range +/- 1MiB
353ece8a530Spatrick   // If a branch cannot reach a pre-created ThunkSection a new one will be
354ece8a530Spatrick   // created so we can handle the rare cases of a Thumb 2 conditional branch.
355ece8a530Spatrick   // We intentionally use a lower size for thunkSectionSpacing than the maximum
356ece8a530Spatrick   // branch range so the end of the ThunkSection is more likely to be within
357ece8a530Spatrick   // range of the branch instruction that is furthest away. The value we shorten
358ece8a530Spatrick   // thunkSectionSpacing by is set conservatively to allow us to create 16,384
359ece8a530Spatrick   // 12 byte Thunks at any offset in a ThunkSection without risk of a branch to
360ece8a530Spatrick   // one of the Thunks going out of range.
361ece8a530Spatrick 
362ece8a530Spatrick   // On Arm the thunkSectionSpacing depends on the range of the Thumb Branch
363ece8a530Spatrick   // range. On earlier Architectures such as ARMv4, ARMv5 and ARMv6 (except
364ece8a530Spatrick   // ARMv6T2) the range is +/- 4MiB.
365ece8a530Spatrick 
366ece8a530Spatrick   return (config->armJ1J2BranchEncoding) ? 0x1000000 - 0x30000
367ece8a530Spatrick                                          : 0x400000 - 0x7500;
368ece8a530Spatrick }
369ece8a530Spatrick 
inBranchRange(RelType type,uint64_t src,uint64_t dst) const370ece8a530Spatrick bool ARM::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
371ece8a530Spatrick   if ((dst & 0x1) == 0)
372ece8a530Spatrick     // Destination is ARM, if ARM caller then Src is already 4-byte aligned.
373ece8a530Spatrick     // If Thumb Caller (BLX) the Src address has bottom 2 bits cleared to ensure
374ece8a530Spatrick     // destination will be 4 byte aligned.
375ece8a530Spatrick     src &= ~0x3;
376ece8a530Spatrick   else
3771cf9926bSpatrick     // Bit 0 == 1 denotes Thumb state, it is not part of the range.
378ece8a530Spatrick     dst &= ~0x1;
379ece8a530Spatrick 
3801cf9926bSpatrick   int64_t offset = dst - src;
3811cf9926bSpatrick   switch (type) {
3821cf9926bSpatrick   case R_ARM_PC24:
3831cf9926bSpatrick   case R_ARM_PLT32:
3841cf9926bSpatrick   case R_ARM_JUMP24:
3851cf9926bSpatrick   case R_ARM_CALL:
3861cf9926bSpatrick     return llvm::isInt<26>(offset);
3871cf9926bSpatrick   case R_ARM_THM_JUMP19:
3881cf9926bSpatrick     return llvm::isInt<21>(offset);
3891cf9926bSpatrick   case R_ARM_THM_JUMP24:
3901cf9926bSpatrick   case R_ARM_THM_CALL:
3911cf9926bSpatrick     return config->armJ1J2BranchEncoding ? llvm::isInt<25>(offset)
3921cf9926bSpatrick                                          : llvm::isInt<23>(offset);
3931cf9926bSpatrick   default:
3941cf9926bSpatrick     return true;
3951cf9926bSpatrick   }
396ece8a530Spatrick }
397ece8a530Spatrick 
398bb684c34Spatrick // Helper to produce message text when LLD detects that a CALL relocation to
399bb684c34Spatrick // a non STT_FUNC symbol that may result in incorrect interworking between ARM
400bb684c34Spatrick // or Thumb.
stateChangeWarning(uint8_t * loc,RelType relt,const Symbol & s)401bb684c34Spatrick static void stateChangeWarning(uint8_t *loc, RelType relt, const Symbol &s) {
402bb684c34Spatrick   assert(!s.isFunc());
403*dfe94b16Srobert   const ErrorPlace place = getErrorPlace(loc);
404*dfe94b16Srobert   std::string hint;
405*dfe94b16Srobert   if (!place.srcLoc.empty())
406*dfe94b16Srobert     hint = "; " + place.srcLoc;
407bb684c34Spatrick   if (s.isSection()) {
408bb684c34Spatrick     // Section symbols must be defined and in a section. Users cannot change
409bb684c34Spatrick     // the type. Use the section name as getName() returns an empty string.
410*dfe94b16Srobert     warn(place.loc + "branch and link relocation: " + toString(relt) +
411*dfe94b16Srobert          " to STT_SECTION symbol " + cast<Defined>(s).section->name +
412*dfe94b16Srobert          " ; interworking not performed" + hint);
413bb684c34Spatrick   } else {
414bb684c34Spatrick     // Warn with hint on how to alter the symbol type.
415bb684c34Spatrick     warn(getErrorLocation(loc) + "branch and link relocation: " +
416bb684c34Spatrick          toString(relt) + " to non STT_FUNC symbol: " + s.getName() +
417bb684c34Spatrick          " interworking not performed; consider using directive '.type " +
418bb684c34Spatrick          s.getName() +
419*dfe94b16Srobert          ", %function' to give symbol type STT_FUNC if interworking between "
420*dfe94b16Srobert          "ARM and Thumb is required" +
421*dfe94b16Srobert          hint);
422bb684c34Spatrick   }
423bb684c34Spatrick }
424bb684c34Spatrick 
425bb684c34Spatrick // Rotate a 32-bit unsigned value right by a specified amt of bits.
rotr32(uint32_t val,uint32_t amt)426bb684c34Spatrick static uint32_t rotr32(uint32_t val, uint32_t amt) {
427bb684c34Spatrick   assert(amt < 32 && "Invalid rotate amount");
428bb684c34Spatrick   return (val >> amt) | (val << ((32 - amt) & 31));
429bb684c34Spatrick }
430bb684c34Spatrick 
getRemAndLZForGroup(unsigned group,uint32_t val)431*dfe94b16Srobert static std::pair<uint32_t, uint32_t> getRemAndLZForGroup(unsigned group,
432*dfe94b16Srobert                                                          uint32_t val) {
433*dfe94b16Srobert   uint32_t rem, lz;
434*dfe94b16Srobert   do {
435*dfe94b16Srobert     lz = llvm::countLeadingZeros(val) & ~1;
436*dfe94b16Srobert     rem = val;
437*dfe94b16Srobert     if (lz == 32) // implies rem == 0
438*dfe94b16Srobert       break;
439*dfe94b16Srobert     val &= 0xffffff >> lz;
440*dfe94b16Srobert   } while (group--);
441*dfe94b16Srobert   return {rem, lz};
442bb684c34Spatrick }
443bb684c34Spatrick 
encodeAluGroup(uint8_t * loc,const Relocation & rel,uint64_t val,int group,bool check)444*dfe94b16Srobert static void encodeAluGroup(uint8_t *loc, const Relocation &rel, uint64_t val,
445*dfe94b16Srobert                            int group, bool check) {
446*dfe94b16Srobert   // ADD/SUB (immediate) add = bit23, sub = bit22
447*dfe94b16Srobert   // immediate field carries is a 12-bit modified immediate, made up of a 4-bit
448*dfe94b16Srobert   // even rotate right and an 8-bit immediate.
449*dfe94b16Srobert   uint32_t opcode = 0x00800000;
450*dfe94b16Srobert   if (val >> 63) {
451*dfe94b16Srobert     opcode = 0x00400000;
452*dfe94b16Srobert     val = -val;
453*dfe94b16Srobert   }
454*dfe94b16Srobert   uint32_t imm, lz;
455*dfe94b16Srobert   std::tie(imm, lz) = getRemAndLZForGroup(group, val);
456*dfe94b16Srobert   uint32_t rot = 0;
457*dfe94b16Srobert   if (lz < 24) {
458*dfe94b16Srobert     imm = rotr32(imm, 24 - lz);
459*dfe94b16Srobert     rot = (lz + 8) << 7;
460*dfe94b16Srobert   }
461*dfe94b16Srobert   if (check && imm > 0xff)
462*dfe94b16Srobert     error(getErrorLocation(loc) + "unencodeable immediate " + Twine(val).str() +
463*dfe94b16Srobert           " for relocation " + toString(rel.type));
464*dfe94b16Srobert   write32le(loc, (read32le(loc) & 0xff3ff000) | opcode | rot | (imm & 0xff));
465bb684c34Spatrick }
466bb684c34Spatrick 
encodeLdrGroup(uint8_t * loc,const Relocation & rel,uint64_t val,int group)467*dfe94b16Srobert static void encodeLdrGroup(uint8_t *loc, const Relocation &rel, uint64_t val,
468*dfe94b16Srobert                            int group) {
469*dfe94b16Srobert   // R_ARM_LDR_PC_Gn is S + A - P, we have ((S + A) | T) - P, if S is a
470*dfe94b16Srobert   // function then addr is 0 (modulo 2) and Pa is 0 (modulo 4) so we can clear
471*dfe94b16Srobert   // bottom bit to recover S + A - P.
472*dfe94b16Srobert   if (rel.sym->isFunc())
473*dfe94b16Srobert     val &= ~0x1;
474*dfe94b16Srobert   // LDR (literal) u = bit23
475*dfe94b16Srobert   uint32_t opcode = 0x00800000;
476*dfe94b16Srobert   if (val >> 63) {
477*dfe94b16Srobert     opcode = 0x0;
478*dfe94b16Srobert     val = -val;
479*dfe94b16Srobert   }
480*dfe94b16Srobert   uint32_t imm = getRemAndLZForGroup(group, val).first;
481*dfe94b16Srobert   checkUInt(loc, imm, 12, rel);
482*dfe94b16Srobert   write32le(loc, (read32le(loc) & 0xff7ff000) | opcode | imm);
483*dfe94b16Srobert }
484*dfe94b16Srobert 
encodeLdrsGroup(uint8_t * loc,const Relocation & rel,uint64_t val,int group)485*dfe94b16Srobert static void encodeLdrsGroup(uint8_t *loc, const Relocation &rel, uint64_t val,
486*dfe94b16Srobert                             int group) {
487*dfe94b16Srobert   // R_ARM_LDRS_PC_Gn is S + A - P, we have ((S + A) | T) - P, if S is a
488*dfe94b16Srobert   // function then addr is 0 (modulo 2) and Pa is 0 (modulo 4) so we can clear
489*dfe94b16Srobert   // bottom bit to recover S + A - P.
490*dfe94b16Srobert   if (rel.sym->isFunc())
491*dfe94b16Srobert     val &= ~0x1;
492*dfe94b16Srobert   // LDRD/LDRH/LDRSB/LDRSH (literal) u = bit23
493*dfe94b16Srobert   uint32_t opcode = 0x00800000;
494*dfe94b16Srobert   if (val >> 63) {
495*dfe94b16Srobert     opcode = 0x0;
496*dfe94b16Srobert     val = -val;
497*dfe94b16Srobert   }
498*dfe94b16Srobert   uint32_t imm = getRemAndLZForGroup(group, val).first;
499*dfe94b16Srobert   checkUInt(loc, imm, 8, rel);
500*dfe94b16Srobert   write32le(loc, (read32le(loc) & 0xff7ff0f0) | opcode | ((imm & 0xf0) << 4) |
501*dfe94b16Srobert                      (imm & 0xf));
502bb684c34Spatrick }
503bb684c34Spatrick 
relocate(uint8_t * loc,const Relocation & rel,uint64_t val) const504bb684c34Spatrick void ARM::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
505bb684c34Spatrick   switch (rel.type) {
506ece8a530Spatrick   case R_ARM_ABS32:
507ece8a530Spatrick   case R_ARM_BASE_PREL:
508ece8a530Spatrick   case R_ARM_GOTOFF32:
509ece8a530Spatrick   case R_ARM_GOT_BREL:
510ece8a530Spatrick   case R_ARM_GOT_PREL:
511ece8a530Spatrick   case R_ARM_REL32:
512ece8a530Spatrick   case R_ARM_RELATIVE:
513ece8a530Spatrick   case R_ARM_SBREL32:
514ece8a530Spatrick   case R_ARM_TARGET1:
515ece8a530Spatrick   case R_ARM_TARGET2:
516ece8a530Spatrick   case R_ARM_TLS_GD32:
517ece8a530Spatrick   case R_ARM_TLS_IE32:
518ece8a530Spatrick   case R_ARM_TLS_LDM32:
519ece8a530Spatrick   case R_ARM_TLS_LDO32:
520ece8a530Spatrick   case R_ARM_TLS_LE32:
521ece8a530Spatrick   case R_ARM_TLS_TPOFF32:
522ece8a530Spatrick   case R_ARM_TLS_DTPOFF32:
523ece8a530Spatrick     write32le(loc, val);
524ece8a530Spatrick     break;
525ece8a530Spatrick   case R_ARM_PREL31:
526bb684c34Spatrick     checkInt(loc, val, 31, rel);
527ece8a530Spatrick     write32le(loc, (read32le(loc) & 0x80000000) | (val & ~0x80000000));
528ece8a530Spatrick     break;
529bb684c34Spatrick   case R_ARM_CALL: {
530bb684c34Spatrick     // R_ARM_CALL is used for BL and BLX instructions, for symbols of type
531bb684c34Spatrick     // STT_FUNC we choose whether to write a BL or BLX depending on the
532bb684c34Spatrick     // value of bit 0 of Val. With bit 0 == 1 denoting Thumb. If the symbol is
533bb684c34Spatrick     // not of type STT_FUNC then we must preserve the original instruction.
534bb684c34Spatrick     // PLT entries are always ARM state so we know we don't need to interwork.
535bb684c34Spatrick     assert(rel.sym); // R_ARM_CALL is always reached via relocate().
536bb684c34Spatrick     bool bit0Thumb = val & 1;
537bb684c34Spatrick     bool isBlx = (read32le(loc) & 0xfe000000) == 0xfa000000;
538bb684c34Spatrick     // lld 10.0 and before always used bit0Thumb when deciding to write a BLX
539bb684c34Spatrick     // even when type not STT_FUNC.
540bb684c34Spatrick     if (!rel.sym->isFunc() && isBlx != bit0Thumb)
541bb684c34Spatrick       stateChangeWarning(loc, rel.type, *rel.sym);
542bb684c34Spatrick     if (rel.sym->isFunc() ? bit0Thumb : isBlx) {
543ece8a530Spatrick       // The BLX encoding is 0xfa:H:imm24 where Val = imm24:H:'1'
544bb684c34Spatrick       checkInt(loc, val, 26, rel);
545ece8a530Spatrick       write32le(loc, 0xfa000000 |                    // opcode
546ece8a530Spatrick                          ((val & 2) << 23) |         // H
547ece8a530Spatrick                          ((val >> 2) & 0x00ffffff)); // imm24
548ece8a530Spatrick       break;
549ece8a530Spatrick     }
550ece8a530Spatrick     // BLX (always unconditional) instruction to an ARM Target, select an
551ece8a530Spatrick     // unconditional BL.
552ece8a530Spatrick     write32le(loc, 0xeb000000 | (read32le(loc) & 0x00ffffff));
553ece8a530Spatrick     // fall through as BL encoding is shared with B
554bb684c34Spatrick   }
555*dfe94b16Srobert     [[fallthrough]];
556ece8a530Spatrick   case R_ARM_JUMP24:
557ece8a530Spatrick   case R_ARM_PC24:
558ece8a530Spatrick   case R_ARM_PLT32:
559bb684c34Spatrick     checkInt(loc, val, 26, rel);
560ece8a530Spatrick     write32le(loc, (read32le(loc) & ~0x00ffffff) | ((val >> 2) & 0x00ffffff));
561ece8a530Spatrick     break;
562*dfe94b16Srobert   case R_ARM_THM_JUMP8:
563*dfe94b16Srobert     // We do a 9 bit check because val is right-shifted by 1 bit.
564*dfe94b16Srobert     checkInt(loc, val, 9, rel);
565*dfe94b16Srobert     write16le(loc, (read32le(loc) & 0xff00) | ((val >> 1) & 0x00ff));
566*dfe94b16Srobert     break;
567ece8a530Spatrick   case R_ARM_THM_JUMP11:
568*dfe94b16Srobert     // We do a 12 bit check because val is right-shifted by 1 bit.
569bb684c34Spatrick     checkInt(loc, val, 12, rel);
570ece8a530Spatrick     write16le(loc, (read32le(loc) & 0xf800) | ((val >> 1) & 0x07ff));
571ece8a530Spatrick     break;
572ece8a530Spatrick   case R_ARM_THM_JUMP19:
573ece8a530Spatrick     // Encoding T3: Val = S:J2:J1:imm6:imm11:0
574bb684c34Spatrick     checkInt(loc, val, 21, rel);
575ece8a530Spatrick     write16le(loc,
576ece8a530Spatrick               (read16le(loc) & 0xfbc0) |   // opcode cond
577ece8a530Spatrick                   ((val >> 10) & 0x0400) | // S
578ece8a530Spatrick                   ((val >> 12) & 0x003f)); // imm6
579ece8a530Spatrick     write16le(loc + 2,
580ece8a530Spatrick               0x8000 |                    // opcode
581ece8a530Spatrick                   ((val >> 8) & 0x0800) | // J2
582ece8a530Spatrick                   ((val >> 5) & 0x2000) | // J1
583ece8a530Spatrick                   ((val >> 1) & 0x07ff)); // imm11
584ece8a530Spatrick     break;
585bb684c34Spatrick   case R_ARM_THM_CALL: {
586bb684c34Spatrick     // R_ARM_THM_CALL is used for BL and BLX instructions, for symbols of type
587bb684c34Spatrick     // STT_FUNC we choose whether to write a BL or BLX depending on the
588bb684c34Spatrick     // value of bit 0 of Val. With bit 0 == 0 denoting ARM, if the symbol is
589bb684c34Spatrick     // not of type STT_FUNC then we must preserve the original instruction.
590bb684c34Spatrick     // PLT entries are always ARM state so we know we need to interwork.
591bb684c34Spatrick     assert(rel.sym); // R_ARM_THM_CALL is always reached via relocate().
592bb684c34Spatrick     bool bit0Thumb = val & 1;
593bb684c34Spatrick     bool isBlx = (read16le(loc + 2) & 0x1000) == 0;
594bb684c34Spatrick     // lld 10.0 and before always used bit0Thumb when deciding to write a BLX
595bb684c34Spatrick     // even when type not STT_FUNC. PLT entries generated by LLD are always ARM.
596bb684c34Spatrick     if (!rel.sym->isFunc() && !rel.sym->isInPlt() && isBlx == bit0Thumb)
597bb684c34Spatrick       stateChangeWarning(loc, rel.type, *rel.sym);
598bb684c34Spatrick     if (rel.sym->isFunc() || rel.sym->isInPlt() ? !bit0Thumb : isBlx) {
599bb684c34Spatrick       // We are writing a BLX. Ensure BLX destination is 4-byte aligned. As
600bb684c34Spatrick       // the BLX instruction may only be two byte aligned. This must be done
601bb684c34Spatrick       // before overflow check.
602ece8a530Spatrick       val = alignTo(val, 4);
603bb684c34Spatrick       write16le(loc + 2, read16le(loc + 2) & ~0x1000);
604bb684c34Spatrick     } else {
605bb684c34Spatrick       write16le(loc + 2, (read16le(loc + 2) & ~0x1000) | 1 << 12);
606ece8a530Spatrick     }
607ece8a530Spatrick     if (!config->armJ1J2BranchEncoding) {
608ece8a530Spatrick       // Older Arm architectures do not support R_ARM_THM_JUMP24 and have
609ece8a530Spatrick       // different encoding rules and range due to J1 and J2 always being 1.
610bb684c34Spatrick       checkInt(loc, val, 23, rel);
611ece8a530Spatrick       write16le(loc,
612ece8a530Spatrick                 0xf000 |                     // opcode
613ece8a530Spatrick                     ((val >> 12) & 0x07ff)); // imm11
614ece8a530Spatrick       write16le(loc + 2,
615ece8a530Spatrick                 (read16le(loc + 2) & 0xd000) | // opcode
616ece8a530Spatrick                     0x2800 |                   // J1 == J2 == 1
617ece8a530Spatrick                     ((val >> 1) & 0x07ff));    // imm11
618ece8a530Spatrick       break;
619ece8a530Spatrick     }
620bb684c34Spatrick   }
621ece8a530Spatrick     // Fall through as rest of encoding is the same as B.W
622*dfe94b16Srobert     [[fallthrough]];
623ece8a530Spatrick   case R_ARM_THM_JUMP24:
624ece8a530Spatrick     // Encoding B  T4, BL T1, BLX T2: Val = S:I1:I2:imm10:imm11:0
625bb684c34Spatrick     checkInt(loc, val, 25, rel);
626ece8a530Spatrick     write16le(loc,
627ece8a530Spatrick               0xf000 |                     // opcode
628ece8a530Spatrick                   ((val >> 14) & 0x0400) | // S
629ece8a530Spatrick                   ((val >> 12) & 0x03ff)); // imm10
630ece8a530Spatrick     write16le(loc + 2,
631ece8a530Spatrick               (read16le(loc + 2) & 0xd000) |                  // opcode
632ece8a530Spatrick                   (((~(val >> 10)) ^ (val >> 11)) & 0x2000) | // J1
633ece8a530Spatrick                   (((~(val >> 11)) ^ (val >> 13)) & 0x0800) | // J2
634ece8a530Spatrick                   ((val >> 1) & 0x07ff));                     // imm11
635ece8a530Spatrick     break;
636ece8a530Spatrick   case R_ARM_MOVW_ABS_NC:
637ece8a530Spatrick   case R_ARM_MOVW_PREL_NC:
638bb684c34Spatrick   case R_ARM_MOVW_BREL_NC:
639ece8a530Spatrick     write32le(loc, (read32le(loc) & ~0x000f0fff) | ((val & 0xf000) << 4) |
640ece8a530Spatrick                        (val & 0x0fff));
641ece8a530Spatrick     break;
642ece8a530Spatrick   case R_ARM_MOVT_ABS:
643ece8a530Spatrick   case R_ARM_MOVT_PREL:
644bb684c34Spatrick   case R_ARM_MOVT_BREL:
645ece8a530Spatrick     write32le(loc, (read32le(loc) & ~0x000f0fff) |
646ece8a530Spatrick                        (((val >> 16) & 0xf000) << 4) | ((val >> 16) & 0xfff));
647ece8a530Spatrick     break;
648ece8a530Spatrick   case R_ARM_THM_MOVT_ABS:
649ece8a530Spatrick   case R_ARM_THM_MOVT_PREL:
650bb684c34Spatrick   case R_ARM_THM_MOVT_BREL:
651ece8a530Spatrick     // Encoding T1: A = imm4:i:imm3:imm8
652ece8a530Spatrick     write16le(loc,
653ece8a530Spatrick               0xf2c0 |                     // opcode
654ece8a530Spatrick                   ((val >> 17) & 0x0400) | // i
655ece8a530Spatrick                   ((val >> 28) & 0x000f)); // imm4
656ece8a530Spatrick     write16le(loc + 2,
657ece8a530Spatrick               (read16le(loc + 2) & 0x8f00) | // opcode
658ece8a530Spatrick                   ((val >> 12) & 0x7000) |   // imm3
659ece8a530Spatrick                   ((val >> 16) & 0x00ff));   // imm8
660ece8a530Spatrick     break;
661ece8a530Spatrick   case R_ARM_THM_MOVW_ABS_NC:
662ece8a530Spatrick   case R_ARM_THM_MOVW_PREL_NC:
663bb684c34Spatrick   case R_ARM_THM_MOVW_BREL_NC:
664ece8a530Spatrick     // Encoding T3: A = imm4:i:imm3:imm8
665ece8a530Spatrick     write16le(loc,
666ece8a530Spatrick               0xf240 |                     // opcode
667ece8a530Spatrick                   ((val >> 1) & 0x0400) |  // i
668ece8a530Spatrick                   ((val >> 12) & 0x000f)); // imm4
669ece8a530Spatrick     write16le(loc + 2,
670ece8a530Spatrick               (read16le(loc + 2) & 0x8f00) | // opcode
671ece8a530Spatrick                   ((val << 4) & 0x7000) |    // imm3
672ece8a530Spatrick                   (val & 0x00ff));           // imm8
673ece8a530Spatrick     break;
674*dfe94b16Srobert   case R_ARM_ALU_PC_G0:
675*dfe94b16Srobert     encodeAluGroup(loc, rel, val, 0, true);
676bb684c34Spatrick     break;
677*dfe94b16Srobert   case R_ARM_ALU_PC_G0_NC:
678*dfe94b16Srobert     encodeAluGroup(loc, rel, val, 0, false);
679bb684c34Spatrick     break;
680*dfe94b16Srobert   case R_ARM_ALU_PC_G1:
681*dfe94b16Srobert     encodeAluGroup(loc, rel, val, 1, true);
682*dfe94b16Srobert     break;
683*dfe94b16Srobert   case R_ARM_ALU_PC_G1_NC:
684*dfe94b16Srobert     encodeAluGroup(loc, rel, val, 1, false);
685*dfe94b16Srobert     break;
686*dfe94b16Srobert   case R_ARM_ALU_PC_G2:
687*dfe94b16Srobert     encodeAluGroup(loc, rel, val, 2, true);
688*dfe94b16Srobert     break;
689*dfe94b16Srobert   case R_ARM_LDR_PC_G0:
690*dfe94b16Srobert     encodeLdrGroup(loc, rel, val, 0);
691*dfe94b16Srobert     break;
692*dfe94b16Srobert   case R_ARM_LDR_PC_G1:
693*dfe94b16Srobert     encodeLdrGroup(loc, rel, val, 1);
694*dfe94b16Srobert     break;
695*dfe94b16Srobert   case R_ARM_LDR_PC_G2:
696*dfe94b16Srobert     encodeLdrGroup(loc, rel, val, 2);
697*dfe94b16Srobert     break;
698*dfe94b16Srobert   case R_ARM_LDRS_PC_G0:
699*dfe94b16Srobert     encodeLdrsGroup(loc, rel, val, 0);
700*dfe94b16Srobert     break;
701*dfe94b16Srobert   case R_ARM_LDRS_PC_G1:
702*dfe94b16Srobert     encodeLdrsGroup(loc, rel, val, 1);
703*dfe94b16Srobert     break;
704*dfe94b16Srobert   case R_ARM_LDRS_PC_G2:
705*dfe94b16Srobert     encodeLdrsGroup(loc, rel, val, 2);
706*dfe94b16Srobert     break;
707bb684c34Spatrick   case R_ARM_THM_ALU_PREL_11_0: {
708bb684c34Spatrick     // ADR encoding T2 (sub), T3 (add) i:imm3:imm8
709bb684c34Spatrick     int64_t imm = val;
710bb684c34Spatrick     uint16_t sub = 0;
711bb684c34Spatrick     if (imm < 0) {
712bb684c34Spatrick       imm = -imm;
713bb684c34Spatrick       sub = 0x00a0;
714bb684c34Spatrick     }
715bb684c34Spatrick     checkUInt(loc, imm, 12, rel);
716bb684c34Spatrick     write16le(loc, (read16le(loc) & 0xfb0f) | sub | (imm & 0x800) >> 1);
717bb684c34Spatrick     write16le(loc + 2,
718bb684c34Spatrick               (read16le(loc + 2) & 0x8f00) | (imm & 0x700) << 4 | (imm & 0xff));
719bb684c34Spatrick     break;
720bb684c34Spatrick   }
721bb684c34Spatrick   case R_ARM_THM_PC8:
722bb684c34Spatrick     // ADR and LDR literal encoding T1 positive offset only imm8:00
723bb684c34Spatrick     // R_ARM_THM_PC8 is S + A - Pa, we have ((S + A) | T) - Pa, if S is a
724bb684c34Spatrick     // function then addr is 0 (modulo 2) and Pa is 0 (modulo 4) so we can clear
725bb684c34Spatrick     // bottom bit to recover S + A - Pa.
726bb684c34Spatrick     if (rel.sym->isFunc())
727bb684c34Spatrick       val &= ~0x1;
728bb684c34Spatrick     checkUInt(loc, val, 10, rel);
729bb684c34Spatrick     checkAlignment(loc, val, 4, rel);
730bb684c34Spatrick     write16le(loc, (read16le(loc) & 0xff00) | (val & 0x3fc) >> 2);
731bb684c34Spatrick     break;
732bb684c34Spatrick   case R_ARM_THM_PC12: {
733bb684c34Spatrick     // LDR (literal) encoding T2, add = (U == '1') imm12
734bb684c34Spatrick     // imm12 is unsigned
735bb684c34Spatrick     // R_ARM_THM_PC12 is S + A - Pa, we have ((S + A) | T) - Pa, if S is a
736bb684c34Spatrick     // function then addr is 0 (modulo 2) and Pa is 0 (modulo 4) so we can clear
737bb684c34Spatrick     // bottom bit to recover S + A - Pa.
738bb684c34Spatrick     if (rel.sym->isFunc())
739bb684c34Spatrick       val &= ~0x1;
740bb684c34Spatrick     int64_t imm12 = val;
741bb684c34Spatrick     uint16_t u = 0x0080;
742bb684c34Spatrick     if (imm12 < 0) {
743bb684c34Spatrick       imm12 = -imm12;
744bb684c34Spatrick       u = 0;
745bb684c34Spatrick     }
746bb684c34Spatrick     checkUInt(loc, imm12, 12, rel);
747bb684c34Spatrick     write16le(loc, read16le(loc) | u);
748bb684c34Spatrick     write16le(loc + 2, (read16le(loc + 2) & 0xf000) | imm12);
749bb684c34Spatrick     break;
750bb684c34Spatrick   }
751ece8a530Spatrick   default:
752*dfe94b16Srobert     llvm_unreachable("unknown relocation");
753ece8a530Spatrick   }
754ece8a530Spatrick }
755ece8a530Spatrick 
getImplicitAddend(const uint8_t * buf,RelType type) const756ece8a530Spatrick int64_t ARM::getImplicitAddend(const uint8_t *buf, RelType type) const {
757ece8a530Spatrick   switch (type) {
758ece8a530Spatrick   default:
7591cf9926bSpatrick     internalLinkerError(getErrorLocation(buf),
7601cf9926bSpatrick                         "cannot read addend for relocation " + toString(type));
761ece8a530Spatrick     return 0;
762ece8a530Spatrick   case R_ARM_ABS32:
763ece8a530Spatrick   case R_ARM_BASE_PREL:
7641cf9926bSpatrick   case R_ARM_GLOB_DAT:
765ece8a530Spatrick   case R_ARM_GOTOFF32:
766ece8a530Spatrick   case R_ARM_GOT_BREL:
767ece8a530Spatrick   case R_ARM_GOT_PREL:
7681cf9926bSpatrick   case R_ARM_IRELATIVE:
769ece8a530Spatrick   case R_ARM_REL32:
7701cf9926bSpatrick   case R_ARM_RELATIVE:
7711cf9926bSpatrick   case R_ARM_SBREL32:
772ece8a530Spatrick   case R_ARM_TARGET1:
773ece8a530Spatrick   case R_ARM_TARGET2:
7741cf9926bSpatrick   case R_ARM_TLS_DTPMOD32:
7751cf9926bSpatrick   case R_ARM_TLS_DTPOFF32:
776ece8a530Spatrick   case R_ARM_TLS_GD32:
777ece8a530Spatrick   case R_ARM_TLS_IE32:
7781cf9926bSpatrick   case R_ARM_TLS_LDM32:
779ece8a530Spatrick   case R_ARM_TLS_LE32:
7801cf9926bSpatrick   case R_ARM_TLS_LDO32:
7811cf9926bSpatrick   case R_ARM_TLS_TPOFF32:
782ece8a530Spatrick     return SignExtend64<32>(read32le(buf));
783ece8a530Spatrick   case R_ARM_PREL31:
784ece8a530Spatrick     return SignExtend64<31>(read32le(buf));
785ece8a530Spatrick   case R_ARM_CALL:
786ece8a530Spatrick   case R_ARM_JUMP24:
787ece8a530Spatrick   case R_ARM_PC24:
788ece8a530Spatrick   case R_ARM_PLT32:
789ece8a530Spatrick     return SignExtend64<26>(read32le(buf) << 2);
790*dfe94b16Srobert   case R_ARM_THM_JUMP8:
791*dfe94b16Srobert     return SignExtend64<9>(read16le(buf) << 1);
792ece8a530Spatrick   case R_ARM_THM_JUMP11:
793ece8a530Spatrick     return SignExtend64<12>(read16le(buf) << 1);
794ece8a530Spatrick   case R_ARM_THM_JUMP19: {
795ece8a530Spatrick     // Encoding T3: A = S:J2:J1:imm10:imm6:0
796ece8a530Spatrick     uint16_t hi = read16le(buf);
797ece8a530Spatrick     uint16_t lo = read16le(buf + 2);
798ece8a530Spatrick     return SignExtend64<20>(((hi & 0x0400) << 10) | // S
799ece8a530Spatrick                             ((lo & 0x0800) << 8) |  // J2
800ece8a530Spatrick                             ((lo & 0x2000) << 5) |  // J1
801ece8a530Spatrick                             ((hi & 0x003f) << 12) | // imm6
802ece8a530Spatrick                             ((lo & 0x07ff) << 1));  // imm11:0
803ece8a530Spatrick   }
804ece8a530Spatrick   case R_ARM_THM_CALL:
805ece8a530Spatrick     if (!config->armJ1J2BranchEncoding) {
806ece8a530Spatrick       // Older Arm architectures do not support R_ARM_THM_JUMP24 and have
807ece8a530Spatrick       // different encoding rules and range due to J1 and J2 always being 1.
808ece8a530Spatrick       uint16_t hi = read16le(buf);
809ece8a530Spatrick       uint16_t lo = read16le(buf + 2);
810ece8a530Spatrick       return SignExtend64<22>(((hi & 0x7ff) << 12) | // imm11
811ece8a530Spatrick                               ((lo & 0x7ff) << 1));  // imm11:0
812ece8a530Spatrick       break;
813ece8a530Spatrick     }
814*dfe94b16Srobert     [[fallthrough]];
815ece8a530Spatrick   case R_ARM_THM_JUMP24: {
816ece8a530Spatrick     // Encoding B T4, BL T1, BLX T2: A = S:I1:I2:imm10:imm11:0
817ece8a530Spatrick     // I1 = NOT(J1 EOR S), I2 = NOT(J2 EOR S)
818ece8a530Spatrick     uint16_t hi = read16le(buf);
819ece8a530Spatrick     uint16_t lo = read16le(buf + 2);
820ece8a530Spatrick     return SignExtend64<24>(((hi & 0x0400) << 14) |                    // S
821ece8a530Spatrick                             (~((lo ^ (hi << 3)) << 10) & 0x00800000) | // I1
822ece8a530Spatrick                             (~((lo ^ (hi << 1)) << 11) & 0x00400000) | // I2
823ece8a530Spatrick                             ((hi & 0x003ff) << 12) |                   // imm0
824ece8a530Spatrick                             ((lo & 0x007ff) << 1)); // imm11:0
825ece8a530Spatrick   }
826ece8a530Spatrick   // ELF for the ARM Architecture 4.6.1.1 the implicit addend for MOVW and
827ece8a530Spatrick   // MOVT is in the range -32768 <= A < 32768
828ece8a530Spatrick   case R_ARM_MOVW_ABS_NC:
829ece8a530Spatrick   case R_ARM_MOVT_ABS:
830ece8a530Spatrick   case R_ARM_MOVW_PREL_NC:
831bb684c34Spatrick   case R_ARM_MOVT_PREL:
832bb684c34Spatrick   case R_ARM_MOVW_BREL_NC:
833bb684c34Spatrick   case R_ARM_MOVT_BREL: {
834ece8a530Spatrick     uint64_t val = read32le(buf) & 0x000f0fff;
835ece8a530Spatrick     return SignExtend64<16>(((val & 0x000f0000) >> 4) | (val & 0x00fff));
836ece8a530Spatrick   }
837ece8a530Spatrick   case R_ARM_THM_MOVW_ABS_NC:
838ece8a530Spatrick   case R_ARM_THM_MOVT_ABS:
839ece8a530Spatrick   case R_ARM_THM_MOVW_PREL_NC:
840bb684c34Spatrick   case R_ARM_THM_MOVT_PREL:
841bb684c34Spatrick   case R_ARM_THM_MOVW_BREL_NC:
842bb684c34Spatrick   case R_ARM_THM_MOVT_BREL: {
843ece8a530Spatrick     // Encoding T3: A = imm4:i:imm3:imm8
844ece8a530Spatrick     uint16_t hi = read16le(buf);
845ece8a530Spatrick     uint16_t lo = read16le(buf + 2);
846ece8a530Spatrick     return SignExtend64<16>(((hi & 0x000f) << 12) | // imm4
847ece8a530Spatrick                             ((hi & 0x0400) << 1) |  // i
848ece8a530Spatrick                             ((lo & 0x7000) >> 4) |  // imm3
849ece8a530Spatrick                             (lo & 0x00ff));         // imm8
850ece8a530Spatrick   }
851*dfe94b16Srobert   case R_ARM_ALU_PC_G0:
852*dfe94b16Srobert   case R_ARM_ALU_PC_G0_NC:
853*dfe94b16Srobert   case R_ARM_ALU_PC_G1:
854*dfe94b16Srobert   case R_ARM_ALU_PC_G1_NC:
855*dfe94b16Srobert   case R_ARM_ALU_PC_G2: {
856bb684c34Spatrick     // 12-bit immediate is a modified immediate made up of a 4-bit even
857bb684c34Spatrick     // right rotation and 8-bit constant. After the rotation the value
858bb684c34Spatrick     // is zero-extended. When bit 23 is set the instruction is an add, when
859bb684c34Spatrick     // bit 22 is set it is a sub.
860bb684c34Spatrick     uint32_t instr = read32le(buf);
861bb684c34Spatrick     uint32_t val = rotr32(instr & 0xff, ((instr & 0xf00) >> 8) * 2);
862bb684c34Spatrick     return (instr & 0x00400000) ? -val : val;
863bb684c34Spatrick   }
864*dfe94b16Srobert   case R_ARM_LDR_PC_G0:
865*dfe94b16Srobert   case R_ARM_LDR_PC_G1:
866*dfe94b16Srobert   case R_ARM_LDR_PC_G2: {
867bb684c34Spatrick     // ADR (literal) add = bit23, sub = bit22
868bb684c34Spatrick     // LDR (literal) u = bit23 unsigned imm12
869bb684c34Spatrick     bool u = read32le(buf) & 0x00800000;
870bb684c34Spatrick     uint32_t imm12 = read32le(buf) & 0xfff;
871bb684c34Spatrick     return u ? imm12 : -imm12;
872bb684c34Spatrick   }
873*dfe94b16Srobert   case R_ARM_LDRS_PC_G0:
874*dfe94b16Srobert   case R_ARM_LDRS_PC_G1:
875*dfe94b16Srobert   case R_ARM_LDRS_PC_G2: {
876*dfe94b16Srobert     // LDRD/LDRH/LDRSB/LDRSH (literal) u = bit23 unsigned imm8
877*dfe94b16Srobert     uint32_t opcode = read32le(buf);
878*dfe94b16Srobert     bool u = opcode & 0x00800000;
879*dfe94b16Srobert     uint32_t imm4l = opcode & 0xf;
880*dfe94b16Srobert     uint32_t imm4h = (opcode & 0xf00) >> 4;
881*dfe94b16Srobert     return u ? (imm4h | imm4l) : -(imm4h | imm4l);
882*dfe94b16Srobert   }
883bb684c34Spatrick   case R_ARM_THM_ALU_PREL_11_0: {
884bb684c34Spatrick     // Thumb2 ADR, which is an alias for a sub or add instruction with an
885bb684c34Spatrick     // unsigned immediate.
886bb684c34Spatrick     // ADR encoding T2 (sub), T3 (add) i:imm3:imm8
887bb684c34Spatrick     uint16_t hi = read16le(buf);
888bb684c34Spatrick     uint16_t lo = read16le(buf + 2);
889bb684c34Spatrick     uint64_t imm = (hi & 0x0400) << 1 | // i
890bb684c34Spatrick                    (lo & 0x7000) >> 4 | // imm3
891bb684c34Spatrick                    (lo & 0x00ff);       // imm8
892bb684c34Spatrick     // For sub, addend is negative, add is positive.
893bb684c34Spatrick     return (hi & 0x00f0) ? -imm : imm;
894bb684c34Spatrick   }
895bb684c34Spatrick   case R_ARM_THM_PC8:
896bb684c34Spatrick     // ADR and LDR (literal) encoding T1
897bb684c34Spatrick     // From ELF for the ARM Architecture the initial signed addend is formed
898bb684c34Spatrick     // from an unsigned field using expression (((imm8:00 + 4) & 0x3ff) – 4)
899bb684c34Spatrick     // this trick permits the PC bias of -4 to be encoded using imm8 = 0xff
900bb684c34Spatrick     return ((((read16le(buf) & 0xff) << 2) + 4) & 0x3ff) - 4;
901bb684c34Spatrick   case R_ARM_THM_PC12: {
902bb684c34Spatrick     // LDR (literal) encoding T2, add = (U == '1') imm12
903bb684c34Spatrick     bool u = read16le(buf) & 0x0080;
904bb684c34Spatrick     uint64_t imm12 = read16le(buf + 2) & 0x0fff;
905bb684c34Spatrick     return u ? imm12 : -imm12;
906bb684c34Spatrick   }
9071cf9926bSpatrick   case R_ARM_NONE:
908*dfe94b16Srobert   case R_ARM_V4BX:
9091cf9926bSpatrick   case R_ARM_JUMP_SLOT:
9101cf9926bSpatrick     // These relocations are defined as not having an implicit addend.
9111cf9926bSpatrick     return 0;
912ece8a530Spatrick   }
913ece8a530Spatrick }
914ece8a530Spatrick 
getARMTargetInfo()915bb684c34Spatrick TargetInfo *elf::getARMTargetInfo() {
916ece8a530Spatrick   static ARM target;
917ece8a530Spatrick   return &target;
918ece8a530Spatrick }
919