[ELF] Set Dot initially to --image-base value when using linker scripts
When parsing linker scripts, LLD previously started with a '.' value of 0,
regardless of the internal default image base for the target, and regardless of
switches such as --image-base. It seems reasonable to use a different image base
value when using linker scripts and --image-base is specified, since otherwise the
switch has no effect. This change does this, as well as removing unnecessary
initialisation of Dot where it is not used.
The default image base should not be used when processing linker
scripts, because this will change the behaviour for existing linker script users,
and potentially result in invalid output being produced, as a subsequent assignment
to Dot could move the location counter backwards. Instead, we maintain the existing
behaviour of starting from 0 if --image-base is not specified.
Reviewers: ruiu
Differential Revision: https://reviews.llvm.org/D38360
llvm-svn: 315293
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 591e6ff..c7e103a 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -365,7 +365,6 @@
// script parser.
CurAddressState = State.get();
CurAddressState->OutSec = Aether;
- Dot = 0;
for (size_t I = 0; I < Opt.Commands.size(); ++I) {
// Handle symbol assignments outside of any output section.
@@ -438,7 +437,7 @@
StartAddr = std::min(StartAddr, KV.second);
auto Expr = [=] {
- return std::min(StartAddr, Config->ImageBase + elf::getHeaderSize());
+ return std::min(StartAddr, Target->getImageBase() + elf::getHeaderSize());
};
Opt.Commands.insert(Opt.Commands.begin(),
make<SymbolAssignment>(".", Expr, ""));
@@ -780,9 +779,11 @@
}
}
+// Assign addresses as instructed by linker script SECTIONS sub-commands.
void LinkerScript::assignAddresses() {
- // Assign addresses as instructed by linker script SECTIONS sub-commands.
- Dot = 0;
+ // By default linker scripts use an initial value of 0 for '.', but prefer
+ // -image-base if set.
+ Dot = Config->ImageBase ? *Config->ImageBase : 0;
auto State = make_unique<AddressState>(Opt);
// CurAddressState captures the local AddressState and makes it accessible
// deliberately. This is needed as there are some cases where we cannot just