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 "lld/Config/Version.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/Support/CommandLine.h" 21 #include "llvm/Support/FileSystem.h" 22 #include "llvm/Support/Path.h" 23 #include "llvm/Support/StringSaver.h" 24 25 using namespace llvm; 26 using namespace llvm::sys; 27 28 using namespace lld; 29 using namespace lld::elf; 30 31 // Create OptTable 32 33 // Create prefix string literals used in Options.td 34 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 35 #include "Options.inc" 36 #undef PREFIX 37 38 // Create table mapping all options defined in Options.td 39 static const opt::OptTable::Info OptInfo[] = { 40 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \ 41 { \ 42 X1, X2, X9, X10, OPT_##ID, opt::Option::KIND##Class, X8, X7, OPT_##GROUP, \ 43 OPT_##ALIAS, X6 \ 44 }, 45 #include "Options.inc" 46 #undef OPTION 47 }; 48 49 ELFOptTable::ELFOptTable() : OptTable(OptInfo) {} 50 51 // Parses a given list of options. 52 opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> Argv) { 53 // Make InputArgList from string vectors. 54 unsigned MissingIndex; 55 unsigned MissingCount; 56 57 // Expand response files. '@<filename>' is replaced by the file's contents. 58 SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size()); 59 StringSaver Saver(Alloc); 60 llvm::cl::ExpandResponseFiles(Saver, llvm::cl::TokenizeGNUCommandLine, Vec); 61 62 // Parse options and then do error checking. 63 opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount); 64 if (MissingCount) 65 error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) + 66 "\", expected " + Twine(MissingCount) + 67 (MissingCount == 1 ? " argument.\n" : " arguments")); 68 69 iterator_range<opt::arg_iterator> Unknowns = Args.filtered(OPT_UNKNOWN); 70 for (auto *Arg : Unknowns) 71 warning("warning: unknown argument: " + Arg->getSpelling()); 72 if (Unknowns.begin() != Unknowns.end()) 73 error("unknown argument(s) found"); 74 return Args; 75 } 76 77 void elf::printHelp(const char *Argv0) { 78 ELFOptTable Table; 79 Table.PrintHelp(outs(), Argv0, "lld", false); 80 } 81 82 void elf::printVersion() { 83 outs() << "LLD " << getLLDVersion(); 84 std::string S = getLLDRepositoryVersion(); 85 if (!S.empty()) 86 outs() << " " << S; 87 outs() << "\n"; 88 } 89 90 // Concatenates S and T so that the resulting path becomes S/T. 91 // There are a few exceptions: 92 // 93 // 1. The result will never escape from S. Therefore, all ".." 94 // are removed from T before concatenatig them. 95 // 2. Windows drive letters are removed from T before concatenation. 96 std::string elf::concat_paths(StringRef S, StringRef T) { 97 // Remove leading '/' or a drive letter, and then remove "..". 98 SmallString<128> T2(path::relative_path(T)); 99 path::remove_dots(T2, /*remove_dot_dot=*/true); 100 101 SmallString<128> Res; 102 path::append(Res, S, T2); 103 return Res.str(); 104 } 105 106 void elf::copyFile(StringRef Src, StringRef Dest) { 107 SmallString<128> Dir(Dest); 108 path::remove_filename(Dir); 109 if (std::error_code EC = sys::fs::create_directories(Dir)) { 110 error(EC, Dir + ": can't create directory"); 111 return; 112 } 113 if (std::error_code EC = sys::fs::copy_file(Src, Dest)) 114 error(EC, "failed to copy file: " + Dest); 115 } 116 117 std::string elf::findFromSearchPaths(StringRef Path) { 118 for (StringRef Dir : Config->SearchPaths) { 119 std::string FullPath = buildSysrootedPath(Dir, Path); 120 if (sys::fs::exists(FullPath)) 121 return FullPath; 122 } 123 return ""; 124 } 125 126 // Searches a given library from input search paths, which are filled 127 // from -L command line switches. Returns a path to an existent library file. 128 std::string elf::searchLibrary(StringRef Path) { 129 if (Path.startswith(":")) 130 return findFromSearchPaths(Path.substr(1)); 131 if (!Config->Static) { 132 std::string S = findFromSearchPaths(("lib" + Path + ".so").str()); 133 if (!S.empty()) 134 return S; 135 } 136 return findFromSearchPaths(("lib" + Path + ".a").str()); 137 } 138 139 // Makes a path by concatenating Dir and File. 140 // If Dir starts with '=' the result will be preceded by Sysroot, 141 // which can be set with --sysroot command line switch. 142 std::string elf::buildSysrootedPath(StringRef Dir, StringRef File) { 143 SmallString<128> Path; 144 if (Dir.startswith("=")) 145 sys::path::append(Path, Config->Sysroot, Dir.substr(1), File); 146 else 147 sys::path::append(Path, Dir, File); 148 return Path.str(); 149 } 150