Subversion Repositories Kolibri OS

Rev

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