Thread: Changing symbol's linkage

  1. #1
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152

    Changing symbol's linkage

    Is there a way to "overload" the symbols of a binary? For example, I compiled my application and sent it to user. He is running it, but he discovers that the application randomly crashes. I suspect that is a memory leak problem, but I can't reproduce the problem myself. I would like the binary to, at execution time use the malloc from a library that I would provide, instead of C malloc. You could argue that I should use Valgrind for a memory leak problem (and I would), but this is just an example of what I want to do. Actually I would like to manage many resources, just to keep trace of them, like file descriptors.

    My target platform is Linux, compiling with GCC. The project is also ported to GCC on Windows.

    Thanks any help or dirctions, I will gladly specify better my problem if needed.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Write a small module with your implementation of malloc(). Compile with -fPIC option and turn it into a shared library:

    Code:
    $ gcc -fPIC -c my_malloc.c
    $ ld -shared -o libmy_malloc.so my_malloc.o -lc
    Send it to the user and have him give the command:

    Code:
    export LD_PRELOAD=libmy_malloc.so
    This will override the standard library malloc() with your own. It will override it for ALL programs he runs after that point, at least in that shell.

    Of course, you probably want your override malloc() to call the built-in malloc() at some point. You can refer to it in your stub code as __libc_malloc() (double underscore at front)

  4. #4
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Thanks so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM