symbol.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. const struct symbol *table, *symbol;
  32. uintptr_t start, end;
  33. size_t size;
  34. table = symbol_table_ptr;
  35. size = symbol_table_size;
  36. for (size_t i = 0; i < size; i++) {
  37. symbol = &table[i];
  38. if (!symbol->name || (symbol->size == 0)) {
  39. continue;
  40. }
  41. start = symbol->addr;
  42. end = symbol->addr + symbol->size;
  43. if ((addr >= start) && (addr < end)) {
  44. return symbol;
  45. }
  46. }
  47. return NULL;
  48. }