I need to collect some data from different .a files to one array. I do it by collecting data to one section
first .c file
TArElement __attribute__((section(".my.special.section"))) uwiveuve = { ...
second .c file
TArElement __attribute__((section(".my.special.section"))) egwegwxb = { ...
etc.
in ld script
__my_mega_array_begin = ABSOLUTE(.);
KEEP(*(.my.special.section))
__my_mega_array_end = ABSOLUTE(.);
in main .c file
extern TArElement *__my_mega_array_begin
extern TArElement *__my_mega_array_end
const t_size array_size = __my_mega_array_end - __my_mega_array_begin;
So anyone can link his code with my code, and my code will know about data in his code. Ok, it works, but not actually... The problem is that KEEP directive works completely with .o files, but not .a . if no sections is used in particullar .o file inside of .a file, then whole .o file will be discarded from linking even though KEEP directive is used.
Using of --whole-arhive option to ld will help, but I'm not allowed to use this option for some reasons. I should do all with ld script file only...
Another way to solve problem is to use partial linking, by creating relocatable file. So all .o files inside of .a will linked to one .o file. But I'm not allowed to use partial linking either.
So I should do it only by using ld script and using .a file.
来源:https://stackoverflow.com/questions/6568844/how-to-collect-data-from-different-a-files-into-one-array-how-to-keep-sections