2017-05-06 47 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瞭解更多詳情。