blob: 4a421f9e76611c64b9e3154f8f7a7b691316698f [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"
20#include "llvm/Support/ManagedStatic.h"
21#include "llvm/Support/PrettyStackTrace.h"
22#include "llvm/Support/Signals.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000023#include "llvm/Support/TargetSelect.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000024#include "llvm/Support/raw_ostream.h"
Peter Collingbourne4e380b02013-09-19 22:15:52 +000025
26using namespace llvm;
27
Rafael Espindola0b385c72013-09-30 16:39:19 +000028static cl::opt<bool>
29DisableOpt("disable-opt", cl::init(false),
30 cl::desc("Do not run any optimization passes"));
Peter Collingbourne4e380b02013-09-19 22:15:52 +000031
Rafael Espindola0b385c72013-09-30 16:39:19 +000032static cl::opt<bool>
33DisableInline("disable-inlining", cl::init(false),
34 cl::desc("Do not run the inliner pass"));
35
36static cl::opt<bool>
37DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
38 cl::desc("Do not run the GVN load PRE pass"));
39
40static cl::list<std::string>
41InputFilenames(cl::Positional, cl::OneOrMore,
42 cl::desc("<input bitcode files>"));
43
44static cl::opt<std::string>
45OutputFilename("o", cl::init(""),
46 cl::desc("Override output filename"),
47 cl::value_desc("filename"));
Peter Collingbourne4e380b02013-09-19 22:15:52 +000048
Rafael Espindoladafc53d2013-10-02 14:12:56 +000049static cl::list<std::string>
50ExportedSymbols("exported-symbol",
51 cl::desc("Symbol to export from the resulting object file"),
52 cl::ZeroOrMore);
53
Rafael Espindolacda29112013-10-03 18:29:09 +000054static cl::list<std::string>
55DSOSymbols("dso-symbol",
56 cl::desc("Symbol to put in the symtab in the resulting dso"),
57 cl::ZeroOrMore);
Rafael Espindoladafc53d2013-10-02 14:12:56 +000058
Rafael Espindola282a4702013-10-31 20:51:58 +000059namespace {
60struct ModuleInfo {
61 std::vector<bool> CanBeHidden;
62};
63}
64
Peter Collingbourne4e380b02013-09-19 22:15:52 +000065int main(int argc, char **argv) {
66 // Print a stack trace if we signal out.
67 sys::PrintStackTraceOnErrorSignal();
68 PrettyStackTraceProgram X(argc, argv);
69
70 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
71 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
72
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000073 // Initialize the configured targets.
74 InitializeAllTargets();
75 InitializeAllTargetMCs();
76 InitializeAllAsmPrinters();
77 InitializeAllAsmParsers();
78
Rafael Espindola0b385c72013-09-30 16:39:19 +000079 // set up the TargetOptions for the machine
80 TargetOptions Options;
81 Options.LessPreciseFPMADOption = EnableFPMAD;
82 Options.NoFramePointerElim = DisableFPElim;
83 Options.AllowFPOpFusion = FuseFPOps;
84 Options.UnsafeFPMath = EnableUnsafeFPMath;
85 Options.NoInfsFPMath = EnableNoInfsFPMath;
86 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
87 Options.HonorSignDependentRoundingFPMathOption =
88 EnableHonorSignDependentRoundingFPMath;
89 Options.UseSoftFloat = GenerateSoftFloatCalls;
90 if (FloatABIForCalls != FloatABI::Default)
91 Options.FloatABIType = FloatABIForCalls;
92 Options.NoZerosInBSS = DontPlaceZerosInBSS;
93 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
94 Options.DisableTailCalls = DisableTailCalls;
95 Options.StackAlignmentOverride = OverrideStackAlignment;
96 Options.TrapFuncName = TrapFuncName;
97 Options.PositionIndependentExecutable = EnablePIE;
98 Options.EnableSegmentedStacks = SegmentedStacks;
99 Options.UseInitArray = UseInitArray;
100
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000101 unsigned BaseArg = 0;
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000102
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000103 LTOCodeGenerator CodeGen;
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000104
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000105 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
106 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
Rafael Espindola0b385c72013-09-30 16:39:19 +0000107 CodeGen.setTargetOptions(Options);
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000108
Rafael Espindola282a4702013-10-31 20:51:58 +0000109 llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet;
110 for (unsigned i = 0; i < DSOSymbols.size(); ++i)
111 DSOSymbolsSet.insert(DSOSymbols[i]);
112
113 std::vector<std::string> KeptDSOSyms;
114
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000115 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000116 std::string error;
117 OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(),
Rafael Espindola0b385c72013-09-30 16:39:19 +0000118 Options, error));
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000119 if (!error.empty()) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000120 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000121 << "': " << error << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000122 return 1;
123 }
124
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000125
126 if (!CodeGen.addModule(Module.get(), error)) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000127 errs() << argv[0] << ": error adding file '" << InputFilenames[i]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000128 << "': " << error << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000129 return 1;
130 }
Rafael Espindola282a4702013-10-31 20:51:58 +0000131
132 unsigned NumSyms = Module->getSymbolCount();
133 for (unsigned I = 0; I < NumSyms; ++I) {
134 StringRef Name = Module->getSymbolName(I);
135 if (!DSOSymbolsSet.count(Name))
136 continue;
137 lto_symbol_attributes Attrs = Module->getSymbolAttributes(I);
138 unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;
139 if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)
140 KeptDSOSyms.push_back(Name);
141 }
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000142 }
143
Rafael Espindoladafc53d2013-10-02 14:12:56 +0000144 // Add all the exported symbols to the table of symbols to preserve.
145 for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
146 CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
147
Rafael Espindolacda29112013-10-03 18:29:09 +0000148 // Add all the dso symbols to the table of symbols to expose.
Rafael Espindola282a4702013-10-31 20:51:58 +0000149 for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)
150 CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str());
Rafael Espindolacda29112013-10-03 18:29:09 +0000151
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000152 if (!OutputFilename.empty()) {
153 size_t len = 0;
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000154 std::string ErrorInfo;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000155 const void *Code = CodeGen.compile(&len, DisableOpt, DisableInline,
156 DisableGVNLoadPRE, ErrorInfo);
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000157 if (Code == NULL) {
158 errs() << argv[0]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000159 << ": error compiling the code: " << ErrorInfo << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000160 return 1;
161 }
162
Rafael Espindoladc9fe0a2013-10-04 21:40:54 +0000163 raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo,
164 sys::fs::F_Binary);
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000165 if (!ErrorInfo.empty()) {
166 errs() << argv[0] << ": error opening the file '" << OutputFilename
167 << "': " << ErrorInfo << "\n";
168 return 1;
169 }
170
171 FileStream.write(reinterpret_cast<const char *>(Code), len);
172 } else {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000173 std::string ErrorInfo;
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000174 const char *OutputName = NULL;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000175 if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline,
176 DisableGVNLoadPRE, ErrorInfo)) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000177 errs() << argv[0]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000178 << ": error compiling the code: " << ErrorInfo
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000179 << "\n";
180 return 1;
181 }
182
183 outs() << "Wrote native object file '" << OutputName << "'\n";
184 }
185
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000186 return 0;
187}