如何用额外的目标文件重新链接现有的共享库 [英] How to relink existing shared library with extra object file

查看:86
本文介绍了如何用额外的目标文件重新链接现有的共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经剥离了现有的Linux共享对象文件(共享库).我想产生一个新版本的库,其中包含一些其他功能.我曾希望像下面这样的东西会起作用,但不会:

I have existing Linux shared object file (shared library) which has been stripped. I want to produce a new version of the library with some additional functions included. I had hoped that something like the following would work, but does not:

ld -o newlib.so newfuncs.o --whole-archive existinglib.so

我没有现有库的源代码.我可以理解,但是要获得一个完整的构建环境并放置必要的依赖项,对于看似简单的问题将需要付出很多努力.

I do not have the source to the existing library. I could get it but getting a full build environment with the necessary dependencies in place would be a lot of effort for what seems like a simple problem.

推荐答案

您可能希望通过使用预加载来加载对象来尝试从稍微不同的角度进行尝试.

You might like to try coming at this from a slightly different angle by loading your object using preloading.

设置LD_PRELOAD指向您的新对象

Set LD_PRELOAD to point to your new object

export LD_PRELOAD=/my/newfuncs/dir/newfuncs.o

,并通过LD_LIBRARY_PATH以相同的方式指定现有库.

and specify the existing library in the same way via your LD_LIBRARY_PATH.

这将指示运行时链接程序在查找位于LD_LIBRARY_PATH中的对象之前先在对象中搜索所需的符号.

This will then instruct the run time linker to search for needed symbols in your object before looking in objects located in your LD_LIBRARY_PATH.

顺便说一句,您可以在对象中放置调用,然后调用如果未指定LD_PRELOAD对象则将调用的函数.这就是为什么有时将其称为插入的原因.

BTW You can put calls in your object to then call the function that would've been called if you hadn't specified an LD_PRELOAD object or objects. This is why this is sometimes called interposing.

这是多少个内存分配分析工具.它们插入malloc()和free()的版本,记录对alloc()和free()的调用,然后再调用实际的系统alloc和free函数以执行内存管理.

This is how many memory allocation analysis tools work. They interpose versions of malloc() and free() that records the calls to alloc() and free() before then calling the actual system alloc and free functions to perform the memory management.

internet上有很多关于使用LD_PRELOAD的教程.最好的原始版本之一仍然是"构建图书馆插入器以获取乐趣和利润".尽管写于9年前并且是为Solaris编写的,但它仍然是一个很好的资源.

There's plenty of tutorials on the interwebs on using LD_PRELOAD. One of the original and best is still "Building library interposers for fun and profit". Though written nine years ago and written for Solaris it is still an excellent resource.

祝你好运.

这篇关于如何用额外的目标文件重新链接现有的共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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