如何隐藏共享库中导出的符号名 [英] How to hide the exported symbols name within a shared library

查看:363
本文介绍了如何隐藏共享库中导出的符号名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关VC,我可以写一个DEF文件,并​​使用NONAME指令留下的DLL导出表只有序号。

For VC, I can write a DEF file and use the 'NONAME' directive to leaving only the ordinal number in dll's export table.

我怎么可以做gcc和ELF格式的共享库是一回事吗?

How could I do the same thing with gcc and ELF format shared library?

或者是有什么相当于ELF共享库就像一个PE格式的DLL序号?如果不是这样,我怎么能隐藏输出符号名称的共享库中?

Or, is there something equivalent in ELF shared library like the ordinal number in a PE format DLL? If not, how could I hide the exported symbol's name within a shared library?

======================================

======================================

更新:一些附加说明:

在Windows中,您可以导出函数只放置一个整数ID(序号)与空的名字。

In Windows, you can export a function by only place a integer ID (the ordinal) with an empty name.

要表现出来,对于一个DLL的导出表正常布局如下:<一href=\"http://home.hiwaay.net/~georgech/WhitePapers/Exporting/HowTo22.gif\">http://home.hiwaay.net/~georgech/WhitePapers/Exporting/HowTo22.gif.

To show it, the normal layout for a dll's export table looks like this: http://home.hiwaay.net/~georgech/WhitePapers/Exporting/HowTo22.gif.

在NONAME一个看起来是这样的:<一href=\"http://home.hiwaay.net/~georgech/WhitePapers/Exporting/HowTo23.gif\">http://home.hiwaay.net/~georgech/WhitePapers/Exporting/HowTo23.gif.

the "NONAME" one looks like this: http://home.hiwaay.net/~georgech/WhitePapers/Exporting/HowTo23.gif.

注意函数名称是N / A,在第二张照片。下面是它的一个完整的交代:hxxp://home.hiwaay.net/~georgech/WhitePapers/Exporting/Exp.htm

Notice the functions name are "N/A" in the second picture. Here is a full explaination of it: hxxp://home.hiwaay.net/~georgech/WhitePapers/Exporting/Exp.htm.

======================================

======================================

更新:很多感谢大家谁使我的意见。最后,我决定保持使用静态库在Linux / POSIX平台。的但提取小的特殊部位(这是使用不适合静态库,例如一些特点:TLS插槽等),以一个正常的共享库的。因为小的正常共享库只是在做一些事情,而这些工作是完全不敏感,所以也没有必要掩盖/隐藏它的API。

UPDATE: A lot of thanks for everyone who make me advice. Finally, I decide to keeping use static library on linux/posix platforms. But extract the small "special part" (which is using some features not suitable for static lib, e.g: TLS Slot, etc.) to a normal shared-library. Because the small normal shared-library only doing few things, and these work are totally insensitive, so there is no need to obscure/hide its APIs.

我认为这是解决我的问题最简单的方法:-D

I think it's the simplest way to solve my problem :-D

推荐答案

在$关于属性p $ pvious答案当你想保持((能见度(隐藏)))是好的code长期的,但如果你只有你想看到,想速战速决几个符号......在你的符号希望的出口使用,添加

The previous answers regarding attribute ((visibility ("hidden"))) is good when you want to maintain the code long term, but if you only have a few symbols that you want visible and want a quick fix... On the symbols that you want to export use, add

__attribute__ ((visibility ("default"))) 

然后就可以通过 = -fvisibility隐藏编译器

有一个彻底的解释在这里:

There is a thorough explanation here:

http://gcc.gnu.org/wiki/Visibility

编辑:另一种方法是建立一个静态库/归档(请用 .A归档AR -cru mylib.a中*的.o )或对象合并成根据本结合两个GCC编译.o目标文件到第三个的.o文件

An alternative would be to build a static library/archive (make .a archive with ar -cru mylib.a *.o) or combine the objects into a single object file according to this combine two GCC compiled .o object files into a third .o file

如果你问为什么相结合的目标文件,而不是仅仅做一个静态库? ......因为连接器​​会比.a文件区别对待.o文件(我不知道为什么,只是它),具体地说,它可以让你一个.o文件链接到一个共享库或者即使二进制的所有的被隐藏(即使您正在使用的)符号这具有减少启动时间(少了一个DSO少了很多符号查找)的好处和二进制文件的大小(一般符号弥补了尺寸〜20%,只有剥取的,大约一半的照顾 - 仅仅是外部可见的部分)

If you are asking "Why combine object files instead of just making a static library?" ... because the linker will treat .o files differently than .a files (I don't know why, just that it does), specifically it will allow you to link a .o file into a shared library or a binary even if all of the symbols are hidden (even the ones you are using) This has the added benefit of reducing startup times (one less DSO and a lot less symbols to look up) and binary size (the symbols typically make up ~20% of the size and stripping only takes care of about half of that - just the externally visible parts)

二进制文件带--strip-所有-R。注意-R的.comment mybinary

图书馆带--strip-不需要-R。注意-R的.comment mylib.so

更多关于静态连接,这里的好处: http://sta.li/faq 但他们不' ŧ讨论这是最主要的原因授权问题的的使用静态库,因为你是想隐藏自己的API,这可能是一个问题。

More on the benefits of static linking here: http://sta.li/faq but they don't discuss licensing issues which are the main reason not to use a static library and since you are wanting to hide your API, that may be an issue

现在,我们知道有一个对象,它是符号干净,就可以通过连接private.o和public.c(别名/出口只有你想社会上引起什么用我们的组合对象建立一个libpublic.so )到共享库。

Now that we know have an object that is "symbol clean", it is possible to use our combined object to build a libpublic.so by linking private.o and public.c (which aliases/exports only what you want public) into a shared library.

此方法非常适用于寻找额外的code那是你的公共API中不需要的为好。如果添加 -fdata截面-ffunction截面到对象构建,当你与 -Wl链接, - GC-部分 - 打印GC-部分,它会删除未使用的部分,并打印辗转什么输出。

This method lends itself well to finding the "extra code" that is unneeded in your public API as well. If you add -fdata-sections -ffunction-sections to your object builds, when you link with -Wl,--gc-sections,--print-gc-sections , it will eliminate unused sections and print an output of what was removed.

编辑2 - 或者你可以隐藏整个API和别名仅​​要导出的功能

Edit 2 - or you could hide the whole API and alias only the functions you want to export

别名(目标)

alias属性应为另一个符号的别名,必须指定的声明。例如,

The alias attribute causes the declaration to be emitted as an alias for another symbol, which must be specified. For instance,

          void __f () { /* Do something. */; }
          void f () __attribute__ ((weak, alias ("__f")));

定义 F'成为 __ F A弱别名。在C ++中,必须使用目标的重整名称。它是如果`__f'不在同一转换单元中定义一个错误。

defines f' to be a weak alias for__f'. In C++, the mangled name for the target must be used. It is an error if `__f' is not defined in the same translation unit.

并不是所有的目标机器支持这个属性。

这篇关于如何隐藏共享库中导出的符号名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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