X86 does not follow -fuse-init-array

Hi Devs,

consider below testcases,
$cat test.cc
class B {
public:
B(int t) {
j=t;
}
int j;
};
B b(1);

I bet if you use -target i386-linux it will work. You passed -target -i386, which is a triple with no OS, so Clang is doing something arbitrary. Grepping Clang sources shows that this flag is handled in an OS-specific manner:

$ git grep -i fuse_init_array …/clang/lib/Driver/
…/clang/lib/Driver/ToolChains/Fuchsia.cpp: if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
…/clang/lib/Driver/ToolChains/Gnu.cpp: if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
…/clang/lib/Driver/ToolChains/NetBSD.cpp: if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
…/clang/lib/Driver/ToolChains/WebAssembly.cpp: if (DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,

The Gnu.cpp file most likely handles the case of Linux.

This logic looks like it is ripe for refactoring. Patches very welcome. =(

We should probably make -fuse-init-array the CC1 default and the default
for Driver/ToolChains/Gnu.cpp. Very few platforms actually use
.ctors/.dtors nowadays. (This caused a bug this year which was fixed by ⚙ D62509 [Driver] Render target options (e.g. -fuse-init-array) for -fembed-bitcode)

If my suggestion does not sound too stupid, I can create a patch.

Fangrui,
All of the OSes/Targets which uses ELF Binary Format go for default -fuse-init-array except NetBSD.

Please see here
https://reviews.llvm.org/D71360

kamlesh

Fangrui,
All of the OSes/Targets which uses ELF Binary Format go for default
-fuse-init-array except NetBSD.
Please see here
⚙ D71360 Honor -fuse-init-array when os is not specified on x86

kamlesh

I have investigated this one month ago (Fix Issue 20220 - Use .init_array/.fini_array instead of .ctors/.dtors on !TARGET_OPENBSD by MaskRay · Pull Request #10562 · dlang/dmd · GitHub).
The OpenBSD support is also relatively new (Aug 2016).

We can delete some -fuse-init-array tests. We only need some
-fno-use-init-array tests on platforms that may need it.