Skip to content

Commit ab6d19b

Browse files
committed
Merging r368145:
------------------------------------------------------------------------ r368145 | ruiu | 2019-08-07 12:16:21 +0200 (Wed, 07 Aug 2019) | 3 lines Handle /align option. Differential Revision: https://reviews.llvm.org/D65736 ------------------------------------------------------------------------ llvm-svn: 369743
1 parent 6e6e769 commit ab6d19b

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

lld/COFF/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ struct Configuration {
189189
// Used for /thinlto-object-suffix-replace:
190190
std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
191191

192+
uint64_t align = 4096;
192193
uint64_t imageBase = -1;
193194
uint64_t fileAlign = 512;
194195
uint64_t stackReserve = 1024 * 1024;

lld/COFF/Driver.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/Option/Option.h"
3737
#include "llvm/Support/Debug.h"
3838
#include "llvm/Support/LEB128.h"
39+
#include "llvm/Support/MathExtras.h"
3940
#include "llvm/Support/Path.h"
4041
#include "llvm/Support/Process.h"
4142
#include "llvm/Support/TarWriter.h"
@@ -1421,6 +1422,13 @@ void LinkerDriver::link(ArrayRef<const char *> argsArr) {
14211422
for (auto *arg : args.filtered(OPT_section))
14221423
parseSection(arg->getValue());
14231424

1425+
// Handle /align
1426+
if (auto *arg = args.getLastArg(OPT_align)) {
1427+
parseNumbers(arg->getValue(), &config->align);
1428+
if (!isPowerOf2_64(config->align))
1429+
error("/align: not a power of two: " + StringRef(arg->getValue()));
1430+
}
1431+
14241432
// Handle /aligncomm
14251433
for (auto *arg : args.filtered(OPT_aligncomm))
14261434
parseAligncomm(arg->getValue());

lld/COFF/Writer.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,9 +1205,11 @@ void Writer::assignAddresses() {
12051205
sizeOfHeaders +=
12061206
config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
12071207
sizeOfHeaders = alignTo(sizeOfHeaders, config->fileAlign);
1208-
uint64_t rva = pageSize; // The first page is kept unmapped.
12091208
fileSize = sizeOfHeaders;
12101209

1210+
// The first page is kept unmapped.
1211+
uint64_t rva = alignTo(sizeOfHeaders, config->align);
1212+
12111213
for (OutputSection *sec : outputSections) {
12121214
if (sec == relocSec)
12131215
addBaserels();
@@ -1237,10 +1239,10 @@ void Writer::assignAddresses() {
12371239
sec->header.SizeOfRawData = rawSize;
12381240
if (rawSize != 0)
12391241
sec->header.PointerToRawData = fileSize;
1240-
rva += alignTo(virtualSize, pageSize);
1242+
rva += alignTo(virtualSize, config->align);
12411243
fileSize += alignTo(rawSize, config->fileAlign);
12421244
}
1243-
sizeOfImage = alignTo(rva, pageSize);
1245+
sizeOfImage = alignTo(rva, config->align);
12441246

12451247
// Assign addresses to sections in MergeChunks.
12461248
for (MergeChunk *mc : MergeChunk::instances)
@@ -1309,7 +1311,7 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
13091311
pe->MinorLinkerVersion = 0;
13101312

13111313
pe->ImageBase = config->imageBase;
1312-
pe->SectionAlignment = pageSize;
1314+
pe->SectionAlignment = config->align;
13131315
pe->FileAlignment = config->fileAlign;
13141316
pe->MajorImageVersion = config->majorImageVersion;
13151317
pe->MinorImageVersion = config->minorImageVersion;

lld/test/COFF/align.s

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# RUN: yaml2obj < %s > %t.obj
2+
# RUN: lld-link /out:%t.exe /entry:main /align:32 %t.obj
3+
# RUN: llvm-readobj --file-headers %t.exe | FileCheck %s
4+
5+
# CHECK: SectionAlignment: 32
6+
7+
--- !COFF
8+
header:
9+
Machine: IMAGE_FILE_MACHINE_AMD64
10+
Characteristics: []
11+
sections:
12+
- Name: .text
13+
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
14+
Alignment: 4096
15+
SectionData: 0000000000000000
16+
Relocations:
17+
- VirtualAddress: 0
18+
SymbolName: __ImageBase
19+
Type: IMAGE_REL_AMD64_ADDR64
20+
symbols:
21+
- Name: .text
22+
Value: 0
23+
SectionNumber: 1
24+
SimpleType: IMAGE_SYM_TYPE_NULL
25+
ComplexType: IMAGE_SYM_DTYPE_NULL
26+
StorageClass: IMAGE_SYM_CLASS_STATIC
27+
SectionDefinition:
28+
Length: 8
29+
NumberOfRelocations: 1
30+
NumberOfLinenumbers: 0
31+
CheckSum: 0
32+
Number: 0
33+
- Name: main
34+
Value: 0
35+
SectionNumber: 1
36+
SimpleType: IMAGE_SYM_TYPE_NULL
37+
ComplexType: IMAGE_SYM_DTYPE_NULL
38+
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
39+
- Name: __ImageBase
40+
Value: 0
41+
SectionNumber: 0
42+
SimpleType: IMAGE_SYM_TYPE_NULL
43+
ComplexType: IMAGE_SYM_DTYPE_NULL
44+
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
45+
...

0 commit comments

Comments
 (0)