symbol.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2018 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stddef.h>
  18. #include <stdint.h>
  19. #include <kern/macros.h>
  20. #include <kern/symbol.h>
  21. /*
  22. * XXX Clang doesn't consider that weak symbols may change at link time
  23. * by default, which turns the lookup into a no-op when optimizations
  24. * are enabled. Make these variables volatile to work around this issue.
  25. */
  26. const volatile size_t symbol_table_size __weak;
  27. const struct symbol* volatile symbol_table_ptr __weak;
  28. const struct symbol*
  29. symbol_lookup (uintptr_t addr)
  30. {
  31. struct symbol_iter iter;
  32. for (symbol_iter_init (&iter); symbol_iter_valid (&iter);
  33. symbol_iter_next (&iter))
  34. {
  35. const _Auto symbol = iter.symbol;
  36. if (addr >= symbol->addr && addr < symbol->addr + symbol->size)
  37. return (symbol);
  38. }
  39. return (NULL);
  40. }
  41. static inline bool
  42. symbol_iter_adv (struct symbol_iter *iter)
  43. {
  44. const struct symbol *table = symbol_table_ptr;
  45. size_t size = symbol_table_size;
  46. while (1)
  47. {
  48. const struct symbol *symbol = &table[iter->idx];
  49. if (symbol->name && symbol->size)
  50. {
  51. iter->symbol = symbol;
  52. return (true);
  53. }
  54. else if (++iter->idx >= size)
  55. {
  56. iter->symbol = NULL;
  57. return (false);
  58. }
  59. }
  60. }
  61. void
  62. symbol_iter_init (struct symbol_iter *iter)
  63. {
  64. iter->idx = 0;
  65. symbol_iter_adv (iter);
  66. }
  67. bool
  68. symbol_iter_next (struct symbol_iter *iter)
  69. {
  70. if (!symbol_iter_valid (iter))
  71. return (false);
  72. ++iter->idx;
  73. return (symbol_iter_adv (iter));
  74. }