简体   繁体   English

如何将二进制文件构建到固定地址 0x80000 的图像中?

[英]How to to build a binary into a image at a fixed address 0x80000?

In our c project, we need to build a binary firmware into a image at a fixed file offset 0x80000.在我们的c项目中,我们需要将一个二进制固件构建到一个固定文件偏移量0x80000的镜像中。

Then when the image is loaded to memory.然后当图像加载到内存时。 We can load firmware from offset 0x80000 to a specified address.我们可以将固件从偏移量 0x80000 加载到指定地址。 Meanwhile, as the firmware is placed at file offset 0x80000, we can upgrade the firmare independently.同时,由于固件位于文件偏移0x80000,我们可以独立升级固件。

So I'm trying to use GNU linker script to implement that.所以我正在尝试使用 GNU 链接器脚本来实现它。 What I do now is use incbin to include my binary file in a asm file.我现在要做的是使用 incbin 将我的二进制文件包含在 asm 文件中。 And in linker script, my code is:在链接器脚本中,我的代码是:

       .fw_image_start : {
                *(.__fw_image_start)
        }

       .fw_image : {
              KEEP(*(.fw_image))
        }

       .fw_image_end : {
                *(.__fw_image_end)
        }

Then I can use fw_image_start to load firmware in image code.然后我可以使用 fw_image_start 在图像代码中加载固件。

But I still can't find a way to put the firmware binary to file offset 0x80000 in the final image.但是我仍然找不到将固件二进制文件放入最终映像中的文件偏移量 0x80000 的方法。

Could you help me on this?你能帮我解决这个问题吗?

Thank you in advance!先感谢您!

What did you find when you looked at the documentation and examples from GNU?当您查看 GNU 的文档和示例时,您发现了什么? Some of it is admittedly confusing or misleading, but some is pretty easy.不可否认,其中一些令人困惑或误导,但有些则很容易。 This should give a hit of at least one way to do it (there are multiple ways to solve your problem).这应该至少有一种方法可以做到(有多种方法可以解决您的问题)。

novectors.s novectors.s

.global _start
_start:
    bl notmain
    b .

.globl bounce
bounce:
    bx lr

.section .hello_world
.word 1,2,3,4

notmain.c不是main.c

void bounce ( unsigned int );
unsigned int mybss[8];
int notmain ( void )
{
    unsigned int ra;

    for(ra=0;ra<1000;ra++) bounce(ra);

    return(0);
}

memmap.ld内存映射表

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

build建造

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

examine results检查结果

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>:
    ...

Naturally:自然:

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
}

gives

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>:
    ...

and as demonstrated the name ram, rom, etc are not special can call it other things like bob, ted, alice...I assume there are some reserved words you cant use.正如所展示的,ram、rom 等名称并不特殊,可以将其称为 bob、ted、alice 等其他名称……我认为有些保留字您不能使用。

Again there are numerous solutions, see the GNU documentation, I like this method as it reads better for me, but you will see solutions that skip the MEMORY part.同样有很多解决方案,请参阅 GNU 文档,我喜欢这种方法,因为它对我来说读起来更好,但是您会看到跳过 MEMORY 部分的解决方案。

(no this wasnt intended to be completely correct code, but demonstrates the assembly language bootstrap, the C code and the linker script). (不,这不是完全正确的代码,而是演示汇编语言引导程序、C 代码和链接描述文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM