Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ld: Undefined symbols building with BUILD_SHARED_LIBS #1224

Closed
JuliusNmn opened this issue Oct 13, 2023 · 8 comments
Closed

ld: Undefined symbols building with BUILD_SHARED_LIBS #1224

JuliusNmn opened this issue Oct 13, 2023 · 8 comments

Comments

@JuliusNmn
Copy link

I'm trying to build SVF as a shared library with the BUILD_SHARED_LIBS flag introduced in 278, but linking fails with ld: undefined symbols. Without the flag, SVF builds without errors.

~ mkdir build & cd build
~ cmake .. -DLLVM_DIR=~/Developer/SVF/llvm-14.0.0.obj -DBUILD_SHARED_LIBS=on
~ make
...
[ 85%] Building CXX object svf-llvm/CMakeFiles/obj.SvfLLVM.dir/lib/SymbolTableBuilder.cpp.o
[ 85%] Built target obj.SvfLLVM
[ 86%] Linking CXX static library libSvfLLVM_static.a
[ 86%] Built target SvfLLVM_static
[ 87%] Linking CXX shared library libSvfLLVM.dylib
ld: Undefined symbols:
  SVF::SVFIR2ItvExeState::globalNullVaddrs, referenced from:
      SVF::SVFIR2ItvExeState::getVAddrs(unsigned int) in SVFIR2ItvExeState.cpp.o
  llvm::BasicBlock::BasicBlock(llvm::LLVMContext&, llvm::Twine const&, llvm::Function*, llvm::BasicBlock*), referenced from:
      llvm::BasicBlock::Create(llvm::LLVMContext&, llvm::Twine const&, llvm::Function*, llvm::BasicBlock*) in LLVMModule.cpp.o
  llvm::DataLayout::DataLayout(llvm::Module const*), referenced from:
      SVF::LLVMUtil::getDataLayout(llvm::Module*) in LLVMUtil.cpp.o
      SVF::LLVMUtil::getDataLayout(llvm::Module*) in SVFIRBuilder.cpp.o
  llvm::ModulePass::assignPassManager(llvm::PMStack&, llvm::PassManagerType), referenced from:
      vtable for SVF::BreakConstantGEPs in BreakConstantExpr.cpp.o
...
@yuleisui
Copy link
Collaborator

This is strange. @jumormt could you take a look at this compilation error for abstract execution on Mac OS?

@jumormt
Copy link
Contributor

jumormt commented Oct 14, 2023

@JuliusNmn Apologies for any inconvenience. It appears that this feature hasn't been tested or received updates for some time. I attempted to replicate the issue on my Ubuntu system and encountered a different error when enabling this option. I will conduct further investigation into this matter. In the meantime, if you are able to resolve this issue on your end, we would welcome a pull request with the fix.

[ 77%] Built target SvfCoreObj
[ 85%] Built target obj.SvfLLVM
[ 85%] Built target SvfCore
[ 85%] Built target SvfLLVM_static
[ 86%] Linking CXX shared library libSvfLLVM.so
[ 86%] Built target extapi_ir
/usr/bin/ld: ../svf/CMakeFiles/SvfCoreObj.dir/lib/Util/NodeIDAllocator.cpp.o: warning: relocation against _ZN3SVF15NodeIDAllocator9Clusterer14NewSbvNumWordsB5cxx11E' in read-only section .text._ZN3SVF15NodeIDAllocator9Clusterer20determineBestMappingERKSt6vectorISt4pairI19hclust_fast_methodsS2_IjSaIjEEESaIS7_EESt13unordered_mapINS_8PointsToEjNS_4HashISD_EESt8equal_toISD_ESaIS3_IKSD_jEEERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERd[_ZN3SVF15NodeIDAllocator9Clusterer20determineBestMappingERKSt6vectorISt4pairI19hclust_fast_methodsS2_IjSaIjEEESaIS7_EESt13unordered_mapINS_8PointsToEjNS_4HashISD_EESt8equal_toISD_ESaIS3_IKSD_jEEERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERd]'
/usr/bin/ld: CMakeFiles/obj.SvfLLVM.dir/lib/BreakConstantExpr.cpp.o: relocation R_X86_64_PC32 against symbol `_ZTVN3SVF17BreakConstantGEPsE' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [svf-llvm/CMakeFiles/SvfLLVM.dir/build.make:298: svf-llvm/libSvfLLVM.so.14] Error 1
make[1]: *** [CMakeFiles/Makefile2:478: svf-llvm/CMakeFiles/SvfLLVM.dir/all] Error 2
make: *** [Makefile:136: all] Error 2

@JuliusNmn
Copy link
Author

I got the shared lib build to work. I have opened up a draft pull request. I assume that add_llvm_library does not properly support shared libs, so this is more of a workaround. Building works fine on macos (with and without BUILD_SHARED_LIBS), but it emits warnings about duplicate libraries, and I have only tested llvm in-source builds. Someone more experienced with CMake should look into this.

@xudon9
Copy link
Contributor

xudon9 commented Oct 18, 2023

Hi, PR #1229 should solve your problem. Feel free to give it a test on your environment.

@JuliusNmn
Copy link
Author

Unfortunately, on the current branch the build fails:

[ 78%] Linking CXX shared library libSvfCore.dylib
ld: Undefined symbols:
  SVF::SVFValue::toString() const, referenced from:
      SVF::SVFIR2ConsExeState::initValVar(SVF::ValVar const*, unsigned int) in SVFIR2ConsExeState.cpp.o
      SVF::SVFIR2ItvExeState::initValVar(SVF::ValVar const*, unsigned int) in SVFIR2ItvExeState.cpp.o
      SVF::FunptrDDAClient::performStat(SVF::PointerAnalysis*) in DDAClient.cpp.o
      SVF::AliasDDAClient::performStat(SVF::PointerAnalysis*) in DDAClient.cpp.o
      SVF::AliasDDAClient::performStat(SVF::PointerAnalysis*) in DDAClient.cpp.o
      SVF::DOTGraphTraits<SVF::ConstraintGraph*>::getNodeLabel(SVF::ConstraintNode*, SVF::ConstraintGraph*) in ConsG.cpp.o
      SVF::IntraICFGNode::toString() const in ICFG.cpp.o
      ...

@yuleisui
Copy link
Collaborator

I just tested and the shared lib had the same compilation error. I guess it is because the dynamic lib needs the definition as a self-contained build, but the definition resides in SVF-llvm. For static build, it is fine, but the dylib is not.

@xudon9 can you help with fixing it.

@yuleisui
Copy link
Collaborator

@JuliusNmn could you try this fix and let us know.
#1237

@JuliusNmn
Copy link
Author

i can confirm that it builds shared libraries now. thanks for fixing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants