brintos

brintos / llvm-project-archived public Read only

0
0
Text · 14.7 KiB · 3ff9d96 Raw
428 lines · cpp
1//===- DriverUtils.cpp ----------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "Config.h"10#include "Driver.h"11#include "InputFiles.h"12 13#include "lld/Common/Args.h"14#include "lld/Common/CommonLinkerContext.h"15#include "lld/Common/Reproduce.h"16#include "llvm/ADT/CachedHashString.h"17#include "llvm/ADT/DenseMap.h"18#include "llvm/LTO/LTO.h"19#include "llvm/Option/Arg.h"20#include "llvm/Option/ArgList.h"21#include "llvm/Option/Option.h"22#include "llvm/Support/CommandLine.h"23#include "llvm/Support/FileSystem.h"24#include "llvm/Support/Path.h"25#include "llvm/TextAPI/InterfaceFile.h"26#include "llvm/TextAPI/TextAPIReader.h"27 28using namespace llvm;29using namespace llvm::MachO;30using namespace llvm::opt;31using namespace llvm::sys;32using namespace lld;33using namespace lld::macho;34 35#define OPTTABLE_STR_TABLE_CODE36#include "Options.inc"37#undef OPTTABLE_STR_TABLE_CODE38 39// Create prefix string literals used in Options.td40#define OPTTABLE_PREFIXES_TABLE_CODE41#include "Options.inc"42#undef OPTTABLE_PREFIXES_TABLE_CODE43 44// Create table mapping all options defined in Options.td45static constexpr OptTable::Info optInfo[] = {46#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS,         \47               VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, METAVAR,     \48               VALUES, SUBCOMMANDIDS_OFFSET)                                   \49  {PREFIX,                                                                     \50   NAME,                                                                       \51   HELPTEXT,                                                                   \52   HELPTEXTSFORVARIANTS,                                                       \53   METAVAR,                                                                    \54   OPT_##ID,                                                                   \55   opt::Option::KIND##Class,                                                   \56   PARAM,                                                                      \57   FLAGS,                                                                      \58   VISIBILITY,                                                                 \59   OPT_##GROUP,                                                                \60   OPT_##ALIAS,                                                                \61   ALIASARGS,                                                                  \62   VALUES,                                                                     \63   SUBCOMMANDIDS_OFFSET},64#include "Options.inc"65#undef OPTION66};67 68MachOOptTable::MachOOptTable()69    : GenericOptTable(OptionStrTable, OptionPrefixesTable, optInfo) {}70 71// Set color diagnostics according to --color-diagnostics={auto,always,never}72// or --no-color-diagnostics flags.73static void handleColorDiagnostics(CommonLinkerContext &ctx,74                                   InputArgList &args) {75  const Arg *arg =76      args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,77                      OPT_no_color_diagnostics);78  if (!arg)79    return;80  auto &errs = ctx.e.errs();81  if (arg->getOption().getID() == OPT_color_diagnostics) {82    errs.enable_colors(true);83  } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {84    errs.enable_colors(false);85  } else {86    StringRef s = arg->getValue();87    if (s == "always")88      errs.enable_colors(true);89    else if (s == "never")90      errs.enable_colors(false);91    else if (s != "auto")92      error("unknown option: --color-diagnostics=" + s);93  }94}95 96InputArgList MachOOptTable::parse(CommonLinkerContext &ctx,97                                  ArrayRef<const char *> argv) {98  // Make InputArgList from string vectors.99  unsigned missingIndex;100  unsigned missingCount;101  SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());102 103  // Expand response files (arguments in the form of @<filename>)104  // and then parse the argument again.105  cl::ExpandResponseFiles(saver(), cl::TokenizeGNUCommandLine, vec);106  InputArgList args = ParseArgs(vec, missingIndex, missingCount);107 108  // Handle -fatal_warnings early since it converts missing argument warnings109  // to errors.110  errorHandler().fatalWarnings = args.hasArg(OPT_fatal_warnings);111  errorHandler().suppressWarnings = args.hasArg(OPT_w);112 113  if (missingCount)114    error(Twine(args.getArgString(missingIndex)) + ": missing argument");115 116  handleColorDiagnostics(ctx, args);117 118  for (const Arg *arg : args.filtered(OPT_UNKNOWN)) {119    std::string nearest;120    if (findNearest(arg->getAsString(args), nearest) > 1)121      error("unknown argument '" + arg->getAsString(args) + "'");122    else123      error("unknown argument '" + arg->getAsString(args) +124            "', did you mean '" + nearest + "'");125  }126  return args;127}128 129void MachOOptTable::printHelp(CommonLinkerContext &ctx, const char *argv0,130                              bool showHidden) const {131  auto &outs = ctx.e.outs();132  OptTable::printHelp(outs, (std::string(argv0) + " [options] file...").c_str(),133                      "LLVM Linker", showHidden);134  outs << '\n';135}136 137static std::string rewritePath(StringRef s) {138  if (fs::exists(s))139    return relativeToRoot(s);140  return std::string(s);141}142 143static std::string rewriteInputPath(StringRef s) {144  // Don't bother rewriting "absolute" paths that are actually under the145  // syslibroot; simply rewriting the syslibroot is sufficient.146  if (rerootPath(s) == s && fs::exists(s))147    return relativeToRoot(s);148  return std::string(s);149}150 151// Reconstructs command line arguments so that so that you can re-run152// the same command with the same inputs. This is for --reproduce.153std::string macho::createResponseFile(const InputArgList &args) {154  SmallString<0> data;155  raw_svector_ostream os(data);156 157  // Copy the command line to the output while rewriting paths.158  for (const Arg *arg : args) {159    switch (arg->getOption().getID()) {160    case OPT_reproduce:161      break;162    case OPT_INPUT:163      os << quote(rewriteInputPath(arg->getValue())) << "\n";164      break;165    case OPT_o:166      os << "-o " << quote(path::filename(arg->getValue())) << "\n";167      break;168    case OPT_filelist:169      if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))170        for (StringRef path : args::getLines(*buffer))171          os << quote(rewriteInputPath(path)) << "\n";172      break;173    case OPT_force_load:174    case OPT_weak_library:175    case OPT_load_hidden:176      os << arg->getSpelling() << " "177         << quote(rewriteInputPath(arg->getValue())) << "\n";178      break;179    case OPT_F:180    case OPT_L:181    case OPT_bundle_loader:182    case OPT_exported_symbols_list:183    case OPT_order_file:184    case OPT_syslibroot:185    case OPT_unexported_symbols_list:186      os << arg->getSpelling() << " " << quote(rewritePath(arg->getValue()))187         << "\n";188      break;189    case OPT_sectcreate:190      os << arg->getSpelling() << " " << quote(arg->getValue(0)) << " "191         << quote(arg->getValue(1)) << " "192         << quote(rewritePath(arg->getValue(2))) << "\n";193      break;194    default:195      os << toString(*arg) << "\n";196    }197  }198  return std::string(data);199}200 201static void searchedDylib(const Twine &path, bool found) {202  if (config->printDylibSearch)203    message("searched " + path + (found ? ", found " : ", not found"));204  if (!found)205    depTracker->logFileNotFound(path);206}207 208std::optional<StringRef> macho::resolveDylibPath(StringRef dylibPath) {209  // TODO: if a tbd and dylib are both present, we should check to make sure210  // they are consistent.211  SmallString<261> tbdPath = dylibPath;212  path::replace_extension(tbdPath, ".tbd");213  bool tbdExists = fs::exists(tbdPath);214  searchedDylib(tbdPath, tbdExists);215  if (tbdExists)216    return saver().save(tbdPath.str());217 218  bool dylibExists = fs::exists(dylibPath);219  searchedDylib(dylibPath, dylibExists);220  if (dylibExists)221    return saver().save(dylibPath);222  return {};223}224 225// It's not uncommon to have multiple attempts to load a single dylib,226// especially if it's a commonly re-exported core library.227static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;228 229static StringRef realPathIfDifferent(StringRef path) {230  SmallString<128> realPathBuf;231  if (fs::real_path(path, realPathBuf))232    return StringRef();233 234  SmallString<128> absPathBuf = path;235  if (!fs::make_absolute(absPathBuf) && realPathBuf == absPathBuf)236    return StringRef();237 238  return uniqueSaver().save(StringRef(realPathBuf));239}240 241DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,242                            bool isBundleLoader, bool explicitlyLinked) {243  CachedHashStringRef path(mbref.getBufferIdentifier());244  DylibFile *&file = loadedDylibs[path];245  if (file) {246    if (explicitlyLinked)247      file->setExplicitlyLinked();248    return file;249  }250 251  // Frameworks can be found from different symlink paths, so resolve252  // symlinks and look up in the dylib cache.253  CachedHashStringRef realPath(254      realPathIfDifferent(mbref.getBufferIdentifier()));255  if (!realPath.val().empty()) {256    // Avoid map insertions here so that we do not invalidate the "file"257    // reference.258    auto it = loadedDylibs.find(realPath);259    if (it != loadedDylibs.end()) {260      DylibFile *realfile = it->second;261      if (explicitlyLinked)262        realfile->setExplicitlyLinked();263      return realfile;264    }265  }266 267  DylibFile *newFile;268  file_magic magic = identify_magic(mbref.getBuffer());269  if (magic == file_magic::tapi_file) {270    Expected<std::unique_ptr<InterfaceFile>> result = TextAPIReader::get(mbref);271    if (!result) {272      error("could not load TAPI file at " + mbref.getBufferIdentifier() +273            ": " + toString(result.takeError()));274      return nullptr;275    }276    file =277        make<DylibFile>(**result, umbrella, isBundleLoader, explicitlyLinked);278 279    // parseReexports() can recursively call loadDylib(). That's fine since280    // we wrote the DylibFile we just loaded to the loadDylib cache via the281    // `file` reference. But the recursive load can grow loadDylibs, so the282    // `file` reference might become invalid after parseReexports() -- so copy283    // the pointer it refers to before continuing.284    newFile = file;285    if (newFile->exportingFile)286      newFile->parseReexports(**result);287  } else {288    assert(magic == file_magic::macho_dynamically_linked_shared_lib ||289           magic == file_magic::macho_dynamically_linked_shared_lib_stub ||290           magic == file_magic::macho_executable ||291           magic == file_magic::macho_bundle);292    file = make<DylibFile>(mbref, umbrella, isBundleLoader, explicitlyLinked);293 294    // parseLoadCommands() can also recursively call loadDylib(). See comment295    // in previous block for why this means we must copy `file` here.296    newFile = file;297    if (newFile->exportingFile)298      newFile->parseLoadCommands(mbref);299  }300 301  if (explicitlyLinked && !newFile->allowableClients.empty()) {302    bool allowed =303        llvm::any_of(newFile->allowableClients, [&](StringRef allowableClient) {304          // We only do a prefix match to match LD64's behaviour.305          return allowableClient.starts_with(config->clientName);306        });307 308    // TODO: This behaviour doesn't quite match the latest available source309    // release of LD64 (ld64-951.9), which allows "parents" and "siblings"310    // to link to libraries even when they're not explicitly named as311    // allowable clients. However, behaviour around this seems to have312    // changed in the latest release of Xcode (ld64-1115.7.3), so it's not313    // clear what the correct thing to do is yet.314    if (!allowed)315      error("cannot link directly with '" +316            sys::path::filename(newFile->installName) + "' because " +317            config->clientName + " is not an allowed client");318  }319 320  // If the load path was a symlink, cache the real path too.321  if (!realPath.val().empty())322    loadedDylibs[realPath] = newFile;323 324  return newFile;325}326 327void macho::resetLoadedDylibs() { loadedDylibs.clear(); }328 329std::optional<StringRef>330macho::findPathCombination(const Twine &name,331                           const std::vector<StringRef> &roots,332                           ArrayRef<StringRef> extensions) {333  SmallString<261> base;334  for (StringRef dir : roots) {335    base = dir;336    path::append(base, name);337    for (StringRef ext : extensions) {338      Twine location = base + ext;339      bool exists = fs::exists(location);340      searchedDylib(location, exists);341      if (exists)342        return saver().save(location.str());343    }344  }345  return {};346}347 348StringRef macho::rerootPath(StringRef path) {349  if (!path::is_absolute(path, path::Style::posix) || path.ends_with(".o"))350    return path;351 352  if (std::optional<StringRef> rerootedPath =353          findPathCombination(path, config->systemLibraryRoots))354    return *rerootedPath;355 356  return path;357}358 359uint32_t macho::getModTime(StringRef path) {360  if (config->zeroModTime)361    return 0;362 363  fs::file_status stat;364  if (!fs::status(path, stat))365    if (fs::exists(stat))366      return toTimeT(stat.getLastModificationTime());367 368  warn("failed to get modification time of " + path);369  return 0;370}371 372void macho::printArchiveMemberLoad(StringRef reason, const InputFile *f) {373  if (config->printEachFile)374    message(toString(f));375  if (config->printWhyLoad)376    message(reason + " forced load of " + toString(f));377}378 379macho::DependencyTracker::DependencyTracker(StringRef path)380    : path(path), active(!path.empty()) {381  if (active && fs::exists(path) && !fs::can_write(path)) {382    warn("Ignoring dependency_info option since specified path is not "383         "writeable.");384    active = false;385  }386}387 388void macho::DependencyTracker::write(StringRef version,389                                     const SetVector<InputFile *> &inputs,390                                     StringRef output) {391  if (!active)392    return;393 394  std::error_code ec;395  raw_fd_ostream os(path, ec, fs::OF_None);396  if (ec) {397    warn("Error writing dependency info to file");398    return;399  }400 401  auto addDep = [&os](DepOpCode opcode, const StringRef &path) {402    // XXX: Even though DepOpCode's underlying type is uint8_t,403    // this cast is still needed because Clang older than 10.x has a bug,404    // where it doesn't know to cast the enum to its underlying type.405    // Hence `<< DepOpCode` is ambiguous to it.406    os << static_cast<uint8_t>(opcode);407    os << path;408    os << '\0';409  };410 411  addDep(DepOpCode::Version, version);412 413  // Sort the input by its names.414  std::vector<StringRef> inputNames;415  inputNames.reserve(inputs.size());416  for (InputFile *f : inputs)417    inputNames.push_back(f->getName());418  llvm::sort(inputNames);419 420  for (const StringRef &in : inputNames)421    addDep(DepOpCode::Input, in);422 423  for (const std::string &f : notFounds)424    addDep(DepOpCode::NotFound, f);425 426  addDep(DepOpCode::Output, output);427}428