Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringSet.h" |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/CommandFlags.h" |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 17 | #include "llvm/LTO/LTOCodeGenerator.h" |
| 18 | #include "llvm/LTO/LTOModule.h" |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame^] | 20 | #include "llvm/Support/FileSystem.h" |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ManagedStatic.h" |
| 22 | #include "llvm/Support/PrettyStackTrace.h" |
| 23 | #include "llvm/Support/Signals.h" |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 24 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 29 | static cl::opt<bool> |
| 30 | DisableOpt("disable-opt", cl::init(false), |
| 31 | cl::desc("Do not run any optimization passes")); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 32 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 33 | static cl::opt<bool> |
| 34 | DisableInline("disable-inlining", cl::init(false), |
| 35 | cl::desc("Do not run the inliner pass")); |
| 36 | |
| 37 | static cl::opt<bool> |
| 38 | DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false), |
| 39 | cl::desc("Do not run the GVN load PRE pass")); |
| 40 | |
| 41 | static cl::list<std::string> |
| 42 | InputFilenames(cl::Positional, cl::OneOrMore, |
| 43 | cl::desc("<input bitcode files>")); |
| 44 | |
| 45 | static cl::opt<std::string> |
| 46 | OutputFilename("o", cl::init(""), |
| 47 | cl::desc("Override output filename"), |
| 48 | cl::value_desc("filename")); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 49 | |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 50 | static cl::list<std::string> |
| 51 | ExportedSymbols("exported-symbol", |
| 52 | cl::desc("Symbol to export from the resulting object file"), |
| 53 | cl::ZeroOrMore); |
| 54 | |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 55 | static cl::list<std::string> |
| 56 | DSOSymbols("dso-symbol", |
| 57 | cl::desc("Symbol to put in the symtab in the resulting dso"), |
| 58 | cl::ZeroOrMore); |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 59 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 60 | namespace { |
| 61 | struct ModuleInfo { |
| 62 | std::vector<bool> CanBeHidden; |
| 63 | }; |
| 64 | } |
| 65 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 66 | int 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 Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 74 | // Initialize the configured targets. |
| 75 | InitializeAllTargets(); |
| 76 | InitializeAllTargetMCs(); |
| 77 | InitializeAllAsmPrinters(); |
| 78 | InitializeAllAsmParsers(); |
| 79 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 80 | // set up the TargetOptions for the machine |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 81 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 82 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 83 | unsigned BaseArg = 0; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 84 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 85 | LTOCodeGenerator CodeGen; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 86 | |
James Molloy | 951e529 | 2014-04-14 13:54:16 +0000 | [diff] [blame] | 87 | 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 Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 101 | CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 102 | CodeGen.setTargetOptions(Options); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 103 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 104 | 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 Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 110 | for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 111 | std::string error; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 112 | std::unique_ptr<LTOModule> Module( |
| 113 | LTOModule::makeLTOModule(InputFilenames[i].c_str(), Options, error)); |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 114 | if (!error.empty()) { |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 115 | errs() << argv[0] << ": error loading file '" << InputFilenames[i] |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 116 | << "': " << error << "\n"; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 117 | return 1; |
| 118 | } |
| 119 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 120 | |
| 121 | if (!CodeGen.addModule(Module.get(), error)) { |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 122 | errs() << argv[0] << ": error adding file '" << InputFilenames[i] |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 123 | << "': " << error << "\n"; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 124 | return 1; |
| 125 | } |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 126 | |
| 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 Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 139 | // 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 Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 143 | // Add all the dso symbols to the table of symbols to expose. |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 144 | for (unsigned i = 0; i < KeptDSOSyms.size(); ++i) |
| 145 | CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str()); |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 146 | |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 147 | 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 Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 157 | if (!OutputFilename.empty()) { |
| 158 | size_t len = 0; |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 159 | std::string ErrorInfo; |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 160 | const void *Code = CodeGen.compile(&len, DisableOpt, DisableInline, |
| 161 | DisableGVNLoadPRE, ErrorInfo); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 162 | if (!Code) { |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 163 | errs() << argv[0] |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 164 | << ": error compiling the code: " << ErrorInfo << "\n"; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 165 | return 1; |
| 166 | } |
| 167 | |
Rafael Espindola | dc9fe0a | 2013-10-04 21:40:54 +0000 | [diff] [blame] | 168 | raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo, |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 169 | sys::fs::F_None); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 170 | 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 Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 178 | std::string ErrorInfo; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 179 | const char *OutputName = nullptr; |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 180 | if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline, |
| 181 | DisableGVNLoadPRE, ErrorInfo)) { |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 182 | errs() << argv[0] |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 183 | << ": error compiling the code: " << ErrorInfo |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 184 | << "\n"; |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | outs() << "Wrote native object file '" << OutputName << "'\n"; |
| 189 | } |
| 190 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 191 | return 0; |
| 192 | } |