Fix a crash bug in --start-lib.

Previously, Lazy symbols were created for undefined symbols even though
such symbols cannot be resolved by loading object files. This patch
fixes that bug.

llvm-svn: 265847
This commit is contained in:
Rui Ueyama 2016-04-08 20:49:31 +00:00
parent e25b65bdb7
commit 1f492893ac
4 changed files with 26 additions and 9 deletions

View File

@ -594,7 +594,8 @@ template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
StringRef StringTable = check(Obj.getStringTableForSymtab(Sec)); StringRef StringTable = check(Obj.getStringTableForSymtab(Sec));
std::vector<StringRef> V; std::vector<StringRef> V;
for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal)) for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
V.push_back(check(Sym.getName(StringTable))); if (Sym.st_shndx != SHN_UNDEF)
V.push_back(check(Sym.getName(StringTable)));
return V; return V;
} }
return {}; return {};
@ -608,6 +609,8 @@ std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
for (const BasicSymbolRef &Sym : Obj->symbols()) { for (const BasicSymbolRef &Sym : Obj->symbols()) {
if (BitcodeFile::shouldSkip(Sym)) if (BitcodeFile::shouldSkip(Sym))
continue; continue;
if (Sym.getFlags() == BasicSymbolRef::SF_Undefined)
continue;
SmallString<64> Name; SmallString<64> Name;
raw_svector_ostream OS(Name); raw_svector_ostream OS(Name);
Sym.printName(OS); Sym.printName(OS);
@ -616,7 +619,7 @@ std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
return V; return V;
} }
// Returns a vector of globally-visible symbol names. // Returns a vector of globally-visible defined symbol names.
std::vector<StringRef> LazyObjectFile::getSymbols() { std::vector<StringRef> LazyObjectFile::getSymbols() {
if (isBitcode(this->MB)) if (isBitcode(this->MB))
return getBitcodeSymbols(); return getBitcodeSymbols();

View File

@ -0,0 +1,3 @@
.globl foo
foo:
call bar

View File

@ -0,0 +1,2 @@
.globl bar
bar:

View File

@ -2,15 +2,24 @@
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \ // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
// RUN: %p/Inputs/whole-archive.s -o %t2.o // RUN: %p/Inputs/start-lib1.s -o %t2.o
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
// RUN: %p/Inputs/start-lib2.s -o %t3.o
// RUN: ld.lld -o %t3 %t1.o %t2.o // RUN: ld.lld -o %t3 %t1.o %t2.o %t3.o
// RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=ADDED %s // RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=TEST1 %s
// ADDED: Name: _bar // TEST1: Name: bar
// TEST1: Name: foo
// RUN: ld.lld -o %t3 %t1.o --start-lib %t2.o // RUN: ld.lld -o %t3 %t1.o -u bar --start-lib %t2.o %t3.o
// RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=LIB %s // RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=TEST2 %s
// LIB-NOT: Name: _bar // TEST2: Name: bar
// TEST2-NOT: Name: foo
// RUN: ld.lld -o %t3 %t1.o --start-lib %t2.o %t3.o
// RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=TEST3 %s
// TEST3-NOT: Name: bar
// TEST3-NOT: Name: foo
.globl _start .globl _start
_start: _start: