xref: /linux/tools/perf/util/symbol-minimal.c (revision 9e906a9dead17d81d6c2687f65e159231d0e3286)
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 	if (!is_regular_file(filename))
108 		return -EWOULDBLOCK;
109 
110 	fd = open(filename, O_RDONLY);
111 	if (fd < 0)
112 		return -1;
113 
114 	if (read(fd, hdrs.ehdr32.e_ident, EI_NIDENT) != EI_NIDENT)
115 		goto out;
116 
117 	if (memcmp(hdrs.ehdr32.e_ident, ELFMAG, SELFMAG) ||
118 	    hdrs.ehdr32.e_ident[EI_VERSION] != EV_CURRENT)
119 		goto out;
120 
121 	need_swap = check_need_swap(hdrs.ehdr32.e_ident[EI_DATA]);
122 	elf32 = hdrs.ehdr32.e_ident[EI_CLASS] == ELFCLASS32;
123 	ehdr_size = (elf32 ? sizeof(hdrs.ehdr32) : sizeof(hdrs.ehdr64)) - EI_NIDENT;
124 
125 	if (read(fd,
126 		 (elf32 ? (void *)&hdrs.ehdr32 : (void *)&hdrs.ehdr64) + EI_NIDENT,
127 		 ehdr_size) != ehdr_size)
128 		goto out;
129 
130 	if (need_swap) {
131 		if (elf32) {
132 			hdrs.ehdr32.e_phoff = bswap_32(hdrs.ehdr32.e_phoff);
133 			hdrs.ehdr32.e_phentsize = bswap_16(hdrs.ehdr32.e_phentsize);
134 			hdrs.ehdr32.e_phnum = bswap_16(hdrs.ehdr32.e_phnum);
135 		} else {
136 			hdrs.ehdr64.e_phoff = bswap_64(hdrs.ehdr64.e_phoff);
137 			hdrs.ehdr64.e_phentsize = bswap_16(hdrs.ehdr64.e_phentsize);
138 			hdrs.ehdr64.e_phnum = bswap_16(hdrs.ehdr64.e_phnum);
139 		}
140 	}
141 	if ((elf32 && hdrs.ehdr32.e_phentsize != sizeof(Elf32_Phdr)) ||
142 	    (!elf32 && hdrs.ehdr64.e_phentsize != sizeof(Elf64_Phdr)))
143 		goto out;
144 
145 	phdr_size = elf32 ? sizeof(Elf32_Phdr) * hdrs.ehdr32.e_phnum
146 			  : sizeof(Elf64_Phdr) * hdrs.ehdr64.e_phnum;
147 	phdr = malloc(phdr_size);
148 	if (phdr == NULL)
149 		goto out;
150 
151 	lseek(fd, elf32 ? hdrs.ehdr32.e_phoff : hdrs.ehdr64.e_phoff, SEEK_SET);
152 	if (read(fd, phdr, phdr_size) != phdr_size)
153 		goto out_free;
154 
155 	if (elf32)
156 		hdrs.phdr32 = phdr;
157 	else
158 		hdrs.phdr64 = phdr;
159 
160 	for (int i = 0; i < (elf32 ? hdrs.ehdr32.e_phnum : hdrs.ehdr64.e_phnum); i++) {
161 		ssize_t p_filesz;
162 
163 		if (need_swap) {
164 			if (elf32) {
165 				hdrs.phdr32[i].p_type = bswap_32(hdrs.phdr32[i].p_type);
166 				hdrs.phdr32[i].p_offset = bswap_32(hdrs.phdr32[i].p_offset);
167 				hdrs.phdr32[i].p_filesz = bswap_32(hdrs.phdr32[i].p_offset);
168 			} else {
169 				hdrs.phdr64[i].p_type = bswap_32(hdrs.phdr64[i].p_type);
170 				hdrs.phdr64[i].p_offset = bswap_64(hdrs.phdr64[i].p_offset);
171 				hdrs.phdr64[i].p_filesz = bswap_64(hdrs.phdr64[i].p_filesz);
172 			}
173 		}
174 		if ((elf32 ? hdrs.phdr32[i].p_type : hdrs.phdr64[i].p_type) != PT_NOTE)
175 			continue;
176 
177 		p_filesz = elf32 ? hdrs.phdr32[i].p_filesz : hdrs.phdr64[i].p_filesz;
178 		if (p_filesz > buf_size) {
179 			void *tmp;
180 
181 			buf_size = p_filesz;
182 			tmp = realloc(buf, buf_size);
183 			if (tmp == NULL)
184 				goto out_free;
185 			buf = tmp;
186 		}
187 		lseek(fd, elf32 ? hdrs.phdr32[i].p_offset : hdrs.phdr64[i].p_offset, SEEK_SET);
188 		if (read(fd, buf, p_filesz) != p_filesz)
189 			goto out_free;
190 
191 		ret = read_build_id(buf, p_filesz, bid, need_swap);
192 		if (ret == 0) {
193 			ret = bid->size;
194 			break;
195 		}
196 	}
197 out_free:
198 	free(buf);
199 	free(phdr);
200 out:
201 	close(fd);
202 	return ret;
203 }
204 
sysfs__read_build_id(const char * filename,struct build_id * bid)205 int sysfs__read_build_id(const char *filename, struct build_id *bid)
206 {
207 	int fd;
208 	int ret = -1;
209 	struct stat stbuf;
210 	size_t buf_size;
211 	void *buf;
212 
213 	fd = open(filename, O_RDONLY);
214 	if (fd < 0)
215 		return -1;
216 
217 	if (fstat(fd, &stbuf) < 0)
218 		goto out;
219 
220 	buf_size = stbuf.st_size;
221 	buf = malloc(buf_size);
222 	if (buf == NULL)
223 		goto out;
224 
225 	if (read(fd, buf, buf_size) != (ssize_t) buf_size)
226 		goto out_free;
227 
228 	ret = read_build_id(buf, buf_size, bid, false);
229 out_free:
230 	free(buf);
231 out:
232 	close(fd);
233 	return ret;
234 }
235 
symsrc__init(struct symsrc * ss,struct dso * dso,const char * name,enum dso_binary_type type)236 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
237 	         enum dso_binary_type type)
238 {
239 	int fd = open(name, O_RDONLY);
240 	if (fd < 0)
241 		goto out_errno;
242 
243 	ss->name = strdup(name);
244 	if (!ss->name)
245 		goto out_close;
246 
247 	ss->fd = fd;
248 	ss->type = type;
249 
250 	return 0;
251 out_close:
252 	close(fd);
253 out_errno:
254 	RC_CHK_ACCESS(dso)->load_errno = errno;
255 	return -1;
256 }
257 
symsrc__possibly_runtime(struct symsrc * ss __maybe_unused)258 bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused)
259 {
260 	/* Assume all sym sources could be a runtime image. */
261 	return true;
262 }
263 
symsrc__has_symtab(struct symsrc * ss __maybe_unused)264 bool symsrc__has_symtab(struct symsrc *ss __maybe_unused)
265 {
266 	return false;
267 }
268 
symsrc__destroy(struct symsrc * ss)269 void symsrc__destroy(struct symsrc *ss)
270 {
271 	zfree(&ss->name);
272 	close(ss->fd);
273 }
274 
dso__synthesize_plt_symbols(struct dso * dso __maybe_unused,struct symsrc * ss __maybe_unused)275 int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
276 				struct symsrc *ss __maybe_unused)
277 {
278 	return 0;
279 }
280 
fd__is_64_bit(int fd)281 static int fd__is_64_bit(int fd)
282 {
283 	u8 e_ident[EI_NIDENT];
284 
285 	if (lseek(fd, 0, SEEK_SET))
286 		return -1;
287 
288 	if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
289 		return -1;
290 
291 	if (memcmp(e_ident, ELFMAG, SELFMAG) ||
292 	    e_ident[EI_VERSION] != EV_CURRENT)
293 		return -1;
294 
295 	return e_ident[EI_CLASS] == ELFCLASS64;
296 }
297 
dso__type_fd(int fd)298 enum dso_type dso__type_fd(int fd)
299 {
300 	Elf64_Ehdr ehdr;
301 	int ret;
302 
303 	ret = fd__is_64_bit(fd);
304 	if (ret < 0)
305 		return DSO__TYPE_UNKNOWN;
306 
307 	if (ret)
308 		return DSO__TYPE_64BIT;
309 
310 	if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
311 		return DSO__TYPE_UNKNOWN;
312 
313 	if (ehdr.e_machine == EM_X86_64)
314 		return DSO__TYPE_X32BIT;
315 
316 	return DSO__TYPE_32BIT;
317 }
318 
dso__load_sym(struct dso * dso,struct map * map __maybe_unused,struct symsrc * ss,struct symsrc * runtime_ss __maybe_unused,int kmodule __maybe_unused)319 int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
320 		  struct symsrc *ss,
321 		  struct symsrc *runtime_ss __maybe_unused,
322 		  int kmodule __maybe_unused)
323 {
324 	struct build_id bid = { .size = 0, };
325 	int ret;
326 
327 	ret = fd__is_64_bit(ss->fd);
328 	if (ret >= 0)
329 		RC_CHK_ACCESS(dso)->is_64_bit = ret;
330 
331 	if (filename__read_build_id(ss->name, &bid) > 0)
332 		dso__set_build_id(dso, &bid);
333 	return 0;
334 }
335 
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)336 int file__read_maps(int fd __maybe_unused, bool exe __maybe_unused,
337 		    mapfn_t mapfn __maybe_unused, void *data __maybe_unused,
338 		    bool *is_64_bit __maybe_unused)
339 {
340 	return -1;
341 }
342 
kcore_extract__create(struct kcore_extract * kce __maybe_unused)343 int kcore_extract__create(struct kcore_extract *kce __maybe_unused)
344 {
345 	return -1;
346 }
347 
kcore_extract__delete(struct kcore_extract * kce __maybe_unused)348 void kcore_extract__delete(struct kcore_extract *kce __maybe_unused)
349 {
350 }
351 
kcore_copy(const char * from_dir __maybe_unused,const char * to_dir __maybe_unused)352 int kcore_copy(const char *from_dir __maybe_unused,
353 	       const char *to_dir __maybe_unused)
354 {
355 	return -1;
356 }
357 
symbol__elf_init(void)358 void symbol__elf_init(void)
359 {
360 }
361 
filename__has_section(const char * filename __maybe_unused,const char * sec __maybe_unused)362 bool filename__has_section(const char *filename __maybe_unused, const char *sec __maybe_unused)
363 {
364 	return false;
365 }
366