xref: /linux-6.15/include/linux/moduleloader.h (revision 38b37d63)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_MODULELOADER_H
31da177e4SLinus Torvalds #define _LINUX_MODULELOADER_H
41da177e4SLinus Torvalds /* The stuff needed for archs to support modules. */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <linux/module.h>
71da177e4SLinus Torvalds #include <linux/elf.h>
81da177e4SLinus Torvalds 
974e08fcfSJonas Bonn /* These may be implemented by architectures that need to hook into the
1074e08fcfSJonas Bonn  * module loader code.  Architectures that don't need to do anything special
1174e08fcfSJonas Bonn  * can just rely on the 'weak' default hooks defined in kernel/module.c.
1274e08fcfSJonas Bonn  * Note, however, that at least one of apply_relocate or apply_relocate_add
1374e08fcfSJonas Bonn  * must be implemented by each architecture.
1474e08fcfSJonas Bonn  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds /* Adjust arch-specific sections.  Return 0 on success.  */
171da177e4SLinus Torvalds int module_frob_arch_sections(Elf_Ehdr *hdr,
181da177e4SLinus Torvalds 			      Elf_Shdr *sechdrs,
191da177e4SLinus Torvalds 			      char *secstrings,
201da177e4SLinus Torvalds 			      struct module *mod);
211da177e4SLinus Torvalds 
22088af9a6SHelge Deller /* Additional bytes needed by arch in front of individual sections */
23088af9a6SHelge Deller unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section);
24088af9a6SHelge Deller 
251da177e4SLinus Torvalds /* Allocator used for allocating struct module, core sections and init
261da177e4SLinus Torvalds    sections.  Returns NULL on failure. */
271da177e4SLinus Torvalds void *module_alloc(unsigned long size);
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /* Free memory returned from module_alloc. */
30be1f221cSRusty Russell void module_memfree(void *module_region);
311da177e4SLinus Torvalds 
32*38b37d63SMatthias Schiffer /* Determines if the section name is an exit section (that is only used during
33*38b37d63SMatthias Schiffer  * module unloading)
34*38b37d63SMatthias Schiffer  */
35*38b37d63SMatthias Schiffer bool module_exit_section(const char *name);
36*38b37d63SMatthias Schiffer 
37786d35d4SDavid Howells /*
38786d35d4SDavid Howells  * Apply the given relocation to the (simplified) ELF.  Return -error
39786d35d4SDavid Howells  * or 0.
40786d35d4SDavid Howells  */
41786d35d4SDavid Howells #ifdef CONFIG_MODULES_USE_ELF_REL
421da177e4SLinus Torvalds int apply_relocate(Elf_Shdr *sechdrs,
431da177e4SLinus Torvalds 		   const char *strtab,
441da177e4SLinus Torvalds 		   unsigned int symindex,
451da177e4SLinus Torvalds 		   unsigned int relsec,
461da177e4SLinus Torvalds 		   struct module *mod);
47786d35d4SDavid Howells #else
48786d35d4SDavid Howells static inline int apply_relocate(Elf_Shdr *sechdrs,
49786d35d4SDavid Howells 				 const char *strtab,
50786d35d4SDavid Howells 				 unsigned int symindex,
51786d35d4SDavid Howells 				 unsigned int relsec,
52786d35d4SDavid Howells 				 struct module *me)
53786d35d4SDavid Howells {
543a611c3cSRusty Russell 	printk(KERN_ERR "module %s: REL relocation unsupported\n",
553a611c3cSRusty Russell 	       module_name(me));
56786d35d4SDavid Howells 	return -ENOEXEC;
57786d35d4SDavid Howells }
58786d35d4SDavid Howells #endif
591da177e4SLinus Torvalds 
60786d35d4SDavid Howells /*
61786d35d4SDavid Howells  * Apply the given add relocation to the (simplified) ELF.  Return
62786d35d4SDavid Howells  * -error or 0
63786d35d4SDavid Howells  */
64786d35d4SDavid Howells #ifdef CONFIG_MODULES_USE_ELF_RELA
651da177e4SLinus Torvalds int apply_relocate_add(Elf_Shdr *sechdrs,
661da177e4SLinus Torvalds 		       const char *strtab,
671da177e4SLinus Torvalds 		       unsigned int symindex,
681da177e4SLinus Torvalds 		       unsigned int relsec,
691da177e4SLinus Torvalds 		       struct module *mod);
70786d35d4SDavid Howells #else
71786d35d4SDavid Howells static inline int apply_relocate_add(Elf_Shdr *sechdrs,
72786d35d4SDavid Howells 				     const char *strtab,
73786d35d4SDavid Howells 				     unsigned int symindex,
74786d35d4SDavid Howells 				     unsigned int relsec,
75786d35d4SDavid Howells 				     struct module *me)
76786d35d4SDavid Howells {
773a611c3cSRusty Russell 	printk(KERN_ERR "module %s: REL relocation unsupported\n",
783a611c3cSRusty Russell 	       module_name(me));
79786d35d4SDavid Howells 	return -ENOEXEC;
80786d35d4SDavid Howells }
81786d35d4SDavid Howells #endif
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /* Any final processing of module before access.  Return -error or 0. */
841da177e4SLinus Torvalds int module_finalize(const Elf_Ehdr *hdr,
851da177e4SLinus Torvalds 		    const Elf_Shdr *sechdrs,
861da177e4SLinus Torvalds 		    struct module *mod);
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /* Any cleanup needed when module leaves. */
891da177e4SLinus Torvalds void module_arch_cleanup(struct module *mod);
901da177e4SLinus Torvalds 
91d453cdedSRusty Russell /* Any cleanup before freeing mod->module_init */
92d453cdedSRusty Russell void module_arch_freeing_init(struct module *mod);
93d3733e5cSAndrey Ryabinin 
94d3733e5cSAndrey Ryabinin #ifdef CONFIG_KASAN
95d3733e5cSAndrey Ryabinin #include <linux/kasan.h>
96d3733e5cSAndrey Ryabinin #define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
97d3733e5cSAndrey Ryabinin #else
98d3733e5cSAndrey Ryabinin #define MODULE_ALIGN PAGE_SIZE
99d3733e5cSAndrey Ryabinin #endif
100d3733e5cSAndrey Ryabinin 
1011da177e4SLinus Torvalds #endif
102