Skip to content
Advertisement

Compiling pintool with sqlite3 database

I am writing a pintool to instrument my binary.

I would like to use sqlite3 database to store the information about instructions.

I can compile and execute sqlite3 “helloworld” example without any problem. As well I can compile and execute my pintool without sqlite connection. However, whenever I integrate sqlite code in the pintool I have an error:

dlopen failed: library “libsqlite3.so.0” not found

What exactly I do:

  1. My linking command looks like (compilation finishes without any error):

    g++ -shared -Wl,–hash-style=sysv /home/roman/Software/pin/intel64/runtime/pincrt/crtbeginS.o -Wl,-Bsymbolic -Wl,–version-script=/home/roman/Software/pin/source/include/pin/pintool.ver -fabi-version=2 -o obj-intel64/sqliteTest.so obj-intel64/sqliteTest.o -L/home/roman/Software/pin/intel64/runtime/pincrt -L/home/roman/Software/pin/intel64/lib -L/home/roman/Software/pin/intel64/lib-ext -L/home/roman/Software/pin/extras/xed-intel64/lib -L/usr/lib/i386-linux-gnu -lpin -lxed /home/roman/Software/pin/intel64/runtime/pincrt/crtendS.o -lpin3dwarf -ldl-dynamic -nostdlib -lstlport-dynamic -lm-dynamic -lc-dynamic -lsqlite3

  2. LDD command ldd obj-intel64/sqliteTest.so gives me the following result:

linux-vdso.so.1 => (0x00007fff4f4aa000)

libxed.so => not found

libpin3dwarf.so => not found

libdl-dynamic.so => not found

libstlport-dynamic.so => not found

libc-dynamic.so => not found

libsqlite3.so.0 => /usr/lib/x86_64-linux-gnu/libsqlite3.so.0 (0x00007fd46221f000)

libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fd462002000)

libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd461dfe000)

libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd461a34000)

/lib64/ld-linux-x86-64.so.2 (0x000055565d930000)

  1. The symbol link ls -l /usr/lib/x86_64-linux-gnu/libsqlite*:

-rw-r–r– 1 root root 1156080 Feb 22 17:43 /usr/lib/x86_64-linux-gnu/libsqlite3.a

-rw-r–r– 1 root root 965 Feb 22 17:43 /usr/lib/x86_64-linux-gnu/libsqlite3.la

lrwxrwxrwx 1 root root 19 Feb 22 17:43 /usr/lib/x86_64-linux-gnu/libsqlite3.so -> libsqlite3.so.0.8.6

lrwxrwxrwx 1 root root 19 Feb 22 17:43 /usr/lib/x86_64-linux-gnu/libsqlite3.so.0 -> libsqlite3.so.0.8.6

-rw-r–r– 1 root root 870240 Feb 22 17:43 /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6

  1. The sqlite3 version is 3.13.0

  2. When I run the pin tool I got the following:

/home/roman/Software/pin/pin -t /home/roman/Software/pin/source/tools/sqliteTest/obj-intel64/sqliteTest.so — ./test.bin

E: Unable to load /home/roman/Software/pin/source/tools/sqliteTest/obj-intel64/sqliteTest.so: dlopen failed: library “libsqlite3.so.0” not found

  1. In case you wonder about the code:

    VOID StartApp(VOID *v){
    
      int rc;
      char nameDB[100];
    
      sprintf(nameDB,  "pin_test_0.dat");
    
      rc = sqlite3_open(nameDB, &sqliteDB);
    
      if( rc ){
        fprintf(stderr, "Can't open database: %sn", sqlite3_errmsg(sqliteDB));
        return;
      }else{
        fprintf(stderr, "Opened database (%s) successfullyn", nameDB);
      }
      sqlite3_close(sqliteDB);
    
      //Start random number generator
      srand(rdtsc());
    }
    ...
    INT main(int argc, char *argv[]){
    
      if( PIN_Init(argc,argv) )
      {
        return Usage();
      }
    
      TRACE_AddInstrumentFunction(Trace, 0);
    
      PIN_AddApplicationStartFunction(StartApp, 0);
    
      PIN_AddFiniFunction(Fini, 0);
    
      PIN_StartProgram();
    
      return 0;
    }
    
  2. When I put -Wl and –verbose options during the linking I got the following info:

attempt to open /usr/lib/i386-linux-gnu/libsqlite3.so failed

attempt to open /usr/lib/i386-linux-gnu/libsqlite3.a failed

attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/libsqlite3.so failed

attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/libsqlite3.a failed

attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libsqlite3.so succeeded

-lsqlite3 (/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libsqlite3.so)

Do you have any idea what is wrong with my hands?

Apparently pin v.3 has specific requirements for external libraries which I could not understand. I downgraded to pin v.2.8 and everything worked fine.

Advertisement

Answer

Pin changes the LD_LIBRARY_PATH when it starts, which may mean it won’t find libsqlite in system directories. Pin 3 also avoids linking against certain system libraries to enhance isolation.

You shouldn’t link against libpthread in either Pin 2 or Pin 3 or you’re going to run into subtle bugs in the future. Sqlite seems to depend on libpthread. If you need to do something that requires a database, I recommend that you create an external process for that and communicate with it using named pipes/sockets.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement