-
Notifications
You must be signed in to change notification settings - Fork 150
/
Symtab.C
3551 lines (3007 loc) · 94.7 KB
/
Symtab.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
/*
* See the dyninst/COPYRIGHT file for copyright information.
*
* We provide the Paradyn Tools (below described as "Paradyn")
* on an AS IS basis, and do not warrant its validity or performance.
* We reserve the right to update, modify, or discontinue this
* software at any time. We shall have no obligation to supply such
* updates or modifications or any other form of support to you.
*
* By your use of Paradyn, you understand and agree that we (or any
* other person or entity with proprietary rights in Paradyn) are
* under no obligation to provide either maintenance services,
* update services, notices of latent defects, or correction of
* defects for Paradyn.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include "common/src/Timer.h"
#include "common/src/debugOstream.h"
#include "common/src/serialize.h"
#include "common/src/pathName.h"
#include "Serialization.h"
#include "Symtab.h"
#include "Module.h"
#include "Collections.h"
#include "Function.h"
#include "Variable.h"
#include "annotations.h"
#include "debug.h"
#include "symtabAPI/src/Object.h"
#if !defined(os_windows)
#include <dlfcn.h>
#else
#include <windows.h>
#endif
#include <iomanip>
#include <stdarg.h>
#include "dyninstversion.h"
using namespace Dyninst;
using namespace Dyninst::SymtabAPI;
using namespace std;
static std::string errMsg;
extern bool parseCompilerType(Object *);
static const int Symtab_major_version = DYNINST_MAJOR_VERSION;
static const int Symtab_minor_version = DYNINST_MINOR_VERSION;
static const int Symtab_maintenance_version = DYNINST_PATCH_VERSION;
void Symtab::version(int& major, int& minor, int& maintenance)
{
major = Symtab_major_version;
minor = Symtab_minor_version;
maintenance = Symtab_maintenance_version;
}
void symtab_log_perror(const char *msg)
{
errMsg = std::string(msg);
};
SymtabError serr;
std::vector<Symtab *> Symtab::allSymtabs;
SymtabError Symtab::getLastSymtabError()
{
return serr;
}
void setSymtabError(SymtabError new_err)
{
serr = new_err;
}
std::string Symtab::printError(SymtabError serr)
{
switch (serr)
{
case Obj_Parsing:
return "Failed to parse the Object"+errMsg;
case Syms_To_Functions:
return "Failed to convert Symbols to Functions";
case No_Such_Function:
return "Function does not exist";
case No_Such_Variable:
return "Variable does not exist";
case No_Such_Module:
return "Module does not exist";
case No_Such_Region:
return "Region does not exist";
case No_Such_Symbol:
return "Symbol does not exist";
case Not_A_File:
return "Not a File. Call openArchive()";
case Not_An_Archive:
return "Not an Archive. Call openFile()";
case Export_Error:
return "Error Constructing XML"+errMsg;
case Emit_Error:
return "Error rewriting binary: " + errMsg;
case Invalid_Flags:
return "Flags passed are invalid.";
case No_Error:
return "No previous Error.";
default:
return "Unknown Error";
}
}
boost::shared_ptr<Type> Symtab::type_Error()
{
static boost::shared_ptr<Type> store =
boost::shared_ptr<Type>(new Type(std::string("<error>"), 0, dataUnknownType));
return store;
}
boost::shared_ptr<Type> Symtab::type_Untyped()
{
static boost::shared_ptr<Type> store =
boost::shared_ptr<Type>(new Type(std::string("<no type>"), 0, dataUnknownType));
return store;
}
boost::shared_ptr<builtInTypeCollection> Symtab::builtInTypes()
{
static boost::shared_ptr<builtInTypeCollection> store =
setupBuiltinTypes();
return store;
}
boost::shared_ptr<typeCollection> Symtab::stdTypes()
{
static boost::shared_ptr<typeCollection> store =
setupStdTypes();
return store;
}
boost::shared_ptr<builtInTypeCollection> Symtab::setupBuiltinTypes()
{
boost::shared_ptr<builtInTypeCollection> builtInTypes =
boost::shared_ptr<builtInTypeCollection>(new builtInTypeCollection);
typeScalar *newType;
// NOTE: integral type mean twos-complement
// -1 int, 32 bit signed integral type
// in stab document, size specified in bits, system size is in bytes
builtInTypes->addBuiltInType(newType = new typeScalar(-1, 4, "int", true));
newType->decrRefCount();
// -2 char, 8 bit type holding a character. GDB treats as signed
builtInTypes->addBuiltInType(newType = new typeScalar(-2, 1, "char", true));
newType->decrRefCount();
// -3 short, 16 bit signed integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-3, 2, "short", true));
newType->decrRefCount();
// -4 long, 32/64 bit signed integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-4, sizeof(long), "long", true));
newType->decrRefCount();
// -5 unsigned char, 8 bit unsigned integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-5, 1, "unsigned char"));
newType->decrRefCount();
// -6 signed char, 8 bit signed integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-6, 1, "signed char", true));
newType->decrRefCount();
// -7 unsigned short, 16 bit unsigned integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-7, 2, "unsigned short"));
newType->decrRefCount();
// -8 unsigned int, 32 bit unsigned integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-8, 4, "unsigned int"));
newType->decrRefCount();
// -9 unsigned, 32 bit unsigned integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-9, 4, "unsigned"));
newType->decrRefCount();
// -10 unsigned long, 32 bit unsigned integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-10, sizeof(unsigned long), "unsigned long"));
newType->decrRefCount();
// -11 void, type indicating the lack of a value
// XXX-size may not be correct jdd 4/22/99
builtInTypes->addBuiltInType(newType = new typeScalar(-11, 0, "void", false));
newType->decrRefCount();
// -12 float, IEEE single precision
builtInTypes->addBuiltInType(newType = new typeScalar(-12, sizeof(float), "float", true));
newType->decrRefCount();
// -13 double, IEEE double precision
builtInTypes->addBuiltInType(newType = new typeScalar(-13, sizeof(double), "double", true));
newType->decrRefCount();
// -14 long double, IEEE double precision, size may increase in future
builtInTypes->addBuiltInType(newType = new typeScalar(-14, sizeof(long double), "long double", true));
newType->decrRefCount();
// -15 integer, 32 bit signed integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-15, 4, "integer", true));
newType->decrRefCount();
// -16 boolean, 32 bit type. GDB/GCC 0=False, 1=True, all other values
// have unspecified meaning
builtInTypes->addBuiltInType(newType = new typeScalar(-16, sizeof(bool), "boolean"));
newType->decrRefCount();
// -17 short real, IEEE single precision
// XXX-size may not be correct jdd 4/22/99
builtInTypes->addBuiltInType(newType = new typeScalar(-17, sizeof(float), "short real", true));
newType->decrRefCount();
// -18 real, IEEE double precision XXX-size may not be correct jdd 4/22/99
builtInTypes->addBuiltInType(newType = new typeScalar(-18, sizeof(double), "real", true));
newType->decrRefCount();
// -19 stringptr XXX- size of void * -- jdd 4/22/99
builtInTypes->addBuiltInType(newType = new typeScalar(-19, sizeof(void *), "stringptr"));
newType->decrRefCount();
// -20 character, 8 bit unsigned character type
builtInTypes->addBuiltInType(newType = new typeScalar(-20, 1, "character"));
newType->decrRefCount();
// -21 logical*1, 8 bit type (Fortran, used for boolean or unsigned int)
builtInTypes->addBuiltInType(newType = new typeScalar(-21, 1, "logical*1"));
newType->decrRefCount();
// -22 logical*2, 16 bit type (Fortran, some for boolean or unsigned int)
builtInTypes->addBuiltInType(newType = new typeScalar(-22, 2, "logical*2"));
newType->decrRefCount();
// -23 logical*4, 32 bit type (Fortran, some for boolean or unsigned int)
builtInTypes->addBuiltInType(newType = new typeScalar(-23, 4, "logical*4"));
newType->decrRefCount();
// -24 logical, 32 bit type (Fortran, some for boolean or unsigned int)
builtInTypes->addBuiltInType(newType = new typeScalar(-24, 4, "logical"));
newType->decrRefCount();
// -25 complex, consists of 2 IEEE single-precision floating point values
builtInTypes->addBuiltInType(newType = new typeScalar(-25, sizeof(float)*2, "complex", true));
newType->decrRefCount();
// -26 complex, consists of 2 IEEE double-precision floating point values
builtInTypes->addBuiltInType(newType = new typeScalar(-26, sizeof(double)*2, "complex*16", true));
newType->decrRefCount();
// -27 integer*1, 8 bit signed integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-27, 1, "integer*1", true));
newType->decrRefCount();
// -28 integer*2, 16 bit signed integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-28, 2, "integer*2", true));
newType->decrRefCount();
/* Quick hack to make integer*4 compatible with int for Fortran
jnb 6/20/01 */
// This seems questionable - let's try removing that hack - jmo 05/21/04
/*
builtInTypes->addBuiltInType(newType = new type("int",-29,
built_inType, 4));
newType->decrRefCount();
*/
// -29 integer*4, 32 bit signed integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-29, 4, "integer*4", true));
newType->decrRefCount();
// -30 wchar, Wide character, 16 bits wide, unsigned (unknown format)
builtInTypes->addBuiltInType(newType = new typeScalar(-30, 2, "wchar"));
newType->decrRefCount();
#if defined(os_windows)
// -31 long long, 64 bit signed integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-31, sizeof(LONGLONG), "long long", true));
newType->decrRefCount();
// -32 unsigned long long, 64 bit unsigned integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-32, sizeof(ULONGLONG), "unsigned long long"));
newType->decrRefCount();
#else
// -31 long long, 64 bit signed integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-31, sizeof(long long), "long long", true));
newType->decrRefCount();
// -32 unsigned long long, 64 bit unsigned integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-32, sizeof(unsigned long long), "unsigned long long"));
newType->decrRefCount();
#endif
// -33 logical*8, 64 bit unsigned integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-33, 8, "logical*8"));
newType->decrRefCount();
// -34 integer*8, 64 bit signed integral type
builtInTypes->addBuiltInType(newType = new typeScalar(-34, 8, "integer*8", true));
newType->decrRefCount();
return builtInTypes;
}
boost::shared_ptr<typeCollection> Symtab::setupStdTypes()
{
boost::shared_ptr<typeCollection> stdTypes =
boost::shared_ptr<typeCollection>(new typeCollection);
typeScalar *newType;
stdTypes->addType(newType = new typeScalar(-1, sizeof(int), "int"));
newType->decrRefCount();
Type *charType = new typeScalar(-2, sizeof(char), "char");
stdTypes->addType(charType);
std::string tName = "char *";
typePointer *newPtrType;
stdTypes->addType(newPtrType = new typePointer(-3, charType, tName));
charType->decrRefCount();
newPtrType->decrRefCount();
Type *voidType = new typeScalar(-11, 0, "void", false);
stdTypes->addType(voidType);
tName = "void *";
stdTypes->addType(newPtrType = new typePointer(-4, voidType, tName));
voidType->decrRefCount();
newPtrType->decrRefCount();
stdTypes->addType(newType = new typeScalar(-12, sizeof(float), "float"));
newType->decrRefCount();
#if defined(i386_unknown_nt4_0)
stdTypes->addType(newType = new typeScalar(-31, sizeof(LONGLONG), "long long"));
#else
stdTypes->addType(newType = new typeScalar(-31, sizeof(long long), "long long"));
#endif
newType->decrRefCount();
return stdTypes;
}
SYMTAB_EXPORT unsigned Symtab::getAddressWidth() const
{
return address_width_;
}
SYMTAB_EXPORT bool Symtab::getABIVersion(int &major, int &minor) const
{
return obj_private->getABIVersion(major, minor);
}
SYMTAB_EXPORT bool Symtab::isBigEndianDataEncoding() const
{
return obj_private->isBigEndianDataEncoding();
}
SYMTAB_EXPORT bool Symtab::isNativeCompiler() const
{
return nativeCompiler;
}
SYMTAB_EXPORT Symtab::Symtab(MappedFile *mf_) :
AnnotatableSparse(),
member_offset_(0),
parentArchive_(NULL),
mf(mf_), mfForDebugInfo(NULL),
imageOffset_(0), imageLen_(0),
dataOffset_(0), dataLen_(0),
is_a_out(false),
main_call_addr_(0),
nativeCompiler(false),
address_width_(sizeof(int)),
code_ptr_(NULL), data_ptr_(NULL),
entry_address_(0), base_address_(0), load_address_(0),
object_type_(obj_Unknown), is_eel_(false),
no_of_sections(0),
newSectionInsertPoint(0),
no_of_symbols(0),
sorted_everyFunction(false),
isTypeInfoValid_(false),
nlines_(0), fdptr_(0), lines_(NULL),
stabstr_(NULL), nstabs_(0), stabs_(NULL),
stringpool_(NULL),
hasRel_(false), hasRela_(false), hasReldyn_(false),
hasReladyn_(false), hasRelplt_(false), hasRelaplt_(false),
isStaticBinary_(false), isDefensiveBinary_(false),
func_lookup(NULL),
mod_lookup_(NULL),
obj_private(NULL),
_ref_cnt(1)
{
init_debug_symtabAPI();
#if defined(os_vxworks)
// This is how we initialize objects from WTX information alone.
// Basically replaces extractInfo().
object_type_ = obj_RelocatableFile;
// (... the rest are now initialized for everyone above ...)
#endif
}
SYMTAB_EXPORT Symtab::Symtab() :
LookupInterface(),
Serializable(),
AnnotatableSparse(),
member_offset_(0),
parentArchive_(NULL),
mf(NULL), mfForDebugInfo(NULL),
imageOffset_(0), imageLen_(0),
dataOffset_(0), dataLen_(0),
is_a_out(false),
main_call_addr_(0),
nativeCompiler(false),
address_width_(sizeof(int)),
code_ptr_(NULL), data_ptr_(NULL),
entry_address_(0), base_address_(0), load_address_(0),
object_type_(obj_Unknown), is_eel_(false),
no_of_sections(0),
newSectionInsertPoint(0),
no_of_symbols(0),
sorted_everyFunction(false),
isTypeInfoValid_(false),
nlines_(0), fdptr_(0), lines_(NULL),
stabstr_(NULL), nstabs_(0), stabs_(NULL),
stringpool_(NULL),
hasRel_(false), hasRela_(false), hasReldyn_(false),
hasReladyn_(false), hasRelplt_(false), hasRelaplt_(false),
isStaticBinary_(false), isDefensiveBinary_(false),
func_lookup(NULL),
mod_lookup_(NULL),
obj_private(NULL),
_ref_cnt(1)
{
init_debug_symtabAPI();
create_printf("%s[%d]: Created symtab via default constructor\n", FILE__, __LINE__);
}
SYMTAB_EXPORT bool Symtab::isExec() const
{
return is_a_out;
}
SYMTAB_EXPORT bool Symtab::isStripped()
{
#if defined(os_linux) || defined(os_freebsd)
Region *sec;
return !findRegion(sec,".symtab");
#else
return (no_of_symbols==0);
#endif
}
SYMTAB_EXPORT Offset Symtab::preferedBase() const
{
return preferedBase_;
}
SYMTAB_EXPORT Offset Symtab::imageOffset() const
{
return imageOffset_;
}
SYMTAB_EXPORT Offset Symtab::dataOffset() const
{
return dataOffset_;
}
SYMTAB_EXPORT Offset Symtab::dataLength() const
{
return dataLen_;
}
SYMTAB_EXPORT Offset Symtab::imageLength() const
{
return imageLen_;
}
SYMTAB_EXPORT void Symtab::fixup_code_and_data(Offset newImageOffset,
Offset newImageLength,
Offset newDataOffset,
Offset newDataLength)
{
imageOffset_ = newImageOffset;
imageLen_ = newImageLength;
dataOffset_ = newDataOffset;
dataLen_ = newDataLength;
// Should we update the underlying Object?
}
/*
SYMTAB_EXPORT char* Symtab::image_ptr () const
{
return code_ptr_;
}
SYMTAB_EXPORT char* Symtab::data_ptr () const
{
return data_ptr_;
}
*/
SYMTAB_EXPORT const char* Symtab::getInterpreterName() const
{
if (interpreter_name_.length())
return interpreter_name_.c_str();
return NULL;
}
SYMTAB_EXPORT Offset Symtab::getEntryOffset() const
{
return entry_address_;
}
SYMTAB_EXPORT Offset Symtab::getBaseOffset() const
{
return base_address_;
}
SYMTAB_EXPORT Offset Symtab::getLoadOffset() const
{
return load_address_;
}
SYMTAB_EXPORT Offset Symtab::getTOCoffset(Function *func) const
{
return getTOCoffset(func ? func->getOffset() : 0);
}
SYMTAB_EXPORT Offset Symtab::getTOCoffset(Offset off) const
{
return obj_private->getTOCoffset(off);
}
void Symtab::setTOCOffset(Offset off) {
obj_private->setTOCoffset(off);
return;
}
SYMTAB_EXPORT string Symtab::getDefaultNamespacePrefix() const
{
return defaultNamespacePrefix;
}
// TODO -- is this g++ specific
bool Symtab::buildDemangledName( const std::string &mangled,
std::string &pretty,
std::string &typed,
bool nativeCompiler,
supportedLanguages lang )
{
/* The C++ demangling function demangles MPI__Allgather (and other MPI__
* functions with start with A) into the MPI constructor. In order to
* prevent this a hack needed to be made, and this seemed the cleanest
* approach.
*/
if ((mangled.length()>5) && (mangled.substr(0,5)==std::string("MPI__")))
{
return false;
}
/* If it's Fortran, eliminate the trailing underscores, if any. */
if (lang == lang_Fortran
|| lang == lang_CMFortran
|| lang == lang_Fortran_with_pretty_debug )
{
if ( mangled[ mangled.length() - 1 ] == '_' )
{
char * demangled = P_strdup( mangled.c_str() );
demangled[ mangled.length() - 1 ] = '\0';
pretty = std::string( demangled );
free ( demangled );
return true;
}
else
{
/* No trailing underscores, do nothing */
return false;
}
} /* end if it's Fortran. */
// Check to see if we have a gnu versioned symbol on our hands.
// These are of the form <symbol>@<version> or <symbol>@@<version>
//
// If we do, we want to create a "demangled" name for the one that
// is of the form <symbol>@@<version> since this is, by definition,
// the default. The "demangled" name will just be <symbol>
// NOTE: this is just a 0th order approach to dealing with versioned
// symbols. We may need to do something more sophisticated
// in the future. JAW 10/03
#if !defined(os_windows)
const char *atat;
if (NULL != (atat = strstr(mangled.c_str(), "@@")))
{
pretty = mangled.substr(0 /*start pos*/,
(int)(atat - mangled.c_str())/*len*/);
//char msg[256];
//sprintf(msg, "%s[%d]: 'demangling' versioned symbol: %s, to %s",
// __FILE__, __LINE__, mangled.c_str(), pretty.c_str());
//cerr << msg << endl;
//logLine(msg);
return true;
}
#endif
bool retval = false;
/* Try demangling it. */
char * demangled = P_cplus_demangle( mangled.c_str(), nativeCompiler, false);
if (demangled)
{
pretty = std::string( demangled );
retval = true;
}
char *t_demangled = P_cplus_demangle(mangled.c_str(), nativeCompiler, true);
if (t_demangled && (strcmp(t_demangled, demangled) != 0))
{
typed = std::string(t_demangled);
retval = true;
}
if (demangled)
free(demangled);
if (t_demangled)
free(t_demangled);
return retval;
} /* end buildDemangledName() */
/*
* extractSymbolsFromFile
*
* Create a Symtab-level list of symbols by pulling out data
* from the low-level parse (linkedFile).
* Technically this causes a duplication of symbols; however,
* we will be rewriting these symbols and so we need our own
* copy.
*
* TODO: delete the linkedFile once we're done?
*/
bool Symtab::extractSymbolsFromFile(Object *linkedFile, std::vector<Symbol *> &raw_syms)
{
for (SymbolIter symIter(*linkedFile); symIter; symIter++) {
Symbol *sym = symIter.currval();
if (!sym) {
create_printf("%s[%d]: range error, stopping now\n", FILE__, __LINE__);
return true;
}
// If a symbol starts with "." we want to skip it. These indicate labels in the
// code.
// removed 1/09: this should be done in Dyninst, not Symtab
// Have to do this before the undef check, below.
fixSymRegion(sym);
// check for undefined dynamic symbols. Used when rewriting relocation section.
// relocation entries have references to these undefined dynamic symbols.
// We also have undefined symbols for the static binary case.
#if !defined(os_vxworks)
if (sym->getRegion() == NULL && !sym->isAbsolute() && !sym->isCommonStorage()) {
undefDynSyms.insert(sym);
continue;
}
#endif
// Check whether this symbol has a valid offset. If they do not we have a
// consistency issue. This should be a null check.
// Symbols can have an offset of 0 if they don't refer to things within a file.
raw_syms.push_back(sym);
}
return true;
}
bool Symtab::fixSymRegion(Symbol *sym) {
if (!sym->getRegion()) return true;
if (sym->getType() != Symbol::ST_FUNCTION &&
sym->getType() != Symbol::ST_OBJECT) return true;
if (sym->getRegion()->getMemOffset() <= sym->getOffset() &&
(sym->getRegion()->getMemOffset() + sym->getRegion()->getMemSize()) > sym->getOffset())
return true;
sym->setRegion(findEnclosingRegion(sym->getOffset()));
return true;
}
/*
* fixSymModules
*
* Add Module information to all symbols.
*/
bool Symtab::fixSymModules(std::vector<Symbol *> &raw_syms)
{
Object *obj = getObject();
if (!obj) {
return false;
}
for (auto i = indexed_modules.begin(); i != indexed_modules.end(); ++i)
{
(*i)->finalizeRanges();
}
// const std::vector<std::pair<std::string, Offset> > &mods = obj->modules_;
// for (unsigned i=0; i< mods.size(); i++) {
// getOrCreateModule(mods[i].first, mods[i].second);
// }
for (unsigned i = 0; i < raw_syms.size(); i++) {
fixSymModule(raw_syms[i]);
}
return true;
}
/*
* demangleSymbols
*
* Perform name demangling on all symbols.
*/
bool Symtab::demangleSymbols(std::vector<Symbol *> &raw_syms)
{
for (unsigned i = 0; i < raw_syms.size(); i++) {
demangleSymbol(raw_syms[i]);
}
return true;
}
/*
* createIndices
*
* We index symbols by various attributes for quick lookup. Build those
* indices here.
*/
bool Symtab::createIndices(std::vector<Symbol *> &raw_syms, bool undefined) {
for (unsigned i = 0; i < raw_syms.size(); i++) {
addSymbolToIndices(raw_syms[i], undefined);
}
return true;
}
/*
* createAggregates
*
* Frequently there will be multiple Symbols that refer to a single
* code object (e.g., function or variable). We use separate objects
* to refer to these aggregates, and build those objects here.
*/
bool Symtab::createAggregates()
{
#if !defined(os_vxworks)
// In VxWorks, symbol offsets are not complete until object is loaded.
for(auto i = everyDefinedSymbol.begin();
i != everyDefinedSymbol.end();
++i)
{
if (!doNotAggregate(*i)) {
addSymbolToAggregates(*i);
}
}
#endif
return true;
}
bool Symtab::fixSymModule(Symbol *&sym)
{
Module* mod = NULL;
findModuleByOffset(mod, sym->getOffset());
if(!mod) mod = getDefaultModule();
sym->setModule(mod);
return true;
}
bool Symtab::demangleSymbol(Symbol *&sym) {
bool typed_demangle = false;
if (sym->getType() == Symbol::ST_FUNCTION) typed_demangle = true;
// This is a bit of a hack; we're trying to demangle undefined symbols which don't necessarily
// have a ST_FUNCTION type.
if (sym->getRegion() == NULL && !sym->isAbsolute() && !sym->isCommonStorage())
typed_demangle = true;
if (typed_demangle) {
Module *rawmod = sym->getModule();
// At this point we need to generate the following information:
// A symtab name.
// A pretty (demangled) name.
// The symtab name goes in the global list as well as the module list.
// Same for the pretty name.
// Finally, check addresses to find aliases.
std::string mangled_name = sym->getMangledName();
std::string working_name = mangled_name;
#if !defined(os_windows)
//Remove extra stabs information
size_t colon = working_name.find(":");
if(colon != std::string::npos) {
working_name = working_name.substr(0, colon);
}
#endif
std::string pretty_name = working_name;
std::string typed_name = working_name;
if (!buildDemangledName(working_name, pretty_name, typed_name,
nativeCompiler, (rawmod ? rawmod->language() : lang_Unknown))) {
pretty_name = working_name;
}
//sym->prettyName_ = pretty_name;
//sym->typedName_ = typed_name;
}
else {
// All cases where there really shouldn't be a mangled
// name, since mangling is for functions.
char *prettyName = P_cplus_demangle(sym->getMangledName().c_str(), nativeCompiler, false);
if (prettyName) {
//sym->prettyName_ = std::string(prettyName);
// XXX caller-freed
free(prettyName);
}
}
return true;
}
bool Symtab::addSymbolToIndices(Symbol *&sym, bool undefined)
{
assert(sym);
if (!undefined) {
if(everyDefinedSymbol.find(sym) == everyDefinedSymbol.end())
everyDefinedSymbol.insert(sym);
}
else {
// multi-index container should handle duplication
undefDynSyms.insert(sym);
}
return true;
}
bool Symtab::addSymbolToAggregates(const Symbol *sym_tmp)
{
Symbol* sym = const_cast<Symbol*>(sym_tmp);
switch(sym->getType()) {
case Symbol::ST_FUNCTION:
case Symbol::ST_INDIRECT:
{
// We want to do the following:
// If no function exists, create and add.
// Combine this information
// Add this symbol's names to the function.
// Keep module information
Function *func = NULL;
findFuncByEntryOffset(func, sym->getOffset());
if (!func) {
// Create a new function
// Also, update the symbol to point to this function.
func = new Function(sym);
everyFunction.push_back(func);
sorted_everyFunction = false;
funcsByOffset[sym->getOffset()] = func;
}
else {
/* XXX
* For relocatable files, the offset of a symbol is relative to the
* beginning of a Region. Therefore, a symbol in a relocatable file
* is not uniquely identifiable by its offset, but it is uniquely
* identifiable by its Region and its offset.
*
* For now, do not add these functions to funcsByOffset collection.
*/
if( func->getRegion() != sym->getRegion() ) {
func = new Function(sym);
everyFunction.push_back(func);
sorted_everyFunction = false;
}
func->addSymbol(sym);
}
sym->setFunction(func);
break;
}
case Symbol::ST_TLS:
case Symbol::ST_OBJECT: {
// The same as the above, but with variables.
Variable *var = NULL;
findVariableByOffset(var, sym->getOffset());
if (!var) {
// Create a new function
// Also, update the symbol to point to this function.
var = new Variable(sym);
everyVariable.push_back(var);
varsByOffset[sym->getOffset()] = var;
}
else {
/* XXX
* For relocatable files, the offset is not a unique identifier for
* a Symbol. With functions, the Region and offset could be used to
* identify the symbol. With variables, the Region and offset may
* not uniquely identify the symbol. The only case were this occurs
* is with COMMON symbols -- their offset is their memory alignment
* and their Region is undefined. In this case, always create a
* new variable.
*/
if( obj_RelocatableFile == getObjectType() &&
( var->getRegion() != sym->getRegion() ||
NULL == sym->getRegion() ) )
{
var = new Variable(sym);
everyVariable.push_back(var);
}else{
var->addSymbol(sym);
}
}
sym->setVariable(var);
break;
}
default: {
break;
}
}
return true;
}
// A hacky override for specially treating symbols that appear
// to be functions or variables but aren't.
//
// Example: IA-32/AMD-64 libc (and others compiled with libc headers)
// uses outlined locking primitives. These are named _L_lock_<num>
// and _L_unlock_<num> and labelled as functions. We explicitly do
// not include them in function scope.
//
// Also, exclude symbols that begin with _imp_ in defensive mode.
// These symbols are entries in the IAT and shouldn't be treated
// as functions.
bool Symtab::doNotAggregate(const Symbol* sym) {
const std::string& mangled = sym->getMangledName();
if (isDefensiveBinary() && mangled.compare(0, 5, "_imp_", 5) == 0) {
return true;
}
if (mangled.compare(0, strlen("_L_lock_"), "_L_lock_") == 0) {
return true;
}
if (mangled.compare(0, strlen("_L_unlock_"), "_L_unlock_") == 0) {
return true;
}
#if 0
// Disabling as a test; this means we find _zero_ Function objects.
// PPC64 Linux symbols in the .opd section appear to be functions,
// but are not.
if (sym->getRegion() && sym->getRegion()->getRegionName() == ".opd") {
return true;
}
#endif
// return !isDefined(sym);
return false;