# Linker script and data allocation

I know that .data will be in the RAM, but I came across article that says during boot of an micro-controller the initial value of variables is stored in the ROM and then the boot-loader copy the initial values of variables from ROM to RAM, Is this true!?

• – Lundin Aug 21 '19 at 6:26

Ask yourself how could the system work if it isn't true?

In most implementations, the .data segment is for initialized data, where .bss is for data that is set to zero. You can't count on RAM being set to anything at all on power-up, so the processor has to zero out the .bss segment and initialize .data.

Usually this is done by storing an image of the start-up value of the .data segment in ROM, and copying it to the correct location in RAM on startup.

• the bootloader of laptops and computer also does the same thing!? – Abd El-Rahman Akram Aug 19 '19 at 20:19
• It is not the bootloader but the startup code of every program stored in ROM regardless of platform. – Eugene Sh. Aug 19 '19 at 20:32
• PC bootloaders are a considerably different animal. The BIOS probably does it (most modern-day BIOS's are probably written in C), but the bootload process is multi-step, and the actual operating system comes from disk. I suggest searching on some variation of "how does the PC bootload process work" if you want to study up on that one. – TimWescott Aug 19 '19 at 20:33
• A modern BIOS is a bunch of tens of firmwares running on different components and loaded from some kind of ROMs at different stages. And yes most (if not all) of these are working this way. – Eugene Sh. Aug 19 '19 at 20:37
• No. A bootloader is a separate program which is running before the others and is intended to allow other programs to run. A startup code is a part of the same program, which is running before main (in C) and responsible for setting up the memory, stack and the data ad bss sections as explained for this specific program. Then it is calling main. – Eugene Sh. Aug 19 '19 at 20:48

Yes. That's how values of variables get initialized, and the rest of the variables are in .bss section which is normally cleared to zero on startup.

• this also happens in big laptops an computer right !? – Abd El-Rahman Akram Aug 19 '19 at 20:18
• Yes if it is a program written in C language for example, C startup code initializes variables for you before main. If it is a program written in assembly, there is no startup code to do anything. – Justme Aug 19 '19 at 21:14
• why in assembly there is no startup code?! How this operation works in assembly – Abd El-Rahman Akram Aug 19 '19 at 21:56
• Nobody forces you to do anything in assembly, you can write your startup if you like, or link the assembly with C startup. Feel free to look at how STM32 startup works for example. But this is now off topic for Electrical Engineering – Justme Aug 20 '19 at 4:08