tccpe: cleanup ELFW() macros etc.

This commit is contained in:
grischka 2011-07-14 19:23:04 +02:00
parent d483ab322f
commit aa80e5b1ff
5 changed files with 44 additions and 59 deletions

10
elf.h
View File

@ -1712,14 +1712,4 @@ typedef Elf32_Addr Elf32_Conflict;
#define R_C60HI16 0x55 // high 16 bit MVKH 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 */

18
tcc.h
View File

@ -1150,6 +1150,24 @@ ST_INLN void inp(void);
ST_FUNC int handle_eob(void);
#endif
#ifdef TCC_TARGET_X86_64
# define ELFCLASSW ELFCLASS64
# define ElfW(type) Elf##64##_##type
# define ELFW(type) ELF##64##_##type
# define ElfW_Rel ElfW(Rela)
# define SHT_RELX SHT_RELA
# define REL_SECTION_FMT ".rela%s"
/* XXX: DLL with PLT would only work with x86-64 for now */
# define TCC_OUTPUT_DLL_WITH_PLT
#else
# define ELFCLASSW ELFCLASS32
# define ElfW(type) Elf##32##_##type
# define ELFW(type) ELF##32##_##type
# define ElfW_Rel ElfW(Rel)
# define SHT_RELX SHT_REL
# define REL_SECTION_FMT ".rel%s"
#endif
/* ------------ xxx-gen.c ------------ */
ST_FUNC void gsym_addr(int t, int a);

View File

@ -20,23 +20,8 @@
#include "tcc.h"
#ifdef TCC_TARGET_X86_64
#define ElfW_Rel ElfW(Rela)
#define SHT_RELX SHT_RELA
#define REL_SECTION_FMT ".rela%s"
/* x86-64 requires PLT for DLLs */
#define TCC_OUTPUT_DLL_WITH_PLT
#else
#define ElfW_Rel ElfW(Rel)
#define SHT_RELX SHT_REL
#define REL_SECTION_FMT ".rel%s"
#endif
static int new_undef_sym = 0; /* Is there a new undefined sym since last new_undef_sym() */
/* XXX: DLL with PLT would only work with x86-64 for now */
//#define TCC_OUTPUT_DLL_WITH_PLT
ST_FUNC int put_elf_str(Section *s, const char *sym)
{
int offset, len;
@ -2119,7 +2104,7 @@ static int elf_output_file(TCCState *s1, const char *filename)
ehdr.e_ident[1] = ELFMAG1;
ehdr.e_ident[2] = ELFMAG2;
ehdr.e_ident[3] = ELFMAG3;
ehdr.e_ident[4] = TCC_ELFCLASS;
ehdr.e_ident[4] = ELFCLASSW;
ehdr.e_ident[5] = ELFDATA2LSB;
ehdr.e_ident[6] = EV_CURRENT;
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)

46
tccpe.c
View File

@ -726,38 +726,20 @@ static int pe_write(struct pe_info *pe)
/*----------------------------------------------------------------------------*/
#if defined(TCC_TARGET_X86_64)
#define ELFW_R_(type) ELF64_R_##type
#define ElfW_Rel ElfW(Rela)
#define REL_TYPE_DIRECT R_X86_64_64
#define R_XXX_THUNKFIX R_X86_64_PC32
#define R_XXX_RELATIVE R_X86_64_RELATIVE
#define ELFW_ST_BIND ELF64_ST_BIND
#define ELFW_ST_TYPE ELF64_ST_TYPE
#define ELFW_ST_INFO ELF64_ST_INFO
#elif defined(TCC_TARGET_I386)
#define ELFW_R_(type) ELF32_R_##type
#define ElfW_Rel ElfW(Rel)
#define REL_TYPE_DIRECT R_386_32
#define R_XXX_THUNKFIX R_386_32
#define R_XXX_RELATIVE R_386_RELATIVE
#define ELFW_ST_BIND ELF32_ST_BIND
#define ELFW_ST_TYPE ELF32_ST_TYPE
#define ELFW_ST_INFO ELF32_ST_INFO
#elif defined(TCC_TARGET_ARM)
#define ELFW_R_(type) ELF32_R_##type
#define ElfW_Rel ElfW(Rel)
#define REL_TYPE_DIRECT R_ARM_ABS32
#define R_XXX_THUNKFIX R_ARM_ABS32
#define R_XXX_RELATIVE R_ARM_RELATIVE
#define ELFW_ST_BIND ELF32_ST_BIND
#define ELFW_ST_TYPE ELF32_ST_TYPE
#define ELFW_ST_INFO ELF32_ST_INFO
#endif
/*----------------------------------------------------------------------------*/
@ -1003,7 +985,7 @@ static void pe_build_reloc (struct pe_info *pe)
for(;;) {
if (rel < rel_end) {
int type = ELFW_R_(TYPE)(rel->r_info);
int type = ELFW(R_TYPE)(rel->r_info);
addr = rel->r_offset + s->sh_addr;
++ rel;
if (type != REL_TYPE_DIRECT)
@ -1185,8 +1167,8 @@ static void pe_relocate_rva (struct pe_info *pe, Section *s)
ElfW_Rel *rel, *rel_end;
rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
for(rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++) {
if (ELFW_R_(TYPE)(rel->r_info) == R_XXX_RELATIVE) {
int sym_index = ELFW_R_(SYM)(rel->r_info);
if (ELFW(R_TYPE)(rel->r_info) == R_XXX_RELATIVE) {
int sym_index = ELFW(R_SYM)(rel->r_info);
DWORD addr = s->sh_addr;
if (sym_index) {
ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + sym_index;
@ -1230,7 +1212,7 @@ static int pe_check_symbols(struct pe_info *pe)
if (sym->st_shndx == SHN_UNDEF) {
const char *name = symtab_section->link->data + sym->st_name;
unsigned type = ELFW_ST_TYPE(sym->st_info);
unsigned type = ELFW(ST_TYPE)(sym->st_info);
int imp_sym = pe_find_import(pe->s1, sym);
struct import_symbol *is;
@ -1274,7 +1256,7 @@ static int pe_check_symbols(struct pe_info *pe)
sprintf(buffer, "IAT.%s", name);
is->iat_index = put_elf_sym(
symtab_section, 0, sizeof(DWORD),
ELFW_ST_INFO(STB_GLOBAL, STT_OBJECT),
ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
0, SHN_UNDEF, buffer);
#ifdef TCC_TARGET_ARM
put_elf_reloc(symtab_section, text_section,
@ -1309,7 +1291,7 @@ static int pe_check_symbols(struct pe_info *pe)
ret = -1;
} else if (pe->s1->rdynamic
&& ELFW_ST_BIND(sym->st_info) != STB_LOCAL) {
&& ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
/* if -rdynamic option, then export all non local symbols */
sym->st_other |= 1;
}
@ -1408,8 +1390,8 @@ static void pe_print_section(FILE * f, Section * s)
(unsigned)sym->st_name,
(unsigned)sym->st_value,
(unsigned)sym->st_size,
(unsigned)ELFW_ST_BIND(sym->st_info),
(unsigned)ELFW_ST_TYPE(sym->st_info),
(unsigned)ELFW(ST_BIND)(sym->st_info),
(unsigned)ELFW(ST_TYPE)(sym->st_info),
(unsigned)sym->st_other,
(unsigned)sym->st_shndx,
name);
@ -1417,12 +1399,12 @@ static void pe_print_section(FILE * f, Section * s)
} else if (s->sh_type == SHT_RELX) {
ElfW_Rel *rel = (ElfW_Rel *) (p + i);
ElfW(Sym) *sym =
(ElfW(Sym) *) s->link->data + ELFW_R_(SYM)(rel->r_info);
(ElfW(Sym) *) s->link->data + ELFW(R_SYM)(rel->r_info);
const char *name = s->link->link->data + sym->st_name;
fprintf(f, " %04X %02X %04X \"%s\"",
(unsigned)rel->r_offset,
(unsigned)ELFW_R_(TYPE)(rel->r_info),
(unsigned)ELFW_R_(SYM)(rel->r_info),
(unsigned)ELFW(R_TYPE)(rel->r_info),
(unsigned)ELFW(R_SYM)(rel->r_info),
name);
} else {
fprintf(f, " ");
@ -1505,7 +1487,7 @@ ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, const voi
s1->dynsymtab_section,
(uplong)value,
dllindex, /* st_size */
ELFW_ST_INFO(STB_GLOBAL, STT_NOTYPE),
ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE),
0,
value ? SHN_ABS : SHN_UNDEF,
name
@ -1794,7 +1776,7 @@ static void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe)
if (start_symbol)
add_elf_sym(symtab_section,
0, 0,
ELFW_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
SHN_UNDEF, start_symbol);
if (0 == s1->nostdlib) {
@ -1820,7 +1802,7 @@ static void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe)
/* for -run GUI's, put '_runwinmain' instead of 'main' */
add_elf_sym(symtab_section,
addr, 0,
ELFW_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
text_section->sh_num, "main");
}

View File

@ -27,6 +27,16 @@
#include "../../elf.h"
#ifdef TCC_TARGET_X86_64
# define ELFCLASSW ELFCLASS64
# define ElfW(type) Elf##64##_##type
# define ELFW(type) ELF##64##_##type
#else
# define ELFCLASSW ELFCLASS32
# define ElfW(type) Elf##32##_##type
# define ELFW(type) ELF##32##_##type
#endif
#define ARMAG "!<arch>\n"
#define ARFMAG "`\n"
@ -144,7 +154,7 @@ int main(int argc, char **argv)
//printf("%s:\n", argv[iarg]);
// elf header
ehdr = (ElfW(Ehdr) *)buf;
if (ehdr->e_ident[4] != TCC_ELFCLASS)
if (ehdr->e_ident[4] != ELFCLASSW)
{
fprintf(stderr, "Unsupported Elf Class: %s\n", argv[iarg]);
fclose(fo);