static const int PageSize = 4096;
+// On linux x86_64 mmap of the first 15 pages fails, so the smallest value
+// that can be used in here is 0x10000.
+// If using 2MB pages, the smallest page aligned address that works is
+// 0x200000, but it looks like every OS uses 4k pages for executables.
+// FIXME: This is architecture and OS dependent.
+static const int VAStart = 0x10000;
+
namespace {
// OutputSection represents a section in an output file. It's a
// container of chunks. OutputSection and Chunk are 1:N relationship.
// Visits all sections to assign incremental, non-overlapping RVAs and
// file offsets.
template <class ELFT> void Writer<ELFT>::assignAddresses() {
- // On linux x86_64 mmap of the first 15 pages fails, so the smallest value
- // that can be used in here is 0x10000.
- // If using 2MB pages, the smallest page aligned address that works is
- // 0x200000, but it looks like every OS uses 4k pages for executables.
- // FIXME: This is architecture and OS dependent.
- uintX_t VA = 0x10000;
+ uintX_t VA = VAStart;
+ uintX_t FileOff = 0;
- uintX_t FileOff = sizeof(Elf_Ehdr);
+ FileOff += sizeof(Elf_Ehdr);
+ VA += sizeof(Elf_Ehdr);
// Reserve space for PHDRs.
ProgramHeaderOff = FileOff;
FileOff = RoundUpToAlignment(FileOff, PageSize);
+ VA = RoundUpToAlignment(VA, PageSize);
NumPhdrs = 0;
+ // Add a PHDR for the elf header and program headers. Some dynamic linkers
+ // (musl at least) require them to be covered by a PT_LOAD.
+ ++NumPhdrs;
+
for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) {
StrTabSec.add(Sec->getName());
Sec->finalize();
EHdr->e_shstrndx = StrTabSec.getSectionIndex();
auto PHdrs = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff);
+ PHdrs->p_type = PT_LOAD;
+ PHdrs->p_flags = PF_R;
+ PHdrs->p_offset = 0;
+ PHdrs->p_vaddr = VAStart;
+ PHdrs->p_paddr = PHdrs->p_vaddr;
+ PHdrs->p_filesz = ProgramHeaderOff + NumPhdrs * sizeof(Elf_Phdr);
+ PHdrs->p_memsz = PHdrs->p_filesz;
+ PHdrs->p_align = PageSize;
+ ++PHdrs;
+
for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) {
if (!outputSectionHasPHDR<ELFT>(Sec))
continue;
# CHECK-NEXT: ]
# CHECK-NEXT: HeaderSize: 64
# CHECK-NEXT: ProgramHeaderEntrySize: 56
-# CHECK-NEXT: ProgramHeaderCount: 1
+# CHECK-NEXT: ProgramHeaderCount: 2
# CHECK-NEXT: SectionHeaderEntrySize: 64
# CHECK-NEXT: SectionHeaderCount: 6
# CHECK-NEXT: StringTableSectionIndex: 5
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_EXECINSTR (0x4)
# CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x10000
+# CHECK-NEXT: Address: 0x11000
# CHECK-NEXT: Offset: 0x1000
# CHECK-NEXT: Size: 16
# CHECK-NEXT: Link: 0
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_WRITE (0x1)
# CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x10010
+# CHECK-NEXT: Address: 0x11010
# CHECK-NEXT: Offset: 0x1010
# CHECK-NEXT: Size: 0
# CHECK-NEXT: Link: 0
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_WRITE (0x1)
# CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x10010
+# CHECK-NEXT: Address: 0x11010
# CHECK-NEXT: Offset: 0x1010
# CHECK-NEXT: Size: 0
# CHECK-NEXT: Link: 0
# CHECK-NEXT: ProgramHeaders [
# CHECK-NEXT: ProgramHeader {
# CHECK-NEXT: Type: PT_LOAD (0x1)
-# CHECK-NEXT: Offset: 0x1000
+# CHECK-NEXT: Offset: 0x0
# CHECK-NEXT: VirtualAddress: 0x10000
# CHECK-NEXT: PhysicalAddress: 0x10000
+# CHECK-NEXT: FileSize: 176
+# CHECK-NEXT: MemSize: 176
+# CHECK-NEXT: Flags [
+# CHECK-NEXT: PF_R
+# CHECK-NEXT: ]
+# CHECK-NEXT: Alignment: 4096
+# CHECK-NEXT: }
+# CHECK-NEXT: ProgramHeader {
+# CHECK-NEXT: Type: PT_LOAD (0x1)
+# CHECK-NEXT: Offset: 0x1000
+# CHECK-NEXT: VirtualAddress: 0x11000
+# CHECK-NEXT: PhysicalAddress: 0x11000
# CHECK-NEXT: FileSize: 16
# CHECK-NEXT: MemSize: 16
# CHECK-NEXT: Flags [ (0x5)
# CHECK-NEXT: Type: Executable (0x2)
# CHECK-NEXT: Machine: EM_386 (0x3)
# CHECK-NEXT: Version: 1
-# CHECK-NEXT: Entry: 0x10000
+# CHECK-NEXT: Entry: 0x11000
# CHECK-NEXT: ProgramHeaderOffset: 0x34
# CHECK-NEXT: SectionHeaderOffset: 0x1058
# CHECK-NEXT: Flags [ (0x0)
# CHECK-NEXT: ]
# CHECK-NEXT: HeaderSize: 52
# CHECK-NEXT: ProgramHeaderEntrySize: 32
-# CHECK-NEXT: ProgramHeaderCount: 1
+# CHECK-NEXT: ProgramHeaderCount: 2
# CHECK-NEXT: SectionHeaderEntrySize: 40
# CHECK-NEXT: SectionHeaderCount: 6
# CHECK-NEXT: StringTableSectionIndex: 5
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_EXECINSTR (0x4)
# CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x10000
+# CHECK-NEXT: Address: 0x11000
# CHECK-NEXT: Offset: 0x1000
# CHECK-NEXT: Size: 12
# CHECK-NEXT: Link: 0
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_WRITE (0x1)
# CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x1000C
+# CHECK-NEXT: Address: 0x1100C
# CHECK-NEXT: Offset: 0x100C
# CHECK-NEXT: Size: 0
# CHECK-NEXT: Link: 0
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_WRITE (0x1)
# CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x1000C
+# CHECK-NEXT: Address: 0x1100C
# CHECK-NEXT: Offset: 0x100C
# CHECK-NEXT: Size: 0
# CHECK-NEXT: Link: 0
# CHECK-NEXT: ProgramHeaders [
# CHECK-NEXT: ProgramHeader {
# CHECK-NEXT: Type: PT_LOAD (0x1)
-# CHECK-NEXT: Offset: 0x1000
+# CHECK-NEXT: Offset: 0x0
# CHECK-NEXT: VirtualAddress: 0x10000
# CHECK-NEXT: PhysicalAddress: 0x10000
+# CHECK-NEXT: FileSize: 116
+# CHECK-NEXT: MemSize: 116
+# CHECK-NEXT: Flags [
+# CHECK-NEXT: PF_R
+# CHECK-NEXT: ]
+# CHECK-NEXT: Alignment: 4096
+# CHECK-NEXT: }
+# CHECK-NEXT: ProgramHeader {
+# CHECK-NEXT: Type: PT_LOAD (0x1)
+# CHECK-NEXT: Offset: 0x1000
+# CHECK-NEXT: VirtualAddress: 0x11000
+# CHECK-NEXT: PhysicalAddress: 0x11000
# CHECK-NEXT: FileSize: 12
# CHECK-NEXT: MemSize: 12
# CHECK-NEXT: Flags [ (0x5)
# CHECK-NEXT: Type: Executable (0x2)
# CHECK-NEXT: Machine: EM_PPC (0x14)
# CHECK-NEXT: Version: 1
-# CHECK-NEXT: Entry: 0x10000
+# CHECK-NEXT: Entry: 0x11000
# CHECK-NEXT: ProgramHeaderOffset: 0x34
# CHECK-NEXT: SectionHeaderOffset: 0x1058
# CHECK-NEXT: Flags [ (0x0)
# CHECK-NEXT: ]
# CHECK-NEXT: HeaderSize: 52
# CHECK-NEXT: ProgramHeaderEntrySize: 32
-# CHECK-NEXT: ProgramHeaderCount: 1
+# CHECK-NEXT: ProgramHeaderCount: 2
# CHECK-NEXT: SectionHeaderEntrySize: 40
# CHECK-NEXT: SectionHeaderCount: 6
# CHECK-NEXT: StringTableSectionIndex: 5
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_EXECINSTR (0x4)
# CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x10000
+# CHECK-NEXT: Address: 0x11000
# CHECK-NEXT: Offset: 0x1000
# CHECK-NEXT: Size: 12
# CHECK-NEXT: Link: 0
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_WRITE (0x1)
# CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x1000C
+# CHECK-NEXT: Address: 0x1100C
# CHECK-NEXT: Offset: 0x100C
# CHECK-NEXT: Size: 0
# CHECK-NEXT: Link: 0
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_WRITE (0x1)
# CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x1000C
+# CHECK-NEXT: Address: 0x1100C
# CHECK-NEXT: Offset: 0x100C
# CHECK-NEXT: Size: 0
# CHECK-NEXT: Link: 0
# CHECK-NEXT: ProgramHeaders [
# CHECK-NEXT: ProgramHeader {
# CHECK-NEXT: Type: PT_LOAD (0x1)
-# CHECK-NEXT: Offset: 0x1000
+# CHECK-NEXT: Offset: 0x0
# CHECK-NEXT: VirtualAddress: 0x10000
# CHECK-NEXT: PhysicalAddress: 0x10000
+# CHECK-NEXT: FileSize: 116
+# CHECK-NEXT: MemSize: 116
+# CHECK-NEXT: Flags [
+# CHECK-NEXT: PF_R
+# CHECK-NEXT: ]
+# CHECK-NEXT: Alignment: 4096
+# CHECK-NEXT: }
+# CHECK-NEXT: ProgramHeader {
+# CHECK-NEXT: Type: PT_LOAD (0x1)
+# CHECK-NEXT: Offset: 0x1000
+# CHECK-NEXT: VirtualAddress: 0x11000
+# CHECK-NEXT: PhysicalAddress: 0x11000
# CHECK-NEXT: FileSize: 12
# CHECK-NEXT: MemSize: 12
# CHECK-NEXT: Flags [ (0x5)
# CHECK-NEXT: Type: Executable (0x2)
# CHECK-NEXT: Machine: EM_PPC64 (0x15)
# CHECK-NEXT: Version: 1
-# CHECK-NEXT: Entry: 0x11000
+# CHECK-NEXT: Entry: 0x12000
# CHECK-NEXT: ProgramHeaderOffset: 0x40
# CHECK-NEXT: SectionHeaderOffset: 0x2078
# CHECK-NEXT: Flags [ (0x0)
# CHECK-NEXT: ]
# CHECK-NEXT: HeaderSize: 64
# CHECK-NEXT: ProgramHeaderEntrySize: 56
-# CHECK-NEXT: ProgramHeaderCount: 2
+# CHECK-NEXT: ProgramHeaderCount: 3
# CHECK-NEXT: SectionHeaderEntrySize: 64
# CHECK-NEXT: SectionHeaderCount: 7
# CHECK-NEXT: StringTableSectionIndex: 6
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_EXECINSTR (0x4)
# CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x10000
+# CHECK-NEXT: Address: 0x11000
# CHECK-NEXT: Offset: 0x1000
# CHECK-NEXT: Size: 12
# CHECK-NEXT: Link: 0
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_WRITE (0x1)
# CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x1000C
+# CHECK-NEXT: Address: 0x1100C
# CHECK-NEXT: Offset: 0x100C
# CHECK-NEXT: Size: 0
# CHECK-NEXT: Link: 0
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_WRITE (0x1)
# CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x1000C
+# CHECK-NEXT: Address: 0x1100C
# CHECK-NEXT: Offset: 0x100C
# CHECK-NEXT: Size: 0
# CHECK-NEXT: Link: 0
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_WRITE (0x1)
# CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x11000
+# CHECK-NEXT: Address: 0x12000
# CHECK-NEXT: Offset: 0x2000
# CHECK-NEXT: Size: 24
# CHECK-NEXT: Link: 0
# CHECK-NEXT: ProgramHeaders [
# CHECK-NEXT: ProgramHeader {
# CHECK-NEXT: Type: PT_LOAD (0x1)
-# CHECK-NEXT: Offset: 0x1000
+# CHECK-NEXT: Offset: 0x0
# CHECK-NEXT: VirtualAddress: 0x10000
# CHECK-NEXT: PhysicalAddress: 0x10000
+# CHECK-NEXT: FileSize: 232
+# CHECK-NEXT: MemSize: 232
+# CHECK-NEXT: Flags [
+# CHECK-NEXT: PF_R
+# CHECK-NEXT: ]
+# CHECK-NEXT: Alignment: 4096
+# CHECK-NEXT: }
+# CHECK-NEXT: ProgramHeader {
+# CHECK-NEXT: Type: PT_LOAD (0x1)
+# CHECK-NEXT: Offset: 0x1000
+# CHECK-NEXT: VirtualAddress: 0x11000
+# CHECK-NEXT: PhysicalAddress: 0x11000
# CHECK-NEXT: FileSize: 12
# CHECK-NEXT: MemSize: 12
# CHECK-NEXT: Flags [ (0x5)
# CHECK-NEXT: ProgramHeader {
# CHECK-NEXT: Type: PT_LOAD (0x1)
# CHECK-NEXT: Offset: 0x2000
-# CHECK-NEXT: VirtualAddress: 0x11000
-# CHECK-NEXT: PhysicalAddress: 0x11000
+# CHECK-NEXT: VirtualAddress: 0x12000
+# CHECK-NEXT: PhysicalAddress: 0x12000
# CHECK-NEXT: FileSize: 24
# CHECK-NEXT: MemSize: 24
# CHECK-NEXT: Flags [ (0x6)
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x10000
+// CHECK-NEXT: Address: 0x11000
// CHECK-NEXT: Offset: 0x1000
// CHECK-NEXT: Size: 4
// CHECK-NEXT: Link: 0
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x10000
+// CHECK-NEXT: Address: 0x11000
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size: 22
// CHECK: Name: sym4
-// CHECK-NEXT: Value: 0x10000
+// CHECK-NEXT: Value: 0x11000
// CHECK-NEXT: Size: 4
// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: Object
// CHECK-NEXT: Section: .bss
// CHECK: Name: sym3
-// CHECK-NEXT: Value: 0x10014
+// CHECK-NEXT: Value: 0x11014
// CHECK-NEXT: Size: 2
// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: Object
// CHECK-NEXT: Section: .bss
// CHECK: Name: sym2
-// CHECK-NEXT: Value: 0x1000C
+// CHECK-NEXT: Value: 0x1100C
// CHECK-NEXT: Size: 8
// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: Object
// CHECK-NEXT: Section: .bss
// CHECK: Name: sym1
-// CHECK-NEXT: Value: 0x10004
+// CHECK-NEXT: Value: 0x11004
// CHECK-NEXT: Size: 8
// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: Object
// R_X86_64_32
// CHECK: bar:
-// CHECK: 10000: ba 00 00 01 00 movl $65536, %edx
+// CHECK: 11000: ba 00 10 01 00 movl $69632, %edx
// CHECK: e8 04 00 00 00 callq 4
// Also check that symbols match.
-// CHECK: 0000000000010000 .text 00000000 bar
+// CHECK: 0000000000011000 .text 00000000 bar
// CHECK-NEXT: }
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name: _start
-// CHECK-NEXT: Value: 0x10000
+// CHECK-NEXT: Value: 0x11000
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: None
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x10000
+// CHECK-NEXT: Address: 0x11000
// CHECK: Name: foobar
// CHECK-NEXT: Type: SHT_PROGBITS
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_EXECINSTR
// CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x10000
+// CHECK-NEXT: Address: 0x11000
// CHECK: Name: .bss (14)
// CHECK-NEXT: Type: SHT_NOBITS (0x8)
// CHECK-NEXT: SHF_ALLOC (0x2)
// CHECK-NEXT: SHF_WRITE (0x1)
// CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x11000
+// CHECK-NEXT: Address: 0x12000
// CHECK-NEXT: Offset: 0x2000
// CHECK-NEXT: Size: 4
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x12000
+// CHECK-NEXT: Address: 0x13000
// CHECK: Symbols [
// CHECK-NEXT: Symbol {
// CHECK-NEXT: }
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name: _start
-// CHECK-NEXT: Value: 0x10000
+// CHECK-NEXT: Value: 0x11000
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Global (0x1)
// CHECK-NEXT: Type: Function
// CHECK-NEXT: }
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name: foo
-// CHECK-NEXT: Value: 0x10000
+// CHECK-NEXT: Value: 0x11000
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Weak (0x2)
// CHECK-NEXT: Type: Object
// CHECK-NEXT: }
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name: common (34)
-// CHECK-NEXT: Value: 0x11000
+// CHECK-NEXT: Value: 0x12000
// CHECK-NEXT: Size: 4
// CHECK-NEXT: Binding: Global (0x1)
// CHECK-NEXT: Type: Object (0x1)
// CHECK-NEXT: }
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name: zed
-// CHECK-NEXT: Value: 0x12000
+// CHECK-NEXT: Value: 0x13000
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Global (0x1)
// CHECK-NEXT: Type: None
// CHECK-NEXT: }
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name: protected
-// CHECK-NEXT: Value: 0x12008
+// CHECK-NEXT: Value: 0x13008
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: None
// CHECK-NEXT: }
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name: zed3
-// CHECK-NEXT: Value: 0x12008
+// CHECK-NEXT: Value: 0x13008
// CHECK-NEXT: Size: 4
// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: None
// CHECK-NEXT: }
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name: zed2
-// CHECK-NEXT: Value: 0x12004
+// CHECK-NEXT: Value: 0x13004
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: None
// CHECK-NEXT: }
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name: _start
-// CHECK-NEXT: Value: 0x10000
+// CHECK-NEXT: Value: 0x11000
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: None
// CHECK-NEXT: }
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name: default
-// CHECK-NEXT: Value: 0x10000
+// CHECK-NEXT: Value: 0x11000
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: None
// CHECK-NEXT: }
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name: protected
-// CHECK-NEXT: Value: 0x10000
+// CHECK-NEXT: Value: 0x11000
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: None