blob: 3e7e2c2ad2f75ef603c32750e94c7afe5fdef994 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * kallsyms.c: in-kernel printing of symbolic oopses and stack traces.
4 *
5 * Rewritten and vastly simplified by Rusty Russell for in-kernel
6 * module loader:
7 * Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
8 *
9 * ChangeLog:
10 *
11 * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com>
12 * Changed the compression method from stem compression to "table lookup"
13 * compression (see scripts/kallsyms.c for a more complete description)
14 */
15#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
17#include <linux/seq_file.h>
18#include <linux/fs.h>
Jason Wessel67fc4e02010-05-20 21:04:21 -050019#include <linux/kdb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/err.h>
21#include <linux/proc_fs.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080022#include <linux/sched.h> /* for cond_resched */
Adam B. Jerome07354a02006-12-06 20:35:30 -080023#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Daniel Borkmann74451e662017-02-16 22:24:50 +010025#include <linux/filter.h>
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -040026#include <linux/ftrace.h>
Adrian Hunterd002b8b2020-05-28 11:00:58 +030027#include <linux/kprobes.h>
Stephen Boyd92945232021-07-07 18:09:20 -070028#include <linux/build_bug.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070029#include <linux/compiler.h>
Stephen Boyd92945232021-07-07 18:09:20 -070030#include <linux/module.h>
31#include <linux/kernel.h>
Jiri Olsabed0d9a2022-05-10 14:26:13 +020032#include <linux/bsearch.h>
Alan Maguire647cafa2022-07-12 13:31:44 +010033#include <linux/btf_ids.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Stephen Brennan71f8c152022-05-16 17:05:07 -070035#include "kallsyms_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Manish Katiyarad6ccfa2009-05-12 13:43:35 -070037/*
38 * Expand a compressed symbol data into the resulting uncompressed string,
Chen Gange3f26752013-04-15 15:04:43 +093039 * if uncompressed string is too long (>= maxlen), it will be truncated,
Manish Katiyarad6ccfa2009-05-12 13:43:35 -070040 * given the offset to where the symbol is in the compressed stream.
41 */
Chen Gange3f26752013-04-15 15:04:43 +093042static unsigned int kallsyms_expand_symbol(unsigned int off,
43 char *result, size_t maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 int len, skipped_first = 0;
Masahiro Yamadacde26a62020-02-02 14:09:22 +090046 const char *tptr;
47 const u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Manish Katiyarad6ccfa2009-05-12 13:43:35 -070049 /* Get the compressed symbol length from the first symbol byte. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 data = &kallsyms_names[off];
51 len = *data;
52 data++;
53
Manish Katiyarad6ccfa2009-05-12 13:43:35 -070054 /*
55 * Update the offset to return the offset for the next symbol on
56 * the compressed stream.
57 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 off += len + 1;
59
Manish Katiyarad6ccfa2009-05-12 13:43:35 -070060 /*
61 * For every byte on the compressed symbol data, copy the table
62 * entry for that byte.
63 */
64 while (len) {
65 tptr = &kallsyms_token_table[kallsyms_token_index[*data]];
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 data++;
67 len--;
68
69 while (*tptr) {
Manish Katiyarad6ccfa2009-05-12 13:43:35 -070070 if (skipped_first) {
Chen Gange3f26752013-04-15 15:04:43 +093071 if (maxlen <= 1)
72 goto tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *result = *tptr;
74 result++;
Chen Gange3f26752013-04-15 15:04:43 +093075 maxlen--;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 } else
77 skipped_first = 1;
78 tptr++;
79 }
80 }
81
Chen Gange3f26752013-04-15 15:04:43 +093082tail:
83 if (maxlen)
84 *result = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Manish Katiyarad6ccfa2009-05-12 13:43:35 -070086 /* Return to offset to the next symbol. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return off;
88}
89
Manish Katiyarad6ccfa2009-05-12 13:43:35 -070090/*
91 * Get symbol type information. This is encoded as a single char at the
92 * beginning of the symbol name.
93 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static char kallsyms_get_symbol_type(unsigned int off)
95{
Manish Katiyarad6ccfa2009-05-12 13:43:35 -070096 /*
97 * Get just the first code, look it up in the token table,
98 * and return the first char from this token.
99 */
100 return kallsyms_token_table[kallsyms_token_index[kallsyms_names[off + 1]]];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700104/*
105 * Find the offset on the compressed stream given and index in the
106 * kallsyms array.
107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static unsigned int get_symbol_offset(unsigned long pos)
109{
Jan Beulichaad09472006-12-08 02:35:57 -0800110 const u8 *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int i;
112
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700113 /*
114 * Use the closest marker we have. We have markers every 256 positions,
115 * so that should be close enough.
116 */
117 name = &kallsyms_names[kallsyms_markers[pos >> 8]];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700119 /*
120 * Sequentially scan all the symbols up to the point we're searching
121 * for. Every symbol is stored in a [<len>][<len> bytes of data] format,
122 * so we just need to add the len to the current pointer for every
123 * symbol we wish to skip.
124 */
125 for (i = 0; i < (pos & 0xFF); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 name = name + (*name) + 1;
127
128 return name - kallsyms_names;
129}
130
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700131static unsigned long kallsyms_sym_address(int idx)
132{
133 if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
134 return kallsyms_addresses[idx];
135
136 /* values are unsigned offsets if --absolute-percpu is not in effect */
137 if (!IS_ENABLED(CONFIG_KALLSYMS_ABSOLUTE_PERCPU))
138 return kallsyms_relative_base + (u32)kallsyms_offsets[idx];
139
140 /* ...otherwise, positive offsets are absolute values */
141 if (kallsyms_offsets[idx] >= 0)
142 return kallsyms_offsets[idx];
143
144 /* ...and negative offsets are relative to kallsyms_relative_base - 1 */
145 return kallsyms_relative_base - 1 - kallsyms_offsets[idx];
146}
147
Nick Desaulniers6eb4bd92021-10-04 09:29:33 -0700148static bool cleanup_symbol_name(char *s)
Sami Tolvanen8b8e6b52021-04-08 11:28:32 -0700149{
150 char *res;
151
Nick Desaulniers6eb4bd92021-10-04 09:29:33 -0700152 if (!IS_ENABLED(CONFIG_LTO_CLANG))
153 return false;
Sami Tolvanen8b8e6b52021-04-08 11:28:32 -0700154
Nick Desaulniers6eb4bd92021-10-04 09:29:33 -0700155 /*
156 * LLVM appends various suffixes for local functions and variables that
157 * must be promoted to global scope as part of LTO. This can break
158 * hooking of static functions with kprobes. '.' is not a valid
159 * character in an identifier in C. Suffixes observed:
160 * - foo.llvm.[0-9a-f]+
161 * - foo.[0-9a-f]+
162 * - foo.[0-9a-f]+.cfi_jt
163 */
164 res = strchr(s, '.');
165 if (res) {
166 *res = '\0';
167 return true;
168 }
169
170 if (!IS_ENABLED(CONFIG_CFI_CLANG) ||
171 !IS_ENABLED(CONFIG_LTO_CLANG_THIN) ||
172 CONFIG_CLANG_VERSION >= 130000)
173 return false;
174
175 /*
176 * Prior to LLVM 13, the following suffixes were observed when thinLTO
177 * and CFI are both enabled:
178 * - foo$[0-9]+
179 */
180 res = strrchr(s, '$');
181 if (res) {
182 *res = '\0';
183 return true;
184 }
185
186 return false;
Sami Tolvanen8b8e6b52021-04-08 11:28:32 -0700187}
Sami Tolvanen8b8e6b52021-04-08 11:28:32 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/* Lookup the address for this symbol. Returns 0 if not found. */
190unsigned long kallsyms_lookup_name(const char *name)
191{
Tejun Heo9281acea2007-07-17 04:03:51 -0700192 char namebuf[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 unsigned long i;
194 unsigned int off;
195
Jiri Olsaaecf4892022-03-16 13:24:08 +0100196 /* Skip the search for empty string. */
197 if (!*name)
198 return 0;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
Chen Gange3f26752013-04-15 15:04:43 +0930201 off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 if (strcmp(namebuf, name) == 0)
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700204 return kallsyms_sym_address(i);
Sami Tolvanen8b8e6b52021-04-08 11:28:32 -0700205
206 if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
207 return kallsyms_sym_address(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209 return module_kallsyms_lookup_name(name);
210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Christoph Hellwig013c1662021-02-02 13:13:26 +0100212/*
213 * Iterate over all symbols in vmlinux. For symbols from modules use
214 * module_kallsyms_on_each_symbol instead.
215 */
Anders Kaseorg75a66612008-12-05 19:03:58 -0500216int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
217 unsigned long),
218 void *data)
219{
220 char namebuf[KSYM_NAME_LEN];
221 unsigned long i;
222 unsigned int off;
223 int ret;
224
225 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
Chen Gange3f26752013-04-15 15:04:43 +0930226 off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700227 ret = fn(data, namebuf, NULL, kallsyms_sym_address(i));
Anders Kaseorg75a66612008-12-05 19:03:58 -0500228 if (ret != 0)
229 return ret;
David Vernetf5bdb342021-12-29 13:56:47 -0800230 cond_resched();
Anders Kaseorg75a66612008-12-05 19:03:58 -0500231 }
Christoph Hellwig013c1662021-02-02 13:13:26 +0100232 return 0;
Anders Kaseorg75a66612008-12-05 19:03:58 -0500233}
Anders Kaseorg75a66612008-12-05 19:03:58 -0500234
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700235static unsigned long get_symbol_pos(unsigned long addr,
236 unsigned long *symbolsize,
237 unsigned long *offset)
238{
239 unsigned long symbol_start = 0, symbol_end = 0;
240 unsigned long i, low, high, mid;
241
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100242 /* This kernel should never had been booted. */
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700243 if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
244 BUG_ON(!kallsyms_addresses);
245 else
246 BUG_ON(!kallsyms_offsets);
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100247
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700248 /* Do a binary search on the sorted kallsyms_addresses array. */
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700249 low = 0;
250 high = kallsyms_num_syms;
251
252 while (high - low > 1) {
Vegard Nossum2fc9c4e2008-07-25 01:45:34 -0700253 mid = low + (high - low) / 2;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700254 if (kallsyms_sym_address(mid) <= addr)
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700255 low = mid;
256 else
257 high = mid;
258 }
259
260 /*
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700261 * Search for the first aliased symbol. Aliased
262 * symbols are symbols with the same address.
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700263 */
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700264 while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low))
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700265 --low;
266
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700267 symbol_start = kallsyms_sym_address(low);
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700268
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700269 /* Search for next non-aliased symbol. */
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700270 for (i = low + 1; i < kallsyms_num_syms; i++) {
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700271 if (kallsyms_sym_address(i) > symbol_start) {
272 symbol_end = kallsyms_sym_address(i);
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700273 break;
274 }
275 }
276
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700277 /* If we found no next symbol, we use the end of the section. */
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700278 if (!symbol_end) {
279 if (is_kernel_inittext(addr))
280 symbol_end = (unsigned long)_einittext;
Masahiro Yamada63b23e22017-07-10 15:51:20 -0700281 else if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700282 symbol_end = (unsigned long)_end;
283 else
284 symbol_end = (unsigned long)_etext;
285 }
286
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700287 if (symbolsize)
288 *symbolsize = symbol_end - symbol_start;
289 if (offset)
290 *offset = addr - symbol_start;
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700291
292 return low;
293}
294
295/*
296 * Lookup an address but don't bother to find any names.
297 */
298int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
299 unsigned long *offset)
300{
Rusty Russell6dd06c92008-01-29 17:13:22 -0500301 char namebuf[KSYM_NAME_LEN];
Daniel Borkmann74451e662017-02-16 22:24:50 +0100302
Marc Zyngier2a1a3fa2019-08-24 14:12:31 +0100303 if (is_ksym_addr(addr)) {
304 get_symbol_pos(addr, symbolsize, offset);
305 return 1;
306 }
Stephen Boyd92945232021-07-07 18:09:20 -0700307 return !!module_address_lookup(addr, symbolsize, offset, NULL, NULL, namebuf) ||
Daniel Borkmann74451e662017-02-16 22:24:50 +0100308 !!__bpf_address_lookup(addr, symbolsize, offset, namebuf);
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700309}
310
Stephen Boyd92945232021-07-07 18:09:20 -0700311static const char *kallsyms_lookup_buildid(unsigned long addr,
312 unsigned long *symbolsize,
313 unsigned long *offset, char **modname,
314 const unsigned char **modbuildid, char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100316 const char *ret;
317
Tejun Heo9281acea2007-07-17 04:03:51 -0700318 namebuf[KSYM_NAME_LEN - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 namebuf[0] = 0;
320
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700321 if (is_ksym_addr(addr)) {
322 unsigned long pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700324 pos = get_symbol_pos(addr, symbolsize, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* Grab name */
Chen Gange3f26752013-04-15 15:04:43 +0930326 kallsyms_expand_symbol(get_symbol_offset(pos),
327 namebuf, KSYM_NAME_LEN);
Kyle McMartin7a74fc42007-05-30 02:43:16 -0400328 if (modname)
329 *modname = NULL;
Stephen Boyd92945232021-07-07 18:09:20 -0700330 if (modbuildid)
331 *modbuildid = NULL;
Sami Tolvanen8b8e6b52021-04-08 11:28:32 -0700332
333 ret = namebuf;
334 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
Daniel Borkmann74451e662017-02-16 22:24:50 +0100337 /* See if it's in a module or a BPF JITed image. */
338 ret = module_address_lookup(addr, symbolsize, offset,
Stephen Boyd92945232021-07-07 18:09:20 -0700339 modname, modbuildid, namebuf);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100340 if (!ret)
341 ret = bpf_address_lookup(addr, symbolsize,
342 offset, modname, namebuf);
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -0400343
344 if (!ret)
345 ret = ftrace_mod_address_lookup(addr, symbolsize,
346 offset, modname, namebuf);
Sami Tolvanen8b8e6b52021-04-08 11:28:32 -0700347
348found:
349 cleanup_symbol_name(namebuf);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100350 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Stephen Boyd92945232021-07-07 18:09:20 -0700353/*
354 * Lookup an address
355 * - modname is set to NULL if it's in the kernel.
356 * - We guarantee that the returned name is valid until we reschedule even if.
357 * It resides in a module.
358 * - We also guarantee that modname will be valid until rescheduled.
359 */
360const char *kallsyms_lookup(unsigned long addr,
361 unsigned long *symbolsize,
362 unsigned long *offset,
363 char **modname, char *namebuf)
364{
365 return kallsyms_lookup_buildid(addr, symbolsize, offset, modname,
366 NULL, namebuf);
367}
368
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700369int lookup_symbol_name(unsigned long addr, char *symname)
370{
Sami Tolvanen8b8e6b52021-04-08 11:28:32 -0700371 int res;
372
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700373 symname[0] = '\0';
Tejun Heo9281acea2007-07-17 04:03:51 -0700374 symname[KSYM_NAME_LEN - 1] = '\0';
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700375
376 if (is_ksym_addr(addr)) {
377 unsigned long pos;
378
379 pos = get_symbol_pos(addr, NULL, NULL);
380 /* Grab name */
Chen Gange3f26752013-04-15 15:04:43 +0930381 kallsyms_expand_symbol(get_symbol_offset(pos),
382 symname, KSYM_NAME_LEN);
Sami Tolvanen8b8e6b52021-04-08 11:28:32 -0700383 goto found;
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700384 }
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700385 /* See if it's in a module. */
Sami Tolvanen8b8e6b52021-04-08 11:28:32 -0700386 res = lookup_module_symbol_name(addr, symname);
387 if (res)
388 return res;
389
390found:
391 cleanup_symbol_name(symname);
392 return 0;
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700393}
394
Alexey Dobriyana5c43da2007-05-08 00:28:47 -0700395int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
396 unsigned long *offset, char *modname, char *name)
397{
Sami Tolvanen8b8e6b52021-04-08 11:28:32 -0700398 int res;
399
Alexey Dobriyana5c43da2007-05-08 00:28:47 -0700400 name[0] = '\0';
Tejun Heo9281acea2007-07-17 04:03:51 -0700401 name[KSYM_NAME_LEN - 1] = '\0';
Alexey Dobriyana5c43da2007-05-08 00:28:47 -0700402
403 if (is_ksym_addr(addr)) {
404 unsigned long pos;
405
406 pos = get_symbol_pos(addr, size, offset);
407 /* Grab name */
Chen Gange3f26752013-04-15 15:04:43 +0930408 kallsyms_expand_symbol(get_symbol_offset(pos),
409 name, KSYM_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -0700410 modname[0] = '\0';
Sami Tolvanen8b8e6b52021-04-08 11:28:32 -0700411 goto found;
Alexey Dobriyana5c43da2007-05-08 00:28:47 -0700412 }
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700413 /* See if it's in a module. */
Sami Tolvanen8b8e6b52021-04-08 11:28:32 -0700414 res = lookup_module_symbol_attrs(addr, size, offset, modname, name);
415 if (res)
416 return res;
417
418found:
419 cleanup_symbol_name(name);
420 return 0;
Alexey Dobriyana5c43da2007-05-08 00:28:47 -0700421}
422
Robert Peterson42e38082007-04-30 15:09:48 -0700423/* Look up a kernel symbol and return it in a text buffer. */
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900424static int __sprint_symbol(char *buffer, unsigned long address,
Stephen Boyd92945232021-07-07 18:09:20 -0700425 int symbol_offset, int add_offset, int add_buildid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 char *modname;
Stephen Boyd92945232021-07-07 18:09:20 -0700428 const unsigned char *buildid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 const char *name;
430 unsigned long offset, size;
Hugh Dickins966c8c12008-11-19 15:36:36 -0800431 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900433 address += symbol_offset;
Stephen Boyd92945232021-07-07 18:09:20 -0700434 name = kallsyms_lookup_buildid(address, &size, &offset, &modname, &buildid,
435 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!name)
Namhyung Kimb86280a2014-08-08 14:19:41 -0700437 return sprintf(buffer, "0x%lx", address - symbol_offset);
Andrew Morton19769b72007-07-15 23:41:24 -0700438
Hugh Dickins966c8c12008-11-19 15:36:36 -0800439 if (name != buffer)
440 strcpy(buffer, name);
441 len = strlen(buffer);
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900442 offset -= symbol_offset;
Hugh Dickins966c8c12008-11-19 15:36:36 -0800443
Stephen Boyd4796dd22012-05-29 15:07:33 -0700444 if (add_offset)
445 len += sprintf(buffer + len, "+%#lx/%#lx", offset, size);
446
Stephen Boyd92945232021-07-07 18:09:20 -0700447 if (modname) {
448 len += sprintf(buffer + len, " [%s", modname);
449#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
450 if (add_buildid && buildid) {
451 /* build ID should match length of sprintf */
452#if IS_ENABLED(CONFIG_MODULES)
453 static_assert(sizeof(typeof_member(struct module, build_id)) == 20);
454#endif
455 len += sprintf(buffer + len, " %20phN", buildid);
456 }
457#endif
458 len += sprintf(buffer + len, "]");
459 }
Hugh Dickins966c8c12008-11-19 15:36:36 -0800460
461 return len;
Robert Peterson42e38082007-04-30 15:09:48 -0700462}
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900463
464/**
465 * sprint_symbol - Look up a kernel symbol and return it in a text buffer
466 * @buffer: buffer to be stored
467 * @address: address to lookup
468 *
469 * This function looks up a kernel symbol with @address and stores its name,
470 * offset, size and module name to @buffer if possible. If no symbol was found,
471 * just saves its @address as is.
472 *
473 * This function returns the number of bytes stored in @buffer.
474 */
475int sprint_symbol(char *buffer, unsigned long address)
476{
Stephen Boyd92945232021-07-07 18:09:20 -0700477 return __sprint_symbol(buffer, address, 0, 1, 0);
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900478}
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700479EXPORT_SYMBOL_GPL(sprint_symbol);
Robert Peterson42e38082007-04-30 15:09:48 -0700480
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900481/**
Stephen Boyd92945232021-07-07 18:09:20 -0700482 * sprint_symbol_build_id - Look up a kernel symbol and return it in a text buffer
483 * @buffer: buffer to be stored
484 * @address: address to lookup
485 *
486 * This function looks up a kernel symbol with @address and stores its name,
487 * offset, size, module name and module build ID to @buffer if possible. If no
488 * symbol was found, just saves its @address as is.
489 *
490 * This function returns the number of bytes stored in @buffer.
491 */
492int sprint_symbol_build_id(char *buffer, unsigned long address)
493{
494 return __sprint_symbol(buffer, address, 0, 1, 1);
495}
496EXPORT_SYMBOL_GPL(sprint_symbol_build_id);
497
498/**
Stephen Boyd4796dd22012-05-29 15:07:33 -0700499 * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer
500 * @buffer: buffer to be stored
501 * @address: address to lookup
502 *
503 * This function looks up a kernel symbol with @address and stores its name
504 * and module name to @buffer if possible. If no symbol was found, just saves
505 * its @address as is.
506 *
507 * This function returns the number of bytes stored in @buffer.
508 */
509int sprint_symbol_no_offset(char *buffer, unsigned long address)
510{
Stephen Boyd92945232021-07-07 18:09:20 -0700511 return __sprint_symbol(buffer, address, 0, 0, 0);
Stephen Boyd4796dd22012-05-29 15:07:33 -0700512}
513EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);
514
515/**
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900516 * sprint_backtrace - Look up a backtrace symbol and return it in a text buffer
517 * @buffer: buffer to be stored
518 * @address: address to lookup
519 *
520 * This function is for stack backtrace and does the same thing as
521 * sprint_symbol() but with modified/decreased @address. If there is a
522 * tail-call to the function marked "noreturn", gcc optimized out code after
523 * the call so that the stack-saved return address could point outside of the
524 * caller. This function ensures that kallsyms will find the original caller
525 * by decreasing @address.
526 *
527 * This function returns the number of bytes stored in @buffer.
528 */
529int sprint_backtrace(char *buffer, unsigned long address)
530{
Stephen Boyd92945232021-07-07 18:09:20 -0700531 return __sprint_symbol(buffer, address, -1, 1, 0);
532}
533
534/**
535 * sprint_backtrace_build_id - Look up a backtrace symbol and return it in a text buffer
536 * @buffer: buffer to be stored
537 * @address: address to lookup
538 *
539 * This function is for stack backtrace and does the same thing as
540 * sprint_symbol() but with modified/decreased @address. If there is a
541 * tail-call to the function marked "noreturn", gcc optimized out code after
542 * the call so that the stack-saved return address could point outside of the
543 * caller. This function ensures that kallsyms will find the original caller
544 * by decreasing @address. This function also appends the module build ID to
545 * the @buffer if @address is within a kernel module.
546 *
547 * This function returns the number of bytes stored in @buffer.
548 */
549int sprint_backtrace_build_id(char *buffer, unsigned long address)
550{
551 return __sprint_symbol(buffer, address, -1, 1, 1);
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700555struct kallsym_iter {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 loff_t pos;
Alexander Shishkind83212d2018-06-06 15:54:10 +0300557 loff_t pos_arch_end;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100558 loff_t pos_mod_end;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -0400559 loff_t pos_ftrace_mod_end;
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300560 loff_t pos_bpf_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 unsigned long value;
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700562 unsigned int nameoff; /* If iterating in core kernel symbols. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 char type;
Tejun Heo9281acea2007-07-17 04:03:51 -0700564 char name[KSYM_NAME_LEN];
565 char module_name[MODULE_NAME_LEN];
Alexey Dobriyanea078902007-05-08 00:28:39 -0700566 int exported;
Linus Torvaldsc0f3ea12017-11-08 12:51:04 -0800567 int show_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568};
569
Alexander Shishkind83212d2018-06-06 15:54:10 +0300570int __weak arch_get_kallsym(unsigned int symnum, unsigned long *value,
571 char *type, char *name)
572{
573 return -EINVAL;
574}
575
576static int get_ksymbol_arch(struct kallsym_iter *iter)
577{
578 int ret = arch_get_kallsym(iter->pos - kallsyms_num_syms,
579 &iter->value, &iter->type,
580 iter->name);
581
582 if (ret < 0) {
583 iter->pos_arch_end = iter->pos;
584 return 0;
585 }
586
587 return 1;
588}
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590static int get_ksymbol_mod(struct kallsym_iter *iter)
591{
Alexander Shishkind83212d2018-06-06 15:54:10 +0300592 int ret = module_get_kallsym(iter->pos - iter->pos_arch_end,
Daniel Borkmann74451e662017-02-16 22:24:50 +0100593 &iter->value, &iter->type,
594 iter->name, iter->module_name,
595 &iter->exported);
596 if (ret < 0) {
597 iter->pos_mod_end = iter->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 0;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100599 }
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 1;
602}
603
Adrian Hunterfc0ea792020-05-12 15:19:13 +0300604/*
605 * ftrace_mod_get_kallsym() may also get symbols for pages allocated for ftrace
606 * purposes. In that case "__builtin__ftrace" is used as a module name, even
607 * though "__builtin__ftrace" is not a module.
608 */
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -0400609static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter)
610{
611 int ret = ftrace_mod_get_kallsym(iter->pos - iter->pos_mod_end,
612 &iter->value, &iter->type,
613 iter->name, iter->module_name,
614 &iter->exported);
615 if (ret < 0) {
616 iter->pos_ftrace_mod_end = iter->pos;
617 return 0;
618 }
619
620 return 1;
621}
622
Daniel Borkmann74451e662017-02-16 22:24:50 +0100623static int get_ksymbol_bpf(struct kallsym_iter *iter)
624{
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300625 int ret;
626
Song Liu69340582019-01-17 08:15:21 -0800627 strlcpy(iter->module_name, "bpf", MODULE_NAME_LEN);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100628 iter->exported = 0;
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300629 ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end,
630 &iter->value, &iter->type,
631 iter->name);
632 if (ret < 0) {
633 iter->pos_bpf_end = iter->pos;
634 return 0;
635 }
636
637 return 1;
638}
639
640/*
641 * This uses "__builtin__kprobes" as a module name for symbols for pages
642 * allocated for kprobes' purposes, even though "__builtin__kprobes" is not a
643 * module.
644 */
645static int get_ksymbol_kprobe(struct kallsym_iter *iter)
646{
647 strlcpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN);
648 iter->exported = 0;
649 return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end,
650 &iter->value, &iter->type,
651 iter->name) < 0 ? 0 : 1;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/* Returns space to next name. */
655static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
656{
657 unsigned off = iter->nameoff;
658
Alexey Dobriyanea078902007-05-08 00:28:39 -0700659 iter->module_name[0] = '\0';
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700660 iter->value = kallsyms_sym_address(iter->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 iter->type = kallsyms_get_symbol_type(off);
663
Chen Gange3f26752013-04-15 15:04:43 +0930664 off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 return off - iter->nameoff;
667}
668
669static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
670{
671 iter->name[0] = '\0';
672 iter->nameoff = get_symbol_offset(new_pos);
673 iter->pos = new_pos;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -0400674 if (new_pos == 0) {
Alexander Shishkind83212d2018-06-06 15:54:10 +0300675 iter->pos_arch_end = 0;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100676 iter->pos_mod_end = 0;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -0400677 iter->pos_ftrace_mod_end = 0;
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300678 iter->pos_bpf_end = 0;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -0400679 }
Daniel Borkmann74451e662017-02-16 22:24:50 +0100680}
681
Adrian Hunterb9667942018-06-06 15:54:09 +0300682/*
683 * The end position (last + 1) of each additional kallsyms section is recorded
684 * in iter->pos_..._end as each section is added, and so can be used to
685 * determine which get_ksymbol_...() function to call next.
686 */
Daniel Borkmann74451e662017-02-16 22:24:50 +0100687static int update_iter_mod(struct kallsym_iter *iter, loff_t pos)
688{
689 iter->pos = pos;
690
Alexander Shishkind83212d2018-06-06 15:54:10 +0300691 if ((!iter->pos_arch_end || iter->pos_arch_end > pos) &&
692 get_ksymbol_arch(iter))
693 return 1;
694
Adrian Hunterb9667942018-06-06 15:54:09 +0300695 if ((!iter->pos_mod_end || iter->pos_mod_end > pos) &&
696 get_ksymbol_mod(iter))
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -0400697 return 1;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -0400698
Adrian Hunterb9667942018-06-06 15:54:09 +0300699 if ((!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > pos) &&
700 get_ksymbol_ftrace_mod(iter))
701 return 1;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100702
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300703 if ((!iter->pos_bpf_end || iter->pos_bpf_end > pos) &&
704 get_ksymbol_bpf(iter))
705 return 1;
706
707 return get_ksymbol_kprobe(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/* Returns false if pos at or past end of file. */
711static int update_iter(struct kallsym_iter *iter, loff_t pos)
712{
713 /* Module symbols can be accessed randomly. */
Daniel Borkmann74451e662017-02-16 22:24:50 +0100714 if (pos >= kallsyms_num_syms)
715 return update_iter_mod(iter, pos);
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* If we're not on the desired position, reset to new position. */
718 if (pos != iter->pos)
719 reset_iter(iter, pos);
720
721 iter->nameoff += get_ksymbol_core(iter);
722 iter->pos++;
723
724 return 1;
725}
726
727static void *s_next(struct seq_file *m, void *p, loff_t *pos)
728{
729 (*pos)++;
730
731 if (!update_iter(m->private, *pos))
732 return NULL;
733 return p;
734}
735
736static void *s_start(struct seq_file *m, loff_t *pos)
737{
738 if (!update_iter(m->private, *pos))
739 return NULL;
740 return m->private;
741}
742
743static void s_stop(struct seq_file *m, void *p)
744{
745}
746
747static int s_show(struct seq_file *m, void *p)
748{
Linus Torvalds668533d2017-11-29 10:30:13 -0800749 void *value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 struct kallsym_iter *iter = m->private;
751
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700752 /* Some debugging symbols have no name. Ignore them. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!iter->name[0])
754 return 0;
755
Linus Torvalds668533d2017-11-29 10:30:13 -0800756 value = iter->show_value ? (void *)iter->value : NULL;
Linus Torvaldsc0f3ea12017-11-08 12:51:04 -0800757
Alexey Dobriyanea078902007-05-08 00:28:39 -0700758 if (iter->module_name[0]) {
759 char type;
760
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700761 /*
762 * Label it "global" if it is exported,
763 * "local" if not exported.
764 */
Alexey Dobriyanea078902007-05-08 00:28:39 -0700765 type = iter->exported ? toupper(iter->type) :
766 tolower(iter->type);
Linus Torvalds668533d2017-11-29 10:30:13 -0800767 seq_printf(m, "%px %c %s\t[%s]\n", value,
Kees Cook9f36e2c2011-03-22 16:34:22 -0700768 type, iter->name, iter->module_name);
Alexey Dobriyanea078902007-05-08 00:28:39 -0700769 } else
Linus Torvalds668533d2017-11-29 10:30:13 -0800770 seq_printf(m, "%px %c %s\n", value,
Kees Cook9f36e2c2011-03-22 16:34:22 -0700771 iter->type, iter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return 0;
773}
774
Helge Deller15ad7cd2006-12-06 20:40:36 -0800775static const struct seq_operations kallsyms_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 .start = s_start,
777 .next = s_next,
778 .stop = s_stop,
779 .show = s_show
780};
781
Alan Maguire647cafa2022-07-12 13:31:44 +0100782#ifdef CONFIG_BPF_SYSCALL
783
784struct bpf_iter__ksym {
785 __bpf_md_ptr(struct bpf_iter_meta *, meta);
786 __bpf_md_ptr(struct kallsym_iter *, ksym);
787};
788
789static int ksym_prog_seq_show(struct seq_file *m, bool in_stop)
790{
791 struct bpf_iter__ksym ctx;
792 struct bpf_iter_meta meta;
793 struct bpf_prog *prog;
794
795 meta.seq = m;
796 prog = bpf_iter_get_info(&meta, in_stop);
797 if (!prog)
798 return 0;
799
800 ctx.meta = &meta;
801 ctx.ksym = m ? m->private : NULL;
802 return bpf_iter_run_prog(prog, &ctx);
803}
804
805static int bpf_iter_ksym_seq_show(struct seq_file *m, void *p)
806{
807 return ksym_prog_seq_show(m, false);
808}
809
810static void bpf_iter_ksym_seq_stop(struct seq_file *m, void *p)
811{
812 if (!p)
813 (void) ksym_prog_seq_show(m, true);
814 else
815 s_stop(m, p);
816}
817
818static const struct seq_operations bpf_iter_ksym_ops = {
819 .start = s_start,
820 .next = s_next,
821 .stop = bpf_iter_ksym_seq_stop,
822 .show = bpf_iter_ksym_seq_show,
823};
824
825static int bpf_iter_ksym_init(void *priv_data, struct bpf_iter_aux_info *aux)
826{
827 struct kallsym_iter *iter = priv_data;
828
829 reset_iter(iter, 0);
830
831 /* cache here as in kallsyms_open() case; use current process
832 * credentials to tell BPF iterators if values should be shown.
833 */
834 iter->show_value = kallsyms_show_value(current_cred());
835
836 return 0;
837}
838
839DEFINE_BPF_ITER_FUNC(ksym, struct bpf_iter_meta *meta, struct kallsym_iter *ksym)
840
841static const struct bpf_iter_seq_info ksym_iter_seq_info = {
842 .seq_ops = &bpf_iter_ksym_ops,
843 .init_seq_private = bpf_iter_ksym_init,
844 .fini_seq_private = NULL,
845 .seq_priv_size = sizeof(struct kallsym_iter),
846};
847
848static struct bpf_iter_reg ksym_iter_reg_info = {
849 .target = "ksym",
850 .feature = BPF_ITER_RESCHED,
851 .ctx_arg_info_size = 1,
852 .ctx_arg_info = {
853 { offsetof(struct bpf_iter__ksym, ksym),
854 PTR_TO_BTF_ID_OR_NULL },
855 },
856 .seq_info = &ksym_iter_seq_info,
857};
858
859BTF_ID_LIST(btf_ksym_iter_id)
860BTF_ID(struct, kallsym_iter)
861
862static int __init bpf_ksym_iter_register(void)
863{
864 ksym_iter_reg_info.ctx_arg_info[0].btf_id = *btf_ksym_iter_id;
865 return bpf_iter_reg_target(&ksym_iter_reg_info);
866}
867
868late_initcall(bpf_ksym_iter_register);
869
870#endif /* CONFIG_BPF_SYSCALL */
871
Linus Torvaldsc0f3ea12017-11-08 12:51:04 -0800872static inline int kallsyms_for_perf(void)
873{
874#ifdef CONFIG_PERF_EVENTS
875 extern int sysctl_perf_event_paranoid;
876 if (sysctl_perf_event_paranoid <= 1)
877 return 1;
878#endif
879 return 0;
880}
881
882/*
883 * We show kallsyms information even to normal users if we've enabled
884 * kernel profiling and are explicitly not paranoid (so kptr_restrict
885 * is clear, and sysctl_perf_event_paranoid isn't set).
886 *
887 * Otherwise, require CAP_SYSLOG (assuming kptr_restrict isn't set to
888 * block even that).
889 */
Kees Cook16025182020-07-02 11:49:23 -0700890bool kallsyms_show_value(const struct cred *cred)
Linus Torvaldsc0f3ea12017-11-08 12:51:04 -0800891{
892 switch (kptr_restrict) {
893 case 0:
894 if (kallsyms_for_perf())
Kees Cook16025182020-07-02 11:49:23 -0700895 return true;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500896 fallthrough;
Linus Torvaldsc0f3ea12017-11-08 12:51:04 -0800897 case 1:
Kees Cook16025182020-07-02 11:49:23 -0700898 if (security_capable(cred, &init_user_ns, CAP_SYSLOG,
899 CAP_OPT_NOAUDIT) == 0)
900 return true;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500901 fallthrough;
Linus Torvaldsc0f3ea12017-11-08 12:51:04 -0800902 default:
Kees Cook16025182020-07-02 11:49:23 -0700903 return false;
Linus Torvaldsc0f3ea12017-11-08 12:51:04 -0800904 }
905}
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static int kallsyms_open(struct inode *inode, struct file *file)
908{
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700909 /*
910 * We keep iterator in m->private, since normal case is to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * s_start from where we left off, so we avoid doing
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700912 * using get_symbol_offset for every symbol.
913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct kallsym_iter *iter;
Rob Jones0049f262014-10-13 15:52:10 -0700915 iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (!iter)
917 return -ENOMEM;
918 reset_iter(iter, 0);
919
Kees Cook16025182020-07-02 11:49:23 -0700920 /*
921 * Instead of checking this on every s_show() call, cache
922 * the result here at open time.
923 */
924 iter->show_value = kallsyms_show_value(file->f_cred);
Rob Jones0049f262014-10-13 15:52:10 -0700925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Jason Wessel67fc4e02010-05-20 21:04:21 -0500928#ifdef CONFIG_KGDB_KDB
929const char *kdb_walk_kallsyms(loff_t *pos)
930{
931 static struct kallsym_iter kdb_walk_kallsyms_iter;
932 if (*pos == 0) {
933 memset(&kdb_walk_kallsyms_iter, 0,
934 sizeof(kdb_walk_kallsyms_iter));
935 reset_iter(&kdb_walk_kallsyms_iter, 0);
936 }
937 while (1) {
938 if (!update_iter(&kdb_walk_kallsyms_iter, *pos))
939 return NULL;
940 ++*pos;
941 /* Some debugging symbols have no name. Ignore them. */
942 if (kdb_walk_kallsyms_iter.name[0])
943 return kdb_walk_kallsyms_iter.name;
944 }
945}
946#endif /* CONFIG_KGDB_KDB */
947
Alexey Dobriyan97a32532020-02-03 17:37:17 -0800948static const struct proc_ops kallsyms_proc_ops = {
949 .proc_open = kallsyms_open,
950 .proc_read = seq_read,
951 .proc_lseek = seq_lseek,
952 .proc_release = seq_release_private,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953};
954
955static int __init kallsyms_init(void)
956{
Alexey Dobriyan97a32532020-02-03 17:37:17 -0800957 proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 0;
959}
Manish Katiyarad6ccfa2009-05-12 13:43:35 -0700960device_initcall(kallsyms_init);