-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelfparse.go
More file actions
160 lines (135 loc) · 4.17 KB
/
elfparse.go
File metadata and controls
160 lines (135 loc) · 4.17 KB
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
// Copyright The Parca Authors
// SPDX-License-Identifier: Apache-2.0
package usdt // import "github.com/parca-dev/usdt"
import (
"encoding/binary"
"strings"
)
// ELFSection represents a minimal ELF section header with its data.
type ELFSection struct {
Name string
Addr uint64
Data []byte
}
// ELFProg represents a minimal ELF program header (PT_LOAD segment).
type ELFProg struct {
Vaddr uint64
Memsz uint64
Off uint64
}
// ELFReader is the interface that USDT probe parsing requires from an ELF file.
// Implementers provide section and program header data; the usdt package handles
// the .note.stapsdt parsing logic.
type ELFReader interface {
// Sections returns all ELF section headers with their data.
// Only .note.stapsdt and .stapsdt.base sections need to have Data populated;
// other sections may have nil Data.
Sections() ([]ELFSection, error)
// LoadSegments returns all PT_LOAD program headers.
LoadSegments() []ELFProg
}
// findLoadSegment finds the PT_LOAD segment containing the given virtual address.
func findLoadSegment(segs []ELFProg, addr uint64) *ELFProg {
for i := range segs {
if addr >= segs[i].Vaddr && addr < segs[i].Vaddr+segs[i].Memsz {
return &segs[i]
}
}
return nil
}
// ParseProbes reads USDT probe information from an ELF file via the ELFReader
// interface. It parses .note.stapsdt sections, applies prelink adjustments,
// and converts virtual addresses to file offsets suitable for uprobe attachment.
func ParseProbes(r ELFReader) ([]Probe, error) {
sections, err := r.Sections()
if err != nil {
return nil, err
}
// Find .note.stapsdt section
var stapsdtData []byte
var baseAddr uint64
for i := range sections {
switch sections[i].Name {
case ".note.stapsdt":
stapsdtData = sections[i].Data
case ".stapsdt.base":
baseAddr = sections[i].Addr
}
}
if stapsdtData == nil {
return nil, nil // No USDT probes in this binary
}
loadSegs := r.LoadSegments()
var probes []Probe
// Parse note entries
offset := 0
for offset < len(stapsdtData) {
if offset+12 > len(stapsdtData) {
break
}
// Note header: namesz(4) + descsz(4) + type(4)
namesz := binary.LittleEndian.Uint32(stapsdtData[offset : offset+4])
descsz := binary.LittleEndian.Uint32(stapsdtData[offset+4 : offset+8])
noteType := binary.LittleEndian.Uint32(stapsdtData[offset+8 : offset+12])
offset += 12
if noteType != 3 { // NT_STAPSDT
// Skip this note
nameEnd := offset + int((namesz+3)&^3) // align to 4 bytes
descEnd := nameEnd + int((descsz+3)&^3)
offset = descEnd
continue
}
// Skip owner name (should be "stapsdt")
nameEnd := offset + int((namesz+3)&^3)
if nameEnd+int(descsz) > len(stapsdtData) {
break
}
// Parse descriptor
desc := stapsdtData[nameEnd : nameEnd+int(descsz)]
if len(desc) < 24 { // 3 uint64 values
offset = nameEnd + int((descsz+3)&^3)
continue
}
location := binary.LittleEndian.Uint64(desc[0:8])
noteBase := binary.LittleEndian.Uint64(desc[8:16])
semaphore := binary.LittleEndian.Uint64(desc[16:24])
// Apply prelink adjustment if .stapsdt.base section exists
// See: https://sourceware.org/systemtap/wiki/UserSpaceProbeImplementation
if baseAddr != 0 && noteBase != 0 {
diff := baseAddr - noteBase
location += diff
if semaphore != 0 {
semaphore += diff
}
}
// Convert virtual address to file offset for uprobe attachment
prog := findLoadSegment(loadSegs, location)
if prog != nil {
location = location - prog.Vaddr + prog.Off
}
// Convert semaphore virtual address to file offset
var semaphoreFileOffset uint64
if semaphore != 0 {
semaProg := findLoadSegment(loadSegs, semaphore)
if semaProg != nil {
semaphoreFileOffset = semaphore - semaProg.Vaddr + semaProg.Off
}
}
// Parse strings: provider\0probe\0arguments\0
stringData := desc[24:]
parts := strings.Split(string(stringData), "\x00")
if len(parts) >= 3 {
probe := Probe{
Provider: parts[0],
Name: parts[1],
Location: location,
Base: noteBase,
SemaphoreOffset: semaphoreFileOffset,
Arguments: parts[2],
}
probes = append(probes, probe)
}
offset = nameEnd + int((descsz+3)&^3)
}
return probes, nil
}