[PATCH] D16820: Implement common symbol size calculation in llvm-size

khemant@codeaurora.org via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 2 13:19:49 PST 2016


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

Commons do not live in any sections. Their sizes need to be added to total size. This option adds them to total size. In Berkely format, this is shown in bss size. In Sys V format a new row named "*COM*" is added that shows the common totals. Both format will have this added to the grand total if the option is set.

This is similar to GNU size binutils tool.

Repository:
  rL LLVM

http://reviews.llvm.org/D16820

Files:
  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
@@ -58,6 +58,10 @@
 DarwinLongFormat("l", cl::desc("When format is darwin, use long format "
                                "to include addresses and offsets."));
 
+cl::opt<bool>
+ELFCommons("common", cl::desc("Print common symbols in the ELF file. \
+When using Berkely format, this is added to bss."), cl::init(false));
+
 static cl::list<std::string>
 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
           cl::ZeroOrMore);
@@ -127,6 +131,16 @@
   }
   return true;
 }
+
+/// @brief Returns total size of all ELF common symbols
+static uint64_t getCommonSize(ObjectFile *Obj) {
+  uint64_t total_commons = 0;
+  for (auto &Sym : Obj->symbols())
+    if (Obj->getSymbolFlags(Sym.getRawDataRefImpl()) & SymbolRef::SF_Common)
+      total_commons += Obj->getCommonSymbolSize(Sym.getRawDataRefImpl());
+  return total_commons;
+}
+
 /// @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
@@ -350,6 +364,12 @@
       outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
     }
 
+    if (ELFCommons) {
+      uint64_t common_size = getCommonSize(Obj);
+      total += common_size;
+      outs() << format(fmt.str().c_str(), std::string("*COM*").c_str(), common_size, static_cast<uint64_t>(0));
+    }
+
     // Print total.
     fmtbuf.clear();
     fmt << "%-" << max_name_len << "s "
@@ -377,6 +397,9 @@
         total_bss += size;
     }
 
+    if (ELFCommons)
+      total_bss += getCommonSize(Obj);
+
     total = total_text + total_data + total_bss;
 
     if (!berkeleyHeaderPrinted) {
Index: test/tools/llvm-size/basic.test
===================================================================
--- test/tools/llvm-size/basic.test
+++ test/tools/llvm-size/basic.test
@@ -11,6 +11,8 @@
 #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
+RUN: llvm-size -A -common %p/Inputs/1.o | FileCheck --check-prefix="SYSVCOMM" %s
+RUN: llvm-size -B -common %p/Inputs/1.o | FileCheck --check-prefix="BSDCOMM" %s
 
 ENOENT: {{.*}}llvm-size{{(\.EXE|\.exe)?}}: {{.*}}.blah: {{[Nn]}}o such file or directory
 SYSV:    {{[ -\(\)_A-Za-z0-9.\\/:]+}}  :
@@ -26,3 +28,16 @@
 BSD:        text    data     bss     dec     hex filename
 BSD-NEXT:     67      56       0     123      7b {{[ -\(\)_A-Za-z0-9.\\/:]+}}
 
+SYSVCOMM:    {{[ -\(\)_A-Za-z0-9.\\/:]+}}  :
+SYSVCOMM-NEXT:    section             size   addr
+SYSVCOMM-NEXT:    .text                 67      0
+SYSVCOMM-NEXT:    .data                  0      0
+SYSVCOMM-NEXT:    .bss                   0      0
+SYSVCOMM-NEXT:    .comment              46      0
+SYSVCOMM-NEXT:    .note.GNU-stack        0      0
+SYSVCOMM-NEXT:    .eh_frame             56      0
+SYSVCOMM-NEXT:    *COM*                 12      0
+SYSVCOMM-NEXT:    Total                181
+
+BSDCOMM:        text    data     bss     dec     hex filename
+BSDCOMM-NEXT:     67      56      12     135      87 {{[ -\(\)_A-Za-z0-9.\\/:]+}}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16820.46691.patch
Type: text/x-patch
Size: 3275 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160202/3100c51a/attachment.bin>


More information about the llvm-commits mailing list