Reputation: 105
I am looking for a command or library function that returns the address of a function, the opposite of addr2line.
Currently, I use the following command:
readelf -s ./a.out|grep "\<main\>"|uniq
> 451: 000000000001967f 148 FUNC GLOBAL DEFAULT 14 main
Upvotes: 1
Views: 263
Reputation: 213955
You could use libelf.
Alternatively, this question and answer shows how to do what nm
does.
You could easily modify the code there to go in reverse: iterate over all symbols until you find the right symbol name, then return the address of that symbol.
If you need to perform lookups over multiple symbol names, you could of course iterate over all symbols once, and build a name
-> address
map, so subsequent lookups are fast.
Upvotes: 1
Reputation: 11020
You could also use gdb for this, info address
prints the address of the given symbol, so something like this should work: gdb ./a.out -ex 'info address main'
Upvotes: 1