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" |
| 20 | #include "llvm/Support/ManagedStatic.h" |
| 21 | #include "llvm/Support/PrettyStackTrace.h" |
| 22 | #include "llvm/Support/Signals.h" |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 23 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 28 | static cl::opt<bool> |
| 29 | DisableOpt("disable-opt", cl::init(false), |
| 30 | cl::desc("Do not run any optimization passes")); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 31 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 32 | static cl::opt<bool> |
| 33 | DisableInline("disable-inlining", cl::init(false), |
| 34 | cl::desc("Do not run the inliner pass")); |
| 35 | |
| 36 | static cl::opt<bool> |
| 37 | DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false), |
| 38 | cl::desc("Do not run the GVN load PRE pass")); |
| 39 | |
| 40 | static cl::list<std::string> |
| 41 | InputFilenames(cl::Positional, cl::OneOrMore, |
| 42 | cl::desc("<input bitcode files>")); |
| 43 | |
| 44 | static cl::opt<std::string> |
| 45 | OutputFilename("o", cl::init(""), |
| 46 | cl::desc("Override output filename"), |
| 47 | cl::value_desc("filename")); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 48 | |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 49 | static cl::list<std::string> |
| 50 | ExportedSymbols("exported-symbol", |
| 51 | cl::desc("Symbol to export from the resulting object file"), |
| 52 | cl::ZeroOrMore); |
| 53 | |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 54 | static cl::list<std::string> |
| 55 | DSOSymbols("dso-symbol", |
| 56 | cl::desc("Symbol to put in the symtab in the resulting dso"), |
| 57 | cl::ZeroOrMore); |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 58 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 59 | namespace { |
| 60 | struct ModuleInfo { |
| 61 | std::vector<bool> CanBeHidden; |
| 62 | }; |
| 63 | } |
| 64 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 65 | int 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 Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 73 | // Initialize the configured targets. |
| 74 | InitializeAllTargets(); |
| 75 | InitializeAllTargetMCs(); |
| 76 | InitializeAllAsmPrinters(); |
| 77 | InitializeAllAsmParsers(); |
| 78 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 79 | // set up the TargetOptions for the machine |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 80 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 81 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 82 | unsigned BaseArg = 0; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 83 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 84 | LTOCodeGenerator CodeGen; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 85 | |
James Molloy | 951e529 | 2014-04-14 13:54:16 +0000 | [diff] [blame^] | 86 | switch (RelocModel) { |
| 87 | case Reloc::Static: |
| 88 | CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_STATIC); |
| 89 | break; |
| 90 | case Reloc::PIC_: |
| 91 | CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC); |
| 92 | break; |
| 93 | case Reloc::DynamicNoPIC: |
| 94 | CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC); |
| 95 | break; |
| 96 | default: |
| 97 | CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DEFAULT); |
| 98 | } |
| 99 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 100 | CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 101 | CodeGen.setTargetOptions(Options); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 102 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 103 | llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet; |
| 104 | for (unsigned i = 0; i < DSOSymbols.size(); ++i) |
| 105 | DSOSymbolsSet.insert(DSOSymbols[i]); |
| 106 | |
| 107 | std::vector<std::string> KeptDSOSyms; |
| 108 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 109 | for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 110 | std::string error; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 111 | std::unique_ptr<LTOModule> Module( |
| 112 | LTOModule::makeLTOModule(InputFilenames[i].c_str(), Options, error)); |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 113 | if (!error.empty()) { |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 114 | errs() << argv[0] << ": error loading file '" << InputFilenames[i] |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 115 | << "': " << error << "\n"; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 116 | return 1; |
| 117 | } |
| 118 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 119 | |
| 120 | if (!CodeGen.addModule(Module.get(), error)) { |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 121 | errs() << argv[0] << ": error adding file '" << InputFilenames[i] |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 122 | << "': " << error << "\n"; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 123 | return 1; |
| 124 | } |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 125 | |
| 126 | unsigned NumSyms = Module->getSymbolCount(); |
| 127 | for (unsigned I = 0; I < NumSyms; ++I) { |
| 128 | StringRef Name = Module->getSymbolName(I); |
| 129 | if (!DSOSymbolsSet.count(Name)) |
| 130 | continue; |
| 131 | lto_symbol_attributes Attrs = Module->getSymbolAttributes(I); |
| 132 | unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK; |
| 133 | if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN) |
| 134 | KeptDSOSyms.push_back(Name); |
| 135 | } |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 138 | // Add all the exported symbols to the table of symbols to preserve. |
| 139 | for (unsigned i = 0; i < ExportedSymbols.size(); ++i) |
| 140 | CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str()); |
| 141 | |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 142 | // Add all the dso symbols to the table of symbols to expose. |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 143 | for (unsigned i = 0; i < KeptDSOSyms.size(); ++i) |
| 144 | CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str()); |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 145 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 146 | if (!OutputFilename.empty()) { |
| 147 | size_t len = 0; |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 148 | std::string ErrorInfo; |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 149 | const void *Code = CodeGen.compile(&len, DisableOpt, DisableInline, |
| 150 | DisableGVNLoadPRE, ErrorInfo); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 151 | if (Code == NULL) { |
| 152 | errs() << argv[0] |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 153 | << ": error compiling the code: " << ErrorInfo << "\n"; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 154 | return 1; |
| 155 | } |
| 156 | |
Rafael Espindola | dc9fe0a | 2013-10-04 21:40:54 +0000 | [diff] [blame] | 157 | raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo, |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 158 | sys::fs::F_None); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 159 | if (!ErrorInfo.empty()) { |
| 160 | errs() << argv[0] << ": error opening the file '" << OutputFilename |
| 161 | << "': " << ErrorInfo << "\n"; |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | FileStream.write(reinterpret_cast<const char *>(Code), len); |
| 166 | } else { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 167 | std::string ErrorInfo; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 168 | const char *OutputName = NULL; |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 169 | if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline, |
| 170 | DisableGVNLoadPRE, ErrorInfo)) { |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 171 | errs() << argv[0] |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 172 | << ": error compiling the code: " << ErrorInfo |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 173 | << "\n"; |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | outs() << "Wrote native object file '" << OutputName << "'\n"; |
| 178 | } |
| 179 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |