<div dir="ltr">I'm trying to understand more about global constructors generated by clang. As far as I understand it, any compilation unit which requires static constructors will cause clang to generate LLVM IR to append to the array:<div><br></div><div>@llvm.global_ctors</div><div><br></div><div>This is an array of structs with 3 elements: a priority, a function pointer and an unknown (seemingly undocumented) member.</div><div><br></div><div>In most cases I only ever see one element in this array (per compilation unit) with name _GLOBAL__sub_I_Filename.cpp. </div><div><br></div><div>However in some more complex cases - which I've yet to pin down but I think relates to templates - I see multiple members. Those members always seem to look something like:</div><div><br></div><div>__cxx_global_var_init89<br></div><div>__cxx_global_var_init90</div><div>_GLOBAL__sub_I_Filename.cpp<br></div><div><br></div><div>Furthermore, the last function usually just wraps other functions with names prefixed by __cxx_global_var_init, e.g.</div><div><br></div><div><div>declare i8* @memset(i8*, i32, i64) #0</div><div>define internal void @__s3e__GLOBAL__sub_I_Filename.cpp() #0 {</div><div>  call void @__cxx_global_var_init(), !dbg !32933</div><div>  call void @__cxx_global_var_init1(), !dbg !32933</div><div>  call void @__cxx_global_var_init2(), !dbg !32933</div><div>  ret void</div><div>}</div></div><div><br></div><div>I'd just like to understand what the logic is behind the generation of these functions. It looks to me like there's implicit constructor priority however the priority member of the @llvm.global_ctors array is never used (well, it's always 65535).</div><div><br></div><div>Does anyone know the secrets behind this?</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div>