使用相同的符号加载两个共享库时,是否存在符号冲突 [英] Is there symbol conflict when loading two shared libraries with a same symbol

查看:747
本文介绍了使用相同的符号加载两个共享库时,是否存在符号冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程式(应用程式)取决于两个共用资料库: liba.so libb.so

liba libb 具有 void Hello()的相同功能,但具有不同的实施方式。
在运行时加载两个共享库并尝试访问两个版本的Hello()。

我通过poco C + +共享库加载liba.so和libb.so,但最终它调用 dlopen()加载共享库。这里是代码:

  #includePoco / SharedLibrary.h
使用Poco :: SharedLibrary;
typedef void(* HelloFunc)(); //函数指针类型


int main(int argc,char ** argv)
{
std :: string path(liba);
path.append(SharedLibrary :: suffix()); //添加.so
SharedLibrary库(路径);
HelloFunc func =(HelloFunc)library.getSymbol(hello);
func();

std :: string path2(libb);
path2.append(SharedLibrary :: suffix()); // added.so
SharedLibrary library2(path2);
HelloFunc func2 =(HelloFunc)library2.getSymbol(Hello);
func2();

library.unload();
library2.unload();

return 0;我的问题是,当应用加载liba.so和libb.so由dlopen时(),两个Hello()实现会有符号冲突吗?

事实上,代码运行良好,但我不知道是否有任何潜在的风险像这样。

解决方案


我的问题是,当应用加载liba.so和libb.so dlopen(),两个Hello()实现会有符号冲突吗?


这些是返回的地址,并且这两个动态加载的库将存在一个单独的地址空间。



即使dlsym函数不能被混淆,因为你传递由dlopen函数返回的句柄



(分别在同一个库中不会出现重载的问题)


An application (app) depends on two shared librarys: liba.so and libb.so.
liba and libb have the same function of void Hello() but with different implementations. Load the two shared libraries at runtime and try to access the two versions of Hello().
I load liba.so and libb.so by poco C++ shared-library, but eventually it calls dlopen() to load shared libraries. Here's the code:

#include "Poco/SharedLibrary.h"
using Poco::SharedLibrary;
typedef void (*HelloFunc)(); // function pointer type


int main(int argc, char** argv)
{
    std::string path("liba");
    path.append(SharedLibrary::suffix()); // adds ".so"
    SharedLibrary library(path);
    HelloFunc func = (HelloFunc) library.getSymbol("hello");
    func();

    std::string path2("libb");
    path2.append(SharedLibrary::suffix()); // adds ".so"
    SharedLibrary library2(path2);
    HelloFunc func2 = (HelloFunc) library2.getSymbol("hello");
    func2();

    library.unload();
    library2.unload();

    return 0;
}

My question is, when app loads liba.so and libb.so by dlopen(), will there be any symbol conflict for the two Hello() implementations?
In fact, the code goes well, but I wonder whether there's any potential risk to load libraries like that.

解决方案

My question is, when app loads liba.so and libb.so by dlopen(), will there be any symbol conflict for the two Hello() implementations?

There is none. These are addresses returned, and both dynamically loaded libraries will live in a separate address space.

Even the dlsym function cannot be confused since you pass the handle returned by the dlopen function, so it would not by any mean become ambiguous.

(This would not even be an issue with overloading within the same library, respectively)

这篇关于使用相同的符号加载两个共享库时,是否存在符号冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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