[PATCH] D38846: [ELF] - Linkerscript: fix issue with SUBALIGN.
George Rimar via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 18 02:53:49 PDT 2017
grimar updated this revision to Diff 119448.
grimar edited the summary of this revision.
grimar added a comment.
- Added "is power of 2" check.
https://reviews.llvm.org/D38846
Files:
ELF/ScriptParser.cpp
test/ELF/linkerscript/subalign.s
Index: test/ELF/linkerscript/subalign.s
===================================================================
--- test/ELF/linkerscript/subalign.s
+++ test/ELF/linkerscript/subalign.s
@@ -22,6 +22,22 @@
# SUBALIGN: 01000000 00000000 02000000 00000000
# SUBALIGN: 03000000 00000000 04000000 00000000
+## Test we do not fail when dot(.) is used inside SUBALIGN. That is not
+## consistent with ld.bfd.
+# RUN: echo "SECTIONS { . = 0x32; .aaa : SUBALIGN(.) { *(.aaa*) } }" > %t3.script
+# RUN: ld.lld %t1.o --script %t3.script -o %t3
+# RUN: llvm-objdump -s %t3 | FileCheck -check-prefix=SUBALIGN %s
+
+## Test we are able to link with zero alignment, this is consistent with bfd 2.26.1.
+# RUN: echo "SECTIONS { .aaa : SUBALIGN(0) { *(.aaa*) } }" > %t4.script
+# RUN: ld.lld %t1.o --script %t4.script -o %t4
+# RUN: llvm-objdump -s %t4 | FileCheck -check-prefix=SUBALIGN %s
+
+## Test we fail gracefuly when alignment value is not a power of 2.
+# RUN: echo "SECTIONS { .aaa : SUBALIGN(3) { *(.aaa*) } }" > %t5.script
+# RUN: not ld.lld %t1.o --script %t5.script -o %t5 2>&1 | FileCheck -check-prefix=ERR %s
+# ERR: {{.*}}.script:1: alignment must be power of 2
+
.global _start
_start:
nop
Index: ELF/ScriptParser.cpp
===================================================================
--- ELF/ScriptParser.cpp
+++ ELF/ScriptParser.cpp
@@ -642,20 +642,30 @@
}
}
+static Expr checkAlignment(Expr E, std::string &Loc) {
+ return [=] {
+ uint64_t Alignment = std::max((uint64_t)1, E().getValue());
+ if (!isPowerOf2_64(Alignment))
+ error(Loc + ": alignment must be power of 2");
+ return Alignment;
+ };
+}
+
OutputSection *ScriptParser::readOutputSectionDescription(StringRef OutSec) {
OutputSection *Cmd =
Script->createOutputSection(OutSec, getCurrentLocation());
if (peek() != ":")
readSectionAddressType(Cmd);
expect(":");
+ std::string Location = getCurrentLocation();
if (consume("AT"))
Cmd->LMAExpr = readParenExpr();
if (consume("ALIGN"))
- Cmd->AlignExpr = readParenExpr();
+ Cmd->AlignExpr = checkAlignment(readParenExpr(), Location);
if (consume("SUBALIGN"))
- Cmd->SubalignExpr = readParenExpr();
+ Cmd->SubalignExpr = checkAlignment(readParenExpr(), Location);
// Parse constraints.
if (consume("ONLY_IF_RO"))
@@ -959,16 +969,16 @@
if (Tok == "ALIGN") {
expect("(");
Expr E = readExpr();
- if (consume(")"))
- return [=] {
- return alignTo(Script->getDot(), std::max((uint64_t)1, E().getValue()));
- };
+ if (consume(")")) {
+ E = checkAlignment(E, Location);
+ return [=] { return alignTo(Script->getDot(), E().getValue()); };
+ }
expect(",");
- Expr E2 = readExpr();
+ Expr E2 = checkAlignment(readExpr(), Location);
expect(")");
return [=] {
ExprValue V = E();
- V.Alignment = std::max((uint64_t)1, E2().getValue());
+ V.Alignment = E2().getValue();
return V;
};
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38846.119448.patch
Type: text/x-patch
Size: 2975 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171018/2614c0f2/attachment.bin>
More information about the llvm-commits
mailing list