如何使共享库延迟加载在Linux上 [英] How to make a shared library delay loaded on Linux

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

问题描述

我一直在寻找一种方法来创建一个共享库(我们命名库 libbar.so 延迟加载在Linux上,应该希望实现在只有链接器的帮助下,不会修改用C ++编写的源代码上的任何内容;我的意思是我不想在父库的源代码中调用 dlopen() dlsym()(让我们将它命名为 libfoo.so )调用 libbar.so 的函数,因为它们使源代码混乱,维护过程很困难。 (简而言之,即使在Linux上,我也希望类似于Visual Studio的 / DELAYLOAD 选项)

I've been searching a way to make a shared library (let's name the library "libbar.so") delay loaded on Linux and it should hopefully be realized with a help from only a linker, not modifying anything on the source code written in C++; I mean I don't want to invoke dlopen() nor dlsym() in the source code of the parent library (let's name it "libfoo.so") to invoke a function of libbar.so because they make the source code messy and the maintenance process difficult. (In short, I'm expecting to go on the similar way to Visual Studio's /DELAYLOAD option even on Linux)

到目前为止,我发现了一些与我的问题相关的不确定的信息,所以很高兴得到你的答案以下的问题,以使信息清楚。

Anyway, I've found some uncertain information pieces related to my question on the internet so far, so it would be very nice to have the answers from you all for the following questions to make the information clear.


  1. GNU ld 是否支持Linux上的延迟加载机制?

  2. 如果不支持, > Clang ?

  3. dlopen()系列是在Linux上加载共享库延迟的唯一方法吗?

  1. Does GNU ld support any delay loading mechanism on Linux?
  2. If it doesn't, how about Clang?
  3. Is the dlopen() family the only way to make a shared library delay loaded on Linux?

我测试了将 -zlazy 标志传递给GCC(g ++)并使用该库的路径,它似乎接受该标志,该行为没有看起来使 libbar.so 延迟加载(不具有 libbar.so ,我希望在第一次调用 libbar.so时有一个异常,但在进入 libfoo.so 之前实际引发的异常)。另一方面,Clang(clang ++)留下了一条警告,它忽略了选项标志。

I tested to pass -zlazy flag to GCC (g++) with a path to the library, it seemed to accept the flag but the behavior did not look making libbar.so delay loaded (Not having libbar.so, I was expecting to have an exception at the first call of libbar.so, but the exception actually raised before entering to libfoo.so). On the other hand, Clang (clang++) left a warning that it ignored the option flag.

最好的问候,

推荐答案

延迟加载不是运行时功能。 MSVC ++实现它没有Windows的帮助。和像 dlopen 是Linux上唯一的方法, GetProcAddress 是Windows上唯一的运行时方法。

Delay loading is NOT a runtime feature. MSVC++ implemented it without help from Windows. And like dlopen is the only way on Linux, GetProcAddress is the only runtime method on Windows.

那么,什么是延迟加载呢?它很简单:任何调用DLL都必须通过一个指针(因为你不知道它将加载在哪里)。这总是由编译器和链接器处理。但是随着延迟加载,MSVC ++最初将此指针设置为为您调用 LoadLibrary GetProcAddress 的存根。

So, what is delay loading then? It's very simple: Any call to a DLL has to go through a pointer (since you don't know where it will load). This has always ben handled by the compiler and linker for you. But with delay loading, MSVC++ sets this pointer initially to a stub that calls LoadLibrary and GetProcAddress for you.

Clang可以在没有 ld 帮助的情况下执行相同操作。在运行时,它只是一个普通的 dlopen 调用,Linux不能确定Clang插入它。

Clang can do the same without help from ld. At runtime, it's just an ordinary dlopen call, and Linux cannot determine that Clang inserted it.

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

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