[llvm] 6cd47f9 - [llvm-objdump] Fix spurious "The end of the file was unexpectedly encountered" if a SHT_NOBITS sh_offset is larger than the file size

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 5 11:14:48 PST 2019


Author: Sid Manning
Date: 2019-11-05T11:14:12-08:00
New Revision: 6cd47f9dd7dd664ff855fb0d1ed26bf5e4bb77fc

URL: https://github.com/llvm/llvm-project/commit/6cd47f9dd7dd664ff855fb0d1ed26bf5e4bb77fc
DIFF: https://github.com/llvm/llvm-project/commit/6cd47f9dd7dd664ff855fb0d1ed26bf5e4bb77fc.diff

LOG: [llvm-objdump] Fix spurious "The end of the file was unexpectedly encountered" if a SHT_NOBITS sh_offset is larger than the file size

llvm-objdump -D this file:

  int a[100000];
  int main() { return 0; }

Will produce an error: "The end of the file was unexpectedly encountered".

This happens because of a check in Binary.h checkOffset.  (Addr + Size > M.getBufferEnd()).

The sh_offset and sh_size fields can be ignored for SHT_NOBITS sections.
Fix the error by changing ELFObjectFile<ELFT>::getSectionContents to use
the file base for SHT_NOBITS sections.

Reviewed By: grimar, MaskRay

Differential Revision: https://reviews.llvm.org/D69192

Added: 
    llvm/test/tools/llvm-objdump/X86/elf-disassemble-bss.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 424289a9ccaa..8a68e49477fd 100644
--- a/llvm/include/llvm/Object/ELFObjectFile.h
+++ b/llvm/include/llvm/Object/ELFObjectFile.h
@@ -723,6 +723,8 @@ template <class ELFT>
 Expected<ArrayRef<uint8_t>>
 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec) const {
   const Elf_Shdr *EShdr = getSection(Sec);
+  if (EShdr->sh_type == ELF::SHT_NOBITS)
+    return makeArrayRef((const uint8_t *)base(), 0);
   if (std::error_code EC =
           checkOffset(getMemoryBufferRef(),
                       (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))

diff  --git a/llvm/test/tools/llvm-objdump/X86/elf-disassemble-bss.test b/llvm/test/tools/llvm-objdump/X86/elf-disassemble-bss.test
new file mode 100644
index 000000000000..01d3a49c9b51
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/X86/elf-disassemble-bss.test
@@ -0,0 +1,37 @@
+## Check that when BSS is larger than the file llvm-objdump doesn't
+## assert with an unexpected end of file error.
+# RUN: yaml2obj --docnum=1 %s > %t
+# RUN: yaml2obj --docnum=2 %s > %t.2
+# RUN: llvm-objdump -D %t | FileCheck %s
+# RUN: llvm-objdump -D %t.2 | FileCheck %s
+
+# CHECK: Disassembly of section .bss:
+# CHECK: .bss:
+# CHECK-NEXT: ...
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_EXEC
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .bss
+    Type:            SHT_NOBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    Size:            0x0000000000001000
+...
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_EXEC
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .bss
+    Type:            SHT_NOBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    Size:            0x0000000000001000
+    ShOffset:        0x0000000080000000
+...


        


More information about the llvm-commits mailing list