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 281da177e4SLinus Torvalds /* Allocator used for allocating struct module, core sections and init 291da177e4SLinus Torvalds sections. Returns NULL on failure. */ 301da177e4SLinus Torvalds void *module_alloc(unsigned long size); 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds /* Free memory returned from module_alloc. */ 33be1f221cSRusty Russell void module_memfree(void *module_region); 341da177e4SLinus Torvalds 3523189766SVincent Whitchurch /* Determines if the section name is an init section (that is only used during 3623189766SVincent Whitchurch * module loading). 3723189766SVincent Whitchurch */ 3823189766SVincent Whitchurch bool module_init_section(const char *name); 3923189766SVincent Whitchurch 4038b37d63SMatthias Schiffer /* Determines if the section name is an exit section (that is only used during 4138b37d63SMatthias Schiffer * module unloading) 4238b37d63SMatthias Schiffer */ 4338b37d63SMatthias Schiffer bool module_exit_section(const char *name); 4438b37d63SMatthias Schiffer 45*2abcc4b5SJames Morse /* Describes whether within_module_init() will consider this an init section 46*2abcc4b5SJames Morse * or not. This behaviour changes with CONFIG_MODULE_UNLOAD. 47*2abcc4b5SJames Morse */ 48*2abcc4b5SJames Morse bool module_init_layout_section(const char *sname); 49*2abcc4b5SJames Morse 50786d35d4SDavid Howells /* 51786d35d4SDavid Howells * Apply the given relocation to the (simplified) ELF. Return -error 52786d35d4SDavid Howells * or 0. 53786d35d4SDavid Howells */ 54786d35d4SDavid Howells #ifdef CONFIG_MODULES_USE_ELF_REL 551da177e4SLinus Torvalds int apply_relocate(Elf_Shdr *sechdrs, 561da177e4SLinus Torvalds const char *strtab, 571da177e4SLinus Torvalds unsigned int symindex, 581da177e4SLinus Torvalds unsigned int relsec, 591da177e4SLinus Torvalds struct module *mod); 60786d35d4SDavid Howells #else 61786d35d4SDavid Howells static inline int apply_relocate(Elf_Shdr *sechdrs, 62786d35d4SDavid Howells const char *strtab, 63786d35d4SDavid Howells unsigned int symindex, 64786d35d4SDavid Howells unsigned int relsec, 65786d35d4SDavid Howells struct module *me) 66786d35d4SDavid Howells { 673a611c3cSRusty Russell printk(KERN_ERR "module %s: REL relocation unsupported\n", 683a611c3cSRusty Russell module_name(me)); 69786d35d4SDavid Howells return -ENOEXEC; 70786d35d4SDavid Howells } 71786d35d4SDavid Howells #endif 721da177e4SLinus Torvalds 73786d35d4SDavid Howells /* 74786d35d4SDavid Howells * Apply the given add relocation to the (simplified) ELF. Return 75786d35d4SDavid Howells * -error or 0 76786d35d4SDavid Howells */ 77786d35d4SDavid Howells #ifdef CONFIG_MODULES_USE_ELF_RELA 781da177e4SLinus Torvalds int apply_relocate_add(Elf_Shdr *sechdrs, 791da177e4SLinus Torvalds const char *strtab, 801da177e4SLinus Torvalds unsigned int symindex, 811da177e4SLinus Torvalds unsigned int relsec, 821da177e4SLinus Torvalds struct module *mod); 830c05e7bdSSong Liu #ifdef CONFIG_LIVEPATCH 840c05e7bdSSong Liu /* 850c05e7bdSSong Liu * Some architectures (namely x86_64 and ppc64) perform sanity checks when 860c05e7bdSSong Liu * applying relocations. If a patched module gets unloaded and then later 870c05e7bdSSong Liu * reloaded (and re-patched), klp re-applies relocations to the replacement 880c05e7bdSSong Liu * function(s). Any leftover relocations from the previous loading of the 890c05e7bdSSong Liu * patched module might trigger the sanity checks. 900c05e7bdSSong Liu * 910c05e7bdSSong Liu * To prevent that, when unloading a patched module, clear out any relocations 920c05e7bdSSong Liu * that might trigger arch-specific sanity checks on a future module reload. 930c05e7bdSSong Liu */ 940c05e7bdSSong Liu void clear_relocate_add(Elf_Shdr *sechdrs, 950c05e7bdSSong Liu const char *strtab, 960c05e7bdSSong Liu unsigned int symindex, 970c05e7bdSSong Liu unsigned int relsec, 980c05e7bdSSong Liu struct module *me); 990c05e7bdSSong Liu #endif 100786d35d4SDavid Howells #else 101786d35d4SDavid Howells static inline int apply_relocate_add(Elf_Shdr *sechdrs, 102786d35d4SDavid Howells const char *strtab, 103786d35d4SDavid Howells unsigned int symindex, 104786d35d4SDavid Howells unsigned int relsec, 105786d35d4SDavid Howells struct module *me) 106786d35d4SDavid Howells { 1073a611c3cSRusty Russell printk(KERN_ERR "module %s: REL relocation unsupported\n", 1083a611c3cSRusty Russell module_name(me)); 109786d35d4SDavid Howells return -ENOEXEC; 110786d35d4SDavid Howells } 111786d35d4SDavid Howells #endif 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* Any final processing of module before access. Return -error or 0. */ 1141da177e4SLinus Torvalds int module_finalize(const Elf_Ehdr *hdr, 1151da177e4SLinus Torvalds const Elf_Shdr *sechdrs, 1161da177e4SLinus Torvalds struct module *mod); 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* Any cleanup needed when module leaves. */ 1191da177e4SLinus Torvalds void module_arch_cleanup(struct module *mod); 1201da177e4SLinus Torvalds 121d453cdedSRusty Russell /* Any cleanup before freeing mod->module_init */ 122d453cdedSRusty Russell void module_arch_freeing_init(struct module *mod); 123d3733e5cSAndrey Ryabinin 1240fea6e9aSAndrey Konovalov #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \ 1250fea6e9aSAndrey Konovalov !defined(CONFIG_KASAN_VMALLOC) 126d3733e5cSAndrey Ryabinin #include <linux/kasan.h> 127d3733e5cSAndrey Ryabinin #define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT) 128d3733e5cSAndrey Ryabinin #else 129d3733e5cSAndrey Ryabinin #define MODULE_ALIGN PAGE_SIZE 130d3733e5cSAndrey Ryabinin #endif 131d3733e5cSAndrey Ryabinin 1321da177e4SLinus Torvalds #endif 133