offload_util.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. Copyright (c) 2014-2016 Intel Corporation. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Intel Corporation nor the names of its
  12. contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "offload_util.h"
  27. #include <errno.h>
  28. #include "liboffload_error_codes.h"
  29. #ifdef TARGET_WINNT
  30. void *thread_getspecific(pthread_key_t key)
  31. {
  32. if (key == 0) {
  33. return NULL;
  34. }
  35. else {
  36. return TlsGetValue(key);
  37. }
  38. }
  39. int thread_setspecific(pthread_key_t key, const void *value)
  40. {
  41. return (TlsSetValue(key, (LPVOID)value)) ? 0 : GetLastError();
  42. }
  43. #endif // TARGET_WINNT
  44. bool __offload_parse_size_string(const char *str, uint64_t &new_size)
  45. {
  46. uint64_t val;
  47. char *suffix;
  48. errno = 0;
  49. #ifdef TARGET_WINNT
  50. val = strtoul(str, &suffix, 10);
  51. #else // TARGET_WINNT
  52. val = strtoull(str, &suffix, 10);
  53. #endif // TARGET_WINNT
  54. if (errno != 0 || suffix == str) {
  55. return false;
  56. }
  57. if (suffix[0] == '\0') {
  58. // default is Kilobytes
  59. new_size = val * 1024;
  60. return true;
  61. }
  62. else if (suffix[1] == '\0') {
  63. // Optional suffixes: B (bytes), K (Kilobytes), M (Megabytes),
  64. // G (Gigabytes), or T (Terabytes) specify the units.
  65. switch (suffix[0]) {
  66. case 'b':
  67. case 'B':
  68. new_size = val;
  69. break;
  70. case 'k':
  71. case 'K':
  72. new_size = val * 1024;
  73. break;
  74. case 'm':
  75. case 'M':
  76. new_size = val * 1024 * 1024;
  77. break;
  78. case 'g':
  79. case 'G':
  80. new_size = val * 1024 * 1024 * 1024;
  81. break;
  82. case 't':
  83. case 'T':
  84. new_size = val * 1024 * 1024 * 1024 * 1024;
  85. break;
  86. default:
  87. return false;
  88. }
  89. return true;
  90. }
  91. return false;
  92. }
  93. bool __offload_parse_int_string(const char *str, int64_t &value)
  94. {
  95. int64_t val;
  96. char *suffix;
  97. errno = 0;
  98. #ifdef TARGET_WINNT
  99. val = strtol(str, &suffix, 0);
  100. #else
  101. val = strtoll(str, &suffix, 0);
  102. #endif
  103. if (errno == 0 && suffix != str && *suffix == '\0') {
  104. value = val;
  105. return true;
  106. }
  107. return false;
  108. }
  109. #ifdef TARGET_WINNT
  110. extern void* DL_open(const char *path)
  111. {
  112. void *handle;
  113. int error_mode;
  114. /*
  115. * do not display message box with error if it the call below fails to
  116. * load dynamic library.
  117. */
  118. error_mode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
  119. /* load dynamic library */
  120. handle = (void*) LoadLibrary(path);
  121. /* restore error mode */
  122. SetErrorMode(error_mode);
  123. return handle;
  124. }
  125. extern int DL_addr(const void *addr, Dl_info *dl_info)
  126. {
  127. MEMORY_BASIC_INFORMATION mem_info;
  128. char mod_name[MAX_PATH];
  129. HMODULE mod_handle;
  130. /* Fill MEMORY_BASIC_INFORMATION struct */
  131. if (!VirtualQuery(addr, &mem_info, sizeof(mem_info))) {
  132. return 0;
  133. }
  134. mod_handle = (HMODULE)mem_info.AllocationBase;
  135. /* ANSI file name for module */
  136. if (!GetModuleFileNameA(mod_handle, (char*) mod_name, sizeof(mod_name))) {
  137. return 0;
  138. }
  139. strcpy(dl_info->dli_fname, mod_name);
  140. dl_info->dli_fbase = mem_info.BaseAddress;
  141. dl_info->dli_saddr = addr;
  142. strcpy(dl_info->dli_sname, mod_name);
  143. return 1;
  144. }
  145. // Run once
  146. static BOOL CALLBACK __offload_run_once_wrapper(
  147. PINIT_ONCE initOnce,
  148. PVOID parameter,
  149. PVOID *context
  150. )
  151. {
  152. void (*init_routine)(void) = (void(*)(void)) parameter;
  153. init_routine();
  154. return true;
  155. }
  156. void __offload_run_once(OffloadOnceControl *ctrl, void (*func)(void))
  157. {
  158. InitOnceExecuteOnce(ctrl, __offload_run_once_wrapper, (void*) func, 0);
  159. }
  160. #endif // TARGET_WINNT
  161. /* ARGSUSED */ // version is not used on windows
  162. void* DL_sym(void *handle, const char *name, const char *version)
  163. {
  164. #ifdef TARGET_WINNT
  165. return GetProcAddress((HMODULE) handle, name);
  166. #else // TARGET_WINNT
  167. if (version == 0) {
  168. return dlsym(handle, name);
  169. }
  170. else {
  171. return dlvsym(handle, name, version);
  172. }
  173. #endif // TARGET_WINNT
  174. }
  175. int64_t get_el_value(
  176. char *base,
  177. int64_t offset,
  178. int64_t size)
  179. {
  180. int64_t val = 0;
  181. switch (size) {
  182. case 1:
  183. val = static_cast<int64_t>(*((char *)(base + offset)));
  184. break;
  185. case 2:
  186. val = static_cast<int64_t>(*((short *)(base + offset)));
  187. break;
  188. case 4:
  189. val = static_cast<int64_t>(*((int *)(base + offset)));
  190. break;
  191. default:
  192. val = *((int64_t *)(base + offset));
  193. break;
  194. }
  195. return val;
  196. }