Index: llvm/trunk/include/llvm/Object/ELFObjectFile.h =================================================================== --- llvm/trunk/include/llvm/Object/ELFObjectFile.h +++ llvm/trunk/include/llvm/Object/ELFObjectFile.h @@ -260,6 +260,8 @@ 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 dynamic_relocation_sections() const override; @@ -760,6 +762,20 @@ } template +bool ELFObjectFile::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 +bool ELFObjectFile::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 relocation_iterator ELFObjectFile::section_rel_begin(DataRefImpl Sec) const { DataRefImpl RelData; Index: llvm/trunk/include/llvm/Object/ObjectFile.h =================================================================== --- llvm/trunk/include/llvm/Object/ObjectFile.h +++ llvm/trunk/include/llvm/Object/ObjectFile.h @@ -104,13 +104,25 @@ 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 @@ 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 @@ 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); } Index: llvm/trunk/lib/Object/ObjectFile.cpp =================================================================== --- llvm/trunk/lib/Object/ObjectFile.cpp +++ llvm/trunk/lib/Object/ObjectFile.cpp @@ -77,6 +77,14 @@ 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)); } Index: llvm/trunk/test/tools/llvm-size/X86/elf-sizes.test =================================================================== --- llvm/trunk/test/tools/llvm-size/X86/elf-sizes.test +++ llvm/trunk/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 Index: llvm/trunk/test/tools/llvm-size/X86/ignore-sections.s =================================================================== --- llvm/trunk/test/tools/llvm-size/X86/ignore-sections.s +++ llvm/trunk/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) Index: llvm/trunk/tools/llvm-size/llvm-size.cpp =================================================================== --- llvm/trunk/tools/llvm-size/llvm-size.cpp +++ llvm/trunk/tools/llvm-size/llvm-size.cpp @@ -457,8 +457,8 @@ // 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;