<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:st1="urn:schemas-microsoft-com:office:smarttags" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered medium)">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]--><o:SmartTagType
 namespaceuri="urn:schemas-microsoft-com:office:smarttags" name="PersonName"/>
<!--[if !mso]>
<style>
st1\:*{behavior:url(#default#ieooui) }
</style>
<![endif]-->
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman";}
a:link, span.MsoHyperlink
        {color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {color:blue;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:Arial;
        color:navy;}
@page Section1
        {size:595.3pt 841.9pt;
        margin:2.0cm 42.5pt 2.0cm 3.0cm;}
div.Section1
        {page:Section1;}
-->
</style>

</head>

<body lang=RU link=blue vlink=blue>

<div class=Section1>

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

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

<p class=MsoNormal><font size=2 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.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 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.<o:p></o:p></span></font></p>

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

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

<p class=MsoNormal><font size=2 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.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 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.<o:p></o:p></span></font></p>

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

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

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

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

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

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

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

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

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

<p class=MsoNormal><font size=2 color=navy face=Arial><span lang=EN-US
style='font-size:10.0pt;font-family:Arial;color:navy'><o:p> </o:p></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 tabindex=-1>

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

<p class=MsoNormal><b><font size=2 face=Tahoma><span style='font-size:10.0pt;
font-family:Tahoma;font-weight:bold'>From:</span></font></b><font size=2
face=Tahoma><span style='font-size:10.0pt;font-family:Tahoma'> Alexey Samsonov
[mailto:samsonov@google.com] <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> <st1:PersonName w:st="on">llvm-commits@cs.uiuc.edu</st1:PersonName>;
Dmitry Vyukov; eli.bendersky@intel.com; 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><o:p></o:p></p>

</div>

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

<div>

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

<div>

<p class=MsoNormal><font size=2 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:<o:p></o:p></span></font></p>

<div>

<div>

<p class=MsoNormal style='margin-bottom:12.0pt'><font size=2 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">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>
><o:p></o:p></span></font></p>

</div>

</div>

<p class=MsoNormal><font size=2 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.<o:p></o:p></span></font></p>

<div>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>

</div>

<div>

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

</div>

<div>

<p class=MsoNormal><font size=2 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?<o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> <o:p></o:p></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 size=2 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.<o:p></o:p></span></font></p>

</blockquote>

<div>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=2 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<o:p></o:p></span></font></p>

</div>

<div>

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

</div>

<div>

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

</div>

<div>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>

</div>

<div>

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

</div>

<div>

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

</div>

<div>

<p class=MsoNormal><font size=2 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<o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=2 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<o:p></o:p></span></font></p>

</div>

<div>

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

</div>

</div>

<div>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>

</div>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>--<o:p></o:p></span></font></p>

<div>

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

</div>

</div>

</div>

</body>

</html>