在链接时提供不同的库/函数
如果我想克隆一个库并仅更改一个函数(例如 memcpy 或 memmove),并拥有一个已构建的可执行链接以用于调试/探索目的,那么执行此操作的正确方法是什么?
我猜我需要通过修改重新编译整个库,但是还有其他方法可以做到这一点吗?
我知道有诸如 malloc 挂钩之类的东西,但这似乎是 malloc 的特例。 我很好奇 valgrind 和 gdb 如何在另一个程序中执行此操作的具体细节(如果有人有这方面的资源)。
我对 Mac 和 Linux 解决方案感兴趣。在 Linux 上,我之前使用过 LD_LIBRARY_PATH - 除了库名称相同之外,这就是我需要做的所有事情吗?我该如何在 mac 上执行此操作?
对于那些好奇我为什么要这样做的人来说,目的是为了实验音乐。我这样做是为了对内存操作进行声音化,因此 memcpy/memmove 将正常工作,但访问的数据也将发送到声卡。我知道还有其他方法可以做到这一点(我已经做了一些其他方法),但目前我对专注于 memcpy/memmove 感兴趣,所以如果您能将您的答案限制在这个焦点上,我将不胜感激。
If I want to clone a library and change just one function, say memcpy or memmove, and have an already built executable link to it for debugging/exploration purposes, what is the proper way to do this?
I am guessing I need to recompile the entire library with my modifications, but is there another way to do this?
I understand that there are things like malloc hooks but this seems to be a special case for malloc.
I am curious about the specifics for how valgrind and gdb do this from within another program, if someone has a resource on that.
I am interested in mac and linux solutions. On linux I've used LD_LIBRARY_PATH before - is this all that I need to do besides have the library names the same? How would I do this on mac?
For those curious as to why I want to do this, the purpose is for experimental music. I am doing this to sonify memory operations, so memcpy/memmove will work as normal but the data accessed will also be sent to the sound card. I know there are other ways of doing this (I have done a few other methods already,) but currently I am interested in focusing on memcpy/memmove, so I will appreciate it if you can restrict your answers to this focus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 LD_LIBRARY_PATH 使程序加载与通常的共享对象库不同的共享对象库。但是,如果您只想替换一个(或几个)函数而不是整个库,则可以使用 LD_PRELOAD 使链接器 (ld.so) 尽早加载特定的共享对象,并且您的程序将使用其中的符号(函数),而不是在通常的地方寻找它们。
You can use
LD_LIBRARY_PATH
to cause a program to load a shared object library different from the usual one. But if you want to replace just one function (or a few) rather than a whole library, you can useLD_PRELOAD
to cause the linker (ld.so) to load a particular shared object early on, and your program will use the symbols (functions) in there rather than looking for them in the usual places.