2017-05-06 45 views
0

当我为android共享库编写我自己的动态加载器时,出现了这样的问题:在库libnativeloader.so中有这样的plt入口android_init_namespaces @ plt,但没有符号android_init_namespaces。我发现这样的代码在仿生:什么共享库包含android_init_namespaces?

LIBC { 
    global: 
    android_dlopen_ext; 
    dl_iterate_phdr; 
    dladdr; 
    dlclose; 
    dlerror; 
    dlopen; 
    dlsym; 
    local: 
    *; 
}; 

LIBC_N { 
    global: 
    android_init_namespaces; 
    android_create_namespace; 
    dlvsym; 
} LIBC; 

LIBC_PLATFORM { 
global: 
    android_get_application_target_sdk_version; 
    android_set_application_target_sdk_version; 
    android_get_LD_LIBRARY_PATH; 
    android_update_LD_LIBRARY_PATH; 
} LIBC_N; 

在这种情况下,似乎这个符号是在libc.so.但libc.so中没有这样的符号。 任何人都可以帮助解决这个问题吗?

回答

0

我想你是在谈论Android N.下面的一切都是基于这个假设。

这些符号不是来自任何共享库,它们在链接器中实现,并通过源代码中的手工制作soinfo导出。

static ElfW(Sym) g_libdl_symtab[] = { 
    // Total length of libdl_info.strtab, including trailing 0. 
    // This is actually the STH_UNDEF entry. Technically, it's 
    // supposed to have st_name == 0, but instead, it points to an index 
    // in the strtab with a \0 to make iterating through the symtab easier. 
    ELFW(SYM_INITIALIZER)(sizeof(ANDROID_LIBDL_STRTAB) - 1, nullptr, 0), 
    ELFW(SYM_INITIALIZER)( 0, &dlopen, 1), 
    ELFW(SYM_INITIALIZER)( 7, &dlclose, 1), 
    ELFW(SYM_INITIALIZER)(15, &dlsym, 1), 
    ELFW(SYM_INITIALIZER)(21, &dlerror, 1), 
    ELFW(SYM_INITIALIZER)(29, &dladdr, 1), 
    ELFW(SYM_INITIALIZER)(36, &android_update_LD_LIBRARY_PATH, 1), 
    ELFW(SYM_INITIALIZER)(67, &android_get_LD_LIBRARY_PATH, 1), 
    ELFW(SYM_INITIALIZER)(95, &dl_iterate_phdr, 1), 
    ELFW(SYM_INITIALIZER)(111, &android_dlopen_ext, 1), 
    ELFW(SYM_INITIALIZER)(130, &android_set_application_target_sdk_version, 1), 
    ELFW(SYM_INITIALIZER)(173, &android_get_application_target_sdk_version, 1), 
    ELFW(SYM_INITIALIZER)(216, &android_init_namespaces, 1), 
    ELFW(SYM_INITIALIZER)(240, &android_create_namespace, 1), 
    ELFW(SYM_INITIALIZER)(265, &dlvsym, 1), 
    ELFW(SYM_INITIALIZER)(272, &android_dlwarning, 1), 
#if defined(__arm__) 
    ELFW(SYM_INITIALIZER)(290, &dl_unwind_find_exidx, 1), 
#endif 
}; 

// This is used by the dynamic linker. Every process gets these symbols for free. 
soinfo* get_libdl_info() { 
    if (__libdl_info == nullptr) { 
    __libdl_info = new (__libdl_info_buf) soinfo(&g_default_namespace, "libdl.so", nullptr, 0, RTLD_GLOBAL); 
    __libdl_info->flags_ |= FLAG_LINKED; 
    __libdl_info->strtab_ = ANDROID_LIBDL_STRTAB; 
    __libdl_info->symtab_ = g_libdl_symtab; 
    __libdl_info->nbucket_ = sizeof(g_libdl_buckets)/sizeof(unsigned); 
    __libdl_info->nchain_ = sizeof(g_libdl_chains)/sizeof(unsigned); 
    __libdl_info->bucket_ = g_libdl_buckets; 
    __libdl_info->chain_ = g_libdl_chains; 
    __libdl_info->ref_count_ = 1; 
    __libdl_info->strtab_size_ = sizeof(ANDROID_LIBDL_STRTAB); 
    __libdl_info->local_group_root_ = __libdl_info; 
    __libdl_info->soname_ = "libdl.so"; 
    __libdl_info->target_sdk_version_ = __ANDROID_API__; 
    __libdl_info->generate_handle(); 
#if defined(__work_around_b_24465209__) 
    strlcpy(__libdl_info->old_name_, __libdl_info->soname_, sizeof(__libdl_info->old_name_)); 
#endif 
    } 

查询https://android.googlesource.com/platform/bionic/+/android-7.0.0_r1/linker/dlfcn.cpp了解更多详情。