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
// 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.
--- /dev/null
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "PHDRS {all PT_LOAD PHDRS;} \
+# RUN: SECTIONS { \
+# RUN: . = 0x2000; \
+# RUN: .text : {*(.text)} :all \
+# RUN: }" > %t.script
+# RUN: ld.lld -o %t.so --script %t.script %t.o -shared
+# RUN: llvm-readobj -program-headers %t.so | FileCheck %s
+
+# CHECK: ProgramHeaders [
+# CHECK-NEXT: ProgramHeader {
+# CHECK-NEXT: Type: PT_LOAD
+# CHECK-NEXT: Offset: 0x40
+# CHECK-NEXT: VirtualAddress: 0x1040
+# CHECK-NEXT: PhysicalAddress: 0x1040
+# CHECK-NEXT: FileSize: 4176
+# CHECK-NEXT: MemSize: 4176
+# CHECK-NEXT: Flags [
+# CHECK-NEXT: PF_R (0x4)
+# CHECK-NEXT: PF_W (0x2)
+# CHECK-NEXT: PF_X (0x1)
+# CHECK-NEXT: ]
+# CHECK-NEXT: Alignment: 4096
+# CHECK-NEXT: }
+# CHECK-NEXT: ]