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