moduleloader.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_MODULELOADER_H
  3. #define _LINUX_MODULELOADER_H
  4. /* The stuff needed for archs to support modules. */
  5. #include <linux/module.h>
  6. #include <linux/elf.h>
  7. /* These may be implemented by architectures that need to hook into the
  8. * module loader code. Architectures that don't need to do anything special
  9. * can just rely on the 'weak' default hooks defined in kernel/module.c.
  10. * Note, however, that at least one of apply_relocate or apply_relocate_add
  11. * must be implemented by each architecture.
  12. */
  13. /* Adjust arch-specific sections. Return 0 on success. */
  14. int module_frob_arch_sections(Elf_Ehdr *hdr,
  15. Elf_Shdr *sechdrs,
  16. char *secstrings,
  17. struct module *mod);
  18. /* Additional bytes needed by arch in front of individual sections */
  19. unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section);
  20. /* Allocator used for allocating struct module, core sections and init
  21. sections. Returns NULL on failure. */
  22. void *module_alloc(unsigned long size);
  23. /* Free memory returned from module_alloc. */
  24. void module_memfree(void *module_region);
  25. /* Determines if the section name is an init section (that is only used during
  26. * module loading).
  27. */
  28. bool module_init_section(const char *name);
  29. /* Determines if the section name is an exit section (that is only used during
  30. * module unloading)
  31. */
  32. bool module_exit_section(const char *name);
  33. /* Describes whether within_module_init() will consider this an init section
  34. * or not. This behaviour changes with CONFIG_MODULE_UNLOAD.
  35. */
  36. bool module_init_layout_section(const char *sname);
  37. /*
  38. * Apply the given relocation to the (simplified) ELF. Return -error
  39. * or 0.
  40. */
  41. #ifdef CONFIG_MODULES_USE_ELF_REL
  42. int apply_relocate(Elf_Shdr *sechdrs,
  43. const char *strtab,
  44. unsigned int symindex,
  45. unsigned int relsec,
  46. struct module *mod);
  47. #else
  48. static inline int apply_relocate(Elf_Shdr *sechdrs,
  49. const char *strtab,
  50. unsigned int symindex,
  51. unsigned int relsec,
  52. struct module *me)
  53. {
  54. printk(KERN_ERR "module %s: REL relocation unsupported\n",
  55. module_name(me));
  56. return -ENOEXEC;
  57. }
  58. #endif
  59. /*
  60. * Apply the given add relocation to the (simplified) ELF. Return
  61. * -error or 0
  62. */
  63. #ifdef CONFIG_MODULES_USE_ELF_RELA
  64. int apply_relocate_add(Elf_Shdr *sechdrs,
  65. const char *strtab,
  66. unsigned int symindex,
  67. unsigned int relsec,
  68. struct module *mod);
  69. #else
  70. static inline int apply_relocate_add(Elf_Shdr *sechdrs,
  71. const char *strtab,
  72. unsigned int symindex,
  73. unsigned int relsec,
  74. struct module *me)
  75. {
  76. printk(KERN_ERR "module %s: REL relocation unsupported\n",
  77. module_name(me));
  78. return -ENOEXEC;
  79. }
  80. #endif
  81. /* Any final processing of module before access. Return -error or 0. */
  82. int module_finalize(const Elf_Ehdr *hdr,
  83. const Elf_Shdr *sechdrs,
  84. struct module *mod);
  85. /* Any cleanup needed when module leaves. */
  86. void module_arch_cleanup(struct module *mod);
  87. /* Any cleanup before freeing mod->module_init */
  88. void module_arch_freeing_init(struct module *mod);
  89. #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
  90. !defined(CONFIG_KASAN_VMALLOC)
  91. #include <linux/kasan.h>
  92. #define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
  93. #else
  94. #define MODULE_ALIGN PAGE_SIZE
  95. #endif
  96. #endif