| 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 | af2312f | 2016-05-04 23:12:55 +0000 | [diff] [blame] | 20 | #include "llvm/Option/Option.h" |
| Rui Ueyama | eeb22f8 | 2015-09-25 15:37:33 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileSystem.h" |
| 23 | #include "llvm/Support/Path.h" |
| Rui Ueyama | c6e1b97 | 2015-10-11 18:19:01 +0000 | [diff] [blame] | 24 | #include "llvm/Support/StringSaver.h" |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| Rui Ueyama | cf0dd1e | 2016-04-26 20:41:32 +0000 | [diff] [blame] | 27 | using namespace llvm::sys; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace lld; |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 30 | using namespace lld::elf; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 31 | |
| 32 | // Create OptTable |
| 33 | |
| 34 | // Create prefix string literals used in Options.td |
| 35 | #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; |
| 36 | #include "Options.inc" |
| 37 | #undef PREFIX |
| 38 | |
| 39 | // Create table mapping all options defined in Options.td |
| Rui Ueyama | 8fe7ce5 | 2016-04-02 19:15:26 +0000 | [diff] [blame] | 40 | static const opt::OptTable::Info OptInfo[] = { |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 41 | #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \ |
| 42 | { \ |
| 43 | X1, X2, X9, X10, OPT_##ID, opt::Option::KIND##Class, X8, X7, OPT_##GROUP, \ |
| 44 | OPT_##ALIAS, X6 \ |
| Rui Ueyama | 8fe7ce5 | 2016-04-02 19:15:26 +0000 | [diff] [blame] | 45 | }, |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 46 | #include "Options.inc" |
| 47 | #undef OPTION |
| 48 | }; |
| 49 | |
| Rui Ueyama | 8fe7ce5 | 2016-04-02 19:15:26 +0000 | [diff] [blame] | 50 | ELFOptTable::ELFOptTable() : OptTable(OptInfo) {} |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 51 | |
| 52 | // Parses a given list of options. |
| Rui Ueyama | 15fa035 | 2016-03-15 18:20:50 +0000 | [diff] [blame] | 53 | opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> Argv) { |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 54 | // Make InputArgList from string vectors. |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 55 | unsigned MissingIndex; |
| 56 | unsigned MissingCount; |
| 57 | |
| Rui Ueyama | eeb22f8 | 2015-09-25 15:37:33 +0000 | [diff] [blame] | 58 | // Expand response files. '@<filename>' is replaced by the file's contents. |
| 59 | SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size()); |
| Rui Ueyama | 15fa035 | 2016-03-15 18:20:50 +0000 | [diff] [blame] | 60 | StringSaver Saver(Alloc); |
| Rui Ueyama | 5002a67 | 2016-05-02 19:59:56 +0000 | [diff] [blame] | 61 | cl::ExpandResponseFiles(Saver, cl::TokenizeGNUCommandLine, Vec); |
| Rui Ueyama | eeb22f8 | 2015-09-25 15:37:33 +0000 | [diff] [blame] | 62 | |
| 63 | // Parse options and then do error checking. |
| Rui Ueyama | 15fa035 | 2016-03-15 18:20:50 +0000 | [diff] [blame] | 64 | opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 65 | if (MissingCount) |
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 66 | error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) + |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 67 | "\", expected " + Twine(MissingCount) + |
| 68 | (MissingCount == 1 ? " argument.\n" : " arguments")); |
| Rui Ueyama | d5b5ab7 | 2015-09-24 18:55:33 +0000 | [diff] [blame] | 69 | |
| 70 | iterator_range<opt::arg_iterator> Unknowns = Args.filtered(OPT_UNKNOWN); |
| 71 | for (auto *Arg : Unknowns) |
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 72 | warning("warning: unknown argument: " + Arg->getSpelling()); |
| Rui Ueyama | d5b5ab7 | 2015-09-24 18:55:33 +0000 | [diff] [blame] | 73 | if (Unknowns.begin() != Unknowns.end()) |
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 74 | error("unknown argument(s) found"); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 75 | return Args; |
| 76 | } |
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 77 | |
| Rui Ueyama | 1eb9f44 | 2016-02-28 03:18:09 +0000 | [diff] [blame] | 78 | void elf::printHelp(const char *Argv0) { |
| 79 | ELFOptTable Table; |
| 80 | Table.PrintHelp(outs(), Argv0, "lld", false); |
| 81 | } |
| 82 | |
| Rui Ueyama | 1abcf37 | 2016-02-28 03:18:07 +0000 | [diff] [blame] | 83 | void elf::printVersion() { |
| 84 | outs() << "LLD " << getLLDVersion(); |
| 85 | std::string S = getLLDRepositoryVersion(); |
| 86 | if (!S.empty()) |
| Rui Ueyama | 5bc2acd | 2016-03-13 23:07:42 +0000 | [diff] [blame] | 87 | outs() << " " << S; |
| 88 | outs() << "\n"; |
| Rui Ueyama | 1abcf37 | 2016-02-28 03:18:07 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 91 | // Makes a given pathname an absolute path first, and then remove |
| 92 | // beginning /. For example, "../foo.o" is converted to "home/john/foo.o", |
| 93 | // assuming that the current directory is "/home/john/bar". |
| Rui Ueyama | 9aea957 | 2016-04-30 22:23:29 +0000 | [diff] [blame] | 94 | static std::string relativeToRoot(StringRef Path) { |
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 95 | SmallString<128> Abs = Path; |
| 96 | if (std::error_code EC = fs::make_absolute(Abs)) |
| 97 | fatal("make_absolute failed: " + EC.message()); |
| 98 | path::remove_dots(Abs, /*remove_dot_dot=*/true); |
| Rui Ueyama | cf0dd1e | 2016-04-26 20:41:32 +0000 | [diff] [blame] | 99 | |
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 100 | // This is Windows specific. root_name() returns a drive letter |
| 101 | // (e.g. "c:") or a UNC name (//net). We want to keep it as part |
| 102 | // of the result. |
| Rui Ueyama | cf0dd1e | 2016-04-26 20:41:32 +0000 | [diff] [blame] | 103 | SmallString<128> Res; |
| Rui Ueyama | ff12f23 | 2016-05-02 18:16:14 +0000 | [diff] [blame] | 104 | StringRef Root = path::root_name(Abs); |
| Rui Ueyama | 38aa57d | 2016-04-30 22:20:27 +0000 | [diff] [blame] | 105 | if (Root.endswith(":")) |
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 106 | Res = Root.drop_back(); |
| Rui Ueyama | 38aa57d | 2016-04-30 22:20:27 +0000 | [diff] [blame] | 107 | else if (Root.startswith("//")) |
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 108 | Res = Root.substr(2); |
| 109 | |
| 110 | path::append(Res, path::relative_path(Abs)); |
| Rui Ueyama | cf0dd1e | 2016-04-26 20:41:32 +0000 | [diff] [blame] | 111 | return Res.str(); |
| 112 | } |
| 113 | |
| Rui Ueyama | 9aea957 | 2016-04-30 22:23:29 +0000 | [diff] [blame] | 114 | static std::string getDestPath(StringRef Path) { |
| 115 | std::string Relpath = relativeToRoot(Path); |
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 116 | SmallString<128> Dest; |
| Rafael Espindola | 1dd2b3d | 2016-05-03 17:30:44 +0000 | [diff] [blame] | 117 | path::append(Dest, path::filename(Config->Reproduce), Relpath); |
| Rui Ueyama | 9aea957 | 2016-04-30 22:23:29 +0000 | [diff] [blame] | 118 | return Dest.str(); |
| 119 | } |
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 120 | |
| Rafael Espindola | 2c51e61 | 2016-05-04 12:47:56 +0000 | [diff] [blame] | 121 | static void maybePrintCpioMemberAux(raw_fd_ostream &OS, StringRef Path, |
| 122 | StringRef Data) { |
| Rafael Espindola | 1dd2b3d | 2016-05-03 17:30:44 +0000 | [diff] [blame] | 123 | OS << "070707"; // c_magic |
| 124 | |
| 125 | // The c_dev/c_ino pair should be unique according to the spec, but no one |
| 126 | // seems to care. |
| 127 | OS << "000000"; // c_dev |
| 128 | OS << "000000"; // c_ino |
| 129 | |
| 130 | OS << "100664"; // c_mode: C_ISREG | rw-rw-r-- |
| 131 | OS << "000000"; // c_uid |
| 132 | OS << "000000"; // c_gid |
| 133 | OS << "000001"; // c_nlink |
| 134 | OS << "000000"; // c_rdev |
| 135 | OS << "00000000000"; // c_mtime |
| 136 | OS << format("%06o", Path.size() + 1); // c_namesize |
| 137 | OS << format("%011o", Data.size()); // c_filesize |
| 138 | OS << Path << '\0'; // c_name |
| 139 | OS << Data; // c_filedata |
| 140 | } |
| 141 | |
| Rafael Espindola | 2c51e61 | 2016-05-04 12:47:56 +0000 | [diff] [blame] | 142 | static void maybePrintCpioMember(StringRef Path, StringRef Data) { |
| 143 | if (Config->Reproduce.empty()) |
| 144 | return; |
| 145 | |
| 146 | if (!Driver->IncludedFiles.insert(Path).second) |
| 147 | return; |
| 148 | raw_fd_ostream &OS = *Driver->ReproduceArchive; |
| 149 | maybePrintCpioMemberAux(OS, Path, Data); |
| 150 | |
| 151 | // Print the trailer and seek back. This way we have a valid archive if we |
| 152 | // crash. |
| Rafael Espindola | f72914f | 2016-05-10 19:26:55 +0000 | [diff] [blame] | 153 | // See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_11 |
| 154 | // for the format details. |
| Rafael Espindola | 2c51e61 | 2016-05-04 12:47:56 +0000 | [diff] [blame] | 155 | uint64_t Pos = OS.tell(); |
| 156 | maybePrintCpioMemberAux(OS, "TRAILER!!!", ""); |
| 157 | OS.seek(Pos); |
| 158 | } |
| 159 | |
| Rafael Espindola | 1dd2b3d | 2016-05-03 17:30:44 +0000 | [diff] [blame] | 160 | // Write file Src with content Data to the archive. |
| 161 | void elf::maybeCopyInputFile(StringRef Src, StringRef Data) { |
| 162 | std::string Dest = getDestPath(Src); |
| 163 | maybePrintCpioMember(Dest, Data); |
| 164 | } |
| 165 | |
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 166 | // Quote a given string if it contains a space character. |
| 167 | static std::string quote(StringRef S) { |
| 168 | if (S.find(' ') == StringRef::npos) |
| 169 | return S; |
| 170 | return ("\"" + S + "\"").str(); |
| 171 | } |
| 172 | |
| Rui Ueyama | 2796ae3 | 2016-04-30 22:46:47 +0000 | [diff] [blame] | 173 | static std::string rewritePath(StringRef S) { |
| 174 | if (fs::exists(S)) |
| Rafael Espindola | e85bcbd | 2016-05-02 14:12:35 +0000 | [diff] [blame] | 175 | return relativeToRoot(S); |
| Rui Ueyama | 2796ae3 | 2016-04-30 22:46:47 +0000 | [diff] [blame] | 176 | return S; |
| 177 | } |
| 178 | |
| Rui Ueyama | af2312f | 2016-05-04 23:12:55 +0000 | [diff] [blame] | 179 | static std::string stringize(opt::Arg *Arg) { |
| 180 | std::string K = Arg->getSpelling(); |
| 181 | if (Arg->getNumValues() == 0) |
| 182 | return K; |
| 183 | std::string V = quote(Arg->getValue()); |
| 184 | if (Arg->getOption().getRenderStyle() == opt::Option::RenderJoinedStyle) |
| 185 | return K + V; |
| 186 | return K + " " + V; |
| 187 | } |
| 188 | |
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 189 | // Copies all input files to Config->Reproduce directory and |
| 190 | // create a response file as "response.txt", so that you can re-run |
| 191 | // the same command with the same inputs just by executing |
| 192 | // "ld.lld @response.txt". Used by --reproduce. This feature is |
| 193 | // supposed to be used by users to report an issue to LLD developers. |
| Rui Ueyama | 5002a67 | 2016-05-02 19:59:56 +0000 | [diff] [blame] | 194 | void elf::createResponseFile(const opt::InputArgList &Args) { |
| Rafael Espindola | 1dd2b3d | 2016-05-03 17:30:44 +0000 | [diff] [blame] | 195 | SmallString<0> Data; |
| 196 | raw_svector_ostream OS(Data); |
| Rui Ueyama | 2796ae3 | 2016-04-30 22:46:47 +0000 | [diff] [blame] | 197 | // Copy the command line to response.txt while rewriting paths. |
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 198 | for (auto *Arg : Args) { |
| 199 | switch (Arg->getOption().getID()) { |
| 200 | case OPT_reproduce: |
| 201 | break; |
| Rui Ueyama | 2796ae3 | 2016-04-30 22:46:47 +0000 | [diff] [blame] | 202 | case OPT_INPUT: |
| 203 | OS << quote(rewritePath(Arg->getValue())) << "\n"; |
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 204 | break; |
| Rui Ueyama | 2796ae3 | 2016-04-30 22:46:47 +0000 | [diff] [blame] | 205 | case OPT_L: |
| 206 | case OPT_dynamic_list: |
| Rui Ueyama | 2796ae3 | 2016-04-30 22:46:47 +0000 | [diff] [blame] | 207 | case OPT_rpath: |
| 208 | case OPT_script: |
| 209 | case OPT_version_script: |
| 210 | OS << Arg->getSpelling() << " " |
| 211 | << quote(rewritePath(Arg->getValue())) << "\n"; |
| 212 | break; |
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 213 | default: |
| Rui Ueyama | af2312f | 2016-05-04 23:12:55 +0000 | [diff] [blame] | 214 | OS << stringize(Arg) << "\n"; |
| Rui Ueyama | aa00e96 | 2016-04-30 21:40:04 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| Rafael Espindola | 1dd2b3d | 2016-05-03 17:30:44 +0000 | [diff] [blame] | 217 | |
| 218 | SmallString<128> Dest; |
| 219 | path::append(Dest, path::filename(Config->Reproduce), "response.txt"); |
| 220 | maybePrintCpioMember(Dest, Data); |
| Rui Ueyama | cf0dd1e | 2016-04-26 20:41:32 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 223 | std::string elf::findFromSearchPaths(StringRef Path) { |
| Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 224 | for (StringRef Dir : Config->SearchPaths) { |
| 225 | std::string FullPath = buildSysrootedPath(Dir, Path); |
| Rui Ueyama | fc28827 | 2016-05-02 18:51:08 +0000 | [diff] [blame] | 226 | if (fs::exists(FullPath)) |
| Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 227 | return FullPath; |
| 228 | } |
| 229 | return ""; |
| 230 | } |
| 231 | |
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 232 | // Searches a given library from input search paths, which are filled |
| 233 | // 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] | 234 | std::string elf::searchLibrary(StringRef Path) { |
| Rui Ueyama | 319d730 | 2016-03-29 20:53:21 +0000 | [diff] [blame] | 235 | if (Path.startswith(":")) |
| 236 | return findFromSearchPaths(Path.substr(1)); |
| 237 | if (!Config->Static) { |
| 238 | std::string S = findFromSearchPaths(("lib" + Path + ".so").str()); |
| Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 239 | if (!S.empty()) |
| 240 | return S; |
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 241 | } |
| Rui Ueyama | 319d730 | 2016-03-29 20:53:21 +0000 | [diff] [blame] | 242 | return findFromSearchPaths(("lib" + Path + ".a").str()); |
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Makes a path by concatenating Dir and File. |
| 246 | // If Dir starts with '=' the result will be preceded by Sysroot, |
| 247 | // which can be set with --sysroot command line switch. |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 248 | std::string elf::buildSysrootedPath(StringRef Dir, StringRef File) { |
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 249 | SmallString<128> Path; |
| 250 | if (Dir.startswith("=")) |
| Rui Ueyama | fc28827 | 2016-05-02 18:51:08 +0000 | [diff] [blame] | 251 | path::append(Path, Config->Sysroot, Dir.substr(1), File); |
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 252 | else |
| Rui Ueyama | fc28827 | 2016-05-02 18:51:08 +0000 | [diff] [blame] | 253 | path::append(Path, Dir, File); |
| Rui Ueyama | 4b02ca9 | 2015-10-11 03:28:39 +0000 | [diff] [blame] | 254 | return Path.str(); |
| 255 | } |