diff options
author | Hans Wennborg <hans@hanshq.net> | 2019-08-23 09:44:42 +0000 |
---|---|---|
committer | Hans Wennborg <hans@hanshq.net> | 2019-08-23 09:44:42 +0000 |
commit | 58617997a173cf6c5ffc624e09cdd219a1bf6fd5 (patch) | |
tree | 71171dcf6e5a36c056618cf60f50d34e8199b752 | |
parent | 8d37f6250a7500cc85dd5b95a58dbb6f3fa4969d (diff) |
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
------------------------------------------------------------------------
git-svn-id: https://llvm.org/svn/llvm-project/lld/branches/release_90@369743 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | COFF/Config.h | 1 | ||||
-rw-r--r-- | COFF/Driver.cpp | 8 | ||||
-rw-r--r-- | COFF/Writer.cpp | 10 | ||||
-rw-r--r-- | test/COFF/align.s | 45 |
4 files changed, 60 insertions, 4 deletions
diff --git a/COFF/Config.h b/COFF/Config.h index 1b0e24042..4b62cd05f 100644 --- a/COFF/Config.h +++ b/COFF/Config.h @@ -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; diff --git a/COFF/Driver.cpp b/COFF/Driver.cpp index 7214d12bd..467eef232 100644 --- a/COFF/Driver.cpp +++ b/COFF/Driver.cpp @@ -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" @@ -1421,6 +1422,13 @@ void LinkerDriver::link(ArrayRef<const char *> argsArr) { 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()); diff --git a/COFF/Writer.cpp b/COFF/Writer.cpp index cc75db0f5..9bc2f092c 100644 --- a/COFF/Writer.cpp +++ b/COFF/Writer.cpp @@ -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 Writer::writeHeader() { 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; diff --git a/test/COFF/align.s b/test/COFF/align.s new file mode 100644 index 000000000..67ca349e0 --- /dev/null +++ b/test/COFF/align.s @@ -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 +... |