[ELF] - Do not crash when ALIGN/DATA_SEGMENT_ALIGN expression used with zero value.

Previously we would crash when tried to ALIGN(0).
Patch uses value 1 instead in this case, that
looks to be consistent with GNU linkers
and reasonable and simple behavior itself.

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

llvm-svn: 309372
This commit is contained in:
George Rimar 2017-07-28 09:27:49 +00:00
parent 5804364f7a
commit 60833f6e22
3 changed files with 24 additions and 3 deletions

View File

@ -900,13 +900,15 @@ Expr ScriptParser::readPrimary() {
expect("(");
Expr E = readExpr();
if (consume(")"))
return [=] { return alignTo(Script->getDot(), E().getValue()); };
return [=] {
return alignTo(Script->getDot(), std::max((uint64_t)1, E().getValue()));
};
expect(",");
Expr E2 = readExpr();
expect(")");
return [=] {
ExprValue V = E();
V.Alignment = E2().getValue();
V.Alignment = std::max((uint64_t)1, E2().getValue());
return V;
};
}
@ -927,7 +929,9 @@ Expr ScriptParser::readPrimary() {
expect(",");
readExpr();
expect(")");
return [=] { return alignTo(Script->getDot(), E().getValue()); };
return [=] {
return alignTo(Script->getDot(), std::max((uint64_t)1, E().getValue()));
};
}
if (Tok == "DATA_SEGMENT_END") {
expect("(");

View File

@ -66,6 +66,21 @@
# SYMBOLS-NEXT: 0000000000011000 .bbb 00000000 __start_bbb
# SYMBOLS-NEXT: 0000000000012000 .bbb 00000000 __end_bbb
## Check that ALIGN zero do nothing and does not crash #1.
# RUN: echo "SECTIONS { . = ALIGN(0x123, 0); .aaa : { *(.aaa) } }" > %t.script
# RUN: ld.lld -o %t4 --script %t.script %t
# RUN: llvm-objdump -section-headers %t4 | FileCheck %s -check-prefix=ZERO
# ZERO: Sections:
# ZERO-NEXT: Idx Name Size Address Type
# ZERO-NEXT: 0 00000000 0000000000000000
# ZERO-NEXT: 1 .aaa 00000008 0000000000000123 DATA
## Check that ALIGN zero do nothing and does not crash #2.
# RUN: echo "SECTIONS { . = 0x123; . = ALIGN(0); .aaa : { *(.aaa) } }" > %t.script
# RUN: ld.lld -o %t5 --script %t.script %t
# RUN: llvm-objdump -section-headers %t5 | FileCheck %s -check-prefix=ZERO
.global _start
_start:
nop

View File

@ -25,6 +25,7 @@
# RUN: commonpagesize = CONSTANT (COMMONPAGESIZE); \
# RUN: . = 0xfff0; \
# RUN: datasegmentalign = DATA_SEGMENT_ALIGN (0xffff, 0); \
# RUN: datasegmentalign2 = DATA_SEGMENT_ALIGN (0, 0); \
# RUN: }" > %t.script
# RUN: ld.lld %t --script %t.script -o %t2
# RUN: llvm-objdump -t %t2 | FileCheck %s
@ -51,6 +52,7 @@
# CHECK: 00000000001000 *ABS* 00000000 maxpagesize
# CHECK: 00000000001000 *ABS* 00000000 commonpagesize
# CHECK: 0000000000ffff *ABS* 00000000 datasegmentalign
# CHECK: 0000000000fff0 *ABS* 00000000 datasegmentalign2
## Mailformed number error.
# RUN: echo "SECTIONS { . = 0x12Q41; }" > %t.script