Index: lld/MachO/Config.h =================================================================== --- lld/MachO/Config.h +++ lld/MachO/Config.h @@ -187,6 +187,8 @@ llvm::StringRef osoPrefix; + llvm::Optional pagezeroSize; + llvm::MachO::Architecture arch() const { return platformInfo.target.Arch; } llvm::MachO::PlatformType platform() const { Index: lld/MachO/Driver.cpp =================================================================== --- lld/MachO/Driver.cpp +++ lld/MachO/Driver.cpp @@ -635,6 +635,19 @@ return nullptr; } + if (args.hasArg(OPT_pagezero_size)) { + uint64_t pagezeroSize = args::getHex(args, OPT_pagezero_size, 0); + + // __PAGEZERO should be aligned to 4KB, not to the page alignment like ld64 + // says. + if ((pagezeroSize % 0x1000) != 0) { + warn("__PAGEZERO size is misaligned, rounding down"); + pagezeroSize &= ~(0xFFFull); + } + + config->pagezeroSize = pagezeroSize; + } + PlatformType platform = parsePlatformVersion(args); config->platformInfo.target = MachO::Target(getArchitectureFromName(archName), platform); Index: lld/MachO/Options.td =================================================================== --- lld/MachO/Options.td +++ lld/MachO/Options.td @@ -444,7 +444,6 @@ def pagezero_size : Separate<["-"], "pagezero_size">, MetaVarName<"">, HelpText<"Size of unreadable segment at address zero is hex (default is 4KB on 32-bit and 4GB on 64-bit)">, - Flags<[HelpHidden]>, Group; def stack_size : Separate<["-"], "stack_size">, MetaVarName<"">, Index: lld/MachO/SyntheticSections.h =================================================================== --- lld/MachO/SyntheticSections.h +++ lld/MachO/SyntheticSections.h @@ -103,6 +103,7 @@ public: PageZeroSection(); bool isHidden() const override { return true; } + bool isNeeded() const override { return target->pageZeroSize != 0; } uint64_t getSize() const override { return target->pageZeroSize; } uint64_t getFileSize() const override { return 0; } void writeTo(uint8_t *buf) const override {} Index: lld/MachO/Target.h =================================================================== --- lld/MachO/Target.h +++ lld/MachO/Target.h @@ -9,6 +9,7 @@ #ifndef LLD_MACHO_TARGET_H #define LLD_MACHO_TARGET_H +#include "Config.h" #include "MachOStructs.h" #include "Relocations.h" @@ -35,6 +36,7 @@ // without having to resort to templates. magic = LP::magic; pageZeroSize = LP::pageZeroSize; + pageZeroSize = config->pagezeroSize.getValueOr(pageZeroSize); headerSize = sizeof(typename LP::mach_header); wordSize = LP::wordSize; } Index: lld/test/MachO/pagesize-zero.s =================================================================== --- /dev/null +++ lld/test/MachO/pagesize-zero.s @@ -0,0 +1,11 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o +# RUN: %lld -lSystem -arch x86_64 -o %t %t.o -pagezero_size 0 +# RUN: llvm-objdump --macho --section-headers %t | FileCheck %s + +# CHECK: Sections: +# CHECK: Idx Name Size VMA Type +# CHECK: 0 __text 00000000 0000000000000280 TEXT + +.globl _main +_main: Index: lld/test/MachO/pagezero-misaligned.s =================================================================== --- /dev/null +++ lld/test/MachO/pagezero-misaligned.s @@ -0,0 +1,14 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o +# RUN: %no_fatal_warnings_lld -lSystem -arch x86_64 -o %t %t.o -pagezero_size 100001 2>&1 | FileCheck %s --check-prefix=LINK + +# LINK: warning: __PAGEZERO size is misaligned, rounding down + +# RUN: llvm-objdump --macho --section-headers %t | FileCheck %s --check-prefix=OBJDUMP + +# OBJDUMP: Sections: +# OBJDUMP: Idx Name Size VMA Type +# OBJDUMP: 0 __text 00000000 00000000001002c8 TEXT + +.globl _main +_main: Index: lld/test/MachO/pagezero.s =================================================================== --- /dev/null +++ lld/test/MachO/pagezero.s @@ -0,0 +1,11 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o +# RUN: %lld -lSystem -arch x86_64 -o %t %t.o -pagezero_size 100000 +# RUN: llvm-objdump --macho --section-headers %t | FileCheck %s + +# CHECK: Sections: +# CHECK: Idx Name Size VMA Type +# CHECK: 0 __text 00000000 00000000001002c8 TEXT + +.globl _main +_main: