diff options
author | Ryan Mansfield <rmansfield@gmail.com> | 2025-09-18 09:07:33 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-09-18 14:07:33 +0100 |
commit | dcd0a2eecca38bbe7813ab1d99639b2d33d9f9ad (patch) | |
tree | f332ff579fa242a1f5f5f20526bce1567a4075cc /llvm/tools/llvm-size/llvm-size.cpp | |
parent | 01b4b2a5b88c4b93d635a5049fa85e569b405982 (diff) | |
download | llvm-dcd0a2eecca38bbe7813ab1d99639b2d33d9f9ad.zip llvm-dcd0a2eecca38bbe7813ab1d99639b2d33d9f9ad.tar.gz llvm-dcd0a2eecca38bbe7813ab1d99639b2d33d9f9ad.tar.bz2 |
[llvm-size] Fix --totals option for Mach-O files (#157904)
The --totals option was not working for Mach-O files because the Darwin
segment size calculation skipped the totals accumulation.
---------
Co-authored-by: James Henderson <James.Henderson@sony.com>
Diffstat (limited to 'llvm/tools/llvm-size/llvm-size.cpp')
-rw-r--r-- | llvm/tools/llvm-size/llvm-size.cpp | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/llvm/tools/llvm-size/llvm-size.cpp b/llvm/tools/llvm-size/llvm-size.cpp index afb6b23..acc7843 100644 --- a/llvm/tools/llvm-size/llvm-size.cpp +++ b/llvm/tools/llvm-size/llvm-size.cpp @@ -78,6 +78,7 @@ static OutputFormatTy OutputFormat; static bool DarwinLongFormat; static RadixTy Radix = RadixTy::decimal; static bool TotalSizes; +static bool HasMachOFiles = false; static std::vector<std::string> InputFilenames; @@ -92,6 +93,10 @@ static uint64_t TotalObjectData = 0; static uint64_t TotalObjectBss = 0; static uint64_t TotalObjectTotal = 0; +// Darwin-specific totals +static uint64_t TotalObjectObjc = 0; +static uint64_t TotalObjectOthers = 0; + static void error(const Twine &Message, StringRef File = "") { HadError = true; if (File.empty()) @@ -283,6 +288,7 @@ static void printDarwinSegmentSizes(MachOObjectFile *MachO) { uint64_t total_data = 0; uint64_t total_objc = 0; uint64_t total_others = 0; + HasMachOFiles = true; for (const auto &Load : MachO->load_commands()) { if (Load.C.cmd == MachO::LC_SEGMENT_64) { MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load); @@ -340,6 +346,14 @@ static void printDarwinSegmentSizes(MachOObjectFile *MachO) { } uint64_t total = total_text + total_data + total_objc + total_others; + if (TotalSizes) { + TotalObjectText += total_text; + TotalObjectData += total_data; + TotalObjectObjc += total_objc; + TotalObjectOthers += total_others; + TotalObjectTotal += total; + } + if (!BerkeleyHeaderPrinted) { outs() << "__TEXT\t__DATA\t__OBJC\tothers\tdec\thex\n"; BerkeleyHeaderPrinted = true; @@ -852,16 +866,25 @@ static void printBerkeleyTotals() { std::string fmtbuf; raw_string_ostream fmt(fmtbuf); const char *radix_fmt = getRadixFmt(); - fmt << "%#7" << radix_fmt << "\t" - << "%#7" << radix_fmt << "\t" - << "%#7" << radix_fmt << "\t"; - outs() << format(fmtbuf.c_str(), TotalObjectText, TotalObjectData, - TotalObjectBss); - fmtbuf.clear(); - fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << "\t" - << "%7" PRIx64 "\t"; - outs() << format(fmtbuf.c_str(), TotalObjectTotal, TotalObjectTotal) - << "(TOTALS)\n"; + + if (HasMachOFiles) { + // Darwin format totals: __TEXT __DATA __OBJC others dec hex + outs() << TotalObjectText << "\t" << TotalObjectData << "\t" + << TotalObjectObjc << "\t" << TotalObjectOthers << "\t" + << TotalObjectTotal << "\t" << format("%" PRIx64, TotalObjectTotal) + << "\t(TOTALS)\n"; + } else { + fmt << "%#7" << radix_fmt << "\t" + << "%#7" << radix_fmt << "\t" + << "%#7" << radix_fmt << "\t"; + outs() << format(fmtbuf.c_str(), TotalObjectText, TotalObjectData, + TotalObjectBss); + fmtbuf.clear(); + fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << "\t" + << "%7" PRIx64 "\t"; + outs() << format(fmtbuf.c_str(), TotalObjectTotal, TotalObjectTotal) + << "(TOTALS)\n"; + } } int llvm_size_main(int argc, char **argv, const llvm::ToolContext &) { |