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/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/Path.h" 27 28 using namespace llvm; 29 using namespace llvm::sys; 30 31 using namespace lld; 32 using namespace lld::elf; 33 34 // Create OptTable 35 36 // Create prefix string literals used in Options.td 37 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 38 #include "Options.inc" 39 #undef PREFIX 40 41 // Create table mapping all options defined in Options.td 42 static const opt::OptTable::Info OptInfo[] = { 43 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \ 44 {X1, X2, X9, X10, OPT_##ID, opt::Option::KIND##Class, \ 45 X8, X7, OPT_##GROUP, OPT_##ALIAS, X6}, 46 #include "Options.inc" 47 #undef OPTION 48 }; 49 50 ELFOptTable::ELFOptTable() : OptTable(OptInfo) {} 51 52 static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &Args) { 53 if (auto *Arg = Args.getLastArg(OPT_rsp_quoting)) { 54 StringRef S = Arg->getValue(); 55 if (S != "windows" && S != "posix") 56 error("invalid response file quoting: " + S); 57 if (S == "windows") 58 return cl::TokenizeWindowsCommandLine; 59 return cl::TokenizeGNUCommandLine; 60 } 61 if (Triple(sys::getProcessTriple()).getOS() == Triple::Win32) 62 return cl::TokenizeWindowsCommandLine; 63 return cl::TokenizeGNUCommandLine; 64 } 65 66 // Parses a given list of options. 67 opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> Argv) { 68 // Make InputArgList from string vectors. 69 unsigned MissingIndex; 70 unsigned MissingCount; 71 SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size()); 72 73 // We need to get the quoting style for response files before parsing all 74 // options so we parse here before and ignore all the options but 75 // --rsp-quoting. 76 opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount); 77 78 // Expand response files. '@<filename>' is replaced by the file's contents. 79 cl::ExpandResponseFiles(Saver, getQuotingStyle(Args), Vec); 80 81 // Parse options and then do error checking. 82 Args = this->ParseArgs(Vec, MissingIndex, MissingCount); 83 if (MissingCount) 84 error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) + 85 "\", expected " + Twine(MissingCount) + 86 (MissingCount == 1 ? " argument.\n" : " arguments")); 87 88 for (auto *Arg : Args.filtered(OPT_UNKNOWN)) 89 error("unknown argument: " + Arg->getSpelling()); 90 return Args; 91 } 92 93 void elf::printHelp(const char *Argv0) { 94 ELFOptTable Table; 95 Table.PrintHelp(outs(), Argv0, "lld", false); 96 } 97 98 // Reconstructs command line arguments so that so that you can re-run 99 // the same command with the same inputs. This is for --reproduce. 100 std::string elf::createResponseFile(const opt::InputArgList &Args) { 101 SmallString<0> Data; 102 raw_svector_ostream OS(Data); 103 104 // Copy the command line to the output while rewriting paths. 105 for (auto *Arg : Args) { 106 switch (Arg->getOption().getID()) { 107 case OPT_reproduce: 108 break; 109 case OPT_INPUT: 110 OS << quote(rewritePath(Arg->getValue())) << "\n"; 111 break; 112 case OPT_L: 113 case OPT_dynamic_list: 114 case OPT_rpath: 115 case OPT_alias_script_T: 116 case OPT_script: 117 case OPT_version_script: 118 OS << Arg->getSpelling() << " " << quote(rewritePath(Arg->getValue())) 119 << "\n"; 120 break; 121 default: 122 OS << stringize(Arg) << "\n"; 123 } 124 } 125 return Data.str(); 126 } 127 128 std::string elf::findFromSearchPaths(StringRef Path) { 129 for (StringRef Dir : Config->SearchPaths) { 130 std::string FullPath = buildSysrootedPath(Dir, Path); 131 if (fs::exists(FullPath)) 132 return FullPath; 133 } 134 return ""; 135 } 136 137 // Searches a given library from input search paths, which are filled 138 // from -L command line switches. Returns a path to an existent library file. 139 std::string elf::searchLibrary(StringRef Path) { 140 if (Path.startswith(":")) 141 return findFromSearchPaths(Path.substr(1)); 142 for (StringRef Dir : Config->SearchPaths) { 143 if (!Config->Static) { 144 std::string S = buildSysrootedPath(Dir, ("lib" + Path + ".so").str()); 145 if (fs::exists(S)) 146 return S; 147 } 148 std::string S = buildSysrootedPath(Dir, ("lib" + Path + ".a").str()); 149 if (fs::exists(S)) 150 return S; 151 } 152 return ""; 153 } 154 155 // Makes a path by concatenating Dir and File. 156 // If Dir starts with '=' the result will be preceded by Sysroot, 157 // which can be set with --sysroot command line switch. 158 std::string elf::buildSysrootedPath(StringRef Dir, StringRef File) { 159 SmallString<128> Path; 160 if (Dir.startswith("=")) 161 path::append(Path, Config->Sysroot, Dir.substr(1), File); 162 else 163 path::append(Path, Dir, File); 164 return Path.str(); 165 } 166