<div dir="ltr">Ah, good.  Well, maybe.<div><br></div><div>The reason I asked, originally, was that if you can use thread-local globals rather than normal globals, then all references to globals will be through a dedicated register, and it's generally easy to set up that register in your entry point, since you say you only have the one entry point.</div><div><br></div><div>On x86 it's a bit more difficult, since it uses a dedicates segment register for thread-local variables... not just a regular register.  And it tends to want to check to see if the thread-local variables are set up in each routine, before using them.  And it still generates relocations.</div><div><br></div><div>So on x86 I'd consider using a different approach a dedicated register of your choice, e.g.:</div><div><br></div><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)"><div><font face="courier new, monospace"><span style="color:rgb(0,0,255)">struct</span> globs {</font></div><div>  <font face="courier new, monospace"><span style="color:rgb(0,0,255)">  char</span> flag;</font></div><div><font face="courier new, monospace">};</font></div><font face="courier new, monospace"><br></font><div><span style="color:rgb(0,0,255)"><font face="courier new, monospace">register</font></span></div><div><font face="courier new, monospace">globs *gp <span style="color:rgb(0,0,255)">asm</span>(<span style="color:rgb(163,21,21)">"rbx"</span>);   <span style="color:rgb(0,128,0)">//storing global variable in register explicitly</span></font></div><font face="courier new, monospace"><br></font><div><font face="courier new, monospace"><span style="color:rgb(0,0,255)">void</span> f(<span style="color:rgb(0,0,255)">char</span> *cp) {</font></div><div><font face="courier new, monospace">  gp->flag = *cp;</font></div><div><font face="courier new, monospace">}</font></div><font face="courier new, monospace"><br></font><div><font face="courier new, monospace"><span style="color:rgb(0,0,255)">int</span> entry_point(globs *globs_ptr, <span style="color:rgb(0,0,255)">char</span> *cp) {</font></div><div><font face="courier new, monospace">  gp = globs_ptr;  // set up access to our globals</font></div><div><font face="courier new, monospace">  f(cp);</font></div><div>  <font face="courier new, monospace"><span style="color:rgb(0,0,255)">  return</span> gp->flag;</font></div><div><font face="courier new, monospace">}</font></div></div></div><div><br></div><div>See <a href="https://godbolt.org/z/oOLJ1n">https://godbolt.org/z/oOLJ1n</a> for generated code.</div><div><br></div><div>Of course, this still means you have to modify the STL (and whatever other libraries you might want to call) to use this global register to access all its global variables... but it beats having to modify every routine to add an extra parameter.  But I haven't been able to think of any approach that doesn't have that problem, and with macros (#define cout gp->cout) it might not be that difficult.  And at least this way, no relocations are emitted.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Jul 7, 2019 at 3:14 PM Hayden Livingston <<a href="mailto:halivingston@gmail.com">halivingston@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">x86-64.<br>
<br>
On Mon, Jul 1, 2019 at 11:07 AM Jorg Brown <<a href="mailto:jorg.brown@gmail.com" target="_blank">jorg.brown@gmail.com</a>> wrote:<br>
><br>
> What architecture do you need this for?<br>
><br>
> Does the code in question ever use more than one thread?<br>
><br>
> On Mon, Jul 1, 2019 at 1:47 AM Hayden Livingston <<a href="mailto:halivingston@gmail.com" target="_blank">halivingston@gmail.com</a>> wrote:<br>
>><br>
>> It is wholly self-contained. It's code that has no references to<br>
>> anything beyond a set of pointers passed in as arguments to the<br>
>> function. This piece of code doesn't do any OS work at all. It is<br>
>> purely calling function pointers, doing math and allocating memory.<br>
>><br>
>> On Mon, Jul 1, 2019 at 12:57 AM Jorg Brown <<a href="mailto:jorg.brown@gmail.com" target="_blank">jorg.brown@gmail.com</a>> wrote:<br>
>> ><br>
>> > Qs for you:<br>
>> ><br>
>> > The code that is being loaded from disk... is it wholly self-contained, or is your executable potentially made up of several pieces that each need to be loaded from disk?<br>
>> ><br>
>> > What does it mean to use the STL but not have global variables?  std::cout is a global variable, so you can't even do Hello World without globals.<br>
>> ><br>
>> > = = =<br>
>> ><br>
>> > Architectures such as 68K and PowerPC and RISC-V have a dedicated register for accessing global variables, rather than the PC-relative globals used in other architectures.  This makes them inherently more amenable to what you describe, since you can put the "array of function pointers" into global space, as part of setting up global space in general, and then load the code from disk, and go.  There is no relocation needed since all access to globals is done via the global register, not relative to wherever the program was loaded.  Of course, access to something like libc might normally need post-loading relocation, but if you do what you're talking about and use an "array of function pointers" to get to libc, no relocation would be needed.<br>
>> ><br>
>> > For what it's worth, the original 68K-based Macintosh used a scheme quite similar to this.  The big difference for the Mac was that to get to the OS (the equivalent of libc), it didn't use an array of function pointers, per se; it used a certain range of illegal instructions, which generated exceptions when used, and the (highly optimized) exception handlers would recover from the exception by dispatching to an OS routine determined by the specific bits in the illegal instruction.<br>
>> ><br>
>> > On Sun, Jun 30, 2019 at 9:07 PM Hayden Livingston via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>> >><br>
>> >> I'm on a mission to generate code that can be loaded from disk without<br>
>> >> any modifications. This means no relocations can occur.<br>
>> >><br>
>> >> Trying to see if this can be done for C++ code that uses STL but has<br>
>> >> no global variables, and a single function, but of course Clang will<br>
>> >> generate more functions for STL code.<br>
>> >><br>
>> >> I want to provide an array of function pointers so that for all<br>
>> >> interactions STL needs to do with LIBC that I'm able to just provide<br>
>> >> it via indirect calls.<br>
>> >><br>
>> >> Has anyone had success with such a thing in LLVM?<br>
>> >> _______________________________________________<br>
>> >> LLVM Developers mailing list<br>
>> >> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
>> >> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>