[lld] r368145 - Handle /align option.
Rui Ueyama via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 7 03:16:22 PDT 2019
Author: ruiu
Date: Wed Aug 7 03:16:21 2019
New Revision: 368145
URL: http://llvm.org/viewvc/llvm-project?rev=368145&view=rev
Log:
Handle /align option.
Differential Revision: https://reviews.llvm.org/D65736
Added:
lld/trunk/test/COFF/align.s
Modified:
lld/trunk/COFF/Config.h
lld/trunk/COFF/Driver.cpp
lld/trunk/COFF/Writer.cpp
Modified: lld/trunk/COFF/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Config.h?rev=368145&r1=368144&r2=368145&view=diff
==============================================================================
--- lld/trunk/COFF/Config.h (original)
+++ lld/trunk/COFF/Config.h Wed Aug 7 03:16:21 2019
@@ -189,6 +189,7 @@ struct Configuration {
// Used for /thinlto-object-suffix-replace:
std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
+ uint64_t align = 4096;
uint64_t imageBase = -1;
uint64_t fileAlign = 512;
uint64_t stackReserve = 1024 * 1024;
Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=368145&r1=368144&r2=368145&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Wed Aug 7 03:16:21 2019
@@ -36,6 +36,7 @@
#include "llvm/Option/Option.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/LEB128.h"
+#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/TarWriter.h"
@@ -1422,6 +1423,13 @@ void LinkerDriver::link(ArrayRef<const c
for (auto *arg : args.filtered(OPT_section))
parseSection(arg->getValue());
+ // Handle /align
+ if (auto *arg = args.getLastArg(OPT_align)) {
+ parseNumbers(arg->getValue(), &config->align);
+ if (!isPowerOf2_64(config->align))
+ error("/align: not a power of two: " + StringRef(arg->getValue()));
+ }
+
// Handle /aligncomm
for (auto *arg : args.filtered(OPT_aligncomm))
parseAligncomm(arg->getValue());
Modified: lld/trunk/COFF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=368145&r1=368144&r2=368145&view=diff
==============================================================================
--- lld/trunk/COFF/Writer.cpp (original)
+++ lld/trunk/COFF/Writer.cpp Wed Aug 7 03:16:21 2019
@@ -1205,9 +1205,11 @@ void Writer::assignAddresses() {
sizeOfHeaders +=
config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
sizeOfHeaders = alignTo(sizeOfHeaders, config->fileAlign);
- uint64_t rva = pageSize; // The first page is kept unmapped.
fileSize = sizeOfHeaders;
+ // The first page is kept unmapped.
+ uint64_t rva = alignTo(sizeOfHeaders, config->align);
+
for (OutputSection *sec : outputSections) {
if (sec == relocSec)
addBaserels();
@@ -1237,10 +1239,10 @@ void Writer::assignAddresses() {
sec->header.SizeOfRawData = rawSize;
if (rawSize != 0)
sec->header.PointerToRawData = fileSize;
- rva += alignTo(virtualSize, pageSize);
+ rva += alignTo(virtualSize, config->align);
fileSize += alignTo(rawSize, config->fileAlign);
}
- sizeOfImage = alignTo(rva, pageSize);
+ sizeOfImage = alignTo(rva, config->align);
// Assign addresses to sections in MergeChunks.
for (MergeChunk *mc : MergeChunk::instances)
@@ -1309,7 +1311,7 @@ template <typename PEHeaderTy> void Writ
pe->MinorLinkerVersion = 0;
pe->ImageBase = config->imageBase;
- pe->SectionAlignment = pageSize;
+ pe->SectionAlignment = config->align;
pe->FileAlignment = config->fileAlign;
pe->MajorImageVersion = config->majorImageVersion;
pe->MinorImageVersion = config->minorImageVersion;
Added: lld/trunk/test/COFF/align.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/align.s?rev=368145&view=auto
==============================================================================
--- lld/trunk/test/COFF/align.s (added)
+++ lld/trunk/test/COFF/align.s Wed Aug 7 03:16:21 2019
@@ -0,0 +1,45 @@
+# RUN: yaml2obj < %s > %t.obj
+# RUN: lld-link /out:%t.exe /entry:main /align:32 %t.obj
+# RUN: llvm-readobj --file-headers %t.exe | FileCheck %s
+
+# CHECK: SectionAlignment: 32
+
+--- !COFF
+header:
+ Machine: IMAGE_FILE_MACHINE_AMD64
+ Characteristics: []
+sections:
+ - Name: .text
+ Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+ Alignment: 4096
+ SectionData: 0000000000000000
+ Relocations:
+ - VirtualAddress: 0
+ SymbolName: __ImageBase
+ Type: IMAGE_REL_AMD64_ADDR64
+symbols:
+ - Name: .text
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ SectionDefinition:
+ Length: 8
+ NumberOfRelocations: 1
+ NumberOfLinenumbers: 0
+ CheckSum: 0
+ Number: 0
+ - Name: main
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+ - Name: __ImageBase
+ Value: 0
+ SectionNumber: 0
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+...
More information about the llvm-commits
mailing list