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
135135 #define SHF_PRIVATE 0x80000000
136136
137137 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 */
140140 int sh_name; /* elf section name (only used during output) */
141141 int sh_num; /* elf section number */
142142 int sh_type; /* elf section type */
@@ -197,7 +197,7 @@ typedef struct BufferedFile { · 54 unmodified lines
197197 unsigned char *buf_end;
198198 int fd;
199199 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 */
201201 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
202202 } BufferedFile;
203203
@@ -337,7 +337,7 @@ struct TCCState { · 133 unmodified lines
337337 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
338338
339339 /* 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) */
341341
342342 #define VT_INT 0 /* integer type */
343343 #define VT_BYTE 1 /* signed byte type */
@@ -594,6 +594,8 @@ static Sym *get_sym_ref(int t, Section *sec, · 250 unmodified lines
594594 unsigned long offset, unsigned long size);
595595
596596 /* section generation */
597+static void *section_ptr(Section *sec, unsigned long size);
598+static void *section_ptr_add(Section *sec, unsigned long size);
597599 void put_extern_sym(Sym *sym, Section *section, unsigned long value);
598600 void greloc(Section *s, Sym *sym, unsigned long addr, int type);
599601 static int put_elf_str(Section *s, const char *sym);
@@ -602,7 +604,10 @@ static int put_elf_sym(Section *s, · 2 unmodified lines
602604 int info, int other, int shndx, const char *name);
603605 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
604606 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);
606611 static void put_stabn(int type, int other, int desc, int value);
607612 static void put_stabd(int type, int other, int desc);
608613 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
777782 error("could not mmap section '%s'", name);
778783 #endif
779784 sec->data = data;
780− sec->data_ptr = data;
785+ sec->data_offset = 0;
781786
782787 /* only add section if not private */
783788 if (!(sh_flags & SHF_PRIVATE)) {
@@ -787,6 +792,20 @@ Section *new_section(const char *name, int sh_type, int sh_flags) · 3 unmodified lines
787792 return sec;
788793 }
789794
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+
790809 /* return a reference to a section, and create it if it does not
791810 exists */
792811 Section *find_section(const char *name)
@@ -1668,7 +1687,7 @@ void preprocess(void) · 875 unmodified lines
16681687 }
16691688
16701689 /* read a number in base b */
1671−int getn(b)
1690+static int getn(int b)
16721691 {
16731692 int n, t;
16741693 n = 0;
@@ -1690,7 +1709,7 @@ int getn(b) · 15 unmodified lines
16901709 }
16911710
16921711 /* read a character for string or char constant and eval escape codes */
1693−int getq()
1712+static int getq(void)
16941713 {
16951714 int c;
16961715
@@ -2094,7 +2113,11 @@ void next_nomacro1(void) · 397 unmodified lines
20942113 tok = TOK_CCHAR;
20952114 char_const:
20962115 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;
20982121 if (ch != '\'')
20992122 expect("\'");
21002123 minp();
@@ -2718,7 +2741,7 @@ void gbound(void) · 617 unmodified lines
27182741 register value (such as structures). */
27192742 int gv(int rc)
27202743 {
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;
27222745 unsigned long long ll;
27232746
27242747 /* NOTE: get_reg can modify vstack[] */
@@ -2739,27 +2762,28 @@ int gv(int rc) · 14 unmodified lines
27392762 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
27402763 int v;
27412764 Sym *sym;
2765+ int *ptr;
27422766
27432767 /* XXX: unify with initializers handling ? */
27442768 /* CPUs usually cannot use float constants, so we store them
27452769 generically in data segment */
27462770 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;
27492773 /* XXX: not portable yet */
2774+ ptr = section_ptr(data_section, size);
27502775 size = size >> 2;
27512776 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];
27532778
27542779 v = anon_sym++;
27552780 sym = sym_push1(&global_stack, v, vtop->t | VT_STATIC, 0);
27562781 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);
27582783
27592784 vtop->r |= VT_LVAL | VT_SYM;
27602785 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;
27632787 }
27642788 #ifdef CONFIG_TCC_BCHECK
27652789 if (vtop->r & VT_MUSTBOUND)
@@ -4673,13 +4697,18 @@ void unary(void) · 1907 unmodified lines
46734697 vsetc(VT_LDOUBLE, VT_CONST, &tokc);
46744698 next();
46754699 } else if (tok == TOK___FUNC__) {
4700+ void *ptr;
4701+ int len;
46764702 /* 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);
46834712 next();
46844713 } else if (tok == TOK_LSTR) {
46854714 t = VT_INT;
@@ -4689,16 +4718,16 @@ void unary(void) · 3 unmodified lines
46894718 t = VT_BYTE;
46904719 str_init:
46914720 type_size(t, &align);
4692− data_offset = data_section->data_ptr - data_section->data;
4721+ data_offset = data_section->data_offset;
46934722 data_offset = (data_offset + align - 1) & -align;
46944723 fc = data_offset;
46954724 /* we must declare it as an array first to use initializer parser */
46964725 t = VT_ARRAY | mk_pointer(t);
46974726 decl_initializer(t, data_section, data_offset, 1, 0);
46984727 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;
47024731 } else {
47034732 t = tok;
47044733 next();
@@ -4758,7 +4787,12 @@ void unary(void) · 53 unmodified lines
47584787 gen_op('^');
47594788 } else
47604789 if (t == '+') {
4790+ /* in order to force cast, we add zero */
47614791 unary();
4792+ if ((vtop->t & VT_BTYPE) == VT_PTR)
4793+ error("pointer not accepted for unary plus");
4794+ vpushi(0);
4795+ gen_op('+');
47624796 } else
47634797 if (t == TOK_SIZEOF) {
47644798 if (tok == '(') {
@@ -5811,14 +5845,13 @@ void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 1046 unmodified lines
58115845 /* XXX: currently, since we do only one pass, we cannot track
58125846 '&' operators, so we add only arrays */
58135847 if (do_bounds_check && (t & VT_ARRAY)) {
5814− int *bounds_ptr;
5848+ unsigned long *bounds_ptr;
58155849 /* add padding between regions */
58165850 loc--;
58175851 /* 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;
58225855 }
58235856 } else {
58245857 /* compute section */
@@ -5829,33 +5862,29 @@ void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 4 unmodified lines
58295862 else
58305863 sec = bss_section;
58315864 }
5832− data_offset = sec->data_ptr - sec->data;
5865+ data_offset = sec->data_offset;
58335866 data_offset = (data_offset + align - 1) & -align;
58345867 addr = data_offset;
58355868 /* very important to increment global pointer at this time
58365869 because initializers themselves can create new initializers */
58375870 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)
58425873 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;
58515875 }
5852− if (v) {
5853− Sym *sym;
5854−
5855− if (!sec) {
5876+ if (!sec) {
5877+ if (v) {
58565878 /* local variable */
58575879 sym_push(v, t, r, addr);
58585880 } else {
5881+ /* push local reference */
5882+ vset(t, r, addr);
5883+ }
5884+ } else {
5885+ Sym *sym;
5886+
5887+ if (v) {
58595888 if (scope == VT_CONST) {
58605889 /* global scope: see if already defined */
58615890 sym = sym_find(v);
@@ -5872,14 +5901,25 @@ void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 10 unmodified lines
58725901 sym = sym_push(v, t, r | VT_SYM, 0);
58735902 }
58745903 put_extern_sym(sym, sec, addr);
5875− }
5876− } else {
5877− if (!sec) {
5878− /* push local reference */
5879− vset(t, r, addr);
58805904 } else {
5905+ CValue cval;
5906+
58815907 /* 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;
58835923 }
58845924 }
58855925 if (has_init) {
@@ -5892,15 +5932,16 @@ void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 6 unmodified lines
58925932 }
58935933 }
58945934
5895−void put_func_debug(int t)
5935+void put_func_debug(Sym *sym)
58965936 {
58975937 char buf[512];
58985938
58995939 /* stabs info */
59005940 /* XXX: we put here a dummy type */
59015941 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);
59045945 func_ind = ind;
59055946 last_ind = 0;
59065947 last_line_num = 0;
@@ -6049,7 +6090,7 @@ void decl(int l) · 142 unmodified lines
60496090 cur_text_section = ad.section;
60506091 if (!cur_text_section)
60516092 cur_text_section = text_section;
6052− ind = (int)cur_text_section->data_ptr;
6093+ ind = cur_text_section->data_offset;
60536094 funcname = get_tok_str(v, NULL);
60546095 sym = sym_find(v);
60556096 if (sym) {
@@ -6059,12 +6100,11 @@ void decl(int l) · 3 unmodified lines
60596100 /* put function symbol */
60606101 sym = sym_push1(&global_stack, v, t, 0);
60616102 }
6062− put_extern_sym(sym, cur_text_section,
6063− ind - (int)cur_text_section->data);
6103+ put_extern_sym(sym, cur_text_section, ind);
60646104 sym->r = VT_SYM | VT_CONST;
60656105 /* put debug symbol */
60666106 if (do_debug)
6067− put_func_debug(t);
6107+ put_func_debug(sym);
60686108 /* push a dummy symbol to enable local sym storage */
60696109 sym_push1(&local_stack, 0, 0, 0);
60706110 gfunc_prolog(t);
@@ -6077,7 +6117,7 @@ void decl(int l) · 6 unmodified lines
60776117 block(NULL, NULL, NULL, NULL, 0);
60786118 gsym(rsym);
60796119 gfunc_epilog();
6080− cur_text_section->data_ptr = (unsigned char *)ind;
6120+ cur_text_section->data_offset = ind;
60816121 sym_pop(&label_stack, NULL); /* reset label stack */
60826122 sym_pop(&local_stack, NULL); /* reset local stack */
60836123 /* end of function */
@@ -6136,7 +6176,7 @@ static int tcc_compile(TCCState *s) · 52 unmodified lines
61366176 {
61376177 Sym *define_start;
61386178 char buf[512];
6139− int p;
6179+ int p, section_sym;
61406180
61416181 funcname = "";
61426182 include_stack_ptr = include_stack;
@@ -6146,12 +6186,17 @@ static int tcc_compile(TCCState *s) · 3 unmodified lines
61466186 anon_sym = SYM_FIRST_ANOM;
61476187
61486188 /* file info: full path + filename */
6189+ section_sym = 0; /* avoid warning */
61496190 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);
61506194 getcwd(buf, sizeof(buf));
61516195 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);
61556200 }
61566201 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
61576202 symbols can be safely used */
@@ -6177,7 +6222,8 @@ static int tcc_compile(TCCState *s) · 19 unmodified lines
61776222
61786223 /* end of translation unit info */
61796224 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);
61816227 }
61826228
61836229 /* 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
62516297
62526298 static int put_elf_str(Section *s, const char *sym)
62536299 {
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);
62626307 return offset;
62636308 }
62646309
@@ -6287,7 +6332,7 @@ static int put_elf_sym(Section *s, · 22 unmodified lines
62876332 Elf32_Sym *sym;
62886333 Section *hs;
62896334
6290− sym = (Elf32_Sym *)s->data_ptr;
6335+ sym = section_ptr_add(s, sizeof(Elf32_Sym));
62916336 if (name)
62926337 name_offset = put_elf_str(s->link, name);
62936338 else
@@ -6302,19 +6347,21 @@ static int put_elf_sym(Section *s, · 8 unmodified lines
63026347 sym_index = sym - (Elf32_Sym *)s->data;
63036348 hs = s->hash;
63046349 if (hs) {
6350+ int *ptr;
6351+ ptr = section_ptr_add(hs, sizeof(int));
63056352 /* only add global or weak symbols */
63066353 if (ELF32_ST_BIND(info) != STB_LOCAL) {
63076354 /* add another hashing entry */
63086355 nbuckets = ((int *)hs->data)[0];
63096356 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];
63116358 ((int *)hs->data)[2 + h] = sym_index;
6359+ } else {
6360+ *ptr = 0;
63126361 }
63136362 /* but still add room for all symbols */
63146363 ((int *)hs->data)[1]++;
6315− hs->data_ptr += sizeof(int);
63166364 }
6317− s->data_ptr += sizeof(Elf32_Sym);
63186365 return sym_index;
63196366 }
63206367
@@ -6483,11 +6530,9 @@ static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, · 162 unmodified lines
64836530 sr->sh_info = s->sh_num;
64846531 s->reloc = sr;
64856532 }
6486− rel = (Elf32_Rel *)sr->data_ptr;
6487− /* XXX: endianness */
6533+ rel = section_ptr_add(sr, sizeof(Elf32_Rel));
64886534 rel->r_offset = offset;
64896535 rel->r_info = ELF32_R_INFO(symbol, type);
6490− sr->data_ptr += sizeof(Elf32_Rel);
64916536 }
64926537
64936538 /* put stab debug information */
@@ -6500,11 +6545,12 @@ typedef struct { · 6 unmodified lines
65006545 unsigned long n_value; /* value of symbol */
65016546 } Stab_Sym;
65026547
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)
65046550 {
65056551 Stab_Sym *sym;
65066552
6507− sym = (Stab_Sym *)stab_section->data_ptr;
6553+ sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
65086554 if (str) {
65096555 sym->n_strx = put_elf_str(stabstr_section, str);
65106556 } else {
@@ -6514,8 +6560,15 @@ static void put_stabs(const char *str, int type, int other, int desc, int value) · 3 unmodified lines
65146560 sym->n_other = other;
65156561 sym->n_desc = desc;
65166562 sym->n_value = value;
6563+}
65176564
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);
65196572 }
65206573
65216574 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
65386591 Elf32_Sym *new_syms;
65396592 int nb_syms, i;
65406593 Elf32_Sym *p, *q;
6541− Elf32_Rel *rel;
6594+ Elf32_Rel *rel, *rel_end;
65426595 Section *sr;
65436596 int type, sym_index;
65446597
6545− nb_syms = (s->data_ptr - s->data) / sizeof(Elf32_Sym);
6598+ nb_syms = s->data_offset / sizeof(Elf32_Sym);
65466599 new_syms = malloc(nb_syms * sizeof(Elf32_Sym));
65476600 if (!new_syms)
65486601 error("memory full");
@@ -6580,8 +6633,9 @@ static void sort_syms(Section *s) · 31 unmodified lines
65806633 for(i = 1; i < nb_sections; i++) {
65816634 sr = sections[i];
65826635 if (sr->sh_type == SHT_REL && sr->link == s) {
6636+ rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
65836637 for(rel = (Elf32_Rel *)sr->data;
6584− rel < (Elf32_Rel *)sr->data_ptr;
6638+ rel < rel_end;
65856639 rel++) {
65866640 sym_index = ELF32_R_SYM(rel->r_info);
65876641 type = ELF32_R_TYPE(rel->r_info);
@@ -6597,21 +6651,22 @@ static void sort_syms(Section *s) · 9 unmodified lines
65976651 /* relocate common symbols in the .bss section */
65986652 static void relocate_common_syms(void)
65996653 {
6600− Elf32_Sym *sym;
6654+ Elf32_Sym *sym, *sym_end;
66016655 unsigned long offset, align;
66026656
6657+ sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
66036658 for(sym = (Elf32_Sym *)symtab_section->data + 1;
6604− sym < (Elf32_Sym *)symtab_section->data_ptr;
6659+ sym < sym_end;
66056660 sym++) {
66066661 if (sym->st_shndx == SHN_COMMON) {
66076662 /* align symbol */
66086663 align = sym->st_value;
6609− offset = bss_section->data_ptr - bss_section->data;
6664+ offset = bss_section->data_offset;
66106665 offset = (offset + align - 1) & -align;
66116666 sym->st_value = offset;
66126667 sym->st_shndx = bss_section->sh_num;
66136668 offset += sym->st_size;
6614− bss_section->data_ptr = bss_section->data + offset;
6669+ bss_section->data_offset = offset;
66156670 }
66166671 }
66176672 }
@@ -6625,13 +6680,14 @@ static void *resolve_sym(const char *sym) · 7 unmodified lines
66256680 true and output error if undefined symbol. */
66266681 static void relocate_syms(int do_resolve)
66276682 {
6628− Elf32_Sym *sym, *esym;
6683+ Elf32_Sym *sym, *esym, *sym_end;
66296684 int sym_bind, sh_num, sym_index;
66306685 const char *name;
66316686 unsigned long addr;
66326687
6688+ sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
66336689 for(sym = (Elf32_Sym *)symtab_section->data + 1;
6634− sym < (Elf32_Sym *)symtab_section->data_ptr;
6690+ sym < sym_end;
66356691 sym++) {
66366692 sh_num = sym->st_shndx;
66376693 if (sh_num == SHN_UNDEF) {
@@ -6707,15 +6763,16 @@ static void elf_reloc(unsigned char *ptr, · 69 unmodified lines
67076763 static void relocate_section(Section *s)
67086764 {
67096765 Section *sr;
6710− Elf32_Rel *rel;
6766+ Elf32_Rel *rel, *rel_end;
67116767 Elf32_Sym *sym;
67126768 int type, sym_index;
67136769 unsigned char *ptr;
67146770 unsigned long val;
67156771
67166772 sr = s->reloc;
6773+ rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
67176774 for(rel = (Elf32_Rel *)sr->data;
6718− rel < (Elf32_Rel *)sr->data_ptr;
6775+ rel < rel_end;
67196776 rel++) {
67206777 ptr = s->data + rel->r_offset;
67216778
@@ -6731,11 +6788,12 @@ static void relocate_section(Section *s) · 9 unmodified lines
67316788 static void relocate_rel(Section *sr)
67326789 {
67336790 Section *s;
6734− Elf32_Rel *rel;
6791+ Elf32_Rel *rel, *rel_end;
67356792
67366793 s = sections[sr->sh_info];
6794+ rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
67376795 for(rel = (Elf32_Rel *)sr->data;
6738− rel < (Elf32_Rel *)sr->data_ptr;
6796+ rel < rel_end;
67396797 rel++) {
67406798 rel->r_offset += s->sh_addr;
67416799 }
@@ -6773,19 +6831,19 @@ static void put32(unsigned char *p, unsigned int val) · 31 unmodified lines
67736831
67746832 static void build_got(void)
67756833 {
6834+ unsigned char *ptr;
6835+
67766836 /* if no got, then create it */
67776837 got = new_section(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
67786838 got->sh_entsize = 4;
67796839 add_elf_sym(symtab_section, 0, 4, ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT),
67806840 got->sh_num, "_GLOBAL_OFFSET_TABLE_");
6841+ ptr = section_ptr_add(got, 3 * sizeof(int));
67816842 /* keep space for _DYNAMIC pointer, if present */
6782− got->data_ptr += 4;
6783−
6843+ put32(ptr, 0);
67846844 /* 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);
67896847 }
67906848
67916849 /* 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
67976855 const char *name;
67986856 Elf32_Sym *sym;
67996857 unsigned long offset;
6858+ int *ptr;
68006859
68016860 if (!got)
68026861 build_got();
@@ -6806,7 +6865,7 @@ static void put_got_entry(int reloc_type, unsigned long size, int info, · 3 unmodified lines
68066865 got_offsets[sym_index] != 0)
68076866 return;
68086867
6809− put_got_offset(sym_index, got->data_ptr - got->data);
6868+ put_got_offset(sym_index, got->data_offset);
68106869
68116870 if (dynsym) {
68126871 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
68156874 /* NOTE: we put temporarily the got offset */
68166875 if (reloc_type == R_386_JMP_SLOT) {
68176876 nb_plt_entries++;
6818− offset = got->data_ptr - got->data;
6877+ offset = got->data_offset;
68196878 }
68206879 index = put_elf_sym(dynsym, offset,
68216880 size, info, 0, sym->st_shndx, name);
68226881 /* put a got entry */
68236882 put_elf_reloc(dynsym, got,
6824− got->data_ptr - got->data,
6883+ got->data_offset,
68256884 reloc_type, index);
68266885 }
6827− put32(got->data_ptr, 0);
6828− got->data_ptr += 4;
6886+ ptr = section_ptr_add(got, sizeof(int));
6887+ *ptr = 0;
68296888 }
68306889
68316890 /* build GOT and PLT entries */
68326891 static void build_got_entries(void)
68336892 {
68346893 Section *s, *symtab;
6835− Elf32_Rel *rel;
6894+ Elf32_Rel *rel, *rel_end;
68366895 Elf32_Sym *sym;
68376896 int i, type, reloc_type, sym_index;
68386897
@@ -6844,8 +6903,9 @@ static void build_got_entries(void) · 5 unmodified lines
68446903 if (s->link != symtab_section)
68456904 continue;
68466905 symtab = s->link;
6906+ rel_end = (Elf32_Rel *)(s->data + s->data_offset);
68476907 for(rel = (Elf32_Rel *)s->data;
6848− rel < (Elf32_Rel *)s->data_ptr;
6908+ rel < rel_end;
68496909 rel++) {
68506910 type = ELF32_R_TYPE(rel->r_info);
68516911 switch(type) {
@@ -6882,7 +6942,7 @@ static Section *new_section_hash(const char *name, int sh_flags, · 30 unmodified lines
68826942 ((int *)hash->data)[0] = nb_buckets;
68836943 ((int *)hash->data)[1] = 1;
68846944 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));
68866946 symtab->hash = hash;
68876947 hash->link = symtab;
68886948 return hash;
@@ -6892,24 +6952,24 @@ static Section *new_section_hash(const char *name, int sh_flags, · 3 unmodified lines
68926952 static void put_dt(Section *dynamic, int dt, unsigned long val)
68936953 {
68946954 Elf32_Dyn *dyn;
6895− dyn = (Elf32_Dyn *)dynamic->data_ptr;
6955+ dyn = section_ptr_add(dynamic, sizeof(Elf32_Dyn));
68966956 dyn->d_tag = dt;
68976957 dyn->d_un.d_val = val;
6898− dynamic->data_ptr += sizeof(Elf32_Dyn);
68996958 }
69006959
69016960 /* add tcc runtime libraries */
69026961 static void tcc_add_runtime(TCCState *s1)
69036962 {
69046963 char buf[1024];
6964+ unsigned long *ptr;
69056965
69066966 snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "libtcc1.o");
69076967 tcc_add_file(s1, buf);
69086968 #ifdef CONFIG_TCC_BCHECK
69096969 if (do_bounds_check) {
69106970 /* 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;
69136973 add_elf_sym(symtab_section, 0, 0,
69146974 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
69156975 bounds_section->sh_num, "__bounds_start");
@@ -6928,15 +6988,15 @@ static void tcc_add_runtime(TCCState *s1) · 12 unmodified lines
69286988 }
69296989 /* add various standard linker symbols */
69306990 add_elf_sym(symtab_section,
6931− text_section->data_ptr - text_section->data, 0,
6991+ text_section->data_offset, 0,
69326992 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
69336993 text_section->sh_num, "_etext");
69346994 add_elf_sym(symtab_section,
6935− data_section->data_ptr - data_section->data, 0,
6995+ data_section->data_offset, 0,
69366996 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
69376997 data_section->sh_num, "_edata");
69386998 add_elf_sym(symtab_section,
6939− bss_section->data_ptr - bss_section->data, 0,
6999+ bss_section->data_offset, 0,
69407000 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
69417001 bss_section->sh_num, "_end");
69427002 }
@@ -6962,7 +7022,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 19 unmodified lines
69627022 Elf32_Shdr shdr, *sh;
69637023 Elf32_Phdr *phdr, *ph;
69647024 Section *interp, *plt, *dynamic, *dynstr, *hash;
6965− unsigned char *saved_dynamic_data_ptr;
7025+ unsigned long saved_dynamic_data_offset;
69667026 Elf32_Sym *sym;
69677027 int type, file_type;
69687028 unsigned long rel_addr, rel_size;
@@ -6980,7 +7040,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 11 unmodified lines
69807040 plt = NULL; /* avoid warning */
69817041 hash = NULL; /* avoid warning */
69827042 dynstr = NULL; /* avoid warning */
6983− saved_dynamic_data_ptr = NULL; /* avoid warning */
7043+ saved_dynamic_data_offset = 0; /* avoid warning */
69847044
69857045 if (file_type != TCC_OUTPUT_OBJ) {
69867046
@@ -6989,14 +7049,15 @@ int tcc_output_file(TCCState *s1, const char *filename) · 2 unmodified lines
69897049 if (!static_link) {
69907050 const char *name;
69917051 int sym_index, index;
6992− Elf32_Sym *esym;
7052+ Elf32_Sym *esym, *sym_end;
69937053
69947054 if (file_type == TCC_OUTPUT_EXE) {
7055+ char *ptr;
69957056 /* add interpreter section only if executable */
69967057 interp = new_section(".interp", SHT_PROGBITS, SHF_ALLOC);
69977058 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);
70007061 }
70017062
70027063 /* add dynamic symbol table */
@@ -7025,8 +7086,10 @@ int tcc_output_file(TCCState *s1, const char *filename) · 22 unmodified lines
70257086 dynamic symbols. If a symbol STT_FUNC is found, then we
70267087 add it in the PLT. If a symbol STT_OBJECT is found, we
70277088 add it in the .bss section with a suitable relocation */
7089+ sym_end = (Elf32_Sym *)(symtab_section->data +
7090+ symtab_section->data_offset);
70287091 for(sym = (Elf32_Sym *)symtab_section->data + 1;
7029− sym < (Elf32_Sym *)symtab_section->data_ptr;
7092+ sym < sym_end;
70307093 sym++) {
70317094 if (sym->st_shndx == SHN_UNDEF) {
70327095 name = symtab_section->link->data + sym->st_name;
@@ -7040,7 +7103,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines
70407103 sym - (Elf32_Sym *)symtab_section->data);
70417104 } else if (type == STT_OBJECT) {
70427105 unsigned long offset;
7043− offset = bss_section->data_ptr - bss_section->data;
7106+ offset = bss_section->data_offset;
70447107 /* XXX: which alignment ? */
70457108 offset = (offset + 8 - 1) & -8;
70467109 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
70497112 put_elf_reloc(dynsym, bss_section,
70507113 offset, R_386_COPY, index);
70517114 offset += esym->st_size;
7052− bss_section->data_ptr = bss_section->data + offset;
7115+ bss_section->data_offset = offset;
70537116 }
70547117 } else {
70557118 /* STT_NOTYPE or STB_WEAK undefined symbols
@@ -7068,8 +7131,10 @@ int tcc_output_file(TCCState *s1, const char *filename) · 12 unmodified lines
70687131
70697132 /* now look at unresolved dynamic symbols and export
70707133 corresponding symbol */
7134+ sym_end = (Elf32_Sym *)(dynsymtab_section->data +
7135+ dynsymtab_section->data_offset);
70717136 for(sym = (Elf32_Sym *)dynsymtab_section->data + 1;
7072− sym < (Elf32_Sym *)dynsymtab_section->data_ptr;
7137+ sym < sym_end;
70737138 sym++) {
70747139 if (sym->st_shndx == SHN_UNDEF) {
70757140 name = dynsymtab_section->link->data + sym->st_name;
@@ -7088,7 +7153,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 12 unmodified lines
70887153 build_got_entries();
70897154
70907155 /* 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);
70927157
70937158 /* add a list of needed dlls */
70947159 for(i = 0; i < nb_loaded_dlls; i++) {
@@ -7098,8 +7163,8 @@ int tcc_output_file(TCCState *s1, const char *filename) · 3 unmodified lines
70987163 }
70997164
71007165 /* 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;
71037168 } else {
71047169 /* still need to build got entries in case of static link */
71057170 build_got_entries();
@@ -7143,7 +7208,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 37 unmodified lines
71437208 for(i = 1; i < nb_sections; i++) {
71447209 s = sections[i];
71457210 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;
71477212 }
71487213
71497214 /* allocate program segment headers */
@@ -7264,6 +7329,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 114 unmodified lines
72647329 if (dynamic) {
72657330 int plt_offset;
72667331 unsigned char *p;
7332+ Elf32_Sym *sym_end;
72677333
72687334 ph = &phdr[phnum - 1];
72697335
@@ -7280,42 +7346,41 @@ int tcc_output_file(TCCState *s1, const char *filename) · 10 unmodified lines
72807346 put32(got->data, dynamic->sh_addr);
72817347
72827348 /* compute the PLT */
7283− plt->data_ptr = plt->data;
7349+ plt->data_offset = 0;
72847350
72857351 /* first plt entry */
7286− p = plt->data_ptr;
7352+ p = section_ptr_add(plt, 16);
72877353 p[0] = 0xff; /* pushl got + 4 */
72887354 p[1] = 0x35;
72897355 put32(p + 2, got->sh_addr + 4);
72907356 p[6] = 0xff; /* jmp *(got + 8) */
72917357 p[7] = 0x25;
72927358 put32(p + 8, got->sh_addr + 8);
7293− plt->data_ptr += 16;
72947359
72957360 /* relocation symbols in .dynsym and build PLT. */
72967361 plt_offset = 0;
7362+ sym_end = (Elf32_Sym *)(dynsym->data + dynsym->data_offset);
72977363 for(sym = (Elf32_Sym *)dynsym->data + 1;
7298− sym < (Elf32_Sym *)dynsym->data_ptr;
7364+ sym < sym_end;
72997365 sym++) {
73007366 type = ELF32_ST_TYPE(sym->st_info);
73017367 if (sym->st_shndx == SHN_UNDEF) {
73027368 if (type == STT_FUNC) {
73037369 /* one more entry in PLT */
7304− p = plt->data_ptr;
7370+ p = section_ptr_add(plt, 16);
73057371 p[0] = 0xff; /* jmp *(got + x) */
73067372 p[1] = 0x25;
73077373 put32(p + 2, got->sh_addr + sym->st_value);
73087374 p[6] = 0x68; /* push $xxx */
73097375 put32(p + 7, plt_offset);
73107376 p[11] = 0xe9; /* jmp plt_start */
7311− put32(p + 12, -(plt->data_ptr + 16 - plt->data));
7377+ put32(p + 12, -(plt->data_offset));
73127378
73137379 /* patch symbol value to point to plt */
73147380 sym->st_value = plt->sh_addr + p - plt->data;
73157381
73167382 plt_offset += 8;
7317− plt->data_ptr += 16;
7318− }
7383+ }
73197384 } else if (sym->st_shndx < SHN_LORESERVE) {
73207385 /* do symbol relocation */
73217386 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
73237388 }
73247389 /* put dynamic section entries */
73257390
7326− dynamic->data_ptr = saved_dynamic_data_ptr;
7391+ dynamic->data_offset = saved_dynamic_data_offset;
73277392 put_dt(dynamic, DT_HASH, hash->sh_addr);
73287393 put_dt(dynamic, DT_STRTAB, dynstr->sh_addr);
73297394 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);
73317396 put_dt(dynamic, DT_SYMENT, sizeof(Elf32_Sym));
73327397 put_dt(dynamic, DT_REL, rel_addr);
73337398 put_dt(dynamic, DT_RELSZ, rel_size);
@@ -7363,12 +7428,12 @@ int tcc_output_file(TCCState *s1, const char *filename) · 29 unmodified lines
73637428 /* XXX: ignore sections with allocated relocations ? */
73647429 for(i = 1; i < nb_sections; i++) {
73657430 s = sections[i];
7366− if ((s->sh_flags & SHF_ALLOC)
7367− && s->reloc && s != got)
7431+ if (s->reloc && s != got)
73687432 relocate_section(s);
73697433 }
73707434
7371− /* relocate relocation entries */
7435+ /* relocate relocation entries if the relocation tables are
7436+ allocated in the executable */
73727437 for(i = 1; i < nb_sections; i++) {
73737438 s = sections[i];
73747439 if ((s->sh_flags & SHF_ALLOC) &&
@@ -7434,7 +7499,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 59 unmodified lines
74347499 fputc(0, f);
74357500 offset++;
74367501 }
7437− size = s->data_ptr - s->data;
7502+ size = s->data_offset;
74387503 fwrite(s->data, 1, size, f);
74397504 offset += size;
74407505 }
@@ -7502,7 +7567,7 @@ static int tcc_load_object_file(TCCState *s1, · 61 unmodified lines
75027567 char *sh_name, *name;
75037568 SectionMergeInfo *sm_table, *sm;
75047569 Elf32_Sym *sym, *symtab;
7505− Elf32_Rel *rel;
7570+ Elf32_Rel *rel, *rel_end;
75067571 Section *s;
75077572
75087573 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
@@ -7585,21 +7650,23 @@ static int tcc_load_object_file(TCCState *s1, · 76 unmodified lines
75857650 goto fail;
75867651
75877652 /* align start of section */
7588− offset = s->data_ptr - s->data;
7653+ offset = s->data_offset;
75897654 size = sh->sh_addralign - 1;
75907655 offset = (offset + size) & ~size;
75917656 if (sh->sh_addralign > s->sh_addralign)
75927657 s->sh_addralign = sh->sh_addralign;
7593− s->data_ptr = s->data + offset;
7658+ s->data_offset = offset;
75947659 sm_table[i].offset = offset;
75957660 sm_table[i].s = s;
75967661 /* concatenate sections */
75977662 size = sh->sh_size;
75987663 if (sh->sh_type != SHT_NOBITS) {
7664+ unsigned char *ptr;
75997665 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);
76017668 }
7602− s->data_ptr += size;
7669+ s->data_offset += size;
76037670 }
76047671
76057672 /* 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
76557722 case SHT_REL:
76567723 /* take relocation offset information */
76577724 offseti = sm_table[sh->sh_info].offset;
7725+ rel_end = (Elf32_Rel *)(s->data + s->data_offset);
76587726 for(rel = (Elf32_Rel *)(s->data + offset);
7659− rel < (Elf32_Rel *)s->data_ptr;
7727+ rel < rel_end;
76607728 rel++) {
76617729 int type;
76627730 unsigned sym_index;
@@ -7945,7 +8013,7 @@ static void rt_printline(unsigned long wanted_pc) · 282 unmodified lines
79458013 last_pc = 0xffffffff;
79468014 last_line_num = 1;
79478015 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);
79498017 while (sym < sym_end) {
79508018 switch(sym->n_type) {
79518019 /* function start or end */
@@ -8098,7 +8166,7 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 146 unmodified lines
80988166 /* relocate each section */
80998167 for(i = 1; i < nb_sections; i++) {
81008168 s = sections[i];
8101− if ((s->sh_flags & SHF_ALLOC) && s->reloc)
8169+ if (s->reloc)
81028170 relocate_section(s);
81038171 }
81048172

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