Skip to content

Commit 6cd47f9

Browse files
SidManningMaskRay
authored andcommitted
[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
1 parent f1b4c4b commit 6cd47f9

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

llvm/include/llvm/Object/ELFObjectFile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ template <class ELFT>
723723
Expected<ArrayRef<uint8_t>>
724724
ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec) const {
725725
const Elf_Shdr *EShdr = getSection(Sec);
726+
if (EShdr->sh_type == ELF::SHT_NOBITS)
727+
return makeArrayRef((const uint8_t *)base(), 0);
726728
if (std::error_code EC =
727729
checkOffset(getMemoryBufferRef(),
728730
(uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Check that when BSS is larger than the file llvm-objdump doesn't
2+
## assert with an unexpected end of file error.
3+
# RUN: yaml2obj --docnum=1 %s > %t
4+
# RUN: yaml2obj --docnum=2 %s > %t.2
5+
# RUN: llvm-objdump -D %t | FileCheck %s
6+
# RUN: llvm-objdump -D %t.2 | FileCheck %s
7+
8+
# CHECK: Disassembly of section .bss:
9+
# CHECK: .bss:
10+
# CHECK-NEXT: ...
11+
12+
--- !ELF
13+
FileHeader:
14+
Class: ELFCLASS64
15+
Data: ELFDATA2LSB
16+
Type: ET_EXEC
17+
Machine: EM_X86_64
18+
Sections:
19+
- Name: .bss
20+
Type: SHT_NOBITS
21+
Flags: [ SHF_WRITE, SHF_ALLOC ]
22+
Size: 0x0000000000001000
23+
...
24+
25+
--- !ELF
26+
FileHeader:
27+
Class: ELFCLASS64
28+
Data: ELFDATA2LSB
29+
Type: ET_EXEC
30+
Machine: EM_X86_64
31+
Sections:
32+
- Name: .bss
33+
Type: SHT_NOBITS
34+
Flags: [ SHF_WRITE, SHF_ALLOC ]
35+
Size: 0x0000000000001000
36+
ShOffset: 0x0000000080000000
37+
...

0 commit comments

Comments
 (0)