如何在MacOs中真正剥离二进制文件

mto*_*toy 8 binary macos executable darwin strip

MacO 10.6,如​​果我有一个文件"unwanted.c",其中包含:

class secret_thing {
public:
secret_thing() {}
void revealing_method_name() {}
};

main()
{
    secret_thing obj;
    obj.revealing_method_name();
}
Run Code Online (Sandbox Code Playgroud)

现在我做:

$ g++ unwanted.c -o unwanted
$ strip unwanted
$ nm unwanted | grep secret
0000000100000eb8 T __ZN12secret_thing21revealing_method_nameEv 
0000000100000eae T __ZN12secret_thingC1Ev
Run Code Online (Sandbox Code Playgroud)

如果我分离出秘密类的接口和实现,就像大多数人在编写C++代码时那样,那么剥离的可执行文件中就没有不需要的符号.可悲的是,我交给了现有数千行代码的代码库,这不是我的选择之一.

我试过-fno-rtti,作为一个疯狂的猜测,并没有解决任何问题.我已经向谷歌众神祈祷并发现许多对脱衣舞俱乐部的引用,但没有任何有用的链接.我已经在mac上浏览了strip,g ++和ld的手册页,并且没有明显的尝试,尽管"私人外部"一词很有趣,我无法弄清楚该怎么做.

[更新]可悲的是,我试图做一个小例子后出现问题.这是一个更复杂的例子,它更接近真正的问题,如果它是经过优化的,它仍然有不需要的符号.

我为坏的例子道歉.事实证明,很难找到最小的实际问题.非常感谢答案,但每个答案都让我接近解决方案.

class base {
public:
    virtual int revealing_method_name() = 0;
    virtual ~base() {};
};

class secret_thing : public base {
public:
    int revealing_method_name() { return 0; };
};

class other_thing : public base {
public:
    int revealing_method_name() { return 1; };
};

int main(int argc, char**)
{
    base *object = 0;
    if( argc > 1 ) object = new secret_thing;
    else object = new other_thing;

    return object->revealing_method_name();
}
Run Code Online (Sandbox Code Playgroud)

小智 9

使用以下编译行我成功地从可执行文件中删除符号:

$ g++ -Xlinker -unexported_symbol -Xlinker "*" -o executable file.cpp
$ strip executable
Run Code Online (Sandbox Code Playgroud)

给定最新的示例文件,结果如下:

$ nm executable
                 U __ZTVN10__cxxabiv117__class_type_infoE
                 U __ZTVN10__cxxabiv120__si_class_type_infoE
                 U __ZdlPv
                 U __Znwm
                 U ___cxa_pure_virtual
                 U ___gxx_personality_v0
0000000100000000 A __mh_execute_header
                 U _exit
                 U dyld_stub_binder
Run Code Online (Sandbox Code Playgroud)