Index: lld/MachO/Driver.cpp =================================================================== --- lld/MachO/Driver.cpp +++ lld/MachO/Driver.cpp @@ -1113,6 +1113,26 @@ 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 dumb behavior and doing the it in a logical + // way, by always rounding down to page size. + if (!isAligned(Align(target->getPageSize()), pagezeroSize)) { + warn("__PAGEZERO size is not page aligned, rounding down to realign"); + pagezeroSize -= pagezeroSize % target->getPageSize(); + } + + 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,53 @@ +# 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: %lld -lSystem -arch x86_64 -o %t/x86_64 %t/x86_64.o -pagezero_size 100000 +# RUN: llvm-objdump --macho --section-headers %t/x86_64 | FileCheck %s --check-prefix=CHECK_X86_64_NOMRAL + +# RUN: llvm-mc -filetype=obj -triple=arm64_32-apple-darwin %s -o %t/arm64_32.o +# RUN: %lld-watchos -lSystem -arch arm64_32 -o %t/arm64_32 %t/arm64_32.o -pagezero_size 100000 +# RUN: llvm-objdump --macho --section-headers %t/arm64_32 | FileCheck %s --check-prefix=CHECK_ARM64_32_NOMRAL + +# CHECK_X86_64_NOMRAL: Sections: +# CHECK_X86_64_NOMRAL: Idx Name Size VMA Type +# CHECK_X86_64_NOMRAL: 0 __text 00000000 00000000001002c8 TEXT + +# CHECK_ARM64_32_NOMRAL: Sections: +# CHECK_ARM64_32_NOMRAL: Idx Name Size VMA Type +# CHECK_ARM64_32_NOMRAL: 0 __text 00000000 00104000 TEXT + +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/zero.o +# RUN: %lld -lSystem -arch x86_64 -o %t/zero %t/zero.o -pagezero_size 0 +# RUN: llvm-objdump --macho --section-headers %t/zero | FileCheck %s --check-prefix=CHECK_ZERO + +# CHECK_ZERO: Sections: +# CHECK_ZERO: Idx Name Size VMA Type +# CHECK_ZERO: 0 __text 00000000 0000000000000280 TEXT + +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/x86_64_misalign.o +# RUN: %no_fatal_warnings_lld -lSystem -arch x86_64 -o %t/x86_64_misalign %t/x86_64_misalign.o -pagezero_size 1001 2>&1 | FileCheck %s --check-prefix=LINK +# RUN: llvm-objdump --macho --section-headers %t/x86_64_misalign | FileCheck %s --check-prefix=CHECK_X86_64_MISSALIGNED + +# RUN: llvm-mc -filetype=obj -triple=arm64_32-apple-darwin %s -o %t/arm64_32_misalign.o +# RUN: %no_fatal_warnings_lld-watchos -lSystem -arch arm64_32 -o %t/arm64_32_misalign_4K %t/arm64_32_misalign.o -pagezero_size 1001 2>&1 | FileCheck %s --check-prefix=LINK +# RUN: %no_fatal_warnings_lld-watchos -lSystem -arch arm64_32 -o %t/arm64_32_misalign_16K %t/arm64_32_misalign.o -pagezero_size 4001 2>&1 | FileCheck %s --check-prefix=LINK +# RUN: llvm-objdump --macho --section-headers %t/arm64_32_misalign_4K | FileCheck %s --check-prefix=CHECK_ARM64_32_MISSALIGNED_4K +# RUN: llvm-objdump --macho --section-headers %t/arm64_32_misalign_16K | FileCheck %s --check-prefix=CHECK_ARM64_32_MISSALIGNED_16K + +# LINK: warning: __PAGEZERO size is not page aligned, rounding down to realign + +# CHECK_X86_64_MISSALIGNED: Sections: +# CHECK_X86_64_MISSALIGNED: Idx Name Size VMA Type +# CHECK_X86_64_MISSALIGNED: 0 __text 00000000 00000000000012c8 TEXT + +# CHECK_ARM64_32_MISSALIGNED_4K: Sections: +# CHECK_ARM64_32_MISSALIGNED_4K: Idx Name Size VMA Type +# CHECK_ARM64_32_MISSALIGNED_4K: 0 __text 00000000 00004000 TEXT + +# CHECK_ARM64_32_MISSALIGNED_16K: Sections: +# CHECK_ARM64_32_MISSALIGNED_16K: Idx Name Size VMA Type +# CHECK_ARM64_32_MISSALIGNED_16K: 0 __text 00000000 00008000 TEXT + +.globl _main +_main: