Skip to content

[Object] Provide operator< for ELFSymbolRef #89861

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 6 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions llvm/include/llvm/Object/ELFObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ class ELFSymbolRef : public SymbolRef {
}
};

inline bool operator<(const ELFSymbolRef &A, const ELFSymbolRef &B) {
const DataRefImpl &DRIA = A.getRawDataRefImpl();
const DataRefImpl &DRIB = B.getRawDataRefImpl();
if (DRIA.d.a == DRIB.d.a)
return DRIA.d.b < DRIB.d.b;
return DRIA.d.a < DRIB.d.a;
}

class elf_symbol_iterator : public symbol_iterator {
public:
elf_symbol_iterator(const basic_symbol_iterator &B)
Expand Down
36 changes: 36 additions & 0 deletions llvm/unittests/Object/ELFObjectFileTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1504,3 +1504,39 @@ TEST(ELFObjectFileTest, GetSectionAndRelocations) {
"SHT_RELA section with index 1: failed to get a "
"relocated section: invalid section index: 255");
}

TEST(ELFObjectFileTest, ELFSymbolRefLess) {
SmallString<0> Storage;
Expected<ELFObjectFile<ELF64LE>> ElfOrErr = toBinary<ELF64LE>(Storage, R"(
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
)");

ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded());
const ELFObjectFile<ELF64LE> &Obj = *ElfOrErr;

llvm::object::DataRefImpl Data1;
Data1.d.a = 0x00000000;
Data1.d.b = 0x00000001;
SymbolRef Symbol1(Data1, &Obj);
ELFSymbolRef ELFSymbol1(Symbol1);

llvm::object::DataRefImpl Data2;
Data2.d.a = 0x00000000;
Data2.d.b = 0x00000100;
SymbolRef Symbol2(Data2, &Obj);
ELFSymbolRef ELFSymbol2(Symbol2);

// SymbolRef operator< uses std::memcmp treating the union as char string.
if (llvm::sys::IsLittleEndianHost)
EXPECT_FALSE(Symbol1 < Symbol2);
else
EXPECT_TRUE(Symbol1 < Symbol2);

// ELFSymbolRef operator< compares struct fields.
EXPECT_TRUE(ELFSymbol1 < ELFSymbol2);
}