ASM
ASM

Reputation: 31

Linker script, init C variables

I try find answer long time. Sorry but I really can't find it.

I use gcc,ld,gcc(for assembler compilation).

I wrote ld script:

SECTIONS
{
  .text : *{.text}
}
LS_size = (SIZEOF(.text) + 2048 ) & ( 0xF800 ) >> 9 ;

I try use LS_size from assembler:

_rom_size:
.byte LS_size

Its work fine, but on C code I can't set value to variable:

struct my_struct vari = {
  .codesize = LS_size,
}

If I extern LS_size, I got:

error: initializer element is not constant

How can I set default value for my constant C variables from variables, which are produced in ld scripts?

Upvotes: 2

Views: 2240

Answers (2)

Write in C:

struct my_st { 
    long the_code_sz;
};

struct my_st vv = {
  .the_code_sz = ((long)(&LS_size))
};

Then you can use

vv.the_code_sz

LS_size is a symbol and the address of that symbol is the value you gave it in the linker script.

Upvotes: 1

Pica
Pica

Reputation: 405

Also remember that linkerskript variables are not datatypes. They represent adresses- and what is stored there is completely arbitrary

Its bascially what you get with &VariableName in C.

Upvotes: 0

Related Questions