Stream: general

Topic: static appears twice in binary


Jethro (Feb 12 2019 at 09:18, on Zulip):

I'm compiling the std unit tests for my target and a particular static ends up twice in the resulting binary, which is... bad

Jethro (Feb 12 2019 at 09:19, on Zulip):

from gdb:

File src/libstd/sys/sgx/abi/tls.rs:
static struct SyncBitset std::sys::sgx::abi::tls::TLS_KEY_IN_USE::h0f340495a814d6d4;
static struct SyncBitset std::sys::sgx::abi::tls::TLS_KEY_IN_USE::hddca68d75da9bb5c;
Jethro (Feb 12 2019 at 09:19, on Zulip):

how does this happen?

rkruppe (Feb 12 2019 at 09:25, on Zulip):

IIRC libstd unit tests are the one case where you might end up with two versions of libstd, the normal one and the cfg(test) one, linked together

RalfJ (Feb 12 2019 at 09:26, on Zulip):

std unit tests are not actually normal unit tests

RalfJ (Feb 12 2019 at 09:26, on Zulip):

they are a separate crate

RalfJ (Feb 12 2019 at 09:26, on Zulip):

with its root in libstd/tests/lib.rs

RalfJ (Feb 12 2019 at 09:27, on Zulip):

oh, hm, that's true for liballoc and libcore, anyway

Jethro (Feb 12 2019 at 09:27, on Zulip):

that file doesn't exist...

RalfJ (Feb 12 2019 at 09:27, on Zulip):

seems like libstd also has tests "inside" the lib?

RalfJ (Feb 12 2019 at 09:27, on Zulip):

weird, I thought these libs would all be organized the same way.

RalfJ (Feb 12 2019 at 09:28, on Zulip):

so yeah seems like there is some really crazy magic at work here, and it doesn't seem unlikely that that can duplicate statics

RalfJ (Feb 12 2019 at 09:28, on Zulip):
// During testing, this crate is not actually the "real" std library, but rather
// it links to the real std library, which was compiled from this same source
// code. So any lang items std defines are conditionally excluded (or else they
// would generate duplicate lang item errors), and any globals it defines are
// _not_ the globals used by "real" std. So this import, defined only during
// testing gives test-std access to real-std lang items and globals. See #2912
#[cfg(test)] extern crate std as realstd;
Jethro (Feb 12 2019 at 09:29, on Zulip):

ok... so shouldn't there be a macro_use on that extern crate as well?

Jethro (Feb 12 2019 at 09:30, on Zulip):

(I'm testing a build now with #[cfg(not(test))] macro_rules! thread_local { ... })

RalfJ (Feb 12 2019 at 09:30, on Zulip):

hm, maybe? no idea^^

RalfJ (Feb 12 2019 at 09:31, on Zulip):

I dont think it matters which of the two macros you use though

RalfJ (Feb 12 2019 at 09:31, on Zulip):

so I dont think that'll help

RalfJ (Feb 12 2019 at 09:31, on Zulip):

not adding the static at all to the test build seems better, like what you did

Jethro (Feb 12 2019 at 10:52, on Zulip):

this appears to work actually

Last update: Nov 21 2019 at 23:25UTC