符号隐藏在使用 Xcode 构建的静态库中 [英] Symbol hiding in static libraries built with Xcode

查看:28
本文介绍了符号隐藏在使用 Xcode 构建的静态库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚我是否可以构建一个静态库来隐藏其所有内部对象和函数等,除了我想要导出的接口.我正在尝试使用 Xcode (gcc 4.2).

I'm trying to figure out whether I can build a static library that hides all of its internal objects and functions, etc, except for the interfaces I want to export. I'm experimenting with Xcode (gcc 4.2).

我根据 属性/documentation/DeveloperTools/Conceptual/CppRuntimeEnv/Articles/SymbolVisibility.html" rel="noreferrer">本文档.我还将小助手 C 函数定义为文件本地(静态)等.

I've used the __attribute__((visibility("hidden"))) attribute on some C++ classes per this documentation. I've also defined little helper C functions as being file-local (static), etc.

然而,当我在生成的 .a 库文件上运行 strings 时,即使在 Release 配置中编译时,我仍然看到我表面上隐藏的类的名称,以及它们的方法名称,甚至文件本地函数的名称也散落在那里.

However, when I run strings on the resulting .a library file, even when compiled in Release configuration, I still see the names of my ostensibly-hidden classes, with their method names, and even the names of file-local functions strewn around in there as well.

我已将 -fvisibility=hidden 甚至 -fno-rtti 添加到 gcc 标志中.虽然这减少了一些字符串,但类名、方法名和静态函数名仍然以普通或错位但可读的形式存在.

I've added the -fvisibility=hidden and even -fno-rtti to the gcc flags. While this reduces some of the strings, the class names, method names, and static functions names are all still in there in plain or mangled-but-readable form.

是否有一种可靠的方法可以让编译器在不将所有内部内容的字符串名称发送到二进制文件中的情况下构建这些内容?没有必要让任何外部客户链接进来.

Is there a reliable way to get the compiler to build this stuff without having the string names of all the internal stuff emitted into the binary? It shouldn't be necessary to have for any external clients linking in.

(澄清:我问的是混淆内部命名,而不是字面导出绑定需求.我感到不安的是,所有内部工作都可以通过 strings 命令可见,无论这些符号是否正式导出.)

(To clarify: I'm asking about obfuscation of internal naming, versus literal export binding needs. I'm disconcerted that all the internal workings are visible via the strings command, regardless of whether these symbols are formally exported or not.)

谢谢.

推荐答案

隐藏内部名称需要一些简单的 Xcode 构建设置,一般不需要修改源或更改构建产品的类型.

Hiding internal names requires a few simple Xcode build settings, and it is not generally necessary to modify source or change the type of the built product.

  1. 通过执行单对象预链接消除模块之间所需的任何内部符号.将名为Perform Single-Object Prelink"的 Xcode 构建设置设置为 Yes (GENERATE_MASTER_OBJECT_FILE=YES).这会导致 ld 使用-r"标志运行.
  2. 确保将条带样式"设置为非全局符号"(STRIP_STYLE=非全局),这会将-x"传递给 ld.
  3. 如果启用了后处理(这不是默认设置),则仅在静态库上实际执行剥离.将 Xcode 构建设置部署后处理"设置为是.(DEPLOYMENT_POSTPROCESSING=YES).还要确保使用单独的条带"设置为是(并非总是默认值)(SEPARATE_STRIP=YES).
  4. 如果除了局部符号之外,如果您需要删除一些全局符号,您可以在 Xcode 构建设置附加条标志"下为 ​​strip 命令提供其他选项.例如.我通常使用带-R somefile"选项来提供一个文件,其中包含我想从全局符号表中删除的附加符号列表.

这篇关于符号隐藏在使用 Xcode 构建的静态库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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