[llvm-dev] Wrapper functions for standard library functions

James Henderson via llvm-dev llvm-dev at lists.llvm.org
Tue Jan 2 03:25:51 PST 2018


Hi,

I don't know if there's any way to do this directly in the compiler, but
most linkers, including LLD, support a "--wrap" option. To wrap malloc(),
you can use "--wrap=malloc" (or -Wl,--wrap=malloc if running through the
compiler driver). This redirects all calls to malloc to a function called
__wrap_malloc. If you want to subsequently call the real malloc function,
you then call __real_malloc, so your example code above becomes:

__wrap_malloc(int size) {
    __real_malloc(size+4);  //call the real malloc here
}

Hope that helps.

James

On 27 December 2017 at 00:40, Barbora Murinov√° via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> Hi,
>
> I would like to wrap some of the library functions such as malloc() into
> for example:
>
> malloc_wrapper(int size) {
>     malloc(size+4);  //call the real malloc here
> }
>
> and have all uses of malloc replaced with malloc_wrapper. Is there a way
> to do that?
>
> --
> ----------------
> Barbora Murinov√°
> The University of Edinburgh
> SK: +421905718390 <+421%20905%20718%20390>
> UK: +447477833795 <+44%207477%20833795>
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180102/d322e485/attachment.html>


More information about the llvm-dev mailing list