[PATCH] D43820: [ELF] - Check that output sections fit in address space.
George Rimar via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 27 06:56:53 PST 2018
grimar updated this revision to Diff 136073.
grimar added a comment.
- Addressed comments. Thanks, James !
https://reviews.llvm.org/D43820
Files:
ELF/Writer.cpp
test/ELF/linkerscript/i386-sections-max-va-overflow.s
test/ELF/linkerscript/output-too-large.s
test/ELF/linkerscript/sections-max-va-overflow.s
Index: test/ELF/linkerscript/sections-max-va-overflow.s
===================================================================
--- test/ELF/linkerscript/sections-max-va-overflow.s
+++ test/ELF/linkerscript/sections-max-va-overflow.s
@@ -0,0 +1,13 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+
+# RUN: echo "SECTIONS { . = 0xfffffffffffffff1;" > %t.script
+# RUN: echo " .bar : { *(.bar*) } }" >> %t.script
+# RUN: not ld.lld -o %t.so --script %t.script %t.o 2>&1 | FileCheck %s -check-prefix=ERR
+
+## .bar section has data in [0xfffffffffffffff1, 0xfffffffffffffff1 + 0x10] ==
+## [0xfffffffffffffff1, 0x1]. Check we can catch this overflow.
+# ERR: error: section .bar at 0xFFFFFFFFFFFFFFF1 of size 0x10 exceeds available address space
+
+.section .bar,"ax", at progbits
+.zero 0x10
Index: test/ELF/linkerscript/output-too-large.s
===================================================================
--- test/ELF/linkerscript/output-too-large.s
+++ test/ELF/linkerscript/output-too-large.s
@@ -1,7 +1,7 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t.o
# RUN: echo "SECTIONS { .text : { . = 0xffffffff; *(.text*); } }" > %t.script
-# RUN: not ld.lld --script %t.script %t.o -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld --no-check-sections --script %t.script %t.o -o %t 2>&1 | FileCheck %s
# CHECK: error: output file too large
.global _start
Index: test/ELF/linkerscript/i386-sections-max-va-overflow.s
===================================================================
--- test/ELF/linkerscript/i386-sections-max-va-overflow.s
+++ test/ELF/linkerscript/i386-sections-max-va-overflow.s
@@ -0,0 +1,13 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=i386-pc-linux %s -o %t.o
+
+# RUN: echo "SECTIONS { . = 0xfffffff1;" > %t.script
+# RUN: echo " .bar : { *(.bar*) } }" >> %t.script
+# RUN: not ld.lld -o %t.so --script %t.script %t.o 2>&1 | FileCheck %s -check-prefix=ERR
+
+## .bar section has data in [0xfffffff1, 0xfffffff1 + 0x10] == [0xffffff1, 0x1].
+## Check we can catch this overflow.
+# ERR: error: section .bar at 0xFFFFFFF1 of size 0x10 exceeds available address space
+
+.section .bar,"ax", at progbits
+.zero 0x10
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -1983,17 +1983,28 @@
}
}
-// Check for overlapping sections
+// Check for overlapping sections and address overflows.
//
// In this function we check that none of the output sections have overlapping
// file offsets. For SHF_ALLOC sections we also check that the load address
// ranges and the virtual address ranges don't overlap
template <class ELFT> void Writer<ELFT>::checkNoOverlappingSections() {
- // First check for overlapping file offsets. In this case we need to skip
- // Any section marked as SHT_NOBITS. These sections don't actually occupy
- // space in the file so Sec->Offset + Sec->Size can overlap with others.
- // If --oformat binary is specified only add SHF_ALLOC sections are added to
- // the output file so we skip any non-allocated sections in that case.
+ // First, check that section's VAs fit in available address space for target.
+ for (OutputSection *OS : OutputSections) {
+ bool Overflow = OS->Addr + OS->Size < OS->Addr;
+ if (!Config->Is64)
+ Overflow |= OS->Addr + OS->Size > UINT32_MAX;
+ if (Overflow)
+ errorOrWarn("section " + OS->Name + " at 0x" + utohexstr(OS->Addr) +
+ " of size 0x" + utohexstr(OS->Size) +
+ " exceeds available address space");
+ }
+
+ // Check for overlapping file offsets. In this case we need to skip any
+ // section marked as SHT_NOBITS. These sections don't actually occupy space in
+ // the file so Sec->Offset + Sec->Size can overlap with others. If --oformat
+ // binary is specified only add SHF_ALLOC sections are added to the output
+ // file so we skip any non-allocated sections in that case.
checkForSectionOverlap(
OutputSections, "file", [](const OutputSection *Sec) { return Sec->Offset; },
[](const OutputSection *Sec) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43820.136073.patch
Type: text/x-patch
Size: 4168 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180227/0b8ad827/attachment.bin>
More information about the llvm-commits
mailing list