Re: How to link file with wrapper function

spam@altium.nl (Dick Streefland)
Tue, 04 Nov 2008 15:10:58 -0000

          From comp.compilers

Related articles
How to link file with wrapper function vasvibhatt@gmail.com (Vasvi Kakkad) (2008-11-04)
Re: How to link file with wrapper function dmaze@mit.edu (David Z Maze) (2008-11-04)
Re: How to link file with wrapper function spam@altium.nl (2008-11-04)
Re: How to link file with wrapper function bill@qswtools.com (Bill Cox) (2008-11-08)
| List of all articles for this month |
From: spam@altium.nl (Dick Streefland)
Newsgroups: comp.compilers
Date: Tue, 04 Nov 2008 15:10:58 -0000
Organization: Altium BV
References: 08-11-018
Keywords: C++, linker, GCC
Posted-Date: 04 Nov 2008 12:54:24 EST

"Vasvi Kakkad" <vasvibhatt@gmail.com> wrote:
| 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));
| -----
| }


Here is an example with wrappers for malloc & free:


/*
  * Compile with: gcc -shared -o wrapper.so wrapper.c
  * Preload with: LD_PRELOAD=./wrapper.so <application>
  */


#include <stdlib.h>
#include <stdio.h>


extern __typeof (malloc) __libc_malloc;


void* malloc ( size_t size )
{
void* ptr;


ptr = __libc_malloc( size );
fprintf( stderr, "malloc(%5d) = 0x%p\n", size, ptr );
return ptr;
}


extern __typeof (free) __libc_free;


void free ( void* ptr )
{
fprintf( stderr, "free(%p)\n", ptr );
__libc_free( ptr );
}


--
Dick Streefland //// Altium BV
dick.streefland@altium.nl (@ @) http://www.altium.com
--------------------------------oOO--(_)--OOo---------------------------
[Note that this hack is very specific to GCC. -John]



Post a followup to this message

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