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-06-06 23:34:37 +08:00
|
|
|
std::string elf::getVersionString() {
|
|
|
|
std::string Version = getLLDVersion();
|
|
|
|
std::string Repo = getLLDRepositoryVersion();
|
|
|
|
if (Repo.empty())
|
|
|
|
return "LLD " + Version + "\n";
|
|
|
|
return "LLD " + Version + " " + Repo + "\n";
|
2016-02-28 11:18:07 +08:00
|
|
|
}
|
|
|
|
|
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-16 01:10:23 +08:00
|
|
|
std::string elf::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-16 01:10:23 +08:00
|
|
|
CpioFile::CpioFile(std::unique_ptr<llvm::raw_fd_ostream> OS, StringRef S)
|
|
|
|
: OS(std::move(OS)), Basename(S) {}
|
2016-05-04 01:30:44 +08:00
|
|
|
|
2016-05-16 01:10:23 +08:00
|
|
|
CpioFile *CpioFile::create(StringRef OutputPath) {
|
|
|
|
std::string Path = (OutputPath + ".cpio").str();
|
|
|
|
std::error_code EC;
|
|
|
|
auto OS = llvm::make_unique<raw_fd_ostream>(Path, EC, fs::F_None);
|
|
|
|
if (EC) {
|
|
|
|
error(EC, "--reproduce: failed to open " + Path);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return new CpioFile(std::move(OS), path::filename(OutputPath));
|
|
|
|
}
|
2016-05-04 01:30:44 +08:00
|
|
|
|
2016-05-16 01:10:23 +08:00
|
|
|
static void writeMember(raw_fd_ostream &OS, StringRef Path, StringRef Data) {
|
|
|
|
// The c_dev/c_ino pair should be unique according to the spec,
|
|
|
|
// but no one seems to care.
|
|
|
|
OS << "070707"; // c_magic
|
|
|
|
OS << "000000"; // c_dev
|
|
|
|
OS << "000000"; // c_ino
|
2016-05-04 01:30:44 +08:00
|
|
|
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-16 01:10:23 +08:00
|
|
|
void CpioFile::append(StringRef Path, StringRef Data) {
|
|
|
|
if (!Seen.insert(Path).second)
|
2016-05-04 20:47:56 +08:00
|
|
|
return;
|
|
|
|
|
2016-05-16 01:10:23 +08:00
|
|
|
// Construct an in-archive filename so that /home/foo/bar is stored
|
|
|
|
// as baz/home/foo/bar where baz is the basename of the output file.
|
|
|
|
// (i.e. in that case we are creating baz.cpio.)
|
|
|
|
SmallString<128> Fullpath;
|
|
|
|
path::append(Fullpath, Basename, Path);
|
2016-07-06 20:09:51 +08:00
|
|
|
|
|
|
|
// Use unix path separators so the cpio can be extracted on both unix and
|
|
|
|
// windows.
|
|
|
|
std::replace(Fullpath.begin(), Fullpath.end(), '\\', '/');
|
|
|
|
|
2016-05-16 01:10:23 +08:00
|
|
|
writeMember(*OS, Fullpath, Data);
|
|
|
|
|
|
|
|
// Print the trailer and seek back.
|
|
|
|
// This way we have a valid archive if we crash.
|
|
|
|
uint64_t Pos = OS->tell();
|
|
|
|
writeMember(*OS, "TRAILER!!!", "");
|
|
|
|
OS->seek(Pos);
|
2016-05-04 01:30:44 +08:00
|
|
|
}
|
|
|
|
|
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-16 01:10:23 +08:00
|
|
|
// Reconstructs command line arguments so that so that you can re-run
|
|
|
|
// the same command with the same inputs. This is for --reproduce.
|
|
|
|
std::string elf::createResponseFile(const opt::InputArgList &Args) {
|
2016-05-04 01:30:44 +08:00
|
|
|
SmallString<0> Data;
|
|
|
|
raw_svector_ostream OS(Data);
|
2016-05-16 01:10:23 +08:00
|
|
|
|
|
|
|
// Copy the command line to the output 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-06-01 14:17:27 +08:00
|
|
|
case OPT_alias_script_T:
|
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-16 01:10:23 +08:00
|
|
|
return Data.str();
|
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));
|
2016-06-27 15:26:28 +08:00
|
|
|
for (StringRef Dir : Config->SearchPaths) {
|
|
|
|
if (!Config->Static) {
|
|
|
|
std::string S = buildSysrootedPath(Dir, ("lib" + Path + ".so").str());
|
|
|
|
if (fs::exists(S))
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
std::string S = buildSysrootedPath(Dir, ("lib" + Path + ".a").str());
|
|
|
|
if (fs::exists(S))
|
2015-10-11 11:28:42 +08:00
|
|
|
return S;
|
2015-10-11 11:28:39 +08:00
|
|
|
}
|
2016-06-27 15:26:28 +08:00
|
|
|
return "";
|
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();
|
|
|
|
}
|