aot_emit_aot_file.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_compiler.h"
  6. #include "../aot/aot_runtime.h"
  7. #define PUT_U64_TO_ADDR(addr, value) \
  8. do { \
  9. union { \
  10. uint64 val; \
  11. uint32 parts[2]; \
  12. } u; \
  13. u.val = (value); \
  14. ((uint32 *)(addr))[0] = u.parts[0]; \
  15. ((uint32 *)(addr))[1] = u.parts[1]; \
  16. } while (0)
  17. #define CHECK_SIZE(size) \
  18. do { \
  19. if (size == (uint32)-1) { \
  20. aot_set_last_error("get symbol size failed."); \
  21. return (uint32)-1; \
  22. } \
  23. } while (0)
  24. /* Internal function in object file */
  25. typedef struct AOTObjectFunc {
  26. char *func_name;
  27. uint64 text_offset;
  28. } AOTObjectFunc;
  29. /* Symbol table list node */
  30. typedef struct AOTSymbolNode {
  31. struct AOTSymbolNode *next;
  32. uint32 str_len;
  33. char *symbol;
  34. } AOTSymbolNode;
  35. typedef struct AOTSymbolList {
  36. AOTSymbolNode *head;
  37. AOTSymbolNode *end;
  38. uint32 len;
  39. } AOTSymbolList;
  40. /* AOT object data */
  41. typedef struct AOTObjectData {
  42. LLVMMemoryBufferRef mem_buf;
  43. LLVMBinaryRef binary;
  44. AOTTargetInfo target_info;
  45. void *text;
  46. uint32 text_size;
  47. /* literal data and size */
  48. void *literal;
  49. uint32 literal_size;
  50. AOTObjectDataSection *data_sections;
  51. uint32 data_sections_count;
  52. AOTObjectFunc *funcs;
  53. uint32 func_count;
  54. AOTSymbolList symbol_list;
  55. AOTRelocationGroup *relocation_groups;
  56. uint32 relocation_group_count;
  57. } AOTObjectData;
  58. #if 0
  59. static void dump_buf(uint8 *buf, uint32 size, char *title)
  60. {
  61. int i;
  62. printf("------ %s -------", title);
  63. for (i = 0; i < size; i++) {
  64. if ((i % 16) == 0)
  65. printf("\n");
  66. printf("%02x ", (unsigned char)buf[i]);
  67. }
  68. printf("\n\n");
  69. }
  70. #endif
  71. static bool
  72. is_32bit_binary(LLVMBinaryRef binary)
  73. {
  74. LLVMBinaryType type = LLVMBinaryGetType(binary);
  75. return (type == LLVMBinaryTypeELF32L || type == LLVMBinaryTypeELF32B);
  76. }
  77. static bool
  78. is_little_endian_binary(LLVMBinaryRef binary)
  79. {
  80. LLVMBinaryType type = LLVMBinaryGetType(binary);
  81. return (type == LLVMBinaryTypeELF32L || type == LLVMBinaryTypeELF64L);
  82. }
  83. static bool
  84. str_starts_with(const char *str, const char *prefix)
  85. {
  86. size_t len_pre = strlen(prefix), len_str = strlen(str);
  87. return (len_str >= len_pre) && !memcmp(str, prefix, len_pre);
  88. }
  89. static uint32
  90. get_file_header_size()
  91. {
  92. /* magic number (4 bytes) + version (4 bytes) */
  93. return sizeof(uint32) + sizeof(uint32);
  94. }
  95. static uint32
  96. get_string_size(const char *s)
  97. {
  98. /* string size (2 bytes) + string content without '\0' */
  99. return (uint32)sizeof(uint16) + (uint32)strlen(s);
  100. }
  101. static uint32
  102. get_target_info_section_size()
  103. {
  104. return sizeof(AOTTargetInfo);
  105. }
  106. static uint32
  107. get_mem_init_data_size(AOTMemInitData *mem_init_data)
  108. {
  109. /* init expr type (4 bytes) + init expr value (8 bytes)
  110. + byte count (4 bytes) + bytes */
  111. uint32 total_size = (uint32)(sizeof(uint32) + sizeof(uint64)
  112. + sizeof(uint32) + mem_init_data->byte_count);
  113. /* bulk_memory enabled:
  114. is_passive (4 bytes) + memory_index (4 bytes)
  115. bulk memory disabled:
  116. placeholder (4 bytes) + placeholder (4 bytes)
  117. */
  118. total_size += (sizeof(uint32) + sizeof(uint32));
  119. return total_size;
  120. }
  121. static uint32
  122. get_mem_init_data_list_size(AOTMemInitData **mem_init_data_list,
  123. uint32 mem_init_data_count)
  124. {
  125. AOTMemInitData **mem_init_data = mem_init_data_list;
  126. uint32 size = 0, i;
  127. for (i = 0; i < mem_init_data_count; i++, mem_init_data++) {
  128. size = align_uint(size, 4);
  129. size += get_mem_init_data_size(*mem_init_data);
  130. }
  131. return size;
  132. }
  133. static uint32
  134. get_import_memory_size(AOTCompData *comp_data)
  135. {
  136. /* currently we only emit import_memory_count = 0 */
  137. return sizeof(uint32);
  138. }
  139. static uint32
  140. get_memory_size(AOTCompData *comp_data)
  141. {
  142. /* memory_count + count * (memory_flags + num_bytes_per_page +
  143. init_page_count + max_page_count) */
  144. return (uint32)(sizeof(uint32)
  145. + comp_data->memory_count * sizeof(uint32) * 4);
  146. }
  147. static uint32
  148. get_mem_info_size(AOTCompData *comp_data)
  149. {
  150. /* import_memory_size + memory_size
  151. + init_data_count + init_data_list */
  152. return get_import_memory_size(comp_data) + get_memory_size(comp_data)
  153. + (uint32)sizeof(uint32)
  154. + get_mem_init_data_list_size(comp_data->mem_init_data_list,
  155. comp_data->mem_init_data_count);
  156. }
  157. static uint32
  158. get_table_init_data_size(AOTTableInitData *table_init_data)
  159. {
  160. /*
  161. * mode (4 bytes), elem_type (4 bytes), do not need is_dropped field
  162. *
  163. * table_index(4 bytes) + init expr type (4 bytes) + init expr value (8
  164. * bytes)
  165. * + func index count (4 bytes) + func indexes
  166. */
  167. return (uint32)(sizeof(uint32) * 2 + sizeof(uint32) + sizeof(uint32)
  168. + sizeof(uint64) + sizeof(uint32)
  169. + sizeof(uint32) * table_init_data->func_index_count);
  170. }
  171. static uint32
  172. get_table_init_data_list_size(AOTTableInitData **table_init_data_list,
  173. uint32 table_init_data_count)
  174. {
  175. /*
  176. * ------------------------------
  177. * | table_init_data_count
  178. * ------------------------------
  179. * | | U32 mode
  180. * | AOTTableInitData[N] | U32 elem_type
  181. * | | U32 table_index
  182. * | | U32 offset.init_expr_type
  183. * | | U64 offset.u.i64
  184. * | | U32 func_index_count
  185. * | | U32[func_index_count]
  186. * ------------------------------
  187. */
  188. AOTTableInitData **table_init_data = table_init_data_list;
  189. uint32 size = 0, i;
  190. size = (uint32)sizeof(uint32);
  191. for (i = 0; i < table_init_data_count; i++, table_init_data++) {
  192. size = align_uint(size, 4);
  193. size += get_table_init_data_size(*table_init_data);
  194. }
  195. return size;
  196. }
  197. static uint32
  198. get_import_table_size(AOTCompData *comp_data)
  199. {
  200. /*
  201. * ------------------------------
  202. * | import_table_count
  203. * ------------------------------
  204. * | | U32 table_init_size
  205. * | | ----------------------
  206. * | AOTImpotTable[N] | U32 table_init_size
  207. * | | ----------------------
  208. * | | U32 possible_grow (convenient than U8)
  209. * ------------------------------
  210. */
  211. return (uint32)(sizeof(uint32)
  212. + comp_data->import_table_count * (sizeof(uint32) * 3));
  213. }
  214. static uint32
  215. get_table_size(AOTCompData *comp_data)
  216. {
  217. /*
  218. * ------------------------------
  219. * | table_count
  220. * ------------------------------
  221. * | | U32 elem_type
  222. * | AOTTable[N] | U32 table_flags
  223. * | | U32 table_init_size
  224. * | | U32 table_max_size
  225. * | | U32 possible_grow (convenient than U8)
  226. * ------------------------------
  227. */
  228. return (uint32)(sizeof(uint32)
  229. + comp_data->table_count * (sizeof(uint32) * 5));
  230. }
  231. static uint32
  232. get_table_info_size(AOTCompData *comp_data)
  233. {
  234. /*
  235. * ------------------------------
  236. * | import_table_count
  237. * ------------------------------
  238. * |
  239. * | AOTImportTable[import_table_count]
  240. * |
  241. * ------------------------------
  242. * | table_count
  243. * ------------------------------
  244. * |
  245. * | AOTTable[table_count]
  246. * |
  247. * ------------------------------
  248. * | table_init_data_count
  249. * ------------------------------
  250. * |
  251. * | AOTTableInitData*[table_init_data_count]
  252. * |
  253. * ------------------------------
  254. */
  255. return get_import_table_size(comp_data) + get_table_size(comp_data)
  256. + get_table_init_data_list_size(comp_data->table_init_data_list,
  257. comp_data->table_init_data_count);
  258. }
  259. static uint32
  260. get_func_type_size(AOTFuncType *func_type)
  261. {
  262. /* param count + result count + types */
  263. return (uint32)sizeof(uint32) * 2 + func_type->param_count
  264. + func_type->result_count;
  265. }
  266. static uint32
  267. get_func_types_size(AOTFuncType **func_types, uint32 func_type_count)
  268. {
  269. AOTFuncType **func_type = func_types;
  270. uint32 size = 0, i;
  271. for (i = 0; i < func_type_count; i++, func_type++) {
  272. size = align_uint(size, 4);
  273. size += get_func_type_size(*func_type);
  274. }
  275. return size;
  276. }
  277. static uint32
  278. get_func_type_info_size(AOTCompData *comp_data)
  279. {
  280. /* func type count + func type list */
  281. return (uint32)sizeof(uint32)
  282. + get_func_types_size(comp_data->func_types,
  283. comp_data->func_type_count);
  284. }
  285. static uint32
  286. get_import_global_size(AOTImportGlobal *import_global)
  287. {
  288. /* type (1 byte) + is_mutable (1 byte) + module_name + global_name */
  289. uint32 size =
  290. (uint32)sizeof(uint8) * 2 + get_string_size(import_global->module_name);
  291. size = align_uint(size, 2);
  292. size += get_string_size(import_global->global_name);
  293. return size;
  294. }
  295. static uint32
  296. get_import_globals_size(AOTImportGlobal *import_globals,
  297. uint32 import_global_count)
  298. {
  299. AOTImportGlobal *import_global = import_globals;
  300. uint32 size = 0, i;
  301. for (i = 0; i < import_global_count; i++, import_global++) {
  302. size = align_uint(size, 2);
  303. size += get_import_global_size(import_global);
  304. }
  305. return size;
  306. }
  307. static uint32
  308. get_import_global_info_size(AOTCompData *comp_data)
  309. {
  310. /* import global count + import globals */
  311. return (uint32)sizeof(uint32)
  312. + get_import_globals_size(comp_data->import_globals,
  313. comp_data->import_global_count);
  314. }
  315. static uint32
  316. get_global_size(AOTGlobal *global)
  317. {
  318. if (global->init_expr.init_expr_type != INIT_EXPR_TYPE_V128_CONST)
  319. /* type (1 byte) + is_mutable (1 byte)
  320. + init expr type (2 byes) + init expr value (8 byes) */
  321. return sizeof(uint8) * 2 + sizeof(uint16) + sizeof(uint64);
  322. else
  323. /* type (1 byte) + is_mutable (1 byte)
  324. + init expr type (2 byes) + v128 value (16 byes) */
  325. return sizeof(uint8) * 2 + sizeof(uint16) + sizeof(uint64) * 2;
  326. }
  327. static uint32
  328. get_globals_size(AOTGlobal *globals, uint32 global_count)
  329. {
  330. AOTGlobal *global = globals;
  331. uint32 size = 0, i;
  332. for (i = 0; i < global_count; i++, global++) {
  333. size = align_uint(size, 4);
  334. size += get_global_size(global);
  335. }
  336. return size;
  337. }
  338. static uint32
  339. get_global_info_size(AOTCompData *comp_data)
  340. {
  341. /* global count + globals */
  342. return (uint32)sizeof(uint32)
  343. + get_globals_size(comp_data->globals, comp_data->global_count);
  344. }
  345. static uint32
  346. get_import_func_size(AOTImportFunc *import_func)
  347. {
  348. /* type index (2 bytes) + module_name + func_name */
  349. uint32 size =
  350. (uint32)sizeof(uint16) + get_string_size(import_func->module_name);
  351. size = align_uint(size, 2);
  352. size += get_string_size(import_func->func_name);
  353. return size;
  354. }
  355. static uint32
  356. get_import_funcs_size(AOTImportFunc *import_funcs, uint32 import_func_count)
  357. {
  358. AOTImportFunc *import_func = import_funcs;
  359. uint32 size = 0, i;
  360. for (i = 0; i < import_func_count; i++, import_func++) {
  361. size = align_uint(size, 2);
  362. size += get_import_func_size(import_func);
  363. }
  364. return size;
  365. }
  366. static uint32
  367. get_import_func_info_size(AOTCompData *comp_data)
  368. {
  369. /* import func count + import funcs */
  370. return (uint32)sizeof(uint32)
  371. + get_import_funcs_size(comp_data->import_funcs,
  372. comp_data->import_func_count);
  373. }
  374. static uint32
  375. get_object_data_section_size(AOTObjectDataSection *data_section)
  376. {
  377. /* name + size + data */
  378. uint32 size = get_string_size(data_section->name);
  379. size = align_uint(size, 4);
  380. size += (uint32)sizeof(uint32);
  381. size += data_section->size;
  382. return size;
  383. }
  384. static uint32
  385. get_object_data_sections_size(AOTObjectDataSection *data_sections,
  386. uint32 data_sections_count)
  387. {
  388. AOTObjectDataSection *data_section = data_sections;
  389. uint32 size = 0, i;
  390. for (i = 0; i < data_sections_count; i++, data_section++) {
  391. size = align_uint(size, 2);
  392. size += get_object_data_section_size(data_section);
  393. }
  394. return size;
  395. }
  396. static uint32
  397. get_object_data_section_info_size(AOTObjectData *obj_data)
  398. {
  399. /* data sections count + data sections */
  400. return (uint32)sizeof(uint32)
  401. + get_object_data_sections_size(obj_data->data_sections,
  402. obj_data->data_sections_count);
  403. }
  404. static uint32
  405. get_init_data_section_size(AOTCompData *comp_data, AOTObjectData *obj_data)
  406. {
  407. uint32 size = 0;
  408. size += get_mem_info_size(comp_data);
  409. size = align_uint(size, 4);
  410. size += get_table_info_size(comp_data);
  411. size = align_uint(size, 4);
  412. size += get_func_type_info_size(comp_data);
  413. size = align_uint(size, 4);
  414. size += get_import_global_info_size(comp_data);
  415. size = align_uint(size, 4);
  416. size += get_global_info_size(comp_data);
  417. size = align_uint(size, 4);
  418. size += get_import_func_info_size(comp_data);
  419. /* func count + start func index */
  420. size = align_uint(size, 4);
  421. size += (uint32)sizeof(uint32) * 2;
  422. /* aux data/heap/stack data */
  423. size += sizeof(uint32) * 7;
  424. size += get_object_data_section_info_size(obj_data);
  425. return size;
  426. }
  427. static uint32
  428. get_text_section_size(AOTObjectData *obj_data)
  429. {
  430. return (sizeof(uint32) + obj_data->literal_size + obj_data->text_size + 3)
  431. & ~3;
  432. }
  433. static uint32
  434. get_func_section_size(AOTCompData *comp_data, AOTObjectData *obj_data)
  435. {
  436. /* text offsets + function type indexs */
  437. uint32 size = 0;
  438. if (is_32bit_binary(obj_data->binary))
  439. size = (uint32)sizeof(uint32) * comp_data->func_count;
  440. else
  441. size = (uint32)sizeof(uint64) * comp_data->func_count;
  442. size += (uint32)sizeof(uint32) * comp_data->func_count;
  443. return size;
  444. }
  445. static uint32
  446. get_export_size(AOTExport *export)
  447. {
  448. /* export index + export kind + 1 byte padding + export name */
  449. return (uint32)sizeof(uint32) + sizeof(uint8) + 1
  450. + get_string_size(export->name);
  451. }
  452. static uint32
  453. get_exports_size(AOTExport *exports, uint32 export_count)
  454. {
  455. AOTExport *export = exports;
  456. uint32 size = 0, i;
  457. for (i = 0; i < export_count; i++, export ++) {
  458. size = align_uint(size, 4);
  459. size += get_export_size(export);
  460. }
  461. return size;
  462. }
  463. static uint32
  464. get_export_section_size(AOTCompData *comp_data)
  465. {
  466. /* export count + exports */
  467. return (uint32)sizeof(uint32)
  468. + get_exports_size(comp_data->wasm_module->exports,
  469. comp_data->wasm_module->export_count);
  470. }
  471. static uint32
  472. get_relocation_size(AOTRelocation *relocation, bool is_32bin)
  473. {
  474. /* offset + addend + relocation type + symbol name */
  475. uint32 size = 0;
  476. if (is_32bin)
  477. size = sizeof(uint32) * 2; /* offset and addend */
  478. else
  479. size = sizeof(uint64) * 2; /* offset and addend */
  480. size += (uint32)sizeof(uint32); /* relocation type */
  481. size += (uint32)sizeof(uint32); /* symbol name index */
  482. return size;
  483. }
  484. static uint32
  485. get_relocations_size(AOTRelocation *relocations, uint32 relocation_count,
  486. bool is_32bin)
  487. {
  488. AOTRelocation *relocation = relocations;
  489. uint32 size = 0, i;
  490. for (i = 0; i < relocation_count; i++, relocation++) {
  491. size = align_uint(size, 4);
  492. size += get_relocation_size(relocation, is_32bin);
  493. }
  494. return size;
  495. }
  496. static uint32
  497. get_relocation_group_size(AOTRelocationGroup *relocation_group, bool is_32bin)
  498. {
  499. uint32 size = 0;
  500. /* section name index + relocation count + relocations */
  501. size += (uint32)sizeof(uint32);
  502. size += (uint32)sizeof(uint32);
  503. size += get_relocations_size(relocation_group->relocations,
  504. relocation_group->relocation_count, is_32bin);
  505. return size;
  506. }
  507. static uint32
  508. get_relocation_groups_size(AOTRelocationGroup *relocation_groups,
  509. uint32 relocation_group_count, bool is_32bin)
  510. {
  511. AOTRelocationGroup *relocation_group = relocation_groups;
  512. uint32 size = 0, i;
  513. for (i = 0; i < relocation_group_count; i++, relocation_group++) {
  514. size = align_uint(size, 4);
  515. size += get_relocation_group_size(relocation_group, is_32bin);
  516. }
  517. return size;
  518. }
  519. /* return the index (in order of insertion) of the symbol,
  520. create if not exits, -1 if failed */
  521. static uint32
  522. get_relocation_symbol_index(const char *symbol_name, bool *is_new,
  523. AOTSymbolList *symbol_list)
  524. {
  525. AOTSymbolNode *sym;
  526. uint32 index = 0;
  527. sym = symbol_list->head;
  528. while (sym) {
  529. if (!strcmp(sym->symbol, symbol_name)) {
  530. if (is_new)
  531. *is_new = false;
  532. return index;
  533. }
  534. sym = sym->next;
  535. index++;
  536. }
  537. /* Not found in symbol_list, add it */
  538. sym = wasm_runtime_malloc(sizeof(AOTSymbolNode));
  539. if (!sym) {
  540. return (uint32)-1;
  541. }
  542. memset(sym, 0, sizeof(AOTSymbolNode));
  543. sym->symbol = (char *)symbol_name;
  544. sym->str_len = (uint32)strlen(symbol_name);
  545. if (!symbol_list->head) {
  546. symbol_list->head = symbol_list->end = sym;
  547. }
  548. else {
  549. symbol_list->end->next = sym;
  550. symbol_list->end = sym;
  551. }
  552. symbol_list->len++;
  553. if (is_new)
  554. *is_new = true;
  555. return index;
  556. }
  557. static uint32
  558. get_relocation_symbol_size(AOTRelocation *relocation,
  559. AOTSymbolList *symbol_list)
  560. {
  561. uint32 size = 0, index = 0;
  562. bool is_new = false;
  563. index = get_relocation_symbol_index(relocation->symbol_name, &is_new,
  564. symbol_list);
  565. CHECK_SIZE(index);
  566. if (is_new) {
  567. size += (uint32)sizeof(uint16);
  568. size += (uint32)strlen(relocation->symbol_name);
  569. size = align_uint(size, 2);
  570. }
  571. relocation->symbol_index = index;
  572. return size;
  573. }
  574. static uint32
  575. get_relocations_symbol_size(AOTRelocation *relocations, uint32 relocation_count,
  576. AOTSymbolList *symbol_list)
  577. {
  578. AOTRelocation *relocation = relocations;
  579. uint32 size = 0, curr_size, i;
  580. for (i = 0; i < relocation_count; i++, relocation++) {
  581. curr_size = get_relocation_symbol_size(relocation, symbol_list);
  582. CHECK_SIZE(curr_size);
  583. size += curr_size;
  584. }
  585. return size;
  586. }
  587. static uint32
  588. get_relocation_group_symbol_size(AOTRelocationGroup *relocation_group,
  589. AOTSymbolList *symbol_list)
  590. {
  591. uint32 size = 0, index = 0, curr_size;
  592. bool is_new = false;
  593. index = get_relocation_symbol_index(relocation_group->section_name, &is_new,
  594. symbol_list);
  595. CHECK_SIZE(index);
  596. if (is_new) {
  597. size += (uint32)sizeof(uint16);
  598. size += (uint32)strlen(relocation_group->section_name);
  599. size = align_uint(size, 2);
  600. }
  601. relocation_group->name_index = index;
  602. curr_size = get_relocations_symbol_size(relocation_group->relocations,
  603. relocation_group->relocation_count,
  604. symbol_list);
  605. CHECK_SIZE(curr_size);
  606. size += curr_size;
  607. return size;
  608. }
  609. static uint32
  610. get_relocation_groups_symbol_size(AOTRelocationGroup *relocation_groups,
  611. uint32 relocation_group_count,
  612. AOTSymbolList *symbol_list)
  613. {
  614. AOTRelocationGroup *relocation_group = relocation_groups;
  615. uint32 size = 0, curr_size, i;
  616. for (i = 0; i < relocation_group_count; i++, relocation_group++) {
  617. curr_size =
  618. get_relocation_group_symbol_size(relocation_group, symbol_list);
  619. CHECK_SIZE(curr_size);
  620. size += curr_size;
  621. }
  622. return size;
  623. }
  624. static uint32
  625. get_symbol_size_from_symbol_list(AOTSymbolList *symbol_list)
  626. {
  627. AOTSymbolNode *sym;
  628. uint32 size = 0;
  629. sym = symbol_list->head;
  630. while (sym) {
  631. /* (uint16)str_len + str */
  632. size += (uint32)sizeof(uint16) + sym->str_len;
  633. size = align_uint(size, 2);
  634. sym = sym->next;
  635. }
  636. return size;
  637. }
  638. static uint32
  639. get_relocation_section_symbol_size(AOTObjectData *obj_data)
  640. {
  641. AOTRelocationGroup *relocation_groups = obj_data->relocation_groups;
  642. uint32 relocation_group_count = obj_data->relocation_group_count;
  643. uint32 string_count = 0, symbol_table_size = 0;
  644. /* section size will be calculated twice,
  645. get symbol size from symbol list directly in the second calculation */
  646. if (obj_data->symbol_list.len > 0) {
  647. symbol_table_size =
  648. get_symbol_size_from_symbol_list(&obj_data->symbol_list);
  649. }
  650. else {
  651. symbol_table_size = get_relocation_groups_symbol_size(
  652. relocation_groups, relocation_group_count, &obj_data->symbol_list);
  653. }
  654. CHECK_SIZE(symbol_table_size);
  655. string_count = obj_data->symbol_list.len;
  656. /* string_count + string_offsets + total_string_len
  657. + [str (string_len + str)] */
  658. return (uint32)(sizeof(uint32) + sizeof(uint32) * string_count
  659. + sizeof(uint32) + symbol_table_size);
  660. }
  661. static uint32
  662. get_relocation_section_size(AOTObjectData *obj_data)
  663. {
  664. AOTRelocationGroup *relocation_groups = obj_data->relocation_groups;
  665. uint32 relocation_group_count = obj_data->relocation_group_count;
  666. uint32 symbol_table_size = 0;
  667. symbol_table_size = get_relocation_section_symbol_size(obj_data);
  668. CHECK_SIZE(symbol_table_size);
  669. symbol_table_size = align_uint(symbol_table_size, 4);
  670. /* relocation group count + symbol_table + relocation groups */
  671. return (uint32)sizeof(uint32) + symbol_table_size
  672. + get_relocation_groups_size(relocation_groups,
  673. relocation_group_count,
  674. is_32bit_binary(obj_data->binary));
  675. }
  676. static uint32
  677. get_native_symbol_list_size(AOTCompContext *comp_ctx)
  678. {
  679. uint32 len = 0;
  680. AOTNativeSymbol *sym = NULL;
  681. sym = bh_list_first_elem(&comp_ctx->native_symbols);
  682. while (sym) {
  683. len = align_uint(len, 2);
  684. len += get_string_size(sym->symbol);
  685. sym = bh_list_elem_next(sym);
  686. }
  687. return len;
  688. }
  689. static uint32
  690. get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
  691. AOTObjectData *obj_data)
  692. {
  693. uint32 size = 0;
  694. /* aot file header */
  695. size += get_file_header_size();
  696. /* target info section */
  697. size = align_uint(size, 4);
  698. /* section id + section size */
  699. size += (uint32)sizeof(uint32) * 2;
  700. size += get_target_info_section_size();
  701. /* init data section */
  702. size = align_uint(size, 4);
  703. /* section id + section size */
  704. size += (uint32)sizeof(uint32) * 2;
  705. size += get_init_data_section_size(comp_data, obj_data);
  706. /* text section */
  707. size = align_uint(size, 4);
  708. /* section id + section size */
  709. size += (uint32)sizeof(uint32) * 2;
  710. size += get_text_section_size(obj_data);
  711. /* function section */
  712. size = align_uint(size, 4);
  713. /* section id + section size */
  714. size += (uint32)sizeof(uint32) * 2;
  715. size += get_func_section_size(comp_data, obj_data);
  716. /* export section */
  717. size = align_uint(size, 4);
  718. /* section id + section size */
  719. size += (uint32)sizeof(uint32) * 2;
  720. size += get_export_section_size(comp_data);
  721. /* relocation section */
  722. size = align_uint(size, 4);
  723. /* section id + section size */
  724. size += (uint32)sizeof(uint32) * 2;
  725. size += get_relocation_section_size(obj_data);
  726. if (get_native_symbol_list_size(comp_ctx) > 0) {
  727. /* emit only when threre are native symbols */
  728. size = align_uint(size, 4);
  729. /* section id + section size + sub section id + symbol count */
  730. size += (uint32)sizeof(uint32) * 4;
  731. size += get_native_symbol_list_size(comp_ctx);
  732. }
  733. return size;
  734. }
  735. #define exchange_uint8(p_data) (void)0
  736. static void
  737. exchange_uint16(uint8 *p_data)
  738. {
  739. uint8 value = *p_data;
  740. *p_data = *(p_data + 1);
  741. *(p_data + 1) = value;
  742. }
  743. static void
  744. exchange_uint32(uint8 *p_data)
  745. {
  746. uint8 value = *p_data;
  747. *p_data = *(p_data + 3);
  748. *(p_data + 3) = value;
  749. value = *(p_data + 1);
  750. *(p_data + 1) = *(p_data + 2);
  751. *(p_data + 2) = value;
  752. }
  753. static void
  754. exchange_uint64(uint8 *pData)
  755. {
  756. uint32 value;
  757. value = *(uint32 *)pData;
  758. *(uint32 *)pData = *(uint32 *)(pData + 4);
  759. *(uint32 *)(pData + 4) = value;
  760. exchange_uint32(pData);
  761. exchange_uint32(pData + 4);
  762. }
  763. static void
  764. exchange_uint128(uint8 *pData)
  765. {
  766. /* swap high 64bit and low 64bit */
  767. uint64 value = *(uint64 *)pData;
  768. *(uint64 *)pData = *(uint64 *)(pData + 8);
  769. *(uint64 *)(pData + 8) = value;
  770. /* exchange high 64bit */
  771. exchange_uint64(pData);
  772. /* exchange low 64bit */
  773. exchange_uint64(pData + 8);
  774. }
  775. static union {
  776. int a;
  777. char b;
  778. } __ue = { .a = 1 };
  779. #define is_little_endian() (__ue.b == 1)
  780. #define CHECK_BUF(length) \
  781. do { \
  782. if (buf + offset + length > buf_end) { \
  783. aot_set_last_error("buf overflow"); \
  784. return false; \
  785. } \
  786. } while (0)
  787. #define EMIT_U8(v) \
  788. do { \
  789. CHECK_BUF(1); \
  790. *(uint8 *)(buf + offset) = (uint8)v; \
  791. offset++; \
  792. } while (0)
  793. #define EMIT_U16(v) \
  794. do { \
  795. uint16 t = (uint16)v; \
  796. CHECK_BUF(2); \
  797. if (!is_little_endian()) \
  798. exchange_uint16((uint8 *)&t); \
  799. *(uint16 *)(buf + offset) = t; \
  800. offset += (uint32)sizeof(uint16); \
  801. } while (0)
  802. #define EMIT_U32(v) \
  803. do { \
  804. uint32 t = (uint32)v; \
  805. CHECK_BUF(4); \
  806. if (!is_little_endian()) \
  807. exchange_uint32((uint8 *)&t); \
  808. *(uint32 *)(buf + offset) = t; \
  809. offset += (uint32)sizeof(uint32); \
  810. } while (0)
  811. #define EMIT_U64(v) \
  812. do { \
  813. uint64 t = (uint64)v; \
  814. CHECK_BUF(8); \
  815. if (!is_little_endian()) \
  816. exchange_uint64((uint8 *)&t); \
  817. PUT_U64_TO_ADDR(buf + offset, t); \
  818. offset += (uint32)sizeof(uint64); \
  819. } while (0)
  820. #define EMIT_V128(v) \
  821. do { \
  822. uint64 *t = (uint64 *)v.i64x2; \
  823. CHECK_BUF(16); \
  824. if (!is_little_endian()) \
  825. exchange_uint128((uint8 *)t); \
  826. PUT_U64_TO_ADDR(buf + offset, t[0]); \
  827. offset += (uint32)sizeof(uint64); \
  828. PUT_U64_TO_ADDR(buf + offset, t[1]); \
  829. offset += (uint32)sizeof(uint64); \
  830. } while (0)
  831. #define EMIT_BUF(v, len) \
  832. do { \
  833. CHECK_BUF(len); \
  834. memcpy(buf + offset, v, len); \
  835. offset += len; \
  836. } while (0)
  837. #define EMIT_STR(s) \
  838. do { \
  839. uint32 str_len = (uint32)strlen(s); \
  840. EMIT_U16(str_len); \
  841. EMIT_BUF(s, str_len); \
  842. } while (0)
  843. static bool
  844. aot_emit_file_header(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  845. AOTCompData *comp_data, AOTObjectData *obj_data)
  846. {
  847. uint32 offset = *p_offset;
  848. uint32 aot_curr_version = AOT_CURRENT_VERSION;
  849. EMIT_U8('\0');
  850. EMIT_U8('a');
  851. EMIT_U8('o');
  852. EMIT_U8('t');
  853. EMIT_U32(aot_curr_version);
  854. *p_offset = offset;
  855. return true;
  856. }
  857. static bool
  858. aot_emit_target_info_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  859. AOTCompData *comp_data, AOTObjectData *obj_data)
  860. {
  861. uint32 offset = *p_offset;
  862. uint32 section_size = get_target_info_section_size();
  863. AOTTargetInfo *target_info = &obj_data->target_info;
  864. *p_offset = offset = align_uint(offset, 4);
  865. EMIT_U32(AOT_SECTION_TYPE_TARGET_INFO);
  866. EMIT_U32(section_size);
  867. EMIT_U16(target_info->bin_type);
  868. EMIT_U16(target_info->abi_type);
  869. EMIT_U16(target_info->e_type);
  870. EMIT_U16(target_info->e_machine);
  871. EMIT_U32(target_info->e_version);
  872. EMIT_U32(target_info->e_flags);
  873. EMIT_U32(target_info->reserved);
  874. EMIT_BUF(target_info->arch, sizeof(target_info->arch));
  875. if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
  876. aot_set_last_error("emit target info failed.");
  877. return false;
  878. }
  879. *p_offset = offset;
  880. return true;
  881. }
  882. static bool
  883. aot_emit_mem_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  884. AOTCompContext *comp_ctx, AOTCompData *comp_data,
  885. AOTObjectData *obj_data)
  886. {
  887. uint32 offset = *p_offset, i;
  888. AOTMemInitData **init_datas = comp_data->mem_init_data_list;
  889. *p_offset = offset = align_uint(offset, 4);
  890. /* Emit import memory count, only emit 0 currently.
  891. TODO: emit the actual import memory count and
  892. the full import memory info. */
  893. EMIT_U32(0);
  894. /* Emit memory count */
  895. EMIT_U32(comp_data->memory_count);
  896. /* Emit memory items */
  897. for (i = 0; i < comp_data->memory_count; i++) {
  898. EMIT_U32(comp_data->memories[i].memory_flags);
  899. EMIT_U32(comp_data->memories[i].num_bytes_per_page);
  900. EMIT_U32(comp_data->memories[i].mem_init_page_count);
  901. EMIT_U32(comp_data->memories[i].mem_max_page_count);
  902. }
  903. /* Emit mem init data count */
  904. EMIT_U32(comp_data->mem_init_data_count);
  905. /* Emit mem init data items */
  906. for (i = 0; i < comp_data->mem_init_data_count; i++) {
  907. offset = align_uint(offset, 4);
  908. #if WASM_ENABLE_BULK_MEMORY != 0
  909. if (comp_ctx->enable_bulk_memory) {
  910. EMIT_U32(init_datas[i]->is_passive);
  911. EMIT_U32(init_datas[i]->memory_index);
  912. }
  913. else
  914. #endif
  915. {
  916. /* emit two placeholder to keep the same size */
  917. EMIT_U32(0);
  918. EMIT_U32(0);
  919. }
  920. EMIT_U32(init_datas[i]->offset.init_expr_type);
  921. EMIT_U64(init_datas[i]->offset.u.i64);
  922. EMIT_U32(init_datas[i]->byte_count);
  923. EMIT_BUF(init_datas[i]->bytes, init_datas[i]->byte_count);
  924. }
  925. if (offset - *p_offset != get_mem_info_size(comp_data)) {
  926. aot_set_last_error("emit memory info failed.");
  927. return false;
  928. }
  929. *p_offset = offset;
  930. return true;
  931. }
  932. static bool
  933. aot_emit_table_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  934. AOTCompData *comp_data, AOTObjectData *obj_data)
  935. {
  936. uint32 offset = *p_offset, i, j;
  937. AOTTableInitData **init_datas = comp_data->table_init_data_list;
  938. *p_offset = offset = align_uint(offset, 4);
  939. /* Emit import table count */
  940. EMIT_U32(comp_data->import_table_count);
  941. /* Emit table items */
  942. for (i = 0; i < comp_data->import_table_count; i++) {
  943. /* TODO:
  944. * EMIT_STR(comp_data->import_tables[i].module_name );
  945. * EMIT_STR(comp_data->import_tables[i].table_name);
  946. */
  947. EMIT_U32(comp_data->import_tables[i].table_init_size);
  948. EMIT_U32(comp_data->import_tables[i].table_max_size);
  949. EMIT_U32(comp_data->import_tables[i].possible_grow & 0x000000FF);
  950. }
  951. /* Emit table count */
  952. EMIT_U32(comp_data->table_count);
  953. /* Emit table items */
  954. for (i = 0; i < comp_data->table_count; i++) {
  955. EMIT_U32(comp_data->tables[i].elem_type);
  956. EMIT_U32(comp_data->tables[i].table_flags);
  957. EMIT_U32(comp_data->tables[i].table_init_size);
  958. EMIT_U32(comp_data->tables[i].table_max_size);
  959. EMIT_U32(comp_data->tables[i].possible_grow & 0x000000FF);
  960. }
  961. /* Emit table init data count */
  962. EMIT_U32(comp_data->table_init_data_count);
  963. /* Emit table init data items */
  964. for (i = 0; i < comp_data->table_init_data_count; i++) {
  965. offset = align_uint(offset, 4);
  966. EMIT_U32(init_datas[i]->mode);
  967. EMIT_U32(init_datas[i]->elem_type);
  968. EMIT_U32(init_datas[i]->table_index);
  969. EMIT_U32(init_datas[i]->offset.init_expr_type);
  970. EMIT_U64(init_datas[i]->offset.u.i64);
  971. EMIT_U32(init_datas[i]->func_index_count);
  972. for (j = 0; j < init_datas[i]->func_index_count; j++)
  973. EMIT_U32(init_datas[i]->func_indexes[j]);
  974. }
  975. if (offset - *p_offset != get_table_info_size(comp_data)) {
  976. aot_set_last_error("emit table info failed.");
  977. return false;
  978. }
  979. *p_offset = offset;
  980. return true;
  981. }
  982. static bool
  983. aot_emit_func_type_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  984. AOTCompData *comp_data, AOTObjectData *obj_data)
  985. {
  986. uint32 offset = *p_offset, i;
  987. AOTFuncType **func_types = comp_data->func_types;
  988. *p_offset = offset = align_uint(offset, 4);
  989. EMIT_U32(comp_data->func_type_count);
  990. for (i = 0; i < comp_data->func_type_count; i++) {
  991. offset = align_uint(offset, 4);
  992. EMIT_U32(func_types[i]->param_count);
  993. EMIT_U32(func_types[i]->result_count);
  994. EMIT_BUF(func_types[i]->types,
  995. func_types[i]->param_count + func_types[i]->result_count);
  996. }
  997. if (offset - *p_offset != get_func_type_info_size(comp_data)) {
  998. aot_set_last_error("emit function type info failed.");
  999. return false;
  1000. }
  1001. *p_offset = offset;
  1002. return true;
  1003. }
  1004. static bool
  1005. aot_emit_import_global_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1006. AOTCompData *comp_data, AOTObjectData *obj_data)
  1007. {
  1008. uint32 offset = *p_offset, i;
  1009. AOTImportGlobal *import_global = comp_data->import_globals;
  1010. *p_offset = offset = align_uint(offset, 4);
  1011. EMIT_U32(comp_data->import_global_count);
  1012. for (i = 0; i < comp_data->import_global_count; i++, import_global++) {
  1013. offset = align_uint(offset, 2);
  1014. EMIT_U8(import_global->type);
  1015. EMIT_U8(import_global->is_mutable);
  1016. EMIT_STR(import_global->module_name);
  1017. offset = align_uint(offset, 2);
  1018. EMIT_STR(import_global->global_name);
  1019. }
  1020. if (offset - *p_offset != get_import_global_info_size(comp_data)) {
  1021. aot_set_last_error("emit import global info failed.");
  1022. return false;
  1023. }
  1024. *p_offset = offset;
  1025. return true;
  1026. }
  1027. static bool
  1028. aot_emit_global_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1029. AOTCompData *comp_data, AOTObjectData *obj_data)
  1030. {
  1031. uint32 offset = *p_offset, i;
  1032. AOTGlobal *global = comp_data->globals;
  1033. *p_offset = offset = align_uint(offset, 4);
  1034. EMIT_U32(comp_data->global_count);
  1035. for (i = 0; i < comp_data->global_count; i++, global++) {
  1036. offset = align_uint(offset, 4);
  1037. EMIT_U8(global->type);
  1038. EMIT_U8(global->is_mutable);
  1039. EMIT_U16(global->init_expr.init_expr_type);
  1040. if (global->init_expr.init_expr_type != INIT_EXPR_TYPE_V128_CONST)
  1041. EMIT_U64(global->init_expr.u.i64);
  1042. else
  1043. EMIT_V128(global->init_expr.u.v128);
  1044. }
  1045. if (offset - *p_offset != get_global_info_size(comp_data)) {
  1046. aot_set_last_error("emit global info failed.");
  1047. return false;
  1048. }
  1049. *p_offset = offset;
  1050. return true;
  1051. }
  1052. static bool
  1053. aot_emit_import_func_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1054. AOTCompData *comp_data, AOTObjectData *obj_data)
  1055. {
  1056. uint32 offset = *p_offset, i;
  1057. AOTImportFunc *import_func = comp_data->import_funcs;
  1058. *p_offset = offset = align_uint(offset, 4);
  1059. EMIT_U32(comp_data->import_func_count);
  1060. for (i = 0; i < comp_data->import_func_count; i++, import_func++) {
  1061. offset = align_uint(offset, 2);
  1062. EMIT_U16(import_func->func_type_index);
  1063. EMIT_STR(import_func->module_name);
  1064. offset = align_uint(offset, 2);
  1065. EMIT_STR(import_func->func_name);
  1066. }
  1067. if (offset - *p_offset != get_import_func_info_size(comp_data)) {
  1068. aot_set_last_error("emit import function info failed.");
  1069. return false;
  1070. }
  1071. *p_offset = offset;
  1072. return true;
  1073. }
  1074. static bool
  1075. aot_emit_object_data_section_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1076. AOTObjectData *obj_data)
  1077. {
  1078. uint32 offset = *p_offset, i;
  1079. AOTObjectDataSection *data_section = obj_data->data_sections;
  1080. *p_offset = offset = align_uint(offset, 4);
  1081. EMIT_U32(obj_data->data_sections_count);
  1082. for (i = 0; i < obj_data->data_sections_count; i++, data_section++) {
  1083. offset = align_uint(offset, 2);
  1084. EMIT_STR(data_section->name);
  1085. offset = align_uint(offset, 4);
  1086. EMIT_U32(data_section->size);
  1087. EMIT_BUF(data_section->data, data_section->size);
  1088. }
  1089. if (offset - *p_offset != get_object_data_section_info_size(obj_data)) {
  1090. aot_set_last_error("emit object data section info failed.");
  1091. return false;
  1092. }
  1093. *p_offset = offset;
  1094. return true;
  1095. }
  1096. static bool
  1097. aot_emit_init_data_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1098. AOTCompContext *comp_ctx, AOTCompData *comp_data,
  1099. AOTObjectData *obj_data)
  1100. {
  1101. uint32 section_size = get_init_data_section_size(comp_data, obj_data);
  1102. uint32 offset = *p_offset;
  1103. *p_offset = offset = align_uint(offset, 4);
  1104. EMIT_U32(AOT_SECTION_TYPE_INIT_DATA);
  1105. EMIT_U32(section_size);
  1106. if (!aot_emit_mem_info(buf, buf_end, &offset, comp_ctx, comp_data, obj_data)
  1107. || !aot_emit_table_info(buf, buf_end, &offset, comp_data, obj_data)
  1108. || !aot_emit_func_type_info(buf, buf_end, &offset, comp_data, obj_data)
  1109. || !aot_emit_import_global_info(buf, buf_end, &offset, comp_data,
  1110. obj_data)
  1111. || !aot_emit_global_info(buf, buf_end, &offset, comp_data, obj_data)
  1112. || !aot_emit_import_func_info(buf, buf_end, &offset, comp_data,
  1113. obj_data))
  1114. return false;
  1115. offset = align_uint(offset, 4);
  1116. EMIT_U32(comp_data->func_count);
  1117. EMIT_U32(comp_data->start_func_index);
  1118. EMIT_U32(comp_data->aux_data_end_global_index);
  1119. EMIT_U32(comp_data->aux_data_end);
  1120. EMIT_U32(comp_data->aux_heap_base_global_index);
  1121. EMIT_U32(comp_data->aux_heap_base);
  1122. EMIT_U32(comp_data->aux_stack_top_global_index);
  1123. EMIT_U32(comp_data->aux_stack_bottom);
  1124. EMIT_U32(comp_data->aux_stack_size);
  1125. if (!aot_emit_object_data_section_info(buf, buf_end, &offset, obj_data))
  1126. return false;
  1127. if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
  1128. aot_set_last_error("emit init data section failed.");
  1129. return false;
  1130. }
  1131. *p_offset = offset;
  1132. return true;
  1133. }
  1134. static bool
  1135. aot_emit_text_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1136. AOTCompData *comp_data, AOTObjectData *obj_data)
  1137. {
  1138. uint32 section_size = get_text_section_size(obj_data);
  1139. uint32 offset = *p_offset;
  1140. uint8 placeholder = 0;
  1141. *p_offset = offset = align_uint(offset, 4);
  1142. EMIT_U32(AOT_SECTION_TYPE_TEXT);
  1143. EMIT_U32(section_size);
  1144. EMIT_U32(obj_data->literal_size);
  1145. if (obj_data->literal_size > 0)
  1146. EMIT_BUF(obj_data->literal, obj_data->literal_size);
  1147. EMIT_BUF(obj_data->text, obj_data->text_size);
  1148. while (offset & 3)
  1149. EMIT_BUF(&placeholder, 1);
  1150. if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
  1151. aot_set_last_error("emit text section failed.");
  1152. return false;
  1153. }
  1154. *p_offset = offset;
  1155. return true;
  1156. }
  1157. static bool
  1158. aot_emit_func_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1159. AOTCompData *comp_data, AOTObjectData *obj_data)
  1160. {
  1161. uint32 section_size = get_func_section_size(comp_data, obj_data);
  1162. uint32 i, offset = *p_offset;
  1163. AOTObjectFunc *func = obj_data->funcs;
  1164. AOTFunc **funcs = comp_data->funcs;
  1165. *p_offset = offset = align_uint(offset, 4);
  1166. EMIT_U32(AOT_SECTION_TYPE_FUNCTION);
  1167. EMIT_U32(section_size);
  1168. for (i = 0; i < obj_data->func_count; i++, func++) {
  1169. if (is_32bit_binary(obj_data->binary))
  1170. EMIT_U32(func->text_offset);
  1171. else
  1172. EMIT_U64(func->text_offset);
  1173. }
  1174. for (i = 0; i < comp_data->func_count; i++)
  1175. EMIT_U32(funcs[i]->func_type_index);
  1176. if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
  1177. aot_set_last_error("emit function section failed.");
  1178. return false;
  1179. }
  1180. *p_offset = offset;
  1181. return true;
  1182. }
  1183. static bool
  1184. aot_emit_export_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1185. AOTCompData *comp_data, AOTObjectData *obj_data)
  1186. {
  1187. uint32 section_size = get_export_section_size(comp_data);
  1188. AOTExport *export = comp_data->wasm_module->exports;
  1189. uint32 export_count = comp_data->wasm_module->export_count;
  1190. uint32 i, offset = *p_offset;
  1191. *p_offset = offset = align_uint(offset, 4);
  1192. EMIT_U32(AOT_SECTION_TYPE_EXPORT);
  1193. EMIT_U32(section_size);
  1194. EMIT_U32(export_count);
  1195. for (i = 0; i < export_count; i++, export ++) {
  1196. offset = align_uint(offset, 4);
  1197. EMIT_U32(export->index);
  1198. EMIT_U8(export->kind);
  1199. EMIT_U8(0);
  1200. EMIT_STR(export->name);
  1201. }
  1202. if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
  1203. aot_set_last_error("emit export section failed.");
  1204. return false;
  1205. }
  1206. *p_offset = offset;
  1207. return true;
  1208. }
  1209. static bool
  1210. aot_emit_relocation_symbol_table(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1211. AOTCompData *comp_data,
  1212. AOTObjectData *obj_data)
  1213. {
  1214. uint32 symbol_offset = 0, total_string_len = 0;
  1215. uint32 offset = *p_offset;
  1216. AOTSymbolNode *sym;
  1217. EMIT_U32(obj_data->symbol_list.len);
  1218. /* emit symbol offsets */
  1219. sym = (AOTSymbolNode *)(obj_data->symbol_list.head);
  1220. while (sym) {
  1221. EMIT_U32(symbol_offset);
  1222. /* string_len + str[0 .. string_len - 1] */
  1223. symbol_offset += (uint32)sizeof(uint16) + sym->str_len;
  1224. symbol_offset = align_uint(symbol_offset, 2);
  1225. sym = sym->next;
  1226. }
  1227. /* emit total string len */
  1228. total_string_len = symbol_offset;
  1229. EMIT_U32(total_string_len);
  1230. /* emit symbols */
  1231. sym = (AOTSymbolNode *)(obj_data->symbol_list.head);
  1232. while (sym) {
  1233. EMIT_STR(sym->symbol);
  1234. offset = align_uint(offset, 2);
  1235. sym = sym->next;
  1236. }
  1237. *p_offset = offset;
  1238. return true;
  1239. }
  1240. static bool
  1241. aot_emit_relocation_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1242. AOTCompData *comp_data, AOTObjectData *obj_data)
  1243. {
  1244. uint32 section_size = get_relocation_section_size(obj_data);
  1245. uint32 i, offset = *p_offset;
  1246. AOTRelocationGroup *relocation_group = obj_data->relocation_groups;
  1247. if (section_size == (uint32)-1)
  1248. return false;
  1249. *p_offset = offset = align_uint(offset, 4);
  1250. EMIT_U32(AOT_SECTION_TYPE_RELOCATION);
  1251. EMIT_U32(section_size);
  1252. aot_emit_relocation_symbol_table(buf, buf_end, &offset, comp_data,
  1253. obj_data);
  1254. offset = align_uint(offset, 4);
  1255. EMIT_U32(obj_data->relocation_group_count);
  1256. /* emit each relocation group */
  1257. for (i = 0; i < obj_data->relocation_group_count; i++, relocation_group++) {
  1258. AOTRelocation *relocation = relocation_group->relocations;
  1259. uint32 j;
  1260. offset = align_uint(offset, 4);
  1261. EMIT_U32(relocation_group->name_index);
  1262. offset = align_uint(offset, 4);
  1263. EMIT_U32(relocation_group->relocation_count);
  1264. /* emit each relocation */
  1265. for (j = 0; j < relocation_group->relocation_count; j++, relocation++) {
  1266. offset = align_uint(offset, 4);
  1267. if (is_32bit_binary(obj_data->binary)) {
  1268. EMIT_U32(relocation->relocation_offset);
  1269. EMIT_U32(relocation->relocation_addend);
  1270. }
  1271. else {
  1272. EMIT_U64(relocation->relocation_offset);
  1273. EMIT_U64(relocation->relocation_addend);
  1274. }
  1275. EMIT_U32(relocation->relocation_type);
  1276. EMIT_U32(relocation->symbol_index);
  1277. }
  1278. }
  1279. if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
  1280. aot_set_last_error("emit relocation section failed.");
  1281. return false;
  1282. }
  1283. *p_offset = offset;
  1284. return true;
  1285. }
  1286. static bool
  1287. aot_emit_native_symbol(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
  1288. AOTCompContext *comp_ctx)
  1289. {
  1290. uint32 offset = *p_offset;
  1291. AOTNativeSymbol *sym = NULL;
  1292. if (bh_list_length(&comp_ctx->native_symbols) == 0)
  1293. /* emit only when threre are native symbols */
  1294. return true;
  1295. *p_offset = offset = align_uint(offset, 4);
  1296. EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
  1297. /* sub section id + symbol count + symbol list */
  1298. EMIT_U32(sizeof(uint32) * 2 + get_native_symbol_list_size(comp_ctx));
  1299. EMIT_U32(AOT_CUSTOM_SECTION_NATIVE_SYMBOL);
  1300. EMIT_U32(bh_list_length(&comp_ctx->native_symbols));
  1301. sym = bh_list_first_elem(&comp_ctx->native_symbols);
  1302. while (sym) {
  1303. offset = align_uint(offset, 2);
  1304. EMIT_STR(sym->symbol);
  1305. sym = bh_list_elem_next(sym);
  1306. }
  1307. *p_offset = offset;
  1308. return true;
  1309. }
  1310. typedef uint32 U32;
  1311. typedef int32 I32;
  1312. typedef uint16 U16;
  1313. typedef uint8 U8;
  1314. struct coff_hdr {
  1315. U16 u16Machine;
  1316. U16 u16NumSections;
  1317. U32 u32DateTimeStamp;
  1318. U32 u32SymTblPtr;
  1319. U32 u32NumSymbols;
  1320. U16 u16PeHdrSize;
  1321. U16 u16Characs;
  1322. };
  1323. #define IMAGE_FILE_MACHINE_AMD64 0x8664
  1324. #define IMAGE_FILE_MACHINE_I386 0x014c
  1325. #define IMAGE_FILE_MACHINE_IA64 0x0200
  1326. #define AOT_COFF_BIN_TYPE 6
  1327. #define EI_NIDENT 16
  1328. typedef uint32 elf32_word;
  1329. typedef int32 elf32_sword;
  1330. typedef uint16 elf32_half;
  1331. typedef uint32 elf32_off;
  1332. typedef uint32 elf32_addr;
  1333. struct elf32_ehdr {
  1334. unsigned char e_ident[EI_NIDENT]; /* ident bytes */
  1335. elf32_half e_type; /* file type */
  1336. elf32_half e_machine; /* target machine */
  1337. elf32_word e_version; /* file version */
  1338. elf32_addr e_entry; /* start address */
  1339. elf32_off e_phoff; /* phdr file offset */
  1340. elf32_off e_shoff; /* shdr file offset */
  1341. elf32_word e_flags; /* file flags */
  1342. elf32_half e_ehsize; /* sizeof ehdr */
  1343. elf32_half e_phentsize; /* sizeof phdr */
  1344. elf32_half e_phnum; /* number phdrs */
  1345. elf32_half e_shentsize; /* sizeof shdr */
  1346. elf32_half e_shnum; /* number shdrs */
  1347. elf32_half e_shstrndx; /* shdr string index */
  1348. };
  1349. struct elf32_rel {
  1350. elf32_addr r_offset;
  1351. elf32_word r_info;
  1352. } elf32_rel;
  1353. struct elf32_rela {
  1354. elf32_addr r_offset;
  1355. elf32_word r_info;
  1356. elf32_sword r_addend;
  1357. } elf32_rela;
  1358. typedef uint32 elf64_word;
  1359. typedef int32 elf64_sword;
  1360. typedef uint64 elf64_xword;
  1361. typedef int64 elf64_sxword;
  1362. typedef uint16 elf64_half;
  1363. typedef uint64 elf64_off;
  1364. typedef uint64 elf64_addr;
  1365. struct elf64_ehdr {
  1366. unsigned char e_ident[EI_NIDENT]; /* ident bytes */
  1367. elf64_half e_type; /* file type */
  1368. elf64_half e_machine; /* target machine */
  1369. elf64_word e_version; /* file version */
  1370. elf64_addr e_entry; /* start address */
  1371. elf64_off e_phoff; /* phdr file offset */
  1372. elf64_off e_shoff; /* shdr file offset */
  1373. elf64_word e_flags; /* file flags */
  1374. elf64_half e_ehsize; /* sizeof ehdr */
  1375. elf64_half e_phentsize; /* sizeof phdr */
  1376. elf64_half e_phnum; /* number phdrs */
  1377. elf64_half e_shentsize; /* sizeof shdr */
  1378. elf64_half e_shnum; /* number shdrs */
  1379. elf64_half e_shstrndx; /* shdr string index */
  1380. };
  1381. typedef struct elf64_rel {
  1382. elf64_addr r_offset;
  1383. elf64_xword r_info;
  1384. } elf64_rel;
  1385. typedef struct elf64_rela {
  1386. elf64_addr r_offset;
  1387. elf64_xword r_info;
  1388. elf64_sxword r_addend;
  1389. } elf64_rela;
  1390. #define SET_TARGET_INFO(f, v, type, little) \
  1391. do { \
  1392. type tmp = elf_header->v; \
  1393. if ((little && !is_little_endian()) \
  1394. || (!little && is_little_endian())) \
  1395. exchange_##type((uint8 *)&tmp); \
  1396. obj_data->target_info.f = tmp; \
  1397. } while (0)
  1398. static bool
  1399. aot_resolve_target_info(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
  1400. {
  1401. LLVMBinaryType bin_type = LLVMBinaryGetType(obj_data->binary);
  1402. const uint8 *elf_buf = (uint8 *)LLVMGetBufferStart(obj_data->mem_buf);
  1403. uint32 elf_size = (uint32)LLVMGetBufferSize(obj_data->mem_buf);
  1404. if (bin_type != LLVMBinaryTypeCOFF && bin_type != LLVMBinaryTypeELF32L
  1405. && bin_type != LLVMBinaryTypeELF32B && bin_type != LLVMBinaryTypeELF64L
  1406. && bin_type != LLVMBinaryTypeELF64B
  1407. && bin_type != LLVMBinaryTypeMachO32L
  1408. && bin_type != LLVMBinaryTypeMachO32B
  1409. && bin_type != LLVMBinaryTypeMachO64L
  1410. && bin_type != LLVMBinaryTypeMachO64B) {
  1411. aot_set_last_error("invaid llvm binary bin_type.");
  1412. return false;
  1413. }
  1414. obj_data->target_info.bin_type = bin_type - LLVMBinaryTypeELF32L;
  1415. if (bin_type == LLVMBinaryTypeCOFF) {
  1416. struct coff_hdr *coff_header;
  1417. if (!elf_buf || elf_size < sizeof(struct coff_hdr)) {
  1418. aot_set_last_error("invalid coff_hdr buffer.");
  1419. return false;
  1420. }
  1421. coff_header = (struct coff_hdr *)elf_buf;
  1422. obj_data->target_info.e_type = 1;
  1423. obj_data->target_info.e_machine = coff_header->u16Machine;
  1424. obj_data->target_info.e_version = 1;
  1425. obj_data->target_info.e_flags = 0;
  1426. if (coff_header->u16Machine == IMAGE_FILE_MACHINE_AMD64)
  1427. obj_data->target_info.bin_type = AOT_COFF_BIN_TYPE;
  1428. }
  1429. else if (bin_type == LLVMBinaryTypeELF32L
  1430. || bin_type == LLVMBinaryTypeELF32B) {
  1431. struct elf32_ehdr *elf_header;
  1432. bool is_little_bin = bin_type == LLVMBinaryTypeELF32L;
  1433. if (!elf_buf || elf_size < sizeof(struct elf32_ehdr)) {
  1434. aot_set_last_error("invalid elf32 buffer.");
  1435. return false;
  1436. }
  1437. elf_header = (struct elf32_ehdr *)elf_buf;
  1438. SET_TARGET_INFO(e_type, e_type, uint16, is_little_bin);
  1439. SET_TARGET_INFO(e_machine, e_machine, uint16, is_little_bin);
  1440. SET_TARGET_INFO(e_version, e_version, uint32, is_little_bin);
  1441. SET_TARGET_INFO(e_flags, e_flags, uint32, is_little_bin);
  1442. }
  1443. else if (bin_type == LLVMBinaryTypeELF64L
  1444. || bin_type == LLVMBinaryTypeELF64B) {
  1445. struct elf64_ehdr *elf_header;
  1446. bool is_little_bin = bin_type == LLVMBinaryTypeELF64L;
  1447. if (!elf_buf || elf_size < sizeof(struct elf64_ehdr)) {
  1448. aot_set_last_error("invalid elf64 buffer.");
  1449. return false;
  1450. }
  1451. elf_header = (struct elf64_ehdr *)elf_buf;
  1452. SET_TARGET_INFO(e_type, e_type, uint16, is_little_bin);
  1453. SET_TARGET_INFO(e_machine, e_machine, uint16, is_little_bin);
  1454. SET_TARGET_INFO(e_version, e_version, uint32, is_little_bin);
  1455. SET_TARGET_INFO(e_flags, e_flags, uint32, is_little_bin);
  1456. }
  1457. else if (bin_type == LLVMBinaryTypeMachO32L
  1458. || bin_type == LLVMBinaryTypeMachO32B) {
  1459. /* TODO: parse file type of Mach-O 32 */
  1460. aot_set_last_error("invaid llvm binary bin_type.");
  1461. return false;
  1462. }
  1463. else if (bin_type == LLVMBinaryTypeMachO64L
  1464. || bin_type == LLVMBinaryTypeMachO64B) {
  1465. /* TODO: parse file type of Mach-O 64 */
  1466. aot_set_last_error("invaid llvm binary bin_type.");
  1467. return false;
  1468. }
  1469. strncpy(obj_data->target_info.arch, comp_ctx->target_arch,
  1470. sizeof(obj_data->target_info.arch));
  1471. return true;
  1472. }
  1473. static bool
  1474. aot_resolve_text(AOTObjectData *obj_data)
  1475. {
  1476. #if WASM_ENABLE_DEBUG_AOT != 0
  1477. LLVMBinaryType bin_type = LLVMBinaryGetType(obj_data->binary);
  1478. if (bin_type == LLVMBinaryTypeELF32L || bin_type == LLVMBinaryTypeELF64L) {
  1479. obj_data->text = (char *)LLVMGetBufferStart(obj_data->mem_buf);
  1480. obj_data->text_size = (uint32)LLVMGetBufferSize(obj_data->mem_buf);
  1481. }
  1482. else
  1483. #endif
  1484. {
  1485. LLVMSectionIteratorRef sec_itr;
  1486. char *name;
  1487. if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  1488. aot_set_last_error("llvm get section iterator failed.");
  1489. return false;
  1490. }
  1491. while (
  1492. !LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
  1493. if ((name = (char *)LLVMGetSectionName(sec_itr))
  1494. && !strcmp(name, ".text")) {
  1495. obj_data->text = (char *)LLVMGetSectionContents(sec_itr);
  1496. obj_data->text_size = (uint32)LLVMGetSectionSize(sec_itr);
  1497. break;
  1498. }
  1499. LLVMMoveToNextSection(sec_itr);
  1500. }
  1501. LLVMDisposeSectionIterator(sec_itr);
  1502. }
  1503. return true;
  1504. }
  1505. static bool
  1506. aot_resolve_literal(AOTObjectData *obj_data)
  1507. {
  1508. LLVMSectionIteratorRef sec_itr;
  1509. char *name;
  1510. if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  1511. aot_set_last_error("llvm get section iterator failed.");
  1512. return false;
  1513. }
  1514. while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
  1515. if ((name = (char *)LLVMGetSectionName(sec_itr))
  1516. && !strcmp(name, ".literal")) {
  1517. obj_data->literal = (char *)LLVMGetSectionContents(sec_itr);
  1518. obj_data->literal_size = (uint32)LLVMGetSectionSize(sec_itr);
  1519. break;
  1520. }
  1521. LLVMMoveToNextSection(sec_itr);
  1522. }
  1523. LLVMDisposeSectionIterator(sec_itr);
  1524. return true;
  1525. }
  1526. static bool
  1527. get_relocations_count(LLVMSectionIteratorRef sec_itr, uint32 *p_count);
  1528. static bool
  1529. is_data_section(LLVMSectionIteratorRef sec_itr, char *section_name)
  1530. {
  1531. uint32 relocation_count = 0;
  1532. return (!strcmp(section_name, ".data") || !strcmp(section_name, ".sdata")
  1533. || !strcmp(section_name, ".rodata")
  1534. /* ".rodata.cst4/8/16/.." */
  1535. || !strncmp(section_name, ".rodata.cst", strlen(".rodata.cst"))
  1536. || (!strcmp(section_name, ".rdata")
  1537. && get_relocations_count(sec_itr, &relocation_count)
  1538. && relocation_count > 0));
  1539. }
  1540. static bool
  1541. get_object_data_sections_count(AOTObjectData *obj_data, uint32 *p_count)
  1542. {
  1543. LLVMSectionIteratorRef sec_itr;
  1544. char *name;
  1545. uint32 count = 0;
  1546. if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  1547. aot_set_last_error("llvm get section iterator failed.");
  1548. return false;
  1549. }
  1550. while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
  1551. if ((name = (char *)LLVMGetSectionName(sec_itr))
  1552. && (is_data_section(sec_itr, name))) {
  1553. count++;
  1554. }
  1555. LLVMMoveToNextSection(sec_itr);
  1556. }
  1557. LLVMDisposeSectionIterator(sec_itr);
  1558. *p_count = count;
  1559. return true;
  1560. }
  1561. static bool
  1562. aot_resolve_object_data_sections(AOTObjectData *obj_data)
  1563. {
  1564. LLVMSectionIteratorRef sec_itr;
  1565. char *name;
  1566. AOTObjectDataSection *data_section;
  1567. uint32 sections_count;
  1568. uint32 size;
  1569. if (!get_object_data_sections_count(obj_data, &sections_count)) {
  1570. return false;
  1571. }
  1572. if (sections_count > 0) {
  1573. size = (uint32)sizeof(AOTObjectDataSection) * sections_count;
  1574. if (!(data_section = obj_data->data_sections =
  1575. wasm_runtime_malloc(size))) {
  1576. aot_set_last_error("allocate memory for data sections failed.");
  1577. return false;
  1578. }
  1579. memset(obj_data->data_sections, 0, size);
  1580. obj_data->data_sections_count = sections_count;
  1581. if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  1582. aot_set_last_error("llvm get section iterator failed.");
  1583. return false;
  1584. }
  1585. while (
  1586. !LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
  1587. if ((name = (char *)LLVMGetSectionName(sec_itr))
  1588. && (is_data_section(sec_itr, name))) {
  1589. data_section->name = name;
  1590. data_section->data = (uint8 *)LLVMGetSectionContents(sec_itr);
  1591. data_section->size = (uint32)LLVMGetSectionSize(sec_itr);
  1592. data_section++;
  1593. }
  1594. LLVMMoveToNextSection(sec_itr);
  1595. }
  1596. LLVMDisposeSectionIterator(sec_itr);
  1597. }
  1598. return true;
  1599. }
  1600. static bool
  1601. aot_resolve_functions(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
  1602. {
  1603. AOTObjectFunc *func;
  1604. LLVMSymbolIteratorRef sym_itr;
  1605. char *name, *prefix = AOT_FUNC_PREFIX;
  1606. uint32 func_index, total_size;
  1607. /* allocate memory for aot function */
  1608. obj_data->func_count = comp_ctx->comp_data->func_count;
  1609. if (obj_data->func_count) {
  1610. total_size = (uint32)sizeof(AOTObjectFunc) * obj_data->func_count;
  1611. if (!(obj_data->funcs = wasm_runtime_malloc(total_size))) {
  1612. aot_set_last_error("allocate memory for functions failed.");
  1613. return false;
  1614. }
  1615. memset(obj_data->funcs, 0, total_size);
  1616. }
  1617. if (!(sym_itr = LLVMObjectFileCopySymbolIterator(obj_data->binary))) {
  1618. aot_set_last_error("llvm get symbol iterator failed.");
  1619. return false;
  1620. }
  1621. while (!LLVMObjectFileIsSymbolIteratorAtEnd(obj_data->binary, sym_itr)) {
  1622. if ((name = (char *)LLVMGetSymbolName(sym_itr))
  1623. && str_starts_with(name, prefix)) {
  1624. func_index = (uint32)atoi(name + strlen(prefix));
  1625. if (func_index < obj_data->func_count) {
  1626. func = obj_data->funcs + func_index;
  1627. func->func_name = name;
  1628. func->text_offset = LLVMGetSymbolAddress(sym_itr);
  1629. }
  1630. }
  1631. LLVMMoveToNextSymbol(sym_itr);
  1632. }
  1633. LLVMDisposeSymbolIterator(sym_itr);
  1634. return true;
  1635. }
  1636. static bool
  1637. get_relocations_count(LLVMSectionIteratorRef sec_itr, uint32 *p_count)
  1638. {
  1639. uint32 relocation_count = 0;
  1640. LLVMRelocationIteratorRef rel_itr;
  1641. if (!(rel_itr = LLVMGetRelocations(sec_itr))) {
  1642. aot_set_last_error("llvm get relocations failed.");
  1643. LLVMDisposeSectionIterator(sec_itr);
  1644. return false;
  1645. }
  1646. while (!LLVMIsRelocationIteratorAtEnd(sec_itr, rel_itr)) {
  1647. relocation_count++;
  1648. LLVMMoveToNextRelocation(rel_itr);
  1649. }
  1650. LLVMDisposeRelocationIterator(rel_itr);
  1651. *p_count = relocation_count;
  1652. return true;
  1653. }
  1654. static bool
  1655. aot_resolve_object_relocation_group(AOTObjectData *obj_data,
  1656. AOTRelocationGroup *group,
  1657. LLVMSectionIteratorRef rel_sec)
  1658. {
  1659. LLVMRelocationIteratorRef rel_itr;
  1660. AOTRelocation *relocation = group->relocations;
  1661. uint32 size;
  1662. bool is_binary_32bit = is_32bit_binary(obj_data->binary);
  1663. bool is_binary_little_endian = is_little_endian_binary(obj_data->binary);
  1664. bool has_addend = str_starts_with(group->section_name, ".rela");
  1665. uint8 *rela_content = NULL;
  1666. /* calculate relocations count and allcate memory */
  1667. if (!get_relocations_count(rel_sec, &group->relocation_count))
  1668. return false;
  1669. if (group->relocation_count == 0) {
  1670. aot_set_last_error("invalid relocations count");
  1671. return false;
  1672. }
  1673. size = (uint32)sizeof(AOTRelocation) * group->relocation_count;
  1674. if (!(relocation = group->relocations = wasm_runtime_malloc(size))) {
  1675. aot_set_last_error("allocate memory for relocations failed.");
  1676. return false;
  1677. }
  1678. memset(group->relocations, 0, size);
  1679. if (has_addend) {
  1680. uint64 rela_content_size;
  1681. /* LLVM doesn't provide C API to get relocation addend. So we have to
  1682. * parse it manually. */
  1683. rela_content = (uint8 *)LLVMGetSectionContents(rel_sec);
  1684. rela_content_size = LLVMGetSectionSize(rel_sec);
  1685. if (is_binary_32bit)
  1686. size = (uint32)sizeof(struct elf32_rela) * group->relocation_count;
  1687. else
  1688. size = (uint32)sizeof(struct elf64_rela) * group->relocation_count;
  1689. if (rela_content_size != (uint64)size) {
  1690. aot_set_last_error("invalid relocation section content.");
  1691. return false;
  1692. }
  1693. }
  1694. /* pares each relocation */
  1695. if (!(rel_itr = LLVMGetRelocations(rel_sec))) {
  1696. aot_set_last_error("llvm get relocations failed.");
  1697. return false;
  1698. }
  1699. while (!LLVMIsRelocationIteratorAtEnd(rel_sec, rel_itr)) {
  1700. uint64 offset = LLVMGetRelocationOffset(rel_itr);
  1701. uint64 type = LLVMGetRelocationType(rel_itr);
  1702. LLVMSymbolIteratorRef rel_sym = LLVMGetRelocationSymbol(rel_itr);
  1703. if (!rel_sym) {
  1704. aot_set_last_error("llvm get relocation symbol failed.");
  1705. goto fail;
  1706. }
  1707. /* parse relocation addend from reloction content */
  1708. if (has_addend) {
  1709. if (is_binary_32bit) {
  1710. uint32 addend =
  1711. (uint32)(((struct elf32_rela *)rela_content)->r_addend);
  1712. if (is_binary_little_endian != is_little_endian())
  1713. exchange_uint32((uint8 *)&addend);
  1714. relocation->relocation_addend = (uint64)addend;
  1715. rela_content += sizeof(struct elf32_rela);
  1716. }
  1717. else {
  1718. uint64 addend =
  1719. (uint64)(((struct elf64_rela *)rela_content)->r_addend);
  1720. if (is_binary_little_endian != is_little_endian())
  1721. exchange_uint64((uint8 *)&addend);
  1722. relocation->relocation_addend = addend;
  1723. rela_content += sizeof(struct elf64_rela);
  1724. }
  1725. }
  1726. /* set relocation fields */
  1727. relocation->relocation_offset = offset;
  1728. relocation->relocation_type = (uint32)type;
  1729. relocation->symbol_name = (char *)LLVMGetSymbolName(rel_sym);
  1730. /* for ".LCPIxxx", ".LJTIxxx" and ".LBBxxx" relocation,
  1731. * transform the symbol name to real section name and set
  1732. * addend to the offset of the symbol in the real section */
  1733. if (relocation->symbol_name
  1734. && (str_starts_with(relocation->symbol_name, ".LCPI")
  1735. || str_starts_with(relocation->symbol_name, ".LJTI")
  1736. || str_starts_with(relocation->symbol_name, ".LBB"))) {
  1737. /* change relocation->relocation_addend and
  1738. relocation->symbol_name */
  1739. LLVMSectionIteratorRef contain_section;
  1740. if (!(contain_section =
  1741. LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  1742. aot_set_last_error("llvm get section iterator failed.");
  1743. goto fail;
  1744. }
  1745. LLVMMoveToContainingSection(contain_section, rel_sym);
  1746. if (LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary,
  1747. contain_section)) {
  1748. LLVMDisposeSectionIterator(contain_section);
  1749. aot_set_last_error("llvm get containing section failed.");
  1750. goto fail;
  1751. }
  1752. relocation->relocation_addend += LLVMGetSymbolAddress(rel_sym);
  1753. relocation->symbol_name =
  1754. (char *)LLVMGetSectionName(contain_section);
  1755. LLVMDisposeSectionIterator(contain_section);
  1756. }
  1757. LLVMDisposeSymbolIterator(rel_sym);
  1758. LLVMMoveToNextRelocation(rel_itr);
  1759. relocation++;
  1760. }
  1761. LLVMDisposeRelocationIterator(rel_itr);
  1762. return true;
  1763. fail:
  1764. LLVMDisposeRelocationIterator(rel_itr);
  1765. return false;
  1766. }
  1767. static bool
  1768. is_relocation_section_name(char *section_name)
  1769. {
  1770. return (!strcmp(section_name, ".rela.text")
  1771. || !strcmp(section_name, ".rel.text")
  1772. || !strcmp(section_name, ".rela.literal")
  1773. || !strcmp(section_name, ".rela.data")
  1774. || !strcmp(section_name, ".rel.data")
  1775. || !strcmp(section_name, ".rela.sdata")
  1776. || !strcmp(section_name, ".rel.sdata")
  1777. || !strcmp(section_name, ".rela.rodata")
  1778. || !strcmp(section_name, ".rel.rodata")
  1779. /* ".rela.rodata.cst4/8/16/.." */
  1780. || !strncmp(section_name, ".rela.rodata.cst",
  1781. strlen(".rela.rodata.cst"))
  1782. /* ".rel.rodata.cst4/8/16/.." */
  1783. || !strncmp(section_name, ".rel.rodata.cst",
  1784. strlen(".rel.rodata.cst")));
  1785. }
  1786. static bool
  1787. is_relocation_section(LLVMSectionIteratorRef sec_itr)
  1788. {
  1789. uint32 count = 0;
  1790. char *name = (char *)LLVMGetSectionName(sec_itr);
  1791. if (name) {
  1792. if (is_relocation_section_name(name))
  1793. return true;
  1794. else if ((!strcmp(name, ".text") || !strcmp(name, ".rdata"))
  1795. && get_relocations_count(sec_itr, &count) && count > 0)
  1796. return true;
  1797. }
  1798. return false;
  1799. }
  1800. static bool
  1801. get_relocation_groups_count(AOTObjectData *obj_data, uint32 *p_count)
  1802. {
  1803. uint32 count = 0;
  1804. LLVMSectionIteratorRef sec_itr;
  1805. if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  1806. aot_set_last_error("llvm get section iterator failed.");
  1807. return false;
  1808. }
  1809. while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
  1810. if (is_relocation_section(sec_itr)) {
  1811. count++;
  1812. }
  1813. LLVMMoveToNextSection(sec_itr);
  1814. }
  1815. LLVMDisposeSectionIterator(sec_itr);
  1816. *p_count = count;
  1817. return true;
  1818. }
  1819. static bool
  1820. aot_resolve_object_relocation_groups(AOTObjectData *obj_data)
  1821. {
  1822. LLVMSectionIteratorRef sec_itr;
  1823. AOTRelocationGroup *relocation_group;
  1824. uint32 group_count;
  1825. char *name;
  1826. uint32 size;
  1827. /* calculate relocation groups count and allcate memory */
  1828. if (!get_relocation_groups_count(obj_data, &group_count))
  1829. return false;
  1830. if (0 == (obj_data->relocation_group_count = group_count))
  1831. return true;
  1832. size = (uint32)sizeof(AOTRelocationGroup) * group_count;
  1833. if (!(relocation_group = obj_data->relocation_groups =
  1834. wasm_runtime_malloc(size))) {
  1835. aot_set_last_error("allocate memory for relocation groups failed.");
  1836. return false;
  1837. }
  1838. memset(obj_data->relocation_groups, 0, size);
  1839. /* resolve each relocation group */
  1840. if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
  1841. aot_set_last_error("llvm get section iterator failed.");
  1842. return false;
  1843. }
  1844. while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
  1845. if (is_relocation_section(sec_itr)) {
  1846. name = (char *)LLVMGetSectionName(sec_itr);
  1847. relocation_group->section_name = name;
  1848. if (!aot_resolve_object_relocation_group(obj_data, relocation_group,
  1849. sec_itr)) {
  1850. LLVMDisposeSectionIterator(sec_itr);
  1851. return false;
  1852. }
  1853. relocation_group++;
  1854. }
  1855. LLVMMoveToNextSection(sec_itr);
  1856. }
  1857. LLVMDisposeSectionIterator(sec_itr);
  1858. return true;
  1859. }
  1860. static void
  1861. destroy_relocation_groups(AOTRelocationGroup *relocation_groups,
  1862. uint32 relocation_group_count)
  1863. {
  1864. uint32 i;
  1865. AOTRelocationGroup *relocation_group = relocation_groups;
  1866. for (i = 0; i < relocation_group_count; i++, relocation_group++)
  1867. if (relocation_group->relocations)
  1868. wasm_runtime_free(relocation_group->relocations);
  1869. wasm_runtime_free(relocation_groups);
  1870. }
  1871. static void
  1872. destroy_relocation_symbol_list(AOTSymbolList *symbol_list)
  1873. {
  1874. AOTSymbolNode *elem;
  1875. elem = symbol_list->head;
  1876. while (elem) {
  1877. AOTSymbolNode *next = elem->next;
  1878. wasm_runtime_free(elem);
  1879. elem = next;
  1880. }
  1881. }
  1882. static void
  1883. aot_obj_data_destroy(AOTObjectData *obj_data)
  1884. {
  1885. if (obj_data->binary)
  1886. LLVMDisposeBinary(obj_data->binary);
  1887. if (obj_data->mem_buf)
  1888. LLVMDisposeMemoryBuffer(obj_data->mem_buf);
  1889. if (obj_data->funcs)
  1890. wasm_runtime_free(obj_data->funcs);
  1891. if (obj_data->data_sections)
  1892. wasm_runtime_free(obj_data->data_sections);
  1893. if (obj_data->relocation_groups)
  1894. destroy_relocation_groups(obj_data->relocation_groups,
  1895. obj_data->relocation_group_count);
  1896. if (obj_data->symbol_list.len)
  1897. destroy_relocation_symbol_list(&obj_data->symbol_list);
  1898. wasm_runtime_free(obj_data);
  1899. }
  1900. static AOTObjectData *
  1901. aot_obj_data_create(AOTCompContext *comp_ctx)
  1902. {
  1903. char *err = NULL;
  1904. AOTObjectData *obj_data;
  1905. LLVMTargetRef target = LLVMGetTargetMachineTarget(comp_ctx->target_machine);
  1906. bh_print_time("Begin to emit object file to buffer");
  1907. if (!(obj_data = wasm_runtime_malloc(sizeof(AOTObjectData)))) {
  1908. aot_set_last_error("allocate memory failed.");
  1909. return false;
  1910. }
  1911. memset(obj_data, 0, sizeof(AOTObjectData));
  1912. bh_print_time("Begin to emit object file");
  1913. if (!strncmp(LLVMGetTargetName(target), "arc", 3)) {
  1914. #if defined(_WIN32) || defined(_WIN32_)
  1915. aot_set_last_error("emit object file on Windows is unsupported.");
  1916. goto fail;
  1917. #else
  1918. /* Emit to assmelby file instead for arc target
  1919. as it cannot emit to object file */
  1920. char file_name[] = "wasm-XXXXXX", buf[128];
  1921. int fd, ret;
  1922. if ((fd = mkstemp(file_name)) <= 0) {
  1923. aot_set_last_error("make temp file failed.");
  1924. goto fail;
  1925. }
  1926. /* close and remove temp file */
  1927. close(fd);
  1928. unlink(file_name);
  1929. snprintf(buf, sizeof(buf), "%s%s", file_name, ".s");
  1930. if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine,
  1931. comp_ctx->module, buf, LLVMAssemblyFile,
  1932. &err)
  1933. != 0) {
  1934. if (err) {
  1935. LLVMDisposeMessage(err);
  1936. err = NULL;
  1937. }
  1938. aot_set_last_error("emit elf to object file failed.");
  1939. goto fail;
  1940. }
  1941. /* call arc gcc to compile assembly file to object file */
  1942. /* TODO: get arc gcc from environment variable firstly
  1943. and check whether the toolchain exists actually */
  1944. snprintf(buf, sizeof(buf), "%s%s%s%s%s%s",
  1945. "/opt/zephyr-sdk/arc-zephyr-elf/bin/arc-zephyr-elf-gcc ",
  1946. "-mcpu=arcem -o ", file_name, ".o -c ", file_name, ".s");
  1947. /* TODO: use try..catch to handle possible exceptions */
  1948. ret = system(buf);
  1949. /* remove temp assembly file */
  1950. snprintf(buf, sizeof(buf), "%s%s", file_name, ".s");
  1951. unlink(buf);
  1952. if (ret != 0) {
  1953. aot_set_last_error("failed to compile asm file to obj file "
  1954. "with arc gcc toolchain.");
  1955. goto fail;
  1956. }
  1957. /* create memory buffer from object file */
  1958. snprintf(buf, sizeof(buf), "%s%s", file_name, ".o");
  1959. ret = LLVMCreateMemoryBufferWithContentsOfFile(buf, &obj_data->mem_buf,
  1960. &err);
  1961. /* remove temp object file */
  1962. snprintf(buf, sizeof(buf), "%s%s", file_name, ".o");
  1963. unlink(buf);
  1964. if (ret != 0) {
  1965. if (err) {
  1966. LLVMDisposeMessage(err);
  1967. err = NULL;
  1968. }
  1969. aot_set_last_error("create mem buffer with file failed.");
  1970. goto fail;
  1971. }
  1972. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  1973. }
  1974. else if (LLVMTargetMachineEmitToMemoryBuffer(
  1975. comp_ctx->target_machine, comp_ctx->module, LLVMObjectFile,
  1976. &err, &obj_data->mem_buf)
  1977. != 0) {
  1978. if (err) {
  1979. LLVMDisposeMessage(err);
  1980. err = NULL;
  1981. }
  1982. aot_set_last_error("llvm emit to memory buffer failed.");
  1983. goto fail;
  1984. }
  1985. if (!(obj_data->binary = LLVMCreateBinary(obj_data->mem_buf, NULL, &err))) {
  1986. if (err) {
  1987. LLVMDisposeMessage(err);
  1988. err = NULL;
  1989. }
  1990. aot_set_last_error("llvm create binary failed.");
  1991. goto fail;
  1992. }
  1993. bh_print_time("Begin to resolve object file info");
  1994. /* resolve target info/text/relocations/functions */
  1995. if (!aot_resolve_target_info(comp_ctx, obj_data)
  1996. || !aot_resolve_text(obj_data) || !aot_resolve_literal(obj_data)
  1997. || !aot_resolve_object_data_sections(obj_data)
  1998. || !aot_resolve_object_relocation_groups(obj_data)
  1999. || !aot_resolve_functions(comp_ctx, obj_data))
  2000. goto fail;
  2001. return obj_data;
  2002. fail:
  2003. aot_obj_data_destroy(obj_data);
  2004. return NULL;
  2005. }
  2006. uint8 *
  2007. aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
  2008. uint32 *p_aot_file_size)
  2009. {
  2010. AOTObjectData *obj_data = aot_obj_data_create(comp_ctx);
  2011. uint8 *aot_file_buf, *buf, *buf_end;
  2012. uint32 aot_file_size, offset = 0;
  2013. if (!obj_data)
  2014. return NULL;
  2015. aot_file_size = get_aot_file_size(comp_ctx, comp_data, obj_data);
  2016. if (!(buf = aot_file_buf = wasm_runtime_malloc(aot_file_size))) {
  2017. aot_set_last_error("allocate memory failed.");
  2018. goto fail1;
  2019. }
  2020. memset(aot_file_buf, 0, aot_file_size);
  2021. buf_end = buf + aot_file_size;
  2022. if (!aot_emit_file_header(buf, buf_end, &offset, comp_data, obj_data)
  2023. || !aot_emit_target_info_section(buf, buf_end, &offset, comp_data,
  2024. obj_data)
  2025. || !aot_emit_init_data_section(buf, buf_end, &offset, comp_ctx,
  2026. comp_data, obj_data)
  2027. || !aot_emit_text_section(buf, buf_end, &offset, comp_data, obj_data)
  2028. || !aot_emit_func_section(buf, buf_end, &offset, comp_data, obj_data)
  2029. || !aot_emit_export_section(buf, buf_end, &offset, comp_data, obj_data)
  2030. || !aot_emit_relocation_section(buf, buf_end, &offset, comp_data,
  2031. obj_data)
  2032. || !aot_emit_native_symbol(buf, buf_end, &offset, comp_ctx))
  2033. goto fail2;
  2034. #if 0
  2035. dump_buf(buf, offset, "sections");
  2036. #endif
  2037. if (offset != aot_file_size) {
  2038. aot_set_last_error("emit aot file failed.");
  2039. goto fail2;
  2040. }
  2041. *p_aot_file_size = aot_file_size;
  2042. aot_obj_data_destroy(obj_data);
  2043. return aot_file_buf;
  2044. fail2:
  2045. wasm_runtime_free(aot_file_buf);
  2046. fail1:
  2047. aot_obj_data_destroy(obj_data);
  2048. return NULL;
  2049. }
  2050. bool
  2051. aot_emit_aot_file(AOTCompContext *comp_ctx, AOTCompData *comp_data,
  2052. const char *file_name)
  2053. {
  2054. uint8 *aot_file_buf;
  2055. uint32 aot_file_size;
  2056. bool ret = false;
  2057. FILE *file;
  2058. bh_print_time("Begin to emit AOT file");
  2059. if (!(aot_file_buf =
  2060. aot_emit_aot_file_buf(comp_ctx, comp_data, &aot_file_size))) {
  2061. return false;
  2062. }
  2063. /* write buffer to file */
  2064. if (!(file = fopen(file_name, "wb"))) {
  2065. aot_set_last_error("open or create aot file failed.");
  2066. goto fail1;
  2067. }
  2068. if (!fwrite(aot_file_buf, aot_file_size, 1, file)) {
  2069. aot_set_last_error("write to aot file failed.");
  2070. goto fail2;
  2071. }
  2072. ret = true;
  2073. fail2:
  2074. fclose(file);
  2075. fail1:
  2076. wasm_runtime_free(aot_file_buf);
  2077. return ret;
  2078. }