Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -291,6 +291,9 @@ // few 32-bit ABIs are using RELA too. bool IsRela; + // True if we are page aligning sections. + bool Paged; + // True if we are creating position-independent code. bool Pic; Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -904,6 +904,10 @@ Config->ZText = getZFlag(Args, "text", "notext", true); Config->ZWxneeded = hasZOption(Args, "wxneeded"); + // -n (--nmagic) and -N (--omagic) disable page alignment of sections. + Config->Paged = !(Config->Omagic || + Args.hasFlag(OPT_nmagic, OPT_no_nmagic, false)); + // Parse LTO options. if (auto *Arg = Args.getLastArg(OPT_plugin_opt_mcpu_eq)) parseClangOption(Saver.save("-mcpu=" + StringRef(Arg->getValue())), @@ -951,11 +955,10 @@ if (Args.hasArg(OPT_print_map)) Config->MapFile = "-"; - // --omagic is an option to create old-fashioned executables in which - // .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) + // Page alignment can be disabled by the -n (--nmagic) and -N (--omagic). + // As PT_GNU_RELRO relies on Paging, do not create it when we have disabled + // it. + if (!Config->Paged) Config->ZRelro = false; std::tie(Config->BuildId, Config->BuildIdVector) = getBuildId(Args); @@ -1113,6 +1116,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 @@ -994,7 +994,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, 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 @@ -1747,7 +1747,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 @@ -2079,7 +2079,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); } @@ -2130,7 +2131,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); } @@ -2177,7 +2178,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 @@ -2470,7 +2472,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: docs/ld.lld.1 =================================================================== --- docs/ld.lld.1 +++ docs/ld.lld.1 @@ -254,6 +254,8 @@ .It Fl -Map Ns = Ns Ar file , Fl M Ar file Print a link map to .Ar file . +.It Fl -nmagic , Fl n +Do not page align sections, link against static libraries. .It Fl -no-allow-shlib-undefined Do not allow unresolved references in shared libraries. This option is enabled by default when linking an executable. @@ -325,7 +327,7 @@ .Cm binary , which produces output with no ELF header. .It Fl -omagic , Fl N -Set the text and data sections to be readable and writable. +Set the text and data sections to be readable and writable, do not page align sections, link against static libraries. .It Fl -opt-remarks-filename Ar file Write optimization remarks in YAML format to .Ar file . Index: test/ELF/linkerscript/nmagic-alignment.test =================================================================== --- /dev/null +++ test/ELF/linkerscript/nmagic-alignment.test @@ -0,0 +1,85 @@ +# REQUIRES: x86 + +# 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. + +# RUN: echo ".text; .globl foo; foo: nop" > %t.s +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %t.s -o %t.o +# RUN: ld.lld %t.o -o %t.so --shared --hash-style=sysv --script %s +# RUN: llvm-readobj --program-headers %t.so | FileCheck %s +# RUN: ld.lld %t.o -o %t2.so --shared --hash-style=sysv -n --script %s +# RUN: llvm-readobj --program-headers %t2.so | FileCheck %s --check-prefix=CHECK-N + +SECTIONS { + . = 0x0 + SIZEOF_HEADERS; + .hash : { *(.hash) } :text + .dynsym : { *(.dynsym) } + .dynstr : { *(.dynstr) } + . = ALIGN(4); + .text : { *(.text*) } + .dynamic : { *(.dynamic) } :text :dynamic +} + +PHDRS { + text PT_LOAD FLAGS(5) FILEHDR PHDRS; + dynamic PT_DYNAMIC FLAGS(4); +} + +# 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: 4096 +# 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: }