0
votes

I wrote a small binary in cortex-a9 board, and defined a linker script like this:

SECTIONS
{
    .text :
    {
        __text = . ;
        *(.vector)
        *(.text)
        *(.text.*)
    }

    .rodata :
    {
        *(.rodata)
        *(.rodata.*)
    }

    .data   :  {
        __data_start = . ;
        *(.data)
        *(.data.*)
    }
    . = ALIGN(4);
    __bss_start = . ;
    .bss       :
    {
      *(.bss)
      *(.bss.*)
      *(COMMON)
    . = ALIGN(4);
    }
    __bss_end = .;

    . = ALIGN(4);
    __heap_start = .;
    . = . + 0x1000;    
    . = ALIGN(4);
    __heap_end = .;
     _end = .       ;
    PROVIDE (end = .)   ;
}

But it seems after --gc-sections worked and removed unused sections, the __heap_start still the value before --gc-sections get workked (I print it in code and check the ld flags):

arm-linux-gnueabihf-gcc -mcpu=cortex-a7 -msoft-float -nostdlib -Wl,--gc-sections -Wl,--print-gc-sections -Wl,-Ttext,0x04000000 -T csrvisor.lds -Wl,-Map,binary.map

Anyone knows how to change the __heap_start to correct value after --gc-sections removed unused sections?

1

1 Answers

1
votes
  • Check your compiler flags: Do they really contain -ffunction-sections -fdata-sections?

  • The heap normally (and in your case as well) starts right after the .bss section. So as for the start of the heap your linker script looks fine

  • Check if the linker really removes unused variables - if it only removes unused text sections, the value for __heap_start won't change.

Code, read-only data, initialized data et. al. normally go into the flash. If something is garbage-collected there, it won't affect your heap.

Data (initialized and uninitialized) will (eventually) turn up in the RAM. If something is garbage-collected there, it will affect your heap. So check if you really have variables which are removed by the garbage collection.

As for your linker script

  • There is no KEEP statement. Normally something like a reset handler, main et. al. must not be removed by the linker garbage collection

  • Your data section does not define the handling of initial values.

  • Your linker script does not contain region declarations (MEMORY). Check which defaults apply

  • Your sections do not have a target region: Again check which defaults apply in your case.

Examples with target regions:

.rodata :
{
    *(.rodata)
    *(.rodata.*)
} >rom

.data   :  {
    __data_start = . ;
    *(.data)
    *(.data.*)
} >ram