blob: 0910df14203540a78c7ce19589017dd547985ddb [file] [log] [blame]
Peter Collingbourne4e380b02013-09-19 22:15:52 +00001//===-- llvm-lto: a simple command-line program to link modules with LTO --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program takes in a list of bitcode files, links them, performs link-time
11// optimization, and outputs an object file.
12//
13//===----------------------------------------------------------------------===//
14
Rafael Espindola282a4702013-10-31 20:51:58 +000015#include "llvm/ADT/StringSet.h"
Rafael Espindola0b385c72013-09-30 16:39:19 +000016#include "llvm/CodeGen/CommandFlags.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000017#include "llvm/LTO/LTOCodeGenerator.h"
18#include "llvm/LTO/LTOModule.h"
Peter Collingbourne4e380b02013-09-19 22:15:52 +000019#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000020#include "llvm/Support/FileSystem.h"
Peter Collingbourne4e380b02013-09-19 22:15:52 +000021#include "llvm/Support/ManagedStatic.h"
22#include "llvm/Support/PrettyStackTrace.h"
23#include "llvm/Support/Signals.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000024#include "llvm/Support/TargetSelect.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000025#include "llvm/Support/raw_ostream.h"
Peter Collingbourne4e380b02013-09-19 22:15:52 +000026
27using namespace llvm;
28
Rafael Espindola0b385c72013-09-30 16:39:19 +000029static cl::opt<bool>
30DisableOpt("disable-opt", cl::init(false),
31 cl::desc("Do not run any optimization passes"));
Peter Collingbourne4e380b02013-09-19 22:15:52 +000032
Rafael Espindola0b385c72013-09-30 16:39:19 +000033static cl::opt<bool>
34DisableInline("disable-inlining", cl::init(false),
35 cl::desc("Do not run the inliner pass"));
36
37static cl::opt<bool>
38DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
39 cl::desc("Do not run the GVN load PRE pass"));
40
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +000041static cl::opt<bool>
42UseDiagnosticHandler("use-diagnostic-handler", cl::init(false),
43 cl::desc("Use a diagnostic handler to test the handler interface"));
44
Rafael Espindola0b385c72013-09-30 16:39:19 +000045static cl::list<std::string>
46InputFilenames(cl::Positional, cl::OneOrMore,
47 cl::desc("<input bitcode files>"));
48
49static cl::opt<std::string>
50OutputFilename("o", cl::init(""),
51 cl::desc("Override output filename"),
52 cl::value_desc("filename"));
Peter Collingbourne4e380b02013-09-19 22:15:52 +000053
Rafael Espindoladafc53d2013-10-02 14:12:56 +000054static cl::list<std::string>
55ExportedSymbols("exported-symbol",
56 cl::desc("Symbol to export from the resulting object file"),
57 cl::ZeroOrMore);
58
Rafael Espindolacda29112013-10-03 18:29:09 +000059static cl::list<std::string>
60DSOSymbols("dso-symbol",
61 cl::desc("Symbol to put in the symtab in the resulting dso"),
62 cl::ZeroOrMore);
Rafael Espindoladafc53d2013-10-02 14:12:56 +000063
Rafael Espindola282a4702013-10-31 20:51:58 +000064namespace {
65struct ModuleInfo {
66 std::vector<bool> CanBeHidden;
67};
68}
69
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +000070void handleDiagnostics(lto_codegen_diagnostic_severity_t Severity,
71 const char *Msg, void *) {
72 switch (Severity) {
73 case LTO_DS_NOTE:
74 errs() << "note: ";
75 break;
76 case LTO_DS_REMARK:
77 errs() << "remark: ";
78 break;
79 case LTO_DS_ERROR:
80 errs() << "error: ";
81 break;
82 case LTO_DS_WARNING:
83 errs() << "warning: ";
84 break;
85 }
86 errs() << Msg << "\n";
87}
88
Peter Collingbourne4e380b02013-09-19 22:15:52 +000089int main(int argc, char **argv) {
90 // Print a stack trace if we signal out.
91 sys::PrintStackTraceOnErrorSignal();
92 PrettyStackTraceProgram X(argc, argv);
93
94 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
95 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
96
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000097 // Initialize the configured targets.
98 InitializeAllTargets();
99 InitializeAllTargetMCs();
100 InitializeAllAsmPrinters();
101 InitializeAllAsmParsers();
102
Rafael Espindola0b385c72013-09-30 16:39:19 +0000103 // set up the TargetOptions for the machine
Eli Benderskyf0f21002014-02-19 17:09:35 +0000104 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000105
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000106 unsigned BaseArg = 0;
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000107
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000108 LTOCodeGenerator CodeGen;
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000109
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000110 if (UseDiagnosticHandler)
111 CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr);
112
James Molloy951e5292014-04-14 13:54:16 +0000113 switch (RelocModel) {
114 case Reloc::Static:
115 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_STATIC);
116 break;
117 case Reloc::PIC_:
118 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
119 break;
120 case Reloc::DynamicNoPIC:
121 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC);
122 break;
123 default:
124 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DEFAULT);
125 }
126
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000127 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
Rafael Espindola0b385c72013-09-30 16:39:19 +0000128 CodeGen.setTargetOptions(Options);
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000129
Rafael Espindola282a4702013-10-31 20:51:58 +0000130 llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet;
131 for (unsigned i = 0; i < DSOSymbols.size(); ++i)
132 DSOSymbolsSet.insert(DSOSymbols[i]);
133
134 std::vector<std::string> KeptDSOSyms;
135
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000136 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000137 std::string error;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000138 std::unique_ptr<LTOModule> Module(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000139 LTOModule::createFromFile(InputFilenames[i].c_str(), Options, error));
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000140 if (!error.empty()) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000141 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000142 << "': " << error << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000143 return 1;
144 }
145
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000146 if (!CodeGen.addModule(Module.get()))
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000147 return 1;
Rafael Espindola282a4702013-10-31 20:51:58 +0000148
149 unsigned NumSyms = Module->getSymbolCount();
150 for (unsigned I = 0; I < NumSyms; ++I) {
151 StringRef Name = Module->getSymbolName(I);
152 if (!DSOSymbolsSet.count(Name))
153 continue;
154 lto_symbol_attributes Attrs = Module->getSymbolAttributes(I);
155 unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;
156 if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)
157 KeptDSOSyms.push_back(Name);
158 }
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000159 }
160
Rafael Espindoladafc53d2013-10-02 14:12:56 +0000161 // Add all the exported symbols to the table of symbols to preserve.
162 for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
163 CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
164
Rafael Espindolacda29112013-10-03 18:29:09 +0000165 // Add all the dso symbols to the table of symbols to expose.
Rafael Espindola282a4702013-10-31 20:51:58 +0000166 for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)
167 CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str());
Rafael Espindolacda29112013-10-03 18:29:09 +0000168
Tom Roederfd1bc602014-04-25 21:46:51 +0000169 std::string attrs;
170 for (unsigned i = 0; i < MAttrs.size(); ++i) {
171 if (i > 0)
172 attrs.append(",");
173 attrs.append(MAttrs[i]);
174 }
175
176 if (!attrs.empty())
177 CodeGen.setAttr(attrs.c_str());
178
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000179 if (!OutputFilename.empty()) {
180 size_t len = 0;
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000181 std::string ErrorInfo;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000182 const void *Code = CodeGen.compile(&len, DisableOpt, DisableInline,
183 DisableGVNLoadPRE, ErrorInfo);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000184 if (!Code) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000185 errs() << argv[0]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000186 << ": error compiling the code: " << ErrorInfo << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000187 return 1;
188 }
189
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000190 std::error_code EC;
191 raw_fd_ostream FileStream(OutputFilename, EC, sys::fs::F_None);
192 if (EC) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000193 errs() << argv[0] << ": error opening the file '" << OutputFilename
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000194 << "': " << EC.message() << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000195 return 1;
196 }
197
198 FileStream.write(reinterpret_cast<const char *>(Code), len);
199 } else {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000200 std::string ErrorInfo;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000201 const char *OutputName = nullptr;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000202 if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline,
203 DisableGVNLoadPRE, ErrorInfo)) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000204 errs() << argv[0]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000205 << ": error compiling the code: " << ErrorInfo
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000206 << "\n";
207 return 1;
208 }
209
210 outs() << "Wrote native object file '" << OutputName << "'\n";
211 }
212
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000213 return 0;
214}