Compilation STM32位置无关二进制文件

Compilation STM32位置无关二进制文件,compilation,linker,stm32,cortex-m,stm32f4,Compilation,Linker,Stm32,Cortex M,Stm32f4,我正在使用以下CFLAG为Cortex-M4(STM32F4)编译我的FreeRTOS应用程序: -fpic -msingle-pic-base -mpic-data-is-text-relative -mpic-register=r10 并为链接器设置了-fpic标志 向量表已正确复制到RAM,并更改了设备当前启动的特定固件插槽的偏移值 然而,从固件插槽启动主应用程序后,设备挂起在硬故障中,CFSR寄存器中设置了不精确的RR。 执行以下命令行时,它出现在vPortSVCHandler中: l

我正在使用以下CFLAG为Cortex-M4(STM32F4)编译我的FreeRTOS应用程序:

-fpic -msingle-pic-base -mpic-data-is-text-relative -mpic-register=r10
并为链接器设置了
-fpic
标志

向量表已正确复制到RAM,并更改了设备当前启动的特定固件插槽的偏移值

然而,从固件插槽启动主应用程序后,设备挂起在硬故障中,CFSR寄存器中设置了不精确的RR。 执行以下命令行时,它出现在vPortSVCHandler中:

ldmia r0!, {r4-r11, r14}
执行此操作后,链接寄存器值等于
0000

我应该做些什么来正确地为STM32创建独立于位置的应用程序吗?重新定位全局偏移表?如果是,记忆中的哪个地方


编辑

根据下面的问题,我的目标是创建具有引导加载程序和2个固件插槽的应用程序。我需要闪存中的固件插槽,以便我的设备能够在运行时更新。 为什么我需要用PIC标志编译固件?每个插槽在内存中都有其空间。默认情况下,它是为第一个插槽地址编译的。从为第一个插槽准备的第二个插槽运行这样的二进制文件是不可能的,因为数据访问等(老实说,这是我第一次使用GOT和PIC编译)是基于程序计数器寄存器值(程序内存空间中的当前位置)。固件更新使用当前未使用的下一个可用插槽。因为我不知道当前使用的是哪个,所以我需要构建二进制文件,它将在所有插槽中正常工作(在这种情况下是2个)。这就是为什么,在阅读了许多网站、帖子等之后,我认为我需要这个

我从引导加载程序切换到主应用程序的流程如下:

  • 检查应使用哪个固件插槽
  • 禁用IRQ
  • 将向量表复制到RAM。两个插槽的RAM部分相同。在复制过程中,我将更改每个地址的偏移量,以便它们与特定的固件插槽兼容。默认情况下,地址没有偏移量,它在编译后阶段被删除
  • 根据RAM中向量表中的第一个字设置堆栈指针。将向量表复制到RAM时,地址不变
  • 设置SCB->VTOR
  • 执行数据同步屏障DSB()
  • 从复制到RAM的向量表跳转到重置处理程序

  • 旧计时器发布答案后编辑

    因此,我尝试为这两个插槽编译代码。以下是GOT拆卸的结果:

    Disassembly of section .got:
    
    080083ac <_got_address>:
     80083ac:   0800beb9    stmdaeq r0, {r0, r3, r4, r5, r7, r9, sl, fp, ip, sp, pc}
     80083b0:   0800bf4c    stmdaeq r0, {r2, r3, r6, r8, r9, sl, fp, ip, sp, pc}
     80083b4:   20000274    andcs   r0, r0, r4, ror r2
     80083b8:   2000022c    andcs   r0, r0, ip, lsr #4
     80083bc:   20012fb4            ; <UNDEFINED> instruction: 0x20012fb4
     80083c0:   080086c5    stmdaeq r0, {r0, r2, r6, r7, r9, sl, pc}
     80083c4:   20000200    andcs   r0, r0, r0, lsl #4
     80083c8:   200132f0    strdcs  r3, [r1], -r0
     80083cc:   20013330    andcs   r3, r1, r0, lsr r3
     80083d0:   080088e1    stmdaeq r0, {r0, r5, r6, r7, fp, pc}
     80083d4:   20013334    andcs   r3, r1, r4, lsr r3
     80083d8:   20013338    andcs   r3, r1, r8, lsr r3
     80083dc:   20000230    andcs   r0, r0, r0, lsr r2
     80083e0:   200132d0    ldrdcs  r3, [r1], -r0
     80083e4:   20012fb8            ; <UNDEFINED> instruction: 0x20012fb8
     80083e8:   20000234    andcs   r0, r0, r4, lsr r2
     80083ec:   200131cc    andcs   r3, r1, ip, asr #3
     80083f0:   0800bed1    stmdaeq r0, {r0, r4, r6, r7, r9, sl, fp, ip, sp, pc}
     80083f4:   080089a1    stmdaeq r0, {r0, r5, r7, r8, fp, pc}
     80083f8:   0800bf7c    stmdaeq r0, {r2, r3, r4, r5, r6, r8, r9, sl, fp, ip, sp, pc}
     80083fc:   080086a5    stmdaeq r0, {r0, r2, r5, r7, r9, sl, pc}
     8008400:   080087f1    stmdaeq r0, {r0, r4, r5, r6, r7, r8, r9, sl, pc}
     8008404:   200132cc    andcs   r3, r1, ip, asr #5
    
    节的分解。得到:
    080083ac:
    80083ac:0800beb9 stmdaeq r0,{r0,r3,r4,r5,r7,r9,sl,fp,ip,sp,pc}
    80083b0:0800bf4c stmdaeq r0,{r2,r3,r6,r8,r9,sl,fp,ip,sp,pc}
    80083b4:20000274和CS r0、r0、r4、ror r2
    80083b8:200002C和CS r0、r0、ip、lsr#4
    公元前80083年:20012fb4年;说明:0x20012fb4
    80083c0:080086c5 stmdaeq r0,{r0,r2,r6,r7,r9,sl,pc}
    80083c4:20000200和CS r0、r0、r0、lsl#4
    80083c8:200132f0标准关系码r3,[r1],-r0
    80083cc:20013330和CS r3、r1、r0、lsr r3
    80083d0:080088e1 stmdaeq r0,{r0,r5,r6,r7,fp,pc}
    80083d4:20013334和CS r3、r1、r4、lsr r3
    80083d8:20013338和CS r3、r1、r8、lsr r3
    80083dc:20000230和CS r0、r0、r0、lsr r2
    80083e0:200132d0 ldrdcs r3,[r1],-r0
    80083e4:20012fb8;说明:0x20012fb8
    80083e8:20000234和CS r0、r0、r4、lsr r2
    80083ec:200131cc和CS r3、r1、ip、asr#3
    80083f0:0800bed1 stmdaeq r0,{r0,r4,r6,r7,r9,sl,fp,ip,sp,pc}
    80083f4:080089a1 stmdaeq r0,{r0,r5,r7,r8,fp,pc}
    80083f8:0800bf7c stmdaeq r0,{r2,r3,r4,r5,r6,r8,r9,sl,fp,ip,sp,pc}
    80083fc:080086a5 stmdaeq r0,{r0,r2,r5,r7,r9,sl,pc}
    8008400:080087f1 stmdaeq r0,{r0,r4,r5,r6,r7,r8,r9,sl,pc}
    8008404:200132cc和CS r3、r1、ip、asr#5
    
    如@old_timer所述,在更改固件插槽后,此更改为:

     Disassembly of section .got:
    
    081043ac <_got_address>:
     81043ac:   08107eb9    ldmdaeq r0, {r0, r3, r4, r5, r7, r9, sl, fp, ip, sp, lr}
     81043b0:   08107f4c    ldmdaeq r0, {r2, r3, r6, r8, r9, sl, fp, ip, sp, lr}
     81043b4:   20000274    andcs   r0, r0, r4, ror r2
     81043b8:   2000022c    andcs   r0, r0, ip, lsr #4
     81043bc:   20012fb4            ; <UNDEFINED> instruction: 0x20012fb4
     81043c0:   081046c5    ldmdaeq r0, {r0, r2, r6, r7, r9, sl, lr}
     81043c4:   20000200    andcs   r0, r0, r0, lsl #4
     81043c8:   200132f0    strdcs  r3, [r1], -r0
     81043cc:   20013330    andcs   r3, r1, r0, lsr r3
     81043d0:   081048e1    ldmdaeq r0, {r0, r5, r6, r7, fp, lr}
     81043d4:   20013334    andcs   r3, r1, r4, lsr r3
     81043d8:   20013338    andcs   r3, r1, r8, lsr r3
     81043dc:   20000230    andcs   r0, r0, r0, lsr r2
     81043e0:   200132d0    ldrdcs  r3, [r1], -r0
     81043e4:   20012fb8            ; <UNDEFINED> instruction: 0x20012fb8
     81043e8:   20000234    andcs   r0, r0, r4, lsr r2
     81043ec:   200131cc    andcs   r3, r1, ip, asr #3
     81043f0:   08107ed1    ldmdaeq r0, {r0, r4, r6, r7, r9, sl, fp, ip, sp, lr}
     81043f4:   081049a1    ldmdaeq r0, {r0, r5, r7, r8, fp, lr}
     81043f8:   08107f7c    ldmdaeq r0, {r2, r3, r4, r5, r6, r8, r9, sl, fp, ip, sp, lr}
     81043fc:   081046a5    ldmdaeq r0, {r0, r2, r5, r7, r9, sl, lr}
     8104400:   081047f1    ldmdaeq r0, {r0, r4, r5, r6, r7, r8, r9, sl, lr}
     8104404:   200132cc    andcs   r3, r1, ip, asr #5
    
    节的分解。得到:
    081043ac:
    81043ac:08107eb9 ldmdaeq r0,{r0,r3,r4,r5,r7,r9,sl,fp,ip,sp,lr}
    81043b0:08107f4c ldmdaeq r0,{r2,r3,r6,r8,r9,sl,fp,ip,sp,lr}
    81043b4:20000274和CS r0、r0、r4、ror r2
    81043b8:200002C和CS r0、r0、ip、lsr#4
    公元前81043年:20012fb4年;说明:0x20012fb4
    81043c0:081046c5 ldmdaeq r0,{r0,r2,r6,r7,r9,sl,lr}
    81043c4:20000200和CS r0、r0、r0、lsl#4
    81043c8:200132f0标准关系码r3,[r1],-r0
    81043cc:20013330和CS r3、r1、r0、lsr r3
    81043d0:081048e1 ldmdaeq r0,{r0,r5,r6,r7,fp,lr}
    81043d4:20013334和CS r3、r1、r4、lsr r3
    81043d8:20013338和CS r3、r1、r8、lsr r3
    81043dc:20000230和CS r0、r0、r0、lsr r2
    81043e0:200132d0 ldrdcs r3,[r1],-r0
    81043e4:20012fb8;说明:0x20012fb8
    81043e8:20000234和CS r0、r0、r4、lsr r2
    81043ec:200131cc和CS r3、r1、ip、asr#3
    81043f0:08107ed1 ldmdaeq r0,{r0,r4,r6,r7,r9,sl,fp,ip,sp,lr}
    81043f4:081049a1 ldmdaeq r0,{r0,r5,r7,r8,fp,lr}
    81043f8:08107f7c ldmdaeq r0,{r2,r3,r4,r5,r6,r8,r9,sl,fp,ip,sp,lr}
    81043fc:081046a5 ldmdaeq r0,{r0,r2,r5,r7,r9,sl,lr}
    8104400:081047f1 ldmdaeq r0,{r0,r4,r5,r6,r7,r8,r9,sl,lr}
    8104404:200132cc和CS r3、r1、ip、asr#5
    
    1。首先要做的是在RAM内存中为GOT保留空间,并请求引导加载程序/启动程序将GOT复制到该位置,并在必要时添加插槽偏移量。我想我可以通过更改链接器代码来做到这一点,以便将GOT放置在RAM的VMA中。所以我假设,在应用程序的其余部分,get地址将更改为RAM中的地址,对吗

    为什么这个二进制文件在设备中不工作?这是因为我应该添加编译标志
    -mpic register=r10
    ,并在将get复制到RAM之后和executi之前将该寄存器设置为get地址
    .thumb
    .globl _start
    _start:
    reset:
        mov r0,pc
        ldr r1,=0xFFFF0000
        and r0,r1
        ldr r1,gotbase
        add r0,r1
        bl centry
        b .
        .align
    gotbase:
        .word _GLOBAL_OFFSET_TABLE_-(_start)
        .word _start
        .word _GLOBAL_OFFSET_TABLE_
        .word _GLOBAL_OFFSET_TABLE_
    
    extern unsigned int fun ( unsigned int );
    unsigned int x;
    unsigned int y;
    unsigned int z;
    void centry ( void )
    {
        x=5;
        y=6;
        z=fun(77);
    }
    
    unsigned int fun ( unsigned int x )
    {
        return(x+3);
    }
    
    MEMORY
    {
        rom : ORIGIN = 0x08020000, LENGTH = 0x1000
        ram : ORIGIN = 0x20000000, LENGTH = 0x1000
    }
    SECTIONS
    {
        .text : { *(.text*) } > rom
        .rodata : { *(.rodata*) } > rom
        .bss : { *(.bss*) } > ram
    }
    
    arm-none-eabi-as --warn  boot.s -o boot.o
    arm-none-eabi-gcc -Wall -O2 -mthumb -fpic -mthumb -c so.c -o so.o
    arm-none-eabi-gcc -Wall -O2 -mthumb -fpic -mthumb -c fun.c -o fun.o
    arm-none-eabi-ld -o so.elf -T flash.ld boot.o so.o fun.o
    arm-none-eabi-objdump -D so.elf > so.list
    arm-none-eabi-objcopy --srec-forceS3 so.elf -O srec so.srec
    arm-none-eabi-objcopy so.elf so.bin -O binary
    
    Disassembly of section .text:
    08020000 <_start>:
     8020000:   4678        mov r0, pc
     8020002:   4907        ldr r1, [pc, #28]   ; (8020020 <gotbase+0x10>)
     8020004:   4008        ands    r0, r1
     8020006:   4902        ldr r1, [pc, #8]    ; (8020010 <gotbase>)
     8020008:   1840        adds    r0, r0, r1
     802000a:   f000 f80b   bl  8020024 <centry>
     802000e:   e7fe        b.n 802000e <_start+0xe>
    
    08020010 <gotbase>:
     8020010:   00000060
     8020014:   08020000
     8020018:   00000048
     802001c:   00000044
     8020020:   ffff0000
    
    08020024 <centry>:
     8020024:   2205        movs    r2, #5
     8020026:   b510        push    {r4, lr}
     8020028:   4c08        ldr r4, [pc, #32]   ; (802004c <centry+0x28>)
     802002a:   4b09        ldr r3, [pc, #36]   ; (8020050 <centry+0x2c>)
     802002c:   447c        add r4, pc
     802002e:   58e3        ldr r3, [r4, r3]
     8020030:   601a        str r2, [r3, #0]
     8020032:   4b08        ldr r3, [pc, #32]   ; (8020054 <centry+0x30>)
     8020034:   58e3        ldr r3, [r4, r3]
     8020036:   3201        adds    r2, #1
     8020038:   204d        movs    r0, #77 ; 0x4d
     802003a:   601a        str r2, [r3, #0]
     802003c:   f000 f80e   bl  802005c <fun>
     8020040:   4b05        ldr r3, [pc, #20]   ; (8020058 <centry+0x34>)
     8020042:   58e3        ldr r3, [r4, r3]
     8020044:   6018        str r0, [r3, #0]
     8020046:   bc10        pop {r4}
     8020048:   bc01        pop {r0}
     802004a:   4700        bx  r0
     802004c:   00000030
     8020050:   00000000
     8020054:   00000008
     8020058:   00000004
    
    0802005c <fun>:
     802005c:   3003        adds    r0, #3
     802005e:   4770        bx  lr
    
    Disassembly of section .got:
    
    08020060 <.got>:
     8020060:   20000000
     8020064:   20000004
     8020068:   20000008
    
    Disassembly of section .got.plt:
    
    0802006c <_GLOBAL_OFFSET_TABLE_>:
        ...
    
    Disassembly of section .bss:
    
    20000000 <x>:
    20000000:   00000000
    
    20000004 <z>:
    20000004:   00000000
    
    20000008 <y>:
    20000008:   00000000
    
    08020060 <.got>:
     8020060:   20000000
     8020064:   20000004
     8020068:   20000008
    
    rom : ORIGIN = 0x08010000, LENGTH = 0x1000
    ram : ORIGIN = 0x30000000, LENGTH = 0x1000
    
    Disassembly of section .got.plt:
    
    0802006c <_GLOBAL_OFFSET_TABLE_>:
        ...
    
     8020024:   2205        movs    r2, #5
    
     8020028:   4c08        ldr r4, [pc, #32]   ; (802004c <centry+0x28>)
    
     802002c:   447c        add r4, pc
    
     8020032:   4b08        ldr r3, [pc, #32]   ; (8020054 <centry+0x30>)
     8020034:   58e3        ldr r3, [r4, r3]
     8020036:   3201        adds    r2, #1
    
     802003a:   601a        str r2, [r3, #0]
    
    08020060 <.got>:
     8020060:   20000000
     8020064:   20000004
     8020068:   20000008
    
    .thumb
    .globl _start
    _start:
    reset:
        b skip
        .align
        .word _GLOBAL_OFFSET_TABLE_-(_start)
    skip:
    
    
    08020000 <_start>:
     8020000:   e002        b.n 8020008 <skip>
     8020002:   46c0        nop         ; (mov r8, r8)
     8020004:   00000068    andeq   r0, r0, r8, rrx
    
    08020008 <skip>:
    
    08020068 <.got>:
     8020068:   20000000
     802006c:   20000004
     8020070:   20000008
    
    08020068 <.got>:
     8020068:   30000000
     802006c:   30000004
     8020070:   30000008
    
    08020068 <.got>:
     8020068:   20000000
     802006c:   20000004
     8020070:   20000008
    
    08020068 <.got>:
     8020068:   30000000
     802006c:   30000004
     8020070:   30000008
    
    Disassembly of section .text:
    
    08020000 <_start>:
     8020000:   e002        b.n 8020008 <skip>
     8020002:   46c0        nop         ; (mov r8, r8)
     8020004:   00000064    andeq   r0, r0, r4, rrx
    
    08020008 <skip>:
     8020008:   4678        mov r0, pc
     802000a:   4907        ldr r1, [pc, #28]   ; (8020028 <gotbase+0x10>)
     802000c:   4008        ands    r0, r1
     802000e:   4902        ldr r1, [pc, #8]    ; (8020018 <gotbase>)
     8020010:   1840        adds    r0, r0, r1
     8020012:   f000 f80b   bl  802002c <centry>
     8020016:   e7fe        b.n 8020016 <skip+0xe>
    
    08020018 <gotbase>:
     8020018:   00000064    andeq   r0, r0, r4, rrx
     802001c:   08020000    stmdaeq r2, {}  ; <UNPREDICTABLE>
     8020020:   00000044    andeq   r0, r0, r4, asr #32
     8020024:   00000040    andeq   r0, r0, r0, asr #32
     8020028:   ffff0000            ; <UNDEFINED> instruction: 0xffff0000
    
    0802002c <centry>:
     802002c:   b510        push    {r4, lr}
     802002e:   4654        mov r4, r10
     8020030:   2205        movs    r2, #5
     8020032:   4b08        ldr r3, [pc, #32]   ; (8020054 <centry+0x28>)
     8020034:   58e3        ldr r3, [r4, r3]
     8020036:   601a        str r2, [r3, #0]
     8020038:   4b07        ldr r3, [pc, #28]   ; (8020058 <centry+0x2c>)
     802003a:   58e3        ldr r3, [r4, r3]
     802003c:   3201        adds    r2, #1
     802003e:   204d        movs    r0, #77 ; 0x4d
     8020040:   601a        str r2, [r3, #0]
     8020042:   f000 f80d   bl  8020060 <fun>
     8020046:   4b05        ldr r3, [pc, #20]   ; (802005c <centry+0x30>)
     8020048:   58e3        ldr r3, [r4, r3]
     802004a:   6018        str r0, [r3, #0]
     802004c:   bc10        pop {r4}
     802004e:   bc01        pop {r0}
     8020050:   4700        bx  r0
     8020052:   46c0        nop         ; (mov r8, r8)
     8020054:   00000000    andeq   r0, r0, r0
     8020058:   00000008    andeq   r0, r0, r8
     802005c:   00000004    andeq   r0, r0, r4
    
    08020060 <fun>:
     8020060:   3003        adds    r0, #3
     8020062:   4770        bx  lr
    
    Disassembly of section .got:
    
    08020064 <.got>:
     8020064:   20000000    andcs   r0, r0, r0
     8020068:   20000004    andcs   r0, r0, r4
     802006c:   20000008    andcs   r0, r0, r8
    
     802002e:   4654        mov r4, r10