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
132#define psym oad132#define psym oad
133 133
134/* instruction + 4 bytes data. Return the address of the data */134/* instruction + 4 bytes data. Return the address of the data */
135int oad(int c, int s)135static int oad(int c, int s)
136{136{
137 int ind1;137 int ind1;
138 138
@@ -147,26 +147,22 @@ int oad(int c, int s) · 8 unmodified lines
147}147}
148 148
149/* output constant with relocation if 'r & VT_SYM' is true */149/* output constant with relocation if 'r & VT_SYM' is true */
150void gen_addr32(int r, int c)150static void gen_addr32(int r, Sym *sym, int c)
151{151{
152 if (!(r & VT_SYM)) {152 if (r & VT_SYM)
153 gen_le32(c);153 greloc(cur_text_section, sym, ind, R_386_32);
154 } else {154 gen_le32(c);
155 greloc(cur_text_section,
156 (Sym *)c, ind, R_386_32);
157 gen_le32(0);
158 }
159}155}
160 156
161/* generate a modrm reference. 'op_reg' contains the addtionnal 3157/* generate a modrm reference. 'op_reg' contains the addtionnal 3
162 opcode bits */158 opcode bits */
163void gen_modrm(int op_reg, int r, int c)159static void gen_modrm(int op_reg, int r, Sym *sym, int c)
164{160{
165 op_reg = op_reg << 3;161 op_reg = op_reg << 3;
166 if ((r & VT_VALMASK) == VT_CONST) {162 if ((r & VT_VALMASK) == VT_CONST) {
167 /* constant memory reference */163 /* constant memory reference */
168 o(0x05 | op_reg);164 o(0x05 | op_reg);
169 gen_addr32(r, c);165 gen_addr32(r, sym, c);
170 } else if ((r & VT_VALMASK) == VT_LOCAL) {166 } else if ((r & VT_VALMASK) == VT_LOCAL) {
171 /* currently, we use only ebp as base */167 /* currently, we use only ebp as base */
172 if (c == (char)c) {168 if (c == (char)c) {
@@ -221,14 +217,14 @@ void load(int r, SValue *sv) · 48 unmodified lines
221 } else {217 } else {
222 o(0x8b); /* movl */218 o(0x8b); /* movl */
223 }219 }
224 gen_modrm(r, fr, fc);220 gen_modrm(r, fr, sv->sym, fc);
225 } else {221 } else {
226 if (v == VT_CONST) {222 if (v == VT_CONST) {
227 o(0xb8 + r); /* mov $xx, r */223 o(0xb8 + r); /* mov $xx, r */
228 gen_addr32(fr, fc);224 gen_addr32(fr, sv->sym, fc);
229 } else if (v == VT_LOCAL) {225 } else if (v == VT_LOCAL) {
230 o(0x8d); /* lea xxx(%ebp), r */226 o(0x8d); /* lea xxx(%ebp), r */
231 gen_modrm(r, VT_LOCAL, fc);227 gen_modrm(r, VT_LOCAL, sv->sym, fc);
232 } else if (v == VT_CMP) {228 } else if (v == VT_CMP) {
233 oad(0xb8 + r, 0); /* mov $0, r */229 oad(0xb8 + r, 0); /* mov $0, r */
234 o(0x0f); /* setxx %br */230 o(0x0f); /* setxx %br */
@@ -278,7 +274,7 @@ void store(int r, SValue *v) · 43 unmodified lines
278 if (fr == VT_CONST ||274 if (fr == VT_CONST ||
279 fr == VT_LOCAL ||275 fr == VT_LOCAL ||
280 (v->r & VT_LVAL)) {276 (v->r & VT_LVAL)) {
281 gen_modrm(r, v->r, fc);277 gen_modrm(r, v->r, v->sym, fc);
282 } else if (fr != r) {278 } else if (fr != r) {
283 o(0xc0 + fr + r * 8); /* mov r, fr */279 o(0xc0 + fr + r * 8); /* mov r, fr */
284 }280 }
@@ -362,9 +358,9 @@ void gfunc_call(GFuncContext *c) · 77 unmodified lines
362 /* constant case */358 /* constant case */
363 if (vtop->r & VT_SYM) {359 if (vtop->r & VT_SYM) {
364 /* relocation case */360 /* relocation case */
365 greloc(cur_text_section, vtop->c.sym, 361 greloc(cur_text_section, vtop->sym,
366 ind + 1, R_386_PC32);362 ind + 1, R_386_PC32);
367 oad(0xe8, -4);363 oad(0xe8, vtop->c.ul - 4);
368 } else {364 } else {
369 oad(0xe8, vtop->c.ul - ind - 5);365 oad(0xe8, vtop->c.ul - ind - 5);
370 }366 }
@@ -443,7 +439,7 @@ void gfunc_epilog(void) · 72 unmodified lines
443 greloc(cur_text_section, sym_data,439 greloc(cur_text_section, sym_data,
444 ind + 1, R_386_32);440 ind + 1, R_386_32);
445 oad(0xb8, 0); /* mov %eax, xxx */441 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);
447 greloc(cur_text_section, sym, 443 greloc(cur_text_section, sym,
448 ind + 1, R_386_PC32);444 ind + 1, R_386_PC32);
449 oad(0xe8, -4);445 oad(0xe8, -4);
@@ -453,7 +449,7 @@ void gfunc_epilog(void) · 3 unmodified lines
453 greloc(cur_text_section, sym_data,449 greloc(cur_text_section, sym_data,
454 ind + 1, R_386_32);450 ind + 1, R_386_32);
455 oad(0xb8, 0); /* mov %eax, xxx */451 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);
457 greloc(cur_text_section, sym, 453 greloc(cur_text_section, sym,
458 ind + 1, R_386_PC32);454 ind + 1, R_386_PC32);
459 oad(0xe8, -4);455 oad(0xe8, -4);
@@ -766,7 +762,7 @@ void gen_opf(int op) · 306 unmodified lines
766 o(0xdc);762 o(0xdc);
767 else763 else
768 o(0xd8);764 o(0xd8);
769 gen_modrm(a, r, fc);765 gen_modrm(a, r, vtop->sym, fc);
770 }766 }
771 vtop--;767 vtop--;
772 }768 }
@@ -816,8 +812,8 @@ void gen_cvt_ftoi(int t) · 43 unmodified lines
816 size = 4;812 size = 4;
817 813
818 o(0x2dd9); /* ldcw xxx */814 o(0x2dd9); /* ldcw xxx */
819 sym = external_sym(TOK___tcc_int_fpu_control, 815 sym = external_global_sym(TOK___tcc_int_fpu_control,
820 VT_SHORT | VT_UNSIGNED, VT_LVAL);816 VT_SHORT | VT_UNSIGNED, VT_LVAL);
821 greloc(cur_text_section, sym, 817 greloc(cur_text_section, sym,
822 ind, R_386_32);818 ind, R_386_32);
823 gen_le32(0);819 gen_le32(0);
@@ -829,8 +825,8 @@ void gen_cvt_ftoi(int t) · 5 unmodified lines
829 o(0x3cdf); /* fistpll */825 o(0x3cdf); /* fistpll */
830 o(0x24);826 o(0x24);
831 o(0x2dd9); /* ldcw xxx */827 o(0x2dd9); /* ldcw xxx */
832 sym = external_sym(TOK___tcc_fpu_control, 828 sym = external_global_sym(TOK___tcc_fpu_control,
833 VT_SHORT | VT_UNSIGNED, VT_LVAL);829 VT_SHORT | VT_UNSIGNED, VT_LVAL);
834 greloc(cur_text_section, sym, 830 greloc(cur_text_section, sym,
835 ind, R_386_32);831 ind, R_386_32);
836 gen_le32(0);832 gen_le32(0);
@@ -871,7 +867,7 @@ void gen_bounded_ptr_add(void) · 34 unmodified lines
871 vtop -= 2;867 vtop -= 2;
872 save_regs(0);868 save_regs(0);
873 /* do a fast function call */869 /* 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);
875 greloc(cur_text_section, sym, 871 greloc(cur_text_section, sym,
876 ind + 1, R_386_PC32);872 ind + 1, R_386_PC32);
877 oad(0xe8, -4);873 oad(0xe8, -4);
@@ -917,7 +913,7 @@ void gen_bounded_ptr_deref(void) · 39 unmodified lines
917 /* patch relocation */913 /* patch relocation */
918 /* XXX: find a better solution ? */914 /* XXX: find a better solution ? */
919 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul);915 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);
921 if (!sym->c)917 if (!sym->c)
922 put_extern_sym(sym, NULL, 0, 0);918 put_extern_sym(sym, NULL, 0, 0);
923 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));919 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
100 long long ll;100 long long ll;
101 unsigned long long ull;101 unsigned long long ull;
102 struct CString *cstr;102 struct CString *cstr;
103 struct Sym *sym;
104 void *ptr;103 void *ptr;
105 int tab[1];104 int tab[1];
106} CValue;105} CValue;
@@ -112,6 +111,7 @@ typedef struct SValue { · 5 unmodified lines
112 unsigned short r2; /* second register, used for 'long long'111 unsigned short r2; /* second register, used for 'long long'
113 type. If not used, set to VT_CONST */112 type. If not used, set to VT_CONST */
114 CValue c; /* constant, if VT_CONST */113 CValue c; /* constant, if VT_CONST */
114 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
115} SValue;115} SValue;
116 116
117/* symbol management */117/* symbol management */
@@ -548,7 +548,7 @@ static char *pstrcat(char *buf, int buf_size, const char *s); · 430 unmodified lines
548void sum(int l);548void sum(int l);
549void next(void);549void next(void);
550void next_nomacro(void);550void next_nomacro(void);
551int expr_const(void);551static int expr_const(void);
552void expr_eq(void);552void expr_eq(void);
553void gexpr(void);553void gexpr(void);
554void decl(int l);554void decl(int l);
@@ -2895,8 +2895,9 @@ static void vpush_ref(int t, Section *sec, unsigned long offset, unsigned long s · 2340 unmodified lines
2895{2895{
2896 CValue cval;2896 CValue cval;
2897 2897
2898 cval.sym = get_sym_ref(t, sec, offset, size);2898 cval.ul = 0;
2899 vsetc(t, VT_CONST | VT_SYM, &cval);2899 vsetc(t, VT_CONST | VT_SYM, &cval);
2900 vtop->sym = get_sym_ref(t, sec, offset, size);
2900}2901}
2901 2902
2902/* define a new external reference to a symbol 'v' of type 'u' */2903/* 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
2934 CValue cval;2935 CValue cval;
2935 2936
2936 sym = external_global_sym(v, t, 0);2937 sym = external_global_sym(v, t, 0);
2937 cval.sym = sym;2938 cval.ul = 0;
2938 vsetc(t, VT_CONST | VT_SYM, &cval);2939 vsetc(t, VT_CONST | VT_SYM, &cval);
2940 vtop->sym = sym;
2939}2941}
2940 2942
2941void vset(int t, int r, int v)2943void vset(int t, int r, int v)
@@ -3160,7 +3162,8 @@ int gv(int rc) · 218 unmodified lines
3160 ptr[i] = vtop->c.tab[i];3162 ptr[i] = vtop->c.tab[i];
3161 sym = get_sym_ref(vtop->t, data_section, offset, size << 2);3163 sym = get_sym_ref(vtop->t, data_section, offset, size << 2);
3162 vtop->r |= VT_LVAL | VT_SYM;3164 vtop->r |= VT_LVAL | VT_SYM;
3163 vtop->c.sym = sym;3165 vtop->sym = sym;
3166 vtop->c.ul = 0;
3164 }3167 }
3165#ifdef CONFIG_TCC_BCHECK3168#ifdef CONFIG_TCC_BCHECK
3166 if (vtop->r & VT_MUSTBOUND) 3169 if (vtop->r & VT_MUSTBOUND)
@@ -3675,6 +3678,14 @@ void gen_opic(int op) · 508 unmodified lines
3675 op = TOK_SHR;3678 op = TOK_SHR;
3676 }3679 }
3677 goto general_case;3680 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;
3678 } else {3689 } else {
3679 general_case:3690 general_case:
3680 /* call low level op generator */3691 /* call low level op generator */
@@ -5189,8 +5200,10 @@ void unary(void) · 1508 unmodified lines
5189 }5200 }
5190 vset(s->t, s->r, s->c);5201 vset(s->t, s->r, s->c);
5191 /* if forward reference, we must point to s */5202 /* if forward reference, we must point to s */
5192 if (vtop->r & VT_SYM)5203 if (vtop->r & VT_SYM) {
5193 vtop->c.sym = s;5204 vtop->sym = s;
5205 vtop->c.ul = 0;
5206 }
5194 }5207 }
5195 }5208 }
5196 5209
@@ -5273,7 +5286,7 @@ void unary(void) · 76 unmodified lines
5273 tok_str_new(&str);5286 tok_str_new(&str);
5274 parlevel = 0;5287 parlevel = 0;
5275 while ((parlevel > 0 || (tok != ')' && tok != ',')) && 5288 while ((parlevel > 0 || (tok != ')' && tok != ',')) &&
5276 tok != -1) {5289 tok != TOK_EOF) {
5277 if (tok == '(')5290 if (tok == '(')
5278 parlevel++;5291 parlevel++;
5279 else if (tok == ')')5292 else if (tok == ')')
@@ -5509,23 +5522,23 @@ void gexpr(void) · 229 unmodified lines
5509 }5522 }
5510}5523}
5511 5524
5512/* parse a constant expression and return value in vtop */5525/* parse a constant expression and return value in vtop. */
5513void expr_const1(void)5526static void expr_const1(void)
5514{5527{
5515 int a;5528 int a;
5516 a = const_wanted;5529 a = const_wanted;
5517 const_wanted = 1;5530 const_wanted = 1;
5518 expr_eq();5531 expr_eq();
5519 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5520 expect("constant");
5521 const_wanted = a;5532 const_wanted = a;
5522}5533}
5523 5534
5524/* parse an integer constant and return its value */5535/* parse an integer constant and return its value. */
5525int expr_const(void)5536static int expr_const(void)
5526{5537{
5527 int c;5538 int c;
5528 expr_const1();5539 expr_const1();
5540 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5541 expect("constant expression");
5529 c = vtop->c.i;5542 c = vtop->c.i;
5530 vpop();5543 vpop();
5531 return c;5544 return c;
@@ -5899,6 +5912,9 @@ static void init_putv(int t, Section *sec, unsigned long c, · 367 unmodified lines
5899 global_expr = 1;5912 global_expr = 1;
5900 expr_const1();5913 expr_const1();
5901 global_expr = saved_global_expr;5914 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");
5902 break;5918 break;
5903 case EXPR_ANY:5919 case EXPR_ANY:
5904 expr_eq();5920 expr_eq();
@@ -5936,11 +5952,9 @@ static void init_putv(int t, Section *sec, unsigned long c, · 31 unmodified lines
5936 break;5952 break;
5937 default:5953 default:
5938 if (vtop->r & VT_SYM) {5954 if (vtop->r & VT_SYM) {
5939 greloc(sec, vtop->c.sym, c, R_DATA_32);5955 greloc(sec, vtop->sym, c, R_DATA_32);
5940 *(int *)ptr = 0;
5941 } else {
5942 *(int *)ptr = vtop->c.i;
5943 }5956 }
5957 *(int *)ptr = vtop->c.i;
5944 break;5958 break;
5945 }5959 }
5946 vtop--;5960 vtop--;
@@ -6284,8 +6298,9 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 337 unmodified lines
6284 6298
6285 /* push global reference */6299 /* push global reference */
6286 sym = get_sym_ref(t, sec, addr, size);6300 sym = get_sym_ref(t, sec, addr, size);
6287 cval.sym = sym;6301 cval.ul = 0;
6288 vsetc(t, VT_CONST | VT_SYM, &cval);6302 vsetc(t, VT_CONST | VT_SYM, &cval);
6303 vtop->sym = sym;
6289 }6304 }
6290 6305
6291 /* handles bounds now because the symbol must be defined6306 /* 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