mgorny updated this revision to Diff 181268. mgorny added a comment. Implemented checking the triple against target registry. Also made `--version` output the detected target.
CHANGES SINCE LAST ACTION https://reviews.llvm.org/D56215/new/ https://reviews.llvm.org/D56215 Files: ELF/Config.h ELF/Driver.cpp ELF/Driver.h
Index: ELF/Driver.h =================================================================== --- ELF/Driver.h +++ ELF/Driver.h @@ -31,7 +31,9 @@ void addLibrary(StringRef Name); private: + void setDefaultTargetTriple(StringRef argv0); void readConfigs(llvm::opt::InputArgList &Args); + void appendDefaultSearchPaths(); void createFiles(llvm::opt::InputArgList &Args); void inferMachineType(); template <class ELFT> void link(llvm::opt::InputArgList &Args); Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -54,6 +54,7 @@ #include "llvm/Support/LEB128.h" #include "llvm/Support/Path.h" #include "llvm/Support/TarWriter.h" +#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_ostream.h" #include <cstdlib> @@ -365,6 +366,23 @@ error("unknown -z value: " + StringRef(Arg->getValue())); } +void LinkerDriver::appendDefaultSearchPaths() { + if (Config->TargetTriple.isOSNetBSD()) { + // NetBSD driver relies on the linker knowing the default search paths. + // Please keep this in sync with clang/lib/Driver/ToolChains/NetBSD.cpp + // (NetBSD::NetBSD constructor) + switch (Config->EMachine) { + case EM_386: + Config->SearchPaths.push_back("=/usr/lib/i386"); + break; + case EM_X86_64: + break; + // TODO: support non-x86 architectures + } + Config->SearchPaths.push_back("=/usr/lib"); + } +} + void LinkerDriver::main(ArrayRef<const char *> ArgsArr) { ELFOptTable Parser; opt::InputArgList Args = Parser.parse(ArgsArr.slice(1)); @@ -378,6 +396,26 @@ return; } + if (const char *Path = getReproduceOption(Args)) { + // Note that --reproduce is a debug option so you can ignore it + // if you are trying to understand the whole picture of the code. + Expected<std::unique_ptr<TarWriter>> ErrOrWriter = + TarWriter::create(Path, path::stem(Path)); + if (ErrOrWriter) { + Tar = std::move(*ErrOrWriter); + Tar->append("response.txt", createResponseFile(Args)); + Tar->append("version.txt", getLLDVersion() + "\n"); + } else { + error("--reproduce: " + toString(ErrOrWriter.takeError())); + } + } + + initLLVM(); + setDefaultTargetTriple(ArgsArr[0]); + readConfigs(Args); + checkZOptions(Args); + appendDefaultSearchPaths(); + // Handle -v or -version. // // A note about "compatible with GNU linkers" message: this is a hack for @@ -393,26 +431,11 @@ // lot of "configure" scripts out there that are generated by old version // of Libtool. We cannot convince every software developer to migrate to // the latest version and re-generate scripts. So we have this hack. - if (Args.hasArg(OPT_v) || Args.hasArg(OPT_version)) + if (Args.hasArg(OPT_v) || Args.hasArg(OPT_version)) { message(getLLDVersion() + " (compatible with GNU linkers)"); - - if (const char *Path = getReproduceOption(Args)) { - // Note that --reproduce is a debug option so you can ignore it - // if you are trying to understand the whole picture of the code. - Expected<std::unique_ptr<TarWriter>> ErrOrWriter = - TarWriter::create(Path, path::stem(Path)); - if (ErrOrWriter) { - Tar = std::move(*ErrOrWriter); - Tar->append("response.txt", createResponseFile(Args)); - Tar->append("version.txt", getLLDVersion() + "\n"); - } else { - error("--reproduce: " + toString(ErrOrWriter.takeError())); - } + message("Target: " + Config->TargetTriple.str()); } - readConfigs(Args); - checkZOptions(Args); - // The behavior of -v or --version is a bit strange, but this is // needed for compatibility with GNU linkers. if (Args.hasArg(OPT_v) && !Args.hasArg(OPT_INPUT)) @@ -420,7 +443,6 @@ if (Args.hasArg(OPT_version)) return; - initLLVM(); createFiles(Args); if (errorCount()) return; @@ -746,6 +768,21 @@ error(Msg + ": " + StringRef(Err).trim()); } +void LinkerDriver::setDefaultTargetTriple(StringRef argv0) { + // Start with a default initial triple + Config->TargetTriple = llvm::Triple(getDefaultTargetTriple()); + + // Try to override triple with program name prefix + std::string ProgName = llvm::sys::path::stem(argv0); + size_t LastComponent = ProgName.rfind('-'); + if (LastComponent != std::string::npos) { + std::string Prefix = ProgName.substr(0, LastComponent); + std::string IgnoredError; + if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) + Config->TargetTriple = llvm::Triple(Prefix); + } +} + // Initializes Config members by the command line options. void LinkerDriver::readConfigs(opt::InputArgList &Args) { errorHandler().Verbose = Args.hasArg(OPT_verbose); @@ -1172,7 +1209,7 @@ // each target. static uint64_t getMaxPageSize(opt::InputArgList &Args) { uint64_t Val = args::getZOptionValue(Args, OPT_z, "max-page-size", - Target->DefaultMaxPageSize); + lld::elf::Target->DefaultMaxPageSize); if (!isPowerOf2_64(Val)) error("max-page-size: value isn't a power of 2"); return Val; Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -14,6 +14,7 @@ #include "llvm/ADT/MapVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" +#include "llvm/ADT/Triple.h" #include "llvm/BinaryFormat/ELF.h" #include "llvm/Support/CachePruning.h" #include "llvm/Support/CodeGen.h" @@ -276,6 +277,10 @@ // 4 for ELF32, 8 for ELF64. int Wordsize; + + // Target triple, inferred from program name or defaulted to LLVM + // default target. + llvm::Triple TargetTriple; }; // The only instance of Configuration struct.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits