Optimize buildinfo RAM usage on 32-bit platforms

Use direct symbol access on 32-bit platforms to avoid 8 bytes of RAM
overhead. Keep indirection workaround only on 64-bit platforms where
PC-relative relocations cause linking issues.
This commit is contained in:
David Woodhouse
2025-12-11 23:35:54 +09:00
parent 295b317809
commit ccebe613e2

View File

@@ -21,16 +21,21 @@ extern const char ESPHOME_BUILD_TIME[];
namespace esphome {
namespace buildinfo {
// Reference the linker symbols as uintptr_t from the *data* section to
// avoid issues with pc-relative relocations on 64-bit platforms. And
// don't let the compiler know they're const or it'll optimise away the
// whole thing and emit a relocation to the ESPHOME_XXX symbols above
// directly, which defaults the whole point!
//
#if __SIZEOF_POINTER__ > 4
// On 64-bit platforms, reference the linker symbols as uintptr_t from the *data* section to
// avoid issues with pc-relative relocations. Don't let the compiler know they're const or
// it'll optimise away the whole thing and emit a relocation to the ESPHOME_XXX symbols
// directly, which defeats the whole point!
// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
static uintptr_t config_hash = (uintptr_t) &ESPHOME_CONFIG_HASH;
static uintptr_t build_time = (uintptr_t) &ESPHOME_BUILD_TIME;
static uintptr_t config_hash_ptr = (uintptr_t) &ESPHOME_CONFIG_HASH;
static uintptr_t build_time_ptr = (uintptr_t) &ESPHOME_BUILD_TIME;
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
#define config_hash config_hash_ptr
#define build_time build_time_ptr
#else
#define config_hash ((uintptr_t) &ESPHOME_CONFIG_HASH)
#define build_time ((uintptr_t) &ESPHOME_BUILD_TIME)
#endif
const char *get_config_hash() {
static char hash_str[9];