summaryrefslogtreecommitdiffstats
path: root/llvm
diff options
context:
space:
mode:
Diffstat (limited to 'llvm')
-rw-r--r--llvm/include/llvm/Object/ELFObjectFile.h16
-rw-r--r--llvm/include/llvm/Object/ObjectFile.h22
-rw-r--r--llvm/lib/Object/ObjectFile.cpp8
-rw-r--r--llvm/test/tools/llvm-size/X86/elf-sizes.test55
-rw-r--r--llvm/test/tools/llvm-size/X86/ignore-sections.s4
-rw-r--r--llvm/tools/llvm-size/llvm-size.cpp4
6 files changed, 105 insertions, 4 deletions
diff --git a/llvm/include/llvm/Object/ELFObjectFile.h b/llvm/include/llvm/Object/ELFObjectFile.h
index f8dab211147..0f620681cd9 100644
--- a/llvm/include/llvm/Object/ELFObjectFile.h
+++ b/llvm/include/llvm/Object/ELFObjectFile.h
@@ -260,6 +260,8 @@ protected:
bool isSectionData(DataRefImpl Sec) const override;
bool isSectionBSS(DataRefImpl Sec) const override;
bool isSectionVirtual(DataRefImpl Sec) const override;
+ bool isBerkeleyText(DataRefImpl Sec) const override;
+ bool isBerkeleyData(DataRefImpl Sec) const override;
relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
relocation_iterator section_rel_end(DataRefImpl Sec) const override;
std::vector<SectionRef> dynamic_relocation_sections() const override;
@@ -760,6 +762,20 @@ bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
}
template <class ELFT>
+bool ELFObjectFile<ELFT>::isBerkeleyText(DataRefImpl Sec) const {
+ return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
+ (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
+ !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
+}
+
+template <class ELFT>
+bool ELFObjectFile<ELFT>::isBerkeleyData(DataRefImpl Sec) const {
+ const Elf_Shdr *EShdr = getSection(Sec);
+ return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
+ EShdr->sh_flags & ELF::SHF_ALLOC;
+}
+
+template <class ELFT>
relocation_iterator
ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
DataRefImpl RelData;
diff --git a/llvm/include/llvm/Object/ObjectFile.h b/llvm/include/llvm/Object/ObjectFile.h
index 02d62e8e487..036c99cb6ba 100644
--- a/llvm/include/llvm/Object/ObjectFile.h
+++ b/llvm/include/llvm/Object/ObjectFile.h
@@ -104,13 +104,25 @@ public:
uint64_t getAlignment() const;
bool isCompressed() const;
+ /// Whether this section contains instructions.
bool isText() const;
+ /// Whether this section contains data, not instructions.
bool isData() const;
+ /// Whether this section contains BSS uninitialized data.
bool isBSS() const;
bool isVirtual() const;
bool isBitcode() const;
bool isStripped() const;
+ /// Whether this section will be placed in the text segment, according to the
+ /// Berkeley size format. This is true if the section is allocatable, and
+ /// contains either code or readonly data.
+ bool isBerkeleyText() const;
+ /// Whether this section will be placed in the data segment, according to the
+ /// Berkeley size format. This is true if the section is allocatable and
+ /// contains data (e.g. PROGBITS), but is not text.
+ bool isBerkeleyData() const;
+
bool containsSymbol(SymbolRef S) const;
relocation_iterator relocation_begin() const;
@@ -238,6 +250,8 @@ protected:
virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
virtual bool isSectionBitcode(DataRefImpl Sec) const;
virtual bool isSectionStripped(DataRefImpl Sec) const;
+ virtual bool isBerkeleyText(DataRefImpl Sec) const;
+ virtual bool isBerkeleyData(DataRefImpl Sec) const;
virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
@@ -449,6 +463,14 @@ inline bool SectionRef::isStripped() const {
return OwningObject->isSectionStripped(SectionPimpl);
}
+inline bool SectionRef::isBerkeleyText() const {
+ return OwningObject->isBerkeleyText(SectionPimpl);
+}
+
+inline bool SectionRef::isBerkeleyData() const {
+ return OwningObject->isBerkeleyData(SectionPimpl);
+}
+
inline relocation_iterator SectionRef::relocation_begin() const {
return OwningObject->section_rel_begin(SectionPimpl);
}
diff --git a/llvm/lib/Object/ObjectFile.cpp b/llvm/lib/Object/ObjectFile.cpp
index db0ff220c4d..cf63b89adc1 100644
--- a/llvm/lib/Object/ObjectFile.cpp
+++ b/llvm/lib/Object/ObjectFile.cpp
@@ -77,6 +77,14 @@ bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
+bool ObjectFile::isBerkeleyText(DataRefImpl Sec) const {
+ return isSectionText(Sec);
+}
+
+bool ObjectFile::isBerkeleyData(DataRefImpl Sec) const {
+ return isSectionData(Sec);
+}
+
section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
return section_iterator(SectionRef(Sec, this));
}
diff --git a/llvm/test/tools/llvm-size/X86/elf-sizes.test b/llvm/test/tools/llvm-size/X86/elf-sizes.test
new file mode 100644
index 00000000000..cb7fcd3a885
--- /dev/null
+++ b/llvm/test/tools/llvm-size/X86/elf-sizes.test
@@ -0,0 +1,55 @@
+# RUN: yaml2obj %s > %t.o
+# RUN: llvm-size -B %t.o | FileCheck %s
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+ Machine: EM_X86_64
+Sections:
+ - Name: .bss
+ Type: SHT_NOBITS
+ Flags: [ SHF_ALLOC, SHF_WRITE ]
+ Size: 1
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Size: 2
+ - Name: .unusual_name_for_code
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Size: 64
+ - Name: .eh_frame
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC ]
+ Size: 4
+ - Name: .data
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_WRITE ]
+ Size: 8
+ - Name: .moar_stuff
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_WRITE ]
+ Size: 128
+ - Name: .text.but_not_really
+ Type: SHT_PROGBITS
+ Flags: [ ]
+ Size: 256
+ - Name: .debug_info
+ Type: SHT_PROGBITS
+ Flags: [ ]
+ Size: 16
+ - Name: .init_array
+ Type: SHT_INIT_ARRAY
+ Flags: [ SHF_ALLOC, SHF_WRITE ]
+ Size: 32
+
+# text is .text, .eh_frame, .unusual_name_for_code: 2 + 4 + 64 = 70
+# data is .data, .init_array, .moar_stuff: 8 + 32 + 128 = 168
+# bss is .bss: 1
+# total: 239
+# unaccounted for (not affecting total) is .debug_info, .text.but_not_really
+
+# CHECK: text data bss dec
+# CHECK: 70 168 1 239
diff --git a/llvm/test/tools/llvm-size/X86/ignore-sections.s b/llvm/test/tools/llvm-size/X86/ignore-sections.s
index c597f848b0b..61aa2e780e5 100644
--- a/llvm/test/tools/llvm-size/X86/ignore-sections.s
+++ b/llvm/test/tools/llvm-size/X86/ignore-sections.s
@@ -25,5 +25,5 @@
// SYSV-NEXT: Total 69
// BSD: text data bss dec hex filename
-// BSD-NEXT: 4 4 4 12 c {{[ -\(\)_A-Za-z0-9.\\/:]+}}
-// BSD-NEXT: 4 4 4 12 c (TOTALS)
+// BSD-NEXT: 52 4 4 60 3c {{[ -\(\)_A-Za-z0-9.\\/:]+}}
+// BSD-NEXT: 52 4 4 60 3c (TOTALS)
diff --git a/llvm/tools/llvm-size/llvm-size.cpp b/llvm/tools/llvm-size/llvm-size.cpp
index a92a8ab25a1..be1e5bb7268 100644
--- a/llvm/tools/llvm-size/llvm-size.cpp
+++ b/llvm/tools/llvm-size/llvm-size.cpp
@@ -457,8 +457,8 @@ static void printObjectSectionSizes(ObjectFile *Obj) {
// Make one pass over the section table to calculate sizes.
for (const SectionRef &Section : Obj->sections()) {
uint64_t size = Section.getSize();
- bool isText = Section.isText();
- bool isData = Section.isData();
+ bool isText = Section.isBerkeleyText();
+ bool isData = Section.isBerkeleyData();
bool isBSS = Section.isBSS();
if (isText)
total_text += size;
OpenPOWER on IntegriCloud