2015-07-25 05:03:07 +08:00
|
|
|
//===- DriverUtils.cpp ----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains utility functions for the driver. Because there
|
|
|
|
// are so many small functions, we created this separate file to make
|
|
|
|
// Driver.cpp less cluttered.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Driver.h"
|
2015-08-06 23:08:23 +08:00
|
|
|
#include "Error.h"
|
2016-02-28 11:18:07 +08:00
|
|
|
#include "lld/Config/Version.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-05-05 07:12:55 +08:00
|
|
|
#include "llvm/Option/Option.h"
|
2015-09-25 23:37:33 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2015-10-11 11:28:39 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2015-10-12 02:19:01 +08:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2016-04-27 04:41:32 +08:00
|
|
|
using namespace llvm::sys;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace lld;
|
2016-02-28 08:25:54 +08:00
|
|
|
using namespace lld::elf;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
// Create OptTable
|
|
|
|
|
|
|
|
// Create prefix string literals used in Options.td
|
|
|
|
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef PREFIX
|
|
|
|
|
|
|
|
// Create table mapping all options defined in Options.td
|
2016-04-03 03:15:26 +08:00
|
|
|
static const opt::OptTable::Info OptInfo[] = {
|
2015-07-25 05:03:07 +08:00
|
|
|
#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \
|
|
|
|
{ \
|
|
|
|
X1, X2, X9, X10, OPT_##ID, opt::Option::KIND##Class, X8, X7, OPT_##GROUP, \
|
|
|
|
OPT_##ALIAS, X6 \
|
2016-04-03 03:15:26 +08:00
|
|
|
},
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
2016-04-03 03:15:26 +08:00
|
|
|
ELFOptTable::ELFOptTable() : OptTable(OptInfo) {}
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
// Parses a given list of options.
|
2016-03-16 02:20:50 +08:00
|
|
|
opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> Argv) {
|
2015-07-25 05:03:07 +08:00
|
|
|
// Make InputArgList from string vectors.
|
|
|
|
unsigned MissingIndex;
|
|
|
|
unsigned MissingCount;
|
|
|
|
|
2015-09-25 23:37:33 +08:00
|
|
|
// Expand response files. '@<filename>' is replaced by the file's contents.
|
|
|
|
SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
|
2016-03-16 02:20:50 +08:00
|
|
|
StringSaver Saver(Alloc);
|
2016-05-03 03:59:56 +08:00
|
|
|
cl::ExpandResponseFiles(Saver, cl::TokenizeGNUCommandLine, Vec);
|
2015-09-25 23:37:33 +08:00
|
|
|
|
|
|
|
// Parse options and then do error checking.
|
2016-03-16 02:20:50 +08:00
|
|
|
opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
|
2015-07-25 05:03:07 +08:00
|
|
|
if (MissingCount)
|
2016-03-11 22:43:02 +08:00
|
|
|
error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) +
|
2015-07-25 05:03:07 +08:00
|
|
|
"\", expected " + Twine(MissingCount) +
|
|
|
|
(MissingCount == 1 ? " argument.\n" : " arguments"));
|
2015-09-25 02:55:33 +08:00
|
|
|
|
|
|
|
iterator_range<opt::arg_iterator> Unknowns = Args.filtered(OPT_UNKNOWN);
|
|
|
|
for (auto *Arg : Unknowns)
|
2016-03-11 22:43:02 +08:00
|
|
|
warning("warning: unknown argument: " + Arg->getSpelling());
|
2015-09-25 02:55:33 +08:00
|
|
|
if (Unknowns.begin() != Unknowns.end())
|
2016-03-11 22:43:02 +08:00
|
|
|
error("unknown argument(s) found");
|
2015-07-25 05:03:07 +08:00
|
|
|
return Args;
|
|
|
|
}
|
2015-10-11 11:28:39 +08:00
|
|
|
|
2016-02-28 11:18:09 +08:00
|
|
|
void elf::printHelp(const char *Argv0) {
|
|
|
|
ELFOptTable Table;
|
|
|
|
Table.PrintHelp(outs(), Argv0, "lld", false);
|
|
|
|
}
|
|
|
|
|
2016-02-28 11:18:07 +08:00
|
|
|
void elf::printVersion() {
|
|
|
|
outs() << "LLD " << getLLDVersion();
|
|
|
|
std::string S = getLLDRepositoryVersion();
|
|
|
|
if (!S.empty())
|
2016-03-14 07:07:42 +08:00
|
|
|
outs() << " " << S;
|
|
|
|
outs() << "\n";
|
2016-02-28 11:18:07 +08:00
|
|
|
}
|
|
|
|
|
2016-05-14 05:55:56 +08:00
|
|
|
// Converts a hex string (e.g. "0x123456") to a vector.
|
|
|
|
std::vector<uint8_t> elf::parseHexstring(StringRef S) {
|
|
|
|
if (S.find_first_not_of("0123456789abcdefABCDEF") != StringRef::npos ||
|
|
|
|
S.size() % 2) {
|
|
|
|
error("malformed hexstring: " + S);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::vector<uint8_t> V;
|
|
|
|
for (; !S.empty(); S = S.substr(2)) {
|
|
|
|
int I;
|
|
|
|
S.substr(0, 2).getAsInteger(16, I);
|
|
|
|
V.push_back(I);
|
|
|
|
}
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2016-05-01 05:40:04 +08:00
|
|
|
// Makes a given pathname an absolute path first, and then remove
|
|
|
|
// beginning /. For example, "../foo.o" is converted to "home/john/foo.o",
|
|
|
|
// assuming that the current directory is "/home/john/bar".
|
2016-05-01 06:23:29 +08:00
|
|
|
static std::string relativeToRoot(StringRef Path) {
|
2016-05-01 05:40:04 +08:00
|
|
|
SmallString<128> Abs = Path;
|
|
|
|
if (std::error_code EC = fs::make_absolute(Abs))
|
|
|
|
fatal("make_absolute failed: " + EC.message());
|
|
|
|
path::remove_dots(Abs, /*remove_dot_dot=*/true);
|
|
|
|
|
|
|
|
// This is Windows specific. root_name() returns a drive letter
|
|
|
|
// (e.g. "c:") or a UNC name (//net). We want to keep it as part
|
|
|
|
// of the result.
|
2016-04-27 04:41:32 +08:00
|
|
|
SmallString<128> Res;
|
2016-05-03 02:16:14 +08:00
|
|
|
StringRef Root = path::root_name(Abs);
|
2016-05-01 06:20:27 +08:00
|
|
|
if (Root.endswith(":"))
|
2016-05-01 05:40:04 +08:00
|
|
|
Res = Root.drop_back();
|
2016-05-01 06:20:27 +08:00
|
|
|
else if (Root.startswith("//"))
|
2016-05-01 05:40:04 +08:00
|
|
|
Res = Root.substr(2);
|
|
|
|
|
|
|
|
path::append(Res, path::relative_path(Abs));
|
2016-04-27 04:41:32 +08:00
|
|
|
return Res.str();
|
|
|
|
}
|
|
|
|
|
2016-05-01 06:23:29 +08:00
|
|
|
static std::string getDestPath(StringRef Path) {
|
|
|
|
std::string Relpath = relativeToRoot(Path);
|
2016-05-01 05:40:04 +08:00
|
|
|
SmallString<128> Dest;
|
2016-05-04 01:30:44 +08:00
|
|
|
path::append(Dest, path::filename(Config->Reproduce), Relpath);
|
2016-05-01 06:23:29 +08:00
|
|
|
return Dest.str();
|
|
|
|
}
|
2016-05-01 05:40:04 +08:00
|
|
|
|
2016-05-04 20:47:56 +08:00
|
|
|
static void maybePrintCpioMemberAux(raw_fd_ostream &OS, StringRef Path,
|
|
|
|
StringRef Data) {
|
2016-05-04 01:30:44 +08:00
|
|
|
OS << "070707"; // c_magic
|
|
|
|
|
|
|
|
// The c_dev/c_ino pair should be unique according to the spec, but no one
|
|
|
|
// seems to care.
|
|
|
|
OS << "000000"; // c_dev
|
|
|
|
OS << "000000"; // c_ino
|
|
|
|
|
|
|
|
OS << "100664"; // c_mode: C_ISREG | rw-rw-r--
|
|
|
|
OS << "000000"; // c_uid
|
|
|
|
OS << "000000"; // c_gid
|
|
|
|
OS << "000001"; // c_nlink
|
|
|
|
OS << "000000"; // c_rdev
|
|
|
|
OS << "00000000000"; // c_mtime
|
|
|
|
OS << format("%06o", Path.size() + 1); // c_namesize
|
|
|
|
OS << format("%011o", Data.size()); // c_filesize
|
|
|
|
OS << Path << '\0'; // c_name
|
|
|
|
OS << Data; // c_filedata
|
|
|
|
}
|
|
|
|
|
2016-05-04 20:47:56 +08:00
|
|
|
static void maybePrintCpioMember(StringRef Path, StringRef Data) {
|
|
|
|
if (Config->Reproduce.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!Driver->IncludedFiles.insert(Path).second)
|
|
|
|
return;
|
|
|
|
raw_fd_ostream &OS = *Driver->ReproduceArchive;
|
|
|
|
maybePrintCpioMemberAux(OS, Path, Data);
|
|
|
|
|
|
|
|
// Print the trailer and seek back. This way we have a valid archive if we
|
|
|
|
// crash.
|
2016-05-11 03:26:55 +08:00
|
|
|
// See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_11
|
|
|
|
// for the format details.
|
2016-05-04 20:47:56 +08:00
|
|
|
uint64_t Pos = OS.tell();
|
|
|
|
maybePrintCpioMemberAux(OS, "TRAILER!!!", "");
|
|
|
|
OS.seek(Pos);
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:30:44 +08:00
|
|
|
// Write file Src with content Data to the archive.
|
|
|
|
void elf::maybeCopyInputFile(StringRef Src, StringRef Data) {
|
2016-05-01 06:23:29 +08:00
|
|
|
std::string Dest = getDestPath(Src);
|
2016-05-04 01:30:44 +08:00
|
|
|
maybePrintCpioMember(Dest, Data);
|
|
|
|
}
|
|
|
|
|
2016-05-01 05:40:04 +08:00
|
|
|
// Quote a given string if it contains a space character.
|
|
|
|
static std::string quote(StringRef S) {
|
|
|
|
if (S.find(' ') == StringRef::npos)
|
|
|
|
return S;
|
|
|
|
return ("\"" + S + "\"").str();
|
|
|
|
}
|
|
|
|
|
2016-05-01 06:46:47 +08:00
|
|
|
static std::string rewritePath(StringRef S) {
|
|
|
|
if (fs::exists(S))
|
2016-05-02 22:12:35 +08:00
|
|
|
return relativeToRoot(S);
|
2016-05-01 06:46:47 +08:00
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2016-05-05 07:12:55 +08:00
|
|
|
static std::string stringize(opt::Arg *Arg) {
|
|
|
|
std::string K = Arg->getSpelling();
|
|
|
|
if (Arg->getNumValues() == 0)
|
|
|
|
return K;
|
|
|
|
std::string V = quote(Arg->getValue());
|
|
|
|
if (Arg->getOption().getRenderStyle() == opt::Option::RenderJoinedStyle)
|
|
|
|
return K + V;
|
|
|
|
return K + " " + V;
|
|
|
|
}
|
|
|
|
|
2016-05-01 05:40:04 +08:00
|
|
|
// Copies all input files to Config->Reproduce directory and
|
|
|
|
// create a response file as "response.txt", so that you can re-run
|
|
|
|
// the same command with the same inputs just by executing
|
|
|
|
// "ld.lld @response.txt". Used by --reproduce. This feature is
|
|
|
|
// supposed to be used by users to report an issue to LLD developers.
|
2016-05-03 03:59:56 +08:00
|
|
|
void elf::createResponseFile(const opt::InputArgList &Args) {
|
2016-05-04 01:30:44 +08:00
|
|
|
SmallString<0> Data;
|
|
|
|
raw_svector_ostream OS(Data);
|
2016-05-01 06:46:47 +08:00
|
|
|
// Copy the command line to response.txt while rewriting paths.
|
2016-05-01 05:40:04 +08:00
|
|
|
for (auto *Arg : Args) {
|
|
|
|
switch (Arg->getOption().getID()) {
|
|
|
|
case OPT_reproduce:
|
|
|
|
break;
|
2016-05-01 06:46:47 +08:00
|
|
|
case OPT_INPUT:
|
|
|
|
OS << quote(rewritePath(Arg->getValue())) << "\n";
|
|
|
|
break;
|
|
|
|
case OPT_L:
|
|
|
|
case OPT_dynamic_list:
|
|
|
|
case OPT_rpath:
|
2016-05-01 05:40:04 +08:00
|
|
|
case OPT_script:
|
2016-05-01 06:46:47 +08:00
|
|
|
case OPT_version_script:
|
|
|
|
OS << Arg->getSpelling() << " "
|
|
|
|
<< quote(rewritePath(Arg->getValue())) << "\n";
|
2016-05-01 05:40:04 +08:00
|
|
|
break;
|
|
|
|
default:
|
2016-05-05 07:12:55 +08:00
|
|
|
OS << stringize(Arg) << "\n";
|
2016-05-01 05:40:04 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-04 01:30:44 +08:00
|
|
|
|
|
|
|
SmallString<128> Dest;
|
|
|
|
path::append(Dest, path::filename(Config->Reproduce), "response.txt");
|
|
|
|
maybePrintCpioMember(Dest, Data);
|
2016-04-27 04:41:32 +08:00
|
|
|
}
|
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
std::string elf::findFromSearchPaths(StringRef Path) {
|
2015-10-11 11:28:42 +08:00
|
|
|
for (StringRef Dir : Config->SearchPaths) {
|
|
|
|
std::string FullPath = buildSysrootedPath(Dir, Path);
|
2016-05-03 02:51:08 +08:00
|
|
|
if (fs::exists(FullPath))
|
2015-10-11 11:28:42 +08:00
|
|
|
return FullPath;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-10-11 11:28:39 +08:00
|
|
|
// Searches a given library from input search paths, which are filled
|
|
|
|
// from -L command line switches. Returns a path to an existent library file.
|
2016-02-28 08:25:54 +08:00
|
|
|
std::string elf::searchLibrary(StringRef Path) {
|
2016-03-30 04:53:21 +08:00
|
|
|
if (Path.startswith(":"))
|
|
|
|
return findFromSearchPaths(Path.substr(1));
|
|
|
|
if (!Config->Static) {
|
|
|
|
std::string S = findFromSearchPaths(("lib" + Path + ".so").str());
|
2015-10-11 11:28:42 +08:00
|
|
|
if (!S.empty())
|
|
|
|
return S;
|
2015-10-11 11:28:39 +08:00
|
|
|
}
|
2016-03-30 04:53:21 +08:00
|
|
|
return findFromSearchPaths(("lib" + Path + ".a").str());
|
2015-10-11 11:28:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Makes a path by concatenating Dir and File.
|
|
|
|
// If Dir starts with '=' the result will be preceded by Sysroot,
|
|
|
|
// which can be set with --sysroot command line switch.
|
2016-02-28 08:25:54 +08:00
|
|
|
std::string elf::buildSysrootedPath(StringRef Dir, StringRef File) {
|
2015-10-11 11:28:39 +08:00
|
|
|
SmallString<128> Path;
|
|
|
|
if (Dir.startswith("="))
|
2016-05-03 02:51:08 +08:00
|
|
|
path::append(Path, Config->Sysroot, Dir.substr(1), File);
|
2015-10-11 11:28:39 +08:00
|
|
|
else
|
2016-05-03 02:51:08 +08:00
|
|
|
path::append(Path, Dir, File);
|
2015-10-11 11:28:39 +08:00
|
|
|
return Path.str();
|
|
|
|
}
|