A Method for Dynamically Loading Functions in MCU

We have all heard of static libraries and dynamic libraries. Static libraries are quite familiar to everyone, especially in microcontroller development.

Dynamic libraries, however, are rarely seen in the microcontroller field. Today, I would like to share a method for dynamically loading functions, which is somewhat similar to dynamic libraries.

Project Address:

printf("hello world!");
A Method for Dynamically Loading Functions in MCU

Introduction

This project is a function library that implements dynamic loading capabilities on microcontrollers (e.g., STM32). It is similar to DLLs in Windows and SO files in Linux, allowing code to be dynamically loaded from other storage media into RAM.

Software Architecture

The project folder contains three parts: the common folder stores functions for generating relocatable .axf files and interacting with the dynamic loader, the src folder provides the source code for the dynamic loader, and the rel_axf_project_template provides a simple example of a relocatable .axf file project. The main functions of all files are as follows:

●/common/dl_extern_lib.h describes the base address of the function vector table for the app program to call the host program and some related macros.

●/common/dl_stdio_lib.h describes the index in the function vector table for functions in the C library stdio.h when called by the app program.

●/common/dl_stdlib_lib.h describes the index in the function vector table for functions in the C library stdlib.h when called by the app program.

●/common/dl_time_lib.h describes the index in the function vector table for functions in the C library time.h when called by the app program.

●/rel_axf_project_template/app/dl_stdio_lib.c redirects functions from the C library stdio.h called by the app program.

●/rel_axf_project_template/app/dl_stdlib_lib.c redirects functions from the C library stdlib.h called by the app program.

●/rel_axf_project_template/app/dl_time_lib.c redirects functions from the C library time.h called by the app program.

●/src/dl_arch.c is used for code data redirection and cache flushing, related to the chip architecture.

●/src/dl_elf.h is used for decoding ELF format files.

●/src/dl_lib.c contains the source code for dynamic loading implementation.

●/src/dl_lib.h provides functions for dynamic loading to be used by the application program.

●/src/dl_port.c implements the function interfaces that the dynamic loader host needs, primarily the files that need to be modified during porting.

●/src/dl_port.h declares the function interfaces that the dynamic loader host needs, including some macros that need to be modified based on your chip during porting.

●/src/dl_vector.c declares the functions provided by the host program for the app program, where the functions that need to be called by the app are declared.

Installation Tutorial

1. First, confirm whether your hardware platform is compatible. Currently, this program has only been tested on the Coretex-M7 kernel of the STMH743 microcontroller. Theoretically, Arm’s Coretex-M series can be used directly, while other Arm series chips may not have good support for relocation instructions and cache.

2. Check whether your chip uses cache. If cache is used, set the macro definition DL_CACHE_USE in /source/host/dl_port.h to 1.

3. Modify the implementation of related functions in /src/dl_port.c according to your software platform. This project uses the FatFs file system as an example. If the file system is different, the function implementation needs to be modified.

4. After porting, use rel_axf_project_template to generate the ELF file for the app program. The default path for the compiled ELF file is rel_axf_project_template/Objects/dll_generate.axf.

5. After using tools like fromelf to ensure that the generated ELF file is correct, you can happily proceed with dynamic loading.

Usage Instructions

1. In the code, first define a handle using DL_Handler, similar to a Windows DLL handle.

2. Load the generated dll_generate.axf into memory.

3. Include the dl_lib.h header file in the code, and use dl_load_lib to load the dll_generate.axf file into the handle while confirming that the return value is DL_NO_ERR.

4. After successful loading, you can use dl_get_func to obtain the starting address of the corresponding function in the library by function name.

5. You can use dl_get_entry to directly obtain the dl_main function in the ELF file.

After the execution of the dynamically loaded program is complete, you can use dl_destroy_lib to release the handle.

Source | Arm Technology School

Copyright belongs to the original author. If there is any infringement, please contact for deletion.


END







关于安芯教育





安芯教育是聚焦AIoT(人工智能+物联网)的创新教育平台,提供从中小学到高等院校的贯通式AIoT教育解决方案。
安芯教育依托Arm技术,开发了ASC(Arm智能互联)课程及人才培养体系。已广泛应用于高等院校产学研合作及中小学STEM教育,致力于为学校和企业培养适应时代需求的智能互联领域人才。


Leave a Comment