Does every shared library get its own .got and .plt section?

81 Views Asked by At

I have the following source code (mathsource.c) which I compiled and linked as follows:

gcc -fpic -c mathsource.c

gcc -o lib/libmath.so.1.2.3 mathsource.o -shared -Wl,-soname,libmath.so.1

int add_nums(int a, int b)
{
    return a + b;
}

int sub_nums(int a, int b)
{
    return a - b;
}

int do_both(int a, int b)
{
    return add_nums(a, b) * sub_nums(a, b);
}

Upon running objdump -D lib/libmath.so.1.2.3 -M intel and looking for the definition of do_both I get the following output

0000000000001167 <do_both>:
    1167:   f3 0f 1e fa             endbr64 
    116b:   55                      push   rbp
    116c:   48 89 e5                mov    rbp,rsp
    116f:   53                      push   rbx
    1170:   48 83 ec 18             sub    rsp,0x18
    1174:   89 7d ec                mov    DWORD PTR [rbp-0x14],edi
    1177:   89 75 e8                mov    DWORD PTR [rbp-0x18],esi
    117a:   8b 55 e8                mov    edx,DWORD PTR [rbp-0x18]
    117d:   8b 45 ec                mov    eax,DWORD PTR [rbp-0x14]
    1180:   89 d6                   mov    esi,edx
    1182:   89 c7                   mov    edi,eax
    1184:   e8 e7 fe ff ff          call   1070 <add_nums@plt>          ; call to PLT
    1189:   89 c3                   mov    ebx,eax
    118b:   8b 55 e8                mov    edx,DWORD PTR [rbp-0x18]
    118e:   8b 45 ec                mov    eax,DWORD PTR [rbp-0x14]
    1191:   89 d6                   mov    esi,edx
    1193:   89 c7                   mov    edi,eax
    1195:   e8 c6 fe ff ff          call   1060 <sub_nums@plt>          ; call to PLT
    119a:   0f af c3                imul   eax,ebx
    119d:   48 8b 5d f8             mov    rbx,QWORD PTR [rbp-0x8]
    11a1:   c9                      leave  
    11a2:   c3                      ret    

Now my questions are:

  1. Considering that both functions used by do_both are defined in the same file, why does it call them via the PLT? why can't it call them with an offset based on the current position of the program counter?
  2. Does every shared library get its own .got and .plt section? Or when an executable is run the loader "joins" the .got and .plt section of every shared library used by the executable into one big .got and .plt section?
0

There are 0 best solutions below