区域内存溢出,部分 .text 不适合区域内存

Vla*_*eev 4 c gcc compilation bare-metal ds-5

我正在尝试使用编译器(标准 C)编译裸机应用程序GCCCyclone V SoC我与处理器一起使用Cortex-A9Eclipse DS-5。我收到这些错误 -"Region ram overflowed by 295376 bytes""section .text will not fit region ram"

我认为问题不在于链接描述文件,而在于其他地方。我看到消息,编译器尝试将.c项目中的所有文件添加到一个.axf文件中,即使我的主.c文件(我编写程序的位置)中没有包含任何文件,当我.c从项目中删除一些未使用的文件时,它说"Region ram overflowed by 275433 bytes"(不同的溢出大小)。我应该怎么做才能摆脱这个错误?

old*_*mer 6

闪存.ld

MEMORY
{
    ram : ORIGIN = 0x00000000, LENGTH = 0x100
}
SECTIONS
{
    .text : { *(.text*) } > ram
    .rodata : { *(.rodata*) } > ram
    .bss : { *(.bss*) } > ram
}
Run Code Online (Sandbox Code Playgroud)

闪存

.globl _start
_start:
    b reset
    b hang
    b hang
    b hang
    b hang
    b hang
    b hang
    b hang
reset:
    mov sp,#0x8000
    bl notmain
    b hang
hang:
    b hang
Run Code Online (Sandbox Code Playgroud)

notmain.c

unsigned int data[1000];
int notmain ( void )
{
    unsigned int ra;
    for(ra=0;ra<1000;ra++) data[ra]=ra;
    return(0);
}
Run Code Online (Sandbox Code Playgroud)

生成文件

ARMGNU = arm-none-eabi

COPS = -O2 -nostdlib -nostartfiles -ffreestanding

all : notmain.bin

clean:
    rm -f *.bin
    rm -f *.o
    rm -f *.elf
    rm -f *.list

flash.o : flash.s
    $(ARMGNU)-as $(AOPS) flash.s -o flash.o

notmain.o : notmain.c
    $(ARMGNU)-gcc $(COPS) -c notmain.c -o notmain.o

notmain.bin : flash.ld flash.o notmain.o
    $(ARMGNU)-ld -o notmain.elf -T flash.ld flash.o notmain.o
    $(ARMGNU)-objdump -D notmain.elf > notmain.list
    $(ARMGNU)-objcopy notmain.elf notmain.bin -O binary
Run Code Online (Sandbox Code Playgroud)

输出:

arm-none-eabi-ld -o notmain.elf -T flash.ld flash.o notmain.o
arm-none-eabi-ld:flash.ld:10: warning: memory region `rom' not declared
arm-none-eabi-ld: notmain.elf section `.bss' will not fit in region `ram'
arm-none-eabi-ld: region `ram' overflowed by 3828 bytes
Makefile:21: recipe for target 'notmain.bin' failed
make: *** [notmain.bin] Error 1
Run Code Online (Sandbox Code Playgroud)

我本可以说 .text 不适合,但这与您遇到的问题相同。更改链接描述文件中的大小。

ram : ORIGIN = 0x00000000, LENGTH = 0x1000
Run Code Online (Sandbox Code Playgroud)

现在它很高兴

arm-none-eabi-ld -o notmain.elf -T flash.ld flash.o notmain.o
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy notmain.elf notmain.bin -O binary
Run Code Online (Sandbox Code Playgroud)

.text 部分(基本上是程序本身)对于为其分配的“内存”来说太大了。如果您使用的链接描述文件反映了您分配的真实大小,则您的程序太大,您需要将其缩小,如果不是(gcc 命令行上的 -O2)或将 static 放入,可以从优化开始前面的函数不是全局的,或者只是通过清理来总体减少代码量。这并不意味着将几行 C 语言变成一长行 C 语言而不删除任何实际功能,您需要让它做更少的事情。

或者就像我在这里的情况一样,也许您有一些 .data 或 .bss 或其他项目也在链接器脚本中定义的同一部分中,并且所有这些项目的组合占用了太多空间。在上面的示例中,将长度更改为 0x10,它会首先抱怨 .text 而没有其他内容,如上所述,如果我将其设置为 0x100,它会抱怨 .bss,然后停止抱怨,所以 ld 抱怨的是主动越线的人而不是那些人还没有被拉进去。

您可以增大长度以使其构建,然后检查 elf 文件(objdump 或 readelf 或其他文件),从那里也许可以了解哪些部分确实太大,哪些函数很大或哪些数据等。那些是全局的,不需要是由优化器内联的,等等。