/
linker.64.c
2529 lines (1773 loc) · 88.7 KB
/
linker.64.c
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
/**
* @file linker.64.c
* @brief Linker implementation for both efi and turnstone executables.
*
* This work is licensed under TURNSTONE OS Public License.
* Please read and understand latest version of Licence.
*/
#include <linker.h>
#include <cpu.h>
#include <memory.h>
#include <memory/frame.h>
#include <memory/paging.h>
#include <systeminfo.h>
#include <logging.h>
#include <strings.h>
#include <efi.h>
#include <list.h>
MODULE("turnstone.lib.linker");
int8_t linker_link_module(linker_context_t* ctx, linker_module_t* module);
int8_t linker_efi_image_relocation_entry_cmp(const void* a, const void* b);
int8_t linker_efi_image_section_header_cmp(const void* a, const void* b);
buffer_t* linker_build_relocation_table_buffer(linker_context_t* ctx);
buffer_t* linker_build_metadata_buffer(linker_context_t* ctx);
const char_t*const linker_section_type_names[LINKER_SECTION_TYPE_NR_SECTIONS] = {
[LINKER_SECTION_TYPE_TEXT] = ".text",
[LINKER_SECTION_TYPE_DATA] = ".data",
[LINKER_SECTION_TYPE_DATARELOC] = ".datareloc",
[LINKER_SECTION_TYPE_RODATA] = ".rodata",
[LINKER_SECTION_TYPE_RODATARELOC] = ".rodatareloc",
[LINKER_SECTION_TYPE_BSS] = ".bss",
[LINKER_SECTION_TYPE_PLT] = ".plt",
[LINKER_SECTION_TYPE_RELOCATION_TABLE] = ".reloc",
[LINKER_SECTION_TYPE_GOT_RELATIVE_RELOCATION_TABLE] = ".gotrel",
[LINKER_SECTION_TYPE_GOT] = ".got",
[LINKER_SECTION_TYPE_STACK] = ".stack",
[LINKER_SECTION_TYPE_HEAP] = ".heap",
};
int8_t linker_efi_image_relocation_entry_cmp(const void* a, const void* b) {
efi_image_relocation_entry_t* entry_a = (efi_image_relocation_entry_t*)a;
efi_image_relocation_entry_t* entry_b = (efi_image_relocation_entry_t*)b;
if(entry_a->page_rva < entry_b->page_rva) {
return -1;
} else if(entry_a->page_rva > entry_b->page_rva) {
return 1;
}
return 0;
}
int8_t linker_efi_image_section_header_cmp(const void* a, const void* b) {
efi_image_section_header_t* header_a = (efi_image_section_header_t*)a;
efi_image_section_header_t* header_b = (efi_image_section_header_t*)b;
if(header_a->virtual_address < header_b->virtual_address) {
return -1;
} else if(header_a->virtual_address > header_b->virtual_address) {
return 1;
}
return 0;
}
int8_t linker_destroy_context(linker_context_t* ctx) {
hashmap_destroy(ctx->got_symbol_index_map);
buffer_destroy(ctx->got_table_buffer);
iterator_t* it = hashmap_iterator_create(ctx->modules);
if(!it) {
PRINTLOG(LINKER, LOG_ERROR, "cannot create iterator for modules");
return -1;
}
while(it->end_of_iterator(it) != 0) {
linker_module_t* module = (linker_module_t*)it->get_item(it);
if(!module) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get module from iterator");
return -1;
}
for(uint8_t i = 0; i < LINKER_SECTION_TYPE_NR_SECTIONS; i++) {
buffer_destroy(module->sections[i].section_data);
}
if(module->plt_offsets) {
hashmap_destroy(module->plt_offsets);
}
memory_free(module);
it = it->next(it);
}
it->destroy(it);
hashmap_destroy(ctx->modules);
memory_free(ctx);
return 0;
}
int8_t linker_build_symbols(linker_context_t* ctx, uint64_t module_id, uint64_t section_id, uint8_t section_type, uint64_t section_offset) {
int8_t res = 0;
tosdb_database_t* db_system = tosdb_database_create_or_open(ctx->tdb, "system");
tosdb_table_t* tbl_symbols = tosdb_table_create_or_open(db_system, "symbols", 1 << 10, 512 << 10, 8);
tosdb_record_t* s_sym_rec = tosdb_table_create_record(tbl_symbols);
if(!s_sym_rec) {
PRINTLOG(LINKER, LOG_ERROR, "cannot create record for searching symbols");
return -1;
}
if(!s_sym_rec->set_uint64(s_sym_rec, "section_id", section_id)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot set search key for records section_id column for section id 0x%llx", section_id);
s_sym_rec->destroy(s_sym_rec);
return -1;
}
list_t* symbols = s_sym_rec->search_record(s_sym_rec);
s_sym_rec->destroy(s_sym_rec);
if(!symbols) {
PRINTLOG(LINKER, LOG_ERROR, "cannot search symbols for section id 0x%llx", section_id);
return -1;
}
PRINTLOG(LINKER, LOG_DEBUG, "found %llu symbols for section id 0x%llx", list_size(symbols), section_id);
iterator_t* it = list_iterator_create(symbols);
if(!it) {
PRINTLOG(LINKER, LOG_ERROR, "cannot create iterator for symbols");
return -1;
}
linker_global_offset_table_entry_t got_entry = {0};
uint64_t symbol_id = 0;
uint8_t symbol_type = 0;
uint8_t symbol_scope = 0;
uint64_t symbol_value = 0;
uint64_t symbol_size = 0;
char_t* symbol_name = NULL;
while(it->end_of_iterator(it) != 0) {
tosdb_record_t* sym_rec = (tosdb_record_t*)it->delete_item(it);
if(!sym_rec) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get symbol record");
goto clean_symbols_iter;
}
if(!sym_rec->get_uint64(sym_rec, "id", &symbol_id)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get symbol id");
goto clean_symbols_iter;
}
if(!sym_rec->get_uint8(sym_rec, "type", &symbol_type)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get symbol type");
goto clean_symbols_iter;
}
if(!sym_rec->get_uint8(sym_rec, "scope", &symbol_scope)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get symbol scope");
goto clean_symbols_iter;
}
if(!sym_rec->get_uint64(sym_rec, "value", &symbol_value)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get symbol value");
goto clean_symbols_iter;
}
if(!sym_rec->get_uint64(sym_rec, "size", &symbol_size)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get symbol size");
goto clean_symbols_iter;
}
if(!sym_rec->get_string(sym_rec, "name", &symbol_name)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get symbol name");
goto clean_symbols_iter;
}
PRINTLOG(LINKER, LOG_DEBUG, "found symbol %s with id 0x%llx size 0x%llx, at section 0x%llx", symbol_name, symbol_id, symbol_size, section_id);
uint64_t got_entry_index = (uint64_t)hashmap_get(ctx->got_symbol_index_map, (void*)symbol_id);
if(got_entry_index) {
linker_global_offset_table_entry_t* existing_got_entry = (linker_global_offset_table_entry_t*)buffer_get_view_at_position(ctx->got_table_buffer, got_entry_index * sizeof(linker_global_offset_table_entry_t), sizeof(linker_global_offset_table_entry_t));
if(!existing_got_entry) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get existing got entry");
memory_free(symbol_name);
goto clean_symbols_iter;
}
if(existing_got_entry->symbol_id != symbol_id || existing_got_entry->module_id != module_id) {
PRINTLOG(LINKER, LOG_ERROR, "got entry symbol/module id mismatch");
memory_free(symbol_name);
goto clean_symbols_iter;
}
existing_got_entry->resolved = true;
existing_got_entry->symbol_type = symbol_type;
existing_got_entry->symbol_scope = symbol_scope;
existing_got_entry->symbol_value = symbol_value + section_offset;
existing_got_entry->symbol_size = symbol_size;
existing_got_entry->section_type = section_type;
} else {
memory_memclean(&got_entry, sizeof(linker_global_offset_table_entry_t));
got_entry.resolved = true;
got_entry.module_id = module_id;
got_entry.symbol_id = symbol_id;
got_entry.symbol_type = symbol_type;
got_entry.symbol_scope = symbol_scope;
got_entry.symbol_value = symbol_value + section_offset;
got_entry.symbol_size = symbol_size;
got_entry.section_type = section_type;
if(ctx->symbol_table_buffer) {
uint64_t symbol_table_index = buffer_get_length(ctx->symbol_table_buffer);
buffer_append_bytes(ctx->symbol_table_buffer, (uint8_t*)symbol_name, strlen(symbol_name) + 1);
got_entry.symbol_name_offset = symbol_table_index;
}
got_entry_index = buffer_get_length(ctx->got_table_buffer) / sizeof(linker_global_offset_table_entry_t);
buffer_append_bytes(ctx->got_table_buffer, (uint8_t*)&got_entry, sizeof(linker_global_offset_table_entry_t));
hashmap_put(ctx->got_symbol_index_map, (void*)symbol_id, (void*)got_entry_index);
}
PRINTLOG(LINKER, LOG_DEBUG, "added symbol %s with id 0x%llx, at section 0x%llx, to got table at index 0x%llx", symbol_name, symbol_id, section_id, got_entry_index);
memory_free(symbol_name);
sym_rec->destroy(sym_rec);
it = it->next(it);
}
it->destroy(it);
list_destroy(symbols);
return res;
clean_symbols_iter:
while(it->end_of_iterator(it) != 0) {
tosdb_record_t* sym_rec = (tosdb_record_t*)it->delete_item(it);
if(sym_rec) {
sym_rec->destroy(sym_rec);
}
it = it->next(it);
}
it->destroy(it);
list_destroy(symbols);
return -1;
}
const uint8_t linker_vm_plt0_entry_data[] = {
0x50, // push %rax
0x48, 0xc7, 0xc0, 0x00, 0x10, 0x00, 0x00, // mov $0x1000, %rax
0x0f, 0x01, 0xc1, // vmcall
0x48, 0x85, 0xc0, // test %rax,%rax
0x75, 0x0c, // jne 1c failed
0x58, // pop %rax
0x41, 0x5e, // pop %r14
0x4f, 0x8b, 0x1c, 0x3b, // mov (%r11,%r15,1),%r11
// 0x41, 0x0f, 0x20, 0xdf, // mov %cr3,%r15
// 0x41, 0x0f, 0x22, 0xdf, // mov %r15,%cr3
0x41, 0x5f, // pop %r15
0x41, 0xff, 0xe3, // jmp *%r11
0xfa, // failed: cli
0x48, 0x8d, 0x0d, 0x15, 0x00, 0x00, 0x00, // lea 0x15(%rip),%rcx failed_msg_size
0x48, 0x8b, 0x09, // mov (%rcx),%rcx
0x48, 0x8d, 0x35, 0x13, 0x00, 0x00, 0x00, // lea 0xf(%rip),%rsi failed_msg
0x66, 0xba, 0xf8, 0x03, // mov $0x3f8,%dx
0xac, // failed_print: lods %ds:(%rsi),%al
0xee, // out %al,(%dx)
0xe2, 0xfc, // loop failed_print
0xf4, // failed_loop: hlt
0xeb, 0xfd, // jmp failed_loop
0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // failed_msg_size
0x64, 0x79, 0x6e, 0x61, 0x6d, 0x69, 0x63, 0x20, // dynamic
0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x20, // loader
0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x2e, 0x20, // failed.
0x68, 0x61, 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x2e, 0x2e, 0x2e, 0x0a, 0x00, // halting...
0x0f, 0x1f, 0x04, 0x00, // nopl (%rax,%rax,1)
0x0f, 0x1f, 0x04, 0x00, // nopl (%rax,%rax,1)
0x0f, 0x1f, 0x04, 0x00, // nopl (%rax,%rax,1)
0x0f, 0x1f, 0x04, 0x00, // nopl (%rax,%rax,1)
0x0f, 0x1f, 0x04, 0x00, // nopl (%rax,%rax,1)
0x0f, 0x1f, 0x04, 0x00, // nopl (%rax,%rax,1)
0x0f, 0x1f, 0x04, 0x00, // nopl (%rax,%rax,1)
};
_Static_assert(sizeof(linker_vm_plt0_entry_data) == 0x80, "plt0 entry size mismatch");
const uint8_t linker_plt_entry_data[] = {
0x41, 0x57, // push %r15
0x41, 0x56, // push %r14
0x49, 0xbf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // movabs $_GLOBAL_OFFSET_TABLE_, %r15
0x4c, 0x8d, 0x35, 0xeb, 0xff, 0xff, 0xff, // lea -0x15(%rip),%r14
0x4d, 0x01, 0xf7, // add %r14,%r15
0x49, 0xbb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // movabs $symbol@GOT, %r11
0x4f, 0x8b, 0x74, 0x3b, 0x08, // mov 0x8(%r11,%r15,1),%r14
0x49, 0x0f, 0xba, 0xe6, 0x00, // bt $0x0, %r14
0x73, 0x0b, // jae <notfound>
0x41, 0x5e, // pop %r14
0x4f, 0x8b, 0x1c, 0x3b, // mov (%r11,%r15,1),%r11
0x41, 0x5f, // pop %r15
0x41, 0xff, 0xe3, // jmp *%r11
0xe9, 0x00, 0x00, 0x00, 0x00, // notfound: jmp PLT0
0x90, // nop
0x90, // nop
};
_Static_assert(sizeof(linker_plt_entry_data) == 0x40, "plt entry size mismatch");
int8_t linker_build_relocations(linker_context_t* ctx, uint64_t section_id, uint8_t section_type, uint64_t section_offset, linker_module_t* module, boolean_t recursive) {
int8_t res = 0;
linker_section_t* reloc_section = &module->sections[LINKER_SECTION_TYPE_RELOCATION_TABLE];
tosdb_database_t* db_system = tosdb_database_create_or_open(ctx->tdb, "system");
tosdb_table_t* tbl_sections = tosdb_table_create_or_open(db_system, "sections", 1 << 10, 512 << 10, 8);
tosdb_table_t* tbl_relocations = tosdb_table_create_or_open(db_system, "relocations", 1 << 10, 512 << 10, 8);
tosdb_record_t* s_rel_reloc = tosdb_table_create_record(tbl_relocations);
if(!s_rel_reloc) {
PRINTLOG(LINKER, LOG_ERROR, "cannot create record for searching relocations");
return -1;
}
if(!s_rel_reloc->set_uint64(s_rel_reloc, "section_id", section_id)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot set search key for records section_id column for section id 0x%llx", section_id);
s_rel_reloc->destroy(s_rel_reloc);
return -1;
}
PRINTLOG(LINKER, LOG_TRACE, "searching relocations for section id 0x%llx", section_id);
list_t* relocations = s_rel_reloc->search_record(s_rel_reloc);
s_rel_reloc->destroy(s_rel_reloc);
if(!relocations) {
PRINTLOG(LINKER, LOG_ERROR, "cannot search relocations for section id 0x%llx", section_id);
return -1;
}
PRINTLOG(LINKER, LOG_DEBUG, "relocations count of section 0x%llx: 0x%llx", section_id, list_size(relocations));
iterator_t* it = list_iterator_create(relocations);
if(!it) {
PRINTLOG(LINKER, LOG_ERROR, "cannot create iterator for relocations");
return -1;
}
linker_relocation_entry_t relocation = {0};
int64_t reloc_id = 0;
int64_t symbol_section_id = 0;
int64_t symbol_id = 0;
int8_t reloc_type = 0;
int64_t reloc_offset = 0;
int64_t reloc_addend = 0;
char_t* symbol_name = NULL;
int64_t module_id = 0;
if(!reloc_section->section_data) {
reloc_section->section_data = buffer_new();
}
while(it->end_of_iterator(it) != 0) {
tosdb_record_t* reloc_rec = (tosdb_record_t*)it->delete_item(it);
boolean_t is_got_symbol = false;
boolean_t symbol_id_missing = false;
if(!reloc_rec) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get relocation record");
goto clean_relocs_iter;
}
PRINTLOG(LINKER, LOG_TRACE, "parsing relocation record");
if(!reloc_rec->get_int64(reloc_rec, "id", &reloc_id)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get relocation id");
reloc_rec->destroy(reloc_rec);
goto clean_relocs_iter;
}
if(!reloc_rec->get_int64(reloc_rec, "symbol_id", &symbol_id)) {
symbol_id_missing = true;
}
if(!reloc_rec->get_int64(reloc_rec, "symbol_section_id", &symbol_section_id)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get relocation symbol section id for relocation id 0x%llx", reloc_id);
reloc_rec->destroy(reloc_rec);
goto clean_relocs_iter;
}
if(!reloc_rec->get_string(reloc_rec, "symbol_name", &symbol_name)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get relocation symbol name for relocation id 0x%llx", reloc_id);
reloc_rec->destroy(reloc_rec);
goto clean_relocs_iter;
}
PRINTLOG(LINKER, LOG_DEBUG, "relocation 0x%llx symbol name: %s id 0x%llx", reloc_id, symbol_name, symbol_id);
if(strcmp(symbol_name, "_GLOBAL_OFFSET_TABLE_") == 0) {
PRINTLOG(LINKER, LOG_TRACE, "found _GLOBAL_OFFSET_TABLE_ symbol for relocation at section 0x%llx", section_id);
is_got_symbol = true;
symbol_id = LINKER_GOT_SYMBOL_ID;
}
if(symbol_id_missing && !is_got_symbol) {
PRINTLOG(LINKER, LOG_ERROR, "symbol id is missing for symbol %s, relocation at section 0x%llx relocation id 0x%llx", symbol_name, section_id, reloc_id);
reloc_rec->destroy(reloc_rec);
memory_free(symbol_name);
goto clean_relocs_iter;
}
if(symbol_section_id == 0) {
if(!is_got_symbol) {
PRINTLOG(LINKER, LOG_ERROR, "symbol section id is missing for symbol %s(%lli), relocation at section 0x%llx relocation id 0x%llx", symbol_name, symbol_id, section_id, reloc_id);
PRINTLOG(LINKER, LOG_ERROR, "relocation record deleted? %s", reloc_rec->is_deleted(reloc_rec) ? "yes" : "no");
reloc_rec->destroy(reloc_rec);
memory_free(symbol_name);
goto clean_relocs_iter;
} else {
symbol_section_id = LINKER_GOT_SECTION_ID;
}
}
memory_free(symbol_name);
if(!reloc_rec->get_int8(reloc_rec, "type", &reloc_type)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get relocation type for relocation id 0x%llx", reloc_id);
reloc_rec->destroy(reloc_rec);
goto clean_relocs_iter;
}
if(!reloc_rec->get_int64(reloc_rec, "offset", &reloc_offset)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get relocation offset for relocation id 0x%llx", reloc_id);
reloc_rec->destroy(reloc_rec);
goto clean_relocs_iter;
}
if(!reloc_rec->get_int64(reloc_rec, "addend", &reloc_addend)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get relocation addend for relocation id 0x%llx", reloc_id);
reloc_rec->destroy(reloc_rec);
goto clean_relocs_iter;
}
if(!is_got_symbol) {
tosdb_record_t* s_sec_rec = tosdb_table_create_record(tbl_sections);
if(!s_sec_rec) {
PRINTLOG(LINKER, LOG_ERROR, "cannot create record for searching section");
reloc_rec->destroy(reloc_rec);
goto clean_relocs_iter;
}
if(!s_sec_rec->set_uint64(s_sec_rec, "id", symbol_section_id)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot set search key for records id column for section id 0x%llx", symbol_section_id);
reloc_rec->destroy(reloc_rec);
s_sec_rec->destroy(s_sec_rec);
goto clean_relocs_iter;
}
if(!s_sec_rec->get_record(s_sec_rec)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get section record for section id 0x%llx for relocation 0x%llx", symbol_section_id, reloc_id);
reloc_rec->destroy(reloc_rec);
s_sec_rec->destroy(s_sec_rec);
goto clean_relocs_iter;
}
if(!s_sec_rec->get_int64(s_sec_rec, "module_id", &module_id)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get section module id, is deleted? %d", s_sec_rec->is_deleted(s_sec_rec));
reloc_rec->destroy(reloc_rec);
s_sec_rec->destroy(s_sec_rec);
goto clean_relocs_iter;
}
s_sec_rec->destroy(s_sec_rec);
PRINTLOG(LINKER, LOG_DEBUG, "relocation 0x%llx source symbol section id 0x%llx", reloc_id, symbol_section_id);
}
if(reloc_type == LINKER_RELOCATION_TYPE_64_PLTOFF64) {
PRINTLOG(LINKER, LOG_TRACE, "relocation 0x%llx is PLTOFF64", reloc_id);
linker_section_t* plt_section = &module->sections[LINKER_SECTION_TYPE_PLT];
if(!module->plt_offsets) {
module->plt_offsets = hashmap_integer(128);
if(!module->plt_offsets) {
PRINTLOG(LINKER, LOG_ERROR, "cannot create plt offsets hashmap");
reloc_rec->destroy(reloc_rec);
goto clean_relocs_iter;
}
}
if(buffer_get_length(plt_section->section_data) == 0) {
PRINTLOG(LINKER, LOG_TRACE, "module 0x%llx needs PLT section", module->id);
plt_section->section_data = buffer_new();
if(!plt_section->section_data) {
PRINTLOG(LINKER, LOG_ERROR, "cannot create plt section data buffer");
reloc_rec->destroy(reloc_rec);
goto clean_relocs_iter;
}
if(ctx->for_hypervisor_application) {
buffer_append_bytes(plt_section->section_data, (uint8_t*)linker_vm_plt0_entry_data, sizeof(linker_vm_plt0_entry_data));
} else {
uint32_t nopl = 0x041f0f;
// fill first 64 bytes with nopl 0x0(%rax,%rax,1)
for(int64_t idx = 0; idx < 32; idx++) {
buffer_append_bytes(plt_section->section_data, (uint8_t*)&nopl, sizeof(uint32_t));
}
}
// each modules PLT0 entry needs to be defined as symbol with id module_id << 32
// also we need to add it to got table
linker_global_offset_table_entry_t got_entry = {0};
uint64_t plt_symbol_id = module->id << 32; // may be we have over 0x100000000 symbols ???
got_entry.resolved = true;
got_entry.module_id = module->id;
got_entry.symbol_id = plt_symbol_id;
got_entry.symbol_type = LINKER_SYMBOL_TYPE_FUNCTION;
got_entry.symbol_scope = LINKER_SYMBOL_SCOPE_LOCAL;
got_entry.symbol_value = 0;
got_entry.symbol_size = 4;
got_entry.section_type = LINKER_SECTION_TYPE_PLT;
uint64_t got_entry_index = buffer_get_length(ctx->got_table_buffer) / sizeof(linker_global_offset_table_entry_t);
buffer_append_bytes(ctx->got_table_buffer, (uint8_t*)&got_entry, sizeof(linker_global_offset_table_entry_t));
hashmap_put(ctx->got_symbol_index_map, (void*)plt_symbol_id, (void*)got_entry_index);
PRINTLOG(LINKER, LOG_TRACE, "added PLT0 0x%llx entry for module id 0x%llx to got table at index 0x%llx", plt_symbol_id, module->id, got_entry_index);
}
uint64_t plt_offset = buffer_get_length(plt_section->section_data);
hashmap_put(module->plt_offsets, (void*)symbol_id, (void*)plt_offset);
_Static_assert(sizeof(linker_global_offset_table_entry_t) == 0x38, "fix plt entry values");
buffer_append_bytes(plt_section->section_data, (uint8_t*)linker_plt_entry_data, sizeof(linker_plt_entry_data));
plt_section->size = buffer_get_length(plt_section->section_data);
// each PLT entry is 0x40 bytes
// it has 3 relocations
// 1. at 0x6 a GOTPC64 for _GLOBAL_OFFSET_TABLE_ (symbol id 0x1)
// 2. at 0x1a a GOT64 for the symbol
// 3. at 0x3a a PC32 for the PLT0 entry
memory_memclean(&relocation, sizeof(linker_relocation_entry_t));
relocation.symbol_id = 1;
relocation.section_type = LINKER_SECTION_TYPE_PLT;
relocation.relocation_type = LINKER_RELOCATION_TYPE_64_GOTPC64;
relocation.offset = plt_offset + 0x6;
relocation.addend = 6; // two push instructions before this and it is also 2 bytes
buffer_append_bytes(reloc_section->section_data, (uint8_t*)&relocation, sizeof(linker_relocation_entry_t));
reloc_section->size += sizeof(linker_relocation_entry_t);
memory_memclean(&relocation, sizeof(linker_relocation_entry_t));
relocation.symbol_id = symbol_id;
relocation.section_type = LINKER_SECTION_TYPE_PLT;
relocation.relocation_type = LINKER_RELOCATION_TYPE_64_GOT64;
relocation.offset = plt_offset + 0x1a;
relocation.addend = 0;
buffer_append_bytes(reloc_section->section_data, (uint8_t*)&relocation, sizeof(linker_relocation_entry_t));
reloc_section->size += sizeof(linker_relocation_entry_t);
memory_memclean(&relocation, sizeof(linker_relocation_entry_t));
relocation.symbol_id = module->id << 32;
relocation.section_type = LINKER_SECTION_TYPE_PLT;
relocation.relocation_type = LINKER_RELOCATION_TYPE_64_PC32;
relocation.offset = plt_offset + 0x3a;
relocation.addend = -4;
buffer_append_bytes(reloc_section->section_data, (uint8_t*)&relocation, sizeof(linker_relocation_entry_t));
reloc_section->size += sizeof(linker_relocation_entry_t);
PRINTLOG(LINKER, LOG_TRACE, "added PLT entry for symbol 0x%llx at offset 0x%llx for module id 0x%llx", symbol_id, plt_offset, module->id);
}
memory_memclean(&relocation, sizeof(linker_relocation_entry_t));
relocation.symbol_id = symbol_id;
relocation.section_type = section_type;
relocation.relocation_type = reloc_type;
relocation.offset = reloc_offset + section_offset;
relocation.addend = reloc_addend;
buffer_append_bytes(reloc_section->section_data, (uint8_t*)&relocation, sizeof(linker_relocation_entry_t));
reloc_section->size += sizeof(linker_relocation_entry_t);
if(!is_got_symbol) {
PRINTLOG(LINKER, LOG_TRACE, "check if symbol 0x%llx loaded?", symbol_id);
uint64_t got_index = (uint64_t)hashmap_get(ctx->got_symbol_index_map, (void*)symbol_id);
if(!got_index) {
if(recursive) {
PRINTLOG(LINKER, LOG_TRACE, "cannot get got index for symbol 0x%llx for module 0x%llx, recursive loading", symbol_id, module_id);
int8_t recursive_res = linker_build_module(ctx, module_id, recursive);
if( recursive_res == -1) {
PRINTLOG(LINKER, LOG_ERROR, "cannot build module for got symbol 0x%llx module 0x%llx", symbol_id, module_id);
reloc_rec->destroy(reloc_rec);
goto clean_relocs_iter;
} else if(recursive_res == -2) {
PRINTLOG(LINKER, LOG_TRACE, "module 0x%llx still loading", module_id);
} else {
got_index = (uint64_t)hashmap_get(ctx->got_symbol_index_map, (void*)symbol_id);
if(!got_index) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get got index for symbol 0x%llx after recursive loading", symbol_id);
reloc_rec->destroy(reloc_rec);
goto clean_relocs_iter;
} else {
PRINTLOG(LINKER, LOG_TRACE, "symbol 0x%llx loaded, got index 0x%llx", symbol_id, got_index);
}
}
} else {
linker_global_offset_table_entry_t got_entry = {0};
memory_memclean(&got_entry, sizeof(linker_global_offset_table_entry_t));
got_entry.module_id = module_id;
got_entry.symbol_id = symbol_id;
got_index = buffer_get_length(ctx->got_table_buffer) / sizeof(linker_global_offset_table_entry_t);
buffer_append_bytes(ctx->got_table_buffer, (uint8_t*)&got_entry, sizeof(linker_global_offset_table_entry_t));
hashmap_put(ctx->got_symbol_index_map, (void*)symbol_id, (void*)got_index);
}
}
}
reloc_rec->destroy(reloc_rec);
it = it->next(it);
}
it->destroy(it);
list_destroy(relocations);
return res;
clean_relocs_iter:
while(it->end_of_iterator(it) != 0) {
tosdb_record_t* reloc_rec = (tosdb_record_t*)it->delete_item(it);
if(reloc_rec) {
reloc_rec->destroy(reloc_rec);
}
it = it->next(it);
}
it->destroy(it);
list_destroy(relocations);
return -1;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak"
int8_t linker_build_module(linker_context_t* ctx, uint64_t module_id, boolean_t recursive) {
int8_t res = 0;
tosdb_database_t* db_system = tosdb_database_create_or_open(ctx->tdb, "system");
tosdb_table_t* tbl_sections = tosdb_table_create_or_open(db_system, "sections", 1 << 10, 512 << 10, 8);
linker_module_t* module = (linker_module_t*)hashmap_get(ctx->modules, (void*)module_id);
if(!module) {
module = memory_malloc(sizeof(linker_module_t));
if(!module) {
PRINTLOG(LINKER, LOG_ERROR, "cannot allocate memory for module 0x%llx", module_id);
return -1;
}
if(ctx->symbol_table_buffer) {
tosdb_table_t* tbl_modules = tosdb_table_create_or_open(db_system, "modules", 1 << 10, 512 << 10, 8);
tosdb_record_t* s_mod_rec = tosdb_table_create_record(tbl_modules);
if(!s_mod_rec) {
PRINTLOG(LINKER, LOG_ERROR, "cannot create record for searching modules");
return -1;
}
if(!s_mod_rec->set_uint64(s_mod_rec, "id", module_id)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot set search key for records id column for module id 0x%llx", module_id);
s_mod_rec->destroy(s_mod_rec);
return -1;
}
if(!s_mod_rec->get_record(s_mod_rec)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get module record for module id 0x%llx", module_id);
s_mod_rec->destroy(s_mod_rec);
return -1;
}
char_t* module_name = NULL;
if(!s_mod_rec->get_string(s_mod_rec, "name", &module_name)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get module name for module id 0x%llx", module_id);
s_mod_rec->destroy(s_mod_rec);
return -1;
}
s_mod_rec->destroy(s_mod_rec);
uint64_t symbol_table_index = buffer_get_length(ctx->symbol_table_buffer);
buffer_append_bytes(ctx->symbol_table_buffer, (uint8_t*)module_name, strlen(module_name) + 1);
module->module_name_offset = symbol_table_index;
memory_free(module_name);
}
module->id = module_id;
hashmap_put(ctx->modules, (void*)module_id, module);
} else {
if(recursive) {
return -2;
}
}
tosdb_record_t* s_sec_rec = tosdb_table_create_record(tbl_sections);
if(!s_sec_rec) {
PRINTLOG(LINKER, LOG_ERROR, "cannot create record for searching sections");
return -1;
}
if(!s_sec_rec->set_uint64(s_sec_rec, "module_id", module_id)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot set search key for records module_id column for module id 0x%llx", module_id);
s_sec_rec->destroy(s_sec_rec);
return -1;
}
list_t* sections = s_sec_rec->search_record(s_sec_rec);
s_sec_rec->destroy(s_sec_rec);
if(!sections) {
PRINTLOG(LINKER, LOG_ERROR, "cannot search sections for module id 0x%llx", module_id);
return -1;
}
PRINTLOG(LINKER, LOG_DEBUG, "module 0x%llx sections count: %llu", module_id, list_size(sections));
uint64_t section_id = 0;
uint8_t section_type = 0;
uint8_t* section_data = NULL;
uint64_t section_size = 0;
uint64_t tmp_section_size = 0;
int64_t section_alignment = 0;
char_t* section_name = NULL;
uint64_t section_offset = 0;
iterator_t* it = list_iterator_create(sections);
while(it->end_of_iterator(it) != 0) {
tosdb_record_t* sec_rec = (tosdb_record_t*)it->delete_item(it);
if(!sec_rec) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get section record");
goto clean_secs_iter;
}
if(!sec_rec->get_uint64(sec_rec, "id", §ion_id)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get section id");
sec_rec->destroy(sec_rec);
goto clean_secs_iter;
}
if(!sec_rec->get_uint8(sec_rec, "type", §ion_type)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get section type");
sec_rec->destroy(sec_rec);
goto clean_secs_iter;
}
if(!sec_rec->get_uint64(sec_rec, "size", §ion_size)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get section size");
sec_rec->destroy(sec_rec);
goto clean_secs_iter;
}
if(!sec_rec->get_int64(sec_rec, "alignment", §ion_alignment)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get section aligment");
sec_rec->destroy(sec_rec);
goto clean_secs_iter;
}
uint64_t padding = 0;
if(module->sections[section_type].size % section_alignment) {
padding = section_alignment - (module->sections[section_type].size % section_alignment);
}
module->sections[section_type].size += padding;
if(section_type != LINKER_SECTION_TYPE_BSS) {
section_data = NULL;
if(!sec_rec->get_bytearray(sec_rec, "value", &tmp_section_size, §ion_data)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get section data");
sec_rec->destroy(sec_rec);
goto clean_secs_iter;
}
if(!section_data) {
PRINTLOG(LINKER, LOG_ERROR, "section data is NULL");
sec_rec->destroy(sec_rec);
goto clean_secs_iter;
}
if(tmp_section_size != section_size) {
PRINTLOG(LINKER, LOG_ERROR, "section size mismatch");
memory_free(section_data);
sec_rec->destroy(sec_rec);
goto clean_secs_iter;
}
if(!module->sections[section_type].section_data) {
module->sections[section_type].section_data = buffer_new();
}
uint8_t zero = 0x0;
for(uint64_t i = 0; i < padding; i++) {
buffer_append_byte(module->sections[section_type].section_data, zero);
}
section_offset = buffer_get_length(module->sections[section_type].section_data);
if(section_offset % section_alignment) {
PRINTLOG(LINKER, LOG_ERROR, "section offset alignment mismatch");
memory_free(section_data);
sec_rec->destroy(sec_rec);
goto clean_secs_iter;
}
if(section_offset != module->sections[section_type].size) {
PRINTLOG(LINKER, LOG_ERROR, "section offset mismatch");
memory_free(section_data);
sec_rec->destroy(sec_rec);
goto clean_secs_iter;
}
if(!buffer_append_bytes(module->sections[section_type].section_data, section_data, section_size)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot append section data");
memory_free(section_data);
sec_rec->destroy(sec_rec);
goto clean_secs_iter;
}
uint64_t section_end = buffer_get_length(module->sections[section_type].section_data);
if(section_end != section_offset + section_size) {
PRINTLOG(LINKER, LOG_ERROR, "section end mismatch");
memory_free(section_data);
sec_rec->destroy(sec_rec);
goto clean_secs_iter;
}
memory_free(section_data);
} else {
section_offset = module->sections[section_type].size;
}
if(!sec_rec->get_string(sec_rec, "name", §ion_name)) {
PRINTLOG(LINKER, LOG_ERROR, "cannot get section name");
sec_rec->destroy(sec_rec);
goto clean_secs_iter;
}
PRINTLOG(LINKER, LOG_DEBUG, "module id 0x%llx section id: 0x%llx, type: %u, name: %s offset 0x%llx alignment 0x%llx size 0x%llx, padding 0x%llx",
module_id, section_id, section_type, section_name, section_offset, section_alignment, section_size, padding);
memory_free(section_name);
if(linker_build_symbols(ctx, module_id, section_id, section_type, section_offset) != 0) {
PRINTLOG(LINKER, LOG_ERROR, "cannot build symbols for section id 0x%llx", section_id);
sec_rec->destroy(sec_rec);
goto clean_secs_iter;
}
if(linker_build_relocations(ctx, section_id, section_type, section_offset, module, recursive) != 0) {
PRINTLOG(LINKER, LOG_ERROR, "cannot build relocations for section id 0x%llx", section_id);