kallsyms.c 1.1 KB

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