Built-in Module Loading

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Therx

Built-in Module Loading

Post 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
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Built-in Module Loading

Post 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 :(
Chris Giese

Re:Built-in Module Loading

Post 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
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Built-in Module Loading

Post 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 ?
Chris Giese

Re:Built-in Module Loading

Post 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"
Post Reply