kallsyms.c 1.0 KB

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