Search code examples
cgccenumsarmld

Enforce 32-bit enums with GCC linker


I'm writing a bare metal application for an ARM device (no OS). I need 32-bit enums, so I compiled the application with the -fno-short-enums compiler flag. Without this flag, I get variable enums (and enforcing the size by adding an additional 0xFFFFFFFF value to each enum is not an option).

Now I get the following linker warning for every object:

c:/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/6.2.1/../../../../arm-none-eabi/bin/ld.exe: warning: ./src/test.o uses 32-bit enums yet the output is to use variable-size enums; use of enum values across objects may fail

It's just a warning, no error. But what does it mean exactly? How can I specify the "output"?

I tried to recompile the newlib with the above flag to ensure that all objects use the same enum size, but I'm still getting the warning. Is there something I missed?


Solution

  • After a while I got it working. I rebuilt the whole toolchain including the compiler with this flag. Here is the way I did:

    1. Get the toolchain source from https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads

    2. Add 3 lines to some sections of the buildscript build-toolchain.sh:

      saveenv
      saveenvvar CFLAGS_FOR_TARGET '-fno-short-enums'
        [...build commands...]
      restoreenv
      

      Modified sections:

      • Task [III-1] /$HOST_NATIVE/gcc-first/
      • Task [III-2] /$HOST_NATIVE/newlib/
      • Task [III-4] /$HOST_NATIVE/gcc-final/
      • Task [IV-3] /$HOST_MINGW/gcc-final/

      I Skipped building newlib-nano and gcc-size-libstdcxx.

    3. Run the modified Scripts build-prerequisites.sh and build-toolchain.sh to build everything.


    After that, the compiler is using the large-enum-mode and the linker is fine with my objects. But now, I get the opposit warning for some objects of the newlib (lib_a-mbtowc_r.o, lib_a-svfiprintf.o, lib_a-svfprintf.o, lib_a-vfprintf.o and lib_a-vfiprintf.o):

    c:/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/6.2.1/../../../../arm-none-eabi/bin/ld.exe: warning: c:/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/6.2.1/../../../../arm-none-eabi/lib\libc.a(lib_a-mbtowc_r.o) uses variable-size enums yet the output is to use 32-bit enums; use of enum values across objects may fail
    

    I looked into the makefiles of these object and they are explicitly set to variable-size-enums, sadly. The only "solution" for this, was to add a linker flag to mute this warning:

    -Xlinker -no-enum-size-warning
    

    That's it.