elf_hashtable_entry.h 988 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #pragma once
  2. #include <stdint.h>
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. struct sym_entry {
  7. uint32_t hash;
  8. uint32_t address;
  9. };
  10. #ifdef __cplusplus
  11. }
  12. #include <array>
  13. #include <algorithm>
  14. #define API_METHOD(x, ret_type, args_type) \
  15. sym_entry { \
  16. .hash = elf_gnu_hash(#x), .address = (uint32_t)(static_cast<ret_type(*) args_type>(x)) \
  17. }
  18. #define API_VARIABLE(x, var_type) \
  19. sym_entry { \
  20. .hash = elf_gnu_hash(#x), .address = (uint32_t)(&(x)), \
  21. }
  22. constexpr bool operator<(const sym_entry& k1, const sym_entry& k2) {
  23. return k1.hash < k2.hash;
  24. }
  25. constexpr uint32_t elf_gnu_hash(const char* s) {
  26. uint32_t h = 0x1505;
  27. for(unsigned char c = *s; c != '\0'; c = *++s) {
  28. h = (h << 5) + h + c;
  29. }
  30. return h;
  31. }
  32. #endif