HOW TO DEBUG shared library using GDB
------------------------------------------------------
[bhushan@Shared_Lib_Debug]$ gcc -fpic -shared -o foo.so foo.c
[bhushan@Shared_Lib_Debug]$ gcc -o main main.c ./foo.so -g
[bhushan@Shared_Lib_Debug]$ gdb main
GNU gdb Red Hat Linux (6.3.0.0-1.21rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".
(gdb) b foo
Function "foo" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (foo) pending.
(gdb) r
Starting program: /home/bhushan/RD/Shared_Lib_Debug/main
Reading symbols from shared object read from target memory...done.
Loaded system supplied DSO at 0x470000
Breakpoint 2 at 0xdb7493
Pending breakpoint "foo" resolved
Breakpoint 2, 0x00db7493 in foo () from ./foo.so
(gdb) s
Single stepping until exit from function foo,
which has no line number information.
main () at main.c:7
7 printf("inside main i = %d\n", i);
(gdb) s
inside main i = 4
8 return 0;
NOW if you build the shared libarary using -g option
[bhushan@Shared_Lib_Debug]$ gcc -fpic -shared -o foo.so foo.c -g
[bhushan@Shared_Lib_Debug]$ gcc -o main main.c ./foo.so -g
[bhushan@Shared_Lib_Debug]$ gdb main
GNU gdb Red Hat Linux (6.3.0.0-1.21rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".
(gdb) b foo
Function "foo" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (foo) pending.
(gdb) r
Starting program: /home/bhushan/RD/Shared_Lib_Debug/main
Reading symbols from shared object read from target memory...done.
Loaded system supplied DSO at 0x470000
Breakpoint 2 at 0x1c5493: file foo.c, line 5.
Pending breakpoint "foo" resolved
Breakpoint 2, foo () at foo.c:5
5 return 2*2;
(gdb) s
7 }
(gdb) s
main () at main.c:7
7 printf("inside main i = %d\n", i);
(gdb) s
inside main i = 4
8 return 0;
(gdb)
U can see the differences in bold lines.
Monday, August 24, 2009
HOW TO DEBUG shared library using GDB
Thursday, June 12, 2008
How to run a shared library on Linux
In my prevoius blog I have written how to run the shared libraries on Open-Solaris.
http://bhushanverma.blogspot.com/2008/06/how-to-run-shared-library-on-open.html
Shared object should have following entries to run:
1. +x permission that is by default is given by the static linker(program linker) when creating a shared object.
2. Entry point at which the program/shared library is starts to run.
3. Interpreter(Run time linker) that is used to run any shared library after loaded by kernel part exec().
Entry point at which the program/shared library is starts to run can be
given by passing -Wl,-e entry_point to the linker at command line:
To create .interp section by using GNU gcc, use the follwing line of code on linux:
const char my_interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
Where /lib/ld-linux.so.2 is the path of interpreter(Run time linker) in linux.
In open solaris we passed -Wl,-I,/usr/lib/ld.so.1 to the sun linker to create this section.
I think in gnu linker this option is available but do other things.
Demo on Linux machine:
-------------------------
$ cat func.c
const char my_interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
#include
void bar();
int
func()
{
printf("Hacking\n");
bar();
exit (0);
}
void
bar()
{
printf("Bye...\n");
}
$ gcc -fPIC -o func.so -shared -Wl,-e,func func.c
You can see that foo.so have .interp section and interp program header.
# readelf -l func.so
Elf file type is DYN (Shared object file)
Entry point 0x4dc
There are 7 program headers, starting at offset 52
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000034 0x00000034 0x00000034 0x000e0 0x000e0 R E 0x4
INTERP 0x0005a3 0x000005a3 0x000005a3 0x00013 0x00013 R 0x1
[Requesting program interpreter: /lib/ld-linux.so.2]
LOAD 0x000000 0x00000000 0x00000000 0x005bc 0x005bc R E 0x1000
LOAD 0x0005bc 0x000015bc 0x000015bc 0x00104 0x0010c RW 0x1000
DYNAMIC 0x0005d4 0x000015d4 0x000015d4 0x000c0 0x000c0 RW 0x4
NOTE 0x000114 0x00000114 0x00000114 0x00024 0x00024 R 0x4
GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4
Section to Segment mapping:
Segment Sections...
00
01 .interp
02 .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .interp .eh_frame
03 .ctors .dtors .jcr .data.rel.ro .dynamic .got .got.plt .bss
04 .dynamic
05 .note.gnu.build-id
06
You can cleary see, func.so have .interp section and
Now try to run func.so:
$ ./func.so
Hacking
Bye...
hua.. thats cool.
Happy hacking.
Sunday, February 24, 2008
Linux/Window boot problem
Some time ago,I was unable to boot my system.
I have two os on my system :windows and linux.
Something get currupted and only grub command line was appearing after rebooting my system.
I remembered that was a very panic for me.I asked to my friends and system admin but not able to find any solution.
At that time I had some other priority works so I formatted my hard disk and reinstall windows.
Currently I faced the same situation in my office working PC.
Now I founded the solution of this problem and able to reboot my windows machine.
To run the window machine ,I read grub.conf over the internt and fire the following command s on grub command line menu
grub> rootnoverify (hd0,0)
grub> chainloader +1
grub>boot
huh.. my windows starts to boot.
Wednesday, February 20, 2008
Open-Solaris Vs Linux
=========================
Configuring the dynamic linker search path
--------------------------------------------
File Name for editing paths:
Linux : /etc/ld.so.conf
Solaris: /var/ld/ld.config
If you have used Linux you might be used to add paths where librarys reside to /etc/ld.so.conf and run ldconfig afterwards.
Under Solaris there's a similar way to configure paths for the dynamic linker. Please note that it's only slightly better than setting LD_LIBRARY_PATH -- you can break things if you don't know exactly what you're doing.
Configuration files for the dynamic linker reside under /var/ld/ in solaris. Unlike under Linux you're not meant to edit those with your favourite editor(vi ,emacs etc). Simply running crle without arguments will give you something like this:
# crle
Default configuration file (/var/ld/ld.config) not found
Default Library Path (ELF): /lib:/usr/lib (system default)
Trusted Directories (ELF): /lib/secure:/usr/lib/secure (system default)
Now you can add a new path -- for example /usr/local/lib -- with crle -l 'path'. This might look like this:
# crle -l /lib:/usr/lib:/usr/local/lib
# crle
Configuration file [version 4]: /var/ld/ld.config
Default Library Path (ELF): /lib:/usr/lib:/usr/local/lib
Trusted Directories (ELF): /lib/secure:/usr/lib/secure (system default)
Command line:
crle -c /var/ld/ld.config -l /lib:/usr/lib:/usr/local/lib
Please note that you have to specify all directories, not just the one you'd like to add.otherwise system will not run since most system libraries (ie libc.so.1,libm.so.1 etc )resides /lib ,/usr/lib
OS x86 Solaris
===========
link-editor(Static Linker) : /usr/lib/
runtime linker/loader : /usr/lib/ld.so.1
configure : crle
config file: /var/ld/ld.config
cache file : /var/ld/ld.config
OS x86 Linux
===========
link-editor (Static Linker) : /usr/bin/ld
runtime linker/loader : /lib/ld-2.3.2.so (/lib/ld-linux.so.2)
configure : ldconfig
config file: /etc/ld.so.conf
cache file : /etc/ld.so.cache
----------------------------------Finish (Configuring ...)--------------------
Application Building on Linux and Solaris
============================
Using Make Files:
Both the Solaris OS and Linux provide the make utility, which can be used to build applications from its
source files. The make utility works recursively on a makefile that describes the relationship between
the target file and its dependencies.
On Linux, GNU make searches for the makefile in the following order:
1. ./GNUmakefile
2. ./makefile
3. ./Makefile
In addition, GNU make implicitly can extract a file from RCS/SCCS directories if necessary.
The Solaris make utility searches for the makefile in the following order:
1. Non-POSIX Mode
• ./makefile
• If ./SCCS/s.makefile is present, it tries to retrieve the most recent version of the makefile.
• ./Makefile
• If ./SCCS/s.Makefile is present, it tries to retrieve the most recent version of the makefile.
2. POSIX Mode
• ./makefile, ./Makefile
• s.makefile, SCCS/s.makefile
• s.Makefile, SCCS/s.Makefile
Both the versions of make have compatible command-line options.