A Preliminary Understanding of Link Map File

What is Link Map File Link Map File is translated into link mapping file in Chinese. It is a link information file generated while Xcode generates exe...

What is Link Map File

Link Map File is translated into link mapping file in Chinese. It is a link information file generated while Xcode generates executable file. It is used to describe the construction part of executable file, including the distribution of code segment and data segment. Xcode does not generate an executable file by default. Developers need to manually set Target - > Build Setting - > Write Link Map File to YES:

Here you can also set the location of Link Map, the default location is:

$(TARGET_TEMP_DIR)/$(PRODUCT_NAME)-LinkMap-$(CURRENT_VARIANT)-$(CURRENT_ARCH).txt

For example:

/Users/zhanggui/Library/Developer/Xcode/DerivedData/LinkMapTest-ffnpzjkbsmhwvdcxorqbxpyvjtob/Build/Intermediates.noindex/LinkMapTest.build/Debug-iphonesimulator/LinkMapTest.build/LinkMapTest-LinkMap-normal-x86_64.txt

Developers can also set the location of the file according to their own needs.

Composition of Link Map File

Open Link Map File, which contains the following parts:

1. Path
# Path: /Users/zhanggui/Library/Developer/Xcode/DerivedData/LinkMapTest-ffnpzjkbsmhwvdcxorqbxpyvjtob/Build/Products/Debug-iphonesimulator/LinkMapTest.app/LinkMapTest

Path is the path to generate executable files.

2. Arch
# Arch: x86_64

Arch refers to the type of architecture.

3. Object files:
# Object files: [ 0] linker synthesized [ 1] /Users/zhanggui/Library/Developer/Xcode/DerivedData/LinkMapTest-ffnpzjkbsmhwvdcxorqbxpyvjtob/Build/Intermediates.noindex/LinkMapTest.build/Debug-iphonesimulator/LinkMapTest.build/LinkMapTest.app-Simulated.xcent [ 2] /Users/zhanggui/Library/Developer/Xcode/DerivedData/LinkMapTest-ffnpzjkbsmhwvdcxorqbxpyvjtob/Build/Intermediates.noindex/LinkMapTest.build/Debug-iphonesimulator/LinkMapTest.build/Objects-normal/x86_64/ViewController.o [ 3] /Users/zhanggui/Library/Developer/Xcode/DerivedData/LinkMapTest-ffnpzjkbsmhwvdcxorqbxpyvjtob/Build/Intermediates.noindex/LinkMapTest.build/Debug-iphonesimulator/LinkMapTest.build/Objects-normal/x86_64/main.o [ 4] /Users/zhanggui/Library/Developer/Xcode/DerivedData/LinkMapTest-ffnpzjkbsmhwvdcxorqbxpyvjtob/Build/Intermediates.noindex/LinkMapTest.build/Debug-iphonesimulator/LinkMapTest.build/Objects-normal/x86_64/AppDelegate.o [ 5] /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.1.sdk/System/Library/Frameworks//Foundation.framework/Foundation.tbd [6]/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.1.sdk/usr/lib/libobjc.tbd [7]/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.1.sdk/System/Library/Frameworks//UIKit.framework/UIKit.tbd

Object Files lists all obj and tbd in the executable file. Each line represents the number of the document. For example, the ViewController.o file, numbered 2. The specific role of numbering will be introduced later.

4. Sections
# Sections: # Address Size Segment Section 0x100001730 0x00000333 __TEXT __text 0x100001A64 0x0000002A __TEXT __stubs 0x100001A90 0x00000056 __TEXT __stub_helper 0x100001AE6 0x00000A27 __TEXT __objc_methname 0x10000250D 0x0000003C __TEXT __objc_classname 0x100002549 0x0000086D __TEXT __objc_methtype 0x100002DB6 0x0000007A __TEXT __cstring 0x100002E30 0x00000182 __TEXT __entitlements 0x100002FB4 0x00000048 __TEXT __unwind_info 0x100003000 0x00000010 __DATA __nl_symbol_ptr 0x100003010 0x00000010 __DATA __got 0x100003020 0x00000038 __DATA __la_symbol_ptr 0x100003058 0x00000010 __DATA __objc_classlist 0x100003068 0x00000010 __DATA __objc_protolist 0x100003078 0x00000008 __DATA __objc_imageinfo 0x100003080 0x00000BE8 __DATA __objc_const 0x100003C68 0x00000010 __DATA __objc_selrefs 0x100003C78 0x00000008 __DATA __objc_classrefs 0x100003C80 0x00000008 __DATA __objc_superrefs 0x100003C88 0x00000008 __DATA __objc_ivar 0x100003C90 0x000000A0 __DATA __objc_data 0x100003D30 0x000000C0 __DATA __data

Literally, each Section contains Address, Size, Segment and Section. Before introducing, here's a brief introduction to the Mach-O file.
The Path in the first part above is the path to an executable file. Use iTerm to go in to the folder, and then use the file command to see the type of the file:

file LinkMapTest

The output results are as follows:

LinkMapTest: Mach-O 64-bit executable x86_64

As you can see, this file is in Mach-O format, which is the execution file format of iOS system applications. The virtual addresses in Mach-O files will eventually be mapped to physical addresses, which will be divided into different segment types: _ TEXT, _ DATA and _ LINKEDIT. The meaning of each paragraph is as follows:

  1. _ TEXT contains the code to be executed. These codes are read-only and executable
  2. _ DATA contains data that will be changed, such as global variables, static variables, etc., readable and writable, but not executable.
  3. _ LINKEDIT contains loader metadata, such as function name and address, read-only.

Segment s are then divided into different Section s, which store different information, such as _ objc method name as the name of the method.
In retrospect, Address is the starting position, Size is the size, Segment is the segment, Section.

5. Symbols
# Address Size File Name 0x100001730 0x0000003C [ 2] -[ViewController viewDidLoad] 0x100001770 0x00000092 [ 3] _main 0x100001810 0x00000080 [ 4] -[AppDelegate application:didFinishLaunchingWithOptions:] 0x100001890 0x00000040 [ 4] -[AppDelegate applicationWillResignActive:] 0x1000018D0 0x00000040 [ 4] -[AppDelegate applicationDidEnterBackground:] 0x100001910 0x00000040 [ 4] -[AppDelegate applicationWillEnterForeground:] 0x100001950 0x00000040 [ 4] -[AppDelegate applicationDidBecomeActive:] 0x100001990 0x00000040 [ 4] -[AppDelegate applicationWillTerminate:] 0x1000019D0 0x00000020 [ 4] -[AppDelegate window] 0x1000019F0 0x00000040 [ 4] -[AppDelegate setWindow:] 0x100001A30 0x00000033 [ 4] -[AppDelegate .cxx_destruct] 0x100001A64 0x00000006 [ 5] _NSStringFromClass 0x100001A6A 0x00000006 [ 7] _UIApplicationMain 0x100001A70 0x00000006 [ 6] _objc_autoreleasePoolPop 0x100001A76 0x00000006 [ 6] _objc_autoreleasePoolPush 0x100001A7C 0x00000006 [ 6] _objc_msgSendSuper2 0x100001A82 0x00000006 [ 6] _objc_retainAutoreleasedReturnValue 0x100001A88 0x00000006 [ 6] _objc_storeStrong 0x100001A90 0x00000010 [ 0] helper helper 0x100001AA0 0x0000000A [ 5] _NSStringFromClass 0x100001AAA 0x0000000A [ 6] _objc_autoreleasePoolPop 0x100001AB4 0x0000000A [ 6] _objc_autoreleasePoolPush 0x100001ABE 0x0000000A [ 6] _objc_msgSendSuper2 0x100001AC8 0x0000000A [ 6] _objc_retainAutoreleasedReturnValue 0x100001AD2 0x0000000A [ 6] _objc_storeStrong 0x100001ADC 0x0000000A [ 7] _UIApplicationMain 0x100001AE6 0x0000000C [ 2] literal string: viewDidLoad . . .

According to the starting address of Sections, Symbols can be divided into groups of Sections, for example, between 0x100001730 and 0x100001A64, which is the _ test code area.
Symbols contains information about:

  1. Address: Starting address
  2. Size: The size of memory occupied, represented here in hexadecimal.
  3. File: The Name's file number, which is the number in parentheses in the Object files section, for example, -[ViewController viewDidLoad] corresponds to a file number of 2. According to the Object files section, you can see that the file belongs to: ViewController.o. This allows you to calculate the size of memory occupied by an O file. Just add up the statistics of Symbols numbered o.
  4. Name is the name of the Sybmols.
6. Dead Stripped Symbols
# Dead Stripped Symbols: # Size File Name <<dead>> 0x00000018 [ 2] CIE <<dead>> 0x00000018 [ 3] CIE <<dead>> 0x00000006 [ 4] literal string: class <<dead>> 0x00000008 [ 4] literal string: v16@0:8 <<dead>> 0x00000018 [ 4] CIE . . .

The above is a brief introduction to Link map file.

ZLinkMapParser

It took two days to write a script file using Ruby based on the learning of Link Map File, which can easily calculate the size of memory occupied by components or tbd in the specified Link Map File, similar to:

AppDelegate.o 8.50KB ViewController.o 735B LinkMapDemo.app-Simulated.xcent 386B main.o 192B linker synthesized 128B libobjc.tbd 120B Foundation.tbd 24B UIKit.tbd 24B //Total size (for reference only): 10.07 KB

For more information, visit ZLinkMapParser

summary

  1. Apple development still has many details to learn.
  2. Learning a script language will also bring great convenience to normal development.

Reference resources

  1. Mach-O executable file
  2. iOS tuning | Deep understanding of Link Map File
  3. Composition of iOS APP executable file

For reprinting, please indicate the source: https://www.cnblogs.com/zhanggui/p/9991455.html

7 May 2019, 03:40 | Views: 2546

Add new comment

For adding a comment, please log in
or create account

0 comments