kallsyms.h 4.3 KB

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