1 //===-- llvm-lto: a simple command-line program to link modules with LTO --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program takes in a list of bitcode files, links them, performs link-time
11 // optimization, and outputs an object file.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/StringSet.h"
16 #include "llvm/CodeGen/CommandFlags.h"
17 #include "llvm/LTO/LTOCodeGenerator.h"
18 #include "llvm/LTO/LTOModule.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/PrettyStackTrace.h"
23 #include "llvm/Support/Signals.h"
24 #include "llvm/Support/TargetSelect.h"
25 #include "llvm/Support/raw_ostream.h"
30 DisableOpt("disable-opt", cl::init(false),
31 cl::desc("Do not run any optimization passes"));
34 DisableInline("disable-inlining", cl::init(false),
35 cl::desc("Do not run the inliner pass"));
38 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
39 cl::desc("Do not run the GVN load PRE pass"));
42 DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
43 cl::desc("Do not run loop or slp vectorization during LTO"));
46 UseDiagnosticHandler("use-diagnostic-handler", cl::init(false),
47 cl::desc("Use a diagnostic handler to test the handler interface"));
49 static cl::list<std::string>
50 InputFilenames(cl::Positional, cl::OneOrMore,
51 cl::desc("<input bitcode files>"));
53 static cl::opt<std::string>
54 OutputFilename("o", cl::init(""),
55 cl::desc("Override output filename"),
56 cl::value_desc("filename"));
58 static cl::list<std::string>
59 ExportedSymbols("exported-symbol",
60 cl::desc("Symbol to export from the resulting object file"),
63 static cl::list<std::string>
64 DSOSymbols("dso-symbol",
65 cl::desc("Symbol to put in the symtab in the resulting dso"),
70 std::vector<bool> CanBeHidden;
74 void handleDiagnostics(lto_codegen_diagnostic_severity_t Severity,
75 const char *Msg, void *) {
87 errs() << "warning: ";
90 errs() << Msg << "\n";
93 int main(int argc, char **argv) {
94 // Print a stack trace if we signal out.
95 sys::PrintStackTraceOnErrorSignal();
96 PrettyStackTraceProgram X(argc, argv);
98 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
99 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
101 // Initialize the configured targets.
102 InitializeAllTargets();
103 InitializeAllTargetMCs();
104 InitializeAllAsmPrinters();
105 InitializeAllAsmParsers();
107 // set up the TargetOptions for the machine
108 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
110 unsigned BaseArg = 0;
112 LTOCodeGenerator CodeGen;
114 if (UseDiagnosticHandler)
115 CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr);
117 switch (RelocModel) {
119 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_STATIC);
122 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
124 case Reloc::DynamicNoPIC:
125 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC);
128 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DEFAULT);
131 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
132 CodeGen.setTargetOptions(Options);
134 llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet;
135 for (unsigned i = 0; i < DSOSymbols.size(); ++i)
136 DSOSymbolsSet.insert(DSOSymbols[i]);
138 std::vector<std::string> KeptDSOSyms;
140 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
142 std::unique_ptr<LTOModule> Module(
143 LTOModule::createFromFile(InputFilenames[i].c_str(), Options, error));
144 if (!error.empty()) {
145 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
146 << "': " << error << "\n";
150 if (!CodeGen.addModule(Module.get()))
153 unsigned NumSyms = Module->getSymbolCount();
154 for (unsigned I = 0; I < NumSyms; ++I) {
155 StringRef Name = Module->getSymbolName(I);
156 if (!DSOSymbolsSet.count(Name))
158 lto_symbol_attributes Attrs = Module->getSymbolAttributes(I);
159 unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;
160 if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)
161 KeptDSOSyms.push_back(Name);
165 // Add all the exported symbols to the table of symbols to preserve.
166 for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
167 CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
169 // Add all the dso symbols to the table of symbols to expose.
170 for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)
171 CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str());
174 for (unsigned i = 0; i < MAttrs.size(); ++i) {
177 attrs.append(MAttrs[i]);
181 CodeGen.setAttr(attrs.c_str());
183 if (!OutputFilename.empty()) {
185 std::string ErrorInfo;
187 CodeGen.compile(&len, DisableOpt, DisableInline, DisableGVNLoadPRE,
188 DisableLTOVectorization, ErrorInfo);
191 << ": error compiling the code: " << ErrorInfo << "\n";
196 raw_fd_ostream FileStream(OutputFilename, EC, sys::fs::F_None);
198 errs() << argv[0] << ": error opening the file '" << OutputFilename
199 << "': " << EC.message() << "\n";
203 FileStream.write(reinterpret_cast<const char *>(Code), len);
205 std::string ErrorInfo;
206 const char *OutputName = nullptr;
207 if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline,
208 DisableGVNLoadPRE, DisableLTOVectorization,
211 << ": error compiling the code: " << ErrorInfo
216 outs() << "Wrote native object file '" << OutputName << "'\n";