writing a pass, questions

I’m moving a couple of dynamically loaded passes
from an older version of LLVM to a newer one.
I fixed various small compilation errors, and things compile now,
but the resulting .so won’t load without error.

So I re-read the manual (Writing an LLVM Pass) and tried the
first example, Hello.cpp which produces LLVMHello.cpp
The code’s already there in the source tree and it builds
and loads correctly. So I tried copying the directory
and doing a little editing. It builds but again fails to load.

Here’s what I type

opt -load LLVMZap.so -help

and here’s what I see

Error opening ‘LLVMZap.so’: /home/briggs/llvm-cilk/new_install/bin/…/lib/LLVMZap.so: undefined symbol: _ZNK4llvm12FunctionPass17createPrinterPassERNS_11raw_ostreamERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE

-load request ignored.

The error message is quite similar to what I see when I’m experimenting with my old pass.

Any ideas?

Here’s the contents of CMake.txt

If we don’t need RTTI or EH, there’s no reason to export anything

from the hello plugin.

if( NOT LLVM_REQUIRES_RTTI )

if( NOT LLVM_REQUIRES_EH )

set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/Zap.exports)

endif()

endif()

add_llvm_loadable_module( LLVMZap

Zap.cpp

DEPENDS

intrinsics_gen

PLUGIN_TOOL

opt

)

And the contents of Zap.cpp

#include “llvm/IR/Function.h”

#include “llvm/Pass.h”

#include “llvm/Support/raw_ostream.h”

using namespace llvm;

namespace {

struct Zap : public FunctionPass {

static char ID; // Pass identification, replacement for typeid

Zap() : FunctionPass(ID) {}

bool runOnFunction(Function &F) override {

errs() << "Zap! ";

errs().write_escaped(F.getName()) << ‘\n’;

return false;

}

};

}

char Zap::ID = 0;

static RegisterPass X(“zap”, “Zap Pass”);

Thanks,
Preston

Can you double-check the shared objects LLVMZap.so references (ldd on Linux)? If you don't see LLVM's Core and Support libraries listed, you might hit this error.

One possible fix is to add this to CMakeLists.txt:

  set(LLVM_LINK_COMPONENTS Core Support)

vedant

and here's what I see

Error opening 'LLVMZap.so': /home/briggs/llvm-cilk/new_install/bin/../lib/LLVMZap.so:
undefined symbol: _ZNK4llvm12FunctionPass17createPrinterPassERNS_11raw_
ostreamERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE
  -load request ignored.

The error message is quite similar to what I see when I'm experimenting
with my old pass.

​Do you build `opt` and your .so with the same LLVM source?

Regards,
chenwj​