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
634 634
635/*----------------------------------------------------------------------------*/635/*----------------------------------------------------------------------------*/
636 636
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
637ST_FN struct import_symbol *pe_add_import(struct pe_info *pe, int sym_index)662ST_FN struct import_symbol *pe_add_import(struct pe_info *pe, int sym_index)
638{663{
639 int i;664 int i;
@@ -641,7 +666,7 @@ ST_FN struct import_symbol *pe_add_import(struct pe_info *pe, int sym_index) · 1 unmodified line
641 struct pe_import_info *p;666 struct pe_import_info *p;
642 struct import_symbol *s;667 struct import_symbol *s;
643 668
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;
645 if (0 == dll_index)670 if (0 == dll_index)
646 return NULL;671 return NULL;
647 672
@@ -708,8 +733,8 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 60 unmodified lines
708 if (k < n) {733 if (k < n) {
709 int iat_index = p->symbols[k]->iat_index;734 int iat_index = p->symbols[k]->iat_index;
710 int sym_index = p->symbols[k]->sym_index;735 int sym_index = p->symbols[k]->sym_index;
711 Elf32_Sym *imp_sym = (Elf32_Sym *)pe->s1->dynsymtab_section->data + sym_index;736 ElfW(Sym) *imp_sym = (ElfW(Sym) *)pe->s1->dynsymtab_section->data + sym_index;
712 Elf32_Sym *org_sym = (Elf32_Sym *)symtab_section->data + iat_index;737 ElfW(Sym) *org_sym = (ElfW(Sym) *)symtab_section->data + iat_index;
713 const char *name = pe->s1->dynsymtab_section->link->data + imp_sym->st_name;738 const char *name = pe->s1->dynsymtab_section->link->data + imp_sym->st_name;
714 739
715 org_sym->st_value = thk_ptr;740 org_sym->st_value = thk_ptr;
@@ -770,7 +795,7 @@ ST_FN int sym_cmp(const void *va, const void *vb) · 54 unmodified lines
770 795
771ST_FN void pe_build_exports(struct pe_info *pe)796ST_FN void pe_build_exports(struct pe_info *pe)
772{797{
773 Elf32_Sym *sym;798 ElfW(Sym) *sym;
774 int sym_index, sym_end;799 int sym_index, sym_end;
775 DWORD rva_base, func_o, name_o, ord_o, str_o;800 DWORD rva_base, func_o, name_o, ord_o, str_o;
776 struct pe_export_header *hdr;801 struct pe_export_header *hdr;
@@ -785,9 +810,9 @@ ST_FN void pe_build_exports(struct pe_info *pe) · 8 unmodified lines
785 rva_base = pe->thunk->sh_addr - pe->imagebase;810 rva_base = pe->thunk->sh_addr - pe->imagebase;
786 sym_count = 0, sorted = NULL, op = NULL;811 sym_count = 0, sorted = NULL, op = NULL;
787 812
788 sym_end = symtab_section->data_offset / sizeof(Elf32_Sym);813 sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));
789 for (sym_index = 1; sym_index < sym_end; ++sym_index) {814 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;
791 name = symtab_section->link->data + sym->st_name;816 name = symtab_section->link->data + sym->st_name;
792 if ((sym->st_other & 1)817 if ((sym->st_other & 1)
793 /* export only symbols from actually written sections */818 /* export only symbols from actually written sections */
@@ -849,7 +874,7 @@ ST_FN void pe_build_exports(struct pe_info *pe) · 55 unmodified lines
849 p = sorted[ord], sym_index = p->index, name = p->name;874 p = sorted[ord], sym_index = p->index, name = p->name;
850 /* insert actual address later in pe_relocate_rva */875 /* insert actual address later in pe_relocate_rva */
851 put_elf_reloc(symtab_section, pe->thunk,876 put_elf_reloc(symtab_section, pe->thunk,
852 func_o, R_386_RELATIVE, sym_index);877 func_o, R_XXX_RELATIVE, sym_index);
853 *(DWORD*)(pe->thunk->data + name_o)878 *(DWORD*)(pe->thunk->data + name_o)
854 = pe->thunk->data_offset + rva_base;879 = pe->thunk->data_offset + rva_base;
855 *(WORD*)(pe->thunk->data + ord_o)880 *(WORD*)(pe->thunk->data + ord_o)
@@ -870,7 +895,7 @@ ST_FN void pe_build_reloc (struct pe_info *pe) · 14 unmodified lines
870{895{
871 DWORD offset, block_ptr, addr;896 DWORD offset, block_ptr, addr;
872 int count, i;897 int count, i;
873 Elf32_Rel *rel, *rel_end;898 ElfW_Rel *rel, *rel_end;
874 Section *s = NULL, *sr;899 Section *s = NULL, *sr;
875 900
876 offset = addr = block_ptr = count = i = 0;901 offset = addr = block_ptr = count = i = 0;
@@ -878,10 +903,10 @@ ST_FN void pe_build_reloc (struct pe_info *pe) · 1 unmodified line
878 903
879 for(;;) {904 for(;;) {
880 if (rel < rel_end) {905 if (rel < rel_end) {
881 int type = ELF32_R_TYPE(rel->r_info);906 int type = ELFW_R_(TYPE)(rel->r_info);
882 addr = rel->r_offset + s->sh_addr;907 addr = rel->r_offset + s->sh_addr;
883 ++ rel;908 ++ rel;
884 if (type != R_386_32)909 if (type != REL_TYPE_DIRECT)
885 continue;910 continue;
886 if (count == 0) { /* new block */911 if (count == 0) { /* new block */
887 block_ptr = pe->reloc->data_offset;912 block_ptr = pe->reloc->data_offset;
@@ -899,8 +924,8 @@ ST_FN void pe_build_reloc (struct pe_info *pe) · 11 unmodified lines
899 } else if (i < pe->sec_count) {924 } else if (i < pe->sec_count) {
900 sr = (s = pe->s1->sections[pe->sec_info[i++].ord])->reloc;925 sr = (s = pe->s1->sections[pe->sec_info[i++].ord])->reloc;
901 if (sr) {926 if (sr) {
902 rel = (Elf32_Rel *)sr->data;927 rel = (ElfW_Rel *)sr->data;
903 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);928 rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
904 }929 }
905 continue;930 continue;
906 }931 }
@@ -1037,7 +1062,7 @@ ST_FN int pe_assign_addresses (struct pe_info *pe) · 130 unmodified lines
1037 type == SHT_NOBITS ? "nobits" :1062 type == SHT_NOBITS ? "nobits" :
1038 type == SHT_SYMTAB ? "symtab" :1063 type == SHT_SYMTAB ? "symtab" :
1039 type == SHT_STRTAB ? "strtab" :1064 type == SHT_STRTAB ? "strtab" :
1040 type == SHT_REL ? "rel" : "???",1065 type == SHT_RELX ? "rel" : "???",
1041 s->data_offset,1066 s->data_offset,
1042 flags & SHF_ALLOC ? "alloc" : "",1067 flags & SHF_ALLOC ? "alloc" : "",
1043 flags & SHF_WRITE ? "write" : "",1068 flags & SHF_WRITE ? "write" : "",
@@ -1055,37 +1080,39 @@ ST_FN int pe_assign_addresses (struct pe_info *pe) · 11 unmodified lines
1055ST_FN void pe_relocate_rva (struct pe_info *pe, Section *s)1080ST_FN void pe_relocate_rva (struct pe_info *pe, Section *s)
1056{1081{
1057 Section *sr = s->reloc;1082 Section *sr = s->reloc;
1058 Elf32_Rel *rel, *rel_end;1083 ElfW_Rel *rel, *rel_end;
1059 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);1084 rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
1060 for(rel = (Elf32_Rel *)sr->data; rel < rel_end; rel++)1085 for(rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++) {
1061 if (ELF32_R_TYPE(rel->r_info) == R_386_RELATIVE) {1086 if (ELFW_R_(TYPE)(rel->r_info) == R_XXX_RELATIVE) {
1062 int sym_index = ELF32_R_SYM(rel->r_info);1087 int sym_index = ELFW_R_(SYM)(rel->r_info);
1063 DWORD addr = s->sh_addr;1088 DWORD addr = s->sh_addr;
1064 if (sym_index) {1089 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;
1066 addr = sym->st_value;1091 addr = sym->st_value;
1067 }1092 }
1093 // printf("reloc rva %08x %08x\n", (DWORD)rel->r_offset, addr);
1068 *(DWORD*)(s->data + rel->r_offset) += addr - pe->imagebase;1094 *(DWORD*)(s->data + rel->r_offset) += addr - pe->imagebase;
1069 }1095 }
1096 }
1070}1097}
1071 1098
1072/*----------------------------------------------------------------------------*/1099/*----------------------------------------------------------------------------*/
1073ST_FN int pe_check_symbols(struct pe_info *pe)1100ST_FN int pe_check_symbols(struct pe_info *pe)
1074{1101{
1075 Elf32_Sym *sym;1102 ElfW(Sym) *sym;
1076 int sym_index, sym_end;1103 int sym_index, sym_end;
1077 int ret = 0;1104 int ret = 0;
1078 1105
1079 pe_align_section(text_section, 8);1106 pe_align_section(text_section, 8);
1080 1107
1081 sym_end = symtab_section->data_offset / sizeof(Elf32_Sym);1108 sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));
1082 for (sym_index = 1; sym_index < sym_end; ++sym_index) {1109 for (sym_index = 1; sym_index < sym_end; ++sym_index) {
1083 1110
1084 sym = (Elf32_Sym*)symtab_section->data + sym_index;1111 sym = (ElfW(Sym) *)symtab_section->data + sym_index;
1085 if (sym->st_shndx == SHN_UNDEF) {1112 if (sym->st_shndx == SHN_UNDEF) {
1086 1113
1087 const char *name = symtab_section->link->data + sym->st_name;1114 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);
1089 int imp_sym = pe_find_import(pe->s1, name);1116 int imp_sym = pe_find_import(pe->s1, name);
1090 struct import_symbol *is;1117 struct import_symbol *is;
1091 1118
@@ -1112,15 +1139,15 @@ ST_FN int pe_check_symbols(struct pe_info *pe) · 20 unmodified lines
1112 sprintf(buffer, "IAT.%s", name);1139 sprintf(buffer, "IAT.%s", name);
1113 is->iat_index = put_elf_sym(1140 is->iat_index = put_elf_sym(
1114 symtab_section, 0, sizeof(DWORD),1141 symtab_section, 0, sizeof(DWORD),
1115 ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT),1142 ELFW_ST_INFO(STB_GLOBAL, STT_OBJECT),
1116 0, SHN_UNDEF, buffer);1143 0, SHN_UNDEF, buffer);
1117 put_elf_reloc(symtab_section, text_section, 1144 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);
1119 is->thk_offset = offset;1146 is->thk_offset = offset;
1120 }1147 }
1121 1148
1122 /* tcc_realloc might have altered sym's address */1149 /* 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;
1124 1151
1125 /* patch the original symbol */1152 /* patch the original symbol */
1126 sym->st_value = offset;1153 sym->st_value = offset;
@@ -1142,7 +1169,7 @@ ST_FN int pe_check_symbols(struct pe_info *pe) · 15 unmodified lines
1142 ret = 1;1169 ret = 1;
1143 1170
1144 } else if (pe->s1->rdynamic1171 } else if (pe->s1->rdynamic
1145 && ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {1172 && ELFW_ST_BIND(sym->st_info) != STB_LOCAL) {
1146 /* if -rdynamic option, then export all non local symbols */1173 /* if -rdynamic option, then export all non local symbols */
1147 sym->st_other |= 1;1174 sym->st_other |= 1;
1148 }1175 }
@@ -1166,8 +1193,8 @@ ST_FN void pe_print_section(FILE * f, Section * s) · 17 unmodified lines
1166 fprintf(f, "\nlink \"%s\"", s->link->name);1193 fprintf(f, "\nlink \"%s\"", s->link->name);
1167 if (s->reloc)1194 if (s->reloc)
1168 fprintf(f, "\nreloc \"%s\"", s->reloc->name);1195 fprintf(f, "\nreloc \"%s\"", s->reloc->name);
1169 fprintf(f, "\nv_addr %08X", s->sh_addr);1196 fprintf(f, "\nv_addr %08X", (unsigned)s->sh_addr);
1170 fprintf(f, "\ncontents %08X", l);1197 fprintf(f, "\ncontents %08X", (unsigned)l);
1171 fprintf(f, "\n\n");1198 fprintf(f, "\n\n");
1172 1199
1173 if (s->sh_type == SHT_NOBITS)1200 if (s->sh_type == SHT_NOBITS)
@@ -1177,9 +1204,9 @@ ST_FN void pe_print_section(FILE * f, Section * s) · 3 unmodified lines
1177 return;1204 return;
1178 1205
1179 if (s->sh_type == SHT_SYMTAB)1206 if (s->sh_type == SHT_SYMTAB)
1180 m = sizeof(Elf32_Sym);1207 m = sizeof(ElfW(Sym));
1181 if (s->sh_type == SHT_REL)1208 else if (s->sh_type == SHT_RELX)
1182 m = sizeof(Elf32_Rel);1209 m = sizeof(ElfW_Rel);
1183 else1210 else
1184 m = 16;1211 m = 16;
1185 1212
@@ -1188,7 +1215,7 @@ ST_FN void pe_print_section(FILE * f, Section * s) · 2 unmodified lines
1188 fprintf(f, " %02x", i);1215 fprintf(f, " %02x", i);
1189 n = 56;1216 n = 56;
1190 1217
1191 if (s->sh_type == SHT_SYMTAB || s->sh_type == SHT_REL) {1218 if (s->sh_type == SHT_SYMTAB || s->sh_type == SHT_RELX) {
1192 const char *fields1[] = {1219 const char *fields1[] = {
1193 "name",1220 "name",
1194 "value",1221 "value",
@@ -1235,25 +1262,28 @@ ST_FN void pe_print_section(FILE * f, Section * s) · 40 unmodified lines
1235 }1262 }
1236 1263
1237 if (s->sh_type == SHT_SYMTAB) {1264 if (s->sh_type == SHT_SYMTAB) {
1238 Elf32_Sym *sym = (Elf32_Sym *) (p + i);1265 ElfW(Sym) *sym = (ElfW(Sym) *) (p + i);
1239 const char *name = s->link->data + sym->st_name;1266 const char *name = s->link->data + sym->st_name;
1240 fprintf(f, " %04X %04X %04X %02X %02X %02X %04X \"%s\"",1267 fprintf(f, " %04X %04X %04X %02X %02X %02X %04X \"%s\"",
1241 sym->st_name,1268 (unsigned)sym->st_name,
1242 sym->st_value,1269 (unsigned)sym->st_value,
1243 sym->st_size,1270 (unsigned)sym->st_size,
1244 ELF32_ST_BIND(sym->st_info),1271 (unsigned)ELFW_ST_BIND(sym->st_info),
1245 ELF32_ST_TYPE(sym->st_info),1272 (unsigned)ELFW_ST_TYPE(sym->st_info),
1246 sym->st_other, sym->st_shndx, name);1273 (unsigned)sym->st_other,
1247 1274 (unsigned)sym->st_shndx,
1248 } else if (s->sh_type == SHT_REL) {1275 name);
1249 Elf32_Rel *rel = (Elf32_Rel *) (p + i);1276
1250 Elf32_Sym *sym =1277 } else if (s->sh_type == SHT_RELX) {
1251 (Elf32_Sym *) s->link->data + ELF32_R_SYM(rel->r_info);1278 ElfW_Rel *rel = (ElfW_Rel *) (p + i);
1279 ElfW(Sym) *sym =
1280 (ElfW(Sym) *) s->link->data + ELFW_R_(SYM)(rel->r_info);
1252 const char *name = s->link->link->data + sym->st_name;1281 const char *name = s->link->link->data + sym->st_name;
1253 fprintf(f, " %04X %02X %04X \"%s\"",1282 fprintf(f, " %04X %02X %04X \"%s\"",
1254 rel->r_offset,1283 (unsigned)rel->r_offset,
1255 ELF32_R_TYPE(rel->r_info),1284 (unsigned)ELFW_R_(TYPE)(rel->r_info),
1256 ELF32_R_SYM(rel->r_info), name);1285 (unsigned)ELFW_R_(SYM)(rel->r_info),
1286 name);
1257 } else {1287 } else {
1258 fprintf(f, " ");1288 fprintf(f, " ");
1259 for (n = 0; n < m; ++n) {1289 for (n = 0; n < m; ++n) {
@@ -1336,7 +1366,7 @@ PUB_FN int pe_load_res_file(TCCState *s1, int fd) · 76 unmodified lines
1336 if (rel.type != 7) /* DIR32NB */1366 if (rel.type != 7) /* DIR32NB */
1337 goto quit;1367 goto quit;
1338 put_elf_reloc(symtab_section, rsrc_section,1368 put_elf_reloc(symtab_section, rsrc_section,
1339 rel.offset, R_386_RELATIVE, 0);1369 rel.offset, R_XXX_RELATIVE, 0);
1340 }1370 }
1341 ret = 0;1371 ret = 0;
1342quit:1372quit:
@@ -1409,7 +1439,7 @@ PUB_FN int pe_load_def_file(TCCState *s1, int fd) · 66 unmodified lines
1409 default:1439 default:
1410 add_elf_sym(s1->dynsymtab_section,1440 add_elf_sym(s1->dynsymtab_section,
1411 s1->nb_loaded_dlls, 0,1441 s1->nb_loaded_dlls, 0,
1412 ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), 0,1442 ELFW_ST_INFO(STB_GLOBAL, STT_FUNC), 0,
1413 text_section->sh_num, p);1443 text_section->sh_num, p);
1414 continue;1444 continue;
1415 }1445 }
@@ -1451,7 +1481,7 @@ ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 35 unmodified lines
1451 if (start_symbol)1481 if (start_symbol)
1452 add_elf_sym(symtab_section,1482 add_elf_sym(symtab_section,
1453 0, 0,1483 0, 0,
1454 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,1484 ELFW_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1455 SHN_UNDEF, start_symbol);1485 SHN_UNDEF, start_symbol);
1456 1486
1457 if (0 == s1->nostdlib) {1487 if (0 == s1->nostdlib) {
@@ -1475,7 +1505,7 @@ ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 17 unmodified lines
1475 /* for -run GUI's, put '_runwinmain' instead of 'main' */1505 /* for -run GUI's, put '_runwinmain' instead of 'main' */
1476 add_elf_sym(symtab_section,1506 add_elf_sym(symtab_section,
1477 addr, 0,1507 addr, 0,
1478 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,1508 ELFW_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1479 text_section->sh_num, "main");1509 text_section->sh_num, "main");
1480 }1510 }
1481 pe->start_addr = addr;1511 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