用于修改 ELF 二进制文件动态部分的工具
是否有工具可以修改 ELF 二进制文件动态部分中的共享库条目?我想显式修改二进制文件中的共享库依赖项(即用自定义路径替换现有库的路径)
Is there a tool for modifying the shared library entries in the dynamic section of an ELF binary? I would like to explicitly modify the shared library dependencies in my binary (i.e. replace path to existing library with a custom path)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果这是您自己的库,那么您可能会像这样链接它:
而不是正确的:
第一种方法告诉Linux链接器应用程序恰好需要该库,只应该使用该库并且不应该覆盖可能的。第二种方法告诉应用程序需要将安装在系统上某个位置的库,无论是在默认库路径中还是在 $LD_LIBRARY_PATH 指向的路径中(将在运行时查找)。 -L 仅在链接时使用。
否则,不要修补 ELF,而是首先检查是否可以使用符号链接替换库。这是常见的技巧:事后很难修改可执行文件,但更改符号链接点的位置却很容易。
If this is your own library, then you probably linking it like that:
instead of the proper:
The first approach tells Linux linker that application needs precisely that library, only that library and no override should be possible. Second approach tells that application needs the library which would be installed somewhere on the system, either in the default library path or one pointed by the $LD_LIBRARY_PATH (would be looked up during run-time). -L is used only during link-time.
Otherwise, instead of patching the ELF, first check if you can substitute the library using a symlink. This is the usual trick: it is hard to modify executable afterward, but it is very easy to change where to the symlink points.
patchelf
就是你想要的看到效果
安装工具很简单:
patchelf
is what you wantTo see the effect
Install the tools is easy:
您可能需要检查
LD_LIBRARY_PATH
环境变量。You may want to check the
LD_LIBRARY_PATH
environment variable.如果您通过 readelf 查看 Linux 中的 .dynsym 部分,您将看到类似以下内容:
它仅包含库的符号名称。但是,如果您包含动态加载程序信息,您会得到:
因此,如上所述,绝对最简单的事情(假设您这样做是为了调试,而不是永远)只是创建一个新会话,导出您的自定义路径在现有的 LD_LIBRARY_PATH 前面,然后从那里开始。
If you look at the .dynsym section in Linux via
readelf
, you'll just see something like:which just contains a symbolic name of the library. However, if you include the dynamic loader info, you get:
So as mentioned, the absolute easiest thing to do (assuming you're doing this for debugging, and not forever) would just be to create a new session, export your custom path in front of the existing
LD_LIBRARY_PATH
, and go from there.