github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/link/internal/loadelf/ldelf.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  // Package loadelf implements an ELF file reader.
     6  package loadelf
     7  
     8  import (
     9  	"github.com/shogo82148/std/cmd/internal/bio"
    10  	"github.com/shogo82148/std/cmd/internal/sys"
    11  	"github.com/shogo82148/std/cmd/link/internal/loader"
    12  	"github.com/shogo82148/std/debug/elf"
    13  	"github.com/shogo82148/std/encoding/binary"
    14  )
    15  
    16  const (
    17  	SHT_ARM_ATTRIBUTES = 0x70000003
    18  )
    19  
    20  type ElfSect struct {
    21  	name        string
    22  	nameoff     uint32
    23  	type_       elf.SectionType
    24  	flags       elf.SectionFlag
    25  	addr        uint64
    26  	off         uint64
    27  	size        uint64
    28  	link        uint32
    29  	info        uint32
    30  	align       uint64
    31  	entsize     uint64
    32  	base        []byte
    33  	readOnlyMem bool
    34  	sym         loader.Sym
    35  }
    36  
    37  type ElfObj struct {
    38  	f         *bio.Reader
    39  	base      int64
    40  	length    int64
    41  	is64      int
    42  	name      string
    43  	e         binary.ByteOrder
    44  	sect      []ElfSect
    45  	nsect     uint
    46  	nsymtab   int
    47  	symtab    *ElfSect
    48  	symstr    *ElfSect
    49  	type_     uint32
    50  	machine   uint32
    51  	version   uint32
    52  	entry     uint64
    53  	phoff     uint64
    54  	shoff     uint64
    55  	flags     uint32
    56  	ehsize    uint32
    57  	phentsize uint32
    58  	phnum     uint32
    59  	shentsize uint32
    60  	shnum     uint32
    61  	shstrndx  uint32
    62  }
    63  
    64  type ElfSym struct {
    65  	name  string
    66  	value uint64
    67  	size  uint64
    68  	bind  elf.SymBind
    69  	type_ elf.SymType
    70  	other uint8
    71  	shndx elf.SectionIndex
    72  	sym   loader.Sym
    73  }
    74  
    75  const (
    76  	TagFile               = 1
    77  	TagCPUName            = 4
    78  	TagCPURawName         = 5
    79  	TagCompatibility      = 32
    80  	TagNoDefaults         = 64
    81  	TagAlsoCompatibleWith = 65
    82  	TagABIVFPArgs         = 28
    83  )
    84  
    85  // Load loads the ELF file pn from f.
    86  // Symbols are installed into the loader, and a slice of the text symbols is returned.
    87  //
    88  // On ARM systems, Load will attempt to determine what ELF header flags to
    89  // emit by scanning the attributes in the ELF file being loaded. The
    90  // parameter initEhdrFlags contains the current header flags for the output
    91  // object, and the returned ehdrFlags contains what this Load function computes.
    92  // TODO: find a better place for this logic.
    93  func Load(l *loader.Loader, arch *sys.Arch, localSymVersion int, f *bio.Reader, pkg string, length int64, pn string, initEhdrFlags uint32) (textp []loader.Sym, ehdrFlags uint32, err error)