diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2012-12-31 16:29:44 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2012-12-31 16:29:44 +0000 |
commit | fc73847d8fa79d310b26b7a80275dc48755ec2e3 (patch) | |
tree | fddcfd7a29cfd445c6fe9e86042880123d479c7e /tools/llvm-readobj | |
parent | 41be2fb1f9e9b8f796effb81c2bee6cf397136cf (diff) |
Dump sections. Extracted from a patch by Sami Liedes.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171304 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/llvm-readobj')
-rw-r--r-- | tools/llvm-readobj/llvm-readobj.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/tools/llvm-readobj/llvm-readobj.cpp b/tools/llvm-readobj/llvm-readobj.cpp index 81d2bfb730..27b5c53d2e 100644 --- a/tools/llvm-readobj/llvm-readobj.cpp +++ b/tools/llvm-readobj/llvm-readobj.cpp @@ -43,6 +43,15 @@ static void dumpSymbolHeader() { << "\n"; } +static void dumpSectionHeader() { + outs() << format(" %-24s", (const char*)"Name") + << format(" %-16s", (const char*)"Address") + << format(" %-16s", (const char*)"Size") + << format(" %-8s", (const char*)"Align") + << format(" %-26s", (const char*)"Flags") + << "\n"; +} + static const char *getTypeStr(SymbolRef::Type Type) { switch (Type) { case SymbolRef::ST_Unknown: return "?"; @@ -84,6 +93,36 @@ static void checkError(error_code ec, const char *msg) { report_fatal_error(std::string(msg) + ": " + ec.message()); } +static std::string getSectionFlagStr(const SectionRef &Section) { + const struct { + error_code (SectionRef::*MemF)(bool &) const; + const char *FlagStr, *ErrorStr; + } Work[] = + {{ &SectionRef::isText, "text,", "Section.isText() failed" }, + { &SectionRef::isData, "data,", "Section.isData() failed" }, + { &SectionRef::isBSS, "bss,", "Section.isBSS() failed" }, + { &SectionRef::isRequiredForExecution, "required,", + "Section.isRequiredForExecution() failed" }, + { &SectionRef::isVirtual, "virtual,", "Section.isVirtual() failed" }, + { &SectionRef::isZeroInit, "zeroinit,", "Section.isZeroInit() failed" }, + { &SectionRef::isReadOnlyData, "rodata,", + "Section.isReadOnlyData() failed" }}; + + std::string result; + for (uint32_t I = 0; I < sizeof(Work)/sizeof(*Work); ++I) { + bool B; + checkError((Section.*Work[I].MemF)(B), Work[I].ErrorStr); + if (B) + result += Work[I].FlagStr; + } + + // Remove trailing comma + if (result.size() > 0) { + result.erase(result.size() - 1); + } + return result; +} + static void dumpSymbol(const SymbolRef &Sym, const ObjectFile *obj, bool IsDynamic) { StringRef Name; @@ -159,12 +198,43 @@ static void dumpDynamicSymbols(const ObjectFile *obj) { outs() << " Total: " << count << "\n\n"; } +static void dumpSection(const SectionRef &Section, const ObjectFile *obj) { + StringRef Name; + checkError(Section.getName(Name), "SectionRef::getName() failed"); + uint64_t Addr, Size, Align; + checkError(Section.getAddress(Addr), "SectionRef::getAddress() failed"); + checkError(Section.getSize(Size), "SectionRef::getSize() failed"); + checkError(Section.getAlignment(Align), "SectionRef::getAlignment() failed"); + outs() << format(" %-24s", std::string(Name).c_str()) + << format(" %16" PRIx64, Addr) + << format(" %16" PRIx64, Size) + << format(" %8" PRIx64, Align) + << " " << getSectionFlagStr(Section) + << "\n"; +} + static void dumpLibrary(const LibraryRef &lib) { StringRef path; lib.getPath(path); outs() << " " << path << "\n"; } +template<typename Iterator, typename Func> +static void dump(const ObjectFile *obj, Func f, Iterator begin, Iterator end, + const char *errStr) { + error_code ec; + uint32_t count = 0; + Iterator it = begin, ie = end; + while (it != ie) { + f(*it, obj); + it.increment(ec); + if (ec) + report_fatal_error(errStr); + ++count; + } + outs() << " Total: " << count << "\n\n"; +} + // Iterate through needed libraries static void dumpLibrariesNeeded(const ObjectFile *obj) { error_code ec; @@ -220,6 +290,12 @@ int main(int argc, char** argv) { dumpHeaders(obj); dumpSymbols(obj); dumpDynamicSymbols(obj); + + outs() << "Sections:\n"; + dumpSectionHeader(); + dump(obj, &dumpSection, obj->begin_sections(), obj->end_sections(), + "Section iteration failed"); + dumpLibrariesNeeded(obj); return 0; } |