Index: lld/MachO/Driver.cpp =================================================================== --- lld/MachO/Driver.cpp +++ lld/MachO/Driver.cpp @@ -1113,6 +1113,27 @@ if (errorCount()) return false; + if (args.hasArg(OPT_pagezero_size)) { + uint64_t pagezeroSize = args::getHex(args, OPT_pagezero_size, 0); + + // ld64 does something really weird. It attempts to realign the value to the + // page size, but assumes the the page size is 4K. This doesn't work with + // most of Apple's ARM64 devices, which use a page size of 16K. This means + // that it will first 4K align it by rounding down, then round up to 16K. + // This probably only happened because no one using this arg with anything + // other then 0, so no one checked if it did what is what it says it does. + + // So we are not coping this weird behavior and doing the it in a logical + // way, by always rounding down to page size. + if (!isAligned(Align(target->getPageSize()), pagezeroSize)) { + pagezeroSize -= pagezeroSize % target->getPageSize(); + warn("__PAGEZERO size is not page aligned, rounding down to 0x" + + Twine::utohexstr(pagezeroSize)); + } + + target->pageZeroSize = pagezeroSize; + } + config->osoPrefix = args.getLastArgValue(OPT_oso_prefix); if (!config->osoPrefix.empty()) { // Expand special characters, such as ".", "..", or "~", if present. 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/test/MachO/lit.local.cfg =================================================================== --- lld/test/MachO/lit.local.cfg +++ lld/test/MachO/lit.local.cfg @@ -10,9 +10,10 @@ # flag will append to the set of library roots. As such, we define a separate # alias for each platform. -config.substitutions.append(('%lld-watchos', - 'ld64.lld -fatal_warnings -arch arm64_32 -platform_version watchos 7.0 8.0 -syslibroot ' + - os.path.join(config.test_source_root, "MachO", "Inputs", "WatchOS.sdk"))) +lld_watchos = ('ld64.lld -arch arm64_32 -platform_version watchos 7.0 8.0 -syslibroot ' + + os.path.join(config.test_source_root, "MachO", "Inputs", "WatchOS.sdk")) +config.substitutions.append(('%lld-watchos', lld_watchos + ' -fatal_warnings')) +config.substitutions.append(('%no_fatal_warnings_lld-watchos', lld_watchos)) # Since most of our tests are written around x86_64, we give this platform the # shortest substitution of "%lld". Index: lld/test/MachO/pagezero.s =================================================================== --- /dev/null +++ lld/test/MachO/pagezero.s @@ -0,0 +1,37 @@ +# REQUIRES: x86, aarch64 +# RUN: rm -rf %t; mkdir %t +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/x86_64.o +# RUN: llvm-mc -filetype=obj -triple=arm64_32-apple-darwin %s -o %t/arm64_32.o + +# RUN: %lld -lSystem -arch x86_64 -o %t/x86_64 %t/x86_64.o -pagezero_size 100000 +# RUN: llvm-readobj --macho-segment %t/x86_64 | FileCheck %s -D#VMSIZE=0x100000 -D#SIZE=72 + +# RUN: %lld-watchos -lSystem -arch arm64_32 -o %t/arm64_32 %t/arm64_32.o -pagezero_size 100000 +# RUN: llvm-readobj --macho-segment %t/arm64_32 | FileCheck %s -D#VMSIZE=0x100000 -D#SIZE=56 + +# RUN: %lld -lSystem -arch x86_64 -o %t/zero %t/x86_64.o -pagezero_size 0 +# RUN: llvm-readobj --macho-segment %t/zero | FileCheck %s --check-prefix=CHECK-ZERO -D#VMSIZE=0x1000 -D#SIZE=152 + +# RUN: %no_fatal_warnings_lld -lSystem -arch x86_64 -o %t/x86_64-misalign %t/x86_64.o -pagezero_size 1001 2>&1 | FileCheck %s --check-prefix=LINK -D#SIZE=0x1000 +# RUN: llvm-readobj --macho-segment %t/x86_64-misalign | FileCheck %s -D#VMSIZE=0x1000 -D#SIZE=72 + +# RUN: %no_fatal_warnings_lld-watchos -lSystem -arch arm64_32 -o %t/arm64_32-misalign-4K %t/arm64_32.o -pagezero_size 1001 2>&1 | FileCheck %s --check-prefix=LINK -D#SIZE=0x0 +# RUN: llvm-readobj --macho-segment %t/arm64_32-misalign-4K | FileCheck %s --check-prefix=CHECK-ZERO -D#VMSIZE=0x4000 -D#SIZE=124 + +# RUN: %no_fatal_warnings_lld-watchos -lSystem -arch arm64_32 -o %t/arm64_32-misalign-16K %t/arm64_32.o -pagezero_size 4001 2>&1 | FileCheck %s --check-prefix=LINK -D#SIZE=0x4000 +# RUN: llvm-readobj --macho-segment %t/arm64_32-misalign-16K | FileCheck %s -D#VMSIZE=0x4000 -D#SIZE=56 + +# LINK: warning: __PAGEZERO size is not page aligned, rounding down to 0x[[#%x,SIZE]] + +# CHECK: Name: __PAGEZERO +# CHECK-NEXT: Size: [[#%d,SIZE]] +# CHECK-NEXT: vmaddr: 0x0 +# CHECK-NEXT: vmsize: 0x[[#%x,VMSIZE]] + +# CHECK-ZERO: Name: __TEXT +# CHECK-ZERO-NEXT: Size: [[#%d,SIZE]] +# CHECK-ZERO-NEXT: vmaddr: 0x0 +# CHECK-ZERO-NEXT: vmsize: 0x[[#%x,VMSIZE]] + +.globl _main +_main: