Skip to content

Commit da92dcc

Browse files
int3Debadri Basak
authored andcommitted
[lld][macho] Error out gracefully when offset is outside literal section (llvm#164660)
We typically shouldn't get this, but when we do (e.g. in llvm#139439) we should error out gracefully instead of crashing. Note that we are stricter than ld64 here; ld64 appears to be able to handle section offsets that point outside literal sections if the end result is a valid pointer to another section in the input object file. Supporting this would probably be a pain given our current design, and it seems like enough of an edge case that it's onot worth it.
1 parent 212c2a6 commit da92dcc

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lld/MachO/InputSection.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ WordLiteralInputSection::WordLiteralInputSection(const Section &section,
348348
}
349349

350350
uint64_t WordLiteralInputSection::getOffset(uint64_t off) const {
351+
if (off >= data.size())
352+
fatal(toString(this) + ": offset is outside the section");
353+
351354
auto *osec = cast<WordLiteralSection>(parent);
352355
const uintptr_t buf = reinterpret_cast<uintptr_t>(data.data());
353356
switch (sectionType(getFlags())) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Test that we properly detect and report out-of-bounds offsets in literal sections.
2+
## We're intentionally testing fatal errors (for malformed input files), and
3+
## fatal errors aren't supported for testing when main is run twice.
4+
# XFAIL: main-run-twice
5+
6+
# REQUIRES: x86
7+
# RUN: rm -rf %t; split-file %s %t
8+
9+
## Test WordLiteralInputSection bounds checking
10+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/word-literal.s -o %t/word-literal.o
11+
# RUN: not %lld -dylib %t/word-literal.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=WORD
12+
13+
## Test CStringInputSection bounds checking
14+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/cstring.s -o %t/cstring.o
15+
# RUN: not %lld -dylib %t/cstring.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=CSTRING
16+
17+
# WORD: error: {{.*}}word-literal.o:(__literal4): offset is outside the section
18+
# CSTRING: error: {{.*}}cstring.o:(__cstring): offset is outside the section
19+
20+
#--- word-literal.s
21+
.section __TEXT,__literal4,4byte_literals
22+
L_literal:
23+
.long 0x01020304
24+
25+
.text
26+
.globl _main
27+
_main:
28+
# We use a subtractor expression to force a section relocation. Symbol relocations
29+
# don't trigger the error.
30+
.long L_literal - _main + 4
31+
32+
.subsections_via_symbols
33+
34+
#--- cstring.s
35+
## Create a cstring section with a reference that points past the end
36+
.cstring
37+
L_str:
38+
.asciz "foo"
39+
40+
.text
41+
.globl _main
42+
_main:
43+
.long L_str - _main + 4
44+
45+
.subsections_via_symbols

0 commit comments

Comments
 (0)