Skip to content

Commit d7ecc02

Browse files
author
Georgii Rymar
committed
[Object/ELF] - Refine the error reported when section's offset + size overruns the file buffer.
This is a follow-up requested in comments for D70826. It changes the message from "section X has a sh_offset (Y) + sh_size (Z) that cannot be represented" to "section X has a sh_offset (Y) + sh_size (Z) that is greater than the file size (0xABC)" when section's sh_offset + sh_size overruns a file buffer. Differential revision: https://reviews.llvm.org/D70893
1 parent 1cc0ba4 commit d7ecc02

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

llvm/include/llvm/Object/ELF.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,17 @@ ELFFile<ELFT>::getSectionContentsAsArray(const Elf_Shdr *Sec) const {
402402
" has an invalid sh_size (" + Twine(Size) +
403403
") which is not a multiple of its sh_entsize (" +
404404
Twine(Sec->sh_entsize) + ")");
405-
if ((std::numeric_limits<uintX_t>::max() - Offset < Size) ||
406-
Offset + Size > Buf.size())
405+
if (std::numeric_limits<uintX_t>::max() - Offset < Size)
407406
return createError("section " + getSecIndexForError(this, Sec) +
408407
" has a sh_offset (0x" + Twine::utohexstr(Offset) +
409408
") + sh_size (0x" + Twine::utohexstr(Size) +
410409
") that cannot be represented");
410+
if (Offset + Size > Buf.size())
411+
return createError("section " + getSecIndexForError(this, Sec) +
412+
" has a sh_offset (0x" + Twine::utohexstr(Offset) +
413+
") + sh_size (0x" + Twine::utohexstr(Size) +
414+
") that is greater than the file size (0x" +
415+
Twine::utohexstr(Buf.size()) + ")");
411416

412417
if (Offset % alignof(T))
413418
// TODO: this error is untested.

llvm/test/Object/invalid.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Sections:
4141
# RUN: not llvm-objdump -s %p/Inputs/invalid-strtab-size.elf 2>&1 \
4242
# RUN: | FileCheck %s -DFILE=%p/Inputs/invalid-strtab-size.elf --check-prefix=INVALID-STRTAB-SIZE
4343

44-
# INVALID-STRTAB-SIZE: error: '[[FILE]]': section [index 1] has a sh_offset (0x70) + sh_size (0xffffff) that cannot be represented
44+
# INVALID-STRTAB-SIZE: error: '[[FILE]]': section [index 1] has a sh_offset (0x70) + sh_size (0xffffff) that is greater than the file size (0x218)
4545

4646
## Check that llvm-dwarfdump reports an error during relocation resolution
4747
## when instead of expected SHT_RELA section it locates a section of a different type.
@@ -252,7 +252,7 @@ Symbols: []
252252
# RUN: not llvm-readobj -r %t12 2>&1 | FileCheck -DFILE=%t12 --check-prefix=INVALID-RELOC-SH-OFFSET %s
253253
# RUN: not llvm-readobj -r %t13 2>&1 | FileCheck -DFILE=%t13 --check-prefix=INVALID-RELOC-SH-OFFSET %s
254254

255-
# INVALID-RELOC-SH-OFFSET: error: '[[FILE]]': section [index 1] has a sh_offset (0x10000) + sh_size (0x0) that cannot be represented
255+
# INVALID-RELOC-SH-OFFSET: error: '[[FILE]]': section [index 1] has a sh_offset (0x10000) + sh_size (0x0) that is greater than the file size (0x160)
256256

257257
--- !ELF
258258
FileHeader:
@@ -286,7 +286,7 @@ Sections:
286286

287287
--- !ELF
288288
FileHeader:
289-
Class: ELFCLASS64
289+
Class: ELFCLASS32
290290
Data: ELFDATA2LSB
291291
Type: ET_REL
292292
Machine: EM_386
@@ -375,7 +375,7 @@ Sections:
375375
# RUN: not llvm-readobj --sections --section-data %t18 2>&1 \
376376
# RUN: | FileCheck -DFILE=%t18 --check-prefix=BROKEN-SECSHOFFSET %s
377377

378-
# BROKEN-SECSHOFFSET: error: '[[FILE]]': section [index 1] has a sh_offset (0xffff0000) + sh_size (0x0) that cannot be represented
378+
# BROKEN-SECSHOFFSET: error: '[[FILE]]': section [index 1] has a sh_offset (0xffff0000) + sh_size (0x0) that is greater than the file size (0x160)
379379

380380
--- !ELF
381381
FileHeader:

llvm/test/tools/llvm-readobj/elf-verdef-invalid.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Sections:
5252
# RUN: llvm-readobj -V %t3 2>&1 | FileCheck %s --check-prefix=INVALID-DATA -DFILE=%t3
5353
# RUN: llvm-readelf -V %t3 2>&1 | FileCheck %s --check-prefix=INVALID-DATA -DFILE=%t3
5454

55-
# INVALID-DATA: warning: '[[FILE]]': cannot read content of SHT_GNU_verdef section with index 1: section [index 1] has a sh_offset (0xffffffff) + sh_size (0x0) that cannot be represented
55+
# INVALID-DATA: warning: '[[FILE]]': cannot read content of SHT_GNU_verdef section with index 1: section [index 1] has a sh_offset (0xffffffff) + sh_size (0x0) that is greater than the file size (0x230)
5656

5757
--- !ELF
5858
FileHeader:

llvm/test/tools/llvm-readobj/elf-verneed-invalid.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ DynamicSymbols:
372372
# RUN: llvm-readobj --sections -V %t7 2>&1 | FileCheck %s -DFILE=%t7 --check-prefix=INVALID-DATA
373373
# RUN: llvm-readelf --sections -V %t7 2>&1 | FileCheck %s -DFILE=%t7 --check-prefix=INVALID-DATA
374374

375-
# INVALID-DATA: warning: '[[FILE]]': cannot read content of SHT_GNU_verneed section with index 1: section [index 1] has a sh_offset (0xffffffff) + sh_size (0x0) that cannot be represented
375+
# INVALID-DATA: warning: '[[FILE]]': cannot read content of SHT_GNU_verneed section with index 1: section [index 1] has a sh_offset (0xffffffff) + sh_size (0x0) that is greater than the file size (0x230)
376376

377377
--- !ELF
378378
FileHeader:

0 commit comments

Comments
 (0)