[ELF2] Add --sysroot command line switch

Reviewers: rafael, ruiu

Subscribers: llvm-commits

Projects: #lld

Differential Revision: http://reviews.llvm.org/D13209

llvm-svn: 248715
This commit is contained in:
Igor Kudrin 2015-09-28 15:01:59 +00:00
parent ba52fcb7d5
commit 1309fc0378
4 changed files with 61 additions and 5 deletions

View File

@ -22,6 +22,7 @@ struct Configuration {
llvm::StringRef DynamicLinker;
std::string RPath;
std::vector<llvm::StringRef> InputSearchPaths;
llvm::StringRef Sysroot;
bool Shared = false;
bool DiscardAll = false;
bool DiscardLocals = false;

View File

@ -60,6 +60,21 @@ static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) {
return createELFFile<ObjectFile>(MB);
}
// 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.
static std::string buildSysrootedPath(StringRef Dir, StringRef File) {
SmallString<128> Path;
if (Dir.startswith("=")) {
Path.assign(Config->Sysroot);
sys::path::append(Path, Dir.substr(1), File);
} else {
Path.assign(Dir);
sys::path::append(Path, File);
}
return Path.str().str();
}
// Searches a given library from input search paths, which are filled
// from -L command line switches. Returns a path to an existent library file.
static std::string searchLibrary(StringRef Path) {
@ -70,13 +85,11 @@ static std::string searchLibrary(StringRef Path) {
Names.push_back((Twine("lib") + Path + ".so").str());
Names.push_back((Twine("lib") + Path + ".a").str());
}
SmallString<128> FullPath;
for (StringRef Dir : Config->InputSearchPaths) {
for (const std::string &Name : Names) {
FullPath = Dir;
sys::path::append(FullPath, Name);
if (sys::fs::exists(FullPath.str()))
return FullPath.str().str();
std::string FullPath = buildSysrootedPath(Dir, Name);
if (sys::fs::exists(FullPath))
return FullPath;
}
}
error(Twine("Unable to find library -l") + Path);
@ -96,6 +109,9 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
if (auto *Arg = Args.getLastArg(OPT_dynamic_linker))
Config->DynamicLinker = Arg->getValue();
if (auto *Arg = Args.getLastArg(OPT_sysroot))
Config->Sysroot = Arg->getValue();
std::vector<StringRef> RPaths;
for (auto *Arg : Args.filtered(OPT_rpath))
RPaths.push_back(Arg->getValue());

View File

@ -49,3 +49,6 @@ def l : Joined<["-"], "l">, MetaVarName<"<libName>">,
def alias_l : Joined<["--"], "library=">,
Alias<l>;
def sysroot : Joined<["--"], "sysroot=">,
HelpText<"Set the system root">;

36
lld/test/elf2/sysroot.s Normal file
View File

@ -0,0 +1,36 @@
// RUN: mkdir -p %t/lib
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t/m.o
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
// RUN: %p/Inputs/libsearch-st.s -o %t/st.o
// RUN: rm -f %t/lib/libls.a
// RUN: llvm-ar rcs %t/lib/libls.a %t/st.o
// REQUIRES: x86
// Should not link because of undefined symbol _bar
// RUN: not lld -flavor gnu2 -o %t/r %t/m.o 2>&1 \
// RUN: | FileCheck --check-prefix=UNDEFINED %s
// UNDEFINED: undefined symbol: _bar
// We need to be sure that there is no suitable library in the /lib directory
// RUN: not lld -flavor gnu2 -o %t/r %t/m.o -L/lib -l:libls.a 2>&1 \
// RUN: | FileCheck --check-prefix=NOLIB %s
// NOLIB: Unable to find library -l:libls.a
// Should just remove the '=' symbol if --sysroot is not specified.
// Case 1: relative path
// RUN: cd %t && lld -flavor gnu2 -o %t/r %t/m.o -L=lib -l:libls.a
// Case 2: absolute path
// RUN: cd %p && lld -flavor gnu2 -o %t/r %t/m.o -L=%t/lib -l:libls.a
// RUN: cd %p
// Should substitute SysRoot if specified
// RUN: lld -flavor gnu2 -o %t/r %t/m.o --sysroot=%t -L=lib -l:libls.a
// RUN: lld -flavor gnu2 -o %t/r %t/m.o --sysroot=%t -L=/lib -l:libls.a
// Should not substitute SysRoot if the directory name does not start with '='
// RUN: not lld -flavor gnu2 -o %t/r %r/m.o --sysroot=%t -Llib -l:libls.a
// RUN: not lld -flavor gnu2 -o %t/r %r/m.o --sysroot=%t -L/lib -l:libls.a
.globl _start,_bar;
_start: