pro_table.c
30.1 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
/*
pro_table.c
$Revision: 1.1.1.1 $ $Date: 2002/05/02 03:29:20 $
*/
#include <bstring.h>
#include <stdlib.h>
#include <string.h>
#include <libXlate.h>
#include "xlateTypes.h"
#define CHECKALLOC(ptr, table, line) \
if (ptr == NULL) { \
if (table != NULL) \
table->tb_errorLine = line; \
return TB_STATUS_ALLOC_FAIL; \
}
/*
This routine returns a handle for the table to be created.
The user specifies the tableKind, and a pointer to the old
table coming in, in oldTable. The new table created will
be composed with the oldTable. makeDebug specifies whether
a debugging table should be created. is_64Bit specifies
whether the table is for a 64-bit object. For 64-bit objects,
upper32BitsNew and upper32BitsOld specify the top 32-bits of
all new and old address resp. This is possible because
Ragnarok guarantees that no text section will cross a 32-bit
address boundary.
The routine zeros out the Xlate_Table_Pro_s struct malloced.
Right now, we are assuming that oldTable is always NULL. For
this reason, the tb_putDebug field is set FALSE, and the table
being built is put into the main table. If makeDebug is true,
we set the hd_version of tb_headerDebug to indicate that the
debugging table is a copy of the main table.
*/
Elf32_Sword
xlate_pro_init (
Xlate_Table_Pro *retTable,
Xlate_TableKind tableKind,
void *oldTable,
Elf32_Word makeDebug,
Elf32_Word is64Bit,
Elf32_Word upper32BitsNew,
Elf32_Word upper32BitsOld
)
{
Xlate_Table_Pro newTable;
*retTable = NULL;
if (tableKind != TK_general &&
tableKind != TK_preserve_size &&
tableKind != TK_preserve_order)
return TB_STATUS_BAD_TABLEKIND;
if (oldTable != NULL)
return TB_STATUS_NOT_YET_IMPLEMENT;
newTable = (Xlate_Table_Pro)malloc(sizeof(struct Xlate_Table_Pro_s));
CHECKALLOC(newTable, newTable, __LINE__);
bzero(newTable, sizeof(struct Xlate_Table_Pro_s));
newTable->tb_oldTable = oldTable;
newTable->tb_makeDebug = makeDebug;
newTable->tb_putDebug = FALSE;
newTable->tb_header.hd_version = TB_TABLE_VERSION_MAIN;
newTable->tb_header.hd_tableKind = tableKind;
newTable->tb_header.hd_is64Bit = is64Bit;
newTable->tb_header.hd_upper32BitsNew = upper32BitsNew;
newTable->tb_header.hd_upper32BitsOld = upper32BitsOld;
newTable->tb_header.hd_blockSize = TB_BLOCK_SIZE;
if (makeDebug) {
newTable->tb_headerDebug.hd_version = TB_TABLE_VERSION_COPY;
}
*retTable = newTable;
return TB_STATUS_NO_ERROR;
}
/*
This routine is used to add miscellaneous
information about the translation to the
appropriate table. The information must
be added before xlate_pro_disk_header() or
its debugging table counterpart has been
called.
*/
Elf32_Sword
xlate_pro_add_info (
Xlate_Table_Pro table,
Elf32_Sword dataMoved,
Elf32_Word startupFwa,
Elf32_Word startupLwa,
Elf32_Word oldTextExists,
Elf32_Word oldTextAlloc
)
{
if (table == NULL) {
return TB_STATUS_NULL_TABLE;
}
if (table->tb_putDebug) {
if (table->tb_memoryReqRetDebug) {
table->tb_errorLine = __LINE__;
return TB_STATUS_ADD_TOO_LATE;
}
table->tb_headerDebug.hd_dataMoved = dataMoved;
table->tb_headerDebug.hd_startupFwa = startupFwa;
table->tb_headerDebug.hd_startupLwa = startupLwa;
table->tb_headerDebug.hd_oldTextExists = oldTextExists;
table->tb_headerDebug.hd_oldTextAlloc = oldTextAlloc;
}
else {
if (table->tb_memoryReqRet) {
table->tb_errorLine = __LINE__;
return TB_STATUS_ADD_TOO_LATE;
}
table->tb_header.hd_dataMoved = dataMoved;
table->tb_header.hd_startupFwa = startupFwa;
table->tb_header.hd_startupLwa = startupLwa;
table->tb_header.hd_oldTextExists = oldTextExists;
table->tb_header.hd_oldTextAlloc = oldTextAlloc;
}
return TB_STATUS_NO_ERROR;
}
/*
This routine is used to add information about the translation
of register in the transformation process. This routine if
called, should be called before xlate_pro_disk_header() or its
debugging counterpart.
*/
Elf32_Sword
xlate_pro_add_reg_info (
Xlate_Table_Pro table,
Dwarf_Small op,
Dwarf_Unsigned val1,
Dwarf_Unsigned val2
)
{
unsigned char regAssemble[1024];
Elf32_Sword regAssembleSize;
Elf32_Sword leb128_length;
Dwarf_Unsigned delta;
if (table == NULL) {
return TB_STATUS_NULL_TABLE;
}
if (table->tb_putDebug) {
if (table->tb_memoryReqRetDebug) {
table->tb_errorLine = __LINE__;
return TB_STATUS_ADD_TOO_LATE;
}
}
else {
if (table->tb_memoryReqRet) {
table->tb_errorLine = __LINE__;
return TB_STATUS_ADD_TOO_LATE;
}
}
regAssemble[0] = op;
switch (op) {
case DW_CFA_advance_loc :
delta = val1 >> 2;
if (delta > 0x3f) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_REG_VAL;
}
regAssemble[0] |= delta;
regAssembleSize = 1;
break;
case DW_CFA_offset :
if (val1 > 0x3f) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_REG_VAL;
}
regAssemble[0] |= val1;
leb128_length =
_xlate_leb128_unsigned_encode(val1, ®Assemble[1]);
regAssembleSize = 1 + leb128_length;
break;
case DW_CFA_restore:
if (val1 > 0x3f) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_REG_VAL;
}
regAssemble[0] |= val1;
regAssembleSize = 1;
break;
case DW_CFA_set_loc :
memcpy(®Assemble[1], &val1, sizeof(Dwarf_Unsigned));
regAssembleSize = 1 + sizeof(Dwarf_Unsigned);
break;
case DW_CFA_advance_loc1 :
delta = val1 >> 2;
if (delta > 0xff) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_REG_VAL;
}
regAssemble[1] = delta;
regAssembleSize = 2;
break;
case DW_CFA_advance_loc2 :
delta = val1 >> 2;
if (delta > 0xffff) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_REG_VAL;
}
regAssemble[1] = delta;
regAssemble[2] = delta >> 8;
regAssembleSize = 3;
break;
case DW_CFA_advance_loc4 :
delta = val1 >> 2;
if (delta > 0xffffffff) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_REG_VAL;
}
regAssemble[1] = delta;
delta >>= 8;
regAssemble[2] = delta;
delta >>= 8;
regAssemble[3] = delta;
delta >>= 8;
regAssemble[4] = delta;
regAssembleSize = 5;
break;
case DW_CFA_offset_extended :
case DW_CFA_def_cfa :
if (val1 >= DW_FRAME_LAST_REG_NUM) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_REG_VAL;
}
leb128_length =
_xlate_leb128_unsigned_encode(val1, ®Assemble[1]);
regAssembleSize = 1 + leb128_length;
leb128_length = _xlate_leb128_unsigned_encode((val2 >> 2),
®Assemble[regAssembleSize]);
regAssembleSize += leb128_length;
break;
case DW_CFA_restore_extended :
case DW_CFA_undefined :
case DW_CFA_same_value :
case DW_CFA_def_cfa_register :
if (val1 >= DW_FRAME_LAST_REG_NUM) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_REG_VAL;
}
leb128_length =
_xlate_leb128_unsigned_encode(val1, ®Assemble[1]);
regAssembleSize = 1 + leb128_length;
break;
case DW_CFA_register :
if (val1 >= DW_FRAME_LAST_REG_NUM) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_REG_VAL;
}
leb128_length =
_xlate_leb128_unsigned_encode(val1, ®Assemble[1]);
regAssembleSize = 1 + leb128_length;
if (val2 >= DW_FRAME_LAST_REG_NUM) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_REG_VAL;
}
leb128_length = _xlate_leb128_unsigned_encode(val2,
®Assemble[regAssembleSize]);
regAssembleSize += leb128_length;
break;
case DW_CFA_remember_state :
case DW_CFA_restore_state :
case DW_CFA_nop :
regAssembleSize = 1;
break;
case DW_CFA_def_cfa_offset :
leb128_length =
_xlate_leb128_unsigned_encode((val1 >> 2), ®Assemble[1]);
regAssembleSize = 1 + leb128_length;
break;
default :
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_REG_OP;
}
if (table->tb_regInfoOffset + regAssembleSize >= table->tb_regInfoSize) {
table->tb_regInfoSize += TB_REGINFO_SIZE;
table->tb_regInfo = realloc(table->tb_regInfo, table->tb_regInfoSize);
CHECKALLOC(table->tb_regInfo, table, __LINE__);
}
memcpy(table->tb_regInfo + table->tb_regInfoOffset, regAssemble,
regAssembleSize);
table->tb_regInfoOffset += regAssembleSize;
return TB_STATUS_NO_ERROR;
}
/*
This routine gets a new block to store the table
being built in. It also zeros out the remaining
bytes of the current block.
*/
static Elf32_Sword
getNewBlock (
Xlate_Table_Pro table,
Elf32_Word useDebug,
Elf32_Word newAddress,
Elf32_Word oldAddress
)
{
Block_s *newBlock;
Elf32_Sword blockSize;
Elf32_Sword blockOffset;
newBlock = (Block_s *)malloc(sizeof(Block_s));
CHECKALLOC(newBlock, table, __LINE__);
newBlock->bk_firstNewAddr = newAddress;
newBlock->bk_firstOldAddr = oldAddress;
newBlock->bk_numEntries = 0;
newBlock->bk_next = NULL;
if (useDebug) {
blockSize = table->tb_headerDebug.hd_blockSize;
if (table->tb_blockHeadDebug == NULL) {
table->tb_blockHeadDebug = table->tb_blockTailDebug = newBlock;
}
else {
blockOffset = table->tb_blockOffsetDebug;
while (blockOffset < blockSize)
table->tb_blockTailDebug->bk_data[blockOffset++] = 0;
table->tb_blockTailDebug->bk_next = newBlock;
table->tb_blockTailDebug = newBlock;
}
table->tb_blockOffsetDebug = 0;
table->tb_headerDebug.hd_numBlocks++;
}
else {
blockSize = table->tb_header.hd_blockSize;
if (table->tb_blockHead == NULL) {
table->tb_blockHead = table->tb_blockTail = newBlock;
}
else {
blockOffset = table->tb_blockOffset;
while (blockOffset < blockSize)
table->tb_blockTail->bk_data[blockOffset++] = 0;
table->tb_blockTail->bk_next = newBlock;
table->tb_blockTail = newBlock;
}
table->tb_blockOffset = 0;
table->tb_header.hd_numBlocks++;
}
newBlock->bk_data = malloc(blockSize);
CHECKALLOC(newBlock->bk_data, table, __LINE__);
return TB_STATUS_NO_ERROR;
}
/*
This routine adds a new range entry to the table.
The table should be an order preserving table,
such as for pixie.
*/
Elf32_Sword
xlate_pro_add_range_PO (
Xlate_Table_Pro table,
Elf32_Word newAddress,
Elf32_Word oldAddress
)
{
Elf32_Word newRange;
char newRangeBuf[BUFSIZ];
Elf32_Sword newRangeLength;
Elf32_Word oldRange;
char oldRangeBuf[BUFSIZ];
Elf32_Sword oldRangeLength;
Elf32_Sword retStatus;
if (table == NULL) {
return TB_STATUS_NULL_TABLE;
}
/* New and old addresses specified should always be ascending. */
if (newAddress <= table->tb_prevNewAddr ||
oldAddress <= table->tb_prevOldAddr) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_ADD_ADDR;
}
/* Check that the addresses specified are word-aligned. */
if ((newAddress & 0x3 != 0) || (oldAddress & 0x3 != 0)) {
table->tb_errorLine = __LINE__;
return TB_STATUS_ADDR_UNALIGNED;
}
/* Store ranges in units of instructions, not bytes. */
newRange = newAddress - table->tb_prevNewAddr;
oldRange = oldAddress - table->tb_prevOldAddr;
newRangeLength =
_xlate_leb128_unsigned_encode((newRange >> 2), newRangeBuf);
oldRangeLength =
_xlate_leb128_unsigned_encode((oldRange >> 2), oldRangeBuf);
if (table->tb_putDebug) {
if (table->tb_headerDebug.hd_tableKind != TK_preserve_order) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_TABLEKIND;
}
if (table->tb_blockHeadDebug == NULL ||
(table->tb_blockOffsetDebug + newRangeLength + oldRangeLength >=
table->tb_headerDebug.hd_blockSize)) {
retStatus = getNewBlock(table, TRUE, newAddress, oldAddress);
if (retStatus != TB_STATUS_NO_ERROR)
return retStatus;
}
strncpy(table->tb_blockTailDebug->bk_data + table->tb_blockOffsetDebug,
newRangeBuf, newRangeLength);
table->tb_blockOffsetDebug += newRangeLength;
strncpy(table->tb_blockTailDebug->bk_data + table->tb_blockOffsetDebug,
oldRangeBuf, oldRangeLength);
table->tb_blockOffsetDebug += oldRangeLength;
table->tb_headerDebug.hd_numEntries++;
table->tb_blockTailDebug->bk_numEntries++;
}
else {
if (table->tb_header.hd_tableKind != TK_preserve_order) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_TABLEKIND;
}
if (table->tb_blockHead == NULL ||
(table->tb_blockOffset + newRangeLength + oldRangeLength >=
table->tb_header.hd_blockSize)) {
retStatus = getNewBlock(table, FALSE, newAddress, oldAddress);
if (retStatus != TB_STATUS_NO_ERROR)
return retStatus;
}
strncpy(table->tb_blockTail->bk_data + table->tb_blockOffset,
newRangeBuf, newRangeLength);
table->tb_blockOffset += newRangeLength;
strncpy(table->tb_blockTail->bk_data + table->tb_blockOffset,
oldRangeBuf, oldRangeLength);
table->tb_blockOffset += oldRangeLength;
table->tb_header.hd_numEntries++;
table->tb_blockTail->bk_numEntries++;
}
table->tb_prevNewAddr = newAddress;
table->tb_prevOldAddr = oldAddress;
return TB_STATUS_NO_ERROR;
}
/*
This routine adds a new range entry to the table.
The table must be size preserving, such as for cord.
*/
Elf32_Sword
xlate_pro_add_range_PS (
Xlate_Table_Pro table,
Elf32_Word newAddress,
Elf32_Word oldAddress
)
{
Elf32_Word newRange;
char newRangeBuf[BUFSIZ];
Elf32_Sword newRangeLength;
Elf32_Sword oldAddrDiff;
char oldAddrDiffBuf[BUFSIZ];
Elf32_Sword oldAddrDiffLength;
Elf32_Sword retStatus;
if (table == NULL) {
return TB_STATUS_NULL_TABLE;
}
/* New addresses must be in ascending order. */
if (newAddress <= table->tb_prevNewAddr) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_ADD_ADDR;
}
/* New and old addresses should be word-aligned. */
if ((newAddress & 0x3 != 0) || (oldAddress & 0x3 != 0)) {
table->tb_errorLine = __LINE__;
return TB_STATUS_ADDR_UNALIGNED;
}
/* The numbers in the table are in units of instructions, not bytes. */
newRange = newAddress - table->tb_prevNewAddr;
oldAddrDiff = oldAddress - table->tb_prevOldAddr;
newRangeLength =
_xlate_leb128_unsigned_encode((newRange >> 2), newRangeBuf);
oldAddrDiffLength =
_xlate_leb128_signed_encode((oldAddrDiff >> 2), oldAddrDiffBuf);
if (table->tb_putDebug) {
if (table->tb_headerDebug.hd_tableKind != TK_preserve_size) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_TABLEKIND;
}
if (table->tb_headerDebug.hd_oldAddrLow == 0) {
table->tb_headerDebug.hd_oldAddrLow = oldAddress;
table->tb_headerDebug.hd_oldAddrHigh = oldAddress;
}
else {
table->tb_headerDebug.hd_oldAddrLow =
oldAddress < table->tb_headerDebug.hd_oldAddrLow ?
oldAddress : table->tb_headerDebug.hd_oldAddrLow;
table->tb_headerDebug.hd_oldAddrHigh =
oldAddress > table->tb_headerDebug.hd_oldAddrHigh ?
oldAddress : table->tb_headerDebug.hd_oldAddrHigh;
}
if (table->tb_blockHeadDebug == NULL ||
(table->tb_blockOffsetDebug + newRangeLength + oldAddrDiffLength >=
table->tb_headerDebug.hd_blockSize)) {
retStatus = getNewBlock(table, TRUE, newAddress, oldAddress);
if (retStatus != TB_STATUS_NO_ERROR)
return retStatus;
}
strncpy(table->tb_blockTailDebug->bk_data + table->tb_blockOffsetDebug,
newRangeBuf, newRangeLength);
table->tb_blockOffsetDebug += newRangeLength;
strncpy(table->tb_blockTailDebug->bk_data + table->tb_blockOffsetDebug,
oldAddrDiffBuf, oldAddrDiffLength);
table->tb_blockOffsetDebug += oldAddrDiffLength;
table->tb_headerDebug.hd_numEntries++;
table->tb_blockTailDebug->bk_numEntries++;
}
else {
if (table->tb_header.hd_tableKind != TK_preserve_size) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_TABLEKIND;
}
if (table->tb_header.hd_oldAddrLow == 0) {
table->tb_header.hd_oldAddrLow = oldAddress;
table->tb_header.hd_oldAddrHigh = oldAddress;
}
else {
table->tb_header.hd_oldAddrLow =
oldAddress < table->tb_header.hd_oldAddrLow ?
oldAddress : table->tb_header.hd_oldAddrLow;
table->tb_header.hd_oldAddrHigh =
oldAddress > table->tb_header.hd_oldAddrHigh ?
oldAddress : table->tb_header.hd_oldAddrHigh;
}
if (table->tb_blockHead == NULL ||
(table->tb_blockOffset + newRangeLength + oldAddrDiffLength >=
table->tb_header.hd_blockSize)) {
retStatus = getNewBlock(table, FALSE, newAddress, oldAddress);
if (retStatus != TB_STATUS_NO_ERROR)
return retStatus;
}
strncpy(table->tb_blockTail->bk_data + table->tb_blockOffset,
newRangeBuf, newRangeLength);
table->tb_blockOffset += newRangeLength;
strncpy(table->tb_blockTail->bk_data + table->tb_blockOffset,
oldAddrDiffBuf, oldAddrDiffLength);
table->tb_blockOffset += oldAddrDiffLength;
table->tb_header.hd_numEntries++;
table->tb_blockTail->bk_numEntries++;
}
table->tb_prevNewAddr = newAddress;
table->tb_prevOldAddr = oldAddress;
return TB_STATUS_NO_ERROR;
}
/*
This routine adds a new range entry to the table.
The table must be for general ranges.
*/
Elf32_Sword
xlate_pro_add_range_GE (
Xlate_Table_Pro table,
Elf32_Word newAddress,
Elf32_Sword newRange,
Elf32_Word oldAddress,
Elf32_Sword oldRange
)
{
char newRangeBuf[BUFSIZ];
Elf32_Sword newRangeLength;
Elf32_Sword oldAddrDiff;
char oldAddrDiffBuf[BUFSIZ];
Elf32_Sword oldAddrDiffLength;
char rangeDiffBuf[BUFSIZ];
Elf32_Sword rangeDiffLength;
Elf32_Sword retStatus;
if (table == NULL) {
return TB_STATUS_NULL_TABLE;
}
/* New addresses must always be in ascending order. */
if (newAddress <= table->tb_prevNewAddr) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_ADD_ADDR;
}
/* New and old addresses must be word-aligned. */
if ((newAddress & 0x3 != 0) || (oldAddress & 0x3 != 0)) {
table->tb_errorLine = __LINE__;
return TB_STATUS_ADDR_UNALIGNED;
}
/* The ranges must also be multiples of a word, and not bytes. */
if ((newRange == 0) || (newRange & 0x3 != 0) || (oldRange & 0x3 != 0)) {
table->tb_errorLine = __LINE__;
return TB_STATUS_RANGE_BAD;
}
/* Numbers in the table are in units of instructions, not bytes. */
newRangeLength =
_xlate_leb128_unsigned_encode((newRange >> 2), newRangeBuf);
oldAddrDiff = oldAddress - table->tb_prevOldAddr;
oldAddrDiffLength =
_xlate_leb128_signed_encode((oldAddrDiff >> 2), oldAddrDiffBuf);
rangeDiffLength =
_xlate_leb128_unsigned_encode((newRange - oldRange) >> 2, rangeDiffBuf);
if (table->tb_putDebug) {
if (table->tb_headerDebug.hd_tableKind != TK_general) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_TABLEKIND;
}
if (table->tb_headerDebug.hd_oldAddrLow == 0) {
table->tb_headerDebug.hd_oldAddrLow = oldAddress;
table->tb_headerDebug.hd_oldAddrHigh = oldAddress;
}
else {
table->tb_headerDebug.hd_oldAddrLow =
oldAddress < table->tb_headerDebug.hd_oldAddrLow ?
oldAddress : table->tb_headerDebug.hd_oldAddrLow;
table->tb_headerDebug.hd_oldAddrHigh =
oldAddress > table->tb_headerDebug.hd_oldAddrHigh ?
oldAddress : table->tb_headerDebug.hd_oldAddrHigh;
}
if (table->tb_blockHeadDebug == NULL ||
(table->tb_blockOffsetDebug + newRangeLength + oldAddrDiffLength +
rangeDiffLength >= table->tb_headerDebug.hd_blockSize)) {
retStatus = getNewBlock(table, TRUE, newAddress, oldAddress);
if (retStatus != TB_STATUS_NO_ERROR)
return retStatus;
}
strncpy(table->tb_blockTailDebug->bk_data + table->tb_blockOffsetDebug,
newRangeBuf, newRangeLength);
table->tb_blockOffsetDebug += newRangeLength;
strncpy(table->tb_blockTailDebug->bk_data + table->tb_blockOffsetDebug,
oldAddrDiffBuf, oldAddrDiffLength);
table->tb_blockOffsetDebug += oldAddrDiffLength;
strncpy(table->tb_blockTailDebug->bk_data + table->tb_blockOffsetDebug,
rangeDiffBuf, rangeDiffLength);
table->tb_blockOffsetDebug += rangeDiffLength;
table->tb_headerDebug.hd_numEntries++;
table->tb_blockTailDebug->bk_numEntries++;
}
else {
if (table->tb_header.hd_tableKind != TK_general) {
table->tb_errorLine = __LINE__;
return TB_STATUS_BAD_TABLEKIND;
}
if (table->tb_header.hd_oldAddrLow == 0) {
table->tb_header.hd_oldAddrLow = oldAddress;
table->tb_header.hd_oldAddrHigh = oldAddress;
}
else {
table->tb_header.hd_oldAddrLow =
oldAddress < table->tb_header.hd_oldAddrLow ?
oldAddress : table->tb_header.hd_oldAddrLow;
table->tb_header.hd_oldAddrHigh =
oldAddress > table->tb_header.hd_oldAddrHigh ?
oldAddress : table->tb_header.hd_oldAddrHigh;
}
if (table->tb_blockHead == NULL ||
(table->tb_blockOffset + newRangeLength + oldAddrDiffLength +
rangeDiffLength >= table->tb_header.hd_blockSize)) {
retStatus = getNewBlock(table, FALSE, newAddress, oldAddress);
if (retStatus != TB_STATUS_NO_ERROR)
return retStatus;
}
strncpy(table->tb_blockTail->bk_data + table->tb_blockOffset,
newRangeBuf, newRangeLength);
table->tb_blockOffset += newRangeLength;
strncpy(table->tb_blockTail->bk_data + table->tb_blockOffset,
oldAddrDiffBuf, oldAddrDiffLength);
table->tb_blockOffset += oldAddrDiffLength;
strncpy(table->tb_blockTail->bk_data + table->tb_blockOffset,
rangeDiffBuf, rangeDiffLength);
table->tb_blockOffset += rangeDiffLength;
table->tb_header.hd_numEntries++;
table->tb_blockTail->bk_numEntries++;
}
table->tb_prevNewAddr = newAddress + newRange - 4;
table->tb_prevOldAddr = oldAddress + oldRange - 4;
return TB_STATUS_NO_ERROR;
}
/*
This routine returns the information needed by
the user to obtain the main table. It returns
the number of bytes needed for the complete main
table, the number of blocks the user needs to get.
This routine also sets the first and last, new
and old addresses in the header.
*/
Elf32_Sword
xlate_pro_disk_header (
Xlate_Table_Pro table,
Elf32_Sword *totalMemoryReq,
Elf32_Sword *numBlocks
)
{
Header_s *header;
if (table == NULL) {
return TB_STATUS_NULL_TABLE;
}
if (table->tb_memoryReqRet) {
table->tb_errorLine = __LINE__;
return TB_STATUS_ALREADY_DONE;
}
table->tb_memoryReqRet = TRUE;
header = &table->tb_header;
if (table->tb_blockHead == NULL) {
header->hd_newAddrLow = 0;
header->hd_newAddrHigh = 0;
header->hd_oldAddrLow = 0;
header->hd_oldAddrHigh = 0;
}
else {
if (header->hd_tableKind == TK_preserve_order) {
header->hd_oldAddrLow = table->tb_blockHead->bk_firstOldAddr;
header->hd_oldAddrHigh = table->tb_prevOldAddr;
}
header->hd_newAddrLow = table->tb_blockHead->bk_firstNewAddr;
header->hd_newAddrHigh = table->tb_prevNewAddr;
}
if (!table->tb_putDebug)
header->hd_regInfoSize = table->tb_regInfoOffset;
*totalMemoryReq = sizeof(Header_s) + header->hd_regInfoSize +
(header->hd_numBlocks * (sizeof(BlockHeader_s) + header->hd_blockSize));
*numBlocks = header->hd_numBlocks + (header->hd_regInfoSize == 0 ? 1 : 2);
return TB_STATUS_NO_ERROR;
}
/*
This routine is the same as xlate_pro_disk_header()
except that it returns the debugging table. It
also returns an error if tb_makeDebug is not set for
the table.
*/
Elf32_Sword
xlate_pro_disk_header_debug (
Xlate_Table_Pro table,
Elf32_Sword *totalMemoryReq,
Elf32_Sword *numBlocks
)
{
Header_s *header;
if (table == NULL) {
return TB_STATUS_NULL_TABLE;
}
if (!table->tb_makeDebug) {
table->tb_errorLine = __LINE__;
return TB_STATUS_NO_DEBUG;
}
if (table->tb_memoryReqRetDebug) {
table->tb_errorLine = __LINE__;
return TB_STATUS_ALREADY_DONE;
}
table->tb_memoryReqRetDebug = TRUE;
header = &table->tb_headerDebug;
if (table->tb_blockHeadDebug == NULL) {
header->hd_newAddrLow = 0;
header->hd_newAddrHigh = 0;
header->hd_oldAddrLow = 0;
header->hd_oldAddrHigh = 0;
}
else if (table->tb_putDebug) {
table->tb_errorLine = __LINE__;
return TB_STATUS_NOT_YET_IMPLEMENT;
}
else {
if (header->hd_tableKind == TK_preserve_order) {
header->hd_oldAddrLow = table->tb_blockHeadDebug->bk_firstOldAddr;
header->hd_oldAddrHigh = table->tb_prevOldAddr;
}
header->hd_newAddrLow = table->tb_blockHeadDebug->bk_firstNewAddr;
header->hd_newAddrHigh = table->tb_prevNewAddr;
}
if (table->tb_putDebug)
header->hd_regInfoSize = table->tb_regInfoOffset;
*totalMemoryReq = sizeof(Header_s) + header->hd_regInfoSize +
(header->hd_numBlocks * (sizeof(BlockHeader_s) + header->hd_blockSize));
*numBlocks = header->hd_numBlocks + (header->hd_regInfoSize == 0 ? 1 : 2);
return TB_STATUS_NO_ERROR;
}
/*
This routine returns the blocks of data comprising
the disk representation of the main table sequentially.
On the first call, it returns a copy of the header,
and thereafter the data blocks.
*/
Elf32_Sword
xlate_pro_disk_next_block (
Xlate_Table_Pro table,
void **data,
Elf32_Sword *dataSize
)
{
Header_s *headerRet;
Header_s *header;
BlockHeader_s *blockHeader;
Block_s *blockRet;
Block_s *block;
Elf32_Sword i;
Elf32_Sword headerSize;
char *regInfo;
if (table == NULL) {
return TB_STATUS_NULL_TABLE;
}
if (!table->tb_headerRet) {
table->tb_headerRet = TRUE;
header = &table->tb_header;
headerSize = sizeof(Header_s) +
header->hd_numBlocks * sizeof(BlockHeader_s);
headerRet = (Header_s *)malloc(headerSize);
CHECKALLOC(headerRet, table, __LINE__);
memcpy(headerRet, header, sizeof(Header_s));
block = table->tb_blockHead;
blockHeader = (BlockHeader_s *)(headerRet + 1);
for (i = 0; i < header->hd_numBlocks; i++) {
blockHeader->bh_firstNewAddr = block->bk_firstNewAddr;
blockHeader->bh_firstOldAddr = block->bk_firstOldAddr;
blockHeader->bh_numEntries = block->bk_numEntries;
block = block->bk_next;
blockHeader++;
}
*data = headerRet;
*dataSize = headerSize;
return TB_STATUS_NO_ERROR;
}
blockRet = (table->tb_blockRet == NULL) ?
table->tb_blockHead : table->tb_blockRet->bk_next;
if (blockRet == NULL) {
/* Return the Register Info Block at the very end. */
if (!table->tb_putDebug && table->tb_regInfo != NULL) {
regInfo = malloc(table->tb_regInfoOffset);
CHECKALLOC(regInfo, table, __LINE__);
memcpy(regInfo, table->tb_regInfo, table->tb_regInfoOffset);
*data = regInfo;
*dataSize = table->tb_regInfoOffset;
free(table->tb_regInfo);
table->tb_regInfo = NULL;
table->tb_regInfoOffset = 0;
table->tb_regInfoSize = 0;
return TB_STATUS_NO_ERROR;
}
table->tb_errorLine = __LINE__;
return TB_STATUS_NO_MORE_BLOCKS;
}
table->tb_blockRet = blockRet;
*data = blockRet->bk_data;
*dataSize = table->tb_header.hd_blockSize;
return TB_STATUS_NO_ERROR;
}
/*
This routine is identical to xlate_pro_disk_next_block()
except that it is for the debugging table.
*/
Elf32_Sword
xlate_pro_disk_next_block_debug (
Xlate_Table_Pro table,
void **data,
Elf32_Sword *dataSize
)
{
Header_s *headerRet;
Header_s *header;
BlockHeader_s *blockHeader;
Block_s *blockRet;
Block_s *block;
Elf32_Sword i;
Elf32_Sword headerSize;
char *regInfo;
if (table == NULL) {
return TB_STATUS_NULL_TABLE;
}
if (!table->tb_headerRetDebug) {
table->tb_headerRetDebug = TRUE;
header = &table->tb_headerDebug;
headerSize = sizeof(Header_s) +
header->hd_numBlocks * sizeof(BlockHeader_s);
headerRet = (Header_s *)malloc(headerSize);
CHECKALLOC(headerRet, table, __LINE__);
memcpy(headerRet, header, sizeof(Header_s));
block = table->tb_blockHeadDebug;
blockHeader = (BlockHeader_s *)(headerRet + 1);
for (i = 0; i < header->hd_numBlocks; i++) {
blockHeader->bh_firstNewAddr = block->bk_firstNewAddr;
blockHeader->bh_firstOldAddr = block->bk_firstOldAddr;
blockHeader->bh_numEntries = block->bk_numEntries;
block = block->bk_next;
blockHeader++;
}
*data = headerRet;
*dataSize = headerSize;
return TB_STATUS_NO_ERROR;
}
blockRet = (table->tb_blockRetDebug == NULL) ?
table->tb_blockHeadDebug : table->tb_blockRetDebug->bk_next;
if (blockRet == NULL) {
/* Return the Register Info Block at the very end. */
if (table->tb_putDebug && table->tb_regInfo != NULL) {
regInfo = malloc(table->tb_regInfoOffset);
CHECKALLOC(regInfo, table, __LINE__);
memcpy(regInfo, table->tb_regInfo, table->tb_regInfoOffset);
*data = regInfo;
*dataSize = table->tb_regInfoOffset;
free(table->tb_regInfo);
table->tb_regInfo = NULL;
table->tb_regInfoOffset = 0;
table->tb_regInfoSize = 0;
return TB_STATUS_NO_ERROR;
}
table->tb_errorLine = __LINE__;
return TB_STATUS_NO_MORE_BLOCKS;
}
table->tb_blockRetDebug = blockRet;
*data = blockRet->bk_data;
*dataSize = table->tb_headerDebug.hd_blockSize;
return TB_STATUS_NO_ERROR;
}
/*
This routine deallocates all the memory that has not
been given back to the user, and deallocates the
handle. table is invalid after this call.
*/
Elf32_Sword
xlate_pro_finish (
Xlate_Table_Pro table
)
{
Block_s *block;
Block_s *thisBlock;
if (table == NULL) {
return TB_STATUS_NULL_TABLE;
}
block = (table->tb_blockRet == NULL) ?
table->tb_blockHead : table->tb_blockRet->bk_next;
while (block != NULL) {
free(block->bk_data);
thisBlock = block;
block = block->bk_next;
free(thisBlock);
}
block = (table->tb_blockRetDebug == NULL) ?
table->tb_blockHeadDebug : table->tb_blockRetDebug->bk_next;
while (block != NULL) {
free(block->bk_data);
thisBlock = block;
block = block->bk_next;
free(thisBlock);
}
if (table->tb_regInfo != NULL)
free(table->tb_regInfo);
free(table);
return TB_STATUS_NO_ERROR;
}