Skip to content
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

Dev/lto sections #29

Merged
merged 5 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions llvm/tools/llvm-lto-sections/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set(LLVM_LINK_COMPONENTS
Core
IRReader
Support
)

add_llvm_tool(llvm-lto-sections
llvm-lto-sections.cpp
)
158 changes: 158 additions & 0 deletions llvm/tools/llvm-lto-sections/llvm-lto-sections.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//===-- llvm-lto-sections: tool for reporting sections for LTO bitcode ---===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This program reports the sections and symbols that may be present in final
// link from included LTO bitcode files.
//
// This is a best-effort attempt to predict the sections. Inherently,
// it can't know whether sections may be removed by the LTO
// process. Mergeable sections can't be accounted for either.
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/StringExtras.h"
#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"


using namespace llvm;

static cl::OptionCategory OptionsCategory("Section Options");

static cl::opt<std::string> InputFilename(cl::Positional,
cl::desc("<input bitcode file>"),
cl::init("-"),
cl::value_desc("filename"),
cl::cat(OptionsCategory));

static cl::opt<bool>
FFunctionSections("ffunction-sections", cl::Prefix, cl::init(false),
cl::desc("Place each function in its own section"),
cl::cat(OptionsCategory));

static cl::opt<bool>
FDataSections("fdata-sections", cl::Prefix, cl::init(false),
cl::desc("Place each data object in its own section"),
cl::cat(OptionsCategory));

static cl::opt<bool> SymbolsReport("symbols", cl::Prefix, cl::init(false),
cl::desc("Report symbol names"),
cl::cat(OptionsCategory));

static cl::opt<bool> SectionsReport("sections", cl::Prefix, cl::init(false),
cl::desc("Report section names"),
cl::cat(OptionsCategory));

static cl::opt<bool>
MapReport("map", cl::Prefix, cl::init(false),
cl::desc("Report mapping of symbols to sections"),
cl::cat(OptionsCategory));

std::string FunctionSectionName(Function &F) {
cme marked this conversation as resolved.
Show resolved Hide resolved
if (F.hasSection()) {
return F.getSection().str();
} else {
if (FFunctionSections) {
return ".text." + F.getName().str();
} else {
return ".text";
}
}
}

std::string GlobalSectionName(GlobalVariable &G) {
cme marked this conversation as resolved.
Show resolved Hide resolved
if (G.hasSection()) {
return G.getSection().str();
} else {
std::string Prefix;
if (G.isConstant()) {
Prefix = ".rodata";
} else {
if (G.hasInitializer() && !G.getInitializer()->isZeroValue()) {
Prefix = ".data";
} else {
Prefix = ".bss";
}
}
if (FDataSections) {
if (G.getName().str()[0] == '.') {
return Prefix + G.getName().str();
} else {
return Prefix + "." + G.getName().str();
}
} else {
return Prefix;
}
}
}

std::map<std::string, std::string> SymbolsToSectionsMap;
std::set<std::string> SectionNames;
std::set<std::string> SymbolNames;
cme marked this conversation as resolved.
Show resolved Hide resolved

int main(int argc, char **argv) {
LLVMContext Context;
SMDiagnostic Err;
cl::HideUnrelatedOptions({&OptionsCategory});
cl::ParseCommandLineOptions(argc, argv, "LLVM LTO section tool\n");

std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);

if (!M) {
Err.print(argv[0], errs());
return 1;
}

auto RecordSymbol = [](std::string SymbolName, std::string SectionName) {
if (SectionsReport || MapReport) {
SectionNames.insert(SectionName);
}
if (SymbolsReport || MapReport) {
SymbolNames.insert(SymbolName);
}
if (MapReport) {
SymbolsToSectionsMap[SymbolName] = SectionName;
}
};

for (GlobalVariable &G : M->globals()) {
RecordSymbol(G.getName().str(), GlobalSectionName(G));
}

for (Function &F : M->functions()) {
RecordSymbol(F.getName().str(), FunctionSectionName(F));
}

if (SectionsReport) {
outs() << SectionNames.size() << " sections:\n";
for (auto &S : SectionNames) {
outs() << S << "\n";
}
}

if (SymbolsReport) {
outs() << SymbolNames.size() << " symbols:\n";
for (auto &S : SymbolNames) {
outs() << S << "\n";
}
}

if (MapReport) {
outs() << SymbolNames.size() << " symbols in " << SectionNames.size()
<< " sections\n";
for (auto &M : SymbolsToSectionsMap) {
outs() << M.first << " " << M.second << "\n";
}
}

return 0;
}