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 10, 2013, 07:27

I’m impressed, I have to say. Really not often do I encounter a blog that’s each educative and entertaining, and let me inform you, you could have hit the nail on the head. Your thought is excellent; the issue is something that not sufficient individuals are talking intelligently about. I’m very happy that I stumbled throughout this in my search for one thing referring to this.

http://www.anykeylogger.com/how-to-catch-a-cheater.html

September 26, 2013, 19:29

The Impossible [Blu-ray] By:

July 13, 2013, 02:25

It is very nice helpful article. But I want to advice to a air conditioner in a system of air conditioning and want heating and colling air conditioning in 24 hour service that I get help from there.
So wait for your next article

September 14, 2013, 19:11

BMW Ladies Microfiber Cap – White/Pink By:
<A>http://www.hiphopcap.info/womens-hats/baseball-caps/bmw-ladies-microfiber-cap-whitepink-by.html

July 13, 2013, 02:25

It is very nice helpful article. But I want to advice to a air conditioner in a system of air conditioning and want heating and colling air conditioning in 24 hour service that I get help from there.
So wait for your next article

September 16, 2013, 18:41

Seven Psychopaths (+UltraViolet Digital Copy) [Blu-ray] By:

July 13, 2013, 02:25

It is very nice helpful article. But I want to advice to a air conditioner in a system of air conditioning and want heating and colling air conditioning in 24 hour service that I get help from there.
So wait for your next article

July 13, 2013, 02:26

It is very nice helpful article. But I want to advice to a air conditioner in a system of air conditioning and want heating and colling air conditioning in 24 hour service that I get help from there.
So wait for your next article

July 13, 2013, 02:26

It is very nice helpful article. But I want to advice to a air conditioner in a system of air conditioning and want heating and colling air conditioning in 24 hour service that I get help from there.
So wait for your next article

July 13, 2013, 02:26

It is very nice helpful article. But I want to advice to a air conditioner in a system of air conditioning and want heating and colling air conditioning in 24 hour service that I get help from there.
So wait for your next article

July 13, 2013, 02:26

It is very nice helpful article. But I want to advice to a air conditioner in a system of air conditioning and want heating and colling air conditioning in 24 hour service that I get help from there.
So wait for your next article

July 13, 2013, 02:26

It is very nice helpful article. But I want to advice to a air conditioner in a system of air conditioning and want heating and colling air conditioning in 24 hour service that I get help from there.
So wait for your next article

July 13, 2013, 10:02

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

July 13, 2013, 10:54

Thanks for this very interesting and charm full article, but I want to advice electrical services lighting
Thank you very much

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 17, 2013, 10:51

Great stuff! its really helpful article from last 3 days i searching such kind of info only...

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. .....

I enjoy it. everyone visits and shares information.

July 28, 2013, 07:47

At last, I've found it. Keep on posting helpful information.

I am happy to find your distinguished way of writing the post.

July 31, 2013, 10:28

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! Keep up the good work..

August 06, 2013, 00:48

nice and good content.thanks for this nice post.

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 20, 2014, 02:11

I was very happy that I discovered this website. I needed to thank you for this excellent information . best forex trading platform I have bookmark this page and after work I read interesting things to update myself and to refresh my mind

March 20, 2014, 02:11

I was very happy that I discovered this website. I needed to thank you for this excellent information . best forex trading platform I have bookmark this page and after work I read interesting things to update myself and to refresh my mind

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.

April 12, 2014, 09:11

Yes that idea is so coreect and helpful

Leave a new comment

(why do I need your e-mail?)

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

Type the first letter of your name: (just to make sure you're a human)

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

Advertisements