Date: Tue, 17 Nov 1998 10:33:44 -0500 From: sbabkin@dcn.att.com To: nate@mt.sri.com, hackers@FreeBSD.ORG Subject: RE: Wrapping a function Message-ID: <C50B6FBA632FD111AF0F0000C0AD71EE018C1D9D@dcn71.dcn.att.com>
next in thread | raw e-mail | index | archive | help
> From: Nate Williams [SMTP:nate@mt.sri.com] > > Does anyone have an easy way of 'wrapping' an already existing library > function so that any programs linked against your .o will call your > function, but so your function can call the 'real' library function? > > Example: > > my_malloc.c: > > void *malloc(size_t size) > { > void *ret; > > printf("Calling malloc\n"); > ret = REALMALLOC(size); > printf("Leaving malloc\n"); > return ret; > } > > Ignoring all of the functions where there is loss of errno and such, are > they any good ideas? Note, the use of the dl* functions is explicitly > not allowed since those happen to be the functions I want to wrap in > this case. > I see no way to do it in C, but I have done such things multiple times with the object files. The key is to rename the entries in the symbol tables of object files. Here are two ways: 1. Rename the calls from another object file. This is useful is you want to substitute your function for only one object file in program, and leave others as is (a good example would be a binary driver linked to the kernel). Take this object file and using a binary editor (xvi, for example) change all the occurrences of "malloc" in the symbol table to something like "xxxloc". Then name your wrapper function "xxxloc" and that's it. 2. Rename the calls from all user-level object file. Copy the library with "malloc", extract the object file with "malloc" from it, change "malloc" to "xxxloc", insert back into the library and link this changed library explicitly instead of the standard one. Your wrapper function should then be named "malloc" and call "xxxloc". -Sergey To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?C50B6FBA632FD111AF0F0000C0AD71EE018C1D9D>