History

added symbol + relocation handling

bellard committed 24 years ago

Commit d2115bf1 parent 46803f02 files · +56 −45Browse files

Unified · Split

2 files
  1. i386-gen.c +22 −26
  2. tcc.c +34 −19
i386-gen.c+22−26
@@ -132,7 +132,7 @@ void gsym(int t) · 131 unmodified lines
132132 #define psym oad
133133
134134 /* instruction + 4 bytes data. Return the address of the data */
135−int oad(int c, int s)
135+static int oad(int c, int s)
136136 {
137137 int ind1;
138138
@@ -147,26 +147,22 @@ int oad(int c, int s) · 8 unmodified lines
147147 }
148148
149149 /* output constant with relocation if 'r & VT_SYM' is true */
150−void gen_addr32(int r, int c)
150+static void gen_addr32(int r, Sym *sym, int c)
151151 {
152− if (!(r & VT_SYM)) {
153− gen_le32(c);
154− } else {
155− greloc(cur_text_section,
156− (Sym *)c, ind, R_386_32);
157− gen_le32(0);
158− }
152+ if (r & VT_SYM)
153+ greloc(cur_text_section, sym, ind, R_386_32);
154+ gen_le32(c);
159155 }
160156
161157 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
162158 opcode bits */
163−void gen_modrm(int op_reg, int r, int c)
159+static void gen_modrm(int op_reg, int r, Sym *sym, int c)
164160 {
165161 op_reg = op_reg << 3;
166162 if ((r & VT_VALMASK) == VT_CONST) {
167163 /* constant memory reference */
168164 o(0x05 | op_reg);
169− gen_addr32(r, c);
165+ gen_addr32(r, sym, c);
170166 } else if ((r & VT_VALMASK) == VT_LOCAL) {
171167 /* currently, we use only ebp as base */
172168 if (c == (char)c) {
@@ -221,14 +217,14 @@ void load(int r, SValue *sv) · 48 unmodified lines
221217 } else {
222218 o(0x8b); /* movl */
223219 }
224− gen_modrm(r, fr, fc);
220+ gen_modrm(r, fr, sv->sym, fc);
225221 } else {
226222 if (v == VT_CONST) {
227223 o(0xb8 + r); /* mov $xx, r */
228− gen_addr32(fr, fc);
224+ gen_addr32(fr, sv->sym, fc);
229225 } else if (v == VT_LOCAL) {
230226 o(0x8d); /* lea xxx(%ebp), r */
231− gen_modrm(r, VT_LOCAL, fc);
227+ gen_modrm(r, VT_LOCAL, sv->sym, fc);
232228 } else if (v == VT_CMP) {
233229 oad(0xb8 + r, 0); /* mov $0, r */
234230 o(0x0f); /* setxx %br */
@@ -278,7 +274,7 @@ void store(int r, SValue *v) · 43 unmodified lines
278274 if (fr == VT_CONST ||
279275 fr == VT_LOCAL ||
280276 (v->r & VT_LVAL)) {
281− gen_modrm(r, v->r, fc);
277+ gen_modrm(r, v->r, v->sym, fc);
282278 } else if (fr != r) {
283279 o(0xc0 + fr + r * 8); /* mov r, fr */
284280 }
@@ -362,9 +358,9 @@ void gfunc_call(GFuncContext *c) · 77 unmodified lines
362358 /* constant case */
363359 if (vtop->r & VT_SYM) {
364360 /* relocation case */
365− greloc(cur_text_section, vtop->c.sym,
361+ greloc(cur_text_section, vtop->sym,
366362 ind + 1, R_386_PC32);
367− oad(0xe8, -4);
363+ oad(0xe8, vtop->c.ul - 4);
368364 } else {
369365 oad(0xe8, vtop->c.ul - ind - 5);
370366 }
@@ -443,7 +439,7 @@ void gfunc_epilog(void) · 72 unmodified lines
443439 greloc(cur_text_section, sym_data,
444440 ind + 1, R_386_32);
445441 oad(0xb8, 0); /* mov %eax, xxx */
446− sym = external_sym(TOK___bound_local_new, func_old_type, 0);
442+ sym = external_global_sym(TOK___bound_local_new, func_old_type, 0);
447443 greloc(cur_text_section, sym,
448444 ind + 1, R_386_PC32);
449445 oad(0xe8, -4);
@@ -453,7 +449,7 @@ void gfunc_epilog(void) · 3 unmodified lines
453449 greloc(cur_text_section, sym_data,
454450 ind + 1, R_386_32);
455451 oad(0xb8, 0); /* mov %eax, xxx */
456− sym = external_sym(TOK___bound_local_delete, func_old_type, 0);
452+ sym = external_global_sym(TOK___bound_local_delete, func_old_type, 0);
457453 greloc(cur_text_section, sym,
458454 ind + 1, R_386_PC32);
459455 oad(0xe8, -4);
@@ -766,7 +762,7 @@ void gen_opf(int op) · 306 unmodified lines
766762 o(0xdc);
767763 else
768764 o(0xd8);
769− gen_modrm(a, r, fc);
765+ gen_modrm(a, r, vtop->sym, fc);
770766 }
771767 vtop--;
772768 }
@@ -816,8 +812,8 @@ void gen_cvt_ftoi(int t) · 43 unmodified lines
816812 size = 4;
817813
818814 o(0x2dd9); /* ldcw xxx */
819− sym = external_sym(TOK___tcc_int_fpu_control,
820− VT_SHORT | VT_UNSIGNED, VT_LVAL);
815+ sym = external_global_sym(TOK___tcc_int_fpu_control,
816+ VT_SHORT | VT_UNSIGNED, VT_LVAL);
821817 greloc(cur_text_section, sym,
822818 ind, R_386_32);
823819 gen_le32(0);
@@ -829,8 +825,8 @@ void gen_cvt_ftoi(int t) · 5 unmodified lines
829825 o(0x3cdf); /* fistpll */
830826 o(0x24);
831827 o(0x2dd9); /* ldcw xxx */
832− sym = external_sym(TOK___tcc_fpu_control,
833− VT_SHORT | VT_UNSIGNED, VT_LVAL);
828+ sym = external_global_sym(TOK___tcc_fpu_control,
829+ VT_SHORT | VT_UNSIGNED, VT_LVAL);
834830 greloc(cur_text_section, sym,
835831 ind, R_386_32);
836832 gen_le32(0);
@@ -871,7 +867,7 @@ void gen_bounded_ptr_add(void) · 34 unmodified lines
871867 vtop -= 2;
872868 save_regs(0);
873869 /* do a fast function call */
874− sym = external_sym(TOK___bound_ptr_add, func_old_type, 0);
870+ sym = external_global_sym(TOK___bound_ptr_add, func_old_type, 0);
875871 greloc(cur_text_section, sym,
876872 ind + 1, R_386_PC32);
877873 oad(0xe8, -4);
@@ -917,7 +913,7 @@ void gen_bounded_ptr_deref(void) · 39 unmodified lines
917913 /* patch relocation */
918914 /* XXX: find a better solution ? */
919915 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul);
920− sym = external_sym(func, func_old_type, 0);
916+ sym = external_global_sym(func, func_old_type, 0);
921917 if (!sym->c)
922918 put_extern_sym(sym, NULL, 0, 0);
923919 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
tcc.c+34−19
@@ -100,7 +100,6 @@ typedef union CValue { · 99 unmodified lines
100100 long long ll;
101101 unsigned long long ull;
102102 struct CString *cstr;
103− struct Sym *sym;
104103 void *ptr;
105104 int tab[1];
106105 } CValue;
@@ -112,6 +111,7 @@ typedef struct SValue { · 5 unmodified lines
112111 unsigned short r2; /* second register, used for 'long long'
113112 type. If not used, set to VT_CONST */
114113 CValue c; /* constant, if VT_CONST */
114+ struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
115115 } SValue;
116116
117117 /* symbol management */
@@ -548,7 +548,7 @@ static char *pstrcat(char *buf, int buf_size, const char *s); · 430 unmodified lines
548548 void sum(int l);
549549 void next(void);
550550 void next_nomacro(void);
551−int expr_const(void);
551+static int expr_const(void);
552552 void expr_eq(void);
553553 void gexpr(void);
554554 void decl(int l);
@@ -2895,8 +2895,9 @@ static void vpush_ref(int t, Section *sec, unsigned long offset, unsigned long s · 2340 unmodified lines
28952895 {
28962896 CValue cval;
28972897
2898− cval.sym = get_sym_ref(t, sec, offset, size);
2898+ cval.ul = 0;
28992899 vsetc(t, VT_CONST | VT_SYM, &cval);
2900+ vtop->sym = get_sym_ref(t, sec, offset, size);
29002901 }
29012902
29022903 /* define a new external reference to a symbol 'v' of type 'u' */
@@ -2934,8 +2935,9 @@ static void vpush_global_sym(int t, int v) · 31 unmodified lines
29342935 CValue cval;
29352936
29362937 sym = external_global_sym(v, t, 0);
2937− cval.sym = sym;
2938+ cval.ul = 0;
29382939 vsetc(t, VT_CONST | VT_SYM, &cval);
2940+ vtop->sym = sym;
29392941 }
29402942
29412943 void vset(int t, int r, int v)
@@ -3160,7 +3162,8 @@ int gv(int rc) · 218 unmodified lines
31603162 ptr[i] = vtop->c.tab[i];
31613163 sym = get_sym_ref(vtop->t, data_section, offset, size << 2);
31623164 vtop->r |= VT_LVAL | VT_SYM;
3163− vtop->c.sym = sym;
3165+ vtop->sym = sym;
3166+ vtop->c.ul = 0;
31643167 }
31653168 #ifdef CONFIG_TCC_BCHECK
31663169 if (vtop->r & VT_MUSTBOUND)
@@ -3675,6 +3678,14 @@ void gen_opic(int op) · 508 unmodified lines
36753678 op = TOK_SHR;
36763679 }
36773680 goto general_case;
3681+ } else if (c2 && (op == '+' || op == '-') &&
3682+ (vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
3683+ (VT_CONST | VT_SYM)) {
3684+ /* symbol + constant case */
3685+ if (op == '-')
3686+ fc = -fc;
3687+ vtop--;
3688+ vtop->c.i += fc;
36783689 } else {
36793690 general_case:
36803691 /* call low level op generator */
@@ -5189,8 +5200,10 @@ void unary(void) · 1508 unmodified lines
51895200 }
51905201 vset(s->t, s->r, s->c);
51915202 /* if forward reference, we must point to s */
5192− if (vtop->r & VT_SYM)
5193− vtop->c.sym = s;
5203+ if (vtop->r & VT_SYM) {
5204+ vtop->sym = s;
5205+ vtop->c.ul = 0;
5206+ }
51945207 }
51955208 }
51965209
@@ -5273,7 +5286,7 @@ void unary(void) · 76 unmodified lines
52735286 tok_str_new(&str);
52745287 parlevel = 0;
52755288 while ((parlevel > 0 || (tok != ')' && tok != ',')) &&
5276− tok != -1) {
5289+ tok != TOK_EOF) {
52775290 if (tok == '(')
52785291 parlevel++;
52795292 else if (tok == ')')
@@ -5509,23 +5522,23 @@ void gexpr(void) · 229 unmodified lines
55095522 }
55105523 }
55115524
5512−/* parse a constant expression and return value in vtop */
5513−void expr_const1(void)
5525+/* parse a constant expression and return value in vtop. */
5526+static void expr_const1(void)
55145527 {
55155528 int a;
55165529 a = const_wanted;
55175530 const_wanted = 1;
55185531 expr_eq();
5519− if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5520− expect("constant");
55215532 const_wanted = a;
55225533 }
55235534
5524−/* parse an integer constant and return its value */
5525−int expr_const(void)
5535+/* parse an integer constant and return its value. */
5536+static int expr_const(void)
55265537 {
55275538 int c;
55285539 expr_const1();
5540+ if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5541+ expect("constant expression");
55295542 c = vtop->c.i;
55305543 vpop();
55315544 return c;
@@ -5899,6 +5912,9 @@ static void init_putv(int t, Section *sec, unsigned long c, · 367 unmodified lines
58995912 global_expr = 1;
59005913 expr_const1();
59015914 global_expr = saved_global_expr;
5915+ /* NOTE: symbols are accepted */
5916+ if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5917+ error("initializer element is not constant");
59025918 break;
59035919 case EXPR_ANY:
59045920 expr_eq();
@@ -5936,11 +5952,9 @@ static void init_putv(int t, Section *sec, unsigned long c, · 31 unmodified lines
59365952 break;
59375953 default:
59385954 if (vtop->r & VT_SYM) {
5939− greloc(sec, vtop->c.sym, c, R_DATA_32);
5940− *(int *)ptr = 0;
5941− } else {
5942− *(int *)ptr = vtop->c.i;
5955+ greloc(sec, vtop->sym, c, R_DATA_32);
59435956 }
5957+ *(int *)ptr = vtop->c.i;
59445958 break;
59455959 }
59465960 vtop--;
@@ -6284,8 +6298,9 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 337 unmodified lines
62846298
62856299 /* push global reference */
62866300 sym = get_sym_ref(t, sec, addr, size);
6287− cval.sym = sym;
6301+ cval.ul = 0;
62886302 vsetc(t, VT_CONST | VT_SYM, &cval);
6303+ vtop->sym = sym;
62896304 }
62906305
62916306 /* handles bounds now because the symbol must be defined

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