xref: /openbmc/linux/tools/lib/symbol/kallsyms.c (revision f7d84fa7)
1 #include <ctype.h>
2 #include "symbol/kallsyms.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 
6 u8 kallsyms2elf_type(char type)
7 {
8 	type = tolower(type);
9 	return (type == 't' || type == 'w') ? STT_FUNC : STT_OBJECT;
10 }
11 
12 int kallsyms__parse(const char *filename, void *arg,
13 		    int (*process_symbol)(void *arg, const char *name,
14 					  char type, u64 start))
15 {
16 	char *line = NULL;
17 	size_t n;
18 	int err = -1;
19 	FILE *file = fopen(filename, "r");
20 
21 	if (file == NULL)
22 		goto out_failure;
23 
24 	err = 0;
25 
26 	while (!feof(file)) {
27 		u64 start;
28 		int line_len, len;
29 		char symbol_type;
30 		char *symbol_name;
31 
32 		line_len = getline(&line, &n, file);
33 		if (line_len < 0 || !line)
34 			break;
35 
36 		line[--line_len] = '\0'; /* \n */
37 
38 		len = hex2u64(line, &start);
39 
40 		len++;
41 		if (len + 2 >= line_len)
42 			continue;
43 
44 		symbol_type = line[len];
45 		len += 2;
46 		symbol_name = line + len;
47 		len = line_len - len;
48 
49 		if (len >= KSYM_NAME_LEN) {
50 			err = -1;
51 			break;
52 		}
53 
54 		err = process_symbol(arg, symbol_name, symbol_type, start);
55 		if (err)
56 			break;
57 	}
58 
59 	free(line);
60 	fclose(file);
61 	return err;
62 
63 out_failure:
64 	return -1;
65 }
66