Re: How to link file with wrapper function

David Z Maze <>
Tue, 04 Nov 2008 10:11:41 -0500

          From comp.compilers

Related articles
How to link file with wrapper function (Vasvi Kakkad) (2008-11-04)
Re: How to link file with wrapper function (David Z Maze) (2008-11-04)
Re: How to link file with wrapper function (2008-11-04)
Re: How to link file with wrapper function (Bill Cox) (2008-11-08)
| List of all articles for this month |

From: David Z Maze <>
Newsgroups: comp.compilers
Date: Tue, 04 Nov 2008 10:11:41 -0500
Organization: Massachusetts Institute of Technology
References: 08-11-018
Keywords: GCC, linker, comment
Posted-Date: 04 Nov 2008 12:53:30 EST

"Vasvi Kakkad" <> writes:

> I have written wrapper functions for some predefined library functions in C.
> Can anybody help me how do I build the sourcecode of gcc with new
> functions and how to link my wrapper lib prior to standard lib? Do I
> to change makefile settings??
> Ex:
> 1) wrapper function file:
> wrapper.c
> void *malloc(size_t size) {
> -----
> return (malloc(size));
> -----
> }

This isn't strictly a compiler question, and it depends some on your

GNU libc on modern ELF-based Linux creates symbols named __libc_malloc
and the like for most functions, then exports "malloc" as a weak alias
for __libc_malloc. I've certainly written:

int close(int fd)
    int res = __libc_close(fd);
    fprintf(stderr, "close: %d ret %d\n", fd, res);
    return res;

to debug runtime issues. Then I built this as a shared library and
loaded it using the Linux dynamic loader's LD_PRELOAD environment

_Linkers and Loaders_ (sect. 6.4) also mentions that "UNIX linkers and
many MS Windows linkers take an intermixed list of object files and
libraries on the command line...and process each in order" and mentions
that you could use this technique by including -lmymalloc before -lc on
the linker command line. I can't find any specific documentation of
this in the GNU ld manual, though.

[That last technique is in the folklore. It won't work if libc has its internal
references already bound, but on a modern system I doubt that it will. -John]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.