[NFC][Py Reformat] Reformat python files in llvm
[llvm-project.git] / llvm / tools / gold / gold-plugin.cpp
bloba47dba35aa472e2fcb8482b53fc70b249c621ec5
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.h"
18 #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
19 #include "llvm/Config/llvm-config.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DiagnosticPrinter.h"
22 #include "llvm/LTO/LTO.h"
23 #include "llvm/Object/Error.h"
24 #include "llvm/Remarks/HotnessThresholdParser.h"
25 #include "llvm/Support/CachePruning.h"
26 #include "llvm/Support/Caching.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/ManagedStatic.h"
30 #include "llvm/Support/MemoryBuffer.h"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/TargetSelect.h"
33 #include "llvm/Support/Threading.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/TargetParser/Host.h"
36 #include <list>
37 #include <map>
38 #include <plugin-api.h>
39 #include <string>
40 #include <system_error>
41 #include <utility>
42 #include <vector>
44 // FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and
45 // Precise and Debian Wheezy (binutils 2.23 is required)
46 #define LDPO_PIE 3
48 #define LDPT_GET_SYMBOLS_V3 28
50 // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
51 // required version.
52 #define LDPT_GET_WRAP_SYMBOLS 32
54 using namespace llvm;
55 using namespace lto;
57 static codegen::RegisterCodeGenFlags CodeGenFlags;
59 // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
60 // required version.
61 typedef enum ld_plugin_status (*ld_plugin_get_wrap_symbols)(
62 uint64_t *num_symbols, const char ***wrap_symbol_list);
64 static ld_plugin_status discard_message(int level, const char *format, ...) {
65 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
66 // callback in the transfer vector. This should never be called.
67 abort();
70 static ld_plugin_release_input_file release_input_file = nullptr;
71 static ld_plugin_get_input_file get_input_file = nullptr;
72 static ld_plugin_message message = discard_message;
73 static ld_plugin_get_wrap_symbols get_wrap_symbols = nullptr;
75 namespace {
76 struct claimed_file {
77 void *handle;
78 void *leader_handle;
79 std::vector<ld_plugin_symbol> syms;
80 off_t filesize;
81 std::string name;
84 /// RAII wrapper to manage opening and releasing of a ld_plugin_input_file.
85 struct PluginInputFile {
86 void *Handle;
87 std::unique_ptr<ld_plugin_input_file> File;
89 PluginInputFile(void *Handle) : Handle(Handle) {
90 File = std::make_unique<ld_plugin_input_file>();
91 if (get_input_file(Handle, File.get()) != LDPS_OK)
92 message(LDPL_FATAL, "Failed to get file information");
94 ~PluginInputFile() {
95 // File would have been reset to nullptr if we moved this object
96 // to a new owner.
97 if (File)
98 if (release_input_file(Handle) != LDPS_OK)
99 message(LDPL_FATAL, "Failed to release file information");
102 ld_plugin_input_file &file() { return *File; }
104 PluginInputFile(PluginInputFile &&RHS) = default;
105 PluginInputFile &operator=(PluginInputFile &&RHS) = default;
108 struct ResolutionInfo {
109 bool CanOmitFromDynSym = true;
110 bool DefaultVisibility = true;
111 bool CanInline = true;
112 bool IsUsedInRegularObj = false;
117 static ld_plugin_add_symbols add_symbols = nullptr;
118 static ld_plugin_get_symbols get_symbols = nullptr;
119 static ld_plugin_add_input_file add_input_file = nullptr;
120 static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
121 static ld_plugin_get_view get_view = nullptr;
122 static bool IsExecutable = false;
123 static bool SplitSections = true;
124 static std::optional<Reloc::Model> RelocationModel;
125 static std::string output_name = "";
126 static std::list<claimed_file> Modules;
127 static DenseMap<int, void *> FDToLeaderHandle;
128 static StringMap<ResolutionInfo> ResInfo;
129 static std::vector<std::string> Cleanup;
131 namespace options {
132 enum OutputType {
133 OT_NORMAL,
134 OT_DISABLE,
135 OT_BC_ONLY,
136 OT_ASM_ONLY,
137 OT_SAVE_TEMPS
139 static OutputType TheOutputType = OT_NORMAL;
140 static unsigned OptLevel = 2;
141 // Currently only affects ThinLTO, where the default is the max cores in the
142 // system. See llvm::get_threadpool_strategy() for acceptable values.
143 static std::string Parallelism;
144 // Default regular LTO codegen parallelism (number of partitions).
145 static unsigned ParallelCodeGenParallelismLevel = 1;
146 #ifdef NDEBUG
147 static bool DisableVerify = true;
148 #else
149 static bool DisableVerify = false;
150 #endif
151 static std::string obj_path;
152 static std::string extra_library_path;
153 static std::string triple;
154 static std::string mcpu;
155 // When the thinlto plugin option is specified, only read the function
156 // the information from intermediate files and write a combined
157 // global index for the ThinLTO backends.
158 static bool thinlto = false;
159 // If false, all ThinLTO backend compilations through code gen are performed
160 // using multiple threads in the gold-plugin, before handing control back to
161 // gold. If true, write individual backend index files which reflect
162 // the import decisions, and exit afterwards. The assumption is
163 // that the build system will launch the backend processes.
164 static bool thinlto_index_only = false;
165 // If non-empty, holds the name of a file in which to write the list of
166 // oject files gold selected for inclusion in the link after symbol
167 // resolution (i.e. they had selected symbols). This will only be non-empty
168 // in the thinlto_index_only case. It is used to identify files, which may
169 // have originally been within archive libraries specified via
170 // --start-lib/--end-lib pairs, that should be included in the final
171 // native link process (since intervening function importing and inlining
172 // may change the symbol resolution detected in the final link and which
173 // files to include out of --start-lib/--end-lib libraries as a result).
174 static std::string thinlto_linked_objects_file;
175 // If true, when generating individual index files for distributed backends,
176 // also generate a "${bitcodefile}.imports" file at the same location for each
177 // bitcode file, listing the files it imports from in plain text. This is to
178 // support distributed build file staging.
179 static bool thinlto_emit_imports_files = false;
180 // Option to control where files for a distributed backend (the individual
181 // index files and optional imports files) are created.
182 // If specified, expects a string of the form "oldprefix:newprefix", and
183 // instead of generating these files in the same directory path as the
184 // corresponding bitcode file, will use a path formed by replacing the
185 // bitcode file's path prefix matching oldprefix with newprefix.
186 static std::string thinlto_prefix_replace;
187 // Option to control the name of modules encoded in the individual index
188 // files for a distributed backend. This enables the use of minimized
189 // bitcode files for the thin link, assuming the name of the full bitcode
190 // file used in the backend differs just in some part of the file suffix.
191 // If specified, expects a string of the form "oldsuffix:newsuffix".
192 static std::string thinlto_object_suffix_replace;
193 // Optional path to a directory for caching ThinLTO objects.
194 static std::string cache_dir;
195 // Optional pruning policy for ThinLTO caches.
196 static std::string cache_policy;
197 // Additional options to pass into the code generator.
198 // Note: This array will contain all plugin options which are not claimed
199 // as plugin exclusive to pass to the code generator.
200 static std::vector<const char *> extra;
201 // Sample profile file path
202 static std::string sample_profile;
203 // Debug new pass manager
204 static bool debug_pass_manager = false;
205 // Directory to store the .dwo files.
206 static std::string dwo_dir;
207 /// Statistics output filename.
208 static std::string stats_file;
209 // Asserts that LTO link has whole program visibility
210 static bool whole_program_visibility = false;
212 // Optimization remarks filename, accepted passes and hotness options
213 static std::string RemarksFilename;
214 static std::string RemarksPasses;
215 static bool RemarksWithHotness = false;
216 static std::optional<uint64_t> RemarksHotnessThreshold = 0;
217 static std::string RemarksFormat;
219 // Context sensitive PGO options.
220 static std::string cs_profile_path;
221 static bool cs_pgo_gen = false;
223 static void process_plugin_option(const char *opt_)
225 if (opt_ == nullptr)
226 return;
227 llvm::StringRef opt = opt_;
229 if (opt.consume_front("mcpu=")) {
230 mcpu = std::string(opt);
231 } else if (opt.consume_front("extra-library-path=")) {
232 extra_library_path = std::string(opt);
233 } else if (opt.consume_front("mtriple=")) {
234 triple = std::string(opt);
235 } else if (opt.consume_front("obj-path=")) {
236 obj_path = std::string(opt);
237 } else if (opt == "emit-llvm") {
238 TheOutputType = OT_BC_ONLY;
239 } else if (opt == "save-temps") {
240 TheOutputType = OT_SAVE_TEMPS;
241 } else if (opt == "disable-output") {
242 TheOutputType = OT_DISABLE;
243 } else if (opt == "emit-asm") {
244 TheOutputType = OT_ASM_ONLY;
245 } else if (opt == "thinlto") {
246 thinlto = true;
247 } else if (opt == "thinlto-index-only") {
248 thinlto_index_only = true;
249 } else if (opt.consume_front("thinlto-index-only=")) {
250 thinlto_index_only = true;
251 thinlto_linked_objects_file = std::string(opt);
252 } else if (opt == "thinlto-emit-imports-files") {
253 thinlto_emit_imports_files = true;
254 } else if (opt.consume_front("thinlto-prefix-replace=")) {
255 thinlto_prefix_replace = std::string(opt);
256 if (thinlto_prefix_replace.find(';') == std::string::npos)
257 message(LDPL_FATAL, "thinlto-prefix-replace expects 'old;new' format");
258 } else if (opt.consume_front("thinlto-object-suffix-replace=")) {
259 thinlto_object_suffix_replace = std::string(opt);
260 if (thinlto_object_suffix_replace.find(';') == std::string::npos)
261 message(LDPL_FATAL,
262 "thinlto-object-suffix-replace expects 'old;new' format");
263 } else if (opt.consume_front("cache-dir=")) {
264 cache_dir = std::string(opt);
265 } else if (opt.consume_front("cache-policy=")) {
266 cache_policy = std::string(opt);
267 } else if (opt.size() == 2 && opt[0] == 'O') {
268 if (opt[1] < '0' || opt[1] > '3')
269 message(LDPL_FATAL, "Optimization level must be between 0 and 3");
270 OptLevel = opt[1] - '0';
271 } else if (opt.consume_front("jobs=")) {
272 Parallelism = std::string(opt);
273 if (!get_threadpool_strategy(opt))
274 message(LDPL_FATAL, "Invalid parallelism level: %s",
275 Parallelism.c_str());
276 } else if (opt.consume_front("lto-partitions=")) {
277 if (opt.getAsInteger(10, ParallelCodeGenParallelismLevel))
278 message(LDPL_FATAL, "Invalid codegen partition level: %s", opt_ + 5);
279 } else if (opt == "disable-verify") {
280 DisableVerify = true;
281 } else if (opt.consume_front("sample-profile=")) {
282 sample_profile = std::string(opt);
283 } else if (opt == "cs-profile-generate") {
284 cs_pgo_gen = true;
285 } else if (opt.consume_front("cs-profile-path=")) {
286 cs_profile_path = std::string(opt);
287 } else if (opt == "new-pass-manager") {
288 // We always use the new pass manager.
289 } else if (opt == "debug-pass-manager") {
290 debug_pass_manager = true;
291 } else if (opt == "whole-program-visibility") {
292 whole_program_visibility = true;
293 } else if (opt.consume_front("dwo_dir=")) {
294 dwo_dir = std::string(opt);
295 } else if (opt.consume_front("opt-remarks-filename=")) {
296 RemarksFilename = std::string(opt);
297 } else if (opt.consume_front("opt-remarks-passes=")) {
298 RemarksPasses = std::string(opt);
299 } else if (opt == "opt-remarks-with-hotness") {
300 RemarksWithHotness = true;
301 } else if (opt.consume_front("opt-remarks-hotness-threshold=")) {
302 auto ResultOrErr = remarks::parseHotnessThresholdOption(opt);
303 if (!ResultOrErr)
304 message(LDPL_FATAL, "Invalid remarks hotness threshold: %s", opt);
305 else
306 RemarksHotnessThreshold = *ResultOrErr;
307 } else if (opt.consume_front("opt-remarks-format=")) {
308 RemarksFormat = std::string(opt);
309 } else if (opt.consume_front("stats-file=")) {
310 stats_file = std::string(opt);
311 } else if (opt == "opaque-pointers") {
312 // We always use opaque pointers.
313 } else {
314 // Save this option to pass to the code generator.
315 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily
316 // add that.
317 if (extra.empty())
318 extra.push_back("LLVMgold");
320 extra.push_back(opt_);
325 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
326 int *claimed);
327 static ld_plugin_status all_symbols_read_hook(void);
328 static ld_plugin_status cleanup_hook(void);
330 extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
331 ld_plugin_status onload(ld_plugin_tv *tv) {
332 InitializeAllTargetInfos();
333 InitializeAllTargets();
334 InitializeAllTargetMCs();
335 InitializeAllAsmParsers();
336 InitializeAllAsmPrinters();
338 // We're given a pointer to the first transfer vector. We read through them
339 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
340 // contain pointers to functions that we need to call to register our own
341 // hooks. The others are addresses of functions we can use to call into gold
342 // for services.
344 bool registeredClaimFile = false;
345 bool RegisteredAllSymbolsRead = false;
347 for (; tv->tv_tag != LDPT_NULL; ++tv) {
348 // Cast tv_tag to int to allow values not in "enum ld_plugin_tag", like, for
349 // example, LDPT_GET_SYMBOLS_V3 when building against an older plugin-api.h
350 // header.
351 switch (static_cast<int>(tv->tv_tag)) {
352 case LDPT_OUTPUT_NAME:
353 output_name = tv->tv_u.tv_string;
354 break;
355 case LDPT_LINKER_OUTPUT:
356 switch (tv->tv_u.tv_val) {
357 case LDPO_REL: // .o
358 IsExecutable = false;
359 SplitSections = false;
360 break;
361 case LDPO_DYN: // .so
362 IsExecutable = false;
363 RelocationModel = Reloc::PIC_;
364 break;
365 case LDPO_PIE: // position independent executable
366 IsExecutable = true;
367 RelocationModel = Reloc::PIC_;
368 break;
369 case LDPO_EXEC: // .exe
370 IsExecutable = true;
371 RelocationModel = Reloc::Static;
372 break;
373 default:
374 message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val);
375 return LDPS_ERR;
377 break;
378 case LDPT_OPTION:
379 options::process_plugin_option(tv->tv_u.tv_string);
380 break;
381 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
382 ld_plugin_register_claim_file callback;
383 callback = tv->tv_u.tv_register_claim_file;
385 if (callback(claim_file_hook) != LDPS_OK)
386 return LDPS_ERR;
388 registeredClaimFile = true;
389 } break;
390 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
391 ld_plugin_register_all_symbols_read callback;
392 callback = tv->tv_u.tv_register_all_symbols_read;
394 if (callback(all_symbols_read_hook) != LDPS_OK)
395 return LDPS_ERR;
397 RegisteredAllSymbolsRead = true;
398 } break;
399 case LDPT_REGISTER_CLEANUP_HOOK: {
400 ld_plugin_register_cleanup callback;
401 callback = tv->tv_u.tv_register_cleanup;
403 if (callback(cleanup_hook) != LDPS_OK)
404 return LDPS_ERR;
405 } break;
406 case LDPT_GET_INPUT_FILE:
407 get_input_file = tv->tv_u.tv_get_input_file;
408 break;
409 case LDPT_RELEASE_INPUT_FILE:
410 release_input_file = tv->tv_u.tv_release_input_file;
411 break;
412 case LDPT_ADD_SYMBOLS:
413 add_symbols = tv->tv_u.tv_add_symbols;
414 break;
415 case LDPT_GET_SYMBOLS_V2:
416 // Do not override get_symbols_v3 with get_symbols_v2.
417 if (!get_symbols)
418 get_symbols = tv->tv_u.tv_get_symbols;
419 break;
420 case LDPT_GET_SYMBOLS_V3:
421 get_symbols = tv->tv_u.tv_get_symbols;
422 break;
423 case LDPT_ADD_INPUT_FILE:
424 add_input_file = tv->tv_u.tv_add_input_file;
425 break;
426 case LDPT_SET_EXTRA_LIBRARY_PATH:
427 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
428 break;
429 case LDPT_GET_VIEW:
430 get_view = tv->tv_u.tv_get_view;
431 break;
432 case LDPT_MESSAGE:
433 message = tv->tv_u.tv_message;
434 break;
435 case LDPT_GET_WRAP_SYMBOLS:
436 // FIXME: When binutils 2.31 (containing gold 1.16) is the minimum
437 // required version, this should be changed to:
438 // get_wrap_symbols = tv->tv_u.tv_get_wrap_symbols;
439 get_wrap_symbols =
440 (ld_plugin_get_wrap_symbols)tv->tv_u.tv_message;
441 break;
442 default:
443 break;
447 if (!registeredClaimFile) {
448 message(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
449 return LDPS_ERR;
451 if (!add_symbols) {
452 message(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
453 return LDPS_ERR;
456 if (!RegisteredAllSymbolsRead)
457 return LDPS_OK;
459 if (!get_input_file) {
460 message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
461 return LDPS_ERR;
463 if (!release_input_file) {
464 message(LDPL_ERROR, "release_input_file not passed to LLVMgold.");
465 return LDPS_ERR;
468 return LDPS_OK;
471 static void diagnosticHandler(const DiagnosticInfo &DI) {
472 std::string ErrStorage;
474 raw_string_ostream OS(ErrStorage);
475 DiagnosticPrinterRawOStream DP(OS);
476 DI.print(DP);
478 ld_plugin_level Level;
479 switch (DI.getSeverity()) {
480 case DS_Error:
481 Level = LDPL_FATAL;
482 break;
483 case DS_Warning:
484 Level = LDPL_WARNING;
485 break;
486 case DS_Note:
487 case DS_Remark:
488 Level = LDPL_INFO;
489 break;
491 message(Level, "LLVM gold plugin: %s", ErrStorage.c_str());
494 static void check(Error E, std::string Msg = "LLVM gold plugin") {
495 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) -> Error {
496 message(LDPL_FATAL, "%s: %s", Msg.c_str(), EIB.message().c_str());
497 return Error::success();
501 template <typename T> static T check(Expected<T> E) {
502 if (E)
503 return std::move(*E);
504 check(E.takeError());
505 return T();
508 /// Called by gold to see whether this file is one that our plugin can handle.
509 /// We'll try to open it and register all the symbols with add_symbol if
510 /// possible.
511 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
512 int *claimed) {
513 MemoryBufferRef BufferRef;
514 std::unique_ptr<MemoryBuffer> Buffer;
515 if (get_view) {
516 const void *view;
517 if (get_view(file->handle, &view) != LDPS_OK) {
518 message(LDPL_ERROR, "Failed to get a view of %s", file->name);
519 return LDPS_ERR;
521 BufferRef =
522 MemoryBufferRef(StringRef((const char *)view, file->filesize), "");
523 } else {
524 int64_t offset = 0;
525 // Gold has found what might be IR part-way inside of a file, such as
526 // an .a archive.
527 if (file->offset) {
528 offset = file->offset;
530 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
531 MemoryBuffer::getOpenFileSlice(sys::fs::convertFDToNativeFile(file->fd),
532 file->name, file->filesize, offset);
533 if (std::error_code EC = BufferOrErr.getError()) {
534 message(LDPL_ERROR, EC.message().c_str());
535 return LDPS_ERR;
537 Buffer = std::move(BufferOrErr.get());
538 BufferRef = Buffer->getMemBufferRef();
541 // Only use bitcode files for LTO. InputFile::create() will load bitcode
542 // from the .llvmbc section within a binary object, this bitcode is typically
543 // generated by -fembed-bitcode and is not to be used by LLVMgold.so for LTO.
544 if (identify_magic(BufferRef.getBuffer()) != file_magic::bitcode) {
545 *claimed = 0;
546 return LDPS_OK;
549 *claimed = 1;
551 Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef);
552 if (!ObjOrErr) {
553 handleAllErrors(ObjOrErr.takeError(), [&](const ErrorInfoBase &EI) {
554 std::error_code EC = EI.convertToErrorCode();
555 if (EC == object::object_error::invalid_file_type ||
556 EC == object::object_error::bitcode_section_not_found)
557 *claimed = 0;
558 else
559 message(LDPL_FATAL,
560 "LLVM gold plugin has failed to create LTO module: %s",
561 EI.message().c_str());
564 return *claimed ? LDPS_ERR : LDPS_OK;
567 std::unique_ptr<InputFile> Obj = std::move(*ObjOrErr);
569 Modules.emplace_back();
570 claimed_file &cf = Modules.back();
572 cf.handle = file->handle;
573 // Keep track of the first handle for each file descriptor, since there are
574 // multiple in the case of an archive. This is used later in the case of
575 // ThinLTO parallel backends to ensure that each file is only opened and
576 // released once.
577 auto LeaderHandle =
578 FDToLeaderHandle.insert(std::make_pair(file->fd, file->handle)).first;
579 cf.leader_handle = LeaderHandle->second;
580 // Save the filesize since for parallel ThinLTO backends we can only
581 // invoke get_input_file once per archive (only for the leader handle).
582 cf.filesize = file->filesize;
583 // In the case of an archive library, all but the first member must have a
584 // non-zero offset, which we can append to the file name to obtain a
585 // unique name.
586 cf.name = file->name;
587 if (file->offset)
588 cf.name += ".llvm." + std::to_string(file->offset) + "." +
589 sys::path::filename(Obj->getSourceFileName()).str();
591 for (auto &Sym : Obj->symbols()) {
592 cf.syms.push_back(ld_plugin_symbol());
593 ld_plugin_symbol &sym = cf.syms.back();
594 sym.version = nullptr;
595 StringRef Name = Sym.getName();
596 sym.name = strdup(Name.str().c_str());
598 ResolutionInfo &Res = ResInfo[Name];
600 Res.CanOmitFromDynSym &= Sym.canBeOmittedFromSymbolTable();
602 sym.visibility = LDPV_DEFAULT;
603 GlobalValue::VisibilityTypes Vis = Sym.getVisibility();
604 if (Vis != GlobalValue::DefaultVisibility)
605 Res.DefaultVisibility = false;
606 switch (Vis) {
607 case GlobalValue::DefaultVisibility:
608 break;
609 case GlobalValue::HiddenVisibility:
610 sym.visibility = LDPV_HIDDEN;
611 break;
612 case GlobalValue::ProtectedVisibility:
613 sym.visibility = LDPV_PROTECTED;
614 break;
617 if (Sym.isUndefined()) {
618 sym.def = LDPK_UNDEF;
619 if (Sym.isWeak())
620 sym.def = LDPK_WEAKUNDEF;
621 } else if (Sym.isCommon())
622 sym.def = LDPK_COMMON;
623 else if (Sym.isWeak())
624 sym.def = LDPK_WEAKDEF;
625 else
626 sym.def = LDPK_DEF;
628 sym.size = 0;
629 sym.comdat_key = nullptr;
630 int CI = Sym.getComdatIndex();
631 if (CI != -1) {
632 // Not setting comdat_key for nodeduplicate ensuress we don't deduplicate.
633 std::pair<StringRef, Comdat::SelectionKind> C = Obj->getComdatTable()[CI];
634 if (C.second != Comdat::NoDeduplicate)
635 sym.comdat_key = strdup(C.first.str().c_str());
638 sym.resolution = LDPR_UNKNOWN;
641 if (!cf.syms.empty()) {
642 if (add_symbols(cf.handle, cf.syms.size(), cf.syms.data()) != LDPS_OK) {
643 message(LDPL_ERROR, "Unable to add symbols!");
644 return LDPS_ERR;
648 // Handle any --wrap options passed to gold, which are than passed
649 // along to the plugin.
650 if (get_wrap_symbols) {
651 const char **wrap_symbols;
652 uint64_t count = 0;
653 if (get_wrap_symbols(&count, &wrap_symbols) != LDPS_OK) {
654 message(LDPL_ERROR, "Unable to get wrap symbols!");
655 return LDPS_ERR;
657 for (uint64_t i = 0; i < count; i++) {
658 StringRef Name = wrap_symbols[i];
659 ResolutionInfo &Res = ResInfo[Name];
660 ResolutionInfo &WrapRes = ResInfo["__wrap_" + Name.str()];
661 ResolutionInfo &RealRes = ResInfo["__real_" + Name.str()];
662 // Tell LTO not to inline symbols that will be overwritten.
663 Res.CanInline = false;
664 RealRes.CanInline = false;
665 // Tell LTO not to eliminate symbols that will be used after renaming.
666 Res.IsUsedInRegularObj = true;
667 WrapRes.IsUsedInRegularObj = true;
671 return LDPS_OK;
674 static void freeSymName(ld_plugin_symbol &Sym) {
675 free(Sym.name);
676 free(Sym.comdat_key);
677 Sym.name = nullptr;
678 Sym.comdat_key = nullptr;
681 /// Helper to get a file's symbols and a view into it via gold callbacks.
682 static const void *getSymbolsAndView(claimed_file &F) {
683 ld_plugin_status status = get_symbols(F.handle, F.syms.size(), F.syms.data());
684 if (status == LDPS_NO_SYMS)
685 return nullptr;
687 if (status != LDPS_OK)
688 message(LDPL_FATAL, "Failed to get symbol information");
690 const void *View;
691 if (get_view(F.handle, &View) != LDPS_OK)
692 message(LDPL_FATAL, "Failed to get a view of file");
694 return View;
697 /// Parse the thinlto-object-suffix-replace option into the \p OldSuffix and
698 /// \p NewSuffix strings, if it was specified.
699 static void getThinLTOOldAndNewSuffix(std::string &OldSuffix,
700 std::string &NewSuffix) {
701 assert(options::thinlto_object_suffix_replace.empty() ||
702 options::thinlto_object_suffix_replace.find(';') != StringRef::npos);
703 StringRef SuffixReplace = options::thinlto_object_suffix_replace;
704 auto Split = SuffixReplace.split(';');
705 OldSuffix = std::string(Split.first);
706 NewSuffix = std::string(Split.second);
709 /// Given the original \p Path to an output file, replace any filename
710 /// suffix matching \p OldSuffix with \p NewSuffix.
711 static std::string getThinLTOObjectFileName(StringRef Path, StringRef OldSuffix,
712 StringRef NewSuffix) {
713 if (Path.consume_back(OldSuffix))
714 return (Path + NewSuffix).str();
715 return std::string(Path);
718 // Returns true if S is valid as a C language identifier.
719 static bool isValidCIdentifier(StringRef S) {
720 return !S.empty() && (isAlpha(S[0]) || S[0] == '_') &&
721 llvm::all_of(llvm::drop_begin(S),
722 [](char C) { return C == '_' || isAlnum(C); });
725 static bool isUndefined(ld_plugin_symbol &Sym) {
726 return Sym.def == LDPK_UNDEF || Sym.def == LDPK_WEAKUNDEF;
729 static void addModule(LTO &Lto, claimed_file &F, const void *View,
730 StringRef Filename) {
731 MemoryBufferRef BufferRef(StringRef((const char *)View, F.filesize),
732 Filename);
733 Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef);
735 if (!ObjOrErr)
736 message(LDPL_FATAL, "Could not read bitcode from file : %s",
737 toString(ObjOrErr.takeError()).c_str());
739 unsigned SymNum = 0;
740 std::unique_ptr<InputFile> Input = std::move(ObjOrErr.get());
741 auto InputFileSyms = Input->symbols();
742 assert(InputFileSyms.size() == F.syms.size());
743 std::vector<SymbolResolution> Resols(F.syms.size());
744 for (ld_plugin_symbol &Sym : F.syms) {
745 const InputFile::Symbol &InpSym = InputFileSyms[SymNum];
746 SymbolResolution &R = Resols[SymNum++];
748 ld_plugin_symbol_resolution Resolution =
749 (ld_plugin_symbol_resolution)Sym.resolution;
751 ResolutionInfo &Res = ResInfo[Sym.name];
753 switch (Resolution) {
754 case LDPR_UNKNOWN:
755 llvm_unreachable("Unexpected resolution");
757 case LDPR_RESOLVED_IR:
758 case LDPR_RESOLVED_EXEC:
759 case LDPR_PREEMPTED_IR:
760 case LDPR_PREEMPTED_REG:
761 case LDPR_UNDEF:
762 break;
764 case LDPR_RESOLVED_DYN:
765 R.ExportDynamic = true;
766 break;
768 case LDPR_PREVAILING_DEF_IRONLY:
769 R.Prevailing = !isUndefined(Sym);
770 break;
772 case LDPR_PREVAILING_DEF:
773 R.Prevailing = !isUndefined(Sym);
774 R.VisibleToRegularObj = true;
775 break;
777 case LDPR_PREVAILING_DEF_IRONLY_EXP:
778 R.Prevailing = !isUndefined(Sym);
779 // Identify symbols exported dynamically, and that therefore could be
780 // referenced by a shared library not visible to the linker.
781 R.ExportDynamic = true;
782 if (!Res.CanOmitFromDynSym)
783 R.VisibleToRegularObj = true;
784 break;
787 // If the symbol has a C identifier section name, we need to mark
788 // it as visible to a regular object so that LTO will keep it around
789 // to ensure the linker generates special __start_<secname> and
790 // __stop_<secname> symbols which may be used elsewhere.
791 if (isValidCIdentifier(InpSym.getSectionName()))
792 R.VisibleToRegularObj = true;
794 if (Resolution != LDPR_RESOLVED_DYN && Resolution != LDPR_UNDEF &&
795 (IsExecutable || !Res.DefaultVisibility))
796 R.FinalDefinitionInLinkageUnit = true;
798 if (!Res.CanInline)
799 R.LinkerRedefined = true;
801 if (Res.IsUsedInRegularObj)
802 R.VisibleToRegularObj = true;
804 freeSymName(Sym);
807 check(Lto.add(std::move(Input), Resols),
808 std::string("Failed to link module ") + F.name);
811 static void recordFile(const std::string &Filename, bool TempOutFile) {
812 if (add_input_file(Filename.c_str()) != LDPS_OK)
813 message(LDPL_FATAL,
814 "Unable to add .o file to the link. File left behind in: %s",
815 Filename.c_str());
816 if (TempOutFile)
817 Cleanup.push_back(Filename);
820 /// Return the desired output filename given a base input name, a flag
821 /// indicating whether a temp file should be generated, and an optional task id.
822 /// The new filename generated is returned in \p NewFilename.
823 static int getOutputFileName(StringRef InFilename, bool TempOutFile,
824 SmallString<128> &NewFilename, int TaskID) {
825 int FD = -1;
826 if (TempOutFile) {
827 std::error_code EC =
828 sys::fs::createTemporaryFile("lto-llvm", "o", FD, NewFilename);
829 if (EC)
830 message(LDPL_FATAL, "Could not create temporary file: %s",
831 EC.message().c_str());
832 } else {
833 NewFilename = InFilename;
834 if (TaskID > 0)
835 NewFilename += utostr(TaskID);
836 std::error_code EC =
837 sys::fs::openFileForWrite(NewFilename, FD, sys::fs::CD_CreateAlways);
838 if (EC)
839 message(LDPL_FATAL, "Could not open file %s: %s", NewFilename.c_str(),
840 EC.message().c_str());
842 return FD;
845 /// Parse the thinlto_prefix_replace option into the \p OldPrefix and
846 /// \p NewPrefix strings, if it was specified.
847 static void getThinLTOOldAndNewPrefix(std::string &OldPrefix,
848 std::string &NewPrefix) {
849 StringRef PrefixReplace = options::thinlto_prefix_replace;
850 assert(PrefixReplace.empty() || PrefixReplace.find(';') != StringRef::npos);
851 auto Split = PrefixReplace.split(';');
852 OldPrefix = std::string(Split.first);
853 NewPrefix = std::string(Split.second);
856 /// Creates instance of LTO.
857 /// OnIndexWrite is callback to let caller know when LTO writes index files.
858 /// LinkedObjectsFile is an output stream to write the list of object files for
859 /// the final ThinLTO linking. Can be nullptr.
860 static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
861 raw_fd_ostream *LinkedObjectsFile) {
862 Config Conf;
863 ThinBackend Backend;
865 Conf.CPU = options::mcpu;
866 Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags(Triple());
868 // Disable the new X86 relax relocations since gold might not support them.
869 // FIXME: Check the gold version or add a new option to enable them.
870 Conf.Options.RelaxELFRelocations = false;
872 // Toggle function/data sections.
873 if (!codegen::getExplicitFunctionSections())
874 Conf.Options.FunctionSections = SplitSections;
875 if (!codegen::getExplicitDataSections())
876 Conf.Options.DataSections = SplitSections;
878 Conf.MAttrs = codegen::getMAttrs();
879 Conf.RelocModel = RelocationModel;
880 Conf.CodeModel = codegen::getExplicitCodeModel();
881 std::optional<CodeGenOpt::Level> CGOptLevelOrNone =
882 CodeGenOpt::getLevel(options::OptLevel);
883 assert(CGOptLevelOrNone && "Invalid optimization level");
884 Conf.CGOptLevel = *CGOptLevelOrNone;
885 Conf.DisableVerify = options::DisableVerify;
886 Conf.OptLevel = options::OptLevel;
887 Conf.PTO.LoopVectorization = options::OptLevel > 1;
888 Conf.PTO.SLPVectorization = options::OptLevel > 1;
889 Conf.AlwaysEmitRegularLTOObj = !options::obj_path.empty();
891 if (options::thinlto_index_only) {
892 std::string OldPrefix, NewPrefix;
893 getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
894 Backend = createWriteIndexesThinBackend(
895 OldPrefix, NewPrefix,
896 // TODO: Add support for optional native object path in
897 // thinlto_prefix_replace option to match lld.
898 /*NativeObjectPrefix=*/"", options::thinlto_emit_imports_files,
899 LinkedObjectsFile, OnIndexWrite);
900 } else {
901 Backend = createInProcessThinBackend(
902 llvm::heavyweight_hardware_concurrency(options::Parallelism));
905 Conf.OverrideTriple = options::triple;
906 Conf.DefaultTriple = sys::getDefaultTargetTriple();
908 Conf.DiagHandler = diagnosticHandler;
910 switch (options::TheOutputType) {
911 case options::OT_NORMAL:
912 break;
914 case options::OT_DISABLE:
915 Conf.PreOptModuleHook = [](size_t Task, const Module &M) { return false; };
916 break;
918 case options::OT_BC_ONLY:
919 Conf.PostInternalizeModuleHook = [](size_t Task, const Module &M) {
920 std::error_code EC;
921 SmallString<128> TaskFilename;
922 getOutputFileName(output_name, /* TempOutFile */ false, TaskFilename,
923 Task);
924 raw_fd_ostream OS(TaskFilename, EC, sys::fs::OpenFlags::OF_None);
925 if (EC)
926 message(LDPL_FATAL, "Failed to write the output file.");
927 WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ false);
928 return false;
930 break;
932 case options::OT_SAVE_TEMPS:
933 check(Conf.addSaveTemps(output_name + ".",
934 /* UseInputModulePath */ true));
935 break;
936 case options::OT_ASM_ONLY:
937 Conf.CGFileType = CGFT_AssemblyFile;
938 break;
941 if (!options::sample_profile.empty())
942 Conf.SampleProfile = options::sample_profile;
944 if (!options::cs_profile_path.empty())
945 Conf.CSIRProfile = options::cs_profile_path;
946 Conf.RunCSIRInstr = options::cs_pgo_gen;
948 Conf.DwoDir = options::dwo_dir;
950 // Set up optimization remarks handling.
951 Conf.RemarksFilename = options::RemarksFilename;
952 Conf.RemarksPasses = options::RemarksPasses;
953 Conf.RemarksWithHotness = options::RemarksWithHotness;
954 Conf.RemarksHotnessThreshold = options::RemarksHotnessThreshold;
955 Conf.RemarksFormat = options::RemarksFormat;
957 // Debug new pass manager if requested
958 Conf.DebugPassManager = options::debug_pass_manager;
960 Conf.HasWholeProgramVisibility = options::whole_program_visibility;
962 Conf.StatsFile = options::stats_file;
963 return std::make_unique<LTO>(std::move(Conf), Backend,
964 options::ParallelCodeGenParallelismLevel);
967 // Write empty files that may be expected by a distributed build
968 // system when invoked with thinlto_index_only. This is invoked when
969 // the linker has decided not to include the given module in the
970 // final link. Frequently the distributed build system will want to
971 // confirm that all expected outputs are created based on all of the
972 // modules provided to the linker.
973 // If SkipModule is true then .thinlto.bc should contain just
974 // SkipModuleByDistributedBackend flag which requests distributed backend
975 // to skip the compilation of the corresponding module and produce an empty
976 // object file.
977 static void writeEmptyDistributedBuildOutputs(const std::string &ModulePath,
978 const std::string &OldPrefix,
979 const std::string &NewPrefix,
980 bool SkipModule) {
981 std::string NewModulePath =
982 getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
983 std::error_code EC;
985 raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
986 sys::fs::OpenFlags::OF_None);
987 if (EC)
988 message(LDPL_FATAL, "Failed to write '%s': %s",
989 (NewModulePath + ".thinlto.bc").c_str(), EC.message().c_str());
991 if (SkipModule) {
992 ModuleSummaryIndex Index(/*HaveGVs*/ false);
993 Index.setSkipModuleByDistributedBackend();
994 writeIndexToFile(Index, OS, nullptr);
997 if (options::thinlto_emit_imports_files) {
998 raw_fd_ostream OS(NewModulePath + ".imports", EC,
999 sys::fs::OpenFlags::OF_None);
1000 if (EC)
1001 message(LDPL_FATAL, "Failed to write '%s': %s",
1002 (NewModulePath + ".imports").c_str(), EC.message().c_str());
1006 // Creates and returns output stream with a list of object files for final
1007 // linking of distributed ThinLTO.
1008 static std::unique_ptr<raw_fd_ostream> CreateLinkedObjectsFile() {
1009 if (options::thinlto_linked_objects_file.empty())
1010 return nullptr;
1011 assert(options::thinlto_index_only);
1012 std::error_code EC;
1013 auto LinkedObjectsFile = std::make_unique<raw_fd_ostream>(
1014 options::thinlto_linked_objects_file, EC, sys::fs::OpenFlags::OF_None);
1015 if (EC)
1016 message(LDPL_FATAL, "Failed to create '%s': %s",
1017 options::thinlto_linked_objects_file.c_str(), EC.message().c_str());
1018 return LinkedObjectsFile;
1021 /// Runs LTO and return a list of pairs <FileName, IsTemporary>.
1022 static std::vector<std::pair<SmallString<128>, bool>> runLTO() {
1023 // Map to own RAII objects that manage the file opening and releasing
1024 // interfaces with gold. This is needed only for ThinLTO mode, since
1025 // unlike regular LTO, where addModule will result in the opened file
1026 // being merged into a new combined module, we need to keep these files open
1027 // through Lto->run().
1028 DenseMap<void *, std::unique_ptr<PluginInputFile>> HandleToInputFile;
1030 // Owns string objects and tells if index file was already created.
1031 StringMap<bool> ObjectToIndexFileState;
1033 std::unique_ptr<raw_fd_ostream> LinkedObjects = CreateLinkedObjectsFile();
1034 std::unique_ptr<LTO> Lto = createLTO(
1035 [&ObjectToIndexFileState](const std::string &Identifier) {
1036 ObjectToIndexFileState[Identifier] = true;
1038 LinkedObjects.get());
1040 std::string OldPrefix, NewPrefix;
1041 if (options::thinlto_index_only)
1042 getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
1044 std::string OldSuffix, NewSuffix;
1045 getThinLTOOldAndNewSuffix(OldSuffix, NewSuffix);
1047 for (claimed_file &F : Modules) {
1048 if (options::thinlto && !HandleToInputFile.count(F.leader_handle))
1049 HandleToInputFile.insert(std::make_pair(
1050 F.leader_handle, std::make_unique<PluginInputFile>(F.handle)));
1051 // In case we are thin linking with a minimized bitcode file, ensure
1052 // the module paths encoded in the index reflect where the backends
1053 // will locate the full bitcode files for compiling/importing.
1054 std::string Identifier =
1055 getThinLTOObjectFileName(F.name, OldSuffix, NewSuffix);
1056 auto ObjFilename = ObjectToIndexFileState.insert({Identifier, false});
1057 assert(ObjFilename.second);
1058 if (const void *View = getSymbolsAndView(F))
1059 addModule(*Lto, F, View, ObjFilename.first->first());
1060 else if (options::thinlto_index_only) {
1061 ObjFilename.first->second = true;
1062 writeEmptyDistributedBuildOutputs(Identifier, OldPrefix, NewPrefix,
1063 /* SkipModule */ true);
1067 SmallString<128> Filename;
1068 // Note that getOutputFileName will append a unique ID for each task
1069 if (!options::obj_path.empty())
1070 Filename = options::obj_path;
1071 else if (options::TheOutputType == options::OT_SAVE_TEMPS)
1072 Filename = output_name + ".lto.o";
1073 else if (options::TheOutputType == options::OT_ASM_ONLY)
1074 Filename = output_name;
1075 bool SaveTemps = !Filename.empty();
1077 size_t MaxTasks = Lto->getMaxTasks();
1078 std::vector<std::pair<SmallString<128>, bool>> Files(MaxTasks);
1080 auto AddStream =
1081 [&](size_t Task,
1082 const Twine &ModuleName) -> std::unique_ptr<CachedFileStream> {
1083 Files[Task].second = !SaveTemps;
1084 int FD = getOutputFileName(Filename, /* TempOutFile */ !SaveTemps,
1085 Files[Task].first, Task);
1086 return std::make_unique<CachedFileStream>(
1087 std::make_unique<llvm::raw_fd_ostream>(FD, true));
1090 auto AddBuffer = [&](size_t Task, const Twine &moduleName,
1091 std::unique_ptr<MemoryBuffer> MB) {
1092 *AddStream(Task, moduleName)->OS << MB->getBuffer();
1095 FileCache Cache;
1096 if (!options::cache_dir.empty())
1097 Cache = check(localCache("ThinLTO", "Thin", options::cache_dir, AddBuffer));
1099 check(Lto->run(AddStream, Cache));
1101 // Write empty output files that may be expected by the distributed build
1102 // system.
1103 if (options::thinlto_index_only)
1104 for (auto &Identifier : ObjectToIndexFileState)
1105 if (!Identifier.getValue())
1106 writeEmptyDistributedBuildOutputs(std::string(Identifier.getKey()),
1107 OldPrefix, NewPrefix,
1108 /* SkipModule */ false);
1110 return Files;
1113 /// gold informs us that all symbols have been read. At this point, we use
1114 /// get_symbols to see if any of our definitions have been overridden by a
1115 /// native object file. Then, perform optimization and codegen.
1116 static ld_plugin_status allSymbolsReadHook() {
1117 if (Modules.empty())
1118 return LDPS_OK;
1120 if (unsigned NumOpts = options::extra.size())
1121 cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
1123 std::vector<std::pair<SmallString<128>, bool>> Files = runLTO();
1125 if (options::TheOutputType == options::OT_DISABLE ||
1126 options::TheOutputType == options::OT_BC_ONLY ||
1127 options::TheOutputType == options::OT_ASM_ONLY)
1128 return LDPS_OK;
1130 if (options::thinlto_index_only) {
1131 llvm_shutdown();
1132 cleanup_hook();
1133 exit(0);
1136 for (const auto &F : Files)
1137 if (!F.first.empty())
1138 recordFile(std::string(F.first.str()), F.second);
1140 if (!options::extra_library_path.empty() &&
1141 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
1142 message(LDPL_FATAL, "Unable to set the extra library path.");
1144 return LDPS_OK;
1147 static ld_plugin_status all_symbols_read_hook(void) {
1148 ld_plugin_status Ret = allSymbolsReadHook();
1149 llvm_shutdown();
1151 if (options::TheOutputType == options::OT_BC_ONLY ||
1152 options::TheOutputType == options::OT_ASM_ONLY ||
1153 options::TheOutputType == options::OT_DISABLE) {
1154 if (options::TheOutputType == options::OT_DISABLE) {
1155 // Remove the output file here since ld.bfd creates the output file
1156 // early.
1157 std::error_code EC = sys::fs::remove(output_name);
1158 if (EC)
1159 message(LDPL_ERROR, "Failed to delete '%s': %s", output_name.c_str(),
1160 EC.message().c_str());
1162 exit(0);
1165 return Ret;
1168 static ld_plugin_status cleanup_hook(void) {
1169 for (std::string &Name : Cleanup) {
1170 std::error_code EC = sys::fs::remove(Name);
1171 if (EC)
1172 message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(),
1173 EC.message().c_str());
1176 // Prune cache
1177 if (!options::cache_dir.empty()) {
1178 CachePruningPolicy policy = check(parseCachePruningPolicy(options::cache_policy));
1179 pruneCache(options::cache_dir, policy);
1182 return LDPS_OK;