Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
utterances-bot opened this issue May 22, 2023 · 6 comments
Open

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

utterances-bot opened this issue May 22, 2023 · 6 comments

Comments

@utterances-bot
Copy link

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/

Copy link

raybellis commented May 22, 2023

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;

Copy link
Member

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.

Copy link

xinyew commented Sep 13, 2023

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
Copy link
Member

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.

Copy link

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

@Javier-varez
Copy link
Member

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

Fixed, thanks for reporting it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants