mirror of
https://github.com/mirror/tinycc.git
synced 2025-01-13 05:10:07 +08:00
support weak attribute on variables
This commit is contained in:
parent
6839382480
commit
4d5105c8f1
2
libtcc.c
2
libtcc.c
@ -429,7 +429,7 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section,
|
|||||||
if (sym->type.t & VT_STATIC)
|
if (sym->type.t & VT_STATIC)
|
||||||
sym_bind = STB_LOCAL;
|
sym_bind = STB_LOCAL;
|
||||||
else {
|
else {
|
||||||
if (sym_type == STT_FUNC && sym->type.ref && FUNC_WEAK(sym->type.ref->r))
|
if (sym->type.t & VT_WEAK)
|
||||||
sym_bind = STB_WEAK;
|
sym_bind = STB_WEAK;
|
||||||
else
|
else
|
||||||
sym_bind = STB_GLOBAL;
|
sym_bind = STB_GLOBAL;
|
||||||
|
6
tcc.h
6
tcc.h
@ -293,7 +293,6 @@ typedef struct AttributeDef {
|
|||||||
#define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)
|
#define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)
|
||||||
#define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)
|
#define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)
|
||||||
#define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)
|
#define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)
|
||||||
#define FUNC_WEAK(r) (((AttributeDef*)&(r))->weak)
|
|
||||||
#define ATTR_MODE(r) (((AttributeDef*)&(r))->mode)
|
#define ATTR_MODE(r) (((AttributeDef*)&(r))->mode)
|
||||||
#define INT_ATTR(ad) (*(int*)(ad))
|
#define INT_ATTR(ad) (*(int*)(ad))
|
||||||
|
|
||||||
@ -629,11 +628,12 @@ struct TCCState {
|
|||||||
#define VT_INLINE 0x00000400 /* inline definition */
|
#define VT_INLINE 0x00000400 /* inline definition */
|
||||||
#define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
|
#define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
|
||||||
#define VT_EXPORT 0x00008000 /* win32: data exported from dll */
|
#define VT_EXPORT 0x00008000 /* win32: data exported from dll */
|
||||||
|
#define VT_WEAK 0x00010000 /* win32: data exported from dll */
|
||||||
|
|
||||||
#define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
|
#define VT_STRUCT_SHIFT 17 /* shift for bitfield shift values */
|
||||||
|
|
||||||
/* type mask (except storage) */
|
/* type mask (except storage) */
|
||||||
#define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT)
|
#define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK)
|
||||||
#define VT_TYPE (~(VT_STORAGE))
|
#define VT_TYPE (~(VT_STORAGE))
|
||||||
|
|
||||||
/* token values */
|
/* token values */
|
||||||
|
7
tccgen.c
7
tccgen.c
@ -2922,6 +2922,9 @@ static int parse_btype(CType *type, AttributeDef *ad)
|
|||||||
case TOK_ATTRIBUTE1:
|
case TOK_ATTRIBUTE1:
|
||||||
case TOK_ATTRIBUTE2:
|
case TOK_ATTRIBUTE2:
|
||||||
parse_attribute(ad);
|
parse_attribute(ad);
|
||||||
|
if (ad->weak) {
|
||||||
|
t |= VT_WEAK;
|
||||||
|
}
|
||||||
if (ad->mode) {
|
if (ad->mode) {
|
||||||
u = ad->mode -1;
|
u = ad->mode -1;
|
||||||
t = (t & ~VT_BTYPE) | u;
|
t = (t & ~VT_BTYPE) | u;
|
||||||
@ -3189,6 +3192,10 @@ static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
|
|||||||
post_type(type, ad);
|
post_type(type, ad);
|
||||||
if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
|
if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
|
||||||
parse_attribute(ad);
|
parse_attribute(ad);
|
||||||
|
|
||||||
|
if (ad->weak)
|
||||||
|
type->t |= VT_WEAK;
|
||||||
|
|
||||||
if (!type1.t)
|
if (!type1.t)
|
||||||
return;
|
return;
|
||||||
/* append type at the end of type1 */
|
/* append type at the end of type1 */
|
||||||
|
@ -82,6 +82,7 @@ void local_label_test(void);
|
|||||||
void statement_expr_test(void);
|
void statement_expr_test(void);
|
||||||
void asm_test(void);
|
void asm_test(void);
|
||||||
void builtin_test(void);
|
void builtin_test(void);
|
||||||
|
void weak_test(void);
|
||||||
|
|
||||||
int fib(int n);
|
int fib(int n);
|
||||||
void num(int n);
|
void num(int n);
|
||||||
@ -537,6 +538,7 @@ int main(int argc, char **argv)
|
|||||||
local_label_test();
|
local_label_test();
|
||||||
asm_test();
|
asm_test();
|
||||||
builtin_test();
|
builtin_test();
|
||||||
|
weak_test();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2227,6 +2229,22 @@ void builtin_test(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
extern int __attribute__((weak)) weak_f1(void);
|
||||||
|
extern int __attribute__((weak)) weak_f2(void);
|
||||||
|
extern int __attribute__((weak)) weak_v1;
|
||||||
|
extern int __attribute__((weak)) weak_v2;
|
||||||
|
|
||||||
|
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_v1=%d\n",&weak_v1 ? weak_v1 : 123);
|
||||||
|
printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
|
||||||
|
}
|
||||||
|
|
||||||
|
int __attribute__((weak)) weak_f2() { return 222; }
|
||||||
|
int __attribute__((weak)) weak_v2 = 222;
|
||||||
|
|
||||||
void const_func(const int a)
|
void const_func(const int a)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user