musl libc ldso dynamic loading research notes: 01

foreword

  • musl is a lightweight standard C library built on top of system calls. It can be considered as a [user mode] C library, which belongs to the same category as glibc or uClibc.

  • The musl-based gcc toolchain includes a cross-compilation toolchain, which can be used to compile Linux or other operating systems. For example, the current Lite-OS and RT-Smart use musl gcc as the compilation toolchain, and use musl libc as the user mode application. C library.

  • musl is based on MIT authorization, open source, lightweight, and free. It is estimated that more and more operating systems have begun to adopt musl as the standard C library

  • Recently, I have been studying the ldso of musl libc, which is the dynamic loading function. The dynamic loading function of musl is integrated in musl libc.so.

elf format file

  • musl itself integrates the dynamic loading function ldso, after a preliminary look at the code, it is found that it supports dynamic compiled application loading in elf format.

  • For applications compiled using the musl gcc toolchain (cross-compilation toolchain), you can readelf -l xxx.elfcheck whether there is dynamic link information in the elf file. If it is included, you need to use musl ldso to load the dependent shared library

  • Example: Check whether a dynamically linked application exists [interpreter segment] information: INTERPtype of

command: aarch64-linux-musleabi-readelf.exe -l glib_gio_test_s.elforreadelf.exe -l glib_gio_test_s.elf

It is found that there are INTERPsegments of information,[Requesting program interpreter: /lib/ld-musl-aarch64.so.1]

insert image description here

View the shared library that the elf file depends on

  • Statically compiled elf has no dynamic link information and does not depend on shared libraries, while dynamically compiled and linked elf files have dynamic link information. You can check the dependent shared libraries through aarch64-linux-musleabi-readelf.exe -d glib_gio_test_s.elforreadelf.exe -d glib_gio_test_s.elf

insert image description here

Check the head information of elf

  • Use aarch64-linux-musleabi-readelf.exe -h glib_gio_test_s.elfor readelf.exe -h glib_gio_test_s.elfto view the header information of the application elf, such as obtaining the address of the entry function, etc.

insert image description here

View elf symbol information

  • The so-called elf symbols are some variable names, function names, arrays, etc., which can still be readelfviewed through

aarch64-linux-musleabi-readelf.exe -s glib_gio_test_s.elfOr readelf.exe -s glib_gio_test_s.elf, note that because some applications have a lot of symbols in the elf file, you can output it to a text file and then use the file viewing tool to view it

aarch64-linux-musleabi-readelf.exe -s glib_gio_test_s.elf > sym_01.s, here export the symbol to sym_01.sa file, and then use the text viewer to open it for viewing

insert image description here

elf viewing tool

  • The above is to view the elf file through readelf, so is there any other tool to view the elf file?

  • Recommended use: EmEditorbinary viewing tool, which can view, search, and edit binary files, and elf files are actually binary files.

insert image description here

  • It is recommended to use Die, the famous Detect It Easy tool to analyze and view the elf file, which can be viewed in binary, disassembled, and viewed in each segment, section, etc.

  • Open an elf file with die

insert image description here

  • Use die to view elf file information: elf header
    insert image description here

summary

  • This article mainly records the viewing of elf files, and has a preliminary understanding of the files to be dynamically loaded, that is, before studying dynamic loading, you must be familiar with the elf format files, file composition, file header, file Program Header, each section of the file, file section, which content of the elf file needs to be loaded dynamically

  • At present, I understand that an elf file has only one header and one or more Program Headers. The size of a Program Header is fixed, that is, a structure. The content inside describes a segment of information. How many does an elf file have? Program Header? This needs to be obtained by parsing the header of the elf file: elf Header

  • It is recommended to check the standard specification of elf, be familiar with elf, its composition and loading mechanism, which is very important for the subsequent study of dynamic loading.

Guess you like

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