Unified · Split
| @@ -1508,7 +1508,8 @@ static void *load_data(int fd, unsigned long file_offset, unsigned long size) · 1507 unmodified lines | ||
| 1508 | 1508 | typedef struct SectionMergeInfo { |
| 1509 | 1509 | Section *s; /* corresponding existing section */ |
| 1510 | 1510 | unsigned long offset; /* offset of the new section in the existing section */ |
| 1511 | − int new_section; /* true if section 's' was added */ | |
| 1511 | + uint8_t new_section; /* true if section 's' was added */ | |
| 1512 | + uint8_t link_once; /* true if link once section */ | |
| 1512 | 1513 | } SectionMergeInfo; |
| 1513 | 1514 | |
| 1514 | 1515 | /* load an object file and merge it with current files */ |
| @@ -1595,8 +1596,19 @@ static int tcc_load_object_file(TCCState *s1, · 80 unmodified lines | ||
| 1595 | 1596 | /* find corresponding section, if any */ |
| 1596 | 1597 | for(j = 1; j < s1->nb_sections;j++) { |
| 1597 | 1598 | s = s1->sections[j]; |
| 1598 | − if (!strcmp(s->name, sh_name)) | |
| 1599 | − goto found; | |
| 1599 | + if (!strcmp(s->name, sh_name)) { | |
| 1600 | + if (!strncmp(sh_name, ".gnu.linkonce", | |
| 1601 | + sizeof(".gnu.linkonce") - 1)) { | |
| 1602 | + /* if a 'linkonce' section is already present, we | |
| 1603 | + do not add it again. It is a little tricky as | |
| 1604 | + symbols can still be defined in | |
| 1605 | + it. */ | |
| 1606 | + sm_table[i].link_once = 1; | |
| 1607 | + goto next; | |
| 1608 | + } else { | |
| 1609 | + goto found; | |
| 1610 | + } | |
| 1611 | + } | |
| 1600 | 1612 | } |
| 1601 | 1613 | /* not found: create new section */ |
| 1602 | 1614 | s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags); |
| @@ -1630,6 +1642,7 @@ static int tcc_load_object_file(TCCState *s1, · 27 unmodified lines | ||
| 1630 | 1642 | } else { |
| 1631 | 1643 | s->data_offset += size; |
| 1632 | 1644 | } |
| 1645 | + next: ; | |
| 1633 | 1646 | } |
| 1634 | 1647 | |
| 1635 | 1648 | /* second short pass to update sh_link and sh_info fields of new |
| @@ -1657,6 +1670,18 @@ static int tcc_load_object_file(TCCState *s1, · 21 unmodified lines | ||
| 1657 | 1670 | if (sym->st_shndx != SHN_UNDEF && |
| 1658 | 1671 | sym->st_shndx < SHN_LORESERVE) { |
| 1659 | 1672 | sm = &sm_table[sym->st_shndx]; |
| 1673 | + if (sm->link_once) { | |
| 1674 | + /* if a symbol is in a link once section, we use the | |
| 1675 | + already defined symbol. It is very important to get | |
| 1676 | + correct relocations */ | |
| 1677 | + if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) { | |
| 1678 | + name = strtab + sym->st_name; | |
| 1679 | + sym_index = find_elf_sym(symtab_section, name); | |
| 1680 | + if (sym_index) | |
| 1681 | + old_to_new_syms[i] = sym_index; | |
| 1682 | + } | |
| 1683 | + continue; | |
| 1684 | + } | |
| 1660 | 1685 | /* if no corresponding section added, no need to add symbol */ |
| 1661 | 1686 | if (!sm->s) |
| 1662 | 1687 | continue; |