xref: /openbsd-src/gnu/llvm/lld/MachO/DriverUtils.cpp (revision dfe94b169149f14cc1aee2cf6dad58a8d9a1860c)
11cf9926bSpatrick //===- DriverUtils.cpp ----------------------------------------------------===//
21cf9926bSpatrick //
31cf9926bSpatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41cf9926bSpatrick // See https://llvm.org/LICENSE.txt for license information.
51cf9926bSpatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61cf9926bSpatrick //
71cf9926bSpatrick //===----------------------------------------------------------------------===//
81cf9926bSpatrick 
91cf9926bSpatrick #include "Config.h"
101cf9926bSpatrick #include "Driver.h"
111cf9926bSpatrick #include "InputFiles.h"
121cf9926bSpatrick #include "ObjC.h"
131cf9926bSpatrick #include "Target.h"
141cf9926bSpatrick 
151cf9926bSpatrick #include "lld/Common/Args.h"
16*dfe94b16Srobert #include "lld/Common/CommonLinkerContext.h"
171cf9926bSpatrick #include "lld/Common/Reproduce.h"
181cf9926bSpatrick #include "llvm/ADT/CachedHashString.h"
191cf9926bSpatrick #include "llvm/ADT/DenseMap.h"
201cf9926bSpatrick #include "llvm/LTO/LTO.h"
211cf9926bSpatrick #include "llvm/Option/Arg.h"
221cf9926bSpatrick #include "llvm/Option/ArgList.h"
231cf9926bSpatrick #include "llvm/Option/Option.h"
241cf9926bSpatrick #include "llvm/Support/CommandLine.h"
251cf9926bSpatrick #include "llvm/Support/FileSystem.h"
261cf9926bSpatrick #include "llvm/Support/Path.h"
271cf9926bSpatrick #include "llvm/TextAPI/InterfaceFile.h"
281cf9926bSpatrick #include "llvm/TextAPI/TextAPIReader.h"
291cf9926bSpatrick 
301cf9926bSpatrick using namespace llvm;
311cf9926bSpatrick using namespace llvm::MachO;
321cf9926bSpatrick using namespace llvm::opt;
331cf9926bSpatrick using namespace llvm::sys;
341cf9926bSpatrick using namespace lld;
351cf9926bSpatrick using namespace lld::macho;
361cf9926bSpatrick 
371cf9926bSpatrick // Create prefix string literals used in Options.td
38*dfe94b16Srobert #define PREFIX(NAME, VALUE)                                                    \
39*dfe94b16Srobert   static constexpr StringLiteral NAME##_init[] = VALUE;                        \
40*dfe94b16Srobert   static constexpr ArrayRef<StringLiteral> NAME(NAME##_init,                   \
41*dfe94b16Srobert                                                 std::size(NAME##_init) - 1);
421cf9926bSpatrick #include "Options.inc"
431cf9926bSpatrick #undef PREFIX
441cf9926bSpatrick 
451cf9926bSpatrick // Create table mapping all options defined in Options.td
46*dfe94b16Srobert static constexpr OptTable::Info optInfo[] = {
471cf9926bSpatrick #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
481cf9926bSpatrick   {X1, X2, X10,         X11,         OPT_##ID, Option::KIND##Class,            \
491cf9926bSpatrick    X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
501cf9926bSpatrick #include "Options.inc"
511cf9926bSpatrick #undef OPTION
521cf9926bSpatrick };
531cf9926bSpatrick 
MachOOptTable()54*dfe94b16Srobert MachOOptTable::MachOOptTable() : GenericOptTable(optInfo) {}
551cf9926bSpatrick 
561cf9926bSpatrick // Set color diagnostics according to --color-diagnostics={auto,always,never}
571cf9926bSpatrick // or --no-color-diagnostics flags.
handleColorDiagnostics(InputArgList & args)581cf9926bSpatrick static void handleColorDiagnostics(InputArgList &args) {
591cf9926bSpatrick   const Arg *arg =
601cf9926bSpatrick       args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
611cf9926bSpatrick                       OPT_no_color_diagnostics);
621cf9926bSpatrick   if (!arg)
631cf9926bSpatrick     return;
641cf9926bSpatrick   if (arg->getOption().getID() == OPT_color_diagnostics) {
651cf9926bSpatrick     lld::errs().enable_colors(true);
661cf9926bSpatrick   } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
671cf9926bSpatrick     lld::errs().enable_colors(false);
681cf9926bSpatrick   } else {
691cf9926bSpatrick     StringRef s = arg->getValue();
701cf9926bSpatrick     if (s == "always")
711cf9926bSpatrick       lld::errs().enable_colors(true);
721cf9926bSpatrick     else if (s == "never")
731cf9926bSpatrick       lld::errs().enable_colors(false);
741cf9926bSpatrick     else if (s != "auto")
751cf9926bSpatrick       error("unknown option: --color-diagnostics=" + s);
761cf9926bSpatrick   }
771cf9926bSpatrick }
781cf9926bSpatrick 
parse(ArrayRef<const char * > argv)791cf9926bSpatrick InputArgList MachOOptTable::parse(ArrayRef<const char *> argv) {
801cf9926bSpatrick   // Make InputArgList from string vectors.
811cf9926bSpatrick   unsigned missingIndex;
821cf9926bSpatrick   unsigned missingCount;
831cf9926bSpatrick   SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
841cf9926bSpatrick 
851cf9926bSpatrick   // Expand response files (arguments in the form of @<filename>)
861cf9926bSpatrick   // and then parse the argument again.
87*dfe94b16Srobert   cl::ExpandResponseFiles(saver(), cl::TokenizeGNUCommandLine, vec);
881cf9926bSpatrick   InputArgList args = ParseArgs(vec, missingIndex, missingCount);
891cf9926bSpatrick 
901cf9926bSpatrick   // Handle -fatal_warnings early since it converts missing argument warnings
911cf9926bSpatrick   // to errors.
921cf9926bSpatrick   errorHandler().fatalWarnings = args.hasArg(OPT_fatal_warnings);
93*dfe94b16Srobert   errorHandler().suppressWarnings = args.hasArg(OPT_w);
941cf9926bSpatrick 
951cf9926bSpatrick   if (missingCount)
961cf9926bSpatrick     error(Twine(args.getArgString(missingIndex)) + ": missing argument");
971cf9926bSpatrick 
981cf9926bSpatrick   handleColorDiagnostics(args);
991cf9926bSpatrick 
1001cf9926bSpatrick   for (const Arg *arg : args.filtered(OPT_UNKNOWN)) {
1011cf9926bSpatrick     std::string nearest;
1021cf9926bSpatrick     if (findNearest(arg->getAsString(args), nearest) > 1)
1031cf9926bSpatrick       error("unknown argument '" + arg->getAsString(args) + "'");
1041cf9926bSpatrick     else
1051cf9926bSpatrick       error("unknown argument '" + arg->getAsString(args) +
1061cf9926bSpatrick             "', did you mean '" + nearest + "'");
1071cf9926bSpatrick   }
1081cf9926bSpatrick   return args;
1091cf9926bSpatrick }
1101cf9926bSpatrick 
printHelp(const char * argv0,bool showHidden) const1111cf9926bSpatrick void MachOOptTable::printHelp(const char *argv0, bool showHidden) const {
1121cf9926bSpatrick   OptTable::printHelp(lld::outs(),
1131cf9926bSpatrick                       (std::string(argv0) + " [options] file...").c_str(),
1141cf9926bSpatrick                       "LLVM Linker", showHidden);
1151cf9926bSpatrick   lld::outs() << "\n";
1161cf9926bSpatrick }
1171cf9926bSpatrick 
rewritePath(StringRef s)1181cf9926bSpatrick static std::string rewritePath(StringRef s) {
1191cf9926bSpatrick   if (fs::exists(s))
1201cf9926bSpatrick     return relativeToRoot(s);
1211cf9926bSpatrick   return std::string(s);
1221cf9926bSpatrick }
1231cf9926bSpatrick 
rewriteInputPath(StringRef s)1241cf9926bSpatrick static std::string rewriteInputPath(StringRef s) {
1251cf9926bSpatrick   // Don't bother rewriting "absolute" paths that are actually under the
1261cf9926bSpatrick   // syslibroot; simply rewriting the syslibroot is sufficient.
1271cf9926bSpatrick   if (rerootPath(s) == s && fs::exists(s))
1281cf9926bSpatrick     return relativeToRoot(s);
1291cf9926bSpatrick   return std::string(s);
1301cf9926bSpatrick }
1311cf9926bSpatrick 
1321cf9926bSpatrick // Reconstructs command line arguments so that so that you can re-run
1331cf9926bSpatrick // the same command with the same inputs. This is for --reproduce.
createResponseFile(const InputArgList & args)1341cf9926bSpatrick std::string macho::createResponseFile(const InputArgList &args) {
1351cf9926bSpatrick   SmallString<0> data;
1361cf9926bSpatrick   raw_svector_ostream os(data);
1371cf9926bSpatrick 
1381cf9926bSpatrick   // Copy the command line to the output while rewriting paths.
1391cf9926bSpatrick   for (const Arg *arg : args) {
1401cf9926bSpatrick     switch (arg->getOption().getID()) {
1411cf9926bSpatrick     case OPT_reproduce:
1421cf9926bSpatrick       break;
1431cf9926bSpatrick     case OPT_INPUT:
1441cf9926bSpatrick       os << quote(rewriteInputPath(arg->getValue())) << "\n";
1451cf9926bSpatrick       break;
1461cf9926bSpatrick     case OPT_o:
1471cf9926bSpatrick       os << "-o " << quote(path::filename(arg->getValue())) << "\n";
1481cf9926bSpatrick       break;
1491cf9926bSpatrick     case OPT_filelist:
150*dfe94b16Srobert       if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
1511cf9926bSpatrick         for (StringRef path : args::getLines(*buffer))
1521cf9926bSpatrick           os << quote(rewriteInputPath(path)) << "\n";
1531cf9926bSpatrick       break;
1541cf9926bSpatrick     case OPT_force_load:
1551cf9926bSpatrick     case OPT_weak_library:
156*dfe94b16Srobert     case OPT_load_hidden:
1571cf9926bSpatrick       os << arg->getSpelling() << " "
1581cf9926bSpatrick          << quote(rewriteInputPath(arg->getValue())) << "\n";
1591cf9926bSpatrick       break;
1601cf9926bSpatrick     case OPT_F:
1611cf9926bSpatrick     case OPT_L:
1621cf9926bSpatrick     case OPT_bundle_loader:
1631cf9926bSpatrick     case OPT_exported_symbols_list:
1641cf9926bSpatrick     case OPT_order_file:
1651cf9926bSpatrick     case OPT_syslibroot:
1661cf9926bSpatrick     case OPT_unexported_symbols_list:
1671cf9926bSpatrick       os << arg->getSpelling() << " " << quote(rewritePath(arg->getValue()))
1681cf9926bSpatrick          << "\n";
1691cf9926bSpatrick       break;
1701cf9926bSpatrick     case OPT_sectcreate:
1711cf9926bSpatrick       os << arg->getSpelling() << " " << quote(arg->getValue(0)) << " "
1721cf9926bSpatrick          << quote(arg->getValue(1)) << " "
1731cf9926bSpatrick          << quote(rewritePath(arg->getValue(2))) << "\n";
1741cf9926bSpatrick       break;
1751cf9926bSpatrick     default:
1761cf9926bSpatrick       os << toString(*arg) << "\n";
1771cf9926bSpatrick     }
1781cf9926bSpatrick   }
1791cf9926bSpatrick   return std::string(data.str());
1801cf9926bSpatrick }
1811cf9926bSpatrick 
searchedDylib(const Twine & path,bool found)1821cf9926bSpatrick static void searchedDylib(const Twine &path, bool found) {
1831cf9926bSpatrick   if (config->printDylibSearch)
1841cf9926bSpatrick     message("searched " + path + (found ? ", found " : ", not found"));
1851cf9926bSpatrick   if (!found)
1861cf9926bSpatrick     depTracker->logFileNotFound(path);
1871cf9926bSpatrick }
1881cf9926bSpatrick 
resolveDylibPath(StringRef dylibPath)189*dfe94b16Srobert std::optional<StringRef> macho::resolveDylibPath(StringRef dylibPath) {
1901cf9926bSpatrick   // TODO: if a tbd and dylib are both present, we should check to make sure
1911cf9926bSpatrick   // they are consistent.
1921cf9926bSpatrick   SmallString<261> tbdPath = dylibPath;
1931cf9926bSpatrick   path::replace_extension(tbdPath, ".tbd");
1941cf9926bSpatrick   bool tbdExists = fs::exists(tbdPath);
1951cf9926bSpatrick   searchedDylib(tbdPath, tbdExists);
1961cf9926bSpatrick   if (tbdExists)
197*dfe94b16Srobert     return saver().save(tbdPath.str());
198*dfe94b16Srobert 
199*dfe94b16Srobert   bool dylibExists = fs::exists(dylibPath);
200*dfe94b16Srobert   searchedDylib(dylibPath, dylibExists);
201*dfe94b16Srobert   if (dylibExists)
202*dfe94b16Srobert     return saver().save(dylibPath);
2031cf9926bSpatrick   return {};
2041cf9926bSpatrick }
2051cf9926bSpatrick 
2061cf9926bSpatrick // It's not uncommon to have multiple attempts to load a single dylib,
2071cf9926bSpatrick // especially if it's a commonly re-exported core library.
2081cf9926bSpatrick static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
2091cf9926bSpatrick 
loadDylib(MemoryBufferRef mbref,DylibFile * umbrella,bool isBundleLoader,bool explicitlyLinked)2101cf9926bSpatrick DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
211*dfe94b16Srobert                             bool isBundleLoader, bool explicitlyLinked) {
2121cf9926bSpatrick   CachedHashStringRef path(mbref.getBufferIdentifier());
2131cf9926bSpatrick   DylibFile *&file = loadedDylibs[path];
214*dfe94b16Srobert   if (file) {
215*dfe94b16Srobert     if (explicitlyLinked)
216*dfe94b16Srobert       file->setExplicitlyLinked();
2171cf9926bSpatrick     return file;
218*dfe94b16Srobert   }
2191cf9926bSpatrick 
2201cf9926bSpatrick   DylibFile *newFile;
2211cf9926bSpatrick   file_magic magic = identify_magic(mbref.getBuffer());
2221cf9926bSpatrick   if (magic == file_magic::tapi_file) {
2231cf9926bSpatrick     Expected<std::unique_ptr<InterfaceFile>> result = TextAPIReader::get(mbref);
2241cf9926bSpatrick     if (!result) {
2251cf9926bSpatrick       error("could not load TAPI file at " + mbref.getBufferIdentifier() +
2261cf9926bSpatrick             ": " + toString(result.takeError()));
2271cf9926bSpatrick       return nullptr;
2281cf9926bSpatrick     }
229*dfe94b16Srobert     file =
230*dfe94b16Srobert         make<DylibFile>(**result, umbrella, isBundleLoader, explicitlyLinked);
2311cf9926bSpatrick 
2321cf9926bSpatrick     // parseReexports() can recursively call loadDylib(). That's fine since
2331cf9926bSpatrick     // we wrote the DylibFile we just loaded to the loadDylib cache via the
2341cf9926bSpatrick     // `file` reference. But the recursive load can grow loadDylibs, so the
2351cf9926bSpatrick     // `file` reference might become invalid after parseReexports() -- so copy
2361cf9926bSpatrick     // the pointer it refers to before continuing.
2371cf9926bSpatrick     newFile = file;
2381cf9926bSpatrick     if (newFile->exportingFile)
2391cf9926bSpatrick       newFile->parseReexports(**result);
2401cf9926bSpatrick   } else {
2411cf9926bSpatrick     assert(magic == file_magic::macho_dynamically_linked_shared_lib ||
2421cf9926bSpatrick            magic == file_magic::macho_dynamically_linked_shared_lib_stub ||
2431cf9926bSpatrick            magic == file_magic::macho_executable ||
2441cf9926bSpatrick            magic == file_magic::macho_bundle);
245*dfe94b16Srobert     file = make<DylibFile>(mbref, umbrella, isBundleLoader, explicitlyLinked);
2461cf9926bSpatrick 
2471cf9926bSpatrick     // parseLoadCommands() can also recursively call loadDylib(). See comment
2481cf9926bSpatrick     // in previous block for why this means we must copy `file` here.
2491cf9926bSpatrick     newFile = file;
2501cf9926bSpatrick     if (newFile->exportingFile)
2511cf9926bSpatrick       newFile->parseLoadCommands(mbref);
2521cf9926bSpatrick   }
2531cf9926bSpatrick   return newFile;
2541cf9926bSpatrick }
2551cf9926bSpatrick 
resetLoadedDylibs()256*dfe94b16Srobert void macho::resetLoadedDylibs() { loadedDylibs.clear(); }
257*dfe94b16Srobert 
258*dfe94b16Srobert std::optional<StringRef>
findPathCombination(const Twine & name,const std::vector<StringRef> & roots,ArrayRef<StringRef> extensions)2591cf9926bSpatrick macho::findPathCombination(const Twine &name,
2601cf9926bSpatrick                            const std::vector<StringRef> &roots,
2611cf9926bSpatrick                            ArrayRef<StringRef> extensions) {
2621cf9926bSpatrick   SmallString<261> base;
2631cf9926bSpatrick   for (StringRef dir : roots) {
2641cf9926bSpatrick     base = dir;
2651cf9926bSpatrick     path::append(base, name);
2661cf9926bSpatrick     for (StringRef ext : extensions) {
2671cf9926bSpatrick       Twine location = base + ext;
2681cf9926bSpatrick       bool exists = fs::exists(location);
2691cf9926bSpatrick       searchedDylib(location, exists);
2701cf9926bSpatrick       if (exists)
271*dfe94b16Srobert         return saver().save(location.str());
2721cf9926bSpatrick     }
2731cf9926bSpatrick   }
2741cf9926bSpatrick   return {};
2751cf9926bSpatrick }
2761cf9926bSpatrick 
rerootPath(StringRef path)2771cf9926bSpatrick StringRef macho::rerootPath(StringRef path) {
2781cf9926bSpatrick   if (!path::is_absolute(path, path::Style::posix) || path.endswith(".o"))
2791cf9926bSpatrick     return path;
2801cf9926bSpatrick 
281*dfe94b16Srobert   if (std::optional<StringRef> rerootedPath =
2821cf9926bSpatrick           findPathCombination(path, config->systemLibraryRoots))
2831cf9926bSpatrick     return *rerootedPath;
2841cf9926bSpatrick 
2851cf9926bSpatrick   return path;
2861cf9926bSpatrick }
2871cf9926bSpatrick 
getModTime(StringRef path)2881cf9926bSpatrick uint32_t macho::getModTime(StringRef path) {
2891cf9926bSpatrick   if (config->zeroModTime)
2901cf9926bSpatrick     return 0;
2911cf9926bSpatrick 
2921cf9926bSpatrick   fs::file_status stat;
2931cf9926bSpatrick   if (!fs::status(path, stat))
2941cf9926bSpatrick     if (fs::exists(stat))
2951cf9926bSpatrick       return toTimeT(stat.getLastModificationTime());
2961cf9926bSpatrick 
2971cf9926bSpatrick   warn("failed to get modification time of " + path);
2981cf9926bSpatrick   return 0;
2991cf9926bSpatrick }
3001cf9926bSpatrick 
printArchiveMemberLoad(StringRef reason,const InputFile * f)3011cf9926bSpatrick void macho::printArchiveMemberLoad(StringRef reason, const InputFile *f) {
3021cf9926bSpatrick   if (config->printEachFile)
3031cf9926bSpatrick     message(toString(f));
3041cf9926bSpatrick   if (config->printWhyLoad)
3051cf9926bSpatrick     message(reason + " forced load of " + toString(f));
3061cf9926bSpatrick }
3071cf9926bSpatrick 
DependencyTracker(StringRef path)3081cf9926bSpatrick macho::DependencyTracker::DependencyTracker(StringRef path)
3091cf9926bSpatrick     : path(path), active(!path.empty()) {
3101cf9926bSpatrick   if (active && fs::exists(path) && !fs::can_write(path)) {
3111cf9926bSpatrick     warn("Ignoring dependency_info option since specified path is not "
3121cf9926bSpatrick          "writeable.");
3131cf9926bSpatrick     active = false;
3141cf9926bSpatrick   }
3151cf9926bSpatrick }
3161cf9926bSpatrick 
write(StringRef version,const SetVector<InputFile * > & inputs,StringRef output)3171cf9926bSpatrick void macho::DependencyTracker::write(StringRef version,
3181cf9926bSpatrick                                      const SetVector<InputFile *> &inputs,
3191cf9926bSpatrick                                      StringRef output) {
3201cf9926bSpatrick   if (!active)
3211cf9926bSpatrick     return;
3221cf9926bSpatrick 
3231cf9926bSpatrick   std::error_code ec;
3241cf9926bSpatrick   raw_fd_ostream os(path, ec, fs::OF_None);
3251cf9926bSpatrick   if (ec) {
3261cf9926bSpatrick     warn("Error writing dependency info to file");
3271cf9926bSpatrick     return;
3281cf9926bSpatrick   }
3291cf9926bSpatrick 
3301cf9926bSpatrick   auto addDep = [&os](DepOpCode opcode, const StringRef &path) {
3311cf9926bSpatrick     // XXX: Even though DepOpCode's underlying type is uint8_t,
3321cf9926bSpatrick     // this cast is still needed because Clang older than 10.x has a bug,
3331cf9926bSpatrick     // where it doesn't know to cast the enum to its underlying type.
3341cf9926bSpatrick     // Hence `<< DepOpCode` is ambiguous to it.
3351cf9926bSpatrick     os << static_cast<uint8_t>(opcode);
3361cf9926bSpatrick     os << path;
3371cf9926bSpatrick     os << '\0';
3381cf9926bSpatrick   };
3391cf9926bSpatrick 
3401cf9926bSpatrick   addDep(DepOpCode::Version, version);
3411cf9926bSpatrick 
3421cf9926bSpatrick   // Sort the input by its names.
3431cf9926bSpatrick   std::vector<StringRef> inputNames;
3441cf9926bSpatrick   inputNames.reserve(inputs.size());
3451cf9926bSpatrick   for (InputFile *f : inputs)
3461cf9926bSpatrick     inputNames.push_back(f->getName());
3471cf9926bSpatrick   llvm::sort(inputNames);
3481cf9926bSpatrick 
3491cf9926bSpatrick   for (const StringRef &in : inputNames)
3501cf9926bSpatrick     addDep(DepOpCode::Input, in);
3511cf9926bSpatrick 
3521cf9926bSpatrick   for (const std::string &f : notFounds)
3531cf9926bSpatrick     addDep(DepOpCode::NotFound, f);
3541cf9926bSpatrick 
3551cf9926bSpatrick   addDep(DepOpCode::Output, output);
3561cf9926bSpatrick }
357