[C++11] Use ObjectFile::sections() in commandline llvm tools

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203802 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alexey Samsonov
2014-03-13 14:37:36 +00:00
parent b920dfe02b
commit 7a2da95e44
5 changed files with 111 additions and 114 deletions

View File

@ -158,13 +158,10 @@ getSectionsAndSymbols(const MachO::mach_header Header,
SI != SE; ++SI) SI != SE; ++SI)
Symbols.push_back(*SI); Symbols.push_back(*SI);
for (section_iterator SI = MachOObj->section_begin(), for (const SectionRef &Section : MachOObj->sections()) {
SE = MachOObj->section_end();
SI != SE; ++SI) {
SectionRef SR = *SI;
StringRef SectName; StringRef SectName;
SR.getName(SectName); Section.getName(SectName);
Sections.push_back(*SI); Sections.push_back(Section);
} }
MachOObjectFile::LoadCommandInfo Command = MachOObjectFile::LoadCommandInfo Command =

View File

@ -385,28 +385,26 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
// Create a mapping, RelocSecs = SectionRelocMap[S], where sections // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
// in RelocSecs contain the relocations for section S. // in RelocSecs contain the relocations for section S.
error_code EC; error_code EC;
std::map<SectionRef, SmallVector<SectionRef, 1> > SectionRelocMap; std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
for (section_iterator I = Obj->section_begin(), E = Obj->section_end(); for (const SectionRef &Section : Obj->sections()) {
I != E; ++I) { section_iterator Sec2 = Section.getRelocatedSection();
section_iterator Sec2 = I->getRelocatedSection();
if (Sec2 != Obj->section_end()) if (Sec2 != Obj->section_end())
SectionRelocMap[*Sec2].push_back(*I); SectionRelocMap[*Sec2].push_back(Section);
} }
for (section_iterator I = Obj->section_begin(), E = Obj->section_end(); for (const SectionRef &Section : Obj->sections()) {
I != E; ++I) {
bool Text; bool Text;
if (error(I->isText(Text))) if (error(Section.isText(Text)))
break; break;
if (!Text) if (!Text)
continue; continue;
uint64_t SectionAddr; uint64_t SectionAddr;
if (error(I->getAddress(SectionAddr))) if (error(Section.getAddress(SectionAddr)))
break; break;
uint64_t SectSize; uint64_t SectSize;
if (error(I->getSize(SectSize))) if (error(Section.getSize(SectSize)))
break; break;
// Make a list of all the symbols in this section. // Make a list of all the symbols in this section.
@ -414,7 +412,7 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
for (symbol_iterator SI = Obj->symbol_begin(), SE = Obj->symbol_end(); for (symbol_iterator SI = Obj->symbol_begin(), SE = Obj->symbol_end();
SI != SE; ++SI) { SI != SE; ++SI) {
bool contains; bool contains;
if (!error(I->containsSymbol(*SI, contains)) && contains) { if (!error(Section.containsSymbol(*SI, contains)) && contains) {
uint64_t Address; uint64_t Address;
if (error(SI->getAddress(Address))) if (error(SI->getAddress(Address)))
break; break;
@ -437,7 +435,7 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
// Make a list of all the relocations for this section. // Make a list of all the relocations for this section.
std::vector<RelocationRef> Rels; std::vector<RelocationRef> Rels;
if (InlineRelocs) { if (InlineRelocs) {
SmallVectorImpl<SectionRef> *RelocSecs = &SectionRelocMap[*I]; SmallVectorImpl<SectionRef> *RelocSecs = &SectionRelocMap[Section];
for (SmallVectorImpl<SectionRef>::iterator RelocSec = RelocSecs->begin(), for (SmallVectorImpl<SectionRef>::iterator RelocSec = RelocSecs->begin(),
E = RelocSecs->end(); E = RelocSecs->end();
RelocSec != E; ++RelocSec) { RelocSec != E; ++RelocSec) {
@ -453,11 +451,11 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
StringRef SegmentName = ""; StringRef SegmentName = "";
if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) { if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
DataRefImpl DR = I->getRawDataRefImpl(); DataRefImpl DR = Section.getRawDataRefImpl();
SegmentName = MachO->getSectionFinalSegmentName(DR); SegmentName = MachO->getSectionFinalSegmentName(DR);
} }
StringRef name; StringRef name;
if (error(I->getName(name))) if (error(Section.getName(name)))
break; break;
outs() << "Disassembly of section "; outs() << "Disassembly of section ";
if (!SegmentName.empty()) if (!SegmentName.empty())
@ -474,7 +472,7 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
raw_svector_ostream CommentStream(Comments); raw_svector_ostream CommentStream(Comments);
StringRef Bytes; StringRef Bytes;
if (error(I->getContents(Bytes))) if (error(Section.getContents(Bytes)))
break; break;
StringRefMemoryObject memoryObject(Bytes, SectionAddr); StringRefMemoryObject memoryObject(Bytes, SectionAddr);
uint64_t Size; uint64_t Size;
@ -554,16 +552,16 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
} }
} }
static void PrintRelocations(const ObjectFile *o) { static void PrintRelocations(const ObjectFile *Obj) {
for (section_iterator si = o->section_begin(), se = o->section_end(); for (const SectionRef &Section : Obj->sections()) {
si != se; ++si) { if (Section.relocation_begin() == Section.relocation_end())
if (si->relocation_begin() == si->relocation_end())
continue; continue;
StringRef secname; StringRef secname;
if (error(si->getName(secname))) continue; if (error(Section.getName(secname)))
continue;
outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n"; outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
for (relocation_iterator ri = si->relocation_begin(), for (relocation_iterator ri = Section.relocation_begin(),
re = si->relocation_end(); re = Section.relocation_end();
ri != re; ++ri) { ri != re; ++ri) {
bool hidden; bool hidden;
uint64_t address; uint64_t address;
@ -580,43 +578,50 @@ static void PrintRelocations(const ObjectFile *o) {
} }
} }
static void PrintSectionHeaders(const ObjectFile *o) { static void PrintSectionHeaders(const ObjectFile *Obj) {
outs() << "Sections:\n" outs() << "Sections:\n"
"Idx Name Size Address Type\n"; "Idx Name Size Address Type\n";
unsigned i = 0; unsigned i = 0;
for (section_iterator si = o->section_begin(), se = o->section_end(); for (const SectionRef &Section : Obj->sections()) {
si != se; ++si) {
StringRef Name; StringRef Name;
if (error(si->getName(Name))) if (error(Section.getName(Name)))
return; return;
uint64_t Address; uint64_t Address;
if (error(si->getAddress(Address))) return; if (error(Section.getAddress(Address)))
return;
uint64_t Size; uint64_t Size;
if (error(si->getSize(Size))) return; if (error(Section.getSize(Size)))
return;
bool Text, Data, BSS; bool Text, Data, BSS;
if (error(si->isText(Text))) return; if (error(Section.isText(Text)))
if (error(si->isData(Data))) return; return;
if (error(si->isBSS(BSS))) return; if (error(Section.isData(Data)))
return;
if (error(Section.isBSS(BSS)))
return;
std::string Type = (std::string(Text ? "TEXT " : "") + std::string Type = (std::string(Text ? "TEXT " : "") +
(Data ? "DATA " : "") + (BSS ? "BSS" : "")); (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
i, Name.str().c_str(), Size, Address, Type.c_str()); Name.str().c_str(), Size, Address, Type.c_str());
++i; ++i;
} }
} }
static void PrintSectionContents(const ObjectFile *o) { static void PrintSectionContents(const ObjectFile *Obj) {
error_code EC; error_code EC;
for (section_iterator si = o->section_begin(), se = o->section_end(); for (const SectionRef &Section : Obj->sections()) {
si != se; ++si) {
StringRef Name; StringRef Name;
StringRef Contents; StringRef Contents;
uint64_t BaseAddr; uint64_t BaseAddr;
bool BSS; bool BSS;
if (error(si->getName(Name))) continue; if (error(Section.getName(Name)))
if (error(si->getContents(Contents))) continue; continue;
if (error(si->getAddress(BaseAddr))) continue; if (error(Section.getContents(Contents)))
if (error(si->isBSS(BSS))) continue; continue;
if (error(Section.getAddress(BaseAddr)))
continue;
if (error(Section.isBSS(BSS)))
continue;
outs() << "Contents of section " << Name << ":\n"; outs() << "Contents of section " << Name << ":\n";
if (BSS) { if (BSS) {

View File

@ -40,10 +40,9 @@ public:
private: private:
void printSymbol(symbol_iterator SymI); void printSymbol(symbol_iterator SymI);
void printRelocation(section_iterator SecI, relocation_iterator RelI); void printRelocation(relocation_iterator RelI);
void printRelocation(const MachOObjectFile *Obj, void printRelocation(const MachOObjectFile *Obj, relocation_iterator RelI);
section_iterator SecI, relocation_iterator RelI);
void printSections(const MachOObjectFile *Obj); void printSections(const MachOObjectFile *Obj);
@ -216,17 +215,15 @@ void MachODumper::printSections(const MachOObjectFile *Obj) {
ListScope Group(W, "Sections"); ListScope Group(W, "Sections");
int SectionIndex = -1; int SectionIndex = -1;
for (section_iterator SecI = Obj->section_begin(), for (const SectionRef &Section : Obj->sections()) {
SecE = Obj->section_end();
SecI != SecE; ++SecI) {
++SectionIndex; ++SectionIndex;
MachOSection Section; MachOSection MOSection;
getSection(Obj, SecI->getRawDataRefImpl(), Section); getSection(Obj, Section.getRawDataRefImpl(), MOSection);
DataRefImpl DR = SecI->getRawDataRefImpl(); DataRefImpl DR = Section.getRawDataRefImpl();
StringRef Name; StringRef Name;
if (error(SecI->getName(Name))) if (error(Section.getName(Name)))
Name = ""; Name = "";
ArrayRef<char> RawName = Obj->getSectionRawName(DR); ArrayRef<char> RawName = Obj->getSectionRawName(DR);
@ -237,34 +234,33 @@ void MachODumper::printSections(const MachOObjectFile *Obj) {
W.printNumber("Index", SectionIndex); W.printNumber("Index", SectionIndex);
W.printBinary("Name", Name, RawName); W.printBinary("Name", Name, RawName);
W.printBinary("Segment", SegmentName, RawSegmentName); W.printBinary("Segment", SegmentName, RawSegmentName);
W.printHex ("Address", Section.Address); W.printHex("Address", MOSection.Address);
W.printHex ("Size", Section.Size); W.printHex("Size", MOSection.Size);
W.printNumber("Offset", Section.Offset); W.printNumber("Offset", MOSection.Offset);
W.printNumber("Alignment", Section.Alignment); W.printNumber("Alignment", MOSection.Alignment);
W.printHex ("RelocationOffset", Section.RelocationTableOffset); W.printHex("RelocationOffset", MOSection.RelocationTableOffset);
W.printNumber("RelocationCount", Section.NumRelocationTableEntries); W.printNumber("RelocationCount", MOSection.NumRelocationTableEntries);
W.printEnum ("Type", Section.Flags & 0xFF, W.printEnum("Type", MOSection.Flags & 0xFF,
makeArrayRef(MachOSectionAttributes)); makeArrayRef(MachOSectionAttributes));
W.printFlags ("Attributes", Section.Flags >> 8, W.printFlags("Attributes", MOSection.Flags >> 8,
makeArrayRef(MachOSectionAttributes)); makeArrayRef(MachOSectionAttributes));
W.printHex ("Reserved1", Section.Reserved1); W.printHex("Reserved1", MOSection.Reserved1);
W.printHex ("Reserved2", Section.Reserved2); W.printHex("Reserved2", MOSection.Reserved2);
if (opts::SectionRelocations) { if (opts::SectionRelocations) {
ListScope D(W, "Relocations"); ListScope D(W, "Relocations");
for (relocation_iterator RelI = SecI->relocation_begin(), for (relocation_iterator RelI = Section.relocation_begin(),
RelE = SecI->relocation_end(); RelE = Section.relocation_end();
RelI != RelE; ++RelI) RelI != RelE; ++RelI)
printRelocation(SecI, RelI); printRelocation(RelI);
} }
if (opts::SectionSymbols) { if (opts::SectionSymbols) {
ListScope D(W, "Symbols"); ListScope D(W, "Symbols");
for (symbol_iterator SymI = Obj->symbol_begin(), for (symbol_iterator SymI = Obj->symbol_begin(), SymE = Obj->symbol_end();
SymE = Obj->symbol_end();
SymI != SymE; ++SymI) { SymI != SymE; ++SymI) {
bool Contained = false; bool Contained = false;
if (SecI->containsSymbol(*SymI, Contained) || !Contained) if (Section.containsSymbol(*SymI, Contained) || !Contained)
continue; continue;
printSymbol(SymI); printSymbol(SymI);
@ -273,7 +269,8 @@ void MachODumper::printSections(const MachOObjectFile *Obj) {
if (opts::SectionData) { if (opts::SectionData) {
StringRef Data; StringRef Data;
if (error(SecI->getContents(Data))) break; if (error(Section.getContents(Data)))
break;
W.printBinaryBlock("SectionData", Data); W.printBinaryBlock("SectionData", Data);
} }
@ -284,16 +281,14 @@ void MachODumper::printRelocations() {
ListScope D(W, "Relocations"); ListScope D(W, "Relocations");
error_code EC; error_code EC;
for (section_iterator SecI = Obj->section_begin(), for (const SectionRef &Section : Obj->sections()) {
SecE = Obj->section_end();
SecI != SecE; ++SecI) {
StringRef Name; StringRef Name;
if (error(SecI->getName(Name))) if (error(Section.getName(Name)))
continue; continue;
bool PrintedGroup = false; bool PrintedGroup = false;
for (relocation_iterator RelI = SecI->relocation_begin(), for (relocation_iterator RelI = Section.relocation_begin(),
RelE = SecI->relocation_end(); RelE = Section.relocation_end();
RelI != RelE; ++RelI) { RelI != RelE; ++RelI) {
if (!PrintedGroup) { if (!PrintedGroup) {
W.startLine() << "Section " << Name << " {\n"; W.startLine() << "Section " << Name << " {\n";
@ -301,7 +296,7 @@ void MachODumper::printRelocations() {
PrintedGroup = true; PrintedGroup = true;
} }
printRelocation(SecI, RelI); printRelocation(RelI);
} }
if (PrintedGroup) { if (PrintedGroup) {
@ -311,13 +306,11 @@ void MachODumper::printRelocations() {
} }
} }
void MachODumper::printRelocation(section_iterator SecI, void MachODumper::printRelocation(relocation_iterator RelI) {
relocation_iterator RelI) { return printRelocation(Obj, RelI);
return printRelocation(Obj, SecI, RelI);
} }
void MachODumper::printRelocation(const MachOObjectFile *Obj, void MachODumper::printRelocation(const MachOObjectFile *Obj,
section_iterator SecI,
relocation_iterator RelI) { relocation_iterator RelI) {
uint64_t Offset; uint64_t Offset;
SmallString<32> RelocName; SmallString<32> RelocName;

View File

@ -85,10 +85,10 @@ static size_t getNumLengthAsString(uint64_t num) {
return result.size(); return result.size();
} }
/// @brief Print the size of each section in @p o. /// @brief Print the size of each section in @p Obj.
/// ///
/// The format used is determined by @c OutputFormat and @c Radix. /// The format used is determined by @c OutputFormat and @c Radix.
static void PrintObjectSectionSizes(ObjectFile *o) { static void PrintObjectSectionSizes(ObjectFile *Obj) {
uint64_t total = 0; uint64_t total = 0;
std::string fmtbuf; std::string fmtbuf;
raw_string_ostream fmt(fmtbuf); raw_string_ostream fmt(fmtbuf);
@ -111,17 +111,18 @@ static void PrintObjectSectionSizes(ObjectFile *o) {
std::size_t max_name_len = strlen("section"); std::size_t max_name_len = strlen("section");
std::size_t max_size_len = strlen("size"); std::size_t max_size_len = strlen("size");
std::size_t max_addr_len = strlen("addr"); std::size_t max_addr_len = strlen("addr");
for (section_iterator i = o->section_begin(), e = o->section_end(); for (const SectionRef &Section : Obj->sections()) {
i != e; ++i) {
uint64_t size = 0; uint64_t size = 0;
if (error(i->getSize(size))) if (error(Section.getSize(size)))
return; return;
total += size; total += size;
StringRef name; StringRef name;
uint64_t addr = 0; uint64_t addr = 0;
if (error(i->getName(name))) return; if (error(Section.getName(name)))
if (error(i->getAddress(addr))) return; return;
if (error(Section.getAddress(addr)))
return;
max_name_len = std::max(max_name_len, name.size()); max_name_len = std::max(max_name_len, name.size());
max_size_len = std::max(max_size_len, getNumLengthAsString(size)); max_size_len = std::max(max_size_len, getNumLengthAsString(size));
max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr)); max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
@ -150,20 +151,19 @@ static void PrintObjectSectionSizes(ObjectFile *o) {
<< "%#" << max_addr_len << radix_fmt << "\n"; << "%#" << max_addr_len << radix_fmt << "\n";
// Print each section. // Print each section.
for (section_iterator i = o->section_begin(), e = o->section_end(); for (const SectionRef &Section : Obj->sections()) {
i != e; ++i) {
StringRef name; StringRef name;
uint64_t size = 0; uint64_t size = 0;
uint64_t addr = 0; uint64_t addr = 0;
if (error(i->getName(name))) return; if (error(Section.getName(name)))
if (error(i->getSize(size))) return; return;
if (error(i->getAddress(addr))) return; if (error(Section.getSize(size)))
return;
if (error(Section.getAddress(addr)))
return;
std::string namestr = name; std::string namestr = name;
outs() << format(fmt.str().c_str(), outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
namestr.c_str(),
size,
addr);
} }
// Print total. // Print total.
@ -181,16 +181,19 @@ static void PrintObjectSectionSizes(ObjectFile *o) {
uint64_t total_bss = 0; uint64_t total_bss = 0;
// Make one pass over the section table to calculate sizes. // Make one pass over the section table to calculate sizes.
for (section_iterator i = o->section_begin(), e = o->section_end(); for (const SectionRef &Section : Obj->sections()) {
i != e; ++i) {
uint64_t size = 0; uint64_t size = 0;
bool isText = false; bool isText = false;
bool isData = false; bool isData = false;
bool isBSS = false; bool isBSS = false;
if (error(i->getSize(size))) return; if (error(Section.getSize(size)))
if (error(i->isText(isText))) return; return;
if (error(i->isData(isData))) return; if (error(Section.isText(isText)))
if (error(i->isBSS(isBSS))) return; return;
if (error(Section.isData(isData)))
return;
if (error(Section.isBSS(isBSS)))
return;
if (isText) if (isText)
total_text += size; total_text += size;
else if (isData) else if (isData)

View File

@ -279,14 +279,13 @@ static bool getGNUDebuglinkContents(const Binary *Bin, std::string &DebugName,
const ObjectFile *Obj = dyn_cast<ObjectFile>(Bin); const ObjectFile *Obj = dyn_cast<ObjectFile>(Bin);
if (!Obj) if (!Obj)
return false; return false;
for (section_iterator I = Obj->section_begin(), E = Obj->section_end(); for (const SectionRef &Section : Obj->sections()) {
I != E; ++I) {
StringRef Name; StringRef Name;
I->getName(Name); Section.getName(Name);
Name = Name.substr(Name.find_first_not_of("._")); Name = Name.substr(Name.find_first_not_of("._"));
if (Name == "gnu_debuglink") { if (Name == "gnu_debuglink") {
StringRef Data; StringRef Data;
I->getContents(Data); Section.getContents(Data);
DataExtractor DE(Data, Obj->isLittleEndian(), 0); DataExtractor DE(Data, Obj->isLittleEndian(), 0);
uint32_t Offset = 0; uint32_t Offset = 0;
if (const char *DebugNameStr = DE.getCStr(&Offset)) { if (const char *DebugNameStr = DE.getCStr(&Offset)) {