wasm_dlfcn.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "bh_platform.h"
  17. static bool sort_flag = false;
  18. typedef struct NativeSymbol {
  19. const char *symbol;
  20. void *func_ptr;
  21. } NativeSymbol;
  22. static bool
  23. sort_symbol_ptr(NativeSymbol *ptr, int len)
  24. {
  25. int i, j;
  26. NativeSymbol temp;
  27. for (i = 0; i < len - 1; ++i) {
  28. for (j = i + 1; j < len; ++j) {
  29. if (strcmp((ptr+i)->symbol, (ptr+j)->symbol) > 0) {
  30. temp = ptr[i];
  31. ptr[i] = ptr[j];
  32. ptr[j] = temp;
  33. }
  34. }
  35. }
  36. return true;
  37. }
  38. static void *
  39. lookup_symbol(NativeSymbol *ptr, int len, const char *symbol)
  40. {
  41. int low = 0, mid, ret;
  42. int high = len - 1;
  43. while (low <= high) {
  44. mid = (low + high) / 2;
  45. ret = strcmp(symbol, ptr[mid].symbol);
  46. if (ret == 0)
  47. return ptr[mid].func_ptr;
  48. else if (ret < 0)
  49. high = mid - 1;
  50. else
  51. low = mid + 1;
  52. }
  53. return NULL;
  54. }
  55. int
  56. get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);
  57. int
  58. get_ext_lib_export_apis(NativeSymbol **p_ext_lib_apis);
  59. static NativeSymbol *base_native_symbol_defs;
  60. static NativeSymbol *ext_native_symbol_defs;
  61. static int base_native_symbol_len;
  62. static int ext_native_symbol_len;
  63. void *
  64. wasm_dlsym(void *handle, const char *symbol)
  65. {
  66. void *ret;
  67. if (!sort_flag) {
  68. base_native_symbol_len = get_base_lib_export_apis(&base_native_symbol_defs);
  69. ext_native_symbol_len = get_ext_lib_export_apis(&ext_native_symbol_defs);
  70. if (base_native_symbol_len > 0)
  71. sort_symbol_ptr(base_native_symbol_defs, base_native_symbol_len);
  72. if (ext_native_symbol_len > 0)
  73. sort_symbol_ptr(ext_native_symbol_defs, ext_native_symbol_len);
  74. sort_flag = true;
  75. }
  76. if (!symbol)
  77. return NULL;
  78. if ((ret = lookup_symbol(base_native_symbol_defs, base_native_symbol_len,
  79. symbol))
  80. || (ret = lookup_symbol(ext_native_symbol_defs, ext_native_symbol_len,
  81. symbol)))
  82. return ret;
  83. return NULL;
  84. }