Add a -exported-symbol option to llvm-lto.
[oota-llvm.git] / tools / llvm-lto / llvm-lto.cpp
1 //===-- 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
15 #include "llvm/CodeGen/CommandFlags.h"
16 #include "llvm/LTO/LTOCodeGenerator.h"
17 #include "llvm/LTO/LTOModule.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/PrettyStackTrace.h"
21 #include "llvm/Support/Signals.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Support/TargetSelect.h"
24
25 using namespace llvm;
26
27 static cl::opt<bool>
28 DisableOpt("disable-opt", cl::init(false),
29   cl::desc("Do not run any optimization passes"));
30
31 static cl::opt<bool>
32 DisableInline("disable-inlining", cl::init(false),
33   cl::desc("Do not run the inliner pass"));
34
35 static cl::opt<bool>
36 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
37   cl::desc("Do not run the GVN load PRE pass"));
38
39 static cl::list<std::string>
40 InputFilenames(cl::Positional, cl::OneOrMore,
41   cl::desc("<input bitcode files>"));
42
43 static cl::opt<std::string>
44 OutputFilename("o", cl::init(""),
45   cl::desc("Override output filename"),
46   cl::value_desc("filename"));
47
48 static cl::list<std::string>
49 ExportedSymbols("exported-symbol",
50   cl::desc("Symbol to export from the resulting object file"),
51   cl::ZeroOrMore);
52
53
54 int main(int argc, char **argv) {
55   // Print a stack trace if we signal out.
56   sys::PrintStackTraceOnErrorSignal();
57   PrettyStackTraceProgram X(argc, argv);
58
59   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
60   cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
61
62   // Initialize the configured targets.
63   InitializeAllTargets();
64   InitializeAllTargetMCs();
65   InitializeAllAsmPrinters();
66   InitializeAllAsmParsers();
67
68   // set up the TargetOptions for the machine
69   TargetOptions Options;
70   Options.LessPreciseFPMADOption = EnableFPMAD;
71   Options.NoFramePointerElim = DisableFPElim;
72   Options.AllowFPOpFusion = FuseFPOps;
73   Options.UnsafeFPMath = EnableUnsafeFPMath;
74   Options.NoInfsFPMath = EnableNoInfsFPMath;
75   Options.NoNaNsFPMath = EnableNoNaNsFPMath;
76   Options.HonorSignDependentRoundingFPMathOption =
77     EnableHonorSignDependentRoundingFPMath;
78   Options.UseSoftFloat = GenerateSoftFloatCalls;
79   if (FloatABIForCalls != FloatABI::Default)
80     Options.FloatABIType = FloatABIForCalls;
81   Options.NoZerosInBSS = DontPlaceZerosInBSS;
82   Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
83   Options.DisableTailCalls = DisableTailCalls;
84   Options.StackAlignmentOverride = OverrideStackAlignment;
85   Options.TrapFuncName = TrapFuncName;
86   Options.PositionIndependentExecutable = EnablePIE;
87   Options.EnableSegmentedStacks = SegmentedStacks;
88   Options.UseInitArray = UseInitArray;
89
90   unsigned BaseArg = 0;
91
92   LTOCodeGenerator CodeGen;
93
94   CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
95   CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
96   CodeGen.setTargetOptions(Options);
97
98   for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
99     std::string error;
100     OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(),
101                                                          Options, error));
102     if (!error.empty()) {
103       errs() << argv[0] << ": error loading file '" << InputFilenames[i]
104              << "': " << error << "\n";
105       return 1;
106     }
107
108
109     if (!CodeGen.addModule(Module.get(), error)) {
110       errs() << argv[0] << ": error adding file '" << InputFilenames[i]
111              << "': " << error << "\n";
112       return 1;
113     }
114   }
115
116   // Add all the exported symbols to the table of symbols to preserve.
117   for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
118     CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
119
120   if (!OutputFilename.empty()) {
121     size_t len = 0;
122     std::string ErrorInfo;
123     const void *Code = CodeGen.compile(&len, DisableOpt, DisableInline,
124                                        DisableGVNLoadPRE, ErrorInfo);
125     if (Code == NULL) {
126       errs() << argv[0]
127              << ": error compiling the code: " << ErrorInfo << "\n";
128       return 1;
129     }
130
131     raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo);
132     if (!ErrorInfo.empty()) {
133       errs() << argv[0] << ": error opening the file '" << OutputFilename
134              << "': " << ErrorInfo << "\n";
135       return 1;
136     }
137
138     FileStream.write(reinterpret_cast<const char *>(Code), len);
139   } else {
140     std::string ErrorInfo;
141     const char *OutputName = NULL;
142     if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline,
143                                  DisableGVNLoadPRE, ErrorInfo)) {
144       errs() << argv[0]
145              << ": error compiling the code: " << ErrorInfo
146              << "\n";
147       return 1;
148     }
149
150     outs() << "Wrote native object file '" << OutputName << "'\n";
151   }
152
153   return 0;
154 }