forked from goretk/gore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elf.go
169 lines (148 loc) · 4.05 KB
/
elf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// This file is part of GoRE.
//
// Copyright (C) 2019-2021 GoRE Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package gore
import (
"debug/elf"
"debug/gosym"
"fmt"
"os"
)
func openELF(fp string) (*elfFile, error) {
osFile, err := os.Open(fp)
if err != nil {
return nil, fmt.Errorf("error when opening the ELF file: %w", err)
}
f, err := elf.NewFile(osFile)
if err != nil {
return nil, fmt.Errorf("error when parsing the ELF file: %w", err)
}
return &elfFile{file: f, osFile: osFile}, nil
}
type elfFile struct {
file *elf.File
osFile *os.File
}
func (e *elfFile) getFile() *os.File {
return e.osFile
}
func (e *elfFile) getPCLNTab() (*gosym.Table, error) {
pclnSection := e.file.Section(".gopclntab")
if pclnSection == nil {
// No section found. Check if the PIE section exist instead.
pclnSection = e.file.Section(".data.rel.ro.gopclntab")
}
if pclnSection == nil {
return nil, fmt.Errorf("no gopclntab section found")
}
pclndat, err := pclnSection.Data()
if err != nil {
return nil, fmt.Errorf("could not get the data for the pclntab: %w", err)
}
pcln := gosym.NewLineTable(pclndat, e.file.Section(".text").Addr)
return gosym.NewTable(make([]byte, 0), pcln)
}
func (e *elfFile) Close() error {
err := e.file.Close()
if err != nil {
return err
}
return e.osFile.Close()
}
func (e *elfFile) getRData() ([]byte, error) {
section := e.file.Section(".rodata")
if section == nil {
return nil, ErrSectionDoesNotExist
}
return section.Data()
}
func (e *elfFile) getCodeSection() ([]byte, error) {
section := e.file.Section(".text")
if section == nil {
return nil, ErrSectionDoesNotExist
}
return section.Data()
}
func (e *elfFile) getPCLNTABData() (uint64, []byte, error) {
start, data, err := e.getSectionData(".gopclntab")
if err == ErrSectionDoesNotExist {
// Try PIE location
return e.getSectionData(".data.rel.ro.gopclntab")
}
return start, data, err
}
func (e *elfFile) moduledataSection() string {
return ".noptrdata"
}
func (e *elfFile) getSectionDataFromOffset(off uint64) (uint64, []byte, error) {
for _, section := range e.file.Sections {
if section.Offset == 0 {
// Only exist in memory
continue
}
if section.Addr <= off && off < (section.Addr+section.Size) {
data, err := section.Data()
return section.Addr, data, err
}
}
return 0, nil, ErrSectionDoesNotExist
}
func (e *elfFile) getSectionData(name string) (uint64, []byte, error) {
section := e.file.Section(name)
if section == nil {
return 0, nil, ErrSectionDoesNotExist
}
data, err := section.Data()
return section.Addr, data, err
}
func (e *elfFile) getFileInfo() *FileInfo {
var wordSize int
class := e.file.FileHeader.Class
if class == elf.ELFCLASS32 {
wordSize = intSize32
}
if class == elf.ELFCLASS64 {
wordSize = intSize64
}
var arch string
switch e.file.Machine {
case elf.EM_386:
arch = Arch386
case elf.EM_MIPS:
arch = ArchMIPS
case elf.EM_X86_64:
arch = ArchAMD64
case elf.EM_ARM:
arch = ArchARM
}
return &FileInfo{
ByteOrder: e.file.FileHeader.ByteOrder,
OS: e.file.Machine.String(),
WordSize: wordSize,
Arch: arch,
}
}
func (e *elfFile) getBuildID() (string, error) {
_, data, err := e.getSectionData(".note.go.buildid")
// If the note section does not exist, we just ignore the build id.
if err == ErrSectionDoesNotExist {
return "", nil
}
if err != nil {
return "", fmt.Errorf("error when getting note section: %w", err)
}
return parseBuildIDFromElf(data, e.file.ByteOrder)
}