静态库转换为共享库是否隐藏符号?

时间:2019-05-08 16:35:18

标签: c++ cmake linker g++ clang++

我正在尝试使用以下cmake命令将v8的静态库链接到共享库

add_library(v8jni SHARED ${THIRDPARTY_LIB_PATH}/shared/v8jni.cpp)
target_link_libraries(v8jni log
        -Wl,--whole-archive v8_inspector
        v8_base
        v8_snapshot
        v8_libplatform
        v8_libsampler
        v8_libbase
        -Wl,--no-whole-archive)

libv8jni.so成功生成。但这没有用,因为所有v8函数都标记为本地。

nm -C libv8_base.a | grep v8::HandleScope
00000000 T v8::HandleScope::Initialize(v8::Isolate*)
00000000 T v8::HandleScope::CreateHandle(v8::internal::NeverReadOnlySpaceObject*, unsigned int)
00000000 T v8::HandleScope::CreateHandle(v8::internal::Isolate*, unsigned int)
00000000 T v8::HandleScope::NumberOfHandles(v8::Isolate*)
00000000 T v8::HandleScope::HandleScope(v8::Isolate*)
00000000 T v8::HandleScope::HandleScope(v8::Isolate*)
00000000 T v8::HandleScope::~HandleScope()
00000000 T v8::HandleScope::~HandleScope()
00000000 T v8::HandleScope::operator delete[](void*, unsigned int)
00000000 T v8::HandleScope::operator delete(void*, unsigned int)
00000000 T v8::HandleScope::operator new[](unsigned int)
00000000 T v8::HandleScope::operator new(unsigned int)
         U v8::HandleScope::CreateHandle(v8::internal::Isolate*, unsigned int)
         U v8::HandleScope::HandleScope(v8::Isolate*)
         U v8::HandleScope::~HandleScope()
         U v8::HandleScope::HandleScope(v8::Isolate*)
         U v8::HandleScope::~HandleScope()

nm -C libv8jni.so |给我以下输出。

请注意,符号类型为t而不是T,这意味着它们只能在本地访问。

nm -C libv8jni.so| grep v8::HandleScope
000c0b48 t v8::HandleScope::Initialize(v8::Isolate*)
000c0c32 t v8::HandleScope::CreateHandle(v8::internal::NeverReadOnlySpaceObject*, unsigned int)
000c0c0c t v8::HandleScope::CreateHandle(v8::internal::Isolate*, unsigned int)
000c0c08 t v8::HandleScope::NumberOfHandles(v8::Isolate*)
000c0b3c t v8::HandleScope::HandleScope(v8::Isolate*)
000c0b3c t v8::HandleScope::HandleScope(v8::Isolate*)
000c4ad4 t v8::HandleScope::~HandleScope()
000c4ad4 t v8::HandleScope::~HandleScope()
000c0c02 t v8::HandleScope::operator delete[](void*, unsigned int)
000c0bfc t v8::HandleScope::operator delete(void*, unsigned int)
000c0bf6 t v8::HandleScope::operator new[](unsigned int)
000c0bf0 t v8::HandleScope::operator new(unsigned int)

v8jni.cpp非常简单

#include <jni.h>
#include <android/log.h>
#include <assert.h>

static const char* LOG_TAG = "v8jni";

extern "C" jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
    JNIEnv* env = NULL;
    jint result = -1;

    __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "JNI_OnLoad");

   if (vm->GetEnv((void **) &env, JNI_VERSION_1_4) != JNI_OK) {
       __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "GetEnv Failed!");
       return result;
   }

   assert(env != nullptr && "env is nullptr");

   return JNI_VERSION_1_4;
}

如下所示的链接命令,它是一个测试脚本。

+ rm ../../../.././build/intermediates/cmake/debug/obj/armeabi-v7a/libv8jni.so
+ /Users/leoliu/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ -v --target=armv7-none-linux-androideabi --gcc-toolchain=/Users/leoliu/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/leoliu/Library/Android/sdk/ndk-bundle/sysroot -fPIC -isystem /Users/leoliu/Library/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=15 -g -DANDROID -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -std=c++11 --sysroot /Users/leoliu/Library/Android/sdk/ndk-bundle/platforms/android-15/arch-arm -L/Users/leoliu/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -shared -Wl,-soname,libv8jni.so -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libv8jni.so CMakeFiles/v8jni.dir/src/main/cpp/thirdparty/shared/v8jni.cpp.o -llog -Wl,--whole-archive ../../../../src/main/cpp/thirdparty/library/static/armeabi-v7a/libv8_inspector.a ../../../../src/main/cpp/thirdparty/library/static/armeabi-v7a/libv8_base.a ../../../../src/main/cpp/thirdparty/library/static/armeabi-v7a/libv8_snapshot.a ../../../../src/main/cpp/thirdparty/library/static/armeabi-v7a/libv8_libplatform.a ../../../../src/main/cpp/thirdparty/library/static/armeabi-v7a/libv8_libsampler.a ../../../../src/main/cpp/thirdparty/library/static/armeabi-v7a/libv8_libbase.a -Wl,--no-whole-archive -latomic -lm /Users/leoliu/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++.a
Android clang version 5.0.300080  (based on LLVM 5.0.300080)
Target: armv7-none-linux-android
Thread model: posix
InstalledDir: /Users/leoliu/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin
Found candidate GCC installation: /Users/leoliu/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
Selected GCC installation: /Users/leoliu/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
Candidate multilib: thumb;@thumb
Candidate multilib: armv7-a;@armv7
Candidate multilib: armv7-a/thumb;@armv7@thumb
Candidate multilib: .;
Selected multilib: armv7-a;@armv7
 "/Users/leoliu/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld" --sysroot=/Users/leoliu/Library/Android/sdk/ndk-bundle/platforms/android-15/arch-arm -X --eh-frame-hdr -m armelf_linux_eabi -shared -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libv8jni.so /Users/leoliu/Library/Android/sdk/ndk-bundle/platforms/android-15/arch-arm/usr/lib/../lib/crtbegin_so.o -L/Users/leoliu/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -L/Users/leoliu/Library/Android/android-ndk-r16b/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/5.0.300080/lib/linux/arm -L/Users/leoliu/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/armv7-a -L/Users/leoliu/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/../lib/armv7-a -L/Users/leoliu/Library/Android/sdk/ndk-bundle/platforms/android-15/arch-arm/usr/lib/../lib -L/Users/leoliu/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/armv7-a -L/Users/leoliu/Library/Android/sdk/ndk-bundle/platforms/android-15/arch-arm/usr/lib -soname libv8jni.so CMakeFiles/v8jni.dir/src/main/cpp/thirdparty/shared/v8jni.cpp.o -llog --whole-archive ../../../../src/main/cpp/thirdparty/library/static/armeabi-v7a/libv8_inspector.a ../../../../src/main/cpp/thirdparty/library/static/armeabi-v7a/libv8_base.a ../../../../src/main/cpp/thirdparty/library/static/armeabi-v7a/libv8_snapshot.a ../../../../src/main/cpp/thirdparty/library/static/armeabi-v7a/libv8_libplatform.a ../../../../src/main/cpp/thirdparty/library/static/armeabi-v7a/libv8_libsampler.a ../../../../src/main/cpp/thirdparty/library/static/armeabi-v7a/libv8_libbase.a --no-whole-archive -latomic -lm /Users/leoliu/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++.a -lstdc++ -lm -lgcc -ldl -lc -lgcc -ldl /Users/leoliu/Library/Android/sdk/ndk-bundle/platforms/android-15/arch-arm/usr/lib/../lib/crtend_so.o
+ nm -C ../../../.././build/intermediates/cmake/debug/obj/armeabi-v7a/libv8jni.so
+ grep v8::HandleScope
000c0b48 t v8::HandleScope::Initialize(v8::Isolate*)
000c0c32 t v8::HandleScope::CreateHandle(v8::internal::NeverReadOnlySpaceObject*, unsigned int)
000c0c0c t v8::HandleScope::CreateHandle(v8::internal::Isolate*, unsigned int)
000c0c08 t v8::HandleScope::NumberOfHandles(v8::Isolate*)
000c0b3c t v8::HandleScope::HandleScope(v8::Isolate*)
000c0b3c t v8::HandleScope::HandleScope(v8::Isolate*)
000c4ad4 t v8::HandleScope::~HandleScope()
000c4ad4 t v8::HandleScope::~HandleScope()
000c0c02 t v8::HandleScope::operator delete[](void*, unsigned int)
000c0bfc t v8::HandleScope::operator delete(void*, unsigned int)
000c0bf6 t v8::HandleScope::operator new[](unsigned int)
000c0bf0 t v8::HandleScope::operator new(unsigned int)

引起问题的任何可能原因?预先感谢。

2 个答案:

答案 0 :(得分:0)

我可能知道答案,我怀疑根本原因是目标文件中的.hidden

cpu-arm.o:     file format elf32-littlearm

SYMBOL TABLE:
00000000 l    df *ABS*  00000000 cpu-arm.cc
00000000 l    d  .text._ZN2v88internal11CpuFeatures11FlushICacheEPvj    00000000 .text._ZN2v88internal11CpuFeatures11FlushICacheEPvj
00000000 g     F .text._ZN2v88internal11CpuFeatures11FlushICacheEPvj    00000010 .hidden _ZN2v88internal11CpuFeatures11FlushICacheEPvj
00000000         *UND*  00000000 __aeabi_unwind_cpp_pr0

nm的输出领先。

What does ".hidden" mean in the output of output objdump -t?

答案 1 :(得分:0)

对于任何遇到问题的人,v8的构建脚本中都有一个切换

v8 / gni / v8.gni

if ((is_posix || is_fuchsia) && (v8_enable_backtrace || v8_monolithic)) {
  v8_remove_configs += [ "//build/config/gcc:symbol_visibility_hidden" ]
  v8_add_configs += [ "//build/config/gcc:symbol_visibility_default" ]
}

所以我们在编译v8时需要设置v8_enable_backtrace=true,问题已解决。