From 95b9a477b6743004e0e9bf728b25bf63d2908777 Mon Sep 17 00:00:00 2001 From: Manuel Simoni Date: Sat, 27 Feb 2010 17:37:59 +0100 Subject: [PATCH] weak function symbols --- examples/ex_weak.c | 11 +++++++++++ examples/weak_f.c | 6 ++++++ libtcc.c | 8 ++++++-- tcc.h | 4 +++- tccgen.c | 4 ++++ tcctok.h | 2 ++ 6 files changed, 32 insertions(+), 3 deletions(-) create mode 100755 examples/ex_weak.c create mode 100644 examples/weak_f.c diff --git a/examples/ex_weak.c b/examples/ex_weak.c new file mode 100755 index 00000000..2a2bd19c --- /dev/null +++ b/examples/ex_weak.c @@ -0,0 +1,11 @@ +#! /usr/local/bin/tcc -run +#include + +extern void weak_f (void) __attribute__ ((weak)); + +int main () +{ + if (weak_f) { + weak_f(); + } +} diff --git a/examples/weak_f.c b/examples/weak_f.c new file mode 100644 index 00000000..e7445317 --- /dev/null +++ b/examples/weak_f.c @@ -0,0 +1,6 @@ +#include + +void weak_f (void) +{ + printf("Weak\n"); +} diff --git a/libtcc.c b/libtcc.c index bea52de1..feb30bc1 100644 --- a/libtcc.c +++ b/libtcc.c @@ -424,8 +424,12 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, if (sym->type.t & VT_STATIC) sym_bind = STB_LOCAL; - else - sym_bind = STB_GLOBAL; + else { + if (FUNC_WEAK(sym->type.ref->r)) + sym_bind = STB_WEAK; + else + sym_bind = STB_GLOBAL; + } if (!sym->c) { name = get_tok_str(sym->v, NULL); diff --git a/tcc.h b/tcc.h index 0170f11c..21cce75d 100644 --- a/tcc.h +++ b/tcc.h @@ -272,7 +272,8 @@ typedef struct AttributeDef { func_import : 1, func_args : 5, mode : 4, - fill : 12; + weak : 1, + fill : 11; struct Section *section; } AttributeDef; @@ -283,6 +284,7 @@ typedef struct AttributeDef { #define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args) #define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned) #define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed) +#define FUNC_WEAK(r) (((AttributeDef*)&(r))->weak) #define ATTR_MODE(r) (((AttributeDef*)&(r))->mode) #define INT_ATTR(ad) (*(int*)(ad)) diff --git a/tccgen.c b/tccgen.c index e761f917..805410b8 100644 --- a/tccgen.c +++ b/tccgen.c @@ -2462,6 +2462,10 @@ static void parse_attribute(AttributeDef *ad) case TOK_PACKED2: ad->packed = 1; break; + case TOK_WEAK1: + case TOK_WEAK2: + ad->weak = 1; + break; case TOK_UNUSED1: case TOK_UNUSED2: /* currently, no need to handle it because tcc does not diff --git a/tcctok.h b/tcctok.h index 9075865a..a69f6f9c 100644 --- a/tcctok.h +++ b/tcctok.h @@ -93,6 +93,8 @@ DEF(TOK_ALIGNED2, "__aligned__") DEF(TOK_PACKED1, "packed") DEF(TOK_PACKED2, "__packed__") + DEF(TOK_WEAK1, "weak") + DEF(TOK_WEAK2, "__weak__") DEF(TOK_UNUSED1, "unused") DEF(TOK_UNUSED2, "__unused__") DEF(TOK_CDECL1, "cdecl")