History

added better relocation support (needed for file output)

bellard committed 24 years ago

Commit 6cd36b11 parent 7ab1ac42 files · +462 −297Browse files

Unified · Split

2 files
  1. i386-gen.c +24 −19
  2. tcc.c +438 −278
i386-gen.c+24−19
@@ -1,7 +1,7 @@
11 /*
22 * X86 code generator for TCC
33 *
4− * Copyright (c) 2001 Fabrice Bellard
4+ * Copyright (c) 2001, 2002 Fabrice Bellard
55 *
66 * This program is free software; you can redistribute it and/or modify
77 * it under the terms of the GNU General Public License as published by
@@ -68,6 +68,9 @@ int reg_classes[NB_REGS] = { · 60 unmodified lines
6868 #define LDOUBLE_SIZE 12
6969 #define LDOUBLE_ALIGN 4
7070
71+/* relocation type for 32 bit data relocation */
72+#define R_DATA_32 R_386_32
73+
7174 /* function call context */
7275 typedef struct GFuncContext {
7376 int args_size;
@@ -101,21 +104,21 @@ void gen_le32(int c) · 27 unmodified lines
101104 g(c >> 24);
102105 }
103106
104−/* patch relocation entry with value 'val' */
105−void greloc_patch1(Reloc *p, int val)
107+void greloc_patch(unsigned char *ptr,
108+ unsigned long addr, unsigned long val, int type)
106109 {
107− switch(p->type) {
108− case RELOC_ADDR32:
109− *(int *)p->addr = val;
110+ switch(type) {
111+ case R_386_32:
112+ *(int *)ptr += val;
110113 break;
111− case RELOC_REL32:
112− *(int *)p->addr = val - p->addr - 4;
114+ case R_386_PC32:
115+ *(int *)ptr += val - addr - 4;
113116 break;
114117 }
115118 }
116119
117120 /* output a symbol and patch all calls to it */
118−void gsym_addr(t, a)
121+void gsym_addr(int t, int a)
119122 {
120123 int n;
121124 while (t) {
@@ -125,7 +128,7 @@ void gsym_addr(t, a) · 3 unmodified lines
125128 }
126129 }
127130
128−void gsym(t)
131+void gsym(int t)
129132 {
130133 gsym_addr(t, ind);
131134 }
@@ -144,13 +147,14 @@ int oad(int c, int s) · 12 unmodified lines
144147 return s;
145148 }
146149
147−/* output constant with relocation if 'r & VT_FORWARD' is true */
150+/* output constant with relocation if 'r & VT_SYM' is true */
148151 void gen_addr32(int r, int c)
149152 {
150− if (!(r & VT_FORWARD)) {
153+ if (!(r & VT_SYM)) {
151154 gen_le32(c);
152155 } else {
153− greloc((Sym *)c, ind, RELOC_ADDR32);
156+ greloc(cur_text_section,
157+ (Sym *)c, ind - (int)cur_text_section->data, R_386_32);
154158 gen_le32(0);
155159 }
156160 }
@@ -347,9 +351,10 @@ void gfunc_call(GFuncContext *c) · 190 unmodified lines
347351 int r;
348352 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
349353 /* constant case */
350− /* forward reference */
351− if (vtop->r & VT_FORWARD) {
352− greloc(vtop->c.sym, ind + 1, RELOC_REL32);
354+ if (vtop->r & VT_SYM) {
355+ /* relocation case */
356+ greloc(cur_text_section, vtop->c.sym,
357+ ind + 1 - (int)cur_text_section->data, R_386_PC32);
353358 oad(0xe8, 0);
354359 } else {
355360 oad(0xe8, vtop->c.ul - ind - 5);
@@ -485,7 +490,7 @@ int gtst(int inv, int t) · 129 unmodified lines
485490 vpushi(0);
486491 gen_op(TOK_NE);
487492 }
488− if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
493+ if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
489494 /* constant jmp optimization */
490495 if ((vtop->c.i != 0) != inv)
491496 t = gjmp(t);
@@ -511,7 +516,7 @@ void gen_opi(int op) · 19 unmodified lines
511516 case TOK_ADDC1: /* add with carry generation */
512517 opc = 0;
513518 gen_op8:
514− if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
519+ if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
515520 /* constant case */
516521 vswap();
517522 r = gv(RC_INT);
@@ -576,7 +581,7 @@ void gen_opi(int op) · 58 unmodified lines
576581 opc = 7;
577582 gen_shift:
578583 opc = 0xc0 | (opc << 3);
579− if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
584+ if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
580585 /* constant case */
581586 vswap();
582587 r = gv(RC_INT);
tcc.c+438−278
@@ -74,6 +74,7 @@ · 73 unmodified lines
7474 #define TOK_HASH_SIZE 2048 /* must be a power of two */
7575 #define TOK_ALLOC_INCR 512 /* must be a power of two */
7676 #define SYM_HASH_SIZE 1031
77+#define ELF_SYM_HASH_SIZE 2048 /* must be a power of two */
7778
7879 /* token symbol management */
7980 typedef struct TokenSym {
@@ -104,7 +105,7 @@ typedef struct SValue { · 24 unmodified lines
104105 unsigned short r; /* register + flags */
105106 unsigned short r2; /* second register, used for 'long long'
106107 type. If not used, set to VT_CONST */
107− CValue c; /* constant */
108+ CValue c; /* constant, if VT_CONST */
108109 } SValue;
109110
110111 /* symbol management */
@@ -123,16 +124,6 @@ typedef struct SymStack { · 12 unmodified lines
123124 struct Sym *hash[SYM_HASH_SIZE];
124125 } SymStack;
125126
126−/* relocation entry (currently only used for functions or variables */
127−typedef struct Reloc {
128− int type; /* type of relocation */
129− int addr; /* address of relocation */
130− struct Reloc *next; /* next relocation */
131−} Reloc;
132−
133−#define RELOC_ADDR32 1 /* 32 bits relocation */
134−#define RELOC_REL32 2 /* 32 bits relative relocation */
135−
136127 /* section definition */
137128 typedef struct Section {
138129 unsigned char *data; /* section data */
@@ -140,8 +131,16 @@ typedef struct Section { · 1 unmodified line
140131 int sh_num; /* elf section number */
141132 int sh_type; /* elf section type */
142133 int sh_flags; /* elf section flags */
134+/* special flag to indicate that the section should not be linked to
135+ the other ones */
136+#define SHF_PRIVATE 0x80000000
143137 int sh_entsize; /* elf entry size */
138+ unsigned long addr; /* address at which the section is relocated */
144139 struct Section *link; /* link to another section */
140+ struct Section *reloc; /* corresponding section for relocation, if any */
141+ struct Section *reloc_sec;/* relocation: pointer to corresponding
142+ data section */
143+ struct Section *hash; /* hash table for symbols */
145144 struct Section *next;
146145 char name[64]; /* section name */
147146 } Section;
@@ -170,6 +169,9 @@ typedef struct AttributeDef { · 22 unmodified lines
170169 #define MACRO_OBJ 0 /* object like macro */
171170 #define MACRO_FUNC 1 /* function like macro */
172171
172+/* field 'Sym.t' for labels */
173+#define LABEL_FORWARD 1 /* label is forward defined */
174+
173175 /* type_decl() types */
174176 #define TYPE_ABSTRACT 1 /* type without variable */
175177 #define TYPE_DIRECT 2 /* type with variable */
@@ -210,16 +212,21 @@ int ch, ch1, tok, tok1; · 34 unmodified lines
210212 CValue tokc, tok1c;
211213
212214 /* sections */
213−Section *first_section;
214−int section_num;
215+/* XXX: suppress first_section */
216+Section *first_section, **sections;
217+int nb_sections; /* number of sections, including first dummy section */
218+int nb_allocated_sections;
215219 Section *text_section, *data_section, *bss_section; /* predefined sections */
216220 Section *cur_text_section; /* current section where function code is
217221 generated */
218222 /* bound check related sections */
219223 Section *bounds_section; /* contains global data bound description */
220224 Section *lbounds_section; /* contains local data bound description */
225+/* symbol sections */
226+Section *symtab_section, *strtab_section, *hashtab_section;
227+
221228 /* debug sections */
222−Section *stab_section, *stabstr_section, *symtab_section, *strtab_section;
229+Section *stab_section, *stabstr_section;
223230
224231 /* loc : local variable index
225232 ind : output code index
@@ -238,8 +245,6 @@ TokenSym **table_ident; · 12 unmodified lines
238245 TokenSym *hash_ident[TOK_HASH_SIZE];
239246 char token_buf[STRING_MAX_SIZE + 1];
240247 char *funcname;
241−/* contains global symbols which remain between each translation unit */
242−SymStack extern_stack;
243248 SymStack define_stack, global_stack, local_stack, label_stack;
244249
245250 SValue vstack[VSTACK_SIZE], *vtop;
@@ -281,7 +286,7 @@ struct TCCState { · 35 unmodified lines
281286 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
282287 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
283288 #define VT_LVAL 0x0100 /* var is an lvalue */
284−#define VT_FORWARD 0x0200 /* value is forward reference */
289+#define VT_SYM 0x0200 /* a symbol value is added */
285290 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
286291 char/short stored in integer registers) */
287292 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
@@ -519,9 +524,9 @@ int expr_const(void); · 231 unmodified lines
519524 void expr_eq(void);
520525 void gexpr(void);
521526 void decl(int l);
522−void decl_initializer(int t, int r, int c, int first, int size_only);
523−int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init,
524− int v, int scope);
527+void decl_initializer(int t, Section *sec, unsigned long c, int first, int size_only);
528+void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init,
529+ int v, int scope);
525530 int gv(int rc);
526531 void gv2(int rc1, int rc2);
527532 void move_reg(int r, int s);
@@ -556,13 +561,17 @@ void vpushi(int v); · 28 unmodified lines
556561 void vset(int t, int r, int v);
557562 void type_to_str(char *buf, int buf_size,
558563 int t, const char *varstr);
564+char *get_tok_str(int v, CValue *cv);
559565
560566 /* section generation */
561−void greloc(Sym *s, int addr, int type);
567+void put_extern_sym(Sym *sym, Section *section, unsigned long value);
568+void greloc(Section *s, Sym *sym, unsigned long addr, int type);
562569 static int put_elf_str(Section *s, const char *sym);
563−static void put_elf_sym(Section *s,
564− unsigned long value, unsigned long size,
565− int info, int other, int shndx, const char *name);
570+static int put_elf_sym(Section *s,
571+ unsigned long value, unsigned long size,
572+ int info, int other, int shndx, const char *name);
573+static void put_elf_reloc(Section *s, unsigned long offset,
574+ int type, int symbol);
566575 static void put_stabs(const char *str, int type, int other, int desc, int value);
567576 static void put_stabn(int type, int other, int desc, int value);
568577 static void put_stabd(int type, int other, int desc);
@@ -755,8 +764,6 @@ Section *new_section(const char *name, int sh_type, int sh_flags) · 186 unmodified lines
755764 error("memory full");
756765 memset(sec, 0, sizeof(Section));
757766 pstrcpy(sec->name, sizeof(sec->name), name);
758− sec->link = NULL;
759− sec->sh_num = ++section_num;
760767 sec->sh_type = sh_type;
761768 sec->sh_flags = sh_flags;
762769 #ifdef WIN32
@@ -774,11 +781,29 @@ Section *new_section(const char *name, int sh_type, int sh_flags) · 11 unmodified lines
774781 #endif
775782 sec->data = data;
776783 sec->data_ptr = data;
777− psec = &first_section;
778− while (*psec != NULL)
779− psec = &(*psec)->next;
780− sec->next = NULL;
781− *psec = sec;
784+
785+ if (!(sh_flags & SHF_PRIVATE)) {
786+ /* only add section if not private */
787+
788+ psec = &first_section;
789+ while (*psec != NULL)
790+ psec = &(*psec)->next;
791+ sec->next = NULL;
792+ *psec = sec;
793+
794+ if ((nb_sections + 1) > nb_allocated_sections) {
795+ if (nb_allocated_sections == 0)
796+ nb_allocated_sections = 1;
797+ nb_allocated_sections *= 2;
798+ sections = realloc(sections,
799+ nb_allocated_sections * sizeof(Section *));
800+ if (!sections)
801+ error("memory full");
802+ }
803+ sections[nb_sections] = sec;
804+ sec->sh_num = nb_sections;
805+ nb_sections++;
806+ }
782807 return sec;
783808 }
784809
@@ -796,36 +821,15 @@ Section *find_section(const char *name) · 11 unmodified lines
796821 return new_section(name, SHT_PROGBITS, SHF_ALLOC);
797822 }
798823
799−/* add a new relocation entry to symbol 's' */
800−void greloc(Sym *s, int addr, int type)
801−{
802− Reloc *p;
803− p = malloc(sizeof(Reloc));
804− if (!p)
805− error("memory full");
806− p->type = type;
807− p->addr = addr;
808− p->next = (Reloc *)s->c;
809− s->c = (int)p;
810−}
811−
812−/* patch each relocation entry with value 'val' */
813−void greloc_patch(Sym *s, int val)
824+/* add a new relocation entry to symbol 'sym' in section 's' */
825+void greloc(Section *s, Sym *sym, unsigned long offset, int type)
814826 {
815− Reloc *p, *p1;
816−
817− p = (Reloc *)s->c;
818− while (p != NULL) {
819− p1 = p->next;
820− greloc_patch1(p, val);
821− free(p);
822− p = p1;
823− }
824− s->c = val;
825− s->r &= ~VT_FORWARD;
827+ if (!sym->c)
828+ put_extern_sym(sym, NULL, 0);
829+ /* now we can add ELF relocation info */
830+ put_elf_reloc(s, offset, type, sym->c);
826831 }
827832
828−
829833 static inline int isid(int c)
830834 {
831835 return (c >= 'a' && c <= 'z') ||
@@ -2502,6 +2506,21 @@ void vpushi(int v) · 1670 unmodified lines
25022506 vsetc(VT_INT, VT_CONST, &cval);
25032507 }
25042508
2509+/* push a reference to a section offset by adding a dummy symbol */
2510+void vpush_ref(int t, Section *sec, unsigned long offset)
2511+{
2512+ int v;
2513+ Sym *sym;
2514+ CValue cval;
2515+
2516+ v = anon_sym++;
2517+ sym = sym_push1(&global_stack, v, t | VT_STATIC, 0);
2518+ sym->r = VT_CONST | VT_SYM;
2519+ put_extern_sym(sym, sec, offset);
2520+ cval.sym = sym;
2521+ vsetc(t, VT_CONST | VT_SYM, &cval);
2522+}
2523+
25052524 void vset(int t, int r, int v)
25062525 {
25072526 CValue cval;
@@ -3016,7 +3035,7 @@ void gen_opl(int op) · 508 unmodified lines
30163035 case TOK_SAR:
30173036 case TOK_SHR:
30183037 case TOK_SHL:
3019− if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
3038+ if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
30203039 t = vtop[-1].t;
30213040 vswap();
30223041 lexpand();
@@ -3142,8 +3161,8 @@ void gen_opic(int op) · 119 unmodified lines
31423161 v1 = vtop - 1;
31433162 v2 = vtop;
31443163 /* currently, we cannot do computations with forward symbols */
3145− c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
3146− c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
3164+ c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3165+ c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
31473166 if (c1 && c2) {
31483167 fc = v2->c.i;
31493168 switch(op) {
@@ -3246,8 +3265,8 @@ void gen_opif(int op) · 96 unmodified lines
32463265 v1 = vtop - 1;
32473266 v2 = vtop;
32483267 /* currently, we cannot do computations with forward symbols */
3249− c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
3250− c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
3268+ c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3269+ c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
32513270 if (c1 && c2) {
32523271 if (v1->t == VT_FLOAT) {
32533272 f1 = v1->c.f;
@@ -3544,7 +3563,7 @@ void gen_cast(int t) · 290 unmodified lines
35443563 if (sbt != dbt) {
35453564 sf = is_float(sbt);
35463565 df = is_float(dbt);
3547− c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
3566+ c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
35483567 if (sf && df) {
35493568 /* convert from fp to fp */
35503569 if (c) {
@@ -3898,7 +3917,7 @@ void gen_assign_cast(int dt) · 347 unmodified lines
38983917 }
38993918 /* '0' can also be a pointer */
39003919 if ((st & VT_BTYPE) == VT_INT &&
3901− ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) &&
3920+ ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) &&
39023921 vtop->c.i == 0)
39033922 goto type_ok;
39043923 }
@@ -4531,16 +4550,17 @@ int type_decl(AttributeDef *ad, int *v, int t, int td) · 626 unmodified lines
45314550 return u;
45324551 }
45334552
4534−/* define a new external reference to a function 'v' of type 'u' */
4553+/* define a new external reference to a symbol 'v' of type 'u' */
45354554 Sym *external_sym(int v, int u, int r)
45364555 {
45374556 Sym *s;
4557+
45384558 s = sym_find(v);
45394559 if (!s) {
45404560 /* push forward reference */
45414561 s = sym_push1(&global_stack,
4542− v, u, 0);
4543− s->r = r | VT_CONST | VT_FORWARD;
4562+ v, u | VT_EXTERN, 0);
4563+ s->r = r | VT_CONST | VT_SYM;
45444564 }
45454565 return s;
45464566 }
@@ -4628,11 +4648,11 @@ void unary(void) · 81 unmodified lines
46284648 } else if (tok == TOK___FUNC__) {
46294649 /* special function name identifier */
46304650 /* generate (char *) type */
4631− data_offset = (int)data_section->data_ptr;
4632− vset(mk_pointer(VT_BYTE), VT_CONST, data_offset);
4633− strcpy((void *)data_offset, funcname);
4651+ data_offset = data_section->data_ptr - data_section->data;
4652+ vpush_ref(mk_pointer(VT_BYTE), data_section, data_offset);
4653+ strcpy(data_section->data + data_offset, funcname);
46344654 data_offset += strlen(funcname) + 1;
4635− data_section->data_ptr = (unsigned char *)data_offset;
4655+ data_section->data_ptr = data_section->data + data_offset;
46364656 next();
46374657 } else if (tok == TOK_LSTR) {
46384658 t = VT_INT;
@@ -4642,16 +4662,16 @@ void unary(void) · 3 unmodified lines
46424662 t = VT_BYTE;
46434663 str_init:
46444664 type_size(t, &align);
4645− data_offset = (int)data_section->data_ptr;
4665+ data_offset = data_section->data_ptr - data_section->data;
46464666 data_offset = (data_offset + align - 1) & -align;
46474667 fc = data_offset;
46484668 /* we must declare it as an array first to use initializer parser */
46494669 t = VT_ARRAY | mk_pointer(t);
4650− decl_initializer(t, VT_CONST, data_offset, 1, 0);
4670+ decl_initializer(t, data_section, data_offset, 1, 0);
46514671 data_offset += type_size(t, &align);
46524672 /* put it as pointer */
4653− vset(t & ~VT_ARRAY, VT_CONST, fc);
4654− data_section->data_ptr = (unsigned char *)data_offset;
4673+ vpush_ref(t & ~VT_ARRAY, data_section, fc);
4674+ data_section->data_ptr = data_section->data + data_offset;
46554675 } else {
46564676 t = tok;
46574677 next();
@@ -4671,8 +4691,7 @@ void unary(void) · 13 unmodified lines
46714691 if (!(ft & VT_ARRAY))
46724692 r |= lvalue_type(ft);
46734693 memset(&ad, 0, sizeof(AttributeDef));
4674− fc = decl_initializer_alloc(ft, &ad, r, 1, 0, 0);
4675− vset(ft, r, fc);
4694+ decl_initializer_alloc(ft, &ad, r, 1, 0, 0);
46764695 } else {
46774696 unary();
46784697 gen_cast(ft);
@@ -4751,14 +4770,14 @@ void unary(void) · 72 unmodified lines
47514770 error("'%s' undeclared", get_tok_str(t, NULL));
47524771 /* for simple function calls, we tolerate undeclared
47534772 external reference */
4754− p = anon_sym++;
4773+ p = anon_sym++;
47554774 sym_push1(&global_stack, p, 0, FUNC_OLD);
47564775 /* int() function */
47574776 s = external_sym(t, VT_FUNC | (p << VT_STRUCT_SHIFT), 0);
47584777 }
47594778 vset(s->t, s->r, s->c);
47604779 /* if forward reference, we must point to s */
4761− if (vtop->r & VT_FORWARD)
4780+ if (vtop->r & VT_SYM)
47624781 vtop->c.sym = s;
47634782 }
47644783 }
@@ -5333,9 +5352,9 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 568 unmodified lines
53335352 s = sym_find1(&label_stack, tok);
53345353 /* put forward definition if needed */
53355354 if (!s)
5336− s = sym_push1(&label_stack, tok, VT_FORWARD, 0);
5355+ s = sym_push1(&label_stack, tok, LABEL_FORWARD, 0);
53375356 /* label already defined */
5338− if (s->t & VT_FORWARD)
5357+ if (s->t & LABEL_FORWARD)
53395358 s->c = gjmp(s->c);
53405359 else
53415360 gjmp_addr(s->c);
@@ -5347,7 +5366,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 5 unmodified lines
53475366 /* label case */
53485367 s = sym_find1(&label_stack, b);
53495368 if (s) {
5350− if (!(s->t & VT_FORWARD))
5369+ if (!(s->t & LABEL_FORWARD))
53515370 error("multiple defined label");
53525371 gsym(s->c);
53535372 s->c = ind;
@@ -5375,7 +5394,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 21 unmodified lines
53755394 address. cur_index/cur_field is the pointer to the current
53765395 value. 'size_only' is true if only size info is needed (only used
53775396 in arrays) */
5378−void decl_designator(int t, int r, int c,
5397+void decl_designator(int t, Section *sec, unsigned long c,
53795398 int *cur_index, Sym **cur_field,
53805399 int size_only)
53815400 {
@@ -5444,7 +5463,7 @@ void decl_designator(int t, int r, int c, · 62 unmodified lines
54445463 c += f->c;
54455464 }
54465465 }
5447− decl_initializer(t, r, c, 0, size_only);
5466+ decl_initializer(t, sec, c, 0, size_only);
54485467 }
54495468
54505469 #define EXPR_VAL 0
@@ -5452,10 +5471,11 @@ void decl_designator(int t, int r, int c, · 1 unmodified line
54525471 #define EXPR_ANY 2
54535472
54545473 /* store a value or an expression directly in global data or in local array */
5455−void init_putv(int t, int r, int c,
5474+void init_putv(int t, Section *sec, unsigned long c,
54565475 int v, int expr_type)
54575476 {
54585477 int saved_global_expr, bt;
5478+ void *ptr;
54595479
54605480 switch(expr_type) {
54615481 case EXPR_VAL:
@@ -5473,33 +5493,47 @@ void init_putv(int t, int r, int c, · 11 unmodified lines
54735493 break;
54745494 }
54755495
5476− if ((r & VT_VALMASK) == VT_CONST) {
5496+ if (sec) {
54775497 /* XXX: not portable */
5498+ /* XXX: generate error if incorrect relocation */
54785499 gen_assign_cast(t);
54795500 bt = t & VT_BTYPE;
5501+ ptr = sec->data + c;
5502+ if ((vtop->r & VT_SYM) &&
5503+ (bt == VT_BYTE ||
5504+ bt == VT_SHORT ||
5505+ bt == VT_DOUBLE ||
5506+ bt == VT_LDOUBLE ||
5507+ bt == VT_LLONG))
5508+ error("initializer element is not computable at load time");
54805509 switch(bt) {
54815510 case VT_BYTE:
5482− *(char *)c = vtop->c.i;
5511+ *(char *)ptr = vtop->c.i;
54835512 break;
54845513 case VT_SHORT:
5485− *(short *)c = vtop->c.i;
5514+ *(short *)ptr = vtop->c.i;
54865515 break;
54875516 case VT_DOUBLE:
5488− *(double *)c = vtop->c.d;
5517+ *(double *)ptr = vtop->c.d;
54895518 break;
54905519 case VT_LDOUBLE:
5491− *(long double *)c = vtop->c.ld;
5520+ *(long double *)ptr = vtop->c.ld;
54925521 break;
54935522 case VT_LLONG:
5494− *(long long *)c = vtop->c.ll;
5523+ *(long long *)ptr = vtop->c.ll;
54955524 break;
54965525 default:
5497− *(int *)c = vtop->c.i;
5526+ if (vtop->r & VT_SYM) {
5527+ greloc(sec, vtop->c.sym, c, R_DATA_32);
5528+ *(int *)ptr = 0;
5529+ } else {
5530+ *(int *)ptr = vtop->c.i;
5531+ }
54985532 break;
54995533 }
55005534 vtop--;
55015535 } else {
5502− vset(t, r, c);
5536+ vset(t, VT_LOCAL, c);
55035537 vswap();
55045538 vstore();
55055539 vpop();
@@ -5507,11 +5541,11 @@ void init_putv(int t, int r, int c, · 1 unmodified line
55075541 }
55085542
55095543 /* put zeros for variable based init */
5510−void init_putz(int t, int r, int c, int size)
5544+void init_putz(int t, Section *sec, unsigned long c, int size)
55115545 {
55125546 GFuncContext gf;
55135547
5514− if ((r & VT_VALMASK) == VT_CONST) {
5548+ if (sec) {
55155549 /* nothing to do because globals are already set to zero */
55165550 } else {
55175551 gfunc_start(&gf, FUNC_CDECL);
@@ -5526,11 +5560,12 @@ void init_putz(int t, int r, int c, int size) · 8 unmodified lines
55265560 }
55275561 }
55285562
5529−/* 't' contains the type and storage info. c is the address of the
5530− object. 'first' is true if array '{' must be read (multi dimension
5531− implicit array init handling). 'size_only' is true if size only
5532− evaluation is wanted (only for arrays). */
5533−void decl_initializer(int t, int r, int c, int first, int size_only)
5563+/* 't' contains the type and storage info. 'c' is the offset of the
5564+ object in section 'sec'. If 'sec' is NULL, it means stack based
5565+ allocation. 'first' is true if array '{' must be read (multi
5566+ dimension implicit array init handling). 'size_only' is true if
5567+ size only evaluation is wanted (only for arrays). */
5568+void decl_initializer(int t, Section *sec, unsigned long c, int first, int size_only)
55345569 {
55355570 int index, array_length, n, no_oblock, nb, parlevel, i;
55365571 int t1, size1, align1, expr_type;
@@ -5568,7 +5603,7 @@ void decl_initializer(int t, int r, int c, int first, int size_only) · 31 unmodified lines
55685603 if (ts->len > nb)
55695604 warning("initializer-string for array is too long");
55705605 for(i=0;i<nb;i++) {
5571− init_putv(t1, r, c + (array_length + i) * size1,
5606+ init_putv(t1, sec, c + (array_length + i) * size1,
55725607 ts->str[i], EXPR_VAL);
55735608 }
55745609 }
@@ -5579,20 +5614,20 @@ void decl_initializer(int t, int r, int c, int first, int size_only) · 4 unmodified lines
55795614 warning in this case since it is standard) */
55805615 if (n < 0 || array_length < n) {
55815616 if (!size_only) {
5582− init_putv(t1, r, c + (array_length * size1), 0, EXPR_VAL);
5617+ init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
55835618 }
55845619 array_length++;
55855620 }
55865621 } else {
55875622 index = 0;
55885623 while (tok != '}') {
5589− decl_designator(t, r, c, &index, NULL, size_only);
5624+ decl_designator(t, sec, c, &index, NULL, size_only);
55905625 if (n >= 0 && index >= n)
55915626 error("index too large");
55925627 /* must put zero in holes (note that doing it that way
55935628 ensures that it even works with designators) */
55945629 if (!size_only && array_length < index) {
5595− init_putz(t1, r, c + array_length * size1,
5630+ init_putz(t1, sec, c + array_length * size1,
55965631 (index - array_length) * size1);
55975632 }
55985633 index++;
@@ -5612,7 +5647,7 @@ void decl_initializer(int t, int r, int c, int first, int size_only) · 13 unmodified lines
56125647 skip('}');
56135648 /* put zeros at the end */
56145649 if (!size_only && n >= 0 && array_length < n) {
5615− init_putz(t1, r, c + array_length * size1,
5650+ init_putz(t1, sec, c + array_length * size1,
56165651 (n - array_length) * size1);
56175652 }
56185653 /* patch type size if needed */
@@ -5627,11 +5662,11 @@ void decl_initializer(int t, int r, int c, int first, int size_only) · 8 unmodified lines
56275662 index = 0;
56285663 n = s->c;
56295664 while (tok != '}') {
5630− decl_designator(t, r, c, NULL, &f, size_only);
5665+ decl_designator(t, sec, c, NULL, &f, size_only);
56315666 /* fill with zero between fields */
56325667 index = f->c;
56335668 if (!size_only && array_length < index) {
5634− init_putz(t, r, c + array_length,
5669+ init_putz(t, sec, c + array_length,
56355670 index - array_length);
56365671 }
56375672 index = index + type_size(f->t, &align1);
@@ -5644,13 +5679,13 @@ void decl_initializer(int t, int r, int c, int first, int size_only) · 6 unmodified lines
56445679 }
56455680 /* put zeros at the end */
56465681 if (!size_only && array_length < n) {
5647− init_putz(t, r, c + array_length,
5682+ init_putz(t, sec, c + array_length,
56485683 n - array_length);
56495684 }
56505685 skip('}');
56515686 } else if (tok == '{') {
56525687 next();
5653− decl_initializer(t, r, c, first, size_only);
5688+ decl_initializer(t, sec, c, first, size_only);
56545689 skip('}');
56555690 } else if (size_only) {
56565691 /* just skip expression */
@@ -5667,19 +5702,20 @@ void decl_initializer(int t, int r, int c, int first, int size_only) · 10 unmodified lines
56675702 /* currently, we always use constant expression for globals
56685703 (may change for scripting case) */
56695704 expr_type = EXPR_CONST;
5670− if ((r & VT_VALMASK) == VT_LOCAL)
5705+ if (!sec)
56715706 expr_type = EXPR_ANY;
5672− init_putv(t, r, c, 0, expr_type);
5707+ init_putv(t, sec, c, 0, expr_type);
56735708 }
56745709 }
56755710
56765711 /* parse an initializer for type 't' if 'has_init' is true, and
56775712 allocate space in local or global data space ('r' is either
5678− VT_LOCAL or VT_CONST). The allocated address in returned. If 'v' is
5679− non zero, then an associated variable 'v' of scope 'scope' is
5680− declared before initializers are parsed. */
5681−int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init,
5682− int v, int scope)
5713+ VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5714+ variable 'v' of scope 'scope' is declared before initializers are
5715+ parsed. If 'v' is zero, then a reference to the new object is put
5716+ in the value stack. */
5717+void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init,
5718+ int v, int scope)
56835719 {
56845720 int size, align, addr, data_offset;
56855721 int level;
@@ -5721,7 +5757,7 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 35 unmodified lines
57215757
57225758 macro_ptr = init_str.str;
57235759 next();
5724− decl_initializer(t, r, 0, 1, 1);
5760+ decl_initializer(t, NULL, 0, 1, 1);
57255761 /* prepare second initializer parsing */
57265762 macro_ptr = init_str.str;
57275763 next();
@@ -5735,6 +5771,7 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 7 unmodified lines
57355771 if (ad->aligned > align)
57365772 align = ad->aligned;
57375773 if ((r & VT_VALMASK) == VT_LOCAL) {
5774+ sec = NULL;
57385775 if (do_bounds_check && (t & VT_ARRAY))
57395776 loc--;
57405777 #ifdef TCC_TARGET_IL
@@ -5768,13 +5805,11 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 27 unmodified lines
57685805 else
57695806 sec = bss_section;
57705807 }
5771− data_offset = (int)sec->data_ptr;
5808+ data_offset = sec->data_ptr - sec->data;
57725809 data_offset = (data_offset + align - 1) & -align;
57735810 addr = data_offset;
5774− /* very important to increment global
5775− pointer at this time because
5776− initializers themselves can create new
5777− initializers */
5811+ /* very important to increment global pointer at this time
5812+ because initializers themselves can create new initializers */
57785813 data_offset += size;
57795814 /* handles bounds */
57805815 if (do_bounds_check) {
@@ -5783,39 +5818,54 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 2 unmodified lines
57835818 data_offset++;
57845819 /* then add global bound info */
57855820 bounds_ptr = (int *)bounds_section->data_ptr;
5786− *bounds_ptr++ = addr;
5821+ /* XXX: add relocation */
5822+ *bounds_ptr++ = addr + (unsigned long)sec->data;
57875823 *bounds_ptr++ = size;
57885824 bounds_section->data_ptr = (unsigned char *)bounds_ptr;
57895825 }
5790− sec->data_ptr = (unsigned char *)data_offset;
5826+ sec->data_ptr = sec->data + data_offset;
57915827 }
57925828 if (v) {
57935829 Sym *sym;
5794− if (scope == VT_CONST) {
5795− /* global scope: see if already defined */
5796− sym = sym_find(v);
5797− if (!sym)
5798− goto do_def;
5799− if (!is_compatible_types(sym->t, t))
5800− error("incompatible types for redefinition of '%s'",
5801− get_tok_str(v, NULL));
5802− if (!(sym->r & VT_FORWARD))
5803− error("redefinition of '%s'", get_tok_str(v, NULL));
5804− greloc_patch(sym, addr);
5805− } else {
5806− do_def:
5830+
5831+ if (!sec) {
5832+ /* local variable */
58075833 sym_push(v, t, r, addr);
5834+ } else {
5835+ if (scope == VT_CONST) {
5836+ /* global scope: see if already defined */
5837+ sym = sym_find(v);
5838+ if (!sym)
5839+ goto do_def;
5840+ if (!is_compatible_types(sym->t, t))
5841+ error("incompatible types for redefinition of '%s'",
5842+ get_tok_str(v, NULL));
5843+ if (!(sym->t & VT_EXTERN))
5844+ error("redefinition of '%s'", get_tok_str(v, NULL));
5845+ sym->t &= ~VT_EXTERN;
5846+ } else {
5847+ do_def:
5848+ sym = sym_push(v, t, r | VT_SYM, 0);
5849+ }
5850+ put_extern_sym(sym, sec, addr);
5851+ }
5852+ } else {
5853+ if (!sec) {
5854+ /* push local reference */
5855+ vset(t, r, addr);
5856+ } else {
5857+ /* push global reference */
5858+ vpush_ref(t, sec, addr);
58085859 }
58095860 }
58105861 if (has_init) {
5811− decl_initializer(t, r, addr, 1, 0);
5862+ decl_initializer(t, sec, addr, 1, 0);
58125863 /* restore parse state if needed */
58135864 if (init_str.str) {
58145865 free(init_str.str);
58155866 restore_parse_state(&saved_parse_state);
58165867 }
58175868 }
5818− return addr;
58195869 }
58205870
58215871 void put_func_debug(int t)
@@ -5894,7 +5944,7 @@ void analyse_function(void) · 72 unmodified lines
58945944 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
58955945 void decl(int l)
58965946 {
5897− int t, b, v, has_init, r, bind;
5947+ int t, b, v, has_init, r;
58985948 Sym *sym;
58995949 AttributeDef ad;
59005950
@@ -5976,25 +6026,18 @@ void decl(int l) · 75 unmodified lines
59766026 if (!cur_text_section)
59776027 cur_text_section = text_section;
59786028 ind = (int)cur_text_section->data_ptr;
5979− /* patch forward references */
5980− if ((sym = sym_find(v)) && (sym->r & VT_FORWARD)) {
5981− greloc_patch(sym, ind);
6029+ funcname = get_tok_str(v, NULL);
6030+ sym = sym_find(v);
6031+ if (sym) {
6032+ /* if symbol is already defined, then put complete type */
59826033 sym->t = t;
59836034 } else {
5984− /* put function address */
5985− sym = sym_push1(&global_stack, v, t, ind);
6035+ /* put function symbol */
6036+ sym = sym_push1(&global_stack, v, t, 0);
59866037 }
5987− sym->r = VT_CONST;
5988− funcname = get_tok_str(v, NULL);
5989− /* add symbol info */
5990− if (t & VT_STATIC)
5991− bind = STB_LOCAL;
5992− else
5993− bind = STB_GLOBAL;
5994− put_elf_sym(symtab_section, ind, 0,
5995− ELF32_ST_INFO(bind, STT_FUNC), 0,
5996− cur_text_section->sh_num, funcname);
5997−
6038+ put_extern_sym(sym, cur_text_section,
6039+ ind - (int)cur_text_section->data);
6040+ sym->r = VT_SYM | VT_CONST;
59986041 /* put debug symbol */
59996042 if (do_debug)
60006043 put_func_debug(t);
@@ -6064,52 +6107,6 @@ void decl(int l) · 63 unmodified lines
60646107 }
60656108 }
60666109
6067−/* put all global symbols in the extern stack and do all the
6068− resolving which can be done without using external symbols from DLLs */
6069−/* XXX: could try to verify types, but would not to save them in
6070− extern_stack too */
6071−void resolve_global_syms(void)
6072−{
6073− Sym *s, *s1, *ext_sym;
6074− Reloc **p;
6075−
6076− s = global_stack.top;
6077− while (s != NULL) {
6078− s1 = s->prev;
6079− /* do not save static or typedefed symbols or types */
6080− if (!(s->t & (VT_STATIC | VT_TYPEDEF)) &&
6081− !(s->v & (SYM_FIELD | SYM_STRUCT)) &&
6082− (s->v < SYM_FIRST_ANOM)) {
6083− ext_sym = sym_find1(&extern_stack, s->v);
6084− if (!ext_sym) {
6085− /* if the symbol do not exist, we simply save it */
6086− ext_sym = sym_push1(&extern_stack, s->v, s->t, s->c);
6087− ext_sym->r = s->r;
6088− } else if (ext_sym->r & VT_FORWARD) {
6089− /* external symbol already exists, but only as forward
6090− definition */
6091− if (!(s->r & VT_FORWARD)) {
6092− /* s is not forward, so we can relocate all symbols */
6093− greloc_patch(ext_sym, s->c);
6094− } else {
6095− /* the two symbols are forward: merge them */
6096− p = (Reloc **)&ext_sym->c;
6097− while (*p != NULL)
6098− p = &(*p)->next;
6099− *p = (Reloc *)s->c;
6100− }
6101− } else {
6102− /* external symbol already exists and is defined :
6103− patch all references to it */
6104− if (!(s->r & VT_FORWARD))
6105− error("'%s' defined twice", get_tok_str(s->v, NULL));
6106− greloc_patch(s, ext_sym->c);
6107− }
6108− }
6109− s = s1;
6110− }
6111−}
6112−
61136110 /* compile the C file opened in 'file'. Return non zero if errors. */
61146111 int tcc_compile(TCCState *s)
61156112 {
@@ -6152,8 +6149,6 @@ int tcc_compile(TCCState *s) · 36 unmodified lines
61526149 they are undefined) */
61536150 sym_pop(&define_stack, define_start);
61546151
6155− resolve_global_syms();
6156−
61576152 sym_pop(&global_stack, NULL);
61586153
61596154 return 0;
@@ -6243,67 +6238,6 @@ int tcc_add_dll(TCCState *s, const char *library_name) · 83 unmodified lines
62436238 return 0;
62446239 }
62456240
6246−/* If the symbol already exists, then it is redefined */
6247−int tcc_add_symbol(TCCState *s, const char *name, void *value)
6248−{
6249− TokenSym *ts;
6250− Sym *ext_sym;
6251− int v;
6252−
6253− ts = tok_alloc(name, 0);
6254− v = ts->tok;
6255− ext_sym = sym_find1(&extern_stack, v);
6256− if (ext_sym) {
6257− if (ext_sym->r & VT_FORWARD) {
6258− greloc_patch(ext_sym, (long)value);
6259− } else {
6260− /* redefine symbol */
6261− ext_sym->c = (long)value;
6262− }
6263− } else {
6264− ext_sym = sym_push1(&extern_stack, v, VT_INT, (long)value);
6265− ext_sym->r = VT_CONST;
6266− }
6267− return 0;
6268−}
6269−
6270−static void *resolve_sym(const char *sym)
6271−{
6272−#ifdef CONFIG_TCC_BCHECK
6273− if (do_bounds_check) {
6274− void *ptr;
6275− ptr = bound_resolve_sym(sym);
6276− if (ptr)
6277− return ptr;
6278− }
6279−#endif
6280− return dlsym(NULL, sym);
6281−}
6282−
6283−void resolve_extern_syms(void)
6284−{
6285− Sym *s, *s1;
6286− char *str;
6287− int addr;
6288−
6289− s = extern_stack.top;
6290− while (s != NULL) {
6291− s1 = s->prev;
6292− if (s->r & VT_FORWARD) {
6293− /* if there is at least one relocation to do, then find it
6294− and patch it */
6295− if (s->c) {
6296− str = get_tok_str(s->v, NULL);
6297− addr = (int)resolve_sym(str);
6298− if (!addr)
6299− error("unresolved external reference '%s'", str);
6300− greloc_patch(s, addr);
6301− }
6302− }
6303− s = s1;
6304− }
6305−}
6306−
63076241 static int put_elf_str(Section *s, const char *sym)
63086242 {
63096243 int c, offset;
@@ -6317,25 +6251,155 @@ static int put_elf_str(Section *s, const char *sym) · 7 unmodified lines
63176251 return offset;
63186252 }
63196253
6320−static void put_elf_sym(Section *s,
6321− unsigned long value, unsigned long size,
6322− int info, int other, int shndx, const char *name)
6254+/* elf symbol hashing function */
6255+static unsigned long elf_hash(const unsigned char *name)
63236256 {
6324− int name_offset;
6325− Elf32_Sym *sym;
6257+ unsigned long h = 0, g;
6258+
6259+ while (*name) {
6260+ h = (h << 4) + *name++;
6261+ g = h & 0xf0000000;
6262+ if (g)
6263+ h ^= g >> 24;
6264+ h &= ~g;
6265+ }
6266+ return h;
6267+}
63266268
6269+/* return the symbol number */
6270+static int put_elf_sym(Section *s,
6271+ unsigned long value, unsigned long size,
6272+ int info, int other, int shndx, const char *name)
6273+{
6274+ int name_offset, nbuckets, h, sym_index;
6275+ Elf32_Sym *sym;
6276+ Section *hs;
6277+
63276278 sym = (Elf32_Sym *)s->data_ptr;
63286279 if (name)
63296280 name_offset = put_elf_str(s->link, name);
63306281 else
63316282 name_offset = 0;
6283+ /* XXX: endianness */
63326284 sym->st_name = name_offset;
63336285 sym->st_value = value;
63346286 sym->st_size = size;
63356287 sym->st_info = info;
63366288 sym->st_other = other;
63376289 sym->st_shndx = shndx;
6290+ sym_index = sym - (Elf32_Sym *)s->data;
6291+ hs = s->hash;
6292+ if (hs && ELF32_ST_BIND(info) == STB_GLOBAL) {
6293+ /* add another hashing entry */
6294+ nbuckets = ((int *)hs->data)[0];
6295+ ((int *)hs->data)[1]++;
6296+ h = elf_hash(name) % nbuckets;
6297+ ((int *)hs->data)[2 + nbuckets + sym_index] = ((int *)hs->data)[2 + h];
6298+ ((int *)hs->data)[2 + h] = sym_index;
6299+ }
63386300 s->data_ptr += sizeof(Elf32_Sym);
6301+ return sym_index;
6302+}
6303+
6304+/* find global ELF symbol 'name' and return its index. Return 0 if not
6305+ found. */
6306+static int find_elf_sym(Section *s, const char *name)
6307+{
6308+ Elf32_Sym *sym;
6309+ Section *hs;
6310+ int nbuckets, sym_index, h;
6311+ const char *name1;
6312+
6313+ hs = s->hash;
6314+ if (!hs)
6315+ return 0;
6316+ nbuckets = ((int *)hs->data)[0];
6317+ h = elf_hash(name) % nbuckets;
6318+ sym_index = ((int *)hs->data)[2 + h];
6319+ while (sym_index != 0) {
6320+ sym = &((Elf32_Sym *)s->data)[sym_index];
6321+ name1 = s->link->data + sym->st_name;
6322+ if (!strcmp(name, name1))
6323+ return sym_index;
6324+ sym_index = ((int *)hs->data)[2 + nbuckets + sym_index];
6325+ }
6326+ return 0;
6327+}
6328+
6329+/* update sym->c so that it points to an external symbol in section
6330+ 'section' with value 'value' */
6331+/* XXX: get rid of put_elf_sym */
6332+void put_extern_sym(Sym *sym, Section *section, unsigned long value)
6333+{
6334+ int sym_type, sym_bind, sh_num;
6335+ Elf32_Sym *esym;
6336+ const char *name;
6337+
6338+ if (section)
6339+ sh_num = section->sh_num;
6340+ else
6341+ sh_num = SHN_UNDEF;
6342+ if (!sym->c) {
6343+ if ((sym->t & VT_BTYPE) == VT_FUNC)
6344+ sym_type = STT_FUNC;
6345+ else
6346+ sym_type = STT_OBJECT;
6347+ if (sym->t & VT_STATIC)
6348+ sym_bind = STB_LOCAL;
6349+ else
6350+ sym_bind = STB_GLOBAL;
6351+ /* if the symbol is global, then we look if it is already
6352+ defined */
6353+ /* NOTE: name can be NULL if anonymous symbol */
6354+ name = get_tok_str(sym->v, NULL);
6355+ if (sym_bind == STB_GLOBAL) {
6356+ sym->c = find_elf_sym(symtab_section, name);
6357+ if (!sym->c)
6358+ goto do_def;
6359+ /* check if not defined */
6360+ if (section) {
6361+ esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
6362+ if (esym->st_shndx != SHN_UNDEF)
6363+ error("'%s' defined twice", name);
6364+ esym->st_shndx = sh_num;
6365+ esym->st_value = value;
6366+ }
6367+ } else {
6368+ do_def:
6369+ sym->c = put_elf_sym(symtab_section, value, 0,
6370+ ELF32_ST_INFO(sym_bind, sym_type), 0,
6371+ sh_num, name);
6372+ }
6373+ } else {
6374+ esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
6375+ esym->st_value = value;
6376+ esym->st_shndx = sh_num;
6377+ }
6378+}
6379+
6380+/* put relocation */
6381+static void put_elf_reloc(Section *s, unsigned long offset,
6382+ int type, int symbol)
6383+{
6384+ char buf[256];
6385+ Section *sr;
6386+ Elf32_Rel *rel;
6387+
6388+ sr = s->reloc;
6389+ if (!sr) {
6390+ /* if no relocation section, create it */
6391+ snprintf(buf, sizeof(buf), ".rel%s", s->name);
6392+ sr = new_section(buf, SHT_REL, 0);
6393+ sr->sh_entsize = sizeof(Elf32_Rel);
6394+ sr->link = symtab_section;
6395+ sr->reloc_sec = s;
6396+ s->reloc = sr;
6397+ }
6398+ rel = (Elf32_Rel *)sr->data_ptr;
6399+ /* XXX: endianness */
6400+ rel->r_offset = offset;
6401+ rel->r_info = ELF32_R_INFO(symbol, type);
6402+ sr->data_ptr += sizeof(Elf32_Rel);
63396403 }
63406404
63416405 /* put stab debug information */
@@ -6427,6 +6491,8 @@ int tcc_output_file(TCCState *s, const char *filename, int file_type) · 85 unmodified lines
64276491 sh->sh_entsize = sec->sh_entsize;
64286492 if (sec->link)
64296493 sh->sh_link = sec->link->sh_num;
6494+ if (sec->reloc_sec)
6495+ sh->sh_info = sec->reloc_sec->sh_num;
64306496 if (sh->sh_type == SHT_STRTAB) {
64316497 sh->sh_addralign = 1;
64326498 } else if (sh->sh_type == SHT_SYMTAB ||
@@ -6677,17 +6743,105 @@ static void sig_error(int signum, siginfo_t *siginf, void *puc) · 244 unmodified lines
66776743 }
66786744 #endif
66796745
6746+static void *resolve_sym(const char *sym)
6747+{
6748+#ifdef CONFIG_TCC_BCHECK
6749+ if (do_bounds_check) {
6750+ void *ptr;
6751+ ptr = bound_resolve_sym(sym);
6752+ if (ptr)
6753+ return ptr;
6754+ }
6755+#endif
6756+ return dlsym(NULL, sym);
6757+}
6758+
6759+static void resolve_extern_syms(void)
6760+{
6761+ unsigned long addr;
6762+ Elf32_Sym *sym;
6763+ const char *name;
6764+
6765+ for(sym = (Elf32_Sym *)symtab_section->data + 1;
6766+ sym < (Elf32_Sym *)symtab_section->data_ptr;
6767+ sym++) {
6768+ if (sym->st_shndx == SHN_UNDEF) {
6769+ name = symtab_section->link->data + sym->st_name;
6770+ addr = (unsigned long)resolve_sym(name);
6771+ if (!addr)
6772+ error("unresolved external reference '%s'", name);
6773+ sym->st_value = addr;
6774+ sym->st_shndx = SHN_ABS;
6775+ }
6776+ }
6777+}
6778+
6779+static unsigned long get_sym_val(int sym_index)
6780+{
6781+ unsigned long val;
6782+ Elf32_Sym *sym;
6783+
6784+ sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
6785+ val = sym->st_value;
6786+ if (sym->st_shndx != SHN_ABS)
6787+ val += sections[sym->st_shndx]->addr;
6788+ return val;
6789+}
6790+
6791+/* relocate sections */
6792+static void relocate_section(Section *s)
6793+{
6794+ Section *sr;
6795+ Elf32_Rel *rel;
6796+ int type;
6797+ unsigned char *ptr;
6798+ unsigned long val;
6799+
6800+ sr = s->reloc;
6801+ if (!sr)
6802+ return;
6803+ for(rel = (Elf32_Rel *)sr->data;
6804+ rel < (Elf32_Rel *)sr->data_ptr;
6805+ rel++) {
6806+ ptr = s->data + rel->r_offset;
6807+ val = get_sym_val(ELF32_R_SYM(rel->r_info));
6808+ type = ELF32_R_TYPE(rel->r_info);
6809+ greloc_patch(ptr, s->addr + rel->r_offset, val, type);
6810+ }
6811+}
6812+
6813+/* relocate all the code and data */
6814+static void relocate_program(void)
6815+{
6816+ Section *s;
6817+
6818+ /* compute relocation address : section are relocated in place */
6819+ for(s = first_section; s != NULL; s = s->next) {
6820+ if (s->sh_type == SHT_PROGBITS)
6821+ s->addr = (unsigned long)s->data;
6822+ }
6823+
6824+ /* relocate each section */
6825+ for(s = first_section; s != NULL; s = s->next) {
6826+ if (s->sh_type == SHT_PROGBITS)
6827+ relocate_section(s);
6828+ }
6829+}
6830+
66806831 /* launch the compiled program with the given arguments */
66816832 int tcc_run(TCCState *s1, int argc, char **argv)
66826833 {
6683− Sym *s;
6684− int (*t)();
6834+ int (*prog_main)(int, char **);
6835+ int sym_index;
66856836
66866837 resolve_extern_syms();
66876838
6688− s = sym_find1(&extern_stack, TOK_MAIN);
6689− if (!s || (s->r & VT_FORWARD))
6839+ relocate_program();
6840+
6841+ sym_index = find_elf_sym(symtab_section, "main");
6842+ if (!sym_index)
66906843 error("main() not defined");
6844+ prog_main = (void *)get_sym_val(sym_index);
66916845
66926846 if (do_debug) {
66936847 #ifdef WIN32
@@ -6721,8 +6875,7 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 27 unmodified lines
67216875 }
67226876 #endif
67236877
6724− t = (int (*)())s->c;
6725− return (*t)(argc, argv);
6878+ return (*prog_main)(argc, argv);
67266879 }
67276880
67286881 TCCState *tcc_new(void)
@@ -6758,6 +6911,7 @@ TCCState *tcc_new(void) · 29 unmodified lines
67586911 tcc_define_symbol(s, "__TINYC__", NULL);
67596912
67606913 /* create standard sections */
6914+ nb_sections = 1;
67616915 text_section = new_section(".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
67626916 data_section = new_section(".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
67636917 /* XXX: should change type to SHT_NOBITS */
@@ -6770,7 +6924,13 @@ TCCState *tcc_new(void) · 6 unmodified lines
67706924 put_elf_str(strtab_section, "");
67716925 symtab_section->link = strtab_section;
67726926 put_elf_sym(symtab_section, 0, 0, 0, 0, 0, NULL);
6773−
6927+
6928+ /* private hash table for extern symbols */
6929+ hashtab_section = new_section(".hashtab", SHT_HASH, SHF_PRIVATE);
6930+ ((int *)hashtab_section->data)[0] = ELF_SYM_HASH_SIZE;
6931+ ((int *)hashtab_section->data)[1] = 1;
6932+ hashtab_section->data_ptr += (2 + ELF_SYM_HASH_SIZE + 1) * 4;
6933+ symtab_section->hash = hashtab_section;
67746934 return s;
67756935 }
67766936

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