Sign in to follow this  
EnergyTurtle

LD_PRELOAD Issue

Recommended Posts

Hi, just a little background: I'm working on Ubuntu 8.04 with C. I've recently wanted to 'hijack' opengl library functions. To do this, I've been looking into writing my own shared object and using the LD_PRELOAD to link it to my target executable. Unfortunately, it seems that my 'hijack' function is not being called. I wrote two simple programs to get this issue ironed out. One simply compared two (different) strings and prints a message based on the comparison. The second program is my new strcmp function. It always returns 0.
//vic.c

#include <stdio.h>
#include <string.h>

int main()
{
   if (!strcmp("one","two"))
   {
      printf("Hijack is a go!\n");
   }
   else
   {
      printf("Failure...\n");
   }
   return 0;
}

//hjk.c

#include <stdio.h>
#include <string.h>

int strcmp(const char *s1, const char *s2) {
   printf("s1 [%s]\n", s1);
   printf("s2 [%s]\n", s2);
   return 0;
}

$ gcc vic.c -o vic
$ gcc -Wall -fPIC -shared -o hjk.so hjk.c -ldl
$ LD_PRELOAD=./hjk.so
$ echo $LD_PRELOAD
./hjk.so
$ ./vic
Failure...
$ ;_; 
bash: syntax error near unexpected token `;'
As you can see, something is obviously going wrong. Google has given me no useful information other that different gcc configurations that didn't seem to make a difference.

Share this post


Link to post
Share on other sites
gcc is too smart for you [smile]

Run objdump -x vic and look for the disassembly of the main function:

08048374 <main>:
8048374: 8d 4c 24 04 lea 0x4(%esp),%ecx
8048378: 83 e4 f0 and $0xfffffff0,%esp
804837b: ff 71 fc pushl -0x4(%ecx)
804837e: 55 push %ebp
804837f: 89 e5 mov %esp,%ebp
8048381: 51 push %ecx
8048382: 83 ec 04 sub $0x4,%esp
8048385: c7 04 24 60 84 04 08 movl $0x8048460,(%esp)
804838c: e8 43 ff ff ff call 80482d4 <puts@plt>
8048391: b8 00 00 00 00 mov $0x0,%eax
8048396: 83 c4 04 add $0x4,%esp
8048399: 59 pop %ecx
804839a: 5d pop %ebp
804839b: 8d 61 fc lea -0x4(%ecx),%esp
804839e: c3 ret
804839f: 90 nop

As you can see, gcc figured out that you're comparing two constant strings and simply optimized the call to strcmp away.

As for your original problem, gcc obviously cannot optimize OpenGL calls away, so I'm not sure what's going on. Perhaps the program you're trying to analyze does not call GL functions via the library mechanism and uses function pointers instead? Also, you might want to look into BuGLe.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this