-
Notifications
You must be signed in to change notification settings - Fork 786
Expand file tree
/
Copy pathaot_emit_aot_file.c
More file actions
4603 lines (4033 loc) · 155 KB
/
aot_emit_aot_file.c
File metadata and controls
4603 lines (4033 loc) · 155 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "aot_emit_aot_file.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;
/* text offset of aot_func#n */
uint64 text_offset;
/* text offset of aot_func_internal#n */
uint64 text_offset_of_aot_func_internal;
} 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 {
AOTCompContext *comp_ctx;
LLVMMemoryBufferRef mem_buf;
LLVMBinaryRef binary;
AOTTargetInfo target_info;
void *text;
uint32 text_size;
void *text_unlikely;
uint32 text_unlikely_size;
void *text_hot;
uint32 text_hot_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;
const char *stack_sizes_section_name;
uint32 stack_sizes_offset;
uint32 *stack_sizes;
} 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(const AOTObjectData *obj_data)
{
/* bit 1: 0 is 32-bit, 1 is 64-bit */
return obj_data->target_info.bin_type & 2 ? false : true;
}
static bool
is_little_endian_binary(const AOTObjectData *obj_data)
{
/* bit 0: 0 is little-endian, 1 is big-endian */
return obj_data->target_info.bin_type & 1 ? false : true;
}
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(AOTCompContext *comp_ctx, const char *s)
{
/* string size (2 bytes) + string content + '\0' */
return (uint32)sizeof(uint16) + (uint32)strlen(s) + 1;
}
static uint32
get_target_info_section_size()
{
return sizeof(AOTTargetInfo);
}
static uint32
get_init_expr_size(const AOTCompContext *comp_ctx, const AOTCompData *comp_data,
InitializerExpression *expr);
static uint32
get_mem_init_data_size(AOTCompContext *comp_ctx, AOTMemInitData *mem_init_data)
{
/* init expr type (4 bytes)
* + init expr value (4 bytes, valid value can only be i32/get_global)
* + byte count (4 bytes) + bytes */
uint32 total_size =
(uint32)(get_init_expr_size(comp_ctx, comp_ctx->comp_data,
&mem_init_data->offset)
+ 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(AOTCompContext *comp_ctx,
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(comp_ctx, *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 * (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(AOTCompContext *comp_ctx, 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_ctx,
comp_data->mem_init_data_list,
comp_data->mem_init_data_count);
}
static uint32
get_init_expr_size(const AOTCompContext *comp_ctx, const AOTCompData *comp_data,
InitializerExpression *expr)
{
/* init_expr_type */
uint32 size = sizeof(uint32);
#if WASM_ENABLE_GC != 0
WASMModule *module = comp_data->wasm_module;
#endif
/* + init value size */
switch (expr->init_expr_type) {
case INIT_EXPR_NONE:
/* no init value, used in table initializer */
break;
case INIT_EXPR_TYPE_I32_CONST:
case INIT_EXPR_TYPE_F32_CONST:
case INIT_EXPR_TYPE_GET_GLOBAL:
size += sizeof(uint32);
break;
case INIT_EXPR_TYPE_I64_CONST:
case INIT_EXPR_TYPE_F64_CONST:
size += sizeof(uint64);
break;
case INIT_EXPR_TYPE_V128_CONST:
size += sizeof(uint64) * 2;
break;
case INIT_EXPR_TYPE_FUNCREF_CONST:
case INIT_EXPR_TYPE_REFNULL_CONST:
/* ref_index */
size += sizeof(uint32);
break;
#if WASM_ENABLE_GC != 0
case INIT_EXPR_TYPE_I31_NEW:
/* i32 */
size += sizeof(uint32);
break;
case INIT_EXPR_TYPE_STRUCT_NEW:
{
uint32 i;
WASMStructNewInitValues *struct_new_init_values =
(WASMStructNewInitValues *)expr->u.data;
/* type_index + field_count + fields */
size += sizeof(uint32) + sizeof(uint32);
bh_assert(struct_new_init_values->type_idx < module->type_count);
for (i = 0; i < struct_new_init_values->count; i++) {
WASMStructType *struct_type =
(WASMStructType *)
module->types[struct_new_init_values->type_idx];
uint32 field_size;
bh_assert(struct_type);
bh_assert(struct_type->field_count
== struct_new_init_values->count);
field_size = wasm_value_type_size_internal(
struct_type->fields[i].field_type, comp_ctx->pointer_size);
if (field_size < sizeof(uint32))
field_size = sizeof(uint32);
size += field_size;
}
break;
}
case INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT:
/* type_index */
size += sizeof(uint32);
break;
case INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT:
/* array_elem_type + type_index + len */
size += sizeof(uint32) * 3;
break;
case INIT_EXPR_TYPE_ARRAY_NEW:
case INIT_EXPR_TYPE_ARRAY_NEW_FIXED:
{
WASMArrayNewInitValues *array_new_init_values =
(WASMArrayNewInitValues *)expr->u.data;
WASMArrayType *array_type = NULL;
uint32 value_count;
array_type =
(WASMArrayType *)module->types[array_new_init_values->type_idx];
bh_assert(array_type);
bh_assert(array_new_init_values->type_idx < module->type_count);
value_count =
(expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED)
? array_new_init_values->length
: 1;
/* array_elem_type + type_index + len + elems */
size += sizeof(uint32) * 3
+ wasm_value_type_size_internal(array_type->elem_type,
comp_ctx->pointer_size)
* value_count;
break;
}
#endif /* end of WASM_ENABLE_GC != 0 */
default:
bh_assert(0);
}
return size;
}
static uint32
get_table_init_data_size(AOTCompContext *comp_ctx,
AOTTableInitData *table_init_data)
{
uint32 size, i;
/*
* mode (4 bytes), elem_type (4 bytes)
*
* table_index(4 bytes) + init expr type (4 bytes) + init expr value (8
* bytes)
*/
size = (uint32)(sizeof(uint32) * 2 + sizeof(uint32) + sizeof(uint32)
+ sizeof(uint64))
/* Size of WasmRefType - inner padding (ref type + nullable +
heap_type) */
+ 8;
/* + value count/func index count (4 bytes) + init_values */
size += sizeof(uint32);
for (i = 0; i < table_init_data->value_count; i++) {
size += get_init_expr_size(comp_ctx, comp_ctx->comp_data,
&table_init_data->init_values[i]);
}
return size;
}
static uint32
get_table_init_data_list_size(AOTCompContext *comp_ctx,
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 / elem_count
* | | UINTPTR [func_index_count] / [elem_count]
* ------------------------------
*/
AOTTableInitData **table_init_data = table_init_data_list;
uint32 size = 0, i;
/* table_init_data_count(4 bytes) */
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(comp_ctx, *table_init_data);
}
return size;
}
static uint32
get_import_table_size(const AOTCompContext *comp_ctx,
const AOTCompData *comp_data)
{
/*
* ------------------------------
* | import_table_count
* ------------------------------
* | | U8 elem_type
* | | U8 flags
* | | U8 possible_grow
* | AOTImportTable[N] | U8 elem_ref_type.nullable (for GC only)
* | | U32 init_size
* | | U32 max_size
* | | U32 elem_ref_type.heap_type (for GC only)
* ------------------------------
*/
uint32 size = 0, i;
size = (uint32)sizeof(uint32);
for (i = 0; i < comp_data->import_table_count; i++) {
size += sizeof(uint32) * 3;
#if WASM_ENABLE_GC != 0
if (comp_ctx->enable_gc
&& comp_data->import_tables[i].table_type.elem_ref_type)
size += sizeof(uint32);
#endif
}
return size;
}
static uint32
get_table_size(const AOTCompContext *comp_ctx, const AOTCompData *comp_data)
{
/*
* ------------------------------
* | table_count
* ------------------------------
* | | U8 elem_type
* | | U8 flags
* | | U8 possible_grow
* | AOTTable[N] | U8 elem_ref_type.nullable (for GC only)
* | | U32 init_size
* | | U32 max_size
* | | U32 elem_ref_type.heap_type (for GC only)
* | | N init_expr (for GC only)
* ------------------------------
*/
uint32 size = 0, i;
size = (uint32)sizeof(uint32);
for (i = 0; i < comp_data->table_count; i++) {
size += sizeof(uint32) * 3;
#if WASM_ENABLE_GC != 0
if (comp_ctx->enable_gc) {
if (comp_data->tables[i].table_type.elem_ref_type) {
size += sizeof(uint32);
}
size += get_init_expr_size(comp_ctx, comp_data,
&comp_data->tables[i].init_expr);
}
#endif
}
return size;
}
static uint32
get_table_info_size(AOTCompContext *comp_ctx, 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_ctx, comp_data)
+ get_table_size(comp_ctx, comp_data)
+ get_table_init_data_list_size(comp_ctx,
comp_data->table_init_data_list,
comp_data->table_init_data_count);
}
static uint32
get_func_type_size(AOTCompContext *comp_ctx, AOTFuncType *func_type)
{
#if WASM_ENABLE_GC != 0
/* type flag + equivalence type flag + is_sub_final + parent_type_idx
+ rec_count + rec_idx + param count + result count
+ ref_type_map_count + types + context of ref_type_map */
if (comp_ctx->enable_gc) {
uint32 size = 0;
/* type flag */
size += sizeof(func_type->base_type.type_flag);
/* equivalence type flag + is_sub_final */
size += sizeof(uint16);
/* parent_type_idx */
size += sizeof(func_type->base_type.parent_type_idx);
/* rec_count */
size += sizeof(func_type->base_type.rec_count);
/* rec_idx */
size += sizeof(func_type->base_type.rec_idx);
/* param count */
size += sizeof(func_type->param_count);
/* result count */
size += sizeof(func_type->result_count);
/* ref_type_map_count */
size += sizeof(func_type->ref_type_map_count);
/* param and result types */
size += func_type->param_count + func_type->result_count;
/* align size */
size = align_uint(size, 4);
/* ref_type_map */
size += func_type->ref_type_map_count * 8;
return size;
}
else
#endif
{
/* type flag + param count + result count + types */
return (uint32)sizeof(uint16) * 3 + func_type->param_count
+ func_type->result_count;
}
}
#if WASM_ENABLE_GC != 0
static uint32
get_struct_type_size(AOTCompContext *comp_ctx, AOTStructType *struct_type)
{
uint32 size = 0;
/* type flag + equivalence type flag + is_sub_final + parent_type_idx
+ rec_count + rec_idx + field count + fields */
/* type flag */
size += sizeof(struct_type->base_type.type_flag);
/* equivalence type flag + is_sub_final */
size += sizeof(uint16);
/* parent_type_idx */
size += sizeof(struct_type->base_type.parent_type_idx);
/* rec_count */
size += sizeof(struct_type->base_type.rec_count);
/* rec_idx */
size += sizeof(struct_type->base_type.rec_idx);
/* field count */
size += sizeof(struct_type->field_count);
/* field types */
size += struct_type->field_count * 2;
/* ref_type_map_count */
size += sizeof(struct_type->ref_type_map_count);
size = align_uint(size, 4);
/* ref_type_map */
size += struct_type->ref_type_map_count * 8;
return size;
}
static uint32
get_array_type_size(AOTCompContext *comp_ctx, AOTArrayType *array_type)
{
uint32 size = 0;
/* type flag + equivalence type flag + is_sub_final + parent_type_idx
+ rec_count + rec_idx + elem_flags + elem_type + elem_ref_type */
/* type flag */
size += sizeof(array_type->base_type.type_flag);
/* equivalence type flag + is_sub_final */
size += sizeof(uint16);
/* parent_type_idx (u32) */
size += sizeof(array_type->base_type.parent_type_idx);
/* rec_count */
size += sizeof(array_type->base_type.rec_count);
/* rec_idx */
size += sizeof(array_type->base_type.rec_idx);
/* elem_flags (u16) */
size += sizeof(array_type->elem_flags);
/* elem_type (u8) */
size += sizeof(array_type->elem_type);
/* elem_ref_type */
if (array_type->elem_ref_type) {
/* nullable (u8) */
size += sizeof(uint8);
/* heap type (u32) */
size += sizeof(uint32);
}
return size;
}
#endif
static uint32
get_type_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
{
/* Initial size with size of type count */
uint32 size = 4;
uint32 i;
#if WASM_ENABLE_GC != 0
if (comp_ctx->enable_gc) {
for (i = 0; i < comp_data->type_count; i++) {
uint32 j;
size = align_uint(size, 4);
/* Emit simple info if there is an equivalence type */
for (j = 0; j < i; j++) {
if (comp_data->types[j] == comp_data->types[i]) {
/* type_flag (2 bytes) + equivalence type flag (1 byte)
+ padding (1 byte) + equivalence type index */
size += 8;
break;
}
}
if (j < i)
continue;
if (comp_data->types[i]->type_flag == WASM_TYPE_FUNC)
size += get_func_type_size(comp_ctx,
(AOTFuncType *)comp_data->types[i]);
else if (comp_data->types[i]->type_flag == WASM_TYPE_STRUCT)
size += get_struct_type_size(
comp_ctx, (AOTStructType *)comp_data->types[i]);
else if (comp_data->types[i]->type_flag == WASM_TYPE_ARRAY)
size += get_array_type_size(
comp_ctx, (AOTArrayType *)comp_data->types[i]);
else
bh_assert(0);
}
}
else
#endif
{
for (i = 0; i < comp_data->type_count; i++) {
size = align_uint(size, 4);
size += get_func_type_size(comp_ctx,
(AOTFuncType *)comp_data->types[i]);
}
}
return size;
}
static uint32
get_import_global_size(AOTCompContext *comp_ctx, AOTImportGlobal *import_global)
{
/* type (1 byte) + is_mutable (1 byte) + module_name + global_name */
uint32 size = (uint32)sizeof(uint8) * 2
+ get_string_size(comp_ctx, import_global->module_name);
size = align_uint(size, 2);
size += get_string_size(comp_ctx, import_global->global_name);
return size;
}
static uint32
get_import_globals_size(AOTCompContext *comp_ctx,
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(comp_ctx, import_global);
}
return size;
}
static uint32
get_import_global_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
{
/* import global count + import globals */
return (uint32)sizeof(uint32)
+ get_import_globals_size(comp_ctx, comp_data->import_globals,
comp_data->import_global_count);
}
static uint32
get_global_size(AOTCompContext *comp_ctx, AOTGlobal *global)
{
/* type (1 byte) + is_mutable (1 byte) + padding (2 bytes)
+ init expr value (include init expr type) */
return sizeof(uint8) * 2 + sizeof(uint8) * 2
+ get_init_expr_size(comp_ctx, comp_ctx->comp_data,
&global->init_expr);
}
static uint32
get_globals_size(AOTCompContext *comp_ctx, 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(comp_ctx, global);
}
return size;
}
static uint32
get_global_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
{
/* global count + globals */
return (uint32)sizeof(uint32)
+ get_globals_size(comp_ctx, comp_data->globals,
comp_data->global_count);
}
static uint32
get_import_func_size(AOTCompContext *comp_ctx, AOTImportFunc *import_func)
{
/* type index (2 bytes) + module_name + func_name */
uint32 size = (uint32)sizeof(uint16)
+ get_string_size(comp_ctx, import_func->module_name);
size = align_uint(size, 2);
size += get_string_size(comp_ctx, import_func->func_name);
return size;
}
static uint32
get_import_funcs_size(AOTCompContext *comp_ctx, 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(comp_ctx, import_func);
}
return size;
}
static uint32
get_import_func_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
{
/* import func count + import funcs */
return (uint32)sizeof(uint32)
+ get_import_funcs_size(comp_ctx, comp_data->import_funcs,
comp_data->import_func_count);
}
static uint32
get_object_data_sections_size(AOTCompContext *comp_ctx,
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++) {
/* name + size + data */
size = align_uint(size, 2);
size += get_string_size(comp_ctx, data_section->name);
size = align_uint(size, 4);
size += (uint32)sizeof(uint32);
size += data_section->size;
}
return size;
}
static uint32
get_object_data_section_info_size(AOTCompContext *comp_ctx,
AOTObjectData *obj_data)
{
/* data sections count + data sections */
return (uint32)sizeof(uint32)
+ get_object_data_sections_size(comp_ctx, obj_data->data_sections,
obj_data->data_sections_count);
}
static uint32
get_init_data_section_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
AOTObjectData *obj_data)
{
uint32 size = 0;
size += get_mem_info_size(comp_ctx, comp_data);
size = align_uint(size, 4);
size += get_table_info_size(comp_ctx, comp_data);
size = align_uint(size, 4);
size += get_type_info_size(comp_ctx, comp_data);
size = align_uint(size, 4);
size += get_import_global_info_size(comp_ctx, comp_data);
size = align_uint(size, 4);
size += get_global_info_size(comp_ctx, comp_data);
size = align_uint(size, 4);
size += get_import_func_info_size(comp_ctx, 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) * 10;
size += get_object_data_section_info_size(comp_ctx, obj_data);
return size;
}
static uint32
get_text_section_size(AOTObjectData *obj_data)
{
return sizeof(uint32) + align_uint(obj_data->literal_size, 4)
+ align_uint(obj_data->text_size, 4)
+ align_uint(obj_data->text_unlikely_size, 4)
+ align_uint(obj_data->text_hot_size, 4);
}
static uint32
get_func_section_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
AOTObjectData *obj_data)
{
uint32 size = 0;
/* text offsets */
if (is_32bit_binary(obj_data))
size = (uint32)sizeof(uint32) * comp_data->func_count;
else
size = (uint32)sizeof(uint64) * comp_data->func_count;
/* function type indexes */
size += (uint32)sizeof(uint32) * comp_data->func_count;
/* max_local_cell_nums */
size += (uint32)sizeof(uint32) * comp_data->func_count;
/* max_stack_cell_nums */
size += (uint32)sizeof(uint32) * comp_data->func_count;
#if WASM_ENABLE_GC != 0
/* func_local_ref_flags */
if (comp_ctx->enable_gc) {
AOTFuncType *func_type;
uint32 i, j, local_ref_flags_cell_num;
for (i = 0; i < comp_data->import_func_count; i++) {
func_type = comp_data->import_funcs[i].func_type;
/* recalculate cell_num based on target pointer size */
local_ref_flags_cell_num = 0;
for (j = 0; j < func_type->param_count; j++) {
local_ref_flags_cell_num += wasm_value_type_cell_num_internal(
func_type->types[j], comp_ctx->pointer_size);
}
local_ref_flags_cell_num =
local_ref_flags_cell_num > 2 ? local_ref_flags_cell_num : 2;
size = align_uint(size, 4);
size += (uint32)sizeof(uint32);
size += (uint32)sizeof(uint8) * local_ref_flags_cell_num;
}
for (i = 0; i < comp_data->func_count; i++) {
func_type = comp_data->funcs[i]->func_type;
local_ref_flags_cell_num = comp_data->funcs[i]->param_cell_num
+ comp_data->funcs[i]->local_cell_num;
size = align_uint(size, 4);
size += (uint32)sizeof(uint32);
size += (uint32)sizeof(uint8) * local_ref_flags_cell_num;
}
}
#endif
return size;
}
static uint32
get_export_size(AOTCompContext *comp_ctx, AOTExport *export)
{
/* export index + export kind + 1 byte padding + export name */
return (uint32)sizeof(uint32) + sizeof(uint8) + 1
+ get_string_size(comp_ctx, export->name);
}
static uint32
get_exports_size(AOTCompContext *comp_ctx, 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(comp_ctx, export);
}
return size;
}
static uint32
get_export_section_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
{
/* export count + exports */
return (uint32)sizeof(uint32)
+ get_exports_size(comp_ctx, 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(AOTObjectData *obj_data,
AOTRelocationGroup *relocation_group,
AOTRelocation *relocations, uint32 relocation_count,
bool is_32bin)
{
AOTRelocation *relocation = relocations;
uint32 size = 0, i;
for (i = 0; i < relocation_count; i++, relocation++) {
/* ignore the relocations to aot_func_internal#n in text section
for windows platform since they will be applied in
aot_emit_text_section */
const char *name = relocation->symbol_name;
if ((!strcmp(relocation_group->section_name, ".text")
|| !strcmp(relocation_group->section_name, ".ltext"))
&& !strncmp(name, AOT_FUNC_INTERNAL_PREFIX,
strlen(AOT_FUNC_INTERNAL_PREFIX))
&& ((!strncmp(obj_data->comp_ctx->target_arch, "x86_64", 6)
/* Windows AOT_COFF64_BIN_TYPE */
&& obj_data->target_info.bin_type == 6
/* IMAGE_REL_AMD64_REL32 in windows x86_64 */
&& relocation->relocation_type == 4)
|| (!strncmp(obj_data->comp_ctx->target_arch, "i386", 4)
/* Windows AOT_COFF32_BIN_TYPE */
&& obj_data->target_info.bin_type == 4
/* IMAGE_REL_I386_REL32 in windows x86_32 */
&& relocation->relocation_type == 20))) {
continue;
}
size = align_uint(size, 4);
size += get_relocation_size(relocation, is_32bin);
}
return size;
}
static uint32
get_relocation_group_size(AOTObjectData *obj_data,
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(obj_data, relocation_group,
relocation_group->relocations,
relocation_group->relocation_count, is_32bin);
return size;
}
static uint32
get_relocation_groups_size(AOTObjectData *obj_data,
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(obj_data, 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;