[llvm] 68914dc - [JITLink] Adopt forEachRelocation() helper in ELF x86-64 backend (NFC)
Stefan Gränitz via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 20 06:47:08 PDT 2021
Author: Stefan Gränitz
Date: 2021-09-20T15:46:41+02:00
New Revision: 68914dc99083716d6e9868798c67e73ef35b021e
URL: https://github.com/llvm/llvm-project/commit/68914dc99083716d6e9868798c67e73ef35b021e
DIFF: https://github.com/llvm/llvm-project/commit/68914dc99083716d6e9868798c67e73ef35b021e.diff
LOG: [JITLink] Adopt forEachRelocation() helper in ELF x86-64 backend (NFC)
Following D109516, this patch re-uses the new helper function for ELF relocation traversal in the x86-64 backend.
Reviewed By: StephenFan
Differential Revision: https://reviews.llvm.org/D109520
Added:
Modified:
llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
Removed:
################################################################################
diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
index f4e1770a61cf..034b1d98b4c8 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
@@ -223,6 +223,7 @@ namespace jitlink {
// generic
class ELFLinkGraphBuilder_x86_64 : public ELFLinkGraphBuilder<object::ELF64LE> {
private:
+ using ELFT = object::ELF64LE;
static Expected<ELF_x86_64_Edges::ELFX86RelocationKind>
getRelocationKind(const uint32_t Type) {
@@ -259,153 +260,108 @@ class ELFLinkGraphBuilder_x86_64 : public ELFLinkGraphBuilder<object::ELF64LE> {
}
Error addRelocations() override {
- LLVM_DEBUG(dbgs() << "Adding relocations\n");
- // TODO a partern is forming of iterate some sections but only give me
- // ones I am interested, i should abstract that concept some where
- for (auto &SecRef : Sections) {
- if (SecRef.sh_type != ELF::SHT_RELA && SecRef.sh_type != ELF::SHT_REL)
- continue;
- // TODO can the elf obj file do this for me?
- if (SecRef.sh_type == ELF::SHT_REL)
- return make_error<llvm::StringError>("Shouldn't have REL in x64",
- llvm::inconvertibleErrorCode());
-
- auto RelSectName = Obj.getSectionName(SecRef);
- if (!RelSectName)
- return RelSectName.takeError();
-
- LLVM_DEBUG({
- dbgs() << "Adding relocations from section " << *RelSectName << "\n";
- });
-
- auto UpdateSection = Obj.getSection(SecRef.sh_info);
- if (!UpdateSection)
- return UpdateSection.takeError();
-
- auto UpdateSectionName = Obj.getSectionName(**UpdateSection);
- if (!UpdateSectionName)
- return UpdateSectionName.takeError();
-
- // Don't process relocations for debug sections.
- if (isDwarfSection(*UpdateSectionName)) {
- LLVM_DEBUG({
- dbgs() << " Target is dwarf section " << *UpdateSectionName
- << ". Skipping.\n";
- });
- continue;
- } else
- LLVM_DEBUG({
- dbgs() << " For target section " << *UpdateSectionName << "\n";
- });
-
- auto JITSection = G->findSectionByName(*UpdateSectionName);
- if (!JITSection)
- return make_error<llvm::StringError>(
- "Refencing a a section that wasn't added to graph" +
- *UpdateSectionName,
- llvm::inconvertibleErrorCode());
-
- auto Relocations = Obj.relas(SecRef);
- if (!Relocations)
- return Relocations.takeError();
-
- for (const auto &Rela : *Relocations) {
- auto Type = Rela.getType(false);
-
- LLVM_DEBUG({
- dbgs() << "Relocation Type: " << Type << "\n"
- << "Name: " << Obj.getRelocationTypeName(Type) << "\n";
- });
- auto SymbolIndex = Rela.getSymbol(false);
- auto Symbol = Obj.getRelocationSymbol(Rela, SymTabSec);
- if (!Symbol)
- return Symbol.takeError();
-
- auto BlockToFix = *(JITSection->blocks().begin());
- auto *TargetSymbol = getGraphSymbol(SymbolIndex);
-
- if (!TargetSymbol) {
- return make_error<llvm::StringError>(
- "Could not find symbol at given index, did you add it to "
- "JITSymbolTable? index: " +
- std::to_string(SymbolIndex) +
- ", shndx: " + std::to_string((*Symbol)->st_shndx) +
- " Size of table: " + std::to_string(GraphSymbols.size()),
- llvm::inconvertibleErrorCode());
- }
- int64_t Addend = Rela.r_addend;
- JITTargetAddress FixupAddress =
- (*UpdateSection)->sh_addr + Rela.r_offset;
-
- LLVM_DEBUG({
- dbgs() << "Processing relocation at "
- << format("0x%016" PRIx64, FixupAddress) << "\n";
- });
- auto ELFRelocKind = getRelocationKind(Type);
- if (!ELFRelocKind)
- return ELFRelocKind.takeError();
-
- Edge::Kind Kind = Edge::Invalid;
- switch (*ELFRelocKind) {
- case PCRel32:
- Kind = x86_64::Delta32;
- break;
- case Delta64:
- Kind = x86_64::Delta64;
- break;
- case Pointer32Signed:
- Kind = x86_64::Pointer32Signed;
- break;
- case Pointer64:
- Kind = x86_64::Pointer64;
- break;
- case PCRel32GOTLoad: {
- Kind = x86_64::RequestGOTAndTransformToDelta32;
- break;
- }
- case PCRel32REXGOTLoadRelaxable: {
- Kind = x86_64::RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable;
- Addend = 0;
- break;
- }
- case PCRel32TLV: {
- Kind = x86_64::RequestTLSDescInGOTAndTransformToDelta32;
- break;
- }
- case PCRel32GOTLoadRelaxable: {
- Kind = x86_64::RequestGOTAndTransformToPCRel32GOTLoadRelaxable;
- Addend = 0;
- break;
- }
- case PCRel64GOT: {
- Kind = x86_64::RequestGOTAndTransformToDelta64;
- break;
- }
- case GOT64: {
- Kind = x86_64::RequestGOTAndTransformToDelta64FromGOT;
- break;
- }
- case GOTOFF64: {
- Kind = x86_64::Delta64FromGOT;
- break;
- }
- case Branch32: {
- Kind = x86_64::BranchPCRel32;
- Addend = 0;
- break;
- }
- }
+ LLVM_DEBUG(dbgs() << "Processing relocations:\n");
+
+ using Base = ELFLinkGraphBuilder<ELFT>;
+ using Self = ELFLinkGraphBuilder_x86_64;
+ for (const auto &RelSect : Base::Sections) {
+ // Sanity check the section to read relocation entries from.
+ if (RelSect.sh_type == ELF::SHT_REL)
+ return make_error<StringError>(
+ "No SHT_REL in valid x64 ELF object files",
+ inconvertibleErrorCode());
+
+ if (Error Err = Base::forEachRelocation(RelSect, this,
+ &Self::addSingleRelocation))
+ return Err;
+ }
+
+ return Error::success();
+ }
- LLVM_DEBUG({
- Edge GE(Kind, FixupAddress - BlockToFix->getAddress(), *TargetSymbol,
- Addend);
- printEdge(dbgs(), *BlockToFix, GE, getELFX86RelocationKindName(Kind));
- dbgs() << "\n";
- });
- BlockToFix->addEdge(Kind, FixupAddress - BlockToFix->getAddress(),
- *TargetSymbol, Addend);
- }
+ Error addSingleRelocation(const typename ELFT::Rela &Rel,
+ const typename ELFT::Shdr &FixupSection,
+ Section &GraphSection) {
+ using Base = ELFLinkGraphBuilder<ELFT>;
+
+ uint32_t SymbolIndex = Rel.getSymbol(false);
+ auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
+ if (!ObjSymbol)
+ return ObjSymbol.takeError();
+
+ Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
+ if (!GraphSymbol)
+ return make_error<StringError>(
+ formatv("Could not find symbol at given index, did you add it to "
+ "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
+ SymbolIndex, (*ObjSymbol)->st_shndx,
+ Base::GraphSymbols.size()),
+ inconvertibleErrorCode());
+
+ // Sanity check the relocation kind.
+ auto ELFRelocKind = getRelocationKind(Rel.getType(false));
+ if (!ELFRelocKind)
+ return ELFRelocKind.takeError();
+
+ int64_t Addend = Rel.r_addend;
+ Edge::Kind Kind = Edge::Invalid;
+ switch (*ELFRelocKind) {
+ case PCRel32:
+ Kind = x86_64::Delta32;
+ break;
+ case Delta64:
+ Kind = x86_64::Delta64;
+ break;
+ case Pointer32Signed:
+ Kind = x86_64::Pointer32Signed;
+ break;
+ case Pointer64:
+ Kind = x86_64::Pointer64;
+ break;
+ case PCRel32GOTLoad: {
+ Kind = x86_64::RequestGOTAndTransformToDelta32;
+ break;
+ }
+ case PCRel32REXGOTLoadRelaxable: {
+ Kind = x86_64::RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable;
+ Addend = 0;
+ break;
+ }
+ case PCRel32GOTLoadRelaxable: {
+ Kind = x86_64::RequestGOTAndTransformToPCRel32GOTLoadRelaxable;
+ Addend = 0;
+ break;
+ }
+ case PCRel64GOT: {
+ Kind = x86_64::RequestGOTAndTransformToDelta64;
+ break;
}
+ case GOT64: {
+ Kind = x86_64::RequestGOTAndTransformToDelta64FromGOT;
+ break;
+ }
+ case GOTOFF64: {
+ Kind = x86_64::Delta64FromGOT;
+ break;
+ }
+ case Branch32: {
+ Kind = x86_64::BranchPCRel32;
+ Addend = 0;
+ break;
+ }
+ }
+
+ Block *BlockToFix = *(GraphSection.blocks().begin());
+ JITTargetAddress FixupAddress = FixupSection.sh_addr + Rel.r_offset;
+ Edge::OffsetT Offset = FixupAddress - BlockToFix->getAddress();
+ Edge GE(Kind, Offset, *GraphSymbol, Addend);
+ LLVM_DEBUG({
+ dbgs() << " ";
+ printEdge(dbgs(), *BlockToFix, GE, getELFX86RelocationKindName(Kind));
+ dbgs() << "\n";
+ });
+
+ BlockToFix->addEdge(std::move(GE));
return Error::success();
}
More information about the llvm-commits
mailing list