[llvm-commits] [llvm] r171304 - in /llvm/trunk: test/Object/readobj-shared-object.test tools/llvm-readobj/llvm-readobj.cpp

Rafael Espindola rafael.espindola at gmail.com
Mon Dec 31 08:29:44 PST 2012


Author: rafael
Date: Mon Dec 31 10:29:44 2012
New Revision: 171304

URL: http://llvm.org/viewvc/llvm-project?rev=171304&view=rev
Log:
Dump sections. Extracted from a patch by Sami Liedes.

Modified:
    llvm/trunk/test/Object/readobj-shared-object.test
    llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp

Modified: llvm/trunk/test/Object/readobj-shared-object.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/readobj-shared-object.test?rev=171304&r1=171303&r2=171304&view=diff
==============================================================================
--- llvm/trunk/test/Object/readobj-shared-object.test (original)
+++ llvm/trunk/test/Object/readobj-shared-object.test Mon Dec 31 10:29:44 2012
@@ -53,6 +53,24 @@
 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

Modified: llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp?rev=171304&r1=171303&r2=171304&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp Mon Dec 31 10:29:44 2012
@@ -43,6 +43,15 @@
          << "\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 @@
     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 @@
   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 @@
   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;
 }





More information about the llvm-commits mailing list