<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">It seems I can find the static offset of the Mach-O header pre-initialisation in the crt without using the special dynamic linker synthesised symbols, rather a statically synthesised symbol that I was previously unaware of “ __mh_execute_header". I later add the slide to find the dynamic offset of the Mach-O headers.</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">.align 3</div><div class="">__image_base:</div><div class="">.quad __mh_execute_header</div></blockquote><div class=""><br class=""></div><div class="">I find the slide by subtracting a static pointer to a well known symbol from an RIP-relative access to the same symbol. </div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">__start_static:<br class="">.quad start<div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">      </span>leaq start(%rip), %rdx</div><div class=""><span class="Apple-tab-span" style="white-space:pre">    </span>subq __start_static(%rip), %rdx</div></blockquote><div class=""><br class=""></div><div class="">The crt then gets the stack pointer, static image base and slide, so it can relocate the image and call constructors.</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>void _start_c(long *p, uintptr_t image_base, uintptr_t slide)</div><div class=""><br class=""></div><div class="">I’m not sure about the second use case for the start and end of the “__mod_init_func” section, which would likely be required for linking dyld.</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On 7 Jun 2017, at 11:08 AM, Michael Clark <<a href="mailto:michaeljclark@mac.com" class="">michaeljclark@mac.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">In asm:</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><blockquote class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class="">/* get imagebase and slide for static PIE and ASLR support in x86_64-xnu-musl */</div><div class=""><br class=""></div><div class="">.align 3</div><div class="">__image_base:</div><div class="">.quad segment$start$__TEXT</div><div class="">__start_static:</div><div class="">.quad start</div><div class="">.text</div><div class="">.align 3</div><div class="">.global start</div><div class="">start:</div><div class="">       xor %rbp,%rbp</div><div class="">       mov %rsp,%rdi</div><div class="">       andq $-16,%rsp</div><div class="">       movq __image_base(%rip), %rsi</div><div class="">       leaq start(%rip), %rdx</div><div class="">       subq __start_static(%rip), %rdx</div><div class="">       call __start_c</div></blockquote><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">In C:</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><blockquote class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class="">/* run C++ constructors in __libc_start_main for x86_64-xnu-musl */</div><div class=""><br class=""></div></blockquote><blockquote class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: 0px 0px 0px 40px; border: none; padding: 0px;">typedef void (*__init_fn)(int, char **, char **, char **);<br class="">extern __init_fn  __init_start  __asm("section$start$__DATA$__mod_init_func");<br class="">extern __init_fn  __init_end    __asm("section$end$__DATA$__mod_init_func”);<br class=""><div class=""><br class=""></div><div class="">static void __init_mod(int argc, char **argv, char **envp, char **applep)</div><div class="">{</div><div class="">        for (__init_fn *p = &__init_start; p < &__init_end; ++p) {</div><div class="">                (*p)(argc, argv, envp, applep);</div><div class="">        }</div><div class="">}</div></blockquote><br class="Apple-interchange-newline"></div></blockquote></div><br class=""></body></html>