Skip to content

[llvm][ELF] Add ELF header/section header table size statistics #109345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions llvm/lib/MC/ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ using namespace llvm;
namespace {
namespace stats {

STATISTIC(ELFHeaderBytes, "Total size of ELF headers");
STATISTIC(SectionHeaderBytes, "Total size of section headers table");
STATISTIC(AllocTextBytes, "Total size of SHF_ALLOC text sections");
STATISTIC(AllocROBytes, "Total size of SHF_ALLOC readonly sections");
STATISTIC(AllocRWBytes, "Total size of SHF_ALLOC read-write sections");
Expand Down Expand Up @@ -945,6 +947,7 @@ void ELFWriter::writeSectionHeader(uint32_t GroupSymbolIndex, uint64_t Offset,
}

void ELFWriter::writeSectionHeaders(const MCAssembler &Asm) {
uint64_t Start = W.OS.tell();
const unsigned NumSections = SectionTable.size();

// Null section first.
Expand Down Expand Up @@ -1008,6 +1011,8 @@ void ELFWriter::writeSectionHeaders(const MCAssembler &Asm) {

writeSectionHeader(GroupSymbolIndex, Offsets.first, Size, *Section);
}

stats::SectionHeaderBytes += W.OS.tell() - Start;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test?

If llvm-project ever gets https://maskray.me/blog/2024-03-31-a-compact-section-header-table-for-elf (impl:
https://github.com/MaskRay/llvm-project/tree/demo-cshdr ; which might need certain extent of agreement), this can be used to show the size difference due to -Wa,--cshdr :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really want to hardcode the value of this stat in the test, and I'm not sure how else I'd test it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encoding the value should be fine. It's very unlikely that the value will ever change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
Expand All @@ -1023,6 +1028,8 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
// Write out the ELF header ...
writeHeader(Asm);

stats::ELFHeaderBytes += W.OS.tell() - StartOffset;

// ... then the sections ...
SmallVector<std::pair<MCSectionELF *, SmallVector<unsigned>>, 0> Groups;
// Map from group section index to group
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/X86/section-stats.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

; CHECK-DAG: 1 elf-object-writer - Total size of SHF_ALLOC text sections
; CHECK-DAG: 1 elf-object-writer - Total size of SHF_ALLOC read-write sections
; CHECK-DAG: 512 elf-object-writer - Total size of section headers table
; CHECK-DAG: 64 elf-object-writer - Total size of ELF headers

target triple = "x86_64-unknown-linux-gnu"

Expand Down
Loading