How can I get VMA and LMA for a section?

The easiest way to get the runtime address (VMA) and load time address (LMA) for a section, is to use the AT keyword at the end of the output section description and get the base and the end address of a section explicitly with the builtin function LOADADDR().

  1. Specify two memory regions:

    • SECTION_MEM — Allocated for the .somesec section.

    • SECTION_FLASH_CODE_CACHED — Allocated for the copy of .somesec section.

      MEMORY
      {
        SECTION_MEM : org = 0, len = 0x10000
        SECTION_FLASH_CODE_CACHED : org = 0xd0000000, len = 0x10000
      }
  2. Define the .somesec section and locate it in the memory region SECTION_MEM for runtime. The AT keyword sets the load address of the section to SECTION_FLASH_CODE_CACHED.

    SECTIONS
    {
        .somesec :
    	{
    		CODE_RAM_BASE_ADDRESS_PTR = .;
    		/* output section commands */
    		CODE_RAM_END_ADDRESS_PTR = .;
    	} 	>SECTION_MEM AT> SECTION_FLASH_CODE_CACHED
    
    	_base_loc_code_rom = LOADADDR(.somesec);
    	_end_loc_code_rom = LOADADDR(.somesec) + SIZEOF(.somesec);
    }

    During runtime, you can copy the .somesec section by using the symbols CODE_RAM_BASE_ADDRESS_PTR for the destination and _base_loc_code_rom for the source. The size of the copy can be calculated by _base_loc_code_rom - _end_loc_code_rom. CODE_RAM_BASE_ADDRESS_PTR gives the runtime address (VMA) and _base_loc_code_rom gives the load time address (LMA).