如何从 bash 更改二进制文件的库包含路径?

发布于 2024-10-15 13:26:12 字数 936 浏览 8 评论 0原文

我在 Kubuntu 上正确安装了一个软件。

现在,我正在修补和测试它的一些库。

如何从 bash 启动软件,以便加载我的修补库而不是官方库?

例如:
官方库位于 /usr/lib/
我的补丁库(在测试开发期间使用)位于 /home/user/dev/lib/

我尝试过:

$ set LD_LIBRARY_PATH=/home/user/dev/lib/  
$ binary_app &

但无济于事。

我更喜欢可以从 bash 设置的解决方案,但如果不可能,我也可以修改这个 C++ 软件的 cmake 文件。

目的是让我可以使用普通库或我修补的库轻松启动应用程序以查看差异。

编辑:它是一个 KDE .so 文件

我正在测试的库是 KDE4 库。官方库位于 /usr/lib/kde4/ 。在该目录中,没有任何库以 lib 前缀开头。

我是否这样做:

/lib/ld-linux-x86-64.so.2 --list  --library-path PATH EXEC  

或者

ldd EXEC  

图书馆根本没有列出。

另一方面,如果将原始库移离 /usr/lib/kde4/,应用程序会启动,但相应的功能会丢失。

KDE4 库是否以特定方式加载?也许要设置的变量不同...

编辑2

所有答案都很好并且有用...不幸的是,事实证明问题似乎与lib路径设置无关。我正在处理插件架构,.so 加载路径似乎是硬编码在应用程序中的某处。我需要花更多的时间在源代码中来理解正在发生的事情......谢谢大家,+1。

I have a software properly installed on Kubuntu.

Now, I am patching and testing some of its libraries.

How can I start the software from bash so that it loads my patched libraries instead of the official libs?

e.g.:
the official libs are locate in /usr/lib/
my patch libraries (used during test development) are in /home/user/dev/lib/

I tried:

$ set LD_LIBRARY_PATH=/home/user/dev/lib/  
$ binary_app &

but to no avail.

I'd prefer a solution that can be set from the bash, but if it's not possible, I could also modify the cmake file of this C++ software.

The aim is to allow me to easily start the application either with the vanilla libs, or with my patched libs to see the differences.

Edit: it's a KDE .so file

The library I am testing is a KDE4 library. The official lib is in /usr/lib/kde4/ . In that directory, none of the library start with the lib prefix.

Whether I do:

/lib/ld-linux-x86-64.so.2 --list  --library-path PATH EXEC  

or

ldd EXEC  

The library is not listed at all.

On the other hand, if if move the original library away from /usr/lib/kde4/, the application starts but the corresponding functionality is missing.

Are KDE4 libraries loaded in a specific way? Maybe the variable to set is different...

Edit 2

All the answers are good and useful... unfortunately, it turned out that the problem does not appear to be related to the lib path setting. I'm dealing with a plugin architecture and the .so loading path appears to be hard-coded somewhere in the application. I need to spend more time within the source code to understand what's happening... Thanks and +1 to all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

野却迷人 2024-10-22 13:26:12

来自“男人狂欢”:

当除内置或 shell 函数之外的简单命令要执行时
被执行,它被调用在
独立的执行环境
由以下内容组成。除非
另有说明,这些值为
从 shell 继承。

[....]

· 标记为导出的 shell 变量和函数,以及
与导出的变量
命令,在环境中传递

如果要让您执行的程序看到变量,您需要“导出”变量。

不过,您也可以尝试以下操作:

/lib/ld-linux.so.2 --library-path 路径可执行文件

请参阅 http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

From 'man bash':

When a simple command other than a builtin or shell function is to
be executed, it is invoked in a
separate execution environment that
consists of the following. Unless
otherwise noted, the values are
inherited from the shell.

[....]

· shell variables and functions marked for export, along
with variables exported for the
command, passed in the environment

You need to 'export' a variable if it is to be seen by programs you execute.

However, you can also try the following:

/lib/ld-linux.so.2 --library-path PATH EXECUTABLE

See http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

你在我安 2024-10-22 13:26:12

尝试使用 export LD_LIBRARY_PATH=... 而不是 set。

Try export LD_LIBRARY_PATH=... instead of set.

⒈起吃苦の倖褔 2024-10-22 13:26:12

我已经将其放在评论中,但经过思考,我认为执行此操作的最佳方法(仅使用不同的库进行测试/调试)是使用 LD_PRELOAD,请参阅 LD_PRELOAD 技巧是什么?

从手册页:

LD_PRELOAD

以空格分隔的附加、用户指定的 ELF 共享库列表,要在所有其他库之前加载。这可用于有选择地覆盖其他共享库中的函数。对于 set-user-ID/set-group-ID ELF 二进制文件,仅加载标准搜索目录中同时也是 set-user-ID 的库。

更新:

更新问题后,应用程序似乎正在使用 dlopen 来使用绝对路径打开库。我认为你对此无能为力。请参阅 man dlopen

更新 2:

也许您可以做一些事情:您可以 LD_PRELOAD 您自己的 dlopen 函数,该函数会修改您的路径自己的图书馆...

I already put this in a comment but after thinking about it I think the best way to do this (using a different library just for testing/debugging) is using LD_PRELOAD, see What is the LD_PRELOAD trick?

From the man page:

LD_PRELOAD

A whitespace-separated list of additional, user-specified, ELF shared libraries to be loaded before all others. This can be used to selectively override functions in other shared libraries. For set-user-ID/set-group-ID ELF binaries, only libraries in the standard search directories that are also set-user-ID will be loaded.

Update:

After the updated question it seems the application is using dlopen to open the library using a absolute path. I don't think you can do anything about it. See man dlopen

Update2:

Maybe there is something you can do: you might be able to LD_PRELOAD your own dlopen function which modifies the path to your own library...

夜无邪 2024-10-22 13:26:12

你的应用程序 setuid 或 setgid 不是偶然的吗?在这种情况下,LD_LIBRARY_PATH 将被忽略。

Isn't you app setuid or setgid by chance? In this case LD_LIBRARY_PATH will be ignored.

苏大泽ㄣ 2024-10-22 13:26:12

将所有内容放在一行上:

LD_LIBRARY_PATH=foo binary_app&

Put everything on one line:

LD_LIBRARY_PATH=foo binary_app&
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文