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

By moving and expanding the __TEXT segment in the Mach-O file, inserting dynamically generated code, and correcting address offset, the flexibility and audit requirements for dynamic code insertion in Apple's system are solved, and efficient function expansion and correct operation are achieved.

CN120010824AActive Publication Date: 2025-05-16BEIJING ZHI YOU WANG AN TECH CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

In the application development of Apple systems, it is difficult for existing technology to dynamically insert code into Mach-O files, resulting in poor flexibility in function expansion and violating Apple's single executable segment requirements, resulting in the application being unable to pass the audit or run crash.

Method used

By moving the __TEXT segment and other subsequent segments, insert code in the blank area inside __TEXT to avoid adding new executable segments, and insert dynamically generated code based on the code that does not require recompilation, and automatically correct address offsets caused by segment movement.

Benefits of technology

It realizes the flexibility of dynamically inserting code into Mach-O files, meets the requirements of a single executable segment of the Apple system, improves development efficiency, and ensures the correct operation of the program.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120010824A_ABST
    Figure CN120010824A_ABST
Patent Text Reader

Abstract

The invention belongs to the technical field of software engineering, and particularly relates to a method for adding codes in an executable file, a storage medium and an intelligent terminal. The method comprises the following steps: acquiring and analyzing the structure of a target Mach-O file in a memory of an iOS system or a macOS system; other segments behind a code segment in the structure are integrally migrated to a blank area preset at the end of the file to vacate a continuous memory space, the code segment is expanded to contain the continuous memory space, a machine code is inserted into the continuous memory space based on a code insertion tool which does not need to be recompiled, and the code segment refers to a TEXT segment; and address offset caused by segment movement is automatically corrected. The invention provides the method for adding the code to the Mach-O file of the Apple system, which accords with an Apple putaway auditing policy and is more flexible in function extension.
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 in particular relates to a method for adding code to an executable file, a storage medium, and an intelligent terminal. Background Art

[0002] In the application development of Apple system (iOS / macOS), the executable file adopts Mach-O (MachObject) format, and its segment and section layout is determined at the compilation stage. Among them, the code segment (__TEXT segment) is the only executable segment, which stores the program code. The existence of multiple executable segments will result in the rejection of the listing review. Other segments (such as __DATA, __LINKEDIT) store data or meta information.

[0003] Two typical methods of prior art attempting to insert code after compilation are as follows: 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.

[0004] Reserved hole filling: Reserve blank space during compilation, and fill the hole with code after compilation. However, the size of the reserved space needs to be calculated in advance, which has poor flexibility and is prone to insertion failure due to insufficient space. Summary of the invention

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

[0006] A first aspect of an embodiment of the present disclosure provides a method for adding code to an executable file, comprising: Get and parse the structure of the target Mach-O file in the iOS or macOS system memory; Migrate all other segments after the code segment in the structure to a blank area preset at the end of the file to free up continuous memory space, expand the code segment to include the continuous memory space, and insert 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; Automatically correct address offsets caused by segment movement.

[0007] In some embodiments of the present disclosure, obtaining and parsing the structure of the target Mach-O file includes: Read and parse the segment header and section header of the target Mach-O file; The location and size of each segment in the structure are extracted.

[0008] In some embodiments of the present disclosure, after obtaining and parsing the structure of the target Mach-O file, the method further includes: At least the code segment, __DATA segment, __LINKEDIT segment, relocation table, symbol table and code signature data in the structure are backed up.

[0009] In some embodiments of the present disclosure, the expanding the code segment to include the continuous memory space includes: The size field of the code segment is modified so that the storage area of ​​the code segment includes the continuous memory space.

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

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

[0012] In some embodiments of the present disclosure, the automatic correction of the 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 of each segment and the offset difference after the move, and based on the segment offset and the offset difference, the absolute address references in the code are corrected in batches.

[0013] In some embodiments of the present disclosure, the step of extending the blank area inside the code segment and inserting the machine code comprises: The section layout of the code segment is scanned to identify the gap locations and sizes.

[0014] Adapt the machine code blocks to be inserted into the gaps, and update the code jump instructions to short jumps to fit the space limit; Through function stubs or springboard mechanisms, scattered code blocks are connected to implement complete logic.

[0015] A second aspect of the embodiments of the present disclosure provides an intelligent terminal, including a memory and a processor. The memory is used to store computer programs; The processor is used to implement the method described in the first aspect of the embodiment of the present disclosure when executing the computer program.

[0016] 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.

[0017] In summary, the methods, smart terminals, and storage media for adding code to an executable file provided by the embodiments of the present disclosure move the __TEXT segment and other segments thereafter, and insert code in the blank area inside __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 correct program operation. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] 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: Figure 1 is a flowchart of a method for adding code to an executable file according to some embodiments of the present disclosure; Figure 2 is an example of a Mach-O file segment header and section header; Figure 3 This is an example of the __TEXT segment details data in a Mach-O file; Figure 4 yes Figure 3 The storage structure after the __TEXT segment is inserted into the code shown; Figure 5 Yes Figure 4 The storage structure shown is the storage structure after the dynamic code is inserted; Figure 6 is a schematic diagram of a smart terminal according to some embodiments of the present disclosure. DETAILED DESCRIPTION

[0019] In the detailed description below, 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 different parts, elements, parts, or assemblies at different levels in a sequential arrangement. However, these terms may be replaced by other expressions if other expressions can achieve the same purpose.

[0020] 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 communicating with other devices, units or modules, or there may be intermediate 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 relevant listed items.

[0021] 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 "one", "a kind" and / or "the" do not specifically refer to the singular, but 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.

[0022] These and other features and characteristics, methods of operation, functions of related elements of the structure, combinations of parts, and economies of manufacture of the present disclosure may be better understood with reference to the following description and drawings, which form a part of the specification. However, it is to be clearly 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.

[0023] 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 above or below structures are not intended to limit the present disclosure. The scope of protection of the present disclosure shall be subject to the claims.

[0024] In iOS / macOS application development, executable files use the Mach-O (Mach Object) format, and the layout of its segments and sections is determined during the compilation phase. Among them, the __TEXT segment is the only executable segment that stores program code, and other segments (such as __DATA, __LINKEDIT) store data or meta information. In the traditional development process, the program code is completely fixed at compile time. If you need to add new function code after compilation, you need to recompile the entire program, which leads to the following problems: Poor flexibility: It is impossible to dynamically insert code into compiled binary files, which makes it difficult to expand functionality.

[0025] 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.

[0026] Two typical methods of prior art attempting to insert code after compilation are as follows: 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, causing the app to fail review or crash during operation.

[0027] Reserved hole filling: Reserve blank space during compilation, and fill the hole with code after compilation. However, the size of the reserved space needs to be calculated in advance, which has poor flexibility and is prone to insertion failure due to insufficient space.

[0028] In order to solve the above problem, the present disclosure proposes a method for adding code to a Mach-O file. By moving the __TEXT segment and other segments after it, the code is inserted into the blank area inside __TEXT to avoid adding an executable segment, thereby meeting the Apple system's requirement that Mach-O files contain only one executable segment for listing review. In some embodiments, the flowchart of the method for adding code to a Mach-O file is shown in 1, which specifically includes the following steps: S110, obtaining and parsing the structure of a target Mach-O file in the iOS system or macOS system memory.

[0029] Specifically, parse the segment and section header information, extract the __TEXT segment offset, size and permission flag, 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.

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

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

[0032] S120, migrate 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, expand the code segment to include the continuous memory space, and insert machine code into the continuous memory space based on a code insertion tool without recompilation, wherein the code segment refers to the __TEXT segment.

[0033] Specifically, all segments after the __TEXT segment (such as __DATA, __LINKEDIT) are moved backward as a whole to make room (recorded 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 still belongs to the __TEXT segment, thereby ensuring that no new executable segments are added.

[0034] The storage structure before inserting the code is: [__TEXT][__DATA][__LINKEDIT]... The storage structure after insertion is: [__TEXT (including __customcode area)][__DATA (move back)][__LINKEDIT (move back)]... Figure 3 The storage structure after the __TEXT segment is inserted into the code is as follows Figure 4 shown.

[0035] Finally, the dynamically generated machine code (such as encryption code, hot fix logic) is written into the __customcode area. Some embodiments of the present disclosure insert code based on a code insertion tool chain (such as a command line tool or IDE plug-in) that does not require recompilation, because there is no need to recompile the code and the code is dynamically generated, thereby improving development efficiency and increasing the flexibility of function expansion.

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

[0037] In some other embodiments of the present disclosure, inserting machine code into a blank area inside a code segment includes: Scan the section layout of the code segment, identify the gap position and size, fit the machine code blocks to be inserted into the gaps, and update the code jump instructions to short jumps to adapt to the space limit. Through the function stub or springboard mechanism, connect the scattered code blocks to realize the complete logic.

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

[0039] First, traverse the Mach-O relocation table, symbol table, and dynamic binding information to fix the address offset caused by segment movement. The details are as follows: / / cpp code: fix absolute address offset for (Reloc&reloc : macho.relocations) { if (reloc.within_moved_segment) { reloc.target_address += segment_move_offset; } } 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.

[0040] Figure 6 is a 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 a computer program; and the processor 610 is used to implement when executing the computer program. Figure 1 The method of adding code to an executable file as described in S110-S130.

[0041] 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 storage medium can be used to implement Figure 1 The method of adding code to an executable file as described in S110-S130.

[0042] In summary, the methods, smart terminals, and storage media for adding code to an executable file provided by the embodiments of the present disclosure move the __TEXT segment and other segments thereafter, and insert code in the blank area inside __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 correct program operation.

[0043] Although the subject matter described herein is provided in the general context of being executed in conjunction with the execution of operating systems and application programs on computer systems, it will be appreciated by those skilled in the art that other implementations may also be performed in conjunction with other types of program modules. In general, program modules include routines, programs, components, data structures, and other types of structures that perform specific tasks or implement specific abstract data types. It will be appreciated by those skilled in the art 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, etc., and may also be used in a distributed computing environment 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.

[0044] Those of ordinary skill 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. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this disclosure.

[0045] It should be understood that the above specific embodiments of the present disclosure are only used to illustrate or explain the principles of the present disclosure, and do not constitute a limitation of the present disclosure. Therefore, any modifications, equivalent substitutions, improvements, etc. made without departing from the spirit and scope of the present disclosure should be included in the protection scope of the present disclosure. In addition, the claims attached to the present disclosure are intended to cover all changes and modifications that fall within the scope and boundaries of the attached claims, or the equivalent forms of such scope and boundaries.

Claims

1. A method for adding code to an executable file, characterized in that: include: Get and parse the structure of the target Mach-O file in the iOS or macOS system memory; Migrate all other segments after the code segment in the structure to a blank area preset at the end of the file to free up continuous memory space, expand the code segment to include the continuous memory space, and insert 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; Automatically correct address offsets caused by segment movement.

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 location 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, characterized in that: The machine code is generated dynamically.

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

7. The method according to claim 2, characterized in that: 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 of each segment and the offset difference after the move, and based on the segment offset and the offset difference, the absolute address references in the code are corrected in batches.

8. The method according to claim 1, characterized in that: The step of extending the blank area inside the code segment and inserting the machine code comprises: Scanning the section layout of the code segment to identify the position and size of the gap; Adapt the machine code blocks to be inserted into the gaps, and update the code jump instructions to short jumps to fit the space limit; Through function stubs or springboard mechanisms, scattered code blocks are connected to implement complete logic.

9. 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-8 when executing the computer program.

10. 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 8.

Citation Information

Patent Citations

  • Code embedding method and device in application and electronic device

    CN109933350A

  • Static injection method and storage medium

    CN119149126A

  • Process injection method and related equipment

    CN119357948A

  • Modification of compiled applications and application management using retrievable policies

    US20140282446A1