| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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" | 
| Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 17 | #include "Error.h" | 
| Rui Ueyama | 1abcf37 | 2016-02-28 03:18:07 +0000 | [diff] [blame] | 18 | #include "lld/Config/Version.h" | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" | 
| Rui Ueyama | eeb22f8 | 2015-09-25 15:37:33 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" | 
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileSystem.h" | 
|  | 22 | #include "llvm/Support/Path.h" | 
| Rui Ueyama | c6e1b97 | 2015-10-11 18:19:01 +0000 | [diff] [blame] | 23 | #include "llvm/Support/StringSaver.h" | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 24 |  | 
|  | 25 | using namespace llvm; | 
| Rui Ueyama | cf0dd1e | 2016-04-26 20:41:32 +0000 | [diff] [blame] | 26 | using namespace llvm::sys; | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 27 |  | 
|  | 28 | using namespace lld; | 
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 29 | using namespace lld::elf; | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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 | 
| Rui Ueyama | 8fe7ce5 | 2016-04-02 19:15:26 +0000 | [diff] [blame] | 39 | static const opt::OptTable::Info OptInfo[] = { | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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                                                        \ | 
| Rui Ueyama | 8fe7ce5 | 2016-04-02 19:15:26 +0000 | [diff] [blame] | 44 | }, | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 45 | #include "Options.inc" | 
|  | 46 | #undef OPTION | 
|  | 47 | }; | 
|  | 48 |  | 
| Rui Ueyama | 8fe7ce5 | 2016-04-02 19:15:26 +0000 | [diff] [blame] | 49 | ELFOptTable::ELFOptTable() : OptTable(OptInfo) {} | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 50 |  | 
|  | 51 | // Parses a given list of options. | 
| Rui Ueyama | 15fa035 | 2016-03-15 18:20:50 +0000 | [diff] [blame] | 52 | opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> Argv) { | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 53 | // Make InputArgList from string vectors. | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 54 | unsigned MissingIndex; | 
|  | 55 | unsigned MissingCount; | 
|  | 56 |  | 
| Rui Ueyama | eeb22f8 | 2015-09-25 15:37:33 +0000 | [diff] [blame] | 57 | // Expand response files. '@<filename>' is replaced by the file's contents. | 
|  | 58 | SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size()); | 
| Rui Ueyama | 15fa035 | 2016-03-15 18:20:50 +0000 | [diff] [blame] | 59 | StringSaver Saver(Alloc); | 
| Rui Ueyama | eeb22f8 | 2015-09-25 15:37:33 +0000 | [diff] [blame] | 60 | llvm::cl::ExpandResponseFiles(Saver, llvm::cl::TokenizeGNUCommandLine, Vec); | 
|  | 61 |  | 
|  | 62 | // Parse options and then do error checking. | 
| Rui Ueyama | 15fa035 | 2016-03-15 18:20:50 +0000 | [diff] [blame] | 63 | opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount); | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 64 | if (MissingCount) | 
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 65 | error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) + | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 66 | "\", expected " + Twine(MissingCount) + | 
|  | 67 | (MissingCount == 1 ? " argument.\n" : " arguments")); | 
| Rui Ueyama | d5b5ab7 | 2015-09-24 18:55:33 +0000 | [diff] [blame] | 68 |  | 
|  | 69 | iterator_range<opt::arg_iterator> Unknowns = Args.filtered(OPT_UNKNOWN); | 
|  | 70 | for (auto *Arg : Unknowns) | 
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 71 | warning("warning: unknown argument: " + Arg->getSpelling()); | 
| Rui Ueyama | d5b5ab7 | 2015-09-24 18:55:33 +0000 | [diff] [blame] | 72 | if (Unknowns.begin() != Unknowns.end()) | 
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 73 | error("unknown argument(s) found"); | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 74 | return Args; | 
|  | 75 | } | 
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 76 |  | 
| Rui Ueyama | 1eb9f44 | 2016-02-28 03:18:09 +0000 | [diff] [blame] | 77 | void elf::printHelp(const char *Argv0) { | 
|  | 78 | ELFOptTable Table; | 
|  | 79 | Table.PrintHelp(outs(), Argv0, "lld", false); | 
|  | 80 | } | 
|  | 81 |  | 
| Rui Ueyama | 1abcf37 | 2016-02-28 03:18:07 +0000 | [diff] [blame] | 82 | void elf::printVersion() { | 
|  | 83 | outs() << "LLD " << getLLDVersion(); | 
|  | 84 | std::string S = getLLDRepositoryVersion(); | 
|  | 85 | if (!S.empty()) | 
| Rui Ueyama | 5bc2acd | 2016-03-13 23:07:42 +0000 | [diff] [blame] | 86 | outs() << " " << S; | 
|  | 87 | outs() << "\n"; | 
| Rui Ueyama | 1abcf37 | 2016-02-28 03:18:07 +0000 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 90 | // Makes a given pathname an absolute path first, and then remove | 
|  | 91 | // beginning /. For example, "../foo.o" is converted to "home/john/foo.o", | 
|  | 92 | // assuming that the current directory is "/home/john/bar". | 
| Rui Ueyama | 9aea957 | 2016-04-30 22:23:29 +0000 | [diff] [blame] | 93 | static std::string relativeToRoot(StringRef Path) { | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 94 | SmallString<128> Abs = Path; | 
|  | 95 | if (std::error_code EC = fs::make_absolute(Abs)) | 
|  | 96 | fatal("make_absolute failed: " + EC.message()); | 
|  | 97 | path::remove_dots(Abs, /*remove_dot_dot=*/true); | 
| Rui Ueyama | cf0dd1e | 2016-04-26 20:41:32 +0000 | [diff] [blame] | 98 |  | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 99 | // This is Windows specific. root_name() returns a drive letter | 
|  | 100 | // (e.g. "c:") or a UNC name (//net). We want to keep it as part | 
|  | 101 | // of the result. | 
| Rui Ueyama | cf0dd1e | 2016-04-26 20:41:32 +0000 | [diff] [blame] | 102 | SmallString<128> Res; | 
| Rui Ueyama | ff12f23 | 2016-05-02 18:16:14 +0000 | [diff] [blame] | 103 | StringRef Root = path::root_name(Abs); | 
| Rui Ueyama | 38aa57d | 2016-04-30 22:20:27 +0000 | [diff] [blame] | 104 | if (Root.endswith(":")) | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 105 | Res = Root.drop_back(); | 
| Rui Ueyama | 38aa57d | 2016-04-30 22:20:27 +0000 | [diff] [blame] | 106 | else if (Root.startswith("//")) | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 107 | Res = Root.substr(2); | 
|  | 108 |  | 
|  | 109 | path::append(Res, path::relative_path(Abs)); | 
| Rui Ueyama | cf0dd1e | 2016-04-26 20:41:32 +0000 | [diff] [blame] | 110 | return Res.str(); | 
|  | 111 | } | 
|  | 112 |  | 
| Rui Ueyama | 9aea957 | 2016-04-30 22:23:29 +0000 | [diff] [blame] | 113 | static std::string getDestPath(StringRef Path) { | 
|  | 114 | std::string Relpath = relativeToRoot(Path); | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 115 | SmallString<128> Dest; | 
|  | 116 | path::append(Dest, Config->Reproduce, Relpath); | 
| Rui Ueyama | 9aea957 | 2016-04-30 22:23:29 +0000 | [diff] [blame] | 117 | return Dest.str(); | 
|  | 118 | } | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 119 |  | 
| Rui Ueyama | 9aea957 | 2016-04-30 22:23:29 +0000 | [diff] [blame] | 120 | // Copies file Src to {Config->Reproduce}/Src. | 
|  | 121 | void elf::copyInputFile(StringRef Src) { | 
|  | 122 | std::string Dest = getDestPath(Src); | 
| Rui Ueyama | cf0dd1e | 2016-04-26 20:41:32 +0000 | [diff] [blame] | 123 | SmallString<128> Dir(Dest); | 
|  | 124 | path::remove_filename(Dir); | 
| Rui Ueyama | fc28827 | 2016-05-02 18:51:08 +0000 | [diff] [blame^] | 125 | if (std::error_code EC = fs::create_directories(Dir)) { | 
| Rui Ueyama | cf0dd1e | 2016-04-26 20:41:32 +0000 | [diff] [blame] | 126 | error(EC, Dir + ": can't create directory"); | 
| Rui Ueyama | 9aea957 | 2016-04-30 22:23:29 +0000 | [diff] [blame] | 127 | return; | 
|  | 128 | } | 
| Rui Ueyama | fc28827 | 2016-05-02 18:51:08 +0000 | [diff] [blame^] | 129 | if (std::error_code EC = fs::copy_file(Src, Dest)) | 
| Rui Ueyama | cf0dd1e | 2016-04-26 20:41:32 +0000 | [diff] [blame] | 130 | error(EC, "failed to copy file: " + Dest); | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
|  | 133 | // Quote a given string if it contains a space character. | 
|  | 134 | static std::string quote(StringRef S) { | 
|  | 135 | if (S.find(' ') == StringRef::npos) | 
|  | 136 | return S; | 
|  | 137 | return ("\"" + S + "\"").str(); | 
|  | 138 | } | 
|  | 139 |  | 
| Rui Ueyama | 2796ae3 | 2016-04-30 22:46:47 +0000 | [diff] [blame] | 140 | static std::string rewritePath(StringRef S) { | 
|  | 141 | if (fs::exists(S)) | 
| Rafael Espindola | e85bcbd | 2016-05-02 14:12:35 +0000 | [diff] [blame] | 142 | return relativeToRoot(S); | 
| Rui Ueyama | 2796ae3 | 2016-04-30 22:46:47 +0000 | [diff] [blame] | 143 | return S; | 
|  | 144 | } | 
|  | 145 |  | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 146 | // Copies all input files to Config->Reproduce directory and | 
|  | 147 | // create a response file as "response.txt", so that you can re-run | 
|  | 148 | // the same command with the same inputs just by executing | 
|  | 149 | // "ld.lld @response.txt". Used by --reproduce. This feature is | 
|  | 150 | // supposed to be used by users to report an issue to LLD developers. | 
| Rui Ueyama | 9aea957 | 2016-04-30 22:23:29 +0000 | [diff] [blame] | 151 | void elf::createResponseFile(const llvm::opt::InputArgList &Args) { | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 152 | // Create the output directory. | 
| Rui Ueyama | fc28827 | 2016-05-02 18:51:08 +0000 | [diff] [blame^] | 153 | if (std::error_code EC = | 
|  | 154 | fs::create_directories(Config->Reproduce, /*IgnoreExisting=*/false)) { | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 155 | error(EC, Config->Reproduce + ": can't create directory"); | 
|  | 156 | return; | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | // Open "response.txt". | 
|  | 160 | SmallString<128> Path; | 
|  | 161 | path::append(Path, Config->Reproduce, "response.txt"); | 
|  | 162 | std::error_code EC; | 
| Rui Ueyama | fc28827 | 2016-05-02 18:51:08 +0000 | [diff] [blame^] | 163 | raw_fd_ostream OS(Path, EC, fs::OpenFlags::F_None); | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 164 | check(EC); | 
|  | 165 |  | 
| Rui Ueyama | 2796ae3 | 2016-04-30 22:46:47 +0000 | [diff] [blame] | 166 | // Copy the command line to response.txt while rewriting paths. | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 167 | for (auto *Arg : Args) { | 
|  | 168 | switch (Arg->getOption().getID()) { | 
|  | 169 | case OPT_reproduce: | 
|  | 170 | break; | 
| Rui Ueyama | 2796ae3 | 2016-04-30 22:46:47 +0000 | [diff] [blame] | 171 | case OPT_INPUT: | 
|  | 172 | OS << quote(rewritePath(Arg->getValue())) << "\n"; | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 173 | break; | 
| Rui Ueyama | 2796ae3 | 2016-04-30 22:46:47 +0000 | [diff] [blame] | 174 | case OPT_L: | 
|  | 175 | case OPT_dynamic_list: | 
| Rui Ueyama | 2796ae3 | 2016-04-30 22:46:47 +0000 | [diff] [blame] | 176 | case OPT_rpath: | 
|  | 177 | case OPT_script: | 
|  | 178 | case OPT_version_script: | 
|  | 179 | OS << Arg->getSpelling() << " " | 
|  | 180 | << quote(rewritePath(Arg->getValue())) << "\n"; | 
|  | 181 | break; | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 182 | default: | 
| Rui Ueyama | e951a1d | 2016-05-02 17:34:17 +0000 | [diff] [blame] | 183 | OS << Arg->getSpelling(); | 
|  | 184 | if (Arg->getNumValues() > 0) | 
|  | 185 | OS << " " << quote(Arg->getValue()); | 
|  | 186 | OS << "\n"; | 
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 187 | } | 
|  | 188 | } | 
| Rui Ueyama | cf0dd1e | 2016-04-26 20:41:32 +0000 | [diff] [blame] | 189 | } | 
|  | 190 |  | 
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 191 | std::string elf::findFromSearchPaths(StringRef Path) { | 
| Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 192 | for (StringRef Dir : Config->SearchPaths) { | 
|  | 193 | std::string FullPath = buildSysrootedPath(Dir, Path); | 
| Rui Ueyama | fc28827 | 2016-05-02 18:51:08 +0000 | [diff] [blame^] | 194 | if (fs::exists(FullPath)) | 
| Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 195 | return FullPath; | 
|  | 196 | } | 
|  | 197 | return ""; | 
|  | 198 | } | 
|  | 199 |  | 
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 200 | // Searches a given library from input search paths, which are filled | 
|  | 201 | // from -L command line switches. Returns a path to an existent library file. | 
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 202 | std::string elf::searchLibrary(StringRef Path) { | 
| Rui Ueyama | 319d730 | 2016-03-29 20:53:21 +0000 | [diff] [blame] | 203 | if (Path.startswith(":")) | 
|  | 204 | return findFromSearchPaths(Path.substr(1)); | 
|  | 205 | if (!Config->Static) { | 
|  | 206 | std::string S = findFromSearchPaths(("lib" + Path + ".so").str()); | 
| Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 207 | if (!S.empty()) | 
|  | 208 | return S; | 
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 209 | } | 
| Rui Ueyama | 319d730 | 2016-03-29 20:53:21 +0000 | [diff] [blame] | 210 | return findFromSearchPaths(("lib" + Path + ".a").str()); | 
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 211 | } | 
|  | 212 |  | 
|  | 213 | // Makes a path by concatenating Dir and File. | 
|  | 214 | // If Dir starts with '=' the result will be preceded by Sysroot, | 
|  | 215 | // which can be set with --sysroot command line switch. | 
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 216 | std::string elf::buildSysrootedPath(StringRef Dir, StringRef File) { | 
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 217 | SmallString<128> Path; | 
|  | 218 | if (Dir.startswith("=")) | 
| Rui Ueyama | fc28827 | 2016-05-02 18:51:08 +0000 | [diff] [blame^] | 219 | path::append(Path, Config->Sysroot, Dir.substr(1), File); | 
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 220 | else | 
| Rui Ueyama | fc28827 | 2016-05-02 18:51:08 +0000 | [diff] [blame^] | 221 | path::append(Path, Dir, File); | 
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 222 | return Path.str(); | 
|  | 223 | } |