Gcc arm ONE eabi是否重写bl指令?

Gcc arm ONE eabi是否重写bl指令?,gcc,arm,cortex-m,thumb,Gcc,Arm,Cortex M,Thumb,我试图理解为什么某些Cortex-M0代码在链接和未链接时表现不同。在这两种情况下,它都加载到0x20000000。看起来,尽管我尽了最大努力通过将-fPIC传递给编译器来生成与位置无关的代码,bl指令在代码通过链接器后似乎有所不同。我读对了吗?这只是链接器在ARM Thumb中工作的一部分吗?有没有更好的方法来生成与位置无关的函数调用 链接: 20000000: 20000000: 0003 movs r3, r0 20000002: 485

我试图理解为什么某些Cortex-M0代码在链接和未链接时表现不同。在这两种情况下,它都加载到
0x20000000
。看起来,尽管我尽了最大努力通过将
-fPIC
传递给编译器来生成与位置无关的代码,
bl
指令在代码通过链接器后似乎有所不同。我读对了吗?这只是链接器在ARM Thumb中工作的一部分吗?有没有更好的方法来生成与位置无关的函数调用

链接:

20000000:
20000000:       0003            movs    r3, r0
20000002:       4852            ldr     r0, [pc, #328]
20000004:       4685            mov     sp, r0
20000006:       0018            movs    r0, r3
20000008:       f000 f802       bl      20000010
2000000c:       46c0            nop                     ; (mov r8, r8)
2000000e:       46c0            nop                     ; (mov r8, r8)
未链接:

00000000:
   0:   0003            movs    r3, r0
   2:   4852            ldr     r0, [pc, #328]
   4:   4685            mov     sp, r0
   6:   0018            movs    r0, r3
   8:   f7ff fffe       bl      10
   c:   46c0            nop                     ; (mov r8, r8)
   e:   46c0            nop                     ; (mov r8, r8)
开始

.globl _start
_start:
.word 0x20001000
.word reset
.word hang
.word hang

.thumb

.thumb_func
reset:
    bl notmain
.thumb_func
hang:
    b .
诺曼

unsigned int x;

unsigned int fun ( unsigned int );
void notmain ( void )
{
    x=fun(x+5);
}
乐趣

记忆地图

MEMORY
{
    ram : ORIGIN = 0x20000000, LENGTH = 0x1000
}

SECTIONS
{
    .text : { *(.text*) } > ram
    .bss : { *(.bss*) } > ram
}
建造

产生

20000000 <_start>:
20000000:   20001000    andcs   r1, r0, r0
20000004:   20000011    andcs   r0, r0, r1, lsl r0
20000008:   20000015    andcs   r0, r0, r5, lsl r0
2000000c:   20000015    andcs   r0, r0, r5, lsl r0

20000010 <reset>:
20000010:   f000 f802   bl  20000018 <notmain>

20000014 <hang>:
20000014:   e7fe        b.n 20000014 <hang>
    ...

20000018 <notmain>:
20000018:   b510        push    {r4, lr}
2000001a:   4b06        ldr r3, [pc, #24]   ; (20000034 <notmain+0x1c>)
2000001c:   4a06        ldr r2, [pc, #24]   ; (20000038 <notmain+0x20>)
2000001e:   447b        add r3, pc
20000020:   589c        ldr r4, [r3, r2]
20000022:   6823        ldr r3, [r4, #0]
20000024:   1d58        adds    r0, r3, #5
20000026:   f000 f809   bl  2000003c <fun>
2000002a:   6020        str r0, [r4, #0]
2000002c:   bc10        pop {r4}
2000002e:   bc01        pop {r0}
20000030:   4700        bx  r0
20000032:   46c0        nop         ; (mov r8, r8)
20000034:   00000032    andeq   r0, r0, r2, lsr r0
20000038:   00000000    andeq   r0, r0, r0

2000003c <fun>:
2000003c:   4b03        ldr r3, [pc, #12]   ; (2000004c <fun+0x10>)
2000003e:   4a04        ldr r2, [pc, #16]   ; (20000050 <fun+0x14>)
20000040:   447b        add r3, pc
20000042:   589b        ldr r3, [r3, r2]
20000044:   681b        ldr r3, [r3, #0]
20000046:   3301        adds    r3, #1
20000048:   1818        adds    r0, r3, r0
2000004a:   4770        bx  lr
2000004c:   00000010    andeq   r0, r0, r0, lsl r0
20000050:   00000004    andeq   r0, r0, r4

Disassembly of section .got:

20000054 <.got>:
20000054:   20000068    andcs   r0, r0, r8, rrx
20000058:   2000006c    andcs   r0, r0, ip, rrx

Disassembly of section .got.plt:

2000005c <_GLOBAL_OFFSET_TABLE_>:
    ...

Disassembly of section .bss:

20000068 <x>:
20000068:   00000000    andeq   r0, r0, r0

2000006c <y>:
2000006c:   00000000    andeq   r0, r0, r0
位置独立

00000000 <fun>:
   0:   4b03        ldr r3, [pc, #12]   ; (10 <fun+0x10>)
   2:   4a04        ldr r2, [pc, #16]   ; (14 <fun+0x14>)
   4:   447b        add r3, pc
   6:   589b        ldr r3, [r3, r2]
   8:   681b        ldr r3, [r3, #0]
   a:   3301        adds    r3, #1
   c:   1818        adds    r0, r3, r0
   e:   4770        bx  lr
  10:   00000008    andeq   r0, r0, r8
  14:   00000000    andeq   r0, r0, r0
00000000 <fun>:
   0:   4b02        ldr r3, [pc, #8]    ; (c <fun+0xc>)
   2:   681b        ldr r3, [r3, #0]
   4:   3301        adds    r3, #1
   6:   1818        adds    r0, r3, r0
   8:   4770        bx  lr
   a:   46c0        nop         ; (mov r8, r8)
   c:   00000000    andeq   r0, r0, r0

诺曼有这些照片

Relocation section '.rel.text' at offset 0x1cc contains 3 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
0000000e  00000a0a R_ARM_THM_CALL    00000000   fun
0000001c  00000b19 R_ARM_BASE_PREL   00000000   _GLOBAL_OFFSET_TABLE_
00000020  00000c1a R_ARM_GOT_BREL    00000004   x
而且没有

Relocation section '.rel.text' at offset 0x198 contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
00000008  00000a0a R_ARM_THM_CALL    00000000   fun
00000014  00000b02 R_ARM_ABS32       00000004   x

因此,简而言之,工具链正在完成它的工作,你不需要重新完成它的工作。请注意,这与手臂或拇指无关。每当您使用对象和链接器模型并允许来自对象的外部项时,链接器都必须修补这些内容以将代码粘合在一起。这就是它的工作原理。

当然,这是链接器工作的一部分,它必须修改对象中的一些代码和/或地址,以便链接它们。否则,您将如何将对象链接在一起?修改分支/跳转/调用并不特定于arm链接器这只是链接器的工作方式这是他们的工作。链接器输入的大多数数字都分组在.text部分的末尾,由未修改的pc相关指令使用。对,编译器将倾向于保留32位位置对于要填充外部地址的链接器来说,这是该体系结构更简洁的方式(例如,对于x86,32位或64位地址是指令的一部分/附加在指令上,因此链接器在这里进行修改)。不是在m0上,而是一个全尺寸的手臂,如果切换模式,链接器可以添加一个蹦床,这样无论哪个指令集中的bl都可以保持在原位,但它降落在bx蹦床上以切换模式。由链接器提供。
00000000 <fun>:
   0:   4b03        ldr r3, [pc, #12]   ; (10 <fun+0x10>)
   2:   4a04        ldr r2, [pc, #16]   ; (14 <fun+0x14>)
   4:   447b        add r3, pc
   6:   589b        ldr r3, [r3, r2]
   8:   681b        ldr r3, [r3, #0]
   a:   3301        adds    r3, #1
   c:   1818        adds    r0, r3, r0
   e:   4770        bx  lr
  10:   00000008    andeq   r0, r0, r8
  14:   00000000    andeq   r0, r0, r0
00000000 <fun>:
   0:   4b02        ldr r3, [pc, #8]    ; (c <fun+0xc>)
   2:   681b        ldr r3, [r3, #0]
   4:   3301        adds    r3, #1
   6:   1818        adds    r0, r3, r0
   8:   4770        bx  lr
   a:   46c0        nop         ; (mov r8, r8)
   c:   00000000    andeq   r0, r0, r0
Relocation section '.rel.text' at offset 0x1a4 contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
00000010  00000a19 R_ARM_BASE_PREL   00000000   _GLOBAL_OFFSET_TABLE_
00000014  00000b1a R_ARM_GOT_BREL    00000004   y
Relocation section '.rel.text' at offset 0x174 contains 1 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
0000000c  00000a02 R_ARM_ABS32       00000004   y
Relocation section '.rel.text' at offset 0x1cc contains 3 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
0000000e  00000a0a R_ARM_THM_CALL    00000000   fun
0000001c  00000b19 R_ARM_BASE_PREL   00000000   _GLOBAL_OFFSET_TABLE_
00000020  00000c1a R_ARM_GOT_BREL    00000004   x
Relocation section '.rel.text' at offset 0x198 contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
00000008  00000a0a R_ARM_THM_CALL    00000000   fun
00000014  00000b02 R_ARM_ABS32       00000004   x