__builtin_return_address dissagrees with manual on x86
Mike Perry
mikepery@fscked.org
Fri Apr 21 03:42:00 GMT 2000
__builtin_return_address doesn't work how the info pages say it should on
x86's.
`__builtin_return_address (LEVEL)'
This function returns the return address of the current function,
or of one of its callers. The LEVEL argument is number of frames
to scan up the call stack. A value of `0' yields the return
address of the current function, a value of `1' yields the return
address of the caller of the current function, and so forth.
The LEVEL argument must be a constant integer.
On some machines it may be impossible to determine the return
address of any function other than the current one; in such cases,
or when the top of the stack has been reached, this function will
return `0'. ~~~~
~~~~~~~~~
On my architecture (x86 GNU/Redhat Linux 6.1) calls to
__builtin_return_address with a level greater than the stack depth will cause
a segmentation fault. Also, they will cause a segmentation fault for levels >
0 if -fomit-frame-pointer is used. It would be nice if the manual agreed with
the implementation, and even nicer if the function actually returned NULL, as
sometimes programs that are being debugged are linked against libraries that
omit the frame pointer. You will notice that this function works as advertised
for gcc on Sparc.
Outputs for the attached .c file follow:
On a FreeBSD (x86) box
FreeBSD 4.0-STABLE (BEAST) #5: Tue Apr 18 23:14:45 CDT 2000
bash-2.03$ gcc -v
Using builtin specs.
gcc version 2.95.2 19991024 (release)
bash-2.03$ gcc return_address.c -o return_address
bash-2.03$ ./return_address
Ret: 0x804b01d
Ret: 0x804b077
Ret: 0x804b087
Ret: 0x804b097
Ret: 0x80483b3
Ret: 0x1
Segmentation fault (core dumped)
bash-2.03$ gcc -fomit-frame-pointer return_address.c -o return_address
bash-2.03$ ./return_address
Ret: 0x804b019
Segmentation fault (core dumped)
On my Linux box:
Linux fscked 2.2.13 #2 Sun Nov 14 23:40:26 CST 1999 i586 unknown
mikepery:/home/mikepery/programming/njamd/tests> gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
mikepery:/home/mikepery/programming/njamd/tests> gcc return_address.c -o
mikepery:/home/mikepery/programming/njamd/tests> ./return_address
Ret: 0x804afed
Ret: 0x804b044
Ret: 0x804b050
Ret: 0x804b05c
Ret: 0x400301eb
Ret: 0x8048361
Segmentation fault (core dumped)
mikepery:/home/mikepery/programming/njamd/tests> gcc -fomit-frame-pointer
return_address.c -o return_address
mikepery:/home/mikepery/programming/njamd/tests> ./return_address
Ret: 0x804afea
Ret: 0x8b0c2444
Segmentation fault (core dumped)
root:/root> uname -a
OpenBSD peru 2.6 PERU#16 sparc
root:/root> gcc -v
Reading specs from /usr/lib/gcc-lib/sparc-unknown-openbsd2.6/2.95.1/specs
gcc version 2.95.1 19990816 (release)
root:/root> gcc return_address.c -o return_address
./re root:/root> ./return_address
Ret: 0x7f98
Ret: 0x8010
Ret: 0x8024
Ret: 0x8040
Ret: 0x2098
T = null
root:/root> gcc -fomit-frame-pointer return_address.c -o return_address
./re root:/root> ./return_address
Ret: 0x7f98
Ret: 0x8010
Ret: 0x8024
Ret: 0x8040
Ret: 0x2098
T = null
I get simmilar results on Solaris:
mperry1:/homes/m/mp/mpe/mperry1> gcc -v
Reading specs from
/usr/local/encap/gcc-2.8.1/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/specs
gcc version 2.8.1
mperry1:/homes/m/mp/mpe/mperry1> uname -a
SunOS glsn22.ews.uiuc.edu 5.6 Generic_105181-19 sun4u sparc SUNW,Ultra-60
Please consider making the x86 results match the Sparc results, as this
feature is used by more than one malloc debugger (including my own), and
probably numerous other forms of debuggers.
--
Mike Perry
http://got.fscked.org ?
More information about the Gcc-bugs
mailing list