• 库打桩机制


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

      

  • 相关阅读:
    50种方法优化SQL Server
    VS2015在Windows 10 下面安装经验
    python中yield的用法详解——最简单,最清晰的解释
    Mac安装Allure
    Python之pymysql数据库操作
    Python操作Excel神器-openpyxl之写入
    Python OS 模块处理路径
    Python操作Excel神器-openpyxl之读取
    Python中的if __name__ == '__main__'
    Python元组常用方法及汇总
  • 原文地址:https://www.cnblogs.com/JsonZhangAA/p/8870777.html
Copyright © 2020-2023  润新知