This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Force link to put a block of variables in a fixed area.

Hi:

 Is there a linker switch or any #progma keywords can force a block of variables located at a fixed area and without re-arrange the order, for example, I have the variable definition as following:

  int a

  int b

  int c

  double d

   double e

   int * f

 I'd like to force the variables locate at contiguous address, like:

   a: 0xc3000000

   b: 0xc3000004

   c: 0xc3000008

   d: 0xc3000010

   e: 0xc3000018

   f: 0xc3000020

  If I don't do anything, the linker may put a, b,c in one address area and  d and e in another address area, not contiguous address to a, b, c. I know it's easy to force a single variable to a fixed address by adding  following statement to command file:

 a=0xc3000000;

  but the project contains too many variables (more than 3000 variables) and it's a tedious work to specify address one by one. Another way is to group them as a struct or union, so they will be a single variable known by linker, but in this way, there are a lot of variable names  need to be modified due to struct name added. So is there a easy way I can assign the 1st variable to a fixed address and let next variables located at the same adjucontinuous address area?

  • You have 3000 separately named global variables, and you need all of them placed in a specific order.  Nothing wrong with that.  But it is a bit unusual.

    There is no simple and clean way to do it.  I generally tell users in this situation to define the variables in hand-coded assembly.  In C code, only provide a declaration of those variables in a header file.

    However, you have 3000 of these variables.  Here is one approach to consider ...

    Write the header file that declares all these variables.  You have to do that anyway.  But write it in a very limited subset of C.  For example, only use // for comments.  Only use a few different type names.  Exactly one declaration per source line.  And so on.  That's because you will write your own tool which uses that file as input to generate the assembly code for defining those variables.  Sticking with a limited subset of C makes this fairly easy.  Write this tool in any language you like: Perl, Python, Ruby, whatever.

    All of these variables will appear in a section you name.  Say you call it ".variables_ordered".  Then, in your linker command file, you can place the section ".variables_ordered" at the exact address required.

    Thanks and regards,

    -George