[PATCH] D16816: Calculate correct section sizes for llvm-size

khemant@codeaurora.org via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 2 11:23:30 PST 2016


khemant created this revision.
khemant added reviewers: llvm-commits, davide.
khemant added subscribers: llvm-commits, davide, echristo.
khemant set the repository for this revision to rL LLVM.

llvm-size adds sections such as symbol table etc. into size totals. This is incorrect.  BSD utilty does not:
$llvm-size -A a.o
a.o  :
section             size   addr
.text                 77      0
.data                  0      0
.bss                   0      0
.comment              44      0
.note.GNU-stack        0      0
.eh_frame            248      0
.rela.eh_frame       168      0
.shstrtab             84      0
.symtab              432      0
.strtab               52      0
Total               1105

$size -A a.o
a.o  :
section           size   addr
.text               77      0
.data                0      0
.bss                 0      0
.comment            44      0
.note.GNU-stack      0      0
.eh_frame          248      0
Total              369



Repository:
  rL LLVM

http://reviews.llvm.org/D16816

Files:
  test/tools/llvm-size/Inputs/1.o
  test/tools/llvm-size/basic.test
  tools/llvm-size/llvm-size.cpp

Index: tools/llvm-size/llvm-size.cpp
===================================================================
--- tools/llvm-size/llvm-size.cpp
+++ tools/llvm-size/llvm-size.cpp
@@ -18,8 +18,10 @@
 #include "llvm/Object/MachO.h"
 #include "llvm/Object/MachOUniversal.h"
 #include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/ELFObjectFile.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/ELF.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/ManagedStatic.h"
@@ -111,6 +113,19 @@
   return nullptr;
 }
 
+/// @brief Remove unneeded ELF sections from calculation
+static bool ConsiderForSize(ObjectFile *Obj, SectionRef Section) {
+  if (Obj->isELF()) {
+    auto ElfSection = static_cast<ELFSectionRef>(Section);
+    if (ElfSection.getType() == ELF::SHT_NULL ||
+        ElfSection.getType() == ELF::SHT_SYMTAB ||
+        ElfSection.getType() == ELF::SHT_STRTAB ||
+        ElfSection.getType() == ELF::SHT_REL ||
+        ElfSection.getType() == ELF::SHT_RELA)
+      return false;
+  }
+  return true;
+}
 /// @brief Print the size of each Mach-O segment and section in @p MachO.
 ///
 /// This is when used when @c OutputFormat is darwin and produces the same
@@ -285,6 +300,8 @@
     std::size_t max_size_len = strlen("size");
     std::size_t max_addr_len = strlen("addr");
     for (const SectionRef &Section : Obj->sections()) {
+      if (!ConsiderForSize(Obj, Section))
+        continue;
       uint64_t size = Section.getSize();
       total += size;
 
@@ -320,6 +337,8 @@
 
     // Print each section.
     for (const SectionRef &Section : Obj->sections()) {
+      if (!ConsiderForSize(Obj, Section))
+        continue;
       StringRef name;
       if (error(Section.getName(name)))
         return;
Index: test/tools/llvm-size/basic.test
===================================================================
--- test/tools/llvm-size/basic.test
+++ test/tools/llvm-size/basic.test
@@ -1,2 +1,28 @@
 RUN: llvm-size %t.blah 2>&1 | FileCheck --check-prefix=ENOENT %s
+#1.o source
+#int x ; int y ; int z;
+#
+#int main () {
+#  x = 1;
+#  y = 1;
+#  z = 1;
+#  return x + y + z;
+#}
+#Compilation: clang -c -O0
+RUN: llvm-size -A %p/Inputs/1.o | FileCheck --check-prefix="SYSV" %s
+RUN: llvm-size -B %p/Inputs/1.o | FileCheck --check-prefix="BSD" %s
+
 ENOENT: {{.*}}llvm-size{{(\.EXE|\.exe)?}}: {{.*}}.blah: {{[Nn]}}o such file or directory
+SYSV:    {{[ -\(\)_A-Za-z0-9.\\/:]+}}  :
+SYSV-NEXT:    section             size   addr
+SYSV-NEXT:    .text                 67      0
+SYSV-NEXT:    .data                  0      0
+SYSV-NEXT:    .bss                   0      0
+SYSV-NEXT:    .comment              46      0
+SYSV-NEXT:    .note.GNU-stack        0      0
+SYSV-NEXT:    .eh_frame             56      0
+SYSV-NEXT:    Total                169
+
+BSD:        text    data     bss     dec     hex filename
+BSD-NEXT:     67      56       0     123      7b {{[ -\(\)_A-Za-z0-9.\\/:]+}}
+


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16816.46683.patch
Type: text/x-patch
Size: 3007 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160202/9dfee9bd/attachment.bin>


More information about the llvm-commits mailing list