Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running binary file with qemu

Tags:

qemu

assembly

arm

I am learning arm assembly language in my one course. I am having little problem in getting started. I have written a simple c code:

int main()
{
    int a = 10;
    int b = 20;
    int c = a+b;
}

And then I converted it to assembly code using gnu arm by giving the command:

arm-elf-gcc -S first.c

This generated a file first.s containing assembly code:

    .file   "first.c"
    .text
    .align  2
    .global main
    .type   main, %function
main:
    @ args = 0, pretend = 0, frame = 12
    @ frame_needed = 1, uses_anonymous_args = 0
    mov ip, sp
    stmfd   sp!, {fp, ip, lr, pc}
    sub fp, ip, #4
    sub sp, sp, #12
    mov r3, #10
    str r3, [fp, #-16]
    mov r3, #20
    str r3, [fp, #-20]
    ldr r2, [fp, #-16]
    ldr r3, [fp, #-20]
    add r3, r2, r3
    str r3, [fp, #-24]
    mov r0, r3
    sub sp, fp, #12
    ldmfd   sp, {fp, sp, pc}
    .size   main, .-main
    .ident  "GCC: (GNU) 3.4.3"

Then I compiled the assembly code using following command:

arm-elf-gcc -g first.s

This generated a.out binary file. Then I tried to run a.out with qemu using command:

qemu-arm a.out

But this generates output

Segmentation fault

I can't find the mistake, what am I doing wrong?

like image 869
UNK Avatar asked Aug 09 '12 11:08

UNK


People also ask

How do I run a binary file in QEMU?

Try with qemu-arm -L /usr/arm-linux-gnueabi/ ./a. out. before that you need to install arm-linux-gnueabi-gcc. And compile also using arm-linux-gnueabi-gcc.

Can QEMU run arm on x86?

qemu-arm is able to execute the ARM executable successfully on x86 machine. This enables us to write and test code of any architecture on usual x86 systems and then port only the final code on to the real system.


1 Answers

You are trying to run qemu in user mode. You also need to link the libraries which corresponds to arm.

take a look at the script files in below pkg.

http://wiki.qemu.org/download/linux-user-test-0.3.tar.gz

You will need to run qemu -L library_PATH_ARM ./a.out

like image 78
peeyush Avatar answered Oct 03 '22 05:10

peeyush