| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454 |
- /*
- * Copyright (C) 2019 Intel Corporation. All rights reserved.
- * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- */
- #include "aot_compiler.h"
- #include "../aot/aot_runtime.h"
- #define PUT_U64_TO_ADDR(addr, value) \
- do { \
- union { \
- uint64 val; \
- uint32 parts[2]; \
- } u; \
- u.val = (value); \
- ((uint32 *)(addr))[0] = u.parts[0]; \
- ((uint32 *)(addr))[1] = u.parts[1]; \
- } while (0)
- #define CHECK_SIZE(size) \
- do { \
- if (size == (uint32)-1) { \
- aot_set_last_error("get symbol size failed."); \
- return (uint32)-1; \
- } \
- } while (0)
- /* Internal function in object file */
- typedef struct AOTObjectFunc {
- char *func_name;
- uint64 text_offset;
- } AOTObjectFunc;
- /* Symbol table list node */
- typedef struct AOTSymbolNode {
- struct AOTSymbolNode *next;
- uint32 str_len;
- char *symbol;
- } AOTSymbolNode;
- typedef struct AOTSymbolList {
- AOTSymbolNode *head;
- AOTSymbolNode *end;
- uint32 len;
- } AOTSymbolList;
- /* AOT object data */
- typedef struct AOTObjectData {
- LLVMMemoryBufferRef mem_buf;
- LLVMBinaryRef binary;
- AOTTargetInfo target_info;
- void *text;
- uint32 text_size;
- /* literal data and size */
- void *literal;
- uint32 literal_size;
- AOTObjectDataSection *data_sections;
- uint32 data_sections_count;
- AOTObjectFunc *funcs;
- uint32 func_count;
- AOTSymbolList symbol_list;
- AOTRelocationGroup *relocation_groups;
- uint32 relocation_group_count;
- } AOTObjectData;
- #if 0
- static void dump_buf(uint8 *buf, uint32 size, char *title)
- {
- int i;
- printf("------ %s -------", title);
- for (i = 0; i < size; i++) {
- if ((i % 16) == 0)
- printf("\n");
- printf("%02x ", (unsigned char)buf[i]);
- }
- printf("\n\n");
- }
- #endif
- static bool
- is_32bit_binary(LLVMBinaryRef binary)
- {
- LLVMBinaryType type = LLVMBinaryGetType(binary);
- return (type == LLVMBinaryTypeELF32L || type == LLVMBinaryTypeELF32B);
- }
- static bool
- is_little_endian_binary(LLVMBinaryRef binary)
- {
- LLVMBinaryType type = LLVMBinaryGetType(binary);
- return (type == LLVMBinaryTypeELF32L || type == LLVMBinaryTypeELF64L);
- }
- static bool
- str_starts_with(const char *str, const char *prefix)
- {
- size_t len_pre = strlen(prefix), len_str = strlen(str);
- return (len_str >= len_pre) && !memcmp(str, prefix, len_pre);
- }
- static uint32
- get_file_header_size()
- {
- /* magic number (4 bytes) + version (4 bytes) */
- return sizeof(uint32) + sizeof(uint32);
- }
- static uint32
- get_string_size(const char *s)
- {
- /* string size (2 bytes) + string content without '\0' */
- return (uint32)sizeof(uint16) + (uint32)strlen(s);
- }
- static uint32
- get_target_info_section_size()
- {
- return sizeof(AOTTargetInfo);
- }
- static uint32
- get_mem_init_data_size(AOTMemInitData *mem_init_data)
- {
- /* init expr type (4 bytes) + init expr value (8 bytes)
- + byte count (4 bytes) + bytes */
- uint32 total_size = (uint32)(sizeof(uint32) + sizeof(uint64)
- + sizeof(uint32) + mem_init_data->byte_count);
- /* bulk_memory enabled:
- is_passive (4 bytes) + memory_index (4 bytes)
- bulk memory disabled:
- placeholder (4 bytes) + placeholder (4 bytes)
- */
- total_size += (sizeof(uint32) + sizeof(uint32));
- return total_size;
- }
- static uint32
- get_mem_init_data_list_size(AOTMemInitData **mem_init_data_list,
- uint32 mem_init_data_count)
- {
- AOTMemInitData **mem_init_data = mem_init_data_list;
- uint32 size = 0, i;
- for (i = 0; i < mem_init_data_count; i++, mem_init_data++) {
- size = align_uint(size, 4);
- size += get_mem_init_data_size(*mem_init_data);
- }
- return size;
- }
- static uint32
- get_import_memory_size(AOTCompData *comp_data)
- {
- /* currently we only emit import_memory_count = 0 */
- return sizeof(uint32);
- }
- static uint32
- get_memory_size(AOTCompData *comp_data)
- {
- /* memory_count + count * (memory_flags + num_bytes_per_page +
- init_page_count + max_page_count) */
- return (uint32)(sizeof(uint32)
- + comp_data->memory_count * sizeof(uint32) * 4);
- }
- static uint32
- get_mem_info_size(AOTCompData *comp_data)
- {
- /* import_memory_size + memory_size
- + init_data_count + init_data_list */
- return get_import_memory_size(comp_data) + get_memory_size(comp_data)
- + (uint32)sizeof(uint32)
- + get_mem_init_data_list_size(comp_data->mem_init_data_list,
- comp_data->mem_init_data_count);
- }
- static uint32
- get_table_init_data_size(AOTTableInitData *table_init_data)
- {
- /*
- * mode (4 bytes), elem_type (4 bytes), do not need is_dropped field
- *
- * table_index(4 bytes) + init expr type (4 bytes) + init expr value (8
- * bytes)
- * + func index count (4 bytes) + func indexes
- */
- return (uint32)(sizeof(uint32) * 2 + sizeof(uint32) + sizeof(uint32)
- + sizeof(uint64) + sizeof(uint32)
- + sizeof(uint32) * table_init_data->func_index_count);
- }
- static uint32
- get_table_init_data_list_size(AOTTableInitData **table_init_data_list,
- uint32 table_init_data_count)
- {
- /*
- * ------------------------------
- * | table_init_data_count
- * ------------------------------
- * | | U32 mode
- * | AOTTableInitData[N] | U32 elem_type
- * | | U32 table_index
- * | | U32 offset.init_expr_type
- * | | U64 offset.u.i64
- * | | U32 func_index_count
- * | | U32[func_index_count]
- * ------------------------------
- */
- AOTTableInitData **table_init_data = table_init_data_list;
- uint32 size = 0, i;
- size = (uint32)sizeof(uint32);
- for (i = 0; i < table_init_data_count; i++, table_init_data++) {
- size = align_uint(size, 4);
- size += get_table_init_data_size(*table_init_data);
- }
- return size;
- }
- static uint32
- get_import_table_size(AOTCompData *comp_data)
- {
- /*
- * ------------------------------
- * | import_table_count
- * ------------------------------
- * | | U32 table_init_size
- * | | ----------------------
- * | AOTImpotTable[N] | U32 table_init_size
- * | | ----------------------
- * | | U32 possible_grow (convenient than U8)
- * ------------------------------
- */
- return (uint32)(sizeof(uint32)
- + comp_data->import_table_count * (sizeof(uint32) * 3));
- }
- static uint32
- get_table_size(AOTCompData *comp_data)
- {
- /*
- * ------------------------------
- * | table_count
- * ------------------------------
- * | | U32 elem_type
- * | AOTTable[N] | U32 table_flags
- * | | U32 table_init_size
- * | | U32 table_max_size
- * | | U32 possible_grow (convenient than U8)
- * ------------------------------
- */
- return (uint32)(sizeof(uint32)
- + comp_data->table_count * (sizeof(uint32) * 5));
- }
- static uint32
- get_table_info_size(AOTCompData *comp_data)
- {
- /*
- * ------------------------------
- * | import_table_count
- * ------------------------------
- * |
- * | AOTImportTable[import_table_count]
- * |
- * ------------------------------
- * | table_count
- * ------------------------------
- * |
- * | AOTTable[table_count]
- * |
- * ------------------------------
- * | table_init_data_count
- * ------------------------------
- * |
- * | AOTTableInitData*[table_init_data_count]
- * |
- * ------------------------------
- */
- return get_import_table_size(comp_data) + get_table_size(comp_data)
- + get_table_init_data_list_size(comp_data->table_init_data_list,
- comp_data->table_init_data_count);
- }
- static uint32
- get_func_type_size(AOTFuncType *func_type)
- {
- /* param count + result count + types */
- return (uint32)sizeof(uint32) * 2 + func_type->param_count
- + func_type->result_count;
- }
- static uint32
- get_func_types_size(AOTFuncType **func_types, uint32 func_type_count)
- {
- AOTFuncType **func_type = func_types;
- uint32 size = 0, i;
- for (i = 0; i < func_type_count; i++, func_type++) {
- size = align_uint(size, 4);
- size += get_func_type_size(*func_type);
- }
- return size;
- }
- static uint32
- get_func_type_info_size(AOTCompData *comp_data)
- {
- /* func type count + func type list */
- return (uint32)sizeof(uint32)
- + get_func_types_size(comp_data->func_types,
- comp_data->func_type_count);
- }
- static uint32
- get_import_global_size(AOTImportGlobal *import_global)
- {
- /* type (1 byte) + is_mutable (1 byte) + module_name + global_name */
- uint32 size =
- (uint32)sizeof(uint8) * 2 + get_string_size(import_global->module_name);
- size = align_uint(size, 2);
- size += get_string_size(import_global->global_name);
- return size;
- }
- static uint32
- get_import_globals_size(AOTImportGlobal *import_globals,
- uint32 import_global_count)
- {
- AOTImportGlobal *import_global = import_globals;
- uint32 size = 0, i;
- for (i = 0; i < import_global_count; i++, import_global++) {
- size = align_uint(size, 2);
- size += get_import_global_size(import_global);
- }
- return size;
- }
- static uint32
- get_import_global_info_size(AOTCompData *comp_data)
- {
- /* import global count + import globals */
- return (uint32)sizeof(uint32)
- + get_import_globals_size(comp_data->import_globals,
- comp_data->import_global_count);
- }
- static uint32
- get_global_size(AOTGlobal *global)
- {
- if (global->init_expr.init_expr_type != INIT_EXPR_TYPE_V128_CONST)
- /* type (1 byte) + is_mutable (1 byte)
- + init expr type (2 byes) + init expr value (8 byes) */
- return sizeof(uint8) * 2 + sizeof(uint16) + sizeof(uint64);
- else
- /* type (1 byte) + is_mutable (1 byte)
- + init expr type (2 byes) + v128 value (16 byes) */
- return sizeof(uint8) * 2 + sizeof(uint16) + sizeof(uint64) * 2;
- }
- static uint32
- get_globals_size(AOTGlobal *globals, uint32 global_count)
- {
- AOTGlobal *global = globals;
- uint32 size = 0, i;
- for (i = 0; i < global_count; i++, global++) {
- size = align_uint(size, 4);
- size += get_global_size(global);
- }
- return size;
- }
- static uint32
- get_global_info_size(AOTCompData *comp_data)
- {
- /* global count + globals */
- return (uint32)sizeof(uint32)
- + get_globals_size(comp_data->globals, comp_data->global_count);
- }
- static uint32
- get_import_func_size(AOTImportFunc *import_func)
- {
- /* type index (2 bytes) + module_name + func_name */
- uint32 size =
- (uint32)sizeof(uint16) + get_string_size(import_func->module_name);
- size = align_uint(size, 2);
- size += get_string_size(import_func->func_name);
- return size;
- }
- static uint32
- get_import_funcs_size(AOTImportFunc *import_funcs, uint32 import_func_count)
- {
- AOTImportFunc *import_func = import_funcs;
- uint32 size = 0, i;
- for (i = 0; i < import_func_count; i++, import_func++) {
- size = align_uint(size, 2);
- size += get_import_func_size(import_func);
- }
- return size;
- }
- static uint32
- get_import_func_info_size(AOTCompData *comp_data)
- {
- /* import func count + import funcs */
- return (uint32)sizeof(uint32)
- + get_import_funcs_size(comp_data->import_funcs,
- comp_data->import_func_count);
- }
- static uint32
- get_object_data_section_size(AOTObjectDataSection *data_section)
- {
- /* name + size + data */
- uint32 size = get_string_size(data_section->name);
- size = align_uint(size, 4);
- size += (uint32)sizeof(uint32);
- size += data_section->size;
- return size;
- }
- static uint32
- get_object_data_sections_size(AOTObjectDataSection *data_sections,
- uint32 data_sections_count)
- {
- AOTObjectDataSection *data_section = data_sections;
- uint32 size = 0, i;
- for (i = 0; i < data_sections_count; i++, data_section++) {
- size = align_uint(size, 2);
- size += get_object_data_section_size(data_section);
- }
- return size;
- }
- static uint32
- get_object_data_section_info_size(AOTObjectData *obj_data)
- {
- /* data sections count + data sections */
- return (uint32)sizeof(uint32)
- + get_object_data_sections_size(obj_data->data_sections,
- obj_data->data_sections_count);
- }
- static uint32
- get_init_data_section_size(AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 size = 0;
- size += get_mem_info_size(comp_data);
- size = align_uint(size, 4);
- size += get_table_info_size(comp_data);
- size = align_uint(size, 4);
- size += get_func_type_info_size(comp_data);
- size = align_uint(size, 4);
- size += get_import_global_info_size(comp_data);
- size = align_uint(size, 4);
- size += get_global_info_size(comp_data);
- size = align_uint(size, 4);
- size += get_import_func_info_size(comp_data);
- /* func count + start func index */
- size = align_uint(size, 4);
- size += (uint32)sizeof(uint32) * 2;
- /* aux data/heap/stack data */
- size += sizeof(uint32) * 7;
- size += get_object_data_section_info_size(obj_data);
- return size;
- }
- static uint32
- get_text_section_size(AOTObjectData *obj_data)
- {
- return (sizeof(uint32) + obj_data->literal_size + obj_data->text_size + 3)
- & ~3;
- }
- static uint32
- get_func_section_size(AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- /* text offsets + function type indexs */
- uint32 size = 0;
- if (is_32bit_binary(obj_data->binary))
- size = (uint32)sizeof(uint32) * comp_data->func_count;
- else
- size = (uint32)sizeof(uint64) * comp_data->func_count;
- size += (uint32)sizeof(uint32) * comp_data->func_count;
- return size;
- }
- static uint32
- get_export_size(AOTExport *export)
- {
- /* export index + export kind + 1 byte padding + export name */
- return (uint32)sizeof(uint32) + sizeof(uint8) + 1
- + get_string_size(export->name);
- }
- static uint32
- get_exports_size(AOTExport *exports, uint32 export_count)
- {
- AOTExport *export = exports;
- uint32 size = 0, i;
- for (i = 0; i < export_count; i++, export ++) {
- size = align_uint(size, 4);
- size += get_export_size(export);
- }
- return size;
- }
- static uint32
- get_export_section_size(AOTCompData *comp_data)
- {
- /* export count + exports */
- return (uint32)sizeof(uint32)
- + get_exports_size(comp_data->wasm_module->exports,
- comp_data->wasm_module->export_count);
- }
- static uint32
- get_relocation_size(AOTRelocation *relocation, bool is_32bin)
- {
- /* offset + addend + relocation type + symbol name */
- uint32 size = 0;
- if (is_32bin)
- size = sizeof(uint32) * 2; /* offset and addend */
- else
- size = sizeof(uint64) * 2; /* offset and addend */
- size += (uint32)sizeof(uint32); /* relocation type */
- size += (uint32)sizeof(uint32); /* symbol name index */
- return size;
- }
- static uint32
- get_relocations_size(AOTRelocation *relocations, uint32 relocation_count,
- bool is_32bin)
- {
- AOTRelocation *relocation = relocations;
- uint32 size = 0, i;
- for (i = 0; i < relocation_count; i++, relocation++) {
- size = align_uint(size, 4);
- size += get_relocation_size(relocation, is_32bin);
- }
- return size;
- }
- static uint32
- get_relocation_group_size(AOTRelocationGroup *relocation_group, bool is_32bin)
- {
- uint32 size = 0;
- /* section name index + relocation count + relocations */
- size += (uint32)sizeof(uint32);
- size += (uint32)sizeof(uint32);
- size += get_relocations_size(relocation_group->relocations,
- relocation_group->relocation_count, is_32bin);
- return size;
- }
- static uint32
- get_relocation_groups_size(AOTRelocationGroup *relocation_groups,
- uint32 relocation_group_count, bool is_32bin)
- {
- AOTRelocationGroup *relocation_group = relocation_groups;
- uint32 size = 0, i;
- for (i = 0; i < relocation_group_count; i++, relocation_group++) {
- size = align_uint(size, 4);
- size += get_relocation_group_size(relocation_group, is_32bin);
- }
- return size;
- }
- /* return the index (in order of insertion) of the symbol,
- create if not exits, -1 if failed */
- static uint32
- get_relocation_symbol_index(const char *symbol_name, bool *is_new,
- AOTSymbolList *symbol_list)
- {
- AOTSymbolNode *sym;
- uint32 index = 0;
- sym = symbol_list->head;
- while (sym) {
- if (!strcmp(sym->symbol, symbol_name)) {
- if (is_new)
- *is_new = false;
- return index;
- }
- sym = sym->next;
- index++;
- }
- /* Not found in symbol_list, add it */
- sym = wasm_runtime_malloc(sizeof(AOTSymbolNode));
- if (!sym) {
- return (uint32)-1;
- }
- memset(sym, 0, sizeof(AOTSymbolNode));
- sym->symbol = (char *)symbol_name;
- sym->str_len = (uint32)strlen(symbol_name);
- if (!symbol_list->head) {
- symbol_list->head = symbol_list->end = sym;
- }
- else {
- symbol_list->end->next = sym;
- symbol_list->end = sym;
- }
- symbol_list->len++;
- if (is_new)
- *is_new = true;
- return index;
- }
- static uint32
- get_relocation_symbol_size(AOTRelocation *relocation,
- AOTSymbolList *symbol_list)
- {
- uint32 size = 0, index = 0;
- bool is_new = false;
- index = get_relocation_symbol_index(relocation->symbol_name, &is_new,
- symbol_list);
- CHECK_SIZE(index);
- if (is_new) {
- size += (uint32)sizeof(uint16);
- size += (uint32)strlen(relocation->symbol_name);
- size = align_uint(size, 2);
- }
- relocation->symbol_index = index;
- return size;
- }
- static uint32
- get_relocations_symbol_size(AOTRelocation *relocations, uint32 relocation_count,
- AOTSymbolList *symbol_list)
- {
- AOTRelocation *relocation = relocations;
- uint32 size = 0, curr_size, i;
- for (i = 0; i < relocation_count; i++, relocation++) {
- curr_size = get_relocation_symbol_size(relocation, symbol_list);
- CHECK_SIZE(curr_size);
- size += curr_size;
- }
- return size;
- }
- static uint32
- get_relocation_group_symbol_size(AOTRelocationGroup *relocation_group,
- AOTSymbolList *symbol_list)
- {
- uint32 size = 0, index = 0, curr_size;
- bool is_new = false;
- index = get_relocation_symbol_index(relocation_group->section_name, &is_new,
- symbol_list);
- CHECK_SIZE(index);
- if (is_new) {
- size += (uint32)sizeof(uint16);
- size += (uint32)strlen(relocation_group->section_name);
- size = align_uint(size, 2);
- }
- relocation_group->name_index = index;
- curr_size = get_relocations_symbol_size(relocation_group->relocations,
- relocation_group->relocation_count,
- symbol_list);
- CHECK_SIZE(curr_size);
- size += curr_size;
- return size;
- }
- static uint32
- get_relocation_groups_symbol_size(AOTRelocationGroup *relocation_groups,
- uint32 relocation_group_count,
- AOTSymbolList *symbol_list)
- {
- AOTRelocationGroup *relocation_group = relocation_groups;
- uint32 size = 0, curr_size, i;
- for (i = 0; i < relocation_group_count; i++, relocation_group++) {
- curr_size =
- get_relocation_group_symbol_size(relocation_group, symbol_list);
- CHECK_SIZE(curr_size);
- size += curr_size;
- }
- return size;
- }
- static uint32
- get_symbol_size_from_symbol_list(AOTSymbolList *symbol_list)
- {
- AOTSymbolNode *sym;
- uint32 size = 0;
- sym = symbol_list->head;
- while (sym) {
- /* (uint16)str_len + str */
- size += (uint32)sizeof(uint16) + sym->str_len;
- size = align_uint(size, 2);
- sym = sym->next;
- }
- return size;
- }
- static uint32
- get_relocation_section_symbol_size(AOTObjectData *obj_data)
- {
- AOTRelocationGroup *relocation_groups = obj_data->relocation_groups;
- uint32 relocation_group_count = obj_data->relocation_group_count;
- uint32 string_count = 0, symbol_table_size = 0;
- /* section size will be calculated twice,
- get symbol size from symbol list directly in the second calculation */
- if (obj_data->symbol_list.len > 0) {
- symbol_table_size =
- get_symbol_size_from_symbol_list(&obj_data->symbol_list);
- }
- else {
- symbol_table_size = get_relocation_groups_symbol_size(
- relocation_groups, relocation_group_count, &obj_data->symbol_list);
- }
- CHECK_SIZE(symbol_table_size);
- string_count = obj_data->symbol_list.len;
- /* string_count + string_offsets + total_string_len
- + [str (string_len + str)] */
- return (uint32)(sizeof(uint32) + sizeof(uint32) * string_count
- + sizeof(uint32) + symbol_table_size);
- }
- static uint32
- get_relocation_section_size(AOTObjectData *obj_data)
- {
- AOTRelocationGroup *relocation_groups = obj_data->relocation_groups;
- uint32 relocation_group_count = obj_data->relocation_group_count;
- uint32 symbol_table_size = 0;
- symbol_table_size = get_relocation_section_symbol_size(obj_data);
- CHECK_SIZE(symbol_table_size);
- symbol_table_size = align_uint(symbol_table_size, 4);
- /* relocation group count + symbol_table + relocation groups */
- return (uint32)sizeof(uint32) + symbol_table_size
- + get_relocation_groups_size(relocation_groups,
- relocation_group_count,
- is_32bit_binary(obj_data->binary));
- }
- static uint32
- get_native_symbol_list_size(AOTCompContext *comp_ctx)
- {
- uint32 len = 0;
- AOTNativeSymbol *sym = NULL;
- sym = bh_list_first_elem(&comp_ctx->native_symbols);
- while (sym) {
- len = align_uint(len, 2);
- len += get_string_size(sym->symbol);
- sym = bh_list_elem_next(sym);
- }
- return len;
- }
- static uint32
- get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 size = 0;
- /* aot file header */
- size += get_file_header_size();
- /* target info section */
- size = align_uint(size, 4);
- /* section id + section size */
- size += (uint32)sizeof(uint32) * 2;
- size += get_target_info_section_size();
- /* init data section */
- size = align_uint(size, 4);
- /* section id + section size */
- size += (uint32)sizeof(uint32) * 2;
- size += get_init_data_section_size(comp_data, obj_data);
- /* text section */
- size = align_uint(size, 4);
- /* section id + section size */
- size += (uint32)sizeof(uint32) * 2;
- size += get_text_section_size(obj_data);
- /* function section */
- size = align_uint(size, 4);
- /* section id + section size */
- size += (uint32)sizeof(uint32) * 2;
- size += get_func_section_size(comp_data, obj_data);
- /* export section */
- size = align_uint(size, 4);
- /* section id + section size */
- size += (uint32)sizeof(uint32) * 2;
- size += get_export_section_size(comp_data);
- /* relocation section */
- size = align_uint(size, 4);
- /* section id + section size */
- size += (uint32)sizeof(uint32) * 2;
- size += get_relocation_section_size(obj_data);
- if (get_native_symbol_list_size(comp_ctx) > 0) {
- /* emit only when threre are native symbols */
- size = align_uint(size, 4);
- /* section id + section size + sub section id + symbol count */
- size += (uint32)sizeof(uint32) * 4;
- size += get_native_symbol_list_size(comp_ctx);
- }
- return size;
- }
- #define exchange_uint8(p_data) (void)0
- static void
- exchange_uint16(uint8 *p_data)
- {
- uint8 value = *p_data;
- *p_data = *(p_data + 1);
- *(p_data + 1) = value;
- }
- static void
- exchange_uint32(uint8 *p_data)
- {
- uint8 value = *p_data;
- *p_data = *(p_data + 3);
- *(p_data + 3) = value;
- value = *(p_data + 1);
- *(p_data + 1) = *(p_data + 2);
- *(p_data + 2) = value;
- }
- static void
- exchange_uint64(uint8 *pData)
- {
- uint32 value;
- value = *(uint32 *)pData;
- *(uint32 *)pData = *(uint32 *)(pData + 4);
- *(uint32 *)(pData + 4) = value;
- exchange_uint32(pData);
- exchange_uint32(pData + 4);
- }
- static void
- exchange_uint128(uint8 *pData)
- {
- /* swap high 64bit and low 64bit */
- uint64 value = *(uint64 *)pData;
- *(uint64 *)pData = *(uint64 *)(pData + 8);
- *(uint64 *)(pData + 8) = value;
- /* exchange high 64bit */
- exchange_uint64(pData);
- /* exchange low 64bit */
- exchange_uint64(pData + 8);
- }
- static union {
- int a;
- char b;
- } __ue = { .a = 1 };
- #define is_little_endian() (__ue.b == 1)
- #define CHECK_BUF(length) \
- do { \
- if (buf + offset + length > buf_end) { \
- aot_set_last_error("buf overflow"); \
- return false; \
- } \
- } while (0)
- #define EMIT_U8(v) \
- do { \
- CHECK_BUF(1); \
- *(uint8 *)(buf + offset) = (uint8)v; \
- offset++; \
- } while (0)
- #define EMIT_U16(v) \
- do { \
- uint16 t = (uint16)v; \
- CHECK_BUF(2); \
- if (!is_little_endian()) \
- exchange_uint16((uint8 *)&t); \
- *(uint16 *)(buf + offset) = t; \
- offset += (uint32)sizeof(uint16); \
- } while (0)
- #define EMIT_U32(v) \
- do { \
- uint32 t = (uint32)v; \
- CHECK_BUF(4); \
- if (!is_little_endian()) \
- exchange_uint32((uint8 *)&t); \
- *(uint32 *)(buf + offset) = t; \
- offset += (uint32)sizeof(uint32); \
- } while (0)
- #define EMIT_U64(v) \
- do { \
- uint64 t = (uint64)v; \
- CHECK_BUF(8); \
- if (!is_little_endian()) \
- exchange_uint64((uint8 *)&t); \
- PUT_U64_TO_ADDR(buf + offset, t); \
- offset += (uint32)sizeof(uint64); \
- } while (0)
- #define EMIT_V128(v) \
- do { \
- uint64 *t = (uint64 *)v.i64x2; \
- CHECK_BUF(16); \
- if (!is_little_endian()) \
- exchange_uint128((uint8 *)t); \
- PUT_U64_TO_ADDR(buf + offset, t[0]); \
- offset += (uint32)sizeof(uint64); \
- PUT_U64_TO_ADDR(buf + offset, t[1]); \
- offset += (uint32)sizeof(uint64); \
- } while (0)
- #define EMIT_BUF(v, len) \
- do { \
- CHECK_BUF(len); \
- memcpy(buf + offset, v, len); \
- offset += len; \
- } while (0)
- #define EMIT_STR(s) \
- do { \
- uint32 str_len = (uint32)strlen(s); \
- EMIT_U16(str_len); \
- EMIT_BUF(s, str_len); \
- } while (0)
- static bool
- aot_emit_file_header(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset;
- uint32 aot_curr_version = AOT_CURRENT_VERSION;
- EMIT_U8('\0');
- EMIT_U8('a');
- EMIT_U8('o');
- EMIT_U8('t');
- EMIT_U32(aot_curr_version);
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_target_info_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset;
- uint32 section_size = get_target_info_section_size();
- AOTTargetInfo *target_info = &obj_data->target_info;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_TARGET_INFO);
- EMIT_U32(section_size);
- EMIT_U16(target_info->bin_type);
- EMIT_U16(target_info->abi_type);
- EMIT_U16(target_info->e_type);
- EMIT_U16(target_info->e_machine);
- EMIT_U32(target_info->e_version);
- EMIT_U32(target_info->e_flags);
- EMIT_U32(target_info->reserved);
- EMIT_BUF(target_info->arch, sizeof(target_info->arch));
- if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
- aot_set_last_error("emit target info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_mem_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i;
- AOTMemInitData **init_datas = comp_data->mem_init_data_list;
- *p_offset = offset = align_uint(offset, 4);
- /* Emit import memory count, only emit 0 currently.
- TODO: emit the actual import memory count and
- the full import memory info. */
- EMIT_U32(0);
- /* Emit memory count */
- EMIT_U32(comp_data->memory_count);
- /* Emit memory items */
- for (i = 0; i < comp_data->memory_count; i++) {
- EMIT_U32(comp_data->memories[i].memory_flags);
- EMIT_U32(comp_data->memories[i].num_bytes_per_page);
- EMIT_U32(comp_data->memories[i].mem_init_page_count);
- EMIT_U32(comp_data->memories[i].mem_max_page_count);
- }
- /* Emit mem init data count */
- EMIT_U32(comp_data->mem_init_data_count);
- /* Emit mem init data items */
- for (i = 0; i < comp_data->mem_init_data_count; i++) {
- offset = align_uint(offset, 4);
- #if WASM_ENABLE_BULK_MEMORY != 0
- if (comp_ctx->enable_bulk_memory) {
- EMIT_U32(init_datas[i]->is_passive);
- EMIT_U32(init_datas[i]->memory_index);
- }
- else
- #endif
- {
- /* emit two placeholder to keep the same size */
- EMIT_U32(0);
- EMIT_U32(0);
- }
- EMIT_U32(init_datas[i]->offset.init_expr_type);
- EMIT_U64(init_datas[i]->offset.u.i64);
- EMIT_U32(init_datas[i]->byte_count);
- EMIT_BUF(init_datas[i]->bytes, init_datas[i]->byte_count);
- }
- if (offset - *p_offset != get_mem_info_size(comp_data)) {
- aot_set_last_error("emit memory info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_table_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i, j;
- AOTTableInitData **init_datas = comp_data->table_init_data_list;
- *p_offset = offset = align_uint(offset, 4);
- /* Emit import table count */
- EMIT_U32(comp_data->import_table_count);
- /* Emit table items */
- for (i = 0; i < comp_data->import_table_count; i++) {
- /* TODO:
- * EMIT_STR(comp_data->import_tables[i].module_name );
- * EMIT_STR(comp_data->import_tables[i].table_name);
- */
- EMIT_U32(comp_data->import_tables[i].table_init_size);
- EMIT_U32(comp_data->import_tables[i].table_max_size);
- EMIT_U32(comp_data->import_tables[i].possible_grow & 0x000000FF);
- }
- /* Emit table count */
- EMIT_U32(comp_data->table_count);
- /* Emit table items */
- for (i = 0; i < comp_data->table_count; i++) {
- EMIT_U32(comp_data->tables[i].elem_type);
- EMIT_U32(comp_data->tables[i].table_flags);
- EMIT_U32(comp_data->tables[i].table_init_size);
- EMIT_U32(comp_data->tables[i].table_max_size);
- EMIT_U32(comp_data->tables[i].possible_grow & 0x000000FF);
- }
- /* Emit table init data count */
- EMIT_U32(comp_data->table_init_data_count);
- /* Emit table init data items */
- for (i = 0; i < comp_data->table_init_data_count; i++) {
- offset = align_uint(offset, 4);
- EMIT_U32(init_datas[i]->mode);
- EMIT_U32(init_datas[i]->elem_type);
- EMIT_U32(init_datas[i]->table_index);
- EMIT_U32(init_datas[i]->offset.init_expr_type);
- EMIT_U64(init_datas[i]->offset.u.i64);
- EMIT_U32(init_datas[i]->func_index_count);
- for (j = 0; j < init_datas[i]->func_index_count; j++)
- EMIT_U32(init_datas[i]->func_indexes[j]);
- }
- if (offset - *p_offset != get_table_info_size(comp_data)) {
- aot_set_last_error("emit table info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_func_type_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i;
- AOTFuncType **func_types = comp_data->func_types;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(comp_data->func_type_count);
- for (i = 0; i < comp_data->func_type_count; i++) {
- offset = align_uint(offset, 4);
- EMIT_U32(func_types[i]->param_count);
- EMIT_U32(func_types[i]->result_count);
- EMIT_BUF(func_types[i]->types,
- func_types[i]->param_count + func_types[i]->result_count);
- }
- if (offset - *p_offset != get_func_type_info_size(comp_data)) {
- aot_set_last_error("emit function type info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_import_global_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i;
- AOTImportGlobal *import_global = comp_data->import_globals;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(comp_data->import_global_count);
- for (i = 0; i < comp_data->import_global_count; i++, import_global++) {
- offset = align_uint(offset, 2);
- EMIT_U8(import_global->type);
- EMIT_U8(import_global->is_mutable);
- EMIT_STR(import_global->module_name);
- offset = align_uint(offset, 2);
- EMIT_STR(import_global->global_name);
- }
- if (offset - *p_offset != get_import_global_info_size(comp_data)) {
- aot_set_last_error("emit import global info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_global_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i;
- AOTGlobal *global = comp_data->globals;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(comp_data->global_count);
- for (i = 0; i < comp_data->global_count; i++, global++) {
- offset = align_uint(offset, 4);
- EMIT_U8(global->type);
- EMIT_U8(global->is_mutable);
- EMIT_U16(global->init_expr.init_expr_type);
- if (global->init_expr.init_expr_type != INIT_EXPR_TYPE_V128_CONST)
- EMIT_U64(global->init_expr.u.i64);
- else
- EMIT_V128(global->init_expr.u.v128);
- }
- if (offset - *p_offset != get_global_info_size(comp_data)) {
- aot_set_last_error("emit global info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_import_func_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i;
- AOTImportFunc *import_func = comp_data->import_funcs;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(comp_data->import_func_count);
- for (i = 0; i < comp_data->import_func_count; i++, import_func++) {
- offset = align_uint(offset, 2);
- EMIT_U16(import_func->func_type_index);
- EMIT_STR(import_func->module_name);
- offset = align_uint(offset, 2);
- EMIT_STR(import_func->func_name);
- }
- if (offset - *p_offset != get_import_func_info_size(comp_data)) {
- aot_set_last_error("emit import function info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_object_data_section_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTObjectData *obj_data)
- {
- uint32 offset = *p_offset, i;
- AOTObjectDataSection *data_section = obj_data->data_sections;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(obj_data->data_sections_count);
- for (i = 0; i < obj_data->data_sections_count; i++, data_section++) {
- offset = align_uint(offset, 2);
- EMIT_STR(data_section->name);
- offset = align_uint(offset, 4);
- EMIT_U32(data_section->size);
- EMIT_BUF(data_section->data, data_section->size);
- }
- if (offset - *p_offset != get_object_data_section_info_size(obj_data)) {
- aot_set_last_error("emit object data section info failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_init_data_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx, AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 section_size = get_init_data_section_size(comp_data, obj_data);
- uint32 offset = *p_offset;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_INIT_DATA);
- EMIT_U32(section_size);
- if (!aot_emit_mem_info(buf, buf_end, &offset, comp_ctx, comp_data, obj_data)
- || !aot_emit_table_info(buf, buf_end, &offset, comp_data, obj_data)
- || !aot_emit_func_type_info(buf, buf_end, &offset, comp_data, obj_data)
- || !aot_emit_import_global_info(buf, buf_end, &offset, comp_data,
- obj_data)
- || !aot_emit_global_info(buf, buf_end, &offset, comp_data, obj_data)
- || !aot_emit_import_func_info(buf, buf_end, &offset, comp_data,
- obj_data))
- return false;
- offset = align_uint(offset, 4);
- EMIT_U32(comp_data->func_count);
- EMIT_U32(comp_data->start_func_index);
- EMIT_U32(comp_data->aux_data_end_global_index);
- EMIT_U32(comp_data->aux_data_end);
- EMIT_U32(comp_data->aux_heap_base_global_index);
- EMIT_U32(comp_data->aux_heap_base);
- EMIT_U32(comp_data->aux_stack_top_global_index);
- EMIT_U32(comp_data->aux_stack_bottom);
- EMIT_U32(comp_data->aux_stack_size);
- if (!aot_emit_object_data_section_info(buf, buf_end, &offset, obj_data))
- return false;
- if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
- aot_set_last_error("emit init data section failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_text_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 section_size = get_text_section_size(obj_data);
- uint32 offset = *p_offset;
- uint8 placeholder = 0;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_TEXT);
- EMIT_U32(section_size);
- EMIT_U32(obj_data->literal_size);
- if (obj_data->literal_size > 0)
- EMIT_BUF(obj_data->literal, obj_data->literal_size);
- EMIT_BUF(obj_data->text, obj_data->text_size);
- while (offset & 3)
- EMIT_BUF(&placeholder, 1);
- if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
- aot_set_last_error("emit text section failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_func_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 section_size = get_func_section_size(comp_data, obj_data);
- uint32 i, offset = *p_offset;
- AOTObjectFunc *func = obj_data->funcs;
- AOTFunc **funcs = comp_data->funcs;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_FUNCTION);
- EMIT_U32(section_size);
- for (i = 0; i < obj_data->func_count; i++, func++) {
- if (is_32bit_binary(obj_data->binary))
- EMIT_U32(func->text_offset);
- else
- EMIT_U64(func->text_offset);
- }
- for (i = 0; i < comp_data->func_count; i++)
- EMIT_U32(funcs[i]->func_type_index);
- if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
- aot_set_last_error("emit function section failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_export_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 section_size = get_export_section_size(comp_data);
- AOTExport *export = comp_data->wasm_module->exports;
- uint32 export_count = comp_data->wasm_module->export_count;
- uint32 i, offset = *p_offset;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_EXPORT);
- EMIT_U32(section_size);
- EMIT_U32(export_count);
- for (i = 0; i < export_count; i++, export ++) {
- offset = align_uint(offset, 4);
- EMIT_U32(export->index);
- EMIT_U8(export->kind);
- EMIT_U8(0);
- EMIT_STR(export->name);
- }
- if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
- aot_set_last_error("emit export section failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_relocation_symbol_table(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data,
- AOTObjectData *obj_data)
- {
- uint32 symbol_offset = 0, total_string_len = 0;
- uint32 offset = *p_offset;
- AOTSymbolNode *sym;
- EMIT_U32(obj_data->symbol_list.len);
- /* emit symbol offsets */
- sym = (AOTSymbolNode *)(obj_data->symbol_list.head);
- while (sym) {
- EMIT_U32(symbol_offset);
- /* string_len + str[0 .. string_len - 1] */
- symbol_offset += (uint32)sizeof(uint16) + sym->str_len;
- symbol_offset = align_uint(symbol_offset, 2);
- sym = sym->next;
- }
- /* emit total string len */
- total_string_len = symbol_offset;
- EMIT_U32(total_string_len);
- /* emit symbols */
- sym = (AOTSymbolNode *)(obj_data->symbol_list.head);
- while (sym) {
- EMIT_STR(sym->symbol);
- offset = align_uint(offset, 2);
- sym = sym->next;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_relocation_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompData *comp_data, AOTObjectData *obj_data)
- {
- uint32 section_size = get_relocation_section_size(obj_data);
- uint32 i, offset = *p_offset;
- AOTRelocationGroup *relocation_group = obj_data->relocation_groups;
- if (section_size == (uint32)-1)
- return false;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_RELOCATION);
- EMIT_U32(section_size);
- aot_emit_relocation_symbol_table(buf, buf_end, &offset, comp_data,
- obj_data);
- offset = align_uint(offset, 4);
- EMIT_U32(obj_data->relocation_group_count);
- /* emit each relocation group */
- for (i = 0; i < obj_data->relocation_group_count; i++, relocation_group++) {
- AOTRelocation *relocation = relocation_group->relocations;
- uint32 j;
- offset = align_uint(offset, 4);
- EMIT_U32(relocation_group->name_index);
- offset = align_uint(offset, 4);
- EMIT_U32(relocation_group->relocation_count);
- /* emit each relocation */
- for (j = 0; j < relocation_group->relocation_count; j++, relocation++) {
- offset = align_uint(offset, 4);
- if (is_32bit_binary(obj_data->binary)) {
- EMIT_U32(relocation->relocation_offset);
- EMIT_U32(relocation->relocation_addend);
- }
- else {
- EMIT_U64(relocation->relocation_offset);
- EMIT_U64(relocation->relocation_addend);
- }
- EMIT_U32(relocation->relocation_type);
- EMIT_U32(relocation->symbol_index);
- }
- }
- if (offset - *p_offset != section_size + sizeof(uint32) * 2) {
- aot_set_last_error("emit relocation section failed.");
- return false;
- }
- *p_offset = offset;
- return true;
- }
- static bool
- aot_emit_native_symbol(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
- AOTCompContext *comp_ctx)
- {
- uint32 offset = *p_offset;
- AOTNativeSymbol *sym = NULL;
- if (bh_list_length(&comp_ctx->native_symbols) == 0)
- /* emit only when threre are native symbols */
- return true;
- *p_offset = offset = align_uint(offset, 4);
- EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
- /* sub section id + symbol count + symbol list */
- EMIT_U32(sizeof(uint32) * 2 + get_native_symbol_list_size(comp_ctx));
- EMIT_U32(AOT_CUSTOM_SECTION_NATIVE_SYMBOL);
- EMIT_U32(bh_list_length(&comp_ctx->native_symbols));
- sym = bh_list_first_elem(&comp_ctx->native_symbols);
- while (sym) {
- offset = align_uint(offset, 2);
- EMIT_STR(sym->symbol);
- sym = bh_list_elem_next(sym);
- }
- *p_offset = offset;
- return true;
- }
- typedef uint32 U32;
- typedef int32 I32;
- typedef uint16 U16;
- typedef uint8 U8;
- struct coff_hdr {
- U16 u16Machine;
- U16 u16NumSections;
- U32 u32DateTimeStamp;
- U32 u32SymTblPtr;
- U32 u32NumSymbols;
- U16 u16PeHdrSize;
- U16 u16Characs;
- };
- #define IMAGE_FILE_MACHINE_AMD64 0x8664
- #define IMAGE_FILE_MACHINE_I386 0x014c
- #define IMAGE_FILE_MACHINE_IA64 0x0200
- #define AOT_COFF_BIN_TYPE 6
- #define EI_NIDENT 16
- typedef uint32 elf32_word;
- typedef int32 elf32_sword;
- typedef uint16 elf32_half;
- typedef uint32 elf32_off;
- typedef uint32 elf32_addr;
- struct elf32_ehdr {
- unsigned char e_ident[EI_NIDENT]; /* ident bytes */
- elf32_half e_type; /* file type */
- elf32_half e_machine; /* target machine */
- elf32_word e_version; /* file version */
- elf32_addr e_entry; /* start address */
- elf32_off e_phoff; /* phdr file offset */
- elf32_off e_shoff; /* shdr file offset */
- elf32_word e_flags; /* file flags */
- elf32_half e_ehsize; /* sizeof ehdr */
- elf32_half e_phentsize; /* sizeof phdr */
- elf32_half e_phnum; /* number phdrs */
- elf32_half e_shentsize; /* sizeof shdr */
- elf32_half e_shnum; /* number shdrs */
- elf32_half e_shstrndx; /* shdr string index */
- };
- struct elf32_rel {
- elf32_addr r_offset;
- elf32_word r_info;
- } elf32_rel;
- struct elf32_rela {
- elf32_addr r_offset;
- elf32_word r_info;
- elf32_sword r_addend;
- } elf32_rela;
- typedef uint32 elf64_word;
- typedef int32 elf64_sword;
- typedef uint64 elf64_xword;
- typedef int64 elf64_sxword;
- typedef uint16 elf64_half;
- typedef uint64 elf64_off;
- typedef uint64 elf64_addr;
- struct elf64_ehdr {
- unsigned char e_ident[EI_NIDENT]; /* ident bytes */
- elf64_half e_type; /* file type */
- elf64_half e_machine; /* target machine */
- elf64_word e_version; /* file version */
- elf64_addr e_entry; /* start address */
- elf64_off e_phoff; /* phdr file offset */
- elf64_off e_shoff; /* shdr file offset */
- elf64_word e_flags; /* file flags */
- elf64_half e_ehsize; /* sizeof ehdr */
- elf64_half e_phentsize; /* sizeof phdr */
- elf64_half e_phnum; /* number phdrs */
- elf64_half e_shentsize; /* sizeof shdr */
- elf64_half e_shnum; /* number shdrs */
- elf64_half e_shstrndx; /* shdr string index */
- };
- typedef struct elf64_rel {
- elf64_addr r_offset;
- elf64_xword r_info;
- } elf64_rel;
- typedef struct elf64_rela {
- elf64_addr r_offset;
- elf64_xword r_info;
- elf64_sxword r_addend;
- } elf64_rela;
- #define SET_TARGET_INFO(f, v, type, little) \
- do { \
- type tmp = elf_header->v; \
- if ((little && !is_little_endian()) \
- || (!little && is_little_endian())) \
- exchange_##type((uint8 *)&tmp); \
- obj_data->target_info.f = tmp; \
- } while (0)
- static bool
- aot_resolve_target_info(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
- {
- LLVMBinaryType bin_type = LLVMBinaryGetType(obj_data->binary);
- const uint8 *elf_buf = (uint8 *)LLVMGetBufferStart(obj_data->mem_buf);
- uint32 elf_size = (uint32)LLVMGetBufferSize(obj_data->mem_buf);
- if (bin_type != LLVMBinaryTypeCOFF && bin_type != LLVMBinaryTypeELF32L
- && bin_type != LLVMBinaryTypeELF32B && bin_type != LLVMBinaryTypeELF64L
- && bin_type != LLVMBinaryTypeELF64B
- && bin_type != LLVMBinaryTypeMachO32L
- && bin_type != LLVMBinaryTypeMachO32B
- && bin_type != LLVMBinaryTypeMachO64L
- && bin_type != LLVMBinaryTypeMachO64B) {
- aot_set_last_error("invaid llvm binary bin_type.");
- return false;
- }
- obj_data->target_info.bin_type = bin_type - LLVMBinaryTypeELF32L;
- if (bin_type == LLVMBinaryTypeCOFF) {
- struct coff_hdr *coff_header;
- if (!elf_buf || elf_size < sizeof(struct coff_hdr)) {
- aot_set_last_error("invalid coff_hdr buffer.");
- return false;
- }
- coff_header = (struct coff_hdr *)elf_buf;
- obj_data->target_info.e_type = 1;
- obj_data->target_info.e_machine = coff_header->u16Machine;
- obj_data->target_info.e_version = 1;
- obj_data->target_info.e_flags = 0;
- if (coff_header->u16Machine == IMAGE_FILE_MACHINE_AMD64)
- obj_data->target_info.bin_type = AOT_COFF_BIN_TYPE;
- }
- else if (bin_type == LLVMBinaryTypeELF32L
- || bin_type == LLVMBinaryTypeELF32B) {
- struct elf32_ehdr *elf_header;
- bool is_little_bin = bin_type == LLVMBinaryTypeELF32L;
- if (!elf_buf || elf_size < sizeof(struct elf32_ehdr)) {
- aot_set_last_error("invalid elf32 buffer.");
- return false;
- }
- elf_header = (struct elf32_ehdr *)elf_buf;
- SET_TARGET_INFO(e_type, e_type, uint16, is_little_bin);
- SET_TARGET_INFO(e_machine, e_machine, uint16, is_little_bin);
- SET_TARGET_INFO(e_version, e_version, uint32, is_little_bin);
- SET_TARGET_INFO(e_flags, e_flags, uint32, is_little_bin);
- }
- else if (bin_type == LLVMBinaryTypeELF64L
- || bin_type == LLVMBinaryTypeELF64B) {
- struct elf64_ehdr *elf_header;
- bool is_little_bin = bin_type == LLVMBinaryTypeELF64L;
- if (!elf_buf || elf_size < sizeof(struct elf64_ehdr)) {
- aot_set_last_error("invalid elf64 buffer.");
- return false;
- }
- elf_header = (struct elf64_ehdr *)elf_buf;
- SET_TARGET_INFO(e_type, e_type, uint16, is_little_bin);
- SET_TARGET_INFO(e_machine, e_machine, uint16, is_little_bin);
- SET_TARGET_INFO(e_version, e_version, uint32, is_little_bin);
- SET_TARGET_INFO(e_flags, e_flags, uint32, is_little_bin);
- }
- else if (bin_type == LLVMBinaryTypeMachO32L
- || bin_type == LLVMBinaryTypeMachO32B) {
- /* TODO: parse file type of Mach-O 32 */
- aot_set_last_error("invaid llvm binary bin_type.");
- return false;
- }
- else if (bin_type == LLVMBinaryTypeMachO64L
- || bin_type == LLVMBinaryTypeMachO64B) {
- /* TODO: parse file type of Mach-O 64 */
- aot_set_last_error("invaid llvm binary bin_type.");
- return false;
- }
- strncpy(obj_data->target_info.arch, comp_ctx->target_arch,
- sizeof(obj_data->target_info.arch));
- return true;
- }
- static bool
- aot_resolve_text(AOTObjectData *obj_data)
- {
- #if WASM_ENABLE_DEBUG_AOT != 0
- LLVMBinaryType bin_type = LLVMBinaryGetType(obj_data->binary);
- if (bin_type == LLVMBinaryTypeELF32L || bin_type == LLVMBinaryTypeELF64L) {
- obj_data->text = (char *)LLVMGetBufferStart(obj_data->mem_buf);
- obj_data->text_size = (uint32)LLVMGetBufferSize(obj_data->mem_buf);
- }
- else
- #endif
- {
- LLVMSectionIteratorRef sec_itr;
- char *name;
- if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- return false;
- }
- while (
- !LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
- if ((name = (char *)LLVMGetSectionName(sec_itr))
- && !strcmp(name, ".text")) {
- obj_data->text = (char *)LLVMGetSectionContents(sec_itr);
- obj_data->text_size = (uint32)LLVMGetSectionSize(sec_itr);
- break;
- }
- LLVMMoveToNextSection(sec_itr);
- }
- LLVMDisposeSectionIterator(sec_itr);
- }
- return true;
- }
- static bool
- aot_resolve_literal(AOTObjectData *obj_data)
- {
- LLVMSectionIteratorRef sec_itr;
- char *name;
- if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- return false;
- }
- while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
- if ((name = (char *)LLVMGetSectionName(sec_itr))
- && !strcmp(name, ".literal")) {
- obj_data->literal = (char *)LLVMGetSectionContents(sec_itr);
- obj_data->literal_size = (uint32)LLVMGetSectionSize(sec_itr);
- break;
- }
- LLVMMoveToNextSection(sec_itr);
- }
- LLVMDisposeSectionIterator(sec_itr);
- return true;
- }
- static bool
- get_relocations_count(LLVMSectionIteratorRef sec_itr, uint32 *p_count);
- static bool
- is_data_section(LLVMSectionIteratorRef sec_itr, char *section_name)
- {
- uint32 relocation_count = 0;
- return (!strcmp(section_name, ".data") || !strcmp(section_name, ".sdata")
- || !strcmp(section_name, ".rodata")
- /* ".rodata.cst4/8/16/.." */
- || !strncmp(section_name, ".rodata.cst", strlen(".rodata.cst"))
- || (!strcmp(section_name, ".rdata")
- && get_relocations_count(sec_itr, &relocation_count)
- && relocation_count > 0));
- }
- static bool
- get_object_data_sections_count(AOTObjectData *obj_data, uint32 *p_count)
- {
- LLVMSectionIteratorRef sec_itr;
- char *name;
- uint32 count = 0;
- if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- return false;
- }
- while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
- if ((name = (char *)LLVMGetSectionName(sec_itr))
- && (is_data_section(sec_itr, name))) {
- count++;
- }
- LLVMMoveToNextSection(sec_itr);
- }
- LLVMDisposeSectionIterator(sec_itr);
- *p_count = count;
- return true;
- }
- static bool
- aot_resolve_object_data_sections(AOTObjectData *obj_data)
- {
- LLVMSectionIteratorRef sec_itr;
- char *name;
- AOTObjectDataSection *data_section;
- uint32 sections_count;
- uint32 size;
- if (!get_object_data_sections_count(obj_data, §ions_count)) {
- return false;
- }
- if (sections_count > 0) {
- size = (uint32)sizeof(AOTObjectDataSection) * sections_count;
- if (!(data_section = obj_data->data_sections =
- wasm_runtime_malloc(size))) {
- aot_set_last_error("allocate memory for data sections failed.");
- return false;
- }
- memset(obj_data->data_sections, 0, size);
- obj_data->data_sections_count = sections_count;
- if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- return false;
- }
- while (
- !LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
- if ((name = (char *)LLVMGetSectionName(sec_itr))
- && (is_data_section(sec_itr, name))) {
- data_section->name = name;
- data_section->data = (uint8 *)LLVMGetSectionContents(sec_itr);
- data_section->size = (uint32)LLVMGetSectionSize(sec_itr);
- data_section++;
- }
- LLVMMoveToNextSection(sec_itr);
- }
- LLVMDisposeSectionIterator(sec_itr);
- }
- return true;
- }
- static bool
- aot_resolve_functions(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
- {
- AOTObjectFunc *func;
- LLVMSymbolIteratorRef sym_itr;
- char *name, *prefix = AOT_FUNC_PREFIX;
- uint32 func_index, total_size;
- /* allocate memory for aot function */
- obj_data->func_count = comp_ctx->comp_data->func_count;
- if (obj_data->func_count) {
- total_size = (uint32)sizeof(AOTObjectFunc) * obj_data->func_count;
- if (!(obj_data->funcs = wasm_runtime_malloc(total_size))) {
- aot_set_last_error("allocate memory for functions failed.");
- return false;
- }
- memset(obj_data->funcs, 0, total_size);
- }
- if (!(sym_itr = LLVMObjectFileCopySymbolIterator(obj_data->binary))) {
- aot_set_last_error("llvm get symbol iterator failed.");
- return false;
- }
- while (!LLVMObjectFileIsSymbolIteratorAtEnd(obj_data->binary, sym_itr)) {
- if ((name = (char *)LLVMGetSymbolName(sym_itr))
- && str_starts_with(name, prefix)) {
- func_index = (uint32)atoi(name + strlen(prefix));
- if (func_index < obj_data->func_count) {
- func = obj_data->funcs + func_index;
- func->func_name = name;
- func->text_offset = LLVMGetSymbolAddress(sym_itr);
- }
- }
- LLVMMoveToNextSymbol(sym_itr);
- }
- LLVMDisposeSymbolIterator(sym_itr);
- return true;
- }
- static bool
- get_relocations_count(LLVMSectionIteratorRef sec_itr, uint32 *p_count)
- {
- uint32 relocation_count = 0;
- LLVMRelocationIteratorRef rel_itr;
- if (!(rel_itr = LLVMGetRelocations(sec_itr))) {
- aot_set_last_error("llvm get relocations failed.");
- LLVMDisposeSectionIterator(sec_itr);
- return false;
- }
- while (!LLVMIsRelocationIteratorAtEnd(sec_itr, rel_itr)) {
- relocation_count++;
- LLVMMoveToNextRelocation(rel_itr);
- }
- LLVMDisposeRelocationIterator(rel_itr);
- *p_count = relocation_count;
- return true;
- }
- static bool
- aot_resolve_object_relocation_group(AOTObjectData *obj_data,
- AOTRelocationGroup *group,
- LLVMSectionIteratorRef rel_sec)
- {
- LLVMRelocationIteratorRef rel_itr;
- AOTRelocation *relocation = group->relocations;
- uint32 size;
- bool is_binary_32bit = is_32bit_binary(obj_data->binary);
- bool is_binary_little_endian = is_little_endian_binary(obj_data->binary);
- bool has_addend = str_starts_with(group->section_name, ".rela");
- uint8 *rela_content = NULL;
- /* calculate relocations count and allcate memory */
- if (!get_relocations_count(rel_sec, &group->relocation_count))
- return false;
- if (group->relocation_count == 0) {
- aot_set_last_error("invalid relocations count");
- return false;
- }
- size = (uint32)sizeof(AOTRelocation) * group->relocation_count;
- if (!(relocation = group->relocations = wasm_runtime_malloc(size))) {
- aot_set_last_error("allocate memory for relocations failed.");
- return false;
- }
- memset(group->relocations, 0, size);
- if (has_addend) {
- uint64 rela_content_size;
- /* LLVM doesn't provide C API to get relocation addend. So we have to
- * parse it manually. */
- rela_content = (uint8 *)LLVMGetSectionContents(rel_sec);
- rela_content_size = LLVMGetSectionSize(rel_sec);
- if (is_binary_32bit)
- size = (uint32)sizeof(struct elf32_rela) * group->relocation_count;
- else
- size = (uint32)sizeof(struct elf64_rela) * group->relocation_count;
- if (rela_content_size != (uint64)size) {
- aot_set_last_error("invalid relocation section content.");
- return false;
- }
- }
- /* pares each relocation */
- if (!(rel_itr = LLVMGetRelocations(rel_sec))) {
- aot_set_last_error("llvm get relocations failed.");
- return false;
- }
- while (!LLVMIsRelocationIteratorAtEnd(rel_sec, rel_itr)) {
- uint64 offset = LLVMGetRelocationOffset(rel_itr);
- uint64 type = LLVMGetRelocationType(rel_itr);
- LLVMSymbolIteratorRef rel_sym = LLVMGetRelocationSymbol(rel_itr);
- if (!rel_sym) {
- aot_set_last_error("llvm get relocation symbol failed.");
- goto fail;
- }
- /* parse relocation addend from reloction content */
- if (has_addend) {
- if (is_binary_32bit) {
- uint32 addend =
- (uint32)(((struct elf32_rela *)rela_content)->r_addend);
- if (is_binary_little_endian != is_little_endian())
- exchange_uint32((uint8 *)&addend);
- relocation->relocation_addend = (uint64)addend;
- rela_content += sizeof(struct elf32_rela);
- }
- else {
- uint64 addend =
- (uint64)(((struct elf64_rela *)rela_content)->r_addend);
- if (is_binary_little_endian != is_little_endian())
- exchange_uint64((uint8 *)&addend);
- relocation->relocation_addend = addend;
- rela_content += sizeof(struct elf64_rela);
- }
- }
- /* set relocation fields */
- relocation->relocation_offset = offset;
- relocation->relocation_type = (uint32)type;
- relocation->symbol_name = (char *)LLVMGetSymbolName(rel_sym);
- /* for ".LCPIxxx", ".LJTIxxx" and ".LBBxxx" relocation,
- * transform the symbol name to real section name and set
- * addend to the offset of the symbol in the real section */
- if (relocation->symbol_name
- && (str_starts_with(relocation->symbol_name, ".LCPI")
- || str_starts_with(relocation->symbol_name, ".LJTI")
- || str_starts_with(relocation->symbol_name, ".LBB"))) {
- /* change relocation->relocation_addend and
- relocation->symbol_name */
- LLVMSectionIteratorRef contain_section;
- if (!(contain_section =
- LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- goto fail;
- }
- LLVMMoveToContainingSection(contain_section, rel_sym);
- if (LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary,
- contain_section)) {
- LLVMDisposeSectionIterator(contain_section);
- aot_set_last_error("llvm get containing section failed.");
- goto fail;
- }
- relocation->relocation_addend += LLVMGetSymbolAddress(rel_sym);
- relocation->symbol_name =
- (char *)LLVMGetSectionName(contain_section);
- LLVMDisposeSectionIterator(contain_section);
- }
- LLVMDisposeSymbolIterator(rel_sym);
- LLVMMoveToNextRelocation(rel_itr);
- relocation++;
- }
- LLVMDisposeRelocationIterator(rel_itr);
- return true;
- fail:
- LLVMDisposeRelocationIterator(rel_itr);
- return false;
- }
- static bool
- is_relocation_section_name(char *section_name)
- {
- return (!strcmp(section_name, ".rela.text")
- || !strcmp(section_name, ".rel.text")
- || !strcmp(section_name, ".rela.literal")
- || !strcmp(section_name, ".rela.data")
- || !strcmp(section_name, ".rel.data")
- || !strcmp(section_name, ".rela.sdata")
- || !strcmp(section_name, ".rel.sdata")
- || !strcmp(section_name, ".rela.rodata")
- || !strcmp(section_name, ".rel.rodata")
- /* ".rela.rodata.cst4/8/16/.." */
- || !strncmp(section_name, ".rela.rodata.cst",
- strlen(".rela.rodata.cst"))
- /* ".rel.rodata.cst4/8/16/.." */
- || !strncmp(section_name, ".rel.rodata.cst",
- strlen(".rel.rodata.cst")));
- }
- static bool
- is_relocation_section(LLVMSectionIteratorRef sec_itr)
- {
- uint32 count = 0;
- char *name = (char *)LLVMGetSectionName(sec_itr);
- if (name) {
- if (is_relocation_section_name(name))
- return true;
- else if ((!strcmp(name, ".text") || !strcmp(name, ".rdata"))
- && get_relocations_count(sec_itr, &count) && count > 0)
- return true;
- }
- return false;
- }
- static bool
- get_relocation_groups_count(AOTObjectData *obj_data, uint32 *p_count)
- {
- uint32 count = 0;
- LLVMSectionIteratorRef sec_itr;
- if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- return false;
- }
- while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
- if (is_relocation_section(sec_itr)) {
- count++;
- }
- LLVMMoveToNextSection(sec_itr);
- }
- LLVMDisposeSectionIterator(sec_itr);
- *p_count = count;
- return true;
- }
- static bool
- aot_resolve_object_relocation_groups(AOTObjectData *obj_data)
- {
- LLVMSectionIteratorRef sec_itr;
- AOTRelocationGroup *relocation_group;
- uint32 group_count;
- char *name;
- uint32 size;
- /* calculate relocation groups count and allcate memory */
- if (!get_relocation_groups_count(obj_data, &group_count))
- return false;
- if (0 == (obj_data->relocation_group_count = group_count))
- return true;
- size = (uint32)sizeof(AOTRelocationGroup) * group_count;
- if (!(relocation_group = obj_data->relocation_groups =
- wasm_runtime_malloc(size))) {
- aot_set_last_error("allocate memory for relocation groups failed.");
- return false;
- }
- memset(obj_data->relocation_groups, 0, size);
- /* resolve each relocation group */
- if (!(sec_itr = LLVMObjectFileCopySectionIterator(obj_data->binary))) {
- aot_set_last_error("llvm get section iterator failed.");
- return false;
- }
- while (!LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
- if (is_relocation_section(sec_itr)) {
- name = (char *)LLVMGetSectionName(sec_itr);
- relocation_group->section_name = name;
- if (!aot_resolve_object_relocation_group(obj_data, relocation_group,
- sec_itr)) {
- LLVMDisposeSectionIterator(sec_itr);
- return false;
- }
- relocation_group++;
- }
- LLVMMoveToNextSection(sec_itr);
- }
- LLVMDisposeSectionIterator(sec_itr);
- return true;
- }
- static void
- destroy_relocation_groups(AOTRelocationGroup *relocation_groups,
- uint32 relocation_group_count)
- {
- uint32 i;
- AOTRelocationGroup *relocation_group = relocation_groups;
- for (i = 0; i < relocation_group_count; i++, relocation_group++)
- if (relocation_group->relocations)
- wasm_runtime_free(relocation_group->relocations);
- wasm_runtime_free(relocation_groups);
- }
- static void
- destroy_relocation_symbol_list(AOTSymbolList *symbol_list)
- {
- AOTSymbolNode *elem;
- elem = symbol_list->head;
- while (elem) {
- AOTSymbolNode *next = elem->next;
- wasm_runtime_free(elem);
- elem = next;
- }
- }
- static void
- aot_obj_data_destroy(AOTObjectData *obj_data)
- {
- if (obj_data->binary)
- LLVMDisposeBinary(obj_data->binary);
- if (obj_data->mem_buf)
- LLVMDisposeMemoryBuffer(obj_data->mem_buf);
- if (obj_data->funcs)
- wasm_runtime_free(obj_data->funcs);
- if (obj_data->data_sections)
- wasm_runtime_free(obj_data->data_sections);
- if (obj_data->relocation_groups)
- destroy_relocation_groups(obj_data->relocation_groups,
- obj_data->relocation_group_count);
- if (obj_data->symbol_list.len)
- destroy_relocation_symbol_list(&obj_data->symbol_list);
- wasm_runtime_free(obj_data);
- }
- static AOTObjectData *
- aot_obj_data_create(AOTCompContext *comp_ctx)
- {
- char *err = NULL;
- AOTObjectData *obj_data;
- LLVMTargetRef target = LLVMGetTargetMachineTarget(comp_ctx->target_machine);
- bh_print_time("Begin to emit object file to buffer");
- if (!(obj_data = wasm_runtime_malloc(sizeof(AOTObjectData)))) {
- aot_set_last_error("allocate memory failed.");
- return false;
- }
- memset(obj_data, 0, sizeof(AOTObjectData));
- bh_print_time("Begin to emit object file");
- if (!strncmp(LLVMGetTargetName(target), "arc", 3)) {
- #if defined(_WIN32) || defined(_WIN32_)
- aot_set_last_error("emit object file on Windows is unsupported.");
- goto fail;
- #else
- /* Emit to assmelby file instead for arc target
- as it cannot emit to object file */
- char file_name[] = "wasm-XXXXXX", buf[128];
- int fd, ret;
- if ((fd = mkstemp(file_name)) <= 0) {
- aot_set_last_error("make temp file failed.");
- goto fail;
- }
- /* close and remove temp file */
- close(fd);
- unlink(file_name);
- snprintf(buf, sizeof(buf), "%s%s", file_name, ".s");
- if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine,
- comp_ctx->module, buf, LLVMAssemblyFile,
- &err)
- != 0) {
- if (err) {
- LLVMDisposeMessage(err);
- err = NULL;
- }
- aot_set_last_error("emit elf to object file failed.");
- goto fail;
- }
- /* call arc gcc to compile assembly file to object file */
- /* TODO: get arc gcc from environment variable firstly
- and check whether the toolchain exists actually */
- snprintf(buf, sizeof(buf), "%s%s%s%s%s%s",
- "/opt/zephyr-sdk/arc-zephyr-elf/bin/arc-zephyr-elf-gcc ",
- "-mcpu=arcem -o ", file_name, ".o -c ", file_name, ".s");
- /* TODO: use try..catch to handle possible exceptions */
- ret = system(buf);
- /* remove temp assembly file */
- snprintf(buf, sizeof(buf), "%s%s", file_name, ".s");
- unlink(buf);
- if (ret != 0) {
- aot_set_last_error("failed to compile asm file to obj file "
- "with arc gcc toolchain.");
- goto fail;
- }
- /* create memory buffer from object file */
- snprintf(buf, sizeof(buf), "%s%s", file_name, ".o");
- ret = LLVMCreateMemoryBufferWithContentsOfFile(buf, &obj_data->mem_buf,
- &err);
- /* remove temp object file */
- snprintf(buf, sizeof(buf), "%s%s", file_name, ".o");
- unlink(buf);
- if (ret != 0) {
- if (err) {
- LLVMDisposeMessage(err);
- err = NULL;
- }
- aot_set_last_error("create mem buffer with file failed.");
- goto fail;
- }
- #endif /* end of defined(_WIN32) || defined(_WIN32_) */
- }
- else if (LLVMTargetMachineEmitToMemoryBuffer(
- comp_ctx->target_machine, comp_ctx->module, LLVMObjectFile,
- &err, &obj_data->mem_buf)
- != 0) {
- if (err) {
- LLVMDisposeMessage(err);
- err = NULL;
- }
- aot_set_last_error("llvm emit to memory buffer failed.");
- goto fail;
- }
- if (!(obj_data->binary = LLVMCreateBinary(obj_data->mem_buf, NULL, &err))) {
- if (err) {
- LLVMDisposeMessage(err);
- err = NULL;
- }
- aot_set_last_error("llvm create binary failed.");
- goto fail;
- }
- bh_print_time("Begin to resolve object file info");
- /* resolve target info/text/relocations/functions */
- if (!aot_resolve_target_info(comp_ctx, obj_data)
- || !aot_resolve_text(obj_data) || !aot_resolve_literal(obj_data)
- || !aot_resolve_object_data_sections(obj_data)
- || !aot_resolve_object_relocation_groups(obj_data)
- || !aot_resolve_functions(comp_ctx, obj_data))
- goto fail;
- return obj_data;
- fail:
- aot_obj_data_destroy(obj_data);
- return NULL;
- }
- uint8 *
- aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
- uint32 *p_aot_file_size)
- {
- AOTObjectData *obj_data = aot_obj_data_create(comp_ctx);
- uint8 *aot_file_buf, *buf, *buf_end;
- uint32 aot_file_size, offset = 0;
- if (!obj_data)
- return NULL;
- aot_file_size = get_aot_file_size(comp_ctx, comp_data, obj_data);
- if (!(buf = aot_file_buf = wasm_runtime_malloc(aot_file_size))) {
- aot_set_last_error("allocate memory failed.");
- goto fail1;
- }
- memset(aot_file_buf, 0, aot_file_size);
- buf_end = buf + aot_file_size;
- if (!aot_emit_file_header(buf, buf_end, &offset, comp_data, obj_data)
- || !aot_emit_target_info_section(buf, buf_end, &offset, comp_data,
- obj_data)
- || !aot_emit_init_data_section(buf, buf_end, &offset, comp_ctx,
- comp_data, obj_data)
- || !aot_emit_text_section(buf, buf_end, &offset, comp_data, obj_data)
- || !aot_emit_func_section(buf, buf_end, &offset, comp_data, obj_data)
- || !aot_emit_export_section(buf, buf_end, &offset, comp_data, obj_data)
- || !aot_emit_relocation_section(buf, buf_end, &offset, comp_data,
- obj_data)
- || !aot_emit_native_symbol(buf, buf_end, &offset, comp_ctx))
- goto fail2;
- #if 0
- dump_buf(buf, offset, "sections");
- #endif
- if (offset != aot_file_size) {
- aot_set_last_error("emit aot file failed.");
- goto fail2;
- }
- *p_aot_file_size = aot_file_size;
- aot_obj_data_destroy(obj_data);
- return aot_file_buf;
- fail2:
- wasm_runtime_free(aot_file_buf);
- fail1:
- aot_obj_data_destroy(obj_data);
- return NULL;
- }
- bool
- aot_emit_aot_file(AOTCompContext *comp_ctx, AOTCompData *comp_data,
- const char *file_name)
- {
- uint8 *aot_file_buf;
- uint32 aot_file_size;
- bool ret = false;
- FILE *file;
- bh_print_time("Begin to emit AOT file");
- if (!(aot_file_buf =
- aot_emit_aot_file_buf(comp_ctx, comp_data, &aot_file_size))) {
- return false;
- }
- /* write buffer to file */
- if (!(file = fopen(file_name, "wb"))) {
- aot_set_last_error("open or create aot file failed.");
- goto fail1;
- }
- if (!fwrite(aot_file_buf, aot_file_size, 1, file)) {
- aot_set_last_error("write to aot file failed.");
- goto fail2;
- }
- ret = true;
- fail2:
- fclose(file);
- fail1:
- wasm_runtime_free(aot_file_buf);
- return ret;
- }
|