1

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
Remy
  • 105
  • 6

2 Answers2

1

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'

mnistic
  • 10,866
  • 2
  • 19
  • 33
1

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.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362