GNU linker script - how to automatically distribute code to multiple dis-contiguous sections
Asked Answered
C

0

6

I'm modifying an existing .ld and I have two flash sections for the code. These two flash sections are not contiguous so I cannot merely extend the definition of one of the sections to include the other.

Currently all of the code can fit in sec1 but if I turn off space optimizations, it doesn't fit.

QUESTION

How do I write the .ld to say place as much of the code as will fit in sec1 and then as much as will fit in sec2 and then ... sec3... etc?

I know how to place specific .obj in specific section but that's not what I want because I'll need to constantly modify the .ld as my code changes.

Cymbiform answered 29/10, 2018 at 21:53 Comment(7)
You need to manually specify what goes in one flash and what goes to the other. The most that the linker script can do, you specify exactly what goes to one flash, and with wildcards the other flash will pick up the rest.Batsman
If it almost fits in sec1, you might be able to put the .text in sec1 and things like .rodata, .init_array, etc. in sec2. That way you won't need to keep changing the script as your code changes. Just an idea.Philip
There is nothing in the built-in functions that might do this. You could make phony output sections and discard them, but see which one will fit and then use a variable to place them in the sec. Another way might be to not have seperate sections, but place a binary called 'hole.o' that is unused, but fills the space in between. One issue is the output format that you eventually end up at. Is it ELF, binary, etc. Some of the choices will depend on the output format. Are you using a flasher, a boot loader, etcTwotime
Of course, you can use scripts/make to determine sizes outside of ld. These can generate synthetic files to be included from the main linker script. So, an external script looking at the '.o' with objdump/readelf can construct 'sec1.ld', 'sed2.ld', to be included in the main link file. I doubt there is a canonical answer given all the choices.Twotime
Hey artless, thanks for your comment. Do you think there's an easy solution when involing external tools, like make and not purely gnu-ld? I think of compiling all c files into o files and then determining which object file goes where by calculating an "optiomal" placement. Do you think that would be feasible? Are there any tools that provide such computation?Rothmuller
@Rothmuller Absolutely, that is feasible. Another way is to create sections and mark them using attributes. You can group the functions this way. Ie, #define FEAT_XX __attribute__((section(".ConfigDancingPixie"))). You can use NOCROSSREFS_TO() to enforce calls between features. I did this to map out initialization code from memory space after boot. If you have a lot of 'extra' room in the combined section, I think this method can work well. If you are trying to be 100% generic, then the 'make' and scripting will work.Twotime
@Rothmuller I don't know of any tools, because it is a little specialized. I have seen OKL4 microvisor and it used 'python' for some of these things. I think that if you can get the sizes, it is easy to manipulate them. But I think that some 'bespoke' code will work best; the code to calculate optimal should not be too difficult.Twotime

© 2022 - 2024 — McMap. All rights reserved.