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
7676 #define TOK_HASH_SIZE 2048 /* must be a power of two */
7777 #define TOK_ALLOC_INCR 512 /* must be a power of two */
7878 #define SYM_HASH_SIZE 1031
79−#define ELF_SYM_HASH_SIZE 2048
80−#define ELF_DYNSYM_HASH_SIZE 32
8179
8280 /* token symbol management */
8381 typedef struct TokenSym {
@@ -147,6 +145,7 @@ typedef struct Section { · 63 unmodified lines
147145 unsigned long sh_size; /* section size (only used during output) */
148146 unsigned long sh_addr; /* address at which the section is relocated */
149147 unsigned long sh_offset; /* address at which the section is relocated */
148+ int nb_hashed_syms; /* used to resize the hash table */
150149 struct Section *link; /* link to another section */
151150 struct Section *reloc; /* corresponding section for relocation, if any */
152151 struct Section *hash; /* hash table for symbols */
@@ -238,7 +237,7 @@ Section *lbounds_section; /* contains local data bound description */ · 85 unmodified lines
238237 /* symbol sections */
239238 Section *symtab_section, *strtab_section;
240239 /* temporary dynamic symbol sections (for dll loading) */
241−Section *dynsymtab_section, *dynstrtab_section;
240+Section *dynsymtab_section;
242241 /* exported dynamic symbol section */
243242 Section *dynsym;
244243 /* got handling */
@@ -246,7 +245,8 @@ Section *got; · 1 unmodified line
246245 unsigned long *got_offsets;
247246 int nb_got_offsets;
248247 int nb_plt_entries;
249−
248+/* give the correspondance from symtab indexes to dynsym indexes */
249+int *symtab_to_dynsym;
250250
251251 /* array of all loaded dlls (including those referenced by loaded
252252 dlls) */
@@ -596,8 +596,9 @@ static Sym *get_sym_ref(int t, Section *sec, · 343 unmodified lines
596596 /* section generation */
597597 static void *section_ptr(Section *sec, unsigned long size);
598598 static void *section_ptr_add(Section *sec, unsigned long size);
599−void put_extern_sym(Sym *sym, Section *section, unsigned long value);
600−void greloc(Section *s, Sym *sym, unsigned long addr, int type);
599+static void put_extern_sym(Sym *sym, Section *section,
600+ unsigned long value, unsigned long size);
601+static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
601602 static int put_elf_str(Section *s, const char *sym);
602603 static int put_elf_sym(Section *s,
603604 unsigned long value, unsigned long size,
@@ -822,10 +823,10 @@ Section *find_section(const char *name) · 218 unmodified lines
822823 }
823824
824825 /* add a new relocation entry to symbol 'sym' in section 's' */
825−void greloc(Section *s, Sym *sym, unsigned long offset, int type)
826+static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
826827 {
827828 if (!sym->c)
828− put_extern_sym(sym, NULL, 0);
829+ put_extern_sym(sym, NULL, 0, 0);
829830 /* now we can add ELF relocation info */
830831 put_elf_reloc(symtab_section, s, offset, type, sym->c);
831832 }
@@ -2536,17 +2537,16 @@ static Sym *get_sym_ref(int t, Section *sec, · 1704 unmodified lines
25362537 v = anon_sym++;
25372538 sym = sym_push1(&global_stack, v, t | VT_STATIC, 0);
25382539 sym->r = VT_CONST | VT_SYM;
2539− put_extern_sym(sym, sec, offset);
2540+ put_extern_sym(sym, sec, offset, size);
25402541 return sym;
25412542 }
25422543
25432544 /* push a reference to a section offset by adding a dummy symbol */
2544−/* XXX: add size */
2545−void vpush_ref(int t, Section *sec, unsigned long offset)
2545+void vpush_ref(int t, Section *sec, unsigned long offset, unsigned long size)
25462546 {
25472547 CValue cval;
25482548
2549− cval.sym = get_sym_ref(t, sec, offset, 0);
2549+ cval.sym = get_sym_ref(t, sec, offset, size);
25502550 vsetc(t, VT_CONST | VT_SYM, &cval);
25512551 }
25522552
@@ -2760,30 +2760,24 @@ int gv(int rc) · 207 unmodified lines
27602760 } else {
27612761 if (is_float(vtop->t) &&
27622762 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
2763− int v;
27642763 Sym *sym;
27652764 int *ptr;
2766−
2765+ unsigned long offset;
2766+
27672767 /* XXX: unify with initializers handling ? */
27682768 /* CPUs usually cannot use float constants, so we store them
27692769 generically in data segment */
27702770 size = type_size(vtop->t, &align);
2771− data_section->data_offset = (data_section->data_offset +
2772− align - 1) & -align;
2771+ offset = (data_section->data_offset + align - 1) & -align;
2772+ data_section->data_offset = offset;
27732773 /* XXX: not portable yet */
2774− ptr = section_ptr(data_section, size);
2774+ ptr = section_ptr_add(data_section, size);
27752775 size = size >> 2;
27762776 for(i=0;i<size;i++)
27772777 ptr[i] = vtop->c.tab[i];
2778−
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−
2778+ sym = get_sym_ref(vtop->t, data_section, offset, size << 2);
27842779 vtop->r |= VT_LVAL | VT_SYM;
27852780 vtop->c.sym = sym;
2786− data_section->data_offset += size << 2;
27872781 }
27882782 #ifdef CONFIG_TCC_BCHECK
27892783 if (vtop->r & VT_MUSTBOUND)
@@ -4706,7 +4700,7 @@ void unary(void) · 1916 unmodified lines
47064700 t = VT_ARRAY | mk_pointer(VT_BYTE);
47074701 s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
47084702 s->c = len;
4709− vpush_ref(t, data_section, data_section->data_offset);
4703+ vpush_ref(t, data_section, data_section->data_offset, len);
47104704 ptr = section_ptr_add(data_section, len);
47114705 memcpy(ptr, funcname, len);
47124706 next();
@@ -4724,9 +4718,9 @@ void unary(void) · 11 unmodified lines
47244718 /* we must declare it as an array first to use initializer parser */
47254719 t = VT_ARRAY | mk_pointer(t);
47264720 decl_initializer(t, data_section, data_offset, 1, 0);
4727− data_offset += type_size(t, &align);
4728− /* XXX: is it correct to put it exactly as an array ? */
4729− vpush_ref(t, data_section, fc);
4721+ size = type_size(t, &align);
4722+ data_offset += size;
4723+ vpush_ref(t, data_section, fc, size);
47304724 data_section->data_offset = data_offset;
47314725 } else {
47324726 t = tok;
@@ -5900,12 +5894,12 @@ void decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init, · 1167 unmodified lines
59005894 do_def:
59015895 sym = sym_push(v, t, r | VT_SYM, 0);
59025896 }
5903− put_extern_sym(sym, sec, addr);
5897+ put_extern_sym(sym, sec, addr, size);
59045898 } else {
59055899 CValue cval;
59065900
59075901 /* push global reference */
5908− sym = get_sym_ref(t, sec, addr, 0);
5902+ sym = get_sym_ref(t, sec, addr, size);
59095903 cval.sym = sym;
59105904 vsetc(t, VT_CONST | VT_SYM, &cval);
59115905 }
@@ -5942,7 +5936,6 @@ void put_func_debug(Sym *sym) · 30 unmodified lines
59425936 funcname, sym->t & VT_STATIC ? 'f' : 'F');
59435937 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
59445938 cur_text_section, sym->c);
5945− func_ind = ind;
59465939 last_ind = 0;
59475940 last_line_num = 0;
59485941 }
@@ -6100,7 +6093,9 @@ void decl(int l) · 151 unmodified lines
61006093 /* put function symbol */
61016094 sym = sym_push1(&global_stack, v, t, 0);
61026095 }
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;
61046099 sym->r = VT_SYM | VT_CONST;
61056100 /* put debug symbol */
61066101 if (do_debug)
@@ -6121,6 +6116,9 @@ void decl(int l) · 14 unmodified lines
61216116 sym_pop(&label_stack, NULL); /* reset label stack */
61226117 sym_pop(&local_stack, NULL); /* reset local stack */
61236118 /* end of function */
6119+ /* patch symbol size */
6120+ ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
6121+ ind - func_ind;
61246122 if (do_debug) {
61256123 put_stabn(N_FUN, 0, 0, ind - func_ind);
61266124 }
@@ -6322,6 +6320,40 @@ static unsigned long elf_hash(const unsigned char *name) · 195 unmodified lines
63226320 return h;
63236321 }
63246322
6323+/* rebuild hash table of section s */
6324+/* NOTE: we do factorize the hash table code to go faster */
6325+static 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+
63256357 /* return the symbol number */
63266358 static int put_elf_sym(Section *s,
63276359 unsigned long value, unsigned long size,
@@ -6347,20 +6379,26 @@ static int put_elf_sym(Section *s, · 19 unmodified lines
63476379 sym_index = sym - (Elf32_Sym *)s->data;
63486380 hs = s->hash;
63496381 if (hs) {
6350− int *ptr;
6382+ int *ptr, *base;
63516383 ptr = section_ptr_add(hs, sizeof(int));
6384+ base = (int *)hs->data;
63526385 /* only add global or weak symbols */
63536386 if (ELF32_ST_BIND(info) != STB_LOCAL) {
63546387 /* add another hashing entry */
6355− nbuckets = ((int *)hs->data)[0];
6388+ nbuckets = base[0];
63566389 h = elf_hash(name) % nbuckets;
6357− *ptr = ((int *)hs->data)[2 + h];
6358− ((int *)hs->data)[2 + h] = sym_index;
6390+ *ptr = base[2 + h];
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+ }
63596398 } else {
63606399 *ptr = 0;
6400+ base[1]++;
63616401 }
6362− /* but still add room for all symbols */
6363− ((int *)hs->data)[1]++;
63646402 }
63656403 return sym_index;
63666404 }
@@ -6457,7 +6495,8 @@ static int add_elf_sym(Section *s, unsigned long value, unsigned long size, · 90 unmodified lines
64576495
64586496 /* update sym->c so that it points to an external symbol in section
64596497 'section' with value 'value' */
6460−void put_extern_sym(Sym *sym, Section *section, unsigned long value)
6498+static void put_extern_sym(Sym *sym, Section *section,
6499+ unsigned long value, unsigned long size)
64616500 {
64626501 int sym_type, sym_bind, sh_num, info;
64636502 Elf32_Sym *esym;
@@ -6502,10 +6541,11 @@ void put_extern_sym(Sym *sym, Section *section, unsigned long value) · 38 unmodified lines
65026541 }
65036542 #endif
65046543 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);
65066545 } else {
65076546 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
65086547 esym->st_value = value;
6548+ esym->st_size = size;
65096549 esym->st_shndx = sh_num;
65106550 }
65116551 }
@@ -6728,50 +6768,20 @@ static void relocate_syms(int do_resolve) · 216 unmodified lines
67286768 }
67296769 }
67306770
6731−/* elf relocation, CPU dependant */
6732−static void elf_reloc(unsigned char *ptr,
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 */
6763−static void relocate_section(Section *s)
6771+/* relocate a given section (CPU dependant) */
6772+static void relocate_section(TCCState *s1, Section *s)
67646773 {
67656774 Section *sr;
6766− Elf32_Rel *rel, *rel_end;
6775+ Elf32_Rel *rel, *rel_end, *qrel;
67676776 Elf32_Sym *sym;
6768− int type, sym_index;
6777+ int type, sym_index, esym_index;
67696778 unsigned char *ptr;
6770− unsigned long val;
6779+ unsigned long val, addr;
67716780
67726781 sr = s->reloc;
67736782 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;
67756785 rel < rel_end;
67766786 rel++) {
67776787 ptr = s->data + rel->r_offset;
@@ -6780,8 +6790,60 @@ static void relocate_section(Section *s) · 2 unmodified lines
67806790 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
67816791 val = sym->st_value;
67826792 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+ }
67846843 }
6844+ /* if the relocation is allocated, we change its symbol table */
6845+ if (sr->sh_flags & SHF_ALLOC)
6846+ sr->link = dynsym;
67856847 }
67866848
67876849 /* relocate relocation table in 'sr' */
@@ -6799,6 +6861,39 @@ static void relocate_rel(Section *sr) · 11 unmodified lines
67996861 }
68006862 }
68016863
6864+/* count the number of dynamic relocations so that we can reserve
6865+ their space */
6866+static 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+
68026897 static void put_got_offset(int index, unsigned long val)
68036898 {
68046899 int n;
@@ -6934,18 +7029,32 @@ static void build_got_entries(void) · 129 unmodified lines
69347029 }
69357030 }
69367031
6937−static Section *new_section_hash(const char *name, int sh_flags,
6938− int nb_buckets, Section *symtab)
7032+static Section *new_symtab(const char *symtab_name, int sh_type, int sh_flags,
7033+ const char *strtab_name,
7034+ const char *hash_name, int hash_sh_flags)
69397035 {
6940− Section *hash;
6941− hash = new_section(name, SHT_HASH, sh_flags);
6942− ((int *)hash->data)[0] = nb_buckets;
6943− ((int *)hash->data)[1] = 1;
7036+ Section *symtab, *strtab, *hash;
7037+ int *ptr, nb_buckets;
7038+
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);
69447049 hash->sh_entsize = sizeof(int);
6945− section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int));
69467050 symtab->hash = hash;
69477051 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;
69497058 }
69507059
69517060 /* put dynamic tag */
@@ -7055,7 +7164,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 103 unmodified lines
70557164 Section *strsec, *s;
70567165 Elf32_Shdr shdr, *sh;
70577166 Elf32_Phdr *phdr, *ph;
7058− Section *interp, *plt, *dynamic, *dynstr, *hash;
7167+ Section *interp, *plt, *dynamic, *dynstr;
70597168 unsigned long saved_dynamic_data_offset;
70607169 Elf32_Sym *sym;
70617170 int type, file_type;
@@ -7072,7 +7181,6 @@ int tcc_output_file(TCCState *s1, const char *filename) · 10 unmodified lines
70727181 got = NULL;
70737182 nb_plt_entries = 0;
70747183 plt = NULL; /* avoid warning */
7075− hash = NULL; /* avoid warning */
70767184 dynstr = NULL; /* avoid warning */
70777185 saved_dynamic_data_offset = 0; /* avoid warning */
70787186
@@ -7095,17 +7203,11 @@ int tcc_output_file(TCCState *s1, const char *filename) · 16 unmodified lines
70957203 }
70967204
70977205 /* add dynamic symbol table */
7098− dynsym = new_section(".dynsym", SHT_DYNSYM, SHF_ALLOC);
7099− dynsym->sh_entsize = sizeof(Elf32_Sym);
7100− dynstr = new_section(".dynstr", SHT_STRTAB, SHF_ALLOC);
7101− put_elf_str(dynstr, "");
7102− dynsym->link = dynstr;
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−
7206+ dynsym = new_symtab(".dynsym", SHT_DYNSYM, SHF_ALLOC,
7207+ ".dynstr",
7208+ ".hash", SHF_ALLOC);
7209+ dynstr = dynsym->link;
7210+
71097211 /* add dynamic section */
71107212 dynamic = new_section(".dynamic", SHT_DYNAMIC,
71117213 SHF_ALLOC | SHF_WRITE);
@@ -7116,72 +7218,98 @@ int tcc_output_file(TCCState *s1, const char *filename) · 4 unmodified lines
71167218 plt = new_section(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
71177219 plt->sh_entsize = 4;
71187220
7221+ build_got();
7222+
71197223 /* scan for undefined symbols and see if they are in the
71207224 dynamic symbols. If a symbol STT_FUNC is found, then we
71217225 add it in the PLT. If a symbol STT_OBJECT is found, we
71227226 add it in the .bss section with a suitable relocation */
71237227 sym_end = (Elf32_Sym *)(symtab_section->data +
71247228 symtab_section->data_offset);
7125− for(sym = (Elf32_Sym *)symtab_section->data + 1;
7126− sym < sym_end;
7127− sym++) {
7128− if (sym->st_shndx == SHN_UNDEF) {
7129− name = symtab_section->link->data + sym->st_name;
7130− sym_index = find_elf_sym(dynsymtab_section, name);
7131− if (sym_index) {
7132− esym = &((Elf32_Sym *)dynsymtab_section->data)[sym_index];
7133− type = ELF32_ST_TYPE(esym->st_info);
7134− if (type == STT_FUNC) {
7135− put_got_entry(R_386_JMP_SLOT, esym->st_size,
7136− esym->st_info,
7137− sym - (Elf32_Sym *)symtab_section->data);
7138− } else if (type == STT_OBJECT) {
7139− unsigned long offset;
7140− offset = bss_section->data_offset;
7141− /* XXX: which alignment ? */
7142− offset = (offset + 8 - 1) & -8;
7143− index = put_elf_sym(dynsym, offset, esym->st_size,
7144− esym->st_info, 0,
7145− bss_section->sh_num, name);
7146− put_elf_reloc(dynsym, bss_section,
7147− offset, R_386_COPY, index);
7148− offset += esym->st_size;
7149− bss_section->data_offset = offset;
7150− }
7151− } else {
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")) {
7229+ if (file_type == TCC_OUTPUT_EXE) {
7230+ for(sym = (Elf32_Sym *)symtab_section->data + 1;
7231+ sym < sym_end;
7232+ sym++) {
7233+ if (sym->st_shndx == SHN_UNDEF) {
7234+ name = symtab_section->link->data + sym->st_name;
7235+ sym_index = find_elf_sym(dynsymtab_section, name);
7236+ if (sym_index) {
7237+ esym = &((Elf32_Sym *)dynsymtab_section->data)[sym_index];
7238+ type = ELF32_ST_TYPE(esym->st_info);
7239+ if (type == STT_FUNC) {
7240+ put_got_entry(R_386_JMP_SLOT, esym->st_size,
7241+ esym->st_info,
7242+ sym - (Elf32_Sym *)symtab_section->data);
7243+ } else if (type == STT_OBJECT) {
7244+ unsigned long offset;
7245+ offset = bss_section->data_offset;
7246+ /* XXX: which alignment ? */
7247+ offset = (offset + 8 - 1) & -8;
7248+ index = put_elf_sym(dynsym, offset, esym->st_size,
7249+ esym->st_info, 0,
7250+ bss_section->sh_num, name);
7251+ put_elf_reloc(dynsym, bss_section,
7252+ offset, R_386_COPY, index);
7253+ offset += esym->st_size;
7254+ bss_section->data_offset = offset;
7255+ }
71577256 } 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+ }
71597265 }
71607266 }
71617267 }
7162− }
71637268
7164− /* now look at unresolved dynamic symbols and export
7165− corresponding symbol */
7166− sym_end = (Elf32_Sym *)(dynsymtab_section->data +
7167− dynsymtab_section->data_offset);
7168− for(sym = (Elf32_Sym *)dynsymtab_section->data + 1;
7169− sym < sym_end;
7170− sym++) {
7171− if (sym->st_shndx == SHN_UNDEF) {
7172− name = dynsymtab_section->link->data + sym->st_name;
7173− sym_index = find_elf_sym(symtab_section, name);
7174− if (sym_index) {
7175− } else {
7176− if (ELF32_ST_BIND(sym->st_info) == STB_WEAK) {
7177− /* weak symbols can stay undefined */
7269+ /* now look at unresolved dynamic symbols and export
7270+ corresponding symbol */
7271+ sym_end = (Elf32_Sym *)(dynsymtab_section->data +
7272+ dynsymtab_section->data_offset);
7273+ for(esym = (Elf32_Sym *)dynsymtab_section->data + 1;
7274+ esym < sym_end;
7275+ esym++) {
7276+ if (esym->st_shndx == SHN_UNDEF) {
7277+ name = dynsymtab_section->link->data + esym->st_name;
7278+ sym_index = find_elf_sym(symtab_section, name);
7279+ if (sym_index) {
7280+ sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
7281+ put_elf_sym(dynsym, sym->st_value, sym->st_size,
7282+ sym->st_info, 0,
7283+ sym->st_shndx, name);
71787284 } 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+ }
71807290 }
71817291 }
71827292 }
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+ }
71837311 }
7184−
7312+
71857313 build_got_entries();
71867314
71877315 /* 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
71937321 if (dllref->level == 0)
71947322 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
71957323 }
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);
71967328
71977329 /* add necessary space for other entries */
71987330 saved_dynamic_data_offset = dynamic->data_offset;
@@ -7243,11 +7375,17 @@ int tcc_output_file(TCCState *s1, const char *filename) · 44 unmodified lines
72437375 for(i = 1; i < nb_sections; i++) {
72447376 s = sections[i];
72457377 s->sh_name = put_elf_str(strsec, s->name);
7246− /* we output all sections if debug or object file */
7247− if (do_debug ||
7378+ /* when generating a DLL, we include relocations but we may
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 ||
72487385 file_type == TCC_OUTPUT_OBJ ||
72497386 (s->sh_flags & SHF_ALLOC) ||
72507387 i == (nb_sections - 1)) {
7388+ /* we output all sections if debug or object file */
72517389 s->sh_size = s->data_offset;
72527390 }
72537391 }
@@ -7430,7 +7568,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 176 unmodified lines
74307568 /* put dynamic section entries */
74317569
74327570 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);
74347572 put_dt(dynamic, DT_STRTAB, dynstr->sh_addr);
74357573 put_dt(dynamic, DT_SYMTAB, dynsym->sh_addr);
74367574 put_dt(dynamic, DT_STRSZ, dynstr->data_offset);
@@ -7470,7 +7608,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 33 unmodified lines
74707608 for(i = 1; i < nb_sections; i++) {
74717609 s = sections[i];
74727610 if (s->reloc && s != got)
7473− relocate_section(s);
7611+ relocate_section(s1, s);
74747612 }
74757613
74767614 /* relocate relocation entries if the relocation tables are
@@ -7484,7 +7622,10 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines
74847622 }
74857623
74867624 /* 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 ? */
74887629 }
74897630
74907631 sort_syms(symtab_section);
@@ -8208,7 +8349,7 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 717 unmodified lines
82088349 for(i = 1; i < nb_sections; i++) {
82098350 s = sections[i];
82108351 if (s->reloc)
8211− relocate_section(s);
8352+ relocate_section(s1, s);
82128353 }
82138354
82148355 prog_main = (void *)get_elf_sym_val("main");
@@ -8288,24 +8429,15 @@ TCCState *tcc_new(void) · 73 unmodified lines
82888429 bss_section = new_section(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
82898430
82908431 /* symbols are always generated for linking stage */
8291− symtab_section = new_section(".symtab", SHT_SYMTAB, 0);
8292− symtab_section->sh_entsize = sizeof(Elf32_Sym);
8293− strtab_section = new_section(".strtab", SHT_STRTAB, 0);
8294− put_elf_str(strtab_section, "");
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);
8432+ symtab_section = new_symtab(".symtab", SHT_SYMTAB, 0,
8433+ ".strtab",
8434+ ".hashtab", SHF_PRIVATE);
8435+ strtab_section = symtab_section->link;
82998436
83008437 /* private symbol table for dynamic symbols */
8301− dynsymtab_section = new_section(".dynsymtab", SHT_SYMTAB, SHF_PRIVATE);
8302− dynsymtab_section->sh_entsize = sizeof(Elf32_Sym);
8303− dynstrtab_section = new_section(".dynstrtab", SHT_STRTAB, 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);
8438+ dynsymtab_section = new_symtab(".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
8439+ ".dynstrtab",
8440+ ".dynhashtab", SHF_PRIVATE);
83098441 return s;
83108442 }
83118443
@@ -8504,7 +8636,7 @@ int tcc_set_output_type(TCCState *s, int output_type) · 192 unmodified lines
85048636
85058637 void help(void)
85068638 {
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"
85088640 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
85098641 " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n"
85108642 " [--] infile1 [infile2... --] [infile_args...]\n"
@@ -8528,7 +8660,7 @@ void help(void) · 17 unmodified lines
85288660 "Linker options:\n"
85298661 " -Ldir add library path 'dir'\n"
85308662 " -llib link with dynamic library 'lib'\n"
8531− " -shared generate a shared library (NOT WORKING YET)\n"
8663+ " -shared generate a shared library\n"
85328664 " -static static linking\n"
85338665 );
85348666 }
@@ -8619,8 +8751,6 @@ int main(int argc, char **argv) · 84 unmodified lines
86198751 output_type = TCC_OUTPUT_EXE;
86208752
86218753 /* warning if not supported features */
8622− if (output_type == TCC_OUTPUT_DLL)
8623− warning("dll output is currently not supported");
86248754 #ifdef CONFIG_TCC_BCHECK
86258755 if (do_bounds_check && output_type != TCC_OUTPUT_MEMORY)
86268756 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