Search code examples
windowsdebuggingsymbolsdebug-symbols

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.


Solution

  • 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.