minsyms.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  1. /* GDB routines for manipulating the minimal symbol tables.
  2. Copyright (C) 1992-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Support, using pieces from other GDB modules.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This file contains support routines for creating, manipulating, and
  16. destroying minimal symbol tables.
  17. Minimal symbol tables are used to hold some very basic information about
  18. all defined global symbols (text, data, bss, abs, etc). The only two
  19. required pieces of information are the symbol's name and the address
  20. associated with that symbol.
  21. In many cases, even if a file was compiled with no special options for
  22. debugging at all, as long as was not stripped it will contain sufficient
  23. information to build useful minimal symbol tables using this structure.
  24. Even when a file contains enough debugging information to build a full
  25. symbol table, these minimal symbols are still useful for quickly mapping
  26. between names and addresses, and vice versa. They are also sometimes used
  27. to figure out what full symbol table entries need to be read in. */
  28. #include "defs.h"
  29. #include <ctype.h>
  30. #include "symtab.h"
  31. #include "bfd.h"
  32. #include "filenames.h"
  33. #include "symfile.h"
  34. #include "objfiles.h"
  35. #include "demangle.h"
  36. #include "value.h"
  37. #include "cp-abi.h"
  38. #include "target.h"
  39. #include "cp-support.h"
  40. #include "language.h"
  41. #include "cli/cli-utils.h"
  42. #include "gdbsupport/symbol.h"
  43. #include <algorithm>
  44. #include "safe-ctype.h"
  45. #include "gdbsupport/parallel-for.h"
  46. #include "inferior.h"
  47. #if CXX_STD_THREAD
  48. #include <mutex>
  49. #endif
  50. /* Return true if MINSYM is a cold clone symbol.
  51. Recognize f.i. these symbols (mangled/demangled):
  52. - _ZL3foov.cold
  53. foo() [clone .cold]
  54. - _ZL9do_rpo_vnP8functionP8edge_defP11bitmap_headbb.cold.138
  55. do_rpo_vn(function*, edge_def*, bitmap_head*, bool, bool) \
  56. [clone .cold.138]. */
  57. static bool
  58. msymbol_is_cold_clone (minimal_symbol *minsym)
  59. {
  60. const char *name = minsym->natural_name ();
  61. size_t name_len = strlen (name);
  62. if (name_len < 1)
  63. return false;
  64. const char *last = &name[name_len - 1];
  65. if (*last != ']')
  66. return false;
  67. const char *suffix = " [clone .cold";
  68. size_t suffix_len = strlen (suffix);
  69. const char *found = strstr (name, suffix);
  70. if (found == nullptr)
  71. return false;
  72. const char *start = &found[suffix_len];
  73. if (*start == ']')
  74. return true;
  75. if (*start != '.')
  76. return false;
  77. const char *p;
  78. for (p = start + 1; p <= last; ++p)
  79. {
  80. if (*p >= '0' && *p <= '9')
  81. continue;
  82. break;
  83. }
  84. if (p == last)
  85. return true;
  86. return false;
  87. }
  88. /* See minsyms.h. */
  89. bool
  90. msymbol_is_function (struct objfile *objfile, minimal_symbol *minsym,
  91. CORE_ADDR *func_address_p)
  92. {
  93. CORE_ADDR msym_addr = MSYMBOL_VALUE_ADDRESS (objfile, minsym);
  94. switch (minsym->type)
  95. {
  96. case mst_slot_got_plt:
  97. case mst_data:
  98. case mst_bss:
  99. case mst_abs:
  100. case mst_file_data:
  101. case mst_file_bss:
  102. case mst_data_gnu_ifunc:
  103. {
  104. struct gdbarch *gdbarch = objfile->arch ();
  105. CORE_ADDR pc = gdbarch_convert_from_func_ptr_addr
  106. (gdbarch, msym_addr, current_inferior ()->top_target ());
  107. if (pc != msym_addr)
  108. {
  109. if (func_address_p != NULL)
  110. *func_address_p = pc;
  111. return true;
  112. }
  113. return false;
  114. }
  115. case mst_file_text:
  116. /* Ignore function symbol that is not a function entry. */
  117. if (msymbol_is_cold_clone (minsym))
  118. return false;
  119. /* fallthru */
  120. default:
  121. if (func_address_p != NULL)
  122. *func_address_p = msym_addr;
  123. return true;
  124. }
  125. }
  126. /* Accumulate the minimal symbols for each objfile in bunches of BUNCH_SIZE.
  127. At the end, copy them all into one newly allocated array. */
  128. #define BUNCH_SIZE 127
  129. struct msym_bunch
  130. {
  131. struct msym_bunch *next;
  132. struct minimal_symbol contents[BUNCH_SIZE];
  133. };
  134. /* See minsyms.h. */
  135. unsigned int
  136. msymbol_hash_iw (const char *string)
  137. {
  138. unsigned int hash = 0;
  139. while (*string && *string != '(')
  140. {
  141. string = skip_spaces (string);
  142. if (*string && *string != '(')
  143. {
  144. hash = SYMBOL_HASH_NEXT (hash, *string);
  145. ++string;
  146. }
  147. }
  148. return hash;
  149. }
  150. /* See minsyms.h. */
  151. unsigned int
  152. msymbol_hash (const char *string)
  153. {
  154. unsigned int hash = 0;
  155. for (; *string; ++string)
  156. hash = SYMBOL_HASH_NEXT (hash, *string);
  157. return hash;
  158. }
  159. /* Add the minimal symbol SYM to an objfile's minsym hash table, TABLE. */
  160. static void
  161. add_minsym_to_hash_table (struct minimal_symbol *sym,
  162. struct minimal_symbol **table,
  163. unsigned int hash_value)
  164. {
  165. if (sym->hash_next == NULL)
  166. {
  167. unsigned int hash = hash_value % MINIMAL_SYMBOL_HASH_SIZE;
  168. sym->hash_next = table[hash];
  169. table[hash] = sym;
  170. }
  171. }
  172. /* Add the minimal symbol SYM to an objfile's minsym demangled hash table,
  173. TABLE. */
  174. static void
  175. add_minsym_to_demangled_hash_table (struct minimal_symbol *sym,
  176. struct objfile *objfile,
  177. unsigned int hash_value)
  178. {
  179. if (sym->demangled_hash_next == NULL)
  180. {
  181. objfile->per_bfd->demangled_hash_languages.set (sym->language ());
  182. struct minimal_symbol **table
  183. = objfile->per_bfd->msymbol_demangled_hash;
  184. unsigned int hash_index = hash_value % MINIMAL_SYMBOL_HASH_SIZE;
  185. sym->demangled_hash_next = table[hash_index];
  186. table[hash_index] = sym;
  187. }
  188. }
  189. /* Worker object for lookup_minimal_symbol. Stores temporary results
  190. while walking the symbol tables. */
  191. struct found_minimal_symbols
  192. {
  193. /* External symbols are best. */
  194. bound_minimal_symbol external_symbol;
  195. /* File-local symbols are next best. */
  196. bound_minimal_symbol file_symbol;
  197. /* Symbols for shared library trampolines are next best. */
  198. bound_minimal_symbol trampoline_symbol;
  199. /* Called when a symbol name matches. Check if the minsym is a
  200. better type than what we had already found, and record it in one
  201. of the members fields if so. Returns true if we collected the
  202. real symbol, in which case we can stop searching. */
  203. bool maybe_collect (const char *sfile, objfile *objf,
  204. minimal_symbol *msymbol);
  205. };
  206. /* See declaration above. */
  207. bool
  208. found_minimal_symbols::maybe_collect (const char *sfile,
  209. struct objfile *objfile,
  210. minimal_symbol *msymbol)
  211. {
  212. switch (MSYMBOL_TYPE (msymbol))
  213. {
  214. case mst_file_text:
  215. case mst_file_data:
  216. case mst_file_bss:
  217. if (sfile == NULL
  218. || filename_cmp (msymbol->filename, sfile) == 0)
  219. {
  220. file_symbol.minsym = msymbol;
  221. file_symbol.objfile = objfile;
  222. }
  223. break;
  224. case mst_solib_trampoline:
  225. /* If a trampoline symbol is found, we prefer to keep
  226. looking for the *real* symbol. If the actual symbol
  227. is not found, then we'll use the trampoline
  228. entry. */
  229. if (trampoline_symbol.minsym == NULL)
  230. {
  231. trampoline_symbol.minsym = msymbol;
  232. trampoline_symbol.objfile = objfile;
  233. }
  234. break;
  235. case mst_unknown:
  236. default:
  237. external_symbol.minsym = msymbol;
  238. external_symbol.objfile = objfile;
  239. /* We have the real symbol. No use looking further. */
  240. return true;
  241. }
  242. /* Keep looking. */
  243. return false;
  244. }
  245. /* Walk the mangled name hash table, and pass each symbol whose name
  246. matches LOOKUP_NAME according to NAMECMP to FOUND. */
  247. static void
  248. lookup_minimal_symbol_mangled (const char *lookup_name,
  249. const char *sfile,
  250. struct objfile *objfile,
  251. struct minimal_symbol **table,
  252. unsigned int hash,
  253. int (*namecmp) (const char *, const char *),
  254. found_minimal_symbols &found)
  255. {
  256. for (minimal_symbol *msymbol = table[hash];
  257. msymbol != NULL;
  258. msymbol = msymbol->hash_next)
  259. {
  260. const char *symbol_name = msymbol->linkage_name ();
  261. if (namecmp (symbol_name, lookup_name) == 0
  262. && found.maybe_collect (sfile, objfile, msymbol))
  263. return;
  264. }
  265. }
  266. /* Walk the demangled name hash table, and pass each symbol whose name
  267. matches LOOKUP_NAME according to MATCHER to FOUND. */
  268. static void
  269. lookup_minimal_symbol_demangled (const lookup_name_info &lookup_name,
  270. const char *sfile,
  271. struct objfile *objfile,
  272. struct minimal_symbol **table,
  273. unsigned int hash,
  274. symbol_name_matcher_ftype *matcher,
  275. found_minimal_symbols &found)
  276. {
  277. for (minimal_symbol *msymbol = table[hash];
  278. msymbol != NULL;
  279. msymbol = msymbol->demangled_hash_next)
  280. {
  281. const char *symbol_name = msymbol->search_name ();
  282. if (matcher (symbol_name, lookup_name, NULL)
  283. && found.maybe_collect (sfile, objfile, msymbol))
  284. return;
  285. }
  286. }
  287. /* Look through all the current minimal symbol tables and find the
  288. first minimal symbol that matches NAME. If OBJF is non-NULL, limit
  289. the search to that objfile. If SFILE is non-NULL, the only file-scope
  290. symbols considered will be from that source file (global symbols are
  291. still preferred). Returns a pointer to the minimal symbol that
  292. matches, or NULL if no match is found.
  293. Note: One instance where there may be duplicate minimal symbols with
  294. the same name is when the symbol tables for a shared library and the
  295. symbol tables for an executable contain global symbols with the same
  296. names (the dynamic linker deals with the duplication).
  297. It's also possible to have minimal symbols with different mangled
  298. names, but identical demangled names. For example, the GNU C++ v3
  299. ABI requires the generation of two (or perhaps three) copies of
  300. constructor functions --- "in-charge", "not-in-charge", and
  301. "allocate" copies; destructors may be duplicated as well.
  302. Obviously, there must be distinct mangled names for each of these,
  303. but the demangled names are all the same: S::S or S::~S. */
  304. struct bound_minimal_symbol
  305. lookup_minimal_symbol (const char *name, const char *sfile,
  306. struct objfile *objf)
  307. {
  308. found_minimal_symbols found;
  309. unsigned int mangled_hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
  310. auto *mangled_cmp
  311. = (case_sensitivity == case_sensitive_on
  312. ? strcmp
  313. : strcasecmp);
  314. if (sfile != NULL)
  315. sfile = lbasename (sfile);
  316. lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
  317. for (objfile *objfile : current_program_space->objfiles ())
  318. {
  319. if (found.external_symbol.minsym != NULL)
  320. break;
  321. if (objf == NULL || objf == objfile
  322. || objf == objfile->separate_debug_objfile_backlink)
  323. {
  324. if (symbol_lookup_debug)
  325. {
  326. gdb_printf (gdb_stdlog,
  327. "lookup_minimal_symbol (%s, %s, %s)\n",
  328. name, sfile != NULL ? sfile : "NULL",
  329. objfile_debug_name (objfile));
  330. }
  331. /* Do two passes: the first over the ordinary hash table,
  332. and the second over the demangled hash table. */
  333. lookup_minimal_symbol_mangled (name, sfile, objfile,
  334. objfile->per_bfd->msymbol_hash,
  335. mangled_hash, mangled_cmp, found);
  336. /* If not found, try the demangled hash table. */
  337. if (found.external_symbol.minsym == NULL)
  338. {
  339. /* Once for each language in the demangled hash names
  340. table (usually just zero or one languages). */
  341. for (unsigned iter = 0; iter < nr_languages; ++iter)
  342. {
  343. if (!objfile->per_bfd->demangled_hash_languages.test (iter))
  344. continue;
  345. enum language lang = (enum language) iter;
  346. unsigned int hash
  347. = (lookup_name.search_name_hash (lang)
  348. % MINIMAL_SYMBOL_HASH_SIZE);
  349. symbol_name_matcher_ftype *match
  350. = language_def (lang)->get_symbol_name_matcher
  351. (lookup_name);
  352. struct minimal_symbol **msymbol_demangled_hash
  353. = objfile->per_bfd->msymbol_demangled_hash;
  354. lookup_minimal_symbol_demangled (lookup_name, sfile, objfile,
  355. msymbol_demangled_hash,
  356. hash, match, found);
  357. if (found.external_symbol.minsym != NULL)
  358. break;
  359. }
  360. }
  361. }
  362. }
  363. /* External symbols are best. */
  364. if (found.external_symbol.minsym != NULL)
  365. {
  366. if (symbol_lookup_debug)
  367. {
  368. minimal_symbol *minsym = found.external_symbol.minsym;
  369. gdb_printf (gdb_stdlog,
  370. "lookup_minimal_symbol (...) = %s (external)\n",
  371. host_address_to_string (minsym));
  372. }
  373. return found.external_symbol;
  374. }
  375. /* File-local symbols are next best. */
  376. if (found.file_symbol.minsym != NULL)
  377. {
  378. if (symbol_lookup_debug)
  379. {
  380. minimal_symbol *minsym = found.file_symbol.minsym;
  381. gdb_printf (gdb_stdlog,
  382. "lookup_minimal_symbol (...) = %s (file-local)\n",
  383. host_address_to_string (minsym));
  384. }
  385. return found.file_symbol;
  386. }
  387. /* Symbols for shared library trampolines are next best. */
  388. if (found.trampoline_symbol.minsym != NULL)
  389. {
  390. if (symbol_lookup_debug)
  391. {
  392. minimal_symbol *minsym = found.trampoline_symbol.minsym;
  393. gdb_printf (gdb_stdlog,
  394. "lookup_minimal_symbol (...) = %s (trampoline)\n",
  395. host_address_to_string (minsym));
  396. }
  397. return found.trampoline_symbol;
  398. }
  399. /* Not found. */
  400. if (symbol_lookup_debug)
  401. gdb_printf (gdb_stdlog, "lookup_minimal_symbol (...) = NULL\n");
  402. return {};
  403. }
  404. /* See minsyms.h. */
  405. struct bound_minimal_symbol
  406. lookup_bound_minimal_symbol (const char *name)
  407. {
  408. return lookup_minimal_symbol (name, NULL, NULL);
  409. }
  410. /* See gdbsupport/symbol.h. */
  411. int
  412. find_minimal_symbol_address (const char *name, CORE_ADDR *addr,
  413. struct objfile *objfile)
  414. {
  415. struct bound_minimal_symbol sym
  416. = lookup_minimal_symbol (name, NULL, objfile);
  417. if (sym.minsym != NULL)
  418. *addr = BMSYMBOL_VALUE_ADDRESS (sym);
  419. return sym.minsym == NULL;
  420. }
  421. /* Get the lookup name form best suitable for linkage name
  422. matching. */
  423. static const char *
  424. linkage_name_str (const lookup_name_info &lookup_name)
  425. {
  426. /* Unlike most languages (including C++), Ada uses the
  427. encoded/linkage name as the search name recorded in symbols. So
  428. if debugging in Ada mode, prefer the Ada-encoded name. This also
  429. makes Ada's verbatim match syntax ("<...>") work, because
  430. "lookup_name.name()" includes the "<>"s, while
  431. "lookup_name.ada().lookup_name()" is the encoded name with "<>"s
  432. stripped. */
  433. if (current_language->la_language == language_ada)
  434. return lookup_name.ada ().lookup_name ().c_str ();
  435. return lookup_name.c_str ();
  436. }
  437. /* See minsyms.h. */
  438. void
  439. iterate_over_minimal_symbols
  440. (struct objfile *objf, const lookup_name_info &lookup_name,
  441. gdb::function_view<bool (struct minimal_symbol *)> callback)
  442. {
  443. /* The first pass is over the ordinary hash table. */
  444. {
  445. const char *name = linkage_name_str (lookup_name);
  446. unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
  447. auto *mangled_cmp
  448. = (case_sensitivity == case_sensitive_on
  449. ? strcmp
  450. : strcasecmp);
  451. for (minimal_symbol *iter = objf->per_bfd->msymbol_hash[hash];
  452. iter != NULL;
  453. iter = iter->hash_next)
  454. {
  455. if (mangled_cmp (iter->linkage_name (), name) == 0)
  456. if (callback (iter))
  457. return;
  458. }
  459. }
  460. /* The second pass is over the demangled table. Once for each
  461. language in the demangled hash names table (usually just zero or
  462. one). */
  463. for (unsigned liter = 0; liter < nr_languages; ++liter)
  464. {
  465. if (!objf->per_bfd->demangled_hash_languages.test (liter))
  466. continue;
  467. enum language lang = (enum language) liter;
  468. const language_defn *lang_def = language_def (lang);
  469. symbol_name_matcher_ftype *name_match
  470. = lang_def->get_symbol_name_matcher (lookup_name);
  471. unsigned int hash
  472. = lookup_name.search_name_hash (lang) % MINIMAL_SYMBOL_HASH_SIZE;
  473. for (minimal_symbol *iter = objf->per_bfd->msymbol_demangled_hash[hash];
  474. iter != NULL;
  475. iter = iter->demangled_hash_next)
  476. if (name_match (iter->search_name (), lookup_name, NULL))
  477. if (callback (iter))
  478. return;
  479. }
  480. }
  481. /* See minsyms.h. */
  482. bound_minimal_symbol
  483. lookup_minimal_symbol_linkage (const char *name, struct objfile *objf)
  484. {
  485. unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
  486. for (objfile *objfile : objf->separate_debug_objfiles ())
  487. {
  488. for (minimal_symbol *msymbol = objfile->per_bfd->msymbol_hash[hash];
  489. msymbol != NULL;
  490. msymbol = msymbol->hash_next)
  491. {
  492. if (strcmp (msymbol->linkage_name (), name) == 0
  493. && (MSYMBOL_TYPE (msymbol) == mst_data
  494. || MSYMBOL_TYPE (msymbol) == mst_bss))
  495. return {msymbol, objfile};
  496. }
  497. }
  498. return {};
  499. }
  500. /* See minsyms.h. */
  501. struct bound_minimal_symbol
  502. lookup_minimal_symbol_text (const char *name, struct objfile *objf)
  503. {
  504. struct minimal_symbol *msymbol;
  505. struct bound_minimal_symbol found_symbol;
  506. struct bound_minimal_symbol found_file_symbol;
  507. unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
  508. for (objfile *objfile : current_program_space->objfiles ())
  509. {
  510. if (found_symbol.minsym != NULL)
  511. break;
  512. if (objf == NULL || objf == objfile
  513. || objf == objfile->separate_debug_objfile_backlink)
  514. {
  515. for (msymbol = objfile->per_bfd->msymbol_hash[hash];
  516. msymbol != NULL && found_symbol.minsym == NULL;
  517. msymbol = msymbol->hash_next)
  518. {
  519. if (strcmp (msymbol->linkage_name (), name) == 0 &&
  520. (MSYMBOL_TYPE (msymbol) == mst_text
  521. || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
  522. || MSYMBOL_TYPE (msymbol) == mst_file_text))
  523. {
  524. switch (MSYMBOL_TYPE (msymbol))
  525. {
  526. case mst_file_text:
  527. found_file_symbol.minsym = msymbol;
  528. found_file_symbol.objfile = objfile;
  529. break;
  530. default:
  531. found_symbol.minsym = msymbol;
  532. found_symbol.objfile = objfile;
  533. break;
  534. }
  535. }
  536. }
  537. }
  538. }
  539. /* External symbols are best. */
  540. if (found_symbol.minsym)
  541. return found_symbol;
  542. /* File-local symbols are next best. */
  543. return found_file_symbol;
  544. }
  545. /* See minsyms.h. */
  546. struct minimal_symbol *
  547. lookup_minimal_symbol_by_pc_name (CORE_ADDR pc, const char *name,
  548. struct objfile *objf)
  549. {
  550. struct minimal_symbol *msymbol;
  551. unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
  552. for (objfile *objfile : current_program_space->objfiles ())
  553. {
  554. if (objf == NULL || objf == objfile
  555. || objf == objfile->separate_debug_objfile_backlink)
  556. {
  557. for (msymbol = objfile->per_bfd->msymbol_hash[hash];
  558. msymbol != NULL;
  559. msymbol = msymbol->hash_next)
  560. {
  561. if (MSYMBOL_VALUE_ADDRESS (objfile, msymbol) == pc
  562. && strcmp (msymbol->linkage_name (), name) == 0)
  563. return msymbol;
  564. }
  565. }
  566. }
  567. return NULL;
  568. }
  569. /* A helper function that makes *PC section-relative. This searches
  570. the sections of OBJFILE and if *PC is in a section, it subtracts
  571. the section offset and returns true. Otherwise it returns
  572. false. */
  573. static int
  574. frob_address (struct objfile *objfile, CORE_ADDR *pc)
  575. {
  576. struct obj_section *iter;
  577. ALL_OBJFILE_OSECTIONS (objfile, iter)
  578. {
  579. if (*pc >= iter->addr () && *pc < iter->endaddr ())
  580. {
  581. *pc -= iter->offset ();
  582. return 1;
  583. }
  584. }
  585. return 0;
  586. }
  587. /* Helper for lookup_minimal_symbol_by_pc_section. Convert a
  588. lookup_msym_prefer to a minimal_symbol_type. */
  589. static minimal_symbol_type
  590. msym_prefer_to_msym_type (lookup_msym_prefer prefer)
  591. {
  592. switch (prefer)
  593. {
  594. case lookup_msym_prefer::TEXT:
  595. return mst_text;
  596. case lookup_msym_prefer::TRAMPOLINE:
  597. return mst_solib_trampoline;
  598. case lookup_msym_prefer::GNU_IFUNC:
  599. return mst_text_gnu_ifunc;
  600. }
  601. /* Assert here instead of in a default switch case above so that
  602. -Wswitch warns if a new enumerator is added. */
  603. gdb_assert_not_reached ("unhandled lookup_msym_prefer");
  604. }
  605. /* See minsyms.h.
  606. Note that we need to look through ALL the minimal symbol tables
  607. before deciding on the symbol that comes closest to the specified PC.
  608. This is because objfiles can overlap, for example objfile A has .text
  609. at 0x100 and .data at 0x40000 and objfile B has .text at 0x234 and
  610. .data at 0x40048. */
  611. bound_minimal_symbol
  612. lookup_minimal_symbol_by_pc_section (CORE_ADDR pc_in, struct obj_section *section,
  613. lookup_msym_prefer prefer,
  614. bound_minimal_symbol *previous)
  615. {
  616. int lo;
  617. int hi;
  618. int newobj;
  619. struct minimal_symbol *msymbol;
  620. struct minimal_symbol *best_symbol = NULL;
  621. struct objfile *best_objfile = NULL;
  622. struct bound_minimal_symbol result;
  623. if (previous != nullptr)
  624. {
  625. previous->minsym = nullptr;
  626. previous->objfile = nullptr;
  627. }
  628. if (section == NULL)
  629. {
  630. section = find_pc_section (pc_in);
  631. if (section == NULL)
  632. return {};
  633. }
  634. minimal_symbol_type want_type = msym_prefer_to_msym_type (prefer);
  635. /* We can not require the symbol found to be in section, because
  636. e.g. IRIX 6.5 mdebug relies on this code returning an absolute
  637. symbol - but find_pc_section won't return an absolute section and
  638. hence the code below would skip over absolute symbols. We can
  639. still take advantage of the call to find_pc_section, though - the
  640. object file still must match. In case we have separate debug
  641. files, search both the file and its separate debug file. There's
  642. no telling which one will have the minimal symbols. */
  643. gdb_assert (section != NULL);
  644. for (objfile *objfile : section->objfile->separate_debug_objfiles ())
  645. {
  646. CORE_ADDR pc = pc_in;
  647. /* If this objfile has a minimal symbol table, go search it
  648. using a binary search. */
  649. if (objfile->per_bfd->minimal_symbol_count > 0)
  650. {
  651. int best_zero_sized = -1;
  652. msymbol = objfile->per_bfd->msymbols.get ();
  653. lo = 0;
  654. hi = objfile->per_bfd->minimal_symbol_count - 1;
  655. /* This code assumes that the minimal symbols are sorted by
  656. ascending address values. If the pc value is greater than or
  657. equal to the first symbol's address, then some symbol in this
  658. minimal symbol table is a suitable candidate for being the
  659. "best" symbol. This includes the last real symbol, for cases
  660. where the pc value is larger than any address in this vector.
  661. By iterating until the address associated with the current
  662. hi index (the endpoint of the test interval) is less than
  663. or equal to the desired pc value, we accomplish two things:
  664. (1) the case where the pc value is larger than any minimal
  665. symbol address is trivially solved, (2) the address associated
  666. with the hi index is always the one we want when the iteration
  667. terminates. In essence, we are iterating the test interval
  668. down until the pc value is pushed out of it from the high end.
  669. Warning: this code is trickier than it would appear at first. */
  670. if (frob_address (objfile, &pc)
  671. && pc >= MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[lo]))
  672. {
  673. while (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi]) > pc)
  674. {
  675. /* pc is still strictly less than highest address. */
  676. /* Note "new" will always be >= lo. */
  677. newobj = (lo + hi) / 2;
  678. if ((MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[newobj]) >= pc)
  679. || (lo == newobj))
  680. {
  681. hi = newobj;
  682. }
  683. else
  684. {
  685. lo = newobj;
  686. }
  687. }
  688. /* If we have multiple symbols at the same address, we want
  689. hi to point to the last one. That way we can find the
  690. right symbol if it has an index greater than hi. */
  691. while (hi < objfile->per_bfd->minimal_symbol_count - 1
  692. && (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
  693. == MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi + 1])))
  694. hi++;
  695. /* Skip various undesirable symbols. */
  696. while (hi >= 0)
  697. {
  698. /* Skip any absolute symbols. This is apparently
  699. what adb and dbx do, and is needed for the CM-5.
  700. There are two known possible problems: (1) on
  701. ELF, apparently end, edata, etc. are absolute.
  702. Not sure ignoring them here is a big deal, but if
  703. we want to use them, the fix would go in
  704. elfread.c. (2) I think shared library entry
  705. points on the NeXT are absolute. If we want
  706. special handling for this it probably should be
  707. triggered by a special mst_abs_or_lib or some
  708. such. */
  709. if (MSYMBOL_TYPE (&msymbol[hi]) == mst_abs)
  710. {
  711. hi--;
  712. continue;
  713. }
  714. /* If SECTION was specified, skip any symbol from
  715. wrong section. */
  716. if (section
  717. /* Some types of debug info, such as COFF,
  718. don't fill the bfd_section member, so don't
  719. throw away symbols on those platforms. */
  720. && msymbol[hi].obj_section (objfile) != nullptr
  721. && (!matching_obj_sections
  722. (msymbol[hi].obj_section (objfile),
  723. section)))
  724. {
  725. hi--;
  726. continue;
  727. }
  728. /* If we are looking for a trampoline and this is a
  729. text symbol, or the other way around, check the
  730. preceding symbol too. If they are otherwise
  731. identical prefer that one. */
  732. if (hi > 0
  733. && MSYMBOL_TYPE (&msymbol[hi]) != want_type
  734. && MSYMBOL_TYPE (&msymbol[hi - 1]) == want_type
  735. && (MSYMBOL_SIZE (&msymbol[hi])
  736. == MSYMBOL_SIZE (&msymbol[hi - 1]))
  737. && (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
  738. == MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi - 1]))
  739. && (msymbol[hi].obj_section (objfile)
  740. == msymbol[hi - 1].obj_section (objfile)))
  741. {
  742. hi--;
  743. continue;
  744. }
  745. /* If the minimal symbol has a zero size, save it
  746. but keep scanning backwards looking for one with
  747. a non-zero size. A zero size may mean that the
  748. symbol isn't an object or function (e.g. a
  749. label), or it may just mean that the size was not
  750. specified. */
  751. if (MSYMBOL_SIZE (&msymbol[hi]) == 0)
  752. {
  753. if (best_zero_sized == -1)
  754. best_zero_sized = hi;
  755. hi--;
  756. continue;
  757. }
  758. /* If we are past the end of the current symbol, try
  759. the previous symbol if it has a larger overlapping
  760. size. This happens on i686-pc-linux-gnu with glibc;
  761. the nocancel variants of system calls are inside
  762. the cancellable variants, but both have sizes. */
  763. if (hi > 0
  764. && MSYMBOL_SIZE (&msymbol[hi]) != 0
  765. && pc >= (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
  766. + MSYMBOL_SIZE (&msymbol[hi]))
  767. && pc < (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi - 1])
  768. + MSYMBOL_SIZE (&msymbol[hi - 1])))
  769. {
  770. hi--;
  771. continue;
  772. }
  773. /* Otherwise, this symbol must be as good as we're going
  774. to get. */
  775. break;
  776. }
  777. /* If HI has a zero size, and best_zero_sized is set,
  778. then we had two or more zero-sized symbols; prefer
  779. the first one we found (which may have a higher
  780. address). Also, if we ran off the end, be sure
  781. to back up. */
  782. if (best_zero_sized != -1
  783. && (hi < 0 || MSYMBOL_SIZE (&msymbol[hi]) == 0))
  784. hi = best_zero_sized;
  785. /* If the minimal symbol has a non-zero size, and this
  786. PC appears to be outside the symbol's contents, then
  787. refuse to use this symbol. If we found a zero-sized
  788. symbol with an address greater than this symbol's,
  789. use that instead. We assume that if symbols have
  790. specified sizes, they do not overlap. */
  791. if (hi >= 0
  792. && MSYMBOL_SIZE (&msymbol[hi]) != 0
  793. && pc >= (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
  794. + MSYMBOL_SIZE (&msymbol[hi])))
  795. {
  796. if (best_zero_sized != -1)
  797. hi = best_zero_sized;
  798. else
  799. {
  800. /* If needed record this symbol as the closest
  801. previous symbol. */
  802. if (previous != nullptr)
  803. {
  804. if (previous->minsym == nullptr
  805. || (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
  806. > MSYMBOL_VALUE_RAW_ADDRESS
  807. (previous->minsym)))
  808. {
  809. previous->minsym = &msymbol[hi];
  810. previous->objfile = objfile;
  811. }
  812. }
  813. /* Go on to the next object file. */
  814. continue;
  815. }
  816. }
  817. /* The minimal symbol indexed by hi now is the best one in this
  818. objfile's minimal symbol table. See if it is the best one
  819. overall. */
  820. if (hi >= 0
  821. && ((best_symbol == NULL) ||
  822. (MSYMBOL_VALUE_RAW_ADDRESS (best_symbol) <
  823. MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi]))))
  824. {
  825. best_symbol = &msymbol[hi];
  826. best_objfile = objfile;
  827. }
  828. }
  829. }
  830. }
  831. result.minsym = best_symbol;
  832. result.objfile = best_objfile;
  833. return result;
  834. }
  835. /* See minsyms.h. */
  836. struct bound_minimal_symbol
  837. lookup_minimal_symbol_by_pc (CORE_ADDR pc)
  838. {
  839. return lookup_minimal_symbol_by_pc_section (pc, NULL);
  840. }
  841. /* Return non-zero iff PC is in an STT_GNU_IFUNC function resolver. */
  842. bool
  843. in_gnu_ifunc_stub (CORE_ADDR pc)
  844. {
  845. bound_minimal_symbol msymbol
  846. = lookup_minimal_symbol_by_pc_section (pc, NULL,
  847. lookup_msym_prefer::GNU_IFUNC);
  848. return msymbol.minsym && MSYMBOL_TYPE (msymbol.minsym) == mst_text_gnu_ifunc;
  849. }
  850. /* See elf_gnu_ifunc_resolve_addr for its real implementation. */
  851. static CORE_ADDR
  852. stub_gnu_ifunc_resolve_addr (struct gdbarch *gdbarch, CORE_ADDR pc)
  853. {
  854. error (_("GDB cannot resolve STT_GNU_IFUNC symbol at address %s without "
  855. "the ELF support compiled in."),
  856. paddress (gdbarch, pc));
  857. }
  858. /* See elf_gnu_ifunc_resolve_name for its real implementation. */
  859. static bool
  860. stub_gnu_ifunc_resolve_name (const char *function_name,
  861. CORE_ADDR *function_address_p)
  862. {
  863. error (_("GDB cannot resolve STT_GNU_IFUNC symbol \"%s\" without "
  864. "the ELF support compiled in."),
  865. function_name);
  866. }
  867. /* See elf_gnu_ifunc_resolver_stop for its real implementation. */
  868. static void
  869. stub_gnu_ifunc_resolver_stop (struct breakpoint *b)
  870. {
  871. internal_error (__FILE__, __LINE__,
  872. _("elf_gnu_ifunc_resolver_stop cannot be reached."));
  873. }
  874. /* See elf_gnu_ifunc_resolver_return_stop for its real implementation. */
  875. static void
  876. stub_gnu_ifunc_resolver_return_stop (struct breakpoint *b)
  877. {
  878. internal_error (__FILE__, __LINE__,
  879. _("elf_gnu_ifunc_resolver_return_stop cannot be reached."));
  880. }
  881. /* See elf_gnu_ifunc_fns for its real implementation. */
  882. static const struct gnu_ifunc_fns stub_gnu_ifunc_fns =
  883. {
  884. stub_gnu_ifunc_resolve_addr,
  885. stub_gnu_ifunc_resolve_name,
  886. stub_gnu_ifunc_resolver_stop,
  887. stub_gnu_ifunc_resolver_return_stop,
  888. };
  889. /* A placeholder for &elf_gnu_ifunc_fns. */
  890. const struct gnu_ifunc_fns *gnu_ifunc_fns_p = &stub_gnu_ifunc_fns;
  891. /* Return leading symbol character for a BFD. If BFD is NULL,
  892. return the leading symbol character from the main objfile. */
  893. static int
  894. get_symbol_leading_char (bfd *abfd)
  895. {
  896. if (abfd != NULL)
  897. return bfd_get_symbol_leading_char (abfd);
  898. if (current_program_space->symfile_object_file != NULL)
  899. {
  900. objfile *objf = current_program_space->symfile_object_file;
  901. if (objf->obfd != NULL)
  902. return bfd_get_symbol_leading_char (objf->obfd);
  903. }
  904. return 0;
  905. }
  906. /* See minsyms.h. */
  907. minimal_symbol_reader::minimal_symbol_reader (struct objfile *obj)
  908. : m_objfile (obj),
  909. m_msym_bunch (NULL),
  910. /* Note that presetting m_msym_bunch_index to BUNCH_SIZE causes the
  911. first call to save a minimal symbol to allocate the memory for
  912. the first bunch. */
  913. m_msym_bunch_index (BUNCH_SIZE),
  914. m_msym_count (0)
  915. {
  916. }
  917. /* Discard the currently collected minimal symbols, if any. If we wish
  918. to save them for later use, we must have already copied them somewhere
  919. else before calling this function. */
  920. minimal_symbol_reader::~minimal_symbol_reader ()
  921. {
  922. struct msym_bunch *next;
  923. while (m_msym_bunch != NULL)
  924. {
  925. next = m_msym_bunch->next;
  926. xfree (m_msym_bunch);
  927. m_msym_bunch = next;
  928. }
  929. }
  930. /* See minsyms.h. */
  931. void
  932. minimal_symbol_reader::record (const char *name, CORE_ADDR address,
  933. enum minimal_symbol_type ms_type)
  934. {
  935. int section;
  936. switch (ms_type)
  937. {
  938. case mst_text:
  939. case mst_text_gnu_ifunc:
  940. case mst_file_text:
  941. case mst_solib_trampoline:
  942. section = SECT_OFF_TEXT (m_objfile);
  943. break;
  944. case mst_data:
  945. case mst_data_gnu_ifunc:
  946. case mst_file_data:
  947. section = SECT_OFF_DATA (m_objfile);
  948. break;
  949. case mst_bss:
  950. case mst_file_bss:
  951. section = SECT_OFF_BSS (m_objfile);
  952. break;
  953. default:
  954. section = -1;
  955. }
  956. record_with_info (name, address, ms_type, section);
  957. }
  958. /* Convert an enumerator of type minimal_symbol_type to its string
  959. representation. */
  960. static const char *
  961. mst_str (minimal_symbol_type t)
  962. {
  963. #define MST_TO_STR(x) case x: return #x;
  964. switch (t)
  965. {
  966. MST_TO_STR (mst_unknown);
  967. MST_TO_STR (mst_text);
  968. MST_TO_STR (mst_text_gnu_ifunc);
  969. MST_TO_STR (mst_slot_got_plt);
  970. MST_TO_STR (mst_data);
  971. MST_TO_STR (mst_bss);
  972. MST_TO_STR (mst_abs);
  973. MST_TO_STR (mst_solib_trampoline);
  974. MST_TO_STR (mst_file_text);
  975. MST_TO_STR (mst_file_data);
  976. MST_TO_STR (mst_file_bss);
  977. default:
  978. return "mst_???";
  979. }
  980. #undef MST_TO_STR
  981. }
  982. /* See minsyms.h. */
  983. struct minimal_symbol *
  984. minimal_symbol_reader::record_full (gdb::string_view name,
  985. bool copy_name, CORE_ADDR address,
  986. enum minimal_symbol_type ms_type,
  987. int section)
  988. {
  989. struct msym_bunch *newobj;
  990. struct minimal_symbol *msymbol;
  991. /* Don't put gcc_compiled, __gnu_compiled_cplus, and friends into
  992. the minimal symbols, because if there is also another symbol
  993. at the same address (e.g. the first function of the file),
  994. lookup_minimal_symbol_by_pc would have no way of getting the
  995. right one. */
  996. if (ms_type == mst_file_text && name[0] == 'g'
  997. && (name == GCC_COMPILED_FLAG_SYMBOL
  998. || name == GCC2_COMPILED_FLAG_SYMBOL))
  999. return (NULL);
  1000. /* It's safe to strip the leading char here once, since the name
  1001. is also stored stripped in the minimal symbol table. */
  1002. if (name[0] == get_symbol_leading_char (m_objfile->obfd))
  1003. name = name.substr (1);
  1004. if (ms_type == mst_file_text && startswith (name, "__gnu_compiled"))
  1005. return (NULL);
  1006. if (symtab_create_debug >= 2)
  1007. gdb_printf (gdb_stdlog,
  1008. "Recording minsym: %-21s %18s %4d %.*s\n",
  1009. mst_str (ms_type), hex_string (address), section,
  1010. (int) name.size (), name.data ());
  1011. if (m_msym_bunch_index == BUNCH_SIZE)
  1012. {
  1013. newobj = XCNEW (struct msym_bunch);
  1014. m_msym_bunch_index = 0;
  1015. newobj->next = m_msym_bunch;
  1016. m_msym_bunch = newobj;
  1017. }
  1018. msymbol = &m_msym_bunch->contents[m_msym_bunch_index];
  1019. msymbol->set_language (language_auto,
  1020. &m_objfile->per_bfd->storage_obstack);
  1021. if (copy_name)
  1022. msymbol->m_name = obstack_strndup (&m_objfile->per_bfd->storage_obstack,
  1023. name.data (), name.size ());
  1024. else
  1025. msymbol->m_name = name.data ();
  1026. SET_MSYMBOL_VALUE_ADDRESS (msymbol, address);
  1027. msymbol->set_section_index (section);
  1028. MSYMBOL_TYPE (msymbol) = ms_type;
  1029. /* If we already read minimal symbols for this objfile, then don't
  1030. ever allocate a new one. */
  1031. if (!m_objfile->per_bfd->minsyms_read)
  1032. {
  1033. m_msym_bunch_index++;
  1034. m_objfile->per_bfd->n_minsyms++;
  1035. }
  1036. m_msym_count++;
  1037. return msymbol;
  1038. }
  1039. /* Compare two minimal symbols by address and return true if FN1's address
  1040. is less than FN2's, so that we sort into unsigned numeric order.
  1041. Within groups with the same address, sort by name. */
  1042. static inline bool
  1043. minimal_symbol_is_less_than (const minimal_symbol &fn1,
  1044. const minimal_symbol &fn2)
  1045. {
  1046. if (MSYMBOL_VALUE_RAW_ADDRESS (&fn1) < MSYMBOL_VALUE_RAW_ADDRESS (&fn2))
  1047. {
  1048. return true; /* addr 1 is less than addr 2. */
  1049. }
  1050. else if (MSYMBOL_VALUE_RAW_ADDRESS (&fn1) > MSYMBOL_VALUE_RAW_ADDRESS (&fn2))
  1051. {
  1052. return false; /* addr 1 is greater than addr 2. */
  1053. }
  1054. else
  1055. /* addrs are equal: sort by name */
  1056. {
  1057. const char *name1 = fn1.linkage_name ();
  1058. const char *name2 = fn2.linkage_name ();
  1059. if (name1 && name2) /* both have names */
  1060. return strcmp (name1, name2) < 0;
  1061. else if (name2)
  1062. return true; /* fn1 has no name, so it is "less". */
  1063. else if (name1) /* fn2 has no name, so it is "less". */
  1064. return false;
  1065. else
  1066. return false; /* Neither has a name, so they're equal. */
  1067. }
  1068. }
  1069. /* Compact duplicate entries out of a minimal symbol table by walking
  1070. through the table and compacting out entries with duplicate addresses
  1071. and matching names. Return the number of entries remaining.
  1072. On entry, the table resides between msymbol[0] and msymbol[mcount].
  1073. On exit, it resides between msymbol[0] and msymbol[result_count].
  1074. When files contain multiple sources of symbol information, it is
  1075. possible for the minimal symbol table to contain many duplicate entries.
  1076. As an example, SVR4 systems use ELF formatted object files, which
  1077. usually contain at least two different types of symbol tables (a
  1078. standard ELF one and a smaller dynamic linking table), as well as
  1079. DWARF debugging information for files compiled with -g.
  1080. Without compacting, the minimal symbol table for gdb itself contains
  1081. over a 1000 duplicates, about a third of the total table size. Aside
  1082. from the potential trap of not noticing that two successive entries
  1083. identify the same location, this duplication impacts the time required
  1084. to linearly scan the table, which is done in a number of places. So we
  1085. just do one linear scan here and toss out the duplicates.
  1086. Since the different sources of information for each symbol may
  1087. have different levels of "completeness", we may have duplicates
  1088. that have one entry with type "mst_unknown" and the other with a
  1089. known type. So if the one we are leaving alone has type mst_unknown,
  1090. overwrite its type with the type from the one we are compacting out. */
  1091. static int
  1092. compact_minimal_symbols (struct minimal_symbol *msymbol, int mcount,
  1093. struct objfile *objfile)
  1094. {
  1095. struct minimal_symbol *copyfrom;
  1096. struct minimal_symbol *copyto;
  1097. if (mcount > 0)
  1098. {
  1099. copyfrom = copyto = msymbol;
  1100. while (copyfrom < msymbol + mcount - 1)
  1101. {
  1102. if (MSYMBOL_VALUE_RAW_ADDRESS (copyfrom)
  1103. == MSYMBOL_VALUE_RAW_ADDRESS ((copyfrom + 1))
  1104. && (copyfrom->section_index ()
  1105. == (copyfrom + 1)->section_index ())
  1106. && strcmp (copyfrom->linkage_name (),
  1107. (copyfrom + 1)->linkage_name ()) == 0)
  1108. {
  1109. if (MSYMBOL_TYPE ((copyfrom + 1)) == mst_unknown)
  1110. {
  1111. MSYMBOL_TYPE ((copyfrom + 1)) = MSYMBOL_TYPE (copyfrom);
  1112. }
  1113. copyfrom++;
  1114. }
  1115. else
  1116. *copyto++ = *copyfrom++;
  1117. }
  1118. *copyto++ = *copyfrom++;
  1119. mcount = copyto - msymbol;
  1120. }
  1121. return (mcount);
  1122. }
  1123. static void
  1124. clear_minimal_symbol_hash_tables (struct objfile *objfile)
  1125. {
  1126. for (size_t i = 0; i < MINIMAL_SYMBOL_HASH_SIZE; i++)
  1127. {
  1128. objfile->per_bfd->msymbol_hash[i] = 0;
  1129. objfile->per_bfd->msymbol_demangled_hash[i] = 0;
  1130. }
  1131. }
  1132. /* This struct is used to store values we compute for msymbols on the
  1133. background threads but don't need to keep around long term. */
  1134. struct computed_hash_values
  1135. {
  1136. /* Length of the linkage_name of the symbol. */
  1137. size_t name_length;
  1138. /* Hash code (using fast_hash) of the linkage_name. */
  1139. hashval_t mangled_name_hash;
  1140. /* The msymbol_hash of the linkage_name. */
  1141. unsigned int minsym_hash;
  1142. /* The msymbol_hash of the search_name. */
  1143. unsigned int minsym_demangled_hash;
  1144. };
  1145. /* Build (or rebuild) the minimal symbol hash tables. This is necessary
  1146. after compacting or sorting the table since the entries move around
  1147. thus causing the internal minimal_symbol pointers to become jumbled. */
  1148. static void
  1149. build_minimal_symbol_hash_tables
  1150. (struct objfile *objfile,
  1151. const std::vector<computed_hash_values>& hash_values)
  1152. {
  1153. int i;
  1154. struct minimal_symbol *msym;
  1155. /* (Re)insert the actual entries. */
  1156. int mcount = objfile->per_bfd->minimal_symbol_count;
  1157. for ((i = 0,
  1158. msym = objfile->per_bfd->msymbols.get ());
  1159. i < mcount;
  1160. i++, msym++)
  1161. {
  1162. msym->hash_next = 0;
  1163. add_minsym_to_hash_table (msym, objfile->per_bfd->msymbol_hash,
  1164. hash_values[i].minsym_hash);
  1165. msym->demangled_hash_next = 0;
  1166. if (msym->search_name () != msym->linkage_name ())
  1167. add_minsym_to_demangled_hash_table
  1168. (msym, objfile, hash_values[i].minsym_demangled_hash);
  1169. }
  1170. }
  1171. /* Add the minimal symbols in the existing bunches to the objfile's official
  1172. minimal symbol table. In most cases there is no minimal symbol table yet
  1173. for this objfile, and the existing bunches are used to create one. Once
  1174. in a while (for shared libraries for example), we add symbols (e.g. common
  1175. symbols) to an existing objfile. */
  1176. void
  1177. minimal_symbol_reader::install ()
  1178. {
  1179. int mcount;
  1180. struct msym_bunch *bunch;
  1181. struct minimal_symbol *msymbols;
  1182. int alloc_count;
  1183. if (m_objfile->per_bfd->minsyms_read)
  1184. return;
  1185. if (m_msym_count > 0)
  1186. {
  1187. if (symtab_create_debug)
  1188. {
  1189. gdb_printf (gdb_stdlog,
  1190. "Installing %d minimal symbols of objfile %s.\n",
  1191. m_msym_count, objfile_name (m_objfile));
  1192. }
  1193. /* Allocate enough space, into which we will gather the bunches
  1194. of new and existing minimal symbols, sort them, and then
  1195. compact out the duplicate entries. Once we have a final
  1196. table, we will give back the excess space. */
  1197. alloc_count = m_msym_count + m_objfile->per_bfd->minimal_symbol_count;
  1198. gdb::unique_xmalloc_ptr<minimal_symbol>
  1199. msym_holder (XNEWVEC (minimal_symbol, alloc_count));
  1200. msymbols = msym_holder.get ();
  1201. /* Copy in the existing minimal symbols, if there are any. */
  1202. if (m_objfile->per_bfd->minimal_symbol_count)
  1203. memcpy (msymbols, m_objfile->per_bfd->msymbols.get (),
  1204. m_objfile->per_bfd->minimal_symbol_count
  1205. * sizeof (struct minimal_symbol));
  1206. /* Walk through the list of minimal symbol bunches, adding each symbol
  1207. to the new contiguous array of symbols. Note that we start with the
  1208. current, possibly partially filled bunch (thus we use the current
  1209. msym_bunch_index for the first bunch we copy over), and thereafter
  1210. each bunch is full. */
  1211. mcount = m_objfile->per_bfd->minimal_symbol_count;
  1212. for (bunch = m_msym_bunch; bunch != NULL; bunch = bunch->next)
  1213. {
  1214. memcpy (&msymbols[mcount], &bunch->contents[0],
  1215. m_msym_bunch_index * sizeof (struct minimal_symbol));
  1216. mcount += m_msym_bunch_index;
  1217. m_msym_bunch_index = BUNCH_SIZE;
  1218. }
  1219. /* Sort the minimal symbols by address. */
  1220. std::sort (msymbols, msymbols + mcount, minimal_symbol_is_less_than);
  1221. /* Compact out any duplicates, and free up whatever space we are
  1222. no longer using. */
  1223. mcount = compact_minimal_symbols (msymbols, mcount, m_objfile);
  1224. msym_holder.reset (XRESIZEVEC (struct minimal_symbol,
  1225. msym_holder.release (),
  1226. mcount));
  1227. /* Attach the minimal symbol table to the specified objfile.
  1228. The strings themselves are also located in the storage_obstack
  1229. of this objfile. */
  1230. if (m_objfile->per_bfd->minimal_symbol_count != 0)
  1231. clear_minimal_symbol_hash_tables (m_objfile);
  1232. m_objfile->per_bfd->minimal_symbol_count = mcount;
  1233. m_objfile->per_bfd->msymbols = std::move (msym_holder);
  1234. #if CXX_STD_THREAD
  1235. /* Mutex that is used when modifying or accessing the demangled
  1236. hash table. */
  1237. std::mutex demangled_mutex;
  1238. #endif
  1239. std::vector<computed_hash_values> hash_values (mcount);
  1240. msymbols = m_objfile->per_bfd->msymbols.get ();
  1241. gdb::parallel_for_each
  1242. (&msymbols[0], &msymbols[mcount],
  1243. [&] (minimal_symbol *start, minimal_symbol *end)
  1244. {
  1245. for (minimal_symbol *msym = start; msym < end; ++msym)
  1246. {
  1247. size_t idx = msym - msymbols;
  1248. hash_values[idx].name_length = strlen (msym->linkage_name ());
  1249. if (!msym->name_set)
  1250. {
  1251. /* This will be freed later, by compute_and_set_names. */
  1252. gdb::unique_xmalloc_ptr<char> demangled_name
  1253. = symbol_find_demangled_name (msym, msym->linkage_name ());
  1254. msym->set_demangled_name
  1255. (demangled_name.release (),
  1256. &m_objfile->per_bfd->storage_obstack);
  1257. msym->name_set = 1;
  1258. }
  1259. /* This mangled_name_hash computation has to be outside of
  1260. the name_set check, or compute_and_set_names below will
  1261. be called with an invalid hash value. */
  1262. hash_values[idx].mangled_name_hash
  1263. = fast_hash (msym->linkage_name (),
  1264. hash_values[idx].name_length);
  1265. hash_values[idx].minsym_hash
  1266. = msymbol_hash (msym->linkage_name ());
  1267. /* We only use this hash code if the search name differs
  1268. from the linkage name. See the code in
  1269. build_minimal_symbol_hash_tables. */
  1270. if (msym->search_name () != msym->linkage_name ())
  1271. hash_values[idx].minsym_demangled_hash
  1272. = search_name_hash (msym->language (), msym->search_name ());
  1273. }
  1274. {
  1275. /* To limit how long we hold the lock, we only acquire it here
  1276. and not while we demangle the names above. */
  1277. #if CXX_STD_THREAD
  1278. std::lock_guard<std::mutex> guard (demangled_mutex);
  1279. #endif
  1280. for (minimal_symbol *msym = start; msym < end; ++msym)
  1281. {
  1282. size_t idx = msym - msymbols;
  1283. msym->compute_and_set_names
  1284. (gdb::string_view (msym->linkage_name (),
  1285. hash_values[idx].name_length),
  1286. false,
  1287. m_objfile->per_bfd,
  1288. hash_values[idx].mangled_name_hash);
  1289. }
  1290. }
  1291. });
  1292. build_minimal_symbol_hash_tables (m_objfile, hash_values);
  1293. }
  1294. }
  1295. /* Check if PC is in a shared library trampoline code stub.
  1296. Return minimal symbol for the trampoline entry or NULL if PC is not
  1297. in a trampoline code stub. */
  1298. static struct minimal_symbol *
  1299. lookup_solib_trampoline_symbol_by_pc (CORE_ADDR pc)
  1300. {
  1301. bound_minimal_symbol msymbol
  1302. = lookup_minimal_symbol_by_pc_section (pc, NULL,
  1303. lookup_msym_prefer::TRAMPOLINE);
  1304. if (msymbol.minsym != NULL
  1305. && MSYMBOL_TYPE (msymbol.minsym) == mst_solib_trampoline)
  1306. return msymbol.minsym;
  1307. return NULL;
  1308. }
  1309. /* If PC is in a shared library trampoline code stub, return the
  1310. address of the `real' function belonging to the stub.
  1311. Return 0 if PC is not in a trampoline code stub or if the real
  1312. function is not found in the minimal symbol table.
  1313. We may fail to find the right function if a function with the
  1314. same name is defined in more than one shared library, but this
  1315. is considered bad programming style. We could return 0 if we find
  1316. a duplicate function in case this matters someday. */
  1317. CORE_ADDR
  1318. find_solib_trampoline_target (struct frame_info *frame, CORE_ADDR pc)
  1319. {
  1320. struct minimal_symbol *tsymbol = lookup_solib_trampoline_symbol_by_pc (pc);
  1321. if (tsymbol != NULL)
  1322. {
  1323. for (objfile *objfile : current_program_space->objfiles ())
  1324. {
  1325. for (minimal_symbol *msymbol : objfile->msymbols ())
  1326. {
  1327. /* Also handle minimal symbols pointing to function
  1328. descriptors. */
  1329. if ((MSYMBOL_TYPE (msymbol) == mst_text
  1330. || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
  1331. || MSYMBOL_TYPE (msymbol) == mst_data
  1332. || MSYMBOL_TYPE (msymbol) == mst_data_gnu_ifunc)
  1333. && strcmp (msymbol->linkage_name (),
  1334. tsymbol->linkage_name ()) == 0)
  1335. {
  1336. CORE_ADDR func;
  1337. /* Ignore data symbols that are not function
  1338. descriptors. */
  1339. if (msymbol_is_function (objfile, msymbol, &func))
  1340. return func;
  1341. }
  1342. }
  1343. }
  1344. }
  1345. return 0;
  1346. }
  1347. /* See minsyms.h. */
  1348. CORE_ADDR
  1349. minimal_symbol_upper_bound (struct bound_minimal_symbol minsym)
  1350. {
  1351. short section;
  1352. struct obj_section *obj_section;
  1353. CORE_ADDR result;
  1354. struct minimal_symbol *iter, *msymbol;
  1355. gdb_assert (minsym.minsym != NULL);
  1356. /* If the minimal symbol has a size, use it. Otherwise use the
  1357. lesser of the next minimal symbol in the same section, or the end
  1358. of the section, as the end of the function. */
  1359. if (MSYMBOL_SIZE (minsym.minsym) != 0)
  1360. return BMSYMBOL_VALUE_ADDRESS (minsym) + MSYMBOL_SIZE (minsym.minsym);
  1361. /* Step over other symbols at this same address, and symbols in
  1362. other sections, to find the next symbol in this section with a
  1363. different address. */
  1364. struct minimal_symbol *past_the_end
  1365. = (minsym.objfile->per_bfd->msymbols.get ()
  1366. + minsym.objfile->per_bfd->minimal_symbol_count);
  1367. msymbol = minsym.minsym;
  1368. section = msymbol->section_index ();
  1369. for (iter = msymbol + 1; iter != past_the_end; ++iter)
  1370. {
  1371. if ((MSYMBOL_VALUE_RAW_ADDRESS (iter)
  1372. != MSYMBOL_VALUE_RAW_ADDRESS (msymbol))
  1373. && iter->section_index () == section)
  1374. break;
  1375. }
  1376. obj_section = minsym.obj_section ();
  1377. if (iter != past_the_end
  1378. && (MSYMBOL_VALUE_ADDRESS (minsym.objfile, iter)
  1379. < obj_section->endaddr ()))
  1380. result = MSYMBOL_VALUE_ADDRESS (minsym.objfile, iter);
  1381. else
  1382. /* We got the start address from the last msymbol in the objfile.
  1383. So the end address is the end of the section. */
  1384. result = obj_section->endaddr ();
  1385. return result;
  1386. }