Path: blob/main/contrib/llvm-project/llvm/tools/llvm-dwarfdump/SectionSizes.cpp
35231 views
//===-- SectionSizes.cpp - Debug section sizes ----------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm-dwarfdump.h"910#define DEBUG_TYPE "dwarfdump"1112using namespace llvm;13using namespace llvm::dwarfdump;14using namespace llvm::object;1516static size_t getNameColumnWidth(const SectionSizes &Sizes,17const StringRef SectionNameTitle) {18// The minimum column width should be the size of "SECTION".19size_t Width = SectionNameTitle.size();20for (const auto &It : Sizes.DebugSectionSizes)21Width = std::max(Width, It.first.size());22return Width;23}2425static size_t getSizeColumnWidth(const SectionSizes &Sizes,26const StringRef SectionSizeTitle) {27// The minimum column width should be the size of the column title.28size_t Width = SectionSizeTitle.size();29for (const auto &It : Sizes.DebugSectionSizes) {30size_t NumWidth = std::to_string(It.second).size();31Width = std::max(Width, NumWidth);32}33return Width;34}3536static void prettyPrintSectionSizes(const ObjectFile &Obj,37const SectionSizes &Sizes,38raw_ostream &OS) {39const StringRef SectionNameTitle = "SECTION";40const StringRef SectionSizeTitle = "SIZE (b)";4142size_t NameColWidth = getNameColumnWidth(Sizes, SectionNameTitle);43size_t SizeColWidth = getSizeColumnWidth(Sizes, SectionSizeTitle);4445OS << "----------------------------------------------------" << '\n';46OS << SectionNameTitle;47size_t SectionNameTitleWidth = SectionNameTitle.size();48for (unsigned i = 0; i < (NameColWidth - SectionNameTitleWidth) + 2; i++)49OS << " ";50OS << SectionSizeTitle << '\n';51for (unsigned i = 0; i < NameColWidth; i++)52OS << "-";53OS << " ";5455for (unsigned i = 0; i < SizeColWidth; i++)56OS << "-";57OS << '\n';5859for (const auto &It : Sizes.DebugSectionSizes) {60OS << left_justify(It.first, NameColWidth) << " ";6162std::string NumBytes = std::to_string(It.second);63OS << right_justify(NumBytes, SizeColWidth) << " ("64<< format("%0.2f",65It.second / static_cast<double>(Sizes.TotalObjectSize) * 100)66<< "%)\n";67}6869OS << '\n';70OS << " Total Size: " << Sizes.TotalDebugSectionsSize << " ("71<< format("%0.2f", Sizes.TotalDebugSectionsSize /72static_cast<double>(Sizes.TotalObjectSize) * 100)73<< "%)\n";74OS << " Total File Size: " << Sizes.TotalObjectSize << '\n';75OS << "----------------------------------------------------" << '\n';76}7778void dwarfdump::calculateSectionSizes(const ObjectFile &Obj,79SectionSizes &Sizes,80const Twine &Filename) {81// Get total size.82Sizes.TotalObjectSize = Obj.getData().size();8384for (const SectionRef &Section : Obj.sections()) {85StringRef SectionName;86if (Expected<StringRef> NameOrErr = Section.getName())87SectionName = *NameOrErr;88else89WithColor::defaultWarningHandler(90createFileError(Filename, NameOrErr.takeError()));9192LLVM_DEBUG(dbgs() << SectionName.str() << ": " << Section.getSize()93<< '\n');9495if (!Section.isDebugSection())96continue;9798Sizes.TotalDebugSectionsSize += Section.getSize();99Sizes.DebugSectionSizes[std::string(SectionName)] += Section.getSize();100}101}102103bool dwarfdump::collectObjectSectionSizes(ObjectFile &Obj,104DWARFContext & /*DICtx*/,105const Twine &Filename,106raw_ostream &OS) {107SectionSizes Sizes;108109// Get the section sizes.110calculateSectionSizes(Obj, Sizes, Filename);111112OS << "----------------------------------------------------\n";113OS << "file: " << Filename.str() << '\n';114115prettyPrintSectionSizes(Obj, Sizes, OS);116117// TODO: If the input file is an archive, print the cumulative summary of all118// files from the archive.119120return true;121}122123124