Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1564,6 +1564,8 @@ // Finalize the program headers. We call this function after we assign // file offsets and VAs to all sections. template void Writer::setPhdrs() { + PhdrEntry *LastRO = nullptr; + PhdrEntry *LastRW = nullptr; for (PhdrEntry &P : Phdrs) { OutputSection *First = P.First; OutputSection *Last = P.Last; @@ -1577,9 +1579,13 @@ if (!P.HasLMA) P.p_paddr = First->getLMA(); } - if (P.p_type == PT_LOAD) + if (P.p_type == PT_LOAD) { P.p_align = Config->MaxPageSize; - else if (P.p_type == PT_GNU_RELRO) { + if (P.p_flags & PF_W) + LastRW = &P; + else + LastRO = &P; + } else if (P.p_type == PT_GNU_RELRO) { P.p_align = 1; // The glibc dynamic loader rounds the size down, so we need to round up // to protect the last page. This is a no-op on FreeBSD which always @@ -1595,6 +1601,9 @@ P.p_memsz = alignTo(P.p_memsz, P.p_align); } } + + if (LastRO && !LastRW) + LastRO->p_memsz = alignTo(LastRO->p_memsz, Target->PageSize); } // The entry point address is chosen in the following ways. Index: test/ELF/avoid-empty-program-headers.s =================================================================== --- test/ELF/avoid-empty-program-headers.s +++ test/ELF/avoid-empty-program-headers.s @@ -43,7 +43,7 @@ // CHECK-NEXT: VirtualAddress: 0x201000 // CHECK-NEXT: PhysicalAddress: 0x201000 // CHECK-NEXT: FileSize: 1 -// CHECK-NEXT: MemSize: 1 +// CHECK-NEXT: MemSize: 4096 // CHECK-NEXT: Flags [ (0x5) // CHECK-NEXT: PF_R (0x4) // CHECK-NEXT: PF_X (0x1) Index: test/ELF/basic-aarch64.s =================================================================== --- test/ELF/basic-aarch64.s +++ test/ELF/basic-aarch64.s @@ -186,7 +186,7 @@ # CHECK-NEXT: VirtualAddress: 0x20000 # CHECK-NEXT: PhysicalAddress: 0x20000 # CHECK-NEXT: FileSize: 12 -# CHECK-NEXT: MemSize: 12 +# CHECK-NEXT: MemSize: 4096 # CHECK-NEXT: Flags [ (0x5) # CHECK-NEXT: PF_R (0x4) # CHECK-NEXT: PF_X (0x1) Index: test/ELF/basic.s =================================================================== --- test/ELF/basic.s +++ test/ELF/basic.s @@ -179,7 +179,7 @@ # CHECK-NEXT: VirtualAddress: 0x201000 # CHECK-NEXT: PhysicalAddress: 0x201000 # CHECK-NEXT: FileSize: 16 -# CHECK-NEXT: MemSize: 16 +# CHECK-NEXT: MemSize: 4096 # CHECK-NEXT: Flags [ (0x5) # CHECK-NEXT: PF_R (0x4) # CHECK-NEXT: PF_X (0x1) Index: test/ELF/basic32.s =================================================================== --- test/ELF/basic32.s +++ test/ELF/basic32.s @@ -156,7 +156,7 @@ # CHECK-NEXT: VirtualAddress: 0x11000 # CHECK-NEXT: PhysicalAddress: 0x11000 # CHECK-NEXT: FileSize: 12 -# CHECK-NEXT: MemSize: 12 +# CHECK-NEXT: MemSize: 4096 # CHECK-NEXT: Flags [ (0x5) # CHECK-NEXT: PF_R (0x4) # CHECK-NEXT: PF_X (0x1) Index: test/ELF/build-id.s =================================================================== --- test/ELF/build-id.s +++ test/ELF/build-id.s @@ -48,15 +48,15 @@ # DEFAULT: Contents of section .note.test: # DEFAULT: Contents of section .note.gnu.build-id: # DEFAULT-NEXT: 04000000 08000000 03000000 474e5500 ............GNU. -# DEFAULT-NEXT: fd36edb1 f6ff02af +# DEFAULT-NEXT: 77953785 9772bbae # MD5: Contents of section .note.gnu.build-id: # MD5-NEXT: 04000000 10000000 03000000 474e5500 ............GNU. -# MD5-NEXT: fc +# MD5-NEXT: d4 # SHA1: Contents of section .note.gnu.build-id: # SHA1-NEXT: 04000000 14000000 03000000 474e5500 ............GNU. -# SHA1-NEXT: 55b1eedb 03b588e1 09987d1d e9a79be7 +# SHA1-NEXT: a382a5c7 387f708c b93f3f1d 8e5b5fc4 # UUID: Contents of section .note.gnu.build-id: # UUID-NEXT: 04000000 10000000 03000000 474e5500 ............GNU. Index: test/ELF/image-base.s =================================================================== --- test/ELF/image-base.s +++ test/ELF/image-base.s @@ -42,7 +42,7 @@ # CHECK-NEXT: VirtualAddress: 0x1001000 # CHECK-NEXT: PhysicalAddress: 0x1001000 # CHECK-NEXT: FileSize: 1 -# CHECK-NEXT: MemSize: 1 +# CHECK-NEXT: MemSize: 4096 # CHECK-NEXT: Flags [ (0x5) # CHECK-NEXT: PF_R (0x4) # CHECK-NEXT: PF_X (0x1) Index: test/ELF/linkerscript/at.s =================================================================== --- test/ELF/linkerscript/at.s +++ test/ELF/linkerscript/at.s @@ -82,7 +82,7 @@ # CHECK-NEXT: VirtualAddress: 0x5000 # CHECK-NEXT: PhysicalAddress: 0x5000 # CHECK-NEXT: FileSize: 9 -# CHECK-NEXT: MemSize: 9 +# CHECK-NEXT: MemSize: 4096 # CHECK-NEXT: Flags [ # CHECK-NEXT: PF_R # CHECK-NEXT: PF_X Index: test/ELF/linkerscript/phdrs-flags.s =================================================================== --- test/ELF/linkerscript/phdrs-flags.s +++ test/ELF/linkerscript/phdrs-flags.s @@ -38,7 +38,7 @@ # DEFHDR-NEXT: VirtualAddress: 0x10000000 # DEFHDR-NEXT: PhysicalAddress: 0x10000000 # DEFHDR-NEXT: FileSize: 521 -# DEFHDR-NEXT: MemSize: 521 +# DEFHDR-NEXT: MemSize: 4096 # DEFHDR-NEXT: Flags [ (0x1) # DEFHDR-NEXT: PF_X (0x1) # DEFHDR-NEXT: ]