[Dwarf-Discuss] dwarf and local variables's offset

ranjith kumar ranjithproxy at gmail.com
Wed Apr 29 23:18:12 PDT 2009

       I compiled the folwwing program(1.c) on x86 machine.
       I am using gcc 4.1 version.
int main()
	int localvariable;
	printf("\n address of localvariable=%x",&localvariable);
I compiled it as
       gcc -g 1.c
I read the dwarf information with
    readelf -w a.out | vim -
The following is the part of that output which is related to "localvariable"
 <2><8d>: Abbrev Number: 6 (DW_TAG_variable)
	<8e>   DW_AT_name        : (indirect string, offset: 0xe): localvariable
	<92>   DW_AT_decl_file   : 1
	 <93>   DW_AT_decl_line   : 4
	<94>   DW_AT_type        : <0x4f>
    <98>   DW_AT_location    : 2 byte block: 91 70      (DW_OP_fbreg: -16)
What I think is that  the address of localvariable = (contents of ebp)-16
  since DW_OP_fbreg: -16  and ebp is the Base Pointer.

But when I   debug, the address of localvariable=0xbfa3ac10 and ebp=0xbfa3ac18
So the differnce between ebp and address of localvariable is -8 not -16.
Why so?
Thanks in advance.

I am writing the debugging output here.
[root at localhost dwarf]# gdb -q a.out
gdb: /usr/lib/libelf.so.1: no version information available (required
by /usr/lib/librpmdb-4.4.so)
	(gdb) b main
	Breakpoint 1 at 0x80483d5: file 1.c, line 5.
	(gdb) r
	Starting program: /root/myprograms/dwarf/a.out

	Breakpoint 1, main () at 1.c:5
	warning: Source file is more recent than executable.
	5		localvariable=12345;
	Missing separate debuginfos, use: debuginfo-install glibc.i686
	(gdb) info registers
	eax            0x1	1
	ecx            0xbfa3ac30	-1079792592
	edx            0xbfa3ac50	-1079792560
	ebx            0xd34ff4	13848564
	esp            0xbfa3abf0	0xbfa3abf0
	ebp            0xbfa3ac18	0xbfa3ac18
	esi            0x0	0
	edi            0x8048310	134513424
	eip            0x80483d5	0x80483d5 <main+17>
	eflags         0x286	[ PF SF IF ]
	cs             0x73	115
	ss             0x7b	123
	ds             0x7b	123
	es             0x7b	123
	fs             0x0	0
	gs             0x33	51
	(gdb) n
	6		printf("\n address of localvariable=%x",&localvariable);

	7	}
	0x00be65d6 in __libc_start_main () from /lib/libc.so.6
	Single stepping until exit from function __libc_start_main,
	which has no line number information.
	 address of localvariable=bfa3ac10
	 Program exited with code 043.

More information about the Dwarf-Discuss mailing list