user process instruction pointer symbol lookup

173 views
Skip to first unread message

TheDiveO

unread,
Apr 2, 2024, 5:34:48 AMApr 2
to golang-nuts
On Linux, given an arbitrary binary executable with symbol information in the executable, how can I lookup an instruction pointer address to get the corresponding symbol name?

The binary (and its process) isn't a Go binary, but any arbitrary executable. The stack unwinding has already been done, so I'm presented with a list of instruction pointer addresses (return addresses) which I need to convert to more useful symbol names.

I've seen the stdlib's debug/elf package, but I lack the ELF knowledge to press the elf package's knobs in the right order. Any examples of how to use debug/elf, and is it even the right package to use in this case?

Ian Lance Taylor

unread,
Apr 2, 2024, 2:01:11 PMApr 2
to TheDiveO, golang-nuts
debug/elf is the package to use. You'll want to call the Symbols
method and look through the Symbols for one whose Value is <= the PC
you want while Value+Size is > the PC you want.

If you are looking at runtime PC's from a stack trace, be aware that
on most systems these days programs are position-independent, so there
will be an offset between the addresses in the binary and the
addresses from the stack trace. I don't know offhand of a standard
way to figure out that offset.

Ian

TheDiveO

unread,
Apr 3, 2024, 7:25:14 AMApr 3
to golang-nuts
Thank you Ian!

Not sure yet if I found something useful related to the real process addresses/offsets, but at least the "bcc" sources have one "bcc_elf_get_text_scn_info()" that returns the address and offset of the ".text" section.

Massimiliano Giovagnoli

unread,
Jun 20, 2024, 8:29:40 PM (9 days ago) Jun 20
to golang-nuts
Thank you all. I was looking at the same goal, having collected stack traces from eBPF helper with IPs.

With debug/elf I've been able to resolve symbols for the use cases I tested, thank you Ian.
In example the right symbol is the one that satisfy ip >= sym.Value  && ip < (sym.Value+sym.Size) (where sym comes from elfBinary.Symbols()).
Reply all
Reply to author
Forward
0 new messages