File Index
Files and directories
Sign in
FILE

symbol-minimal.c

File preview with syntax highlighting.

linux-6.19.14/tools/perf/util/symbol-minimal.c C
  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
 15static 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
 33static 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
 78int 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 */
 88int 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	}
199out_free:
200	free(buf);
201	free(phdr);
202out:
203	close(fd);
204	return ret;
205}
206
207int 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);
231out_free:
232	free(buf);
233out:
234	close(fd);
235	return ret;
236}
237
238int 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;
253out_close:
254	close(fd);
255out_errno:
256	RC_CHK_ACCESS(dso)->load_errno = errno;
257	return -1;
258}
259
260bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused)
261{
262	/* Assume all sym sources could be a runtime image. */
263	return true;
264}
265
266bool symsrc__has_symtab(struct symsrc *ss __maybe_unused)
267{
268	return false;
269}
270
271void symsrc__destroy(struct symsrc *ss)
272{
273	zfree(&ss->name);
274	close(ss->fd);
275}
276
277int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
278				struct symsrc *ss __maybe_unused)
279{
280	return 0;
281}
282
283static 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
300enum 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
321int 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
338int 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
345int kcore_extract__create(struct kcore_extract *kce __maybe_unused)
346{
347	return -1;
348}
349
350void kcore_extract__delete(struct kcore_extract *kce __maybe_unused)
351{
352}
353
354int kcore_copy(const char *from_dir __maybe_unused,
355	       const char *to_dir __maybe_unused)
356{
357	return -1;
358}
359
360void symbol__elf_init(void)
361{
362}
363
364bool filename__has_section(const char *filename __maybe_unused, const char *sec __maybe_unused)
365{
366	return false;
367}
~/ Read-only file preview.