[llvm-dev] unusual use of getenv

Luke Drummond via llvm-dev llvm-dev at lists.llvm.org
Mon Nov 2 12:44:37 PST 2020


On Mon Nov 2, 2020 at 8:00 PM GMT, David Blaikie wrote:

> Oh, right, this stuff. I guess the non-windows solution might've been a
> volatile read, for instance? So maybe not so much that the general
> machinery isn't needed, but that perhaps MSVC does something interesting
> with a volatile read or whatever other solution might've been used.

I'm thinking in ELF terms, but perhaps something like this could help:

static volatile int doNotRemoveFlag;
int void __attribute__((weak)) doNotRemove() {
    return doNotRemoveFlag;
}

...

if (!doNotRemove()) {
    return;
}

AFAIU, the linker is not allowed to remove the call to `doNotRemove`
because it might be interposed at runtime. This probably isn't
guaranteed for statically linked binaries however (I investigated this
at one point but have since forgotten the rules for static libraries and
weak symbols). Additionally, for GNU style linkers we could also provide
a list of `KEEP()` symbols, which would mean this would survive LTO of
static binaries too. I don't know if this will be sufficient or doable
on other formats like PE-COFF or MachO

>
> Hmm, not sure why the whole file was added only when MSVC support was
> added
> - if it is a "static library object file selection" issue. Wouldn't that
> have turned up on other platforms before that moment?

Indeed. I'm still curious about this.

-- 
Codeplay Software Ltd.
Company registered in England and Wales, number: 04567874
Registered office: Regent House, 316 Beulah Hill, London, SE19 3HF


More information about the llvm-dev mailing list