History

Clean tccelf.c

- remove debug printf and commented out code - remove C++-like comments - remove whitespace at end of lines - replace tabs by spaces

Thomas Preud'homme committed 12 years ago

Commit 3d4b57f1 parent 6f3569e1 file · +182 −202Browse files

Unified · Split

tccelf.c+182−202
@@ -1,6 +1,6 @@
1/*1/*
2 * ELF file handling for TCC2 * ELF file handling for TCC
3 * 3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard4 * Copyright (c) 2001-2004 Fabrice Bellard
5 *5 *
6 * This library is free software; you can redistribute it and/or6 * This library is free software; you can redistribute it and/or
@@ -38,7 +38,7 @@ ST_FUNC int put_elf_str(Section *s, const char *sym) · 31 unmodified lines
38static unsigned long elf_hash(const unsigned char *name)38static unsigned long elf_hash(const unsigned char *name)
39{39{
40 unsigned long h = 0, g;40 unsigned long h = 0, g;
41 41
42 while (*name) {42 while (*name) {
43 h = (h << 4) + *name++;43 h = (h << 4) + *name++;
44 g = h & 0xf0000000;44 g = h & 0xf0000000;
@@ -91,7 +91,7 @@ ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, · 46 unmodified lines
91 int nbuckets, h;91 int nbuckets, h;
92 ElfW(Sym) *sym;92 ElfW(Sym) *sym;
93 Section *hs;93 Section *hs;
94 94
95 sym = section_ptr_add(s, sizeof(ElfW(Sym)));95 sym = section_ptr_add(s, sizeof(ElfW(Sym)));
96 if (name)96 if (name)
97 name_offset = put_elf_str(s->link, name);97 name_offset = put_elf_str(s->link, name);
@@ -139,7 +139,7 @@ ST_FUNC int find_elf_sym(Section *s, const char *name) · 41 unmodified lines
139 Section *hs;139 Section *hs;
140 int nbuckets, sym_index, h;140 int nbuckets, sym_index, h;
141 const char *name1;141 const char *name1;
142 142
143 hs = s->hash;143 hs = s->hash;
144 if (!hs)144 if (!hs)
145 return 0;145 return 0;
@@ -198,7 +198,7 @@ ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, · 52 unmodified lines
198 sym_bind = ELFW(ST_BIND)(info);198 sym_bind = ELFW(ST_BIND)(info);
199 sym_type = ELFW(ST_TYPE)(info);199 sym_type = ELFW(ST_TYPE)(info);
200 sym_vis = ELFW(ST_VISIBILITY)(other);200 sym_vis = ELFW(ST_VISIBILITY)(other);
201 201
202 if (sym_bind != STB_LOCAL) {202 if (sym_bind != STB_LOCAL) {
203 /* we search global or weak symbols */203 /* we search global or weak symbols */
204 sym_index = find_elf_sym(s, name);204 sym_index = find_elf_sym(s, name);
@@ -257,8 +257,8 @@ ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, · 52 unmodified lines
257 }257 }
258 } else {258 } else {
259 do_def:259 do_def:
260 sym_index = put_elf_sym(s, value, size, 260 sym_index = put_elf_sym(s, value, size,
261 ELFW(ST_INFO)(sym_bind, sym_type), other, 261 ELFW(ST_INFO)(sym_bind, sym_type), other,
262 sh_num, name);262 sh_num, name);
263 }263 }
264 return sym_index;264 return sym_index;
@@ -311,11 +311,11 @@ ST_FUNC void put_stabs(const char *str, int type, int other, int desc, · 46 unmodified lines
311 sym->n_value = value;311 sym->n_value = value;
312}312}
313 313
314ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc, 314ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc,
315 unsigned long value, Section *sec, int sym_index)315 unsigned long value, Section *sec, int sym_index)
316{316{
317 put_stabs(str, type, other, desc, value);317 put_stabs(str, type, other, desc, value);
318 put_elf_reloc(symtab_section, stab_section, 318 put_elf_reloc(symtab_section, stab_section,
319 stab_section->data_offset - sizeof(unsigned int),319 stab_section->data_offset - sizeof(unsigned int),
320 R_DATA_32, sym_index);320 R_DATA_32, sym_index);
321}321}
@@ -370,7 +370,7 @@ static void sort_syms(TCCState *s1, Section *s) · 48 unmodified lines
370 }370 }
371 p++;371 p++;
372 }372 }
373 373
374 /* we copy the new symbols to the old */374 /* we copy the new symbols to the old */
375 memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));375 memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));
376 tcc_free(new_syms);376 tcc_free(new_syms);
@@ -390,7 +390,7 @@ static void sort_syms(TCCState *s1, Section *s) · 13 unmodified lines
390 }390 }
391 }391 }
392 }392 }
393 393
394 tcc_free(old_to_new_syms);394 tcc_free(old_to_new_syms);
395}395}
396 396
@@ -588,13 +588,13 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 191 unmodified lines
588 case R_386_16:588 case R_386_16:
589 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {589 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
590 output_file:590 output_file:
591 tcc_error("can only produce 16-bit binary files");591 tcc_error("can only produce 16-bit binary files");
592 }592 }
593 *(short *)ptr += val;593 *(short *)ptr += val;
594 break;594 break;
595 case R_386_PC16:595 case R_386_PC16:
596 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)596 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
597 goto output_file;597 goto output_file;
598 *(short *)ptr += val - addr;598 *(short *)ptr += val - addr;
599 break;599 break;
600#elif defined(TCC_TARGET_ARM)600#elif defined(TCC_TARGET_ARM)
@@ -642,7 +642,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 41 unmodified lines
642 guard its use */642 guard its use */
643 case R_ARM_THM_PC22:643 case R_ARM_THM_PC22:
644 case R_ARM_THM_JUMP24:644 case R_ARM_THM_JUMP24:
645 {645 {
646 int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;646 int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;
647 int to_thumb, is_call, to_plt, blx_bit = 1 << 12;647 int to_thumb, is_call, to_plt, blx_bit = 1 << 12;
648 Section *plt;648 Section *plt;
@@ -720,7 +720,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 71 unmodified lines
720 *(int *)ptr |= x;720 *(int *)ptr |= x;
721 else721 else
722 *(int *)ptr += x;722 *(int *)ptr += x;
723 }723 }
724 break;724 break;
725 case R_ARM_THM_MOVT_ABS:725 case R_ARM_THM_MOVT_ABS:
726 case R_ARM_THM_MOVW_ABS_NC:726 case R_ARM_THM_MOVW_ABS_NC:
@@ -737,7 +737,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 10 unmodified lines
737 *(int *)ptr |= x;737 *(int *)ptr |= x;
738 else738 else
739 *(int *)ptr += x;739 *(int *)ptr += x;
740 }740 }
741 break;741 break;
742 case R_ARM_PREL31:742 case R_ARM_PREL31:
743 {743 {
@@ -784,15 +784,15 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 40 unmodified lines
784 case R_C60LO16:784 case R_C60LO16:
785 {785 {
786 uint32_t orig;786 uint32_t orig;
787 787
788 /* put the low 16 bits of the absolute address */788 /* put the low 16 bits of the absolute address
789 // add to what is already there789 add to what is already there */
790 790
791 orig = ((*(int *)(ptr )) >> 7) & 0xffff;791 orig = ((*(int *)(ptr )) >> 7) & 0xffff;
792 orig |= (((*(int *)(ptr+4)) >> 7) & 0xffff) << 16;792 orig |= (((*(int *)(ptr+4)) >> 7) & 0xffff) << 16;
793 793
794 //patch both at once - assumes always in pairs Low - High794 /* patch both at once - assumes always in pairs Low - High */
795 795
796 *(int *) ptr = (*(int *) ptr & (~(0xffff << 7)) ) | (((val+orig) & 0xffff) << 7);796 *(int *) ptr = (*(int *) ptr & (~(0xffff << 7)) ) | (((val+orig) & 0xffff) << 7);
797 *(int *)(ptr+4) = (*(int *)(ptr+4) & (~(0xffff << 7)) ) | ((((val+orig)>>16) & 0xffff) << 7);797 *(int *)(ptr+4) = (*(int *)(ptr+4) & (~(0xffff << 7)) ) | ((((val+orig)>>16) & 0xffff) << 7);
798 }798 }
@@ -990,7 +990,7 @@ static void build_got(TCCState *s1) · 191 unmodified lines
990 /* if no got, then create it */990 /* if no got, then create it */
991 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);991 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
992 s1->got->sh_entsize = 4;992 s1->got->sh_entsize = 4;
993 add_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT), 993 add_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
994 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");994 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
995 ptr = section_ptr_add(s1->got, 3 * PTR_SIZE);995 ptr = section_ptr_add(s1->got, 3 * PTR_SIZE);
996#if PTR_SIZE == 4996#if PTR_SIZE == 4
@@ -1014,7 +1014,7 @@ static void build_got(TCCState *s1) · 17 unmodified lines
1014/* put a got entry corresponding to a symbol in symtab_section. 'size'1014/* put a got entry corresponding to a symbol in symtab_section. 'size'
1015 and 'info' can be modifed if more precise info comes from the DLL */1015 and 'info' can be modifed if more precise info comes from the DLL */
1016static void put_got_entry(TCCState *s1,1016static void put_got_entry(TCCState *s1,
1017 int reloc_type, unsigned long size, int info, 1017 int reloc_type, unsigned long size, int info,
1018 int sym_index)1018 int sym_index)
1019{1019{
1020 int index;1020 int index;
@@ -1092,7 +1092,7 @@ static void put_got_entry(TCCState *s1, · 71 unmodified lines
1092 if (reloc_type == R_ARM_JUMP_SLOT) {1092 if (reloc_type == R_ARM_JUMP_SLOT) {
1093 Section *plt;1093 Section *plt;
1094 uint8_t *p;1094 uint8_t *p;
1095 1095
1096 /* if we build a DLL, we add a %ebx offset */1096 /* if we build a DLL, we add a %ebx offset */
1097 if (s1->output_type == TCC_OUTPUT_DLL)1097 if (s1->output_type == TCC_OUTPUT_DLL)
1098 tcc_error("DLLs unimplemented!");1098 tcc_error("DLLs unimplemented!");
@@ -1110,14 +1110,14 @@ static void put_got_entry(TCCState *s1, · 11 unmodified lines
1110 1110
1111 if (s1->sym_attrs[sym_index].plt_thumb_stub) {1111 if (s1->sym_attrs[sym_index].plt_thumb_stub) {
1112 p = section_ptr_add(plt, 20);1112 p = section_ptr_add(plt, 20);
1113 put32(p , 0x4778); // bx pc1113 put32(p , 0x4778); /* bx pc */
1114 put32(p+2, 0x46c0); // nop1114 put32(p+2, 0x46c0); /* nop */
1115 p += 4;1115 p += 4;
1116 } else1116 } else
1117 p = section_ptr_add(plt, 16);1117 p = section_ptr_add(plt, 16);
1118 put32(p , 0xe59fc004); // ldr ip, [pc, #4] // offset in GOT1118 put32(p , 0xe59fc004); /* ldr ip, [pc, #4] ; offset in GOT */
1119 put32(p+4, 0xe08fc00c); // add ip, pc, ip // absolute address or offset1119 put32(p+4, 0xe08fc00c); /* add ip, pc, ip ; absolute address or offset */
1120 put32(p+8, 0xe59cf000); // ldr pc, [ip] // load absolute address or load offset1120 put32(p+8, 0xe59cf000); /* ldr pc, [ip] ; load absolute address or load offset */
1121 put32(p+12, s1->got->data_offset);1121 put32(p+12, s1->got->data_offset);
1122 1122
1123 /* the symbol is modified so that it will be relocated to1123 /* the symbol is modified so that it will be relocated to
@@ -1130,11 +1130,11 @@ static void put_got_entry(TCCState *s1, · 6 unmodified lines
1130#else1130#else
1131#error unsupported CPU1131#error unsupported CPU
1132#endif1132#endif
1133 index = put_elf_sym(s1->dynsym, offset, 1133 index = put_elf_sym(s1->dynsym, offset,
1134 size, info, 0, sym->st_shndx, name);1134 size, info, 0, sym->st_shndx, name);
1135 /* put a got entry */1135 /* put a got entry */
1136 put_elf_reloc(s1->dynsym, s1->got, 1136 put_elf_reloc(s1->dynsym, s1->got,
1137 s1->got->data_offset, 1137 s1->got->data_offset,
1138 reloc_type, index);1138 reloc_type, index);
1139 }1139 }
1140 ptr = section_ptr_add(s1->got, PTR_SIZE);1140 ptr = section_ptr_add(s1->got, PTR_SIZE);
@@ -1177,7 +1177,7 @@ ST_FUNC void build_got_entries(TCCState *s1) · 36 unmodified lines
1177 reloc_type = R_386_GLOB_DAT;1177 reloc_type = R_386_GLOB_DAT;
1178 else1178 else
1179 reloc_type = R_386_JMP_SLOT;1179 reloc_type = R_386_JMP_SLOT;
1180 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info, 1180 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1181 sym_index);1181 sym_index);
1182 }1182 }
1183 break;1183 break;
@@ -1196,7 +1196,7 @@ ST_FUNC void build_got_entries(TCCState *s1) · 12 unmodified lines
1196 reloc_type = R_ARM_GLOB_DAT;1196 reloc_type = R_ARM_GLOB_DAT;
1197 else1197 else
1198 reloc_type = R_ARM_JUMP_SLOT;1198 reloc_type = R_ARM_JUMP_SLOT;
1199 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info, 1199 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1200 sym_index);1200 sym_index);
1201 }1201 }
1202 break;1202 break;
@@ -1215,7 +1215,7 @@ ST_FUNC void build_got_entries(TCCState *s1) · 12 unmodified lines
1215 reloc_type = R_C60_GLOB_DAT;1215 reloc_type = R_C60_GLOB_DAT;
1216 else1216 else
1217 reloc_type = R_C60_JMP_SLOT;1217 reloc_type = R_C60_JMP_SLOT;
1218 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info, 1218 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1219 sym_index);1219 sym_index);
1220 }1220 }
1221 break;1221 break;
@@ -1235,7 +1235,7 @@ ST_FUNC void build_got_entries(TCCState *s1) · 13 unmodified lines
1235 reloc_type = R_X86_64_GLOB_DAT;1235 reloc_type = R_X86_64_GLOB_DAT;
1236 else1236 else
1237 reloc_type = R_X86_64_JUMP_SLOT;1237 reloc_type = R_X86_64_JUMP_SLOT;
1238 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info, 1238 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1239 sym_index);1239 sym_index);
1240 }1240 }
1241 break;1241 break;
@@ -1251,7 +1251,7 @@ ST_FUNC void build_got_entries(TCCState *s1) · 9 unmodified lines
1251 1251
1252ST_FUNC Section *new_symtab(TCCState *s1,1252ST_FUNC Section *new_symtab(TCCState *s1,
1253 const char *symtab_name, int sh_type, int sh_flags,1253 const char *symtab_name, int sh_type, int sh_flags,
1254 const char *strtab_name, 1254 const char *strtab_name,
1255 const char *hash_name, int hash_sh_flags)1255 const char *hash_name, int hash_sh_flags)
1256{1256{
1257 Section *symtab, *strtab, *hash;1257 Section *symtab, *strtab, *hash;
@@ -1263,7 +1263,7 @@ ST_FUNC Section *new_symtab(TCCState *s1, · 5 unmodified lines
1263 put_elf_str(strtab, "");1263 put_elf_str(strtab, "");
1264 symtab->link = strtab;1264 symtab->link = strtab;
1265 put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);1265 put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);
1266 1266
1267 nb_buckets = 1;1267 nb_buckets = 1;
1268 1268
1269 hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);1269 hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
@@ -1293,7 +1293,7 @@ static void add_init_array_defines(TCCState *s1, const char *section_name) · 23 unmodified lines
1293 long end_offset;1293 long end_offset;
1294 char sym_start[1024];1294 char sym_start[1024];
1295 char sym_end[1024];1295 char sym_end[1024];
1296 1296
1297 snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1);1297 snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1);
1298 snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1);1298 snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1);
1299 1299
@@ -1305,11 +1305,11 @@ static void add_init_array_defines(TCCState *s1, const char *section_name) · 5 unmodified lines
1305 end_offset = s->data_offset;1305 end_offset = s->data_offset;
1306 }1306 }
1307 1307
1308 add_elf_sym(symtab_section, 1308 add_elf_sym(symtab_section,
1309 0, 0,1309 0, 0,
1310 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1310 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1311 s->sh_num, sym_start);1311 s->sh_num, sym_start);
1312 add_elf_sym(symtab_section, 1312 add_elf_sym(symtab_section,
1313 end_offset, 0,1313 end_offset, 0,
1314 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1314 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1315 s->sh_num, sym_end);1315 s->sh_num, sym_end);
@@ -1385,15 +1385,15 @@ ST_FUNC void tcc_add_linker_symbols(TCCState *s1) · 69 unmodified lines
1385 int i;1385 int i;
1386 Section *s;1386 Section *s;
1387 1387
1388 add_elf_sym(symtab_section, 1388 add_elf_sym(symtab_section,
1389 text_section->data_offset, 0,1389 text_section->data_offset, 0,
1390 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1390 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1391 text_section->sh_num, "_etext");1391 text_section->sh_num, "_etext");
1392 add_elf_sym(symtab_section, 1392 add_elf_sym(symtab_section,
1393 data_section->data_offset, 0,1393 data_section->data_offset, 0,
1394 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1394 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1395 data_section->sh_num, "_edata");1395 data_section->sh_num, "_edata");
1396 add_elf_sym(symtab_section, 1396 add_elf_sym(symtab_section,
1397 bss_section->data_offset, 0,1397 bss_section->data_offset, 0,
1398 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1398 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1399 bss_section->sh_num, "_end");1399 bss_section->sh_num, "_end");
@@ -1401,7 +1401,7 @@ ST_FUNC void tcc_add_linker_symbols(TCCState *s1) · 1 unmodified line
1401 add_init_array_defines(s1, ".preinit_array");1401 add_init_array_defines(s1, ".preinit_array");
1402 add_init_array_defines(s1, ".init_array");1402 add_init_array_defines(s1, ".init_array");
1403 add_init_array_defines(s1, ".fini_array");1403 add_init_array_defines(s1, ".fini_array");
1404 1404
1405 /* add start and stop symbols for sections whose name can be1405 /* add start and stop symbols for sections whose name can be
1406 expressed in C */1406 expressed in C */
1407 for(i = 1; i < s1->nb_sections; i++) {1407 for(i = 1; i < s1->nb_sections; i++) {
@@ -1422,7 +1422,7 @@ ST_FUNC void tcc_add_linker_symbols(TCCState *s1) · 14 unmodified lines
1422 p++;1422 p++;
1423 }1423 }
1424 snprintf(buf, sizeof(buf), "__start_%s", s->name);1424 snprintf(buf, sizeof(buf), "__start_%s", s->name);
1425 add_elf_sym(symtab_section, 1425 add_elf_sym(symtab_section,
1426 0, 0,1426 0, 0,
1427 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1427 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1428 s->sh_num, buf);1428 s->sh_num, buf);
@@ -1459,8 +1459,8 @@ static void tcc_output_binary(TCCState *s1, FILE *f, · 30 unmodified lines
1459}1459}
1460 1460
1461#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)1461#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1462#define HAVE_PHDR 11462#define HAVE_PHDR 1
1463#define EXTRA_RELITEMS 141463#define EXTRA_RELITEMS 14
1464 1464
1465/* move the relocation value from .dynsym to .got */1465/* move the relocation value from .dynsym to .got */
1466void patch_dynsym_undef(TCCState *s1, Section *s)1466void patch_dynsym_undef(TCCState *s1, Section *s)
@@ -1468,19 +1468,19 @@ void patch_dynsym_undef(TCCState *s1, Section *s) · 1 unmodified line
1468 uint32_t *gotd = (void *)s1->got->data;1468 uint32_t *gotd = (void *)s1->got->data;
1469 ElfW(Sym) *sym, *sym_end;1469 ElfW(Sym) *sym, *sym_end;
1470 1470
1471 gotd += 3; // dummy entries in .got1471 gotd += 3; /* dummy entries in .got */
1472 /* relocate symbols in .dynsym */1472 /* relocate symbols in .dynsym */
1473 sym_end = (ElfW(Sym) *)(s->data + s->data_offset);1473 sym_end = (ElfW(Sym) *)(s->data + s->data_offset);
1474 for (sym = (ElfW(Sym) *)s->data + 1; sym < sym_end; sym++) {1474 for (sym = (ElfW(Sym) *)s->data + 1; sym < sym_end; sym++) {
1475 if (sym->st_shndx == SHN_UNDEF) {1475 if (sym->st_shndx == SHN_UNDEF) {
1476 *gotd++ = sym->st_value + 6; // XXX 6 is magic ?1476 *gotd++ = sym->st_value + 6; /* XXX 6 is magic ? */
1477 sym->st_value = 0;1477 sym->st_value = 0;
1478 }1478 }
1479 }1479 }
1480}1480}
1481#else1481#else
1482#define HAVE_PHDR 01482#define HAVE_PHDR 0
1483#define EXTRA_RELITEMS 91483#define EXTRA_RELITEMS 9
1484 1484
1485/* zero plt offsets of weak symbols in .dynsym */1485/* zero plt offsets of weak symbols in .dynsym */
1486void patch_dynsym_undef(TCCState *s1, Section *s)1486void patch_dynsym_undef(TCCState *s1, Section *s)
@@ -1496,45 +1496,45 @@ void patch_dynsym_undef(TCCState *s1, Section *s) · 9 unmodified lines
1496 1496
1497ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel)1497ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel)
1498{1498{
1499 int sym_index = ELFW(R_SYM) (rel->r_info);1499 int sym_index = ELFW(R_SYM) (rel->r_info);
1500 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];1500 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1501 unsigned long offset;1501 unsigned long offset;
1502 1502
1503 if (sym_index >= s1->nb_sym_attrs)1503 if (sym_index >= s1->nb_sym_attrs)
1504 return;1504 return;
1505 offset = s1->sym_attrs[sym_index].got_offset;1505 offset = s1->sym_attrs[sym_index].got_offset;
1506 section_reserve(s1->got, offset + PTR_SIZE);1506 section_reserve(s1->got, offset + PTR_SIZE);
1507#ifdef TCC_TARGET_X86_641507#ifdef TCC_TARGET_X86_64
1508 /* only works for x86-64 */1508 /* only works for x86-64 */
1509 put32(s1->got->data + offset + 4, sym->st_value >> 32);1509 put32(s1->got->data + offset + 4, sym->st_value >> 32);
1510#endif1510#endif
1511 put32(s1->got->data + offset, sym->st_value & 0xffffffff);1511 put32(s1->got->data + offset, sym->st_value & 0xffffffff);
1512}1512}
1513 1513
1514ST_FUNC void fill_got(TCCState *s1)1514ST_FUNC void fill_got(TCCState *s1)
1515{1515{
1516 Section *s;1516 Section *s;
1517 ElfW_Rel *rel, *rel_end;1517 ElfW_Rel *rel, *rel_end;
1518 int i;1518 int i;
1519 1519
1520 for(i = 1; i < s1->nb_sections; i++) {1520 for(i = 1; i < s1->nb_sections; i++) {
1521 s = s1->sections[i];1521 s = s1->sections[i];
1522 if (s->sh_type != SHT_RELX)1522 if (s->sh_type != SHT_RELX)
1523 continue;1523 continue;
1524 /* no need to handle got relocations */1524 /* no need to handle got relocations */
1525 if (s->link != symtab_section)1525 if (s->link != symtab_section)
1526 continue;1526 continue;
1527 rel_end = (ElfW_Rel *) (s->data + s->data_offset);1527 rel_end = (ElfW_Rel *) (s->data + s->data_offset);
1528 for(rel = (ElfW_Rel *) s->data; rel < rel_end; rel++) {1528 for(rel = (ElfW_Rel *) s->data; rel < rel_end; rel++) {
1529 switch (ELFW(R_TYPE) (rel->r_info)) {1529 switch (ELFW(R_TYPE) (rel->r_info)) {
1530 case R_X86_64_GOT32:1530 case R_X86_64_GOT32:
1531 case R_X86_64_GOTPCREL:1531 case R_X86_64_GOTPCREL:
1532 case R_X86_64_PLT32:1532 case R_X86_64_PLT32:
1533 fill_got_entry(s1, rel);1533 fill_got_entry(s1, rel);
1534 break;1534 break;
1535 }1535 }
1536 }1536 }
1537 }1537 }
1538}1538}
1539 1539
1540 1540
@@ -1574,7 +1574,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 33 unmodified lines
1574 dynamic = NULL;1574 dynamic = NULL;
1575 dynstr = NULL; /* avoid warning */1575 dynstr = NULL; /* avoid warning */
1576 saved_dynamic_data_offset = 0; /* avoid warning */1576 saved_dynamic_data_offset = 0; /* avoid warning */
1577 1577
1578 if (file_type != TCC_OUTPUT_OBJ) {1578 if (file_type != TCC_OUTPUT_OBJ) {
1579 relocate_common_syms();1579 relocate_common_syms();
1580 1580
@@ -1587,10 +1587,10 @@ static int elf_output_file(TCCState *s1, const char *filename) · 6 unmodified lines
1587 1587
1588 if (file_type == TCC_OUTPUT_EXE) {1588 if (file_type == TCC_OUTPUT_EXE) {
1589 char *ptr;1589 char *ptr;
1590 /* allow override the dynamic loader */1590 /* allow override the dynamic loader */
1591 const char *elfint = getenv("LD_SO");1591 const char *elfint = getenv("LD_SO");
1592 if (elfint == NULL)1592 if (elfint == NULL)
1593 elfint = DEFAULT_ELFINTERP(s1);1593 elfint = DEFAULT_ELFINTERP(s1);
1594 /* add interpreter section only if executable */1594 /* add interpreter section only if executable */
1595 interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);1595 interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
1596 interp->sh_addralign = 1;1596 interp->sh_addralign = 1;
@@ -1600,18 +1600,18 @@ static int elf_output_file(TCCState *s1, const char *filename) · 3 unmodified lines
1600 1600
1601 /* add dynamic symbol table */1601 /* add dynamic symbol table */
1602 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,1602 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
1603 ".dynstr", 1603 ".dynstr",
1604 ".hash", SHF_ALLOC);1604 ".hash", SHF_ALLOC);
1605 dynstr = s1->dynsym->link;1605 dynstr = s1->dynsym->link;
1606 1606
1607 /* add dynamic section */1607 /* add dynamic section */
1608 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC, 1608 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
1609 SHF_ALLOC | SHF_WRITE);1609 SHF_ALLOC | SHF_WRITE);
1610 dynamic->link = dynstr;1610 dynamic->link = dynstr;
1611 dynamic->sh_entsize = sizeof(ElfW(Dyn));1611 dynamic->sh_entsize = sizeof(ElfW(Dyn));
1612 1612
1613 /* add PLT */1613 /* add PLT */
1614 s1->plt = new_section(s1, ".plt", SHT_PROGBITS, 1614 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
1615 SHF_ALLOC | SHF_EXECINSTR);1615 SHF_ALLOC | SHF_EXECINSTR);
1616 s1->plt->sh_entsize = 4;1616 s1->plt->sh_entsize = 4;
1617 1617
@@ -1643,7 +1643,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 25 unmodified lines
1643 * function wanted by the caller of dlsym1643 * function wanted by the caller of dlsym
1644 * instead of the address of the function that1644 * instead of the address of the function that
1645 * would return that address */1645 * would return that address */
1646 put_got_entry(s1, R_JMP_SLOT, esym->st_size, 1646 put_got_entry(s1, R_JMP_SLOT, esym->st_size,
1647 ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC),1647 ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC),
1648 sym - (ElfW(Sym) *)symtab_section->data);1648 sym - (ElfW(Sym) *)symtab_section->data);
1649 } else if (type == STT_OBJECT) {1649 } else if (type == STT_OBJECT) {
@@ -1652,10 +1652,10 @@ static int elf_output_file(TCCState *s1, const char *filename) · 2 unmodified lines
1652 offset = bss_section->data_offset;1652 offset = bss_section->data_offset;
1653 /* XXX: which alignment ? */1653 /* XXX: which alignment ? */
1654 offset = (offset + 16 - 1) & -16;1654 offset = (offset + 16 - 1) & -16;
1655 index = put_elf_sym(s1->dynsym, offset, esym->st_size, 1655 index = put_elf_sym(s1->dynsym, offset, esym->st_size,
1656 esym->st_info, 0, 1656 esym->st_info, 0,
1657 bss_section->sh_num, name);1657 bss_section->sh_num, name);
1658 // Ensure R_COPY works for weak symbol aliases1658 /* Ensure R_COPY works for weak symbol aliases */
1659 if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {1659 if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
1660 dynsym_end = (ElfW(Sym) *)1660 dynsym_end = (ElfW(Sym) *)
1661 (s1->dynsymtab_section->data +1661 (s1->dynsymtab_section->data +
@@ -1676,7 +1676,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 14 unmodified lines
1676 }1676 }
1677 }1677 }
1678 }1678 }
1679 put_elf_reloc(s1->dynsym, bss_section, 1679 put_elf_reloc(s1->dynsym, bss_section,
1680 offset, R_COPY, index);1680 offset, R_COPY, index);
1681 offset += esym->st_size;1681 offset += esym->st_size;
1682 bss_section->data_offset = offset;1682 bss_section->data_offset = offset;
@@ -1691,17 +1691,17 @@ static int elf_output_file(TCCState *s1, const char *filename) · 8 unmodified lines
1691 tcc_error_noabort("undefined symbol '%s'", name);1691 tcc_error_noabort("undefined symbol '%s'", name);
1692 }1692 }
1693 }1693 }
1694 } else if (s1->rdynamic && 1694 } else if (s1->rdynamic &&
1695 ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {1695 ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1696 /* if -rdynamic option, then export all non1696 /* if -rdynamic option, then export all non
1697 local symbols */1697 local symbols */
1698 name = symtab_section->link->data + sym->st_name;1698 name = symtab_section->link->data + sym->st_name;
1699 put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, 1699 put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1700 sym->st_info, 0, 1700 sym->st_info, 0,
1701 sym->st_shndx, name);1701 sym->st_shndx, name);
1702 }1702 }
1703 }1703 }
1704 1704
1705 if (s1->nb_errors)1705 if (s1->nb_errors)
1706 goto fail;1706 goto fail;
1707 1707
@@ -1719,8 +1719,8 @@ static int elf_output_file(TCCState *s1, const char *filename) · 11 unmodified lines
1719 /* XXX: avoid adding a symbol if already1719 /* XXX: avoid adding a symbol if already
1720 present because of -rdynamic ? */1720 present because of -rdynamic ? */
1721 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];1721 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1722 put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, 1722 put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1723 sym->st_info, 0, 1723 sym->st_info, 0,
1724 sym->st_shndx, name);1724 sym->st_shndx, name);
1725 } else {1725 } else {
1726 if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {1726 if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
@@ -1745,24 +1745,24 @@ static int elf_output_file(TCCState *s1, const char *filename) · 18 unmodified lines
1745 ELFW(ST_TYPE)(sym->st_info) == STT_GNU_IFUNC)1745 ELFW(ST_TYPE)(sym->st_info) == STT_GNU_IFUNC)
1746 && sym->st_shndx == SHN_UNDEF) {1746 && sym->st_shndx == SHN_UNDEF) {
1747 int visibility = ELFW(ST_BIND)(sym->st_info);1747 int visibility = ELFW(ST_BIND)(sym->st_info);
1748 put_got_entry(s1, R_JMP_SLOT, sym->st_size, 1748 put_got_entry(s1, R_JMP_SLOT, sym->st_size,
1749 ELFW(ST_INFO)(visibility,STT_FUNC),1749 ELFW(ST_INFO)(visibility,STT_FUNC),
1750 sym - (ElfW(Sym) *)symtab_section->data);1750 sym - (ElfW(Sym) *)symtab_section->data);
1751 }1751 }
1752 else if (ELFW(ST_TYPE)(sym->st_info) == STT_OBJECT) {1752 else if (ELFW(ST_TYPE)(sym->st_info) == STT_OBJECT) {
1753 put_got_entry(s1, R_X86_64_GLOB_DAT, sym->st_size, 1753 put_got_entry(s1, R_X86_64_GLOB_DAT, sym->st_size,
1754 sym->st_info, 1754 sym->st_info,
1755 sym - (ElfW(Sym) *)symtab_section->data);1755 sym - (ElfW(Sym) *)symtab_section->data);
1756 }1756 }
1757 else1757 else
1758#endif1758#endif
1759 {1759 {
1760 name = symtab_section->link->data + sym->st_name;1760 name = symtab_section->link->data + sym->st_name;
1761 index = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, 1761 index = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1762 sym->st_info, 0, 1762 sym->st_info, 0,
1763 sym->st_shndx, name);1763 sym->st_shndx, name);
1764 s1->symtab_to_dynsym[sym - 1764 s1->symtab_to_dynsym[sym -
1765 (ElfW(Sym) *)symtab_section->data] = 1765 (ElfW(Sym) *)symtab_section->data] =
1766 index;1766 index;
1767 }1767 }
1768 }1768 }
@@ -1770,7 +1770,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 1 unmodified line
1770 }1770 }
1771 1771
1772 build_got_entries(s1);1772 build_got_entries(s1);
1773 1773
1774 /* add a list of needed dlls */1774 /* add a list of needed dlls */
1775 for(i = 0; i < s1->nb_loaded_dlls; i++) {1775 for(i = 0; i < s1->nb_loaded_dlls; i++) {
1776 DLLReference *dllref = s1->loaded_dlls[i];1776 DLLReference *dllref = s1->loaded_dlls[i];
@@ -1806,7 +1806,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 29 unmodified lines
1806 /* we add a section for symbols */1806 /* we add a section for symbols */
1807 strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);1807 strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
1808 put_elf_str(strsec, "");1808 put_elf_str(strsec, "");
1809 1809
1810 /* compute number of sections */1810 /* compute number of sections */
1811 shnum = s1->nb_sections;1811 shnum = s1->nb_sections;
1812 1812
@@ -1814,7 +1814,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 1 unmodified line
1814 section_order = tcc_malloc(sizeof(int) * shnum);1814 section_order = tcc_malloc(sizeof(int) * shnum);
1815 section_order[0] = 0;1815 section_order[0] = 0;
1816 sh_order_index = 1;1816 sh_order_index = 1;
1817 1817
1818 /* compute number of program headers */1818 /* compute number of program headers */
1819 switch(file_type) {1819 switch(file_type) {
1820 default:1820 default:
@@ -1839,27 +1839,18 @@ static int elf_output_file(TCCState *s1, const char *filename) · 18 unmodified lines
1839 for(i = 1; i < s1->nb_sections; i++) {1839 for(i = 1; i < s1->nb_sections; i++) {
1840 s = s1->sections[i];1840 s = s1->sections[i];
1841 s->sh_name = put_elf_str(strsec, s->name);1841 s->sh_name = put_elf_str(strsec, s->name);
1842#if 0 /* gr */
1843 printf("section: f=%08x t=%08x i=%08x %s %s\n",
1844 s->sh_flags,
1845 s->sh_type,
1846 s->sh_info,
1847 s->name,
1848 s->reloc ? s->reloc->name : "n"
1849 );
1850#endif
1851 /* when generating a DLL, we include relocations but we may1842 /* when generating a DLL, we include relocations but we may
1852 patch them */1843 patch them */
1853 if (file_type == TCC_OUTPUT_DLL && 1844 if (file_type == TCC_OUTPUT_DLL &&
1854 s->sh_type == SHT_RELX && 1845 s->sh_type == SHT_RELX &&
1855 !(s->sh_flags & SHF_ALLOC)) {1846 !(s->sh_flags & SHF_ALLOC)) {
1856 /* //gr: avoid bogus relocs for empty (debug) sections */1847 /* gr: avoid bogus relocs for empty (debug) sections */
1857 if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC)1848 if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC)
1858 prepare_dynamic_rel(s1, s);1849 prepare_dynamic_rel(s1, s);
1859 else if (s1->do_debug)1850 else if (s1->do_debug)
1860 s->sh_size = s->data_offset;1851 s->sh_size = s->data_offset;
1861 } else if (s1->do_debug ||1852 } else if (s1->do_debug ||
1862 file_type == TCC_OUTPUT_OBJ || 1853 file_type == TCC_OUTPUT_OBJ ||
1863 (s->sh_flags & SHF_ALLOC) ||1854 (s->sh_flags & SHF_ALLOC) ||
1864 i == (s1->nb_sections - 1)) {1855 i == (s1->nb_sections - 1)) {
1865 /* we output all sections if debug or object file */1856 /* we output all sections if debug or object file */
@@ -1869,7 +1860,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 3 unmodified lines
1869 1860
1870 /* allocate program segment headers */1861 /* allocate program segment headers */
1871 phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));1862 phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
1872 1863
1873 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {1864 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
1874 file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));1865 file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1875 } else {1866 } else {
@@ -1877,14 +1868,14 @@ static int elf_output_file(TCCState *s1, const char *filename) · 1 unmodified line
1877 }1868 }
1878 if (phnum > 0) {1869 if (phnum > 0) {
1879 /* compute section to program header mapping */1870 /* compute section to program header mapping */
1880 if (s1->has_text_addr) { 1871 if (s1->has_text_addr) {
1881 int a_offset, p_offset;1872 int a_offset, p_offset;
1882 addr = s1->text_addr;1873 addr = s1->text_addr;
1883 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %1874 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1884 ELF_PAGE_SIZE */1875 ELF_PAGE_SIZE */
1885 a_offset = (int) (addr & (s1->section_align - 1));1876 a_offset = (int) (addr & (s1->section_align - 1));
1886 p_offset = file_offset & (s1->section_align - 1);1877 p_offset = file_offset & (s1->section_align - 1);
1887 if (a_offset < p_offset) 1878 if (a_offset < p_offset)
1888 a_offset += s1->section_align;1879 a_offset += s1->section_align;
1889 file_offset += (a_offset - p_offset);1880 file_offset += (a_offset - p_offset);
1890 } else {1881 } else {
@@ -1895,7 +1886,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 4 unmodified lines
1895 /* compute address after headers */1886 /* compute address after headers */
1896 addr += (file_offset & (s1->section_align - 1));1887 addr += (file_offset & (s1->section_align - 1));
1897 }1888 }
1898 1889
1899 /* dynamic relocation table information, for .dynamic section */1890 /* dynamic relocation table information, for .dynamic section */
1900 rel_size = 0;1891 rel_size = 0;
1901 rel_addr = 0;1892 rel_addr = 0;
@@ -1915,7 +1906,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 13 unmodified lines
1915 else1906 else
1916 ph->p_flags = PF_R | PF_W;1907 ph->p_flags = PF_R | PF_W;
1917 ph->p_align = s1->section_align;1908 ph->p_align = s1->section_align;
1918 1909
1919 /* we do the following ordering: interp, symbol tables,1910 /* we do the following ordering: interp, symbol tables,
1920 relocations, progbits, nobits */1911 relocations, progbits, nobits */
1921 /* XXX: do faster and simpler sorting */1912 /* XXX: do faster and simpler sorting */
@@ -1924,11 +1915,11 @@ static int elf_output_file(TCCState *s1, const char *filename) · 2 unmodified lines
1924 s = s1->sections[i];1915 s = s1->sections[i];
1925 /* compute if section should be included */1916 /* compute if section should be included */
1926 if (j == 0) {1917 if (j == 0) {
1927 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != 1918 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1928 SHF_ALLOC)1919 SHF_ALLOC)
1929 continue;1920 continue;
1930 } else {1921 } else {
1931 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != 1922 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1932 (SHF_ALLOC | SHF_WRITE))1923 (SHF_ALLOC | SHF_WRITE))
1933 continue;1924 continue;
1934 }1925 }
@@ -1954,12 +1945,12 @@ static int elf_output_file(TCCState *s1, const char *filename) · 19 unmodified lines
1954 1945
1955 /* section matches: we align it and add its size */1946 /* section matches: we align it and add its size */
1956 tmp = addr;1947 tmp = addr;
1957 addr = (addr + s->sh_addralign - 1) & 1948 addr = (addr + s->sh_addralign - 1) &
1958 ~(s->sh_addralign - 1);1949 ~(s->sh_addralign - 1);
1959 file_offset += (int) ( addr - tmp );1950 file_offset += (int) ( addr - tmp );
1960 s->sh_offset = file_offset;1951 s->sh_offset = file_offset;
1961 s->sh_addr = addr;1952 s->sh_addr = addr;
1962 1953
1963 /* update program header infos */1954 /* update program header infos */
1964 if (ph->p_offset == 0) {1955 if (ph->p_offset == 0) {
1965 ph->p_offset = file_offset;1956 ph->p_offset = file_offset;
@@ -1969,14 +1960,14 @@ static int elf_output_file(TCCState *s1, const char *filename) · 3 unmodified lines
1969 /* update dynamic relocation infos */1960 /* update dynamic relocation infos */
1970 if (s->sh_type == SHT_RELX) {1961 if (s->sh_type == SHT_RELX) {
1971#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)1962#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1972 if (!strcmp(strsec->data + s->sh_name, ".rel.got")) { // rel_size == 0) {1963 if (!strcmp(strsec->data + s->sh_name, ".rel.got")) {
1973 rel_addr = addr;1964 rel_addr = addr;
1974 rel_size += s->sh_size; // XXX only first rel.1965 rel_size += s->sh_size; /* XXX only first rel. */
1975 }1966 }
1976 if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) { // rel_size == 0) {1967 if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) {
1977 bss_addr = addr;1968 bss_addr = addr;
1978 bss_size = s->sh_size; // XXX only first rel.1969 bss_size = s->sh_size; /* XXX only first rel. */
1979 }1970 }
1980#else1971#else
1981 if (rel_size == 0)1972 if (rel_size == 0)
1982 rel_addr = addr;1973 rel_addr = addr;
@@ -2010,18 +2001,18 @@ static int elf_output_file(TCCState *s1, const char *filename) · 27 unmodified lines
2010 ph = &phdr[0];2001 ph = &phdr[0];
2011 2002
2012#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)2003#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2013 {2004 {
2014 int len = phnum * sizeof(ElfW(Phdr));2005 int len = phnum * sizeof(ElfW(Phdr));
2015 2006
2016 ph->p_type = PT_PHDR;2007 ph->p_type = PT_PHDR;
2017 ph->p_offset = sizeof(ElfW(Ehdr));2008 ph->p_offset = sizeof(ElfW(Ehdr));
2018 ph->p_vaddr = interp->sh_addr - len;2009 ph->p_vaddr = interp->sh_addr - len;
2019 ph->p_paddr = ph->p_vaddr;2010 ph->p_paddr = ph->p_vaddr;
2020 ph->p_filesz = ph->p_memsz = len;2011 ph->p_filesz = ph->p_memsz = len;
2021 ph->p_flags = PF_R | PF_X;2012 ph->p_flags = PF_R | PF_X;
2022 ph->p_align = 4; // interp->sh_addralign;2013 ph->p_align = 4; /* interp->sh_addralign; */
2023 ph++;2014 ph++;
2024 }2015 }
2025#endif2016#endif
2026 2017
2027 ph->p_type = PT_INTERP;2018 ph->p_type = PT_INTERP;
@@ -2033,13 +2024,13 @@ static int elf_output_file(TCCState *s1, const char *filename) · 5 unmodified lines
2033 ph->p_flags = PF_R;2024 ph->p_flags = PF_R;
2034 ph->p_align = interp->sh_addralign;2025 ph->p_align = interp->sh_addralign;
2035 }2026 }
2036 2027
2037 /* if dynamic section, then add corresponing program header */2028 /* if dynamic section, then add corresponing program header */
2038 if (dynamic) {2029 if (dynamic) {
2039 ElfW(Sym) *sym_end;2030 ElfW(Sym) *sym_end;
2040 2031
2041 ph = &phdr[phnum - 1];2032 ph = &phdr[phnum - 1];
2042 2033
2043 ph->p_type = PT_DYNAMIC;2034 ph->p_type = PT_DYNAMIC;
2044 ph->p_offset = dynamic->sh_offset;2035 ph->p_offset = dynamic->sh_offset;
2045 ph->p_vaddr = dynamic->sh_addr;2036 ph->p_vaddr = dynamic->sh_addr;
@@ -2155,14 +2146,14 @@ static int elf_output_file(TCCState *s1, const char *filename) · 109 unmodified lines
2155 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))2146 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
2156 continue;2147 continue;
2157 section_order[sh_order_index++] = i;2148 section_order[sh_order_index++] = i;
2158 2149
2159 file_offset = (file_offset + s->sh_addralign - 1) & 2150 file_offset = (file_offset + s->sh_addralign - 1) &
2160 ~(s->sh_addralign - 1);2151 ~(s->sh_addralign - 1);
2161 s->sh_offset = file_offset;2152 s->sh_offset = file_offset;
2162 if (s->sh_type != SHT_NOBITS)2153 if (s->sh_type != SHT_NOBITS)
2163 file_offset += s->sh_size;2154 file_offset += s->sh_size;
2164 }2155 }
2165 2156
2166 /* if building executable or DLL, then relocate each section2157 /* if building executable or DLL, then relocate each section
2167 except the GOT which is already relocated */2158 except the GOT which is already relocated */
2168 if (file_type != TCC_OUTPUT_OBJ) {2159 if (file_type != TCC_OUTPUT_OBJ) {
@@ -2207,7 +2198,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 38 unmodified lines
2207 else2198 else
2208 mode = 0777;2199 mode = 0777;
2209 unlink(filename);2200 unlink(filename);
2210 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode); 2201 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
2211 if (fd < 0) {2202 if (fd < 0) {
2212 tcc_error_noabort("could not write '%s'", filename);2203 tcc_error_noabort("could not write '%s'", filename);
2213 goto fail;2204 goto fail;
@@ -2223,10 +2214,10 @@ static int elf_output_file(TCCState *s1, const char *filename) · 9 unmodified lines
2223#endif2214#endif
2224 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {2215 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
2225 sort_syms(s1, symtab_section);2216 sort_syms(s1, symtab_section);
2226 2217
2227 /* align to 4 */2218 /* align to 4 */
2228 file_offset = (file_offset + 3) & -4;2219 file_offset = (file_offset + 3) & -4;
2229 2220
2230 /* fill header */2221 /* fill header */
2231 ehdr.e_ident[0] = ELFMAG0;2222 ehdr.e_ident[0] = ELFMAG0;
2232 ehdr.e_ident[1] = ELFMAG1;2223 ehdr.e_ident[1] = ELFMAG1;
@@ -2271,7 +2262,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 38 unmodified lines
2271 ehdr.e_shentsize = sizeof(ElfW(Shdr));2262 ehdr.e_shentsize = sizeof(ElfW(Shdr));
2272 ehdr.e_shnum = shnum;2263 ehdr.e_shnum = shnum;
2273 ehdr.e_shstrndx = shnum - 1;2264 ehdr.e_shstrndx = shnum - 1;
2274 2265
2275 fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);2266 fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
2276 fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);2267 fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
2277 offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));2268 offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
@@ -2296,7 +2287,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 18 unmodified lines
2296 fputc(0, f);2287 fputc(0, f);
2297 offset++;2288 offset++;
2298 }2289 }
2299 2290
2300 for(i=0;i<s1->nb_sections;i++) {2291 for(i=0;i<s1->nb_sections;i++) {
2301 sh = &shdr;2292 sh = &shdr;
2302 memset(sh, 0, sizeof(ElfW(Shdr)));2293 memset(sh, 0, sizeof(ElfW(Shdr)));
@@ -2363,9 +2354,9 @@ typedef struct SectionMergeInfo { · 60 unmodified lines
2363 2354
2364/* load an object file and merge it with current files */2355/* load an object file and merge it with current files */
2365/* XXX: handle correctly stab (debug) info */2356/* XXX: handle correctly stab (debug) info */
2366ST_FUNC int tcc_load_object_file(TCCState *s1, 2357ST_FUNC int tcc_load_object_file(TCCState *s1,
2367 int fd, unsigned long file_offset)2358 int fd, unsigned long file_offset)
2368{ 2359{
2369 ElfW(Ehdr) ehdr;2360 ElfW(Ehdr) ehdr;
2370 ElfW(Shdr) *shdr, *sh;2361 ElfW(Shdr) *shdr, *sh;
2371 int size, i, j, offset, offseti, nb_syms, sym_index, ret;2362 int size, i, j, offset, offseti, nb_syms, sym_index, ret;
@@ -2400,10 +2391,10 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 28 unmodified lines
2400 return -1;2391 return -1;
2401 }2392 }
2402 /* read sections */2393 /* read sections */
2403 shdr = load_data(fd, file_offset + ehdr.e_shoff, 2394 shdr = load_data(fd, file_offset + ehdr.e_shoff,
2404 sizeof(ElfW(Shdr)) * ehdr.e_shnum);2395 sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2405 sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);2396 sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
2406 2397
2407 /* load section names */2398 /* load section names */
2408 sh = &shdr[ehdr.e_shstrndx];2399 sh = &shdr[ehdr.e_shstrndx];
2409 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);2400 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
@@ -2431,7 +2422,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 21 unmodified lines
2431 strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);2422 strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2432 }2423 }
2433 }2424 }
2434 2425
2435 /* now examine each section and try to merge its content with the2426 /* now examine each section and try to merge its content with the
2436 ones in memory */2427 ones in memory */
2437 for(i = 1; i < ehdr.e_shnum; i++) {2428 for(i = 1; i < ehdr.e_shnum; i++) {
@@ -2442,11 +2433,11 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 4 unmodified lines
2442 sh_name = strsec + sh->sh_name;2433 sh_name = strsec + sh->sh_name;
2443 /* ignore sections types we do not handle */2434 /* ignore sections types we do not handle */
2444 if (sh->sh_type != SHT_PROGBITS &&2435 if (sh->sh_type != SHT_PROGBITS &&
2445 sh->sh_type != SHT_RELX && 2436 sh->sh_type != SHT_RELX &&
2446#ifdef TCC_ARM_EABI2437#ifdef TCC_ARM_EABI
2447 sh->sh_type != SHT_ARM_EXIDX &&2438 sh->sh_type != SHT_ARM_EXIDX &&
2448#endif2439#endif
2449 sh->sh_type != SHT_NOBITS && 2440 sh->sh_type != SHT_NOBITS &&
2450 sh->sh_type != SHT_PREINIT_ARRAY &&2441 sh->sh_type != SHT_PREINIT_ARRAY &&
2451 sh->sh_type != SHT_INIT_ARRAY &&2442 sh->sh_type != SHT_INIT_ARRAY &&
2452 sh->sh_type != SHT_FINI_ARRAY &&2443 sh->sh_type != SHT_FINI_ARRAY &&
@@ -2459,7 +2450,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 6 unmodified lines
2459 for(j = 1; j < s1->nb_sections;j++) {2450 for(j = 1; j < s1->nb_sections;j++) {
2460 s = s1->sections[j];2451 s = s1->sections[j];
2461 if (!strcmp(s->name, sh_name)) {2452 if (!strcmp(s->name, sh_name)) {
2462 if (!strncmp(sh_name, ".gnu.linkonce", 2453 if (!strncmp(sh_name, ".gnu.linkonce",
2463 sizeof(".gnu.linkonce") - 1)) {2454 sizeof(".gnu.linkonce") - 1)) {
2464 /* if a 'linkonce' section is already present, we2455 /* if a 'linkonce' section is already present, we
2465 do not add it again. It is a little tricky as2456 do not add it again. It is a little tricky as
@@ -2518,7 +2509,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 52 unmodified lines
2518 next: ;2509 next: ;
2519 }2510 }
2520 2511
2521 /* //gr relocate stab strings */2512 /* gr relocate stab strings */
2522 if (stab_index && stabstr_index) {2513 if (stab_index && stabstr_index) {
2523 Stab_Sym *a, *b;2514 Stab_Sym *a, *b;
2524 unsigned o;2515 unsigned o;
@@ -2526,7 +2517,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 1 unmodified line
2526 a = (Stab_Sym *)(s->data + sm_table[stab_index].offset);2517 a = (Stab_Sym *)(s->data + sm_table[stab_index].offset);
2527 b = (Stab_Sym *)(s->data + s->data_offset);2518 b = (Stab_Sym *)(s->data + s->data_offset);
2528 o = sm_table[stabstr_index].offset;2519 o = sm_table[stabstr_index].offset;
2529 while (a < b) 2520 while (a < b)
2530 a->n_strx += o, a++;2521 a->n_strx += o, a++;
2531 }2522 }
2532 2523
@@ -2577,8 +2568,8 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 44 unmodified lines
2577 }2568 }
2578 /* add symbol */2569 /* add symbol */
2579 name = strtab + sym->st_name;2570 name = strtab + sym->st_name;
2580 sym_index = add_elf_sym(symtab_section, sym->st_value, sym->st_size, 2571 sym_index = add_elf_sym(symtab_section, sym->st_value, sym->st_size,
2581 sym->st_info, sym->st_other, 2572 sym->st_info, sym->st_other,
2582 sym->st_shndx, name);2573 sym->st_shndx, name);
2583 old_to_new_syms[i] = sym_index;2574 old_to_new_syms[i] = sym_index;
2584 }2575 }
@@ -2639,7 +2630,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 54 unmodified lines
2639 break;2630 break;
2640 }2631 }
2641 }2632 }
2642 2633
2643 ret = 0;2634 ret = 0;
2644 the_end:2635 the_end:
2645 tcc_free(symtab);2636 tcc_free(symtab);
@@ -2690,9 +2681,6 @@ static int tcc_load_alacarte(TCCState *s1, int fd, int size) · 44 unmodified lines
2690 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];2681 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
2691 if(sym->st_shndx == SHN_UNDEF) {2682 if(sym->st_shndx == SHN_UNDEF) {
2692 off = get_be32(ar_index + i * 4) + sizeof(ArchiveHeader);2683 off = get_be32(ar_index + i * 4) + sizeof(ArchiveHeader);
2693#if 0
2694 printf("%5d\t%s\t%08x\n", i, p, sym->st_shndx);
2695#endif
2696 ++bound;2684 ++bound;
2697 lseek(fd, off, SEEK_SET);2685 lseek(fd, off, SEEK_SET);
2698 if(tcc_load_object_file(s1, fd, off) < 0) {2686 if(tcc_load_object_file(s1, fd, off) < 0) {
@@ -2722,7 +2710,7 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd) · 23 unmodified lines
2722 2710
2723 /* skip magic which was already checked */2711 /* skip magic which was already checked */
2724 read(fd, magic, sizeof(magic));2712 read(fd, magic, sizeof(magic));
2725 2713
2726 for(;;) {2714 for(;;) {
2727 len = read(fd, &hdr, sizeof(hdr));2715 len = read(fd, &hdr, sizeof(hdr));
2728 if (len == 0)2716 if (len == 0)
@@ -2740,7 +2728,6 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd) · 11 unmodified lines
2740 break;2728 break;
2741 }2729 }
2742 ar_name[i + 1] = '\0';2730 ar_name[i + 1] = '\0';
2743 // printf("name='%s' size=%d %s\n", ar_name, size, ar_size);
2744 file_offset = lseek(fd, 0, SEEK_CUR);2731 file_offset = lseek(fd, 0, SEEK_CUR);
2745 /* align to even */2732 /* align to even */
2746 size = (size + 1) & ~1;2733 size = (size + 1) & ~1;
@@ -2767,7 +2754,7 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd) · 20 unmodified lines
2767 is referenced by the user (so it should be added as DT_NEEDED in2754 is referenced by the user (so it should be added as DT_NEEDED in
2768 the generated ELF file) */2755 the generated ELF file) */
2769ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)2756ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
2770{ 2757{
2771 ElfW(Ehdr) ehdr;2758 ElfW(Ehdr) ehdr;
2772 ElfW(Shdr) *shdr, *sh, *sh1;2759 ElfW(Shdr) *shdr, *sh, *sh1;
2773 int i, j, nb_syms, nb_dts, sym_bind, ret;2760 int i, j, nb_syms, nb_dts, sym_bind, ret;
@@ -2776,7 +2763,7 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 2 unmodified lines
2776 unsigned char *dynstr;2763 unsigned char *dynstr;
2777 const char *name, *soname;2764 const char *name, *soname;
2778 DLLReference *dllref;2765 DLLReference *dllref;
2779 2766
2780 read(fd, &ehdr, sizeof(ehdr));2767 read(fd, &ehdr, sizeof(ehdr));
2781 2768
2782 /* test CPU specific stuff */2769 /* test CPU specific stuff */
@@ -2811,10 +2798,10 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 28 unmodified lines
2811 break;2798 break;
2812 }2799 }
2813 }2800 }
2814 2801
2815 /* compute the real library name */2802 /* compute the real library name */
2816 soname = tcc_basename(filename);2803 soname = tcc_basename(filename);
2817 2804
2818 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {2805 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2819 if (dt->d_tag == DT_SONAME) {2806 if (dt->d_tag == DT_SONAME) {
2820 soname = dynstr + dt->d_un.d_val;2807 soname = dynstr + dt->d_un.d_val;
@@ -2832,8 +2819,6 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 11 unmodified lines
2832 goto the_end;2819 goto the_end;
2833 }2820 }
2834 }2821 }
2835
2836 // printf("loading dll '%s'\n", soname);
2837 2822
2838 /* add the dll and its level */2823 /* add the dll and its level */
2839 dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));2824 dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
@@ -2993,11 +2978,6 @@ static int ld_next(TCCState *s1, char *name, int name_size) · 153 unmodified lines
2993 inp();2978 inp();
2994 break;2979 break;
2995 }2980 }
2996#if 0
2997 printf("tok=%c %d\n", c, c);
2998 if (c == LD_TOK_NAME)
2999 printf(" name=%s\n", name);
3000#endif
3001 return c;2981 return c;
3002}2982}
3003 2983
@@ -3101,7 +3081,7 @@ ST_FUNC int tcc_load_ldscript(TCCState *s1) · 97 unmodified lines
3101 char cmd[64];3081 char cmd[64];
3102 char filename[1024];3082 char filename[1024];
3103 int t, ret;3083 int t, ret;
3104 3084
3105 ch = file->buf_ptr[0];3085 ch = file->buf_ptr[0];
3106 ch = handle_eob();3086 ch = handle_eob();
3107 for(;;) {3087 for(;;) {
@@ -3136,4 +3116,4 @@ ST_FUNC int tcc_load_ldscript(TCCState *s1) · 28 unmodified lines
3136 }3116 }
3137 return 0;3117 return 0;
3138}3118}
3139#endif /* ndef TCC_TARGET_PE */3119#endif /* !TCC_TARGET_PE */

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