History

support weak attribute on variables

Joe Soroka committed 15 years ago

Commit 4d5105c1 parent 68393824 files · +29 −4Browse files

Unified · Split

4 files
  1. libtcc.c +1 −1
  2. tcc.h +3 −3
  3. tccgen.c +7 −0
  4. tests/tcctest.c +18 −0
libtcc.c+1−1
@@ -429,7 +429,7 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, · 428 unmodified lines
429429 if (sym->type.t & VT_STATIC)
430430 sym_bind = STB_LOCAL;
431431 else {
432− if (sym_type == STT_FUNC && sym->type.ref && FUNC_WEAK(sym->type.ref->r))
432+ if (sym->type.t & VT_WEAK)
433433 sym_bind = STB_WEAK;
434434 else
435435 sym_bind = STB_GLOBAL;
tcc.h+3−3
@@ -293,7 +293,6 @@ typedef struct AttributeDef { · 292 unmodified lines
293293 #define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)
294294 #define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)
295295 #define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)
296−#define FUNC_WEAK(r) (((AttributeDef*)&(r))->weak)
297296 #define ATTR_MODE(r) (((AttributeDef*)&(r))->mode)
298297 #define INT_ATTR(ad) (*(int*)(ad))
299298
@@ -629,11 +628,12 @@ struct TCCState { · 329 unmodified lines
629628 #define VT_INLINE 0x00000400 /* inline definition */
630629 #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
631630 #define VT_EXPORT 0x00008000 /* win32: data exported from dll */
631+#define VT_WEAK 0x00010000 /* win32: data exported from dll */
632632
633−#define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
633+#define VT_STRUCT_SHIFT 17 /* shift for bitfield shift values */
634634
635635 /* type mask (except storage) */
636−#define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT)
636+#define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK)
637637 #define VT_TYPE (~(VT_STORAGE))
638638
639639 /* token values */
tccgen.c+7−0
@@ -2922,6 +2922,9 @@ static int parse_btype(CType *type, AttributeDef *ad) · 2921 unmodified lines
29222922 case TOK_ATTRIBUTE1:
29232923 case TOK_ATTRIBUTE2:
29242924 parse_attribute(ad);
2925+ if (ad->weak) {
2926+ t |= VT_WEAK;
2927+ }
29252928 if (ad->mode) {
29262929 u = ad->mode -1;
29272930 t = (t & ~VT_BTYPE) | u;
@@ -3189,6 +3192,10 @@ static void type_decl(CType *type, AttributeDef *ad, int *v, int td) · 261 unmodified lines
31893192 post_type(type, ad);
31903193 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
31913194 parse_attribute(ad);
3195+
3196+ if (ad->weak)
3197+ type->t |= VT_WEAK;
3198+
31923199 if (!type1.t)
31933200 return;
31943201 /* append type at the end of type1 */
tests/tcctest.c+18−0
@@ -82,6 +82,7 @@ void local_label_test(void); · 81 unmodified lines
8282 void statement_expr_test(void);
8383 void asm_test(void);
8484 void builtin_test(void);
85+void weak_test(void);
8586
8687 int fib(int n);
8788 void num(int n);
@@ -537,6 +538,7 @@ int main(int argc, char **argv) · 449 unmodified lines
537538 local_label_test();
538539 asm_test();
539540 builtin_test();
541+ weak_test();
540542 return 0;
541543 }
542544
@@ -2227,6 +2229,22 @@ void builtin_test(void) · 1684 unmodified lines
22272229 }
22282230
22292231
2232+extern int __attribute__((weak)) weak_f1(void);
2233+extern int __attribute__((weak)) weak_f2(void);
2234+extern int __attribute__((weak)) weak_v1;
2235+extern int __attribute__((weak)) weak_v2;
2236+
2237+void __attribute__((weak)) weak_test(void)
2238+{
2239+ printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
2240+ printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
2241+ printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
2242+ printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
2243+}
2244+
2245+int __attribute__((weak)) weak_f2() { return 222; }
2246+int __attribute__((weak)) weak_v2 = 222;
2247+
22302248 void const_func(const int a)
22312249 {
22322250 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t