Build a Simple Pin Tool

In this post, I prepare a post about how to build your first pin tool. Building a pin tool is somehow difficult.  After compiling the first program, there where lots of new problems, so, believe me, it annoys you a lot but keep in mind if you discover a new problem in compiling, please comment about that and I add your problem and solving solution to the post. After opening the Visual Studio, I see a huge number of errors that didn’t seem to be solved without violence 😉 so that I decided to use Gnu Win32 make.exe in order to compile my pin tool. Let start.

Preparing Environment

First of all download pin from Intel’s official site and then download Gnu Win32 make.exe and its dependencies. In the following list, I mentioned the dependencies that I downloaded and put beside of make.exe (in the same directory) but it might be something different in your case then you should download them manually based on the error that “make” shows you when you run it. Libicon2.dll Libintl3.dll Libcharset.dll After solving all the errors about dependency then I realized that make.exe uses cut.exe in its compiling process so I also download cut.exe and as you might know, cut.exe has some dependencies too! Cggiconv-2.dll Cygintkl-8.dll Cygwin1.dll I found this dll files from the path where Cygwin GCC is installed (it is also available at CoreUtils) so you can first download Cygwin from here then you can find and copy cut.exe and its dependencies from where you installed it. Again running make causes new errors 🙁 This time make.exe needs iconv.exe and after downloading this file make.exe seems to run correctly. You should also have c++ compiler installed previously and as I already install Visual Studio then these files are ready and I just need to provide an environment variable that points to this path and I’m good to go.  In my visual studio, the path is as follows but it might be different in your case.

and in the case when you want to compile a 64bit pin tool then you should change this variable to :

Important Note: Keep in mind to restart your computer after adding a new variable or modifying one. In my case when I want to change my context to build x64 binaries then I add \amd64 to the path and restart the computer again. You should now be able to build your first pin tool but there are some errors that sometimes might happen because of linker problem or syntax error or header mismatching that I discuss some of them in the rest of this topic Please note that as I said above, if you encountered new errors please comment on this post to add your problem to the following list.

Problem with Linking

The very first problem that I encountered during compiling my first application was that linker can’t find libraries of some APIs. In my case it needs kernek32.lib. I found kernel32.lib from the following path (And you know it might be different in your case.)

Or in the case of 64-bit pin tool :

I copied this file to the make.exe path but you can also export this path to path in environment variables as I described previously. After copying this file I added the following line to my tool’s source code after all #include(s) :

You by now should understand that your tool might need another .lib file to compile so you can provide more libraries for linker, same as what you did for kernel32.lib.

Building A Pin Tool

To build a new pin tool you should use make as follows : In the case of ia32 or x86 architecture :

In the case of Amd64 or x64 architecture :

Note: If your tool source file is like “inscount0.cpp” then you should use “inscount0.dll”, I mean its name should be same as source code except its extension which changes from .cpp to .dll. Please note as I mentioned above you should change Environment Variable in the case of x64 and x86 version.

Run Pin with your Tool

To run your pin tool you should run the following command.

I just instrument cmd.exe using pin by inscount0.dll tool. I also pass the dir as an argument to cmd.exe.

The last thing

As I tested, pin tool has a problem with passing x64 tools to the x64 version of the pin so please don’t use the following path for running x64 tools :

Instead, you should just use the following path (For x64 tools) :

That’s it, guys, I should also give my special thanks to Sobhan Sekhavatian one of my best friends for helping me to write this post. I hope by now you built your first pin tool  😉

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.