Dynamic linking method based on GD32 single-chip microcomputer
By adopting the dynamic linking method on the microcontroller, a .axf file with relocation section area is generated and loaded into memory before starting, which solves the problems of high resource consumption and low performance caused by static linking, and realizes the efficient operation of the microcontroller program and The convenience of library updates.
Patent Information
- Application Number
- CN202411470890.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-21
- Publication Date
- 2024-12-27
AI Technical Summary
Due to resource limitations, the generated execution files are large, the memory usage efficiency is low, and the static link library update requires overall compilation, high resource consumption and low performance.
Using the dynamic linking method based on GD32 microcontroller, the .axf file with relocation section area is generated by configuring and compiling in the integrated IDE, and loading it into memory before the system is started, and dynamic linking and running is achieved using dynamic loading functions. Dynamic loading and unloading of functions.
It improves the running efficiency of microcontroller programs, reduces memory usage and resource consumption during updates, just update the library files, and the program automatically loads the new version of the target file when it is run next time.
Smart Images

Figure CN119201286A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of computer control and relates to a dynamic linking method based on a GD32 single-chip microcomputer. Background Art
[0002] Program linking is divided into static linking and dynamic linking. Static linking is done by the linker adding the contents of the library to the executable program at link time. In dynamic linking, the library is loaded by the operating system loader when the executable file is loaded or run.
[0003] Static linking has little reliance on the operating environment and has better compatibility. At the same time, the generated program is larger, requires more system resources, and takes more time to load into memory. Once the library function is updated, the application must be recompiled.
[0004] Unlike static linking, dynamic linking does not have multiple copies of the same library in memory. Instead, these multiple programs share the same copy when they are executed. In addition, when updating a dynamic link, you only need to replace the original target file, without having to relink all the programs. When the program runs next time, the new version of the target file will be automatically loaded into memory and linked, and the program will have completed the upgrade goal. However, the disadvantage of dynamic linking is that because the link is postponed until the program is running, it needs to be linked every time the program is executed, so there will be a certain performance loss. Summary of the invention
[0005] 1. Technical issues to be resolved
[0006] Due to resource limitations, single-chip microcomputers usually use static linking. However, since static linking includes all static link libraries, the final executable file is relatively large, and the memory usage efficiency is low. Multiple modules are statically linked to the same library, and each module will copy its copy in the memory, which greatly occupies memory resources. In addition, when updating static link libraries, the entire library needs to be compiled, while dynamic linking only needs to update the library. Therefore, dynamic linking is a good choice for single-chip microcomputers with few resources and low performance.
[0007] (II) Technical solution
[0008] In order to solve the above technical problems, the present invention provides a dynamic linking method based on a GD32 single-chip microcomputer, which comprises the following steps:
[0009] S1: Create a project based on GD32H737 in the integrated IDE;
[0010] S2: Configure the LR_IROM1 load domain in the configuration file;
[0011] S3: In the IDE development environment, configure the parameters of the multi-function control Misc controls: base_platform, entry = dl_main, no_comment_section--no_debug, export_dynamic;
[0012] S4: Use the ARMCC compiler directive __attribute__((visibility("default"))) to compile the configuration parameters into dynamic symbols, and the upper-layer application is compiled into a dynamically loadable axf file;
[0013] S5: Before the system starts, the axf file is loaded from the flash to the memory through the assembly instruction.
[0014] In step S1, a scatter-loading file dllgen.sct with a suffix of .sct is created in the project directory, and the scatter-loading file dllgen.sct is loaded in the project configuration options. The scatter-loading file dllgen.sct allocates all codes and variables to corresponding physical addresses.
[0015] In step S2, the starting address of the LR_IROM1 loading domain is 0x00000000, which contains two running domains: ER_IROM1 and RW_IRAM1. After the loading file is configured, it is used as a parameter of armlink, and the generated AXF file allocates code and data according to the configuration file.
[0016] In step S3, the parameter base_platform turns on the linker base platform model, so that the generated .axf has dynamic information, that is, it has a relocation section. The parameter entry = dl_main is used to specify the initial entry point of the image file to ensure that the dynamic upper layer has a fixed entry address; for all underlying interfaces called by the upper layer, the function body pointer is assigned to an empty instruction during the compilation phase to ensure compilation, and then points to the real address of the underlying layer. The parameter no_comment_section--no_debug is used to reduce the size of the generated axf file. The parameter export_dynamic is used to export dynamic symbols. After it is turned on, a dynamic section consisting of a string and an offset address is added to the axf file.
[0017] Step S5 includes the following process:
[0018] (1) Define the handle dl_handler in the program, the type is an unsigned pointer type;
[0019] (2) Use the dynamic loading function dlopen to load the axf file, assign the return value to the handle dl_handler, and determine whether dl_handler is empty;
[0020] (3) After loading successfully, use dl_sym to obtain the address of the corresponding function in the library through the function name, and run it dynamically through the function pointer;
[0021] (4) Use dlclose to unload the dynamic library, and the parameter passed in is the handle dl_handler.
[0022] (III) Beneficial effects
[0023] The dynamic linking method based on the GD32 microcontroller provided by the above technical solution generates an .axf file with a relocation section by using an integrated IDE, and dynamically links and runs the file on the microcontroller, thereby realizing dynamic linking of functions and improving the running efficiency of the microcontroller program. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Figure 1 The present invention is a flow chart of a dynamic linking method based on a GD32 single-chip microcomputer. DETAILED DESCRIPTION
[0025] In order to make the purpose, content and advantages of the present invention more clear, the specific implementation methods of the present invention are further described in detail below in conjunction with the drawings and examples.
[0026] Reference Figure 1 As shown, the dynamic linking method based on the GD32 single-chip microcomputer in this embodiment includes the following steps:
[0027] S1: Create a project based on GD32H737 in the integrated IDE, create a scatter-loading file dllgen.sct with the suffix .sct in the project directory, and load the scatter-loading file dllgen.sct in the project configuration options. The scatter-loading file dllgen.sct allocates all codes and variables to the corresponding physical addresses.
[0028] S2: Configure the LR_IROM1 loading domain in the configuration file, with the starting address of 0x00000000, including two running domains: ER_IROM1 and RW_IRAM1. After the loading file is configured, it is used as a parameter of armlink, and the generated AXF file allocates code and data according to the configuration file.
[0029] S3: Configure the following parameters in the IDE development environment Misc controls
[0030] (1) Configuration parameter --base_platform turns on the linker base platform model so that the generated .axf file has dynamic information, that is, it has a relocation section.
[0031] (2) Configuration parameter --entry=dl_main is used to specify the initial entry point of the image file, ensuring that the dynamic upper layer has a fixed entry address. For all underlying interfaces called by the upper layer, the function body pointer is assigned a null instruction during the compilation phase to ensure compilation, and then points to the real address of the underlying layer.
[0032] (3)Configure the parameters --no_comment_section --no_debug to reduce the size of the generated axf file.
[0033] (4) The configuration parameter --export_dynamic is used to export dynamic symbols. After it is enabled, a dynamic section consisting of a string and an offset address is added to the axf file.
[0034] With the above configuration, the program can be called directly without going through the entry function.
[0035] S4: Use the ARMCC compiler directive __attribute__((visibility("default"))) to compile the configuration parameters into dynamic symbols, and the upper-level application is compiled into a dynamically loadable axf file.
[0036] S5: Before the system starts, the axf file is loaded from the flash to the memory through the assembly instruction.
[0037] (1) Define the handle dl_handler in the program, the type of which is an unsigned pointer type.
[0038] (2) Use the dynamic loading function dlopen to load the axf file, assign the return value to the handle dl_handler, and check whether dl_handler is empty.
[0039] (3) After successful loading, use dl_sym to obtain the address of the corresponding function in the library through the function name, and run it dynamically through the function pointer.
[0040] (4) Use dlclose to unload the dynamic library, and the parameter passed in is the handle dl_handler.
[0041] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the technical principles of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A dynamic linking method based on GD32 single-chip microcomputer, characterized in that: The following steps are involved: S1: Create a project based on GD32H737 in the integrated IDE; S2: Configure the LR_IROM1 load domain in the configuration file; S3: In the IDE development environment, configure the parameters of the multi-function control Misc controls: base_platform, entry = dl_main, no_comment_section--no_debug, export_dynamic; S4: Use the ARMCC compiler directive __attribute__((visibility("default"))) to compile the configuration parameters into dynamic symbols, and the upper-layer application is compiled into a dynamically loadable axf file; S5: Before the system starts, the axf file is loaded from the flash to the memory through the assembly instruction.
2. The dynamic linking method based on the GD32 single-chip microcomputer as claimed in claim 1, characterized in that: In step S1, a scatter-loading file dllgen.sct with a suffix of .sct is created in a project directory, and the scatter-loading file dllgen.sct is loaded in the project configuration option.
3. The dynamic linking method based on the GD32 single-chip microcomputer as claimed in claim 2, characterized in that: In step S1, the scatter-loading file dllgen.sct allocates all codes and variables to corresponding physical addresses.
4. The dynamic linking method based on the GD32 single-chip microcomputer as claimed in claim 3, characterized in that: In step S2, the starting address of the LR_IROM1 loading domain is 0x00000000, and it contains two running domains: ER_IROM1 and RW_IRAM1.
5. The dynamic linking method based on the GD32 single-chip microcomputer as claimed in claim 4, characterized in that: In step S2, after the loading file is configured, it is used as a parameter of armlink, and the generated AXF file allocates code and data according to the configuration file.
6. The dynamic linking method based on the GD32 single-chip microcomputer as claimed in claim 5, characterized in that: In step S3, the parameter base_platform turns on the linker base platform model, so that the generated .axf has dynamic information, that is, it has a relocation section.
7. The dynamic linking method based on the GD32 single-chip microcomputer as claimed in claim 6, characterized in that: In step S3, parameter entry=dl_main is used to specify the initial entry point of the image file to ensure that the dynamic upper layer has a fixed entry address; for all underlying interfaces called by the upper layer, the function body pointer is assigned a null instruction during the compilation phase to ensure compilation, and then points to the real address of the underlying layer.
8. The dynamic linking method based on the GD32 single-chip microcomputer as claimed in claim 7, characterized in that: In step S3, the parameter no_comment_section--no_debug is used to reduce the size of the generated axf file.
9. The dynamic linking method based on the GD32 single-chip microcomputer as claimed in claim 8, characterized in that: In step S3, the parameter export_dynamic is used to export dynamic symbols. After it is turned on, a dynamic section consisting of a string and an offset address is added to the axf file.
10. The dynamic linking method based on GD32 single-chip microcomputer as claimed in claim 9, characterized in that: Step S5 The process includes: (1) Define the handle dl_handler in the program, the type is an unsigned pointer type; (2) Use the dynamic loading function dlopen to load the axf file, assign the return value to the handle dl_handler, and determine whether dl_handler is empty; (3) After loading successfully, use dl_sym to obtain the address of the corresponding function in the library through the function name, and run it dynamically through the function pointer; (4) Use dlclose to unload the dynamic library, and the parameter passed in is the handle dl_handler.