0

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.

Michael Chinen
  • 17,737
  • 5
  • 33
  • 45

1 Answers1

1

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 use LD_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.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • cool. As long as I have the signatures matching everything just works? – Michael Chinen Apr 18 '11 at 09:51
  • Well to be clear these are dangerous waters, maybe not "everything" will "just work," but yeah, so long as the signatures match there's hope it'll work as you expect, just be careful because obviously some libraries may not appreciate having their functions replaced (if yours has different side effects or whatever). As for how the linker finds the functions, it's worth noting that it's by (mangled) name only, and does not depend on the type signature. – John Zwinck Apr 18 '11 at 13:01