Page 1 of 1
Built-in Module Loading
Posted: Wed Jun 04, 2003 10:52 am
by Therx
Is it possible to make a loop which calls every function linked into the kernel which begin "init_" I'd rather do this than have to add and remove lines as I add and remove module source files from the linker script
Re:Built-in Module Loading
Posted: Wed Jun 04, 2003 11:36 am
by Pype.Clicker
Yes. You can for instance write down a small perl utility that would call objdump --symbols on every binary files and put calls to _init_[a-zA-Z0-9_]+ in a autoinit.c file you would generate dinamically.
It is also possible to make use of the section-selection feature of ELF files: you could then #define a small initializor(fctname) that adds the initializing info about <fctname> into a special .init section ...
(iirc, this implies the use of some asm("name"), but it's not compatible with COFF output, though
Re:Built-in Module Loading
Posted: Wed Jun 04, 2003 10:24 pm
by Chris Giese
You could use something like static C++ constructors, but in
C. GCC supports this with __attribute__((constructor)):
Code: Select all
int init_keyboard(void) __attribute__((constructor));
int init_keyboard(void) { /* ...whatever... */ return 0; }
int init_video(void) __attribute__((constructor));
int init_video(void) { /* ... */ return 0; }
Pointers to each 'constructor' function will be placed in
the .ctors section of the .o file. Use a linker script to
fold this section into .text, and define labels at the
start and end of the pointer array:
Code: Select all
.text : {
/* ... */
*(.text*)
*(.rodata*)
start_ctors = .; _start_ctors = .;
*(.ctor*)
end_ctors = .; _end_ctors = .;
}
Now you can go through the table, calling each 'constructor'
function, from start_ctors to end_ctors. In AS:
Code: Select all
mov $start_ctors,%ebx
jmp 2f
.1:
call *(%ebx)
add $4,%ebx
.2:
cmp $end_ctors,%ebx
jb 1b
In C, maybe something like this:
Code: Select all
typedef int (*int_fn_void_t)(void);
extern int_fn_void_t *start_ctors, *end_ctors;
int main(void) {
int_fn_void_t *init;
/* ... */
for(init = start_ctors; init < end_ctors; init++)
(*init)();
/* ... */
Turbo C does something like this, using #pragma startup
Re:Built-in Module Loading
Posted: Thu Jun 05, 2003 2:14 am
by Pype.Clicker
hmm ... i didn't know about that "constructor" attribute ... is this something that came with last versions of GCC or is it something that was already present in 2.95.x ?
Re:Built-in Module Loading
Posted: Thu Jun 05, 2003 10:00 pm
by Chris Giese
__attribute__((constructor)) works with the two versions of GCC 2.95.2 available to me (DJGPP and MinGW).
The name of the constructor section was changed, though. Under GCC 2.95.2, it was ".ctor", now (GCC 3.2) it's ".ctors"