This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symtab.c
7175 lines (6032 loc) · 207 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-2024 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 "dwarf2/call-site.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 "gdbsupport/gdb_regex.h"
#include "expression.h"
#include "language.h"
#include "demangle.h"
#include "inferior.h"
#include "source.h"
#include "filenames.h"
#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 "cli/cli-style.h"
#include "cli/cli-cmds.h"
#include "fnmatch.h"
#include "hashtab.h"
#include "typeprint.h"
#include "gdbsupport/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 "observable.h"
#include "solist.h"
#include "macrotab.h"
#include "macroscope.h"
#include "parser-defs.h"
#include "completer.h"
#include "progspace-and-thread.h"
#include <optional>
#include "filename-seen-cache.h"
#include "arch-utils.h"
#include <algorithm>
#include <string_view>
#include "gdbsupport/pathstuff.h"
#include "gdbsupport/common-utils.h"
#include <optional>
/* Forward declarations for local functions. */
static void rbreak_command (const char *, int);
static int find_line_common (const linetable *, int, int *, int);
static struct block_symbol
lookup_symbol_aux (const char *name,
symbol_name_match_type match_type,
const struct block *block,
const domain_search_flags domain,
enum language language,
struct field_of_this_result *);
static
struct block_symbol lookup_local_symbol (const char *name,
symbol_name_match_type match_type,
const struct block *block,
const domain_search_flags domain,
enum language language);
static struct block_symbol
lookup_symbol_in_objfile (struct objfile *objfile,
enum block_enum block_index,
const char *name,
const domain_search_flags domain);
static void set_main_name (program_space *pspace, const char *name,
language lang);
/* Type of the data stored on the program space. */
struct main_info
{
/* Name of "main". */
std::string name_of_main;
/* Language of "main". */
enum language language_of_main = language_unknown;
};
/* Program space key for finding name and language of "main". */
static const registry<program_space>::key<main_info> main_progspace_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;
/* The domain that was searched for initially. This must exactly
match. */
domain_search_flags domain;
union
{
struct block_symbol found;
char *name;
} value;
};
/* Clear out SLOT. */
static void
symbol_cache_clear_slot (struct symbol_cache_slot *slot)
{
if (slot->state == SYMBOL_SLOT_NOT_FOUND)
xfree (slot->value.name);
slot->state = SYMBOL_SLOT_UNUSED;
}
/* 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];
};
/* Clear all slots of BSC and free BSC. */
static void
destroy_block_symbol_cache (struct block_symbol_cache *bsc)
{
if (bsc != nullptr)
{
for (unsigned int i = 0; i < bsc->size; i++)
symbol_cache_clear_slot (&bsc->symbols[i]);
xfree (bsc);
}
}
/* 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
{
symbol_cache () = default;
~symbol_cache ()
{
destroy_block_symbol_cache (global_symbols);
destroy_block_symbol_cache (static_symbols);
}
struct block_symbol_cache *global_symbols = nullptr;
struct block_symbol_cache *static_symbols = nullptr;
};
/* Program space key for finding its symbol cache. */
static const registry<program_space>::key<symbol_cache> symbol_cache_key;
/* 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;
/* True 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. */
bool basenames_may_differ = false;
/* 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;
/* When TRUE, ignore the prologue-end flag in linetable_entry when searching
for the SAL past a function prologue. */
static bool ignore_prologue_end_flag = false;
/* 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)
{
#define SYM_DOMAIN(X) \
case X ## _DOMAIN: return #X "_DOMAIN";
#include "sym-domains.def"
#undef SYM_DOMAIN
default: gdb_assert_not_reached ("bad domain_enum");
}
}
/* See symtab.h. */
std::string
domain_name (domain_search_flags flags)
{
static constexpr domain_search_flags::string_mapping mapping[] = {
#define SYM_DOMAIN(X) \
MAP_ENUM_FLAG (SEARCH_ ## X ## _DOMAIN),
#include "sym-domains.def"
#undef SYM_DOMAIN
};
return flags.to_string (mapping);
}
/* See symtab.h. */
domain_search_flags
from_scripting_domain (int val)
{
if ((val & SCRIPTING_SEARCH_FLAG) == 0)
{
/* VAL should be one of the domain constants. Verify this and
convert it to a search constant. */
switch (val)
{
#define SYM_DOMAIN(X) \
case X ## _DOMAIN: break;
#include "sym-domains.def"
#undef SYM_DOMAIN
default:
error (_("unrecognized domain constant"));
}
domain_search_flags result = to_search_flags ((domain_enum) val);
if (val == VAR_DOMAIN)
{
/* This matches the historical practice. */
result |= SEARCH_TYPE_DOMAIN | SEARCH_FUNCTION_DOMAIN;
}
return result;
}
else
{
/* VAL is several search constants or'd together. Verify
this. */
val &= ~SCRIPTING_SEARCH_FLAG;
int check = val;
#define SYM_DOMAIN(X) \
check &= ~ (int) SEARCH_ ## X ## _DOMAIN;
#include "sym-domains.def"
#undef SYM_DOMAIN
if (check != 0)
error (_("unrecognized domain constant"));
return (domain_search_flag) val;
}
}
/* See symtab.h. */
CORE_ADDR
linetable_entry::pc (const struct objfile *objfile) const
{
return CORE_ADDR (m_pc) + objfile->text_section_offset ();
}
/* See symtab.h. */
call_site *
compunit_symtab::find_call_site (CORE_ADDR pc) const
{
if (m_call_site_htab == nullptr)
return nullptr;
CORE_ADDR delta = this->objfile ()->text_section_offset ();
unrelocated_addr unrelocated_pc = (unrelocated_addr) (pc - delta);
struct call_site call_site_local (unrelocated_pc, nullptr, nullptr);
void **slot
= htab_find_slot (m_call_site_htab, &call_site_local, NO_INSERT);
if (slot != nullptr)
return (call_site *) *slot;
/* See if the arch knows another PC we should try. On some
platforms, GCC emits a DWARF call site that is offset from the
actual return location. */
struct gdbarch *arch = objfile ()->arch ();
CORE_ADDR new_pc = gdbarch_update_call_site_pc (arch, pc);
if (pc == new_pc)
return nullptr;
unrelocated_pc = (unrelocated_addr) (new_pc - delta);
call_site new_call_site_local (unrelocated_pc, nullptr, nullptr);
slot = htab_find_slot (m_call_site_htab, &new_call_site_local, NO_INSERT);
if (slot == nullptr)
return nullptr;
return (call_site *) *slot;
}
/* See symtab.h. */
void
compunit_symtab::set_call_site_htab (htab_t call_site_htab)
{
gdb_assert (m_call_site_htab == nullptr);
m_call_site_htab = call_site_htab;
}
/* See symtab.h. */
void
compunit_symtab::set_primary_filetab (symtab *primary_filetab)
{
symtab *prev_filetab = nullptr;
/* Move PRIMARY_FILETAB to the head of the filetab list. */
for (symtab *filetab : this->filetabs ())
{
if (filetab == primary_filetab)
{
if (prev_filetab != nullptr)
{
prev_filetab->next = primary_filetab->next;
primary_filetab->next = m_filetabs;
m_filetabs = primary_filetab;
}
break;
}
prev_filetab = filetab;
}
gdb_assert (primary_filetab == m_filetabs);
}
/* See symtab.h. */
struct symtab *
compunit_symtab::primary_filetab () const
{
gdb_assert (m_filetabs != nullptr);
/* The primary file symtab is the first one in the list. */
return m_filetabs;
}
/* See symtab.h. */
enum language
compunit_symtab::language () const
{
struct symtab *symtab = primary_filetab ();
/* The language of the compunit symtab is the language of its
primary source file. */
return symtab->language ();
}
/* The relocated address of the minimal symbol, using the section
offsets from OBJFILE. */
CORE_ADDR
minimal_symbol::value_address (objfile *objfile) const
{
if (this->maybe_copied (objfile))
return this->get_maybe_copied_address (objfile);
else
return (CORE_ADDR (this->unrelocated_address ())
+ objfile->section_offsets[this->section_index ()]);
}
/* See symtab.h. */
bool
minimal_symbol::data_p () const
{
return m_type == mst_data
|| m_type == mst_bss
|| m_type == mst_abs
|| m_type == mst_file_data
|| m_type == mst_file_bss;
}
/* See symtab.h. */
bool
minimal_symbol::text_p () const
{
return m_type == mst_text
|| m_type == mst_text_gnu_ifunc
|| m_type == mst_data_gnu_ifunc
|| m_type == mst_slot_got_plt
|| m_type == mst_solib_trampoline
|| m_type == mst_file_text;
}
/* See symtab.h. */
bool
minimal_symbol::maybe_copied (objfile *objfile) const
{
return (objfile->object_format_has_copy_relocs
&& (objfile->flags & OBJF_MAINLINE) == 0
&& (m_type == mst_data || m_type == mst_bss));
}
/* 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. */
bool
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 false;
/* The tail of FILENAME must match. */
if (FILENAME_CMP (filename + len - search_len, search_name) != 0)
return false;
/* 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]));
}
/* Same as compare_filenames_for_search, but for glob-style patterns.
Heads up on the order of the arguments. They match the order of
compare_filenames_for_search, but it's the opposite of the order of
arguments to gdb_filename_fnmatch. */
bool
compare_glob_filenames_for_search (const char *filename,
const char *search_name)
{
/* We rely on the property of glob-style patterns with FNM_FILE_NAME that
all /s have to be explicitly specified. */
int file_path_elements = count_path_elements (filename);
int search_path_elements = count_path_elements (search_name);
if (search_path_elements > file_path_elements)
return false;
if (IS_ABSOLUTE_PATH (search_name))
{
return (search_path_elements == file_path_elements
&& gdb_filename_fnmatch (search_name, filename,
FNM_FILE_NAME | FNM_NOESCAPE) == 0);
}
{
const char *file_to_compare
= strip_leading_path_elements (filename,
file_path_elements - search_path_elements);
return gdb_filename_fnmatch (search_name, file_to_compare,
FNM_FILE_NAME | FNM_NOESCAPE) == 0;
}
}
/* 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 and CALLBACK 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. */
bool
iterate_over_some_symtabs (const char *name,
const char *real_path,
struct compunit_symtab *first,
struct compunit_symtab *after_last,
gdb::function_view<bool (symtab *)> callback)
{
struct compunit_symtab *cust;
const char* base_name = lbasename (name);
for (cust = first; cust != NULL && cust != after_last; cust = cust->next)
{
/* Skip included compunits. */
if (cust->user != nullptr)
continue;
for (symtab *s : cust->filetabs ())
{
if (compare_filenames_for_search (s->filename, name))
{
if (callback (s))
return true;
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))
return true;
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));
gdb::unique_xmalloc_ptr<char> fullname_real_path
= gdb_realpath (fullname);
fullname = fullname_real_path.get ();
if (FILENAME_CMP (real_path, fullname) == 0)
{
if (callback (s))
return true;
continue;
}
}
}
}
return false;
}
/* 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. If CALLBACK returns
true, the search stops. */
void
iterate_over_symtabs (const char *name,
gdb::function_view<bool (symtab *)> callback)
{
gdb::unique_xmalloc_ptr<char> real_path;
/* Here we are interested in canonicalizing an absolute path, not
absolutizing a relative path. */
if (IS_ABSOLUTE_PATH (name))
{
real_path = gdb_realpath (name);
gdb_assert (IS_ABSOLUTE_PATH (real_path.get ()));
}
for (objfile *objfile : current_program_space->objfiles ())
{
if (iterate_over_some_symtabs (name, real_path.get (),
objfile->compunit_symtabs, NULL,
callback))
return;
}
/* Same search rules as above apply here, but now we look thru the
psymtabs. */
for (objfile *objfile : current_program_space->objfiles ())
{
if (objfile->map_symtabs_matching_filename (name, real_path.get (),
callback))
return;
}
}
/* 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, [&] (symtab *symtab)
{
result = symtab;
return true;
});
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 ();
/* 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. */
const char *const_prefix = method->is_const ? "C" : "";
const 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);
}
/* See symtab.h. */
void
general_symbol_info::set_demangled_name (const char *name,
struct obstack *obstack)
{
if (language () == language_ada)
{
if (name == NULL)
{
ada_mangled = 0;
language_specific.obstack = obstack;
}
else
{
ada_mangled = 1;
language_specific.demangled_name = name;
}
}
else
language_specific.demangled_name = name;
}
/* Initialize the language dependent portion of a symbol
depending upon the language for the symbol. */
void
general_symbol_info::set_language (enum language language,
struct obstack *obstack)
{
m_language = language;
if (language == language_cplus
|| language == language_d
|| language == language_go
|| language == language_objc
|| language == language_fortran)
{
set_demangled_name (NULL, obstack);
}
else if (language == language_ada)
{
gdb_assert (ada_mangled == 0);
language_specific.obstack = obstack;
}
else
{
memset (&language_specific, 0, sizeof (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
{
demangled_name_entry (std::string_view mangled_name)
: mangled (mangled_name) {}
std::string_view mangled;
enum language language;
gdb::unique_xmalloc_ptr<char> demangled;
};
/* Hash function for the demangled name hash. */
static hashval_t
hash_demangled_name_entry (const void *data)
{
const struct demangled_name_entry *e
= (const struct demangled_name_entry *) data;
return gdb::string_view_hash () (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
= (const struct demangled_name_entry *) a;
const struct demangled_name_entry *db
= (const struct demangled_name_entry *) b;
return da->mangled == db->mangled;
}
static void
free_demangled_name_entry (void *data)
{
struct demangled_name_entry *e
= (struct demangled_name_entry *) data;
e->~demangled_name_entry();
}
/* 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_per_bfd_storage *per_bfd)
{
/* 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. However, if the minsym count is already
initialized (e.g. because symbol name setting was deferred to
a background thread) we can initialize the hashtable with a count
based on that, because we will almost certainly have at least that
many entries. If we have a nonzero number but less than 256,
we still stay with 256 to have some space for psymbols, etc. */
/* htab will expand the table when it is 3/4th full, so we account for that
here. +2 to round up. */
int minsym_based_count = (per_bfd->minimal_symbol_count + 2) / 3 * 4;
int count = std::max (per_bfd->minimal_symbol_count, minsym_based_count);
per_bfd->demangled_names_hash.reset (htab_create_alloc
(count, hash_demangled_name_entry, eq_demangled_name_entry,
free_demangled_name_entry, xcalloc, xfree));
}
/* See symtab.h */
gdb::unique_xmalloc_ptr<char>
symbol_find_demangled_name (struct general_symbol_info *gsymbol,
const char *mangled)
{
gdb::unique_xmalloc_ptr<char> demangled;
int i;
if (gsymbol->language () != language_unknown)
{
const struct language_defn *lang = language_def (gsymbol->language ());
lang->sniff_from_mangled_name (mangled, &demangled);
return demangled;
}
for (i = language_unknown; i < nr_languages; ++i)
{
enum language l = (enum language) i;
const struct language_defn *lang = language_def (l);
if (lang->sniff_from_mangled_name (mangled, &demangled))
{
gsymbol->m_language = l;
return demangled;
}
}
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. */
void
general_symbol_info::compute_and_set_names (std::string_view linkage_name,
bool copy_name,
objfile_per_bfd_storage *per_bfd,
std::optional<hashval_t> hash)
{
struct demangled_name_entry **slot;
if (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. */
if (!copy_name)
m_name = linkage_name.data ();
else
m_name = obstack_strndup (&per_bfd->storage_obstack,
linkage_name.data (),
linkage_name.length ());