C 如何将二进制文件构建到固定地址0x80000的映像中?
在我们的c项目中,我们需要在固定文件偏移量0x80000处将二进制固件构建到映像中 然后,当图像加载到内存时。我们可以将固件从偏移量0x80000加载到指定的地址。 同时,由于固件位于文件偏移量0x80000处,我们可以独立升级固件 所以我尝试使用GNU链接器脚本来实现它。 我现在要做的是使用incbin将我的二进制文件包含在asm文件中。 在链接器脚本中,我的代码是:C 如何将二进制文件构建到固定地址0x80000的映像中?,c,arm,gnu,ld,linker-scripts,C,Arm,Gnu,Ld,Linker Scripts,在我们的c项目中,我们需要在固定文件偏移量0x80000处将二进制固件构建到映像中 然后,当图像加载到内存时。我们可以将固件从偏移量0x80000加载到指定的地址。 同时,由于固件位于文件偏移量0x80000处,我们可以独立升级固件 所以我尝试使用GNU链接器脚本来实现它。 我现在要做的是使用incbin将我的二进制文件包含在asm文件中。 在链接器脚本中,我的代码是: .fw_image_start : { *(.__fw_image_start)
.fw_image_start : {
*(.__fw_image_start)
}
.fw_image : {
KEEP(*(.fw_image))
}
.fw_image_end : {
*(.__fw_image_end)
}
然后我可以使用fw_image_start在映像代码中加载固件
但我仍然找不到在最终映像中将固件二进制文件放入文件偏移量0x80000的方法
你能帮我吗
提前谢谢你 当您查看GNU的文档和示例时,您发现了什么?诚然,其中有些是令人困惑或误导的,但有些是相当简单的。这应该至少有一种方法(有多种方法可以解决您的问题) 中篇小说
.global _start
_start:
bl notmain
b .
.globl bounce
bounce:
bx lr
.section .hello_world
.word 1,2,3,4
诺曼
void bounce ( unsigned int );
unsigned int mybss[8];
int notmain ( void )
{
unsigned int ra;
for(ra=0;ra<1000;ra++) bounce(ra);
return(0);
}
建造
检查结果
Disassembly of section .text:
00080000 <_start>:
80000: eb000001 bl 8000c <notmain>
80004: eafffffe b 80004 <_start+0x4>
00080008 <bounce>:
80008: e12fff1e bx lr
0008000c <notmain>:
8000c: e92d4010 push {r4, lr}
80010: e3a04000 mov r4, #0
80014: e1a00004 mov r0, r4
80018: e2844001 add r4, r4, #1
8001c: ebfffff9 bl 80008 <bounce>
80020: e3540ffa cmp r4, #1000 ; 0x3e8
80024: 1afffffa bne 80014 <notmain+0x8>
80028: e3a00000 mov r0, #0
8002c: e8bd4010 pop {r4, lr}
80030: e12fff1e bx lr
Disassembly of section .hello_world:
00080034 <.hello_world>:
80034: 00000001 andeq r0, r0, r1
80038: 00000002 andeq r0, r0, r2
8003c: 00000003 andeq r0, r0, r3
80040: 00000004 andeq r0, r0, r4
Disassembly of section .bss:
00080034 <mybss>:
...
给予
节的反汇编。文本:
00080000 :
80000:eb000001 bl 8000c
80004:eafffffe b 80004
00080008 :
80008:E12FF1E bx lr
0008000c:
8000c:e92d4010推送{r4,lr}
80010:e3a04000 mov r4,#0
80014:e1a00004 mov r0,r4
80018:e2844001加上r4,r4,#1
8001c:EBFFF9 bl 80008
80020:e3540ffa cmp r4,#1000;0x3e8
80024:1AFFFA bne 80014
80028:e3a00000 mov r0,#0
8002c:e8bd4010 pop{r4,lr}
80030:E12FF1E bx lr
节的分解。hello_world:
000b0000:
b0000:00000001 andeq r0、r0、r1
b0004:00000002和r0、r0、r2
b0008:00000003和r0、r0、r3
b000c:00000004和r0、r0、r4
bss部分的拆卸:
000b0000:
...
如图所示,ram、rom等名称并不特殊,可以称之为bob、ted、alice等其他名称……我想有些保留字是不能使用的
同样有很多解决方案,请参阅GNU文档,我喜欢这种方法,因为它对我来说读起来更好,但您将看到跳过内存部分的解决方案
(不,这不是完全正确的代码,而是演示了汇编语言引导、C代码和链接器脚本)。非常感谢您的回复。但我的意思是将二进制文件放在输出映像中的0x80000,而不是VMA地址。例如,如果没有固件二进制文件,原始映像为200K。固件二进制文件为100K。然后,我需要创建一个组合输出图像,如:---。这只是arm…objcopy-O二进制输入输出,一旦有了链接器脚本。请参见上面的后一个示例。bin文件在第一个定义的地址空间和另一个定义的地址空间之间具有填充。你可以根据自己的意愿制作任意数量的。如果您试图在一个二进制文件中执行两个程序,那么构建两个单独的二进制文件要简单得多,并且编写5-10行程序以将它们组合到所需的输出中。比起让gnu来做,我的工作量和痛苦要少得多。我知道在linux下如何将它与cat命令结合起来。但是在第一个图像中的代码中,我需要使用fw_image_start值来读取二进制数据。这就是为什么我使用incbin将二进制数据添加到输出图像中。在没有gnu噩梦的情况下解决这个问题的各种方法,不是建议使用cat,这既不可靠也不可取。只有几行/十几行C…比gnu ld容易得多。但是对于gnuld,就像在其他stackoverflow问题中一样,您可能必须将第二个图像中的所有对象拉入链接器脚本。
arm-none-eabi-as --warn --fatal-warnings novectors.s -o novectors.o
arm-none-eabi-gcc -Wall -Werror -O2 -nostdlib -nostartfiles -ffreestanding -c notmain.c -o notmain.o
arm-none-eabi-ld -o notmain.elf -T memmap.ld novectors.o notmain.o
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy notmain.elf notmain.bin -O binary
Disassembly of section .text:
00080000 <_start>:
80000: eb000001 bl 8000c <notmain>
80004: eafffffe b 80004 <_start+0x4>
00080008 <bounce>:
80008: e12fff1e bx lr
0008000c <notmain>:
8000c: e92d4010 push {r4, lr}
80010: e3a04000 mov r4, #0
80014: e1a00004 mov r0, r4
80018: e2844001 add r4, r4, #1
8001c: ebfffff9 bl 80008 <bounce>
80020: e3540ffa cmp r4, #1000 ; 0x3e8
80024: 1afffffa bne 80014 <notmain+0x8>
80028: e3a00000 mov r0, #0
8002c: e8bd4010 pop {r4, lr}
80030: e12fff1e bx lr
Disassembly of section .hello_world:
00080034 <.hello_world>:
80034: 00000001 andeq r0, r0, r1
80038: 00000002 andeq r0, r0, r2
8003c: 00000003 andeq r0, r0, r3
80040: 00000004 andeq r0, r0, r4
Disassembly of section .bss:
00080034 <mybss>:
...
MEMORY
{
bob : ORIGIN = 0x80000, LENGTH = 0x1000
ted : ORIGIN = 0xB0000, LENGTH = 0x1000
}
SECTIONS
{
.text : { *(.text*) } > bob
.rodata : { *(.rodata*) } > bob
.hello_world : { *(.hello_world) } > ted
.bss : { *(.bss*) } > ted
}
Disassembly of section .text:
00080000 <_start>:
80000: eb000001 bl 8000c <notmain>
80004: eafffffe b 80004 <_start+0x4>
00080008 <bounce>:
80008: e12fff1e bx lr
0008000c <notmain>:
8000c: e92d4010 push {r4, lr}
80010: e3a04000 mov r4, #0
80014: e1a00004 mov r0, r4
80018: e2844001 add r4, r4, #1
8001c: ebfffff9 bl 80008 <bounce>
80020: e3540ffa cmp r4, #1000 ; 0x3e8
80024: 1afffffa bne 80014 <notmain+0x8>
80028: e3a00000 mov r0, #0
8002c: e8bd4010 pop {r4, lr}
80030: e12fff1e bx lr
Disassembly of section .hello_world:
000b0000 <.hello_world>:
b0000: 00000001 andeq r0, r0, r1
b0004: 00000002 andeq r0, r0, r2
b0008: 00000003 andeq r0, r0, r3
b000c: 00000004 andeq r0, r0, r4
Disassembly of section .bss:
000b0000 <mybss>:
...