-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformats.inc
4195 lines (4187 loc) · 84 KB
/
formats.inc
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
; flat assembler core
; Copyright (c) 1999-2022, Tomasz Grysztar.
; All rights reserved.
formatter:
mov [current_offset],edi
cmp [output_file],0
jne output_path_ok
mov esi,[input_file]
mov edi,[free_additional_memory]
duplicate_output_path:
lods byte [esi]
cmp edi,[structures_buffer]
jae out_of_memory
stos byte [edi]
or al,al
jnz duplicate_output_path
dec edi
mov eax,edi
find_extension:
dec eax
cmp eax,[free_additional_memory]
jb extension_found
cmp byte [eax],'\'
je extension_found
cmp byte [eax],'/'
je extension_found
cmp byte [eax],'.'
jne find_extension
mov edi,eax
extension_found:
lea eax,[edi+9]
cmp eax,[structures_buffer]
jae out_of_memory
cmp [file_extension],0
jne extension_specified
mov al,[output_format]
cmp al,2
je exe_extension
jb bin_extension
cmp al,4
je obj_extension
cmp al,5
je o_extension
cmp al,3
jne no_extension
cmp [subsystem],1
je sys_extension
cmp [subsystem],10
jae efi_extension
bt [format_flags],8
jnc exe_extension
mov eax,'.dll'
jmp make_extension
sys_extension:
mov eax,'.sys'
jmp make_extension
efi_extension:
mov eax,'.efi'
jmp make_extension
bin_extension:
mov eax,'.bin'
bt [format_flags],0
jnc make_extension
mov eax,'.com'
jmp make_extension
obj_extension:
mov eax,'.obj'
jmp make_extension
o_extension:
mov eax,'.o'
bt [format_flags],0
jnc make_extension
no_extension:
xor eax,eax
jmp make_extension
exe_extension:
mov eax,'.exe'
make_extension:
xchg eax,[edi]
scas dword [edi]
mov byte [edi],0
scas byte [edi]
mov esi,edi
stos dword [edi]
sub edi,9
xor eax,eax
mov ebx,characters
adapt_case:
mov al,[esi]
or al,al
jz adapt_next
xlat byte [ebx]
cmp al,[esi]
je adapt_ok
sub byte [edi],20h
adapt_ok:
inc esi
adapt_next:
inc edi
cmp byte [edi],0
jne adapt_case
jmp extension_ok
extension_specified:
mov al,'.'
stos byte [edi]
mov esi,[file_extension]
copy_extension:
lods byte [esi]
stos byte [edi]
test al,al
jnz copy_extension
dec edi
extension_ok:
mov esi,edi
lea ecx,[esi+1]
sub ecx,[free_additional_memory]
mov edi,[structures_buffer]
dec edi
std
rep movs byte [edi],[esi]
cld
inc edi
mov [structures_buffer],edi
mov [output_file],edi
output_path_ok:
cmp [symbols_file],0
je labels_table_ok
mov ecx,[memory_end]
sub ecx,[labels_list]
mov edi,[tagged_blocks]
sub edi,8
mov [edi],ecx
or dword [edi+4],-1
sub edi,ecx
cmp edi,[current_offset]
jbe out_of_memory
mov [tagged_blocks],edi
mov esi,[memory_end]
copy_labels:
sub esi,32
cmp esi,[labels_list]
jb labels_table_ok
mov ecx,32 shr 2
rep movs dword [edi],[esi]
sub esi,32
jmp copy_labels
labels_table_ok:
mov edi,[current_offset]
cmp [output_format],4
je coff_formatter
cmp [output_format],5
jne common_formatter
bt [format_flags],0
jnc elf_formatter
common_formatter:
mov eax,edi
sub eax,[code_start]
mov [real_code_size],eax
cmp edi,[undefined_data_end]
jne calculate_code_size
mov edi,[undefined_data_start]
calculate_code_size:
mov [current_offset],edi
sub edi,[code_start]
mov [code_size],edi
and [written_size],0
mov edx,[output_file]
call create
jc write_failed
cmp [output_format],3
jne stub_written
mov edx,[code_start]
mov ecx,[stub_size]
sub edx,ecx
add [written_size],ecx
call write
stub_written:
cmp [output_format],2
jne write_output
call write_mz_header
write_output:
call write_code
output_written:
call close
cmp [symbols_file],0
jne dump_symbols
ret
write_code:
mov eax,[written_size]
mov [headers_size],eax
mov edx,[code_start]
mov ecx,[code_size]
add [written_size],ecx
lea eax,[edx+ecx]
call write
jc write_failed
ret
format_directive:
cmp edi,[code_start]
jne unexpected_instruction
mov ebp,[addressing_space]
test byte [ds:ebp+0Ah],1
jnz unexpected_instruction
cmp [output_format],0
jne unexpected_instruction
lods byte [esi]
cmp al,1Ch
je format_prefix
cmp al,18h
jne invalid_argument
lods byte [esi]
select_format:
mov dl,al
shr al,4
mov [output_format],al
and edx,0Fh
or [format_flags],edx
cmp al,2
je format_mz
cmp al,3
je format_pe
cmp al,4
je format_coff
cmp al,5
je format_elf
format_defined:
cmp byte [esi],86h
jne instruction_assembled
cmp word [esi+1],'('
jne invalid_argument
mov eax,[esi+3]
add esi,3+4
mov [file_extension],esi
lea esi,[esi+eax+1]
jmp instruction_assembled
format_prefix:
lods byte [esi]
mov ah,al
lods byte [esi]
cmp al,18h
jne invalid_argument
lods byte [esi]
mov edx,eax
shr dl,4
shr dh,4
cmp dl,dh
jne invalid_argument
or al,ah
jmp select_format
entry_directive:
bts [format_flags],10h
jc setting_already_specified
mov al,[output_format]
cmp al,2
je mz_entry
cmp al,3
je pe_entry
cmp al,5
jne illegal_instruction
bt [format_flags],0
jc elf_entry
jmp illegal_instruction
stack_directive:
bts [format_flags],11h
jc setting_already_specified
mov al,[output_format]
cmp al,2
je mz_stack
cmp al,3
je pe_stack
jmp illegal_instruction
heap_directive:
bts [format_flags],12h
jc setting_already_specified
mov al,[output_format]
cmp al,2
je mz_heap
cmp al,3
je pe_heap
jmp illegal_instruction
segment_directive:
mov al,[output_format]
cmp al,2
je mz_segment
cmp al,5
je elf_segment
jmp illegal_instruction
section_directive:
mov al,[output_format]
cmp al,3
je pe_section
cmp al,4
je coff_section
cmp al,5
je elf_section
jmp illegal_instruction
public_directive:
mov al,[output_format]
cmp al,4
je public_allowed
cmp al,5
jne illegal_instruction
bt [format_flags],0
jc illegal_instruction
public_allowed:
mov [base_code],0C0h
lods byte [esi]
cmp al,2
je public_label
cmp al,1Dh
jne invalid_argument
lods byte [esi]
and al,7
add [base_code],al
lods byte [esi]
cmp al,2
jne invalid_argument
public_label:
lods dword [esi]
cmp eax,0Fh
jb invalid_use_of_symbol
je reserved_word_used_as_symbol
inc esi
mov dx,[current_pass]
mov [eax+18],dx
or byte [eax+8],8
mov ebx,eax
call store_label_reference
mov eax,ebx
mov ebx,[free_additional_memory]
lea edx,[ebx+10h]
cmp edx,[structures_buffer]
jae out_of_memory
mov [free_additional_memory],edx
mov [ebx+8],eax
mov eax,[current_line]
mov [ebx+0Ch],eax
lods byte [esi]
cmp al,86h
jne invalid_argument
lods word [esi]
cmp ax,'('
jne invalid_argument
mov [ebx+4],esi
lods dword [esi]
lea esi,[esi+eax+1]
mov al,[base_code]
mov [ebx],al
jmp instruction_assembled
extrn_directive:
mov al,[output_format]
cmp al,4
je extrn_allowed
cmp al,5
jne illegal_instruction
bt [format_flags],0
jc illegal_instruction
extrn_allowed:
lods word [esi]
cmp ax,'('
jne invalid_argument
mov ebx,esi
lods dword [esi]
lea esi,[esi+eax+1]
mov edx,[free_additional_memory]
lea eax,[edx+0Ch]
cmp eax,[structures_buffer]
jae out_of_memory
mov [free_additional_memory],eax
mov byte [edx],80h
mov [edx+4],ebx
lods byte [esi]
cmp al,86h
jne invalid_argument
lods byte [esi]
cmp al,2
jne invalid_argument
lods dword [esi]
cmp eax,0Fh
jb invalid_use_of_symbol
je reserved_word_used_as_symbol
inc esi
mov ebx,eax
xor ah,ah
lods byte [esi]
cmp al,':'
je get_extrn_size
dec esi
cmp al,11h
jne extrn_size_ok
get_extrn_size:
lods word [esi]
cmp al,11h
jne invalid_argument
extrn_size_ok:
mov [address_symbol],edx
mov [label_size],ah
movzx ecx,ah
mov [edx+8],ecx
xor eax,eax
xor edx,edx
xor ebp,ebp
mov [address_sign],0
mov ch,2
test [format_flags],8
jz make_free_label
mov ch,4
jmp make_free_label
mark_relocation:
cmp [value_type],0
je relocation_ok
mov ebp,[addressing_space]
test byte [ds:ebp+0Ah],1
jnz relocation_ok
cmp [output_format],2
je mark_mz_relocation
cmp [output_format],3
je mark_pe_relocation
cmp [output_format],4
je mark_coff_relocation
cmp [output_format],5
je mark_elf_relocation
relocation_ok:
ret
close_pass:
mov al,[output_format]
cmp al,3
je close_pe
cmp al,4
je close_coff
cmp al,5
je close_elf
ret
format_mz:
mov edx,[additional_memory]
push edi
mov edi,edx
mov ecx,1Ch shr 2
xor eax,eax
rep stos dword [edi]
mov [free_additional_memory],edi
pop edi
mov word [edx+0Ch],0FFFFh
mov word [edx+10h],1000h
mov [code_type],16
jmp format_defined
mark_mz_relocation:
push eax ebx
inc word [number_of_relocations]
jz format_limitations_exceeded
mov ebx,[free_additional_memory]
mov eax,edi
sub eax,[code_start]
mov [ebx],ax
shr eax,16
shl ax,12
mov [ebx+2],ax
cmp word [ebx],0FFFFh
jne mz_relocation_ok
inc word [ebx+2]
sub word [ebx],10h
mz_relocation_ok:
add ebx,4
cmp ebx,[structures_buffer]
jae out_of_memory
mov [free_additional_memory],ebx
pop ebx eax
ret
mz_segment:
lods byte [esi]
cmp al,2
jne invalid_argument
lods dword [esi]
cmp eax,0Fh
jb invalid_use_of_symbol
je reserved_word_used_as_symbol
inc esi
mov ebx,eax
mov eax,edi
sub eax,[code_start]
mov ecx,0Fh
add eax,0Fh
and eax,1111b
sub ecx,eax
mov edx,edi
xor eax,eax
rep stos byte [edi]
mov eax,edx
call undefined_data
push ebx
call create_addressing_space
pop ebx
mov eax,edi
sub eax,[code_start]
shr eax,4
cmp eax,10000h
jae value_out_of_range
mov edx,eax
mov al,16
cmp byte [esi],13h
jne segment_type_ok
inc esi
lods byte [esi]
segment_type_ok:
mov [code_type],al
mov eax,edx
mov ch,1
mov [address_sign],0
xor edx,edx
xor ebp,ebp
mov [label_size],0
mov [address_symbol],edx
jmp make_free_label
mz_entry:
lods byte [esi]
cmp al,'('
jne invalid_argument
call get_word_value
cmp [value_type],1
je initial_cs_ok
call recoverable_invalid_address
initial_cs_ok:
mov edx,[additional_memory]
mov [edx+16h],ax
lods byte [esi]
cmp al,':'
jne invalid_argument
lods byte [esi]
cmp al,'('
jne invalid_argument
ja invalid_address
call get_word_value
cmp [value_type],0
jne invalid_use_of_symbol
mov edx,[additional_memory]
mov [edx+14h],ax
jmp instruction_assembled
recoverable_invalid_address:
cmp [error_line],0
jne ignore_invalid_address
push [current_line]
pop [error_line]
mov [error],invalid_address
ignore_invalid_address:
ret
mz_stack:
lods byte [esi]
cmp al,'('
jne invalid_argument
call get_word_value
cmp byte [esi],':'
je stack_pointer
cmp ax,10h
jb invalid_value
cmp [value_type],0
jne invalid_use_of_symbol
mov edx,[additional_memory]
mov [edx+10h],ax
jmp instruction_assembled
stack_pointer:
cmp [value_type],1
je initial_ss_ok
call recoverable_invalid_address
initial_ss_ok:
mov edx,[additional_memory]
mov [edx+0Eh],ax
lods byte [esi]
cmp al,':'
jne invalid_argument
lods byte [esi]
cmp al,'('
jne invalid_argument
call get_word_value
cmp [value_type],0
jne invalid_use_of_symbol
mov edx,[additional_memory]
mov [edx+10h],ax
bts [format_flags],4
jmp instruction_assembled
mz_heap:
cmp [output_format],2
jne illegal_instruction
lods byte [esi]
call get_size_operator
cmp ah,1
je invalid_value
cmp ah,2
ja invalid_value
cmp al,'('
jne invalid_argument
call get_word_value
cmp [value_type],0
jne invalid_use_of_symbol
mov edx,[additional_memory]
mov [edx+0Ch],ax
jmp instruction_assembled
write_mz_header:
mov edx,[additional_memory]
bt [format_flags],4
jc mz_stack_ok
mov eax,[real_code_size]
dec eax
shr eax,4
inc eax
mov [edx+0Eh],ax
shl eax,4
movzx ecx,word [edx+10h]
add eax,ecx
mov [real_code_size],eax
mz_stack_ok:
mov edi,[free_additional_memory]
mov eax,[number_of_relocations]
shl eax,2
add eax,1Ch
sub edi,eax
xchg edi,[free_additional_memory]
mov ecx,0Fh
add eax,0Fh
and eax,1111b
sub ecx,eax
xor al,al
rep stos byte [edi]
sub edi,[free_additional_memory]
mov ecx,edi
shr edi,4
mov word [edx],'MZ' ; signature
mov [edx+8],di ; header size in paragraphs
mov eax,[number_of_relocations]
mov [edx+6],ax ; number of relocation entries
mov eax,[code_size]
add eax,ecx
mov esi,eax
shr esi,9
and eax,1FFh
inc si
or ax,ax
jnz mz_size_ok
dec si
mz_size_ok:
mov [edx+2],ax ; number of bytes in last page
mov [edx+4],si ; number of pages
mov eax,[real_code_size]
dec eax
shr eax,4
inc eax
mov esi,[code_size]
dec esi
shr esi,4
inc esi
sub eax,esi
mov [edx+0Ah],ax ; minimum memory in addition to code
add [edx+0Ch],ax ; maximum memory in addition to code
salc
mov ah,al
or [edx+0Ch],ax
mov word [edx+18h],1Ch ; offset of relocation table
add [written_size],ecx
call write
jc write_failed
ret
make_stub:
mov [stub_file],edx
or edx,edx
jnz stub_from_file
push esi
mov edx,edi
xor eax,eax
mov ecx,20h
rep stos dword [edi]
mov eax,40h+default_stub_end-default_stub
mov cx,100h+default_stub_end-default_stub
mov word [edx],'MZ'
mov byte [edx+4],1
mov word [edx+2],ax
mov byte [edx+8],4
mov byte [edx+0Ah],10h
mov word [edx+0Ch],0FFFFh
mov word [edx+10h],cx
mov word [edx+3Ch],ax
mov byte [edx+18h],40h
lea edi,[edx+40h]
mov esi,default_stub
mov ecx,default_stub_end-default_stub
rep movs byte [edi],[esi]
pop esi
jmp stub_ok
default_stub:
use16
push cs
pop ds
mov dx,stub_message-default_stub
mov ah,9
int 21h
mov ax,4C01h
int 21h
stub_message db 'This program cannot be run in DOS mode.',0Dh,0Ah,24h
rq 1
default_stub_end:
use32
stub_from_file:
push esi
mov esi,edx
call open_binary_file
mov edx,edi
mov ecx,1Ch
mov esi,edx
call read
jc binary_stub
cmp word [esi],'MZ'
jne binary_stub
add edi,1Ch
movzx ecx,word [esi+6]
add ecx,11b
and ecx,not 11b
add ecx,(40h-1Ch) shr 2
lea eax,[edi+ecx*4]
cmp edi,[tagged_blocks]
jae out_of_memory
xor eax,eax
rep stos dword [edi]
mov edx,40h
xchg dx,[esi+18h]
xor al,al
call lseek
movzx ecx,word [esi+6]
shl ecx,2
lea edx,[esi+40h]
call read
mov edx,edi
sub edx,esi
shr edx,4
xchg dx,[esi+8]
shl edx,4
xor al,al
call lseek
movzx ecx,word [esi+4]
dec ecx
js out_of_memory
shl ecx,9
movzx edx,word [esi+2]
test edx,edx
jnz stub_header_size_ok
mov dx,200h
stub_header_size_ok:
add ecx,edx
mov edx,edi
sub ecx,eax
je read_stub_code
jb stub_code_ok
push ecx
dec ecx
shr ecx,3
inc ecx
shl ecx,1
lea eax,[edi+ecx*4]
cmp eax,[tagged_blocks]
jae out_of_memory
xor eax,eax
rep stos dword [edi]
pop ecx
read_stub_code:
call read
stub_code_ok:
call close
mov edx,edi
sub edx,esi
mov ax,dx
and ax,1FFh
mov [esi+2],ax
dec edx
shr edx,9
inc edx
mov [esi+4],dx
mov eax,edi
sub eax,esi
mov [esi+3Ch],eax
pop esi
stub_ok:
ret
binary_stub:
mov esi,edi
mov ecx,40h shr 2
xor eax,eax
rep stos dword [edi]
mov al,2
xor edx,edx
call lseek
push eax
xor al,al
xor edx,edx
call lseek
mov ecx,[esp]
add ecx,40h+111b
and ecx,not 111b
mov ax,cx
and ax,1FFh
mov [esi+2],ax
lea eax,[ecx+1FFh]
shr eax,9
mov [esi+4],ax
mov [esi+3Ch],ecx
sub ecx,40h
mov eax,10000h
sub eax,ecx
jbe binary_heap_ok
shr eax,4
mov [esi+0Ah],ax
binary_heap_ok:
mov word [esi],'MZ'
mov byte [esi+8],4
mov ax,0FFFFh
mov [esi+0Ch],ax
dec ax
mov [esi+10h],ax
sub ax,0Eh
mov [esi+0Eh],ax
mov [esi+16h],ax
mov word [esi+14h],100h
mov byte [esi+18h],40h
mov eax,[tagged_blocks]
sub eax,ecx
cmp edi,eax
jae out_of_memory
mov edx,edi
shr ecx,2
xor eax,eax
rep stos dword [edi]
pop ecx
call read
call close
pop esi
ret
format_pe:
xor edx,edx
mov [machine],14Ch
mov [subsystem],3
mov [subsystem_version],3 + 10 shl 16
mov [image_base],400000h
and [image_base_high],0
test [format_flags],8
jz pe_settings
mov [machine],8664h
mov [subsystem_version],5 + 0 shl 16
pe_settings:
cmp byte [esi],84h
je get_stub_name
cmp byte [esi],80h
je get_pe_base
cmp byte [esi],1Bh
jne pe_settings_ok
lods byte [esi]
lods byte [esi]
test al,80h+40h
jz subsystem_setting
cmp al,80h
je dll_flag
cmp al,81h
je wdm_flag
cmp al,82h
je large_flag
cmp al,83h
je nx_flag
jmp pe_settings
dll_flag:
bts [format_flags],8
jc setting_already_specified
jmp pe_settings
wdm_flag:
bts [format_flags],9
jc setting_already_specified
jmp pe_settings
large_flag:
bts [format_flags],11
jc setting_already_specified
test [format_flags],8
jnz invalid_argument
jmp pe_settings
nx_flag:
bts [format_flags],12
jc setting_already_specified
jmp pe_settings
subsystem_setting:
bts [format_flags],7
jc setting_already_specified
and ax,3Fh
mov [subsystem],ax
cmp ax,10
jb subsystem_type_ok
or [format_flags],4
subsystem_type_ok:
cmp byte [esi],'('
jne pe_settings
inc esi
cmp byte [esi],'.'
jne invalid_value
inc esi
push edx
cmp byte [esi+11],0
jne invalid_value
cmp byte [esi+10],2
ja invalid_value
mov dx,[esi+8]
cmp dx,8000h
je zero_version
mov eax,[esi+4]
cmp dx,7
jg invalid_value
mov cx,7
sub cx,dx
mov eax,[esi+4]
shr eax,cl
mov ebx,eax
shr ebx,24
cmp bl,100
jae invalid_value
and eax,0FFFFFFh
mov ecx,100
mul ecx
shrd eax,edx,24
jnc version_value_ok
inc eax
version_value_ok:
shl eax,16
mov ax,bx
jmp subsystem_version_ok
zero_version:
xor eax,eax
subsystem_version_ok:
pop edx
add esi,13
mov [subsystem_version],eax
jmp pe_settings
get_pe_base:
bts [format_flags],10
jc setting_already_specified
lods word [esi]
cmp ah,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
push edx edi
add edi,[stub_size]
test [format_flags],4
jnz get_peplus_base
call get_dword_value
mov [image_base],eax
jmp pe_base_ok
get_peplus_base:
call get_qword_value
mov [image_base],eax
mov [image_base_high],edx
pe_base_ok:
pop edi edx
cmp [value_type],0
jne invalid_use_of_symbol
cmp byte [esi],84h
jne pe_settings_ok
get_stub_name:
lods byte [esi]
lods word [esi]
cmp ax,'('
jne invalid_argument
lods dword [esi]
mov edx,esi
add esi,eax
inc esi
pe_settings_ok:
mov ebp,[stub_size]
or ebp,ebp
jz make_pe_stub
cmp edx,[stub_file]
je pe_stub_ok
sub edi,[stub_size]
mov [code_start],edi
make_pe_stub:
call make_stub
mov eax,edi
sub eax,[code_start]
mov [stub_size],eax
mov [code_start],edi
mov ebp,eax
pe_stub_ok:
mov edx,edi
mov ecx,18h+0E0h
test [format_flags],4
jz zero_pe_header
add ecx,10h
zero_pe_header:
add ebp,ecx
shr ecx,2
xor eax,eax
rep stos dword [edi]
mov word [edx],'PE' ; signature
mov ax,[machine]
mov word [edx+4],ax