gdb step into function in executable compiled with -z now

Maurizio Manfredini <[email protected]>
Newsgroups gmane.comp.gdb.devel
Message-ID <[email protected]>
Sorry, the linker option should obviously read "-z now", not "-r now"

On 10/02/2017 08:48 PM, Manfred wrote:
> Hi all,
> 
> I would like some explanation about the following behaviour, that I 
> can't understand from the documentation:
> 
> Using gcc and gdb, I am experiencing that with an executable linked with 
> -z now I can't step into functions defined in shared libraries.
> Just removing -r now from the linker options allows gdb to step in as 
> expected
> Here is some sample code (gcc 7.2.1 and gdb 8.0.1 on x86_64):
> 
> // lib/foo.h
> void foo(int);
> 
> // main.c
> #include <stdio.h>
> #include "lib/foo.h"
> 
> int main()
> {
>    const int arg = 13;
>    printf("calling foo(%d)...\n", arg);
>    foo(arg);
>    puts("back into main()");
> }
> 
> // lib/foo.c
> #include <stdio.h>
> #include "foo.h"
> 
> void foo(int val)
> {
>    printf("running foo(%d)\n", val);
> }
> 
> building the above with -z now:
> 
> $ make
> cc -g -Wall -fPIC   -c -o main.o main.c
> make -C ./lib
> make[1]: Entering directory './lib'
> cc -g -Wall -fPIC   -c -o foo.o foo.c
> gcc -shared -o libfoo.so foo.o -Wl,-h,libfoo.so.0 -Wl,-z,relro,-z,now 
> -Wl,--export-dynamic -Wl,--as-needed -fstack-protector -Wl,-rpath,`pwd` 
> -Wl,-Bdynamic
> ln -s libfoo.so libfoo.so.0
> make[1]: Leaving directory './lib'
> gcc -o foo main.o -pie -Wl,-z,relro -Wl,-z,now -lpthread 
> -Wl,-no-undefined -Wl,--export-dynamic -Wl,--as-needed -fstack-protector 
> -Wl,-rpath,`pwd`/lib -L`pwd`/lib -Wl,-Bdynamic -lfoo
> 
> and gdb:
> 
> $ gdb foo
> GNU gdb (GDB) Fedora 8.0.1-26.fc26
> Copyright (C) 2017 Free Software Foundation, Inc.
> .....
> Reading symbols from foo...done.
> (gdb) b main
> Breakpoint 1 at 0x8c2: file main.c, line 7.
> (gdb) run
> Starting program: ./foo
> [Thread debugging using libthread_db enabled]
> Using host libthread_db library "/lib64/libthread_db.so.1".
> 
> Breakpoint 1, main () at main.c:7
> 7      const int arg = 13;
> (gdb) next
> 8      printf("calling foo(%d)...\n", arg);
> (gdb)
> calling foo(13)...
> 9      foo(arg);
> (gdb) step                            <== this doesn't work
> running foo(13)
> 10      puts("back into main()");
> (gdb) cont
> Continuing.
> back into main()
> [Inferior 1 (process 7113) exited normally]
> (gdb)
> 
> linking the executable without -z now:
> 
> $ make
> cc -g -Wall -fPIC   -c -o main.o main.c
> make -C ./lib
> make[1]: Entering directory './lib'
> cc -g -Wall -fPIC   -c -o foo.o foo.c
> gcc -shared -o libfoo.so foo.o -Wl,-h,libfoo.so.0 -Wl,-z,relro,-z,now 
> -Wl,--export-dynamic -Wl,--as-needed -fstack-protector -Wl,-rpath,`pwd` 
> -Wl,-Bdynamic
> ln -s libfoo.so libfoo.so.0
> make[1]: Leaving directory './lib'
> *** -r now *removed* in the following ***
> gcc -o foo main.o -pie -Wl,-z,relro  -lpthread -Wl,-no-undefined 
> -Wl,--export-dynamic -Wl,--as-needed -fstack-protector 
> -Wl,-rpath,`pwd`/lib -L`pwd`/lib -Wl,-Bdynamic -lfoo
> 
> and gdb:
> 
> $ gdb foo
> GNU gdb (GDB) Fedora 8.0.1-26.fc26
> ......
> Reading symbols from foo...done.
> (gdb) b main
> Breakpoint 1 at 0x8e2: file main.c, line 7.
> (gdb) run
> Starting program: ./foo
> [Thread debugging using libthread_db enabled]
> Using host libthread_db library "/lib64/libthread_db.so.1".
> 
> Breakpoint 1, main () at main.c:7
> 7      const int arg = 13;
> (gdb) next
> 8      printf("calling foo(%d)...\n", arg);
> (gdb)
> calling foo(13)...
> 9      foo(arg);
> (gdb) step                            <== this does work now
> foo (val=13) at foo.c:7
> 7      printf("running foo(%d)\n", val);
> (gdb) cont
> Continuing.
> running foo(13)
> back into main()
> [Inferior 1 (process 7200) exited normally]
> (gdb)
> 
> stepi does step into, but it is less friendly.
> 
> Thanks in advance,
> Manfred
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.