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.\\/:]+}} 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 +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 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(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) {