Dynamic program loading method based on RISC-V

By converting RISC-V's static library into an independent executable project and adding dynamic calls to run interface functions to the original project, the problem of executable file size increase and relinking and compilation caused by static library updates is solved, dynamic loading and function expansion is achieved, and the flexibility and scalability of the system is improved.

CN120066613AActive Publication Date: 2025-05-30ZHEJIANG DALI TECH
View PDF 8 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

When using static libraries in the prior art, the executable file size will increase, and if the code in the static library needs to be updated, all programs that rely on the library will need to be relinked and compiled.

Method used

Provide a dynamic loader method based on RISC-V, converting the static library in the original project into an independent executable project. By modifying the link configuration file and environment initialization process, the executable project has the ability to return to the original project, and isolating its registers from the values ​​stored in the original project general register.

Benefits of technology

The original project dynamically loads programs in the executable project by calling the executable file generated by the executable project. The runtime can dynamically adjust the function pointer pointer according to actual needs, which facilitates function expansion and code optimization, improves the flexibility and scalability of the system, and reduces development and maintenance costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120066613A_ABST
    Figure CN120066613A_ABST
Patent Text Reader

Abstract

The invention relates to a dynamic program loading method based on RISC-V. A static library in an original project is converted into an independent executable project, and the method comprises the following steps: modifying a link configuration file of the executable project to specify a code address of the executable project; modifying an executable project environment initialization process code to enable the executable project to have the capability of returning to the original project, and enabling values stored in an executable project register and a general register of the original project to be mutually isolated; removing a static library file from the original project, and modifying a mode of calling a static library by a static library function name of the original project into a mode of dynamically calling an executable project by a function pointer; a dynamic calling operation interface function written by an assembly language is added in an original project, so that the original project can call an executable file generated by an executable project. The invention provides an efficient and stable method for dynamically loading the program in the CPU environment based on the RISC-V and without the MMU.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

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

[0002] In the software development process based on the Efinity RISC-V IDE, a static library is an archive file packed by a set 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 using a static library, the linker directly copies the code in the library into the final executable file, making the executable file contain all the code required for running, which enhances the independence and portability of the program. However, it also causes the increase in the volume of the executable file, and if the code in the static library needs to be updated, all programs depending on this library need to be re-linked and recompiled. Summary of the Invention

[0003] In view of the above analysis, embodiments of the present invention aim to provide a dynamic loading program method based on RISC-V to solve the problems that when using a static library in the prior art, the volume of the executable file will increase, and if the code in the static library needs to be updated, all programs depending on this library need to be re-linked and recompiled.

[0004] Embodiments of the present invention provide 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 code address of the executable project; modifying the code of the executable project environment initialization process to enable the executable project to have the ability to return to the original project, and to isolate the values stored in the registers of the executable project from those of the general-purpose registers of the original project;

[0005] removing the static library file from the original project, and modifying the way of calling the static library by the static library function name in the original project to the way of dynamically calling the executable project by function pointers;

[0006] adding a dynamic call running interface function written in assembly language in the original project to enable the original project to call the executable file generated by the executable project.

[0007] Furthermore, the dynamic call of the runtime interface function includes: setting the ra general-purpose register to store the address for the executable project to return to the original project, the a0 general-purpose register to store the entry address of the executable project code, and the a1 general-purpose register to store the function pointer table in the executable project; saving the values stored in the general-purpose registers except x0 and a0 to a custom array, and jumping to the entry address of the executable project code through a jump instruction; after the executable project finishes execution, returning to the next instruction after the jump instruction and restoring the values stored in all general-purpose registers and temporary registers saved to a custom array.

[0008] Furthermore, 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.

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

[0010] Furthermore, modifying the way of calling the static library by the original project with the static library function name to the way of dynamically calling the executable project with function pointers includes: adding an interface function for initializing function pointers in the original project.

[0011] Furthermore, adding an interface function for initializing function pointers in the original project includes: creating a structure array, and respectively assigning the addresses of the functions in the executable projects converted from the static library functions required to be called by the original project to each element in the structure array.

[0012] Furthermore, removing the static library file from the original project includes: removing the.a static library file from the original project.

[0013] Furthermore, modifying the code for the initialization process of the executable project environment to isolate the executable project registers from the general-purpose register parameters of the original project includes: pushing the values of the a0 and a1 general-purpose registers onto the stack during the initialization process code of the executable project environment, and restoring the values pushed onto the stack of the a0 and a1 general-purpose registers from the stack after the executable project finishes execution, and restoring the space of the stack pointer SP.

[0014] Furthermore, to isolate the values stored in the executable project registers from those in the general-purpose registers of the original project, it also includes: adding an option to turn off the GP global register in the compilation conditions.

[0015] Furthermore, modifying the code for the initialization process of the executable project environment to enable the executable project to have the ability to return to the original project includes: pushing the value of the ra general-purpose register onto the stack during the initialization process code of the executable project environment, and restoring the value pushed onto the stack of the ra general-purpose register from the stack after the executable project finishes execution, enabling the executable project to have the ability to return to the original project.

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

[0017] 1. For a method of dynamically loading a program based on RISC-V in the present invention, a dynamically callable runtime interface function written in assembly language is added to the original project, enabling the original project to call the executable file generated by the executable project; the static library file is removed from the original project, and the way of calling the static library by the original project using the static library function name is modified to a way of dynamically calling the executable project using function pointers; the original project realizes dynamically loading the program in the executable project by calling the executable file generated by the executable project, and can dynamically adjust the function pointer pointing according to actual needs during runtime, facilitating function expansion and code optimization, and helping the software system to run more efficiently and stably in a CPU environment without an MMU, enhancing the flexibility and scalability of the system.

[0018] 2. For a method of dynamically loading a program based on RISC-V in the present invention, the static library in the original project is transformed into an independent executable project, including: modifying the link configuration file of the executable project to specify the code address of the executable project; modifying the code for the initialization process of the executable project environment to enable the executable project to have the ability to return to the original project, and to isolate the values stored in the general registers of the executable project from those of the original project, and compiling and linking the executable project to generate an executable file, improving the independence and maintainability of the executable project module, enabling each static library to independently carry out development, testing, and release work, and reducing development and maintenance costs.

[0019] 3. For a method of dynamically loading a program based on RISC-V in the present invention, the static library file is removed from the original project, and the way of calling the static library by the original project using the static library function name is modified to a way of dynamically calling the executable project using function pointers; the main program of the original project can only see the function pointer, and the actual executable project code address is initialized during module loading, which can prevent illegal access and modification of the internal code of the executable project module from the outside, enhancing the security and stability of the system.

[0020] In the present invention, the above technical solutions can also be combined with each other to achieve more preferred combined solutions. Other features and advantages of the present invention will be described in the subsequent specification, and some advantages can be made obvious from the specification, or understood by implementing the present invention. The objectives and other advantages of the present invention can be realized and obtained through the content specifically pointed out in the specification and the drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] The drawings are only for the purpose of showing specific embodiments and are not considered as limiting the present invention. Throughout the drawings, the same reference signs denote the same components.

[0022] Figure 1 This is a flowchart of a method for a dynamic loading program based on RISC-V according to the present invention. Specific implementation manner

[0023] The following will specifically describe the preferred embodiments of the present invention with reference to the accompanying drawings. The accompanying drawings form a part of this application and are used together with the embodiments of the present invention to explain the principles of the present invention, rather than to limit the scope of the present invention.

[0024] A specific embodiment of the present invention discloses a method for a dynamic loading program based on RISC-V, as Figure 1 shown. The specific steps include S1 - S3.

[0025] S1. Convert 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 code address of the executable project; modifying the code of the executable project environment initialization process to enable the executable project to have the ability to return to the original project and isolate the values stored in the registers of the executable project from those of the general registers of the original project.

[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. This can enable each static library to be developed, tested, and released independently, reducing the development and maintenance costs.

[0028] For the sake of text description, two terms are defined: the original project and the executable project. That is, the original project code (for example, including 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] In this embodiment, the two independent projects are divided to enable the original project to call the executable project (multiple). In addition, the so-called dynamic loading means that the original static library code could only be linked with the original project during the compilation stage, but now after the original project runs, the code of the executable project can be loaded into the memory and then triggered to execute by the original project code.

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

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

[0032] This embodiment's modification of the defaul.ld table code includes:

[0033]

[0034] Modify the code for initializing the executable project environment to isolate the registers of the executable project from the general registers of the original project, including: pushing the values of general registers a0 and a1 onto the stack during the initialization process of the executable project environment, restoring the values of a0 and a1 pushed onto the stack from the stack after the execution of the executable project, and restoring the space of the stack pointer SP.

[0035] To isolate the values stored in the registers of the executable project from those of the general registers of the original project, it also includes: adding an option to turn off the GP global register in the compilation conditions.

[0036] Specifically, modify the start.S code of the executable project, which is the code for initializing the C language environment before entering main. The original start.S code was in an infinite loop after executing call main, believing that the main function in the executable project had finished execution and the task of the start.S code was completed without performing any operations. After being changed to an independently running module, the start.S code not only still needs to complete the initialization of the C language environment of the executable project itself, but also needs to receive some parameters from the function pointer table called by the main function of the original project. After entering the main function of the original project, the executable project must assign the address of its own interface function to this function pointer table. At the same time, the CPU execution right needs to be returned to the main program of the original project.

[0037] The modification of the start.S code includes, in sequence: protecting the three values ra, a0, and a1 passed in from the original project and pushing them into the stack space. ra is the return address of the original project, and a1 is the function pointer table in the original project. Before executing the call main instruction (returning to the original project), restore the values of ra, a0, and a1 from the stack space, and let the executable project obtain the table through the parameter argv of the main function main 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 of the start.S code in this embodiment includes:

[0039] Protect the three values ra, a0, and a1 passed in from the original project and push them into the stack space:

[0040] addi sp,sp,-16

[0041] sw ra,12(sp)

[0042] sw a0,8(sp)

[0043] sw a1,4(sp)

[0044] Restore the values of ra, a0, and a1 from the 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. Remove the static library files from the original project, and modify the way the original project calls the static library by the static library function name to the way of dynamically calling the executable project by function pointer.

[0051] Remove the static library files from the original project, including: removing the.a static library files from the original project.

[0052] Modify the way the original project calls the static library by the static library function name to the way of dynamically calling the executable project by function pointer, including: adding an interface function to initialize the function pointer in the original project.

[0053] The code for adding the interface function to initialize the function pointer in this embodiment includes:

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

[0055]

[0056] Define a function pointer table:

[0057]

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

[0059] Add an interface function to initialize the function pointer in the original project, including: creating a structure array, and each element in the structure array is assigned the address of each function in the executable project converted from the static library function that the original project needs to call.

[0060] Specifically, in the original project, a dynamic call runtime interface function written in assembly language is added. In this function, the general-purpose register ra stores the address for the executable project to return to the original project, the general-purpose register a0 stores the entry address of the executable project code, and the general-purpose register a1 stores the function pointer table in the executable project. The original project also enables the executable project to obtain the function pointer table of the executable project through the parameter argv of the main function in the original project via the newly added interface function, thus completing the transfer of the function pointer table from the original project to the executable project.

[0061] Modify the code for the initialization process of the executable project environment to enable the executable project to have the ability to return to the original project, including: pushing the value of the general-purpose register ra onto the stack during the initialization process code of the executable project environment, and restoring the value pushed onto the stack of the general-purpose register ra from the stack after the executable project finishes execution, so that the executable project has the ability to return to the original project.

[0062] Isolate the values stored in the general-purpose registers of the executable project from those of the original project's general-purpose registers, which also includes: adding an option to turn off the GP global register in the compilation conditions.

[0063] S3. Add a dynamic call runtime interface function written in assembly language in the original project to enable the original project to call the executable file generated by the executable project.

[0064] The functions of the dynamic call runtime interface function include: setting the general-purpose register ra to store the address for the executable project to return to the original project, the general-purpose register a0 to store the entry address of the executable project code, and the general-purpose register a1 to store the function pointer table in the executable project; saving the values stored in the general-purpose registers except x0 and a0 to a custom array, and jumping to the entry address of the executable project code via a jump instruction; after the executable project finishes execution, returning to the next instruction after the jump instruction and restoring the values stored in all the general-purpose registers and temporary registers saved to the custom array.

[0065] Specifically, through 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 values stored in the registers are isolated by the protection mechanisms of the original project and each executable project. However, x0 is a constant zero register and does not need to be backed up, and a0 is the return value of the executable project. When the executable project returns to the original project, it is not desired to restore the original value of a0 in the main program of the original project. Therefore, a0 does not need to be backed up. The executable project runs independently (similar to the dynamic library call method), and the original project needs to dynamically find the dynamic source of the executable project through the interface function initialized by the function pointer. In RISC-V, since there is no MMU, the dynamic library compiler cannot find the function programming dynamically. By modifying the defaul.ld table in this embodiment and defining it as the entry address of the executable project code, the original project can dynamically find the entry address of the executable project code, and the addresses of each function of the executable project are found through the function pointer table passed in through the a1 register to ensure its correct execution.

[0066] This function uses assembly to prevent the compiler from controlling the SP stack. The static library is changed to an independent executable project, which has an independent and complete C running environment, and all registers in the CPU need to be protected and isolated.

[0067] The dynamic call running interface function needs to be written in assembly code to manage the protection of the SP stack (the stack pointer SP is an 8-bit register whose value is the address of the stack top, that is, it points to the stack top, and SP is an indirect register for accessing the stack). A 124-byte array is established to store 30 general-purpose registers of RISC-V (except for the RISC-V general-purpose register x0, x0 is a constant zero register and does not need to be backed up, and the a0 register is not backed up as the return value) and a temporary register (t0). When the original project calls the executable project, the two projects share one SP register, so the environments of the two projects will affect each other. To avoid being affected, the t0 register is used to store the values of all general-purpose registers in the 124-byte array (at this time, a ra register is particularly important, which saves the return address of the dynamic call running interface function. At the same time, the executable project is also responsible for protecting the values of the general-purpose registers during operation. When calling, because the code address of the executable project is uncertain, the jalr long jump instruction jalr ra,0(a0) is used to execute. The dynamic call running interface function takes two parameters, one is a pointer variable a0 pointing to the code address of the executable project, and the other is a pointer variable a1 pointing to the function pointer table. After the jalr instruction is executed, the values in the 124-byte array are restored to all corresponding general-purpose registers (at this time, ra is also restored to the address that the original project should return to after running the executable file of the executable project), and the execution power of the CPU is returned to the original project (new) through the ret instruction.

[0068] Compared with the prior art, a RISC-V-based dynamic loading program method provided in this embodiment adds a dynamically called running interface function written in assembly language to the original project, enabling the original project to call the executable file generated by the executable project; the original project removes the static library file and changes the way of calling the static library by the static library function name in the original project to the way of dynamically calling the executable project by function pointer; it realizes that the original project dynamically loads the program in the executable project by calling the executable file generated by the executable project, and can dynamically adjust the function pointer pointing according to actual needs during operation, which is convenient for function expansion and code optimization, helps the software system to run more efficiently and stably in a CPU environment without MMU, and improves the flexibility and scalability of the system. The RISC-V-based dynamic loading program method provided in this embodiment 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 code address of the executable project; modifying the code of the executable project environment initialization process to enable the executable project to have the ability to return to the original project, and to isolate the values stored in the executable project registers from those in the general registers of the original project, compiling and linking the executable project to generate an executable file, which improves the independence and maintainability of the executable project module, enables each static library to independently carry out development, testing and release work, and reduces the development and maintenance costs. The RISC-V-based dynamic loading program method provided in this embodiment removes the static library file from the original project and changes the way of calling the static library by the static library function name in the original project to the way of dynamically calling the executable project by function pointer; the main program of the original project can only see the function pointer, and the actual executable project code address is initialized during module loading, which can prevent external illegal access and modification of the internal code of the executable project module, and enhance the security and stability of the system.

[0069] Those skilled in the art can understand that all or part of the processes of implementing the methods in the above embodiments can be completed by instructing relevant hardware through a computer program, and the program can be stored in a computer-readable storage medium. Among them, the computer-readable storage medium is a disk, an optical disc, a read-only memory or a random access memory, etc.

[0070] As mentioned above, the above are only the preferred specific embodiments of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by those skilled in the art within the technical scope disclosed by the present invention should be covered within the protection scope of the present invention.

Claims

1. A dynamic loader method based on RISC-V, characterized in that: Convert 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 to enable the executable project to return to the original project, and to isolate the values ​​stored in the executable project registers from the values ​​stored in the general registers of the original project; Remove the static library file from the original project, and change the original project's method of calling the static library by static library function name to a method of dynamically calling the executable project by function pointer; Add a dynamic call run interface function written in assembly language to the original project, so that the original project can call the executable file generated by the executable project.

2. The RISC-V-based dynamic loader method according to claim 1, characterized in that: The dynamic call operation interface 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; saving the values ​​stored in the general registers except x0 and a0 to a custom array, and jumping to the executable project code entry address through a jump instruction; after the executable project is executed, returning to the next instruction of the jump instruction, and restoring the values ​​stored in all general registers and temporary registers saved in a custom array.

3. The RISC-V-based dynamic loader method according to claim 1, characterized in that: 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.

4. The RISC-V-based dynamic loader method according to claim 1, characterized in that: The number of static libraries that the original project needs to call is equal to the number of converted independent executable projects.

5. The RISC-V-based dynamic loader method according to claim 1, characterized in that: The original project changes the method of calling the static library with the static library function name to the method of dynamically calling the executable project with the function pointer, including: the original project adds an interface function to initialize the function pointer.

6. The RISC-V-based dynamic loader method according to claim 5, characterized in that: The original project adds an interface function to initialize the function pointer, including: 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 that the original project needs to call.

7. The RISC-V-based dynamic loader method according to claim 1, characterized in that: The original project removes static library files, including: the original project removes .a static library files.

8. The RISC-V-based dynamic loader method according to claim 2, characterized in that: Modify the executable project environment initialization process code to isolate the executable project registers from the original project general register parameters, including: pushing the values ​​of the a0 and a1 general registers into the stack in the executable project environment initialization process code, restoring the values ​​of the a0 and a1 general registers pushed into the stack from the stack after the executable project is executed, and restoring the space of the stack pointer SP.

9. The RISC-V-based dynamic loader method according to claim 8, characterized in that: The executable project registers are isolated from the values ​​stored in the general registers of the original project, and also include: adding a GP global register closing option in the compilation conditions.

10. The RISC-V-based dynamic loader method according to claim 2, characterized in that: Modify the executable project environment initialization process code so that the executable project has the ability to return to the original project, including: pushing the 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 pushed into the stack from the stack after the executable project is executed, so that the executable project has the ability 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

  • A function integration method based on an iOS application

    CN109725924A

  • Executable program running method and device, electronic equipment and computer storage medium

    CN112631684A

  • Memory overflow defense method based on pointer encryption mechanism and RISC-V coprocessor

    CN113673002A