<div dir="ltr"><div dir="ltr">Hi,<div>Even if that would be a straightforward and good way to proceed, I cannot take the way you proposed with objdump as the files I'll be dealing with are .s only, no obj files at all.</div><div><br></div><div>So far, I've been able to intercept tha emission of the Label and Value while parsing; in particular, this has been done through the override of the <u><i>virtual void MCStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc())</i></u> method.</div><div>For what I've been able to read from the doxygen documentation, MCExpr should have an attribute of MCSymbol * type which points to the symbol it refers to (in this way it should be easy to match the "myvar" label). Unfortunately I can't find a way to extract the actual value from it (i.e. .long directives followed by the values, as shown in the asm file in the last reply).</div><div><br></div><div>Now, is it possible to extract the value the way I've just described? If yes, how?</div><div><br></div><div>I'm open to other suggestions and ways to achieve this, anyway (if any).</div><div><br></div><div>Thanks!</div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno gio 18 feb 2021 alle ore 05:09 Brian Cain <<a href="mailto:brian.cain@gmail.com">brian.cain@gmail.com</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">Well, one good way to get this is to let the assembler make the object file and dump the contents of the object file. I've illustrated w/objdump disassembly but a more robust way might be to use the location and read the data directly from the object file. See below for an example w/objdump.<div><br></div><div>But if you can't make the object file for some reason, I think ELFWriter might have what you need, or at least be a starting point. There is no arch-independent / object-file-independent way to determine where "myvar" will end up. So the way I think it makes sense to get this information is to wait until it's being emitted into the object file. But this is so late in assembly that it doesn't seem to make as much sense to modify the assembler so much as you might want to dissect its output. And there's lots of great tools and libraries for dissecting object files. </div><div><br></div><div>However, another approach might be to modify the assembler to look for labels matching "myvar" and set some kind of mode that lets you accumulate the subsequent ".word" directives.</div><div><br></div><div>Also: you described "myvar" as being a variable in a C program. So this is compiler-emitted assembly? Maybe it makes more sense to intercept this value in the compiler instead of in the assembler. It might not be very robust to try and scoop up .word's - this output could vary and still be legitimate compiler output.<br><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><font face="monospace">$ /opt/clang-latest/bin/llvm-mc -filetype=obj -triple=armv7 pietro.S -o pietro.o<br></font><div><font face="monospace">$ </font><span style="font-family:monospace">/opt/clang-latest/bin/llvm-objdump --triple=armv7 -d pietro.o</span></div><font face="monospace"><br>pietro.o: file format elf32-littlearm<br><br>Disassembly of section .text:<br><br>...<br><b>00000034 <myvar>:<br> 34: 00 00 00 00 .word 0x00000000<br> 38: 01 00 00 00 .word 0x00000001<br> 3c: 02 00 00 00 .word 0x00000002<br> 40: 03 00 00 00 .word 0x00000003</b><br></font><br></blockquote></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 17, 2021 at 10:48 AM Pietro D'Ettole <<a href="mailto:progettoiotpolimi2019@gmail.com" target="_blank">progettoiotpolimi2019@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hi,</div><div dir="ltr">I'll try to give you more context through an example.<br><div>What I need is to extract the value of a variable (aka a label) inside an assembly file; for example given the following assembly (compiled with armv7-a clang 10.0.0):</div><div><br></div><div>---------------------------------------------</div><div><div>main:</div><div> sub sp, sp, #12</div><div> mov r2, #0</div><div> str r2, [sp, #8]</div><div> str r0, [sp, #4]</div><div> str r1, [sp]</div><div> ldr r0, .LCPI0_0</div><div> ldr r1, [r0, #4]</div><div> ldr r0, [r0, #8]</div><div> mul r2, r1, r0</div><div> mov r0, r2</div><div> add sp, sp, #12</div><div> bx lr</div><div>.LCPI0_0:</div><div> .long myvar</div><div>myvar:</div><div> .long 0 @ 0x0</div><div> .long 1 @ 0x1</div><div> .long 2 @ 0x2</div><div> .long 3 @ 0x3</div></div><div>---------------------------------------------<br></div><div><br></div><div>As you can see there's a label, namely "myvar", which is a variable in the C program. What I'm trying to achieve is, while parsing with the Asm Parser, get the parsed value of "myvar" out of the asm file (i.e. get the values of 0x0, 0x1, 0x2, 0x3). Is this possible?</div><div><br></div><div>Thanks a lot!</div></div></div></div></div><div id="gmail-m_8689612410467660059gmail-m_-9023349424044236096DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br> <table style="border-top:1px solid rgb(211,212,222)">
<tbody><tr>
<td style="width:55px;padding-top:18px"><a href="http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail" target="_blank"><img src="https://ipmcdn.avast.com/images/icons/icon-envelope-tick-green-avg-v1.png" alt="" width="46" height="29" style="width: 46px; height: 29px;"></a></td>
<td style="width:470px;padding-top:17px;color:rgb(65,66,78);font-size:13px;font-family:Arial,Helvetica,sans-serif;line-height:18px">Mail priva di virus. <a href="http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail" style="color:rgb(68,83,234)" target="_blank">www.avg.com</a> </td>
</tr>
</tbody></table>
<a href="#m_8689612410467660059_m_-9023349424044236096_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1" height="1"></a></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno mar 9 feb 2021 alle ore 03:01 Brian Cain <<a href="mailto:brian.cain@gmail.com" target="_blank">brian.cain@gmail.com</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I don't quite know exactly, but I suppose one way would be to modify ELFWriter::writeSymbol() to emit something when a symbol appears that matches your criteria. I'm taking some liberty here assuming you can use an ELF object file. I imagine there's something similar for macho/coff.<div><br></div><div>Then again, that information is present in the object file too. You could use llvm-readelf or obj2yaml to extract what you want.<br></div><div><div><br></div><div>Maybe you could give a little more context about how you plan to use the info and the community could offer a better answer.<br><div><br></div><div><br></div><div><br></div><div> </div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Feb 8, 2021 at 5:03 PM Pietro D'Ettole <<a href="mailto:progettoiotpolimi2019@gmail.com" target="_blank">progettoiotpolimi2019@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Brian, thanks for your reply.<div><br></div><div>My goal is to be able to extract from an assembly file (i.e. source compiled to assembly, directly) the static global vars declared in the source. So far I haven't found any API in the llvm asm parser to serve my purpose. Do you know if/ how I can accomplish that?<div><br></div><div>Thank you.<br><div><br><br>Il giorno sabato 6 febbraio 2021, Brian Cain <<a href="mailto:brian.cain@gmail.com" target="_blank">brian.cain@gmail.com</a>> ha scritto:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div>You need to extract it from the source? Is it possible to use the resulting object file instead?</div><div dir="auto"><br></div><div dir="auto">Note that there's no way to get an 'address' but you can get a section offset. The section offset for both code and data are available in the object file and from the assembler as it writes the object file.</div><div dir="auto"><br></div><div dir="auto">The contents of the assembly file - instructions and directives - contribute to the resulting layout. The AsmParser can find tokens and build instructions but shouldn't know how it will get layed out.<br><br><div class="gmail_quote" dir="auto"><div dir="ltr" class="gmail_attr">On Sat, Feb 6, 2021, 3:16 AM Pietro D'Ettole via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi all,<div><br></div><div>I'm a little bit stuck in code reading right now. Maybe some of you can help me to understand fast if what I need is feasible or not.</div><div>I was trying to understand if currently asm parser in llvm allows to easily extract from an asm file variable names alongside with their values and their addresses.</div><div><br></div><div>Thanks for the help!</div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div></div>
</blockquote></div></div></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr">-Brian</div>
</blockquote></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr">-Brian</div></div>
</blockquote></div>