1

I currently have a very rudimentary linker script, which sets the start of memory to 0x80000000, and then has the text, data, and bss sections and nothing else, as seen below:

MEMORY
{
   mem: ORIGIN = 0x80000000, LENGTH = 4M
}

SECTIONS
{
  .text : { *(.text) } > mem
  .data : { *(.data) } > mem
  .bss : { *(.bss) } > mem
}

This seems to work fine, until I try to define a static integer within a function, and to test I gave it an initialized value of 0x12345678.

The code compiles and links, and according to the map file that static variable is allocated right at the beginning of the bss section. The trouble is, it's not being assigned to 12345678, or to anything else I might try to set it to initially. The address in the output binary that it's getting assigned to actually contains the compiler info string. If I open the output binary in a hex editor, I can't find 0x12345678 anywhere, nor its endian-swapped counterpart 0x78563412.

I'm fairly certain there's something I'm missing in the linker script, but I have no idea what it could be. Does anyone know?

3
  • It's a very common setup for embedded systems to have a "minimal" setup, where .data and .bss initialization are removed entirely, in order to cut down system boot-up time. The opposite of this is usually called "standard setup" or "ANSI/ISO setup" etc. Which toolchain are you using and how did you create the project? Commented Jun 5, 2020 at 6:36
  • I miss something of a section which is usually called .data_ro. The read-only init values for initialising the .data section. If it is missing or your setup does not use it (see comment by Lundin above) it would explain the lack of init. Commented Jun 5, 2020 at 6:54
  • Also, on the typical embedded system you have flash memory and then .text is never placed together with .data/.bss, because those will be in RAM but .text in flash. Commented Jun 5, 2020 at 8:44

1 Answer 1

1

it will not magically set itself and the linker script is not responsible for that.

  1. The initialized data is not placed in the .bss segment. It should be placed in the .data segmnent.
  2. The .data segment is initialized by the startup code - it copies the constant data from the non volatile memory (it has to remembered somewhere between resets). You do not have nonvolatile segment defined and probably you do not have the startup code as well. So you should not expect variables to be initialized or zeroed.
  3. This linker script is far too simple (even trivial).
Sign up to request clarification or add additional context in comments.

2 Comments

If the linker script is too simple, do you know of a good place to get or to learn to write a better one? I'm trying to set a fixed address for the binary, and to have the entry point be right at the beginning.

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.