Unified · Split
| @@ -135,8 +135,8 @@ typedef struct SymStack { · 134 unmodified lines | ||
| 135 | 135 | #define SHF_PRIVATE 0x80000000 |
| 136 | 136 | |
| 137 | 137 | typedef struct Section { |
| 138 | − unsigned char *data; /* section data */ | |
| 139 | − unsigned char *data_ptr; /* current data pointer */ | |
| 138 | + unsigned long data_offset; /* current data offset */ | |
| 139 | + unsigned char *data; /* section data */ | |
| 140 | 140 | int sh_name; /* elf section name (only used during output) */ |
| 141 | 141 | int sh_num; /* elf section number */ |
| 142 | 142 | int sh_type; /* elf section type */ |
| @@ -197,7 +197,7 @@ typedef struct BufferedFile { · 54 unmodified lines | ||
| 197 | 197 | unsigned char *buf_end; |
| 198 | 198 | int fd; |
| 199 | 199 | int line_num; /* current line number - here to simply code */ |
| 200 | − char filename[1024]; /* current filename - here to simply code */ | |
| 200 | + char filename[1024]; /* current filename - here to simplify code */ | |
| 201 | 201 | unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */ |
| 202 | 202 | } BufferedFile; |
| 203 | 203 | |
| @@ -337,7 +337,7 @@ struct TCCState { · 133 unmodified lines | ||
| 337 | 337 | #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED) |
| 338 | 338 | |
| 339 | 339 | /* types */ |
| 340 | −#define VT_STRUCT_SHIFT 16 /* structure/enum name shift (16 bits left) */ | |
| 340 | +#define VT_STRUCT_SHIFT 12 /* structure/enum name shift (20 bits left) */ | |
| 341 | 341 | |
| 342 | 342 | #define VT_INT 0 /* integer type */ |
| 343 | 343 | #define VT_BYTE 1 /* signed byte type */ |
| @@ -594,6 +594,8 @@ static Sym *get_sym_ref(int t, Section *sec, · 250 unmodified lines | ||
| 594 | 594 | unsigned long offset, unsigned long size); |
| 595 | 595 | |
| 596 | 596 | /* section generation */ |
| 597 | +static void *section_ptr(Section *sec, unsigned long size); | |
| 598 | +static void *section_ptr_add(Section *sec, unsigned long size); | |
| 597 | 599 | void put_extern_sym(Sym *sym, Section *section, unsigned long value); |
| 598 | 600 | void greloc(Section *s, Sym *sym, unsigned long addr, int type); |
| 599 | 601 | static int put_elf_str(Section *s, const char *sym); |
| @@ -602,7 +604,10 @@ static int put_elf_sym(Section *s, · 2 unmodified lines | ||
| 602 | 604 | int info, int other, int shndx, const char *name); |
| 603 | 605 | static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, |
| 604 | 606 | int type, int symbol); |
| 605 | −static void put_stabs(const char *str, int type, int other, int desc, int value); | |
| 607 | +static void put_stabs(const char *str, int type, int other, int desc, | |
| 608 | + unsigned long value); | |
| 609 | +static void put_stabs_r(const char *str, int type, int other, int desc, | |
| 610 | + unsigned long value, Section *sec, int sym_index); | |
| 606 | 611 | static void put_stabn(int type, int other, int desc, int value); |
| 607 | 612 | static void put_stabd(int type, int other, int desc); |
| 608 | 613 | static int tcc_add_dll(TCCState *s, const char *filename, int flags); |
| @@ -777,7 +782,7 @@ Section *new_section(const char *name, int sh_type, int sh_flags) · 168 unmodified lines | ||
| 777 | 782 | error("could not mmap section '%s'", name); |
| 778 | 783 | #endif |
| 779 | 784 | sec->data = data; |
| 780 | − sec->data_ptr = data; | |
| 785 | + sec->data_offset = 0; | |
| 781 | 786 | |
| 782 | 787 | /* only add section if not private */ |
| 783 | 788 | if (!(sh_flags & SHF_PRIVATE)) { |
| @@ -787,6 +792,20 @@ Section *new_section(const char *name, int sh_type, int sh_flags) · 3 unmodified lines | ||
| 787 | 792 | return sec; |
| 788 | 793 | } |
| 789 | 794 | |
| 795 | +/* reserve at least 'size' bytes in section 'sec' from sec->data_offset */ | |
| 796 | +static void *section_ptr(Section *sec, unsigned long size) | |
| 797 | +{ | |
| 798 | + return sec->data + sec->data_offset; | |
| 799 | +} | |
| 800 | + | |
| 801 | +static void *section_ptr_add(Section *sec, unsigned long size) | |
| 802 | +{ | |
| 803 | + void *ptr; | |
| 804 | + ptr = sec->data + sec->data_offset; | |
| 805 | + sec->data_offset += size; | |
| 806 | + return ptr; | |
| 807 | +} | |
| 808 | + | |
| 790 | 809 | /* return a reference to a section, and create it if it does not |
| 791 | 810 | exists */ |
| 792 | 811 | Section *find_section(const char *name) |
| @@ -1668,7 +1687,7 @@ void preprocess(void) · 875 unmodified lines | ||
| 1668 | 1687 | } |
| 1669 | 1688 | |
| 1670 | 1689 | /* read a number in base b */ |
| 1671 | −int getn(b) | |
| 1690 | +static int getn(int b) | |
| 1672 | 1691 | { |
| 1673 | 1692 | int n, t; |
| 1674 | 1693 | n = 0; |
| @@ -1690,7 +1709,7 @@ int getn(b) · 15 unmodified lines | ||
| 1690 | 1709 | } |
| 1691 | 1710 | |
| 1692 | 1711 | /* read a character for string or char constant and eval escape codes */ |
| 1693 | −int getq() | |
| 1712 | +static int getq(void) | |
| 1694 | 1713 | { |
| 1695 | 1714 | int c; |
| 1696 | 1715 | |
| @@ -2094,7 +2113,11 @@ void next_nomacro1(void) · 397 unmodified lines | ||
| 2094 | 2113 | tok = TOK_CCHAR; |
| 2095 | 2114 | char_const: |
| 2096 | 2115 | minp(); |
| 2097 | − tokc.i = getq(); | |
| 2116 | + b = getq(); | |
| 2117 | + /* this cast is needed if >= 128 */ | |
| 2118 | + if (tok == TOK_CCHAR) | |
| 2119 | + b = (char)b; | |
| 2120 | + tokc.i = b; | |
| 2098 | 2121 | if (ch != '\'') |
| 2099 | 2122 | expect("\'"); |
| 2100 | 2123 | minp(); |
| @@ -2718,7 +2741,7 @@ void gbound(void) · 617 unmodified lines | ||
| 2718 | 2741 | register value (such as structures). */ |
| 2719 | 2742 | int gv(int rc) |
| 2720 | 2743 | { |
| 2721 | − int r, r2, rc2, bit_pos, bit_size, size, align, i, data_offset; | |
| 2744 | + int r, r2, rc2, bit_pos, bit_size, size, align, i; | |
| 2722 | 2745 | unsigned long long ll; |
| 2723 | 2746 | |
| 2724 | 2747 | /* NOTE: get_reg can modify vstack[] */ |
| @@ -2739,27 +2762,28 @@ int gv(int rc) · 14 unmodified lines | ||
| 2739 | 2762 | (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { |
| 2740 | 2763 | int v; |
| 2741 | 2764 | Sym *sym; |
| 2765 | + int *ptr; | |
| 2742 | 2766 | |
| 2743 | 2767 | /* XXX: unify with initializers handling ? */ |
| 2744 | 2768 | /* CPUs usually cannot use float constants, so we store them |
| 2745 | 2769 | generically in data segment */ |
| 2746 | 2770 | size = type_size(vtop->t, &align); |
| 2747 | − data_offset = data_section->data_ptr - data_section->data; | |
| 2748 | − data_offset = (data_offset + align - 1) & -align; | |
| 2771 | + data_section->data_offset = (data_section->data_offset + | |
| 2772 | + align - 1) & -align; | |
| 2749 | 2773 | /* XXX: not portable yet */ |
| 2774 | + ptr = section_ptr(data_section, size); | |
| 2750 | 2775 | size = size >> 2; |
| 2751 | 2776 | for(i=0;i<size;i++) |
| 2752 | − ((int *)(data_section->data + data_offset))[i] = vtop->c.tab[i]; | |
| 2777 | + ptr[i] = vtop->c.tab[i]; | |
| 2753 | 2778 | |
| 2754 | 2779 | v = anon_sym++; |
| 2755 | 2780 | sym = sym_push1(&global_stack, v, vtop->t | VT_STATIC, 0); |
| 2756 | 2781 | sym->r = VT_CONST | VT_SYM; |
| 2757 | − put_extern_sym(sym, data_section, data_offset); | |
| 2782 | + put_extern_sym(sym, data_section, data_section->data_offset); | |
| 2758 | 2783 | |
| 2759 | 2784 | vtop->r |= VT_LVAL | VT_SYM; |
| 2760 | 2785 | vtop->c.sym = sym; |
| 2761 | − data_offset += size << 2; | |
| 2762 | − data_section->data_ptr = data_section->data + data_offset; | |
| 2786 | + data_section->data_offset += size << 2; | |
| 2763 | 2787 | } |
| 2764 | 2788 | #ifdef CONFIG_TCC_BCHECK |
| 2765 | 2789 | if (vtop->r & VT_MUSTBOUND) |
| @@ -4673,13 +4697,18 @@ void unary(void) · 1907 unmodified lines | ||
| 4673 | 4697 | vsetc(VT_LDOUBLE, VT_CONST, &tokc); |
| 4674 | 4698 | next(); |
| 4675 | 4699 | } else if (tok == TOK___FUNC__) { |
| 4700 | + void *ptr; | |
| 4701 | + int len; | |
| 4676 | 4702 | /* special function name identifier */ |
| 4677 | − /* generate (char *) type */ | |
| 4678 | − data_offset = data_section->data_ptr - data_section->data; | |
| 4679 | − vpush_ref(mk_pointer(VT_BYTE), data_section, data_offset); | |
| 4680 | − strcpy(data_section->data + data_offset, funcname); | |
| 4681 | − data_offset += strlen(funcname) + 1; | |
| 4682 | − data_section->data_ptr = data_section->data + data_offset; | |
| 4703 | + | |
| 4704 | + len = strlen(funcname) + 1; | |
| 4705 | + /* generate char[len] type */ | |
| 4706 | + t = VT_ARRAY | mk_pointer(VT_BYTE); | |
| 4707 | + s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT)); | |
| 4708 | + s->c = len; | |
| 4709 | + vpush_ref(t, data_section, data_section->data_offset); | |
| 4710 | + ptr = section_ptr_add(data_section, len); | |
| 4711 | + memcpy(ptr, funcname, len); | |
| 4683 | 4712 | next(); |
| 4684 | 4713 | } else if (tok == TOK_LSTR) { |
| 4685 | 4714 | t = VT_INT; |
| @@ -4689,16 +4718,16 @@ void unary(void) · 3 unmodified lines | ||
| 4689 | 4718 | t = VT_BYTE; |
| 4690 | 4719 | str_init: |
| 4691 | 4720 | type_size(t, &align); |
| 4692 | − data_offset = data_section->data_ptr - data_section->data; | |
| 4721 | + data_offset = data_section->data_offset; | |
| 4693 | 4722 | data_offset = (data_offset + align - 1) & -align; |
| 4694 | 4723 | fc = data_offset; |
| 4695 | 4724 | /* we must declare it as an array first to use initializer parser */ |
| 4696 | 4725 | t = VT_ARRAY | mk_pointer(t); |
| 4697 | 4726 | decl_initializer(t, data_section, data_offset, 1, 0); |
| 4698 | 4727 | data_offset += type_size(t, &align); |
| 4699 | − /* put it as pointer */ | |
| 4700 | − vpush_ref(t & ~VT_ARRAY, data_section, fc); | |
| 4701 | − data_section->data_ptr = data_section->data + data_offset; | |
| 4728 | + /* XXX: is it correct to put it exactly as an array ? */ | |
| 4729 | + vpush_ref(t, data_section, fc); | |
| 4730 | + data_section->data_offset = data_offset; | |
| 4702 | 4731 | } else { |
| 4703 | 4732 | t = tok; |
| 4704 | 4733 | next(); |
| @@ -4758,7 +4787,12 @@ void unary(void) · 53 unmodified lines | ||
| 4758 | 4787 | gen_op('^'); |
| 4759 | 4788 | } else |
| 4760 | 4789 | if (t == '+') { |
| 4790 | + /* in order to force cast, we add zero */ | |
| 4761 | 4791 | unary(); |
| 4792 | + if ((vtop->t & VT_BTYPE) == VT_PTR) | |
| 4793 | + error("pointer not accepted for unary plus"); | |
| 4794 | + vpushi(0); | |
| 4795 | + gen_op('+'); | |
| 4762 | 4796 | } else |
| 4763 | 4797 | if (t == TOK_SIZEOF) { |
| 4764 | 4798 | if (tok == '(') { |
| @@ -5811,14 +5845,13 @@ void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 1046 unmodified lines | ||
| 5811 | 5845 | /* XXX: currently, since we do only one pass, we cannot track |
| 5812 | 5846 | '&' operators, so we add only arrays */ |
| 5813 | 5847 | if (do_bounds_check && (t & VT_ARRAY)) { |
| 5814 | − int *bounds_ptr; | |
| 5848 | + unsigned long *bounds_ptr; | |
| 5815 | 5849 | /* add padding between regions */ |
| 5816 | 5850 | loc--; |
| 5817 | 5851 | /* then add local bound info */ |
| 5818 | − bounds_ptr = (int *)lbounds_section->data_ptr; | |
| 5819 | − *bounds_ptr++ = addr; | |
| 5820 | − *bounds_ptr++ = size; | |
| 5821 | − lbounds_section->data_ptr = (unsigned char *)bounds_ptr; | |
| 5852 | + bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long)); | |
| 5853 | + bounds_ptr[0] = addr; | |
| 5854 | + bounds_ptr[1] = size; | |
| 5822 | 5855 | } |
| 5823 | 5856 | } else { |
| 5824 | 5857 | /* compute section */ |
| @@ -5829,33 +5862,29 @@ void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 4 unmodified lines | ||
| 5829 | 5862 | else |
| 5830 | 5863 | sec = bss_section; |
| 5831 | 5864 | } |
| 5832 | − data_offset = sec->data_ptr - sec->data; | |
| 5865 | + data_offset = sec->data_offset; | |
| 5833 | 5866 | data_offset = (data_offset + align - 1) & -align; |
| 5834 | 5867 | addr = data_offset; |
| 5835 | 5868 | /* very important to increment global pointer at this time |
| 5836 | 5869 | because initializers themselves can create new initializers */ |
| 5837 | 5870 | data_offset += size; |
| 5838 | − /* handles bounds */ | |
| 5839 | − if (do_bounds_check) { | |
| 5840 | − int *bounds_ptr; | |
| 5841 | − /* first, we need to add at least one byte between each region */ | |
| 5871 | + /* add padding if bound check */ | |
| 5872 | + if (do_bounds_check) | |
| 5842 | 5873 | data_offset++; |
| 5843 | − /* then add global bound info */ | |
| 5844 | − bounds_ptr = (int *)bounds_section->data_ptr; | |
| 5845 | − /* XXX: add relocation */ | |
| 5846 | − *bounds_ptr++ = addr + (unsigned long)sec->data; | |
| 5847 | − *bounds_ptr++ = size; | |
| 5848 | − bounds_section->data_ptr = (unsigned char *)bounds_ptr; | |
| 5849 | − } | |
| 5850 | − sec->data_ptr = sec->data + data_offset; | |
| 5874 | + sec->data_offset = data_offset; | |
| 5851 | 5875 | } |
| 5852 | − if (v) { | |
| 5853 | − Sym *sym; | |
| 5854 | − | |
| 5855 | − if (!sec) { | |
| 5876 | + if (!sec) { | |
| 5877 | + if (v) { | |
| 5856 | 5878 | /* local variable */ |
| 5857 | 5879 | sym_push(v, t, r, addr); |
| 5858 | 5880 | } else { |
| 5881 | + /* push local reference */ | |
| 5882 | + vset(t, r, addr); | |
| 5883 | + } | |
| 5884 | + } else { | |
| 5885 | + Sym *sym; | |
| 5886 | + | |
| 5887 | + if (v) { | |
| 5859 | 5888 | if (scope == VT_CONST) { |
| 5860 | 5889 | /* global scope: see if already defined */ |
| 5861 | 5890 | sym = sym_find(v); |
| @@ -5872,14 +5901,25 @@ void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 10 unmodified lines | ||
| 5872 | 5901 | sym = sym_push(v, t, r | VT_SYM, 0); |
| 5873 | 5902 | } |
| 5874 | 5903 | put_extern_sym(sym, sec, addr); |
| 5875 | − } | |
| 5876 | − } else { | |
| 5877 | − if (!sec) { | |
| 5878 | − /* push local reference */ | |
| 5879 | − vset(t, r, addr); | |
| 5880 | 5904 | } else { |
| 5905 | + CValue cval; | |
| 5906 | + | |
| 5881 | 5907 | /* push global reference */ |
| 5882 | − vpush_ref(t, sec, addr); | |
| 5908 | + sym = get_sym_ref(t, sec, addr, 0); | |
| 5909 | + cval.sym = sym; | |
| 5910 | + vsetc(t, VT_CONST | VT_SYM, &cval); | |
| 5911 | + } | |
| 5912 | + | |
| 5913 | + /* handles bounds now because the symbol must be defined | |
| 5914 | + before for the relocation */ | |
| 5915 | + if (do_bounds_check) { | |
| 5916 | + unsigned long *bounds_ptr; | |
| 5917 | + | |
| 5918 | + greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32); | |
| 5919 | + /* then add global bound info */ | |
| 5920 | + bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long)); | |
| 5921 | + bounds_ptr[0] = 0; /* relocated */ | |
| 5922 | + bounds_ptr[1] = size; | |
| 5883 | 5923 | } |
| 5884 | 5924 | } |
| 5885 | 5925 | if (has_init) { |
| @@ -5892,15 +5932,16 @@ void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 6 unmodified lines | ||
| 5892 | 5932 | } |
| 5893 | 5933 | } |
| 5894 | 5934 | |
| 5895 | −void put_func_debug(int t) | |
| 5935 | +void put_func_debug(Sym *sym) | |
| 5896 | 5936 | { |
| 5897 | 5937 | char buf[512]; |
| 5898 | 5938 | |
| 5899 | 5939 | /* stabs info */ |
| 5900 | 5940 | /* XXX: we put here a dummy type */ |
| 5901 | 5941 | snprintf(buf, sizeof(buf), "%s:%c1", |
| 5902 | − funcname, t & VT_STATIC ? 'f' : 'F'); | |
| 5903 | − put_stabs(buf, N_FUN, 0, file->line_num, ind); | |
| 5942 | + funcname, sym->t & VT_STATIC ? 'f' : 'F'); | |
| 5943 | + put_stabs_r(buf, N_FUN, 0, file->line_num, 0, | |
| 5944 | + cur_text_section, sym->c); | |
| 5904 | 5945 | func_ind = ind; |
| 5905 | 5946 | last_ind = 0; |
| 5906 | 5947 | last_line_num = 0; |
| @@ -6049,7 +6090,7 @@ void decl(int l) · 142 unmodified lines | ||
| 6049 | 6090 | cur_text_section = ad.section; |
| 6050 | 6091 | if (!cur_text_section) |
| 6051 | 6092 | cur_text_section = text_section; |
| 6052 | − ind = (int)cur_text_section->data_ptr; | |
| 6093 | + ind = cur_text_section->data_offset; | |
| 6053 | 6094 | funcname = get_tok_str(v, NULL); |
| 6054 | 6095 | sym = sym_find(v); |
| 6055 | 6096 | if (sym) { |
| @@ -6059,12 +6100,11 @@ void decl(int l) · 3 unmodified lines | ||
| 6059 | 6100 | /* put function symbol */ |
| 6060 | 6101 | sym = sym_push1(&global_stack, v, t, 0); |
| 6061 | 6102 | } |
| 6062 | − put_extern_sym(sym, cur_text_section, | |
| 6063 | − ind - (int)cur_text_section->data); | |
| 6103 | + put_extern_sym(sym, cur_text_section, ind); | |
| 6064 | 6104 | sym->r = VT_SYM | VT_CONST; |
| 6065 | 6105 | /* put debug symbol */ |
| 6066 | 6106 | if (do_debug) |
| 6067 | − put_func_debug(t); | |
| 6107 | + put_func_debug(sym); | |
| 6068 | 6108 | /* push a dummy symbol to enable local sym storage */ |
| 6069 | 6109 | sym_push1(&local_stack, 0, 0, 0); |
| 6070 | 6110 | gfunc_prolog(t); |
| @@ -6077,7 +6117,7 @@ void decl(int l) · 6 unmodified lines | ||
| 6077 | 6117 | block(NULL, NULL, NULL, NULL, 0); |
| 6078 | 6118 | gsym(rsym); |
| 6079 | 6119 | gfunc_epilog(); |
| 6080 | − cur_text_section->data_ptr = (unsigned char *)ind; | |
| 6120 | + cur_text_section->data_offset = ind; | |
| 6081 | 6121 | sym_pop(&label_stack, NULL); /* reset label stack */ |
| 6082 | 6122 | sym_pop(&local_stack, NULL); /* reset local stack */ |
| 6083 | 6123 | /* end of function */ |
| @@ -6136,7 +6176,7 @@ static int tcc_compile(TCCState *s) · 52 unmodified lines | ||
| 6136 | 6176 | { |
| 6137 | 6177 | Sym *define_start; |
| 6138 | 6178 | char buf[512]; |
| 6139 | − int p; | |
| 6179 | + int p, section_sym; | |
| 6140 | 6180 | |
| 6141 | 6181 | funcname = ""; |
| 6142 | 6182 | include_stack_ptr = include_stack; |
| @@ -6146,12 +6186,17 @@ static int tcc_compile(TCCState *s) · 3 unmodified lines | ||
| 6146 | 6186 | anon_sym = SYM_FIRST_ANOM; |
| 6147 | 6187 | |
| 6148 | 6188 | /* file info: full path + filename */ |
| 6189 | + section_sym = 0; /* avoid warning */ | |
| 6149 | 6190 | if (do_debug) { |
| 6191 | + section_sym = put_elf_sym(symtab_section, 0, 0, | |
| 6192 | + ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0, | |
| 6193 | + text_section->sh_num, NULL); | |
| 6150 | 6194 | getcwd(buf, sizeof(buf)); |
| 6151 | 6195 | pstrcat(buf, sizeof(buf), "/"); |
| 6152 | − put_stabs(buf, N_SO, 0, 0, (unsigned long)text_section->data_ptr); | |
| 6153 | − put_stabs(file->filename, N_SO, 0, 0, | |
| 6154 | − (unsigned long)text_section->data_ptr); | |
| 6196 | + put_stabs_r(buf, N_SO, 0, 0, | |
| 6197 | + text_section->data_offset, text_section, section_sym); | |
| 6198 | + put_stabs_r(file->filename, N_SO, 0, 0, | |
| 6199 | + text_section->data_offset, text_section, section_sym); | |
| 6155 | 6200 | } |
| 6156 | 6201 | /* an elf symbol of type STT_FILE must be put so that STB_LOCAL |
| 6157 | 6202 | symbols can be safely used */ |
| @@ -6177,7 +6222,8 @@ static int tcc_compile(TCCState *s) · 19 unmodified lines | ||
| 6177 | 6222 | |
| 6178 | 6223 | /* end of translation unit info */ |
| 6179 | 6224 | if (do_debug) { |
| 6180 | − put_stabn(N_SO, 0, 0, (unsigned long)text_section->data_ptr); | |
| 6225 | + put_stabs_r(NULL, N_SO, 0, 0, | |
| 6226 | + text_section->data_offset, text_section, section_sym); | |
| 6181 | 6227 | } |
| 6182 | 6228 | |
| 6183 | 6229 | /* reset define stack, but leave -Dsymbols (may be incorrect if |
| @@ -6251,14 +6297,13 @@ void tcc_undefine_symbol(TCCState *s1, const char *sym) · 67 unmodified lines | ||
| 6251 | 6297 | |
| 6252 | 6298 | static int put_elf_str(Section *s, const char *sym) |
| 6253 | 6299 | { |
| 6254 | − int c, offset; | |
| 6255 | − offset = s->data_ptr - s->data; | |
| 6256 | − for(;;) { | |
| 6257 | − c = *sym++; | |
| 6258 | − *s->data_ptr++ = c; | |
| 6259 | − if (c == '\0') | |
| 6260 | − break; | |
| 6261 | − } | |
| 6300 | + int offset, len; | |
| 6301 | + char *ptr; | |
| 6302 | + | |
| 6303 | + len = strlen(sym) + 1; | |
| 6304 | + offset = s->data_offset; | |
| 6305 | + ptr = section_ptr_add(s, len); | |
| 6306 | + memcpy(ptr, sym, len); | |
| 6262 | 6307 | return offset; |
| 6263 | 6308 | } |
| 6264 | 6309 | |
| @@ -6287,7 +6332,7 @@ static int put_elf_sym(Section *s, · 22 unmodified lines | ||
| 6287 | 6332 | Elf32_Sym *sym; |
| 6288 | 6333 | Section *hs; |
| 6289 | 6334 | |
| 6290 | − sym = (Elf32_Sym *)s->data_ptr; | |
| 6335 | + sym = section_ptr_add(s, sizeof(Elf32_Sym)); | |
| 6291 | 6336 | if (name) |
| 6292 | 6337 | name_offset = put_elf_str(s->link, name); |
| 6293 | 6338 | else |
| @@ -6302,19 +6347,21 @@ static int put_elf_sym(Section *s, · 8 unmodified lines | ||
| 6302 | 6347 | sym_index = sym - (Elf32_Sym *)s->data; |
| 6303 | 6348 | hs = s->hash; |
| 6304 | 6349 | if (hs) { |
| 6350 | + int *ptr; | |
| 6351 | + ptr = section_ptr_add(hs, sizeof(int)); | |
| 6305 | 6352 | /* only add global or weak symbols */ |
| 6306 | 6353 | if (ELF32_ST_BIND(info) != STB_LOCAL) { |
| 6307 | 6354 | /* add another hashing entry */ |
| 6308 | 6355 | nbuckets = ((int *)hs->data)[0]; |
| 6309 | 6356 | h = elf_hash(name) % nbuckets; |
| 6310 | − ((int *)hs->data)[2 + nbuckets + sym_index] = ((int *)hs->data)[2 + h]; | |
| 6357 | + *ptr = ((int *)hs->data)[2 + h]; | |
| 6311 | 6358 | ((int *)hs->data)[2 + h] = sym_index; |
| 6359 | + } else { | |
| 6360 | + *ptr = 0; | |
| 6312 | 6361 | } |
| 6313 | 6362 | /* but still add room for all symbols */ |
| 6314 | 6363 | ((int *)hs->data)[1]++; |
| 6315 | − hs->data_ptr += sizeof(int); | |
| 6316 | 6364 | } |
| 6317 | − s->data_ptr += sizeof(Elf32_Sym); | |
| 6318 | 6365 | return sym_index; |
| 6319 | 6366 | } |
| 6320 | 6367 | |
| @@ -6483,11 +6530,9 @@ static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, · 162 unmodified lines | ||
| 6483 | 6530 | sr->sh_info = s->sh_num; |
| 6484 | 6531 | s->reloc = sr; |
| 6485 | 6532 | } |
| 6486 | − rel = (Elf32_Rel *)sr->data_ptr; | |
| 6487 | − /* XXX: endianness */ | |
| 6533 | + rel = section_ptr_add(sr, sizeof(Elf32_Rel)); | |
| 6488 | 6534 | rel->r_offset = offset; |
| 6489 | 6535 | rel->r_info = ELF32_R_INFO(symbol, type); |
| 6490 | − sr->data_ptr += sizeof(Elf32_Rel); | |
| 6491 | 6536 | } |
| 6492 | 6537 | |
| 6493 | 6538 | /* put stab debug information */ |
| @@ -6500,11 +6545,12 @@ typedef struct { · 6 unmodified lines | ||
| 6500 | 6545 | unsigned long n_value; /* value of symbol */ |
| 6501 | 6546 | } Stab_Sym; |
| 6502 | 6547 | |
| 6503 | −static void put_stabs(const char *str, int type, int other, int desc, int value) | |
| 6548 | +static void put_stabs(const char *str, int type, int other, int desc, | |
| 6549 | + unsigned long value) | |
| 6504 | 6550 | { |
| 6505 | 6551 | Stab_Sym *sym; |
| 6506 | 6552 | |
| 6507 | − sym = (Stab_Sym *)stab_section->data_ptr; | |
| 6553 | + sym = section_ptr_add(stab_section, sizeof(Stab_Sym)); | |
| 6508 | 6554 | if (str) { |
| 6509 | 6555 | sym->n_strx = put_elf_str(stabstr_section, str); |
| 6510 | 6556 | } else { |
| @@ -6514,8 +6560,15 @@ static void put_stabs(const char *str, int type, int other, int desc, int value) · 3 unmodified lines | ||
| 6514 | 6560 | sym->n_other = other; |
| 6515 | 6561 | sym->n_desc = desc; |
| 6516 | 6562 | sym->n_value = value; |
| 6563 | +} | |
| 6517 | 6564 | |
| 6518 | − stab_section->data_ptr += sizeof(Stab_Sym); | |
| 6565 | +static void put_stabs_r(const char *str, int type, int other, int desc, | |
| 6566 | + unsigned long value, Section *sec, int sym_index) | |
| 6567 | +{ | |
| 6568 | + put_stabs(str, type, other, desc, value); | |
| 6569 | + put_elf_reloc(symtab_section, stab_section, | |
| 6570 | + stab_section->data_offset - sizeof(unsigned long), | |
| 6571 | + R_DATA_32, sym_index); | |
| 6519 | 6572 | } |
| 6520 | 6573 | |
| 6521 | 6574 | static void put_stabn(int type, int other, int desc, int value) |
| @@ -6538,11 +6591,11 @@ static void sort_syms(Section *s) · 16 unmodified lines | ||
| 6538 | 6591 | Elf32_Sym *new_syms; |
| 6539 | 6592 | int nb_syms, i; |
| 6540 | 6593 | Elf32_Sym *p, *q; |
| 6541 | − Elf32_Rel *rel; | |
| 6594 | + Elf32_Rel *rel, *rel_end; | |
| 6542 | 6595 | Section *sr; |
| 6543 | 6596 | int type, sym_index; |
| 6544 | 6597 | |
| 6545 | − nb_syms = (s->data_ptr - s->data) / sizeof(Elf32_Sym); | |
| 6598 | + nb_syms = s->data_offset / sizeof(Elf32_Sym); | |
| 6546 | 6599 | new_syms = malloc(nb_syms * sizeof(Elf32_Sym)); |
| 6547 | 6600 | if (!new_syms) |
| 6548 | 6601 | error("memory full"); |
| @@ -6580,8 +6633,9 @@ static void sort_syms(Section *s) · 31 unmodified lines | ||
| 6580 | 6633 | for(i = 1; i < nb_sections; i++) { |
| 6581 | 6634 | sr = sections[i]; |
| 6582 | 6635 | if (sr->sh_type == SHT_REL && sr->link == s) { |
| 6636 | + rel_end = (Elf32_Rel *)(sr->data + sr->data_offset); | |
| 6583 | 6637 | for(rel = (Elf32_Rel *)sr->data; |
| 6584 | − rel < (Elf32_Rel *)sr->data_ptr; | |
| 6638 | + rel < rel_end; | |
| 6585 | 6639 | rel++) { |
| 6586 | 6640 | sym_index = ELF32_R_SYM(rel->r_info); |
| 6587 | 6641 | type = ELF32_R_TYPE(rel->r_info); |
| @@ -6597,21 +6651,22 @@ static void sort_syms(Section *s) · 9 unmodified lines | ||
| 6597 | 6651 | /* relocate common symbols in the .bss section */ |
| 6598 | 6652 | static void relocate_common_syms(void) |
| 6599 | 6653 | { |
| 6600 | − Elf32_Sym *sym; | |
| 6654 | + Elf32_Sym *sym, *sym_end; | |
| 6601 | 6655 | unsigned long offset, align; |
| 6602 | 6656 | |
| 6657 | + sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset); | |
| 6603 | 6658 | for(sym = (Elf32_Sym *)symtab_section->data + 1; |
| 6604 | − sym < (Elf32_Sym *)symtab_section->data_ptr; | |
| 6659 | + sym < sym_end; | |
| 6605 | 6660 | sym++) { |
| 6606 | 6661 | if (sym->st_shndx == SHN_COMMON) { |
| 6607 | 6662 | /* align symbol */ |
| 6608 | 6663 | align = sym->st_value; |
| 6609 | − offset = bss_section->data_ptr - bss_section->data; | |
| 6664 | + offset = bss_section->data_offset; | |
| 6610 | 6665 | offset = (offset + align - 1) & -align; |
| 6611 | 6666 | sym->st_value = offset; |
| 6612 | 6667 | sym->st_shndx = bss_section->sh_num; |
| 6613 | 6668 | offset += sym->st_size; |
| 6614 | − bss_section->data_ptr = bss_section->data + offset; | |
| 6669 | + bss_section->data_offset = offset; | |
| 6615 | 6670 | } |
| 6616 | 6671 | } |
| 6617 | 6672 | } |
| @@ -6625,13 +6680,14 @@ static void *resolve_sym(const char *sym) · 7 unmodified lines | ||
| 6625 | 6680 | true and output error if undefined symbol. */ |
| 6626 | 6681 | static void relocate_syms(int do_resolve) |
| 6627 | 6682 | { |
| 6628 | − Elf32_Sym *sym, *esym; | |
| 6683 | + Elf32_Sym *sym, *esym, *sym_end; | |
| 6629 | 6684 | int sym_bind, sh_num, sym_index; |
| 6630 | 6685 | const char *name; |
| 6631 | 6686 | unsigned long addr; |
| 6632 | 6687 | |
| 6688 | + sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset); | |
| 6633 | 6689 | for(sym = (Elf32_Sym *)symtab_section->data + 1; |
| 6634 | − sym < (Elf32_Sym *)symtab_section->data_ptr; | |
| 6690 | + sym < sym_end; | |
| 6635 | 6691 | sym++) { |
| 6636 | 6692 | sh_num = sym->st_shndx; |
| 6637 | 6693 | if (sh_num == SHN_UNDEF) { |
| @@ -6707,15 +6763,16 @@ static void elf_reloc(unsigned char *ptr, · 69 unmodified lines | ||
| 6707 | 6763 | static void relocate_section(Section *s) |
| 6708 | 6764 | { |
| 6709 | 6765 | Section *sr; |
| 6710 | − Elf32_Rel *rel; | |
| 6766 | + Elf32_Rel *rel, *rel_end; | |
| 6711 | 6767 | Elf32_Sym *sym; |
| 6712 | 6768 | int type, sym_index; |
| 6713 | 6769 | unsigned char *ptr; |
| 6714 | 6770 | unsigned long val; |
| 6715 | 6771 | |
| 6716 | 6772 | sr = s->reloc; |
| 6773 | + rel_end = (Elf32_Rel *)(sr->data + sr->data_offset); | |
| 6717 | 6774 | for(rel = (Elf32_Rel *)sr->data; |
| 6718 | − rel < (Elf32_Rel *)sr->data_ptr; | |
| 6775 | + rel < rel_end; | |
| 6719 | 6776 | rel++) { |
| 6720 | 6777 | ptr = s->data + rel->r_offset; |
| 6721 | 6778 | |
| @@ -6731,11 +6788,12 @@ static void relocate_section(Section *s) · 9 unmodified lines | ||
| 6731 | 6788 | static void relocate_rel(Section *sr) |
| 6732 | 6789 | { |
| 6733 | 6790 | Section *s; |
| 6734 | − Elf32_Rel *rel; | |
| 6791 | + Elf32_Rel *rel, *rel_end; | |
| 6735 | 6792 | |
| 6736 | 6793 | s = sections[sr->sh_info]; |
| 6794 | + rel_end = (Elf32_Rel *)(sr->data + sr->data_offset); | |
| 6737 | 6795 | for(rel = (Elf32_Rel *)sr->data; |
| 6738 | − rel < (Elf32_Rel *)sr->data_ptr; | |
| 6796 | + rel < rel_end; | |
| 6739 | 6797 | rel++) { |
| 6740 | 6798 | rel->r_offset += s->sh_addr; |
| 6741 | 6799 | } |
| @@ -6773,19 +6831,19 @@ static void put32(unsigned char *p, unsigned int val) · 31 unmodified lines | ||
| 6773 | 6831 | |
| 6774 | 6832 | static void build_got(void) |
| 6775 | 6833 | { |
| 6834 | + unsigned char *ptr; | |
| 6835 | + | |
| 6776 | 6836 | /* if no got, then create it */ |
| 6777 | 6837 | got = new_section(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE); |
| 6778 | 6838 | got->sh_entsize = 4; |
| 6779 | 6839 | add_elf_sym(symtab_section, 0, 4, ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT), |
| 6780 | 6840 | got->sh_num, "_GLOBAL_OFFSET_TABLE_"); |
| 6841 | + ptr = section_ptr_add(got, 3 * sizeof(int)); | |
| 6781 | 6842 | /* keep space for _DYNAMIC pointer, if present */ |
| 6782 | − got->data_ptr += 4; | |
| 6783 | − | |
| 6843 | + put32(ptr, 0); | |
| 6784 | 6844 | /* two dummy got entries */ |
| 6785 | − put32(got->data_ptr, 0); | |
| 6786 | − got->data_ptr += 4; | |
| 6787 | − put32(got->data_ptr, 0); | |
| 6788 | − got->data_ptr += 4; | |
| 6845 | + put32(ptr + 4, 0); | |
| 6846 | + put32(ptr + 8, 0); | |
| 6789 | 6847 | } |
| 6790 | 6848 | |
| 6791 | 6849 | /* put a got entry corresponding to a symbol in symtab_section. 'size' |
| @@ -6797,6 +6855,7 @@ static void put_got_entry(int reloc_type, unsigned long size, int info, · 5 unmodified lines | ||
| 6797 | 6855 | const char *name; |
| 6798 | 6856 | Elf32_Sym *sym; |
| 6799 | 6857 | unsigned long offset; |
| 6858 | + int *ptr; | |
| 6800 | 6859 | |
| 6801 | 6860 | if (!got) |
| 6802 | 6861 | build_got(); |
| @@ -6806,7 +6865,7 @@ static void put_got_entry(int reloc_type, unsigned long size, int info, · 3 unmodified lines | ||
| 6806 | 6865 | got_offsets[sym_index] != 0) |
| 6807 | 6866 | return; |
| 6808 | 6867 | |
| 6809 | − put_got_offset(sym_index, got->data_ptr - got->data); | |
| 6868 | + put_got_offset(sym_index, got->data_offset); | |
| 6810 | 6869 | |
| 6811 | 6870 | if (dynsym) { |
| 6812 | 6871 | sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; |
| @@ -6815,24 +6874,24 @@ static void put_got_entry(int reloc_type, unsigned long size, int info, · 2 unmodified lines | ||
| 6815 | 6874 | /* NOTE: we put temporarily the got offset */ |
| 6816 | 6875 | if (reloc_type == R_386_JMP_SLOT) { |
| 6817 | 6876 | nb_plt_entries++; |
| 6818 | − offset = got->data_ptr - got->data; | |
| 6877 | + offset = got->data_offset; | |
| 6819 | 6878 | } |
| 6820 | 6879 | index = put_elf_sym(dynsym, offset, |
| 6821 | 6880 | size, info, 0, sym->st_shndx, name); |
| 6822 | 6881 | /* put a got entry */ |
| 6823 | 6882 | put_elf_reloc(dynsym, got, |
| 6824 | − got->data_ptr - got->data, | |
| 6883 | + got->data_offset, | |
| 6825 | 6884 | reloc_type, index); |
| 6826 | 6885 | } |
| 6827 | − put32(got->data_ptr, 0); | |
| 6828 | − got->data_ptr += 4; | |
| 6886 | + ptr = section_ptr_add(got, sizeof(int)); | |
| 6887 | + *ptr = 0; | |
| 6829 | 6888 | } |
| 6830 | 6889 | |
| 6831 | 6890 | /* build GOT and PLT entries */ |
| 6832 | 6891 | static void build_got_entries(void) |
| 6833 | 6892 | { |
| 6834 | 6893 | Section *s, *symtab; |
| 6835 | − Elf32_Rel *rel; | |
| 6894 | + Elf32_Rel *rel, *rel_end; | |
| 6836 | 6895 | Elf32_Sym *sym; |
| 6837 | 6896 | int i, type, reloc_type, sym_index; |
| 6838 | 6897 | |
| @@ -6844,8 +6903,9 @@ static void build_got_entries(void) · 5 unmodified lines | ||
| 6844 | 6903 | if (s->link != symtab_section) |
| 6845 | 6904 | continue; |
| 6846 | 6905 | symtab = s->link; |
| 6906 | + rel_end = (Elf32_Rel *)(s->data + s->data_offset); | |
| 6847 | 6907 | for(rel = (Elf32_Rel *)s->data; |
| 6848 | − rel < (Elf32_Rel *)s->data_ptr; | |
| 6908 | + rel < rel_end; | |
| 6849 | 6909 | rel++) { |
| 6850 | 6910 | type = ELF32_R_TYPE(rel->r_info); |
| 6851 | 6911 | switch(type) { |
| @@ -6882,7 +6942,7 @@ static Section *new_section_hash(const char *name, int sh_flags, · 30 unmodified lines | ||
| 6882 | 6942 | ((int *)hash->data)[0] = nb_buckets; |
| 6883 | 6943 | ((int *)hash->data)[1] = 1; |
| 6884 | 6944 | hash->sh_entsize = sizeof(int); |
| 6885 | − hash->data_ptr += (2 + nb_buckets + 1) * sizeof(int); | |
| 6945 | + section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int)); | |
| 6886 | 6946 | symtab->hash = hash; |
| 6887 | 6947 | hash->link = symtab; |
| 6888 | 6948 | return hash; |
| @@ -6892,24 +6952,24 @@ static Section *new_section_hash(const char *name, int sh_flags, · 3 unmodified lines | ||
| 6892 | 6952 | static void put_dt(Section *dynamic, int dt, unsigned long val) |
| 6893 | 6953 | { |
| 6894 | 6954 | Elf32_Dyn *dyn; |
| 6895 | − dyn = (Elf32_Dyn *)dynamic->data_ptr; | |
| 6955 | + dyn = section_ptr_add(dynamic, sizeof(Elf32_Dyn)); | |
| 6896 | 6956 | dyn->d_tag = dt; |
| 6897 | 6957 | dyn->d_un.d_val = val; |
| 6898 | − dynamic->data_ptr += sizeof(Elf32_Dyn); | |
| 6899 | 6958 | } |
| 6900 | 6959 | |
| 6901 | 6960 | /* add tcc runtime libraries */ |
| 6902 | 6961 | static void tcc_add_runtime(TCCState *s1) |
| 6903 | 6962 | { |
| 6904 | 6963 | char buf[1024]; |
| 6964 | + unsigned long *ptr; | |
| 6905 | 6965 | |
| 6906 | 6966 | snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "libtcc1.o"); |
| 6907 | 6967 | tcc_add_file(s1, buf); |
| 6908 | 6968 | #ifdef CONFIG_TCC_BCHECK |
| 6909 | 6969 | if (do_bounds_check) { |
| 6910 | 6970 | /* XXX: add an object file to do that */ |
| 6911 | − *(int *)bounds_section->data_ptr = 0; | |
| 6912 | − bounds_section->data_ptr += sizeof(int); | |
| 6971 | + ptr = section_ptr_add(bounds_section, sizeof(unsigned long)); | |
| 6972 | + *ptr = 0; | |
| 6913 | 6973 | add_elf_sym(symtab_section, 0, 0, |
| 6914 | 6974 | ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), |
| 6915 | 6975 | bounds_section->sh_num, "__bounds_start"); |
| @@ -6928,15 +6988,15 @@ static void tcc_add_runtime(TCCState *s1) · 12 unmodified lines | ||
| 6928 | 6988 | } |
| 6929 | 6989 | /* add various standard linker symbols */ |
| 6930 | 6990 | add_elf_sym(symtab_section, |
| 6931 | − text_section->data_ptr - text_section->data, 0, | |
| 6991 | + text_section->data_offset, 0, | |
| 6932 | 6992 | ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), |
| 6933 | 6993 | text_section->sh_num, "_etext"); |
| 6934 | 6994 | add_elf_sym(symtab_section, |
| 6935 | − data_section->data_ptr - data_section->data, 0, | |
| 6995 | + data_section->data_offset, 0, | |
| 6936 | 6996 | ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), |
| 6937 | 6997 | data_section->sh_num, "_edata"); |
| 6938 | 6998 | add_elf_sym(symtab_section, |
| 6939 | − bss_section->data_ptr - bss_section->data, 0, | |
| 6999 | + bss_section->data_offset, 0, | |
| 6940 | 7000 | ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), |
| 6941 | 7001 | bss_section->sh_num, "_end"); |
| 6942 | 7002 | } |
| @@ -6962,7 +7022,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 19 unmodified lines | ||
| 6962 | 7022 | Elf32_Shdr shdr, *sh; |
| 6963 | 7023 | Elf32_Phdr *phdr, *ph; |
| 6964 | 7024 | Section *interp, *plt, *dynamic, *dynstr, *hash; |
| 6965 | − unsigned char *saved_dynamic_data_ptr; | |
| 7025 | + unsigned long saved_dynamic_data_offset; | |
| 6966 | 7026 | Elf32_Sym *sym; |
| 6967 | 7027 | int type, file_type; |
| 6968 | 7028 | unsigned long rel_addr, rel_size; |
| @@ -6980,7 +7040,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 11 unmodified lines | ||
| 6980 | 7040 | plt = NULL; /* avoid warning */ |
| 6981 | 7041 | hash = NULL; /* avoid warning */ |
| 6982 | 7042 | dynstr = NULL; /* avoid warning */ |
| 6983 | − saved_dynamic_data_ptr = NULL; /* avoid warning */ | |
| 7043 | + saved_dynamic_data_offset = 0; /* avoid warning */ | |
| 6984 | 7044 | |
| 6985 | 7045 | if (file_type != TCC_OUTPUT_OBJ) { |
| 6986 | 7046 | |
| @@ -6989,14 +7049,15 @@ int tcc_output_file(TCCState *s1, const char *filename) · 2 unmodified lines | ||
| 6989 | 7049 | if (!static_link) { |
| 6990 | 7050 | const char *name; |
| 6991 | 7051 | int sym_index, index; |
| 6992 | − Elf32_Sym *esym; | |
| 7052 | + Elf32_Sym *esym, *sym_end; | |
| 6993 | 7053 | |
| 6994 | 7054 | if (file_type == TCC_OUTPUT_EXE) { |
| 7055 | + char *ptr; | |
| 6995 | 7056 | /* add interpreter section only if executable */ |
| 6996 | 7057 | interp = new_section(".interp", SHT_PROGBITS, SHF_ALLOC); |
| 6997 | 7058 | interp->sh_addralign = 1; |
| 6998 | − strcpy(interp->data_ptr, elf_interp); | |
| 6999 | − interp->data_ptr += sizeof(elf_interp); | |
| 7059 | + ptr = section_ptr_add(interp, sizeof(elf_interp)); | |
| 7060 | + strcpy(ptr, elf_interp); | |
| 7000 | 7061 | } |
| 7001 | 7062 | |
| 7002 | 7063 | /* add dynamic symbol table */ |
| @@ -7025,8 +7086,10 @@ int tcc_output_file(TCCState *s1, const char *filename) · 22 unmodified lines | ||
| 7025 | 7086 | dynamic symbols. If a symbol STT_FUNC is found, then we |
| 7026 | 7087 | add it in the PLT. If a symbol STT_OBJECT is found, we |
| 7027 | 7088 | add it in the .bss section with a suitable relocation */ |
| 7089 | + sym_end = (Elf32_Sym *)(symtab_section->data + | |
| 7090 | + symtab_section->data_offset); | |
| 7028 | 7091 | for(sym = (Elf32_Sym *)symtab_section->data + 1; |
| 7029 | − sym < (Elf32_Sym *)symtab_section->data_ptr; | |
| 7092 | + sym < sym_end; | |
| 7030 | 7093 | sym++) { |
| 7031 | 7094 | if (sym->st_shndx == SHN_UNDEF) { |
| 7032 | 7095 | name = symtab_section->link->data + sym->st_name; |
| @@ -7040,7 +7103,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines | ||
| 7040 | 7103 | sym - (Elf32_Sym *)symtab_section->data); |
| 7041 | 7104 | } else if (type == STT_OBJECT) { |
| 7042 | 7105 | unsigned long offset; |
| 7043 | − offset = bss_section->data_ptr - bss_section->data; | |
| 7106 | + offset = bss_section->data_offset; | |
| 7044 | 7107 | /* XXX: which alignment ? */ |
| 7045 | 7108 | offset = (offset + 8 - 1) & -8; |
| 7046 | 7109 | index = put_elf_sym(dynsym, offset, esym->st_size, |
| @@ -7049,7 +7112,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 2 unmodified lines | ||
| 7049 | 7112 | put_elf_reloc(dynsym, bss_section, |
| 7050 | 7113 | offset, R_386_COPY, index); |
| 7051 | 7114 | offset += esym->st_size; |
| 7052 | − bss_section->data_ptr = bss_section->data + offset; | |
| 7115 | + bss_section->data_offset = offset; | |
| 7053 | 7116 | } |
| 7054 | 7117 | } else { |
| 7055 | 7118 | /* STT_NOTYPE or STB_WEAK undefined symbols |
| @@ -7068,8 +7131,10 @@ int tcc_output_file(TCCState *s1, const char *filename) · 12 unmodified lines | ||
| 7068 | 7131 | |
| 7069 | 7132 | /* now look at unresolved dynamic symbols and export |
| 7070 | 7133 | corresponding symbol */ |
| 7134 | + sym_end = (Elf32_Sym *)(dynsymtab_section->data + | |
| 7135 | + dynsymtab_section->data_offset); | |
| 7071 | 7136 | for(sym = (Elf32_Sym *)dynsymtab_section->data + 1; |
| 7072 | − sym < (Elf32_Sym *)dynsymtab_section->data_ptr; | |
| 7137 | + sym < sym_end; | |
| 7073 | 7138 | sym++) { |
| 7074 | 7139 | if (sym->st_shndx == SHN_UNDEF) { |
| 7075 | 7140 | name = dynsymtab_section->link->data + sym->st_name; |
| @@ -7088,7 +7153,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 12 unmodified lines | ||
| 7088 | 7153 | build_got_entries(); |
| 7089 | 7154 | |
| 7090 | 7155 | /* update PLT/GOT sizes so that we can allocate their space */ |
| 7091 | − plt->data_ptr += 16 * (nb_plt_entries + 1); | |
| 7156 | + plt->data_offset += 16 * (nb_plt_entries + 1); | |
| 7092 | 7157 | |
| 7093 | 7158 | /* add a list of needed dlls */ |
| 7094 | 7159 | for(i = 0; i < nb_loaded_dlls; i++) { |
| @@ -7098,8 +7163,8 @@ int tcc_output_file(TCCState *s1, const char *filename) · 3 unmodified lines | ||
| 7098 | 7163 | } |
| 7099 | 7164 | |
| 7100 | 7165 | /* add necessary space for other entries */ |
| 7101 | − saved_dynamic_data_ptr = dynamic->data_ptr; | |
| 7102 | − dynamic->data_ptr += 8 * 9; | |
| 7166 | + saved_dynamic_data_offset = dynamic->data_offset; | |
| 7167 | + dynamic->data_offset += 8 * 9; | |
| 7103 | 7168 | } else { |
| 7104 | 7169 | /* still need to build got entries in case of static link */ |
| 7105 | 7170 | build_got_entries(); |
| @@ -7143,7 +7208,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 37 unmodified lines | ||
| 7143 | 7208 | for(i = 1; i < nb_sections; i++) { |
| 7144 | 7209 | s = sections[i]; |
| 7145 | 7210 | s->sh_name = put_elf_str(strsec, s->name); |
| 7146 | − s->sh_size = s->data_ptr - s->data; | |
| 7211 | + s->sh_size = s->data_offset; | |
| 7147 | 7212 | } |
| 7148 | 7213 | |
| 7149 | 7214 | /* allocate program segment headers */ |
| @@ -7264,6 +7329,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 114 unmodified lines | ||
| 7264 | 7329 | if (dynamic) { |
| 7265 | 7330 | int plt_offset; |
| 7266 | 7331 | unsigned char *p; |
| 7332 | + Elf32_Sym *sym_end; | |
| 7267 | 7333 | |
| 7268 | 7334 | ph = &phdr[phnum - 1]; |
| 7269 | 7335 | |
| @@ -7280,42 +7346,41 @@ int tcc_output_file(TCCState *s1, const char *filename) · 10 unmodified lines | ||
| 7280 | 7346 | put32(got->data, dynamic->sh_addr); |
| 7281 | 7347 | |
| 7282 | 7348 | /* compute the PLT */ |
| 7283 | − plt->data_ptr = plt->data; | |
| 7349 | + plt->data_offset = 0; | |
| 7284 | 7350 | |
| 7285 | 7351 | /* first plt entry */ |
| 7286 | − p = plt->data_ptr; | |
| 7352 | + p = section_ptr_add(plt, 16); | |
| 7287 | 7353 | p[0] = 0xff; /* pushl got + 4 */ |
| 7288 | 7354 | p[1] = 0x35; |
| 7289 | 7355 | put32(p + 2, got->sh_addr + 4); |
| 7290 | 7356 | p[6] = 0xff; /* jmp *(got + 8) */ |
| 7291 | 7357 | p[7] = 0x25; |
| 7292 | 7358 | put32(p + 8, got->sh_addr + 8); |
| 7293 | − plt->data_ptr += 16; | |
| 7294 | 7359 | |
| 7295 | 7360 | /* relocation symbols in .dynsym and build PLT. */ |
| 7296 | 7361 | plt_offset = 0; |
| 7362 | + sym_end = (Elf32_Sym *)(dynsym->data + dynsym->data_offset); | |
| 7297 | 7363 | for(sym = (Elf32_Sym *)dynsym->data + 1; |
| 7298 | − sym < (Elf32_Sym *)dynsym->data_ptr; | |
| 7364 | + sym < sym_end; | |
| 7299 | 7365 | sym++) { |
| 7300 | 7366 | type = ELF32_ST_TYPE(sym->st_info); |
| 7301 | 7367 | if (sym->st_shndx == SHN_UNDEF) { |
| 7302 | 7368 | if (type == STT_FUNC) { |
| 7303 | 7369 | /* one more entry in PLT */ |
| 7304 | − p = plt->data_ptr; | |
| 7370 | + p = section_ptr_add(plt, 16); | |
| 7305 | 7371 | p[0] = 0xff; /* jmp *(got + x) */ |
| 7306 | 7372 | p[1] = 0x25; |
| 7307 | 7373 | put32(p + 2, got->sh_addr + sym->st_value); |
| 7308 | 7374 | p[6] = 0x68; /* push $xxx */ |
| 7309 | 7375 | put32(p + 7, plt_offset); |
| 7310 | 7376 | p[11] = 0xe9; /* jmp plt_start */ |
| 7311 | − put32(p + 12, -(plt->data_ptr + 16 - plt->data)); | |
| 7377 | + put32(p + 12, -(plt->data_offset)); | |
| 7312 | 7378 | |
| 7313 | 7379 | /* patch symbol value to point to plt */ |
| 7314 | 7380 | sym->st_value = plt->sh_addr + p - plt->data; |
| 7315 | 7381 | |
| 7316 | 7382 | plt_offset += 8; |
| 7317 | − plt->data_ptr += 16; | |
| 7318 | − } | |
| 7383 | + } | |
| 7319 | 7384 | } else if (sym->st_shndx < SHN_LORESERVE) { |
| 7320 | 7385 | /* do symbol relocation */ |
| 7321 | 7386 | sym->st_value += sections[sym->st_shndx]->sh_addr; |
| @@ -7323,11 +7388,11 @@ int tcc_output_file(TCCState *s1, const char *filename) · 1 unmodified line | ||
| 7323 | 7388 | } |
| 7324 | 7389 | /* put dynamic section entries */ |
| 7325 | 7390 | |
| 7326 | − dynamic->data_ptr = saved_dynamic_data_ptr; | |
| 7391 | + dynamic->data_offset = saved_dynamic_data_offset; | |
| 7327 | 7392 | put_dt(dynamic, DT_HASH, hash->sh_addr); |
| 7328 | 7393 | put_dt(dynamic, DT_STRTAB, dynstr->sh_addr); |
| 7329 | 7394 | put_dt(dynamic, DT_SYMTAB, dynsym->sh_addr); |
| 7330 | − put_dt(dynamic, DT_STRSZ, dynstr->data_ptr - dynstr->data); | |
| 7395 | + put_dt(dynamic, DT_STRSZ, dynstr->data_offset); | |
| 7331 | 7396 | put_dt(dynamic, DT_SYMENT, sizeof(Elf32_Sym)); |
| 7332 | 7397 | put_dt(dynamic, DT_REL, rel_addr); |
| 7333 | 7398 | put_dt(dynamic, DT_RELSZ, rel_size); |
| @@ -7363,12 +7428,12 @@ int tcc_output_file(TCCState *s1, const char *filename) · 29 unmodified lines | ||
| 7363 | 7428 | /* XXX: ignore sections with allocated relocations ? */ |
| 7364 | 7429 | for(i = 1; i < nb_sections; i++) { |
| 7365 | 7430 | s = sections[i]; |
| 7366 | − if ((s->sh_flags & SHF_ALLOC) | |
| 7367 | − && s->reloc && s != got) | |
| 7431 | + if (s->reloc && s != got) | |
| 7368 | 7432 | relocate_section(s); |
| 7369 | 7433 | } |
| 7370 | 7434 | |
| 7371 | − /* relocate relocation entries */ | |
| 7435 | + /* relocate relocation entries if the relocation tables are | |
| 7436 | + allocated in the executable */ | |
| 7372 | 7437 | for(i = 1; i < nb_sections; i++) { |
| 7373 | 7438 | s = sections[i]; |
| 7374 | 7439 | if ((s->sh_flags & SHF_ALLOC) && |
| @@ -7434,7 +7499,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 59 unmodified lines | ||
| 7434 | 7499 | fputc(0, f); |
| 7435 | 7500 | offset++; |
| 7436 | 7501 | } |
| 7437 | − size = s->data_ptr - s->data; | |
| 7502 | + size = s->data_offset; | |
| 7438 | 7503 | fwrite(s->data, 1, size, f); |
| 7439 | 7504 | offset += size; |
| 7440 | 7505 | } |
| @@ -7502,7 +7567,7 @@ static int tcc_load_object_file(TCCState *s1, · 61 unmodified lines | ||
| 7502 | 7567 | char *sh_name, *name; |
| 7503 | 7568 | SectionMergeInfo *sm_table, *sm; |
| 7504 | 7569 | Elf32_Sym *sym, *symtab; |
| 7505 | − Elf32_Rel *rel; | |
| 7570 | + Elf32_Rel *rel, *rel_end; | |
| 7506 | 7571 | Section *s; |
| 7507 | 7572 | |
| 7508 | 7573 | if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) |
| @@ -7585,21 +7650,23 @@ static int tcc_load_object_file(TCCState *s1, · 76 unmodified lines | ||
| 7585 | 7650 | goto fail; |
| 7586 | 7651 | |
| 7587 | 7652 | /* align start of section */ |
| 7588 | − offset = s->data_ptr - s->data; | |
| 7653 | + offset = s->data_offset; | |
| 7589 | 7654 | size = sh->sh_addralign - 1; |
| 7590 | 7655 | offset = (offset + size) & ~size; |
| 7591 | 7656 | if (sh->sh_addralign > s->sh_addralign) |
| 7592 | 7657 | s->sh_addralign = sh->sh_addralign; |
| 7593 | − s->data_ptr = s->data + offset; | |
| 7658 | + s->data_offset = offset; | |
| 7594 | 7659 | sm_table[i].offset = offset; |
| 7595 | 7660 | sm_table[i].s = s; |
| 7596 | 7661 | /* concatenate sections */ |
| 7597 | 7662 | size = sh->sh_size; |
| 7598 | 7663 | if (sh->sh_type != SHT_NOBITS) { |
| 7664 | + unsigned char *ptr; | |
| 7599 | 7665 | lseek(fd, file_offset + sh->sh_offset, SEEK_SET); |
| 7600 | − read(fd, s->data_ptr, size); | |
| 7666 | + ptr = section_ptr(s, size); | |
| 7667 | + read(fd, ptr, size); | |
| 7601 | 7668 | } |
| 7602 | − s->data_ptr += size; | |
| 7669 | + s->data_offset += size; | |
| 7603 | 7670 | } |
| 7604 | 7671 | |
| 7605 | 7672 | /* second short pass to update sh_link and sh_info fields of new |
| @@ -7655,8 +7722,9 @@ static int tcc_load_object_file(TCCState *s1, · 49 unmodified lines | ||
| 7655 | 7722 | case SHT_REL: |
| 7656 | 7723 | /* take relocation offset information */ |
| 7657 | 7724 | offseti = sm_table[sh->sh_info].offset; |
| 7725 | + rel_end = (Elf32_Rel *)(s->data + s->data_offset); | |
| 7658 | 7726 | for(rel = (Elf32_Rel *)(s->data + offset); |
| 7659 | − rel < (Elf32_Rel *)s->data_ptr; | |
| 7727 | + rel < rel_end; | |
| 7660 | 7728 | rel++) { |
| 7661 | 7729 | int type; |
| 7662 | 7730 | unsigned sym_index; |
| @@ -7945,7 +8013,7 @@ static void rt_printline(unsigned long wanted_pc) · 282 unmodified lines | ||
| 7945 | 8013 | last_pc = 0xffffffff; |
| 7946 | 8014 | last_line_num = 1; |
| 7947 | 8015 | sym = (Stab_Sym *)stab_section->data + 1; |
| 7948 | − sym_end = (Stab_Sym *)stab_section->data_ptr; | |
| 8016 | + sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset); | |
| 7949 | 8017 | while (sym < sym_end) { |
| 7950 | 8018 | switch(sym->n_type) { |
| 7951 | 8019 | /* function start or end */ |
| @@ -8098,7 +8166,7 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 146 unmodified lines | ||
| 8098 | 8166 | /* relocate each section */ |
| 8099 | 8167 | for(i = 1; i < nb_sections; i++) { |
| 8100 | 8168 | s = sections[i]; |
| 8101 | − if ((s->sh_flags & SHF_ALLOC) && s->reloc) | |
| 8169 | + if (s->reloc) | |
| 8102 | 8170 | relocate_section(s); |
| 8103 | 8171 | } |
| 8104 | 8172 | |