We have a substantial embedded project using the ARM Cortex M33 processor, written in about 30,000 lines of 'C'. We use the Gnu compiler/linker toolchain. There is a mix of custom software, some open source libraries (e.g. - FatFS), and vendor-specific libraries (e.g. - Bluetooth stack). We have about 500 named static variables and data structures that occupy RAM. Variables are a range of sizes from one byte up to hundreds of byte for a structure. For the most part, variables are one, two, or four bytes.
The linker is grouping all variables from a single file by size and adds alignment bytes when switching between groups of four byte and two byte variables, or two byte and one byte byte variables. The amount of RAM lost to this alignment has become a problem, as RAM is limited.
I would like to somehow instruct the compiler and linker to put all variables of one byte into a single linker section, two bytes into another linker section, four bytes into a third linker section, and everything else into a fourth linker section. Alignment for each section can then be controlled and the alignment padding should disappear or be minimized. It would also work if I could tell the linker to sort variables by size when assigning memory addresses.
How can I do this, or something like this? The gnu compiler and linker manuals don't seem to indicate how to tell the compiler to do anything like this. The amount of RAM that has disappeared to fill in the cracks is now about 3% of our total available memory and we are running out. I am looking for creative suggestions and would like to hear about any partial solutions you may know of.