What is the first argument to Apple's getsectiondata function?

26 views Asked by At

I need to access binary resources wrapped by ld -sectcreate. Some newer materials (such as comments in this answer) told that I should use getsectiondata instead of getsectbyname to correctly work with ASLR. However, there is an extra first argument to getsectiondata that is a struct I can find very few materials. What is it? And how can I get it (from a program and from shared lib)?

1

There are 1 answers

0
jiandingzhe On

Finally I find a way to get current image's header. Apple provides a function dladdr in dlfcn.h that can obtain image information from arbitrary address "belonging" to that image. So you can create a global variable as a grabber, and use its address at runtime to grab the image header.

#include <dlfcn.h>
#include <mach-o/getsect.h>

static int rcs_addr_handle = 0;

std::pair<const void*, size_t> get_resource( const char* sec_name )
{
    // get image header from a global address
    Dl_info img_info = {};
    int img_index = dladdr( &rcs_addr_handle, &img_info );
    if ( img_index == 0 ) return { nullptr, 0 };
    auto* header = (struct mach_header_64*) img_info.dli_fbase;

    // get resource address
    unsigned long size = 0;
    auto* addr = getsectiondata( header, "my_rcs_segment", sec_name, &size );
    return { addr, size };
}