c - 运行进程的重载符号(LD_PRELOAD附件)

标签 c linux gdb runtime dynamic-linking

我正在为 Linux 开发堆分析器,称为 heaptrack .目前,我依靠 LD_PRELOAD 来重载各种(取消)分配函数,并且效果非常好。

现在我想扩展该工具以允许运行时附加到现有进程,该进程是在没有LD_PRELOAD我的工具的情况下启动的。我可以通过 GDB dlopen 我的库就好了,但这不会覆盖 malloc 等。我认为,这是因为此时链接器已经解析了位置相关代码已经运行的进程 - 正确吗?

那么我该怎么做才能重载 malloc 和 friend ?

我不精通汇编代码。根据我目前所读的内容,我想我必须以某种方式修补 malloc 和其他函数,以便它们首先回调到我的跟踪函数,然后继续它们的实际实现?那是对的吗?我该怎么做?

我希望有现成的工具,或者我可以为此利用 GDB/ptrace。

最佳答案

只是为了 lulz,另一种解决方案无需跟踪您自己的过程或接触单行汇编或使用 /proc。您只需在进程的上下文中加载库,让奇迹发生。

我建议的解决方案是使用构造函数 功能(由gcc 从C++ 引入C)在加载库时运行一些代码。然后这个库只是修补 malloc 的 GOT(全局偏移表)条目。 GOT 存储库函数的真实地址,因此名称解析只发生一次。要修补 GOT,您必须使用 ELF 结构(参见 man 5 elf)。 Linux 非常友好地为您提供了 aux vector (参见 man 3 getauxval),告诉您在内存中的何处可以找到 程序头当前程序。但是,dl_iterate_phdr 提供了更好的接口(interface),下面将使用它。

这是库的示例代码,它在调用 init 函数时执行此操作。尽管可以使用 gdb 脚本实现相同的目的。

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <sys/auxv.h>
#include <elf.h>
#include <link.h>
#include <sys/mman.h>


struct strtab {
    char *tab;
    ElfW(Xword) size;
};


struct jmpreltab {
    ElfW(Rela) *tab;
    ElfW(Xword) size;
};


struct symtab {
    ElfW(Sym) *tab;
    ElfW(Xword) entsz;
};



/* Backup of the real malloc function */
static void *(*realmalloc)(size_t) = NULL;


/* My local versions of the malloc functions */
static void *mymalloc(size_t size);


/*************/
/* ELF stuff */
/*************/
static const ElfW(Phdr) *get_phdr_dynamic(const ElfW(Phdr) *phdr,
        uint16_t phnum, uint16_t phentsize) {
    int i;

    for (i = 0; i < phnum; i++) {
        if (phdr->p_type == PT_DYNAMIC)
            return phdr;
        phdr = (ElfW(Phdr) *)((char *)phdr + phentsize);
    }

    return NULL;
}



static const ElfW(Dyn) *get_dynentry(ElfW(Addr) base, const ElfW(Phdr) *pdyn,
        uint32_t type) {
    ElfW(Dyn) *dyn;

    for (dyn = (ElfW(Dyn) *)(base + pdyn->p_vaddr); dyn->d_tag; dyn++) {
        if (dyn->d_tag == type)
            return dyn;
    }

    return NULL;
}



static struct jmpreltab get_jmprel(ElfW(Addr) base, const ElfW(Phdr) *pdyn) {
    struct jmpreltab table;
    const ElfW(Dyn) *dyn;

    dyn = get_dynentry(base, pdyn, DT_JMPREL);
    table.tab = (dyn == NULL) ? NULL : (ElfW(Rela) *)dyn->d_un.d_ptr;

    dyn = get_dynentry(base, pdyn, DT_PLTRELSZ);
    table.size = (dyn == NULL) ? 0 : dyn->d_un.d_val;
    return table;
}



static struct symtab get_symtab(ElfW(Addr) base, const ElfW(Phdr) *pdyn) {
    struct symtab table;
    const ElfW(Dyn) *dyn;

    dyn = get_dynentry(base, pdyn, DT_SYMTAB);
    table.tab = (dyn == NULL) ? NULL : (ElfW(Sym) *)dyn->d_un.d_ptr;
    dyn = get_dynentry(base, pdyn, DT_SYMENT);
    table.entsz = (dyn == NULL) ? 0 : dyn->d_un.d_val;
    return table;
}



static struct strtab get_strtab(ElfW(Addr) base, const ElfW(Phdr) *pdyn) {
    struct strtab table;
    const ElfW(Dyn) *dyn;

    dyn = get_dynentry(base, pdyn, DT_STRTAB);
    table.tab = (dyn == NULL) ? NULL : (char *)dyn->d_un.d_ptr;
    dyn = get_dynentry(base, pdyn, DT_STRSZ);
    table.size = (dyn == NULL) ? 0 : dyn->d_un.d_val;
    return table;
}



static void *get_got_entry(ElfW(Addr) base, struct jmpreltab jmprel,
        struct symtab symtab, struct strtab strtab, const char *symname) {

    ElfW(Rela) *rela;
    ElfW(Rela) *relaend;

    relaend = (ElfW(Rela) *)((char *)jmprel.tab + jmprel.size);
    for (rela = jmprel.tab; rela < relaend; rela++) {
        uint32_t relsymidx;
        char *relsymname;
        relsymidx = ELF64_R_SYM(rela->r_info);
        relsymname = strtab.tab + symtab.tab[relsymidx].st_name;

        if (strcmp(symname, relsymname) == 0)
            return (void *)(base + rela->r_offset);
    }

    return NULL;
}



static void patch_got(ElfW(Addr) base, const ElfW(Phdr) *phdr, int16_t phnum,
        int16_t phentsize) {

    const ElfW(Phdr) *dphdr;
    struct jmpreltab jmprel;
    struct symtab symtab;
    struct strtab strtab;
    void *(**mallocgot)(size_t);

    dphdr = get_phdr_dynamic(phdr, phnum, phentsize);
    jmprel = get_jmprel(base, dphdr);
    symtab = get_symtab(base, dphdr);
    strtab = get_strtab(base, dphdr);
    mallocgot = get_got_entry(base, jmprel, symtab, strtab, "malloc");

    /* Replace the pointer with our version. */
    if (mallocgot != NULL) {
        /* Quick & dirty hack for some programs that need it. */
        /* Should check the returned value. */
        void *page = (void *)((intptr_t)mallocgot & ~(0x1000 - 1));
        mprotect(page, 0x1000, PROT_READ | PROT_WRITE);
        *mallocgot = mymalloc;
    }
}



static int callback(struct dl_phdr_info *info, size_t size, void *data) {
    uint16_t phentsize;
    data = data;
    size = size;

    printf("Patching GOT entry of \"%s\"\n", info->dlpi_name);
    phentsize = getauxval(AT_PHENT);
    patch_got(info->dlpi_addr, info->dlpi_phdr, info->dlpi_phnum, phentsize);

    return 0;
}



/*****************/
/* Init function */
/*****************/
__attribute__((constructor)) static void init(void) {
    realmalloc = malloc;
    dl_iterate_phdr(callback, NULL);
}



/*********************************************/
/* Here come the malloc function and sisters */
/*********************************************/
static void *mymalloc(size_t size) {
    printf("hello from my malloc\n");
    return realmalloc(size);
}

还有一个示例程序,它只是在两次 malloc 调用之间加载库。

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>



void loadmymalloc(void) {
    /* Should check return value. */
    dlopen("./mymalloc.so", RTLD_LAZY);
}



int main(void) {
    void *ptr;

    ptr = malloc(42);
    printf("malloc returned: %p\n", ptr);

    loadmymalloc();

    ptr = malloc(42);
    printf("malloc returned: %p\n", ptr);

    return EXIT_SUCCESS;
}

mprotect 的调用通常是无用的。但是我发现 gvim(编译为共享对象)需要它。如果您还想将对 malloc 的引用捕获为指针(这可能允许稍后调用真正的函数并绕过您的函数),您可以对 指向的符号表应用完全相同的过程code>DT_RELA 动态入口。

如果 constructor 功能对您不可用,您所要做的就是从新加载的库中解析 init 符号并调用它。

请注意,您可能还想替换 dlopen,以便在您的库之后加载的库也得到修补。如果您很早就加载了您的库,或者如果应用程序动态加载了插件,则可能会发生这种情况。

关于c - 运行进程的重载符号(LD_PRELOAD附件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27137527/

相关文章:

c++ - 调试来自第三方库的 SIGPIPE 信号

c - 如何使用 GDB 重定向到不同的位置?

c - 免费后使用ASAN堆

linux - 我有一个虚拟转速。 rpm -qpR lin.rpm

将 void 转换为具有相同格式的结构

linux - 如何在邮件进入 spamassassin 之前将收件人列入白名单?

linux - 在两个远程主机之间复制数据 - 它是如何工作的?

c - GDB - 设置没有类型的变量

c - libiptc : adding DNAT rule with destination IP

c - 如何使用 GTK+/Cairo 旋转图像