github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/link/internal/ld/elf.go (about)

     1  // Copyright 2009 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  package ld
     6  
     7  import (
     8  	"github.com/shogo82148/std/cmd/internal/sys"
     9  	"github.com/shogo82148/std/cmd/link/internal/loader"
    10  	"github.com/shogo82148/std/debug/elf"
    11  )
    12  
    13  /*
    14   * Relocation types.
    15   */
    16  const (
    17  	ARM_MAGIC_TRAMP_NUMBER = 0x5c000003
    18  )
    19  
    20  /*
    21   * ELF header.
    22   */
    23  type ElfEhdr elf.Header64
    24  
    25  /*
    26   * Section header.
    27   */
    28  type ElfShdr struct {
    29  	elf.Section64
    30  	shnum elf.SectionIndex
    31  }
    32  
    33  /*
    34   * Program header.
    35   */
    36  type ElfPhdr elf.ProgHeader
    37  
    38  /*
    39   * Go linker interface
    40   */
    41  const (
    42  	ELF64HDRSIZE  = 64
    43  	ELF64PHDRSIZE = 56
    44  	ELF64SHDRSIZE = 64
    45  	ELF64RELSIZE  = 16
    46  	ELF64RELASIZE = 24
    47  	ELF64SYMSIZE  = 24
    48  	ELF32HDRSIZE  = 52
    49  	ELF32PHDRSIZE = 32
    50  	ELF32SHDRSIZE = 40
    51  	ELF32SYMSIZE  = 16
    52  	ELF32RELSIZE  = 8
    53  )
    54  
    55  /*
    56   * Total amount of space to reserve at the start of the file
    57   * for Header, PHeaders, SHeaders, and interp.
    58   * May waste some.
    59   * On FreeBSD, cannot be larger than a page.
    60   */
    61  const (
    62  	ELFRESERVE = 4096
    63  )
    64  
    65  /*
    66   * We use the 64-bit data structures on both 32- and 64-bit machines
    67   * in order to write the code just once.  The 64-bit data structure is
    68   * written in the 32-bit format on the 32-bit machines.
    69   */
    70  const (
    71  	NSECT = 400
    72  )
    73  
    74  var (
    75  	Nelfsym = 1
    76  )
    77  
    78  // ELFArch includes target-specific hooks for ELF targets.
    79  // This is initialized by the target-specific Init function
    80  // called by the linker's main function in cmd/link/main.go.
    81  type ELFArch struct {
    82  	Androiddynld   string
    83  	Linuxdynld     string
    84  	LinuxdynldMusl string
    85  	Freebsddynld   string
    86  	Netbsddynld    string
    87  	Openbsddynld   string
    88  	Dragonflydynld string
    89  	Solarisdynld   string
    90  
    91  	Reloc1    func(*Link, *OutBuf, *loader.Loader, loader.Sym, loader.ExtReloc, int, int64) bool
    92  	RelocSize uint32
    93  	SetupPLT  func(ctxt *Link, ldr *loader.Loader, plt, gotplt *loader.SymbolBuilder, dynamic loader.Sym)
    94  
    95  	// DynamicReadOnly can be set to true to make the .dynamic
    96  	// section read-only. By default it is writable.
    97  	// This is used by MIPS targets.
    98  	DynamicReadOnly bool
    99  }
   100  
   101  type Elfstring struct {
   102  	s   string
   103  	off int
   104  }
   105  
   106  /*
   107  Initialize the global variable that describes the ELF header. It will be updated as
   108  we write section and prog headers.
   109  */
   110  func Elfinit(ctxt *Link)
   111  
   112  func Elfwritedynent(arch *sys.Arch, s *loader.SymbolBuilder, tag elf.DynTag, val uint64)
   113  
   114  func Elfwritedynentsymplus(ctxt *Link, s *loader.SymbolBuilder, tag elf.DynTag, t loader.Sym, add int64)
   115  
   116  // member of .gnu.attributes of MIPS for fpAbi
   117  const (
   118  	// No floating point is present in the module (default)
   119  	MIPS_FPABI_NONE = 0
   120  	// FP code in the module uses the FP32 ABI for a 32-bit ABI
   121  	MIPS_FPABI_ANY = 1
   122  	// FP code in the module only uses single precision ABI
   123  	MIPS_FPABI_SINGLE = 2
   124  	// FP code in the module uses soft-float ABI
   125  	MIPS_FPABI_SOFT = 3
   126  	// FP code in the module assumes an FPU with FR=1 and has 12
   127  	// callee-saved doubles. Historic, no longer supported.
   128  	MIPS_FPABI_HIST = 4
   129  	// FP code in the module uses the FPXX  ABI
   130  	MIPS_FPABI_FPXX = 5
   131  	// FP code in the module uses the FP64  ABI
   132  	MIPS_FPABI_FP64 = 6
   133  	// FP code in the module uses the FP64A ABI
   134  	MIPS_FPABI_FP64A = 7
   135  )
   136  
   137  // NetBSD Signature (as per sys/exec_elf.h)
   138  const (
   139  	ELF_NOTE_NETBSD_NAMESZ  = 7
   140  	ELF_NOTE_NETBSD_DESCSZ  = 4
   141  	ELF_NOTE_NETBSD_TAG     = 1
   142  	ELF_NOTE_NETBSD_VERSION = 700000000
   143  )
   144  
   145  var ELF_NOTE_NETBSD_NAME = []byte("NetBSD\x00")
   146  
   147  // OpenBSD Signature
   148  const (
   149  	ELF_NOTE_OPENBSD_NAMESZ  = 8
   150  	ELF_NOTE_OPENBSD_DESCSZ  = 4
   151  	ELF_NOTE_OPENBSD_TAG     = 1
   152  	ELF_NOTE_OPENBSD_VERSION = 0
   153  )
   154  
   155  var ELF_NOTE_OPENBSD_NAME = []byte("OpenBSD\x00")
   156  
   157  // FreeBSD Signature (as per sys/elf_common.h)
   158  const (
   159  	ELF_NOTE_FREEBSD_NAMESZ            = 8
   160  	ELF_NOTE_FREEBSD_DESCSZ            = 4
   161  	ELF_NOTE_FREEBSD_ABI_TAG           = 1
   162  	ELF_NOTE_FREEBSD_NOINIT_TAG        = 2
   163  	ELF_NOTE_FREEBSD_FEATURE_CTL_TAG   = 4
   164  	ELF_NOTE_FREEBSD_VERSION           = 1203000
   165  	ELF_NOTE_FREEBSD_FCTL_ASLR_DISABLE = 0x1
   166  )
   167  
   168  const ELF_NOTE_FREEBSD_NAME = "FreeBSD\x00"
   169  
   170  // Build info note
   171  const (
   172  	ELF_NOTE_BUILDINFO_NAMESZ = 4
   173  	ELF_NOTE_BUILDINFO_TAG    = 3
   174  )
   175  
   176  var ELF_NOTE_BUILDINFO_NAME = []byte("GNU\x00")
   177  
   178  // Go specific notes
   179  const (
   180  	ELF_NOTE_GOPKGLIST_TAG = 1
   181  	ELF_NOTE_GOABIHASH_TAG = 2
   182  	ELF_NOTE_GODEPS_TAG    = 3
   183  	ELF_NOTE_GOBUILDID_TAG = 4
   184  )
   185  
   186  var ELF_NOTE_GO_NAME = []byte("Go\x00\x00")
   187  
   188  type Elfaux struct {
   189  	next *Elfaux
   190  	num  int
   191  	vers string
   192  }
   193  
   194  type Elflib struct {
   195  	next *Elflib
   196  	aux  *Elfaux
   197  	file string
   198  }
   199  
   200  func Asmbelfsetup()