kallsyms.h 4.6 KB

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