Last time I stopped at showing how to override functions in shared libraries by compiling your own shared library and preloading it via the LD_PRELOAD environment variable. Today I'll show you how to call the original function from the overridden function.

First let's review the code example that we used in the previous article. We had a program called prog.c that simply used fopen:

#include <stdio.h>

int main(void) {
    printf("Calling the fopen() function...\n");

    FILE *fd = fopen("test.txt", "r");
    if (!fd) {
        printf("fopen() returned NULL\n");
        return 1;
    }

    printf("fopen() succeeded\n");

    return 0;
}

Today let's write a shared library called myfopen.c that overrides fopen in prog.c and calls the original fopen from the c standard library:

#define _GNU_SOURCE

#include <stdio.h>
#include <dlfcn.h>

FILE *fopen(const char *path, const char *mode) {
    printf("In our own fopen, opening %s\n", path);

    FILE *(*original_fopen)(const char*, const char*);
    original_fopen = dlsym(RTLD_NEXT, "fopen");
    return (*original_fopen)(path, mode);
}

This shared library exports the fopen function that prints the path and then uses dlsym with the RTLD_NEXT pseudohandle to find the original fopen function. We must define the _GNU_SOURCE feature test macro in order to get the RTLD_NEXT definition from <dlfcn.h>. RTLD_NEXT finds the next occurrence of a function in the search order after the current library.

We can compile this shared library this way:

gcc -Wall -fPIC -shared -o myfopen.so myfopen.c -ldl

Now when we preload it and run prog we get the following output that shows that test.txt was successfully opened:

$ LD_PRELOAD=./myfopen.so ./prog
Calling the fopen() function...
In our own fopen, opening test.txt
fopen() succeeded

This is really useful if you need to change how a part of a program works or do some advanced debugging. Next time we'll look at how LD_PRELOAD is implemented.

Comments

Jalal Hajigholamali Permalink
June 05, 2013, 14:37

Hi,

Very nice and useful article

Thanks a lot

July 13, 2013, 10:02

This post is so much helpful. It is very understandable.

July 16, 2013, 21:21

thanks for the codes

July 17, 2013, 07:01

It's very helpful post.Lot's of thanks for publishing this article.

July 23, 2013, 00:56

I am so happy after the visit of this blog because it contains an informative and amazing post which i liked very much. .....

August 29, 2013, 08:53

thanks sir. form a long time i am searching for this program.

September 12, 2013, 03:49

thanks for nice share! This post is so much helpful. It is very understandable.

September 16, 2013, 09:29

Perfectly written content, really enjoyed reading through.

September 26, 2013, 10:45

Your post really helped me to understand about this. It has great details and yet it is easy to understand.That's what i was looking for. I will definitely share it with others.Thanks for sharing.
Rajshahi Market is the first and best ecommerce shopping place for get good quality products. 100% Fresh, organics and Non Preservative fruits, organic grocery stores at Rajshahi Bangladesh.

September 29, 2013, 09:52

De Plak bh is de ideale oplossing voor vrouwen om borsten te liften, zonder BH bandjes tijdens het dragen van een jurkje, bikini of strapless.

September 29, 2013, 09:56

It has great details and yet it is easy to understand.That's what i was looking for.

October 02, 2013, 20:07

Nice blog . I wanna learn more about this tutorial . please mail me when 2nd one released :)

October 26, 2013, 01:17

It has been years since I wrote in C and compiled with gcc. I stumbled upon your post and reading the little bit of code I just did in your post motivates me to play around with it again. ... This may turn into a very late night. :-)

kartikeyan Permalink
December 03, 2013, 05:05

Awesome. Its the helloworld of LD_PRELOAD.

December 30, 2013, 02:06

I am so happy after the visit of this blog because it contains an informative and amazing post which i liked very much. .....

March 23, 2014, 05:30

Thanks for sharing .Keep posting articles like this. A good example of content presentation. A piece of information from you every now and then is really great.

March 26, 2014, 10:11

Very nice information shared by you. I was just searching this type of information and luckily I got it from your blog. I like your blog also. Keep it up.

June 20, 2014, 06:15

That is so ok and thanks

October 30, 2014, 08:26

This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here!
Food Delivery Online
Keep up the good work..

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "quake3": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements