Check out the following account for more iOS related content


This article deals with the theoretical aspects of OS startup optimization.

The Mach -o terminology

Mach -o is the file type of the runtime executable. These file types include:

  • Executable file: The most important binary of the application and the main binary of the application extension file.
  • Dylib: Dynamic link library (also known as DSO or DLL). Dynamic link libraries include all system frameworks used in iOS, libobjc for loading OC Runtime methods, and system-level libsystems such as libdispatch(GCD) and libsystem_blocks(Block).
  • Bundle: A Dylib that cannot be linked and can only be loaded at runtime using dlopen(), which is used by macOS plug-ins.

Image: refers to any three types. Framework: This refers to a special Dylib that has a directory structure to store the files required by the Dylib.

All dynamic and static libraries.a and all class files.o files are eventually loaded into memory by Dyld (Apple’s dynamic linker).

Mach-o image file

The mach-o mirror file is split into segments with the segment names in uppercase. All segments are integer multiples of the page size.

Page size depends on the hardware:

  • Arm64 is 16 KB
  • The rest is 4KB

A Segment contains several sections, so section names are lowercase, partitions do not follow the page size, and partitions do not overlap.

Almost all binary files contain these three segments: __TEXT,__DATA, and __LINKEDIT:

  • The TEXT segment is the beginning of the file and contains the Mach header file, the code being executed, and read-only constants such as C strings. Read-only executable (R-x).
  • The DATA segment is the rewrite segment that contains all: global variables, static variables, and so on. Read-write (RW -).
  • The LINKEDIT segment contains the loader’s metadata, such as the function’s name and address. Read only (r-).

A generic file for Mach-O

Suppose you build a 64-bit iOS app and you have a Mach-O file. What happens in Xcode if you want it to run on a 32-bit machine as well? Xcode regenerates a Mach-O file, which is armV7s for 32 bits, and then merges the two files into a third file, which is the generic mach-O file.

Virtual memory

Virtual memory is an indirect layer. Each process is a logical address space mapped to a physical page of RAM (random access memory), not necessarily one-to-one.

Features are as follows:

  • If there is a logical address that does not map any physical RAM, page Defult is triggered when the process accesses that address, and the kernel stops the thread and tries to figure out a solution.

  • When multiple logical addresses are mapped to the same physical RAM, multiple processes share memory.

  • File-based mapping

    • Instead of reading the entire file into physical RAM at once, it can be read using a paging map(mmap() function). That is, a section of a file is mapped to a page in the process’s logical memory.
    • Lazy reading.

    Summary: Dylib or IMGAE TEXT segments can be mapped to multiple processes, resulting in slow reads, and all these pages can be shared between processes.

  • Copy-on-write (Copy while writing), COW for short. That is, when multiple processes share a page of RAM space, once a process wants to do a write operation, it will make a copy of the page of memory content, and then remap the logical address to the new RAM page. That is, the process itself has a copy of that memory.

  • Dirty vs. Clean Pages: Copies made above are considered Dirty pages, which are pages that contain process-specific information. Clean pages are pages that the kernel can recreate as needed, such as re-reading disks. Dirty pages are much more expensive than clean pages,

  • Permissions: Here, a page can be labeled, read, write, or execute, or any combination of them.

Mach-o image loading

__TEXT and __LINKEDIT are read-only and can share memory when multiple processes load mach-O images. __DATA is read-write and generates dirty pages. When dyld completes, the __LINKEDIT is useless and the corresponding memory page is reclaimed.

security

Two security issues affect Dyld:

  • ASLR: Randomize the address space layout. The image is loaded at a random address.
  • Code signing: To verify the signature of a Mach-O file at run time, rather than reading the entire file repeatedly each time, the contents of each page are generated as a separate cryptographic hash value and stored in __LINKEDIT. This allows the contents of each page of the file to be verified and not tampered with as they are read.

IOS startup optimization from Exec () to Main ()