Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve function name from function address using link register (like backtrace_symbol) in linux

Tags:

c

linux

arm

I want to write c program (like backtrace) I am getting the address of functions but I don't know how to convert those address to symbols (function name ). Please help me

like image 565
Bhavith C Acharya Avatar asked Jan 27 '26 15:01

Bhavith C Acharya


1 Answers

The first answer is that the symbol handling is an internal ABI hidden away. Some OS even perform this magic in kernel space. But you obviously want ARM + linux.

First information needed is to map addresses back to their origin. This mapping you can retrieve from here: /proc/self/stat

Next part is more tricky, reversing those offsets from those files into symbols. For that you will need to parse the ELF files. If you do not want to parse the binary data, you can cheat and use objdump and parse the ASCII formatted data instead

http://man7.org/linux/man-pages/man5/elf.5.html
objdump -t -T -r -R /lib/x86_64-linux-gnu/libnss_files-2.19.so

If you want even more details information than this, you will need to parse the section that contains the debug information if present - and it might be moved to a separate file to allow apt to have those nice -dbg packages, but that is probably way to much work, and easier to hack gdb instead or extract code from projects like valgrind.

PS: If your use-case is to perform debug/diagnostics when things go wrong, I will recommend to use valgrind

like image 110
Stian Skjelstad Avatar answered Jan 30 '26 08:01

Stian Skjelstad