2

I'm wondering if there's a way to obtain the ROM address used to seed the initial value of a RAM variable? Given a statement static uint32_t foo = 0x12345678; the initial value 0x12345678 is present as ROM somewhere to serve as the initial value at &foo. At some point in time I'd like to be able to reset the value of foo to it's initial state.

I could create a second variable const static uint32_t initial_foo = 0x12345678; to use but that will then double the ROM data space needed to store variables for this use case.

Can the offset &foo from the beginning of RAM (or more specifically &_srelocate, see the linker script below) be reliably correlated with one of the symbols in ROM space?

Selected parts of the ARM/GNU C linker v 6.3.1 script are below.

OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
OUTPUT_ARCH(arm)
SEARCH_DIR(.)

/* Memory Spaces Definitions */
MEMORY
{
  rom (rx)  : ORIGIN = 0x00400000, LENGTH = 0x00100000
  ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00020000
}

/* The stack size used by the application. NOTE: you need to adjust according to your application. */
__stack_size__ = DEFINED(__stack_size__) ? __stack_size__ : 0x3000;
__ram_end__ = ORIGIN(ram) + LENGTH(ram) - 4;

SECTIONS
{
    .text :
    {
        . = ALIGN(4);
        _sfixed = .;
        KEEP(*(.vectors .vectors.*))
        *(.text .text.* .gnu.linkonce.t.*)
        *(.glue_7t) *(.glue_7)
        *(.rodata .rodata* .gnu.linkonce.r.*)
        *(.ARM.extab* .gnu.linkonce.armextab.*)

        /* Support C constructors, and C destructors in both user code
           and the C library. This also provides support for C++ code. */
        . = ALIGN(4);
        KEEP(*(.init))
        . = ALIGN(4);
        __preinit_array_start = .;
        KEEP (*(.preinit_array))
        __preinit_array_end = .;

        . = ALIGN(4);
        __init_array_start = .;
        KEEP (*(SORT(.init_array.*)))
        KEEP (*(.init_array))
        __init_array_end = .;

        . = ALIGN(0x4);
        KEEP (*crtbegin.o(.ctors))
        KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))
        KEEP (*(SORT(.ctors.*)))
        KEEP (*crtend.o(.ctors))

        . = ALIGN(4);
        KEEP(*(.fini))

        . = ALIGN(4);
        __fini_array_start = .;
        KEEP (*(.fini_array))
        KEEP (*(SORT(.fini_array.*)))
        __fini_array_end = .;

        KEEP (*crtbegin.o(.dtors))
        KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))
        KEEP (*(SORT(.dtors.*)))
        KEEP (*crtend.o(.dtors))

        . = ALIGN(4);
        _efixed = .;            /* End of text section */
    } > rom

    /* .ARM.exidx is sorted, so has to go in its own output section.  */
    PROVIDE_HIDDEN (__exidx_start = .);
    .ARM.exidx :
    {
      *(.ARM.exidx* .gnu.linkonce.armexidx.*)
    } > rom
    PROVIDE_HIDDEN (__exidx_end = .);

    . = ALIGN(4);
    _etext = .;

    .relocate : AT (_etext)
    {
        . = ALIGN(4);
        _srelocate = .;
        *(.ramfunc .ramfunc.*);
        *(.data .data.*);
        . = ALIGN(4);
        _erelocate = .;
    } > ram

    /* .bss and stack sections removed */

    . = ALIGN(4);
    _end = . ;
}
2
  • If you just write the assignment foo = 0x12345678;, will the compiler not reinitialize the variable in the most efficient way?
    – Bo Persson
    Jul 12, 2018 at 14:59
  • You could get the address of the variable from the ROM through the linker script and do a memcpy at runtime to RAM. Definitely Lundin's answer is easier and faster.
    – Jose
    Jul 12, 2018 at 15:27

1 Answer 1

5

Instead of having one RAM variable type foo = value;, simply make one variable const type foo = value; and ensure it is allocated in ROM (should be the case if the variable has static storage duration). Then manually copy it to RAM as needed. This way the value is only stored once in ROM; initializers of ROM variables are not stored separately.

Then you won't be wasting any memory and you won't have to worry about somehow searching through .rodata for the initializer value, which would be rather questionable practice.

6
  • Yes good point. The default value of the RAM based variable can be left unspecified and explicitly copied from ROM during initialization or reset of the module.
    – mje
    Jul 12, 2018 at 17:36
  • The code to copy the value will require more ROM space than having the value stored twice (initialiser plus const var). Better use a macro for the value, an initialiser (which is very efficient if you have more than 2 or 3 variables) and just assign it if you have to "reset" the value. This avoids redundancy for the value and lets the compiler decide how to do the assignment. Jul 13, 2018 at 12:33
  • The uint32_t example above was just to serve as an example. More complex struct types consume more memory thus the motivation to avoid unnecessary copies. I don't think the ARM/GNU C (or any) compiler resolves recurring uses of a macro literal to the same memory address, thus ROM copies would be produced at each use. Using 50 "foo" literals for example, produces 50 instances of "foo" in ROM last time I checked.
    – mje
    Jul 13, 2018 at 13:33
  • @mje: Yeah, that is a bit more complex. Nevertheless, without more information, this could be an XY problem. It's in general not good practice to have extensive global (non-const) variables. Jul 13, 2018 at 19:04
  • @Olaf No, naturally an extra initializer is not stored in ROM, as that would be pointless. That's not how code is generated on any ROM-based system I have ever seen.
    – Lundin
    Jul 15, 2018 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.