weak definitions overrule non-weak prototypes

This commit is contained in:
Joe Soroka 2011-02-01 09:41:03 -08:00
parent c59d3426b8
commit cf08675702
3 changed files with 27 additions and 2 deletions

View File

@ -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);
}

View File

@ -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)

View File

@ -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)
{