-
Notifications
You must be signed in to change notification settings - Fork 2
/
tpo_mod.c
1061 lines (800 loc) · 27.1 KB
/
tpo_mod.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
/* copyright iadix 2016 */
#define LIBC_API C_EXPORT
#include "base/std_def.h"
#include "base/std_mem.h"
#include "base/std_str.h"
#include "base/mem_base.h"
#define KERNEL_API C_EXPORT
#include "include/strs.h"
#include "include/fsio.h"
#include "include/mem_stream.h"
#include "include/tpo_mod.h"
unsigned int debug = 0xFFFFFFFF;
extern unsigned int kernel_log_id;
KERNEL_API unsigned int KERN_API_FUNC tpo_mod_imp_func_addr_c(unsigned int mod_hash, unsigned int crc_func);
KERNEL_API unsigned int KERN_API_FUNC tpo_mod_add_func_addr_c(unsigned int mod_hash, unsigned int crc_func, unsigned int func_addr);
KERNEL_API unsigned int KERN_API_FUNC tpo_add_mod_c(unsigned int mod_hash, unsigned int deco_type, unsigned int string_table_addr);
KERNEL_API unsigned int KERN_API_FUNC tpo_mod_add_section_c(unsigned int mod_idx, unsigned int section_addr, unsigned int section_size);
KERNEL_API struct kern_mod_fn_t *KERN_API_FUNC tpo_mod_add_func_c(unsigned int mod_idx, unsigned int func_addr, unsigned int func_type, unsigned int string_id);
KERNEL_API struct kern_mod_t *KERN_API_FUNC tpo_get_mod_entry_hash_c(unsigned int mod_hash);
KERNEL_API struct kern_mod_t *KERN_API_FUNC tpo_get_mod_entry_idx_c(unsigned int idx);
KERNEL_API struct kern_mod_sec_t *KERN_API_FUNC tpo_get_mod_sec_idx_c(unsigned int mod_idx, unsigned int sec_idx);
KERNEL_API struct kern_mod_fn_t *KERN_API_FUNC tpo_get_fn_entry_idx_c(unsigned int mod_hash, unsigned int idx_func);
KERNEL_API struct kern_mod_fn_t *KERN_API_FUNC tpo_get_fn_entry_hash_c(unsigned int mod_hash, unsigned int crc_func);
KERNEL_API struct kern_mod_fn_t *KERN_API_FUNC tpo_get_fn_entry_name_c(unsigned int mod_idx, unsigned int mod_hash, unsigned int str_idx, unsigned int deco_type);
KERNEL_API unsigned int KERN_API_FUNC tpo_calc_exp_func_hash_c(unsigned int mod_idx, unsigned int string_id);
KERNEL_API unsigned int KERN_API_FUNC tpo_calc_exp_func_hash_name_c(const char *func_name, unsigned int deco_type);
KERNEL_API unsigned int KERN_API_FUNC tpo_calc_imp_func_hash_name_c(const char *func_name, unsigned int src_deco_type, unsigned int deco_type);
/*KERNEL_API unsigned int KERN_API_FUNC sys_add_tpo_mod_func_name(const char *name, const char *fn_name, mem_ptr addr,unsigned int deco );*/
OS_API_C_FUNC(void) tpo_mod_init (tpo_mod_file *tpo_mod)
{
int n;
tpo_mod->data_sections.zone=PTR_NULL;
allocate_new_empty_zone (0,&tpo_mod->data_sections);
memset_c (tpo_mod->name,0,64);
n=0;
while(n<16)
{
tpo_mod->sections[n].section_ptr =0xFFFFFFFF;
tpo_mod->sections[n].section_img_ofset =0;
tpo_mod->sections[n].section_size =0;
tpo_mod->sections[n].exports_fnc.zone =PTR_NULL;
tpo_mod->sections[n].imports_fnc.zone =PTR_NULL;
allocate_new_empty_zone (0,&tpo_mod->sections[n].exports_fnc);
n++;
}
}
struct kern_mod_fn_t *find_sym(unsigned int mod_idx,size_t sym_ofset, unsigned int deco_type)
{
unsigned int n;
struct kern_mod_t *mod;
n = 0;
while ( (mod=tpo_get_mod_entry_idx_c(n)) != PTR_NULL)
{
struct kern_mod_fn_t *func_ptr;
func_ptr = tpo_get_fn_entry_name_c(mod_idx, mod->mod_hash, sym_ofset, deco_type);
if (func_ptr != PTR_FF)
return func_ptr;
n++;
}
return PTR_FF;
}
unsigned int tpo_mod_add_section (tpo_mod_file *tpo_mod,mem_size img_ofset,mem_ptr ptr,mem_size section_size,unsigned int *crc_data)
{
unsigned int n;
size_t new_size;
size_t total_size;
n=0;
while(n<16)
{
if(tpo_mod->sections[n].section_ptr==0xFFFFFFFF)
{
mem_ptr new_sec_ptr;
total_size = get_zone_size (&tpo_mod->data_sections);
new_size = total_size+section_size;
if(realloc_zone(&tpo_mod->data_sections,new_size)<0)
{
/*
log_message (kernel_log_id,"could not realloc ");
writeptr (get_zone_ptr(&tpo_mod->data_sections,0));
writestr (" size : ");
writesz (total_size,16);
writestr (" newsize : ");
writesz (new_size,16);
writestr ("\n");
*/
return 0xFFFFFFFF;
}
else
{
/*
new_sec_ptr = get_zone_ptr(&tpo_mod->data_sections,0);
writestr("secion data realloc from ");
writeptr(cur_sec_ptr);
writestr("to ");
writeptr(new_sec_ptr);
writestr("(");
writeint(mem_sub(cur_sec_ptr,new_sec_ptr),16);
writestr(")");
writestr("\n");
*/
}
tpo_mod->sections[n].section_ptr = total_size;
tpo_mod->sections[n].section_img_ofset = img_ofset;
tpo_mod->sections[n].section_size = section_size;
new_sec_ptr = get_zone_ptr(&tpo_mod->data_sections,tpo_mod->sections[n].section_ptr);
memcpy_c (new_sec_ptr,ptr,section_size);
(*crc_data)=calc_crc32_c(new_sec_ptr,(unsigned int)section_size);
return n;
}
n++;
}
return 0xFFFFFFFF;
}
size_t tpo_mod_get_section_img_ofset (tpo_mod_file *tpo_mod,unsigned int sec_idx)
{
return tpo_mod->sections[sec_idx].section_img_ofset;
}
unsigned int tpo_mode_do_import (tpo_mod_file *tpo_mod)
{
int sec_idx;
sec_idx=0;
while(tpo_mod->sections[sec_idx].section_size>0)
{
mem_ptr section_ptr;
mem_ptr section_relloc_addr;
int relloc_value;
int imp_idx;
tpo_import *imp_ptr;
size_t size;
mem_ptr end_zone;
if(tpo_mod->sections[sec_idx].imports_fnc.zone!=NULL)
{
imp_ptr = get_zone_ptr (&tpo_mod->sections[sec_idx].imports_fnc,0);
size = get_zone_size (&tpo_mod->sections[sec_idx].imports_fnc);
end_zone = mem_add(imp_ptr,size);
section_ptr = get_zone_ptr(&tpo_mod->data_sections,tpo_mod->sections[sec_idx].section_ptr);
imp_idx = 0;
while(imp_ptr->reloc_addr!=0xFFFFFFFF)
{
section_relloc_addr = mem_add(section_ptr,imp_ptr->reloc_addr);
relloc_value = *((int *)(section_relloc_addr));
if(relloc_value==-4)
{
int relative_addr;
int reloc_addr;
reloc_addr = mem_to_int(section_relloc_addr);
relative_addr = ((int)(imp_ptr->sym_addr))-(reloc_addr+4);
*((int *)(section_relloc_addr)) = relative_addr;
}
else
{
*((unsigned int *)(section_relloc_addr))=imp_ptr->sym_addr;
}
imp_ptr++;
}
}
sec_idx++;
}
return 1;
}
unsigned int tpo_mod_write_import (tpo_mod_file *tpo_mod,unsigned int sec_idx,mem_size ofset_addr,mem_ptr ptr)
{
tpo_import *imp_ptr;
size_t size;
tpo_import *end_zone;
if(tpo_mod->sections[sec_idx].section_ptr==0xFFFFFFFF)return 0;
imp_ptr = get_zone_ptr (&tpo_mod->sections[sec_idx].imports_fnc,0);
size = get_zone_size (&tpo_mod->sections[sec_idx].imports_fnc);
end_zone= mem_add (imp_ptr,size);
while(imp_ptr->reloc_addr!=0xFFFFFFFF)
{
imp_ptr++;
if(imp_ptr>=end_zone)return 0;
}
imp_ptr->reloc_addr = ofset_addr;
imp_ptr->sym_addr = mem_to_uint(ptr);
imp_ptr++;
return 1;
}
unsigned int tpo_mod_write_reloc(tpo_mod_file *tpo_mod,unsigned int sec_idx,mem_ptr baseAddr,unsigned int ofset_addr)
{
mem_ptr section_ptr;
mem_ptr section_relloc_addr_ptr;
mem_ptr section_relloc_img_ofset;
mem_ptr section_relloc_addr_abs;
mem_size section_relloc_addr_rel;
unsigned int *dest_addr_ptr;
unsigned int n;
if(tpo_mod->sections[sec_idx].section_size<=0)return 0;
section_ptr = get_zone_ptr(&tpo_mod->data_sections,tpo_mod->sections[sec_idx].section_ptr);
section_relloc_addr_ptr = mem_add(section_ptr,ofset_addr);
dest_addr_ptr = ((unsigned int *)(section_relloc_addr_ptr));
section_relloc_img_ofset = uint_to_mem(*dest_addr_ptr);
if((*dest_addr_ptr)==0xFFFFFFFC)
{
int relative_addr;
int reloc_addr;
n = 0;
while(n<16)
{
if(tpo_mod->sections[n].section_size>0)
{
mem_ptr sec_start_img;
mem_ptr sec_end_img;
sec_start_img = size_to_mem(tpo_mod->sections[n].section_img_ofset);
sec_end_img = mem_add(sec_start_img,tpo_mod->sections[n].section_size);
if((baseAddr>=sec_start_img)&&(baseAddr<sec_end_img))
{
mem_ptr target_sec_ptr;
target_sec_ptr = get_zone_ptr (&tpo_mod->data_sections,tpo_mod->sections[n].section_ptr);
section_relloc_addr_rel = mem_sub (sec_start_img ,baseAddr);
section_relloc_addr_abs = mem_add (target_sec_ptr ,section_relloc_addr_rel);
reloc_addr = mem_to_int(dest_addr_ptr);
relative_addr = mem_to_int(section_relloc_addr_abs)-(reloc_addr+4);
*((int *)(dest_addr_ptr)) = relative_addr;
return 1;
}
}
n++;
}
}
else
{
n = 0;
while(n<16)
{
if(tpo_mod->sections[n].section_size>0)
{
mem_ptr sec_start_img;
mem_ptr sec_end_img;
sec_start_img = size_to_mem(tpo_mod->sections[n].section_img_ofset);
sec_end_img = mem_add(sec_start_img,tpo_mod->sections[n].section_size);
if((section_relloc_img_ofset>=sec_start_img)&&(section_relloc_img_ofset<sec_end_img))
{
mem_ptr target_sec_ptr;
section_relloc_addr_rel = mem_sub (sec_start_img ,section_relloc_img_ofset);
target_sec_ptr = get_zone_ptr (&tpo_mod->data_sections,tpo_mod->sections[n].section_ptr);
section_relloc_addr_abs = mem_add (target_sec_ptr ,section_relloc_addr_rel);
*dest_addr_ptr = mem_to_uint(section_relloc_addr_abs);
return 1;
}
}
n++;
}
}
return 0;
}
unsigned int tpo_mod_add_export(tpo_mod_file *tpo_mod,unsigned int sec_idx,unsigned int crc_32,unsigned int string_idx,unsigned int ofsetAddr)
{
int n;
tpo_export *exports;
tpo_export *end_zone;
size_t size;
exports = get_zone_ptr (&tpo_mod->sections[sec_idx].exports_fnc,0);
size = get_zone_size (&tpo_mod->sections[sec_idx].exports_fnc);
end_zone= mem_add(exports,size);
n = 0;
while((exports->crc_32!=crc_32)&&(exports->crc_32!=0)){ exports++; if(exports>=end_zone)return 0; }
exports->crc_32 = crc_32;
exports->sym_addr = ofsetAddr;
exports->string_idx = string_idx;
return 1;
}
OS_API_C_FUNC(int) set_tpo_mod_exp_value32_name(const tpo_mod_file *tpo_mod,const char *name,unsigned int value)
{
tpo_export *exports;
tpo_export *end_zone;
mem_ptr sec_ptr,sym_ptr;
size_t size;
unsigned int n;
unsigned int crc_32;
crc_32 = calc_crc32_c(name,256);
n = 0;
while(tpo_mod->sections[n].section_size>0)
{
size = get_zone_size (&tpo_mod->sections[n].exports_fnc);
if(size>0)
{
exports = get_zone_ptr (&tpo_mod->sections[n].exports_fnc,0);
end_zone= mem_add (exports,size);
while(exports<end_zone)
{
if(exports->crc_32==0)break;
if(exports->crc_32==crc_32)
{
unsigned int *sym_ptr_ptr;
sec_ptr = get_zone_ptr(&tpo_mod->data_sections,tpo_mod->sections[n].section_ptr);
sym_ptr = mem_add (sec_ptr, exports->sym_addr);
/*
sym_ptr_addr = (unsigned int **)(sym_ptr);
sym_ptr_ptr = ((unsigned int *)((*sym_ptr_addr)));
(*sym_ptr_ptr) = value;
*/
sym_ptr_ptr = ((unsigned int *)(sym_ptr));
(*sym_ptr_ptr) = value;
return 1;
}
exports++;
}
}
n++;
}
return 0;
}
OS_API_C_FUNC(void_func_ptr) get_tpo_mod_exp_addr_name(const tpo_mod_file *tpo_mod, const char *name, unsigned int deco_type)
{
tpo_export *exports;
tpo_export *end_zone;
mem_ptr sec_ptr;
void_func_ptr sym_ptr;
size_t size;
unsigned int n;
unsigned int crc_32;
char func_name[256];
switch (tpo_mod->deco_type)
{
case MSVC_STDCALL_32:
strcpy_cs(func_name,256,"_");
strcat_cs(func_name,256,name);
break;
/*
case GCC_STDCALL_32:
strcpy_cs(func_name,256,"_");
strcat_cs(func_name,256,name);
break;
*/
default:
strcpy_cs(func_name,256,name);
break;
}
crc_32 = calc_crc32_c(func_name,256);
/*crc_32 = tpo_calc_imp_func_hash_name_c(name,0,tpo_mod->deco_type);*/
n = 0;
while(tpo_mod->sections[n].section_size>0)
{
size = get_zone_size (&tpo_mod->sections[n].exports_fnc);
if(size>=sizeof(tpo_export))
{
exports = get_zone_ptr (&tpo_mod->sections[n].exports_fnc,0);
end_zone= mem_add (exports,size-sizeof(tpo_export));
while(exports<end_zone)
{
if(exports->crc_32==0)break;
if(exports->crc_32==crc_32)
{
sec_ptr= get_zone_ptr(&tpo_mod->data_sections,tpo_mod->sections[n].section_ptr);
sym_ptr= (void_func_ptr)mem_add(sec_ptr, exports->sym_addr);
return sym_ptr;
}
exports++;
}
}
n++;
}
return PTR_NULL;
}
OS_API_C_FUNC(void) register_tpo_exports(tpo_mod_file *tpo_mod,const char *mod_name)
{
unsigned int crc_dll_name;
tpo_section *section;
tpo_export *end_zone;
size_t size;
if(mod_name!=PTR_NULL)
crc_dll_name = calc_crc32_c(mod_name,64);
else
crc_dll_name = calc_crc32_c(tpo_mod->name,64);
section=tpo_mod->sections;
while(section->section_size>0)
{
tpo_export *exports;
if(get_zone_size(§ion->exports_fnc)>0)
{
exports = get_zone_ptr (§ion->exports_fnc,0);
if(exports != uint_to_mem(0xFFFFFFFF))
{
size = get_zone_size (§ion->exports_fnc);
end_zone= mem_add(exports,size);
while((exports<end_zone)&&(exports->crc_32!=0))
{
if(tpo_get_fn_entry_name_c(tpo_mod->mod_idx,crc_dll_name,exports->string_idx,tpo_mod->deco_type)==uint_to_mem(0xFFFFFFFF))
{
mem_ptr sec_ptr,sym_ptr;
sec_ptr= get_zone_ptr(&tpo_mod->data_sections,section->section_ptr);
sym_ptr= mem_add (sec_ptr, exports->sym_addr);
tpo_mod_add_func_c (tpo_mod->mod_idx,mem_to_uint(sym_ptr),mem_to_uint(sec_ptr),exports->string_idx);
}
exports++;
}
}
}
section++;
}
}
OS_API_C_FUNC(int) tpo_mod_load_tpo(mem_stream *file_stream,tpo_mod_file *tpo_file,unsigned int imp_func_addr)
{
char mod_name[128];
unsigned int nsecs;
mem_ptr section_remaps[16];
unsigned int section_remaps_n[16];
unsigned int n;
size_t file_start;
size_t file_ofset;
file_start = file_stream->current_ptr;
mem_stream_read (file_stream,mod_name,128);
if ((!strncmp_c(mod_name, "libblock", 8)) || (!strncmp_c(mod_name, "libprotocol", 11)))
strcpy_cs(tpo_file->name, 64, &mod_name[3]);
else
strcpy_cs(tpo_file->name, 64, mod_name);
tpo_file->string_buffer_len = mem_stream_read_32(file_stream);
tpo_file->string_buffer_ref.zone = PTR_NULL;
/*
kernel_log (kernel_log_id,"loading tpo mod '");
writestr (mod_name);
writestr ("' ");
writeptr (get_zone_ptr(&file_stream->data,file_stream->current_ptr));
writestr (" ");
writeint (tpo_file->string_buffer_len,10);
writestr ("\n");
*/
allocate_new_zone (0,tpo_file->string_buffer_len ,&tpo_file->string_buffer_ref);
mem_stream_read (file_stream,get_zone_ptr(&tpo_file->string_buffer_ref,0),tpo_file->string_buffer_len);
debug=0;
/*
if(!strcmp_c(mod_name,"hid"))
debug=3;
if(!strcmp_c(mod_name,"bus_manager"))
debug=4;
*/
tpo_file->deco_type = mem_stream_read_32(file_stream);
tpo_file->name_hash = MOD_HASH(tpo_file->name);
tpo_file->mod_idx = tpo_add_mod_c(tpo_file->name_hash,tpo_file->deco_type,mem_to_uint(get_zone_ptr(&tpo_file->string_buffer_ref,0)));
/*
kernel_log (kernel_log_id,"new mod added '");
writestr (mod_name);
writestr ("' ");
writeint (tpo_file->mod_idx,10);
writestr (" ");
writeint (tpo_file->name_hash,16);
writestr (" ");
writeint (tpo_file->deco_type,10);
writestr ("\n");
*/
nsecs = mem_stream_read_32(file_stream);
n=0;
while(n<nsecs)
{
unsigned int sec_data_len;
unsigned int sec_imps_n;
unsigned int sec_exps_n;
unsigned int sec_imps_o;
unsigned int sec_exps_o;
unsigned int sec_flags;
unsigned int num_remap;
unsigned int sec_idx;
unsigned int crc_data;
unsigned int crc_ndis_sys,crc_hal_sys;
unsigned int crc_file;
unsigned int n_exps,n_imps;
size_t file_pos;
mem_ptr sec_data_ptr;
mem_stream_skip(file_stream,8);
mem_stream_skip(file_stream,4);
sec_flags = mem_stream_read_32(file_stream);
crc_file = mem_stream_read_32(file_stream);
mem_stream_skip(file_stream,4);
sec_data_len = mem_stream_read_32 (file_stream);
file_ofset = file_stream->current_ptr-file_start;
file_pos = file_ofset;
if((file_pos&0x0000000F)!=0)
file_stream->current_ptr+= (((file_pos&0xFFFFFFF0)+16)-file_pos);
sec_data_ptr = get_zone_ptr (&file_stream->data,file_stream->current_ptr+file_stream->buf_ofs);
file_ofset = file_stream->current_ptr-file_start;
sec_idx = tpo_mod_add_section (tpo_file,file_ofset,sec_data_ptr,sec_data_len,&crc_data);
/*
if(crc_data!=crc_file)
{
kernel_log (kernel_log_id,"crc do not match section data [");
writeint (sec_idx,10);
writestr ("]");
writestr ("section start : ");
writesz (file_ofset,16);
writestr ("section size : ");
writesz (sec_data_len,16);
writestr ("\n");
}
if(sec_idx==0xFFFFFFFF)
kernel_log (kernel_log_id,"could not load tpo section \n");
*/
mem_stream_skip (file_stream,sec_data_len);
crc_ndis_sys = calc_crc32_c("NDIS",64);
crc_hal_sys = calc_crc32_c("HAL",64);
sec_imps_n = mem_stream_read_32(file_stream);
n_imps = 0;
if(sec_imps_n>0)
{
int sz;
sz =(sec_imps_n+1)*sizeof(tpo_import);
if (allocate_new_zone(0, sz, &tpo_file->sections[sec_idx].imports_fnc) != 1)
return 0;
memset_c(get_zone_ptr(&tpo_file->sections[sec_idx].imports_fnc,0),0xFF,sz);
}
while(n_imps<sec_imps_n)
{
char dll_name[64];
char dll_imp_name[64];
char sym_name[256];
unsigned int fn_crc,dll_crc,ofs_addr,new_addr;
unsigned int imp_ofs,str_n;
struct kern_mod_fn_t *func_ptr;
memset_c(dll_name, 0, 64);
memset_c(dll_imp_name, 0, 64);
memset_c(sym_name, 0, 64);
if(sec_flags&0x00000001)
{
dll_crc = mem_stream_read_32(file_stream);
fn_crc = mem_stream_read_32(file_stream);
new_addr = tpo_mod_imp_func_addr_c(dll_crc,fn_crc);
}
else
{
int ofset;
ofset = mem_stream_read_32 (file_stream);
strcpy_cs(dll_name, 64, get_zone_ptr(&tpo_file->string_buffer_ref, ofset));
if (!strcmp_c(dll_name, "libcon_d"))
strcpy_cs(dll_name, 64, "libcon");
if (!strncmp_c(dll_name, "libbase", 7))
strcpy_cs(dll_name, 64, "libbase");
dll_crc = calc_crc32_c(dll_name,64);
ofset = mem_stream_read_32 (file_stream);
strcpy_cs (sym_name,256,get_zone_ptr(&tpo_file->string_buffer_ref,ofset));
/*fn_crc = calc_crc32_c(sym_name,256);*/
imp_ofs = 0;
func_ptr = uint_to_mem(0xFFFFFFFF);
while(func_ptr == uint_to_mem(0xFFFFFFFF))
{
str_n = 0;
while((dll_name[imp_ofs]!=';')&&(dll_name[imp_ofs]!=0))
{
dll_imp_name[str_n]=dll_name[imp_ofs];
str_n++;
imp_ofs++;
}
dll_imp_name[str_n] = 0;
if ((!strncmp_c(dll_imp_name, "libblock", 8)) || (!strncmp_c(dll_imp_name, "libprotocol", 11)))
strcpy_cs(dll_imp_name, 64, &dll_imp_name[3]);
else
strcpy_cs(dll_imp_name, 64, dll_imp_name);
dll_crc = calc_crc32_c(dll_imp_name,64);
func_ptr = tpo_get_fn_entry_name_c(tpo_file->mod_idx, dll_crc, ofset, tpo_file->deco_type);
if(dll_name[imp_ofs]==0)break;
imp_ofs++;
}
if (func_ptr == uint_to_mem(0xFFFFFFFF))
func_ptr = find_sym(tpo_file->mod_idx,ofset, tpo_file->deco_type);
}
ofs_addr = mem_stream_read_32(file_stream);
if(func_ptr != uint_to_mem(0xFFFFFFFF))
{
tpo_mod_write_import(tpo_file, sec_idx, ofs_addr, uint_to_mem(func_ptr->func_addr));
}
else
{
console_print("import symbol not found ");
console_print(sym_name);
console_print(" ");
console_print(dll_name);
console_print(" in ");
console_print(tpo_file->name);
console_print("\n");
}
n_imps++;
}
sec_imps_o = mem_stream_read_32(file_stream);
mem_stream_skip (file_stream,sec_imps_o*12);
sec_exps_n = mem_stream_read_32(file_stream);
if(sec_exps_n>0)
{
int sz;
sz =(sec_exps_n+1)*sizeof(tpo_export);
tpo_file->sections[sec_idx].exports_fnc.zone=PTR_NULL;
if (allocate_new_zone(0, sz, &tpo_file->sections[sec_idx].exports_fnc) != 1)
return 0;
memset_c(get_zone_ptr(&tpo_file->sections[sec_idx].exports_fnc,0),0,sz);
n_exps = 0;
while(n_exps<sec_exps_n)
{
char dll_name[64];
char sym_name[256];
unsigned int crc_dll,crc_func,sym_ofs;
int mod_str_ofs,fn_str_ofs;
if(sec_flags&0x00000001)
{
crc_dll =mem_stream_read_32(file_stream);
crc_func=mem_stream_read_32(file_stream);
}
else
{
mod_str_ofs = mem_stream_read_32 (file_stream);
strcpy_cs (dll_name,64,get_zone_ptr(&tpo_file->string_buffer_ref,mod_str_ofs));
fn_str_ofs = mem_stream_read_32 (file_stream);
strcpy_cs (sym_name,256,get_zone_ptr(&tpo_file->string_buffer_ref,fn_str_ofs));
crc_dll = calc_crc32_c(dll_name,64);
crc_func = tpo_calc_exp_func_hash_c(tpo_file->mod_idx,fn_str_ofs);
}
sym_ofs = mem_stream_read_32 (file_stream);
if(tpo_mod_add_export(tpo_file,sec_idx,crc_func,fn_str_ofs,sym_ofs)!=1)
{
console_print("could not add tpo export ");
console_print(sym_name);
console_print(" ");
console_print(dll_name);
console_print("\n");
}
n_exps++;
}
}
sec_exps_o = mem_stream_read_32(file_stream);
mem_stream_skip (file_stream,sec_exps_o*12);
num_remap = mem_stream_read_32(file_stream);
section_remaps[sec_idx] = get_zone_ptr(&file_stream->data,file_stream->current_ptr+file_stream->buf_ofs);
section_remaps_n[sec_idx] = num_remap;
mem_stream_skip (file_stream,num_remap*8);
n++;
}
tpo_mode_do_import (tpo_file);
n = 0;
while(n<nsecs)
{
unsigned int n_rmp;
unsigned int *remap_ptr;
n_rmp = 0;
remap_ptr = (unsigned int *)section_remaps[n];
while(n_rmp<section_remaps_n[n])
{
unsigned int offset;
mem_ptr baseAddr;
baseAddr=uint_to_mem(remap_ptr[n_rmp*2+0]);
offset =remap_ptr[n_rmp*2+1];
if(!tpo_mod_write_reloc (tpo_file,n,baseAddr,offset))
{
console_print("error in hard remap \n");
}
n_rmp++;
}
tpo_mod_add_section_c (tpo_file->mod_idx,mem_to_uint(get_zone_ptr(&tpo_file->data_sections,tpo_file->sections[n].section_ptr)),(unsigned int)tpo_file->sections[n].section_size);
n++;
}
set_mem_exe(&tpo_file->data_sections);
return 1;
}
OS_API_C_FUNC(void_func_ptr) tpo_mod_get_exp_addr(mem_stream *file_stream,const char *sym)
{
char mod_name[128];
unsigned int nsecs,deco_type;
unsigned int crc_sym;
unsigned int n;
size_t file_start;
unsigned int str_buffer_len;
char *str_buffer ;
file_stream->current_ptr=0;
file_start =file_stream->current_ptr;
mem_stream_read (file_stream,mod_name,128);
str_buffer_len = mem_stream_read_32(file_stream);
str_buffer = get_zone_ptr(&file_stream->data,file_stream->current_ptr+file_stream->buf_ofs);
mem_stream_skip (file_stream,str_buffer_len);
deco_type = mem_stream_read_32(file_stream);
nsecs = mem_stream_read_32(file_stream);
crc_sym = calc_crc32_c(sym,256);
/*
kernel_log (kernel_log_id,"loading tpo export ");
writestr (mod_name);
writestr (" ");
writeint (nsecs,16);
writestr ("\n");
*/
n=0;
while(n<nsecs)
{
unsigned int sec_data_len;
unsigned int sec_imps_n;
unsigned int sec_exps_n;
unsigned int sec_imps_o;
unsigned int sec_exps_o;
unsigned int sec_flags;
unsigned int num_remap;
unsigned int n_exps;
size_t file_pos;
size_t file_ofset;
mem_ptr sec_data_ptr;
mem_stream_skip(file_stream,8);
mem_stream_skip(file_stream,4);
sec_flags = mem_stream_read_32(file_stream);
mem_stream_skip(file_stream,4);
mem_stream_skip(file_stream,4);
sec_data_len = mem_stream_read_32 (file_stream);
file_ofset = file_stream->current_ptr-file_start;
file_pos = file_ofset;
if((file_pos&0x0000000F)!=0)
file_stream->current_ptr+= (((file_pos&0xFFFFFFF0)+16)-file_pos);
sec_data_ptr = get_zone_ptr (&file_stream->data,file_stream->current_ptr+file_stream->buf_ofs);
mem_stream_skip (file_stream,sec_data_len);
sec_imps_n = mem_stream_read_32(file_stream);
mem_stream_skip (file_stream,sec_imps_n*12);
sec_imps_o = mem_stream_read_32(file_stream);
mem_stream_skip (file_stream,sec_imps_o*12);
sec_exps_n = mem_stream_read_32(file_stream);
n_exps = 0;
while(n_exps<sec_exps_n)
{
unsigned int crc_dll,crc_func,sym_ofs;
void_func_ptr sym_addr;
if(sec_flags&0x00000001)
{
crc_dll =mem_stream_read_32(file_stream);
crc_func=mem_stream_read_32(file_stream);
}
else
{
char dll_name[64];
char sym_name[256];
int ofset;
ofset = mem_stream_read_32 (file_stream);
strcpy_cs (dll_name,64,&str_buffer[ofset]);
ofset = mem_stream_read_32 (file_stream);
strcpy_cs (sym_name,256,&str_buffer[ofset]);
/*mem_stream_read (file_stream,dll_name,64);*/
/*mem_stream_read (file_stream,sym_name,256);*/
crc_dll = calc_crc32_c(dll_name,64);
crc_func = calc_crc32_c(sym_name,256);
/*
kernel_log(kernel_log_id,"func exp ");
writestr(sym_name);
writestr(" ");
writestr(sym);
writestr("\n");
*/
}
sym_ofs = mem_stream_read_32 (file_stream);
sym_addr = (void_func_ptr)mem_add(sec_data_ptr, sym_ofs);
if(crc_func==crc_sym)
{
/*
writestr ("export symbole found : ");
writeptr (sec_data_ptr);
writestr (" ");
writeint (sym_ofs,16);
writestr (" ");
writeptr (sym_addr);
writestr ("\n");
*/