From 9268cbd42e7c93dc64a4ed66dce294547e0998ee Mon Sep 17 00:00:00 2001 From: Philippe Symons Date: Wed, 9 Apr 2025 20:04:08 +0200 Subject: [PATCH] Reduce binary size by eliminating libstdc++ This commit removes all references to things in the libstdc++ library to remove a decent chunk of bloat. This means every std::to_string() call, std::string and std::vector. (as well as iostream related stuff). I replaced those with my own versions ptgb::to_string() and ptgb::vector. Especially the latter is not exactly the same, but close enough. I also replaced operator new and delete with my own implementation to avoid pulling in everything related to exceptions from libstdc++ Another problem was the fact that libtonc uses siscanf, which pulls in everything related to the scanf family of functions and locale support. The worst part of that was that it included a 13KB "categories" symbol from libc_a-categories.o, which was pulled in because of the locale support integrated into newlibc's siscanf() function. To fix that, I created a custom, extremely restricted implementation of siscanf. libtonc only used this function to parse at most 2 integers from a string anyway. --- Makefile | 6 +- include/button_menu.h | 6 +- include/gameboy_colour.h | 1 - include/gb_link.h | 3 +- include/libraries/nanoprintf/CONTRIBUTORS.md | 10 + include/libraries/nanoprintf/LICENSE | 53 + include/libraries/nanoprintf/nanoprintf.h | 1140 ++++++++++++++++++ include/libstd_replacements.h | 187 +++ include/pokemon_data.h | 1 - include/script_var.h | 14 +- include/select_menu.h | 7 +- include/z80_asm.h | 24 +- loader/data/multiboot_rom.bin | Bin 201716 -> 0 bytes source/button_menu.cpp | 4 +- source/flash_mem.cpp | 14 +- source/gameboy_colour.cpp | 60 +- source/global_frame_controller.cpp | 4 +- source/libstd_replacements.cpp | 117 ++ source/main.cpp | 36 +- source/mystery_gift_builder.cpp | 49 +- source/payload_builder.cpp | 76 +- source/pokemon.cpp | 13 +- source/rom_data.cpp | 26 +- source/script_var.cpp | 9 +- source/z80_asm.cpp | 102 +- 25 files changed, 1763 insertions(+), 199 deletions(-) create mode 100644 include/libraries/nanoprintf/CONTRIBUTORS.md create mode 100644 include/libraries/nanoprintf/LICENSE create mode 100644 include/libraries/nanoprintf/nanoprintf.h create mode 100644 include/libstd_replacements.h delete mode 100644 loader/data/multiboot_rom.bin create mode 100644 source/libstd_replacements.cpp diff --git a/Makefile b/Makefile index 80e7e29..7239e05 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ CFLAGS := -g -Wall -O2\ $(ARCH) CFLAGS += $(INCLUDE) -ffunction-sections -fdata-sections -Os -Wall -mthumb -mcpu=arm7tdmi -mtune=arm7tdmi -CXXFLAGS := $(CFLAGS) -g0 -fno-rtti -fno-exceptions -std=c++20 -Wno-volatile -D_GLIBCXX_USE_CXX20_ABI=0 +CXXFLAGS := $(CFLAGS) -g0 -fno-rtti -fno-exceptions -fdata-sections -ffunction-sections -std=c++20 -Wno-volatile -D_GLIBCXX_USE_CXX20_ABI=0 ifeq ($(BUILD_TYPE), debug) CFLAGS += -g -DDEBUG @@ -57,7 +57,7 @@ else ifeq ($(BUILD_TYPE), release) endif ASFLAGS := -g $(ARCH) -LDFLAGS = -Os -g $(ARCH) -Wl,-Map,$(notdir $*.map) -Wl,--gc-sections -mthumb -mcpu=arm7tdmi -mtune=arm7tdmi +LDFLAGS = -Os -g $(ARCH) -Wl,-Map,$(notdir $*.map) -Wl,--gc-sections -mthumb -mcpu=arm7tdmi -mtune=arm7tdmi -Wl,-Map,output.map,--cref -nodefaultlibs CFLAGS += -flto LDFLAGS += -flto @@ -65,7 +65,7 @@ LDFLAGS += -flto #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project #--------------------------------------------------------------------------------- -LIBS := -lmm -ltonc -lgba -lc -lgcc +LIBS := -lmm -ltonc -lgba -lc -lgcc -lsysbase #--------------------------------------------------------------------------------- diff --git a/include/button_menu.h b/include/button_menu.h index fe5597d..f01c755 100644 --- a/include/button_menu.h +++ b/include/button_menu.h @@ -2,7 +2,7 @@ #define MAIN_MENU_H #include -#include +#include "libstd_replacements.h" #include "button_handler.h" @@ -33,8 +33,8 @@ public: void clear_vector(); private: - std::vector