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