Skip to content

Commit 9b3064a

Browse files
authored
[llvm-objdump][RISCV] Display `@plt' symbols when disassembling .plt section (#147933)
This patch adds dummy symbols for PLT entries for RISC-V 32-bit and 64-bit targets so llvm-objdump can show the function symbol that corresponds to each PLT entry.
1 parent 156e4cb commit 9b3064a

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

llvm/lib/Object/ELFObjectFile.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,10 @@ ELFObjectFileBase::getPltEntries(const MCSubtargetInfo &STI) const {
810810
JumpSlotReloc = ELF::R_HEX_JMP_SLOT;
811811
GlobDatReloc = ELF::R_HEX_GLOB_DAT;
812812
break;
813+
case Triple::riscv32:
814+
case Triple::riscv64:
815+
JumpSlotReloc = ELF::R_RISCV_JUMP_SLOT;
816+
break;
813817
default:
814818
return {};
815819
}

llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "llvm/MC/TargetRegistry.h"
3131
#include "llvm/Support/Compiler.h"
3232
#include "llvm/Support/ErrorHandling.h"
33+
#include "llvm/Support/MathExtras.h"
3334
#include <bitset>
3435

3536
#define GET_INSTRINFO_MC_DESC
@@ -305,6 +306,47 @@ class RISCVMCInstrAnalysis : public MCInstrAnalysis {
305306
}
306307
}
307308

309+
/// Returns (PLT virtual address, GOT virtual address) pairs for PLT entries.
310+
std::vector<std::pair<uint64_t, uint64_t>>
311+
findPltEntries(uint64_t PltSectionVA, ArrayRef<uint8_t> PltContents,
312+
const MCSubtargetInfo &STI) const override {
313+
uint32_t LoadInsnOpCode;
314+
if (const Triple &T = STI.getTargetTriple(); T.isRISCV64())
315+
LoadInsnOpCode = 0x3003; // ld
316+
else if (T.isRISCV32())
317+
LoadInsnOpCode = 0x2003; // lw
318+
else
319+
return {};
320+
321+
constexpr uint64_t FirstEntryAt = 32, EntrySize = 16;
322+
if (PltContents.size() < FirstEntryAt + EntrySize)
323+
return {};
324+
325+
std::vector<std::pair<uint64_t, uint64_t>> Results;
326+
for (uint64_t EntryStart = FirstEntryAt,
327+
EntryStartEnd = PltContents.size() - EntrySize;
328+
EntryStart <= EntryStartEnd; EntryStart += EntrySize) {
329+
const uint32_t AuipcInsn =
330+
support::endian::read32le(PltContents.data() + EntryStart);
331+
const bool IsAuipc = (AuipcInsn & 0x7F) == 0x17;
332+
if (!IsAuipc)
333+
continue;
334+
335+
const uint32_t LoadInsn =
336+
support::endian::read32le(PltContents.data() + EntryStart + 4);
337+
const bool IsLoad = (LoadInsn & 0x707F) == LoadInsnOpCode;
338+
if (!IsLoad)
339+
continue;
340+
341+
const uint64_t GotPltSlotVA = PltSectionVA + EntryStart +
342+
(AuipcInsn & 0xFFFFF000) +
343+
SignExtend64<12>(LoadInsn >> 20);
344+
Results.emplace_back(PltSectionVA + EntryStart, GotPltSlotVA);
345+
}
346+
347+
return Results;
348+
}
349+
308350
private:
309351
static bool maybeReturnAddress(MCRegister Reg) {
310352
// X1 is used for normal returns, X5 for returns from outlined functions.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# RUN: yaml2obj %s | llvm-objdump -d --no-show-raw-insn - | FileCheck %s
2+
3+
# CHECK: 00001470 <strcmp@plt>:
4+
# CHECK-NEXT: 1470: auipc t3, 0x2
5+
# CHECK-NEXT: 1474: lw t3, 0xfc(t3)
6+
# CHECK-NEXT: 1478: jalr t1, t3
7+
# CHECK-NEXT: 147c: nop
8+
9+
--- !ELF
10+
FileHeader:
11+
Class: ELFCLASS32
12+
Data: ELFDATA2LSB
13+
Type: ET_DYN
14+
Machine: EM_RISCV
15+
Flags: [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_DOUBLE ]
16+
Sections:
17+
- Name: .rela.plt
18+
Type: SHT_RELA
19+
Flags: [ SHF_ALLOC, SHF_INFO_LINK ]
20+
Address: 0x290
21+
Link: .dynsym
22+
AddressAlign: 0x4
23+
Info: .got.plt
24+
Relocations:
25+
- Offset: 0x356C
26+
Symbol: strcmp
27+
Type: R_RISCV_JUMP_SLOT
28+
- Name: .plt
29+
Type: SHT_PROGBITS
30+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
31+
Address: 0x1450
32+
AddressAlign: 0x10
33+
Content: 972300003303C34103AE4311130343FD938243111353230083A2420067000E00172E0000032ECE0F67030E0013000000
34+
- Name: .got.plt
35+
Type: SHT_PROGBITS
36+
Flags: [ SHF_WRITE, SHF_ALLOC ]
37+
Address: 0x3564
38+
AddressAlign: 0x4
39+
Content: '000000000000000050140000'
40+
DynamicSymbols:
41+
- Name: strcmp
42+
Type: STT_FUNC
43+
Binding: STB_GLOBAL
44+
...
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# RUN: yaml2obj %s | llvm-objdump -d --no-show-raw-insn - | FileCheck %s
2+
3+
# CHECK: 0000000000001630 <strcmp@plt>:
4+
# CHECK-NEXT: 1630: auipc t3, 0x2
5+
# CHECK-NEXT: 1634: ld t3, 0x1b8(t3)
6+
# CHECK-NEXT: 1638: jalr t1, t3
7+
# CHECK-NEXT: 163c: nop
8+
9+
--- !ELF
10+
FileHeader:
11+
Class: ELFCLASS64
12+
Data: ELFDATA2LSB
13+
Type: ET_DYN
14+
Machine: EM_RISCV
15+
Flags: [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_DOUBLE ]
16+
Sections:
17+
- Name: .rela.plt
18+
Type: SHT_RELA
19+
Flags: [ SHF_ALLOC, SHF_INFO_LINK ]
20+
Address: 0x408
21+
Link: .dynsym
22+
AddressAlign: 0x8
23+
Info: .got.plt
24+
Relocations:
25+
- Offset: 0x37E8
26+
Symbol: strcmp
27+
Type: R_RISCV_JUMP_SLOT
28+
- Name: .plt
29+
Type: SHT_PROGBITS
30+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
31+
Address: 0x1610
32+
AddressAlign: 0x10
33+
Content: 972300003303C34103BE831C130343FD9382831C1353130083B2820067000E00172E0000033E8E1B67030E0013000000
34+
- Name: .got.plt
35+
Type: SHT_PROGBITS
36+
Flags: [ SHF_WRITE, SHF_ALLOC ]
37+
Address: 0x37D8
38+
AddressAlign: 0x8
39+
Content: '000000000000000000000000000000001016000000000000'
40+
DynamicSymbols:
41+
- Name: strcmp
42+
Type: STT_FUNC
43+
Binding: STB_GLOBAL
44+
...

0 commit comments

Comments
 (0)