Does Android support weak symbols?

微笑、不失礼 提交于 2019-11-28 13:01:17

Android doesn't support weak symbol override.

In the recent release android-5.0.2_r1, see the comment at line 539 in linker.cpp source code

/*
 *
 * Notes on weak symbols:
 * The ELF specs are ambigious about treatment of weak definitions in
 * dynamic linking.  Some systems return the first definition found
 * and some the first non-weak definition.   This is system dependent.
 * Here we return the first definition found for simplicity.
 */

This comment exists from version 2.2_r1 (which is in linker.c) to newest version 5.0.2_r1

tl;dr; Android does support weak symbols

Note that this is not android-specific it is equally true for ld-linux.so/ld:

This needs some clarification, because there are 2 cases where weak symbols are used:

  1. static libraries/object files
  2. dynamic libraries/executables

(1) For static libraries and object files can define multiple weak symbols and the correct one (strongest or fist) is picked during compile time linking of final object (.so or executable).

(2) For dynamic libraries weak symbols behave the same way as default ones with one exception. This means there is no such thing as weak symbol override for shared libraries. In other words during relocation/dlsym() the first found (GLOBAL) symbol will be returned: weak or default.

For example (here we assume that none of the objects are -Bsymbolic, this is another exception):

| main.executable <- weak (WEAK) definition of foo()
| -> lib1.so <- strong (DEFAULT) definition of foo()
| -> lib2.so <- uses foo()

the lib2.so will use the main.executalbe's implementation of foo(), despite the fast that lib1.so exports DEFAULT foo().

The exception is that WEAK symbols are allowed to remain unresolved during runtime linking process and result in null reference in most useful cases... when in case of unresolved DEFAULT symbol runtime linker fails.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!