Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a function address to a symbol

Let's say I have a program like this

// print-addresses.cpp
#include <stdio.h>

void foo() { }

void bar() { }

void moo() { }

int main(int argc, const char** argv) {
  printf("%p\n", foo);
  printf("%p\n", bar);
  printf("%p\n", moo);
  return 0;
}

It prints some numbers like

013510F0
013510A0
01351109

How do I convert those numbers back into the correct symbols? Effectively I'd like to be able to do this

print-addresses > address.txt
addresses-to-symbols < address.txt

And have it print

foo
bar
moo

I know this has something to do with the Debug Interface Access SDK but it's not entirely clear to me how I go from an address to a symbol.

like image 310
gman Avatar asked Dec 27 '12 06:12

gman


2 Answers

This seems like exactly what you're looking for: Retrieving Symbol Information by Address. This uses DbgHelp.dll and relies on calling SymFromAddr. You have to do that (I think) from within the running application, or by reading in a minidump file.

You can also use the DIA, but the calling sequence is a bit more complicated. Call IDiaDataSource::loadDataForExe and IDiaDataSource::openSession to get an IDiaSession, then IDiaSession::getSymbolsByAddr to get IDiaEnumSymbolsByAddr. Then, IDiaEnumSymbolsByAddr::symbolByAddr will let you look up a symbol by address. There is also a way (shown in the example at the last link) to enumerate all symbols.

EDIT: This DIA sample application might be a good starting point for using DIA: http://msdn.microsoft.com/en-us/library/hd8h6f46%28v=vs.71%29.aspx . Particularly check out the parts using IDiaEnumSymbolsByAddr.

You could also parse the output of dumpbin, probably with /SYMBOLS or /DISASM option.

like image 194
Alex I Avatar answered Sep 19 '22 11:09

Alex I


if you are in linux, you could try addr2line

addr2line addr -e execuablebin -f
like image 30
slggamer Avatar answered Sep 20 '22 11:09

slggamer