I have a file which content represents x64 machine code in hex.
48 c7 c0 7b 00 00 00 48 c7 c1 59 01 00 00 48 01 c8 c3
The example above can be disassembled to:
0: 48 c7 c0 7b 00 00 00 mov rax,0x7b
7: 48 c7 c1 59 01 00 00 mov rcx,0x159
e: 48 01 c8 add rax,rcx
11: c3 ret
This machine code is being generated directly, so the assembler shown above is for the sake of the example. I do not have access to it in general.
What I want to do is convert this hex file to and executable in the most direct way possible. I tried using xxd -r
, but this does not seem to create a well-formatted executable since I get a Exec format error
when trying to run it.
How should I generate an executable from the hex code under Linux?
From my understanding of the discussion which followed my question, what I want to do is create an ELF file. This question covers how to create your own ELF header.
Although, if you do not want to go through the trouble, you can actually use gcc
to compile a sequence of bytes to an executable.
.section .text
.global main
main:
.byte 0x48 0xc7 0xc0 0x7b 0x00 0x00 0x00 0x48 0xc7 0xc1 0x59 0x01 ...
You can then compile the above with gcc file.S -o file
. This will create the desired ELF file.