xref: /linux-6.15/include/linux/moduleloader.h (revision c287c072)
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 
16f9231a99SNicholas Piggin /* arch may override to do additional checking of ELF header architecture */
17f9231a99SNicholas Piggin bool module_elf_check_arch(Elf_Ehdr *hdr);
18f9231a99SNicholas Piggin 
191da177e4SLinus Torvalds /* Adjust arch-specific sections.  Return 0 on success.  */
201da177e4SLinus Torvalds int module_frob_arch_sections(Elf_Ehdr *hdr,
211da177e4SLinus Torvalds 			      Elf_Shdr *sechdrs,
221da177e4SLinus Torvalds 			      char *secstrings,
231da177e4SLinus Torvalds 			      struct module *mod);
241da177e4SLinus Torvalds 
25088af9a6SHelge Deller /* Additional bytes needed by arch in front of individual sections */
26088af9a6SHelge Deller unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section);
27088af9a6SHelge Deller 
2823189766SVincent Whitchurch /* Determines if the section name is an init section (that is only used during
2923189766SVincent Whitchurch  * module loading).
3023189766SVincent Whitchurch  */
3123189766SVincent Whitchurch bool module_init_section(const char *name);
3223189766SVincent Whitchurch 
3338b37d63SMatthias Schiffer /* Determines if the section name is an exit section (that is only used during
3438b37d63SMatthias Schiffer  * module unloading)
3538b37d63SMatthias Schiffer  */
3638b37d63SMatthias Schiffer bool module_exit_section(const char *name);
3738b37d63SMatthias Schiffer 
382abcc4b5SJames Morse /* Describes whether within_module_init() will consider this an init section
392abcc4b5SJames Morse  * or not. This behaviour changes with CONFIG_MODULE_UNLOAD.
402abcc4b5SJames Morse  */
412abcc4b5SJames Morse bool module_init_layout_section(const char *sname);
422abcc4b5SJames Morse 
43786d35d4SDavid Howells /*
44786d35d4SDavid Howells  * Apply the given relocation to the (simplified) ELF.  Return -error
45786d35d4SDavid Howells  * or 0.
46786d35d4SDavid Howells  */
47786d35d4SDavid Howells #ifdef CONFIG_MODULES_USE_ELF_REL
481da177e4SLinus Torvalds int apply_relocate(Elf_Shdr *sechdrs,
491da177e4SLinus Torvalds 		   const char *strtab,
501da177e4SLinus Torvalds 		   unsigned int symindex,
511da177e4SLinus Torvalds 		   unsigned int relsec,
521da177e4SLinus Torvalds 		   struct module *mod);
53786d35d4SDavid Howells #else
apply_relocate(Elf_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)54786d35d4SDavid Howells static inline int apply_relocate(Elf_Shdr *sechdrs,
55786d35d4SDavid Howells 				 const char *strtab,
56786d35d4SDavid Howells 				 unsigned int symindex,
57786d35d4SDavid Howells 				 unsigned int relsec,
58786d35d4SDavid Howells 				 struct module *me)
59786d35d4SDavid Howells {
603a611c3cSRusty Russell 	printk(KERN_ERR "module %s: REL relocation unsupported\n",
613a611c3cSRusty Russell 	       module_name(me));
62786d35d4SDavid Howells 	return -ENOEXEC;
63786d35d4SDavid Howells }
64786d35d4SDavid Howells #endif
651da177e4SLinus Torvalds 
66786d35d4SDavid Howells /*
67786d35d4SDavid Howells  * Apply the given add relocation to the (simplified) ELF.  Return
68786d35d4SDavid Howells  * -error or 0
69786d35d4SDavid Howells  */
70786d35d4SDavid Howells #ifdef CONFIG_MODULES_USE_ELF_RELA
711da177e4SLinus Torvalds int apply_relocate_add(Elf_Shdr *sechdrs,
721da177e4SLinus Torvalds 		       const char *strtab,
731da177e4SLinus Torvalds 		       unsigned int symindex,
741da177e4SLinus Torvalds 		       unsigned int relsec,
751da177e4SLinus Torvalds 		       struct module *mod);
760c05e7bdSSong Liu #ifdef CONFIG_LIVEPATCH
770c05e7bdSSong Liu /*
780c05e7bdSSong Liu  * Some architectures (namely x86_64 and ppc64) perform sanity checks when
790c05e7bdSSong Liu  * applying relocations.  If a patched module gets unloaded and then later
800c05e7bdSSong Liu  * reloaded (and re-patched), klp re-applies relocations to the replacement
810c05e7bdSSong Liu  * function(s).  Any leftover relocations from the previous loading of the
820c05e7bdSSong Liu  * patched module might trigger the sanity checks.
830c05e7bdSSong Liu  *
840c05e7bdSSong Liu  * To prevent that, when unloading a patched module, clear out any relocations
850c05e7bdSSong Liu  * that might trigger arch-specific sanity checks on a future module reload.
860c05e7bdSSong Liu  */
870c05e7bdSSong Liu void clear_relocate_add(Elf_Shdr *sechdrs,
880c05e7bdSSong Liu 		   const char *strtab,
890c05e7bdSSong Liu 		   unsigned int symindex,
900c05e7bdSSong Liu 		   unsigned int relsec,
910c05e7bdSSong Liu 		   struct module *me);
920c05e7bdSSong Liu #endif
93786d35d4SDavid Howells #else
apply_relocate_add(Elf_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)94786d35d4SDavid Howells static inline int apply_relocate_add(Elf_Shdr *sechdrs,
95786d35d4SDavid Howells 				     const char *strtab,
96786d35d4SDavid Howells 				     unsigned int symindex,
97786d35d4SDavid Howells 				     unsigned int relsec,
98786d35d4SDavid Howells 				     struct module *me)
99786d35d4SDavid Howells {
1003a611c3cSRusty Russell 	printk(KERN_ERR "module %s: REL relocation unsupported\n",
1013a611c3cSRusty Russell 	       module_name(me));
102786d35d4SDavid Howells 	return -ENOEXEC;
103786d35d4SDavid Howells }
104786d35d4SDavid Howells #endif
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /* Any final processing of module before access.  Return -error or 0. */
1071da177e4SLinus Torvalds int module_finalize(const Elf_Ehdr *hdr,
1081da177e4SLinus Torvalds 		    const Elf_Shdr *sechdrs,
1091da177e4SLinus Torvalds 		    struct module *mod);
1101da177e4SLinus Torvalds 
111*8f8cd6c0SChangbin Du #ifdef CONFIG_MODULES
112*8f8cd6c0SChangbin Du void flush_module_init_free_work(void);
113*8f8cd6c0SChangbin Du #else
flush_module_init_free_work(void)114*8f8cd6c0SChangbin Du static inline void flush_module_init_free_work(void)
115*8f8cd6c0SChangbin Du {
116*8f8cd6c0SChangbin Du }
117*8f8cd6c0SChangbin Du #endif
118*8f8cd6c0SChangbin Du 
1191da177e4SLinus Torvalds /* Any cleanup needed when module leaves. */
1201da177e4SLinus Torvalds void module_arch_cleanup(struct module *mod);
1211da177e4SLinus Torvalds 
122d453cdedSRusty Russell /* Any cleanup before freeing mod->module_init */
123d453cdedSRusty Russell void module_arch_freeing_init(struct module *mod);
124d3733e5cSAndrey Ryabinin 
1251da177e4SLinus Torvalds #endif
126