Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 2 | #include <ctype.h> |
Arnaldo Carvalho de Melo | c506c96 | 2013-12-11 09:15:00 -0300 | [diff] [blame] | 3 | #include "symbol/kallsyms.h" |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | |
Arnaldo Carvalho de Melo | f845086 | 2015-09-30 13:02:08 -0300 | [diff] [blame] | 7 | u8 kallsyms2elf_type(char type) |
| 8 | { |
| 9 | type = tolower(type); |
| 10 | return (type == 't' || type == 'w') ? STT_FUNC : STT_OBJECT; |
| 11 | } |
| 12 | |
Arnaldo Carvalho de Melo | c506c96 | 2013-12-11 09:15:00 -0300 | [diff] [blame] | 13 | int kallsyms__parse(const char *filename, void *arg, |
| 14 | int (*process_symbol)(void *arg, const char *name, |
| 15 | char type, u64 start)) |
| 16 | { |
| 17 | char *line = NULL; |
| 18 | size_t n; |
| 19 | int err = -1; |
| 20 | FILE *file = fopen(filename, "r"); |
| 21 | |
| 22 | if (file == NULL) |
| 23 | goto out_failure; |
| 24 | |
| 25 | err = 0; |
| 26 | |
| 27 | while (!feof(file)) { |
| 28 | u64 start; |
| 29 | int line_len, len; |
| 30 | char symbol_type; |
| 31 | char *symbol_name; |
| 32 | |
| 33 | line_len = getline(&line, &n, file); |
| 34 | if (line_len < 0 || !line) |
| 35 | break; |
| 36 | |
| 37 | line[--line_len] = '\0'; /* \n */ |
| 38 | |
| 39 | len = hex2u64(line, &start); |
| 40 | |
| 41 | len++; |
| 42 | if (len + 2 >= line_len) |
| 43 | continue; |
| 44 | |
| 45 | symbol_type = line[len]; |
| 46 | len += 2; |
| 47 | symbol_name = line + len; |
| 48 | len = line_len - len; |
| 49 | |
| 50 | if (len >= KSYM_NAME_LEN) { |
| 51 | err = -1; |
| 52 | break; |
| 53 | } |
| 54 | |
| 55 | err = process_symbol(arg, symbol_name, symbol_type, start); |
| 56 | if (err) |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | free(line); |
| 61 | fclose(file); |
| 62 | return err; |
| 63 | |
| 64 | out_failure: |
| 65 | return -1; |
| 66 | } |