xref: /linux/tools/perf/util/symbol-minimal.c (revision 04eeb606a8383b306f4bc6991da8231b5f3924b0)
1 #include "symbol.h"
2 #include "util.h"
3 
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <byteswap.h>
8 #include <sys/stat.h>
9 
10 
11 static bool check_need_swap(int file_endian)
12 {
13 	const int data = 1;
14 	u8 *check = (u8 *)&data;
15 	int host_endian;
16 
17 	if (check[0] == 1)
18 		host_endian = ELFDATA2LSB;
19 	else
20 		host_endian = ELFDATA2MSB;
21 
22 	return host_endian != file_endian;
23 }
24 
25 #define NOTE_ALIGN(sz) (((sz) + 3) & ~3)
26 
27 #define NT_GNU_BUILD_ID	3
28 
29 static int read_build_id(void *note_data, size_t note_len, void *bf,
30 			 size_t size, bool need_swap)
31 {
32 	struct {
33 		u32 n_namesz;
34 		u32 n_descsz;
35 		u32 n_type;
36 	} *nhdr;
37 	void *ptr;
38 
39 	ptr = note_data;
40 	while (ptr < (note_data + note_len)) {
41 		const char *name;
42 		size_t namesz, descsz;
43 
44 		nhdr = ptr;
45 		if (need_swap) {
46 			nhdr->n_namesz = bswap_32(nhdr->n_namesz);
47 			nhdr->n_descsz = bswap_32(nhdr->n_descsz);
48 			nhdr->n_type = bswap_32(nhdr->n_type);
49 		}
50 
51 		namesz = NOTE_ALIGN(nhdr->n_namesz);
52 		descsz = NOTE_ALIGN(nhdr->n_descsz);
53 
54 		ptr += sizeof(*nhdr);
55 		name = ptr;
56 		ptr += namesz;
57 		if (nhdr->n_type == NT_GNU_BUILD_ID &&
58 		    nhdr->n_namesz == sizeof("GNU")) {
59 			if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
60 				size_t sz = min(size, descsz);
61 				memcpy(bf, ptr, sz);
62 				memset(bf + sz, 0, size - sz);
63 				return 0;
64 			}
65 		}
66 		ptr += descsz;
67 	}
68 
69 	return -1;
70 }
71 
72 int filename__read_debuglink(const char *filename __maybe_unused,
73 			     char *debuglink __maybe_unused,
74 			     size_t size __maybe_unused)
75 {
76 	return -1;
77 }
78 
79 /*
80  * Just try PT_NOTE header otherwise fails
81  */
82 int filename__read_build_id(const char *filename, void *bf, size_t size)
83 {
84 	FILE *fp;
85 	int ret = -1;
86 	bool need_swap = false;
87 	u8 e_ident[EI_NIDENT];
88 	size_t buf_size;
89 	void *buf;
90 	int i;
91 
92 	fp = fopen(filename, "r");
93 	if (fp == NULL)
94 		return -1;
95 
96 	if (fread(e_ident, sizeof(e_ident), 1, fp) != 1)
97 		goto out;
98 
99 	if (memcmp(e_ident, ELFMAG, SELFMAG) ||
100 	    e_ident[EI_VERSION] != EV_CURRENT)
101 		goto out;
102 
103 	need_swap = check_need_swap(e_ident[EI_DATA]);
104 
105 	/* for simplicity */
106 	fseek(fp, 0, SEEK_SET);
107 
108 	if (e_ident[EI_CLASS] == ELFCLASS32) {
109 		Elf32_Ehdr ehdr;
110 		Elf32_Phdr *phdr;
111 
112 		if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
113 			goto out;
114 
115 		if (need_swap) {
116 			ehdr.e_phoff = bswap_32(ehdr.e_phoff);
117 			ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
118 			ehdr.e_phnum = bswap_16(ehdr.e_phnum);
119 		}
120 
121 		buf_size = ehdr.e_phentsize * ehdr.e_phnum;
122 		buf = malloc(buf_size);
123 		if (buf == NULL)
124 			goto out;
125 
126 		fseek(fp, ehdr.e_phoff, SEEK_SET);
127 		if (fread(buf, buf_size, 1, fp) != 1)
128 			goto out_free;
129 
130 		for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
131 			void *tmp;
132 
133 			if (need_swap) {
134 				phdr->p_type = bswap_32(phdr->p_type);
135 				phdr->p_offset = bswap_32(phdr->p_offset);
136 				phdr->p_filesz = bswap_32(phdr->p_filesz);
137 			}
138 
139 			if (phdr->p_type != PT_NOTE)
140 				continue;
141 
142 			buf_size = phdr->p_filesz;
143 			tmp = realloc(buf, buf_size);
144 			if (tmp == NULL)
145 				goto out_free;
146 
147 			buf = tmp;
148 			fseek(fp, phdr->p_offset, SEEK_SET);
149 			if (fread(buf, buf_size, 1, fp) != 1)
150 				goto out_free;
151 
152 			ret = read_build_id(buf, buf_size, bf, size, need_swap);
153 			if (ret == 0)
154 				ret = size;
155 			break;
156 		}
157 	} else {
158 		Elf64_Ehdr ehdr;
159 		Elf64_Phdr *phdr;
160 
161 		if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
162 			goto out;
163 
164 		if (need_swap) {
165 			ehdr.e_phoff = bswap_64(ehdr.e_phoff);
166 			ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
167 			ehdr.e_phnum = bswap_16(ehdr.e_phnum);
168 		}
169 
170 		buf_size = ehdr.e_phentsize * ehdr.e_phnum;
171 		buf = malloc(buf_size);
172 		if (buf == NULL)
173 			goto out;
174 
175 		fseek(fp, ehdr.e_phoff, SEEK_SET);
176 		if (fread(buf, buf_size, 1, fp) != 1)
177 			goto out_free;
178 
179 		for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
180 			void *tmp;
181 
182 			if (need_swap) {
183 				phdr->p_type = bswap_32(phdr->p_type);
184 				phdr->p_offset = bswap_64(phdr->p_offset);
185 				phdr->p_filesz = bswap_64(phdr->p_filesz);
186 			}
187 
188 			if (phdr->p_type != PT_NOTE)
189 				continue;
190 
191 			buf_size = phdr->p_filesz;
192 			tmp = realloc(buf, buf_size);
193 			if (tmp == NULL)
194 				goto out_free;
195 
196 			buf = tmp;
197 			fseek(fp, phdr->p_offset, SEEK_SET);
198 			if (fread(buf, buf_size, 1, fp) != 1)
199 				goto out_free;
200 
201 			ret = read_build_id(buf, buf_size, bf, size, need_swap);
202 			if (ret == 0)
203 				ret = size;
204 			break;
205 		}
206 	}
207 out_free:
208 	free(buf);
209 out:
210 	fclose(fp);
211 	return ret;
212 }
213 
214 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
215 {
216 	int fd;
217 	int ret = -1;
218 	struct stat stbuf;
219 	size_t buf_size;
220 	void *buf;
221 
222 	fd = open(filename, O_RDONLY);
223 	if (fd < 0)
224 		return -1;
225 
226 	if (fstat(fd, &stbuf) < 0)
227 		goto out;
228 
229 	buf_size = stbuf.st_size;
230 	buf = malloc(buf_size);
231 	if (buf == NULL)
232 		goto out;
233 
234 	if (read(fd, buf, buf_size) != (ssize_t) buf_size)
235 		goto out_free;
236 
237 	ret = read_build_id(buf, buf_size, build_id, size, false);
238 out_free:
239 	free(buf);
240 out:
241 	close(fd);
242 	return ret;
243 }
244 
245 int symsrc__init(struct symsrc *ss, struct dso *dso __maybe_unused,
246 		 const char *name,
247 	         enum dso_binary_type type)
248 {
249 	int fd = open(name, O_RDONLY);
250 	if (fd < 0)
251 		return -1;
252 
253 	ss->name = strdup(name);
254 	if (!ss->name)
255 		goto out_close;
256 
257 	ss->fd = fd;
258 	ss->type = type;
259 
260 	return 0;
261 out_close:
262 	close(fd);
263 	return -1;
264 }
265 
266 bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused)
267 {
268 	/* Assume all sym sources could be a runtime image. */
269 	return true;
270 }
271 
272 bool symsrc__has_symtab(struct symsrc *ss __maybe_unused)
273 {
274 	return false;
275 }
276 
277 void symsrc__destroy(struct symsrc *ss)
278 {
279 	zfree(&ss->name);
280 	close(ss->fd);
281 }
282 
283 int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
284 				struct symsrc *ss __maybe_unused,
285 				struct map *map __maybe_unused,
286 				symbol_filter_t filter __maybe_unused)
287 {
288 	return 0;
289 }
290 
291 static int fd__is_64_bit(int fd)
292 {
293 	u8 e_ident[EI_NIDENT];
294 
295 	if (lseek(fd, 0, SEEK_SET))
296 		return -1;
297 
298 	if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
299 		return -1;
300 
301 	if (memcmp(e_ident, ELFMAG, SELFMAG) ||
302 	    e_ident[EI_VERSION] != EV_CURRENT)
303 		return -1;
304 
305 	return e_ident[EI_CLASS] == ELFCLASS64;
306 }
307 
308 enum dso_type dso__type_fd(int fd)
309 {
310 	Elf64_Ehdr ehdr;
311 	int ret;
312 
313 	ret = fd__is_64_bit(fd);
314 	if (ret < 0)
315 		return DSO__TYPE_UNKNOWN;
316 
317 	if (ret)
318 		return DSO__TYPE_64BIT;
319 
320 	if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
321 		return DSO__TYPE_UNKNOWN;
322 
323 	if (ehdr.e_machine == EM_X86_64)
324 		return DSO__TYPE_X32BIT;
325 
326 	return DSO__TYPE_32BIT;
327 }
328 
329 int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
330 		  struct symsrc *ss,
331 		  struct symsrc *runtime_ss __maybe_unused,
332 		  symbol_filter_t filter __maybe_unused,
333 		  int kmodule __maybe_unused)
334 {
335 	unsigned char *build_id[BUILD_ID_SIZE];
336 	int ret;
337 
338 	ret = fd__is_64_bit(ss->fd);
339 	if (ret >= 0)
340 		dso->is_64_bit = ret;
341 
342 	if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) {
343 		dso__set_build_id(dso, build_id);
344 		return 1;
345 	}
346 	return 0;
347 }
348 
349 int file__read_maps(int fd __maybe_unused, bool exe __maybe_unused,
350 		    mapfn_t mapfn __maybe_unused, void *data __maybe_unused,
351 		    bool *is_64_bit __maybe_unused)
352 {
353 	return -1;
354 }
355 
356 int kcore_extract__create(struct kcore_extract *kce __maybe_unused)
357 {
358 	return -1;
359 }
360 
361 void kcore_extract__delete(struct kcore_extract *kce __maybe_unused)
362 {
363 }
364 
365 int kcore_copy(const char *from_dir __maybe_unused,
366 	       const char *to_dir __maybe_unused)
367 {
368 	return -1;
369 }
370 
371 void symbol__elf_init(void)
372 {
373 }
374