[LLVMdev] Question about appending linkage

John Criswell criswell at uiuc.edu
Wed Apr 2 00:22:28 PDT 2008

Talin wrote:
> John Criswell wrote:
>> 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.
> This assumes that I'm writing my own linker instead of using the
> llvm-link tool, right?
Not necessarily.  You could simply run your pass with the opt tool after 
using llvm-link to link the bytecode.  That's what we do for the Linux 
kernel in the SVA project.

> At the moment I'm writing out separate bitcode files for each source
> module, and then linking them together with llvm-link. In my current
> build process none of my code is involved in the link phase. Since the
> compiler doesn't know how many modules will get linked, it doesn't know
> the array size.
Right.  You definitely need to do this after the linking phase, but you 
don't need to replace llvm-link; just run your pass via opt after 
llvm-link is done but before you do code generation.

-- John T.

More information about the llvm-dev mailing list