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
    
    其他两个一样
    

      

  • 相关阅读:
    针对Ext js的分页存储过程适用于sqlserver2008
    30分钟LINQ教程
    windows server 2003 sp2安装VS2010之后需要打的几个布丁
    【翻译】Prism4:初始化Prism应用程序(上)
    ASP.NET WebAPI 路由规则与POST数据
    基于.net开发chrome核心浏览器【二】
    六种SQL Server删除重复行的方法
    Web在线操作Office文件 (转)
    ASP.NET 中如何对生成的 HTML 内容流进行控制?
    使用键值表实现通用流水号(转)
  • 原文地址:https://www.cnblogs.com/JsonZhangAA/p/8870777.html
Copyright © 2011-2022 走看看