kallsyms.c 1.2 KB

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