From 08c777053cb3b7f4f5d33bb0beeb82787d30ebe0 Mon Sep 17 00:00:00 2001 From: Petr Skocik Date: Sun, 8 Oct 2023 18:55:04 +0200 Subject: [PATCH] Support aliasing static global nonfunction objects Enables code such as: #undef NDEBUG #include #include static int st_x = 42; static int st_x_ __attribute((alias("st_x"))); int main(void){ assert((uintptr_t)&st_x == (uintptr_t)&st_x_); } which would previously fail with no compiler warnings. The limitation of this is that the alias must be done (or redone) after an actual definition. An alias done right after a later overridden tentative declaration won't work (sufficient for my use case). --- tccgen.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tccgen.c b/tccgen.c index fd0680fc..e640e78b 100644 --- a/tccgen.c +++ b/tccgen.c @@ -8661,6 +8661,16 @@ static int decl(int l) else if (l == VT_CONST) /* uninitialized global variables may be overridden */ type.t |= VT_EXTERN; + + if (ad.alias_target && 1) { + Sym *alias_target = sym_find(ad.alias_target); + ElfSym *esym = elfsym(alias_target); + if (!esym) tcc_error("unsupported forward __alias__ attribute"); + sym = external_sym(v, &type, r, &ad); + put_extern_sym2(sym, esym->st_shndx, + esym->st_value, esym->st_size, 1); + } + decl_initializer_alloc(&type, &ad, r, has_init, v, l == VT_CONST); } } -- 2.11.4.GIT