| @@ -74,6 +74,7 @@ · 73 unmodified lines |
| 74 | #define TOK_HASH_SIZE 2048 /* must be a power of two */ | 74 | #define TOK_HASH_SIZE 2048 /* must be a power of two */ |
| 75 | #define TOK_ALLOC_INCR 512 /* must be a power of two */ | 75 | #define TOK_ALLOC_INCR 512 /* must be a power of two */ |
| 76 | #define SYM_HASH_SIZE 1031 | 76 | #define SYM_HASH_SIZE 1031 |
| | 77 | #define ELF_SYM_HASH_SIZE 2048 /* must be a power of two */ |
| 77 | | 78 | |
| 78 | /* token symbol management */ | 79 | /* token symbol management */ |
| 79 | typedef struct TokenSym { | 80 | typedef struct TokenSym { |
| @@ -104,7 +105,7 @@ typedef struct SValue { · 24 unmodified lines |
| 104 | unsigned short r; /* register + flags */ | 105 | unsigned short r; /* register + flags */ |
| 105 | unsigned short r2; /* second register, used for 'long long' | 106 | unsigned short r2; /* second register, used for 'long long' |
| 106 | type. If not used, set to VT_CONST */ | 107 | type. If not used, set to VT_CONST */ |
| 107 | CValue c; /* constant */ | 108 | CValue c; /* constant, if VT_CONST */ |
| 108 | } SValue; | 109 | } SValue; |
| 109 | | 110 | |
| 110 | /* symbol management */ | 111 | /* symbol management */ |
| @@ -123,16 +124,6 @@ typedef struct SymStack { · 12 unmodified lines |
| 123 | struct Sym *hash[SYM_HASH_SIZE]; | 124 | struct Sym *hash[SYM_HASH_SIZE]; |
| 124 | } SymStack; | 125 | } SymStack; |
| 125 | | 126 | |
| 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 | | | |
| 136 | /* section definition */ | 127 | /* section definition */ |
| 137 | typedef struct Section { | 128 | typedef struct Section { |
| 138 | unsigned char *data; /* section data */ | 129 | unsigned char *data; /* section data */ |
| @@ -140,8 +131,16 @@ typedef struct Section { · 1 unmodified line |
| 140 | int sh_num; /* elf section number */ | 131 | int sh_num; /* elf section number */ |
| 141 | int sh_type; /* elf section type */ | 132 | int sh_type; /* elf section type */ |
| 142 | int sh_flags; /* elf section flags */ | 133 | 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 |
| 143 | int sh_entsize; /* elf entry size */ | 137 | int sh_entsize; /* elf entry size */ |
| | 138 | unsigned long addr; /* address at which the section is relocated */ |
| 144 | struct Section *link; /* link to another section */ | 139 | 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 */ |
| 145 | struct Section *next; | 144 | struct Section *next; |
| 146 | char name[64]; /* section name */ | 145 | char name[64]; /* section name */ |
| 147 | } Section; | 146 | } Section; |
| @@ -170,6 +169,9 @@ typedef struct AttributeDef { · 22 unmodified lines |
| 170 | #define MACRO_OBJ 0 /* object like macro */ | 169 | #define MACRO_OBJ 0 /* object like macro */ |
| 171 | #define MACRO_FUNC 1 /* function like macro */ | 170 | #define MACRO_FUNC 1 /* function like macro */ |
| 172 | | 171 | |
| | 172 | /* field 'Sym.t' for labels */ |
| | 173 | #define LABEL_FORWARD 1 /* label is forward defined */ |
| | 174 | |
| 173 | /* type_decl() types */ | 175 | /* type_decl() types */ |
| 174 | #define TYPE_ABSTRACT 1 /* type without variable */ | 176 | #define TYPE_ABSTRACT 1 /* type without variable */ |
| 175 | #define TYPE_DIRECT 2 /* type with variable */ | 177 | #define TYPE_DIRECT 2 /* type with variable */ |
| @@ -210,16 +212,21 @@ int ch, ch1, tok, tok1; · 34 unmodified lines |
| 210 | CValue tokc, tok1c; | 212 | CValue tokc, tok1c; |
| 211 | | 213 | |
| 212 | /* sections */ | 214 | /* sections */ |
| 213 | Section *first_section; | 215 | /* XXX: suppress first_section */ |
| 214 | int section_num; | 216 | Section *first_section, **sections; |
| | 217 | int nb_sections; /* number of sections, including first dummy section */ |
| | 218 | int nb_allocated_sections; |
| 215 | Section *text_section, *data_section, *bss_section; /* predefined sections */ | 219 | Section *text_section, *data_section, *bss_section; /* predefined sections */ |
| 216 | Section *cur_text_section; /* current section where function code is | 220 | Section *cur_text_section; /* current section where function code is |
| 217 | generated */ | 221 | generated */ |
| 218 | /* bound check related sections */ | 222 | /* bound check related sections */ |
| 219 | Section *bounds_section; /* contains global data bound description */ | 223 | Section *bounds_section; /* contains global data bound description */ |
| 220 | Section *lbounds_section; /* contains local data bound description */ | 224 | Section *lbounds_section; /* contains local data bound description */ |
| | 225 | /* symbol sections */ |
| | 226 | Section *symtab_section, *strtab_section, *hashtab_section; |
| | 227 | |
| 221 | /* debug sections */ | 228 | /* debug sections */ |
| 222 | Section *stab_section, *stabstr_section, *symtab_section, *strtab_section; | 229 | Section *stab_section, *stabstr_section; |
| 223 | | 230 | |
| 224 | /* loc : local variable index | 231 | /* loc : local variable index |
| 225 | ind : output code index | 232 | ind : output code index |
| @@ -238,8 +245,6 @@ TokenSym **table_ident; · 12 unmodified lines |
| 238 | TokenSym *hash_ident[TOK_HASH_SIZE]; | 245 | TokenSym *hash_ident[TOK_HASH_SIZE]; |
| 239 | char token_buf[STRING_MAX_SIZE + 1]; | 246 | char token_buf[STRING_MAX_SIZE + 1]; |
| 240 | char *funcname; | 247 | char *funcname; |
| 241 | /* contains global symbols which remain between each translation unit */ | | |
| 242 | SymStack extern_stack; | | |
| 243 | SymStack define_stack, global_stack, local_stack, label_stack; | 248 | SymStack define_stack, global_stack, local_stack, label_stack; |
| 244 | | 249 | |
| 245 | SValue vstack[VSTACK_SIZE], *vtop; | 250 | SValue vstack[VSTACK_SIZE], *vtop; |
| @@ -281,7 +286,7 @@ struct TCCState { · 35 unmodified lines |
| 281 | #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */ | 286 | #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */ |
| 282 | #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */ | 287 | #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */ |
| 283 | #define VT_LVAL 0x0100 /* var is an lvalue */ | 288 | #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 */ |
| 285 | #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for | 290 | #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for |
| 286 | char/short stored in integer registers) */ | 291 | char/short stored in integer registers) */ |
| 287 | #define VT_MUSTBOUND 0x0800 /* bound checking must be done before | 292 | #define VT_MUSTBOUND 0x0800 /* bound checking must be done before |
| @@ -519,9 +524,9 @@ int expr_const(void); · 231 unmodified lines |
| 519 | void expr_eq(void); | 524 | void expr_eq(void); |
| 520 | void gexpr(void); | 525 | void gexpr(void); |
| 521 | void decl(int l); | 526 | void decl(int l); |
| 522 | void decl_initializer(int t, int r, int c, int first, int size_only); | 527 | void decl_initializer(int t, Section *sec, unsigned long c, int first, int size_only); |
| 523 | int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, | 528 | void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, |
| 524 | int v, int scope); | 529 | int v, int scope); |
| 525 | int gv(int rc); | 530 | int gv(int rc); |
| 526 | void gv2(int rc1, int rc2); | 531 | void gv2(int rc1, int rc2); |
| 527 | void move_reg(int r, int s); | 532 | void move_reg(int r, int s); |
| @@ -556,13 +561,17 @@ void vpushi(int v); · 28 unmodified lines |
| 556 | void vset(int t, int r, int v); | 561 | void vset(int t, int r, int v); |
| 557 | void type_to_str(char *buf, int buf_size, | 562 | void type_to_str(char *buf, int buf_size, |
| 558 | int t, const char *varstr); | 563 | int t, const char *varstr); |
| | 564 | char *get_tok_str(int v, CValue *cv); |
| 559 | | 565 | |
| 560 | /* section generation */ | 566 | /* 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); |
| 562 | static int put_elf_str(Section *s, const char *sym); | 569 | static int put_elf_str(Section *s, const char *sym); |
| 563 | static void put_elf_sym(Section *s, | 570 | static int put_elf_sym(Section *s, |
| 564 | unsigned long value, unsigned long size, | 571 | unsigned long value, unsigned long size, |
| 565 | int info, int other, int shndx, const char *name); | 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); |
| 566 | static void put_stabs(const char *str, int type, int other, int desc, int value); | 575 | static void put_stabs(const char *str, int type, int other, int desc, int value); |
| 567 | static void put_stabn(int type, int other, int desc, int value); | 576 | static void put_stabn(int type, int other, int desc, int value); |
| 568 | static void put_stabd(int type, int other, int desc); | 577 | 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 |
| 755 | error("memory full"); | 764 | error("memory full"); |
| 756 | memset(sec, 0, sizeof(Section)); | 765 | memset(sec, 0, sizeof(Section)); |
| 757 | pstrcpy(sec->name, sizeof(sec->name), name); | 766 | pstrcpy(sec->name, sizeof(sec->name), name); |
| 758 | sec->link = NULL; | | |
| 759 | sec->sh_num = ++section_num; | | |
| 760 | sec->sh_type = sh_type; | 767 | sec->sh_type = sh_type; |
| 761 | sec->sh_flags = sh_flags; | 768 | sec->sh_flags = sh_flags; |
| 762 | #ifdef WIN32 | 769 | #ifdef WIN32 |
| @@ -774,11 +781,29 @@ Section *new_section(const char *name, int sh_type, int sh_flags) · 11 unmodified lines |
| 774 | #endif | 781 | #endif |
| 775 | sec->data = data; | 782 | sec->data = data; |
| 776 | sec->data_ptr = data; | 783 | sec->data_ptr = data; |
| 777 | psec = &first_section; | 784 | |
| 778 | while (*psec != NULL) | 785 | if (!(sh_flags & SHF_PRIVATE)) { |
| 779 | psec = &(*psec)->next; | 786 | /* only add section if not private */ |
| 780 | sec->next = NULL; | 787 | |
| 781 | *psec = sec; | 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 | } |
| 782 | return sec; | 807 | return sec; |
| 783 | } | 808 | } |
| 784 | | 809 | |
| @@ -796,36 +821,15 @@ Section *find_section(const char *name) · 11 unmodified lines |
| 796 | return new_section(name, SHT_PROGBITS, SHF_ALLOC); | 821 | return new_section(name, SHT_PROGBITS, SHF_ALLOC); |
| 797 | } | 822 | } |
| 798 | | 823 | |
| 799 | /* add a new relocation entry to symbol 's' */ | 824 | /* add a new relocation entry to symbol 'sym' in section 's' */ |
| 800 | void greloc(Sym *s, int addr, int type) | 825 | void greloc(Section *s, Sym *sym, unsigned long offset, 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) | | |
| 814 | { | 826 | { |
| 815 | Reloc *p, *p1; | 827 | if (!sym->c) |
| 816 | | 828 | put_extern_sym(sym, NULL, 0); |
| 817 | p = (Reloc *)s->c; | 829 | /* now we can add ELF relocation info */ |
| 818 | while (p != NULL) { | 830 | put_elf_reloc(s, offset, type, sym->c); |
| 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; | | |
| 826 | } | 831 | } |
| 827 | | 832 | |
| 828 | | | |
| 829 | static inline int isid(int c) | 833 | static inline int isid(int c) |
| 830 | { | 834 | { |
| 831 | return (c >= 'a' && c <= 'z') || | 835 | return (c >= 'a' && c <= 'z') || |
| @@ -2502,6 +2506,21 @@ void vpushi(int v) · 1670 unmodified lines |
| 2502 | vsetc(VT_INT, VT_CONST, &cval); | 2506 | vsetc(VT_INT, VT_CONST, &cval); |
| 2503 | } | 2507 | } |
| 2504 | | 2508 | |
| | 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 | |
| 2505 | void vset(int t, int r, int v) | 2524 | void vset(int t, int r, int v) |
| 2506 | { | 2525 | { |
| 2507 | CValue cval; | 2526 | CValue cval; |
| @@ -3016,7 +3035,7 @@ void gen_opl(int op) · 508 unmodified lines |
| 3016 | case TOK_SAR: | 3035 | case TOK_SAR: |
| 3017 | case TOK_SHR: | 3036 | case TOK_SHR: |
| 3018 | case TOK_SHL: | 3037 | 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) { |
| 3020 | t = vtop[-1].t; | 3039 | t = vtop[-1].t; |
| 3021 | vswap(); | 3040 | vswap(); |
| 3022 | lexpand(); | 3041 | lexpand(); |
| @@ -3142,8 +3161,8 @@ void gen_opic(int op) · 119 unmodified lines |
| 3142 | v1 = vtop - 1; | 3161 | v1 = vtop - 1; |
| 3143 | v2 = vtop; | 3162 | v2 = vtop; |
| 3144 | /* currently, we cannot do computations with forward symbols */ | 3163 | /* currently, we cannot do computations with forward symbols */ |
| 3145 | c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST; | 3164 | c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; |
| 3146 | c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST; | 3165 | c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; |
| 3147 | if (c1 && c2) { | 3166 | if (c1 && c2) { |
| 3148 | fc = v2->c.i; | 3167 | fc = v2->c.i; |
| 3149 | switch(op) { | 3168 | switch(op) { |
| @@ -3246,8 +3265,8 @@ void gen_opif(int op) · 96 unmodified lines |
| 3246 | v1 = vtop - 1; | 3265 | v1 = vtop - 1; |
| 3247 | v2 = vtop; | 3266 | v2 = vtop; |
| 3248 | /* currently, we cannot do computations with forward symbols */ | 3267 | /* currently, we cannot do computations with forward symbols */ |
| 3249 | c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST; | 3268 | c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; |
| 3250 | c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST; | 3269 | c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; |
| 3251 | if (c1 && c2) { | 3270 | if (c1 && c2) { |
| 3252 | if (v1->t == VT_FLOAT) { | 3271 | if (v1->t == VT_FLOAT) { |
| 3253 | f1 = v1->c.f; | 3272 | f1 = v1->c.f; |
| @@ -3544,7 +3563,7 @@ void gen_cast(int t) · 290 unmodified lines |
| 3544 | if (sbt != dbt) { | 3563 | if (sbt != dbt) { |
| 3545 | sf = is_float(sbt); | 3564 | sf = is_float(sbt); |
| 3546 | df = is_float(dbt); | 3565 | 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; |
| 3548 | if (sf && df) { | 3567 | if (sf && df) { |
| 3549 | /* convert from fp to fp */ | 3568 | /* convert from fp to fp */ |
| 3550 | if (c) { | 3569 | if (c) { |
| @@ -3898,7 +3917,7 @@ void gen_assign_cast(int dt) · 347 unmodified lines |
| 3898 | } | 3917 | } |
| 3899 | /* '0' can also be a pointer */ | 3918 | /* '0' can also be a pointer */ |
| 3900 | if ((st & VT_BTYPE) == VT_INT && | 3919 | 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) && |
| 3902 | vtop->c.i == 0) | 3921 | vtop->c.i == 0) |
| 3903 | goto type_ok; | 3922 | goto type_ok; |
| 3904 | } | 3923 | } |
| @@ -4531,16 +4550,17 @@ int type_decl(AttributeDef *ad, int *v, int t, int td) · 626 unmodified lines |
| 4531 | return u; | 4550 | return u; |
| 4532 | } | 4551 | } |
| 4533 | | 4552 | |
| 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' */ |
| 4535 | Sym *external_sym(int v, int u, int r) | 4554 | Sym *external_sym(int v, int u, int r) |
| 4536 | { | 4555 | { |
| 4537 | Sym *s; | 4556 | Sym *s; |
| | 4557 | |
| 4538 | s = sym_find(v); | 4558 | s = sym_find(v); |
| 4539 | if (!s) { | 4559 | if (!s) { |
| 4540 | /* push forward reference */ | 4560 | /* push forward reference */ |
| 4541 | s = sym_push1(&global_stack, | 4561 | s = sym_push1(&global_stack, |
| 4542 | v, u, 0); | 4562 | v, u | VT_EXTERN, 0); |
| 4543 | s->r = r | VT_CONST | VT_FORWARD; | 4563 | s->r = r | VT_CONST | VT_SYM; |
| 4544 | } | 4564 | } |
| 4545 | return s; | 4565 | return s; |
| 4546 | } | 4566 | } |
| @@ -4628,11 +4648,11 @@ void unary(void) · 81 unmodified lines |
| 4628 | } else if (tok == TOK___FUNC__) { | 4648 | } else if (tok == TOK___FUNC__) { |
| 4629 | /* special function name identifier */ | 4649 | /* special function name identifier */ |
| 4630 | /* generate (char *) type */ | 4650 | /* generate (char *) type */ |
| 4631 | data_offset = (int)data_section->data_ptr; | 4651 | data_offset = data_section->data_ptr - data_section->data; |
| 4632 | vset(mk_pointer(VT_BYTE), VT_CONST, data_offset); | 4652 | vpush_ref(mk_pointer(VT_BYTE), data_section, data_offset); |
| 4633 | strcpy((void *)data_offset, funcname); | 4653 | strcpy(data_section->data + data_offset, funcname); |
| 4634 | data_offset += strlen(funcname) + 1; | 4654 | data_offset += strlen(funcname) + 1; |
| 4635 | data_section->data_ptr = (unsigned char *)data_offset; | 4655 | data_section->data_ptr = data_section->data + data_offset; |
| 4636 | next(); | 4656 | next(); |
| 4637 | } else if (tok == TOK_LSTR) { | 4657 | } else if (tok == TOK_LSTR) { |
| 4638 | t = VT_INT; | 4658 | t = VT_INT; |
| @@ -4642,16 +4662,16 @@ void unary(void) · 3 unmodified lines |
| 4642 | t = VT_BYTE; | 4662 | t = VT_BYTE; |
| 4643 | str_init: | 4663 | str_init: |
| 4644 | type_size(t, &align); | 4664 | type_size(t, &align); |
| 4645 | data_offset = (int)data_section->data_ptr; | 4665 | data_offset = data_section->data_ptr - data_section->data; |
| 4646 | data_offset = (data_offset + align - 1) & -align; | 4666 | data_offset = (data_offset + align - 1) & -align; |
| 4647 | fc = data_offset; | 4667 | fc = data_offset; |
| 4648 | /* we must declare it as an array first to use initializer parser */ | 4668 | /* we must declare it as an array first to use initializer parser */ |
| 4649 | t = VT_ARRAY | mk_pointer(t); | 4669 | 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); |
| 4651 | data_offset += type_size(t, &align); | 4671 | data_offset += type_size(t, &align); |
| 4652 | /* put it as pointer */ | 4672 | /* put it as pointer */ |
| 4653 | vset(t & ~VT_ARRAY, VT_CONST, fc); | 4673 | vpush_ref(t & ~VT_ARRAY, data_section, fc); |
| 4654 | data_section->data_ptr = (unsigned char *)data_offset; | 4674 | data_section->data_ptr = data_section->data + data_offset; |
| 4655 | } else { | 4675 | } else { |
| 4656 | t = tok; | 4676 | t = tok; |
| 4657 | next(); | 4677 | next(); |
| @@ -4671,8 +4691,7 @@ void unary(void) · 13 unmodified lines |
| 4671 | if (!(ft & VT_ARRAY)) | 4691 | if (!(ft & VT_ARRAY)) |
| 4672 | r |= lvalue_type(ft); | 4692 | r |= lvalue_type(ft); |
| 4673 | memset(&ad, 0, sizeof(AttributeDef)); | 4693 | memset(&ad, 0, sizeof(AttributeDef)); |
| 4674 | fc = decl_initializer_alloc(ft, &ad, r, 1, 0, 0); | 4694 | decl_initializer_alloc(ft, &ad, r, 1, 0, 0); |
| 4675 | vset(ft, r, fc); | | |
| 4676 | } else { | 4695 | } else { |
| 4677 | unary(); | 4696 | unary(); |
| 4678 | gen_cast(ft); | 4697 | gen_cast(ft); |
| @@ -4751,14 +4770,14 @@ void unary(void) · 72 unmodified lines |
| 4751 | error("'%s' undeclared", get_tok_str(t, NULL)); | 4770 | error("'%s' undeclared", get_tok_str(t, NULL)); |
| 4752 | /* for simple function calls, we tolerate undeclared | 4771 | /* for simple function calls, we tolerate undeclared |
| 4753 | external reference */ | 4772 | external reference */ |
| 4754 | p = anon_sym++; | 4773 | p = anon_sym++; |
| 4755 | sym_push1(&global_stack, p, 0, FUNC_OLD); | 4774 | sym_push1(&global_stack, p, 0, FUNC_OLD); |
| 4756 | /* int() function */ | 4775 | /* int() function */ |
| 4757 | s = external_sym(t, VT_FUNC | (p << VT_STRUCT_SHIFT), 0); | 4776 | s = external_sym(t, VT_FUNC | (p << VT_STRUCT_SHIFT), 0); |
| 4758 | } | 4777 | } |
| 4759 | vset(s->t, s->r, s->c); | 4778 | vset(s->t, s->r, s->c); |
| 4760 | /* if forward reference, we must point to s */ | 4779 | /* if forward reference, we must point to s */ |
| 4761 | if (vtop->r & VT_FORWARD) | 4780 | if (vtop->r & VT_SYM) |
| 4762 | vtop->c.sym = s; | 4781 | vtop->c.sym = s; |
| 4763 | } | 4782 | } |
| 4764 | } | 4783 | } |
| @@ -5333,9 +5352,9 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 568 unmodified lines |
| 5333 | s = sym_find1(&label_stack, tok); | 5352 | s = sym_find1(&label_stack, tok); |
| 5334 | /* put forward definition if needed */ | 5353 | /* put forward definition if needed */ |
| 5335 | if (!s) | 5354 | if (!s) |
| 5336 | s = sym_push1(&label_stack, tok, VT_FORWARD, 0); | 5355 | s = sym_push1(&label_stack, tok, LABEL_FORWARD, 0); |
| 5337 | /* label already defined */ | 5356 | /* label already defined */ |
| 5338 | if (s->t & VT_FORWARD) | 5357 | if (s->t & LABEL_FORWARD) |
| 5339 | s->c = gjmp(s->c); | 5358 | s->c = gjmp(s->c); |
| 5340 | else | 5359 | else |
| 5341 | gjmp_addr(s->c); | 5360 | 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 |
| 5347 | /* label case */ | 5366 | /* label case */ |
| 5348 | s = sym_find1(&label_stack, b); | 5367 | s = sym_find1(&label_stack, b); |
| 5349 | if (s) { | 5368 | if (s) { |
| 5350 | if (!(s->t & VT_FORWARD)) | 5369 | if (!(s->t & LABEL_FORWARD)) |
| 5351 | error("multiple defined label"); | 5370 | error("multiple defined label"); |
| 5352 | gsym(s->c); | 5371 | gsym(s->c); |
| 5353 | s->c = ind; | 5372 | 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 |
| 5375 | address. cur_index/cur_field is the pointer to the current | 5394 | address. cur_index/cur_field is the pointer to the current |
| 5376 | value. 'size_only' is true if only size info is needed (only used | 5395 | value. 'size_only' is true if only size info is needed (only used |
| 5377 | in arrays) */ | 5396 | in arrays) */ |
| 5378 | void decl_designator(int t, int r, int c, | 5397 | void decl_designator(int t, Section *sec, unsigned long c, |
| 5379 | int *cur_index, Sym **cur_field, | 5398 | int *cur_index, Sym **cur_field, |
| 5380 | int size_only) | 5399 | int size_only) |
| 5381 | { | 5400 | { |
| @@ -5444,7 +5463,7 @@ void decl_designator(int t, int r, int c, · 62 unmodified lines |
| 5444 | c += f->c; | 5463 | c += f->c; |
| 5445 | } | 5464 | } |
| 5446 | } | 5465 | } |
| 5447 | decl_initializer(t, r, c, 0, size_only); | 5466 | decl_initializer(t, sec, c, 0, size_only); |
| 5448 | } | 5467 | } |
| 5449 | | 5468 | |
| 5450 | #define EXPR_VAL 0 | 5469 | #define EXPR_VAL 0 |
| @@ -5452,10 +5471,11 @@ void decl_designator(int t, int r, int c, · 1 unmodified line |
| 5452 | #define EXPR_ANY 2 | 5471 | #define EXPR_ANY 2 |
| 5453 | | 5472 | |
| 5454 | /* store a value or an expression directly in global data or in local array */ | 5473 | /* 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, |
| 5456 | int v, int expr_type) | 5475 | int v, int expr_type) |
| 5457 | { | 5476 | { |
| 5458 | int saved_global_expr, bt; | 5477 | int saved_global_expr, bt; |
| | 5478 | void *ptr; |
| 5459 | | 5479 | |
| 5460 | switch(expr_type) { | 5480 | switch(expr_type) { |
| 5461 | case EXPR_VAL: | 5481 | case EXPR_VAL: |
| @@ -5473,33 +5493,47 @@ void init_putv(int t, int r, int c, · 11 unmodified lines |
| 5473 | break; | 5493 | break; |
| 5474 | } | 5494 | } |
| 5475 | | 5495 | |
| 5476 | if ((r & VT_VALMASK) == VT_CONST) { | 5496 | if (sec) { |
| 5477 | /* XXX: not portable */ | 5497 | /* XXX: not portable */ |
| | 5498 | /* XXX: generate error if incorrect relocation */ |
| 5478 | gen_assign_cast(t); | 5499 | gen_assign_cast(t); |
| 5479 | bt = t & VT_BTYPE; | 5500 | 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"); |
| 5480 | switch(bt) { | 5509 | switch(bt) { |
| 5481 | case VT_BYTE: | 5510 | case VT_BYTE: |
| 5482 | *(char *)c = vtop->c.i; | 5511 | *(char *)ptr = vtop->c.i; |
| 5483 | break; | 5512 | break; |
| 5484 | case VT_SHORT: | 5513 | case VT_SHORT: |
| 5485 | *(short *)c = vtop->c.i; | 5514 | *(short *)ptr = vtop->c.i; |
| 5486 | break; | 5515 | break; |
| 5487 | case VT_DOUBLE: | 5516 | case VT_DOUBLE: |
| 5488 | *(double *)c = vtop->c.d; | 5517 | *(double *)ptr = vtop->c.d; |
| 5489 | break; | 5518 | break; |
| 5490 | case VT_LDOUBLE: | 5519 | case VT_LDOUBLE: |
| 5491 | *(long double *)c = vtop->c.ld; | 5520 | *(long double *)ptr = vtop->c.ld; |
| 5492 | break; | 5521 | break; |
| 5493 | case VT_LLONG: | 5522 | case VT_LLONG: |
| 5494 | *(long long *)c = vtop->c.ll; | 5523 | *(long long *)ptr = vtop->c.ll; |
| 5495 | break; | 5524 | break; |
| 5496 | default: | 5525 | 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 | } |
| 5498 | break; | 5532 | break; |
| 5499 | } | 5533 | } |
| 5500 | vtop--; | 5534 | vtop--; |
| 5501 | } else { | 5535 | } else { |
| 5502 | vset(t, r, c); | 5536 | vset(t, VT_LOCAL, c); |
| 5503 | vswap(); | 5537 | vswap(); |
| 5504 | vstore(); | 5538 | vstore(); |
| 5505 | vpop(); | 5539 | vpop(); |
| @@ -5507,11 +5541,11 @@ void init_putv(int t, int r, int c, · 1 unmodified line |
| 5507 | } | 5541 | } |
| 5508 | | 5542 | |
| 5509 | /* put zeros for variable based init */ | 5543 | /* 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) |
| 5511 | { | 5545 | { |
| 5512 | GFuncContext gf; | 5546 | GFuncContext gf; |
| 5513 | | 5547 | |
| 5514 | if ((r & VT_VALMASK) == VT_CONST) { | 5548 | if (sec) { |
| 5515 | /* nothing to do because globals are already set to zero */ | 5549 | /* nothing to do because globals are already set to zero */ |
| 5516 | } else { | 5550 | } else { |
| 5517 | gfunc_start(&gf, FUNC_CDECL); | 5551 | gfunc_start(&gf, FUNC_CDECL); |
| @@ -5526,11 +5560,12 @@ void init_putz(int t, int r, int c, int size) · 8 unmodified lines |
| 5526 | } | 5560 | } |
| 5527 | } | 5561 | } |
| 5528 | | 5562 | |
| 5529 | /* 't' contains the type and storage info. c is the address of the | 5563 | /* 't' contains the type and storage info. 'c' is the offset of the |
| 5530 | object. 'first' is true if array '{' must be read (multi dimension | 5564 | object in section 'sec'. If 'sec' is NULL, it means stack based |
| 5531 | implicit array init handling). 'size_only' is true if size only | 5565 | allocation. 'first' is true if array '{' must be read (multi |
| 5532 | evaluation is wanted (only for arrays). */ | 5566 | dimension implicit array init handling). 'size_only' is true if |
| 5533 | void decl_initializer(int t, int r, int c, int first, int size_only) | 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) |
| 5534 | { | 5569 | { |
| 5535 | int index, array_length, n, no_oblock, nb, parlevel, i; | 5570 | int index, array_length, n, no_oblock, nb, parlevel, i; |
| 5536 | int t1, size1, align1, expr_type; | 5571 | 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 |
| 5568 | if (ts->len > nb) | 5603 | if (ts->len > nb) |
| 5569 | warning("initializer-string for array is too long"); | 5604 | warning("initializer-string for array is too long"); |
| 5570 | for(i=0;i<nb;i++) { | 5605 | 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, |
| 5572 | ts->str[i], EXPR_VAL); | 5607 | ts->str[i], EXPR_VAL); |
| 5573 | } | 5608 | } |
| 5574 | } | 5609 | } |
| @@ -5579,20 +5614,20 @@ void decl_initializer(int t, int r, int c, int first, int size_only) · 4 unmodified lines |
| 5579 | warning in this case since it is standard) */ | 5614 | warning in this case since it is standard) */ |
| 5580 | if (n < 0 || array_length < n) { | 5615 | if (n < 0 || array_length < n) { |
| 5581 | if (!size_only) { | 5616 | 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); |
| 5583 | } | 5618 | } |
| 5584 | array_length++; | 5619 | array_length++; |
| 5585 | } | 5620 | } |
| 5586 | } else { | 5621 | } else { |
| 5587 | index = 0; | 5622 | index = 0; |
| 5588 | while (tok != '}') { | 5623 | while (tok != '}') { |
| 5589 | decl_designator(t, r, c, &index, NULL, size_only); | 5624 | decl_designator(t, sec, c, &index, NULL, size_only); |
| 5590 | if (n >= 0 && index >= n) | 5625 | if (n >= 0 && index >= n) |
| 5591 | error("index too large"); | 5626 | error("index too large"); |
| 5592 | /* must put zero in holes (note that doing it that way | 5627 | /* must put zero in holes (note that doing it that way |
| 5593 | ensures that it even works with designators) */ | 5628 | ensures that it even works with designators) */ |
| 5594 | if (!size_only && array_length < index) { | 5629 | if (!size_only && array_length < index) { |
| 5595 | init_putz(t1, r, c + array_length * size1, | 5630 | init_putz(t1, sec, c + array_length * size1, |
| 5596 | (index - array_length) * size1); | 5631 | (index - array_length) * size1); |
| 5597 | } | 5632 | } |
| 5598 | index++; | 5633 | index++; |
| @@ -5612,7 +5647,7 @@ void decl_initializer(int t, int r, int c, int first, int size_only) · 13 unmodified lines |
| 5612 | skip('}'); | 5647 | skip('}'); |
| 5613 | /* put zeros at the end */ | 5648 | /* put zeros at the end */ |
| 5614 | if (!size_only && n >= 0 && array_length < n) { | 5649 | 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, |
| 5616 | (n - array_length) * size1); | 5651 | (n - array_length) * size1); |
| 5617 | } | 5652 | } |
| 5618 | /* patch type size if needed */ | 5653 | /* 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 |
| 5627 | index = 0; | 5662 | index = 0; |
| 5628 | n = s->c; | 5663 | n = s->c; |
| 5629 | while (tok != '}') { | 5664 | while (tok != '}') { |
| 5630 | decl_designator(t, r, c, NULL, &f, size_only); | 5665 | decl_designator(t, sec, c, NULL, &f, size_only); |
| 5631 | /* fill with zero between fields */ | 5666 | /* fill with zero between fields */ |
| 5632 | index = f->c; | 5667 | index = f->c; |
| 5633 | if (!size_only && array_length < index) { | 5668 | if (!size_only && array_length < index) { |
| 5634 | init_putz(t, r, c + array_length, | 5669 | init_putz(t, sec, c + array_length, |
| 5635 | index - array_length); | 5670 | index - array_length); |
| 5636 | } | 5671 | } |
| 5637 | index = index + type_size(f->t, &align1); | 5672 | 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 |
| 5644 | } | 5679 | } |
| 5645 | /* put zeros at the end */ | 5680 | /* put zeros at the end */ |
| 5646 | if (!size_only && array_length < n) { | 5681 | if (!size_only && array_length < n) { |
| 5647 | init_putz(t, r, c + array_length, | 5682 | init_putz(t, sec, c + array_length, |
| 5648 | n - array_length); | 5683 | n - array_length); |
| 5649 | } | 5684 | } |
| 5650 | skip('}'); | 5685 | skip('}'); |
| 5651 | } else if (tok == '{') { | 5686 | } else if (tok == '{') { |
| 5652 | next(); | 5687 | next(); |
| 5653 | decl_initializer(t, r, c, first, size_only); | 5688 | decl_initializer(t, sec, c, first, size_only); |
| 5654 | skip('}'); | 5689 | skip('}'); |
| 5655 | } else if (size_only) { | 5690 | } else if (size_only) { |
| 5656 | /* just skip expression */ | 5691 | /* just skip expression */ |
| @@ -5667,19 +5702,20 @@ void decl_initializer(int t, int r, int c, int first, int size_only) · 10 unmodified lines |
| 5667 | /* currently, we always use constant expression for globals | 5702 | /* currently, we always use constant expression for globals |
| 5668 | (may change for scripting case) */ | 5703 | (may change for scripting case) */ |
| 5669 | expr_type = EXPR_CONST; | 5704 | expr_type = EXPR_CONST; |
| 5670 | if ((r & VT_VALMASK) == VT_LOCAL) | 5705 | if (!sec) |
| 5671 | expr_type = EXPR_ANY; | 5706 | expr_type = EXPR_ANY; |
| 5672 | init_putv(t, r, c, 0, expr_type); | 5707 | init_putv(t, sec, c, 0, expr_type); |
| 5673 | } | 5708 | } |
| 5674 | } | 5709 | } |
| 5675 | | 5710 | |
| 5676 | /* parse an initializer for type 't' if 'has_init' is true, and | 5711 | /* parse an initializer for type 't' if 'has_init' is true, and |
| 5677 | allocate space in local or global data space ('r' is either | 5712 | 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 | 5713 | VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated |
| 5679 | non zero, then an associated variable 'v' of scope 'scope' is | 5714 | variable 'v' of scope 'scope' is declared before initializers are |
| 5680 | declared before initializers are parsed. */ | 5715 | parsed. If 'v' is zero, then a reference to the new object is put |
| 5681 | int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, | 5716 | in the value stack. */ |
| 5682 | int v, int scope) | 5717 | void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, |
| | 5718 | int v, int scope) |
| 5683 | { | 5719 | { |
| 5684 | int size, align, addr, data_offset; | 5720 | int size, align, addr, data_offset; |
| 5685 | int level; | 5721 | int level; |
| @@ -5721,7 +5757,7 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 35 unmodified lines |
| 5721 | | 5757 | |
| 5722 | macro_ptr = init_str.str; | 5758 | macro_ptr = init_str.str; |
| 5723 | next(); | 5759 | next(); |
| 5724 | decl_initializer(t, r, 0, 1, 1); | 5760 | decl_initializer(t, NULL, 0, 1, 1); |
| 5725 | /* prepare second initializer parsing */ | 5761 | /* prepare second initializer parsing */ |
| 5726 | macro_ptr = init_str.str; | 5762 | macro_ptr = init_str.str; |
| 5727 | next(); | 5763 | next(); |
| @@ -5735,6 +5771,7 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 7 unmodified lines |
| 5735 | if (ad->aligned > align) | 5771 | if (ad->aligned > align) |
| 5736 | align = ad->aligned; | 5772 | align = ad->aligned; |
| 5737 | if ((r & VT_VALMASK) == VT_LOCAL) { | 5773 | if ((r & VT_VALMASK) == VT_LOCAL) { |
| | 5774 | sec = NULL; |
| 5738 | if (do_bounds_check && (t & VT_ARRAY)) | 5775 | if (do_bounds_check && (t & VT_ARRAY)) |
| 5739 | loc--; | 5776 | loc--; |
| 5740 | #ifdef TCC_TARGET_IL | 5777 | #ifdef TCC_TARGET_IL |
| @@ -5768,13 +5805,11 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 27 unmodified lines |
| 5768 | else | 5805 | else |
| 5769 | sec = bss_section; | 5806 | sec = bss_section; |
| 5770 | } | 5807 | } |
| 5771 | data_offset = (int)sec->data_ptr; | 5808 | data_offset = sec->data_ptr - sec->data; |
| 5772 | data_offset = (data_offset + align - 1) & -align; | 5809 | data_offset = (data_offset + align - 1) & -align; |
| 5773 | addr = data_offset; | 5810 | addr = data_offset; |
| 5774 | /* very important to increment global | 5811 | /* very important to increment global pointer at this time |
| 5775 | pointer at this time because | 5812 | because initializers themselves can create new initializers */ |
| 5776 | initializers themselves can create new | | |
| 5777 | initializers */ | | |
| 5778 | data_offset += size; | 5813 | data_offset += size; |
| 5779 | /* handles bounds */ | 5814 | /* handles bounds */ |
| 5780 | if (do_bounds_check) { | 5815 | if (do_bounds_check) { |
| @@ -5783,39 +5818,54 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 2 unmodified lines |
| 5783 | data_offset++; | 5818 | data_offset++; |
| 5784 | /* then add global bound info */ | 5819 | /* then add global bound info */ |
| 5785 | bounds_ptr = (int *)bounds_section->data_ptr; | 5820 | bounds_ptr = (int *)bounds_section->data_ptr; |
| 5786 | *bounds_ptr++ = addr; | 5821 | /* XXX: add relocation */ |
| | 5822 | *bounds_ptr++ = addr + (unsigned long)sec->data; |
| 5787 | *bounds_ptr++ = size; | 5823 | *bounds_ptr++ = size; |
| 5788 | bounds_section->data_ptr = (unsigned char *)bounds_ptr; | 5824 | bounds_section->data_ptr = (unsigned char *)bounds_ptr; |
| 5789 | } | 5825 | } |
| 5790 | sec->data_ptr = (unsigned char *)data_offset; | 5826 | sec->data_ptr = sec->data + data_offset; |
| 5791 | } | 5827 | } |
| 5792 | if (v) { | 5828 | if (v) { |
| 5793 | Sym *sym; | 5829 | Sym *sym; |
| 5794 | if (scope == VT_CONST) { | 5830 | |
| 5795 | /* global scope: see if already defined */ | 5831 | if (!sec) { |
| 5796 | sym = sym_find(v); | 5832 | /* local variable */ |
| 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: | | |
| 5807 | sym_push(v, t, r, addr); | 5833 | 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); |
| 5808 | } | 5859 | } |
| 5809 | } | 5860 | } |
| 5810 | if (has_init) { | 5861 | if (has_init) { |
| 5811 | decl_initializer(t, r, addr, 1, 0); | 5862 | decl_initializer(t, sec, addr, 1, 0); |
| 5812 | /* restore parse state if needed */ | 5863 | /* restore parse state if needed */ |
| 5813 | if (init_str.str) { | 5864 | if (init_str.str) { |
| 5814 | free(init_str.str); | 5865 | free(init_str.str); |
| 5815 | restore_parse_state(&saved_parse_state); | 5866 | restore_parse_state(&saved_parse_state); |
| 5816 | } | 5867 | } |
| 5817 | } | 5868 | } |
| 5818 | return addr; | | |
| 5819 | } | 5869 | } |
| 5820 | | 5870 | |
| 5821 | void put_func_debug(int t) | 5871 | void put_func_debug(int t) |
| @@ -5894,7 +5944,7 @@ void analyse_function(void) · 72 unmodified lines |
| 5894 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ | 5944 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ |
| 5895 | void decl(int l) | 5945 | void decl(int l) |
| 5896 | { | 5946 | { |
| 5897 | int t, b, v, has_init, r, bind; | 5947 | int t, b, v, has_init, r; |
| 5898 | Sym *sym; | 5948 | Sym *sym; |
| 5899 | AttributeDef ad; | 5949 | AttributeDef ad; |
| 5900 | | 5950 | |
| @@ -5976,25 +6026,18 @@ void decl(int l) · 75 unmodified lines |
| 5976 | if (!cur_text_section) | 6026 | if (!cur_text_section) |
| 5977 | cur_text_section = text_section; | 6027 | cur_text_section = text_section; |
| 5978 | ind = (int)cur_text_section->data_ptr; | 6028 | ind = (int)cur_text_section->data_ptr; |
| 5979 | /* patch forward references */ | 6029 | funcname = get_tok_str(v, NULL); |
| 5980 | if ((sym = sym_find(v)) && (sym->r & VT_FORWARD)) { | 6030 | sym = sym_find(v); |
| 5981 | greloc_patch(sym, ind); | 6031 | if (sym) { |
| | 6032 | /* if symbol is already defined, then put complete type */ |
| 5982 | sym->t = t; | 6033 | sym->t = t; |
| 5983 | } else { | 6034 | } else { |
| 5984 | /* put function address */ | 6035 | /* put function symbol */ |
| 5985 | sym = sym_push1(&global_stack, v, t, ind); | 6036 | sym = sym_push1(&global_stack, v, t, 0); |
| 5986 | } | 6037 | } |
| 5987 | sym->r = VT_CONST; | 6038 | put_extern_sym(sym, cur_text_section, |
| 5988 | funcname = get_tok_str(v, NULL); | 6039 | ind - (int)cur_text_section->data); |
| 5989 | /* add symbol info */ | 6040 | sym->r = VT_SYM | VT_CONST; |
| 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 | | | |
| 5998 | /* put debug symbol */ | 6041 | /* put debug symbol */ |
| 5999 | if (do_debug) | 6042 | if (do_debug) |
| 6000 | put_func_debug(t); | 6043 | put_func_debug(t); |
| @@ -6064,52 +6107,6 @@ void decl(int l) · 63 unmodified lines |
| 6064 | } | 6107 | } |
| 6065 | } | 6108 | } |
| 6066 | | 6109 | |
| 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 | | | |
| 6113 | /* compile the C file opened in 'file'. Return non zero if errors. */ | 6110 | /* compile the C file opened in 'file'. Return non zero if errors. */ |
| 6114 | int tcc_compile(TCCState *s) | 6111 | int tcc_compile(TCCState *s) |
| 6115 | { | 6112 | { |
| @@ -6152,8 +6149,6 @@ int tcc_compile(TCCState *s) · 36 unmodified lines |
| 6152 | they are undefined) */ | 6149 | they are undefined) */ |
| 6153 | sym_pop(&define_stack, define_start); | 6150 | sym_pop(&define_stack, define_start); |
| 6154 | | 6151 | |
| 6155 | resolve_global_syms(); | | |
| 6156 | | | |
| 6157 | sym_pop(&global_stack, NULL); | 6152 | sym_pop(&global_stack, NULL); |
| 6158 | | 6153 | |
| 6159 | return 0; | 6154 | return 0; |
| @@ -6243,67 +6238,6 @@ int tcc_add_dll(TCCState *s, const char *library_name) · 83 unmodified lines |
| 6243 | return 0; | 6238 | return 0; |
| 6244 | } | 6239 | } |
| 6245 | | 6240 | |
| 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 | | | |
| 6307 | static int put_elf_str(Section *s, const char *sym) | 6241 | static int put_elf_str(Section *s, const char *sym) |
| 6308 | { | 6242 | { |
| 6309 | int c, offset; | 6243 | int c, offset; |
| @@ -6317,25 +6251,155 @@ static int put_elf_str(Section *s, const char *sym) · 7 unmodified lines |
| 6317 | return offset; | 6251 | return offset; |
| 6318 | } | 6252 | } |
| 6319 | | 6253 | |
| 6320 | static void put_elf_sym(Section *s, | 6254 | /* elf symbol hashing function */ |
| 6321 | unsigned long value, unsigned long size, | 6255 | static unsigned long elf_hash(const unsigned char *name) |
| 6322 | int info, int other, int shndx, const char *name) | | |
| 6323 | { | 6256 | { |
| 6324 | int name_offset; | 6257 | unsigned long h = 0, g; |
| 6325 | Elf32_Sym *sym; | 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 | } |
| 6326 | | 6268 | |
| | 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 | |
| 6327 | sym = (Elf32_Sym *)s->data_ptr; | 6278 | sym = (Elf32_Sym *)s->data_ptr; |
| 6328 | if (name) | 6279 | if (name) |
| 6329 | name_offset = put_elf_str(s->link, name); | 6280 | name_offset = put_elf_str(s->link, name); |
| 6330 | else | 6281 | else |
| 6331 | name_offset = 0; | 6282 | name_offset = 0; |
| | 6283 | /* XXX: endianness */ |
| 6332 | sym->st_name = name_offset; | 6284 | sym->st_name = name_offset; |
| 6333 | sym->st_value = value; | 6285 | sym->st_value = value; |
| 6334 | sym->st_size = size; | 6286 | sym->st_size = size; |
| 6335 | sym->st_info = info; | 6287 | sym->st_info = info; |
| 6336 | sym->st_other = other; | 6288 | sym->st_other = other; |
| 6337 | sym->st_shndx = shndx; | 6289 | 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 | } |
| 6338 | s->data_ptr += sizeof(Elf32_Sym); | 6300 | 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); |
| 6339 | } | 6403 | } |
| 6340 | | 6404 | |
| 6341 | /* put stab debug information */ | 6405 | /* put stab debug information */ |
| @@ -6427,6 +6491,8 @@ int tcc_output_file(TCCState *s, const char *filename, int file_type) · 85 unmodified lines |
| 6427 | sh->sh_entsize = sec->sh_entsize; | 6491 | sh->sh_entsize = sec->sh_entsize; |
| 6428 | if (sec->link) | 6492 | if (sec->link) |
| 6429 | sh->sh_link = sec->link->sh_num; | 6493 | sh->sh_link = sec->link->sh_num; |
| | 6494 | if (sec->reloc_sec) |
| | 6495 | sh->sh_info = sec->reloc_sec->sh_num; |
| 6430 | if (sh->sh_type == SHT_STRTAB) { | 6496 | if (sh->sh_type == SHT_STRTAB) { |
| 6431 | sh->sh_addralign = 1; | 6497 | sh->sh_addralign = 1; |
| 6432 | } else if (sh->sh_type == SHT_SYMTAB || | 6498 | } 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 |
| 6677 | } | 6743 | } |
| 6678 | #endif | 6744 | #endif |
| 6679 | | 6745 | |
| | 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 | |
| 6680 | /* launch the compiled program with the given arguments */ | 6831 | /* launch the compiled program with the given arguments */ |
| 6681 | int tcc_run(TCCState *s1, int argc, char **argv) | 6832 | int tcc_run(TCCState *s1, int argc, char **argv) |
| 6682 | { | 6833 | { |
| 6683 | Sym *s; | 6834 | int (*prog_main)(int, char **); |
| 6684 | int (*t)(); | 6835 | int sym_index; |
| 6685 | | 6836 | |
| 6686 | resolve_extern_syms(); | 6837 | resolve_extern_syms(); |
| 6687 | | 6838 | |
| 6688 | s = sym_find1(&extern_stack, TOK_MAIN); | 6839 | relocate_program(); |
| 6689 | if (!s || (s->r & VT_FORWARD)) | 6840 | |
| | 6841 | sym_index = find_elf_sym(symtab_section, "main"); |
| | 6842 | if (!sym_index) |
| 6690 | error("main() not defined"); | 6843 | error("main() not defined"); |
| | 6844 | prog_main = (void *)get_sym_val(sym_index); |
| 6691 | | 6845 | |
| 6692 | if (do_debug) { | 6846 | if (do_debug) { |
| 6693 | #ifdef WIN32 | 6847 | #ifdef WIN32 |
| @@ -6721,8 +6875,7 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 27 unmodified lines |
| 6721 | } | 6875 | } |
| 6722 | #endif | 6876 | #endif |
| 6723 | | 6877 | |
| 6724 | t = (int (*)())s->c; | 6878 | return (*prog_main)(argc, argv); |
| 6725 | return (*t)(argc, argv); | | |
| 6726 | } | 6879 | } |
| 6727 | | 6880 | |
| 6728 | TCCState *tcc_new(void) | 6881 | TCCState *tcc_new(void) |
| @@ -6758,6 +6911,7 @@ TCCState *tcc_new(void) · 29 unmodified lines |
| 6758 | tcc_define_symbol(s, "__TINYC__", NULL); | 6911 | tcc_define_symbol(s, "__TINYC__", NULL); |
| 6759 | | 6912 | |
| 6760 | /* create standard sections */ | 6913 | /* create standard sections */ |
| | 6914 | nb_sections = 1; |
| 6761 | text_section = new_section(".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR); | 6915 | text_section = new_section(".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR); |
| 6762 | data_section = new_section(".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE); | 6916 | data_section = new_section(".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE); |
| 6763 | /* XXX: should change type to SHT_NOBITS */ | 6917 | /* XXX: should change type to SHT_NOBITS */ |
| @@ -6770,7 +6924,13 @@ TCCState *tcc_new(void) · 6 unmodified lines |
| 6770 | put_elf_str(strtab_section, ""); | 6924 | put_elf_str(strtab_section, ""); |
| 6771 | symtab_section->link = strtab_section; | 6925 | symtab_section->link = strtab_section; |
| 6772 | put_elf_sym(symtab_section, 0, 0, 0, 0, 0, NULL); | 6926 | 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; |
| 6774 | return s; | 6934 | return s; |
| 6775 | } | 6935 | } |
| 6776 | | 6936 | |