addPassesToEmitFile

Hi,

Long time ago (llvm-svn june 2008) I asked here about a way to output
the assembly code of my JIT generated code to a string, so I could use
it to read it on the screen. I came up with this solution:

    std::string Err;
    const llvm::TargetMachineRegistry::entry* _arch =
llvm::TargetMachineRegistry::getClosestTargetForJIT(Err);

    std::string FeaturesStr;
    llvm::TargetMachine* target(_arch->CtorFn(*_module, FeaturesStr));
    assert(target && "Could not allocate target machine!");

    std::ostringstream os;

    target->addPassesToEmitFile(*_passManager, os,
llvm::TargetMachine::AssemblyFile, true);
    target->addPassesToEmitFileFinish(*_passManager, 0, true);

However, in newer versions of llvm, like 2.5 that I'm testing now, I
had to change the code to something like this:

    std::string Err;
    const llvm::TargetMachineRegistry::entry* _arch =
llvm::TargetMachineRegistry::getClosestTargetForJIT(Err);

    std::string FeaturesStr;
    std::auto_ptr<llvm::TargetMachine> target(_arch->CtorFn(*_module,
FeaturesStr));

    std::ostringstream os;
    llvm::raw_ostream *Out = new llvm::raw_os_ostream(os);
    target->addPassesToEmitFile(*_passManager, *Out,
llvm::TargetMachine::AssemblyFile, true);
    target->addPassesToEmitFileFinish(*_passManager, 0, true);

Both versions were based on the code from llc of its release.
Everything compiles perfectly in both versions. However, when the

_passManager->run(*_module);

is called, I get a Segmentation fault that I'm unable to figure out,
as I can't track it with gdb. The first version was working perfectly.

I would really appreciate if someone could give me a clue of what I'm
doing wrong...

Thank you,

Bests,

Well, I've been before hours trying this, but soon after I sent the
email I found something. However is quite intriguing.
I just changed the order of and the static libraries that I was linking.

How can this be possible??

I'm using Cmake for building my llvm projects, so I choose the order
and I pick the .a libraries I want to link by hand...

Thank you,

alvaro

When you say 'static libraries' do you mean static libraries or shared
objects (.so)... Because if you mean shared objects, then it could
very well explain you crash.

No, I mean these libraries and objects:

libLLVMAnalysis.a
libLLVMArchive.a
libLLVMAsmParser.a
libLLVMAsmPrinter.a
libLLVMBitReader.a
libLLVMBitWriter.a
libLLVMCodeGen.a
libLLVMCore.a
libLLVMDebugger.a
libLLVMInstrumentation.a
libLLVMipa.a
libLLVMipo.a
libLLVMLinker.a
libLLVMScalarOpts.a
libLLVMSelectionDAG.a
libLLVMSupport.a
libLLVMSystem.a
libLLVMTarget.a
libLLVMTransformUtils.a
LLVMAlphaAsmPrinter.o
LLVMAlphaCodeGen.o
LLVMARMAsmPrinter.o
LLVMARMCodeGen.o
LLVMCBackend.o
LLVMCellSPUAsmPrinter.o
LLVMCellSPUCodeGen.o
LLVMCppBackend.o
LLVMExecutionEngine.o
LLVMInterpreter.o
LLVMJIT.o
LLVMSparcCodeGen.o
LLVMX86AsmPrinter.o
LLVMX86CodeGen.o

However the ones that compile and run are not these ones, but a
selection and a specific order. The point is that choosing wrong ones
or changing the order leads to the crash (but perfect compilation and
linking). Unfortunately, I was mad trying combinations, so I only kept
the working one :frowning:

Thanks for your answer.