Don't crash in 'llvm -s' when an archive has no symtab.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186029 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2013-07-10 20:14:22 +00:00
parent ed094c32ff
commit 5159718095
3 changed files with 39 additions and 23 deletions

View File

@ -210,7 +210,7 @@ error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const {
}
Archive::Archive(MemoryBuffer *source, error_code &ec)
: Binary(Binary::ID_Archive, source) {
: Binary(Binary::ID_Archive, source), SymbolTable(end_children()) {
// Check for sufficient magic.
if (!source || source->getBufferSize()
< (8 + sizeof(ArchiveMemberHeader)) // Smallest archive.
@ -375,6 +375,9 @@ Archive::Symbol Archive::Symbol::getNext() const {
}
Archive::symbol_iterator Archive::begin_symbols() const {
if (SymbolTable == end_children())
return symbol_iterator(Symbol(this, 0, 0));
const char *buf = SymbolTable->getBuffer().begin();
if (kind() == K_GNU) {
uint32_t symbol_count = 0;
@ -395,6 +398,9 @@ Archive::symbol_iterator Archive::begin_symbols() const {
}
Archive::symbol_iterator Archive::end_symbols() const {
if (SymbolTable == end_children())
return symbol_iterator(Symbol(this, 0, 0));
const char *buf = SymbolTable->getBuffer().begin();
uint32_t symbol_count = 0;
if (kind() == K_GNU) {

View File

@ -1,17 +1,6 @@
RUN: llvm-nm %p/Inputs/archive-test.a-coff-i386 \
RUN: | FileCheck %s -check-prefix COFF
RUN: llvm-as %p/Inputs/trivial.ll -o=%t1
RUN: rm -f %t2
RUN: llvm-ar rcs %t2 %t1
RUN: llvm-nm %t2 | FileCheck %s -check-prefix BITCODE
Test we don't error with an archive with no symtab.
RUN: llvm-nm %p/Inputs/archive-test.a-gnu-no-symtab
Or in an archive with no symtab or string table.
RUN: llvm-nm %p/Inputs/archive-test.a-gnu-minimal
COFF: trivial-object-test.coff-i386:
COFF-NEXT: 00000000 d .data
COFF-NEXT: 00000000 t .text
@ -20,6 +9,24 @@ COFF-NEXT: U _SomeOtherFunction
COFF-NEXT: 00000000 T _main
COFF-NEXT: U _puts
RUN: llvm-as %p/Inputs/trivial.ll -o=%t1
RUN: rm -f %t2
RUN: llvm-ar rcs %t2 %t1
RUN: llvm-nm %t2 | FileCheck %s -check-prefix BITCODE
BITCODE: U SomeOtherFunction
BITCODE-NEXT: T main
BITCODE-NEXT: U puts
Test we don't error with an archive with no symtab.
RUN: llvm-nm %p/Inputs/archive-test.a-gnu-no-symtab
Or in an archive with no symtab or string table.
RUN: llvm-nm %p/Inputs/archive-test.a-gnu-minimal
And don't crash when asked to print a non existing symtab.
RUN: llvm-nm -s %p/Inputs/archive-test.a-gnu-minimal

View File

@ -365,21 +365,24 @@ static void DumpSymbolNamesFromFile(std::string &Filename) {
if (object::Archive *a = dyn_cast<object::Archive>(arch.get())) {
if (ArchiveMap) {
outs() << "Archive map" << "\n";
for (object::Archive::symbol_iterator i = a->begin_symbols(),
e = a->end_symbols(); i != e; ++i) {
object::Archive::child_iterator c;
StringRef symname;
StringRef filename;
if (error(i->getMember(c)))
object::Archive::symbol_iterator I = a->begin_symbols();
object::Archive::symbol_iterator E = a->end_symbols();
if (I !=E) {
outs() << "Archive map" << "\n";
for (; I != E; ++I) {
object::Archive::child_iterator c;
StringRef symname;
StringRef filename;
if (error(I->getMember(c)))
return;
if (error(i->getName(symname)))
if (error(I->getName(symname)))
return;
if (error(c->getName(filename)))
if (error(c->getName(filename)))
return;
outs() << symname << " in " << filename << "\n";
outs() << symname << " in " << filename << "\n";
}
outs() << "\n";
}
outs() << "\n";
}
for (object::Archive::child_iterator i = a->begin_children(),