[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / tools / gold / gold-plugin.cpp
blob0f720b6be23a4fd0d80941345c9425072874d7ae
1 //===-- gold-plugin.cpp - Plugin to gold for Link Time Optimization ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is a gold plugin for LLVM. It provides an LLVM implementation of the
10 // interface described in http://gcc.gnu.org/wiki/whopr/driver .
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/Bitcode/BitcodeReader.h"
16 #include "llvm/Bitcode/BitcodeWriter.h"
17 #include "llvm/CodeGen/CommandFlags.inc"
18 #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/LTO/Caching.h"
22 #include "llvm/LTO/LTO.h"
23 #include "llvm/Object/Error.h"
24 #include "llvm/Support/CachePruning.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/ManagedStatic.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/TargetSelect.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <list>
33 #include <map>
34 #include <plugin-api.h>
35 #include <string>
36 #include <system_error>
37 #include <utility>
38 #include <vector>
40 // FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and
41 // Precise and Debian Wheezy (binutils 2.23 is required)
42 #define LDPO_PIE 3
44 #define LDPT_GET_SYMBOLS_V3 28
46 // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
47 // required version.
48 #define LDPT_GET_WRAP_SYMBOLS 32
50 using namespace llvm;
51 using namespace lto;
53 // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
54 // required version.
55 typedef enum ld_plugin_status (*ld_plugin_get_wrap_symbols)(
56 uint64_t *num_symbols, const char ***wrap_symbol_list);
58 static ld_plugin_status discard_message(int level, const char *format, ...) {
59 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
60 // callback in the transfer vector. This should never be called.
61 abort();
64 static ld_plugin_release_input_file release_input_file = nullptr;
65 static ld_plugin_get_input_file get_input_file = nullptr;
66 static ld_plugin_message message = discard_message;
67 static ld_plugin_get_wrap_symbols get_wrap_symbols = nullptr;
69 namespace {
70 struct claimed_file {
71 void *handle;
72 void *leader_handle;
73 std::vector<ld_plugin_symbol> syms;
74 off_t filesize;
75 std::string name;
78 /// RAII wrapper to manage opening and releasing of a ld_plugin_input_file.
79 struct PluginInputFile {
80 void *Handle;
81 std::unique_ptr<ld_plugin_input_file> File;
83 PluginInputFile(void *Handle) : Handle(Handle) {
84 File = std::make_unique<ld_plugin_input_file>();
85 if (get_input_file(Handle, File.get()) != LDPS_OK)
86 message(LDPL_FATAL, "Failed to get file information");
88 ~PluginInputFile() {
89 // File would have been reset to nullptr if we moved this object
90 // to a new owner.
91 if (File)
92 if (release_input_file(Handle) != LDPS_OK)
93 message(LDPL_FATAL, "Failed to release file information");
96 ld_plugin_input_file &file() { return *File; }
98 PluginInputFile(PluginInputFile &&RHS) = default;
99 PluginInputFile &operator=(PluginInputFile &&RHS) = default;
102 struct ResolutionInfo {
103 bool CanOmitFromDynSym = true;
104 bool DefaultVisibility = true;
105 bool CanInline = true;
106 bool IsUsedInRegularObj = false;
111 static ld_plugin_add_symbols add_symbols = nullptr;
112 static ld_plugin_get_symbols get_symbols = nullptr;
113 static ld_plugin_add_input_file add_input_file = nullptr;
114 static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
115 static ld_plugin_get_view get_view = nullptr;
116 static bool IsExecutable = false;
117 static bool SplitSections = true;
118 static Optional<Reloc::Model> RelocationModel = None;
119 static std::string output_name = "";
120 static std::list<claimed_file> Modules;
121 static DenseMap<int, void *> FDToLeaderHandle;
122 static StringMap<ResolutionInfo> ResInfo;
123 static std::vector<std::string> Cleanup;
125 namespace options {
126 enum OutputType {
127 OT_NORMAL,
128 OT_DISABLE,
129 OT_BC_ONLY,
130 OT_ASM_ONLY,
131 OT_SAVE_TEMPS
133 static OutputType TheOutputType = OT_NORMAL;
134 static unsigned OptLevel = 2;
135 // Default parallelism of 0 used to indicate that user did not specify.
136 // Actual parallelism default value depends on implementation.
137 // Currently only affects ThinLTO, where the default is
138 // llvm::heavyweight_hardware_concurrency.
139 static unsigned Parallelism = 0;
140 // Default regular LTO codegen parallelism (number of partitions).
141 static unsigned ParallelCodeGenParallelismLevel = 1;
142 #ifdef NDEBUG
143 static bool DisableVerify = true;
144 #else
145 static bool DisableVerify = false;
146 #endif
147 static std::string obj_path;
148 static std::string extra_library_path;
149 static std::string triple;
150 static std::string mcpu;
151 // When the thinlto plugin option is specified, only read the function
152 // the information from intermediate files and write a combined
153 // global index for the ThinLTO backends.
154 static bool thinlto = false;
155 // If false, all ThinLTO backend compilations through code gen are performed
156 // using multiple threads in the gold-plugin, before handing control back to
157 // gold. If true, write individual backend index files which reflect
158 // the import decisions, and exit afterwards. The assumption is
159 // that the build system will launch the backend processes.
160 static bool thinlto_index_only = false;
161 // If non-empty, holds the name of a file in which to write the list of
162 // oject files gold selected for inclusion in the link after symbol
163 // resolution (i.e. they had selected symbols). This will only be non-empty
164 // in the thinlto_index_only case. It is used to identify files, which may
165 // have originally been within archive libraries specified via
166 // --start-lib/--end-lib pairs, that should be included in the final
167 // native link process (since intervening function importing and inlining
168 // may change the symbol resolution detected in the final link and which
169 // files to include out of --start-lib/--end-lib libraries as a result).
170 static std::string thinlto_linked_objects_file;
171 // If true, when generating individual index files for distributed backends,
172 // also generate a "${bitcodefile}.imports" file at the same location for each
173 // bitcode file, listing the files it imports from in plain text. This is to
174 // support distributed build file staging.
175 static bool thinlto_emit_imports_files = false;
176 // Option to control where files for a distributed backend (the individual
177 // index files and optional imports files) are created.
178 // If specified, expects a string of the form "oldprefix:newprefix", and
179 // instead of generating these files in the same directory path as the
180 // corresponding bitcode file, will use a path formed by replacing the
181 // bitcode file's path prefix matching oldprefix with newprefix.
182 static std::string thinlto_prefix_replace;
183 // Option to control the name of modules encoded in the individual index
184 // files for a distributed backend. This enables the use of minimized
185 // bitcode files for the thin link, assuming the name of the full bitcode
186 // file used in the backend differs just in some part of the file suffix.
187 // If specified, expects a string of the form "oldsuffix:newsuffix".
188 static std::string thinlto_object_suffix_replace;
189 // Optional path to a directory for caching ThinLTO objects.
190 static std::string cache_dir;
191 // Optional pruning policy for ThinLTO caches.
192 static std::string cache_policy;
193 // Additional options to pass into the code generator.
194 // Note: This array will contain all plugin options which are not claimed
195 // as plugin exclusive to pass to the code generator.
196 static std::vector<const char *> extra;
197 // Sample profile file path
198 static std::string sample_profile;
199 // New pass manager
200 static bool new_pass_manager = false;
201 // Debug new pass manager
202 static bool debug_pass_manager = false;
203 // Directory to store the .dwo files.
204 static std::string dwo_dir;
205 /// Statistics output filename.
206 static std::string stats_file;
208 // Optimization remarks filename, accepted passes and hotness options
209 static std::string RemarksFilename;
210 static std::string RemarksPasses;
211 static bool RemarksWithHotness = false;
212 static std::string RemarksFormat;
214 // Context sensitive PGO options.
215 static std::string cs_profile_path;
216 static bool cs_pgo_gen = false;
218 static void process_plugin_option(const char *opt_)
220 if (opt_ == nullptr)
221 return;
222 llvm::StringRef opt = opt_;
224 if (opt.startswith("mcpu=")) {
225 mcpu = opt.substr(strlen("mcpu="));
226 } else if (opt.startswith("extra-library-path=")) {
227 extra_library_path = opt.substr(strlen("extra_library_path="));
228 } else if (opt.startswith("mtriple=")) {
229 triple = opt.substr(strlen("mtriple="));
230 } else if (opt.startswith("obj-path=")) {
231 obj_path = opt.substr(strlen("obj-path="));
232 } else if (opt == "emit-llvm") {
233 TheOutputType = OT_BC_ONLY;
234 } else if (opt == "save-temps") {
235 TheOutputType = OT_SAVE_TEMPS;
236 } else if (opt == "disable-output") {
237 TheOutputType = OT_DISABLE;
238 } else if (opt == "emit-asm") {
239 TheOutputType = OT_ASM_ONLY;
240 } else if (opt == "thinlto") {
241 thinlto = true;
242 } else if (opt == "thinlto-index-only") {
243 thinlto_index_only = true;
244 } else if (opt.startswith("thinlto-index-only=")) {
245 thinlto_index_only = true;
246 thinlto_linked_objects_file = opt.substr(strlen("thinlto-index-only="));
247 } else if (opt == "thinlto-emit-imports-files") {
248 thinlto_emit_imports_files = true;
249 } else if (opt.startswith("thinlto-prefix-replace=")) {
250 thinlto_prefix_replace = opt.substr(strlen("thinlto-prefix-replace="));
251 if (thinlto_prefix_replace.find(';') == std::string::npos)
252 message(LDPL_FATAL, "thinlto-prefix-replace expects 'old;new' format");
253 } else if (opt.startswith("thinlto-object-suffix-replace=")) {
254 thinlto_object_suffix_replace =
255 opt.substr(strlen("thinlto-object-suffix-replace="));
256 if (thinlto_object_suffix_replace.find(';') == std::string::npos)
257 message(LDPL_FATAL,
258 "thinlto-object-suffix-replace expects 'old;new' format");
259 } else if (opt.startswith("cache-dir=")) {
260 cache_dir = opt.substr(strlen("cache-dir="));
261 } else if (opt.startswith("cache-policy=")) {
262 cache_policy = opt.substr(strlen("cache-policy="));
263 } else if (opt.size() == 2 && opt[0] == 'O') {
264 if (opt[1] < '0' || opt[1] > '3')
265 message(LDPL_FATAL, "Optimization level must be between 0 and 3");
266 OptLevel = opt[1] - '0';
267 } else if (opt.startswith("jobs=")) {
268 if (StringRef(opt_ + 5).getAsInteger(10, Parallelism))
269 message(LDPL_FATAL, "Invalid parallelism level: %s", opt_ + 5);
270 } else if (opt.startswith("lto-partitions=")) {
271 if (opt.substr(strlen("lto-partitions="))
272 .getAsInteger(10, ParallelCodeGenParallelismLevel))
273 message(LDPL_FATAL, "Invalid codegen partition level: %s", opt_ + 5);
274 } else if (opt == "disable-verify") {
275 DisableVerify = true;
276 } else if (opt.startswith("sample-profile=")) {
277 sample_profile = opt.substr(strlen("sample-profile="));
278 } else if (opt == "cs-profile-generate") {
279 cs_pgo_gen = true;
280 } else if (opt.startswith("cs-profile-path=")) {
281 cs_profile_path = opt.substr(strlen("cs-profile-path="));
282 } else if (opt == "new-pass-manager") {
283 new_pass_manager = true;
284 } else if (opt == "debug-pass-manager") {
285 debug_pass_manager = true;
286 } else if (opt.startswith("dwo_dir=")) {
287 dwo_dir = opt.substr(strlen("dwo_dir="));
288 } else if (opt.startswith("opt-remarks-filename=")) {
289 RemarksFilename = opt.substr(strlen("opt-remarks-filename="));
290 } else if (opt.startswith("opt-remarks-passes=")) {
291 RemarksPasses = opt.substr(strlen("opt-remarks-passes="));
292 } else if (opt == "opt-remarks-with-hotness") {
293 RemarksWithHotness = true;
294 } else if (opt.startswith("opt-remarks-format=")) {
295 RemarksFormat = opt.substr(strlen("opt-remarks-format="));
296 } else if (opt.startswith("stats-file=")) {
297 stats_file = opt.substr(strlen("stats-file="));
298 } else {
299 // Save this option to pass to the code generator.
300 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily
301 // add that.
302 if (extra.empty())
303 extra.push_back("LLVMgold");
305 extra.push_back(opt_);
310 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
311 int *claimed);
312 static ld_plugin_status all_symbols_read_hook(void);
313 static ld_plugin_status cleanup_hook(void);
315 extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
316 ld_plugin_status onload(ld_plugin_tv *tv) {
317 InitializeAllTargetInfos();
318 InitializeAllTargets();
319 InitializeAllTargetMCs();
320 InitializeAllAsmParsers();
321 InitializeAllAsmPrinters();
323 // We're given a pointer to the first transfer vector. We read through them
324 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
325 // contain pointers to functions that we need to call to register our own
326 // hooks. The others are addresses of functions we can use to call into gold
327 // for services.
329 bool registeredClaimFile = false;
330 bool RegisteredAllSymbolsRead = false;
332 for (; tv->tv_tag != LDPT_NULL; ++tv) {
333 // Cast tv_tag to int to allow values not in "enum ld_plugin_tag", like, for
334 // example, LDPT_GET_SYMBOLS_V3 when building against an older plugin-api.h
335 // header.
336 switch (static_cast<int>(tv->tv_tag)) {
337 case LDPT_OUTPUT_NAME:
338 output_name = tv->tv_u.tv_string;
339 break;
340 case LDPT_LINKER_OUTPUT:
341 switch (tv->tv_u.tv_val) {
342 case LDPO_REL: // .o
343 IsExecutable = false;
344 SplitSections = false;
345 break;
346 case LDPO_DYN: // .so
347 IsExecutable = false;
348 RelocationModel = Reloc::PIC_;
349 break;
350 case LDPO_PIE: // position independent executable
351 IsExecutable = true;
352 RelocationModel = Reloc::PIC_;
353 break;
354 case LDPO_EXEC: // .exe
355 IsExecutable = true;
356 RelocationModel = Reloc::Static;
357 break;
358 default:
359 message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val);
360 return LDPS_ERR;
362 break;
363 case LDPT_OPTION:
364 options::process_plugin_option(tv->tv_u.tv_string);
365 break;
366 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
367 ld_plugin_register_claim_file callback;
368 callback = tv->tv_u.tv_register_claim_file;
370 if (callback(claim_file_hook) != LDPS_OK)
371 return LDPS_ERR;
373 registeredClaimFile = true;
374 } break;
375 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
376 ld_plugin_register_all_symbols_read callback;
377 callback = tv->tv_u.tv_register_all_symbols_read;
379 if (callback(all_symbols_read_hook) != LDPS_OK)
380 return LDPS_ERR;
382 RegisteredAllSymbolsRead = true;
383 } break;
384 case LDPT_REGISTER_CLEANUP_HOOK: {
385 ld_plugin_register_cleanup callback;
386 callback = tv->tv_u.tv_register_cleanup;
388 if (callback(cleanup_hook) != LDPS_OK)
389 return LDPS_ERR;
390 } break;
391 case LDPT_GET_INPUT_FILE:
392 get_input_file = tv->tv_u.tv_get_input_file;
393 break;
394 case LDPT_RELEASE_INPUT_FILE:
395 release_input_file = tv->tv_u.tv_release_input_file;
396 break;
397 case LDPT_ADD_SYMBOLS:
398 add_symbols = tv->tv_u.tv_add_symbols;
399 break;
400 case LDPT_GET_SYMBOLS_V2:
401 // Do not override get_symbols_v3 with get_symbols_v2.
402 if (!get_symbols)
403 get_symbols = tv->tv_u.tv_get_symbols;
404 break;
405 case LDPT_GET_SYMBOLS_V3:
406 get_symbols = tv->tv_u.tv_get_symbols;
407 break;
408 case LDPT_ADD_INPUT_FILE:
409 add_input_file = tv->tv_u.tv_add_input_file;
410 break;
411 case LDPT_SET_EXTRA_LIBRARY_PATH:
412 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
413 break;
414 case LDPT_GET_VIEW:
415 get_view = tv->tv_u.tv_get_view;
416 break;
417 case LDPT_MESSAGE:
418 message = tv->tv_u.tv_message;
419 break;
420 case LDPT_GET_WRAP_SYMBOLS:
421 // FIXME: When binutils 2.31 (containing gold 1.16) is the minimum
422 // required version, this should be changed to:
423 // get_wrap_symbols = tv->tv_u.tv_get_wrap_symbols;
424 get_wrap_symbols =
425 (ld_plugin_get_wrap_symbols)tv->tv_u.tv_message;
426 break;
427 default:
428 break;
432 if (!registeredClaimFile) {
433 message(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
434 return LDPS_ERR;
436 if (!add_symbols) {
437 message(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
438 return LDPS_ERR;
441 if (!RegisteredAllSymbolsRead)
442 return LDPS_OK;
444 if (!get_input_file) {
445 message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
446 return LDPS_ERR;
448 if (!release_input_file) {
449 message(LDPL_ERROR, "release_input_file not passed to LLVMgold.");
450 return LDPS_ERR;
453 return LDPS_OK;
456 static void diagnosticHandler(const DiagnosticInfo &DI) {
457 std::string ErrStorage;
459 raw_string_ostream OS(ErrStorage);
460 DiagnosticPrinterRawOStream DP(OS);
461 DI.print(DP);
463 ld_plugin_level Level;
464 switch (DI.getSeverity()) {
465 case DS_Error:
466 Level = LDPL_FATAL;
467 break;
468 case DS_Warning:
469 Level = LDPL_WARNING;
470 break;
471 case DS_Note:
472 case DS_Remark:
473 Level = LDPL_INFO;
474 break;
476 message(Level, "LLVM gold plugin: %s", ErrStorage.c_str());
479 static void check(Error E, std::string Msg = "LLVM gold plugin") {
480 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) -> Error {
481 message(LDPL_FATAL, "%s: %s", Msg.c_str(), EIB.message().c_str());
482 return Error::success();
486 template <typename T> static T check(Expected<T> E) {
487 if (E)
488 return std::move(*E);
489 check(E.takeError());
490 return T();
493 /// Called by gold to see whether this file is one that our plugin can handle.
494 /// We'll try to open it and register all the symbols with add_symbol if
495 /// possible.
496 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
497 int *claimed) {
498 MemoryBufferRef BufferRef;
499 std::unique_ptr<MemoryBuffer> Buffer;
500 if (get_view) {
501 const void *view;
502 if (get_view(file->handle, &view) != LDPS_OK) {
503 message(LDPL_ERROR, "Failed to get a view of %s", file->name);
504 return LDPS_ERR;
506 BufferRef =
507 MemoryBufferRef(StringRef((const char *)view, file->filesize), "");
508 } else {
509 int64_t offset = 0;
510 // Gold has found what might be IR part-way inside of a file, such as
511 // an .a archive.
512 if (file->offset) {
513 offset = file->offset;
515 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
516 MemoryBuffer::getOpenFileSlice(sys::fs::convertFDToNativeFile(file->fd),
517 file->name, file->filesize, offset);
518 if (std::error_code EC = BufferOrErr.getError()) {
519 message(LDPL_ERROR, EC.message().c_str());
520 return LDPS_ERR;
522 Buffer = std::move(BufferOrErr.get());
523 BufferRef = Buffer->getMemBufferRef();
526 *claimed = 1;
528 Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef);
529 if (!ObjOrErr) {
530 handleAllErrors(ObjOrErr.takeError(), [&](const ErrorInfoBase &EI) {
531 std::error_code EC = EI.convertToErrorCode();
532 if (EC == object::object_error::invalid_file_type ||
533 EC == object::object_error::bitcode_section_not_found)
534 *claimed = 0;
535 else
536 message(LDPL_FATAL,
537 "LLVM gold plugin has failed to create LTO module: %s",
538 EI.message().c_str());
541 return *claimed ? LDPS_ERR : LDPS_OK;
544 std::unique_ptr<InputFile> Obj = std::move(*ObjOrErr);
546 Modules.emplace_back();
547 claimed_file &cf = Modules.back();
549 cf.handle = file->handle;
550 // Keep track of the first handle for each file descriptor, since there are
551 // multiple in the case of an archive. This is used later in the case of
552 // ThinLTO parallel backends to ensure that each file is only opened and
553 // released once.
554 auto LeaderHandle =
555 FDToLeaderHandle.insert(std::make_pair(file->fd, file->handle)).first;
556 cf.leader_handle = LeaderHandle->second;
557 // Save the filesize since for parallel ThinLTO backends we can only
558 // invoke get_input_file once per archive (only for the leader handle).
559 cf.filesize = file->filesize;
560 // In the case of an archive library, all but the first member must have a
561 // non-zero offset, which we can append to the file name to obtain a
562 // unique name.
563 cf.name = file->name;
564 if (file->offset)
565 cf.name += ".llvm." + std::to_string(file->offset) + "." +
566 sys::path::filename(Obj->getSourceFileName()).str();
568 for (auto &Sym : Obj->symbols()) {
569 cf.syms.push_back(ld_plugin_symbol());
570 ld_plugin_symbol &sym = cf.syms.back();
571 sym.version = nullptr;
572 StringRef Name = Sym.getName();
573 sym.name = strdup(Name.str().c_str());
575 ResolutionInfo &Res = ResInfo[Name];
577 Res.CanOmitFromDynSym &= Sym.canBeOmittedFromSymbolTable();
579 sym.visibility = LDPV_DEFAULT;
580 GlobalValue::VisibilityTypes Vis = Sym.getVisibility();
581 if (Vis != GlobalValue::DefaultVisibility)
582 Res.DefaultVisibility = false;
583 switch (Vis) {
584 case GlobalValue::DefaultVisibility:
585 break;
586 case GlobalValue::HiddenVisibility:
587 sym.visibility = LDPV_HIDDEN;
588 break;
589 case GlobalValue::ProtectedVisibility:
590 sym.visibility = LDPV_PROTECTED;
591 break;
594 if (Sym.isUndefined()) {
595 sym.def = LDPK_UNDEF;
596 if (Sym.isWeak())
597 sym.def = LDPK_WEAKUNDEF;
598 } else if (Sym.isCommon())
599 sym.def = LDPK_COMMON;
600 else if (Sym.isWeak())
601 sym.def = LDPK_WEAKDEF;
602 else
603 sym.def = LDPK_DEF;
605 sym.size = 0;
606 sym.comdat_key = nullptr;
607 int CI = Sym.getComdatIndex();
608 if (CI != -1) {
609 StringRef C = Obj->getComdatTable()[CI];
610 sym.comdat_key = strdup(C.str().c_str());
613 sym.resolution = LDPR_UNKNOWN;
616 if (!cf.syms.empty()) {
617 if (add_symbols(cf.handle, cf.syms.size(), cf.syms.data()) != LDPS_OK) {
618 message(LDPL_ERROR, "Unable to add symbols!");
619 return LDPS_ERR;
623 // Handle any --wrap options passed to gold, which are than passed
624 // along to the plugin.
625 if (get_wrap_symbols) {
626 const char **wrap_symbols;
627 uint64_t count = 0;
628 if (get_wrap_symbols(&count, &wrap_symbols) != LDPS_OK) {
629 message(LDPL_ERROR, "Unable to get wrap symbols!");
630 return LDPS_ERR;
632 for (uint64_t i = 0; i < count; i++) {
633 StringRef Name = wrap_symbols[i];
634 ResolutionInfo &Res = ResInfo[Name];
635 ResolutionInfo &WrapRes = ResInfo["__wrap_" + Name.str()];
636 ResolutionInfo &RealRes = ResInfo["__real_" + Name.str()];
637 // Tell LTO not to inline symbols that will be overwritten.
638 Res.CanInline = false;
639 RealRes.CanInline = false;
640 // Tell LTO not to eliminate symbols that will be used after renaming.
641 Res.IsUsedInRegularObj = true;
642 WrapRes.IsUsedInRegularObj = true;
646 return LDPS_OK;
649 static void freeSymName(ld_plugin_symbol &Sym) {
650 free(Sym.name);
651 free(Sym.comdat_key);
652 Sym.name = nullptr;
653 Sym.comdat_key = nullptr;
656 /// Helper to get a file's symbols and a view into it via gold callbacks.
657 static const void *getSymbolsAndView(claimed_file &F) {
658 ld_plugin_status status = get_symbols(F.handle, F.syms.size(), F.syms.data());
659 if (status == LDPS_NO_SYMS)
660 return nullptr;
662 if (status != LDPS_OK)
663 message(LDPL_FATAL, "Failed to get symbol information");
665 const void *View;
666 if (get_view(F.handle, &View) != LDPS_OK)
667 message(LDPL_FATAL, "Failed to get a view of file");
669 return View;
672 /// Parse the thinlto-object-suffix-replace option into the \p OldSuffix and
673 /// \p NewSuffix strings, if it was specified.
674 static void getThinLTOOldAndNewSuffix(std::string &OldSuffix,
675 std::string &NewSuffix) {
676 assert(options::thinlto_object_suffix_replace.empty() ||
677 options::thinlto_object_suffix_replace.find(";") != StringRef::npos);
678 StringRef SuffixReplace = options::thinlto_object_suffix_replace;
679 std::tie(OldSuffix, NewSuffix) = SuffixReplace.split(';');
682 /// Given the original \p Path to an output file, replace any filename
683 /// suffix matching \p OldSuffix with \p NewSuffix.
684 static std::string getThinLTOObjectFileName(StringRef Path, StringRef OldSuffix,
685 StringRef NewSuffix) {
686 if (Path.consume_back(OldSuffix))
687 return (Path + NewSuffix).str();
688 return Path;
691 // Returns true if S is valid as a C language identifier.
692 static bool isValidCIdentifier(StringRef S) {
693 return !S.empty() && (isAlpha(S[0]) || S[0] == '_') &&
694 std::all_of(S.begin() + 1, S.end(),
695 [](char C) { return C == '_' || isAlnum(C); });
698 static bool isUndefined(ld_plugin_symbol &Sym) {
699 return Sym.def == LDPK_UNDEF || Sym.def == LDPK_WEAKUNDEF;
702 static void addModule(LTO &Lto, claimed_file &F, const void *View,
703 StringRef Filename) {
704 MemoryBufferRef BufferRef(StringRef((const char *)View, F.filesize),
705 Filename);
706 Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef);
708 if (!ObjOrErr)
709 message(LDPL_FATAL, "Could not read bitcode from file : %s",
710 toString(ObjOrErr.takeError()).c_str());
712 unsigned SymNum = 0;
713 std::unique_ptr<InputFile> Input = std::move(ObjOrErr.get());
714 auto InputFileSyms = Input->symbols();
715 assert(InputFileSyms.size() == F.syms.size());
716 std::vector<SymbolResolution> Resols(F.syms.size());
717 for (ld_plugin_symbol &Sym : F.syms) {
718 const InputFile::Symbol &InpSym = InputFileSyms[SymNum];
719 SymbolResolution &R = Resols[SymNum++];
721 ld_plugin_symbol_resolution Resolution =
722 (ld_plugin_symbol_resolution)Sym.resolution;
724 ResolutionInfo &Res = ResInfo[Sym.name];
726 switch (Resolution) {
727 case LDPR_UNKNOWN:
728 llvm_unreachable("Unexpected resolution");
730 case LDPR_RESOLVED_IR:
731 case LDPR_RESOLVED_EXEC:
732 case LDPR_RESOLVED_DYN:
733 case LDPR_PREEMPTED_IR:
734 case LDPR_PREEMPTED_REG:
735 case LDPR_UNDEF:
736 break;
738 case LDPR_PREVAILING_DEF_IRONLY:
739 R.Prevailing = !isUndefined(Sym);
740 break;
742 case LDPR_PREVAILING_DEF:
743 R.Prevailing = !isUndefined(Sym);
744 R.VisibleToRegularObj = true;
745 break;
747 case LDPR_PREVAILING_DEF_IRONLY_EXP:
748 R.Prevailing = !isUndefined(Sym);
749 if (!Res.CanOmitFromDynSym)
750 R.VisibleToRegularObj = true;
751 break;
754 // If the symbol has a C identifier section name, we need to mark
755 // it as visible to a regular object so that LTO will keep it around
756 // to ensure the linker generates special __start_<secname> and
757 // __stop_<secname> symbols which may be used elsewhere.
758 if (isValidCIdentifier(InpSym.getSectionName()))
759 R.VisibleToRegularObj = true;
761 if (Resolution != LDPR_RESOLVED_DYN && Resolution != LDPR_UNDEF &&
762 (IsExecutable || !Res.DefaultVisibility))
763 R.FinalDefinitionInLinkageUnit = true;
765 if (!Res.CanInline)
766 R.LinkerRedefined = true;
768 if (Res.IsUsedInRegularObj)
769 R.VisibleToRegularObj = true;
771 freeSymName(Sym);
774 check(Lto.add(std::move(Input), Resols),
775 std::string("Failed to link module ") + F.name);
778 static void recordFile(const std::string &Filename, bool TempOutFile) {
779 if (add_input_file(Filename.c_str()) != LDPS_OK)
780 message(LDPL_FATAL,
781 "Unable to add .o file to the link. File left behind in: %s",
782 Filename.c_str());
783 if (TempOutFile)
784 Cleanup.push_back(Filename);
787 /// Return the desired output filename given a base input name, a flag
788 /// indicating whether a temp file should be generated, and an optional task id.
789 /// The new filename generated is returned in \p NewFilename.
790 static int getOutputFileName(StringRef InFilename, bool TempOutFile,
791 SmallString<128> &NewFilename, int TaskID) {
792 int FD = -1;
793 if (TempOutFile) {
794 std::error_code EC =
795 sys::fs::createTemporaryFile("lto-llvm", "o", FD, NewFilename);
796 if (EC)
797 message(LDPL_FATAL, "Could not create temporary file: %s",
798 EC.message().c_str());
799 } else {
800 NewFilename = InFilename;
801 if (TaskID > 0)
802 NewFilename += utostr(TaskID);
803 std::error_code EC =
804 sys::fs::openFileForWrite(NewFilename, FD, sys::fs::CD_CreateAlways);
805 if (EC)
806 message(LDPL_FATAL, "Could not open file %s: %s", NewFilename.c_str(),
807 EC.message().c_str());
809 return FD;
812 static CodeGenOpt::Level getCGOptLevel() {
813 switch (options::OptLevel) {
814 case 0:
815 return CodeGenOpt::None;
816 case 1:
817 return CodeGenOpt::Less;
818 case 2:
819 return CodeGenOpt::Default;
820 case 3:
821 return CodeGenOpt::Aggressive;
823 llvm_unreachable("Invalid optimization level");
826 /// Parse the thinlto_prefix_replace option into the \p OldPrefix and
827 /// \p NewPrefix strings, if it was specified.
828 static void getThinLTOOldAndNewPrefix(std::string &OldPrefix,
829 std::string &NewPrefix) {
830 StringRef PrefixReplace = options::thinlto_prefix_replace;
831 assert(PrefixReplace.empty() || PrefixReplace.find(";") != StringRef::npos);
832 std::tie(OldPrefix, NewPrefix) = PrefixReplace.split(';');
835 /// Creates instance of LTO.
836 /// OnIndexWrite is callback to let caller know when LTO writes index files.
837 /// LinkedObjectsFile is an output stream to write the list of object files for
838 /// the final ThinLTO linking. Can be nullptr.
839 static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
840 raw_fd_ostream *LinkedObjectsFile) {
841 Config Conf;
842 ThinBackend Backend;
844 Conf.CPU = options::mcpu;
845 Conf.Options = InitTargetOptionsFromCodeGenFlags();
847 // Disable the new X86 relax relocations since gold might not support them.
848 // FIXME: Check the gold version or add a new option to enable them.
849 Conf.Options.RelaxELFRelocations = false;
851 // Toggle function/data sections.
852 if (FunctionSections.getNumOccurrences() == 0)
853 Conf.Options.FunctionSections = SplitSections;
854 if (DataSections.getNumOccurrences() == 0)
855 Conf.Options.DataSections = SplitSections;
857 Conf.MAttrs = MAttrs;
858 Conf.RelocModel = RelocationModel;
859 Conf.CodeModel = getCodeModel();
860 Conf.CGOptLevel = getCGOptLevel();
861 Conf.DisableVerify = options::DisableVerify;
862 Conf.OptLevel = options::OptLevel;
863 if (options::Parallelism)
864 Backend = createInProcessThinBackend(options::Parallelism);
865 if (options::thinlto_index_only) {
866 std::string OldPrefix, NewPrefix;
867 getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
868 Backend = createWriteIndexesThinBackend(OldPrefix, NewPrefix,
869 options::thinlto_emit_imports_files,
870 LinkedObjectsFile, OnIndexWrite);
873 Conf.OverrideTriple = options::triple;
874 Conf.DefaultTriple = sys::getDefaultTargetTriple();
876 Conf.DiagHandler = diagnosticHandler;
878 switch (options::TheOutputType) {
879 case options::OT_NORMAL:
880 break;
882 case options::OT_DISABLE:
883 Conf.PreOptModuleHook = [](size_t Task, const Module &M) { return false; };
884 break;
886 case options::OT_BC_ONLY:
887 Conf.PostInternalizeModuleHook = [](size_t Task, const Module &M) {
888 std::error_code EC;
889 raw_fd_ostream OS(output_name, EC, sys::fs::OpenFlags::OF_None);
890 if (EC)
891 message(LDPL_FATAL, "Failed to write the output file.");
892 WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ false);
893 return false;
895 break;
897 case options::OT_SAVE_TEMPS:
898 check(Conf.addSaveTemps(output_name + ".",
899 /* UseInputModulePath */ true));
900 break;
901 case options::OT_ASM_ONLY:
902 Conf.CGFileType = TargetMachine::CGFT_AssemblyFile;
903 break;
906 if (!options::sample_profile.empty())
907 Conf.SampleProfile = options::sample_profile;
909 if (!options::cs_profile_path.empty())
910 Conf.CSIRProfile = options::cs_profile_path;
911 Conf.RunCSIRInstr = options::cs_pgo_gen;
913 Conf.DwoDir = options::dwo_dir;
915 // Set up optimization remarks handling.
916 Conf.RemarksFilename = options::RemarksFilename;
917 Conf.RemarksPasses = options::RemarksPasses;
918 Conf.RemarksWithHotness = options::RemarksWithHotness;
919 Conf.RemarksFormat = options::RemarksFormat;
921 // Use new pass manager if set in driver
922 Conf.UseNewPM = options::new_pass_manager;
923 // Debug new pass manager if requested
924 Conf.DebugPassManager = options::debug_pass_manager;
926 Conf.StatsFile = options::stats_file;
927 return std::make_unique<LTO>(std::move(Conf), Backend,
928 options::ParallelCodeGenParallelismLevel);
931 // Write empty files that may be expected by a distributed build
932 // system when invoked with thinlto_index_only. This is invoked when
933 // the linker has decided not to include the given module in the
934 // final link. Frequently the distributed build system will want to
935 // confirm that all expected outputs are created based on all of the
936 // modules provided to the linker.
937 // If SkipModule is true then .thinlto.bc should contain just
938 // SkipModuleByDistributedBackend flag which requests distributed backend
939 // to skip the compilation of the corresponding module and produce an empty
940 // object file.
941 static void writeEmptyDistributedBuildOutputs(const std::string &ModulePath,
942 const std::string &OldPrefix,
943 const std::string &NewPrefix,
944 bool SkipModule) {
945 std::string NewModulePath =
946 getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
947 std::error_code EC;
949 raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
950 sys::fs::OpenFlags::OF_None);
951 if (EC)
952 message(LDPL_FATAL, "Failed to write '%s': %s",
953 (NewModulePath + ".thinlto.bc").c_str(), EC.message().c_str());
955 if (SkipModule) {
956 ModuleSummaryIndex Index(/*HaveGVs*/ false);
957 Index.setSkipModuleByDistributedBackend();
958 WriteIndexToFile(Index, OS, nullptr);
961 if (options::thinlto_emit_imports_files) {
962 raw_fd_ostream OS(NewModulePath + ".imports", EC,
963 sys::fs::OpenFlags::OF_None);
964 if (EC)
965 message(LDPL_FATAL, "Failed to write '%s': %s",
966 (NewModulePath + ".imports").c_str(), EC.message().c_str());
970 // Creates and returns output stream with a list of object files for final
971 // linking of distributed ThinLTO.
972 static std::unique_ptr<raw_fd_ostream> CreateLinkedObjectsFile() {
973 if (options::thinlto_linked_objects_file.empty())
974 return nullptr;
975 assert(options::thinlto_index_only);
976 std::error_code EC;
977 auto LinkedObjectsFile = std::make_unique<raw_fd_ostream>(
978 options::thinlto_linked_objects_file, EC, sys::fs::OpenFlags::OF_None);
979 if (EC)
980 message(LDPL_FATAL, "Failed to create '%s': %s",
981 options::thinlto_linked_objects_file.c_str(), EC.message().c_str());
982 return LinkedObjectsFile;
985 /// Runs LTO and return a list of pairs <FileName, IsTemporary>.
986 static std::vector<std::pair<SmallString<128>, bool>> runLTO() {
987 // Map to own RAII objects that manage the file opening and releasing
988 // interfaces with gold. This is needed only for ThinLTO mode, since
989 // unlike regular LTO, where addModule will result in the opened file
990 // being merged into a new combined module, we need to keep these files open
991 // through Lto->run().
992 DenseMap<void *, std::unique_ptr<PluginInputFile>> HandleToInputFile;
994 // Owns string objects and tells if index file was already created.
995 StringMap<bool> ObjectToIndexFileState;
997 std::unique_ptr<raw_fd_ostream> LinkedObjects = CreateLinkedObjectsFile();
998 std::unique_ptr<LTO> Lto = createLTO(
999 [&ObjectToIndexFileState](const std::string &Identifier) {
1000 ObjectToIndexFileState[Identifier] = true;
1002 LinkedObjects.get());
1004 std::string OldPrefix, NewPrefix;
1005 if (options::thinlto_index_only)
1006 getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
1008 std::string OldSuffix, NewSuffix;
1009 getThinLTOOldAndNewSuffix(OldSuffix, NewSuffix);
1011 for (claimed_file &F : Modules) {
1012 if (options::thinlto && !HandleToInputFile.count(F.leader_handle))
1013 HandleToInputFile.insert(std::make_pair(
1014 F.leader_handle, std::make_unique<PluginInputFile>(F.handle)));
1015 // In case we are thin linking with a minimized bitcode file, ensure
1016 // the module paths encoded in the index reflect where the backends
1017 // will locate the full bitcode files for compiling/importing.
1018 std::string Identifier =
1019 getThinLTOObjectFileName(F.name, OldSuffix, NewSuffix);
1020 auto ObjFilename = ObjectToIndexFileState.insert({Identifier, false});
1021 assert(ObjFilename.second);
1022 if (const void *View = getSymbolsAndView(F))
1023 addModule(*Lto, F, View, ObjFilename.first->first());
1024 else if (options::thinlto_index_only) {
1025 ObjFilename.first->second = true;
1026 writeEmptyDistributedBuildOutputs(Identifier, OldPrefix, NewPrefix,
1027 /* SkipModule */ true);
1031 SmallString<128> Filename;
1032 // Note that getOutputFileName will append a unique ID for each task
1033 if (!options::obj_path.empty())
1034 Filename = options::obj_path;
1035 else if (options::TheOutputType == options::OT_SAVE_TEMPS)
1036 Filename = output_name + ".o";
1037 else if (options::TheOutputType == options::OT_ASM_ONLY)
1038 Filename = output_name;
1039 bool SaveTemps = !Filename.empty();
1041 size_t MaxTasks = Lto->getMaxTasks();
1042 std::vector<std::pair<SmallString<128>, bool>> Files(MaxTasks);
1044 auto AddStream =
1045 [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
1046 Files[Task].second = !SaveTemps;
1047 int FD = getOutputFileName(Filename, /* TempOutFile */ !SaveTemps,
1048 Files[Task].first, Task);
1049 return std::make_unique<lto::NativeObjectStream>(
1050 std::make_unique<llvm::raw_fd_ostream>(FD, true));
1053 auto AddBuffer = [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
1054 *AddStream(Task)->OS << MB->getBuffer();
1057 NativeObjectCache Cache;
1058 if (!options::cache_dir.empty())
1059 Cache = check(localCache(options::cache_dir, AddBuffer));
1061 check(Lto->run(AddStream, Cache));
1063 // Write empty output files that may be expected by the distributed build
1064 // system.
1065 if (options::thinlto_index_only)
1066 for (auto &Identifier : ObjectToIndexFileState)
1067 if (!Identifier.getValue())
1068 writeEmptyDistributedBuildOutputs(Identifier.getKey(), OldPrefix,
1069 NewPrefix, /* SkipModule */ false);
1071 return Files;
1074 /// gold informs us that all symbols have been read. At this point, we use
1075 /// get_symbols to see if any of our definitions have been overridden by a
1076 /// native object file. Then, perform optimization and codegen.
1077 static ld_plugin_status allSymbolsReadHook() {
1078 if (Modules.empty())
1079 return LDPS_OK;
1081 if (unsigned NumOpts = options::extra.size())
1082 cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
1084 std::vector<std::pair<SmallString<128>, bool>> Files = runLTO();
1086 if (options::TheOutputType == options::OT_DISABLE ||
1087 options::TheOutputType == options::OT_BC_ONLY ||
1088 options::TheOutputType == options::OT_ASM_ONLY)
1089 return LDPS_OK;
1091 if (options::thinlto_index_only) {
1092 llvm_shutdown();
1093 cleanup_hook();
1094 exit(0);
1097 for (const auto &F : Files)
1098 if (!F.first.empty())
1099 recordFile(F.first.str(), F.second);
1101 if (!options::extra_library_path.empty() &&
1102 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
1103 message(LDPL_FATAL, "Unable to set the extra library path.");
1105 return LDPS_OK;
1108 static ld_plugin_status all_symbols_read_hook(void) {
1109 ld_plugin_status Ret = allSymbolsReadHook();
1110 llvm_shutdown();
1112 if (options::TheOutputType == options::OT_BC_ONLY ||
1113 options::TheOutputType == options::OT_ASM_ONLY ||
1114 options::TheOutputType == options::OT_DISABLE) {
1115 if (options::TheOutputType == options::OT_DISABLE) {
1116 // Remove the output file here since ld.bfd creates the output file
1117 // early.
1118 std::error_code EC = sys::fs::remove(output_name);
1119 if (EC)
1120 message(LDPL_ERROR, "Failed to delete '%s': %s", output_name.c_str(),
1121 EC.message().c_str());
1123 exit(0);
1126 return Ret;
1129 static ld_plugin_status cleanup_hook(void) {
1130 for (std::string &Name : Cleanup) {
1131 std::error_code EC = sys::fs::remove(Name);
1132 if (EC)
1133 message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(),
1134 EC.message().c_str());
1137 // Prune cache
1138 if (!options::cache_dir.empty()) {
1139 CachePruningPolicy policy = check(parseCachePruningPolicy(options::cache_policy));
1140 pruneCache(options::cache_dir, policy);
1143 return LDPS_OK;