[lld] r329063 - [ELF] - Check that output sections fit in address space.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 3 05:39:29 PDT 2018


Author: grimar
Date: Tue Apr  3 05:39:28 2018
New Revision: 329063

URL: http://llvm.org/viewvc/llvm-project?rev=329063&view=rev
Log:
[ELF] - Check that output sections fit in address space.

Added checks to test that we do not produce
output where VA of sections overruns the address
space available.

Differential revision: https://reviews.llvm.org/D43820

Added:
    lld/trunk/test/ELF/linkerscript/i386-sections-max-va-overflow.s
    lld/trunk/test/ELF/linkerscript/sections-max-va-overflow.s
Modified:
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/ELF/linkerscript/output-too-large.s

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=329063&r1=329062&r2=329063&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Apr  3 05:39:28 2018
@@ -2063,17 +2063,25 @@ static void checkOverlap(StringRef Name,
   }
 }
 
-// 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>::checkSectionOverlap() {
-  // 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)
+    if ((OS->Addr + OS->Size < OS->Addr) ||
+        (!ELFT::Is64Bits && OS->Addr + OS->Size > UINT32_MAX))
+      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.
   std::vector<SectionOffset> FileOffs;
   for (OutputSection *Sec : OutputSections)
     if (0 < Sec->Size && Sec->Type != SHT_NOBITS &&

Added: lld/trunk/test/ELF/linkerscript/i386-sections-max-va-overflow.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/i386-sections-max-va-overflow.s?rev=329063&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/i386-sections-max-va-overflow.s (added)
+++ lld/trunk/test/ELF/linkerscript/i386-sections-max-va-overflow.s Tue Apr  3 05:39:28 2018
@@ -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

Modified: lld/trunk/test/ELF/linkerscript/output-too-large.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/output-too-large.s?rev=329063&r1=329062&r2=329063&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/output-too-large.s (original)
+++ lld/trunk/test/ELF/linkerscript/output-too-large.s Tue Apr  3 05:39:28 2018
@@ -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

Added: lld/trunk/test/ELF/linkerscript/sections-max-va-overflow.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/sections-max-va-overflow.s?rev=329063&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/sections-max-va-overflow.s (added)
+++ lld/trunk/test/ELF/linkerscript/sections-max-va-overflow.s Tue Apr  3 05:39:28 2018
@@ -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




More information about the llvm-commits mailing list