2

I'm digging in dynamic relocation process and i created a very simple shared object:

int func_1(int v)
{
    v + 10;
}

int func_2()
{
   return func_1(10);
}

compiled as:

gcc -fPIC -c libtest.c
gcc -shared -nostdlib -o libtest.so libtest.o

If we look at dynamic relocations for the shared object:

$ objdump -R libtest.so

libtest.so:    file format elf32-i386

DYNAMIC RELOCATION RECORDS
OFFSET   TYPE              VALUE
00002000 R_386_JUMP_SLOT   func_1

there is a R_386_JUMP_SLOT for the symbol func_1 so the call in the func_2 is resolved by PLT. I can't figure out the reason for this...If func_1 is declared as private (static) the relocation disappears and the call is resolved (by static linker) with a relative branch. Why passing from PLT is better than a relative jump?

MirkoBanchi
  • 2,173
  • 5
  • 35
  • 52
  • 1
    As @Miroslav wrote, that's to allow interposing. If you don't want to restrict use of `func1` to a single file (translation unit) but still don't want to export the symbol, have a look at http://stackoverflow.com/questions/6538501/linking-two-shared-libraries-with-some-of-the-same-symbols/6540059#6540059 – ninjalj Dec 18 '13 at 16:58

1 Answers1

1

With PLT you can override the call from func_2 to call to some other version of func_1 at runtime. For example via LD_PRELOAD. With static keyword, you just hardcoded your own private version of func_1. It's flexibility vs. small run-time overhead.

Miroslav Franc
  • 1,282
  • 12
  • 13