Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

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