sections.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_SECTIONS_H_
  3. #define _ASM_GENERIC_SECTIONS_H_
  4. /* References to section boundaries */
  5. #include <linux/compiler.h>
  6. #include <linux/types.h>
  7. /*
  8. * Usage guidelines:
  9. * _text, _data: architecture specific, don't use them in arch-independent code
  10. * [_stext, _etext]: contains .text.* sections, may also contain .rodata.*
  11. * and/or .init.* sections
  12. * [_sdata, _edata]: contains .data.* sections, may also contain .rodata.*
  13. * and/or .init.* sections.
  14. * [__start_rodata, __end_rodata]: contains .rodata.* sections
  15. * [__start_ro_after_init, __end_ro_after_init]:
  16. * contains .data..ro_after_init section
  17. * [__init_begin, __init_end]: contains .init.* sections, but .init.text.*
  18. * may be out of this range on some architectures.
  19. * [_sinittext, _einittext]: contains .init.text.* sections
  20. * [__bss_start, __bss_stop]: contains BSS sections
  21. *
  22. * Following global variables are optional and may be unavailable on some
  23. * architectures and/or kernel configurations.
  24. * _text, _data
  25. * __kprobes_text_start, __kprobes_text_end
  26. * __entry_text_start, __entry_text_end
  27. * __ctors_start, __ctors_end
  28. * __irqentry_text_start, __irqentry_text_end
  29. * __softirqentry_text_start, __softirqentry_text_end
  30. * __start_opd, __end_opd
  31. */
  32. extern char _text[], _stext[], _etext[];
  33. extern char _data[], _sdata[], _edata[];
  34. extern char __bss_start[], __bss_stop[];
  35. extern char __init_begin[], __init_end[];
  36. extern char _sinittext[], _einittext[];
  37. extern char __start_ro_after_init[], __end_ro_after_init[];
  38. extern char _end[];
  39. extern char __per_cpu_load[], __per_cpu_start[], __per_cpu_end[];
  40. extern char __kprobes_text_start[], __kprobes_text_end[];
  41. extern char __entry_text_start[], __entry_text_end[];
  42. extern char __start_rodata[], __end_rodata[];
  43. extern char __irqentry_text_start[], __irqentry_text_end[];
  44. extern char __softirqentry_text_start[], __softirqentry_text_end[];
  45. extern char __start_once[], __end_once[];
  46. /* Start and end of .ctors section - used for constructor calls. */
  47. extern char __ctors_start[], __ctors_end[];
  48. /* Start and end of .opd section - used for function descriptors. */
  49. extern char __start_opd[], __end_opd[];
  50. /* Start and end of instrumentation protected text section */
  51. extern char __noinstr_text_start[], __noinstr_text_end[];
  52. extern __visible const void __nosave_begin, __nosave_end;
  53. /* Function descriptor handling (if any). Override in asm/sections.h */
  54. #ifndef dereference_function_descriptor
  55. #define dereference_function_descriptor(p) ((void *)(p))
  56. #define dereference_kernel_function_descriptor(p) ((void *)(p))
  57. #endif
  58. /* random extra sections (if any). Override
  59. * in asm/sections.h */
  60. #ifndef arch_is_kernel_text
  61. static inline int arch_is_kernel_text(unsigned long addr)
  62. {
  63. return 0;
  64. }
  65. #endif
  66. #ifndef arch_is_kernel_data
  67. static inline int arch_is_kernel_data(unsigned long addr)
  68. {
  69. return 0;
  70. }
  71. #endif
  72. /*
  73. * Check if an address is part of freed initmem. This is needed on architectures
  74. * with virt == phys kernel mapping, for code that wants to check if an address
  75. * is part of a static object within [_stext, _end]. After initmem is freed,
  76. * memory can be allocated from it, and such allocations would then have
  77. * addresses within the range [_stext, _end].
  78. */
  79. #ifndef arch_is_kernel_initmem_freed
  80. static inline int arch_is_kernel_initmem_freed(unsigned long addr)
  81. {
  82. return 0;
  83. }
  84. #endif
  85. /**
  86. * memory_contains - checks if an object is contained within a memory region
  87. * @begin: virtual address of the beginning of the memory region
  88. * @end: virtual address of the end of the memory region
  89. * @virt: virtual address of the memory object
  90. * @size: size of the memory object
  91. *
  92. * Returns: true if the object specified by @virt and @size is entirely
  93. * contained within the memory region defined by @begin and @end, false
  94. * otherwise.
  95. */
  96. static inline bool memory_contains(void *begin, void *end, void *virt,
  97. size_t size)
  98. {
  99. return virt >= begin && virt + size <= end;
  100. }
  101. /**
  102. * memory_intersects - checks if the region occupied by an object intersects
  103. * with another memory region
  104. * @begin: virtual address of the beginning of the memory regien
  105. * @end: virtual address of the end of the memory region
  106. * @virt: virtual address of the memory object
  107. * @size: size of the memory object
  108. *
  109. * Returns: true if an object's memory region, specified by @virt and @size,
  110. * intersects with the region specified by @begin and @end, false otherwise.
  111. */
  112. static inline bool memory_intersects(void *begin, void *end, void *virt,
  113. size_t size)
  114. {
  115. void *vend = virt + size;
  116. return (virt >= begin && virt < end) || (vend >= begin && vend < end);
  117. }
  118. /**
  119. * init_section_contains - checks if an object is contained within the init
  120. * section
  121. * @virt: virtual address of the memory object
  122. * @size: size of the memory object
  123. *
  124. * Returns: true if the object specified by @virt and @size is entirely
  125. * contained within the init section, false otherwise.
  126. */
  127. static inline bool init_section_contains(void *virt, size_t size)
  128. {
  129. return memory_contains(__init_begin, __init_end, virt, size);
  130. }
  131. /**
  132. * init_section_intersects - checks if the region occupied by an object
  133. * intersects with the init section
  134. * @virt: virtual address of the memory object
  135. * @size: size of the memory object
  136. *
  137. * Returns: true if an object's memory region, specified by @virt and @size,
  138. * intersects with the init section, false otherwise.
  139. */
  140. static inline bool init_section_intersects(void *virt, size_t size)
  141. {
  142. return memory_intersects(__init_begin, __init_end, virt, size);
  143. }
  144. /**
  145. * is_kernel_rodata - checks if the pointer address is located in the
  146. * .rodata section
  147. *
  148. * @addr: address to check
  149. *
  150. * Returns: true if the address is located in .rodata, false otherwise.
  151. */
  152. static inline bool is_kernel_rodata(unsigned long addr)
  153. {
  154. return addr >= (unsigned long)__start_rodata &&
  155. addr < (unsigned long)__end_rodata;
  156. }
  157. #endif /* _ASM_GENERIC_SECTIONS_H_ */