在Cortex-M4上编译和运行ARM程序集二进制文件(在QEMU中模拟)

时间:2017-09-09 08:16:32

标签: arm qemu cortex-m

我使用以下过程在虚拟QEMU嵌入式系统connex上成功编译并执行了ARM二进制文件:

arm-none-eabi-as -o program.o program.s
arm-none-eabi-ld -Ttext=0x0 -o program.elf program.o  
arm-none-eabi-objcopy -O binary program.elf program.bin
dd if=/dev/zero of=flash.bin bs=4096 count=4096
dd if=program.bin of=flash.bin bs=4096 conv=notrunc
qemu-system-arm -M connex -pflash flash.bin -nographic -serial /dev/null

在第四行中,我创建了一个归零空磁盘,代表闪存,在第五行,我将二进制文件复制到闪存中。

所以这就像一个魅力,但它模拟整个嵌入式系统,而我只想模拟ARM内核,例如Cortex-M4。这就是我尝试使用qemu-arm代替qemu-system-arm的原因。

所以我第一次尝试编译并运行我的程序(第1-3行与上面相同):

arm-none-eabi-as -o program.o program.s
arm-none-eabi-ld -Ttext=0x0 -o program.elf program.o  
arm-none-eabi-objcopy -O binary program.elf program.bin
qemu-arm -cpu cortex-m4 program.bin

这不起作用 - 它说:

Error while loading program.bin: Exec format error

所以我尝试像之前一样创建flash图像(因为它有效):

arm-none-eabi-as -o program.o program.s
arm-none-eabi-ld -Ttext=0x0 -o program.elf program.o  
arm-none-eabi-objcopy -O binary program.elf program.bin
dd if=/dev/zero of=flash.bin bs=4096 count=4096
dd if=program.bin of=flash.bin bs=4096 conv=notrunc
qemu-arm -cpu cortex-m4 flash.bin

我明白了:

Error while loading flash.bin: Permission denied

有人可以帮我一点吗?使用sudo无效。

2 个答案:

答案 0 :(得分:2)

qemu-arm的目的不是"只模拟一个ARM内核"。它是运行单个 Linux 二进制文件",并且它期望您提供的二进制文件是Linux格式的ELF可执行文件。试图用其他东西喂它是行不通的。

由于Linux假设A-profile核心,而不是M-profile核心,你在qemu-arm上使用-cpu cortex-m4做的任何事情都只能靠运气而不是故意。 (我们不会禁用那些CPU类型,因为有一些GCC测试用例场景使用半主机,这种类型的工作类型和我们不想故意破坏。但这些都是运气好的别的什么。)

答案 1 :(得分:0)

与微控制器构建相比,您需要一个入口点(并且它是ram)。

的start.s

.thumb
.thumb_func
.global _start
_start:
    @mov r0,=0x10000
    @mov sp,r0
    bl notmain
    mov r7,#0x1
    mov r0,#0
    swi #0
.word 0xFFFFFFFF
    b .

.thumb_func
.globl PUT32
PUT32:
    str r1,[r0]
    bx lr

.thumb_func
.globl GET32
GET32:
    ldr r0,[r0]
    bx lr

.thumb_func
.globl dummy
dummy:
    bx lr

.thumb_func
.globl write
write:
    push {r7,lr}
    mov r7,#0x04
    swi 0
    pop {r7,pc}
    b .

.end

notmain.c

void PUT32 ( unsigned int, unsigned int );
unsigned int GET32 ( unsigned int );
void dummy ( unsigned int );
void write ( unsigned int, char *, unsigned int );
int notmain ( void )
{
    //unsigned int ra;
    //for(ra=0;ra<1000;ra++) dummy(ra);
    write(1,"Hello\n",6);
    return(0);

}

hello.ld

ENTRY(_start)
MEMORY
{
    ram : ORIGIN = 0x00010000, LENGTH = 0x1000
}
SECTIONS
{
    .text : { *(.text*) } > ram
    .rodata : { *(.rodata*) } > ram
    .bss : { *(.bss*) } > ram
}

构建

arm-none-eabi-as --warn --fatal-warnings start.s -o start.o
arm-none-eabi-gcc -Wall -Werror -O2 -nostdlib -nostartfiles -ffreestanding  -mthumb -c notmain.c -o notmain.o
arm-none-eabi-ld -o notmain.elf -T hello.ld start.o notmain.o
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy notmain.elf notmain.bin -O binary

运行

qemu-arm -d in_asm,cpu,cpu_reset -D hello -cpu cortex-m4 notmain.elf 
Hello

转储日志

cat hello
CPU Reset (CPU 0)
R00=00000000 R01=00000000 R02=00000000 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00000000 R11=00000000
R12=00000000 R13=00000000 R14=00000000 R15=00000000
PSR=40000000 -Z-- A usr26
CPU Reset (CPU 0)
R00=00000000 R01=00000000 R02=00000000 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00000000 R11=00000000
R12=00000000 R13=00000000 R14=00000000 R15=00000000
PSR=40000010 -Z-- A usr32
Reserved 0xf7000000 bytes of guest address space
host mmap_min_addr=0x10000
guest_base  0x7f4347fb4000
start    end      size     prot
00010000-00011000 00001000 r-x
f67ff000-f6800000 00001000 ---
f6800000-f7000000 00800000 rw-
start_brk   0x00000000
end_code    0x00010044
start_code  0x00010000
start_data  0x00010044
end_data    0x00010044
start_stack 0xf6fff350
brk         0x00010044
entry       0x00010001
----------------
IN: 
0x00010000:  f000 f810  bl  0x10024

R00=00000000 R01=f6fff4c2 R02=00000000 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00010044 R11=00000000
R12=00000000 R13=f6fff350 R14=00000000 R15=00010000
PSR=00000030 ---- T usr32
----------------
IN: notmain
0x00010024:  b508       push    {r3, lr}
0x00010026:  2001       movs    r0, #1
0x00010028:  4903       ldr r1, [pc, #12]   (0x10038)
0x0001002a:  2206       movs    r2, #6
0x0001002c:  f7ff fff5  bl  0x1001a

R00=00000000 R01=f6fff4c2 R02=00000000 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00010044 R11=00000000
R12=00000000 R13=f6fff350 R14=00010005 R15=00010024
PSR=00000030 ---- T usr32
----------------
IN: 
0x0001001a:  b580       push    {r7, lr}
0x0001001c:  2704       movs    r7, #4
0x0001001e:  df00       svc 0

R00=00000001 R01=0001003c R02=00000006 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00010044 R11=00000000
R12=00000000 R13=f6fff348 R14=00010031 R15=0001001a
PSR=00000030 ---- T usr32
----------------
IN: 
0x00010020:  bd80       pop {r7, pc}

R00=00000006 R01=0001003c R02=00000006 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000004
R08=00000000 R09=00000000 R10=00010044 R11=00000000
R12=00000000 R13=f6fff340 R14=00010031 R15=00010020
PSR=00000030 ---- T usr32
----------------
IN: notmain
0x00010030:  2000       movs    r0, #0
0x00010032:  bc08       pop {r3}
0x00010034:  bc02       pop {r1}
0x00010036:  4708       bx  r1

R00=00000006 R01=0001003c R02=00000006 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00010044 R11=00000000
R12=00000000 R13=f6fff348 R14=00010031 R15=00010030
PSR=00000030 ---- T usr32
----------------
IN: 
0x00010004:  2701       movs    r7, #1
0x00010006:  2000       movs    r0, #0
0x00010008:  df00       svc 0

R00=00000000 R01=00010005 R02=00000006 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00010044 R11=00000000
R12=00000000 R13=f6fff350 R14=00010031 R15=00010004
PSR=40000030 -Z-- T usr32

如果触摸堆栈指针会不高兴,所以不要......

感谢您指出这个程序,不知道它,会有一些乐趣......

修改

抱歉,你只是想组装。

的start.s

.thumb
.thumb_func
.global _start
_start:
    mov r4,#10
top:
    nop
    sub r4,#1
    bne top

    mov r7,#0x1
    mov r0,#0
    swi #0
.word 0xFFFFFFFF
    b .

.end

上面的链接器脚本

构建

arm-none-eabi-as --warn --fatal-warnings start.s -o start.o
arm-none-eabi-ld -o notmain.elf -T hello.ld start.o
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy notmain.elf notmain.bin -O binary

运行

qemu-arm -d in_asm,cpu,cpu_reset -D hello -cpu cortex-m4 notmain.elf 

转储日志

cat hello
CPU Reset (CPU 0)
R00=00000000 R01=00000000 R02=00000000 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00000000 R11=00000000
R12=00000000 R13=00000000 R14=00000000 R15=00000000
PSR=40000000 -Z-- A usr26
CPU Reset (CPU 0)
R00=00000000 R01=00000000 R02=00000000 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00000000 R11=00000000
R12=00000000 R13=00000000 R14=00000000 R15=00000000
PSR=40000010 -Z-- A usr32
Reserved 0xf7000000 bytes of guest address space
host mmap_min_addr=0x10000
guest_base  0x7f36110fc000
start    end      size     prot
00010000-00011000 00001000 r-x
f67ff000-f6800000 00001000 ---
f6800000-f7000000 00800000 rw-
start_brk   0x00000000
end_code    0x00010014
start_code  0x00010000
start_data  0x00010014
end_data    0x00010014
start_stack 0xf6fff350
brk         0x00010014
entry       0x00010001
----------------
IN: 
0x00010000:  240a       movs    r4, #10
0x00010002:  46c0       nop         (mov r8, r8)
0x00010004:  3c01       subs    r4, #1
0x00010006:  d1fc       bne.n   0x10002

R00=00000000 R01=f6fff4c2 R02=00000000 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00010014 R11=00000000
R12=00000000 R13=f6fff350 R14=00000000 R15=00010000
PSR=00000030 ---- T usr32
----------------
IN: 
0x00010002:  46c0       nop         (mov r8, r8)
0x00010004:  3c01       subs    r4, #1
0x00010006:  d1fc       bne.n   0x10002

R00=00000000 R01=f6fff4c2 R02=00000000 R03=00000000
R04=00000009 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00010014 R11=00000000
R12=00000000 R13=f6fff350 R14=00000000 R15=00010002
PSR=20000030 --C- T usr32
R00=00000000 R01=f6fff4c2 R02=00000000 R03=00000000
R04=00000008 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00010014 R11=00000000
R12=00000000 R13=f6fff350 R14=00000000 R15=00010002
PSR=20000030 --C- T usr32
----------------
IN: 
0x00010008:  2701       movs    r7, #1
0x0001000a:  2000       movs    r0, #0
0x0001000c:  df00       svc 0

R00=00000000 R01=f6fff4c2 R02=00000000 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00010014 R11=00000000
R12=00000000 R13=f6fff350 R14=00000000 R15=00010008
PSR=60000030 -ZC- T usr32