Introduce ElfW macro and ELFW to encapsulate the difference between Elf32_* and Elf64_*. Also, introduce ElfW_Rel and SHT_RELX for difference between REL and RELA.

This commit is contained in:
Shinichiro Hamaji 2008-11-30 08:14:07 +09:00 committed by grischka
parent 76b02c2a03
commit 7dd792ef51
3 changed files with 198 additions and 177 deletions

10
elf.h
View File

@ -1630,4 +1630,14 @@ typedef Elf32_Addr Elf32_Conflict;
#define R_C60HI16 0x55 // high 16 bit MVKH embedded #define R_C60HI16 0x55 // high 16 bit MVKH embedded
#define R_C60LO16 0x54 // low 16 bit MVKL embedded #define R_C60LO16 0x54 // low 16 bit MVKL embedded
#ifdef TCC_TARGET_X86_64
#define TCC_ELFCLASS ELFCLASS64
#define ElfW(type) Elf##64##_##type
#define ELFW(type) ELF##64##_##type
#else
#define TCC_ELFCLASS ELFCLASS32
#define ElfW(type) Elf##32##_##type
#define ELFW(type) ELF##32##_##type
#endif
#endif /* elf.h */ #endif /* elf.h */

29
tcc.c
View File

@ -1261,6 +1261,7 @@ Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
switch(sh_type) { switch(sh_type) {
case SHT_HASH: case SHT_HASH:
case SHT_REL: case SHT_REL:
case SHT_RELA:
case SHT_DYNSYM: case SHT_DYNSYM:
case SHT_SYMTAB: case SHT_SYMTAB:
case SHT_DYNAMIC: case SHT_DYNAMIC:
@ -1349,7 +1350,7 @@ static void put_extern_sym2(Sym *sym, Section *section,
int can_add_underscore) int can_add_underscore)
{ {
int sym_type, sym_bind, sh_num, info, other, attr; int sym_type, sym_bind, sh_num, info, other, attr;
Elf32_Sym *esym; ElfW(Sym) *esym;
const char *name; const char *name;
char buf1[256]; char buf1[256];
@ -1424,10 +1425,10 @@ static void put_extern_sym2(Sym *sym, Section *section,
pstrcpy(buf1 + 1, sizeof(buf1) - 1, name); pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
name = buf1; name = buf1;
} }
info = ELF32_ST_INFO(sym_bind, sym_type); info = ELFW(ST_INFO)(sym_bind, sym_type);
sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name); sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
} else { } else {
esym = &((Elf32_Sym *)symtab_section->data)[sym->c]; esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
esym->st_value = value; esym->st_value = value;
esym->st_size = size; esym->st_size = size;
esym->st_shndx = sh_num; esym->st_shndx = sh_num;
@ -9140,11 +9141,11 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
if (sec) { if (sec) {
put_extern_sym(sym, sec, addr, size); put_extern_sym(sym, sec, addr, size);
} else { } else {
Elf32_Sym *esym; ElfW(Sym) *esym;
/* put a common area */ /* put a common area */
put_extern_sym(sym, NULL, align, size); put_extern_sym(sym, NULL, align, size);
/* XXX: find a nicer way */ /* XXX: find a nicer way */
esym = &((Elf32_Sym *)symtab_section->data)[sym->c]; esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
esym->st_shndx = SHN_COMMON; esym->st_shndx = SHN_COMMON;
} }
} else { } else {
@ -9270,7 +9271,7 @@ static void gen_function(Sym *sym)
sym_pop(&local_stack, NULL); /* reset local stack */ sym_pop(&local_stack, NULL); /* reset local stack */
/* end of function */ /* end of function */
/* patch symbol size */ /* patch symbol size */
((Elf32_Sym *)symtab_section->data)[sym->c].st_size = ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
ind - func_ind; ind - func_ind;
if (do_debug) { if (do_debug) {
put_stabn(N_FUN, 0, 0, ind - func_ind); put_stabn(N_FUN, 0, 0, ind - func_ind);
@ -9552,7 +9553,7 @@ static int tcc_compile(TCCState *s1)
section_sym = 0; /* avoid warning */ section_sym = 0; /* avoid warning */
if (do_debug) { if (do_debug) {
section_sym = put_elf_sym(symtab_section, 0, 0, section_sym = put_elf_sym(symtab_section, 0, 0,
ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0, ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
text_section->sh_num, NULL); text_section->sh_num, NULL);
getcwd(buf, sizeof(buf)); getcwd(buf, sizeof(buf));
#ifdef _WIN32 #ifdef _WIN32
@ -9567,7 +9568,7 @@ static int tcc_compile(TCCState *s1)
/* an elf symbol of type STT_FILE must be put so that STB_LOCAL /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
symbols can be safely used */ symbols can be safely used */
put_elf_sym(symtab_section, 0, 0, put_elf_sym(symtab_section, 0, 0,
ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0, ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
SHN_ABS, file->filename); SHN_ABS, file->filename);
/* define some often used types */ /* define some often used types */
@ -9872,14 +9873,14 @@ static void rt_printline(unsigned long wanted_pc)
/* second pass: we try symtab symbols (no line number info) */ /* second pass: we try symtab symbols (no line number info) */
incl_index = 0; incl_index = 0;
{ {
Elf32_Sym *sym, *sym_end; ElfW(Sym) *sym, *sym_end;
int type; int type;
sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset); sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
for(sym = (Elf32_Sym *)symtab_section->data + 1; for(sym = (ElfW(Sym) *)symtab_section->data + 1;
sym < sym_end; sym < sym_end;
sym++) { sym++) {
type = ELF32_ST_TYPE(sym->st_info); type = ELFW(ST_TYPE)(sym->st_info);
if (type == STT_FUNC) { if (type == STT_FUNC) {
if (wanted_pc >= sym->st_value && if (wanted_pc >= sym->st_value &&
wanted_pc < sym->st_value + sym->st_size) { wanted_pc < sym->st_value + sym->st_size) {
@ -10329,7 +10330,7 @@ int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
{ {
const char *ext; const char *ext;
Elf32_Ehdr ehdr; ElfW(Ehdr) ehdr;
int fd, ret; int fd, ret;
BufferedFile *saved_file; BufferedFile *saved_file;
@ -10502,7 +10503,7 @@ int tcc_add_library(TCCState *s, const char *libraryname)
int tcc_add_symbol(TCCState *s, const char *name, unsigned long val) int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
{ {
add_elf_sym(symtab_section, val, 0, add_elf_sym(symtab_section, val, 0,
ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
SHN_ABS, name); SHN_ABS, name);
return 0; return 0;
} }

336
tccelf.c
View File

@ -18,6 +18,16 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#ifdef TCC_TARGET_X86_64
#define ElfW_Rel ElfW(Rela)
#define SHT_RELX SHT_RELA
#define REL_SECTION_FMT ".rela%s"
#else
#define ElfW_Rel ElfW(Rel)
#define SHT_RELX SHT_REL
#define REL_SECTION_FMT ".rel%s"
#endif
static int put_elf_str(Section *s, const char *sym) static int put_elf_str(Section *s, const char *sym)
{ {
int offset, len; int offset, len;
@ -49,12 +59,12 @@ static unsigned long elf_hash(const unsigned char *name)
/* NOTE: we do factorize the hash table code to go faster */ /* NOTE: we do factorize the hash table code to go faster */
static void rebuild_hash(Section *s, unsigned int nb_buckets) static void rebuild_hash(Section *s, unsigned int nb_buckets)
{ {
Elf32_Sym *sym; ElfW(Sym) *sym;
int *ptr, *hash, nb_syms, sym_index, h; int *ptr, *hash, nb_syms, sym_index, h;
char *strtab; char *strtab;
strtab = s->link->data; strtab = s->link->data;
nb_syms = s->data_offset / sizeof(Elf32_Sym); nb_syms = s->data_offset / sizeof(ElfW(Sym));
s->hash->data_offset = 0; s->hash->data_offset = 0;
ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int)); ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int));
@ -65,9 +75,9 @@ static void rebuild_hash(Section *s, unsigned int nb_buckets)
memset(hash, 0, (nb_buckets + 1) * sizeof(int)); memset(hash, 0, (nb_buckets + 1) * sizeof(int));
ptr += nb_buckets + 1; ptr += nb_buckets + 1;
sym = (Elf32_Sym *)s->data + 1; sym = (ElfW(Sym) *)s->data + 1;
for(sym_index = 1; sym_index < nb_syms; sym_index++) { for(sym_index = 1; sym_index < nb_syms; sym_index++) {
if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) { if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
h = elf_hash(strtab + sym->st_name) % nb_buckets; h = elf_hash(strtab + sym->st_name) % nb_buckets;
*ptr = hash[h]; *ptr = hash[h];
hash[h] = sym_index; hash[h] = sym_index;
@ -86,10 +96,10 @@ static int put_elf_sym(Section *s,
{ {
int name_offset, sym_index; int name_offset, sym_index;
int nbuckets, h; int nbuckets, h;
Elf32_Sym *sym; ElfW(Sym) *sym;
Section *hs; Section *hs;
sym = section_ptr_add(s, sizeof(Elf32_Sym)); sym = section_ptr_add(s, sizeof(ElfW(Sym)));
if (name) if (name)
name_offset = put_elf_str(s->link, name); name_offset = put_elf_str(s->link, name);
else else
@ -101,14 +111,14 @@ static int put_elf_sym(Section *s,
sym->st_info = info; sym->st_info = info;
sym->st_other = other; sym->st_other = other;
sym->st_shndx = shndx; sym->st_shndx = shndx;
sym_index = sym - (Elf32_Sym *)s->data; sym_index = sym - (ElfW(Sym) *)s->data;
hs = s->hash; hs = s->hash;
if (hs) { if (hs) {
int *ptr, *base; int *ptr, *base;
ptr = section_ptr_add(hs, sizeof(int)); ptr = section_ptr_add(hs, sizeof(int));
base = (int *)hs->data; base = (int *)hs->data;
/* only add global or weak symbols */ /* only add global or weak symbols */
if (ELF32_ST_BIND(info) != STB_LOCAL) { if (ELFW(ST_BIND)(info) != STB_LOCAL) {
/* add another hashing entry */ /* add another hashing entry */
nbuckets = base[0]; nbuckets = base[0];
h = elf_hash(name) % nbuckets; h = elf_hash(name) % nbuckets;
@ -132,7 +142,7 @@ static int put_elf_sym(Section *s,
found. */ found. */
static int find_elf_sym(Section *s, const char *name) static int find_elf_sym(Section *s, const char *name)
{ {
Elf32_Sym *sym; ElfW(Sym) *sym;
Section *hs; Section *hs;
int nbuckets, sym_index, h; int nbuckets, sym_index, h;
const char *name1; const char *name1;
@ -144,7 +154,7 @@ static int find_elf_sym(Section *s, const char *name)
h = elf_hash(name) % nbuckets; h = elf_hash(name) % nbuckets;
sym_index = ((int *)hs->data)[2 + h]; sym_index = ((int *)hs->data)[2 + h];
while (sym_index != 0) { while (sym_index != 0) {
sym = &((Elf32_Sym *)s->data)[sym_index]; sym = &((ElfW(Sym) *)s->data)[sym_index];
name1 = s->link->data + sym->st_name; name1 = s->link->data + sym->st_name;
if (!strcmp(name, name1)) if (!strcmp(name, name1))
return sym_index; return sym_index;
@ -157,12 +167,12 @@ static int find_elf_sym(Section *s, const char *name)
int tcc_get_symbol(TCCState *s, unsigned long *pval, const char *name) int tcc_get_symbol(TCCState *s, unsigned long *pval, const char *name)
{ {
int sym_index; int sym_index;
Elf32_Sym *sym; ElfW(Sym) *sym;
sym_index = find_elf_sym(symtab_section, name); sym_index = find_elf_sym(symtab_section, name);
if (!sym_index) if (!sym_index)
return -1; return -1;
sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
*pval = sym->st_value; *pval = sym->st_value;
return 0; return 0;
} }
@ -180,25 +190,25 @@ void *tcc_get_symbol_err(TCCState *s, const char *name)
static int add_elf_sym(Section *s, unsigned long value, unsigned long size, static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
int info, int other, int sh_num, const char *name) int info, int other, int sh_num, const char *name)
{ {
Elf32_Sym *esym; ElfW(Sym) *esym;
int sym_bind, sym_index, sym_type, esym_bind; int sym_bind, sym_index, sym_type, esym_bind;
unsigned char sym_vis, esym_vis, new_vis; unsigned char sym_vis, esym_vis, new_vis;
sym_bind = ELF32_ST_BIND(info); sym_bind = ELFW(ST_BIND)(info);
sym_type = ELF32_ST_TYPE(info); sym_type = ELFW(ST_TYPE)(info);
sym_vis = ELF32_ST_VISIBILITY(other); sym_vis = ELFW(ST_VISIBILITY)(other);
if (sym_bind != STB_LOCAL) { if (sym_bind != STB_LOCAL) {
/* we search global or weak symbols */ /* we search global or weak symbols */
sym_index = find_elf_sym(s, name); sym_index = find_elf_sym(s, name);
if (!sym_index) if (!sym_index)
goto do_def; goto do_def;
esym = &((Elf32_Sym *)s->data)[sym_index]; esym = &((ElfW(Sym) *)s->data)[sym_index];
if (esym->st_shndx != SHN_UNDEF) { if (esym->st_shndx != SHN_UNDEF) {
esym_bind = ELF32_ST_BIND(esym->st_info); esym_bind = ELFW(ST_BIND)(esym->st_info);
/* propagate the most constraining visibility */ /* propagate the most constraining visibility */
/* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */ /* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
esym_vis = ELF32_ST_VISIBILITY(esym->st_other); esym_vis = ELFW(ST_VISIBILITY)(esym->st_other);
if (esym_vis == STV_DEFAULT) { if (esym_vis == STV_DEFAULT) {
new_vis = sym_vis; new_vis = sym_vis;
} else if (sym_vis == STV_DEFAULT) { } else if (sym_vis == STV_DEFAULT) {
@ -206,7 +216,7 @@ static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
} else { } else {
new_vis = (esym_vis < sym_vis) ? esym_vis : sym_vis; new_vis = (esym_vis < sym_vis) ? esym_vis : sym_vis;
} }
esym->st_other = (esym->st_other & ~ELF32_ST_VISIBILITY(-1)) esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
| new_vis; | new_vis;
other = esym->st_other; /* in case we have to patch esym */ other = esym->st_other; /* in case we have to patch esym */
if (sh_num == SHN_UNDEF) { if (sh_num == SHN_UNDEF) {
@ -234,7 +244,7 @@ static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
} }
} else { } else {
do_patch: do_patch:
esym->st_info = ELF32_ST_INFO(sym_bind, sym_type); esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
esym->st_shndx = sh_num; esym->st_shndx = sh_num;
esym->st_value = value; esym->st_value = value;
esym->st_size = size; esym->st_size = size;
@ -243,7 +253,7 @@ static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
} else { } else {
do_def: do_def:
sym_index = put_elf_sym(s, value, size, sym_index = put_elf_sym(s, value, size,
ELF32_ST_INFO(sym_bind, sym_type), other, ELFW(ST_INFO)(sym_bind, sym_type), other,
sh_num, name); sh_num, name);
} }
return sym_index; return sym_index;
@ -255,7 +265,7 @@ static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
{ {
char buf[256]; char buf[256];
Section *sr; Section *sr;
Elf32_Rel *rel; ElfW_Rel *rel;
sr = s->reloc; sr = s->reloc;
if (!sr) { if (!sr) {
@ -263,15 +273,15 @@ static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
snprintf(buf, sizeof(buf), ".rel%s", s->name); snprintf(buf, sizeof(buf), ".rel%s", s->name);
/* if the symtab is allocated, then we consider the relocation /* if the symtab is allocated, then we consider the relocation
are also */ are also */
sr = new_section(tcc_state, buf, SHT_REL, symtab->sh_flags); sr = new_section(tcc_state, buf, SHT_RELX, symtab->sh_flags);
sr->sh_entsize = sizeof(Elf32_Rel); sr->sh_entsize = sizeof(ElfW_Rel);
sr->link = symtab; sr->link = symtab;
sr->sh_info = s->sh_num; sr->sh_info = s->sh_num;
s->reloc = sr; s->reloc = sr;
} }
rel = section_ptr_add(sr, sizeof(Elf32_Rel)); rel = section_ptr_add(sr, sizeof(ElfW_Rel));
rel->r_offset = offset; rel->r_offset = offset;
rel->r_info = ELF32_R_INFO(symbol, type); rel->r_info = ELFW(R_INFO)(symbol, type);
} }
/* put stab debug information */ /* put stab debug information */
@ -327,22 +337,22 @@ static void put_stabd(int type, int other, int desc)
static void sort_syms(TCCState *s1, Section *s) static void sort_syms(TCCState *s1, Section *s)
{ {
int *old_to_new_syms; int *old_to_new_syms;
Elf32_Sym *new_syms; ElfW(Sym) *new_syms;
int nb_syms, i; int nb_syms, i;
Elf32_Sym *p, *q; ElfW(Sym) *p, *q;
Elf32_Rel *rel, *rel_end; ElfW_Rel *rel, *rel_end;
Section *sr; Section *sr;
int type, sym_index; int type, sym_index;
nb_syms = s->data_offset / sizeof(Elf32_Sym); nb_syms = s->data_offset / sizeof(ElfW(Sym));
new_syms = tcc_malloc(nb_syms * sizeof(Elf32_Sym)); new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym)));
old_to_new_syms = tcc_malloc(nb_syms * sizeof(int)); old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
/* first pass for local symbols */ /* first pass for local symbols */
p = (Elf32_Sym *)s->data; p = (ElfW(Sym) *)s->data;
q = new_syms; q = new_syms;
for(i = 0; i < nb_syms; i++) { for(i = 0; i < nb_syms; i++) {
if (ELF32_ST_BIND(p->st_info) == STB_LOCAL) { if (ELFW(ST_BIND)(p->st_info) == STB_LOCAL) {
old_to_new_syms[i] = q - new_syms; old_to_new_syms[i] = q - new_syms;
*q++ = *p; *q++ = *p;
} }
@ -352,9 +362,9 @@ static void sort_syms(TCCState *s1, Section *s)
s->sh_info = q - new_syms; s->sh_info = q - new_syms;
/* then second pass for non local symbols */ /* then second pass for non local symbols */
p = (Elf32_Sym *)s->data; p = (ElfW(Sym) *)s->data;
for(i = 0; i < nb_syms; i++) { for(i = 0; i < nb_syms; i++) {
if (ELF32_ST_BIND(p->st_info) != STB_LOCAL) { if (ELFW(ST_BIND)(p->st_info) != STB_LOCAL) {
old_to_new_syms[i] = q - new_syms; old_to_new_syms[i] = q - new_syms;
*q++ = *p; *q++ = *p;
} }
@ -362,21 +372,21 @@ static void sort_syms(TCCState *s1, Section *s)
} }
/* we copy the new symbols to the old */ /* we copy the new symbols to the old */
memcpy(s->data, new_syms, nb_syms * sizeof(Elf32_Sym)); memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));
tcc_free(new_syms); tcc_free(new_syms);
/* now we modify all the relocations */ /* now we modify all the relocations */
for(i = 1; i < s1->nb_sections; i++) { for(i = 1; i < s1->nb_sections; i++) {
sr = s1->sections[i]; sr = s1->sections[i];
if (sr->sh_type == SHT_REL && sr->link == s) { if (sr->sh_type == SHT_RELX && sr->link == s) {
rel_end = (Elf32_Rel *)(sr->data + sr->data_offset); rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
for(rel = (Elf32_Rel *)sr->data; for(rel = (ElfW_Rel *)sr->data;
rel < rel_end; rel < rel_end;
rel++) { rel++) {
sym_index = ELF32_R_SYM(rel->r_info); sym_index = ELFW(R_SYM)(rel->r_info);
type = ELF32_R_TYPE(rel->r_info); type = ELFW(R_TYPE)(rel->r_info);
sym_index = old_to_new_syms[sym_index]; sym_index = old_to_new_syms[sym_index];
rel->r_info = ELF32_R_INFO(sym_index, type); rel->r_info = ELFW(R_INFO)(sym_index, type);
} }
} }
} }
@ -387,11 +397,11 @@ static void sort_syms(TCCState *s1, Section *s)
/* relocate common symbols in the .bss section */ /* relocate common symbols in the .bss section */
static void relocate_common_syms(void) static void relocate_common_syms(void)
{ {
Elf32_Sym *sym, *sym_end; ElfW(Sym) *sym, *sym_end;
unsigned long offset, align; unsigned long offset, align;
sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset); sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
for(sym = (Elf32_Sym *)symtab_section->data + 1; for(sym = (ElfW(Sym) *)symtab_section->data + 1;
sym < sym_end; sym < sym_end;
sym++) { sym++) {
if (sym->st_shndx == SHN_COMMON) { if (sym->st_shndx == SHN_COMMON) {
@ -411,13 +421,13 @@ static void relocate_common_syms(void)
true and output error if undefined symbol. */ true and output error if undefined symbol. */
static void relocate_syms(TCCState *s1, int do_resolve) static void relocate_syms(TCCState *s1, int do_resolve)
{ {
Elf32_Sym *sym, *esym, *sym_end; ElfW(Sym) *sym, *esym, *sym_end;
int sym_bind, sh_num, sym_index; int sym_bind, sh_num, sym_index;
const char *name; const char *name;
unsigned long addr; unsigned long addr;
sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset); sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
for(sym = (Elf32_Sym *)symtab_section->data + 1; for(sym = (ElfW(Sym) *)symtab_section->data + 1;
sym < sym_end; sym < sym_end;
sym++) { sym++) {
sh_num = sym->st_shndx; sh_num = sym->st_shndx;
@ -425,7 +435,7 @@ static void relocate_syms(TCCState *s1, int do_resolve)
name = strtab_section->data + sym->st_name; name = strtab_section->data + sym->st_name;
if (do_resolve) { if (do_resolve) {
name = symtab_section->link->data + sym->st_name; name = symtab_section->link->data + sym->st_name;
addr = (unsigned long)resolve_sym(s1, name, ELF32_ST_TYPE(sym->st_info)); addr = (unsigned long)resolve_sym(s1, name, ELFW(ST_TYPE)(sym->st_info));
if (addr) { if (addr) {
sym->st_value = addr; sym->st_value = addr;
goto found; goto found;
@ -434,7 +444,7 @@ static void relocate_syms(TCCState *s1, int do_resolve)
/* if dynamic symbol exist, then use it */ /* if dynamic symbol exist, then use it */
sym_index = find_elf_sym(s1->dynsym, name); sym_index = find_elf_sym(s1->dynsym, name);
if (sym_index) { if (sym_index) {
esym = &((Elf32_Sym *)s1->dynsym->data)[sym_index]; esym = &((ElfW(Sym) *)s1->dynsym->data)[sym_index];
sym->st_value = esym->st_value; sym->st_value = esym->st_value;
goto found; goto found;
} }
@ -445,7 +455,7 @@ static void relocate_syms(TCCState *s1, int do_resolve)
goto found; goto found;
/* only weak symbols are accepted to be undefined. Their /* only weak symbols are accepted to be undefined. Their
value is zero */ value is zero */
sym_bind = ELF32_ST_BIND(sym->st_info); sym_bind = ELFW(ST_BIND)(sym->st_info);
if (sym_bind == STB_WEAK) { if (sym_bind == STB_WEAK) {
sym->st_value = 0; sym->st_value = 0;
} else { } else {
@ -463,8 +473,8 @@ static void relocate_syms(TCCState *s1, int do_resolve)
static void relocate_section(TCCState *s1, Section *s) static void relocate_section(TCCState *s1, Section *s)
{ {
Section *sr; Section *sr;
Elf32_Rel *rel, *rel_end, *qrel; ElfW_Rel *rel, *rel_end, *qrel;
Elf32_Sym *sym; ElfW(Sym) *sym;
int type, sym_index; int type, sym_index;
unsigned char *ptr; unsigned char *ptr;
unsigned long val, addr; unsigned long val, addr;
@ -473,17 +483,17 @@ static void relocate_section(TCCState *s1, Section *s)
#endif #endif
sr = s->reloc; sr = s->reloc;
rel_end = (Elf32_Rel *)(sr->data + sr->data_offset); rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
qrel = (Elf32_Rel *)sr->data; qrel = (ElfW_Rel *)sr->data;
for(rel = qrel; for(rel = qrel;
rel < rel_end; rel < rel_end;
rel++) { rel++) {
ptr = s->data + rel->r_offset; ptr = s->data + rel->r_offset;
sym_index = ELF32_R_SYM(rel->r_info); sym_index = ELFW(R_SYM)(rel->r_info);
sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
val = sym->st_value; val = sym->st_value;
type = ELF32_R_TYPE(rel->r_info); type = ELFW(R_TYPE)(rel->r_info);
addr = s->sh_addr + rel->r_offset; addr = s->sh_addr + rel->r_offset;
/* CPU specific */ /* CPU specific */
@ -494,11 +504,11 @@ static void relocate_section(TCCState *s1, Section *s)
esym_index = s1->symtab_to_dynsym[sym_index]; esym_index = s1->symtab_to_dynsym[sym_index];
qrel->r_offset = rel->r_offset; qrel->r_offset = rel->r_offset;
if (esym_index) { if (esym_index) {
qrel->r_info = ELF32_R_INFO(esym_index, R_386_32); qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
qrel++; qrel++;
break; break;
} else { } else {
qrel->r_info = ELF32_R_INFO(0, R_386_RELATIVE); qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
qrel++; qrel++;
} }
} }
@ -510,7 +520,7 @@ static void relocate_section(TCCState *s1, Section *s)
esym_index = s1->symtab_to_dynsym[sym_index]; esym_index = s1->symtab_to_dynsym[sym_index];
if (esym_index) { if (esym_index) {
qrel->r_offset = rel->r_offset; qrel->r_offset = rel->r_offset;
qrel->r_info = ELF32_R_INFO(esym_index, R_386_PC32); qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
qrel++; qrel++;
break; break;
} }
@ -624,11 +634,11 @@ static void relocate_section(TCCState *s1, Section *s)
static void relocate_rel(TCCState *s1, Section *sr) static void relocate_rel(TCCState *s1, Section *sr)
{ {
Section *s; Section *s;
Elf32_Rel *rel, *rel_end; ElfW_Rel *rel, *rel_end;
s = s1->sections[sr->sh_info]; s = s1->sections[sr->sh_info];
rel_end = (Elf32_Rel *)(sr->data + sr->data_offset); rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
for(rel = (Elf32_Rel *)sr->data; for(rel = (ElfW_Rel *)sr->data;
rel < rel_end; rel < rel_end;
rel++) { rel++) {
rel->r_offset += s->sh_addr; rel->r_offset += s->sh_addr;
@ -639,14 +649,14 @@ static void relocate_rel(TCCState *s1, Section *sr)
their space */ their space */
static int prepare_dynamic_rel(TCCState *s1, Section *sr) static int prepare_dynamic_rel(TCCState *s1, Section *sr)
{ {
Elf32_Rel *rel, *rel_end; ElfW_Rel *rel, *rel_end;
int sym_index, esym_index, type, count; int sym_index, esym_index, type, count;
count = 0; count = 0;
rel_end = (Elf32_Rel *)(sr->data + sr->data_offset); rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
for(rel = (Elf32_Rel *)sr->data; rel < rel_end; rel++) { for(rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++) {
sym_index = ELF32_R_SYM(rel->r_info); sym_index = ELFW(R_SYM)(rel->r_info);
type = ELF32_R_TYPE(rel->r_info); type = ELFW(R_TYPE)(rel->r_info);
switch(type) { switch(type) {
case R_386_32: case R_386_32:
count++; count++;
@ -663,7 +673,7 @@ static int prepare_dynamic_rel(TCCState *s1, Section *sr)
if (count) { if (count) {
/* allocate the section */ /* allocate the section */
sr->sh_flags |= SHF_ALLOC; sr->sh_flags |= SHF_ALLOC;
sr->sh_size = count * sizeof(Elf32_Rel); sr->sh_size = count * sizeof(ElfW_Rel);
} }
return count; return count;
} }
@ -712,7 +722,7 @@ static void build_got(TCCState *s1)
/* if no got, then create it */ /* if no got, then create it */
s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE); s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
s1->got->sh_entsize = 4; s1->got->sh_entsize = 4;
add_elf_sym(symtab_section, 0, 4, ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT), add_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_"); 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
ptr = section_ptr_add(s1->got, 3 * sizeof(int)); ptr = section_ptr_add(s1->got, 3 * sizeof(int));
/* keep space for _DYNAMIC pointer, if present */ /* keep space for _DYNAMIC pointer, if present */
@ -730,7 +740,7 @@ static void put_got_entry(TCCState *s1,
{ {
int index; int index;
const char *name; const char *name;
Elf32_Sym *sym; ElfW(Sym) *sym;
unsigned long offset; unsigned long offset;
int *ptr; int *ptr;
@ -745,7 +755,7 @@ static void put_got_entry(TCCState *s1,
put_got_offset(s1, sym_index, s1->got->data_offset); put_got_offset(s1, sym_index, s1->got->data_offset);
if (s1->dynsym) { if (s1->dynsym) {
sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
name = symtab_section->link->data + sym->st_name; name = symtab_section->link->data + sym->st_name;
offset = sym->st_value; offset = sym->st_value;
#ifdef TCC_TARGET_I386 #ifdef TCC_TARGET_I386
@ -838,23 +848,23 @@ static void put_got_entry(TCCState *s1,
static void build_got_entries(TCCState *s1) static void build_got_entries(TCCState *s1)
{ {
Section *s, *symtab; Section *s, *symtab;
Elf32_Rel *rel, *rel_end; ElfW_Rel *rel, *rel_end;
Elf32_Sym *sym; ElfW(Sym) *sym;
int i, type, reloc_type, sym_index; int i, type, reloc_type, sym_index;
for(i = 1; i < s1->nb_sections; i++) { for(i = 1; i < s1->nb_sections; i++) {
s = s1->sections[i]; s = s1->sections[i];
if (s->sh_type != SHT_REL) if (s->sh_type != SHT_RELX)
continue; continue;
/* no need to handle got relocations */ /* no need to handle got relocations */
if (s->link != symtab_section) if (s->link != symtab_section)
continue; continue;
symtab = s->link; symtab = s->link;
rel_end = (Elf32_Rel *)(s->data + s->data_offset); rel_end = (ElfW_Rel *)(s->data + s->data_offset);
for(rel = (Elf32_Rel *)s->data; for(rel = (ElfW_Rel *)s->data;
rel < rel_end; rel < rel_end;
rel++) { rel++) {
type = ELF32_R_TYPE(rel->r_info); type = ELFW(R_TYPE)(rel->r_info);
switch(type) { switch(type) {
#if defined(TCC_TARGET_I386) #if defined(TCC_TARGET_I386)
case R_386_GOT32: case R_386_GOT32:
@ -864,8 +874,8 @@ static void build_got_entries(TCCState *s1)
if (!s1->got) if (!s1->got)
build_got(s1); build_got(s1);
if (type == R_386_GOT32 || type == R_386_PLT32) { if (type == R_386_GOT32 || type == R_386_PLT32) {
sym_index = ELF32_R_SYM(rel->r_info); sym_index = ELFW(R_SYM)(rel->r_info);
sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
/* look at the symbol got offset. If none, then add one */ /* look at the symbol got offset. If none, then add one */
if (type == R_386_GOT32) if (type == R_386_GOT32)
reloc_type = R_386_GLOB_DAT; reloc_type = R_386_GLOB_DAT;
@ -883,8 +893,8 @@ static void build_got_entries(TCCState *s1)
if (!s1->got) if (!s1->got)
build_got(s1); build_got(s1);
if (type == R_ARM_GOT_BREL || type == R_ARM_PLT32) { if (type == R_ARM_GOT_BREL || type == R_ARM_PLT32) {
sym_index = ELF32_R_SYM(rel->r_info); sym_index = ELFW(R_SYM)(rel->r_info);
sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
/* look at the symbol got offset. If none, then add one */ /* look at the symbol got offset. If none, then add one */
if (type == R_ARM_GOT_BREL) if (type == R_ARM_GOT_BREL)
reloc_type = R_ARM_GLOB_DAT; reloc_type = R_ARM_GLOB_DAT;
@ -902,8 +912,8 @@ static void build_got_entries(TCCState *s1)
if (!s1->got) if (!s1->got)
build_got(s1); build_got(s1);
if (type == R_C60_GOT32 || type == R_C60_PLT32) { if (type == R_C60_GOT32 || type == R_C60_PLT32) {
sym_index = ELF32_R_SYM(rel->r_info); sym_index = ELFW(R_SYM)(rel->r_info);
sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
/* look at the symbol got offset. If none, then add one */ /* look at the symbol got offset. If none, then add one */
if (type == R_C60_GOT32) if (type == R_C60_GOT32)
reloc_type = R_C60_GLOB_DAT; reloc_type = R_C60_GLOB_DAT;
@ -932,7 +942,7 @@ static Section *new_symtab(TCCState *s1,
int *ptr, nb_buckets; int *ptr, nb_buckets;
symtab = new_section(s1, symtab_name, sh_type, sh_flags); symtab = new_section(s1, symtab_name, sh_type, sh_flags);
symtab->sh_entsize = sizeof(Elf32_Sym); symtab->sh_entsize = sizeof(ElfW(Sym));
strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags); strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags);
put_elf_str(strtab, ""); put_elf_str(strtab, "");
symtab->link = strtab; symtab->link = strtab;
@ -955,8 +965,8 @@ static Section *new_symtab(TCCState *s1,
/* put dynamic tag */ /* put dynamic tag */
static void put_dt(Section *dynamic, int dt, unsigned long val) static void put_dt(Section *dynamic, int dt, unsigned long val)
{ {
Elf32_Dyn *dyn; ElfW(Dyn) *dyn;
dyn = section_ptr_add(dynamic, sizeof(Elf32_Dyn)); dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));
dyn->d_tag = dt; dyn->d_tag = dt;
dyn->d_un.d_val = val; dyn->d_un.d_val = val;
} }
@ -981,11 +991,11 @@ static void add_init_array_defines(TCCState *s1, const char *section_name)
add_elf_sym(symtab_section, add_elf_sym(symtab_section,
0, 0, 0, 0,
ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
s->sh_num, sym_start); s->sh_num, sym_start);
add_elf_sym(symtab_section, add_elf_sym(symtab_section,
end_offset, 0, end_offset, 0,
ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
s->sh_num, sym_end); s->sh_num, sym_end);
} }
@ -1007,7 +1017,7 @@ static void tcc_add_runtime(TCCState *s1)
ptr = section_ptr_add(bounds_section, sizeof(unsigned long)); ptr = section_ptr_add(bounds_section, sizeof(unsigned long));
*ptr = 0; *ptr = 0;
add_elf_sym(symtab_section, 0, 0, add_elf_sym(symtab_section, 0, 0,
ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
bounds_section->sh_num, "__bounds_start"); bounds_section->sh_num, "__bounds_start");
/* add bound check code */ /* add bound check code */
snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "bcheck.o"); snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "bcheck.o");
@ -1054,15 +1064,15 @@ static void tcc_add_linker_symbols(TCCState *s1)
add_elf_sym(symtab_section, add_elf_sym(symtab_section,
text_section->data_offset, 0, text_section->data_offset, 0,
ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
text_section->sh_num, "_etext"); text_section->sh_num, "_etext");
add_elf_sym(symtab_section, add_elf_sym(symtab_section,
data_section->data_offset, 0, data_section->data_offset, 0,
ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
data_section->sh_num, "_edata"); data_section->sh_num, "_edata");
add_elf_sym(symtab_section, add_elf_sym(symtab_section,
bss_section->data_offset, 0, bss_section->data_offset, 0,
ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
bss_section->sh_num, "_end"); bss_section->sh_num, "_end");
/* horrible new standard ldscript defines */ /* horrible new standard ldscript defines */
add_init_array_defines(s1, ".preinit_array"); add_init_array_defines(s1, ".preinit_array");
@ -1091,12 +1101,12 @@ static void tcc_add_linker_symbols(TCCState *s1)
snprintf(buf, sizeof(buf), "__start_%s", s->name); snprintf(buf, sizeof(buf), "__start_%s", s->name);
add_elf_sym(symtab_section, add_elf_sym(symtab_section,
0, 0, 0, 0,
ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
s->sh_num, buf); s->sh_num, buf);
snprintf(buf, sizeof(buf), "__stop_%s", s->name); snprintf(buf, sizeof(buf), "__stop_%s", s->name);
add_elf_sym(symtab_section, add_elf_sym(symtab_section,
s->data_offset, 0, s->data_offset, 0,
ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
s->sh_num, buf); s->sh_num, buf);
} }
next_sec: ; next_sec: ;
@ -1140,18 +1150,18 @@ static void tcc_output_binary(TCCState *s1, FILE *f,
/* XXX: suppress unneeded sections */ /* XXX: suppress unneeded sections */
int elf_output_file(TCCState *s1, const char *filename) int elf_output_file(TCCState *s1, const char *filename)
{ {
Elf32_Ehdr ehdr; ElfW(Ehdr) ehdr;
FILE *f; FILE *f;
int fd, mode, ret; int fd, mode, ret;
int *section_order; int *section_order;
int shnum, i, phnum, file_offset, offset, size, j, tmp, sh_order_index, k; int shnum, i, phnum, file_offset, offset, size, j, tmp, sh_order_index, k;
unsigned long addr; unsigned long addr;
Section *strsec, *s; Section *strsec, *s;
Elf32_Shdr shdr, *sh; ElfW(Shdr) shdr, *sh;
Elf32_Phdr *phdr, *ph; ElfW(Phdr) *phdr, *ph;
Section *interp, *dynamic, *dynstr; Section *interp, *dynamic, *dynstr;
unsigned long saved_dynamic_data_offset; unsigned long saved_dynamic_data_offset;
Elf32_Sym *sym; ElfW(Sym) *sym;
int type, file_type; int type, file_type;
unsigned long rel_addr, rel_size; unsigned long rel_addr, rel_size;
@ -1177,7 +1187,7 @@ int elf_output_file(TCCState *s1, const char *filename)
if (!s1->static_link) { if (!s1->static_link) {
const char *name; const char *name;
int sym_index, index; int sym_index, index;
Elf32_Sym *esym, *sym_end; ElfW(Sym) *esym, *sym_end;
if (file_type == TCC_OUTPUT_EXE) { if (file_type == TCC_OUTPUT_EXE) {
char *ptr; char *ptr;
@ -1198,7 +1208,7 @@ int elf_output_file(TCCState *s1, const char *filename)
dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC, dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
SHF_ALLOC | SHF_WRITE); SHF_ALLOC | SHF_WRITE);
dynamic->link = dynstr; dynamic->link = dynstr;
dynamic->sh_entsize = sizeof(Elf32_Dyn); dynamic->sh_entsize = sizeof(ElfW(Dyn));
/* add PLT */ /* add PLT */
s1->plt = new_section(s1, ".plt", SHT_PROGBITS, s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
@ -1211,22 +1221,22 @@ int elf_output_file(TCCState *s1, const char *filename)
dynamic symbols. If a symbol STT_FUNC is found, then we dynamic symbols. If a symbol STT_FUNC is found, then we
add it in the PLT. If a symbol STT_OBJECT is found, we add it in the PLT. If a symbol STT_OBJECT is found, we
add it in the .bss section with a suitable relocation */ add it in the .bss section with a suitable relocation */
sym_end = (Elf32_Sym *)(symtab_section->data + sym_end = (ElfW(Sym) *)(symtab_section->data +
symtab_section->data_offset); symtab_section->data_offset);
if (file_type == TCC_OUTPUT_EXE) { if (file_type == TCC_OUTPUT_EXE) {
for(sym = (Elf32_Sym *)symtab_section->data + 1; for(sym = (ElfW(Sym) *)symtab_section->data + 1;
sym < sym_end; sym < sym_end;
sym++) { sym++) {
if (sym->st_shndx == SHN_UNDEF) { if (sym->st_shndx == SHN_UNDEF) {
name = symtab_section->link->data + sym->st_name; name = symtab_section->link->data + sym->st_name;
sym_index = find_elf_sym(s1->dynsymtab_section, name); sym_index = find_elf_sym(s1->dynsymtab_section, name);
if (sym_index) { if (sym_index) {
esym = &((Elf32_Sym *)s1->dynsymtab_section->data)[sym_index]; esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index];
type = ELF32_ST_TYPE(esym->st_info); type = ELFW(ST_TYPE)(esym->st_info);
if (type == STT_FUNC) { if (type == STT_FUNC) {
put_got_entry(s1, R_JMP_SLOT, esym->st_size, put_got_entry(s1, R_JMP_SLOT, esym->st_size,
esym->st_info, esym->st_info,
sym - (Elf32_Sym *)symtab_section->data); sym - (ElfW(Sym) *)symtab_section->data);
} else if (type == STT_OBJECT) { } else if (type == STT_OBJECT) {
unsigned long offset; unsigned long offset;
offset = bss_section->data_offset; offset = bss_section->data_offset;
@ -1244,14 +1254,14 @@ int elf_output_file(TCCState *s1, const char *filename)
/* STB_WEAK undefined symbols are accepted */ /* STB_WEAK undefined symbols are accepted */
/* XXX: _fp_hw seems to be part of the ABI, so we ignore /* XXX: _fp_hw seems to be part of the ABI, so we ignore
it */ it */
if (ELF32_ST_BIND(sym->st_info) == STB_WEAK || if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK ||
!strcmp(name, "_fp_hw")) { !strcmp(name, "_fp_hw")) {
} else { } else {
error_noabort("undefined symbol '%s'", name); error_noabort("undefined symbol '%s'", name);
} }
} }
} else if (s1->rdynamic && } else if (s1->rdynamic &&
ELF32_ST_BIND(sym->st_info) != STB_LOCAL) { ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
/* if -rdynamic option, then export all non /* if -rdynamic option, then export all non
local symbols */ local symbols */
name = symtab_section->link->data + sym->st_name; name = symtab_section->link->data + sym->st_name;
@ -1266,9 +1276,9 @@ int elf_output_file(TCCState *s1, const char *filename)
/* now look at unresolved dynamic symbols and export /* now look at unresolved dynamic symbols and export
corresponding symbol */ corresponding symbol */
sym_end = (Elf32_Sym *)(s1->dynsymtab_section->data + sym_end = (ElfW(Sym) *)(s1->dynsymtab_section->data +
s1->dynsymtab_section->data_offset); s1->dynsymtab_section->data_offset);
for(esym = (Elf32_Sym *)s1->dynsymtab_section->data + 1; for(esym = (ElfW(Sym) *)s1->dynsymtab_section->data + 1;
esym < sym_end; esym < sym_end;
esym++) { esym++) {
if (esym->st_shndx == SHN_UNDEF) { if (esym->st_shndx == SHN_UNDEF) {
@ -1277,12 +1287,12 @@ int elf_output_file(TCCState *s1, const char *filename)
if (sym_index) { if (sym_index) {
/* XXX: avoid adding a symbol if already /* XXX: avoid adding a symbol if already
present because of -rdynamic ? */ present because of -rdynamic ? */
sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
sym->st_info, 0, sym->st_info, 0,
sym->st_shndx, name); sym->st_shndx, name);
} else { } else {
if (ELF32_ST_BIND(esym->st_info) == STB_WEAK) { if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
/* weak symbols can stay undefined */ /* weak symbols can stay undefined */
} else { } else {
warning("undefined dynamic symbol '%s'", name); warning("undefined dynamic symbol '%s'", name);
@ -1293,18 +1303,18 @@ int elf_output_file(TCCState *s1, const char *filename)
} else { } else {
int nb_syms; int nb_syms;
/* shared library case : we simply export all the global symbols */ /* shared library case : we simply export all the global symbols */
nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym); nb_syms = symtab_section->data_offset / sizeof(ElfW(Sym));
s1->symtab_to_dynsym = tcc_mallocz(sizeof(int) * nb_syms); s1->symtab_to_dynsym = tcc_mallocz(sizeof(int) * nb_syms);
for(sym = (Elf32_Sym *)symtab_section->data + 1; for(sym = (ElfW(Sym) *)symtab_section->data + 1;
sym < sym_end; sym < sym_end;
sym++) { sym++) {
if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) { if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
name = symtab_section->link->data + sym->st_name; name = symtab_section->link->data + sym->st_name;
index = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, index = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
sym->st_info, 0, sym->st_info, 0,
sym->st_shndx, name); sym->st_shndx, name);
s1->symtab_to_dynsym[sym - s1->symtab_to_dynsym[sym -
(Elf32_Sym *)symtab_section->data] = (ElfW(Sym) *)symtab_section->data] =
index; index;
} }
} }
@ -1385,7 +1395,7 @@ int elf_output_file(TCCState *s1, const char *filename)
/* when generating a DLL, we include relocations but we may /* when generating a DLL, we include relocations but we may
patch them */ patch them */
if (file_type == TCC_OUTPUT_DLL && if (file_type == TCC_OUTPUT_DLL &&
s->sh_type == SHT_REL && s->sh_type == SHT_RELX &&
!(s->sh_flags & SHF_ALLOC)) { !(s->sh_flags & SHF_ALLOC)) {
/* //gr: avoid bogus relocs for empty (debug) sections */ /* //gr: avoid bogus relocs for empty (debug) sections */
if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC) if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC)
@ -1402,10 +1412,10 @@ int elf_output_file(TCCState *s1, const char *filename)
} }
/* allocate program segment headers */ /* allocate program segment headers */
phdr = tcc_mallocz(phnum * sizeof(Elf32_Phdr)); phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) { if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
file_offset = sizeof(Elf32_Ehdr) + phnum * sizeof(Elf32_Phdr); file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
} else { } else {
file_offset = 0; file_offset = 0;
} }
@ -1471,7 +1481,7 @@ int elf_output_file(TCCState *s1, const char *filename)
s->sh_type == SHT_HASH) { s->sh_type == SHT_HASH) {
if (k != 1) if (k != 1)
continue; continue;
} else if (s->sh_type == SHT_REL) { } else if (s->sh_type == SHT_RELX) {
if (k != 2) if (k != 2)
continue; continue;
} else if (s->sh_type == SHT_NOBITS) { } else if (s->sh_type == SHT_NOBITS) {
@ -1498,7 +1508,7 @@ int elf_output_file(TCCState *s1, const char *filename)
ph->p_paddr = ph->p_vaddr; ph->p_paddr = ph->p_vaddr;
} }
/* update dynamic relocation infos */ /* update dynamic relocation infos */
if (s->sh_type == SHT_REL) { if (s->sh_type == SHT_RELX) {
if (rel_size == 0) if (rel_size == 0)
rel_addr = addr; rel_addr = addr;
rel_size += s->sh_size; rel_size += s->sh_size;
@ -1541,7 +1551,7 @@ int elf_output_file(TCCState *s1, const char *filename)
/* if dynamic section, then add corresponing program header */ /* if dynamic section, then add corresponing program header */
if (dynamic) { if (dynamic) {
Elf32_Sym *sym_end; ElfW(Sym) *sym_end;
ph = &phdr[phnum - 1]; ph = &phdr[phnum - 1];
@ -1589,8 +1599,8 @@ int elf_output_file(TCCState *s1, const char *filename)
} }
/* relocate symbols in .dynsym */ /* relocate symbols in .dynsym */
sym_end = (Elf32_Sym *)(s1->dynsym->data + s1->dynsym->data_offset); sym_end = (ElfW(Sym) *)(s1->dynsym->data + s1->dynsym->data_offset);
for(sym = (Elf32_Sym *)s1->dynsym->data + 1; for(sym = (ElfW(Sym) *)s1->dynsym->data + 1;
sym < sym_end; sym < sym_end;
sym++) { sym++) {
if (sym->st_shndx == SHN_UNDEF) { if (sym->st_shndx == SHN_UNDEF) {
@ -1610,18 +1620,18 @@ int elf_output_file(TCCState *s1, const char *filename)
put_dt(dynamic, DT_STRTAB, dynstr->sh_addr); put_dt(dynamic, DT_STRTAB, dynstr->sh_addr);
put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr); put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
put_dt(dynamic, DT_STRSZ, dynstr->data_offset); put_dt(dynamic, DT_STRSZ, dynstr->data_offset);
put_dt(dynamic, DT_SYMENT, sizeof(Elf32_Sym)); put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
put_dt(dynamic, DT_REL, rel_addr); put_dt(dynamic, DT_REL, rel_addr);
put_dt(dynamic, DT_RELSZ, rel_size); put_dt(dynamic, DT_RELSZ, rel_size);
put_dt(dynamic, DT_RELENT, sizeof(Elf32_Rel)); put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
if (do_debug) if (do_debug)
put_dt(dynamic, DT_DEBUG, 0); put_dt(dynamic, DT_DEBUG, 0);
put_dt(dynamic, DT_NULL, 0); put_dt(dynamic, DT_NULL, 0);
} }
ehdr.e_phentsize = sizeof(Elf32_Phdr); ehdr.e_phentsize = sizeof(ElfW(Phdr));
ehdr.e_phnum = phnum; ehdr.e_phnum = phnum;
ehdr.e_phoff = sizeof(Elf32_Ehdr); ehdr.e_phoff = sizeof(ElfW(Ehdr));
} }
/* all other sections come after */ /* all other sections come after */
@ -1662,7 +1672,7 @@ int elf_output_file(TCCState *s1, const char *filename)
for(i = 1; i < s1->nb_sections; i++) { for(i = 1; i < s1->nb_sections; i++) {
s = s1->sections[i]; s = s1->sections[i];
if ((s->sh_flags & SHF_ALLOC) && if ((s->sh_flags & SHF_ALLOC) &&
s->sh_type == SHT_REL) { s->sh_type == SHT_RELX) {
relocate_rel(s1, s); relocate_rel(s1, s);
} }
} }
@ -1733,14 +1743,14 @@ int elf_output_file(TCCState *s1, const char *filename)
ehdr.e_machine = EM_TCC_TARGET; ehdr.e_machine = EM_TCC_TARGET;
ehdr.e_version = EV_CURRENT; ehdr.e_version = EV_CURRENT;
ehdr.e_shoff = file_offset; ehdr.e_shoff = file_offset;
ehdr.e_ehsize = sizeof(Elf32_Ehdr); ehdr.e_ehsize = sizeof(ElfW(Ehdr));
ehdr.e_shentsize = sizeof(Elf32_Shdr); ehdr.e_shentsize = sizeof(ElfW(Shdr));
ehdr.e_shnum = shnum; ehdr.e_shnum = shnum;
ehdr.e_shstrndx = shnum - 1; ehdr.e_shstrndx = shnum - 1;
fwrite(&ehdr, 1, sizeof(Elf32_Ehdr), f); fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
fwrite(phdr, 1, phnum * sizeof(Elf32_Phdr), f); fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
offset = sizeof(Elf32_Ehdr) + phnum * sizeof(Elf32_Phdr); offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
for(i=1;i<s1->nb_sections;i++) { for(i=1;i<s1->nb_sections;i++) {
s = s1->sections[section_order[i]]; s = s1->sections[section_order[i]];
@ -1763,7 +1773,7 @@ int elf_output_file(TCCState *s1, const char *filename)
for(i=0;i<s1->nb_sections;i++) { for(i=0;i<s1->nb_sections;i++) {
sh = &shdr; sh = &shdr;
memset(sh, 0, sizeof(Elf32_Shdr)); memset(sh, 0, sizeof(ElfW(Shdr)));
s = s1->sections[i]; s = s1->sections[i];
if (s) { if (s) {
sh->sh_name = s->sh_name; sh->sh_name = s->sh_name;
@ -1778,7 +1788,7 @@ int elf_output_file(TCCState *s1, const char *filename)
sh->sh_offset = s->sh_offset; sh->sh_offset = s->sh_offset;
sh->sh_size = s->sh_size; sh->sh_size = s->sh_size;
} }
fwrite(sh, 1, sizeof(Elf32_Shdr), f); fwrite(sh, 1, sizeof(ElfW(Shdr)), f);
} }
} else { } else {
tcc_output_binary(s1, f, section_order); tcc_output_binary(s1, f, section_order);
@ -1830,15 +1840,15 @@ typedef struct SectionMergeInfo {
static int tcc_load_object_file(TCCState *s1, static int tcc_load_object_file(TCCState *s1,
int fd, unsigned long file_offset) int fd, unsigned long file_offset)
{ {
Elf32_Ehdr ehdr; ElfW(Ehdr) ehdr;
Elf32_Shdr *shdr, *sh; ElfW(Shdr) *shdr, *sh;
int size, i, j, offset, offseti, nb_syms, sym_index, ret; int size, i, j, offset, offseti, nb_syms, sym_index, ret;
unsigned char *strsec, *strtab; unsigned char *strsec, *strtab;
int *old_to_new_syms; int *old_to_new_syms;
char *sh_name, *name; char *sh_name, *name;
SectionMergeInfo *sm_table, *sm; SectionMergeInfo *sm_table, *sm;
Elf32_Sym *sym, *symtab; ElfW(Sym) *sym, *symtab;
Elf32_Rel *rel, *rel_end; ElfW_Rel *rel, *rel_end;
Section *s; Section *s;
int stab_index; int stab_index;
@ -1865,7 +1875,7 @@ static int tcc_load_object_file(TCCState *s1,
} }
/* read sections */ /* read sections */
shdr = load_data(fd, file_offset + ehdr.e_shoff, shdr = load_data(fd, file_offset + ehdr.e_shoff,
sizeof(Elf32_Shdr) * ehdr.e_shnum); sizeof(ElfW(Shdr)) * ehdr.e_shnum);
sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum); sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
/* load section names */ /* load section names */
@ -1886,7 +1896,7 @@ static int tcc_load_object_file(TCCState *s1,
ret = -1; ret = -1;
goto the_end; goto the_end;
} }
nb_syms = sh->sh_size / sizeof(Elf32_Sym); nb_syms = sh->sh_size / sizeof(ElfW(Sym));
symtab = load_data(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; sm_table[i].s = symtab_section;
@ -1906,7 +1916,7 @@ static int tcc_load_object_file(TCCState *s1,
sh_name = strsec + sh->sh_name; sh_name = strsec + sh->sh_name;
/* ignore sections types we do not handle */ /* ignore sections types we do not handle */
if (sh->sh_type != SHT_PROGBITS && if (sh->sh_type != SHT_PROGBITS &&
sh->sh_type != SHT_REL && sh->sh_type != SHT_RELX &&
#ifdef TCC_ARM_EABI #ifdef TCC_ARM_EABI
sh->sh_type != SHT_ARM_EXIDX && sh->sh_type != SHT_ARM_EXIDX &&
#endif #endif
@ -2000,7 +2010,7 @@ static int tcc_load_object_file(TCCState *s1,
sh = &shdr[i]; sh = &shdr[i];
if (sh->sh_link > 0) if (sh->sh_link > 0)
s->link = sm_table[sh->sh_link].s; s->link = sm_table[sh->sh_link].s;
if (sh->sh_type == SHT_REL) { if (sh->sh_type == SHT_RELX) {
s->sh_info = sm_table[sh->sh_info].s->sh_num; s->sh_info = sm_table[sh->sh_info].s->sh_num;
/* update backward link */ /* update backward link */
s1->sections[s->sh_info]->reloc = s; s1->sections[s->sh_info]->reloc = s;
@ -2020,7 +2030,7 @@ static int tcc_load_object_file(TCCState *s1,
/* if a symbol is in a link once section, we use the /* if a symbol is in a link once section, we use the
already defined symbol. It is very important to get already defined symbol. It is very important to get
correct relocations */ correct relocations */
if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) { if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
name = strtab + sym->st_name; name = strtab + sym->st_name;
sym_index = find_elf_sym(symtab_section, name); sym_index = find_elf_sym(symtab_section, name);
if (sym_index) if (sym_index)
@ -2052,18 +2062,18 @@ static int tcc_load_object_file(TCCState *s1,
sh = &shdr[i]; sh = &shdr[i];
offset = sm_table[i].offset; offset = sm_table[i].offset;
switch(s->sh_type) { switch(s->sh_type) {
case SHT_REL: case SHT_RELX:
/* take relocation offset information */ /* take relocation offset information */
offseti = sm_table[sh->sh_info].offset; offseti = sm_table[sh->sh_info].offset;
rel_end = (Elf32_Rel *)(s->data + s->data_offset); rel_end = (ElfW_Rel *)(s->data + s->data_offset);
for(rel = (Elf32_Rel *)(s->data + offset); for(rel = (ElfW_Rel *)(s->data + offset);
rel < rel_end; rel < rel_end;
rel++) { rel++) {
int type; int type;
unsigned sym_index; unsigned sym_index;
/* convert symbol index */ /* convert symbol index */
type = ELF32_R_TYPE(rel->r_info); type = ELFW(R_TYPE)(rel->r_info);
sym_index = ELF32_R_SYM(rel->r_info); sym_index = ELFW(R_SYM)(rel->r_info);
/* NOTE: only one symtab assumed */ /* NOTE: only one symtab assumed */
if (sym_index >= nb_syms) if (sym_index >= nb_syms)
goto invalid_reloc; goto invalid_reloc;
@ -2075,7 +2085,7 @@ static int tcc_load_object_file(TCCState *s1,
i, strsec + sh->sh_name, rel->r_offset); i, strsec + sh->sh_name, rel->r_offset);
goto fail; goto fail;
} }
rel->r_info = ELF32_R_INFO(sym_index, type); rel->r_info = ELFW(R_INFO)(sym_index, type);
/* offset the relocation offset */ /* offset the relocation offset */
rel->r_offset += offseti; rel->r_offset += offseti;
} }
@ -2120,7 +2130,7 @@ static int tcc_load_alacarte(TCCState *s1, int fd, int size)
uint8_t *data; uint8_t *data;
const char *ar_names, *p; const char *ar_names, *p;
const uint8_t *ar_index; const uint8_t *ar_index;
Elf32_Sym *sym; ElfW(Sym) *sym;
data = tcc_malloc(size); data = tcc_malloc(size);
if (read(fd, data, size) != size) if (read(fd, data, size) != size)
@ -2134,7 +2144,7 @@ static int tcc_load_alacarte(TCCState *s1, int fd, int size)
for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) { for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {
sym_index = find_elf_sym(symtab_section, p); sym_index = find_elf_sym(symtab_section, p);
if(sym_index) { if(sym_index) {
sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
if(sym->st_shndx == SHN_UNDEF) { if(sym->st_shndx == SHN_UNDEF) {
off = get_be32(ar_index + i * 4) + sizeof(ArchiveHeader); off = get_be32(ar_index + i * 4) + sizeof(ArchiveHeader);
#if 0 #if 0
@ -2214,11 +2224,11 @@ static int tcc_load_archive(TCCState *s1, int fd)
the generated ELF file) */ the generated ELF file) */
static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
{ {
Elf32_Ehdr ehdr; ElfW(Ehdr) ehdr;
Elf32_Shdr *shdr, *sh, *sh1; ElfW(Shdr) *shdr, *sh, *sh1;
int i, j, nb_syms, nb_dts, sym_bind, ret; int i, j, nb_syms, nb_dts, sym_bind, ret;
Elf32_Sym *sym, *dynsym; ElfW(Sym) *sym, *dynsym;
Elf32_Dyn *dt, *dynamic; ElfW(Dyn) *dt, *dynamic;
unsigned char *dynstr; unsigned char *dynstr;
const char *name, *soname; const char *name, *soname;
DLLReference *dllref; DLLReference *dllref;
@ -2233,7 +2243,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
} }
/* read sections */ /* read sections */
shdr = load_data(fd, ehdr.e_shoff, sizeof(Elf32_Shdr) * ehdr.e_shnum); shdr = load_data(fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum);
/* load dynamic section and dynamic symbols */ /* load dynamic section and dynamic symbols */
nb_syms = 0; nb_syms = 0;
@ -2244,11 +2254,11 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) { for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) {
switch(sh->sh_type) { switch(sh->sh_type) {
case SHT_DYNAMIC: case SHT_DYNAMIC:
nb_dts = sh->sh_size / sizeof(Elf32_Dyn); nb_dts = sh->sh_size / sizeof(ElfW(Dyn));
dynamic = load_data(fd, sh->sh_offset, sh->sh_size); dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
break; break;
case SHT_DYNSYM: case SHT_DYNSYM:
nb_syms = sh->sh_size / sizeof(Elf32_Sym); nb_syms = sh->sh_size / sizeof(ElfW(Sym));
dynsym = load_data(fd, sh->sh_offset, sh->sh_size); dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
sh1 = &shdr[sh->sh_link]; sh1 = &shdr[sh->sh_link];
dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size); dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size);
@ -2289,7 +2299,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
/* add dynamic symbols in dynsym_section */ /* add dynamic symbols in dynsym_section */
for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) { for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
sym_bind = ELF32_ST_BIND(sym->st_info); sym_bind = ELFW(ST_BIND)(sym->st_info);
if (sym_bind == STB_LOCAL) if (sym_bind == STB_LOCAL)
continue; continue;
name = dynstr + sym->st_name; name = dynstr + sym->st_name;