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

Michael Clark via llvm-dev llvm-dev at lists.llvm.org
Tue Jun 6 16:08:29 PDT 2017


Hi Folks,

I have a question regarding LLD support for ld64 mach-o linker synthesised symbols. I did a quick search of the LLD source and I can not find support for them so before I start trying to use lld I thought I would ask.

I have found a couple of cases where they are essential. i.e. where there is no other way to get the required information, such as getting the address of the mach-o headers of the current process, with ASLR enabled, if the process is not dyld as exec on macOS only provides the mach header address to dyld (*1). They are used inside of dyld and I am now using them in “x86_64-xnu-musl”.

It’s possible to resolve a mach-o segment offset or a mach-o section offset using these special ld64 linker synthesised symbols. See resolveUndefines:

- https://opensource.apple.com/source/ld64/ld64-274.2/src/ld/Resolver.cpp.auto.html <https://opensource.apple.com/source/ld64/ld64-274.2/src/ld/Resolver.cpp.auto.html>

There are 4 special symbol prefixes for the mach-o linker synthesised symbols:

- segment$start$__SEGMENT
- segment$end$__SEGMENT
- section$start$__SEGMENT$__section
- section$end$__SEGMENT$__section

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);
        }
}

Michael.

[1] https://github.com/opensource-apple/xnu/blob/dc0628e187c3148723505cf1f1d35bb948d3195b/bsd/kern/kern_exec.c#L1072-L1111 <https://github.com/opensource-apple/xnu/blob/dc0628e187c3148723505cf1f1d35bb948d3195b/bsd/kern/kern_exec.c#L1072-L1111>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170607/eb534f68/attachment.html>


More information about the llvm-dev mailing list