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


Jalal Hajigholamali Permalink
June 05, 2013, 14:37


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

a href="">Lingaa Movie Torrent Downl


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.

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

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