History

tccpe: use ElfW macros

grischka committed 17 years ago

Commit fe8def31 parent dcfad3a1 file · +82 −52Browse files

Unified · Split

tccpe.c+82−52
@@ -634,6 +634,31 @@ ST_FN int pe_write(struct pe_info *pe) · 633 unmodified lines
634634
635635 /*----------------------------------------------------------------------------*/
636636
637+#ifdef TCC_TARGET_X86_64
638+#define ELFW_R_(type) ELF64_R_##type
639+#define ElfW_Rel ElfW(Rela)
640+
641+#define REL_TYPE_DIRECT R_X86_64_64
642+#define R_XXX_THUNKFIX R_X86_64_PC32
643+#define R_XXX_RELATIVE R_X86_64_RELATIVE
644+
645+#define ELFW_ST_BIND ELF64_ST_BIND
646+#define ELFW_ST_TYPE ELF64_ST_TYPE
647+#define ELFW_ST_INFO ELF64_ST_INFO
648+#else
649+#define ELFW_R_(type) ELF32_R_##type
650+#define ElfW_Rel ElfW(Rel)
651+
652+#define REL_TYPE_DIRECT R_386_32
653+#define R_XXX_THUNKFIX R_386_32
654+#define R_XXX_RELATIVE R_386_RELATIVE
655+
656+#define ELFW_ST_BIND ELF32_ST_BIND
657+#define ELFW_ST_TYPE ELF32_ST_TYPE
658+#define ELFW_ST_INFO ELF32_ST_INFO
659+#endif
660+/*----------------------------------------------------------------------------*/
661+
637662 ST_FN struct import_symbol *pe_add_import(struct pe_info *pe, int sym_index)
638663 {
639664 int i;
@@ -641,7 +666,7 @@ ST_FN struct import_symbol *pe_add_import(struct pe_info *pe, int sym_index) · 1 unmodified line
641666 struct pe_import_info *p;
642667 struct import_symbol *s;
643668
644− dll_index = ((Elf32_Sym *)pe->s1->dynsymtab_section->data + sym_index)->st_value;
669+ dll_index = ((ElfW(Sym) *)pe->s1->dynsymtab_section->data + sym_index)->st_value;
645670 if (0 == dll_index)
646671 return NULL;
647672
@@ -708,8 +733,8 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 60 unmodified lines
708733 if (k < n) {
709734 int iat_index = p->symbols[k]->iat_index;
710735 int sym_index = p->symbols[k]->sym_index;
711− Elf32_Sym *imp_sym = (Elf32_Sym *)pe->s1->dynsymtab_section->data + sym_index;
712− Elf32_Sym *org_sym = (Elf32_Sym *)symtab_section->data + iat_index;
736+ ElfW(Sym) *imp_sym = (ElfW(Sym) *)pe->s1->dynsymtab_section->data + sym_index;
737+ ElfW(Sym) *org_sym = (ElfW(Sym) *)symtab_section->data + iat_index;
713738 const char *name = pe->s1->dynsymtab_section->link->data + imp_sym->st_name;
714739
715740 org_sym->st_value = thk_ptr;
@@ -770,7 +795,7 @@ ST_FN int sym_cmp(const void *va, const void *vb) · 54 unmodified lines
770795
771796 ST_FN void pe_build_exports(struct pe_info *pe)
772797 {
773− Elf32_Sym *sym;
798+ ElfW(Sym) *sym;
774799 int sym_index, sym_end;
775800 DWORD rva_base, func_o, name_o, ord_o, str_o;
776801 struct pe_export_header *hdr;
@@ -785,9 +810,9 @@ ST_FN void pe_build_exports(struct pe_info *pe) · 8 unmodified lines
785810 rva_base = pe->thunk->sh_addr - pe->imagebase;
786811 sym_count = 0, sorted = NULL, op = NULL;
787812
788− sym_end = symtab_section->data_offset / sizeof(Elf32_Sym);
813+ sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));
789814 for (sym_index = 1; sym_index < sym_end; ++sym_index) {
790− sym = (Elf32_Sym*)symtab_section->data + sym_index;
815+ sym = (ElfW(Sym)*)symtab_section->data + sym_index;
791816 name = symtab_section->link->data + sym->st_name;
792817 if ((sym->st_other & 1)
793818 /* export only symbols from actually written sections */
@@ -849,7 +874,7 @@ ST_FN void pe_build_exports(struct pe_info *pe) · 55 unmodified lines
849874 p = sorted[ord], sym_index = p->index, name = p->name;
850875 /* insert actual address later in pe_relocate_rva */
851876 put_elf_reloc(symtab_section, pe->thunk,
852− func_o, R_386_RELATIVE, sym_index);
877+ func_o, R_XXX_RELATIVE, sym_index);
853878 *(DWORD*)(pe->thunk->data + name_o)
854879 = pe->thunk->data_offset + rva_base;
855880 *(WORD*)(pe->thunk->data + ord_o)
@@ -870,7 +895,7 @@ ST_FN void pe_build_reloc (struct pe_info *pe) · 14 unmodified lines
870895 {
871896 DWORD offset, block_ptr, addr;
872897 int count, i;
873− Elf32_Rel *rel, *rel_end;
898+ ElfW_Rel *rel, *rel_end;
874899 Section *s = NULL, *sr;
875900
876901 offset = addr = block_ptr = count = i = 0;
@@ -878,10 +903,10 @@ ST_FN void pe_build_reloc (struct pe_info *pe) · 1 unmodified line
878903
879904 for(;;) {
880905 if (rel < rel_end) {
881− int type = ELF32_R_TYPE(rel->r_info);
906+ int type = ELFW_R_(TYPE)(rel->r_info);
882907 addr = rel->r_offset + s->sh_addr;
883908 ++ rel;
884− if (type != R_386_32)
909+ if (type != REL_TYPE_DIRECT)
885910 continue;
886911 if (count == 0) { /* new block */
887912 block_ptr = pe->reloc->data_offset;
@@ -899,8 +924,8 @@ ST_FN void pe_build_reloc (struct pe_info *pe) · 11 unmodified lines
899924 } else if (i < pe->sec_count) {
900925 sr = (s = pe->s1->sections[pe->sec_info[i++].ord])->reloc;
901926 if (sr) {
902− rel = (Elf32_Rel *)sr->data;
903− rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
927+ rel = (ElfW_Rel *)sr->data;
928+ rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
904929 }
905930 continue;
906931 }
@@ -1037,7 +1062,7 @@ ST_FN int pe_assign_addresses (struct pe_info *pe) · 130 unmodified lines
10371062 type == SHT_NOBITS ? "nobits" :
10381063 type == SHT_SYMTAB ? "symtab" :
10391064 type == SHT_STRTAB ? "strtab" :
1040− type == SHT_REL ? "rel" : "???",
1065+ type == SHT_RELX ? "rel" : "???",
10411066 s->data_offset,
10421067 flags & SHF_ALLOC ? "alloc" : "",
10431068 flags & SHF_WRITE ? "write" : "",
@@ -1055,37 +1080,39 @@ ST_FN int pe_assign_addresses (struct pe_info *pe) · 11 unmodified lines
10551080 ST_FN void pe_relocate_rva (struct pe_info *pe, Section *s)
10561081 {
10571082 Section *sr = s->reloc;
1058− Elf32_Rel *rel, *rel_end;
1059− rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
1060− for(rel = (Elf32_Rel *)sr->data; rel < rel_end; rel++)
1061− if (ELF32_R_TYPE(rel->r_info) == R_386_RELATIVE) {
1062− int sym_index = ELF32_R_SYM(rel->r_info);
1083+ ElfW_Rel *rel, *rel_end;
1084+ rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
1085+ for(rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++) {
1086+ if (ELFW_R_(TYPE)(rel->r_info) == R_XXX_RELATIVE) {
1087+ int sym_index = ELFW_R_(SYM)(rel->r_info);
10631088 DWORD addr = s->sh_addr;
10641089 if (sym_index) {
1065− Elf32_Sym *sym = (Elf32_Sym *)symtab_section->data + sym_index;
1090+ ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + sym_index;
10661091 addr = sym->st_value;
10671092 }
1093+ // printf("reloc rva %08x %08x\n", (DWORD)rel->r_offset, addr);
10681094 *(DWORD*)(s->data + rel->r_offset) += addr - pe->imagebase;
10691095 }
1096+ }
10701097 }
10711098
10721099 /*----------------------------------------------------------------------------*/
10731100 ST_FN int pe_check_symbols(struct pe_info *pe)
10741101 {
1075− Elf32_Sym *sym;
1102+ ElfW(Sym) *sym;
10761103 int sym_index, sym_end;
10771104 int ret = 0;
10781105
10791106 pe_align_section(text_section, 8);
10801107
1081− sym_end = symtab_section->data_offset / sizeof(Elf32_Sym);
1108+ sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));
10821109 for (sym_index = 1; sym_index < sym_end; ++sym_index) {
10831110
1084− sym = (Elf32_Sym*)symtab_section->data + sym_index;
1111+ sym = (ElfW(Sym) *)symtab_section->data + sym_index;
10851112 if (sym->st_shndx == SHN_UNDEF) {
10861113
10871114 const char *name = symtab_section->link->data + sym->st_name;
1088− unsigned type = ELF32_ST_TYPE(sym->st_info);
1115+ unsigned type = ELFW_ST_TYPE(sym->st_info);
10891116 int imp_sym = pe_find_import(pe->s1, name);
10901117 struct import_symbol *is;
10911118
@@ -1112,15 +1139,15 @@ ST_FN int pe_check_symbols(struct pe_info *pe) · 20 unmodified lines
11121139 sprintf(buffer, "IAT.%s", name);
11131140 is->iat_index = put_elf_sym(
11141141 symtab_section, 0, sizeof(DWORD),
1115− ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT),
1142+ ELFW_ST_INFO(STB_GLOBAL, STT_OBJECT),
11161143 0, SHN_UNDEF, buffer);
11171144 put_elf_reloc(symtab_section, text_section,
1118− offset + 2, R_386_32, is->iat_index);
1145+ offset + 2, R_XXX_THUNKFIX, is->iat_index);
11191146 is->thk_offset = offset;
11201147 }
11211148
11221149 /* tcc_realloc might have altered sym's address */
1123− sym = (Elf32_Sym*)symtab_section->data + sym_index;
1150+ sym = (ElfW(Sym) *)symtab_section->data + sym_index;
11241151
11251152 /* patch the original symbol */
11261153 sym->st_value = offset;
@@ -1142,7 +1169,7 @@ ST_FN int pe_check_symbols(struct pe_info *pe) · 15 unmodified lines
11421169 ret = 1;
11431170
11441171 } else if (pe->s1->rdynamic
1145− && ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {
1172+ && ELFW_ST_BIND(sym->st_info) != STB_LOCAL) {
11461173 /* if -rdynamic option, then export all non local symbols */
11471174 sym->st_other |= 1;
11481175 }
@@ -1166,8 +1193,8 @@ ST_FN void pe_print_section(FILE * f, Section * s) · 17 unmodified lines
11661193 fprintf(f, "\nlink \"%s\"", s->link->name);
11671194 if (s->reloc)
11681195 fprintf(f, "\nreloc \"%s\"", s->reloc->name);
1169− fprintf(f, "\nv_addr %08X", s->sh_addr);
1170− fprintf(f, "\ncontents %08X", l);
1196+ fprintf(f, "\nv_addr %08X", (unsigned)s->sh_addr);
1197+ fprintf(f, "\ncontents %08X", (unsigned)l);
11711198 fprintf(f, "\n\n");
11721199
11731200 if (s->sh_type == SHT_NOBITS)
@@ -1177,9 +1204,9 @@ ST_FN void pe_print_section(FILE * f, Section * s) · 3 unmodified lines
11771204 return;
11781205
11791206 if (s->sh_type == SHT_SYMTAB)
1180− m = sizeof(Elf32_Sym);
1181− if (s->sh_type == SHT_REL)
1182− m = sizeof(Elf32_Rel);
1207+ m = sizeof(ElfW(Sym));
1208+ else if (s->sh_type == SHT_RELX)
1209+ m = sizeof(ElfW_Rel);
11831210 else
11841211 m = 16;
11851212
@@ -1188,7 +1215,7 @@ ST_FN void pe_print_section(FILE * f, Section * s) · 2 unmodified lines
11881215 fprintf(f, " %02x", i);
11891216 n = 56;
11901217
1191− if (s->sh_type == SHT_SYMTAB || s->sh_type == SHT_REL) {
1218+ if (s->sh_type == SHT_SYMTAB || s->sh_type == SHT_RELX) {
11921219 const char *fields1[] = {
11931220 "name",
11941221 "value",
@@ -1235,25 +1262,28 @@ ST_FN void pe_print_section(FILE * f, Section * s) · 40 unmodified lines
12351262 }
12361263
12371264 if (s->sh_type == SHT_SYMTAB) {
1238− Elf32_Sym *sym = (Elf32_Sym *) (p + i);
1265+ ElfW(Sym) *sym = (ElfW(Sym) *) (p + i);
12391266 const char *name = s->link->data + sym->st_name;
12401267 fprintf(f, " %04X %04X %04X %02X %02X %02X %04X \"%s\"",
1241− sym->st_name,
1242− sym->st_value,
1243− sym->st_size,
1244− ELF32_ST_BIND(sym->st_info),
1245− ELF32_ST_TYPE(sym->st_info),
1246− sym->st_other, sym->st_shndx, name);
1247−
1248− } else if (s->sh_type == SHT_REL) {
1249− Elf32_Rel *rel = (Elf32_Rel *) (p + i);
1250− Elf32_Sym *sym =
1251− (Elf32_Sym *) s->link->data + ELF32_R_SYM(rel->r_info);
1268+ (unsigned)sym->st_name,
1269+ (unsigned)sym->st_value,
1270+ (unsigned)sym->st_size,
1271+ (unsigned)ELFW_ST_BIND(sym->st_info),
1272+ (unsigned)ELFW_ST_TYPE(sym->st_info),
1273+ (unsigned)sym->st_other,
1274+ (unsigned)sym->st_shndx,
1275+ name);
1276+
1277+ } else if (s->sh_type == SHT_RELX) {
1278+ ElfW_Rel *rel = (ElfW_Rel *) (p + i);
1279+ ElfW(Sym) *sym =
1280+ (ElfW(Sym) *) s->link->data + ELFW_R_(SYM)(rel->r_info);
12521281 const char *name = s->link->link->data + sym->st_name;
12531282 fprintf(f, " %04X %02X %04X \"%s\"",
1254− rel->r_offset,
1255− ELF32_R_TYPE(rel->r_info),
1256− ELF32_R_SYM(rel->r_info), name);
1283+ (unsigned)rel->r_offset,
1284+ (unsigned)ELFW_R_(TYPE)(rel->r_info),
1285+ (unsigned)ELFW_R_(SYM)(rel->r_info),
1286+ name);
12571287 } else {
12581288 fprintf(f, " ");
12591289 for (n = 0; n < m; ++n) {
@@ -1336,7 +1366,7 @@ PUB_FN int pe_load_res_file(TCCState *s1, int fd) · 76 unmodified lines
13361366 if (rel.type != 7) /* DIR32NB */
13371367 goto quit;
13381368 put_elf_reloc(symtab_section, rsrc_section,
1339− rel.offset, R_386_RELATIVE, 0);
1369+ rel.offset, R_XXX_RELATIVE, 0);
13401370 }
13411371 ret = 0;
13421372 quit:
@@ -1409,7 +1439,7 @@ PUB_FN int pe_load_def_file(TCCState *s1, int fd) · 66 unmodified lines
14091439 default:
14101440 add_elf_sym(s1->dynsymtab_section,
14111441 s1->nb_loaded_dlls, 0,
1412− ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), 0,
1442+ ELFW_ST_INFO(STB_GLOBAL, STT_FUNC), 0,
14131443 text_section->sh_num, p);
14141444 continue;
14151445 }
@@ -1451,7 +1481,7 @@ ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 35 unmodified lines
14511481 if (start_symbol)
14521482 add_elf_sym(symtab_section,
14531483 0, 0,
1454− ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1484+ ELFW_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
14551485 SHN_UNDEF, start_symbol);
14561486
14571487 if (0 == s1->nostdlib) {
@@ -1475,7 +1505,7 @@ ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 17 unmodified lines
14751505 /* for -run GUI's, put '_runwinmain' instead of 'main' */
14761506 add_elf_sym(symtab_section,
14771507 addr, 0,
1478− ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1508+ ELFW_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
14791509 text_section->sh_num, "main");
14801510 }
14811511 pe->start_addr = addr;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t