Subversion Repositories Kolibri OS

Rev

Rev 624 | 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_coff_public
692 heavyiron 2669
	mov	cx,[current_pass]
157 heavyiron 2670
	cmp	cx,[eax+16]
2671
	jne	undefined_coff_public
692 heavyiron 2672
	mov	cl,[eax+11]
157 heavyiron 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
      undefined_coff_public:
692 heavyiron 2681
	mov	eax,[eax+24]
2682
	mov	[error_info],eax
2683
	jmp	undefined_symbol
2684
      check_64bit_public_symbol:
31 halyavin 2685
	cmp	cl,4
157 heavyiron 2686
	jne	invalid_use_of_symbol
2687
      public_symbol_type_ok:
31 halyavin 2688
	mov	ecx,[eax+20]
157 heavyiron 2689
	cmp	byte [ecx],80h
624 heavyiron 2690
	je	alias_symbol
157 heavyiron 2691
	cmp	byte [ecx],0
2692
	jne	invalid_use_of_symbol
2693
	mov	cx,[ecx+1Eh]
2694
	mov	[ebx+0Ch],cx
2695
      public_symbol_section_ok:
31 halyavin 2696
	cmp	dword [eax+4],0
157 heavyiron 2697
	je	store_public_symbol
2698
	cmp	dword [eax+4],-1
2699
	jne	value_out_of_range
2700
	bt	dword [eax],31
2701
	jnc	value_out_of_range
2702
      store_public_symbol:
31 halyavin 2703
	mov	eax,[eax]
157 heavyiron 2704
	mov	[ebx+8],eax
2705
	mov	al,2
624 heavyiron 2706
	cmp	byte [esi],0C0h
2707
	je	store_symbol_class
2708
	inc	al
2709
	cmp	byte [esi],0C1h
2710
	je	store_symbol_class
2711
	mov	al,105
2712
      store_symbol_class:
2713
	mov	byte [ebx+10h],al
2714
	add	esi,10h
157 heavyiron 2715
	add	ebx,12h
2716
	jmp	make_symbols_table
2717
      alias_symbol:
31 halyavin 2718
	bt	[format_flags],0
157 heavyiron 2719
	jnc	invalid_use_of_symbol
2720
	mov	ecx,[eax]
2721
	or	ecx,[eax+4]
2722
	jnz	invalid_use_of_symbol
2723
	mov	byte [ebx+10h],69h
2724
	mov	byte [ebx+11h],1
2725
	add	ebx,12h
2726
	mov	ecx,[eax+20]
2727
	mov	ecx,[ecx]
2728
	shr	ecx,8
2729
	mov	[ebx],ecx
2730
	mov	byte [ebx+4],3
2731
	add	esi,10h
2732
	add	ebx,12h
2733
	jmp	make_symbols_table
2734
      public_constant:
31 halyavin 2735
	mov	word [ebx+0Ch],0FFFFh
157 heavyiron 2736
	jmp	public_symbol_section_ok
2737
      symbols_table_ok:
31 halyavin 2738
	mov	eax,edi
157 heavyiron 2739
	sub	eax,edx
2740
	mov	[edx],eax
2741
	sub	edi,[code_start]
2742
	mov	[code_size],edi
2743
	mov	[written_size],0
2744
	mov	edx,[output_file]
2745
	call	create
2746
	jc	write_failed
2747
	mov	edx,[free_additional_memory]
2748
	pop	ecx
2749
	add	[written_size],ecx
2750
	call	write
2751
	jc	write_failed
2752
	jmp	write_output
2753
      store_symbol_name:
31 halyavin 2754
	push	esi
157 heavyiron 2755
	mov	esi,[esi+4]
2756
	or	esi,esi
2757
	jz	default_name
2758
	lods	dword [esi]
2759
	mov	ecx,eax
2760
	cmp	ecx,8
2761
	ja	add_string
2762
	push	edi
2763
	mov	edi,ebx
2764
	rep	movs byte [edi],[esi]
2765
	pop	edi esi
2766
	ret
2767
      default_name:
31 halyavin 2768
	mov	dword [ebx],'.fla'
157 heavyiron 2769
	mov	dword [ebx+4],'t'
2770
	pop	esi
2771
	ret
2772
      add_string:
31 halyavin 2773
	mov	eax,edi
157 heavyiron 2774
	sub	eax,edx
2775
	mov	[ebx+4],eax
2776
	inc	ecx
2777
	rep	movs byte [edi],[esi]
2778
	pop	esi
2779
	ret
2780
31 halyavin 2781
 
2782
	test	[format_flags],8
157 heavyiron 2783
	jnz	format_elf64
2784
	mov	edx,edi
2785
	mov	ecx,34h shr 2
2786
	lea	eax,[edi+ecx*4]
2787
	cmp	eax,[display_buffer]
2788
	jae	out_of_memory
2789
	xor	eax,eax
2790
	rep	stos dword [edi]
2791
	mov	dword [edx],7Fh + 'ELF' shl 8
2792
	mov	al,1
2793
	mov	[edx+4],al
2794
	mov	[edx+5],al
2795
	mov	[edx+6],al
2796
	mov	[edx+14h],al
2797
	mov	byte [edx+12h],3
2798
	mov	byte [edx+28h],34h
2799
	mov	byte [edx+2Eh],28h
2800
	mov	[code_type],32
2801
	cmp	word [esi],1D19h
2802
	je	format_elf_exe
2803
	mov	[labels_type],2
2804
      elf_header_ok:
31 halyavin 2805
	mov	byte [edx+10h],1
157 heavyiron 2806
	mov	eax,[additional_memory]
2807
	mov	[symbols_stream],eax
2808
	mov	ebx,eax
2809
	add	eax,20h
2810
	cmp	eax,[structures_buffer]
2811
	jae	out_of_memory
2812
	mov	[free_additional_memory],eax
2813
	xor	eax,eax
2814
	mov	[current_section],ebx
2815
	mov	[number_of_sections],eax
2816
	mov	dword [org_origin],edi
2817
	mov	dword [org_origin+4],eax
2818
	mov	[org_registers],eax
2819
	mov	[org_start],edi
2820
	mov	[org_symbol],ebx
2821
	mov	[ebx],al
2822
	mov	[ebx+4],eax
2823
	mov	[ebx+8],edi
2824
	mov	al,111b
2825
	mov	[ebx+14h],eax
2826
	mov	al,4
2827
	mov	[ebx+10h],eax
2828
	test	[format_flags],8
2829
	jz	format_defined
607 heavyiron 2830
	mov	byte [ebx+10h],8
157 heavyiron 2831
	jmp	format_defined
607 heavyiron 2832
      format_elf64:
31 halyavin 2833
	mov	edx,edi
157 heavyiron 2834
	mov	ecx,40h shr 2
2835
	lea	eax,[edi+ecx*4]
2836
	cmp	eax,[display_buffer]
2837
	jae	out_of_memory
2838
	xor	eax,eax
2839
	rep	stos dword [edi]
2840
	mov	dword [edx],7Fh + 'ELF' shl 8
2841
	mov	al,1
2842
	mov	[edx+5],al
2843
	mov	[edx+6],al
2844
	mov	[edx+14h],al
2845
	mov	byte [edx+4],2
2846
	mov	byte [edx+12h],62
2847
	mov	byte [edx+34h],40h
2848
	mov	byte [edx+3Ah],40h
2849
	mov	[code_type],64
2850
	cmp	word [esi],1D19h
2851
	je	format_elf64_exe
2852
	mov	[labels_type],4
2853
	jmp	elf_header_ok
2854
elf_section:
31 halyavin 2855
	bt	[format_flags],0
157 heavyiron 2856
	jc	illegal_instruction
2857
	call	close_coff_section
2858
	mov	ebx,[free_additional_memory]
2859
	lea	eax,[ebx+20h]
2860
	cmp	eax,[structures_buffer]
2861
	jae	out_of_memory
2862
	mov	[free_additional_memory],eax
2863
	mov	[current_section],ebx
2864
	inc	word [number_of_sections]
2865
	jz	format_limitations_exceeded
2866
	xor	eax,eax
2867
	mov	[ebx],al
2868
	mov	[ebx+8],edi
2869
	mov	dword [org_origin],edi
2870
	mov	dword [org_origin+4],0
2871
	mov	[org_registers],0
2872
	mov	[org_start],edi
2873
	mov	[org_symbol],ebx
2874
	test	[format_flags],8
2875
	jnz	elf64_labels_type
2876
	mov	[labels_type],2
2877
	jmp	elf_labels_type_ok
2878
      elf64_labels_type:
31 halyavin 2879
	mov	[labels_type],4
157 heavyiron 2880
      elf_labels_type_ok:
31 halyavin 2881
	mov	[ebx+10h],eax
157 heavyiron 2882
	mov	al,10b
2883
	mov	[ebx+14h],eax
2884
	lods	word [esi]
2885
	cmp	ax,'('
2886
	jne	invalid_argument
2887
	mov	[ebx+4],esi
2888
	mov	ecx,[esi]
2889
	lea	esi,[esi+4+ecx+1]
2890
      elf_section_flags:
31 halyavin 2891
	cmp	byte [esi],1Ch
157 heavyiron 2892
	je	elf_section_alignment
2893
	cmp	byte [esi],19h
2894
	jne	elf_section_settings_ok
2895
	inc	esi
2896
	lods	byte [esi]
2897
	sub	al,28
2898
	xor	al,11b
2899
	test	al,not 10b
2900
	jnz	invalid_argument
2901
	mov	cl,al
2902
	mov	al,1
2903
	shl	al,cl
2904
	test	byte [ebx+14h],al
2905
	jnz	setting_already_specified
2906
	or	byte [ebx+14h],al
2907
	jmp	elf_section_flags
2908
      elf_section_alignment:
31 halyavin 2909
	inc	esi
157 heavyiron 2910
	lods	byte [esi]
2911
	or	al,al
2912
	jnz	invalid_argument
2913
	lods	byte [esi]
2914
	cmp	al,'('
2915
	jne	invalid_argument
2916
	cmp	byte [esi],'.'
2917
	je	invalid_value
2918
	push	ebx
2919
	call	get_dword_value
2920
	pop	ebx
2921
	cmp	[value_type],0
2922
	jne	invalid_use_of_symbol
2923
	mov	edx,eax
2924
	dec	edx
2925
	test	eax,edx
2926
	jnz	invalid_value
2927
	or	eax,eax
2928
	jz	invalid_value
2929
	xchg	[ebx+10h],eax
2930
	or	eax,eax
2931
	jnz	setting_already_specified
2932
	jmp	elf_section_flags
2933
      elf_section_settings_ok:
31 halyavin 2934
	cmp	dword [ebx+10h],0
157 heavyiron 2935
	jne	instruction_assembled
2936
	mov	dword [ebx+10h],4
2937
	test	[format_flags],8
2938
	jz	instruction_assembled
2939
	mov	byte [ebx+10h],8
2940
	jmp	instruction_assembled
2941
mark_elf_relocation:
31 halyavin 2942
	cmp	[value_type],3
157 heavyiron 2943
	je	elf_relocation_relative
2944
	cmp	[value_type],7
174 heavyiron 2945
	je	elf_relocation_relative
2946
	push	ebx eax
157 heavyiron 2947
	cmp	[value_type],5
174 heavyiron 2948
	je	elf_gotoff_relocation
157 heavyiron 2949
	ja	invalid_use_of_symbol
174 heavyiron 2950
	mov	al,1			; R_386_32 / R_AMD64_64
2951
	test	[format_flags],8
157 heavyiron 2952
	jz	coff_relocation
2953
	cmp	[value_type],4
2954
	je	coff_relocation
2955
	mov	al,11			; R_AMD64_32S
174 heavyiron 2956
	jmp	coff_relocation
157 heavyiron 2957
      elf_gotoff_relocation:
109 heavyiron 2958
	test	[format_flags],8
174 heavyiron 2959
	jnz	invalid_use_of_symbol
2960
	mov	al,9			; R_386_GOTOFF
2961
	jmp	coff_relocation
157 heavyiron 2962
      elf_relocation_relative:
31 halyavin 2963
	cmp	[labels_type],0
157 heavyiron 2964
	je	invalid_use_of_symbol
2965
	push	ebx
2966
	mov	ebx,[current_section]
2967
	mov	ebx,[ebx+8]
2968
	sub	ebx,edi
2969
	sub	eax,ebx
2970
	push	eax
2971
	mov	al,2			; R_386_PC32 / R_AMD64_PC32
174 heavyiron 2972
	cmp	[value_type],3
2973
	je	coff_relocation
2974
	mov	al,4			; R_386_PLT32 / R_AMD64_PLT32
2975
	jmp	coff_relocation
157 heavyiron 2976
close_elf:
31 halyavin 2977
	bt	[format_flags],0
157 heavyiron 2978
	jc	close_elf_exe
2979
	call	close_coff_section
2980
	cmp	[next_pass_needed],0
2981
	je	elf_closed
2982
	mov	eax,[symbols_stream]
2983
	mov	[free_additional_memory],eax
2984
      elf_closed:
31 halyavin 2985
	ret
157 heavyiron 2986
elf_formatter:
31 halyavin 2987
	push	edi
157 heavyiron 2988
	call	prepare_default_section
2989
	mov	esi,[symbols_stream]
2990
	mov	edi,[free_additional_memory]
2991
	xor	eax,eax
2992
	mov	ecx,4
2993
	rep	stos dword [edi]
2994
	test	[format_flags],8
2995
	jz	find_first_section
2996
	mov	ecx,2
2997
	rep	stos dword [edi]
2998
      find_first_section:
31 halyavin 2999
	mov	al,[esi]
157 heavyiron 3000
	or	al,al
3001
	jz	first_section_found
3002
	cmp	al,0C0h
624 heavyiron 3003
	jb	skip_other_symbol
3004
	add	esi,4
157 heavyiron 3005
      skip_other_symbol:
31 halyavin 3006
	add	esi,0Ch
157 heavyiron 3007
	jmp	find_first_section
3008
      first_section_found:
31 halyavin 3009
	mov	ebx,esi
157 heavyiron 3010
	mov	ebp,esi
3011
	add	esi,20h
3012
	xor	ecx,ecx
3013
	xor	edx,edx
3014
      find_next_section:
31 halyavin 3015
	cmp	esi,[free_additional_memory]
157 heavyiron 3016
	je	make_section_symbol
3017
	mov	al,[esi]
3018
	or	al,al
3019
	jz	make_section_symbol
3020
	cmp	al,0C0h
624 heavyiron 3021
	jae	skip_public
3022
	cmp	al,80h
157 heavyiron 3023
	jae	skip_extrn
624 heavyiron 3024
	or	byte [ebx+14h],40h
157 heavyiron 3025
      skip_extrn:
31 halyavin 3026
	add	esi,0Ch
157 heavyiron 3027
	jmp	find_next_section
3028
      skip_public:
31 halyavin 3029
	add	esi,10h
157 heavyiron 3030
	jmp	find_next_section
3031
      make_section_symbol:
31 halyavin 3032
	mov	eax,edi
157 heavyiron 3033
	xchg	eax,[ebx+4]
3034
	stos	dword [edi]
3035
	test	[format_flags],8
3036
	jnz	elf64_section_symbol
3037
	xor	eax,eax
3038
	stos	dword [edi]
3039
	stos	dword [edi]
3040
	call	store_section_index
3041
	jmp	section_symbol_ok
3042
      store_section_index:
31 halyavin 3043
	inc	ecx
157 heavyiron 3044
	mov	eax,ecx
3045
	shl	eax,8
3046
	mov	[ebx],eax
3047
	inc	dx
3048
	jz	format_limitations_exceeded
3049
	mov	eax,edx
3050
	shl	eax,16
3051
	mov	al,3
3052
	test	byte [ebx+14h],40h
3053
	jz	section_index_ok
3054
	or	ah,-1
3055
	inc	dx
3056
	jz	format_limitations_exceeded
3057
      section_index_ok:
31 halyavin 3058
	stos	dword [edi]
157 heavyiron 3059
	ret
3060
      elf64_section_symbol:
31 halyavin 3061
	call	store_section_index
157 heavyiron 3062
	xor	eax,eax
3063
	stos	dword [edi]
3064
	stos	dword [edi]
3065
	stos	dword [edi]
3066
	stos	dword [edi]
3067
      section_symbol_ok:
31 halyavin 3068
	mov	ebx,esi
157 heavyiron 3069
	add	esi,20h
3070
	cmp	ebx,[free_additional_memory]
3071
	jne	find_next_section
3072
	inc	dx
3073
	jz	format_limitations_exceeded
3074
	mov	[current_section],edx
3075
	mov	esi,[symbols_stream]
3076
      find_other_symbols:
31 halyavin 3077
	cmp	esi,[free_additional_memory]
157 heavyiron 3078
	je	elf_symbol_table_ok
3079
	mov	al,[esi]
3080
	or	al,al
3081
	jz	skip_section
3082
	cmp	al,0C0h
624 heavyiron 3083
	jae	make_public_symbol
3084
	cmp	al,80h
157 heavyiron 3085
	jae	make_extrn_symbol
624 heavyiron 3086
	add	esi,0Ch
157 heavyiron 3087
	jmp	find_other_symbols
3088
      skip_section:
31 halyavin 3089
	add	esi,20h
157 heavyiron 3090
	jmp	find_other_symbols
3091
      make_public_symbol:
31 halyavin 3092
	mov	eax,[esi+0Ch]
157 heavyiron 3093
	mov	[current_line],eax
3094
	cmp	byte [esi],0C0h
624 heavyiron 3095
	jne	invalid_argument
3096
	mov	ebx,[esi+8]
157 heavyiron 3097
	test	byte [ebx+8],1
3098
	jz	undefined_public
692 heavyiron 3099
	mov	ax,[current_pass]
157 heavyiron 3100
	cmp	ax,[ebx+16]
3101
	jne	undefined_public
692 heavyiron 3102
	mov	dl,[ebx+11]
157 heavyiron 3103
	or	dl,dl
3104
	jz	public_absolute
3105
	mov	eax,[ebx+20]
3106
	cmp	byte [eax],0
3107
	jne	invalid_use_of_symbol
3108
	mov	eax,[eax+4]
3109
	test	[format_flags],8
3110
	jnz	elf64_public
3111
	cmp	dl,2
3112
	jne	invalid_use_of_symbol
3113
	mov	dx,[eax+0Eh]
3114
	jmp	section_for_public_ok
3115
      undefined_public:
692 heavyiron 3116
	mov	eax,[ebx+24]
3117
	mov	[error_info],eax
3118
	jmp	undefined_symbol
3119
      elf64_public:
31 halyavin 3120
	cmp	dl,4
157 heavyiron 3121
	jne	invalid_use_of_symbol
3122
	mov	dx,[eax+6]
3123
	jmp	section_for_public_ok
3124
      public_absolute:
31 halyavin 3125
	mov	dx,0FFF1h
157 heavyiron 3126
      section_for_public_ok:
31 halyavin 3127
	mov	eax,[esi+4]
157 heavyiron 3128
	stos	dword [edi]
3129
	test	[format_flags],8
3130
	jnz	elf64_public_symbol
3131
	call	get_public_value
3132
	stos	dword [edi]
3133
	xor	eax,eax
3134
	mov	al,[ebx+10]
3135
	stos	dword [edi]
3136
	mov	eax,edx
3137
	shl	eax,16
3138
	mov	al,10h
3139
	cmp	byte [ebx+10],0
3140
	je	elf_public_function
3141
	or	al,1
3142
	jmp	store_elf_public_info
3143
      elf_public_function:
109 heavyiron 3144
	or	al,2
157 heavyiron 3145
      store_elf_public_info:
109 heavyiron 3146
	stos	dword [edi]
157 heavyiron 3147
	jmp	public_symbol_ok
3148
      elf64_public_symbol:
31 halyavin 3149
	mov	eax,edx
157 heavyiron 3150
	shl	eax,16
3151
	mov	al,10h
3152
	cmp	byte [ebx+10],0
3153
	je	elf64_public_function
3154
	or	al,1
3155
	jmp	store_elf64_public_info
3156
      elf64_public_function:
109 heavyiron 3157
	or	al,2
157 heavyiron 3158
      store_elf64_public_info:
109 heavyiron 3159
	stos	dword [edi]
157 heavyiron 3160
	call	get_public_value
3161
	stos	dword [edi]
3162
	xor	eax,eax
3163
	stos	dword [edi]
3164
	mov	al,[ebx+10]
3165
	stos	dword [edi]
3166
	xor	al,al
3167
	stos	dword [edi]
3168
      public_symbol_ok:
31 halyavin 3169
	inc	ecx
157 heavyiron 3170
	mov	eax,ecx
3171
	shl	eax,8
3172
	mov	al,0C0h
624 heavyiron 3173
	mov	[esi],eax
157 heavyiron 3174
	add	esi,10h
3175
	jmp	find_other_symbols
3176
      get_public_value:
31 halyavin 3177
	mov	eax,[ebx]
157 heavyiron 3178
	cmp	dword [ebx+4],0
3179
	je	public_value_ok
3180
	cmp	dword [ebx+4],-1
3181
	jne	value_out_of_range
3182
	bt	eax,31
3183
	jnc	value_out_of_range
3184
      public_value_ok:
31 halyavin 3185
	ret
157 heavyiron 3186
      make_extrn_symbol:
31 halyavin 3187
	mov	eax,[esi+4]
157 heavyiron 3188
	stos	dword [edi]
3189
	test	[format_flags],8
3190
	jnz	elf64_extrn_symbol
3191
	xor	eax,eax
3192
	stos	dword [edi]
3193
	mov	eax,[esi+8]
3194
	stos	dword [edi]
3195
	mov	eax,10h
3196
	stos	dword [edi]
3197
	jmp	extrn_symbol_ok
3198
      elf64_extrn_symbol:
31 halyavin 3199
	mov	eax,10h
157 heavyiron 3200
	stos	dword [edi]
3201
	xor	al,al
3202
	stos	dword [edi]
3203
	stos	dword [edi]
3204
	mov	eax,[esi+8]
3205
	stos	dword [edi]
3206
	xor	eax,eax
3207
	stos	dword [edi]
3208
      extrn_symbol_ok:
31 halyavin 3209
	inc	ecx
157 heavyiron 3210
	mov	eax,ecx
3211
	shl	eax,8
3212
	mov	al,80h
624 heavyiron 3213
	mov	[esi],eax
157 heavyiron 3214
	add	esi,0Ch
3215
	jmp	find_other_symbols
3216
      elf_symbol_table_ok:
31 halyavin 3217
	mov	edx,edi
157 heavyiron 3218
	mov	ebx,[free_additional_memory]
3219
	xor	al,al
3220
	stos	byte [edi]
3221
	add	edi,16
3222
	mov	[edx+1],edx
3223
	add	ebx,10h
3224
	test	[format_flags],8
3225
	jz	make_string_table
3226
	add	ebx,8
3227
      make_string_table:
31 halyavin 3228
	cmp	ebx,edx
157 heavyiron 3229
	je	elf_string_table_ok
3230
	test	[format_flags],8
3231
	jnz	make_elf64_string
3232
	cmp	byte [ebx+0Dh],0
3233
	je	rel_prefix_ok
3234
	mov	byte [ebx+0Dh],0
3235
	mov	eax,'.rel'
3236
	stos	dword [edi]
3237
      rel_prefix_ok:
31 halyavin 3238
	mov	esi,edi
157 heavyiron 3239
	sub	esi,edx
3240
	xchg	esi,[ebx]
3241
	add	ebx,10h
3242
      make_elf_string:
31 halyavin 3243
	or	esi,esi
157 heavyiron 3244
	jz	default_string
3245
	lods	dword [esi]
3246
	mov	ecx,eax
3247
	rep	movs byte [edi],[esi]
3248
	xor	al,al
3249
	stos	byte [edi]
3250
	jmp	make_string_table
3251
      make_elf64_string:
31 halyavin 3252
	cmp	byte [ebx+5],0
157 heavyiron 3253
	je	elf64_rel_prefix_ok
3254
	mov	byte [ebx+5],0
3255
	mov	eax,'.rel'
3256
	stos	dword [edi]
3257
	mov	al,'a'
3258
	stos	byte [edi]
3259
      elf64_rel_prefix_ok:
31 halyavin 3260
	mov	esi,edi
157 heavyiron 3261
	sub	esi,edx
3262
	xchg	esi,[ebx]
3263
	add	ebx,18h
3264
	jmp	make_elf_string
3265
      default_string:
31 halyavin 3266
	mov	eax,'.fla'
157 heavyiron 3267
	stos	dword [edi]
3268
	mov	ax,'t'
3269
	stos	word [edi]
3270
	jmp	make_string_table
3271
      elf_string_table_ok:
31 halyavin 3272
	mov	[edx+1+8],edi
157 heavyiron 3273
	mov	ebx,[code_start]
3274
	mov	eax,edi
3275
	sub	eax,[free_additional_memory]
3276
	test	[format_flags],8
3277
	jnz	finish_elf64_header
3278
	mov	[ebx+20h],eax
3279
	mov	eax,[current_section]
3280
	inc	ax
3281
	jz	format_limitations_exceeded
3282
	mov	[ebx+32h],ax
3283
	inc	ax
3284
	jz	format_limitations_exceeded
3285
	mov	[ebx+30h],ax
3286
	jmp	elf_header_finished
3287
      finish_elf64_header:
31 halyavin 3288
	mov	[ebx+28h],eax
157 heavyiron 3289
	mov	eax,[current_section]
3290
	inc	ax
3291
	jz	format_limitations_exceeded
3292
	mov	[ebx+3Eh],ax
3293
	inc	ax
3294
	jz	format_limitations_exceeded
3295
	mov	[ebx+3Ch],ax
3296
      elf_header_finished:
31 halyavin 3297
	xor	eax,eax
157 heavyiron 3298
	mov	ecx,10
3299
	rep	stos dword [edi]
3300
	test	[format_flags],8
3301
	jz	elf_null_section_ok
3302
	mov	ecx,6
3303
	rep	stos dword [edi]
3304
      elf_null_section_ok:
31 halyavin 3305
	mov	esi,ebp
157 heavyiron 3306
	xor	ecx,ecx
3307
      make_section_entry:
31 halyavin 3308
	mov	ebx,edi
157 heavyiron 3309
	mov	eax,[esi+4]
3310
	mov	eax,[eax]
3311
	stos	dword [edi]
3312
	mov	eax,1
3313
	cmp	dword [esi+0Ch],0
3314
	je	bss_section
3315
	test	byte [esi+14h],80h
3316
	jz	section_type_ok
3317
      bss_section:
31 halyavin 3318
	mov	al,8
157 heavyiron 3319
      section_type_ok:
31 halyavin 3320
	stos	dword [edi]
157 heavyiron 3321
	mov	eax,[esi+14h]
3322
	and	al,3Fh
3323
	call	store_elf_machine_word
3324
	xor	eax,eax
3325
	call	store_elf_machine_word
3326
	mov	eax,[esi+8]
3327
	mov	[image_base],eax
3328
	sub	eax,[code_start]
3329
	call	store_elf_machine_word
3330
	mov	eax,[esi+0Ch]
3331
	call	store_elf_machine_word
3332
	xor	eax,eax
3333
	stos	dword [edi]
3334
	stos	dword [edi]
3335
	mov	eax,[esi+10h]
3336
	call	store_elf_machine_word
3337
	xor	eax,eax
3338
	call	store_elf_machine_word
3339
	inc	ecx
3340
	add	esi,20h
3341
	xchg	edi,[esp]
3342
	mov	ebp,edi
3343
      convert_relocations:
31 halyavin 3344
	cmp	esi,[free_additional_memory]
157 heavyiron 3345
	je	relocations_converted
3346
	mov	al,[esi]
3347
	or	al,al
3348
	jz	relocations_converted
3349
	cmp	al,80h
3350
	jb	make_relocation_entry
3351
	cmp	al,0C0h
624 heavyiron 3352
	jb	relocation_entry_ok
3353
	add	esi,10h
157 heavyiron 3354
	jmp	convert_relocations
3355
      make_relocation_entry:
31 halyavin 3356
	test	[format_flags],8
157 heavyiron 3357
	jnz	make_elf64_relocation_entry
3358
	mov	eax,[esi+4]
3359
	stos	dword [edi]
3360
	mov	eax,[esi+8]
3361
	mov	eax,[eax]
3362
	mov	al,[esi]
3363
	stos	dword [edi]
3364
	jmp	relocation_entry_ok
3365
      make_elf64_relocation_entry:
31 halyavin 3366
	mov	eax,[esi+4]
157 heavyiron 3367
	stos	dword [edi]
3368
	xor	eax,eax
3369
	stos	dword [edi]
3370
	movzx	eax,byte [esi]
3371
	stos	dword [edi]
3372
	mov	eax,[esi+8]
3373
	mov	eax,[eax]
3374
	shr	eax,8
3375
	stos	dword [edi]
3376
	xor	eax,eax
3377
	stos	dword [edi]
3378
	stos	dword [edi]
3379
      relocation_entry_ok:
31 halyavin 3380
	add	esi,0Ch
157 heavyiron 3381
	jmp	convert_relocations
3382
      store_elf_machine_word:
31 halyavin 3383
	stos	dword [edi]
157 heavyiron 3384
	test	[format_flags],8
3385
	jz	elf_machine_word_ok
3386
	mov	dword [edi],0
3387
	add	edi,4
3388
      elf_machine_word_ok:
31 halyavin 3389
	ret
157 heavyiron 3390
      relocations_converted:
31 halyavin 3391
	cmp	edi,ebp
157 heavyiron 3392
	xchg	edi,[esp]
3393
	je	rel_section_ok
3394
	mov	eax,[ebx]
3395
	sub	eax,4
3396
	test	[format_flags],8
3397
	jz	store_relocations_name_offset
3398
	dec	eax
3399
      store_relocations_name_offset:
31 halyavin 3400
	stos	dword [edi]
157 heavyiron 3401
	test	[format_flags],8
3402
	jnz	rela_section
3403
	mov	eax,9
3404
	jmp	store_relocations_type
3405
      rela_section:
31 halyavin 3406
	mov	eax,4
157 heavyiron 3407
      store_relocations_type:
31 halyavin 3408
	stos	dword [edi]
157 heavyiron 3409
	xor	al,al
3410
	call	store_elf_machine_word
3411
	call	store_elf_machine_word
3412
	mov	eax,ebp
3413
	sub	eax,[code_start]
3414
	call	store_elf_machine_word
3415
	mov	eax,[esp]
3416
	sub	eax,ebp
3417
	call	store_elf_machine_word
3418
	mov	eax,[current_section]
3419
	stos	dword [edi]
3420
	mov	eax,ecx
3421
	stos	dword [edi]
3422
	inc	ecx
3423
	test	[format_flags],8
3424
	jnz	finish_elf64_rela_section
3425
	mov	eax,4
3426
	stos	dword [edi]
3427
	mov	al,8
3428
	stos	dword [edi]
3429
	jmp	rel_section_ok
3430
      finish_elf64_rela_section:
31 halyavin 3431
	mov	eax,8
157 heavyiron 3432
	stos	dword [edi]
3433
	xor	al,al
3434
	stos	dword [edi]
3435
	mov	al,24
3436
	stos	dword [edi]
3437
	xor	al,al
3438
	stos	dword [edi]
3439
      rel_section_ok:
31 halyavin 3440
	cmp	esi,[free_additional_memory]
157 heavyiron 3441
	jne	make_section_entry
3442
	pop	eax
3443
	mov	ebx,[code_start]
3444
	sub	eax,ebx
3445
	mov	[code_size],eax
3446
	mov	ecx,20h
3447
	test	[format_flags],8
3448
	jz	adjust_elf_section_headers_offset
3449
	mov	ecx,28h
3450
      adjust_elf_section_headers_offset:
31 halyavin 3451
	add	[ebx+ecx],eax
157 heavyiron 3452
	mov	eax,1
3453
	stos	dword [edi]
3454
	mov	al,2
3455
	stos	dword [edi]
3456
	xor	al,al
3457
	call	store_elf_machine_word
3458
	call	store_elf_machine_word
3459
	mov	eax,[code_size]
3460
	call	store_elf_machine_word
3461
	mov	eax,[edx+1]
3462
	sub	eax,[free_additional_memory]
3463
	call	store_elf_machine_word
3464
	mov	eax,[current_section]
3465
	inc	eax
3466
	stos	dword [edi]
3467
	mov	eax,[number_of_sections]
3468
	inc	eax
3469
	stos	dword [edi]
3470
	test	[format_flags],8
3471
	jnz	finish_elf64_sym_section
3472
	mov	eax,4
3473
	stos	dword [edi]
3474
	mov	al,10h
3475
	stos	dword [edi]
3476
	jmp	sym_section_ok
3477
      finish_elf64_sym_section:
31 halyavin 3478
	mov	eax,8
157 heavyiron 3479
	stos	dword [edi]
3480
	xor	al,al
3481
	stos	dword [edi]
3482
	mov	al,18h
3483
	stos	dword [edi]
3484
	xor	al,al
3485
	stos	dword [edi]
3486
      sym_section_ok:
31 halyavin 3487
	mov	al,1+8
157 heavyiron 3488
	stos	dword [edi]
3489
	mov	al,3
3490
	stos	dword [edi]
3491
	xor	al,al
3492
	call	store_elf_machine_word
3493
	call	store_elf_machine_word
3494
	mov	eax,[edx+1]
3495
	sub	eax,[free_additional_memory]
3496
	add	eax,[code_size]
3497
	call	store_elf_machine_word
3498
	mov	eax,[edx+1+8]
3499
	sub	eax,[edx+1]
3500
	call	store_elf_machine_word
3501
	xor	eax,eax
3502
	stos	dword [edi]
3503
	stos	dword [edi]
3504
	mov	al,1
3505
	call	store_elf_machine_word
3506
	xor	eax,eax
3507
	call	store_elf_machine_word
3508
	mov	eax,'tab'
3509
	mov	dword [edx+1],'.sym'
3510
	mov	[edx+1+4],eax
3511
	mov	dword [edx+1+8],'.str'
3512
	mov	[edx+1+8+4],eax
3513
	mov	[written_size],0
3514
	mov	edx,[output_file]
3515
	call	create
3516
	jc	write_failed
3517
	call	write_code
3518
	mov	ecx,edi
3519
	mov	edx,[free_additional_memory]
3520
	sub	ecx,edx
3521
	add	[written_size],ecx
3522
	call	write
3523
	jc	write_failed
3524
	jmp	output_written
3525
31 halyavin 3526
 
3527
	add	esi,2
157 heavyiron 3528
	or	[format_flags],1
3529
	mov	[image_base],8048000h
3530
	cmp	byte [esi],80h
3531
	jne	elf_exe_base_ok
3532
	lods	word [esi]
3533
	cmp	ah,'('
3534
	jne	invalid_argument
3535
	cmp	byte [esi],'.'
3536
	je	invalid_value
3537
	push	edx
3538
	call	get_dword_value
3539
	cmp	[value_type],0
3540
	jne	invalid_use_of_symbol
3541
	mov	[image_base],eax
3542
	pop	edx
3543
      elf_exe_base_ok:
31 halyavin 3544
	mov	byte [edx+10h],2
157 heavyiron 3545
	mov	byte [edx+2Ah],20h
3546
	mov	ebx,edi
3547
	mov	ecx,20h shr 2
3548
	cmp	[current_pass],0
3549
	je	init_elf_segments
3550
	imul	ecx,[number_of_sections]
3551
      init_elf_segments:
109 heavyiron 3552
	xor	eax,eax
157 heavyiron 3553
	rep	stos dword [edi]
3554
	mov	[number_of_sections],0
3555
	mov	byte [ebx],1
3556
	mov	word [ebx+1Ch],1000h
3557
	mov	byte [ebx+18h],111b
3558
	mov	eax,edi
3559
	sub	eax,[code_start]
3560
	mov	[ebx+4],eax
3561
	add	eax,[image_base]
3562
	mov	[ebx+8],eax
3563
	mov	[ebx+0Ch],eax
3564
	mov	[edx+18h],eax
3565
	xor	edx,edx
3566
	not	eax
3567
	not	edx
3568
	add	eax,1
3569
	adc	edx,0
3570
	add	eax,edi
3571
	adc	edx,0
3572
	mov	dword [org_origin],eax
3573
	mov	dword [org_origin+4],edx
3574
	mov	[org_registers],0
3575
	mov	[org_start],edi
3576
	mov	[symbols_stream],edi
3577
	jmp	format_defined
607 heavyiron 3578
      format_elf64_exe:
31 halyavin 3579
	add	esi,2
157 heavyiron 3580
	or	[format_flags],1
3581
	mov	[image_base],400000h
3582
	mov	[image_base_high],0
3583
	cmp	byte [esi],80h
3584
	jne	elf64_exe_base_ok
3585
	lods	word [esi]
3586
	cmp	ah,'('
3587
	jne	invalid_argument
3588
	cmp	byte [esi],'.'
3589
	je	invalid_value
3590
	push	edx
3591
	call	get_qword_value
3592
	cmp	[value_type],0
3593
	jne	invalid_use_of_symbol
3594
	mov	[image_base],eax
3595
	mov	[image_base_high],edx
3596
	pop	edx
3597
      elf64_exe_base_ok:
31 halyavin 3598
	mov	byte [edx+10h],2
157 heavyiron 3599
	mov	byte [edx+36h],38h
3600
	mov	ebx,edi
3601
	mov	ecx,38h shr 2
3602
	cmp	[current_pass],0
3603
	je	init_elf64_segments
3604
	imul	ecx,[number_of_sections]
3605
      init_elf64_segments:
109 heavyiron 3606
	xor	eax,eax
157 heavyiron 3607
	rep	stos dword [edi]
3608
	mov	[number_of_sections],0
3609
	mov	byte [ebx],1
3610
	mov	word [ebx+30h],1000h
3611
	mov	byte [ebx+4],111b
3612
	push	edx
3613
	mov	eax,edi
3614
	sub	eax,[code_start]
3615
	mov	[ebx+8],eax
3616
	xor	edx,edx
3617
	add	eax,[image_base]
3618
	adc	edx,[image_base_high]
3619
	mov	[ebx+10h],eax
3620
	mov	[ebx+10h+4],edx
3621
	mov	[ebx+18h],eax
3622
	mov	[ebx+18h+4],edx
3623
	pop	ebx
3624
	mov	[ebx+18h],eax
3625
	mov	[ebx+18h+4],edx
3626
	not	eax
3627
	not	edx
3628
	add	eax,1
3629
	adc	edx,0
3630
	add	eax,edi
3631
	adc	edx,0
3632
	mov	dword [org_origin],eax
3633
	mov	dword [org_origin+4],edx
3634
	mov	[org_registers],0
3635
	mov	[org_start],edi
3636
	mov	[symbols_stream],edi
3637
	jmp	format_defined
607 heavyiron 3638
elf_entry:
31 halyavin 3639
	lods	byte [esi]
157 heavyiron 3640
	cmp	al,'('
3641
	jne	invalid_argument
3642
	cmp	byte [esi],'.'
3643
	je	invalid_value
3644
	test	[format_flags],8
3645
	jnz	elf64_entry
3646
	call	get_dword_value
3647
	cmp	[value_type],0
3648
	jne	invalid_use_of_symbol
3649
	mov	edx,[code_start]
3650
	mov	[edx+18h],eax
3651
	jmp	instruction_assembled
3652
      elf64_entry:
31 halyavin 3653
	call	get_qword_value
157 heavyiron 3654
	cmp	[value_type],0
3655
	jne	invalid_use_of_symbol
3656
	mov	ebx,[code_start]
3657
	mov	[ebx+18h],eax
3658
	mov	[ebx+1Ch],edx
3659
	jmp	instruction_assembled
3660
elf_segment:
109 heavyiron 3661
	bt	[format_flags],0
157 heavyiron 3662
	jnc	illegal_instruction
3663
	test	[format_flags],8
3664
	jnz	elf64_segment
3665
	call	close_elf_segment
3666
	push	eax
3667
	mov	ebx,[number_of_sections]
3668
	shl	ebx,5
3669
	add	ebx,[code_start]
3670
	add	ebx,34h
3671
	cmp	ebx,[symbols_stream]
3672
	jb	new_elf_segment
3673
	mov	ebx,[symbols_stream]
3674
	sub	ebx,20h
3675
	push	edi
3676
	mov	edi,ebx
3677
	mov	ecx,20h shr 2
3678
	xor	eax,eax
3679
	rep	stos dword [edi]
3680
	pop	edi
3681
	or	[next_pass_needed],-1
3682
      new_elf_segment:
109 heavyiron 3683
	mov	byte [ebx],1
157 heavyiron 3684
	mov	word [ebx+1Ch],1000h
3685
      elf_segment_flags:
109 heavyiron 3686
	cmp	byte [esi],19h
157 heavyiron 3687
	jne	elf_segment_flags_ok
3688
	lods	word [esi]
3689
	sub	ah,28
3690
	jbe	invalid_argument
3691
	cmp	ah,1
3692
	je	mark_elf_segment_flag
3693
	cmp	ah,3
3694
	ja	invalid_argument
3695
	xor	ah,1
3696
	cmp	ah,2
3697
	je	mark_elf_segment_flag
3698
	inc	ah
3699
      mark_elf_segment_flag:
109 heavyiron 3700
	test	[ebx+18h],ah
157 heavyiron 3701
	jnz	setting_already_specified
3702
	or	[ebx+18h],ah
3703
	jmp	elf_segment_flags
3704
      elf_segment_flags_ok:
109 heavyiron 3705
	mov	eax,edi
157 heavyiron 3706
	sub	eax,[code_start]
3707
	mov	[ebx+4],eax
3708
	pop	edx
3709
	and	eax,0FFFh
3710
	add	edx,eax
3711
	mov	[ebx+8],edx
3712
	mov	[ebx+0Ch],edx
3713
	mov	eax,edx
3714
	xor	edx,edx
3715
	not	eax
3716
	not	edx
3717
	add	eax,1
3718
	adc	edx,0
3719
	add	eax,edi
3720
	adc	edx,0
3721
	mov	dword [org_origin],eax
3722
	mov	dword [org_origin+4],edx
3723
	mov	[org_registers],0
3724
	mov	[org_start],edi
3725
	inc	[number_of_sections]
3726
	jmp	instruction_assembled
3727
      close_elf_segment:
109 heavyiron 3728
	cmp	[number_of_sections],0
157 heavyiron 3729
	jne	finish_elf_segment
3730
	cmp	edi,[symbols_stream]
3731
	jne	first_elf_segment_ok
3732
	push	edi
3733
	mov	edi,[code_start]
3734
	add	edi,34h
3735
	mov	ecx,20h shr 2
3736
	xor	eax,eax
3737
	rep	stos dword [edi]
3738
	pop	edi
3739
	mov	eax,[image_base]
3740
	ret
3741
      first_elf_segment_ok:
109 heavyiron 3742
	inc	[number_of_sections]
157 heavyiron 3743
      finish_elf_segment:
109 heavyiron 3744
	mov	ebx,[number_of_sections]
157 heavyiron 3745
	dec	ebx
3746
	shl	ebx,5
3747
	add	ebx,[code_start]
3748
	add	ebx,34h
3749
	mov	eax,edi
3750
	sub	eax,[code_start]
3751
	sub	eax,[ebx+4]
3752
	mov	edx,edi
3753
	cmp	edi,[undefined_data_end]
3754
	jne	elf_segment_size_ok
3755
	mov	edi,[undefined_data_start]
3756
      elf_segment_size_ok:
109 heavyiron 3757
	mov	[ebx+14h],eax
157 heavyiron 3758
	add	eax,edi
3759
	sub	eax,edx
3760
	mov	[ebx+10h],eax
3761
	mov	eax,[ebx+8]
3762
	add	eax,[ebx+14h]
3763
	add	eax,0FFFh
3764
	and	eax,not 0FFFh
3765
	ret
3766
      elf64_segment:
109 heavyiron 3767
	call	close_elf64_segment
157 heavyiron 3768
	push	eax edx
3769
	mov	ebx,[number_of_sections]
3770
	imul	ebx,38h
3771
	add	ebx,[code_start]
3772
	add	ebx,40h
3773
	cmp	ebx,[symbols_stream]
3774
	jb	new_elf64_segment
3775
	mov	ebx,[symbols_stream]
3776
	sub	ebx,38h
3777
	push	edi
3778
	mov	edi,ebx
3779
	mov	ecx,38h shr 2
3780
	xor	eax,eax
3781
	rep	stos dword [edi]
3782
	pop	edi
3783
	or	[next_pass_needed],-1
3784
      new_elf64_segment:
109 heavyiron 3785
	mov	byte [ebx],1
157 heavyiron 3786
	mov	word [ebx+30h],1000h
3787
      elf64_segment_flags:
109 heavyiron 3788
	cmp	byte [esi],19h
157 heavyiron 3789
	jne	elf64_segment_flags_ok
3790
	lods	word [esi]
3791
	sub	ah,28
3792
	jbe	invalid_argument
3793
	cmp	ah,1
3794
	je	mark_elf64_segment_flag
3795
	cmp	ah,3
3796
	ja	invalid_argument
3797
	xor	ah,1
3798
	cmp	ah,2
3799
	je	mark_elf64_segment_flag
3800
	inc	ah
3801
      mark_elf64_segment_flag:
109 heavyiron 3802
	test	[ebx+4],ah
157 heavyiron 3803
	jnz	setting_already_specified
3804
	or	[ebx+4],ah
3805
	jmp	elf64_segment_flags
3806
      elf64_segment_flags_ok:
109 heavyiron 3807
	mov	ecx,edi
157 heavyiron 3808
	sub	ecx,[code_start]
3809
	mov	[ebx+8],ecx
3810
	pop	edx eax
3811
	and	ecx,0FFFh
3812
	add	eax,ecx
3813
	adc	edx,0
3814
	mov	[ebx+10h],eax
3815
	mov	[ebx+10h+4],edx
3816
	mov	[ebx+18h],eax
3817
	mov	[ebx+18h+4],edx
3818
	not	eax
3819
	not	edx
3820
	add	eax,1
3821
	adc	edx,0
3822
	add	eax,edi
3823
	adc	edx,0
3824
	mov	dword [org_origin],eax
3825
	mov	dword [org_origin+4],edx
3826
	mov	[org_registers],0
3827
	mov	[org_start],edi
3828
	inc	[number_of_sections]
3829
	jmp	instruction_assembled
3830
      close_elf64_segment:
109 heavyiron 3831
	cmp	[number_of_sections],0
157 heavyiron 3832
	jne	finish_elf64_segment
3833
	cmp	edi,[symbols_stream]
3834
	jne	first_elf64_segment_ok
3835
	push	edi
3836
	mov	edi,[code_start]
3837
	add	edi,40h
3838
	mov	ecx,38h shr 2
3839
	xor	eax,eax
3840
	rep	stos dword [edi]
3841
	pop	edi
3842
	mov	eax,[image_base]
3843
	mov	edx,[image_base_high]
3844
	ret
3845
      first_elf64_segment_ok:
109 heavyiron 3846
	inc	[number_of_sections]
157 heavyiron 3847
      finish_elf64_segment:
109 heavyiron 3848
	mov	ebx,[number_of_sections]
157 heavyiron 3849
	dec	ebx
3850
	imul	ebx,38h
3851
	add	ebx,[code_start]
3852
	add	ebx,40h
3853
	mov	eax,edi
3854
	sub	eax,[code_start]
3855
	sub	eax,[ebx+8]
3856
	mov	edx,edi
3857
	cmp	edi,[undefined_data_end]
3858
	jne	elf64_segment_size_ok
3859
	mov	edi,[undefined_data_start]
3860
      elf64_segment_size_ok:
109 heavyiron 3861
	mov	[ebx+28h],eax
157 heavyiron 3862
	add	eax,edi
3863
	sub	eax,edx
3864
	mov	[ebx+20h],eax
3865
	mov	eax,[ebx+10h]
3866
	mov	edx,[ebx+10h+4]
3867
	add	eax,[ebx+28h]
3868
	adc	edx,0
3869
	sub	eax,1
3870
	sbb	edx,0
3871
	shrd	eax,edx,12
3872
	shr	edx,12
3873
	add	eax,1
3874
	adc	edx,0
3875
	shld	edx,eax,12
3876
	shl	eax,12
3877
	ret
3878
close_elf_exe:
31 halyavin 3879
	test	[format_flags],8
157 heavyiron 3880
	jnz	close_elf64_exe
3881
	call	close_elf_segment
3882
	mov	edx,[code_start]
3883
	mov	eax,[number_of_sections]
3884
	mov	byte [edx+1Ch],34h
3885
	mov	[edx+2Ch],ax
3886
	shl	eax,5
3887
	add	eax,edx
3888
	add	eax,34h
3889
	cmp	eax,[symbols_stream]
3890
	je	elf_exe_ok
3891
	or	[next_pass_needed],-1
3892
      elf_exe_ok:
31 halyavin 3893
	ret
157 heavyiron 3894
      close_elf64_exe:
31 halyavin 3895
	call	close_elf64_segment
157 heavyiron 3896
	mov	edx,[code_start]
3897
	mov	eax,[number_of_sections]
3898
	mov	byte [edx+20h],40h
3899
	mov	[edx+38h],ax
3900
	imul	eax,38h
3901
	add	eax,edx
3902
	add	eax,40h
3903
	cmp	eax,[symbols_stream]
3904
	je	elf64_exe_ok
3905
	or	[next_pass_needed],-1
3906
      elf64_exe_ok:
31 halyavin 3907
	ret
157 heavyiron 3908