IOS enhancement: symbol

Time:2022-1-1
IOS enhancement: symbol

preface:
  • I’ve been in contact with bugly before, andBugly Document CenterHere are some contents of IOS symbol table. Symbol is a concept often contacted in daily development. Although there are few direct application scenarios in daily development, symbol compilation time and runtime play an important role. Today, let’s learn the symbol and prepare for the later study of dynamic library and static library.

  • reference resources:Handsome Camel

catalog:
    1. Definition of symbol
    1. Types of symbol tables
    1. Practical exploration of symbols
    1. Extension of symbols
  • 5 . Split strip command of symbol

1、 Definition of symbol

In the book “understanding computer systems in depth”, there is a paragraph on the method adopted by Linux compiling system:

At compile time, the compiler outputs each to the assemblerGlobal symbol, either strong or weak, and the assembler implicitly encodes this information inSymbol table of relocatable target fileInside. Functions and initialized global variables are strongly signed, and uninitialized global variables are weakly signed.

  • Symbol

The symbol is adata structure, including metadata such as name (string) and type. The symbol corresponds to the name of a function or dataaddress

Symbols directly affect the volume of the API

  • Symbol table

What is a symbol table?

Symbol table is a mapping table between memory address and function name, file name and line number. The symbol table elements are as follows:
<Start address> <End address> <function> [<File name: line number>]

  • Symbol Table: the symbol table stores the symbol information of the current file. Both the static linker (LD) and the dynamic linker (dyld) will read the symbol table during the linking process. In addition, the debugger will also use the symbol table to map symbols to the source file, that is, ⽤ to save symbols.

  • String Table: is to save the name of the symbol.

  • Indirect Symbol TableIndirect symbol table。 Save the external symbol that makes ⽤. The more accurate “point” is the symbol of the external dynamic library. yesSymbol TableSet of.

The data structure of symbols stored in the symbol table is as follows:

struct nlist_64 {  
   union {       
      uint32_t  n_strx; /* index into the string table */   
    } n_un;  
   uint8_t n_type;        /* type flag, see below */
   uint8_t n_sect;        /* p number or NO_SECT */    
   uint16_t n_desc;       /* see <mach-o/stab.h> */   
   uint64_t n_value;      /* value of this symbol (or stab offset) */
};

Position of symbol table in mach-o

IOS enhancement: symbol

Through twoLoad Commands, descriptionSymbol TableThe size and location of, and other metadata

  • LC_SYMTAB: the symbol table used to describe the file. This load command is used by both static and dynamic linkers when linking this file. The debugger can also use the load command to find debugging information

  • LC_DYSYMTABDescription: dynamic linker uses otherSymbol TableInformation,

definitionLC_SYMTABLoad command specific properties. stay/usr/include/mach-o/loader.hDefined in:

struct symtab_command {
    //Common properties. Indicates the loading command currently described, which is currently set to LC_ SYMTAB
    uint32_t cmd ;
    //Common properties. Indicates the size of the load command, which is currently set to sizeof (symtab_command)
    uint32_t cmdsize;
    //Represents the offset from the beginning of the file to the location of the symbol table. Symbol table is represented by [nlist]
    uint32_t symoff;
    //Number of symbols in the symbol table
    uint32_t nsyms;
    //Represents the offset from the beginning of the file to the location of the string table.
    uint32_t stroff;
    //Indicates the size of string table (in bytes)
    uint32_t strsize;
};

2、 Types of symbol tables

2.1 classification

By module:
  • Global symbol: the entire project is visible
  • Local symbol: the current class is visible
By location:
  • External symbol: the symbol is not in the current file and needs to be solved by LD or dyld when linking
  • Non external symbol: the symbol in the current file
According to function:
Type explain
f File
F Function
O Data
d Debug
ABS Absolute
COM Common
UND Undefined
By symbol type:
Type explain
U undefined(undefined)
A absolute(absolute symbol)
T text section symbol__TEXT.__text
D data section symbol__DATA.__data
B bss section symbol__DATA.__bss
C common symbol(can only appear inMH_OBJECTTypeMach-O⽂ middle part)
debugger symbol table
S In addition to the above ⾯, store in other placessectionFor example, uninitialized global variables are stored in(__DATA,__common)Medium
I indirect symbol(the same symbol information represents the same ⼀ symbol)
u Write in dynamic shared libraryuIndicates that there are undefined references to private external symbols in another module in the same library

Note: markingofType, ⼩ write for local symbol(local symbol

The lowercase letters in the nm command correspond to local symbols, and the uppercase letters represent global symbols; U means undefined, that is, undefined external symbols

IOS enhancement: symbol

3、 Practical exploration of symbols

IOS enhancement: symbol

3.1 visibility

A very common case is that you have 1000 functions, but only 10 functions are public. It is hoped that the final generated dynamic library does not contain the symbols of other 990 functions. At this time, you can use clang’s attribute:

//Symbols can be linked externally__ attribute__ ((visibility ("default")) // the symbol will not be placed in the dynamic symbol table, which means that it can no longer be linked by other compilation units__ attribute__ ((visibility("hidden")))

Clang provides a global switch to set the default visibility of symbols:

IOS enhancement: symbol

If the target of the dynamic library turns on this switch, it will be found that the dynamic library can still be compiled, but the app will report a bunch of link errors because the symbol becomes hidden.

But this is a common compilation method: let the symbol default to hidden, that is-fvisibility=hidden, and then manually add__attribute__((visibility("default")))

//Header file #define LH_ EXPORT __ attribute__ ((visibility("default")))LH_ EXPORT void method_ 1(void);// Implementation file LH_ EXPORT void method_ 1(){    NSLog(@"1");}

Global symbolandLocal symbol, their essential difference isvisibility

There are two kinds of visibility of a symbol:

  • default: default value. The defined symbol type is global or local
  • hidden: hide the global symbol and change it to a local symbol

Therefore, there are two ways to hide global symbols:

  • Decorate with static keyword
  • useattribute((visibility(“hidden”)))

give an example:

Case 1:

Open the project, which contains twoProject

IOS enhancement: symbol

comeLGOneFramework, openLGOneObject.mFiles, implementationglobal_objectfunction

IOS enhancement: symbol

stayLGOneObject.hIn the document, it is not exposedglobal_objectfunction

IOS enhancement: symbol

comeLGApp(another)Project), openViewController.mFiles, definitionsglobal_objectFunction, but not implemented. stayviewDidLoadCall in methodglobal_objectfunction

IOS enhancement: symbol

Run the project,LGOneFrameworkImplemented inglobal_objectThe function is called normally

IOS enhancement: symbol

Case 2:

If willLGOneFrameworkMediumglobal_objectFunctions, usingstaticKeyword modification

IOS enhancement: symbol

stayLGAppIn,global_objectThe calling code of the function remains unchanged

IOS enhancement: symbol

hereglobal_objectThe function becomes a local symbol and is visible only to the current file. So inLGAppCall this function, compile and report errors, and prompt undefined symbols.

IOS enhancement: symbol

Case 3:

stayLGOneFrameworkIn, implementglobal_objectFunction, not usedstaticKeyword modification

IOS enhancement: symbol

stayLGAppofViewController.mIn, it is also realizedglobal_objectfunction

IOS enhancement: symbol

Run the project, callingLGAppMediumglobal_objectfunction

IOS enhancement: symbol

Case 3Why is there no conflict?
stayLGAppandLGOneFrameworkTwoProjectIn, all are definedglobal_objectFunction, forglobal_objectFunctions, they are actually stored in twoMach-OYes. Because the compiler has the concept of ⼆ level namespace, there are twoglobal_objectThe symbols of functions are actually different

two_levelnamespace & flat_namespace
⼆ level namespace and ⼀ level namespace. By default, the linker adopts a ⽤ level namespace, that is, in addition to recording the symbol name, it also records which symbol belongs toMach-OYes, ⽐ if it will be recorded_NSLogCome onFoundation

3.2 scope

Import symbolandExport symbol

  • Because the dynamic library does not know how to use it outside, the best way is to export all the symbols exposed in all header files. From the perspective of package size, it must be the symbols used and the code corresponding to which symbols are retained. LD provides such a scheme throughexported_symbolTo keep only specific symbols

export symbol: exporting symbols means telling other modules that I have ⼀ such symbols and you can export them(Import)。

withNSLogFor example:

NSLog(@"%d", static_init_value);

NSLogstore inFoundationIn the library

  • aboutFoundationFor Ku,NSLogIs an exported symbol for external use
  • For the current program,NSLogSubordinateFoundationSymbols imported from the library

Export symbolnamelyGlobal symbol

Global variables defined in the project are generated into global symbols, which will be exported by default. These symbols can be viewed and used by the outside world

useobjdump --macho --exports-trie ${MACH_PATH}Command to view exported symbols

Exports trie:
0x100000000  __mh_execute_header
0x100003F50  _main
0x100008010  _global_init_value
0x100008020  _global_uninit_value

  • Only the above four export symbols correspond to the four global symbols in the symbol table

The dynamic library is loaded only at run time, and only symbols are provided at the compile link stage.Mach-OThe dynamic library symbols used in are saved in the indirect symbol table

useobjdump --macho --indirect-symbols ${MACH_PATH}Command view indirect symbol table

Indirect symbols for (__TEXT,__stubs) 1 entries
address            index name
0x0000000100003f90     8 _NSLog
Indirect symbols for (__DATA_CONST,__got) 1 entries
address            index name
0x0000000100004000    10 dyld_stub_binder
Indirect symbols for (__DATA,__la_symbol_ptr) 1 entries
address            index name
0x0000000100008000     8 _NSLog

  • Symbol inMach-OWhen splitting symbols, the indirect symbol table cannot be deleted
  • Mach-OThe global symbols of the dynamic library used cannot be deleted
  • The dynamic library can only split all symbols that are not global symbols

seeOCSymbols in

openLGOneObject.mFile, write the following code:

#import "LGOneObject.h"

@interface LGOneObject : NSObject

- (void)testOneObject;

@end

@implementation LGOneObject

- (void)testOneObject {
    NSLog(@"testOneObject");
}

@end

useobjdump --macho --exports-trie ${MACH_PATH}Command to view exported symbols

Exports trie:
0x100000000  __mh_execute_header
0x100003F20  _main
0x1000080B8  _OBJC_METACLASS_$_LGOneObject
0x1000080E0  _OBJC_CLASS_$_LGOneObject
0x100008110  _global_init_value
0x100008120  _global_uninit_value

OCThe default isGlobal symbol, but alsoExport symbol。 They can be viewed and used by the outside world and will increaseMach-OVolume of

developmentOCWhen using dynamic libraries, you want to reduceMach-OThe external symbols that do not need to be used are stripped. At this point, you can use the linker toSymbols you don't want to exposeDeclare asDo not export symbols

openxcconfigFiles, addingOTHER_LDFLAGSConfiguration item

OTHER_LDFLAGS=$(inherited) -Xlinker -unexported_symbol -Xlinker _OBJC_CLASS_$_LGOneObject
OTHER_LDFLAGS=$(inherited) -Xlinker -unexported_symbol -Xlinker _OBJC_METACLASS_$_LGOneObject
  • take_OBJC_CLASS_$_LGOneObjectDeclared not to export symbols
  • take_OBJC_METACLASS_$_LGOneObjectDeclared not to export symbols

Compile the project, and thenOCThe two exported symbols of have been hidden

Exports trie:
0x100000000  __mh_execute_header
0x100003F20  _main
0x100008110  _global_init_value
0x100008120  _global_uninit_value
  • hideOCSymbols that do not want to be exposed need to be declared as non exported symbols with the help of linker
  • becauseOCIs a runtime language and cannot be used directlyvisibility("hidden")
  • Do not export symbols, change global symbols into local symbols, and these symbols can be stripped to reduceMach-OVolume of
  • Hide symbols that do not need to be exposed, so as to avoid being viewed and used by the outside world and solve potential safety hazards

Another method provided by the linker: specify a file and declare all symbols in the file as non exported symbols

establishsymbol.txtFile, put it into the project directory, and define the symbols you don’t want to expose

_OBJC_CLASS_$_LGOneObject
_OBJC_METACLASS_$_LGOneObject
_global_init_value
_global_uninit_value

openxcconfigFiles, addingOTHER_LDFLAGSConfiguration item

OTHER_LDFLAGS=$(inherited) -Xlinker -unexported_symbols_list >$(PROJECT_DIR)/symbol.txt

Compile the project, and thensymbol.txtThe four symbols defined in the file have been hidden

Exports trie:
0x100000000  __mh_execute_header
0x100003F20  _main

3.3 Weak Symbol

Weakly defined symbol

Weak Defintion Symbol: indicates that this symbol is weakly defined. If the static or dynamic linker finds another (⾮ weak) definition for this symbol, the weak definition is ignored. Only symbols in merged sections can be marked as weak definitions

openWeakSymbol.mFile, write the following code:

#import "WeakSymbol.h"
#import <Foundation/Foundation.h>

void weak_function(void) {
   NSLog(@"weak_function");
}
  • hereweak_functionIs a global symbol and also an export symbol

openWeakSymbol.hFile, write the following code:

void weak_function(void)  __attribute__((weak));
  • use__attribute__((weak))takeweak_functionDeclare as weakly defined symbol

useobjdump --macho --exports-trie ${MACH_PATH}Command to view exported symbols

Exports trie:
0x100000000  __mh_execute_header
0x100003EF0  _weak_function [weak_def]
0x100003F30  _main
0x100008010  _global_init_value
0x100008020  _global_uninit_value
  • weak_functionOr export symbol, which also proves that it is still a global symbol, and then it is added[weak_def]Mark of

Role of weakly defined symbols

stayWeakSymbol.mandmain.mAll of them implement oneweak_functionfunction

void weak_function(void) {
   NSLog(@"weak_function");
}

SameProjectIn, two identical global symbols appear. At this time, an error is reported during compilation, prompting the occurrence of duplicate symbols

IOS enhancement: symbol

One of themweak_functionThe function is declared as a weakly defined symbol, and the compilation is successful

void weak_function(void)  __attribute__((weak));
  • The function of weakly defined symbols: it can solve the conflict of symbols with the same name; After the linker finds the implementation of one symbol, the symbols with the same name in other places will be ignored

If used at the same timeweakandvisibility("hidden"), the symbol becomes a weakly defined local symbol

openWeakSymbol.mFile, write the following code:

void weak_hidden_function(void) {
   NSLog(@"weak_hidden_function");
}

openWeakSymbol.hFile, willweak_hidden_functionFunctions are used at the same timeweakandvisibility("hidden")modification

void weak_hidden_function(void) __attribute__((weak, visibility("hidden")));

useobjdump --macho --syms ${MACH_PATH}Command view symbol table

SYMBOL TABLE:
0000000100003f10 lw    F __TEXT,__text _weak_hidden_function
0000000100008008 l     O __DATA,__data __dyld_private
0000000100008014 l     O __DATA,__data _static_init_value
0000000100008018 l     O __DATA,__bss _static_uninit_value
0000000100008028 l     O __DATA,__common _default_x
0000000100000000 g     F __TEXT,__text __mh_execute_header
0000000100008010 g     O __DATA,__data _global_init_value
0000000100008020 g     O __DATA,__common _global_uninit_value
0000000100003f30 g     F __TEXT,__text _main
0000000100003ef0 gw    F __TEXT,__text _weak_function
0000000000000000         *UND* _NSLog
0000000000000000         *UND* ___CFConstantStringClassReference
0000000000000000         *UND* dyld_stub_binder

  • here_weak_hidden_functionMarked aslw, becomes weakly defined local symbol
Weak reference symbol

Weak Reference Symbol: indicates that this undefined symbol is a weak reference ⽤. If the dynamic linker cannot find the definition of the symbol, set it to0。 The linker sets this symbol as a weak link flag

openWeakImportSymbol.hFile, write the following code:

void weak_import_function(void) __attribute__((weak_import));

  • use__attribute__((weak_import))takeweak_import_functionDeclared as a reference symbol
  • There is no in the project at this timeweak_import_functionFunction implementation

openmain.mFile, write the following code:

#import <Foundation/Foundation.h>
#import "WeakImportSymbol.h"

int main(int argc, char *argv[]) {
   if (weak_import_function) {
       weak_import_function();
   }
   return 0;
}

becauseweak_import_functionFunction is not implemented, but inmain.mIs used in. At this time, an error is reported during compilation, indicating that no symbol is defined

IOS enhancement: symbol

  • When importing.hWhen using a header file and symbols, similar toAPITo use, just find the declaration of the symbol. Even if the function is not implemented, the object file can be generated. However, when linking to generate an executable file, you need to know the specific location of the symbol. If the function is not implemented, an error message will appear: no symbol is defined

To solve the problem of using weak reference symbols, you can declare symbols as dynamic links through linkers

useman ldCommand to view linker parameters:

IOS enhancement: symbol

  • -U: indicates that the symbol is undefined and needs to be dynamically searched at run time

openxcconfigFiles, addingOTHER_LDFLAGSConfiguration item

OTHER_LDFLAGS=$(inherited) -Xlinker -U -Xlinker _weak_import_function
  • At this point, the project can be compiled successfully
  • adopt-UParameter to tell the linker that this symbol is dynamically linked, so in the link phase, even if it is an undefined symbol, ignore it and ignore it. Because at run time, the dynamic linker will find it automatically

Run the project, althoughweak_import_functionThe function is not implemented, but the operation will not report an error

  • becausemainCall in functionweak_import_functionBefore functionif (weak_import_function)Judgment of
  • When the dynamic linker cannot find the definition of the symbol, it sets it to0。 thereforeweak_import_functionThe function is not called

Role of weak reference symbols

  • Declaring a symbol as a weak reference symbol can avoid errors in compiling links. Add condition judgment before calling, and no error will be reported at run time
  • When using a dynamic library, you can declare the whole dynamic library as a weak reference. At this time, even if the dynamic library is not imported, there will be no error that the dynamic library is not found
Common Symbol

Uninitialized global symbol when defined

For example:main.mFile, uninitializedglobal_uninit_valueGlobal variable, which belongs toCommon Symbol

int global_uninit_value;

openmain.mFile to define two global variables with the same name. One is initialized and the other is not initialized. This operation will not report an error

int global_init_value = 10;
int global_init_value;
  • Common SymbolRole of
  • In the process of compiling and linking, if defined symbols are found, the undefined symbols will be deleted automatically
  • During the linking process, the linker will change undefined symbols into forcibly defined symbols by default

Linker settings:

  • -d: mandatory definitionCommon Symbol
  • -commons: specify treatmentCommon SymbolHow to respond
Re export symbols

withNSLogFor example:

IOS enhancement: symbol

  • For the current program,NSLogBelongs to an undefined symbol stored in the indirect symbol table

NSLogIt can be used in the current program. If you want other programs using this program to use it, you need to re export this symbol. The re exported symbols will be placed in the exported symbol table and can only be viewed and used by the outside world

useman ldCommand to view linker parameters:

IOS enhancement: symbol

  • -alias: aliases can only be created for symbols in the indirect symbol table. Alias symbols have global visibility

openxcconfigFiles, addingOTHER_LDFLAGSConfiguration item

OTHER_LDFLAGS=$(inherited) -Xlinker -alias -Xlinker _NSLog -Xlinker Cat_NSLog

  • to_NSLogSymbol creationCat_NSLogalias

usenm -m ${MACH_PATH} | grep "Cat_NSLog"Command view symbol table, specifying"Cat_NSLog"keyword

(indirect) external Cat_NSLog (for _NSLog)

  • hereCat_NSLogIs an indirect external symbol, is_NSLogAlias for symbol

useobjdump --macho --exports-trie ${MACH_PATH}Command to view exported symbols

Exports trie:
0x100000000  __mh_execute_header
0x100003F20  _main
0x100008018  _global_init_value
0x100008028  _global_uninit_value
[re-export] Cat_NSLog (_NSLog from Foundation)

  • Cat_NSLogIs the exported symbol and is marked as[re-export]For re exporting symbols

Role of re exported symbols

  • Declaring symbols in an indirect symbol table as re exported symbols can be used by other programs using this program
  • When program linksA dynamic library, andA dynamic libraryLink againB dynamic libraryWhen,B dynamic libraryIt is invisible to the program. At this time, you can use the re export method to makeB dynamic libraryVisible to program

4、 Extension of symbols

View information such as third-party libraries and symbols used by the project

Through the linker, you can view information such as third-party libraries and symbols used in the current project

useman ldCommand to view linker parameters:

IOS enhancement: symbol

  • -map: exports all symbol details to the specified file

openxcconfigFiles, addingOTHER_LDFLAGSConfiguration item

OTHER_LDFLAGS=$(inherited) -Xlinker -map -Xlinker $(PROJECT_DIR)/export.txt

Compile the project, and there are more in the project directoryexport.txtfile

IOS enhancement: symbol

openexport.txtfile

# Path: /Users/zang/Library/Developer/Xcode/DerivedData/MachOAndSymbol-bdzlylfoorwnhggerxdmwocpeyad/Build/Products/Debug/MachOAndSymbol
# Arch: x86_64
# Object files:
[  0] linker synthesized
[  1] /Users/zang/Library/Developer/Xcode/DerivedData/MachOAndSymbol-bdzlylfoorwnhggerxdmwocpeyad/Build/Intermediates.noindex/MachOAndSymbol.build/Debug/MachOAndSymbol.build/Objects-normal/x86_64/LGOneObject.o
[  2] /Users/zang/Library/Developer/Xcode/DerivedData/MachOAndSymbol-bdzlylfoorwnhggerxdmwocpeyad/Build/Intermediates.noindex/MachOAndSymbol.build/Debug/MachOAndSymbol.build/Objects-normal/x86_64/main.o
[  3] /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks//Foundation.framework/Foundation.tbd
# Sections:
# Address   Size        Segment Section
0x100003EF0 0x0000006F  __TEXT  __text
0x100003F60 0x00000006  __TEXT  __stubs
0x100003F68 0x0000001A  __TEXT  __stub_helper
0x100003F82 0x00000011  __TEXT  __cstring
0x100003F93 0x0000000C  __TEXT  __objc_classname
0x100003F9F 0x0000000E  __TEXT  __objc_methname
0x100003FAD 0x00000008  __TEXT  __objc_methtype
0x100003FB8 0x00000048  __TEXT  __unwind_info
0x100004000 0x00000008  __DATA_CONST    __got
0x100004008 0x00000040  __DATA_CONST    __cfstring
0x100004048 0x00000008  __DATA_CONST    __objc_classlist
0x100004050 0x00000008  __DATA_CONST    __objc_imageinfo
0x100008000 0x00000008  __DATA  __la_symbol_ptr
0x100008008 0x000000B0  __DATA  __objc_const
0x1000080B8 0x00000050  __DATA  __objc_data
0x100008108 0x00000010  __DATA  __data
0x100008118 0x00000004  __DATA  __bss
0x100008120 0x00000010  __DATA  __common
# Symbols:
# Address   Size        File  Name
0x100003EF0 0x00000027  [  1] -[LGOneObject testOneObject]
0x100003F20 0x0000003F  [  2] _main
0x100003F60 0x00000006  [  3] _NSLog
0x100003F68 0x00000010  [  0] helper helper
0x100003F78 0x0000000A  [  3] _NSLog
0x100003F82 0x0000000E  [  1] literal string: testOneObject
0x100003F90 0x00000003  [  2] literal string: %d
0x100003F93 0x0000000C  [  1] literal string: LGOneObject
0x100003F9F 0x0000000E  [  1] literal string: testOneObject
0x100003FAD 0x00000008  [  1] literal string: [email protected]:8
0x100003FB8 0x00000048  [  0] compact unwind info
0x100004000 0x00000008  [  0] non-lazy-pointer-to-local: dyld_stub_binder
0x100004008 0x00000020  [  1] CFString
0x100004028 0x00000020  [  2] CFString
0x100004048 0x00000008  [  1] objc-cat-list
0x100004050 0x00000008  [  0] objc image info
0x100008000 0x00000008  [  3] _NSLog
0x100008008 0x00000048  [  1] __OBJC_METACLASS_RO_$_LGOneObject
0x100008050 0x00000020  [  1] __OBJC_$_INSTANCE_METHODS_LGOneObject
0x100008070 0x00000048  [  1] __OBJC_CLASS_RO_$_LGOneObject
0x1000080B8 0x00000028  [  1] _OBJC_METACLASS_$_LGOneObject
0x1000080E0 0x00000028  [  1] _OBJC_CLASS_$_LGOneObject
0x100008108 0x00000008  [  0] __dyld_private
0x100008110 0x00000004  [  2] _global_init_value
0x100008114 0x00000004  [  2] _static_init_value
0x100008118 0x00000004  [  2] _static_uninit_value
0x100008120 0x00000008  [  2] _global_uninit_value
0x100008128 0x00000008  [  2] _default_x

  • The file contains the target file generated when compiling the link, the third-party library used in the project, and theSectionsandSymbolsOther information
Name and function of section
name effect
TEXT.text Executable machine code
TEXT.cstring After weight removalCcharacter string
TEXT.const Initialized constants
TEXT.stubs Symbol pile.lazybindingThe code of the address pointed to by the corresponding entry pointer of the table
TEXT.stub_ helper Auxiliary functions. When inlazybindingWhen the real symbolic address represented by the pointer of the corresponding item is not found in the table of, point to this
TEXT.unwind_info Store and handle exception information
TEXT.eh_frame Debugging auxiliary information
DATA.data Initialized variable data
DATA.nI_symbol_ptr wronglazy-bindingThe pointer in each table points to a symbol searched by the dynamic linker during loading
DATA.Ia_symbol_ptr lazy-bindingThe pointer in each table points tostub_helper
DATA.const Uninitialized constants
DATA.mod_init_func Initialization function, inmainPrevious call
DATA.mod_term_func Termination function, inmainAfter return, call
DATA.bss No static variables initialized
DATA.common There is no initialized symbol declaration(for example, int I;
Swift symbol table

openSwiftSymbol.swiftFile, write the following code:

public class LGSwiftClassSymbol {
   func testSwiftSymbol() {
   }
}

useobjdump --macho --syms ${MACH_PATH} | grep "Swift"Command view symbol table, specifyingSwiftkeyword

0000000100003f8a lw    O __TEXT,__swift5_typeref _symbolic _____ 14MachOAndSymbol012LGSwiftClassC0C
0000000100003f90 l     O __TEXT,__swift5_fieldmd _$s14MachOAndSymbol012LGSwiftClassC0CMF
0000000100008020 l     O __DATA,__objc_const __METACLASS_DATA__TtC14MachOAndSymbol18LGSwiftClassSymbol
0000000100008068 l     O __DATA,__objc_const __DATA__TtC14MachOAndSymbol18LGSwiftClassSymbol
00000001000080e8 l     O __DATA,__data _$s14MachOAndSymbol012LGSwiftClassC0CMf
0000000100003d90 g     F __TEXT,__text _$s14MachOAndSymbol012LGSwiftClassC0C09testSwiftC0yyF
0000000100003f78 g     O __TEXT,__const _$s14MachOAndSymbol012LGSwiftClassC0C09testSwiftC0yyFTq
0000000100003e10 g     F __TEXT,__text _$s14MachOAndSymbol012LGSwiftClassC0CACycfC
0000000100003f80 g     O __TEXT,__const _$s14MachOAndSymbol012LGSwiftClassC0CACycfCTq
0000000100003e40 g     F __TEXT,__text _$s14MachOAndSymbol012LGSwiftClassC0CACycfc
0000000100003e60 g     F __TEXT,__text _$s14MachOAndSymbol012LGSwiftClassC0CMa
00000001000080c0 g     O __DATA,__data _$s14MachOAndSymbol012LGSwiftClassC0CMm
0000000100003f44 g     O __TEXT,__const _$s14MachOAndSymbol012LGSwiftClassC0CMn
00000001000080f8 g     O __DATA,__data _$s14MachOAndSymbol012LGSwiftClassC0CN
0000000100003dd0 g     F __TEXT,__text _$s14MachOAndSymbol012LGSwiftClassC0CfD
0000000100003db0 g     F __TEXT,__text _$s14MachOAndSymbol012LGSwiftClassC0Cfd
0000000000000000         *UND* _OBJC_CLASS_$__TtCs12_SwiftObject
0000000000000000         *UND* _OBJC_METACLASS_$__TtCs12_SwiftObject

  • Find all that containSwiftKeyword symbols, many of which are marked asgGlobal symbol for

openSwiftSymbol.swiftFiles, modifyingLGSwiftClassSymbolClass, changed toprivatemodification

private class LGSwiftClassSymbol {
   func testSwiftSymbol() {
   }
}

useobjdump --macho --syms ${MACH_PATH} | grep "Swift"Command view symbol table, specifyingSwiftkeyword

0000000100003d10 l     F __TEXT,__text _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLC09testSwiftC0yyF
0000000100003d30 l     F __TEXT,__text _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLCfd
0000000100003d50 l     F __TEXT,__text _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLCfD
0000000100003d90 l     F __TEXT,__text _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLCMa
0000000100003db0 l     F __TEXT,__text _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLCADycfC
0000000100003de0 l     F __TEXT,__text _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLCADycfc
0000000100003f1c lw    O __TEXT,__const _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLCMXX
0000000100003f44 l     O __TEXT,__const _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLCMn
0000000100003f78 l     O __TEXT,__const _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLC09testSwiftC0yyFTq
0000000100003f80 l     O __TEXT,__const _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLCADycfCTq
0000000100003f8a lw    O __TEXT,__swift5_typeref _symbolic _____ 14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLC
0000000100003f90 l     O __TEXT,__swift5_fieldmd _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLCMF
0000000100008020 l     O __DATA,__objc_const __METACLASS_DATA__TtC14MachOAndSymbolP33_66093EBE10D00815F1A5CBD65FFF466118LGSwiftClassSymbol
0000000100008068 l     O __DATA,__objc_const __DATA__TtC14MachOAndSymbolP33_66093EBE10D00815F1A5CBD65FFF466118LGSwiftClassSymbol
00000001000080c0 l     O __DATA,__data _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLCMm
00000001000080e8 l     O __DATA,__data _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLCMf
00000001000080f8 l     O __DATA,__data _$s14MachOAndSymbol012LGSwiftClassC033_66093EBE10D00815F1A5CBD65FFF4661LLCN
0000000000000000         *UND* _OBJC_CLASS_$__TtCs12_SwiftObject
0000000000000000         *UND* _OBJC_METACLASS_$__TtCs12_SwiftObject

  • Those previously marked asgGlobal symbols, all become local symbols
$(SRCROOT)and$(PROJECT_DIR)Differences between
  • $(SRCROOT)Represents the root directory of the project
  • $(PROJECT_DIR)It represents the whole project

When adding files to a project, for example.aFile, firstShow in Finder, copy to the project directory, and then drag toxcodeIn the project

And sometimes.aNot in project directory. For example, in the parent directory of a project, it can be written as:$(SRCROOT)/../Spark/libSDK。 among/../Is to point to the parent directory

5、 Split strip command of symbol

  • Some symbols in the symbol table are necessary, but many symbols are removed. Strip is often used to remove some symbol table and debugging symbol table information in the target file to reduce the size of the program;
  • Strip command: removes or modifies symbols in the symbol table. App can remove all symbols from local symbols and global symbols, leaving only the symbols in the indirect symbol table.
5.1 how to remove debugging symbols
IOS enhancement: symbol

5.2 how to remove all symbols
IOS enhancement: symbol

  • The static library removes symbols. The symbols placed in the relocation symbol table cannot be removed. Only debugging symbols can be removed
IOS enhancement: symbol

Debug symbols: generated by the assembler O file, a debugging information in dwarf format will be generated, which will be put into__ The dwarf segment, when linked, will__ The dwarf segment is placed in the symbol table, and all symbols are placed in the symbol table after linking

  • The dynamic library is de signed. As long as it is not a global symbol, it can be killed (non global symbols)

How to remove non global symbols

IOS enhancement: symbol

When a static library is linked, the symbols in the static library will be merged into the app, but will not be stored in the indirect symbol table. Then, when the app is de signed, the volume can be reduced

Link a dynamic library, and the symbol table in the dynamic library will be placed in the indirect symbol table of app. When the app is de signed, the indirect symbol table cannot be removed

5.3 strip style selection of build setting
  • Debugging Symbols
  • All Symbols
  • Non-Global Symbols
IOS enhancement: symbol

5.4 timing of strip execution

After compiling and executing the script, before signing, so this is the post stripping symbol, which has no effect

IOS enhancement: symbol

5.5 strip stripping symbol
  • -x : non_global
  • No parameter: represents all symbols
  • -S: Peel debug symbols

App slimming:

    1. Optimize – O1, – OZ when compiling production target files;
    1. Dead code strip code stripping at the time of link;
    1. Strip the symbol after the macho file is produced;

Pass parameter – s to linker (remove debug symbol)

IOS enhancement: symbol

Recommended Today

Could not get a resource from the pool when the springboot project starts redis; nested exception is io. lettuce. core.

resolvent: Find your redis installation path: Start redis server Exe After successful startup: Restart project resolution. ———————————————————————->Here’s the point:<——————————————————————- Here, if you close the redis command window, the project console will report an error. If you restart the project, the same error will be reported at the beginning, The reason is: It is inconvenient to […]