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
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
|
/*
Copyright (C) 2015-2019 David Anderson. All Rights Reserved.
This program is free software; you can redistribute it and/or modify it
under the terms of version 2.1 of the GNU Lesser General Public License
as published by the Free Software Foundation.
This program is distributed in the hope that it would be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Further, this software is distributed without any warranty that it is
free of the rightful claim of any third person regarding infringement
or the like. Any license provided herein, whether implied or
otherwise, applies only to this software file. Patent licenses, if
any, provided herein do not apply to combinations of this program with
other software, or any other product whatsoever.
You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
USA.
*/
#include "config.h"
#include <stdio.h>
#include <limits.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#ifdef HAVE_MALLOC_H
/* Useful include for some Windows compilers. */
#include <malloc.h>
#endif /* HAVE_MALLOC_H */
#include "dwarf_incl.h"
#include "dwarf_alloc.h"
#include "dwarf_error.h"
#include "dwarf_util.h"
#include "dwarf_macro5.h"
#define TRUE 1
#define FALSE 0
/* Section 6.3: Macro Information:
Each macro unit ends with an entry
containing an opcode of 0. */
static const Dwarf_Small dwarf_udata_string_form[] = {DW_FORM_udata,DW_FORM_string};
static const Dwarf_Small dwarf_udata_udata_form[] = {DW_FORM_udata,DW_FORM_udata};
static const Dwarf_Small dwarf_udata_strp_form[] = {DW_FORM_udata,DW_FORM_strp};
static const Dwarf_Small dwarf_udata_strp_sup_form[] = {DW_FORM_udata,DW_FORM_strp_sup};
static const Dwarf_Small dwarf_secoffset_form[] = {DW_FORM_sec_offset};
static const Dwarf_Small dwarf_udata_strx_form[] = {DW_FORM_udata,DW_FORM_strx};
struct Dwarf_Macro_Forms_s dw5formsarray[] = {
{0,0,0},
{DW_MACRO_define,2,dwarf_udata_string_form},
{DW_MACRO_undef,2,dwarf_udata_string_form},
{DW_MACRO_start_file,2,dwarf_udata_udata_form},
{DW_MACRO_end_file,0,0},
{DW_MACRO_define_strp,2,dwarf_udata_strp_form},
{DW_MACRO_undef_strp,2,dwarf_udata_strp_form},
{DW_MACRO_import,1,dwarf_secoffset_form},
{DW_MACRO_define_sup,2,dwarf_udata_strp_sup_form},
{DW_MACRO_undef_sup,2,dwarf_udata_strp_sup_form},
{DW_MACRO_import_sup,1,dwarf_secoffset_form},
{DW_MACRO_define_strx,2,dwarf_udata_strx_form},
{DW_MACRO_undef_strx,2,dwarf_udata_strx_form},
};
/* Represents DWARF 5 macro info */
/* .debug_macro predefined, in order by value */
static const struct Dwarf_Macro_OperationsList_s dwarf_default_macro_opslist = {
13, dw5formsarray
};
static int _dwarf_internal_macro_context_by_offset(Dwarf_Debug dbg,
Dwarf_Unsigned offset,
Dwarf_Unsigned * version_out,
Dwarf_Macro_Context * macro_context_out,
Dwarf_Unsigned *macro_ops_count_out,
Dwarf_Unsigned *macro_ops_data_length,
char **srcfiles,
Dwarf_Signed srcfilescount,
const char *comp_dir,
const char *comp_name,
Dwarf_CU_Context cu_context,
Dwarf_Error * error);
static int _dwarf_internal_macro_context(Dwarf_Die die,
Dwarf_Bool offset_specified,
Dwarf_Unsigned offset,
Dwarf_Unsigned * version_out,
Dwarf_Macro_Context * macro_context_out,
Dwarf_Unsigned *macro_unit_offset_out,
Dwarf_Unsigned *macro_ops_count_out,
Dwarf_Unsigned *macro_ops_data_length,
Dwarf_Error * error);
static int
is_std_moperator(Dwarf_Small op)
{
if (op >= 1 && op <= DW_MACRO_undef_strx) {
return TRUE;
}
return FALSE;
}
static int
_dwarf_skim_forms(Dwarf_Debug dbg,
Dwarf_Macro_Context mcontext,
Dwarf_Small *mdata_start,
unsigned formcount,
const Dwarf_Small *forms,
Dwarf_Small *section_end,
Dwarf_Unsigned *forms_length,
Dwarf_Error *error)
{
unsigned i = 0;
Dwarf_Small curform = 0 ;
Dwarf_Unsigned totallen = 0;
Dwarf_Unsigned v = 0;
Dwarf_Unsigned ret_value = 0;
Dwarf_Unsigned length;
Dwarf_Small *mdata = mdata_start;
Dwarf_Unsigned leb128_length = 0;
for( ; i < formcount; ++i) {
curform = forms[i];
if (mdata >= section_end) {
_dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
return DW_DLV_ERROR;
}
switch(curform) {
default:
_dwarf_error(dbg,error,
DW_DLE_DEBUG_FORM_HANDLING_INCOMPLETE);
return DW_DLV_ERROR;
case DW_FORM_block1:
v = *(Dwarf_Small *) mdata;
totallen += v+1;
mdata += v+1;
break;
case DW_FORM_block2:
READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
mdata, DWARF_HALF_SIZE,
error,section_end);
v = ret_value + DWARF_HALF_SIZE;
totallen += v;
mdata += v;
break;
case DW_FORM_block4:
READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
mdata, DWARF_32BIT_SIZE,
error,section_end);
v = ret_value + DWARF_32BIT_SIZE;
totallen += v;
mdata += v;
break;
case DW_FORM_data1:
v = 1;
totallen += v;
mdata += v;
break;
case DW_FORM_data2:
v = 2;
totallen += v;
mdata += v;
break;
case DW_FORM_data4:
v = 4;
totallen += v;
mdata += v;
break;
case DW_FORM_data8:
v = 8;
totallen += v;
mdata += v;
break;
case DW_FORM_data16:
v = 8;
totallen += v;
mdata += v;
break;
case DW_FORM_string: {
int res = _dwarf_check_string_valid(dbg,
mdata,mdata, section_end,
DW_DLE_MACRO_STRING_BAD,error);
if(res != DW_DLV_OK) {
return res;
}
v = strlen((char *) mdata) + 1;
totallen += v;
mdata += v;
}
break;
case DW_FORM_block:
DECODE_LEB128_UWORD_LEN_CK(mdata,length,leb128_length,
dbg, error,section_end);
v = length + leb128_length;
totallen += v;
break;
case DW_FORM_flag:
v = 1;
totallen += v;
mdata += v;
break;
case DW_FORM_sec_offset:
/* If 32bit dwarf, is 4. Else is 64bit dwarf and is 8. */
v = mcontext->mc_offset_size;
totallen += v;
mdata += v;
break;
case DW_FORM_sdata:
/* Discard the decoded value, we just want the length
of the value. */
DECODE_LEB128_UWORD_LEN_CK(mdata,length,leb128_length,
dbg, error,section_end);
totallen += v;
break;
case DW_FORM_strx:
DECODE_LEB128_UWORD_LEN_CK(mdata,length,leb128_length,
dbg, error,section_end);
totallen += leb128_length;;
break;
case DW_FORM_strp:
v = mcontext->mc_offset_size;
mdata += v;
totallen += v;
break;
case DW_FORM_udata:
/* Discard the decoded value, we just want the length
of the value. */
DECODE_LEB128_UWORD_LEN_CK(mdata,length,leb128_length,
dbg, error,section_end);
totallen += leb128_length;
break;
}
}
if (mdata > section_end) {
_dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
return DW_DLV_ERROR;
}
*forms_length = totallen;
return DW_DLV_OK;
}
#if 0 /* FOR DEBUGGING */
static void
dump_bytes(Dwarf_Small * start, long len)
{
Dwarf_Small *end = start + len;
Dwarf_Small *cur = start;
unsigned pos = 0;
printf("dump %ld bytes, start at 0x%lx\n",len,(unsigned long)start);
printf("0x");
for (; cur < end;pos++, cur++) {
if (!(pos %4)) {
printf(" ");
}
printf("%02x",*cur);
}
printf("\n");
}
Dwarf_Bool
is_defundef(unsigned op)
{
switch(op){
case DW_MACRO_define:
case DW_MACRO_undef:
case DW_MACRO_define_strp:
case DW_MACRO_undef_strp:
case DW_MACRO_define_strx:
case DW_MACRO_undef_strx:
case DW_MACRO_define_sup:
case DW_MACRO_undef_sup:
return TRUE;
}
return FALSE;
}
#endif /* FOR DEBUGGING */
/* On first call (for this macro_context),
build_ops_array is FALSE. On second,
it is TRUE and we know the count so we allocate and fill in
the ops array. */
static int
_dwarf_get_macro_ops_count_internal(Dwarf_Macro_Context macro_context,
Dwarf_Bool build_ops_array,
Dwarf_Error *error)
{
Dwarf_Debug dbg = 0;
Dwarf_Small *mdata = 0;
Dwarf_Small *section_end = 0;
Dwarf_Small *section_base = 0;
Dwarf_Unsigned opcount = 0;
Dwarf_Unsigned known_ops_count = 0;
struct Dwarf_Macro_Operator_s *opsarray = 0;
struct Dwarf_Macro_Operator_s *curopsentry = 0;
int res = 0;
dbg = macro_context->mc_dbg;
if (build_ops_array) {
known_ops_count = macro_context->mc_macro_ops_count;
opsarray = (struct Dwarf_Macro_Operator_s *)
calloc(known_ops_count,sizeof(struct Dwarf_Macro_Operator_s));
if(!opsarray) {
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
return DW_DLV_ERROR;
}
curopsentry = opsarray;
macro_context->mc_ops = opsarray;
}
section_base = dbg->de_debug_macro.dss_data;
section_end = section_base + dbg->de_debug_macro.dss_size;
mdata = macro_context->mc_macro_ops;
while (mdata < section_end) {
Dwarf_Small op = 0;
op = *mdata;
++opcount;
++mdata;
if (!op) {
Dwarf_Unsigned opslen = 0;
/* End of ops, this is terminator, count the ending 0
as an operator so dwarfdump can print it. */
opslen = mdata - macro_context->mc_macro_ops;
macro_context->mc_macro_ops_count = opcount;
macro_context->mc_ops_data_length = opslen;
macro_context->mc_total_length = opslen +
macro_context->mc_macro_header_length;
return DW_DLV_OK;
}
if (is_std_moperator(op)) {
struct Dwarf_Macro_Forms_s * ourform =
dw5formsarray + op;
/* ASSERT: op == ourform->mf_code */
unsigned formcount = ourform->mf_formcount;
const Dwarf_Small *forms = ourform->mf_formbytes;
Dwarf_Unsigned forms_length = 0;
res = _dwarf_skim_forms(dbg,macro_context,mdata,
formcount,forms,
section_end,
&forms_length,error);
if ( res != DW_DLV_OK) {
return res;
}
if(build_ops_array) {
curopsentry->mo_opcode = op;
curopsentry->mo_form = ourform;
curopsentry->mo_data = mdata;
}
mdata += forms_length;
} else {
/* FIXME Add support for user defined ops. */
_dwarf_error(dbg, error, DW_DLE_MACRO_OP_UNHANDLED);
return DW_DLV_ERROR;
}
if (mdata > section_end) {
_dwarf_error(dbg, error, DW_DLE_MACRO_PAST_END);
return DW_DLV_ERROR;
}
if (build_ops_array) {
curopsentry++;
}
}
_dwarf_error(dbg, error, DW_DLE_MACRO_PAST_END);
return DW_DLV_ERROR;
}
int
dwarf_get_macro_op(Dwarf_Macro_Context macro_context,
Dwarf_Unsigned op_number,
Dwarf_Unsigned * op_start_section_offset,
Dwarf_Half * macro_operator,
Dwarf_Half * forms_count,
const Dwarf_Small ** formcode_array,
Dwarf_Error *error)
{
struct Dwarf_Macro_Operator_s *curop = 0;
Dwarf_Debug dbg = 0;
if (!macro_context || macro_context->mc_sentinel != 0xada) {
if(macro_context) {
dbg = macro_context->mc_dbg;
}
_dwarf_error(dbg, error,DW_DLE_BAD_MACRO_HEADER_POINTER);
return DW_DLV_ERROR;
}
dbg = macro_context->mc_dbg;
if (op_number >= macro_context->mc_macro_ops_count) {
_dwarf_error(dbg, error,DW_DLE_BAD_MACRO_INDEX);
return DW_DLV_ERROR;
}
curop = macro_context->mc_ops + op_number;
/* ASSERT: *op_start_section_offset ==
(curop->mo_data -1) - dbg->de_debug_macro.dss_data */
*op_start_section_offset =
((curop->mo_data -1) - macro_context->mc_macro_header) +
macro_context->mc_section_offset;
*macro_operator = curop->mo_opcode;
if (curop->mo_form) {
*forms_count = curop->mo_form->mf_formcount;
*formcode_array = curop->mo_form->mf_formbytes;
} else {
/* ASSERT: macro_operator == 0 */
*forms_count = 0;
*formcode_array = 0;
}
return DW_DLV_OK;
}
/* Here a DW_DLV_NO_ENTRY return means the macro operator
is not a def/undef operator. */
int
dwarf_get_macro_defundef(Dwarf_Macro_Context macro_context,
Dwarf_Unsigned op_number,
Dwarf_Unsigned * line_number,
Dwarf_Unsigned * index,
Dwarf_Unsigned * offset,
Dwarf_Half * forms_count,
const char ** macro_string,
Dwarf_Error *error)
{
Dwarf_Debug dbg = 0;
Dwarf_Small *mdata = 0;
int res = 0;
Dwarf_Small *startptr = 0;
Dwarf_Small *endptr = 0;
Dwarf_Half lformscount = 0;
struct Dwarf_Macro_Operator_s *curop = 0;
unsigned macop = 0;
if (!macro_context || macro_context->mc_sentinel != 0xada) {
if(macro_context) {
dbg = macro_context->mc_dbg;
}
_dwarf_error(dbg, error,DW_DLE_BAD_MACRO_HEADER_POINTER);
return DW_DLV_ERROR;
}
dbg = macro_context->mc_dbg;
if (op_number >= macro_context->mc_macro_ops_count) {
_dwarf_error(dbg, error,DW_DLE_BAD_MACRO_INDEX);
return DW_DLV_ERROR;
}
curop = macro_context->mc_ops + op_number;
macop = curop->mo_opcode;
startptr = macro_context->mc_macro_header;
endptr = startptr + macro_context->mc_total_length;
mdata = curop->mo_data;
lformscount = curop->mo_form->mf_formcount;
if (lformscount != 2) {
/*_dwarf_error(dbg, error,DW_DLE_MACRO_OPCODE_FORM_BAD);*/
return DW_DLV_NO_ENTRY;
}
switch(macop){
case DW_MACRO_define:
case DW_MACRO_undef: {
Dwarf_Unsigned linenum = 0;
const char * content = 0;
DECODE_LEB128_UWORD_CK(mdata,linenum,
dbg, error,endptr);
content = (const char *)mdata;
res = _dwarf_check_string_valid(dbg,
startptr,mdata, endptr,
DW_DLE_MACRO_STRING_BAD,error);
if(res != DW_DLV_OK) {
return res;
}
*line_number = linenum;
*index = 0;
*offset = 0;
*forms_count = lformscount;
*macro_string = content;
}
return DW_DLV_OK;
case DW_MACRO_define_strp:
case DW_MACRO_undef_strp: {
Dwarf_Unsigned linenum = 0;
Dwarf_Unsigned stringoffset = 0;
Dwarf_Small form1 = curop->mo_form->mf_formbytes[1];
char * localstr = 0;
DECODE_LEB128_UWORD_CK(mdata,linenum,
dbg, error,endptr);
READ_UNALIGNED_CK(dbg,stringoffset,Dwarf_Unsigned,
mdata,macro_context->mc_offset_size,
error,endptr);
res = _dwarf_extract_local_debug_str_string_given_offset(dbg,
form1,
stringoffset,
&localstr,
error);
*index = 0;
*line_number = linenum;
*offset = stringoffset;
*forms_count = lformscount;
if (res == DW_DLV_ERROR) {
*macro_string = "<Error: getting local .debug_str>";
return res;
} else if (res == DW_DLV_NO_ENTRY) {
*macro_string = "<Error: NO_ENTRY on .debug_string (strp)>";
} else {
*macro_string = (const char *)localstr;
}
}
return DW_DLV_OK;
case DW_MACRO_define_strx:
case DW_MACRO_undef_strx: {
Dwarf_Unsigned linenum = 0;
Dwarf_Unsigned stringindex = 0;
Dwarf_Unsigned offsettostr= 0;
int ress = 0;
Dwarf_Byte_Ptr mdata_copy = 0;
Dwarf_Small form1 = curop->mo_form->mf_formbytes[1];
DECODE_LEB128_UWORD_CK(mdata,linenum, dbg, error,endptr);
*line_number = linenum;
mdata_copy = mdata;
DECODE_LEB128_UWORD_CK(mdata_copy,stringindex, dbg, error,endptr);
/* mdata_copy is for call below */
*index = stringindex;
*forms_count = lformscount;
/* Redoes the index-getting. Gets offset. */
ress = _dwarf_extract_string_offset_via_str_offsets(dbg,
mdata_copy,
endptr,
DW_AT_macros, /*arbitrary, unused by called routine. */
form1,
macro_context->mc_cu_context,
&offsettostr,
error);
if (ress == DW_DLV_ERROR) {
return ress;
}
if (ress == DW_DLV_OK) {
char *localstr = 0;
*index = stringindex;
*offset = offsettostr;
ress = _dwarf_extract_local_debug_str_string_given_offset(dbg,
form1,
offsettostr,
&localstr,
error);
if(ress == DW_DLV_ERROR) {
return ress;
} else if (ress == DW_DLV_NO_ENTRY){
*macro_string = "<:No string available>";
} else {
*macro_string = (const char *)localstr;
/* All is ok. */
}
} else {
*index = stringindex;
*offset = 0;
*macro_string = "<.debug_str_offsets not available>";
}
}
return DW_DLV_OK;
case DW_MACRO_define_sup:
case DW_MACRO_undef_sup: {
Dwarf_Unsigned linenum = 0;
Dwarf_Unsigned supoffset = 0;
char *localstring = 0;
int resup = 0;
Dwarf_Error lerr = 0;
DECODE_LEB128_UWORD_CK(mdata,linenum,
dbg, error,endptr);
READ_UNALIGNED_CK(dbg,supoffset,Dwarf_Unsigned,
mdata,macro_context->mc_offset_size,
error,endptr);
*line_number = linenum;
*index = 0;
*offset = supoffset;
*forms_count = lformscount;
resup = _dwarf_get_string_from_tied(dbg, supoffset,
&localstring, &lerr);
if (resup != DW_DLV_OK) {
if (resup == DW_DLV_ERROR) {
int myerrno = dwarf_errno(lerr);
if(myerrno == DW_DLE_NO_TIED_FILE_AVAILABLE) {
*macro_string =
(char *)"<DW_FORM_str_sup-no-tied_file>";
} else {
_dwarf_error(dbg,error,myerrno);
*macro_string =
(char *)"<Error: DW_FORM_str_sup-got-error>";
}
dwarf_dealloc(dbg,lerr,DW_DLA_ERROR);
} else {
*macro_string = "<DW_FORM_str_sup-no-entry>";
}
return resup;
}
*macro_string = (const char *)localstring;
/* If NO ENTRY available, return DW_DLV_NO_ENTRY.
We suspect this is better than DW_DLV_OK. */
return resup;
}
default:
_dwarf_error(dbg,error,DW_DLE_MACRO_OP_UNHANDLED);
return DW_DLV_ERROR;
}
return DW_DLV_NO_ENTRY;
}
/* ASSERT: we elsewhere guarantee room to copy into.
If trimtarg ==1, trim trailing slash in targ.
Caller should not pass in 'src'
with leading / */
static void
specialcat(char *targ,char *src,int trimtarg)
{
char *last = 0;
while( *targ) {
last = targ;
targ++;
}
/* TARG now points at terminating NUL */
/* LAST points at final character in targ. */
if (trimtarg ) {
if(last && *last == '/') {
/* Truncate. */
*last = 0;
targ = last;
/* TARG again points at terminating NUL */
}
}
while (*src) {
*targ = *src;
targ++;
src++;
}
*targ = 0;
}
/* If returns NULL caller must handle it. */
static const char *
construct_from_dir_and_name(const char *dir,
const char *name)
{
int truelen = 0;
char *final = 0;
/* Allow for NUL char and added / */
truelen = strlen(dir) + strlen(name) + 1 +1;
final = (char *)malloc(truelen);
if(!final) {
return NULL;
}
final[0] = 0;
specialcat(final,(char *)dir,1);
strcat(final,"/");
specialcat(final,(char *)name,0);
return final;
}
/* If returns NULL caller must handle it. */
static const char *
construct_at_path_from_parts(Dwarf_Macro_Context mc)
{
if (mc->mc_file_path) {
return mc->mc_file_path;
}
if(!mc->mc_at_comp_dir || !mc->mc_at_comp_dir[0]) {
return mc->mc_at_name;
}
if (!mc->mc_at_name || !mc->mc_at_name[0]) {
return NULL;
}
if(_dwarf_file_name_is_full_path((Dwarf_Small *)mc->mc_at_name)) {
return mc->mc_at_name;
}
/* Dwarf_Macro_Context destructor will free this. */
mc->mc_file_path = construct_from_dir_and_name(
mc->mc_at_comp_dir,mc->mc_at_name);
return mc->mc_file_path;
}
int
dwarf_get_macro_startend_file(Dwarf_Macro_Context macro_context,
Dwarf_Unsigned op_number,
Dwarf_Unsigned * line_number,
Dwarf_Unsigned * name_index_to_line_tab,
const char ** src_file_name,
Dwarf_Error *error)
{
Dwarf_Debug dbg = 0;
Dwarf_Small *mdata = 0;
unsigned macop = 0;
struct Dwarf_Macro_Operator_s *curop = 0;
Dwarf_Byte_Ptr startptr = 0;
Dwarf_Byte_Ptr endptr = 0;
if (!macro_context || macro_context->mc_sentinel != 0xada) {
if(macro_context) {
dbg = macro_context->mc_dbg;
}
_dwarf_error(dbg, error,DW_DLE_BAD_MACRO_HEADER_POINTER);
return DW_DLV_ERROR;
}
dbg = macro_context->mc_dbg;
if (op_number >= macro_context->mc_macro_ops_count) {
_dwarf_error(dbg, error,DW_DLE_BAD_MACRO_INDEX);
return DW_DLV_ERROR;
}
startptr = macro_context->mc_macro_header;
endptr = startptr + macro_context->mc_total_length;
curop = macro_context->mc_ops + op_number;
macop = curop->mo_opcode;
mdata = curop->mo_data;
if (macop != DW_MACRO_start_file && macop != DW_MACRO_end_file) {
return DW_DLV_NO_ENTRY;
}
if (macop == DW_MACRO_start_file) {
Dwarf_Unsigned linenum = 0;
Dwarf_Unsigned srcindex = 0;
Dwarf_Signed trueindex = 0;
DECODE_LEB128_UWORD_CK(mdata,linenum, dbg, error,endptr);
DECODE_LEB128_UWORD_CK(mdata,srcindex, dbg, error,endptr);
*line_number = linenum;
*name_index_to_line_tab = srcindex;
/* For DWARF 2,3,4, decrement by 1.
FOR DWARF 5 do not decrement. */
if(macro_context->mc_version_number >= 5) {
trueindex = srcindex;
if (trueindex < 0) {
*src_file_name = "<source-file-index-low-no-name-available>";
return DW_DLV_OK;
}
if (trueindex < macro_context->mc_srcfiles_count) {
*src_file_name = macro_context->mc_srcfiles[trueindex];
return DW_DLV_OK;
} else {
*src_file_name =
"<src-index-high-no-source-file-name-available>";
return DW_DLV_OK;
}
} else {
/* Unsigned to signed here. */
trueindex = srcindex;
/* Protects against crazy big srcindex, overflow territory. */
if (trueindex < 0 ) {
/* Something insane here. */
*src_file_name = "<source-file-index-low-no-name-available>";
return DW_DLV_OK;
}
/* Protects against crazy big srcindex, overflow territory. */
if (trueindex > (macro_context->mc_srcfiles_count+1)) {
/* Something insane here. */
*src_file_name =
"<source-file-index-high-no-name-available>";
return DW_DLV_OK;
}
--trueindex;
if (trueindex > macro_context->mc_srcfiles_count) {
*src_file_name =
"<adjusted-source-file-index-high-no-name-available>";
}
if (srcindex > 0 &&
trueindex < macro_context->mc_srcfiles_count) {
*src_file_name = macro_context->mc_srcfiles[trueindex];
} else {
const char *mcatcomp = construct_at_path_from_parts(
macro_context);
if(mcatcomp) {
*src_file_name = mcatcomp;
} else {
*src_file_name = "<no-source-file-name-available>";
}
}
}
} else {
/* DW_MACRO_end_file. No operands. */
}
return DW_DLV_OK;
}
/* Target_offset is the offset in a .debug_macro section,
of a macro unit header.
Returns DW_DLV_NO_ENTRY if the macro operator is not
one of the import operators. */
int
dwarf_get_macro_import(Dwarf_Macro_Context macro_context,
Dwarf_Unsigned op_number,
Dwarf_Unsigned * target_offset,
Dwarf_Error *error)
{
Dwarf_Unsigned supoffset = 0;
Dwarf_Debug dbg = 0;
unsigned macop = 0;
struct Dwarf_Macro_Operator_s *curop = 0;
Dwarf_Small *mdata = 0;
Dwarf_Byte_Ptr startptr = 0;
Dwarf_Byte_Ptr endptr = 0;
if (!macro_context || macro_context->mc_sentinel != 0xada) {
if(macro_context) {
dbg = macro_context->mc_dbg;
}
_dwarf_error(dbg, error,DW_DLE_BAD_MACRO_HEADER_POINTER);
return DW_DLV_ERROR;
}
startptr = macro_context->mc_macro_header;
endptr = startptr + macro_context->mc_total_length;
dbg = macro_context->mc_dbg;
if (op_number >= macro_context->mc_macro_ops_count) {
_dwarf_error(dbg, error,DW_DLE_BAD_MACRO_INDEX);
return DW_DLV_ERROR;
}
curop = macro_context->mc_ops + op_number;
macop = curop->mo_opcode;
mdata = curop->mo_data;
if (macop != DW_MACRO_import && macop != DW_MACRO_import_sup) {
return DW_DLV_NO_ENTRY;
}
READ_UNALIGNED_CK(dbg,supoffset,Dwarf_Unsigned,
mdata,macro_context->mc_offset_size,
error,endptr);
*target_offset = supoffset;
return DW_DLV_OK;
}
/* */
static int
valid_macro_form(Dwarf_Half form)
{
switch(form) {
case DW_FORM_block:
case DW_FORM_block1:
case DW_FORM_block2:
case DW_FORM_block4:
case DW_FORM_data1:
case DW_FORM_data2:
case DW_FORM_data4:
case DW_FORM_data8:
case DW_FORM_data16:
case DW_FORM_sdata:
case DW_FORM_udata:
case DW_FORM_flag:
case DW_FORM_sec_offset:
case DW_FORM_string:
case DW_FORM_strp:
case DW_FORM_strx:
return TRUE;
}
return FALSE;
}
static int
validate_opcode(Dwarf_Debug dbg,
struct Dwarf_Macro_Forms_s *curform,
Dwarf_Error * error)
{
unsigned i = 0;
struct Dwarf_Macro_Forms_s *stdfptr = 0;
if (curform->mf_code >= DW_MACRO_lo_user) {
/* Nothing to check. user level. */
return DW_DLV_OK;
}
if (curform->mf_code > DW_MACRO_undef_strx) {
_dwarf_error(dbg, error, DW_DLE_MACRO_OPCODE_BAD);
return (DW_DLV_ERROR);
}
if (!curform->mf_code){
_dwarf_error(dbg, error, DW_DLE_MACRO_OPCODE_BAD);
return (DW_DLV_ERROR);
}
stdfptr = &dwarf_default_macro_opslist.mol_data[curform->mf_code];
if (curform->mf_formcount != stdfptr->mf_formcount) {
_dwarf_error(dbg, error, DW_DLE_MACRO_OPCODE_FORM_BAD);
return (DW_DLV_ERROR);
}
for(i = 0; i < curform->mf_formcount; ++i) {
if (curform->mf_formbytes[i] != stdfptr->mf_formbytes[1]) {
_dwarf_error(dbg, error, DW_DLE_MACRO_OPCODE_FORM_BAD);
return (DW_DLV_ERROR);
}
}
return DW_DLV_OK;
}
static int
read_operands_table(Dwarf_Macro_Context macro_context,
Dwarf_Small * macro_header,
Dwarf_Small * macro_data,
Dwarf_Small * section_base,
Dwarf_Unsigned section_size,
Dwarf_Unsigned *table_size_out,
Dwarf_Error *error)
{
Dwarf_Small* table_data_start = macro_data;
Dwarf_Unsigned local_size = 0;
Dwarf_Unsigned cur_offset = 0;
Dwarf_Small operand_table_count = 0;
unsigned i = 0;
struct Dwarf_Macro_Forms_s *curformentry = 0;
Dwarf_Debug dbg = 0;
Dwarf_Byte_Ptr startptr = 0;
Dwarf_Byte_Ptr endptr = 0;
if (!macro_context || macro_context->mc_sentinel != 0xada) {
if(macro_context) {
dbg = macro_context->mc_dbg;
}
_dwarf_error(dbg, error,DW_DLE_BAD_MACRO_HEADER_POINTER);
return DW_DLV_ERROR;
}
dbg = macro_context->mc_dbg;
cur_offset = (1+ macro_data) - macro_header;
if (cur_offset >= section_size) {
_dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
return (DW_DLV_ERROR);
}
startptr = macro_context->mc_macro_header;
endptr = startptr + macro_context->mc_total_length;
READ_UNALIGNED_CK(dbg,operand_table_count,Dwarf_Small,
macro_data,sizeof(Dwarf_Small),error,endptr);
macro_data += sizeof(Dwarf_Small);
/* Estimating minimum size */
local_size = operand_table_count * 4;
cur_offset = (local_size+ macro_data) - section_base;
if (cur_offset >= section_size) {
_dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
return (DW_DLV_ERROR);
}
/* first, get size of table. */
table_data_start = macro_data;
for (i = 0; i < operand_table_count; ++i) {
/* Compiler warning about unused opcode_number
variable should be ignored. */
UNUSEDARG Dwarf_Small opcode_number = 0;
Dwarf_Unsigned formcount = 0;
READ_UNALIGNED_CK(dbg,opcode_number,Dwarf_Small,
macro_data,sizeof(Dwarf_Small),error,endptr);
macro_data += sizeof(Dwarf_Small);
DECODE_LEB128_UWORD_CK(macro_data,formcount,
dbg, error, endptr);
cur_offset = (formcount+ macro_data) - section_base;
if (cur_offset >= section_size) {
_dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
return (DW_DLV_ERROR);
}
/* The 1 ubyte forms follow. Step past them. */
macro_data += formcount;
}
/* reset for reread. */
macro_data = table_data_start;
/* allocate table */
macro_context->mc_opcode_forms = (struct Dwarf_Macro_Forms_s *)
calloc(operand_table_count,
sizeof(struct Dwarf_Macro_Forms_s));
macro_context->mc_opcode_count = operand_table_count;
if(!macro_context->mc_opcode_forms) {
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
return DW_DLV_ERROR;
}
curformentry = macro_context->mc_opcode_forms;
for (i = 0; i < operand_table_count; ++i,++curformentry) {
Dwarf_Small opcode_number = 0;
Dwarf_Unsigned formcount = 0;
int res = 0;
cur_offset = (2 + macro_data) - section_base;
if (cur_offset >= section_size) {
_dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
return (DW_DLV_ERROR);
}
READ_UNALIGNED_CK(dbg,opcode_number,Dwarf_Small,
macro_data,sizeof(Dwarf_Small),
error,endptr);
macro_data += sizeof(Dwarf_Small);
DECODE_LEB128_UWORD_CK(macro_data,formcount,
dbg, error, endptr);
curformentry->mf_code = opcode_number;
curformentry->mf_formcount = formcount;
cur_offset = (formcount+ macro_data) - section_base;
if (cur_offset >= section_size) {
_dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
return (DW_DLV_ERROR);
}
curformentry->mf_formbytes = macro_data;
macro_data += formcount;
if (opcode_number > DW_MACRO_undef_strx ) {
Dwarf_Half k = 0;
for(k = 0; k < formcount; ++k) {
if (!valid_macro_form(curformentry->mf_formbytes[k])) {
_dwarf_error(dbg, error, DW_DLE_MACRO_OP_UNHANDLED);
return (DW_DLV_ERROR);
}
}
}
res = validate_opcode(macro_context->mc_dbg,curformentry, error);
if(res != DW_DLV_OK) {
return res;
}
}
*table_size_out = macro_data - table_data_start;
return DW_DLV_OK;
}
/* This is not the normal srcfiles from dwarf_srcfiles.
See translate translate_srcfiles_to_srcfiles2().
It is a list, but the contents were directly malloc,
not _dwarf_get_alloc.
*/
static void
dealloc_macro_srcfiles(char ** srcfiles,
Dwarf_Signed srcfiles_count)
{
Dwarf_Signed i = 0;
if (!srcfiles || !srcfiles_count) {
return;
}
for (i = 0; i < srcfiles_count; ++i) {
if (srcfiles[i]) {
free(srcfiles[i]);
srcfiles[i] = 0;
}
}
free(srcfiles);
}
/* This makes the macro context safe from
duplicate frees in case of error. */
static int
translate_srcfiles_to_srcfiles2(char **srcfiles,
Dwarf_Signed srcfiles_count,
char **srcfiles2)
{
Dwarf_Signed i = 0;
for(i = 0; i < srcfiles_count; ++i) {
char * ostr = 0;
char * newstr = 0;
size_t slen = 0;
ostr = srcfiles[i];
slen = strlen(ostr);
newstr = calloc(1,slen+1);
if (!newstr) {
return DW_DLV_ERROR;
}
strcpy(newstr,ostr);
srcfiles2[i] = newstr;
}
return DW_DLV_OK;
}
static void
drop_srcfiles(Dwarf_Debug dbg,char ** srcfiles,
Dwarf_Signed srcfiles_count)
{
Dwarf_Signed i = 0;
for (i = 0; i < srcfiles_count; ++i) {
if(srcfiles[i]) {
dwarf_dealloc(dbg, srcfiles[i], DW_DLA_STRING);
}
}
dwarf_dealloc(dbg, srcfiles, DW_DLA_LIST);
}
static int
_dwarf_internal_macro_context(Dwarf_Die die,
Dwarf_Bool offset_specified,
Dwarf_Unsigned offset_in,
Dwarf_Unsigned * version_out,
Dwarf_Macro_Context * macro_context_out,
Dwarf_Unsigned * macro_unit_offset_out,
Dwarf_Unsigned * macro_ops_count_out,
Dwarf_Unsigned * macro_ops_data_length,
Dwarf_Error * error)
{
Dwarf_CU_Context cu_context = 0;
/* The Dwarf_Debug this die belongs to. */
Dwarf_Debug dbg = 0;
int resattr = DW_DLV_ERROR;
int lres = DW_DLV_ERROR;
int res = DW_DLV_ERROR;
Dwarf_Unsigned macro_offset = 0;
Dwarf_Attribute macro_attr = 0;
Dwarf_Signed srcfiles_count = 0;
Dwarf_Signed srcfiles2_count = 0;
char ** srcfiles = 0;
/* srcfiles uses dwarf_get_alloc for strings
so dealloc_macro_srcfiles() here will result in double-dealloc
when dwarf_finish() happens to see the string deallocs
before the macro context dealloc (the context dealloc
will call dealloc_macro_srcfiles() !).
Also see the comment at _dwarf_macro_destructor() here.
*/
char ** srcfiles2 = 0;
const char *comp_dir = 0;
const char *comp_name = 0;
/* ***** BEGIN CODE ***** */
if (error != NULL) {
*error = NULL;
}
CHECK_DIE(die, DW_DLV_ERROR);
cu_context = die->di_cu_context;
dbg = cu_context->cc_dbg;
/* Doing the load here results in duplication of the
section-load call (in the by_offset
interface below) but detects the missing section
quickly. */
res = _dwarf_load_section(dbg, &dbg->de_debug_macro,error);
if (res != DW_DLV_OK) {
return res;
}
if (!dbg->de_debug_macro.dss_size) {
return (DW_DLV_NO_ENTRY);
}
resattr = dwarf_attr(die, DW_AT_macros, ¯o_attr, error);
if (resattr == DW_DLV_NO_ENTRY) {
resattr = dwarf_attr(die, DW_AT_GNU_macros, ¯o_attr, error);
}
if (resattr != DW_DLV_OK) {
return resattr;
}
if (!offset_specified) {
lres = dwarf_global_formref(macro_attr, ¯o_offset, error);
if (lres != DW_DLV_OK) {
dwarf_dealloc(dbg,macro_attr,DW_DLA_ATTR);
return lres;
}
} else {
macro_offset = offset_in;
}
lres = dwarf_srcfiles(die,&srcfiles,&srcfiles_count, error);
if (lres == DW_DLV_ERROR) {
dwarf_dealloc(dbg,macro_attr,DW_DLA_ATTR);
return lres;
}
lres = _dwarf_internal_get_die_comp_dir(die, &comp_dir,
&comp_name,error);
if (lres == DW_DLV_ERROR) {
drop_srcfiles(dbg,srcfiles,srcfiles_count);
srcfiles = 0;
srcfiles_count = 0;
dwarf_dealloc(dbg,macro_attr,DW_DLA_ATTR);
srcfiles = 0;
return lres;
}
*macro_unit_offset_out = macro_offset;
/* We cannot use space allocated by
_dwarf_get_alloc() in the macro_context
we will allocate shortly.
So copy from what we have to a similar data set
but malloc space directly. */
if (srcfiles_count > 0) {
srcfiles2 = (char **) calloc(srcfiles_count, sizeof(char *));
if (!srcfiles2) {
dwarf_dealloc(dbg,macro_attr,DW_DLA_ATTR);
drop_srcfiles(dbg,srcfiles,srcfiles_count);
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
return (DW_DLV_ERROR);
}
lres = translate_srcfiles_to_srcfiles2(srcfiles,
srcfiles_count,srcfiles2);
drop_srcfiles(dbg,srcfiles,srcfiles_count);
srcfiles2_count = srcfiles_count;
srcfiles = 0;
srcfiles_count = 0;
if (lres != DW_DLV_OK) {
dwarf_dealloc(dbg,macro_attr,DW_DLA_ATTR);
dealloc_macro_srcfiles(srcfiles2, srcfiles2_count);
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
return lres;
}
} else {
drop_srcfiles(dbg,srcfiles,srcfiles_count);
srcfiles = 0;
srcfiles_count = 0;
}
dwarf_dealloc(dbg,macro_attr,DW_DLA_ATTR);
/* NO ENTRY or OK we accept, though NO ENTRY means there
are no source files available. */
lres = _dwarf_internal_macro_context_by_offset(dbg,
macro_offset,version_out,macro_context_out,
macro_ops_count_out,
macro_ops_data_length,
srcfiles2,srcfiles2_count,
comp_dir,
comp_name,
cu_context,
error);
/* In case of ERROR or NO_ENTRY srcfiles2 is already freed. */
return lres;
}
static int
_dwarf_internal_macro_context_by_offset(Dwarf_Debug dbg,
Dwarf_Unsigned offset,
Dwarf_Unsigned * version_out,
Dwarf_Macro_Context * macro_context_out,
Dwarf_Unsigned * macro_ops_count_out,
Dwarf_Unsigned * macro_ops_data_length,
char **srcfiles,
Dwarf_Signed srcfilescount,
const char *comp_dir,
const char *comp_name,
Dwarf_CU_Context cu_context,
Dwarf_Error * error)
{
Dwarf_Unsigned line_table_offset = 0;
Dwarf_Small * macro_header = 0;
Dwarf_Small * macro_data = 0;
Dwarf_Unsigned version = 0;
Dwarf_Unsigned flags = 0;
Dwarf_Small offset_size = 4;
Dwarf_Unsigned cur_offset = 0;
Dwarf_Unsigned section_size = 0;
Dwarf_Small *section_base = 0;
Dwarf_Small *section_end = 0;
Dwarf_Unsigned optablesize = 0;
Dwarf_Unsigned macro_offset = offset;
int res = 0;
Dwarf_Macro_Context macro_context = 0;
Dwarf_Bool build_ops_array = FALSE;
res = _dwarf_load_section(dbg, &dbg->de_debug_macro,error);
if (res != DW_DLV_OK) {
dealloc_macro_srcfiles(srcfiles,srcfilescount);
return res;
}
if (!dbg->de_debug_macro.dss_size) {
dealloc_macro_srcfiles(srcfiles,srcfilescount);
return (DW_DLV_NO_ENTRY);
}
section_base = dbg->de_debug_macro.dss_data;
section_size = dbg->de_debug_macro.dss_size;
/* The '3' ensures the header initial bytes present too. */
if ((3+macro_offset) >= section_size) {
dealloc_macro_srcfiles(srcfiles,srcfilescount);
_dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
return (DW_DLV_ERROR);
}
macro_header = macro_offset + section_base;
macro_data = macro_header;
section_end = section_base +section_size;
macro_context = (Dwarf_Macro_Context)
_dwarf_get_alloc(dbg,DW_DLA_MACRO_CONTEXT,1);
if (!macro_context) {
dealloc_macro_srcfiles(srcfiles,srcfilescount);
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
return DW_DLV_ERROR;
}
if ((section_base + DWARF_HALF_SIZE + sizeof(Dwarf_Small)) > section_end ) {
dealloc_macro_srcfiles(srcfiles,srcfilescount);
dwarf_dealloc_macro_context(macro_context);
_dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
return DW_DLV_ERROR;
}
/* Note here so if error return we get these freed eventually. */
macro_context->mc_srcfiles = srcfiles;
macro_context->mc_srcfiles_count = srcfilescount;
macro_context->mc_cu_context = cu_context;
res = _dwarf_read_unaligned_ck_wrapper(dbg,
&version,macro_data,DWARF_HALF_SIZE,section_end,
error);
if (res != DW_DLV_OK) {
dwarf_dealloc_macro_context(macro_context);
return res;
}
macro_data += DWARF_HALF_SIZE;
res = _dwarf_read_unaligned_ck_wrapper(dbg,
&flags,macro_data,sizeof(Dwarf_Small),section_end,
error);
if (res != DW_DLV_OK) {
dwarf_dealloc_macro_context(macro_context);
return res;
}
macro_data += sizeof(Dwarf_Small);
macro_context->mc_at_comp_dir = comp_dir;
macro_context->mc_at_name = comp_name;
macro_context->mc_macro_header = macro_header;
macro_context->mc_section_offset = macro_offset;
macro_context->mc_version_number = version;
macro_context->mc_flags = flags;
macro_context->mc_dbg = dbg;
macro_context->mc_offset_size_flag =
flags& MACRO_OFFSET_SIZE_FLAG?TRUE:FALSE;
macro_context->mc_debug_line_offset_flag =
flags& MACRO_LINE_OFFSET_FLAG?TRUE:FALSE;
macro_context->mc_operands_table_flag =
flags& MACRO_OP_TABLE_FLAG?TRUE:FALSE;
offset_size = macro_context->mc_offset_size_flag?8:4;
macro_context->mc_offset_size = offset_size;
if (macro_context->mc_debug_line_offset_flag) {
cur_offset = (offset_size+ macro_data) - section_base;
if (cur_offset >= section_size) {
dwarf_dealloc_macro_context(macro_context);
_dwarf_error(dbg, error, DW_DLE_MACRO_OFFSET_BAD);
return (DW_DLV_ERROR);
}
res = _dwarf_read_unaligned_ck_wrapper(dbg,
&line_table_offset,macro_data,
offset_size,section_end,
error);
if (res != DW_DLV_OK) {
dwarf_dealloc_macro_context(macro_context);
return res;
}
macro_data += offset_size;
macro_context->mc_debug_line_offset = line_table_offset;
}
if (macro_context->mc_operands_table_flag) {
res = read_operands_table(macro_context,
macro_header,
macro_data,
section_base,
section_size,
&optablesize,
error);
if (res != DW_DLV_OK) {
dwarf_dealloc_macro_context(macro_context);
return res;
}
}
macro_data += optablesize;
macro_context->mc_macro_ops = macro_data;
macro_context->mc_macro_header_length =macro_data - macro_header;
build_ops_array = FALSE;
res = _dwarf_get_macro_ops_count_internal(macro_context,
build_ops_array,
error);
if (res != DW_DLV_OK) {
dwarf_dealloc_macro_context(macro_context);
return res;
}
build_ops_array = TRUE;
res = _dwarf_get_macro_ops_count_internal(macro_context,
build_ops_array,
error);
if (res != DW_DLV_OK) {
dwarf_dealloc_macro_context(macro_context);
return res;
}
*macro_ops_count_out = macro_context->mc_macro_ops_count;
*macro_ops_data_length = macro_context->mc_ops_data_length;
*version_out = version;
*macro_context_out = macro_context;
return DW_DLV_OK;
}
int dwarf_macro_context_head(Dwarf_Macro_Context head,
Dwarf_Half * version,
Dwarf_Unsigned * mac_offset,
Dwarf_Unsigned * mac_len,
Dwarf_Unsigned * mac_header_len,
unsigned * flags,
Dwarf_Bool * has_line_offset,
Dwarf_Unsigned * line_offset,
Dwarf_Bool * has_offset_size_64,
Dwarf_Bool * has_operands_table,
Dwarf_Half * opcode_count,
Dwarf_Error *error)
{
if (!head || head->mc_sentinel != 0xada) {
Dwarf_Debug dbg = 0;
if(head) {
dbg = head->mc_dbg;
}
_dwarf_error(dbg, error,DW_DLE_BAD_MACRO_HEADER_POINTER);
return DW_DLV_ERROR;
}
*version = head->mc_version_number;
*mac_offset = head->mc_section_offset;
*mac_len = head->mc_total_length;
*mac_header_len = head->mc_macro_header_length;
*flags = head->mc_flags;
*line_offset = head->mc_debug_line_offset;
*has_line_offset = head->mc_debug_line_offset_flag;
*has_offset_size_64 = head->mc_offset_size_flag;
*has_operands_table = head->mc_operands_table_flag;
*opcode_count = head->mc_opcode_count;
return DW_DLV_OK;
}
int dwarf_macro_operands_table(Dwarf_Macro_Context head,
Dwarf_Half index, /* 0 to opcode_count -1 */
Dwarf_Half *opcode_number,
Dwarf_Half *operand_count,
const Dwarf_Small **operand_array,
Dwarf_Error *error)
{
struct Dwarf_Macro_Forms_s * ops = 0;
Dwarf_Debug dbg = 0;
if (!head || head->mc_sentinel != 0xada) {
if(head) {
dbg = head->mc_dbg;
}
_dwarf_error(dbg, error,DW_DLE_BAD_MACRO_HEADER_POINTER);
return DW_DLV_ERROR;
}
dbg = head->mc_dbg;
if (index >= head->mc_opcode_count) {
_dwarf_error(dbg, error, DW_DLE_BAD_MACRO_INDEX);
return DW_DLV_ERROR;
}
ops = head->mc_opcode_forms + index;
*opcode_number = ops->mf_code;
*operand_count = ops->mf_formcount;
*operand_array = ops->mf_formbytes;
return DW_DLV_OK;
}
/* The base interface to the .debug_macro section data
for a specific CU.
The version number passed back by *version_out
may be 4 (a gnu extension of DWARF) or 5. */
int
dwarf_get_macro_context(Dwarf_Die cu_die,
Dwarf_Unsigned * version_out,
Dwarf_Macro_Context * macro_context,
Dwarf_Unsigned * macro_unit_offset_out,
Dwarf_Unsigned * macro_ops_count_out,
Dwarf_Unsigned * macro_ops_data_length,
Dwarf_Error * error)
{
int res = 0;
Dwarf_Bool offset_specified = FALSE;
Dwarf_Unsigned offset = 0;
res = _dwarf_internal_macro_context(cu_die,
offset_specified,
offset,
version_out,
macro_context,
macro_unit_offset_out,
macro_ops_count_out,
macro_ops_data_length,
error);
return res;
}
/* Like dwarf_get_macro_context but
here we use a specfied offset instead of
the offset in the cu_die. */
int
dwarf_get_macro_context_by_offset(Dwarf_Die cu_die,
Dwarf_Unsigned offset,
Dwarf_Unsigned * version_out,
Dwarf_Macro_Context * macro_context,
Dwarf_Unsigned * macro_ops_count_out,
Dwarf_Unsigned * macro_ops_data_length,
Dwarf_Error * error)
{
int res = 0;
Dwarf_Bool offset_specified = TRUE;
Dwarf_Unsigned macro_unit_offset_out = 0;
res = _dwarf_internal_macro_context(cu_die,
offset_specified,
offset,
version_out,
macro_context,
¯o_unit_offset_out,
macro_ops_count_out,
macro_ops_data_length,
error);
return res;
}
int dwarf_get_macro_section_name(Dwarf_Debug dbg,
const char **sec_name_out,
UNUSEDARG Dwarf_Error *error)
{
struct Dwarf_Section_s *sec = 0;
sec = &dbg->de_debug_macro;
if (sec->dss_size == 0) {
/* We don't have such a section at all. */
return DW_DLV_NO_ENTRY;
}
*sec_name_out = sec->dss_name;
return DW_DLV_OK;
}
void
dwarf_dealloc_macro_context(Dwarf_Macro_Context mc)
{
Dwarf_Debug dbg = 0;
if (!mc) {
return;
}
dbg = mc->mc_dbg;
/* See _dwarf_macro_destructor() here */
dwarf_dealloc(dbg,mc,DW_DLA_MACRO_CONTEXT);
}
int
_dwarf_macro_constructor(Dwarf_Debug dbg, void *m)
{
/* Nothing to do, the space is zeroed out */
Dwarf_Macro_Context mc= (Dwarf_Macro_Context)m;
/* Arbitrary sentinel. For debugging. */
mc->mc_sentinel = 0xada;
mc->mc_dbg = dbg;
return DW_DLV_OK;
}
/* Here we free various fields of Dwarf_Macro_Context.
The fields do not get dealloc'd.
If we had a separate destructor for hand-calling
(meaning when an error is detected during creation
of a Dwarf_Macro_Context)
and one for calling by dwarf_dealloc() then
we could have the hand-calling dwarf_dealloc the fields
and the one called on the dealloc of a Dwarf_Macro_Context
could leave the _dwarf_get_alloc() fields for for
normal dwarf_finish() cleanup.
But for now we share this destructor for both purposes
so no fields are _dwarf_get_alloc() and all are free-d
here..
*/
void
_dwarf_macro_destructor(void *m)
{
Dwarf_Macro_Context mc= (Dwarf_Macro_Context)m;
dealloc_macro_srcfiles(mc->mc_srcfiles, mc->mc_srcfiles_count);
mc->mc_srcfiles = 0;
mc->mc_srcfiles_count = 0;
free((void *)mc->mc_file_path);
mc->mc_file_path = 0;
free(mc->mc_ops);
mc->mc_ops = 0;
free(mc->mc_opcode_forms);
mc->mc_opcode_forms = 0;
memset(mc,0,sizeof(*mc));
/* Just a recognizable sentinel. For debugging. No real meaning. */
mc->mc_sentinel = 0xdeadbeef;
}
|