如何在Linux上创建存根共享库 [英] How to create stub shared libraries on Linux

查看:115
本文介绍了如何在Linux上创建存根共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我解释一下存根共享库的含义:可用于链接的共享库(带有真实库提供的特定接口),但不包含实际代码(因此没有功能) ).

Let's first explain what I mean with a stub shared library: a shared library that can be used to link against (w/ a certain interface provided by a real library) but don't contain the actual code (so has no functionality).

与头文件一起,它提供了针对库进行开发所需的一切.

Along with the header files it provides everything needed to develop against the library.

存根可以允许在没有可用代码的情况下链接到某个库,但是出于兼容性考虑,链接到某个库的存根可能很有用.例如,请参见在Linux中使用了存根用于标准库.为什么需要存根?

Stubs can allow linking to a certain library without having the code available, but also for compatibility it can be useful to link against a stub of a certain library. See for example In Linux stubs are used for standard libraries. Why are stubs required?

理想情况下,我需要的是一种从符号映射文件生成虚拟库的方法.反过来,此映射文件是从现有的.so库或在相同的构建过程中生成的.

Ideally what I need is a way to generate a dummy library from a symbol map file. This map file is, in turn, generated either from an existing .so library or in the same build process.

有免费的工具吗?还是我需要自己动手?

Are there any tools for this freely available? Or would I need to roll my own?

推荐答案

我想对于简单的C库,您可以在真正的共享库上使用nm -D的输出来制作存根.例如,您可以将其传送到一个小的awk脚本中,该脚本定义相同名称的函数,等等.

I guess that for simple C libraries, you could use the output of nm -D on your real shared library to make the stub. For instance you could pipe it into a small awk script which defines functions of that same name, etc.

另一种方法是将您的 MELT 扩展为最近的 gcc-melt@googlegroups.com

Another approach would be to make your tiny MELT extension to a recent GCC compiler which would generate the stub (e.g. in C++ or C form) when compiling the real library, or which would clear every function body (in a special mode for compiling a stub-only library). This would work for any language compiled by GCC (but requires some understanding of GCC internals, e.g. Trees and Gimples). Ask on gcc-melt@googlegroups.com

但是,我不确定要了解此类存根的实际兴趣.实际上,共享库具有一些特定的编码规则和用法,使用存根时不会对此进行验证.具体来说,如果使用Xlib,则需要首先调用XOpenDisplay,最后调用XCloseDisplay,并且无法使用自动生成的存根等检查此规则.

However, I am not sure to understand the practical interest of such stubs. In practice a shared library has some specific coding rules and usage, and this is not verified when using stubs. To be concrete, if using Xlib, you need to call XOpenDisplay at first and XCloseDisplay at last, and such rule can't be checked with an automatically generated stub, etc...

这篇关于如何在Linux上创建存根共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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