Debian Codegraph @

src/cmd/internal/objfile/objfile.go

raw
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 implements portable access to OS-specific executable files.
6 package objfile
7
8 import (
9 "cmd/internal/archive"
10 "cmp"
11 "debug/dwarf"
12 "debug/gosym"
13 "fmt"
14 "io"
15 "os"
16 "slices"
17 )
18
19 type rawFile interface {
20 symbols() (syms []Sym, err error)
21 pcln() (textStart uint64, pclntab []byte, err error)
22 text() (textStart uint64, text []byte, err error)
23 goarch() string
24 loadAddress() (uint64, error)
25 dwarf() (*dwarf.Data, error)
26 }
27
28 // A File is an opened executable file.
29 type File struct {
30 r *os.File
31 entries []*Entry
32 }
33
34 type Entry struct {
35 name string
36 raw rawFile
37 }
38
39 // A Sym is a symbol defined in an executable file.
40 type Sym struct {
41 Name string // symbol name
42 Addr uint64 // virtual address of symbol
43 Size int64 // size in bytes
44 Code rune // nm code (T for text, D for data, and so on)
45 Type string // XXX?
46 Relocs []Reloc // in increasing Addr order
47 }
48
49 type Reloc struct {
50 Addr uint64 // Address of first byte that reloc applies to.
51 Size uint64 // Number of bytes
52 Stringer RelocStringer
53 }
54
55 type RelocStringer interface {
56 // insnOffset is the offset of the instruction containing the relocation
57 // from the start of the symbol containing the relocation.
58 String(insnOffset uint64) string
59 }
60
61 var openers = []func(io.ReaderAt) (rawFile, error){
62 openElf,
63 openMacho,
64 openPE,
65 openPlan9,
66 openXcoff,
67 }
68
69 // Open opens the named file.
70 // The caller must call f.Close when the file is no longer needed.
71 func Open(name string) (*File, error) {
72 r, err := os.Open(name)
73 if err != nil {
74 return nil, err
75 }
76 if f, err := openGoFile(r); err == nil {
77 return f, nil
78 } else if _, ok := err.(archive.ErrGoObjOtherVersion); ok {
79 return nil, fmt.Errorf("open %s: %v", name, err)
80 }
81 for _, try := range openers {
82 if raw, err := try(r); err == nil {
83 return &File{r, []*Entry{{raw: raw}}}, nil
84 }
85 }
86 r.Close()
87 return nil, fmt.Errorf("open %s: unrecognized object file", name)
88 }
89
90 func (f *File) Close() error {
91 return f.r.Close()
92 }
93
94 func (f *File) Entries() []*Entry {
95 return f.entries
96 }
97
98 func (f *File) Symbols() ([]Sym, error) {
99 return f.entries[0].Symbols()
100 }
101
102 func (f *File) PCLineTable() (Liner, error) {
103 return f.entries[0].PCLineTable()
104 }
105
106 func (f *File) Text() (uint64, []byte, error) {
107 return f.entries[0].Text()
108 }
109
110 func (f *File) GOARCH() string {
111 return f.entries[0].GOARCH()
112 }
113
114 func (f *File) LoadAddress() (uint64, error) {
115 return f.entries[0].LoadAddress()
116 }
117
118 func (f *File) DWARF() (*dwarf.Data, error) {
119 return f.entries[0].DWARF()
120 }
121
122 func (e *Entry) Name() string {
123 return e.name
124 }
125
126 func (e *Entry) Symbols() ([]Sym, error) {
127 syms, err := e.raw.symbols()
128 if err != nil {
129 return nil, err
130 }
131 slices.SortFunc(syms, func(a, b Sym) int {
132 return cmp.Compare(a.Addr, b.Addr)
133 })
134 return syms, nil
135 }
136
137 func (e *Entry) PCLineTable() (Liner, error) {
138 // If the raw file implements Liner directly, use that.
139 // Currently, only Go intermediate objects and archives (goobj) use this path.
140 if pcln, ok := e.raw.(Liner); ok {
141 return pcln, nil
142 }
143 // Otherwise, read the pcln tables and build a Liner out of that.
144 textStart, pclntab, err := e.raw.pcln()
145 if err != nil {
146 return nil, err
147 }
148 syms, err := e.raw.symbols()
149 if err == nil {
150 for _, s := range syms {
151 if s.Name == "runtime.text" {
152 textStart = s.Addr
153 break
154 }
155 }
156 }
157 return gosym.NewTable(nil, gosym.NewLineTable(pclntab, textStart))
158 }
159
160 func (e *Entry) Text() (uint64, []byte, error) {
161 return e.raw.text()
162 }
163
164 func (e *Entry) GOARCH() string {
165 return e.raw.goarch()
166 }
167
168 // LoadAddress returns the expected load address of the file.
169 // This differs from the actual load address for a position-independent
170 // executable.
171 func (e *Entry) LoadAddress() (uint64, error) {
172 return e.raw.loadAddress()
173 }
174
175 // DWARF returns DWARF debug data for the file, if any.
176 // This is for cmd/pprof to locate cgo functions.
177 func (e *Entry) DWARF() (*dwarf.Data, error) {
178 return e.raw.dwarf()
179 }
180
181 type Liner interface {
182 // Given a pc, returns the corresponding file, line, and function data.
183 // If unknown, returns "",0,nil.
184 PCToLine(uint64) (string, int, *gosym.Func)
185 }