Method for adding code to executable file, storage medium and intelligent terminal

By moving the __TEXT segment in the Mach-O file and expanding the blank area to insert code, the flexibility and auditing problems of code insertion in iOS/macOS application development are solved, and flexible function expansion and correct operation are achieved.

CN120010824BActive Publication Date: 2025-08-29BEIJING ZHI YOU WANG AN TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202510495166.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-04-21
Publication Date
2025-08-29
Estimated Expiration
2045-04-21

AI Technical Summary

Technical Problem

In the development of iOS/macOS application, the existing technology cannot flexibly insert code into compiled Mach-O files, and the addition of executable segments or reserved hollow filling methods violate the requirements of the Apple system's single executable segment, resulting in the failure of the shelf review or the program crash.

Method used

By moving the __TEXT segment and other segments thereafter, insert code in the blank area inside __TEXT, and insert dynamically generated machine code through a toolchain without recompilation, correcting the address offset to ensure the program runs correctly.

Benefits of technology

It realizes meeting the Apple system audit requirements without adding new executable segments, improves the flexibility and development efficiency of function expansion, and ensures that the program runs correctly.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120010824B_ABST
    Figure CN120010824B_ABST
Patent Text Reader

Abstract

The present disclosure belongs to the field of software engineering technology, and specifically relates to a method, storage medium, and smart terminal for adding code to an executable file. The method comprises: obtaining and parsing the structure of a target Mach‑O file in the memory of an iOS system or a macOS system; migrating all segments after the code segment in the structure to a preset blank area at the end of the file to free up continuous memory space, expanding the code segment to include the continuous memory space, inserting machine code into the continuous memory space based on a code insertion tool that does not require recompilation, wherein the code segment refers to the __TEXT segment; and automatically correcting the address offset caused by segment movement. The present disclosure provides a method for adding code to an Apple system Mach‑O file that complies with Apple's listing review policy and has more flexible functional expansion.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure belongs to the technical field of software engineering, and particularly relates to a method for adding code to an executable file, a storage medium, and an intelligent terminal. Background Art

[0002] In application development for Apple systems (iOS / macOS), executable files use the Mach-O (MachObject) format, with the segment and section layout determined during compilation. The code segment (__TEXT segment) is the only executable segment, storing program code. The presence of multiple executable segments will result in a listing rejection. Other segments (such as __DATA and __LINKEDIT) store data or metadata.

[0003] Two typical methods of prior art attempting to insert code after compilation are as follows:

[0004] Adding a new executable segment: Directly adding a new executable segment (such as __CUSTOM_TEXT) to the Mach-O file, but violating Apple's single executable segment requirement will cause the app to fail review or crash during operation.

[0005] Reserved hole filling: This method reserves blank space during compilation and then fills the holes with code after compilation. However, the size of the reserved space must be calculated in advance, which reduces flexibility and can easily lead to insertion failures due to insufficient space. Summary of the Invention

[0006] In response to the above problems, various embodiments of the present disclosure propose a solution for adding code to a Mach-O file.

[0007] A first aspect of an embodiment of the present disclosure provides a method for adding code to an executable file, comprising:

[0008] Obtain and parse the structure of the target Mach-O file in the iOS or macOS system memory;

[0009] Migrating all segments following the code segment in the structure to a blank area preset at the end of the file to free up continuous memory space, expanding the code segment to include the continuous memory space, and inserting machine code into the continuous memory space using a code insertion tool that does not require recompilation, wherein the code segment is the __TEXT segment;

[0010] Automatically correct address offsets caused by segment movement.

[0011] In some embodiments of the present disclosure, obtaining and parsing the structure of the target Mach-O file includes:

[0012] Read and parse the segment header and section header of the target Mach-O file;

[0013] The position and size of each segment in the structure are extracted.

[0014] In some embodiments of the present disclosure, after obtaining and parsing the structure of the target Mach-O file, the method further includes:

[0015] At least the code segment, __DATA segment, __LINKEDIT segment, relocation table, symbol table and code signature data in the structure are backed up.

[0016] In some embodiments of the present disclosure, the expanding the code segment to include the continuous memory space includes:

[0017] The size field of the code segment is modified so that the storage area of ​​the code segment includes the continuous memory space.

[0018] In some embodiments of the present disclosure, the machine code is dynamically generated.

[0019] In some embodiments of the present disclosure, the code insertion tool refers to a command line tool or an IDE plug-in.

[0020] In some embodiments of the present disclosure, automatically correcting the address offset caused by segment movement includes:

[0021] The relocation table, symbol table and dynamic binding information of the target Mach-O file are traversed to obtain the segment offset and the offset difference after the move of each segment, and based on the segment offset and the offset difference, the absolute address references in the code are corrected in batches.

[0022] In some embodiments of the present disclosure, the step of extending the blank area within the code segment and inserting the machine code includes:

[0023] The section layout of the code segment is scanned to identify the location and size of gaps.

[0024] Fit the machine code blocks to be inserted into the gaps, and update the code jump instructions to short jumps to fit the space limitations;

[0025] Through function stubs or springboard mechanisms, separate code blocks are connected to implement complete logic.

[0026] A second aspect of the embodiments of the present disclosure provides an intelligent terminal, including a memory and a processor.

[0027] The memory is used to store computer programs;

[0028] The processor is configured to implement the method described in the first aspect of the embodiment of the present disclosure when executing the computer program.

[0029] A third aspect of an embodiment of the present disclosure provides a storage medium on which computer-executable instructions are stored. When the computer-executable instructions are executed by a computing device, they can be used to implement the method described in the first aspect of the embodiment of the present disclosure.

[0030] In summary, the methods, smart terminals, and storage media for adding code to executable files provided by the various embodiments of the present disclosure move the __TEXT segment and other segments thereafter, and insert code in the blank area expanded within __TEXT to avoid adding new executable segments, thereby meeting the Apple system's requirement for Mach-O files to contain only one executable segment for listing review; at the same time, dynamically generated code is inserted based on a code insertion tool chain that does not require recompilation, thereby improving development efficiency and increasing the flexibility of functional expansion; and address reference errors caused by segment movement are eliminated through dynamic address repair to ensure the correct operation of the program. BRIEF DESCRIPTION OF THE DRAWINGS

[0031] The features and advantages of the present disclosure will be more clearly understood by referring to the accompanying drawings, which are schematic and should not be construed as limiting the present disclosure in any way. In the accompanying drawings:

[0032] Figure 1 is a flowchart of a method for adding code to an executable file according to some embodiments of the present disclosure;

[0033] Figure 2 is an example of a Mach-O file segment header and section header;

[0034] Figure 3 This is an example of the __TEXT segment details data in a Mach-O file;

[0035] Figure 4 yes Figure 3 The storage structure after the __TEXT segment is inserted into the code shown;

[0036] Figure 5 Yes Figure 4 The storage structure shown is the storage structure after the dynamic code is inserted;

[0037] Figure 6 is a schematic diagram of a smart terminal according to some embodiments of the present disclosure. DETAILED DESCRIPTION

[0038] In the detailed description that follows, many specific details of the present disclosure are set forth by way of example in order to provide a thorough understanding of the relevant disclosure. However, it will be apparent to one of ordinary skill in the art that the present disclosure can be implemented without these details. It should be understood that the use of the terms "system," "device," "unit," and / or "module" in the present disclosure is a method for distinguishing between different parts, elements, parts, or assemblies at different levels in a sequential arrangement. However, these terms may be replaced by other expressions if they can achieve the same purpose.

[0039] It should be understood that when a device, unit, or module is referred to as being "on," "connected to," or "coupled to" another device, unit, or module, it may be directly on, connected to, coupled to, or in communication with the other device, unit, or module, or there may be intervening devices, units, or modules, unless the context clearly indicates an exception. For example, the term "and / or" as used in this disclosure includes any and all combinations of one or more of the associated listed items.

[0040] The terms used in this disclosure are only for describing specific embodiments and are not intended to limit the scope of this disclosure. As shown in the specification and claims of this disclosure, unless the context clearly indicates an exception, the words "a", "an", "a kind" and / or "the" do not specifically refer to the singular and may also include the plural. Generally speaking, the terms "include" and "comprise" only indicate the inclusion of clearly identified features, wholes, steps, operations, elements and / or components, and such expressions do not constitute an exclusive list, and other features, wholes, steps, operations, elements and / or components may also be included.

[0041] These and other features and characteristics of the present disclosure, as well as the methods of operation, the functions of the related elements of the structure, the combination of parts, and the economy of manufacture may be better understood with reference to the following description and accompanying drawings, which form a part of this specification. However, it is to be expressly understood that the drawings are for illustration and description purposes only and are not intended to limit the scope of protection of the present disclosure. It is to be understood that the drawings are not drawn to scale.

[0042] Various structural diagrams are used in this disclosure to illustrate various variations of the embodiments of the present disclosure. It should be understood that the preceding or following structures are not intended to limit the present disclosure. The scope of protection of the present disclosure is subject to the claims.

[0043] In iOS / macOS application development, executable files use the Mach-O (Mach Object) format, with the segment and section layout determined during the compilation phase. The __TEXT segment is the only executable segment, storing program code, while other segments (such as __DATA and __LINKEDIT) store data or metadata. In traditional development processes, program code is completely fixed at compile time. Adding new functionality after compilation requires recompiling the entire program, leading to the following issues:

[0044] Poor flexibility: Code cannot be dynamically inserted into compiled binary files, making functional expansion difficult.

[0045] Apple review restrictions: Apple's system requires that Mach-O files contain only one executable segment (__TEXT). Directly adding an executable segment will result in rejection of the listing review.

[0046] Two typical methods of prior art attempting to insert code after compilation are as follows:

[0047] Adding a new executable segment: Directly adding a new executable segment (such as __CUSTOM_TEXT) to the Mach-O file violates Apple's single executable segment requirement, causing the app to fail review or crash during operation.

[0048] Reserved hole filling: This method reserves blank space during compilation and then fills the holes with code after compilation. However, the size of the reserved space must be calculated in advance, which reduces flexibility and can easily lead to insertion failures due to insufficient space.

[0049] To solve the above problem, the present disclosure proposes a method for adding code to Mach-O files. By moving the __TEXT segment and other segments after it, the code is inserted into the blank area inside __TEXT, avoiding the addition of a new executable segment. This satisfies Apple's system's requirement that Mach-O files contain only one executable segment for listing. In some embodiments, the flowchart of the method for adding code to a Mach-O file is shown in Figure 1, which specifically includes the following steps:

[0050] S110, obtaining and parsing the structure of the target Mach-O file in the iOS system or macOS system memory.

[0051] Specifically, parse the segment and section header information, extract the __TEXT segment offset, size and permission flags, read the segment headers and section headers of the target Mach-O file, and parse the location and size of the __TEXT, __DATA and other segments. Figure 2 is an example of a segment header and section header in a Mach-O file. Figure 3 This is an example of __TEXT segment detail data.

[0052] Figure 3 Where fileoff represents the relative file offset of the __TEXT segment data in bytes. filesize represents the size of the __TEXT segment.

[0053] Some embodiments of the present disclosure further include backing up the __TEXT segment, __DATA segment, __LINKEDIT segment, relocation table, symbol table and code signature data of the original file.

[0054] S120, migrating the other segments after the code segment in the structure to a preset blank area at the end of the file to free up continuous memory space, expanding the code segment to include the continuous memory space, and inserting machine code into the continuous memory space based on a code insertion tool that does not require recompilation, wherein the code segment refers to the __TEXT segment.

[0055] Specifically, all segments following the __TEXT segment (e.g., __DATA and __LINKEDIT) are moved backward to create space (referred to as __customcode) for inserting new code. Some embodiments of the present disclosure modify the size of the __TEXT segment so that the moved __customcode area remains part of the __TEXT segment, thus ensuring that no new executable segments are added.

[0056] The storage structure before inserting the code is: [__TEXT][__DATA][__LINKEDIT]...

[0057] The storage structure after insertion is:

[0058] [__TEXT (including __customcode area)][__DATA (shifted back)][__LINKEDIT (shifted back)]...

[0059] Figure 3 The storage structure after the __TEXT segment is inserted into the code is as follows Figure 4 shown.

[0060] Ultimately, dynamically generated machine code (such as encryption code and hotfix logic) is written to the __customcode area. Some embodiments of the present disclosure utilize a code insertion toolchain (such as a command-line tool or IDE plugin) that doesn't require recompilation. Because code is dynamically generated and doesn't require recompilation, development efficiency is improved and flexibility for functional expansion is increased.

[0061] exist Figure 4 The storage structure after inserting the code into the storage structure shown is as follows Figure 5shown.

[0062] In some other embodiments of the present disclosure, inserting machine code into a blank area within a code segment includes:

[0063] The section layout of the code segment is scanned to identify the location and size of gaps. The machine code blocks to be inserted are adapted to the gaps. Code jump instructions are updated to short jumps to fit within the space constraints. Function stubs or springboard mechanisms are used to connect the scattered code blocks to implement complete logic.

[0064] S130, automatically correcting the address offset caused by segment movement.

[0065] First, traverse the Mach-O relocation table (Relocations), symbol table (Symbol Table) and dynamic binding information (Binding Info) to fix the address offset caused by segment movement. The details are as follows:

[0066] / / cpp code: fix absolute address offset

[0067] for (Reloc&reloc : macho.relocations) {

[0068] if (reloc.within_moved_segment) {

[0069] reloc.target_address += segment_move_offset;

[0070] }

[0071] }

[0072] Then, based on the difference between the original segment offset and the offset after the move, the absolute address references in the code (such as `b / bl / adrp / adr` instructions and data pointers) are corrected in batches.

[0073] Figure 6 Schematic diagram of a smart terminal according to some embodiments of the present disclosure. Figure 6 As shown, the intelligent terminal 600 includes a memory 620 and a processor 610, wherein the memory 620 is used to store computer programs; the processor 610 is used to implement Figure 1 The method for adding code to an executable file as described in S110-S130.

[0074] Some embodiments of the present disclosure disclose a storage medium having computer executable instructions stored thereon. When the computer executable instructions are executed by a computing device, the computer executable instructions can be used to implement Figure 1The method for adding code to an executable file as described in S110-S130.

[0075] In summary, the methods, smart terminals, and storage media for adding code to executable files provided by the various embodiments of the present disclosure move the __TEXT segment and other segments thereafter, and insert code in the blank area expanded within __TEXT to avoid adding new executable segments, thereby meeting the Apple system's requirement for Mach-O files to contain only one executable segment for listing review; at the same time, dynamically generated code is inserted based on a code insertion tool chain that does not require recompilation, thereby improving development efficiency and increasing the flexibility of functional expansion; and address reference errors caused by segment movement are eliminated through dynamic address repair to ensure the correct operation of the program.

[0076] Although the subject matter described herein is provided in the general context of being executed in conjunction with the execution of an operating system and application programs on a computer system, those skilled in the art will recognize that other implementations may also be performed in conjunction with other types of program modules. Generally speaking, program modules include routines, programs, components, data structures, and other types of structures that perform specific tasks or implement specific abstract data types. Those skilled in the art will appreciate that the subject matter described herein may be practiced using other computer system configurations, including handheld devices, multiprocessor systems, microprocessor-based or programmable consumer electronics, minicomputers, mainframe computers, and the like, and may also be used in distributed computing environments in which tasks are performed by remote processing devices connected via a communication network. In a distributed computing environment, program modules may be located in both local and remote memory storage devices.

[0077] Those skilled in the art will appreciate that the units and method steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians 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 disclosure.

[0078] It should be understood that the above-described specific embodiments of the present disclosure are merely illustrative of or explanation of the principles of the present disclosure and do not constitute limitations on the present disclosure. Therefore, any modifications, equivalent substitutions, improvements, etc. made without departing from the spirit and scope of the present disclosure shall be included within the scope of protection of the present disclosure. In addition, the claims appended to the present disclosure are intended to cover all variations and modifications that fall within the scope and metes and bounds of the appended claims, or equivalents of such scope and metes and bounds.

Claims

1. A method for adding code to an executable file, characterized in that: include: Obtain and parse the structure of the target Mach-O file in the iOS or macOS system memory; Migrating all segments following the code segment in the structure to a blank area preset at the end of the file to free up continuous memory space, expanding the code segment to include the continuous memory space, and inserting machine code into the continuous memory space using a code insertion tool that does not require recompilation, wherein the code segment is the __TEXT segment; Automatically correct address offsets caused by segment movement; Wherein, the machine code is dynamically generated, Inserting the machine code in the continuous memory space based on the code insertion tool without recompiling includes: Scanning the section layout of the code segment to identify gap locations and sizes; Fit the machine code blocks to be inserted into the gaps, and update the code jump instructions to short jumps to fit the space limitations; Connect scattered code blocks to implement complete logic through function stubs or springboard mechanisms; The automatic correction of address offset caused by segment movement includes: The relocation table, symbol table and dynamic binding information of the target Mach-O file are traversed to obtain the segment offset and the offset difference after the move of each segment, and based on the segment offset and the offset difference, the absolute address references in the code are corrected in batches.

2. The method according to claim 1, characterized in that The structure of obtaining and parsing the target Mach-O file includes: Read and parse the segment header and section header of the target Mach-O file; The position and size of each segment in the structure are extracted.

3. The method according to claim 1, characterized in that After obtaining and parsing the structure of the target Mach-O file, the following steps are also included: At least the code segment, __DATA segment, __LINKEDIT segment, relocation table, symbol table and code signature data in the structure are backed up.

4. The method according to claim 1, characterized in that The expanding the code segment to include the continuous memory space comprises: The size field of the code segment is modified so that the storage area of ​​the code segment includes the continuous memory space.

5. The method according to claim 1, wherein: The code insertion tool is a command line tool or an IDE plug-in.

6. An intelligent terminal, characterized in that: including memory and processor, The memory is used to store computer programs; The processor is configured to implement the method according to any one of claims 1 to 5 when executing the computer program.

7. A storage medium having computer-executable instructions stored thereon, which, when executed by a computing device, can be used to implement the method according to any one of claims 1 to 5.

Citation Information

Patent Citations

  • Process injection method and related equipment

    CN119357948A