<div dir="ltr">hi Devang and thanks for the tips, i finally managed to fit all the pieces together into something that seems to work.<br><br>It's probably not the best (or even correct!) way of doing it but here's a brief overview for reference:<br>

<br>An instruction in the LLVM IR gets converted into an SDNode in the DAG then later into a MachineInstr.<br>I'd already attached my own MDNodes to IR instructions I was interested in. I wanted to propagate that info to the final binary.<br>

<br>I Added a field to the SelectionDAGBuilder holding the current metadata which I update in SelectionDAGISel::SetDebugLoc()  for every IR instruction.<br>Next, in SelectionDAGBuilder::visit() i transfer the current instruction's metadata from the DAGBuilder to the instruction's SDNode.<br>

<br>In InstrEmitter::EmitNode() I copy the metadata from the SDNode to the MachineInstr. DwarfDebug::endModule() creates my user-defined DIE (after defining my own DW_TAG and DW_AT IDs in Dwarf.h) and adds it to the ModuleCU (for simplicity I'm adding my DIEs to the module's debug_info section)<br>

<br>I Added a few lines to Dwarf.cpp for emitting the correct name for my new DW_TAG and AT (useful when looking at commented assembly)<br><br>Finally, in AsmPrinter::processDebugLoc() I grab the metadata from the MachineInstr and pass it to the DwarfWriter to add it to its DwarfDebug member. When the assembly is emitted, the debug_info section contains my new dwarf DIE which I've managed to retrieve from the binary with a dwarf consumer.<br>

<br>cheers,<br>rw.<br><br><br><br><div class="gmail_quote">On 24 August 2010 02:46, Devang Patel <span dir="ltr"><<a href="mailto:devang.patel@gmail.com">devang.patel@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

Hi Roger,<br><br><div class="gmail_quote"><div class="im">On Mon, Aug 23, 2010 at 4:01 PM, Roger Wang <span dir="ltr"><<a href="mailto:innit42@gmail.com" target="_blank">innit42@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div class="gmail_quote">Dear all,<div><br></div><div>I'd like to find the memory location of certain instructions in a compiled/linked binary. During the IR phase, I tag instructions I'm interested in with LLVM'-2.7's new metadata (MDNodes with an identifiable ID). I'd now like to propagate that data to the assembly via a custom DWARF tag I attach to each X86 instruction created from a tagged IR instruction. This will then find its way at assembly time into the binary from where I retrieve it (by locating my custom tags with a DWARF consumer and dumping the addresses of the instruction they're attached to).</div>




<div><br></div><div>Does this sound reasonable?</div><div><br></div><div>I've completed the first part, attaching the MDNodes to IR instructions but I'm a bit overwhelmed by all the backend stuff.</div></div></blockquote>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_quote"><div>How can I identify which IR instruction an X86 instruction came from (with a view to attaching an identifying DW_TAG to it)?</div>


</div></blockquote><div><br></div></div><div>1) LLVM IR instructions are converted into  MachineInstr during instruction selection time. At this point you need to transfer your custom metadata to MachineInstr. See how DebugLoc is transfered (in CodeGen/SelectionDAG directory).</div>


<div><br></div><div>2) At AsmPrint time, while emitting your assembly instructions you have access to coresponding MachineInstr and any custom metadata attached with it.</div><div class="im"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div class="gmail_quote"><div><br></div><div>I've found the tag definitions in include/llvm/Support/Dwarf.h and added my own.</div><div>lib/CodeGen/AsmPrinter/DwarfDebug.cpp seems to be the only place that emits dwarf data into the assembly stream.</div>


</div></blockquote><div><br></div></div><div>See have DwarfDebug.cpp handles DebugLoc attached with each instruction (::beginScope() and ::endScope()).</div><div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div class="gmail_quote"><div> It also seems to create a DebugInfoFinder which accesses the IR instructions.</div></div></blockquote><div><br></div></div><div>This path will allow you to browse entire function and collect info which you can use later on. </div>


<div><br></div><div>-</div></div>Devang<font color="#888888"><br>
</font></blockquote></div><br></div>