moduleloader.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /*
  34. * Apply the given relocation to the (simplified) ELF. Return -error
  35. * or 0.
  36. */
  37. #ifdef CONFIG_MODULES_USE_ELF_REL
  38. int apply_relocate(Elf_Shdr *sechdrs,
  39. const char *strtab,
  40. unsigned int symindex,
  41. unsigned int relsec,
  42. struct module *mod);
  43. #else
  44. static inline int apply_relocate(Elf_Shdr *sechdrs,
  45. const char *strtab,
  46. unsigned int symindex,
  47. unsigned int relsec,
  48. struct module *me)
  49. {
  50. printk(KERN_ERR "module %s: REL relocation unsupported\n",
  51. module_name(me));
  52. return -ENOEXEC;
  53. }
  54. #endif
  55. /*
  56. * Apply the given add relocation to the (simplified) ELF. Return
  57. * -error or 0
  58. */
  59. #ifdef CONFIG_MODULES_USE_ELF_RELA
  60. int apply_relocate_add(Elf_Shdr *sechdrs,
  61. const char *strtab,
  62. unsigned int symindex,
  63. unsigned int relsec,
  64. struct module *mod);
  65. #else
  66. static inline int apply_relocate_add(Elf_Shdr *sechdrs,
  67. const char *strtab,
  68. unsigned int symindex,
  69. unsigned int relsec,
  70. struct module *me)
  71. {
  72. printk(KERN_ERR "module %s: REL relocation unsupported\n",
  73. module_name(me));
  74. return -ENOEXEC;
  75. }
  76. #endif
  77. /* Any final processing of module before access. Return -error or 0. */
  78. int module_finalize(const Elf_Ehdr *hdr,
  79. const Elf_Shdr *sechdrs,
  80. struct module *mod);
  81. /* Any cleanup needed when module leaves. */
  82. void module_arch_cleanup(struct module *mod);
  83. /* Any cleanup before freeing mod->module_init */
  84. void module_arch_freeing_init(struct module *mod);
  85. #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
  86. !defined(CONFIG_KASAN_VMALLOC)
  87. #include <linux/kasan.h>
  88. #define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
  89. #else
  90. #define MODULE_ALIGN PAGE_SIZE
  91. #endif
  92. #endif