Let's suppose I have a C file with no external dependency, and only const data section. I would like to compile this file, and then get a binary blob I can load in another program, where the function would be used through a function pointer.
Let's take an example, here is a fictionnal binary module, f1.c
static const unsigned char mylut[256] = {
[0 ... 127] = 0,
[128 ... 255] = 1,
};
void f1(unsigned char * src, unsigned char * dst, int len)
{
while(len) {
*dst++ = mylut[*src++];
len--;
}
}
I would like to compile it to f1.o, then f1.bin, and use it like this in prog.c
int somefunc() {
unsigned char * codedata;
f1_type_ptr f1_ptr;
/* open f1.bin, and read it into codedata */
/* set function pointer to beginning of loaded data */
f1_ptr =(f1_type_ptr)codedata;
/* call !*/
f1_ptr(src, dst, len);
}
I suppose going from f1.c to f1.o involves -fPIC to get position independance. What are the flags or linker script that I can use to go from f1.o to f1.bin ?
Clarification :
I know about dynamic linking. dynamic linking is not possible in this case. The linking step has to be cast func pointer to loaded data, if it is possible.
Please assume there is no OS support. If I could, I would for example write f1 in assembly with PC related adressing.