From c60f85d0731f45a8bcb6f6c14409a33cf88d15af Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Wed, 25 Jul 2018 21:53:18 +0000 Subject: [PATCH] [ELF] Use search paths for --version-script= Summary: This behavior matches ld.bfd -Ld --version-script=t.script a.o Reviewers: ruiu, espindola Subscribers: emaste, arichardson, llvm-commits Differential Revision: https://reviews.llvm.org/D49820 llvm-svn: 337969 --- lld/ELF/Driver.cpp | 10 +++++++--- lld/ELF/Driver.h | 2 +- lld/ELF/DriverUtils.cpp | 8 ++++---- lld/ELF/ScriptParser.cpp | 2 +- lld/test/ELF/version-script-in-search-path.s | 10 ++++++++++ 5 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 lld/test/ELF/version-script-in-search-path.s diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 15714534eeb0..1fc552f011b6 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -939,8 +939,12 @@ void LinkerDriver::readConfigs(opt::InputArgList &Args) { Config->Undefined.push_back(Arg->getValue()); for (auto *Arg : Args.filtered(OPT_version_script)) - if (Optional Buffer = readFile(Arg->getValue())) - readVersionScript(*Buffer); + if (Optional Path = searchScript(Arg->getValue())) { + if (Optional Buffer = readFile(*Path)) + readVersionScript(*Buffer); + } else { + error(Twine("cannot find version script ") + Arg->getValue()); + } } // Some Config members do not directly correspond to any particular @@ -1022,7 +1026,7 @@ void LinkerDriver::createFiles(opt::InputArgList &Args) { break; } case OPT_script: - if (Optional Path = searchLinkerScript(Arg->getValue())) { + if (Optional Path = searchScript(Arg->getValue())) { if (Optional MB = readFile(*Path)) readLinkerScript(*MB); break; diff --git a/lld/ELF/Driver.h b/lld/ELF/Driver.h index 3e4e13085324..99e194d9b66c 100644 --- a/lld/ELF/Driver.h +++ b/lld/ELF/Driver.h @@ -67,7 +67,7 @@ void printHelp(); std::string createResponseFile(const llvm::opt::InputArgList &Args); llvm::Optional findFromSearchPaths(StringRef Path); -llvm::Optional searchLinkerScript(StringRef Path); +llvm::Optional searchScript(StringRef Path); llvm::Optional searchLibrary(StringRef Path); } // namespace elf diff --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp index 35e3ba19f5b1..698e06edfe63 100644 --- a/lld/ELF/DriverUtils.cpp +++ b/lld/ELF/DriverUtils.cpp @@ -227,10 +227,10 @@ Optional elf::searchLibrary(StringRef Name) { return None; } -// If a linker script doesn't exist in the current directory, we also look for -// the script in the '-L' search paths. This matches the behaviour of both '-T' -// and linker script INPUT() directives in ld.bfd. -Optional elf::searchLinkerScript(StringRef Name) { +// If a linker/version script doesn't exist in the current directory, we also +// look for the script in the '-L' search paths. This matches the behaviour of +// '-T', --version-script=, and linker script INPUT() command in ld.bfd. +Optional elf::searchScript(StringRef Name) { if (fs::exists(Name)) return Name.str(); return findFromSearchPaths(Name); diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp index b0982f57bc50..ddb4a49a3e5e 100644 --- a/lld/ELF/ScriptParser.cpp +++ b/lld/ELF/ScriptParser.cpp @@ -344,7 +344,7 @@ void ScriptParser::readInclude() { return; } - if (Optional Path = searchLinkerScript(Tok)) { + if (Optional Path = searchScript(Tok)) { if (Optional MB = readFile(*Path)) tokenize(*MB); return; diff --git a/lld/test/ELF/version-script-in-search-path.s b/lld/test/ELF/version-script-in-search-path.s new file mode 100644 index 000000000000..733890c50af6 --- /dev/null +++ b/lld/test/ELF/version-script-in-search-path.s @@ -0,0 +1,10 @@ +# REQUIRES: x86 +# Check that we fall back to search paths if a version script was not found +# This behaviour matches ld.bfd. + +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: mkdir -p %T/searchpath +# RUN: echo '{};' > %T/searchpath/t.script +# RUN: ld.lld -L%T/searchpath --version-script=t.script %t.o -o /dev/null +# RUN: not ld.lld --version-script=t.script %t.o 2>&1 | FileCheck -check-prefix ERROR %s +# ERROR: error: cannot find version script