diff --git a/llvm/include/llvm/Object/COFF.h b/llvm/include/llvm/Object/COFF.h index 6091e6104697..248545d196fc 100644 --- a/llvm/include/llvm/Object/COFF.h +++ b/llvm/include/llvm/Object/COFF.h @@ -335,8 +335,6 @@ public: COFFObjectFile(MemoryBuffer *Object, error_code &EC, bool BufferOwned = true); symbol_iterator begin_symbols() const LLVM_OVERRIDE; symbol_iterator end_symbols() const LLVM_OVERRIDE; - symbol_iterator begin_dynamic_symbols() const LLVM_OVERRIDE; - symbol_iterator end_dynamic_symbols() const LLVM_OVERRIDE; library_iterator begin_libraries_needed() const LLVM_OVERRIDE; library_iterator end_libraries_needed() const LLVM_OVERRIDE; section_iterator begin_sections() const LLVM_OVERRIDE; diff --git a/llvm/include/llvm/Object/ELFObjectFile.h b/llvm/include/llvm/Object/ELFObjectFile.h index 45bcde6155b1..f6b874397a59 100644 --- a/llvm/include/llvm/Object/ELFObjectFile.h +++ b/llvm/include/llvm/Object/ELFObjectFile.h @@ -183,8 +183,8 @@ public: symbol_iterator begin_symbols() const LLVM_OVERRIDE; symbol_iterator end_symbols() const LLVM_OVERRIDE; - symbol_iterator begin_dynamic_symbols() const LLVM_OVERRIDE; - symbol_iterator end_dynamic_symbols() const LLVM_OVERRIDE; + symbol_iterator begin_dynamic_symbols() const; + symbol_iterator end_dynamic_symbols() const; section_iterator begin_sections() const LLVM_OVERRIDE; section_iterator end_sections() const LLVM_OVERRIDE; diff --git a/llvm/include/llvm/Object/MachO.h b/llvm/include/llvm/Object/MachO.h index fb548e32622f..1f36938ea61d 100644 --- a/llvm/include/llvm/Object/MachO.h +++ b/llvm/include/llvm/Object/MachO.h @@ -130,9 +130,6 @@ public: symbol_iterator begin_symbols() const LLVM_OVERRIDE; symbol_iterator end_symbols() const LLVM_OVERRIDE; - symbol_iterator begin_dynamic_symbols() const LLVM_OVERRIDE; - symbol_iterator end_dynamic_symbols() const LLVM_OVERRIDE; - section_iterator begin_sections() const LLVM_OVERRIDE; section_iterator end_sections() const LLVM_OVERRIDE; diff --git a/llvm/include/llvm/Object/ObjectFile.h b/llvm/include/llvm/Object/ObjectFile.h index 1d6c536d4f66..69300c662885 100644 --- a/llvm/include/llvm/Object/ObjectFile.h +++ b/llvm/include/llvm/Object/ObjectFile.h @@ -347,9 +347,6 @@ public: virtual symbol_iterator begin_symbols() const = 0; virtual symbol_iterator end_symbols() const = 0; - virtual symbol_iterator begin_dynamic_symbols() const = 0; - virtual symbol_iterator end_dynamic_symbols() const = 0; - virtual section_iterator begin_sections() const = 0; virtual section_iterator end_sections() const = 0; diff --git a/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp index 6439056dfad2..43321c43a4ca 100644 --- a/llvm/lib/Object/COFFObjectFile.cpp +++ b/llvm/lib/Object/COFFObjectFile.cpp @@ -550,16 +550,6 @@ symbol_iterator COFFObjectFile::end_symbols() const { return symbol_iterator(SymbolRef(Ret, this)); } -symbol_iterator COFFObjectFile::begin_dynamic_symbols() const { - // TODO: implement - report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile"); -} - -symbol_iterator COFFObjectFile::end_dynamic_symbols() const { - // TODO: implement - report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile"); -} - library_iterator COFFObjectFile::begin_libraries_needed() const { // TODO: implement report_fatal_error("Libraries needed unimplemented in COFFObjectFile"); diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp index 49e3148ac8f6..aca12197eeac 100644 --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -1192,16 +1192,6 @@ symbol_iterator MachOObjectFile::end_symbols() const { return symbol_iterator(SymbolRef(DRI, this)); } -symbol_iterator MachOObjectFile::begin_dynamic_symbols() const { - // TODO: implement - report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile"); -} - -symbol_iterator MachOObjectFile::end_dynamic_symbols() const { - // TODO: implement - report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile"); -} - section_iterator MachOObjectFile::begin_sections() const { DataRefImpl DRI; return section_iterator(SectionRef(DRI, this)); diff --git a/llvm/test/Object/nm-shared-object.test b/llvm/test/Object/nm-shared-object.test index b15ed795651a..319ed14ceed5 100644 --- a/llvm/test/Object/nm-shared-object.test +++ b/llvm/test/Object/nm-shared-object.test @@ -1,7 +1,5 @@ RUN: llvm-nm -D %p/Inputs/shared-object-test.elf-i386 \ RUN: | FileCheck %s -check-prefix ELF-32 -RUN: llvm-nm -D %p/Inputs/shared-object-test.elf-x86-64 \ -RUN: | FileCheck %s -check-prefix ELF-64 ELF-32: 0012c8 A __bss_start ELF-32: 0012c8 A _edata @@ -11,6 +9,9 @@ ELF-32: 0012c4 D defined_sym ELF-32: 0001f0 T global_func ELF-32: 000000 D tls_sym +RUN: llvm-nm -D %p/Inputs/shared-object-test.elf-x86-64 \ +RUN: | FileCheck %s -check-prefix ELF-64 + ELF-64: 200454 A __bss_start ELF-64: 200454 A _edata ELF-64: 200458 A _end @@ -18,3 +19,11 @@ ELF-64: 200454 B common_sym ELF-64: 200450 D defined_sym ELF-64: 0002f0 T global_func ELF-64: 000000 D tls_sym + +RUN: not llvm-nm -D %p/Inputs/weak-global-symbol.macho-i386 2>&1 \ +RUN: | FileCheck %s -check-prefix ERROR + +RUN: not llvm-nm -D %p/Inputs/trivial-object-test.coff-i386 2>&1 \ +RUN: | FileCheck %s -check-prefix ERROR + +ERROR: File format has no dynamic symbol table. diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp index 2071aa627db4..a35c34e8570d 100644 --- a/llvm/tools/llvm-nm/llvm-nm.cpp +++ b/llvm/tools/llvm-nm/llvm-nm.cpp @@ -527,12 +527,38 @@ static char getNMTypeChar(ObjectFile *Obj, symbol_iterator I) { return Res; } +static void getDynamicSymbolIterators(ObjectFile *Obj, symbol_iterator &Begin, + symbol_iterator &End) { + if (ELF32LEObjectFile *ELF = dyn_cast(Obj)) { + Begin = ELF->begin_dynamic_symbols(); + End = ELF->end_dynamic_symbols(); + return; + } + if (ELF64LEObjectFile *ELF = dyn_cast(Obj)) { + Begin = ELF->begin_dynamic_symbols(); + End = ELF->end_dynamic_symbols(); + return; + } + if (ELF32BEObjectFile *ELF = dyn_cast(Obj)) { + Begin = ELF->begin_dynamic_symbols(); + End = ELF->end_dynamic_symbols(); + return; + } + ELF64BEObjectFile *ELF = cast(Obj); + Begin = ELF->begin_dynamic_symbols(); + End = ELF->end_dynamic_symbols(); + return; +} + static void dumpSymbolNamesFromObject(ObjectFile *Obj) { symbol_iterator IBegin = Obj->begin_symbols(); symbol_iterator IEnd = Obj->end_symbols(); if (DynamicSyms) { - IBegin = Obj->begin_dynamic_symbols(); - IEnd = Obj->end_dynamic_symbols(); + if (!Obj->isELF()) { + error("File format has no dynamic symbol table", Obj->getFileName()); + return; + } + getDynamicSymbolIterators(Obj, IBegin, IEnd); } for (symbol_iterator I = IBegin; I != IEnd; ++I) { uint32_t SymFlags;