Index: head/contrib/elftoolchain/elfcopy/main.c =================================================================== --- head/contrib/elftoolchain/elfcopy/main.c +++ head/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 @@ -379,14 +387,6 @@ * to symbol table changes. */ 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: head/contrib/elftoolchain/libelf/Makefile =================================================================== --- head/contrib/elftoolchain/libelf/Makefile +++ head/contrib/elftoolchain/libelf/Makefile @@ -35,6 +35,7 @@ gelf_ehdr.c \ gelf_getclass.c \ gelf_fsize.c \ + gelf_mips64el.c \ gelf_move.c \ gelf_phdr.c \ gelf_rel.c \ Index: head/contrib/elftoolchain/libelf/_libelf.h =================================================================== --- head/contrib/elftoolchain/libelf/_libelf.h +++ head/contrib/elftoolchain/libelf/_libelf.h @@ -216,12 +216,15 @@ void *_libelf_getphdr(Elf *_e, int _elfclass); void *_libelf_getshdr(Elf_Scn *_scn, int _elfclass); void _libelf_init_elf(Elf *_e, Elf_Kind _kind); +int _libelf_is_mips64el(Elf *e); int _libelf_load_section_headers(Elf *e, void *ehdr); unsigned int _libelf_malign(Elf_Type _t, int _elfclass); Elf *_libelf_memory(unsigned char *_image, size_t _sz, int _reporterror); 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: head/contrib/elftoolchain/libelf/gelf_mips64el.c =================================================================== --- head/contrib/elftoolchain/libelf/gelf_mips64el.c +++ head/contrib/elftoolchain/libelf/gelf_mips64el.c @@ -0,0 +1,81 @@ +/*- + * Copyright (c) 2018 John Baldwin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include "_libelf.h" + +ELFTC_VCSID("$Id$"); + +int +_libelf_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); +} Index: head/contrib/elftoolchain/libelf/gelf_rel.c =================================================================== --- head/contrib/elftoolchain/libelf/gelf_rel.c +++ head/contrib/elftoolchain/libelf/gelf_rel.c @@ -90,6 +90,9 @@ rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx; *dst = *rel64; + + if (_libelf_is_mips64el(e)) + dst->r_info = _libelf_mips64el_r_info_tom(rel64->r_info); } return (dst); @@ -156,6 +159,9 @@ rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx; *rel64 = *dr; + + if (_libelf_is_mips64el(e)) + rel64->r_info = _libelf_mips64el_r_info_tof(dr->r_info); } return (1); Index: head/contrib/elftoolchain/libelf/gelf_rela.c =================================================================== --- head/contrib/elftoolchain/libelf/gelf_rela.c +++ head/contrib/elftoolchain/libelf/gelf_rela.c @@ -91,6 +91,10 @@ rela64 = (Elf64_Rela *) d->d_data.d_buf + ndx; *dst = *rela64; + + if (_libelf_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 (_libelf_is_mips64el(e)) + rela64->r_info = _libelf_mips64el_r_info_tof(dr->r_info); } return (1); Index: head/sys/sys/param.h =================================================================== --- head/sys/sys/param.h +++ head/sys/sys/param.h @@ -60,7 +60,7 @@ * in the range 5 to 9. */ #undef __FreeBSD_version -#define __FreeBSD_version 1200083 /* Master, propagated to newvers */ +#define __FreeBSD_version 1200084 /* Master, propagated to newvers */ /* * __FreeBSD_kernel__ indicates that this system uses the kernel of FreeBSD,