aot_loader.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_runtime.h"
  6. #include "bh_common.h"
  7. #include "bh_memory.h"
  8. #include "bh_log.h"
  9. #include "../common/wasm_runtime_common.h"
  10. #include "../common/wasm_native.h"
  11. #include "../compilation/aot.h"
  12. #if WASM_ENABLE_JIT != 0
  13. #include "../compilation/aot_llvm.h"
  14. #include "../interpreter/wasm_loader.h"
  15. #endif
  16. static void
  17. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  18. {
  19. if (error_buf != NULL)
  20. snprintf(error_buf, error_buf_size, "%s", string);
  21. }
  22. #define exchange_uint8(p_data) (void)0
  23. static void
  24. exchange_uint16(uint8 *p_data)
  25. {
  26. uint8 value = *p_data;
  27. *p_data = *(p_data + 1);
  28. *(p_data + 1) = value;
  29. }
  30. static void
  31. exchange_uint32(uint8 *p_data)
  32. {
  33. uint8 value = *p_data;
  34. *p_data = *(p_data + 3);
  35. *(p_data + 3) = value;
  36. value = *(p_data + 1);
  37. *(p_data + 1) = *(p_data + 2);
  38. *(p_data + 2) = value;
  39. }
  40. static void
  41. exchange_uint64(uint8 *pData)
  42. {
  43. exchange_uint32(pData);
  44. exchange_uint32(pData + 4);
  45. }
  46. static union {
  47. int a;
  48. char b;
  49. } __ue = { .a = 1 };
  50. #define is_little_endian() (__ue.b == 1)
  51. #define CHECK_BUF(buf, buf_end, length) do { \
  52. if (buf + length > buf_end) { \
  53. set_error_buf(error_buf, error_buf_size, \
  54. "Read data failed: unexpected end."); \
  55. goto fail; \
  56. } \
  57. } while (0)
  58. static uint8*
  59. align_ptr(const uint8 *p, uint32 b)
  60. {
  61. uintptr_t v = (uintptr_t)p;
  62. uintptr_t m = b - 1;
  63. return (uint8*)((v + m) & ~m);
  64. }
  65. static inline uint64
  66. GET_U64_FROM_ADDR(uint32 *addr)
  67. {
  68. union { uint64 val; uint32 parts[2]; } u;
  69. u.parts[0] = addr[0];
  70. u.parts[1] = addr[1];
  71. return u.val;
  72. }
  73. #define TEMPLATE_READ(p, p_end, res, type) do { \
  74. if (sizeof(type) != sizeof(uint64)) \
  75. p = (uint8*)align_ptr(p, sizeof(type)); \
  76. else \
  77. /* align 4 bytes if type is uint64 */ \
  78. p = (uint8*)align_ptr(p, sizeof(uint32)); \
  79. CHECK_BUF(p, p_end, sizeof(type)); \
  80. if (sizeof(type) != sizeof(uint64)) \
  81. res = *(type*)p; \
  82. else \
  83. res = (type)GET_U64_FROM_ADDR((uint32*)p); \
  84. if (!is_little_endian()) \
  85. exchange_##type((uint8*)&res); \
  86. p += sizeof(type); \
  87. } while (0)
  88. #define read_uint8(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint8)
  89. #define read_uint16(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint16)
  90. #define read_uint32(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint32)
  91. #define read_uint64(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint64)
  92. #define read_byte_array(p, p_end, addr, len) do { \
  93. CHECK_BUF(p, p_end, len); \
  94. memcpy(addr, p, len); \
  95. p += len; \
  96. } while (0)
  97. #define read_string(p, p_end, str) do { \
  98. uint16 str_len; \
  99. read_uint16(p, p_end, str_len); \
  100. CHECK_BUF(p, p_end, str_len); \
  101. if (!(str = const_str_set_insert \
  102. (p, str_len, module, \
  103. error_buf, error_buf_size))) { \
  104. goto fail; \
  105. } \
  106. p += str_len; \
  107. } while (0)
  108. /* Legal values for bin_type */
  109. #define BIN_TYPE_ELF32L 0 /* 32-bit little endian */
  110. #define BIN_TYPE_ELF32B 1 /* 32-bit big endian */
  111. #define BIN_TYPE_ELF64L 2 /* 64-bit little endian */
  112. #define BIN_TYPE_ELF64B 3 /* 64-bit big endian */
  113. /* Legal values for e_type (object file type). */
  114. #define E_TYPE_NONE 0 /* No file type */
  115. #define E_TYPE_REL 1 /* Relocatable file */
  116. #define E_TYPE_EXEC 2 /* Executable file */
  117. #define E_TYPE_DYN 3 /* Shared object file */
  118. /* Legal values for e_machine (architecture). */
  119. #define E_MACHINE_386 3 /* Intel 80386 */
  120. #define E_MACHINE_MIPS 8 /* MIPS R3000 big-endian */
  121. #define E_MACHINE_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */
  122. #define E_MACHINE_ARM 40 /* ARM/Thumb */
  123. #define E_MACHINE_ARC 45 /* Argonaut RISC Core */
  124. #define E_MACHINE_IA_64 50 /* Intel Merced */
  125. #define E_MACHINE_MIPS_X 51 /* Stanford MIPS-X */
  126. #define E_MACHINE_X86_64 62 /* AMD x86-64 architecture */
  127. #define E_MACHINE_XTENSA 94 /* Tensilica Xtensa Architecture */
  128. /* Legal values for e_version */
  129. #define E_VERSION_CURRENT 1 /* Current version */
  130. static char*
  131. const_str_set_insert(const uint8 *str, int32 len, AOTModule *module,
  132. char* error_buf, uint32 error_buf_size)
  133. {
  134. HashMap *set = module->const_str_set;
  135. char *c_str = wasm_malloc((uint32)len + 1), *value;
  136. if (!c_str) {
  137. set_error_buf(error_buf, error_buf_size,
  138. "AOT module load failed: "
  139. "allocate memory failed.");
  140. return NULL;
  141. }
  142. bh_memcpy_s(c_str, (uint32)(len + 1), str, (uint32)len);
  143. c_str[len] = '\0';
  144. if ((value = bh_hash_map_find(set, c_str))) {
  145. wasm_free(c_str);
  146. return value;
  147. }
  148. if (!bh_hash_map_insert(set, c_str, c_str)) {
  149. set_error_buf(error_buf, error_buf_size,
  150. "AOT module load failed: "
  151. "insert string to hash map failed.");
  152. wasm_free(c_str);
  153. return NULL;
  154. }
  155. return c_str;
  156. }
  157. static void
  158. get_current_target(char *target_buf, uint32 target_buf_size)
  159. {
  160. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  161. snprintf(target_buf, target_buf_size, "x86_64");
  162. #elif defined(BUILD_TARGET_X86_32)
  163. snprintf(target_buf, target_buf_size, "i386");
  164. #elif defined(BUILD_TARGET_ARM) \
  165. || defined(BUILD_TARGET_ARM_VFP) \
  166. || defined(BUILD_TARGET_THUMB) \
  167. || defined(BUILD_TARGET_THUMB_VFP)
  168. char *build_target = BUILD_TARGET;
  169. char *p = target_buf, *p_end;
  170. snprintf(target_buf, target_buf_size, "%s", build_target);
  171. p_end = p + strlen(target_buf);
  172. while (p < p_end) {
  173. if (*p >= 'A' && *p <= 'Z')
  174. *p++ += 'a' - 'A';
  175. else
  176. p++;
  177. }
  178. if (!strcmp(target_buf, "arm"))
  179. snprintf(target_buf, target_buf_size, "armv4");
  180. else if (!strcmp(target_buf, "thumb"))
  181. snprintf(target_buf, target_buf_size, "thumbv4t");
  182. #elif defined(BUILD_TARGET_MIPS)
  183. snprintf(target_buf, target_buf_size, "mips");
  184. #elif defined(BUILD_TARGET_XTENSA)
  185. snprintf(target_buf, target_buf_size, "xtensa");
  186. #endif
  187. }
  188. static bool
  189. get_aot_file_target(AOTTargetInfo *target_info,
  190. char *target_buf, uint32 target_buf_size,
  191. char *error_buf, uint32 error_buf_size)
  192. {
  193. char *machine_type = NULL;
  194. switch (target_info->e_machine) {
  195. case E_MACHINE_X86_64:
  196. machine_type = "x86_64";
  197. break;
  198. case E_MACHINE_386:
  199. machine_type = "i386";
  200. break;
  201. case E_MACHINE_ARM:
  202. machine_type = target_info->arch;
  203. break;
  204. case E_MACHINE_MIPS:
  205. machine_type = "mips";
  206. break;
  207. case E_MACHINE_XTENSA:
  208. machine_type = "xtensa";
  209. break;
  210. default:
  211. if (error_buf)
  212. snprintf(error_buf, error_buf_size,
  213. "AOT module load failed: unknown machine type %d.",
  214. target_info->e_machine);
  215. return false;
  216. }
  217. if (strncmp(target_info->arch, machine_type, strlen(machine_type))) {
  218. if (error_buf)
  219. snprintf(error_buf, error_buf_size,
  220. "AOT module load failed: "
  221. "machine type (%s) isn't consistent with target type (%s).",
  222. machine_type, target_info->arch);
  223. return false;
  224. }
  225. snprintf(target_buf, target_buf_size, "%s", target_info->arch);
  226. return true;
  227. }
  228. static bool
  229. check_machine_info(AOTTargetInfo *target_info,
  230. char *error_buf, uint32 error_buf_size)
  231. {
  232. char target_expected[32], target_got[32];
  233. get_current_target(target_expected, sizeof(target_expected));
  234. if (!get_aot_file_target(target_info, target_got, sizeof(target_got),
  235. error_buf, error_buf_size))
  236. return false;
  237. if (strcmp(target_expected, target_got)) {
  238. if (error_buf) {
  239. snprintf(error_buf, error_buf_size,
  240. "AOT module load failed: invalid target type, "
  241. "expected %s but got %s.",
  242. target_expected, target_got);
  243. }
  244. return false;
  245. }
  246. return true;
  247. }
  248. static bool
  249. load_target_info_section(const uint8 *buf, const uint8 *buf_end,
  250. AOTModule *module,
  251. char *error_buf, uint32 error_buf_size)
  252. {
  253. AOTTargetInfo target_info;
  254. const uint8 *p = buf, *p_end = buf_end;
  255. bool is_target_little_endian, is_target_64_bit;
  256. read_uint16(p, p_end, target_info.bin_type);
  257. read_uint16(p, p_end, target_info.abi_type);
  258. read_uint16(p, p_end, target_info.e_type);
  259. read_uint16(p, p_end, target_info.e_machine);
  260. read_uint32(p, p_end, target_info.e_version);
  261. read_uint32(p, p_end, target_info.e_flags);
  262. read_uint32(p, p_end, target_info.reserved);
  263. read_byte_array(p, p_end,
  264. target_info.arch, sizeof(target_info.arch));
  265. if (p != buf_end) {
  266. set_error_buf(error_buf, error_buf_size,
  267. "AOT module load failed: invalid section size.");
  268. return false;
  269. }
  270. /* Check target endian type */
  271. is_target_little_endian = target_info.bin_type & 1 ? false : true;
  272. if (is_little_endian() != is_target_little_endian) {
  273. if (error_buf)
  274. snprintf(error_buf, error_buf_size,
  275. "AOT module load failed: "
  276. "invalid target endian type, expected %s but got %s.",
  277. is_little_endian() ? "little endian" : "big endian",
  278. is_target_little_endian ? "little endian" : "big endian");
  279. return false;
  280. }
  281. /* Check target bit width */
  282. is_target_64_bit = target_info.bin_type & 2 ? true : false;
  283. if ((sizeof(void*) == 8 ? true : false) != is_target_64_bit) {
  284. if (error_buf)
  285. snprintf(error_buf, error_buf_size,
  286. "AOT module load failed: "
  287. "invalid target bit width, expected %s but got %s.",
  288. sizeof(void*) == 8 ? "64-bit" : "32-bit",
  289. is_target_64_bit ? "64-bit" : "32-bit");
  290. return false;
  291. }
  292. /* Check target elf file type */
  293. if (target_info.e_type != E_TYPE_REL) {
  294. set_error_buf(error_buf, error_buf_size,
  295. "AOT module load failed: invalid object file type, "
  296. "expected relocatable file type but got others.");
  297. return false;
  298. }
  299. /* Check machine info */
  300. if (!check_machine_info(&target_info, error_buf, error_buf_size)) {
  301. return false;
  302. }
  303. if (target_info.e_version != E_VERSION_CURRENT) {
  304. set_error_buf(error_buf, error_buf_size,
  305. "AOT module load failed: invalid elf file version.");
  306. return false;
  307. }
  308. return true;
  309. fail:
  310. return false;
  311. }
  312. static void
  313. destroy_mem_init_data_list(AOTMemInitData **data_list, uint32 count,
  314. bool is_jit_mode)
  315. {
  316. if (!is_jit_mode) {
  317. uint32 i;
  318. for (i = 0; i < count; i++)
  319. if (data_list[i])
  320. wasm_free(data_list[i]);
  321. wasm_free(data_list);
  322. }
  323. }
  324. static bool
  325. load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
  326. AOTModule *module,
  327. char *error_buf, uint32 error_buf_size)
  328. {
  329. const uint8 *buf = *p_buf;
  330. AOTMemInitData **data_list;
  331. uint64 size;
  332. uint32 i;
  333. /* Allocate memory */
  334. size = sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count;
  335. if (size >= UINT32_MAX
  336. || !(module->mem_init_data_list =
  337. data_list = wasm_malloc((uint32)size))) {
  338. set_error_buf(error_buf, error_buf_size,
  339. "AOT module load failed: "
  340. "allocate memory failed.");
  341. return false;
  342. }
  343. memset(data_list, 0, size);
  344. /* Create each memory data segment */
  345. for (i = 0; i < module->mem_init_data_count; i++) {
  346. uint32 init_expr_type, byte_count;
  347. uint64 init_expr_value;
  348. read_uint32(buf, buf_end, init_expr_type);
  349. read_uint64(buf, buf_end, init_expr_value);
  350. read_uint32(buf, buf_end, byte_count);
  351. size = offsetof(AOTMemInitData, bytes) + (uint64)byte_count;
  352. if (size >= UINT32_MAX
  353. || !(data_list[i] = wasm_malloc((uint32)size))) {
  354. set_error_buf(error_buf, error_buf_size,
  355. "AOT module load failed: "
  356. "allocate memory failed.");
  357. return false;
  358. }
  359. data_list[i]->offset.init_expr_type = (uint8)init_expr_type;
  360. data_list[i]->offset.u.i64 = (int64)init_expr_value;
  361. data_list[i]->byte_count = byte_count;
  362. read_byte_array(buf, buf_end,
  363. data_list[i]->bytes, data_list[i]->byte_count);
  364. }
  365. *p_buf = buf;
  366. return true;
  367. fail:
  368. return false;
  369. }
  370. static bool
  371. load_memory_info(const uint8 **p_buf, const uint8 *buf_end,
  372. AOTModule *module,
  373. char *error_buf, uint32 error_buf_size)
  374. {
  375. const uint8 *buf = *p_buf;
  376. read_uint32(buf, buf_end, module->num_bytes_per_page);
  377. read_uint32(buf, buf_end, module->mem_init_page_count);
  378. read_uint32(buf, buf_end, module->mem_max_page_count);
  379. read_uint32(buf, buf_end, module->mem_init_data_count);
  380. /* load memory init data list */
  381. if (module->mem_init_data_count > 0
  382. && !load_mem_init_data_list(&buf, buf_end, module,
  383. error_buf, error_buf_size))
  384. return false;
  385. *p_buf = buf;
  386. return true;
  387. fail:
  388. return false;
  389. }
  390. static void
  391. destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count,
  392. bool is_jit_mode)
  393. {
  394. if (!is_jit_mode) {
  395. uint32 i;
  396. for (i = 0; i < count; i++)
  397. if (data_list[i])
  398. wasm_free(data_list[i]);
  399. wasm_free(data_list);
  400. }
  401. }
  402. static bool
  403. load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
  404. AOTModule *module,
  405. char *error_buf, uint32 error_buf_size)
  406. {
  407. const uint8 *buf = *p_buf;
  408. AOTTableInitData **data_list;
  409. uint64 size;
  410. uint32 i;
  411. /* Allocate memory */
  412. size = sizeof(AOTTableInitData *) * (uint64)module->table_init_data_count;
  413. if (size >= UINT32_MAX
  414. || !(module->table_init_data_list =
  415. data_list = wasm_malloc((uint32)size))) {
  416. set_error_buf(error_buf, error_buf_size,
  417. "AOT module load failed: "
  418. "allocate memory failed.");
  419. return false;
  420. }
  421. memset(data_list, 0, size);
  422. /* Create each table data segment */
  423. for (i = 0; i < module->table_init_data_count; i++) {
  424. uint32 init_expr_type, func_index_count;
  425. uint64 init_expr_value, size1;
  426. read_uint32(buf, buf_end, init_expr_type);
  427. read_uint64(buf, buf_end, init_expr_value);
  428. read_uint32(buf, buf_end, func_index_count);
  429. size1 = sizeof(uint32) * (uint64)func_index_count;
  430. size = offsetof(AOTTableInitData, func_indexes) + size1;
  431. if (size >= UINT32_MAX
  432. || !(data_list[i] = wasm_malloc((uint32)size))) {
  433. set_error_buf(error_buf, error_buf_size,
  434. "AOT module load failed: "
  435. "allocate memory failed.");
  436. return false;
  437. }
  438. data_list[i]->offset.init_expr_type = (uint8)init_expr_type;
  439. data_list[i]->offset.u.i64 = (int64)init_expr_value;
  440. data_list[i]->func_index_count = func_index_count;
  441. read_byte_array(buf, buf_end, data_list[i]->func_indexes, size1);
  442. }
  443. *p_buf = buf;
  444. return true;
  445. fail:
  446. return false;
  447. }
  448. static bool
  449. load_table_info(const uint8 **p_buf, const uint8 *buf_end,
  450. AOTModule *module,
  451. char *error_buf, uint32 error_buf_size)
  452. {
  453. const uint8 *buf = *p_buf;
  454. read_uint32(buf, buf_end, module->table_size);
  455. read_uint32(buf, buf_end, module->table_init_data_count);
  456. /* load table init data list */
  457. if (module->table_init_data_count > 0
  458. && !load_table_init_data_list(&buf, buf_end, module,
  459. error_buf, error_buf_size))
  460. return false;
  461. *p_buf = buf;
  462. return true;
  463. fail:
  464. return false;
  465. }
  466. static void
  467. destroy_func_types(AOTFuncType **func_types, uint32 count, bool is_jit_mode)
  468. {
  469. if (!is_jit_mode) {
  470. uint32 i;
  471. for (i = 0; i < count; i++)
  472. if (func_types[i])
  473. wasm_free(func_types[i]);
  474. wasm_free(func_types);
  475. }
  476. }
  477. static bool
  478. load_func_types(const uint8 **p_buf, const uint8 *buf_end,
  479. AOTModule *module,
  480. char *error_buf, uint32 error_buf_size)
  481. {
  482. const uint8 *buf = *p_buf;
  483. AOTFuncType **func_types;
  484. uint64 size;
  485. uint32 i;
  486. /* Allocate memory */
  487. size = sizeof(AOTFuncType *) * (uint64)module->func_type_count;
  488. if (size >= UINT32_MAX
  489. || !(module->func_types = func_types = wasm_malloc((uint32)size))) {
  490. set_error_buf(error_buf, error_buf_size,
  491. "AOT module load failed: "
  492. "allocate memory failed.");
  493. return false;
  494. }
  495. memset(func_types, 0, size);
  496. /* Create each function type */
  497. for (i = 0; i < module->func_type_count; i++) {
  498. uint32 param_count, result_count;
  499. uint64 size1;
  500. read_uint32(buf, buf_end, param_count);
  501. read_uint32(buf, buf_end, result_count);
  502. size1 = (uint64)param_count + (uint64)result_count;
  503. size = offsetof(AOTFuncType, types) + size1;
  504. if (size >= UINT32_MAX
  505. || !(func_types[i] = wasm_malloc((uint32)size))) {
  506. set_error_buf(error_buf, error_buf_size,
  507. "AOT module load failed: "
  508. "allocate memory failed.");
  509. return false;
  510. }
  511. func_types[i]->param_count = param_count;
  512. func_types[i]->result_count = result_count;
  513. read_byte_array(buf, buf_end, func_types[i]->types, (uint32)size1);
  514. }
  515. *p_buf = buf;
  516. return true;
  517. fail:
  518. return false;
  519. }
  520. static bool
  521. load_func_type_info(const uint8 **p_buf, const uint8 *buf_end,
  522. AOTModule *module,
  523. char *error_buf, uint32 error_buf_size)
  524. {
  525. const uint8 *buf = *p_buf;
  526. read_uint32(buf, buf_end, module->func_type_count);
  527. /* load function type */
  528. if (module->func_type_count > 0
  529. && !load_func_types(&buf, buf_end, module, error_buf, error_buf_size))
  530. return false;
  531. *p_buf = buf;
  532. return true;
  533. fail:
  534. return false;
  535. }
  536. static void
  537. destroy_import_globals(AOTImportGlobal *import_globals, bool is_jit_mode)
  538. {
  539. if (!is_jit_mode)
  540. wasm_free(import_globals);
  541. }
  542. static bool
  543. load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
  544. AOTModule *module,
  545. char *error_buf, uint32 error_buf_size)
  546. {
  547. const uint8 *buf = *p_buf;
  548. AOTImportGlobal *import_globals;
  549. uint64 size;
  550. uint32 i, data_offset = 0;
  551. /* Allocate memory */
  552. size = sizeof(AOTImportGlobal) * (uint64)module->import_global_count;
  553. if (size >= UINT32_MAX
  554. || !(module->import_globals =
  555. import_globals = wasm_malloc((uint32)size))) {
  556. set_error_buf(error_buf, error_buf_size,
  557. "AOT module load failed: "
  558. "allocate memory failed.");
  559. return false;
  560. }
  561. memset(import_globals, 0, size);
  562. /* Create each import global */
  563. for (i = 0; i < module->import_global_count; i++) {
  564. read_uint8(buf, buf_end, import_globals[i].type);
  565. read_uint8(buf, buf_end, import_globals[i].is_mutable);
  566. read_string(buf, buf_end, import_globals[i].module_name);
  567. read_string(buf, buf_end, import_globals[i].global_name);
  568. import_globals[i].size = wasm_value_type_size(import_globals[i].type);
  569. import_globals[i].data_offset = data_offset;
  570. data_offset += import_globals[i].size;
  571. module->global_data_size += import_globals[i].size;
  572. }
  573. *p_buf = buf;
  574. return true;
  575. fail:
  576. return false;
  577. }
  578. static bool
  579. load_import_global_info(const uint8 **p_buf, const uint8 *buf_end,
  580. AOTModule *module,
  581. char *error_buf, uint32 error_buf_size)
  582. {
  583. const uint8 *buf = *p_buf;
  584. read_uint32(buf, buf_end, module->import_global_count);
  585. /* load import globals */
  586. if (module->import_global_count > 0
  587. && !load_import_globals(&buf, buf_end, module,
  588. error_buf, error_buf_size))
  589. return false;
  590. *p_buf = buf;
  591. return true;
  592. fail:
  593. return false;
  594. }
  595. static void
  596. destroy_globals(AOTGlobal *globals, bool is_jit_mode)
  597. {
  598. if (!is_jit_mode)
  599. wasm_free(globals);
  600. }
  601. static bool
  602. load_globals(const uint8 **p_buf, const uint8 *buf_end,
  603. AOTModule *module,
  604. char *error_buf, uint32 error_buf_size)
  605. {
  606. const uint8 *buf = *p_buf;
  607. AOTGlobal *globals;
  608. uint64 size;
  609. uint32 i, data_offset = 0;
  610. AOTImportGlobal *last_import_global;
  611. /* Allocate memory */
  612. size = sizeof(AOTGlobal) * (uint64)module->global_count;
  613. if (size >= UINT32_MAX
  614. || !(module->globals = globals = wasm_malloc((uint32)size))) {
  615. set_error_buf(error_buf, error_buf_size,
  616. "AOT module load failed: "
  617. "allocate memory failed.");
  618. return false;
  619. }
  620. memset(globals, 0, size);
  621. if (module->import_global_count > 0) {
  622. last_import_global =
  623. &module->import_globals[module->import_global_count - 1];
  624. data_offset = last_import_global->data_offset
  625. + last_import_global->size;
  626. }
  627. /* Create each global */
  628. for (i = 0; i < module->global_count; i++) {
  629. uint16 init_expr_type;
  630. uint64 init_expr_value;
  631. read_uint8(buf, buf_end, globals[i].type);
  632. read_uint8(buf, buf_end, globals[i].is_mutable);
  633. read_uint16(buf, buf_end, init_expr_type);
  634. read_uint64(buf, buf_end, init_expr_value);
  635. globals[i].init_expr.init_expr_type = (uint8)init_expr_type;
  636. globals[i].init_expr.u.i64 = (int64)init_expr_value;
  637. globals[i].size = wasm_value_type_size(globals[i].type);
  638. globals[i].data_offset = data_offset;
  639. data_offset += globals[i].size;
  640. module->global_data_size += globals[i].size;
  641. }
  642. *p_buf = buf;
  643. return true;
  644. fail:
  645. return false;
  646. }
  647. static bool
  648. load_global_info(const uint8 **p_buf, const uint8 *buf_end,
  649. AOTModule *module,
  650. char *error_buf, uint32 error_buf_size)
  651. {
  652. const uint8 *buf = *p_buf;
  653. read_uint32(buf, buf_end, module->global_count);
  654. /* load globals */
  655. if (module->global_count > 0
  656. && !load_globals(&buf, buf_end, module, error_buf, error_buf_size))
  657. return false;
  658. *p_buf = buf;
  659. return true;
  660. fail:
  661. return false;
  662. }
  663. static void
  664. destroy_import_funcs(AOTImportFunc *import_funcs, bool is_jit_mode)
  665. {
  666. if (!is_jit_mode)
  667. wasm_free(import_funcs);
  668. }
  669. static void*
  670. resolve_sym(const char *module_name, const char *field_name)
  671. {
  672. void *sym;
  673. #if WASM_ENABLE_LIBC_BUILTIN != 0
  674. if ((sym = wasm_native_lookup_libc_builtin_func(module_name,
  675. field_name)))
  676. return sym;
  677. #endif
  678. #if WASM_ENABLE_LIBC_WASI != 0
  679. if ((sym = wasm_native_lookup_libc_wasi_func(module_name,
  680. field_name)))
  681. return sym;
  682. #endif
  683. #if WASM_ENABLE_BASE_LIB != 0
  684. if ((sym = wasm_native_lookup_base_lib_func(module_name,
  685. field_name)))
  686. return sym;
  687. #endif
  688. if ((sym = wasm_native_lookup_extension_lib_func(module_name,
  689. field_name)))
  690. return sym;
  691. return NULL;
  692. }
  693. static bool
  694. load_import_funcs(const uint8 **p_buf, const uint8 *buf_end,
  695. AOTModule *module,
  696. char *error_buf, uint32 error_buf_size)
  697. {
  698. const char *module_name, *field_name;
  699. const uint8 *buf = *p_buf;
  700. AOTImportFunc *import_funcs;
  701. uint64 size;
  702. uint32 i;
  703. /* Allocate memory */
  704. size = sizeof(AOTImportFunc) * (uint64)module->import_func_count;
  705. if (size >= UINT32_MAX
  706. || !(module->import_funcs =
  707. import_funcs = wasm_malloc((uint32)size))) {
  708. set_error_buf(error_buf, error_buf_size,
  709. "AOT module load failed: "
  710. "allocate memory failed.");
  711. return false;
  712. }
  713. memset(import_funcs, 0, size);
  714. /* Create each import func */
  715. for (i = 0; i < module->import_func_count; i++) {
  716. read_uint16(buf, buf_end, import_funcs[i].func_type_index);
  717. if (import_funcs[i].func_type_index >= module->func_type_count) {
  718. set_error_buf(error_buf, error_buf_size,
  719. "AOT module load failed: "
  720. "invalid function type index.");
  721. return false;
  722. }
  723. read_string(buf, buf_end, import_funcs[i].module_name);
  724. read_string(buf, buf_end, import_funcs[i].func_name);
  725. module_name = import_funcs[i].module_name;
  726. field_name = import_funcs[i].func_name;
  727. if (!(import_funcs[i].func_ptr_linked =
  728. resolve_sym(module_name, field_name))) {
  729. LOG_WARNING("warning: fail to link import function (%s, %s)\n",
  730. module_name, field_name);
  731. }
  732. #if WASM_ENABLE_LIBC_WASI != 0
  733. if (!strcmp(import_funcs[i].module_name, "wasi_unstable"))
  734. module->is_wasi_module = true;
  735. #endif
  736. }
  737. *p_buf = buf;
  738. return true;
  739. fail:
  740. return false;
  741. }
  742. static bool
  743. load_import_func_info(const uint8 **p_buf, const uint8 *buf_end,
  744. AOTModule *module,
  745. char *error_buf, uint32 error_buf_size)
  746. {
  747. const uint8 *buf = *p_buf;
  748. read_uint32(buf, buf_end, module->import_func_count);
  749. /* load import funcs */
  750. if (module->import_func_count > 0
  751. && !load_import_funcs(&buf, buf_end, module,
  752. error_buf, error_buf_size))
  753. return false;
  754. *p_buf = buf;
  755. return true;
  756. fail:
  757. return false;
  758. }
  759. static void
  760. destroy_object_data_sections(AOTObjectDataSection *data_sections,
  761. uint32 data_section_count)
  762. {
  763. uint32 i;
  764. AOTObjectDataSection *data_section = data_sections;
  765. for (i = 0; i < data_section_count; i++, data_section++)
  766. if (data_section->data)
  767. bh_munmap(data_section->data, data_section->size);
  768. wasm_free(data_sections);
  769. }
  770. static bool
  771. load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,
  772. AOTModule *module,
  773. char *error_buf, uint32 error_buf_size)
  774. {
  775. const uint8 *buf = *p_buf;
  776. AOTObjectDataSection *data_sections;
  777. uint64 size;
  778. uint32 i;
  779. /* Allocate memory */
  780. size = sizeof(AOTObjectDataSection) * (uint64)module->data_section_count;
  781. if (size >= UINT32_MAX
  782. || !(module->data_sections =
  783. data_sections = wasm_malloc((uint32)size))) {
  784. set_error_buf(error_buf, error_buf_size,
  785. "AOT module load failed: "
  786. "allocate memory failed.");
  787. return false;
  788. }
  789. memset(data_sections, 0, size);
  790. /* Create each data section */
  791. for (i = 0; i < module->data_section_count; i++) {
  792. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE;
  793. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  794. /* aot code and data in x86_64 must be in range 0 to 2G due to
  795. relocation for R_X86_64_32/32S/PC32 */
  796. int map_flags = MMAP_MAP_32BIT;
  797. #else
  798. int map_flags = MMAP_MAP_NONE;
  799. #endif
  800. read_string(buf, buf_end, data_sections[i].name);
  801. read_uint32(buf, buf_end, data_sections[i].size);
  802. /* Allocate memory for data */
  803. if (!(data_sections[i].data =
  804. bh_mmap(NULL, data_sections[i].size, map_prot, map_flags))) {
  805. set_error_buf(error_buf, error_buf_size,
  806. "AOT module load failed: "
  807. "allocate memory failed.");
  808. return false;
  809. }
  810. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  811. /* address must be in the first 2 Gigabytes of
  812. the process address space */
  813. bh_assert((uintptr_t)data_sections[i].data < INT32_MAX);
  814. #endif
  815. read_byte_array(buf, buf_end,
  816. data_sections[i].data, data_sections[i].size);
  817. }
  818. *p_buf = buf;
  819. return true;
  820. fail:
  821. return false;
  822. }
  823. static bool
  824. load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end,
  825. AOTModule *module,
  826. char *error_buf, uint32 error_buf_size)
  827. {
  828. const uint8 *buf = *p_buf;
  829. read_uint32(buf, buf_end, module->data_section_count);
  830. /* load object data sections */
  831. if (module->data_section_count > 0
  832. && !load_object_data_sections(&buf, buf_end, module,
  833. error_buf, error_buf_size))
  834. return false;
  835. *p_buf = buf;
  836. return true;
  837. fail:
  838. return false;
  839. }
  840. static bool
  841. load_init_data_section(const uint8 *buf, const uint8 *buf_end,
  842. AOTModule *module,
  843. char *error_buf, uint32 error_buf_size)
  844. {
  845. const uint8 *p = buf, *p_end = buf_end;
  846. if (!load_memory_info(&p, p_end, module, error_buf, error_buf_size)
  847. || !load_table_info(&p, p_end, module, error_buf, error_buf_size)
  848. || !load_func_type_info(&p, p_end, module, error_buf, error_buf_size)
  849. || !load_import_global_info(&p, p_end, module, error_buf, error_buf_size)
  850. || !load_global_info(&p, p_end, module, error_buf, error_buf_size)
  851. || !load_import_func_info(&p, p_end, module, error_buf, error_buf_size))
  852. return false;
  853. /* load function count and start function index */
  854. read_uint32(p, p_end, module->func_count);
  855. read_uint32(p, p_end, module->start_func_index);
  856. /* check start function index */
  857. if (module->start_func_index != (uint32)-1
  858. && (module->start_func_index < module->import_func_count
  859. || module->start_func_index >= module->import_func_count
  860. + module->func_count)) {
  861. set_error_buf(error_buf, error_buf_size,
  862. "AOT module load failed: "
  863. "invalid start function index");
  864. return false;
  865. }
  866. read_uint32(p, p_end, module->llvm_aux_data_end);
  867. read_uint32(p, p_end, module->llvm_aux_stack_bottom);
  868. read_uint32(p, p_end, module->llvm_aux_stack_size);
  869. read_uint32(p, p_end, module->llvm_aux_stack_global_index);
  870. if (!load_object_data_sections_info(&p, p_end, module,
  871. error_buf, error_buf_size))
  872. return false;
  873. if (p != p_end) {
  874. set_error_buf(error_buf, error_buf_size,
  875. "AOT module load failed: "
  876. "invalid init data section size");
  877. return false;
  878. }
  879. return true;
  880. fail:
  881. return false;
  882. }
  883. static uint32
  884. get_plt_item_size();
  885. static uint32
  886. get_plt_table_size();
  887. static void
  888. init_plt_table(uint8 *plt);
  889. static bool
  890. load_text_section(const uint8 *buf, const uint8 *buf_end,
  891. AOTModule *module,
  892. char *error_buf, uint32 error_buf_size)
  893. {
  894. uint8 *plt_base;
  895. if (module->func_count > 0 && buf_end == buf) {
  896. set_error_buf(error_buf, error_buf_size,
  897. "AOT module load failed: invalid code size.");
  898. return false;
  899. }
  900. module->code = (void*)buf;
  901. module->code_size = (uint32)(buf_end - buf);
  902. if (module->code_size > 0) {
  903. plt_base = (uint8*)buf_end - get_plt_table_size();
  904. init_plt_table(plt_base);
  905. }
  906. return true;
  907. }
  908. static bool
  909. load_function_section(const uint8 *buf, const uint8 *buf_end,
  910. AOTModule *module,
  911. char *error_buf, uint32 error_buf_size)
  912. {
  913. const uint8 *p = buf, *p_end = buf_end;
  914. uint32 i;
  915. uint64 size, text_offset;
  916. size = sizeof(void*) * (uint64)module->func_count;
  917. if (size >= UINT32_MAX
  918. || !(module->func_ptrs = wasm_malloc((uint32)size))) {
  919. set_error_buf(error_buf, error_buf_size,
  920. "AOT module load failed: allocate memory failed.");
  921. return false;
  922. }
  923. for (i = 0; i < module->func_count; i++) {
  924. if (sizeof(void*) == 8) {
  925. read_uint64(p, p_end, text_offset);
  926. }
  927. else {
  928. uint32 text_offset32;
  929. read_uint32(p, p_end, text_offset32);
  930. text_offset = text_offset32;
  931. }
  932. if (text_offset >= module->code_size) {
  933. set_error_buf(error_buf, error_buf_size,
  934. "AOT module load failed: "
  935. "invalid function code offset.");
  936. return false;
  937. }
  938. module->func_ptrs[i] = (uint8*)module->code + text_offset;
  939. #if defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_THUMB_VFP)
  940. /* bits[0] of thumb function address must be 1 */
  941. module->func_ptrs[i] = (void*)((uintptr_t)module->func_ptrs[i] | 1);
  942. #endif
  943. }
  944. /* Set start function when function pointers are resolved */
  945. if (module->start_func_index != (uint32)-1) {
  946. module->start_function =
  947. module->func_ptrs[module->start_func_index
  948. - module->import_func_count];
  949. }
  950. else {
  951. module->start_function = NULL;
  952. }
  953. size = sizeof(uint32) * (uint64)module->func_count;
  954. if (size >= UINT32_MAX
  955. || !(module->func_type_indexes = wasm_malloc((uint32)size))) {
  956. set_error_buf(error_buf, error_buf_size,
  957. "AOT module load failed: allocate memory failed.");
  958. return false;
  959. }
  960. for (i = 0; i < module->func_count; i++) {
  961. read_uint32(p, p_end, module->func_type_indexes[i]);
  962. if (module->func_type_indexes[i] >= module->func_type_count) {
  963. set_error_buf(error_buf, error_buf_size,
  964. "AOT module load failed: "
  965. "invalid function type index.");
  966. return false;
  967. }
  968. }
  969. if (p != buf_end) {
  970. set_error_buf(error_buf, error_buf_size,
  971. "AOT module load failed: "
  972. "invalid function section size");
  973. return false;
  974. }
  975. return true;
  976. fail:
  977. return false;
  978. }
  979. static void
  980. destroy_export_funcs(AOTExportFunc *export_funcs, bool is_jit_mode)
  981. {
  982. if (!is_jit_mode)
  983. wasm_free(export_funcs);
  984. }
  985. static bool
  986. load_export_funcs(const uint8 **p_buf, const uint8 *buf_end,
  987. AOTModule *module,
  988. char *error_buf, uint32 error_buf_size)
  989. {
  990. const uint8 *buf = *p_buf;
  991. AOTExportFunc *export_funcs;
  992. uint64 size;
  993. uint32 i;
  994. /* Allocate memory */
  995. size = sizeof(AOTExportFunc) * (uint64)module->export_func_count;
  996. if (size >= UINT32_MAX
  997. || !(module->export_funcs =
  998. export_funcs = wasm_malloc((uint32)size))) {
  999. set_error_buf(error_buf, error_buf_size,
  1000. "AOT module load failed: "
  1001. "allocate memory failed.");
  1002. return false;
  1003. }
  1004. memset(export_funcs, 0, size);
  1005. /* Create each export func */
  1006. for (i = 0; i < module->export_func_count; i++) {
  1007. read_uint32(buf, buf_end, export_funcs[i].func_index);
  1008. if (export_funcs[i].func_index >=
  1009. module->func_count + module->import_func_count) {
  1010. set_error_buf(error_buf, error_buf_size,
  1011. "AOT module load failed: "
  1012. "function index is out of range.");
  1013. return false;
  1014. }
  1015. read_string(buf, buf_end, export_funcs[i].func_name);
  1016. }
  1017. *p_buf = buf;
  1018. return true;
  1019. fail:
  1020. return false;
  1021. }
  1022. static bool
  1023. load_export_section(const uint8 *buf, const uint8 *buf_end,
  1024. AOTModule *module,
  1025. char *error_buf, uint32 error_buf_size)
  1026. {
  1027. const uint8 *p = buf, *p_end = buf_end;
  1028. /* load export functions */
  1029. read_uint32(p, p_end, module->export_func_count);
  1030. if (module->export_func_count > 0
  1031. && !load_export_funcs(&p, p_end, module, error_buf, error_buf_size))
  1032. return false;
  1033. if (p != p_end) {
  1034. set_error_buf(error_buf, error_buf_size,
  1035. "AOT module load failed: "
  1036. "invalid export section size");
  1037. return false;
  1038. }
  1039. return true;
  1040. fail:
  1041. return false;
  1042. }
  1043. #define R_386_32 1 /* Direct 32 bit */
  1044. #define R_386_PC32 2 /* PC relative 32 bit */
  1045. #define R_X86_64_64 1 /* Direct 64 bit */
  1046. #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
  1047. #define R_X86_64_PLT32 4 /* 32 bit PLT address */
  1048. #define R_X86_64_32 10 /* Direct 32 bit zero extended */
  1049. #define R_X86_64_32S 11 /* Direct 32 bit sign extended */
  1050. #define R_ARM_CALL 28 /* PC relative 24 bit (BL, BLX). */
  1051. #define R_ARM_JMP24 29 /* PC relative 24 bit (B/BL<cond>). */
  1052. #define R_ARM_ABS32 2 /* Direct 32 bit */
  1053. #define R_ARM_THM_CALL 10 /* PC relative (Thumb BL and ARMv5 Thumb BLX). */
  1054. #define R_ARM_THM_JMP24 30 /* B.W */
  1055. #define R_MIPS_32 2 /* Direct 32 bit */
  1056. #define R_MIPS_26 4 /* Direct 26 bit shifted */
  1057. #ifndef BH_MB
  1058. #define BH_MB 1024 * 1024
  1059. #endif
  1060. static bool
  1061. check_reloc_offset(uint32 target_section_size,
  1062. uint64 reloc_offset, uint32 reloc_data_size,
  1063. char *error_buf, uint32 error_buf_size)
  1064. {
  1065. if (!(reloc_offset < (uint64)target_section_size
  1066. && reloc_offset + reloc_data_size <= (uint64)target_section_size)) {
  1067. set_error_buf(error_buf, error_buf_size,
  1068. "AOT module load failed: invalid relocation offset.");
  1069. return false;
  1070. }
  1071. return true;
  1072. }
  1073. #define CHECK_RELOC_OFFSET(data_size) do { \
  1074. if (!check_reloc_offset(target_section_size, reloc_offset, data_size, \
  1075. error_buf, error_buf_size)) \
  1076. return false; \
  1077. } while (0)
  1078. static bool
  1079. apply_relocation(AOTModule *module,
  1080. uint8 *target_section_addr, uint32 target_section_size,
  1081. uint64 reloc_offset, uint64 reloc_addend,
  1082. uint32 reloc_type, void *symbol_addr, int32 symbol_index,
  1083. char *error_buf, uint32 error_buf_size)
  1084. {
  1085. switch (reloc_type) {
  1086. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1087. case R_X86_64_64:
  1088. {
  1089. intptr_t value;
  1090. CHECK_RELOC_OFFSET(sizeof(void*));
  1091. value = *(intptr_t*)(target_section_addr + (uint32)reloc_offset);
  1092. *(uint8**)(target_section_addr + reloc_offset)
  1093. = (uint8*)symbol_addr + reloc_addend + value; /* S + A */
  1094. break;
  1095. }
  1096. case R_X86_64_PC32:
  1097. {
  1098. intptr_t target_addr = (intptr_t) /* S + A - P */
  1099. ((uint8*)symbol_addr + reloc_addend
  1100. - (target_section_addr + reloc_offset));
  1101. CHECK_RELOC_OFFSET(sizeof(int32));
  1102. if ((int32)target_addr != target_addr) {
  1103. set_error_buf(error_buf, error_buf_size,
  1104. "AOT module load failed: "
  1105. "relocation truncated to fit R_X86_64_PC32 failed");
  1106. return false;
  1107. }
  1108. *(int32*)(target_section_addr + reloc_offset) = (int32)target_addr;
  1109. break;
  1110. }
  1111. case R_X86_64_32:
  1112. case R_X86_64_32S:
  1113. {
  1114. char buf[128];
  1115. uintptr_t target_addr = (uintptr_t) /* S + A */
  1116. ((uint8*)symbol_addr + reloc_addend);
  1117. CHECK_RELOC_OFFSET(sizeof(int32));
  1118. if ((reloc_type == R_X86_64_32
  1119. && (uint32)target_addr != (uint64)target_addr)
  1120. || (reloc_type == R_X86_64_32S
  1121. && (int32)target_addr != (int64)target_addr)) {
  1122. snprintf(buf, sizeof(buf),
  1123. "AOT module load failed: "
  1124. "relocation truncated to fit %s failed",
  1125. reloc_type == R_X86_64_32
  1126. ? "R_X86_64_32" : "R_X86_64_32S");
  1127. set_error_buf(error_buf, error_buf_size, buf);
  1128. return false;
  1129. }
  1130. *(int32*)(target_section_addr + reloc_offset) = (int32)target_addr;
  1131. break;
  1132. }
  1133. case R_X86_64_PLT32:
  1134. {
  1135. uint8 *plt = module->code + module->code_size - get_plt_table_size()
  1136. + get_plt_item_size() * symbol_index;
  1137. intptr_t target_addr = (intptr_t) /* L + A - P */
  1138. (plt + reloc_addend
  1139. - (target_section_addr + reloc_offset));
  1140. CHECK_RELOC_OFFSET(sizeof(int32));
  1141. if (symbol_index < 0) {
  1142. set_error_buf(error_buf, error_buf_size,
  1143. "AOT module load failed: "
  1144. "invalid symbol index for relocation");
  1145. return false;
  1146. }
  1147. if ((int32)target_addr != target_addr) {
  1148. set_error_buf(error_buf, error_buf_size,
  1149. "AOT module load failed: "
  1150. "relocation truncated to fit R_X86_64_PC32 failed");
  1151. return false;
  1152. }
  1153. *(int32*)(target_section_addr + reloc_offset) = (int32)target_addr;
  1154. break;
  1155. }
  1156. #endif /* end of BUILD_TARGET_X86_64 || BUILD_TARGET_AMD_64 */
  1157. #if defined(BUILD_TARGET_X86_32)
  1158. case R_386_32:
  1159. {
  1160. intptr_t value;
  1161. CHECK_RELOC_OFFSET(sizeof(void*));
  1162. value = *(intptr_t*)(target_section_addr + (uint32)reloc_offset);
  1163. *(uint8**)(target_section_addr + reloc_offset)
  1164. = (uint8*)symbol_addr + reloc_addend + value; /* S + A */
  1165. break;
  1166. }
  1167. case R_386_PC32:
  1168. {
  1169. int32 value;
  1170. CHECK_RELOC_OFFSET(sizeof(void*));
  1171. value = *(int32*)(target_section_addr + (uint32)reloc_offset);
  1172. *(uint32*)(target_section_addr + (uint32)reloc_offset) = (uint32)
  1173. ((uint8*)symbol_addr + (uint32)reloc_addend
  1174. - (uint8*)(target_section_addr + (uint32)reloc_offset)
  1175. + value); /* S + A - P */
  1176. break;
  1177. }
  1178. #endif /* end of BUILD_TARGET_X86_32 */
  1179. #if defined(BUILD_TARGET_ARM) || defined(BUILD_TARGET_ARM_VFP)
  1180. /* TODO: implement ARM relocation */
  1181. case R_ARM_CALL:
  1182. case R_ARM_JMP24:
  1183. case R_ARM_ABS32:
  1184. #endif
  1185. #if defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_THUMB_VFP)
  1186. /* TODO: implement THUMB relocation */
  1187. case R_ARM_THM_CALL:
  1188. case R_ARM_THM_JMP24:
  1189. #endif
  1190. #if defined(BUILD_TARGET_MIPS_32)
  1191. case R_MIPS_26:
  1192. case R_MIPS_32:
  1193. /* TODO: implement relocation for mips */
  1194. #endif
  1195. default:
  1196. if (error_buf != NULL)
  1197. snprintf(error_buf, error_buf_size,
  1198. "Load import section failed: "
  1199. "invalid relocation type %d.",
  1200. reloc_type);
  1201. return false;
  1202. }
  1203. return true;
  1204. }
  1205. static void *
  1206. get_data_section_addr(AOTModule *module, const char *section_name,
  1207. uint32 *p_data_size)
  1208. {
  1209. uint32 i;
  1210. AOTObjectDataSection *data_section = module->data_sections;
  1211. for (i = 0; i < module->data_section_count; i++, data_section++)
  1212. if (!strcmp(data_section->name, section_name)) {
  1213. if (p_data_size)
  1214. *p_data_size = data_section->size;
  1215. return data_section->data;
  1216. }
  1217. return NULL;
  1218. }
  1219. typedef struct {
  1220. const char *symbol_name;
  1221. void *symbol_addr;
  1222. } SymbolMap;
  1223. #define REG_SYM(symbol) { #symbol, (void*)symbol }
  1224. #if defined(BUILD_TARGET_X86_32)
  1225. void __divdi3();
  1226. void __udivdi3();
  1227. void __moddi3();
  1228. void __umoddi3();
  1229. #endif
  1230. #if defined(BUILD_TARGET_ARM) \
  1231. || defined(BUILD_TARGET_ARM_VFP) \
  1232. || defined(BUILD_TARGET_THUMB) \
  1233. || defined(BUILD_TARGET_THUMB_VFP)
  1234. void __divdi3();
  1235. void __udivdi3();
  1236. void __moddi3();
  1237. void __umoddi3();
  1238. void __divsi3();
  1239. void __udivsi3();
  1240. void __modsi3();
  1241. void __udivmoddi4();
  1242. void __clzsi2();
  1243. void __fixsfdi();
  1244. void __fixunssfdi();
  1245. void __fixdfdi();
  1246. void __fixunsdfdi();
  1247. void __floatdisf();
  1248. void __floatundisf();
  1249. void __floatdidf();
  1250. void __floatundidf();
  1251. void __aeabi_l2f();
  1252. void __aeabi_f2lz();
  1253. void __aeabi_ul2f();
  1254. void __aeabi_d2lz();
  1255. void __aeabi_l2d();
  1256. void __aeabi_f2ulz();
  1257. void __aeabi_ul2d();
  1258. void __aeabi_d2ulz();
  1259. void __aeabi_idiv();
  1260. void __aeabi_uidiv();
  1261. void __aeabi_idivmod();
  1262. void __aeabi_uidivmod();
  1263. void __aeabi_ldivmod();
  1264. void __aeabi_uldivmod();
  1265. #endif
  1266. static SymbolMap target_sym_map[] = {
  1267. REG_SYM(aot_set_exception_with_id),
  1268. REG_SYM(aot_get_exception),
  1269. REG_SYM(aot_is_wasm_type_equal),
  1270. REG_SYM(wasm_runtime_enlarge_memory),
  1271. REG_SYM(wasm_runtime_set_exception),
  1272. REG_SYM(fmin),
  1273. REG_SYM(fminf),
  1274. REG_SYM(fmax),
  1275. REG_SYM(fmaxf),
  1276. REG_SYM(ceil),
  1277. REG_SYM(ceilf),
  1278. REG_SYM(floor),
  1279. REG_SYM(floorf),
  1280. REG_SYM(trunc),
  1281. REG_SYM(truncf),
  1282. REG_SYM(rint),
  1283. REG_SYM(rintf),
  1284. /* compiler-rt symbols that come from compiler(e.g. gcc) */
  1285. #if defined(BUILD_TARGET_X86_32)
  1286. REG_SYM(__divdi3),
  1287. REG_SYM(__udivdi3),
  1288. REG_SYM(__moddi3),
  1289. REG_SYM(__umoddi3)
  1290. #elif defined(BUILD_TARGET_ARM) \
  1291. || defined(BUILD_TARGET_ARM_VFP) \
  1292. || defined(BUILD_TARGET_THUMB) \
  1293. || defined(BUILD_TARGET_THUMB_VFP)
  1294. REG_SYM(__divdi3),
  1295. REG_SYM(__udivdi3),
  1296. REG_SYM(__umoddi3),
  1297. REG_SYM(__divsi3),
  1298. REG_SYM(__udivsi3),
  1299. REG_SYM(__modsi3),
  1300. REG_SYM(__udivmoddi4),
  1301. REG_SYM(__clzsi2),
  1302. REG_SYM(__fixsfdi),
  1303. REG_SYM(__fixunssfdi),
  1304. REG_SYM(__fixdfdi),
  1305. REG_SYM(__fixunsdfdi),
  1306. REG_SYM(__floatdisf),
  1307. REG_SYM(__floatundisf),
  1308. REG_SYM(__floatdidf),
  1309. REG_SYM(__floatundidf),
  1310. REG_SYM(__aeabi_l2f),
  1311. REG_SYM(__aeabi_f2lz),
  1312. REG_SYM(__aeabi_ul2f),
  1313. REG_SYM(__aeabi_d2lz),
  1314. REG_SYM(__aeabi_l2d),
  1315. REG_SYM(__aeabi_f2ulz),
  1316. REG_SYM(__aeabi_ul2d),
  1317. REG_SYM(__aeabi_d2ulz),
  1318. REG_SYM(__aeabi_idiv),
  1319. REG_SYM(__aeabi_uidiv),
  1320. REG_SYM(__aeabi_idivmod),
  1321. REG_SYM(__aeabi_uidivmod),
  1322. REG_SYM(__aeabi_ldivmod),
  1323. REG_SYM(__aeabi_uldivmod),
  1324. #endif /* end of BUILD_TARGET_X86_32 */
  1325. };
  1326. static void *
  1327. resolve_target_sym(const char *symbol, int32 *p_index)
  1328. {
  1329. uint32 i, num = sizeof(target_sym_map) / sizeof(SymbolMap);
  1330. for (i = 0; i < num; i++)
  1331. if (!strcmp(target_sym_map[i].symbol_name, symbol)) {
  1332. *p_index = (int32)i;
  1333. return target_sym_map[i].symbol_addr;
  1334. }
  1335. return NULL;
  1336. }
  1337. static inline uint32
  1338. get_plt_item_size()
  1339. {
  1340. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1341. /* size of mov instruction and jmp instruction */
  1342. return 12;
  1343. #elif defined(BUILD_TARGET_ARM) || defined(BUILD_TARGET_ARM_VFP)
  1344. /* 20 bytes instructions and 4 bytes symbol address */
  1345. return 24;
  1346. #elif defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_THUMB_VFP)
  1347. /* 16 bytes instructions and 4 bytes symbol address */
  1348. return 20;
  1349. #endif
  1350. return 0;
  1351. }
  1352. static uint32
  1353. get_plt_table_size()
  1354. {
  1355. return get_plt_item_size() * (sizeof(target_sym_map) / sizeof(SymbolMap));
  1356. }
  1357. static void
  1358. init_plt_table(uint8 *plt)
  1359. {
  1360. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1361. uint32 i, num = sizeof(target_sym_map) / sizeof(SymbolMap);
  1362. for (i = 0; i < num; i++) {
  1363. uint8 *p = plt;
  1364. /* mov symbol_addr, rax */
  1365. *p++ = 0x48;
  1366. *p++ = 0xB8;
  1367. *(uint64*)p = (uint64)(uintptr_t)target_sym_map[i].symbol_addr;
  1368. p += sizeof(uint64);
  1369. /* jmp rax */
  1370. *p++ = 0xFF;
  1371. *p++ = 0xE0;
  1372. plt += get_plt_item_size();
  1373. }
  1374. #endif
  1375. #if defined(BUILD_TARGET_ARM) || defined(BUILD_TARGET_ARM_VFP)
  1376. uint32 i, num = sizeof(target_sym_map) / sizeof(SymbolMap);
  1377. for (i = 0; i < num; i++) {
  1378. uint32 *p = (uint32*)plt;
  1379. /* push {lr} */
  1380. *p++ = 0xe52de004;
  1381. /* ldr lr, [pc, #8] */
  1382. *p++ = 0xe59fe008;
  1383. /* blx lr */
  1384. *p++ = 0xe12fff3e;
  1385. /* pop {lr} */
  1386. *p++ = 0xe49de004;
  1387. /* bx lr */
  1388. *p++ = 0xe12fff1e;
  1389. /* symbol addr */
  1390. *p++ = (uint32)(uintptr_t)target_sym_map[i].symbol_addr;;
  1391. plt += get_plt_item_size();
  1392. }
  1393. #endif
  1394. #if defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_THUMB_VFP)
  1395. uint32 i, num = sizeof(target_sym_map) / sizeof(SymbolMap);
  1396. for (i = 0; i < num; i++) {
  1397. uint16 *p = (uint16*)plt;
  1398. /* push {lr} */
  1399. *p++ = 0xb500;
  1400. /* push {r4, r5} */
  1401. *p++ = 0xb430;
  1402. /* add r4, pc, #8 */
  1403. *p++ = 0xa402;
  1404. /* ldr r5, [r4, #0] */
  1405. *p++ = 0x6825;
  1406. /* blx r5 */
  1407. *p++ = 0x47a8;
  1408. /* pop {r4, r5} */
  1409. *p++ = 0xbc30;
  1410. /* pop {pc} */
  1411. *p++ = 0xbd00;
  1412. p++;
  1413. /* symbol addr */
  1414. *(uint32*)p = (uint32)(uintptr_t)target_sym_map[i].symbol_addr;;
  1415. plt += get_plt_item_size();
  1416. }
  1417. #endif
  1418. }
  1419. static bool
  1420. do_text_relocation(AOTModule *module,
  1421. AOTRelocationGroup *group,
  1422. char *error_buf, uint32 error_buf_size)
  1423. {
  1424. uint8 *aot_text = module->code;
  1425. uint32 aot_text_size = module->code_size;
  1426. uint32 i, func_index, symbol_len;
  1427. char symbol_buf[128] = { 0 }, *symbol, *p;
  1428. void *symbol_addr;
  1429. AOTRelocation *relocation = group->relocations;
  1430. if (group->relocation_count > 0 && !aot_text) {
  1431. set_error_buf(error_buf, error_buf_size,
  1432. "AOT module load failed: invalid text relocation count.");
  1433. return false;
  1434. }
  1435. for (i = 0; i < group->relocation_count; i++, relocation++) {
  1436. int32 symbol_index = -1;
  1437. symbol_len = (uint32)strlen(relocation->symbol_name);
  1438. if (symbol_len + 1 <= sizeof(symbol_buf))
  1439. symbol = symbol_buf;
  1440. else {
  1441. if (!(symbol = wasm_malloc(symbol_len + 1))) {
  1442. set_error_buf(error_buf, error_buf_size,
  1443. "AOT module load failed: "
  1444. "allocate memory failed.");
  1445. return false;
  1446. }
  1447. }
  1448. memcpy(symbol, relocation->symbol_name, symbol_len);
  1449. symbol[symbol_len] = '\0';
  1450. if (!strncmp(symbol, AOT_FUNC_PREFIX, strlen(AOT_FUNC_PREFIX))) {
  1451. p = symbol + strlen(AOT_FUNC_PREFIX);
  1452. if (*p == '\0'
  1453. || (func_index = (uint32)atoi(p)) > module->func_count) {
  1454. if (error_buf != NULL)
  1455. snprintf(error_buf, error_buf_size,
  1456. "AOT module load failed: "
  1457. "invalid import symbol %s.",
  1458. symbol);
  1459. goto check_symbol_fail;
  1460. }
  1461. symbol_addr = module->func_ptrs[func_index];
  1462. }
  1463. else if (!strcmp(symbol, ".text")) {
  1464. symbol_addr = module->code;
  1465. }
  1466. else if (!strcmp(symbol, ".data")
  1467. || !strcmp(symbol, ".rodata")
  1468. /* ".rodata.cst4/8/16/.." */
  1469. || !strncmp(symbol, ".rodata.cst", strlen(".rodata.cst"))) {
  1470. symbol_addr = get_data_section_addr(module, symbol, NULL);
  1471. if (!symbol_addr) {
  1472. if (error_buf != NULL)
  1473. snprintf(error_buf, error_buf_size,
  1474. "AOT module load failed: "
  1475. "invalid data section (%s).",
  1476. symbol);
  1477. goto check_symbol_fail;
  1478. }
  1479. }
  1480. else if (!(symbol_addr = resolve_target_sym(symbol, &symbol_index))) {
  1481. if (error_buf != NULL)
  1482. snprintf(error_buf, error_buf_size,
  1483. "AOT module load failed: "
  1484. "resolve symbol %s failed.",
  1485. symbol);
  1486. goto check_symbol_fail;
  1487. }
  1488. if (symbol != symbol_buf)
  1489. wasm_free(symbol);
  1490. if (!apply_relocation(module,
  1491. aot_text, aot_text_size,
  1492. relocation->relocation_offset,
  1493. relocation->relocation_addend,
  1494. relocation->relocation_type,
  1495. symbol_addr, symbol_index,
  1496. error_buf, error_buf_size))
  1497. return false;
  1498. }
  1499. return true;
  1500. check_symbol_fail:
  1501. if (symbol != symbol_buf)
  1502. wasm_free(symbol);
  1503. return false;
  1504. }
  1505. static bool
  1506. do_data_relocation(AOTModule *module,
  1507. AOTRelocationGroup *group,
  1508. char *error_buf, uint32 error_buf_size)
  1509. {
  1510. uint8 *data_addr;
  1511. uint32 data_size = 0, i;
  1512. AOTRelocation *relocation = group->relocations;
  1513. void *symbol_addr;
  1514. char *symbol, *data_section_name;
  1515. if (!strncmp(group->section_name, ".rela.", 6)) {
  1516. data_section_name = group->section_name + strlen(".rela");
  1517. }
  1518. else if (!strncmp(group->section_name, ".rel.", 5)) {
  1519. data_section_name = group->section_name + strlen(".rel");
  1520. }
  1521. else {
  1522. set_error_buf(error_buf, error_buf_size,
  1523. "AOT module load failed: "
  1524. "invalid data relocation section name.");
  1525. return false;
  1526. }
  1527. data_addr = get_data_section_addr(module, data_section_name,
  1528. &data_size);
  1529. if (group->relocation_count > 0 && !data_addr) {
  1530. set_error_buf(error_buf, error_buf_size,
  1531. "AOT module load failed: invalid data relocation count.");
  1532. return false;
  1533. }
  1534. for (i = 0; i < group->relocation_count; i++, relocation++) {
  1535. symbol = relocation->symbol_name;
  1536. if (!strcmp(symbol, ".text")) {
  1537. symbol_addr = module->code;
  1538. }
  1539. else {
  1540. if (error_buf != NULL)
  1541. snprintf(error_buf, error_buf_size,
  1542. "AOT module load failed: "
  1543. "invalid relocation symbol %s.",
  1544. symbol);
  1545. return false;
  1546. }
  1547. if (!apply_relocation(module,
  1548. data_addr, data_size,
  1549. relocation->relocation_offset,
  1550. relocation->relocation_addend,
  1551. relocation->relocation_type,
  1552. symbol_addr, -1,
  1553. error_buf, error_buf_size))
  1554. return false;
  1555. }
  1556. return true;
  1557. }
  1558. static bool
  1559. validate_symbol_table(uint8 *buf, uint8 *buf_end,
  1560. uint32 *offsets, uint32 count,
  1561. char *error_buf, uint32 error_buf_size)
  1562. {
  1563. uint32 i, str_len_addr = 0;
  1564. uint16 str_len;
  1565. for (i = 0; i < count; i++) {
  1566. if (offsets[i] != str_len_addr)
  1567. return false;
  1568. read_uint16(buf, buf_end, str_len);
  1569. str_len_addr += (uint32)sizeof(uint16) + str_len;
  1570. str_len_addr = align_uint(str_len_addr, 2);
  1571. buf += str_len;
  1572. buf = (uint8*)align_ptr(buf, 2);
  1573. }
  1574. if (buf == buf_end)
  1575. return true;
  1576. fail:
  1577. return false;
  1578. }
  1579. static bool
  1580. load_relocation_section(const uint8 *buf, const uint8 *buf_end,
  1581. AOTModule *module,
  1582. char *error_buf, uint32 error_buf_size)
  1583. {
  1584. AOTRelocationGroup *groups = NULL, *group;
  1585. uint32 symbol_count = 0;
  1586. uint32 group_count = 0, i, j, func_index, func_type_index;
  1587. uint64 size;
  1588. uint32 *symbol_offsets, total_string_len;
  1589. uint8 *symbol_buf, *symbol_buf_end;
  1590. bool ret = false;
  1591. AOTExportFunc *export_func;
  1592. read_uint32(buf, buf_end, symbol_count);
  1593. symbol_offsets = (uint32 *)buf;
  1594. for (i = 0; i < symbol_count; i++) {
  1595. CHECK_BUF(buf, buf_end, sizeof(uint32));
  1596. buf += sizeof(uint32);
  1597. }
  1598. read_uint32(buf, buf_end, total_string_len);
  1599. symbol_buf = (uint8 *)buf;
  1600. symbol_buf_end = symbol_buf + total_string_len;
  1601. if (!validate_symbol_table(symbol_buf, symbol_buf_end,
  1602. symbol_offsets, symbol_count,
  1603. error_buf, error_buf_size)) {
  1604. set_error_buf(error_buf, error_buf_size,
  1605. "AOT module load failed: "
  1606. "validate symbol table failed.");
  1607. goto fail;
  1608. }
  1609. buf = symbol_buf_end;
  1610. read_uint32(buf, buf_end, group_count);
  1611. /* Allocate memory for relocation groups */
  1612. size = sizeof(AOTRelocationGroup) * (uint64)group_count;
  1613. if (size >= UINT32_MAX || !(groups = wasm_malloc((uint32)size))) {
  1614. set_error_buf(error_buf, error_buf_size,
  1615. "AOT module load failed: "
  1616. "allocate memory failed.");
  1617. goto fail;
  1618. }
  1619. memset(groups, 0, size);
  1620. /* Load each relocation group */
  1621. for (i = 0, group = groups; i < group_count; i++, group++) {
  1622. AOTRelocation *relocation;
  1623. uint32 name_index;
  1624. uint16 str_len;
  1625. uint8 *name_addr;
  1626. /* section name address is 4 bytes aligned. */
  1627. buf = (uint8*)align_ptr(buf, sizeof(uint32));
  1628. read_uint32(buf, buf_end, name_index);
  1629. if (name_index >= symbol_count) {
  1630. set_error_buf(error_buf, error_buf_size,
  1631. "AOT module load failed: "
  1632. "symbol index out of range.");
  1633. goto fail;
  1634. }
  1635. name_addr = symbol_buf + symbol_offsets[name_index];
  1636. str_len = *(uint16 *)name_addr;
  1637. if (!(group->section_name =
  1638. const_str_set_insert(name_addr + sizeof(uint16),
  1639. (int32)str_len, module,
  1640. error_buf, error_buf_size))) {
  1641. goto fail;
  1642. }
  1643. read_uint32(buf, buf_end, group->relocation_count);
  1644. /* Allocate memory for relocations */
  1645. size = sizeof(AOTRelocation) * (uint64)group->relocation_count;
  1646. if (size >= UINT32_MAX
  1647. || !(group->relocations = relocation =
  1648. wasm_malloc((uint32)size))) {
  1649. set_error_buf(error_buf, error_buf_size,
  1650. "AOT module load failed: "
  1651. "allocate memory failed.");
  1652. ret = false;
  1653. goto fail;
  1654. }
  1655. memset(group->relocations, 0, size);
  1656. /* Load each relocation */
  1657. for (j = 0; j < group->relocation_count; j++, relocation++) {
  1658. uint32 symbol_index;
  1659. uint16 str_len;
  1660. uint8 *symbol_addr;
  1661. if (sizeof(void *) == 8) {
  1662. read_uint64(buf, buf_end, relocation->relocation_offset);
  1663. read_uint64(buf, buf_end, relocation->relocation_addend);
  1664. }
  1665. else {
  1666. uint32 offset32, addend32;
  1667. read_uint32(buf, buf_end, offset32);
  1668. relocation->relocation_offset = (uint64)offset32;
  1669. read_uint32(buf, buf_end, addend32);
  1670. relocation->relocation_addend = (uint64)addend32;
  1671. }
  1672. read_uint32(buf, buf_end, relocation->relocation_type);
  1673. read_uint32(buf, buf_end, symbol_index);
  1674. if (symbol_index >= symbol_count) {
  1675. set_error_buf(error_buf, error_buf_size,
  1676. "AOT module load failed: "
  1677. "symbol index out of range.");
  1678. goto fail;
  1679. }
  1680. symbol_addr = symbol_buf + symbol_offsets[symbol_index];
  1681. str_len = *(uint16 *)symbol_addr;
  1682. if (!(relocation->symbol_name =
  1683. const_str_set_insert(symbol_addr + sizeof(uint16),
  1684. (int32)str_len, module,
  1685. error_buf, error_buf_size))) {
  1686. goto fail;
  1687. }
  1688. }
  1689. if (!strcmp(group->section_name, ".rel.text")
  1690. || !strcmp(group->section_name, ".rela.text")) {
  1691. if (!do_text_relocation(module, group, error_buf, error_buf_size))
  1692. return false;
  1693. }
  1694. else {
  1695. if (!do_data_relocation(module, group, error_buf, error_buf_size))
  1696. return false;
  1697. }
  1698. }
  1699. export_func = module->export_funcs;
  1700. for (i = 0; i < module->export_func_count; i++, export_func++) {
  1701. func_index = export_func->func_index - module->import_func_count;
  1702. if (func_index >= module->func_count) {
  1703. set_error_buf(error_buf, error_buf_size,
  1704. "AOT module load failed: "
  1705. "invalid export function index.");
  1706. ret = false;
  1707. goto fail;
  1708. }
  1709. func_type_index = module->func_type_indexes[func_index];
  1710. export_func->func_type = module->func_types[func_type_index];
  1711. export_func->func_ptr = module->func_ptrs[func_index];
  1712. }
  1713. ret = true;
  1714. fail:
  1715. if (groups) {
  1716. for (i = 0, group = groups; i < group_count; i++, group++)
  1717. if (group->relocations)
  1718. wasm_free(group->relocations);
  1719. wasm_free(groups);
  1720. }
  1721. return ret;
  1722. }
  1723. static bool
  1724. load_from_sections(AOTModule *module, AOTSection *sections,
  1725. char *error_buf, uint32 error_buf_size)
  1726. {
  1727. AOTSection *section = sections;
  1728. const uint8 *buf, *buf_end;
  1729. uint32 last_section_type = (uint32)-1, section_type;
  1730. while (section) {
  1731. buf = section->section_body;
  1732. buf_end = buf + section->section_body_size;
  1733. /* Check sections */
  1734. section_type = (uint32)section->section_type;
  1735. if ((last_section_type == (uint32)-1
  1736. && section_type != AOT_SECTION_TYPE_TARGET_INFO)
  1737. || (last_section_type != (uint32)-1
  1738. && section_type != last_section_type + 1)) {
  1739. set_error_buf(error_buf, error_buf_size,
  1740. "AOT module load failed: invalid section order.");
  1741. return false;
  1742. }
  1743. last_section_type = section_type;
  1744. switch (section_type) {
  1745. case AOT_SECTION_TYPE_TARGET_INFO:
  1746. if (!load_target_info_section(buf, buf_end, module,
  1747. error_buf, error_buf_size))
  1748. return false;
  1749. break;
  1750. case AOT_SECTION_TYPE_INIT_DATA:
  1751. if (!load_init_data_section(buf, buf_end, module,
  1752. error_buf, error_buf_size))
  1753. return false;
  1754. break;
  1755. case AOT_SECTION_TYPE_TEXT:
  1756. if (!load_text_section(buf, buf_end, module,
  1757. error_buf, error_buf_size))
  1758. return false;
  1759. break;
  1760. case AOT_SECTION_TYPE_FUNCTION:
  1761. if (!load_function_section(buf, buf_end, module,
  1762. error_buf, error_buf_size))
  1763. return false;
  1764. break;
  1765. case AOT_SECTION_TYPE_EXPORT:
  1766. if (!load_export_section(buf, buf_end, module,
  1767. error_buf, error_buf_size))
  1768. return false;
  1769. break;
  1770. case AOT_SECTION_TYPE_RELOCATION:
  1771. if (!load_relocation_section(buf, buf_end, module,
  1772. error_buf, error_buf_size))
  1773. return false;
  1774. break;
  1775. }
  1776. section = section->next;
  1777. }
  1778. if (last_section_type != AOT_SECTION_TYPE_RELOCATION) {
  1779. set_error_buf(error_buf, error_buf_size,
  1780. "AOT module load failed: section missing.");
  1781. return false;
  1782. }
  1783. return true;
  1784. }
  1785. #if BEIHAI_ENABLE_MEMORY_PROFILING != 0
  1786. static void aot_free(void *ptr)
  1787. {
  1788. wasm_free(ptr);
  1789. }
  1790. #else
  1791. #define aot_free wasm_free
  1792. #endif
  1793. static AOTModule*
  1794. create_module(char *error_buf, uint32 error_buf_size)
  1795. {
  1796. AOTModule *module = wasm_malloc(sizeof(AOTModule));
  1797. if (!module) {
  1798. set_error_buf(error_buf, error_buf_size,
  1799. "AOT module load failed: "
  1800. "allocate memory failed.");
  1801. return NULL;
  1802. }
  1803. memset(module, 0, sizeof(AOTModule));
  1804. module->module_type = Wasm_Module_AoT;
  1805. if (!(module->const_str_set =
  1806. bh_hash_map_create(32, false,
  1807. (HashFunc)wasm_string_hash,
  1808. (KeyEqualFunc)wasm_string_equal,
  1809. NULL,
  1810. aot_free))) {
  1811. set_error_buf(error_buf, error_buf_size,
  1812. "AOT module load failed: "
  1813. "create const string set failed.");
  1814. wasm_free(module);
  1815. return NULL;
  1816. }
  1817. return module;
  1818. }
  1819. AOTModule*
  1820. aot_load_from_sections(AOTSection *section_list,
  1821. char *error_buf, uint32 error_buf_size)
  1822. {
  1823. AOTModule *module = create_module(error_buf, error_buf_size);
  1824. if (!module)
  1825. return NULL;
  1826. if (!load_from_sections(module, section_list,
  1827. error_buf, error_buf_size)) {
  1828. aot_unload(module);
  1829. return NULL;
  1830. }
  1831. LOG_VERBOSE("Load module from sections success.\n");
  1832. return module;
  1833. }
  1834. static void
  1835. destroy_sections(AOTSection *section_list, bool destroy_aot_text)
  1836. {
  1837. AOTSection *section = section_list, *next;
  1838. while (section) {
  1839. next = section->next;
  1840. if (destroy_aot_text
  1841. && section->section_type == AOT_SECTION_TYPE_TEXT
  1842. && section->section_body)
  1843. bh_munmap((uint8*)section->section_body, section->section_body_size);
  1844. wasm_free(section);
  1845. section = next;
  1846. }
  1847. }
  1848. static bool
  1849. create_sections(const uint8 *buf, uint32 size,
  1850. AOTSection **p_section_list,
  1851. char *error_buf, uint32 error_buf_size)
  1852. {
  1853. AOTSection *section_list = NULL, *section_list_end = NULL, *section;
  1854. const uint8 *p = buf, *p_end = buf + size;
  1855. uint32 section_type;
  1856. uint32 section_size;
  1857. uint64 total_size;
  1858. uint8 *aot_text;
  1859. p += 8;
  1860. while (p < p_end) {
  1861. read_uint32(p, p_end, section_type);
  1862. if (section_type < AOT_SECTION_TYPE_SIGANATURE) {
  1863. read_uint32(p, p_end, section_size);
  1864. CHECK_BUF(p, p_end, section_size);
  1865. if (!(section = wasm_malloc(sizeof(AOTSection)))) {
  1866. set_error_buf(error_buf, error_buf_size,
  1867. "AOT module load failed: "
  1868. "allocate memory failed.");
  1869. goto fail;
  1870. }
  1871. memset(section, 0, sizeof(AOTSection));
  1872. section->section_type = (int32)section_type;
  1873. section->section_body = p;
  1874. section->section_body_size = section_size;
  1875. if (section_type == AOT_SECTION_TYPE_TEXT) {
  1876. if (section_size > 0) {
  1877. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE
  1878. | MMAP_PROT_EXEC;
  1879. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1880. /* aot code and data in x86_64 must be in range 0 to 2G due to
  1881. relocation for R_X86_64_32/32S/PC32 */
  1882. int map_flags = MMAP_MAP_32BIT;
  1883. #else
  1884. int map_flags = MMAP_MAP_NONE;
  1885. #endif
  1886. total_size = (uint64)section_size + aot_get_plt_table_size();
  1887. total_size = (total_size + 3) & ~((uint64)3);
  1888. if (total_size >= UINT32_MAX
  1889. || !(aot_text = bh_mmap(NULL, (uint32)total_size,
  1890. map_prot, map_flags))) {
  1891. wasm_free(section);
  1892. set_error_buf(error_buf, error_buf_size,
  1893. "AOT module load failed: "
  1894. "mmap memory failed.");
  1895. goto fail;
  1896. }
  1897. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1898. /* address must be in the first 2 Gigabytes of
  1899. the process address space */
  1900. bh_assert((uintptr_t)aot_text < INT32_MAX);
  1901. #endif
  1902. bh_memcpy_s(aot_text, (uint32)total_size,
  1903. section->section_body, (uint32)section_size);
  1904. section->section_body = aot_text;
  1905. if ((uint32)total_size > section->section_body_size) {
  1906. memset(aot_text + (uint32)section_size,
  1907. 0, (uint32)total_size - section_size);
  1908. section->section_body_size = (uint32)total_size;
  1909. }
  1910. }
  1911. else
  1912. section->section_body = NULL;
  1913. }
  1914. if (!section_list)
  1915. section_list = section_list_end = section;
  1916. else {
  1917. section_list_end->next = section;
  1918. section_list_end = section;
  1919. }
  1920. p += section_size;
  1921. }
  1922. else {
  1923. set_error_buf(error_buf, error_buf_size,
  1924. "AOT module load failed: invalid section id.");
  1925. goto fail;
  1926. }
  1927. }
  1928. if (!section_list) {
  1929. set_error_buf(error_buf, error_buf_size,
  1930. "AOT module load failed: create section list failed.");
  1931. return false;
  1932. }
  1933. *p_section_list = section_list;
  1934. return true;
  1935. fail:
  1936. if (section_list)
  1937. destroy_sections(section_list, true);
  1938. return false;
  1939. }
  1940. static bool
  1941. load(const uint8 *buf, uint32 size, AOTModule *module,
  1942. char *error_buf, uint32 error_buf_size)
  1943. {
  1944. const uint8 *buf_end = buf + size;
  1945. const uint8 *p = buf, *p_end = buf_end;
  1946. uint32 magic_number, version;
  1947. AOTSection *section_list = NULL;
  1948. bool ret;
  1949. read_uint32(p, p_end, magic_number);
  1950. if (magic_number != AOT_MAGIC_NUMBER) {
  1951. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  1952. return false;
  1953. }
  1954. read_uint32(p, p_end, version);
  1955. if (version != AOT_CURRENT_VERSION) {
  1956. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  1957. return false;
  1958. }
  1959. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size))
  1960. return false;
  1961. ret = load_from_sections(module, section_list, error_buf, error_buf_size);
  1962. if (!ret) {
  1963. /* If load_from_sections() fails, then aot text is destroyed
  1964. in destroy_sections() */
  1965. destroy_sections(section_list, true);
  1966. /* aot_unload() won't destroy aot text again */
  1967. module->code = NULL;
  1968. }
  1969. else {
  1970. /* If load_from_sections() succeeds, then aot text is set to
  1971. module->code and will be destroyed in aot_unload() */
  1972. destroy_sections(section_list, false);
  1973. }
  1974. return ret;
  1975. fail:
  1976. return false;
  1977. }
  1978. AOTModule*
  1979. aot_load_from_aot_file(const uint8 *buf, uint32 size,
  1980. char *error_buf, uint32 error_buf_size)
  1981. {
  1982. AOTModule *module = create_module(error_buf, error_buf_size);
  1983. if (!module)
  1984. return NULL;
  1985. if (!load(buf, size, module, error_buf, error_buf_size)) {
  1986. aot_unload(module);
  1987. return NULL;
  1988. }
  1989. LOG_VERBOSE("Load module success.\n");
  1990. return module;
  1991. }
  1992. #if WASM_ENABLE_JIT != 0
  1993. static AOTModule*
  1994. aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
  1995. char *error_buf, uint32 error_buf_size)
  1996. {
  1997. uint32 i;
  1998. uint64 size;
  1999. char func_name[32];
  2000. AOTModule *module;
  2001. /* Allocate memory for module */
  2002. if (!(module = wasm_malloc(sizeof(AOTModule)))) {
  2003. set_error_buf(error_buf, error_buf_size,
  2004. "Allocate memory for AOT module failed.");
  2005. return NULL;
  2006. }
  2007. memset(module, 0, sizeof(AOTModule));
  2008. module->module_type = Wasm_Module_AoT;
  2009. module->num_bytes_per_page = comp_data->num_bytes_per_page;
  2010. module->mem_init_page_count = comp_data->mem_init_page_count;
  2011. module->mem_max_page_count = comp_data->mem_max_page_count;
  2012. module->mem_init_data_list = comp_data->mem_init_data_list;
  2013. module->mem_init_data_count = comp_data->mem_init_data_count;
  2014. module->table_init_data_list = comp_data->table_init_data_list;
  2015. module->table_init_data_count = comp_data->table_init_data_count;
  2016. module->table_size = comp_data->table_size;
  2017. module->func_type_count = comp_data->func_type_count;
  2018. module->func_types = comp_data->func_types;
  2019. module->import_global_count = comp_data->import_global_count;
  2020. module->import_globals = comp_data->import_globals;
  2021. module->global_count = comp_data->global_count;
  2022. module->globals = comp_data->globals;
  2023. module->global_count = comp_data->global_count;
  2024. module->globals = comp_data->globals;
  2025. module->global_data_size = comp_data->global_data_size;
  2026. module->import_func_count = comp_data->import_func_count;
  2027. module->import_funcs = comp_data->import_funcs;
  2028. module->func_count = comp_data->func_count;
  2029. /* Allocate memory for function pointers */
  2030. size = (uint64)module->func_count * sizeof(void *);
  2031. if (size >= UINT32_MAX
  2032. || !(module->func_ptrs = wasm_malloc((uint32)size))) {
  2033. set_error_buf(error_buf, error_buf_size, "Create func ptrs fail.");
  2034. goto fail1;
  2035. }
  2036. /* Resolve function addresses */
  2037. bh_assert(comp_ctx->exec_engine);
  2038. memset(module->func_ptrs, 0, (uint32)size);
  2039. for (i = 0; i < comp_data->func_count; i++) {
  2040. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  2041. if (!(module->func_ptrs[i] =
  2042. (void *)LLVMGetFunctionAddress(comp_ctx->exec_engine,
  2043. func_name))) {
  2044. set_error_buf(error_buf, error_buf_size,
  2045. "Get function address fail.");
  2046. goto fail2;
  2047. }
  2048. }
  2049. /* Allocation memory for function type indexes */
  2050. size = (uint64)module->func_count * sizeof(uint32);
  2051. if (size >= UINT32_MAX
  2052. || !(module->func_type_indexes = wasm_malloc((uint32)size))) {
  2053. set_error_buf(error_buf, error_buf_size, "Create func type indexes fail.");
  2054. goto fail2;
  2055. }
  2056. memset(module->func_type_indexes, 0, (uint32)size);
  2057. for (i = 0; i < comp_data->func_count; i++)
  2058. module->func_type_indexes[i] = comp_data->funcs[i]->func_type_index;
  2059. module->export_func_count = comp_data->export_func_count;
  2060. module->export_funcs = comp_data->export_funcs;
  2061. /* Set export function pointers */
  2062. for (i = 0; i < module->export_func_count; i++) {
  2063. module->export_funcs[i].func_ptr =
  2064. module->func_ptrs[module->export_funcs[i].func_index
  2065. - module->import_func_count];
  2066. }
  2067. module->start_func_index = comp_data->start_func_index;
  2068. if (comp_data->start_func_index != (uint32)-1) {
  2069. bh_assert(comp_data->start_func_index >= module->import_func_count
  2070. && comp_data->start_func_index < module->import_func_count
  2071. + module->func_count);
  2072. module->start_function =
  2073. module->func_ptrs[comp_data->start_func_index
  2074. - module->import_func_count];
  2075. }
  2076. else {
  2077. module->start_function = NULL;
  2078. }
  2079. module->llvm_aux_data_end = comp_data->llvm_aux_data_end;
  2080. module->llvm_aux_stack_bottom = comp_data->llvm_aux_stack_bottom;
  2081. module->llvm_aux_stack_size = comp_data->llvm_aux_stack_size;
  2082. module->llvm_aux_stack_global_index = comp_data->llvm_aux_stack_global_index;
  2083. module->code = NULL;
  2084. module->code_size = 0;
  2085. module->is_jit_mode = true;
  2086. module->wasm_module = comp_data->wasm_module;
  2087. module->comp_ctx = comp_ctx;
  2088. module->comp_data = comp_data;
  2089. #if WASM_ENABLE_LIBC_WASI != 0
  2090. module->is_wasi_module = comp_data->wasm_module->is_wasi_module;
  2091. #endif
  2092. return module;
  2093. fail2:
  2094. wasm_free(module->func_ptrs);
  2095. fail1:
  2096. wasm_free(module);
  2097. return NULL;
  2098. }
  2099. AOTModule*
  2100. aot_convert_wasm_module(WASMModule *wasm_module,
  2101. char *error_buf, uint32 error_buf_size)
  2102. {
  2103. AOTCompData *comp_data;
  2104. AOTCompContext *comp_ctx;
  2105. AOTModule *aot_module;
  2106. AOTCompOption option = { 0 };
  2107. char *aot_last_error;
  2108. comp_data = aot_create_comp_data(wasm_module);
  2109. if (!comp_data) {
  2110. aot_last_error = aot_get_last_error();
  2111. bh_assert(aot_last_error != NULL);
  2112. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2113. return NULL;
  2114. }
  2115. option.is_jit_mode = true;
  2116. comp_ctx = aot_create_comp_context(comp_data, &option);
  2117. if (!comp_ctx) {
  2118. aot_last_error = aot_get_last_error();
  2119. bh_assert(aot_last_error != NULL);
  2120. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2121. goto fail1;
  2122. }
  2123. if (!aot_compile_wasm(comp_ctx)) {
  2124. aot_last_error = aot_get_last_error();
  2125. bh_assert(aot_last_error != NULL);
  2126. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2127. goto fail2;
  2128. }
  2129. aot_module = aot_load_from_comp_data(comp_data, comp_ctx,
  2130. error_buf, error_buf_size);
  2131. if (!aot_module) {
  2132. goto fail2;
  2133. }
  2134. return aot_module;
  2135. fail2:
  2136. aot_destroy_comp_context(comp_ctx);
  2137. fail1:
  2138. aot_destroy_comp_data(comp_data);
  2139. return NULL;
  2140. }
  2141. #endif
  2142. void
  2143. aot_unload(AOTModule *module)
  2144. {
  2145. #if WASM_ENABLE_JIT != 0
  2146. if (module->comp_data)
  2147. aot_destroy_comp_data(module->comp_data);
  2148. if (module->comp_ctx)
  2149. aot_destroy_comp_context(module->comp_ctx);
  2150. if (module->wasm_module)
  2151. wasm_loader_unload(module->wasm_module);
  2152. #endif
  2153. if (module->mem_init_data_list)
  2154. destroy_mem_init_data_list(module->mem_init_data_list,
  2155. module->mem_init_data_count,
  2156. module->is_jit_mode);
  2157. if (module->table_init_data_list)
  2158. destroy_table_init_data_list(module->table_init_data_list,
  2159. module->table_init_data_count,
  2160. module->is_jit_mode);
  2161. if (module->func_types)
  2162. destroy_func_types(module->func_types,
  2163. module->func_type_count,
  2164. module->is_jit_mode);
  2165. if (module->import_globals)
  2166. destroy_import_globals(module->import_globals,
  2167. module->is_jit_mode);
  2168. if (module->globals)
  2169. destroy_globals(module->globals,
  2170. module->is_jit_mode);
  2171. if (module->import_funcs)
  2172. destroy_import_funcs(module->import_funcs,
  2173. module->is_jit_mode);
  2174. if (module->export_funcs)
  2175. destroy_export_funcs(module->export_funcs,
  2176. module->is_jit_mode);
  2177. if (module->func_type_indexes)
  2178. wasm_free(module->func_type_indexes);
  2179. if (module->func_ptrs)
  2180. wasm_free(module->func_ptrs);
  2181. if (module->const_str_set)
  2182. bh_hash_map_destroy(module->const_str_set);
  2183. if (module->code)
  2184. bh_munmap(module->code, module->code_size);
  2185. if (module->data_sections)
  2186. destroy_object_data_sections(module->data_sections,
  2187. module->data_section_count);
  2188. wasm_free(module);
  2189. }
  2190. uint32
  2191. aot_get_plt_table_size()
  2192. {
  2193. return get_plt_table_size();
  2194. }