如何在 MacOs 中真正剥离二进制文件 [英] How to REALLY strip a binary in MacOs

查看:27
本文介绍了如何在 MacOs 中真正剥离二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

MacOs 10.6, if I have a file "unwanted.c" which contains:

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++ 代码时那样拆分秘密类的接口和实现,那么剥离的可执行文件中就没有不需要的符号.遗憾的是,我收到了一个包含数千行代码的现有代码库,这不是我的选择之一.

If I split out the interface and implementation of the secret class, as most people do when writing C++ code, then there are no unwanted symbols in the stripped executable. Sadly, I am handed an existing code base of many thousand lines of code and this isn't one of my choices.

我已经尝试过 -fno-rtti,作为一个疯狂的猜测,但这并没有解决任何问题.我向谷歌众神祈祷,发现很多脱衣舞俱乐部的参考资料,但没有有用的链接.我在 mac 上浏览了 strip、g++ 和 ld 的手册页,没有明显的尝试,虽然私人外部人员"这个短语很有趣,但我不知道该怎么做.

I have tried -fno-rtti, as a wild guess, and that didn't fix anything. I have prayed to the Google gods and found many references to strip clubs, but no helpful links. I have skimmed the man pages for strip, g++, and ld on the mac, and there were no obvious things to try, though the phrase "private externs" was intriguing, I couldn't figure out what to do about that.

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

[ update ] Sadly, there turns out to be a problem with my attempt to make a small example. Here is a more complicated example, which is closer to what the real problem is, which still has unwanted symbols if it is built optimized.

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

I apologize for the bad examples. It turns out to be hard to find the smallest actual problem. Many thanks for the answers, though, each answer pushes me close to a solution.

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();
}

推荐答案

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

Using the following compile line I successfully strip the symbols from the executable:

$ g++ -Xlinker -unexported_symbol -Xlinker "*" -o executable file.cpp
$ strip executable

鉴于最新的示例文件,结果如下:

Given the latest sample file, this results in:

$ 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

这篇关于如何在 MacOs 中真正剥离二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆