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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains utility functions for the driver. Because there 10 // are so many small functions, we created this separate file to make 11 // Driver.cpp less cluttered. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "Driver.h" 16 #include "lld/Common/ErrorHandler.h" 17 #include "lld/Common/Memory.h" 18 #include "lld/Common/Reproduce.h" 19 #include "lld/Common/Version.h" 20 #include "llvm/ADT/Optional.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/ADT/Triple.h" 23 #include "llvm/Option/Option.h" 24 #include "llvm/Support/CommandLine.h" 25 #include "llvm/Support/FileSystem.h" 26 #include "llvm/Support/Host.h" 27 #include "llvm/Support/Path.h" 28 #include "llvm/Support/Process.h" 29 #include "llvm/Support/TimeProfiler.h" 30 31 using namespace llvm; 32 using namespace llvm::sys; 33 using namespace llvm::opt; 34 using namespace lld; 35 using namespace lld::elf; 36 37 // Create OptTable 38 39 // Create prefix string literals used in Options.td 40 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 41 #include "Options.inc" 42 #undef PREFIX 43 44 // Create table mapping all options defined in Options.td 45 static const opt::OptTable::Info optInfo[] = { 46 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \ 47 {X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \ 48 X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12}, 49 #include "Options.inc" 50 #undef OPTION 51 }; 52 53 ELFOptTable::ELFOptTable() : OptTable(optInfo) {} 54 55 // Set color diagnostics according to -color-diagnostics={auto,always,never} 56 // or -no-color-diagnostics flags. 57 static void handleColorDiagnostics(opt::InputArgList &args) { 58 auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq, 59 OPT_no_color_diagnostics); 60 if (!arg) 61 return; 62 if (arg->getOption().getID() == OPT_color_diagnostics) { 63 lld::errs().enable_colors(true); 64 } else if (arg->getOption().getID() == OPT_no_color_diagnostics) { 65 lld::errs().enable_colors(false); 66 } else { 67 StringRef s = arg->getValue(); 68 if (s == "always") 69 lld::errs().enable_colors(true); 70 else if (s == "never") 71 lld::errs().enable_colors(false); 72 else if (s != "auto") 73 error("unknown option: --color-diagnostics=" + s); 74 } 75 } 76 77 static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) { 78 if (auto *arg = args.getLastArg(OPT_rsp_quoting)) { 79 StringRef s = arg->getValue(); 80 if (s != "windows" && s != "posix") 81 error("invalid response file quoting: " + s); 82 if (s == "windows") 83 return cl::TokenizeWindowsCommandLine; 84 return cl::TokenizeGNUCommandLine; 85 } 86 if (Triple(sys::getProcessTriple()).isOSWindows()) 87 return cl::TokenizeWindowsCommandLine; 88 return cl::TokenizeGNUCommandLine; 89 } 90 91 // Gold LTO plugin takes a `--plugin-opt foo=bar` option as an alias for 92 // `--plugin-opt=foo=bar`. We want to handle `--plugin-opt=foo=` as an 93 // option name and `bar` as a value. Unfortunately, OptParser cannot 94 // handle an option with a space in it. 95 // 96 // In this function, we concatenate command line arguments so that 97 // `--plugin-opt <foo>` is converted to `--plugin-opt=<foo>`. This is a 98 // bit hacky, but looks like it is still better than handling --plugin-opt 99 // options by hand. 100 static void concatLTOPluginOptions(SmallVectorImpl<const char *> &args) { 101 SmallVector<const char *, 256> v; 102 for (size_t i = 0, e = args.size(); i != e; ++i) { 103 StringRef s = args[i]; 104 if ((s == "-plugin-opt" || s == "--plugin-opt") && i + 1 != e) { 105 v.push_back(saver.save(s + "=" + args[i + 1]).data()); 106 ++i; 107 } else { 108 v.push_back(args[i]); 109 } 110 } 111 args = std::move(v); 112 } 113 114 // Parses a given list of options. 115 opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> argv) { 116 // Make InputArgList from string vectors. 117 unsigned missingIndex; 118 unsigned missingCount; 119 SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size()); 120 121 // We need to get the quoting style for response files before parsing all 122 // options so we parse here before and ignore all the options but 123 // --rsp-quoting. 124 opt::InputArgList args = this->ParseArgs(vec, missingIndex, missingCount); 125 126 // Expand response files (arguments in the form of @<filename>) 127 // and then parse the argument again. 128 cl::ExpandResponseFiles(saver, getQuotingStyle(args), vec); 129 concatLTOPluginOptions(vec); 130 args = this->ParseArgs(vec, missingIndex, missingCount); 131 132 handleColorDiagnostics(args); 133 if (missingCount) 134 error(Twine(args.getArgString(missingIndex)) + ": missing argument"); 135 136 for (opt::Arg *arg : args.filtered(OPT_UNKNOWN)) { 137 std::string nearest; 138 if (findNearest(arg->getAsString(args), nearest) > 1) 139 error("unknown argument '" + arg->getAsString(args) + "'"); 140 else 141 error("unknown argument '" + arg->getAsString(args) + 142 "', did you mean '" + nearest + "'"); 143 } 144 return args; 145 } 146 147 void elf::printHelp() { 148 ELFOptTable().PrintHelp( 149 lld::outs(), (config->progName + " [options] file...").str().c_str(), 150 "lld", false /*ShowHidden*/, true /*ShowAllAliases*/); 151 lld::outs() << "\n"; 152 153 // Scripts generated by Libtool versions up to at least 2.4.6 (the most 154 // recent version as of March 2017) expect /: supported targets:.* elf/ 155 // in a message for the -help option. If it doesn't match, the scripts 156 // assume that the linker doesn't support very basic features such as 157 // shared libraries. Therefore, we need to print out at least "elf". 158 lld::outs() << config->progName << ": supported targets: elf\n"; 159 } 160 161 static std::string rewritePath(StringRef s) { 162 if (fs::exists(s)) 163 return relativeToRoot(s); 164 return std::string(s); 165 } 166 167 // Reconstructs command line arguments so that so that you can re-run 168 // the same command with the same inputs. This is for --reproduce. 169 std::string elf::createResponseFile(const opt::InputArgList &args) { 170 SmallString<0> data; 171 raw_svector_ostream os(data); 172 os << "--chroot .\n"; 173 174 // Copy the command line to the output while rewriting paths. 175 for (auto *arg : args) { 176 switch (arg->getOption().getID()) { 177 case OPT_reproduce: 178 break; 179 case OPT_INPUT: 180 os << quote(rewritePath(arg->getValue())) << "\n"; 181 break; 182 case OPT_o: 183 // If -o path contains directories, "lld @response.txt" will likely 184 // fail because the archive we are creating doesn't contain empty 185 // directories for the output path (-o doesn't create directories). 186 // Strip directories to prevent the issue. 187 os << "-o " << quote(path::filename(arg->getValue())) << "\n"; 188 break; 189 case OPT_call_graph_ordering_file: 190 case OPT_dynamic_list: 191 case OPT_just_symbols: 192 case OPT_library_path: 193 case OPT_retain_symbols_file: 194 case OPT_rpath: 195 case OPT_script: 196 case OPT_symbol_ordering_file: 197 case OPT_sysroot: 198 case OPT_version_script: 199 os << arg->getSpelling() << " " << quote(rewritePath(arg->getValue())) 200 << "\n"; 201 break; 202 default: 203 os << toString(*arg) << "\n"; 204 } 205 } 206 return std::string(data.str()); 207 } 208 209 // Find a file by concatenating given paths. If a resulting path 210 // starts with "=", the character is replaced with a --sysroot value. 211 static Optional<std::string> findFile(StringRef path1, const Twine &path2) { 212 SmallString<128> s; 213 if (path1.startswith("=")) 214 path::append(s, config->sysroot, path1.substr(1), path2); 215 else 216 path::append(s, path1, path2); 217 218 if (fs::exists(s)) 219 return std::string(s); 220 return None; 221 } 222 223 Optional<std::string> elf::findFromSearchPaths(StringRef path) { 224 for (StringRef dir : config->searchPaths) 225 if (Optional<std::string> s = findFile(dir, path)) 226 return s; 227 return None; 228 } 229 230 // This is for -l<basename>. We'll look for lib<basename>.so or lib<basename>.a from 231 // search paths. 232 Optional<std::string> elf::searchLibraryBaseName(StringRef name) { 233 for (StringRef dir : config->searchPaths) { 234 if (!config->isStatic) 235 if (Optional<std::string> s = findFile(dir, "lib" + name + ".so")) 236 return s; 237 if (Optional<std::string> s = findFile(dir, "lib" + name + ".a")) 238 return s; 239 } 240 return None; 241 } 242 243 // This is for -l<namespec>. 244 Optional<std::string> elf::searchLibrary(StringRef name) { 245 llvm::TimeTraceScope timeScope("Locate library", name); 246 if (name.startswith(":")) 247 return findFromSearchPaths(name.substr(1)); 248 return searchLibraryBaseName(name); 249 } 250 251 // If a linker/version script doesn't exist in the current directory, we also 252 // look for the script in the '-L' search paths. This matches the behaviour of 253 // '-T', --version-script=, and linker script INPUT() command in ld.bfd. 254 Optional<std::string> elf::searchScript(StringRef name) { 255 if (fs::exists(name)) 256 return name.str(); 257 return findFromSearchPaths(name); 258 } 259