All pastes #794399 Raw Edit

Stuff

public text v1 · immutable
#794399 ·published 2007-11-23 02:37 UTC
rendered paste body
#############################################################
# During Make....
gcc -g -Wall -fPIC -Isdk  -c init.c
init.c:14: warning: initialization makes integer from pointer without a cast
init.c: In function 'isCurrentProcess':
init.c:42: warning: implicit declaration of function 'basename'
init.c:42: warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast
gcc -g -Wall -fPIC -Isdk  -c hook.c
hook.c: In function 'inithooks':
hook.c:24: warning: cast to pointer from integer of different size
gcc -ldl -shared -s -fPIC /usr/lib/libghf.a init.o hook.o -o fusion.so
#############################################################

#############################################################
# Makefile

CC = gcc
LD = gcc
CFLAGS = -g -Wall -fPIC -Isdk 
LDFLAGS = -ldl -shared -s -fPIC $(DETOURLIBS)

RM = /bin/rm -f

DETOURLIBS = /usr/lib/libghf.a
OBJS = init.o hook.o
PROG = fusion.so

# Main rule
all: $(PROG)

# Link
$(PROG): $(OBJS)
	$(LD) $(LDFLAGS) $(OBJS) -o $(PROG)

init.o: init.c
	$(CC) $(CFLAGS) -c init.c
hook.o: hook.c
	$(CC) $(CFLAGS) -c hook.c

clean:
	$(RM) $(PROG) $(OBJS)
#############################################################

#############################################################
# fusion.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <gelf.h>
#include <ghf.h>

#include "src/qcommon/vm_local.h"

/* Defines */
#define FUSION_VERSION     "Icarus"
#define TREMULOUS_EXE_NAME "tremulous.x86"
#define PROC_SELF_EXE      "/proc/self/exe"
#define CGAME_QVM_NAME     "cgame"
#define UI_QVM_NAME        "ui"

/* Prototypes */
int  isCurrentProcess( char *name );
void inithooks();
void *detourFunction(void *orig, void *det);
GElf_Sym *getSymbol(char *filename, char *symbol);


/* Function typedefs */
typedef vmHeader_t *VM_LoadQVM_t(vm_t *vm, qboolean alloc);

/* Detour function declarations */
vmHeader_t *fVM_LoadQVM(vm_t *vm, qboolean alloc);

/* Original function declarations */
VM_LoadQVM_t *VM_LoadQVM;
#############################################################

#############################################################
# init.c

#include "fusion.h"

int fusion = NULL;

__attribute__ ((constructor))
void fusionStart() {  
  printf("[FUSION] Starting up...\n");

  if(isCurrentProcess(TREMULOUS_EXE_NAME)){
    printf("[FUSION] \"%s\" initalization started. \n", FUSION_VERSION);
    printf("[FUSION] Current process match!\n");
    inithooks();
  } else {
    printf("[FUSION] Fatal Error: Not a Tremulous executable.\n" );
    exit(1);
  }
}

int isCurrentProcess( char *name ) {
  char exeFilename[512];
  memset(exeFilename, 0, sizeof(exeFilename));

  if(readlink(PROC_SELF_EXE, exeFilename, sizeof(exeFilename)) <= 0) {
     perror("[FUSION] Fatal Error: Can't find executable! " PROC_SELF_EXE);
     exit(1);
  }

  return !strcmp(basename(exeFilename), name);
}
#############################################################

#############################################################
# hook.c

#include "fusion.h"

/* Dynamic vmHeader*/
vmHeader_t vmHeader;

/* Main struct pointers */

/* Hook function */
void inithooks() {
  VM_LoadQVM = (VM_LoadQVM_t *)detourFunction( (void *)getSymbol(NULL, "VM_LoadQVM")->st_value, (void *)fVM_LoadQVM);
}

vmHeader_t *fVM_LoadQVM(vm_t *vm, qboolean alloc) {
  vmHeader_t *result = VM_LoadQVM(vm, alloc);

  if (!strcmp(vm->name, CGAME_QVM_NAME))
      vmHeader = *result;
  return result;
}

#############################################################