Skip to content

Commit

Permalink
[ELF] nmagic or omagic: don't allocate PT_PHDR or PF_R PT_LOAD for th…
Browse files Browse the repository at this point in the history
…e !hasPhdrsCommands case

```
part.phdrs = script->hasPhdrsCommands() ? script->createPhdrs() : createPhdrs(part);
```

createPhdrs() allocates a PT_PHDR and a PF_R PT_LOAD, which will be
deleted later in LinkerScript::allocateHeaders, but leave a gap between
the program headers and the first section. Don't allocate the segments
to avoid the gap. PT_INTERP is likely not needed as well.

Reviewed By: ruiu

Differential Revision: https://reviews.llvm.org/D67324

llvm-svn: 371398
  • Loading branch information
MaskRay committed Sep 9, 2019
1 parent 388b979 commit e8c0d93
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 47 deletions.
39 changes: 22 additions & 17 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2030,27 +2030,32 @@ std::vector<PhdrEntry *> Writer<ELFT>::createPhdrs(Partition &part) {
unsigned partNo = part.getNumber();
bool isMain = partNo == 1;

// The first phdr entry is PT_PHDR which describes the program header itself.
if (isMain)
addHdr(PT_PHDR, PF_R)->add(Out::programHeaders);
else
addHdr(PT_PHDR, PF_R)->add(part.programHeaders->getParent());

// PT_INTERP must be the second entry if exists.
if (OutputSection *cmd = findSection(".interp", partNo))
addHdr(PT_INTERP, cmd->getPhdrFlags())->add(cmd);

// Add the first PT_LOAD segment for regular output sections.
uint64_t flags = computeFlags(PF_R);
PhdrEntry *load = nullptr;

// Add the headers. We will remove them if they don't fit.
// In the other partitions the headers are ordinary sections, so they don't
// need to be added here.
if (isMain) {
load = addHdr(PT_LOAD, flags);
load->add(Out::elfHeader);
load->add(Out::programHeaders);
// nmagic or omagic output does not have PT_PHDR, PT_INTERP, or the readonly
// PT_LOAD.
if (!config->nmagic && !config->omagic) {
// The first phdr entry is PT_PHDR which describes the program header
// itself.
if (isMain)
addHdr(PT_PHDR, PF_R)->add(Out::programHeaders);
else
addHdr(PT_PHDR, PF_R)->add(part.programHeaders->getParent());

// PT_INTERP must be the second entry if exists.
if (OutputSection *cmd = findSection(".interp", partNo))
addHdr(PT_INTERP, cmd->getPhdrFlags())->add(cmd);

// Add the headers. We will remove them if they don't fit.
// In the other partitions the headers are ordinary sections, so they don't
// need to be added here.
if (isMain) {
load = addHdr(PT_LOAD, flags);
load->add(Out::elfHeader);
load->add(Out::programHeaders);
}
}

// PT_GNU_RELRO includes all sections that should be marked as
Expand Down
12 changes: 6 additions & 6 deletions lld/test/ELF/magic-page-combo-warn.s
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# OMAGIC: ProgramHeader {
# OMAGIC: Type: PT_LOAD
# OMAGIC-NEXT: Offset: 0xE8
# OMAGIC-NEXT: Offset: 0xB0
# OMAGIC-NEXT: VirtualAddress:
# OMAGIC-NEXT: PhysicalAddress:
# OMAGIC-NEXT: FileSize:
Expand All @@ -23,7 +23,7 @@
# OMAGIC-NEXT: PF_W
# OMAGIC-NEXT: PF_X
# OMAGIC-NEXT: ]
# OMAGIC-NEXT: Alignment: 8
# OMAGIC-NEXT: Alignment: 4
# OMAGIC-NEXT: }
# OMAGIC-NEXT: ProgramHeader {
# OMAGIC-NEXT: Type: PT_GNU_STACK
Expand All @@ -35,19 +35,19 @@

# NMAGIC: ProgramHeader {
# NMAGIC-NEXT: Type: PT_LOAD
# NMAGIC-NEXT: Offset: 0x158
# NMAGIC-NEXT: Offset: 0x120
# NMAGIC-NEXT: VirtualAddress:
# NMAGIC-NEXT: PhysicalAddress:
# NMAGIC-NEXT: FileSize: 1
# NMAGIC-NEXT: MemSize: 1
# NMAGIC-NEXT: Flags [
# NMAGIC-NEXT: PF_R
# NMAGIC-NEXT: ]
# NMAGIC-NEXT: Alignment: 8
# NMAGIC-NEXT: Alignment: 1
# NMAGIC-NEXT: }
# NMAGIC-NEXT: ProgramHeader {
# NMAGIC-NEXT: Type: PT_LOAD
# NMAGIC-NEXT: Offset: 0x15C
# NMAGIC-NEXT: Offset: 0x124
# NMAGIC-NEXT: VirtualAddress:
# NMAGIC-NEXT: PhysicalAddress:
# NMAGIC-NEXT: FileSize: 2
Expand All @@ -60,7 +60,7 @@
# NMAGIC-NEXT: }
# NMAGIC-NEXT: ProgramHeader {
# NMAGIC-NEXT: Type: PT_LOAD (0x1)
# NMAGIC-NEXT: Offset: 0x15E
# NMAGIC-NEXT: Offset: 0x126
# NMAGIC-NEXT: VirtualAddress:
# NMAGIC-NEXT: PhysicalAddress:
# NMAGIC-NEXT: FileSize: 1
Expand Down
12 changes: 6 additions & 6 deletions lld/test/ELF/nmagic.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
# RUN: llvm-readelf --section-headers %t.exe | FileCheck %s

# CHECK: [ 0] NULL 00000000 000000 000000 00 0 0 0
# CHECK: [ 1] .text PROGBITS 00000000 0000d4 000001 00 AX 0 0 4
# CHECK: [ 2] .rodata PROGBITS 00000008 0000d8 000008 00 A 0 0 8
# CHECK: [ 3] .comment PROGBITS 00000000 0000e0 000008 01 MS 0 0 1
# CHECK: [ 4] .symtab SYMTAB 00000000 0000e8 000020 10 6 1 4
# CHECK: [ 5] .shstrtab STRTAB 00000000 000108 000032 00 0 0 1
# CHECK: [ 6] .strtab STRTAB 00000000 00013a 000008 00 0 0 1
# CHECK: [ 1] .text PROGBITS 00000000 000094 000001 00 AX 0 0 4
# CHECK: [ 2] .rodata PROGBITS 00000008 000098 000008 00 A 0 0 8
# CHECK: [ 3] .comment PROGBITS 00000000 0000a0 000008 01 MS 0 0 1
# CHECK: [ 4] .symtab SYMTAB 00000000 0000a8 000020 10 6 1 4
# CHECK: [ 5] .shstrtab STRTAB 00000000 0000c8 000032 00 0 0 1
# CHECK: [ 6] .strtab STRTAB 00000000 0000fa 000008 00 0 0 1

.globl _start
.text
Expand Down
24 changes: 12 additions & 12 deletions lld/test/ELF/relro-omagic.s
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
# NORELRO: Sections:
# NORELRO-NEXT: Idx Name Size VMA Type
# NORELRO-NEXT: 0 00000000 0000000000000000
# NORELRO-NEXT: 1 .dynsym 00000048 0000000000200120
# NORELRO-NEXT: 2 .hash 00000020 0000000000200168
# NORELRO-NEXT: 3 .dynstr 00000021 0000000000200188
# NORELRO-NEXT: 4 .rela.dyn 00000018 00000000002001b0
# NORELRO-NEXT: 5 .rela.plt 00000018 00000000002001c8
# NORELRO-NEXT: 6 .text 0000000a 00000000002001e0 TEXT
# NORELRO-NEXT: 7 .plt 00000020 00000000002001f0 TEXT
# NORELRO-NEXT: 8 .data 00000008 0000000000200210 DATA
# NORELRO-NEXT: 9 .foo 00000004 0000000000200218 DATA
# NORELRO-NEXT: 10 .dynamic 000000f0 0000000000200220
# NORELRO-NEXT: 11 .got 00000008 0000000000200310 DATA
# NORELRO-NEXT: 12 .got.plt 00000020 0000000000200318 DATA
# NORELRO-NEXT: 1 .dynsym 00000048 00000000002000e8
# NORELRO-NEXT: 2 .hash 00000020 0000000000200130
# NORELRO-NEXT: 3 .dynstr 00000021 0000000000200150
# NORELRO-NEXT: 4 .rela.dyn 00000018 0000000000200178
# NORELRO-NEXT: 5 .rela.plt 00000018 0000000000200190
# NORELRO-NEXT: 6 .text 0000000a 00000000002001a8 TEXT
# NORELRO-NEXT: 7 .plt 00000020 00000000002001c0 TEXT
# NORELRO-NEXT: 8 .data 00000008 00000000002001e0 DATA
# NORELRO-NEXT: 9 .foo 00000004 00000000002001e8 DATA
# NORELRO-NEXT: 10 .dynamic 000000f0 00000000002001f0
# NORELRO-NEXT: 11 .got 00000008 00000000002002e0 DATA
# NORELRO-NEXT: 12 .got.plt 00000020 00000000002002e8 DATA

# NOPHDRS: ProgramHeaders [
# NOPHDRS-NOT: PT_GNU_RELRO
Expand Down
12 changes: 6 additions & 6 deletions lld/test/ELF/segments.s
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@

# OMAGIC: ProgramHeader {
# OMAGIC: Type: PT_LOAD
# OMAGIC-NEXT: Offset: 0xE8
# OMAGIC-NEXT: Offset: 0xB0
# OMAGIC-NEXT: VirtualAddress:
# OMAGIC-NEXT: PhysicalAddress:
# OMAGIC-NEXT: FileSize:
Expand All @@ -94,7 +94,7 @@
# OMAGIC-NEXT: PF_W
# OMAGIC-NEXT: PF_X
# OMAGIC-NEXT: ]
# OMAGIC-NEXT: Alignment: 8
# OMAGIC-NEXT: Alignment: 4
# OMAGIC-NEXT: }
# OMAGIC-NEXT: ProgramHeader {
# OMAGIC-NEXT: Type: PT_GNU_STACK
Expand All @@ -106,19 +106,19 @@

# NMAGIC: ProgramHeader {
# NMAGIC-NEXT: Type: PT_LOAD
# NMAGIC-NEXT: Offset: 0x158
# NMAGIC-NEXT: Offset: 0x120
# NMAGIC-NEXT: VirtualAddress:
# NMAGIC-NEXT: PhysicalAddress:
# NMAGIC-NEXT: FileSize: 1
# NMAGIC-NEXT: MemSize: 1
# NMAGIC-NEXT: Flags [
# NMAGIC-NEXT: PF_R
# NMAGIC-NEXT: ]
# NMAGIC-NEXT: Alignment: 8
# NMAGIC-NEXT: Alignment: 1
# NMAGIC-NEXT: }
# NMAGIC-NEXT: ProgramHeader {
# NMAGIC-NEXT: Type: PT_LOAD
# NMAGIC-NEXT: Offset: 0x15C
# NMAGIC-NEXT: Offset: 0x124
# NMAGIC-NEXT: VirtualAddress:
# NMAGIC-NEXT: PhysicalAddress:
# NMAGIC-NEXT: FileSize: 2
Expand All @@ -131,7 +131,7 @@
# NMAGIC-NEXT: }
# NMAGIC-NEXT: ProgramHeader {
# NMAGIC-NEXT: Type: PT_LOAD (0x1)
# NMAGIC-NEXT: Offset: 0x15E
# NMAGIC-NEXT: Offset: 0x126
# NMAGIC-NEXT: VirtualAddress:
# NMAGIC-NEXT: PhysicalAddress:
# NMAGIC-NEXT: FileSize: 1
Expand Down

0 comments on commit e8c0d93

Please sign in to comment.