1 // SPDX-License-Identifier: GPL-2.0
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <poll.h>
10 #include <unistd.h>
11 #include <linux/perf_event.h>
12 #include <sys/mman.h>
13 #include "trace_helpers.h"
14 
15 #define DEBUGFS "/sys/kernel/debug/tracing/"
16 
17 #define MAX_SYMS 300000
18 static struct ksym syms[MAX_SYMS];
19 static int sym_cnt;
20 
21 static int ksym_cmp(const void *p1, const void *p2)
22 {
23 	return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
24 }
25 
26 int load_kallsyms(void)
27 {
28 	FILE *f = fopen("/proc/kallsyms", "r");
29 	char func[256], buf[256];
30 	char symbol;
31 	void *addr;
32 	int i = 0;
33 
34 	if (!f)
35 		return -ENOENT;
36 
37 	/*
38 	 * This is called/used from multiplace places,
39 	 * load symbols just once.
40 	 */
41 	if (sym_cnt)
42 		return 0;
43 
44 	while (fgets(buf, sizeof(buf), f)) {
45 		if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
46 			break;
47 		if (!addr)
48 			continue;
49 		syms[i].addr = (long) addr;
50 		syms[i].name = strdup(func);
51 		i++;
52 	}
53 	fclose(f);
54 	sym_cnt = i;
55 	qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
56 	return 0;
57 }
58 
59 struct ksym *ksym_search(long key)
60 {
61 	int start = 0, end = sym_cnt;
62 	int result;
63 
64 	/* kallsyms not loaded. return NULL */
65 	if (sym_cnt <= 0)
66 		return NULL;
67 
68 	while (start < end) {
69 		size_t mid = start + (end - start) / 2;
70 
71 		result = key - syms[mid].addr;
72 		if (result < 0)
73 			end = mid;
74 		else if (result > 0)
75 			start = mid + 1;
76 		else
77 			return &syms[mid];
78 	}
79 
80 	if (start >= 1 && syms[start - 1].addr < key &&
81 	    key < syms[start].addr)
82 		/* valid ksym */
83 		return &syms[start - 1];
84 
85 	/* out of range. return _stext */
86 	return &syms[0];
87 }
88 
89 long ksym_get_addr(const char *name)
90 {
91 	int i;
92 
93 	for (i = 0; i < sym_cnt; i++) {
94 		if (strcmp(syms[i].name, name) == 0)
95 			return syms[i].addr;
96 	}
97 
98 	return 0;
99 }
100 
101 /* open kallsyms and read symbol addresses on the fly. Without caching all symbols,
102  * this is faster than load + find.
103  */
104 int kallsyms_find(const char *sym, unsigned long long *addr)
105 {
106 	char type, name[500];
107 	unsigned long long value;
108 	int err = 0;
109 	FILE *f;
110 
111 	f = fopen("/proc/kallsyms", "r");
112 	if (!f)
113 		return -EINVAL;
114 
115 	while (fscanf(f, "%llx %c %499s%*[^\n]\n", &value, &type, name) > 0) {
116 		if (strcmp(name, sym) == 0) {
117 			*addr = value;
118 			goto out;
119 		}
120 	}
121 	err = -ENOENT;
122 
123 out:
124 	fclose(f);
125 	return err;
126 }
127 
128 void read_trace_pipe(void)
129 {
130 	int trace_fd;
131 
132 	trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
133 	if (trace_fd < 0)
134 		return;
135 
136 	while (1) {
137 		static char buf[4096];
138 		ssize_t sz;
139 
140 		sz = read(trace_fd, buf, sizeof(buf) - 1);
141 		if (sz > 0) {
142 			buf[sz] = 0;
143 			puts(buf);
144 		}
145 	}
146 }
147 
148 ssize_t get_uprobe_offset(const void *addr)
149 {
150 	size_t start, end, base;
151 	char buf[256];
152 	bool found = false;
153 	FILE *f;
154 
155 	f = fopen("/proc/self/maps", "r");
156 	if (!f)
157 		return -errno;
158 
159 	while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &base) == 4) {
160 		if (buf[2] == 'x' && (uintptr_t)addr >= start && (uintptr_t)addr < end) {
161 			found = true;
162 			break;
163 		}
164 	}
165 
166 	fclose(f);
167 
168 	if (!found)
169 		return -ESRCH;
170 
171 #if defined(__powerpc64__) && defined(_CALL_ELF) && _CALL_ELF == 2
172 
173 #define OP_RT_RA_MASK   0xffff0000UL
174 #define LIS_R2          0x3c400000UL
175 #define ADDIS_R2_R12    0x3c4c0000UL
176 #define ADDI_R2_R2      0x38420000UL
177 
178 	/*
179 	 * A PPC64 ABIv2 function may have a local and a global entry
180 	 * point. We need to use the local entry point when patching
181 	 * functions, so identify and step over the global entry point
182 	 * sequence.
183 	 *
184 	 * The global entry point sequence is always of the form:
185 	 *
186 	 * addis r2,r12,XXXX
187 	 * addi  r2,r2,XXXX
188 	 *
189 	 * A linker optimisation may convert the addis to lis:
190 	 *
191 	 * lis   r2,XXXX
192 	 * addi  r2,r2,XXXX
193 	 */
194 	{
195 		const u32 *insn = (const u32 *)(uintptr_t)addr;
196 
197 		if ((((*insn & OP_RT_RA_MASK) == ADDIS_R2_R12) ||
198 		     ((*insn & OP_RT_RA_MASK) == LIS_R2)) &&
199 		    ((*(insn + 1) & OP_RT_RA_MASK) == ADDI_R2_R2))
200 			return (uintptr_t)(insn + 2) - start + base;
201 	}
202 #endif
203 	return (uintptr_t)addr - start + base;
204 }
205 
206 ssize_t get_rel_offset(uintptr_t addr)
207 {
208 	size_t start, end, offset;
209 	char buf[256];
210 	FILE *f;
211 
212 	f = fopen("/proc/self/maps", "r");
213 	if (!f)
214 		return -errno;
215 
216 	while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &offset) == 4) {
217 		if (addr >= start && addr < end) {
218 			fclose(f);
219 			return (size_t)addr - start + offset;
220 		}
221 	}
222 
223 	fclose(f);
224 	return -EINVAL;
225 }
226