I have a PIC shared library which also has a main function
#include <dtest2.h>
#include <stdio.h>
extern const char elf_interpreter[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
int dtestfunc1(int x,int y) {
int i=0;
int sum = 0;
for(i=0;i<=x;i++) {
sum+=y;
sum+=dtestfunc2(x,y);
}
return sum;
}
int main (int argc, char const* argv[])
{
printf("starting sharedlib main\n");
int val = dtestfunc1(4,5);
printf("val = %d\n",val);
_exit(0);
}
This library is linked to another shared library libdtest2 which has the implementation of dtestfunc2 called from dtestfunc1.
If i run gdb directly on dtestfunc1 using
gdb libdtest1.so
the symbols for libdtest2.so are not loaded by gdb. I cannot step into dtestfunc1 because of this and if i press s, the function just executes and gets out.
If i create a driver program which calls dlopen on the shared library, gdb loads the symbols properly after dlopen is executed and everything works fine.
- Why is gdb behaving differently in both these cases?
- How can i manually point gdb to the shared library if i am running gdb directly on the shared library?
Note: This is a toy example that mirrors my problem for a much larger shared library. All my binaries and libraries are compiled with the -ggdb3 flag.
Edit: My shared library is runnable. I added the proper interpreter path using the extern definition in the source code. I compiled it with gcc -ggdb3 -O0 -shared -Wl,-soname,libdtest1.so.1 -ldtest2 -L/usr/lib -Wl,-e,main -o libdtest1.so.1.0 dtest1.o.
I can run it and it executes perfectly.Running the shared library is not the issue here.
Because you didn't build
libdtest1.socorrectly for GDB to work with it.In particular, your
libdtest1.sois lackingDT_DEBUGentry in its dynamic section, which you can confirm like so:You should see nothing. In a correctly built runnable
libdtest1.so(which you can build using-pieflag), the output should look like this:The runtime loader updates
DT_DEBUGto point to itsr_debugstructure, which then allows GDB to find other loaded shared libraries. WithoutDT_DEBUG, GDB can't find them.Update:
Terminology: it's not a
DEBUGsection. It's aDT_DEBUGentry in the.dynamicsection.It is still missing because
-sharedoverrides-pie. Remove-sharedfrom the link line.You would also not need
-Wl,-e,main, nor would you need to specify the.interp-- GCC will do that for you.The correct link command:
(Order of sources and libraries on the link line matters, yours is wrong.)
Added bonus: your main will receive correct
argcandargv[], instead of bogus values you are getting now.