{"id":339,"date":"2021-12-01T11:59:22","date_gmt":"2021-12-01T03:59:22","guid":{"rendered":"http:\/\/pareto.fun\/?p=339"},"modified":"2021-12-01T14:28:01","modified_gmt":"2021-12-01T06:28:01","slug":"android8%e4%b8%adget_libdl_info%e7%9a%84%e7%96%91%e9%97%ae","status":"publish","type":"post","link":"http:\/\/pareto.fun\/?p=339","title":{"rendered":"android8\u4e2dget_libdl_info\u7684\u7591\u95ee"},"content":{"rendered":"\n<p>\u5728android8\u6e90\u7801\u8ffd\u8e2adlopen\u8c03\u7528\u8fc7\u7a0b\u65f6\uff0c\u9047\u5230\u95ee\u9898\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"543\" height=\"118\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image.png\" alt=\"\" class=\"wp-image-340\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image.png 543w, http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-300x65.png 300w\" sizes=\"(max-width: 543px) 100vw, 543px\" \/><\/figure>\n\n\n\n<p>\u5982\u56fe\uff0c\u9700\u8981\u627e__loader_dlopen\u3002\u5728bionic\u76ee\u5f55\u5168\u5c40\u627e\u7b26\u53f7 \uff0c\u53ea\u5728ANDROID_LIBDL_STRTAB\u627e\u5230<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"803\" height=\"571\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-1.png\" alt=\"\" class=\"wp-image-342\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-1.png 803w, http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-1-300x213.png 300w, http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-1-768x546.png 768w\" sizes=\"(max-width: 803px) 100vw, 803px\" \/><\/figure>\n\n\n\n<p>ANDROID_LIBDL_STRTAB \u53c8\u53ea\u5728 g_libdl_symtab \u4f7f\u7528<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">static ElfW(Sym) g_libdl_symtab[] = {\n  \/\/ Total length of libdl_info.strtab, including trailing 0.\n  \/\/ This is actually the STH_UNDEF entry. Technically, it's\n  \/\/ supposed to have st_name == 0, but instead, it points to an index\n  \/\/ in the strtab with a \\0 to make iterating through the symtab easier.\n  ELFW(SYM_INITIALIZER)(sizeof(ANDROID_LIBDL_STRTAB) - 1, nullptr, 0),\n  ELFW(SYM_INITIALIZER)(  0, &amp;__dlopen, 1),\n  ELFW(SYM_INITIALIZER)( 16, &amp;, 1),\n  ELFW(SYM_INITIALIZER)( 33, &amp;__dlsym, 1),\n  ELFW(SYM_INITIALIZER)( 48, &amp;__dlerror, 1),\n  ELFW(SYM_INITIALIZER)( 65, &amp;__dladdr, 1),\n  ELFW(SYM_INITIALIZER)( 81, &amp;__android_update_LD_LIBRARY_PATH, 1),\n  ELFW(SYM_INITIALIZER)(121, &amp;__android_get_LD_LIBRARY_PATH, 1),\n  ELFW(SYM_INITIALIZER)(158, &amp;dl_iterate_phdr, 1),\n  ELFW(SYM_INITIALIZER)(183, &amp;__android_dlopen_ext, 1),\n  ELFW(SYM_INITIALIZER)(211, &amp;__android_set_application_target_sdk_version, 1),\n  ELFW(SYM_INITIALIZER)(263, &amp;__android_get_application_target_sdk_version, 1),\n  ELFW(SYM_INITIALIZER)(315, &amp;__android_init_anonymous_namespace, 1),\n  ELFW(SYM_INITIALIZER)(357, &amp;__android_create_namespace, 1),\n  ELFW(SYM_INITIALIZER)(391, &amp;__dlvsym, 1),\n  ELFW(SYM_INITIALIZER)(407, &amp;__android_dlwarning, 1),\n  ELFW(SYM_INITIALIZER)(434, &amp;__cfi_fail, 1),\n  ELFW(SYM_INITIALIZER)(452, &amp;__android_link_namespaces, 1),\n  ELFW(SYM_INITIALIZER)(485, &amp;__android_get_exported_namespace, 1),\n#if defined(__arm__)\n  ELFW(SYM_INITIALIZER)(525, &amp;__dl_unwind_find_exidx, 1),\n#endif\n};\n\n\u7528\u5230\u7684\u5b8f\u5b9a\u4e49\uff1a\n\n#define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \\\n    { name_offset, \\\n      reinterpret_cast&lt;Elf32_Addr&gt;(value), \\\n      \/* st_size *\/ 0, \\\n      ((shndx) == 0) ? 0 : (STB_GLOBAL &lt;&lt; 4), \\\n      \/* st_other *\/ 0, \\\n      shndx, \\\n    }\n\n#define ELFW(what) ELF32_ ## what\n<\/pre>\n\n\n\n<p>\u5c06\u5b8f\u5c55\u5f00<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"173\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-2-1024x173.png\" alt=\"\" class=\"wp-image-343\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-2-1024x173.png 1024w, http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-2-300x51.png 300w, http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-2-768x130.png 768w, http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-2-1536x260.png 1536w, http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-2.png 1885w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u5047\u8bbe\u771f\u628a\u8fd9\u4e2a\u4e1c\u897f\u5f53\u4f5c\u7b26\u53f7\u8868\uff0c\u90a3\u4e48 __loader_dlopen \u51fd\u6570\u5b9e\u73b0\u5bf9\u5e94\u7684\u51fd\u6570\u5c31\u662f__dlopen \u3002\u4f46\u662fELF\u6587\u4ef6\u7b26\u53f7\u8868\u8fd8\u80fd\u8fd9\u4e48\u73a9\u5417\uff1f\uff1f \u7ed3\u5408\u6ce8\u91ca\u548c g_libdl_symtab \u7684\u4f7f\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Fake out a hash table with a single bucket.\n\/\/\n\/\/ A search of the hash table will look through g_libdl_symtab starting with index 1, then\n\/\/ use g_libdl_chains to find the next index to look at. g_libdl_chains should be set up to\n\/\/ walk through every element in g_libdl_symtab, and then end with 0 (sentinel value).\n\/\/\n\/\/ That is, g_libdl_chains should look like { 0, 2, 3, ... N, 0 } where N is the number\n\/\/ of actual symbols, or nelems(g_libdl_symtab)-1 (since the first element of g_libdl_symtab is not\n\/\/ a real symbol). (See soinfo_elf_lookup().)\n\/\/\n\/\/ Note that adding any new symbols here requires stubbing them out in libdl.\n\n\nsoinfo* get_libdl_info(const char* linker_path, const link_map&amp; linker_map) {\n  if (__libdl_info == nullptr) {\n    __libdl_info = new (__libdl_info_buf) soinfo(&amp;g_default_namespace, linker_path, nullptr, 0, 0);\n    __libdl_info-&gt;flags_ |= FLAG_LINKED;\n    __libdl_info-&gt;strtab_ = ANDROID_LIBDL_STRTAB;\n    __libdl_info-&gt;symtab_ = g_libdl_symtab;\n    __libdl_info-&gt;nbucket_ = sizeof(g_libdl_buckets)\/sizeof(unsigned);\n    __libdl_info-&gt;nchain_ = sizeof(g_libdl_chains)\/sizeof(unsigned);\n    __libdl_info-&gt;bucket_ = g_libdl_buckets;\n    __libdl_info-&gt;chain_ = g_libdl_chains;\n    __libdl_info-&gt;ref_count_ = 1;\n    __libdl_info-&gt;strtab_size_ = sizeof(ANDROID_LIBDL_STRTAB);\n    __libdl_info-&gt;local_group_root_ = __libdl_info;\n    __libdl_info-&gt;soname_ = \"ld-android.so\";\n    __libdl_info-&gt;target_sdk_version_ = __ANDROID_API__;\n    __libdl_info-&gt;generate_handle();\n    __libdl_info-&gt;link_map_head.l_addr = linker_map.l_addr;\n    __libdl_info-&gt;link_map_head.l_name = linker_map.l_name;\n    __libdl_info-&gt;link_map_head.l_ld = linker_map.l_ld;\n#if defined(__work_around_b_24465209__)\n    strlcpy(__libdl_info-&gt;old_name_, __libdl_info-&gt;soname_, sizeof(__libdl_info-&gt;old_name_));\n#endif\n  }\n\n  return __libdl_info;\n}<\/pre>\n\n\n\n<p>\u4f3c\u4e4e\u662f\u4f2a\u9020\u4e86 soinfo_elf_lookup \u67e5\u627e\u7b26\u53f7\u7528\u7684soinfo\u3002<\/p>\n\n\n\n<p>\u5e0c\u671b\u770b\u5230\u7684\u5927\u4f6c\u80fd\u591f\u89e3\u91ca\u4e00\u4e0b\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u5bf9\u7167android7\u7248\u672c\u4ee3\u7801<\/p>\n\n\n\n<p><a href=\"https:\/\/android.googlesource.com\/platform\/bionic\/+\/android-7.0.0_r1\/linker\/dlfcn.cpp\">https:\/\/android.googlesource.com\/platform\/bionic\/+\/android-7.0.0_r1\/linker\/dlfcn.cpp<\/a><\/p>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u770b\u51fadlopen \u5c31\u662f\u8c03\u7528 __dlopen \u51fd\u6570\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4ee5\u4e0a\u4e3a\u4ee3\u7801\u5206\u6790\u3002<\/p>\n\n\n\n<p>\u5728\u505a\u5b9e\u9645\u9a8c\u8bc1\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u8c03\u7528\u7684\u662f<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">__dl___dlopen(char const*, int, void const*)\u51fd\u6570\u3002\n\n<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"709\" height=\"83\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-3.png\" alt=\"\" class=\"wp-image-346\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-3.png 709w, http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-3-300x35.png 300w\" sizes=\"(max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p>\u5728bionic\/linker\/Android.bp \u6587\u4ef6\u4e0b \uff0c \u8fd9\u7c7b\u5927\u578b\u7684\u5de5\u7a0b\uff0c\u7ed3\u6784\u4f1a\u51fa\u4e4e\u610f\u6599\u7684\u590d\u6742\u3002\u3002\u3002\u3002\u3002\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"730\" height=\"144\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-4.png\" alt=\"\" class=\"wp-image-348\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-4.png 730w, http:\/\/pareto.fun\/wp-content\/uploads\/2021\/12\/image-4-300x59.png 300w\" sizes=\"(max-width: 730px) 100vw, 730px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5728android8\u6e90\u7801\u8ffd\u8e2adlopen\u8c03\u7528\u8fc7\u7a0b\u65f6\uff0c\u9047\u5230\u95ee\u9898\u3002 \u5982\u56fe\uff0c\u9700\u8981\u627e__loader_dlop [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[20],"_links":{"self":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/339"}],"collection":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=339"}],"version-history":[{"count":3,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/339\/revisions"}],"predecessor-version":[{"id":349,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/339\/revisions\/349"}],"wp:attachment":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=339"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}