Index: ELF/Driver.h =================================================================== --- ELF/Driver.h +++ ELF/Driver.h @@ -67,6 +67,7 @@ std::string createResponseFile(const llvm::opt::InputArgList &Args); llvm::Optional findFromSearchPaths(StringRef Path); +llvm::Optional searchLinkerScript(StringRef Path); llvm::Optional searchLibrary(StringRef Path); } // namespace elf Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -862,8 +862,12 @@ addFile(Arg->getValue(), /*WithLOption=*/false); break; case OPT_script: - if (Optional MB = readFile(Arg->getValue())) - readLinkerScript(*MB); + if (Optional Path = searchLinkerScript(Arg->getValue())) { + if (Optional MB = readFile(*Path)) + readLinkerScript(*MB); + } else { + error(Twine("cannot find linker script ") + Arg->getValue()); + } break; case OPT_as_needed: Config->AsNeeded = true; Index: ELF/DriverUtils.cpp =================================================================== --- ELF/DriverUtils.cpp +++ ELF/DriverUtils.cpp @@ -207,3 +207,24 @@ } return None; } + +// If a linker script doesn't exist, we fall back to the search paths to +// match the behaviour of ld.bfd. This is the behaviour both for '-T' and +// linker script INPUT() directives in ld.bfd: +// +// https://sourceware.org/binutils/docs/ld/File-Commands.html: +// The file will be searched for in the current directory, and in any +// directory specified with the -L option. +// +// https://sourceware.org/binutils/docs/ld/Options.html#Options +// If scriptfile does not exist in the current directory, ld looks for it in +// the directories specified by any preceding ‘-L’ options. Multiple ‘-T’ +// options accumulate. + +Optional elf::searchLinkerScript(StringRef Name) { + if (fs::exists(Name)) + return Name.str(); + else if (Optional Path = findFromSearchPaths(Name)) + return Path; + return None; +} Index: ELF/ScriptParser.cpp =================================================================== --- ELF/ScriptParser.cpp +++ ELF/ScriptParser.cpp @@ -350,20 +350,12 @@ return; } - // https://sourceware.org/binutils/docs/ld/File-Commands.html: - // The file will be searched for in the current directory, and in any - // directory specified with the -L option. - if (sys::fs::exists(Tok)) { - if (Optional MB = readFile(Tok)) - tokenize(*MB); - return; - } - if (Optional Path = findFromSearchPaths(Tok)) { + if (Optional Path = searchLinkerScript(Tok)) { if (Optional MB = readFile(*Path)) tokenize(*MB); - return; + } else { + setError("cannot find linker script " + Tok); } - setError("cannot open " + Tok); } void ScriptParser::readOutput() { Index: test/ELF/invalid-linkerscript.test =================================================================== --- test/ELF/invalid-linkerscript.test +++ test/ELF/invalid-linkerscript.test @@ -45,7 +45,7 @@ # RUN: echo "INCLUDE /no/such/file" > %t7 # RUN: not ld.lld %t7 no-such-file 2>&1 | FileCheck -check-prefix=ERR7 %s -# ERR7: cannot open /no/such/file +# ERR7: cannot find linker script /no/such/file # ERR7: cannot open no-such-file: # RUN: echo "OUTPUT_FORMAT(x y z)" > %t8 Index: test/ELF/linkerscript/linker-script-in-search-path.s =================================================================== --- /dev/null +++ test/ELF/linkerscript/linker-script-in-search-path.s @@ -0,0 +1,25 @@ +# Check that we fall back to search paths if a linker script was not found +# This behaviour matches ld.bfd and various projects appear to rely on this + +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o + +# RUN: mkdir -p %T/searchpath +# RUN: echo "OUTPUT(\"%t.out\")" > %T/searchpath/foo.script +# RUN: ld.lld -T%T/searchpath/foo.script %t.o +# RUN: llvm-readobj %t.out | FileCheck %s + +# If the linker script specified with -T is missing we should emit an error +# RUN: not ld.lld -Tfoo.script %t.o 2>&1 | FileCheck %s -check-prefix ERROR + +# But if it exists in the search path we should fall back to that instead: +# RUN: rm %t.out +# RUN: ld.lld -L %T/searchpath -Tfoo.script %t.o +# RUN: llvm-readobj %t.out | FileCheck %s + +# CHECK: Format: ELF64-x86-64 +# ERROR: error: cannot find linker script foo.script + +.globl _start +_start: + ret \ No newline at end of file Index: test/ELF/linkerscript/linkerscript.s =================================================================== --- test/ELF/linkerscript/linkerscript.s +++ test/ELF/linkerscript/linkerscript.s @@ -37,7 +37,7 @@ # RUN: echo "OUTPUT(\"%t.out\")" > %T/foo.script # RUN: not ld.lld %t.script > %t.log 2>&1 # RUN: FileCheck -check-prefix=INCLUDE_ERR %s < %t.log -# INCLUDE_ERR: error: {{.+}}.script:1: cannot open foo.script +# INCLUDE_ERR: error: {{.+}}.script:1: cannot find linker script foo.script # INCLUDE_ERR-NEXT: INCLUDE "foo.script" # RUN: ld.lld -L %T %t.script %t