@@ -231,36 +231,67 @@ static void writePltHeaderLong(uint8_t *buf) {
231
231
// The default PLT header requires the .got.plt to be within 128 Mb of the
232
232
// .plt in the positive direction.
233
233
void ARM::writePltHeader (uint8_t *buf) const {
234
- // Use a similar sequence to that in writePlt(), the difference is the calling
235
- // conventions mean we use lr instead of ip. The PLT entry is responsible for
236
- // saving lr on the stack, the dynamic loader is responsible for reloading
237
- // it.
238
- const uint32_t pltData[] = {
239
- 0xe52de004 , // L1: str lr, [sp,#-4]!
240
- 0xe28fe600 , // add lr, pc, #0x0NN00000 &(.got.plt - L1 - 4)
241
- 0xe28eea00 , // add lr, lr, #0x000NN000 &(.got.plt - L1 - 4)
242
- 0xe5bef000 , // ldr pc, [lr, #0x00000NNN] &(.got.plt -L1 - 4)
243
- };
244
-
245
- uint64_t offset = in.gotPlt ->getVA () - in.plt ->getVA () - 4 ;
246
- if (!llvm::isUInt<27 >(offset)) {
247
- // We cannot encode the Offset, use the long form.
248
- writePltHeaderLong (buf);
249
- return ;
234
+ if (!config->armThumbPLTs ) {
235
+ // Use a similar sequence to that in writePlt(), the difference is the calling
236
+ // conventions mean we use lr instead of ip. The PLT entry is responsible for
237
+ // saving lr on the stack, the dynamic loader is responsible for reloading
238
+ // it.
239
+ const uint32_t pltData[] = {
240
+ 0xe52de004 , // L1: str lr, [sp,#-4]!
241
+ 0xe28fe600 , // add lr, pc, #0x0NN00000 &(.got.plt - L1 - 4)
242
+ 0xe28eea00 , // add lr, lr, #0x000NN000 &(.got.plt - L1 - 4)
243
+ 0xe5bef000 , // ldr pc, [lr, #0x00000NNN] &(.got.plt -L1 - 4)
244
+ };
245
+
246
+ uint64_t offset = in.gotPlt ->getVA () - in.plt ->getVA () - 4 ;
247
+ if (!llvm::isUInt<27 >(offset)) {
248
+ // We cannot encode the Offset, use the long form.
249
+ writePltHeaderLong (buf);
250
+ return ;
251
+ }
252
+ write32 (buf + 0 , pltData[0 ]);
253
+ write32 (buf + 4 , pltData[1 ] | ((offset >> 20 ) & 0xff ));
254
+ write32 (buf + 8 , pltData[2 ] | ((offset >> 12 ) & 0xff ));
255
+ write32 (buf + 12 , pltData[3 ] | (offset & 0xfff ));
256
+ memcpy (buf + 16 , trapInstr.data (), 4 ); // Pad to 32-byte boundary
257
+ memcpy (buf + 20 , trapInstr.data (), 4 );
258
+ memcpy (buf + 24 , trapInstr.data (), 4 );
259
+ memcpy (buf + 28 , trapInstr.data (), 4 );
260
+ } else {
261
+ // The instruction sequence for thumb:
262
+ //
263
+ // 0: b500 push {lr}
264
+ // 2: f8df e008 ldr.w lr, [pc, #0x8] @ 0xe <func+0xe>
265
+ // 6: 44fe add lr, pc
266
+ // 8: f85e ff08 ldr pc, [lr, #8]!
267
+ // e: .word .got.plt - .plt - 16
268
+ //
269
+ // At 0x8, we want to jump to .got.plt, the -16 accounts for 8 bytes from
270
+ // `pc` in the add instruction and 8 bytes for the `lr` adjustment.
271
+ //
272
+ uint64_t offset = in.gotPlt ->getVA () - in.plt ->getVA () - 16 ;
273
+ assert (llvm::isUInt<32 >(offset) && " This should always fit into a 32-bit offset" );
274
+ write16 (buf + 0 , 0xb500 );
275
+ write32 (buf + 2 , 0xe008f8df );
276
+ write16 (buf + 6 , 0x44fe );
277
+ write32 (buf + 8 , 0xff08f85e );
278
+ write32 (buf + 12 , offset);
279
+
280
+ memcpy (buf + 16 , trapInstr.data (), 4 ); // Pad to 32-byte boundary
281
+ memcpy (buf + 20 , trapInstr.data (), 4 );
282
+ memcpy (buf + 24 , trapInstr.data (), 4 );
283
+ memcpy (buf + 28 , trapInstr.data (), 4 );
250
284
}
251
- write32 (buf + 0 , pltData[0 ]);
252
- write32 (buf + 4 , pltData[1 ] | ((offset >> 20 ) & 0xff ));
253
- write32 (buf + 8 , pltData[2 ] | ((offset >> 12 ) & 0xff ));
254
- write32 (buf + 12 , pltData[3 ] | (offset & 0xfff ));
255
- memcpy (buf + 16 , trapInstr.data (), 4 ); // Pad to 32-byte boundary
256
- memcpy (buf + 20 , trapInstr.data (), 4 );
257
- memcpy (buf + 24 , trapInstr.data (), 4 );
258
- memcpy (buf + 28 , trapInstr.data (), 4 );
259
285
}
260
286
261
287
void ARM::addPltHeaderSymbols (InputSection &isec) const {
262
- addSyntheticLocal (" $a" , STT_NOTYPE, 0 , 0 , isec);
263
- addSyntheticLocal (" $d" , STT_NOTYPE, 16 , 0 , isec);
288
+ if (!config->armThumbPLTs ) {
289
+ addSyntheticLocal (" $a" , STT_NOTYPE, 0 , 0 , isec);
290
+ addSyntheticLocal (" $d" , STT_NOTYPE, 16 , 0 , isec);
291
+ } else {
292
+ addSyntheticLocal (" $t" , STT_NOTYPE, 0 , 0 , isec);
293
+ addSyntheticLocal (" $d" , STT_NOTYPE, 12 , 0 , isec);
294
+ }
264
295
}
265
296
266
297
// Long form PLT entries that do not have any restrictions on the displacement
@@ -279,32 +310,97 @@ static void writePltLong(uint8_t *buf, uint64_t gotPltEntryAddr,
279
310
// .plt in the positive direction.
280
311
void ARM::writePlt (uint8_t *buf, const Symbol &sym,
281
312
uint64_t pltEntryAddr) const {
282
- // The PLT entry is similar to the example given in Appendix A of ELF for
283
- // the Arm Architecture. Instead of using the Group Relocations to find the
284
- // optimal rotation for the 8-bit immediate used in the add instructions we
285
- // hard code the most compact rotations for simplicity. This saves a load
286
- // instruction over the long plt sequences.
287
- const uint32_t pltData[] = {
288
- 0xe28fc600 , // L1: add ip, pc, #0x0NN00000 Offset(&(.got.plt) - L1 - 8
289
- 0xe28cca00 , // add ip, ip, #0x000NN000 Offset(&(.got.plt) - L1 - 8
290
- 0xe5bcf000 , // ldr pc, [ip, #0x00000NNN] Offset(&(.got.plt) - L1 - 8
291
- };
292
313
293
- uint64_t offset = sym.getGotPltVA () - pltEntryAddr - 8 ;
294
- if (!llvm::isUInt<27 >(offset)) {
295
- // We cannot encode the Offset, use the long form.
296
- writePltLong (buf, sym.getGotPltVA (), pltEntryAddr);
297
- return ;
314
+ if (!config->armThumbPLTs ) {
315
+ uint64_t offset = sym.getGotPltVA () - pltEntryAddr - 8 ;
316
+
317
+ // The PLT entry is similar to the example given in Appendix A of ELF for
318
+ // the Arm Architecture. Instead of using the Group Relocations to find the
319
+ // optimal rotation for the 8-bit immediate used in the add instructions we
320
+ // hard code the most compact rotations for simplicity. This saves a load
321
+ // instruction over the long plt sequences.
322
+ const uint32_t pltData[] = {
323
+ 0xe28fc600 , // L1: add ip, pc, #0x0NN00000 Offset(&(.got.plt) - L1 - 8
324
+ 0xe28cca00 , // add ip, ip, #0x000NN000 Offset(&(.got.plt) - L1 - 8
325
+ 0xe5bcf000 , // ldr pc, [ip, #0x00000NNN] Offset(&(.got.plt) - L1 - 8
326
+ };
327
+ if (!llvm::isUInt<27 >(offset)) {
328
+ // We cannot encode the Offset, use the long form.
329
+ writePltLong (buf, sym.getGotPltVA (), pltEntryAddr);
330
+ return ;
331
+ }
332
+ write32 (buf + 0 , pltData[0 ] | ((offset >> 20 ) & 0xff ));
333
+ write32 (buf + 4 , pltData[1 ] | ((offset >> 12 ) & 0xff ));
334
+ write32 (buf + 8 , pltData[2 ] | (offset & 0xfff ));
335
+ memcpy (buf + 12 , trapInstr.data (), 4 ); // Pad to 16-byte boundary
336
+ } else {
337
+ uint64_t offset = sym.getGotPltVA () - pltEntryAddr - 12 ;
338
+ assert (llvm::isUInt<32 >(offset) && " This should always fit into a 32-bit offset" );
339
+
340
+ // A PLT entry will be:
341
+ //
342
+ // movw ip, #<lower 16 bits>
343
+ // movt ip, #<upper 16 bits>
344
+ // add ip, pc
345
+ // L1: ldr.w pc, [ip]
346
+ // b L1
347
+ //
348
+ // where ip = r12 = 0xc
349
+ //
350
+ constexpr uint32_t pltData[] = {
351
+ 0x0c00f240 , // movw ip, <offset lower 16>
352
+ 0x0c00f2c0 , // movt ip, <offset higher 16>
353
+ };
354
+
355
+ // movw encoding:
356
+ //
357
+ // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
358
+ // 1 1 1 1 0 i 1 0 0 1 0 0 imm4
359
+ //
360
+ // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
361
+ // 0 imm3 Rd0 imm8
362
+ //
363
+ // imm16 = imm4:i:imm3:imm8, i = bit 11
364
+ //
365
+ uint16_t offset_lower = offset & 0xffff ;
366
+ uint32_t movwImm8 = offset_lower & 0xff ;
367
+ uint32_t movwImm3 = (offset_lower >> 8 ) & 0x7 ;
368
+ uint32_t movwI = (offset_lower >> 11 ) & 0x1 ;
369
+ uint32_t movwImm4 = (offset_lower >> 12 ) & 0xf ;
370
+ uint32_t movwBits = (movwI << 10 ) | (movwImm4 << 0 ) | (movwImm3 << 28 ) | (movwImm8 << 16 );
371
+ write32 (buf + 0 , pltData[0 ] | movwBits);
372
+
373
+ // movt encoding:
374
+ //
375
+ // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
376
+ // 1 1 1 1 0 i 1 0 1 1 0 0 imm4
377
+ //
378
+ // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
379
+ // 0 imm3 Rd0 imm8
380
+ //
381
+ // imm16 = imm4:i:imm3:imm8, i = bit 11
382
+ //
383
+ uint16_t offset_upper = static_cast <uint16_t >(offset >> 16 );
384
+ uint32_t movtImm8 = offset_upper & 0xff ;
385
+ uint32_t movtImm3 = (offset_upper >> 8 ) & 0x7 ;
386
+ uint32_t movtI = (offset_upper >> 11 ) & 0x1 ;
387
+ uint32_t movtImm4 = (offset_upper >> 12 ) & 0xf ;
388
+ uint32_t movtBits = (movtI << 10 ) | (movtImm4 << 0 ) | (movtImm3 << 28 ) | (movtImm8 << 16 );
389
+ write32 (buf + 4 , pltData[1 ] | movtBits);
390
+
391
+ write16 (buf + 8 , 0x44fc ); // add ip, pc
392
+ write32 (buf + 10 , 0xf000f8dc ); // ldr.w pc, [ip]
393
+ write16 (buf + 14 , 0xe7fc ); // Branch to previous instruction
298
394
}
299
- write32 (buf + 0 , pltData[0 ] | ((offset >> 20 ) & 0xff ));
300
- write32 (buf + 4 , pltData[1 ] | ((offset >> 12 ) & 0xff ));
301
- write32 (buf + 8 , pltData[2 ] | (offset & 0xfff ));
302
- memcpy (buf + 12 , trapInstr.data (), 4 ); // Pad to 16-byte boundary
303
395
}
304
396
305
397
void ARM::addPltSymbols (InputSection &isec, uint64_t off) const {
306
- addSyntheticLocal (" $a" , STT_NOTYPE, off, 0 , isec);
307
- addSyntheticLocal (" $d" , STT_NOTYPE, off + 12 , 0 , isec);
398
+ if (!config->armThumbPLTs ) {
399
+ addSyntheticLocal (" $a" , STT_NOTYPE, off, 0 , isec);
400
+ addSyntheticLocal (" $d" , STT_NOTYPE, off + 12 , 0 , isec);
401
+ } else {
402
+ addSyntheticLocal (" $t" , STT_NOTYPE, off, 0 , isec);
403
+ }
308
404
}
309
405
310
406
bool ARM::needsThunk (RelExpr expr, RelType type, const InputFile *file,
@@ -337,7 +433,7 @@ bool ARM::needsThunk(RelExpr expr, RelType type, const InputFile *file,
337
433
case R_ARM_THM_JUMP24:
338
434
// Source is Thumb, all PLT entries are ARM so interworking is required.
339
435
// Otherwise we need to interwork if STT_FUNC Symbol has bit 0 clear (ARM).
340
- if (expr == R_PLT_PC || (s.isFunc () && (s.getVA () & 1 ) == 0 ))
436
+ if (( expr == R_PLT_PC && !config-> armThumbPLTs ) || (s.isFunc () && (s.getVA () & 1 ) == 0 ))
341
437
return true ;
342
438
[[fallthrough]];
343
439
case R_ARM_THM_CALL: {
@@ -547,7 +643,6 @@ void ARM::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
547
643
// STT_FUNC we choose whether to write a BL or BLX depending on the
548
644
// value of bit 0 of Val. With bit 0 == 1 denoting Thumb. If the symbol is
549
645
// not of type STT_FUNC then we must preserve the original instruction.
550
- // PLT entries are always ARM state so we know we don't need to interwork.
551
646
assert (rel.sym ); // R_ARM_CALL is always reached via relocate().
552
647
bool bit0Thumb = val & 1 ;
553
648
bool isBlx = (read32 (loc) & 0xfe000000 ) == 0xfa000000 ;
@@ -606,12 +701,13 @@ void ARM::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
606
701
// PLT entries are always ARM state so we know we need to interwork.
607
702
assert (rel.sym ); // R_ARM_THM_CALL is always reached via relocate().
608
703
bool bit0Thumb = val & 1 ;
704
+ bool useThumb = bit0Thumb || config->armThumbPLTs ;
609
705
bool isBlx = (read16 (loc + 2 ) & 0x1000 ) == 0 ;
610
706
// lld 10.0 and before always used bit0Thumb when deciding to write a BLX
611
- // even when type not STT_FUNC. PLT entries generated by LLD are always ARM.
612
- if (!rel.sym ->isFunc () && !rel.sym ->isInPlt () && isBlx == bit0Thumb )
707
+ // even when type not STT_FUNC.
708
+ if (!rel.sym ->isFunc () && !rel.sym ->isInPlt () && isBlx == useThumb )
613
709
stateChangeWarning (loc, rel.type , *rel.sym );
614
- if (rel.sym ->isFunc () || rel.sym ->isInPlt () ? !bit0Thumb : isBlx) {
710
+ if (( rel.sym ->isFunc () || rel.sym ->isInPlt ()) ? !useThumb : isBlx) {
615
711
// We are writing a BLX. Ensure BLX destination is 4-byte aligned. As
616
712
// the BLX instruction may only be two byte aligned. This must be done
617
713
// before overflow check.
0 commit comments