From cf08675702044c180553c866ba1fde0414f00590 Mon Sep 17 00:00:00 2001 From: Joe Soroka Date: Tue, 1 Feb 2011 09:41:03 -0800 Subject: [PATCH] weak definitions overrule non-weak prototypes --- tccgen.c | 12 ++++++++++++ tests/Makefile | 11 +++++++++-- tests/tcctest.c | 6 ++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/tccgen.c b/tccgen.c index 2d7e4aec..7144ccdd 100644 --- a/tccgen.c +++ b/tccgen.c @@ -5194,6 +5194,12 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, vsetc(type, VT_CONST | VT_SYM, &cval); vtop->sym = sym; } + /* patch symbol weakness */ + if (type->t & VT_WEAK) { + unsigned char *st_info = + &((ElfW(Sym) *)symtab_section->data)[sym->c].st_info; + *st_info = ELF32_ST_INFO(STB_WEAK, ELF32_ST_TYPE(*st_info)); + } #ifdef CONFIG_TCC_BCHECK /* handles bounds now because the symbol must be defined before for the relocation */ @@ -5314,6 +5320,12 @@ static void gen_function(Sym *sym) /* patch symbol size */ ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size = ind - func_ind; + /* patch symbol weakness (this definition overrules any prototype) */ + if (sym->type.t & VT_WEAK) { + unsigned char *st_info = + &((ElfW(Sym) *)symtab_section->data)[sym->c].st_info; + *st_info = ELF32_ST_INFO(STB_WEAK, ELF32_ST_TYPE(*st_info)); + } if (tcc_state->do_debug) { put_stabn(N_FUN, 0, 0, ind - func_ind); } diff --git a/tests/Makefile b/tests/Makefile index 017f35cf..4bebc2be 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -6,7 +6,7 @@ TESTS = libtest test3 # these should work too -# TESTS += test1 test2 speedtest btest +# TESTS += test1 test2 speedtest btest weaktest # these don't work as they should # TESTS += test4 asmtest @@ -26,7 +26,7 @@ DISAS=objdump -d all test : $(TESTS) # make sure that tcc exists -test1 test2 test3 test4 btest speedtest asmtest : ../tcc +test1 test2 test3 test4 btest speedtest asmtest weaktest : ../tcc ../%: $(MAKE) -C .. $* @@ -116,6 +116,13 @@ speedtest: ex2 ex3 time ./ex3 35 time $(TCC) -run ../examples/ex3.c 35 +weaktest: test.ref + $(TCC) -c tcctest.c -o weaktest.tcc.o + $(CC) -c tcctest.c -o weaktest.gcc.o -I. -w $(CFLAGS) + objdump -t weaktest.tcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.tcc.o.txt + objdump -t weaktest.gcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.gcc.o.txt + diff weaktest.gcc.o.txt weaktest.tcc.o.txt && echo "Weak Auto Test OK" + ex%: ../examples/ex%.c $(CC) -o $@ $< $(CFLAGS) diff --git a/tests/tcctest.c b/tests/tcctest.c index 8c2b2a25..01289575 100644 --- a/tests/tcctest.c +++ b/tests/tcctest.c @@ -2231,19 +2231,25 @@ void builtin_test(void) extern int __attribute__((weak)) weak_f1(void); extern int __attribute__((weak)) weak_f2(void); +extern int weak_f3(void); extern int __attribute__((weak)) weak_v1; extern int __attribute__((weak)) weak_v2; +extern int weak_v3; void __attribute__((weak)) weak_test(void) { printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123); printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123); + printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123); printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123); printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123); + printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123); } int __attribute__((weak)) weak_f2() { return 222; } +int __attribute__((weak)) weak_f3() { return 333; } int __attribute__((weak)) weak_v2 = 222; +int __attribute__((weak)) weak_v3 = 333; void const_func(const int a) { -- 2.11.4.GIT