<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 19, 2015 at 8:51 PM, Kaylor, Andrew <span dir="ltr"><<a href="mailto:andrew.kaylor@intel.com" target="_blank">andrew.kaylor@intel.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Hi Keno,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">I _think_ that the GOT support we currently have can be made to work if the memory manager provides the necessary help (more on that below), but I will readily
 admit that it is implemented in a fairly non-standard way that is likely to seem completely wrong on first inspection (and probably still seems at least slightly wrong on second inspection).  It may also have inherent limitations that can’t be overcome without
 a redesign, but if so I don’t know what those limitations might be.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">It may be helpful to refer to the comments in my original GOT implementation patch (<a href="http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20130812/184265.html" target="_blank">http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20130812/184265.html</a>)
 when trying to decipher the intent of the existing code as unfortunately I seem to have said quite a bit more there than I did in the actual code comments.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">I’m pretty sure that the “multiple GOT” patch was intended to support the case where additional modules are loaded after finalizeLoad() has been called.  It
 looks like we were at some point trying to use a single GOT for all modules, but once it had been “finalized” another GOT had to be created for subsequent loads.  It’s been a while since I looked at this code, but I believe that we defer calculating the offsets
 for the GOT until a “finalize” is performed.  This is because the memory for loaded sections may be remapped before that time to handle remote (or out-of-process) execution.  It appears that we are also deferring allocation of the GOT section memory until
 this time.</span></p></div></div></blockquote><div><br></div><div>We call finalizeLoad for every object I believe, so we essentially end up with one GOT per object file anyway. We're deferring filling (though not allocating) the GOT until we call resolveRelocations. </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple"><div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">With regard to the 2 GB+ offset problem, we’re dependent on the memory manager in that regard.  Even with a single object being loaded there is no guarantee
 that the memory allocated for the GOT section will be within 2 GB of the memory allocated for other sections unless the memory manager does something to make it so.  An interface was added sometime in the past year (I think) that optionally pre-calculates
 the amount of memory that will be needed for an object load so that the memory manager can allocate all of this memory as a single block.  I’m not sure this interface properly accounts for the possibility of GOT sections and I don’t know how it works with
 multiple modules.</span></p></div></div></blockquote><div><br></div><div>While this is true, it's actually not the case I'm worried about. The case I'm worried about is where we load enough object files to exhaust 2GB worth of objects (this doesn't even have to be 2GB worth of code, for example I hit this with msan). The current interface basically forces all code to fit within two GB, which is precisely what the GOT is supposed to avoid.</div><div><br></div><div>Just to be very explicit, the case I'm concerned about is</div><div><br></div><div>- Allocate Object file 1 with GOTPCREL to `foo`</div><div>- [ Allocate 2GB worth of other data ]</div><div>- Allocate Object file 2 with GOTPCREL to `foo`</div><div><br></div><div>Object file 2 will reuse Object file 1's GOT (though we'll still allocate space in object file 2's GOT, so it's not like we're doing this to save memory)</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">The default memory manager attempts to use system address hints to allocate sections in the same region of the address space, but not all OSs support the flags
 we’d like to use and the address requests are never guaranteed to be respected.  FWIW, Address Sanitizer is very good at exposing issues of this sort.</span></p></div></blockquote><div><br></div><div>Yes, I agree this is a concern, though it seems solvable to always allocate one ObjectFile within 2GB, while it doesn't necessarily seem right to impose this to impose the restriction that all code ever loaded has to fit within 2GB.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple"><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">I should also mention that there is some variation in how GOT-related issues are handled from architecture to architecture within RuntimeDyldELF.  When I implemented
 the GOT support, I intended for it to be capable of supporting any architecture, but there was some support for GOT-related relocations for non-x86 platforms that pre-dated my GOT implementation and I suspect those will continue to be used as long as they
 are working correctly.  For instance, several architectures extended the allocated size of code sections and use the extra space at the end of the section to create stubs for PC-relative function calls.</span></p></div></blockquote><div><br></div><div>Yes, I've seen this code. </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple"><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Let me know if there’s anything more I can do to help you get things working.</span><span style="color:rgb(31,73,125);font-family:Calibri,sans-serif;font-size:11pt"> </span></p></div></blockquote><div><br></div><div>Thanks for replying. I have a half-way functioning prototype that makes GOTs local to each object file again and also deduplicates GOTEntries where possible. I'll finish it up and post it here as soon as I can.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple"><div><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">-Andy<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> Keno Fischer [mailto:<a href="mailto:kfischer@college.harvard.edu" target="_blank">kfischer@college.harvard.edu</a>]
<br>
<b>Sent:</b> Sunday, January 18, 2015 5:38 AM<br>
<b>To:</b> LLVM Developers Mailing List; Lang Hames; Kaylor, Andrew; Thirumurthi, Ashok<br>
<b>Subject:</b> [MCJIT] Multiple GOT handling in RuntimeDyldELF<u></u><u></u></span></p><div><div class="h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">Hello everyone,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal">As part of my quest to add TLS relocation support to MCJIT, I've been taking a closer look at the GOT implementation in RuntimeDyldELF and I believe that is not valid as currently implemented. In particular, I am wondering about the multiple
 GOT handling support introduced in r192020. If I understand correctly this can make code reuse the GOT table entry in a different object file. This doesn't seem correct to me as there is no guarantee that the loaded object files are allocated within 2GB of
 each other in memory. What was the intended use case of this feature? Additionally, it seems that currently every access through the GOT get it's own entry, when identical relocations could be combined into one entry. The GOTEntries array is also never cleared,
 causing memory and performance problems when loading multiple object files (this is a bug and easily fixed, but makes me think this feature isn't particularly well tested). I'm planning to redesign the GOT mechanism, but I would like to understand the use
 case intended in r192020 first, to make sure I don't design myself into a corner.<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Thanks,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Keno<u></u><u></u></p>
</div>
</div>
</div></div></div>
</div>

</blockquote></div><br></div></div>