Source: elfutils Version: 0.159-4 Severity: wishlist Tags: patch Hi,
I've added some basic support for mips64 to elfutils. I haven't done extensive testing, but it passes the testsuite (with 7 skips like the mips32 version) and appears to work properly. Patch 1 prevents elfutils from claiming all mips executables are big-endian when they're not (although originally this was correct, it isn't anymore and is now very confusing). Patch 2 adds support for the 64-bit abis in mips_retval.c Patch 3 adjusts the relocation code which is needed because mips64 uses a different relocation format to everyone else. The patch makes automatically converts it so that it appears correct to clients. I know this is a bit of a hack, but I can't see how else it could be fixed. The patch also contains a modification to strip so that it can detect that it's processing a mips elf file. Thanks, James
From 77cb4a53c270d5854d3af24f19547bc3de825233 Mon Sep 17 00:00:00 2001 From: James Cowgill <[email protected]> Date: Mon, 5 Jan 2015 15:16:58 +0000 Subject: [PATCH 1/3] Ignore differences between mips machine identifiers Little endian binaries actually use EM_MIPS so you can't tell the endianness from the elf machine id. Also, the EM_MIPS_RS3_LE machine is dead anyway (the kernel will not load binaries containing it). Signed-off-by: James Cowgill <[email protected]> --- backends/mips_init.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/backends/mips_init.c b/backends/mips_init.c index 7429a89..d10e940 100644 --- a/backends/mips_init.c +++ b/backends/mips_init.c @@ -46,11 +46,7 @@ mips_init (elf, machine, eh, ehlen) return NULL; /* We handle it. */ - if (machine == EM_MIPS) - eh->name = "MIPS R3000 big-endian"; - else if (machine == EM_MIPS_RS3_LE) - eh->name = "MIPS R3000 little-endian"; - + eh->name = "MIPS"; mips_init_reloc (eh); HOOK (eh, reloc_simple_type); HOOK (eh, return_value_location); -- 2.1.4
From fdaab18a65ed2529656baa64cb6169f34d7e507b Mon Sep 17 00:00:00 2001 From: James Cowgill <[email protected]> Date: Mon, 5 Jan 2015 15:17:01 +0000 Subject: [PATCH 2/3] Add support for mips64 abis in mips_retval.c Signed-off-by: James Cowgill <[email protected]> --- backends/mips_retval.c | 104 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 94 insertions(+), 10 deletions(-) diff --git a/backends/mips_retval.c b/backends/mips_retval.c index 33f12a7..d5c6ef0 100644 --- a/backends/mips_retval.c +++ b/backends/mips_retval.c @@ -91,6 +91,8 @@ enum mips_abi find_mips_abi(Elf *elf) default: if ((elf_flags & EF_MIPS_ABI2)) return MIPS_ABI_N32; + else if ((ehdr->e_ident[EI_CLASS] == ELFCLASS64)) + return MIPS_ABI_N64; } /* GCC creates a pseudo-section whose name describes the ABI. */ @@ -195,6 +197,57 @@ static const Dwarf_Op loc_aggregate[] = }; #define nloc_aggregate 1 +/* Test if a struct member is a float */ +static int is_float_child(Dwarf_Die *childdie) +{ + /* Test if this is actually a struct member */ + if (dwarf_tag(childdie) != DW_TAG_member) + return 0; + + /* Get type of member */ + Dwarf_Attribute attr_mem; + Dwarf_Die child_type_mem; + Dwarf_Die *child_typedie = + dwarf_formref_die(dwarf_attr_integrate(childdie, + DW_AT_type, + &attr_mem), &child_type_mem); + + if (dwarf_tag(child_typedie) != DW_TAG_base_type) + return 0; + + /* Get base subtype */ + Dwarf_Word encoding; + if (dwarf_formudata (dwarf_attr_integrate (child_typedie, + DW_AT_encoding, + &attr_mem), &encoding) != 0) + return 0; + + return encoding == DW_ATE_float; +} + +/* Returns the number of fpregs which can be returned in the given struct */ +static int get_struct_fpregs(Dwarf_Die *structtypedie) +{ + Dwarf_Die child_mem; + int fpregs = 0; + + /* Get first structure member */ + if (dwarf_child(structtypedie, &child_mem) != 0) + return 0; + + do + { + /* Ensure this register is a float */ + if (!is_float_child(&child_mem)) + return 0; + + fpregs++; + } + while (dwarf_siblingof (&child_mem, &child_mem) == 0); + + return fpregs; +} + int mips_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) { @@ -240,6 +293,7 @@ mips_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) tag = dwarf_tag (typedie); } + Dwarf_Word size; switch (tag) { case -1: @@ -258,8 +312,6 @@ mips_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) case DW_TAG_enumeration_type: case DW_TAG_pointer_type: case DW_TAG_ptr_to_member_type: - { - Dwarf_Word size; if (dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem), &size) != 0) { @@ -289,7 +341,7 @@ mips_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) if (size <= 4*regsize && abi == MIPS_ABI_O32) return nloc_fpregquad; - goto aggregate; + goto large; } } *locp = ABI_LOC(loc_intreg, regsize); @@ -298,18 +350,50 @@ mips_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) if (size <= 2*regsize) return nloc_intregpair; - /* Else fall through. Shouldn't happen though (at least with gcc) */ - } + /* Else pass in memory. Shouldn't happen though (at least with gcc) */ + goto large; case DW_TAG_structure_type: case DW_TAG_class_type: case DW_TAG_union_type: - case DW_TAG_array_type: - aggregate: - /* XXX TODO: Can't handle structure return with other ABI's yet :-/ */ - if ((abi != MIPS_ABI_O32) && (abi != MIPS_ABI_O64)) - return -2; + /* Handle special cases for structures <= 128 bytes in newer ABIs */ + if (abi == MIPS_ABI_EABI32 || abi == MIPS_ABI_EABI64 || + abi == MIPS_ABI_N32 || abi == MIPS_ABI_N64) + { + if (dwarf_aggregate_size (typedie, &size) == 0 && size <= 16) + { + /* + * Special case in N64 / N32 - + * structures containing only floats are returned in fp regs. + * Everything else is returned in integer regs. + */ + if (tag != DW_TAG_union_type && + (abi == MIPS_ABI_N32 || abi == MIPS_ABI_N64)) + { + int num_fpregs = get_struct_fpregs(typedie); + if (num_fpregs == 1 || num_fpregs == 2) + { + *locp = loc_fpreg; + if (num_fpregs == 1) + return nloc_fpreg; + else + return nloc_fpregpair; + } + } + + *locp = loc_intreg; + if (size <= 8) + return nloc_intreg; + else + return nloc_intregpair; + } + } + + /* Fallthrough to handle large types */ + case DW_TAG_array_type: + large: + /* Return large structures in memory */ *locp = loc_aggregate; return nloc_aggregate; } -- 2.1.4
From 59d4b8c48e5040af7e02b34eb26ea602ec82a38e Mon Sep 17 00:00:00 2001 From: James Cowgill <[email protected]> Date: Mon, 5 Jan 2015 15:17:02 +0000 Subject: [PATCH 3/3] Add mips n64 relocation format hack MIPSEL N64 ELF files use a slightly different format for storing relocation entries which is incompatible with the normal R_SYM / R_INFO macros. To workaround this, we rearrange the bytes in the relocation's r_info field when reading and writing the relocations. This patch also ensures that strip.c sets the correct value of e_machine before manipulating relocations so that these changes take effect. Signed-off-by: James Cowgill <[email protected]> --- libelf/gelf_getrel.c | 25 +++++++++++++++++++++++-- libelf/gelf_getrela.c | 25 +++++++++++++++++++++++-- libelf/gelf_update_rel.c | 20 +++++++++++++++++++- libelf/gelf_update_rela.c | 20 +++++++++++++++++++- src/strip.c | 17 +++++++++++++++++ 5 files changed, 101 insertions(+), 6 deletions(-) diff --git a/libelf/gelf_getrel.c b/libelf/gelf_getrel.c index 1f786ff..f8c0f37 100644 --- a/libelf/gelf_getrel.c +++ b/libelf/gelf_getrel.c @@ -36,6 +36,7 @@ #include "libelfP.h" +#define EF_MIPS_ABI 0x0000F000 GElf_Rel * gelf_getrel (data, ndx, dst) @@ -92,8 +93,28 @@ gelf_getrel (data, ndx, dst) result = NULL; } else - result = memcpy (dst, &((Elf64_Rel *) data_scn->d.d_buf)[ndx], - sizeof (Elf64_Rel)); + { + GElf_Ehdr hdr; + result = memcpy (dst, &((Elf64_Rela *) data_scn->d.d_buf)[ndx], + sizeof (Elf64_Rela)); + + if (gelf_getehdr(scn->elf, &hdr) != NULL && + hdr.e_ident[EI_DATA] == ELFDATA2LSB && + hdr.e_machine == EM_MIPS && + (hdr.e_flags & EF_MIPS_ABI) == 0) + { + /* + * The relocation format is mangled on MIPSEL N64 + * We'll adjust it so at least R_SYM will work on it + */ + GElf_Xword r_info = dst->r_info; + dst->r_info = (r_info << 32) | + ((r_info >> 8) & 0xFF000000) | + ((r_info >> 24) & 0x00FF0000) | + ((r_info >> 40) & 0x0000FF00) | + ((r_info >> 56) & 0x000000FF); + } + } } rwlock_unlock (scn->elf->lock); diff --git a/libelf/gelf_getrela.c b/libelf/gelf_getrela.c index cead7ee..725442d 100644 --- a/libelf/gelf_getrela.c +++ b/libelf/gelf_getrela.c @@ -36,6 +36,7 @@ #include "libelfP.h" +#define EF_MIPS_ABI 0x0000F000 GElf_Rela * gelf_getrela (data, ndx, dst) @@ -93,8 +94,28 @@ gelf_getrela (data, ndx, dst) result = NULL; } else - result = memcpy (dst, &((Elf64_Rela *) data_scn->d.d_buf)[ndx], - sizeof (Elf64_Rela)); + { + GElf_Ehdr hdr; + result = memcpy (dst, &((Elf64_Rela *) data_scn->d.d_buf)[ndx], + sizeof (Elf64_Rela)); + + if (gelf_getehdr(scn->elf, &hdr) != NULL && + hdr.e_ident[EI_DATA] == ELFDATA2LSB && + hdr.e_machine == EM_MIPS && + (hdr.e_flags & EF_MIPS_ABI) == 0) + { + /* + * The relocation format is mangled on MIPSEL N64 + * We'll adjust it so at least R_SYM will work on it + */ + GElf_Xword r_info = dst->r_info; + dst->r_info = (r_info << 32) | + ((r_info >> 8) & 0xFF000000) | + ((r_info >> 24) & 0x00FF0000) | + ((r_info >> 40) & 0x0000FF00) | + ((r_info >> 56) & 0x000000FF); + } + } } rwlock_unlock (scn->elf->lock); diff --git a/libelf/gelf_update_rel.c b/libelf/gelf_update_rel.c index 14f62e9..9095556 100644 --- a/libelf/gelf_update_rel.c +++ b/libelf/gelf_update_rel.c @@ -36,6 +36,7 @@ #include "libelfP.h" +#define EF_MIPS_ABI 0x0000F000 int gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src) @@ -86,6 +87,9 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src) } else { + GElf_Ehdr hdr; + GElf_Rel value = *src; + /* Check whether we have to resize the data buffer. */ if (INVALID_NDX (ndx, Elf64_Rel, &data_scn->d)) { @@ -93,7 +97,21 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src) goto out; } - ((Elf64_Rel *) data_scn->d.d_buf)[ndx] = *src; + if (gelf_getehdr(scn->elf, &hdr) != NULL && + hdr.e_ident[EI_DATA] == ELFDATA2LSB && + hdr.e_machine == EM_MIPS && + (hdr.e_flags & EF_MIPS_ABI) == 0) + { + /* Undo the MIPSEL N64 hack from gelf_getrel */ + GElf_Xword r_info = value.r_info; + value.r_info = (r_info >> 32) | + ((r_info << 8) & 0x000000FF00000000) | + ((r_info << 24) & 0x0000FF0000000000) | + ((r_info << 40) & 0x00FF000000000000) | + ((r_info << 56) & 0xFF00000000000000); + } + + ((Elf64_Rel *) data_scn->d.d_buf)[ndx] = value; } result = 1; diff --git a/libelf/gelf_update_rela.c b/libelf/gelf_update_rela.c index 8825270..4caa85f 100644 --- a/libelf/gelf_update_rela.c +++ b/libelf/gelf_update_rela.c @@ -36,6 +36,7 @@ #include "libelfP.h" +#define EF_MIPS_ABI 0x0000F000 int gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src) @@ -89,6 +90,9 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src) } else { + GElf_Ehdr hdr; + GElf_Rela value = *src; + /* Check whether we have to resize the data buffer. */ if (INVALID_NDX (ndx, Elf64_Rela, &data_scn->d)) { @@ -96,7 +100,21 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src) goto out; } - ((Elf64_Rela *) data_scn->d.d_buf)[ndx] = *src; + if (gelf_getehdr(scn->elf, &hdr) != NULL && + hdr.e_ident[EI_DATA] == ELFDATA2LSB && + hdr.e_machine == EM_MIPS && + (hdr.e_flags & EF_MIPS_ABI) == 0) + { + /* Undo the MIPSEL N64 hack from gelf_getrel */ + GElf_Xword r_info = value.r_info; + value.r_info = (r_info >> 32) | + ((r_info << 8) & 0x000000FF00000000) | + ((r_info << 24) & 0x0000FF0000000000) | + ((r_info << 40) & 0x00FF000000000000) | + ((r_info << 56) & 0xFF00000000000000); + } + + ((Elf64_Rela *) data_scn->d.d_buf)[ndx] = value; } result = 1; diff --git a/src/strip.c b/src/strip.c index ebe18a9..53dbc81 100644 --- a/src/strip.c +++ b/src/strip.c @@ -536,6 +536,23 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname, goto fail; } + /* Copy identity part of the ELF header now */ + newehdr = gelf_getehdr (newelf, &newehdr_mem); + if (newehdr == NULL) + INTERNAL_ERROR (fname); + + memcpy (newehdr->e_ident, ehdr->e_ident, EI_NIDENT); + newehdr->e_type = ehdr->e_type; + newehdr->e_machine = ehdr->e_machine; + newehdr->e_version = ehdr->e_version; + + if (gelf_update_ehdr (newelf, newehdr) == 0) + { + error (0, 0, gettext ("%s: error while creating ELF header: %s"), + fname, elf_errmsg (-1)); + return 1; + } + /* Copy over the old program header if needed. */ if (ehdr->e_type != ET_REL) for (cnt = 0; cnt < ehdr->e_phnum; ++cnt) -- 2.1.4

