Ginn Chen wrote: > I have two programs, malloc.c and main.c. > I have malloc/free functions in malloc.c, and main in main.c. > > If I compile malloc.c to a shared object, and main.c to a.out, then I > can override the malloc function at runtime by using LD_PRELOAD = libc > or libumem, etc. > > If I compile and link both malloc.c and main.c into a.out, is it > possible to override the malloc function at runtime? > Is there a ld option to do something like this? > > Thanks, > > Ginn > > -------- > Ginn Chen > Software Engineer, Browser Team > Sun Microsystems, Inc. > Phone: x82869 / +86-10-62673869 > Fax: +86-10-62780969 > > > _______________________________________________ > tools-linking mailing list > tools-linking at opensolaris.org
If you link your own malloc into your a.out, then your malloc will be the one used. Are you asking if you will then be able to override this using LD_PRELOAD? If that's what you're asking, then the answer is yes, as long as your malloc() is global, and you didn't use an option like -Bsymbolic when you linked the program. To verify, you could run your program with the LD_DEBUG environment variable, and look for references to 'malloc' in the output: % LD_DEBUG=bindings ./a.out I'd like to encourage you on general principles not to use a private malloc() if you have any choice in the matter. That's a decision you'll have to make, based on what you know about the problem you're trying to solve. On average though, private mallocs cause more problems than they solve. - Ali