From 276d9235698bc981fdd0bfb43d1f72628ddadf99 Mon Sep 17 00:00:00 2001 From: Ash Date: Sat, 14 Dec 2024 21:11:07 +1100 Subject: [PATCH] Enable LTO build (#77) * fix(Makefile): Don't double up on optimisation flags This is already provided by BUILD_CFLAGS later on * feat(common): Mark explicitly-placed things as used If we put something in a given section, we should be able to expect it to actually turn up in that section! Doing this will prevent LTO from slurping all the hooks away as "unused". * feat: Enable LTO This uses "fat" binaries, so normal builds won't break, but also includes the LTO data for builds that wish to use it --- Makefile | 3 ++- include/wups/common.h | 11 ++++++++--- include/wups/function_patching.h | 4 +--- include/wups/hooks.h | 3 +-- include/wups/meta.h | 9 +++------ 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 88d2bf3..9dd924b 100644 --- a/Makefile +++ b/Makefile @@ -23,8 +23,9 @@ INCLUDES := include #--------------------------------------------------------------------------------- # options for code generation #--------------------------------------------------------------------------------- -CFLAGS := -g -O2 -Wall -Werror -save-temps \ +CFLAGS := -g -Wall -Werror -save-temps \ -ffunction-sections -fdata-sections \ + -flto -ffat-lto-objects \ $(MACHDEP) \ $(BUILD_CFLAGS) diff --git a/include/wups/common.h b/include/wups/common.h index 8aadc30..e7ca0c2 100644 --- a/include/wups/common.h +++ b/include/wups/common.h @@ -38,11 +38,16 @@ extern "C" { #endif -#define WUPS_SECTION(x) __attribute__((__section__(".wups." x))) +#ifdef __cplusplus +#define WUPS_EXTERN_C extern "C" +#else +#define WUPS_EXTERN_C +#endif + +#define WUPS_SECTION(x) WUPS_EXTERN_C __attribute__((__section__(".wups." x), used)) #define WUPS_META(id, value) \ - extern const char wups_meta_##id[] WUPS_SECTION("meta"); \ - const char wups_meta_##id[] = #id "=" value + WUPS_SECTION("meta") const char wups_meta_##id[] = #id "=" value #ifdef __cplusplus } diff --git a/include/wups/function_patching.h b/include/wups/function_patching.h index dd1923a..a58c09e 100644 --- a/include/wups/function_patching.h +++ b/include/wups/function_patching.h @@ -148,9 +148,7 @@ typedef struct wups_loader_entry_t { #define WUPS_MUST_REPLACE_FOR_PROCESS(x, lib, function_name, targetProcess) WUPS_MUST_REPLACE_EX(NULL, NULL, real_##x, lib, my_##x, function_name, targetProcess) #define WUPS_MUST_REPLACE_EX(pAddress, vAddress, original_func, rpl_type, replace_func, replace_function_name, process) \ - extern const wups_loader_entry_t wups_load_##replace_func \ - WUPS_SECTION("load"); \ - const wups_loader_entry_t wups_load_##replace_func = { \ + WUPS_SECTION("load") const wups_loader_entry_t wups_load_##replace_func = { \ .type = WUPS_LOADER_ENTRY_FUNCTION_MANDATORY, \ ._function = { \ .physical_address = (const void *) pAddress, \ diff --git a/include/wups/hooks.h b/include/wups/hooks.h index a416f46..7f39350 100644 --- a/include/wups/hooks.h +++ b/include/wups/hooks.h @@ -23,8 +23,7 @@ extern "C" { #endif #define WUPS_HOOK_EX(type_def, original_func) \ - extern const wups_loader_hook_t wups_hooks_##original_func WUPS_SECTION("hooks"); \ - const wups_loader_hook_t wups_hooks_##original_func = { \ + WUPS_SECTION("hooks") const wups_loader_hook_t wups_hooks_##original_func = { \ .type = type_def, \ .target = (const void *) &(original_func)} diff --git a/include/wups/meta.h b/include/wups/meta.h index 004a49d..53ec4ad 100644 --- a/include/wups/meta.h +++ b/include/wups/meta.h @@ -50,14 +50,11 @@ extern "C" { WUPS___FINI_WRAPPER(); \ WUPS_INIT_CONFIG_FUNCTIONS(); \ WUPS_META(buildtimestamp, __DATE__ " " __TIME__); \ - extern const char wups_meta_plugin_name[] WUPS_SECTION("meta"); \ - const char wups_meta_plugin_name[] = __plugin_name; \ - extern const char wups_meta_info_dump[] WUPS_SECTION("meta"); \ - const char wups_meta_info_dump[] = "(plugin: " __plugin_name ";" \ + WUPS_SECTION("meta") const char wups_meta_plugin_name[] = __plugin_name; \ + WUPS_SECTION("meta") const char wups_meta_info_dump[] = "(plugin: " __plugin_name ";" \ "wups " WUPS_VERSION_STR ";" \ "buildtime: " __DATE__ " " __TIME__ ")"; \ - extern const char wups_meta_info_linking_order[] WUPS_SECTION("meta"); \ - const char wups_meta_info_linking_order[] = "Loading \"" __plugin_name "\" failed.\n" \ + WUPS_SECTION("meta") const char wups_meta_info_linking_order[] = "Loading \"" __plugin_name "\" failed.\n" \ "Function \"wut_get_thread_specific\" returned unexpected value.\n" \ "Please check linking order (expected \"-lwups -lwut\")";