[llvm] 541bead - [Object] object::ELFObjectFile::dynamic_symbol_begin(): skip symbol index 0
via llvm-commits
llvm-commits at lists.llvm.org
Sat Apr 4 03:44:11 PDT 2020
Author: vgxbj
Date: 2020-04-04T18:45:52+08:00
New Revision: 541bead8b424c46b43b3d5fcf19377db70802573
URL: https://github.com/llvm/llvm-project/commit/541bead8b424c46b43b3d5fcf19377db70802573
DIFF: https://github.com/llvm/llvm-project/commit/541bead8b424c46b43b3d5fcf19377db70802573.diff
LOG: [Object] object::ELFObjectFile::dynamic_symbol_begin(): skip symbol index 0
Summary:
Note: This revision is very similar to D62296.
In D75756, we need `getDynamicSymbolIterators()` to skip first NULL symbol in `.dynsym`. And I believe it might be worth pointing this out in a separate patch to gather you experts' opinions.
I have checked that current code base will not be affected by this change.
```
dynamic_symbol_begin()
|- dynamic_symbol_end(): Ok
`- getDynamicSymbolIterators()
|- addDynamicElfSymbols(): llvm/tools/llvm-objdump/llvm-objdump.cpp, Line 934
| Ok, NULL symbol will be omitted by Line 945-947
| StringRef Name = unwrapOrError(Symbol.getName(), Obj->getName());
| if (Name.empty()) continue;
|- dumpSymbolNameFromObject(): llvm/tools/llvm-nm/llvm-nm.cpp, Line 1192
| There's no test for dumping dynamic debugging symbol. This patch helps improve llvm-nm behavior. (we should add test for this later)
`- computeSymbolSizes(): llvm/lib/Object/SymbolSize.cpp, Line 52
|- OProfileJITEventListener::notifyObjectLoaded(): llvm/lib/ExecutionEngine/OProfileJIT/OProfileJITEventListener.cpp, Line 92
| Ok, NULL symbol will be omitted by Line 94-95
| if (!Sym.getType() || *Sym.getType() != SF_Function) continue;
|- IntelJITEventListener::notifyObjectLoaded(): llvm/lib/ExecutionEngine/IntelJITEvents/IntelJITEventListener.cpp, Line 98
| Ok, NULL symbol will be omitted by Line 124-126 (same as previous one)
|- PerfJITEventListener::notifyObjectLoaded(): llvm/lib/ExecutionEngine/PerfJITEvents/PerfJITEventListener.cpp, Line 244
| Ok, NULL symbol will be omitted by Line 254-256, (same as previous one)
|- SymbolizableObjectFile::create(): llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp, Line 73
| Ok, NULL symbol will be omitted by Line 75
| res->addSymbol()
| In addSymbol(), Line 167-168
| if (!Sec || (Obj && Obj->section_end() == *Sec)) return std::error_code();
|- dumpCXXData(): llvm/tools/llvm-cxxdump/llvm-cxxdump.cpp, Line 189
| Ok, NULL symbol will be omitted by Line 199-202
| object::section_iterator SecI = *SecIOrErr;
| // Skip external symbols.
| if (SecI == Obj->section_end())
| continue;
`- printLineInfoForInput(): llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp, Line 418
Ok, NULL symbol will be omitted by Line 430-477
if (Type == object::SymbolRef::ST_Function) {
...
}
```
Reviewers: grimar, jhenderson, MaskRay
Reviewed By: jhenderson, MaskRay
Subscribers: rupprecht, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76081
Added:
llvm/test/tools/llvm-nm/dynamic.test
Modified:
llvm/include/llvm/Object/ELFObjectFile.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/Object/ELFObjectFile.h b/llvm/include/llvm/Object/ELFObjectFile.h
index 5c10badda394..c6212be6daa9 100644
--- a/llvm/include/llvm/Object/ELFObjectFile.h
+++ b/llvm/include/llvm/Object/ELFObjectFile.h
@@ -1027,8 +1027,12 @@ basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end() const {
template <class ELFT>
elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
- DataRefImpl Sym = toDRI(DotDynSymSec, 0);
- return symbol_iterator(SymbolRef(Sym, this));
+ if (!DotDynSymSec || DotDynSymSec->sh_size < sizeof(Elf_Sym))
+ // Ignore errors here where the dynsym is empty or sh_size less than the
+ // size of one symbol. These should be handled elsewhere.
+ return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 0), this));
+ // Skip 0-index NULL symbol.
+ return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 1), this));
}
template <class ELFT>
diff --git a/llvm/test/tools/llvm-nm/dynamic.test b/llvm/test/tools/llvm-nm/dynamic.test
new file mode 100644
index 000000000000..7c7ec8241ec7
--- /dev/null
+++ b/llvm/test/tools/llvm-nm/dynamic.test
@@ -0,0 +1,62 @@
+## This is a test for --dynamic/-D option.
+
+## Test llvm-nm dumping ELF file with valid .dynsym section.
+# RUN: yaml2obj --docnum=1 %s -o %t1.o
+# RUN: llvm-nm --dynamic %t1.o | \
+# RUN: FileCheck %s --match-full-lines --strict-whitespace --check-prefix DYNSYM
+# RUN: llvm-nm -D %t1.o | \
+# RUN: FileCheck %s --match-full-lines --strict-whitespace --check-prefix DYNSYM
+
+# DYNSYM: U globalsym
+# DYNSYM-NEXT: U localsym1
+# DYNSYM-NEXT:0000000000000000 n localsym2
+# DYNSYM-EMPTY:
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_DYN
+ Machine: EM_X86_64
+Sections:
+ - Name: section
+ Type: SHT_PROGBITS
+DynamicSymbols:
+ - Name: localsym1
+ Type: STT_OBJECT
+ - Name: localsym2
+ Section: section
+ - Name: globalsym
+ Type: STT_OBJECT
+ Binding: STB_GLOBAL
+
+## Test llvm-nm dumping ELF file without a .dynsym section.
+# RUN: yaml2obj --docnum=2 %s -o %t2.o
+# RUN: llvm-nm --dynamic %t2.o 2>&1 | \
+# RUN: FileCheck %s --match-full-lines --strict-whitespace -DFILE=%t2.o --check-prefix NO-SYMS
+
+# NO-SYMS:[[FILE]]: no symbols
+# NO-SYMS-EMPTY:
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_DYN
+ Machine: EM_X86_64
+
+## Test llvm-nm dumping ELF file with an empty .dynsym section.
+# RUN: yaml2obj --docnum=3 %s -o %t3.o
+# RUN: llvm-nm --dynamic %t3.o 2>&1 | \
+# RUN: FileCheck %s --match-full-lines --strict-whitespace -DFILE=%t3.o --check-prefix NO-SYMS
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_DYN
+ Machine: EM_X86_64
+Sections:
+ - Name: .dynsym
+ Type: SHT_DYNSYM
+ Size: 0
More information about the llvm-commits
mailing list