History

Fix bind problem macho

The problem occurs when a shared library creates a pointer to a function and then the application also uses this function.

herman ten brugge committed 3 years ago

Commit da0cc611 parent 62d857a1 file · +14 −2Browse files

Unified · Split

tccmacho.c+14−2
@@ -665,7 +665,7 @@ static void check_relocs(TCCState *s1, struct macho *mo) · 664 unmodified lines
665665 Section *s;
666666 ElfW_Rel *rel, save_rel;
667667 ElfW(Sym) *sym;
668− int i, type, gotplt_entry, sym_index, for_code;
668+ int i, j, type, gotplt_entry, sym_index, for_code;
669669 uint32_t *pi, *goti;
670670 struct sym_attr *attr;
671671
@@ -711,8 +711,8 @@ static void check_relocs(TCCState *s1, struct macho *mo) · 39 unmodified lines
711711 && type == R_AARCH64_ADR_GOT_PAGE
712712 #endif
713713 ) {
714+ attr->plt_offset = -mo->n_bind_rebase - 1;
714715 bind_rebase_add(mo, 1, s1->got->reloc->sh_info, &save_rel, attr);
715− attr->plt_offset = 0; // ignore next bind
716716 s1->got->reloc->data_offset -= sizeof (ElfW_Rel);
717717 }
718718 if (for_code && sym->st_shndx == SHN_UNDEF)
@@ -720,6 +720,11 @@ static void check_relocs(TCCState *s1, struct macho *mo) · 1 unmodified line
720720 }
721721 }
722722 if (for_code && sym->st_shndx == SHN_UNDEF) {
723+ if ((int)attr->plt_offset < -1) {
724+ /* remove above bind and replace with plt */
725+ mo->bind_rebase[-attr->plt_offset - 1].bind = 2;
726+ attr->plt_offset = -1;
727+ }
723728 if (attr->plt_offset == -1) {
724729 uint8_t *jmp;
725730
@@ -764,6 +769,13 @@ static void check_relocs(TCCState *s1, struct macho *mo) · 38 unmodified lines
764769 s->sh_info, &save_rel, NULL);
765770 }
766771 }
772+ /* remove deleted binds */
773+ for (i = 0, j = 0; i < mo->n_bind_rebase; i++)
774+ if (mo->bind_rebase[i].bind == 2)
775+ mo->n_bind--;
776+ else
777+ mo->bind_rebase[j++] = mo->bind_rebase[i];
778+ mo->n_bind_rebase = j;
767779 pi = section_ptr_add(mo->indirsyms, mo->n_got * sizeof(*pi));
768780 memcpy(pi, goti, mo->n_got * sizeof(*pi));
769781 tcc_free(goti);

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