[LLVMdev] Question about appending linkage

John Criswell criswell at uiuc.edu
Tue Apr 1 22:49:51 PDT 2008


Talin wrote:
> I'm trying to figure out how to do static initialization (like static
> constructors in C++ or Java). I figured I would use appending linkage -
> that is, for each module I'd generate a function that did all of the
> static initialization for that module, and then I'd put a pointer to
> that function in an array with appending linkage. Then my
> compiler-generated startup code would simply iterate through the array
> and call each function in turn, before calling my "main" method.
>
> Only problem is - how do I know when I've reached the end of the array?
>   
There are several ways to do this:

1) Link in a bytecode file at the end that has an array that ends with a 
NULL function pointer.  The last time I checked, appending arrays 
appended arrays in link order, so the NULL will be at the end.  LLVM 
does not define this behavior, so it may disappear in future releases.

2) Write a transform that either puts a NULL function pointer at the end 
or creates a global variable with the array size.

3) Put the array into its own section.  Write a GNU ld linker script 
that defines symbols at the beginning and end of the section.  You then 
write your code so that it loops until it hits the variable at the end 
of the section (the Linux 2.4 linker does this).

My personal preference would be option 2.  The transform is trivial to 
write and does not rely on undefined behavior.

-- John T.

> -- Talin
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>   




More information about the llvm-dev mailing list