Jays
Jays

Reputation: 13

c compiler, overlap memory error

Below is a portion of the C code I am using:

pushbutton_ISR()
{

    int press;
    int key_pressed;

    press = *(KEYS_ptr + 3);                // read the pushbutton Edge Det Register interrupt register
    *(KEYS_ptr + 3) = 0;                    // Clear the Edge Det registers. 

    if (press & 0x1) {                      // KEY1
        key_pressed = KEY1;
        //sum = sum + *NEW_NUMBER; 
        }
    else if (press & 0x2)   {               // KEY2
        key_pressed = KEY2;
        *GREEN_LEDS = *NEW_NUMBER;
         sum = sum + *NEW_NUMBER;
        *RED_LEDS = sum;
        }
    else                                    // i.e. (press & 0x8), which is KEY3
        sum = *(NEW_NUMBER);                // Read the SW slider switch values; store in pattern

    return;
}

The compiler compiles this fine and the code appears to run (on an Altera board) fine. However, when I change the first if statement to:

if (press & 0x1) {                      // KEY1
    //key_pressed = KEY1;
    sum = sum + *NEW_NUMBER; 
}

the compiler gives the following error messages:

.../nios2-elf/bin/ld.exe: section .data loaded at [00000a00,00000e0f] overlaps section .text loaded at [00000500,00000a0f]
.../nios2-elf/bin/ld.exe: section .ctors loaded at [00000a10,00000a13] overlaps section .data loaded at [00000a00,00000e0f]
.../nios2-elf/bin/ld.exe: Z:/Projects/Altera/3215_W15_LabB/Part2/from_handout.elf: section .data vma 0xa00 overlaps previous sections
.../nios2-elf/bin/ld.exe: Z:/Projects/Altera/3215_W15_LabB/Part2/from_handout.elf: section .ctors vma 0xa10 overlaps previous sections
.../nios2-elf/bin/ld.exe: Z:/Projects/Altera/3215_W15_LabB/Part2/from_handout.elf: section .rodata vma 0xa14 overlaps previous sections
.../nios2-elf/bin/ld.exe: Z:/Projects/Altera/3215_W15_LabB/Part2/from_handout.elf: section .sdata vma 0xe10 overlaps previous sections
.../nios2-elf/bin/ld.exe: Z:/Projects/Altera/3215_W15_LabB/Part2/from_handout.elf: section .sbss vma 0xe18 overlaps previous sections

Could you please advise me about the reasons for these errors, and how to resolve them.

Upvotes: 1

Views: 2270

Answers (2)

M.M
M.M

Reputation: 141628

When you add in this line, it causes the size of the compiled code to be too big for the memory area that you are loading the code into.

You can see from the first line of the linker error message that .text (the code) is loaded at 0x500, and .data (the non-zero static variables) is loaded at 0xa00. However, the .text section is so long that it is too big to fit in the space between 0x500 and 0xa00.

To fix this you will either need to:

  • Make your code smaller
  • Increase the amount of space available for .text

To do the first one, you could use -Os or similar compiler option to compile for minimum code size ; or manually rewrite your code to be smaller.

For the second one you really need to understand the hardware you are loading the code into. Is it a hardware requirement that code goes at 0x500 and data goes at 0xa00? If not, then you may be able to load the code and/or data into different addresses.

These addresses are configured in your linker script (this may be hardcoded into the makefile or it may be an actual file somewhere). Hopefully the hardware device came with documentation that explains how much memory it has and where you're allowed to load your code to.

Upvotes: 0

Brian McFarland
Brian McFarland

Reputation: 9432

This has nothing to do with your code being incorrect.

These are linker errors (it even tells you ld.exe is the program complaining) about output sections overlapping. This probably means you just ran out of space, but could also mean the linker directive file your project is using has some problems.

Upvotes: 2

Related Questions