Skip to content

Commit 274457e

Browse files
author
Hemant Kulkarni
committed
[llvm-size] Implement --common option
Differential Revision: http://reviews.llvm.org/D16820 llvm-svn: 264591
1 parent 088a726 commit 274457e

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// #Check that with common switch commons are added to bss or
2+
// #Shown as *COM* otherwise their size is discounted
3+
// RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
4+
// RUN: llvm-size -A -common %t.o | FileCheck --check-prefix="SYSV" %s
5+
// RUN: llvm-size -B -common %t.o| FileCheck --check-prefix="BSD" %s
6+
// RUN: llvm-size -A %t.o | FileCheck --check-prefix="SYSVNOCOMM" %s
7+
// RUN: llvm-size -B %t.o| FileCheck --check-prefix="BSDNOCOMM" %s
8+
.type x,@object
9+
.comm x,4,4
10+
.type y,@object
11+
.comm y,4,4
12+
.type z,@object
13+
.comm z,4,4
14+
// SYSV: {{[ -\(\)_A-Za-z0-9.\\/:]+}} :
15+
// SYSV-NEXT: section size addr
16+
// SYSV-NEXT: .text 0 0
17+
// SYSV-NEXT: *COM* 12 0
18+
// SYSV-NEXT: Total 12
19+
20+
// SYSVNOCOMM: {{[ -\(\)_A-Za-z0-9.\\/:]+}} :
21+
// SYSVNOCOMM-NEXT: section size addr
22+
// SYSVNOCOMM-NEXT: .text 0 0
23+
// SYSVNOCOMM-NEXT: Total 0
24+
25+
// BSD: text data bss dec hex filename
26+
// BSD-NEXT: 0 0 12 12 c {{[ -\(\)_A-Za-z0-9.\\/:]+}}
27+
28+
// BSDNOCOMM: text data bss dec hex filename
29+
// BSDNOCOMM-NEXT: 0 0 0 0 0 {{[ -\(\)_A-Za-z0-9.\\/:]+}}

llvm/tools/llvm-size/llvm-size.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ cl::opt<bool>
5757
DarwinLongFormat("l", cl::desc("When format is darwin, use long format "
5858
"to include addresses and offsets."));
5959

60+
cl::opt<bool>
61+
ELFCommons("common",
62+
cl::desc("Print common symbols in the ELF file. When using "
63+
"Berkely format, this is added to bss."),
64+
cl::init(false));
65+
6066
static cl::list<std::string>
6167
ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
6268
cl::ZeroOrMore);
@@ -127,6 +133,15 @@ static bool considerForSize(ObjectFile *Obj, SectionRef Section) {
127133
return true;
128134
}
129135

136+
/// Total size of all ELF common symbols
137+
static uint64_t getCommonSize(ObjectFile *Obj) {
138+
uint64_t TotalCommons = 0;
139+
for (auto &Sym : Obj->symbols())
140+
if (Obj->getSymbolFlags(Sym.getRawDataRefImpl()) & SymbolRef::SF_Common)
141+
TotalCommons += Obj->getCommonSymbolSize(Sym.getRawDataRefImpl());
142+
return TotalCommons;
143+
}
144+
130145
/// Print the size of each Mach-O segment and section in @p MachO.
131146
///
132147
/// This is when used when @c OutputFormat is darwin and produces the same
@@ -352,6 +367,13 @@ static void printObjectSectionSizes(ObjectFile *Obj) {
352367
outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
353368
}
354369

370+
if (ELFCommons) {
371+
uint64_t CommonSize = getCommonSize(Obj);
372+
total += CommonSize;
373+
outs() << format(fmt.str().c_str(), std::string("*COM*").c_str(),
374+
CommonSize, static_cast<uint64_t>(0));
375+
}
376+
355377
// Print total.
356378
fmtbuf.clear();
357379
fmt << "%-" << max_name_len << "s "
@@ -379,6 +401,9 @@ static void printObjectSectionSizes(ObjectFile *Obj) {
379401
total_bss += size;
380402
}
381403

404+
if (ELFCommons)
405+
total_bss += getCommonSize(Obj);
406+
382407
total = total_text + total_data + total_bss;
383408

384409
if (!BerkeleyHeaderPrinted) {

0 commit comments

Comments
 (0)