10b57cec5SDimitry Andric //===- DriverUtils.cpp ----------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file contains utility functions for the driver. Because there 100b57cec5SDimitry Andric // are so many small functions, we created this separate file to make 110b57cec5SDimitry Andric // Driver.cpp less cluttered. 120b57cec5SDimitry Andric // 130b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric #include "Driver.h" 160b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h" 170b57cec5SDimitry Andric #include "lld/Common/Memory.h" 180b57cec5SDimitry Andric #include "lld/Common/Reproduce.h" 190b57cec5SDimitry Andric #include "lld/Common/Version.h" 200b57cec5SDimitry Andric #include "llvm/ADT/Optional.h" 210b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 220b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 230b57cec5SDimitry Andric #include "llvm/Option/Option.h" 240b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 250b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h" 26*5ffd83dbSDimitry Andric #include "llvm/Support/Host.h" 270b57cec5SDimitry Andric #include "llvm/Support/Path.h" 280b57cec5SDimitry Andric #include "llvm/Support/Process.h" 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric using namespace llvm; 310b57cec5SDimitry Andric using namespace llvm::sys; 320b57cec5SDimitry Andric using namespace llvm::opt; 33*5ffd83dbSDimitry Andric using namespace lld; 34*5ffd83dbSDimitry Andric using namespace lld::elf; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric // Create OptTable 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric // Create prefix string literals used in Options.td 390b57cec5SDimitry Andric #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 400b57cec5SDimitry Andric #include "Options.inc" 410b57cec5SDimitry Andric #undef PREFIX 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric // Create table mapping all options defined in Options.td 440b57cec5SDimitry Andric static const opt::OptTable::Info optInfo[] = { 450b57cec5SDimitry Andric #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \ 460b57cec5SDimitry Andric {X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \ 470b57cec5SDimitry Andric X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12}, 480b57cec5SDimitry Andric #include "Options.inc" 490b57cec5SDimitry Andric #undef OPTION 500b57cec5SDimitry Andric }; 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric ELFOptTable::ELFOptTable() : OptTable(optInfo) {} 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric // Set color diagnostics according to -color-diagnostics={auto,always,never} 550b57cec5SDimitry Andric // or -no-color-diagnostics flags. 560b57cec5SDimitry Andric static void handleColorDiagnostics(opt::InputArgList &args) { 570b57cec5SDimitry Andric auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq, 580b57cec5SDimitry Andric OPT_no_color_diagnostics); 590b57cec5SDimitry Andric if (!arg) 600b57cec5SDimitry Andric return; 610b57cec5SDimitry Andric if (arg->getOption().getID() == OPT_color_diagnostics) { 62480093f4SDimitry Andric lld::errs().enable_colors(true); 630b57cec5SDimitry Andric } else if (arg->getOption().getID() == OPT_no_color_diagnostics) { 64480093f4SDimitry Andric lld::errs().enable_colors(false); 650b57cec5SDimitry Andric } else { 660b57cec5SDimitry Andric StringRef s = arg->getValue(); 670b57cec5SDimitry Andric if (s == "always") 68480093f4SDimitry Andric lld::errs().enable_colors(true); 690b57cec5SDimitry Andric else if (s == "never") 70480093f4SDimitry Andric lld::errs().enable_colors(false); 710b57cec5SDimitry Andric else if (s != "auto") 720b57cec5SDimitry Andric error("unknown option: --color-diagnostics=" + s); 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) { 770b57cec5SDimitry Andric if (auto *arg = args.getLastArg(OPT_rsp_quoting)) { 780b57cec5SDimitry Andric StringRef s = arg->getValue(); 790b57cec5SDimitry Andric if (s != "windows" && s != "posix") 800b57cec5SDimitry Andric error("invalid response file quoting: " + s); 810b57cec5SDimitry Andric if (s == "windows") 820b57cec5SDimitry Andric return cl::TokenizeWindowsCommandLine; 830b57cec5SDimitry Andric return cl::TokenizeGNUCommandLine; 840b57cec5SDimitry Andric } 85*5ffd83dbSDimitry Andric if (Triple(sys::getProcessTriple()).isOSWindows()) 860b57cec5SDimitry Andric return cl::TokenizeWindowsCommandLine; 870b57cec5SDimitry Andric return cl::TokenizeGNUCommandLine; 880b57cec5SDimitry Andric } 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric // Gold LTO plugin takes a `--plugin-opt foo=bar` option as an alias for 910b57cec5SDimitry Andric // `--plugin-opt=foo=bar`. We want to handle `--plugin-opt=foo=` as an 920b57cec5SDimitry Andric // option name and `bar` as a value. Unfortunately, OptParser cannot 930b57cec5SDimitry Andric // handle an option with a space in it. 940b57cec5SDimitry Andric // 950b57cec5SDimitry Andric // In this function, we concatenate command line arguments so that 960b57cec5SDimitry Andric // `--plugin-opt <foo>` is converted to `--plugin-opt=<foo>`. This is a 970b57cec5SDimitry Andric // bit hacky, but looks like it is still better than handling --plugin-opt 980b57cec5SDimitry Andric // options by hand. 990b57cec5SDimitry Andric static void concatLTOPluginOptions(SmallVectorImpl<const char *> &args) { 1000b57cec5SDimitry Andric SmallVector<const char *, 256> v; 1010b57cec5SDimitry Andric for (size_t i = 0, e = args.size(); i != e; ++i) { 1020b57cec5SDimitry Andric StringRef s = args[i]; 1030b57cec5SDimitry Andric if ((s == "-plugin-opt" || s == "--plugin-opt") && i + 1 != e) { 1040b57cec5SDimitry Andric v.push_back(saver.save(s + "=" + args[i + 1]).data()); 1050b57cec5SDimitry Andric ++i; 1060b57cec5SDimitry Andric } else { 1070b57cec5SDimitry Andric v.push_back(args[i]); 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric args = std::move(v); 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric // Parses a given list of options. 1140b57cec5SDimitry Andric opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> argv) { 1150b57cec5SDimitry Andric // Make InputArgList from string vectors. 1160b57cec5SDimitry Andric unsigned missingIndex; 1170b57cec5SDimitry Andric unsigned missingCount; 1180b57cec5SDimitry Andric SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size()); 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric // We need to get the quoting style for response files before parsing all 1210b57cec5SDimitry Andric // options so we parse here before and ignore all the options but 1220b57cec5SDimitry Andric // --rsp-quoting. 1230b57cec5SDimitry Andric opt::InputArgList args = this->ParseArgs(vec, missingIndex, missingCount); 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric // Expand response files (arguments in the form of @<filename>) 1260b57cec5SDimitry Andric // and then parse the argument again. 1270b57cec5SDimitry Andric cl::ExpandResponseFiles(saver, getQuotingStyle(args), vec); 1280b57cec5SDimitry Andric concatLTOPluginOptions(vec); 1290b57cec5SDimitry Andric args = this->ParseArgs(vec, missingIndex, missingCount); 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric handleColorDiagnostics(args); 1320b57cec5SDimitry Andric if (missingCount) 1330b57cec5SDimitry Andric error(Twine(args.getArgString(missingIndex)) + ": missing argument"); 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric for (auto *arg : args.filtered(OPT_UNKNOWN)) { 1360b57cec5SDimitry Andric std::string nearest; 1370b57cec5SDimitry Andric if (findNearest(arg->getAsString(args), nearest) > 1) 1380b57cec5SDimitry Andric error("unknown argument '" + arg->getAsString(args) + "'"); 1390b57cec5SDimitry Andric else 1400b57cec5SDimitry Andric error("unknown argument '" + arg->getAsString(args) + 1410b57cec5SDimitry Andric "', did you mean '" + nearest + "'"); 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric return args; 1440b57cec5SDimitry Andric } 1450b57cec5SDimitry Andric 146*5ffd83dbSDimitry Andric void elf::printHelp() { 1470b57cec5SDimitry Andric ELFOptTable().PrintHelp( 148480093f4SDimitry Andric lld::outs(), (config->progName + " [options] file...").str().c_str(), 149480093f4SDimitry Andric "lld", false /*ShowHidden*/, true /*ShowAllAliases*/); 150480093f4SDimitry Andric lld::outs() << "\n"; 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric // Scripts generated by Libtool versions up to at least 2.4.6 (the most 1530b57cec5SDimitry Andric // recent version as of March 2017) expect /: supported targets:.* elf/ 1540b57cec5SDimitry Andric // in a message for the -help option. If it doesn't match, the scripts 1550b57cec5SDimitry Andric // assume that the linker doesn't support very basic features such as 1560b57cec5SDimitry Andric // shared libraries. Therefore, we need to print out at least "elf". 157480093f4SDimitry Andric lld::outs() << config->progName << ": supported targets: elf\n"; 1580b57cec5SDimitry Andric } 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric static std::string rewritePath(StringRef s) { 1610b57cec5SDimitry Andric if (fs::exists(s)) 1620b57cec5SDimitry Andric return relativeToRoot(s); 163*5ffd83dbSDimitry Andric return std::string(s); 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric // Reconstructs command line arguments so that so that you can re-run 1670b57cec5SDimitry Andric // the same command with the same inputs. This is for --reproduce. 168*5ffd83dbSDimitry Andric std::string elf::createResponseFile(const opt::InputArgList &args) { 1690b57cec5SDimitry Andric SmallString<0> data; 1700b57cec5SDimitry Andric raw_svector_ostream os(data); 1710b57cec5SDimitry Andric os << "--chroot .\n"; 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric // Copy the command line to the output while rewriting paths. 1740b57cec5SDimitry Andric for (auto *arg : args) { 1750b57cec5SDimitry Andric switch (arg->getOption().getID()) { 1760b57cec5SDimitry Andric case OPT_reproduce: 1770b57cec5SDimitry Andric break; 1780b57cec5SDimitry Andric case OPT_INPUT: 1790b57cec5SDimitry Andric os << quote(rewritePath(arg->getValue())) << "\n"; 1800b57cec5SDimitry Andric break; 1810b57cec5SDimitry Andric case OPT_o: 1820b57cec5SDimitry Andric // If -o path contains directories, "lld @response.txt" will likely 1830b57cec5SDimitry Andric // fail because the archive we are creating doesn't contain empty 1840b57cec5SDimitry Andric // directories for the output path (-o doesn't create directories). 1850b57cec5SDimitry Andric // Strip directories to prevent the issue. 1860b57cec5SDimitry Andric os << "-o " << quote(sys::path::filename(arg->getValue())) << "\n"; 1870b57cec5SDimitry Andric break; 1880b57cec5SDimitry Andric case OPT_dynamic_list: 1890b57cec5SDimitry Andric case OPT_library_path: 1900b57cec5SDimitry Andric case OPT_rpath: 1910b57cec5SDimitry Andric case OPT_script: 1920b57cec5SDimitry Andric case OPT_symbol_ordering_file: 1930b57cec5SDimitry Andric case OPT_sysroot: 1940b57cec5SDimitry Andric case OPT_version_script: 1950b57cec5SDimitry Andric os << arg->getSpelling() << " " << quote(rewritePath(arg->getValue())) 1960b57cec5SDimitry Andric << "\n"; 1970b57cec5SDimitry Andric break; 1980b57cec5SDimitry Andric default: 1990b57cec5SDimitry Andric os << toString(*arg) << "\n"; 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric } 202*5ffd83dbSDimitry Andric return std::string(data.str()); 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric // Find a file by concatenating given paths. If a resulting path 2060b57cec5SDimitry Andric // starts with "=", the character is replaced with a --sysroot value. 2070b57cec5SDimitry Andric static Optional<std::string> findFile(StringRef path1, const Twine &path2) { 2080b57cec5SDimitry Andric SmallString<128> s; 2090b57cec5SDimitry Andric if (path1.startswith("=")) 2100b57cec5SDimitry Andric path::append(s, config->sysroot, path1.substr(1), path2); 2110b57cec5SDimitry Andric else 2120b57cec5SDimitry Andric path::append(s, path1, path2); 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric if (fs::exists(s)) 215*5ffd83dbSDimitry Andric return std::string(s); 2160b57cec5SDimitry Andric return None; 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric 219*5ffd83dbSDimitry Andric Optional<std::string> elf::findFromSearchPaths(StringRef path) { 2200b57cec5SDimitry Andric for (StringRef dir : config->searchPaths) 2210b57cec5SDimitry Andric if (Optional<std::string> s = findFile(dir, path)) 2220b57cec5SDimitry Andric return s; 2230b57cec5SDimitry Andric return None; 2240b57cec5SDimitry Andric } 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric // This is for -l<basename>. We'll look for lib<basename>.so or lib<basename>.a from 2270b57cec5SDimitry Andric // search paths. 228*5ffd83dbSDimitry Andric Optional<std::string> elf::searchLibraryBaseName(StringRef name) { 2290b57cec5SDimitry Andric for (StringRef dir : config->searchPaths) { 2300b57cec5SDimitry Andric if (!config->isStatic) 2310b57cec5SDimitry Andric if (Optional<std::string> s = findFile(dir, "lib" + name + ".so")) 2320b57cec5SDimitry Andric return s; 2330b57cec5SDimitry Andric if (Optional<std::string> s = findFile(dir, "lib" + name + ".a")) 2340b57cec5SDimitry Andric return s; 2350b57cec5SDimitry Andric } 2360b57cec5SDimitry Andric return None; 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric // This is for -l<namespec>. 240*5ffd83dbSDimitry Andric Optional<std::string> elf::searchLibrary(StringRef name) { 2410b57cec5SDimitry Andric if (name.startswith(":")) 2420b57cec5SDimitry Andric return findFromSearchPaths(name.substr(1)); 2430b57cec5SDimitry Andric return searchLibraryBaseName(name); 2440b57cec5SDimitry Andric } 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric // If a linker/version script doesn't exist in the current directory, we also 2470b57cec5SDimitry Andric // look for the script in the '-L' search paths. This matches the behaviour of 2480b57cec5SDimitry Andric // '-T', --version-script=, and linker script INPUT() command in ld.bfd. 249*5ffd83dbSDimitry Andric Optional<std::string> elf::searchScript(StringRef name) { 2500b57cec5SDimitry Andric if (fs::exists(name)) 2510b57cec5SDimitry Andric return name.str(); 2520b57cec5SDimitry Andric return findFromSearchPaths(name); 2530b57cec5SDimitry Andric } 254