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