2

Observations

When the Linux executable is compiled as PIE (Position Independent Executable, default on Ubuntu 18.04), the symbols from shared libraries (e.g. libc) will be resolved when the program starts executing, setting LD_BIND_NOW environment variable to null will not defer this process.

However, if the executable is compiled with the -no-pie flag, the symbol resolution can be controlled by LD_BIND_NOW.

Question

Is it possible to control when the symbols from share libraries to be resolved on a ELF PIE executable?

Below is the test code and system info,

ubuntu: 18.04
kernel: Linux 4.15.0-50-generic #54-Ubuntu SMP Mon May 6 18:46:08 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
gcc: gcc (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0


$ gcc -o helloworld helloworld.c

$ file helloworld
helloworld: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 3.2.0, BuildID[sha1]=70143fcc329797b2d0af84143ce0125775ab330f, not stripped

#include <stdio.h>
int main() {
    printf("Hello world!\n");
}
1

Yes, it is possible while compiling with clang: clang -o helloworld helloworld.c. To test it, run:

export LD_DEBUG=reloc,symbols
./helloWorld

with LD_BIND_NOW null and then with LD_BIND_NOW equal 1. You will see that in the first case the call to printf is indeed being resolved at demand, and in the second case it will be resolved before transferring control to the program.

LD_BIND_NOW=null

LD_BIND_NOW_NULL

LD_BIND_NOW=1

LD_BIND_NOW_1

It doesn't work for GCC for some reason, as you have noticed, at least without specifying relevant options.

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.