Skip to content

Commit 630c617

Browse files
author
George Rimar
committedJul 26, 2016
[ELF] - Linkerscript: implemented ALIGN modificatior of output sections.
Output section description can contain ALIGN modificator: https://sourceware.org/binutils/docs/ld/Output-Section-Description.html#Output-Section-Description Patch implements it. Differential revision: https://reviews.llvm.org/D22674 llvm-svn: 276780
1 parent cb42ea0 commit 630c617

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed
 

‎lld/ELF/LinkerScript.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ void LinkerScript<ELFT>::assignAddresses(
226226
if (Cmd->AddrExpr)
227227
Dot = Cmd->AddrExpr(Dot);
228228

229+
if (Cmd->AlignExpr)
230+
Sec->updateAlignment(Cmd->AlignExpr(Dot));
231+
229232
if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
230233
uintX_t TVA = Dot + ThreadBssOffset;
231234
TVA = alignTo(TVA, Sec->getAlignment());
@@ -433,6 +436,7 @@ class elf::ScriptParser : public ScriptParserBase {
433436
std::vector<StringRef> readOutputSectionPhdrs();
434437
unsigned readPhdrType();
435438
void readProvide(bool Hidden);
439+
void readAlign(OutputSectionCommand *Cmd);
436440

437441
Expr readExpr();
438442
Expr readExpr1(Expr Lhs, int MinPrec);
@@ -671,6 +675,12 @@ void ScriptParser::readKeep(OutputSectionCommand *Cmd) {
671675
expect(")");
672676
}
673677

678+
void ScriptParser::readAlign(OutputSectionCommand *Cmd) {
679+
expect("(");
680+
Cmd->AlignExpr = readExpr();
681+
expect(")");
682+
}
683+
674684
void ScriptParser::readOutputSectionDescription(StringRef OutSec) {
675685
OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
676686
Opt.Commands.emplace_back(Cmd);
@@ -682,6 +692,9 @@ void ScriptParser::readOutputSectionDescription(StringRef OutSec) {
682692

683693
expect(":");
684694

695+
if (skip("ALIGN"))
696+
readAlign(Cmd);
697+
685698
// Parse constraints.
686699
if (skip("ONLY_IF_RO"))
687700
Cmd->Constraint = ConstraintKind::ReadOnly;

‎lld/ELF/LinkerScript.h

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct OutputSectionCommand : BaseCommand {
7373
static bool classof(const BaseCommand *C);
7474
StringRef Name;
7575
Expr AddrExpr;
76+
Expr AlignExpr;
7677
std::vector<std::unique_ptr<BaseCommand>> Commands;
7778
std::vector<StringRef> Phdrs;
7879
std::vector<uint8_t> Filler;

‎lld/test/ELF/linkerscript/linkerscript-align.s

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# REQUIRES: x86
22
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
33

4+
## Check that ALIGN command workable using location counter
45
# RUN: echo "SECTIONS { \
56
# RUN: . = 0x10000; \
67
# RUN: .aaa : \
@@ -27,6 +28,25 @@
2728
# CHECK-NEXT: 2 .bbb 00000008 0000000000011000 DATA
2829
# CHECK-NEXT: 3 .ccc 00000008 0000000000014000 DATA
2930

31+
## Check output sections ALIGN modificator
32+
# RUN: echo "SECTIONS { \
33+
# RUN: . = 0x10000; \
34+
# RUN: .aaa : \
35+
# RUN: { \
36+
# RUN: *(.aaa) \
37+
# RUN: } \
38+
# RUN: .bbb : ALIGN(4096) \
39+
# RUN: { \
40+
# RUN: *(.bbb) \
41+
# RUN: } \
42+
# RUN: .ccc : ALIGN(4096 * 4) \
43+
# RUN: { \
44+
# RUN: *(.ccc) \
45+
# RUN: } \
46+
# RUN: }" > %t2.script
47+
# RUN: ld.lld -o %t2 --script %t2.script %t
48+
# RUN: llvm-objdump -section-headers %t1 | FileCheck %s
49+
3050
.global _start
3151
_start:
3252
nop

0 commit comments

Comments
 (0)