Skip to content

[Hexagon] Add support for decoding PLT symbols #123425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lld/test/ELF/hexagon-plt.s
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@
# DIS-NEXT: 20054: { r14 = asr(r14,#2)
# DIS-NEXT: 20058: jumpr r28 }
# DIS-NEXT: 2005c: { trap0(#219) }
## bar's plt slot
# DIS-EMPTY:
# DIS-NEXT: 00020060 <bar@plt>:
# DIS-NEXT: 20060: { immext(#131072)
# DIS-NEXT: 20064: r14 = add(pc,##131096) }
# DIS-NEXT: 20068: { r28 = memw(r14+#0) }
# DIS-NEXT: 2006c: { jumpr r28 }
## weak's plt slot
# DIS-EMPTY:
# DIS-NEXT: 00020070 <weak@plt>:
# DIS-NEXT: 20070: { immext(#131072)
# DIS-NEXT: 20074: r14 = add(pc,##131084) }
# DIS-NEXT: 20078: { r28 = memw(r14+#0) }
Expand Down
2 changes: 2 additions & 0 deletions lld/test/ELF/hexagon-shared.s
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pvar:
# PLT-NEXT: { r14 = asr(r14,#2)
# PLT-NEXT: jumpr r28 }
# PLT-NEXT: { trap0(#219) }
# PLT-EMPTY:
# PLT-NEXT: 000102f0 <foo@plt>:
# PLT-NEXT: immext(#131200)
# PLT-NEXT: r14 = add(pc,##131252) }
# PLT-NEXT: r28 = memw(r14+#0) }
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Object/ELFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,10 @@ std::vector<ELFPltEntry> ELFObjectFileBase::getPltEntries() const {
case Triple::aarch64_be:
JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
break;
case Triple::hexagon:
JumpSlotReloc = ELF::R_HEX_JMP_SLOT;
GlobDatReloc = ELF::R_HEX_GLOB_DAT;
break;
default:
return {};
}
Expand Down
36 changes: 35 additions & 1 deletion llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,42 @@ class HexagonMCInstrAnalysis : public MCInstrAnalysis {
Target = Value;
return true;
}

uint32_t getValueFromMask(uint32_t Instruction, uint32_t Mask) const {
uint32_t Result = 0;
uint32_t Offset = 0;
while (Mask) {
if (Instruction & (Mask & -Mask))
Result |= (1 << Offset);
Mask &= (Mask - 1);
++Offset;
}
return Result;
}

std::vector<std::pair<uint64_t, uint64_t>>
findPltEntries(uint64_t PltSectionVA, ArrayRef<uint8_t> PltContents,
const Triple &TargetTriple) const override {
// Do a lightweight parsing of PLT entries.
std::vector<std::pair<uint64_t, uint64_t>> Result;
for (uint64_t Byte = 0x0, End = PltContents.size(); Byte < End; Byte += 4) {
// Recognize immext(##gotpltn)
uint32_t ImmExt = support::endian::read32le(PltContents.data() + Byte);
if ((ImmExt & 0x00004000) != 0x00004000)
continue;
uint32_t LoadGotPlt =
support::endian::read32le(PltContents.data() + Byte + 4);
if ((LoadGotPlt & 0x6a49c00c) != 0x6a49c00c)
continue;
uint32_t Address = (getValueFromMask(ImmExt, 0xfff3fff) << 6) +
getValueFromMask(LoadGotPlt, 0x1f80) + PltSectionVA +
Byte;
Result.emplace_back(PltSectionVA + Byte, Address);
}
return Result;
}
};
}
} // namespace

static MCInstrAnalysis *createHexagonMCInstrAnalysis(const MCInstrInfo *Info) {
return new HexagonMCInstrAnalysis(Info);
Expand Down
42 changes: 42 additions & 0 deletions llvm/test/tools/llvm-objdump/ELF/Hexagon/plt.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# RUN: yaml2obj %s | llvm-objdump -d - | FileCheck %s

# CHECK: 00000310 <printf@plt>:
# CHECK-NEXT: 310: 36 40 00 00 00004036 { immext(#0xd80)
# CHECK-NEXT: 314: 0e de 49 6a 6a49de0e r14 = add(pc,##0xdbc) }
# CHECK-NEXT: 318: 1c c0 8e 91 918ec01c { r28 = memw(r14+#0x0) }
# CHECK-NEXT: 31c: 00 c0 9c 52 529cc000 { jumpr r28 }

--- !ELF
FileHeader:
Class: ELFCLASS32
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_HEXAGON
Sections:
- Name: .rela.plt
Type: SHT_RELA
Flags: [ SHF_ALLOC ]
Info: .got.plt
Relocations:
- Offset: 0x10CC
Symbol: printf
Type: R_HEX_JMP_SLOT
- Name: .plt
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Address: 0x2B0
Content: 384000001CC0496A0E429CE24F409C913CC09C910E420E8C00C09C520000000000000000000000000000000000000000374000000ED0496A1CC08E9100C09C52374000000ECA496A1CC08E9100C09C52374000000EC4496A1CC08E9100C09C52364000000EDE496A1CC08E9100C09C52
- Name: .text
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Address: 0x320
Content: 0240096AC97FFF0F01C6007802C221F3FF7FFF0F80C7007800C002F300C0809100C0007500C05F5300C0096ADAFFFF5901C09DA082FFFEBF00C0423C0140000000D4496AD6FFFF5B00C000781EC01E96
- Name: .got.plt
Type: SHT_PROGBITS
Flags: [ SHF_WRITE, SHF_ALLOC ]
Address: 0x10B0
Content: 00000000000000000000000000000000B0020000B0020000B0020000B0020000
Symbols:
- Name: printf
Binding: STB_GLOBAL
...