是否有可能得到一个函数的签名在共享库编程? [英] Is it possible to get the signature of a function in a shared library programatically?

查看:102
本文介绍了是否有可能得到一个函数的签名在共享库编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题是明确的,我们可以通过加载库 dl_open

The title is clear, we can loaded a library by dl_open etc..

但是,我怎么能得到的函数签名的吗?

But how can I get the signature of functions in it?

推荐答案

这答案不能被一般的回答。从技术上讲,如果你编译详尽的调试信息(code可能仍然是一个优化,发布版本)的可执行文件,那么可执行文件将包含额外的部分,提供了一些二进制的反射率。在* nix系统(您简称 dl_open ),这是通过的 ELF 二进制的额外部分>矮调试数据。类似它适合的马赫通用二进制代码的MacOS X上。

This answer cannot be answered in general. Technically if you compiled your executable with exhaustive debugging information (code may still be an optimized, release version), then the executable will contain extra sections, providing some kind of reflectivity of the binary. On *nix systems (you referred to dl_open) this is implemented through DWARF debugging data in extra sections of the ELF binary. Similar it works for Mach Universal Binaries on MacOS X.

的Windows PE的使用完全不同的格式,所以很遗憾矮人不是我的3D引擎,我实现了适用于Windows的ELF / DWARF装载机的早期开发阶段truley跨plattform(实际上,这样我可以使用的通用格式发动机各个模块,所以用一些严重的努力,这样可以做到)。

Windows PEs however uses a completely different format, so unfortunately DWARF is not truley cross plattform (actually in the early development stages of my 3D engine I implemented an ELF/DWARF loader for Windows, so that I could use a common format for the engines various modules, so with some serious effort such can be done).

如果你不想进入实现自己的装载机,或调试信息访问,那么你可以通过嵌入出口一些额外的符号,它是指函数名称表的反射信息(由一些标准的命名方案)映射到他们的签名。在C源文件编写解析器来提取源文件本身的信息的情况下是相当琐碎。 C ++ OTOH是那么非常难以正确解析,你需要一些羽翼丰满的编译器得到它的权利。为此GCCXML的开发,在技术上发出XML格式,而不是一个对象二进制AST一个GCC。然后所发射的XML是更容易分析

If you don't want to go into implementing your own loaders, or debugging information accessors, then you may embed the reflection information through some extra symbols exported (by some standard naming scheme) which refer to a table of function names, mapping to their signature. In the case of C source files writing a parser to extract the information from the source file itself is rather trivial. C++ OTOH is so notoriously difficult to parse correctly, that you need some fully fledged compiler to get it right. For this purpose GCCXML was developed, technically a GCC that emits the AST in XML form instead of an object binary. The emitted XML then is much easier to parse.

从提取的信息创建某种链表/阵列/等的源文件。结构描述每个功能。如果你不直接导出每个函数的符号,而是初始化一些领域中的反射结构的函数指针,你有一个非常漂亮,干净注明的出口计划。从技术上讲,你可以把二进制的spearate部分信息为好,但把它的只读数据部分做这项工作为好,太。

From the extracted information create a source file with some kind of linked list/array/etc. structure describing each function. If you don't directly export each function's symbol but instead initialize some field in the reflection structure with the function pointer you got a really nice and clean annotated exporting scheme. Technically you could place this information in a spearate section of the binary as well, but putting it in the read only data section does the job as well, too.

不过,如果你是给予第三方二​​进制 - 说最坏的情况已经从C源代码,没有调试信息,而不是外部引用删除所有符号编译 - 你是pretty多拧。你能做的最好的,正在申请的功能,在访问哪些参数可以通过各个地方的路上一些二进制分析。

However if you're given a 3rd party binary – say worst case scenario it has been compiled from C source, no debugging information and all symbols not externally referenced stripped – you're pretty much screwed. The best you could do, was applying some binary analysis of the way the function accesses the various places in which parameters can be passed.

此将只告诉你的参数的数量和每一个参数值的大小,但不是类型或名称/含义。当逆向工程某些程序(例如恶意软件分析和安全审计),确定传递给函数的参数的类型和意义是主要努力之一。最近我遇到了一些司机,我不得不反向调试的目的,你可以不相信我是多么震惊被我发现一个Linux内核模块的C ++符号(在Linux内核在一个健全的方式,你不能使用C ++的事实),但也松了口气,因为C ++名称重整用大量的信息提供给我。

This will only tell you the number of parameters and the size of each parameter value, but not the type or name/meaning. When reverse engineering some program (e.g. malware analysis or security audit), identifying the type and meaning of the parameters passed to functions is one of the major efforts. Recently I came across some driver I had to reverse for debugging purposes, and you cannot believe how astounded I was by the fact that I found C++ symbols in a Linux kernel module (you can't use C++ in the Linux kernel in a sane way), but also relieved, because the C++ name mangling provided me with plenty information.

这篇关于是否有可能得到一个函数的签名在共享库编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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