简体   繁体   English

Linux上是否有等效的.def文件用于控制共享库中的导出函数名称?

[英]Is there a .def file equivalent on Linux for controlling exported function names in a shared library?

I am building a shared library on Ubuntu 9.10. 我正在Ubuntu 9.10上构建一个共享库。 I want to export only a subset of my functions from the library. 我想从库中只导出我的一部分函数。 On the Windows platform, this would be done using a module definition ( .def ) file which would contain a list of the external and internal names of the functions exported from the library. 在Windows平台上,这将使用模块定义( .def )文件来完成,该文件将包含从库中导出的函数的外部和内部名称的列表。

I have the following questions: 我有以下问题:

  1. How can I restrict the exported functions of a shared library to those I want (ie a .def file equivalent) 如何将共享库的导出函数限制为我想要的(即等效的.def文件)

  2. Using .def files as an example, you can give a function an external name that is different from its internal name (useful for prevent name collisions and also redecorating mangled names etc) 以.def文件为例,您可以为函数提供一个与其内部名称不同的外部名称(对于防止名称冲突和重新修饰损坏的名称等有用)

  3. On windows I can use the EXPORT command (IIRC) to check the list of exported functions and addresses, what is the equivalent way to do this on Linux? 在Windows上我可以使用EXPORT命令(IIRC)来检查导出的函数和地址列表,在Linux上执行此操作的等效方法是什么?

The most common way to only make certain symbols visible in a shared object on linux is to pass the -fvisibility=hidden to gcc and then decorate the symbols that you want to be visible with __attribute__((visibility("default"))) . 仅在linux上的共享对象中显示某些符号的最常见方法是将-fvisibility=hidden传递给gcc,然后使用__attribute__((visibility("default")))装饰要显示的符号。

If your looking for an export file like solution you might want to look at the linker option --retain-symbols-file=FILENAME which may do what you are looking for. 如果您正在寻找像解决方案这样的导出文件,您可能需要查看链接器选项--retain-symbols-file=FILENAME ,它可以执行您要查找的内容。

I don't know an easy way of exporting a function with a different name from its function name, but it is probably possible with an elf editor. 我不知道从函数名称导出具有不同名称的函数的简单方法,但可能使用精灵编辑器。 Edit: I think you can use a linker script (have a look at the man page for ld) to assign values to symbols in the link step, hence giving an alternative name to a given function. 编辑:我认为您可以使用链接描述文件(查看ld的手册页)在链接步骤中为符号赋值,从而为给定函数提供替代名称。 Note, I haven't ever actually tried this. 请注意,我从未尝试过这个。

To view the visible symbols in a shared object you can use the readelf command. 要查看共享对象中的可见符号,可以使用readelf命令。 readelf -Ds if I remember correctly. readelf -Ds如果我没记错的话。

How can I restrict the exported functions of a shared library to those I want (ie a .def file equivalent) 如何将共享库的导出函数限制为我想要的(即等效的.def文件)

Perhaps you're looking for GNU Export Maps or Symbol Versioning 也许您正在寻找GNU导出地图符号版本控制

g++ -shared spaceship.cpp -o libspaceship.so.1 -Wl,-soname=libspaceship.so.1 -Wl, --version-script=spaceship.expmap

gcc also supports the VC syntax of __declspec(dllexport). gcc还支持__declspec(dllexport)的VC语法。 See this . 看到这个

Another option is to use the strip command with this way: 另一种选择是以这种方式使用strip命令:

strip --keep-symbol=symbol_to_export1 --keep-symbol=symbol_to_export2 ... \
     libtotrip.so -o libout.so

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM