How to extract the starting address of each basic block with llvm?

Dear all,

I use the clang frontend to produce the control flow graph of a C program through
this command:
clang -Xclang -analyze -Xclang -analyzer-checker=debug.DumpCFG test.c

The produced information contains all the basic blocks identified by labels,
e.g. B1, B2, etc. along with their predecessors and successors.

Is there a way to get the starting address of each of these basic block?

I would like to map the basic block labels produced by CFG with the actual

corresponding basic block starting addresses.

Could I use llvm to do this? From what I understand this issued is unrelevant with

IR. Maybe I could get something from the machine code (MC). Perhaps
I have to write a pass for llvm backend. Maybe I could get this information from
BranchFolder and IfConverter machine function passes:
http://llvm.org/docs/WritingAnLLVMBackend.html#branch-folding-and-if-conversion

but I am not sure.

I am new to llvm so I need some help,

Thank you,

Check out blockaddress(@function, %block) as documented in:
http://llvm.org/docs/LangRef.html#addresses-of-basic-blocks

The documentation says it only has defined behavior when used with
indirectbr, but I have been able to store the block address in a
global variable that survives lowering and is the correct address.

      GlobalVariable *GV = new GlobalVariable(M, bbPtr, /*isConstant=*/true,
                                   GlobalValue::ExternalLinkage,
                                   BlockAddress::get(bb), name);

Beware, the IR BB may be optimized away when lowering to assembly. You
can detect this case when blockaddress() returns 1.

Cheers,
Jevin

Thank you very much Jevin for the suggestion!

But how I can use blockaddress function? Do I have to write a pass in order to use it?
As I told you I am new in llvm. Could you provide me a guide or an example?

Thank you very much,
Thanasis

Well, a basic block doesn't have a concrete address until it is linked
by the linker. I think that you will have to write a pass that stores
the blockaddress of every BB to global variables like I mentioned in
my last mail. The perhaps you could use objdump + a script to dump the
addresses?

Cheers,
Jevin