<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style>
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">The __start_SECTIONNAME trick is ELF-specifc. Note that the section name has to be a valid C identifier for this to work: it can only contain letters, numbers (except at the start), and underscores. In particular, the section name can’t
have a period (.) in it. LLD ELF’s logic for this is at <a href="https://github.com/llvm/llvm-project/blob/a506f7f9105eec4baac296d21c922457d6f4b52a/lld/ELF/Writer.cpp#L1973">
https://github.com/llvm/llvm-project/blob/a506f7f9105eec4baac296d21c922457d6f4b52a/lld/ELF/Writer.cpp#L1973</a><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In COFF, this is usually implemented using grouped sections: if you have sections in your input files named .foo$A, .foo$B, etc., the linker places them in the output section .foo but orders them according to the input section name (e.g.
all .foo$B contents will end up after all .foo$A contents). You can therefore have your start of list symbol in .foo$A, your end of list symbol in .foo$Z, and your list contents in .foo$B (or any other letter that’s not A or Z), and iterate over the list that
way. More details on grouped sections can be found at <a href="https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#grouped-sections-object-only">
https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#grouped-sections-object-only</a><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’m not familiar with how to do this on Mach-O, but I believe there are dynamic linker APIs for traversing the contents of a particular section that can be used for this purpose.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:12.0pt;color:black">From: </span></b><span style="font-size:12.0pt;color:black">llvm-dev <llvm-dev-bounces@lists.llvm.org> on behalf of James Y Knight via llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Reply-To: </b>James Y Knight <jyknight@google.com><br>
<b>Date: </b>Thursday, January 9, 2020 at 11:30 AM<br>
<b>To: </b>Yafei Liu <yfliu@mobvoi.com><br>
<b>Cc: </b>llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Subject: </b>Re: [llvm-dev] Is there some sort of "@llvm.thread_ctors."?<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Thu, Jan 9, 2020 at 6:47 AM Yafei Liu via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<p class="MsoNormal">We know that in C++, the constructor of a static member will get called when the program starts up. I checked the generated IR code and found this is implemented by defining a __cxx_global_var_init() function and marked it as section ".text.startup"
and assign it to @llvm.global_ctors.<o:p></o:p></p>
</div>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">The last is the important part. But that's just an abstraction across the various global constructor mechanisms provided by the underlying platforms.<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">We also know that in C++, the constructor of a static thread_local member will
<b>not</b> get called when the program starts, but the first time this member is used, I also checked the IR code, this is implemented by calling it's constructor at the usage, and then call __cxa_thread_atexit to register it's destructor.<o:p></o:p></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">This behavior is not guaranteed by C++. It would also be acceptable according to the standard to construct the thread-locals eagerly upon thread startup, as you want to do. However, yes, clang does always lazy initialize them.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal">Now I want to add thread_local feature to my own language, and I want my thread_local member acts simlar like a static member, that is, when a new thread is created, the constructors of thread_local members will automatically get called,
instead get called when used?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I read the LLVM Language Reference Manual, but it doesn't talk much in Thread Local Storage Models. <o:p></o:p></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">No platform I know of provides such a mechanism that you (or llvm) could hook into. If you want this, you'll need to create your own thread-startup routine that calls your own thread-local initializers.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">The most obvious way to do this when there's no shared-libraries in the mix would be to emit the initializer-function pointers into a custom named section, and then iterate over that section in your thread startup routine. The linker automatically
creates __start_SECTIONNAME and __stop_SECTIONNAME symbols, so you can just iterate on the pointers between them. (At least this works in ELF, not sure if it's the same on COFF/MachO).<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">If you need to also support shared libraries, then finding all constructors across all libraries gets somewhat more complex...<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
</div>
</body>
</html>