Short description: when stm32hal been built from CumeMX sources, with the same options as PIO use, .data.impure_data appears in output and eats 1K RAM.
I use CubeMX to generate chip init configs. For consistency reasons, i’l like to build HAL from CubeMX sources, instead of PIO built-ins. So, carefully inspected stm32cube.py, _bare.py and created post-script to patch _bare.py output envs - make it look like after stm32cube.py.
In general, that works, but memory consumption increased for 1K due injected impure_data variable. CLI params of compiler/linker looks the same. HAL version is the same (F0 1.9.0). I did not edited HAL files and application files. Only added the same version, generated by CubeMX and builded project. Intrusion is zero.
Found working solution is to remove -Wl, prefix from -Wl,-T<path> variable in LINKFLAGS.
But i could not understand why this help :(. Could anyone explain?
Here is final version of post-hook script:
for e in [ env, projenv ]:
# Fix options after "_bare.py"
e.Replace(LINKFLAGS = [i for i in e['LINKFLAGS'] if i not in [ '-nostartfiles', '-nostdlib' ]])
e.Append(LINKFLAGS = [ "--specs=nano.specs", "--specs=nosys.specs" ])
e.Replace(AS = '$CC', ASCOM = '$ASPPCOM')
# .ld script should be passed to linker directly as "-T<path>"
# instead of "-Wl,-T<path>", to avoid 1K RAM loss for ".data.impure_data"
e.Replace(BUILD_FLAGS = [i for i in e['BUILD_FLAGS'] if not i.startswith('-Wl,-T')])
e.Replace(LINKFLAGS = [(i[4:] if i.startswith('-Wl,-T') else i) for i in e['LINKFLAGS']])
I can confirm this. When using the default linker script with “pio run -v”, I see a “-T …” arg on the final arm-none-eabi-g++ call. When adding my own “-Wl,-T…” to build_flags, the resulting binary is over 1 KB larger, with an “impure” data section added. When I add “-T…” to to build_flags, I can see with “pio run -v” that it’s not passed to the final link, instead using the default .ld file from pio.
To fix it, I can manually re-enter the “pio run -v” link command, removing the “-Wl,” prefix, and all is well.
PS. For completeness, the 1k-larger result is with these settings:
Indeed, it’s strange why the linker behaves differently if the ld-script is specified directly. Anyway, the impure_data section is part of the newlib-nano library’s reentrancy support and in order to disable it you need to comment call to __libc_init_array in your startup routine and specify -nostartfiles flag in LINKFLAGS. Besides, starting with the next release of ststm32 platform, -Wl-T flag will be considered as a deprecated way of specifying linker scripts, instead please use the following syntax to specify custom one:
board_build.ldscript = path/to/script.ld
In this case this script will be added using the -T flag.