在Linux中功能的设置,而不对dlsym [英] Function interposition in Linux without dlsym

查看:223
本文介绍了在Linux中功能的设置,而不对dlsym的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个项目,我需要跟踪的几个系统调用和低级别的功能,如 MMAP BRK使用 SBRK 。到目前为止,我一直在做这个使用功能的设置:我写的同名因为我替换( MMAP 为例)功能的包装函数,我通过设置 LD_ preLOAD 环境变量加载它的程序。我所说的真正功能通过我和则dlsym 加载一个指针。

不幸的是,我想换的功能之一, SBRK ,在内部被则dlsym 使用,所以当我尝试加载符号程序崩溃。 SBRK 不是一个系统调用在Linux中,所以我不能简单地使用系统调用来间接调用它。

我的问题是,我怎么能调用自同名的包装函数,而无需使用库函数则dlsym ?有没有什么绝招编译(使用gcc),让我指的是原有的功能?


解决方案

请参阅ld的选项 - 包装符号。从手册页:


  

- 包装符号使用包装函数符号。任何未定义
  参考符号将得到解决
  为 __ wrap_symbol 。任何未定义
  参照 __ real_symbol
  被解析为符号。


  
  

这可被用来提供一
  包装器系统的功能。该
  包装函数应被称为
   __ wrap_symbol 。如果它希望调用
  该系统的功能,它应该调用
   __ real_symbol


  
  

下面是一个简单的例子:


 无效*
__wrap_malloc(为size_t C)
{
    的printf(调用%祖\\ n的malloc,C);
    返回__real_malloc(C);
}


  

如果您链接其他code本
  文件中使用malloc的--wrap,那么所有
  调用的malloc 将调用
  函数 __ wrap_malloc 来代替。该
  在结果调用__real_malloc
   __ wrap_malloc 将调用真正的
  的malloc 的功能。


  
  

您可能希望提供一个
   __ real_malloc 功能一样,所以
  链接没有--wrap选项
  将会成功。如果你这样做,你
  不应该把定义
  在同一文件中的 __ real_malloc
   __ wrap_malloc ;如果这样做,
  汇编程序可能会解决呼叫前
  连接器有机会就换到
  malloc的。


另一种选择是可能看源ltrace,它或多或少地做同样的事情:-P。

下面是一个想法,但。你可以有你的 LD_ preLOAD 编辑库改变PLT条目指向code。这个你技术上 SBRK()功能还是从code nativly调用。

I'm currently working on a project where I need to track the usage of several system calls and low-level functions like mmap, brk, sbrk. So far, I've been doing this using function interposition: I write a wrapper function with the same name as the function I'm replacing (mmap for example), and I load it in a program by setting the LD_PRELOAD environment variable. I call the real function through a pointer that I load with dlsym.

Unfortunately, one of the functions I want to wrap, sbrk, is used internally by dlsym, so the program crashes when I try to load the symbol. sbrk is not a system call in Linux, so I can't simply use syscall to call it indirectly.

So my question is, how can I call a library function from a wrapper function of the same name without using dlsym? Is there any compiler trick (using gcc) that lets me refer to the original function?

解决方案

see ld's option --wrap symbol. From the man page:

--wrap symbol Use a wrapper function for symbol. Any undefined reference to symbol will be resolved to "__wrap_symbol". Any undefined reference to "__real_symbol" will be resolved to symbol.

This can be used to provide a wrapper for a system function. The wrapper function should be called "__wrap_symbol". If it wishes to call the system function, it should call "__real_symbol".

Here is a trivial example:

void *
__wrap_malloc (size_t c)
{
    printf ("malloc called with %zu\n", c);
    return __real_malloc (c);
}

If you link other code with this file using --wrap malloc, then all calls to "malloc" will call the function "__wrap_malloc" instead. The call to "__real_malloc" in
"__wrap_malloc" will call the real "malloc" function.

You may wish to provide a "__real_malloc" function as well, so that links without the --wrap option will succeed. If you do this, you should not put the definition of "__real_malloc" in the same file as "__wrap_malloc"; if you do, the assembler may resolve the call before the linker has a chance to wrap it to "malloc".

The other option is to possibly look at the source for ltrace, it is more or less does the same thing :-P.

Here's an idea though. You could have your LD_PRELOAD'ed library change the PLT entries to point to your code. This you technically the sbrk() function is still callable from your code nativly.

这篇关于在Linux中功能的设置,而不对dlsym的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆