Fixed address variable in C

Your linker and compiler don’t know about that (without you telling it anything, of course). It’s up to the designer of the ABI of your platform to specify they don’t allocate objects at those addresses.

So, there is sometimes (the platform i worked on had that) a range in the virtual address space that is mapped directly to physical addresses and another range that can be used by user space processes to grow the stack or to allocate heap memory.

You can use the defsym option with GNU ld to allocate some symbol at a fixed address:

--defsym symbol=expression

Or if the expression is more complicated than simple arithmetic, use a custom linker script. That is the place where you can define regions of memory and tell the linker what regions should be given to what sections/objects. See here for an explanation. Though that is usually exactly the job of the writer of the tool-chain you use. They take the spec of the ABI and then write linker scripts and assembler/compiler back-ends that fulfill the requirements of your platform.

Incidentally, GCC has an attribute section that you can use to place your struct into a specific section. You could then tell the linker to place that section into the region where your registers live.

Registers regs __attribute__((section("REGS")));

Leave a Comment