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"
|
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);
|
2015-09-25 23:37:33 +08:00
|
|
|
llvm::cl::ExpandResponseFiles(Saver, llvm::cl::TokenizeGNUCommandLine, Vec);
|
|
|
|
|
|
|
|
// 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-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-01 05:40:04 +08:00
|
|
|
StringRef Root = path::root_name(Path);
|
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;
|
|
|
|
path::append(Dest, Config->Reproduce, Relpath);
|
2016-05-01 06:23:29 +08:00
|
|
|
return Dest.str();
|
|
|
|
}
|
2016-05-01 05:40:04 +08:00
|
|
|
|
2016-05-01 06:23:29 +08:00
|
|
|
// Copies file Src to {Config->Reproduce}/Src.
|
|
|
|
void elf::copyInputFile(StringRef Src) {
|
|
|
|
std::string Dest = getDestPath(Src);
|
2016-04-27 04:41:32 +08:00
|
|
|
SmallString<128> Dir(Dest);
|
|
|
|
path::remove_filename(Dir);
|
2016-05-01 06:23:29 +08:00
|
|
|
if (std::error_code EC = sys::fs::create_directories(Dir)) {
|
2016-04-27 04:41:32 +08:00
|
|
|
error(EC, Dir + ": can't create directory");
|
2016-05-01 06:23:29 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-04-27 04:41:32 +08:00
|
|
|
if (std::error_code EC = sys::fs::copy_file(Src, Dest))
|
|
|
|
error(EC, "failed to copy file: " + Dest);
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-01 06:23:29 +08:00
|
|
|
void elf::createResponseFile(const llvm::opt::InputArgList &Args) {
|
2016-05-01 05:40:04 +08:00
|
|
|
// Create the output directory.
|
|
|
|
if (std::error_code EC = sys::fs::create_directories(
|
|
|
|
Config->Reproduce, /*IgnoreExisting=*/false)) {
|
|
|
|
error(EC, Config->Reproduce + ": can't create directory");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open "response.txt".
|
|
|
|
SmallString<128> Path;
|
|
|
|
path::append(Path, Config->Reproduce, "response.txt");
|
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
|
|
|
|
check(EC);
|
|
|
|
|
|
|
|
// Dump the command line to response.txt while copying files
|
|
|
|
// and rewriting paths.
|
|
|
|
for (auto *Arg : Args) {
|
|
|
|
switch (Arg->getOption().getID()) {
|
|
|
|
case OPT_reproduce:
|
|
|
|
break;
|
|
|
|
case OPT_script:
|
|
|
|
OS << "--script ";
|
|
|
|
// fallthrough
|
|
|
|
case OPT_INPUT: {
|
|
|
|
StringRef Path = Arg->getValue();
|
|
|
|
if (fs::exists(Path))
|
2016-05-01 06:23:29 +08:00
|
|
|
OS << quote(getDestPath(Path)) << "\n";
|
2016-05-01 05:40:04 +08:00
|
|
|
else
|
|
|
|
OS << quote(Path) << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
OS << Arg->getAsString(Args) << "\n";
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
if (sys::fs::exists(FullPath))
|
|
|
|
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("="))
|
|
|
|
sys::path::append(Path, Config->Sysroot, Dir.substr(1), File);
|
|
|
|
else
|
|
|
|
sys::path::append(Path, Dir, File);
|
|
|
|
return Path.str();
|
|
|
|
}
|