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 
ARM()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 = {0xd4, 0xd4, 0xd4, 0xd4};
65   NeedsThunks = true;
66 }
67 
calcEFlags() const68 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 
getRelExpr(RelType Type,const Symbol & S,const uint8_t * Loc) const84 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 
getDynRel(RelType Type) const148 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 
writeGotPlt(uint8_t * Buf,const Symbol &) const154 void ARM::writeGotPlt(uint8_t *Buf, const Symbol &) const {
155   write32le(Buf, In.Plt->getVA());
156 }
157 
writeIgotPlt(uint8_t * Buf,const Symbol & S) const158 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.
writePltHeaderLong(uint8_t * Buf)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.
writePltHeader(uint8_t * Buf) const183 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 
addPltHeaderSymbols(InputSection & IS) const211 void ARM::addPltHeaderSymbols(InputSection &IS) const {
212   addSyntheticLocal("$a", STT_NOTYPE, 0, 0, IS);
213   addSyntheticLocal("$d", STT_NOTYPE, 16, 0, IS);
214 }
215 
216 // Long form PLT entries that do not have any restrictions on the displacement
217 // of the .plt from the .plt.got.
writePltLong(uint8_t * Buf,uint64_t GotPltEntryAddr,uint64_t PltEntryAddr,int32_t Index,unsigned RelOff)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.
writePlt(uint8_t * Buf,uint64_t GotPltEntryAddr,uint64_t PltEntryAddr,int32_t Index,unsigned RelOff) const234 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 
addPltSymbols(InputSection & IS,uint64_t Off) const260 void ARM::addPltSymbols(InputSection &IS, uint64_t Off) const {
261   addSyntheticLocal("$a", STT_NOTYPE, Off, 0, IS);
262   addSyntheticLocal("$d", STT_NOTYPE, Off + 12, 0, IS);
263 }
264 
needsThunk(RelExpr Expr,RelType Type,const InputFile * File,uint64_t BranchAddr,const Symbol & S) const265 bool ARM::needsThunk(RelExpr Expr, RelType Type, const InputFile *File,
266                      uint64_t BranchAddr, const Symbol &S) 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 
getThunkSectionSpacing() const302 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 
inBranchRange(RelType Type,uint64_t Src,uint64_t Dst) const336 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 
relocateOne(uint8_t * Loc,RelType Type,uint64_t Val) const379 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_GLOB_DAT:
384   case R_ARM_GOTOFF32:
385   case R_ARM_GOT_BREL:
386   case R_ARM_GOT_PREL:
387   case R_ARM_REL32:
388   case R_ARM_RELATIVE:
389   case R_ARM_SBREL32:
390   case R_ARM_TARGET1:
391   case R_ARM_TARGET2:
392   case R_ARM_TLS_GD32:
393   case R_ARM_TLS_IE32:
394   case R_ARM_TLS_LDM32:
395   case R_ARM_TLS_LDO32:
396   case R_ARM_TLS_LE32:
397   case R_ARM_TLS_TPOFF32:
398   case R_ARM_TLS_DTPOFF32:
399     write32le(Loc, Val);
400     break;
401   case R_ARM_TLS_DTPMOD32:
402     write32le(Loc, 1);
403     break;
404   case R_ARM_PREL31:
405     checkInt(Loc, Val, 31, Type);
406     write32le(Loc, (read32le(Loc) & 0x80000000) | (Val & ~0x80000000));
407     break;
408   case R_ARM_CALL:
409     // R_ARM_CALL is used for BL and BLX instructions, depending on the
410     // value of bit 0 of Val, we must select a BL or BLX instruction
411     if (Val & 1) {
412       // If bit 0 of Val is 1 the target is Thumb, we must select a BLX.
413       // The BLX encoding is 0xfa:H:imm24 where Val = imm24:H:'1'
414       checkInt(Loc, Val, 26, Type);
415       write32le(Loc, 0xfa000000 |                    // opcode
416                          ((Val & 2) << 23) |         // H
417                          ((Val >> 2) & 0x00ffffff)); // imm24
418       break;
419     }
420     if ((read32le(Loc) & 0xfe000000) == 0xfa000000)
421       // BLX (always unconditional) instruction to an ARM Target, select an
422       // unconditional BL.
423       write32le(Loc, 0xeb000000 | (read32le(Loc) & 0x00ffffff));
424     // fall through as BL encoding is shared with B
425     LLVM_FALLTHROUGH;
426   case R_ARM_JUMP24:
427   case R_ARM_PC24:
428   case R_ARM_PLT32:
429     checkInt(Loc, Val, 26, Type);
430     write32le(Loc, (read32le(Loc) & ~0x00ffffff) | ((Val >> 2) & 0x00ffffff));
431     break;
432   case R_ARM_THM_JUMP11:
433     checkInt(Loc, Val, 12, Type);
434     write16le(Loc, (read32le(Loc) & 0xf800) | ((Val >> 1) & 0x07ff));
435     break;
436   case R_ARM_THM_JUMP19:
437     // Encoding T3: Val = S:J2:J1:imm6:imm11:0
438     checkInt(Loc, Val, 21, Type);
439     write16le(Loc,
440               (read16le(Loc) & 0xfbc0) |   // opcode cond
441                   ((Val >> 10) & 0x0400) | // S
442                   ((Val >> 12) & 0x003f)); // imm6
443     write16le(Loc + 2,
444               0x8000 |                    // opcode
445                   ((Val >> 8) & 0x0800) | // J2
446                   ((Val >> 5) & 0x2000) | // J1
447                   ((Val >> 1) & 0x07ff)); // imm11
448     break;
449   case R_ARM_THM_CALL:
450     // R_ARM_THM_CALL is used for BL and BLX instructions, depending on the
451     // value of bit 0 of Val, we must select a BL or BLX instruction
452     if ((Val & 1) == 0) {
453       // Ensure BLX destination is 4-byte aligned. As BLX instruction may
454       // only be two byte aligned. This must be done before overflow check
455       Val = alignTo(Val, 4);
456     }
457     // Bit 12 is 0 for BLX, 1 for BL
458     write16le(Loc + 2, (read16le(Loc + 2) & ~0x1000) | (Val & 1) << 12);
459     if (!Config->ARMJ1J2BranchEncoding) {
460       // Older Arm architectures do not support R_ARM_THM_JUMP24 and have
461       // different encoding rules and range due to J1 and J2 always being 1.
462       checkInt(Loc, Val, 23, Type);
463       write16le(Loc,
464                 0xf000 |                     // opcode
465                     ((Val >> 12) & 0x07ff)); // imm11
466       write16le(Loc + 2,
467                 (read16le(Loc + 2) & 0xd000) | // opcode
468                     0x2800 |                   // J1 == J2 == 1
469                     ((Val >> 1) & 0x07ff));    // imm11
470       break;
471     }
472     // Fall through as rest of encoding is the same as B.W
473     LLVM_FALLTHROUGH;
474   case R_ARM_THM_JUMP24:
475     // Encoding B  T4, BL T1, BLX T2: Val = S:I1:I2:imm10:imm11:0
476     checkInt(Loc, Val, 25, Type);
477     write16le(Loc,
478               0xf000 |                     // opcode
479                   ((Val >> 14) & 0x0400) | // S
480                   ((Val >> 12) & 0x03ff)); // imm10
481     write16le(Loc + 2,
482               (read16le(Loc + 2) & 0xd000) |                  // opcode
483                   (((~(Val >> 10)) ^ (Val >> 11)) & 0x2000) | // J1
484                   (((~(Val >> 11)) ^ (Val >> 13)) & 0x0800) | // J2
485                   ((Val >> 1) & 0x07ff));                     // imm11
486     break;
487   case R_ARM_MOVW_ABS_NC:
488   case R_ARM_MOVW_PREL_NC:
489     write32le(Loc, (read32le(Loc) & ~0x000f0fff) | ((Val & 0xf000) << 4) |
490                        (Val & 0x0fff));
491     break;
492   case R_ARM_MOVT_ABS:
493   case R_ARM_MOVT_PREL:
494     write32le(Loc, (read32le(Loc) & ~0x000f0fff) |
495                        (((Val >> 16) & 0xf000) << 4) | ((Val >> 16) & 0xfff));
496     break;
497   case R_ARM_THM_MOVT_ABS:
498   case R_ARM_THM_MOVT_PREL:
499     // Encoding T1: A = imm4:i:imm3:imm8
500     write16le(Loc,
501               0xf2c0 |                     // opcode
502                   ((Val >> 17) & 0x0400) | // i
503                   ((Val >> 28) & 0x000f)); // imm4
504     write16le(Loc + 2,
505               (read16le(Loc + 2) & 0x8f00) | // opcode
506                   ((Val >> 12) & 0x7000) |   // imm3
507                   ((Val >> 16) & 0x00ff));   // imm8
508     break;
509   case R_ARM_THM_MOVW_ABS_NC:
510   case R_ARM_THM_MOVW_PREL_NC:
511     // Encoding T3: A = imm4:i:imm3:imm8
512     write16le(Loc,
513               0xf240 |                     // opcode
514                   ((Val >> 1) & 0x0400) |  // i
515                   ((Val >> 12) & 0x000f)); // imm4
516     write16le(Loc + 2,
517               (read16le(Loc + 2) & 0x8f00) | // opcode
518                   ((Val << 4) & 0x7000) |    // imm3
519                   (Val & 0x00ff));           // imm8
520     break;
521   default:
522     error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
523   }
524 }
525 
getImplicitAddend(const uint8_t * Buf,RelType Type) const526 int64_t ARM::getImplicitAddend(const uint8_t *Buf, RelType Type) const {
527   switch (Type) {
528   default:
529     return 0;
530   case R_ARM_ABS32:
531   case R_ARM_BASE_PREL:
532   case R_ARM_GOTOFF32:
533   case R_ARM_GOT_BREL:
534   case R_ARM_GOT_PREL:
535   case R_ARM_REL32:
536   case R_ARM_TARGET1:
537   case R_ARM_TARGET2:
538   case R_ARM_TLS_GD32:
539   case R_ARM_TLS_LDM32:
540   case R_ARM_TLS_LDO32:
541   case R_ARM_TLS_IE32:
542   case R_ARM_TLS_LE32:
543     return SignExtend64<32>(read32le(Buf));
544   case R_ARM_PREL31:
545     return SignExtend64<31>(read32le(Buf));
546   case R_ARM_CALL:
547   case R_ARM_JUMP24:
548   case R_ARM_PC24:
549   case R_ARM_PLT32:
550     return SignExtend64<26>(read32le(Buf) << 2);
551   case R_ARM_THM_JUMP11:
552     return SignExtend64<12>(read16le(Buf) << 1);
553   case R_ARM_THM_JUMP19: {
554     // Encoding T3: A = S:J2:J1:imm10:imm6:0
555     uint16_t Hi = read16le(Buf);
556     uint16_t Lo = read16le(Buf + 2);
557     return SignExtend64<20>(((Hi & 0x0400) << 10) | // S
558                             ((Lo & 0x0800) << 8) |  // J2
559                             ((Lo & 0x2000) << 5) |  // J1
560                             ((Hi & 0x003f) << 12) | // imm6
561                             ((Lo & 0x07ff) << 1));  // imm11:0
562   }
563   case R_ARM_THM_CALL:
564     if (!Config->ARMJ1J2BranchEncoding) {
565       // Older Arm architectures do not support R_ARM_THM_JUMP24 and have
566       // different encoding rules and range due to J1 and J2 always being 1.
567       uint16_t Hi = read16le(Buf);
568       uint16_t Lo = read16le(Buf + 2);
569       return SignExtend64<22>(((Hi & 0x7ff) << 12) | // imm11
570                               ((Lo & 0x7ff) << 1));  // imm11:0
571       break;
572     }
573     LLVM_FALLTHROUGH;
574   case R_ARM_THM_JUMP24: {
575     // Encoding B T4, BL T1, BLX T2: A = S:I1:I2:imm10:imm11:0
576     // I1 = NOT(J1 EOR S), I2 = NOT(J2 EOR S)
577     uint16_t Hi = read16le(Buf);
578     uint16_t Lo = read16le(Buf + 2);
579     return SignExtend64<24>(((Hi & 0x0400) << 14) |                    // S
580                             (~((Lo ^ (Hi << 3)) << 10) & 0x00800000) | // I1
581                             (~((Lo ^ (Hi << 1)) << 11) & 0x00400000) | // I2
582                             ((Hi & 0x003ff) << 12) |                   // imm0
583                             ((Lo & 0x007ff) << 1)); // imm11:0
584   }
585   // ELF for the ARM Architecture 4.6.1.1 the implicit addend for MOVW and
586   // MOVT is in the range -32768 <= A < 32768
587   case R_ARM_MOVW_ABS_NC:
588   case R_ARM_MOVT_ABS:
589   case R_ARM_MOVW_PREL_NC:
590   case R_ARM_MOVT_PREL: {
591     uint64_t Val = read32le(Buf) & 0x000f0fff;
592     return SignExtend64<16>(((Val & 0x000f0000) >> 4) | (Val & 0x00fff));
593   }
594   case R_ARM_THM_MOVW_ABS_NC:
595   case R_ARM_THM_MOVT_ABS:
596   case R_ARM_THM_MOVW_PREL_NC:
597   case R_ARM_THM_MOVT_PREL: {
598     // Encoding T3: A = imm4:i:imm3:imm8
599     uint16_t Hi = read16le(Buf);
600     uint16_t Lo = read16le(Buf + 2);
601     return SignExtend64<16>(((Hi & 0x000f) << 12) | // imm4
602                             ((Hi & 0x0400) << 1) |  // i
603                             ((Lo & 0x7000) >> 4) |  // imm3
604                             (Lo & 0x00ff));         // imm8
605   }
606   }
607 }
608 
getARMTargetInfo()609 TargetInfo *elf::getARMTargetInfo() {
610   static ARM Target;
611   return &Target;
612 }
613