Subversion Repositories Kolibri OS

Rev

Rev 1054 | 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	eax,eax
1115 heavyiron 484
	rep	stos byte [edi]
157 heavyiron 485
	mov	dword [org_origin],edi
486
	mov	dword [org_origin+4],eax
1115 heavyiron 487
	mov	[org_registers],eax
488
	mov	[org_start],edi
157 heavyiron 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	byte [edx+4],1
1115 heavyiron 671
	mov	word [edx+2],ax
157 heavyiron 672
	mov	byte [edx+8],4
1115 heavyiron 673
	mov	byte [edx+0Ah],10h
674
	mov	word [edx+0Ch],0FFFFh
157 heavyiron 675
	mov	word [edx+10h],cx
676
	mov	word [edx+3Ch],ax
677
	mov	byte [edx+18h],40h
1115 heavyiron 678
	lea	edi,[edx+40h]
157 heavyiron 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	byte [esi+8],4
1115 heavyiron 807
	mov	ax,0FFFFh
157 heavyiron 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	byte [esi+18h],40h
1115 heavyiron 816
	mov	eax,[display_buffer]
157 heavyiron 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	byte [edx+38h+1],10h	; section alignment
1115 heavyiron 980
	mov	byte [edx+3Ch+1],2	; file alignment
981
	mov	byte [edx+40h],1	; OS version
982
	mov	eax,[subsystem_version]
157 heavyiron 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	byte [edx+14h],0E0h	; size of optional header
1115 heavyiron 996
	mov	dword [edx+16h],10B010Eh; flags and magic value
157 heavyiron 997
	mov	eax,[image_base]
998
	mov	[edx+34h],eax
1115 heavyiron 999
	mov	byte [edx+60h+1],10h	; stack reserve
1000
	mov	byte [edx+64h+1],10h	; stack commit
1001
	mov	byte [edx+68h+2],1	; heap reserve
1002
	mov	byte [edx+74h],16	; number of directories
1003
	jmp	pe_header_ok
157 heavyiron 1004
      init_peplus_specific:
992 heavyiron 1005
	mov	byte [edx+14h],0F0h	; size of optional header
1115 heavyiron 1006
	mov	dword [edx+16h],20B002Eh; flags and magic value
157 heavyiron 1007
	mov	eax,[image_base]
1008
	mov	[edx+30h],eax
1115 heavyiron 1009
	mov	eax,[image_base_high]
157 heavyiron 1010
	mov	[edx+34h],eax
1115 heavyiron 1011
	mov	byte [edx+60h+1],10h	; stack reserve
1012
	mov	byte [edx+68h+1],10h	; stack commit
1013
	mov	byte [edx+70h+2],1	; heap reserve
1014
	mov	byte [edx+84h],16	; number of directories
1015
      pe_header_ok:
31 halyavin 1016
	bsf	ecx,[edx+3Ch]
157 heavyiron 1017
	imul	ebx,[number_of_sections],28h
1018
	or	ebx,ebx
1019
	jnz	reserve_space_for_section_headers
1020
	mov	ebx,28h
1021
      reserve_space_for_section_headers:
31 halyavin 1022
	add	ebx,ebp
157 heavyiron 1023
	dec	ebx
1024
	shr	ebx,cl
1025
	inc	ebx
1026
	shl	ebx,cl
1027
	sub	ebx,ebp
1028
	mov	ecx,ebx
1029
	mov	eax,[display_buffer]
1030
	sub	eax,ecx
1031
	cmp	edi,eax
1032
	jae	out_of_memory
1033
	shr	ecx,2
1034
	xor	eax,eax
1035
	rep	stos dword [edi]
1036
	mov	eax,edi
1037
	sub	eax,[code_start]
1038
	add	eax,[stub_size]
1039
	mov	[edx+54h],eax		; size of headers
1040
	mov	ecx,[edx+38h]
1041
	dec	ecx
1042
	add	eax,ecx
1043
	not	ecx
1044
	and	eax,ecx
1045
	bt	[format_flags],8
1046
	jc	pe_entry_init_ok
1047
	mov	[edx+28h],eax		; entry point rva
1048
      pe_entry_init_ok:
31 halyavin 1049
	mov	[number_of_sections],0
157 heavyiron 1050
	movzx	ebx,word [edx+14h]
1051
	lea	ebx,[edx+18h+ebx]
1052
	mov	[current_section],ebx
1053
	mov	dword [ebx],'.fla'
1054
	mov	dword [ebx+4],'t'
1055
	mov	[ebx+14h],edi
1056
	mov	[ebx+0Ch],eax
1057
	mov	dword [ebx+24h],0E0000060h
1058
	xor	ecx,ecx
1059
	not	eax
1060
	not	ecx
1061
	add	eax,1
1062
	adc	ecx,0
1063
	add	eax,edi
1064
	adc	ecx,0
1065
	test	[format_flags],8
1066
	jnz	peplus_org
992 heavyiron 1067
	sub	eax,[edx+34h]
157 heavyiron 1068
	sbb	ecx,0
1069
	mov	bl,2
1070
	mov	[code_type],32
1071
	jmp	pe_org_ok
1072
      peplus_org:
992 heavyiron 1073
	sub	eax,[edx+30h]
157 heavyiron 1074
	sbb	ecx,[edx+34h]
1075
	mov	bl,4
1076
	mov	[code_type],64
1077
      pe_org_ok:
31 halyavin 1078
	bt	[resolver_flags],0
157 heavyiron 1079
	jc	pe_labels_type_ok
1080
	xor	bl,bl
1081
      pe_labels_type_ok:
109 heavyiron 1082
	mov	[labels_type],bl
157 heavyiron 1083
	mov	dword [org_origin],eax
1084
	mov	dword [org_origin+4],ecx
1085
	mov	[org_registers],0
1086
	mov	[org_start],edi
1087
	bt	[format_flags],8
1088
	jnc	dll_flag_ok
1089
	or	byte [edx+16h+1],20h
1115 heavyiron 1090
      dll_flag_ok:
31 halyavin 1091
	bt	[format_flags],9
157 heavyiron 1092
	jnc	wdm_flag_ok
1093
	or	byte [edx+5Eh+1],20h
1115 heavyiron 1094
      wdm_flag_ok:
31 halyavin 1095
	jmp	format_defined
607 heavyiron 1096
pe_section:
31 halyavin 1097
	call	close_pe_section
157 heavyiron 1098
	bts	[format_flags],5
1099
	lea	ecx,[ebx+28h]
1100
	add	edx,[edx+54h]
1101
	sub	edx,[stub_size]
1102
	cmp	ecx,edx
1103
	jbe	new_section
1104
	lea	ebx,[edx-28h]
1105
	or	[next_pass_needed],-1
1106
	push	edi
1107
	mov	edi,ebx
1108
	mov	ecx,28h shr 4
1109
	xor	eax,eax
1110
	rep	stos dword [edi]
1111
	pop	edi
1112
      new_section:
31 halyavin 1113
	mov	[ebx+0Ch],eax
157 heavyiron 1114
	lods	word [esi]
1115
	cmp	ax,'('
1116
	jne	invalid_argument
1117
	lea	edx,[esi+4]
1118
	mov	ecx,[esi]
1119
	lea	esi,[esi+4+ecx+1]
1120
	cmp	ecx,8
1121
	ja	name_too_long
1122
	xor	eax,eax
1123
	mov	[ebx],eax
1124
	mov	[ebx+4],eax
1125
	push	esi edi
1126
	mov	edi,ebx
1127
	mov	esi,edx
1128
	rep	movs byte [edi],[esi]
1129
	pop	edi esi
1130
	mov	dword [ebx+24h],0
1131
	mov	[ebx+14h],edi
1132
	mov	edx,[code_start]
1133
	mov	eax,edi
1134
	xor	ecx,ecx
1135
	sub	eax,[ebx+0Ch]
1136
	sbb	ecx,0
1137
	test	[format_flags],8
1138
	jnz	peplus_section_org
992 heavyiron 1139
	sub	eax,[edx+34h]
157 heavyiron 1140
	sbb	ecx,0
1141
	mov	[labels_type],2
1142
	mov	[code_type],32
1143
	bt	[resolver_flags],0
1144
	jc	pe_section_org_ok
1145
	mov	[labels_type],0
1146
	jmp	pe_section_org_ok
1147
      peplus_section_org:
992 heavyiron 1148
	sub	eax,[edx+30h]
157 heavyiron 1149
	sbb	ecx,[edx+34h]
1150
	mov	[labels_type],4
1151
	mov	[code_type],64
1152
	bt	[resolver_flags],0
1153
	jc	pe_section_org_ok
1154
	mov	[labels_type],0
1155
      pe_section_org_ok:
31 halyavin 1156
	mov	dword [org_origin],eax
157 heavyiron 1157
	mov	dword [org_origin+4],ecx
1158
	mov	[org_registers],0
1159
	mov	[org_start],edi
1160
      get_section_flags:
31 halyavin 1161
	lods	byte [esi]
157 heavyiron 1162
	cmp	al,1Ah
1163
	je	set_directory
1164
	cmp	al,19h
1165
	je	section_flag
1166
	dec	esi
1167
	jmp	instruction_assembled
1168
      set_directory:
31 halyavin 1169
	movzx	eax,byte [esi]
157 heavyiron 1170
	inc	esi
1171
	mov	ecx,ebx
1172
	test	[format_flags],8
1173
	jnz	peplus_directory
992 heavyiron 1174
	xchg	ecx,[edx+78h+eax*8]
157 heavyiron 1175
	mov	dword [edx+78h+eax*8+4],-1
1176
	jmp	pe_directory_set
1177
      peplus_directory:
992 heavyiron 1178
	xchg	ecx,[edx+88h+eax*8]
157 heavyiron 1179
	mov	dword [edx+88h+eax*8+4],-1
1180
      pe_directory_set:
31 halyavin 1181
	or	ecx,ecx
157 heavyiron 1182
	jnz	data_already_defined
1183
	push	ebx edx
1184
	call	generate_pe_data
1185
	pop	edx ebx
1186
	jmp	get_section_flags
1187
      section_flag:
31 halyavin 1188
	lods	byte [esi]
157 heavyiron 1189
	cmp	al,9
1190
	je	invalid_argument
1191
	cmp	al,11
1192
	je	invalid_argument
1193
	mov	cl,al
1194
	mov	eax,1
1195
	shl	eax,cl
1196
	test	dword [ebx+24h],eax
1197
	jnz	setting_already_specified
1198
	or	dword [ebx+24h],eax
1199
	jmp	get_section_flags
1200
      close_pe_section:
31 halyavin 1201
	mov	ebx,[current_section]
157 heavyiron 1202
	mov	edx,[code_start]
1203
	mov	eax,edi
1204
	sub	eax,[ebx+14h]
1205
	jnz	finish_section
1206
	bt	[format_flags],5
1207
	jc	finish_section
1208
	mov	eax,[ebx+0Ch]
1209
	ret
1210
      finish_section:
31 halyavin 1211
	mov	[ebx+8],eax
157 heavyiron 1212
	cmp	edi,[undefined_data_end]
1213
	jne	align_section
1214
	cmp	dword [edx+38h],1000h
1215
	jb	align_section
1216
	mov	edi,[undefined_data_start]
1217
      align_section:
31 halyavin 1218
	mov	[undefined_data_end],0
157 heavyiron 1219
	mov	ebp,edi
1220
	sub	ebp,[ebx+14h]
1221
	mov	ecx,[edx+3Ch]
1222
	dec	ecx
1223
	lea	eax,[ebp+ecx]
1224
	not	ecx
1225
	and	eax,ecx
1226
	mov	[ebx+10h],eax
1227
	sub	eax,ebp
1228
	mov	ecx,eax
1229
	xor	al,al
1230
	rep	stos byte [edi]
1231
	mov	eax,[code_start]
1232
	sub	eax,[stub_size]
1233
	sub	[ebx+14h],eax
1234
	mov	ecx,[ebx+10h]
992 heavyiron 1235
	test	byte [ebx+24h],20h
1236
	jz	pe_code_sum_ok
1237
	add	[edx+1Ch],ecx
1238
	cmp	dword [edx+2Ch],0
1239
	jne	pe_code_sum_ok
1240
	mov	eax,[ebx+0Ch]
1241
	mov	[edx+2Ch],eax
1242
      pe_code_sum_ok:
1243
	test	byte [ebx+24h],40h
1244
	jz	pe_data_sum_ok
1245
	add	[edx+20h],ecx
1246
	test	[format_flags],8
1247
	jnz	pe_data_sum_ok
1248
	cmp	dword [edx+30h],0
1249
	jne	pe_data_sum_ok
1250
	mov	eax,[ebx+0Ch]
1251
	mov	[edx+30h],eax
1252
      pe_data_sum_ok:
1253
	mov	eax,[ebx+8]
157 heavyiron 1254
	or	eax,eax
1255
	jz	udata_ok
1256
	cmp	dword [ebx+10h],0
1257
	jne	udata_ok
1258
	or	byte [ebx+24h],80h
1259
	add	[edx+24h],ecx
992 heavyiron 1260
      udata_ok:
31 halyavin 1261
	mov	ecx,[edx+38h]
157 heavyiron 1262
	dec	ecx
1263
	add	eax,ecx
1264
	not	ecx
1265
	and	eax,ecx
1266
	add	eax,[ebx+0Ch]
1267
	add	ebx,28h
1268
	mov	[current_section],ebx
1269
	inc	word [number_of_sections]
1270
	jz	format_limitations_exceeded
1271
	ret
1272
data_directive:
31 halyavin 1273
	cmp	[output_format],3
157 heavyiron 1274
	jne	illegal_instruction
1275
	lods	byte [esi]
1276
	cmp	al,1Ah
1277
	je	predefined_data_type
1278
	cmp	al,'('
1279
	jne	invalid_argument
1280
	call	get_byte_value
1281
	cmp	al,16
1282
	jb	data_type_ok
1283
	jmp	invalid_value
1284
      predefined_data_type:
31 halyavin 1285
	movzx	eax,byte [esi]
157 heavyiron 1286
	inc	esi
1287
      data_type_ok:
31 halyavin 1288
	mov	ebx,[current_section]
157 heavyiron 1289
	mov	ecx,edi
1290
	sub	ecx,[ebx+14h]
1291
	add	ecx,[ebx+0Ch]
1292
	mov	edx,[code_start]
1293
	test	[format_flags],8
1294
	jnz	peplus_data
992 heavyiron 1295
	xchg	ecx,[edx+78h+eax*8]
157 heavyiron 1296
	jmp	init_pe_data
1297
      peplus_data:
992 heavyiron 1298
	xchg	ecx,[edx+88h+eax*8]
157 heavyiron 1299
      init_pe_data:
31 halyavin 1300
	or	ecx,ecx
157 heavyiron 1301
	jnz	data_already_defined
1302
	call	allocate_structure_data
1303
	mov	word [ebx],data_directive-assembler
1304
	mov	[ebx+2],al
1305
	mov	edx,[current_line]
1306
	mov	[ebx+4],edx
1307
	call	generate_pe_data
1308
	jmp	instruction_assembled
1309
      end_data:
31 halyavin 1310
	cmp	[output_format],3
157 heavyiron 1311
	jne	illegal_instruction
1312
	call	find_structure_data
1313
	jc	unexpected_instruction
1314
	movzx	eax,byte [ebx+2]
1315
	mov	edx,[current_section]
1316
	mov	ecx,edi
1317
	sub	ecx,[edx+14h]
1318
	add	ecx,[edx+0Ch]
1319
	mov	edx,[code_start]
1320
	test	[format_flags],8
1321
	jnz	end_peplus_data
992 heavyiron 1322
	sub	ecx,[edx+78h+eax*8]
157 heavyiron 1323
	mov	[edx+78h+eax*8+4],ecx
1324
	jmp	remove_structure_data
1325
      end_peplus_data:
992 heavyiron 1326
	sub	ecx,[edx+88h+eax*8]
157 heavyiron 1327
	mov	[edx+88h+eax*8+4],ecx
1328
	jmp	remove_structure_data
1329
pe_entry:
31 halyavin 1330
	lods	byte [esi]
157 heavyiron 1331
	cmp	al,'('
1332
	jne	invalid_argument
1333
	cmp	byte [esi],'.'
1334
	je	invalid_value
1335
	test	[format_flags],8
1336
	jnz	peplus_entry
992 heavyiron 1337
	call	get_dword_value
157 heavyiron 1338
	mov	bl,2
1339
	bt	[resolver_flags],0
1340
	jc	check_pe_entry_label_type
1341
	xor	bl,bl
1342
      check_pe_entry_label_type:
109 heavyiron 1343
	cmp	[value_type],bl
157 heavyiron 1344
	je	pe_entry_ok
1345
	cmp	[error_line],0
1346
	jne	pe_entry_ok
1347
	mov	edx,[current_line]
1348
	mov	[error_line],edx
1349
	mov	[error],invalid_address
1350
      pe_entry_ok:
31 halyavin 1351
	mov	edx,[code_start]
157 heavyiron 1352
	sub	eax,[edx+34h]
1353
	mov	[edx+28h],eax
1354
	jmp	instruction_assembled
1355
      peplus_entry:
992 heavyiron 1356
	call	get_qword_value
157 heavyiron 1357
	mov	bl,4
1358
	bt	[resolver_flags],0
1359
	jc	check_peplus_entry_label_type
992 heavyiron 1360
	xor	bl,bl
157 heavyiron 1361
      check_peplus_entry_label_type:
992 heavyiron 1362
	cmp	[value_type],bl
157 heavyiron 1363
	je	peplus_entry_type_ok
992 heavyiron 1364
	cmp	[error_line],0
157 heavyiron 1365
	jne	peplus_entry_type_ok
992 heavyiron 1366
	mov	edx,[current_line]
157 heavyiron 1367
	mov	[error_line],edx
1368
	mov	[error],invalid_address
1369
      peplus_entry_type_ok:
992 heavyiron 1370
	mov	ecx,[code_start]
157 heavyiron 1371
	sub	eax,[ecx+30h]
1372
	sbb	edx,[ecx+34h]
1373
	jz	peplus_entry_range_ok
992 heavyiron 1374
	mov	edx,[current_line]
157 heavyiron 1375
	mov	[error_line],edx
1376
	mov	[error],value_out_of_range
1377
      peplus_entry_range_ok:
992 heavyiron 1378
	mov	[ecx+28h],eax
157 heavyiron 1379
	jmp	instruction_assembled
1380
pe_stack:
31 halyavin 1381
	lods	byte [esi]
157 heavyiron 1382
	cmp	al,'('
1383
	jne	invalid_argument
1384
	cmp	byte [esi],'.'
1385
	je	invalid_value
1386
	test	[format_flags],8
1387
	jnz	peplus_stack
992 heavyiron 1388
	call	get_dword_value
157 heavyiron 1389
	cmp	[value_type],0
1390
	jne	invalid_use_of_symbol
1391
	mov	edx,[code_start]
1392
	mov	[edx+60h],eax
1393
	cmp	byte [esi],','
1394
	jne	default_stack_commit
1395
	lods	byte [esi]
1396
	lods	byte [esi]
1397
	cmp	al,'('
1398
	jne	invalid_argument
1399
	cmp	byte [esi],'.'
1400
	je	invalid_value
1401
	call	get_dword_value
1402
	cmp	[value_type],0
1403
	jne	invalid_use_of_symbol
1404
	mov	edx,[code_start]
1405
	mov	[edx+64h],eax
1406
	cmp	eax,[edx+60h]
1407
	ja	value_out_of_range
1408
	jmp	instruction_assembled
1409
      default_stack_commit:
31 halyavin 1410
	mov	dword [edx+64h],1000h
157 heavyiron 1411
	mov	eax,[edx+60h]
1412
	cmp	eax,1000h
1413
	ja	instruction_assembled
1414
	mov	dword [edx+64h],eax
1415
	jmp	instruction_assembled
1416
      peplus_stack:
992 heavyiron 1417
	call	get_qword_value
157 heavyiron 1418
	cmp	[value_type],0
1419
	jne	invalid_use_of_symbol
1420
	mov	ecx,[code_start]
1421
	mov	[ecx+60h],eax
1422
	mov	[ecx+64h],edx
1423
	cmp	byte [esi],','
1424
	jne	default_peplus_stack_commit
992 heavyiron 1425
	lods	byte [esi]
157 heavyiron 1426
	lods	byte [esi]
1427
	cmp	al,'('
1428
	jne	invalid_argument
1429
	cmp	byte [esi],'.'
1430
	je	invalid_value
1431
	call	get_qword_value
1432
	cmp	[value_type],0
1433
	jne	invalid_use_of_symbol
1434
	mov	ecx,[code_start]
1435
	mov	[ecx+68h],eax
1436
	mov	[ecx+6Ch],edx
1437
	cmp	edx,[ecx+64h]
1438
	ja	value_out_of_range
1439
	jb	instruction_assembled
1440
	cmp	eax,[ecx+60h]
607 heavyiron 1441
	ja	value_out_of_range
157 heavyiron 1442
	jmp	instruction_assembled
1443
      default_peplus_stack_commit:
992 heavyiron 1444
	mov	dword [ecx+68h],1000h
607 heavyiron 1445
	cmp	dword [ecx+64h],0
1446
	jne	instruction_assembled
157 heavyiron 1447
	mov	eax,[ecx+60h]
607 heavyiron 1448
	cmp	eax,1000h
157 heavyiron 1449
	ja	instruction_assembled
1450
	mov	dword [ecx+68h],eax
607 heavyiron 1451
	jmp	instruction_assembled
157 heavyiron 1452
pe_heap:
31 halyavin 1453
	lods	byte [esi]
157 heavyiron 1454
	cmp	al,'('
1455
	jne	invalid_argument
1456
	cmp	byte [esi],'.'
1457
	je	invalid_value
1458
	test	[format_flags],8
1459
	jnz	peplus_heap
992 heavyiron 1460
	call	get_dword_value
157 heavyiron 1461
	cmp	[value_type],0
1462
	jne	invalid_use_of_symbol
1463
	mov	edx,[code_start]
1464
	mov	[edx+68h],eax
1465
	cmp	byte [esi],','
1466
	jne	instruction_assembled
1467
	lods	byte [esi]
1468
	lods	byte [esi]
1469
	cmp	al,'('
1470
	jne	invalid_argument
1471
	cmp	byte [esi],'.'
1472
	je	invalid_value
1473
	call	get_dword_value
1474
	cmp	[value_type],0
1475
	jne	invalid_use_of_symbol
1476
	mov	edx,[code_start]
1477
	mov	[edx+6Ch],eax
1478
	cmp	eax,[edx+68h]
1479
	ja	value_out_of_range
1480
	jmp	instruction_assembled
1481
      peplus_heap:
992 heavyiron 1482
	call	get_qword_value
157 heavyiron 1483
	cmp	[value_type],0
1484
	jne	invalid_use_of_symbol
1485
	mov	ecx,[code_start]
1486
	mov	[ecx+70h],eax
1487
	mov	[ecx+74h],edx
1488
	cmp	byte [esi],','
1489
	jne	instruction_assembled
1490
	lods	byte [esi]
1491
	lods	byte [esi]
1492
	cmp	al,'('
1493
	jne	invalid_argument
1494
	cmp	byte [esi],'.'
1495
	je	invalid_value
1496
	call	get_qword_value
1497
	cmp	[value_type],0
1498
	jne	invalid_use_of_symbol
1499
	mov	ecx,[code_start]
1500
	mov	[ecx+78h],eax
1501
	mov	[ecx+7Ch],edx
1502
	cmp	edx,[ecx+74h]
1503
	ja	value_out_of_range
1504
	jb	instruction_assembled
1505
	cmp	eax,[edx+70h]
1506
	ja	value_out_of_range
1507
	jmp	instruction_assembled
1508
mark_pe_relocation:
31 halyavin 1509
	push	eax ebx
157 heavyiron 1510
	test	[format_flags],8
1511
	jz	check_pe32_relocation_type
370 heavyiron 1512
	cmp	[value_type],4
1513
	je	pe_relocation_type_ok
157 heavyiron 1514
      check_pe32_relocation_type:
370 heavyiron 1515
	cmp	[value_type],2
1516
	je	pe_relocation_type_ok
1517
	cmp	[error_line],0
157 heavyiron 1518
	jne	pe_relocation_type_ok
1519
	mov	eax,[current_line]
1520
	mov	[error_line],eax
1521
	mov	[error],invalid_use_of_symbol
1522
      pe_relocation_type_ok:
109 heavyiron 1523
	mov	ebx,[current_section]
157 heavyiron 1524
	mov	eax,edi
1525
	sub	eax,[ebx+14h]
1526
	add	eax,[ebx+0Ch]
1527
	mov	ebx,[free_additional_memory]
1528
	inc	[number_of_relocations]
1529
	jz	invalid_use_of_symbol
1530
	add	ebx,5
370 heavyiron 1531
	cmp	ebx,[structures_buffer]
157 heavyiron 1532
	jae	out_of_memory
1533
	mov	[free_additional_memory],ebx
1534
	mov	[ebx-5],eax
370 heavyiron 1535
	cmp	[value_type],2
1536
	je	fixup_32bit
1537
	mov	byte [ebx-1],0Ah
1538
	jmp	fixup_ok
1539
      fixup_32bit:
1540
	mov	byte [ebx-1],3
1541
      fixup_ok:
1542
	pop	ebx eax
157 heavyiron 1543
	ret
1544
generate_pe_data:
31 halyavin 1545
	cmp	al,2
157 heavyiron 1546
	je	make_pe_resource
1547
	cmp	al,5
1548
	je	make_pe_fixups
1549
	ret
1550
make_pe_fixups:
370 heavyiron 1551
	bts	[resolver_flags],0
1552
	jc	pe_relocatable_ok
1553
	or	[next_pass_needed],-1
1554
     pe_relocatable_ok:
1555
	push	esi
1556
	mov	ecx,[number_of_relocations]
1557
	mov	esi,[free_additional_memory]
1558
	lea	eax,[ecx*5]
1559
	sub	esi,eax
1560
	mov	[free_additional_memory],esi
1561
	or	[number_of_relocations],-1
1562
	xor	edx,edx
1563
	mov	ebp,edi
1564
      make_fixups:
1565
	cmp	[esi],edx
1566
	jb	store_fixup
1567
	mov	eax,edi
1568
	sub	eax,ebp
1569
	test	eax,11b
1570
	jz	fixups_block
1571
	xor	ax,ax
1572
	stos	word [edi]
1573
	add	dword [ebx],2
1574
      fixups_block:
1575
	mov	eax,edx
1576
	add	edx,1000h
1577
	cmp	[esi],edx
1578
	jae	fixups_block
1579
	stos	dword [edi]
1580
	mov	ebx,edi
1581
	mov	eax,8
1582
	stos	dword [edi]
1583
      store_fixup:
1584
	jecxz	fixups_done
1585
	add	dword [ebx],2
1586
	mov	ah,[esi+1]
607 heavyiron 1587
	and	ah,0Fh
1588
	mov	al,[esi+4]
1589
	shl	al,4
1590
	or	ah,al
1591
	mov	al,[esi]
1592
	stos	word [edi]
370 heavyiron 1593
	add	esi,5
1594
	loop	make_fixups
1595
      fixups_done:
1596
	pop	esi
1597
	ret
1598
make_pe_resource:
31 halyavin 1599
	cmp	byte [esi],82h
157 heavyiron 1600
	jne	resource_done
1601
	inc	esi
1602
	lods	word [esi]
1603
	cmp	ax,'('
1604
	jne	invalid_argument
1605
	lods	dword [esi]
1606
	mov	edx,esi
1607
	lea	esi,[esi+eax+1]
1608
	cmp	[next_pass_needed],0
1609
	je	resource_from_file
1610
	cmp	[current_pass],0
1611
	jne	reserve_space_for_resource
1612
	mov	[resource_size],0
1613
      reserve_space_for_resource:
31 halyavin 1614
	add	edi,[resource_size]
157 heavyiron 1615
	cmp	edi,[display_buffer]
1616
	ja	out_of_memory
1617
	jmp	resource_done
1618
      resource_from_file:
31 halyavin 1619
	push	esi
157 heavyiron 1620
	mov	esi,edx
1621
	call	open_binary_file
1622
	push	ebx
1623
	mov	esi,[free_additional_memory]
1624
	lea	eax,[esi+20h]
1625
	cmp	eax,[structures_buffer]
1626
	ja	out_of_memory
1627
	mov	edx,esi
1628
	mov	ecx,20h
1629
	call	read
1630
	jc	invalid_file_format
1631
	xor	eax,eax
1632
	cmp	[esi],eax
1633
	jne	invalid_file_format
1634
	mov	ax,0FFFFh
1635
	cmp	[esi+8],eax
1636
	jne	invalid_file_format
1637
	cmp	[esi+12],eax
1638
	jne	invalid_file_format
1639
	mov	eax,20h
1640
	cmp	[esi+4],eax
1641
	jne	invalid_file_format
1642
      read_resource_headers:
31 halyavin 1643
	test	eax,11b
157 heavyiron 1644
	jz	resource_file_alignment_ok
1645
	mov	edx,4
1646
	and	eax,11b
1647
	sub	edx,eax
1648
	mov	al,1
1649
	call	lseek
1650
      resource_file_alignment_ok:
31 halyavin 1651
	mov	[esi],eax
157 heavyiron 1652
	lea	edx,[esi+12]
1653
	mov	ecx,8
1654
	call	read
1655
	jc	resource_headers_ok
1656
	mov	ecx,[esi+16]
1657
	add	[esi],ecx
1658
	lea	edx,[esi+20]
1659
	sub	ecx,8
1660
	mov	[esi+16],ecx
1661
	lea	eax,[edx+ecx]
1662
	cmp	eax,[structures_buffer]
1663
	ja	out_of_memory
1664
	call	read
1665
	jc	invalid_file_format
1666
	mov	edx,[esi]
1667
	add	edx,[esi+12]
1668
	mov	eax,[esi+16]
1669
	lea	ecx,[esi+20]
1670
	lea	esi,[ecx+eax]
1671
	add	ecx,2
1672
	cmp	word [ecx-2],0FFFFh
1673
	je	resource_header_type_ok
1674
      check_resource_header_type:
31 halyavin 1675
	cmp	ecx,esi
157 heavyiron 1676
	jae	invalid_file_format
1677
	cmp	word [ecx],0
1678
	je	resource_header_type_ok
1679
	add	ecx,2
1680
	jmp	check_resource_header_type
1681
      resource_header_type_ok:
31 halyavin 1682
	add	ecx,2
157 heavyiron 1683
	cmp	word [ecx],0FFFFh
1684
	je	resource_header_name_ok
1685
      check_resource_header_name:
31 halyavin 1686
	cmp	ecx,esi
157 heavyiron 1687
	jae	invalid_file_format
1688
	cmp	word [ecx],0
1689
	je	resource_header_name_ok
1690
	add	ecx,2
1691
	jmp	check_resource_header_name
1692
      resource_header_name_ok:
31 halyavin 1693
	xor	al,al
157 heavyiron 1694
	call	lseek
1695
	jmp	read_resource_headers
1696
      resource_headers_ok:
31 halyavin 1697
	xor	eax,eax
157 heavyiron 1698
	mov	[esi],eax
1699
	mov	[resource_data],edi
1700
	lea	eax,[edi+16]
1701
	cmp	eax,[display_buffer]
1702
	jae	out_of_memory
1703
	xor	eax,eax
1704
	stos	dword [edi]
1705
	call	make_timestamp
1706
	stos	dword [edi]
1707
	xor	eax,eax
1708
	stos	dword [edi]
1709
	stos	dword [edi]
1710
	xor	ebx,ebx
1711
      make_type_name_directory:
31 halyavin 1712
	mov	esi,[free_additional_memory]
157 heavyiron 1713
	xor	edx,edx
1714
      find_type_name:
31 halyavin 1715
	cmp	dword [esi],0
157 heavyiron 1716
	je	type_name_ok
1717
	add	esi,20
1718
	cmp	word [esi],0FFFFh
1719
	je	check_next_type_name
1720
	or	ebx,ebx
1721
	jz	check_this_type_name
1722
	xor	ecx,ecx
1723
      compare_with_previous_type_name:
31 halyavin 1724
	mov	ax,[esi+ecx]
157 heavyiron 1725
	cmp	ax,[ebx+ecx]
1726
	ja	check_this_type_name
1727
	jb	check_next_type_name
1728
	add	ecx,2
1729
	mov	ax,[esi+ecx]
1730
	or	ax,[ebx+ecx]
1731
	jnz	compare_with_previous_type_name
1732
	jmp	check_next_type_name
1733
      check_this_type_name:
31 halyavin 1734
	or	edx,edx
157 heavyiron 1735
	jz	type_name_found
1736
	xor	ecx,ecx
1737
      compare_with_current_type_name:
31 halyavin 1738
	mov	ax,[esi+ecx]
157 heavyiron 1739
	cmp	ax,[edx+ecx]
1740
	ja	check_next_type_name
1741
	jb	type_name_found
1742
	add	ecx,2
1743
	mov	ax,[esi+ecx]
1744
	or	ax,[edx+ecx]
1745
	jnz	compare_with_current_type_name
1746
	jmp	same_type_name
1747
      type_name_found:
31 halyavin 1748
	mov	edx,esi
157 heavyiron 1749
      same_type_name:
31 halyavin 1750
	mov	[esi-16],edi
157 heavyiron 1751
      check_next_type_name:
31 halyavin 1752
	mov	eax,[esi-4]
157 heavyiron 1753
	add	esi,eax
1754
	jmp	find_type_name
1755
      type_name_ok:
31 halyavin 1756
	or	edx,edx
157 heavyiron 1757
	jz	type_name_directory_done
1758
	mov	ebx,edx
1759
      make_type_name_entry:
31 halyavin 1760
	mov	eax,[resource_data]
157 heavyiron 1761
	inc	word [eax+12]
1762
	lea	eax,[edi+8]
1763
	cmp	eax,[display_buffer]
1764
	jae	out_of_memory
1765
	mov	eax,ebx
1766
	stos	dword [edi]
1767
	xor	eax,eax
1768
	stos	dword [edi]
1769
	jmp	make_type_name_directory
1770
      type_name_directory_done:
31 halyavin 1771
	mov	ebx,-1
157 heavyiron 1772
      make_type_id_directory:
31 halyavin 1773
	mov	esi,[free_additional_memory]
157 heavyiron 1774
	mov	edx,10000h
1775
      find_type_id:
31 halyavin 1776
	cmp	dword [esi],0
157 heavyiron 1777
	je	type_id_ok
1778
	add	esi,20
1779
	cmp	word [esi],0FFFFh
1780
	jne	check_next_type_id
1781
	movzx	eax,word [esi+2]
1782
	cmp	eax,ebx
1783
	jle	check_next_type_id
1784
	cmp	eax,edx
1785
	jg	check_next_type_id
1786
	mov	edx,eax
1787
	mov	[esi-16],edi
1788
      check_next_type_id:
31 halyavin 1789
	mov	eax,[esi-4]
157 heavyiron 1790
	add	esi,eax
1791
	jmp	find_type_id
1792
      type_id_ok:
31 halyavin 1793
	cmp	edx,10000h
157 heavyiron 1794
	je	type_id_directory_done
1795
	mov	ebx,edx
1796
      make_type_id_entry:
31 halyavin 1797
	mov	eax,[resource_data]
157 heavyiron 1798
	inc	word [eax+14]
1799
	lea	eax,[edi+8]
1800
	cmp	eax,[display_buffer]
1801
	jae	out_of_memory
1802
	mov	eax,ebx
1803
	stos	dword [edi]
1804
	xor	eax,eax
1805
	stos	dword [edi]
1806
	jmp	make_type_id_directory
1807
      type_id_directory_done:
31 halyavin 1808
	mov	esi,[resource_data]
157 heavyiron 1809
	add	esi,10h
1810
	mov	ecx,[esi-4]
1811
	or	cx,cx
1812
	jz	resource_directories_ok
1813
      make_resource_directories:
31 halyavin 1814
	push	ecx
157 heavyiron 1815
	push	edi
1816
	mov	edx,edi
1817
	sub	edx,[resource_data]
1818
	bts	edx,31
1819
	mov	[esi+4],edx
1820
	lea	eax,[edi+16]
1821
	cmp	eax,[display_buffer]
1822
	jae	out_of_memory
1823
	xor	eax,eax
1824
	stos	dword [edi]
1825
	call	make_timestamp
1826
	stos	dword [edi]
1827
	xor	eax,eax
1828
	stos	dword [edi]
1829
	stos	dword [edi]
1830
	mov	ebp,esi
1831
	xor	ebx,ebx
1832
      make_resource_name_directory:
31 halyavin 1833
	mov	esi,[free_additional_memory]
157 heavyiron 1834
	xor	edx,edx
1835
      find_resource_name:
31 halyavin 1836
	cmp	dword [esi],0
157 heavyiron 1837
	je	resource_name_ok
1838
	push	esi
1839
	cmp	[esi+4],ebp
1840
	jne	check_next_resource_name
1841
	add	esi,20
1842
	call	skip_resource_name
1843
	cmp	word [esi],0FFFFh
1844
	je	check_next_resource_name
1845
	or	ebx,ebx
1846
	jz	check_this_resource_name
1847
	xor	ecx,ecx
1848
      compare_with_previous_resource_name:
31 halyavin 1849
	mov	ax,[esi+ecx]
157 heavyiron 1850
	cmp	ax,[ebx+ecx]
1851
	ja	check_this_resource_name
1852
	jb	check_next_resource_name
1853
	add	ecx,2
1854
	mov	ax,[esi+ecx]
1855
	or	ax,[ebx+ecx]
1856
	jnz	compare_with_previous_resource_name
1857
	jmp	check_next_resource_name
1858
      skip_resource_name:
31 halyavin 1859
	cmp	word [esi],0FFFFh
157 heavyiron 1860
	jne	skip_unicode_string
1861
	add	esi,4
1862
	ret
1863
      skip_unicode_string:
31 halyavin 1864
	add	esi,2
157 heavyiron 1865
	cmp	word [esi-2],0
1866
	jne	skip_unicode_string
1867
	ret
1868
      check_this_resource_name:
31 halyavin 1869
	or	edx,edx
157 heavyiron 1870
	jz	resource_name_found
1871
	xor	ecx,ecx
1872
      compare_with_current_resource_name:
31 halyavin 1873
	mov	ax,[esi+ecx]
157 heavyiron 1874
	cmp	ax,[edx+ecx]
1875
	ja	check_next_resource_name
1876
	jb	resource_name_found
1877
	add	ecx,2
1878
	mov	ax,[esi+ecx]
1879
	or	ax,[edx+ecx]
1880
	jnz	compare_with_current_resource_name
1881
	jmp	same_resource_name
1882
      resource_name_found:
31 halyavin 1883
	mov	edx,esi
157 heavyiron 1884
      same_resource_name:
31 halyavin 1885
	mov	eax,[esp]
157 heavyiron 1886
	mov	[eax+8],edi
1887
      check_next_resource_name:
31 halyavin 1888
	pop	esi
157 heavyiron 1889
	mov	eax,[esi+16]
1890
	lea	esi,[esi+20+eax]
1891
	jmp	find_resource_name
1892
      resource_name_ok:
31 halyavin 1893
	or	edx,edx
157 heavyiron 1894
	jz	resource_name_directory_done
1895
	mov	ebx,edx
1896
      make_resource_name_entry:
31 halyavin 1897
	mov	eax,[esp]
157 heavyiron 1898
	inc	word [eax+12]
1899
	lea	eax,[edi+8]
1900
	cmp	eax,[display_buffer]
1901
	jae	out_of_memory
1902
	mov	eax,ebx
1903
	stos	dword [edi]
1904
	xor	eax,eax
1905
	stos	dword [edi]
1906
	jmp	make_resource_name_directory
1907
      resource_name_directory_done:
31 halyavin 1908
	mov	ebx,-1
157 heavyiron 1909
      make_resource_id_directory:
31 halyavin 1910
	mov	esi,[free_additional_memory]
157 heavyiron 1911
	mov	edx,10000h
1912
      find_resource_id:
31 halyavin 1913
	cmp	dword [esi],0
157 heavyiron 1914
	je	resource_id_ok
1915
	push	esi
1916
	cmp	[esi+4],ebp
1917
	jne	check_next_resource_id
1918
	add	esi,20
1919
	call	skip_resource_name
1920
	cmp	word [esi],0FFFFh
1921
	jne	check_next_resource_id
1922
	movzx	eax,word [esi+2]
1923
	cmp	eax,ebx
1924
	jle	check_next_resource_id
1925
	cmp	eax,edx
1926
	jg	check_next_resource_id
1927
	mov	edx,eax
1928
	mov	eax,[esp]
1929
	mov	[eax+8],edi
1930
      check_next_resource_id:
31 halyavin 1931
	pop	esi
157 heavyiron 1932
	mov	eax,[esi+16]
1933
	lea	esi,[esi+20+eax]
1934
	jmp	find_resource_id
1935
      resource_id_ok:
31 halyavin 1936
	cmp	edx,10000h
157 heavyiron 1937
	je	resource_id_directory_done
1938
	mov	ebx,edx
1939
      make_resource_id_entry:
31 halyavin 1940
	mov	eax,[esp]
157 heavyiron 1941
	inc	word [eax+14]
1942
	lea	eax,[edi+8]
1943
	cmp	eax,[display_buffer]
1944
	jae	out_of_memory
1945
	mov	eax,ebx
1946
	stos	dword [edi]
1947
	xor	eax,eax
1948
	stos	dword [edi]
1949
	jmp	make_resource_id_directory
1950
      resource_id_directory_done:
31 halyavin 1951
	pop	eax
157 heavyiron 1952
	mov	esi,ebp
1953
	pop	ecx
1954
	add	esi,8
1955
	dec	cx
1956
	jnz	make_resource_directories
1957
      resource_directories_ok:
31 halyavin 1958
	shr	ecx,16
157 heavyiron 1959
	jnz	make_resource_directories
1960
	mov	esi,[resource_data]
1961
	add	esi,10h
1962
	movzx	eax,word [esi-4]
1963
	movzx	edx,word [esi-2]
1964
	add	eax,edx
1965
	lea	esi,[esi+eax*8]
1966
	push	edi			; address of language directories
1967
      update_resource_directories:
31 halyavin 1968
	cmp	esi,[esp]
157 heavyiron 1969
	je	resource_directories_updated
1970
	add	esi,10h
1971
	mov	ecx,[esi-4]
1972
	or	cx,cx
1973
	jz	language_directories_ok
1974
      make_language_directories:
31 halyavin 1975
	push	ecx
157 heavyiron 1976
	push	edi
1977
	mov	edx,edi
1978
	sub	edx,[resource_data]
1979
	bts	edx,31
1980
	mov	[esi+4],edx
1981
	lea	eax,[edi+16]
1982
	cmp	eax,[display_buffer]
1983
	jae	out_of_memory
1984
	xor	eax,eax
1985
	stos	dword [edi]
1986
	call	make_timestamp
1987
	stos	dword [edi]
1988
	xor	eax,eax
1989
	stos	dword [edi]
1990
	stos	dword [edi]
1991
	mov	ebp,esi
1992
	mov	ebx,-1
1993
      make_language_id_directory:
31 halyavin 1994
	mov	esi,[free_additional_memory]
157 heavyiron 1995
	mov	edx,10000h
1996
      find_language_id:
31 halyavin 1997
	cmp	dword [esi],0
157 heavyiron 1998
	je	language_id_ok
1999
	push	esi
2000
	cmp	[esi+8],ebp
2001
	jne	check_next_language_id
2002
	add	esi,20
2003
	mov	eax,esi
2004
	call	skip_resource_name
2005
	call	skip_resource_name
2006
	neg	eax
2007
	add	eax,esi
2008
	and	eax,11b
2009
	add	esi,eax
2010
      get_language_id:
31 halyavin 2011
	movzx	eax,word [esi+6]
157 heavyiron 2012
	cmp	eax,ebx
2013
	jle	check_next_language_id
2014
	cmp	eax,edx
2015
	jge	check_next_language_id
2016
	mov	edx,eax
2017
	mov	eax,[esp]
2018
	mov	dword [value],eax
992 heavyiron 2019
      check_next_language_id:
31 halyavin 2020
	pop	esi
157 heavyiron 2021
	mov	eax,[esi+16]
2022
	lea	esi,[esi+20+eax]
2023
	jmp	find_language_id
2024
      language_id_ok:
31 halyavin 2025
	cmp	edx,10000h
157 heavyiron 2026
	je	language_id_directory_done
2027
	mov	ebx,edx
2028
      make_language_id_entry:
31 halyavin 2029
	mov	eax,[esp]
157 heavyiron 2030
	inc	word [eax+14]
2031
	lea	eax,[edi+8]
2032
	cmp	eax,[display_buffer]
2033
	jae	out_of_memory
2034
	mov	eax,ebx
2035
	stos	dword [edi]
2036
	mov	eax,dword [value]
992 heavyiron 2037
	stos	dword [edi]
157 heavyiron 2038
	jmp	make_language_id_directory
2039
      language_id_directory_done:
31 halyavin 2040
	pop	eax
157 heavyiron 2041
	mov	esi,ebp
2042
	pop	ecx
2043
	add	esi,8
2044
	dec	cx
2045
	jnz	make_language_directories
2046
      language_directories_ok:
31 halyavin 2047
	shr	ecx,16
157 heavyiron 2048
	jnz	make_language_directories
2049
	jmp	update_resource_directories
2050
      resource_directories_updated:
31 halyavin 2051
	mov	esi,[resource_data]
157 heavyiron 2052
	push	edi
2053
      make_name_strings:
31 halyavin 2054
	add	esi,10h
157 heavyiron 2055
	movzx	eax,word [esi-2]
2056
	movzx	ecx,word [esi-4]
2057
	add	eax,ecx
2058
	lea	eax,[esi+eax*8]
2059
	push	eax
2060
	or	ecx,ecx
2061
	jz	string_entries_processed
2062
      process_string_entries:
31 halyavin 2063
	push	ecx
157 heavyiron 2064
	mov	edx,edi
2065
	sub	edx,[resource_data]
2066
	bts	edx,31
2067
	xchg	[esi],edx
2068
	mov	ebx,edi
2069
	xor	ax,ax
2070
	stos	word [edi]
2071
      copy_string_data:
31 halyavin 2072
	lea	eax,[edi+2]
157 heavyiron 2073
	cmp	eax,[display_buffer]
2074
	jae	out_of_memory
2075
	mov	ax,[edx]
2076
	or	ax,ax
2077
	jz	string_data_copied
2078
	stos	word [edi]
2079
	inc	word [ebx]
2080
	add	edx,2
2081
	jmp	copy_string_data
2082
      string_data_copied:
31 halyavin 2083
	add	esi,8
157 heavyiron 2084
	pop	ecx
2085
	loop	process_string_entries
2086
      string_entries_processed:
31 halyavin 2087
	pop	esi
157 heavyiron 2088
	cmp	esi,[esp]
2089
	jb	make_name_strings
2090
	mov	eax,edi
2091
	sub	eax,[resource_data]
2092
	test	al,11b
2093
	jz	resource_strings_alignment_ok
2094
	xor	ax,ax
2095
	stos	word [edi]
2096
      resource_strings_alignment_ok:
31 halyavin 2097
	pop	edx
157 heavyiron 2098
	pop	ebx			; address of language directories
2099
	mov	ebp,edi
2100
      update_language_directories:
31 halyavin 2101
	add	ebx,10h
157 heavyiron 2102
	movzx	eax,word [ebx-2]
2103
	movzx	ecx,word [ebx-4]
2104
	add	ecx,eax
2105
      make_data_records:
31 halyavin 2106
	push	ecx
157 heavyiron 2107
	mov	esi,edi
2108
	sub	esi,[resource_data]
2109
	xchg	esi,[ebx+4]
2110
	lea	eax,[edi+16]
2111
	cmp	eax,[display_buffer]
2112
	jae	out_of_memory
2113
	mov	eax,esi
2114
	stos	dword [edi]
2115
	mov	eax,[esi+12]
2116
	stos	dword [edi]
2117
	xor	eax,eax
2118
	stos	dword [edi]
2119
	stos	dword [edi]
2120
	pop	ecx
2121
	add	ebx,8
2122
	loop	make_data_records
2123
	cmp	ebx,edx
2124
	jb	update_language_directories
2125
	pop	ebx			; file handle
2126
	mov	esi,ebp
2127
	mov	ebp,edi
2128
      update_data_records:
31 halyavin 2129
	push	ebp
157 heavyiron 2130
	mov	ecx,edi
2131
	mov	eax,[current_section]
2132
	sub	ecx,[eax+14h]
2133
	add	ecx,[eax+0Ch]
2134
	xchg	ecx,[esi]
2135
	mov	edx,[ecx]
2136
	xor	al,al
2137
	call	lseek
2138
	mov	edx,edi
2139
	mov	ecx,[esi+4]
2140
	add	edi,ecx
2141
	cmp	edi,[display_buffer]
2142
	ja	out_of_memory
2143
	call	read
2144
	mov	eax,edi
2145
	sub	eax,[resource_data]
2146
	and	eax,11b
2147
	jz	resource_data_alignment_ok
2148
	mov	ecx,4
2149
	sub	ecx,eax
2150
	xor	al,al
2151
	rep	stos byte [edi]
2152
      resource_data_alignment_ok:
31 halyavin 2153
	pop	ebp
157 heavyiron 2154
	add	esi,16
2155
	cmp	esi,ebp
2156
	jb	update_data_records
2157
	pop	esi
2158
	call	close
2159
	mov	eax,edi
2160
	sub	eax,[resource_data]
2161
	mov	[resource_size],eax
2162
      resource_done:
31 halyavin 2163
	ret
157 heavyiron 2164
close_pe:
31 halyavin 2165
	call	close_pe_section
157 heavyiron 2166
	mov	edx,[code_start]
2167
	mov	[edx+50h],eax
2168
	call	make_timestamp
2169
	mov	edx,[code_start]
2170
	mov	[edx+8],eax
2171
	mov	eax,[number_of_relocations]
2172
	cmp	eax,-1
2173
	je	pe_relocations_ok
2174
	shl	eax,2
2175
	sub	[free_additional_memory],eax
2176
	btr	[resolver_flags],0
2177
	jnc	pe_relocations_ok
2178
	or	[next_pass_needed],-1
2179
      pe_relocations_ok:
109 heavyiron 2180
	mov	eax,[number_of_sections]
157 heavyiron 2181
	mov	[edx+6],ax
2182
	imul	eax,28h
2183
	movzx	ecx,word [edx+14h]
2184
	lea	eax,[eax+18h+ecx]
2185
	add	eax,[stub_size]
2186
	mov	ecx,[edx+3Ch]
2187
	dec	ecx
2188
	add	eax,ecx
2189
	not	ecx
2190
	and	eax,ecx
2191
	cmp	eax,[edx+54h]
2192
	je	pe_sections_ok
2193
	or	[next_pass_needed],-1
2194
      pe_sections_ok:
31 halyavin 2195
	xor	ecx,ecx
157 heavyiron 2196
	add	edx,78h
2197
	test	[format_flags],8
2198
	jz	process_directories
2199
	add	edx,10h
2200
      process_directories:
31 halyavin 2201
	mov	eax,[edx+ecx*8]
157 heavyiron 2202
	or	eax,eax
2203
	jz	directory_ok
2204
	cmp	dword [edx+ecx*8+4],-1
2205
	jne	directory_ok
2206
      section_data:
31 halyavin 2207
	mov	ebx,[edx+ecx*8]
157 heavyiron 2208
	mov	eax,[ebx+0Ch]
2209
	mov	[edx+ecx*8],eax 	; directory rva
2210
	mov	eax,[ebx+8]
2211
	mov	[edx+ecx*8+4],eax	; directory size
2212
      directory_ok:
31 halyavin 2213
	inc	cl
157 heavyiron 2214
	cmp	cl,10h
2215
	jb	process_directories
2216
	mov	ebx,[code_start]
2217
	sub	ebx,[stub_size]
2218
	mov	ecx,edi
2219
	sub	ecx,ebx
2220
	mov	ebp,ecx
2221
	shr	ecx,1
2222
	xor	eax,eax
2223
	cdq
2224
      calculate_checksum:
31 halyavin 2225
	mov	dx,[ebx]
157 heavyiron 2226
	add	eax,edx
2227
	mov	dx,ax
2228
	shr	eax,16
2229
	add	eax,edx
2230
	add	ebx,2
2231
	loop	calculate_checksum
2232
	add	eax,ebp
2233
	mov	ebx,[code_start]
2234
	mov	[ebx+58h],eax
2235
	ret
2236
31 halyavin 2237
 
2238
	mov	eax,[additional_memory]
157 heavyiron 2239
	mov	[symbols_stream],eax
2240
	mov	ebx,eax
2241
	add	eax,20h
2242
	cmp	eax,[structures_buffer]
2243
	jae	out_of_memory
2244
	mov	[free_additional_memory],eax
2245
	xor	eax,eax
2246
	mov	[ebx],al
2247
	mov	[ebx+4],eax
2248
	mov	[ebx+8],edi
2249
	mov	al,4
2250
	mov	[ebx+10h],eax
2251
	mov	al,60h
2252
	bt	[format_flags],0
2253
	jnc	flat_section_flags_ok
2254
	or	eax,0E0000000h
2255
      flat_section_flags_ok:
31 halyavin 2256
	mov	dword [ebx+14h],eax
157 heavyiron 2257
	mov	[current_section],ebx
2258
	xor	eax,eax
1115 heavyiron 2259
	mov	[number_of_sections],eax
2260
	mov	dword [org_origin],edi
157 heavyiron 2261
	mov	dword [org_origin+4],eax
1115 heavyiron 2262
	mov	[org_registers],eax
2263
	mov	[org_start],edi
157 heavyiron 2264
	mov	[org_symbol],ebx
2265
	mov	[labels_type],2
2266
	mov	[code_type],32
2267
	test	[format_flags],8
2268
	jz	format_defined
607 heavyiron 2269
	mov	[labels_type],4
157 heavyiron 2270
	mov	[code_type],64
2271
	jmp	format_defined
607 heavyiron 2272
coff_section:
31 halyavin 2273
	call	close_coff_section
157 heavyiron 2274
	mov	ebx,[free_additional_memory]
2275
	lea	eax,[ebx+20h]
2276
	cmp	eax,[structures_buffer]
2277
	jae	out_of_memory
2278
	mov	[free_additional_memory],eax
2279
	mov	[current_section],ebx
2280
	inc	[number_of_sections]
2281
	xor	eax,eax
2282
	mov	[ebx],al
2283
	mov	[ebx+8],edi
2284
	mov	dword [org_origin],edi
2285
	mov	dword [org_origin+4],eax
1115 heavyiron 2286
	mov	[org_registers],eax
2287
	mov	[org_start],edi
157 heavyiron 2288
	mov	[org_symbol],ebx
2289
	mov	[labels_type],2
2290
	test	[format_flags],8
2291
	jz	coff_labels_type_ok
2292
	mov	[labels_type],4
2293
      coff_labels_type_ok:
31 halyavin 2294
	mov	[ebx+10h],eax
157 heavyiron 2295
	mov	[ebx+14h],eax
2296
	lods	word [esi]
2297
	cmp	ax,'('
2298
	jne	invalid_argument
2299
	mov	[ebx+4],esi
2300
	mov	ecx,[esi]
2301
	lea	esi,[esi+4+ecx+1]
2302
	cmp	ecx,8
2303
	ja	name_too_long
2304
      coff_section_flags:
31 halyavin 2305
	cmp	byte [esi],1Ch
157 heavyiron 2306
	je	coff_section_alignment
2307
	cmp	byte [esi],19h
2308
	jne	coff_section_settings_ok
2309
	inc	esi
2310
	lods	byte [esi]
2311
	bt	[format_flags],0
2312
	jc	coff_section_flag_ok
2313
	cmp	al,7
2314
	ja	invalid_argument
2315
      coff_section_flag_ok:
31 halyavin 2316
	mov	cl,al
157 heavyiron 2317
	mov	eax,1
2318
	shl	eax,cl
2319
	test	dword [ebx+14h],eax
2320
	jnz	setting_already_specified
2321
	or	dword [ebx+14h],eax
2322
	jmp	coff_section_flags
2323
      coff_section_alignment:
31 halyavin 2324
	bt	[format_flags],0
157 heavyiron 2325
	jnc	invalid_argument
2326
	inc	esi
2327
	lods	byte [esi]
2328
	or	al,al
2329
	jnz	invalid_argument
2330
	lods	byte [esi]
2331
	cmp	al,'('
2332
	jne	invalid_argument
2333
	cmp	byte [esi],'.'
2334
	je	invalid_value
2335
	push	ebx
2336
	call	get_dword_value
2337
	pop	ebx
2338
	cmp	[value_type],0
2339
	jne	invalid_use_of_symbol
2340
	mov	edx,eax
2341
	dec	edx
2342
	test	eax,edx
2343
	jnz	invalid_value
2344
	or	eax,eax
2345
	jz	invalid_value
2346
	cmp	eax,2000h
2347
	ja	invalid_value
2348
	bsf	edx,eax
2349
	inc	edx
2350
	shl	edx,20
2351
	or	[ebx+14h],edx
2352
	xchg	[ebx+10h],eax
2353
	or	eax,eax
2354
	jnz	setting_already_specified
2355
	jmp	coff_section_flags
2356
      coff_section_settings_ok:
31 halyavin 2357
	cmp	dword [ebx+10h],0
157 heavyiron 2358
	jne	instruction_assembled
2359
	mov	dword [ebx+10h],4
2360
	bt	[format_flags],0
2361
	jnc	instruction_assembled
2362
	or	dword [ebx+14h],300000h
2363
	jmp	instruction_assembled
2364
      close_coff_section:
31 halyavin 2365
	mov	ebx,[current_section]
157 heavyiron 2366
	mov	eax,edi
2367
	mov	edx,[ebx+8]
2368
	sub	eax,edx
2369
	mov	[ebx+0Ch],eax
2370
	xor	eax,eax
2371
	xchg	[undefined_data_end],eax
2372
	cmp	eax,edi
2373
	jne	coff_section_ok
2374
	cmp	edx,[undefined_data_start]
2375
	jne	coff_section_ok
2376
	mov	edi,edx
2377
	or	byte [ebx+14h],80h
2378
      coff_section_ok:
31 halyavin 2379
	ret
157 heavyiron 2380
mark_coff_relocation:
31 halyavin 2381
	cmp	[value_type],3
157 heavyiron 2382
	je	coff_relocation_relative
2383
	push	ebx eax
2384
	test	[format_flags],8
2385
	jnz	coff_64bit_relocation
2386
	mov	al,6
2387
	cmp	[value_type],5
992 heavyiron 2388
	jne	coff_relocation
2389
	inc	al
2390
	jmp	coff_relocation
157 heavyiron 2391
      coff_64bit_relocation:
31 halyavin 2392
	mov	al,1
157 heavyiron 2393
	cmp	[value_type],4
2394
	je	coff_relocation
2395
	mov	al,2
2396
	cmp	[value_type],5
992 heavyiron 2397
	jne	coff_relocation
2398
	inc	al
2399
	jmp	coff_relocation
157 heavyiron 2400
      coff_relocation_relative:
31 halyavin 2401
	push	ebx
157 heavyiron 2402
	bt	[format_flags],0
2403
	jnc	relative_ok
2404
	mov	ebx,[current_section]
2405
	mov	ebx,[ebx+8]
2406
	sub	ebx,edi
2407
	sub	eax,ebx
2408
	add	eax,4
2409
      relative_ok:
31 halyavin 2410
	push	eax
157 heavyiron 2411
	mov	al,20
2412
	test	[format_flags],8
2413
	jnz	relative_coff_64bit_relocation
2414
	cmp	[labels_type],2
2415
	jne	invalid_use_of_symbol
2416
	jmp	coff_relocation
2417
      relative_coff_64bit_relocation:
31 halyavin 2418
	mov	al,4
157 heavyiron 2419
	cmp	[labels_type],4
2420
	jne	invalid_use_of_symbol
2421
      coff_relocation:
31 halyavin 2422
	mov	ebx,[free_additional_memory]
157 heavyiron 2423
	add	ebx,0Ch
2424
	cmp	ebx,[structures_buffer]
2425
	jae	out_of_memory
2426
	mov	[free_additional_memory],ebx
2427
	mov	byte [ebx-0Ch],al
2428
	mov	eax,[current_section]
2429
	mov	eax,[eax+8]
2430
	neg	eax
2431
	add	eax,edi
2432
	mov	[ebx-0Ch+4],eax
2433
	mov	eax,[symbol_identifier]
2434
	mov	[ebx-0Ch+8],eax
2435
	pop	eax ebx
2436
	ret
2437
close_coff:
31 halyavin 2438
	call	close_coff_section
157 heavyiron 2439
	cmp	[next_pass_needed],0
2440
	je	coff_closed
2441
	mov	eax,[symbols_stream]
2442
	mov	[free_additional_memory],eax
2443
      coff_closed:
31 halyavin 2444
	ret
157 heavyiron 2445
coff_formatter:
31 halyavin 2446
	sub	edi,[code_start]
157 heavyiron 2447
	mov	[code_size],edi
2448
	call	prepare_default_section
2449
	mov	edi,[free_additional_memory]
2450
	mov	ebx,edi
2451
	mov	ecx,28h shr 2
2452
	imul	ecx,[number_of_sections]
2453
	add	ecx,14h shr 2
2454
	lea	eax,[edi+ecx*4]
2455
	cmp	eax,[structures_buffer]
2456
	jae	out_of_memory
2457
	xor	eax,eax
2458
	rep	stos dword [edi]
2459
	mov	word [ebx],14Ch
2460
	test	[format_flags],8
2461
	jz	coff_magic_ok
2462
	mov	word [ebx],8664h
2463
      coff_magic_ok:
31 halyavin 2464
	mov	word [ebx+12h],104h
157 heavyiron 2465
	bt	[format_flags],0
2466
	jnc	coff_flags_ok
2467
	or	byte [ebx+12h],80h
2468
      coff_flags_ok:
31 halyavin 2469
	push	ebx
157 heavyiron 2470
	call	make_timestamp
2471
	pop	ebx
2472
	mov	[ebx+4],eax
2473
	mov	eax,[number_of_sections]
2474
	mov	[ebx+2],ax
2475
	mov	esi,[symbols_stream]
2476
	xor	eax,eax
2477
	xor	ecx,ecx
2478
      enumerate_symbols:
31 halyavin 2479
	cmp	esi,[free_additional_memory]
157 heavyiron 2480
	je	symbols_enumerated
2481
	mov	dl,[esi]
2482
	or	dl,dl
2483
	jz	enumerate_section
2484
	cmp	dl,0C0h
624 heavyiron 2485
	jae	enumerate_public
2486
	cmp	dl,80h
157 heavyiron 2487
	jae	enumerate_extrn
624 heavyiron 2488
	add	esi,0Ch
157 heavyiron 2489
	jmp	enumerate_symbols
2490
      enumerate_section:
31 halyavin 2491
	mov	edx,eax
157 heavyiron 2492
	shl	edx,8
2493
	mov	[esi],edx
2494
	inc	eax
2495
	inc	ecx
2496
	mov	[esi+1Eh],cx
2497
	add	esi,20h
2498
	jmp	enumerate_symbols
2499
      enumerate_public:
31 halyavin 2500
	mov	edx,eax
157 heavyiron 2501
	shl	edx,8
2502
	mov	dl,[esi]
624 heavyiron 2503
	mov	[esi],edx
157 heavyiron 2504
	mov	edx,[esi+8]
2505
	add	esi,10h
2506
	inc	eax
2507
	cmp	byte [edx+11],2
2508
	jne	enumerate_symbols
2509
	mov	edx,[edx+20]
2510
	cmp	byte [edx],0C0h
624 heavyiron 2511
	jae	enumerate_symbols
2512
	cmp	byte [edx],80h
2513
	jb	enumerate_symbols
2514
	inc	eax
157 heavyiron 2515
	jmp	enumerate_symbols
2516
      enumerate_extrn:
31 halyavin 2517
	mov	edx,eax
157 heavyiron 2518
	shl	edx,8
2519
	mov	dl,[esi]
624 heavyiron 2520
	mov	[esi],edx
157 heavyiron 2521
	add	esi,0Ch
2522
	inc	eax
2523
	jmp	enumerate_symbols
2524
      prepare_default_section:
31 halyavin 2525
	mov	ebx,[symbols_stream]
157 heavyiron 2526
	cmp	dword [ebx+0Ch],0
2527
	jne	default_section_ok
2528
	cmp	[number_of_sections],0
2529
	je	default_section_ok
2530
	mov	edx,ebx
2531
      find_references_to_default_section:
31 halyavin 2532
	cmp	ebx,[free_additional_memory]
157 heavyiron 2533
	jne	check_reference
2534
	add	[symbols_stream],20h
2535
	ret
2536
      check_reference:
31 halyavin 2537
	mov	al,[ebx]
157 heavyiron 2538
	or	al,al
2539
	jz	skip_other_section
2540
	cmp	al,0C0h
624 heavyiron 2541
	jae	check_public_reference
2542
	cmp	al,80h
157 heavyiron 2543
	jae	next_reference
624 heavyiron 2544
	cmp	edx,[ebx+8]
157 heavyiron 2545
	je	default_section_ok
2546
      next_reference:
31 halyavin 2547
	add	ebx,0Ch
157 heavyiron 2548
	jmp	find_references_to_default_section
2549
      check_public_reference:
31 halyavin 2550
	mov	eax,[ebx+8]
157 heavyiron 2551
	add	ebx,10h
2552
	test	byte [eax+8],1
2553
	jz	find_references_to_default_section
2554
	mov	cx,[current_pass]
2555
	cmp	cx,[eax+16]
2556
	jne	find_references_to_default_section
2557
	cmp	edx,[eax+20]
2558
	je	default_section_ok
2559
	jmp	find_references_to_default_section
2560
      skip_other_section:
31 halyavin 2561
	add	ebx,20h
157 heavyiron 2562
	jmp	find_references_to_default_section
2563
      default_section_ok:
31 halyavin 2564
	inc	[number_of_sections]
157 heavyiron 2565
	ret
2566
      symbols_enumerated:
31 halyavin 2567
	mov	[ebx+0Ch],eax
157 heavyiron 2568
	mov	ebp,edi
2569
	sub	ebp,ebx
2570
	push	ebp
2571
	lea	edi,[ebx+14h]
2572
	mov	esi,[symbols_stream]
2573
      find_section:
31 halyavin 2574
	cmp	esi,[free_additional_memory]
157 heavyiron 2575
	je	sections_finished
2576
	mov	al,[esi]
2577
	or	al,al
2578
	jz	section_found
2579
	add	esi,0Ch
2580
	cmp	al,0C0h
624 heavyiron 2581
	jb	find_section
2582
	add	esi,4
157 heavyiron 2583
	jmp	find_section
2584
      section_found:
31 halyavin 2585
	push	esi edi
157 heavyiron 2586
	mov	esi,[esi+4]
2587
	or	esi,esi
2588
	jz	default_section
2589
	mov	ecx,[esi]
2590
	add	esi,4
2591
	rep	movs byte [edi],[esi]
2592
	jmp	section_name_ok
2593
      default_section:
31 halyavin 2594
	mov	al,'.'
157 heavyiron 2595
	stos	byte [edi]
2596
	mov	eax,'flat'
2597
	stos	dword [edi]
2598
      section_name_ok:
31 halyavin 2599
	pop	edi esi
157 heavyiron 2600
	mov	eax,[esi+0Ch]
2601
	mov	[edi+10h],eax
2602
	mov	eax,[esi+14h]
2603
	mov	[edi+24h],eax
2604
	test	al,80h
2605
	jnz	section_ptr_ok
2606
	mov	eax,[esi+8]
2607
	sub	eax,[code_start]
2608
	add	eax,ebp
2609
	mov	[edi+14h],eax
2610
      section_ptr_ok:
31 halyavin 2611
	mov	ebx,[code_start]
157 heavyiron 2612
	mov	edx,[code_size]
2613
	add	ebx,edx
2614
	add	edx,ebp
2615
	xor	ecx,ecx
2616
	add	esi,20h
2617
      find_relocations:
31 halyavin 2618
	cmp	esi,[free_additional_memory]
157 heavyiron 2619
	je	section_relocations_done
2620
	mov	al,[esi]
2621
	or	al,al
2622
	jz	section_relocations_done
2623
	cmp	al,80h
2624
	jb	add_relocation
2625
	cmp	al,0C0h
624 heavyiron 2626
	jb	next_relocation
2627
	add	esi,10h
157 heavyiron 2628
	jmp	find_relocations
2629
      add_relocation:
31 halyavin 2630
	lea	eax,[ebx+0Ah]
157 heavyiron 2631
	cmp	eax,[display_buffer]
2632
	ja	out_of_memory
2633
	mov	eax,[esi+4]
2634
	mov	[ebx],eax
2635
	mov	eax,[esi+8]
2636
	mov	eax,[eax]
2637
	shr	eax,8
2638
	mov	[ebx+4],eax
2639
	movzx	ax,byte [esi]
2640
	mov	[ebx+8],ax
2641
	add	ebx,0Ah
2642
	inc	ecx
2643
      next_relocation:
31 halyavin 2644
	add	esi,0Ch
157 heavyiron 2645
	jmp	find_relocations
2646
      section_relocations_done:
31 halyavin 2647
	cmp	ecx,10000h
157 heavyiron 2648
	jb	section_relocations_count_16bit
2649
	bt	[format_flags],0
2650
	jnc	format_limitations_exceeded
2651
	mov	word [edi+20h],0FFFFh
2652
	or	dword [edi+24h],1000000h
2653
	mov	[edi+18h],edx
2654
	push	esi edi
2655
	push	ecx
2656
	lea	esi,[ebx-1]
2657
	add	ebx,0Ah
2658
	lea	edi,[ebx-1]
2659
	imul	ecx,0Ah
2660
	std
2661
	rep	movs byte [edi],[esi]
2662
	cld
2663
	pop	ecx
2664
	inc	esi
2665
	inc	ecx
2666
	mov	[esi],ecx
2667
	xor	eax,eax
2668
	mov	[esi+4],eax
2669
	mov	[esi+8],ax
2670
	pop	edi esi
2671
	jmp	section_relocations_ok
2672
      section_relocations_count_16bit:
31 halyavin 2673
	mov	[edi+20h],cx
157 heavyiron 2674
	jcxz	section_relocations_ok
2675
	mov	[edi+18h],edx
2676
      section_relocations_ok:
31 halyavin 2677
	sub	ebx,[code_start]
157 heavyiron 2678
	mov	[code_size],ebx
2679
	add	edi,28h
2680
	jmp	find_section
2681
      sections_finished:
31 halyavin 2682
	mov	edx,[free_additional_memory]
157 heavyiron 2683
	mov	ebx,[code_size]
2684
	add	ebp,ebx
2685
	mov	[edx+8],ebp
2686
	add	ebx,[code_start]
2687
	mov	edi,ebx
2688
	mov	ecx,[edx+0Ch]
2689
	imul	ecx,12h shr 1
2690
	xor	eax,eax
2691
	shr	ecx,1
2692
	jnc	zero_symbols_table
2693
	stos	word [edi]
2694
      zero_symbols_table:
31 halyavin 2695
	rep	stos dword [edi]
157 heavyiron 2696
	mov	edx,edi
2697
	stos	dword [edi]
2698
	mov	esi,[symbols_stream]
2699
      make_symbols_table:
109 heavyiron 2700
	cmp	esi,[free_additional_memory]
157 heavyiron 2701
	je	symbols_table_ok
2702
	mov	al,[esi]
2703
	cmp	al,0C0h
624 heavyiron 2704
	jae	add_public_symbol
2705
	cmp	al,80h
157 heavyiron 2706
	jae	add_extrn_symbol
624 heavyiron 2707
	or	al,al
157 heavyiron 2708
	jz	add_section_symbol
2709
	add	esi,0Ch
2710
	jmp	make_symbols_table
2711
      add_section_symbol:
31 halyavin 2712
	call	store_symbol_name
157 heavyiron 2713
	movzx	eax,word [esi+1Eh]
2714
	mov	[ebx+0Ch],ax
2715
	mov	byte [ebx+10h],3
2716
	add	esi,20h
2717
	add	ebx,12h
2718
	jmp	make_symbols_table
2719
      add_extrn_symbol:
31 halyavin 2720
	call	store_symbol_name
157 heavyiron 2721
	mov	byte [ebx+10h],2
2722
	add	esi,0Ch
2723
	add	ebx,12h
2724
	jmp	make_symbols_table
2725
      add_public_symbol:
31 halyavin 2726
	call	store_symbol_name
157 heavyiron 2727
	mov	eax,[esi+0Ch]
2728
	mov	[current_line],eax
2729
	mov	eax,[esi+8]
2730
	test	byte [eax+8],1
2731
	jz	undefined_coff_public
692 heavyiron 2732
	mov	cx,[current_pass]
157 heavyiron 2733
	cmp	cx,[eax+16]
2734
	jne	undefined_coff_public
692 heavyiron 2735
	mov	cl,[eax+11]
157 heavyiron 2736
	or	cl,cl
2737
	jz	public_constant
2738
	test	[format_flags],8
2739
	jnz	check_64bit_public_symbol
2740
	cmp	cl,2
2741
	je	public_symbol_type_ok
2742
	jmp	invalid_use_of_symbol
2743
      undefined_coff_public:
692 heavyiron 2744
	mov	eax,[eax+24]
2745
	mov	[error_info],eax
2746
	jmp	undefined_symbol
2747
      check_64bit_public_symbol:
31 halyavin 2748
	cmp	cl,4
157 heavyiron 2749
	jne	invalid_use_of_symbol
2750
      public_symbol_type_ok:
31 halyavin 2751
	mov	ecx,[eax+20]
157 heavyiron 2752
	cmp	byte [ecx],80h
624 heavyiron 2753
	je	alias_symbol
157 heavyiron 2754
	cmp	byte [ecx],0
2755
	jne	invalid_use_of_symbol
2756
	mov	cx,[ecx+1Eh]
2757
	mov	[ebx+0Ch],cx
2758
      public_symbol_section_ok:
31 halyavin 2759
	cmp	dword [eax+4],0
157 heavyiron 2760
	je	store_public_symbol
2761
	cmp	dword [eax+4],-1
2762
	jne	value_out_of_range
2763
	bt	dword [eax],31
2764
	jnc	value_out_of_range
2765
      store_public_symbol:
31 halyavin 2766
	mov	eax,[eax]
157 heavyiron 2767
	mov	[ebx+8],eax
2768
	mov	al,2
624 heavyiron 2769
	cmp	byte [esi],0C0h
2770
	je	store_symbol_class
2771
	inc	al
2772
	cmp	byte [esi],0C1h
2773
	je	store_symbol_class
2774
	mov	al,105
2775
      store_symbol_class:
2776
	mov	byte [ebx+10h],al
2777
	add	esi,10h
157 heavyiron 2778
	add	ebx,12h
2779
	jmp	make_symbols_table
2780
      alias_symbol:
31 halyavin 2781
	bt	[format_flags],0
157 heavyiron 2782
	jnc	invalid_use_of_symbol
2783
	mov	ecx,[eax]
2784
	or	ecx,[eax+4]
2785
	jnz	invalid_use_of_symbol
2786
	mov	byte [ebx+10h],69h
2787
	mov	byte [ebx+11h],1
2788
	add	ebx,12h
2789
	mov	ecx,[eax+20]
2790
	mov	ecx,[ecx]
2791
	shr	ecx,8
2792
	mov	[ebx],ecx
2793
	mov	byte [ebx+4],3
2794
	add	esi,10h
2795
	add	ebx,12h
2796
	jmp	make_symbols_table
2797
      public_constant:
31 halyavin 2798
	mov	word [ebx+0Ch],0FFFFh
157 heavyiron 2799
	jmp	public_symbol_section_ok
2800
      symbols_table_ok:
31 halyavin 2801
	mov	eax,edi
157 heavyiron 2802
	sub	eax,edx
2803
	mov	[edx],eax
2804
	sub	edi,[code_start]
2805
	mov	[code_size],edi
2806
	mov	[written_size],0
2807
	mov	edx,[output_file]
2808
	call	create
2809
	jc	write_failed
2810
	mov	edx,[free_additional_memory]
2811
	pop	ecx
2812
	add	[written_size],ecx
2813
	call	write
2814
	jc	write_failed
2815
	jmp	write_output
2816
      store_symbol_name:
31 halyavin 2817
	push	esi
157 heavyiron 2818
	mov	esi,[esi+4]
2819
	or	esi,esi
2820
	jz	default_name
2821
	lods	dword [esi]
2822
	mov	ecx,eax
2823
	cmp	ecx,8
2824
	ja	add_string
2825
	push	edi
2826
	mov	edi,ebx
2827
	rep	movs byte [edi],[esi]
2828
	pop	edi esi
2829
	ret
2830
      default_name:
31 halyavin 2831
	mov	dword [ebx],'.fla'
157 heavyiron 2832
	mov	dword [ebx+4],'t'
2833
	pop	esi
2834
	ret
2835
      add_string:
31 halyavin 2836
	mov	eax,edi
157 heavyiron 2837
	sub	eax,edx
2838
	mov	[ebx+4],eax
2839
	inc	ecx
2840
	rep	movs byte [edi],[esi]
2841
	pop	esi
2842
	ret
2843
31 halyavin 2844
 
2845
	test	[format_flags],8
157 heavyiron 2846
	jnz	format_elf64
2847
	mov	edx,edi
2848
	mov	ecx,34h shr 2
2849
	lea	eax,[edi+ecx*4]
2850
	cmp	eax,[display_buffer]
2851
	jae	out_of_memory
2852
	xor	eax,eax
2853
	rep	stos dword [edi]
2854
	mov	dword [edx],7Fh + 'ELF' shl 8
2855
	mov	al,1
2856
	mov	[edx+4],al
2857
	mov	[edx+5],al
2858
	mov	[edx+6],al
2859
	mov	[edx+14h],al
2860
	mov	byte [edx+12h],3
2861
	mov	byte [edx+28h],34h
2862
	mov	byte [edx+2Eh],28h
2863
	mov	[code_type],32
2864
	cmp	word [esi],1D19h
2865
	je	format_elf_exe
2866
	mov	[labels_type],2
2867
      elf_header_ok:
31 halyavin 2868
	mov	byte [edx+10h],1
157 heavyiron 2869
	mov	eax,[additional_memory]
2870
	mov	[symbols_stream],eax
2871
	mov	ebx,eax
2872
	add	eax,20h
2873
	cmp	eax,[structures_buffer]
2874
	jae	out_of_memory
2875
	mov	[free_additional_memory],eax
2876
	xor	eax,eax
2877
	mov	[current_section],ebx
2878
	mov	[number_of_sections],eax
2879
	mov	dword [org_origin],edi
2880
	mov	dword [org_origin+4],eax
2881
	mov	[org_registers],eax
2882
	mov	[org_start],edi
2883
	mov	[org_symbol],ebx
2884
	mov	[ebx],al
2885
	mov	[ebx+4],eax
2886
	mov	[ebx+8],edi
2887
	mov	al,111b
2888
	mov	[ebx+14h],eax
2889
	mov	al,4
2890
	mov	[ebx+10h],eax
2891
	test	[format_flags],8
2892
	jz	format_defined
607 heavyiron 2893
	mov	byte [ebx+10h],8
157 heavyiron 2894
	jmp	format_defined
607 heavyiron 2895
      format_elf64:
31 halyavin 2896
	mov	edx,edi
157 heavyiron 2897
	mov	ecx,40h shr 2
2898
	lea	eax,[edi+ecx*4]
2899
	cmp	eax,[display_buffer]
2900
	jae	out_of_memory
2901
	xor	eax,eax
2902
	rep	stos dword [edi]
2903
	mov	dword [edx],7Fh + 'ELF' shl 8
2904
	mov	al,1
2905
	mov	[edx+5],al
2906
	mov	[edx+6],al
2907
	mov	[edx+14h],al
2908
	mov	byte [edx+4],2
2909
	mov	byte [edx+12h],62
2910
	mov	byte [edx+34h],40h
2911
	mov	byte [edx+3Ah],40h
2912
	mov	[code_type],64
2913
	cmp	word [esi],1D19h
2914
	je	format_elf64_exe
2915
	mov	[labels_type],4
2916
	jmp	elf_header_ok
2917
elf_section:
31 halyavin 2918
	bt	[format_flags],0
157 heavyiron 2919
	jc	illegal_instruction
2920
	call	close_coff_section
2921
	mov	ebx,[free_additional_memory]
2922
	lea	eax,[ebx+20h]
2923
	cmp	eax,[structures_buffer]
2924
	jae	out_of_memory
2925
	mov	[free_additional_memory],eax
2926
	mov	[current_section],ebx
2927
	inc	word [number_of_sections]
2928
	jz	format_limitations_exceeded
2929
	xor	eax,eax
2930
	mov	[ebx],al
2931
	mov	[ebx+8],edi
2932
	mov	dword [org_origin],edi
2933
	mov	dword [org_origin+4],eax
1115 heavyiron 2934
	mov	[org_registers],eax
2935
	mov	[org_start],edi
157 heavyiron 2936
	mov	[org_symbol],ebx
2937
	test	[format_flags],8
2938
	jnz	elf64_labels_type
2939
	mov	[labels_type],2
2940
	jmp	elf_labels_type_ok
2941
      elf64_labels_type:
31 halyavin 2942
	mov	[labels_type],4
157 heavyiron 2943
      elf_labels_type_ok:
31 halyavin 2944
	mov	[ebx+10h],eax
157 heavyiron 2945
	mov	al,10b
2946
	mov	[ebx+14h],eax
2947
	lods	word [esi]
2948
	cmp	ax,'('
2949
	jne	invalid_argument
2950
	mov	[ebx+4],esi
2951
	mov	ecx,[esi]
2952
	lea	esi,[esi+4+ecx+1]
2953
      elf_section_flags:
31 halyavin 2954
	cmp	byte [esi],1Ch
157 heavyiron 2955
	je	elf_section_alignment
2956
	cmp	byte [esi],19h
2957
	jne	elf_section_settings_ok
2958
	inc	esi
2959
	lods	byte [esi]
2960
	sub	al,28
2961
	xor	al,11b
2962
	test	al,not 10b
2963
	jnz	invalid_argument
2964
	mov	cl,al
2965
	mov	al,1
2966
	shl	al,cl
2967
	test	byte [ebx+14h],al
2968
	jnz	setting_already_specified
2969
	or	byte [ebx+14h],al
2970
	jmp	elf_section_flags
2971
      elf_section_alignment:
31 halyavin 2972
	inc	esi
157 heavyiron 2973
	lods	byte [esi]
2974
	or	al,al
2975
	jnz	invalid_argument
2976
	lods	byte [esi]
2977
	cmp	al,'('
2978
	jne	invalid_argument
2979
	cmp	byte [esi],'.'
2980
	je	invalid_value
2981
	push	ebx
2982
	call	get_dword_value
2983
	pop	ebx
2984
	cmp	[value_type],0
2985
	jne	invalid_use_of_symbol
2986
	mov	edx,eax
2987
	dec	edx
2988
	test	eax,edx
2989
	jnz	invalid_value
2990
	or	eax,eax
2991
	jz	invalid_value
2992
	xchg	[ebx+10h],eax
2993
	or	eax,eax
2994
	jnz	setting_already_specified
2995
	jmp	elf_section_flags
2996
      elf_section_settings_ok:
31 halyavin 2997
	cmp	dword [ebx+10h],0
157 heavyiron 2998
	jne	instruction_assembled
2999
	mov	dword [ebx+10h],4
3000
	test	[format_flags],8
3001
	jz	instruction_assembled
3002
	mov	byte [ebx+10h],8
3003
	jmp	instruction_assembled
3004
mark_elf_relocation:
31 halyavin 3005
	cmp	[value_type],3
157 heavyiron 3006
	je	elf_relocation_relative
3007
	cmp	[value_type],7
174 heavyiron 3008
	je	elf_relocation_relative
3009
	push	ebx eax
157 heavyiron 3010
	cmp	[value_type],5
174 heavyiron 3011
	je	elf_gotoff_relocation
157 heavyiron 3012
	ja	invalid_use_of_symbol
174 heavyiron 3013
	mov	al,1			; R_386_32 / R_AMD64_64
3014
	test	[format_flags],8
157 heavyiron 3015
	jz	coff_relocation
3016
	cmp	[value_type],4
3017
	je	coff_relocation
3018
	mov	al,11			; R_AMD64_32S
174 heavyiron 3019
	jmp	coff_relocation
157 heavyiron 3020
      elf_gotoff_relocation:
109 heavyiron 3021
	test	[format_flags],8
174 heavyiron 3022
	jnz	invalid_use_of_symbol
3023
	mov	al,9			; R_386_GOTOFF
3024
	jmp	coff_relocation
157 heavyiron 3025
      elf_relocation_relative:
31 halyavin 3026
	cmp	[labels_type],0
157 heavyiron 3027
	je	invalid_use_of_symbol
3028
	push	ebx
3029
	mov	ebx,[current_section]
3030
	mov	ebx,[ebx+8]
3031
	sub	ebx,edi
3032
	sub	eax,ebx
3033
	push	eax
3034
	mov	al,2			; R_386_PC32 / R_AMD64_PC32
174 heavyiron 3035
	cmp	[value_type],3
3036
	je	coff_relocation
3037
	mov	al,4			; R_386_PLT32 / R_AMD64_PLT32
3038
	jmp	coff_relocation
157 heavyiron 3039
close_elf:
31 halyavin 3040
	bt	[format_flags],0
157 heavyiron 3041
	jc	close_elf_exe
3042
	call	close_coff_section
3043
	cmp	[next_pass_needed],0
3044
	je	elf_closed
3045
	mov	eax,[symbols_stream]
3046
	mov	[free_additional_memory],eax
3047
      elf_closed:
31 halyavin 3048
	ret
157 heavyiron 3049
elf_formatter:
31 halyavin 3050
	push	edi
157 heavyiron 3051
	call	prepare_default_section
3052
	mov	esi,[symbols_stream]
3053
	mov	edi,[free_additional_memory]
3054
	xor	eax,eax
3055
	mov	ecx,4
3056
	rep	stos dword [edi]
3057
	test	[format_flags],8
3058
	jz	find_first_section
3059
	mov	ecx,2
3060
	rep	stos dword [edi]
3061
      find_first_section:
31 halyavin 3062
	mov	al,[esi]
157 heavyiron 3063
	or	al,al
3064
	jz	first_section_found
3065
	cmp	al,0C0h
624 heavyiron 3066
	jb	skip_other_symbol
3067
	add	esi,4
157 heavyiron 3068
      skip_other_symbol:
31 halyavin 3069
	add	esi,0Ch
157 heavyiron 3070
	jmp	find_first_section
3071
      first_section_found:
31 halyavin 3072
	mov	ebx,esi
157 heavyiron 3073
	mov	ebp,esi
3074
	add	esi,20h
3075
	xor	ecx,ecx
3076
	xor	edx,edx
3077
      find_next_section:
31 halyavin 3078
	cmp	esi,[free_additional_memory]
157 heavyiron 3079
	je	make_section_symbol
3080
	mov	al,[esi]
3081
	or	al,al
3082
	jz	make_section_symbol
3083
	cmp	al,0C0h
624 heavyiron 3084
	jae	skip_public
3085
	cmp	al,80h
157 heavyiron 3086
	jae	skip_extrn
624 heavyiron 3087
	or	byte [ebx+14h],40h
157 heavyiron 3088
      skip_extrn:
31 halyavin 3089
	add	esi,0Ch
157 heavyiron 3090
	jmp	find_next_section
3091
      skip_public:
31 halyavin 3092
	add	esi,10h
157 heavyiron 3093
	jmp	find_next_section
3094
      make_section_symbol:
31 halyavin 3095
	mov	eax,edi
157 heavyiron 3096
	xchg	eax,[ebx+4]
3097
	stos	dword [edi]
3098
	test	[format_flags],8
3099
	jnz	elf64_section_symbol
3100
	xor	eax,eax
3101
	stos	dword [edi]
3102
	stos	dword [edi]
3103
	call	store_section_index
3104
	jmp	section_symbol_ok
3105
      store_section_index:
31 halyavin 3106
	inc	ecx
157 heavyiron 3107
	mov	eax,ecx
3108
	shl	eax,8
3109
	mov	[ebx],eax
3110
	inc	dx
3111
	jz	format_limitations_exceeded
3112
	mov	eax,edx
3113
	shl	eax,16
3114
	mov	al,3
3115
	test	byte [ebx+14h],40h
3116
	jz	section_index_ok
3117
	or	ah,-1
3118
	inc	dx
3119
	jz	format_limitations_exceeded
3120
      section_index_ok:
31 halyavin 3121
	stos	dword [edi]
157 heavyiron 3122
	ret
3123
      elf64_section_symbol:
31 halyavin 3124
	call	store_section_index
157 heavyiron 3125
	xor	eax,eax
3126
	stos	dword [edi]
3127
	stos	dword [edi]
3128
	stos	dword [edi]
3129
	stos	dword [edi]
3130
      section_symbol_ok:
31 halyavin 3131
	mov	ebx,esi
157 heavyiron 3132
	add	esi,20h
3133
	cmp	ebx,[free_additional_memory]
3134
	jne	find_next_section
3135
	inc	dx
3136
	jz	format_limitations_exceeded
3137
	mov	[current_section],edx
3138
	mov	esi,[symbols_stream]
3139
      find_other_symbols:
31 halyavin 3140
	cmp	esi,[free_additional_memory]
157 heavyiron 3141
	je	elf_symbol_table_ok
3142
	mov	al,[esi]
3143
	or	al,al
3144
	jz	skip_section
3145
	cmp	al,0C0h
624 heavyiron 3146
	jae	make_public_symbol
3147
	cmp	al,80h
157 heavyiron 3148
	jae	make_extrn_symbol
624 heavyiron 3149
	add	esi,0Ch
157 heavyiron 3150
	jmp	find_other_symbols
3151
      skip_section:
31 halyavin 3152
	add	esi,20h
157 heavyiron 3153
	jmp	find_other_symbols
3154
      make_public_symbol:
31 halyavin 3155
	mov	eax,[esi+0Ch]
157 heavyiron 3156
	mov	[current_line],eax
3157
	cmp	byte [esi],0C0h
624 heavyiron 3158
	jne	invalid_argument
3159
	mov	ebx,[esi+8]
157 heavyiron 3160
	test	byte [ebx+8],1
3161
	jz	undefined_public
692 heavyiron 3162
	mov	ax,[current_pass]
157 heavyiron 3163
	cmp	ax,[ebx+16]
3164
	jne	undefined_public
692 heavyiron 3165
	mov	dl,[ebx+11]
157 heavyiron 3166
	or	dl,dl
3167
	jz	public_absolute
3168
	mov	eax,[ebx+20]
3169
	cmp	byte [eax],0
3170
	jne	invalid_use_of_symbol
3171
	mov	eax,[eax+4]
3172
	test	[format_flags],8
3173
	jnz	elf64_public
3174
	cmp	dl,2
3175
	jne	invalid_use_of_symbol
3176
	mov	dx,[eax+0Eh]
3177
	jmp	section_for_public_ok
3178
      undefined_public:
692 heavyiron 3179
	mov	eax,[ebx+24]
3180
	mov	[error_info],eax
3181
	jmp	undefined_symbol
3182
      elf64_public:
31 halyavin 3183
	cmp	dl,4
157 heavyiron 3184
	jne	invalid_use_of_symbol
3185
	mov	dx,[eax+6]
3186
	jmp	section_for_public_ok
3187
      public_absolute:
31 halyavin 3188
	mov	dx,0FFF1h
157 heavyiron 3189
      section_for_public_ok:
31 halyavin 3190
	mov	eax,[esi+4]
157 heavyiron 3191
	stos	dword [edi]
3192
	test	[format_flags],8
3193
	jnz	elf64_public_symbol
3194
	call	get_public_value
3195
	stos	dword [edi]
3196
	xor	eax,eax
3197
	mov	al,[ebx+10]
3198
	stos	dword [edi]
3199
	mov	eax,edx
3200
	shl	eax,16
3201
	mov	al,10h
3202
	cmp	byte [ebx+10],0
3203
	je	elf_public_function
3204
	or	al,1
3205
	jmp	store_elf_public_info
3206
      elf_public_function:
109 heavyiron 3207
	or	al,2
157 heavyiron 3208
      store_elf_public_info:
109 heavyiron 3209
	stos	dword [edi]
157 heavyiron 3210
	jmp	public_symbol_ok
3211
      elf64_public_symbol:
31 halyavin 3212
	mov	eax,edx
157 heavyiron 3213
	shl	eax,16
3214
	mov	al,10h
3215
	cmp	byte [ebx+10],0
3216
	je	elf64_public_function
3217
	or	al,1
3218
	jmp	store_elf64_public_info
3219
      elf64_public_function:
109 heavyiron 3220
	or	al,2
157 heavyiron 3221
      store_elf64_public_info:
109 heavyiron 3222
	stos	dword [edi]
157 heavyiron 3223
	call	get_public_value
3224
	stos	dword [edi]
3225
	xor	eax,eax
3226
	stos	dword [edi]
3227
	mov	al,[ebx+10]
3228
	stos	dword [edi]
3229
	xor	al,al
3230
	stos	dword [edi]
3231
      public_symbol_ok:
31 halyavin 3232
	inc	ecx
157 heavyiron 3233
	mov	eax,ecx
3234
	shl	eax,8
3235
	mov	al,0C0h
624 heavyiron 3236
	mov	[esi],eax
157 heavyiron 3237
	add	esi,10h
3238
	jmp	find_other_symbols
3239
      get_public_value:
31 halyavin 3240
	mov	eax,[ebx]
157 heavyiron 3241
	cmp	dword [ebx+4],0
3242
	je	public_value_ok
3243
	cmp	dword [ebx+4],-1
3244
	jne	value_out_of_range
3245
	bt	eax,31
3246
	jnc	value_out_of_range
3247
      public_value_ok:
31 halyavin 3248
	ret
157 heavyiron 3249
      make_extrn_symbol:
31 halyavin 3250
	mov	eax,[esi+4]
157 heavyiron 3251
	stos	dword [edi]
3252
	test	[format_flags],8
3253
	jnz	elf64_extrn_symbol
3254
	xor	eax,eax
3255
	stos	dword [edi]
3256
	mov	eax,[esi+8]
3257
	stos	dword [edi]
3258
	mov	eax,10h
3259
	stos	dword [edi]
3260
	jmp	extrn_symbol_ok
3261
      elf64_extrn_symbol:
31 halyavin 3262
	mov	eax,10h
157 heavyiron 3263
	stos	dword [edi]
3264
	xor	al,al
3265
	stos	dword [edi]
3266
	stos	dword [edi]
3267
	mov	eax,[esi+8]
3268
	stos	dword [edi]
3269
	xor	eax,eax
3270
	stos	dword [edi]
3271
      extrn_symbol_ok:
31 halyavin 3272
	inc	ecx
157 heavyiron 3273
	mov	eax,ecx
3274
	shl	eax,8
3275
	mov	al,80h
624 heavyiron 3276
	mov	[esi],eax
157 heavyiron 3277
	add	esi,0Ch
3278
	jmp	find_other_symbols
3279
      elf_symbol_table_ok:
31 halyavin 3280
	mov	edx,edi
157 heavyiron 3281
	mov	ebx,[free_additional_memory]
3282
	xor	al,al
3283
	stos	byte [edi]
3284
	add	edi,16
3285
	mov	[edx+1],edx
3286
	add	ebx,10h
3287
	test	[format_flags],8
3288
	jz	make_string_table
3289
	add	ebx,8
3290
      make_string_table:
31 halyavin 3291
	cmp	ebx,edx
157 heavyiron 3292
	je	elf_string_table_ok
3293
	test	[format_flags],8
3294
	jnz	make_elf64_string
3295
	cmp	byte [ebx+0Dh],0
3296
	je	rel_prefix_ok
3297
	mov	byte [ebx+0Dh],0
3298
	mov	eax,'.rel'
3299
	stos	dword [edi]
3300
      rel_prefix_ok:
31 halyavin 3301
	mov	esi,edi
157 heavyiron 3302
	sub	esi,edx
3303
	xchg	esi,[ebx]
3304
	add	ebx,10h
3305
      make_elf_string:
31 halyavin 3306
	or	esi,esi
157 heavyiron 3307
	jz	default_string
3308
	lods	dword [esi]
3309
	mov	ecx,eax
3310
	rep	movs byte [edi],[esi]
3311
	xor	al,al
3312
	stos	byte [edi]
3313
	jmp	make_string_table
3314
      make_elf64_string:
31 halyavin 3315
	cmp	byte [ebx+5],0
157 heavyiron 3316
	je	elf64_rel_prefix_ok
3317
	mov	byte [ebx+5],0
3318
	mov	eax,'.rel'
3319
	stos	dword [edi]
3320
	mov	al,'a'
3321
	stos	byte [edi]
3322
      elf64_rel_prefix_ok:
31 halyavin 3323
	mov	esi,edi
157 heavyiron 3324
	sub	esi,edx
3325
	xchg	esi,[ebx]
3326
	add	ebx,18h
3327
	jmp	make_elf_string
3328
      default_string:
31 halyavin 3329
	mov	eax,'.fla'
157 heavyiron 3330
	stos	dword [edi]
3331
	mov	ax,'t'
3332
	stos	word [edi]
3333
	jmp	make_string_table
3334
      elf_string_table_ok:
31 halyavin 3335
	mov	[edx+1+8],edi
157 heavyiron 3336
	mov	ebx,[code_start]
3337
	mov	eax,edi
3338
	sub	eax,[free_additional_memory]
3339
	test	[format_flags],8
3340
	jnz	finish_elf64_header
3341
	mov	[ebx+20h],eax
3342
	mov	eax,[current_section]
3343
	inc	ax
3344
	jz	format_limitations_exceeded
3345
	mov	[ebx+32h],ax
3346
	inc	ax
3347
	jz	format_limitations_exceeded
3348
	mov	[ebx+30h],ax
3349
	jmp	elf_header_finished
3350
      finish_elf64_header:
31 halyavin 3351
	mov	[ebx+28h],eax
157 heavyiron 3352
	mov	eax,[current_section]
3353
	inc	ax
3354
	jz	format_limitations_exceeded
3355
	mov	[ebx+3Eh],ax
3356
	inc	ax
3357
	jz	format_limitations_exceeded
3358
	mov	[ebx+3Ch],ax
3359
      elf_header_finished:
31 halyavin 3360
	xor	eax,eax
157 heavyiron 3361
	mov	ecx,10
3362
	rep	stos dword [edi]
3363
	test	[format_flags],8
3364
	jz	elf_null_section_ok
3365
	mov	ecx,6
3366
	rep	stos dword [edi]
3367
      elf_null_section_ok:
31 halyavin 3368
	mov	esi,ebp
157 heavyiron 3369
	xor	ecx,ecx
3370
      make_section_entry:
31 halyavin 3371
	mov	ebx,edi
157 heavyiron 3372
	mov	eax,[esi+4]
3373
	mov	eax,[eax]
3374
	stos	dword [edi]
3375
	mov	eax,1
3376
	cmp	dword [esi+0Ch],0
3377
	je	bss_section
3378
	test	byte [esi+14h],80h
3379
	jz	section_type_ok
3380
      bss_section:
31 halyavin 3381
	mov	al,8
157 heavyiron 3382
      section_type_ok:
31 halyavin 3383
	stos	dword [edi]
157 heavyiron 3384
	mov	eax,[esi+14h]
3385
	and	al,3Fh
3386
	call	store_elf_machine_word
3387
	xor	eax,eax
3388
	call	store_elf_machine_word
3389
	mov	eax,[esi+8]
3390
	mov	[image_base],eax
3391
	sub	eax,[code_start]
3392
	call	store_elf_machine_word
3393
	mov	eax,[esi+0Ch]
3394
	call	store_elf_machine_word
3395
	xor	eax,eax
3396
	stos	dword [edi]
3397
	stos	dword [edi]
3398
	mov	eax,[esi+10h]
3399
	call	store_elf_machine_word
3400
	xor	eax,eax
3401
	call	store_elf_machine_word
3402
	inc	ecx
3403
	add	esi,20h
3404
	xchg	edi,[esp]
3405
	mov	ebp,edi
3406
      convert_relocations:
31 halyavin 3407
	cmp	esi,[free_additional_memory]
157 heavyiron 3408
	je	relocations_converted
3409
	mov	al,[esi]
3410
	or	al,al
3411
	jz	relocations_converted
3412
	cmp	al,80h
3413
	jb	make_relocation_entry
3414
	cmp	al,0C0h
624 heavyiron 3415
	jb	relocation_entry_ok
3416
	add	esi,10h
157 heavyiron 3417
	jmp	convert_relocations
3418
      make_relocation_entry:
31 halyavin 3419
	test	[format_flags],8
157 heavyiron 3420
	jnz	make_elf64_relocation_entry
3421
	mov	eax,[esi+4]
3422
	stos	dword [edi]
3423
	mov	eax,[esi+8]
3424
	mov	eax,[eax]
3425
	mov	al,[esi]
3426
	stos	dword [edi]
3427
	jmp	relocation_entry_ok
3428
      make_elf64_relocation_entry:
31 halyavin 3429
	mov	eax,[esi+4]
157 heavyiron 3430
	stos	dword [edi]
3431
	xor	eax,eax
3432
	stos	dword [edi]
3433
	movzx	eax,byte [esi]
3434
	stos	dword [edi]
3435
	mov	eax,[esi+8]
3436
	mov	eax,[eax]
3437
	shr	eax,8
3438
	stos	dword [edi]
3439
	xor	eax,eax
3440
	stos	dword [edi]
3441
	stos	dword [edi]
3442
      relocation_entry_ok:
31 halyavin 3443
	add	esi,0Ch
157 heavyiron 3444
	jmp	convert_relocations
3445
      store_elf_machine_word:
31 halyavin 3446
	stos	dword [edi]
157 heavyiron 3447
	test	[format_flags],8
3448
	jz	elf_machine_word_ok
3449
	mov	dword [edi],0
3450
	add	edi,4
3451
      elf_machine_word_ok:
31 halyavin 3452
	ret
157 heavyiron 3453
      relocations_converted:
31 halyavin 3454
	cmp	edi,ebp
157 heavyiron 3455
	xchg	edi,[esp]
3456
	je	rel_section_ok
3457
	mov	eax,[ebx]
3458
	sub	eax,4
3459
	test	[format_flags],8
3460
	jz	store_relocations_name_offset
3461
	dec	eax
3462
      store_relocations_name_offset:
31 halyavin 3463
	stos	dword [edi]
157 heavyiron 3464
	test	[format_flags],8
3465
	jnz	rela_section
3466
	mov	eax,9
3467
	jmp	store_relocations_type
3468
      rela_section:
31 halyavin 3469
	mov	eax,4
157 heavyiron 3470
      store_relocations_type:
31 halyavin 3471
	stos	dword [edi]
157 heavyiron 3472
	xor	al,al
3473
	call	store_elf_machine_word
3474
	call	store_elf_machine_word
3475
	mov	eax,ebp
3476
	sub	eax,[code_start]
3477
	call	store_elf_machine_word
3478
	mov	eax,[esp]
3479
	sub	eax,ebp
3480
	call	store_elf_machine_word
3481
	mov	eax,[current_section]
3482
	stos	dword [edi]
3483
	mov	eax,ecx
3484
	stos	dword [edi]
3485
	inc	ecx
3486
	test	[format_flags],8
3487
	jnz	finish_elf64_rela_section
3488
	mov	eax,4
3489
	stos	dword [edi]
3490
	mov	al,8
3491
	stos	dword [edi]
3492
	jmp	rel_section_ok
3493
      finish_elf64_rela_section:
31 halyavin 3494
	mov	eax,8
157 heavyiron 3495
	stos	dword [edi]
3496
	xor	al,al
3497
	stos	dword [edi]
3498
	mov	al,24
3499
	stos	dword [edi]
3500
	xor	al,al
3501
	stos	dword [edi]
3502
      rel_section_ok:
31 halyavin 3503
	cmp	esi,[free_additional_memory]
157 heavyiron 3504
	jne	make_section_entry
3505
	pop	eax
3506
	mov	ebx,[code_start]
3507
	sub	eax,ebx
3508
	mov	[code_size],eax
3509
	mov	ecx,20h
3510
	test	[format_flags],8
3511
	jz	adjust_elf_section_headers_offset
3512
	mov	ecx,28h
3513
      adjust_elf_section_headers_offset:
31 halyavin 3514
	add	[ebx+ecx],eax
157 heavyiron 3515
	mov	eax,1
3516
	stos	dword [edi]
3517
	mov	al,2
3518
	stos	dword [edi]
3519
	xor	al,al
3520
	call	store_elf_machine_word
3521
	call	store_elf_machine_word
3522
	mov	eax,[code_size]
3523
	call	store_elf_machine_word
3524
	mov	eax,[edx+1]
3525
	sub	eax,[free_additional_memory]
3526
	call	store_elf_machine_word
3527
	mov	eax,[current_section]
3528
	inc	eax
3529
	stos	dword [edi]
3530
	mov	eax,[number_of_sections]
3531
	inc	eax
3532
	stos	dword [edi]
3533
	test	[format_flags],8
3534
	jnz	finish_elf64_sym_section
3535
	mov	eax,4
3536
	stos	dword [edi]
3537
	mov	al,10h
3538
	stos	dword [edi]
3539
	jmp	sym_section_ok
3540
      finish_elf64_sym_section:
31 halyavin 3541
	mov	eax,8
157 heavyiron 3542
	stos	dword [edi]
3543
	xor	al,al
3544
	stos	dword [edi]
3545
	mov	al,18h
3546
	stos	dword [edi]
3547
	xor	al,al
3548
	stos	dword [edi]
3549
      sym_section_ok:
31 halyavin 3550
	mov	al,1+8
157 heavyiron 3551
	stos	dword [edi]
3552
	mov	al,3
3553
	stos	dword [edi]
3554
	xor	al,al
3555
	call	store_elf_machine_word
3556
	call	store_elf_machine_word
3557
	mov	eax,[edx+1]
3558
	sub	eax,[free_additional_memory]
3559
	add	eax,[code_size]
3560
	call	store_elf_machine_word
3561
	mov	eax,[edx+1+8]
3562
	sub	eax,[edx+1]
3563
	call	store_elf_machine_word
3564
	xor	eax,eax
3565
	stos	dword [edi]
3566
	stos	dword [edi]
3567
	mov	al,1
3568
	call	store_elf_machine_word
3569
	xor	eax,eax
3570
	call	store_elf_machine_word
3571
	mov	eax,'tab'
3572
	mov	dword [edx+1],'.sym'
3573
	mov	[edx+1+4],eax
3574
	mov	dword [edx+1+8],'.str'
3575
	mov	[edx+1+8+4],eax
3576
	mov	[resource_data],edx
992 heavyiron 3577
	mov	[written_size],0
157 heavyiron 3578
	mov	edx,[output_file]
3579
	call	create
3580
	jc	write_failed
3581
	call	write_code
3582
	mov	ecx,edi
3583
	mov	edx,[free_additional_memory]
3584
	sub	ecx,edx
3585
	add	[written_size],ecx
3586
	call	write
3587
	jc	write_failed
3588
	jmp	output_written
3589
31 halyavin 3590
 
3591
	add	esi,2
157 heavyiron 3592
	or	[format_flags],1
3593
	mov	[image_base],8048000h
3594
	cmp	byte [esi],80h
3595
	jne	elf_exe_base_ok
3596
	lods	word [esi]
3597
	cmp	ah,'('
3598
	jne	invalid_argument
3599
	cmp	byte [esi],'.'
3600
	je	invalid_value
3601
	push	edx
3602
	call	get_dword_value
3603
	cmp	[value_type],0
3604
	jne	invalid_use_of_symbol
3605
	mov	[image_base],eax
3606
	pop	edx
3607
      elf_exe_base_ok:
31 halyavin 3608
	mov	byte [edx+10h],2
157 heavyiron 3609
	mov	byte [edx+2Ah],20h
3610
	mov	ebx,edi
3611
	mov	ecx,20h shr 2
3612
	cmp	[current_pass],0
3613
	je	init_elf_segments
3614
	imul	ecx,[number_of_sections]
3615
      init_elf_segments:
109 heavyiron 3616
	xor	eax,eax
157 heavyiron 3617
	rep	stos dword [edi]
3618
	mov	[number_of_sections],0
3619
	mov	byte [ebx],1
3620
	mov	word [ebx+1Ch],1000h
3621
	mov	byte [ebx+18h],111b
3622
	mov	eax,edi
3623
	sub	eax,[code_start]
3624
	mov	[ebx+4],eax
3625
	add	eax,[image_base]
3626
	mov	[ebx+8],eax
3627
	mov	[ebx+0Ch],eax
3628
	mov	[edx+18h],eax
3629
	xor	edx,edx
3630
	not	eax
3631
	not	edx
3632
	add	eax,1
3633
	adc	edx,0
3634
	add	eax,edi
3635
	adc	edx,0
3636
	mov	dword [org_origin],eax
3637
	mov	dword [org_origin+4],edx
3638
	mov	[org_registers],0
3639
	mov	[org_start],edi
3640
	mov	[symbols_stream],edi
3641
	jmp	format_defined
607 heavyiron 3642
      format_elf64_exe:
31 halyavin 3643
	add	esi,2
157 heavyiron 3644
	or	[format_flags],1
3645
	mov	[image_base],400000h
3646
	mov	[image_base_high],0
3647
	cmp	byte [esi],80h
3648
	jne	elf64_exe_base_ok
3649
	lods	word [esi]
3650
	cmp	ah,'('
3651
	jne	invalid_argument
3652
	cmp	byte [esi],'.'
3653
	je	invalid_value
3654
	push	edx
3655
	call	get_qword_value
3656
	cmp	[value_type],0
3657
	jne	invalid_use_of_symbol
3658
	mov	[image_base],eax
3659
	mov	[image_base_high],edx
3660
	pop	edx
3661
      elf64_exe_base_ok:
31 halyavin 3662
	mov	byte [edx+10h],2
157 heavyiron 3663
	mov	byte [edx+36h],38h
3664
	mov	ebx,edi
3665
	mov	ecx,38h shr 2
3666
	cmp	[current_pass],0
3667
	je	init_elf64_segments
3668
	imul	ecx,[number_of_sections]
3669
      init_elf64_segments:
109 heavyiron 3670
	xor	eax,eax
157 heavyiron 3671
	rep	stos dword [edi]
3672
	mov	[number_of_sections],0
3673
	mov	byte [ebx],1
3674
	mov	word [ebx+30h],1000h
3675
	mov	byte [ebx+4],111b
3676
	push	edx
3677
	mov	eax,edi
3678
	sub	eax,[code_start]
3679
	mov	[ebx+8],eax
3680
	xor	edx,edx
3681
	add	eax,[image_base]
3682
	adc	edx,[image_base_high]
3683
	mov	[ebx+10h],eax
3684
	mov	[ebx+10h+4],edx
3685
	mov	[ebx+18h],eax
3686
	mov	[ebx+18h+4],edx
3687
	pop	ebx
3688
	mov	[ebx+18h],eax
3689
	mov	[ebx+18h+4],edx
3690
	not	eax
3691
	not	edx
3692
	add	eax,1
3693
	adc	edx,0
3694
	add	eax,edi
3695
	adc	edx,0
3696
	mov	dword [org_origin],eax
3697
	mov	dword [org_origin+4],edx
3698
	mov	[org_registers],0
3699
	mov	[org_start],edi
3700
	mov	[symbols_stream],edi
3701
	jmp	format_defined
607 heavyiron 3702
elf_entry:
31 halyavin 3703
	lods	byte [esi]
157 heavyiron 3704
	cmp	al,'('
3705
	jne	invalid_argument
3706
	cmp	byte [esi],'.'
3707
	je	invalid_value
3708
	test	[format_flags],8
3709
	jnz	elf64_entry
3710
	call	get_dword_value
3711
	cmp	[value_type],0
3712
	jne	invalid_use_of_symbol
3713
	mov	edx,[code_start]
3714
	mov	[edx+18h],eax
3715
	jmp	instruction_assembled
3716
      elf64_entry:
31 halyavin 3717
	call	get_qword_value
157 heavyiron 3718
	cmp	[value_type],0
3719
	jne	invalid_use_of_symbol
3720
	mov	ebx,[code_start]
3721
	mov	[ebx+18h],eax
3722
	mov	[ebx+1Ch],edx
3723
	jmp	instruction_assembled
3724
elf_segment:
109 heavyiron 3725
	bt	[format_flags],0
157 heavyiron 3726
	jnc	illegal_instruction
3727
	test	[format_flags],8
3728
	jnz	elf64_segment
3729
	call	close_elf_segment
3730
	push	eax
3731
	mov	ebx,[number_of_sections]
3732
	shl	ebx,5
3733
	add	ebx,[code_start]
3734
	add	ebx,34h
3735
	cmp	ebx,[symbols_stream]
3736
	jb	new_elf_segment
3737
	mov	ebx,[symbols_stream]
3738
	sub	ebx,20h
3739
	push	edi
3740
	mov	edi,ebx
3741
	mov	ecx,20h shr 2
3742
	xor	eax,eax
3743
	rep	stos dword [edi]
3744
	pop	edi
3745
	or	[next_pass_needed],-1
3746
      new_elf_segment:
109 heavyiron 3747
	mov	byte [ebx],1
157 heavyiron 3748
	mov	word [ebx+1Ch],1000h
3749
      elf_segment_flags:
109 heavyiron 3750
	cmp	byte [esi],19h
157 heavyiron 3751
	jne	elf_segment_flags_ok
3752
	lods	word [esi]
3753
	sub	ah,28
3754
	jbe	invalid_argument
3755
	cmp	ah,1
3756
	je	mark_elf_segment_flag
3757
	cmp	ah,3
3758
	ja	invalid_argument
3759
	xor	ah,1
3760
	cmp	ah,2
3761
	je	mark_elf_segment_flag
3762
	inc	ah
3763
      mark_elf_segment_flag:
109 heavyiron 3764
	test	[ebx+18h],ah
157 heavyiron 3765
	jnz	setting_already_specified
3766
	or	[ebx+18h],ah
3767
	jmp	elf_segment_flags
3768
      elf_segment_flags_ok:
109 heavyiron 3769
	mov	eax,edi
157 heavyiron 3770
	sub	eax,[code_start]
3771
	mov	[ebx+4],eax
3772
	pop	edx
3773
	and	eax,0FFFh
3774
	add	edx,eax
3775
	mov	[ebx+8],edx
3776
	mov	[ebx+0Ch],edx
3777
	mov	eax,edx
3778
	xor	edx,edx
3779
	not	eax
3780
	not	edx
3781
	add	eax,1
3782
	adc	edx,0
3783
	add	eax,edi
3784
	adc	edx,0
3785
	mov	dword [org_origin],eax
3786
	mov	dword [org_origin+4],edx
3787
	mov	[org_registers],0
3788
	mov	[org_start],edi
3789
	inc	[number_of_sections]
3790
	jmp	instruction_assembled
3791
      close_elf_segment:
109 heavyiron 3792
	cmp	[number_of_sections],0
157 heavyiron 3793
	jne	finish_elf_segment
3794
	cmp	edi,[symbols_stream]
3795
	jne	first_elf_segment_ok
3796
	push	edi
3797
	mov	edi,[code_start]
3798
	add	edi,34h
3799
	mov	ecx,20h shr 2
3800
	xor	eax,eax
3801
	rep	stos dword [edi]
3802
	pop	edi
3803
	mov	eax,[image_base]
3804
	ret
3805
      first_elf_segment_ok:
109 heavyiron 3806
	inc	[number_of_sections]
157 heavyiron 3807
      finish_elf_segment:
109 heavyiron 3808
	mov	ebx,[number_of_sections]
157 heavyiron 3809
	dec	ebx
3810
	shl	ebx,5
3811
	add	ebx,[code_start]
3812
	add	ebx,34h
3813
	mov	eax,edi
3814
	sub	eax,[code_start]
3815
	sub	eax,[ebx+4]
3816
	mov	edx,edi
3817
	cmp	edi,[undefined_data_end]
3818
	jne	elf_segment_size_ok
3819
	mov	edi,[undefined_data_start]
3820
      elf_segment_size_ok:
109 heavyiron 3821
	mov	[ebx+14h],eax
157 heavyiron 3822
	add	eax,edi
3823
	sub	eax,edx
3824
	mov	[ebx+10h],eax
3825
	mov	eax,[ebx+8]
3826
	add	eax,[ebx+14h]
3827
	add	eax,0FFFh
3828
	and	eax,not 0FFFh
3829
	ret
3830
      elf64_segment:
109 heavyiron 3831
	call	close_elf64_segment
157 heavyiron 3832
	push	eax edx
3833
	mov	ebx,[number_of_sections]
3834
	imul	ebx,38h
3835
	add	ebx,[code_start]
3836
	add	ebx,40h
3837
	cmp	ebx,[symbols_stream]
3838
	jb	new_elf64_segment
3839
	mov	ebx,[symbols_stream]
3840
	sub	ebx,38h
3841
	push	edi
3842
	mov	edi,ebx
3843
	mov	ecx,38h shr 2
3844
	xor	eax,eax
3845
	rep	stos dword [edi]
3846
	pop	edi
3847
	or	[next_pass_needed],-1
3848
      new_elf64_segment:
109 heavyiron 3849
	mov	byte [ebx],1
157 heavyiron 3850
	mov	word [ebx+30h],1000h
3851
      elf64_segment_flags:
109 heavyiron 3852
	cmp	byte [esi],19h
157 heavyiron 3853
	jne	elf64_segment_flags_ok
3854
	lods	word [esi]
3855
	sub	ah,28
3856
	jbe	invalid_argument
3857
	cmp	ah,1
3858
	je	mark_elf64_segment_flag
3859
	cmp	ah,3
3860
	ja	invalid_argument
3861
	xor	ah,1
3862
	cmp	ah,2
3863
	je	mark_elf64_segment_flag
3864
	inc	ah
3865
      mark_elf64_segment_flag:
109 heavyiron 3866
	test	[ebx+4],ah
157 heavyiron 3867
	jnz	setting_already_specified
3868
	or	[ebx+4],ah
3869
	jmp	elf64_segment_flags
3870
      elf64_segment_flags_ok:
109 heavyiron 3871
	mov	ecx,edi
157 heavyiron 3872
	sub	ecx,[code_start]
3873
	mov	[ebx+8],ecx
3874
	pop	edx eax
3875
	and	ecx,0FFFh
3876
	add	eax,ecx
3877
	adc	edx,0
3878
	mov	[ebx+10h],eax
3879
	mov	[ebx+10h+4],edx
3880
	mov	[ebx+18h],eax
3881
	mov	[ebx+18h+4],edx
3882
	not	eax
3883
	not	edx
3884
	add	eax,1
3885
	adc	edx,0
3886
	add	eax,edi
3887
	adc	edx,0
3888
	mov	dword [org_origin],eax
3889
	mov	dword [org_origin+4],edx
3890
	mov	[org_registers],0
3891
	mov	[org_start],edi
3892
	inc	[number_of_sections]
3893
	jmp	instruction_assembled
3894
      close_elf64_segment:
109 heavyiron 3895
	cmp	[number_of_sections],0
157 heavyiron 3896
	jne	finish_elf64_segment
3897
	cmp	edi,[symbols_stream]
3898
	jne	first_elf64_segment_ok
3899
	push	edi
3900
	mov	edi,[code_start]
3901
	add	edi,40h
3902
	mov	ecx,38h shr 2
3903
	xor	eax,eax
3904
	rep	stos dword [edi]
3905
	pop	edi
3906
	mov	eax,[image_base]
3907
	mov	edx,[image_base_high]
3908
	ret
3909
      first_elf64_segment_ok:
109 heavyiron 3910
	inc	[number_of_sections]
157 heavyiron 3911
      finish_elf64_segment:
109 heavyiron 3912
	mov	ebx,[number_of_sections]
157 heavyiron 3913
	dec	ebx
3914
	imul	ebx,38h
3915
	add	ebx,[code_start]
3916
	add	ebx,40h
3917
	mov	eax,edi
3918
	sub	eax,[code_start]
3919
	sub	eax,[ebx+8]
3920
	mov	edx,edi
3921
	cmp	edi,[undefined_data_end]
3922
	jne	elf64_segment_size_ok
3923
	mov	edi,[undefined_data_start]
3924
      elf64_segment_size_ok:
109 heavyiron 3925
	mov	[ebx+28h],eax
157 heavyiron 3926
	add	eax,edi
3927
	sub	eax,edx
3928
	mov	[ebx+20h],eax
3929
	mov	eax,[ebx+10h]
3930
	mov	edx,[ebx+10h+4]
3931
	add	eax,[ebx+28h]
3932
	adc	edx,0
3933
	sub	eax,1
3934
	sbb	edx,0
3935
	shrd	eax,edx,12
3936
	shr	edx,12
3937
	add	eax,1
3938
	adc	edx,0
3939
	shld	edx,eax,12
3940
	shl	eax,12
3941
	ret
3942
close_elf_exe:
31 halyavin 3943
	test	[format_flags],8
157 heavyiron 3944
	jnz	close_elf64_exe
3945
	call	close_elf_segment
3946
	mov	edx,[code_start]
3947
	mov	eax,[number_of_sections]
3948
	mov	byte [edx+1Ch],34h
3949
	mov	[edx+2Ch],ax
3950
	shl	eax,5
3951
	add	eax,edx
3952
	add	eax,34h
3953
	cmp	eax,[symbols_stream]
3954
	je	elf_exe_ok
3955
	or	[next_pass_needed],-1
3956
      elf_exe_ok:
31 halyavin 3957
	ret
157 heavyiron 3958
      close_elf64_exe:
31 halyavin 3959
	call	close_elf64_segment
157 heavyiron 3960
	mov	edx,[code_start]
3961
	mov	eax,[number_of_sections]
3962
	mov	byte [edx+20h],40h
3963
	mov	[edx+38h],ax
3964
	imul	eax,38h
3965
	add	eax,edx
3966
	add	eax,40h
3967
	cmp	eax,[symbols_stream]
3968
	je	elf64_exe_ok
3969
	or	[next_pass_needed],-1
3970
      elf64_exe_ok:
31 halyavin 3971
	ret
157 heavyiron 3972
992 heavyiron 3973
 
3974
	mov	ebx,[code_start]
3975
	mov	dword [ebx],'fas'+1Ah shl 24
1115 heavyiron 3976
	mov	dword [ebx+4],VERSION_MAJOR + VERSION_MINOR shl 8 + 38h shl 16
992 heavyiron 3977
	add	ebx,38h
3978
	mov	edi,ebx
3979
	mov	dword [ebx-38h+10h],38h
3980
	mov	dword [ebx-38h+8],0
3981
	mov	esi,[input_file]
3982
	call	copy_asciiz
3983
	cmp	edi,[display_buffer]
3984
	jae	out_of_memory
3985
	mov	eax,edi
3986
	sub	eax,ebx
3987
	mov	[ebx-38h+0Ch],eax
3988
	mov	esi,[output_file]
3989
	call	copy_asciiz
3990
	cmp	edi,[display_buffer]
3991
	jae	out_of_memory
3992
	mov	edx,[symbols_stream]
3993
	mov	ebp,[free_additional_memory]
3994
	mov	[number_of_sections],0
3995
	cmp	[output_format],4
3996
	je	prepare_strings_table
3997
	cmp	[output_format],5
3998
	jne	strings_table_ready
3999
	bt	[format_flags],0
4000
	jc	strings_table_ready
4001
      prepare_strings_table:
4002
	cmp	edx,ebp
4003
	je	strings_table_ready
4004
	mov	al,[edx]
4005
	test	al,al
4006
	jz	prepare_string
4007
	cmp	al,80h
4008
	je	prepare_string
4009
	add	edx,0Ch
4010
	cmp	al,0C0h
4011
	jne	prepare_strings_table
4012
	add	edx,4
4013
	jmp	prepare_strings_table
4014
      prepare_string:
4015
	mov	esi,edi
4016
	sub	esi,ebx
4017
	xchg	esi,[edx+4]
4018
	test	al,al
4019
	jz	prepare_section_string
4020
	or	dword [edx+4],1 shl 31
4021
	add	edx,0Ch
4022
      prepare_external_string:
4023
	mov	ecx,[esi]
4024
	add	esi,4
4025
	rep	movs byte [edi],[esi]
4026
	mov	byte [edi],0
4027
	inc	edi
4028
	cmp	edi,[display_buffer]
4029
	jae	out_of_memory
4030
	jmp	prepare_strings_table
4031
      prepare_section_string:
4032
	mov	ecx,[number_of_sections]
4033
	mov	eax,ecx
4034
	inc	eax
4035
	mov	[number_of_sections],eax
4036
	xchg	eax,[edx+4]
4037
	shl	ecx,2
4038
	add	ecx,[free_additional_memory]
4039
	mov	[ecx],eax
4040
	add	edx,20h
4041
	test	esi,esi
4042
	jz	prepare_default_section_string
4043
	cmp	[output_format],5
4044
	jne	prepare_external_string
4045
	bt	[format_flags],0
4046
	jc	prepare_external_string
4047
	mov	esi,[esi]
4048
	add	esi,[resource_data]
4049
      copy_elf_section_name:
4050
	lods	byte [esi]
4051
	cmp	edi,[display_buffer]
4052
	jae	out_of_memory
4053
	stos	byte [edi]
4054
	test	al,al
4055
	jnz	copy_elf_section_name
4056
	jmp	prepare_strings_table
4057
      prepare_default_section_string:
4058
	mov	eax,'.fla'
4059
	stos	dword [edi]
4060
	mov	ax,'t'
4061
	stos	word [edi]
4062
	cmp	edi,[display_buffer]
4063
	jae	out_of_memory
4064
	jmp	prepare_strings_table
4065
      strings_table_ready:
4066
	mov	edx,[display_buffer]
4067
	mov	ebp,[memory_end]
4068
	sub	ebp,[labels_list]
4069
	add	ebp,edx
4070
      prepare_labels_dump:
4071
	cmp	edx,ebp
4072
	je	labels_dump_ok
4073
	mov	eax,[edx+24]
4074
	test	eax,eax
4075
	jz	label_dump_name_ok
4076
	cmp	eax,[memory_start]
4077
	jb	label_name_outside_source
4078
	cmp	eax,[source_start]
4079
	ja	label_name_outside_source
4080
	sub	eax,[memory_start]
4081
	dec	eax
4082
	mov	[edx+24],eax
4083
	jmp	label_dump_name_ok
4084
      label_name_outside_source:
4085
	mov	esi,eax
4086
	mov	eax,edi
4087
	sub	eax,ebx
4088
	or	eax,1 shl 31
4089
	mov	[edx+24],eax
4090
	movzx	ecx,byte [esi-1]
4091
	lea	eax,[edi+ecx+1]
4092
	cmp	edi,[display_buffer]
4093
	jae	out_of_memory
4094
	rep	movsb
4095
	xor	al,al
4096
	stosb
4097
      label_dump_name_ok:
4098
	mov	eax,[edx+28]
4099
	test	eax,eax
4100
	jz	label_dump_line_ok
4101
	sub	eax,[memory_start]
4102
	mov	[edx+28],eax
4103
      label_dump_line_ok:
4104
	mov	eax,[edx+20]
4105
	test	eax,eax
4106
	jz	base_symbol_for_label_ok
4107
	cmp	eax,[symbols_stream]
4108
	mov	eax,[eax+4]
4109
	jae	base_symbol_for_label_ok
4110
	xor	eax,eax
4111
      base_symbol_for_label_ok:
4112
	mov	[edx+20],eax
4113
	add	edx,LABEL_STRUCTURE_SIZE
1053 heavyiron 4114
	jmp	prepare_labels_dump
992 heavyiron 4115
      labels_dump_ok:
4116
	mov	eax,edi
4117
	sub	eax,ebx
4118
	mov	[ebx-38h+14h],eax
4119
	add	eax,38h
4120
	mov	[ebx-38h+18h],eax
4121
	mov	ecx,[memory_end]
4122
	sub	ecx,[labels_list]
4123
	mov	[ebx-38h+1Ch],ecx
4124
	add	eax,ecx
4125
	mov	[ebx-38h+20h],eax
4126
	mov	ecx,[source_start]
4127
	sub	ecx,[memory_start]
4128
	mov	[ebx-38h+24h],ecx
4129
	add	eax,ecx
4130
	mov	[ebx-38h+28h],eax
4131
	mov	eax,[number_of_sections]
4132
	shl	eax,2
1054 heavyiron 4133
	mov	[ebx-38h+34h],eax
992 heavyiron 4134
	mov	esi,[memory_start]
4135
      prepare_preprocessed_source:
4136
	cmp	esi,[source_start]
4137
	jae	preprocessed_source_ok
4138
	mov	eax,[memory_start]
4139
	mov	edx,[input_file]
4140
	cmp	[esi],edx
4141
	jne	line_not_from_main_input
4142
	mov	[esi],eax
4143
      line_not_from_main_input:
4144
	sub	[esi],eax
4145
	test	byte [esi+7],1 shl 7
4146
	jz	prepare_next_preprocessed_line
4147
	sub	[esi+8],eax
4148
	sub	[esi+12],eax
4149
      prepare_next_preprocessed_line:
4150
	add	esi,16
4151
	call	skip_foreign_line
4152
	jmp	prepare_preprocessed_source
4153
      preprocessed_source_ok:
4154
	mov	esi,[labels_list]
4155
	mov	ebp,edi
4156
      make_lines_dump:
4157
	cmp	esi,[display_buffer]
4158
	je	lines_dump_ok
4159
	mov	eax,[esi-4]
4160
	mov	ecx,[esi-8]
4161
	sub	esi,8
4162
	sub	esi,ecx
4163
	cmp	eax,1
4164
	jne	make_lines_dump
4165
	mov	eax,[esi+4]
4166
	sub	eax,[code_start]
4167
	add	eax,[headers_size]
4168
	cmp	byte [esi+1Ah],0
4169
	je	store_offset
4170
	xor	eax,eax
4171
      store_offset:
4172
	stos	dword [edi]
4173
	mov	eax,[esi]
4174
	sub	eax,[memory_start]
4175
	stos	dword [edi]
4176
	mov	eax,[esi+4]
4177
	xor	edx,edx
4178
	sub	eax,[esi+8]
4179
	sbb	edx,[esi+8+4]
4180
	stos	dword [edi]
4181
	mov	eax,edx
4182
	stos	dword [edi]
4183
	mov	eax,[esi+10h]
4184
	stos	dword [edi]
4185
	mov	eax,[esi+14h]
4186
	test	eax,eax
4187
	jz	base_symbol_for_line_ok
4188
	cmp	eax,[symbols_stream]
4189
	mov	eax,[eax+4]
4190
	jae	base_symbol_for_line_ok
4191
	xor	eax,eax
4192
      base_symbol_for_line_ok:
4193
	stos	dword [edi]
4194
	mov	eax,[esi+18h]
4195
	and	eax,001FFFFh
4196
	stos	dword [edi]
4197
	jmp	make_lines_dump
4198
      lines_dump_ok:
4199
	mov	edx,edi
4200
	mov	eax,[current_offset]
4201
	sub	eax,[code_start]
4202
	add	eax,[headers_size]
4203
	stos	dword [edi]
4204
	mov	ecx,edi
4205
	sub	ecx,ebx
4206
	sub	ecx,[ebx-38h+14h]
4207
	mov	[ebx-38h+2Ch],ecx
4208
	add	ecx,[ebx-38h+28h]
4209
	mov	[ebx-38h+30h],ecx
4210
      find_inexisting_offsets:
4211
	sub	edx,1Ch
4212
	cmp	edx,ebp
4213
	jb	write_symbols
4214
	test	byte [edx+1Ah],1
4215
	jnz	find_inexisting_offsets
4216
	cmp	eax,[edx]
4217
	jb	correct_inexisting_offset
4218
	mov	eax,[edx]
4219
	jmp	find_inexisting_offsets
4220
      correct_inexisting_offset:
4221
	mov	dword [edx],0
4222
	or	byte [edx+1Ah],2
4223
	jmp	find_inexisting_offsets
4224
      write_symbols:
4225
	mov	edx,[symbols_file]
4226
	call	create
4227
	jc	write_failed
4228
	mov	edx,[code_start]
4229
	mov	ecx,[edx+14h]
4230
	add	ecx,38h
4231
	call	write
4232
	mov	edx,[display_buffer]
4233
	mov	ecx,[memory_end]
4234
	sub	ecx,[labels_list]
4235
	call	write
4236
	mov	edx,[memory_start]
4237
	mov	ecx,[source_start]
4238
	sub	ecx,edx
4239
	call	write
4240
	mov	edx,ebp
4241
	mov	ecx,edi
4242
	sub	ecx,edx
4243
	call	write
4244
	mov	edx,[free_additional_memory]
4245
	mov	ecx,[number_of_sections]
4246
	shl	ecx,2
4247
	call	write
4248
	call	close
4249
	ret
4250