github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/internal/goobj/objfile.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This package defines the Go object file format, and provide "low-level" functions
     6  // for reading and writing object files.
     7  
     8  // The object file is understood by the compiler, assembler, linker, and tools. They
     9  // have "high level" code that operates on object files, handling application-specific
    10  // logics, and use this package for the actual reading and writing. Specifically, the
    11  // code below:
    12  //
    13  // - cmd/internal/obj/objfile.go (used by cmd/asm and cmd/compile)
    14  // - cmd/internal/objfile/goobj.go (used cmd/nm, cmd/objdump)
    15  // - cmd/link/internal/loader package (used by cmd/link)
    16  //
    17  // If the object file format changes, they may (or may not) need to change.
    18  
    19  package goobj
    20  
    21  import (
    22  	"github.com/shogo82148/std/cmd/internal/bio"
    23  )
    24  
    25  type FingerprintType [8]byte
    26  
    27  func (fp FingerprintType) IsZero() bool
    28  
    29  // Package Index.
    30  const (
    31  	PkgIdxNone = (1<<31 - 1) - iota
    32  	PkgIdxHashed64
    33  	PkgIdxHashed
    34  	PkgIdxBuiltin
    35  	PkgIdxSelf
    36  	PkgIdxSpecial = PkgIdxSelf
    37  	PkgIdxInvalid = 0
    38  )
    39  
    40  // Blocks
    41  const (
    42  	BlkAutolib = iota
    43  	BlkPkgIdx
    44  	BlkFile
    45  	BlkSymdef
    46  	BlkHashed64def
    47  	BlkHasheddef
    48  	BlkNonpkgdef
    49  	BlkNonpkgref
    50  	BlkRefFlags
    51  	BlkHash64
    52  	BlkHash
    53  	BlkRelocIdx
    54  	BlkAuxIdx
    55  	BlkDataIdx
    56  	BlkReloc
    57  	BlkAux
    58  	BlkData
    59  	BlkRefName
    60  	BlkEnd
    61  	NBlk
    62  )
    63  
    64  // File header.
    65  // TODO: probably no need to export this.
    66  type Header struct {
    67  	Magic       string
    68  	Fingerprint FingerprintType
    69  	Flags       uint32
    70  	Offsets     [NBlk]uint32
    71  }
    72  
    73  const Magic = "\x00go120ld"
    74  
    75  func (h *Header) Write(w *Writer)
    76  
    77  func (h *Header) Read(r *Reader) error
    78  
    79  func (h *Header) Size() int
    80  
    81  // Autolib
    82  type ImportedPkg struct {
    83  	Pkg         string
    84  	Fingerprint FingerprintType
    85  }
    86  
    87  func (p *ImportedPkg) Write(w *Writer)
    88  
    89  // Symbol definition.
    90  //
    91  // Serialized format:
    92  //
    93  //	Sym struct {
    94  //	   Name  string
    95  //	   ABI   uint16
    96  //	   Type  uint8
    97  //	   Flag  uint8
    98  //	   Flag2 uint8
    99  //	   Siz   uint32
   100  //	   Align uint32
   101  //	}
   102  type Sym [SymSize]byte
   103  
   104  const SymSize = stringRefSize + 2 + 1 + 1 + 1 + 4 + 4
   105  
   106  const SymABIstatic = ^uint16(0)
   107  
   108  const (
   109  	ObjFlagShared = 1 << iota
   110  	_
   111  	ObjFlagFromAssembly
   112  	ObjFlagUnlinkable
   113  )
   114  
   115  // Sym.Flag
   116  const (
   117  	SymFlagDupok = 1 << iota
   118  	SymFlagLocal
   119  	SymFlagTypelink
   120  	SymFlagLeaf
   121  	SymFlagNoSplit
   122  	SymFlagReflectMethod
   123  	SymFlagGoType
   124  )
   125  
   126  // Sym.Flag2
   127  const (
   128  	SymFlagUsedInIface = 1 << iota
   129  	SymFlagItab
   130  	SymFlagDict
   131  	SymFlagPkgInit
   132  )
   133  
   134  // Returns the length of the name of the symbol.
   135  func (s *Sym) NameLen(r *Reader) int
   136  
   137  func (s *Sym) Name(r *Reader) string
   138  
   139  func (s *Sym) ABI() uint16
   140  func (s *Sym) Type() uint8
   141  func (s *Sym) Flag() uint8
   142  func (s *Sym) Flag2() uint8
   143  func (s *Sym) Siz() uint32
   144  func (s *Sym) Align() uint32
   145  
   146  func (s *Sym) Dupok() bool
   147  func (s *Sym) Local() bool
   148  func (s *Sym) Typelink() bool
   149  func (s *Sym) Leaf() bool
   150  func (s *Sym) NoSplit() bool
   151  func (s *Sym) ReflectMethod() bool
   152  func (s *Sym) IsGoType() bool
   153  func (s *Sym) UsedInIface() bool
   154  func (s *Sym) IsItab() bool
   155  func (s *Sym) IsDict() bool
   156  func (s *Sym) IsPkgInit() bool
   157  
   158  func (s *Sym) SetName(x string, w *Writer)
   159  
   160  func (s *Sym) SetABI(x uint16)
   161  func (s *Sym) SetType(x uint8)
   162  func (s *Sym) SetFlag(x uint8)
   163  func (s *Sym) SetFlag2(x uint8)
   164  func (s *Sym) SetSiz(x uint32)
   165  func (s *Sym) SetAlign(x uint32)
   166  
   167  func (s *Sym) Write(w *Writer)
   168  
   169  // Symbol reference.
   170  type SymRef struct {
   171  	PkgIdx uint32
   172  	SymIdx uint32
   173  }
   174  
   175  func (s SymRef) IsZero() bool
   176  
   177  // Hash64
   178  type Hash64Type [Hash64Size]byte
   179  
   180  const Hash64Size = 8
   181  
   182  // Hash
   183  type HashType [HashSize]byte
   184  
   185  const HashSize = 16
   186  
   187  // Relocation.
   188  //
   189  // Serialized format:
   190  //
   191  //	Reloc struct {
   192  //	   Off  int32
   193  //	   Siz  uint8
   194  //	   Type uint16
   195  //	   Add  int64
   196  //	   Sym  SymRef
   197  //	}
   198  type Reloc [RelocSize]byte
   199  
   200  const RelocSize = 4 + 1 + 2 + 8 + 8
   201  
   202  func (r *Reloc) Off() int32
   203  func (r *Reloc) Siz() uint8
   204  func (r *Reloc) Type() uint16
   205  func (r *Reloc) Add() int64
   206  func (r *Reloc) Sym() SymRef
   207  
   208  func (r *Reloc) SetOff(x int32)
   209  func (r *Reloc) SetSiz(x uint8)
   210  func (r *Reloc) SetType(x uint16)
   211  func (r *Reloc) SetAdd(x int64)
   212  func (r *Reloc) SetSym(x SymRef)
   213  
   214  func (r *Reloc) Set(off int32, size uint8, typ uint16, add int64, sym SymRef)
   215  
   216  func (r *Reloc) Write(w *Writer)
   217  
   218  // Aux symbol info.
   219  //
   220  // Serialized format:
   221  //
   222  //	Aux struct {
   223  //	   Type uint8
   224  //	   Sym  SymRef
   225  //	}
   226  type Aux [AuxSize]byte
   227  
   228  const AuxSize = 1 + 8
   229  
   230  // Aux Type
   231  const (
   232  	AuxGotype = iota
   233  	AuxFuncInfo
   234  	AuxFuncdata
   235  	AuxDwarfInfo
   236  	AuxDwarfLoc
   237  	AuxDwarfRanges
   238  	AuxDwarfLines
   239  	AuxPcsp
   240  	AuxPcfile
   241  	AuxPcline
   242  	AuxPcinline
   243  	AuxPcdata
   244  	AuxWasmImport
   245  	AuxSehUnwindInfo
   246  )
   247  
   248  func (a *Aux) Type() uint8
   249  func (a *Aux) Sym() SymRef
   250  
   251  func (a *Aux) SetType(x uint8)
   252  func (a *Aux) SetSym(x SymRef)
   253  
   254  func (a *Aux) Write(w *Writer)
   255  
   256  // Referenced symbol flags.
   257  //
   258  // Serialized format:
   259  //
   260  //	RefFlags struct {
   261  //	   Sym   symRef
   262  //	   Flag  uint8
   263  //	   Flag2 uint8
   264  //	}
   265  type RefFlags [RefFlagsSize]byte
   266  
   267  const RefFlagsSize = 8 + 1 + 1
   268  
   269  func (r *RefFlags) Sym() SymRef
   270  
   271  func (r *RefFlags) Flag() uint8
   272  func (r *RefFlags) Flag2() uint8
   273  
   274  func (r *RefFlags) SetSym(x SymRef)
   275  
   276  func (r *RefFlags) SetFlag(x uint8)
   277  func (r *RefFlags) SetFlag2(x uint8)
   278  
   279  func (r *RefFlags) Write(w *Writer)
   280  
   281  // Referenced symbol name.
   282  //
   283  // Serialized format:
   284  //
   285  //	RefName struct {
   286  //	   Sym  symRef
   287  //	   Name string
   288  //	}
   289  type RefName [RefNameSize]byte
   290  
   291  const RefNameSize = 8 + stringRefSize
   292  
   293  func (n *RefName) Sym() SymRef
   294  
   295  func (n *RefName) Name(r *Reader) string
   296  
   297  func (n *RefName) SetSym(x SymRef)
   298  
   299  func (n *RefName) SetName(x string, w *Writer)
   300  
   301  func (n *RefName) Write(w *Writer)
   302  
   303  type Writer struct {
   304  	wr        *bio.Writer
   305  	stringMap map[string]uint32
   306  	off       uint32
   307  
   308  	b [8]byte
   309  }
   310  
   311  func NewWriter(wr *bio.Writer) *Writer
   312  
   313  func (w *Writer) AddString(s string)
   314  
   315  func (w *Writer) StringRef(s string)
   316  
   317  func (w *Writer) RawString(s string)
   318  
   319  func (w *Writer) Bytes(s []byte)
   320  
   321  func (w *Writer) Uint64(x uint64)
   322  
   323  func (w *Writer) Uint32(x uint32)
   324  
   325  func (w *Writer) Uint16(x uint16)
   326  
   327  func (w *Writer) Uint8(x uint8)
   328  
   329  func (w *Writer) Offset() uint32
   330  
   331  type Reader struct {
   332  	b        []byte
   333  	readonly bool
   334  
   335  	start uint32
   336  	h     Header
   337  }
   338  
   339  func NewReaderFromBytes(b []byte, readonly bool) *Reader
   340  
   341  func (r *Reader) BytesAt(off uint32, len int) []byte
   342  
   343  func (r *Reader) StringAt(off uint32, len uint32) string
   344  
   345  func (r *Reader) StringRef(off uint32) string
   346  
   347  func (r *Reader) Fingerprint() FingerprintType
   348  
   349  func (r *Reader) Autolib() []ImportedPkg
   350  
   351  func (r *Reader) Pkglist() []string
   352  
   353  func (r *Reader) NPkg() int
   354  
   355  func (r *Reader) Pkg(i int) string
   356  
   357  func (r *Reader) NFile() int
   358  
   359  func (r *Reader) File(i int) string
   360  
   361  func (r *Reader) NSym() int
   362  
   363  func (r *Reader) NHashed64def() int
   364  
   365  func (r *Reader) NHasheddef() int
   366  
   367  func (r *Reader) NNonpkgdef() int
   368  
   369  func (r *Reader) NNonpkgref() int
   370  
   371  // SymOff returns the offset of the i-th symbol.
   372  func (r *Reader) SymOff(i uint32) uint32
   373  
   374  // Sym returns a pointer to the i-th symbol.
   375  func (r *Reader) Sym(i uint32) *Sym
   376  
   377  // NRefFlags returns the number of referenced symbol flags.
   378  func (r *Reader) NRefFlags() int
   379  
   380  // RefFlags returns a pointer to the i-th referenced symbol flags.
   381  // Note: here i is not a local symbol index, just a counter.
   382  func (r *Reader) RefFlags(i int) *RefFlags
   383  
   384  // Hash64 returns the i-th short hashed symbol's hash.
   385  // Note: here i is the index of short hashed symbols, not all symbols
   386  // (unlike other accessors).
   387  func (r *Reader) Hash64(i uint32) uint64
   388  
   389  // Hash returns a pointer to the i-th hashed symbol's hash.
   390  // Note: here i is the index of hashed symbols, not all symbols
   391  // (unlike other accessors).
   392  func (r *Reader) Hash(i uint32) *HashType
   393  
   394  // NReloc returns the number of relocations of the i-th symbol.
   395  func (r *Reader) NReloc(i uint32) int
   396  
   397  // RelocOff returns the offset of the j-th relocation of the i-th symbol.
   398  func (r *Reader) RelocOff(i uint32, j int) uint32
   399  
   400  // Reloc returns a pointer to the j-th relocation of the i-th symbol.
   401  func (r *Reader) Reloc(i uint32, j int) *Reloc
   402  
   403  // Relocs returns a pointer to the relocations of the i-th symbol.
   404  func (r *Reader) Relocs(i uint32) []Reloc
   405  
   406  // NAux returns the number of aux symbols of the i-th symbol.
   407  func (r *Reader) NAux(i uint32) int
   408  
   409  // AuxOff returns the offset of the j-th aux symbol of the i-th symbol.
   410  func (r *Reader) AuxOff(i uint32, j int) uint32
   411  
   412  // Aux returns a pointer to the j-th aux symbol of the i-th symbol.
   413  func (r *Reader) Aux(i uint32, j int) *Aux
   414  
   415  // Auxs returns the aux symbols of the i-th symbol.
   416  func (r *Reader) Auxs(i uint32) []Aux
   417  
   418  // DataOff returns the offset of the i-th symbol's data.
   419  func (r *Reader) DataOff(i uint32) uint32
   420  
   421  // DataSize returns the size of the i-th symbol's data.
   422  func (r *Reader) DataSize(i uint32) int
   423  
   424  // Data returns the i-th symbol's data.
   425  func (r *Reader) Data(i uint32) []byte
   426  
   427  // DataString returns the i-th symbol's data as a string.
   428  func (r *Reader) DataString(i uint32) string
   429  
   430  // NRefName returns the number of referenced symbol names.
   431  func (r *Reader) NRefName() int
   432  
   433  // RefName returns a pointer to the i-th referenced symbol name.
   434  // Note: here i is not a local symbol index, just a counter.
   435  func (r *Reader) RefName(i int) *RefName
   436  
   437  // ReadOnly returns whether r.BytesAt returns read-only bytes.
   438  func (r *Reader) ReadOnly() bool
   439  
   440  // Flags returns the flag bits read from the object file header.
   441  func (r *Reader) Flags() uint32
   442  
   443  func (r *Reader) Shared() bool
   444  func (r *Reader) FromAssembly() bool
   445  func (r *Reader) Unlinkable() bool