Bionic链接过程的相关分析

由mk文件可知crtbegin_dynamic.o由crtbegin_dynamic.S生成。

GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtbegin_dynamic.o

$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin_dynamic.S

@mkdir -p $(dir $@)

$(TARGET_CC) $(libc_crt_target_cflags) -o $@ -c $<

crtbegin_dynamic.S中:

_start:

    略

    call     __libc_init

0:

    jmp   main

先调libc.so中的__libc_init函数,然后直接转跳main执行。

而glibc中的_star则是:

_start:

略

/* Call the user's main function, and exit with its value.

   But let the libc call main.    */

call BP_SYM (__libc_start_main)@PLT

调libc.so中的__libc_start_main函数完成初始化和调用main函数。

之前用Android NDK提供的编译工具出来的程序运行时出现:“没有那个文件或目录”,原因是其无法找到运行时加载动态库的程序。这个程序在linux中是/lib/ld-linux.so.2,而使用NDK编译出来的程序调用的是/system/bin/linker。

Contents of section .interp:

80480f4 2f737973 74656d2f 62696e2f 6c696e6b /system/bin/link

8048104 657200 er.

将bionic编译生成的linker和libc.so分别放到/system/bin/和system/lib/中后,使用NDK提供的编译器、库、头文件和入口库编译出来的helloworld程序(gcc的优化以全关掉)可以在ubuntu下正常运行!

Dynamic Linker:

Bionic comes with its own dynamic linker (just like ld.so on Linux really comes from GLibc). This linker does not support all the relocations generated by other GCC ARM toolchains.