Skip to content

Commit 2a4d6d4

Browse files
committed
[Object] Add getRISCVVendorRelocationTypeName to render RISCV vendor-specific relocations to strings.
This will be used in places like LLD to render them for error messages.
1 parent edbf9e4 commit 2a4d6d4

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

llvm/include/llvm/Object/ELF.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ struct VersionEntry {
7272
};
7373

7474
LLVM_ABI StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type);
75+
LLVM_ABI StringRef getRISCVVendorRelocationTypeName(uint32_t Type,
76+
StringRef Vendor);
7577
LLVM_ABI uint32_t getELFRelativeRelocationType(uint32_t Machine);
7678
LLVM_ABI StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type);
7779

llvm/lib/Object/ELF.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine,
191191

192192
#undef ELF_RELOC
193193

194+
#define ELF_RISCV_NONSTANDARD_RELOC(vendor, name, number) \
195+
if (Vendor == #vendor && Type == number) \
196+
return #name;
197+
198+
StringRef llvm::object::getRISCVVendorRelocationTypeName(uint32_t Type,
199+
StringRef Vendor) {
200+
#include "llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def"
201+
return "Unknown";
202+
}
203+
204+
#undef ELF_RISCV_NONSTANDARD_RELOC
205+
194206
uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) {
195207
switch (Machine) {
196208
case ELF::EM_X86_64:

llvm/unittests/Object/ELFTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,20 @@ TEST(ELFTest, getELFRelocationTypeNameForLoongArch) {
255255
getELFRelocationTypeName(EM_LOONGARCH, R_LARCH_CALL36));
256256
}
257257

258+
TEST(ELFTest, getRISCVVendorRelocationTypeName) {
259+
EXPECT_EQ("R_RISCV_QC_ABS20_U",
260+
getRISCVVendorRelocationTypeName(R_RISCV_CUSTOM192, "QUALCOMM"));
261+
EXPECT_EQ("R_RISCV_QC_E_BRANCH",
262+
getRISCVVendorRelocationTypeName(R_RISCV_CUSTOM193, "QUALCOMM"));
263+
EXPECT_EQ("R_RISCV_QC_E_32",
264+
getRISCVVendorRelocationTypeName(R_RISCV_CUSTOM194, "QUALCOMM"));
265+
EXPECT_EQ("R_RISCV_QC_E_CALL_PLT",
266+
getRISCVVendorRelocationTypeName(R_RISCV_CUSTOM195, "QUALCOMM"));
267+
268+
EXPECT_EQ("R_RISCV_NDS_BRANCH_10",
269+
getRISCVVendorRelocationTypeName(R_RISCV_CUSTOM241, "ANDES"));
270+
}
271+
258272
TEST(ELFTest, getELFRelativeRelocationType) {
259273
EXPECT_EQ(ELF::R_VE_RELATIVE, getELFRelativeRelocationType(EM_VE));
260274
EXPECT_EQ(ELF::R_LARCH_RELATIVE, getELFRelativeRelocationType(EM_LOONGARCH));

0 commit comments

Comments
 (0)