forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymtab.c
6375 lines (5355 loc) · 182 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
/* Symbol table lookup for the GNU debugger, GDB.
Copyright (C) 1986-2015 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of 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.
This program 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 a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "symtab.h"
#include "gdbtypes.h"
#include "gdbcore.h"
#include "frame.h"
#include "target.h"
#include "value.h"
#include "symfile.h"
#include "objfiles.h"
#include "gdbcmd.h"
#include "gdb_regex.h"
#include "expression.h"
#include "language.h"
#include "demangle.h"
#include "inferior.h"
#include "source.h"
#include "filenames.h" /* for FILENAME_CMP */
#include "objc-lang.h"
#include "d-lang.h"
#include "ada-lang.h"
#include "go-lang.h"
#include "p-lang.h"
#include "addrmap.h"
#include "cli/cli-utils.h"
#include "hashtab.h"
#include "gdb_obstack.h"
#include "block.h"
#include "dictionary.h"
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <ctype.h>
#include "cp-abi.h"
#include "cp-support.h"
#include "observer.h"
#include "solist.h"
#include "macrotab.h"
#include "macroscope.h"
#include "parser-defs.h"
#include "completer.h"
/* Forward declarations for local functions. */
static void rbreak_command (char *, int);
static int find_line_common (struct linetable *, int, int *, int);
static struct block_symbol
lookup_symbol_aux (const char *name,
const struct block *block,
const domain_enum domain,
enum language language,
struct field_of_this_result *);
static
struct block_symbol lookup_local_symbol (const char *name,
const struct block *block,
const domain_enum domain,
enum language language);
static struct block_symbol
lookup_symbol_in_objfile (struct objfile *objfile, int block_index,
const char *name, const domain_enum domain);
extern initialize_file_ftype _initialize_symtab;
/* Program space key for finding name and language of "main". */
static const struct program_space_data *main_progspace_key;
/* Type of the data stored on the program space. */
struct main_info
{
/* Name of "main". */
char *name_of_main;
/* Language of "main". */
enum language language_of_main;
};
/* Program space key for finding its symbol cache. */
static const struct program_space_data *symbol_cache_key;
/* The default symbol cache size.
There is no extra cpu cost for large N (except when flushing the cache,
which is rare). The value here is just a first attempt. A better default
value may be higher or lower. A prime number can make up for a bad hash
computation, so that's why the number is what it is. */
#define DEFAULT_SYMBOL_CACHE_SIZE 1021
/* The maximum symbol cache size.
There's no method to the decision of what value to use here, other than
there's no point in allowing a user typo to make gdb consume all memory. */
#define MAX_SYMBOL_CACHE_SIZE (1024*1024)
/* symbol_cache_lookup returns this if a previous lookup failed to find the
symbol in any objfile. */
#define SYMBOL_LOOKUP_FAILED \
((struct block_symbol) {(struct symbol *) 1, NULL})
#define SYMBOL_LOOKUP_FAILED_P(SIB) (SIB.symbol == (struct symbol *) 1)
/* Recording lookups that don't find the symbol is just as important, if not
more so, than recording found symbols. */
enum symbol_cache_slot_state
{
SYMBOL_SLOT_UNUSED,
SYMBOL_SLOT_NOT_FOUND,
SYMBOL_SLOT_FOUND
};
struct symbol_cache_slot
{
enum symbol_cache_slot_state state;
/* The objfile that was current when the symbol was looked up.
This is only needed for global blocks, but for simplicity's sake
we allocate the space for both. If data shows the extra space used
for static blocks is a problem, we can split things up then.
Global blocks need cache lookup to include the objfile context because
we need to account for gdbarch_iterate_over_objfiles_in_search_order
which can traverse objfiles in, effectively, any order, depending on
the current objfile, thus affecting which symbol is found. Normally,
only the current objfile is searched first, and then the rest are
searched in recorded order; but putting cache lookup inside
gdbarch_iterate_over_objfiles_in_search_order would be awkward.
Instead we just make the current objfile part of the context of
cache lookup. This means we can record the same symbol multiple times,
each with a different "current objfile" that was in effect when the
lookup was saved in the cache, but cache space is pretty cheap. */
const struct objfile *objfile_context;
union
{
struct block_symbol found;
struct
{
char *name;
domain_enum domain;
} not_found;
} value;
};
/* Symbols don't specify global vs static block.
So keep them in separate caches. */
struct block_symbol_cache
{
unsigned int hits;
unsigned int misses;
unsigned int collisions;
/* SYMBOLS is a variable length array of this size.
One can imagine that in general one cache (global/static) should be a
fraction of the size of the other, but there's no data at the moment
on which to decide. */
unsigned int size;
struct symbol_cache_slot symbols[1];
};
/* The symbol cache.
Searching for symbols in the static and global blocks over multiple objfiles
again and again can be slow, as can searching very big objfiles. This is a
simple cache to improve symbol lookup performance, which is critical to
overall gdb performance.
Symbols are hashed on the name, its domain, and block.
They are also hashed on their objfile for objfile-specific lookups. */
struct symbol_cache
{
struct block_symbol_cache *global_symbols;
struct block_symbol_cache *static_symbols;
};
/* When non-zero, print debugging messages related to symtab creation. */
unsigned int symtab_create_debug = 0;
/* When non-zero, print debugging messages related to symbol lookup. */
unsigned int symbol_lookup_debug = 0;
/* The size of the cache is staged here. */
static unsigned int new_symbol_cache_size = DEFAULT_SYMBOL_CACHE_SIZE;
/* The current value of the symbol cache size.
This is saved so that if the user enters a value too big we can restore
the original value from here. */
static unsigned int symbol_cache_size = DEFAULT_SYMBOL_CACHE_SIZE;
/* Non-zero if a file may be known by two different basenames.
This is the uncommon case, and significantly slows down gdb.
Default set to "off" to not slow down the common case. */
int basenames_may_differ = 0;
/* Allow the user to configure the debugger behavior with respect
to multiple-choice menus when more than one symbol matches during
a symbol lookup. */
const char multiple_symbols_ask[] = "ask";
const char multiple_symbols_all[] = "all";
const char multiple_symbols_cancel[] = "cancel";
static const char *const multiple_symbols_modes[] =
{
multiple_symbols_ask,
multiple_symbols_all,
multiple_symbols_cancel,
NULL
};
static const char *multiple_symbols_mode = multiple_symbols_all;
/* Read-only accessor to AUTO_SELECT_MODE. */
const char *
multiple_symbols_select_mode (void)
{
return multiple_symbols_mode;
}
/* Return the name of a domain_enum. */
const char *
domain_name (domain_enum e)
{
switch (e)
{
case UNDEF_DOMAIN: return "UNDEF_DOMAIN";
case VAR_DOMAIN: return "VAR_DOMAIN";
case STRUCT_DOMAIN: return "STRUCT_DOMAIN";
case MODULE_DOMAIN: return "MODULE_DOMAIN";
case LABEL_DOMAIN: return "LABEL_DOMAIN";
case COMMON_BLOCK_DOMAIN: return "COMMON_BLOCK_DOMAIN";
default: gdb_assert_not_reached ("bad domain_enum");
}
}
/* Return the name of a search_domain . */
const char *
search_domain_name (enum search_domain e)
{
switch (e)
{
case VARIABLES_DOMAIN: return "VARIABLES_DOMAIN";
case FUNCTIONS_DOMAIN: return "FUNCTIONS_DOMAIN";
case TYPES_DOMAIN: return "TYPES_DOMAIN";
case ALL_DOMAIN: return "ALL_DOMAIN";
default: gdb_assert_not_reached ("bad search_domain");
}
}
/* See symtab.h. */
struct symtab *
compunit_primary_filetab (const struct compunit_symtab *cust)
{
gdb_assert (COMPUNIT_FILETABS (cust) != NULL);
/* The primary file symtab is the first one in the list. */
return COMPUNIT_FILETABS (cust);
}
/* See symtab.h. */
enum language
compunit_language (const struct compunit_symtab *cust)
{
struct symtab *symtab = compunit_primary_filetab (cust);
/* The language of the compunit symtab is the language of its primary
source file. */
return SYMTAB_LANGUAGE (symtab);
}
/* See whether FILENAME matches SEARCH_NAME using the rule that we
advertise to the user. (The manual's description of linespecs
describes what we advertise). Returns true if they match, false
otherwise. */
int
compare_filenames_for_search (const char *filename, const char *search_name)
{
int len = strlen (filename);
size_t search_len = strlen (search_name);
if (len < search_len)
return 0;
/* The tail of FILENAME must match. */
if (FILENAME_CMP (filename + len - search_len, search_name) != 0)
return 0;
/* Either the names must completely match, or the character
preceding the trailing SEARCH_NAME segment of FILENAME must be a
directory separator.
The check !IS_ABSOLUTE_PATH ensures SEARCH_NAME "/dir/file.c"
cannot match FILENAME "/path//dir/file.c" - as user has requested
absolute path. The sama applies for "c:\file.c" possibly
incorrectly hypothetically matching "d:\dir\c:\file.c".
The HAS_DRIVE_SPEC purpose is to make FILENAME "c:file.c"
compatible with SEARCH_NAME "file.c". In such case a compiler had
to put the "c:file.c" name into debug info. Such compatibility
works only on GDB built for DOS host. */
return (len == search_len
|| (!IS_ABSOLUTE_PATH (search_name)
&& IS_DIR_SEPARATOR (filename[len - search_len - 1]))
|| (HAS_DRIVE_SPEC (filename)
&& STRIP_DRIVE_SPEC (filename) == &filename[len - search_len]));
}
/* Check for a symtab of a specific name by searching some symtabs.
This is a helper function for callbacks of iterate_over_symtabs.
If NAME is not absolute, then REAL_PATH is NULL
If NAME is absolute, then REAL_PATH is the gdb_realpath form of NAME.
The return value, NAME, REAL_PATH, CALLBACK, and DATA
are identical to the `map_symtabs_matching_filename' method of
quick_symbol_functions.
FIRST and AFTER_LAST indicate the range of compunit symtabs to search.
Each symtab within the specified compunit symtab is also searched.
AFTER_LAST is one past the last compunit symtab to search; NULL means to
search until the end of the list. */
int
iterate_over_some_symtabs (const char *name,
const char *real_path,
int (*callback) (struct symtab *symtab,
void *data),
void *data,
struct compunit_symtab *first,
struct compunit_symtab *after_last)
{
struct compunit_symtab *cust;
struct symtab *s;
const char* base_name = lbasename (name);
for (cust = first; cust != NULL && cust != after_last; cust = cust->next)
{
ALL_COMPUNIT_FILETABS (cust, s)
{
if (compare_filenames_for_search (s->filename, name))
{
if (callback (s, data))
return 1;
continue;
}
/* Before we invoke realpath, which can get expensive when many
files are involved, do a quick comparison of the basenames. */
if (! basenames_may_differ
&& FILENAME_CMP (base_name, lbasename (s->filename)) != 0)
continue;
if (compare_filenames_for_search (symtab_to_fullname (s), name))
{
if (callback (s, data))
return 1;
continue;
}
/* If the user gave us an absolute path, try to find the file in
this symtab and use its absolute path. */
if (real_path != NULL)
{
const char *fullname = symtab_to_fullname (s);
gdb_assert (IS_ABSOLUTE_PATH (real_path));
gdb_assert (IS_ABSOLUTE_PATH (name));
if (FILENAME_CMP (real_path, fullname) == 0)
{
if (callback (s, data))
return 1;
continue;
}
}
}
}
return 0;
}
/* Check for a symtab of a specific name; first in symtabs, then in
psymtabs. *If* there is no '/' in the name, a match after a '/'
in the symtab filename will also work.
Calls CALLBACK with each symtab that is found and with the supplied
DATA. If CALLBACK returns true, the search stops. */
void
iterate_over_symtabs (const char *name,
int (*callback) (struct symtab *symtab,
void *data),
void *data)
{
struct objfile *objfile;
char *real_path = NULL;
struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
/* Here we are interested in canonicalizing an absolute path, not
absolutizing a relative path. */
if (IS_ABSOLUTE_PATH (name))
{
real_path = gdb_realpath (name);
make_cleanup (xfree, real_path);
gdb_assert (IS_ABSOLUTE_PATH (real_path));
}
ALL_OBJFILES (objfile)
{
if (iterate_over_some_symtabs (name, real_path, callback, data,
objfile->compunit_symtabs, NULL))
{
do_cleanups (cleanups);
return;
}
}
/* Same search rules as above apply here, but now we look thru the
psymtabs. */
ALL_OBJFILES (objfile)
{
if (objfile->sf
&& objfile->sf->qf->map_symtabs_matching_filename (objfile,
name,
real_path,
callback,
data))
{
do_cleanups (cleanups);
return;
}
}
do_cleanups (cleanups);
}
/* The callback function used by lookup_symtab. */
static int
lookup_symtab_callback (struct symtab *symtab, void *data)
{
struct symtab **result_ptr = data;
*result_ptr = symtab;
return 1;
}
/* A wrapper for iterate_over_symtabs that returns the first matching
symtab, or NULL. */
struct symtab *
lookup_symtab (const char *name)
{
struct symtab *result = NULL;
iterate_over_symtabs (name, lookup_symtab_callback, &result);
return result;
}
/* Mangle a GDB method stub type. This actually reassembles the pieces of the
full method name, which consist of the class name (from T), the unadorned
method name from METHOD_ID, and the signature for the specific overload,
specified by SIGNATURE_ID. Note that this function is g++ specific. */
char *
gdb_mangle_name (struct type *type, int method_id, int signature_id)
{
int mangled_name_len;
char *mangled_name;
struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
struct fn_field *method = &f[signature_id];
const char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
const char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
const char *newname = type_name_no_tag (type);
/* Does the form of physname indicate that it is the full mangled name
of a constructor (not just the args)? */
int is_full_physname_constructor;
int is_constructor;
int is_destructor = is_destructor_name (physname);
/* Need a new type prefix. */
char *const_prefix = method->is_const ? "C" : "";
char *volatile_prefix = method->is_volatile ? "V" : "";
char buf[20];
int len = (newname == NULL ? 0 : strlen (newname));
/* Nothing to do if physname already contains a fully mangled v3 abi name
or an operator name. */
if ((physname[0] == '_' && physname[1] == 'Z')
|| is_operator_name (field_name))
return xstrdup (physname);
is_full_physname_constructor = is_constructor_name (physname);
is_constructor = is_full_physname_constructor
|| (newname && strcmp (field_name, newname) == 0);
if (!is_destructor)
is_destructor = (startswith (physname, "__dt"));
if (is_destructor || is_full_physname_constructor)
{
mangled_name = (char *) xmalloc (strlen (physname) + 1);
strcpy (mangled_name, physname);
return mangled_name;
}
if (len == 0)
{
xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
}
else if (physname[0] == 't' || physname[0] == 'Q')
{
/* The physname for template and qualified methods already includes
the class name. */
xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
newname = NULL;
len = 0;
}
else
{
xsnprintf (buf, sizeof (buf), "__%s%s%d", const_prefix,
volatile_prefix, len);
}
mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
+ strlen (buf) + len + strlen (physname) + 1);
mangled_name = (char *) xmalloc (mangled_name_len);
if (is_constructor)
mangled_name[0] = '\0';
else
strcpy (mangled_name, field_name);
strcat (mangled_name, buf);
/* If the class doesn't have a name, i.e. newname NULL, then we just
mangle it using 0 for the length of the class. Thus it gets mangled
as something starting with `::' rather than `classname::'. */
if (newname != NULL)
strcat (mangled_name, newname);
strcat (mangled_name, physname);
return (mangled_name);
}
/* Set the demangled name of GSYMBOL to NAME. NAME must be already
correctly allocated. */
void
symbol_set_demangled_name (struct general_symbol_info *gsymbol,
const char *name,
struct obstack *obstack)
{
if (gsymbol->language == language_ada)
{
if (name == NULL)
{
gsymbol->ada_mangled = 0;
gsymbol->language_specific.obstack = obstack;
}
else
{
gsymbol->ada_mangled = 1;
gsymbol->language_specific.mangled_lang.demangled_name = name;
}
}
else
gsymbol->language_specific.mangled_lang.demangled_name = name;
}
/* Return the demangled name of GSYMBOL. */
const char *
symbol_get_demangled_name (const struct general_symbol_info *gsymbol)
{
if (gsymbol->language == language_ada)
{
if (!gsymbol->ada_mangled)
return NULL;
/* Fall through. */
}
return gsymbol->language_specific.mangled_lang.demangled_name;
}
/* Initialize the language dependent portion of a symbol
depending upon the language for the symbol. */
void
symbol_set_language (struct general_symbol_info *gsymbol,
enum language language,
struct obstack *obstack)
{
gsymbol->language = language;
if (gsymbol->language == language_cplus
|| gsymbol->language == language_d
|| gsymbol->language == language_go
|| gsymbol->language == language_java
|| gsymbol->language == language_objc
|| gsymbol->language == language_fortran)
{
symbol_set_demangled_name (gsymbol, NULL, obstack);
}
else if (gsymbol->language == language_ada)
{
gdb_assert (gsymbol->ada_mangled == 0);
gsymbol->language_specific.obstack = obstack;
}
else
{
memset (&gsymbol->language_specific, 0,
sizeof (gsymbol->language_specific));
}
}
/* Functions to initialize a symbol's mangled name. */
/* Objects of this type are stored in the demangled name hash table. */
struct demangled_name_entry
{
const char *mangled;
char demangled[1];
};
/* Hash function for the demangled name hash. */
static hashval_t
hash_demangled_name_entry (const void *data)
{
const struct demangled_name_entry *e = data;
return htab_hash_string (e->mangled);
}
/* Equality function for the demangled name hash. */
static int
eq_demangled_name_entry (const void *a, const void *b)
{
const struct demangled_name_entry *da = a;
const struct demangled_name_entry *db = b;
return strcmp (da->mangled, db->mangled) == 0;
}
/* Create the hash table used for demangled names. Each hash entry is
a pair of strings; one for the mangled name and one for the demangled
name. The entry is hashed via just the mangled name. */
static void
create_demangled_names_hash (struct objfile *objfile)
{
/* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
The hash table code will round this up to the next prime number.
Choosing a much larger table size wastes memory, and saves only about
1% in symbol reading. */
objfile->per_bfd->demangled_names_hash = htab_create_alloc
(256, hash_demangled_name_entry, eq_demangled_name_entry,
NULL, xcalloc, xfree);
}
/* Try to determine the demangled name for a symbol, based on the
language of that symbol. If the language is set to language_auto,
it will attempt to find any demangling algorithm that works and
then set the language appropriately. The returned name is allocated
by the demangler and should be xfree'd. */
static char *
symbol_find_demangled_name (struct general_symbol_info *gsymbol,
const char *mangled)
{
char *demangled = NULL;
if (gsymbol->language == language_unknown)
gsymbol->language = language_auto;
if (gsymbol->language == language_objc
|| gsymbol->language == language_auto)
{
demangled =
objc_demangle (mangled, 0);
if (demangled != NULL)
{
gsymbol->language = language_objc;
return demangled;
}
}
if (gsymbol->language == language_cplus
|| gsymbol->language == language_auto)
{
demangled =
gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
if (demangled != NULL)
{
gsymbol->language = language_cplus;
return demangled;
}
}
if (gsymbol->language == language_java)
{
demangled =
gdb_demangle (mangled,
DMGL_PARAMS | DMGL_ANSI | DMGL_JAVA);
if (demangled != NULL)
{
gsymbol->language = language_java;
return demangled;
}
}
if (gsymbol->language == language_d
|| gsymbol->language == language_auto)
{
demangled = d_demangle(mangled, 0);
if (demangled != NULL)
{
gsymbol->language = language_d;
return demangled;
}
}
/* FIXME(dje): Continually adding languages here is clumsy.
Better to just call la_demangle if !auto, and if auto then call
a utility routine that tries successive languages in turn and reports
which one it finds. I realize the la_demangle options may be different
for different languages but there's already a FIXME for that. */
if (gsymbol->language == language_go
|| gsymbol->language == language_auto)
{
demangled = go_demangle (mangled, 0);
if (demangled != NULL)
{
gsymbol->language = language_go;
return demangled;
}
}
/* We could support `gsymbol->language == language_fortran' here to provide
module namespaces also for inferiors with only minimal symbol table (ELF
symbols). Just the mangling standard is not standardized across compilers
and there is no DW_AT_producer available for inferiors with only the ELF
symbols to check the mangling kind. */
/* Check for Ada symbols last. See comment below explaining why. */
if (gsymbol->language == language_auto)
{
const char *demangled = ada_decode (mangled);
if (demangled != mangled && demangled != NULL && demangled[0] != '<')
{
/* Set the gsymbol language to Ada, but still return NULL.
Two reasons for that:
1. For Ada, we prefer computing the symbol's decoded name
on the fly rather than pre-compute it, in order to save
memory (Ada projects are typically very large).
2. There are some areas in the definition of the GNAT
encoding where, with a bit of bad luck, we might be able
to decode a non-Ada symbol, generating an incorrect
demangled name (Eg: names ending with "TB" for instance
are identified as task bodies and so stripped from
the decoded name returned).
Returning NULL, here, helps us get a little bit of
the best of both worlds. Because we're last, we should
not affect any of the other languages that were able to
demangle the symbol before us; we get to correctly tag
Ada symbols as such; and even if we incorrectly tagged
a non-Ada symbol, which should be rare, any routing
through the Ada language should be transparent (Ada
tries to behave much like C/C++ with non-Ada symbols). */
gsymbol->language = language_ada;
return NULL;
}
}
return NULL;
}
/* Set both the mangled and demangled (if any) names for GSYMBOL based
on LINKAGE_NAME and LEN. Ordinarily, NAME is copied onto the
objfile's obstack; but if COPY_NAME is 0 and if NAME is
NUL-terminated, then this function assumes that NAME is already
correctly saved (either permanently or with a lifetime tied to the
objfile), and it will not be copied.
The hash table corresponding to OBJFILE is used, and the memory
comes from the per-BFD storage_obstack. LINKAGE_NAME is copied,
so the pointer can be discarded after calling this function. */
/* We have to be careful when dealing with Java names: when we run
into a Java minimal symbol, we don't know it's a Java symbol, so it
gets demangled as a C++ name. This is unfortunate, but there's not
much we can do about it: but when demangling partial symbols and
regular symbols, we'd better not reuse the wrong demangled name.
(See PR gdb/1039.) We solve this by putting a distinctive prefix
on Java names when storing them in the hash table. */
/* FIXME: carlton/2003-03-13: This is an unfortunate situation. I
don't mind the Java prefix so much: different languages have
different demangling requirements, so it's only natural that we
need to keep language data around in our demangling cache. But
it's not good that the minimal symbol has the wrong demangled name.
Unfortunately, I can't think of any easy solution to that
problem. */
#define JAVA_PREFIX "##JAVA$$"
#define JAVA_PREFIX_LEN 8
void
symbol_set_names (struct general_symbol_info *gsymbol,
const char *linkage_name, int len, int copy_name,
struct objfile *objfile)
{
struct demangled_name_entry **slot;
/* A 0-terminated copy of the linkage name. */
const char *linkage_name_copy;
/* A copy of the linkage name that might have a special Java prefix
added to it, for use when looking names up in the hash table. */
const char *lookup_name;
/* The length of lookup_name. */
int lookup_len;
struct demangled_name_entry entry;
struct objfile_per_bfd_storage *per_bfd = objfile->per_bfd;
if (gsymbol->language == language_ada)
{
/* In Ada, we do the symbol lookups using the mangled name, so
we can save some space by not storing the demangled name.
As a side note, we have also observed some overlap between
the C++ mangling and Ada mangling, similarly to what has
been observed with Java. Because we don't store the demangled
name with the symbol, we don't need to use the same trick
as Java. */
if (!copy_name)
gsymbol->name = linkage_name;
else
{
char *name = obstack_alloc (&per_bfd->storage_obstack, len + 1);
memcpy (name, linkage_name, len);
name[len] = '\0';
gsymbol->name = name;
}
symbol_set_demangled_name (gsymbol, NULL, &per_bfd->storage_obstack);
return;
}
if (per_bfd->demangled_names_hash == NULL)
create_demangled_names_hash (objfile);
/* The stabs reader generally provides names that are not
NUL-terminated; most of the other readers don't do this, so we
can just use the given copy, unless we're in the Java case. */
if (gsymbol->language == language_java)
{
char *alloc_name;
lookup_len = len + JAVA_PREFIX_LEN;
alloc_name = alloca (lookup_len + 1);
memcpy (alloc_name, JAVA_PREFIX, JAVA_PREFIX_LEN);
memcpy (alloc_name + JAVA_PREFIX_LEN, linkage_name, len);
alloc_name[lookup_len] = '\0';
lookup_name = alloc_name;
linkage_name_copy = alloc_name + JAVA_PREFIX_LEN;
}
else if (linkage_name[len] != '\0')
{
char *alloc_name;
lookup_len = len;
alloc_name = alloca (lookup_len + 1);
memcpy (alloc_name, linkage_name, len);
alloc_name[lookup_len] = '\0';
lookup_name = alloc_name;
linkage_name_copy = alloc_name;
}
else
{
lookup_len = len;
lookup_name = linkage_name;
linkage_name_copy = linkage_name;
}
entry.mangled = lookup_name;
slot = ((struct demangled_name_entry **)
htab_find_slot (per_bfd->demangled_names_hash,
&entry, INSERT));
/* If this name is not in the hash table, add it. */
if (*slot == NULL
/* A C version of the symbol may have already snuck into the table.
This happens to, e.g., main.init (__go_init_main). Cope. */
|| (gsymbol->language == language_go
&& (*slot)->demangled[0] == '\0'))
{
char *demangled_name = symbol_find_demangled_name (gsymbol,
linkage_name_copy);
int demangled_len = demangled_name ? strlen (demangled_name) : 0;
/* Suppose we have demangled_name==NULL, copy_name==0, and
lookup_name==linkage_name. In this case, we already have the
mangled name saved, and we don't have a demangled name. So,
you might think we could save a little space by not recording
this in the hash table at all.
It turns out that it is actually important to still save such
an entry in the hash table, because storing this name gives
us better bcache hit rates for partial symbols. */
if (!copy_name && lookup_name == linkage_name)
{
*slot = obstack_alloc (&per_bfd->storage_obstack,
offsetof (struct demangled_name_entry,
demangled)
+ demangled_len + 1);
(*slot)->mangled = lookup_name;
}
else
{
char *mangled_ptr;
/* If we must copy the mangled name, put it directly after
the demangled name so we can have a single
allocation. */
*slot = obstack_alloc (&per_bfd->storage_obstack,
offsetof (struct demangled_name_entry,
demangled)
+ lookup_len + demangled_len + 2);
mangled_ptr = &((*slot)->demangled[demangled_len + 1]);
strcpy (mangled_ptr, lookup_name);
(*slot)->mangled = mangled_ptr;
}
if (demangled_name != NULL)
{
strcpy ((*slot)->demangled, demangled_name);
xfree (demangled_name);
}
else
(*slot)->demangled[0] = '\0';
}
gsymbol->name = (*slot)->mangled + lookup_len - len;
if ((*slot)->demangled[0] != '\0')
symbol_set_demangled_name (gsymbol, (*slot)->demangled,
&per_bfd->storage_obstack);
else
symbol_set_demangled_name (gsymbol, NULL, &per_bfd->storage_obstack);
}
/* Return the source code name of a symbol. In languages where
demangling is necessary, this is the demangled name. */
const char *
symbol_natural_name (const struct general_symbol_info *gsymbol)
{
switch (gsymbol->language)
{
case language_cplus: