Linux binary file-based link dependence conversion method
By dividing the Linux binary file into fragments and replacing the output stream class in the link fragment, the problem of increased file size and compilation complexity caused by dynamic linking of Linux binary files is solved, and the library upgrade function without recompilation is realized.
Patent Information
- Application Number
- CN202410252093.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-06
- Publication Date
- 2024-08-23
AI Technical Summary
Linux binary files use dynamic linking during compilation, which results in complex dependencies between third-party libraries and standard libraries, resulting in an increase in file size and the need to recompile when updating and upgrading, resulting in large processing loads and difficulties.
Divide the binary file format into file header fragments, code fragments, data fragments and link fragments, locate the link fragments through the section table, and replace the output stream class in the link fragment according to the predetermined replacement content to achieve dynamic replacement and avoid recompilation.
It realizes the independent upgrade of third-party libraries between different projects. When the function name and parameters remain unchanged, there is no need to modify the source code, achieving a dynamic replacement effect and reducing the amount of compilation processing.
Smart Images

Figure CN118535164A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing, and in particular to a link dependency conversion method based on Linux binary files. Background Art
[0002] A binary file is a file format that contains non-text data. It stores data in binary form. Unlike text files, binary files can contain any type of data, including integers, floating-point numbers, characters, structures, etc. Binary files are often used to store and transmit information that needs to maintain data structure and data type, such as images, audio, video, databases, etc. Since binary files do not encode data, large amounts of data can be read and written faster.
[0003] For example, Chinese Patent Publication No.: CN115878178A discloses a method, device and storage medium for porting CODESYS runtime software, and relates to the technical field of software porting. The method includes obtaining specific macro definitions in the Makefile script; obtaining the libcobalt library header file and the Xenomai header file based on the compilation parameters in the Makefile script; finding the source code file of the specified format under the path where the Makefile script is located, and converting the source code file of the specified format into a binary target file based on the libcobalt library header file and the Xenomai header file; obtaining static library files; obtaining the library files of the real-time related library libcobalt and the library files of the memory management library; passing the packaging parameters of the main function to the local linker; linking the binary target file, the static library file, the library files of the real-time related library libcobalt and the library files of the memory management library through the local linker to obtain the target file that can be executed under the Xenomai framework. The method, device and storage medium disclosed in the present invention can port CODESYS runtime software to a system under the Xenomai framework;
[0004] For example, Chinese Patent Publication No.: CN114519043A discloses a method for reverse analysis of executable binary file formats, which obtains the executable binary file content, splits the file structure and repackages it into component content; collects the method offsets contained in the method component, and cuts the method component with two adjacent offsets; traverses the instructions for accessing field variables in the method, and infers the field type based on the meaning of the instructions; finds static methods and sets them to a static state, initializes method access permissions, and links the methods of the interface with the methods in the implementation class through index values; executes instructions in the method through stack simulation, parses the instructions to obtain the method's descriptor information, and implements reverse analysis of the executable binary file based on the method descriptor information. The use of the method for reverse analysis of executable binary file formats disclosed in the present invention reduces the dependence of the file reverse analysis process on the descriptor component, lays a foundation for file format conversion, has a wide range of applications, and is highly practical.
[0005] However, the prior art still has the following problems:
[0006] When compiling Linux binary files, dynamic linking is used to write the names of third-party libraries and standard libraries that need to be called into the binary files. Because there may be many referenced standard libraries and the referenced third-party libraries may also be very large, and the third-party libraries will continue to reference other libraries. If all these dependencies are placed in this binary file, then this binary file may be very large. When updating and upgrading between different projects, the program needs to be recompiled, which has a large amount of processing and is difficult. Summary of the invention
[0007] To this end, the present invention provides a link dependency conversion method based on Linux binary files to overcome the problem in the prior art that binary files may be very large, and when updating and upgrading between different projects, the program needs to be recompiled, the processing volume is large, and there are difficulties.
[0008] To achieve the above object, the present invention provides a link dependency conversion method based on Linux binary files, which comprises:
[0009] Step S1, dividing the binary file format into several segments, including a file header segment, a code segment, a data segment, and a link segment;
[0010] Step S2, read the file header segment, and locate the section table according to the file header segment;
[0011] Step S3, locating the link segment according to the content in the section table;
[0012] Step S4, traversing the link segments, and replacing the output stream class of the link segments according to the predetermined replacement content.
[0013] Furthermore, in step S1, the file header segment is used to store the file type, target architecture, and entry point address.
[0014] Furthermore, in step S1, the code snippet is used to store the execution logic of the code.
[0015] Furthermore, in step S1, the data segments are used to store data that needs to be read and written during program execution.
[0016] Furthermore, the link fragment is used to store a dynamic symbol table, which includes symbol information corresponding to the dynamic link.
[0017] Furthermore, in step S2, locating the section table according to the file header fragment includes:
[0018] Use the fread function to read the file header fragment into an Elf64_Ehdr structure, and use the fseek function to position the file pointer to the section table.
[0019] Furthermore, in step S2, the offset and size of the link segment are recorded in the section table.
[0020] Further, in step S3, according to the content in the section table, the process of locating the link fragment includes reading the content of the section table, traversing the section table, finding the section containing the dynamic symbol table, locating the offset and size of the dynamic symbol table, and reading the content of the dynamic symbol table.
[0021] Furthermore, in step S4, the length of the predetermined replacement content must be consistent with the length of the content to be replaced.
[0022] Furthermore, the predetermined replacement content includes one replacement content or multiple replacement contents, and a single replacement content corresponds one-to-one to a corresponding replacement object.
[0023] Compared with the prior art, the present invention includes step S1, dividing the binary file format into several segments, including a file header segment, a code segment, a data segment and a link segment, step S2, reading the file header segment, and locating the section table according to the file header segment, step S3, locating the link segment according to the content in the section table, step S4, traversing the link segment, and replacing the output stream class of the link segment according to the predetermined replacement content. Different projects can be upgraded independently without affecting each other. When the third-party library is upgraded, as long as the called function name and function parameters are guaranteed to remain unchanged, the effect of dynamic replacement can be achieved without modifying the source code or recompiling. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Figure 1 This is a step diagram of a link dependency conversion method based on Linux binary files according to an embodiment of the invention. DETAILED DESCRIPTION
[0025] In order to make the objects and advantages of the present invention more clearly understood, the present invention is further described below in conjunction with embodiments; it should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0026] The preferred embodiments of the present invention are described below with reference to the accompanying drawings. It should be understood by those skilled in the art that these embodiments are only used to explain the technical principles of the present invention and are not intended to limit the protection scope of the present invention.
[0027] See also Figure 1 As shown, it is a schematic diagram of the steps of the link dependency conversion method based on Linux binary files according to an embodiment of the present invention. The link dependency conversion method based on Linux binary files according to the present invention comprises:
[0028] Step S1, dividing the binary file format into several segments, including a file header segment, a code segment, a data segment, and a link segment;
[0029] Step S2, read the file header segment, and locate the section table according to the file header segment;
[0030] Step S3, locating the link segment according to the content in the section table;
[0031] Step S4, traversing the link segments, and replacing the output stream class of the link segments according to the predetermined replacement content.
[0032] Specifically, in step S1, the file header segment is used to store the file type, target architecture, and entry point address.
[0033] Specifically, in step S1, the code snippet is used to store the execution logic of the code.
[0034] Specifically, in step S1, the data segments are used to store data that needs to be read and written during program execution.
[0035] Specifically, the link fragment is used to store the dynamic symbol table, which includes symbol information corresponding to the dynamic link.
[0036] Specifically, in step S2, locating the section table according to the file header fragment includes:
[0037] Use the fread function to read the file header fragment into an Elf64_Ehdr structure, and use the fseek function to position the file pointer to the section table.
[0038] Specifically, in step S2, the offset and size of the link segment are recorded in the section table.
[0039] Specifically, in step S3, according to the content in the section table, the process of locating the link fragment includes reading the content of the section table, traversing the section table, finding the section containing the dynamic symbol table, locating the offset and size of the dynamic symbol table, and reading the content of the dynamic symbol table.
[0040] For example, std::ofstream is stored in .dynsym. This information can be modified by changing std::ofstream to myx::ofstream. The myx name can be modified by technicians in this field according to specific needs. It only needs to be consistent with the length of std. The length of this "std::ofstream" will be recorded in dynsym.
[0041] Specifically, in step S4, the length of the predetermined replacement content must be consistent with the length of the content to be replaced.
[0042] Specifically, the predetermined replacement content includes one replacement content or multiple replacement contents, and a single replacement content corresponds to a corresponding replacement object one by one.
[0043] If the link dependency conversion method based on Linux binary files of the present invention is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium, including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present invention, and the aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.
[0044] So far, the technical solutions of the present invention have been described in conjunction with the preferred embodiments shown in the accompanying drawings. However, it is easy for those skilled in the art to understand that the protection scope of the present invention is obviously not limited to these specific embodiments. Without departing from the principle of the present invention, those skilled in the art can make equivalent changes or substitutions to the relevant technical features, and the technical solutions after these changes or substitutions will fall within the protection scope of the present invention.
Claims
1. A link dependency conversion method based on Linux binary files, characterized in that: include: Step S1, dividing the binary file format into several segments, including a file header segment, a code segment, a data segment, and a link segment; Step S2, read the file header segment, and locate the section table according to the file header segment; Step S3, locating the link segment according to the content in the section table; Step S4, traversing the link segments, and replacing the output stream class of the link segments according to the predetermined replacement content.
2. The link dependency conversion method based on Linux binary files according to claim 1 is characterized in that: In step S1, the file header segment is used to store the file type, target architecture, and entry point address.
3. The link dependency conversion method based on Linux binary files according to claim 1 is characterized in that: In step S1, the code snippet is used to store the execution logic of the code.
4. The link dependency conversion method based on Linux binary files according to claim 1 is characterized in that: In step S1, the data segments are used to store data that needs to be read and written during program execution.
5. The link dependency conversion method based on Linux binary files according to claim 1 is characterized in that: The link fragment is used to store the dynamic symbol table, which contains the symbol information corresponding to the dynamic link.
6. The link dependency conversion method based on Linux binary files according to claim 1 is characterized in that: In step S2, locating the section table according to the file header fragment includes: Use the fread function to read the file header segment into an Elf64_Ehdr structure, and use the fseek function to position the file pointer to the section table.
7. The link dependency conversion method based on Linux binary files according to claim 1 is characterized in that: In step S2, the offset and size of the link segment are recorded in the section table.
8. The link dependency conversion method based on Linux binary files according to claim 1 is characterized in that: In step S3, according to the content in the section table, the process of locating the link fragment includes reading the content of the section table, traversing the section table, finding the section containing the dynamic symbol table, locating the offset and size of the dynamic symbol table, and reading the content of the dynamic symbol table.
9. The link dependency conversion method based on Linux binary files according to claim 1 is characterized in that: In step S4, the length of the predetermined replacement content must be consistent with the length of the content to be replaced.
10. The link dependency conversion method based on Linux binary files according to claim 1, characterized in that: The predetermined replacement content includes one replacement content or multiple replacement contents, and a single replacement content corresponds to a corresponding replacement object one by one.
