The gnu ld component provides the -sort-common option, which sorts uninitialized global parameters known as COMMON section characters by size. When the linker aligns characters to even addresses, this option helps minimize holes in the section. For example, if we define:
- main.c
char a;
short b;
char c;
int main () {return 0; }
and compile it on main.o without "sort-common", we get a "hole" of one byte between the address "a" and the address "b". If we use "sort-common", the linker will change the order of the parameters to "a, c, b", and since the size is 1 byte and the size of c is 1 byte, there will be no "hole" between their addresses. My problem is that my code is as follows:
- main.c
char a = 0;
short b = 0;
char c = 0;
...
In this case, a, b, and c are in the BSS section, which means that sort-common will not affect them.
How to sort characters of sections other than the "COMMON" section?
I also searched for many options in LD scripts, to no avail.
Update . I read something about the meaning of "Order" in the_flags segment and did not understand how to use it. Any help is noticeable.