-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreflect.c
2368 lines (2126 loc) · 98.8 KB
/
reflect.c
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
/*
* j2 - A simple concatenative programming language that can understand
* the structure of and dynamically link with compatible C binaries.
*
* Copyright (C) 2011 Jason Nyberg <[email protected]>
* Copyright (C) 2018 Jason Nyberg <[email protected]> (dual-licensed)
* (C) Copyright 2019 Hewlett Packard Enterprise Development LP.
*
* This file is part of j2.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either
*
* * the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version
*
* or
*
* * the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version
*
* or both in parallel, as here.
*
* j2 is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received copies of the GNU General Public License and
* the GNU Lesser General Public License along with this program. If
* not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE // strndupa, stpcpy
#define __USE_GNU // strndupa, stpcpy
#include <sys/types.h> /* For open() */
#include <sys/stat.h> /* For open() */
#include <fcntl.h> /* For open() */
#include <unistd.h> /* For close() */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libdwarf/dwarf.h>
#include <libdwarf/libdwarf.h>
#include <dlfcn.h> // dlopen/dlsym/dlclose
#include <arpa/inet.h>
#include <ffi.h>
#include "util.h"
#include "listree.h"
#include "reflect.h"
#include "vm.h"
#include "extensions.h"
static int cif_curate_module(LTV *module, int bootstrap);
LTV *cif_module = NULL; // initialized/populated during bootstrap
void cif_init(int bootstrap) {
if (!cif_module) {
Dl_info dl_info;
dladdr((void *)cif_init, &dl_info);
fprintf(stderr, CODE_RED "reflection module path is: %s" CODE_RESET "\n", dl_info.dli_fname);
cif_module = LTV_init(NEW(LTV), (char *)dl_info.dli_fname, strlen(dl_info.dli_fname), LT_DUP | LT_RO);
cif_preview_module(cif_module);
print_ltv(stderr, CODE_RED, cif_module, CODE_RESET "\n", 0);
cif_curate_module(cif_module, bootstrap);
}
}
char *Type_pushUVAL(TYPE_UVALUE *uval,char *buf);
TYPE_UVALUE *Type_pullUVAL(TYPE_UVALUE *uval,char *buf);
TYPE_UTYPE Type_getUVAL(LTV *cvar,TYPE_UVALUE *uval);
int Type_putUVAL(LTV *cvar,TYPE_UVALUE *uval);
/////////////////////////////////////////////////////////////
LTV *attr_set(LTV *ltv,char *attr,char *val) { return LT_put(ltv,attr,TAIL,LTV_init(NEW(LTV),val,-1,LT_DUP)); }
LTV *attr_own(LTV *ltv,char *attr,char *val) { return LT_put(ltv,attr,TAIL,LTV_init(NEW(LTV),val,-1,LT_OWN)); }
LTV *attr_imm(LTV *ltv,char *attr,long long imm) { return LT_put(ltv,attr,TAIL,LTV_init(NEW(LTV),(void *) imm,0,LT_IMM)); }
//extern char *attr_get(LTV *ltv,char *attr);
//extern void attr_del(LTV *ltv,char *attr);
extern char *attr_get(LTV *ltv,char *attr)
{
LTV *attr_ltv=LT_get(ltv,attr,TAIL,KEEP);
return attr_ltv?attr_ltv->data:NULL;
}
extern void attr_del(LTV *ltv,char *attr) { LTV_erase(ltv,LTI_resolve(ltv,attr,false)); }
// look up an attribute in one LTV, and look up it's value within another LTV.
extern char *attr_deref(LTV *ltv,char *attr,LTV *index)
{
char *attr_val=attr_get(ltv,attr);
return attr_val?attr_get(index,attr_val):NULL;
}
/////////////////////////////////////////////////////////////
//
// Dwarf Traversal
//
/////////////////////////////////////////////////////////////
typedef enum {
RDW_none =0x0,
RDW_is_info =0x1,
RDW_traverse_sibs =0x2,
} DIEWALK_FLAGS;
typedef int (*DIE_OP)(Dwarf_Debug dbg,Dwarf_Die die,DIEWALK_FLAGS flags);
int traverse_siblings(Dwarf_Debug dbg,Dwarf_Die die,DIE_OP op,DIEWALK_FLAGS flags)
{
int status=0;
Dwarf_Error error=0;
Dwarf_Die sibling=0;
iterate:
if (die)
STRY(op(dbg,die,flags),"operate on die");
// else get first die
TRY(dwarf_siblingof_b(dbg,die,(flags&RDW_is_info)!=0,&sibling,&error),"retrieve dwarf_sibling");
CATCH(status==DW_DLV_NO_ENTRY,0,goto done,"check for DW_DLV_NO_ENTRY"); /* Done at this level. */
CATCH(dwarf_errno(error)==DW_DLE_DBG_NO_CU_CONTEXT,1,goto done,"check for DW_DLE_DBG_NO_CU_CONTEX %s",dwarf_errmsg(error));
SCATCH("check dwarf_siblingof");
if (die)
dwarf_dealloc(dbg,die,DW_DLA_DIE);
if ((!die || (flags&RDW_traverse_sibs)) && (die=sibling))
goto iterate;
done:
return status;
}
int traverse_child(Dwarf_Debug dbg,Dwarf_Die die,DIE_OP op,DIEWALK_FLAGS flags)
{
int status=0;
Dwarf_Error error=0;
Dwarf_Die child=0;
TRY(dwarf_child(die,&child,&error),"retrieve dwarf_child");
CATCH(status==DW_DLV_NO_ENTRY,0,goto done,"check dwarf child's existence");
SCATCH("check dwarf_child for error");
STRY(traverse_siblings(dbg,child,op,flags),"process %s",flags&RDW_traverse_sibs?"children":"child");
done:
return status;
}
static char *DW_IDX_STRING[] = {"header_cu_type==0 (invalid)",
"DW_UT_compile",
"DW_UT_type",
"DW_UT_partial",
"DW_UT_skeleton",
"DW_UT_split_compile",
"DW_UT_split_type",
"DW_UT_lo_user",
"DW_UT_hi_user" };
int traverse_cus(char *filename,DIE_OP op,CU_DATA *cu_data,DIEWALK_FLAGS flags)
{
int status=0;
Dwarf_Debug dbg;
Dwarf_Error error=0;
int read_cu_list() {
CU_DATA cu_data_local;
if (!cu_data) cu_data=&cu_data_local; // allow caller to not care
Dwarf_Die die;
while (1) {
TRY(dwarf_next_cu_header_d(dbg,
(flags&RDW_is_info)!=0,
&cu_data->header_length,
&cu_data->version_stamp,
&cu_data->abbrev_offset,
&cu_data->address_size,
&cu_data->length_size,
&cu_data->extension_size,
&cu_data->sig8,
&cu_data->offset,
&cu_data->next_cu_header_offset,
&cu_data->header_cu_type,
&error),
"read next cu header");
DWARF_ID(cu_data->next_cu_header_offset_str,cu_data->next_cu_header_offset);
CATCH(status==DW_DLV_NO_ENTRY,0,goto done,"check for no next cu header");
CATCH(status!=DW_DLV_OK,status,goto done,"check error dwarf_next_cu_header");
static char alias[32];
DWARF_ALIAS(alias,cu_data->sig8);
DEBUG(fprintf(stdout,CODE_BLUE "Read a CU header, offset 0x%x sig8 %s cu_type %s" CODE_RESET "\n",
cu_data->offset,alias,DW_IDX_STRING[cu_data->header_cu_type]));
STRY(traverse_siblings(dbg,NULL,op,flags),"process cu die and sibs");
}
done:
return status;
}
LTV *debug_link_filename=get_separated_debug_filename(filename);
if (debug_link_filename) {
fprintf(stderr,"Using alt debug filename %s for %s\n",(char *) debug_link_filename->data,filename);
filename=(char *) debug_link_filename->data;
}
int filedesc = -1;
STRY((filedesc=open(filename,O_RDONLY))<0,"open dwarf2edict input file %s",filename);
TRYCATCH(dwarf_init(filedesc,DW_DLC_READ,NULL,NULL,&dbg,&error),status,close_file,"initialize dwarf reader");
TRYCATCH(read_cu_list(),status,close_dwarf,"read cu list");
close_dwarf:
STRY(dwarf_finish(dbg,&error),"finalize dwarf reader");
close_file:
close(filedesc);
done:
LTV_release(debug_link_filename);
return status;
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
int print_cu_data(FILE *ofile,CU_DATA *cu_data)
{
int status=0;
STRY(!cu_data,"validate cu data");
fprintf(ofile,"CU DATA");
#define output_cu_field(fmt,field) fprintf(ofile,"|" #field fmt,cu_data->field)
output_cu_field("%s",offset_str);
output_cu_field("%s",next_cu_header_offset_str);
output_cu_field("%x",header_length);
output_cu_field("%x",version_stamp);
output_cu_field("%x",abbrev_offset);
output_cu_field("%x",address_size);
output_cu_field("%x",length_size);
output_cu_field("%x",extension_size);
output_cu_field("%s",dwo_name);
output_cu_field("%s",linkage_name);
done:
return status;
}
int dot_cu_data(FILE *ofile,CU_DATA *cu_data)
{
int status=0;
STRY(!cu_data,"validate cu data");
fprintf(ofile,CVAR_FORMAT " [shape=record label=\"{");
print_cu_data(ofile,cu_data);
fprintf(ofile,"}\"");
fprintf(ofile,"]\n");
done:
return status;
}
int print_type_info(FILE *ofile,LTV *ltv)
{
int status=0;
if (!(ltv->flags & LT_TYPE))
goto done;
TYPE_INFO_LTV *type_info = (TYPE_INFO_LTV *) ltv;
fprintf(ofile, "|TYPE_INFO %s", type_info->id_str);
const char *str=NULL;
static char alias[32];
DWARF_ALIAS(alias,type_info->sig8);
dwarf_get_TAG_name(type_info->tag,&str);
fprintf(ofile,"|%s",str+7);
if (type_info->flags&TYPEF_BASE) fprintf(ofile,"|base %s", type_info->base_str);
if (type_info->flags&TYPEF_CONSTVAL) fprintf(ofile,"|constval 0x%x", type_info->const_value);
if (type_info->flags&TYPEF_BYTESIZE) fprintf(ofile,"|bytesize %u", type_info->bytesize);
if (type_info->flags&TYPEF_BITSIZE) fprintf(ofile,"|bitsize %u", type_info->bitsize);
if (type_info->flags&TYPEF_BITOFFSET) fprintf(ofile,"|bitoffset %u", type_info->bitoffset);
if (type_info->flags&TYPEF_ENCODING) fprintf(ofile,"|encoding %u", type_info->encoding);
if (type_info->flags&TYPEF_UPPERBOUND) fprintf(ofile,"|upperbound 0x%x",type_info->upper_bound);
if (type_info->flags&TYPEF_LOWPC) fprintf(ofile,"|lowpc 0x%x", type_info->low_pc);
if (type_info->flags&TYPEF_MEMBERLOC) fprintf(ofile,"|member loc 0x%x",type_info->data_member_location);
if (type_info->flags&TYPEF_LOCATION) fprintf(ofile,"|location 0x%x", type_info->location);
if (type_info->flags&TYPEF_ADDR) fprintf(ofile,"|addr 0x%x", type_info->addr);
if (type_info->flags&TYPEF_EXTERNAL) fprintf(ofile,"|external %u", type_info->external);
if (type_info->flags&TYPEF_SYMBOLIC) fprintf(ofile,"|symbolic");
if (type_info->flags&TYPEF_OFFSET) fprintf(ofile,"|offset %x", type_info->offset);
if (type_info->flags&TYPEF_IS_INFO) fprintf(ofile,"|info");
if (type_info->flags&TYPEF_IS_DECL) fprintf(ofile,"|decl");
if (type_info->flags&TYPEF_SIGNATURE) fprintf(ofile,"|sig %s", alias);
done:
return status;
}
int dot_type_info(FILE *ofile, LTV *ltv) {
int status = 0;
fprintf(ofile, "shape=record color=%s style=filled label=\"{{CVAR(%x)", ltv->flags & LT_RO ? "red" : "black", ltv->data);
print_type_info(ofile, ltv);
fprintf(ofile, "}}\"");
if (!(ltv->flags & LT_TYPE)) {
fprintf(ofile, " fillcolor=beige");
goto done;
}
TYPE_INFO_LTV *type_info = (TYPE_INFO_LTV *) ltv;
switch (type_info->tag) {
case DW_TAG_compile_unit: fprintf(ofile, " fillcolor=red rank=max"); break;
case DW_TAG_subprogram: fprintf(ofile, " fillcolor=orange"); break;
case DW_TAG_subroutine_type: fprintf(ofile, " fillcolor=darkorange"); break;
case DW_TAG_formal_parameter: fprintf(ofile, " fillcolor=gold"); break;
case DW_TAG_variable: fprintf(ofile, " fillcolor=cyan"); break;
case DW_TAG_typedef: fprintf(ofile, " fillcolor=green"); break;
case DW_TAG_structure_type: fprintf(ofile, " fillcolor=blue"); break;
case DW_TAG_class_type: fprintf(ofile, " fillcolor=blue"); break;
case DW_TAG_union_type: fprintf(ofile, " fillcolor=violet"); break;
case DW_TAG_member: fprintf(ofile, " fillcolor=lightblue"); break;
case DW_TAG_enumeration_type: fprintf(ofile, " fillcolor=magenta"); break;
case DW_TAG_array_type: fprintf(ofile, " fillcolor=gray80"); break;
case DW_TAG_pointer_type: fprintf(ofile, " fillcolor=gray90"); break;
case DW_TAG_base_type: fprintf(ofile, " fillcolor=yellow rank=min"); break;
case DW_TAG_enumerator: fprintf(ofile, " fillcolor=pink rank=min"); break;
case DW_TAG_subrange_type: fprintf(ofile, " fillcolor=gray80 rank=min"); break;
case DW_TAG_type_unit: fprintf(ofile, " fillcolor=darkred rank=min"); break;
}
done:
return status;
}
LTV *cif_find_base(LTV *type,int tag)
{
TYPE_INFO_LTV *type_info=NULL;
while (type && (type->flags<_TYPE)) {
type_info=(TYPE_INFO_LTV *) type;
if (type_info->tag==tag)
return type;
type=LT_get(type,TYPE_BASE,HEAD,KEEP);
}
return NULL;
}
LTV *cif_find_symbolic(LTV *type)
{
TYPE_INFO_LTV *type_info=NULL;
while (type && (type->flags<_TYPE)) {
type_info=(TYPE_INFO_LTV *) type;
if (type_info->flags&TYPEF_SYMBOLIC)
return type;
type=LT_get(type,TYPE_BASE,HEAD,KEEP);
}
return NULL;
}
LTV *cif_find_concrete(LTV *type)
{
TYPE_INFO_LTV *type_info=NULL;
while (type && (type->flags<_TYPE)) {
type_info=(TYPE_INFO_LTV *) type;
if (!(type_info->flags&TYPEF_SIGNATURE) || (type_info->flags&TYPEF_IS_DECL))
switch (type_info->tag) {
case DW_TAG_structure_type:
case DW_TAG_class_type:
case DW_TAG_union_type:
case DW_TAG_enumeration_type:
case DW_TAG_array_type:
case DW_TAG_pointer_type:
case DW_TAG_base_type:
return type;
default:
break;
}
type=LT_get(type,TYPE_BASE,HEAD,KEEP);
}
return NULL;
}
LTV *cif_find_indexable(LTV *type)
{
TYPE_INFO_LTV *type_info=NULL;
while (type && (type->flags<_TYPE)) {
type_info=(TYPE_INFO_LTV *) type;
if (!(type_info->flags&TYPEF_SIGNATURE) || (type_info->flags&TYPEF_IS_DECL))
switch (type_info->tag) {
case DW_TAG_structure_type:
case DW_TAG_class_type:
case DW_TAG_union_type:
case DW_TAG_array_type:
case DW_TAG_pointer_type:
return type;
default:
break;
}
type=LT_get(type,TYPE_BASE,HEAD,KEEP);
}
return NULL;
}
LTV *cif_find_function(LTV *type)
{
TYPE_INFO_LTV *type_info=NULL;
while (type && (type->flags<_TYPE)) {
type_info=(TYPE_INFO_LTV *) type;
if (type_info->tag==DW_TAG_subprogram || type_info->tag==DW_TAG_subroutine_type)
return type;
type=LT_get(type,TYPE_BASE,HEAD,KEEP);
}
return NULL;
}
LTV *cif_get_child(LTV *type,char *childname)
{
if (type->flags<_TYPE) {
LTV *child=LT_get(type,childname,HEAD,KEEP);
if (child && child->flags<_TYPE)
return child;
}
return NULL;
}
LTV *cif_get_element(LTV *type,int index)
{
// see Type_findMemberByIndex
}
LTV *cif_create_cvar(LTV *type,void *data,char *member)
{
int status=0;
LTV *basic_type=NULL,*member_type=NULL,*cvar=NULL;
TYPE_INFO_LTV *type_info=(TYPE_INFO_LTV *) type->data;
basic_type=member?cif_find_indexable(type):cif_find_concrete(type);
int size=0;
int is_array=0;
if (!basic_type) {
switch(type_info->tag) {
case DW_TAG_subprogram:
case DW_TAG_subroutine_type:
size=sizeof(void *);
break;
default:
goto done;
}
} else {
TYPE_INFO_LTV *basic_type_info=(TYPE_INFO_LTV *) basic_type->data;
switch(basic_type_info->tag) {
case DW_TAG_structure_type:
case DW_TAG_class_type:
case DW_TAG_union_type:
if (member) {
if ((status=!(member_type=cif_get_child(basic_type,member))))
goto done; // fail w/o message
TYPE_INFO_LTV *member_type_info=(TYPE_INFO_LTV *) member_type->data;
cvar=cif_create_cvar(member_type,data+member_type_info->data_member_location,NULL);
goto done;
}
break;
case DW_TAG_array_type:
is_array=LT_ARR; // when arranging ffi args, use address of data (i.e. pointer to array location)
// fall thru
case DW_TAG_pointer_type:
if (member) {
errno=0;
char *end;
int index=strtol(member,&end,0);
if (errno || (end==member))
return NULL; // failed to find member, but we don't need to STRY/report it so just return right here
STRY(!(basic_type=cif_find_concrete(LT_get(basic_type,TYPE_BASE,HEAD,KEEP))),"dereference pointer type");
basic_type_info=(TYPE_INFO_LTV *) basic_type->data;
int offset=index*basic_type_info->bytesize;
if (!is_array) // data points to location of a pointer
data=*(void **) data;
// else data points to location of first element of array
cvar=cif_create_cvar(basic_type,data+offset,NULL);
goto done;
}
break;
default:
if ((status=member!=NULL))
goto done; // fail w/o message
break;
}
size=basic_type_info->bytesize;
}
if (data)
STRY(!(cvar=LTV_init(NEW(LTV),data,size,LT_BIN|LT_CVAR|is_array)),"create reference cvar");
else
STRY(!(cvar=LTV_init(NEW(LTV),(void *) mymalloc(size),size,LT_OWN|LT_BIN|LT_CVAR|is_array)),"create allocated cvar");
LT_put(cvar,TYPE_BASE,HEAD,type);
done:
return status?NULL:cvar;
}
LTV *cif_assign_cvar(LTV *dst,LTV *src)
{
int status=0;
LTV *type=NULL;
TYPE_UVALUE dst_uval={},src_uval={};
STRY(!Type_getUVAL(dst,&dst_uval),"test cvar dest compatibility");
if (Type_getUVAL(src,&src_uval))
Type_putUVAL(dst,&src_uval);
else
Type_putUVAL(dst,Type_pullUVAL(&dst_uval,src->data));
done:
return status?NULL:dst;
}
int cif_dump_cvar(FILE *ofile,LTV *cvar,int depth)
{
int status=0;
LTV *type;
TRY(!(type=LT_get(cvar,TYPE_BASE,HEAD,KEEP)),"validate cvar via type");
CATCH(status, 0, goto done, "not descendable");
CLL queue;
CLL_init(&queue);
LTV_enq(&queue,cif_create_cvar(type,cvar->data,NULL),TAIL); // copy cvar so we don't mess with it
int traverse_array(LTV *cvar,int count)
{
int status=0;
STRY(!(type=LT_get(cvar,TYPE_BASE,HEAD,KEEP)),"look up cvar type");
TYPE_INFO_LTV *type_info=(TYPE_INFO_LTV *) type->data;
for (int i=0;i<count;i++) {
cif_dump_cvar(ofile,cvar,depth+4);
cvar->data+=type_info->bytesize;
}
done:
return status;
}
int process_type_info(LTV *cvar) {
int status=0;
LTV *type=NULL;
void *type_info_op(CLL *lnk) {
LTV *ltv=((LTVR *) lnk)->ltv;
TYPE_INFO_LTV *type_info=(TYPE_INFO_LTV *) ltv;
switch (type_info->tag) {
case DW_TAG_member:
LTV_enq(&queue,cif_create_cvar(ltv,cvar->data+type_info->data_member_location,NULL),TAIL);
break;
default:
fprintf(ofile,CODE_RED "child tag %d unimplemented" CODE_RESET "\n",type_info->tag);
break;
}
done:
return status?(void *) NON_NULL:(void *) NULL;
};
STRY(!(type=LT_get(cvar,TYPE_BASE,HEAD,KEEP)),"look up cvar type");
TYPE_INFO_LTV *type_info=(TYPE_INFO_LTV *) type->data;
char *name=attr_get(type,TYPE_SYMB);
{
const char *str=NULL;
dwarf_get_TAG_name(type_info->tag,&str);
fprintf(ofile,"%*c%s \"%s\" ",depth*4,' ',str+7,name);
switch(type_info->tag) {
case DW_TAG_union_type:
case DW_TAG_structure_type:
case DW_TAG_class_type: {
LTI *children=NULL;
if ((children=LTI_resolve(type,TYPE_LIST,false)))
CLL_map(&children->ltvs,FWD,type_info_op);
break;
}
case DW_TAG_subprogram:
case DW_TAG_subroutine_type:
fprintf(ofile,"0x%x",cvar->data);
break;
case DW_TAG_pointer_type:
fprintf(ofile,"0x%x",*((void **) cvar->data));
break;
case DW_TAG_array_type: {
TYPE_INFO_LTV *base_info=NULL;
if (type_info->flags&TYPEF_BASE) // link to base type
STRY(!(base_info=(TYPE_INFO_LTV *) LT_get(&type_info->ltv,TYPE_BASE,HEAD,KEEP)),"look up base die for %s",type_info->id_str);
char *base_symb=base_info && (base_info->flags&TYPEF_SYMBOLIC)? attr_get(&base_info->ltv,TYPE_SYMB):NULL;
if (base_symb) {
LTV *subrange_ltv=LT_get(&type_info->ltv,"subrange type",HEAD,KEEP);
TYPE_INFO_LTV *subrange=subrange_ltv?(TYPE_INFO_LTV *) subrange_ltv->data:NULL;
if (subrange && (subrange->flags&TYPEF_UPPERBOUND)) {
fprintf(ofile,"\n");
LTV *tcvar=cif_create_cvar(&base_info->ltv,cvar->data,NULL);
traverse_array(tcvar,subrange->upper_bound+1);
LTV_release(tcvar);
}
else
fprintf(ofile,"(unbounded array of %s)",base_symb);
}
break;
}
case DW_TAG_enumeration_type:
fprintf(ofile,"(enum display unimplemented)");
break;
case DW_TAG_enumerator:
fprintf(ofile,"0x%x",type_info->const_value);
break;
case DW_TAG_member:
if (type_info->flags&TYPEF_BYTESIZE)
; /* fall thru! */
else {
LTV_enq(&queue,cif_create_cvar(cif_find_concrete(type),cvar->data,NULL),HEAD);
break;
}
case DW_TAG_base_type: {
TYPE_UVALUE uval={};
char buf[64];
if (Type_getUVAL(cvar,&uval))
fprintf(ofile,"%s (%p)",Type_pushUVAL(&uval,buf),cvar->data);
break;
}
default:
LTV_enq(&queue,cif_create_cvar(cif_find_concrete(type),cvar->data,NULL),HEAD);
break;
}
}
done:
fprintf(ofile,"\n");
return status;
};
while ((cvar=LTV_deq(&queue,HEAD))) {
process_type_info(cvar);
LTV_release(cvar);
}
done:
return status;
}
int cif_print_cvar(FILE *ofile,LTV *ltv,int depth) {
int status=0;
if (ltv->flags<_FFI)
fprintf(ofile,"FFI ");
if (ltv->flags<_CIF)
fprintf(ofile,"CIF ");
if (ltv->flags<_TYPE) // special case
print_type_info(ofile, ltv);
else
cif_dump_cvar(ofile,ltv,depth); // use reflection!!!!!
done:
fprintf(ofile,"\n");
return status;
}
int cif_dot_cvar(FILE *ofile,LTV *ltv) {
int status=0;
fprintf(ofile, "\"LTV%x\" [", ltv);
dot_type_info(ofile, ltv);
fprintf(ofile, "]\n");
done:
return status;
}
extern void graph_types_to_file(char *filename,LTV *ltv) {
FILE *ofile=fopen(filename,"w");
CLL ltvs;
CLL_init(<vs);
LTV_enq(<vs,ltv,HEAD);
fprintf(ofile,"digraph iftree\n{\ngraph [rankdir=LR, ratio=compress, concentrate=true] node [shape=record] edge []\n");
ltvs2dot_simple(ofile,<vs,0,filename);
//ltvs2dot(ofile,<vs,0,filename);
fprintf(ofile,"}\n");
LTV_deq(<vs,HEAD);
fclose(ofile);
}
int cif_die_offset(Dwarf_Die die,Dwarf_Off *offset,Dwarf_Error *error) {
int status=0;
Dwarf_Off cu_offset=0,die_offset=0;
STRY(dwarf_CU_dieoffset_given_die(die,&cu_offset,error),"get cu offset");
STRY(dwarf_die_CU_offset(die,&cu_offset,error),"get cu offset");
*offset=cu_offset+die_offset;
done:
return status;
}
#define IF_OK(cond,followup) if (cond==DW_DLV_OK) followup
int populate_type_info(Dwarf_Debug dbg,Dwarf_Die die,TYPE_INFO_LTV *type_info,CU_DATA *cu_data)
{
int status=0;
Dwarf_Error error=0;
char *diename = NULL;
STRY(!type_info || !cu_data,"validat params");
STRY(die==NULL,"test for null die");
STRY(dwarf_die_CU_offset(die,&type_info->offset,&error),"get CU-relative die offset");
Dwarf_Off goff;
STRY(dwarf_dieoffset(die,&goff,&error),"get global die offset");
DWARF_ID(type_info->id_str,goff);
STRY(dwarf_tag(die,&type_info->tag,&error),"get die tag");
if (dwarf_get_die_infotypes_flag(die))
type_info->flags|=TYPEF_IS_INFO;
switch (type_info->tag) {
case DW_TAG_type_unit:
case DW_TAG_compile_unit:
DWARF_ID(cu_data->offset_str,goff);
// FALL THRU!
case DW_TAG_pointer_type:
case DW_TAG_array_type:
case DW_TAG_volatile_type:
case DW_TAG_const_type:
case DW_TAG_restrict_type:
case DW_TAG_structure_type:
case DW_TAG_class_type:
case DW_TAG_union_type:
case DW_TAG_enumeration_type:
case DW_TAG_base_type:
case DW_TAG_typedef:
case DW_TAG_subrange_type:
case DW_TAG_member:
case DW_TAG_enumerator:
case DW_TAG_formal_parameter:
case DW_TAG_unspecified_parameters: // varargs
case DW_TAG_rvalue_reference_type:
case DW_TAG_reference_type:
case DW_TAG_template_type_parameter:
case DW_TAG_template_value_parameter:
break;
case DW_TAG_variable:
case DW_TAG_subprogram:
case DW_TAG_subroutine_type:
if (0 && type_info->depth>1) {
type_info->tag=0;
goto done;
}
break;
default:
fprintf(stderr,CODE_RED "Unrecognized tag 0x%x\n" CODE_RESET,type_info->tag);
case DW_TAG_lexical_block:
case DW_AT_GNU_all_tail_call_sites:
case DW_TAG_label:
case DW_TAG_inlined_subroutine:
case DW_TAG_GNU_call_site:
case DW_TAG_GNU_call_site_parameter:
case DW_TAG_dwarf_procedure:
case DW_TAG_namespace:
case DW_TAG_inheritance:
case DW_TAG_imported_declaration:
case DW_TAG_imported_module:
case DW_TAG_ptr_to_member_type:
case DW_TAG_GNU_template_parameter_pack:
case DW_TAG_unspecified_type:
type_info->tag=0; // reject
goto done;
}
Dwarf_Signed atcnt=0;
Dwarf_Attribute *atlist=NULL;
TRY(dwarf_attrlist(die,&atlist,&atcnt,&error),"get die attrlist");
CATCH(status==DW_DLV_NO_ENTRY,0,goto done,"check for DW_DLV_NO_ENTRY in dwarf_attrlist");
SCATCH("get die attrlist");
Dwarf_Attribute *attr=NULL;
while (atcnt--) {
char *prefix;
attr=&atlist[atcnt];
Dwarf_Half vshort;
STRY(dwarf_whatattr(*attr,&vshort,&error),"get attr type");
Dwarf_Off voffset;
Dwarf_Sig8 vsig8;
char *vstr;
int dump_attr(FILE *ofile) {
int status=0;
fprintf(ofile,CODE_RED "dump attr 0x%x\n",vshort);
Dwarf_Signed vint;
Dwarf_Unsigned vuint;
Dwarf_Addr vaddr;
Dwarf_Off voffset;
Dwarf_Half vshort;
Dwarf_Bool vbool;
Dwarf_Ptr vptr;
Dwarf_Block *vblock;
Dwarf_Sig8 vsig8;
char *vstr;
const char *vcstr;
STRY(dwarf_whatform(*attr,&vshort,&error),"get attr form");
STRY(dwarf_get_FORM_name(vshort,&vcstr),"get attr formname");
fprintf(ofile,"form %d (%s) ",vshort,vcstr);
STRY(dwarf_whatform_direct(*attr,&vshort,&error),"get attr form_direct");
STRY(dwarf_get_FORM_name(vshort,&vcstr),"get attr form_direct name");
fprintf(ofile,"form_direct %d (%s) ",vshort,vcstr);
IF_OK(dwarf_formref(*attr,&voffset,&error), fprintf(ofile,"formref 0x%" DW_PR_DSx " ",voffset));
IF_OK(dwarf_global_formref(*attr,&voffset,&error),fprintf(ofile,"global_formref 0x%" DW_PR_DSx " ",voffset));
IF_OK(dwarf_formaddr(*attr,&vaddr,&error), fprintf(ofile,"addr 0x%" DW_PR_DUx " ",vaddr));
IF_OK(dwarf_formflag(*attr,&vbool,&error), fprintf(ofile,"flag %" DW_PR_DSd " ",vbool));
IF_OK(dwarf_formudata(*attr,&vuint,&error), fprintf(ofile,"udata %" DW_PR_DUu " ",vuint));
IF_OK(dwarf_formsdata(*attr,&vint,&error), fprintf(ofile,"sdata %" DW_PR_DSd " ",vint));
IF_OK(dwarf_formblock(*attr,&vblock,&error), fprintf(ofile,"block 0x%" DW_PR_DUx " ",vblock->bl_len));
IF_OK(dwarf_formstring(*attr,&vstr,&error), fprintf(ofile,"string %s ", vstr));
IF_OK(dwarf_formsig8(*attr,&vsig8,&error), fprintf(ofile,"addr %02d:%02d:%02d:%02d:%02d:%02d:%02d:%02d ",
vsig8.signature[0],vsig8.signature[1],vsig8.signature[2],vsig8.signature[3],
vsig8.signature[4],vsig8.signature[5],vsig8.signature[6],vsig8.signature[7]));
fprintf(ofile,"\n");
done:
return status;
};
switch (vshort) {
case DW_AT_name: // string
type_info->flags|=TYPEF_HAS_NAME;
break;
case DW_AT_type: // global_formref
//STRY(dwarf_whatform(*attr,&vshort,&error),"get attr form");
IF_OK(dwarf_global_formref(*attr,&type_info->base,&error),type_info->flags|=TYPEF_BASE);
DWARF_ID(type_info->base_str,type_info->base);
IF_OK(dwarf_formsig8(*attr,&type_info->sig8,&error),type_info->flags|=(TYPEF_BASE|TYPEF_SIGNATURE));
break;
case DW_AT_low_pc:
IF_OK(dwarf_formsdata(*attr,&type_info->low_pc,&error),type_info->flags|=TYPEF_LOWPC);
break;
case DW_AT_data_member_location: // sdata
IF_OK(dwarf_formsdata(*attr,&type_info->data_member_location,&error),type_info->flags|=TYPEF_MEMBERLOC);
break;
case DW_AT_const_value: // sdata
IF_OK(dwarf_formsdata(*attr,&type_info->const_value,&error),type_info->flags|=TYPEF_CONSTVAL);
break;
case DW_AT_location: // sdata
IF_OK(dwarf_formsdata(*attr,&type_info->location,&error),type_info->flags|=TYPEF_LOCATION);
break;
case DW_AT_byte_size:
IF_OK(dwarf_formudata(*attr,&type_info->bytesize,&error),type_info->flags|=TYPEF_BYTESIZE);
break;
case DW_AT_bit_offset:
IF_OK(dwarf_formudata(*attr,&type_info->bitoffset,&error),type_info->flags|=TYPEF_BITOFFSET);
break;
case DW_AT_bit_size:
IF_OK(dwarf_formudata(*attr,&type_info->bitsize,&error),type_info->flags|=TYPEF_BITSIZE);
break;
case DW_AT_external:
IF_OK(dwarf_formflag(*attr,&type_info->external,&error),type_info->flags|=TYPEF_EXTERNAL);
break;
case DW_AT_upper_bound:
IF_OK(dwarf_formudata(*attr,&type_info->upper_bound,&error),type_info->flags|=TYPEF_UPPERBOUND);
break;
case DW_AT_encoding: // DW_ATE_unsigned, etc.
IF_OK(dwarf_formudata(*attr,&type_info->encoding,&error),type_info->flags|=TYPEF_ENCODING);
break;
case DW_AT_GNU_vector: // "The main difference between a regular array and the vector variant is that vectors are passed by value to functions."
type_info->flags|=TYPEF_VECTOR; // an attribute of an array
break;
case DW_AT_signature:
//case DW_AT_GNU_odr_signature: // One Definition Rule
IF_OK(dwarf_formsig8(*attr,&type_info->sig8,&error),type_info->flags|=TYPEF_SIGNATURE);
IF_OK(dwarf_formudata(*attr,(Dwarf_Unsigned *) &type_info->sig8,&error),type_info->flags|=TYPEF_SIGNATURE);
if (!(type_info->flags&TYPEF_SIGNATURE)) {
printf("sig attr (%d) with wrong form\n",vshort);
dump_attr(stderr);
}
break;
case DW_AT_linkage_name: // C++ mangling
IF_OK(dwarf_formstring(*attr,&vstr,&error),type_info->flags|=TYPEF_LINKAGE);
attr_set(&type_info->ltv,TYPE_LINK,vstr);
break;
case DW_AT_declaration: // i.e. not a definition (BOOLEAN)
type_info->flags|=TYPEF_IS_DECL;
break;
case DW_AT_GNU_odr_signature: // One Definition Rule
case DW_AT_sibling:
case DW_AT_high_pc:
case DW_AT_decl_column:
case DW_AT_decl_line:
case DW_AT_decl_file:
case DW_AT_call_line:
case DW_AT_call_file:
case DW_AT_GNU_all_tail_call_sites:
case DW_AT_GNU_all_call_sites:
case DW_AT_stmt_list:
case DW_AT_comp_dir:
case DW_AT_static_link:
case DW_AT_artificial: // __line__, etc.
case DW_AT_frame_base: // stack?
case DW_AT_inline:
case DW_AT_prototyped: // signature?
case DW_AT_language:
case DW_AT_producer:
case DW_AT_abstract_origin: // associated with DW_TAG_inlined_subroutine
case DW_AT_GNU_tail_call:
case DW_AT_GNU_call_site_value:
case DW_AT_GNU_macros: //
case DW_AT_specification: // C++?
case DW_AT_object_pointer: // C++
case DW_AT_pure: // C++
case DW_AT_accessibility:
case DW_AT_ranges:
case DW_AT_explicit:
case DW_AT_alignment:
case DW_AT_identifier_case:
case DW_AT_deleted:
case DW_AT_defaulted:
case DW_AT_const_expr:
case DW_AT_noreturn:
case DW_AT_default_value:
case DW_AT_GNU_dwo_id: // GNU DebugFission (split dwarf)
break;
default:
dump_attr(stderr);
break;
}
int get_expr_loclist_data(Dwarf_Unsigned exprlen,Dwarf_Ptr exprloc) {
int status=0;
Dwarf_Locdesc *llbuf;
Dwarf_Signed listlen;
STRY(dwarf_loclist_from_expr(dbg,exprloc,exprlen,&llbuf,&listlen,&error),"get exprloc");
for (int j=0;j<llbuf->ld_cents;j++)
{
if (llbuf->ld_s[j].lr_atom >= DW_OP_breg0 && llbuf->ld_s[j].lr_atom <= DW_OP_breg31) ;
else if (llbuf->ld_s[j].lr_atom >= DW_OP_reg0 && llbuf->ld_s[j].lr_atom <= DW_OP_reg31) ;
else if (llbuf->ld_s[j].lr_atom >= DW_OP_lit0 && llbuf->ld_s[j].lr_atom <= DW_OP_lit31) ;
else
switch(llbuf->ld_s[j].lr_atom)
{
case DW_OP_addr:
type_info->addr=llbuf->ld_s[j].lr_number;
type_info->flags|=TYPEF_ADDR;
break;
case DW_OP_GNU_push_tls_address: // THREAD LOCAL STORAGE
type_info->tag=0; // disqualify TLS variables
break;
case DW_OP_consts: case DW_OP_const1s: case DW_OP_const2s: case DW_OP_const4s: case DW_OP_const8s: // (Dwarf_Signed) llbuf->ld_s[j].lr_number
case DW_OP_constu: case DW_OP_const1u: case DW_OP_const2u: case DW_OP_const4u: case DW_OP_const8u: // llbuf->ld_s[j].lr_number
case DW_OP_fbreg: // (Dwarf_Signed) llbuf->ld_s[j].lr_number
case DW_OP_bregx: // printf(stdout," bregx %" DW_PR_DUu " + (%" DW_PR_DSd ") ",llbuf->ld_s[j].lr_number,llbuf->ld_s[j].lr_number2);
case DW_OP_regx: // printf(stdout," regx %" DW_PR_DUu " + (%" DW_PR_DSd ") ",llbuf->ld_s[j].lr_number,llbuf->ld_s[j].lr_number2);
case DW_OP_pick:
case DW_OP_plus_uconst:
case DW_OP_piece:
case DW_OP_deref_size:
case DW_OP_xderef_size:
case DW_OP_GNU_uninit:
case DW_OP_GNU_encoded_addr:
case DW_OP_GNU_implicit_pointer:
case DW_OP_GNU_entry_value:
case DW_OP_call_frame_cfa:
case DW_OP_deref:
case DW_OP_skip:
case DW_OP_bra:
case DW_OP_plus:
case DW_OP_shl:
case DW_OP_or:
case DW_OP_and:
case DW_OP_xor:
case DW_OP_eq:
case DW_OP_ne:
case DW_OP_gt:
case DW_OP_lt:
case DW_OP_shra:
case DW_OP_mul:
case DW_OP_minus:
case DW_OP_stack_value: // 0x9f
case DW_OP_lit16: // 0x40
case DW_OP_GNU_parameter_ref: // unreferenced parameter
case DW_OP_GNU_addr_index: // GNU DebugFission
case DW_OP_GNU_const_index: // GNU DebugFission
// fprintf(stdout," Ingnored DW_OP 0x%x n 0x%x n2 0x%x offset 0x%x",llbuf->ld_s[j].lr_atom,llbuf->ld_s[j].lr_number,llbuf->ld_s[j].lr_number2,llbuf->ld_s[j].lr_offset);
break;
default:
fprintf(stderr," Unrecognized DW_OP 0x%x n 0x%x n2 0x%x offset 0x%x",llbuf->ld_s[j].lr_atom,llbuf->ld_s[j].lr_number,llbuf->ld_s[j].lr_number2,llbuf->ld_s[j].lr_offset);
fprintf(stderr,CODE_RED " lowpc %" DW_PR_DUx " hipc %" DW_PR_DUx " ld_section_offset %" DW_PR_DUx " ld_from_loclist %s ld_cents %d ",
llbuf->ld_lopc,llbuf->ld_hipc,llbuf->ld_section_offset,llbuf->ld_from_loclist?"debug_loc":"debug_info",llbuf->ld_cents);
fprintf(stderr,CODE_RESET "\n");
break;
}
}
dwarf_dealloc(dbg,llbuf->ld_s, DW_DLA_LOC_BLOCK);
dwarf_dealloc(dbg,llbuf, DW_DLA_LOCDESC);
done:
return status;
};
Dwarf_Unsigned vuint;