From b0c4e6fd2fe46211002bfd0efa26612f79285f39 Mon Sep 17 00:00:00 2001 From: Lephe Date: Tue, 6 Aug 2024 11:52:41 +0200 Subject: [PATCH] meta: build with -ffreestanding As of now (fxSDK 2.11), this flag is set globally by the fxSDK, so this change doesn't have an immediate effect. However, I've been experimenting with removing -ffreestanding at the global level to allow applications to build with the hosted C++ library (which isn't fully supported by has many supported features, like containers, that aren't available in free-standing mode). Without -ffreestanding, LTO makes weird decisions and ends up pruning way too many symbols from libraries, leading to undefined references for symbols provided by the standard library. Here is a minimal example. --- % cat abort.c void abort(void) { __builtin_unreachable(); } % cat main.c extern void abort(void); int start(void) { abort(); return 0; } % cat sh-min.x ENTRY(_start) OUTPUT_FORMAT(elf32-sh) SECTIONS { . = 0x1000; .text : { *(.text) } .data : { *(.data) } .bss : { *(.bss) } } % sh-elf-gcc -flto -nostdlib -ffreestanding -c abort.c -o abort.o % sh-elf-gcc-ar rcs abort.a abort.o % sh-elf-gcc -flto -nostdlib -c main.c -o main.o % sh-elf-gcc -flto -nostdlib -save-temps main.o -o main -T ./sh-min.x abort.a -lgcc ld: ./main.ltrans0.ltrans.o: in function `_start': :(.text+0xc): undefined reference to `_abort' --- To solve the bug in this example, add -ffreestanding when making main.o. I haven't been able to sufficiently dump/introspect intermediate files to understand what's happening yet. It's also unclear whether the fix is clean since LTO normally requires all files to be built with the same settings, so adding -ffreestanding to gint but not the add-in seems suspicious. There is, however, an inherent incompatibility in the conjunction of (1) building kernels with -ffreestanding, (2) building add-ins that use the C++ library thus require -fhosted, and (3) only linking objects files built with the same options. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eafed97..3b2dd45 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -261,7 +261,7 @@ fxconv_declare_assets(${ASSETS_FX} ${ASSETS_CG}) include_directories( "${PROJECT_SOURCE_DIR}/include" "${PROJECT_BINARY_DIR}/include") -add_compile_options(-Wall -Wextra -std=c11 -Os -g -fstrict-volatile-bitfields -mtas -flto) +add_compile_options(-Wall -Wextra -std=c11 -Os -g -fstrict-volatile-bitfields -mtas -ffreestanding -flto) if("${FXSDK_PLATFORM_LONG}" STREQUAL fx9860G) add_compile_definitions(FX9860G)