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/ManagedStatic.h"
21 #include "llvm/Support/PrettyStackTrace.h"
22 #include "llvm/Support/Signals.h"
23 #include "llvm/Support/TargetSelect.h"
24 #include "llvm/Support/raw_ostream.h"
29 DisableOpt("disable-opt", cl::init(false),
30 cl::desc("Do not run any optimization passes"));
33 DisableInline("disable-inlining", cl::init(false),
34 cl::desc("Do not run the inliner pass"));
37 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
38 cl::desc("Do not run the GVN load PRE pass"));
40 static cl::list<std::string>
41 InputFilenames(cl::Positional, cl::OneOrMore,
42 cl::desc("<input bitcode files>"));
44 static cl::opt<std::string>
45 OutputFilename("o", cl::init(""),
46 cl::desc("Override output filename"),
47 cl::value_desc("filename"));
49 static cl::list<std::string>
50 ExportedSymbols("exported-symbol",
51 cl::desc("Symbol to export from the resulting object file"),
54 static cl::list<std::string>
55 DSOSymbols("dso-symbol",
56 cl::desc("Symbol to put in the symtab in the resulting dso"),
61 std::vector<bool> CanBeHidden;
65 int main(int argc, char **argv) {
66 // Print a stack trace if we signal out.
67 sys::PrintStackTraceOnErrorSignal();
68 PrettyStackTraceProgram X(argc, argv);
70 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
71 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
73 // Initialize the configured targets.
74 InitializeAllTargets();
75 InitializeAllTargetMCs();
76 InitializeAllAsmPrinters();
77 InitializeAllAsmParsers();
79 // set up the TargetOptions for the machine
80 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
84 LTOCodeGenerator CodeGen;
88 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_STATIC);
91 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
93 case Reloc::DynamicNoPIC:
94 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC);
97 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DEFAULT);
100 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
101 CodeGen.setTargetOptions(Options);
103 llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet;
104 for (unsigned i = 0; i < DSOSymbols.size(); ++i)
105 DSOSymbolsSet.insert(DSOSymbols[i]);
107 std::vector<std::string> KeptDSOSyms;
109 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
111 std::unique_ptr<LTOModule> Module(
112 LTOModule::makeLTOModule(InputFilenames[i].c_str(), Options, error));
113 if (!error.empty()) {
114 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
115 << "': " << error << "\n";
120 if (!CodeGen.addModule(Module.get(), error)) {
121 errs() << argv[0] << ": error adding file '" << InputFilenames[i]
122 << "': " << error << "\n";
126 unsigned NumSyms = Module->getSymbolCount();
127 for (unsigned I = 0; I < NumSyms; ++I) {
128 StringRef Name = Module->getSymbolName(I);
129 if (!DSOSymbolsSet.count(Name))
131 lto_symbol_attributes Attrs = Module->getSymbolAttributes(I);
132 unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;
133 if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)
134 KeptDSOSyms.push_back(Name);
138 // Add all the exported symbols to the table of symbols to preserve.
139 for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
140 CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
142 // Add all the dso symbols to the table of symbols to expose.
143 for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)
144 CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str());
147 for (unsigned i = 0; i < MAttrs.size(); ++i) {
150 attrs.append(MAttrs[i]);
154 CodeGen.setAttr(attrs.c_str());
156 if (!OutputFilename.empty()) {
158 std::string ErrorInfo;
159 const void *Code = CodeGen.compile(&len, DisableOpt, DisableInline,
160 DisableGVNLoadPRE, ErrorInfo);
163 << ": error compiling the code: " << ErrorInfo << "\n";
167 raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo,
169 if (!ErrorInfo.empty()) {
170 errs() << argv[0] << ": error opening the file '" << OutputFilename
171 << "': " << ErrorInfo << "\n";
175 FileStream.write(reinterpret_cast<const char *>(Code), len);
177 std::string ErrorInfo;
178 const char *OutputName = nullptr;
179 if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline,
180 DisableGVNLoadPRE, ErrorInfo)) {
182 << ": error compiling the code: " << ErrorInfo
187 outs() << "Wrote native object file '" << OutputName << "'\n";