Fix address computation for headers.
If the linker script has SECTIONS, the address computation is now
always done in LinkerScript::assignAddresses, like for any other
section.
Before fixHeaders would do a tentative computation that
assignAddresses would sometimes override.
This patch also splits the cases where assignAddresses needs to add
the headers to the first PT_LOAD and the address computation. The net
effect is that we no longer create an empty page for no reason in the
included test case, which matches bfd behavior.
llvm-svn: 287565
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 38477c8..cdc6695 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -707,24 +707,21 @@
if (FirstPTLoad == Phdrs.end())
return;
- if (HeaderSize <= MinVA) {
- // If linker script specifies program headers and first PT_LOAD doesn't
- // have both PHDRS and FILEHDR attributes then do nothing
- if (!Opt.PhdrsCommands.empty()) {
- size_t SegNum = std::distance(Phdrs.begin(), FirstPTLoad);
- if (!Opt.PhdrsCommands[SegNum].HasPhdrs ||
- !Opt.PhdrsCommands[SegNum].HasFilehdr)
- return;
- }
- // ELF and Program headers need to be right before the first section in
- // memory. Set their addresses accordingly.
- MinVA = alignDown(MinVA - HeaderSize, Target->PageSize);
- Out<ELFT>::ElfHeader->Addr = MinVA;
- Out<ELFT>::ProgramHeaders->Addr = Out<ELFT>::ElfHeader->Size + MinVA;
+ // If the linker script doesn't have PHDRS, add ElfHeader and ProgramHeaders
+ // now that we know we have space.
+ if (HeaderSize <= MinVA && !hasPhdrsCommands()) {
FirstPTLoad->First = Out<ELFT>::ElfHeader;
if (!FirstPTLoad->Last)
FirstPTLoad->Last = Out<ELFT>::ProgramHeaders;
- } else if (!FirstPTLoad->First) {
+ }
+
+ // ELF and Program headers need to be right before the first section in
+ // memory. Set their addresses accordingly.
+ MinVA = alignDown(MinVA - HeaderSize, Target->PageSize);
+ Out<ELFT>::ElfHeader->Addr = MinVA;
+ Out<ELFT>::ProgramHeaders->Addr = Out<ELFT>::ElfHeader->Size + MinVA;
+
+ if (!FirstPTLoad->First) {
// Sometimes the very first PT_LOAD segment can be empty.
// This happens if (all conditions met):
// - Linker script is used