github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/cataloger/golang/internal/xcoff/xcoff.go (about)

     1  // The code in this package comes from:
     2  // https://github.com/golang/go/tree/master/src/internal/xcoff
     3  // it was copied over to add support for xcoff binaries.
     4  // Golang keeps this package as internal, forbidding its external use.
     5  
     6  // Copyright 2018 The Go Authors. All rights reserved.
     7  // Use of this source code is governed by a BSD-style
     8  // license that can be found in the LICENSE file.
     9  
    10  //nolint:all
    11  package xcoff
    12  
    13  // File Header.
    14  type FileHeader32 struct {
    15  	Fmagic   uint16 // Target machine
    16  	Fnscns   uint16 // Number of sections
    17  	Ftimedat int32  // Time and date of file creation
    18  	Fsymptr  uint32 // Byte offset to symbol table start
    19  	Fnsyms   int32  // Number of entries in symbol table
    20  	Fopthdr  uint16 // Number of bytes in optional header
    21  	Fflags   uint16 // Flags
    22  }
    23  
    24  type FileHeader64 struct {
    25  	Fmagic   uint16 // Target machine
    26  	Fnscns   uint16 // Number of sections
    27  	Ftimedat int32  // Time and date of file creation
    28  	Fsymptr  uint64 // Byte offset to symbol table start
    29  	Fopthdr  uint16 // Number of bytes in optional header
    30  	Fflags   uint16 // Flags
    31  	Fnsyms   int32  // Number of entries in symbol table
    32  }
    33  
    34  const (
    35  	FILHSZ_32 = 20
    36  	FILHSZ_64 = 24
    37  )
    38  const (
    39  	U802TOCMAGIC = 0737 // AIX 32-bit XCOFF
    40  	U64_TOCMAGIC = 0767 // AIX 64-bit XCOFF
    41  )
    42  
    43  // Flags that describe the type of the object file.
    44  const (
    45  	F_RELFLG    = 0x0001
    46  	F_EXEC      = 0x0002
    47  	F_LNNO      = 0x0004
    48  	F_FDPR_PROF = 0x0010
    49  	F_FDPR_OPTI = 0x0020
    50  	F_DSA       = 0x0040
    51  	F_VARPG     = 0x0100
    52  	F_DYNLOAD   = 0x1000
    53  	F_SHROBJ    = 0x2000
    54  	F_LOADONLY  = 0x4000
    55  )
    56  
    57  // Section Header.
    58  type SectionHeader32 struct {
    59  	Sname    [8]byte // Section name
    60  	Spaddr   uint32  // Physical address
    61  	Svaddr   uint32  // Virtual address
    62  	Ssize    uint32  // Section size
    63  	Sscnptr  uint32  // Offset in file to raw data for section
    64  	Srelptr  uint32  // Offset in file to relocation entries for section
    65  	Slnnoptr uint32  // Offset in file to line number entries for section
    66  	Snreloc  uint16  // Number of relocation entries
    67  	Snlnno   uint16  // Number of line number entries
    68  	Sflags   uint32  // Flags to define the section type
    69  }
    70  
    71  type SectionHeader64 struct {
    72  	Sname    [8]byte // Section name
    73  	Spaddr   uint64  // Physical address
    74  	Svaddr   uint64  // Virtual address
    75  	Ssize    uint64  // Section size
    76  	Sscnptr  uint64  // Offset in file to raw data for section
    77  	Srelptr  uint64  // Offset in file to relocation entries for section
    78  	Slnnoptr uint64  // Offset in file to line number entries for section
    79  	Snreloc  uint32  // Number of relocation entries
    80  	Snlnno   uint32  // Number of line number entries
    81  	Sflags   uint32  // Flags to define the section type
    82  	Spad     uint32  // Needs to be 72 bytes long
    83  }
    84  
    85  // Flags defining the section type.
    86  const (
    87  	STYP_DWARF  = 0x0010
    88  	STYP_TEXT   = 0x0020
    89  	STYP_DATA   = 0x0040
    90  	STYP_BSS    = 0x0080
    91  	STYP_EXCEPT = 0x0100
    92  	STYP_INFO   = 0x0200
    93  	STYP_TDATA  = 0x0400
    94  	STYP_TBSS   = 0x0800
    95  	STYP_LOADER = 0x1000
    96  	STYP_DEBUG  = 0x2000
    97  	STYP_TYPCHK = 0x4000
    98  	STYP_OVRFLO = 0x8000
    99  )
   100  const (
   101  	SSUBTYP_DWINFO  = 0x10000 // DWARF info section
   102  	SSUBTYP_DWLINE  = 0x20000 // DWARF line-number section
   103  	SSUBTYP_DWPBNMS = 0x30000 // DWARF public names section
   104  	SSUBTYP_DWPBTYP = 0x40000 // DWARF public types section
   105  	SSUBTYP_DWARNGE = 0x50000 // DWARF aranges section
   106  	SSUBTYP_DWABREV = 0x60000 // DWARF abbreviation section
   107  	SSUBTYP_DWSTR   = 0x70000 // DWARF strings section
   108  	SSUBTYP_DWRNGES = 0x80000 // DWARF ranges section
   109  	SSUBTYP_DWLOC   = 0x90000 // DWARF location lists section
   110  	SSUBTYP_DWFRAME = 0xA0000 // DWARF frames section
   111  	SSUBTYP_DWMAC   = 0xB0000 // DWARF macros section
   112  )
   113  
   114  // Symbol Table Entry.
   115  type SymEnt32 struct {
   116  	Nname   [8]byte // Symbol name
   117  	Nvalue  uint32  // Symbol value
   118  	Nscnum  int16   // Section number of symbol
   119  	Ntype   uint16  // Basic and derived type specification
   120  	Nsclass int8    // Storage class of symbol
   121  	Nnumaux int8    // Number of auxiliary entries
   122  }
   123  
   124  type SymEnt64 struct {
   125  	Nvalue  uint64 // Symbol value
   126  	Noffset uint32 // Offset of the name in string table or .debug section
   127  	Nscnum  int16  // Section number of symbol
   128  	Ntype   uint16 // Basic and derived type specification
   129  	Nsclass int8   // Storage class of symbol
   130  	Nnumaux int8   // Number of auxiliary entries
   131  }
   132  
   133  const SYMESZ = 18
   134  
   135  const (
   136  	// Nscnum
   137  	N_DEBUG = -2
   138  	N_ABS   = -1
   139  	N_UNDEF = 0
   140  
   141  	//Ntype
   142  	SYM_V_INTERNAL  = 0x1000
   143  	SYM_V_HIDDEN    = 0x2000
   144  	SYM_V_PROTECTED = 0x3000
   145  	SYM_V_EXPORTED  = 0x4000
   146  	SYM_TYPE_FUNC   = 0x0020 // is function
   147  )
   148  
   149  // Storage Class.
   150  const (
   151  	C_NULL    = 0   // Symbol table entry marked for deletion
   152  	C_EXT     = 2   // External symbol
   153  	C_STAT    = 3   // Static symbol
   154  	C_BLOCK   = 100 // Beginning or end of inner block
   155  	C_FCN     = 101 // Beginning or end of function
   156  	C_FILE    = 103 // Source file name and compiler information
   157  	C_HIDEXT  = 107 // Unnamed external symbol
   158  	C_BINCL   = 108 // Beginning of include file
   159  	C_EINCL   = 109 // End of include file
   160  	C_WEAKEXT = 111 // Weak external symbol
   161  	C_DWARF   = 112 // DWARF symbol
   162  	C_GSYM    = 128 // Global variable
   163  	C_LSYM    = 129 // Automatic variable allocated on stack
   164  	C_PSYM    = 130 // Argument to subroutine allocated on stack
   165  	C_RSYM    = 131 // Register variable
   166  	C_RPSYM   = 132 // Argument to function or procedure stored in register
   167  	C_STSYM   = 133 // Statically allocated symbol
   168  	C_BCOMM   = 135 // Beginning of common block
   169  	C_ECOML   = 136 // Local member of common block
   170  	C_ECOMM   = 137 // End of common block
   171  	C_DECL    = 140 // Declaration of object
   172  	C_ENTRY   = 141 // Alternate entry
   173  	C_FUN     = 142 // Function or procedure
   174  	C_BSTAT   = 143 // Beginning of static block
   175  	C_ESTAT   = 144 // End of static block
   176  	C_GTLS    = 145 // Global thread-local variable
   177  	C_STTLS   = 146 // Static thread-local variable
   178  )
   179  
   180  // File Auxiliary Entry
   181  type AuxFile64 struct {
   182  	Xfname   [8]byte // Name or offset inside string table
   183  	Xftype   uint8   // Source file string type
   184  	Xauxtype uint8   // Type of auxiliary entry
   185  }
   186  
   187  // Function Auxiliary Entry
   188  type AuxFcn32 struct {
   189  	Xexptr   uint32 // File offset to exception table entry
   190  	Xfsize   uint32 // Size of function in bytes
   191  	Xlnnoptr uint32 // File pointer to line number
   192  	Xendndx  uint32 // Symbol table index of next entry
   193  	Xpad     uint16 // Unused
   194  }
   195  type AuxFcn64 struct {
   196  	Xlnnoptr uint64 // File pointer to line number
   197  	Xfsize   uint32 // Size of function in bytes
   198  	Xendndx  uint32 // Symbol table index of next entry
   199  	Xpad     uint8  // Unused
   200  	Xauxtype uint8  // Type of auxiliary entry
   201  }
   202  
   203  type AuxSect64 struct {
   204  	Xscnlen  uint64 // section length
   205  	Xnreloc  uint64 // Num RLDs
   206  	pad      uint8
   207  	Xauxtype uint8 // Type of auxiliary entry
   208  }
   209  
   210  // csect Auxiliary Entry.
   211  type AuxCSect32 struct {
   212  	Xscnlen   int32  // Length or symbol table index
   213  	Xparmhash uint32 // Offset of parameter type-check string
   214  	Xsnhash   uint16 // .typchk section number
   215  	Xsmtyp    uint8  // Symbol alignment and type
   216  	Xsmclas   uint8  // Storage-mapping class
   217  	Xstab     uint32 // Reserved
   218  	Xsnstab   uint16 // Reserved
   219  }
   220  
   221  type AuxCSect64 struct {
   222  	Xscnlenlo uint32 // Lower 4 bytes of length or symbol table index
   223  	Xparmhash uint32 // Offset of parameter type-check string
   224  	Xsnhash   uint16 // .typchk section number
   225  	Xsmtyp    uint8  // Symbol alignment and type
   226  	Xsmclas   uint8  // Storage-mapping class
   227  	Xscnlenhi int32  // Upper 4 bytes of length or symbol table index
   228  	Xpad      uint8  // Unused
   229  	Xauxtype  uint8  // Type of auxiliary entry
   230  }
   231  
   232  // Auxiliary type
   233  // const (
   234  // 	_AUX_EXCEPT = 255
   235  // 	_AUX_FCN    = 254
   236  // 	_AUX_SYM    = 253
   237  // 	_AUX_FILE   = 252
   238  // 	_AUX_CSECT  = 251
   239  // 	_AUX_SECT   = 250
   240  // )
   241  
   242  // Symbol type field.
   243  const (
   244  	XTY_ER = 0 // External reference
   245  	XTY_SD = 1 // Section definition
   246  	XTY_LD = 2 // Label definition
   247  	XTY_CM = 3 // Common csect definition
   248  )
   249  
   250  // Defines for File auxiliary definitions: x_ftype field of x_file
   251  const (
   252  	XFT_FN = 0   // Source File Name
   253  	XFT_CT = 1   // Compile Time Stamp
   254  	XFT_CV = 2   // Compiler Version Number
   255  	XFT_CD = 128 // Compiler Defined Information
   256  )
   257  
   258  // Storage-mapping class.
   259  const (
   260  	XMC_PR     = 0  // Program code
   261  	XMC_RO     = 1  // Read-only constant
   262  	XMC_DB     = 2  // Debug dictionary table
   263  	XMC_TC     = 3  // TOC entry
   264  	XMC_UA     = 4  // Unclassified
   265  	XMC_RW     = 5  // Read/Write data
   266  	XMC_GL     = 6  // Global linkage
   267  	XMC_XO     = 7  // Extended operation
   268  	XMC_SV     = 8  // 32-bit supervisor call descriptor
   269  	XMC_BS     = 9  // BSS class
   270  	XMC_DS     = 10 // Function descriptor
   271  	XMC_UC     = 11 // Unnamed FORTRAN common
   272  	XMC_TC0    = 15 // TOC anchor
   273  	XMC_TD     = 16 // Scalar data entry in the TOC
   274  	XMC_SV64   = 17 // 64-bit supervisor call descriptor
   275  	XMC_SV3264 = 18 // Supervisor call descriptor for both 32-bit and 64-bit
   276  	XMC_TL     = 20 // Read/Write thread-local data
   277  	XMC_UL     = 21 // Read/Write thread-local data (.tbss)
   278  	XMC_TE     = 22 // TOC entry
   279  )
   280  
   281  // Loader Header.
   282  type LoaderHeader32 struct {
   283  	Lversion int32  // Loader section version number
   284  	Lnsyms   int32  // Number of symbol table entries
   285  	Lnreloc  int32  // Number of relocation table entries
   286  	Listlen  uint32 // Length of import file ID string table
   287  	Lnimpid  int32  // Number of import file IDs
   288  	Limpoff  uint32 // Offset to start of import file IDs
   289  	Lstlen   uint32 // Length of string table
   290  	Lstoff   uint32 // Offset to start of string table
   291  }
   292  
   293  type LoaderHeader64 struct {
   294  	Lversion int32  // Loader section version number
   295  	Lnsyms   int32  // Number of symbol table entries
   296  	Lnreloc  int32  // Number of relocation table entries
   297  	Listlen  uint32 // Length of import file ID string table
   298  	Lnimpid  int32  // Number of import file IDs
   299  	Lstlen   uint32 // Length of string table
   300  	Limpoff  uint64 // Offset to start of import file IDs
   301  	Lstoff   uint64 // Offset to start of string table
   302  	Lsymoff  uint64 // Offset to start of symbol table
   303  	Lrldoff  uint64 // Offset to start of relocation entries
   304  }
   305  
   306  const (
   307  	LDHDRSZ_32 = 32
   308  	LDHDRSZ_64 = 56
   309  )
   310  
   311  // Loader Symbol.
   312  type LoaderSymbol32 struct {
   313  	Lname   [8]byte // Symbol name or byte offset into string table
   314  	Lvalue  uint32  // Address field
   315  	Lscnum  int16   // Section number containing symbol
   316  	Lsmtype int8    // Symbol type, export, import flags
   317  	Lsmclas int8    // Symbol storage class
   318  	Lifile  int32   // Import file ID; ordinal of import file IDs
   319  	Lparm   uint32  // Parameter type-check field
   320  }
   321  
   322  type LoaderSymbol64 struct {
   323  	Lvalue  uint64 // Address field
   324  	Loffset uint32 // Byte offset into string table of symbol name
   325  	Lscnum  int16  // Section number containing symbol
   326  	Lsmtype int8   // Symbol type, export, import flags
   327  	Lsmclas int8   // Symbol storage class
   328  	Lifile  int32  // Import file ID; ordinal of import file IDs
   329  	Lparm   uint32 // Parameter type-check field
   330  }
   331  
   332  type Reloc32 struct {
   333  	Rvaddr  uint32 // (virtual) address of reference
   334  	Rsymndx uint32 // Index into symbol table
   335  	Rsize   uint8  // Sign and reloc bit len
   336  	Rtype   uint8  // Toc relocation type
   337  }
   338  
   339  type Reloc64 struct {
   340  	Rvaddr  uint64 // (virtual) address of reference
   341  	Rsymndx uint32 // Index into symbol table
   342  	Rsize   uint8  // Sign and reloc bit len
   343  	Rtype   uint8  // Toc relocation type
   344  }
   345  
   346  const (
   347  	R_POS = 0x00 // A(sym) Positive Relocation
   348  	R_NEG = 0x01 // -A(sym) Negative Relocation
   349  	R_REL = 0x02 // A(sym-*) Relative to self
   350  	R_TOC = 0x03 // A(sym-TOC) Relative to TOC
   351  	R_TRL = 0x12 // A(sym-TOC) TOC Relative indirect load.
   352  
   353  	R_TRLA = 0x13 // A(sym-TOC) TOC Rel load address. modifiable inst
   354  	R_GL   = 0x05 // A(external TOC of sym) Global Linkage
   355  	R_TCL  = 0x06 // A(local TOC of sym) Local object TOC address
   356  	R_RL   = 0x0C // A(sym) Pos indirect load. modifiable instruction
   357  	R_RLA  = 0x0D // A(sym) Pos Load Address. modifiable instruction
   358  	R_REF  = 0x0F // AL0(sym) Non relocating ref. No garbage collect
   359  	R_BA   = 0x08 // A(sym) Branch absolute. Cannot modify instruction
   360  	R_RBA  = 0x18 // A(sym) Branch absolute. modifiable instruction
   361  	R_BR   = 0x0A // A(sym-*) Branch rel to self. non modifiable
   362  	R_RBR  = 0x1A // A(sym-*) Branch rel to self. modifiable instr
   363  
   364  	R_TLS    = 0x20 // General-dynamic reference to TLS symbol
   365  	R_TLS_IE = 0x21 // Initial-exec reference to TLS symbol
   366  	R_TLS_LD = 0x22 // Local-dynamic reference to TLS symbol
   367  	R_TLS_LE = 0x23 // Local-exec reference to TLS symbol
   368  	R_TLSM   = 0x24 // Module reference to TLS symbol
   369  	R_TLSML  = 0x25 // Module reference to local (own) module
   370  
   371  	R_TOCU = 0x30 // Relative to TOC - high order bits
   372  	R_TOCL = 0x31 // Relative to TOC - low order bits
   373  )