aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafael Espindola <rafael.espindola@gmail.com>2012-12-31 16:29:44 +0000
committerRafael Espindola <rafael.espindola@gmail.com>2012-12-31 16:29:44 +0000
commitfc73847d8fa79d310b26b7a80275dc48755ec2e3 (patch)
treefddcfd7a29cfd445c6fe9e86042880123d479c7e
parent41be2fb1f9e9b8f796effb81c2bee6cf397136cf (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
-rw-r--r--test/Object/readobj-shared-object.test18
-rw-r--r--tools/llvm-readobj/llvm-readobj.cpp76
2 files changed, 94 insertions, 0 deletions
diff --git a/test/Object/readobj-shared-object.test b/test/Object/readobj-shared-object.test
index 8d9b179002..c1dd15b1f9 100644
--- a/test/Object/readobj-shared-object.test
+++ b/test/Object/readobj-shared-object.test
@@ -53,6 +53,24 @@ ELF: global_func FUNC {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-
ELF: _edata ? {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} global,absolute
ELF: Total: {{[0-9a-f]+}}
+ELF:Sections:
+ELF: Name Address Size Align Flags
+ELF: {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} rodata
+ELF: .hash {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} required,rodata
+ELF: .dynsym {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} required,rodata
+ELF: .dynstr {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} required,rodata
+ELF: .text {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} text,{{(data,)?}}required
+ELF: .eh_frame {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} data,required,rodata
+ELF: .tdata {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} data,required
+ELF: .dynamic {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} required
+ELF: .got.plt {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} data,required
+ELF: .data {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} data,required
+ELF: .bss {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} {{[a-z,]*}}
+ELF: .shstrtab {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} rodata
+ELF: .symtab {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} rodata
+ELF: .strtab {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} rodata
+ELF: Total: 14
+
ELF:Libraries needed:
ELF: libc.so.6
ELF: libm.so.6
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;
}