A controller stack invocation method, device, apparatus and storage medium
By placing the function stack into the read-only segment of the executable program during compilation and accessing the data area using relocation, the problem of function stack overruns in industrial control systems is solved, enabling normal operation and flexible configuration of the controller and avoiding hardware upgrade costs.
Patent Information
- Application Number
- CN202211693888.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-28
- Publication Date
- 2026-08-25
- Estimated Expiration
- 2042-12-28
AI Technical Summary
In existing technologies, controllers in industrial control systems occupy a large amount of function stack space due to the use of structure and array type parameters, which makes the function stack prone to exceeding limits, thus restricting the performance and flexibility of the controller, and increasing the cost of hardware upgrades.
During compilation, the function stack is placed in the read-only segment of the executable program. The data area of the read-only segment is accessed through relocation to replace the function stack, thus enabling parameter passing.
The stack data transfer problem was solved at the compilation level, ensuring the normal operation of the control system, reducing the need for hardware upgrades, and supporting flexible controller configuration.
Smart Images

Figure CN115934213B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of industrial control, and in particular to a controller stack invocation method, apparatus, device, and storage medium. Background Technology
[0002] In industrial public service systems, the engineering programs run by the controllers come from the configuration software on the host computer. Common function parameter passing in configuration programs requires the use of the program stack. For simple data type parameter passing, the function stack used is relatively small. However, the size of parameters of structure type and array type is unpredictable. Therefore, the function stack may be filled by the parameters used by a function.
[0003] Controllers in industrial control systems are embedded devices with limited internal resources and small program stacks. However, as control programs become increasingly complex, the need for more complex data transfers grows. Current technologies limit the function stack to a maximum value by restricting the number of function parameters and function call depth, or by upgrading existing hardware to expand the function stack and increase parameter transfer capabilities, thus ensuring normal controller operation. However, this approach limits controller performance and increases operating costs. Therefore, how to handle the function stack issues during parameter transfer of structures and arrays to meet complex data transfer requirements has become a pressing problem in the field of industrial control. Summary of the Invention
[0004] In view of this, the purpose of this invention is to provide a controller stack invocation method, apparatus, device, and storage medium, which allows the function stack to be placed in the read-only segment of the executable program during compilation. Access to the data area of the read-only segment is achieved through relocation, replacing the function stack's occupancy. This solves the stack data transfer problem at the compilation level, ensuring the normal operation of the control system. The specific solution is as follows:
[0005] Firstly, this application provides a controller stack invocation method applied to industrial controllers, including:
[0006] Write the preset type function data corresponding to the currently executed function into the target stack space of the industrial controller, and write the target function parameters corresponding to the currently executed function into the reserved stack space in the read-only segment of the preset executable program to obtain the target executable program;
[0007] The target executable program is compiled to generate machine code, and the machine code is linked to generate a corresponding executable file; the executable file includes relocation code generated based on the target function parameters in the machine code;
[0008] Based on the relocation code in the executable file, the address of the target function parameter in the reserved stack space is determined as the load address of the target executable program;
[0009] The target function parameters are read from the reserved stack space through the loading address, and the currently executing function is run using the read target function parameters and the preset type function data in the target stack space to perform the corresponding industrial control operation.
[0010] Optionally, before writing the target function parameters corresponding to the currently executing function into the reserved stack space in the read-only segment of the preset executable program, the method further includes:
[0011] Determine the space occupied by the function parameters corresponding to the currently executing function;
[0012] Determine whether the occupied space exceeds a preset threshold. If so, filter out the target function parameters that exceed the preset threshold from the function parameters. The preset threshold is set according to the stack out-of-bounds condition of the target stack space.
[0013] Optionally, writing the target function parameters corresponding to the currently executing function into the reserved stack space in the read-only segment of the preset executable program includes:
[0014] By modifying the compiler code of the preset executable program, the target function parameters corresponding to the currently executing function are written into the reserved stack space in the read-only segment of the preset executable program.
[0015] Optionally, compiling the target executable program into machine code includes:
[0016] The preset object file is updated according to the target function parameters written into the reserved stack space to obtain the target object file, and the target executable program is compiled based on the target object file to generate machine code.
[0017] Optionally, the process of linking the machine code to generate the corresponding executable file includes:
[0018] Read the target object file and generate relocation code corresponding to the target function parameters based on the target object file.
[0019] Optionally, linking the machine code to generate the corresponding executable file includes:
[0020] In the preset linker script, the process stack trace instruction is set at the starting address of the read-only segment in the machine code and linked to generate the corresponding executable file.
[0021] Optionally, the address of the target function parameter in the reserved stack space is the starting address of the reserved stack space;
[0022] Accordingly, determining the address of the target function parameter in the reserved stack space as the load address of the target executable program includes:
[0023] The starting address of the reserved stack space is determined as the loading address of the target executable program, and the address of the read-only segment in the executable file is updated to the loading address according to the process stack trace instructions.
[0024] Secondly, this application provides a controller stack invocation device for industrial controllers, comprising:
[0025] The data writing module is used to write the preset type function data corresponding to the currently executed function into the target stack space of the industrial controller, and write the target function parameters corresponding to the currently executed function into the reserved stack space in the read-only segment of the preset executable program to obtain the target executable program;
[0026] The program compilation module is used to compile the target executable program to generate machine code, and link the machine code to generate a corresponding executable file; the executable file includes relocation code generated based on the target function parameters in the machine code;
[0027] The address determination module is used to determine the address of the target function parameter in the reserved stack space as the load address of the target executable program based on the relocation code in the executable file.
[0028] The function execution module is used to read the target function parameters in the reserved stack space through the loading address, and use the read target function parameters and the preset type function data in the target stack space to run the currently executing function to perform corresponding industrial control operations.
[0029] Thirdly, this application provides an electronic device, which includes a processor and a memory; wherein the memory is used to store a computer program, which is loaded and executed by the processor to implement the aforementioned controller stack calling method.
[0030] Fourthly, this application provides a computer-readable storage medium for storing a computer program that, when executed by a processor, implements the aforementioned controller stack invocation method.
[0031] In this application, preset type function data corresponding to the currently executed function is written into the target stack space of the industrial controller, and the target function parameters corresponding to the currently executed function are written into the reserved stack space in the read-only segment of the preset executable program to obtain the target executable program; the target executable program is compiled to generate machine code, and the machine code is linked to generate the corresponding executable file; the executable file includes relocation code generated based on the target function parameters in the machine code; according to the relocation code in the executable file, the address of the target function parameters in the reserved stack space is determined as the load address of the target executable program; the target function parameters in the reserved stack space are read through the load address, and the currently executed function is run using the read target function parameters and the preset type function data in the target stack space to perform the corresponding industrial control operation. Therefore, this application avoids the limitation of parameter specification on the configuration flexibility of the control system and the problem of increased equipment cost due to hardware upgrades by placing the function stack in the read-only segment of the executable program during the compilation process and accessing the data area of the read-only segment through relocation. It solves the problem of stack data transfer at the compilation level, ensures the normal operation of the control system, and allows the program to run with a small amount of stack space, supporting flexible configuration. Attached Figure Description
[0032] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.
[0033] Figure 1 A flowchart of a controller stack invocation method is provided in this application;
[0034] Figure 2 A controller stack call flowchart is provided for this application;
[0035] Figure 3 This application provides a specific controller stack call flowchart;
[0036] Figure 4 A controller code execution flowchart is provided for this application;
[0037] Figure 5 A flowchart of a specific controller stack invocation method is provided in this application;
[0038] Figure 6This application provides a specific controller code execution flowchart;
[0039] Figure 7 This application provides a schematic diagram of a controller stack relocation process;
[0040] Figure 8 This application provides a schematic diagram of a controller stack calling device structure;
[0041] Figure 9 This application provides a structural diagram of an electronic device. Detailed Implementation
[0042] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0043] In industrial control systems, as control programs become more complex, more complex data transfer is required. However, the controller's stack size is fixed, so as the space occupied by data gradually increases, the controller's operation becomes less stable. While existing parameter specifications can ensure normal program operation, they limit configuration flexibility, and upgrading hardware increases costs. This application addresses this issue by placing the function stack into the read-only segment of the executable program during compilation and accessing the data area of the read-only segment through relocation, thus replacing the function stack's footprint. This solves the stack data transfer problem at the compilation level, allowing program execution with a smaller stack space.
[0044] See Figure 1 As shown, this embodiment of the invention discloses a controller stack invocation method, applied to an industrial controller, including:
[0045] Step S11: Write the preset type function data corresponding to the current execution function into the target stack space of the industrial controller, and write the target function parameters corresponding to the current execution function into the reserved stack space in the read-only segment of the preset executable program to obtain the target executable program.
[0046] Understandably, during the execution of the program in the controller, the parameters, return values, and local variables of each running function occupy the system's stack space. The controller's stack size is fixed, and the maximum value of the program stack inside the controller in an industrial control system is very small. However, the function parameters change according to the logic of the program configuration, so the maximum value of the controller's stack space can be reached quickly during configuration. For example, assuming the stack space size corresponding to the controller's control code is 64K, then a 64K function parameter being passed will prevent the controller from functioning correctly. Figure 2 As shown, if there is a function call chain A(...)->B(...)->C(...) and the currently executing function is C(...), the controller can only run normally if the parameter passed from function B to C is less than 64K, but this is usually difficult to achieve in practice.
[0047] Therefore, in this embodiment, the preset type function data corresponding to the currently executing function can be written into the target stack space of the industrial controller. Furthermore, by modifying the compiler code of the preset executable program, the target function parameters corresponding to the currently executing function can be written into the reserved stack space in the read-only segment of the preset executable program, thus obtaining the target executable program. It is understood that the preset type function data can be pre-defined by the programmer, i.e., function parameter types that typically occupy less space, such as... Figure 3 The function local variables and return values are stored in the function. For function parameters passed between functions, they are written to the reserved stack space in the read-only segment of the preset executable program. It should be noted that before writing function parameters to the reserved stack space in the read-only segment of the preset executable program, the space occupied by the function parameters corresponding to the currently executing function can be determined first; then, it is determined whether the space occupied by the function parameters exceeds a preset threshold. If so, target function parameters exceeding the preset threshold are selected from the function parameters. The preset threshold can be set according to the stack out-of-bounds condition of the target stack space. It is understood that, based on this, the space occupied by the above-mentioned preset type function data can also be judged. The specific parameter type judged can be set according to different function running conditions, which will not be specifically discussed in this embodiment. By writing target function parameters whose space exceeds the preset threshold to the reserved stack space in the read-only segment of the preset executable program, it is possible to avoid the controller malfunctioning when passing function parameters whose space exceeds the stack space. Furthermore, it allows for flexible control of the function parameter types written to the reserved stack space in the read-only segment of the executable program, making the storage of function parameters more flexible.
[0048] Step S12: Compile the target executable program to generate machine code, and link the machine code to generate a corresponding executable file; the executable file includes relocation code generated based on the target function parameters in the machine code.
[0049] In this embodiment, as Figure 4 As shown, for code to become an executable program and complete execution, it needs to be compiled into machine code, then linked into an executable file, and finally loaded into memory by a loader for execution. In this embodiment, the compilation and relocation processes are particularly noteworthy. During compilation, the target stack used by the currently executing function is placed into the read-only segment of the executable program. During linking, the information requiring relocation—namely, the target function parameters written into the reserved stack space in the read-only segment of the executable program—is used to generate relocation code. Based on this, the linking of the machine code is completed. By generating relocation code, the addresses of the target function parameters can be determined during the subsequent loading stage for program execution.
[0050] Step S13: Based on the relocation code in the executable file, determine the address of the target function parameter in the reserved stack space as the load address of the target executable program.
[0051] In this embodiment, as Figure 3 As shown, the address of the target function parameters stored in the reserved stack space is determined as the load address of the target executable program based on the relocation code.
[0052] Step S14: Read the target function parameters in the reserved stack space through the loading address, and use the read target function parameters and the preset type function data in the target stack space to run the current execution function to perform the corresponding industrial control operation.
[0053] In this embodiment, the target function parameters are read from the reserved stack space using the load address determined in the above steps, for example... Figure 3 As shown, when function A finishes running and obtains the parameters to be passed to function B, instead of directly storing the parameters on the target stack, the parameters, i.e., the target function parameters, are stored in reserved stack space, and the corresponding address is determined as the load address of the target executable program. In this way, after function B receives the address passed by function A, it reads the target function parameters through the received address and completes its execution. By passing the corresponding addresses of function parameters between functions instead of directly passing the function parameters, the space usage of the target stack is effectively reduced, and the uncontrolled expansion of the stack space is minimized, ensuring the normal operation of the controller when passing function parameters that occupy a large amount of space.
[0054] Therefore, in this embodiment, the preset type function data corresponding to the currently executed function is written into the target stack space of the industrial controller, and the target function parameters corresponding to the currently executed function are written into the reserved stack space in the read-only segment of the preset executable program to obtain the target executable program; the target executable program is compiled to generate machine code, and the machine code is linked to generate the corresponding executable file; the executable file includes relocation code generated based on the target function parameters in the machine code; according to the relocation code in the executable file, the address of the target function parameters in the reserved stack space is determined as the loading address of the target executable program; the target function parameters in the reserved stack space are read through the loading address, and the currently executed function is run using the read target function parameters and the preset type function data in the target stack space to perform the corresponding industrial control operation. The above technical solution can compress the stack space of the business code within a controllable range, control the size of the stack space used according to the program specifications, and put the memory that originally belonged to the stack space in the business code into a read-only segment to prevent it from being tampered with in the program, and limit the rampant expansion of the business stack through specifications.
[0055] As can be seen from the previous embodiment, this application can write the target function parameters corresponding to the currently executing function into the reserved stack space in the read-only segment of the preset executable program to reduce the consumption of target stack space. Next, this embodiment will describe in detail the specific method for reading function parameters from the reserved stack space. See [link to documentation]. Figure 5 As shown in the figure, this application discloses a controller stack invocation method, including:
[0056] Step S21: Write the preset type function data corresponding to the current execution function into the target stack space of the industrial controller, and write the target function parameters corresponding to the current execution function into the reserved stack space in the read-only segment of the preset executable program to obtain the target executable program.
[0057] Step S22: Update the preset object file according to the target function parameters written into the reserved stack space to obtain the target object file, and compile the target executable program based on the target object file to generate machine code.
[0058] In this embodiment, to read function parameters from the reserved stack space, the data of the target function parameters in the read-only segment needs to be modified to the load address of the executable program during the relocation phase, such as... Figure 6 As shown, during the program compilation phase, the information requiring relocation, i.e., the target function parameters written to the reserved stack space, is placed into a preset object file. It can be understood that the preset object file, i.e., the .o file, contains the target function parameters. Figure 6 The .text and .rodata sections in the target file are used to compile the target executable program to generate machine code, so that relocation code can be generated during the linking process based on the aforementioned information that needs to be relocated.
[0059] Step S23: Read the target object file in the machine code, generate relocation code corresponding to the target function parameters based on the target object file, and set the trace process stack instruction in the preset linker script to the starting address of the read-only segment in the machine code and link it to generate the corresponding executable file.
[0060] In this embodiment, relocation code corresponding to the target function parameters is generated based on the target object file. Then, during the linking process, the starting address of the read-only segment in the machine code is traced from the process stack instructions. Figure 7 As shown, the linker script specifies the starting address of the g_pStack instruction in the read-only segment to generate the corresponding executable file, so that the program can be run by specifying the load address as the starting address of the reserved stack space through the g_pStack instruction.
[0061] Step S24: Based on the relocation code in the executable file, determine the starting address of the reserved stack space as the loading address of the target executable program, and update the address of the read-only segment in the executable file to the loading address according to the trace process stack instructions.
[0062] In this embodiment, the address of the target function parameter in the reserved stack space is the starting address of the reserved stack space. Therefore, during the relocation process before the target executable program runs, the address of the read-only segment is modified to the starting address of the reserved stack space based on the g_pStack instruction, and the function can run normally.
[0063] Step S25: Read the target function parameters in the reserved stack space through the loading address, and run the currently executing function using the read target function parameters and the preset type function data in the target stack space to perform the corresponding industrial control operation.
[0064] For more detailed processing procedures of steps S21 and S25 in this embodiment, please refer to the corresponding content disclosed in the previous embodiments, which will not be repeated here.
[0065] Therefore, in this embodiment, the preset type function data corresponding to the currently executed function is written to the target stack space, and the target function parameters are written to the reserved stack space in the read-only segment of the preset executable program to obtain the target executable program. The preset object file is updated according to the target function parameters to obtain the target object file, and machine code is generated based on the target object file. The target object file in the machine code is read, relocation code is generated, and the trace process stack instruction is set in the preset linker script to the starting address of the read-only segment in the machine code to generate the corresponding executable file. According to the relocation code in the executable file, the starting address of the reserved stack space is determined as the load address of the target executable program, and the address of the read-only segment in the executable file is updated to the load address according to the trace process stack instruction. The target function parameters in the reserved stack space are read through the load address, and the currently executed function is run using the read target function parameters and the preset type function data in the target stack space to perform the corresponding industrial control operation. By changing the parameter passing method to pointer passing during compilation, the function parameters are saved to a specified location, that is, the reserved stack space saved in the read-only segment memory is used to save the target function parameters. Then, by tracing the process stack instructions to set the program load address to complete the function execution, the normal operation of the controller can be completed using a small amount of stack space, while realizing the flexible configuration of the controller.
[0066] See Figure 8 As shown in the illustration, this application also discloses a controller stack invocation device, applied to an industrial controller, comprising:
[0067] The data writing module 11 is used to write the preset type function data corresponding to the currently executed function into the target stack space of the industrial controller, and write the target function parameters corresponding to the currently executed function into the reserved stack space in the read-only segment of the preset executable program to obtain the target executable program;
[0068] The program compilation module 12 is used to compile the target executable program to generate machine code, and link the machine code to generate a corresponding executable file; the executable file includes relocation code generated based on the target function parameters in the machine code;
[0069] Address determination module 13 is used to determine the address of the target function parameter in the reserved stack space as the loading address of the target executable program based on the relocation code in the executable file;
[0070] The function execution module 14 is used to read the target function parameters in the reserved stack space through the loading address, and use the read target function parameters and the preset type function data in the target stack space to run the currently executing function to perform corresponding industrial control operations.
[0071] In this embodiment, the preset type function data corresponding to the currently executed function is written into the target stack space of the industrial controller, and the target function parameters corresponding to the currently executed function are written into the reserved stack space in the read-only segment of the preset executable program to obtain the target executable program; the target executable program is compiled to generate machine code, and the machine code is linked to generate the corresponding executable file; the executable file includes relocation code generated based on the target function parameters in the machine code; according to the relocation code in the executable file, the address of the target function parameters in the reserved stack space is determined as the load address of the target executable program; the target function parameters in the reserved stack space are read through the load address, and the currently executed function is run using the read target function parameters and the preset type function data in the target stack space to perform the corresponding industrial control operation. Therefore, the above technical solution can place the function stack into the read-only segment of the executable program during the compilation process, and access the data area of the read-only segment through relocation to replace the function stack. This solves the problem of stack data transfer at the compilation level, ensures the normal operation of the control system, and allows the use of a small amount of stack space, supporting flexible configuration.
[0072] In some specific embodiments, the data writing module 11 further includes:
[0073] A space determination unit is used to determine the space occupied by the function parameters corresponding to the currently executed function;
[0074] A space judgment unit is used to determine whether the occupied space exceeds a preset threshold. If so, it filters out target function parameters that exceed the preset threshold from the function parameters. The preset threshold is set according to the stack out-of-bounds condition of the target stack space.
[0075] In some specific embodiments, the data writing module 11 specifically includes:
[0076] The parameter writing unit is used to write the target function parameters corresponding to the currently executing function into the reserved stack space in the read-only segment of the preset executable program by modifying the compiler code of the preset executable program.
[0077] In some specific embodiments, the program compilation module 12 specifically includes:
[0078] The file update unit is used to update the preset object file according to the target function parameters written to the reserved stack space to obtain the target object file, and compile the target executable program based on the target object file to generate machine code.
[0079] In some specific embodiments, the program compilation module 12 specifically includes:
[0080] The code generation module is used to read the target object file and generate relocation code corresponding to the target function parameters based on the target object file.
[0081] In some specific embodiments, the program compilation module 12 specifically includes:
[0082] The instruction setting unit is used to set the trace process stack instructions in the preset linker script to the starting address of the read-only segment in the machine code and link them to generate the corresponding executable file.
[0083] In some specific embodiments, the address determination module 13 specifically includes:
[0084] The address determination unit is used to determine the starting address of the reserved stack space as the loading address of the target executable program, and update the address of the read-only segment in the executable file to the loading address according to the trace process stack instructions.
[0085] Furthermore, embodiments of this application also disclose an electronic device, Figure 9 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content of the diagram should not be construed as limiting the scope of this application.
[0086] Figure 9 This is a schematic diagram of the structure of an electronic device 20 provided in an embodiment of this application. Specifically, the electronic device 20 may include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 stores a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the controller stack calling method disclosed in any of the foregoing embodiments. Alternatively, the electronic device 20 in this embodiment may specifically be an electronic computer.
[0087] In this embodiment, the power supply 23 is used to provide operating voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and external devices, and the communication protocol it follows can be any communication protocol applicable to the technical solution of this application, and is not specifically limited here; the input / output interface 25 is used to acquire external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs, and is not specifically limited here.
[0088] In addition, the memory 22, as a carrier for resource storage, can be a read-only memory, random access memory, disk or optical disk, etc. The resources stored thereon can include operating system 221, computer program 222, etc., and the storage method can be temporary storage or permanent storage.
[0089] The operating system 221 is used to manage and control the various hardware devices on the electronic device 20 and the computer program 222, which may be Windows Server, Netware, Unix, Linux, etc. In addition to including a computer program capable of performing the controller stack call method executed by the electronic device 20 as disclosed in any of the foregoing embodiments, the computer program 222 may further include a computer program capable of performing other specific tasks.
[0090] Furthermore, this application also discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, it implements the aforementioned controller stack invocation method. The specific steps of this method can be found in the corresponding content disclosed in the foregoing embodiments, and will not be repeated here.
[0091] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to in the method section.
[0092] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0093] The steps of the methods or algorithms described in conjunction with the embodiments disclosed herein can be implemented directly by hardware, a software module executed by a processor, or a combination of both. The software module can be located in random access memory (RAM), main memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium known in the art.
[0094] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0095] The technical solutions provided in this application have been described in detail above. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the methods and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.
Claims
1. A controller stack invocation method, characterized in that, Applications in industrial controllers, including: Write the preset type function data corresponding to the currently executed function into the target stack space of the industrial controller, and write the target function parameters corresponding to the currently executed function into the reserved stack space in the read-only segment of the preset executable program to obtain the target executable program; The target executable program is compiled to generate machine code, and the machine code is linked to generate a corresponding executable file; the executable file includes relocation code generated based on the target function parameters in the machine code; Based on the relocation code in the executable file, the address of the target function parameter in the reserved stack space is determined as the load address of the target executable program; The target function parameters are read from the reserved stack space through the loading address, and the currently executing function is run using the read target function parameters and the preset type function data in the target stack space to perform corresponding industrial control operations. Before writing the target function parameters corresponding to the currently executing function into the reserved stack space in the read-only segment of the preset executable program, the method further includes: Determine the space occupied by the function parameters corresponding to the currently executing function; Determine whether the occupied space exceeds a preset threshold. If so, filter out the target function parameters that exceed the preset threshold from the function parameters. The preset threshold is set according to the stack out-of-bounds condition of the target stack space.
2. The controller stack invocation method according to claim 1, characterized in that, The step of writing the target function parameters corresponding to the currently executing function into the reserved stack space in the read-only segment of the preset executable program includes: By modifying the compiler code of the preset executable program, the target function parameters corresponding to the currently executing function are written into the reserved stack space in the read-only segment of the preset executable program.
3. The controller stack invocation method according to claim 1, characterized in that, The process of compiling the target executable program into machine code includes: The preset object file is updated according to the target function parameters written into the reserved stack space to obtain the target object file, and the target executable program is compiled based on the target object file to generate machine code.
4. The controller stack invocation method according to claim 3, characterized in that, The process of linking the machine code to generate the corresponding executable file includes: Read the target object file and generate relocation code corresponding to the target function parameters based on the target object file.
5. The controller stack invocation method according to any one of claims 1 to 4, characterized in that, The linking of the machine code to generate the corresponding executable file includes: In the preset linker script, the process stack trace instruction is set at the starting address of the read-only segment in the machine code and linked to generate the corresponding executable file.
6. The controller stack invocation method according to claim 5, characterized in that, The address of the objective function parameter in the reserved stack space is the starting address of the reserved stack space; Accordingly, determining the address of the target function parameter in the reserved stack space as the load address of the target executable program includes: The starting address of the reserved stack space is determined as the loading address of the target executable program, and the address of the read-only segment in the executable file is updated to the loading address according to the process stack trace instructions.
7. A controller stack invocation device, characterized in that, Applications in industrial controllers, including: The data writing module is used to write the preset type function data corresponding to the currently executed function into the target stack space of the industrial controller, and write the target function parameters corresponding to the currently executed function into the reserved stack space in the read-only segment of the preset executable program to obtain the target executable program; The program compilation module is used to compile the target executable program to generate machine code, and link the machine code to generate a corresponding executable file; the executable file includes relocation code generated based on the target function parameters in the machine code; The address determination module is used to determine the address of the target function parameter in the reserved stack space as the load address of the target executable program based on the relocation code in the executable file. The function execution module is used to read the target function parameters in the reserved stack space through the loading address, and use the read target function parameters and the preset type function data in the target stack space to run the currently executing function to perform corresponding industrial control operations; The data writing module further includes: A space determination unit is used to determine the space occupied by the function parameters corresponding to the currently executed function; A space judgment unit is used to determine whether the occupied space exceeds a preset threshold. If so, it filters out target function parameters that exceed the preset threshold from the function parameters. The preset threshold is set according to the stack out-of-bounds condition of the target stack space.
8. An electronic device, characterized in that, The electronic device includes a processor and a memory; wherein the memory is used to store a computer program, which is loaded and executed by the processor to implement the controller stack invocation method as described in any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that, Used to store a computer program, which, when executed by a processor, implements the controller stack invocation method as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Method for compiling function type POU object
CN105116800A
Application loading method and device
CN105426223A