zoukankan      html  css  js  c++  java
  • 库打桩机制

    1.编译时打桩
    linux>gcc -DCOMPILETIME -c mymalloc.c
    linux>gcc -I. -o intc int.c mymalloc.o
    linux>./intc
    使用-I.参数,它会使C预处理器会在搜索通常的系统目录之前,现在当前目录中查找

    mymalloc.c:
    #ifdef COMPILETIME
    #include <stdio.h>
    #include <malloc.h>
    void * mymalloc(size_t size){
        void * ptr=malloc(size);
        printf("malloc(%d)=%p
    ",(int)size,ptr);
        return ptr;
    }
    void myfree(void * ptr){
        free(ptr);
        printf("free(%p)
    ",ptr);
    }
    #endif
    
    malloc.h:
    #define malloc(size) mymalloc(size)
    #define free(ptr) myfree(ptr)
    void * mymalloc(size_t size);
    void myfree(void * ptr);
    
    int.c:
    #include <stdio.h>
    #include <malloc.h>
    int main(){
        int * p=malloc(32);
        free(p);
        return 0;
    }
    

      

    2.链接时打桩
    Linux静态连接器支持使用--wrap f标志来进行链接时打桩,链接器会将f解析为__wrap_f,还要把对符号__real_f解析为f。
    linux>gcc -DLINKTIME -c mymalloc.c
    linux>gcc -c int.c
    linux>gcc -Wl,--wrap,malloc --Wl,--wrap,free -o intl int.o mymalloc.o

    mymalloc:
    #ifdef LINKTIME
    #include <stdio.h>
    void * __real_malloc(size_t size);
    void __real_free(void * ptr);
    void * __wrap_malloc(size_t size){
    	void * ptr=__real_malloc(size);
    	printf("malloc(%d)=%p
    ",(int)size,ptr);
    	return ptr;
    }
    void __wrap_free(void * ptr){
    	__real_free(ptr);
    	printf("free(%p)
    ",ptr);
    }
    #endif
    
    malloc.h:
    #define malloc(size) mymalloc(size)
    #define free(ptr) myfree(ptr)
    void * mymalloc(size_t size)
    void myfree(void * free)
    
    int.c
    #include <stdio.h>
    #include <malloc.h>
    int main()
    {
    	int * p=malloc(32);
    	free(p);
    	return 0;
    }
    

      

    3.运行时打桩
    通过设置LD_PRELOAD环境变量,来使动态链接器先搜索LD_PRELOAD库,然后再搜索其他的库。
    linux>gcc -DRUNTIME -shared -fpic -o mymalloc.so mymalloc.c -ldl
    linux>gcc -o intr int.c
    linux>LD_PRELOAD="./mymalloc.so" ./intr

    mymalloc.c:
    #ifdef RUNTIME
    #define _GNU_SOURCE
    #include <stdio.h>
    #include <stdlib.h>
    #include <dlfcn.h>
    void * malloc(size_t size){
    	void *(*mallocp)(size_t size);
    	char * error;
    	mallocp=dlsym(RTLD_NEXT,"malloc");
    	if((error=dlerror())!=NULL){
    		fputs(error,stderr);
    		exit(1);
    	}
    	char * ptr=mallocp(size);
    	printf("malloc(%d)=%p
    ",(int)size,ptr);
    	return ptr;
    }
    void free(void * ptr){
    	void (*freep)(void *)=NULL;
    	char * error;
    	if(!ptr)
    		return;
    	freep=dlsym(RTLD_NEXT,"free");
    	if((error=dlerror())!=NULL){
    		fputs(error,stderr);
    		exit(1);
    	}
    	freep(ptr);
    	printf("free(%p)
    ",ptr);
    }
    #endif
    
    其他两个一样
    

      

  • 相关阅读:
    elinput 金额校验
    使用PHPExcel实现Excel文件的导入和导出(模板导出) (转载自用)
    Scrapy 轻松定制网络爬虫
    在线学习(Online Learning)
    AdaBoost ,online Adaboost
    斯坦福飞鱼滑翔装置完胜跳跃机器人
    机器学习中的集成学习方法(一)理论
    Boosting, Online Boosting,adaboost
    struts.xml配置方法
    java实现单向链表
  • 原文地址:https://www.cnblogs.com/JsonZhangAA/p/8870777.html
Copyright © 2011-2022 走看看