Is this a use for Linker script?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
karim
Posts: 21
Joined: Mon Jun 23, 2025 8:03 pm

Is this a use for Linker script?

Post by karim »

If I have 3 C files and compile them, I get 3 .o (object) files. The linker takes these 3 .o files and combines their code into one executable file. The linker script is like a map that says where to place the .text section (the code) and the .data section (the variables) in the RAM. So, the code from the 3 .o files gets merged into one .text section in the executable, and the linker script decides where this .text and .data go in the RAM. For example, if one C file has a function declaration and another has its definition, the linker combines them into one file. It puts the code from the first C file and the code from the second file (which has the function’s implementation used in the first file). The linker changes every jump to a specific address in the RAM and every call to a function by replacing it with an address calculated based on the address specified in the linker script. It also places the .data at a specific address and calculates all these addresses based on the code’s byte size. If the space allocated for the code is smaller than its size, it’ll throw an error to avoid overlapping with the .data space. For example, if you say the first code instruction goes at address 0x1000 in the RAM, and the .data starts at 0x2000 in the RAM, the code must fit in the space from 0x1000 to 0x1FFF. It can’t go beyond that. So, the code from the two files goes in the space from 0x1000 to 0x1FFF. Is what I’m saying correct?
Octocontrabass
Member
Member
Posts: 6245
Joined: Mon Mar 25, 2013 7:01 pm

Re: Is this a use for Linker script?

Post by Octocontrabass »

You can include the link to your Reddit post. Or this one. Or this one. Or this one. Also, have you considered posting your follow-up questions in one of your previous threads instead of starting a new thread?
karim wrote: Mon Jul 21, 2025 2:44 pmFor example, if you say the first code instruction goes at address 0x1000 in the RAM, and the .data starts at 0x2000 in the RAM, the code must fit in the space from 0x1000 to 0x1FFF.
For OS development, you usually specify only one start address and tell the linker to put each section after the end of the previous section, so there's no limit to how large each section may be. Limiting the size of each section like this usually only happens in embedded systems.
karim wrote: Mon Jul 21, 2025 2:44 pmIs what I’m saying correct?
For the most part, yes.
Post Reply