赞
踩
在写内核驱动程序的过程中,往往可以看到一些内核并没有被导出,而刚好在做开发的过程中需要用到,这个时候就要想办法使用内核未导出函数,这里简单分享下怎么处理这些函数,前提是在符号表中可以找到对应函数的地址。
kallsyms_lookup_name()
//内核函数原型,为导出函数,用于找到输入函数名所对应的函数入口地址 #include <linux/kallsyms.h> /* Lookup the address for this symbol. Returns 0 if not found. */ unsigned long kallsyms_lookup_name(const char *name) { char namebuf[KSYM_NAME_LEN]; unsigned long i; unsigned int off; for (i = 0, off = 0; i < kallsyms_num_syms; i++) { off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); if (strcmp(namebuf, name) == 0) return kallsyms_sym_address(i); } return module_kallsyms_lookup_name(name); } EXPORT_SYMBOL_GPL(kallsyms_lookup_name);
想要使用而未导出的内核函数__lookup_mnt,根据传入vfsmount和dentry结构体返回struct mount;
/*
* find the first mount at @dentry on vfsmount @mnt.
* call under rcu_read_lock()
*/
struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
{
struct hlist_head *head = m_hash(mnt, dentry);
struct mount *p;
hlist_for_each_entry_rcu(p, head, mnt_hash)
if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
return p;
return NULL;
}
//先声明函数原型,返回值类型为struct mount *的函数指针
struct mount *(*__lookup_mnt_p)(struct vfsmount *mnt, struct dentry *dentry);
//使用kallsyms_lookup_name()函数找到函数入口地址并赋值给自定义的函数指针
__lookup_mnt_p = (struct mount *(*)(struct vfsmount *, struct dentry *))kallsyms_lookup_name("__lookup_mnt");
//通过上面两个步骤就可以正常使用这个未导出函数
struct path *path = NULL;
struct mount *mount = NULL;
mount = __lookup_mnt_p(path->mnt, path->dentry);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。