Subversion Repositories Kolibri OS

Rev

Rev 1053 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
205 heavyiron 1
 
31 halyavin 2
; Copyright (c) 1999-2009, Tomasz Grysztar.
1039 heavyiron 3
; All rights reserved.
31 halyavin 4
5
 
6
	mov	[current_offset],edi
992 heavyiron 7
	cmp	[output_file],0
157 heavyiron 8
	jne	output_path_ok
9
	mov	esi,[input_file]
10
	mov	edi,[free_additional_memory]
11
      copy_output_path:
31 halyavin 12
	lods	byte [esi]
157 heavyiron 13
	cmp	edi,[structures_buffer]
14
	jae	out_of_memory
15
	stos	byte [edi]
16
	or	al,al
17
	jnz	copy_output_path
18
	dec	edi
19
	mov	eax,edi
20
      find_extension:
31 halyavin 21
	dec	eax
157 heavyiron 22
	cmp	eax,[free_additional_memory]
23
	jb	extension_found
24
	cmp	byte [eax],'\'
25
	je	extension_found
26
	cmp	byte [eax],'/'
27
	je	extension_found
28
	cmp	byte [eax],'.'
29
	jne	find_extension
30
	mov	edi,eax
31
      extension_found:
31 halyavin 32
	lea	eax,[edi+9]
157 heavyiron 33
	cmp	eax,[structures_buffer]
34
	jae	out_of_memory
35
	cmp	[file_extension],0
607 heavyiron 36
	jne	extension_specified
37
	cmp	[output_format],2
157 heavyiron 38
	je	exe_extension
39
	jb	bin_extension
40
	cmp	[output_format],4
41
	je	obj_extension
42
	cmp	[output_format],5
43
	je	o_extension
44
	cmp	[output_format],3
45
	jne	no_extension
46
	cmp	[subsystem],1
47
	je	sys_extension
48
	cmp	[subsystem],10
1054 heavyiron 49
	jae	efi_extension
50
	bt	[format_flags],8
157 heavyiron 51
	jnc	exe_extension
52
	mov	eax,'.dll'
53
	jmp	make_extension
54
      sys_extension:
31 halyavin 55
	mov	eax,'.sys'
157 heavyiron 56
	jmp	make_extension
57
      efi_extension:
1054 heavyiron 58
	mov	eax,'.efi'
59
	jmp	make_extension
60
      bin_extension:
31 halyavin 61
	mov	eax,'.bin'
157 heavyiron 62
	bt	[format_flags],0
63
	jnc	make_extension
64
	mov	eax,'.com'
65
	jmp	make_extension
66
      obj_extension:
31 halyavin 67
	mov	eax,'.obj'
157 heavyiron 68
	jmp	make_extension
69
      o_extension:
31 halyavin 70
	mov	eax,'.o'
157 heavyiron 71
	bt	[format_flags],0
72
	jnc	make_extension
73
      no_extension:
31 halyavin 74
	xor	eax,eax
157 heavyiron 75
	jmp	make_extension
76
      exe_extension:
31 halyavin 77
	mov	eax,'.exe'
157 heavyiron 78
      make_extension:
31 halyavin 79
	xchg	eax,[edi]
157 heavyiron 80
	scas	dword [edi]
81
	mov	byte [edi],0
82
	scas	byte [edi]
83
	mov	esi,edi
84
	stos	dword [edi]
85
	sub	edi,9
86
	xor	eax,eax
87
	mov	ebx,characters
88
      adapt_case:
31 halyavin 89
	mov	al,[esi]
157 heavyiron 90
	or	al,al
91
	jz	adapt_next
92
	xlat	byte [ebx]
93
	cmp	al,[esi]
94
	je	adapt_ok
95
	sub	byte [edi],20h
96
      adapt_ok:
31 halyavin 97
	inc	esi
157 heavyiron 98
      adapt_next:
31 halyavin 99
	inc	edi
157 heavyiron 100
	cmp	byte [edi],0
101
	jne	adapt_case
102
	jmp	extension_ok
607 heavyiron 103
      extension_specified:
104
	mov	al,'.'
105
	stos	byte [edi]
106
	mov	esi,[file_extension]
107
      copy_extension:
108
	lods	byte [esi]
109
	stos	byte [edi]
110
	test	al,al
111
	jnz	copy_extension
112
	dec	edi
113
      extension_ok:
114
	mov	esi,edi
157 heavyiron 115
	lea	ecx,[esi+1]
116
	sub	ecx,[free_additional_memory]
117
	mov	edi,[structures_buffer]
118
	dec	edi
119
	std
120
	rep	movs byte [edi],[esi]
121
	cld
122
	inc	edi
123
	mov	[structures_buffer],edi
124
	mov	[output_file],edi
125
      output_path_ok:
31 halyavin 126
	cmp	[symbols_file],0
992 heavyiron 127
	je	labels_table_ok
128
	mov	ecx,[memory_end]
129
	sub	ecx,[labels_list]
130
	mov	edi,[display_buffer]
131
	sub	edi,8
132
	mov	[edi],ecx
133
	or	dword [edi+4],-1
134
	sub	edi,ecx
135
	cmp	edi,[esp]
136
	jbe	out_of_memory
137
	mov	[display_buffer],edi
138
	mov	esi,[memory_end]
139
      copy_labels:
140
	sub	esi,32
141
	cmp	esi,[labels_list]
142
	jb	labels_table_ok
143
	mov	ecx,32 shr 2
144
	rep	movs dword [edi],[esi]
145
	sub	esi,32
146
	jmp	copy_labels
147
      labels_table_ok:
148
	mov	edi,[current_offset]
149
	cmp	[output_format],4
157 heavyiron 150
	je	coff_formatter
151
	cmp	[output_format],5
152
	jne	common_formatter
153
	bt	[format_flags],0
154
	jnc	elf_formatter
155
      common_formatter:
31 halyavin 156
	mov	eax,edi
157 heavyiron 157
	sub	eax,[code_start]
158
	mov	[real_code_size],eax
159
	cmp	edi,[undefined_data_end]
160
	jne	calculate_code_size
161
	mov	edi,[undefined_data_start]
162
      calculate_code_size:
31 halyavin 163
	mov	[current_offset],edi
992 heavyiron 164
	sub	edi,[code_start]
157 heavyiron 165
	mov	[code_size],edi
166
	mov	[written_size],0
167
	mov	edx,[output_file]
168
	call	create
169
	jc	write_failed
170
	cmp	[output_format],3
171
	jne	stub_written
172
	mov	edx,[code_start]
173
	mov	ecx,[stub_size]
174
	sub	edx,ecx
175
	add	[written_size],ecx
176
	call	write
177
      stub_written:
31 halyavin 178
	cmp	[output_format],2
157 heavyiron 179
	jne	write_output
180
	call	write_mz_header
181
      write_output:
31 halyavin 182
	call	write_code
157 heavyiron 183
      output_written:
31 halyavin 184
	call	close
157 heavyiron 185
	cmp	[symbols_file],0
992 heavyiron 186
	jne	dump_symbols
187
	ret
157 heavyiron 188
      write_code:
31 halyavin 189
	mov	eax,[written_size]
157 heavyiron 190
	mov	[headers_size],eax
191
	mov	edx,[code_start]
192
	mov	ecx,[code_size]
193
	add	[written_size],ecx
194
	lea	eax,[edx+ecx]
992 heavyiron 195
	call	write
157 heavyiron 196
	jc	write_failed
197
	ret
198
format_directive:
31 halyavin 199
	cmp	edi,[code_start]
157 heavyiron 200
	jne	unexpected_instruction
201
	cmp	[virtual_data],0
202
	jne	unexpected_instruction
203
	cmp	[output_format],0
204
	jne	unexpected_instruction
205
	lods	byte [esi]
206
	cmp	al,17h
207
	je	format_prefix
208
	cmp	al,18h
209
	jne	invalid_argument
210
	lods	byte [esi]
211
      select_format:
31 halyavin 212
	mov	dl,al
157 heavyiron 213
	shr	al,4
214
	mov	[output_format],al
215
	and	edx,0Fh
216
	or	[format_flags],edx
217
	cmp	al,2
218
	je	format_mz
219
	cmp	al,3
220
	je	format_pe
221
	cmp	al,4
222
	je	format_coff
223
	cmp	al,5
224
	je	format_elf
225
      format_defined:
607 heavyiron 226
	cmp	byte [esi],86h
227
	jne	instruction_assembled
228
	cmp	word [esi+1],'('
229
	jne	invalid_argument
230
	mov	eax,[esi+3]
231
	add	esi,3+4
232
	mov	[file_extension],esi
233
	lea	esi,[esi+eax+1]
234
	jmp	instruction_assembled
157 heavyiron 235
      format_prefix:
31 halyavin 236
	lods	byte [esi]
157 heavyiron 237
	mov	ah,al
238
	lods	byte [esi]
239
	cmp	al,18h
240
	jne	invalid_argument
241
	lods	byte [esi]
242
	mov	edx,eax
243
	shr	dl,4
244
	shr	dh,4
245
	cmp	dl,dh
246
	jne	invalid_argument
247
	or	al,ah
248
	jmp	select_format
249
entry_directive:
31 halyavin 250
	bts	[format_flags],10h
157 heavyiron 251
	jc	setting_already_specified
252
	mov	al,[output_format]
253
	cmp	al,2
254
	je	mz_entry
255
	cmp	al,3
256
	je	pe_entry
257
	cmp	al,5
258
	jne	illegal_instruction
259
	bt	[format_flags],0
260
	jc	elf_entry
261
	jmp	illegal_instruction
262
stack_directive:
31 halyavin 263
	bts	[format_flags],11h
157 heavyiron 264
	jc	setting_already_specified
265
	mov	al,[output_format]
266
	cmp	al,2
267
	je	mz_stack
268
	cmp	al,3
269
	je	pe_stack
270
	jmp	illegal_instruction
271
heap_directive:
31 halyavin 272
	bts	[format_flags],12h
157 heavyiron 273
	jc	setting_already_specified
274
	mov	al,[output_format]
275
	cmp	al,2
276
	je	mz_heap
277
	cmp	al,3
278
	je	pe_heap
279
	jmp	illegal_instruction
280
segment_directive:
109 heavyiron 281
	cmp	[virtual_data],0
157 heavyiron 282
	jne	illegal_instruction
283
	mov	al,[output_format]
284
	cmp	al,2
285
	je	mz_segment
286
	cmp	al,5
287
	je	elf_segment
288
	jmp	illegal_instruction
289
section_directive:
31 halyavin 290
	cmp	[virtual_data],0
157 heavyiron 291
	jne	illegal_instruction
292
	mov	al,[output_format]
293
	cmp	al,3
294
	je	pe_section
295
	cmp	al,4
296
	je	coff_section
297
	cmp	al,5
298
	je	elf_section
299
	jmp	illegal_instruction
300
public_directive:
31 halyavin 301
	mov	al,[output_format]
157 heavyiron 302
	cmp	al,4
303
	je	public_allowed
304
	cmp	al,5
305
	jne	illegal_instruction
306
	bt	[format_flags],0
307
	jc	illegal_instruction
308
      public_allowed:
31 halyavin 309
	mov	[base_code],0C0h
624 heavyiron 310
	lods	byte [esi]
157 heavyiron 311
	cmp	al,2
312
	je	public_label
624 heavyiron 313
	cmp	al,1Dh
314
	jne	invalid_argument
157 heavyiron 315
	lods	byte [esi]
624 heavyiron 316
	and	al,7
317
	add	[base_code],al
318
	lods	byte [esi]
319
	cmp	al,2
320
	jne	invalid_argument
321
      public_label:
322
	lods	dword [esi]
157 heavyiron 323
	cmp	eax,0Fh
324
	jb	invalid_use_of_symbol
325
	je	reserved_word_used_as_symbol
326
	mov	dx,[current_pass]
327
	mov	[eax+18],dx
328
	or	byte [eax+8],8
329
	inc	esi
330
	mov	ebx,[free_additional_memory]
331
	lea	edx,[ebx+10h]
332
	cmp	edx,[structures_buffer]
333
	jae	out_of_memory
334
	mov	[free_additional_memory],edx
335
	mov	[ebx+8],eax
336
	mov	eax,[current_line]
337
	mov	[ebx+0Ch],eax
338
	lods	byte [esi]
339
	cmp	al,86h
340
	jne	invalid_argument
341
	lods	word [esi]
342
	cmp	ax,'('
343
	jne	invalid_argument
344
	mov	[ebx+4],esi
345
	lods	dword [esi]
346
	lea	esi,[esi+eax+1]
347
	mov	al,[base_code]
624 heavyiron 348
	mov	[ebx],al
349
	jmp	instruction_assembled
157 heavyiron 350
extrn_directive:
31 halyavin 351
	mov	al,[output_format]
157 heavyiron 352
	cmp	al,4
353
	je	extrn_allowed
354
	cmp	al,5
355
	jne	illegal_instruction
356
	bt	[format_flags],0
357
	jc	illegal_instruction
358
      extrn_allowed:
31 halyavin 359
	lods	word [esi]
157 heavyiron 360
	cmp	ax,'('
361
	jne	invalid_argument
362
	mov	ebx,esi
363
	lods	dword [esi]
364
	lea	esi,[esi+eax+1]
365
	mov	edx,[free_additional_memory]
366
	lea	eax,[edx+0Ch]
367
	cmp	eax,[structures_buffer]
368
	jae	out_of_memory
369
	mov	[free_additional_memory],eax
370
	mov	byte [edx],80h
624 heavyiron 371
	mov	[edx+4],ebx
157 heavyiron 372
	lods	byte [esi]
373
	cmp	al,86h
374
	jne	invalid_argument
375
	lods	byte [esi]
376
	cmp	al,2
377
	jne	invalid_argument
378
	lods	dword [esi]
379
	cmp	eax,0Fh
380
	jb	invalid_use_of_symbol
381
	je	reserved_word_used_as_symbol
382
	inc	esi
383
	mov	ebx,eax
384
	xor	ah,ah
385
	lods	byte [esi]
386
	cmp	al,':'
387
	je	get_extrn_size
388
	dec	esi
389
	cmp	al,11h
390
	jne	extrn_size_ok
391
      get_extrn_size:
31 halyavin 392
	lods	word [esi]
157 heavyiron 393
	cmp	al,11h
394
	jne	invalid_argument
395
      extrn_size_ok:
31 halyavin 396
	mov	[address_symbol],edx
157 heavyiron 397
	movzx	ecx,ah
398
	mov	[edx+8],ecx
399
	xor	eax,eax
400
	xor	edx,edx
401
	xor	ebp,ebp
402
	mov	ch,2
403
	test	[format_flags],8
404
	jz	make_free_label
405
	mov	ch,4
406
	jmp	make_free_label
407
mark_relocation:
31 halyavin 408
	cmp	[value_type],0
157 heavyiron 409
	je	relocation_ok
410
	cmp	[virtual_data],0
411
	jne	relocation_ok
412
	cmp	[output_format],2
413
	je	mark_mz_relocation
414
	cmp	[output_format],3
415
	je	mark_pe_relocation
416
	cmp	[output_format],4
417
	je	mark_coff_relocation
418
	cmp	[output_format],5
419
	je	mark_elf_relocation
420
      relocation_ok:
31 halyavin 421
	ret
157 heavyiron 422
close_pass:
31 halyavin 423
	mov	al,[output_format]
157 heavyiron 424
	cmp	al,3
425
	je	close_pe
426
	cmp	al,4
427
	je	close_coff
428
	cmp	al,5
429
	je	close_elf
430
	ret
431
31 halyavin 432
 
433
	mov	edx,[additional_memory]
157 heavyiron 434
	push	edi
435
	mov	edi,edx
436
	mov	ecx,1Ch shr 2
437
	xor	eax,eax
438
	rep	stos dword [edi]
439
	mov	[free_additional_memory],edi
440
	pop	edi
441
	mov	word [edx+0Ch],0FFFFh
442
	mov	word [edx+10h],1000h
443
	mov	[code_type],16
444
	jmp	format_defined
607 heavyiron 445
mark_mz_relocation:
31 halyavin 446
	push	eax ebx
157 heavyiron 447
	inc	[number_of_relocations]
448
	mov	ebx,[free_additional_memory]
449
	mov	eax,edi
450
	sub	eax,[code_start]
451
	mov	[ebx],ax
452
	shr	eax,16
453
	shl	ax,12
454
	mov	[ebx+2],ax
455
	cmp	word [ebx],0FFFFh
456
	jne	mz_relocation_ok
457
	inc	word [ebx+2]
458
	sub	word [ebx],10h
459
      mz_relocation_ok:
31 halyavin 460
	add	ebx,4
157 heavyiron 461
	cmp	ebx,[structures_buffer]
462
	jae	out_of_memory
463
	mov	[free_additional_memory],ebx
464
	pop	ebx eax
465
	ret
466
mz_segment:
109 heavyiron 467
	lods	byte [esi]
157 heavyiron 468
	cmp	al,2
469
	jne	invalid_argument
470
	lods	dword [esi]
471
	cmp	eax,0Fh
472
	jb	invalid_use_of_symbol
473
	je	reserved_word_used_as_symbol
474
	inc	esi
475
	mov	ebx,eax
476
	mov	eax,edi
477
	sub	eax,[code_start]
478
	mov	ecx,0Fh
479
	add	eax,0Fh
480
	and	eax,1111b
481
	sub	ecx,eax
482
	mov	edx,edi
483
	xor	al,al
484
	rep	stos byte [edi]
485
	mov	dword [org_origin],edi
486
	mov	dword [org_origin+4],0
487
	mov	[org_registers],0
488
	mov	[org_start],edi
489
	mov	eax,edx
490
	call	undefined_data
491
	mov	eax,edi
492
	sub	eax,[code_start]
493
	shr	eax,4
494
	cmp	eax,10000h
495
	jae	value_out_of_range
496
	mov	edx,eax
497
	mov	al,16
498
	cmp	byte [esi],13h
499
	jne	segment_type_ok
500
	inc	esi
501
	lods	byte [esi]
502
      segment_type_ok:
31 halyavin 503
	mov	[code_type],al
157 heavyiron 504
	mov	eax,edx
505
	mov	cx,0100h
506
	xor	edx,edx
507
	xor	ebp,ebp
508
	mov	[address_symbol],edx
509
	jmp	make_free_label
510
mz_entry:
31 halyavin 511
	lods	byte [esi]
157 heavyiron 512
	cmp	al,'('
513
	jne	invalid_argument
514
	call	get_word_value
515
	cmp	[value_type],1
516
	je	initial_cs_ok
517
	cmp	[error_line],0
518
	jne	initial_cs_ok
519
	mov	eax,[current_line]
520
	mov	[error_line],eax
521
	mov	[error],invalid_address
522
      initial_cs_ok:
31 halyavin 523
	mov	edx,[additional_memory]
157 heavyiron 524
	mov	[edx+16h],ax
525
	lods	byte [esi]
526
	cmp	al,':'
527
	jne	invalid_argument
528
	lods	byte [esi]
529
	cmp	al,'('
530
	jne	invalid_argument
531
	ja	invalid_address
532
	call	get_word_value
533
	cmp	[value_type],0
534
	jne	invalid_use_of_symbol
535
	mov	edx,[additional_memory]
536
	mov	[edx+14h],ax
537
	jmp	instruction_assembled
538
mz_stack:
31 halyavin 539
	lods	byte [esi]
157 heavyiron 540
	cmp	al,'('
541
	jne	invalid_argument
542
	call	get_word_value
543
	cmp	byte [esi],':'
544
	je	stack_pointer
545
	cmp	ax,10h
546
	jb	invalid_value
547
	cmp	[value_type],0
548
	jne	invalid_use_of_symbol
549
	mov	edx,[additional_memory]
550
	mov	[edx+10h],ax
551
	jmp	instruction_assembled
552
      stack_pointer:
31 halyavin 553
	cmp	[value_type],1
157 heavyiron 554
	je	initial_ss_ok
555
	cmp	[error_line],0
556
	jne	initial_ss_ok
557
	mov	eax,[current_line]
558
	mov	[error_line],eax
559
	mov	[error],invalid_address
560
      initial_ss_ok:
31 halyavin 561
	mov	edx,[additional_memory]
157 heavyiron 562
	mov	[edx+0Eh],ax
563
	lods	byte [esi]
564
	cmp	al,':'
565
	jne	invalid_argument
566
	lods	byte [esi]
567
	cmp	al,'('
568
	jne	invalid_argument
569
	call	get_word_value
570
	cmp	[value_type],0
571
	jne	invalid_use_of_symbol
572
	mov	edx,[additional_memory]
573
	mov	[edx+10h],ax
574
	bts	[format_flags],4
575
	jmp	instruction_assembled
576
mz_heap:
31 halyavin 577
	cmp	[output_format],2
157 heavyiron 578
	jne	illegal_instruction
579
	lods	byte [esi]
580
	call	get_size_operator
581
	cmp	ah,1
582
	je	invalid_value
583
	cmp	ah,2
584
	ja	invalid_value
585
	cmp	al,'('
586
	jne	invalid_argument
587
	call	get_word_value
588
	cmp	[value_type],0
589
	jne	invalid_use_of_symbol
590
	mov	edx,[additional_memory]
591
	mov	[edx+0Ch],ax
592
	jmp	instruction_assembled
593
write_mz_header:
31 halyavin 594
	mov	edx,[additional_memory]
157 heavyiron 595
	bt	[format_flags],4
596
	jc	mz_stack_ok
597
	mov	eax,[real_code_size]
598
	dec	eax
599
	shr	eax,4
600
	inc	eax
601
	mov	[edx+0Eh],ax
602
	shl	eax,4
603
	movzx	ecx,word [edx+10h]
604
	add	eax,ecx
605
	mov	[real_code_size],eax
606
      mz_stack_ok:
31 halyavin 607
	mov	edi,[free_additional_memory]
157 heavyiron 608
	mov	eax,[number_of_relocations]
609
	shl	eax,2
610
	add	eax,1Ch
611
	sub	edi,eax
612
	xchg	edi,[free_additional_memory]
613
	mov	ecx,0Fh
614
	add	eax,0Fh
615
	and	eax,1111b
616
	sub	ecx,eax
617
	xor	al,al
618
	rep	stos byte [edi]
619
	sub	edi,[free_additional_memory]
620
	mov	ecx,edi
621
	shr	edi,4
622
	mov	word [edx],'MZ' 	; signature
623
	mov	[edx+8],di		; header size in paragraphs
624
	mov	eax,[number_of_relocations]
625
	mov	[edx+6],ax		; number of relocation entries
626
	mov	eax,[code_size]
627
	add	eax,ecx
628
	mov	esi,eax
629
	shr	esi,9
630
	and	eax,1FFh
631
	inc	si
632
	or	ax,ax
633
	jnz	mz_size_ok
634
	dec	si
635
      mz_size_ok:
31 halyavin 636
	mov	[edx+2],ax		; number of bytes in last page
157 heavyiron 637
	mov	[edx+4],si		; number of pages
638
	mov	eax,[real_code_size]
639
	dec	eax
640
	shr	eax,4
641
	inc	eax
642
	mov	esi,[code_size]
643
	dec	esi
644
	shr	esi,4
645
	inc	esi
646
	sub	eax,esi
647
	mov	[edx+0Ah],ax		; minimum memory in addition to code
648
	add	[edx+0Ch],ax		; maximum memory in addition to code
649
	salc
650
	mov	ah,al
651
	or	[edx+0Ch],ax
652
	mov	word [edx+18h],1Ch	; offset of relocation table
653
	add	[written_size],ecx
654
	call	write
655
	jc	write_failed
656
	ret
657
31 halyavin 658
 
659
	mov	[stub_file],edx
157 heavyiron 660
	or	edx,edx
661
	jnz	stub_from_file
662
	push	esi
663
	mov	edx,edi
664
	xor	eax,eax
665
	mov	ecx,20h
666
	rep	stos dword [edi]
667
	mov	eax,40h+default_stub_end-default_stub
668
	mov	cx,100h+default_stub_end-default_stub
669
	mov	word [edx],'MZ'
670
	mov	word [edx+4],1
671
	mov	word [edx+2],ax
672
	mov	word [edx+8],4
673
	mov	word [edx+0Ah],10h
674
	mov	word [edx+0Ch],0FFFFh
675
	mov	word [edx+10h],cx
676
	mov	word [edx+3Ch],ax
677
	mov	word [edx+18h],40h
678
	lea	edi,[edx+40h]
679
	mov	esi,default_stub
680
	mov	ecx,default_stub_end-default_stub
681
	rep	movs byte [edi],[esi]
682
	pop	esi
683
	jmp	stub_ok
684
      default_stub:
31 halyavin 685
	use16
157 heavyiron 686
	push	cs
687
	pop	ds
688
	mov	dx,stub_message-default_stub
689
	mov	ah,9
690
	int	21h
691
	mov	ax,4C01h
692
	int	21h
693
      stub_message db 'This program cannot be run in DOS mode.',0Dh,0Ah,24h
31 halyavin 694
	rq	1
157 heavyiron 695
      default_stub_end:
31 halyavin 696
	use32
157 heavyiron 697
      stub_from_file:
31 halyavin 698
	push	esi
157 heavyiron 699
	mov	esi,edx
700
	call	open_binary_file
701
	mov	edx,edi
702
	mov	ecx,1Ch
703
	mov	esi,edx
704
	call	read
705
	jc	binary_stub
706
	cmp	word [esi],'MZ'
707
	jne	binary_stub
708
	add	edi,1Ch
709
	movzx	ecx,word [esi+6]
710
	add	ecx,11b
711
	and	ecx,not 11b
712
	add	ecx,(40h-1Ch) shr 2
713
	lea	eax,[edi+ecx*4]
714
	cmp	edi,[display_buffer]
715
	jae	out_of_memory
716
	xor	eax,eax
717
	rep	stos dword [edi]
718
	mov	edx,40h
719
	xchg	dx,[esi+18h]
720
	xor	al,al
721
	call	lseek
722
	movzx	ecx,word [esi+6]
723
	shl	ecx,2
724
	lea	edx,[esi+40h]
725
	call	read
726
	mov	edx,edi
727
	sub	edx,esi
728
	shr	edx,4
729
	xchg	dx,[esi+8]
730
	shl	edx,4
731
	xor	al,al
732
	call	lseek
733
	movzx	ecx,word [esi+4]
734
	dec	ecx
735
	shl	ecx,9
736
	movzx	edx,word [esi+2]
737
	test	edx,edx
872 heavyiron 738
	jnz	stub_header_size_ok
739
	mov	dx,200h
740
     stub_header_size_ok:
741
	add	ecx,edx
157 heavyiron 742
	mov	edx,edi
743
	sub	ecx,eax
744
	je	read_stub_code
745
	jb	stub_code_ok
746
	push	ecx
747
	dec	ecx
748
	shr	ecx,3
749
	inc	ecx
750
	shl	ecx,1
751
	lea	eax,[edi+ecx*4]
752
	cmp	eax,[display_buffer]
753
	jae	out_of_memory
754
	xor	eax,eax
755
	rep	stos dword [edi]
756
	pop	ecx
757
     read_stub_code:
31 halyavin 758
	call	read
157 heavyiron 759
     stub_code_ok:
31 halyavin 760
	call	close
157 heavyiron 761
	mov	edx,edi
762
	sub	edx,esi
763
	mov	ax,dx
764
	and	ax,1FFh
765
	mov	[esi+2],ax
766
	dec	edx
767
	shr	edx,9
768
	inc	edx
769
	mov	[esi+4],dx
770
	mov	eax,edi
771
	sub	eax,esi
772
	mov	[esi+3Ch],eax
773
	pop	esi
774
      stub_ok:
31 halyavin 775
	ret
157 heavyiron 776
      binary_stub:
31 halyavin 777
	mov	esi,edi
157 heavyiron 778
	mov	ecx,40h shr 2
779
	xor	eax,eax
780
	rep	stos dword [edi]
781
	mov	al,2
782
	xor	edx,edx
783
	call	lseek
784
	push	eax
785
	xor	al,al
786
	xor	edx,edx
787
	call	lseek
788
	mov	ecx,[esp]
789
	add	ecx,40h+111b
790
	and	ecx,not 111b
791
	mov	ax,cx
792
	and	ax,1FFh
793
	mov	[esi+2],ax
794
	lea	eax,[ecx+1FFh]
795
	shr	eax,9
796
	mov	[esi+4],ax
797
	mov	[esi+3Ch],ecx
798
	sub	ecx,40h
799
	mov	eax,10000h
800
	sub	eax,ecx
801
	jbe	binary_heap_ok
802
	shr	eax,4
803
	mov	[esi+0Ah],ax
804
      binary_heap_ok:
31 halyavin 805
	mov	word [esi],'MZ'
157 heavyiron 806
	mov	word [esi+8],4
807
	mov	ax,0FFFFh
808
	mov	[esi+0Ch],ax
809
	dec	ax
810
	mov	[esi+10h],ax
811
	sub	ax,0Eh
812
	mov	[esi+0Eh],ax
813
	mov	[esi+16h],ax
814
	mov	word [esi+14h],100h
815
	mov	word [esi+18h],40h
816
	mov	eax,[display_buffer]
817
	sub	eax,ecx
818
	cmp	edi,eax
819
	jae	out_of_memory
820
	mov	edx,edi
821
	shr	ecx,2
822
	xor	eax,eax
823
	rep	stos dword [edi]
824
	pop	ecx
825
	call	read
826
	call	close
827
	pop	esi
828
	ret
829
31 halyavin 830
 
831
	xor	edx,edx
157 heavyiron 832
	mov	[machine],14Ch
833
	mov	[subsystem],3
834
	mov	[subsystem_version],3 + 10 shl 16
835
	mov	[image_base],400000h
836
	test	[format_flags],8
837
	jz	pe_settings
838
	mov	[machine],8664h
839
	mov	[subsystem_version],5 + 0 shl 16
840
	mov	[image_base_high],0
841
      pe_settings:
31 halyavin 842
	cmp	byte [esi],84h
157 heavyiron 843
	je	get_stub_name
844
	cmp	byte [esi],80h
845
	je	get_pe_base
846
	cmp	byte [esi],1Bh
847
	jne	pe_settings_ok
848
	lods	byte [esi]
849
	lods	byte [esi]
850
	test	al,80h+40h
851
	jz	subsystem_setting
852
	cmp	al,80h
853
	je	dll_flag
854
	cmp	al,81h
855
	je	wdm_flag
856
	jmp	pe_settings
857
      dll_flag:
31 halyavin 858
	bts	[format_flags],8
157 heavyiron 859
	jc	setting_already_specified
860
	jmp	pe_settings
861
      wdm_flag:
31 halyavin 862
	bts	[format_flags],9
157 heavyiron 863
	jc	setting_already_specified
864
	jmp	pe_settings
865
      subsystem_setting:
31 halyavin 866
	bts	[format_flags],7
157 heavyiron 867
	jc	setting_already_specified
868
	and	ax,3Fh
869
	mov	[subsystem],ax
870
	cmp	ax,10
992 heavyiron 871
	jb	subsystem_type_ok
872
	or	[format_flags],8
873
      subsystem_type_ok:
874
	cmp	byte [esi],'('
157 heavyiron 875
	jne	pe_settings
876
	inc	esi
877
	cmp	byte [esi],'.'
878
	jne	invalid_value
879
	inc	esi
880
	push	edx
881
	cmp	byte [esi+11],0
882
	jne	invalid_value
883
	cmp	byte [esi+10],2
884
	ja	invalid_value
885
	mov	dx,[esi+8]
886
	cmp	dx,8000h
887
	je	zero_version
888
	mov	eax,[esi+4]
889
	cmp	dx,7
890
	jg	invalid_value
891
	mov	cx,7
892
	sub	cx,dx
893
	mov	eax,[esi+4]
894
	shr	eax,cl
895
	mov	ebx,eax
896
	shr	ebx,24
897
	cmp	bl,100
898
	jae	invalid_value
899
	and	eax,0FFFFFFh
900
	mov	ecx,100
901
	mul	ecx
902
	shrd	eax,edx,24
903
	jnc	version_value_ok
904
	inc	eax
905
      version_value_ok:
31 halyavin 906
	shl	eax,16
157 heavyiron 907
	mov	ax,bx
908
	jmp	subsystem_version_ok
909
      zero_version:
31 halyavin 910
	xor	eax,eax
157 heavyiron 911
      subsystem_version_ok:
31 halyavin 912
	pop	edx
157 heavyiron 913
	add	esi,13
914
	mov	[subsystem_version],eax
915
	jmp	pe_settings
916
      get_pe_base:
31 halyavin 917
	bts	[format_flags],10
157 heavyiron 918
	jc	setting_already_specified
919
	lods	word [esi]
920
	cmp	ah,'('
921
	jne	invalid_argument
922
	cmp	byte [esi],'.'
923
	je	invalid_value
924
	push	edx edi
925
	add	edi,[stub_size]
926
	test	[format_flags],8
927
	jnz	get_peplus_base
992 heavyiron 928
	call	get_dword_value
157 heavyiron 929
	mov	[image_base],eax
930
	jmp	pe_base_ok
931
      get_peplus_base:
992 heavyiron 932
	call	get_qword_value
157 heavyiron 933
	mov	[image_base],eax
934
	mov	[image_base_high],edx
935
      pe_base_ok:
31 halyavin 936
	pop	edi edx
157 heavyiron 937
	cmp	[value_type],0
938
	jne	invalid_use_of_symbol
939
	cmp	byte [esi],84h
940
	jne	pe_settings_ok
941
      get_stub_name:
31 halyavin 942
	lods	byte [esi]
157 heavyiron 943
	lods	word [esi]
944
	cmp	ax,'('
945
	jne	invalid_argument
946
	lods	dword [esi]
947
	mov	edx,esi
948
	add	esi,eax
949
	inc	esi
950
      pe_settings_ok:
31 halyavin 951
	mov	ebp,[stub_size]
157 heavyiron 952
	or	ebp,ebp
953
	jz	make_pe_stub
954
	cmp	edx,[stub_file]
955
	je	pe_stub_ok
956
	sub	edi,[stub_size]
957
	mov	[code_start],edi
958
      make_pe_stub:
31 halyavin 959
	call	make_stub
157 heavyiron 960
	mov	eax,edi
961
	sub	eax,[code_start]
962
	mov	[stub_size],eax
963
	mov	[code_start],edi
964
	mov	ebp,eax
965
      pe_stub_ok:
31 halyavin 966
	mov	edx,edi
157 heavyiron 967
	mov	ecx,18h+0E0h
968
	test	[format_flags],8
969
	jz	zero_pe_header
970
	add	ecx,10h
971
      zero_pe_header:
31 halyavin 972
	add	ebp,ecx
157 heavyiron 973
	shr	ecx,2
974
	xor	eax,eax
975
	rep	stos dword [edi]
976
	mov	word [edx],'PE' 	; signature
977
	mov	ax,[machine]
978
	mov	word [edx+4],ax
979
	mov	dword [edx+38h],1000h	; section alignment
980
	mov	dword [edx+3Ch],200h	; file alignment
981
	mov	word [edx+40h],1	; OS version
982
	mov	eax,[subsystem_version]
983
	mov	[edx+48h],eax
984
	mov	ax,[subsystem]
985
	mov	[edx+5Ch],ax
986
	cmp	ax,1
987
	jne	pe_alignment_ok
988
	mov	eax,20h
989
	mov	dword [edx+38h],eax
990
	mov	dword [edx+3Ch],eax
991
      pe_alignment_ok:
31 halyavin 992
	mov	word [edx+1Ah],VERSION_MAJOR + VERSION_MINOR shl 8
157 heavyiron 993
	test	[format_flags],8
994
	jnz	init_peplus_specific
992 heavyiron 995
	mov	dword [edx+14h],0E0h	; size of optional header
157 heavyiron 996
	mov	dword [edx+16h],10B010Eh; flags and magic value
997
	mov	eax,[image_base]
998
	mov	dword [edx+34h],eax
999
	mov	dword [edx+60h],1000h	; stack reserve
1000
	mov	dword [edx+64h],1000h	; stack commit
1001
	mov	dword [edx+68h],10000h	; heap reserve
1002
	mov	dword [edx+6Ch],0	; heap commit
1003
	mov	dword [edx+74h],16	; number of directories
1004
	jmp	pe_header_ok
1005
      init_peplus_specific:
992 heavyiron 1006
	mov	dword [edx+14h],0F0h	; size of optional header
157 heavyiron 1007
	mov	dword [edx+16h],20B002Eh; flags and magic value
1008
	mov	eax,[image_base]
1009
	mov	dword [edx+30h],eax
1010
	mov	eax,[image_base_high]
1011
	mov	dword [edx+34h],eax
1012
	mov	dword [edx+60h],1000h	; stack reserve
1013
	mov	dword [edx+68h],1000h	; stack commit
1014
	mov	dword [edx+70h],10000h	; heap reserve
1015
	mov	dword [edx+78h],0	; heap commit
1016
	mov	dword [edx+84h],16	; number of directories
1017
      pe_header_ok:
31 halyavin 1018
	bsf	ecx,[edx+3Ch]
157 heavyiron 1019
	imul	ebx,[number_of_sections],28h
1020
	or	ebx,ebx
1021
	jnz	reserve_space_for_section_headers
1022
	mov	ebx,28h
1023
      reserve_space_for_section_headers:
31 halyavin 1024
	add	ebx,ebp
157 heavyiron 1025
	dec	ebx
1026
	shr	ebx,cl
1027
	inc	ebx
1028
	shl	ebx,cl
1029
	sub	ebx,ebp
1030
	mov	ecx,ebx
1031
	mov	eax,[display_buffer]
1032
	sub	eax,ecx
1033
	cmp	edi,eax
1034
	jae	out_of_memory
1035
	shr	ecx,2
1036
	xor	eax,eax
1037
	rep	stos dword [edi]
1038
	mov	eax,edi
1039
	sub	eax,[code_start]
1040
	add	eax,[stub_size]
1041
	mov	[edx+54h],eax		; size of headers
1042
	mov	ecx,[edx+38h]
1043
	dec	ecx
1044
	add	eax,ecx
1045
	not	ecx
1046
	and	eax,ecx
1047
	bt	[format_flags],8
1048
	jc	pe_entry_init_ok
1049
	mov	[edx+28h],eax		; entry point rva
1050
      pe_entry_init_ok:
31 halyavin 1051
	mov	[number_of_sections],0
157 heavyiron 1052
	movzx	ebx,word [edx+14h]
1053
	lea	ebx,[edx+18h+ebx]
1054
	mov	[current_section],ebx
1055
	mov	dword [ebx],'.fla'
1056
	mov	dword [ebx+4],'t'
1057
	mov	[ebx+14h],edi
1058
	mov	[ebx+0Ch],eax
1059
	mov	dword [ebx+24h],0E0000060h
1060
	xor	ecx,ecx
1061
	not	eax
1062
	not	ecx
1063
	add	eax,1
1064
	adc	ecx,0
1065
	add	eax,edi
1066
	adc	ecx,0
1067
	test	[format_flags],8
1068
	jnz	peplus_org
992 heavyiron 1069
	sub	eax,[edx+34h]
157 heavyiron 1070
	sbb	ecx,0
1071
	mov	bl,2
1072
	mov	[code_type],32
1073
	jmp	pe_org_ok
1074
      peplus_org:
992 heavyiron 1075
	sub	eax,[edx+30h]
157 heavyiron 1076
	sbb	ecx,[edx+34h]
1077
	mov	bl,4
1078
	mov	[code_type],64
1079
      pe_org_ok:
31 halyavin 1080
	bt	[resolver_flags],0
157 heavyiron 1081
	jc	pe_labels_type_ok
1082
	xor	bl,bl
1083
      pe_labels_type_ok:
109 heavyiron 1084
	mov	[labels_type],bl
157 heavyiron 1085
	mov	dword [org_origin],eax
1086
	mov	dword [org_origin+4],ecx
1087
	mov	[org_registers],0
1088
	mov	[org_start],edi
1089
	bt	[format_flags],8
1090
	jnc	dll_flag_ok
1091
	or	dword [edx+16h],2000h
1092
      dll_flag_ok:
31 halyavin 1093
	bt	[format_flags],9
157 heavyiron 1094
	jnc	wdm_flag_ok
1095
	or	word [edx+5Eh],2000h
1096
      wdm_flag_ok:
31 halyavin 1097
	jmp	format_defined
607 heavyiron 1098
pe_section:
31 halyavin 1099
	call	close_pe_section
157 heavyiron 1100
	bts	[format_flags],5
1101
	lea	ecx,[ebx+28h]
1102
	add	edx,[edx+54h]
1103
	sub	edx,[stub_size]
1104
	cmp	ecx,edx
1105
	jbe	new_section
1106
	lea	ebx,[edx-28h]
1107
	or	[next_pass_needed],-1
1108
	push	edi
1109
	mov	edi,ebx
1110
	mov	ecx,28h shr 4
1111
	xor	eax,eax
1112
	rep	stos dword [edi]
1113
	pop	edi
1114
      new_section:
31 halyavin 1115
	mov	[ebx+0Ch],eax
157 heavyiron 1116
	lods	word [esi]
1117
	cmp	ax,'('
1118
	jne	invalid_argument
1119
	lea	edx,[esi+4]
1120
	mov	ecx,[esi]
1121
	lea	esi,[esi+4+ecx+1]
1122
	cmp	ecx,8
1123
	ja	name_too_long
1124
	xor	eax,eax
1125
	mov	[ebx],eax
1126
	mov	[ebx+4],eax
1127
	push	esi edi
1128
	mov	edi,ebx
1129
	mov	esi,edx
1130
	rep	movs byte [edi],[esi]
1131
	pop	edi esi
1132
	mov	dword [ebx+24h],0
1133
	mov	[ebx+14h],edi
1134
	mov	edx,[code_start]
1135
	mov	eax,edi
1136
	xor	ecx,ecx
1137
	sub	eax,[ebx+0Ch]
1138
	sbb	ecx,0
1139
	test	[format_flags],8
1140
	jnz	peplus_section_org
992 heavyiron 1141
	sub	eax,[edx+34h]
157 heavyiron 1142
	sbb	ecx,0
1143
	mov	[labels_type],2
1144
	mov	[code_type],32
1145
	bt	[resolver_flags],0
1146
	jc	pe_section_org_ok
1147
	mov	[labels_type],0
1148
	jmp	pe_section_org_ok
1149
      peplus_section_org:
992 heavyiron 1150
	sub	eax,[edx+30h]
157 heavyiron 1151
	sbb	ecx,[edx+34h]
1152
	mov	[labels_type],4
1153
	mov	[code_type],64
1154
	bt	[resolver_flags],0
1155
	jc	pe_section_org_ok
1156
	mov	[labels_type],0
1157
      pe_section_org_ok:
31 halyavin 1158
	mov	dword [org_origin],eax
157 heavyiron 1159
	mov	dword [org_origin+4],ecx
1160
	mov	[org_registers],0
1161
	mov	[org_start],edi
1162
      get_section_flags:
31 halyavin 1163
	lods	byte [esi]
157 heavyiron 1164
	cmp	al,1Ah
1165
	je	set_directory
1166
	cmp	al,19h
1167
	je	section_flag
1168
	dec	esi
1169
	jmp	instruction_assembled
1170
      set_directory:
31 halyavin 1171
	movzx	eax,byte [esi]
157 heavyiron 1172
	inc	esi
1173
	mov	ecx,ebx
1174
	test	[format_flags],8
1175
	jnz	peplus_directory
992 heavyiron 1176
	xchg	ecx,[edx+78h+eax*8]
157 heavyiron 1177
	mov	dword [edx+78h+eax*8+4],-1
1178
	jmp	pe_directory_set
1179
      peplus_directory:
992 heavyiron 1180
	xchg	ecx,[edx+88h+eax*8]
157 heavyiron 1181
	mov	dword [edx+88h+eax*8+4],-1
1182
      pe_directory_set:
31 halyavin 1183
	or	ecx,ecx
157 heavyiron 1184
	jnz	data_already_defined
1185
	push	ebx edx
1186
	call	generate_pe_data
1187
	pop	edx ebx
1188
	jmp	get_section_flags
1189
      section_flag:
31 halyavin 1190
	lods	byte [esi]
157 heavyiron 1191
	cmp	al,9
1192
	je	invalid_argument
1193
	cmp	al,11
1194
	je	invalid_argument
1195
	mov	cl,al
1196
	mov	eax,1
1197
	shl	eax,cl
1198
	test	dword [ebx+24h],eax
1199
	jnz	setting_already_specified
1200
	or	dword [ebx+24h],eax
1201
	jmp	get_section_flags
1202
      close_pe_section:
31 halyavin 1203
	mov	ebx,[current_section]
157 heavyiron 1204
	mov	edx,[code_start]
1205
	mov	eax,edi
1206
	sub	eax,[ebx+14h]
1207
	jnz	finish_section
1208
	bt	[format_flags],5
1209
	jc	finish_section
1210
	mov	eax,[ebx+0Ch]
1211
	ret
1212
      finish_section:
31 halyavin 1213
	mov	[ebx+8],eax
157 heavyiron 1214
	cmp	edi,[undefined_data_end]
1215
	jne	align_section
1216
	cmp	dword [edx+38h],1000h
1217
	jb	align_section
1218
	mov	edi,[undefined_data_start]
1219
      align_section:
31 halyavin 1220
	mov	[undefined_data_end],0
157 heavyiron 1221
	mov	ebp,edi
1222
	sub	ebp,[ebx+14h]
1223
	mov	ecx,[edx+3Ch]
1224
	dec	ecx
1225
	lea	eax,[ebp+ecx]
1226
	not	ecx
1227
	and	eax,ecx
1228
	mov	[ebx+10h],eax
1229
	sub	eax,ebp
1230
	mov	ecx,eax
1231
	xor	al,al
1232
	rep	stos byte [edi]
1233
	mov	eax,[code_start]
1234
	sub	eax,[stub_size]
1235
	sub	[ebx+14h],eax
1236
992 heavyiron 1237
 
1238
	test	byte [ebx+24h],20h
1239
	jz	pe_code_sum_ok
1240
	add	[edx+1Ch],ecx
1241
	cmp	dword [edx+2Ch],0
1242
	jne	pe_code_sum_ok
1243
	mov	eax,[ebx+0Ch]
1244
	mov	[edx+2Ch],eax
1245
      pe_code_sum_ok:
1246
	test	byte [ebx+24h],40h
1247
	jz	pe_data_sum_ok
1248
	add	[edx+20h],ecx
1249
	test	[format_flags],8
1250
	jnz	pe_data_sum_ok
1251
	cmp	dword [edx+30h],0
1252
	jne	pe_data_sum_ok
1253
	mov	eax,[ebx+0Ch]
1254
	mov	[edx+30h],eax
1255
      pe_data_sum_ok:
1256
1257
 
157 heavyiron 1258
	or	eax,eax
1259
	jz	udata_ok
1260
	cmp	dword [ebx+10h],0
1261
	jne	udata_ok
1262
	or	byte [ebx+24h],80h
1263
	add	[edx+24h],ecx
992 heavyiron 1264
      udata_ok:
31 halyavin 1265
992 heavyiron 1266
 
157 heavyiron 1267
	dec	ecx
1268
	add	eax,ecx
1269
	not	ecx
1270
	and	eax,ecx
1271
	add	eax,[ebx+0Ch]
1272
	add	ebx,28h
1273
	mov	[current_section],ebx
1274
	inc	word [number_of_sections]
1275
	jz	format_limitations_exceeded
1276
	ret
1277
data_directive:
31 halyavin 1278
	cmp	[output_format],3
157 heavyiron 1279
	jne	illegal_instruction
1280
	lods	byte [esi]
1281
	cmp	al,1Ah
1282
	je	predefined_data_type
1283
	cmp	al,'('
1284
	jne	invalid_argument
1285
	call	get_byte_value
1286
	cmp	al,16
1287
	jb	data_type_ok
1288
	jmp	invalid_value
1289
      predefined_data_type:
31 halyavin 1290
	movzx	eax,byte [esi]
157 heavyiron 1291
	inc	esi
1292
      data_type_ok:
31 halyavin 1293
	mov	ebx,[current_section]
157 heavyiron 1294
	mov	ecx,edi
1295
	sub	ecx,[ebx+14h]
1296
	add	ecx,[ebx+0Ch]
1297
	mov	edx,[code_start]
1298
	test	[format_flags],8
1299
	jnz	peplus_data
992 heavyiron 1300
	xchg	ecx,[edx+78h+eax*8]
157 heavyiron 1301
	jmp	init_pe_data
1302
      peplus_data:
992 heavyiron 1303
	xchg	ecx,[edx+88h+eax*8]
157 heavyiron 1304
      init_pe_data:
31 halyavin 1305
	or	ecx,ecx
157 heavyiron 1306
	jnz	data_already_defined
1307
	call	allocate_structure_data
1308
	mov	word [ebx],data_directive-assembler
1309
	mov	[ebx+2],al
1310
	mov	edx,[current_line]
1311
	mov	[ebx+4],edx
1312
	call	generate_pe_data
1313
	jmp	instruction_assembled
1314
      end_data:
31 halyavin 1315
	cmp	[output_format],3
157 heavyiron 1316
	jne	illegal_instruction
1317
	call	find_structure_data
1318
	jc	unexpected_instruction
1319
	movzx	eax,byte [ebx+2]
1320
	mov	edx,[current_section]
1321
	mov	ecx,edi
1322
	sub	ecx,[edx+14h]
1323
	add	ecx,[edx+0Ch]
1324
	mov	edx,[code_start]
1325
	test	[format_flags],8
1326
	jnz	end_peplus_data
992 heavyiron 1327
	sub	ecx,[edx+78h+eax*8]
157 heavyiron 1328
	mov	[edx+78h+eax*8+4],ecx
1329
	jmp	remove_structure_data
1330
      end_peplus_data:
992 heavyiron 1331
	sub	ecx,[edx+88h+eax*8]
157 heavyiron 1332
	mov	[edx+88h+eax*8+4],ecx
1333
	jmp	remove_structure_data
1334
pe_entry:
31 halyavin 1335
	lods	byte [esi]
157 heavyiron 1336
	cmp	al,'('
1337
	jne	invalid_argument
1338
	cmp	byte [esi],'.'
1339
	je	invalid_value
1340
	test	[format_flags],8
1341
	jnz	peplus_entry
992 heavyiron 1342
	call	get_dword_value
157 heavyiron 1343
	mov	bl,2
1344
	bt	[resolver_flags],0
1345
	jc	check_pe_entry_label_type
1346
	xor	bl,bl
1347
      check_pe_entry_label_type:
109 heavyiron 1348
	cmp	[value_type],bl
157 heavyiron 1349
	je	pe_entry_ok
1350
	cmp	[error_line],0
1351
	jne	pe_entry_ok
1352
	mov	edx,[current_line]
1353
	mov	[error_line],edx
1354
	mov	[error],invalid_address
1355
      pe_entry_ok:
31 halyavin 1356
	mov	edx,[code_start]
157 heavyiron 1357
	sub	eax,[edx+34h]
1358
	mov	[edx+28h],eax
1359
	jmp	instruction_assembled
1360
      peplus_entry:
992 heavyiron 1361
	call	get_qword_value
157 heavyiron 1362
	mov	bl,4
1363
	bt	[resolver_flags],0
1364
	jc	check_peplus_entry_label_type
992 heavyiron 1365
	xor	bl,bl
157 heavyiron 1366
      check_peplus_entry_label_type:
992 heavyiron 1367
	cmp	[value_type],bl
157 heavyiron 1368
	je	peplus_entry_type_ok
992 heavyiron 1369
	cmp	[error_line],0
157 heavyiron 1370
	jne	peplus_entry_type_ok
992 heavyiron 1371
	mov	edx,[current_line]
157 heavyiron 1372
	mov	[error_line],edx
1373
	mov	[error],invalid_address
1374
      peplus_entry_type_ok:
992 heavyiron 1375
	mov	ecx,[code_start]
157 heavyiron 1376
	sub	eax,[ecx+30h]
1377
	sbb	edx,[ecx+34h]
1378
	jz	peplus_entry_range_ok
992 heavyiron 1379
	mov	edx,[current_line]
157 heavyiron 1380
	mov	[error_line],edx
1381
	mov	[error],value_out_of_range
1382
      peplus_entry_range_ok:
992 heavyiron 1383
	mov	[ecx+28h],eax
157 heavyiron 1384
	jmp	instruction_assembled
1385
pe_stack:
31 halyavin 1386
	lods	byte [esi]
157 heavyiron 1387
	cmp	al,'('
1388
	jne	invalid_argument
1389
	cmp	byte [esi],'.'
1390
	je	invalid_value
1391
	test	[format_flags],8
1392
	jnz	peplus_stack
992 heavyiron 1393
	call	get_dword_value
157 heavyiron 1394
	cmp	[value_type],0
1395
	jne	invalid_use_of_symbol
1396
	mov	edx,[code_start]
1397
	mov	[edx+60h],eax
1398
	cmp	byte [esi],','
1399
	jne	default_stack_commit
1400
	lods	byte [esi]
1401
	lods	byte [esi]
1402
	cmp	al,'('
1403
	jne	invalid_argument
1404
	cmp	byte [esi],'.'
1405
	je	invalid_value
1406
	call	get_dword_value
1407
	cmp	[value_type],0
1408
	jne	invalid_use_of_symbol
1409
	mov	edx,[code_start]
1410
	mov	[edx+64h],eax
1411
	cmp	eax,[edx+60h]
1412
	ja	value_out_of_range
1413
	jmp	instruction_assembled
1414
      default_stack_commit:
31 halyavin 1415
	mov	dword [edx+64h],1000h
157 heavyiron 1416
	mov	eax,[edx+60h]
1417
	cmp	eax,1000h
1418
	ja	instruction_assembled
1419
	mov	dword [edx+64h],eax
1420
	jmp	instruction_assembled
1421
      peplus_stack:
992 heavyiron 1422
	call	get_qword_value
157 heavyiron 1423
	cmp	[value_type],0
1424
	jne	invalid_use_of_symbol
1425
	mov	ecx,[code_start]
1426
	mov	[ecx+60h],eax
1427
	mov	[ecx+64h],edx
1428
	cmp	byte [esi],','
1429
	jne	default_peplus_stack_commit
992 heavyiron 1430
	lods	byte [esi]
157 heavyiron 1431
	lods	byte [esi]
1432
	cmp	al,'('
1433
	jne	invalid_argument
1434
	cmp	byte [esi],'.'
1435
	je	invalid_value
1436
	call	get_qword_value
1437
	cmp	[value_type],0
1438
	jne	invalid_use_of_symbol
1439
	mov	ecx,[code_start]
1440
	mov	[ecx+68h],eax
1441
	mov	[ecx+6Ch],edx
1442
	cmp	edx,[ecx+64h]
1443
	ja	value_out_of_range
1444
	jb	instruction_assembled
1445
	cmp	eax,[ecx+60h]
607 heavyiron 1446
	ja	value_out_of_range
157 heavyiron 1447
	jmp	instruction_assembled
1448
      default_peplus_stack_commit:
992 heavyiron 1449
	mov	dword [ecx+68h],1000h
607 heavyiron 1450
	cmp	dword [ecx+64h],0
1451
	jne	instruction_assembled
157 heavyiron 1452
	mov	eax,[ecx+60h]
607 heavyiron 1453
	cmp	eax,1000h
157 heavyiron 1454
	ja	instruction_assembled
1455
	mov	dword [ecx+68h],eax
607 heavyiron 1456
	jmp	instruction_assembled
157 heavyiron 1457
pe_heap:
31 halyavin 1458
	lods	byte [esi]
157 heavyiron 1459
	cmp	al,'('
1460
	jne	invalid_argument
1461
	cmp	byte [esi],'.'
1462
	je	invalid_value
1463
	test	[format_flags],8
1464
	jnz	peplus_heap
992 heavyiron 1465
	call	get_dword_value
157 heavyiron 1466
	cmp	[value_type],0
1467
	jne	invalid_use_of_symbol
1468
	mov	edx,[code_start]
1469
	mov	[edx+68h],eax
1470
	cmp	byte [esi],','
1471
	jne	instruction_assembled
1472
	lods	byte [esi]
1473
	lods	byte [esi]
1474
	cmp	al,'('
1475
	jne	invalid_argument
1476
	cmp	byte [esi],'.'
1477
	je	invalid_value
1478
	call	get_dword_value
1479
	cmp	[value_type],0
1480
	jne	invalid_use_of_symbol
1481
	mov	edx,[code_start]
1482
	mov	[edx+6Ch],eax
1483
	cmp	eax,[edx+68h]
1484
	ja	value_out_of_range
1485
	jmp	instruction_assembled
1486
      peplus_heap:
992 heavyiron 1487
	call	get_qword_value
157 heavyiron 1488
	cmp	[value_type],0
1489
	jne	invalid_use_of_symbol
1490
	mov	ecx,[code_start]
1491
	mov	[ecx+70h],eax
1492
	mov	[ecx+74h],edx
1493
	cmp	byte [esi],','
1494
	jne	instruction_assembled
1495
	lods	byte [esi]
1496
	lods	byte [esi]
1497
	cmp	al,'('
1498
	jne	invalid_argument
1499
	cmp	byte [esi],'.'
1500
	je	invalid_value
1501
	call	get_qword_value
1502
	cmp	[value_type],0
1503
	jne	invalid_use_of_symbol
1504
	mov	ecx,[code_start]
1505
	mov	[ecx+78h],eax
1506
	mov	[ecx+7Ch],edx
1507
	cmp	edx,[ecx+74h]
1508
	ja	value_out_of_range
1509
	jb	instruction_assembled
1510
	cmp	eax,[edx+70h]
1511
	ja	value_out_of_range
1512
	jmp	instruction_assembled
1513
mark_pe_relocation:
31 halyavin 1514
	push	eax ebx
157 heavyiron 1515
	test	[format_flags],8
1516
	jz	check_pe32_relocation_type
370 heavyiron 1517
	cmp	[value_type],4
1518
	je	pe_relocation_type_ok
157 heavyiron 1519
      check_pe32_relocation_type:
370 heavyiron 1520
	cmp	[value_type],2
1521
	je	pe_relocation_type_ok
1522
	cmp	[error_line],0
157 heavyiron 1523
	jne	pe_relocation_type_ok
1524
	mov	eax,[current_line]
1525
	mov	[error_line],eax
1526
	mov	[error],invalid_use_of_symbol
1527
      pe_relocation_type_ok:
109 heavyiron 1528
	mov	ebx,[current_section]
157 heavyiron 1529
	mov	eax,edi
1530
	sub	eax,[ebx+14h]
1531
	add	eax,[ebx+0Ch]
1532
	mov	ebx,[free_additional_memory]
1533
	inc	[number_of_relocations]
1534
	jz	invalid_use_of_symbol
1535
	add	ebx,5
370 heavyiron 1536
	cmp	ebx,[structures_buffer]
157 heavyiron 1537
	jae	out_of_memory
1538
	mov	[free_additional_memory],ebx
1539
	mov	[ebx-5],eax
370 heavyiron 1540
	cmp	[value_type],2
1541
	je	fixup_32bit
1542
	mov	byte [ebx-1],0Ah
1543
	jmp	fixup_ok
1544
      fixup_32bit:
1545
	mov	byte [ebx-1],3
1546
      fixup_ok:
1547
	pop	ebx eax
157 heavyiron 1548
	ret
1549
generate_pe_data:
31 halyavin 1550
	cmp	al,2
157 heavyiron 1551
	je	make_pe_resource
1552
	cmp	al,5
1553
	je	make_pe_fixups
1554
	ret
1555
make_pe_fixups:
370 heavyiron 1556
	bts	[resolver_flags],0
1557
	jc	pe_relocatable_ok
1558
	or	[next_pass_needed],-1
1559
     pe_relocatable_ok:
1560
	push	esi
1561
	mov	ecx,[number_of_relocations]
1562
	mov	esi,[free_additional_memory]
1563
	lea	eax,[ecx*5]
1564
	sub	esi,eax
1565
	mov	[free_additional_memory],esi
1566
	or	[number_of_relocations],-1
1567
	xor	edx,edx
1568
	mov	ebp,edi
1569
      make_fixups:
1570
	cmp	[esi],edx
1571
	jb	store_fixup
1572
	mov	eax,edi
1573
	sub	eax,ebp
1574
	test	eax,11b
1575
	jz	fixups_block
1576
	xor	ax,ax
1577
	stos	word [edi]
1578
	add	dword [ebx],2
1579
      fixups_block:
1580
	mov	eax,edx
1581
	add	edx,1000h
1582
	cmp	[esi],edx
1583
	jae	fixups_block
1584
	stos	dword [edi]
1585
	mov	ebx,edi
1586
	mov	eax,8
1587
	stos	dword [edi]
1588
      store_fixup:
1589
	jecxz	fixups_done
1590
	add	dword [ebx],2
1591
	mov	ah,[esi+1]
607 heavyiron 1592
	and	ah,0Fh
1593
	mov	al,[esi+4]
1594
	shl	al,4
1595
	or	ah,al
1596
	mov	al,[esi]
1597
	stos	word [edi]
370 heavyiron 1598
	add	esi,5
1599
	loop	make_fixups
1600
      fixups_done:
1601
	pop	esi
1602
	ret
1603
make_pe_resource:
31 halyavin 1604
	cmp	byte [esi],82h
157 heavyiron 1605
	jne	resource_done
1606
	inc	esi
1607
	lods	word [esi]
1608
	cmp	ax,'('
1609
	jne	invalid_argument
1610
	lods	dword [esi]
1611
	mov	edx,esi
1612
	lea	esi,[esi+eax+1]
1613
	cmp	[next_pass_needed],0
1614
	je	resource_from_file
1615
	cmp	[current_pass],0
1616
	jne	reserve_space_for_resource
1617
	mov	[resource_size],0
1618
      reserve_space_for_resource:
31 halyavin 1619
	add	edi,[resource_size]
157 heavyiron 1620
	cmp	edi,[display_buffer]
1621
	ja	out_of_memory
1622
	jmp	resource_done
1623
      resource_from_file:
31 halyavin 1624
	push	esi
157 heavyiron 1625
	mov	esi,edx
1626
	call	open_binary_file
1627
	push	ebx
1628
	mov	esi,[free_additional_memory]
1629
	lea	eax,[esi+20h]
1630
	cmp	eax,[structures_buffer]
1631
	ja	out_of_memory
1632
	mov	edx,esi
1633
	mov	ecx,20h
1634
	call	read
1635
	jc	invalid_file_format
1636
	xor	eax,eax
1637
	cmp	[esi],eax
1638
	jne	invalid_file_format
1639
	mov	ax,0FFFFh
1640
	cmp	[esi+8],eax
1641
	jne	invalid_file_format
1642
	cmp	[esi+12],eax
1643
	jne	invalid_file_format
1644
	mov	eax,20h
1645
	cmp	[esi+4],eax
1646
	jne	invalid_file_format
1647
      read_resource_headers:
31 halyavin 1648
	test	eax,11b
157 heavyiron 1649
	jz	resource_file_alignment_ok
1650
	mov	edx,4
1651
	and	eax,11b
1652
	sub	edx,eax
1653
	mov	al,1
1654
	call	lseek
1655
      resource_file_alignment_ok:
31 halyavin 1656
	mov	[esi],eax
157 heavyiron 1657
	lea	edx,[esi+12]
1658
	mov	ecx,8
1659
	call	read
1660
	jc	resource_headers_ok
1661
	mov	ecx,[esi+16]
1662
	add	[esi],ecx
1663
	lea	edx,[esi+20]
1664
	sub	ecx,8
1665
	mov	[esi+16],ecx
1666
	lea	eax,[edx+ecx]
1667
	cmp	eax,[structures_buffer]
1668
	ja	out_of_memory
1669
	call	read
1670
	jc	invalid_file_format
1671
	mov	edx,[esi]
1672
	add	edx,[esi+12]
1673
	mov	eax,[esi+16]
1674
	lea	ecx,[esi+20]
1675
	lea	esi,[ecx+eax]
1676
	add	ecx,2
1677
	cmp	word [ecx-2],0FFFFh
1678
	je	resource_header_type_ok
1679
      check_resource_header_type:
31 halyavin 1680
	cmp	ecx,esi
157 heavyiron 1681
	jae	invalid_file_format
1682
	cmp	word [ecx],0
1683
	je	resource_header_type_ok
1684
	add	ecx,2
1685
	jmp	check_resource_header_type
1686
      resource_header_type_ok:
31 halyavin 1687
	add	ecx,2
157 heavyiron 1688
	cmp	word [ecx],0FFFFh
1689
	je	resource_header_name_ok
1690
      check_resource_header_name:
31 halyavin 1691
	cmp	ecx,esi
157 heavyiron 1692
	jae	invalid_file_format
1693
	cmp	word [ecx],0
1694
	je	resource_header_name_ok
1695
	add	ecx,2
1696
	jmp	check_resource_header_name
1697
      resource_header_name_ok:
31 halyavin 1698
	xor	al,al
157 heavyiron 1699
	call	lseek
1700
	jmp	read_resource_headers
1701
      resource_headers_ok:
31 halyavin 1702
	xor	eax,eax
157 heavyiron 1703
	mov	[esi],eax
1704
	mov	[resource_data],edi
1705
	lea	eax,[edi+16]
1706
	cmp	eax,[display_buffer]
1707
	jae	out_of_memory
1708
	xor	eax,eax
1709
	stos	dword [edi]
1710
	call	make_timestamp
1711
	stos	dword [edi]
1712
	xor	eax,eax
1713
	stos	dword [edi]
1714
	stos	dword [edi]
1715
	xor	ebx,ebx
1716
      make_type_name_directory:
31 halyavin 1717
	mov	esi,[free_additional_memory]
157 heavyiron 1718
	xor	edx,edx
1719
      find_type_name:
31 halyavin 1720
	cmp	dword [esi],0
157 heavyiron 1721
	je	type_name_ok
1722
	add	esi,20
1723
	cmp	word [esi],0FFFFh
1724
	je	check_next_type_name
1725
	or	ebx,ebx
1726
	jz	check_this_type_name
1727
	xor	ecx,ecx
1728
      compare_with_previous_type_name:
31 halyavin 1729
	mov	ax,[esi+ecx]
157 heavyiron 1730
	cmp	ax,[ebx+ecx]
1731
	ja	check_this_type_name
1732
	jb	check_next_type_name
1733
	add	ecx,2
1734
	mov	ax,[esi+ecx]
1735
	or	ax,[ebx+ecx]
1736
	jnz	compare_with_previous_type_name
1737
	jmp	check_next_type_name
1738
      check_this_type_name:
31 halyavin 1739
	or	edx,edx
157 heavyiron 1740
	jz	type_name_found
1741
	xor	ecx,ecx
1742
      compare_with_current_type_name:
31 halyavin 1743
	mov	ax,[esi+ecx]
157 heavyiron 1744
	cmp	ax,[edx+ecx]
1745
	ja	check_next_type_name
1746
	jb	type_name_found
1747
	add	ecx,2
1748
	mov	ax,[esi+ecx]
1749
	or	ax,[edx+ecx]
1750
	jnz	compare_with_current_type_name
1751
	jmp	same_type_name
1752
      type_name_found:
31 halyavin 1753
	mov	edx,esi
157 heavyiron 1754
      same_type_name:
31 halyavin 1755
	mov	[esi-16],edi
157 heavyiron 1756
      check_next_type_name:
31 halyavin 1757
	mov	eax,[esi-4]
157 heavyiron 1758
	add	esi,eax
1759
	jmp	find_type_name
1760
      type_name_ok:
31 halyavin 1761
	or	edx,edx
157 heavyiron 1762
	jz	type_name_directory_done
1763
	mov	ebx,edx
1764
      make_type_name_entry:
31 halyavin 1765
	mov	eax,[resource_data]
157 heavyiron 1766
	inc	word [eax+12]
1767
	lea	eax,[edi+8]
1768
	cmp	eax,[display_buffer]
1769
	jae	out_of_memory
1770
	mov	eax,ebx
1771
	stos	dword [edi]
1772
	xor	eax,eax
1773
	stos	dword [edi]
1774
	jmp	make_type_name_directory
1775
      type_name_directory_done:
31 halyavin 1776
	mov	ebx,-1
157 heavyiron 1777
      make_type_id_directory:
31 halyavin 1778
	mov	esi,[free_additional_memory]
157 heavyiron 1779
	mov	edx,10000h
1780
      find_type_id:
31 halyavin 1781
	cmp	dword [esi],0
157 heavyiron 1782
	je	type_id_ok
1783
	add	esi,20
1784
	cmp	word [esi],0FFFFh
1785
	jne	check_next_type_id
1786
	movzx	eax,word [esi+2]
1787
	cmp	eax,ebx
1788
	jle	check_next_type_id
1789
	cmp	eax,edx
1790
	jg	check_next_type_id
1791
	mov	edx,eax
1792
	mov	[esi-16],edi
1793
      check_next_type_id:
31 halyavin 1794
	mov	eax,[esi-4]
157 heavyiron 1795
	add	esi,eax
1796
	jmp	find_type_id
1797
      type_id_ok:
31 halyavin 1798
	cmp	edx,10000h
157 heavyiron 1799
	je	type_id_directory_done
1800
	mov	ebx,edx
1801
      make_type_id_entry:
31 halyavin 1802
	mov	eax,[resource_data]
157 heavyiron 1803
	inc	word [eax+14]
1804
	lea	eax,[edi+8]
1805
	cmp	eax,[display_buffer]
1806
	jae	out_of_memory
1807
	mov	eax,ebx
1808
	stos	dword [edi]
1809
	xor	eax,eax
1810
	stos	dword [edi]
1811
	jmp	make_type_id_directory
1812
      type_id_directory_done:
31 halyavin 1813
	mov	esi,[resource_data]
157 heavyiron 1814
	add	esi,10h
1815
	mov	ecx,[esi-4]
1816
	or	cx,cx
1817
	jz	resource_directories_ok
1818
      make_resource_directories:
31 halyavin 1819
	push	ecx
157 heavyiron 1820
	push	edi
1821
	mov	edx,edi
1822
	sub	edx,[resource_data]
1823
	bts	edx,31
1824
	mov	[esi+4],edx
1825
	lea	eax,[edi+16]
1826
	cmp	eax,[display_buffer]
1827
	jae	out_of_memory
1828
	xor	eax,eax
1829
	stos	dword [edi]
1830
	call	make_timestamp
1831
	stos	dword [edi]
1832
	xor	eax,eax
1833
	stos	dword [edi]
1834
	stos	dword [edi]
1835
	mov	ebp,esi
1836
	xor	ebx,ebx
1837
      make_resource_name_directory:
31 halyavin 1838
	mov	esi,[free_additional_memory]
157 heavyiron 1839
	xor	edx,edx
1840
      find_resource_name:
31 halyavin 1841
	cmp	dword [esi],0
157 heavyiron 1842
	je	resource_name_ok
1843
	push	esi
1844
	cmp	[esi+4],ebp
1845
	jne	check_next_resource_name
1846
	add	esi,20
1847
	call	skip_resource_name
1848
	cmp	word [esi],0FFFFh
1849
	je	check_next_resource_name
1850
	or	ebx,ebx
1851
	jz	check_this_resource_name
1852
	xor	ecx,ecx
1853
      compare_with_previous_resource_name:
31 halyavin 1854
	mov	ax,[esi+ecx]
157 heavyiron 1855
	cmp	ax,[ebx+ecx]
1856
	ja	check_this_resource_name
1857
	jb	check_next_resource_name
1858
	add	ecx,2
1859
	mov	ax,[esi+ecx]
1860
	or	ax,[ebx+ecx]
1861
	jnz	compare_with_previous_resource_name
1862
	jmp	check_next_resource_name
1863
      skip_resource_name:
31 halyavin 1864
	cmp	word [esi],0FFFFh
157 heavyiron 1865
	jne	skip_unicode_string
1866
	add	esi,4
1867
	ret
1868
      skip_unicode_string:
31 halyavin 1869
	add	esi,2
157 heavyiron 1870
	cmp	word [esi-2],0
1871
	jne	skip_unicode_string
1872
	ret
1873
      check_this_resource_name:
31 halyavin 1874
	or	edx,edx
157 heavyiron 1875
	jz	resource_name_found
1876
	xor	ecx,ecx
1877
      compare_with_current_resource_name:
31 halyavin 1878
	mov	ax,[esi+ecx]
157 heavyiron 1879
	cmp	ax,[edx+ecx]
1880
	ja	check_next_resource_name
1881
	jb	resource_name_found
1882
	add	ecx,2
1883
	mov	ax,[esi+ecx]
1884
	or	ax,[edx+ecx]
1885
	jnz	compare_with_current_resource_name
1886
	jmp	same_resource_name
1887
      resource_name_found:
31 halyavin 1888
	mov	edx,esi
157 heavyiron 1889
      same_resource_name:
31 halyavin 1890
	mov	eax,[esp]
157 heavyiron 1891
	mov	[eax+8],edi
1892
      check_next_resource_name:
31 halyavin 1893
	pop	esi
157 heavyiron 1894
	mov	eax,[esi+16]
1895
	lea	esi,[esi+20+eax]
1896
	jmp	find_resource_name
1897
      resource_name_ok:
31 halyavin 1898
	or	edx,edx
157 heavyiron 1899
	jz	resource_name_directory_done
1900
	mov	ebx,edx
1901
      make_resource_name_entry:
31 halyavin 1902
	mov	eax,[esp]
157 heavyiron 1903
	inc	word [eax+12]
1904
	lea	eax,[edi+8]
1905
	cmp	eax,[display_buffer]
1906
	jae	out_of_memory
1907
	mov	eax,ebx
1908
	stos	dword [edi]
1909
	xor	eax,eax
1910
	stos	dword [edi]
1911
	jmp	make_resource_name_directory
1912
      resource_name_directory_done:
31 halyavin 1913
	mov	ebx,-1
157 heavyiron 1914
      make_resource_id_directory:
31 halyavin 1915
	mov	esi,[free_additional_memory]
157 heavyiron 1916
	mov	edx,10000h
1917
      find_resource_id:
31 halyavin 1918
	cmp	dword [esi],0
157 heavyiron 1919
	je	resource_id_ok
1920
	push	esi
1921
	cmp	[esi+4],ebp
1922
	jne	check_next_resource_id
1923
	add	esi,20
1924
	call	skip_resource_name
1925
	cmp	word [esi],0FFFFh
1926
	jne	check_next_resource_id
1927
	movzx	eax,word [esi+2]
1928
	cmp	eax,ebx
1929
	jle	check_next_resource_id
1930
	cmp	eax,edx
1931
	jg	check_next_resource_id
1932
	mov	edx,eax
1933
	mov	eax,[esp]
1934
	mov	[eax+8],edi
1935
      check_next_resource_id:
31 halyavin 1936
	pop	esi
157 heavyiron 1937
	mov	eax,[esi+16]
1938
	lea	esi,[esi+20+eax]
1939
	jmp	find_resource_id
1940
      resource_id_ok:
31 halyavin 1941
	cmp	edx,10000h
157 heavyiron 1942
	je	resource_id_directory_done
1943
	mov	ebx,edx
1944
      make_resource_id_entry:
31 halyavin 1945
	mov	eax,[esp]
157 heavyiron 1946
	inc	word [eax+14]
1947
	lea	eax,[edi+8]
1948
	cmp	eax,[display_buffer]
1949
	jae	out_of_memory
1950
	mov	eax,ebx
1951
	stos	dword [edi]
1952
	xor	eax,eax
1953
	stos	dword [edi]
1954
	jmp	make_resource_id_directory
1955
      resource_id_directory_done:
31 halyavin 1956
	pop	eax
157 heavyiron 1957
	mov	esi,ebp
1958
	pop	ecx
1959
	add	esi,8
1960
	dec	cx
1961
	jnz	make_resource_directories
1962
      resource_directories_ok:
31 halyavin 1963
	shr	ecx,16
157 heavyiron 1964
	jnz	make_resource_directories
1965
	mov	esi,[resource_data]
1966
	add	esi,10h
1967
	movzx	eax,word [esi-4]
1968
	movzx	edx,word [esi-2]
1969
	add	eax,edx
1970
	lea	esi,[esi+eax*8]
1971
	push	edi			; address of language directories
1972
      update_resource_directories:
31 halyavin 1973
	cmp	esi,[esp]
157 heavyiron 1974
	je	resource_directories_updated
1975
	add	esi,10h
1976
	mov	ecx,[esi-4]
1977
	or	cx,cx
1978
	jz	language_directories_ok
1979
      make_language_directories:
31 halyavin 1980
	push	ecx
157 heavyiron 1981
	push	edi
1982
	mov	edx,edi
1983
	sub	edx,[resource_data]
1984
	bts	edx,31
1985
	mov	[esi+4],edx
1986
	lea	eax,[edi+16]
1987
	cmp	eax,[display_buffer]
1988
	jae	out_of_memory
1989
	xor	eax,eax
1990
	stos	dword [edi]
1991
	call	make_timestamp
1992
	stos	dword [edi]
1993
	xor	eax,eax
1994
	stos	dword [edi]
1995
	stos	dword [edi]
1996
	mov	ebp,esi
1997
	mov	ebx,-1
1998
      make_language_id_directory:
31 halyavin 1999
	mov	esi,[free_additional_memory]
157 heavyiron 2000
	mov	edx,10000h
2001
      find_language_id:
31 halyavin 2002
	cmp	dword [esi],0
157 heavyiron 2003
	je	language_id_ok
2004
	push	esi
2005
	cmp	[esi+8],ebp
2006
	jne	check_next_language_id
2007
	add	esi,20
2008
	mov	eax,esi
2009
	call	skip_resource_name
2010
	call	skip_resource_name
2011
	neg	eax
2012
	add	eax,esi
2013
	and	eax,11b
2014
	add	esi,eax
2015
      get_language_id:
31 halyavin 2016
	movzx	eax,word [esi+6]
157 heavyiron 2017
	cmp	eax,ebx
2018
	jle	check_next_language_id
2019
	cmp	eax,edx
2020
	jge	check_next_language_id
2021
	mov	edx,eax
2022
	mov	eax,[esp]
2023
	mov	dword [value],eax
992 heavyiron 2024
      check_next_language_id:
31 halyavin 2025
	pop	esi
157 heavyiron 2026
	mov	eax,[esi+16]
2027
	lea	esi,[esi+20+eax]
2028
	jmp	find_language_id
2029
      language_id_ok:
31 halyavin 2030
	cmp	edx,10000h
157 heavyiron 2031
	je	language_id_directory_done
2032
	mov	ebx,edx
2033
      make_language_id_entry:
31 halyavin 2034
	mov	eax,[esp]
157 heavyiron 2035
	inc	word [eax+14]
2036
	lea	eax,[edi+8]
2037
	cmp	eax,[display_buffer]
2038
	jae	out_of_memory
2039
	mov	eax,ebx
2040
	stos	dword [edi]
2041
	mov	eax,dword [value]
992 heavyiron 2042
	stos	dword [edi]
157 heavyiron 2043
	jmp	make_language_id_directory
2044
      language_id_directory_done:
31 halyavin 2045
	pop	eax
157 heavyiron 2046
	mov	esi,ebp
2047
	pop	ecx
2048
	add	esi,8
2049
	dec	cx
2050
	jnz	make_language_directories
2051
      language_directories_ok:
31 halyavin 2052
	shr	ecx,16
157 heavyiron 2053
	jnz	make_language_directories
2054
	jmp	update_resource_directories
2055
      resource_directories_updated:
31 halyavin 2056
	mov	esi,[resource_data]
157 heavyiron 2057
	push	edi
2058
      make_name_strings:
31 halyavin 2059
	add	esi,10h
157 heavyiron 2060
	movzx	eax,word [esi-2]
2061
	movzx	ecx,word [esi-4]
2062
	add	eax,ecx
2063
	lea	eax,[esi+eax*8]
2064
	push	eax
2065
	or	ecx,ecx
2066
	jz	string_entries_processed
2067
      process_string_entries:
31 halyavin 2068
	push	ecx
157 heavyiron 2069
	mov	edx,edi
2070
	sub	edx,[resource_data]
2071
	bts	edx,31
2072
	xchg	[esi],edx
2073
	mov	ebx,edi
2074
	xor	ax,ax
2075
	stos	word [edi]
2076
      copy_string_data:
31 halyavin 2077
	lea	eax,[edi+2]
157 heavyiron 2078
	cmp	eax,[display_buffer]
2079
	jae	out_of_memory
2080
	mov	ax,[edx]
2081
	or	ax,ax
2082
	jz	string_data_copied
2083
	stos	word [edi]
2084
	inc	word [ebx]
2085
	add	edx,2
2086
	jmp	copy_string_data
2087
      string_data_copied:
31 halyavin 2088
	add	esi,8
157 heavyiron 2089
	pop	ecx
2090
	loop	process_string_entries
2091
      string_entries_processed:
31 halyavin 2092
	pop	esi
157 heavyiron 2093
	cmp	esi,[esp]
2094
	jb	make_name_strings
2095
	mov	eax,edi
2096
	sub	eax,[resource_data]
2097
	test	al,11b
2098
	jz	resource_strings_alignment_ok
2099
	xor	ax,ax
2100
	stos	word [edi]
2101
      resource_strings_alignment_ok:
31 halyavin 2102
	pop	edx
157 heavyiron 2103
	pop	ebx			; address of language directories
2104
	mov	ebp,edi
2105
      update_language_directories:
31 halyavin 2106
	add	ebx,10h
157 heavyiron 2107
	movzx	eax,word [ebx-2]
2108
	movzx	ecx,word [ebx-4]
2109
	add	ecx,eax
2110
      make_data_records:
31 halyavin 2111
	push	ecx
157 heavyiron 2112
	mov	esi,edi
2113
	sub	esi,[resource_data]
2114
	xchg	esi,[ebx+4]
2115
	lea	eax,[edi+16]
2116
	cmp	eax,[display_buffer]
2117
	jae	out_of_memory
2118
	mov	eax,esi
2119
	stos	dword [edi]
2120
	mov	eax,[esi+12]
2121
	stos	dword [edi]
2122
	xor	eax,eax
2123
	stos	dword [edi]
2124
	stos	dword [edi]
2125
	pop	ecx
2126
	add	ebx,8
2127
	loop	make_data_records
2128
	cmp	ebx,edx
2129
	jb	update_language_directories
2130
	pop	ebx			; file handle
2131
	mov	esi,ebp
2132
	mov	ebp,edi
2133
      update_data_records:
31 halyavin 2134
	push	ebp
157 heavyiron 2135
	mov	ecx,edi
2136
	mov	eax,[current_section]
2137
	sub	ecx,[eax+14h]
2138
	add	ecx,[eax+0Ch]
2139
	xchg	ecx,[esi]
2140
	mov	edx,[ecx]
2141
	xor	al,al
2142
	call	lseek
2143
	mov	edx,edi
2144
	mov	ecx,[esi+4]
2145
	add	edi,ecx
2146
	cmp	edi,[display_buffer]
2147
	ja	out_of_memory
2148
	call	read
2149
	mov	eax,edi
2150
	sub	eax,[resource_data]
2151
	and	eax,11b
2152
	jz	resource_data_alignment_ok
2153
	mov	ecx,4
2154
	sub	ecx,eax
2155
	xor	al,al
2156
	rep	stos byte [edi]
2157
      resource_data_alignment_ok:
31 halyavin 2158
	pop	ebp
157 heavyiron 2159
	add	esi,16
2160
	cmp	esi,ebp
2161
	jb	update_data_records
2162
	pop	esi
2163
	call	close
2164
	mov	eax,edi
2165
	sub	eax,[resource_data]
2166
	mov	[resource_size],eax
2167
      resource_done:
31 halyavin 2168
	ret
157 heavyiron 2169
close_pe:
31 halyavin 2170
	call	close_pe_section
157 heavyiron 2171
	mov	edx,[code_start]
2172
	mov	[edx+50h],eax
2173
	call	make_timestamp
2174
	mov	edx,[code_start]
2175
	mov	[edx+8],eax
2176
	mov	eax,[number_of_relocations]
2177
	cmp	eax,-1
2178
	je	pe_relocations_ok
2179
	shl	eax,2
2180
	sub	[free_additional_memory],eax
2181
	btr	[resolver_flags],0
2182
	jnc	pe_relocations_ok
2183
	or	[next_pass_needed],-1
2184
      pe_relocations_ok:
109 heavyiron 2185
	mov	eax,[number_of_sections]
157 heavyiron 2186
	mov	[edx+6],ax
2187
	imul	eax,28h
2188
	movzx	ecx,word [edx+14h]
2189
	lea	eax,[eax+18h+ecx]
2190
	add	eax,[stub_size]
2191
	mov	ecx,[edx+3Ch]
2192
	dec	ecx
2193
	add	eax,ecx
2194
	not	ecx
2195
	and	eax,ecx
2196
	cmp	eax,[edx+54h]
2197
	je	pe_sections_ok
2198
	or	[next_pass_needed],-1
2199
      pe_sections_ok:
31 halyavin 2200
	xor	ecx,ecx
157 heavyiron 2201
	add	edx,78h
2202
	test	[format_flags],8
2203
	jz	process_directories
2204
	add	edx,10h
2205
      process_directories:
31 halyavin 2206
	mov	eax,[edx+ecx*8]
157 heavyiron 2207
	or	eax,eax
2208
	jz	directory_ok
2209
	cmp	dword [edx+ecx*8+4],-1
2210
	jne	directory_ok
2211
      section_data:
31 halyavin 2212
	mov	ebx,[edx+ecx*8]
157 heavyiron 2213
	mov	eax,[ebx+0Ch]
2214
	mov	[edx+ecx*8],eax 	; directory rva
2215
	mov	eax,[ebx+8]
2216
	mov	[edx+ecx*8+4],eax	; directory size
2217
      directory_ok:
31 halyavin 2218
	inc	cl
157 heavyiron 2219
	cmp	cl,10h
2220
	jb	process_directories
2221
	mov	ebx,[code_start]
2222
	sub	ebx,[stub_size]
2223
	mov	ecx,edi
2224
	sub	ecx,ebx
2225
	mov	ebp,ecx
2226
	shr	ecx,1
2227
	xor	eax,eax
2228
	cdq
2229
      calculate_checksum:
31 halyavin 2230
	mov	dx,[ebx]
157 heavyiron 2231
	add	eax,edx
2232
	mov	dx,ax
2233
	shr	eax,16
2234
	add	eax,edx
2235
	add	ebx,2
2236
	loop	calculate_checksum
2237
	add	eax,ebp
2238
	mov	ebx,[code_start]
2239
	mov	[ebx+58h],eax
2240
	ret
2241
31 halyavin 2242
 
2243
	mov	eax,[additional_memory]
157 heavyiron 2244
	mov	[symbols_stream],eax
2245
	mov	ebx,eax
2246
	add	eax,20h
2247
	cmp	eax,[structures_buffer]
2248
	jae	out_of_memory
2249
	mov	[free_additional_memory],eax
2250
	xor	eax,eax
2251
	mov	[ebx],al
2252
	mov	[ebx+4],eax
2253
	mov	[ebx+8],edi
2254
	mov	al,4
2255
	mov	[ebx+10h],eax
2256
	mov	al,60h
2257
	bt	[format_flags],0
2258
	jnc	flat_section_flags_ok
2259
	or	eax,0E0000000h
2260
      flat_section_flags_ok:
31 halyavin 2261
	mov	dword [ebx+14h],eax
157 heavyiron 2262
	mov	[current_section],ebx
2263
	mov	[number_of_sections],0
2264
	mov	dword [org_origin],edi
2265
	mov	dword [org_origin+4],0
2266
	mov	[org_registers],0
2267
	mov	[org_start],edi
2268
	mov	[org_symbol],ebx
2269
	mov	[labels_type],2
2270
	mov	[code_type],32
2271
	test	[format_flags],8
2272
	jz	format_defined
607 heavyiron 2273
	mov	[labels_type],4
157 heavyiron 2274
	mov	[code_type],64
2275
	jmp	format_defined
607 heavyiron 2276
coff_section:
31 halyavin 2277
	call	close_coff_section
157 heavyiron 2278
	mov	ebx,[free_additional_memory]
2279
	lea	eax,[ebx+20h]
2280
	cmp	eax,[structures_buffer]
2281
	jae	out_of_memory
2282
	mov	[free_additional_memory],eax
2283
	mov	[current_section],ebx
2284
	inc	[number_of_sections]
2285
	xor	eax,eax
2286
	mov	[ebx],al
2287
	mov	[ebx+8],edi
2288
	mov	dword [org_origin],edi
2289
	mov	dword [org_origin+4],0
2290
	mov	[org_registers],0
2291
	mov	[org_start],edi
2292
	mov	[org_symbol],ebx
2293
	mov	[labels_type],2
2294
	test	[format_flags],8
2295
	jz	coff_labels_type_ok
2296
	mov	[labels_type],4
2297
      coff_labels_type_ok:
31 halyavin 2298
	mov	[ebx+10h],eax
157 heavyiron 2299
	mov	[ebx+14h],eax
2300
	lods	word [esi]
2301
	cmp	ax,'('
2302
	jne	invalid_argument
2303
	mov	[ebx+4],esi
2304
	mov	ecx,[esi]
2305
	lea	esi,[esi+4+ecx+1]
2306
	cmp	ecx,8
2307
	ja	name_too_long
2308
      coff_section_flags:
31 halyavin 2309
	cmp	byte [esi],1Ch
157 heavyiron 2310
	je	coff_section_alignment
2311
	cmp	byte [esi],19h
2312
	jne	coff_section_settings_ok
2313
	inc	esi
2314
	lods	byte [esi]
2315
	bt	[format_flags],0
2316
	jc	coff_section_flag_ok
2317
	cmp	al,7
2318
	ja	invalid_argument
2319
      coff_section_flag_ok:
31 halyavin 2320
	mov	cl,al
157 heavyiron 2321
	mov	eax,1
2322
	shl	eax,cl
2323
	test	dword [ebx+14h],eax
2324
	jnz	setting_already_specified
2325
	or	dword [ebx+14h],eax
2326
	jmp	coff_section_flags
2327
      coff_section_alignment:
31 halyavin 2328
	bt	[format_flags],0
157 heavyiron 2329
	jnc	invalid_argument
2330
	inc	esi
2331
	lods	byte [esi]
2332
	or	al,al
2333
	jnz	invalid_argument
2334
	lods	byte [esi]
2335
	cmp	al,'('
2336
	jne	invalid_argument
2337
	cmp	byte [esi],'.'
2338
	je	invalid_value
2339
	push	ebx
2340
	call	get_dword_value
2341
	pop	ebx
2342
	cmp	[value_type],0
2343
	jne	invalid_use_of_symbol
2344
	mov	edx,eax
2345
	dec	edx
2346
	test	eax,edx
2347
	jnz	invalid_value
2348
	or	eax,eax
2349
	jz	invalid_value
2350
	cmp	eax,2000h
2351
	ja	invalid_value
2352
	bsf	edx,eax
2353
	inc	edx
2354
	shl	edx,20
2355
	or	[ebx+14h],edx
2356
	xchg	[ebx+10h],eax
2357
	or	eax,eax
2358
	jnz	setting_already_specified
2359
	jmp	coff_section_flags
2360
      coff_section_settings_ok:
31 halyavin 2361
	cmp	dword [ebx+10h],0
157 heavyiron 2362
	jne	instruction_assembled
2363
	mov	dword [ebx+10h],4
2364
	bt	[format_flags],0
2365
	jnc	instruction_assembled
2366
	or	dword [ebx+14h],300000h
2367
	jmp	instruction_assembled
2368
      close_coff_section:
31 halyavin 2369
	mov	ebx,[current_section]
157 heavyiron 2370
	mov	eax,edi
2371
	mov	edx,[ebx+8]
2372
	sub	eax,edx
2373
	mov	[ebx+0Ch],eax
2374
	xor	eax,eax
2375
	xchg	[undefined_data_end],eax
2376
	cmp	eax,edi
2377
	jne	coff_section_ok
2378
	cmp	edx,[undefined_data_start]
2379
	jne	coff_section_ok
2380
	mov	edi,edx
2381
	or	byte [ebx+14h],80h
2382
      coff_section_ok:
31 halyavin 2383
	ret
157 heavyiron 2384
mark_coff_relocation:
31 halyavin 2385
	cmp	[value_type],3
157 heavyiron 2386
	je	coff_relocation_relative
2387
	push	ebx eax
2388
	test	[format_flags],8
2389
	jnz	coff_64bit_relocation
2390
	mov	al,6
2391
	cmp	[value_type],5
992 heavyiron 2392
	jne	coff_relocation
2393
	inc	al
2394
	jmp	coff_relocation
157 heavyiron 2395
      coff_64bit_relocation:
31 halyavin 2396
	mov	al,1
157 heavyiron 2397
	cmp	[value_type],4
2398
	je	coff_relocation
2399
	mov	al,2
2400
	cmp	[value_type],5
992 heavyiron 2401
	jne	coff_relocation
2402
	inc	al
2403
	jmp	coff_relocation
157 heavyiron 2404
      coff_relocation_relative:
31 halyavin 2405
	push	ebx
157 heavyiron 2406
	bt	[format_flags],0
2407
	jnc	relative_ok
2408
	mov	ebx,[current_section]
2409
	mov	ebx,[ebx+8]
2410
	sub	ebx,edi
2411
	sub	eax,ebx
2412
	add	eax,4
2413
      relative_ok:
31 halyavin 2414
	push	eax
157 heavyiron 2415
	mov	al,20
2416
	test	[format_flags],8
2417
	jnz	relative_coff_64bit_relocation
2418
	cmp	[labels_type],2
2419
	jne	invalid_use_of_symbol
2420
	jmp	coff_relocation
2421
      relative_coff_64bit_relocation:
31 halyavin 2422
	mov	al,4
157 heavyiron 2423
	cmp	[labels_type],4
2424
	jne	invalid_use_of_symbol
2425
      coff_relocation:
31 halyavin 2426
	mov	ebx,[free_additional_memory]
157 heavyiron 2427
	add	ebx,0Ch
2428
	cmp	ebx,[structures_buffer]
2429
	jae	out_of_memory
2430
	mov	[free_additional_memory],ebx
2431
	mov	byte [ebx-0Ch],al
2432
	mov	eax,[current_section]
2433
	mov	eax,[eax+8]
2434
	neg	eax
2435
	add	eax,edi
2436
	mov	[ebx-0Ch+4],eax
2437
	mov	eax,[symbol_identifier]
2438
	mov	[ebx-0Ch+8],eax
2439
	pop	eax ebx
2440
	ret
2441
close_coff:
31 halyavin 2442
	call	close_coff_section
157 heavyiron 2443
	cmp	[next_pass_needed],0
2444
	je	coff_closed
2445
	mov	eax,[symbols_stream]
2446
	mov	[free_additional_memory],eax
2447
      coff_closed:
31 halyavin 2448
	ret
157 heavyiron 2449
coff_formatter:
31 halyavin 2450
	sub	edi,[code_start]
157 heavyiron 2451
	mov	[code_size],edi
2452
	call	prepare_default_section
2453
	mov	edi,[free_additional_memory]
2454
	mov	ebx,edi
2455
	mov	ecx,28h shr 2
2456
	imul	ecx,[number_of_sections]
2457
	add	ecx,14h shr 2
2458
	lea	eax,[edi+ecx*4]
2459
	cmp	eax,[structures_buffer]
2460
	jae	out_of_memory
2461
	xor	eax,eax
2462
	rep	stos dword [edi]
2463
	mov	word [ebx],14Ch
2464
	test	[format_flags],8
2465
	jz	coff_magic_ok
2466
	mov	word [ebx],8664h
2467
      coff_magic_ok:
31 halyavin 2468
	mov	word [ebx+12h],104h
157 heavyiron 2469
	bt	[format_flags],0
2470
	jnc	coff_flags_ok
2471
	or	byte [ebx+12h],80h
2472
      coff_flags_ok:
31 halyavin 2473
	push	ebx
157 heavyiron 2474
	call	make_timestamp
2475
	pop	ebx
2476
	mov	[ebx+4],eax
2477
	mov	eax,[number_of_sections]
2478
	mov	[ebx+2],ax
2479
	mov	esi,[symbols_stream]
2480
	xor	eax,eax
2481
	xor	ecx,ecx
2482
      enumerate_symbols:
31 halyavin 2483
	cmp	esi,[free_additional_memory]
157 heavyiron 2484
	je	symbols_enumerated
2485
	mov	dl,[esi]
2486
	or	dl,dl
2487
	jz	enumerate_section
2488
	cmp	dl,0C0h
624 heavyiron 2489
	jae	enumerate_public
2490
	cmp	dl,80h
157 heavyiron 2491
	jae	enumerate_extrn
624 heavyiron 2492
	add	esi,0Ch
157 heavyiron 2493
	jmp	enumerate_symbols
2494
      enumerate_section:
31 halyavin 2495
	mov	edx,eax
157 heavyiron 2496
	shl	edx,8
2497
	mov	[esi],edx
2498
	inc	eax
2499
	inc	ecx
2500
	mov	[esi+1Eh],cx
2501
	add	esi,20h
2502
	jmp	enumerate_symbols
2503
      enumerate_public:
31 halyavin 2504
	mov	edx,eax
157 heavyiron 2505
	shl	edx,8
2506
	mov	dl,[esi]
624 heavyiron 2507
	mov	[esi],edx
157 heavyiron 2508
	mov	edx,[esi+8]
2509
	add	esi,10h
2510
	inc	eax
2511
	cmp	byte [edx+11],2
2512
	jne	enumerate_symbols
2513
	mov	edx,[edx+20]
2514
	cmp	byte [edx],0C0h
624 heavyiron 2515
	jae	enumerate_symbols
2516
	cmp	byte [edx],80h
2517
	jb	enumerate_symbols
2518
	inc	eax
157 heavyiron 2519
	jmp	enumerate_symbols
2520
      enumerate_extrn:
31 halyavin 2521
	mov	edx,eax
157 heavyiron 2522
	shl	edx,8
2523
	mov	dl,[esi]
624 heavyiron 2524
	mov	[esi],edx
157 heavyiron 2525
	add	esi,0Ch
2526
	inc	eax
2527
	jmp	enumerate_symbols
2528
      prepare_default_section:
31 halyavin 2529
	mov	ebx,[symbols_stream]
157 heavyiron 2530
	cmp	dword [ebx+0Ch],0
2531
	jne	default_section_ok
2532
	cmp	[number_of_sections],0
2533
	je	default_section_ok
2534
	mov	edx,ebx
2535
      find_references_to_default_section:
31 halyavin 2536
	cmp	ebx,[free_additional_memory]
157 heavyiron 2537
	jne	check_reference
2538
	add	[symbols_stream],20h
2539
	ret
2540
      check_reference:
31 halyavin 2541
	mov	al,[ebx]
157 heavyiron 2542
	or	al,al
2543
	jz	skip_other_section
2544
	cmp	al,0C0h
624 heavyiron 2545
	jae	check_public_reference
2546
	cmp	al,80h
157 heavyiron 2547
	jae	next_reference
624 heavyiron 2548
	cmp	edx,[ebx+8]
157 heavyiron 2549
	je	default_section_ok
2550
      next_reference:
31 halyavin 2551
	add	ebx,0Ch
157 heavyiron 2552
	jmp	find_references_to_default_section
2553
      check_public_reference:
31 halyavin 2554
	mov	eax,[ebx+8]
157 heavyiron 2555
	add	ebx,10h
2556
	test	byte [eax+8],1
2557
	jz	find_references_to_default_section
2558
	mov	cx,[current_pass]
2559
	cmp	cx,[eax+16]
2560
	jne	find_references_to_default_section
2561
	cmp	edx,[eax+20]
2562
	je	default_section_ok
2563
	jmp	find_references_to_default_section
2564
      skip_other_section:
31 halyavin 2565
	add	ebx,20h
157 heavyiron 2566
	jmp	find_references_to_default_section
2567
      default_section_ok:
31 halyavin 2568
	inc	[number_of_sections]
157 heavyiron 2569
	ret
2570
      symbols_enumerated:
31 halyavin 2571
	mov	[ebx+0Ch],eax
157 heavyiron 2572
	mov	ebp,edi
2573
	sub	ebp,ebx
2574
	push	ebp
2575
	lea	edi,[ebx+14h]
2576
	mov	esi,[symbols_stream]
2577
      find_section:
31 halyavin 2578
	cmp	esi,[free_additional_memory]
157 heavyiron 2579
	je	sections_finished
2580
	mov	al,[esi]
2581
	or	al,al
2582
	jz	section_found
2583
	add	esi,0Ch
2584
	cmp	al,0C0h
624 heavyiron 2585
	jb	find_section
2586
	add	esi,4
157 heavyiron 2587
	jmp	find_section
2588
      section_found:
31 halyavin 2589
	push	esi edi
157 heavyiron 2590
	mov	esi,[esi+4]
2591
	or	esi,esi
2592
	jz	default_section
2593
	mov	ecx,[esi]
2594
	add	esi,4
2595
	rep	movs byte [edi],[esi]
2596
	jmp	section_name_ok
2597
      default_section:
31 halyavin 2598
	mov	al,'.'
157 heavyiron 2599
	stos	byte [edi]
2600
	mov	eax,'flat'
2601
	stos	dword [edi]
2602
      section_name_ok:
31 halyavin 2603
	pop	edi esi
157 heavyiron 2604
	mov	eax,[esi+0Ch]
2605
	mov	[edi+10h],eax
2606
	mov	eax,[esi+14h]
2607
	mov	[edi+24h],eax
2608
	test	al,80h
2609
	jnz	section_ptr_ok
2610
	mov	eax,[esi+8]
2611
	sub	eax,[code_start]
2612
	add	eax,ebp
2613
	mov	[edi+14h],eax
2614
      section_ptr_ok:
31 halyavin 2615
	mov	ebx,[code_start]
157 heavyiron 2616
	mov	edx,[code_size]
2617
	add	ebx,edx
2618
	add	edx,ebp
2619
	xor	ecx,ecx
2620
	add	esi,20h
2621
      find_relocations:
31 halyavin 2622
	cmp	esi,[free_additional_memory]
157 heavyiron 2623
	je	section_relocations_done
2624
	mov	al,[esi]
2625
	or	al,al
2626
	jz	section_relocations_done
2627
	cmp	al,80h
2628
	jb	add_relocation
2629
	cmp	al,0C0h
624 heavyiron 2630
	jb	next_relocation
2631
	add	esi,10h
157 heavyiron 2632
	jmp	find_relocations
2633
      add_relocation:
31 halyavin 2634
	lea	eax,[ebx+0Ah]
157 heavyiron 2635
	cmp	eax,[display_buffer]
2636
	ja	out_of_memory
2637
	mov	eax,[esi+4]
2638
	mov	[ebx],eax
2639
	mov	eax,[esi+8]
2640
	mov	eax,[eax]
2641
	shr	eax,8
2642
	mov	[ebx+4],eax
2643
	movzx	ax,byte [esi]
2644
	mov	[ebx+8],ax
2645
	add	ebx,0Ah
2646
	inc	ecx
2647
      next_relocation:
31 halyavin 2648
	add	esi,0Ch
157 heavyiron 2649
	jmp	find_relocations
2650
      section_relocations_done:
31 halyavin 2651
	cmp	ecx,10000h
157 heavyiron 2652
	jb	section_relocations_count_16bit
2653
	bt	[format_flags],0
2654
	jnc	format_limitations_exceeded
2655
	mov	word [edi+20h],0FFFFh
2656
	or	dword [edi+24h],1000000h
2657
	mov	[edi+18h],edx
2658
	push	esi edi
2659
	push	ecx
2660
	lea	esi,[ebx-1]
2661
	add	ebx,0Ah
2662
	lea	edi,[ebx-1]
2663
	imul	ecx,0Ah
2664
	std
2665
	rep	movs byte [edi],[esi]
2666
	cld
2667
	pop	ecx
2668
	inc	esi
2669
	inc	ecx
2670
	mov	[esi],ecx
2671
	xor	eax,eax
2672
	mov	[esi+4],eax
2673
	mov	[esi+8],ax
2674
	pop	edi esi
2675
	jmp	section_relocations_ok
2676
      section_relocations_count_16bit:
31 halyavin 2677
	mov	[edi+20h],cx
157 heavyiron 2678
	jcxz	section_relocations_ok
2679
	mov	[edi+18h],edx
2680
      section_relocations_ok:
31 halyavin 2681
	sub	ebx,[code_start]
157 heavyiron 2682
	mov	[code_size],ebx
2683
	add	edi,28h
2684
	jmp	find_section
2685
      sections_finished:
31 halyavin 2686
	mov	edx,[free_additional_memory]
157 heavyiron 2687
	mov	ebx,[code_size]
2688
	add	ebp,ebx
2689
	mov	[edx+8],ebp
2690
	add	ebx,[code_start]
2691
	mov	edi,ebx
2692
	mov	ecx,[edx+0Ch]
2693
	imul	ecx,12h shr 1
2694
	xor	eax,eax
2695
	shr	ecx,1
2696
	jnc	zero_symbols_table
2697
	stos	word [edi]
2698
      zero_symbols_table:
31 halyavin 2699
	rep	stos dword [edi]
157 heavyiron 2700
	mov	edx,edi
2701
	stos	dword [edi]
2702
	mov	esi,[symbols_stream]
2703
      make_symbols_table:
109 heavyiron 2704
	cmp	esi,[free_additional_memory]
157 heavyiron 2705
	je	symbols_table_ok
2706
	mov	al,[esi]
2707
	cmp	al,0C0h
624 heavyiron 2708
	jae	add_public_symbol
2709
	cmp	al,80h
157 heavyiron 2710
	jae	add_extrn_symbol
624 heavyiron 2711
	or	al,al
157 heavyiron 2712
	jz	add_section_symbol
2713
	add	esi,0Ch
2714
	jmp	make_symbols_table
2715
      add_section_symbol:
31 halyavin 2716
	call	store_symbol_name
157 heavyiron 2717
	movzx	eax,word [esi+1Eh]
2718
	mov	[ebx+0Ch],ax
2719
	mov	byte [ebx+10h],3
2720
	add	esi,20h
2721
	add	ebx,12h
2722
	jmp	make_symbols_table
2723
      add_extrn_symbol:
31 halyavin 2724
	call	store_symbol_name
157 heavyiron 2725
	mov	byte [ebx+10h],2
2726
	add	esi,0Ch
2727
	add	ebx,12h
2728
	jmp	make_symbols_table
2729
      add_public_symbol:
31 halyavin 2730
	call	store_symbol_name
157 heavyiron 2731
	mov	eax,[esi+0Ch]
2732
	mov	[current_line],eax
2733
	mov	eax,[esi+8]
2734
	test	byte [eax+8],1
2735
	jz	undefined_coff_public
692 heavyiron 2736
	mov	cx,[current_pass]
157 heavyiron 2737
	cmp	cx,[eax+16]
2738
	jne	undefined_coff_public
692 heavyiron 2739
	mov	cl,[eax+11]
157 heavyiron 2740
	or	cl,cl
2741
	jz	public_constant
2742
	test	[format_flags],8
2743
	jnz	check_64bit_public_symbol
2744
	cmp	cl,2
2745
	je	public_symbol_type_ok
2746
	jmp	invalid_use_of_symbol
2747
      undefined_coff_public:
692 heavyiron 2748
	mov	eax,[eax+24]
2749
	mov	[error_info],eax
2750
	jmp	undefined_symbol
2751
      check_64bit_public_symbol:
31 halyavin 2752
	cmp	cl,4
157 heavyiron 2753
	jne	invalid_use_of_symbol
2754
      public_symbol_type_ok:
31 halyavin 2755
	mov	ecx,[eax+20]
157 heavyiron 2756
	cmp	byte [ecx],80h
624 heavyiron 2757
	je	alias_symbol
157 heavyiron 2758
	cmp	byte [ecx],0
2759
	jne	invalid_use_of_symbol
2760
	mov	cx,[ecx+1Eh]
2761
	mov	[ebx+0Ch],cx
2762
      public_symbol_section_ok:
31 halyavin 2763
	cmp	dword [eax+4],0
157 heavyiron 2764
	je	store_public_symbol
2765
	cmp	dword [eax+4],-1
2766
	jne	value_out_of_range
2767
	bt	dword [eax],31
2768
	jnc	value_out_of_range
2769
      store_public_symbol:
31 halyavin 2770
	mov	eax,[eax]
157 heavyiron 2771
	mov	[ebx+8],eax
2772
	mov	al,2
624 heavyiron 2773
	cmp	byte [esi],0C0h
2774
	je	store_symbol_class
2775
	inc	al
2776
	cmp	byte [esi],0C1h
2777
	je	store_symbol_class
2778
	mov	al,105
2779
      store_symbol_class:
2780
	mov	byte [ebx+10h],al
2781
	add	esi,10h
157 heavyiron 2782
	add	ebx,12h
2783
	jmp	make_symbols_table
2784
      alias_symbol:
31 halyavin 2785
	bt	[format_flags],0
157 heavyiron 2786
	jnc	invalid_use_of_symbol
2787
	mov	ecx,[eax]
2788
	or	ecx,[eax+4]
2789
	jnz	invalid_use_of_symbol
2790
	mov	byte [ebx+10h],69h
2791
	mov	byte [ebx+11h],1
2792
	add	ebx,12h
2793
	mov	ecx,[eax+20]
2794
	mov	ecx,[ecx]
2795
	shr	ecx,8
2796
	mov	[ebx],ecx
2797
	mov	byte [ebx+4],3
2798
	add	esi,10h
2799
	add	ebx,12h
2800
	jmp	make_symbols_table
2801
      public_constant:
31 halyavin 2802
	mov	word [ebx+0Ch],0FFFFh
157 heavyiron 2803
	jmp	public_symbol_section_ok
2804
      symbols_table_ok:
31 halyavin 2805
	mov	eax,edi
157 heavyiron 2806
	sub	eax,edx
2807
	mov	[edx],eax
2808
	sub	edi,[code_start]
2809
	mov	[code_size],edi
2810
	mov	[written_size],0
2811
	mov	edx,[output_file]
2812
	call	create
2813
	jc	write_failed
2814
	mov	edx,[free_additional_memory]
2815
	pop	ecx
2816
	add	[written_size],ecx
2817
	call	write
2818
	jc	write_failed
2819
	jmp	write_output
2820
      store_symbol_name:
31 halyavin 2821
	push	esi
157 heavyiron 2822
	mov	esi,[esi+4]
2823
	or	esi,esi
2824
	jz	default_name
2825
	lods	dword [esi]
2826
	mov	ecx,eax
2827
	cmp	ecx,8
2828
	ja	add_string
2829
	push	edi
2830
	mov	edi,ebx
2831
	rep	movs byte [edi],[esi]
2832
	pop	edi esi
2833
	ret
2834
      default_name:
31 halyavin 2835
	mov	dword [ebx],'.fla'
157 heavyiron 2836
	mov	dword [ebx+4],'t'
2837
	pop	esi
2838
	ret
2839
      add_string:
31 halyavin 2840
	mov	eax,edi
157 heavyiron 2841
	sub	eax,edx
2842
	mov	[ebx+4],eax
2843
	inc	ecx
2844
	rep	movs byte [edi],[esi]
2845
	pop	esi
2846
	ret
2847
31 halyavin 2848
 
2849
	test	[format_flags],8
157 heavyiron 2850
	jnz	format_elf64
2851
	mov	edx,edi
2852
	mov	ecx,34h shr 2
2853
	lea	eax,[edi+ecx*4]
2854
	cmp	eax,[display_buffer]
2855
	jae	out_of_memory
2856
	xor	eax,eax
2857
	rep	stos dword [edi]
2858
	mov	dword [edx],7Fh + 'ELF' shl 8
2859
	mov	al,1
2860
	mov	[edx+4],al
2861
	mov	[edx+5],al
2862
	mov	[edx+6],al
2863
	mov	[edx+14h],al
2864
	mov	byte [edx+12h],3
2865
	mov	byte [edx+28h],34h
2866
	mov	byte [edx+2Eh],28h
2867
	mov	[code_type],32
2868
	cmp	word [esi],1D19h
2869
	je	format_elf_exe
2870
	mov	[labels_type],2
2871
      elf_header_ok:
31 halyavin 2872
	mov	byte [edx+10h],1
157 heavyiron 2873
	mov	eax,[additional_memory]
2874
	mov	[symbols_stream],eax
2875
	mov	ebx,eax
2876
	add	eax,20h
2877
	cmp	eax,[structures_buffer]
2878
	jae	out_of_memory
2879
	mov	[free_additional_memory],eax
2880
	xor	eax,eax
2881
	mov	[current_section],ebx
2882
	mov	[number_of_sections],eax
2883
	mov	dword [org_origin],edi
2884
	mov	dword [org_origin+4],eax
2885
	mov	[org_registers],eax
2886
	mov	[org_start],edi
2887
	mov	[org_symbol],ebx
2888
	mov	[ebx],al
2889
	mov	[ebx+4],eax
2890
	mov	[ebx+8],edi
2891
	mov	al,111b
2892
	mov	[ebx+14h],eax
2893
	mov	al,4
2894
	mov	[ebx+10h],eax
2895
	test	[format_flags],8
2896
	jz	format_defined
607 heavyiron 2897
	mov	byte [ebx+10h],8
157 heavyiron 2898
	jmp	format_defined
607 heavyiron 2899
      format_elf64:
31 halyavin 2900
	mov	edx,edi
157 heavyiron 2901
	mov	ecx,40h shr 2
2902
	lea	eax,[edi+ecx*4]
2903
	cmp	eax,[display_buffer]
2904
	jae	out_of_memory
2905
	xor	eax,eax
2906
	rep	stos dword [edi]
2907
	mov	dword [edx],7Fh + 'ELF' shl 8
2908
	mov	al,1
2909
	mov	[edx+5],al
2910
	mov	[edx+6],al
2911
	mov	[edx+14h],al
2912
	mov	byte [edx+4],2
2913
	mov	byte [edx+12h],62
2914
	mov	byte [edx+34h],40h
2915
	mov	byte [edx+3Ah],40h
2916
	mov	[code_type],64
2917
	cmp	word [esi],1D19h
2918
	je	format_elf64_exe
2919
	mov	[labels_type],4
2920
	jmp	elf_header_ok
2921
elf_section:
31 halyavin 2922
	bt	[format_flags],0
157 heavyiron 2923
	jc	illegal_instruction
2924
	call	close_coff_section
2925
	mov	ebx,[free_additional_memory]
2926
	lea	eax,[ebx+20h]
2927
	cmp	eax,[structures_buffer]
2928
	jae	out_of_memory
2929
	mov	[free_additional_memory],eax
2930
	mov	[current_section],ebx
2931
	inc	word [number_of_sections]
2932
	jz	format_limitations_exceeded
2933
	xor	eax,eax
2934
	mov	[ebx],al
2935
	mov	[ebx+8],edi
2936
	mov	dword [org_origin],edi
2937
	mov	dword [org_origin+4],0
2938
	mov	[org_registers],0
2939
	mov	[org_start],edi
2940
	mov	[org_symbol],ebx
2941
	test	[format_flags],8
2942
	jnz	elf64_labels_type
2943
	mov	[labels_type],2
2944
	jmp	elf_labels_type_ok
2945
      elf64_labels_type:
31 halyavin 2946
	mov	[labels_type],4
157 heavyiron 2947
      elf_labels_type_ok:
31 halyavin 2948
	mov	[ebx+10h],eax
157 heavyiron 2949
	mov	al,10b
2950
	mov	[ebx+14h],eax
2951
	lods	word [esi]
2952
	cmp	ax,'('
2953
	jne	invalid_argument
2954
	mov	[ebx+4],esi
2955
	mov	ecx,[esi]
2956
	lea	esi,[esi+4+ecx+1]
2957
      elf_section_flags:
31 halyavin 2958
	cmp	byte [esi],1Ch
157 heavyiron 2959
	je	elf_section_alignment
2960
	cmp	byte [esi],19h
2961
	jne	elf_section_settings_ok
2962
	inc	esi
2963
	lods	byte [esi]
2964
	sub	al,28
2965
	xor	al,11b
2966
	test	al,not 10b
2967
	jnz	invalid_argument
2968
	mov	cl,al
2969
	mov	al,1
2970
	shl	al,cl
2971
	test	byte [ebx+14h],al
2972
	jnz	setting_already_specified
2973
	or	byte [ebx+14h],al
2974
	jmp	elf_section_flags
2975
      elf_section_alignment:
31 halyavin 2976
	inc	esi
157 heavyiron 2977
	lods	byte [esi]
2978
	or	al,al
2979
	jnz	invalid_argument
2980
	lods	byte [esi]
2981
	cmp	al,'('
2982
	jne	invalid_argument
2983
	cmp	byte [esi],'.'
2984
	je	invalid_value
2985
	push	ebx
2986
	call	get_dword_value
2987
	pop	ebx
2988
	cmp	[value_type],0
2989
	jne	invalid_use_of_symbol
2990
	mov	edx,eax
2991
	dec	edx
2992
	test	eax,edx
2993
	jnz	invalid_value
2994
	or	eax,eax
2995
	jz	invalid_value
2996
	xchg	[ebx+10h],eax
2997
	or	eax,eax
2998
	jnz	setting_already_specified
2999
	jmp	elf_section_flags
3000
      elf_section_settings_ok:
31 halyavin 3001
	cmp	dword [ebx+10h],0
157 heavyiron 3002
	jne	instruction_assembled
3003
	mov	dword [ebx+10h],4
3004
	test	[format_flags],8
3005
	jz	instruction_assembled
3006
	mov	byte [ebx+10h],8
3007
	jmp	instruction_assembled
3008
mark_elf_relocation:
31 halyavin 3009
	cmp	[value_type],3
157 heavyiron 3010
	je	elf_relocation_relative
3011
	cmp	[value_type],7
174 heavyiron 3012
	je	elf_relocation_relative
3013
	push	ebx eax
157 heavyiron 3014
	cmp	[value_type],5
174 heavyiron 3015
	je	elf_gotoff_relocation
157 heavyiron 3016
	ja	invalid_use_of_symbol
174 heavyiron 3017
	mov	al,1			; R_386_32 / R_AMD64_64
3018
	test	[format_flags],8
157 heavyiron 3019
	jz	coff_relocation
3020
	cmp	[value_type],4
3021
	je	coff_relocation
3022
	mov	al,11			; R_AMD64_32S
174 heavyiron 3023
	jmp	coff_relocation
157 heavyiron 3024
      elf_gotoff_relocation:
109 heavyiron 3025
	test	[format_flags],8
174 heavyiron 3026
	jnz	invalid_use_of_symbol
3027
	mov	al,9			; R_386_GOTOFF
3028
	jmp	coff_relocation
157 heavyiron 3029
      elf_relocation_relative:
31 halyavin 3030
	cmp	[labels_type],0
157 heavyiron 3031
	je	invalid_use_of_symbol
3032
	push	ebx
3033
	mov	ebx,[current_section]
3034
	mov	ebx,[ebx+8]
3035
	sub	ebx,edi
3036
	sub	eax,ebx
3037
	push	eax
3038
	mov	al,2			; R_386_PC32 / R_AMD64_PC32
174 heavyiron 3039
	cmp	[value_type],3
3040
	je	coff_relocation
3041
	mov	al,4			; R_386_PLT32 / R_AMD64_PLT32
3042
	jmp	coff_relocation
157 heavyiron 3043
close_elf:
31 halyavin 3044
	bt	[format_flags],0
157 heavyiron 3045
	jc	close_elf_exe
3046
	call	close_coff_section
3047
	cmp	[next_pass_needed],0
3048
	je	elf_closed
3049
	mov	eax,[symbols_stream]
3050
	mov	[free_additional_memory],eax
3051
      elf_closed:
31 halyavin 3052
	ret
157 heavyiron 3053
elf_formatter:
31 halyavin 3054
	push	edi
157 heavyiron 3055
	call	prepare_default_section
3056
	mov	esi,[symbols_stream]
3057
	mov	edi,[free_additional_memory]
3058
	xor	eax,eax
3059
	mov	ecx,4
3060
	rep	stos dword [edi]
3061
	test	[format_flags],8
3062
	jz	find_first_section
3063
	mov	ecx,2
3064
	rep	stos dword [edi]
3065
      find_first_section:
31 halyavin 3066
	mov	al,[esi]
157 heavyiron 3067
	or	al,al
3068
	jz	first_section_found
3069
	cmp	al,0C0h
624 heavyiron 3070
	jb	skip_other_symbol
3071
	add	esi,4
157 heavyiron 3072
      skip_other_symbol:
31 halyavin 3073
	add	esi,0Ch
157 heavyiron 3074
	jmp	find_first_section
3075
      first_section_found:
31 halyavin 3076
	mov	ebx,esi
157 heavyiron 3077
	mov	ebp,esi
3078
	add	esi,20h
3079
	xor	ecx,ecx
3080
	xor	edx,edx
3081
      find_next_section:
31 halyavin 3082
	cmp	esi,[free_additional_memory]
157 heavyiron 3083
	je	make_section_symbol
3084
	mov	al,[esi]
3085
	or	al,al
3086
	jz	make_section_symbol
3087
	cmp	al,0C0h
624 heavyiron 3088
	jae	skip_public
3089
	cmp	al,80h
157 heavyiron 3090
	jae	skip_extrn
624 heavyiron 3091
	or	byte [ebx+14h],40h
157 heavyiron 3092
      skip_extrn:
31 halyavin 3093
	add	esi,0Ch
157 heavyiron 3094
	jmp	find_next_section
3095
      skip_public:
31 halyavin 3096
	add	esi,10h
157 heavyiron 3097
	jmp	find_next_section
3098
      make_section_symbol:
31 halyavin 3099
	mov	eax,edi
157 heavyiron 3100
	xchg	eax,[ebx+4]
3101
	stos	dword [edi]
3102
	test	[format_flags],8
3103
	jnz	elf64_section_symbol
3104
	xor	eax,eax
3105
	stos	dword [edi]
3106
	stos	dword [edi]
3107
	call	store_section_index
3108
	jmp	section_symbol_ok
3109
      store_section_index:
31 halyavin 3110
	inc	ecx
157 heavyiron 3111
	mov	eax,ecx
3112
	shl	eax,8
3113
	mov	[ebx],eax
3114
	inc	dx
3115
	jz	format_limitations_exceeded
3116
	mov	eax,edx
3117
	shl	eax,16
3118
	mov	al,3
3119
	test	byte [ebx+14h],40h
3120
	jz	section_index_ok
3121
	or	ah,-1
3122
	inc	dx
3123
	jz	format_limitations_exceeded
3124
      section_index_ok:
31 halyavin 3125
	stos	dword [edi]
157 heavyiron 3126
	ret
3127
      elf64_section_symbol:
31 halyavin 3128
	call	store_section_index
157 heavyiron 3129
	xor	eax,eax
3130
	stos	dword [edi]
3131
	stos	dword [edi]
3132
	stos	dword [edi]
3133
	stos	dword [edi]
3134
      section_symbol_ok:
31 halyavin 3135
	mov	ebx,esi
157 heavyiron 3136
	add	esi,20h
3137
	cmp	ebx,[free_additional_memory]
3138
	jne	find_next_section
3139
	inc	dx
3140
	jz	format_limitations_exceeded
3141
	mov	[current_section],edx
3142
	mov	esi,[symbols_stream]
3143
      find_other_symbols:
31 halyavin 3144
	cmp	esi,[free_additional_memory]
157 heavyiron 3145
	je	elf_symbol_table_ok
3146
	mov	al,[esi]
3147
	or	al,al
3148
	jz	skip_section
3149
	cmp	al,0C0h
624 heavyiron 3150
	jae	make_public_symbol
3151
	cmp	al,80h
157 heavyiron 3152
	jae	make_extrn_symbol
624 heavyiron 3153
	add	esi,0Ch
157 heavyiron 3154
	jmp	find_other_symbols
3155
      skip_section:
31 halyavin 3156
	add	esi,20h
157 heavyiron 3157
	jmp	find_other_symbols
3158
      make_public_symbol:
31 halyavin 3159
	mov	eax,[esi+0Ch]
157 heavyiron 3160
	mov	[current_line],eax
3161
	cmp	byte [esi],0C0h
624 heavyiron 3162
	jne	invalid_argument
3163
	mov	ebx,[esi+8]
157 heavyiron 3164
	test	byte [ebx+8],1
3165
	jz	undefined_public
692 heavyiron 3166
	mov	ax,[current_pass]
157 heavyiron 3167
	cmp	ax,[ebx+16]
3168
	jne	undefined_public
692 heavyiron 3169
	mov	dl,[ebx+11]
157 heavyiron 3170
	or	dl,dl
3171
	jz	public_absolute
3172
	mov	eax,[ebx+20]
3173
	cmp	byte [eax],0
3174
	jne	invalid_use_of_symbol
3175
	mov	eax,[eax+4]
3176
	test	[format_flags],8
3177
	jnz	elf64_public
3178
	cmp	dl,2
3179
	jne	invalid_use_of_symbol
3180
	mov	dx,[eax+0Eh]
3181
	jmp	section_for_public_ok
3182
      undefined_public:
692 heavyiron 3183
	mov	eax,[ebx+24]
3184
	mov	[error_info],eax
3185
	jmp	undefined_symbol
3186
      elf64_public:
31 halyavin 3187
	cmp	dl,4
157 heavyiron 3188
	jne	invalid_use_of_symbol
3189
	mov	dx,[eax+6]
3190
	jmp	section_for_public_ok
3191
      public_absolute:
31 halyavin 3192
	mov	dx,0FFF1h
157 heavyiron 3193
      section_for_public_ok:
31 halyavin 3194
	mov	eax,[esi+4]
157 heavyiron 3195
	stos	dword [edi]
3196
	test	[format_flags],8
3197
	jnz	elf64_public_symbol
3198
	call	get_public_value
3199
	stos	dword [edi]
3200
	xor	eax,eax
3201
	mov	al,[ebx+10]
3202
	stos	dword [edi]
3203
	mov	eax,edx
3204
	shl	eax,16
3205
	mov	al,10h
3206
	cmp	byte [ebx+10],0
3207
	je	elf_public_function
3208
	or	al,1
3209
	jmp	store_elf_public_info
3210
      elf_public_function:
109 heavyiron 3211
	or	al,2
157 heavyiron 3212
      store_elf_public_info:
109 heavyiron 3213
	stos	dword [edi]
157 heavyiron 3214
	jmp	public_symbol_ok
3215
      elf64_public_symbol:
31 halyavin 3216
	mov	eax,edx
157 heavyiron 3217
	shl	eax,16
3218
	mov	al,10h
3219
	cmp	byte [ebx+10],0
3220
	je	elf64_public_function
3221
	or	al,1
3222
	jmp	store_elf64_public_info
3223
      elf64_public_function:
109 heavyiron 3224
	or	al,2
157 heavyiron 3225
      store_elf64_public_info:
109 heavyiron 3226
	stos	dword [edi]
157 heavyiron 3227
	call	get_public_value
3228
	stos	dword [edi]
3229
	xor	eax,eax
3230
	stos	dword [edi]
3231
	mov	al,[ebx+10]
3232
	stos	dword [edi]
3233
	xor	al,al
3234
	stos	dword [edi]
3235
      public_symbol_ok:
31 halyavin 3236
	inc	ecx
157 heavyiron 3237
	mov	eax,ecx
3238
	shl	eax,8
3239
	mov	al,0C0h
624 heavyiron 3240
	mov	[esi],eax
157 heavyiron 3241
	add	esi,10h
3242
	jmp	find_other_symbols
3243
      get_public_value:
31 halyavin 3244
	mov	eax,[ebx]
157 heavyiron 3245
	cmp	dword [ebx+4],0
3246
	je	public_value_ok
3247
	cmp	dword [ebx+4],-1
3248
	jne	value_out_of_range
3249
	bt	eax,31
3250
	jnc	value_out_of_range
3251
      public_value_ok:
31 halyavin 3252
	ret
157 heavyiron 3253
      make_extrn_symbol:
31 halyavin 3254
	mov	eax,[esi+4]
157 heavyiron 3255
	stos	dword [edi]
3256
	test	[format_flags],8
3257
	jnz	elf64_extrn_symbol
3258
	xor	eax,eax
3259
	stos	dword [edi]
3260
	mov	eax,[esi+8]
3261
	stos	dword [edi]
3262
	mov	eax,10h
3263
	stos	dword [edi]
3264
	jmp	extrn_symbol_ok
3265
      elf64_extrn_symbol:
31 halyavin 3266
	mov	eax,10h
157 heavyiron 3267
	stos	dword [edi]
3268
	xor	al,al
3269
	stos	dword [edi]
3270
	stos	dword [edi]
3271
	mov	eax,[esi+8]
3272
	stos	dword [edi]
3273
	xor	eax,eax
3274
	stos	dword [edi]
3275
      extrn_symbol_ok:
31 halyavin 3276
	inc	ecx
157 heavyiron 3277
	mov	eax,ecx
3278
	shl	eax,8
3279
	mov	al,80h
624 heavyiron 3280
	mov	[esi],eax
157 heavyiron 3281
	add	esi,0Ch
3282
	jmp	find_other_symbols
3283
      elf_symbol_table_ok:
31 halyavin 3284
	mov	edx,edi
157 heavyiron 3285
	mov	ebx,[free_additional_memory]
3286
	xor	al,al
3287
	stos	byte [edi]
3288
	add	edi,16
3289
	mov	[edx+1],edx
3290
	add	ebx,10h
3291
	test	[format_flags],8
3292
	jz	make_string_table
3293
	add	ebx,8
3294
      make_string_table:
31 halyavin 3295
	cmp	ebx,edx
157 heavyiron 3296
	je	elf_string_table_ok
3297
	test	[format_flags],8
3298
	jnz	make_elf64_string
3299
	cmp	byte [ebx+0Dh],0
3300
	je	rel_prefix_ok
3301
	mov	byte [ebx+0Dh],0
3302
	mov	eax,'.rel'
3303
	stos	dword [edi]
3304
      rel_prefix_ok:
31 halyavin 3305
	mov	esi,edi
157 heavyiron 3306
	sub	esi,edx
3307
	xchg	esi,[ebx]
3308
	add	ebx,10h
3309
      make_elf_string:
31 halyavin 3310
	or	esi,esi
157 heavyiron 3311
	jz	default_string
3312
	lods	dword [esi]
3313
	mov	ecx,eax
3314
	rep	movs byte [edi],[esi]
3315
	xor	al,al
3316
	stos	byte [edi]
3317
	jmp	make_string_table
3318
      make_elf64_string:
31 halyavin 3319
	cmp	byte [ebx+5],0
157 heavyiron 3320
	je	elf64_rel_prefix_ok
3321
	mov	byte [ebx+5],0
3322
	mov	eax,'.rel'
3323
	stos	dword [edi]
3324
	mov	al,'a'
3325
	stos	byte [edi]
3326
      elf64_rel_prefix_ok:
31 halyavin 3327
	mov	esi,edi
157 heavyiron 3328
	sub	esi,edx
3329
	xchg	esi,[ebx]
3330
	add	ebx,18h
3331
	jmp	make_elf_string
3332
      default_string:
31 halyavin 3333
	mov	eax,'.fla'
157 heavyiron 3334
	stos	dword [edi]
3335
	mov	ax,'t'
3336
	stos	word [edi]
3337
	jmp	make_string_table
3338
      elf_string_table_ok:
31 halyavin 3339
	mov	[edx+1+8],edi
157 heavyiron 3340
	mov	ebx,[code_start]
3341
	mov	eax,edi
3342
	sub	eax,[free_additional_memory]
3343
	test	[format_flags],8
3344
	jnz	finish_elf64_header
3345
	mov	[ebx+20h],eax
3346
	mov	eax,[current_section]
3347
	inc	ax
3348
	jz	format_limitations_exceeded
3349
	mov	[ebx+32h],ax
3350
	inc	ax
3351
	jz	format_limitations_exceeded
3352
	mov	[ebx+30h],ax
3353
	jmp	elf_header_finished
3354
      finish_elf64_header:
31 halyavin 3355
	mov	[ebx+28h],eax
157 heavyiron 3356
	mov	eax,[current_section]
3357
	inc	ax
3358
	jz	format_limitations_exceeded
3359
	mov	[ebx+3Eh],ax
3360
	inc	ax
3361
	jz	format_limitations_exceeded
3362
	mov	[ebx+3Ch],ax
3363
      elf_header_finished:
31 halyavin 3364
	xor	eax,eax
157 heavyiron 3365
	mov	ecx,10
3366
	rep	stos dword [edi]
3367
	test	[format_flags],8
3368
	jz	elf_null_section_ok
3369
	mov	ecx,6
3370
	rep	stos dword [edi]
3371
      elf_null_section_ok:
31 halyavin 3372
	mov	esi,ebp
157 heavyiron 3373
	xor	ecx,ecx
3374
      make_section_entry:
31 halyavin 3375
	mov	ebx,edi
157 heavyiron 3376
	mov	eax,[esi+4]
3377
	mov	eax,[eax]
3378
	stos	dword [edi]
3379
	mov	eax,1
3380
	cmp	dword [esi+0Ch],0
3381
	je	bss_section
3382
	test	byte [esi+14h],80h
3383
	jz	section_type_ok
3384
      bss_section:
31 halyavin 3385
	mov	al,8
157 heavyiron 3386
      section_type_ok:
31 halyavin 3387
	stos	dword [edi]
157 heavyiron 3388
	mov	eax,[esi+14h]
3389
	and	al,3Fh
3390
	call	store_elf_machine_word
3391
	xor	eax,eax
3392
	call	store_elf_machine_word
3393
	mov	eax,[esi+8]
3394
	mov	[image_base],eax
3395
	sub	eax,[code_start]
3396
	call	store_elf_machine_word
3397
	mov	eax,[esi+0Ch]
3398
	call	store_elf_machine_word
3399
	xor	eax,eax
3400
	stos	dword [edi]
3401
	stos	dword [edi]
3402
	mov	eax,[esi+10h]
3403
	call	store_elf_machine_word
3404
	xor	eax,eax
3405
	call	store_elf_machine_word
3406
	inc	ecx
3407
	add	esi,20h
3408
	xchg	edi,[esp]
3409
	mov	ebp,edi
3410
      convert_relocations:
31 halyavin 3411
	cmp	esi,[free_additional_memory]
157 heavyiron 3412
	je	relocations_converted
3413
	mov	al,[esi]
3414
	or	al,al
3415
	jz	relocations_converted
3416
	cmp	al,80h
3417
	jb	make_relocation_entry
3418
	cmp	al,0C0h
624 heavyiron 3419
	jb	relocation_entry_ok
3420
	add	esi,10h
157 heavyiron 3421
	jmp	convert_relocations
3422
      make_relocation_entry:
31 halyavin 3423
	test	[format_flags],8
157 heavyiron 3424
	jnz	make_elf64_relocation_entry
3425
	mov	eax,[esi+4]
3426
	stos	dword [edi]
3427
	mov	eax,[esi+8]
3428
	mov	eax,[eax]
3429
	mov	al,[esi]
3430
	stos	dword [edi]
3431
	jmp	relocation_entry_ok
3432
      make_elf64_relocation_entry:
31 halyavin 3433
	mov	eax,[esi+4]
157 heavyiron 3434
	stos	dword [edi]
3435
	xor	eax,eax
3436
	stos	dword [edi]
3437
	movzx	eax,byte [esi]
3438
	stos	dword [edi]
3439
	mov	eax,[esi+8]
3440
	mov	eax,[eax]
3441
	shr	eax,8
3442
	stos	dword [edi]
3443
	xor	eax,eax
3444
	stos	dword [edi]
3445
	stos	dword [edi]
3446
      relocation_entry_ok:
31 halyavin 3447
	add	esi,0Ch
157 heavyiron 3448
	jmp	convert_relocations
3449
      store_elf_machine_word:
31 halyavin 3450
	stos	dword [edi]
157 heavyiron 3451
	test	[format_flags],8
3452
	jz	elf_machine_word_ok
3453
	mov	dword [edi],0
3454
	add	edi,4
3455
      elf_machine_word_ok:
31 halyavin 3456
	ret
157 heavyiron 3457
      relocations_converted:
31 halyavin 3458
	cmp	edi,ebp
157 heavyiron 3459
	xchg	edi,[esp]
3460
	je	rel_section_ok
3461
	mov	eax,[ebx]
3462
	sub	eax,4
3463
	test	[format_flags],8
3464
	jz	store_relocations_name_offset
3465
	dec	eax
3466
      store_relocations_name_offset:
31 halyavin 3467
	stos	dword [edi]
157 heavyiron 3468
	test	[format_flags],8
3469
	jnz	rela_section
3470
	mov	eax,9
3471
	jmp	store_relocations_type
3472
      rela_section:
31 halyavin 3473
	mov	eax,4
157 heavyiron 3474
      store_relocations_type:
31 halyavin 3475
	stos	dword [edi]
157 heavyiron 3476
	xor	al,al
3477
	call	store_elf_machine_word
3478
	call	store_elf_machine_word
3479
	mov	eax,ebp
3480
	sub	eax,[code_start]
3481
	call	store_elf_machine_word
3482
	mov	eax,[esp]
3483
	sub	eax,ebp
3484
	call	store_elf_machine_word
3485
	mov	eax,[current_section]
3486
	stos	dword [edi]
3487
	mov	eax,ecx
3488
	stos	dword [edi]
3489
	inc	ecx
3490
	test	[format_flags],8
3491
	jnz	finish_elf64_rela_section
3492
	mov	eax,4
3493
	stos	dword [edi]
3494
	mov	al,8
3495
	stos	dword [edi]
3496
	jmp	rel_section_ok
3497
      finish_elf64_rela_section:
31 halyavin 3498
	mov	eax,8
157 heavyiron 3499
	stos	dword [edi]
3500
	xor	al,al
3501
	stos	dword [edi]
3502
	mov	al,24
3503
	stos	dword [edi]
3504
	xor	al,al
3505
	stos	dword [edi]
3506
      rel_section_ok:
31 halyavin 3507
	cmp	esi,[free_additional_memory]
157 heavyiron 3508
	jne	make_section_entry
3509
	pop	eax
3510
	mov	ebx,[code_start]
3511
	sub	eax,ebx
3512
	mov	[code_size],eax
3513
	mov	ecx,20h
3514
	test	[format_flags],8
3515
	jz	adjust_elf_section_headers_offset
3516
	mov	ecx,28h
3517
      adjust_elf_section_headers_offset:
31 halyavin 3518
	add	[ebx+ecx],eax
157 heavyiron 3519
	mov	eax,1
3520
	stos	dword [edi]
3521
	mov	al,2
3522
	stos	dword [edi]
3523
	xor	al,al
3524
	call	store_elf_machine_word
3525
	call	store_elf_machine_word
3526
	mov	eax,[code_size]
3527
	call	store_elf_machine_word
3528
	mov	eax,[edx+1]
3529
	sub	eax,[free_additional_memory]
3530
	call	store_elf_machine_word
3531
	mov	eax,[current_section]
3532
	inc	eax
3533
	stos	dword [edi]
3534
	mov	eax,[number_of_sections]
3535
	inc	eax
3536
	stos	dword [edi]
3537
	test	[format_flags],8
3538
	jnz	finish_elf64_sym_section
3539
	mov	eax,4
3540
	stos	dword [edi]
3541
	mov	al,10h
3542
	stos	dword [edi]
3543
	jmp	sym_section_ok
3544
      finish_elf64_sym_section:
31 halyavin 3545
	mov	eax,8
157 heavyiron 3546
	stos	dword [edi]
3547
	xor	al,al
3548
	stos	dword [edi]
3549
	mov	al,18h
3550
	stos	dword [edi]
3551
	xor	al,al
3552
	stos	dword [edi]
3553
      sym_section_ok:
31 halyavin 3554
	mov	al,1+8
157 heavyiron 3555
	stos	dword [edi]
3556
	mov	al,3
3557
	stos	dword [edi]
3558
	xor	al,al
3559
	call	store_elf_machine_word
3560
	call	store_elf_machine_word
3561
	mov	eax,[edx+1]
3562
	sub	eax,[free_additional_memory]
3563
	add	eax,[code_size]
3564
	call	store_elf_machine_word
3565
	mov	eax,[edx+1+8]
3566
	sub	eax,[edx+1]
3567
	call	store_elf_machine_word
3568
	xor	eax,eax
3569
	stos	dword [edi]
3570
	stos	dword [edi]
3571
	mov	al,1
3572
	call	store_elf_machine_word
3573
	xor	eax,eax
3574
	call	store_elf_machine_word
3575
	mov	eax,'tab'
3576
	mov	dword [edx+1],'.sym'
3577
	mov	[edx+1+4],eax
3578
	mov	dword [edx+1+8],'.str'
3579
	mov	[edx+1+8+4],eax
3580
	mov	[resource_data],edx
992 heavyiron 3581
	mov	[written_size],0
157 heavyiron 3582
	mov	edx,[output_file]
3583
	call	create
3584
	jc	write_failed
3585
	call	write_code
3586
	mov	ecx,edi
3587
	mov	edx,[free_additional_memory]
3588
	sub	ecx,edx
3589
	add	[written_size],ecx
3590
	call	write
3591
	jc	write_failed
3592
	jmp	output_written
3593
31 halyavin 3594
 
3595
	add	esi,2
157 heavyiron 3596
	or	[format_flags],1
3597
	mov	[image_base],8048000h
3598
	cmp	byte [esi],80h
3599
	jne	elf_exe_base_ok
3600
	lods	word [esi]
3601
	cmp	ah,'('
3602
	jne	invalid_argument
3603
	cmp	byte [esi],'.'
3604
	je	invalid_value
3605
	push	edx
3606
	call	get_dword_value
3607
	cmp	[value_type],0
3608
	jne	invalid_use_of_symbol
3609
	mov	[image_base],eax
3610
	pop	edx
3611
      elf_exe_base_ok:
31 halyavin 3612
	mov	byte [edx+10h],2
157 heavyiron 3613
	mov	byte [edx+2Ah],20h
3614
	mov	ebx,edi
3615
	mov	ecx,20h shr 2
3616
	cmp	[current_pass],0
3617
	je	init_elf_segments
3618
	imul	ecx,[number_of_sections]
3619
      init_elf_segments:
109 heavyiron 3620
	xor	eax,eax
157 heavyiron 3621
	rep	stos dword [edi]
3622
	mov	[number_of_sections],0
3623
	mov	byte [ebx],1
3624
	mov	word [ebx+1Ch],1000h
3625
	mov	byte [ebx+18h],111b
3626
	mov	eax,edi
3627
	sub	eax,[code_start]
3628
	mov	[ebx+4],eax
3629
	add	eax,[image_base]
3630
	mov	[ebx+8],eax
3631
	mov	[ebx+0Ch],eax
3632
	mov	[edx+18h],eax
3633
	xor	edx,edx
3634
	not	eax
3635
	not	edx
3636
	add	eax,1
3637
	adc	edx,0
3638
	add	eax,edi
3639
	adc	edx,0
3640
	mov	dword [org_origin],eax
3641
	mov	dword [org_origin+4],edx
3642
	mov	[org_registers],0
3643
	mov	[org_start],edi
3644
	mov	[symbols_stream],edi
3645
	jmp	format_defined
607 heavyiron 3646
      format_elf64_exe:
31 halyavin 3647
	add	esi,2
157 heavyiron 3648
	or	[format_flags],1
3649
	mov	[image_base],400000h
3650
	mov	[image_base_high],0
3651
	cmp	byte [esi],80h
3652
	jne	elf64_exe_base_ok
3653
	lods	word [esi]
3654
	cmp	ah,'('
3655
	jne	invalid_argument
3656
	cmp	byte [esi],'.'
3657
	je	invalid_value
3658
	push	edx
3659
	call	get_qword_value
3660
	cmp	[value_type],0
3661
	jne	invalid_use_of_symbol
3662
	mov	[image_base],eax
3663
	mov	[image_base_high],edx
3664
	pop	edx
3665
      elf64_exe_base_ok:
31 halyavin 3666
	mov	byte [edx+10h],2
157 heavyiron 3667
	mov	byte [edx+36h],38h
3668
	mov	ebx,edi
3669
	mov	ecx,38h shr 2
3670
	cmp	[current_pass],0
3671
	je	init_elf64_segments
3672
	imul	ecx,[number_of_sections]
3673
      init_elf64_segments:
109 heavyiron 3674
	xor	eax,eax
157 heavyiron 3675
	rep	stos dword [edi]
3676
	mov	[number_of_sections],0
3677
	mov	byte [ebx],1
3678
	mov	word [ebx+30h],1000h
3679
	mov	byte [ebx+4],111b
3680
	push	edx
3681
	mov	eax,edi
3682
	sub	eax,[code_start]
3683
	mov	[ebx+8],eax
3684
	xor	edx,edx
3685
	add	eax,[image_base]
3686
	adc	edx,[image_base_high]
3687
	mov	[ebx+10h],eax
3688
	mov	[ebx+10h+4],edx
3689
	mov	[ebx+18h],eax
3690
	mov	[ebx+18h+4],edx
3691
	pop	ebx
3692
	mov	[ebx+18h],eax
3693
	mov	[ebx+18h+4],edx
3694
	not	eax
3695
	not	edx
3696
	add	eax,1
3697
	adc	edx,0
3698
	add	eax,edi
3699
	adc	edx,0
3700
	mov	dword [org_origin],eax
3701
	mov	dword [org_origin+4],edx
3702
	mov	[org_registers],0
3703
	mov	[org_start],edi
3704
	mov	[symbols_stream],edi
3705
	jmp	format_defined
607 heavyiron 3706
elf_entry:
31 halyavin 3707
	lods	byte [esi]
157 heavyiron 3708
	cmp	al,'('
3709
	jne	invalid_argument
3710
	cmp	byte [esi],'.'
3711
	je	invalid_value
3712
	test	[format_flags],8
3713
	jnz	elf64_entry
3714
	call	get_dword_value
3715
	cmp	[value_type],0
3716
	jne	invalid_use_of_symbol
3717
	mov	edx,[code_start]
3718
	mov	[edx+18h],eax
3719
	jmp	instruction_assembled
3720
      elf64_entry:
31 halyavin 3721
	call	get_qword_value
157 heavyiron 3722
	cmp	[value_type],0
3723
	jne	invalid_use_of_symbol
3724
	mov	ebx,[code_start]
3725
	mov	[ebx+18h],eax
3726
	mov	[ebx+1Ch],edx
3727
	jmp	instruction_assembled
3728
elf_segment:
109 heavyiron 3729
	bt	[format_flags],0
157 heavyiron 3730
	jnc	illegal_instruction
3731
	test	[format_flags],8
3732
	jnz	elf64_segment
3733
	call	close_elf_segment
3734
	push	eax
3735
	mov	ebx,[number_of_sections]
3736
	shl	ebx,5
3737
	add	ebx,[code_start]
3738
	add	ebx,34h
3739
	cmp	ebx,[symbols_stream]
3740
	jb	new_elf_segment
3741
	mov	ebx,[symbols_stream]
3742
	sub	ebx,20h
3743
	push	edi
3744
	mov	edi,ebx
3745
	mov	ecx,20h shr 2
3746
	xor	eax,eax
3747
	rep	stos dword [edi]
3748
	pop	edi
3749
	or	[next_pass_needed],-1
3750
      new_elf_segment:
109 heavyiron 3751
	mov	byte [ebx],1
157 heavyiron 3752
	mov	word [ebx+1Ch],1000h
3753
      elf_segment_flags:
109 heavyiron 3754
	cmp	byte [esi],19h
157 heavyiron 3755
	jne	elf_segment_flags_ok
3756
	lods	word [esi]
3757
	sub	ah,28
3758
	jbe	invalid_argument
3759
	cmp	ah,1
3760
	je	mark_elf_segment_flag
3761
	cmp	ah,3
3762
	ja	invalid_argument
3763
	xor	ah,1
3764
	cmp	ah,2
3765
	je	mark_elf_segment_flag
3766
	inc	ah
3767
      mark_elf_segment_flag:
109 heavyiron 3768
	test	[ebx+18h],ah
157 heavyiron 3769
	jnz	setting_already_specified
3770
	or	[ebx+18h],ah
3771
	jmp	elf_segment_flags
3772
      elf_segment_flags_ok:
109 heavyiron 3773
	mov	eax,edi
157 heavyiron 3774
	sub	eax,[code_start]
3775
	mov	[ebx+4],eax
3776
	pop	edx
3777
	and	eax,0FFFh
3778
	add	edx,eax
3779
	mov	[ebx+8],edx
3780
	mov	[ebx+0Ch],edx
3781
	mov	eax,edx
3782
	xor	edx,edx
3783
	not	eax
3784
	not	edx
3785
	add	eax,1
3786
	adc	edx,0
3787
	add	eax,edi
3788
	adc	edx,0
3789
	mov	dword [org_origin],eax
3790
	mov	dword [org_origin+4],edx
3791
	mov	[org_registers],0
3792
	mov	[org_start],edi
3793
	inc	[number_of_sections]
3794
	jmp	instruction_assembled
3795
      close_elf_segment:
109 heavyiron 3796
	cmp	[number_of_sections],0
157 heavyiron 3797
	jne	finish_elf_segment
3798
	cmp	edi,[symbols_stream]
3799
	jne	first_elf_segment_ok
3800
	push	edi
3801
	mov	edi,[code_start]
3802
	add	edi,34h
3803
	mov	ecx,20h shr 2
3804
	xor	eax,eax
3805
	rep	stos dword [edi]
3806
	pop	edi
3807
	mov	eax,[image_base]
3808
	ret
3809
      first_elf_segment_ok:
109 heavyiron 3810
	inc	[number_of_sections]
157 heavyiron 3811
      finish_elf_segment:
109 heavyiron 3812
	mov	ebx,[number_of_sections]
157 heavyiron 3813
	dec	ebx
3814
	shl	ebx,5
3815
	add	ebx,[code_start]
3816
	add	ebx,34h
3817
	mov	eax,edi
3818
	sub	eax,[code_start]
3819
	sub	eax,[ebx+4]
3820
	mov	edx,edi
3821
	cmp	edi,[undefined_data_end]
3822
	jne	elf_segment_size_ok
3823
	mov	edi,[undefined_data_start]
3824
      elf_segment_size_ok:
109 heavyiron 3825
	mov	[ebx+14h],eax
157 heavyiron 3826
	add	eax,edi
3827
	sub	eax,edx
3828
	mov	[ebx+10h],eax
3829
	mov	eax,[ebx+8]
3830
	add	eax,[ebx+14h]
3831
	add	eax,0FFFh
3832
	and	eax,not 0FFFh
3833
	ret
3834
      elf64_segment:
109 heavyiron 3835
	call	close_elf64_segment
157 heavyiron 3836
	push	eax edx
3837
	mov	ebx,[number_of_sections]
3838
	imul	ebx,38h
3839
	add	ebx,[code_start]
3840
	add	ebx,40h
3841
	cmp	ebx,[symbols_stream]
3842
	jb	new_elf64_segment
3843
	mov	ebx,[symbols_stream]
3844
	sub	ebx,38h
3845
	push	edi
3846
	mov	edi,ebx
3847
	mov	ecx,38h shr 2
3848
	xor	eax,eax
3849
	rep	stos dword [edi]
3850
	pop	edi
3851
	or	[next_pass_needed],-1
3852
      new_elf64_segment:
109 heavyiron 3853
	mov	byte [ebx],1
157 heavyiron 3854
	mov	word [ebx+30h],1000h
3855
      elf64_segment_flags:
109 heavyiron 3856
	cmp	byte [esi],19h
157 heavyiron 3857
	jne	elf64_segment_flags_ok
3858
	lods	word [esi]
3859
	sub	ah,28
3860
	jbe	invalid_argument
3861
	cmp	ah,1
3862
	je	mark_elf64_segment_flag
3863
	cmp	ah,3
3864
	ja	invalid_argument
3865
	xor	ah,1
3866
	cmp	ah,2
3867
	je	mark_elf64_segment_flag
3868
	inc	ah
3869
      mark_elf64_segment_flag:
109 heavyiron 3870
	test	[ebx+4],ah
157 heavyiron 3871
	jnz	setting_already_specified
3872
	or	[ebx+4],ah
3873
	jmp	elf64_segment_flags
3874
      elf64_segment_flags_ok:
109 heavyiron 3875
	mov	ecx,edi
157 heavyiron 3876
	sub	ecx,[code_start]
3877
	mov	[ebx+8],ecx
3878
	pop	edx eax
3879
	and	ecx,0FFFh
3880
	add	eax,ecx
3881
	adc	edx,0
3882
	mov	[ebx+10h],eax
3883
	mov	[ebx+10h+4],edx
3884
	mov	[ebx+18h],eax
3885
	mov	[ebx+18h+4],edx
3886
	not	eax
3887
	not	edx
3888
	add	eax,1
3889
	adc	edx,0
3890
	add	eax,edi
3891
	adc	edx,0
3892
	mov	dword [org_origin],eax
3893
	mov	dword [org_origin+4],edx
3894
	mov	[org_registers],0
3895
	mov	[org_start],edi
3896
	inc	[number_of_sections]
3897
	jmp	instruction_assembled
3898
      close_elf64_segment:
109 heavyiron 3899
	cmp	[number_of_sections],0
157 heavyiron 3900
	jne	finish_elf64_segment
3901
	cmp	edi,[symbols_stream]
3902
	jne	first_elf64_segment_ok
3903
	push	edi
3904
	mov	edi,[code_start]
3905
	add	edi,40h
3906
	mov	ecx,38h shr 2
3907
	xor	eax,eax
3908
	rep	stos dword [edi]
3909
	pop	edi
3910
	mov	eax,[image_base]
3911
	mov	edx,[image_base_high]
3912
	ret
3913
      first_elf64_segment_ok:
109 heavyiron 3914
	inc	[number_of_sections]
157 heavyiron 3915
      finish_elf64_segment:
109 heavyiron 3916
	mov	ebx,[number_of_sections]
157 heavyiron 3917
	dec	ebx
3918
	imul	ebx,38h
3919
	add	ebx,[code_start]
3920
	add	ebx,40h
3921
	mov	eax,edi
3922
	sub	eax,[code_start]
3923
	sub	eax,[ebx+8]
3924
	mov	edx,edi
3925
	cmp	edi,[undefined_data_end]
3926
	jne	elf64_segment_size_ok
3927
	mov	edi,[undefined_data_start]
3928
      elf64_segment_size_ok:
109 heavyiron 3929
	mov	[ebx+28h],eax
157 heavyiron 3930
	add	eax,edi
3931
	sub	eax,edx
3932
	mov	[ebx+20h],eax
3933
	mov	eax,[ebx+10h]
3934
	mov	edx,[ebx+10h+4]
3935
	add	eax,[ebx+28h]
3936
	adc	edx,0
3937
	sub	eax,1
3938
	sbb	edx,0
3939
	shrd	eax,edx,12
3940
	shr	edx,12
3941
	add	eax,1
3942
	adc	edx,0
3943
	shld	edx,eax,12
3944
	shl	eax,12
3945
	ret
3946
close_elf_exe:
31 halyavin 3947
	test	[format_flags],8
157 heavyiron 3948
	jnz	close_elf64_exe
3949
	call	close_elf_segment
3950
	mov	edx,[code_start]
3951
	mov	eax,[number_of_sections]
3952
	mov	byte [edx+1Ch],34h
3953
	mov	[edx+2Ch],ax
3954
	shl	eax,5
3955
	add	eax,edx
3956
	add	eax,34h
3957
	cmp	eax,[symbols_stream]
3958
	je	elf_exe_ok
3959
	or	[next_pass_needed],-1
3960
      elf_exe_ok:
31 halyavin 3961
	ret
157 heavyiron 3962
      close_elf64_exe:
31 halyavin 3963
	call	close_elf64_segment
157 heavyiron 3964
	mov	edx,[code_start]
3965
	mov	eax,[number_of_sections]
3966
	mov	byte [edx+20h],40h
3967
	mov	[edx+38h],ax
3968
	imul	eax,38h
3969
	add	eax,edx
3970
	add	eax,40h
3971
	cmp	eax,[symbols_stream]
3972
	je	elf64_exe_ok
3973
	or	[next_pass_needed],-1
3974
      elf64_exe_ok:
31 halyavin 3975
	ret
157 heavyiron 3976
992 heavyiron 3977
 
3978
	mov	ebx,[code_start]
3979
	mov	dword [ebx],'fas'+27 shl 24
3980
	mov	dword [ebx+4],VERSION_MAJOR + VERSION_MINOR shl 8 + 38h shl 16
3981
	add	ebx,38h
3982
	mov	edi,ebx
3983
	mov	dword [ebx-38h+10h],38h
3984
	mov	dword [ebx-38h+8],0
3985
	mov	esi,[input_file]
3986
	call	copy_asciiz
3987
	cmp	edi,[display_buffer]
3988
	jae	out_of_memory
3989
	mov	eax,edi
3990
	sub	eax,ebx
3991
	mov	[ebx-38h+0Ch],eax
3992
	mov	esi,[output_file]
3993
	call	copy_asciiz
3994
	cmp	edi,[display_buffer]
3995
	jae	out_of_memory
3996
	mov	edx,[symbols_stream]
3997
	mov	ebp,[free_additional_memory]
3998
	mov	[number_of_sections],0
3999
	cmp	[output_format],4
4000
	je	prepare_strings_table
4001
	cmp	[output_format],5
4002
	jne	strings_table_ready
4003
	bt	[format_flags],0
4004
	jc	strings_table_ready
4005
      prepare_strings_table:
4006
	cmp	edx,ebp
4007
	je	strings_table_ready
4008
	mov	al,[edx]
4009
	test	al,al
4010
	jz	prepare_string
4011
	cmp	al,80h
4012
	je	prepare_string
4013
	add	edx,0Ch
4014
	cmp	al,0C0h
4015
	jne	prepare_strings_table
4016
	add	edx,4
4017
	jmp	prepare_strings_table
4018
      prepare_string:
4019
	mov	esi,edi
4020
	sub	esi,ebx
4021
	xchg	esi,[edx+4]
4022
	test	al,al
4023
	jz	prepare_section_string
4024
	or	dword [edx+4],1 shl 31
4025
	add	edx,0Ch
4026
      prepare_external_string:
4027
	mov	ecx,[esi]
4028
	add	esi,4
4029
	rep	movs byte [edi],[esi]
4030
	mov	byte [edi],0
4031
	inc	edi
4032
	cmp	edi,[display_buffer]
4033
	jae	out_of_memory
4034
	jmp	prepare_strings_table
4035
      prepare_section_string:
4036
	mov	ecx,[number_of_sections]
4037
	mov	eax,ecx
4038
	inc	eax
4039
	mov	[number_of_sections],eax
4040
	xchg	eax,[edx+4]
4041
	shl	ecx,2
4042
	add	ecx,[free_additional_memory]
4043
	mov	[ecx],eax
4044
	add	edx,20h
4045
	test	esi,esi
4046
	jz	prepare_default_section_string
4047
	cmp	[output_format],5
4048
	jne	prepare_external_string
4049
	bt	[format_flags],0
4050
	jc	prepare_external_string
4051
	mov	esi,[esi]
4052
	add	esi,[resource_data]
4053
      copy_elf_section_name:
4054
	lods	byte [esi]
4055
	cmp	edi,[display_buffer]
4056
	jae	out_of_memory
4057
	stos	byte [edi]
4058
	test	al,al
4059
	jnz	copy_elf_section_name
4060
	jmp	prepare_strings_table
4061
      prepare_default_section_string:
4062
	mov	eax,'.fla'
4063
	stos	dword [edi]
4064
	mov	ax,'t'
4065
	stos	word [edi]
4066
	cmp	edi,[display_buffer]
4067
	jae	out_of_memory
4068
	jmp	prepare_strings_table
4069
      strings_table_ready:
4070
	mov	edx,[display_buffer]
4071
	mov	ebp,[memory_end]
4072
	sub	ebp,[labels_list]
4073
	add	ebp,edx
4074
      prepare_labels_dump:
4075
	cmp	edx,ebp
4076
	je	labels_dump_ok
4077
	mov	eax,[edx+24]
4078
	test	eax,eax
4079
	jz	label_dump_name_ok
4080
	cmp	eax,[memory_start]
4081
	jb	label_name_outside_source
4082
	cmp	eax,[source_start]
4083
	ja	label_name_outside_source
4084
	sub	eax,[memory_start]
4085
	dec	eax
4086
	mov	[edx+24],eax
4087
	jmp	label_dump_name_ok
4088
      label_name_outside_source:
4089
	mov	esi,eax
4090
	mov	eax,edi
4091
	sub	eax,ebx
4092
	or	eax,1 shl 31
4093
	mov	[edx+24],eax
4094
	movzx	ecx,byte [esi-1]
4095
	lea	eax,[edi+ecx+1]
4096
	cmp	edi,[display_buffer]
4097
	jae	out_of_memory
4098
	rep	movsb
4099
	xor	al,al
4100
	stosb
4101
      label_dump_name_ok:
4102
	mov	eax,[edx+28]
4103
	test	eax,eax
4104
	jz	label_dump_line_ok
4105
	sub	eax,[memory_start]
4106
	mov	[edx+28],eax
4107
      label_dump_line_ok:
4108
	mov	eax,[edx+20]
4109
	test	eax,eax
4110
	jz	base_symbol_for_label_ok
4111
	cmp	eax,[symbols_stream]
4112
	mov	eax,[eax+4]
4113
	jae	base_symbol_for_label_ok
4114
	xor	eax,eax
4115
      base_symbol_for_label_ok:
4116
	mov	[edx+20],eax
4117
	add	edx,LABEL_STRUCTURE_SIZE
1053 heavyiron 4118
	jmp	prepare_labels_dump
992 heavyiron 4119
      labels_dump_ok:
4120
	mov	eax,edi
4121
	sub	eax,ebx
4122
	mov	[ebx-38h+14h],eax
4123
	add	eax,38h
4124
	mov	[ebx-38h+18h],eax
4125
	mov	ecx,[memory_end]
4126
	sub	ecx,[labels_list]
4127
	mov	[ebx-38h+1Ch],ecx
4128
	add	eax,ecx
4129
	mov	[ebx-38h+20h],eax
4130
	mov	ecx,[source_start]
4131
	sub	ecx,[memory_start]
4132
	mov	[ebx-38h+24h],ecx
4133
	add	eax,ecx
4134
	mov	[ebx-38h+28h],eax
4135
	mov	eax,[number_of_sections]
4136
	shl	eax,2
1054 heavyiron 4137
	mov	[ebx-38h+34h],eax
992 heavyiron 4138
	mov	esi,[memory_start]
4139
      prepare_preprocessed_source:
4140
	cmp	esi,[source_start]
4141
	jae	preprocessed_source_ok
4142
	mov	eax,[memory_start]
4143
	mov	edx,[input_file]
4144
	cmp	[esi],edx
4145
	jne	line_not_from_main_input
4146
	mov	[esi],eax
4147
      line_not_from_main_input:
4148
	sub	[esi],eax
4149
	test	byte [esi+7],1 shl 7
4150
	jz	prepare_next_preprocessed_line
4151
	sub	[esi+8],eax
4152
	sub	[esi+12],eax
4153
      prepare_next_preprocessed_line:
4154
	add	esi,16
4155
	call	skip_foreign_line
4156
	jmp	prepare_preprocessed_source
4157
      preprocessed_source_ok:
4158
	mov	esi,[labels_list]
4159
	mov	ebp,edi
4160
      make_lines_dump:
4161
	cmp	esi,[display_buffer]
4162
	je	lines_dump_ok
4163
	mov	eax,[esi-4]
4164
	mov	ecx,[esi-8]
4165
	sub	esi,8
4166
	sub	esi,ecx
4167
	cmp	eax,1
4168
	jne	make_lines_dump
4169
	mov	eax,[esi+4]
4170
	sub	eax,[code_start]
4171
	add	eax,[headers_size]
4172
	cmp	byte [esi+1Ah],0
4173
	je	store_offset
4174
	xor	eax,eax
4175
      store_offset:
4176
	stos	dword [edi]
4177
	mov	eax,[esi]
4178
	sub	eax,[memory_start]
4179
	stos	dword [edi]
4180
	mov	eax,[esi+4]
4181
	xor	edx,edx
4182
	sub	eax,[esi+8]
4183
	sbb	edx,[esi+8+4]
4184
	stos	dword [edi]
4185
	mov	eax,edx
4186
	stos	dword [edi]
4187
	mov	eax,[esi+10h]
4188
	stos	dword [edi]
4189
	mov	eax,[esi+14h]
4190
	test	eax,eax
4191
	jz	base_symbol_for_line_ok
4192
	cmp	eax,[symbols_stream]
4193
	mov	eax,[eax+4]
4194
	jae	base_symbol_for_line_ok
4195
	xor	eax,eax
4196
      base_symbol_for_line_ok:
4197
	stos	dword [edi]
4198
	mov	eax,[esi+18h]
4199
	and	eax,001FFFFh
4200
	stos	dword [edi]
4201
	jmp	make_lines_dump
4202
      lines_dump_ok:
4203
	mov	edx,edi
4204
	mov	eax,[current_offset]
4205
	sub	eax,[code_start]
4206
	add	eax,[headers_size]
4207
	stos	dword [edi]
4208
	mov	ecx,edi
4209
	sub	ecx,ebx
4210
	sub	ecx,[ebx-38h+14h]
4211
	mov	[ebx-38h+2Ch],ecx
4212
	add	ecx,[ebx-38h+28h]
4213
	mov	[ebx-38h+30h],ecx
4214
      find_inexisting_offsets:
4215
	sub	edx,1Ch
4216
	cmp	edx,ebp
4217
	jb	write_symbols
4218
	test	byte [edx+1Ah],1
4219
	jnz	find_inexisting_offsets
4220
	cmp	eax,[edx]
4221
	jb	correct_inexisting_offset
4222
	mov	eax,[edx]
4223
	jmp	find_inexisting_offsets
4224
      correct_inexisting_offset:
4225
	mov	dword [edx],0
4226
	or	byte [edx+1Ah],2
4227
	jmp	find_inexisting_offsets
4228
      write_symbols:
4229
	mov	edx,[symbols_file]
4230
	call	create
4231
	jc	write_failed
4232
	mov	edx,[code_start]
4233
	mov	ecx,[edx+14h]
4234
	add	ecx,38h
4235
	call	write
4236
	mov	edx,[display_buffer]
4237
	mov	ecx,[memory_end]
4238
	sub	ecx,[labels_list]
4239
	call	write
4240
	mov	edx,[memory_start]
4241
	mov	ecx,[source_start]
4242
	sub	ecx,edx
4243
	call	write
4244
	mov	edx,ebp
4245
	mov	ecx,edi
4246
	sub	ecx,edx
4247
	call	write
4248
	mov	edx,[free_additional_memory]
4249
	mov	ecx,[number_of_sections]
4250
	shl	ecx,2
4251
	call	write
4252
	call	close
4253
	ret
4254