A linker script tells the linker the target's memory layout and how to place the program's output sections into it, essentially "here are the physical memory regions (flash, RAM) with their addresses and sizes, and here's which sections go where." Its MEMORY command declares regions like FLASH at 0x08000000 and RAM at 0x20000000 with their lengths, and its SECTIONS command assigns .text and .rodata to flash, .data and .bss to RAM, places the vector table at the start of flash, and defines symbols and the entry point. You need a custom one on bare metal because there's no OS to provide a default memory layout; the addresses are dictated by your specific chip's memory map, and the linker must produce an image whose vector table sits where the CPU looks at reset, whose code runs from the right flash address, and whose RAM sections are set up for the startup code to initialize. The default host linker script targets a hosted OS environment and is wrong for an MCU, using it (or a mismatched one) yields an image that doesn't boot, faults, or has uninitialized globals. So the linker script is the bridge between your compiled objects and the physical reality of the chip.
Debugging & Toolchain · Interview question
What does a linker script do, and why do you need a custom one on bare metal?
A strong answer
What a weak answer sounds like
You know the answer. Do you know what gets you dinged?
Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.
From the lesson
Linker Scripts & Memory Sections
Telling the linker the chip's memory map: MEMORY and SECTIONS place .text in flash and .data/.bss in RAM, and the VMA-vs-LMA split is why startup code must copy initialized data.