History

better external allocs handling

bellard committed 24 years ago

Commit 8523baf1 parent 18a80131 file · +185 −57Browse files

Unified · Split

tcc.c+185−57
@@ -71,6 +71,17 @@ typedef struct SymStack { · 70 unmodified lines
7171 struct Sym *hash[SYM_HASH_SIZE];
7272 } SymStack;
7373
74+/* relocation entry (currently only used for functions or variables */
75+typedef struct Reloc {
76+ int type; /* type of relocation */
77+ int addr; /* address of relocation */
78+ struct Reloc *next; /* next relocation */
79+} Reloc;
80+
81+#define RELOC_ADDR32 1 /* 32 bits relocation */
82+#define RELOC_REL32 2 /* 32 bits relative relocation */
83+
84+
7485 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
7586 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
7687
@@ -144,13 +155,15 @@ int nb_include_paths; · 67 unmodified lines
144155 #define VT_ENUM 0x00800 /* enum definition */
145156 #define VT_FUNC 0x01000 /* function type */
146157 #define VT_STRUCT 0x002000 /* struct/union definition */
147−#define VT_TYPEDEF 0x004000 /* typedef definition */
148−#define VT_EXTERN 0x008000 /* extern definition */
149−#define VT_STATIC 0x010000 /* static variable */
150−#define VT_SHORT 0x020000 /* short type */
158+#define VT_SHORT 0x004000 /* short type */
151159 #define VT_STRUCT_SHIFT 18 /* structure/enum name shift (14 bits left) */
152160
153−#define VT_TYPE 0xffffffc0 /* type mask */
161+/* storage */
162+#define VT_EXTERN 0x00008000 /* extern definition */
163+#define VT_STATIC 0x00010000 /* static variable */
164+#define VT_TYPEDEF 0x00020000 /* typedef definition */
165+
166+#define VT_TYPE 0xfffc7fc0 /* type mask */
154167 #define VT_TYPEN 0x0000003f /* ~VT_TYPE */
155168 #define VT_FUNCN -4097 /* ~VT_FUNC */
156169
@@ -269,12 +282,12 @@ enum { · 112 unmodified lines
269282 };
270283
271284 void sum();
272−void next();
285+void next(void);
273286 void next_nomacro();
274287 int expr_const();
275288 void expr_eq();
276−void expr();
277−void decl();
289+void expr(void);
290+void decl(int l);
278291 void decl_initializer(int t, int c, int first, int size_only);
279292 int decl_initializer_alloc(int t, int has_init);
280293 int gv(void);
@@ -1450,12 +1463,12 @@ typedef struct GFuncContext { · 1169 unmodified lines
14501463 int args_size;
14511464 } GFuncContext;
14521465
1453−void g(c)
1466+void g(int c)
14541467 {
14551468 *(char *)ind++ = c;
14561469 }
14571470
1458−void o(c)
1471+void o(int c)
14591472 {
14601473 while (c) {
14611474 g(c);
@@ -1463,6 +1476,49 @@ void o(c) · 1 unmodified line
14631476 }
14641477 }
14651478
1479+void gen_le32(int c)
1480+{
1481+ g(c);
1482+ g(c >> 8);
1483+ g(c >> 16);
1484+ g(c >> 24);
1485+}
1486+
1487+/* add a new relocation entry to symbol 's' */
1488+void greloc(Sym *s, int addr, int type)
1489+{
1490+ Reloc *p;
1491+ p = malloc(sizeof(Reloc));
1492+ if (!p)
1493+ error("memory full");
1494+ p->type = type;
1495+ p->addr = addr;
1496+ p->next = (Reloc *)s->c;
1497+ s->c = (int)p;
1498+}
1499+
1500+/* patch each relocation entry with value 'val' */
1501+void greloc_patch(Sym *s, int val)
1502+{
1503+ Reloc *p, *p1;
1504+
1505+ p = (Reloc *)s->c;
1506+ while (p != NULL) {
1507+ p1 = p->next;
1508+ switch(p->type) {
1509+ case RELOC_ADDR32:
1510+ *(int *)p->addr = val;
1511+ break;
1512+ case RELOC_REL32:
1513+ *(int *)p->addr = val - p->addr - 4;
1514+ break;
1515+ }
1516+ free(p);
1517+ p = p1;
1518+ }
1519+ s->c = val;
1520+}
1521+
14661522 /* output a symbol and patch all calls to it */
14671523 void gsym_addr(t, a)
14681524 {
@@ -1484,7 +1540,7 @@ void gsym(t) · 15 unmodified lines
14841540 #define psym oad
14851541
14861542 /* instruction + 4 bytes data. Return the address of the data */
1487−int oad(c, s)
1543+int oad(int c, int s)
14881544 {
14891545 o(c);
14901546 *(int *)ind = s;
@@ -1493,6 +1549,17 @@ int oad(c, s) · 2 unmodified lines
14931549 return s;
14941550 }
14951551
1552+/* output constant with relocation if 't & VT_FORWARD' is true */
1553+void gen_addr32(int c, int t)
1554+{
1555+ if (!(t & VT_FORWARD)) {
1556+ gen_le32(c);
1557+ } else {
1558+ greloc((Sym *)c, ind, RELOC_ADDR32);
1559+ gen_le32(0);
1560+ }
1561+}
1562+
14961563 /* XXX: generate correct pointer for forward references to functions */
14971564 /* r = (ft, fc) */
14981565 void load(r, ft, fc)
@@ -1516,7 +1583,8 @@ void load(r, ft, fc) · 17 unmodified lines
15161583 else
15171584 o(0x8b); /* movl */
15181585 if (v == VT_CONST) {
1519− oad(0x05 + r * 8, fc); /* 0xXX, r */
1586+ o(0x05 + r * 8); /* 0xXX, r */
1587+ gen_addr32(fc, ft);
15201588 } else if (v == VT_LOCAL) {
15211589 oad(0x85 + r * 8, fc); /* xx(%ebp), r */
15221590 } else {
@@ -1524,7 +1592,8 @@ void load(r, ft, fc) · 1 unmodified line
15241592 }
15251593 } else {
15261594 if (v == VT_CONST) {
1527− oad(0xb8 + r, fc); /* mov $xx, r */
1595+ o(0xb8 + r); /* mov $xx, r */
1596+ gen_addr32(fc, ft);
15281597 } else if (v == VT_LOCAL) {
15291598 o(0x8d);
15301599 oad(0x85 + r * 8, fc); /* lea xxx(%ebp), r */
@@ -1559,7 +1628,8 @@ void store(r, ft, fc) · 28 unmodified lines
15591628 o(0x66);
15601629 o(0x89 - b);
15611630 if (fr == VT_CONST) {
1562− oad(0x05 + r * 8, fc); /* mov r,xxx */
1631+ o(0x05 + r * 8); /* mov r,xxx */
1632+ gen_addr32(fc, ft);
15631633 } else if (fr == VT_LOCAL) {
15641634 oad(0x85 + r * 8, fc); /* mov r,xxx(%ebp) */
15651635 } else if (ft & VT_LVAL) {
@@ -1615,9 +1685,11 @@ void gfunc_call(GFuncContext *c, int ft, int fc) · 49 unmodified lines
16151685 if (r == VT_CONST) {
16161686 /* forward reference */
16171687 if (ft & VT_FORWARD) {
1618− *(int *)fc = psym(0xe8, *(int *)fc);
1619− } else
1688+ greloc((Sym *)fc, ind + 1, RELOC_REL32);
1689+ oad(0xe8, 0);
1690+ } else {
16201691 oad(0xe8, fc - ind - 5);
1692+ }
16211693 } else if (r == VT_LOCAL) {
16221694 oad(0x95ff, fc); /* call *xxx(%ebp) */
16231695 } else {
@@ -2171,7 +2243,7 @@ void inc(post, c) · 547 unmodified lines
21712243 }
21722244
21732245 /* enum/struct/union declaration */
2174−int struct_decl(u)
2246+int struct_decl(int u)
21752247 {
21762248 int a, t, b, v, size, align, maxalign, c;
21772249 Sym *s, *ss, **ps;
@@ -2428,27 +2500,19 @@ int type_decl(int *v, int t, int td) · 250 unmodified lines
24282500 }
24292501
24302502 /* define a new external reference to a function 'v' of type 'u' */
2431−Sym *external_func(v, u)
2503+Sym *external_sym(int v, int u)
24322504 {
2433− int n;
24342505 Sym *s;
24352506 s = sym_find(v);
24362507 if (!s) {
2437− n = (int)dlsym(0, get_tok_str(v, 0));
2438− if (n == 0) {
2439− /* used to generate symbol list */
2440− s = sym_push1(&global_stack,
2441− v, u | VT_CONST | VT_LVAL | VT_FORWARD, 0);
2442− } else {
2443− /* int f() */
2444− s = sym_push1(&global_stack,
2445− v, u | VT_CONST | VT_LVAL, n);
2446− }
2508+ /* push forward reference */
2509+ s = sym_push1(&global_stack,
2510+ v, u | VT_CONST | VT_FORWARD, 0);
24472511 }
24482512 return s;
24492513 }
24502514
2451−void indir()
2515+void indir(void)
24522516 {
24532517 if (vt & VT_LVAL)
24542518 gv();
@@ -2459,7 +2523,7 @@ void indir() · 4 unmodified lines
24592523 vt |= VT_LVAL;
24602524 }
24612525
2462−void unary()
2526+void unary(void)
24632527 {
24642528 int n, t, ft, fc, p, align, size;
24652529 Sym *s;
@@ -2523,7 +2587,12 @@ void unary() · 57 unmodified lines
25232587 indir();
25242588 } else if (t == '&') {
25252589 unary();
2526− test_lvalue();
2590+ /* functions names must be treated as function pointers,
2591+ except for unary '&' and sizeof. Since we consider that
2592+ functions are not lvalues, we only have to handle it
2593+ there and in function calls. */
2594+ if (!(vt & VT_FUNC))
2595+ test_lvalue();
25272596 vt = mk_pointer(vt & VT_LVALN);
25282597 } else
25292598 if (t == '!') {
@@ -2577,12 +2646,12 @@ void unary() · 47 unmodified lines
25772646 p = anon_sym++;
25782647 sym_push1(&global_stack, p, 0, FUNC_OLD);
25792648 /* int() function */
2580− s = external_func(t, VT_FUNC | (p << VT_STRUCT_SHIFT));
2649+ s = external_sym(t, VT_FUNC | (p << VT_STRUCT_SHIFT));
25812650 }
25822651 vset(s->t, s->c);
2583− /* if forward reference, we must point to s->c */
2652+ /* if forward reference, we must point to s */
25842653 if (vt & VT_FORWARD)
2585− vc = (int)&s->c;
2654+ vc = (int)s;
25862655 }
25872656 }
25882657
@@ -2632,8 +2701,16 @@ void unary() · 43 unmodified lines
26322701 int rett, retc;
26332702
26342703 /* function call */
2635− if (!(vt & VT_FUNC))
2636− expect("function type");
2704+ if (!(vt & VT_FUNC)) {
2705+ if ((vt & (VT_PTR | VT_ARRAY)) == VT_PTR) {
2706+ vt = pointed_type(vt);
2707+ if (!(vt & VT_FUNC))
2708+ goto error_func;
2709+ } else {
2710+ error_func:
2711+ expect("function type");
2712+ }
2713+ }
26372714 /* get return type */
26382715 s = sym_find((unsigned)vt >> VT_STRUCT_SHIFT);
26392716
@@ -2725,6 +2802,30 @@ void unary() · 85 unmodified lines
27252802 }
27262803 }
27272804
2805+/* check if types are compatible for assignation */
2806+int same_types(int t1, int t2)
2807+{
2808+ t1 &= VT_TYPE;
2809+ t2 &= VT_TYPE;
2810+ if (t1 & VT_PTR) {
2811+ /* XXX: zero test ? */
2812+ if (!(t2 & VT_PTR))
2813+ return 0;
2814+ t1 = pointed_type(t1);
2815+ t2 = pointed_type(t2);
2816+ /* void matches everything */
2817+ if (t1 == VT_VOID || t2 == VT_VOID)
2818+ return 1;
2819+ return same_types(t1, t2);
2820+ } else if (t1 & VT_STRUCT) {
2821+ return (t2 == t1);
2822+ } else {
2823+ /* XXX: not complete */
2824+ return 1;
2825+ }
2826+}
2827+
2828+
27282829 void uneq()
27292830 {
27302831 int t;
@@ -2740,9 +2841,8 @@ void uneq() · 9 unmodified lines
27402841 next();
27412842 if (t == '=') {
27422843 expr_eq();
2743− /* XXX: be more precise */
2744− if ((vt & VT_PTR) != (vstack_ptr[-2] & VT_PTR))
2745− warning("incompatible type");
2844+ if (!same_types(vt, vstack_ptr[-2]))
2845+ error("incompatible types");
27462846 } else {
27472847 vpush();
27482848 expr_eq();
@@ -2919,7 +3019,8 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 170 unmodified lines
29193019 s = local_stack.top;
29203020 while (tok != '}') {
29213021 decl(VT_LOCAL);
2922− block(bsym, csym, case_sym, def_sym, case_reg);
3022+ if (tok != '}')
3023+ block(bsym, csym, case_sym, def_sym, case_reg);
29233024 }
29243025 /* pop locally defined symbols */
29253026 sym_pop(&local_stack, s);
@@ -3002,6 +3103,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 76 unmodified lines
30023103 gsym_addr(c, d);
30033104 skip(')');
30043105 gsym(a);
3106+ skip(';');
30053107 } else
30063108 if (tok == TOK_SWITCH) {
30073109 next();
@@ -3297,9 +3399,9 @@ void decl_initializer(int t, int c, int first, int size_only) · 289 unmodified lines
32973399 /* patch type size if needed */
32983400 if (n < 0)
32993401 s->c = array_length;
3300− } else if (t & VT_STRUCT) {
3402+ } else if ((t & VT_STRUCT) && tok == '{') {
33013403 /* XXX: union needs only one init */
3302− skip('{');
3404+ next();
33033405 s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
33043406 f = s->next;
33053407 array_length = 0;
@@ -3428,14 +3530,20 @@ int decl_initializer_alloc(int t, int has_init) · 122 unmodified lines
34283530
34293531
34303532 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
3431−void decl(l)
3533+void decl(int l)
34323534 {
3433− int *a, t, b, v, u, n, addr, has_init, size, align;
3535+ int *a, t, b, v, u, addr, has_init, size, align;
34343536 Sym *sym;
34353537
34363538 while (1) {
34373539 b = ist();
34383540 if (!b) {
3541+ /* skip redundant ';' */
3542+ /* XXX: find more elegant solution */
3543+ if (tok == ';') {
3544+ next();
3545+ continue;
3546+ }
34393547 /* special test for old K&R protos without explicit int
34403548 type. Only accepted when defining global data */
34413549 if (l == VT_LOCAL || tok < TOK_DEFINE)
@@ -3450,16 +3558,17 @@ void decl(l) · 8 unmodified lines
34503558 while (1) { /* iterate thru each declaration */
34513559 t = type_decl(&v, b, TYPE_DIRECT);
34523560 if (tok == '{') {
3561+ if (l == VT_LOCAL)
3562+ error("cannot use local functions");
34533563 if (!(t & VT_FUNC))
34543564 expect("function definition");
34553565 /* patch forward references */
34563566 if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) {
3457− gsym(sym->c);
3458− sym->c = ind;
3459− sym->t = VT_CONST | VT_LVAL | t;
3567+ greloc_patch(sym, ind);
3568+ sym->t = VT_CONST | t;
34603569 } else {
34613570 /* put function address */
3462− sym_push1(&global_stack, v, VT_CONST | VT_LVAL | t, ind);
3571+ sym_push1(&global_stack, v, VT_CONST | t, ind);
34633572 }
34643573 funcname = get_tok_str(v, 0);
34653574 /* push a dummy symbol to enable local sym storage */
@@ -3516,21 +3625,15 @@ void decl(l) · 50 unmodified lines
35163625 /* save typedefed type */
35173626 sym_push(v, t | VT_TYPEDEF, 0);
35183627 } else if (t & VT_FUNC) {
3519− /* XXX: incorrect to flush, but needed while
3520− waiting for function prototypes */
35213628 /* external function definition */
3522− external_func(v, t);
3629+ external_sym(v, t);
35233630 } else {
35243631 /* not lvalue if array */
35253632 if (!(t & VT_ARRAY))
35263633 t |= VT_LVAL;
35273634 if (b & VT_EXTERN) {
35283635 /* external variable */
3529− /* XXX: factorize with external function def */
3530− n = (int)dlsym(NULL, get_tok_str(v, 0));
3531− if (!n)
3532− error("unknown external variable");
3533− sym_push(v, VT_CONST | t, n);
3636+ external_sym(v, t);
35343637 } else {
35353638 u = l;
35363639 if (t & VT_STATIC)
@@ -3566,6 +3669,30 @@ void open_dll(char *libname) · 29 unmodified lines
35663669 error((char *)dlerror());
35673670 }
35683671
3672+void reloc_external_syms(void)
3673+{
3674+ Sym *s, *s1;
3675+ char *str;
3676+ int addr;
3677+
3678+ s = global_stack.top;
3679+ while (s != NULL) {
3680+ s1 = s->prev;
3681+ if (s->t & VT_FORWARD) {
3682+ /* if there is at least one relocation to do, then find it
3683+ and patch it */
3684+ if (s->c) {
3685+ str = get_tok_str(s->v, 0);
3686+ addr = (int)dlsym(NULL, str);
3687+ if (!addr)
3688+ error("unresolved external reference '%s'", str);
3689+ greloc_patch(s, addr);
3690+ }
3691+ }
3692+ s = s1;
3693+ }
3694+}
3695+
35693696 /* output a binary file (for testing) */
35703697 void build_exe(char *filename)
35713698 {
@@ -3658,6 +3785,7 @@ int main(int argc, char **argv) · 86 unmodified lines
36583785 decl(VT_CONST);
36593786 if (tok != -1)
36603787 expect("declaration");
3788+ reloc_external_syms();
36613789 if (outfile) {
36623790 build_exe(outfile);
36633791 return 0;

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