Skip to content

[llvm-objdump] -r: support CREL #97382

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 8 commits into from
Jul 8, 2024
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
43 changes: 43 additions & 0 deletions llvm/include/llvm/Object/ELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/Object/ELFTypes.h"
#include "llvm/Object/Error.h"
#include "llvm/Support/DataExtractor.h"
#include "llvm/Support/Error.h"
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
#include <utility>

namespace llvm {
Expand Down Expand Up @@ -207,6 +209,47 @@ bool isSectionInSegment(const typename ELFT::Phdr &Phdr,
checkSectionVMA<ELFT>(Phdr, Sec);
}

// HdrHandler is called once with the number of relocations and whether the
// relocations have addends. EntryHandler is called once per decoded relocation.
template <bool Is64>
Error decodeCrel(
ArrayRef<uint8_t> Content,
function_ref<void(uint64_t /*relocation count*/, bool /*explicit addends*/)>
HdrHandler,
function_ref<void(Elf_Crel_Impl<Is64>)> EntryHandler) {
DataExtractor Data(Content, true, 8); // endian and address size are unused
DataExtractor::Cursor Cur(0);
const uint64_t Hdr = Data.getULEB128(Cur);
size_t Count = Hdr / 8;
const size_t FlagBits = Hdr & ELF::CREL_HDR_ADDEND ? 3 : 2;
const size_t Shift = Hdr % ELF::CREL_HDR_ADDEND;
using uint = typename Elf_Crel_Impl<Is64>::uint;
uint Offset = 0, Addend = 0;
HdrHandler(Count, Hdr & ELF::CREL_HDR_ADDEND);
uint32_t SymIdx = 0, Type = 0;
for (; Count; --Count) {
// The delta offset and flags member may be larger than uint64_t. Special
// case the first byte (2 or 3 flag bits; the rest are offset bits). Other
// ULEB128 bytes encode the remaining delta offset bits.
const uint8_t B = Data.getU8(Cur);
Offset += B >> FlagBits;
if (B >= 0x80)
Offset += (Data.getULEB128(Cur) << (7 - FlagBits)) - (0x80 >> FlagBits);
// Delta symidx/type/addend members (SLEB128).
if (B & 1)
SymIdx += Data.getSLEB128(Cur);
if (B & 2)
Type += Data.getSLEB128(Cur);
if (B & 4 & Hdr)
Addend += Data.getSLEB128(Cur);
if (!Cur)
break;
EntryHandler(
{Offset << Shift, SymIdx, Type, std::make_signed_t<uint>(Addend)});
}
return Cur.takeError();
}

template <class ELFT>
class ELFFile {
public:
Expand Down
69 changes: 63 additions & 6 deletions llvm/include/llvm/Object/ELFObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "llvm/Support/ELFAttributes.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/MemoryBufferRef.h"
#include "llvm/Support/ScopedPrinter.h"
#include "llvm/TargetParser/SubtargetFeature.h"
Expand Down Expand Up @@ -122,6 +123,8 @@ class ELFObjectFileBase : public ObjectFile {
Expected<std::vector<BBAddrMap>>
readBBAddrMap(std::optional<unsigned> TextSectionIndex = std::nullopt,
std::vector<PGOAnalysisMap> *PGOAnalyses = nullptr) const;

StringRef getCrelDecodeProblem(SectionRef Sec) const;
};

class ELFSectionRef : public SectionRef {
Expand Down Expand Up @@ -292,6 +295,10 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
const Elf_Shdr *DotSymtabShndxSec = nullptr; // SHT_SYMTAB_SHNDX section.

// Hold CREL relocations for SectionRef::relocations().
mutable SmallVector<SmallVector<Elf_Crel, 0>, 0> Crels;
mutable SmallVector<std::string, 0> CrelDecodeProblems;

Error initContent() override;

void moveSymbolNext(DataRefImpl &Symb) const override;
Expand Down Expand Up @@ -446,6 +453,7 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {

const Elf_Rel *getRel(DataRefImpl Rel) const;
const Elf_Rela *getRela(DataRefImpl Rela) const;
Elf_Crel getCrel(DataRefImpl Crel) const;

Expected<const Elf_Sym *> getSymbol(DataRefImpl Sym) const {
return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
Expand Down Expand Up @@ -499,6 +507,8 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
bool isRelocatableObject() const override;

void createFakeSections() { EF.createFakeSections(); }

StringRef getCrelDecodeProblem(DataRefImpl Sec) const;
};

using ELF32LEObjectFile = ELFObjectFile<ELF32LE>;
Expand Down Expand Up @@ -1022,6 +1032,24 @@ ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
RelData.d.a = (Sec.p - SHT) / EF.getHeader().e_shentsize;
RelData.d.b = 0;
if (reinterpret_cast<const Elf_Shdr *>(Sec.p)->sh_type == ELF::SHT_CREL) {
if (RelData.d.a + 1 > Crels.size())
Crels.resize(RelData.d.a + 1);
auto &Crel = Crels[RelData.d.a];
if (Crel.empty()) {
ArrayRef<uint8_t> Content = cantFail(getSectionContents(Sec));
size_t I = 0;
Error Err = decodeCrel<ELFT::Is64Bits>(
Content, [&](uint64_t Count, bool) { Crel.resize(Count); },
[&](Elf_Crel Crel) { Crels[RelData.d.a][I++] = Crel; });
if (Err) {
Crel.assign(1, Elf_Crel{0, 0, 0, 0});
if (RelData.d.a + 1 > CrelDecodeProblems.size())
CrelDecodeProblems.resize(RelData.d.a + 1);
CrelDecodeProblems[RelData.d.a] = toString(std::move(Err));
}
}
}
return relocation_iterator(RelocationRef(RelData, this));
}

Expand All @@ -1030,9 +1058,13 @@ relocation_iterator
ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
relocation_iterator Begin = section_rel_begin(Sec);
DataRefImpl RelData = Begin->getRawDataRefImpl();
if (S->sh_type == ELF::SHT_CREL) {
RelData.d.b = Crels[RelData.d.a].size();
return relocation_iterator(RelocationRef(RelData, this));
}
if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
return Begin;
DataRefImpl RelData = Begin->getRawDataRefImpl();
const Elf_Shdr *RelSec = getRelSection(RelData);

// Error check sh_link here so that getRelocationSymbol can just use it.
Expand All @@ -1050,7 +1082,7 @@ Expected<section_iterator>
ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
const Elf_Shdr *EShdr = getSection(Sec);
uintX_t Type = EShdr->sh_type;
if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA && Type != ELF::SHT_CREL)
return section_end();

Expected<const Elf_Shdr *> SecOrErr = EF.getSection(EShdr->sh_info);
Expand All @@ -1070,7 +1102,9 @@ symbol_iterator
ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
uint32_t symbolIdx;
const Elf_Shdr *sec = getRelSection(Rel);
if (sec->sh_type == ELF::SHT_REL)
if (sec->sh_type == ELF::SHT_CREL)
symbolIdx = getCrel(Rel).r_symidx;
else if (sec->sh_type == ELF::SHT_REL)
symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
else
symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
Expand All @@ -1087,6 +1121,8 @@ ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
template <class ELFT>
uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
const Elf_Shdr *sec = getRelSection(Rel);
if (sec->sh_type == ELF::SHT_CREL)
return getCrel(Rel).r_offset;
if (sec->sh_type == ELF::SHT_REL)
return getRel(Rel)->r_offset;

Expand All @@ -1096,6 +1132,8 @@ uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
template <class ELFT>
uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
const Elf_Shdr *sec = getRelSection(Rel);
if (sec->sh_type == ELF::SHT_CREL)
return getCrel(Rel).r_type;
if (sec->sh_type == ELF::SHT_REL)
return getRel(Rel)->getType(EF.isMips64EL());
else
Expand All @@ -1117,9 +1155,11 @@ void ELFObjectFile<ELFT>::getRelocationTypeName(
template <class ELFT>
Expected<int64_t>
ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
return createError("Section is not SHT_RELA");
return (int64_t)getRela(Rel)->r_addend;
if (getRelSection(Rel)->sh_type == ELF::SHT_RELA)
return (int64_t)getRela(Rel)->r_addend;
if (getRelSection(Rel)->sh_type == ELF::SHT_CREL)
return (int64_t)getCrel(Rel).r_addend;
return createError("Relocation section does not have addends");
}

template <class ELFT>
Expand All @@ -1142,6 +1182,14 @@ ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
return *Ret;
}

template <class ELFT>
typename ELFObjectFile<ELFT>::Elf_Crel
ELFObjectFile<ELFT>::getCrel(DataRefImpl Crel) const {
assert(getRelSection(Crel)->sh_type == ELF::SHT_CREL);
assert(Crel.d.a < Crels.size());
return Crels[Crel.d.a][Crel.d.b];
}

template <class ELFT>
Expected<ELFObjectFile<ELFT>>
ELFObjectFile<ELFT>::create(MemoryBufferRef Object, bool InitContent) {
Expand Down Expand Up @@ -1453,6 +1501,15 @@ template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
return EF.getHeader().e_type == ELF::ET_REL;
}

template <class ELFT>
StringRef ELFObjectFile<ELFT>::getCrelDecodeProblem(DataRefImpl Sec) const {
uintptr_t SHT = reinterpret_cast<uintptr_t>(cantFail(EF.sections()).begin());
auto I = (Sec.p - SHT) / EF.getHeader().e_shentsize;
if (I < CrelDecodeProblems.size())
return CrelDecodeProblems[I];
return "";
}

} // end namespace object
} // end namespace llvm

Expand Down
61 changes: 23 additions & 38 deletions llvm/lib/Object/ELF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,46 +408,31 @@ ELFFile<ELFT>::getCrelHeader(ArrayRef<uint8_t> Content) const {
template <class ELFT>
Expected<typename ELFFile<ELFT>::RelsOrRelas>
ELFFile<ELFT>::decodeCrel(ArrayRef<uint8_t> Content) const {
DataExtractor Data(Content, isLE(), sizeof(typename ELFT::Addr));
DataExtractor::Cursor Cur(0);
const uint64_t Hdr = Data.getULEB128(Cur);
const size_t Count = Hdr / 8;
const size_t FlagBits = Hdr & ELF::CREL_HDR_ADDEND ? 3 : 2;
const size_t Shift = Hdr % ELF::CREL_HDR_ADDEND;
std::vector<Elf_Rel> Rels;
std::vector<Elf_Rela> Relas;
if (Hdr & ELF::CREL_HDR_ADDEND)
Relas.resize(Count);
else
Rels.resize(Count);
typename ELFT::uint Offset = 0, Addend = 0;
uint32_t SymIdx = 0, Type = 0;
for (size_t I = 0; I != Count; ++I) {
// The delta offset and flags member may be larger than uint64_t. Special
// case the first byte (2 or 3 flag bits; the rest are offset bits). Other
// ULEB128 bytes encode the remaining delta offset bits.
const uint8_t B = Data.getU8(Cur);
Offset += B >> FlagBits;
if (B >= 0x80)
Offset += (Data.getULEB128(Cur) << (7 - FlagBits)) - (0x80 >> FlagBits);
// Delta symidx/type/addend members (SLEB128).
if (B & 1)
SymIdx += Data.getSLEB128(Cur);
if (B & 2)
Type += Data.getSLEB128(Cur);
if (B & 4 & Hdr)
Addend += Data.getSLEB128(Cur);
if (Hdr & ELF::CREL_HDR_ADDEND) {
Relas[I].r_offset = Offset << Shift;
Relas[I].setSymbolAndType(SymIdx, Type, false);
Relas[I].r_addend = Addend;
} else {
Rels[I].r_offset = Offset << Shift;
Rels[I].setSymbolAndType(SymIdx, Type, false);
}
}
if (!Cur)
return std::move(Cur.takeError());
size_t I = 0;
bool HasAddend;
Error Err = object::decodeCrel<ELFT::Is64Bits>(
Content,
[&](uint64_t Count, bool HasA) {
HasAddend = HasA;
if (HasAddend)
Relas.resize(Count);
else
Rels.resize(Count);
},
[&](Elf_Crel Crel) {
if (HasAddend) {
Relas[I].r_offset = Crel.r_offset;
Relas[I].setSymbolAndType(Crel.r_symidx, Crel.r_type, false);
Relas[I++].r_addend = Crel.r_addend;
} else {
Rels[I].r_offset = Crel.r_offset;
Rels[I++].setSymbolAndType(Crel.r_symidx, Crel.r_type, false);
}
});
if (Err)
return std::move(Err);
return std::make_pair(std::move(Rels), std::move(Relas));
}

Expand Down
11 changes: 11 additions & 0 deletions llvm/lib/Object/ELFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,3 +1013,14 @@ Expected<std::vector<BBAddrMap>> ELFObjectFileBase::readBBAddrMap(
return readBBAddrMapImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
TextSectionIndex, PGOAnalyses);
}

StringRef ELFObjectFileBase::getCrelDecodeProblem(SectionRef Sec) const {
auto Data = Sec.getRawDataRefImpl();
if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
return Obj->getCrelDecodeProblem(Data);
if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
return Obj->getCrelDecodeProblem(Data);
if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
return Obj->getCrelDecodeProblem(Data);
return cast<ELF64BEObjectFile>(this)->getCrelDecodeProblem(Data);
}
Loading
Loading