-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJuicer.cpp
5785 lines (4962 loc) · 240 KB
/
Juicer.cpp
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
/****************************************************************************
*
* Copyright (c) 2017 Windhover Labs, L.L.C. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name Windhover Labs nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/
#include "Juicer.h"
#include <ctype.h>
#include <errno.h>
#include <libelf.h>
#include <memory.h>
#include <openssl/md5.h>
#include <string.h>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
#include "Artifact.h"
#include "ElfFile.h"
#include "Enumeration.h"
#include "Field.h"
#include "IDataContainer.h"
#include "Symbol.h"
#include "Variable.h"
struct macro_counts_s
{
long mc_start_file;
long mc_end_file;
long mc_define;
long mc_undef;
long mc_extension;
long mc_code_zero;
long mc_unknown;
};
Juicer::Juicer() {}
DefineMacro Juicer::getDefineMacroFromString(std::string macro_string)
{
// It is enforced by the DWARF standard that there is exactly 1-space character(" " space) between the macro name and value
int spacePos = macro_string.find(" ");
std::string name = macro_string.substr(0, spacePos);
std::string value = macro_string.substr(spacePos + 1, (macro_string.length() - name.length()) - 1);
DefineMacro newMacro{name, value};
return newMacro;
}
DefineMacro Juicer::getDefineMacro(Dwarf_Half macro_operator, Dwarf_Macro_Context mac_context, int i, Dwarf_Unsigned line_number, Dwarf_Unsigned index,
Dwarf_Unsigned offset, const char *macro_string, Dwarf_Half &forms_count, Dwarf_Error &error, Dwarf_Die cu_die, ElfFile &elf)
{
DefineMacro outMacro{"", ""};
int res = 0;
switch (macro_operator)
{
case 0:
{
/* End of these DWARF_MACRO ops */
logger.logDebug("End of these DWARF_MACRO ops");
break;
}
case DW_MACRO_end_file:
{
logger.logDebug("DW_MACRO_end_file\n");
break;
}
case DW_MACRO_define:
case DW_MACRO_undef:
{
res = dwarf_get_macro_defundef(mac_context, i, &line_number, &index, &offset, &forms_count, ¯o_string, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_get_macro_defundef. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
// return res;
}
else
{
auto macro = getDefineMacroFromString(macro_string);
outMacro = macro;
elf.addDefineMacro(macro);
}
break;
}
case DW_MACRO_define_strp:
case DW_MACRO_undef_strp:
{
res = dwarf_get_macro_defundef(mac_context, i, &line_number, &index, &offset, &forms_count, ¯o_string, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_get_macro_defundef. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
else
{
auto macro = getDefineMacroFromString(macro_string);
outMacro = macro;
elf.addDefineMacro(macro);
}
break;
}
case DW_MACRO_define_strx:
case DW_MACRO_undef_strx:
{
logger.logWarning("DW_MACRO_define_strx and DW_MACRO_undef_strx are not supported at the moment");
break;
}
case DW_MACRO_define_sup:
case DW_MACRO_undef_sup:
{
/* The strings here are from a supplementary
object file, not this object file.
Until we have a way to find
the supplementary object file
those will show name
"<no-name-available>"
*/
logger.logWarning("DW_MACRO_define_sup and DW_MACRO_undef_sup are not supported at the moment");
break;
}
case DW_MACRO_start_file:
{
logger.logWarning("DW_MACRO_start_file is not supported at the moment");
break;
}
case DW_MACRO_import:
{
res = dwarf_get_macro_import(mac_context, i, &offset, &error);
if (offset == 0)
{
/**
* @todo This should be re-visited.
* We're making an assumption that an offset of zero means that there is nothing there...
*
*/
logger.logWarning("Found offset of zero for DW_MACRO_import. Ignoring macro Entry.");
return outMacro;
}
if (res != DW_DLV_OK)
{
/**
* @todo Should be returning the actual return code rather than using a string this outMacro...
*/
return outMacro;
}
else
{
Dwarf_Unsigned mac_import_version;
Dwarf_Macro_Context mac_import_context;
Dwarf_Unsigned mac_import_unit_offset;
Dwarf_Unsigned mac_import_ops_count;
Dwarf_Unsigned mac_import_ops_data_length;
int res = dwarf_get_macro_context_by_offset(cu_die, offset, &mac_import_version, &mac_import_context, &mac_import_ops_count,
&mac_import_ops_data_length, &error);
logger.logDebug("mac_import_ops_count:%d\n", mac_import_ops_count);
logger.logDebug("mac_import_ops_data_length:%d\n", mac_import_ops_data_length);
for (int i = 0; i < mac_import_ops_count; i++)
{
Dwarf_Unsigned section_offset = 0;
Dwarf_Half macro_operator = 0;
Dwarf_Half forms_count = 0;
const Dwarf_Small *formcode_array = 0;
Dwarf_Unsigned line_number = 0;
Dwarf_Unsigned index = 0;
Dwarf_Unsigned offset = 0;
const char *macro_string = 0;
res = dwarf_get_macro_op(mac_import_context, i, §ion_offset, ¯o_operator, &forms_count, &formcode_array, &error);
if (res == DW_DLV_ERROR)
{
/**
* @todo Should be returning the actual return code rather than using a string this outMacro...
*/
return outMacro;
}
logger.logDebug("Found macro string:%s\n", macro_string);
auto newMacro =
getDefineMacro(macro_operator, mac_import_context, i, line_number, index, offset, macro_string, forms_count, error, cu_die, elf);
if (!newMacro.getName().empty())
{
elf.addDefineMacro(newMacro);
logger.logDebug("Found imported macro:%s", newMacro.getName());
}
}
}
break;
}
case DW_MACRO_import_sup:
{
logger.logWarning("DW_MACRO_start_file is not supported at the moment");
break;
}
} /* End switch(macro_operator) */
return outMacro;
}
/**
* Iterates through the CU lists of the dbg.
*/
int Juicer::readCUList(ElfFile &elf, Dwarf_Debug dbg, Dwarf_Error &error)
{
Dwarf_Unsigned cu_header_length = 0;
Dwarf_Half version_stamp = 0;
Dwarf_Unsigned abbrev_offset = 0;
Dwarf_Half address_size = 0;
Dwarf_Unsigned next_cu_header = 0;
// Dwarf_Error error = 0;
int cu_number = 0;
int return_value = JUICER_OK;
int res = 0;
while (1)
{
Dwarf_Die no_die = 0;
Dwarf_Die cu_die = 0;
int res = DW_DLV_ERROR;
++cu_number;
logger.logDebug("Reading CU %u.", cu_number);
DisplayDie(cu_die, 0);
Dwarf_Unsigned mac_version;
Dwarf_Macro_Context mac_context;
Dwarf_Unsigned mac_unit_offset;
Dwarf_Unsigned mac_ops_count;
Dwarf_Unsigned mac_ops_data_length;
res = dwarf_next_cu_header(dbg, &cu_header_length, &version_stamp, &abbrev_offset, &address_size, &next_cu_header, &error);
if (res == DW_DLV_ERROR)
{
logger.logError("Error in dwarf_next_cu_header. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
return_value = JUICER_ERROR;
}
else if (res == DW_DLV_NO_ENTRY)
{
/* Done. */
return_value = JUICER_OK;
break;
}
if (JUICER_OK == return_value)
{
/* The CU will have a single sibling, a cu_die. */
res = dwarf_siblingof(dbg, no_die, &cu_die, &error);
int mac_res = dwarf_get_macro_context(cu_die, &mac_version, &mac_context, &mac_unit_offset, &mac_ops_count, &mac_ops_data_length, &error);
Dwarf_Unsigned section_offset = 0;
Dwarf_Half macro_operator = 0;
Dwarf_Half forms_count = 0;
const Dwarf_Small *formcode_array = 0;
if (mac_res == 0)
{
for (int i = 0; i < mac_ops_count; i++)
{
Dwarf_Unsigned section_offset = 0;
Dwarf_Half macro_operator = 0;
Dwarf_Half forms_count = 0;
const Dwarf_Small *formcode_array = 0;
Dwarf_Unsigned line_number = 0;
Dwarf_Unsigned index = 0;
Dwarf_Unsigned offset = 0;
const char *macro_string = 0;
res = dwarf_get_macro_op(mac_context, i, §ion_offset, ¯o_operator, &forms_count, &formcode_array, &error);
if (res == DW_DLV_ERROR)
{
logger.logError("Error in dwarf_get_macro_op. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
else
{
auto newMacro =
getDefineMacro(macro_operator, mac_context, i, line_number, index, offset, macro_string, forms_count, error, cu_die, elf);
if (!newMacro.getName().empty())
{
elf.addDefineMacro(newMacro);
}
}
}
}
else
{
logger.logError("Error in dwarf_get_macro_context. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
/* Access to the macro operations, 0 to macro_ops_count_out-1
Where the last of these will have macro_operator 0 (which appears
in the ops data and means end-of-ops).
op_start_section_offset is the section offset of
the macro operator (which is a single unsigned byte,
and is followed by the macro operand data). */
// 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*/);
//
// 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*/);
if (res == DW_DLV_ERROR)
{
logger.logError("Error in dwarf_siblingof on CU die. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
return_value = JUICER_ERROR;
}
else if (res == DW_DLV_NO_ENTRY)
{
/* Impossible case. */
logger.logError("no entry! in dwarf_siblingof on CU die. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
return_value = JUICER_ERROR;
}
}
if (JUICER_OK == return_value)
{
char **filePaths = nullptr;
Dwarf_Signed fileCount = 0;
/**
* According to 6.2 Line Number Information in DWARF 4:
* Line number information generated for a compilation unit is represented in the .debug_line
* section of an object file and is referenced by a corresponding compilation unit debugging
* information entry (see Section 3.1.1) in the .debug_info section.
* This is why we are using dwarf_siblingof_b instead of dwarf_siblingof and setting
* the is_info to true.
*
* We are using a new Dwarf_Die because if we use cur_die, we segfault.
*
* My theory on this is that even though when we initially call dwarf_siblingof on
* cur_die and as we read different kinds of tags/attributes(in particular type-related),
* the libdwarf library is modifying the die when I call dwarf_srcfiles on it.
*
* Notice that in https://penguin.windhoverlabs.lan/gitlab/ground-systems/libdwarf/-/blob/main/libdwarf/libdwarf/dwarf_die_deliv.c#L1365
*
* This is just a a theory, however. In the future we may revisit this
* to figure out the root cause of this.
*
*/
// TODO: Move logic to the place where we load the CU die for the first time
// and make filePaths a field that all methods can access. Then, I think,
// we can index into that array with file_path_numbr and don't have to iterate through the
// entire list every time we find a new symbol.
Dwarf_Die src_die = 0;
int sres = dwarf_siblingof_b(dbg, NULL, true, &src_die, &error);
if (sres == DW_DLV_OK)
{
dwarf_srcfiles(src_die, &filePaths, &fileCount, &error);
}
if (filePaths != nullptr)
{
dbgSourceFiles.insert(dbgSourceFiles.begin(), filePaths, &filePaths[fileCount]);
}
return_value = getDieAndSiblings(elf, dbg, cu_die, 0);
}
if (JUICER_OK != return_value)
{
logger.logError("Error on siblings func");
}
dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
}
return return_value;
}
char *Juicer::dwarfStringToChar(char *dwarfString)
{
uint32_t length = strlen(dwarfString);
char *inOut = 0;
for (uint32_t i = 0; i < length; ++i)
{
if (!isdigit(dwarfString[i]))
{
inOut = &dwarfString[i];
break;
}
}
return inOut;
}
/**
* @brief Processes an array that belongs to the die inDie.
*
*@param elf The elf to write the new array symbol to.
*@param dbg the Dwarf Debug section structure
*@param inDie the die that contains the array.
*
* @todo Store the array data(its name, size, etc) in the elf for the
* database to store it. Make sure to add a "multiplicity" column to the
* symbols table.
*
* @return If the array symbol is added successfully to the elf, then DW_DLV_OK is returned.
* Otherwise, DW_DLV_ERROR is returned. Please note that just because DW_DLV_ERROR is returned
* it does NOT mean that no array was found. There are cases where an array is found on the die,
* however, because it has no name we decide to not add it to the elf at all.
*/
int Juicer::process_DW_TAG_array_type(ElfFile &elf, Symbol &symbol, Dwarf_Debug dbg, Dwarf_Die inDie)
{
Dwarf_Die dieSubrangeType;
Dwarf_Unsigned dwfUpperBound = 0;
DimensionList dimList{};
Dwarf_Error error = 0;
Dwarf_Attribute attr_struct = 0;
char *arrayName = nullptr;
int res = 0;
Dwarf_Die sib_die = 0;
Symbol *outSymbol = nullptr;
return res;
/* Now lets get the array size. Get the array size by getting
* the first child, which should be the subrange_type. */
res = dwarf_child(inDie, &dieSubrangeType, &error);
if (res == DW_DLV_ERROR)
{
logger.logError("Error in dwarf_child. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
/* Make sure this is the subrange_type tag. */
if (res == DW_DLV_OK)
{
Dwarf_Half childTag;
res = dwarf_tag(dieSubrangeType, &childTag, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_tag. %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
}
else
{
if (childTag != DW_TAG_subrange_type)
{
logger.logError("Unexpected child in array. tag=%u", childTag);
res = DW_DLV_ERROR;
}
}
}
/* Get the upper bound. */
if (res == DW_DLV_OK)
{
res = dwarf_attr(dieSubrangeType, DW_AT_upper_bound, &attr_struct, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_attr(DW_AT_upper_bound). %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
}
if (res == DW_DLV_OK)
{
res = dwarf_formudata(attr_struct, &dwfUpperBound, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_formudata. line=%u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
}
}
/* Set the multiplicity, the array's size. */
if (res == DW_DLV_OK)
{
dimList = getDimList(dbg, dieSubrangeType);
}
}
res = dwarf_siblingof(dbg, inDie, &sib_die, &error);
if (res == DW_DLV_ERROR)
{
logger.logError("Error in dwarf_siblingof , function process_DW_TAG_array_type. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
;
}
else
{
res = dwarf_diename(sib_die, &arrayName, &error);
if (DW_DLV_ERROR == res || DW_DLV_NO_ENTRY == res)
{
logger.logError("Error in dwarf_diename , function process_DW_TAG_array_type. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
else
{
/**
*@todo Logic needs to be cleaned up.
*I think we need to handle the case when arraySymbol is nullptr.
*/
std::string stdString{arrayName};
Symbol *arraySymbol = getBaseTypeSymbol(elf, inDie, dimList);
if (nullptr == arraySymbol)
{
res = DW_DLV_ERROR;
logger.logError("Base type not found for %s", arrayName);
}
else
{
std::string arrayBaseType{arraySymbol->getName().c_str()};
outSymbol = elf.getSymbol(arrayBaseType);
outSymbol->addField(stdString, 0, *outSymbol, dimList, elf.isLittleEndian());
}
}
}
return res;
}
char *Juicer::getFirstAncestorName(Dwarf_Die inDie)
{
Dwarf_Attribute attr_struct;
Dwarf_Off typeOffset = 0;
Dwarf_Die typeDie;
char *outName = nullptr;
Dwarf_Bool hasName = false;
Dwarf_Error error = 0;
/* Get the type attribute. */
res = dwarf_attr(inDie, DW_AT_type, &attr_struct, &error);
/* Get the offset to the type Die. */
if (res == DW_DLV_OK)
{
res = dwarf_global_formref(attr_struct, &typeOffset, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_formref. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
}
/* Get the type Die. */
if (res == DW_DLV_OK)
{
res = dwarf_offdie(dbg, typeOffset, &typeDie, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_offdie. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
}
/* Does this die have a name? */
if (res == DW_DLV_OK)
{
res = dwarf_hasattr(typeDie, DW_AT_name, &hasName, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_hasattr(DW_AT_name). %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
}
}
if (res == DW_DLV_OK)
{
if (hasName == false)
{
outName = getFirstAncestorName(typeDie);
}
else
{
/* Get the name of the type Die. */
res = dwarf_attr(typeDie, DW_AT_name, &attr_struct, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_attr(DW_AT_name). %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
}
if (res == DW_DLV_OK)
{
res = dwarf_formstring(attr_struct, &outName, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_formstring. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
}
}
}
return outName;
}
Symbol *Juicer::process_DW_TAG_pointer_type(ElfFile &elf, Dwarf_Debug dbg, Dwarf_Die inDie)
{
Symbol *outSymbol = 0;
Dwarf_Attribute attr_struct = nullptr;
Dwarf_Off typeOffset = 0;
Dwarf_Die typeDie = nullptr;
Dwarf_Error error = 0;
char *typeDieName;
/* Get the type attribute. */
res = dwarf_attr(inDie, DW_AT_type, &attr_struct, &error);
if (res != DW_DLV_OK)
{
logger.logDebug("Ignoring error in dwarf_attr(DW_AT_type). %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
int voidRes = dwarf_attr(inDie, DW_AT_byte_size, &attr_struct, &error);
if (voidRes != DW_DLV_OK)
{
logger.logDebug("Ignoring error in dwarf_attr(DW_AT_byte_size). %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
}
else
{
Dwarf_Unsigned byteSize = 0;
std::string voidType{"void*"};
voidRes = dwarf_formudata(attr_struct, &byteSize, &error);
if (DW_DLV_OK == voidRes)
{
// TODO:Not sure how to deal with this in the case of void* at the moment...
unsigned long long pathIndex = 0;
res = dwarf_formudata(attr_struct, &pathIndex, &error);
/**
* According to 6.2 Line Number Information in DWARF 4:
* Line number information generated for a compilation unit is represented in the .debug_line
* section of an object file and is referenced by a corresponding compilation unit debugging
* information entry (see Section 3.1.1) in the .debug_info section.
* This is why we are using dwarf_siblingof_b instead of dwarf_siblingof and setting
* the is_info to true.
*
* We are using a new Dwarf_Die because if we use cur_die, we segfault.
*
* My theory on this is that even though when we initially call dwarf_siblingof on
* cur_die and as we read different kinds of tags/attributes(in particular type-related),
* the libdwarf library is modifying the die when I call dwarf_srcfiles on it.
*
* Notice that in https://penguin.windhoverlabs.lan/gitlab/ground-systems/libdwarf/-/blob/main/libdwarf/libdwarf/dwarf_die_deliv.c#L1365
*
* This is just a theory, however. In the future we may revisit this
* to figure out the root cause of this.
*
*/
if (pathIndex != 0)
{
/**
* Why we are checking against 0 as per DWARF section 2.14:
*
* The value of the DW_AT_decl_file attribute corresponds to a file number from the line number
* information table for the compilation unit containing the debugging information entry and
* represents the source file in which the declaration appeared (see Section 6.2 ). The value 0
* indicates that no source file has been specified.
*
*/
/* This branch represents a "void*" since there is no valid type.
* Read section 5.2 of DWARF4 for details on this.*/
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex).value_or(std::string{"NOT_FOUND:"})};
std::string checkSum = generateMD5SumForFile(newArtifact.getFilePath());
newArtifact.setMD5(checkSum);
outSymbol = elf.addSymbol(voidType, byteSize, newArtifact);
}
else
{
/* This branch represents a "void*" since there is no valid type.
* Read section 5.2 of DWARF4 for details on this.*/
Artifact newArtifact{elf, "NOT_FOUND:" + voidType};
std::string checkSum{};
newArtifact.setMD5(checkSum);
outSymbol = elf.addSymbol(voidType, byteSize, newArtifact);
}
}
}
}
/* Get the offset to the type Die. */
if (res == DW_DLV_OK)
{
res = dwarf_global_formref(attr_struct, &typeOffset, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_formref. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
}
/* Get the type Die. */
if (res == DW_DLV_OK)
{
res = dwarf_offdie(dbg, typeOffset, &typeDie, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_offdie. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
}
/* Get the name of the type Die. */
typeDieName = getFirstAncestorName(inDie);
if (res == DW_DLV_OK)
{
Dwarf_Unsigned byteSize;
std::string name = typeDieName;
name = name + "*";
res = dwarf_bytesize(inDie, &byteSize, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_bytesize. %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
}
if (res == DW_DLV_OK)
{
/**
* According to 6.2 Line Number Information in DWARF 4:
* Line number information generated for a compilation unit is represented in the .debug_line
* section of an object file and is referenced by a corresponding compilation unit debugging
* information entry (see Section 3.1.1) in the .debug_info section.
* This is why we are using dwarf_siblingof_b instead of dwarf_siblingof and setting
* the is_info to true.
*
* We are using a new Dwarf_Die because if we use cur_die, we segfault.
*
* My theory on this is that even though when we initially call dwarf_siblingof on
* cur_die and as we read different kinds of tags/attributes(in particular type-related),
* the libdwarf library is modifying the die when I call dwarf_srcfiles on it.
*
* Notice that in https://penguin.windhoverlabs.lan/gitlab/ground-systems/libdwarf/-/blob/main/libdwarf/libdwarf/dwarf_die_deliv.c#L1365
*
* This is just a theory, however. In the future we may revisit this
* to figure out the root cause of this.
*
*/
// As per the DWARF; pointer types do not have any "DECL" attributes, aka declaration coords (line numbers, declaration files, etc).
Artifact newArtifact{elf, "NOT_FOUND:" + name};
std::string checkSum{};
newArtifact.setMD5(checkSum);
outSymbol = elf.addSymbol(name, byteSize, newArtifact);
}
}
return outSymbol;
}
Symbol *Juicer::process_DW_TAG_variable_type(ElfFile &elf, Dwarf_Debug dbg, Dwarf_Die inDie)
{
Symbol *outSymbol = 0;
Dwarf_Attribute attr_struct = nullptr;
Dwarf_Off typeOffset = 0;
Dwarf_Die typeDie = nullptr;
Dwarf_Error error = 0;
char *typeDieName;
/* Get the type attribute. */
res = dwarf_attr(inDie, DW_AT_type, &attr_struct, &error);
if (res != DW_DLV_OK)
{
logger.logDebug("Ignoring error in dwarf_attr(DW_AT_type). %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
}
/* Get the offset to the type Die. */
if (res == DW_DLV_OK)
{
res = dwarf_global_formref(attr_struct, &typeOffset, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_formref. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
}
/* Get the type Die. */
if (res == DW_DLV_OK)
{
res = dwarf_offdie(dbg, typeOffset, &typeDie, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_offdie. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
}
/* Get the name of the type Die. */
typeDieName = getFirstAncestorName(inDie);
if (res == DW_DLV_OK)
{
Dwarf_Unsigned byteSize;
std::string name = typeDieName;
name = name + "*";
char *outName;
Dwarf_Bool hasName;
/* Does this die have a name? */
if (res == DW_DLV_OK)
{
res = dwarf_hasattr(inDie, DW_AT_name, &hasName, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_hasattr(DW_AT_name). %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
}
}
if (res == DW_DLV_OK)
{
if (hasName == false)
{
// TODO:
// outName = getFirstAncestorName(typeDie);
}
else
{
/* Get the name of the type Die. */
res = dwarf_attr(inDie, DW_AT_name, &attr_struct, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_attr(DW_AT_name). %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
}
if (res == DW_DLV_OK)
{
res = dwarf_formstring(attr_struct, &outName, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_formstring. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
if (res == DW_DLV_OK)
{
logger.logInfo("Found variable with name \"%s\"", outName);
DimensionList dimList{};
// TODO:Really don't like the pattern of passing an empty object to getBaseTypeSymbol...
Symbol *s = getBaseTypeSymbol(elf, inDie, dimList);
if (s != nullptr)
{
Variable newVariable{outName, *s, elf};
if (elf.getInitializedSymbolData().find(outName) != elf.getInitializedSymbolData().end())
{
/**
* @todo variableData might be useful if we want to store the data
* inside the ELF inside of the db.
* CFS tables is an example of this.
* Though it should be noted that the data can be
* extracted by querying the SQL db; query_symbols.py is an example of this.
*
*/
std::vector<uint8_t> variableData = elf.getInitializedSymbolData().at(outName);
}
elf.addVariable(newVariable);
}
}
}
}
}
}
return outSymbol;
}
Symbol *Juicer::getBaseTypeSymbol(ElfFile &elf, Dwarf_Die inDie, DimensionList &dimList)
{
int res = DW_DLV_OK;
Dwarf_Attribute attr_struct;
Dwarf_Die typeDie = 0;
Dwarf_Off typeOffset = 0;
Symbol *outSymbol = 0;
char *dieName = 0;
Dwarf_Half tag;
std::string cName;
Dwarf_Error error = 0;
/* Get the type attribute. */
res = dwarf_attr(inDie, DW_AT_type, &attr_struct, &error);
if (res != DW_DLV_OK)
{
logger.logWarning("Cannot find data type. Skipping. %u errno=%u %s ", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
}
/* Get the offset to the type Die. */
if (res == DW_DLV_OK)
{
res = dwarf_global_formref(attr_struct, &typeOffset, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_formref. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
}
/* Get the type Die. */
if (res == DW_DLV_OK)
{
res = dwarf_offdie(dbg, typeOffset, &typeDie, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_offdie. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
}
/* Get the tag so we know how to process it. */
if (res == DW_DLV_OK)
{
res = dwarf_tag(typeDie, &tag, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_tag. %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
}
}
if (res == DW_DLV_OK)
{
switch (tag)
{
case DW_TAG_pointer_type:
{
outSymbol = process_DW_TAG_pointer_type(elf, dbg, typeDie);
break;
}
case DW_TAG_structure_type:
{
Dwarf_Bool structHasName = false;
Dwarf_Bool parentHasName = false;
Dwarf_Unsigned byteSize = 0;
/* Does the structure type itself have the name? */
res = dwarf_hasattr(typeDie, DW_AT_name, &structHasName, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_hasattr(DW_AT_name). errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
res = dwarf_hasattr(inDie, DW_AT_name, &parentHasName, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_hasattr(DW_AT_name). errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
/* Read the name from the Die that has it. */
if (structHasName)
{
res = dwarf_attr(typeDie, DW_AT_name, &attr_struct, &error);