Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the dynamic libraries required by an ELF Binary in C++?

How can I get a list of all the dynamic libraries that is required by an elf binary in linux using C++?

Once I've managed to extract the information (filename?) from the binary I can find the actual file by searching through the PATH, but I haven't been able to find any information regarding extracting unmangled information from the ELF binary.

Thoughts?

like image 753
John Smith Avatar asked Mar 24 '14 14:03

John Smith


People also ask

Which command shows all shared libraries required by a binary executable or another shared library?

Using the ldd Command It outputs the shared libraries required by a program.

Which command would inform the info about dynamically linked library dependencies of executable file?

You can do this with ldd command: NAME ldd - print shared library dependencies SYNOPSIS ldd [OPTION]... FILE...


1 Answers

The list of required shared objects is stored in the so-called dynamic section of the executable. The rough algorithm of getting the necessary info would be something like this:

  1. Parse the ELF header, check that the file is a dynamic executable (ET_EXEC or ET_DYN).
  2. Get the offset and count of the program headers (e_phoff/e_phnum/e_phentsize), check that they're non-zero and valid.
  3. parse the program headers, looking for the PT_DYNAMIC one. Also remember virtual address -> file offset mappings for the PT_LOAD segments.
  4. Once found, parse the dynamic section. Look for the DT_NEEDED and DT_STRTAB entries.

The d_val field of the DT_NEEDED entries is the offset into the DT_STRTAB's string table, which will be the SONAME of the required libraries. Note that since DT_STRTAB entry is the run-time address and not the offset of the string table, you'll need to map it back to a file offset using information stored at step 3.

like image 148
Igor Skochinsky Avatar answered Sep 27 '22 22:09

Igor Skochinsky