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

Kaur deep Permalink
December 04, 2014, 03:31

Excellent and knowledgeable links and post.thanks for sharing.
Now, The Time has come for the Happy New Year 2015. It wil bring a lot fo happinesin our life.
Happy New Year 2015
And I am sure, You wil anjoy this new year day with yoru fiends and wil send some images wishes,
New Year 2015

Happy New Year 2015 Wishes
Greetings to yoru close ones, Your friends, You lover, etc..
Happy New Year 2015 Images
Even I am waititng for that day, because it just changes our life.
Happy New Year 2015 Greetings

Happy New Year 2015 SMS
So, That's why I am commenting here to wish you a very happy new year.
Happy New Year 2015 Messages
I hope this New year 2015 wil bring success to your website. But before that Merry Christmas is coming.
Merry Christmas Quotes
It is also a big event for everyone.
Merry Christmas Pictures
Everyone knows that Christmas is a day when People seems to be very happy, they just leave all sadness behind.
Merry Christmas Wallpapers
And I am sure, you also enjoy this Merry Christmas day of 2014.
Christmas day Images

Christmas stocking
But if you are not looking out to enjoy this christmas, then you are missing out on a big thing, because it really brings happiness.
Merry Christmas Images

Merry Christmas 2014
So, what you are waiting for?
Merry Christmas 2015
Enjoy This Merry Christmas day. Cheers :).

December 09, 2014, 09:35

nice
a href=" http://mynycstreets.com/2014/12/download-lingaa-movie-torrentlingaa-movie-free-download/">Lingaa Movie Torrent Downl

<oad>

lingaa movie in mp4lingaa movie in hdlingaa movie in free downloadLingaa Movie download in torrentfree download lingaa movie
diggnice article

December 09, 2014, 09:46

Hello, I was reading this article and this article is just awesome. catonmatIt was very informative and useful I'm gonna going to bookmark this website for further reference. First of all i have thank the author of the blog for writing such wonderful article.uc browser for pc Thank you so much. Keep writing such useful and knowledgeable article. Bookmarked your site.

Diana Permalink
December 17, 2014, 15:18

This article is quite helpful and informative too. I enjoyed a lot. Thanks for sharing such a great article.

christmas quotes
christmas wishes quotes
merry christmas quotes
funny christmas quotes
inspirational christmas quotes
christmas eve quotes
best christmas quotes
famous christmas quotes
happy christmas quotes
christmas blessing quotes
christmas greeting quotes
quotes about christmas
christmas inspirational quotes

merry christmas message for Whatsapp
christmas message for Whatsapp
Christmas Messages for Whatsapp
christmas message
christmas greetings message
christmas greeting messages
christmas greetings message
christmas greeting message
christmas messages
christmas card messages
christmas greeting card messages
christmas messages for cards
christmas sms messages
christmas message for friends
merry christmas message
christmas message for family
christmas wishes messages
christmas cards messages
funny christmas messages
top christmas messages
best christmas messages
besutiful christmas messages

christmas greeting
christmas greeting cards
christmas greetings for cards
christmas greeting words
christmas wishes greetings
christmas greetings wishes
free christmas greetings
merry christmas greeting
christmas cards greetings
christmas card greetings
christmas greetings
christmas greeting card sayings
christmas greetings sayings

christmas wishes
christmas wishes text
merry christmas wishes
christmas wishes messages
christmas wishes for cards
best christmas wishes
funny christmas wishes
christmas wishes for friends
christmas wishes words
christmas wishes sms
christmas wish
merry christmas wishes messages

christmas wishes sayings
christmas greeting card sayings
christmas greetings sayings
What to Write in Christmas Card

christmas wallpaper
free christmas wallpaper
christmas wallpapers
merry christmas wallpaper
christmas wallpaper hd
christmas desktop wallpaper
free christmas wallpapers
merry christmas wallpapers
christmas tree wallpaper
animated christmas wallpapers
hd christmas wallpapers
wallpaper christmas

christmas tree decoration
christmas tree decorating ideas
christmas tree decorations
christmas tree decoration ideas
christmas tree decorating
christmas tree decorations ideas

Thanks for sharing such a great article.

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 "computer_386": (just to make sure you're a human)

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

Advertisements