[LLVMdev] [cfe-dev] weak_odr constant versus weak_odr global

John McCall rjmccall at apple.com
Tue Nov 8 00:12:54 PST 2011


On Nov 7, 2011, at 9:47 AM, Richard Smith wrote:
>> In cases where the C++ standard requires static initialization,
>> introducing a write violates the guarantees of the C++ standard for static
>> initialization.  Therefore, I'm not sure the whole "make the constant
>> writable" approach is actually viable.
> 
> There is another problem which afflicts all solutions presented thus far, for
> the other kind of weak global values in C++. For a static variable defined
> within an inline function, we can select the variable plus guard from a TU
> with dynamic initialization, and select the function definition from a TU with
> static initialization, with the result that the object doesn't get initialized
> at all.

> I have two new proposals for fixing this, which I believe actually work.
> 
> 1) [Requires ABI change] We emit dynamic initialization code for weak globals
> (even in TUs where static initialization is required to be performed), unless
> we can prove that every translation unit will use static initialization. We
> emit the global plus its guard variable as a single object so the linker can't
> separate them (this is the ABI change). If we can perform static
> initialization in any translation unit, then that TU emits a constant weak
> object (in .rodata if we want) containing the folded value and with the guard
> variable set to 1 (per Eli's proposal).

The ABI actually suggests doing exactly this, except using multiple
symbols linked with a COMDAT group.  Unfortunately, LLVM doesn't
support that COMDAT feature yet, but it could certainly be taught to.
This guarantees correctness as long as every translation unit emits the
code the same way, which is exactly what we'd get from an ABI change,
except without actually breaking ABI conformance.

Mach-O doesn't support anything like COMDAT, but the Darwin linker
apparently gives significantly stronger guarantees about which object
files it will take symbols from, as long as all objects have all of the
symbols.

John.



More information about the llvm-dev mailing list