linux/include/linux/kallsyms.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0 */
   2/* Rewritten and vastly simplified by Rusty Russell for in-kernel
   3 * module loader:
   4 *   Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
   5 */
   6#ifndef _LINUX_KALLSYMS_H
   7#define _LINUX_KALLSYMS_H
   8
   9#include <linux/errno.h>
  10#include <linux/kernel.h>
  11#include <linux/stddef.h>
  12#include <linux/mm.h>
  13#include <linux/module.h>
  14
  15#include <asm/sections.h>
  16
  17#define KSYM_NAME_LEN 128
  18#define KSYM_SYMBOL_LEN (sizeof("%s+%#lx/%#lx [%s]") + (KSYM_NAME_LEN - 1) + \
  19                         2*(BITS_PER_LONG*3/10) + (MODULE_NAME_LEN - 1) + 1)
  20
  21struct cred;
  22struct module;
  23
  24static inline int is_kernel_inittext(unsigned long addr)
  25{
  26        if (addr >= (unsigned long)_sinittext
  27            && addr <= (unsigned long)_einittext)
  28                return 1;
  29        return 0;
  30}
  31
  32static inline int is_kernel_text(unsigned long addr)
  33{
  34        if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext) ||
  35            arch_is_kernel_text(addr))
  36                return 1;
  37        return in_gate_area_no_mm(addr);
  38}
  39
  40static inline int is_kernel(unsigned long addr)
  41{
  42        if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
  43                return 1;
  44        return in_gate_area_no_mm(addr);
  45}
  46
  47static inline int is_ksym_addr(unsigned long addr)
  48{
  49        if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
  50                return is_kernel(addr);
  51
  52        return is_kernel_text(addr) || is_kernel_inittext(addr);
  53}
  54
  55static inline void *dereference_symbol_descriptor(void *ptr)
  56{
  57#ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR
  58        struct module *mod;
  59
  60        ptr = dereference_kernel_function_descriptor(ptr);
  61        if (is_ksym_addr((unsigned long)ptr))
  62                return ptr;
  63
  64        preempt_disable();
  65        mod = __module_address((unsigned long)ptr);
  66        preempt_enable();
  67
  68        if (mod)
  69                ptr = dereference_module_function_descriptor(mod, ptr);
  70#endif
  71        return ptr;
  72}
  73
  74int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
  75                                      unsigned long),
  76                            void *data);
  77
  78#ifdef CONFIG_KALLSYMS
  79/* Lookup the address for a symbol. Returns 0 if not found. */
  80unsigned long kallsyms_lookup_name(const char *name);
  81
  82extern int kallsyms_lookup_size_offset(unsigned long addr,
  83                                  unsigned long *symbolsize,
  84                                  unsigned long *offset);
  85
  86/* Lookup an address.  modname is set to NULL if it's in the kernel. */
  87const char *kallsyms_lookup(unsigned long addr,
  88                            unsigned long *symbolsize,
  89                            unsigned long *offset,
  90                            char **modname, char *namebuf);
  91
  92/* Look up a kernel symbol and return it in a text buffer. */
  93extern int sprint_symbol(char *buffer, unsigned long address);
  94extern int sprint_symbol_no_offset(char *buffer, unsigned long address);
  95extern int sprint_backtrace(char *buffer, unsigned long address);
  96
  97int lookup_symbol_name(unsigned long addr, char *symname);
  98int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
  99
 100/* How and when do we show kallsyms values? */
 101extern bool kallsyms_show_value(const struct cred *cred);
 102
 103#else /* !CONFIG_KALLSYMS */
 104
 105static inline unsigned long kallsyms_lookup_name(const char *name)
 106{
 107        return 0;
 108}
 109
 110static inline int kallsyms_lookup_size_offset(unsigned long addr,
 111                                              unsigned long *symbolsize,
 112                                              unsigned long *offset)
 113{
 114        return 0;
 115}
 116
 117static inline const char *kallsyms_lookup(unsigned long addr,
 118                                          unsigned long *symbolsize,
 119                                          unsigned long *offset,
 120                                          char **modname, char *namebuf)
 121{
 122        return NULL;
 123}
 124
 125static inline int sprint_symbol(char *buffer, unsigned long addr)
 126{
 127        *buffer = '\0';
 128        return 0;
 129}
 130
 131static inline int sprint_symbol_no_offset(char *buffer, unsigned long addr)
 132{
 133        *buffer = '\0';
 134        return 0;
 135}
 136
 137static inline int sprint_backtrace(char *buffer, unsigned long addr)
 138{
 139        *buffer = '\0';
 140        return 0;
 141}
 142
 143static inline int lookup_symbol_name(unsigned long addr, char *symname)
 144{
 145        return -ERANGE;
 146}
 147
 148static inline int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
 149{
 150        return -ERANGE;
 151}
 152
 153static inline bool kallsyms_show_value(const struct cred *cred)
 154{
 155        return false;
 156}
 157
 158#endif /*CONFIG_KALLSYMS*/
 159
 160static inline void print_ip_sym(const char *loglvl, unsigned long ip)
 161{
 162        printk("%s[<%px>] %pS\n", loglvl, (void *) ip, (void *) ip);
 163}
 164
 165#endif /*_LINUX_KALLSYMS_H*/
 166