2

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?

4
  • LS_size isn't known at compile time, since it's a linker script variable. You could try adding an extern const size_t LS_size; declaration in your C code, and move the initialization of vari.codesize to some function. Commented Oct 9, 2013 at 10:24
  • Then I compile the asm(.S) source I got: 0x00, (it is a _rom_size). But then I link it i got: 0x04. This means that I can specify which object file what data need to write to link stage. But I don't know how I can to write this on C code. Commented Oct 9, 2013 at 10:39
  • Well, I might be wrong, but I when you initialize the struct while declaring it I think the RHS must be a compile-time constant. Hence the suggestion to move the initialization in my previous comment. Commented Oct 9, 2013 at 10:49
  • If this is true, then it is very bad. Commented Oct 9, 2013 at 10:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.