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