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

Binary 如何在MacOs中真正剥离二进制文件,binary,executable,strip,darwin,macos,Binary,Executable,Strip,Darwin,Macos,MacOs 10.6,如果我有一个文件“多余的.c”,其中包含: class secret_thing { public: secret_thing() {} void revealing_method_name() {} }; main() { secret_thing obj; obj.revealing_method_name(); } 现在我做到了: $ g++ unwanted.c -o unwanted $ strip unwanted $ nm unwanted

MacOs 10.6,如果我有一个文件“多余的.c”,其中包含:

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

main()
{
    secret_thing obj;
    obj.revealing_method_name();
}
现在我做到了:

$ g++ unwanted.c -o unwanted
$ strip unwanted
$ nm unwanted | grep secret
0000000100000eb8 T __ZN12secret_thing21revealing_method_nameEv 
0000000100000eae T __ZN12secret_thingC1Ev

如果我拆开了秘密类的接口和实现,就像大多数人在编写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();
}

这似乎按预期工作…:

$ strip unwanted
$ nm unwanted | grep secret | cut -f 3 -d ' ' > /tmp/remove
$ strip -R /tmp/remove unwanted

给定您发布的示例代码,添加“-O”优化标志会导致编译后这些符号不会显示在
nm
中。

听起来您真正想要的不是剥离可执行文件,而是混淆其符号表。我不确定,但也许类似的东西会有帮助。至少,“C++符号表混淆器”可能是一个更好的Google搜索字符串。

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

$ g++ -Xlinker -unexported_symbol -Xlinker "*" -o executable file.cpp
$ strip executable
根据最新的示例文件,这将导致:

$ 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

我发错代码了。使用正确的示例代码,如问题所述,使用类定义中方法的实现,strip说:strip:由间接符号表条目引用的符号,不能在:/Users/michael.toy/dispured _zn12; secret 2; thing21显示方法_nameEv __zn12; secret 2; thingc1ev但是答案非常有用,因为它给了我更多的谷歌素材,该错误字符串确实揭示了一些更有趣的调查领域。我们发布了一个更接近实际问题的示例,该示例仍然显示了问题。驱动我达到这一点的原始应用程序是经过优化构建的。谢谢你的回复。我真的很想删除可执行文件。然而,这可能会出现在笔记本的标题“我希望我永远不需要实施备份解决方案”下。它就在“编写我自己的后处理器,它链接需要这些符号的未链接的内容,然后删除这些符号”