[llvm-dev] LLD support for ld64 mach-o linker synthesised symbols

Michael Clark via llvm-dev llvm-dev at lists.llvm.org
Wed Jun 7 17:55:00 PDT 2017

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.

.align 3
.quad __mh_execute_header

I find the slide by subtracting a static pointer to a well known symbol from an RIP-relative access to the same symbol. 

.quad start

	leaq start(%rip), %rdx
	subq __start_static(%rip), %rdx

The crt then gets the stack pointer, static image base and slide, so it can relocate the image and call constructors.

	void _start_c(long *p, uintptr_t image_base, uintptr_t slide)

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.

> On 7 Jun 2017, at 11:08 AM, Michael Clark <michaeljclark at mac.com> wrote:
> In asm:
> /* get imagebase and slide for static PIE and ASLR support in x86_64-xnu-musl */
> .align 3
> __image_base:
> .quad segment$start$__TEXT
> __start_static:
> .quad start
> .text
> .align 3
> .global start
> start:
>        xor %rbp,%rbp
>        mov %rsp,%rdi
>        andq $-16,%rsp
>        movq __image_base(%rip), %rsi
>        leaq start(%rip), %rdx
>        subq __start_static(%rip), %rdx
>        call __start_c
> In C:
> /* run C++ constructors in __libc_start_main for x86_64-xnu-musl */
> typedef void (*__init_fn)(int, char **, char **, char **);
> extern __init_fn  __init_start  __asm("section$start$__DATA$__mod_init_func");
> extern __init_fn  __init_end    __asm("section$end$__DATA$__mod_init_func”);
> static void __init_mod(int argc, char **argv, char **envp, char **applep)
> {
>         for (__init_fn *p = &__init_start; p < &__init_end; ++p) {
>                 (*p)(argc, argv, envp, applep);
>         }
> }

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170608/6346691b/attachment.html>

More information about the llvm-dev mailing list