[llvm] df6d2fa - [Object] Provide operator< for ELFSymbolRef (#89861)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 29 09:07:29 PDT 2024
Author: Amir Ayupov
Date: 2024-04-29T09:07:26-07:00
New Revision: df6d2faa22bf19fb27b5c083e590d13024a8d7cd
URL: https://github.com/llvm/llvm-project/commit/df6d2faa22bf19fb27b5c083e590d13024a8d7cd
DIFF: https://github.com/llvm/llvm-project/commit/df6d2faa22bf19fb27b5c083e590d13024a8d7cd.diff
LOG: [Object] Provide operator< for ELFSymbolRef (#89861)
Normally, operator< accepting DataRefImpl is used when comparing
SymbolRef/ELFSymbolRef. However, it uses std::memcmp which interprets
DataRefImpl union as char string so that the result depends on host
endianness.
For ELFSymbolRef a specialized operator< can be used instead to produce
consistent ordering regardless of endianness by comparing the symbol
table index and symbol index fields separately.
Added:
Modified:
llvm/include/llvm/Object/ELFObjectFile.h
llvm/unittests/Object/ELFObjectFileTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Object/ELFObjectFile.h b/llvm/include/llvm/Object/ELFObjectFile.h
index 4494d9b96189bc..8cc09e7fd7d551 100644
--- a/llvm/include/llvm/Object/ELFObjectFile.h
+++ b/llvm/include/llvm/Object/ELFObjectFile.h
@@ -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)
diff --git a/llvm/unittests/Object/ELFObjectFileTest.cpp b/llvm/unittests/Object/ELFObjectFileTest.cpp
index c4d2b4ae8b9acd..c13dc0e3fab898 100644
--- a/llvm/unittests/Object/ELFObjectFileTest.cpp
+++ b/llvm/unittests/Object/ELFObjectFileTest.cpp
@@ -1504,3 +1504,46 @@ 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;
+
+ const uint32_t ValLow = 0x00000001;
+ const uint32_t ValHigh = 0x00000100;
+
+ auto MakeSymbol = [&Obj](size_t SymtabIndex, size_t SymbolIndex) {
+ DataRefImpl Data;
+ Data.d.a = SymtabIndex;
+ Data.d.b = SymbolIndex;
+ SymbolRef Sym(Data, &Obj);
+ return ELFSymbolRef(Sym);
+ };
+
+ ELFSymbolRef ELFSymLowLow = MakeSymbol(ValLow, ValLow);
+ ELFSymbolRef ELFSymLowHigh = MakeSymbol(ValLow, ValHigh);
+ ELFSymbolRef ELFSymHighLow = MakeSymbol(ValHigh, ValLow);
+ ELFSymbolRef ELFSymHighHigh = MakeSymbol(ValHigh, ValHigh);
+
+ EXPECT_TRUE(ELFSymLowLow < ELFSymLowHigh);
+ EXPECT_FALSE(ELFSymLowHigh < ELFSymLowLow);
+ EXPECT_FALSE(ELFSymLowLow < ELFSymLowLow);
+
+ EXPECT_TRUE(ELFSymLowLow < ELFSymHighHigh);
+ EXPECT_TRUE(ELFSymLowHigh < ELFSymHighLow);
+ EXPECT_TRUE(ELFSymLowLow < ELFSymHighLow);
+
+ EXPECT_FALSE(ELFSymHighLow < ELFSymLowHigh);
+ EXPECT_FALSE(ELFSymHighHigh < ELFSymLowLow);
+ EXPECT_FALSE(ELFSymHighLow < ELFSymLowLow);
+}
More information about the llvm-commits
mailing list