Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -159,11 +159,13 @@ bool LTONewPassManager; bool MergeArmExidx; bool MipsN32Abi = false; + bool Nmagic; bool NoinhibitExec; bool Nostdlib; bool OFormatBinary; bool Omagic; bool OptRemarksWithHotness; + bool Paged; bool PicThunk; bool Pie; bool PrintGcSections; Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -823,6 +823,7 @@ Config->MipsGotSize = args::getInteger(Args, OPT_mips_got_size, 0xfff0); Config->MergeArmExidx = Args.hasFlag(OPT_merge_exidx_entries, OPT_no_merge_exidx_entries, true); + Config->Nmagic = Args.hasFlag(OPT_nmagic, OPT_no_nmagic, false); Config->NoinhibitExec = Args.hasArg(OPT_noinhibit_exec); Config->Nostdlib = Args.hasArg(OPT_nostdlib); Config->OFormatBinary = isOutputFormatBinary(Args); @@ -955,7 +956,7 @@ // .text segments are writable. Today, the option is still in use to // create special-purpose programs such as boot loaders. It doesn't // make sense to create PT_GNU_RELRO for such executables. - if (Config->Omagic) + if (Config->Omagic || Config->Nmagic) Config->ZRelro = false; std::tie(Config->BuildId, Config->BuildIdVector) = getBuildId(Args); @@ -1023,6 +1024,7 @@ Config->Endianness = Config->IsLE ? endianness::little : endianness::big; Config->IsMips64EL = (K == ELF64LEKind && M == EM_MIPS); Config->Pic = Config->Pie || Config->Shared; + Config->Paged = !Config->Omagic && !Config->Nmagic; Config->PicThunk = Args.hasArg(OPT_pic_veneer, Config->Pic); Config->Wordsize = Config->Is64 ? 8 : 4; @@ -1113,6 +1115,10 @@ case OPT_Bdynamic: Config->Static = false; break; + case OPT_omagic: + case OPT_nmagic: + Config->Static = true; + break; case OPT_whole_archive: InWholeArchive = true; break; Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -993,7 +993,8 @@ return Cmd.HasPhdrs || Cmd.HasFilehdr; }); uint64_t HeaderSize = getHeaderSize(); - if (HeaderSize <= Min - computeBase(Min, HasExplicitHeaders)) { + if ((Config->Paged || HasExplicitHeaders) && + HeaderSize <= Min - computeBase(Min, HasExplicitHeaders)) { Min = alignDown(Min - HeaderSize, Config->MaxPageSize); Out::ElfHeader->Addr = Min; Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size; Index: ELF/Options.td =================================================================== --- ELF/Options.td +++ ELF/Options.td @@ -218,6 +218,9 @@ "Enable merging .ARM.exidx entries (default)", "Disable merging .ARM.exidx entries">; +def nmagic: F<"nmagic">, MetaVarName<"">, + HelpText<"Do not page align sections, link against static libraries.">; + def nostdlib: F<"nostdlib">, HelpText<"Only search directories specified on the command line">; @@ -230,8 +233,11 @@ def noinhibit_exec: F<"noinhibit-exec">, HelpText<"Retain the executable output file whenever it is still usable">; +def no_nmagic: F<"no-nmagic">, MetaVarName<"">, + HelpText<"Page align sections.">; + def no_omagic: F<"no-omagic">, MetaVarName<"">, - HelpText<"Do not set the text data sections to be writable">; + HelpText<"Do not set the text data sections to be writable, page align sections">; def no_rosegment: F<"no-rosegment">, HelpText<"Do not put read-only non-executable sections in their own segment">; @@ -246,7 +252,7 @@ HelpText<"Specify the binary format for the output object file">; def omagic: Flag<["--"], "omagic">, MetaVarName<"">, - HelpText<"Set the text and data sections to be readable and writable">; + HelpText<"Set the text and data sections to be readable and writable, do not page align sections and link against static libraries">; defm orphan_handling: Eq<"orphan-handling", "Control how orphan sections are handled when linker script used">; @@ -414,6 +420,7 @@ def: JoinedOrSeparate<["-"], "l">, Alias, HelpText<"Alias for --library">; def: JoinedOrSeparate<["-"], "L">, Alias, HelpText<"Alias for --library-path">; def: F<"no-pic-executable">, Alias, HelpText<"Alias for --no-pie">; +def: Flag<["-"], "n">, Alias, HelpText<"Alias for --nmagic">; def: Flag<["-"], "N">, Alias, HelpText<"Alias for --omagic">; def: Joined<["--"], "output=">, Alias, HelpText<"Alias for -o">; def: Separate<["--"], "output">, Alias, HelpText<"Alias for -o">; Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1748,7 +1748,7 @@ finalizeSynthetic(In.VerNeed); finalizeSynthetic(In.Dynamic); - if (!Script->HasSectionsCommand && !Config->Relocatable) + if (!Script->HasSectionsCommand && !Config->Relocatable && Config->Paged) fixSectionAlignments(); // SHFLinkOrder processing must be processed after relative section placements are @@ -2080,7 +2080,8 @@ // module the page size. OutputSection *First = OS->PtLoad->FirstSec; if (OS == First) { - uint64_t Alignment = std::max(OS->Alignment, Config->MaxPageSize); + uint64_t PageSize = Config->Paged ? Config->MaxPageSize : 1; + uint64_t Alignment = std::max(OS->Alignment, PageSize); return alignTo(Off, Alignment, OS->Addr); } @@ -2131,7 +2132,7 @@ // If this is a last section of the last executable segment and that // segment is the last loadable segment, align the offset of the // following section to avoid loading non-segments parts of the file. - if (LastRX && LastRX->LastSec == Sec) + if (LastRX && LastRX->LastSec == Sec && Config->Paged) Off = alignTo(Off, Target->PageSize); } @@ -2178,7 +2179,8 @@ } if (P->p_type == PT_LOAD) { - P->p_align = std::max(P->p_align, Config->MaxPageSize); + uint64_t Alignment = Config->Paged ? Config->MaxPageSize : 1; + P->p_align = std::max(P->p_align, Alignment); } 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 @@ -2471,7 +2473,7 @@ // We'll leave other pages in segments as-is because the rest will be // overwritten by output sections. template void Writer::writeTrapInstr() { - if (Script->HasSectionsCommand) + if (Script->HasSectionsCommand || !Config->Paged) return; // Fill the last page. Index: test/ELF/aarch64-script-nmagic.s =================================================================== --- /dev/null +++ test/ELF/aarch64-script-nmagic.s @@ -0,0 +1,87 @@ +// REQUIRES: aarch64 +// RUN: llvm-mc -filetype=obj -triple=aarch64-linux-gnu %s -o %t.o +// RUN: echo "SECTIONS \ +// RUN: { \ +// RUN: . = 0x0 + SIZEOF_HEADERS; \ +// RUN: .hash : { *(.hash) } :text \ +// RUN: .dynsym : { *(.dynsym) } \ +// RUN: .dynstr : { *(.dynstr) } \ +// RUN: . = ALIGN(4); \ +// RUN: .text : { *(.text*) } \ +// RUN: .dynamic : { *(.dynamic) } :text :dynamic \ +// RUN: } \ +// RUN: PHDRS \ +// RUN: { \ +// RUN: text PT_LOAD FLAGS(5) FILEHDR PHDRS; \ +// RUN: dynamic PT_DYNAMIC FLAGS(4); \ +// RUN: } " > %t.script +// RUN: ld.lld %t.o -o %t.so --shared --hash-style=sysv --script %t.script +// RUN: llvm-readobj --program-headers %t.so | FileCheck %s +// RUN: ld.lld %t.o -o %t2.so --shared --hash-style=sysv -n --script %t.script +// RUN: llvm-readobj --program-headers %t2.so + +// Test that mimics a use case of -n to produce a kernel mapped shared-object +// in a non paged context. We specifically want to test: +// - We can allocate the headers into the first program header (via PHDRS) +// typically -n does not allocate headers. +// - The alignment of the .text section is not page aligned. + .text + .globl foo + .type foo, %function +foo: ret + +// CHECK: ProgramHeaders [ +// CHECK-NEXT: ProgramHeader { +// CHECK-NEXT: Type: PT_LOAD +// CHECK-NEXT: Offset: 0x0 +// CHECK-NEXT: VirtualAddress: 0x0 +// CHECK-NEXT: PhysicalAddress: 0x0 +// CHECK-NEXT: FileSize: +// CHECK-NEXT: MemSize: +// CHECK-NEXT: Flags [ +// CHECK-NEXT: PF_R +// CHECK-NEXT: PF_X +// CHECK-NEXT: ] +// CHECK-NEXT: Alignment: 65536 +// CHECK-NEXT: } +// CHECK-NEXT: ProgramHeader { +// CHECK-NEXT: Type: PT_DYNAMIC +// CHECK-NEXT: Offset: 0x108 +// CHECK-NEXT: VirtualAddress: 0x108 +// CHECK-NEXT: PhysicalAddress: 0x108 +// CHECK-NEXT: FileSize: +// CHECK-NEXT: MemSize: +// CHECK-NEXT: Flags [ +// CHECK-NEXT: PF_R +// CHECK-NEXT: ] +// CHECK-NEXT: Alignment: 8 +// CHECK-NEXT: } +// CHECK-NEXT: ] + +// CHECK-N: ProgramHeaders [ +// CHECK-N-NEXT: ProgramHeader { +// CHECK-N-NEXT: Type: PT_LOAD +// CHECK-N-NEXT: Offset: 0x0 +// CHECK-N-NEXT: VirtualAddress: 0x0 +// CHECK-N-NEXT: PhysicalAddress: 0x0 +// CHECK-N-NEXT: FileSize: 360 +// CHECK-N-NEXT: MemSize: 360 +// CHECK-N-NEXT: Flags [ +// CHECK-N-NEXT: PF_R +// CHECK-N-NEXT: PF_X +// CHECK-N-NEXT: ] +// CHECK-N-NEXT: Alignment: 8 +// CHECK-N-NEXT: } +// CHECK-N-NEXT: ProgramHeader { +// CHECK-N-NEXT: Type: PT_DYNAMIC +// CHECK-N-NEXT: Offset: 0x108 +// CHECK-N-NEXT: VirtualAddress: 0x108 +// CHECK-N-NEXT: PhysicalAddress: 0x108 +// CHECK-N-NEXT: FileSize: +// CHECK-N-NEXT: MemSize: +// CHECK-N-NEXT: Flags [ +// CHECK-N-NEXT: PF_R +// CHECK-N-NEXT: ] +// CHECK-N-NEXT: Alignment: 8 +// CHECK-N-NEXT: } +// CHECK-N-NEXT: ] Index: test/ELF/relro-omagic.s =================================================================== --- test/ELF/relro-omagic.s +++ test/ELF/relro-omagic.s @@ -2,7 +2,7 @@ # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/shared.s -o %t2.o # RUN: ld.lld -shared %t2.o -o %t2.so -soname relro-omagic.s.tmp2.so -# RUN: ld.lld --hash-style=sysv -N %t.o %t2.so -o %t +# RUN: ld.lld --hash-style=sysv -N %t.o -Bdynamic %t2.so -o %t # RUN: llvm-objdump -section-headers %t | FileCheck --check-prefix=NORELRO %s # RUN: llvm-readobj --program-headers %t | FileCheck --check-prefix=NOPHDRS %s Index: test/ELF/segments.s =================================================================== --- test/ELF/segments.s +++ test/ELF/segments.s @@ -84,7 +84,7 @@ # OMAGIC: ProgramHeader { # OMAGIC: Type: PT_LOAD -# OMAGIC-NEXT: Offset: 0x0 +# OMAGIC-NEXT: Offset: 0xE8 # OMAGIC-NEXT: VirtualAddress: # OMAGIC-NEXT: PhysicalAddress: # OMAGIC-NEXT: FileSize: @@ -94,11 +94,17 @@ # OMAGIC-NEXT: PF_W # OMAGIC-NEXT: PF_X # OMAGIC-NEXT: ] -# OMAGIC-NEXT: Alignment: 4096 +# OMAGIC-NEXT: Alignment: 8 # OMAGIC-NEXT: } # OMAGIC-NEXT: ProgramHeader { # OMAGIC-NEXT: Type: PT_GNU_STACK +# RUN: ld.lld -n %t -o %t4 +# RUN: llvm-readobj --program-headers %t4 | FileCheck --check-prefix=NMAGIC %s +# RUN: ld.lld --nmagic %t -o %t4 +# RUN: llvm-readobj --program-headers %t4 | FileCheck --check-prefix=NMAGIC %s + + .global _start _start: nop @@ -111,3 +117,42 @@ .section .rx,"ax" nop + +# NMAGIC: ProgramHeader { +# NMAGIC-NEXT: Type: PT_LOAD +# NMAGIC-NEXT: Offset: 0x158 +# 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: } +# NMAGIC-NEXT: ProgramHeader { +# NMAGIC-NEXT: Type: PT_LOAD +# NMAGIC-NEXT: Offset: 0x15C +# NMAGIC-NEXT: VirtualAddress: +# NMAGIC-NEXT: PhysicalAddress: +# NMAGIC-NEXT: FileSize: 2 +# NMAGIC-NEXT: MemSize: 2 +# NMAGIC-NEXT: Flags [ +# NMAGIC-NEXT: PF_R +# NMAGIC-NEXT: PF_X +# NMAGIC-NEXT: ] +# NMAGIC-NEXT: Alignment: 4 +# NMAGIC-NEXT: } +# NMAGIC-NEXT: ProgramHeader { +# NMAGIC-NEXT: Type: PT_LOAD (0x1) +# NMAGIC-NEXT: Offset: 0x15E +# NMAGIC-NEXT: VirtualAddress: +# NMAGIC-NEXT: PhysicalAddress: +# NMAGIC-NEXT: FileSize: 1 +# NMAGIC-NEXT: MemSize: 1 +# NMAGIC-NEXT: Flags [ +# NMAGIC-NEXT: PF_R +# NMAGIC-NEXT: PF_W +# NMAGIC-NEXT: ] +# NMAGIC-NEXT: Alignment: 1 +# NMAGIC-NEXT: }