Source file src/cmd/link/internal/sym/symkind.go

     1  // Derived from Inferno utils/6l/l.h and related files.
     2  // https://bitbucket.org/inferno-os/inferno-os/src/master/utils/6l/l.h
     3  //
     4  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     6  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     7  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     8  //	Portions Copyright © 2004,2006 Bruce Ellis
     9  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    10  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    11  //	Portions Copyright © 2009 The Go Authors. All rights reserved.
    12  //
    13  // Permission is hereby granted, free of charge, to any person obtaining a copy
    14  // of this software and associated documentation files (the "Software"), to deal
    15  // in the Software without restriction, including without limitation the rights
    16  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17  // copies of the Software, and to permit persons to whom the Software is
    18  // furnished to do so, subject to the following conditions:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    23  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    26  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29  // THE SOFTWARE.
    30  
    31  package sym
    32  
    33  import "cmd/internal/objabi"
    34  
    35  // A SymKind describes the kind of memory represented by a symbol.
    36  type SymKind uint8
    37  
    38  // Defined SymKind values.
    39  //
    40  // TODO(rsc): Give idiomatic Go names.
    41  //
    42  //go:generate stringer -type=SymKind
    43  const (
    44  	// An otherwise invalid zero value for the type.
    45  	Sxxx SymKind = iota
    46  	// The text segment, containing executable instructions.
    47  	STEXT          // General executable code.
    48  	STEXTFIPSSTART // Start of FIPS text section.
    49  	STEXTFIPS      // Instructions hashed for FIPS checks.
    50  	STEXTFIPSEND   // End of FIPS text section.
    51  	STEXTEND       // End of text section.
    52  	SELFRXSECT     // Executable PLT; PPC64 .glink.
    53  	SMACHOPLT      // Mach-O PLT.
    54  
    55  	// Read-only, non-executable, segment.
    56  	STYPE            // Type descriptors.
    57  	SSTRING          // Used only for XCOFF runtime.rodata symbol?
    58  	SGOSTRING        // Go string constants.
    59  	SGOFUNC          // Function descriptors and funcdata symbols.
    60  	SGCBITS          // GC bit masks and programs.
    61  	SRODATA          // General read-only data.
    62  	SRODATAFIPSSTART // Start of FIPS read-only data.
    63  	SRODATAFIPS      // FIPS read-only data.
    64  	SRODATAFIPSEND   // End of FIPS read-only data.
    65  	SRODATAEND       // End of read-only data.
    66  	SFUNCTAB         // Appears to be unused, except for runtime.etypes.
    67  	SPCLNTAB         // Pclntab data.
    68  	SELFROSECT       // ELF read-only data: relocs, dynamic linking info.
    69  
    70  	// Read-only, non-executable, dynamically relocatable segment.
    71  	//
    72  	// Types STYPE-SFUNCTAB above are written to the .rodata section by default.
    73  	// When linking a shared object, some conceptually "read only" types need to
    74  	// be written to by relocations and putting them in a section called
    75  	// ".rodata" interacts poorly with the system linkers. The GNU linkers
    76  	// support this situation by arranging for sections of the name
    77  	// ".data.rel.ro.XXX" to be mprotected read only by the dynamic linker after
    78  	// relocations have applied, so when the Go linker is creating a shared
    79  	// object it checks all objects of the above types and bumps any object that
    80  	// has a relocation to it to the corresponding type below, which are then
    81  	// written to sections with appropriate magic names.
    82  	STYPERELRO
    83  	SSTRINGRELRO
    84  	SGOSTRINGRELRO
    85  	SGOFUNCRELRO
    86  	SGCBITSRELRO
    87  	SRODATARELRO
    88  	SFUNCTABRELRO
    89  
    90  	SELFRELROSECT   // ELF-specific read-only relocatable: PLT, etc.
    91  	SMACHORELROSECT // Mach-O specific read-only relocatable.
    92  
    93  	STYPELINK // Type links.
    94  	SITABLINK // Itab links.
    95  
    96  	// Allocated writable segment.
    97  	SFirstWritable
    98  	SBUILDINFO          // debug/buildinfo data (why is this writable?).
    99  	SFIPSINFO           // go:fipsinfo aka crypto/internal/fips140/check.Linkinfo (why is this writable)?
   100  	SELFSECT            // .got.plt, .plt, .dynamic where appropriate.
   101  	SMACHO              // Used only for .llvmasm?
   102  	SWINDOWS            // Windows dynamic symbols.
   103  	SMODULEDATA         // Linker generated moduledata struct.
   104  	SELFGOT             // Writable ELF GOT section.
   105  	SMACHOGOT           // Mach-O GOT.
   106  	SNOPTRDATA          // Data with no heap pointers.
   107  	SNOPTRDATAFIPSSTART // Start of FIPS non-pointer writable data.
   108  	SNOPTRDATAFIPS      // FIPS non-pointer writable data.
   109  	SNOPTRDATAFIPSEND   // End of FIPS non-pointer writable data.
   110  	SNOPTRDATAEND       // End of data with no heap pointers.
   111  	SINITARR            // ELF .init_array section.
   112  	SDATA               // Data that may have heap pointers.
   113  	SDATAFIPSSTART      // Start of FIPS writable data.
   114  	SDATAFIPS           // FIPS writable data.
   115  	SDATAFIPSEND        // End of FIPS writable data.
   116  	SDATAEND            // End of data that may have heap pointers.
   117  	SXCOFFTOC           // AIX TOC entries.
   118  
   119  	// Allocated zero-initialized segment.
   120  	SBSS                    // Zeroed data that may have heap pointers.
   121  	SNOPTRBSS               // Zeroed data with no heap pointers.
   122  	SLIBFUZZER_8BIT_COUNTER // Fuzzer counters.
   123  	SCOVERAGE_COUNTER       // Coverage counters.
   124  	SCOVERAGE_AUXVAR        // Compiler generated coverage symbols.
   125  	STLSBSS                 // Thread-local zeroed data.
   126  
   127  	// Unallocated segment.
   128  	SFirstUnallocated
   129  	SXREF             // Reference from non-Go object file.
   130  	SMACHOSYMSTR      // Mach-O string table.
   131  	SMACHOSYMTAB      // Mach-O symbol table.
   132  	SMACHOINDIRECTPLT // Mach-O indirect PLT.
   133  	SMACHOINDIRECTGOT // Mach-O indirect GOT.
   134  	SDYNIMPORT        // Reference to symbol defined in shared library.
   135  	SHOSTOBJ          // Symbol defined in non-Go object file.
   136  	SUNDEFEXT         // Undefined symbol for resolution by external linker.
   137  
   138  	// Unallocated DWARF debugging segment.
   139  	SDWARFSECT
   140  	// DWARF symbol types created by compiler or linker.
   141  	SDWARFCUINFO
   142  	SDWARFCONST
   143  	SDWARFFCN
   144  	SDWARFABSFCN
   145  	SDWARFTYPE
   146  	SDWARFVAR
   147  	SDWARFRANGE
   148  	SDWARFLOC
   149  	SDWARFLINES
   150  	SDWARFADDR
   151  
   152  	// SEH symbol types. These are probably allocated at run time.
   153  	SSEHUNWINDINFO // Compiler generated Windows SEH info.
   154  	SSEHSECT       // Windows SEH data.
   155  )
   156  
   157  // AbiSymKindToSymKind maps values read from object files (which are
   158  // of type cmd/internal/objabi.SymKind) to values of type SymKind.
   159  var AbiSymKindToSymKind = [...]SymKind{
   160  	objabi.Sxxx:                    Sxxx,
   161  	objabi.STEXT:                   STEXT,
   162  	objabi.STEXTFIPS:               STEXTFIPS,
   163  	objabi.SRODATA:                 SRODATA,
   164  	objabi.SRODATAFIPS:             SRODATAFIPS,
   165  	objabi.SNOPTRDATA:              SNOPTRDATA,
   166  	objabi.SNOPTRDATAFIPS:          SNOPTRDATAFIPS,
   167  	objabi.SDATA:                   SDATA,
   168  	objabi.SDATAFIPS:               SDATAFIPS,
   169  	objabi.SBSS:                    SBSS,
   170  	objabi.SNOPTRBSS:               SNOPTRBSS,
   171  	objabi.STLSBSS:                 STLSBSS,
   172  	objabi.SDWARFCUINFO:            SDWARFCUINFO,
   173  	objabi.SDWARFCONST:             SDWARFCONST,
   174  	objabi.SDWARFFCN:               SDWARFFCN,
   175  	objabi.SDWARFABSFCN:            SDWARFABSFCN,
   176  	objabi.SDWARFTYPE:              SDWARFTYPE,
   177  	objabi.SDWARFVAR:               SDWARFVAR,
   178  	objabi.SDWARFRANGE:             SDWARFRANGE,
   179  	objabi.SDWARFLOC:               SDWARFLOC,
   180  	objabi.SDWARFLINES:             SDWARFLINES,
   181  	objabi.SDWARFADDR:              SDWARFADDR,
   182  	objabi.SLIBFUZZER_8BIT_COUNTER: SLIBFUZZER_8BIT_COUNTER,
   183  	objabi.SCOVERAGE_COUNTER:       SCOVERAGE_COUNTER,
   184  	objabi.SCOVERAGE_AUXVAR:        SCOVERAGE_AUXVAR,
   185  	objabi.SSEHUNWINDINFO:          SSEHUNWINDINFO,
   186  }
   187  
   188  // ReadOnly are the symbol kinds that form read-only sections. In some
   189  // cases, if they will require relocations, they are transformed into
   190  // rel-ro sections using relROMap.
   191  var ReadOnly = []SymKind{
   192  	STYPE,
   193  	SSTRING,
   194  	SGOSTRING,
   195  	SGOFUNC,
   196  	SGCBITS,
   197  	SRODATA,
   198  	SRODATAFIPSSTART,
   199  	SRODATAFIPS,
   200  	SRODATAFIPSEND,
   201  	SRODATAEND,
   202  	SFUNCTAB,
   203  }
   204  
   205  // RelROMap describes the transformation of read-only symbols to rel-ro
   206  // symbols.
   207  var RelROMap = map[SymKind]SymKind{
   208  	STYPE:     STYPERELRO,
   209  	SSTRING:   SSTRINGRELRO,
   210  	SGOSTRING: SGOSTRINGRELRO,
   211  	SGOFUNC:   SGOFUNCRELRO,
   212  	SGCBITS:   SGCBITSRELRO,
   213  	SRODATA:   SRODATARELRO,
   214  	SFUNCTAB:  SFUNCTABRELRO,
   215  }
   216  
   217  // IsText returns true if t is a text type.
   218  func (t SymKind) IsText() bool {
   219  	return STEXT <= t && t <= STEXTEND
   220  }
   221  
   222  // IsData returns true if t is any kind of data type.
   223  func (t SymKind) IsData() bool {
   224  	return SNOPTRDATA <= t && t <= SNOPTRBSS
   225  }
   226  
   227  // IsDATA returns true if t is one of the SDATA types.
   228  func (t SymKind) IsDATA() bool {
   229  	return SDATA <= t && t <= SDATAEND
   230  }
   231  
   232  // IsRODATA returns true if t is one of the SRODATA types.
   233  func (t SymKind) IsRODATA() bool {
   234  	return SRODATA <= t && t <= SRODATAEND
   235  }
   236  
   237  // IsNOPTRDATA returns true if t is one of the SNOPTRDATA types.
   238  func (t SymKind) IsNOPTRDATA() bool {
   239  	return SNOPTRDATA <= t && t <= SNOPTRDATAEND
   240  }
   241  
   242  func (t SymKind) IsDWARF() bool {
   243  	return SDWARFSECT <= t && t <= SDWARFADDR
   244  }
   245  

View as plain text