当前位置:   article > 正文

Linux内核未导出符号使用方法_unknown symbol sched_setscheduler (err -2)

unknown symbol sched_setscheduler (err -2)
Linux内核未导出符号使用方法
一、背景

在写内核驱动程序的过程中,往往可以看到一些内核并没有被导出,而刚好在做开发的过程中需要用到,这个时候就要想办法使用内核未导出函数,这里简单分享下怎么处理这些函数,前提是在符号表中可以找到对应函数的地址。

二、实例分析涉及函数

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

想要使用而未导出的内核函数__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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
三、代码实现
//先声明函数原型,返回值类型为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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/183424
推荐阅读
相关标签
  

闽ICP备14008679号