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
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index a5d34b5..13ddf61 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1267,11 +1267,13 @@
// sections. These are special, we do not include them into output sections
// list, but have them to simplify the code.
template <class ELFT> void Writer<ELFT>::fixHeaders() {
- uintX_t BaseVA = ScriptConfig->HasSections ? 0 : Config->ImageBase;
- Out<ELFT>::ElfHeader->Addr = BaseVA;
- uintX_t Off = Out<ELFT>::ElfHeader->Size;
- Out<ELFT>::ProgramHeaders->Addr = Off + BaseVA;
Out<ELFT>::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
+ // If the script has SECTIONS, assignAddresses will compute the values.
+ if (ScriptConfig->HasSections)
+ return;
+ uintX_t BaseVA = Config->ImageBase;
+ Out<ELFT>::ElfHeader->Addr = BaseVA;
+ Out<ELFT>::ProgramHeaders->Addr = BaseVA + Out<ELFT>::ElfHeader->Size;
}
// Assign VAs (addresses at run-time) to output sections.