Subversion Repositories Kolibri OS

Rev

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