musl libc ldso dynamic loading research notes: dynamic library loading order and initialization order

foreword

  • In what order does musl ldso load the shared libraries of dynamically linked applications? If there are dependencies between shared libraries, how does musl ldso handle which shared library is initialized first?

  • The code of musl ldso can be found in musl official code: ldso\dlstart.cand ldso\dynlink.c, where the dynamic library loading part is mainly implemented in the function ldso\dynlink.cin load_library.

Dynamic library loading order

  • The current loading order of musl ldso readelf -d xx.sois consistent with that shown in the list of dependent dynamic libraries. This loading order is the order of dependent libraries when linking. If you want to adjust this order, just adjust the order of each dependent library when linking

insert image description here

  • The loading order of the dynamic library is to load the dependent dynamic library into the memory in order, and this order is not important in most cases

  • If two shared libraries without dependencies need to be loaded, the initialization of the dynamic library has a sequential relationship. It is recommended to adjust the sequence of the dynamic library when linking, so that the dynamic library that needs to be initialized first is placed in front (left).

  • Dependent library initialization order: this is important, the dependent ones are initialized first, and the design of musl ldso meets this requirement, such as A->B->C, then the dynamic library initialization order is: C->B->A.

insert image description here

insert image description here

insert image description here

Library initialization sequence

  • Whether there is a dependency relationship between two dynamic libraries is reflected in the dependency list of the library.

insert image description here

  • For multiple library dependencies, initialize the last-end dependent library, such as A->B->C, the initialization order is: C->B->A

  • If the two libraries have no dependencies, the initialization order is initialized according to the list order of the application dependent libraries

  • You can adjust the linking order of the library, and adjust the initialization order of two (two groups) libraries that do not depend on each other

insert image description here

  • Adjust the order of dynamic libraries when linking, and put lib1 in front, then lib3, which lib1 depends on, will be initialized first. So the initialization order can adjust the location of the dynamic library when linking.

insert image description here

summary

  • To verify the dynamic library loading of musl ldso, you can use gdb to debug and increase the way of LOG printing

  • Although the current dynamic library loading of musl ldso is integrated in musl libc.so, the functions are still complete and complete

  • You can get familiar with the process and implementation method of ldso dynamic loading by viewing the code of musl

Guess you like

Origin blog.csdn.net/tcjy1000/article/details/132460404