Clang -- incorrectly putting stuff in cxx_global_var_init (when building GCC subcomponents)

Hi. First time posting here.

I’m trying to use a macOS host (x86_64) for NetBSD (aarch64) target cross-development, which I’ve done in the past. But somewhat recent updates NetBSD include a newer GCC in the tree, and that has introduced a glitch. I’m not really an expert on any of this, but I’ll try to describe. Please bear with me.

As a first step, the NetBSD build system creates a cross-building tool chain. This works fine, and I get a working aarch64–netbsd-gcc set of tools that run on x86_64-darwin. All great.

As a next step, I try to build an actual NetBSD “distribution” (i.e., full tree with all the libraries, binaries, header files, etc). During this process, NetBSD builds another gcc toolchain – this one is an aarch64 toolchain that would run on an aarch64 NetBSD system.

During the GCC build process, there are some “generator” programs that ACTUALLY build for the host system (in this case, x86_64-darwin). And this is where we run into a glitch.

When it builds the “gencondmd” program, it fails to link, complaining about several variables not being found:

Undefined symbols for architecture x86_64:
“_aarch64_isa_flags”, referenced from:
___cxx_global_var_init in gencondmd.lo
“_const_int_rtx”, referenced from:
___cxx_global_var_init in gencondmd.lo
“_flag_modulo_sched”, referenced from:
___cxx_global_var_init in gencondmd.lo
“_flag_rounding_math”, referenced from:
___cxx_global_var_init in gencondmd.lo
“_operands”, referenced from:
___cxx_global_var_init in gencondmd.lo
“_optimize”, referenced from:
___cxx_global_var_init in gencondmd.lo
“_reload_completed”, referenced from:
___cxx_global_var_init in gencondmd.lo
“_reload_in_progress”, referenced from:
___cxx_global_var_init in gencondmd.lo
“_this_target_rtl”, referenced from:
___cxx_global_var_init in gencondmd.lo
ld: symbol(s) not found for architecture x86_64

Obviously, those symbols didn’t matter when it built the tools for aarch64-on-x86_64 the first time, but this time we’re not doing quite the same thing – although it is still compiling these tools for x86_64. But I think clang is getting tripped up and thinking these variables are actually used in this case, and I don’t think they are. (The code builds fine on a NetBSD system, btw, but that’s using GCC.)

If I add a little bit of code to the bottom of gencondmd.c (basically instantiating all of these variables), then it works:

#ifdef GENERATOR_FILE

    unsigned long aarch64_isa_flags;
    enum aarch64_tls_type aarch64_tls_dialect;
    int flag_modulo_sched;
    int flag_rounding_math;
    int optimize;

    GTY(()) rtx const_int_rtx[MAX_SAVED_CONST_INT * 2 + 1];
    int reload_completed;
    int reload_in_progress;

    rtx operands[1];

    machine_mode ptr_mode;

#endif

But this seems really ugly and wrong (and might break the x86_64 build – I haven’t tried that yet).

Is there an optimization flag or something that might help clang try harder to realize no one is really referencing these variables?

Thanks!

Rob