/
cgobj.d
3784 lines (3277 loc) · 104 KB
/
cgobj.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Compiler implementation of the
* $(LINK2 https://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) 1984-1998 by Symantec
* Copyright (C) 2000-2024 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/backend/cgobj.d, backend/cgobj.d)
*/
module dmd.backend.cgobj;
import core.stdc.ctype;
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import dmd.backend.barray;
import dmd.backend.cc;
import dmd.backend.cdef;
import dmd.backend.cgcv;
import dmd.backend.code;
import dmd.backend.code_x86;
import dmd.backend.dlist;
import dmd.backend.dvec;
import dmd.backend.el;
import dmd.backend.md5;
import dmd.backend.mem;
import dmd.backend.global;
import dmd.backend.obj;
import dmd.backend.oper;
import dmd.backend.rtlsym;
import dmd.backend.ty;
import dmd.backend.type;
import dmd.common.outbuffer;
nothrow:
@safe:
import dmd.backend.dvarstats;
import dmd.backend.filespec : filespecdotext, filespecgetroot, filespecname;
version (Windows)
{
extern (C) int memicmp(const(void)*, const(void)*, size_t) pure nothrow @nogc;
extern (C) int stricmp(const(char)*, const(char)*) pure nothrow @nogc;
alias filespeccmp = stricmp;
}
else
alias filespeccmp = strcmp;
extern(C) char* getcwd(char*,size_t);
struct Loc
{
char *filename;
uint linnum;
uint charnum;
this(int y, int x)
{
linnum = y;
charnum = x;
filename = null;
}
}
version (Windows)
{
extern(C) char* strupr(char*);
}
version (Posix)
{
@trusted
extern(C) char* strupr(char* s)
{
for (char* p = s; *p; ++p)
{
char c = *p;
if ('a' <= c && c <= 'z')
*p = cast(char)(c - 'a' + 'A');
}
return s;
}
}
enum MULTISCOPE = 1; /* account for bug in MultiScope debugger
where it cannot handle a line number
with multiple offsets. We use a bit vector
to filter out the extra offsets.
*/
import dmd.backend.dcgcv : TOOFFSET;
@trusted
void TOWORD(void* a, uint b)
{
*cast(ushort*)a = cast(ushort)b;
}
@trusted
void TOLONG(void* a, uint b)
{
*cast(uint*)a = b;
}
/**************************
* Record types:
*/
enum
{
RHEADR = 0x6E,
REGINT = 0x70,
REDATA = 0x72,
RIDATA = 0x74,
OVLDEF = 0x76,
ENDREC = 0x78,
BLKDEF = 0x7A,
BLKEND = 0x7C,
// DEBSYM = 0x7E,
THEADR = 0x80,
LHEADR = 0x82,
PEDATA = 0x84,
PIDATA = 0x86,
COMENT = 0x88,
MODEND = 0x8A,
EXTDEF = 0x8C,
TYPDEF = 0x8E,
PUBDEF = 0x90,
PUB386 = 0x91,
LOCSYM = 0x92,
LINNUM = 0x94,
LNAMES = 0x96,
SEGDEF = 0x98,
SEG386 = 0x99,
GRPDEF = 0x9A,
FIXUPP = 0x9C,
FIX386 = 0x9D,
LEDATA = 0xA0,
LED386 = 0xA1,
LIDATA = 0xA2,
LID386 = 0xA3,
LIBHED = 0xA4,
LIBNAM = 0xA6,
LIBLOC = 0xA8,
LIBDIC = 0xAA,
COMDEF = 0xB0,
LEXTDEF = 0xB4,
LPUBDEF = 0xB6,
LCOMDEF = 0xB8,
CEXTDEF = 0xBC,
COMDAT = 0xC2,
LINSYM = 0xC4,
ALIAS = 0xC6,
LLNAMES = 0xCA,
}
// Some definitions for .OBJ files. Trial and error to determine which
// one to use when. Page #s refer to Intel spec on .OBJ files.
// Values for LOCAT byte: (pg. 71)
enum
{
LOCATselfrel = 0x8000,
LOCATsegrel = 0xC000,
// OR'd with one of the following:
LOClobyte = 0x0000,
LOCbase = 0x0800,
LOChibyte = 0x1000,
LOCloader_resolved = 0x1400,
// Unfortunately, the fixup stuff is different for EASY OMF and Microsoft
EASY_LOCoffset = 0x1400, // 32 bit offset
EASY_LOCpointer = 0x1800, // 48 bit seg/offset
LOC32offset = 0x2400,
LOC32tlsoffset = 0x2800,
LOC32pointer = 0x2C00,
LOC16offset = 0x0400,
LOC16pointer = 0x0C00,
LOCxx = 0x3C00
}
// FDxxxx are constants for the FIXDAT byte in fixup records (pg. 72)
enum
{
FD_F0 = 0x00, // segment index
FD_F1 = 0x10, // group index
FD_F2 = 0x20, // external index
FD_F4 = 0x40, // canonic frame of LSEG that contains Location
FD_F5 = 0x50, // Target determines the frame
FD_T0 = 0, // segment index
FD_T1 = 1, // group index
FD_T2 = 2, // external index
FD_T4 = 4, // segment index, 0 displacement
FD_T5 = 5, // group index, 0 displacement
FD_T6 = 6, // external index, 0 displacement
}
/***************
* Fixup list.
*/
struct FIXUP
{
FIXUP *FUnext;
targ_size_t FUoffset; // offset from start of ledata
ushort FUlcfd; // LCxxxx | FDxxxx
ushort FUframedatum;
ushort FUtargetdatum;
}
@trusted
FIXUP* list_fixup(list_t fl) { return cast(FIXUP *)list_ptr(fl); }
int seg_is_comdat(int seg) { return seg < 0; }
/*****************************
* Ledata records
*/
enum LEDATAMAX = 1024-14;
struct Ledatarec
{
ubyte[14] header; // big enough to handle COMDAT header
ubyte[LEDATAMAX] data;
int lseg; // segment value
uint i; // number of bytes in data
targ_size_t offset; // segment offset of start of data
FIXUP *fixuplist; // fixups for this ledata
// For COMDATs
ubyte flags; // flags byte of COMDAT
ubyte alloctyp; // allocation type of COMDAT
ubyte _align; // align type
int typidx;
int pubbase;
int pubnamidx;
}
/*****************************
* For defining segments.
*/
uint SEG_ATTR(uint A, uint C, uint B, uint P)
{
return (A << 5) | (C << 2) | (B << 1) | P;
}
enum
{
// Segment alignment A
SEG_ALIGN0 = 0, // absolute segment
SEG_ALIGN1 = 1, // byte align
SEG_ALIGN2 = 2, // word align
SEG_ALIGN16 = 3, // paragraph align
SEG_ALIGN4K = 4, // 4Kb page align
SEG_ALIGN4 = 5, // dword align
// Segment combine types C
SEG_C_ABS = 0,
SEG_C_PUBLIC = 2,
SEG_C_STACK = 5,
SEG_C_COMMON = 6,
// Segment type P
USE16 = 0,
USE32 = 1,
USE32_CODE = (4+2), // use32 + execute/read
USE32_DATA = (4+3), // use32 + read/write
}
/*****************************
* Line number support.
*/
struct Linnum
{
const(char)* filename; // source file name
int cseg; // our internal segment number
int seg; // segment/public index
OutBuffer data; // linnum/offset data
void reset() nothrow
{
data.reset();
}
}
/*****************************
*/
struct PtrRef
{
align(4):
Symbol* sym;
uint offset;
}
enum LINRECMAX = 2 + 255 * 2; // room for 255 line numbers
/************************************
* State of object file.
*/
struct Objstate
{
const(char)* modname;
char *csegname;
OutBuffer *buf; // output buffer
int fdsegattr; // far data segment attribute
int csegattr; // code segment attribute
int lastfardatasegi; // SegData[] index of last far data seg
int LOCoffset;
int LOCpointer;
int mlidata;
int mpubdef;
int mfixupp;
int mmodend;
int lnameidx; // index of next LNAMES record
int segidx; // index of next SEGDEF record
int extidx; // index of next EXTDEF record
int pubnamidx; // index of COMDAT public name index
Symbol *startaddress; // if !null, then Symbol is start address
debug
int fixup_count;
// Line numbers
char *linrec; // line number record
uint linreci; // index of next avail in linrec[]
uint linrecheader; // size of line record header
uint linrecnum; // number of line record entries
int mlinnum;
int recseg;
int term;
static if (MULTISCOPE)
{
vec_t linvec; // bit vector of line numbers used
vec_t offvec; // and offsets used
}
int fisegi; // SegData[] index of FI segment
int fmsegi; // SegData[] of FM segment
int datrefsegi; // SegData[] of DATA pointer ref segment
int tlsrefsegi; // SegData[] of TLS pointer ref segment
int tlssegi; // SegData[] of tls segment
int fardataidx;
char[1024] pubdata;
int pubdatai;
char[1024] extdata;
int extdatai;
// For OmfObj_far16thunk
int code16segi; // SegData[] index
targ_size_t CODE16offset;
int fltused;
int nullext;
// The rest don't get re-zeroed for each object file, they get reset
Rarray!(Ledatarec*) ledatas;
Barray!(Symbol*) resetSymbols; // reset symbols
Rarray!(Linnum) linnum_list;
Barray!(char*) linreclist; // array of line records
Barray!PtrRef ptrrefs; // buffer for pointer references
}
__gshared
{
extern (C++) Rarray!(seg_data*) SegData;
Objstate obj;
}
/*******************************
* Output an object file data record.
* Params:
* rectyp = record type
* record = the data
*/
@trusted
void objrecord(uint rectyp, scope const(char)[] record)
{
auto o = obj.buf;
//printf("rectyp = x%x, record[0] = x%x, reclen = x%x\n",rectyp,record[0],reclen);
o.reserve(record.length + 4);
o.writeByten(cast(ubyte)rectyp);
o.write16n(cast(int)(record.length) + 1); // record length includes checksum
o.writen(record.ptr, record.length);
o.writeByten(0); // use 0 for checksum
}
@trusted
void too_many_symbols()
{
error(null, 0, 0, "more than %d symbols in object file %s", 0x7FFF, obj.modname);
fatal();
}
version (X86) version (DigitalMars)
version = X86ASM;
@trusted
int insidx(char *p,uint index)
{
//if (index > 0x7FFF) printf("index = x%x\n",index);
/* OFM spec says it could be <=0x7F, but that seems to cause
* "library is corrupted" messages. Unverified. See Bugzilla 3601
*/
if (index < 0x7F)
{
*p = cast(char)index;
return 1;
}
else if (index <= 0x7FFF)
{
*(p + 1) = cast(char)index;
*p = cast(char)((index >> 8) | 0x80);
return 2;
}
else
{
too_many_symbols();
return 0;
}
}
/**************************
* Insert a type index number.
* Input:
* p . where to put the 1 or 2 byte index
* index = the 15 bit index
* Returns:
* # of bytes stored
*/
@trusted
int instypidx(char *p,uint index)
{
if (index <= 127)
{ *p = cast(char)index;
return 1;
}
else if (index <= 0x7FFF)
{ *(p + 1) = cast(char)index;
*p = cast(char)((index >> 8) | 0x80);
return 2;
}
else // overflow
{ *p = 0; // the linker ignores this field anyway
return 1;
}
}
/****************************
* Read index.
*/
@trusted
int getindex(ubyte* p)
{
return ((*p & 0x80)
? ((*p & 0x7F) << 8) | *(p + 1)
: *p);
}
enum ONS_OHD = 4; // max # of extra bytes added by obj_namestring()
/******************************
* Allocate a new segment.
* Return index for the new segment.
*/
@trusted
seg_data *getsegment()
{
const int seg = cast(int)SegData.length;
seg_data** ppseg = SegData.push();
seg_data* pseg = *ppseg;
if (!pseg)
{
pseg = cast(seg_data *)mem_calloc(seg_data.sizeof);
//printf("test2: SegData[%d] = %p\n", seg, SegData[seg]);
SegData[seg] = pseg;
}
else
memset(pseg, 0, seg_data.sizeof);
pseg.SDseg = seg;
pseg.segidx = 0;
return pseg;
}
/**************************
* Output read only data and generate a symbol for it.
*
*/
Symbol * OmfObj_sym_cdata(tym_t ty,char *p,int len)
{
Symbol *s;
alignOffset(CDATA, tysize(ty));
s = symboldata(Offset(CDATA), ty);
s.Sseg = CDATA;
OmfObj_bytes(CDATA, Offset(CDATA), len, p);
Offset(CDATA) += len;
s.Sfl = FLdata; //FLextern;
return s;
}
/**************************
* Ouput read only data for data.
* Output:
* *pseg segment of that data
* Returns:
* offset of that data
*/
int OmfObj_data_readonly(char *p, int len, int *pseg)
{
targ_size_t oldoff = Offset(CDATA);
OmfObj_bytes(CDATA,Offset(CDATA),len,p);
Offset(CDATA) += len;
*pseg = CDATA;
return cast(int)oldoff;
}
@trusted
int OmfObj_data_readonly(char *p, int len)
{
int pseg;
return OmfObj_data_readonly(p, len, &pseg);
}
/*****************************
* Get segment for readonly string literals.
* The linker will pool strings in this section.
* Params:
* sz = number of bytes per character (1, 2, or 4)
* Returns:
* segment index
*/
int OmfObj_string_literal_segment(uint sz)
{
assert(0);
}
segidx_t OmfObj_seg_debugT()
{
return DEBTYP;
}
/******************************
* Perform initialization that applies to all .obj output files.
* Input:
* filename source file name
* csegname code segment name (can be null)
*/
@system
Obj OmfObj_init(OutBuffer *objbuf, const(char)* filename, const(char)* csegname)
{
//printf("OmfObj_init()\n");
Obj mobj = cast(Obj)mem_calloc(__traits(classInstanceSize, Obj));
// Zero obj up to ledatas
memset(&obj,0,obj.ledatas.offsetof);
obj.ledatas.reset(); // recycle the memory used by ledatas
foreach (s; obj.resetSymbols)
symbol_reset(s);
obj.resetSymbols.reset();
obj.buf = objbuf;
obj.buf.reserve(40_000);
obj.lastfardatasegi = -1;
obj.mlidata = LIDATA;
obj.mpubdef = PUBDEF;
obj.mfixupp = FIXUPP;
obj.mmodend = MODEND;
obj.mlinnum = LINNUM;
// Reset for different OBJ file formats
if (I32)
{ if (config.flags & CFGeasyomf)
{ obj.LOCoffset = EASY_LOCoffset;
obj.LOCpointer = EASY_LOCpointer;
}
else
{
obj.mlidata = LID386;
obj.mpubdef = PUB386;
obj.mfixupp = FIX386;
obj.mmodend = MODEND + 1;
obj.LOCoffset = LOC32offset;
obj.LOCpointer = LOC32pointer;
}
obj.fdsegattr = SEG_ATTR(SEG_ALIGN16,SEG_C_PUBLIC,0,USE32);
obj.csegattr = SEG_ATTR(SEG_ALIGN4, SEG_C_PUBLIC,0,USE32);
}
else
{
obj.LOCoffset = LOC16offset;
obj.LOCpointer = LOC16pointer;
obj.fdsegattr = SEG_ATTR(SEG_ALIGN16,SEG_C_PUBLIC,0,USE16);
obj.csegattr = SEG_ATTR(SEG_ALIGN2, SEG_C_PUBLIC,0,USE16);
}
if (config.flags4 & CFG4speed && // if optimized for speed
config.target_cpu == TARGET_80486)
// 486 is only CPU that really benefits from alignment
obj.csegattr = I32 ? SEG_ATTR(SEG_ALIGN16, SEG_C_PUBLIC,0,USE32)
: SEG_ATTR(SEG_ALIGN16, SEG_C_PUBLIC,0,USE16);
SegData.reset(); // recycle memory
getsegment(); // element 0 is reserved
getsegment();
getsegment();
getsegment();
getsegment();
SegData[CODE].SDseg = CODE;
SegData[DATA].SDseg = DATA;
SegData[CDATA].SDseg = CDATA;
SegData[UDATA].SDseg = UDATA;
SegData[CODE].segidx = CODE;
SegData[DATA].segidx = DATA;
SegData[CDATA].segidx = CDATA;
SegData[UDATA].segidx = UDATA;
if (config.fulltypes)
{
getsegment();
getsegment();
SegData[DEBSYM].SDseg = DEBSYM;
SegData[DEBTYP].SDseg = DEBTYP;
SegData[DEBSYM].segidx = DEBSYM;
SegData[DEBTYP].segidx = DEBTYP;
}
OmfObj_theadr(filename);
obj.modname = filename;
if (!csegname || !*csegname) // if no code seg name supplied
obj.csegname = objmodtoseg(obj.modname); // generate one
else
obj.csegname = mem_strdup(csegname); // our own copy
objheader(obj.csegname);
OmfObj_segment_group(0,0,0,0); // obj seg and grp info
ledata_new(cseg,0); // so ledata is never null
if (config.fulltypes) // if full typing information
{ objmod = mobj;
cv_init(); // initialize debug output code
}
return mobj;
}
/**************************
* Initialize the start of object output for this particular .obj file.
*/
void OmfObj_initfile(const(char)* filename,const(char)* csegname, const(char)* modname)
{
}
/***************************
* Fixup and terminate object file.
*/
void OmfObj_termfile()
{
}
/*********************************
* Terminate package.
*/
@trusted
void OmfObj_term(const(char)* objfilename)
{
//printf("OmfObj_term()\n");
list_t dl;
uint size;
obj_defaultlib();
objflush_pointerRefs();
outfixlist(); // backpatches
if (config.fulltypes)
cv_term(); // write out final debug info
outextdata(); // finish writing EXTDEFs
outpubdata(); // finish writing PUBDEFs
// Put out LEDATA records and associated fixups
for (size_t i = 0; i < obj.ledatas.length; i++)
{ Ledatarec *d = obj.ledatas[i];
if (d.i) // if any data in this record
{ // Fill in header
int headersize;
int rectyp;
assert(d.lseg > 0 && d.lseg < SegData.length);
int lseg = SegData[d.lseg].segidx;
char[(d.header).sizeof] header = void;
if (seg_is_comdat(lseg)) // if COMDAT
{
header[0] = d.flags | (d.offset ? 1 : 0); // continuation flag
header[1] = d.alloctyp;
header[2] = d._align;
TOOFFSET(header.ptr + 3,d.offset);
headersize = 3 + _tysize[TYint];
headersize += instypidx(header.ptr + headersize,d.typidx);
if ((header[1] & 0x0F) == 0)
{ // Group index
header[headersize] = (d.pubbase == DATA) ? 1 : 0;
headersize++;
// Segment index
headersize += insidx(header.ptr + headersize,d.pubbase);
}
headersize += insidx(header.ptr + headersize,d.pubnamidx);
rectyp = I32 ? COMDAT + 1 : COMDAT;
}
else
{
rectyp = LEDATA;
headersize = insidx(header.ptr,lseg);
if (_tysize[TYint] == LONGSIZE || d.offset & ~0xFFFFL)
{ if (!(config.flags & CFGeasyomf))
rectyp++;
TOLONG(header.ptr + headersize,cast(uint)d.offset);
headersize += 4;
}
else
{
TOWORD(header.ptr + headersize,cast(uint)d.offset);
headersize += 2;
}
}
assert(headersize <= (d.header).sizeof);
// Right-justify data in d.header[]
memcpy(d.header.ptr + (d.header).sizeof - headersize,header.ptr,headersize);
//printf("objrecord(rectyp=x%02x, d=%p, p=%p, size = %d)\n",
//rectyp,d,d.header.ptr + ((d.header).sizeof - headersize),d.i + headersize);
const start = d.header.length - headersize;
const length = d.i + headersize;
objrecord(rectyp,cast(char[])d.header.ptr[start .. start + length]);
objfixupp(d.fixuplist);
}
}
static if (TERMCODE)
{
//list_free(&obj.ledata_list,mem_freefp);
}
linnum_term();
obj_modend();
size = cast(uint)obj.buf.length();
obj.buf.reset(); // rewind file
OmfObj_theadr(obj.modname);
objheader(obj.csegname);
mem_free(obj.csegname);
OmfObj_segment_group(SegData[CODE].SDoffset, SegData[DATA].SDoffset, SegData[CDATA].SDoffset, SegData[UDATA].SDoffset); // do real sizes
// Update any out-of-date far segment sizes
for (size_t i = 0; i < SegData.length; i++)
{
seg_data* f = SegData[i];
if (f.isfarseg && f.origsize != f.SDoffset)
{ obj.buf.setsize(cast(int)f.seek);
objsegdef(f.attr,f.SDoffset,f.lnameidx,f.classidx);
}
}
//mem_free(obj.farseg);
//printf("Ledata max = %d\n", obj.ledatas.length);
//printf("Max # of fixups = %d\n",obj.fixup_count);
obj.buf.setsize(size);
}
/*****************************
* Line number support.
*/
/***************************
* Record line number linnum at offset.
* Params:
* srcpos = source file position
* seg = segment it corresponds to (negative for COMDAT segments)
* offset = offset within seg
* pubnamidx = public name index
* obj.mlinnum = LINNUM or LINSYM
*/
@trusted
void OmfObj_linnum(Srcpos srcpos,int seg,targ_size_t offset)
{
varStats_recordLineOffset(srcpos, offset);
uint linnum = srcpos.Slinnum;
static if (0)
{
printf("OmfObj_linnum(seg=%d, offset=0x%x) ", seg, cast(int)offset);
srcpos.print("");
}
char linos2 = config.exe == EX_OS2 && !seg_is_comdat(SegData[seg].segidx);
bool cond = (!obj.term &&
(seg_is_comdat(SegData[seg].segidx) || (srcpos.Sfilename && srcpos.Sfilename != obj.modname)));
if (cond)
{
// Not original source file, or a COMDAT.
// Save data away and deal with it at close of compile.
// It is done this way because presumably 99% of the lines
// will be in the original source file, so we wish to minimize
// memory consumption and maximize speed.
if (linos2)
return; // BUG: not supported under OS/2
Linnum* ln;
foreach (ref rln; obj.linnum_list)
{
bool cond2 = rln.filename == srcpos.Sfilename;
if (cond2 &&
rln.cseg == seg)
{
ln = &rln; // found existing entry with room
goto L1;
}
}
// Create new entry
ln = obj.linnum_list.push();
ln.filename = srcpos.Sfilename;
ln.cseg = seg;
ln.seg = obj.pubnamidx;
ln.reset();
L1:
//printf("offset = x%x, line = %d\n", cast(int)offset, linnum);
ln.data.write16(linnum);
if (_tysize[TYint] == 2)
ln.data.write16(cast(int)offset);
else
ln.data.write32(cast(int)offset);
}
else
{
if (linos2 && obj.linreci > LINRECMAX - 8)
obj.linrec = null; // allocate a new one
else if (seg != obj.recseg)
linnum_flush();
if (!obj.linrec) // if not allocated
{
obj.linrec = cast(char* ) mem_calloc(LINRECMAX);
obj.linrec[0] = 0; // base group / flags
obj.linrecheader = 1 + insidx(obj.linrec + 1,seg_is_comdat(SegData[seg].segidx) ? obj.pubnamidx : SegData[seg].segidx);
obj.linreci = obj.linrecheader;
obj.recseg = seg;
static if (MULTISCOPE)
{
if (!obj.linvec)
{
obj.linvec = vec_calloc(1000);
obj.offvec = vec_calloc(1000);
}
}
if (linos2)
{
if (obj.linreclist.length == 0) // if first line number record
obj.linreci += 8; // leave room for header
obj.linreclist.push(obj.linrec);
}
// Select record type to use
obj.mlinnum = seg_is_comdat(SegData[seg].segidx) ? LINSYM : LINNUM;
if (I32 && !(config.flags & CFGeasyomf))
obj.mlinnum++;
}
else if (obj.linreci > LINRECMAX - (2 + _tysize[TYint]))
{
objrecord(obj.mlinnum,obj.linrec[0 .. obj.linreci]); // output data
obj.linreci = obj.linrecheader;
if (seg_is_comdat(SegData[seg].segidx)) // if LINSYM record
obj.linrec[0] |= 1; // continuation bit
}
static if (MULTISCOPE)
{
if (linnum >= vec_numbits(obj.linvec))
obj.linvec = vec_realloc(obj.linvec,linnum + 1000);
if (offset >= vec_numbits(obj.offvec))
{
if (offset < 0xFF00) // otherwise we overflow ph_malloc()
obj.offvec = vec_realloc(obj.offvec,cast(uint)offset * 2);
}
bool cond3 =
// disallow multiple offsets per line
!vec_testbit(linnum,obj.linvec) && // if linnum not already used
// disallow multiple lines per offset
(offset >= 0xFF00 || !vec_testbit(cast(uint)offset,obj.offvec)); // and offset not already used
}
else
enum cond3 = true;
if (cond3)
{
static if (MULTISCOPE)
{
vec_setbit(linnum,obj.linvec); // mark linnum as used
if (offset < 0xFF00)
vec_setbit(cast(uint)offset,obj.offvec); // mark offset as used
}
TOWORD(obj.linrec + obj.linreci,linnum);
if (linos2)
{
obj.linrec[obj.linreci + 2] = 1; // source file index
TOLONG(obj.linrec + obj.linreci + 4,cast(uint)offset);
obj.linrecnum++;
obj.linreci += 8;
}
else
{
TOOFFSET(obj.linrec + obj.linreci + 2,offset);
obj.linreci += 2 + _tysize[TYint];
}
}
}
}
/***************************
* Flush any pending line number records.
*/
@trusted
private void linnum_flush()
{
if (obj.linreclist.length)
{
obj.linrec = obj.linreclist[0];
TOWORD(obj.linrec + 6,obj.linrecnum);
foreach (i; 0 .. obj.linreclist.length - 1)
{
obj.linrec = obj.linreclist[i];
objrecord(obj.mlinnum, obj.linrec[0 .. LINRECMAX]);
mem_free(obj.linrec);
}
obj.linrec = obj.linreclist[obj.linreclist.length - 1];
objrecord(obj.mlinnum,obj.linrec[0 .. obj.linreci]);
obj.linreclist.reset();
// Put out File Names Table
TOLONG(obj.linrec + 2,0); // record no. of start of source (???)
TOLONG(obj.linrec + 6,obj.linrecnum); // number of primary source records
TOLONG(obj.linrec + 10,1); // number of source and listing files