<br><br><div class="gmail_quote">On Thu, Sep 6, 2012 at 6:17 PM, Alexey Samsonov <span dir="ltr"><<a href="mailto:samsonov@google.com" target="_blank">samsonov@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi!<div><br></div><div><div class="im">On Mon, Jun 25, 2012 at 11:28 PM, Danil Malyshev <span dir="ltr"><<a href="mailto:dmalyshev@accesssoftek.com" target="_blank">dmalyshev@accesssoftek.com</a>></span> wrote:<br></div>
<div class="gmail_quote"><div class="im">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">




<u></u>





<div lang="RU" link="blue" vlink="blue">

<div>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy">Hi,<u></u><u></u></span></font></p>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy"><u></u> <u></u></span></font></p>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy">MCJIT uses only
getFileOffset() and only for the relocatable file, so this patch does not affect
to MCJIT.<u></u><u></u></span></font></p>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy">Nevertheless, it's looks
like the getFileOffset(), and the getAddress() contain errors and it's great
that someone want to fix them.<u></u><u></u></span></font></p>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy"><u></u> <u></u></span></font></p>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy">My vision of an ideal
situation:<u></u><u></u></span></font></p>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy">The getAddress() returns
the address of the symbol for those file types, where it makes sense (such as
an executable file). In other cases the result may be undefined.</span></font></p></div></div></blockquote><div><br></div></div><div>As I understand it, symbol table (at least the one in ELF) contains a value, which means different things for different symbols / object types, so it's worth</div>

<div>writing a non-trivial getAddress() function, that in case of ELF would return (a) symbol value for execs/shared libraries and (b) symbol value + section address</div><div>for relocatable files. IIUC some libObject clients (like llvm-nm or llvm-objdump) depend on that function.</div>

<div><br></div><div>I've fixed this function on ELF, and renamed it throughout codebase to getVirtualAddress() to better reflect its meaning. Could you please take a look at the patch?</div></div></div></blockquote><div>
<br></div><div>ping</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="gmail_quote"><div><br></div><div>Code review: <a href="http://codereview.appspot.com/6503082/" target="_blank">http://codereview.appspot.com/6503082/</a></div>
<div class="im">
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="RU" link="blue" vlink="blue"><div><p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy"><u></u><u></u></span></font></p>



<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy">The getFileOffset()
returns the offset of the symbol from the beginning of the file. For an
executable file its can be calculated as something like: symbol_address -
section_address + section_offset.<u></u><u></u></span></font></p><div>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy"><u></u> <u></u></span></font></p>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy"><u></u> <u></u></span></font></p>

<p class="MsoNormal"><font face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial">> How can I easily distinguish between relocatable
files and executables?<u></u><u></u></span></font></p>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy"><u></u> <u></u></span></font></p>

</div><p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy">Header->e_type</span></font><font color="navy" face="Arial"><span style="font-size:10.0pt;font-family:Arial;color:navy">?</span></font></p>

</div></div></blockquote><div><br></div></div><div>Ghm. Indeed :)</div><div><div class="h5"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="RU" link="blue" vlink="blue">
<div>
<p class="MsoNormal"><font color="navy" face="Arial"><span style="font-size:10.0pt;font-family:Arial;color:navy"><u></u><u></u></span></font></p>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy"><u></u> <u></u></span></font></p>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy"><u></u> <u></u></span></font></p>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy">Regards,<u></u><u></u></span></font></p>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy">Danil<u></u><u></u></span></font></p>

<p class="MsoNormal"><font color="navy" face="Arial"><span lang="EN-US" style="font-size:10.0pt;font-family:Arial;color:navy"><u></u> <u></u></span></font></p>

<div>

<div class="MsoNormal" align="center" style="text-align:center"><font size="3" face="Times New Roman"><span style="font-size:12.0pt">

<hr size="2" width="100%" align="center">

</span></font></div>

<p class="MsoNormal"><b><font face="Tahoma"><span style="font-size:10.0pt;font-family:Tahoma;font-weight:bold">From:</span></font></b><font face="Tahoma"><span style="font-size:10.0pt;font-family:Tahoma"> Alexey Samsonov
[mailto:<a href="mailto:samsonov@google.com" target="_blank">samsonov@google.com</a>] <br>
<b><span style="font-weight:bold">Sent:</span></b> Saturday, June 23, 2012
12:10 PM<br>
<b><span style="font-weight:bold">To:</span></b> Michael Spencer<br>
<b><span style="font-weight:bold">Cc:</span></b> <u></u><a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><u></u>;
Dmitry Vyukov; <a href="mailto:eli.bendersky@intel.com" target="_blank">eli.bendersky@intel.com</a>; Danil Malyshev; Owen Anderson<br>
<b><span style="font-weight:bold">Subject:</span></b> Re: PATCH: Fix
ELFObjectFile::getSymbolAddress which make llvm-nm work incorrectly on
executables</span></font><u></u><u></u></p>

</div><div><div>

<p class="MsoNormal"><font size="3" face="Times New Roman"><span style="font-size:12.0pt"><u></u> <u></u></span></font></p>

<div>

<p class="MsoNormal" style="margin-bottom:12.0pt"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial"><u></u> <u></u></span></font></p>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">On Fri, Jun 22, 2012 at 11:49 PM, Michael Spencer <<a href="mailto:bigcheesegs@gmail.com" target="_blank">bigcheesegs@gmail.com</a>>
wrote:<u></u><u></u></span></font></p>

<div>

<div>

<p class="MsoNormal" style="margin-bottom:12.0pt"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">On Fri, Jun 22, 2012 at 3:11 AM,
Alexey Samsonov <<a href="mailto:samsonov@google.com" target="_blank">samsonov@google.com</a>>
wrote:<br>
> Hi!<br>
><br>
> libObject seems to incorrectly implement<br>
> ELFObjectFile::getSymbolAddress. See this reproducer:<br>
> $ cat main.cc<br>
> int main() {<br>
>   return 0;<br>
> }<br>
> $ g++ main.cc -o main.out<br>
> $ nm main.out | grep main<br>
>                  U __libc_start_main@@GLIBC_2.2.5<br>
> 00000000004004b4 T main<br>
> $ llvm-nm main.out | grep main<br>
>          U __libc_start_main@@GLIBC_2.2.5<br>
> 00800884 T main<br>
><br>
> Let's try to get what's wrong:<br>
> 800884 - 4004b4 = 4003d0<br>
> $ objdump -h main.out | grep .text<br>
>  11 .text         000001c8  00000000004003d0
 00000000004003d0  000003d0<br>
>  2**4<br>
><br>
> So, the symbol address is incorrectly incremented by the section offset.
To<br>
> my understanding, attached patch should be applied to fix this. Please
check<br>
> if this is ok to apply.<br>
> getSymbolFileOffset in the same file seems to be fine, at least according
to<br>
> this quote from ELF specs:<br>
><br>
> Symbol table entries for different object file types have slightly
different<br>
> interpretations for the st_value member.<br>
> <...><br>
> * In relocatable files, st_value holds a section offset for a defined<br>
> symbol. That is, st_value is an offset from the beginning of the section<br>
> that st_shndx identifies.<br>
> * In executable and shared object files, st_value holds a virtual address.<br>
> [...]<br>
><br>
> --<br>
> Alexey Samsonov, MSK<br>
><u></u><u></u></span></font></p>

</div>

</div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">I agree that llvm-nm is incorrect here, but I'm not sure
this is the<br>
correct fix. The issue is that exactly what getSymbolAddress is<br>
supposed to return is undocumented. There was quite a bit of<br>
discussion about it in "[llvm-commits] MachOObjectFile fix
functions",<br>
but even after reading it I'm not 100% sure what it should do.  This<br>
patch also doesn't seem to handle the difference between a relocatable<br>
file and an executable.<u></u><u></u></span></font></p>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial"><u></u> <u></u></span></font></p>

</div>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">True. How can I easily distinguish between relocatable files
and executables?<u></u><u></u></span></font></p>

</div>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">Is it a bad idea to provide two different methods for
different types of files?<u></u><u></u></span></font></p>

</div>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial"> <u></u><u></u></span></font></p>

</div>

<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">I've CCed the people from the above thread. I would like to
decide on<br>
a well defined meaning for all of the Address/Offset functions and<br>
document that in the code before we change anything, as I believe the<br>
ELF MCJIT is relying on the current behavior.<u></u><u></u></span></font></p>

</blockquote>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial"><u></u> <u></u></span></font></p>

</div>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">Yes, I would really like the behavior to be documented, as
it's a bit confusing<u></u><u></u></span></font></p>

</div>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">that system nm and "objdump -t" provide different
results than "llvm-nm"<u></u><u></u></span></font></p>

</div>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">and "llvm-objdump -t". <u></u><u></u></span></font></p>

</div>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial"><u></u> <u></u></span></font></p>

</div>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">What I was actually trying to achieve is to<u></u><u></u></span></font></p>

</div>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">to symbolize a given instruction address - get the name of
function that contains<u></u><u></u></span></font></p>

</div>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">this instruction. I thought that the easy and
straightforward way to do this is to<u></u><u></u></span></font></p>

</div>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">use libLLVMObject, iterate over all symbols from symbol
table in executable, get symbol name and size<u></u><u></u></span></font></p>

</div>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">and do a simple check. Well, it doesn't work this way :)<u></u><u></u></span></font></p>

</div>

</div>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial"><u></u> <u></u></span></font></p>

</div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">--<u></u><u></u></span></font></p>

<div>

<p class="MsoNormal"><font face="Arial"><span style="font-size:10.0pt;font-family:Arial">Alexey Samsonov, MSK<u></u><u></u></span></font></p>

</div>

</div>

</div></div></div>

</div>


</blockquote></div></div></div><span class="HOEnZb"><font color="#888888"><br><br clear="all"><div><br></div>-- <br><div>Alexey Samsonov, MSK</div><br>
</font></span></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div>Alexey Samsonov, MSK</div><br>