[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
Thu Nov 26 00:47:19 PST 2020


grimar updated this revision to Diff 307778.
grimar marked 2 inline comments as done.
grimar added a comment.

- Addressed review comments.


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

https://reviews.llvm.org/D92098

Files:
  llvm/test/tools/obj2yaml/ELF/eshnum.yaml
  llvm/tools/obj2yaml/elf2yaml.cpp


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 no 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:   Class: ELFCLASS64
+# CHECK-NEXT:   Data:  ELFDATA2LSB
+# CHECK-NEXT:   Type:  ET_REL
+# CHECK-NEXT: ...
+
+--- !ELF
+FileHeader:
+  Class:  ELFCLASS64
+  Data:   ELFDATA2LSB
+  Type:   ET_REL
+  EShNum: 0x0
+Sections:
+  - Type: SHT_NULL
+    Size: 0x0


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92098.307778.patch
Type: text/x-patch
Size: 1799 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201126/ca38403b/attachment.bin>


More information about the llvm-commits mailing list