blob: 8c2d1cdfbb8ea9e1300fea78ec53f9d69fca31a4 [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
41static cl::list<std::string>
42InputFilenames(cl::Positional, cl::OneOrMore,
43 cl::desc("<input bitcode files>"));
44
45static cl::opt<std::string>
46OutputFilename("o", cl::init(""),
47 cl::desc("Override output filename"),
48 cl::value_desc("filename"));
Peter Collingbourne4e380b02013-09-19 22:15:52 +000049
Rafael Espindoladafc53d2013-10-02 14:12:56 +000050static cl::list<std::string>
51ExportedSymbols("exported-symbol",
52 cl::desc("Symbol to export from the resulting object file"),
53 cl::ZeroOrMore);
54
Rafael Espindolacda29112013-10-03 18:29:09 +000055static cl::list<std::string>
56DSOSymbols("dso-symbol",
57 cl::desc("Symbol to put in the symtab in the resulting dso"),
58 cl::ZeroOrMore);
Rafael Espindoladafc53d2013-10-02 14:12:56 +000059
Rafael Espindola282a4702013-10-31 20:51:58 +000060namespace {
61struct ModuleInfo {
62 std::vector<bool> CanBeHidden;
63};
64}
65
Peter Collingbourne4e380b02013-09-19 22:15:52 +000066int main(int argc, char **argv) {
67 // Print a stack trace if we signal out.
68 sys::PrintStackTraceOnErrorSignal();
69 PrettyStackTraceProgram X(argc, argv);
70
71 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
72 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
73
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000074 // Initialize the configured targets.
75 InitializeAllTargets();
76 InitializeAllTargetMCs();
77 InitializeAllAsmPrinters();
78 InitializeAllAsmParsers();
79
Rafael Espindola0b385c72013-09-30 16:39:19 +000080 // set up the TargetOptions for the machine
Eli Benderskyf0f21002014-02-19 17:09:35 +000081 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +000082
Peter Collingbourne4e380b02013-09-19 22:15:52 +000083 unsigned BaseArg = 0;
Peter Collingbourne4e380b02013-09-19 22:15:52 +000084
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000085 LTOCodeGenerator CodeGen;
Peter Collingbourne4e380b02013-09-19 22:15:52 +000086
James Molloy951e5292014-04-14 13:54:16 +000087 switch (RelocModel) {
88 case Reloc::Static:
89 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_STATIC);
90 break;
91 case Reloc::PIC_:
92 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
93 break;
94 case Reloc::DynamicNoPIC:
95 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC);
96 break;
97 default:
98 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DEFAULT);
99 }
100
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000101 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
Rafael Espindola0b385c72013-09-30 16:39:19 +0000102 CodeGen.setTargetOptions(Options);
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000103
Rafael Espindola282a4702013-10-31 20:51:58 +0000104 llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet;
105 for (unsigned i = 0; i < DSOSymbols.size(); ++i)
106 DSOSymbolsSet.insert(DSOSymbols[i]);
107
108 std::vector<std::string> KeptDSOSyms;
109
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000110 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000111 std::string error;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000112 std::unique_ptr<LTOModule> Module(
113 LTOModule::makeLTOModule(InputFilenames[i].c_str(), Options, error));
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000114 if (!error.empty()) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000115 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000116 << "': " << error << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000117 return 1;
118 }
119
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000120
121 if (!CodeGen.addModule(Module.get(), error)) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000122 errs() << argv[0] << ": error adding file '" << InputFilenames[i]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000123 << "': " << error << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000124 return 1;
125 }
Rafael Espindola282a4702013-10-31 20:51:58 +0000126
127 unsigned NumSyms = Module->getSymbolCount();
128 for (unsigned I = 0; I < NumSyms; ++I) {
129 StringRef Name = Module->getSymbolName(I);
130 if (!DSOSymbolsSet.count(Name))
131 continue;
132 lto_symbol_attributes Attrs = Module->getSymbolAttributes(I);
133 unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;
134 if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)
135 KeptDSOSyms.push_back(Name);
136 }
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000137 }
138
Rafael Espindoladafc53d2013-10-02 14:12:56 +0000139 // Add all the exported symbols to the table of symbols to preserve.
140 for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
141 CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
142
Rafael Espindolacda29112013-10-03 18:29:09 +0000143 // Add all the dso symbols to the table of symbols to expose.
Rafael Espindola282a4702013-10-31 20:51:58 +0000144 for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)
145 CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str());
Rafael Espindolacda29112013-10-03 18:29:09 +0000146
Tom Roederfd1bc602014-04-25 21:46:51 +0000147 std::string attrs;
148 for (unsigned i = 0; i < MAttrs.size(); ++i) {
149 if (i > 0)
150 attrs.append(",");
151 attrs.append(MAttrs[i]);
152 }
153
154 if (!attrs.empty())
155 CodeGen.setAttr(attrs.c_str());
156
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000157 if (!OutputFilename.empty()) {
158 size_t len = 0;
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000159 std::string ErrorInfo;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000160 const void *Code = CodeGen.compile(&len, DisableOpt, DisableInline,
161 DisableGVNLoadPRE, ErrorInfo);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000162 if (!Code) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000163 errs() << argv[0]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000164 << ": error compiling the code: " << ErrorInfo << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000165 return 1;
166 }
167
Rafael Espindoladc9fe0a2013-10-04 21:40:54 +0000168 raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo,
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000169 sys::fs::F_None);
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000170 if (!ErrorInfo.empty()) {
171 errs() << argv[0] << ": error opening the file '" << OutputFilename
172 << "': " << ErrorInfo << "\n";
173 return 1;
174 }
175
176 FileStream.write(reinterpret_cast<const char *>(Code), len);
177 } else {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000178 std::string ErrorInfo;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000179 const char *OutputName = nullptr;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000180 if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline,
181 DisableGVNLoadPRE, ErrorInfo)) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000182 errs() << argv[0]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000183 << ": error compiling the code: " << ErrorInfo
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000184 << "\n";
185 return 1;
186 }
187
188 outs() << "Wrote native object file '" << OutputName << "'\n";
189 }
190
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000191 return 0;
192}