Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 5d73c0d

Browse files
author
George Rimar
committed
[llvm-objdump] - Introduce getRelocsMap() helper. NFCI.
Currently disassembleObject() is a ~550 lines length function. This patch extracts the code that creates a section->their relocation mapping into a new helper function to simplify/reduce it a bit. Differential revision: https://reviews.llvm.org/D57019 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351824 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent f315bf0 commit 5d73c0d

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,23 @@ static size_t countSkippableZeroBytes(ArrayRef<uint8_t> Buf) {
868868
return N & ~0x3;
869869
}
870870

871+
// Returns a map from sections to their relocations.
872+
static std::map<SectionRef, std::vector<RelocationRef>>
873+
getRelocsMap(llvm::object::ObjectFile const &Obj) {
874+
std::map<SectionRef, std::vector<RelocationRef>> Ret;
875+
for (const SectionRef &Section : ToolSectionFilter(Obj)) {
876+
section_iterator RelSec = Section.getRelocatedSection();
877+
if (RelSec == Obj.section_end())
878+
continue;
879+
std::vector<RelocationRef> &V = Ret[*RelSec];
880+
for (const RelocationRef &R : Section.relocations())
881+
V.push_back(R);
882+
// Sort relocations by address.
883+
llvm::sort(V, isRelocAddressLess);
884+
}
885+
return Ret;
886+
}
887+
871888
static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
872889
if (StartAddress > StopAddress)
873890
error("Start address should be less than stop address");
@@ -929,15 +946,9 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
929946

930947
SourcePrinter SP(Obj, TheTarget->getName());
931948

932-
// Create a mapping, RelocSecs = SectionRelocMap[S], where sections
933-
// in RelocSecs contain the relocations for section S.
934-
std::error_code EC;
935-
std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
936-
for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
937-
section_iterator Sec2 = Section.getRelocatedSection();
938-
if (Sec2 != Obj->section_end())
939-
SectionRelocMap[*Sec2].push_back(Section);
940-
}
949+
std::map<SectionRef, std::vector<RelocationRef>> RelocMap;
950+
if (InlineRelocs)
951+
RelocMap = getRelocsMap(*Obj);
941952

942953
// Create a mapping from virtual address to symbol name. This is used to
943954
// pretty print the symbols while disassembling.
@@ -1062,19 +1073,6 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
10621073
}
10631074
}
10641075

1065-
// Make a list of all the relocations for this section.
1066-
std::vector<RelocationRef> Rels;
1067-
if (InlineRelocs) {
1068-
for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
1069-
for (const RelocationRef &Reloc : RelocSec.relocations()) {
1070-
Rels.push_back(Reloc);
1071-
}
1072-
}
1073-
}
1074-
1075-
// Sort relocations by address.
1076-
llvm::sort(Rels, isRelocAddressLess);
1077-
10781076
StringRef SegmentName = "";
10791077
if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
10801078
DataRefImpl DR = Section.getRawDataRefImpl();
@@ -1103,6 +1101,7 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
11031101
uint64_t Index;
11041102
bool PrintedSection = false;
11051103

1104+
std::vector<RelocationRef> Rels = RelocMap[Section];
11061105
std::vector<RelocationRef>::const_iterator RelCur = Rels.begin();
11071106
std::vector<RelocationRef>::const_iterator RelEnd = Rels.end();
11081107
// Disassemble symbol by symbol.

0 commit comments

Comments
 (0)