3

An example usage of LD_DEBUG looks like this:

LD_DEBUG=libs applicationName

LD_DEBUG=libs is just an assignment - so how it works (prints result) in bash ?

2 Answers 2

3

LD_DEBUG in that context is an environment variable that you've assigned the value of libs to. This on its own does does nothing except put LD_DEBUG into the environment of the command you just started.

When you start a dynamically linked executable on of the first things that happens is the dynamic linker ld.so(8) does runtime linking and symbol resolution. The code in the dynamic linker that does this can read environment variables, as can any compiled code. This program reads this environment variable and many others and modifies its behavior based upon them. The man page details all of the environment variables that effect the dynamic linker.

4
  • I use UBuntu 15.04 and for 64 bit app following lib is used: /lib64/ld-linux-x86-64.so.2 -> /lib/x86_64-linux-gnu/ld-2.21.so Please see unix.stackexchange.com/questions/231106/…
    – Irbis
    Sep 22, 2015 at 16:29
  • @Irbis yes, that is normal for ELF binaries linked against the glibc2 C runtime on x86_64 platforms. This is all documented in the ld.so(8) man page and not relevant to your question because they behavior is the same for the dynamic linkers documented in that man page (you don't have to worry about this detail because the proper dynamic linker is handled behind the scenes for you).
    – casey
    Sep 22, 2015 at 16:37
  • One more thing: when I use LD_DEBUG=help [whatever] it prints help and doesn't check if an app exists, when I write LD_DEBUG=help and then after press Enter I wrtie [whatever] it doesn't work (command not found), but echo $LD_DEBUG prints help. How it works ?
    – Irbis
    Sep 23, 2015 at 7:04
  • @Irbis it works by exactly the same mechanism above. You set that environmental variable to help and when the dynamic linker takes a look at the variable and sees help it takes a specific action.
    – casey
    Sep 23, 2015 at 12:53
1

LD_DEBUG is evaluated in ld.so.1 (the dynamic linker that is called from the ELF binary)

Check the man page for the dynamic linker at:

http://schillix.sourceforge.net/man/man1/ld.so.1.1.html

Nothing is done in the shell, everything is done "by" the executed binary.

BTW: the command ldd does nothing special, it just sets some LD_* variables and then tries to start the command. If no options are specified, ldd just sets LD_TRACE_LOADED_OBJECTS_[AE], see

http://schillix.sourceforge.net/man/man1/ldd.1.html

and the related sourcecode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.