<div dir="ltr">Hi Alex,<div><br></div><div>I think adding a method to RuntimeDyld to run the constructors (or retrieve their addresses) would make sense, and should work on Linux and MachO. I can't speak to COFF though.</div><div><br></div><div>This would be an improvement over the current utility code, as it would support static constructors in pre-compiled object files too.</div><div><br></div><div>Cheers,</div><div>Lang.</div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Jun 26, 2018 at 2:36 AM Alex Denisov <<a href="mailto:1101.debian@gmail.com">1101.debian@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello everyone,<br>
<br>
The question is related to the recent "runStaticConstructorsDestructors() causes<br>
crash on exit" thread [1].<br>
<br>
I was curious if the constructors/destructors thing can be done directly via<br>
inspecting object files. Below is what I've found so far.<br>
<br>
On macOS, I was able to get symbols for constructors by looking at relocations<br>
at _mod_init_func. Then, I used the symbol names to retrieve addresses of actual<br>
constructors (after relocations were applied) and calling them.<br>
It worked well in this very case.<br>
<br>
On Linux, same relocations are located inside of .rela.ctors or .rela.init_array.<br>
I tried the same approach as with MachO, but not all symbols have names.<br>
I.e., there are just references to some code (e.g. .text.startup+0x280).<br>
So I cannot retrieve the address based on the symbol name.<br>
<br>
I assume there can be an API within RuntimeDyld that can give the address of<br>
a RelocationRef corresponding to a particular constructor relocation.<br>
In that case, it will work both for Linux and macOS. Probably on Windows as well.<br>
<br>
The question is: in what case this approach will not work?<br>
Note: I am only interested in a non-lazy JIT of a native code.<br>
<br>
Thank you,<br>
Alex.<br>
<br>
[1] <a href="http://lists.llvm.org/pipermail/llvm-dev/2018-June/124031.html" rel="noreferrer" target="_blank">http://lists.llvm.org/pipermail/llvm-dev/2018-June/124031.html</a><br>
</blockquote></div>