文章目录
开始
在调试、追踪、反汇编定位内核函数时候经常会用到/proc/kallsyms这个接口查找当前内核有哪些可追踪的函数
$ cat /proc/kallsyms
0000000000000000 T srso_alias_untrain_ret
0000000000000000 T _stext
0000000000000000 T _text
0000000000000000 T __pi__text
0000000000000000 T __entry_text_start
0000000000000000 T x86_verw_sel
0000000000000000 T entry_SYSCALL_64
0000000000000000 T entry_SYSCALL_64_safe_stack
0000000000000000 T entry_SYSCALL_64_after_hwframe
那么,这个功能是如何实现的呢?
make
在内核的构建过程中,可以看到中间有一些*_vmlinux.kallsyms中间产物,从vmlinux中导出,再用一些方法融合在一起。
# make
......
AR vmlinux.a
LD vmlinux.o
......
LD .tmp_vmlinux.kallsyms1
NM .tmp_vmlinux.kallsyms1.syms
KSYMS .tmp_vmlinux.kallsyms1.S
AS .tmp_vmlinux.kallsyms1.o
LD .tmp_vmlinux.kallsyms2
NM .tmp_vmlinux.kallsyms2.syms
KSYMS .tmp_vmlinux.kallsyms2.S
AS .tmp_vmlinux.kallsyms2.o
LD vmlinux
.S文件中可以看到符号信息,包括地址和符号名
.tmp_vmlinux.kallsyms1.S
.globl kallsyms_offsets
ALGN
kallsyms_offsets:
.long 0 /* Afixed_percpu_data */
.long 0 /* A__per_cpu_start */
.long 0x1000 /* Acpu_debug_store */
.long 0x2000 /* Airq_stack_backing_store */
kallsyms_offsets出现在kernel/kallsyms_internal.h中
kernel/kallsyms_internal.h: 12
extern const int kallsyms_offsets[] __weak; // 与所有符号中最小地址符号的偏移量 ref: scripts/kallsyms.c: 490
检索AS .tmp_vmlinux.kallsyms,在这个脚本中出现:
scripts/link-vmlinux.sh: 172
# Perform one step in kallsyms generation, including temporary linking of
# vmlinux.
kallsyms_step()
{
kallsymso_prev=${kallsymso}
kallsyms_vmlinux=.tmp_vmlinux.kallsyms${1}
kallsymso=${kallsyms_vmlinux}.o
kallsyms_S=${kallsyms_vmlinux}.S
vmlinux_link ${kallsyms_vmlinux} "${kallsymso_prev}" ${btf_vmlinux_bin_o}
mksysmap ${kallsyms_vmlinux} ${kallsyms_vmlinux}.syms ${kallsymso_prev}
kallsyms ${kallsyms_vmlinux}.syms ${kallsyms_S}
info AS ${kallsymso}
${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \
${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
-c -o ${kallsymso} ${kallsyms_S}
}
if is_enabled CONFIG_KALLSYMS; then
# kallsyms support
# Generate section listing all symbols and add it into vmlinux
# It's a three step process:
# 1) Link .tmp_vmlinux.kallsyms1 so it has all symbols and sections,
# but __kallsyms is empty.
# Running kallsyms on that gives us .tmp_kallsyms1.o with
# the right size
# 2) Link .tmp_vmlinux.kallsyms2 so it now has a __kallsyms section of
# the right size, but due to the added section, some
# addresses have shifted.
# From here, we generate a correct .tmp_vmlinux.kallsyms2.o
# 3) That link may have expanded the kernel image enough that
# more linker branch stubs (桩) / trampolines had to be added, 有些架构跳转指令有跳转长度限制 链接器会插入跳板 which
# introduces new names, which further expands kallsyms. 再次扩大 Do another
# pass if that is the case. In theory (理论上) it's possible this results
# in even more stubs, but unlikely.
# KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
# other bugs.
# 4) The correct ${kallsymso} is linked into the final vmlinux.
#
# a) Verify that the System.map from vmlinux matches the map from
# ${kallsymso}.
kallsyms_step 1
kallsyms_step 2
# step 3 链接器插入指令导致继续扩大需要再来一遍
size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso_prev})
size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
kallsyms_step 3
fi
fi
内核在构建出vmlinux.o后,通过nm导出符号表,再生成.tmp_vmlinux.kallsyms.S文件,再和vmlinux.o一起编译生成最终的vmlinux
tmp_vmlinux.kallsyms.S 一共出现两次,分别是tmp_vmlinux.kallsyms1.S、tmp_vmlinux.kallsyms2.S,这是因为第一次时候tmp_vmlinux.kallsyms1.S内的符号类型和内容不全,大小也不对,第一次和入vmlinux后再生成一遍就带上了所有的符号(包括tmp_vmlinux.kallsyms1.S)与大小了,地址也对了。
Makefile 调用链:
Makefile: 35
targets += vmlinux
vmlinux: scripts/link-vmlinux.sh vmlinux.o $(KBUILD_LDS) FORCE
+$(call if_changed_dep,link_vmlinux)
Makefile: 1163
vmlinux: vmlinux.o $(KBUILD_LDS) modpost
$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.vmlinux
scripts/kallsyms.c
将nm命令导出的符号.tmp_vmlinux.kallsyms1.syms变成.tmp_vmlinux.kallsyms1.S的代码位于scripts/kallsyms.c
NM .tmp_vmlinux.kallsyms1.syms
KSYMS .tmp_vmlinux.kallsyms1.S
int main(int argc, char **argv)
{
......
read_map(argv[optind]); // 读取 in(.tmp_vmlinux.kallsyms.syms) 存到 table 中
shrink_table(); // 根据 --all_symbols 参数, 未指定参数时仅处理地址位于 _text _inittext 节内的符号,调整 table
if (absolute_percpu)
make_percpus_absolute(); // per_cpu 节内符号 table[i]->sym[0] = 'A'; table[i]->percpu_absolute = 1;
sort_symbols(); // 按照地址、weak、linker script provide、_ 前缀、name 排序 table
if (base_relative)
record_relative_base(); // --absolute_percpu 时(L835) per_cpu 节内符号最小相对偏移
optimize_token_table(); // 符号名压缩
write_src();
read
read_map逐行读入.tmp_vmlinux.kallsyms.syms
static void read_map(const char *in) // 读取 in 存到 table 中
{
fp = fopen(in, "r");
if (!fp) {
perror(in);
exit(1);
}
while (!feof(fp)) {
sym = read_symbol(fp, &buf, &buflen); // 读取 in 一行 -> struct sym_entry
sym->start_pos = table_cnt;
table[table_cnt++] = sym;
最终填充struct sym_entry数组table,一共table_cnt个符号
struct sym_entry {
unsigned long long addr; // 地址
unsigned int len; // len(sym) + 1('\n')
unsigned int seq; // 地址和符号名表的 idx, ref: L422
unsigned int start_pos; // 初次导入 nm 输出文件时所在 table index, ref: read_map
unsigned int percpu_absolute; // --absolute_percpu 参数时标记 per_cpu 节内符号为 1, ref: L835
unsigned char sym[]; // 符号名
};
static struct sym_entry **table; // 从 nm 结果中检索符号存储位置
读入主要包括地址、类型、符号名,更新_text、_per_cpu两个节的符号范围,读入过程:
static struct sym_entry *read_symbol(FILE *in, char **buf, size_t *buf_len) // 读取 in 一行 -> struct sym_entry
{
char *name, type, *p;
unsigned long long addr;
size_t len;
ssize_t readlen;
struct sym_entry *sym;
readlen = getline(buf, buf_len, in);
addr = strtoull(*buf, &p, 16);
if (*buf == p || *p++ != ' ' || !isascii((type = *p++)) || *p++ != ' ') {
fprintf(stderr, "line format error\n");
exit(EXIT_FAILURE);
}
name = p;
len = strlen(name);
if (strcmp(name, "_text") == 0)
_text = addr;
/* Ignore most absolute/undefined (?) symbols. */
if (is_ignored_symbol(name, type))
return NULL;
check_symbol_range(name, addr, text_ranges, ARRAY_SIZE(text_ranges)); // 根据 name 是否是 text_ranges 中的符号更新范围地址
check_symbol_range(name, addr, &percpu_range, 1);
/* include the type field in the symbol name, so that it gets
* compressed together */
len++;
sym = malloc(sizeof(*sym) + len + 1);
sym->addr = addr;
sym->len = len;
sym->sym[0] = type; // ref: https://man7.org/linux/man-pages/man1/nm.1.html
strcpy(sym_name(sym), name);
sym->percpu_absolute = 0;
return sym;
}
根据absolute_percpu选项标记_per_cpu类型符号类型为A,table[i]->percpu_absolute = 1;则是后面导出为.S时会使用,per_cpu节的地址(一定是相对地址,相对于每个cpu的基地址)会和常规符号地址(elf中是虚拟地址绝对地址)处理方式区分。
int main(int argc, char **argv)
{
if (absolute_percpu)
make_percpus_absolute(); // per_cpu 节内符号 table[i]->sym[0] = 'A'; table[i]->percpu_absolute = 1;
static void make_percpus_absolute(void)
{
unsigned int i;
for (i = 0; i < table_cnt; i++)
if (symbol_in_range(table[i], &percpu_range, 1)) {
/*
* Keep the 'A' override for percpu symbols to
* ensure consistent behavior compared to older
* versions of this tool.
*/
table[i]->sym[0] = 'A';
table[i]->percpu_absolute = 1;
}
}
压缩
kallsyms 使用自己的一套方法在scripts/kallsyms.c中压缩符号字符串,在内核使用的时候解压
int main(int argc, char **argv)
{
optimize_token_table(); // 符号名压缩
压缩分为3步,分别是统计字符频率,创建映射表,压缩:
static void optimize_token_table(void) // 符号名压缩
{
build_initial_token_table(); // 初始化 token_profit 统计相邻两字节出现频率
insert_real_symbols_in_table(); // 统计用到的字符信息到 best_table best_table_len
optimize_result(); // 符号名压缩
}
统计频率
统计相邻两个字节一起出现的频率,后面会用没用到的一字节编码(ascii编码可能没用完)替换这两个字节
/* do the initial token count */
static void build_initial_token_table(void)
{
unsigned int i;
for (i = 0; i < table_cnt; i++)
learn_symbol(table[i]->sym, table[i]->len);
}
/* count all the possible tokens in a symbol */
static void learn_symbol(const unsigned char *symbol, int len)
{
int i;
for (i = 0; i < len - 1; i++)
token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++; // 统计相邻两字节出现频率
}
接下来再统计出现过的一个字节的字符,这些字符不会字节映射
/* start by placing the symbols that are actually used on the table */
static void insert_real_symbols_in_table(void)
{
unsigned int i, j, c;
for (i = 0; i < table_cnt; i++) {
for (j = 0; j < table[i]->len; j++) {
c = table[i]->sym[j];
best_table[c][0]=c; // 统计用到的字符
best_table_len[c]=1; // 这些不会压缩 1字节就对应1字节
}
}
}
压缩
best = find_best_token();返回的是最经常使用的双字节组合,这里的循环就是把没有用到的单字节字符替换出现频率最高的双字节字符
/* this is the core of the algorithm: calculate the "best" table */
static void optimize_result(void) // 符号名压缩
{
int i, best;
/* using the '\0' symbol last allows compress_symbols to use standard
* fast string functions */
for (i = 255; i >= 0; i--) {
/* if this table slot is empty (it is not used by an actual
* original char code */
if (!best_table_len[i]) { // 该字符未使用到
/* find the token with the best profit value */
best = find_best_token(); // 找到最大使用次数的 token_profile 双字节值 ref: 559
if (token_profit[best] == 0) // 已无双字节组合
break;
/* place it in the "best" table */
best_table_len[i] = 2;
best_table[i][0] = best & 0xFF;
best_table[i][1] = (best >> 8) & 0xFF;
/* replace this token in all the valid symbols */
compress_symbols(best_table[i], i); // 根据该字符压缩字符串 再次调整 token_profit 中字符出现频率
}
}
}
/* search the token with the maximum profit */
static int find_best_token(void) // 找到最大使用次数的 token_profile
{
int i, best, bestprofit;
bestprofit=-10000;
best = 0;
for (i = 0; i < 0x10000; i++) { // optimize_result 移除后内容会变动,所以没有排序而是每次遍历
if (token_profit[i] > bestprofit) {
best = i;
bestprofit = token_profit[i];
}
}
return best;
}
每次替换一次后,还会更新字符组合频率(替换后的字符是之前没出现过的,那么和下一字符的组合一定是新的),如果新组合字符频率更新,还会发生循环替换。
/* replace a given token in all the valid symbols. Use the sampled symbols
* to update the counts */
static void compress_symbols(const unsigned char *str, int idx) // str: 高频双字节字符, idx: 字替换单字节符号,
{
unsigned int i, len, size;
unsigned char *p1, *p2;
for (i = 0; i < table_cnt; i++) {
len = table[i]->len;
p1 = table[i]->sym;
/* find the token on the symbol */
p2 = find_token(p1, len, str); // sym 中需替换双字节字符 idx
if (!p2) continue;
/* decrease the counts for this symbol's tokens */
forget_symbol(table[i]->sym, len); // 该双字节将会被替换 token_profit 计数--
size = len;
do {
*p2 = idx;
p2++;
size -= (p2 - p1); // 剩余需要搬动长度
memmove(p2, p2 + 1, size);
p1 = p2;
len--;
if (size < 2) break;
/* find the token on the symbol */
p2 = find_token(p1, size, str);
} while (p2);
table[i]->len = len;
/* increase the counts for this symbol's new tokens */
learn_symbol(table[i]->sym, len); // 经过调整后可能会再次出现字符组合计数 更新
}
}
至此,需要的内容就绪。
导出 .tmp_vmlinux.kallsyms.S
scripts/kallsyms.c导出的符号与内核使用的kernel/kallsyms_internal.h变量一致:
kernel/kallsyms_internal.h
/*
* These will be re-linked against their real values
* during the second link stage.
*/
extern const unsigned long kallsyms_addresses[] __weak; // vmlinux 中符号的地址
extern const int kallsyms_offsets[] __weak; // 与所有符号中最小地址符号的偏移量 ref: scripts/kallsyms.c: 490
extern const u8 kallsyms_names[] __weak; // 经过压缩的符号名 按照地址、weak、linker script provide、_ 前缀、name 排序
/*z
* Tell the compiler that the count isn't in the small data section if the arch
* has one (eg: FRV).
*/
extern const unsigned int kallsyms_num_syms // 所有符号数量 ref: scripts/kallsyms.c: 404
__section(".rodata") __attribute__((weak));
extern const unsigned long kallsyms_relative_base // 所有非 per_cpu 符号最小地址 ref: scripts/kallsyms.c: 531
__section(".rodata") __attribute__((weak));
extern const char kallsyms_token_table[] __weak; // 字符映射表的映射结果 ref: scripts/kallsyms.c: 472
extern const u16 kallsyms_token_index[] __weak; // kallsyms_token_table 的偏移, 用于定位字符映射结果位置 ref: scripts/kallsyms.c: 482
extern const unsigned int kallsyms_markers[] __weak; // 每 0xff 个符号名记下 offset, 用于快速访问 ref: scripts/kallsyms.c: 421
extern const u8 kallsyms_seqs_of_names[] __weak; // 按符号名排序查找 ref: scripts/kallsyms.c: 541
导出步骤:
- 如果一个符号大小小于
0x7F,符号名的第一个字节是大小,如果大于0x80,那么会用两个字节表示这个符号大小 - 符号名大小后面紧跟第一个字符是符号类型
size + type + comprss(name)
scripts/kallsyms.c
static void write_src(void)
{
unsigned int i, k, off;
unsigned int best_idx[256];
unsigned int *markers;
char buf[KSYM_NAME_LEN];
printf("#include <asm/bitsperlong.h>\n");
printf("#if BITS_PER_LONG == 64\n");
printf("#define PTR .quad\n");
printf("#define ALGN .balign 8\n");
printf("#else\n");
printf("#define PTR .long\n");
printf("#define ALGN .balign 4\n");
printf("#endif\n");
printf("\t.section .rodata, \"a\"\n");
output_label("kallsyms_num_syms");
printf("\t.long\t%u\n", table_cnt);
printf("\n");
/* table of offset markers, that give the offset in the compressed stream
* every 256 symbols */
markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
if (!markers) {
fprintf(stderr, "kallsyms failure: "
"unable to allocate required memory\n");
exit(EXIT_FAILURE);
}
output_label("kallsyms_names");
off = 0;
for (i = 0; i < table_cnt; i++) { // 输出已压缩符号表 eg: .byte 0x06, 0x41, 0x67, 0x64, 0xfc, 0x16, 0x65
if ((i & 0xFF) == 0)
markers[i >> 8] = off; // 每 0xff 个符号名记下 offset, 用于快速访问
table[i]->seq = i; // 地址和符号名表的 idx
/* There cannot be any symbol of length zero. */
if (table[i]->len == 0) {
fprintf(stderr, "kallsyms failure: "
"unexpected zero symbol length\n");
exit(EXIT_FAILURE);
}
/* Only lengths that fit in up-to-two-byte ULEB128 are supported. */
if (table[i]->len > 0x3FFF) {
fprintf(stderr, "kallsyms failure: "
"unexpected huge symbol length\n");
exit(EXIT_FAILURE);
}
/* Encode length with ULEB128. */
if (table[i]->len <= 0x7F) {
/* Most symbols use a single byte for the length. */
printf("\t.byte 0x%02x", table[i]->len);
off += table[i]->len + 1; // 1 是 len
} else {
/* "Big" symbols use two bytes. */
printf("\t.byte 0x%02x, 0x%02x",
(table[i]->len & 0x7F) | 0x80,
(table[i]->len >> 7) & 0x7F);
off += table[i]->len + 2; // 2 是 len 需要2字节表示
}
for (k = 0; k < table[i]->len; k++)
printf(", 0x%02x", table[i]->sym[k]);
printf("\n");
}
printf("\n");
/*
* Now that we wrote out the compressed symbol names, restore the
* original names, which are needed in some of the later steps.
*/
for (i = 0; i < table_cnt; i++) {
expand_symbol(table[i]->sym, table[i]->len, buf);
strcpy((char *)table[i]->sym, buf); // sym 复原成可读字符串
}
output_label("kallsyms_markers");
for (i = 0; i < ((table_cnt + 255) >> 8); i++)
printf("\t.long\t%u\n", markers[i]);
printf("\n");
free(markers);
output_label("kallsyms_token_table"); // 字符映射表的映射结果
off = 0;
for (i = 0; i < 256; i++) {
best_idx[i] = off;
expand_symbol(best_table[i], best_table_len[i], buf); // 字符映射 -> buf
printf("\t.asciz\t\"%s\"\n", buf);
off += strlen(buf) + 1;
}
printf("\n");
output_label("kallsyms_token_index"); // kallsyms_token_table 的偏移, 用于定位字符映射结果位置
for (i = 0; i < 256; i++)
printf("\t.short\t%d\n", best_idx[i]);
printf("\n");
if (!base_relative)
output_label("kallsyms_addresses");
else
output_label("kallsyms_offsets");
for (i = 0; i < table_cnt; i++) {
if (base_relative) { //
/*
* Use the offset relative to the lowest value
* encountered of all relative symbols, and emit
* non-relocatable fixed offsets that will be fixed
* up at runtime.
*/
long long offset;
int overflow;
if (!absolute_percpu) {
offset = table[i]->addr - relative_base;
overflow = (offset < 0 || offset > UINT_MAX);
} else if (symbol_absolute(table[i])) {
offset = table[i]->addr; // per_cpu 本身就是偏移量
overflow = (offset < 0 || offset > INT_MAX);
} else { // 启用了 per_cpu 节特殊处理,此时是普通符号
offset = relative_base - table[i]->addr - 1; // 用负数和 per_cpu 的地址区分
overflow = (offset < INT_MIN || offset >= 0);
}
if (overflow) {
fprintf(stderr, "kallsyms failure: "
"%s symbol value %#llx out of range in relative mode\n",
symbol_absolute(table[i]) ? "absolute" : "relative",
table[i]->addr);
exit(EXIT_FAILURE);
}
printf("\t.long\t%#x /* %s */\n", (int)offset, table[i]->sym); // eg: .long 0x15008 /* Aespfix_stack */
} else if (!symbol_absolute(table[i])) { // 不是 _per_cpu 节
output_address(table[i]->addr); // 输出相对于 _text 的地址 (+-)
} else {
printf("\tPTR\t%#llx\n", table[i]->addr);
}
}
printf("\n");
if (base_relative) { // 所有非 per_cpu 符号最小地址
output_label("kallsyms_relative_base");
output_address(relative_base);
printf("\n");
}
if (lto_clang)
for (i = 0; i < table_cnt; i++)
cleanup_symbol_name((char *)table[i]->sym); // .llvm. -> \0llvm.
sort_symbols_by_name();
output_label("kallsyms_seqs_of_names"); // 按符号名排序查找
for (i = 0; i < table_cnt; i++)
printf("\t.byte 0x%02x, 0x%02x, 0x%02x\n",
(unsigned char)(table[i]->seq >> 16), // seq 是地址和符号名表的 idx, ref: L422
(unsigned char)(table[i]->seq >> 8),
(unsigned char)(table[i]->seq >> 0));
printf("\n");
}
/proc/kallsysms
在内核中检索字符串"kallsyms"找到/proc/kallsysms创建位置:
kernel/kallsyms.c: 952
static int __init kallsyms_init(void)
{
proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops);
return 0;
}
device_initcall(kallsyms_init);
最终kallsyms解析方法放在kallsyms_op
kernel/kallsyms.c:807
static const struct seq_operations kallsyms_op = {
.start = s_start,
.next = s_next,
.stop = s_stop,
.show = s_show
};
static int kallsyms_open(struct inode *inode, struct file *file)
{
iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter));
在s_start和s_next都会调用update_iter
static void *s_next(struct seq_file *m, void *p, loff_t *pos)
{
(*pos)++;
if (!update_iter(m->private, *pos)) // 获取 *pos 处信息,为下一位置准备
return NULL;
return p;
}
static void *s_start(struct seq_file *m, loff_t *pos)
{
if (!update_iter(m->private, *pos)) // 获取 *pos 处信息,为下一位置准备
return NULL;
return m->private;
}
update_iter -> get_ksymbol_core,提取符号地址、类型、符号名
6.6 内核获取 type 没考虑到 len 可能占 2 字节,可惜 bug 被华为2025年修复了: https://github.com/torvalds/linux/commit/f3f9f42232dee596d15491ca3f611d02174db49c
/* Returns false if pos at or past end of file. */
static int update_iter(struct kallsym_iter *iter, loff_t pos) // 获取 pos 处信息,为下一位置准备
{
iter->nameoff += get_ksymbol_core(iter); // 获取 iter 处的 type, name, 返回当前 iter 的信息长度,使得 nameoff 指向下一个
iter->pos++; // 指向下一位置
/* Returns space to next name. */
static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
{
unsigned off = iter->nameoff;
iter->value = kallsyms_sym_address(iter->pos); // 根据 idx 和 config 配置返回对应地址 per_cpu 相对偏移地址或符号 vmlinux 中的地址,正数, per_cpu 的偏移量,复数根据最小符号地址转成 vmlinux 中的地址
iter->type = kallsyms_get_symbol_type(off); // name_offset 的第一个字母 eg: T
off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name)); // off 处指向的压缩符号名解压到 result
return off - iter->nameoff;
}
提取符号名的步骤基本是scripts/kallsyms.c中static void write_src(void)方法逆序
/*
* Expand a compressed symbol data into the resulting uncompressed string,
* if uncompressed string is too long (>= maxlen), it will be truncated,
* given the offset to where the symbol is in the compressed stream.
*/
static unsigned int kallsyms_expand_symbol(unsigned int off,
char *result, size_t maxlen) // off 处指向的压缩符号名解压到 result
{
int len, skipped_first = 0;
const char *tptr;
const u8 *data;
/* Get the compressed symbol length from the first symbol byte. */
data = &kallsyms_names[off];
len = *data;
data++;
off++;
/* If MSB is 1, it is a "big" symbol, so needs an additional byte. */
if ((len & 0x80) != 0) {
len = (len & 0x7F) | (*data << 7);
data++;
off++;
}
/*
* Update the offset to return the offset for the next symbol on
* the compressed stream.
*/
off += len;
/*
* For every byte on the compressed symbol data, copy the table
* entry for that byte.
*/
while (len) {
tptr = &kallsyms_token_table[kallsyms_token_index[*data]];
data++;
len--;
while (*tptr) {
if (skipped_first) {
if (maxlen <= 1)
goto tail;
*result = *tptr;
result++;
maxlen--;
} else
skipped_first = 1;
tptr++;
}
}
tail:
if (maxlen)
*result = '\0';
/* Return to offset to the next symbol. */
return off;
}
接下来在s_show中输出结果,至此结束。
static int s_show(struct seq_file *m, void *p)
{
seq_printf(m, "%px %c %s\n", value,
iter->type, iter->name);