最近调试了weston的一个coredump,对libffi有了一些了解,在此记录下,使用的是arm处理器,32位,soft float,libffi3.1,使用的abi是SYSV。
libffi简介和使用示例:http://www.atmark-techno.com/~yashi/libffi.html,建议先看完,有所了解再继续看本文。大体意思就是libffi用于高级语言之间的相互调用。由于函数指针,参数类型,参数个数,参数的值都可以在运行时指定,所以在脚本语言调用c里面用的比较多,比如python 的ctypes;也可以调用不同abi(应用程序二进制接口)编译的程序,这个了解的不多。
数据类型
libffi定义了ffi_type结构体,用于描述对应的c语言中的uint32, sint32, floate, void *, struct等类型:
typedef struct _ffi_type
{
size_t size;
unsigned short alignment;
unsigned short type;
struct _ffi_type **elements;
} ffi_type;
比如变量ffi_type_uint32用于描述c语言的uint32类型,它所占大小是4;对齐大小是4;在libffi中用于标记类型的数字是FFI_TYPE_UINT32,也就是9;elements在c语言基本类型中没有用到,固定为NULL,elements在结构体中才会用到,为结构体中的元素。
ffi_type_uint32变量是通过FFI_TYPEDEF(uint32, UINT32, FFI_TYPE_UINT32)得到的
#define FFI_TYPEDEF(name, type, id) \
struct struct_align_##name { \
char c; \
type x; \
}; \
const ffi_type ffi_type_##name = { \
sizeof(type), \
offsetof(struct struct_align_##name, x), \
id, NULL \
}
#define FFI_NONCONST_TYPEDEF(name, type, id) \
struct struct_align_##name { \
char c; \
type x; \
}; \
ffi_type ffi_type_##name = { \
sizeof(type), \
offsetof(struct struct_align_##name, x), \
id, NULL \
}
定义了struct_align_uint32结构体,这一系列结构体的第一个元素都是char,第二个是具体的uint32,sint32,void *等,用于之后求取对齐字节数。
ffi_type_uint32为ffi_type类型的const变量,sizeof(uint32)得到uint32的大小;offsetof类似于内核里面著名的container_of函数中求取结构体中元素偏移字节数的代码,可以得到uint32在struct_align_uint32中的偏移为4,表示uint32是4字节对齐的;id是FFI_TYPE_UINT32,值为9;elements为NULL。
函数调用
有了类型,下面就看函数调用,分为两步:
一、初始化ffi_cif结构体
ffi_cif结构体定义为:
typedef struct {
ffi_abi abi;
unsigned nargs;
ffi_type **arg_types;
ffi_type *rtype;
unsigned bytes;
unsigned flags;
#ifdef FFI_EXTRA_CIF_FIELDS
FFI_EXTRA_CIF_FIELDS;
#endif
} ffi_cif;
表示了函数调用中的一些信息,比如abi;输入参数个数;输入参数类型(ffi_type_uint32之类的);返回值类型;输入参数占用空间的大小(aapcs要求进入arm函数时堆栈是8字节对齐的。由于