Module and ARM Module updates and fixes
Subsystems (4)
v2 cover Module and ARM Module updates and fixes
This series fixes various bugs and bit-rot issues with the module loading code. It also ports a couple of modules features from the Linux
kernel: arch specific section fixups, and module PLTs for ARM modules, to contain veneers for 'bl' instructions.
Changes since v1:
- Added #ifndef MODULE guards to init.h, and removed #undefine directives from module.h
- Created a weak function for module_frob_arch_sections default implementation
David Dgien (8):
Makefile: Initialize and export KBUILD variables
module: Add init macros to module.h
module: Fix adding module to list after layout
module: Fix module command registration
module: Implement HAVE_MOD_ARCH_SPECIFIC
arm: makefile: Fix compiler flag variable
arm: elf: Add THM relocation types
arm: module: Allow modules outside of bl range
Makefile | 12 +- arch/arm/Kconfig | 15 +++ arch/arm/Makefile | 6 +- arch/arm/cpu/Kconfig | 1 + arch/arm/include/asm/elf.h | 3 + arch/arm/include/asm/module.h | 33 ++++- arch/arm/lib32/Makefile | 1 + arch/arm/lib32/module-plts.c | 229 ++++++++++++++++++++++++++++++++++ arch/arm/lib32/module.c | 14 +++ arch/arm/lib32/module.lds | 4 + common/Kconfig | 7 ++ common/module.c | 68 ++++++---- include/asm-generic/module.h | 49 ++++++++ include/init.h | 26 ++-- include/module.h | 78 ++++++++++++ 15 files changed, 505 insertions(+), 41 deletions(-) create mode 100644 arch/arm/lib32/module-plts.c create mode 100644 arch/arm/lib32/module.lds create mode 100644 include/asm-generic/module.h
Signature · 7 lines
2.27.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v2 1/8 Makefile: Initialize and export KBUILD variables
Initialize and export KBUILD KERNEL and MODULE variables. Otherwise, appending to them elsewhere in the srctree will not be seen by the build commands in Makefile.lib
--- Makefile | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
Makefile +10 -2
@@ -397,6 +397,8 @@ BASH = bash CHECKFLAGS := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ -Wbitwise $(CF) CFLAGS_KERNEL = AFLAGS_KERNEL = +CFLAGS_MODULE = +AFLAGS_MODULE = LDFLAGS_MODULE = -T common/module.lds
@@ -420,6 +422,10 @@ KBUILD_CFLAGS := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \ -Werror=implicit-function-declaration -Werror=implicit-int \ -Os -pipe -Wmissing-prototypes -std=gnu89 KBUILD_AFLAGS := -D__ASSEMBLY__ +KBUILD_AFLAGS_KERNEL := +KBUILD_CFLAGS_KERNEL := +KBUILD_AFLAGS_MODULE := -DMODULE +KBUILD_CFLAGS_MODULE := -DMODULE LDFLAGS_barebox := -Map barebox.map
@@ -434,8 +440,10 @@ export HOSTCXX CHECK CHECKFLAGS export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS -export KBUILD_CFLAGS CFLAGS_KERNEL -export KBUILD_AFLAGS AFLAGS_KERNEL +export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE +export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE +export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE +export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL export LDFLAGS_barebox export LDFLAGS_pbl
Signature · 7 lines
2.27.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v2 2/8 module: Add init macros to module.h
In reply to David DgienPort module initialization macros and initcall redefinition macros from Linux
--- include/init.h | 26 ++++++++++++------ include/module.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 9 deletions(-)
include/init.h +17 -9
@@ -18,6 +18,21 @@typedef int (*initcall_t )(void );typedef void (*exitcall_t )(void ); +/* section for code used very early when +* - we're not running from where we linked at +* - bss not cleared +* - static variables not initialized +* +* Mainly useful for booting from NAND Controllers +*/ +#define __bare_init __section(.text_bare_init.text) + +#endif + +#ifndef MODULE + +#ifndef __ASSEMBLY__ +#define __define_initcall (level,fn,id) \ static initcall_t __initcall_##fn##id __attribute__((__used__)) \ __attribute__((__section__(".initcall." level))) = fn
@@ -58,16 +73,9 @@ typedef void (*exitcall_t)(void);#define archshutdown_exitcall (fn) __define_exitcall("5",fn,5)#define postarchshutdown_exitcall (fn) __define_exitcall("6",fn,6) -/* section for code used very early when -* - we're not running from where we linked at -* - bss not cleared -* - static variables not initialized -* -* Mainly useful for booting from NAND Controllers -*/ -#define __bare_init __section(.text_bare_init.text) +#endif /* __ASSEMBLY__ */ -#endif +#endif /* MODULE */ #endif /* _INIT_H */
include/module.h +71 -0
83 lines not on this page · raw message
Signature · 7 lines
2.27.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v2 3/8 module: Fix adding module to list after layout
In reply to David DgienDuring load_module(), the 'this_module' section is relocated, but the pointer to the module struct isn't updated to account account for the move. Do so before adding the module to the module_list.
As a side effect of properly pointing to the relocated module struct, we no longer need to manually search for and fixup the init_module symbol, so remove that code.
--- common/module.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-)
common/module.c +2 -9
@@ -297,13 +297,8 @@ struct module * load_module(void *mod_image, unsigned long len) } } -for (i =0 ; i < numsyms; i++) { -if (!strcmp (strtab + sym[i].st_name,MODULE_SYMBOL_PREFIX "init_module" )) { -printf ("found init_module() at 0x%08x\n" , sym[i].st_value); - module->init = (void *)sym[i].st_value; - } - } - +/* Module has been moved */ + module = (void *)sechdrs[modindex].sh_addr;list_add_tail (&module->list, &module_list);return module;
@@ -311,8 +306,6 @@ struct module * load_module(void *mod_image, unsigned long len) cleanup:if (ptr)free (ptr); -if (module) -free (module);return NULL ; }
Signature · 7 lines
2.27.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v2 4/8 module: Fix module command registration
In reply to David DgienCommit 1e4a948673d7 ("command: Use array of pointers to commands") updated the command linker sections to contain an array of pointers to the command structs, rather than the command structs themselves. However, the load_module function was not updated to account for the change to the linker section, so fix that here.
Additionally, commit 22bdecc9c591 ("lds: Move start/end address variables into defines") moved the _start and _end symbols out of the linker script and into the section macro defines, causing them to show up in the module symbol table as well, so we take can advantage of those to find the array of pointers.
--- common/module.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-)
common/module.c +29 -14
@@ -176,6 +176,34 @@ static void layout_sections( struct module *mod,debug ("core_size: %ld\n" , mod->core_size); } +static void register_module_cmds (Elf32_Shdr *sechdrs,const char *strtab,unsigned int symindex) +{ +Elf32_Sym *sym; +unsigned int numsyms; +unsigned int i; +struct command *const *cmd_start =NULL ; +struct command *const *cmd_end =NULL ; +struct command *const *cmd; + + numsyms = sechdrs[symindex].sh_size /sizeof (Elf32_Sym); + sym = (void *)sechdrs[symindex].sh_addr; + +for (i =0 ; i < numsyms; i++) { +if (strcmp (strtab + sym[i].st_name,MODULE_SYMBOL_PREFIX "__barebox_cmd_start" ) ==0 ) + cmd_start = (struct command *const *)sym[i].st_value; + +if (strcmp (strtab + sym[i].st_name,MODULE_SYMBOL_PREFIX "__barebox_cmd_end" ) ==0 ) + cmd_end = (struct command *const *)sym[i].st_value; + } + +if (cmd_start && cmd_end) { +debug ("found __barebox_cmd_start at 0x%08x\n" , (uint32_t )cmd_start); +for (cmd = cmd_start; cmd != cmd_end; cmd++) { +register_command (*cmd); + } + } +} +LIST_HEAD (module_list);struct module *load_module (void *mod_image,unsigned long len)
@@ -183,8 +211,6 @@ struct module * load_module(void *mod_image, unsigned long len)struct module *module =NULL ;Elf32_Ehdr *ehdr;/* Elf header structure pointer */ Elf32_Shdr *sechdrs;/* Section header structure pointer */ -Elf32_Sym *sym; -unsigned int numsyms;char *strtab =0 ;/* String table pointer */ int i;/* Loop counter */ unsigned int strindex =0 ;
@@ -193,7 +219,6 @@ struct module * load_module(void *mod_image, unsigned long len)char *secstrings;void *ptr =NULL ;int err; -int cmdindex;if (len <sizeof (*ehdr))return NULL ;
@@ -285,17 +310,7 @@ struct module * load_module(void *mod_image, unsigned long len)apply_relocate_add (sechdrs, strtab, symindex, i, module); } - numsyms = sechdrs[symindex].sh_size /sizeof (Elf32_Sym); - sym = (void *)sechdrs[symindex].sh_addr; - - cmdindex =find_sec (ehdr, sechdrs, secstrings,".barebox_cmd" ); -if (cmdindex) { -struct command *cmd =(struct command *)sechdrs[cmdindex].sh_addr; -for (i =0 ; i < sechdrs[cmdindex].sh_size /sizeof (struct command ); i++) { -register_command (cmd); - cmd++; - } - } +register_module_cmds (sechdrs, strtab, symindex);/* Module has been moved */ module = (void *)sechdrs[modindex].sh_addr;
Signature · 7 lines
2.27.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v2 5/8 module: Implement HAVE_MOD_ARCH_SPECIFIC
In reply to David DgienImplement HAVE_MOD_ARCH_SPECIFIC Kconfig and module_frob_arch_sections() function prototype from Linux module subsystem. module_frob_arch_sections() should be implemented by any architecture that selects HAVE_MOD_ARCH_SPECIFIC, and is called from load_module()
--- common/Kconfig | 7 ++++++ common/module.c | 14 +++++++++++ include/asm-generic/module.h | 49 ++++++++++++++++++++++++++++++++++++ include/module.h | 7 ++++++ 4 files changed, 77 insertions(+) create mode 100644 include/asm-generic/module.h
common/Kconfig +7 -0
@@ -326,6 +326,13 @@ config MODULES way to compile modules and the list of exported symbols to actually make use of modules is short to nonexistent +config HAVE_MOD_ARCH_SPECIFIC + bool + help + The arch uses struct mod_arch_specific to store data. Many arches + just need a simple module loader without arch specific data - those + should not enable this. + config KALLSYMS depends on HAS_KALLSYMS bool "kallsyms"
common/module.c +14 -0
@@ -176,6 +176,14 @@ static void layout_sections( struct module *mod,debug ("core_size: %ld\n" , mod->core_size); } +int __weak module_frob_arch_sections (Elf_Ehdr *hdr, +Elf_Shdr *sechdrs, +char *secstrings, +struct module *mod) +{ +return 0 ; +} +static void register_module_cmds (Elf32_Shdr *sechdrs,const char *strtab,unsigned int symindex) {Elf32_Sym *sym;
@@ -271,6 +279,12 @@ struct module * load_module(void *mod_image, unsigned long len) goto cleanup; } +/* Allow arches to frob section contents and sizes. */ + err =module_frob_arch_sections (ehdr, sechdrs, + secstrings, module); +if (err <0 ) + goto cleanup; +/* Determine total sizes, and put offsets in sh_entsize. For now this is done generically; there doesn't appear to be any special cases for the architectures. */
include/asm-generic/module.h new file +49 -0
49 lines not on this page · raw message
include/module.h +7 -0
@@ -122,6 +122,13 @@ int apply_relocate_add(Elf_Shdr *sechdrs,unsigned int symindex, unsigned int relsec, struct module *mod); + +/* Adjust arch-specific sections. Return 0 on success. */ +int module_frob_arch_sections (Elf_Ehdr *hdr, +Elf_Shdr *sechdrs, +char *secstrings, +struct module *mod); +#endif /* CONFIG_MODULES */ extern struct list_head module_list;
Signature · 7 lines
2.27.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v2 6/8 arm: makefile: Fix compiler flag variable
In reply to David DgienIn order for ARM modules to compile with the correct relocation types, they must be built without -fPIE. Move -fPIE from KBUILD_CPPFLAGS to KBUILD_CFLAGS_KERNEL so that the flag is only included when compiling files being built into the barebox image.
--- arch/arm/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/Makefile +1 -1
@@ -137,7 +137,7 @@ LDFLAGS_barebox += --gc-sections LDFLAGS_pbl += --gc-sections # early code often runs at addresses we are not linked at -KBUILD_CPPFLAGS += -fPIE +KBUILD_CFLAGS_KERNEL += -fPIE ifdef CONFIG_RELOCATABLE LDFLAGS_barebox += -pie
Signature · 7 lines
2.27.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v2 7/8 arm: elf: Add THM relocation types
In reply to David Dgien--- arch/arm/include/asm/elf.h | 3 +++ 1 file changed, 3 insertions(+)
arch/arm/include/asm/elf.h +3 -0
@@ -30,6 +30,9 @@ typedef struct user_fp elf_fpregset_t;#define R_ARM_CALL 28#define R_ARM_JUMP24 29 +#define R_ARM_THM_CALL 10 +#define R_ARM_THM_JUMP24 30 +/* * These are used to set parameters in the core dumps. */
Signature · 7 lines
2.27.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v2 8/8 arm: module: Allow modules outside of bl range
In reply to David DgienUnlike the Linux kernel, barebox does not have a dedicated heap for storing modules. Therefore, if the system memory configuration places the general heap further away than can be reached by a 'bl' instruction (24 bits of address, or 16 MiB), then the module relocations will fail due to being out of range.
Allocate PLTs when loading modules so that jumps and calls whose targets are too far away for their relative offsets to be encoded in the instructions themselves can be bounced via veneers in the module's PLT. The modules will use slightly more memory, but after rounding up to page size, the actual memory footprint is usually the same.
Adoption of Linux commits:
66e94ba3c8ea ARM: kernel: avoid brute force search on PLT generation 1031a7e674d1 ARM: kernel: sort relocation sections before allocating PLTs 05123fef0982 ARM: kernel: allocate PLT entries only for external symbols 35fa91eed817 ARM: kernel: merge core and init PLTs 7d485f647c1f ARM: 8220/1: allow modules outside of bl range
--- arch/arm/Kconfig | 15 +++ arch/arm/Makefile | 4 + arch/arm/cpu/Kconfig | 1 + arch/arm/include/asm/module.h | 33 ++++- arch/arm/lib32/Makefile | 1 + arch/arm/lib32/module-plts.c | 229 ++++++++++++++++++++++++++++++++++ arch/arm/lib32/module.c | 14 +++ arch/arm/lib32/module.lds | 4 + 8 files changed, 295 insertions(+), 6 deletions(-) create mode 100644 arch/arm/lib32/module-plts.c create mode 100644 arch/arm/lib32/module.lds
arch/arm/Kconfig +15 -0
@@ -477,4 +477,19 @@ config ARM_PSCI_DEBUG putc function. Only use for debugging. +config ARM_MODULE_PLTS + bool "Use PLTs to allow loading modules placed far from barebox image" + depends on MODULES + select QSORT + help + Allocate PLTs when loading modules so that jumps and calls whose + targets are too far away for their relative offsets to be encoded + in the instructions themselves can be bounced via veneers in the + module's PLT. The modules will use slightly more memory, but after + rounding up to page size, the actual memory footprint is usually + the same. + + Say y if your memory configuration puts the heap to far away from the + barebox image, causing relocation out of range errors + endmenu
arch/arm/Makefile +4 -0
@@ -16,6 +16,10 @@ KBUILD_CPPFLAGS += -mlittle-endian LD += -EL endif +ifeq ($(CONFIG_ARM_MODULE_PLTS),y) +LDFLAGS_MODULE += -T $(srctree)/arch/arm/lib32/module.lds +endif + # Unaligned access is not supported when MMU is disabled, so given how # at least some of the code would be executed with MMU off, lets be # conservative and instruct the compiler not to generate any unaligned
arch/arm/cpu/Kconfig +1 -0
@@ -6,6 +6,7 @@ config PHYS_ADDR_T_64BIT config CPU_32 bool select HAS_MODULES + select HAVE_MOD_ARCH_SPECIFIC select HAS_DMA select HAVE_PBL_IMAGE
arch/arm/include/asm/module.h +27 -6
@@ -1,13 +1,34 @@#ifndef _ASM_ARM_MODULE_H#define _ASM_ARM_MODULE_H -struct mod_arch_specific -{ -int foo; +#include <asm-generic/module.h> + +struct unwind_table ; + +#ifdef CONFIG_ARM_UNWIND +enum { +ARM_SEC_INIT , +ARM_SEC_DEVINIT , +ARM_SEC_CORE , +ARM_SEC_EXIT , +ARM_SEC_DEVEXIT , +ARM_SEC_HOT , +ARM_SEC_UNLIKELY , +ARM_SEC_MAX , +}; +#endif + +struct mod_arch_specific { +#ifdef CONFIG_ARM_UNWIND +struct unwind_table *unwind[ARM_SEC_MAX ]; +#endif +#ifdef CONFIG_ARM_MODULE_PLTS +struct elf32_shdr *plt; +int plt_count; +#endif }; -#define Elf_Shdr Elf32_Shdr -#define Elf_Sym Elf32_Sym -#define Elf_Ehdr Elf32_Ehdr +struct module ; +u32 get_module_plt (struct module *mod,unsigned long loc,Elf32_Addr val);#endif /* _ASM_ARM_MODULE_H */
arch/arm/lib32/Makefile +1 -0
@@ -23,6 +23,7 @@ obj-$(CONFIG_ARM_OPTIMZED_STRING_FUNCTIONS) += memset.o obj-$(CONFIG_ARM_UNWIND) += unwind.o obj-$(CONFIG_ARM_SEMIHOSTING) += semihosting-trap.o semihosting.o obj-$(CONFIG_MODULES) += module.o +obj-$(CONFIG_ARM_MODULE_PLTS) += module-plts.o extra-y += barebox.lds pbl-y += lib1funcs.o
arch/arm/lib32/module-plts.c new file +229 -0
229 lines not on this page · raw message
arch/arm/lib32/module.c +14 -0
@@ -64,6 +64,20 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, offset -=0x04000000 ; offset += sym->st_value - loc; + +/* +* Route through a PLT entry if 'offset' exceeds the +* supported range. Note that 'offset + loc + 8' +* contains the absolute jump target, i.e., +* @sym + addend, corrected for the +8 PC bias. +*/ +if (IS_ENABLED (CONFIG_ARM_MODULE_PLTS ) && + (offset <= (s32 )0xfe000000 || + offset >= (s32 )0x02000000 )) + offset =get_module_plt (module, loc, + offset + loc +8 ) + - loc -8 ; +if (offset &3 || offset <= (s32 )0xfe000000 || offset >= (s32 )0x02000000 ) {
arch/arm/lib32/module.lds new file +4 -0
@@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +SECTIONS { + .plt : { BYTE(0) } +}
Signature · 7 lines
2.27.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Sascha Hauer
AppliedQuoted 11 lines · David Dgien
This series fixes various bugs and bit-rot issues with the module loading code. It also ports a couple of modules features from the Linux
kernel: arch specific section fixups, and module PLTs for ARM modules, to contain veneers for 'bl' instructions.Changes since v1:
- Added #ifndef MODULE guards toinit.h, and removed #undefine directives frommodule.h
- Created a weak function formodule_frob_arch_sectionsdefault implementation
Quoted 9 lines
David Dgien (8):
Makefile: Initialize and export KBUILD variables
module: Add init macros tomodule.h
module: Fix adding module to list after layout
module: Fix module command registration
module: ImplementHAVE_MOD_ARCH_SPECIFIC
arm: makefile: Fix compiler flag variable
arm: elf: Add THM relocation types
arm: module: Allow modules outside of bl range
Applied, thanks
Sascha
Signature · 9 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox