Menu

#526 Add elf32-x86-64 target support to objcopy

RELEASE_1_0
accepted
Kai Wang
elfcopy
Feature request
2016-05-10
2016-05-06
Roger
No

Hello,

I've came across a project [0] that uses objcopy (or elfcopy for the matter) with an elf32-x86-64 output target (-O elf32-x86-64). Currently elfcopy doesn't seem to support this target, or at least I couldn't see how to enable it. This is used in order to create an ELF32 file with amd64 code inside of it. The entry point is in 32bit mode, but then the code itself does the jump to 64bits.

I've been told elf32-x86-64 is used as a target (instead of plain elf32-i386) in order to allow objdump to correctly dissassemble the code. Sadly, I haven't been able to find a proper document explaining the exact target format for elf32-x86-64, I have a feeling this is related to the x32 ABI, but I'm quite lost at where/how to find this specification.

Is there any chance elfcopy could gain support for this target? I know the information I've provided is quite vague, but sadly I cannot find anything more concrete about it.

Thanks, Roger.

[0] http://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen-test-framework.git;a=summary

Discussion

  • Ed Maste

    Ed Maste - 2016-05-09

    Do you have an example objcopy invocation?

    You could give this naive, untested patch a try:

    diff --git a/libelftc/libelftc_bfdtarget.c b/libelftc/libelftc_bfdtarget.c     
    index 9c925eb..ba14c92 100644
    --- a/libelftc/libelftc_bfdtarget.c
    +++ b/libelftc/libelftc_bfdtarget.c
    @@ -195,6 +195,14 @@ struct _Elftc_Bfd_Target _libelftc_targets[] = {
            },
    
            {
    +               .bt_name      = "elf32-x86-64",
    +               .bt_type      = ETF_ELF,
    +               .bt_byteorder = ELFDATA2LSB,
    +               .bt_elfclass  = ELFCLASS32,
    +               .bt_machine   = EM_X86_64,
    +       },
    +
    +       {
                    .bt_name      = "elf64-alpha",
                    .bt_type      = ETF_ELF,
                    .bt_byteorder = ELFDATA2LSB,
    

    that is, the same as elf64-x86-64 but with .bt_elfclass=ELFCLASS32 instead.

     
  • Roger

    Roger - 2016-05-10

    Hello,

    This is the same that I've tried myself, sadly the resulting binary is not fully elf32-x86-64 compliant. The header looks fine AFAICT, but a binutils objdump with elf32-x86-64 support still fails to disassemble it. TBH, I'm not sure how important this is, the only reason elf32-x86-64 is used in the project I'm dealing with is for ease when using objdump with the resulting binary.

    The same result can be achieved by using elf32-i386 and then if you want to disassemble the binary converting them to elf64-x86-64 before using objdump on it.

    In case you want to look more into this, I've created a patch to enable elf32-x86-64 support in FreeBSD's binutils ports (https://reviews.freebsd.org/D6305), you can apply it in order to get GNU objcopy/objdump to understand this target, and then I've also uploaded a sample binary file at:

    https://people.freebsd.org/~royger/objcopy/

    The procedure for generating the desired output is to use:

    objcopy original -O elf32-x86-64 <result>

    I've also uploaded the result of running this with both GNU objcopy and a patched version of elfcopy (converted.gnu and converted.elftc respectively).

    Thanks, Roger.

     
  • Kai Wang

    Kai Wang - 2016-05-10
    • status: new --> accepted
    • assigned_to: Kai Wang
     
  • Kai Wang

    Kai Wang - 2016-05-10

    I can take a look.

     

Log in to post a comment.