Reputation: 1059
I want to place a specific variable at the end of its memory section.
So if I have:
file1.cpp:
__attribute__((section(".mysection"))) char var1[] = "var1";
and in another file2.cpp:
__attribute__((section(".mysection"))) char var2[] = "var2";
How can I force var2
to be at the end of mysection
?
Upvotes: 4
Views: 7979
Reputation: 1059
Well, I ended up taking a whole different approach but i wanted to share my final conclusion here: I base this on How to fetch the end address of my code
In the code, you must add an extern reference to the variable:
extern char var2[];
A linker script must be written as follows:
SECTIONS
{
.mysection : {
*(.mysection);
var2 = .;
}
}
INSERT AFTER .mysection
Add the linker script during the linkage (e.g ld -T <PATH_TO_MY_LINKER_SCRIPT>
)
The INSERT AFTER
part is used so my linker script would be added to the default linker script.
I had to use 'gold' to link my elf file and apparently the version I used doesn't support the 'INSERT AFTER' syntax. So the actual solution should be to copy the default linker script and just add my script information to it.
I haven't tested it though, but I still hope it can help someone.
Upvotes: 3
Reputation: 57678
You will have to create your own section in the linker command file and place your section appropriately in the variables section in the linker command file.
With most linkers, you cannot tell it the order of the exact variables. The easier solution is to create one section for each variable and tell the linker how you want the sections ordered.
Look up the exact syntax for the linker command file of the GNU compiler collection.
Upvotes: 0