preface

Learning is like rowing upstream; not to advance is to drop back. ‘!!!!!!!!!!

Idle, I came to dig gold fishing. Please tell us about Symbol Table. It’s an article. It’s not an article. Take my record as it is, I am idle to kill time, if there are some mistakes, please correct them. Finally, don’t be stingy with your likes and followings. Your likes and followings are my driving force, and my thick skin is getting a little rough.

Symbol Table

With two Load Commands ғ

  • LC_SYMTAB

  • LC_DYSYMTAB: describes the dynamic linker using other Symbol Table information

Used to describe the size and location of the Symbol Table, as well as other metadata.

LC_SYMTAB

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

symtab_command

Define the LC_SYMTAB load command properties. Define in usr/include/mach-o/loader.h:

Struct symtab_command {// common attributes. Specifies the current described load command, currently set to LC_SYMTAB uint32_t CMD; // Share attributes. Sizeof (symtab_command) uint32_t cmdsize; // Represents the offset from the beginning of the file to the location of the Symbol table. Uint32_t nsyms; // Uint32_t stroff // uint32_t strsize; // Uint32_t strsize; };Copy the code

nlist

To define the specific meaning of a symbol:

Struct nlist{// struct nlist{// struct nlist{// struct nlist{// // index long n_strx; } n_un; Unsigned char n_type; /*type flag, see below */ unsigned char n_sect; /* section number or NO_SECT */ shortn_desc; N_sesc; /* see <mach-o/stab.h> */ unsigned long n_value; /* value of this symbol (or stab offset) */};Copy the code
  • n_type

1 byte, saving data with a four-digit mask:

    • N_STAB(0xe0) : If the current n_type contains any of these three bits, the symbol is stab. In this case, the whole
    • The n_type field will be interpreted as stab value. See /usr/include/mach-o/stab.h for a valid stab value.
    • N_PEXT(0x10) : If the current n_type contains this bit. Mark this symbol as a private external symbol __ private_extern__(visibility=hidden), which can only be referenced and accessed within the program. When a file is linked through a static linker, do not convert it to a static symbol (this can be turned off by ld’s -keep_private_externs).
    • N_TYPE(0x0e) : If the current N_TYPE contains this bit. This symbol is an external symbol. The symbol is defined outside or in the file, but can be used in other files. #####N_TYPE The values of the N_TYPE field include:
    • N_UNDF(0x0): This symbol is undefined. An undefined symbol is a symbol referenced in the current module but defined in another module. The n_sect field is set to NO_SECT.
    • N_ABS(0x2) : This symbol is absolute. The linker does not change the value of the absolute symbol. The n_sect field is set to NO_SECT
    • N_SECT(0xE) : This symbol is defined in the segment number specified in the N_SECT.
    • N_PBUD(0xC) : This symbol is undefined and the mirror uses the pre-bound value of this symbol. The n_sect field is set to NO_SECT.
    • N_INDR(0xA) : This symbol is defined to be identical to another symbol. The n_value field is the index in the String table that specifies the name of another symbol. When the symbol is linked, it has the same definition type and value as another symbol.

Stab Value includes:

#define N_GSYM 0x20 /* Procedure name (f77 kludge): Name,,NO_SECT,0,0 */ #define N_FUN 0x24 /* method/function name,,n_sect,linenumber,address */ #define N_STSYM 0x26 /* static symbol :name,,n_sect,type,address */ #define N_LCSYM 0x28 /*. Lcomm symbol ݩ :name,,n_sect,type,address */ #define N_BNSYM 0x2e /* Nsect ᒧ ݩ ୏ ত : 0,,n_sect,0,address */ #define N_OPT 0x3c /* emitted with gcc2_compiled and in gcc source */ #define N_RSYM 0x40 /* Register symbols :name,,NO_SECT,type,register */ #define N_SLINE 0x44 0,,n_sect,linenumber,address */ #define N_ENSYM 0x4E /* NSect end: 0,,n_sect,0,address */ #define N_SSYM 0x60 /* struct_offset */ #define N_SO 0x64 Name,,n_sect,0,address */ #define N_OSO 0x66 Name,,0,0,st_mtime */ #define N_LSYM 0x80 /* local symbols :name, NO_SECT,type,offset */ #define N_BINCL 0x82 /* include file start: Name,,NO_SECT,0,sum */ #define N_SOL 0x84 Name,,n_sect,0,address */ #define N_PARAMS 0x86 /* Compiler parameters: name,,NO_SECT,0,0 */ #define N_VERSION 0x88 / Name,,NO_SECT,0,0 */ #define N_OLEVEL 0x8A /* compiler -o level :name,,NO_SECT,0,0 */ #define N_PSYM 0xa0 /* ݇ NO_SECT,type,offset */ #define N_EINCL 0xA2 /* include file End Name,,NO_SECT,0,0 */ #define N_ENTRY 0xa4 /* alternate entry: Name,,n_sect,linenumber,address */ #define N_LBRAC 0xc0 / Nesting level,address */ #define N_EXCL 0xc2 /* Deleted include file: Name,,NO_SECT,0,sum */ #define N_RBRAC 0xe0 /* ݦ Right bracket: 0,,NO_SECT,nesting level,address */ #define N_BCOMM 0xe2 /* Common to begin: Name,,NO_SECT,0,0 */ #define N_ECOMM 0xe4 /* general end: Name,n_sect,0,0 */ #define N_ECOML 0xe8 /* end common (local name): 0,,n_sect,0,address */ #define N_LENG 0xfe /* second stab entry with length information */ /* * for the berkeley pascal compiler, pc(1): */ #define N_PC 0x30 /* global pascal symbol: name,,NO_SECT,subtype,line */Copy the code
  • n_sect

An integer used to find the symbol in the numbered section; NO_SECT if the symbol is not found in any part of the image. The sections are numbered consecutively, starting with 1, based on the order in which they appear in the LC_SEGMENT load command.

  • n_desc

16-bit value used to describe non – modal symbols. The third bit uses REFERENCE_TYPE:

  • REFERENCE_FLAG_UNDEFINED_NON_LAZY(0x0) : This symbol is a reference to an external non-delayed (data) symbol
  • REFERENCE_FLAG_UNDEFINED_LAZY(0x1) : This symbol is a reference to an external lateness symbol (that is, a function call)
  • REFERENCE_FLAG_DEFINED(0x2) : This symbol is defined in this module
  • REFERENCE_FLAG_PRIVATE_DEFINED(0x3) : This symbol is defined in this module, but is only visible to modules in the shared library.
  • REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY(0x4) : This symbol is defined in another module of the file, is a non-lazy loading (data) symbol, and is only visible to modules in the shared library
  • REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY(0x5) : This symbol is defined in another module of the file, is a lazy loading (function) symbol, and is only visible to modules in the shared library.
In addition, the following identification bits can be set:Copy the code
  • REFERENCED_DYNAMICALLY(0x10) : Symbols defined must be used in dynamic loading (such as DLSYM and NSLookupSymbolInImage) Instead of a normal undefined symbol reference. Strip uses this bit to avoid removing symbols that must exist (if the symbol sets this bit, Strip does not strip it back).
  • N_DESC_DISCARDED(0x20) : Dynamic linker may use this symbol when a fully linked image is run. Do not set this bit in a fully linked image.
  • N_NO_DEAD_STRIP(0x20) : Symbol setting defined in a relocatable object file of type MH_OBJECT indicates that the static linker does not dead-strip the symbol. (Note that N_DESC_DISCARDED(0x20) is used for two different woods.)
  • N_WEAK_REF(0x40) : indicates a weak reference when this symbol is not defined. If the dynamic linker cannot find the definition of the symbol, its symbolic address is set to 0. The static linker sets the symbol to a weak link flag.
  • N_WEAK_DEF(0x80) : Indicates that this symbol is a weakly defined symbol. If the static or dynamic linker finds another (non-weak) definition for this symbol, the weak definition is ignored. Symbols in the merged section can only be marked as weakly defined.

If the file is a two-level named namespace image (that is, if the MH_TWOLEVEL flag is set in mach_header), the high 8 bits of n_desc indicate the number of the library that defines this undefined symbol. Use the GET_LIBRARY_ORDINAL macro to get this value, or use the SET_LIBRARY_ORDINAL macro to set this value. 0 specifies the current image. 1 to 253 indicates the library number based on the order of the LC_LOAD_DYLIB commands in the file. 254 for undefined symbols that need to be looked up dynamically (supported only in OSX V10.3 and later). For plug-ins that load symbols from executable programs. 255, which specifies the executable image. For flat Namespaceimages, the high 8 bits must be 0.

  • n_value

    Symbolic value. The value is expressed differently for each item in the Symbol Table (as explained by the n_type field). For the N_SECT symbol type, n_value is the address of the symbol. For information about other possible values, see the description of the n_type field.

    The Common symbols must be of type N_UNDF and the N_EXT bit must be set. The n_value of Common Symbols is the size (in bytes) of the data represented by the symbol. In C, the Common Symbol is a variable declared in the file but not initialized. Common Symbols can only appear in MH_OBJECT type Mach-O files.Copy the code

Section name and function

The name of the functionCopy the code
  • Text. TEXT Specifies the executable machine code
  • Text. cstring Specifies the deleted C string
  • TEXT. Const Initialized constant
  • TEXT. Stubs symbol pile. The code for the address to which the lazyBinding table points
  • Text.stub_helper helper function. The actual symbolic address of the pointer to the corresponding item is not found in the lazyBinding table
  • Text.unwind_info Stores information about handling exceptions
  • Text. eh_frame Auxiliary debugging information
  • Data. DATA The initialized mutable DATA
  • Data.nl_symbol_ptr non-lazy-binding pointer tables, with Pointers in each table pointing to a lazy-binding pointer table for the loading process data.la_symbol_ptr, and Pointers in each table starting with stub_helper
  • Data. const An uninitialized constant
  • Data. mod_init_func initializes the function, called before main
  • Data. mod_term_func terminates the function and is called after main returns
  • Data.bss has no initialized static variable
  • Uninitialized symbol declaration (for example, int I;)

The nm command

Print the symbol table of nList structure.

Common nm command parameters

Nm-pa a. o-a: displays all the contents of the symbol table. -g: displays global symbols. -p: does not sort. -r: reverses the order. -U: displays undefined symbols. -m: displays symbols of the N_SECT type (Mach-O symbols).Copy the code
Buried a few holes, see if you can find them. If you find that you can leave a comment in the comments section, you may become the mentor of the future iOS developer. Don't skimp. If you need iOS information, you can click below to get it, and finally ask for a wave of likes and followers.Copy the code

IOS Information: Download address