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

     1  // Copyright 2014 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 objfile
     6  
     7  import (
     8  	"github.com/shogo82148/std/container/list"
     9  	"github.com/shogo82148/std/debug/gosym"
    10  	"github.com/shogo82148/std/encoding/binary"
    11  	"github.com/shogo82148/std/io"
    12  	"github.com/shogo82148/std/regexp"
    13  )
    14  
    15  // Disasm is a disassembler for a given File.
    16  type Disasm struct {
    17  	syms      []Sym
    18  	pcln      Liner
    19  	text      []byte
    20  	textStart uint64
    21  	textEnd   uint64
    22  	goarch    string
    23  	disasm    disasmFunc
    24  	byteOrder binary.ByteOrder
    25  }
    26  
    27  // Disasm returns a disassembler for the file f.
    28  func (e *Entry) Disasm() (*Disasm, error)
    29  
    30  // CachedFile contains the content of a file split into lines.
    31  type CachedFile struct {
    32  	FileName string
    33  	Lines    [][]byte
    34  }
    35  
    36  // FileCache is a simple LRU cache of file contents.
    37  type FileCache struct {
    38  	files  *list.List
    39  	maxLen int
    40  }
    41  
    42  // NewFileCache returns a FileCache which can contain up to maxLen cached file contents.
    43  func NewFileCache(maxLen int) *FileCache
    44  
    45  // Line returns the source code line for the given file and line number.
    46  // If the file is not already cached, reads it, inserts it into the cache,
    47  // and removes the least recently used file if necessary.
    48  // If the file is in cache, it is moved to the front of the list.
    49  func (fc *FileCache) Line(filename string, line int) ([]byte, error)
    50  
    51  // Print prints a disassembly of the file to w.
    52  // If filter is non-nil, the disassembly only includes functions with names matching filter.
    53  // If printCode is true, the disassembly includs corresponding source lines.
    54  // The disassembly only includes functions that overlap the range [start, end).
    55  func (d *Disasm) Print(w io.Writer, filter *regexp.Regexp, start, end uint64, printCode bool, gnuAsm bool)
    56  
    57  // Decode disassembles the text segment range [start, end), calling f for each instruction.
    58  func (d *Disasm) Decode(start, end uint64, relocs []Reloc, gnuAsm bool, f func(pc, size uint64, file string, line int, text string))
    59  
    60  type Liner interface {
    61  	PCToLine(uint64) (string, int, *gosym.Func)
    62  }