Converting swiftc generated LLVM IR to executable and linking against still causes segmentation fault

I am trying to generate the LLVM IR of a swift program, run a pass on it and then compile this down to an executable to run it. I identified the variables that are linked against the executable when compiled with swiftc directly and linked them. However, when I run the executable it causes a segmentation fault. This works fine for simple programs that do not make use of any data structures but as soon as I use arrays and have to link against a certain library it causes the problem. A replicable example would be the following:

Swift version I am using is 5.1

var list = [1, 3, 4, 5]; 
var temp = 0; 
list[0] = 1; 
temp = list[0];

I get the llvm ir (I need the Onone debug version)

swiftc -g -Onone -emit-llvm

I get the required libraries with ldd from the executable created without emitting the IR.

I then like the libraries with clang as llc does not accept llvm ir as input. I link the libraries and that works just fine however when I run the problem I get a segmentation fault and I cannot understand why. does anybody have an idea?

thank you!

1 Like