[lld] r300011 - [lld] Keep full library path in DT_NEEDED.
Evgeniy Stepanov via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 11 17:13:48 PDT 2017
Author: eugenis
Date: Tue Apr 11 19:13:48 2017
New Revision: 300011
URL: http://llvm.org/viewvc/llvm-project?rev=300011&view=rev
Log:
[lld] Keep full library path in DT_NEEDED.
Fixes PR32572.
When
(a) a library has no soname
and (b) library is given on the command line with path (and not through -L/-l flags)
DT_NEEDED entry for such library keeps the path as given.
This behavior is consistent with gold and bfd, and is used in compiler-rt test suite.
This is a second attempt after r300007 got reverted. This time relro-omagic test is
changed in a way to avoid hardcoding the path to the test directory in the objdump'd
binary.
Added:
lld/trunk/test/ELF/no-soname.s
Modified:
lld/trunk/ELF/Driver.cpp
lld/trunk/ELF/Driver.h
lld/trunk/ELF/InputFiles.cpp
lld/trunk/ELF/InputFiles.h
lld/trunk/ELF/ScriptParser.cpp
lld/trunk/test/ELF/as-needed-no-reloc.s
lld/trunk/test/ELF/relro-omagic.s
Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=300011&r1=300010&r2=300011&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Tue Apr 11 19:13:48 2017
@@ -153,7 +153,7 @@ LinkerDriver::getArchiveMembers(MemoryBu
// Opens and parses a file. Path has to be resolved already.
// Newly created memory buffers are owned by this driver.
-void LinkerDriver::addFile(StringRef Path) {
+void LinkerDriver::addFile(StringRef Path, bool WithLOption) {
using namespace sys::fs;
Optional<MemoryBufferRef> Buffer = readFile(Path);
@@ -184,6 +184,11 @@ void LinkerDriver::addFile(StringRef Pat
return;
}
Files.push_back(createSharedFile(MBRef));
+ // If the library is found at an explicitly given path use the entire path
+ // as he default soname. Such libraries should not require RPATH or
+ // LD_LIBRARY_PATH to run.
+ Files.back()->DefaultSoName =
+ WithLOption ? sys::path::filename(Path) : Path;
return;
default:
if (InLib)
@@ -196,7 +201,7 @@ void LinkerDriver::addFile(StringRef Pat
// Add a given library by searching it from input search paths.
void LinkerDriver::addLibrary(StringRef Name) {
if (Optional<std::string> Path = searchLibrary(Name))
- addFile(*Path);
+ addFile(*Path, /*WithLOption=*/true);
else
error("unable to find library -l" + Name);
}
@@ -762,7 +767,7 @@ void LinkerDriver::createFiles(opt::Inpu
addLibrary(Arg->getValue());
break;
case OPT_INPUT:
- addFile(Arg->getValue());
+ addFile(Arg->getValue(), /*WithLOption=*/false);
break;
case OPT_alias_script_T:
case OPT_script:
Modified: lld/trunk/ELF/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.h?rev=300011&r1=300010&r2=300011&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.h (original)
+++ lld/trunk/ELF/Driver.h Tue Apr 11 19:13:48 2017
@@ -27,7 +27,7 @@ extern class LinkerDriver *Driver;
class LinkerDriver {
public:
void main(ArrayRef<const char *> Args, bool CanExitEarly);
- void addFile(StringRef Path);
+ void addFile(StringRef Path, bool WithLOption);
void addLibrary(StringRef Name);
private:
Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=300011&r1=300010&r2=300011&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Tue Apr 11 19:13:48 2017
@@ -661,7 +661,7 @@ template <class ELFT> void SharedFile<EL
// DSOs are identified by soname, and they usually contain
// DT_SONAME tag in their header. But if they are missing,
// filenames are used as default sonames.
- SoName = sys::path::filename(this->getName());
+ SoName = this->DefaultSoName;
if (!DynamicSec)
return;
Modified: lld/trunk/ELF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=300011&r1=300010&r2=300011&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.h (original)
+++ lld/trunk/ELF/InputFiles.h Tue Apr 11 19:13:48 2017
@@ -93,6 +93,10 @@ public:
uint16_t EMachine = llvm::ELF::EM_NONE;
uint8_t OSABI = 0;
+ // For SharedKind inputs, the string to use in DT_NEEDED when the library
+ // has no soname.
+ std::string DefaultSoName;
+
// Cache for toString(). Only toString() should use this member.
mutable std::string ToStringCache;
Modified: lld/trunk/ELF/ScriptParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptParser.cpp?rev=300011&r1=300010&r2=300011&view=diff
==============================================================================
--- lld/trunk/ELF/ScriptParser.cpp (original)
+++ lld/trunk/ELF/ScriptParser.cpp Tue Apr 11 19:13:48 2017
@@ -243,25 +243,26 @@ void ScriptParser::addFile(StringRef S)
SmallString<128> PathData;
StringRef Path = (Config->Sysroot + S).toStringRef(PathData);
if (sys::fs::exists(Path)) {
- Driver->addFile(Saver.save(Path));
+ Driver->addFile(Saver.save(Path), /*WithLOption=*/false);
return;
}
}
if (sys::path::is_absolute(S)) {
- Driver->addFile(S);
+ Driver->addFile(S, /*WithLOption=*/false);
} else if (S.startswith("=")) {
if (Config->Sysroot.empty())
- Driver->addFile(S.substr(1));
+ Driver->addFile(S.substr(1), /*WithLOption=*/false);
else
- Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
+ Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)),
+ /*WithLOption=*/false);
} else if (S.startswith("-l")) {
Driver->addLibrary(S.substr(2));
} else if (sys::fs::exists(S)) {
- Driver->addFile(S);
+ Driver->addFile(S, /*WithLOption=*/false);
} else {
if (Optional<std::string> Path = findFromSearchPaths(S))
- Driver->addFile(Saver.save(*Path));
+ Driver->addFile(Saver.save(*Path), /*WithLOption=*/true);
else
setError("unable to find " + S);
}
Modified: lld/trunk/test/ELF/as-needed-no-reloc.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/as-needed-no-reloc.s?rev=300011&r1=300010&r2=300011&view=diff
==============================================================================
--- lld/trunk/test/ELF/as-needed-no-reloc.s (original)
+++ lld/trunk/test/ELF/as-needed-no-reloc.s Tue Apr 11 19:13:48 2017
@@ -16,7 +16,7 @@
# CHECK-NEXT: Other: 0
# CHECK-NEXT: Section: Undefined
-# CHECK: NEEDED SharedLibrary (as-needed-no-reloc{{.*}}2.so)
+# CHECK: NEEDED SharedLibrary ({{.*}}as-needed-no-reloc{{.*}}2.so)
.globl _start
_start:
Added: lld/trunk/test/ELF/no-soname.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/no-soname.s?rev=300011&view=auto
==============================================================================
--- lld/trunk/test/ELF/no-soname.s (added)
+++ lld/trunk/test/ELF/no-soname.s Tue Apr 11 19:13:48 2017
@@ -0,0 +1,31 @@
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+// RUN: mkdir -p %T/no-soname
+// RUN: ld.lld %t.o -shared -o %T/no-soname/libfoo.so
+
+// RUN: ld.lld %t.o %T/no-soname/libfoo.so -o %t
+// RUN: llvm-readobj --dynamic-table %t | FileCheck %s
+
+// CHECK: 0x0000000000000001 NEEDED SharedLibrary ({{.*}}/no-soname/libfoo.so)
+// CHECK-NOT: NEEDED
+
+// RUN: ld.lld %t.o %T/no-soname/../no-soname/libfoo.so -o %t
+// RUN: llvm-readobj --dynamic-table %t | FileCheck %s --check-prefix=CHECK2
+
+// CHECK2: 0x0000000000000001 NEEDED SharedLibrary ({{.*}}/no-soname/../no-soname/libfoo.so)
+// CHECK2-NOT: NEEDED
+
+// RUN: ld.lld %t.o -L%T/no-soname/../no-soname -lfoo -o %t
+// RUN: llvm-readobj --dynamic-table %t | FileCheck %s --check-prefix=CHECK3
+
+// CHECK3: 0x0000000000000001 NEEDED SharedLibrary (libfoo.so)
+// CHECK3-NOT: NEEDED
+
+// RUN: ld.lld %t.o -shared -soname libbar.so -o %T/no-soname/libbar.so
+// RUN: ld.lld %t.o %T/no-soname/libbar.so -o %t
+// RUN: llvm-readobj --dynamic-table %t | FileCheck %s --check-prefix=CHECK4
+
+// CHECK4: 0x0000000000000001 NEEDED SharedLibrary (libbar.so)
+// CHECK4-NOT: NEEDED
+
+.global _start
+_start:
Modified: lld/trunk/test/ELF/relro-omagic.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/relro-omagic.s?rev=300011&r1=300010&r2=300011&view=diff
==============================================================================
--- lld/trunk/test/ELF/relro-omagic.s (original)
+++ lld/trunk/test/ELF/relro-omagic.s Tue Apr 11 19:13:48 2017
@@ -1,6 +1,6 @@
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/shared.s -o %t2.o
-# RUN: ld.lld -shared %t2.o -o %t2.so
+# RUN: ld.lld -shared %t2.o -o %t2.so -soname relro-omagic.s.tmp2.so
# RUN: ld.lld -N %t.o %t2.so -o %t
# RUN: llvm-objdump -section-headers %t | FileCheck --check-prefix=NORELRO %s
# RUN: llvm-readobj --program-headers %t | FileCheck --check-prefix=NOPHDRS %s
More information about the llvm-commits
mailing list