A dynamic loading program method based on RISC-V

By converting static libraries into independent executable projects and dynamically calling them through function pointers, the problems of increased executable file size and updates caused by static libraries are solved, improving the system's flexibility and security, and reducing development and maintenance costs.

CN120066613BActive Publication Date: 2025-12-26ZHEJIANG DALI TECH
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510217222.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-02-26
Publication Date
2025-12-26
Estimated Expiration
2045-02-26

AI Technical Summary

Technical Problem

The use of static libraries in existing technologies leads to an increase in executable file size, and updates to static library code require all dependent programs to be relinked and recompiled, resulting in a lack of flexibility and independence.

Method used

The static library is converted into an independent executable project. By modifying the linker configuration file and environment initialization code, the executable project is made capable of returning to the original project. The executable project is then dynamically called through function pointers. Static library files are removed, and dynamic call to the runtime interface function is added to achieve dynamic loading.

Benefits of technology

It achieves the independence and flexibility of executable files, reduces development and maintenance costs, enhances system security and stability, and is suitable for CPU environments without an MMU.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120066613B_ABST
    Figure CN120066613B_ABST
Patent Text Reader

Abstract

The application relates to a RISC-V-based dynamic loading program method, which converts a static library in an original project into an independent executable project, and comprises the following steps: modifying a link configuration file of the executable project to specify an executable project code address; modifying an executable project environment initialization process code, so that the executable project has the capability of returning to the original project, and the executable project register and the value stored in the general register of the original project are isolated from each other; the original project removes a static library file, and modifies a static library calling mode in the original project according to a static library function name into a dynamic calling executable project mode according to a function pointer; and a dynamic calling running interface function written in an assembly language is added to the original project, so that the original project can call an executable file generated by the executable project. The application provides a high-efficiency and stable dynamic loading program method under a RISC-V CPU environment without MMU.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of dynamic loading program, and particularly relates to a dynamic loading program method based on RISC-V. BACKGROUND

[0002] In the software development process based on Efinity RISC-V IDE, a static library is an archive file packaged by a group of object files. In the software development process, the static library can provide some common functions and code modules for other programs to call. When the static library is used, the linker will copy the code in the library directly to the final executable file, so that the executable file contains all the required code for running, enhancing the independence and portability of the program. However, it will also cause the executable file to increase in size, and if the code in the static library needs to be updated, all programs that depend on the library need to be re-linked and compiled. SUMMARY

[0003] In view of the above analysis, the embodiment of the present application aims to provide a dynamic loading program method based on RISC-V, to solve the problem that the use of static library in the prior art will cause the executable file to increase in size, and if the code in the static library needs to be updated, all programs that depend on the library need to be re-linked and compiled.

[0004] The embodiment of the present application provides a dynamic loading program method based on RISC-V, which converts the static library in the original project into an independent executable project, including: modifying the link configuration file of the executable project to specify the executable project code address; modifying the executable project environment initialization process code, so that the executable project has the ability to return to the original project, and the executable project registers and the values stored in the general registers of the original project are isolated from each other;

[0005] The original project removes the static library file, and modifies the way of calling the static library by the original project in the form of static library function name to the way of dynamically calling the executable project in the form of function pointer;

[0006] A dynamic call running interface function written in assembly language is added in the original project, so that the original project can call the executable file generated by the executable project.

[0007] Further, the dynamic calling of the function of the interface of the running program includes: setting the address of the return of the original program in the ra general register storing the executable program, the address of the code entry of the executable program in the a0 general register, and the function pointer table in the executable program in the a1 general register; saving the values stored in the general registers except x0 and a0 into a self-defined array, jumping to the code entry address of the executable program through a jump instruction; returning to the next instruction of the jump instruction after the executable program is executed, and restoring the values stored in all general registers and temporary registers saved in the self-defined array.

[0008] Further, the modification of the link configuration file of the executable program to specify the executable program code address includes: modifying the executable program code address to unoccupied space.

[0009] Further, the number of static libraries required to be called by the original program is equal to the number of independent executable programs.

[0010] Further, the modification of the original program from the static library function name calling static library mode to the function pointer dynamic calling executable program mode includes: adding an interface function for initializing the function pointer in the original program.

[0011] Further, the interface function for initializing the function pointer added in the original program includes: creating a structure array, and assigning each element in the structure array to the function address in the executable program converted from the static library function required to be called by the original program.

[0012] Further, the removal of the static library file in the original program includes: removing the.a static library file in the original program.

[0013] Further, the modification of the executable program environment initialization process code to isolate the registers of the executable program from the general register parameters of the original program includes: pushing the values of the a0 and a1 general registers into the stack in the executable program environment initialization process code, restoring the values of the a0 and a1 general registers pushed into the stack after the executable program is executed, and restoring the space of the stack pointer SP.

[0014] Further, the isolation of the values stored in the registers of the executable program from the general registers of the original program also includes: adding a GP global register option in the compilation condition.

[0015] Further, the modification of the executable program environment initialization process code to enable the executable program to return to the original program includes: pushing the value of the ra general register into the stack in the executable program environment initialization process code, restoring the value of the ra general register pushed into the stack after the executable program is executed, and enabling the executable program to return to the original program.

[0016] Compared with the prior art, the present application can achieve at least one of the following beneficial effects:

[0017] 1. The RISC-V-based dynamic loading program method increases a dynamic call running interface function written in assembly language in the original project, so that the original project can call an executable file generated by an executable project; the original project removes a static library file, and modifies a static library function name call to a static library to a function pointer dynamic call to an executable project; the original project can dynamically load a program in the executable project by calling the executable file generated by the executable project, the function pointer can be dynamically adjusted according to actual needs during running, the function extension and code optimization are facilitated, the software system can be more efficiently and stably run in a CPU environment without MMU, and the flexibility and expandability of the system are improved.

[0018] 2. The RISC-V-based dynamic loading program method converts a static library in the original project into an independent executable project, including: modifying a link configuration file of the executable project to specify an executable project code address; modifying an executable project environment initialization process code, so that the executable project has the ability to return to the original project, and the values stored in the registers of the executable project and the general registers of the original project are isolated from each other; the executable project is compiled and linked to generate an executable file, the independence and maintainability of the executable project module are improved, each static library can independently develop, test and publish, and the development and maintenance costs are reduced.

[0019] 3. The RISC-V-based dynamic loading program method removes a static library file in the original project, and modifies a static library function name call to a static library to a function pointer dynamic call to an executable project; the original project main program can only see the function pointer, and the actual executable project code address is initialized when the module is loaded, so that illegal access and modification of the internal code of the executable project module by the outside can be prevented, and the security and stability of the system are enhanced.

[0020] In the present application, the above technical solutions can be combined with each other to achieve more preferred combination solutions. Other features and advantages of the present application will be described in the subsequent specification, and some advantages will become apparent from the specification, or will be understood by implementing the present application. The purposes and other advantages of the present application can be achieved and obtained from the contents specifically pointed out in the specification and the drawings. BRIEF DESCRIPTION OF DRAWINGS

[0021] The accompanying drawings are included to provide a further understanding of the embodiments, and are incorporated in and constitute a part of this specification, illustrate embodiments of the application, and together with the description serve to explain the principles of the application.

[0022] Figure 1 A RISC-V-based dynamic loading program method flowchart. DETAILED DESCRIPTION

[0023] The preferred embodiments of the present application will be described in detail below with reference to the drawings, which form a part of this application. The drawings illustrate embodiments of the application and serve to explain the principle of the application, but are not intended to limit the scope of the application.

[0024] One embodiment of the present application discloses a RISC-V-based dynamic loading program method, as shown in the figure. The specific steps include S1-S3. Figure 1

[0025] S1, converting the static library in the original project into an independent executable project, including: modifying the link configuration file of the executable project to specify the executable project code address; modifying the executable project environment initialization process code, so that the executable project has the ability to return to the original project, and the executable project registers and the values stored in the general registers of the original project are isolated from each other.

[0026] The number of static libraries required to be called by the original project is equal to the number of converted independent executable projects.

[0027] Specifically, each static library corresponds to an independent executable project. In this way, each static library can be developed, tested and released independently, reducing development and maintenance costs.

[0028] For the sake of textual description, two terms are defined, the original project and the executable project, that is, the original project code (for example, containing a static library) is divided into two: the original project code (new) and the executable project code, and two independent projects are also generated: the original project and the executable project.

[0029] The two independent projects divided in this embodiment realize that the original project can call the executable project (which can be multiple). In addition, the so-called dynamic loading means that the original static library code can only be linked with the original project in the compilation phase, and now the executable project code can be loaded into the memory after the original project is running, and then the original project code triggers its execution.

[0030] Modifying the link configuration file of the executable project to specify the executable project code address includes: modifying the executable project code address to an unoccupied space.

[0031] Specifically, since the memory of the CPU is uniformly distributed, the address space of each module needs to be pre-allocated, and by modifying the defaul.ld table, the executable project code entry address is defined, and the occupied space range is occupied.

[0032] The defaul.ld table code modified in this embodiment includes:​

[0033]

[0034] Modify the executable project environment initialization process code to isolate the executable project registers from the original project general-purpose register parameters. This includes: pushing the values ​​of general-purpose registers a0 and a1 onto the stack in the executable project environment initialization process code; restoring the values ​​of general-purpose registers a0 and a1 from the stack after the executable project finishes execution; and restoring the space of the stack pointer SP.

[0035] To isolate the values ​​stored in the executable project registers from those in the original project's general-purpose registers, the compiler also includes adding an option to disable the GP global register in the compilation conditions.

[0036] Specifically, the executable project's startup code, start.S, is modified. This code initializes the C language environment before entering main. The original start.S code, after executing `call main`, enters an infinite loop, assuming the main function in the executable project has finished executing, and its task is complete; it then stops performing operations. However, after being changed to an independently running module, start.S not only still initializes the executable project's own C language environment but also receives parameters from the function pointer table called by the original project's main function. Upon entering the original project's main function, the executable project must assign its own interface function addresses to this function pointer table. Simultaneously, it must return CPU execution to the original project's main program.

[0037] The modifications to the start.S code include: protecting the three values ​​ra, a0, and a1 passed from the original project and pushing them onto the stack; ra being the address of the returned original project and a1 being the function pointer table in the original project; restoring the values ​​of ra, a0, and a1 from the stack before executing the call main instruction (returning to the original project); allowing the executable project to obtain the table through the argv parameter of the main function of the original project; completing the transfer of the table from the original project to the executable project; and handing it over to the original project's BIN_Init() for concretization.

[0038] The modified code in start.S of this embodiment includes:

[0039] The three values ​​ra, a0, and a1 passed from the original project are protected and pushed onto the stack:

[0040] addi sp,sp,-16

[0041] sw ra,12(sp)

[0042] sw a0,8(sp)

[0043] sw a1, 4(sp)

[0044] Restore ra, a0, a1 values from stack space:

[0045] lw a0, 8(sp)

[0046] lw a1, 4(sp)

[0047] lw ra, 12(sp)

[0048] addi sp, sp, 16

[0049] ret

[0050] S2, the original project removes static library files, the original project with static library function name call static library way modified to function pointer dynamic call executable project way.

[0051] The original project removes static library files, including: the original project removes.a static library files.

[0052] The original project with static library function name call static library way modified to function pointer dynamic call executable project way, including: the original project adds an interface function to initialize the function pointer.

[0053] The interface function to initialize the function pointer added in the embodiment includes:

[0054] Define a structure array, the member is the address of the function pointer

[0055]

[0056] Define a function pointer table:

[0057]

[0058] The BIN_Init() is used for concretization because the function pointer table is located in the original project, only the table address is known, and the address of each function is unknown.

[0059] The interface function to initialize the function pointer added in the original project includes: creating a structure array, and assigning each element in the structure array to the address of each function in the executable project converted from the static library function to be called by the original project.

[0060] Specifically, in the original project, the assembly language is used to write a dynamic calling interface function to set the ra general register to store the address of the executable project returning to the original project, the a0 general register to store the executable project code entry address, and the a1 general register to store the function pointer table in the executable project. The original project further obtains the executable project function pointer table through the original project main parameter argv by adding a new interface function, and completes the transfer of the function pointer table from the original project to the executable project.

[0061] The executable project environment initialization process code is modified to enable the executable project to return to the original project, including: the value of the ra general register is pushed into the stack in the executable project environment initialization process code, and the value of the ra general register pushed into the stack is recovered after the executable project is executed, so that the executable project has the ability to return to the original project.

[0062] The executable project registers are isolated from the values stored in the original project general registers, and further including: the GP global register option is closed in the compilation condition.

[0063] S3, a dynamic calling interface function written in assembly language is added in the original project, so that the original project can call the executable file generated by the executable project.

[0064] The dynamic calling interface function function includes: setting the ra general register to store the address of the executable project returning to the original project, the a0 general register to store the executable project code entry address, and the a1 general register to store the function pointer table in the executable project; the values stored in the general registers except x0 and a0 are saved in a self-defined array, and the executable project code entry address is jumped to through a jump instruction; after the executable project is executed, it returns to the next instruction of the jump instruction, and the values stored in all general registers and temporary registers saved in the self-defined array are recovered.

[0065] Specifically, by steps S1 and S2, the original project and one or more independent executable projects have been created; since RISC V has 32 general-purpose registers, it is necessary to ensure that the general-purpose registers are isolated in the protection mechanism of the original project and each executable project to store the values of the registers, but x0 is a constant zero register and does not need to be backed up, and a0 is the return value of the executable project, which is not expected to be restored to the original value of a0 of the original project main program when the executable project returns to the original project, so a0 does not need to be backed up. The executable project runs independently (similar to the dynamic library calling mode), and the original project dynamically finds the source end of the executable project through the interface function initialized by the function pointer. In RISC V, there is no MMU, and the dynamic library compiler cannot find the function programming dynamic, so the defaul.ld table is modified in this embodiment to define the executable project code entry address, so that the original project can dynamically find the executable project code entry address, and the function pointer table is transmitted through the a1 register to find the executable project function address, so that it can be executed correctly.

[0066] This function uses assembly to prevent the compiler from controlling the SP stack, and the static library is changed to an independent executable project with a complete C runtime environment, which requires protection and isolation of all registers in the CPU.

[0067] Dynamic calling and running of the interface function need to be written by assembly code, and the SP stack (the stack pointer SP is an 8-bit register, and its value is the address of the top of the stack, that is, it points to the top of the stack, and the SP is an interval address register for accessing the stack) needs to be protected and managed, and a 124-byte array is established to store 30 general-purpose registers of RISC-V (except RISC-V general-purpose registers x0, x0 is a constant zero register and does not need to be backed up, and a0 register is not backed up as a return value) and a temporary register (t0). When the original project calls the executable project, the two projects share an SP register, so the environments of the two projects will affect each other, in order not to be affected, the values of all general-purpose registers are stored in the 124-byte array using the t0 register (at this time, the ra register is particularly important, which saves the return address of the dynamically called and run interface function, and the executable project is also responsible for protecting the values of the general-purpose registers when running, and since the executable project code address is uncertain, the jalr long jump instruction jalr ra, 0(a0) is executed. Dynamic calling and running of the interface function have two parameters, one is a pointer variable a0 pointing to the executable project code address, and the other is a pointer variable a1 pointing to the function pointer table. After the execution of the jalr instruction, the values of all general-purpose registers are recovered from the 124-byte array (at this time, ra is also restored to the address returned after the executable project runs the executable file of the original project), and the execution right of the CPU is returned to the original project (new) through the ret instruction.

[0068] Compared with the prior art, the RISC-V-based dynamic loading program method provided by the embodiment increases a dynamic call running interface function written in an assembly language in an original project, so that the original project can call an executable file generated by an executable project; the original project removes a static library file, and modifies a static library function name calling manner of the original project to a function pointer dynamic calling executable project manner; the original project is enabled to dynamically load a program in the executable project by calling the executable file generated by the executable project, the function pointer can be dynamically adjusted according to actual needs during running, function expansion and code optimization are facilitated, the software system can be more efficiently and stably run in a CPU environment without MMU, and the flexibility and scalability of the system are improved. The RISC-V-based dynamic loading program method provided by the embodiment converts a static library in the original project into an independent executable project, including: modifying a link configuration file of the executable project to specify an executable project code address; modifying an executable project environment initialization process code, so that the executable project has the ability to return to the original project, and the executable project register and the value stored in the general register of the original project are isolated from each other; the executable project is compiled and linked to generate an executable file, the independence and maintainability of the executable project module are improved, each static library can independently develop, test and publish, and the development and maintenance costs are reduced. The RISC-V-based dynamic loading program method provided by the embodiment removes the static library file in the original project, and modifies the static library function name calling manner of the original project to the function pointer dynamic calling executable project manner; the original project main program can only see the function pointer, and the actual executable project code address is initialized when the module is loaded, so that illegal access and modification of the executable project module internal code by an external device can be prevented, and the security and stability of the system are enhanced.

[0069] Those skilled in the art can understand that all or part of the processes of the above-mentioned embodiments can be completed by a computer program instructing related hardware, and the program can be stored in a computer readable storage medium, such as a magnetic disk, an optical disk, a read-only memory or a random access memory.

[0070] The above description is only a preferred embodiment of the present application, but the protection scope of the present application is not limited thereto, and any person skilled in the art can easily think of changes or replacements within the technical range disclosed by the present application, which should be covered within the protection scope of the present application.

Claims

1. A RISC-V based dynamic loader method, characterized in that, The static library in the original project is converted into an independent executable project, including: modifying a link configuration file of the executable project to specify an executable project code address; modifying an executable project environment initialization process code to enable the executable project to return to the original project and to isolate registers of the executable project from values stored in general registers of the original project; The original project removes static library files and modifies a static library calling mode of the original project by using a function name to a dynamic executable project calling mode by using a function pointer; A dynamic calling running interface function written in an assembly language is added to the original project to enable the original project to call an executable file generated by the executable project; The function of the dynamic calling running interface function includes: setting the ra general register to store an address of the executable project returning to the original project, the a0 general register to store an executable project code entry address, and the a1 general register to store a function pointer table in the executable project; saving values stored in general registers except x0 and a0 into a self-defined array, and jumping to the executable project code entry address through a jump instruction; and returning to a next instruction of the jump instruction after the executable project is executed, and restoring values stored in all general registers and temporary registers saved into the self-defined array.

2. The RISC-V based dynamic loader method of claim 1, wherein, The link configuration file of the executable project is modified to specify the executable project code address, including: modifying the executable project code address to an unoccupied space.

3. The RISC-V based dynamic loader method of claim 1, wherein, The number of static libraries required to be called by the original project is equal to the number of independent executable projects converted.

4. The RISC-V based dynamic loader method of claim 1, wherein, The original project is modified from a static library calling mode by using a function name to a dynamic executable project calling mode by using a function pointer, including: adding an interface function for initializing the function pointer to the original project.

5. The RISC-V based dynamic loader method of claim 4, wherein, The interface function for initializing the function pointer added to the original project includes: creating a structure array, and assigning each element in the structure array to a function address in the executable project of the static library function required to be called by the original project.

6. The RISC-V based dynamic loader method of claim 1, wherein, The original project removes static library files, including: removing.a static library files in the original project.

7. The RISC-V based dynamic loader method of claim 1, wherein, The executable project environment initialization process code is modified to isolate registers of the executable project from values stored in general registers of the original project, including: pressing values of the a0 and a1 general registers into a stack in the executable project environment initialization process code, restoring the values of the a0 and a1 general registers pressed into the stack after the executable project is executed, and restoring a space of a stack pointer SP.

8. The RISC-V based dynamic loader method of claim 7, wherein, The registers of the executable project are also isolated from the values stored in the general registers of the original project, including: adding a GP global register option to be closed in a compilation condition.

9. The RISC-V based dynamic loader method of claim 1, wherein, The executable project environment initialization process code is modified to enable the executable project to return to the original project, including: pressing a value of the ra general register into the stack in the executable project environment initialization process code, and restoring the value of the ra general register pressed into the stack after the executable project is executed to enable the executable project to return to the original project.

Citation Information

Patent Citations

  • Dividing method of source code in software engineering

    CN101826014A

  • Payment terminal application simulation method and device

    CN107193628A