Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux ldd command

Linux ldd command

Linux Command Reference Linux Command Reference


ldd (List Dynamic Dependencies) is a very useful command-line tool in Linux for displaying the dynamic libraries that a program or shared library depends on. When you run a program on Linux, the system needs to load the various shared libraries (.so files) it depends on; ldd is the tool used to view these dependencies.


Basic syntax of the ldd command

ldd [选项] 可执行文件或共享库

Description of common options and parameters

Option Description
-v Display detailed version information
-u Display unused direct dependencies
-d Perform relocation and report missing functions
-r Perform relocation and report missing functions and data
--help Display help information

How the ldd command works

ldd is actually a script that runs the target program by setting special environment variables, causing the program to display its dependent library information at load time instead of executing normally. Specifically:

  1. For executables in ELF format, ldd parses their dynamic segment
  2. checks DT_NEEDED entries to determine the required shared libraries
  3. looks for these library files in the system library paths
  4. displays the full path and memory address mapping of each library

Usage examples

Example 1: Viewing the dependencies of a simple program

ldd /bin/ls

Typical output:

linux-vdso.so.1 (0x00007ffd5a3f0000)

libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f8e4a3b0000)

libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8e4a1c0000)

libpcre2-8.so.0 => /usr/lib/x86_64-linux-gnu/libpcre2-8.so.0 (0x00007f8e4a130000)

libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f8e4a120000)

/lib64/ld-linux-x86-64.so.2 (0x00007f8e4a400000)

libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f8e4a100000)

Example 2: Checking unused direct dependencies

ldd -u /usr/bin/python3

Example 3: Viewing dependencies in verbose mode

ldd -v /usr/bin/gcc


Practical Application Scenarios

Scenario 1: Resolving a "library not found" error

When a program fails to run due to a missing library, ldd can help quickly locate the problem:

ldd ./my_program | grep "not found"

Scenario 2: Checking for library version conflicts

ldd -r ./my_program

Scenario 3: Verifying that library paths are correct

ldd /path/to/your/executable | awk '{print $3}' | xargs ls -la


Notes and common issues

  1. Security Warning: Do not use ldd on untrusted executables, because it actually attempts to execute part of the program's code

  2. Alternative:objdump -p /path/to/program | grep NEEDED
  3. - Alternative: `objdump -p /path/to/program | grep NEEDED`
    1. Statically linked programs: For fully statically linked programs, ldd displays "not a dynamic executable"

    2. Cross-compilation environments: In a cross-compilation environment, you need to use the ldd tool for the corresponding platform

    3. Common mistakes:

    4. ldd: ./program: No such file or directory -> possibly a missing interpreter or the program itself does not exist
    5. ldd: exited with unknown exit code -> the program may have crashed during runtime
    6. - `ldd: ./program: No such file or directory` -> possibly a missing interpreter or the program itself does not exist - `ldd: exited with unknown exit code` -> the program may have crashed during runtime

      Advanced Tips

      1. Use readelf together to view more detailed dependency information

      readelf -d /path/to/program | grep NEEDED
      
      

      2. Use the LD_TRACE_LOADED_OBJECTS environment variable

      LD_TRACE_LOADED_OBJECTS=1 /path/to/program
      
      

      3. Check the library dependency tree

      ldd /path/to/library.so | awk '{print $3}' | xargs ldd
      
      

      Summary

      ldd is an indispensable tool in Linux system administration and program debugging; it can:

      Mastering the ldd command can greatly improve your efficiency in solving dependency problems in a Linux environment.


      Linux Command Reference Linux Command Reference