From 44e8c238656c21560ca9c262a2a284a5862dc656 Mon Sep 17 00:00:00 2001 From: Jeremy Lorelli Date: Wed, 18 Mar 2026 18:54:10 -0700 Subject: [PATCH] Fix missing table types when linking linStat.a into an IOC When using static libraries, the linker will eliminate object files that it determines to be unreferenced, leading to linStat table types not being registered. The only sure-fire way to fix this is to generate references to symbols within the eliminated object files. This can be done at link time with -Wl,-u or with ugly hacky methods like the one in this commit. --- statApp/src/linStat.h | 5 ++++- statApp/src/tblBase.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/statApp/src/linStat.h b/statApp/src/linStat.h index 16f1f26..be03ba5 100644 --- a/statApp/src/linStat.h +++ b/statApp/src/linStat.h @@ -118,13 +118,16 @@ template std::shared_ptr tblFactory(const std::string& inst, const Reactor& react) { return std::make_shared(inst, react); } + +// When adding a new table, make sure to update tblBase.cpp:crossReference() #define DEFINE_TABLE(name, TblKlass) \ static \ __attribute__((section("linStatTableFactory"), retain, used)) \ const linStat::StatTableFactory tbl ## TblKlass = { \ name, \ &tblFactory, \ - }; + }; \ + int TblKlass ## _Ref = 0; struct StatTableIter { diff --git a/statApp/src/tblBase.cpp b/statApp/src/tblBase.cpp index 7c66588..cf22c2c 100644 --- a/statApp/src/tblBase.cpp +++ b/statApp/src/tblBase.cpp @@ -31,6 +31,31 @@ extern const char __start_linStatTableFactory; extern const char __stop_linStatTableFactory; } +#define REF_TABLE(_klass) \ + extern int _klass ## _Ref; \ + _klass ## _Ref = 1; + +// Kludgy attempt to work around object file elimination in static libraries. +static void __attribute__((used)) +crossReference() { + REF_TABLE(UptimeTable); + REF_TABLE(HWMonTable); + REF_TABLE(FDTable); + REF_TABLE(NetstatTable); + REF_TABLE(IFStatTable); + REF_TABLE(IRQTable); + REF_TABLE(ProcStatusTable); + REF_TABLE(SysStatTable); + REF_TABLE(PidTable); + REF_TABLE(EthtoolTable); + REF_TABLE(MallInfoTable); + REF_TABLE(StatVFSTable); + REF_TABLE(PDBTable); + REF_TABLE(HostTable); + REF_TABLE(ProcStatTable); + REF_TABLE(MemInfoTable); +} + namespace linStat { StatTable::~StatTable() {}