Subversion Repositories Kolibri OS

Rev

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