summaryrefslogtreecommitdiff
path: root/tccelf.c
diff options
context:
space:
mode:
Diffstat (limited to 'tccelf.c')
-rw-r--r--tccelf.c1347
1 files changed, 675 insertions, 672 deletions
diff --git a/tccelf.c b/tccelf.c
index 16337da..ddbce88 100644
--- a/tccelf.c
+++ b/tccelf.c
@@ -34,13 +34,13 @@ struct sym_version {
int prev_same_lib;
};
-#define nb_sym_versions S->nb_sym_versions
-#define sym_versions S->sym_versions
-#define nb_sym_to_version S->nb_sym_to_version
-#define sym_to_version S->sym_to_version
-#define dt_verneednum S->dt_verneednum
-#define versym_section S->versym_section
-#define verneed_section S->verneed_section
+#define nb_sym_versions s1->nb_sym_versions
+#define sym_versions s1->sym_versions
+#define nb_sym_to_version s1->nb_sym_to_version
+#define sym_to_version s1->sym_to_version
+#define dt_verneednum s1->dt_verneednum
+#define versym_section s1->versym_section
+#define verneed_section s1->verneed_section
/* special flag to indicate that the section should not be linked to the other ones */
#define SHF_PRIVATE 0x80000000
@@ -49,96 +49,99 @@ struct sym_version {
/* ------------------------------------------------------------------------- */
-ST_FUNC void tccelf_new(TCCState *S)
+ST_FUNC void tccelf_new(TCCState *s)
{
+ TCCState *s1 = s;
/* no section zero */
- dynarray_add(S, &S->sections, &S->nb_sections, NULL);
+ dynarray_add(&s->sections, &s->nb_sections, NULL);
/* create standard sections */
- text_section = new_section(S, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
- data_section = new_section(S, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
+ text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
+ data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
#ifdef TCC_TARGET_PE
- rodata_section = new_section(S, ".rdata", SHT_PROGBITS, SHF_ALLOC);
+ rodata_section = new_section(s, ".rdata", SHT_PROGBITS, SHF_ALLOC);
#else
/* create ro data section (make ro after relocation done with GNU_RELRO) */
- rodata_section = new_section(S, ".data.ro", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
+ rodata_section = new_section(s, ".data.ro", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
#endif
- bss_section = new_section(S, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
- common_section = new_section(S, ".common", SHT_NOBITS, SHF_PRIVATE);
+ bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
+ common_section = new_section(s, ".common", SHT_NOBITS, SHF_PRIVATE);
common_section->sh_num = SHN_COMMON;
/* symbols are always generated for linking stage */
- symtab_section = new_symtab(S, ".symtab", SHT_SYMTAB, 0,
+ symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
".strtab",
".hashtab", SHF_PRIVATE);
- S->symtab = symtab_section;
+ s->symtab = symtab_section;
/* private symbol table for dynamic symbols */
- S->dynsymtab_section = new_symtab(S, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE|SHF_DYNSYM,
+ s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE|SHF_DYNSYM,
".dynstrtab",
".dynhashtab", SHF_PRIVATE);
- get_sym_attr(S, 0, 1);
+ get_sym_attr(s, 0, 1);
}
#ifdef CONFIG_TCC_BCHECK
-ST_FUNC void tccelf_bounds_new(TCCState *S)
+ST_FUNC void tccelf_bounds_new(TCCState *s)
{
+ TCCState *s1 = s;
/* create bounds sections (make ro after relocation done with GNU_RELRO) */
- bounds_section = new_section(S, ".bounds",
+ bounds_section = new_section(s, ".bounds",
SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
- lbounds_section = new_section(S, ".lbounds",
+ lbounds_section = new_section(s, ".lbounds",
SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
}
#endif
-ST_FUNC void tccelf_stab_new(TCCState *S)
+ST_FUNC void tccelf_stab_new(TCCState *s)
{
+ TCCState *s1 = s;
int shf = 0;
#ifdef CONFIG_TCC_BACKTRACE
/* include stab info with standalone backtrace support */
- if (S->do_backtrace && S->output_type != TCC_OUTPUT_MEMORY)
+ if (s->do_backtrace && s->output_type != TCC_OUTPUT_MEMORY)
shf = SHF_ALLOC | SHF_WRITE; // SHF_WRITE needed for musl/SELINUX
#endif
- stab_section = new_section(S, ".stab", SHT_PROGBITS, shf);
+ stab_section = new_section(s, ".stab", SHT_PROGBITS, shf);
stab_section->sh_entsize = sizeof(Stab_Sym);
stab_section->sh_addralign = sizeof ((Stab_Sym*)0)->n_value;
- stab_section->link = new_section(S, ".stabstr", SHT_STRTAB, shf);
+ stab_section->link = new_section(s, ".stabstr", SHT_STRTAB, shf);
/* put first entry */
- put_stabs(S, "", 0, 0, 0, 0);
+ put_stabs(s, "", 0, 0, 0, 0);
}
-static void free_section(TCCState* S, Section *s)
+static void free_section(Section *s)
{
- tcc_free(S, s->data);
+ tcc_free(s->data);
}
-ST_FUNC void tccelf_delete(TCCState *S)
+ST_FUNC void tccelf_delete(TCCState *s1)
{
int i;
#ifndef ELF_OBJ_ONLY
/* free symbol versions */
for (i = 0; i < nb_sym_versions; i++) {
- tcc_free(S, sym_versions[i].version);
- tcc_free(S, sym_versions[i].lib);
+ tcc_free(sym_versions[i].version);
+ tcc_free(sym_versions[i].lib);
}
- tcc_free(S, sym_versions);
- tcc_free(S, sym_to_version);
+ tcc_free(sym_versions);
+ tcc_free(sym_to_version);
#endif
/* free all sections */
- for(i = 1; i < S->nb_sections; i++)
- free_section(S, S->sections[i]);
- dynarray_reset(S, &S->sections, &S->nb_sections);
+ for(i = 1; i < s1->nb_sections; i++)
+ free_section(s1->sections[i]);
+ dynarray_reset(&s1->sections, &s1->nb_sections);
- for(i = 0; i < S->nb_priv_sections; i++)
- free_section(S, S->priv_sections[i]);
- dynarray_reset(S, &S->priv_sections, &S->nb_priv_sections);
+ for(i = 0; i < s1->nb_priv_sections; i++)
+ free_section(s1->priv_sections[i]);
+ dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections);
/* free any loaded DLLs */
#ifdef TCC_IS_NATIVE
- for ( i = 0; i < S->nb_loaded_dlls; i++) {
- DLLReference *ref = S->loaded_dlls[i];
+ for ( i = 0; i < s1->nb_loaded_dlls; i++) {
+ DLLReference *ref = s1->loaded_dlls[i];
if ( ref->handle )
# ifdef _WIN32
FreeLibrary((HMODULE)ref->handle);
@@ -148,32 +151,32 @@ ST_FUNC void tccelf_delete(TCCState *S)
}
#endif
/* free loaded dlls array */
- dynarray_reset(S, &S->loaded_dlls, &S->nb_loaded_dlls);
- tcc_free(S, S->sym_attrs);
+ dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
+ tcc_free(s1->sym_attrs);
symtab_section = NULL; /* for tccrun.c:rt_printline() */
}
/* save section data state */
-ST_FUNC void tccelf_begin_file(TCCState *S)
+ST_FUNC void tccelf_begin_file(TCCState *s1)
{
Section *s; int i;
- for (i = 1; i < S->nb_sections; i++) {
- s = S->sections[i];
+ for (i = 1; i < s1->nb_sections; i++) {
+ s = s1->sections[i];
s->sh_offset = s->data_offset;
}
/* disable symbol hashing during compilation */
- s = S->symtab, s->reloc = s->hash, s->hash = NULL;
+ s = s1->symtab, s->reloc = s->hash, s->hash = NULL;
#if defined TCC_TARGET_X86_64 && defined TCC_TARGET_PE
- S->uw_sym = 0;
+ s1->uw_sym = 0;
#endif
}
/* At the end of compilation, convert any UNDEF syms to global, and merge
with previously existing symbols */
-ST_FUNC void tccelf_end_file(TCCState *S)
+ST_FUNC void tccelf_end_file(TCCState *s1)
{
- Section *s = S->symtab;
+ Section *s = s1->symtab;
int first_sym, nb_syms, *tr, i;
first_sym = s->sh_offset / sizeof (ElfSym);
@@ -181,7 +184,7 @@ ST_FUNC void tccelf_end_file(TCCState *S)
s->data_offset = s->sh_offset;
s->link->data_offset = s->link->sh_offset;
s->hash = s->reloc, s->reloc = NULL;
- tr = tcc_mallocz(S, nb_syms * sizeof *tr);
+ tr = tcc_mallocz(nb_syms * sizeof *tr);
for (i = 0; i < nb_syms; ++i) {
ElfSym *sym = (ElfSym*)s->data + first_sym + i;
@@ -192,33 +195,33 @@ ST_FUNC void tccelf_end_file(TCCState *S)
sym->st_other, sym->st_shndx, (char*)s->link->data + sym->st_name);
}
/* now update relocations */
- for (i = 1; i < S->nb_sections; i++) {
- Section *sr = S->sections[i];
+ for (i = 1; i < s1->nb_sections; i++) {
+ Section *sr = s1->sections[i];
if (sr->sh_type == SHT_RELX && sr->link == s) {
ElfW_Rel *rel = (ElfW_Rel*)(sr->data + sr->sh_offset);
ElfW_Rel *rel_end = (ElfW_Rel*)(sr->data + sr->data_offset);
for (; rel < rel_end; ++rel) {
int n = ELFW(R_SYM)(rel->r_info) - first_sym;
- //if (n < 0) tcc_error(S, "internal: invalid symbol index in relocation");
+ //if (n < 0) tcc_error("internal: invalid symbol index in relocation");
rel->r_info = ELFW(R_INFO)(tr[n], ELFW(R_TYPE)(rel->r_info));
}
}
}
- tcc_free(S, tr);
+ tcc_free(tr);
/* record text/data/bss output for -bench info */
for (i = 0; i < 4; ++i) {
- s = S->sections[i + 1];
- S->total_output[i] += s->data_offset - s->sh_offset;
+ s = s1->sections[i + 1];
+ s1->total_output[i] += s->data_offset - s->sh_offset;
}
}
-ST_FUNC Section *new_section(TCCState *S, const char *name, int sh_type, int sh_flags)
+ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
{
Section *sec;
- sec = tcc_mallocz(S, sizeof(Section) + strlen(name));
- sec->S = S;
+ sec = tcc_mallocz(sizeof(Section) + strlen(name));
+ sec->s1 = s1;
strcpy(sec->name, name);
sec->sh_type = sh_type;
sec->sh_flags = sh_flags;
@@ -245,16 +248,16 @@ ST_FUNC Section *new_section(TCCState *S, const char *name, int sh_type, int sh_
}
if (sh_flags & SHF_PRIVATE) {
- dynarray_add(S, &S->priv_sections, &S->nb_priv_sections, sec);
+ dynarray_add(&s1->priv_sections, &s1->nb_priv_sections, sec);
} else {
- sec->sh_num = S->nb_sections;
- dynarray_add(S, &S->sections, &S->nb_sections, sec);
+ sec->sh_num = s1->nb_sections;
+ dynarray_add(&s1->sections, &s1->nb_sections, sec);
}
return sec;
}
-ST_FUNC Section *new_symtab(TCCState *S,
+ST_FUNC Section *new_symtab(TCCState *s1,
const char *symtab_name, int sh_type, int sh_flags,
const char *strtab_name,
const char *hash_name, int hash_sh_flags)
@@ -262,21 +265,21 @@ ST_FUNC Section *new_symtab(TCCState *S,
Section *symtab, *strtab, *hash;
int *ptr, nb_buckets;
- symtab = new_section(S, symtab_name, sh_type, sh_flags);
+ symtab = new_section(s1, symtab_name, sh_type, sh_flags);
symtab->sh_entsize = sizeof(ElfW(Sym));
- strtab = new_section(S, strtab_name, SHT_STRTAB, sh_flags);
- put_elf_str(S, strtab, "");
+ strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags);
+ put_elf_str(strtab, "");
symtab->link = strtab;
- put_elf_sym(S, symtab, 0, 0, 0, 0, 0, NULL);
+ put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);
nb_buckets = 1;
- hash = new_section(S, hash_name, SHT_HASH, hash_sh_flags);
+ hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
hash->sh_entsize = sizeof(int);
symtab->hash = hash;
hash->link = symtab;
- ptr = section_ptr_add(S, hash, (2 + nb_buckets + 1) * sizeof(int));
+ ptr = section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int));
ptr[0] = nb_buckets;
ptr[1] = 1;
memset(ptr + 2, 0, (nb_buckets + 1) * sizeof(int));
@@ -284,7 +287,7 @@ ST_FUNC Section *new_symtab(TCCState *S,
}
/* realloc section and set its content to zero */
-ST_FUNC void section_realloc(TCCState* S, Section *sec, unsigned long new_size)
+ST_FUNC void section_realloc(Section *sec, unsigned long new_size)
{
unsigned long size;
unsigned char *data;
@@ -294,7 +297,7 @@ ST_FUNC void section_realloc(TCCState* S, Section *sec, unsigned long new_size)
size = 1;
while (size < new_size)
size = size * 2;
- data = tcc_realloc(S, sec->data, size);
+ data = tcc_realloc(sec->data, size);
memset(data + sec->data_allocated, 0, size - sec->data_allocated);
sec->data = data;
sec->data_allocated = size;
@@ -302,14 +305,14 @@ ST_FUNC void section_realloc(TCCState* S, Section *sec, unsigned long new_size)
/* reserve at least 'size' bytes aligned per 'align' in section
'sec' from current offset, and return the aligned offset */
-ST_FUNC size_t section_add(TCCState* S, Section *sec, addr_t size, int align)
+ST_FUNC size_t section_add(Section *sec, addr_t size, int align)
{
size_t offset, offset1;
offset = (sec->data_offset + align - 1) & -align;
offset1 = offset + size;
if (sec->sh_type != SHT_NOBITS && offset1 > sec->data_allocated)
- section_realloc(S, sec, offset1);
+ section_realloc(sec, offset1);
sec->data_offset = offset1;
if (align > sec->sh_addralign)
sec->sh_addralign = align;
@@ -318,53 +321,53 @@ ST_FUNC size_t section_add(TCCState* S, Section *sec, addr_t size, int align)
/* reserve at least 'size' bytes in section 'sec' from
sec->data_offset. */
-ST_FUNC void *section_ptr_add(TCCState* S, Section *sec, addr_t size)
+ST_FUNC void *section_ptr_add(Section *sec, addr_t size)
{
- size_t offset = section_add(S, sec, size, 1);
+ size_t offset = section_add(sec, size, 1);
return sec->data + offset;
}
#ifndef ELF_OBJ_ONLY
/* reserve at least 'size' bytes from section start */
-static void section_reserve(TCCState* S, Section *sec, unsigned long size)
+static void section_reserve(Section *sec, unsigned long size)
{
if (size > sec->data_allocated)
- section_realloc(S, sec, size);
+ section_realloc(sec, size);
if (size > sec->data_offset)
sec->data_offset = size;
}
#endif
-static Section *find_section_create (TCCState *S, const char *name, int create)
+static Section *find_section_create (TCCState *s1, const char *name, int create)
{
Section *sec;
int i;
- for(i = 1; i < S->nb_sections; i++) {
- sec = S->sections[i];
+ for(i = 1; i < s1->nb_sections; i++) {
+ sec = s1->sections[i];
if (!strcmp(name, sec->name))
return sec;
}
/* sections are created as PROGBITS */
- return create ? new_section(S, name, SHT_PROGBITS, SHF_ALLOC) : NULL;
+ return create ? new_section(s1, name, SHT_PROGBITS, SHF_ALLOC) : NULL;
}
/* return a reference to a section, and create it if it does not
exists */
-ST_FUNC Section *find_section(TCCState *S, const char *name)
+ST_FUNC Section *find_section(TCCState *s1, const char *name)
{
- return find_section_create (S, name, 1);
+ return find_section_create (s1, name, 1);
}
/* ------------------------------------------------------------------------- */
-ST_FUNC int put_elf_str(TCCState* S, Section *s, const char *sym)
+ST_FUNC int put_elf_str(Section *s, const char *sym)
{
int offset, len;
char *ptr;
len = strlen(sym) + 1;
offset = s->data_offset;
- ptr = section_ptr_add(S, s, len);
+ ptr = section_ptr_add(s, len);
memmove(ptr, sym, len);
return offset;
}
@@ -386,7 +389,7 @@ static unsigned long elf_hash(const unsigned char *name)
/* rebuild hash table of section s */
/* NOTE: we do factorize the hash table code to go faster */
-static void rebuild_hash(TCCState* S, Section *s, unsigned int nb_buckets)
+static void rebuild_hash(Section *s, unsigned int nb_buckets)
{
ElfW(Sym) *sym;
int *ptr, *hash, nb_syms, sym_index, h;
@@ -399,7 +402,7 @@ static void rebuild_hash(TCCState* S, Section *s, unsigned int nb_buckets)
nb_buckets = ((int*)s->hash->data)[0];
s->hash->data_offset = 0;
- ptr = section_ptr_add(S, s->hash, (2 + nb_buckets + nb_syms) * sizeof(int));
+ ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int));
ptr[0] = nb_buckets;
ptr[1] = nb_syms;
ptr += 2;
@@ -422,7 +425,7 @@ static void rebuild_hash(TCCState* S, Section *s, unsigned int nb_buckets)
}
/* return the symbol number */
-ST_FUNC int put_elf_sym(TCCState* S, Section *s, addr_t value, unsigned long size,
+ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size,
int info, int other, int shndx, const char *name)
{
int name_offset, sym_index;
@@ -430,9 +433,9 @@ ST_FUNC int put_elf_sym(TCCState* S, Section *s, addr_t value, unsigned long siz
ElfW(Sym) *sym;
Section *hs;
- sym = section_ptr_add(S, s, sizeof(ElfW(Sym)));
+ sym = section_ptr_add(s, sizeof(ElfW(Sym)));
if (name && name[0])
- name_offset = put_elf_str(S, s->link, name);
+ name_offset = put_elf_str(s->link, name);
else
name_offset = 0;
/* XXX: endianness */
@@ -446,7 +449,7 @@ ST_FUNC int put_elf_sym(TCCState* S, Section *s, addr_t value, unsigned long siz
hs = s->hash;
if (hs) {
int *ptr, *base;
- ptr = section_ptr_add(S, hs, sizeof(int));
+ ptr = section_ptr_add(hs, sizeof(int));
base = (int *)hs->data;
/* only add global or weak symbols. */
if (ELFW(ST_BIND)(info) != STB_LOCAL) {
@@ -459,7 +462,7 @@ ST_FUNC int put_elf_sym(TCCState* S, Section *s, addr_t value, unsigned long siz
/* we resize the hash table */
hs->nb_hashed_syms++;
if (hs->nb_hashed_syms > 2 * nbuckets) {
- rebuild_hash(S, s, 2 * nbuckets);
+ rebuild_hash(s, 2 * nbuckets);
}
} else {
*ptr = 0;
@@ -494,12 +497,12 @@ ST_FUNC int find_elf_sym(Section *s, const char *name)
/* return elf symbol value, signal error if 'err' is nonzero, decorate
name if FORC */
-ST_FUNC addr_t get_sym_addr(TCCState *S, const char *name, int err, int forc)
+ST_FUNC addr_t get_sym_addr(TCCState *s1, const char *name, int err, int forc)
{
int sym_index;
ElfW(Sym) *sym;
char buf[256];
- if (forc && S->leading_underscore
+ if (forc && s1->leading_underscore
#ifdef TCC_TARGET_PE
/* win32-32bit stdcall symbols always have _ already */
&& !strchr(name, '@')
@@ -509,25 +512,25 @@ ST_FUNC addr_t get_sym_addr(TCCState *S, const char *name, int err, int forc)
pstrcpy(buf + 1, sizeof(buf) - 1, name);
name = buf;
}
- sym_index = find_elf_sym(S->symtab, name);
- sym = &((ElfW(Sym) *)S->symtab->data)[sym_index];
+ sym_index = find_elf_sym(s1->symtab, name);
+ sym = &((ElfW(Sym) *)s1->symtab->data)[sym_index];
if (!sym_index || sym->st_shndx == SHN_UNDEF) {
if (err)
- tcc_error(S, "%s not defined", name);
+ tcc_error("%s not defined", name);
return (addr_t)-1;
}
return sym->st_value;
}
/* return elf symbol value */
-LIBTCCAPI void *tcc_get_symbol(TCCState *S, const char *name)
+LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name)
{
- addr_t addr = get_sym_addr(S, name, 0, 1);
+ addr_t addr = get_sym_addr(s, name, 0, 1);
return addr == -1 ? NULL : (void*)(uintptr_t)addr;
}
/* list elf symbol names and values */
-ST_FUNC void list_elf_symbols(TCCState *S, void *ctx,
+ST_FUNC void list_elf_symbols(TCCState *s, void *ctx,
void (*symbol_cb)(void *ctx, const char *name, const void *val))
{
ElfW(Sym) *sym;
@@ -536,7 +539,7 @@ ST_FUNC void list_elf_symbols(TCCState *S, void *ctx,
const char *name;
unsigned char sym_vis, sym_bind;
- symtab = S->symtab;
+ symtab = s->symtab;
end_sym = symtab->data_offset / sizeof (ElfSym);
for (sym_index = 0; sym_index < end_sym; ++sym_index) {
sym = &((ElfW(Sym) *)symtab->data)[sym_index];
@@ -551,15 +554,15 @@ ST_FUNC void list_elf_symbols(TCCState *S, void *ctx,
}
/* list elf symbol names and values */
-LIBTCCAPI void tcc_list_symbols(TCCState *S, void *ctx,
+LIBTCCAPI void tcc_list_symbols(TCCState *s, void *ctx,
void (*symbol_cb)(void *ctx, const char *name, const void *val))
{
- list_elf_symbols(S, ctx, symbol_cb);
+ list_elf_symbols(s, ctx, symbol_cb);
}
#ifndef ELF_OBJ_ONLY
static void
-version_add (TCCState *S)
+version_add (TCCState *s1)
{
int i;
ElfW(Sym) *sym;
@@ -571,19 +574,19 @@ version_add (TCCState *S)
if (0 == nb_sym_versions)
return;
- versym_section = new_section(S, ".gnu.version", SHT_GNU_versym, SHF_ALLOC);
+ versym_section = new_section(s1, ".gnu.version", SHT_GNU_versym, SHF_ALLOC);
versym_section->sh_entsize = sizeof(ElfW(Half));
- versym_section->link = S->dynsym;
+ versym_section->link = s1->dynsym;
/* add needed symbols */
- symtab = S->dynsym;
+ symtab = s1->dynsym;
end_sym = symtab->data_offset / sizeof (ElfSym);
- versym = section_ptr_add(S, versym_section, end_sym * sizeof(ElfW(Half)));
+ versym = section_ptr_add(versym_section, end_sym * sizeof(ElfW(Half)));
for (sym_index = 0; sym_index < end_sym; ++sym_index) {
int dllindex, verndx;
sym = &((ElfW(Sym) *)symtab->data)[sym_index];
name = (char *) symtab->link->data + sym->st_name;
- dllindex = find_elf_sym(S->dynsymtab_section, name);
+ dllindex = find_elf_sym(s1->dynsymtab_section, name);
verndx = (dllindex && dllindex < nb_sym_to_version)
? sym_to_version[dllindex] : -1;
if (verndx >= 0) {
@@ -597,9 +600,9 @@ version_add (TCCState *S)
dynamic linkers look at their contents even when DTVERNEEDNUM and
section size is zero. */
if (nb_versions > 2) {
- verneed_section = new_section(S, ".gnu.version_r",
+ verneed_section = new_section(s1, ".gnu.version_r",
SHT_GNU_verneed, SHF_ALLOC);
- verneed_section->link = S->dynsym->link;
+ verneed_section->link = s1->dynsym->link;
for (i = nb_sym_versions; i-- > 0;) {
struct sym_version *sv = &sym_versions[i];
int n_same_libs = 0, prev;
@@ -607,20 +610,20 @@ version_add (TCCState *S)
ElfW(Vernaux) *vna = 0;
if (sv->out_index < 1)
continue;
- vnofs = section_add(S, verneed_section, sizeof(*vn), 1);
+ vnofs = section_add(verneed_section, sizeof(*vn), 1);
vn = (ElfW(Verneed)*)(verneed_section->data + vnofs);
vn->vn_version = 1;
- vn->vn_file = put_elf_str(S, verneed_section->link, sv->lib);
+ vn->vn_file = put_elf_str(verneed_section->link, sv->lib);
vn->vn_aux = sizeof (*vn);
do {
prev = sv->prev_same_lib;
if (sv->out_index > 0) {
- vna = section_ptr_add(S, verneed_section, sizeof(*vna));
+ vna = section_ptr_add(verneed_section, sizeof(*vna));
vna->vna_hash = elf_hash ((const unsigned char *)sv->version);
vna->vna_flags = 0;
vna->vna_other = sv->out_index;
sv->out_index = -2;
- vna->vna_name = put_elf_str(S, verneed_section->link, sv->version);
+ vna->vna_name = put_elf_str(verneed_section->link, sv->version);
vna->vna_next = sizeof (*vna);
n_same_libs++;
}
@@ -646,7 +649,7 @@ version_add (TCCState *S)
ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
int info, int other, int shndx, const char *name)
{
- TCCState *S = s->S;
+ TCCState *s1 = s->s1;
ElfW(Sym) *esym;
int sym_bind, sym_index, sym_type, esym_bind;
unsigned char sym_vis, esym_vis, new_vis;
@@ -710,20 +713,20 @@ ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
sym_bind, shndx, new_vis, esym_bind, esym->st_shndx, esym_vis);
#endif
- tcc_error_noabort(S, "'%s' defined twice", name);
+ tcc_error_noabort("'%s' defined twice", name);
}
} else {
do_patch:
esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
esym->st_shndx = shndx;
- S->new_undef_sym = 1;
+ s1->new_undef_sym = 1;
esym->st_value = value;
esym->st_size = size;
esym->st_other = other;
}
} else {
do_def:
- sym_index = put_elf_sym(S, s, value, size,
+ sym_index = put_elf_sym(s, value, size,
ELFW(ST_INFO)(sym_bind, sym_type), other,
shndx, name);
}
@@ -734,7 +737,7 @@ ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset,
int type, int symbol, addr_t addend)
{
- TCCState *S = s->S;
+ TCCState *s1 = s->s1;
char buf[256];
Section *sr;
ElfW_Rel *rel;
@@ -745,20 +748,20 @@ ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset,
snprintf(buf, sizeof(buf), REL_SECTION_FMT, s->name);
/* if the symtab is allocated, then we consider the relocation
are also */
- sr = new_section(s->S, buf, SHT_RELX, symtab->sh_flags);
+ sr = new_section(s->s1, buf, SHT_RELX, symtab->sh_flags);
sr->sh_entsize = sizeof(ElfW_Rel);
sr->link = symtab;
sr->sh_info = s->sh_num;
s->reloc = sr;
}
- rel = section_ptr_add(S, sr, sizeof(ElfW_Rel));
+ rel = section_ptr_add(sr, sizeof(ElfW_Rel));
rel->r_offset = offset;
rel->r_info = ELFW(R_INFO)(symbol, type);
#if SHT_RELX == SHT_RELA
rel->r_addend = addend;
#endif
if (SHT_RELX != SHT_RELA && addend)
- tcc_error(S, "non-zero addend on REL architecture");
+ tcc_error("non-zero addend on REL architecture");
}
ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
@@ -768,7 +771,7 @@ ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
}
/* put stab debug information */
-ST_FUNC void put_stabs(TCCState *S, const char *str, int type, int other, int desc,
+ST_FUNC void put_stabs(TCCState *s1, const char *str, int type, int other, int desc,
unsigned long value)
{
Stab_Sym *sym;
@@ -784,9 +787,9 @@ ST_FUNC void put_stabs(TCCState *S, const char *str, int type, int other, int de
return;
}
- sym = section_ptr_add(S, stab_section, sizeof(Stab_Sym));
+ sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
if (str) {
- sym->n_strx = put_elf_str(S, stab_section->link, str);
+ sym->n_strx = put_elf_str(stab_section->link, str);
} else {
sym->n_strx = 0;
}
@@ -796,47 +799,47 @@ ST_FUNC void put_stabs(TCCState *S, const char *str, int type, int other, int de
sym->n_value = value;
}
-ST_FUNC void put_stabs_r(TCCState *S, const char *str, int type, int other, int desc,
+ST_FUNC void put_stabs_r(TCCState *s1, const char *str, int type, int other, int desc,
unsigned long value, Section *sec, int sym_index)
{
put_elf_reloc(symtab_section, stab_section,
stab_section->data_offset + 8,
sizeof ((Stab_Sym*)0)->n_value == PTR_SIZE ? R_DATA_PTR : R_DATA_32,
sym_index);
- put_stabs(S, str, type, other, desc, value);
+ put_stabs(s1, str, type, other, desc, value);
}
-ST_FUNC void put_stabn(TCCState *S, int type, int other, int desc, int value)
+ST_FUNC void put_stabn(TCCState *s1, int type, int other, int desc, int value)
{
- put_stabs(S, NULL, type, other, desc, value);
+ put_stabs(s1, NULL, type, other, desc, value);
}
-ST_FUNC struct sym_attr *get_sym_attr(TCCState *S, int index, int alloc)
+ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc)
{
int n;
struct sym_attr *tab;
- if (index >= S->nb_sym_attrs) {
+ if (index >= s1->nb_sym_attrs) {
if (!alloc)
- return S->sym_attrs;
+ return s1->sym_attrs;
/* find immediately bigger power of 2 and reallocate array */
n = 1;
while (index >= n)
n *= 2;
- tab = tcc_realloc(S, S->sym_attrs, n * sizeof(*S->sym_attrs));
- S->sym_attrs = tab;
- memset(S->sym_attrs + S->nb_sym_attrs, 0,
- (n - S->nb_sym_attrs) * sizeof(*S->sym_attrs));
- S->nb_sym_attrs = n;
+ tab = tcc_realloc(s1->sym_attrs, n * sizeof(*s1->sym_attrs));
+ s1->sym_attrs = tab;
+ memset(s1->sym_attrs + s1->nb_sym_attrs, 0,
+ (n - s1->nb_sym_attrs) * sizeof(*s1->sym_attrs));
+ s1->nb_sym_attrs = n;
}
- return &S->sym_attrs[index];
+ return &s1->sym_attrs[index];
}
/* In an ELF file symbol table, the local symbols must appear below
the global and weak ones. Since TCC cannot sort it while generating
the code, we must do it after. All the relocation tables are also
modified to take into account the symbol table sorting */
-static void sort_syms(TCCState *S, Section *s)
+static void sort_syms(TCCState *s1, Section *s)
{
int *old_to_new_syms;
ElfW(Sym) *new_syms;
@@ -847,8 +850,8 @@ static void sort_syms(TCCState *S, Section *s)
int type, sym_index;
nb_syms = s->data_offset / sizeof(ElfW(Sym));
- new_syms = tcc_malloc(S, nb_syms * sizeof(ElfW(Sym)));
- old_to_new_syms = tcc_malloc(S, nb_syms * sizeof(int));
+ new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym)));
+ old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
/* first pass for local symbols */
p = (ElfW(Sym) *)s->data;
@@ -876,11 +879,11 @@ static void sort_syms(TCCState *S, Section *s)
/* we copy the new symbols to the old */
memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));
- tcc_free(S, new_syms);
+ tcc_free(new_syms);
/* now we modify all the relocations */
- for(i = 1; i < S->nb_sections; i++) {
- sr = S->sections[i];
+ for(i = 1; i < s1->nb_sections; i++) {
+ sr = s1->sections[i];
if (sr->sh_type == SHT_RELX && sr->link == s) {
for_each_elem(sr, 0, rel, ElfW_Rel) {
sym_index = ELFW(R_SYM)(rel->r_info);
@@ -891,12 +894,12 @@ static void sort_syms(TCCState *S, Section *s)
}
}
- tcc_free(S, old_to_new_syms);
+ tcc_free(old_to_new_syms);
}
/* relocate symbol table, resolve undefined symbols if do_resolve is
true and output error if undefined symbol. */
-ST_FUNC void relocate_syms(TCCState *S, Section *symtab, int do_resolve)
+ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve)
{
ElfW(Sym) *sym;
int sym_bind, sh_num;
@@ -905,17 +908,17 @@ ST_FUNC void relocate_syms(TCCState *S, Section *symtab, int do_resolve)
for_each_elem(symtab, 1, sym, ElfW(Sym)) {
sh_num = sym->st_shndx;
if (sh_num == SHN_UNDEF) {
- name = (char *) S->symtab->link->data + sym->st_name;
+ name = (char *) s1->symtab->link->data + sym->st_name;
/* Use ld.so to resolve symbol for us (for tcc -run) */
if (do_resolve) {
#if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
/* dlsym() needs the undecorated name. */
- void *addr = dlsym(RTLD_DEFAULT, &name[S->leading_underscore]);
+ void *addr = dlsym(RTLD_DEFAULT, &name[s1->leading_underscore]);
#if TARGETOS_OpenBSD || TARGETOS_FreeBSD || TARGETOS_NetBSD
if (addr == NULL) {
int i;
- for (i = 0; i < S->nb_loaded_dlls; i++)
- if ((addr = dlsym(S->loaded_dlls[i]->handle, name)))
+ for (i = 0; i < s1->nb_loaded_dlls; i++)
+ if ((addr = dlsym(s1->loaded_dlls[i]->handle, name)))
break;
}
#endif
@@ -928,7 +931,7 @@ ST_FUNC void relocate_syms(TCCState *S, Section *symtab, int do_resolve)
}
#endif
/* if dynamic symbol exist, it will be used in relocate_section */
- } else if (S->dynsym && find_elf_sym(S->dynsym, name))
+ } else if (s1->dynsym && find_elf_sym(s1->dynsym, name))
goto found;
/* XXX: _fp_hw seems to be part of the ABI, so we ignore
it */
@@ -940,10 +943,10 @@ ST_FUNC void relocate_syms(TCCState *S, Section *symtab, int do_resolve)
if (sym_bind == STB_WEAK)
sym->st_value = 0;
else
- tcc_error_noabort(S, "undefined symbol '%s'", name);
+ tcc_error_noabort("undefined symbol '%s'", name);
} else if (sh_num < SHN_LORESERVE) {
/* add section base */
- sym->st_value += S->sections[sym->st_shndx]->sh_addr;
+ sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
}
found: ;
}
@@ -951,7 +954,7 @@ ST_FUNC void relocate_syms(TCCState *S, Section *symtab, int do_resolve)
/* relocate a given section (CPU dependent) by applying the relocations
in the associated relocation section */
-static void relocate_section(TCCState *S, Section *s, Section *sr)
+static void relocate_section(TCCState *s1, Section *s, Section *sr)
{
ElfW_Rel *rel;
ElfW(Sym) *sym;
@@ -970,13 +973,13 @@ static void relocate_section(TCCState *S, Section *s, Section *sr)
tgt += rel->r_addend;
#endif
addr = s->sh_addr + rel->r_offset;
- relocate(S, rel, type, ptr, addr, tgt);
+ relocate(s1, rel, type, ptr, addr, tgt);
}
#ifndef ELF_OBJ_ONLY
/* if the relocation is allocated, we change its symbol table */
if (sr->sh_flags & SHF_ALLOC) {
- sr->link = S->dynsym;
- if (S->output_type == TCC_OUTPUT_DLL) {
+ sr->link = s1->dynsym;
+ if (s1->output_type == TCC_OUTPUT_DLL) {
size_t r = (uint8_t*)qrel - sr->data;
if (sizeof ((Stab_Sym*)0)->n_value < PTR_SIZE
&& 0 == strcmp(s->name, ".stab"))
@@ -988,23 +991,23 @@ static void relocate_section(TCCState *S, Section *s, Section *sr)
}
/* relocate all sections */
-ST_FUNC void relocate_sections(TCCState *S)
+ST_FUNC void relocate_sections(TCCState *s1)
{
int i;
Section *s, *sr;
- for (i = 1; i < S->nb_sections; ++i) {
- sr = S->sections[i];
+ for (i = 1; i < s1->nb_sections; ++i) {
+ sr = s1->sections[i];
if (sr->sh_type != SHT_RELX)
continue;
- s = S->sections[sr->sh_info];
+ s = s1->sections[sr->sh_info];
#ifndef TCC_TARGET_MACHO
- if (s != S->got
- || S->static_link
- || S->output_type == TCC_OUTPUT_MEMORY)
+ if (s != s1->got
+ || s1->static_link
+ || s1->output_type == TCC_OUTPUT_MEMORY)
#endif
{
- relocate_section(S, s, sr);
+ relocate_section(s1, s, sr);
}
#ifndef ELF_OBJ_ONLY
if (sr->sh_flags & SHF_ALLOC) {
@@ -1020,7 +1023,7 @@ ST_FUNC void relocate_sections(TCCState *S)
#ifndef ELF_OBJ_ONLY
/* count the number of dynamic relocations so that we can reserve
their space */
-static int prepare_dynamic_rel(TCCState *S, Section *sr)
+static int prepare_dynamic_rel(TCCState *s1, Section *sr)
{
int count = 0;
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64) || \
@@ -1033,7 +1036,7 @@ static int prepare_dynamic_rel(TCCState *S, Section *sr)
switch(type) {
#if defined(TCC_TARGET_I386)
case R_386_32:
- if (!get_sym_attr(S, sym_index, 0)->dyn_index
+ if (!get_sym_attr(s1, sym_index, 0)->dyn_index
&& ((ElfW(Sym)*)symtab_section->data + sym_index)->st_shndx == SHN_UNDEF) {
/* don't fixup unresolved (weak) symbols */
rel->r_info = ELFW(R_INFO)(sym_index, R_386_RELATIVE);
@@ -1074,7 +1077,7 @@ static int prepare_dynamic_rel(TCCState *S, Section *sr)
#elif defined(TCC_TARGET_ARM64)
case R_AARCH64_PREL32:
#endif
- if (get_sym_attr(S, sym_index, 0)->dyn_index)
+ if (get_sym_attr(s1, sym_index, 0)->dyn_index)
count++;
break;
default:
@@ -1087,22 +1090,22 @@ static int prepare_dynamic_rel(TCCState *S, Section *sr)
#endif
#if !defined(ELF_OBJ_ONLY) || (defined(TCC_TARGET_MACHO) && defined TCC_IS_NATIVE)
-static void build_got(TCCState *S)
+static void build_got(TCCState *s1)
{
/* if no got, then create it */
- S->got = new_section(S, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
- S->got->sh_entsize = 4;
+ s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
+ s1->got->sh_entsize = 4;
set_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
- 0, S->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
+ 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
/* keep space for _DYNAMIC pointer and two dummy got entries */
- section_ptr_add(S, S->got, 3 * PTR_SIZE);
+ section_ptr_add(s1->got, 3 * PTR_SIZE);
}
/* Create a GOT and (for function call) a PLT entry corresponding to a symbol
- in S->symtab. When creating the dynamic symbol table entry for the GOT
+ in s1->symtab. When creating the dynamic symbol table entry for the GOT
relocation, use 'size' and 'info' for the corresponding symbol metadata.
Returns the offset of the GOT or (if any) PLT entry. */
-static struct sym_attr * put_got_entry(TCCState *S, int dyn_reloc_type,
+static struct sym_attr * put_got_entry(TCCState *s1, int dyn_reloc_type,
int sym_index)
{
int need_plt_entry;
@@ -1115,7 +1118,7 @@ static struct sym_attr * put_got_entry(TCCState *S, int dyn_reloc_type,
Section *s_rel;
need_plt_entry = (dyn_reloc_type == R_JMP_SLOT);
- attr = get_sym_attr(S, sym_index, 1);
+ attr = get_sym_attr(s1, sym_index, 1);
/* In case a function is both called and its address taken 2 GOT entries
are created, one for taking the address (GOT) and the other for the PLT
@@ -1123,18 +1126,18 @@ static struct sym_attr * put_got_entry(TCCState *S, int dyn_reloc_type,
if (need_plt_entry ? attr->plt_offset : attr->got_offset)
return attr;
- s_rel = S->got;
+ s_rel = s1->got;
if (need_plt_entry) {
- if (!S->plt) {
- S->plt = new_section(S, ".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
- S->plt->sh_entsize = 4;
+ if (!s1->plt) {
+ s1->plt = new_section(s1, ".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
+ s1->plt->sh_entsize = 4;
}
- s_rel = S->plt;
+ s_rel = s1->plt;
}
/* create the GOT entry */
- got_offset = S->got->data_offset;
- section_ptr_add(S, S->got, PTR_SIZE);
+ got_offset = s1->got->data_offset;
+ section_ptr_add(s1->got, PTR_SIZE);
/* Create the GOT relocation that will insert the address of the object or
function of interest in the GOT entry. This is a static relocation for
@@ -1148,7 +1151,7 @@ static struct sym_attr * put_got_entry(TCCState *S, int dyn_reloc_type,
name = (char *) symtab_section->link->data + sym->st_name;
//printf("sym %d %s\n", need_plt_entry, name);
- if (S->dynsym) {
+ if (s1->dynsym) {
if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
/* Hack alarm. We don't want to emit dynamic symbols
and symbol based relocs for STB_LOCAL symbols, but rather
@@ -1157,30 +1160,30 @@ static struct sym_attr * put_got_entry(TCCState *S, int dyn_reloc_type,
have to create a RELATIVE reloc anyway, so we misuse the
relocation slot to smuggle the symbol reference until
fill_local_got_entries. Not that the sym_index is
- relative to symtab_section, not S->dynsym! Nevertheless
- we use S->dyn_sym so that if this is the first call
+ relative to symtab_section, not s1->dynsym! Nevertheless
+ we use s1->dyn_sym so that if this is the first call
that got->reloc is correctly created. Also note that
RELATIVE relocs are not normally created for the .got,
so the types serves as a marker for later (and is retained
also for the final output, which is okay because then the
got is just normal data). */
- put_elf_reloc(S->dynsym, S->got, got_offset, R_RELATIVE,
+ put_elf_reloc(s1->dynsym, s1->got, got_offset, R_RELATIVE,
sym_index);
} else {
if (0 == attr->dyn_index)
- attr->dyn_index = set_elf_sym(S->dynsym, sym->st_value,
+ attr->dyn_index = set_elf_sym(s1->dynsym, sym->st_value,
sym->st_size, sym->st_info, 0,
sym->st_shndx, name);
- put_elf_reloc(S->dynsym, s_rel, got_offset, dyn_reloc_type,
+ put_elf_reloc(s1->dynsym, s_rel, got_offset, dyn_reloc_type,
attr->dyn_index);
}
} else {
- put_elf_reloc(symtab_section, S->got, got_offset, dyn_reloc_type,
+ put_elf_reloc(symtab_section, s1->got, got_offset, dyn_reloc_type,
sym_index);
}
if (need_plt_entry) {
- attr->plt_offset = create_plt_entry(S, got_offset, attr);
+ attr->plt_offset = create_plt_entry(s1, got_offset, attr);
/* create a symbol 'sym@plt' for the PLT jump vector */
len = strlen(name);
@@ -1188,8 +1191,8 @@ static struct sym_attr * put_got_entry(TCCState *S, int dyn_reloc_type,
len = sizeof plt_name - 5;
memcpy(plt_name, name, len);
strcpy(plt_name + len, "@plt");
- attr->plt_sym = put_elf_sym(S, S->symtab, attr->plt_offset, sym->st_size,
- ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0, S->plt->sh_num, plt_name);
+ attr->plt_sym = put_elf_sym(s1->symtab, attr->plt_offset, sym->st_size,
+ ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0, s1->plt->sh_num, plt_name);
} else {
attr->got_offset = got_offset;
}
@@ -1200,7 +1203,7 @@ static struct sym_attr * put_got_entry(TCCState *S, int dyn_reloc_type,
/* build GOT and PLT entries */
/* Two passes because R_JMP_SLOT should become first. Some targets
(arm, arm64) do not allow mixing R_JMP_SLOT and R_GLOB_DAT. */
-ST_FUNC void build_got_entries(TCCState *S)
+ST_FUNC void build_got_entries(TCCState *s1)
{
Section *s;
ElfW_Rel *rel;
@@ -1210,8 +1213,8 @@ ST_FUNC void build_got_entries(TCCState *S)
int pass = 0;
redo:
- for(i = 1; i < S->nb_sections; i++) {
- s = S->sections[i];
+ for(i = 1; i < s1->nb_sections; i++) {
+ s = s1->sections[i];
if (s->sh_type != SHT_RELX)
continue;
/* no need to handle got relocations */
@@ -1221,7 +1224,7 @@ redo:
type = ELFW(R_TYPE)(rel->r_info);
gotplt_entry = gotplt_entry_type(type);
if (gotplt_entry == -1)
- tcc_error(S, "Unknown relocation type for got: %d", type);
+ tcc_error ("Unknown relocation type for got: %d", type);
sym_index = ELFW(R_SYM)(rel->r_info);
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
@@ -1237,7 +1240,7 @@ redo:
if (sym->st_shndx == SHN_UNDEF) {
ElfW(Sym) *esym;
int dynindex;
- if (S->output_type == TCC_OUTPUT_DLL && ! PCRELATIVE_DLLPLT)
+ if (s1->output_type == TCC_OUTPUT_DLL && ! PCRELATIVE_DLLPLT)
continue;
/* Relocations for UNDEF symbols would normally need
to be transferred into the executable or shared object.
@@ -1249,10 +1252,10 @@ redo:
bind_exe_dynsyms (and the symbol adjusted to be defined),
and for functions we were generated a dynamic symbol
of function type. */
- if (S->dynsym) {
+ if (s1->dynsym) {
/* dynsym isn't set for -run :-/ */
- dynindex = get_sym_attr(S, sym_index, 0)->dyn_index;
- esym = (ElfW(Sym) *)S->dynsym->data + dynindex;
+ dynindex = get_sym_attr(s1, sym_index, 0)->dyn_index;
+ esym = (ElfW(Sym) *)s1->dynsym->data + dynindex;
if (dynindex
&& (ELFW(ST_TYPE)(esym->st_info) == STT_FUNC
|| (ELFW(ST_TYPE)(esym->st_info) == STT_NOTYPE
@@ -1277,7 +1280,7 @@ redo:
sym->st_shndx != SHN_UNDEF &&
(ELFW(ST_VISIBILITY)(sym->st_other) != STV_DEFAULT ||
ELFW(ST_BIND)(sym->st_info) == STB_LOCAL ||
- S->output_type == TCC_OUTPUT_EXE)) {
+ s1->output_type == TCC_OUTPUT_EXE)) {
if (pass != 0)
continue;
rel->r_info = ELFW(R_INFO)(sym_index, R_X86_64_PC32);
@@ -1286,7 +1289,7 @@ redo:
#endif
reloc_type = code_reloc(type);
if (reloc_type == -1)
- tcc_error(S, "Unknown relocation type: %d", type);
+ tcc_error ("Unknown relocation type: %d", type);
if (reloc_type != 0) {
jmp_slot:
@@ -1299,13 +1302,13 @@ redo:
reloc_type = R_GLOB_DAT;
}
- if (!S->got)
- build_got(S);
+ if (!s1->got)
+ build_got(s1);
if (gotplt_entry == BUILD_GOT_ONLY)
continue;
- attr = put_got_entry(S, reloc_type, sym_index);
+ attr = put_got_entry(s1, reloc_type, sym_index);
if (reloc_type == R_JMP_SLOT)
rel->r_info = ELFW(R_INFO)(attr->plt_sym, type);
@@ -1315,13 +1318,13 @@ redo:
goto redo;
/* .rel.plt refers to .got actually */
- if (S->plt && S->plt->reloc)
- S->plt->reloc->sh_info = S->got->sh_num;
+ if (s1->plt && s1->plt->reloc)
+ s1->plt->reloc->sh_info = s1->got->sh_num;
}
#endif
-ST_FUNC int set_global_sym(TCCState *S, const char *name, Section *sec, addr_t offs)
+ST_FUNC int set_global_sym(TCCState *s1, const char *name, Section *sec, addr_t offs)
{
int shn = sec ? sec->sh_num : offs || !name ? SHN_ABS : SHN_UNDEF;
if (sec && offs == -1)
@@ -1330,12 +1333,12 @@ ST_FUNC int set_global_sym(TCCState *S, const char *name, Section *sec, addr_t o
ELFW(ST_INFO)(name ? STB_GLOBAL : STB_LOCAL, STT_NOTYPE), 0, shn, name);
}
-static void add_init_array_defines(TCCState *S, const char *section_name)
+static void add_init_array_defines(TCCState *s1, const char *section_name)
{
Section *s;
addr_t end_offset;
char buf[1024];
- s = find_section_create(S, section_name, 0);
+ s = find_section_create(s1, section_name, 0);
if (!s) {
end_offset = 0;
s = data_section;
@@ -1343,48 +1346,48 @@ static void add_init_array_defines(TCCState *S, const char *section_name)
end_offset = s->data_offset;
}
snprintf(buf, sizeof(buf), "__%s_start", section_name + 1);
- set_global_sym(S, buf, s, 0);
+ set_global_sym(s1, buf, s, 0);
snprintf(buf, sizeof(buf), "__%s_end", section_name + 1);
- set_global_sym(S, buf, s, end_offset);
+ set_global_sym(s1, buf, s, end_offset);
}
#ifndef TCC_TARGET_PE
-static void tcc_add_support(TCCState *S, const char *filename)
+static void tcc_add_support(TCCState *s1, const char *filename)
{
char buf[1024];
- snprintf(buf, sizeof(buf), "%s/%s", S->tcc_lib_path, filename);
- tcc_add_file(S, buf);
+ snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, filename);
+ tcc_add_file(s1, buf);
}
#endif
-ST_FUNC void add_array (TCCState *S, const char *sec, int c)
+ST_FUNC void add_array (TCCState *s1, const char *sec, int c)
{
Section *s;
- s = find_section(S, sec);
+ s = find_section(s1, sec);
s->sh_flags |= SHF_WRITE;
#ifndef TCC_TARGET_PE
s->sh_type = sec[1] == 'i' ? SHT_INIT_ARRAY : SHT_FINI_ARRAY;
#endif
- put_elf_reloc (S->symtab, s, s->data_offset, R_DATA_PTR, c);
- section_ptr_add(S, s, PTR_SIZE);
+ put_elf_reloc (s1->symtab, s, s->data_offset, R_DATA_PTR, c);
+ section_ptr_add(s, PTR_SIZE);
}
#ifdef CONFIG_TCC_BCHECK
-ST_FUNC void tcc_add_bcheck(TCCState *S)
+ST_FUNC void tcc_add_bcheck(TCCState *s1)
{
- if (0 == S->do_bounds_check)
+ if (0 == s1->do_bounds_check)
return;
- section_ptr_add(S, bounds_section, sizeof(addr_t));
+ section_ptr_add(bounds_section, sizeof(addr_t));
}
#endif
/* set symbol to STB_LOCAL and resolve. The point is to not export it as
a dynamic symbol to allow so's to have one each with a different value. */
-static void set_local_sym(TCCState *S, const char *name, Section *s, int offset)
+static void set_local_sym(TCCState *s1, const char *name, Section *s, int offset)
{
- int c = find_elf_sym(S->symtab, name);
+ int c = find_elf_sym(s1->symtab, name);
if (c) {
- ElfW(Sym) *esym = (ElfW(Sym)*)S->symtab->data + c;
+ ElfW(Sym) *esym = (ElfW(Sym)*)s1->symtab->data + c;
esym->st_info = ELFW(ST_INFO)(STB_LOCAL, STT_NOTYPE);
esym->st_value = offset;
esym->st_shndx = s->sh_num;
@@ -1392,16 +1395,16 @@ static void set_local_sym(TCCState *S, const char *name, Section *s, int offset)
}
#ifdef CONFIG_TCC_BACKTRACE
-static void put_ptr(TCCState *S, Section *s, int offs)
+static void put_ptr(TCCState *s1, Section *s, int offs)
{
int c;
- c = set_global_sym(S, NULL, s, offs);
+ c = set_global_sym(s1, NULL, s, offs);
s = data_section;
- put_elf_reloc (S->symtab, s, s->data_offset, R_DATA_PTR, c);
- section_ptr_add(S, s, PTR_SIZE);
+ put_elf_reloc (s1->symtab, s, s->data_offset, R_DATA_PTR, c);
+ section_ptr_add(s, PTR_SIZE);
}
-ST_FUNC void tcc_add_btstub(TCCState *S)
+ST_FUNC void tcc_add_btstub(TCCState *s1)
{
Section *s;
int n, o;
@@ -1409,45 +1412,45 @@ ST_FUNC void tcc_add_btstub(TCCState *S)
s = data_section;
/* Align to PTR_SIZE */
- section_ptr_add(S, s, -s->data_offset & (PTR_SIZE - 1));
+ section_ptr_add(s, -s->data_offset & (PTR_SIZE - 1));
o = s->data_offset;
/* create (part of) a struct rt_context (see tccrun.c) */
- put_ptr(S, stab_section, 0);
- put_ptr(S, stab_section, -1);
- put_ptr(S, stab_section->link, 0);
- section_ptr_add(S, s, 3 * PTR_SIZE);
+ put_ptr(s1, stab_section, 0);
+ put_ptr(s1, stab_section, -1);
+ put_ptr(s1, stab_section->link, 0);
+ section_ptr_add(s, 3 * PTR_SIZE);
/* prog_base : local nameless symbol with offset 0 at SHN_ABS */
- put_ptr(S, NULL, 0);
+ put_ptr(s1, NULL, 0);
n = 2 * PTR_SIZE;
#ifdef CONFIG_TCC_BCHECK
- if (S->do_bounds_check) {
- put_ptr(S, bounds_section, 0);
+ if (s1->do_bounds_check) {
+ put_ptr(s1, bounds_section, 0);
n -= PTR_SIZE;
}
#endif
- section_ptr_add(S, s, n);
- cstr_new(S, &cstr);
- cstr_printf(S, &cstr,
+ section_ptr_add(s, n);
+ cstr_new(&cstr);
+ cstr_printf(&cstr,
"extern void __bt_init(),__bt_init_dll();"
"static void *__rt_info[];"
"__attribute__((constructor)) static void __bt_init_rt(){");
#ifdef TCC_TARGET_PE
- if (S->output_type == TCC_OUTPUT_DLL)
+ if (s1->output_type == TCC_OUTPUT_DLL)
#ifdef CONFIG_TCC_BCHECK
- cstr_printf(S, &cstr, "__bt_init_dll(%d);", S->do_bounds_check);
+ cstr_printf(&cstr, "__bt_init_dll(%d);", s1->do_bounds_check);
#else
- cstr_printf(S, &cstr, "__bt_init_dll(0);");
+ cstr_printf(&cstr, "__bt_init_dll(0);");
#endif
#endif
- cstr_printf(S, &cstr, "__bt_init(__rt_info,%d);}",
- S->output_type == TCC_OUTPUT_DLL ? 0 : S->rt_num_callers + 1);
- tcc_compile_string(S, cstr.data);
- cstr_free(S, &cstr);
- set_local_sym(S, &"___rt_info"[!S->leading_underscore], s, o);
+ cstr_printf(&cstr, "__bt_init(__rt_info,%d);}",
+ s1->output_type == TCC_OUTPUT_DLL ? 0 : s1->rt_num_callers + 1);
+ tcc_compile_string(s1, cstr.data);
+ cstr_free(&cstr);
+ set_local_sym(s1, &"___rt_info"[!s1->leading_underscore], s, o);
}
#endif
-static void tcc_tcov_add_file(TCCState *S, const char *filename)
+static void tcc_tcov_add_file(TCCState *s1, const char *filename)
{
CString cstr;
void *ptr;
@@ -1455,100 +1458,100 @@ static void tcc_tcov_add_file(TCCState *S, const char *filename)
if (tcov_section == NULL)
return;
- section_ptr_add(S, tcov_section, 1);
+ section_ptr_add(tcov_section, 1);
write32le (tcov_section->data, tcov_section->data_offset);
- cstr_new (S, &cstr);
+ cstr_new (&cstr);
if (filename[0] == '/')
- cstr_printf (S, &cstr, "%s.tcov", filename);
+ cstr_printf (&cstr, "%s.tcov", filename);
else {
getcwd (wd, sizeof(wd));
- cstr_printf (S, &cstr, "%s/%s.tcov", wd, filename);
+ cstr_printf (&cstr, "%s/%s.tcov", wd, filename);
}
- ptr = section_ptr_add(S, tcov_section, cstr.size + 1);
+ ptr = section_ptr_add(tcov_section, cstr.size + 1);
strcpy((char *)ptr, cstr.data);
unlink((char *)ptr);
#ifdef _WIN32
normalize_slashes((char *)ptr);
#endif
- cstr_free (S, &cstr);
+ cstr_free (&cstr);
- cstr_new(S, &cstr);
- cstr_printf(S, &cstr,
+ cstr_new(&cstr);
+ cstr_printf(&cstr,
"extern char *__tcov_data[];"
"extern void __store_test_coverage ();"
"__attribute__((destructor)) static void __tcov_exit() {"
"__store_test_coverage(__tcov_data);"
"}");
- tcc_compile_string(S, cstr.data);
- cstr_free(S, &cstr);
- set_local_sym(S, &"___tcov_data"[!S->leading_underscore], tcov_section, 0);
+ tcc_compile_string(s1, cstr.data);
+ cstr_free(&cstr);
+ set_local_sym(s1, &"___tcov_data"[!s1->leading_underscore], tcov_section, 0);
}
#ifndef TCC_TARGET_PE
/* add tcc runtime libraries */
-ST_FUNC void tcc_add_runtime(TCCState *S)
+ST_FUNC void tcc_add_runtime(TCCState *s1)
{
- S->filetype = 0;
+ s1->filetype = 0;
#ifdef CONFIG_TCC_BCHECK
- tcc_add_bcheck(S);
+ tcc_add_bcheck(s1);
#endif
- tcc_add_pragma_libs(S);
+ tcc_add_pragma_libs(s1);
/* add libc */
- if (!S->nostdlib) {
- if (S->option_pthread)
- tcc_add_library_err(S, "pthread");
- tcc_add_library_err(S, "c");
+ if (!s1->nostdlib) {
+ if (s1->option_pthread)
+ tcc_add_library_err(s1, "pthread");
+ tcc_add_library_err(s1, "c");
#ifdef TCC_LIBGCC
- if (!S->static_link) {
+ if (!s1->static_link) {
if (TCC_LIBGCC[0] == '/')
- tcc_add_file(S, TCC_LIBGCC);
+ tcc_add_file(s1, TCC_LIBGCC);
else
- tcc_add_dll(S, TCC_LIBGCC, 0);
+ tcc_add_dll(s1, TCC_LIBGCC, 0);
}
#endif
#if TCC_TARGET_ARM && TARGETOS_FreeBSD
- tcc_add_library_err(S, "gcc_s"); // unwind code
+ tcc_add_library_err(s1, "gcc_s"); // unwind code
#endif
#ifdef CONFIG_TCC_BCHECK
- if (S->do_bounds_check && S->output_type != TCC_OUTPUT_DLL) {
- tcc_add_library_err(S, "pthread");
+ if (s1->do_bounds_check && s1->output_type != TCC_OUTPUT_DLL) {
+ tcc_add_library_err(s1, "pthread");
#if !TARGETOS_OpenBSD && !TARGETOS_NetBSD
- tcc_add_library_err(S, "dl");
+ tcc_add_library_err(s1, "dl");
#endif
- tcc_add_support(S, "bcheck.o");
- if (S->static_link)
- tcc_add_library_err(S, "c");
+ tcc_add_support(s1, "bcheck.o");
+ if (s1->static_link)
+ tcc_add_library_err(s1, "c");
}
#endif
#ifdef CONFIG_TCC_BACKTRACE
- if (S->do_backtrace) {
- if (S->output_type == TCC_OUTPUT_EXE)
- tcc_add_support(S, "bt-exe.o");
- if (S->output_type != TCC_OUTPUT_DLL)
- tcc_add_support(S, "bt-log.o");
- if (S->output_type != TCC_OUTPUT_MEMORY)
- tcc_add_btstub(S);
+ if (s1->do_backtrace) {
+ if (s1->output_type == TCC_OUTPUT_EXE)
+ tcc_add_support(s1, "bt-exe.o");
+ if (s1->output_type != TCC_OUTPUT_DLL)
+ tcc_add_support(s1, "bt-log.o");
+ if (s1->output_type != TCC_OUTPUT_MEMORY)
+ tcc_add_btstub(s1);
}
#endif
if (TCC_LIBTCC1[0])
- tcc_add_support(S, TCC_LIBTCC1);
+ tcc_add_support(s1, TCC_LIBTCC1);
#if TARGETOS_OpenBSD || TARGETOS_FreeBSD || TARGETOS_NetBSD
/* add crt end if not memory output */
- if (S->output_type != TCC_OUTPUT_MEMORY) {
- if (S->output_type == TCC_OUTPUT_DLL)
- tcc_add_crt(S, "crtendS.o");
+ if (s1->output_type != TCC_OUTPUT_MEMORY) {
+ if (s1->output_type == TCC_OUTPUT_DLL)
+ tcc_add_crt(s1, "crtendS.o");
else
- tcc_add_crt(S, "crtend.o");
+ tcc_add_crt(s1, "crtend.o");
#if TARGETOS_FreeBSD || TARGETOS_NetBSD
- tcc_add_crt(S, "crtn.o");
+ tcc_add_crt(s1, "crtn.o");
#endif
}
#elif !defined(TCC_TARGET_MACHO)
/* add crt end if not memory output */
- if (S->output_type != TCC_OUTPUT_MEMORY)
- tcc_add_crt(S, "crtn.o");
+ if (s1->output_type != TCC_OUTPUT_MEMORY)
+ tcc_add_crt(s1, "crtn.o");
#endif
}
}
@@ -1557,32 +1560,32 @@ ST_FUNC void tcc_add_runtime(TCCState *S)
/* add various standard linker symbols (must be done after the
sections are filled (for example after allocating common
symbols)) */
-static void tcc_add_linker_symbols(TCCState *S)
+static void tcc_add_linker_symbols(TCCState *s1)
{
char buf[1024];
int i;
Section *s;
- set_global_sym(S, "_etext", text_section, -1);
- set_global_sym(S, "_edata", data_section, -1);
- set_global_sym(S, "_end", bss_section, -1);
+ set_global_sym(s1, "_etext", text_section, -1);
+ set_global_sym(s1, "_edata", data_section, -1);
+ set_global_sym(s1, "_end", bss_section, -1);
#if TARGETOS_OpenBSD
- set_global_sym(S, "__executable_start", NULL, ELF_START_ADDR);
+ set_global_sym(s1, "__executable_start", NULL, ELF_START_ADDR);
#endif
#ifdef TCC_TARGET_RISCV64
/* XXX should be .sdata+0x800, not .data+0x800 */
- set_global_sym(S, "__global_pointer$", data_section, 0x800);
+ set_global_sym(s1, "__global_pointer$", data_section, 0x800);
#endif
/* horrible new standard ldscript defines */
#ifndef TCC_TARGET_PE
- add_init_array_defines(S, ".preinit_array");
+ add_init_array_defines(s1, ".preinit_array");
#endif
- add_init_array_defines(S, ".init_array");
- add_init_array_defines(S, ".fini_array");
+ add_init_array_defines(s1, ".init_array");
+ add_init_array_defines(s1, ".fini_array");
/* add start and stop symbols for sections whose name can be
expressed in C */
- for(i = 1; i < S->nb_sections; i++) {
- s = S->sections[i];
+ for(i = 1; i < s1->nb_sections; i++) {
+ s = s1->sections[i];
if ((s->sh_flags & SHF_ALLOC)
&& (s->sh_type == SHT_PROGBITS
|| s->sh_type == SHT_STRTAB)) {
@@ -1598,15 +1601,15 @@ static void tcc_add_linker_symbols(TCCState *S)
p++;
}
snprintf(buf, sizeof(buf), "__start_%s", s->name);
- set_global_sym(S, buf, s, 0);
+ set_global_sym(s1, buf, s, 0);
snprintf(buf, sizeof(buf), "__stop_%s", s->name);
- set_global_sym(S, buf, s, -1);
+ set_global_sym(s1, buf, s, -1);
}
next_sec: ;
}
}
-ST_FUNC void resolve_common_syms(TCCState *S)
+ST_FUNC void resolve_common_syms(TCCState *s1)
{
ElfW(Sym) *sym;
@@ -1614,44 +1617,44 @@ ST_FUNC void resolve_common_syms(TCCState *S)
for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
if (sym->st_shndx == SHN_COMMON) {
/* symbol alignment is in st_value for SHN_COMMONs */
- sym->st_value = section_add(S, bss_section, sym->st_size,
+ sym->st_value = section_add(bss_section, sym->st_size,
sym->st_value);
sym->st_shndx = bss_section->sh_num;
}
}
/* Now assign linker provided symbols their value. */
- tcc_add_linker_symbols(S);
+ tcc_add_linker_symbols(s1);
}
#ifndef ELF_OBJ_ONLY
-ST_FUNC void fill_got_entry(TCCState *S, ElfW_Rel *rel)
+ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel)
{
int sym_index = ELFW(R_SYM) (rel->r_info);
ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
- struct sym_attr *attr = get_sym_attr(S, sym_index, 0);
+ struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
unsigned offset = attr->got_offset;
if (0 == offset)
return;
- section_reserve(S, S->got, offset + PTR_SIZE);
+ section_reserve(s1->got, offset + PTR_SIZE);
#if PTR_SIZE == 8
- write64le(S->got->data + offset, sym->st_value);
+ write64le(s1->got->data + offset, sym->st_value);
#else
- write32le(S->got->data + offset, sym->st_value);
+ write32le(s1->got->data + offset, sym->st_value);
#endif
}
/* Perform relocation to GOT or PLT entries */
-ST_FUNC void fill_got(TCCState *S)
+ST_FUNC void fill_got(TCCState *s1)
{
Section *s;
ElfW_Rel *rel;
int i;
- for(i = 1; i < S->nb_sections; i++) {
- s = S->sections[i];
+ for(i = 1; i < s1->nb_sections; i++) {
+ s = s1->sections[i];
if (s->sh_type != SHT_RELX)
continue;
/* no need to handle got relocations */
@@ -1664,7 +1667,7 @@ ST_FUNC void fill_got(TCCState *S)
case R_X86_64_GOTPCRELX:
case R_X86_64_REX_GOTPCRELX:
case R_X86_64_PLT32:
- fill_got_entry(S, rel);
+ fill_got_entry(s1, rel);
break;
}
}
@@ -1673,25 +1676,25 @@ ST_FUNC void fill_got(TCCState *S)
/* See put_got_entry for a description. This is the second stage
where GOT references to local defined symbols are rewritten. */
-static void fill_local_got_entries(TCCState *S)
+static void fill_local_got_entries(TCCState *s1)
{
ElfW_Rel *rel;
- if (!S->got->reloc)
+ if (!s1->got->reloc)
return;
- for_each_elem(S->got->reloc, 0, rel, ElfW_Rel) {
+ for_each_elem(s1->got->reloc, 0, rel, ElfW_Rel) {
if (ELFW(R_TYPE)(rel->r_info) == R_RELATIVE) {
int sym_index = ELFW(R_SYM) (rel->r_info);
ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
- struct sym_attr *attr = get_sym_attr(S, sym_index, 0);
+ struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
unsigned offset = attr->got_offset;
- if (offset != rel->r_offset - S->got->sh_addr)
- tcc_error_noabort(S, "huh");
+ if (offset != rel->r_offset - s1->got->sh_addr)
+ tcc_error_noabort("huh");
rel->r_info = ELFW(R_INFO)(0, R_RELATIVE);
#if SHT_RELX == SHT_RELA
rel->r_addend = sym->st_value;
#else
/* All our REL architectures also happen to be 32bit LE. */
- write32le(S->got->data + offset, sym->st_value);
+ write32le(s1->got->data + offset, sym->st_value);
#endif
}
}
@@ -1700,7 +1703,7 @@ static void fill_local_got_entries(TCCState *S)
/* Bind symbols of executable: resolve undefined symbols from exported symbols
in shared libraries and export non local defined symbols to shared libraries
if -rdynamic switch was given on command line */
-static void bind_exe_dynsyms(TCCState *S)
+static void bind_exe_dynsyms(TCCState *s1)
{
const char *name;
int sym_index, index;
@@ -1713,9 +1716,9 @@ static void bind_exe_dynsyms(TCCState *S)
for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
if (sym->st_shndx == SHN_UNDEF) {
name = (char *) symtab_section->link->data + sym->st_name;
- sym_index = find_elf_sym(S->dynsymtab_section, name);
+ sym_index = find_elf_sym(s1->dynsymtab_section, name);
if (sym_index) {
- esym = &((ElfW(Sym) *)S->dynsymtab_section->data)[sym_index];
+ esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index];
type = ELFW(ST_TYPE)(esym->st_info);
if ((type == STT_FUNC) || (type == STT_GNU_IFUNC)) {
/* Indirect functions shall have STT_FUNC type in executable
@@ -1726,31 +1729,31 @@ static void bind_exe_dynsyms(TCCState *S)
* the address of the function that would return that
* address */
int dynindex
- = put_elf_sym(S, S->dynsym, 0, esym->st_size,
+ = put_elf_sym(s1->dynsym, 0, esym->st_size,
ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC), 0, 0,
name);
int index = sym - (ElfW(Sym) *) symtab_section->data;
- get_sym_attr(S, index, 1)->dyn_index = dynindex;
+ get_sym_attr(s1, index, 1)->dyn_index = dynindex;
} else if (type == STT_OBJECT) {
unsigned long offset;
ElfW(Sym) *dynsym;
offset = bss_section->data_offset;
/* XXX: which alignment ? */
offset = (offset + 16 - 1) & -16;
- set_elf_sym (S->symtab, offset, esym->st_size,
+ set_elf_sym (s1->symtab, offset, esym->st_size,
esym->st_info, 0, bss_section->sh_num, name);
- index = put_elf_sym(S, S->dynsym, offset, esym->st_size,
+ index = put_elf_sym(s1->dynsym, offset, esym->st_size,
esym->st_info, 0, bss_section->sh_num,
name);
/* Ensure R_COPY works for weak symbol aliases */
if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
- for_each_elem(S->dynsymtab_section, 1, dynsym, ElfW(Sym)) {
+ for_each_elem(s1->dynsymtab_section, 1, dynsym, ElfW(Sym)) {
if ((dynsym->st_value == esym->st_value)
&& (ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL)) {
- char *dynname = (char *) S->dynsymtab_section->link->data
+ char *dynname = (char *) s1->dynsymtab_section->link->data
+ dynsym->st_name;
- put_elf_sym(S, S->dynsym, offset, dynsym->st_size,
+ put_elf_sym(s1->dynsym, offset, dynsym->st_size,
dynsym->st_info, 0,
bss_section->sh_num, dynname);
break;
@@ -1758,7 +1761,7 @@ static void bind_exe_dynsyms(TCCState *S)
}
}
- put_elf_reloc(S->dynsym, bss_section,
+ put_elf_reloc(s1->dynsym, bss_section,
offset, R_COPY, index);
offset += esym->st_size;
bss_section->data_offset = offset;
@@ -1769,13 +1772,13 @@ static void bind_exe_dynsyms(TCCState *S)
if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK ||
!strcmp(name, "_fp_hw")) {
} else {
- tcc_error_noabort(S, "undefined symbol '%s'", name);
+ tcc_error_noabort("undefined symbol '%s'", name);
}
}
- } else if (S->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
+ } else if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
/* if -rdynamic option, then export all non local symbols */
name = (char *) symtab_section->link->data + sym->st_name;
- set_elf_sym(S->dynsym, sym->st_value, sym->st_size, sym->st_info,
+ set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
0, sym->st_shndx, name);
}
}
@@ -1786,24 +1789,24 @@ static void bind_exe_dynsyms(TCCState *S)
search symbol first in executable and then in libraries. Therefore a
reference to a symbol already defined by a library can still be resolved by
a symbol in the executable. */
-static void bind_libs_dynsyms(TCCState *S)
+static void bind_libs_dynsyms(TCCState *s1)
{
const char *name;
int sym_index;
ElfW(Sym) *sym, *esym;
- for_each_elem(S->dynsymtab_section, 1, esym, ElfW(Sym)) {
- name = (char *) S->dynsymtab_section->link->data + esym->st_name;
+ for_each_elem(s1->dynsymtab_section, 1, esym, ElfW(Sym)) {
+ name = (char *) s1->dynsymtab_section->link->data + esym->st_name;
sym_index = find_elf_sym(symtab_section, name);
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
if (sym_index && sym->st_shndx != SHN_UNDEF
&& ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
- set_elf_sym(S->dynsym, sym->st_value, sym->st_size,
+ set_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
sym->st_info, 0, sym->st_shndx, name);
} else if (esym->st_shndx == SHN_UNDEF) {
/* weak symbols can stay undefined */
if (ELFW(ST_BIND)(esym->st_info) != STB_WEAK)
- tcc_warning(S, "undefined dynamic symbol '%s'", name);
+ tcc_warning("undefined dynamic symbol '%s'", name);
}
}
}
@@ -1812,7 +1815,7 @@ static void bind_libs_dynsyms(TCCState *S)
non local symbols they define can resolve a reference in another shared
library or in the executable. Correspondingly, it allows undefined local
symbols to be resolved by other shared libraries or by the executable. */
-static void export_global_syms(TCCState *S)
+static void export_global_syms(TCCState *s1)
{
int dynindex, index;
const char *name;
@@ -1821,36 +1824,36 @@ static void export_global_syms(TCCState *S)
for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
name = (char *) symtab_section->link->data + sym->st_name;
- dynindex = put_elf_sym(S, S->dynsym, sym->st_value, sym->st_size,
+ dynindex = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
sym->st_info, 0, sym->st_shndx, name);
index = sym - (ElfW(Sym) *) symtab_section->data;
- get_sym_attr(S, index, 1)->dyn_index = dynindex;
+ get_sym_attr(s1, index, 1)->dyn_index = dynindex;
}
}
}
/* decide if an unallocated section should be output. */
-static int set_sec_sizes(TCCState *S)
+static int set_sec_sizes(TCCState *s1)
{
int i;
Section *s;
int textrel = 0;
- int file_type = S->output_type;
+ int file_type = s1->output_type;
/* Allocate strings for section names */
- for(i = 1; i < S->nb_sections; i++) {
- s = S->sections[i];
+ for(i = 1; i < s1->nb_sections; i++) {
+ s = s1->sections[i];
if (s->sh_type == SHT_RELX && !(s->sh_flags & SHF_ALLOC)) {
/* when generating a DLL, we include relocations but
we may patch them */
if (file_type == TCC_OUTPUT_DLL
- && (S->sections[s->sh_info]->sh_flags & SHF_ALLOC)) {
- int count = prepare_dynamic_rel(S, s);
+ && (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC)) {
+ int count = prepare_dynamic_rel(s1, s);
if (count) {
/* allocate the section */
s->sh_flags |= SHF_ALLOC;
s->sh_size = count * sizeof(ElfW_Rel);
- if (!(S->sections[s->sh_info]->sh_flags & SHF_WRITE))
+ if (!(s1->sections[s->sh_info]->sh_flags & SHF_WRITE))
textrel = 1;
}
}
@@ -1858,7 +1861,7 @@ static int set_sec_sizes(TCCState *S)
#ifdef TCC_TARGET_ARM
|| s->sh_type == SHT_ARM_ATTRIBUTES
#endif
- || S->do_debug) {
+ || s1->do_debug) {
s->sh_size = s->data_offset;
}
@@ -1892,16 +1895,16 @@ struct ro_inf {
};
static void alloc_sec_names(
- TCCState *S, int is_obj
+ TCCState *s1, int is_obj
);
static int layout_any_sections(
- TCCState *S, int file_offset, int *sec_order, int is_obj
+ TCCState *s1, int file_offset, int *sec_order, int is_obj
);
/* Assign sections to segments and decide how are sections laid out when loaded
in memory. This function also fills corresponding program headers. */
-static int layout_sections(TCCState *S, ElfW(Phdr) *phdr,
+static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr,
int phnum, int phfill,
Section *interp,
struct ro_inf *roinf, int *sec_order)
@@ -1910,7 +1913,7 @@ static int layout_sections(TCCState *S, ElfW(Phdr) *phdr,
Section *s;
file_offset = 0;
- if (S->output_format == TCC_OUTPUT_FORMAT_ELF)
+ if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
{
@@ -1918,15 +1921,15 @@ static int layout_sections(TCCState *S, ElfW(Phdr) *phdr,
long long tmp;
addr_t addr;
ElfW(Phdr) *ph;
- int j, k, f, file_type = S->output_type;
+ int j, k, f, file_type = s1->output_type;
s_align = ELF_PAGE_SIZE;
- if (S->section_align)
- s_align = S->section_align;
+ if (s1->section_align)
+ s_align = s1->section_align;
- if (S->has_text_addr) {
+ if (s1->has_text_addr) {
int a_offset, p_offset;
- addr = S->text_addr;
+ addr = s1->text_addr;
/* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
ELF_PAGE_SIZE */
a_offset = (int) (addr & (s_align - 1));
@@ -1968,8 +1971,8 @@ static int layout_sections(TCCState *S, ElfW(Phdr) *phdr,
/* XXX: do faster and simpler sorting */
f = -1;
for(k = 0; k < 7; k++) {
- for(i = 1; i < S->nb_sections; i++) {
- s = S->sections[i];
+ for(i = 1; i < s1->nb_sections; i++) {
+ s = s1->sections[i];
/* compute if section should be included */
if (j == 0) {
if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_TLS)) !=
@@ -1994,7 +1997,7 @@ static int layout_sections(TCCState *S, ElfW(Phdr) *phdr,
if (k != 1)
continue;
} else if (s->sh_type == SHT_RELX) {
- if (S->plt && s == S->plt->reloc) {
+ if (s1->plt && s == s1->plt->reloc) {
if (k != 3)
continue;
} else {
@@ -2064,7 +2067,7 @@ static int layout_sections(TCCState *S, ElfW(Phdr) *phdr,
ph->p_memsz = addr - ph->p_vaddr;
ph++;
if (j == 0) {
- if (S->output_format == TCC_OUTPUT_FORMAT_ELF) {
+ if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
/* if in the middle of a page, we duplicate the page in
memory so that one copy is RX and the other is RW */
if ((addr & (s_align - 1)) != 0)
@@ -2078,14 +2081,14 @@ static int layout_sections(TCCState *S, ElfW(Phdr) *phdr,
}
/* all other sections come after */
- return layout_any_sections(S, file_offset, sec_order, 0);
+ return layout_any_sections(s1, file_offset, sec_order, 0);
}
/* put dynamic tag */
-static void put_dt(TCCState* S, Section *dynamic, int dt, addr_t val)
+static void put_dt(Section *dynamic, int dt, addr_t val)
{
ElfW(Dyn) *dyn;
- dyn = section_ptr_add(S, dynamic, sizeof(ElfW(Dyn)));
+ dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));
dyn->d_tag = dt;
dyn->d_un.d_val = val;
}
@@ -2161,72 +2164,72 @@ static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp,
/* Fill the dynamic section with tags describing the address and size of
sections */
-static void fill_dynamic(TCCState *S, struct dyn_inf *dyninf)
+static void fill_dynamic(TCCState *s1, struct dyn_inf *dyninf)
{
Section *dynamic = dyninf->dynamic;
Section *s;
/* put dynamic section entries */
- put_dt(S, dynamic, DT_HASH, S->dynsym->hash->sh_addr);
- put_dt(S, dynamic, DT_STRTAB, dyninf->dynstr->sh_addr);
- put_dt(S, dynamic, DT_SYMTAB, S->dynsym->sh_addr);
- put_dt(S, dynamic, DT_STRSZ, dyninf->dynstr->data_offset);
- put_dt(S, dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
+ put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
+ put_dt(dynamic, DT_STRTAB, dyninf->dynstr->sh_addr);
+ put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
+ put_dt(dynamic, DT_STRSZ, dyninf->dynstr->data_offset);
+ put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
#if PTR_SIZE == 8
- put_dt(S, dynamic, DT_RELA, dyninf->rel_addr);
- put_dt(S, dynamic, DT_RELASZ, dyninf->rel_size);
- put_dt(S, dynamic, DT_RELAENT, sizeof(ElfW_Rel));
- if (S->plt && S->plt->reloc) {
- put_dt(S, dynamic, DT_PLTGOT, S->got->sh_addr);
- put_dt(S, dynamic, DT_PLTRELSZ, S->plt->reloc->data_offset);
- put_dt(S, dynamic, DT_JMPREL, S->plt->reloc->sh_addr);
- put_dt(S, dynamic, DT_PLTREL, DT_RELA);
- }
- put_dt(S, dynamic, DT_RELACOUNT, 0);
+ put_dt(dynamic, DT_RELA, dyninf->rel_addr);
+ put_dt(dynamic, DT_RELASZ, dyninf->rel_size);
+ put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel));
+ if (s1->plt && s1->plt->reloc) {
+ put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr);
+ put_dt(dynamic, DT_PLTRELSZ, s1->plt->reloc->data_offset);
+ put_dt(dynamic, DT_JMPREL, s1->plt->reloc->sh_addr);
+ put_dt(dynamic, DT_PLTREL, DT_RELA);
+ }
+ put_dt(dynamic, DT_RELACOUNT, 0);
#else
- put_dt(S, dynamic, DT_REL, dyninf->rel_addr);
- put_dt(S, dynamic, DT_RELSZ, dyninf->rel_size);
- put_dt(S, dynamic, DT_RELENT, sizeof(ElfW_Rel));
- if (S->plt && S->plt->reloc) {
- put_dt(S, dynamic, DT_PLTGOT, S->got->sh_addr);
- put_dt(S, dynamic, DT_PLTRELSZ, S->plt->reloc->data_offset);
- put_dt(S, dynamic, DT_JMPREL, S->plt->reloc->sh_addr);
- put_dt(S, dynamic, DT_PLTREL, DT_REL);
+ put_dt(dynamic, DT_REL, dyninf->rel_addr);
+ put_dt(dynamic, DT_RELSZ, dyninf->rel_size);
+ put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
+ if (s1->plt && s1->plt->reloc) {
+ put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr);
+ put_dt(dynamic, DT_PLTRELSZ, s1->plt->reloc->data_offset);
+ put_dt(dynamic, DT_JMPREL, s1->plt->reloc->sh_addr);
+ put_dt(dynamic, DT_PLTREL, DT_REL);
}
- put_dt(S, dynamic, DT_RELCOUNT, 0);
+ put_dt(dynamic, DT_RELCOUNT, 0);
#endif
if (versym_section && verneed_section) {
/* The dynamic linker can not handle VERSYM without VERNEED */
- put_dt(S, dynamic, DT_VERSYM, versym_section->sh_addr);
- put_dt(S, dynamic, DT_VERNEED, verneed_section->sh_addr);
- put_dt(S, dynamic, DT_VERNEEDNUM, dt_verneednum);
+ put_dt(dynamic, DT_VERSYM, versym_section->sh_addr);
+ put_dt(dynamic, DT_VERNEED, verneed_section->sh_addr);
+ put_dt(dynamic, DT_VERNEEDNUM, dt_verneednum);
}
- s = find_section_create (S, ".preinit_array", 0);
+ s = find_section_create (s1, ".preinit_array", 0);
if (s && s->data_offset) {
- put_dt(S, dynamic, DT_PREINIT_ARRAY, s->sh_addr);
- put_dt(S, dynamic, DT_PREINIT_ARRAYSZ, s->data_offset);
+ put_dt(dynamic, DT_PREINIT_ARRAY, s->sh_addr);
+ put_dt(dynamic, DT_PREINIT_ARRAYSZ, s->data_offset);
}
- s = find_section_create (S, ".init_array", 0);
+ s = find_section_create (s1, ".init_array", 0);
if (s && s->data_offset) {
- put_dt(S, dynamic, DT_INIT_ARRAY, s->sh_addr);
- put_dt(S, dynamic, DT_INIT_ARRAYSZ, s->data_offset);
+ put_dt(dynamic, DT_INIT_ARRAY, s->sh_addr);
+ put_dt(dynamic, DT_INIT_ARRAYSZ, s->data_offset);
}
- s = find_section_create (S, ".fini_array", 0);
+ s = find_section_create (s1, ".fini_array", 0);
if (s && s->data_offset) {
- put_dt(S, dynamic, DT_FINI_ARRAY, s->sh_addr);
- put_dt(S, dynamic, DT_FINI_ARRAYSZ, s->data_offset);
+ put_dt(dynamic, DT_FINI_ARRAY, s->sh_addr);
+ put_dt(dynamic, DT_FINI_ARRAYSZ, s->data_offset);
}
- s = find_section_create (S, ".init", 0);
+ s = find_section_create (s1, ".init", 0);
if (s && s->data_offset) {
- put_dt(S, dynamic, DT_INIT, s->sh_addr);
+ put_dt(dynamic, DT_INIT, s->sh_addr);
}
- s = find_section_create (S, ".fini", 0);
+ s = find_section_create (s1, ".fini", 0);
if (s && s->data_offset) {
- put_dt(S, dynamic, DT_FINI, s->sh_addr);
+ put_dt(dynamic, DT_FINI, s->sh_addr);
}
- if (S->do_debug)
- put_dt(S, dynamic, DT_DEBUG, 0);
- put_dt(S, dynamic, DT_NULL, 0);
+ if (s1->do_debug)
+ put_dt(dynamic, DT_DEBUG, 0);
+ put_dt(dynamic, DT_NULL, 0);
}
/* Remove gaps between RELX sections.
@@ -2234,18 +2237,18 @@ static void fill_dynamic(TCCState *S, struct dyn_inf *dyninf)
The gaps are then filled with 0 in tcc_output_elf. The 0 is intepreted as
R_...NONE reloc. This does work on most targets but on OpenBSD/arm64 this
is illegal. OpenBSD/arm64 does not support R_...NONE reloc. */
-static void update_reloc_sections(TCCState *S, struct dyn_inf *dyninf)
+static void update_reloc_sections(TCCState *s1, struct dyn_inf *dyninf)
{
int i;
unsigned long file_offset = 0;
Section *s;
- Section *relocplt = S->plt ? S->plt->reloc : NULL;
+ Section *relocplt = s1->plt ? s1->plt->reloc : NULL;
/* dynamic relocation table information, for .dynamic section */
dyninf->rel_addr = dyninf->rel_size = 0;
- for(i = 1; i < S->nb_sections; i++) {
- s = S->sections[i];
+ for(i = 1; i < s1->nb_sections; i++) {
+ s = s1->sections[i];
if (s->sh_type == SHT_RELX && s != relocplt) {
if (dyninf->rel_size == 0) {
dyninf->rel_addr = s->sh_addr;
@@ -2264,7 +2267,7 @@ static void update_reloc_sections(TCCState *S, struct dyn_inf *dyninf)
/* Create an ELF file on disk.
This function handle ELF specific layout requirements */
-static void tcc_output_elf(TCCState *S, FILE *f, int phnum, ElfW(Phdr) *phdr,
+static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
int file_offset, int *sec_order)
{
int i, shnum, offset, size, file_type;
@@ -2272,8 +2275,8 @@ static void tcc_output_elf(TCCState *S, FILE *f, int phnum, ElfW(Phdr) *phdr,
ElfW(Ehdr) ehdr;
ElfW(Shdr) shdr, *sh;
- file_type = S->output_type;
- shnum = S->nb_sections;
+ file_type = s1->output_type;
+ shnum = s1->nb_sections;
memset(&ehdr, 0, sizeof(ehdr));
@@ -2303,7 +2306,7 @@ static void tcc_output_elf(TCCState *S, FILE *f, int phnum, ElfW(Phdr) *phdr,
ehdr.e_flags = EF_ARM_EABI_VER4;
if (file_type == TCC_OUTPUT_EXE || file_type == TCC_OUTPUT_DLL)
ehdr.e_flags |= EF_ARM_HASENTRY;
- if (S->float_abi == ARM_HARD_FLOAT)
+ if (s1->float_abi == ARM_HARD_FLOAT)
ehdr.e_flags |= EF_ARM_VFP_FLOAT;
else
ehdr.e_flags |= EF_ARM_SOFT_FLOAT;
@@ -2317,7 +2320,7 @@ static void tcc_output_elf(TCCState *S, FILE *f, int phnum, ElfW(Phdr) *phdr,
default:
case TCC_OUTPUT_EXE:
ehdr.e_type = ET_EXEC;
- ehdr.e_entry = get_sym_addr(S, "_start", 1, 0);
+ ehdr.e_entry = get_sym_addr(s1, "_start", 1, 0);
break;
case TCC_OUTPUT_DLL:
ehdr.e_type = ET_DYN;
@@ -2340,9 +2343,9 @@ static void tcc_output_elf(TCCState *S, FILE *f, int phnum, ElfW(Phdr) *phdr,
fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
- sort_syms(S, symtab_section);
- for(i = 1; i < S->nb_sections; i++) {
- s = S->sections[sec_order[i]];
+ sort_syms(s1, symtab_section);
+ for(i = 1; i < s1->nb_sections; i++) {
+ s = s1->sections[sec_order[i]];
if (s->sh_type != SHT_NOBITS) {
while (offset < s->sh_offset) {
fputc(0, f);
@@ -2361,10 +2364,10 @@ static void tcc_output_elf(TCCState *S, FILE *f, int phnum, ElfW(Phdr) *phdr,
offset++;
}
- for(i = 0; i < S->nb_sections; i++) {
+ for(i = 0; i < s1->nb_sections; i++) {
sh = &shdr;
memset(sh, 0, sizeof(ElfW(Shdr)));
- s = S->sections[i];
+ s = s1->sections[i];
if (s) {
sh->sh_name = s->sh_name;
sh->sh_type = s->sh_type;
@@ -2382,15 +2385,15 @@ static void tcc_output_elf(TCCState *S, FILE *f, int phnum, ElfW(Phdr) *phdr,
}
}
-static void tcc_output_binary(TCCState *S, FILE *f,
+static void tcc_output_binary(TCCState *s1, FILE *f,
const int *sec_order)
{
Section *s;
int i, offset, size;
offset = 0;
- for(i=1;i<S->nb_sections;i++) {
- s = S->sections[sec_order[i]];
+ for(i=1;i<s1->nb_sections;i++) {
+ s = s1->sections[sec_order[i]];
if (s->sh_type != SHT_NOBITS &&
(s->sh_flags & SHF_ALLOC)) {
while (offset < s->sh_offset) {
@@ -2405,13 +2408,13 @@ static void tcc_output_binary(TCCState *S, FILE *f,
}
/* Write an elf, coff or "binary" file */
-static int tcc_write_elf_file(TCCState *S, const char *filename, int phnum,
+static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum,
ElfW(Phdr) *phdr, int file_offset, int *sec_order)
{
int fd, mode, file_type;
FILE *f;
- file_type = S->output_type;
+ file_type = s1->output_type;
if (file_type == TCC_OUTPUT_OBJ)
mode = 0666;
else
@@ -2419,22 +2422,22 @@ static int tcc_write_elf_file(TCCState *S, const char *filename, int phnum,
unlink(filename);
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
if (fd < 0) {
- tcc_error_noabort(S, "could not write '%s'", filename);
+ tcc_error_noabort("could not write '%s'", filename);
return -1;
}
f = fdopen(fd, "wb");
- if (S->verbose)
+ if (s1->verbose)
printf("<- %s\n", filename);
#ifdef TCC_TARGET_COFF
- if (S->output_format == TCC_OUTPUT_FORMAT_COFF)
- tcc_output_coff(S, f);
+ if (s1->output_format == TCC_OUTPUT_FORMAT_COFF)
+ tcc_output_coff(s1, f);
else
#endif
- if (S->output_format == TCC_OUTPUT_FORMAT_ELF)
- tcc_output_elf(S, f, phnum, phdr, file_offset, sec_order);
+ if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
+ tcc_output_elf(s1, f, phnum, phdr, file_offset, sec_order);
else
- tcc_output_binary(S, f, sec_order);
+ tcc_output_binary(s1, f, sec_order);
fclose(f);
return 0;
@@ -2443,16 +2446,16 @@ static int tcc_write_elf_file(TCCState *S, const char *filename, int phnum,
#ifndef ELF_OBJ_ONLY
/* Sort section headers by assigned sh_addr, remove sections
that we aren't going to output. */
-static void tidy_section_headers(TCCState *S, int *sec_order)
+static void tidy_section_headers(TCCState *s1, int *sec_order)
{
int i, nnew, l, *backmap;
Section **snew, *s;
ElfW(Sym) *sym;
- snew = tcc_malloc(S, S->nb_sections * sizeof(snew[0]));
- backmap = tcc_malloc(S, S->nb_sections * sizeof(backmap[0]));
- for (i = 0, nnew = 0, l = S->nb_sections; i < S->nb_sections; i++) {
- s = S->sections[sec_order[i]];
+ snew = tcc_malloc(s1->nb_sections * sizeof(snew[0]));
+ backmap = tcc_malloc(s1->nb_sections * sizeof(backmap[0]));
+ for (i = 0, nnew = 0, l = s1->nb_sections; i < s1->nb_sections; i++) {
+ s = s1->sections[sec_order[i]];
if (!i || s->sh_name) {
backmap[sec_order[i]] = nnew;
snew[nnew] = s;
@@ -2474,21 +2477,21 @@ static void tidy_section_headers(TCCState *S, int *sec_order)
for_each_elem(symtab_section, 1, sym, ElfW(Sym))
if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
sym->st_shndx = backmap[sym->st_shndx];
- if ( !S->static_link ) {
- for_each_elem(S->dynsym, 1, sym, ElfW(Sym))
+ if ( !s1->static_link ) {
+ for_each_elem(s1->dynsym, 1, sym, ElfW(Sym))
if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
sym->st_shndx = backmap[sym->st_shndx];
}
- for (i = 0; i < S->nb_sections; i++)
+ for (i = 0; i < s1->nb_sections; i++)
sec_order[i] = i;
- tcc_free(S, S->sections);
- S->sections = snew;
- S->nb_sections = nnew;
- tcc_free(S, backmap);
+ tcc_free(s1->sections);
+ s1->sections = snew;
+ s1->nb_sections = nnew;
+ tcc_free(backmap);
}
#ifdef TCC_TARGET_ARM
-static void create_arm_attribute_section(TCCState *S)
+static void create_arm_attribute_section(TCCState *s1)
{
// Needed for DLL support.
static const unsigned char arm_attr[] = {
@@ -2511,11 +2514,11 @@ static void create_arm_attribute_section(TCCState *S)
0x1c, 0x01, // 'ABI_VFP_args', 'VFP registers'
0x22, 0x01 // 'CPU_unaligned_access', 'v6'
};
- Section *attr = new_section(S, ".ARM.attributes", SHT_ARM_ATTRIBUTES, 0);
- unsigned char *ptr = section_ptr_add(S, attr, sizeof(arm_attr));
+ Section *attr = new_section(s1, ".ARM.attributes", SHT_ARM_ATTRIBUTES, 0);
+ unsigned char *ptr = section_ptr_add(attr, sizeof(arm_attr));
attr->sh_addralign = 1;
memcpy(ptr, arm_attr, sizeof(arm_attr));
- if (S->float_abi != ARM_HARD_FLOAT) {
+ if (s1->float_abi != ARM_HARD_FLOAT) {
ptr[26] = 0x00; // 'FP_arch', 'No'
ptr[41] = 0x1e; // 'ABI_optimization_goals'
ptr[42] = 0x06; // 'Aggressive Debug'
@@ -2524,14 +2527,14 @@ static void create_arm_attribute_section(TCCState *S)
#endif
#if TARGETOS_OpenBSD || TARGETOS_NetBSD
-static Section *create_bsd_note_section(TCCState *S,
+static Section *create_bsd_note_section(TCCState *s1,
const char *name,
const char *value)
{
- Section *s = find_section (S, name);
+ Section *s = find_section (s1, name);
if (s->data_offset == 0) {
- char *ptr = section_ptr_add(S, s, sizeof(ElfW(Nhdr)) + 8 + 4);
+ char *ptr = section_ptr_add(s, sizeof(ElfW(Nhdr)) + 8 + 4);
ElfW(Nhdr) *note = (ElfW(Nhdr) *) ptr;
s->sh_type = SHT_NOTE;
@@ -2546,7 +2549,7 @@ static Section *create_bsd_note_section(TCCState *S,
/* Output an elf, coff or binary file */
/* XXX: suppress unneeded sections */
-static int elf_output_file(TCCState *S, const char *filename)
+static int elf_output_file(TCCState *s1, const char *filename)
{
int i, ret, phnum, phfill, shnum, file_type, file_offset, *sec_order;
struct dyn_inf dyninf = {0};
@@ -2556,121 +2559,121 @@ static int elf_output_file(TCCState *S, const char *filename)
struct ro_inf *roinf_use = NULL;
int textrel;
- file_type = S->output_type;
- S->nb_errors = 0;
+ file_type = s1->output_type;
+ s1->nb_errors = 0;
ret = -1;
phdr = NULL;
sec_order = NULL;
interp = dynamic = dynstr = note = NULL;
#ifdef TCC_TARGET_ARM
- create_arm_attribute_section (S);
+ create_arm_attribute_section (s1);
#endif
#if TARGETOS_OpenBSD
- note = create_bsd_note_section (S, ".note.openbsd.ident", "OpenBSD");
+ note = create_bsd_note_section (s1, ".note.openbsd.ident", "OpenBSD");
#endif
#if TARGETOS_NetBSD
- note = create_bsd_note_section (S, ".note.netbsd.ident", "NetBSD");
+ note = create_bsd_note_section (s1, ".note.netbsd.ident", "NetBSD");
#endif
{
/* if linking, also link in runtime libraries (libc, libgcc, etc.) */
- tcc_add_runtime(S);
- resolve_common_syms(S);
+ tcc_add_runtime(s1);
+ resolve_common_syms(s1);
- if (!S->static_link) {
+ if (!s1->static_link) {
if (file_type == TCC_OUTPUT_EXE) {
char *ptr;
/* allow override the dynamic loader */
const char *elfint = getenv("LD_SO");
if (elfint == NULL)
- elfint = DEFAULT_ELFINTERP(S);
+ elfint = DEFAULT_ELFINTERP(s1);
/* add interpreter section only if executable */
- interp = new_section(S, ".interp", SHT_PROGBITS, SHF_ALLOC);
+ interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
interp->sh_addralign = 1;
- ptr = section_ptr_add(S, interp, 1 + strlen(elfint));
+ ptr = section_ptr_add(interp, 1 + strlen(elfint));
strcpy(ptr, elfint);
}
/* add dynamic symbol table */
- S->dynsym = new_symtab(S, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
+ s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
".dynstr",
".hash", SHF_ALLOC);
/* Number of local symbols (readelf complains if not set) */
- S->dynsym->sh_info = 1;
- dynstr = S->dynsym->link;
+ s1->dynsym->sh_info = 1;
+ dynstr = s1->dynsym->link;
/* add dynamic section */
- dynamic = new_section(S, ".dynamic", SHT_DYNAMIC,
+ dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
SHF_ALLOC | SHF_WRITE);
dynamic->link = dynstr;
dynamic->sh_entsize = sizeof(ElfW(Dyn));
- build_got(S);
+ build_got(s1);
if (file_type == TCC_OUTPUT_EXE) {
- bind_exe_dynsyms(S);
- if (S->nb_errors)
+ bind_exe_dynsyms(s1);
+ if (s1->nb_errors)
goto the_end;
- bind_libs_dynsyms(S);
+ bind_libs_dynsyms(s1);
} else {
/* shared library case: simply export all global symbols */
- export_global_syms(S);
+ export_global_syms(s1);
}
}
- build_got_entries(S);
- version_add (S);
+ build_got_entries(s1);
+ version_add (s1);
}
- textrel = set_sec_sizes(S);
- alloc_sec_names(S, 0);
+ textrel = set_sec_sizes(s1);
+ alloc_sec_names(s1, 0);
- if (!S->static_link) {
+ if (!s1->static_link) {
int i;
/* add a list of needed dlls */
- for(i = 0; i < S->nb_loaded_dlls; i++) {
- DLLReference *dllref = S->loaded_dlls[i];
+ for(i = 0; i < s1->nb_loaded_dlls; i++) {
+ DLLReference *dllref = s1->loaded_dlls[i];
if (dllref->level == 0)
- put_dt(S, dynamic, DT_NEEDED, put_elf_str(S, dynstr, dllref->name));
+ put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
}
- if (S->rpath)
- put_dt(S, dynamic, S->enable_new_dtags ? DT_RUNPATH : DT_RPATH,
- put_elf_str(S, dynstr, S->rpath));
+ if (s1->rpath)
+ put_dt(dynamic, s1->enable_new_dtags ? DT_RUNPATH : DT_RPATH,
+ put_elf_str(dynstr, s1->rpath));
if (file_type == TCC_OUTPUT_DLL) {
- if (S->soname)
- put_dt(S, dynamic, DT_SONAME, put_elf_str(S, dynstr, S->soname));
+ if (s1->soname)
+ put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname));
/* XXX: currently, since we do not handle PIC code, we
must relocate the readonly segments */
if (textrel)
- put_dt(S, dynamic, DT_TEXTREL, 0);
+ put_dt(dynamic, DT_TEXTREL, 0);
}
- if (S->symbolic)
- put_dt(S, dynamic, DT_SYMBOLIC, 0);
+ if (s1->symbolic)
+ put_dt(dynamic, DT_SYMBOLIC, 0);
dyninf.dynamic = dynamic;
dyninf.dynstr = dynstr;
/* remember offset and reserve space for 2nd call below */
dyninf.data_offset = dynamic->data_offset;
- fill_dynamic(S, &dyninf);
+ fill_dynamic(s1, &dyninf);
dynamic->sh_size = dynamic->data_offset;
dynstr->sh_size = dynstr->data_offset;
}
- for (i = 1; i < S->nb_sections &&
- !(S->sections[i]->sh_flags & SHF_TLS); i++);
- phfill = 2 + (i < S->nb_sections);
+ for (i = 1; i < s1->nb_sections &&
+ !(s1->sections[i]->sh_flags & SHF_TLS); i++);
+ phfill = 2 + (i < s1->nb_sections);
/* compute number of program headers */
if (file_type == TCC_OUTPUT_DLL)
phnum = 3;
- else if (S->static_link)
+ else if (s1->static_link)
phnum = 3;
else {
- phnum = 5 + (i < S->nb_sections);
+ phnum = 5 + (i < s1->nb_sections);
}
phnum += note != NULL;
@@ -2680,15 +2683,15 @@ static int elf_output_file(TCCState *S, const char *filename)
#endif
/* allocate program segment headers */
- phdr = tcc_mallocz(S, phnum * sizeof(ElfW(Phdr)));
+ phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
/* compute number of sections */
- shnum = S->nb_sections;
+ shnum = s1->nb_sections;
/* this array is used to reorder sections in the output file */
- sec_order = tcc_malloc(S, sizeof(int) * shnum);
+ sec_order = tcc_malloc(sizeof(int) * shnum);
sec_order[0] = 0;
/* compute section to program header mapping */
- file_offset = layout_sections(S, phdr, phnum, phfill, interp, &roinf, sec_order + 1);
+ file_offset = layout_sections(s1, phdr, phnum, phfill, interp, &roinf, sec_order + 1);
/* Fill remaining program header and finalize relocation related to dynamic
linking. */
@@ -2698,75 +2701,75 @@ static int elf_output_file(TCCState *S, const char *filename)
ElfW(Sym) *sym;
/* put in GOT the dynamic section address and relocate PLT */
- write32le(S->got->data, dynamic->sh_addr);
+ write32le(s1->got->data, dynamic->sh_addr);
if (file_type == TCC_OUTPUT_EXE
|| (RELOCATE_DLLPLT && file_type == TCC_OUTPUT_DLL))
- relocate_plt(S);
+ relocate_plt(s1);
/* relocate symbols in .dynsym now that final addresses are known */
- for_each_elem(S->dynsym, 1, sym, ElfW(Sym)) {
+ for_each_elem(s1->dynsym, 1, sym, ElfW(Sym)) {
if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE) {
/* do symbol relocation */
- sym->st_value += S->sections[sym->st_shndx]->sh_addr;
+ sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
}
}
}
/* if building executable or DLL, then relocate each section
except the GOT which is already relocated */
- relocate_syms(S, S->symtab, 0);
+ relocate_syms(s1, s1->symtab, 0);
ret = -1;
- if (S->nb_errors != 0)
+ if (s1->nb_errors != 0)
goto the_end;
- relocate_sections(S);
+ relocate_sections(s1);
if (dynamic) {
- update_reloc_sections (S, &dyninf);
+ update_reloc_sections (s1, &dyninf);
dynamic->data_offset = dyninf.data_offset;
- fill_dynamic(S, &dyninf);
+ fill_dynamic(s1, &dyninf);
}
- tidy_section_headers(S, sec_order);
+ tidy_section_headers(s1, sec_order);
/* Perform relocation to GOT or PLT entries */
- if (file_type == TCC_OUTPUT_EXE && S->static_link)
- fill_got(S);
- else if (S->got)
- fill_local_got_entries(S);
+ if (file_type == TCC_OUTPUT_EXE && s1->static_link)
+ fill_got(s1);
+ else if (s1->got)
+ fill_local_got_entries(s1);
}
/* Create the ELF file with name 'filename' */
- ret = tcc_write_elf_file(S, filename, phnum, phdr, file_offset, sec_order);
- S->nb_sections = shnum;
+ ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order);
+ s1->nb_sections = shnum;
the_end:
- tcc_free(S, sec_order);
- tcc_free(S, phdr);
+ tcc_free(sec_order);
+ tcc_free(phdr);
return ret;
}
#endif /* ndef ELF_OBJ_ONLY */
/* Allocate strings for section names */
-static void alloc_sec_names(TCCState *S, int is_obj)
+static void alloc_sec_names(TCCState *s1, int is_obj)
{
int i;
Section *s, *strsec;
- strsec = new_section(S, ".shstrtab", SHT_STRTAB, 0);
- put_elf_str(S, strsec, "");
- for(i = 1; i < S->nb_sections; i++) {
- s = S->sections[i];
+ strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
+ put_elf_str(strsec, "");
+ for(i = 1; i < s1->nb_sections; i++) {
+ s = s1->sections[i];
if (is_obj)
s->sh_size = s->data_offset;
if (s == strsec || s->sh_size || (s->sh_flags & SHF_ALLOC))
- s->sh_name = put_elf_str(S, strsec, s->name);
+ s->sh_name = put_elf_str(strsec, s->name);
}
strsec->sh_size = strsec->data_offset;
}
-static int layout_any_sections(TCCState *S, int file_offset, int *sec_order, int is_obj)
+static int layout_any_sections(TCCState *s1, int file_offset, int *sec_order, int is_obj)
{
int i;
Section *s;
- for(i = 1; i < S->nb_sections; i++) {
- s = S->sections[i];
+ for(i = 1; i < s1->nb_sections; i++) {
+ s = s1->sections[i];
if (!is_obj && (s->sh_flags & SHF_ALLOC))
continue;
*sec_order++ = i;
@@ -2780,38 +2783,38 @@ static int layout_any_sections(TCCState *S, int file_offset, int *sec_order, int
}
/* Output an elf .o file */
-static int elf_output_obj(TCCState *S, const char *filename)
+static int elf_output_obj(TCCState *s1, const char *filename)
{
int ret, file_offset;
int *sec_order;
- S->nb_errors = 0;
+ s1->nb_errors = 0;
/* Allocate strings for section names */
- alloc_sec_names(S, 1);
+ alloc_sec_names(s1, 1);
/* this array is used to reorder sections in the output file */
- sec_order = tcc_malloc(S, sizeof(int) * S->nb_sections);
+ sec_order = tcc_malloc(sizeof(int) * s1->nb_sections);
sec_order[0] = 0;
- file_offset = layout_any_sections(S, sizeof (ElfW(Ehdr)), sec_order + 1, 1);
+ file_offset = layout_any_sections(s1, sizeof (ElfW(Ehdr)), sec_order + 1, 1);
/* Create the ELF file with name 'filename' */
- ret = tcc_write_elf_file(S, filename, 0, NULL, file_offset, sec_order);
- tcc_free(S, sec_order);
+ ret = tcc_write_elf_file(s1, filename, 0, NULL, file_offset, sec_order);
+ tcc_free(sec_order);
return ret;
}
-LIBTCCAPI int tcc_output_file(TCCState *S, const char *filename)
+LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename)
{
- if (S->test_coverage)
- tcc_tcov_add_file(S, filename);
- if (S->output_type == TCC_OUTPUT_OBJ)
- return elf_output_obj(S, filename);
+ if (s->test_coverage)
+ tcc_tcov_add_file(s, filename);
+ if (s->output_type == TCC_OUTPUT_OBJ)
+ return elf_output_obj(s, filename);
#ifdef TCC_TARGET_PE
- return pe_output_file(S, filename);
+ return pe_output_file(s, filename);
#elif TCC_TARGET_MACHO
- return macho_output_file(S, filename);
+ return macho_output_file(s, filename);
#else
- return elf_output_file(S, filename);
+ return elf_output_file(s, filename);
#endif
}
@@ -2827,11 +2830,11 @@ ST_FUNC ssize_t full_read(int fd, void *buf, size_t count) {
}
}
-ST_FUNC void *load_data(TCCState* S, int fd, unsigned long file_offset, unsigned long size)
+ST_FUNC void *load_data(int fd, unsigned long file_offset, unsigned long size)
{
void *data;
- data = tcc_malloc(S, size);
+ data = tcc_malloc(size);
lseek(fd, file_offset, SEEK_SET);
full_read(fd, data, size);
return data;
@@ -2865,7 +2868,7 @@ ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h)
/* load an object file and merge it with current files */
/* XXX: handle correctly stab (debug) info */
-ST_FUNC int tcc_load_object_file(TCCState *S,
+ST_FUNC int tcc_load_object_file(TCCState *s1,
int fd, unsigned long file_offset)
{
ElfW(Ehdr) ehdr;
@@ -2887,17 +2890,17 @@ ST_FUNC int tcc_load_object_file(TCCState *S,
if (ehdr.e_ident[5] != ELFDATA2LSB ||
ehdr.e_machine != EM_TCC_TARGET) {
fail1:
- tcc_error_noabort(S, "invalid object file");
+ tcc_error_noabort("invalid object file");
return -1;
}
/* read sections */
- shdr = load_data(S, fd, file_offset + ehdr.e_shoff,
+ shdr = load_data(fd, file_offset + ehdr.e_shoff,
sizeof(ElfW(Shdr)) * ehdr.e_shnum);
- sm_table = tcc_mallocz(S, sizeof(SectionMergeInfo) * ehdr.e_shnum);
+ sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
/* load section names */
sh = &shdr[ehdr.e_shstrndx];
- strsec = load_data(S, fd, file_offset + sh->sh_offset, sh->sh_size);
+ strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
/* load symtab and strtab */
old_to_new_syms = NULL;
@@ -2911,18 +2914,18 @@ ST_FUNC int tcc_load_object_file(TCCState *S,
sh = &shdr[i];
if (sh->sh_type == SHT_SYMTAB) {
if (symtab) {
- tcc_error_noabort(S, "object must contain only one symtab");
+ tcc_error_noabort("object must contain only one symtab");
fail:
ret = -1;
goto the_end;
}
nb_syms = sh->sh_size / sizeof(ElfW(Sym));
- symtab = load_data(S, fd, file_offset + sh->sh_offset, sh->sh_size);
+ symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
sm_table[i].s = symtab_section;
/* now load strtab */
sh = &shdr[sh->sh_link];
- strtab = load_data(S, fd, file_offset + sh->sh_offset, sh->sh_size);
+ strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
}
if (sh->sh_flags & SHF_COMPRESSED)
seencompressed = 1;
@@ -2962,8 +2965,8 @@ ST_FUNC int tcc_load_object_file(TCCState *S,
if (sh->sh_addralign < 1)
sh->sh_addralign = 1;
/* find corresponding section, if any */
- for(j = 1; j < S->nb_sections;j++) {
- s = S->sections[j];
+ for(j = 1; j < s1->nb_sections;j++) {
+ s = s1->sections[j];
if (!strcmp(s->name, sh_name)) {
if (!strncmp(sh_name, ".gnu.linkonce",
sizeof(".gnu.linkonce") - 1)) {
@@ -2984,7 +2987,7 @@ ST_FUNC int tcc_load_object_file(TCCState *S,
}
}
/* not found: create new section */
- s = new_section(S, sh_name, sh->sh_type, sh->sh_flags & ~SHF_GROUP);
+ s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags & ~SHF_GROUP);
/* take as much info as possible from the section. sh_link and
sh_info will be updated later */
s->sh_addralign = sh->sh_addralign;
@@ -2996,7 +2999,7 @@ ST_FUNC int tcc_load_object_file(TCCState *S,
if (strcmp (s->name, ".eh_frame"))
#endif
{
- tcc_error_noabort(S, "invalid section type");
+ tcc_error_noabort("invalid section type");
goto fail;
}
}
@@ -3011,7 +3014,7 @@ ST_FUNC int tcc_load_object_file(TCCState *S,
if (sh->sh_type != SHT_NOBITS) {
unsigned char *ptr;
lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
- ptr = section_ptr_add(S, s, size);
+ ptr = section_ptr_add(s, size);
full_read(fd, ptr, size);
} else {
s->data_offset += size;
@@ -3046,12 +3049,12 @@ ST_FUNC int tcc_load_object_file(TCCState *S,
if (sh->sh_type == SHT_RELX) {
s->sh_info = sm_table[sh->sh_info].s->sh_num;
/* update backward link */
- S->sections[s->sh_info]->reloc = s;
+ s1->sections[s->sh_info]->reloc = s;
}
}
/* resolve symbols */
- old_to_new_syms = tcc_mallocz(S, nb_syms * sizeof(int));
+ old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int));
sym = symtab + 1;
for(i = 1; i < nb_syms; i++, sym++) {
@@ -3120,7 +3123,7 @@ ST_FUNC int tcc_load_object_file(TCCState *S,
#endif
) {
invalid_reloc:
- tcc_error_noabort(S, "Invalid relocation entry [%2d] '%s' @ %.8x",
+ tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
i, strsec + sh->sh_name, (int)rel->r_offset);
goto fail;
}
@@ -3137,7 +3140,7 @@ ST_FUNC int tcc_load_object_file(TCCState *S,
switch to ARM mode. We set bit plt_thumb_stub of the
attribute of a symbol to indicate such a case. */
if (type == R_ARM_THM_JUMP24)
- get_sym_attr(S, sym_index, 1)->plt_thumb_stub = 1;
+ get_sym_attr(s1, sym_index, 1)->plt_thumb_stub = 1;
#endif
}
break;
@@ -3148,12 +3151,12 @@ ST_FUNC int tcc_load_object_file(TCCState *S,
ret = 0;
the_end:
- tcc_free(S, symtab);
- tcc_free(S, strtab);
- tcc_free(S, old_to_new_syms);
- tcc_free(S, sm_table);
- tcc_free(S, strsec);
- tcc_free(S, shdr);
+ tcc_free(symtab);
+ tcc_free(strtab);
+ tcc_free(old_to_new_syms);
+ tcc_free(sm_table);
+ tcc_free(strsec);
+ tcc_free(shdr);
return ret;
}
@@ -3194,7 +3197,7 @@ static int read_ar_header(int fd, int offset, ArchiveHeader *hdr)
}
/* load only the objects which resolve undefined symbols */
-static int tcc_load_alacarte(TCCState *S, int fd, int size, int entrysize)
+static int tcc_load_alacarte(TCCState *s1, int fd, int size, int entrysize)
{
int i, bound, nsyms, sym_index, len, ret = -1;
unsigned long long off;
@@ -3204,7 +3207,7 @@ static int tcc_load_alacarte(TCCState *S, int fd, int size, int entrysize)
ElfW(Sym) *sym;
ArchiveHeader hdr;
- data = tcc_malloc(S, size);
+ data = tcc_malloc(size);
if (full_read(fd, data, size) != size)
goto the_end;
nsyms = get_be(data, entrysize);
@@ -3224,25 +3227,25 @@ static int tcc_load_alacarte(TCCState *S, int fd, int size, int entrysize)
off = get_be(ar_index + i * entrysize, entrysize);
len = read_ar_header(fd, off, &hdr);
if (len <= 0 || memcmp(hdr.ar_fmag, ARFMAG, 2)) {
- tcc_error_noabort(S, "invalid archive");
+ tcc_error_noabort("invalid archive");
goto the_end;
}
off += len;
- if (S->verbose == 2)
+ if (s1->verbose == 2)
printf(" -> %s\n", hdr.ar_name);
- if (tcc_load_object_file(S, fd, off) < 0)
+ if (tcc_load_object_file(s1, fd, off) < 0)
goto the_end;
++bound;
}
} while(bound);
ret = 0;
the_end:
- tcc_free(S, data);
+ tcc_free(data);
return ret;
}
/* load a '.a' file */
-ST_FUNC int tcc_load_archive(TCCState *S, int fd, int alacarte)
+ST_FUNC int tcc_load_archive(TCCState *s1, int fd, int alacarte)
{
ArchiveHeader hdr;
/* char magic[8]; */
@@ -3259,7 +3262,7 @@ ST_FUNC int tcc_load_archive(TCCState *S, int fd, int alacarte)
if (len == 0)
return 0;
if (len < 0) {
- tcc_error_noabort(S, "invalid archive");
+ tcc_error_noabort("invalid archive");
return -1;
}
file_offset += len;
@@ -3269,13 +3272,13 @@ ST_FUNC int tcc_load_archive(TCCState *S, int fd, int alacarte)
if (alacarte) {
/* coff symbol table : we handle it */
if (!strcmp(hdr.ar_name, "/"))
- return tcc_load_alacarte(S, fd, size, 4);
+ return tcc_load_alacarte(s1, fd, size, 4);
if (!strcmp(hdr.ar_name, "/SYM64/"))
- return tcc_load_alacarte(S, fd, size, 8);
+ return tcc_load_alacarte(s1, fd, size, 8);
} else if (tcc_object_type(fd, &ehdr) == AFF_BINTYPE_REL) {
- if (S->verbose == 2)
+ if (s1->verbose == 2)
printf(" -> %s\n", hdr.ar_name);
- if (tcc_load_object_file(S, fd, file_offset) < 0)
+ if (tcc_load_object_file(s1, fd, file_offset) < 0)
return -1;
}
file_offset += size;
@@ -3285,10 +3288,10 @@ ST_FUNC int tcc_load_archive(TCCState *S, int fd, int alacarte)
#ifndef ELF_OBJ_ONLY
/* Set LV[I] to the global index of sym-version (LIB,VERSION). Maybe resizes
LV, maybe create a new entry for (LIB,VERSION). */
-static void set_ver_to_ver(TCCState *S, int *n, int **lv, int i, char *lib, char *version)
+static void set_ver_to_ver(TCCState *s1, int *n, int **lv, int i, char *lib, char *version)
{
while (i >= *n) {
- *lv = tcc_realloc(S, *lv, (*n + 1) * sizeof(**lv));
+ *lv = tcc_realloc(*lv, (*n + 1) * sizeof(**lv));
(*lv)[(*n)++] = -1;
}
if ((*lv)[i] == -1) {
@@ -3301,10 +3304,10 @@ static void set_ver_to_ver(TCCState *S, int *n, int **lv, int i, char *lib, char
break;
}
if (v == nb_sym_versions) {
- sym_versions = tcc_realloc (S, sym_versions,
+ sym_versions = tcc_realloc (sym_versions,
(v + 1) * sizeof(*sym_versions));
- sym_versions[v].lib = tcc_strdup(S, lib);
- sym_versions[v].version = tcc_strdup(S, version);
+ sym_versions[v].lib = tcc_strdup(lib);
+ sym_versions[v].version = tcc_strdup(version);
sym_versions[v].out_index = 0;
sym_versions[v].prev_same_lib = prev_same_lib;
nb_sym_versions++;
@@ -3316,11 +3319,11 @@ static void set_ver_to_ver(TCCState *S, int *n, int **lv, int i, char *lib, char
/* Associates symbol SYM_INDEX (in dynsymtab) with sym-version index
VERNDX. */
static void
-set_sym_version(TCCState *S, int sym_index, int verndx)
+set_sym_version(TCCState *s1, int sym_index, int verndx)
{
if (sym_index >= nb_sym_to_version) {
int newelems = sym_index ? sym_index * 2 : 1;
- sym_to_version = tcc_realloc(S, sym_to_version,
+ sym_to_version = tcc_realloc(sym_to_version,
newelems * sizeof(*sym_to_version));
memset(sym_to_version + nb_sym_to_version, -1,
(newelems - nb_sym_to_version) * sizeof(*sym_to_version));
@@ -3339,7 +3342,7 @@ struct versym_info {
};
-static void store_version(TCCState *S, struct versym_info *v, char *dynstr)
+static void store_version(TCCState *s1, struct versym_info *v, char *dynstr)
{
char *lib, *version;
uint32_t next;
@@ -3365,7 +3368,7 @@ static void store_version(TCCState *S, struct versym_info *v, char *dynstr)
if (lib == NULL)
lib = version;
else
- set_ver_to_ver(S, &v->nb_local_ver, &v->local_ver, vdef->vd_ndx,
+ set_ver_to_ver(s1, &v->nb_local_ver, &v->local_ver, vdef->vd_ndx,
lib, version);
#if DEBUG_VERSION
printf (" verdaux(%u): %s\n", vdef->vd_ndx, version);
@@ -3388,7 +3391,7 @@ static void store_version(TCCState *S, struct versym_info *v, char *dynstr)
for (i = 0; i < vneed->vn_cnt; i++) {
if ((vernaux->vna_other & 0x8000) == 0) { /* hidden */
version = dynstr + vernaux->vna_name;
- set_ver_to_ver(S, &v->nb_local_ver, &v->local_ver, vernaux->vna_other,
+ set_ver_to_ver(s1, &v->nb_local_ver, &v->local_ver, vernaux->vna_other,
lib, version);
#if DEBUG_VERSION
printf (" vernaux(%u): %u %u %s\n",
@@ -3417,7 +3420,7 @@ static void store_version(TCCState *S, struct versym_info *v, char *dynstr)
/* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
is referenced by the user (so it should be added as DT_NEEDED in
the generated ELF file) */
-ST_FUNC int tcc_load_dll(TCCState *S, int fd, const char *filename, int level)
+ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
{
ElfW(Ehdr) ehdr;
ElfW(Shdr) *shdr, *sh, *sh1;
@@ -3436,12 +3439,12 @@ ST_FUNC int tcc_load_dll(TCCState *S, int fd, const char *filename, int level)
/* test CPU specific stuff */
if (ehdr.e_ident[5] != ELFDATA2LSB ||
ehdr.e_machine != EM_TCC_TARGET) {
- tcc_error_noabort(S, "bad architecture");
+ tcc_error_noabort("bad architecture");
return -1;
}
/* read sections */
- shdr = load_data(S, fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum);
+ shdr = load_data(fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum);
/* load dynamic section and dynamic symbols */
nb_syms = 0;
@@ -3455,23 +3458,23 @@ ST_FUNC int tcc_load_dll(TCCState *S, int fd, const char *filename, int level)
switch(sh->sh_type) {
case SHT_DYNAMIC:
nb_dts = sh->sh_size / sizeof(ElfW(Dyn));
- dynamic = load_data(S, fd, sh->sh_offset, sh->sh_size);
+ dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
break;
case SHT_DYNSYM:
nb_syms = sh->sh_size / sizeof(ElfW(Sym));
- dynsym = load_data(S, fd, sh->sh_offset, sh->sh_size);
+ dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
sh1 = &shdr[sh->sh_link];
- dynstr = load_data(S, fd, sh1->sh_offset, sh1->sh_size);
+ dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size);
break;
case SHT_GNU_verdef:
- v.verdef = load_data(S, fd, sh->sh_offset, sh->sh_size);
+ v.verdef = load_data(fd, sh->sh_offset, sh->sh_size);
break;
case SHT_GNU_verneed:
- v.verneed = load_data(S, fd, sh->sh_offset, sh->sh_size);
+ v.verneed = load_data(fd, sh->sh_offset, sh->sh_size);
break;
case SHT_GNU_versym:
v.nb_versyms = sh->sh_size / sizeof(ElfW(Half));
- v.versym = load_data(S, fd, sh->sh_offset, sh->sh_size);
+ v.versym = load_data(fd, sh->sh_offset, sh->sh_size);
break;
default:
break;
@@ -3488,8 +3491,8 @@ ST_FUNC int tcc_load_dll(TCCState *S, int fd, const char *filename, int level)
}
/* if the dll is already loaded, do not load it */
- for(i = 0; i < S->nb_loaded_dlls; i++) {
- dllref = S->loaded_dlls[i];
+ for(i = 0; i < s1->nb_loaded_dlls; i++) {
+ dllref = s1->loaded_dlls[i];
if (!strcmp(soname, dllref->name)) {
/* but update level if needed */
if (level < dllref->level)
@@ -3500,12 +3503,12 @@ ST_FUNC int tcc_load_dll(TCCState *S, int fd, const char *filename, int level)
}
if (v.nb_versyms != nb_syms)
- tcc_free (S, v.versym), v.versym = NULL;
+ tcc_free (v.versym), v.versym = NULL;
else
- store_version(S, &v, dynstr);
+ store_version(s1, &v, dynstr);
/* add the dll and its level */
- tcc_add_dllref(S, soname)->level = level;
+ tcc_add_dllref(s1, soname)->level = level;
/* add dynamic symbols in dynsym_section */
for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
@@ -3513,12 +3516,12 @@ ST_FUNC int tcc_load_dll(TCCState *S, int fd, const char *filename, int level)
if (sym_bind == STB_LOCAL)
continue;
name = dynstr + sym->st_name;
- sym_index = set_elf_sym(S->dynsymtab_section, sym->st_value, sym->st_size,
+ sym_index = set_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
sym->st_info, sym->st_other, sym->st_shndx, name);
if (v.versym) {
ElfW(Half) vsym = v.versym[i];
if ((vsym & 0x8000) == 0 && vsym > 0 && vsym < v.nb_local_ver)
- set_sym_version(S, sym_index, v.local_ver[vsym]);
+ set_sym_version(s1, sym_index, v.local_ver[vsym]);
}
}
@@ -3527,13 +3530,13 @@ ST_FUNC int tcc_load_dll(TCCState *S, int fd, const char *filename, int level)
switch(dt->d_tag) {
case DT_NEEDED:
name = dynstr + dt->d_un.d_val;
- for(j = 0; j < S->nb_loaded_dlls; j++) {
- dllref = S->loaded_dlls[j];
+ for(j = 0; j < s1->nb_loaded_dlls; j++) {
+ dllref = s1->loaded_dlls[j];
if (!strcmp(name, dllref->name))
goto already_loaded;
}
- if (tcc_add_dll(S, name, AFF_REFERENCED_DLL) < 0) {
- tcc_error_noabort(S, "referenced dll '%s' not found", name);
+ if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
+ tcc_error_noabort("referenced dll '%s' not found", name);
ret = -1;
goto the_end;
}
@@ -3543,41 +3546,41 @@ ST_FUNC int tcc_load_dll(TCCState *S, int fd, const char *filename, int level)
}
ret = 0;
the_end:
- tcc_free(S, dynstr);
- tcc_free(S, dynsym);
- tcc_free(S, dynamic);
- tcc_free(S, shdr);
- tcc_free(S, v.local_ver);
- tcc_free(S, v.verdef);
- tcc_free(S, v.verneed);
- tcc_free(S, v.versym);
+ tcc_free(dynstr);
+ tcc_free(dynsym);
+ tcc_free(dynamic);
+ tcc_free(shdr);
+ tcc_free(v.local_ver);
+ tcc_free(v.verdef);
+ tcc_free(v.verneed);
+ tcc_free(v.versym);
return ret;
}
#define LD_TOK_NAME 256
#define LD_TOK_EOF (-1)
-static int ld_inp(TCCState *S)
+static int ld_inp(TCCState *s1)
{
char b;
- if (S->cc != -1) {
- int c = S->cc;
- S->cc = -1;
+ if (s1->cc != -1) {
+ int c = s1->cc;
+ s1->cc = -1;
return c;
}
- if (1 == read(S->fd, &b, 1))
+ if (1 == read(s1->fd, &b, 1))
return b;
return CH_EOF;
}
/* return next ld script token */
-static int ld_next(TCCState *S, char *name, int name_size)
+static int ld_next(TCCState *s1, char *name, int name_size)
{
int c, d, ch;
char *q;
redo:
- ch = ld_inp(S);
+ ch = ld_inp(s1);
switch(ch) {
case ' ':
case '\t':
@@ -3587,10 +3590,10 @@ static int ld_next(TCCState *S, char *name, int name_size)
case '\n':
goto redo;
case '/':
- ch = ld_inp(S);
+ ch = ld_inp(s1);
if (ch == '*') { /* comment */
for (d = 0;; d = ch) {
- ch = ld_inp(S);
+ ch = ld_inp(s1);
if (ch == CH_EOF || (ch == '/' && d == '*'))
break;
}
@@ -3671,9 +3674,9 @@ static int ld_next(TCCState *S, char *name, int name_size)
if ((q - name) < name_size - 1) {
*q++ = ch;
}
- ch = ld_inp(S);
+ ch = ld_inp(s1);
}
- S->cc = ch;
+ s1->cc = ch;
*q = '\0';
c = LD_TOK_NAME;
break;
@@ -3687,18 +3690,18 @@ static int ld_next(TCCState *S, char *name, int name_size)
return c;
}
-static int ld_add_file(TCCState *S, const char filename[])
+static int ld_add_file(TCCState *s1, const char filename[])
{
if (filename[0] == '/') {
if (CONFIG_SYSROOT[0] == '\0'
- && tcc_add_file_internal(S, filename, AFF_TYPE_BIN) == 0)
+ && tcc_add_file_internal(s1, filename, AFF_TYPE_BIN) == 0)
return 0;
filename = tcc_basename(filename);
}
- return tcc_add_dll(S, filename, 0);
+ return tcc_add_dll(s1, filename, 0);
}
-static int ld_add_file_list(TCCState *S, const char *cmd, int as_needed)
+static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed)
{
char filename[1024], libname[1024];
int t, group, nblibs = 0, ret = 0;
@@ -3706,109 +3709,109 @@ static int ld_add_file_list(TCCState *S, const char *cmd, int as_needed)
group = !strcmp(cmd, "GROUP");
if (!as_needed)
- S->new_undef_sym = 0;
- t = ld_next(S, filename, sizeof(filename));
+ s1->new_undef_sym = 0;
+ t = ld_next(s1, filename, sizeof(filename));
if (t != '(') {
- tcc_error_noabort(S, "( expected");
+ tcc_error_noabort("( expected");
ret = -1;
goto lib_parse_error;
}
- t = ld_next(S, filename, sizeof(filename));
+ t = ld_next(s1, filename, sizeof(filename));
for(;;) {
libname[0] = '\0';
if (t == LD_TOK_EOF) {
- tcc_error_noabort(S, "unexpected end of file");
+ tcc_error_noabort("unexpected end of file");
ret = -1;
goto lib_parse_error;
} else if (t == ')') {
break;
} else if (t == '-') {
- t = ld_next(S, filename, sizeof(filename));
+ t = ld_next(s1, filename, sizeof(filename));
if ((t != LD_TOK_NAME) || (filename[0] != 'l')) {
- tcc_error_noabort(S, "library name expected");
+ tcc_error_noabort("library name expected");
ret = -1;
goto lib_parse_error;
}
pstrcpy(libname, sizeof libname, &filename[1]);
- if (S->static_link) {
+ if (s1->static_link) {
snprintf(filename, sizeof filename, "lib%s.a", libname);
} else {
snprintf(filename, sizeof filename, "lib%s.so", libname);
}
} else if (t != LD_TOK_NAME) {
- tcc_error_noabort(S, "filename expected");
+ tcc_error_noabort("filename expected");
ret = -1;
goto lib_parse_error;
}
if (!strcmp(filename, "AS_NEEDED")) {
- ret = ld_add_file_list(S, cmd, 1);
+ ret = ld_add_file_list(s1, cmd, 1);
if (ret)
goto lib_parse_error;
} else {
/* TODO: Implement AS_NEEDED support. Ignore it for now */
if (!as_needed) {
- ret = ld_add_file(S, filename);
+ ret = ld_add_file(s1, filename);
if (ret)
goto lib_parse_error;
if (group) {
/* Add the filename *and* the libname to avoid future conversions */
- dynarray_add(S, &libs, &nblibs, tcc_strdup(S, filename));
+ dynarray_add(&libs, &nblibs, tcc_strdup(filename));
if (libname[0] != '\0')
- dynarray_add(S, &libs, &nblibs, tcc_strdup(S, libname));
+ dynarray_add(&libs, &nblibs, tcc_strdup(libname));
}
}
}
- t = ld_next(S, filename, sizeof(filename));
+ t = ld_next(s1, filename, sizeof(filename));
if (t == ',') {
- t = ld_next(S, filename, sizeof(filename));
+ t = ld_next(s1, filename, sizeof(filename));
}
}
if (group && !as_needed) {
- while (S->new_undef_sym) {
+ while (s1->new_undef_sym) {
int i;
- S->new_undef_sym = 0;
+ s1->new_undef_sym = 0;
for (i = 0; i < nblibs; i ++)
- ld_add_file(S, libs[i]);
+ ld_add_file(s1, libs[i]);
}
}
lib_parse_error:
- dynarray_reset(S, &libs, &nblibs);
+ dynarray_reset(&libs, &nblibs);
return ret;
}
/* interpret a subset of GNU ldscripts to handle the dummy libc.so
files */
-ST_FUNC int tcc_load_ldscript(TCCState *S, int fd)
+ST_FUNC int tcc_load_ldscript(TCCState *s1, int fd)
{
char cmd[64];
char filename[1024];
int t, ret;
- S->fd = fd;
- S->cc = -1;
+ s1->fd = fd;
+ s1->cc = -1;
for(;;) {
- t = ld_next(S, cmd, sizeof(cmd));
+ t = ld_next(s1, cmd, sizeof(cmd));
if (t == LD_TOK_EOF)
return 0;
else if (t != LD_TOK_NAME)
return -1;
if (!strcmp(cmd, "INPUT") ||
!strcmp(cmd, "GROUP")) {
- ret = ld_add_file_list(S, cmd, 0);
+ ret = ld_add_file_list(s1, cmd, 0);
if (ret)
return ret;
} else if (!strcmp(cmd, "OUTPUT_FORMAT") ||
!strcmp(cmd, "TARGET")) {
/* ignore some commands */
- t = ld_next(S, cmd, sizeof(cmd));
+ t = ld_next(s1, cmd, sizeof(cmd));
if (t != '(') {
- tcc_error_noabort(S, "( expected");
+ tcc_error_noabort("( expected");
return -1;
}
for(;;) {
- t = ld_next(S, filename, sizeof(filename));
+ t = ld_next(s1, filename, sizeof(filename));
if (t == LD_TOK_EOF) {
- tcc_error_noabort(S, "unexpected end of file");
+ tcc_error_noabort("unexpected end of file");
return -1;
} else if (t == ')') {
break;