Exporting C global variables to Cortex-M vector table

Asked by Prakash K Balasubramanian

I have in my C program two global variables defined.
      const uint32_t gVar1 = 25;
      const uint32_t gVar2 = 35;

The vector table is defined in an assembly file and I would like the values from the aforesaid global variables to be assigned as individual entries in the cortex vector table

    Vector Table:

     .long InterruptHandler1
     .long InterruptHandler2
     .long gVar1
     .long gVar2
     .long InterruptHandler3 and so ob

When a dump of the generated elf is taken, it is found that the vector table contains the addresses of the two variables rather than the values assigned to them.

The following also doesnt quite help.
     .long =gVar1
     .long =gVar2

How can this be accomplished?

Question information

Language:
English Edit question
Status:
Solved
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
chengbin
Solved:
Last query:
Last reply:
Revision history for this message
Joey Ye (jinyun-ye) said :
#1

     .long InterruptHandler1
     .long InterruptHandler2
.global gVar1
gVar1:
.long 25

.global gVar2
gVar2:
.long 35

Revision history for this message
Prakash K Balasubramanian (prakash-balasubramanian) said :
#2

Hi Joey.

Constraint here is that the global variables have to be defined in the C file. And these values can change from one project to another while the startup files dont (such as the assembly file).

I even tried:
 Vector Table:
     .long InterruptHandler1
     .long InterruptHandler2
     .long CLKVAL1
     .long CLKVAL2
     .long InterruptHandler3 and so on

.extern globVar1
.extern globVar2

.equ CLKVAL1,globVar1
.equ CLKVAL2,globVar2

The behavior remained the same.

Revision history for this message
Andreas Fritiofson (andreas-fritiofson) said :
#3

Hi!

It can't be done like that. The contents of variables isn't visible outside the compilation unit, only their names (symbols) and their addresses.

The normal way to share values (not variables) between compilation units is to use #defines declared in a common header. You can use the preprocessor for assembler files as well, but you need to name the file with a capital .S extension (or add some flags to force it).

Revision history for this message
Best chengbin (can-finner) said :
#4

Well, there is another way with the help of link script.
step1: Define vector table as you did
 Vector Table:
     .long InterruptHandler1
     .long InterruptHandler2
     .long CLKVAL1
     .long CLKVAL2
     .long InterruptHandler3 and so on
step2: Declare the two extern variables
extern long global_clkval1, global_clkval2;

step3: Provide below code in link script
SECTIONS
{
  .text :
    {
      *(.text)
      PROVIDE(global_clkval1= ADDRESS_OF_CLKVAL1);
      PROVIDE(global_clkval2= ADDRESS_OF_CLKVAL2);
    }
}
And some notes:
1. Don't define global_clkval1/global_clkval2 in any assembly/c code.
2. Make sure the ADDRESS_OF_CLKVAL1/ADDRESS_OF_CLKVAL2 are the address of the two table entry when program running.
3. You cannot modify the two variables If the vector table is in read only memory

Now you can refer global_clkvar1/global_clkvar2 as normal ones .

Revision history for this message
Prakash K Balasubramanian (prakash-balasubramanian) said :
#5

Solutions of Andreas and Chengbin work.

I am implementing Andreas' solution as this must be replicated for other toolchains as well.

Revision history for this message
Prakash K Balasubramanian (prakash-balasubramanian) said :
#6

Thanks chengbin, that solved my question.