Region ram overflowed and section .text will not fit region ram

Vlad Timofeev picture Vlad Timofeev · Feb 4, 2017 · Viewed 7.6k times · Source

I'm trying to compile a bare-metal app with GCC compilator (Standart C). I use Cyclone V SoC with Cortex-A9 processor. Eclipse DS-5. I get an errors - "Region ram overflowed by 295376 bytes" and "section .text will not fit region ram".

I think that the problem isn't in linker script but in something else. I see messages that compiler tries to add all my .c files in project into one .axf file even if I include none of them in my main .c file (where I write the program) When I delete some unused .c files from project it says "Region ram overflowed by 275433 bytes" (different overflow size). What should I do to get rid of this mistake?

Answer

old_timer picture old_timer · Feb 4, 2017

flash.ld

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

flash.s

.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

notmain.c

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

Makefile

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

output:

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

I could have made it say .text wont fit, but it is the same problem you are having. Change the size in the linker script.

ram : ORIGIN = 0x00000000, LENGTH = 0x1000

and now it is happy

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

The .text section, which is your program itself basically, was too big for the "memory" allocated for it. If the linker script you are using reflects the true size of what you are allocated, your program is too big you need to make it smaller, can start by optimizing if you are not (-O2 on the gcc command line) or putting static in front of functions that are not global, or just overall reducing the amount of code by cleaning up. that doesnt mean make a few lines of C into one long line of C with no real functionality removed, you need to have it do fewer things.

Or as in my case here perhaps you have some .data or .bss or other items that are also in the same section defined in the linker script and the combination of all of them are taking up too much space. Changing the length to 0x10 in my example above it complains about .text first without the others, as above if I make it 0x100 it complains about .bss then stops complaining, so ld is complaining about the one that actively crosses the line not the ones that didnt get pulled in yet.

You can make the length larger to get it to build, then examine the elf file (objdump or readelf or whatever) and from there perhaps get an idea of what part is really too big, what functions are huge or what data, etc. Functions that are global that dont need to be that are being inlined by the optimizer, etc.