github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/debug/plan9obj/file.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  /*
     6  plan9objパッケージは、Plan 9 a.outオブジェクトファイルへのアクセスを実装します。
     7  
     8  # セキュリティ
     9  
    10  このパッケージは、敵対的な入力に対して強化されるように設計されていませんし、
    11  https://go.dev/security/policy の範囲外です。特に、オブジェクトファイルを解析する際には基本的な
    12  検証のみが行われます。そのため、信頼できない入力を解析する際には注意が必要です。なぜなら、
    13  不正なファイルを解析すると、大量のリソースを消費したり、パニックを引き起こす可能性があるからです。
    14  */
    15  package plan9obj
    16  
    17  import (
    18  	"github.com/shogo82148/std/errors"
    19  	"github.com/shogo82148/std/io"
    20  )
    21  
    22  // FileHeaderは、Plan 9 a.outファイルヘッダーを表します。
    23  type FileHeader struct {
    24  	Magic       uint32
    25  	Bss         uint32
    26  	Entry       uint64
    27  	PtrSize     int
    28  	LoadAddress uint64
    29  	HdrSize     uint64
    30  }
    31  
    32  // Fileは、開いているPlan 9 a.outファイルを表します。
    33  type File struct {
    34  	FileHeader
    35  	Sections []*Section
    36  	closer   io.Closer
    37  }
    38  
    39  // SectionHeaderは、単一のPlan 9 a.outセクションヘッダーを表します。
    40  // この構造体はディスク上には存在せず、オブジェクトファイルを通じた
    41  // ナビゲーションを容易にします。
    42  type SectionHeader struct {
    43  	Name   string
    44  	Size   uint32
    45  	Offset uint32
    46  }
    47  
    48  // Sectionは、Plan 9 a.outファイルの単一のセクションを表します。
    49  type Section struct {
    50  	SectionHeader
    51  
    52  	// ReadAtメソッドのためにReaderAtを埋め込みます。
    53  	// ReadとSeekを持つことを避けるために、
    54  	// SectionReaderを直接埋め込むことはありません。
    55  	// クライアントがReadとSeekを使用したい場合は、
    56  	// 他のクライアントとのシークオフセットの競合を避けるために
    57  	// Open()を使用する必要があります。
    58  	io.ReaderAt
    59  	sr *io.SectionReader
    60  }
    61  
    62  // Dataは、Plan 9 a.outセクションの内容を読み取り、返します。
    63  func (s *Section) Data() ([]byte, error)
    64  
    65  // Openは、Plan 9 a.outセクションを読み取る新しいReadSeekerを返します。
    66  func (s *Section) Open() io.ReadSeeker
    67  
    68  // Symは、Plan 9 a.outのシンボルテーブルセクションのエントリを表します。
    69  type Sym struct {
    70  	Value uint64
    71  	Type  rune
    72  	Name  string
    73  }
    74  
    75  // Openは、[os.Open] を使用して指定された名前のファイルを開き、
    76  // それをPlan 9 a.outバイナリとして使用するための準備をします。
    77  func Open(name string) (*File, error)
    78  
    79  // Closeは、[File] を閉じます。
    80  // [File] が [Open] ではなく [NewFile] を直接使用して作成された場合、
    81  // Closeは何も影響を及ぼしません。
    82  func (f *File) Close() error
    83  
    84  // NewFileは、基礎となるリーダーでPlan 9バイナリにアクセスするための新しい [File] を作成します。
    85  // Plan 9バイナリは、ReaderAtの位置0で開始することが期待されます。
    86  func NewFile(r io.ReaderAt) (*File, error)
    87  
    88  // ErrNoSymbolsは、File内にそのようなセクションがない場合に、
    89  // [File.Symbols] によって返されるエラーです。
    90  var ErrNoSymbols = errors.New("no symbol section")
    91  
    92  // Symbolsは、fのシンボルテーブルを返します。
    93  func (f *File) Symbols() ([]Sym, error)
    94  
    95  // Sectionは、指定された名前のセクションを返します。
    96  // そのようなセクションが存在しない場合はnilを返します。
    97  func (f *File) Section(name string) *Section