I want to add some functionality to an existing binary file. The binary file was created using gcc. Do I need to decompile the binary first, even though I sufficiently understand the functioning of the program ? How should I go about adding the necessary code ?Do I need any tools to be able to do this ?
How do I add functionality to an existing binary executable?
linux;c;executable;hll mapping
There are several broad ways in which you could do this.Dynamic instrumentationTools such as PIN, Valgrind, or DynamoRIO allow you to dynamically change the behavior of a program.  For instance, you can add calls to new functions at particular addresses, intercept library calls and change them, and much more.The downside is that dynamic instrumentation often has high overhead.Static instrumentationYou can also try to statically modify the program to add the desired behavior.  One challenge is that you often need to muck around with the executable file format.  Some tools, such as elfsh from the ERESI project exist for this, but I have found them buggy and difficult to use.Another strategy for static instrumentation is to recompile.  You can do this by decompiling the program, modifying the source code, and recompiling.  In theory, you could also use a tool like BAP to lift the program to IL, modify it, and then re-compile it using LLVM.  However, the current version is probably not mature enough for this.Dynamic loadingYou can use LD_PRELOAD to override functions that are going to be dynamically linked.  This is a nice option when you want to change the behavior of a library function.  Naturally, it does not work on statically linked binaries, or for static functions.Binary patchingYou can often make simple changes to a binary using a hex-editor.  For instance, if there is a function call or branch you would like to skip, you can often replace it with nop instructions.  If you need to add a large amount of new code, you will probably need to use something like elfsh from the ERESI project to help you resize the binary.