| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 1 | //===- LTO.cpp ------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "LTO.h" |
| 11 | #include "Config.h" |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 12 | #include "InputFiles.h" |
| Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 +0000 | [diff] [blame] | 13 | #include "LinkerScript.h" |
| Rafael Espindola | 4b075bb | 2017-07-26 23:39:10 +0000 | [diff] [blame] | 14 | #include "SymbolTable.h" |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 15 | #include "Symbols.h" |
| Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 16 | #include "lld/Common/ErrorHandler.h" |
| Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 +0000 | [diff] [blame] | 17 | #include "lld/Common/TargetOptionsCommandFlags.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
| Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/ADT/Twine.h" |
| Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 22 | #include "llvm/BinaryFormat/ELF.h" |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DiagnosticPrinter.h" |
| Peter Collingbourne | e02775f | 2017-03-01 23:00:10 +0000 | [diff] [blame] | 24 | #include "llvm/LTO/Caching.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 25 | #include "llvm/LTO/Config.h" |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 26 | #include "llvm/LTO/LTO.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 27 | #include "llvm/Object/SymbolicFile.h" |
| 28 | #include "llvm/Support/CodeGen.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Error.h" |
| 30 | #include "llvm/Support/FileSystem.h" |
| 31 | #include "llvm/Support/MemoryBuffer.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | #include <algorithm> |
| 34 | #include <cstddef> |
| 35 | #include <memory> |
| 36 | #include <string> |
| 37 | #include <system_error> |
| 38 | #include <vector> |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace llvm; |
| 41 | using namespace llvm::object; |
| 42 | using namespace llvm::ELF; |
| 43 | |
| 44 | using namespace lld; |
| 45 | using namespace lld::elf; |
| 46 | |
| 47 | // This is for use when debugging LTO. |
| Rui Ueyama | 48da5cf | 2016-07-15 02:17:13 +0000 | [diff] [blame] | 48 | static void saveBuffer(StringRef Buffer, const Twine &Path) { |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 49 | std::error_code EC; |
| Rui Ueyama | 48da5cf | 2016-07-15 02:17:13 +0000 | [diff] [blame] | 50 | raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None); |
| Rui Ueyama | f8292e9 | 2016-07-15 02:01:03 +0000 | [diff] [blame] | 51 | if (EC) |
| Rui Ueyama | c8d3a83 | 2017-01-12 22:18:04 +0000 | [diff] [blame] | 52 | error("cannot create " + Path + ": " + EC.message()); |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 53 | OS << Buffer; |
| 54 | } |
| 55 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 56 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 57 | SmallString<128> ErrStorage; |
| 58 | raw_svector_ostream OS(ErrStorage); |
| 59 | DiagnosticPrinterRawOStream DP(OS); |
| 60 | DI.print(DP); |
| Rui Ueyama | d31e13f | 2016-09-29 21:00:23 +0000 | [diff] [blame] | 61 | warn(ErrStorage); |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| Rui Ueyama | 6c5cbff | 2016-09-29 21:00:26 +0000 | [diff] [blame] | 64 | static void checkError(Error E) { |
| Rafael Espindola | 5329c75 | 2017-09-21 22:50:52 +0000 | [diff] [blame] | 65 | handleAllErrors(std::move(E), |
| 66 | [&](ErrorInfoBase &EIB) { error(EIB.message()); }); |
| Rui Ueyama | 6c5cbff | 2016-09-29 21:00:26 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 69 | static std::unique_ptr<lto::LTO> createLTO() { |
| 70 | lto::Config Conf; |
| Davide Italiano | d26c4a1 | 2016-05-15 19:29:38 +0000 | [diff] [blame] | 71 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 72 | // LLD supports the new relocations. |
| 73 | Conf.Options = InitTargetOptionsFromCodeGenFlags(); |
| 74 | Conf.Options.RelaxELFRelocations = true; |
| Davide Italiano | df24d5b | 2016-06-02 22:58:11 +0000 | [diff] [blame] | 75 | |
| Davide Italiano | 957f120 | 2017-07-24 20:15:07 +0000 | [diff] [blame] | 76 | // Always emit a section per function/datum with LTO. |
| Davide Italiano | 1f4e29c | 2017-07-24 19:38:13 +0000 | [diff] [blame] | 77 | Conf.Options.FunctionSections = true; |
| Davide Italiano | 957f120 | 2017-07-24 20:15:07 +0000 | [diff] [blame] | 78 | Conf.Options.DataSections = true; |
| Davide Italiano | 1f4e29c | 2017-07-24 19:38:13 +0000 | [diff] [blame] | 79 | |
| Evgeniy Stepanov | f12ac5b | 2017-05-22 21:11:44 +0000 | [diff] [blame] | 80 | if (Config->Relocatable) |
| 81 | Conf.RelocModel = None; |
| 82 | else if (Config->Pic) |
| 83 | Conf.RelocModel = Reloc::PIC_; |
| 84 | else |
| 85 | Conf.RelocModel = Reloc::Static; |
| Martell Malone | 6b43b7a | 2017-02-28 23:43:26 +0000 | [diff] [blame] | 86 | Conf.CodeModel = GetCodeModelFromCMModel(); |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 87 | Conf.DisableVerify = Config->DisableVerify; |
| 88 | Conf.DiagHandler = diagnosticHandler; |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 89 | Conf.OptLevel = Config->LTOO; |
| Rafael Espindola | 3a730d8 | 2018-01-30 18:18:59 +0000 | [diff] [blame] | 90 | Conf.CPU = GetCPUStr(); |
| Davide Italiano | df24d5b | 2016-06-02 22:58:11 +0000 | [diff] [blame] | 91 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 92 | // Set up a custom pipeline if we've been asked to. |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 93 | Conf.OptPipeline = Config->LTONewPmPasses; |
| 94 | Conf.AAPipeline = Config->LTOAAPipeline; |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 95 | |
| Davide Italiano | db4b0a7 | 2017-02-13 17:49:18 +0000 | [diff] [blame] | 96 | // Set up optimization remarks if we've been asked to. |
| 97 | Conf.RemarksFilename = Config->OptRemarksFilename; |
| 98 | Conf.RemarksWithHotness = Config->OptRemarksWithHotness; |
| 99 | |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 100 | if (Config->SaveTemps) |
| Rui Ueyama | 6c5cbff | 2016-09-29 21:00:26 +0000 | [diff] [blame] | 101 | checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".", |
| George Rimar | a4c7e74 | 2016-10-20 08:36:42 +0000 | [diff] [blame] | 102 | /*UseInputModulePath*/ true)); |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 103 | |
| Davide Italiano | 7a7b35a | 2016-10-10 18:12:53 +0000 | [diff] [blame] | 104 | lto::ThinBackend Backend; |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 105 | if (Config->ThinLTOJobs != -1u) |
| 106 | Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs); |
| Davide Italiano | b6e6e4a | 2016-10-10 23:12:14 +0000 | [diff] [blame] | 107 | return llvm::make_unique<lto::LTO>(std::move(Conf), Backend, |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 108 | Config->LTOPartitions); |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| Rafael Espindola | 4b075bb | 2017-07-26 23:39:10 +0000 | [diff] [blame] | 111 | BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) { |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 112 | for (Symbol *Sym : Symtab->getSymbols()) { |
| Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 113 | StringRef Name = Sym->getName(); |
| Rafael Espindola | 4b075bb | 2017-07-26 23:39:10 +0000 | [diff] [blame] | 114 | for (StringRef Prefix : {"__start_", "__stop_"}) |
| 115 | if (Name.startswith(Prefix)) |
| 116 | UsedStartStop.insert(Name.substr(Prefix.size())); |
| 117 | } |
| 118 | } |
| Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame] | 119 | |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 120 | BitcodeCompiler::~BitcodeCompiler() = default; |
| Rui Ueyama | 412c802 | 2016-04-22 21:16:18 +0000 | [diff] [blame] | 121 | |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 122 | static void undefine(Symbol *S) { |
| Rafael Espindola | bec3765 | 2017-11-17 01:37:50 +0000 | [diff] [blame] | 123 | replaceSymbol<Undefined>(S, nullptr, S->getName(), STB_GLOBAL, STV_DEFAULT, |
| 124 | S->Type); |
| Peter Collingbourne | 0ef3874 | 2016-05-12 19:46:14 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 127 | void BitcodeCompiler::add(BitcodeFile &F) { |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 128 | lto::InputFile &Obj = *F.Obj; |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 129 | unsigned SymNum = 0; |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 130 | std::vector<Symbol *> Syms = F.getSymbols(); |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 131 | std::vector<lto::SymbolResolution> Resols(Syms.size()); |
| Davide Italiano | 334fce9 | 2016-05-11 01:07:22 +0000 | [diff] [blame] | 132 | |
| Rafael Espindola | c6df38c | 2018-01-16 16:49:05 +0000 | [diff] [blame] | 133 | bool IsExecutable = !Config->Shared && !Config->Relocatable; |
| 134 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 135 | // Provide a resolution to the LTO API for each symbol. |
| 136 | for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) { |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 137 | Symbol *Sym = Syms[SymNum]; |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 138 | lto::SymbolResolution &R = Resols[SymNum]; |
| 139 | ++SymNum; |
| Davide Italiano | 86f2bd5 | 2016-03-29 21:46:35 +0000 | [diff] [blame] | 140 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 141 | // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile |
| 142 | // reports two symbols for module ASM defined. Without this check, lld |
| 143 | // flags an undefined in IR with a definition in ASM as prevailing. |
| 144 | // Once IRObjectFile is fixed to report only one symbol this hack can |
| 145 | // be removed. |
| Rafael Espindola | dfebd36 | 2017-11-29 22:47:35 +0000 | [diff] [blame] | 146 | R.Prevailing = !ObjSym.isUndefined() && Sym->File == &F; |
| Peter Collingbourne | 3ad1c1e | 2016-05-05 17:13:49 +0000 | [diff] [blame] | 147 | |
| George Rimar | 3a1af22 | 2017-08-22 08:36:54 +0000 | [diff] [blame] | 148 | // We ask LTO to preserve following global symbols: |
| 149 | // 1) All symbols when doing relocatable link, so that them can be used |
| 150 | // for doing final link. |
| 151 | // 2) Symbols that are used in regular objects. |
| 152 | // 3) C named sections if we have corresponding __start_/__stop_ symbol. |
| 153 | // 4) Symbols that are defined in bitcode files and used for dynamic linking. |
| 154 | R.VisibleToRegularObj = Config->Relocatable || Sym->IsUsedInRegularObj || |
| Rafael Espindola | affe720 | 2017-07-25 22:51:05 +0000 | [diff] [blame] | 155 | (R.Prevailing && Sym->includeInDynsym()) || |
| Rafael Espindola | 4b075bb | 2017-07-26 23:39:10 +0000 | [diff] [blame] | 156 | UsedStartStop.count(ObjSym.getSectionName()); |
| Dmitry Mikulin | c84e0ee | 2018-02-07 00:49:51 +0000 | [diff] [blame] | 157 | const auto *DR = dyn_cast<Defined>(Sym); |
| Rafael Espindola | c6df38c | 2018-01-16 16:49:05 +0000 | [diff] [blame] | 158 | R.FinalDefinitionInLinkageUnit = |
| Dmitry Mikulin | c84e0ee | 2018-02-07 00:49:51 +0000 | [diff] [blame] | 159 | (IsExecutable || Sym->Visibility != STV_DEFAULT) && DR && |
| 160 | // Skip absolute symbols from ELF objects, otherwise PC-rel relocations |
| 161 | // will be generated by for them, triggering linker errors. |
| 162 | // Symbol section is always null for bitcode symbols, hence the check |
| Dmitry Mikulin | 8ddd922 | 2018-02-08 04:25:52 +0000 | [diff] [blame] | 163 | // for isElf(). Skip linker script defined symbols as well: they have |
| 164 | // no File defined. |
| 165 | !(DR->Section == nullptr && (!Sym->File || Sym->File->isElf())); |
| Rafael Espindola | c6df38c | 2018-01-16 16:49:05 +0000 | [diff] [blame] | 166 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 167 | if (R.Prevailing) |
| Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 168 | undefine(Sym); |
| George Rimar | d28c26b | 2017-09-25 09:31:43 +0000 | [diff] [blame] | 169 | |
| George Rimar | c4ccfb5 | 2018-01-30 09:04:27 +0000 | [diff] [blame] | 170 | // We tell LTO to not apply interprocedural optimization for wrapped |
| 171 | // (with --wrap) symbols because otherwise LTO would inline them while |
| 172 | // their values are still not final. |
| 173 | R.LinkerRedefined = !Sym->CanInline; |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 174 | } |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 175 | checkError(LTOObj->add(std::move(F.Obj), Resols)); |
| Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 178 | // Merge all the bitcode files we have seen, codegen the result |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 179 | // and return the resulting ObjectFile(s). |
| Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 180 | std::vector<InputFile *> BitcodeCompiler::compile() { |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 181 | std::vector<InputFile *> Ret; |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 182 | unsigned MaxTasks = LTOObj->getMaxTasks(); |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 183 | Buff.resize(MaxTasks); |
| Peter Collingbourne | e02775f | 2017-03-01 23:00:10 +0000 | [diff] [blame] | 184 | Files.resize(MaxTasks); |
| Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 185 | |
| Peter Collingbourne | e02775f | 2017-03-01 23:00:10 +0000 | [diff] [blame] | 186 | // The --thinlto-cache-dir option specifies the path to a directory in which |
| 187 | // to cache native object files for ThinLTO incremental builds. If a path was |
| 188 | // specified, configure LTO to use it as the cache directory. |
| 189 | lto::NativeObjectCache Cache; |
| 190 | if (!Config->ThinLTOCacheDir.empty()) |
| Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 191 | Cache = check( |
| 192 | lto::localCache(Config->ThinLTOCacheDir, |
| Teresa Johnson | 2c2ed3c | 2018-02-20 20:21:59 +0000 | [diff] [blame^] | 193 | [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) { |
| 194 | Files[Task] = std::move(MB); |
| 195 | })); |
| Peter Collingbourne | e02775f | 2017-03-01 23:00:10 +0000 | [diff] [blame] | 196 | |
| 197 | checkError(LTOObj->run( |
| 198 | [&](size_t Task) { |
| 199 | return llvm::make_unique<lto::NativeObjectStream>( |
| 200 | llvm::make_unique<raw_svector_ostream>(Buff[Task])); |
| 201 | }, |
| 202 | Cache)); |
| Rafael Espindola | abf6c65 | 2016-04-17 23:20:08 +0000 | [diff] [blame] | 203 | |
| Peter Collingbourne | ee59e43 | 2017-03-17 02:24:16 +0000 | [diff] [blame] | 204 | if (!Config->ThinLTOCacheDir.empty()) |
| 205 | pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy); |
| 206 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 207 | for (unsigned I = 0; I != MaxTasks; ++I) { |
| 208 | if (Buff[I].empty()) |
| 209 | continue; |
| 210 | if (Config->SaveTemps) { |
| Peter Collingbourne | d22ec64 | 2017-01-26 02:18:28 +0000 | [diff] [blame] | 211 | if (I == 0) |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 212 | saveBuffer(Buff[I], Config->OutputFile + ".lto.o"); |
| 213 | else |
| 214 | saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.o"); |
| 215 | } |
| Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 216 | InputFile *Obj = createObjectFile(MemoryBufferRef(Buff[I], "lto.tmp")); |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 217 | Ret.push_back(Obj); |
| 218 | } |
| Peter Collingbourne | e02775f | 2017-03-01 23:00:10 +0000 | [diff] [blame] | 219 | |
| 220 | for (std::unique_ptr<MemoryBuffer> &File : Files) |
| 221 | if (File) |
| 222 | Ret.push_back(createObjectFile(*File)); |
| 223 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 224 | return Ret; |
| Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 +0000 | [diff] [blame] | 225 | } |