am writing a hook to a function using the basic x86 method of inserting a jump worth 5 bytes. My code is rusty but I think i have the logic. I get a segmentation fault error when I run against LD\_PRELOAD env var. Am basically using a replacement function, hook func, trampoline function to modify and return the original address. Below is the code link.

**foo.h**

```
#ifndef foo_h__
#define foo_h__

extern void foo(const char*);

#endif  // foo_h_

```

**foo.c**

```
#include <stdio.h>

void foo(const char*str)
{
    puts(str);
}

```

**main.c**

```
#include <stdio.h>
#include "foo.h"

int main(void)
{const char*str="I am a shared lib!\n";
        int count=1;
    puts("This is a shared library test...");
    while(count!=200){
        printf("%d time!\n",count);    
    foo(str);
    count++;
}
    return 0;
}

```

**hook.c**

```
# include <stdio.h>
# include <unistd.h>
# define __USE_GNU
# include <dlfcn.h>
# include <stdint.h>
# include <sys/mman.h>

const char*str = "Hooked! ma fucker!\n";
struct hookdata
{
    int64_t*origFunc;
    int64_t*newFunc;
    const char*s;
    void (*foo_trampoline)(const char*str);
}*hkd;

void fooHooked(const char*str)
{
    puts(str);
    hkd->foo_trampoline(hkd->s);

}

void hook(void)
{
//Get pointers to the original and new functions and calculate the jump offset
    hkd->origFunc = dlsym(RTLD_NOW, "foo");
    hkd->newFunc = (int64_t*) &fooHooked;
    int64_t offset = hkd->newFunc - (hkd->origFunc + 5);
//Make the memory containing the original funcion writable
//Code from http://stackoverflow.com/questions/20381812/mprotect-always-returns-invalid-arguments
    size_t pageSize = sysconf(_SC_PAGESIZE);
    uintptr_t start = (uintptr_t) hkd->origFunc;
    uintptr_t end = start + 1;
    uintptr_t pageStart = start & -pageSize;
    mprotect((void *) pageStart, end - pageStart,
            PROT_READ | PROT_WRITE | PROT_EXEC);
//Insert the jump instruction at the beginning of the original function
    int32_t instruction = 0xe9 | offset << 8;
    *hkd->origFunc = instruction;
}
void foo(const char*str)
{
    if (*hkd->origFunc == 0xe9)
    {
        printf("hook detected!");
    }
    else
        hook();
}

```
['https://Stackoverflow.com/questions/42156145', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6423730/']
You can pass it as null. It's mentioned in the doc also

> 
> inCertificate is the Object which is holding Certificate Information.
>  Pass this as null if no client certificate is used by the merchant (as
>  given above in the Prerequisites section)
> 
> 
>
I suggest you to use PGSDK 2.0 instead of old version which is not required any **Client side certificate** 

```
----------
protected void onCreate(Bundle savedInstanceState) {

        super.onCreate( savedInstanceState );

 PaytmButton.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
                 getPaytmWindow() ;
            }
        } );
}

}

private void getPaytmWindow(String checksum) {
//use PaytmPGService Service = PaytmPGService.getProductionService();in production//

  PaytmPGService Service = PaytmPGService.getStagingService();

        //Kindly create complete Map and checksum on your server side and then put it here in paramMap.

        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put( "MID", abc14146028455452" );
        paramMap.put( "ORDER_ID", "GTR6079" );
        paramMap.put( "CUST_ID", "1132" );
        paramMap.put( "INDUSTRY_TYPE_ID", "Retail" );
        paramMap.put( "CHANNEL_ID", "WAP" );
        paramMap.put( "TXN_AMOUNT", "76" );
        paramMap.put( "WEBSITE", "APP_STAGING" );
        paramMap.put( "EMAIL", "test@gmail.com" );
        paramMap.put( "MOBILE_NO", "7777777777" );
        paramMap.put( "CALLBACK_URL", "https://pguat.paytm.com/paytmchecksum/paytmCallback.jsp" );
        paramMap.put( "CHECKSUMHASH", "DkrZCCNCKS0h4IbLEk8HWqTClr6PCK/+Zh3xyW7fXfOsfHsmhmt3/wGx5pxgWrCSNCObPHyYFE3VJob0v7QQdkBFNyNAO7CR2+e2BiVgQpM=" );
        PaytmOrder Order = new PaytmOrder( paramMap );

        Service.initialize( Order, null );

        Service.startPaymentTransaction( this, true, true,
                new PaytmPaymentTransactionCallback() {

                    @Override
                    public void someUIErrorOccurred(String inErrorMessage) {
                        // Some UI Error Occurred in Payment Gateway Activity.
                        // // This may be due to initialization of views in
                        // Payment Gateway Activity or may be due to //
                        // initialization of webview. // Error Message details
                        // the error occurred.
                        Log.d( "LOG123444", "someUIErrorOccurred : " + inErrorMessage );
                    }

                    @Override
                    public void onTransactionResponse(Bundle inResponse) {
                        Log.d( "LOG123444", "Payment Transaction : " + inResponse );

                        if (inResponse.getString( "STATUS" ).contains( "TXN_SUCCESS" ))
                        {

                            Toast.makeText(getApplicationContext(),"Transaction completed",Toast.LENGTH_LONG).show();
                        }

                            Log.i( "LOG123444", inResponse.getString( "STATUS" )  );

//                        Toast.makeText( getApplicationContext(), "Payment Transaction response " + inResponse.toString(), Toast.LENGTH_LONG ).show();
                    }

                    @Override
                    public void networkNotAvailable() {
                        // If network is not
                        // available, then this
                        // method gets called.
                    }

                    @Override
                    public void clientAuthenticationFailed(String inErrorMessage) {
                        // This method gets called if client authentication
                        // failed. // Failure may be due to following reasons //
                        // 1. Server error or downtime. // 2. Server unable to
                        // generate checksum or checksum response is not in
                        // proper format. // 3. Server failed to authenticate
                        // that client. That is value of payt_STATUS is 2. //
                        // Error Message describes the reason for failure.
                        Log.i( "LOG123444", "clientAuthenticationFailed " + inErrorMessage );
                    }

                    @Override
                    public void onErrorLoadingWebPage(int iniErrorCode,
                                                      String inErrorMessage, String inFailingUrl) {
                        Log.i( "LOG", "inErrorMessage " + inErrorMessage );
                        Log.i( "LOG", "inFailingUrl " + inFailingUrl );

                    }

                    // had to be added: NOTE
                    @Override
                    public void onBackPressedCancelTransaction() {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void onTransactionCancel(String inErrorMessage, Bundle inResponse) {
                        Log.d( "LOG", "Payment Transaction Failed " + inErrorMessage );
                        Toast.makeText( getBaseContext(), "Payment Transaction Failed ", Toast.LENGTH_LONG ).show();
                    }

                } );

}

```