gdb-index.cc 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. // gdb-index.cc -- generate .gdb_index section for fast debug lookup
  2. // Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. // Written by Cary Coutant <ccoutant@google.com>.
  4. // This file is part of gold.
  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, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include "gdb-index.h"
  19. #include "dwarf_reader.h"
  20. #include "dwarf.h"
  21. #include "object.h"
  22. #include "output.h"
  23. #include "demangle.h"
  24. namespace gold
  25. {
  26. const int gdb_index_version = 7;
  27. // Sizes of various records in the .gdb_index section.
  28. const int gdb_index_offset_size = 4;
  29. const int gdb_index_hdr_size = 6 * gdb_index_offset_size;
  30. const int gdb_index_cu_size = 16;
  31. const int gdb_index_tu_size = 24;
  32. const int gdb_index_addr_size = 16 + gdb_index_offset_size;
  33. const int gdb_index_sym_size = 2 * gdb_index_offset_size;
  34. // This class manages the hashed symbol table for the .gdb_index section.
  35. // It is essentially equivalent to the hashtab implementation in libiberty,
  36. // but is copied into gdb sources and here for compatibility because its
  37. // data structure is exposed on disk.
  38. template <typename T>
  39. class Gdb_hashtab
  40. {
  41. public:
  42. Gdb_hashtab()
  43. : size_(0), capacity_(0), hashtab_(NULL)
  44. { }
  45. ~Gdb_hashtab()
  46. {
  47. for (size_t i = 0; i < this->capacity_; ++i)
  48. if (this->hashtab_[i] != NULL)
  49. delete this->hashtab_[i];
  50. delete[] this->hashtab_;
  51. }
  52. // Add a symbol.
  53. T*
  54. add(T* symbol)
  55. {
  56. // Resize the hash table if necessary.
  57. if (4 * this->size_ / 3 >= this->capacity_)
  58. this->expand();
  59. T** slot = this->find_slot(symbol);
  60. if (*slot == NULL)
  61. {
  62. ++this->size_;
  63. *slot = symbol;
  64. }
  65. return *slot;
  66. }
  67. // Return the current size.
  68. size_t
  69. size() const
  70. { return this->size_; }
  71. // Return the current capacity.
  72. size_t
  73. capacity() const
  74. { return this->capacity_; }
  75. // Return the contents of slot N.
  76. T*
  77. operator[](size_t n)
  78. { return this->hashtab_[n]; }
  79. private:
  80. // Find a symbol in the hash table, or return an empty slot if
  81. // the symbol is not in the table.
  82. T**
  83. find_slot(T* symbol)
  84. {
  85. unsigned int index = symbol->hash() & (this->capacity_ - 1);
  86. unsigned int step = ((symbol->hash() * 17) & (this->capacity_ - 1)) | 1;
  87. for (;;)
  88. {
  89. if (this->hashtab_[index] == NULL
  90. || this->hashtab_[index]->equal(symbol))
  91. return &this->hashtab_[index];
  92. index = (index + step) & (this->capacity_ - 1);
  93. }
  94. }
  95. // Expand the hash table.
  96. void
  97. expand()
  98. {
  99. if (this->capacity_ == 0)
  100. {
  101. // Allocate the hash table for the first time.
  102. this->capacity_ = Gdb_hashtab::initial_size;
  103. this->hashtab_ = new T*[this->capacity_];
  104. memset(this->hashtab_, 0, this->capacity_ * sizeof(T*));
  105. }
  106. else
  107. {
  108. // Expand and rehash.
  109. unsigned int old_cap = this->capacity_;
  110. T** old_hashtab = this->hashtab_;
  111. this->capacity_ *= 2;
  112. this->hashtab_ = new T*[this->capacity_];
  113. memset(this->hashtab_, 0, this->capacity_ * sizeof(T*));
  114. for (size_t i = 0; i < old_cap; ++i)
  115. {
  116. if (old_hashtab[i] != NULL)
  117. {
  118. T** slot = this->find_slot(old_hashtab[i]);
  119. *slot = old_hashtab[i];
  120. }
  121. }
  122. delete[] old_hashtab;
  123. }
  124. }
  125. // Initial size of the hash table; must be a power of 2.
  126. static const int initial_size = 1024;
  127. size_t size_;
  128. size_t capacity_;
  129. T** hashtab_;
  130. };
  131. // The hash function for strings in the mapped index. This is copied
  132. // directly from gdb/dwarf2read.c.
  133. static unsigned int
  134. mapped_index_string_hash(const unsigned char* str)
  135. {
  136. unsigned int r = 0;
  137. unsigned char c;
  138. while ((c = *str++) != 0)
  139. {
  140. if (gdb_index_version >= 5)
  141. c = tolower (c);
  142. r = r * 67 + c - 113;
  143. }
  144. return r;
  145. }
  146. // A specialization of Dwarf_info_reader, for building the .gdb_index.
  147. class Gdb_index_info_reader : public Dwarf_info_reader
  148. {
  149. public:
  150. Gdb_index_info_reader(bool is_type_unit,
  151. Relobj* object,
  152. const unsigned char* symbols,
  153. off_t symbols_size,
  154. unsigned int shndx,
  155. unsigned int reloc_shndx,
  156. unsigned int reloc_type,
  157. Gdb_index* gdb_index)
  158. : Dwarf_info_reader(is_type_unit, object, symbols, symbols_size, shndx,
  159. reloc_shndx, reloc_type),
  160. gdb_index_(gdb_index), cu_index_(0), cu_language_(0)
  161. { }
  162. ~Gdb_index_info_reader()
  163. { this->clear_declarations(); }
  164. // Print usage statistics.
  165. static void
  166. print_stats();
  167. protected:
  168. // Visit a compilation unit.
  169. virtual void
  170. visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*);
  171. // Visit a type unit.
  172. virtual void
  173. visit_type_unit(off_t tu_offset, off_t tu_length, off_t type_offset,
  174. uint64_t signature, Dwarf_die*);
  175. private:
  176. // A map for recording DIEs we've seen that may be referred to be
  177. // later DIEs (via DW_AT_specification or DW_AT_abstract_origin).
  178. // The map is indexed by a DIE offset within the compile unit.
  179. // PARENT_OFFSET_ is the offset of the DIE that represents the
  180. // outer context, and NAME_ is a pointer to a component of the
  181. // fully-qualified name.
  182. // Normally, the names we point to are in a string table, so we don't
  183. // have to manage them, but when we have a fully-qualified name
  184. // computed, we put it in the table, and set PARENT_OFFSET_ to -1
  185. // indicate a string that we are managing.
  186. struct Declaration_pair
  187. {
  188. Declaration_pair(off_t parent_offset, const char* name)
  189. : parent_offset_(parent_offset), name_(name)
  190. { }
  191. off_t parent_offset_;
  192. const char* name_;
  193. };
  194. typedef Unordered_map<off_t, Declaration_pair> Declaration_map;
  195. // Visit a top-level DIE.
  196. void
  197. visit_top_die(Dwarf_die* die);
  198. // Visit the children of a DIE.
  199. void
  200. visit_children(Dwarf_die* die, Dwarf_die* context);
  201. // Visit a DIE.
  202. void
  203. visit_die(Dwarf_die* die, Dwarf_die* context);
  204. // Visit the children of a DIE.
  205. void
  206. visit_children_for_decls(Dwarf_die* die);
  207. // Visit a DIE.
  208. void
  209. visit_die_for_decls(Dwarf_die* die, Dwarf_die* context);
  210. // Guess a fully-qualified name for a class type, based on member function
  211. // linkage names.
  212. std::string
  213. guess_full_class_name(Dwarf_die* die);
  214. // Add a declaration DIE to the table of declarations.
  215. void
  216. add_declaration(Dwarf_die* die, Dwarf_die* context);
  217. // Add a declaration whose fully-qualified name is already known.
  218. void
  219. add_declaration_with_full_name(Dwarf_die* die, const char* full_name);
  220. // Return the context for a DIE whose parent is at DIE_OFFSET.
  221. std::string
  222. get_context(off_t die_offset);
  223. // Construct a fully-qualified name for DIE.
  224. std::string
  225. get_qualified_name(Dwarf_die* die, Dwarf_die* context);
  226. // Record the address ranges for a compilation unit.
  227. void
  228. record_cu_ranges(Dwarf_die* die);
  229. // Wrapper for read_pubtable.
  230. bool
  231. read_pubnames_and_pubtypes(Dwarf_die* die);
  232. // Read the .debug_pubnames and .debug_pubtypes tables.
  233. bool
  234. read_pubtable(Dwarf_pubnames_table* table, off_t offset);
  235. // Clear the declarations map.
  236. void
  237. clear_declarations();
  238. // The Gdb_index section.
  239. Gdb_index* gdb_index_;
  240. // The current CU index (negative for a TU).
  241. int cu_index_;
  242. // The language of the current CU or TU.
  243. unsigned int cu_language_;
  244. // Map from DIE offset to (parent offset, name) pair,
  245. // for DW_AT_specification.
  246. Declaration_map declarations_;
  247. // Statistics.
  248. // Total number of DWARF compilation units processed.
  249. static unsigned int dwarf_cu_count;
  250. // Number of DWARF compilation units with pubnames/pubtypes.
  251. static unsigned int dwarf_cu_nopubnames_count;
  252. // Total number of DWARF type units processed.
  253. static unsigned int dwarf_tu_count;
  254. // Number of DWARF type units with pubnames/pubtypes.
  255. static unsigned int dwarf_tu_nopubnames_count;
  256. };
  257. // Total number of DWARF compilation units processed.
  258. unsigned int Gdb_index_info_reader::dwarf_cu_count = 0;
  259. // Number of DWARF compilation units without pubnames/pubtypes.
  260. unsigned int Gdb_index_info_reader::dwarf_cu_nopubnames_count = 0;
  261. // Total number of DWARF type units processed.
  262. unsigned int Gdb_index_info_reader::dwarf_tu_count = 0;
  263. // Number of DWARF type units without pubnames/pubtypes.
  264. unsigned int Gdb_index_info_reader::dwarf_tu_nopubnames_count = 0;
  265. // Process a compilation unit and parse its child DIE.
  266. void
  267. Gdb_index_info_reader::visit_compilation_unit(off_t cu_offset, off_t cu_length,
  268. Dwarf_die* root_die)
  269. {
  270. ++Gdb_index_info_reader::dwarf_cu_count;
  271. this->cu_index_ = this->gdb_index_->add_comp_unit(cu_offset, cu_length);
  272. this->visit_top_die(root_die);
  273. }
  274. // Process a type unit and parse its child DIE.
  275. void
  276. Gdb_index_info_reader::visit_type_unit(off_t tu_offset, off_t,
  277. off_t type_offset, uint64_t signature,
  278. Dwarf_die* root_die)
  279. {
  280. ++Gdb_index_info_reader::dwarf_tu_count;
  281. // Use a negative index to flag this as a TU instead of a CU.
  282. this->cu_index_ = -1 - this->gdb_index_->add_type_unit(tu_offset, type_offset,
  283. signature);
  284. this->visit_top_die(root_die);
  285. }
  286. // Process a top-level DIE.
  287. // For compile_unit DIEs, record the address ranges. For all
  288. // interesting tags, add qualified names to the symbol table
  289. // and process interesting children. We may need to process
  290. // certain children just for saving declarations that might be
  291. // referenced by later DIEs with a DW_AT_specification attribute.
  292. void
  293. Gdb_index_info_reader::visit_top_die(Dwarf_die* die)
  294. {
  295. this->clear_declarations();
  296. switch (die->tag())
  297. {
  298. case elfcpp::DW_TAG_compile_unit:
  299. case elfcpp::DW_TAG_type_unit:
  300. this->cu_language_ = die->int_attribute(elfcpp::DW_AT_language);
  301. if (die->tag() == elfcpp::DW_TAG_compile_unit)
  302. this->record_cu_ranges(die);
  303. // If there is a pubnames and/or pubtypes section for this
  304. // compilation unit, use those; otherwise, parse the DWARF
  305. // info to extract the names.
  306. if (!this->read_pubnames_and_pubtypes(die))
  307. {
  308. // Check for languages that require specialized knowledge to
  309. // construct fully-qualified names, that we don't yet support.
  310. if (this->cu_language_ == elfcpp::DW_LANG_Ada83
  311. || this->cu_language_ == elfcpp::DW_LANG_Fortran77
  312. || this->cu_language_ == elfcpp::DW_LANG_Fortran90
  313. || this->cu_language_ == elfcpp::DW_LANG_Java
  314. || this->cu_language_ == elfcpp::DW_LANG_Ada95
  315. || this->cu_language_ == elfcpp::DW_LANG_Fortran95
  316. || this->cu_language_ == elfcpp::DW_LANG_Fortran03
  317. || this->cu_language_ == elfcpp::DW_LANG_Fortran08)
  318. {
  319. gold_warning(_("%s: --gdb-index currently supports "
  320. "only C and C++ languages"),
  321. this->object()->name().c_str());
  322. return;
  323. }
  324. if (die->tag() == elfcpp::DW_TAG_compile_unit)
  325. ++Gdb_index_info_reader::dwarf_cu_nopubnames_count;
  326. else
  327. ++Gdb_index_info_reader::dwarf_tu_nopubnames_count;
  328. this->visit_children(die, NULL);
  329. }
  330. break;
  331. default:
  332. // The top level DIE should be one of the above.
  333. gold_warning(_("%s: top level DIE is not DW_TAG_compile_unit "
  334. "or DW_TAG_type_unit"),
  335. this->object()->name().c_str());
  336. return;
  337. }
  338. }
  339. // Visit the children of PARENT, looking for symbols to add to the index.
  340. // CONTEXT points to the DIE to use for constructing the qualified name --
  341. // NULL if PARENT is the top-level DIE; otherwise it is the same as PARENT.
  342. void
  343. Gdb_index_info_reader::visit_children(Dwarf_die* parent, Dwarf_die* context)
  344. {
  345. off_t next_offset = 0;
  346. for (off_t die_offset = parent->child_offset();
  347. die_offset != 0;
  348. die_offset = next_offset)
  349. {
  350. Dwarf_die die(this, die_offset, parent);
  351. if (die.tag() == 0)
  352. break;
  353. this->visit_die(&die, context);
  354. next_offset = die.sibling_offset();
  355. }
  356. }
  357. // Visit a child DIE, looking for symbols to add to the index.
  358. // CONTEXT is the parent DIE, used for constructing the qualified name;
  359. // it is NULL if the parent DIE is the top-level DIE.
  360. void
  361. Gdb_index_info_reader::visit_die(Dwarf_die* die, Dwarf_die* context)
  362. {
  363. switch (die->tag())
  364. {
  365. case elfcpp::DW_TAG_subprogram:
  366. case elfcpp::DW_TAG_constant:
  367. case elfcpp::DW_TAG_variable:
  368. case elfcpp::DW_TAG_enumerator:
  369. case elfcpp::DW_TAG_base_type:
  370. if (die->is_declaration())
  371. this->add_declaration(die, context);
  372. else
  373. {
  374. // If the DIE is not a declaration, add it to the index.
  375. std::string full_name = this->get_qualified_name(die, context);
  376. if (!full_name.empty())
  377. this->gdb_index_->add_symbol(this->cu_index_,
  378. full_name.c_str(), 0);
  379. }
  380. break;
  381. case elfcpp::DW_TAG_typedef:
  382. case elfcpp::DW_TAG_union_type:
  383. case elfcpp::DW_TAG_class_type:
  384. case elfcpp::DW_TAG_interface_type:
  385. case elfcpp::DW_TAG_structure_type:
  386. case elfcpp::DW_TAG_enumeration_type:
  387. case elfcpp::DW_TAG_subrange_type:
  388. case elfcpp::DW_TAG_namespace:
  389. {
  390. std::string full_name;
  391. // For classes at the top level, we need to look for a
  392. // member function with a linkage name in order to get
  393. // the properly-canonicalized name.
  394. if (context == NULL
  395. && (die->tag() == elfcpp::DW_TAG_class_type
  396. || die->tag() == elfcpp::DW_TAG_structure_type
  397. || die->tag() == elfcpp::DW_TAG_union_type))
  398. full_name.assign(this->guess_full_class_name(die));
  399. // Because we will visit the children, we need to add this DIE
  400. // to the declarations table.
  401. if (full_name.empty())
  402. this->add_declaration(die, context);
  403. else
  404. this->add_declaration_with_full_name(die, full_name.c_str());
  405. // If the DIE is not a declaration, add it to the index.
  406. // Gdb stores a namespace in the index even when it is
  407. // a declaration.
  408. if (die->tag() == elfcpp::DW_TAG_namespace
  409. || !die->is_declaration())
  410. {
  411. if (full_name.empty())
  412. full_name = this->get_qualified_name(die, context);
  413. if (!full_name.empty())
  414. this->gdb_index_->add_symbol(this->cu_index_,
  415. full_name.c_str(), 0);
  416. }
  417. // We're interested in the children only for namespaces and
  418. // enumeration types. For enumeration types, we do not include
  419. // the enumeration tag as part of the full name. For other tags,
  420. // visit the children only to collect declarations.
  421. if (die->tag() == elfcpp::DW_TAG_namespace
  422. || die->tag() == elfcpp::DW_TAG_enumeration_type)
  423. this->visit_children(die, die);
  424. else
  425. this->visit_children_for_decls(die);
  426. }
  427. break;
  428. default:
  429. break;
  430. }
  431. }
  432. // Visit the children of PARENT, looking only for declarations that
  433. // may be referenced by later specification DIEs.
  434. void
  435. Gdb_index_info_reader::visit_children_for_decls(Dwarf_die* parent)
  436. {
  437. off_t next_offset = 0;
  438. for (off_t die_offset = parent->child_offset();
  439. die_offset != 0;
  440. die_offset = next_offset)
  441. {
  442. Dwarf_die die(this, die_offset, parent);
  443. if (die.tag() == 0)
  444. break;
  445. this->visit_die_for_decls(&die, parent);
  446. next_offset = die.sibling_offset();
  447. }
  448. }
  449. // Visit a child DIE, looking only for declarations that
  450. // may be referenced by later specification DIEs.
  451. void
  452. Gdb_index_info_reader::visit_die_for_decls(Dwarf_die* die, Dwarf_die* context)
  453. {
  454. switch (die->tag())
  455. {
  456. case elfcpp::DW_TAG_subprogram:
  457. case elfcpp::DW_TAG_constant:
  458. case elfcpp::DW_TAG_variable:
  459. case elfcpp::DW_TAG_enumerator:
  460. case elfcpp::DW_TAG_base_type:
  461. {
  462. if (die->is_declaration())
  463. this->add_declaration(die, context);
  464. }
  465. break;
  466. case elfcpp::DW_TAG_typedef:
  467. case elfcpp::DW_TAG_union_type:
  468. case elfcpp::DW_TAG_class_type:
  469. case elfcpp::DW_TAG_interface_type:
  470. case elfcpp::DW_TAG_structure_type:
  471. case elfcpp::DW_TAG_enumeration_type:
  472. case elfcpp::DW_TAG_subrange_type:
  473. case elfcpp::DW_TAG_namespace:
  474. {
  475. if (die->is_declaration())
  476. this->add_declaration(die, context);
  477. this->visit_children_for_decls(die);
  478. }
  479. break;
  480. default:
  481. break;
  482. }
  483. }
  484. // Extract the class name from the linkage name of a member function.
  485. // This code is adapted from ../gdb/cp-support.c.
  486. #define d_left(dc) (dc)->u.s_binary.left
  487. #define d_right(dc) (dc)->u.s_binary.right
  488. static char*
  489. class_name_from_linkage_name(const char* linkage_name)
  490. {
  491. void* storage;
  492. struct demangle_component* tree =
  493. cplus_demangle_v3_components(linkage_name, DMGL_NO_OPTS, &storage);
  494. if (tree == NULL)
  495. return NULL;
  496. int done = 0;
  497. // First strip off any qualifiers, if we have a function or
  498. // method.
  499. while (!done)
  500. switch (tree->type)
  501. {
  502. case DEMANGLE_COMPONENT_CONST:
  503. case DEMANGLE_COMPONENT_RESTRICT:
  504. case DEMANGLE_COMPONENT_VOLATILE:
  505. case DEMANGLE_COMPONENT_CONST_THIS:
  506. case DEMANGLE_COMPONENT_RESTRICT_THIS:
  507. case DEMANGLE_COMPONENT_VOLATILE_THIS:
  508. case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
  509. tree = d_left(tree);
  510. break;
  511. default:
  512. done = 1;
  513. break;
  514. }
  515. // If what we have now is a function, discard the argument list.
  516. if (tree->type == DEMANGLE_COMPONENT_TYPED_NAME)
  517. tree = d_left(tree);
  518. // If what we have now is a template, strip off the template
  519. // arguments. The left subtree may be a qualified name.
  520. if (tree->type == DEMANGLE_COMPONENT_TEMPLATE)
  521. tree = d_left(tree);
  522. // What we have now should be a name, possibly qualified.
  523. // Additional qualifiers could live in the left subtree or the right
  524. // subtree. Find the last piece.
  525. done = 0;
  526. struct demangle_component* prev_comp = NULL;
  527. struct demangle_component* cur_comp = tree;
  528. while (!done)
  529. switch (cur_comp->type)
  530. {
  531. case DEMANGLE_COMPONENT_QUAL_NAME:
  532. case DEMANGLE_COMPONENT_LOCAL_NAME:
  533. prev_comp = cur_comp;
  534. cur_comp = d_right(cur_comp);
  535. break;
  536. case DEMANGLE_COMPONENT_TEMPLATE:
  537. case DEMANGLE_COMPONENT_NAME:
  538. case DEMANGLE_COMPONENT_CTOR:
  539. case DEMANGLE_COMPONENT_DTOR:
  540. case DEMANGLE_COMPONENT_OPERATOR:
  541. case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
  542. done = 1;
  543. break;
  544. default:
  545. done = 1;
  546. cur_comp = NULL;
  547. break;
  548. }
  549. char* ret = NULL;
  550. if (cur_comp != NULL && prev_comp != NULL)
  551. {
  552. // We want to discard the rightmost child of PREV_COMP.
  553. *prev_comp = *d_left(prev_comp);
  554. size_t allocated_size;
  555. ret = cplus_demangle_print(DMGL_NO_OPTS, tree, 30, &allocated_size);
  556. }
  557. free(storage);
  558. return ret;
  559. }
  560. // Guess a fully-qualified name for a class type, based on member function
  561. // linkage names. This is needed for class/struct/union types at the
  562. // top level, because GCC does not always properly embed them within
  563. // the namespace. As in gdb, we look for a member function with a linkage
  564. // name and extract the qualified name from the demangled name.
  565. std::string
  566. Gdb_index_info_reader::guess_full_class_name(Dwarf_die* die)
  567. {
  568. std::string full_name;
  569. off_t next_offset = 0;
  570. // This routine scans ahead in the DIE structure, possibly advancing
  571. // the relocation tracker beyond the current DIE. We need to checkpoint
  572. // the tracker and reset it when we're done.
  573. uint64_t checkpoint = this->get_reloc_checkpoint();
  574. for (off_t child_offset = die->child_offset();
  575. child_offset != 0;
  576. child_offset = next_offset)
  577. {
  578. Dwarf_die child(this, child_offset, die);
  579. if (child.tag() == 0)
  580. break;
  581. if (child.tag() == elfcpp::DW_TAG_subprogram)
  582. {
  583. const char* linkage_name = child.linkage_name();
  584. if (linkage_name != NULL)
  585. {
  586. char* guess = class_name_from_linkage_name(linkage_name);
  587. if (guess != NULL)
  588. {
  589. full_name.assign(guess);
  590. free(guess);
  591. break;
  592. }
  593. }
  594. }
  595. next_offset = child.sibling_offset();
  596. }
  597. this->reset_relocs(checkpoint);
  598. return full_name;
  599. }
  600. // Add a declaration DIE to the table of declarations.
  601. void
  602. Gdb_index_info_reader::add_declaration(Dwarf_die* die, Dwarf_die* context)
  603. {
  604. const char* name = die->name();
  605. off_t parent_offset = context != NULL ? context->offset() : 0;
  606. // If this DIE has a DW_AT_specification or DW_AT_abstract_origin
  607. // attribute, use the parent and name from the earlier declaration.
  608. off_t spec = die->specification();
  609. if (spec == 0)
  610. spec = die->abstract_origin();
  611. if (spec > 0)
  612. {
  613. Declaration_map::iterator it = this->declarations_.find(spec);
  614. if (it != this->declarations_.end())
  615. {
  616. parent_offset = it->second.parent_offset_;
  617. name = it->second.name_;
  618. }
  619. }
  620. if (name == NULL)
  621. {
  622. if (die->tag() == elfcpp::DW_TAG_namespace)
  623. name = "(anonymous namespace)";
  624. else if (die->tag() == elfcpp::DW_TAG_union_type)
  625. name = "(anonymous union)";
  626. else
  627. name = "(unknown)";
  628. }
  629. Declaration_pair decl(parent_offset, name);
  630. this->declarations_.insert(std::make_pair(die->offset(), decl));
  631. }
  632. // Add a declaration whose fully-qualified name is already known.
  633. // In the case where we had to get the canonical name by demangling
  634. // a linkage name, this ensures we use that name instead of the one
  635. // provided in DW_AT_name.
  636. void
  637. Gdb_index_info_reader::add_declaration_with_full_name(
  638. Dwarf_die* die,
  639. const char* full_name)
  640. {
  641. // We need to copy the name.
  642. int len = strlen(full_name);
  643. char* copy = new char[len + 1];
  644. memcpy(copy, full_name, len + 1);
  645. // Flag that we now manage the memory this points to.
  646. Declaration_pair decl(-1, copy);
  647. this->declarations_.insert(std::make_pair(die->offset(), decl));
  648. }
  649. // Return the context for a DIE whose parent is at DIE_OFFSET.
  650. std::string
  651. Gdb_index_info_reader::get_context(off_t die_offset)
  652. {
  653. std::string context;
  654. Declaration_map::iterator it = this->declarations_.find(die_offset);
  655. if (it != this->declarations_.end())
  656. {
  657. off_t parent_offset = it->second.parent_offset_;
  658. if (parent_offset > 0)
  659. {
  660. context = get_context(parent_offset);
  661. context.append("::");
  662. }
  663. if (it->second.name_ != NULL)
  664. context.append(it->second.name_);
  665. }
  666. return context;
  667. }
  668. // Construct the fully-qualified name for DIE.
  669. std::string
  670. Gdb_index_info_reader::get_qualified_name(Dwarf_die* die, Dwarf_die* context)
  671. {
  672. std::string full_name;
  673. const char* name = die->name();
  674. off_t parent_offset = context != NULL ? context->offset() : 0;
  675. // If this DIE has a DW_AT_specification or DW_AT_abstract_origin
  676. // attribute, use the parent and name from the earlier declaration.
  677. off_t spec = die->specification();
  678. if (spec == 0)
  679. spec = die->abstract_origin();
  680. if (spec > 0)
  681. {
  682. Declaration_map::iterator it = this->declarations_.find(spec);
  683. if (it != this->declarations_.end())
  684. {
  685. parent_offset = it->second.parent_offset_;
  686. name = it->second.name_;
  687. }
  688. }
  689. if (name == NULL && die->tag() == elfcpp::DW_TAG_namespace)
  690. name = "(anonymous namespace)";
  691. else if (name == NULL)
  692. return full_name;
  693. // If this is an enumerator constant, skip the immediate parent,
  694. // which is the enumeration tag.
  695. if (die->tag() == elfcpp::DW_TAG_enumerator)
  696. {
  697. Declaration_map::iterator it = this->declarations_.find(parent_offset);
  698. if (it != this->declarations_.end())
  699. parent_offset = it->second.parent_offset_;
  700. }
  701. if (parent_offset > 0)
  702. {
  703. full_name.assign(this->get_context(parent_offset));
  704. full_name.append("::");
  705. }
  706. full_name.append(name);
  707. return full_name;
  708. }
  709. // Record the address ranges for a compilation unit.
  710. void
  711. Gdb_index_info_reader::record_cu_ranges(Dwarf_die* die)
  712. {
  713. unsigned int shndx;
  714. unsigned int shndx2;
  715. off_t ranges_offset = die->ref_attribute(elfcpp::DW_AT_ranges, &shndx);
  716. if (ranges_offset != -1)
  717. {
  718. Dwarf_range_list* ranges = this->read_range_list(shndx, ranges_offset);
  719. if (ranges != NULL)
  720. this->gdb_index_->add_address_range_list(this->object(),
  721. this->cu_index_, ranges);
  722. return;
  723. }
  724. off_t low_pc = die->address_attribute(elfcpp::DW_AT_low_pc, &shndx);
  725. off_t high_pc = die->address_attribute(elfcpp::DW_AT_high_pc, &shndx2);
  726. if (high_pc == -1)
  727. {
  728. high_pc = die->uint_attribute(elfcpp::DW_AT_high_pc);
  729. high_pc += low_pc;
  730. shndx2 = shndx;
  731. }
  732. if ((low_pc != 0 || high_pc != 0) && low_pc != -1)
  733. {
  734. if (shndx != shndx2)
  735. {
  736. gold_warning(_("%s: DWARF info may be corrupt; low_pc and high_pc "
  737. "are in different sections"),
  738. this->object()->name().c_str());
  739. return;
  740. }
  741. if (shndx == 0 || this->object()->is_section_included(shndx))
  742. {
  743. Dwarf_range_list* ranges = new Dwarf_range_list();
  744. ranges->add(shndx, low_pc, high_pc);
  745. this->gdb_index_->add_address_range_list(this->object(),
  746. this->cu_index_, ranges);
  747. }
  748. }
  749. }
  750. // Read table and add the relevant names to the index. Returns true
  751. // if any names were added.
  752. bool
  753. Gdb_index_info_reader::read_pubtable(Dwarf_pubnames_table* table, off_t offset)
  754. {
  755. // If we couldn't read the section when building the cu_pubname_map,
  756. // then we won't find any pubnames now.
  757. if (table == NULL)
  758. return false;
  759. if (!table->read_header(offset))
  760. return false;
  761. while (true)
  762. {
  763. uint8_t flag_byte;
  764. const char* name = table->next_name(&flag_byte);
  765. if (name == NULL)
  766. break;
  767. this->gdb_index_->add_symbol(this->cu_index_, name, flag_byte);
  768. }
  769. return true;
  770. }
  771. // Read the .debug_pubnames and .debug_pubtypes tables for the CU or TU.
  772. // Returns TRUE if either a pubnames or pubtypes section was found.
  773. bool
  774. Gdb_index_info_reader::read_pubnames_and_pubtypes(Dwarf_die* die)
  775. {
  776. // If this is a skeleton debug-type die (generated via
  777. // -gsplit-dwarf), then the associated pubnames should have been
  778. // read along with the corresponding CU. In any case, there isn't
  779. // enough info inside to build a gdb index entry.
  780. if (die->tag() == elfcpp::DW_TAG_type_unit
  781. && die->string_attribute(elfcpp::DW_AT_GNU_dwo_name))
  782. return true;
  783. // We use stmt_list_off as a unique identifier for the
  784. // compilation unit and its associated type units.
  785. unsigned int shndx;
  786. off_t stmt_list_off = die->ref_attribute (elfcpp::DW_AT_stmt_list,
  787. &shndx);
  788. // Look for the attr as either a flag or a ref.
  789. off_t offset = die->ref_attribute(elfcpp::DW_AT_GNU_pubnames, &shndx);
  790. // Newer versions of GCC generate CUs, but not TUs, with
  791. // DW_AT_FORM_flag_present.
  792. unsigned int flag = die->uint_attribute(elfcpp::DW_AT_GNU_pubnames);
  793. if (offset == -1 && flag == 0)
  794. {
  795. // Didn't find the attribute.
  796. if (die->tag() == elfcpp::DW_TAG_type_unit)
  797. {
  798. // If die is a TU, then it might correspond to a CU which we
  799. // have read. If it does, then no need to read the pubnames.
  800. // If it doesn't, then the caller will have to parse the
  801. // dies manually to find the names.
  802. return this->gdb_index_->pubnames_read(this->object(),
  803. stmt_list_off);
  804. }
  805. else
  806. {
  807. // No attribute on the CU means that no pubnames were read.
  808. return false;
  809. }
  810. }
  811. // We found the attribute, so we can check if the corresponding
  812. // pubnames have been read.
  813. if (this->gdb_index_->pubnames_read(this->object(), stmt_list_off))
  814. return true;
  815. this->gdb_index_->set_pubnames_read(this->object(), stmt_list_off);
  816. // We have an attribute, and the pubnames haven't been read, so read
  817. // them.
  818. bool names = false;
  819. // In some of the cases, we could rely on the previous value of
  820. // offset here, but sorting out which cases complicates the logic
  821. // enough that it isn't worth it. So just look up the offset again.
  822. offset = this->gdb_index_->find_pubname_offset(this->cu_offset());
  823. names = this->read_pubtable(this->gdb_index_->pubnames_table(), offset);
  824. bool types = false;
  825. offset = this->gdb_index_->find_pubtype_offset(this->cu_offset());
  826. types = this->read_pubtable(this->gdb_index_->pubtypes_table(), offset);
  827. return names || types;
  828. }
  829. // Clear the declarations map.
  830. void
  831. Gdb_index_info_reader::clear_declarations()
  832. {
  833. // Free strings in memory we manage.
  834. for (Declaration_map::iterator it = this->declarations_.begin();
  835. it != this->declarations_.end();
  836. ++it)
  837. {
  838. if (it->second.parent_offset_ == -1)
  839. delete[] it->second.name_;
  840. }
  841. this->declarations_.clear();
  842. }
  843. // Print usage statistics.
  844. void
  845. Gdb_index_info_reader::print_stats()
  846. {
  847. fprintf(stderr, _("%s: DWARF CUs: %u\n"),
  848. program_name, Gdb_index_info_reader::dwarf_cu_count);
  849. fprintf(stderr, _("%s: DWARF CUs without pubnames/pubtypes: %u\n"),
  850. program_name, Gdb_index_info_reader::dwarf_cu_nopubnames_count);
  851. fprintf(stderr, _("%s: DWARF TUs: %u\n"),
  852. program_name, Gdb_index_info_reader::dwarf_tu_count);
  853. fprintf(stderr, _("%s: DWARF TUs without pubnames/pubtypes: %u\n"),
  854. program_name, Gdb_index_info_reader::dwarf_tu_nopubnames_count);
  855. }
  856. // Class Gdb_index.
  857. // Construct the .gdb_index section.
  858. Gdb_index::Gdb_index(Output_section* gdb_index_section)
  859. : Output_section_data(4),
  860. pubnames_table_(NULL),
  861. pubtypes_table_(NULL),
  862. gdb_index_section_(gdb_index_section),
  863. comp_units_(),
  864. type_units_(),
  865. ranges_(),
  866. cu_vector_list_(),
  867. cu_vector_offsets_(NULL),
  868. stringpool_(),
  869. tu_offset_(0),
  870. addr_offset_(0),
  871. symtab_offset_(0),
  872. cu_pool_offset_(0),
  873. stringpool_offset_(0),
  874. pubnames_object_(NULL),
  875. stmt_list_offset_(-1)
  876. {
  877. this->gdb_symtab_ = new Gdb_hashtab<Gdb_symbol>();
  878. }
  879. Gdb_index::~Gdb_index()
  880. {
  881. // Free the memory used by the symbol table.
  882. delete this->gdb_symtab_;
  883. // Free the memory used by the CU vectors.
  884. for (unsigned int i = 0; i < this->cu_vector_list_.size(); ++i)
  885. delete this->cu_vector_list_[i];
  886. }
  887. // Scan the pubnames and pubtypes sections and build a map of the
  888. // various cus and tus they refer to, so we can process the entries
  889. // when we encounter the die for that cu or tu.
  890. // Return the just-read table so it can be cached.
  891. Dwarf_pubnames_table*
  892. Gdb_index::map_pubtable_to_dies(unsigned int attr,
  893. Gdb_index_info_reader* dwinfo,
  894. Relobj* object,
  895. const unsigned char* symbols,
  896. off_t symbols_size)
  897. {
  898. uint64_t section_offset = 0;
  899. Dwarf_pubnames_table* table;
  900. Pubname_offset_map* map;
  901. if (attr == elfcpp::DW_AT_GNU_pubnames)
  902. {
  903. table = new Dwarf_pubnames_table(dwinfo, false);
  904. map = &this->cu_pubname_map_;
  905. }
  906. else
  907. {
  908. table = new Dwarf_pubnames_table(dwinfo, true);
  909. map = &this->cu_pubtype_map_;
  910. }
  911. map->clear();
  912. if (!table->read_section(object, symbols, symbols_size))
  913. return NULL;
  914. while (table->read_header(section_offset))
  915. {
  916. map->insert(std::make_pair(table->cu_offset(), section_offset));
  917. section_offset += table->subsection_size();
  918. }
  919. return table;
  920. }
  921. // Wrapper for map_pubtable_to_dies
  922. void
  923. Gdb_index::map_pubnames_and_types_to_dies(Gdb_index_info_reader* dwinfo,
  924. Relobj* object,
  925. const unsigned char* symbols,
  926. off_t symbols_size)
  927. {
  928. // This is a new object, so reset the relevant variables.
  929. this->pubnames_object_ = object;
  930. this->stmt_list_offset_ = -1;
  931. delete this->pubnames_table_;
  932. this->pubnames_table_
  933. = this->map_pubtable_to_dies(elfcpp::DW_AT_GNU_pubnames, dwinfo,
  934. object, symbols, symbols_size);
  935. delete this->pubtypes_table_;
  936. this->pubtypes_table_
  937. = this->map_pubtable_to_dies(elfcpp::DW_AT_GNU_pubtypes, dwinfo,
  938. object, symbols, symbols_size);
  939. }
  940. // Given a cu_offset, find the associated section of the pubnames
  941. // table.
  942. off_t
  943. Gdb_index::find_pubname_offset(off_t cu_offset)
  944. {
  945. Pubname_offset_map::iterator it = this->cu_pubname_map_.find(cu_offset);
  946. if (it != this->cu_pubname_map_.end())
  947. return it->second;
  948. return -1;
  949. }
  950. // Given a cu_offset, find the associated section of the pubnames
  951. // table.
  952. off_t
  953. Gdb_index::find_pubtype_offset(off_t cu_offset)
  954. {
  955. Pubname_offset_map::iterator it = this->cu_pubtype_map_.find(cu_offset);
  956. if (it != this->cu_pubtype_map_.end())
  957. return it->second;
  958. return -1;
  959. }
  960. // Scan a .debug_info or .debug_types input section.
  961. void
  962. Gdb_index::scan_debug_info(bool is_type_unit,
  963. Relobj* object,
  964. const unsigned char* symbols,
  965. off_t symbols_size,
  966. unsigned int shndx,
  967. unsigned int reloc_shndx,
  968. unsigned int reloc_type)
  969. {
  970. Gdb_index_info_reader dwinfo(is_type_unit, object,
  971. symbols, symbols_size,
  972. shndx, reloc_shndx,
  973. reloc_type, this);
  974. if (object != this->pubnames_object_)
  975. map_pubnames_and_types_to_dies(&dwinfo, object, symbols, symbols_size);
  976. dwinfo.parse();
  977. }
  978. // Add a symbol.
  979. void
  980. Gdb_index::add_symbol(int cu_index, const char* sym_name, uint8_t flags)
  981. {
  982. unsigned int hash = mapped_index_string_hash(
  983. reinterpret_cast<const unsigned char*>(sym_name));
  984. Gdb_symbol* sym = new Gdb_symbol();
  985. this->stringpool_.add(sym_name, true, &sym->name_key);
  986. sym->hashval = hash;
  987. sym->cu_vector_index = 0;
  988. Gdb_symbol* found = this->gdb_symtab_->add(sym);
  989. if (found == sym)
  990. {
  991. // New symbol -- allocate a new CU index vector.
  992. found->cu_vector_index = this->cu_vector_list_.size();
  993. this->cu_vector_list_.push_back(new Cu_vector());
  994. }
  995. else
  996. {
  997. // Found an existing symbol -- append to the existing
  998. // CU index vector.
  999. delete sym;
  1000. }
  1001. // Add the CU index to the vector list for this symbol,
  1002. // if it's not already on the list. We only need to
  1003. // check the last added entry.
  1004. Cu_vector* cu_vec = this->cu_vector_list_[found->cu_vector_index];
  1005. if (cu_vec->size() == 0
  1006. || cu_vec->back().first != cu_index
  1007. || cu_vec->back().second != flags)
  1008. cu_vec->push_back(std::make_pair(cu_index, flags));
  1009. }
  1010. // Return TRUE if we have already processed the pubnames associated
  1011. // with the statement list at the given OFFSET.
  1012. bool
  1013. Gdb_index::pubnames_read(const Relobj* object, off_t offset)
  1014. {
  1015. bool ret = (this->pubnames_object_ == object
  1016. && this->stmt_list_offset_ == offset);
  1017. return ret;
  1018. }
  1019. // Record that we have processed the pubnames associated with the
  1020. // statement list for OBJECT at the given OFFSET.
  1021. void
  1022. Gdb_index::set_pubnames_read(const Relobj* object, off_t offset)
  1023. {
  1024. this->pubnames_object_ = object;
  1025. this->stmt_list_offset_ = offset;
  1026. }
  1027. // Set the size of the .gdb_index section.
  1028. void
  1029. Gdb_index::set_final_data_size()
  1030. {
  1031. // Finalize the string pool.
  1032. this->stringpool_.set_string_offsets();
  1033. // Compute the total size of the CU vectors.
  1034. // For each CU vector, include one entry for the count at the
  1035. // beginning of the vector.
  1036. unsigned int cu_vector_count = this->cu_vector_list_.size();
  1037. unsigned int cu_vector_size = 0;
  1038. this->cu_vector_offsets_ = new off_t[cu_vector_count];
  1039. for (unsigned int i = 0; i < cu_vector_count; ++i)
  1040. {
  1041. Cu_vector* cu_vec = this->cu_vector_list_[i];
  1042. cu_vector_offsets_[i] = cu_vector_size;
  1043. cu_vector_size += gdb_index_offset_size * (cu_vec->size() + 1);
  1044. }
  1045. // Assign relative offsets to each portion of the index,
  1046. // and find the total size of the section.
  1047. section_size_type data_size = gdb_index_hdr_size;
  1048. data_size += this->comp_units_.size() * gdb_index_cu_size;
  1049. this->tu_offset_ = data_size;
  1050. data_size += this->type_units_.size() * gdb_index_tu_size;
  1051. this->addr_offset_ = data_size;
  1052. for (unsigned int i = 0; i < this->ranges_.size(); ++i)
  1053. data_size += this->ranges_[i].ranges->size() * gdb_index_addr_size;
  1054. this->symtab_offset_ = data_size;
  1055. data_size += this->gdb_symtab_->capacity() * gdb_index_sym_size;
  1056. this->cu_pool_offset_ = data_size;
  1057. data_size += cu_vector_size;
  1058. this->stringpool_offset_ = data_size;
  1059. data_size += this->stringpool_.get_strtab_size();
  1060. this->set_data_size(data_size);
  1061. }
  1062. // Write the data to the file.
  1063. void
  1064. Gdb_index::do_write(Output_file* of)
  1065. {
  1066. const off_t off = this->offset();
  1067. const off_t oview_size = this->data_size();
  1068. unsigned char* const oview = of->get_output_view(off, oview_size);
  1069. unsigned char* pov = oview;
  1070. // Write the file header.
  1071. // (1) Version number.
  1072. elfcpp::Swap<32, false>::writeval(pov, gdb_index_version);
  1073. pov += 4;
  1074. // (2) Offset of the CU list.
  1075. elfcpp::Swap<32, false>::writeval(pov, gdb_index_hdr_size);
  1076. pov += 4;
  1077. // (3) Offset of the types CU list.
  1078. elfcpp::Swap<32, false>::writeval(pov, this->tu_offset_);
  1079. pov += 4;
  1080. // (4) Offset of the address area.
  1081. elfcpp::Swap<32, false>::writeval(pov, this->addr_offset_);
  1082. pov += 4;
  1083. // (5) Offset of the symbol table.
  1084. elfcpp::Swap<32, false>::writeval(pov, this->symtab_offset_);
  1085. pov += 4;
  1086. // (6) Offset of the constant pool.
  1087. elfcpp::Swap<32, false>::writeval(pov, this->cu_pool_offset_);
  1088. pov += 4;
  1089. gold_assert(pov - oview == gdb_index_hdr_size);
  1090. // Write the CU list.
  1091. unsigned int comp_units_count = this->comp_units_.size();
  1092. for (unsigned int i = 0; i < comp_units_count; ++i)
  1093. {
  1094. const Comp_unit& cu = this->comp_units_[i];
  1095. elfcpp::Swap<64, false>::writeval(pov, cu.cu_offset);
  1096. elfcpp::Swap<64, false>::writeval(pov + 8, cu.cu_length);
  1097. pov += 16;
  1098. }
  1099. gold_assert(pov - oview == this->tu_offset_);
  1100. // Write the types CU list.
  1101. for (unsigned int i = 0; i < this->type_units_.size(); ++i)
  1102. {
  1103. const Type_unit& tu = this->type_units_[i];
  1104. elfcpp::Swap<64, false>::writeval(pov, tu.tu_offset);
  1105. elfcpp::Swap<64, false>::writeval(pov + 8, tu.type_offset);
  1106. elfcpp::Swap<64, false>::writeval(pov + 16, tu.type_signature);
  1107. pov += 24;
  1108. }
  1109. gold_assert(pov - oview == this->addr_offset_);
  1110. // Write the address area.
  1111. for (unsigned int i = 0; i < this->ranges_.size(); ++i)
  1112. {
  1113. int cu_index = this->ranges_[i].cu_index;
  1114. // Translate negative indexes, which refer to a TU, to a
  1115. // logical index into a concatenated CU/TU list.
  1116. if (cu_index < 0)
  1117. cu_index = comp_units_count + (-1 - cu_index);
  1118. Relobj* object = this->ranges_[i].object;
  1119. const Dwarf_range_list& ranges = *this->ranges_[i].ranges;
  1120. for (unsigned int j = 0; j < ranges.size(); ++j)
  1121. {
  1122. const Dwarf_range_list::Range& range = ranges[j];
  1123. uint64_t base = 0;
  1124. if (range.shndx > 0)
  1125. {
  1126. const Output_section* os = object->output_section(range.shndx);
  1127. base = (os->address()
  1128. + object->output_section_offset(range.shndx));
  1129. }
  1130. elfcpp::Swap_aligned32<64, false>::writeval(pov, base + range.start);
  1131. elfcpp::Swap_aligned32<64, false>::writeval(pov + 8,
  1132. base + range.end);
  1133. elfcpp::Swap<32, false>::writeval(pov + 16, cu_index);
  1134. pov += 20;
  1135. }
  1136. }
  1137. gold_assert(pov - oview == this->symtab_offset_);
  1138. // Write the symbol table.
  1139. for (unsigned int i = 0; i < this->gdb_symtab_->capacity(); ++i)
  1140. {
  1141. const Gdb_symbol* sym = (*this->gdb_symtab_)[i];
  1142. section_offset_type name_offset = 0;
  1143. unsigned int cu_vector_offset = 0;
  1144. if (sym != NULL)
  1145. {
  1146. name_offset = (this->stringpool_.get_offset_from_key(sym->name_key)
  1147. + this->stringpool_offset_ - this->cu_pool_offset_);
  1148. cu_vector_offset = this->cu_vector_offsets_[sym->cu_vector_index];
  1149. }
  1150. elfcpp::Swap<32, false>::writeval(pov, name_offset);
  1151. elfcpp::Swap<32, false>::writeval(pov + 4, cu_vector_offset);
  1152. pov += 8;
  1153. }
  1154. gold_assert(pov - oview == this->cu_pool_offset_);
  1155. // Write the CU vectors into the constant pool.
  1156. for (unsigned int i = 0; i < this->cu_vector_list_.size(); ++i)
  1157. {
  1158. Cu_vector* cu_vec = this->cu_vector_list_[i];
  1159. elfcpp::Swap<32, false>::writeval(pov, cu_vec->size());
  1160. pov += 4;
  1161. for (unsigned int j = 0; j < cu_vec->size(); ++j)
  1162. {
  1163. int cu_index = (*cu_vec)[j].first;
  1164. uint8_t flags = (*cu_vec)[j].second;
  1165. if (cu_index < 0)
  1166. cu_index = comp_units_count + (-1 - cu_index);
  1167. cu_index |= flags << 24;
  1168. elfcpp::Swap<32, false>::writeval(pov, cu_index);
  1169. pov += 4;
  1170. }
  1171. }
  1172. gold_assert(pov - oview == this->stringpool_offset_);
  1173. // Write the strings into the constant pool.
  1174. this->stringpool_.write_to_buffer(pov, oview_size - this->stringpool_offset_);
  1175. of->write_output_view(off, oview_size, oview);
  1176. }
  1177. // Print usage statistics.
  1178. void
  1179. Gdb_index::print_stats()
  1180. {
  1181. if (parameters->options().gdb_index())
  1182. Gdb_index_info_reader::print_stats();
  1183. }
  1184. } // End namespace gold.