Skip to content

Commit

Permalink
[llvm-objdump] - Introduce getRelocsMap() helper. NFCI.
Browse files Browse the repository at this point in the history
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

llvm-svn: 351824
  • Loading branch information
George Rimar committed Jan 22, 2019
1 parent 0a9c9a8 commit 121fcd7
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions llvm/tools/llvm-objdump/llvm-objdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,23 @@ static size_t countSkippableZeroBytes(ArrayRef<uint8_t> Buf) {
return N & ~0x3;
}

// Returns a map from sections to their relocations.
static std::map<SectionRef, std::vector<RelocationRef>>
getRelocsMap(llvm::object::ObjectFile const &Obj) {
std::map<SectionRef, std::vector<RelocationRef>> Ret;
for (const SectionRef &Section : ToolSectionFilter(Obj)) {
section_iterator RelSec = Section.getRelocatedSection();
if (RelSec == Obj.section_end())
continue;
std::vector<RelocationRef> &V = Ret[*RelSec];
for (const RelocationRef &R : Section.relocations())
V.push_back(R);
// Sort relocations by address.
llvm::sort(V, isRelocAddressLess);
}
return Ret;
}

static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
if (StartAddress > StopAddress)
error("Start address should be less than stop address");
Expand Down Expand Up @@ -929,15 +946,9 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {

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

// Create a mapping, RelocSecs = SectionRelocMap[S], where sections
// in RelocSecs contain the relocations for section S.
std::error_code EC;
std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
section_iterator Sec2 = Section.getRelocatedSection();
if (Sec2 != Obj->section_end())
SectionRelocMap[*Sec2].push_back(Section);
}
std::map<SectionRef, std::vector<RelocationRef>> RelocMap;
if (InlineRelocs)
RelocMap = getRelocsMap(*Obj);

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

// Make a list of all the relocations for this section.
std::vector<RelocationRef> Rels;
if (InlineRelocs) {
for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
for (const RelocationRef &Reloc : RelocSec.relocations()) {
Rels.push_back(Reloc);
}
}
}

// Sort relocations by address.
llvm::sort(Rels, isRelocAddressLess);

StringRef SegmentName = "";
if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
DataRefImpl DR = Section.getRawDataRefImpl();
Expand Down Expand Up @@ -1103,6 +1101,7 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
uint64_t Index;
bool PrintedSection = false;

std::vector<RelocationRef> Rels = RelocMap[Section];
std::vector<RelocationRef>::const_iterator RelCur = Rels.begin();
std::vector<RelocationRef>::const_iterator RelEnd = Rels.end();
// Disassemble symbol by symbol.
Expand Down

0 comments on commit 121fcd7

Please sign in to comment.