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':
<artificial>:(.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.
This commit is contained in:
Lephe 2024-08-06 11:52:41 +02:00
parent ae3250edd0
commit b0c4e6fd2f
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495

View file

@ -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)