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