[lld] r309953 - [ELF] - Do not segfault if linkerscript tries to access Target too early.
George Rimar via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 3 09:05:08 PDT 2017
Author: grimar
Date: Thu Aug 3 09:05:08 2017
New Revision: 309953
URL: http://llvm.org/viewvc/llvm-project?rev=309953&view=rev
Log:
[ELF] - Do not segfault if linkerscript tries to access Target too early.
Following possible scripts triggered accessing to Target when it was not yet
initialized (was nullptr).
MEMORY { name : ORIGIN = DATA_SEGMENT_RELRO_END; }
MEMORY { name : ORIGIN = CONSTANT(COMMONPAGESIZE); }
Patch errors out instead.
Differential revision: https://reviews.llvm.org/D36140
Added:
lld/trunk/test/ELF/linkerscript/memory-err.s
Modified:
lld/trunk/ELF/ScriptParser.cpp
Modified: lld/trunk/ELF/ScriptParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptParser.cpp?rev=309953&r1=309952&r2=309953&view=diff
==============================================================================
--- lld/trunk/ELF/ScriptParser.cpp (original)
+++ lld/trunk/ELF/ScriptParser.cpp Thu Aug 3 09:05:08 2017
@@ -90,6 +90,8 @@ private:
void readSort();
AssertCommand *readAssert();
Expr readAssertExpr();
+ Expr readConstant();
+ Expr getPageSize();
uint64_t readMemoryAssignment(StringRef, StringRef, StringRef);
std::pair<uint32_t, uint32_t> readMemoryAttributes();
@@ -793,13 +795,24 @@ Expr ScriptParser::readExpr1(Expr Lhs, i
return Lhs;
}
-uint64_t static getConstant(StringRef S) {
+Expr ScriptParser::getPageSize() {
+ std::string Location = getCurrentLocation();
+ return [=]() -> uint64_t {
+ if (Target)
+ return Target->PageSize;
+ error(Location + ": unable to calculate page size");
+ return 4096; // Return a dummy value.
+ };
+}
+
+Expr ScriptParser::readConstant() {
+ StringRef S = readParenLiteral();
if (S == "COMMONPAGESIZE")
- return Target->PageSize;
+ return getPageSize();
if (S == "MAXPAGESIZE")
- return Config->MaxPageSize;
- error("unknown constant: " + S);
- return 0;
+ return [] { return Config->MaxPageSize; };
+ setError("unknown constant: " + S);
+ return {};
}
// Parses Tok as an integer. It recognizes hexadecimal (prefixed with
@@ -919,10 +932,8 @@ Expr ScriptParser::readPrimary() {
}
if (Tok == "ASSERT")
return readAssertExpr();
- if (Tok == "CONSTANT") {
- StringRef Name = readParenLiteral();
- return [=] { return getConstant(Name); };
- }
+ if (Tok == "CONSTANT")
+ return readConstant();
if (Tok == "DATA_SEGMENT_ALIGN") {
expect("(");
Expr E = readExpr();
@@ -948,7 +959,8 @@ Expr ScriptParser::readPrimary() {
expect(",");
readExpr();
expect(")");
- return [] { return alignTo(Script->getDot(), Target->PageSize); };
+ Expr E = getPageSize();
+ return [=] { return alignTo(Script->getDot(), E().getValue()); };
}
if (Tok == "DEFINED") {
StringRef Name = readParenLiteral();
Added: lld/trunk/test/ELF/linkerscript/memory-err.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/memory-err.s?rev=309953&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/memory-err.s (added)
+++ lld/trunk/test/ELF/linkerscript/memory-err.s Thu Aug 3 09:05:08 2017
@@ -0,0 +1,10 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: echo "MEMORY { name : ORIGIN = DATA_SEGMENT_RELRO_END; }" > %t.script
+# RUN: not ld.lld -shared -o %t2 --script %t.script %t 2>&1 | FileCheck %s
+# CHECK: error: {{.*}}.script:1: unable to calculate page size
+
+# RUN: echo "MEMORY { name : ORIGIN = CONSTANT(COMMONPAGESIZE); }" > %t.script
+# RUN: not ld.lld -shared -o %t2 --script %t.script %t 2>&1 |\
+# RUN: FileCheck %s --check-prefix=ERR2
+# ERR2: error: {{.*}}.script:1: unable to calculate page size
More information about the llvm-commits
mailing list