rsp2elfd.cxx
18.3 KB
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
#include <fstream.h>
#include <strstream.h>
#include <string.h>
#include <elf.h>
#include <sym.h>
#include <symconst.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "rsp2elf.h"
extern char filenamebuf[256], exenamebuf[256];
extern ifstream lineFile;
extern ofstream ELFfile;
extern int stringTableSize, hasLineInfo;
extern int fileGlobals, fileCount, procCount, labelCount, simplified;
extern char * cwd;
extern char hostName[32];
extern struct stat IMEMstat;
extern source * fileList;
extern unsigned int codeBase;
extern int textSectionSize;
extern int constants;
int lineInfoSize, totalLineCount;
char srcBuf[1024];
long debugInfoOffset;
int debugInfoSize;
struct stat sourceStat;
#define TEXT_SECT 1;
#define STRING_SECT 4
extern symbol * symTable;
HDRR dH; /* Thankfully, initialized to zero */
FDR fileH;
EXTR startSym;
SYMR fileSym;
PDR procDescriptor;
/*
The functions in this file write a minimal mdebug block as follows:
mdebug header
relative file descriptor ( 1 entry )
aux table ( 2 entries )
proc table
local strings
global strings
file table
local symbols
global symbols
*/
/************************************************************************
Function: writefail
Args: none
Type: void
Purpose: This function complains about failing to write out the ELF file
and dies.
************************************************************************/
void writefail( int which )
{
cerr << "Write failed in WriteDebugInfo, called from " << which
<< endl;
exit( 1 );
}
int PutDelta( line * ln, int lastLine )
{
char li;
int delta;
delta = ln->LineIs() - lastLine;
if ( delta > 7 || delta < -7) /* need alt form */
{
ELFfile.put( 0x80 ); /* long form tag */
ELFfile.put( (delta >> 8) & 0xff );
ELFfile.put( ( delta & 0xff ) );
return 3;
}
li = delta << 4;
ELFfile.put( li );
return 1;
}
/************************************************************************
Function: WriteDebugInfo
Args: none
Type: void
Purpose: This uncomfortably large function does all the interesting work
by creating the .mdebug section for the output ELF file. This is
where debuggers really get all their information. Since this
information does not really exist except in random places, we
collect it from all possible sources and synthesize what is missing.
The resulting ELF file .mdebug section thus contains everything we
have to say about the microcode.
************************************************************************/
void WriteDebugInfo( void )
{
int genericInt, symIndex;
long value;
long localStrings, globStrings, globSyms, maxExtStringIndex,
maxLocalStringIndex, localSyms, fileDesc, procTable,
lineTable, here;
symbol * sym;
source * sfp;
proc * pro;
int auxEntry = 0, sibling, procsEncountered = 0;;
debugInfoOffset = ELFfile.tellp();
cout << "Writing Debug section at offset " << hex <<
debugInfoOffset << endl;
if ( simplified )
procCount = 1;
/* Step one - write the debug header */
dH.magic = 0x7009;
dH.vstamp = 0x312;
dH.ifdMax = fileCount;
dH.ipdMax = procCount;
dH.iauxMax = 2 * procCount;
dH.isymMax = 2 * fileCount + 2 * procCount + labelCount + constants;
if ( simplified )
++dH.isymMax; // one extra global for synth
dH.iextMax = fileGlobals;
dH.crfd = 1;
dH.cbRfdOffset = debugInfoOffset + sizeof( HDRR );
dH.cbAuxOffset = debugInfoOffset + sizeof( HDRR ) + sizeof( int );
ELFfile.write( (char *) &dH, sizeof( HDRR ) );
if ( ELFfile.fail() ) writefail(1);
debugInfoSize = sizeof( HDRR );
/* make a dummy rfd entry (always follows header) */
genericInt = 0;
ELFfile.write( (char *) &genericInt, sizeof( int ) );
if ( ELFfile.fail() ) writefail(2);
debugInfoSize += sizeof( int );
/* make a very simple aux table (always follows rfd ) */
if ( simplified )
{
genericInt = fileGlobals + 1;
ELFfile.write( (char *) &genericInt, sizeof( int ) );
if ( ELFfile.fail() ) writefail(3);
genericInt = btVoid << 24;
ELFfile.write( (char *) &genericInt, sizeof( int ) );
if ( ELFfile.fail() ) writefail(4);
debugInfoSize += 2 * sizeof( int );
}
else // write two aux records for each proc
{
sfp = fileList;
while ( sfp )
{
int relativeAux;
sfp->AsgAuxBase( auxEntry );
relativeAux = 0;
sibling = 1; // skip entry for the file start symbol
pro = sfp->GetProcs();
while ( pro )
{
int labels;
++sibling; // skip entry for proc start
labels = 0;
pro->AsgAux( relativeAux );
auxEntry += 2;
relativeAux += 2;
sym = pro->GetLabels();
while ( sym )
{
++labels;
sym = sym->pnext;
}
genericInt = sibling + labels;
sibling = genericInt + 1; // skip over proc end
ELFfile.write( (char *) &genericInt, sizeof( int ) );
if ( ELFfile.fail() ) writefail(3);
genericInt = btVoid << 24;
ELFfile.write( (char *) &genericInt, sizeof( int ) );
if ( ELFfile.fail() ) writefail(4);
debugInfoSize += 2 * sizeof( int );
pro = pro->next;
}
sfp = sfp->next;
}
}
/* Write line info as a function of proc and file info */
lineTable = ELFfile.tellp();
if ( hasLineInfo )
{
line * ln;
int lastLine, lineBytes;
sfp = fileList;
while ( sfp )
{
int size;
lineBytes = 0;
sfp->AsgLineBase( lineInfoSize );
pro = sfp->GetProcs();
while ( pro )
{
ln = pro->GetLines();
if ( ln )
{
lastLine = ln->LineIs();
pro->AsgLineIndex( lineInfoSize );
}
while( ln )
{
size = PutDelta( ln, lastLine );
lineInfoSize += size;
lineBytes += size;
lastLine = ln->LineIs();
ln = ln->next;
sfp->OneMoreLine();
}
pro = pro->next;
}
ln = sfp->GetLines();
if ( ln )
lastLine = ln->LineIs();
while ( ln )
{
size = PutDelta( ln, lastLine );
lineInfoSize += size;
lineBytes += size;
lastLine = ln->LineIs();
ln = ln->next;
}
sfp->AsgLineBytes( lineBytes ); /* save it */
sfp = sfp->next;
}
genericInt = lineInfoSize;
while ( genericInt & 3 )
{
ELFfile.put( 0 );
++genericInt;
}
}
/* We also need some local strings information */
localStrings = ELFfile.tellp();
stringTableSize = 0;
sfp = fileList;
/*
Write out the file names - the first one gets all the
information on symbols. The others just get name records.
We specify the host and name of each file, the time of
creating of the object (IMEM), and the size of each source
file if we can stat it.
*/
while ( sfp )
{
ostrstream fileDate( srcBuf, sizeof( srcBuf ) );
sfp->AsgStringIndex( stringTableSize );
WriteString( "" );
WriteString( sfp->filename() );
WriteString( "</4Debug/>" );
WriteString( strcat( strcat( strcat( strcat( strcpy( srcBuf, hostName ),
":" ), cwd ), "/" ), sfp->filename() ) );
fileDate << IMEMstat.st_mtime << '\0';
WriteString( srcBuf );
if ( stat( sfp->filename(), &sourceStat ) )
{
cerr << "WARNING: Couldn't stat file " <<
sfp->filename() << endl;
WriteString( "0" );
}
else
{
ostrstream fileSize( srcBuf, sizeof( srcBuf ) );
fileSize << sourceStat.st_size << '\0';
WriteString( srcBuf );
}
/*
In the simplified case, we create a string for
each file, plus a string for each global. In the
proc oriented case, we create strings for not
only the file, but for each proc in it and for
each label in the proc. We do not, however, create
strings for the globals in this string table.
*/
if ( simplified )
{
// If this is the first file, write out the globals
if ( sfp == fileList )
{
sym = symTable;
while ( sym )
{
sym->AsgLocalIndex( stringTableSize -
sfp->StringIndexIs() );
WriteString( sym->NameIs() );
sym = sym->next;
}
}
}
else
{
pro = sfp->GetProcs();
while ( pro )
{
pro->AsgLocalIndex( stringTableSize -
sfp->StringIndexIs() );
WriteString( pro->nameIs() );
sym = pro->GetLabels();
while ( sym )
{
sym->AsgLocalIndex( stringTableSize -
sfp->StringIndexIs() );
WriteString( sym->NameIs() );
sym = sym->pnext;
}
pro = pro->next;
}
// Now write out any orphan symbols (those without procs)
sym = sfp->GetLabels();
while ( sym )
{
sym->AsgLocalIndex( stringTableSize -
sfp->StringIndexIs() );
WriteString( sym->NameIs() );
sym = sym->pnext;
}
}
sfp->AsgStringSize( stringTableSize - sfp->StringIndexIs() );
sfp = sfp->next;
}
while ( stringTableSize & 3 )
WriteString( "" ); /* should be even */
debugInfoSize += stringTableSize;
maxLocalStringIndex = stringTableSize;
/* We also need some global strings information */
globStrings = ELFfile.tellp();
stringTableSize = 0;
WriteString( "" );
sym = symTable;
while ( sym )
{
sym->AsgGlobalIndex( stringTableSize );
WriteString( sym->NameIs() );
sym = sym->next;
}
while ( stringTableSize & 3 )
WriteString( "" ); /* should be even */
debugInfoSize += stringTableSize;
maxExtStringIndex = stringTableSize;
/********************************/
/* Local symbols section */
/********************************/
localSyms = ELFfile.tellp();
sfp = fileList;
symIndex = 0;
while ( sfp )
{
sfp->AsgFirstSym( symIndex );
fileSym.iss = 1; /* relative to base, so always 1 */
fileSym.st = stFile;
fileSym.sc = scText;
fileSym.value = sfp->FirstInst();
if ( simplified )
{
if ( sfp == fileList )
fileSym.index = 3 + fileGlobals;
else
fileSym.index = 2;
}
else
fileSym.index = 2 + 2 * sfp->ProcCountIs() +
sfp->LabelCountIs();
ELFfile.write( (char *) &fileSym, sizeof( SYMR ) );
if ( ELFfile.fail() ) writefail(0);
debugInfoSize += sizeof( SYMR );
++symIndex;
/*
Now write out the symbols for the procs. For
the simplified case, there is only the synth proc.
It contains all the local symbols inside the proc.
For the general case, there are procs per file and
each contains labels.
*/
if ( simplified )
{
if ( sfp == fileList )
{
sym = symTable;
while ( sym )
{
fileSym.iss = sym->GetLocalIndex();
fileSym.sc = scText;
if ( sym == symTable ) // is synth
{
fileSym.st = stProc;
fileSym.index = 0; // only one aux
}
else
{
fileSym.st = sym->GetType();
if ( fileSym.st == stConstant )
fileSym.sc = scInfo;
fileSym.index = (unsigned) -1;
}
fileSym.value = sym->GetValue();
ELFfile.write( (char *) &fileSym, sizeof( SYMR ) );
if ( ELFfile.fail() ) writefail(0);
debugInfoSize += sizeof( SYMR );
sym->AsgSymIndex( symIndex );
++symIndex;
sym = sym->next;
}
// Write the end of proc symbol
fileSym.iss = symTable->GetLocalIndex();
fileSym.st = stEnd;
fileSym.sc = scText;
fileSym.value = textSectionSize;
fileSym.index = 1;
ELFfile.write( (char *) &fileSym, sizeof( SYMR ) );
if ( ELFfile.fail() ) writefail(0);
debugInfoSize += sizeof( SYMR );
++symIndex;
}
}
else
{
pro = sfp->GetProcs();
while ( pro )
{
long procStartIndex;
long procSymIndex;
pro->AsgFirstSym( symIndex );
// Write start of proc symbol
procStartIndex = fileSym.iss = pro->GetStringIndex();
fileSym.st = stProc;
fileSym.sc = scText;
fileSym.value = pro->FirstInst();
fileSym.index = pro->GetAux();
ELFfile.write( (char *) &fileSym, sizeof( SYMR ) );
if ( ELFfile.fail() ) writefail(0);
debugInfoSize += sizeof( SYMR );
pro->AsgSymIndex( symIndex );
procSymIndex = symIndex - sfp->FirstSymIs();
++symIndex;
// Write out any local labels for this proc
sym = pro->GetLabels();
while ( sym )
{
fileSym.iss = sym->GetLocalIndex();
fileSym.st = stLabel;
fileSym.sc = scText;
fileSym.value = sym->GetValue();
fileSym.index = (unsigned) -1;
ELFfile.write( (char *) &fileSym, sizeof( SYMR ) );
if ( ELFfile.fail() ) writefail(0);
debugInfoSize += sizeof( SYMR );
sym->AsgSymIndex( symIndex );
++symIndex;
sym = sym->pnext;
}
// Write end of proc symbol
fileSym.iss = procStartIndex;
fileSym.st = stEnd; /* of proc */
fileSym.sc = scText;
fileSym.value = pro->NextInst() - pro->FirstInst();
fileSym.index = procSymIndex;
ELFfile.write( (char *) &fileSym, sizeof( SYMR ) );
if ( ELFfile.fail() ) writefail(0);
debugInfoSize += sizeof( SYMR );
++symIndex;
pro = pro->next;
}
// Write out any orphan symbols for this file
sym = sfp->GetLabels();
while ( sym )
{
fileSym.iss = sym->GetLocalIndex();
fileSym.st = stLabel;
fileSym.sc = scText;
fileSym.value = sym->GetValue();
fileSym.index = (unsigned) -1;
ELFfile.write( (char *) &fileSym, sizeof( SYMR ) );
if ( ELFfile.fail() ) writefail(0);
debugInfoSize += sizeof( SYMR );
sym->AsgSymIndex( symIndex );
++symIndex;
sym = sym->pnext;
}
}
// Write out the end of file symbol
fileSym.iss = 1;
fileSym.st = stEnd; /* of file */
fileSym.sc = scText;
fileSym.value = sfp->NextInst() - sfp->FirstInst();
fileSym.index = 0;
ELFfile.write( (char *) &fileSym, sizeof( SYMR ) );
if ( ELFfile.fail() ) writefail(0);
debugInfoSize += sizeof( SYMR );
symIndex++;
sfp = sfp->next;
}
/********************************/
/* Global symbols section */
/********************************/
globSyms = ELFfile.tellp();
sym = symTable;
while ( sym )
{
startSym.asym.iss = sym->GetGlobalIndex();
startSym.asym.st = sym->GetType();
startSym.asym.sc = sym->GetType() == stConstant ? scInfo : scText;
startSym.asym.value = sym->GetValue();
startSym.asym.index = sym->GetType() == stConstant ? -1 :
sym->GetSymIndex();
if ( !startSym.asym.index ) // can't be 0
startSym.asym.index = (unsigned) -1;
if ( startSym.asym.st == stProc || startSym.asym.st == stLabel )
{
if ( simplified )
{
startSym.ifd = 0;
}
else
{
startSym.ifd =
GetFileOfAddr( sym->GetValue() );
startSym.asym.index = startSym.asym.index - SymBaseOfFile( startSym.ifd );
// startSym.asym.index -=
// SymBaseOfFile( startSym.ifd );
}
}
else
startSym.ifd = 0;
if ( startSym.ifd == -1 )
{
// label symbol not in range, make it into a const
startSym.asym.st = stConstant;
startSym.asym.sc = scInfo;
startSym.asym.index = (unsigned) -1;
startSym.ifd = 0;
}
ELFfile.write( (char *) &startSym, sizeof( EXTR ) );
if ( ELFfile.fail() ) writefail(0);
debugInfoSize += sizeof( EXTR );
sym = sym->next;
}
/********************************/
/* Procedure info section */
/********************************/
procTable = ELFfile.tellp();
sfp = fileList;
while ( sfp )
{
pro = sfp->GetProcs();
while ( pro )
{
procDescriptor.adr = pro->FirstInst();
procDescriptor.lnLow = pro->FirstLine();
procDescriptor.lnHigh = pro->LastLine();
if ( simplified )
{
procDescriptor.iline = -1;
procDescriptor.isym = 1;
}
else
{
procDescriptor.iline = pro->GetLineIndex()
- sfp->LineBaseIs();
procDescriptor.isym = pro->GetFirstSym() -
sfp->FirstSymIs(); // rel to file
}
ELFfile.write( (char *) &procDescriptor, sizeof( PDR ) );
if ( ELFfile.fail() ) writefail(0);
debugInfoSize += sizeof( PDR );
pro = pro->next;
}
sfp = sfp->next;
}
/********************************/
/* File descriptor section */
/********************************/
fileDesc = ELFfile.tellp();
sfp = fileList;
while ( sfp )
{
fileH.adr = sfp->FirstInst();
fileH.rss = 1;
fileH.issBase = sfp->StringIndexIs();
fileH.cbSs = sfp->StringSizeIs();
if ( simplified )
{
if ( sfp == fileList )
{
fileH.csym = 3 + fileGlobals;
fileH.cpd = 1;
fileH.caux = 2;
}
else
{
fileH.csym = 2;
fileH.cpd = 0;
fileH.caux = 0;
}
}
else
{
fileH.csym = 2 + 2 * sfp->ProcCountIs() +
sfp->LabelCountIs();
fileH.cpd = sfp->ProcCountIs();
fileH.caux = 2 * sfp->ProcCountIs();
fileH.iauxBase = fileH.caux ? // there were procs
sfp->GetAuxBase() : 0;
fileH.ipdFirst = procsEncountered;
procsEncountered += sfp->ProcCountIs();
}
fileH.isymBase = sfp->FirstSymIs();
fileH.fBigendian = 1;
fileH.crfd = 0;
fileH.lang = langAssembler;
totalLineCount += sfp->LineCountIs();
if ( fileH.cpd ) // tell about lines
{
fileH.cline = sfp->LineCountIs();
fileH.cbLine = sfp->LineBytesIs();
fileH.cbLineOffset = sfp->LineBaseIs();
fileH.ilineBase = sfp->LineBaseIs();
}
else
fileH.cline = fileH.cbLine = fileH.cbLineOffset =
fileH.ilineBase = 0;
ELFfile.write( (char *) &fileH, sizeof( FDR ) );
if ( ELFfile.fail() ) writefail(0);
debugInfoSize += sizeof( FDR );
sfp = sfp->next;
}
here = ELFfile.tellp();
/* for local strings section, write size and addr */
value = (char *) &dH.issMax - (char *) &dH;
ELFfile.seekp( debugInfoOffset + value );
ELFfile.write( (char *) &maxLocalStringIndex, 4 );
if ( ELFfile.fail() ) writefail(0);
ELFfile.write( (char *) &localStrings, 4 );
if ( ELFfile.fail() ) writefail(0);
/* for global strings section, write size and addr */
value = (char *) &dH.issExtMax - (char *) &dH;
ELFfile.seekp( debugInfoOffset + value );
ELFfile.write( (char *) &maxExtStringIndex, 4 );
if ( ELFfile.fail() ) writefail(0);
ELFfile.write( (char *) &globStrings, 4 );
if ( ELFfile.fail() ) writefail(0);
/* point to global symbols section */
value = (char *) &dH.cbExtOffset - (char *) &dH;
ELFfile.seekp( debugInfoOffset + value );
ELFfile.write( (char *) &globSyms, 4 );
if ( ELFfile.fail() ) writefail(0);
/* point to local symbols section */
value = (char *) &dH.cbSymOffset - (char *) &dH;
ELFfile.seekp( debugInfoOffset + value );
ELFfile.write( (char *) &localSyms, 4 );
if ( ELFfile.fail() ) writefail(0);
/* point to file descriptor */
value = (char *) &dH.cbFdOffset - (char *) &dH;
ELFfile.seekp( debugInfoOffset + value );
ELFfile.write( (char *) &fileDesc, 4 );
if ( ELFfile.fail() ) writefail(0);
/* point to proc table */
value = (char *) &dH.cbPdOffset - (char *) &dH;
ELFfile.seekp( debugInfoOffset + value );
ELFfile.write( (char *) &procTable, 4 );
if ( ELFfile.fail() ) writefail(0);
/* point to line table */
value = (char *) &dH.ilineMax - (char *) &dH;
ELFfile.seekp( debugInfoOffset + value );
ELFfile.write( (char *) &totalLineCount, 4 );
if ( ELFfile.fail() ) writefail(0);
ELFfile.write( (char *) &lineInfoSize, 4 );
if ( ELFfile.fail() ) writefail(0);
ELFfile.write( (char *) &lineTable, 4 );
if ( ELFfile.fail() ) writefail(0);
ELFfile.seekp( here );
}