Featured post
How to create modules in C -
i have interface want able statically link modules. example, want able call functions (albeit in seperate files) called foo or match prototype, make call function in file without header in other files. dont impossible since found hack can it, want non hacked method. (the hack use nm functions , prototypes can dynamically call function). also, know can dynamic linking, however, want statically link files. ideas?
this common in writing test code. e.g., want call functions start test_. have shell script grep's through .c files , pulls out function names match test_.*. script generates test.c file contains function calls test functions.
e.g., generated program like:
int main() { inittestcode(); testa(); testb(); testc(); }
another way use linker tricks. linux kernel initialization. functions init code marked qualifier __init. defined in linux/init.h follows:
#define __init __section(.init.text) __cold notrace
this causes linker put function in section .init.text. kernel reclaim memory section after system boots.
for calling functions, each module declare initcall function other macros core_initcall(func), arch_initcall(func), et cetera (also defined in linux/init.h). these macros put pointer function linker section called .initcall.
at boot-time, kernel "walk" through .initcall section calling of pointers there. code walks through looks this:
extern initcall_t __initcall_start[], __initcall_end[], __early_initcall_end[]; static void __init do_initcalls(void) { initcall_t *fn; (fn = __early_initcall_end; fn < __initcall_end; fn++) do_one_initcall(*fn); /* make sure there no pending stuff initcall sequence */ flush_scheduled_work(); }
the symbols __initcall_start, __initcall_end, etc. defined in linker script.
in general, linux kernel of cleverest tricks gcc pre-processor, compiler , linker possible. it's been great reference c tricks.
- Get link
- X
- Other Apps
Comments
Post a Comment