1 //===- DriverUtils.cpp ----------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains utility functions for the driver. Because there 11 // are so many small functions, we created this separate file to make 12 // Driver.cpp less cluttered. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "Driver.h" 17 #include "Error.h" 18 #include "Memory.h" 19 #include "lld/Config/Version.h" 20 #include "lld/Core/Reproduce.h" 21 #include "llvm/ADT/Optional.h" 22 #include "llvm/ADT/STLExtras.h" 23 #include "llvm/ADT/Triple.h" 24 #include "llvm/Option/Option.h" 25 #include "llvm/Support/CommandLine.h" 26 #include "llvm/Support/FileSystem.h" 27 #include "llvm/Support/Path.h" 28 #include "llvm/Support/Process.h" 29 30 using namespace llvm; 31 using namespace llvm::sys; 32 33 using namespace lld; 34 using namespace lld::elf; 35 36 // Create OptTable 37 38 // Create prefix string literals used in Options.td 39 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 40 #include "Options.inc" 41 #undef PREFIX 42 43 // Create table mapping all options defined in Options.td 44 static const opt::OptTable::Info OptInfo[] = { 45 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \ 46 {X1, X2, X9, X10, OPT_##ID, opt::Option::KIND##Class, \ 47 X8, X7, OPT_##GROUP, OPT_##ALIAS, X6}, 48 #include "Options.inc" 49 #undef OPTION 50 }; 51 52 ELFOptTable::ELFOptTable() : OptTable(OptInfo) {} 53 54 // Parse -color-diagnostics={auto,always,never} or -no-color-diagnostics. 55 static bool getColorDiagnostics(opt::InputArgList &Args) { 56 bool Default = (ErrorOS == &errs() && Process::StandardErrHasColors()); 57 58 auto *Arg = Args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq, 59 OPT_no_color_diagnostics); 60 if (!Arg) 61 return Default; 62 if (Arg->getOption().getID() == OPT_color_diagnostics) 63 return true; 64 if (Arg->getOption().getID() == OPT_no_color_diagnostics) 65 return false; 66 67 StringRef S = Arg->getValue(); 68 if (S == "auto") 69 return Default; 70 if (S == "always") 71 return true; 72 if (S != "never") 73 error("unknown option: -color-diagnostics=" + S); 74 return false; 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()).getOS() == Triple::Win32) 87 return cl::TokenizeWindowsCommandLine; 88 return cl::TokenizeGNUCommandLine; 89 } 90 91 // Parses a given list of options. 92 opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> Argv) { 93 // Make InputArgList from string vectors. 94 unsigned MissingIndex; 95 unsigned MissingCount; 96 SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size()); 97 98 // We need to get the quoting style for response files before parsing all 99 // options so we parse here before and ignore all the options but 100 // --rsp-quoting. 101 opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount); 102 103 // Expand response files (arguments in the form of @<filename>) 104 // and then parse the argument again. 105 cl::ExpandResponseFiles(Saver, getQuotingStyle(Args), Vec); 106 Args = this->ParseArgs(Vec, MissingIndex, MissingCount); 107 108 // Interpret -color-diagnostics early so that error messages 109 // for unknown flags are colored. 110 Config->ColorDiagnostics = getColorDiagnostics(Args); 111 if (MissingCount) 112 error(Twine(Args.getArgString(MissingIndex)) + ": missing argument"); 113 114 for (auto *Arg : Args.filtered(OPT_UNKNOWN)) 115 error("unknown argument: " + Arg->getSpelling()); 116 return Args; 117 } 118 119 void elf::printHelp(const char *Argv0) { 120 ELFOptTable Table; 121 Table.PrintHelp(outs(), Argv0, "lld", false); 122 } 123 124 // Reconstructs command line arguments so that so that you can re-run 125 // the same command with the same inputs. This is for --reproduce. 126 std::string elf::createResponseFile(const opt::InputArgList &Args) { 127 SmallString<0> Data; 128 raw_svector_ostream OS(Data); 129 130 // Copy the command line to the output while rewriting paths. 131 for (auto *Arg : Args) { 132 switch (Arg->getOption().getID()) { 133 case OPT_reproduce: 134 break; 135 case OPT_INPUT: 136 OS << quote(rewritePath(Arg->getValue())) << "\n"; 137 break; 138 case OPT_o: 139 // If -o path contains directories, "lld @response.txt" will likely 140 // fail because the archive we are creating doesn't contain empty 141 // directories for the output path (-o doesn't create directories). 142 // Strip directories to prevent the issue. 143 OS << "-o " << quote(sys::path::filename(Arg->getValue())) << "\n"; 144 break; 145 case OPT_L: 146 case OPT_dynamic_list: 147 case OPT_rpath: 148 case OPT_alias_script_T: 149 case OPT_script: 150 case OPT_version_script: 151 OS << Arg->getSpelling() << " " << quote(rewritePath(Arg->getValue())) 152 << "\n"; 153 break; 154 default: 155 OS << toString(Arg) << "\n"; 156 } 157 } 158 return Data.str(); 159 } 160 161 // Find a file by concatenating given paths. If a resulting path 162 // starts with "=", the character is replaced with a --sysroot value. 163 static Optional<std::string> findFile(StringRef Path1, const Twine &Path2) { 164 SmallString<128> S; 165 if (Path1.startswith("=")) 166 path::append(S, Config->Sysroot, Path1.substr(1), Path2); 167 else 168 path::append(S, Path1, Path2); 169 170 if (fs::exists(S)) 171 return S.str().str(); 172 return None; 173 } 174 175 Optional<std::string> elf::findFromSearchPaths(StringRef Path) { 176 for (StringRef Dir : Config->SearchPaths) 177 if (Optional<std::string> S = findFile(Dir, Path)) 178 return S; 179 return None; 180 } 181 182 // This is for -lfoo. We'll look for libfoo.so or libfoo.a from 183 // search paths. 184 Optional<std::string> elf::searchLibrary(StringRef Name) { 185 if (Name.startswith(":")) 186 return findFromSearchPaths(Name.substr(1)); 187 188 for (StringRef Dir : Config->SearchPaths) { 189 if (!Config->Static) 190 if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".so")) 191 return S; 192 if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".a")) 193 return S; 194 } 195 return None; 196 } 197