Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place constant at specific address with LD linker command file?

Tags:

c

gcc

ld

I'm developing a c application on an embedded processor with a gcc-based toolchain. In my application I need to place a flag at a specific memory location. I need help with the linker command file syntax to accomplish this.

like image 897
ejwipp Avatar asked Nov 05 '13 03:11

ejwipp


2 Answers

In your C file write something like:

static int flag __attribute__ ((section (".flag"))) __attribute__ ((__used__)) = 6;

In your custom linker script, add .flag to the desired section:

_flag_start = 0x00001234;

.flag _flag_start :
{
  KEEP(*(.flag)) ;
}

Be sure to add this on the correct place, as the location pointer can only grow The location pointer will be set to _flag_start + [size of your flag] after this block, meaning that all subsequent sections will be placed at that address or higher.

And of course, read and use the manual David Grayson provided.

like image 72
parvus Avatar answered Nov 14 '22 22:11

parvus


I had to do this on a Cortex-M3 one time. My linker script is long, complicated, and private but this page helped me to write it and you might find it useful too:

http://www.linuxselfhelp.com/gnu/ld/html_chapter/ld_3.html

I would recommend finding the existing linker script that your compiler is using by default, and then modifying that by adding a special section at a specific address, and putting your flag in that section in the source code.

like image 38
David Grayson Avatar answered Nov 14 '22 22:11

David Grayson