Subversion Repositories Kolibri OS

Rev

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