Stripping linux shared libraries

前端 未结 6 1957
醉梦人生
醉梦人生 2020-12-12 18:00

We\'ve recently been asked to ship a Linux version of one of our libraries, previously we\'ve developed under Linux and shipped for Windows where deploying libraries is gene

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 19:01

    So the solution we have for now is as follows:

    test.cpp

    #include 
    #include 
    #include 
    
    struct private_struct
    {
        float f;
    };
    
    float private_function(float f)
    {
        return std::abs(f);
    }
    
    void other_private_function()
    {
        std::vector f(1);
    }
    
    extern "C" void __attribute__ ((visibility ("default"))) public_function2()
    {
        other_private_function();
    }
    
    extern "C" float __attribute__ ((visibility ("default"))) public_function1(float f)
    {
        return private_function(f);
    }
    

    exports.version

    LIBTEST 
    {
    global:
        public*;
    local:
        *;
    };
    

    compiled with

    g++ -shared test.cpp -o libtest.so -fvisibility=hidden -fvisibility-inlines-hidden -s -Wl,--version-script=exports.version
    

    gives

    00000000 A LIBTEST
             w _Jv_RegisterClasses
             U _Unwind_Resume
             U std::__throw_bad_alloc()
             U operator delete(void*)
             U operator new(unsigned int)
             w __cxa_finalize
             w __gmon_start__
             U __gxx_personality_v0
    000005db T public_function1
    00000676 T public_function2
    

    Which is fairly close to what we're looking for. There are a few gotchas though:

    • We have to ensure we don't use the "exported" prefix (in this simple example "public", but obviously something more useful in our case) in the internal code.
    • Many symbol names still end up in the string table, which appears to be down to RTTI, -fno-rtti makes them go away in my simple tests, but is a rather nuclear solution.

    I'm happy to accept any better solutions anyone comes up with!

提交回复
热议问题