Index: contrib/elftoolchain/elfcopy/main.c =================================================================== --- contrib/elftoolchain/elfcopy/main.c +++ contrib/elftoolchain/elfcopy/main.c @@ -372,6 +372,14 @@ create_symtab(ecp); /* + * Write the underlying ehdr. Note that it should be called + * before elf_setshstrndx() since it will overwrite e->e_shstrndx. + */ + if (gelf_update_ehdr(ecp->eout, &oeh) == 0) + errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", + elf_errmsg(-1)); + + /* * First processing of output sections: at this stage we copy the * content of each section from input to output object. Section * content will be modified and printed (mcs) if need. Also content of @@ -380,14 +388,6 @@ */ copy_content(ecp); - /* - * Write the underlying ehdr. Note that it should be called - * before elf_setshstrndx() since it will overwrite e->e_shstrndx. - */ - if (gelf_update_ehdr(ecp->eout, &oeh) == 0) - errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", - elf_errmsg(-1)); - /* Generate section name string table (.shstrtab). */ set_shstrtab(ecp); Index: contrib/elftoolchain/libelf/_libelf.h =================================================================== --- contrib/elftoolchain/libelf/_libelf.h +++ contrib/elftoolchain/libelf/_libelf.h @@ -197,6 +197,7 @@ #ifdef __cplusplus extern "C" { #endif +int _is_mips64el(Elf *e); struct _Libelf_Data *_libelf_allocate_data(Elf_Scn *_s); Elf *_libelf_allocate_elf(void); Elf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx); @@ -222,6 +223,8 @@ size_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version); void *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count); Elf *_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror); +Elf64_Xword _libelf_mips64el_r_info_tof(Elf64_Xword r_info); +Elf64_Xword _libelf_mips64el_r_info_tom(Elf64_Xword r_info); struct _Libelf_Data *_libelf_release_data(struct _Libelf_Data *_d); Elf *_libelf_release_elf(Elf *_e); Elf_Scn *_libelf_release_scn(Elf_Scn *_s); Index: contrib/elftoolchain/libelf/gelf_rel.c =================================================================== --- contrib/elftoolchain/libelf/gelf_rel.c +++ contrib/elftoolchain/libelf/gelf_rel.c @@ -33,6 +33,56 @@ ELFTC_VCSID("$Id: gelf_rel.c 3177 2015-03-30 18:19:41Z emaste $"); +int +_is_mips64el(Elf *e) +{ + + return (e->e_kind == ELF_K_ELF && e->e_byteorder == ELFDATA2LSB && + e->e_u.e_elf.e_ehdr.e_ehdr64->e_machine == EM_MIPS); +} + +/* + * For MIPS64, the r_info field is actually stored as a 32-bit symbol + * index (r_sym) followed by four single-byte fields (r_ssym, r_type3, + * r_type2, and r_type). The byte-swap for the little-endian case + * jumbles this incorrectly so compensate. + */ +Elf64_Xword +_libelf_mips64el_r_info_tof(Elf64_Xword r_info) +{ + Elf64_Xword new_info; + uint8_t ssym, type3, type2, type; + + ssym = r_info >> 24; + type3 = r_info >> 16; + type2 = r_info >> 8; + type = r_info; + new_info = r_info >> 32; + new_info |= (Elf64_Xword)ssym << 32; + new_info |= (Elf64_Xword)type3 << 40; + new_info |= (Elf64_Xword)type2 << 48; + new_info |= (Elf64_Xword)type << 56; + return (new_info); +} + +Elf64_Xword +_libelf_mips64el_r_info_tom(Elf64_Xword r_info) +{ + Elf64_Xword new_info; + uint8_t ssym, type3, type2, type; + + ssym = r_info >> 32; + type3 = r_info >> 40; + type2 = r_info >> 48; + type = r_info >> 56; + new_info = (r_info & 0xffffffff) << 32; + new_info |= (Elf64_Xword)ssym << 24; + new_info |= (Elf64_Xword)type3 << 16; + new_info |= (Elf64_Xword)type2 << 8; + new_info |= (Elf64_Xword)type; + return (new_info); +} + GElf_Rel * gelf_getrel(Elf_Data *ed, int ndx, GElf_Rel *dst) { @@ -90,6 +140,9 @@ rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx; *dst = *rel64; + + if (_is_mips64el(e)) + dst->r_info = _libelf_mips64el_r_info_tom(rel64->r_info); } return (dst); @@ -156,6 +209,9 @@ rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx; *rel64 = *dr; + + if (_is_mips64el(e)) + rel64->r_info = _libelf_mips64el_r_info_tof(dr->r_info); } return (1); Index: contrib/elftoolchain/libelf/gelf_rela.c =================================================================== --- contrib/elftoolchain/libelf/gelf_rela.c +++ contrib/elftoolchain/libelf/gelf_rela.c @@ -91,6 +91,10 @@ rela64 = (Elf64_Rela *) d->d_data.d_buf + ndx; *dst = *rela64; + + if (_is_mips64el(e)) + dst->r_info = + _libelf_mips64el_r_info_tom(rela64->r_info); } return (dst); @@ -159,6 +163,9 @@ rela64 = (Elf64_Rela *) d->d_data.d_buf + ndx; *rela64 = *dr; + + if (_is_mips64el(e)) + rela64->r_info = _libelf_mips64el_r_info_tof(dr->r_info); } return (1);