This reverts commit 3cbc7a2dccf13b96c572623582d6c54394f98c36.
Unified · Split
| @@ -330,12 +330,6 @@ ST_FUNC void put_stabd(int type, int other, int desc) · 329 unmodified lines | ||
| 330 | 330 | put_stabs(NULL, type, other, desc, 0); |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | −/* Browse each elem of type <type> in section <sec> starting at elem <startoff> | |
| 334 | − using variable <elem> */ | |
| 335 | −#define for_each_elem(sec, startoff, elem, type) \ | |
| 336 | − for (elem = (type *) sec->data + startoff; \ | |
| 337 | − elem < (type *) (sec->data + sec->data_offset); elem++) | |
| 338 | − | |
| 339 | 333 | /* In an ELF file symbol table, the local symbols must appear below |
| 340 | 334 | the global and weak ones. Since TCC cannot sort it while generating |
| 341 | 335 | the code, we must do it after. All the relocation tables are also |
| @@ -346,7 +340,7 @@ static void sort_syms(TCCState *s1, Section *s) · 4 unmodified lines | ||
| 346 | 340 | ElfW(Sym) *new_syms; |
| 347 | 341 | int nb_syms, i; |
| 348 | 342 | ElfW(Sym) *p, *q; |
| 349 | − ElfW_Rel *rel; | |
| 343 | + ElfW_Rel *rel, *rel_end; | |
| 350 | 344 | Section *sr; |
| 351 | 345 | int type, sym_index; |
| 352 | 346 | |
| @@ -385,7 +379,10 @@ static void sort_syms(TCCState *s1, Section *s) · 32 unmodified lines | ||
| 385 | 379 | for(i = 1; i < s1->nb_sections; i++) { |
| 386 | 380 | sr = s1->sections[i]; |
| 387 | 381 | if (sr->sh_type == SHT_RELX && sr->link == s) { |
| 388 | − for_each_elem(sr, 0, rel, ElfW_Rel) { | |
| 382 | + rel_end = (ElfW_Rel *)(sr->data + sr->data_offset); | |
| 383 | + for(rel = (ElfW_Rel *)sr->data; | |
| 384 | + rel < rel_end; | |
| 385 | + rel++) { | |
| 389 | 386 | sym_index = ELFW(R_SYM)(rel->r_info); |
| 390 | 387 | type = ELFW(R_TYPE)(rel->r_info); |
| 391 | 388 | sym_index = old_to_new_syms[sym_index]; |
| @@ -400,10 +397,13 @@ static void sort_syms(TCCState *s1, Section *s) · 8 unmodified lines | ||
| 400 | 397 | /* relocate common symbols in the .bss section */ |
| 401 | 398 | ST_FUNC void relocate_common_syms(void) |
| 402 | 399 | { |
| 403 | − ElfW(Sym) *sym; | |
| 400 | + ElfW(Sym) *sym, *sym_end; | |
| 404 | 401 | unsigned long offset, align; |
| 405 | − | |
| 406 | − for_each_elem(symtab_section, 1, sym, ElfW(Sym)) { | |
| 402 | + | |
| 403 | + sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset); | |
| 404 | + for(sym = (ElfW(Sym) *)symtab_section->data + 1; | |
| 405 | + sym < sym_end; | |
| 406 | + sym++) { | |
| 407 | 407 | if (sym->st_shndx == SHN_COMMON) { |
| 408 | 408 | /* align symbol */ |
| 409 | 409 | align = sym->st_value; |
| @@ -421,11 +421,14 @@ ST_FUNC void relocate_common_syms(void) · 11 unmodified lines | ||
| 421 | 421 | true and output error if undefined symbol. */ |
| 422 | 422 | ST_FUNC void relocate_syms(TCCState *s1, int do_resolve) |
| 423 | 423 | { |
| 424 | − ElfW(Sym) *sym, *esym; | |
| 424 | + ElfW(Sym) *sym, *esym, *sym_end; | |
| 425 | 425 | int sym_bind, sh_num, sym_index; |
| 426 | 426 | const char *name; |
| 427 | 427 | |
| 428 | − for_each_elem(symtab_section, 1, sym, ElfW(Sym)) { | |
| 428 | + sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset); | |
| 429 | + for(sym = (ElfW(Sym) *)symtab_section->data + 1; | |
| 430 | + sym < sym_end; | |
| 431 | + sym++) { | |
| 429 | 432 | sh_num = sym->st_shndx; |
| 430 | 433 | if (sh_num == SHN_UNDEF) { |
| 431 | 434 | name = strtab_section->data + sym->st_name; |
| @@ -508,7 +511,7 @@ static addr_t add_jmp_table(TCCState *s1, int val) · 76 unmodified lines | ||
| 508 | 511 | ST_FUNC void relocate_section(TCCState *s1, Section *s) |
| 509 | 512 | { |
| 510 | 513 | Section *sr; |
| 511 | − ElfW_Rel *rel; | |
| 514 | + ElfW_Rel *rel, *rel_end, *qrel; | |
| 512 | 515 | ElfW(Sym) *sym; |
| 513 | 516 | int type, sym_index; |
| 514 | 517 | unsigned char *ptr; |
| @@ -518,7 +521,11 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 3 unmodified lines | ||
| 518 | 521 | #endif |
| 519 | 522 | |
| 520 | 523 | sr = s->reloc; |
| 521 | − for_each_elem(sr, 0, rel, ElfW_Rel) { | |
| 524 | + rel_end = (ElfW_Rel *)(sr->data + sr->data_offset); | |
| 525 | + qrel = (ElfW_Rel *)sr->data; | |
| 526 | + for(rel = qrel; | |
| 527 | + rel < rel_end; | |
| 528 | + rel++) { | |
| 522 | 529 | ptr = s->data + rel->r_offset; |
| 523 | 530 | |
| 524 | 531 | sym_index = ELFW(R_SYM)(rel->r_info); |
| @@ -886,22 +893,27 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 361 unmodified lines | ||
| 886 | 893 | static void relocate_rel(TCCState *s1, Section *sr) |
| 887 | 894 | { |
| 888 | 895 | Section *s; |
| 889 | − ElfW_Rel *rel; | |
| 890 | − | |
| 896 | + ElfW_Rel *rel, *rel_end; | |
| 897 | + | |
| 891 | 898 | s = s1->sections[sr->sh_info]; |
| 892 | − for_each_elem(sr, 0, rel, ElfW_Rel) | |
| 899 | + rel_end = (ElfW_Rel *)(sr->data + sr->data_offset); | |
| 900 | + for(rel = (ElfW_Rel *)sr->data; | |
| 901 | + rel < rel_end; | |
| 902 | + rel++) { | |
| 893 | 903 | rel->r_offset += s->sh_addr; |
| 904 | + } | |
| 894 | 905 | } |
| 895 | 906 | |
| 896 | 907 | /* count the number of dynamic relocations so that we can reserve |
| 897 | 908 | their space */ |
| 898 | 909 | static int prepare_dynamic_rel(TCCState *s1, Section *sr) |
| 899 | 910 | { |
| 900 | − ElfW_Rel *rel; | |
| 911 | + ElfW_Rel *rel, *rel_end; | |
| 901 | 912 | int sym_index, esym_index, type, count; |
| 902 | 913 | |
| 903 | 914 | count = 0; |
| 904 | − for_each_elem(sr, 0, rel, ElfW_Rel) { | |
| 915 | + rel_end = (ElfW_Rel *)(sr->data + sr->data_offset); | |
| 916 | + for(rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++) { | |
| 905 | 917 | sym_index = ELFW(R_SYM)(rel->r_info); |
| 906 | 918 | type = ELFW(R_TYPE)(rel->r_info); |
| 907 | 919 | switch(type) { |
| @@ -1133,7 +1145,7 @@ static void put_got_entry(TCCState *s1, · 225 unmodified lines | ||
| 1133 | 1145 | ST_FUNC void build_got_entries(TCCState *s1) |
| 1134 | 1146 | { |
| 1135 | 1147 | Section *s; |
| 1136 | − ElfW_Rel *rel; | |
| 1148 | + ElfW_Rel *rel, *rel_end; | |
| 1137 | 1149 | ElfW(Sym) *sym; |
| 1138 | 1150 | int i, type, reloc_type, sym_index; |
| 1139 | 1151 | |
| @@ -1144,7 +1156,10 @@ ST_FUNC void build_got_entries(TCCState *s1) · 4 unmodified lines | ||
| 1144 | 1156 | /* no need to handle got relocations */ |
| 1145 | 1157 | if (s->link != symtab_section) |
| 1146 | 1158 | continue; |
| 1147 | − for_each_elem(s, 0, rel, ElfW_Rel) { | |
| 1159 | + rel_end = (ElfW_Rel *)(s->data + s->data_offset); | |
| 1160 | + for(rel = (ElfW_Rel *)s->data; | |
| 1161 | + rel < rel_end; | |
| 1162 | + rel++) { | |
| 1148 | 1163 | type = ELFW(R_TYPE)(rel->r_info); |
| 1149 | 1164 | switch(type) { |
| 1150 | 1165 | #if defined(TCC_TARGET_I386) |
| @@ -1451,11 +1466,12 @@ static void tcc_output_binary(TCCState *s1, FILE *f, · 300 unmodified lines | ||
| 1451 | 1466 | void patch_dynsym_undef(TCCState *s1, Section *s) |
| 1452 | 1467 | { |
| 1453 | 1468 | uint32_t *gotd = (void *)s1->got->data; |
| 1454 | − ElfW(Sym) *sym; | |
| 1469 | + ElfW(Sym) *sym, *sym_end; | |
| 1455 | 1470 | |
| 1456 | 1471 | gotd += 3; /* dummy entries in .got */ |
| 1457 | 1472 | /* relocate symbols in .dynsym */ |
| 1458 | − for_each_elem(s, 1, sym, ElfW(Sym)) { | |
| 1473 | + sym_end = (ElfW(Sym) *)(s->data + s->data_offset); | |
| 1474 | + for (sym = (ElfW(Sym) *)s->data + 1; sym < sym_end; sym++) { | |
| 1459 | 1475 | if (sym->st_shndx == SHN_UNDEF) { |
| 1460 | 1476 | *gotd++ = sym->st_value + 6; /* XXX 6 is magic ? */ |
| 1461 | 1477 | sym->st_value = 0; |
| @@ -1469,9 +1485,10 @@ void patch_dynsym_undef(TCCState *s1, Section *s) · 7 unmodified lines | ||
| 1469 | 1485 | /* zero plt offsets of weak symbols in .dynsym */ |
| 1470 | 1486 | void patch_dynsym_undef(TCCState *s1, Section *s) |
| 1471 | 1487 | { |
| 1472 | − ElfW(Sym) *sym; | |
| 1488 | + ElfW(Sym) *sym, *sym_end; | |
| 1473 | 1489 | |
| 1474 | − for_each_elem(s, 1, sym, ElfW(Sym)) | |
| 1490 | + sym_end = (ElfW(Sym) *)(s->data + s->data_offset); | |
| 1491 | + for (sym = (ElfW(Sym) *)s->data + 1; sym < sym_end; sym++) | |
| 1475 | 1492 | if (sym->st_shndx == SHN_UNDEF && ELFW(ST_BIND)(sym->st_info) == STB_WEAK) |
| 1476 | 1493 | sym->st_value = 0; |
| 1477 | 1494 | } |
| @@ -1497,7 +1514,7 @@ ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel) · 19 unmodified lines | ||
| 1497 | 1514 | ST_FUNC void fill_got(TCCState *s1) |
| 1498 | 1515 | { |
| 1499 | 1516 | Section *s; |
| 1500 | − ElfW_Rel *rel; | |
| 1517 | + ElfW_Rel *rel, *rel_end; | |
| 1501 | 1518 | int i; |
| 1502 | 1519 | |
| 1503 | 1520 | for(i = 1; i < s1->nb_sections; i++) { |
| @@ -1507,7 +1524,8 @@ ST_FUNC void fill_got(TCCState *s1) · 3 unmodified lines | ||
| 1507 | 1524 | /* no need to handle got relocations */ |
| 1508 | 1525 | if (s->link != symtab_section) |
| 1509 | 1526 | continue; |
| 1510 | − for_each_elem(s, 0, rel, ElfW_Rel) { | |
| 1527 | + rel_end = (ElfW_Rel *) (s->data + s->data_offset); | |
| 1528 | + for(rel = (ElfW_Rel *) s->data; rel < rel_end; rel++) { | |
| 1511 | 1529 | switch (ELFW(R_TYPE) (rel->r_info)) { |
| 1512 | 1530 | case R_X86_64_GOT32: |
| 1513 | 1531 | case R_X86_64_GOTPCREL: |
| @@ -1565,7 +1583,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 51 unmodified lines | ||
| 1565 | 1583 | if (!s1->static_link) { |
| 1566 | 1584 | const char *name; |
| 1567 | 1585 | int sym_index, index; |
| 1568 | − ElfW(Sym) *esym; | |
| 1586 | + ElfW(Sym) *esym, *sym_end; | |
| 1569 | 1587 | |
| 1570 | 1588 | if (file_type == TCC_OUTPUT_EXE) { |
| 1571 | 1589 | char *ptr; |
| @@ -1604,8 +1622,12 @@ static int elf_output_file(TCCState *s1, const char *filename) · 32 unmodified lines | ||
| 1604 | 1622 | is found, then we add it in the PLT. If a symbol |
| 1605 | 1623 | STT_OBJECT is found, we add it in the .bss section with |
| 1606 | 1624 | a suitable relocation */ |
| 1625 | + sym_end = (ElfW(Sym) *)(symtab_section->data + | |
| 1626 | + symtab_section->data_offset); | |
| 1607 | 1627 | if (file_type == TCC_OUTPUT_EXE) { |
| 1608 | − for_each_elem(symtab_section, 1, sym, ElfW(Sym)) { | |
| 1628 | + for(sym = (ElfW(Sym) *)symtab_section->data + 1; | |
| 1629 | + sym < sym_end; | |
| 1630 | + sym++) { | |
| 1609 | 1631 | if (sym->st_shndx == SHN_UNDEF) { |
| 1610 | 1632 | name = symtab_section->link->data + sym->st_name; |
| 1611 | 1633 | sym_index = find_elf_sym(s1->dynsymtab_section, name); |
| @@ -1626,7 +1648,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 14 unmodified lines | ||
| 1626 | 1648 | sym - (ElfW(Sym) *)symtab_section->data); |
| 1627 | 1649 | } else if (type == STT_OBJECT) { |
| 1628 | 1650 | unsigned long offset; |
| 1629 | − ElfW(Sym) *dynsym; | |
| 1651 | + ElfW(Sym) *dynsym, *dynsym_end; | |
| 1630 | 1652 | offset = bss_section->data_offset; |
| 1631 | 1653 | /* XXX: which alignment ? */ |
| 1632 | 1654 | offset = (offset + 16 - 1) & -16; |
| @@ -1635,7 +1657,11 @@ static int elf_output_file(TCCState *s1, const char *filename) · 2 unmodified lines | ||
| 1635 | 1657 | bss_section->sh_num, name); |
| 1636 | 1658 | /* Ensure R_COPY works for weak symbol aliases */ |
| 1637 | 1659 | if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) { |
| 1638 | − for_each_elem(s1->dynsymtab_section, 1, dynsym, ElfW(Sym)) { | |
| 1660 | + dynsym_end = (ElfW(Sym) *) | |
| 1661 | + (s1->dynsymtab_section->data + | |
| 1662 | + s1->dynsymtab_section->data_offset); | |
| 1663 | + for(dynsym = (ElfW(Sym) *)s1->dynsymtab_section->data + 1; | |
| 1664 | + dynsym < dynsym_end; dynsym++) { | |
| 1639 | 1665 | if ((dynsym->st_value == esym->st_value) |
| 1640 | 1666 | && (ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL)) { |
| 1641 | 1667 | char *dynname; |
| @@ -1681,7 +1707,11 @@ static int elf_output_file(TCCState *s1, const char *filename) · 39 unmodified lines | ||
| 1681 | 1707 | |
| 1682 | 1708 | /* now look at unresolved dynamic symbols and export |
| 1683 | 1709 | corresponding symbol */ |
| 1684 | − for_each_elem(s1->dynsymtab_section, 1, esym, ElfW(Sym)) { | |
| 1710 | + sym_end = (ElfW(Sym) *)(s1->dynsymtab_section->data + | |
| 1711 | + s1->dynsymtab_section->data_offset); | |
| 1712 | + for(esym = (ElfW(Sym) *)s1->dynsymtab_section->data + 1; | |
| 1713 | + esym < sym_end; | |
| 1714 | + esym++) { | |
| 1685 | 1715 | if (esym->st_shndx == SHN_UNDEF) { |
| 1686 | 1716 | name = s1->dynsymtab_section->link->data + esym->st_name; |
| 1687 | 1717 | sym_index = find_elf_sym(symtab_section, name); |
| @@ -1706,7 +1736,9 @@ static int elf_output_file(TCCState *s1, const char *filename) · 18 unmodified lines | ||
| 1706 | 1736 | /* shared library case : we simply export all the global symbols */ |
| 1707 | 1737 | nb_syms = symtab_section->data_offset / sizeof(ElfW(Sym)); |
| 1708 | 1738 | s1->symtab_to_dynsym = tcc_mallocz(sizeof(int) * nb_syms); |
| 1709 | − for_each_elem(symtab_section, 1, sym, ElfW(Sym)) { | |
| 1739 | + for(sym = (ElfW(Sym) *)symtab_section->data + 1; | |
| 1740 | + sym < sym_end; | |
| 1741 | + sym++) { | |
| 1710 | 1742 | if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { |
| 1711 | 1743 | #if defined(TCC_OUTPUT_DLL_WITH_PLT) |
| 1712 | 1744 | if ((ELFW(ST_TYPE)(sym->st_info) == STT_FUNC || |
| @@ -1995,6 +2027,8 @@ static int elf_output_file(TCCState *s1, const char *filename) · 282 unmodified lines | ||
| 1995 | 2027 | |
| 1996 | 2028 | /* if dynamic section, then add corresponing program header */ |
| 1997 | 2029 | if (dynamic) { |
| 2030 | + ElfW(Sym) *sym_end; | |
| 2031 | + | |
| 1998 | 2032 | ph = &phdr[phnum - 1]; |
| 1999 | 2033 | |
| 2000 | 2034 | ph->p_type = PT_DYNAMIC; |
| @@ -2056,7 +2090,10 @@ static int elf_output_file(TCCState *s1, const char *filename) · 55 unmodified lines | ||
| 2056 | 2090 | } |
| 2057 | 2091 | |
| 2058 | 2092 | /* relocate symbols in .dynsym */ |
| 2059 | − for_each_elem(s1->dynsym, 1, sym, ElfW(Sym)) { | |
| 2093 | + sym_end = (ElfW(Sym) *)(s1->dynsym->data + s1->dynsym->data_offset); | |
| 2094 | + for(sym = (ElfW(Sym) *)s1->dynsym->data + 1; | |
| 2095 | + sym < sym_end; | |
| 2096 | + sym++) { | |
| 2060 | 2097 | if (sym->st_shndx == SHN_UNDEF) { |
| 2061 | 2098 | /* relocate to the PLT if the symbol corresponds |
| 2062 | 2099 | to a PLT entry */ |
| @@ -2328,7 +2365,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 265 unmodified lines | ||
| 2328 | 2365 | char *sh_name, *name; |
| 2329 | 2366 | SectionMergeInfo *sm_table, *sm; |
| 2330 | 2367 | ElfW(Sym) *sym, *symtab; |
| 2331 | − ElfW_Rel *rel; | |
| 2368 | + ElfW_Rel *rel, *rel_end; | |
| 2332 | 2369 | Section *s; |
| 2333 | 2370 | |
| 2334 | 2371 | int stab_index; |
| @@ -2548,7 +2585,10 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 213 unmodified lines | ||
| 2548 | 2585 | case SHT_RELX: |
| 2549 | 2586 | /* take relocation offset information */ |
| 2550 | 2587 | offseti = sm_table[sh->sh_info].offset; |
| 2551 | − for_each_elem(s, (offset / sizeof(*rel)), rel, ElfW_Rel) { | |
| 2588 | + rel_end = (ElfW_Rel *)(s->data + s->data_offset); | |
| 2589 | + for(rel = (ElfW_Rel *)(s->data + offset); | |
| 2590 | + rel < rel_end; | |
| 2591 | + rel++) { | |
| 2552 | 2592 | int type; |
| 2553 | 2593 | unsigned sym_index; |
| 2554 | 2594 | /* convert symbol index */ |