xref: /llvm-project-15.0.7/lld/ELF/Arch/ARM.cpp (revision f97936fa)
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, uint64_t gotPltEntryAddr, uint64_t pltEntryAddr,
38                 int32_t index, unsigned relOff) 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 relocateOne(uint8_t *loc, RelType type, 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   noneRel = R_ARM_NONE;
56   pltRel = R_ARM_JUMP_SLOT;
57   symbolicRel = R_ARM_ABS32;
58   tlsGotRel = R_ARM_TLS_TPOFF32;
59   tlsModuleIndexRel = R_ARM_TLS_DTPMOD32;
60   tlsOffsetRel = R_ARM_TLS_DTPOFF32;
61   gotBaseSymInGotPlt = false;
62   pltEntrySize = 16;
63   pltHeaderSize = 32;
64   trapInstr = {0xd4, 0xd4, 0xd4, 0xd4};
65   needsThunks = true;
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_BASE_PREL:
123     // B(S) + A - P
124     // FIXME: currently B(S) assumed to be .got, this may not hold for all
125     // platforms.
126     return R_GOTONLY_PC;
127   case R_ARM_MOVW_PREL_NC:
128   case R_ARM_MOVT_PREL:
129   case R_ARM_REL32:
130   case R_ARM_THM_MOVW_PREL_NC:
131   case R_ARM_THM_MOVT_PREL:
132     return R_PC;
133   case R_ARM_NONE:
134     return R_NONE;
135   case R_ARM_TLS_LE32:
136     return R_TLS;
137   case R_ARM_V4BX:
138     // V4BX is just a marker to indicate there's a "bx rN" instruction at the
139     // given address. It can be used to implement a special linker mode which
140     // rewrites ARMv4T inputs to ARMv4. Since we support only ARMv4 input and
141     // not ARMv4 output, we can just ignore it.
142     return R_HINT;
143   default:
144     return R_ABS;
145   }
146 }
147 
148 RelType ARM::getDynRel(RelType type) const {
149   if ((type == R_ARM_ABS32) || (type == R_ARM_TARGET1 && !config->target1Rel))
150     return R_ARM_ABS32;
151   return R_ARM_NONE;
152 }
153 
154 void ARM::writeGotPlt(uint8_t *buf, const Symbol &) const {
155   write32le(buf, in.plt->getVA());
156 }
157 
158 void ARM::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
159   // An ARM entry is the address of the ifunc resolver function.
160   write32le(buf, s.getVA());
161 }
162 
163 // Long form PLT Header that does not have any restrictions on the displacement
164 // of the .plt from the .plt.got.
165 static void writePltHeaderLong(uint8_t *buf) {
166   const uint8_t pltData[] = {
167       0x04, 0xe0, 0x2d, 0xe5, //     str lr, [sp,#-4]!
168       0x04, 0xe0, 0x9f, 0xe5, //     ldr lr, L2
169       0x0e, 0xe0, 0x8f, 0xe0, // L1: add lr, pc, lr
170       0x08, 0xf0, 0xbe, 0xe5, //     ldr pc, [lr, #8]
171       0x00, 0x00, 0x00, 0x00, // L2: .word   &(.got.plt) - L1 - 8
172       0xd4, 0xd4, 0xd4, 0xd4, //     Pad to 32-byte boundary
173       0xd4, 0xd4, 0xd4, 0xd4, //     Pad to 32-byte boundary
174       0xd4, 0xd4, 0xd4, 0xd4};
175   memcpy(buf, pltData, sizeof(pltData));
176   uint64_t gotPlt = in.gotPlt->getVA();
177   uint64_t l1 = in.plt->getVA() + 8;
178   write32le(buf + 16, gotPlt - l1 - 8);
179 }
180 
181 // The default PLT header requires the .plt.got to be within 128 Mb of the
182 // .plt in the positive direction.
183 void ARM::writePltHeader(uint8_t *buf) const {
184   // Use a similar sequence to that in writePlt(), the difference is the calling
185   // conventions mean we use lr instead of ip. The PLT entry is responsible for
186   // saving lr on the stack, the dynamic loader is responsible for reloading
187   // it.
188   const uint32_t pltData[] = {
189       0xe52de004, // L1: str lr, [sp,#-4]!
190       0xe28fe600, //     add lr, pc,  #0x0NN00000 &(.got.plt - L1 - 4)
191       0xe28eea00, //     add lr, lr,  #0x000NN000 &(.got.plt - L1 - 4)
192       0xe5bef000, //     ldr pc, [lr, #0x00000NNN] &(.got.plt -L1 - 4)
193   };
194 
195   uint64_t offset = in.gotPlt->getVA() - in.plt->getVA() - 4;
196   if (!llvm::isUInt<27>(offset)) {
197     // We cannot encode the Offset, use the long form.
198     writePltHeaderLong(buf);
199     return;
200   }
201   write32le(buf + 0, pltData[0]);
202   write32le(buf + 4, pltData[1] | ((offset >> 20) & 0xff));
203   write32le(buf + 8, pltData[2] | ((offset >> 12) & 0xff));
204   write32le(buf + 12, pltData[3] | (offset & 0xfff));
205   memcpy(buf + 16, trapInstr.data(), 4); // Pad to 32-byte boundary
206   memcpy(buf + 20, trapInstr.data(), 4);
207   memcpy(buf + 24, trapInstr.data(), 4);
208   memcpy(buf + 28, trapInstr.data(), 4);
209 }
210 
211 void ARM::addPltHeaderSymbols(InputSection &isec) const {
212   addSyntheticLocal("$a", STT_NOTYPE, 0, 0, isec);
213   addSyntheticLocal("$d", STT_NOTYPE, 16, 0, isec);
214 }
215 
216 // Long form PLT entries that do not have any restrictions on the displacement
217 // of the .plt from the .plt.got.
218 static void writePltLong(uint8_t *buf, uint64_t gotPltEntryAddr,
219                          uint64_t pltEntryAddr, int32_t index,
220                          unsigned relOff) {
221   const uint8_t pltData[] = {
222       0x04, 0xc0, 0x9f, 0xe5, //     ldr ip, L2
223       0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
224       0x00, 0xf0, 0x9c, 0xe5, //     ldr pc, [ip]
225       0x00, 0x00, 0x00, 0x00, // L2: .word   Offset(&(.plt.got) - L1 - 8
226   };
227   memcpy(buf, pltData, sizeof(pltData));
228   uint64_t l1 = pltEntryAddr + 4;
229   write32le(buf + 12, gotPltEntryAddr - l1 - 8);
230 }
231 
232 // The default PLT entries require the .plt.got to be within 128 Mb of the
233 // .plt in the positive direction.
234 void ARM::writePlt(uint8_t *buf, uint64_t gotPltEntryAddr,
235                    uint64_t pltEntryAddr, int32_t index,
236                    unsigned relOff) const {
237   // The PLT entry is similar to the example given in Appendix A of ELF for
238   // the Arm Architecture. Instead of using the Group Relocations to find the
239   // optimal rotation for the 8-bit immediate used in the add instructions we
240   // hard code the most compact rotations for simplicity. This saves a load
241   // instruction over the long plt sequences.
242   const uint32_t pltData[] = {
243       0xe28fc600, // L1: add ip, pc,  #0x0NN00000  Offset(&(.plt.got) - L1 - 8
244       0xe28cca00, //     add ip, ip,  #0x000NN000  Offset(&(.plt.got) - L1 - 8
245       0xe5bcf000, //     ldr pc, [ip, #0x00000NNN] Offset(&(.plt.got) - L1 - 8
246   };
247 
248   uint64_t offset = gotPltEntryAddr - pltEntryAddr - 8;
249   if (!llvm::isUInt<27>(offset)) {
250     // We cannot encode the Offset, use the long form.
251     writePltLong(buf, gotPltEntryAddr, pltEntryAddr, index, relOff);
252     return;
253   }
254   write32le(buf + 0, pltData[0] | ((offset >> 20) & 0xff));
255   write32le(buf + 4, pltData[1] | ((offset >> 12) & 0xff));
256   write32le(buf + 8, pltData[2] | (offset & 0xfff));
257   memcpy(buf + 12, trapInstr.data(), 4); // Pad to 16-byte boundary
258 }
259 
260 void ARM::addPltSymbols(InputSection &isec, uint64_t off) const {
261   addSyntheticLocal("$a", STT_NOTYPE, off, 0, isec);
262   addSyntheticLocal("$d", STT_NOTYPE, off + 12, 0, isec);
263 }
264 
265 bool ARM::needsThunk(RelExpr expr, RelType type, const InputFile *file,
266                      uint64_t branchAddr, const Symbol &s, int64_t /*a*/) const {
267   // If S is an undefined weak symbol and does not have a PLT entry then it
268   // will be resolved as a branch to the next instruction.
269   if (s.isUndefWeak() && !s.isInPlt())
270     return false;
271   // A state change from ARM to Thumb and vice versa must go through an
272   // interworking thunk if the relocation type is not R_ARM_CALL or
273   // R_ARM_THM_CALL.
274   switch (type) {
275   case R_ARM_PC24:
276   case R_ARM_PLT32:
277   case R_ARM_JUMP24:
278     // Source is ARM, all PLT entries are ARM so no interworking required.
279     // Otherwise we need to interwork if Symbol has bit 0 set (Thumb).
280     if (expr == R_PC && ((s.getVA() & 1) == 1))
281       return true;
282     LLVM_FALLTHROUGH;
283   case R_ARM_CALL: {
284     uint64_t dst = (expr == R_PLT_PC) ? s.getPltVA() : s.getVA();
285     return !inBranchRange(type, branchAddr, dst);
286   }
287   case R_ARM_THM_JUMP19:
288   case R_ARM_THM_JUMP24:
289     // Source is Thumb, all PLT entries are ARM so interworking is required.
290     // Otherwise we need to interwork if Symbol has bit 0 clear (ARM).
291     if (expr == R_PLT_PC || ((s.getVA() & 1) == 0))
292       return true;
293     LLVM_FALLTHROUGH;
294   case R_ARM_THM_CALL: {
295     uint64_t dst = (expr == R_PLT_PC) ? s.getPltVA() : s.getVA();
296     return !inBranchRange(type, branchAddr, dst);
297   }
298   }
299   return false;
300 }
301 
302 uint32_t ARM::getThunkSectionSpacing() const {
303   // The placing of pre-created ThunkSections is controlled by the value
304   // thunkSectionSpacing returned by getThunkSectionSpacing(). The aim is to
305   // place the ThunkSection such that all branches from the InputSections
306   // prior to the ThunkSection can reach a Thunk placed at the end of the
307   // ThunkSection. Graphically:
308   // | up to thunkSectionSpacing .text input sections |
309   // | ThunkSection                                   |
310   // | up to thunkSectionSpacing .text input sections |
311   // | ThunkSection                                   |
312 
313   // Pre-created ThunkSections are spaced roughly 16MiB apart on ARMv7. This
314   // is to match the most common expected case of a Thumb 2 encoded BL, BLX or
315   // B.W:
316   // ARM B, BL, BLX range +/- 32MiB
317   // Thumb B.W, BL, BLX range +/- 16MiB
318   // Thumb B<cc>.W range +/- 1MiB
319   // If a branch cannot reach a pre-created ThunkSection a new one will be
320   // created so we can handle the rare cases of a Thumb 2 conditional branch.
321   // We intentionally use a lower size for thunkSectionSpacing than the maximum
322   // branch range so the end of the ThunkSection is more likely to be within
323   // range of the branch instruction that is furthest away. The value we shorten
324   // thunkSectionSpacing by is set conservatively to allow us to create 16,384
325   // 12 byte Thunks at any offset in a ThunkSection without risk of a branch to
326   // one of the Thunks going out of range.
327 
328   // On Arm the thunkSectionSpacing depends on the range of the Thumb Branch
329   // range. On earlier Architectures such as ARMv4, ARMv5 and ARMv6 (except
330   // ARMv6T2) the range is +/- 4MiB.
331 
332   return (config->armJ1J2BranchEncoding) ? 0x1000000 - 0x30000
333                                          : 0x400000 - 0x7500;
334 }
335 
336 bool ARM::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
337   uint64_t range;
338   uint64_t instrSize;
339 
340   switch (type) {
341   case R_ARM_PC24:
342   case R_ARM_PLT32:
343   case R_ARM_JUMP24:
344   case R_ARM_CALL:
345     range = 0x2000000;
346     instrSize = 4;
347     break;
348   case R_ARM_THM_JUMP19:
349     range = 0x100000;
350     instrSize = 2;
351     break;
352   case R_ARM_THM_JUMP24:
353   case R_ARM_THM_CALL:
354     range = config->armJ1J2BranchEncoding ? 0x1000000 : 0x400000;
355     instrSize = 2;
356     break;
357   default:
358     return true;
359   }
360   // PC at Src is 2 instructions ahead, immediate of branch is signed
361   if (src > dst)
362     range -= 2 * instrSize;
363   else
364     range += instrSize;
365 
366   if ((dst & 0x1) == 0)
367     // Destination is ARM, if ARM caller then Src is already 4-byte aligned.
368     // If Thumb Caller (BLX) the Src address has bottom 2 bits cleared to ensure
369     // destination will be 4 byte aligned.
370     src &= ~0x3;
371   else
372     // Bit 0 == 1 denotes Thumb state, it is not part of the range
373     dst &= ~0x1;
374 
375   uint64_t distance = (src > dst) ? src - dst : dst - src;
376   return distance <= range;
377 }
378 
379 void ARM::relocateOne(uint8_t *loc, RelType type, uint64_t val) const {
380   switch (type) {
381   case R_ARM_ABS32:
382   case R_ARM_BASE_PREL:
383   case R_ARM_GOTOFF32:
384   case R_ARM_GOT_BREL:
385   case R_ARM_GOT_PREL:
386   case R_ARM_REL32:
387   case R_ARM_RELATIVE:
388   case R_ARM_SBREL32:
389   case R_ARM_TARGET1:
390   case R_ARM_TARGET2:
391   case R_ARM_TLS_GD32:
392   case R_ARM_TLS_IE32:
393   case R_ARM_TLS_LDM32:
394   case R_ARM_TLS_LDO32:
395   case R_ARM_TLS_LE32:
396   case R_ARM_TLS_TPOFF32:
397   case R_ARM_TLS_DTPOFF32:
398     write32le(loc, val);
399     break;
400   case R_ARM_PREL31:
401     checkInt(loc, val, 31, type);
402     write32le(loc, (read32le(loc) & 0x80000000) | (val & ~0x80000000));
403     break;
404   case R_ARM_CALL:
405     // R_ARM_CALL is used for BL and BLX instructions, depending on the
406     // value of bit 0 of Val, we must select a BL or BLX instruction
407     if (val & 1) {
408       // If bit 0 of Val is 1 the target is Thumb, we must select a BLX.
409       // The BLX encoding is 0xfa:H:imm24 where Val = imm24:H:'1'
410       checkInt(loc, val, 26, type);
411       write32le(loc, 0xfa000000 |                    // opcode
412                          ((val & 2) << 23) |         // H
413                          ((val >> 2) & 0x00ffffff)); // imm24
414       break;
415     }
416     if ((read32le(loc) & 0xfe000000) == 0xfa000000)
417       // BLX (always unconditional) instruction to an ARM Target, select an
418       // unconditional BL.
419       write32le(loc, 0xeb000000 | (read32le(loc) & 0x00ffffff));
420     // fall through as BL encoding is shared with B
421     LLVM_FALLTHROUGH;
422   case R_ARM_JUMP24:
423   case R_ARM_PC24:
424   case R_ARM_PLT32:
425     checkInt(loc, val, 26, type);
426     write32le(loc, (read32le(loc) & ~0x00ffffff) | ((val >> 2) & 0x00ffffff));
427     break;
428   case R_ARM_THM_JUMP11:
429     checkInt(loc, val, 12, type);
430     write16le(loc, (read32le(loc) & 0xf800) | ((val >> 1) & 0x07ff));
431     break;
432   case R_ARM_THM_JUMP19:
433     // Encoding T3: Val = S:J2:J1:imm6:imm11:0
434     checkInt(loc, val, 21, type);
435     write16le(loc,
436               (read16le(loc) & 0xfbc0) |   // opcode cond
437                   ((val >> 10) & 0x0400) | // S
438                   ((val >> 12) & 0x003f)); // imm6
439     write16le(loc + 2,
440               0x8000 |                    // opcode
441                   ((val >> 8) & 0x0800) | // J2
442                   ((val >> 5) & 0x2000) | // J1
443                   ((val >> 1) & 0x07ff)); // imm11
444     break;
445   case R_ARM_THM_CALL:
446     // R_ARM_THM_CALL is used for BL and BLX instructions, depending on the
447     // value of bit 0 of Val, we must select a BL or BLX instruction
448     if ((val & 1) == 0) {
449       // Ensure BLX destination is 4-byte aligned. As BLX instruction may
450       // only be two byte aligned. This must be done before overflow check
451       val = alignTo(val, 4);
452     }
453     // Bit 12 is 0 for BLX, 1 for BL
454     write16le(loc + 2, (read16le(loc + 2) & ~0x1000) | (val & 1) << 12);
455     if (!config->armJ1J2BranchEncoding) {
456       // Older Arm architectures do not support R_ARM_THM_JUMP24 and have
457       // different encoding rules and range due to J1 and J2 always being 1.
458       checkInt(loc, val, 23, type);
459       write16le(loc,
460                 0xf000 |                     // opcode
461                     ((val >> 12) & 0x07ff)); // imm11
462       write16le(loc + 2,
463                 (read16le(loc + 2) & 0xd000) | // opcode
464                     0x2800 |                   // J1 == J2 == 1
465                     ((val >> 1) & 0x07ff));    // imm11
466       break;
467     }
468     // Fall through as rest of encoding is the same as B.W
469     LLVM_FALLTHROUGH;
470   case R_ARM_THM_JUMP24:
471     // Encoding B  T4, BL T1, BLX T2: Val = S:I1:I2:imm10:imm11:0
472     checkInt(loc, val, 25, type);
473     write16le(loc,
474               0xf000 |                     // opcode
475                   ((val >> 14) & 0x0400) | // S
476                   ((val >> 12) & 0x03ff)); // imm10
477     write16le(loc + 2,
478               (read16le(loc + 2) & 0xd000) |                  // opcode
479                   (((~(val >> 10)) ^ (val >> 11)) & 0x2000) | // J1
480                   (((~(val >> 11)) ^ (val >> 13)) & 0x0800) | // J2
481                   ((val >> 1) & 0x07ff));                     // imm11
482     break;
483   case R_ARM_MOVW_ABS_NC:
484   case R_ARM_MOVW_PREL_NC:
485     write32le(loc, (read32le(loc) & ~0x000f0fff) | ((val & 0xf000) << 4) |
486                        (val & 0x0fff));
487     break;
488   case R_ARM_MOVT_ABS:
489   case R_ARM_MOVT_PREL:
490     write32le(loc, (read32le(loc) & ~0x000f0fff) |
491                        (((val >> 16) & 0xf000) << 4) | ((val >> 16) & 0xfff));
492     break;
493   case R_ARM_THM_MOVT_ABS:
494   case R_ARM_THM_MOVT_PREL:
495     // Encoding T1: A = imm4:i:imm3:imm8
496     write16le(loc,
497               0xf2c0 |                     // opcode
498                   ((val >> 17) & 0x0400) | // i
499                   ((val >> 28) & 0x000f)); // imm4
500     write16le(loc + 2,
501               (read16le(loc + 2) & 0x8f00) | // opcode
502                   ((val >> 12) & 0x7000) |   // imm3
503                   ((val >> 16) & 0x00ff));   // imm8
504     break;
505   case R_ARM_THM_MOVW_ABS_NC:
506   case R_ARM_THM_MOVW_PREL_NC:
507     // Encoding T3: A = imm4:i:imm3:imm8
508     write16le(loc,
509               0xf240 |                     // opcode
510                   ((val >> 1) & 0x0400) |  // i
511                   ((val >> 12) & 0x000f)); // imm4
512     write16le(loc + 2,
513               (read16le(loc + 2) & 0x8f00) | // opcode
514                   ((val << 4) & 0x7000) |    // imm3
515                   (val & 0x00ff));           // imm8
516     break;
517   default:
518     error(getErrorLocation(loc) + "unrecognized relocation " + toString(type));
519   }
520 }
521 
522 int64_t ARM::getImplicitAddend(const uint8_t *buf, RelType type) const {
523   switch (type) {
524   default:
525     return 0;
526   case R_ARM_ABS32:
527   case R_ARM_BASE_PREL:
528   case R_ARM_GOTOFF32:
529   case R_ARM_GOT_BREL:
530   case R_ARM_GOT_PREL:
531   case R_ARM_REL32:
532   case R_ARM_TARGET1:
533   case R_ARM_TARGET2:
534   case R_ARM_TLS_GD32:
535   case R_ARM_TLS_LDM32:
536   case R_ARM_TLS_LDO32:
537   case R_ARM_TLS_IE32:
538   case R_ARM_TLS_LE32:
539     return SignExtend64<32>(read32le(buf));
540   case R_ARM_PREL31:
541     return SignExtend64<31>(read32le(buf));
542   case R_ARM_CALL:
543   case R_ARM_JUMP24:
544   case R_ARM_PC24:
545   case R_ARM_PLT32:
546     return SignExtend64<26>(read32le(buf) << 2);
547   case R_ARM_THM_JUMP11:
548     return SignExtend64<12>(read16le(buf) << 1);
549   case R_ARM_THM_JUMP19: {
550     // Encoding T3: A = S:J2:J1:imm10:imm6:0
551     uint16_t hi = read16le(buf);
552     uint16_t lo = read16le(buf + 2);
553     return SignExtend64<20>(((hi & 0x0400) << 10) | // S
554                             ((lo & 0x0800) << 8) |  // J2
555                             ((lo & 0x2000) << 5) |  // J1
556                             ((hi & 0x003f) << 12) | // imm6
557                             ((lo & 0x07ff) << 1));  // imm11:0
558   }
559   case R_ARM_THM_CALL:
560     if (!config->armJ1J2BranchEncoding) {
561       // Older Arm architectures do not support R_ARM_THM_JUMP24 and have
562       // different encoding rules and range due to J1 and J2 always being 1.
563       uint16_t hi = read16le(buf);
564       uint16_t lo = read16le(buf + 2);
565       return SignExtend64<22>(((hi & 0x7ff) << 12) | // imm11
566                               ((lo & 0x7ff) << 1));  // imm11:0
567       break;
568     }
569     LLVM_FALLTHROUGH;
570   case R_ARM_THM_JUMP24: {
571     // Encoding B T4, BL T1, BLX T2: A = S:I1:I2:imm10:imm11:0
572     // I1 = NOT(J1 EOR S), I2 = NOT(J2 EOR S)
573     uint16_t hi = read16le(buf);
574     uint16_t lo = read16le(buf + 2);
575     return SignExtend64<24>(((hi & 0x0400) << 14) |                    // S
576                             (~((lo ^ (hi << 3)) << 10) & 0x00800000) | // I1
577                             (~((lo ^ (hi << 1)) << 11) & 0x00400000) | // I2
578                             ((hi & 0x003ff) << 12) |                   // imm0
579                             ((lo & 0x007ff) << 1)); // imm11:0
580   }
581   // ELF for the ARM Architecture 4.6.1.1 the implicit addend for MOVW and
582   // MOVT is in the range -32768 <= A < 32768
583   case R_ARM_MOVW_ABS_NC:
584   case R_ARM_MOVT_ABS:
585   case R_ARM_MOVW_PREL_NC:
586   case R_ARM_MOVT_PREL: {
587     uint64_t val = read32le(buf) & 0x000f0fff;
588     return SignExtend64<16>(((val & 0x000f0000) >> 4) | (val & 0x00fff));
589   }
590   case R_ARM_THM_MOVW_ABS_NC:
591   case R_ARM_THM_MOVT_ABS:
592   case R_ARM_THM_MOVW_PREL_NC:
593   case R_ARM_THM_MOVT_PREL: {
594     // Encoding T3: A = imm4:i:imm3:imm8
595     uint16_t hi = read16le(buf);
596     uint16_t lo = read16le(buf + 2);
597     return SignExtend64<16>(((hi & 0x000f) << 12) | // imm4
598                             ((hi & 0x0400) << 1) |  // i
599                             ((lo & 0x7000) >> 4) |  // imm3
600                             (lo & 0x00ff));         // imm8
601   }
602   }
603 }
604 
605 TargetInfo *getARMTargetInfo() {
606   static ARM target;
607   return &target;
608 }
609 
610 } // namespace elf
611 } // namespace lld
612