[PATCH] D77289: [Object] Fix crash caused by unhandled error.

Xing GUO via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 2 02:08:49 PDT 2020


Higuoxing updated this revision to Diff 254451.
Higuoxing added a comment.

Apply `clang-format`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77289/new/

https://reviews.llvm.org/D77289

Files:
  llvm/include/llvm/Object/ELFObjectFile.h
  llvm/test/Object/nm-invalid-section-size.test


Index: llvm/test/Object/nm-invalid-section-size.test
===================================================================
--- /dev/null
+++ llvm/test/Object/nm-invalid-section-size.test
@@ -0,0 +1,22 @@
+## This test ensures llvm-nm will not crash when dumping a symbol table
+## whose sh_size isn't a multiple of the symbol size (sh_size % sizeof(Elf_Sym) != 0).
+## TODO: It will be helpful if we could warn about this in future.
+
+# RUN: yaml2obj -DBITS=32 -DSIZE=33 %s -o %t.32-bit.o
+# RUN: llvm-nm %t.32-bit.o 2>&1 | FileCheck --implicit-check-not=warning %s
+# RUN: yaml2obj -DBITS=64 -DSIZE=49 %s -o %t.64-bit.o
+# RUN: llvm-nm %t.64-bit.o 2>&1 | FileCheck --implicit-check-not=warning %s
+
+#       CHECK: U
+# CHECK-EMPTY:
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS[[BITS]]
+  Data:    ELFDATA2LSB
+  Type:    ET_DYN
+  Machine: EM_386
+Sections:
+  - Name: .symtab
+    Type: SHT_SYMTAB
+    Size: [[SIZE]]
Index: llvm/include/llvm/Object/ELFObjectFile.h
===================================================================
--- llvm/include/llvm/Object/ELFObjectFile.h
+++ llvm/include/llvm/Object/ELFObjectFile.h
@@ -238,6 +238,7 @@
   using Elf_Rel = typename ELFT::Rel;
   using Elf_Rela = typename ELFT::Rela;
   using Elf_Dyn = typename ELFT::Dyn;
+  using Elf_Sym_Range = typename ELFT::SymRange;
 
   SectionRef toSectionRef(const Elf_Shdr *Sec) const {
     return SectionRef(toDRI(Sec), this);
@@ -625,12 +626,20 @@
   if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
     Result |= SymbolRef::SF_FormatSpecific;
 
-  auto DotSymtabSecSyms = EF.symbols(DotSymtabSec);
-  if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin())
-    Result |= SymbolRef::SF_FormatSpecific;
-  auto DotDynSymSecSyms = EF.symbols(DotDynSymSec);
-  if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin())
-    Result |= SymbolRef::SF_FormatSpecific;
+  if (Expected<Elf_Sym_Range> DotSymtabSecSymsOrErr =
+          EF.symbols(DotSymtabSec)) {
+    if (ESym == DotSymtabSecSymsOrErr->begin())
+      Result |= SymbolRef::SF_FormatSpecific;
+  } else
+    // TODO: Actually report errors helpfully.
+    consumeError(DotSymtabSecSymsOrErr.takeError());
+  if (Expected<Elf_Sym_Range> DotDynSymSecSymsOrErr =
+          EF.symbols(DotDynSymSec)) {
+    if (ESym == DotDynSymSecSymsOrErr->begin())
+      Result |= SymbolRef::SF_FormatSpecific;
+  } else
+    // TODO: Actually report errors helpfully.
+    consumeError(DotDynSymSecSymsOrErr.takeError());
 
   if (EF.getHeader()->e_machine == ELF::EM_ARM) {
     if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77289.254451.patch
Type: text/x-patch
Size: 2609 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200402/9231ee69/attachment.bin>


More information about the llvm-commits mailing list