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

      

  • 相关阅读:
    HDU1879 kruscal 继续畅通工程
    poj1094 拓扑 Sorting It All Out
    (转)搞ACM的你伤不起
    (转)女生应该找一个玩ACM的男生
    poj3259 bellman——ford Wormholes解绝负权问题
    poj2253 最短路 floyd Frogger
    Leetcode 42. Trapping Rain Water
    Leetcode 41. First Missing Positive
    Leetcode 4. Median of Two Sorted Arrays(二分)
    Codeforces:Good Bye 2018(题解)
  • 原文地址:https://www.cnblogs.com/JsonZhangAA/p/8870777.html
Copyright © 2011-2022 走看看