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 .
11 //
12 //===----------------------------------------------------------------------===//
13 
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>
39 
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
43 
44 #define LDPT_GET_SYMBOLS_V3 28
45 
46 // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
47 // required version.
48 #define LDPT_GET_WRAP_SYMBOLS 32
49 
50 using namespace llvm;
51 using namespace lto;
52 
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);
57 
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();
62 }
63 
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;
68 
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;
76 };
77 
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;
82 
PluginInputFile__anone9f7df2a0111::PluginInputFile83   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");
87   }
~PluginInputFile__anone9f7df2a0111::PluginInputFile88   ~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");
94   }
95 
file__anone9f7df2a0111::PluginInputFile96   ld_plugin_input_file &file() { return *File; }
97 
98   PluginInputFile(PluginInputFile &&RHS) = default;
99   PluginInputFile &operator=(PluginInputFile &&RHS) = default;
100 };
101 
102 struct ResolutionInfo {
103   bool CanOmitFromDynSym = true;
104   bool DefaultVisibility = true;
105   bool CanInline = true;
106   bool IsUsedInRegularObj = false;
107 };
108 
109 }
110 
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;
124 
125 namespace options {
126   enum OutputType {
127     OT_NORMAL,
128     OT_DISABLE,
129     OT_BC_ONLY,
130     OT_ASM_ONLY,
131     OT_SAVE_TEMPS
132   };
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;
207 
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;
213 
214   // Context sensitive PGO options.
215   static std::string cs_profile_path;
216   static bool cs_pgo_gen = false;
217 
process_plugin_option(const char * opt_)218   static void process_plugin_option(const char *opt_)
219   {
220     if (opt_ == nullptr)
221       return;
222     llvm::StringRef opt = opt_;
223 
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");
304 
305       extra.push_back(opt_);
306     }
307   }
308 }
309 
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);
314 
315 extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
onload(ld_plugin_tv * tv)316 ld_plugin_status onload(ld_plugin_tv *tv) {
317   InitializeAllTargetInfos();
318   InitializeAllTargets();
319   InitializeAllTargetMCs();
320   InitializeAllAsmParsers();
321   InitializeAllAsmPrinters();
322 
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.
328 
329   bool registeredClaimFile = false;
330   bool RegisteredAllSymbolsRead = false;
331 
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;
361       }
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;
369 
370       if (callback(claim_file_hook) != LDPS_OK)
371         return LDPS_ERR;
372 
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;
378 
379       if (callback(all_symbols_read_hook) != LDPS_OK)
380         return LDPS_ERR;
381 
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;
387 
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;
429     }
430   }
431 
432   if (!registeredClaimFile) {
433     message(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
434     return LDPS_ERR;
435   }
436   if (!add_symbols) {
437     message(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
438     return LDPS_ERR;
439   }
440 
441   if (!RegisteredAllSymbolsRead)
442     return LDPS_OK;
443 
444   if (!get_input_file) {
445     message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
446     return LDPS_ERR;
447   }
448   if (!release_input_file) {
449     message(LDPL_ERROR, "release_input_file not passed to LLVMgold.");
450     return LDPS_ERR;
451   }
452 
453   return LDPS_OK;
454 }
455 
diagnosticHandler(const DiagnosticInfo & DI)456 static void diagnosticHandler(const DiagnosticInfo &DI) {
457   std::string ErrStorage;
458   {
459     raw_string_ostream OS(ErrStorage);
460     DiagnosticPrinterRawOStream DP(OS);
461     DI.print(DP);
462   }
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;
475   }
476   message(Level, "LLVM gold plugin: %s",  ErrStorage.c_str());
477 }
478 
check(Error E,std::string Msg="LLVM gold plugin")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();
483   });
484 }
485 
check(Expected<T> E)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();
491 }
492 
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.
claim_file_hook(const ld_plugin_input_file * file,int * claimed)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;
505     }
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;
514     }
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;
521     }
522     Buffer = std::move(BufferOrErr.get());
523     BufferRef = Buffer->getMemBufferRef();
524   }
525 
526   *claimed = 1;
527 
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());
539     });
540 
541     return *claimed ? LDPS_ERR : LDPS_OK;
542   }
543 
544   std::unique_ptr<InputFile> Obj = std::move(*ObjOrErr);
545 
546   Modules.emplace_back();
547   claimed_file &cf = Modules.back();
548 
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();
567 
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());
574 
575     ResolutionInfo &Res = ResInfo[Name];
576 
577     Res.CanOmitFromDynSym &= Sym.canBeOmittedFromSymbolTable();
578 
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;
592     }
593 
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;
604 
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());
611     }
612 
613     sym.resolution = LDPR_UNKNOWN;
614   }
615 
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;
620     }
621   }
622 
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;
631     }
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;
643     }
644   }
645 
646   return LDPS_OK;
647 }
648 
freeSymName(ld_plugin_symbol & Sym)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;
654 }
655 
656 /// Helper to get a file's symbols and a view into it via gold callbacks.
getSymbolsAndView(claimed_file & F)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;
661 
662   if (status != LDPS_OK)
663     message(LDPL_FATAL, "Failed to get symbol information");
664 
665   const void *View;
666   if (get_view(F.handle, &View) != LDPS_OK)
667     message(LDPL_FATAL, "Failed to get a view of file");
668 
669   return View;
670 }
671 
672 /// Parse the thinlto-object-suffix-replace option into the \p OldSuffix and
673 /// \p NewSuffix strings, if it was specified.
getThinLTOOldAndNewSuffix(std::string & OldSuffix,std::string & NewSuffix)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(';');
680 }
681 
682 /// Given the original \p Path to an output file, replace any filename
683 /// suffix matching \p OldSuffix with \p NewSuffix.
getThinLTOObjectFileName(StringRef Path,StringRef OldSuffix,StringRef 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;
689 }
690 
691 // Returns true if S is valid as a C language identifier.
isValidCIdentifier(StringRef S)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); });
696 }
697 
isUndefined(ld_plugin_symbol & Sym)698 static bool isUndefined(ld_plugin_symbol &Sym) {
699   return Sym.def == LDPK_UNDEF || Sym.def == LDPK_WEAKUNDEF;
700 }
701 
addModule(LTO & Lto,claimed_file & F,const void * View,StringRef Filename)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);
707 
708   if (!ObjOrErr)
709     message(LDPL_FATAL, "Could not read bitcode from file : %s",
710             toString(ObjOrErr.takeError()).c_str());
711 
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++];
720 
721     ld_plugin_symbol_resolution Resolution =
722         (ld_plugin_symbol_resolution)Sym.resolution;
723 
724     ResolutionInfo &Res = ResInfo[Sym.name];
725 
726     switch (Resolution) {
727     case LDPR_UNKNOWN:
728       llvm_unreachable("Unexpected resolution");
729 
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;
737 
738     case LDPR_PREVAILING_DEF_IRONLY:
739       R.Prevailing = !isUndefined(Sym);
740       break;
741 
742     case LDPR_PREVAILING_DEF:
743       R.Prevailing = !isUndefined(Sym);
744       R.VisibleToRegularObj = true;
745       break;
746 
747     case LDPR_PREVAILING_DEF_IRONLY_EXP:
748       R.Prevailing = !isUndefined(Sym);
749       if (!Res.CanOmitFromDynSym)
750         R.VisibleToRegularObj = true;
751       break;
752     }
753 
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;
760 
761     if (Resolution != LDPR_RESOLVED_DYN && Resolution != LDPR_UNDEF &&
762         (IsExecutable || !Res.DefaultVisibility))
763       R.FinalDefinitionInLinkageUnit = true;
764 
765     if (!Res.CanInline)
766       R.LinkerRedefined = true;
767 
768     if (Res.IsUsedInRegularObj)
769       R.VisibleToRegularObj = true;
770 
771     freeSymName(Sym);
772   }
773 
774   check(Lto.add(std::move(Input), Resols),
775         std::string("Failed to link module ") + F.name);
776 }
777 
recordFile(const std::string & Filename,bool TempOutFile)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);
785 }
786 
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.
getOutputFileName(StringRef InFilename,bool TempOutFile,SmallString<128> & NewFilename,int TaskID)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());
808   }
809   return FD;
810 }
811 
getCGOptLevel()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;
822   }
823   llvm_unreachable("Invalid optimization level");
824 }
825 
826 /// Parse the thinlto_prefix_replace option into the \p OldPrefix and
827 /// \p NewPrefix strings, if it was specified.
getThinLTOOldAndNewPrefix(std::string & OldPrefix,std::string & NewPrefix)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(';');
833 }
834 
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.
createLTO(IndexWriteCallback OnIndexWrite,raw_fd_ostream * LinkedObjectsFile)839 static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
840                                       raw_fd_ostream *LinkedObjectsFile) {
841   Config Conf;
842   ThinBackend Backend;
843 
844   Conf.CPU = options::mcpu;
845   Conf.Options = InitTargetOptionsFromCodeGenFlags();
846 
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;
850 
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;
856 
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   Conf.PTO.LoopVectorization = options::OptLevel > 1;
864   Conf.PTO.SLPVectorization = options::OptLevel > 1;
865 
866   if (options::Parallelism)
867     Backend = createInProcessThinBackend(options::Parallelism);
868   if (options::thinlto_index_only) {
869     std::string OldPrefix, NewPrefix;
870     getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
871     Backend = createWriteIndexesThinBackend(OldPrefix, NewPrefix,
872                                             options::thinlto_emit_imports_files,
873                                             LinkedObjectsFile, OnIndexWrite);
874   }
875 
876   Conf.OverrideTriple = options::triple;
877   Conf.DefaultTriple = sys::getDefaultTargetTriple();
878 
879   Conf.DiagHandler = diagnosticHandler;
880 
881   switch (options::TheOutputType) {
882   case options::OT_NORMAL:
883     break;
884 
885   case options::OT_DISABLE:
886     Conf.PreOptModuleHook = [](size_t Task, const Module &M) { return false; };
887     break;
888 
889   case options::OT_BC_ONLY:
890     Conf.PostInternalizeModuleHook = [](size_t Task, const Module &M) {
891       std::error_code EC;
892       raw_fd_ostream OS(output_name, EC, sys::fs::OpenFlags::OF_None);
893       if (EC)
894         message(LDPL_FATAL, "Failed to write the output file.");
895       WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ false);
896       return false;
897     };
898     break;
899 
900   case options::OT_SAVE_TEMPS:
901     check(Conf.addSaveTemps(output_name + ".",
902                             /* UseInputModulePath */ true));
903     break;
904   case options::OT_ASM_ONLY:
905     Conf.CGFileType = CGFT_AssemblyFile;
906     break;
907   }
908 
909   if (!options::sample_profile.empty())
910     Conf.SampleProfile = options::sample_profile;
911 
912   if (!options::cs_profile_path.empty())
913     Conf.CSIRProfile = options::cs_profile_path;
914   Conf.RunCSIRInstr = options::cs_pgo_gen;
915 
916   Conf.DwoDir = options::dwo_dir;
917 
918   // Set up optimization remarks handling.
919   Conf.RemarksFilename = options::RemarksFilename;
920   Conf.RemarksPasses = options::RemarksPasses;
921   Conf.RemarksWithHotness = options::RemarksWithHotness;
922   Conf.RemarksFormat = options::RemarksFormat;
923 
924   // Use new pass manager if set in driver
925   Conf.UseNewPM = options::new_pass_manager;
926   // Debug new pass manager if requested
927   Conf.DebugPassManager = options::debug_pass_manager;
928 
929   Conf.StatsFile = options::stats_file;
930   return std::make_unique<LTO>(std::move(Conf), Backend,
931                                 options::ParallelCodeGenParallelismLevel);
932 }
933 
934 // Write empty files that may be expected by a distributed build
935 // system when invoked with thinlto_index_only. This is invoked when
936 // the linker has decided not to include the given module in the
937 // final link. Frequently the distributed build system will want to
938 // confirm that all expected outputs are created based on all of the
939 // modules provided to the linker.
940 // If SkipModule is true then .thinlto.bc should contain just
941 // SkipModuleByDistributedBackend flag which requests distributed backend
942 // to skip the compilation of the corresponding module and produce an empty
943 // object file.
writeEmptyDistributedBuildOutputs(const std::string & ModulePath,const std::string & OldPrefix,const std::string & NewPrefix,bool SkipModule)944 static void writeEmptyDistributedBuildOutputs(const std::string &ModulePath,
945                                               const std::string &OldPrefix,
946                                               const std::string &NewPrefix,
947                                               bool SkipModule) {
948   std::string NewModulePath =
949       getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
950   std::error_code EC;
951   {
952     raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
953                       sys::fs::OpenFlags::OF_None);
954     if (EC)
955       message(LDPL_FATAL, "Failed to write '%s': %s",
956               (NewModulePath + ".thinlto.bc").c_str(), EC.message().c_str());
957 
958     if (SkipModule) {
959       ModuleSummaryIndex Index(/*HaveGVs*/ false);
960       Index.setSkipModuleByDistributedBackend();
961       WriteIndexToFile(Index, OS, nullptr);
962     }
963   }
964   if (options::thinlto_emit_imports_files) {
965     raw_fd_ostream OS(NewModulePath + ".imports", EC,
966                       sys::fs::OpenFlags::OF_None);
967     if (EC)
968       message(LDPL_FATAL, "Failed to write '%s': %s",
969               (NewModulePath + ".imports").c_str(), EC.message().c_str());
970   }
971 }
972 
973 // Creates and returns output stream with a list of object files for final
974 // linking of distributed ThinLTO.
CreateLinkedObjectsFile()975 static std::unique_ptr<raw_fd_ostream> CreateLinkedObjectsFile() {
976   if (options::thinlto_linked_objects_file.empty())
977     return nullptr;
978   assert(options::thinlto_index_only);
979   std::error_code EC;
980   auto LinkedObjectsFile = std::make_unique<raw_fd_ostream>(
981       options::thinlto_linked_objects_file, EC, sys::fs::OpenFlags::OF_None);
982   if (EC)
983     message(LDPL_FATAL, "Failed to create '%s': %s",
984             options::thinlto_linked_objects_file.c_str(), EC.message().c_str());
985   return LinkedObjectsFile;
986 }
987 
988 /// Runs LTO and return a list of pairs <FileName, IsTemporary>.
runLTO()989 static std::vector<std::pair<SmallString<128>, bool>> runLTO() {
990   // Map to own RAII objects that manage the file opening and releasing
991   // interfaces with gold. This is needed only for ThinLTO mode, since
992   // unlike regular LTO, where addModule will result in the opened file
993   // being merged into a new combined module, we need to keep these files open
994   // through Lto->run().
995   DenseMap<void *, std::unique_ptr<PluginInputFile>> HandleToInputFile;
996 
997   // Owns string objects and tells if index file was already created.
998   StringMap<bool> ObjectToIndexFileState;
999 
1000   std::unique_ptr<raw_fd_ostream> LinkedObjects = CreateLinkedObjectsFile();
1001   std::unique_ptr<LTO> Lto = createLTO(
1002       [&ObjectToIndexFileState](const std::string &Identifier) {
1003         ObjectToIndexFileState[Identifier] = true;
1004       },
1005       LinkedObjects.get());
1006 
1007   std::string OldPrefix, NewPrefix;
1008   if (options::thinlto_index_only)
1009     getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
1010 
1011   std::string OldSuffix, NewSuffix;
1012   getThinLTOOldAndNewSuffix(OldSuffix, NewSuffix);
1013 
1014   for (claimed_file &F : Modules) {
1015     if (options::thinlto && !HandleToInputFile.count(F.leader_handle))
1016       HandleToInputFile.insert(std::make_pair(
1017           F.leader_handle, std::make_unique<PluginInputFile>(F.handle)));
1018     // In case we are thin linking with a minimized bitcode file, ensure
1019     // the module paths encoded in the index reflect where the backends
1020     // will locate the full bitcode files for compiling/importing.
1021     std::string Identifier =
1022         getThinLTOObjectFileName(F.name, OldSuffix, NewSuffix);
1023     auto ObjFilename = ObjectToIndexFileState.insert({Identifier, false});
1024     assert(ObjFilename.second);
1025     if (const void *View = getSymbolsAndView(F))
1026       addModule(*Lto, F, View, ObjFilename.first->first());
1027     else if (options::thinlto_index_only) {
1028       ObjFilename.first->second = true;
1029       writeEmptyDistributedBuildOutputs(Identifier, OldPrefix, NewPrefix,
1030                                         /* SkipModule */ true);
1031     }
1032   }
1033 
1034   SmallString<128> Filename;
1035   // Note that getOutputFileName will append a unique ID for each task
1036   if (!options::obj_path.empty())
1037     Filename = options::obj_path;
1038   else if (options::TheOutputType == options::OT_SAVE_TEMPS)
1039     Filename = output_name + ".o";
1040   else if (options::TheOutputType == options::OT_ASM_ONLY)
1041     Filename = output_name;
1042   bool SaveTemps = !Filename.empty();
1043 
1044   size_t MaxTasks = Lto->getMaxTasks();
1045   std::vector<std::pair<SmallString<128>, bool>> Files(MaxTasks);
1046 
1047   auto AddStream =
1048       [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
1049     Files[Task].second = !SaveTemps;
1050     int FD = getOutputFileName(Filename, /* TempOutFile */ !SaveTemps,
1051                                Files[Task].first, Task);
1052     return std::make_unique<lto::NativeObjectStream>(
1053         std::make_unique<llvm::raw_fd_ostream>(FD, true));
1054   };
1055 
1056   auto AddBuffer = [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
1057     *AddStream(Task)->OS << MB->getBuffer();
1058   };
1059 
1060   NativeObjectCache Cache;
1061   if (!options::cache_dir.empty())
1062     Cache = check(localCache(options::cache_dir, AddBuffer));
1063 
1064   check(Lto->run(AddStream, Cache));
1065 
1066   // Write empty output files that may be expected by the distributed build
1067   // system.
1068   if (options::thinlto_index_only)
1069     for (auto &Identifier : ObjectToIndexFileState)
1070       if (!Identifier.getValue())
1071         writeEmptyDistributedBuildOutputs(Identifier.getKey(), OldPrefix,
1072                                           NewPrefix, /* SkipModule */ false);
1073 
1074   return Files;
1075 }
1076 
1077 /// gold informs us that all symbols have been read. At this point, we use
1078 /// get_symbols to see if any of our definitions have been overridden by a
1079 /// native object file. Then, perform optimization and codegen.
allSymbolsReadHook()1080 static ld_plugin_status allSymbolsReadHook() {
1081   if (Modules.empty())
1082     return LDPS_OK;
1083 
1084   if (unsigned NumOpts = options::extra.size())
1085     cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
1086 
1087   std::vector<std::pair<SmallString<128>, bool>> Files = runLTO();
1088 
1089   if (options::TheOutputType == options::OT_DISABLE ||
1090       options::TheOutputType == options::OT_BC_ONLY ||
1091       options::TheOutputType == options::OT_ASM_ONLY)
1092     return LDPS_OK;
1093 
1094   if (options::thinlto_index_only) {
1095     llvm_shutdown();
1096     cleanup_hook();
1097     exit(0);
1098   }
1099 
1100   for (const auto &F : Files)
1101     if (!F.first.empty())
1102       recordFile(F.first.str(), F.second);
1103 
1104   if (!options::extra_library_path.empty() &&
1105       set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
1106     message(LDPL_FATAL, "Unable to set the extra library path.");
1107 
1108   return LDPS_OK;
1109 }
1110 
all_symbols_read_hook(void)1111 static ld_plugin_status all_symbols_read_hook(void) {
1112   ld_plugin_status Ret = allSymbolsReadHook();
1113   llvm_shutdown();
1114 
1115   if (options::TheOutputType == options::OT_BC_ONLY ||
1116       options::TheOutputType == options::OT_ASM_ONLY ||
1117       options::TheOutputType == options::OT_DISABLE) {
1118     if (options::TheOutputType == options::OT_DISABLE) {
1119       // Remove the output file here since ld.bfd creates the output file
1120       // early.
1121       std::error_code EC = sys::fs::remove(output_name);
1122       if (EC)
1123         message(LDPL_ERROR, "Failed to delete '%s': %s", output_name.c_str(),
1124                 EC.message().c_str());
1125     }
1126     exit(0);
1127   }
1128 
1129   return Ret;
1130 }
1131 
cleanup_hook(void)1132 static ld_plugin_status cleanup_hook(void) {
1133   for (std::string &Name : Cleanup) {
1134     std::error_code EC = sys::fs::remove(Name);
1135     if (EC)
1136       message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(),
1137               EC.message().c_str());
1138   }
1139 
1140   // Prune cache
1141   if (!options::cache_dir.empty()) {
1142     CachePruningPolicy policy = check(parseCachePruningPolicy(options::cache_policy));
1143     pruneCache(options::cache_dir, policy);
1144   }
1145 
1146   return LDPS_OK;
1147 }
1148