5

This is an OSX linker question. I don't think OSX (BSD or Mach layers) cares how large the zero page is or indeed whether it even exists. I think this is a tools thing. But that's my opinion and that's why I'm asking.

-pagezero_size size: By default the linker creates an unreadable segment starting at address zero named __PAGEZERO. Its existence will cause a bus error if a NULL pointer is dereferenced.

This is clear; it's for trapping NULL ptrs. On a 32b OSX system, the size of the segment is 4KB which is the system pagesize. But on current 64b system, the size of this segment increases to 4GB. Why doesn't it remain at the system pagesize 4KB or the architecture's maximum pagesize, 2MB? This means I can't use 32b absolute addressing at all.

Are there any problems with using this flag and overriding the default? Apple Store rules, ...?

(This feature is specific to the OSX ld64 linker. The feature dates at least to ld64-47.2 March 2006. Address Space Layout Randomization and 64b support start with Leopard in October 2007.)

8
  • 1
    I'm going to guess, having looked over x86_64 addressing modes, that this doesn't 'cost' anything since the addressing modes don't really support short addresses. At the same time, if you forbid the lower 4GB of addressing then you force programs to be 64b clean. However, this is an OSX thing and GNU ld doesn't do this and doesn't provide this switch.
    – Olsonist
    Oct 24, 2017 at 20:19
  • The code in ld64/Options.cpp says: case CPU_TYPE_ARM64: case CPU_TYPE_X86_64: // first 4GB for x86_64 on all OS's fZeroPageSize = 0x100000000ULL; break; So it's 4GB for x86_64 and ARMv8 but doesn't say why.
    – Olsonist
    Oct 25, 2017 at 16:22
  • A trivial hello world program compiled+linked with "cc -pagezero_size=0x1000 test.c" dies immediately with "Killed: 9". Looking at the a.out with "otool -l a.out|more" it is missing a __PAGEZERO section. Same result with "cc -pagezero_size=0x0000000100000000 test.c". So you can't trivially set the PAGEZERO size to 4096.
    – Olsonist
    Oct 25, 2017 at 21:24
  • 1
    "This means I can't use 32b absolute addressing at all" - which is a good thing, because the macho64 file format simply doesn't support it. Even if you changed the flag, the assembler is not going to let you generate a macho64 object file containing 32 bit absolute addressing. See here and here. The purpose seems to be to invalidate all 32 bit pointers, which has advantages.
    – Crowman
    Oct 25, 2017 at 22:12
  • Thanks but I was going to mmap() a page to the low 4GB and then use normal 64b register addressing. I had a good reason for this. Yeah, I do think that Apple is trying to invalidate all 32b points for 64b cleanliness reasons as well as ASLR. And yep, I don't think there's any way of getting around this.
    – Olsonist
    Oct 25, 2017 at 22:24

1 Answer 1

5

The -pagezero_size option is a linker option, not a compiler option. So, when you use the compiler to drive linking, you need to pass it as -Wl,-pagezero_size,0x1000 (or whatever size you want). This works fine. The Wine project, to which I'm a contributor, relies on this for compatibility of its 64-bit build.

My understanding as to why the default page-zero size is 4GB for 64-bit is to catch cases where a pointer was inadvertently stored in a 32-bit variable and thus truncated. When it's eventually cast back to a pointer, it will be in the low 4GB and therefore invalid. Any attempt to dereference it will cause an access violation.

Update:

It seems that -pagezero_size is recognized as a compiler option, too, and works just fine in my testing. Either way, I get a functioning executable and otool shows a __PAGEZERO segment of the desired size.

What versions of the tools are you using? I'm using Xcode 8 on Sierra (10.12.6):

$ cc --version
Apple LLVM version 8.1.0 (clang-802.0.41)
...
1
  • Command line clang. Apple LLVM version 9.0.0 (clang-900.0.38) which should be latest+greatest. Thanks. This works for me. It makes sense that Wine uses this and it's good to know I'm in well trodden territory because I thought I wasn't.
    – Olsonist
    Oct 26, 2017 at 0:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.