allthingsembedded / allthingsembedded.github.io

AllThingsEmbedded blog sources
https://allthingsembedded.com
0 stars 0 forks source link

post/2020-04-11-mastering-the-gnu-linker-script/ #3

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Mastering the GNU linker script - AllThingsEmbedded

Most people getting started with embedded development seem to find linker scripts just another piece of magic required to get up and running with their system. Even when they might already be familiar with memory-mapped peripherals and basic embedded concepts, the linker script and how it interacts with the GNU linker (ld) is still pretty mysterious. Today we will go through the main functions of a linker script to try to shed some light onto their operation.

https://allthingsembedded.com/post/2020-04-11-mastering-the-gnu-linker-script/

raybellis commented 1 year ago

In the last section, why bother with the .gpio_reg section and __attribute__ when you can just declare the symbol direct in the linker script:

gpio_reg = 0x40002000;
Javier-varez commented 1 year ago

Both approaches would work well. My preference is to keep the linker script with as few symbol definitions as possible, restricted to what is strictly necessary for your bootstrapping the target application. Using a section and declaring the variable in that section has the advantage that the symbol does not require C linkage and could be name-spaced or could have internal linkage, providing better encapsulation.

xinyew commented 1 year ago

Thanks for the wonderful tutorial. One question: why the code in the last example is S_GPIO_REG gpio_reg __attribute_((section(".bss.gpio_reg"))); instead of S_GPIO_REG gpio_reg __attribute_((section(".gpio_reg")));? Isn't the name of the section to be used .gpio_reg?

Javier-varez commented 12 months ago

Great question @xinyew. I didn't touch upon why I lean towards using .bss.gpio_reg instead of plain gpio_reg. I simply prefer it this way because you may still build and test your driver into a unit test without having to change any of the sources. Since the default linker script typically will have a .bss* input section matcher, it will cause all register sections to be included within the bss, which can help to mock registers.

SeanZhou0616 commented 6 months ago

nit: __attribute__(()) missed an underscore in the last example C code.

Javier-varez commented 5 months ago

nit: __attribute__(()) missed an underscore in the last example C code.

Fixed, thanks for reporting it.