History

use data_offset in sections instead of pointer to ease section reallocation - relocated .stabs and .bounds sections - fixed char constants casts - fixed implicit cast to unary plus - fixed type for string constants

bellard committed 24 years ago

Commit 9b482371 parent 540cd6f1 file · +222 −154Browse files

Unified · Split

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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t