[llvm-cxxdump] Don't rely on global state

Differential Revision:	http://reviews.llvm.org/D11227


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@242509 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Davide Italiano 2015-07-17 06:18:36 +00:00
parent b974e0babe
commit ac018543f8

View File

@ -40,18 +40,14 @@ cl::list<std::string> InputFilenames(cl::Positional,
cl::ZeroOrMore); cl::ZeroOrMore);
} // namespace opts } // namespace opts
static int ReturnValue = EXIT_SUCCESS;
namespace llvm { namespace llvm {
static bool error(std::error_code EC) { static void error(std::error_code EC) {
if (!EC) if (!EC)
return false; return;
ReturnValue = EXIT_FAILURE;
outs() << "\nError reading file: " << EC.message() << ".\n"; outs() << "\nError reading file: " << EC.message() << ".\n";
outs().flush(); outs().flush();
return true; exit(1);
} }
} // namespace llvm } // namespace llvm
@ -59,10 +55,9 @@ static bool error(std::error_code EC) {
static void reportError(StringRef Input, StringRef Message) { static void reportError(StringRef Input, StringRef Message) {
if (Input == "-") if (Input == "-")
Input = "<stdin>"; Input = "<stdin>";
errs() << Input << ": " << Message << "\n"; errs() << Input << ": " << Message << "\n";
errs().flush(); errs().flush();
ReturnValue = EXIT_FAILURE; exit(1);
} }
static void reportError(StringRef Input, std::error_code EC) { static void reportError(StringRef Input, std::error_code EC) {
@ -84,7 +79,7 @@ static SmallVectorImpl<SectionRef> &getRelocSections(const ObjectFile *Obj,
return SectionRelocMap[Sec]; return SectionRelocMap[Sec];
} }
static bool collectRelocatedSymbols(const ObjectFile *Obj, static void collectRelocatedSymbols(const ObjectFile *Obj,
const SectionRef &Sec, uint64_t SecAddress, const SectionRef &Sec, uint64_t SecAddress,
uint64_t SymAddress, uint64_t SymSize, uint64_t SymAddress, uint64_t SymSize,
StringRef *I, StringRef *E) { StringRef *I, StringRef *E) {
@ -98,8 +93,7 @@ static bool collectRelocatedSymbols(const ObjectFile *Obj,
if (RelocSymI == Obj->symbol_end()) if (RelocSymI == Obj->symbol_end())
continue; continue;
ErrorOr<StringRef> RelocSymName = RelocSymI->getName(); ErrorOr<StringRef> RelocSymName = RelocSymI->getName();
if (error(RelocSymName.getError())) error(RelocSymName.getError());
return true;
uint64_t Offset = Reloc.getOffset(); uint64_t Offset = Reloc.getOffset();
if (Offset >= SymOffset && Offset < SymEnd) { if (Offset >= SymOffset && Offset < SymEnd) {
*I = *RelocSymName; *I = *RelocSymName;
@ -107,10 +101,9 @@ static bool collectRelocatedSymbols(const ObjectFile *Obj,
} }
} }
} }
return false;
} }
static bool collectRelocationOffsets( static void collectRelocationOffsets(
const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress, const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
uint64_t SymAddress, uint64_t SymSize, StringRef SymName, uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) { std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
@ -122,14 +115,12 @@ static bool collectRelocationOffsets(
if (RelocSymI == Obj->symbol_end()) if (RelocSymI == Obj->symbol_end())
continue; continue;
ErrorOr<StringRef> RelocSymName = RelocSymI->getName(); ErrorOr<StringRef> RelocSymName = RelocSymI->getName();
if (error(RelocSymName.getError())) error(RelocSymName.getError());
return true;
uint64_t Offset = Reloc.getOffset(); uint64_t Offset = Reloc.getOffset();
if (Offset >= SymOffset && Offset < SymEnd) if (Offset >= SymOffset && Offset < SymEnd)
Collection[std::make_pair(SymName, Offset - SymOffset)] = *RelocSymName; Collection[std::make_pair(SymName, Offset - SymOffset)] = *RelocSymName;
} }
} }
return false;
} }
static void dumpCXXData(const ObjectFile *Obj) { static void dumpCXXData(const ObjectFile *Obj) {
@ -191,12 +182,10 @@ static void dumpCXXData(const ObjectFile *Obj) {
object::SymbolRef Sym = P.first; object::SymbolRef Sym = P.first;
uint64_t SymSize = P.second; uint64_t SymSize = P.second;
ErrorOr<StringRef> SymNameOrErr = Sym.getName(); ErrorOr<StringRef> SymNameOrErr = Sym.getName();
if (error(SymNameOrErr.getError())) error(SymNameOrErr.getError());
return;
StringRef SymName = *SymNameOrErr; StringRef SymName = *SymNameOrErr;
object::section_iterator SecI(Obj->section_begin()); object::section_iterator SecI(Obj->section_begin());
if (error(Sym.getSection(SecI))) error(Sym.getSection(SecI));
return;
// Skip external symbols. // Skip external symbols.
if (SecI == Obj->section_end()) if (SecI == Obj->section_end())
continue; continue;
@ -205,11 +194,9 @@ static void dumpCXXData(const ObjectFile *Obj) {
if (Sec.isBSS() || Sec.isVirtual()) if (Sec.isBSS() || Sec.isVirtual())
continue; continue;
StringRef SecContents; StringRef SecContents;
if (error(Sec.getContents(SecContents))) error(Sec.getContents(SecContents));
return;
ErrorOr<uint64_t> SymAddressOrErr = Sym.getAddress(); ErrorOr<uint64_t> SymAddressOrErr = Sym.getAddress();
if (error(SymAddressOrErr.getError())) error(SymAddressOrErr.getError());
return;
uint64_t SymAddress = *SymAddressOrErr; uint64_t SymAddress = *SymAddressOrErr;
uint64_t SecAddress = Sec.getAddress(); uint64_t SecAddress = Sec.getAddress();
uint64_t SecSize = Sec.getSize(); uint64_t SecSize = Sec.getSize();
@ -239,9 +226,7 @@ static void dumpCXXData(const ObjectFile *Obj) {
COL.Data = ArrayRef<little32_t>( COL.Data = ArrayRef<little32_t>(
reinterpret_cast<const little32_t *>(SymContents.data()), 3); reinterpret_cast<const little32_t *>(SymContents.data()), 3);
StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols); StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
E))
return;
COLs[SymName] = COL; COLs[SymName] = COL;
} }
// Class hierarchy descriptors in the MS-ABI start with '??_R3' // Class hierarchy descriptors in the MS-ABI start with '??_R3'
@ -250,9 +235,7 @@ static void dumpCXXData(const ObjectFile *Obj) {
CHD.Data = ArrayRef<little32_t>( CHD.Data = ArrayRef<little32_t>(
reinterpret_cast<const little32_t *>(SymContents.data()), 3); reinterpret_cast<const little32_t *>(SymContents.data()), 3);
StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols); StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
E))
return;
CHDs[SymName] = CHD; CHDs[SymName] = CHD;
} }
// Class hierarchy descriptors in the MS-ABI start with '??_R2' // Class hierarchy descriptors in the MS-ABI start with '??_R2'
@ -268,9 +251,7 @@ static void dumpCXXData(const ObjectFile *Obj) {
BCD.Data = ArrayRef<little32_t>( BCD.Data = ArrayRef<little32_t>(
reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5); reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols); StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
E))
return;
BCDs[SymName] = BCD; BCDs[SymName] = BCD;
} }
// Type descriptors in the MS-ABI start with '??_R0' // Type descriptors in the MS-ABI start with '??_R0'
@ -283,9 +264,7 @@ static void dumpCXXData(const ObjectFile *Obj) {
TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr); TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
TD.MangledName = SymContents.drop_front(BytesInAddress * 2); TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols); StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
E))
return;
TDs[SymName] = TD; TDs[SymName] = TD;
} }
// Throw descriptors in the MS-ABI start with '_TI' // Throw descriptors in the MS-ABI start with '_TI'
@ -316,9 +295,7 @@ static void dumpCXXData(const ObjectFile *Obj) {
CT.VirtualBaseAdjustmentOffset = DataPtr[4]; CT.VirtualBaseAdjustmentOffset = DataPtr[4];
CT.Size = DataPtr[5]; CT.Size = DataPtr[5];
StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols); StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
E))
return;
CTs[SymName] = CT; CTs[SymName] = CT;
} }
// Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'. // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
@ -569,5 +546,5 @@ int main(int argc, const char *argv[]) {
std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(), std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
dumpInput); dumpInput);
return ReturnValue; return EXIT_SUCCESS;
} }