I'm new to coding 64-bit x86, but I am running Ubuntu 14.04 (trusty), and I have a very simple 64-bit piece of code that I assemble using as. The output I get has strange permissions and file type.
When I run:
as file.s
I get a file a.out
with 770 permissions.
I get this error when I execute it:
bash: ./a.out: cannot execute binary file: Exec format error
When I run:
file ./a.out
I get:
./a.out: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
The assembly code I am using is:
.section .data
.LC0:
.string "/bin/sh"
.LC1:
.string "/bin/sh"
.LC3:
.quad .LC1, 0
.text
.globl _start
_start:
.LFB0:
pushq %rbp
movq %rsp, %rbp
movq $59,%rax # System Call to execve
movq $.LC0, %rdi # Pass program to execute
movq $.LC3, %rsi # Pass command line arguments
syscall
movl $0, %eax
popq %rbp
ret
You want to assemble the program with GNU Assembler (AS) first and then use the linker to generate the final executable. To do that you should be able to use something like:
as file.s -o file.o
ld file.o -o file
The first command tells the assembler to output an ELF64 object called file.o
using the -o
option. The second command links file.o
to a 64-bit ELF executable called file
using the -o
option.
The typical default behaviour on a 64-bit system is for the assembler and linker to generate 64-bit objects and executables.
You can then run it with:
./file