[PATCH] D92098: [obj2yaml] - Don't crash when dumping an object with no sections.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 25 06:19:23 PST 2020

grimar created this revision.
grimar added reviewers: jhenderson, MaskRay.
Herald added a subscriber: emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.
grimar requested review of this revision.

When an object has e_shoff != 0 and no sections, obj2yaml crashes.
This patch fixes the issue.



Index: llvm/tools/obj2yaml/elf2yaml.cpp
--- llvm/tools/obj2yaml/elf2yaml.cpp
+++ llvm/tools/obj2yaml/elf2yaml.cpp
@@ -237,6 +237,9 @@
                                ArrayRef<ELFYAML::ProgramHeader> Phdrs,
                                std::vector<std::unique_ptr<ELFYAML::Chunk>> &V,
                                ArrayRef<typename ELFT::Shdr> S) {
+  if (V.empty())
+    return;
   uint64_t ExpectedOffset;
   if (Header.e_phoff > 0)
     ExpectedOffset = Header.e_phoff + Header.e_phentsize * Header.e_phnum;
Index: llvm/test/tools/obj2yaml/ELF/eshnum.yaml
--- /dev/null
+++ llvm/test/tools/obj2yaml/ELF/eshnum.yaml
@@ -0,0 +1,30 @@
+## Check cases related to the value of the e_shnum field of the ELF header live here.
+## Normally an object that does not have sections has e_shoff == 0.
+## Also, e_shoff might be 0, when the the number of entries in the section
+## header table is larger than or equal to SHN_LORESERVE (0xff00). In this case
+## the real number of entries in the section header table is held in the sh_size
+## member of the initial entry in section header table.
+## In the test case below we have an object with a non-zero section header table
+## file offset and the zero number of sections. Here we check that we are
+## able to dump such objects properly.
+# RUN: yaml2obj %s -o %t
+# RUN: obj2yaml %t | FileCheck %s
+# CHECK:      --- !ELF
+# CHECK-NEXT: FileHeader:
+# CHECK-NEXT:   Type:  ET_REL
+# CHECK-NEXT: ...
+--- !ELF
+  Class:  ELFCLASS64
+  Data:   ELFDATA2LSB
+  Type:   ET_REL
+  EShNum: 0x0
+  - Type: SHT_NULL
+    Size: 0x0

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92098.307599.patch
Type: text/x-patch
Size: 1815 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201125/6f308334/attachment.bin>

More information about the llvm-commits mailing list