History

better DLL generation - added size to all ELF symbols - dynamic hash table size

bellard committed 24 years ago

Commit 7ac43f61 parent 18e40e11 file · +305 −175Browse files

Unified · Split

tcc.c+305−175
@@ -76,8 +76,6 @@ · 75 unmodified lines
76#define TOK_HASH_SIZE 2048 /* must be a power of two */76#define TOK_HASH_SIZE 2048 /* must be a power of two */
77#define TOK_ALLOC_INCR 512 /* must be a power of two */77#define TOK_ALLOC_INCR 512 /* must be a power of two */
78#define SYM_HASH_SIZE 103178#define SYM_HASH_SIZE 1031
79#define ELF_SYM_HASH_SIZE 2048
80#define ELF_DYNSYM_HASH_SIZE 32
81 79
82/* token symbol management */80/* token symbol management */
83typedef struct TokenSym {81typedef struct TokenSym {
@@ -147,6 +145,7 @@ typedef struct Section { · 63 unmodified lines
147 unsigned long sh_size; /* section size (only used during output) */145 unsigned long sh_size; /* section size (only used during output) */
148 unsigned long sh_addr; /* address at which the section is relocated */146 unsigned long sh_addr; /* address at which the section is relocated */
149 unsigned long sh_offset; /* address at which the section is relocated */147 unsigned long sh_offset; /* address at which the section is relocated */
148 int nb_hashed_syms; /* used to resize the hash table */
150 struct Section *link; /* link to another section */149 struct Section *link; /* link to another section */
151 struct Section *reloc; /* corresponding section for relocation, if any */150 struct Section *reloc; /* corresponding section for relocation, if any */
152 struct Section *hash; /* hash table for symbols */151 struct Section *hash; /* hash table for symbols */
@@ -238,7 +237,7 @@ Section *lbounds_section; /* contains local data bound description */ · 85 unmodified lines
238/* symbol sections */237/* symbol sections */
239Section *symtab_section, *strtab_section;238Section *symtab_section, *strtab_section;
240/* temporary dynamic symbol sections (for dll loading) */239/* temporary dynamic symbol sections (for dll loading) */
241Section *dynsymtab_section, *dynstrtab_section;240Section *dynsymtab_section;
242/* exported dynamic symbol section */241/* exported dynamic symbol section */
243Section *dynsym;242Section *dynsym;
244/* got handling */243/* got handling */
@@ -246,7 +245,8 @@ Section *got; · 1 unmodified line
246unsigned long *got_offsets;245unsigned long *got_offsets;
247int nb_got_offsets;246int nb_got_offsets;
248int nb_plt_entries;247int nb_plt_entries;
249 248/* give the correspondance from symtab indexes to dynsym indexes */
249int *symtab_to_dynsym;
250 250
251/* array of all loaded dlls (including those referenced by loaded251/* array of all loaded dlls (including those referenced by loaded
252 dlls) */252 dlls) */
@@ -596,8 +596,9 @@ static Sym *get_sym_ref(int t, Section *sec, · 343 unmodified lines
596/* section generation */596/* section generation */
597static void *section_ptr(Section *sec, unsigned long size);597static void *section_ptr(Section *sec, unsigned long size);
598static void *section_ptr_add(Section *sec, unsigned long size);598static void *section_ptr_add(Section *sec, unsigned long size);
599void put_extern_sym(Sym *sym, Section *section, unsigned long value);599static void put_extern_sym(Sym *sym, Section *section,
600void greloc(Section *s, Sym *sym, unsigned long addr, int type);600 unsigned long value, unsigned long size);
601static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
601static int put_elf_str(Section *s, const char *sym);602static int put_elf_str(Section *s, const char *sym);
602static int put_elf_sym(Section *s, 603static int put_elf_sym(Section *s,
603 unsigned long value, unsigned long size,604 unsigned long value, unsigned long size,
@@ -822,10 +823,10 @@ Section *find_section(const char *name) · 218 unmodified lines
822}823}
823 824
824/* add a new relocation entry to symbol 'sym' in section 's' */825/* add a new relocation entry to symbol 'sym' in section 's' */
825void greloc(Section *s, Sym *sym, unsigned long offset, int type)826static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
826{827{
827 if (!sym->c) 828 if (!sym->c)
828 put_extern_sym(sym, NULL, 0);829 put_extern_sym(sym, NULL, 0, 0);
829 /* now we can add ELF relocation info */830 /* now we can add ELF relocation info */
830 put_elf_reloc(symtab_section, s, offset, type, sym->c);831 put_elf_reloc(symtab_section, s, offset, type, sym->c);
831}832}
@@ -2536,17 +2537,16 @@ static Sym *get_sym_ref(int t, Section *sec, · 1704 unmodified lines
2536 v = anon_sym++;2537 v = anon_sym++;
2537 sym = sym_push1(&global_stack, v, t | VT_STATIC, 0);2538 sym = sym_push1(&global_stack, v, t | VT_STATIC, 0);
2538 sym->r = VT_CONST | VT_SYM;2539 sym->r = VT_CONST | VT_SYM;
2539 put_extern_sym(sym, sec, offset);2540 put_extern_sym(sym, sec, offset, size);
2540 return sym;2541 return sym;
2541}2542}
2542 2543
2543/* push a reference to a section offset by adding a dummy symbol */2544/* push a reference to a section offset by adding a dummy symbol */
2544/* XXX: add size */2545void vpush_ref(int t, Section *sec, unsigned long offset, unsigned long size)
2545void vpush_ref(int t, Section *sec, unsigned long offset)
2546{2546{
2547 CValue cval;2547 CValue cval;
2548 2548
2549 cval.sym = get_sym_ref(t, sec, offset, 0);2549 cval.sym = get_sym_ref(t, sec, offset, size);
2550 vsetc(t, VT_CONST | VT_SYM, &cval);2550 vsetc(t, VT_CONST | VT_SYM, &cval);
2551}2551}
2552 2552
@@ -2760,30 +2760,24 @@ int gv(int rc) · 207 unmodified lines
2760 } else {2760 } else {
2761 if (is_float(vtop->t) && 2761 if (is_float(vtop->t) &&
2762 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {2762 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
2763 int v;
2764 Sym *sym;2763 Sym *sym;
2765 int *ptr;2764 int *ptr;
2766 2765 unsigned long offset;
2766
2767 /* XXX: unify with initializers handling ? */2767 /* XXX: unify with initializers handling ? */
2768 /* CPUs usually cannot use float constants, so we store them2768 /* CPUs usually cannot use float constants, so we store them
2769 generically in data segment */2769 generically in data segment */
2770 size = type_size(vtop->t, &align);2770 size = type_size(vtop->t, &align);
2771 data_section->data_offset = (data_section->data_offset + 2771 offset = (data_section->data_offset + align - 1) & -align;
2772 align - 1) & -align;2772 data_section->data_offset = offset;
2773 /* XXX: not portable yet */2773 /* XXX: not portable yet */
2774 ptr = section_ptr(data_section, size);2774 ptr = section_ptr_add(data_section, size);
2775 size = size >> 2;2775 size = size >> 2;
2776 for(i=0;i<size;i++)2776 for(i=0;i<size;i++)
2777 ptr[i] = vtop->c.tab[i];2777 ptr[i] = vtop->c.tab[i];
2778 2778 sym = get_sym_ref(vtop->t, data_section, offset, size << 2);
2779 v = anon_sym++;
2780 sym = sym_push1(&global_stack, v, vtop->t | VT_STATIC, 0);
2781 sym->r = VT_CONST | VT_SYM;
2782 put_extern_sym(sym, data_section, data_section->data_offset);
2783
2784 vtop->r |= VT_LVAL | VT_SYM;2779 vtop->r |= VT_LVAL | VT_SYM;
2785 vtop->c.sym = sym;2780 vtop->c.sym = sym;
2786 data_section->data_offset += size << 2;
2787 }2781 }
2788#ifdef CONFIG_TCC_BCHECK2782#ifdef CONFIG_TCC_BCHECK
2789 if (vtop->r & VT_MUSTBOUND) 2783 if (vtop->r & VT_MUSTBOUND)
@@ -4706,7 +4700,7 @@ void unary(void) · 1916 unmodified lines
4706 t = VT_ARRAY | mk_pointer(VT_BYTE);4700 t = VT_ARRAY | mk_pointer(VT_BYTE);
4707 s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));4701 s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
4708 s->c = len;4702 s->c = len;
4709 vpush_ref(t, data_section, data_section->data_offset);4703 vpush_ref(t, data_section, data_section->data_offset, len);
4710 ptr = section_ptr_add(data_section, len);4704 ptr = section_ptr_add(data_section, len);
4711 memcpy(ptr, funcname, len);4705 memcpy(ptr, funcname, len);
4712 next();4706 next();
@@ -4724,9 +4718,9 @@ void unary(void) · 11 unmodified lines
4724 /* we must declare it as an array first to use initializer parser */4718 /* we must declare it as an array first to use initializer parser */
4725 t = VT_ARRAY | mk_pointer(t);4719 t = VT_ARRAY | mk_pointer(t);
4726 decl_initializer(t, data_section, data_offset, 1, 0);4720 decl_initializer(t, data_section, data_offset, 1, 0);
4727 data_offset += type_size(t, &align);4721 size = type_size(t, &align);
4728 /* XXX: is it correct to put it exactly as an array ? */4722 data_offset += size;
4729 vpush_ref(t, data_section, fc);4723 vpush_ref(t, data_section, fc, size);
4730 data_section->data_offset = data_offset;4724 data_section->data_offset = data_offset;
4731 } else {4725 } else {
4732 t = tok;4726 t = tok;
@@ -5900,12 +5894,12 @@ void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 1167 unmodified lines
5900 do_def:5894 do_def:
5901 sym = sym_push(v, t, r | VT_SYM, 0);5895 sym = sym_push(v, t, r | VT_SYM, 0);
5902 }5896 }
5903 put_extern_sym(sym, sec, addr);5897 put_extern_sym(sym, sec, addr, size);
5904 } else {5898 } else {
5905 CValue cval;5899 CValue cval;
5906 5900
5907 /* push global reference */5901 /* push global reference */
5908 sym = get_sym_ref(t, sec, addr, 0);5902 sym = get_sym_ref(t, sec, addr, size);
5909 cval.sym = sym;5903 cval.sym = sym;
5910 vsetc(t, VT_CONST | VT_SYM, &cval);5904 vsetc(t, VT_CONST | VT_SYM, &cval);
5911 }5905 }
@@ -5942,7 +5936,6 @@ void put_func_debug(Sym *sym) · 30 unmodified lines
5942 funcname, sym->t & VT_STATIC ? 'f' : 'F');5936 funcname, sym->t & VT_STATIC ? 'f' : 'F');
5943 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,5937 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5944 cur_text_section, sym->c);5938 cur_text_section, sym->c);
5945 func_ind = ind;
5946 last_ind = 0;5939 last_ind = 0;
5947 last_line_num = 0;5940 last_line_num = 0;
5948}5941}
@@ -6100,7 +6093,9 @@ void decl(int l) · 151 unmodified lines
6100 /* put function symbol */6093 /* put function symbol */
6101 sym = sym_push1(&global_stack, v, t, 0);6094 sym = sym_push1(&global_stack, v, t, 0);
6102 }6095 }
6103 put_extern_sym(sym, cur_text_section, ind);6096 /* NOTE: we patch the symbol size later */
6097 put_extern_sym(sym, cur_text_section, ind, 0);
6098 func_ind = ind;
6104 sym->r = VT_SYM | VT_CONST;6099 sym->r = VT_SYM | VT_CONST;
6105 /* put debug symbol */6100 /* put debug symbol */
6106 if (do_debug)6101 if (do_debug)
@@ -6121,6 +6116,9 @@ void decl(int l) · 14 unmodified lines
6121 sym_pop(&label_stack, NULL); /* reset label stack */6116 sym_pop(&label_stack, NULL); /* reset label stack */
6122 sym_pop(&local_stack, NULL); /* reset local stack */6117 sym_pop(&local_stack, NULL); /* reset local stack */
6123 /* end of function */6118 /* end of function */
6119 /* patch symbol size */
6120 ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
6121 ind - func_ind;
6124 if (do_debug) {6122 if (do_debug) {
6125 put_stabn(N_FUN, 0, 0, ind - func_ind);6123 put_stabn(N_FUN, 0, 0, ind - func_ind);
6126 }6124 }
@@ -6322,6 +6320,40 @@ static unsigned long elf_hash(const unsigned char *name) · 195 unmodified lines
6322 return h;6320 return h;
6323}6321}
6324 6322
6323/* rebuild hash table of section s */
6324/* NOTE: we do factorize the hash table code to go faster */
6325static void rebuild_hash(Section *s, unsigned int nb_buckets)
6326{
6327 Elf32_Sym *sym;
6328 int *ptr, *hash, nb_syms, sym_index, h;
6329 char *strtab;
6330
6331 strtab = s->link->data;
6332 nb_syms = s->data_offset / sizeof(Elf32_Sym);
6333
6334 s->hash->data_offset = 0;
6335 ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int));
6336 ptr[0] = nb_buckets;
6337 ptr[1] = nb_syms;
6338 ptr += 2;
6339 hash = ptr;
6340 memset(hash, 0, (nb_buckets + 1) * sizeof(int));
6341 ptr += nb_buckets + 1;
6342
6343 sym = (Elf32_Sym *)s->data + 1;
6344 for(sym_index = 1; sym_index < nb_syms; sym_index++) {
6345 if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {
6346 h = elf_hash(strtab + sym->st_name) % nb_buckets;
6347 *ptr = hash[h];
6348 hash[h] = sym_index;
6349 } else {
6350 *ptr = 0;
6351 }
6352 ptr++;
6353 sym++;
6354 }
6355}
6356
6325/* return the symbol number */6357/* return the symbol number */
6326static int put_elf_sym(Section *s, 6358static int put_elf_sym(Section *s,
6327 unsigned long value, unsigned long size,6359 unsigned long value, unsigned long size,
@@ -6347,20 +6379,26 @@ static int put_elf_sym(Section *s, · 19 unmodified lines
6347 sym_index = sym - (Elf32_Sym *)s->data;6379 sym_index = sym - (Elf32_Sym *)s->data;
6348 hs = s->hash;6380 hs = s->hash;
6349 if (hs) {6381 if (hs) {
6350 int *ptr;6382 int *ptr, *base;
6351 ptr = section_ptr_add(hs, sizeof(int));6383 ptr = section_ptr_add(hs, sizeof(int));
6384 base = (int *)hs->data;
6352 /* only add global or weak symbols */6385 /* only add global or weak symbols */
6353 if (ELF32_ST_BIND(info) != STB_LOCAL) {6386 if (ELF32_ST_BIND(info) != STB_LOCAL) {
6354 /* add another hashing entry */6387 /* add another hashing entry */
6355 nbuckets = ((int *)hs->data)[0];6388 nbuckets = base[0];
6356 h = elf_hash(name) % nbuckets;6389 h = elf_hash(name) % nbuckets;
6357 *ptr = ((int *)hs->data)[2 + h];6390 *ptr = base[2 + h];
6358 ((int *)hs->data)[2 + h] = sym_index;6391 base[2 + h] = sym_index;
6392 base[1]++;
6393 /* we resize the hash table */
6394 hs->nb_hashed_syms++;
6395 if (hs->nb_hashed_syms > 2 * nbuckets) {
6396 rebuild_hash(s, 2 * nbuckets);
6397 }
6359 } else {6398 } else {
6360 *ptr = 0;6399 *ptr = 0;
6400 base[1]++;
6361 }6401 }
6362 /* but still add room for all symbols */
6363 ((int *)hs->data)[1]++;
6364 }6402 }
6365 return sym_index;6403 return sym_index;
6366}6404}
@@ -6457,7 +6495,8 @@ static int add_elf_sym(Section *s, unsigned long value, unsigned long size, · 90 unmodified lines
6457 6495
6458/* update sym->c so that it points to an external symbol in section6496/* update sym->c so that it points to an external symbol in section
6459 'section' with value 'value' */6497 'section' with value 'value' */
6460void put_extern_sym(Sym *sym, Section *section, unsigned long value)6498static void put_extern_sym(Sym *sym, Section *section,
6499 unsigned long value, unsigned long size)
6461{6500{
6462 int sym_type, sym_bind, sh_num, info;6501 int sym_type, sym_bind, sh_num, info;
6463 Elf32_Sym *esym;6502 Elf32_Sym *esym;
@@ -6502,10 +6541,11 @@ void put_extern_sym(Sym *sym, Section *section, unsigned long value) · 38 unmodified lines
6502 }6541 }
6503#endif6542#endif
6504 info = ELF32_ST_INFO(sym_bind, sym_type);6543 info = ELF32_ST_INFO(sym_bind, sym_type);
6505 sym->c = add_elf_sym(symtab_section, value, 0, info, sh_num, name);6544 sym->c = add_elf_sym(symtab_section, value, size, info, sh_num, name);
6506 } else {6545 } else {
6507 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];6546 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
6508 esym->st_value = value;6547 esym->st_value = value;
6548 esym->st_size = size;
6509 esym->st_shndx = sh_num;6549 esym->st_shndx = sh_num;
6510 }6550 }
6511}6551}
@@ -6728,50 +6768,20 @@ static void relocate_syms(int do_resolve) · 216 unmodified lines
6728 }6768 }
6729}6769}
6730 6770
6731/* elf relocation, CPU dependant */6771/* relocate a given section (CPU dependant) */
6732static void elf_reloc(unsigned char *ptr, 6772static void relocate_section(TCCState *s1, Section *s)
6733 unsigned long addr, unsigned long val, int type,
6734 int sym_index)
6735{
6736 switch(type) {
6737 case R_386_32:
6738 *(int *)ptr += val;
6739 break;
6740 case R_386_PLT32:
6741 case R_386_PC32:
6742 *(int *)ptr += val - addr;
6743 break;
6744 case R_386_GLOB_DAT:
6745 case R_386_JMP_SLOT:
6746 *(int *)ptr = val;
6747 break;
6748 case R_386_GOTPC:
6749 *(int *)ptr += got->sh_addr - addr;
6750 break;
6751 case R_386_GOTOFF:
6752 *(int *)ptr += val - got->sh_addr;
6753 break;
6754 case R_386_GOT32:
6755 /* we load the got offset */
6756 *(int *)ptr += got_offsets[sym_index];
6757 break;
6758 }
6759}
6760
6761
6762/* relocate a given section */
6763static void relocate_section(Section *s)
6764{6773{
6765 Section *sr;6774 Section *sr;
6766 Elf32_Rel *rel, *rel_end;6775 Elf32_Rel *rel, *rel_end, *qrel;
6767 Elf32_Sym *sym;6776 Elf32_Sym *sym;
6768 int type, sym_index;6777 int type, sym_index, esym_index;
6769 unsigned char *ptr;6778 unsigned char *ptr;
6770 unsigned long val;6779 unsigned long val, addr;
6771 6780
6772 sr = s->reloc;6781 sr = s->reloc;
6773 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);6782 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
6774 for(rel = (Elf32_Rel *)sr->data;6783 qrel = (Elf32_Rel *)sr->data;
6784 for(rel = qrel;
6775 rel < rel_end;6785 rel < rel_end;
6776 rel++) {6786 rel++) {
6777 ptr = s->data + rel->r_offset;6787 ptr = s->data + rel->r_offset;
@@ -6780,8 +6790,60 @@ static void relocate_section(Section *s) · 2 unmodified lines
6780 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];6790 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
6781 val = sym->st_value;6791 val = sym->st_value;
6782 type = ELF32_R_TYPE(rel->r_info);6792 type = ELF32_R_TYPE(rel->r_info);
6783 elf_reloc(ptr, s->sh_addr + rel->r_offset, val, type, sym_index);6793 addr = s->sh_addr + rel->r_offset;
6794
6795 /* CPU specific */
6796 switch(type) {
6797 case R_386_32:
6798 if (s1->output_type == TCC_OUTPUT_DLL) {
6799 esym_index = symtab_to_dynsym[sym_index];
6800 qrel->r_offset = rel->r_offset;
6801 if (esym_index) {
6802 qrel->r_info = ELF32_R_INFO(esym_index, R_386_32);
6803 qrel++;
6804 break;
6805 } else {
6806 qrel->r_info = ELF32_R_INFO(0, R_386_RELATIVE);
6807 qrel++;
6808 }
6809 }
6810 *(int *)ptr += val;
6811 break;
6812 case R_386_PC32:
6813 if (s1->output_type == TCC_OUTPUT_DLL) {
6814 /* DLL relocation */
6815 esym_index = symtab_to_dynsym[sym_index];
6816 if (esym_index) {
6817 qrel->r_offset = rel->r_offset;
6818 qrel->r_info = ELF32_R_INFO(esym_index, R_386_PC32);
6819 qrel++;
6820 break;
6821 }
6822 }
6823 *(int *)ptr += val - addr;
6824 break;
6825 case R_386_PLT32:
6826 *(int *)ptr += val - addr;
6827 break;
6828 case R_386_GLOB_DAT:
6829 case R_386_JMP_SLOT:
6830 *(int *)ptr = val;
6831 break;
6832 case R_386_GOTPC:
6833 *(int *)ptr += got->sh_addr - addr;
6834 break;
6835 case R_386_GOTOFF:
6836 *(int *)ptr += val - got->sh_addr;
6837 break;
6838 case R_386_GOT32:
6839 /* we load the got offset */
6840 *(int *)ptr += got_offsets[sym_index];
6841 break;
6842 }
6784 }6843 }
6844 /* if the relocation is allocated, we change its symbol table */
6845 if (sr->sh_flags & SHF_ALLOC)
6846 sr->link = dynsym;
6785}6847}
6786 6848
6787/* relocate relocation table in 'sr' */6849/* relocate relocation table in 'sr' */
@@ -6799,6 +6861,39 @@ static void relocate_rel(Section *sr) · 11 unmodified lines
6799 }6861 }
6800}6862}
6801 6863
6864/* count the number of dynamic relocations so that we can reserve
6865 their space */
6866static int prepare_dynamic_rel(Section *sr)
6867{
6868 Elf32_Rel *rel, *rel_end;
6869 int sym_index, esym_index, type, count;
6870
6871 count = 0;
6872 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
6873 for(rel = (Elf32_Rel *)sr->data; rel < rel_end; rel++) {
6874 sym_index = ELF32_R_SYM(rel->r_info);
6875 type = ELF32_R_TYPE(rel->r_info);
6876 switch(type) {
6877 case R_386_32:
6878 count++;
6879 break;
6880 case R_386_PC32:
6881 esym_index = symtab_to_dynsym[sym_index];
6882 if (esym_index)
6883 count++;
6884 break;
6885 default:
6886 break;
6887 }
6888 }
6889 if (count) {
6890 /* allocate the section */
6891 sr->sh_flags |= SHF_ALLOC;
6892 sr->sh_size = count * sizeof(Elf32_Rel);
6893 }
6894 return count;
6895}
6896
6802static void put_got_offset(int index, unsigned long val)6897static void put_got_offset(int index, unsigned long val)
6803{6898{
6804 int n;6899 int n;
@@ -6934,18 +7029,32 @@ static void build_got_entries(void) · 129 unmodified lines
6934 }7029 }
6935}7030}
6936 7031
6937static Section *new_section_hash(const char *name, int sh_flags, 7032static Section *new_symtab(const char *symtab_name, int sh_type, int sh_flags,
6938 int nb_buckets, Section *symtab)7033 const char *strtab_name,
7034 const char *hash_name, int hash_sh_flags)
6939{7035{
6940 Section *hash;7036 Section *symtab, *strtab, *hash;
6941 hash = new_section(name, SHT_HASH, sh_flags);7037 int *ptr, nb_buckets;
6942 ((int *)hash->data)[0] = nb_buckets;7038
6943 ((int *)hash->data)[1] = 1;7039 symtab = new_section(symtab_name, sh_type, sh_flags);
7040 symtab->sh_entsize = sizeof(Elf32_Sym);
7041 strtab = new_section(strtab_name, SHT_STRTAB, sh_flags);
7042 put_elf_str(strtab, "");
7043 symtab->link = strtab;
7044 put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);
7045
7046 nb_buckets = 1;
7047
7048 hash = new_section(hash_name, SHT_HASH, hash_sh_flags);
6944 hash->sh_entsize = sizeof(int);7049 hash->sh_entsize = sizeof(int);
6945 section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int));
6946 symtab->hash = hash;7050 symtab->hash = hash;
6947 hash->link = symtab;7051 hash->link = symtab;
6948 return hash;7052
7053 ptr = section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int));
7054 ptr[0] = nb_buckets;
7055 ptr[1] = 1;
7056 memset(ptr + 2, 0, (nb_buckets + 1) * sizeof(int));
7057 return symtab;
6949}7058}
6950 7059
6951/* put dynamic tag */7060/* put dynamic tag */
@@ -7055,7 +7164,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 103 unmodified lines
7055 Section *strsec, *s;7164 Section *strsec, *s;
7056 Elf32_Shdr shdr, *sh;7165 Elf32_Shdr shdr, *sh;
7057 Elf32_Phdr *phdr, *ph;7166 Elf32_Phdr *phdr, *ph;
7058 Section *interp, *plt, *dynamic, *dynstr, *hash;7167 Section *interp, *plt, *dynamic, *dynstr;
7059 unsigned long saved_dynamic_data_offset;7168 unsigned long saved_dynamic_data_offset;
7060 Elf32_Sym *sym;7169 Elf32_Sym *sym;
7061 int type, file_type;7170 int type, file_type;
@@ -7072,7 +7181,6 @@ int tcc_output_file(TCCState *s1, const char *filename) · 10 unmodified lines
7072 got = NULL;7181 got = NULL;
7073 nb_plt_entries = 0;7182 nb_plt_entries = 0;
7074 plt = NULL; /* avoid warning */7183 plt = NULL; /* avoid warning */
7075 hash = NULL; /* avoid warning */
7076 dynstr = NULL; /* avoid warning */7184 dynstr = NULL; /* avoid warning */
7077 saved_dynamic_data_offset = 0; /* avoid warning */7185 saved_dynamic_data_offset = 0; /* avoid warning */
7078 7186
@@ -7095,17 +7203,11 @@ int tcc_output_file(TCCState *s1, const char *filename) · 16 unmodified lines
7095 }7203 }
7096 7204
7097 /* add dynamic symbol table */7205 /* add dynamic symbol table */
7098 dynsym = new_section(".dynsym", SHT_DYNSYM, SHF_ALLOC);7206 dynsym = new_symtab(".dynsym", SHT_DYNSYM, SHF_ALLOC,
7099 dynsym->sh_entsize = sizeof(Elf32_Sym);7207 ".dynstr",
7100 dynstr = new_section(".dynstr", SHT_STRTAB, SHF_ALLOC);7208 ".hash", SHF_ALLOC);
7101 put_elf_str(dynstr, "");7209 dynstr = dynsym->link;
7102 dynsym->link = dynstr;7210
7103 put_elf_sym(dynsym, 0, 0, 0, 0, 0, NULL);
7104
7105 /* hash table */
7106 hash = new_section_hash(".hash", SHF_ALLOC,
7107 ELF_DYNSYM_HASH_SIZE, dynsym);
7108
7109 /* add dynamic section */7211 /* add dynamic section */
7110 dynamic = new_section(".dynamic", SHT_DYNAMIC, 7212 dynamic = new_section(".dynamic", SHT_DYNAMIC,
7111 SHF_ALLOC | SHF_WRITE);7213 SHF_ALLOC | SHF_WRITE);
@@ -7116,72 +7218,98 @@ int tcc_output_file(TCCState *s1, const char *filename) · 4 unmodified lines
7116 plt = new_section(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);7218 plt = new_section(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
7117 plt->sh_entsize = 4;7219 plt->sh_entsize = 4;
7118 7220
7221 build_got();
7222
7119 /* scan for undefined symbols and see if they are in the7223 /* scan for undefined symbols and see if they are in the
7120 dynamic symbols. If a symbol STT_FUNC is found, then we7224 dynamic symbols. If a symbol STT_FUNC is found, then we
7121 add it in the PLT. If a symbol STT_OBJECT is found, we7225 add it in the PLT. If a symbol STT_OBJECT is found, we
7122 add it in the .bss section with a suitable relocation */7226 add it in the .bss section with a suitable relocation */
7123 sym_end = (Elf32_Sym *)(symtab_section->data + 7227 sym_end = (Elf32_Sym *)(symtab_section->data +
7124 symtab_section->data_offset);7228 symtab_section->data_offset);
7125 for(sym = (Elf32_Sym *)symtab_section->data + 1; 7229 if (file_type == TCC_OUTPUT_EXE) {
7126 sym < sym_end;7230 for(sym = (Elf32_Sym *)symtab_section->data + 1;
7127 sym++) {7231 sym < sym_end;
7128 if (sym->st_shndx == SHN_UNDEF) {7232 sym++) {
7129 name = symtab_section->link->data + sym->st_name;7233 if (sym->st_shndx == SHN_UNDEF) {
7130 sym_index = find_elf_sym(dynsymtab_section, name);7234 name = symtab_section->link->data + sym->st_name;
7131 if (sym_index) {7235 sym_index = find_elf_sym(dynsymtab_section, name);
7132 esym = &((Elf32_Sym *)dynsymtab_section->data)[sym_index];7236 if (sym_index) {
7133 type = ELF32_ST_TYPE(esym->st_info);7237 esym = &((Elf32_Sym *)dynsymtab_section->data)[sym_index];
7134 if (type == STT_FUNC) {7238 type = ELF32_ST_TYPE(esym->st_info);
7135 put_got_entry(R_386_JMP_SLOT, esym->st_size, 7239 if (type == STT_FUNC) {
7136 esym->st_info, 7240 put_got_entry(R_386_JMP_SLOT, esym->st_size,
7137 sym - (Elf32_Sym *)symtab_section->data);7241 esym->st_info,
7138 } else if (type == STT_OBJECT) {7242 sym - (Elf32_Sym *)symtab_section->data);
7139 unsigned long offset;7243 } else if (type == STT_OBJECT) {
7140 offset = bss_section->data_offset;7244 unsigned long offset;
7141 /* XXX: which alignment ? */7245 offset = bss_section->data_offset;
7142 offset = (offset + 8 - 1) & -8;7246 /* XXX: which alignment ? */
7143 index = put_elf_sym(dynsym, offset, esym->st_size, 7247 offset = (offset + 8 - 1) & -8;
7144 esym->st_info, 0, 7248 index = put_elf_sym(dynsym, offset, esym->st_size,
7145 bss_section->sh_num, name);7249 esym->st_info, 0,
7146 put_elf_reloc(dynsym, bss_section, 7250 bss_section->sh_num, name);
7147 offset, R_386_COPY, index);7251 put_elf_reloc(dynsym, bss_section,
7148 offset += esym->st_size;7252 offset, R_386_COPY, index);
7149 bss_section->data_offset = offset;7253 offset += esym->st_size;
7150 }7254 bss_section->data_offset = offset;
7151 } else {7255 }
7152 /* STB_WEAK undefined symbols are accepted */
7153 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
7154 it */
7155 if (ELF32_ST_BIND(sym->st_info) == STB_WEAK ||
7156 !strcmp(name, "_fp_hw")) {
7157 } else {7256 } else {
7158 error("undefined symbol '%s'", name);7257 /* STB_WEAK undefined symbols are accepted */
7258 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
7259 it */
7260 if (ELF32_ST_BIND(sym->st_info) == STB_WEAK ||
7261 !strcmp(name, "_fp_hw")) {
7262 } else {
7263 error("undefined symbol '%s'", name);
7264 }
7159 }7265 }
7160 }7266 }
7161 }7267 }
7162 }
7163 7268
7164 /* now look at unresolved dynamic symbols and export7269 /* now look at unresolved dynamic symbols and export
7165 corresponding symbol */7270 corresponding symbol */
7166 sym_end = (Elf32_Sym *)(dynsymtab_section->data + 7271 sym_end = (Elf32_Sym *)(dynsymtab_section->data +
7167 dynsymtab_section->data_offset);7272 dynsymtab_section->data_offset);
7168 for(sym = (Elf32_Sym *)dynsymtab_section->data + 1; 7273 for(esym = (Elf32_Sym *)dynsymtab_section->data + 1;
7169 sym < sym_end;7274 esym < sym_end;
7170 sym++) {7275 esym++) {
7171 if (sym->st_shndx == SHN_UNDEF) {7276 if (esym->st_shndx == SHN_UNDEF) {
7172 name = dynsymtab_section->link->data + sym->st_name;7277 name = dynsymtab_section->link->data + esym->st_name;
7173 sym_index = find_elf_sym(symtab_section, name);7278 sym_index = find_elf_sym(symtab_section, name);
7174 if (sym_index) {7279 if (sym_index) {
7175 } else {7280 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
7176 if (ELF32_ST_BIND(sym->st_info) == STB_WEAK) {7281 put_elf_sym(dynsym, sym->st_value, sym->st_size,
7177 /* weak symbols can stay undefined */7282 sym->st_info, 0,
7283 sym->st_shndx, name);
7178 } else {7284 } else {
7179 warning("undefined dynamic symbol '%s'", name);7285 if (ELF32_ST_BIND(esym->st_info) == STB_WEAK) {
7286 /* weak symbols can stay undefined */
7287 } else {
7288 warning("undefined dynamic symbol '%s'", name);
7289 }
7180 }7290 }
7181 }7291 }
7182 }7292 }
7293 } else {
7294 int nb_syms;
7295 /* shared library case : we simply export all the global symbols */
7296 nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);
7297 symtab_to_dynsym = malloc(sizeof(int) * nb_syms);
7298 memset(symtab_to_dynsym, 0, sizeof(int) * nb_syms);
7299 for(sym = (Elf32_Sym *)symtab_section->data + 1;
7300 sym < sym_end;
7301 sym++) {
7302 if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {
7303 name = symtab_section->link->data + sym->st_name;
7304 index = put_elf_sym(dynsym, sym->st_value, sym->st_size,
7305 sym->st_info, 0,
7306 sym->st_shndx, name);
7307 symtab_to_dynsym[sym - (Elf32_Sym *)symtab_section->data] =
7308 index;
7309 }
7310 }
7183 }7311 }
7184 7312
7185 build_got_entries();7313 build_got_entries();
7186 7314
7187 /* update PLT/GOT sizes so that we can allocate their space */7315 /* update PLT/GOT sizes so that we can allocate their space */
@@ -7193,6 +7321,10 @@ int tcc_output_file(TCCState *s1, const char *filename) · 5 unmodified lines
7193 if (dllref->level == 0)7321 if (dllref->level == 0)
7194 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));7322 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
7195 }7323 }
7324 /* XXX: currently, since we do not handle PIC code, we
7325 must relocate the readonly segments */
7326 if (file_type == TCC_OUTPUT_DLL)
7327 put_dt(dynamic, DT_TEXTREL, 0);
7196 7328
7197 /* add necessary space for other entries */7329 /* add necessary space for other entries */
7198 saved_dynamic_data_offset = dynamic->data_offset;7330 saved_dynamic_data_offset = dynamic->data_offset;
@@ -7243,11 +7375,17 @@ int tcc_output_file(TCCState *s1, const char *filename) · 44 unmodified lines
7243 for(i = 1; i < nb_sections; i++) {7375 for(i = 1; i < nb_sections; i++) {
7244 s = sections[i];7376 s = sections[i];
7245 s->sh_name = put_elf_str(strsec, s->name);7377 s->sh_name = put_elf_str(strsec, s->name);
7246 /* we output all sections if debug or object file */7378 /* when generating a DLL, we include relocations but we may
7247 if (do_debug || 7379 patch them */
7380 if (file_type == TCC_OUTPUT_DLL &&
7381 s->sh_type == SHT_REL &&
7382 !(s->sh_flags & SHF_ALLOC)) {
7383 prepare_dynamic_rel(s);
7384 } else if (do_debug ||
7248 file_type == TCC_OUTPUT_OBJ || 7385 file_type == TCC_OUTPUT_OBJ ||
7249 (s->sh_flags & SHF_ALLOC) ||7386 (s->sh_flags & SHF_ALLOC) ||
7250 i == (nb_sections - 1)) {7387 i == (nb_sections - 1)) {
7388 /* we output all sections if debug or object file */
7251 s->sh_size = s->data_offset;7389 s->sh_size = s->data_offset;
7252 }7390 }
7253 }7391 }
@@ -7430,7 +7568,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 176 unmodified lines
7430 /* put dynamic section entries */7568 /* put dynamic section entries */
7431 7569
7432 dynamic->data_offset = saved_dynamic_data_offset;7570 dynamic->data_offset = saved_dynamic_data_offset;
7433 put_dt(dynamic, DT_HASH, hash->sh_addr);7571 put_dt(dynamic, DT_HASH, dynsym->hash->sh_addr);
7434 put_dt(dynamic, DT_STRTAB, dynstr->sh_addr);7572 put_dt(dynamic, DT_STRTAB, dynstr->sh_addr);
7435 put_dt(dynamic, DT_SYMTAB, dynsym->sh_addr);7573 put_dt(dynamic, DT_SYMTAB, dynsym->sh_addr);
7436 put_dt(dynamic, DT_STRSZ, dynstr->data_offset);7574 put_dt(dynamic, DT_STRSZ, dynstr->data_offset);
@@ -7470,7 +7608,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 33 unmodified lines
7470 for(i = 1; i < nb_sections; i++) {7608 for(i = 1; i < nb_sections; i++) {
7471 s = sections[i];7609 s = sections[i];
7472 if (s->reloc && s != got)7610 if (s->reloc && s != got)
7473 relocate_section(s);7611 relocate_section(s1, s);
7474 }7612 }
7475 7613
7476 /* relocate relocation entries if the relocation tables are7614 /* relocate relocation entries if the relocation tables are
@@ -7484,7 +7622,10 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines
7484 }7622 }
7485 7623
7486 /* get entry point address */7624 /* get entry point address */
7487 ehdr.e_entry = get_elf_sym_val("_start");7625 if (file_type == TCC_OUTPUT_EXE)
7626 ehdr.e_entry = get_elf_sym_val("_start");
7627 else
7628 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
7488 }7629 }
7489 7630
7490 sort_syms(symtab_section);7631 sort_syms(symtab_section);
@@ -8208,7 +8349,7 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 717 unmodified lines
8208 for(i = 1; i < nb_sections; i++) {8349 for(i = 1; i < nb_sections; i++) {
8209 s = sections[i];8350 s = sections[i];
8210 if (s->reloc)8351 if (s->reloc)
8211 relocate_section(s);8352 relocate_section(s1, s);
8212 }8353 }
8213 8354
8214 prog_main = (void *)get_elf_sym_val("main");8355 prog_main = (void *)get_elf_sym_val("main");
@@ -8288,24 +8429,15 @@ TCCState *tcc_new(void) · 73 unmodified lines
8288 bss_section = new_section(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);8429 bss_section = new_section(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
8289 8430
8290 /* symbols are always generated for linking stage */8431 /* symbols are always generated for linking stage */
8291 symtab_section = new_section(".symtab", SHT_SYMTAB, 0);8432 symtab_section = new_symtab(".symtab", SHT_SYMTAB, 0,
8292 symtab_section->sh_entsize = sizeof(Elf32_Sym);8433 ".strtab",
8293 strtab_section = new_section(".strtab", SHT_STRTAB, 0);8434 ".hashtab", SHF_PRIVATE);
8294 put_elf_str(strtab_section, "");8435 strtab_section = symtab_section->link;
8295 symtab_section->link = strtab_section;
8296 put_elf_sym(symtab_section, 0, 0, 0, 0, 0, NULL);
8297 new_section_hash(".hashtab", SHF_PRIVATE,
8298 ELF_SYM_HASH_SIZE, symtab_section);
8299 8436
8300 /* private symbol table for dynamic symbols */8437 /* private symbol table for dynamic symbols */
8301 dynsymtab_section = new_section(".dynsymtab", SHT_SYMTAB, SHF_PRIVATE);8438 dynsymtab_section = new_symtab(".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
8302 dynsymtab_section->sh_entsize = sizeof(Elf32_Sym);8439 ".dynstrtab",
8303 dynstrtab_section = new_section(".dynstrtab", SHT_STRTAB, SHF_PRIVATE);8440 ".dynhashtab", SHF_PRIVATE);
8304 put_elf_str(dynstrtab_section, "");
8305 dynsymtab_section->link = dynstrtab_section;
8306 put_elf_sym(dynsymtab_section, 0, 0, 0, 0, 0, NULL);
8307 new_section_hash(".dynhashtab", SHF_PRIVATE,
8308 ELF_SYM_HASH_SIZE, dynsymtab_section);
8309 return s;8441 return s;
8310}8442}
8311 8443
@@ -8504,7 +8636,7 @@ int tcc_set_output_type(TCCState *s, int output_type) · 192 unmodified lines
8504 8636
8505void help(void)8637void help(void)
8506{8638{
8507 printf("tcc version 0.9.8 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n" 8639 printf("tcc version 0.9.9 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
8508 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"8640 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
8509 " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n"8641 " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n"
8510 " [--] infile1 [infile2... --] [infile_args...]\n"8642 " [--] infile1 [infile2... --] [infile_args...]\n"
@@ -8528,7 +8660,7 @@ void help(void) · 17 unmodified lines
8528 "Linker options:\n"8660 "Linker options:\n"
8529 " -Ldir add library path 'dir'\n"8661 " -Ldir add library path 'dir'\n"
8530 " -llib link with dynamic library 'lib'\n"8662 " -llib link with dynamic library 'lib'\n"
8531 " -shared generate a shared library (NOT WORKING YET)\n"8663 " -shared generate a shared library\n"
8532 " -static static linking\n"8664 " -static static linking\n"
8533 );8665 );
8534}8666}
@@ -8619,8 +8751,6 @@ int main(int argc, char **argv) · 84 unmodified lines
8619 output_type = TCC_OUTPUT_EXE;8751 output_type = TCC_OUTPUT_EXE;
8620 8752
8621 /* warning if not supported features */8753 /* warning if not supported features */
8622 if (output_type == TCC_OUTPUT_DLL)
8623 warning("dll output is currently not supported");
8624#ifdef CONFIG_TCC_BCHECK8754#ifdef CONFIG_TCC_BCHECK
8625 if (do_bounds_check && output_type != TCC_OUTPUT_MEMORY)8755 if (do_bounds_check && output_type != TCC_OUTPUT_MEMORY)
8626 warning("bounds checking is currently only supported for in-memory execution");8756 warning("bounds checking is currently only supported for in-memory execution");

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