Search code examples
c++linkerstartupld

variables defined in linker script not coming through in c++ startup file


I have the following linker script (left out irrelevant parts)

MEMORY
{
  PROGRAM_FLASH (rx) : ORIGIN = 0x60000000, LENGTH = 0x40000 
  SRAM_DTC (rwx) : ORIGIN = 0x20000000, LENGTH = 0x10000 
  SRAM_ITC (rwx) : ORIGIN = 0x0, LENGTH = 0x10000
  SRAM_OC (rwx) : ORIGIN = 0x20200000, LENGTH = 0x20000 
}

ENTRY(ResetISR)

SECTIONS
{
     /* Image Vector Table and Boot Data for booting from external flash */
.boot_hdr : ALIGN(4)
{
    FILL(0xff)
    __boot_hdr_start__ = ABSOLUTE(.) ;
    KEEP(*(.boot_hdr.conf))
    . = 0x1000 ;
    KEEP(*(.boot_hdr.ivt))
    . = 0x1020 ;
    KEEP(*(.boot_hdr.boot_data))
    . = 0x1030 ;
    KEEP(*(.boot_hdr.dcd_data))
    __boot_hdr_end__ = ABSOLUTE(.) ;
    . = 0x2000 ;
} >PROGRAM_FLASH

.vector : ALIGN(4)
{
    FILL(0xff)
    __vector_table_flash_start__ = . ;
    __vector_table_dtc_start__ = LOADADDR(.vector) ;
    
    KEEP(*(.isr_vector))
    
    . = ALIGN(4) ;
    
    __vector_table_size__ = . ;
     
} >PROGRAM_FLASH

So relevant takeaways:

  • flash memory starts at 0x600000000
  • some XIP boot bytes are written to dedicated locations beginning of flash, followed by "my own stuff".
  • I put vector tables at 0x60002000 (or better put, nxp sdk expects some xip bytes and jumps to 0x60002000 where I can put my own stuff).
  • I am creating some variables with the current location so that I can use them in my startup script
  • When I look at the disassembled code, i can clearly see that my vector tables described 0x45 words, so with above linker script I would expect that my __vector_table_size__ at least would be 0x45

In my startup .cpp I added some externs so that I can read the memory info created by the linker.

extern unsigned int __vector_table_flash_start__;
extern unsigned int __vector_table_dtc_start__;
extern unsigned int __vector_table_size__;
    
/* some temp vars so that my assignments don't get optimized out */
volatile unsigned int i;
volatile unsigned int ii;
volatile unsigned int iii;

__attribute__ ((naked, section(".after_vectors.reset")))
void ResetISR(void) {
    // Disable interrupts
    __asm volatile ("cpsid i");
    __asm volatile ("MSR MSP, %0" : : "r" (&_vStackTop) : );
    //__asm volatile ("LDR r9, = __global_offset_table_flash_start__");
    
    i = __vector_table_flash_start__;
    ii = __vector_table_dtc_start__;
    iii = __vector_table_size__;
}

Again, nothing interesting, just some code to make sure nothing is optimized out, and that I can see the actual values of the variables in the debugger.

When we know that my flash starts at 0x60000000 and that my .vector section has an offset of 0x2000

Variable Expected Actual
vector_table_flash_start 0x6002000 0x20010000
vector_table_dtc_start 0x6002000 0x20010000
vector_table_size 0x45 0xffffffff

What I am doing wrong here??

For reference, the relevant section of disassemble (arm-none-eabi-objdump --disassemble-all eclipse/MIMXRT1024_Project/Debug/MIMXRT1024_Project.axf > disassemble_pic)

enter image description here


Solution

  • You need to check the address of the linker script variables, not their value. The address is what the linker script sets. The value is what happens to be at that address - presumably the first vector in the vector table.