[llvm-commits] [llvm] r151600 - in /llvm/trunk: include/llvm/Object/COFF.h include/llvm/Object/ELF.h include/llvm/Object/MachO.h include/llvm/Object/ObjectFile.h lib/Object/COFFObjectFile.cpp lib/Object/MachOObjectFile.cpp test/Object/Inputs/shared-object-test.elf-i386 test/Object/Inputs/shared-object-test.elf-x86-64 test/Object/Inputs/shared.ll test/Object/nm-shared-object.test tools/llvm-nm/llvm-nm.cpp
Michael J. Spencer
bigcheesegs at gmail.com
Mon Feb 27 16:40:38 PST 2012
Author: mspencer
Date: Mon Feb 27 18:40:37 2012
New Revision: 151600
URL: http://llvm.org/viewvc/llvm-project?rev=151600&view=rev
Log:
[Object] Add {begin,end}_dynamic_symbols stubs and implementation for ELF.
Add -D option to llvm-nm to dump dynamic symbols.
Patch by David Meyer.
Added:
llvm/trunk/test/Object/Inputs/shared-object-test.elf-i386
llvm/trunk/test/Object/Inputs/shared-object-test.elf-x86-64
llvm/trunk/test/Object/Inputs/shared.ll
llvm/trunk/test/Object/nm-shared-object.test
Modified:
llvm/trunk/include/llvm/Object/COFF.h
llvm/trunk/include/llvm/Object/ELF.h
llvm/trunk/include/llvm/Object/MachO.h
llvm/trunk/include/llvm/Object/ObjectFile.h
llvm/trunk/lib/Object/COFFObjectFile.cpp
llvm/trunk/lib/Object/MachOObjectFile.cpp
llvm/trunk/tools/llvm-nm/llvm-nm.cpp
Modified: llvm/trunk/include/llvm/Object/COFF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/COFF.h?rev=151600&r1=151599&r2=151600&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/COFF.h (original)
+++ llvm/trunk/include/llvm/Object/COFF.h Mon Feb 27 18:40:37 2012
@@ -152,6 +152,8 @@
COFFObjectFile(MemoryBuffer *Object, error_code &ec);
virtual symbol_iterator begin_symbols() const;
virtual symbol_iterator end_symbols() const;
+ virtual symbol_iterator begin_dynamic_symbols() const;
+ virtual symbol_iterator end_dynamic_symbols() const;
virtual section_iterator begin_sections() const;
virtual section_iterator end_sections() const;
Modified: llvm/trunk/include/llvm/Object/ELF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELF.h?rev=151600&r1=151599&r2=151600&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELF.h (original)
+++ llvm/trunk/include/llvm/Object/ELF.h Mon Feb 27 18:40:37 2012
@@ -295,6 +295,7 @@
const Elf_Shdr *SectionHeaderTable;
const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
+ const Elf_Shdr *dot_dynstr_sec; // Dynamic symbol string table.
Sections_t SymbolTableSections;
IndexMap_t SymbolTableSectionsIndexMap;
DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable;
@@ -319,7 +320,10 @@
const Elf_Rela *getRela(DataRefImpl Rela) const;
const char *getString(uint32_t section, uint32_t offset) const;
const char *getString(const Elf_Shdr *section, uint32_t offset) const;
- error_code getSymbolName(const Elf_Sym *Symb, StringRef &Res) const;
+ error_code getSymbolName(const Elf_Shdr *section,
+ const Elf_Sym *Symb,
+ StringRef &Res) const;
+ void VerifyStrTab(const Elf_Shdr *sh) const;
protected:
const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
@@ -375,6 +379,8 @@
ELFObjectFile(MemoryBuffer *Object, error_code &ec);
virtual symbol_iterator begin_symbols() const;
virtual symbol_iterator end_symbols() const;
+ virtual symbol_iterator begin_dynamic_symbols() const;
+ virtual symbol_iterator end_dynamic_symbols() const;
virtual section_iterator begin_sections() const;
virtual section_iterator end_sections() const;
@@ -425,10 +431,14 @@
// Check to see if we are at the end of this symbol table.
if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
// We are at the end. If there are other symbol tables, jump to them.
- ++Symb.d.b;
- Symb.d.a = 1; // The 0th symbol in ELF is fake.
+ // If the symbol table is .dynsym, we are iterating dynamic symbols,
+ // and there is only one table of these.
+ if (Symb.d.b != 0) {
+ ++Symb.d.b;
+ Symb.d.a = 1; // The 0th symbol in ELF is fake.
+ }
// Otherwise return the terminator.
- if (Symb.d.b >= SymbolTableSections.size()) {
+ if (Symb.d.b == 0 || Symb.d.b >= SymbolTableSections.size()) {
Symb.d.a = std::numeric_limits<uint32_t>::max();
Symb.d.b = std::numeric_limits<uint32_t>::max();
}
@@ -444,7 +454,7 @@
StringRef &Result) const {
validateSymbol(Symb);
const Elf_Sym *symb = getSymbol(Symb);
- return getSymbolName(symb, Result);
+ return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result);
}
template<support::endianness target_endianness, bool is64Bits>
@@ -1128,7 +1138,7 @@
}
const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
StringRef symname;
- if (error_code ec = getSymbolName(symb, symname))
+ if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
return ec;
switch (Header->e_machine) {
case ELF::EM_X86_64:
@@ -1156,6 +1166,16 @@
return object_error::success;
}
+// Verify that the last byte in the string table in a null.
+template<support::endianness target_endianness, bool is64Bits>
+void ELFObjectFile<target_endianness, is64Bits>
+ ::VerifyStrTab(const Elf_Shdr *sh) const {
+ const char *strtab = (const char*)base() + sh->sh_offset;
+ if (strtab[sh->sh_size - 1] != 0)
+ // FIXME: Proper error handling.
+ report_fatal_error("String table must end with a null terminator!");
+}
+
template<support::endianness target_endianness, bool is64Bits>
ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
, error_code &ec)
@@ -1163,7 +1183,8 @@
, isDyldELFObject(false)
, SectionHeaderTable(0)
, dot_shstrtab_sec(0)
- , dot_strtab_sec(0) {
+ , dot_strtab_sec(0)
+ , dot_dynstr_sec(0) {
const uint64_t FileSize = Data->getBufferSize();
@@ -1194,6 +1215,10 @@
// To find the symbol tables we walk the section table to find SHT_SYMTAB.
const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
const Elf_Shdr* sh = SectionHeaderTable;
+
+ // Reserve SymbolTableSections[0] for .dynsym
+ SymbolTableSections.push_back(NULL);
+
for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
if (sh->sh_type == ELF::SHT_SYMTAB_SHNDX) {
if (SymbolTableSectionHeaderIndex)
@@ -1205,6 +1230,13 @@
SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
SymbolTableSections.push_back(sh);
}
+ if (sh->sh_type == ELF::SHT_DYNSYM) {
+ if (SymbolTableSections[0] != NULL)
+ // FIXME: Proper error handling.
+ report_fatal_error("More than one .dynsym!");
+ SymbolTableSectionsIndexMap[i] = 0;
+ SymbolTableSections[0] = sh;
+ }
if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
SectionRelocMap[getSection(sh->sh_info)].push_back(i);
}
@@ -1221,10 +1253,7 @@
dot_shstrtab_sec = getSection(getStringTableIndex());
if (dot_shstrtab_sec) {
// Verify that the last byte in the string table in a null.
- if (((const char*)base() + dot_shstrtab_sec->sh_offset)
- [dot_shstrtab_sec->sh_size - 1] != 0)
- // FIXME: Proper error handling.
- report_fatal_error("String table must end with a null terminator!");
+ VerifyStrTab(dot_shstrtab_sec);
}
// Merge this into the above loop.
@@ -1239,10 +1268,13 @@
// FIXME: Proper error handling.
report_fatal_error("Already found section named .strtab!");
dot_strtab_sec = sh;
- const char *dot_strtab = (const char*)base() + sh->sh_offset;
- if (dot_strtab[sh->sh_size - 1] != 0)
- // FIXME: Proper error handling.
- report_fatal_error("String table must end with a null terminator!");
+ VerifyStrTab(dot_strtab_sec);
+ } else if (SectionName == ".dynstr") {
+ if (dot_dynstr_sec != 0)
+ // FIXME: Proper error handling.
+ report_fatal_error("Already found section named .dynstr!");
+ dot_dynstr_sec = sh;
+ VerifyStrTab(dot_dynstr_sec);
}
}
}
@@ -1268,12 +1300,12 @@
::begin_symbols() const {
DataRefImpl SymbolData;
memset(&SymbolData, 0, sizeof(SymbolData));
- if (SymbolTableSections.size() == 0) {
+ if (SymbolTableSections.size() <= 1) {
SymbolData.d.a = std::numeric_limits<uint32_t>::max();
SymbolData.d.b = std::numeric_limits<uint32_t>::max();
} else {
SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
- SymbolData.d.b = 0;
+ SymbolData.d.b = 1; // The 0th table is .dynsym
}
return symbol_iterator(SymbolRef(SymbolData, this));
}
@@ -1289,6 +1321,31 @@
}
template<support::endianness target_endianness, bool is64Bits>
+symbol_iterator ELFObjectFile<target_endianness, is64Bits>
+ ::begin_dynamic_symbols() const {
+ DataRefImpl SymbolData;
+ memset(&SymbolData, 0, sizeof(SymbolData));
+ if (SymbolTableSections[0] == NULL) {
+ SymbolData.d.a = std::numeric_limits<uint32_t>::max();
+ SymbolData.d.b = std::numeric_limits<uint32_t>::max();
+ } else {
+ SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
+ SymbolData.d.b = 0; // The 0th table is .dynsym
+ }
+ return symbol_iterator(SymbolRef(SymbolData, this));
+}
+
+template<support::endianness target_endianness, bool is64Bits>
+symbol_iterator ELFObjectFile<target_endianness, is64Bits>
+ ::end_dynamic_symbols() const {
+ DataRefImpl SymbolData;
+ memset(&SymbolData, 0, sizeof(SymbolData));
+ SymbolData.d.a = std::numeric_limits<uint32_t>::max();
+ SymbolData.d.b = std::numeric_limits<uint32_t>::max();
+ return symbol_iterator(SymbolRef(SymbolData, this));
+}
+
+template<support::endianness target_endianness, bool is64Bits>
section_iterator ELFObjectFile<target_endianness, is64Bits>
::begin_sections() const {
DataRefImpl ret;
@@ -1461,7 +1518,8 @@
template<support::endianness target_endianness, bool is64Bits>
error_code ELFObjectFile<target_endianness, is64Bits>
- ::getSymbolName(const Elf_Sym *symb,
+ ::getSymbolName(const Elf_Shdr *section,
+ const Elf_Sym *symb,
StringRef &Result) const {
if (symb->st_name == 0) {
const Elf_Shdr *section = getSection(symb);
@@ -1472,8 +1530,13 @@
return object_error::success;
}
- // Use the default symbol table name section.
- Result = getString(dot_strtab_sec, symb->st_name);
+ if (section == SymbolTableSections[0]) {
+ // Symbol is in .dynsym, use .dynstr string table
+ Result = getString(dot_dynstr_sec, symb->st_name);
+ } else {
+ // Use the default symbol table name section.
+ Result = getString(dot_strtab_sec, symb->st_name);
+ }
return object_error::success;
}
Modified: llvm/trunk/include/llvm/Object/MachO.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/MachO.h?rev=151600&r1=151599&r2=151600&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/MachO.h (original)
+++ llvm/trunk/include/llvm/Object/MachO.h Mon Feb 27 18:40:37 2012
@@ -32,6 +32,8 @@
virtual symbol_iterator begin_symbols() const;
virtual symbol_iterator end_symbols() const;
+ virtual symbol_iterator begin_dynamic_symbols() const;
+ virtual symbol_iterator end_dynamic_symbols() const;
virtual section_iterator begin_sections() const;
virtual section_iterator end_sections() const;
Modified: llvm/trunk/include/llvm/Object/ObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ObjectFile.h?rev=151600&r1=151599&r2=151600&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ObjectFile.h Mon Feb 27 18:40:37 2012
@@ -313,6 +313,9 @@
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;
Modified: llvm/trunk/lib/Object/COFFObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/COFFObjectFile.cpp?rev=151600&r1=151599&r2=151600&view=diff
==============================================================================
--- llvm/trunk/lib/Object/COFFObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/COFFObjectFile.cpp Mon Feb 27 18:40:37 2012
@@ -508,6 +508,16 @@
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");
+}
+
section_iterator COFFObjectFile::begin_sections() const {
DataRefImpl ret;
std::memset(&ret, 0, sizeof(DataRefImpl));
Modified: llvm/trunk/lib/Object/MachOObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOObjectFile.cpp?rev=151600&r1=151599&r2=151600&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/MachOObjectFile.cpp Mon Feb 27 18:40:37 2012
@@ -388,6 +388,15 @@
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");
+}
/*===-- Sections ----------------------------------------------------------===*/
Added: llvm/trunk/test/Object/Inputs/shared-object-test.elf-i386
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/Inputs/shared-object-test.elf-i386?rev=151600&view=auto
==============================================================================
Binary files llvm/trunk/test/Object/Inputs/shared-object-test.elf-i386 (added) and llvm/trunk/test/Object/Inputs/shared-object-test.elf-i386 Mon Feb 27 18:40:37 2012 differ
Added: llvm/trunk/test/Object/Inputs/shared-object-test.elf-x86-64
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/Inputs/shared-object-test.elf-x86-64?rev=151600&view=auto
==============================================================================
Binary files llvm/trunk/test/Object/Inputs/shared-object-test.elf-x86-64 (added) and llvm/trunk/test/Object/Inputs/shared-object-test.elf-x86-64 Mon Feb 27 18:40:37 2012 differ
Added: llvm/trunk/test/Object/Inputs/shared.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/Inputs/shared.ll?rev=151600&view=auto
==============================================================================
--- llvm/trunk/test/Object/Inputs/shared.ll (added)
+++ llvm/trunk/test/Object/Inputs/shared.ll Mon Feb 27 18:40:37 2012
@@ -0,0 +1,31 @@
+; How to make the shared objects from this file:
+;
+; X86-32 ELF:
+; llc -mtriple=i386-linux-gnu shared.ll -filetype=obj -o tmp32.o -relocation-model=pic
+; ld -melf_i386 -shared tmp32.o -o shared-object-test.elf-i386 --unresolved-symbols=ignore-all
+;
+; X86-64 ELF:
+; llc -mtriple=x86_64-linux-gnu shared.ll -filetype=obj -o tmp64.o -relocation-model=pic
+; ld -melf_x86_64 -shared tmp64.o -o shared-object-test.elf-x86-64 --unresolved-symbols=ignore-all
+
+ at defined_sym = global i32 1, align 4
+
+ at tls_sym = thread_local global i32 2, align 4
+
+ at undef_sym = external global i32
+
+ at undef_tls_sym = external thread_local global i32
+
+ at common_sym = common global i32 0, align 4
+
+define i32 @global_func() nounwind uwtable {
+entry:
+ ret i32 0
+}
+
+declare i32 @undef_func(...)
+
+define internal i32 @local_func() nounwind uwtable {
+entry:
+ ret i32 0
+}
Added: llvm/trunk/test/Object/nm-shared-object.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/nm-shared-object.test?rev=151600&view=auto
==============================================================================
--- llvm/trunk/test/Object/nm-shared-object.test (added)
+++ llvm/trunk/test/Object/nm-shared-object.test Mon Feb 27 18:40:37 2012
@@ -0,0 +1,15 @@
+RUN: llvm-nm -D %p/Inputs/shared-object-test.elf-i386 \
+RUN: | FileCheck %s -check-prefix ELF
+RUN: llvm-nm -D %p/Inputs/shared-object-test.elf-x86-64 \
+RUN: | FileCheck %s -check-prefix ELF
+
+; Note: tls_sym should be 'D' (not '?'), but TLS is not
+; yet recognized by ObjectFile.
+
+ELF: {{[0-9a-f]+}} A __bss_start
+ELF: {{[0-9a-f]+}} A _edata
+ELF: {{[0-9a-f]+}} A _end
+ELF: {{[0-9a-f]+}} B common_sym
+ELF: {{[0-9a-f]+}} D defined_sym
+ELF: {{[0-9a-f]+}} T global_func
+ELF: ? tls_sym
Modified: llvm/trunk/tools/llvm-nm/llvm-nm.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-nm/llvm-nm.cpp?rev=151600&r1=151599&r2=151600&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-nm/llvm-nm.cpp (original)
+++ llvm/trunk/tools/llvm-nm/llvm-nm.cpp Mon Feb 27 18:40:37 2012
@@ -61,6 +61,12 @@
cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
cl::aliasopt(UndefinedOnly));
+ cl::opt<bool> DynamicSyms("dynamic",
+ cl::desc("Display the dynamic symbols instead "
+ "of normal symbols."));
+ cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
+ cl::aliasopt(DynamicSyms));
+
cl::opt<bool> DefinedOnly("defined-only",
cl::desc("Show only defined symbols"));
@@ -277,9 +283,13 @@
static void DumpSymbolNamesFromObject(ObjectFile *obj) {
error_code ec;
- for (symbol_iterator i = obj->begin_symbols(),
- e = obj->end_symbols();
- i != e; i.increment(ec)) {
+ symbol_iterator ibegin = obj->begin_symbols();
+ symbol_iterator iend = obj->end_symbols();
+ if (DynamicSyms) {
+ ibegin = obj->begin_dynamic_symbols();
+ iend = obj->end_dynamic_symbols();
+ }
+ for (symbol_iterator i = ibegin; i != iend; i.increment(ec)) {
if (error(ec)) break;
bool internal;
if (error(i->isInternal(internal))) break;
More information about the llvm-commits
mailing list