[llvm] 0e8b61f - llvm-objdump/ELF: fix crash when reading dyn str table (#87519)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Apr 6 04:22:28 PDT 2024
Author: Ramkumar Ramachandra
Date: 2024-04-06T12:22:24+01:00
New Revision: 0e8b61f8e0bd37e99f3de06e4e8885844f904eba
URL: https://github.com/llvm/llvm-project/commit/0e8b61f8e0bd37e99f3de06e4e8885844f904eba
DIFF: https://github.com/llvm/llvm-project/commit/0e8b61f8e0bd37e99f3de06e4e8885844f904eba.diff
LOG: llvm-objdump/ELF: fix crash when reading dyn str table (#87519)
When reading the dynamic string table, llvm-objdump used to crash if the
ELF was malformed, due to an erroneous consumption of error status.
Instead, propogate the error status to the caller, fixing the crash, and
printing a warning.
Added:
Modified:
llvm/test/tools/llvm-objdump/ELF/dynamic-malformed.test
llvm/tools/llvm-objdump/ELFDump.cpp
Removed:
################################################################################
diff --git a/llvm/test/tools/llvm-objdump/ELF/dynamic-malformed.test b/llvm/test/tools/llvm-objdump/ELF/dynamic-malformed.test
index b10e4f5e44f181..b1e3ca17b50490 100644
--- a/llvm/test/tools/llvm-objdump/ELF/dynamic-malformed.test
+++ b/llvm/test/tools/llvm-objdump/ELF/dynamic-malformed.test
@@ -12,7 +12,6 @@ FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
- Machine: EM_X86_64
Sections:
- Name: .dynamic
Type: SHT_DYNAMIC
@@ -29,10 +28,35 @@ FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
- Machine: EM_X86_64
Sections:
- Name: .dynamic
Type: SHT_DYNAMIC
Entries:
- Tag: DT_SONAME
Value: 1
+
+# RUN: yaml2obj %s --docnum=3 -o %t.invalidaddr
+# RUN: llvm-objdump -p %t.invalidaddr 2>&1 | \
+# RUN: FileCheck %s -DFILE=%t.invalidaddr --implicit-check-not=warning: --check-prefix=ADDR
+
+# ADDR: Dynamic Section:
+# ADDR-NEXT: warning: '[[FILE]]': virtual address is not in any segment: 0x474
+# ADDR-NEXT: NEEDED 0xffffffffbe5a0b5f
+# ADDR-NEXT: STRTAB 0x0000000000000474
+
+---
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_DYN
+Sections:
+ - Name: .dynamic
+ Type: SHT_DYNAMIC
+ Entries:
+ - Tag: DT_NEEDED
+ Value: 0xFFFFFFFFBE5A0B5F
+ - Tag: DT_STRTAB
+ Value: 0x474
+ - Tag: DT_NULL
+ Value: 0x0
diff --git a/llvm/tools/llvm-objdump/ELFDump.cpp b/llvm/tools/llvm-objdump/ELFDump.cpp
index fda99bd6d33e17..8c184fc1fbb66a 100644
--- a/llvm/tools/llvm-objdump/ELFDump.cpp
+++ b/llvm/tools/llvm-objdump/ELFDump.cpp
@@ -68,7 +68,7 @@ static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> &Elf) {
if (Dyn.d_tag == ELF::DT_STRTAB) {
auto MappedAddrOrError = Elf.toMappedAddr(Dyn.getPtr());
if (!MappedAddrOrError)
- consumeError(MappedAddrOrError.takeError());
+ return MappedAddrOrError.takeError();
return StringRef(reinterpret_cast<const char *>(*MappedAddrOrError));
}
}
@@ -223,7 +223,6 @@ template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
continue;
std::string Str = Elf.getDynamicTagAsString(Dyn.d_tag);
- outs() << format(TagFmt.c_str(), Str.c_str());
const char *Fmt =
ELFT::Is64Bits ? "0x%016" PRIx64 "\n" : "0x%08" PRIx64 "\n";
@@ -232,14 +231,16 @@ template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
Dyn.d_tag == ELF::DT_AUXILIARY || Dyn.d_tag == ELF::DT_FILTER) {
Expected<StringRef> StrTabOrErr = getDynamicStrTab(Elf);
if (StrTabOrErr) {
- const char *Data = StrTabOrErr.get().data();
- outs() << (Data + Dyn.d_un.d_val) << "\n";
+ const char *Data = StrTabOrErr->data();
+ outs() << format(TagFmt.c_str(), Str.c_str()) << Data + Dyn.getVal()
+ << "\n";
continue;
}
reportWarning(toString(StrTabOrErr.takeError()), Obj.getFileName());
consumeError(StrTabOrErr.takeError());
}
- outs() << format(Fmt, (uint64_t)Dyn.d_un.d_val);
+ outs() << format(TagFmt.c_str(), Str.c_str())
+ << format(Fmt, (uint64_t)Dyn.getVal());
}
}
More information about the llvm-commits
mailing list