gdb-index.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // gdb-index.h -- generate .gdb_index section for fast debug lookup -*- C++ -*-
  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 <sys/types.h>
  18. #include <vector>
  19. #include "gold.h"
  20. #include "output.h"
  21. #include "mapfile.h"
  22. #include "stringpool.h"
  23. #ifndef GOLD_GDB_INDEX_H
  24. #define GOLD_GDB_INDEX_H
  25. namespace gold
  26. {
  27. class Output_section;
  28. class Output_file;
  29. class Mapfile;
  30. template<int size, bool big_endian>
  31. class Sized_relobj;
  32. class Dwarf_range_list;
  33. template <typename T>
  34. class Gdb_hashtab;
  35. class Gdb_index_info_reader;
  36. class Dwarf_pubnames_table;
  37. // This class manages the .gdb_index section, which is a fast
  38. // lookup table for DWARF information used by the gdb debugger.
  39. // The format of this section is described in gdb/doc/gdb.texinfo.
  40. class Gdb_index : public Output_section_data
  41. {
  42. public:
  43. Gdb_index(Output_section* gdb_index_section);
  44. ~Gdb_index();
  45. // Scan a .debug_info or .debug_types input section.
  46. void scan_debug_info(bool is_type_unit,
  47. Relobj* object,
  48. const unsigned char* symbols,
  49. off_t symbols_size,
  50. unsigned int shndx,
  51. unsigned int reloc_shndx,
  52. unsigned int reloc_type);
  53. // Add a compilation unit.
  54. int
  55. add_comp_unit(off_t cu_offset, off_t cu_length)
  56. {
  57. this->comp_units_.push_back(Comp_unit(cu_offset, cu_length));
  58. return this->comp_units_.size() - 1;
  59. }
  60. // Add a type unit.
  61. int
  62. add_type_unit(off_t tu_offset, off_t type_offset, uint64_t signature)
  63. {
  64. this->type_units_.push_back(Type_unit(tu_offset, type_offset, signature));
  65. return this->type_units_.size() - 1;
  66. }
  67. // Add an address range.
  68. void
  69. add_address_range_list(Relobj* object, unsigned int cu_index,
  70. Dwarf_range_list* ranges)
  71. {
  72. this->ranges_.push_back(Per_cu_range_list(object, cu_index, ranges));
  73. }
  74. // Add a symbol. FLAGS are the gdb_index version 7 flags to be stored in
  75. // the high-byte of the cu_index field.
  76. void
  77. add_symbol(int cu_index, const char* sym_name, uint8_t flags);
  78. // Return the offset into the pubnames table for the cu at the given
  79. // offset.
  80. off_t
  81. find_pubname_offset(off_t cu_offset);
  82. // Return the offset into the pubtypes table for the cu at the
  83. // given offset.
  84. off_t
  85. find_pubtype_offset(off_t cu_offset);
  86. // Return TRUE if we have already processed the pubnames and types
  87. // set for OBJECT of the CUs and TUS associated with the statement
  88. // list at OFFSET.
  89. bool
  90. pubnames_read(const Relobj* object, off_t offset);
  91. // Record that we have already read the pubnames associated with
  92. // OBJECT and OFFSET.
  93. void
  94. set_pubnames_read(const Relobj* object, off_t offset);
  95. // Return a pointer to the given table.
  96. Dwarf_pubnames_table*
  97. pubnames_table()
  98. { return pubnames_table_; }
  99. Dwarf_pubnames_table*
  100. pubtypes_table()
  101. { return pubtypes_table_; }
  102. // Print usage statistics.
  103. static void
  104. print_stats();
  105. protected:
  106. // This is called to update the section size prior to assigning
  107. // the address and file offset.
  108. void
  109. update_data_size()
  110. { this->set_final_data_size(); }
  111. // Set the final data size.
  112. void
  113. set_final_data_size();
  114. // Write the data to the file.
  115. void
  116. do_write(Output_file*);
  117. // Write to a map file.
  118. void
  119. do_print_to_mapfile(Mapfile* mapfile) const
  120. { mapfile->print_output_data(this, _("** gdb_index")); }
  121. // Create a map from dies to pubnames.
  122. Dwarf_pubnames_table*
  123. map_pubtable_to_dies(unsigned int attr,
  124. Gdb_index_info_reader* dwinfo,
  125. Relobj* object,
  126. const unsigned char* symbols,
  127. off_t symbols_size);
  128. // Wrapper for map_pubtable_to_dies
  129. void
  130. map_pubnames_and_types_to_dies(Gdb_index_info_reader* dwinfo,
  131. Relobj* object,
  132. const unsigned char* symbols,
  133. off_t symbols_size);
  134. private:
  135. // An entry in the compilation unit list.
  136. struct Comp_unit
  137. {
  138. Comp_unit(off_t off, off_t len)
  139. : cu_offset(off), cu_length(len)
  140. { }
  141. uint64_t cu_offset;
  142. uint64_t cu_length;
  143. };
  144. // An entry in the type unit list.
  145. struct Type_unit
  146. {
  147. Type_unit(off_t off, off_t toff, uint64_t sig)
  148. : tu_offset(off), type_offset(toff), type_signature(sig)
  149. { }
  150. uint64_t tu_offset;
  151. uint64_t type_offset;
  152. uint64_t type_signature;
  153. };
  154. // An entry in the address range list.
  155. struct Per_cu_range_list
  156. {
  157. Per_cu_range_list(Relobj* obj, uint32_t index, Dwarf_range_list* r)
  158. : object(obj), cu_index(index), ranges(r)
  159. { }
  160. Relobj* object;
  161. uint32_t cu_index;
  162. Dwarf_range_list* ranges;
  163. };
  164. // A symbol table entry.
  165. struct Gdb_symbol
  166. {
  167. Stringpool::Key name_key;
  168. unsigned int hashval;
  169. unsigned int cu_vector_index;
  170. // Return the hash value.
  171. unsigned int
  172. hash()
  173. { return this->hashval; }
  174. // Return true if this symbol is the same as SYMBOL.
  175. bool
  176. equal(Gdb_symbol* symbol)
  177. { return this->name_key == symbol->name_key; }
  178. };
  179. typedef std::vector<std::pair<int, uint8_t> > Cu_vector;
  180. typedef Unordered_map<off_t, off_t> Pubname_offset_map;
  181. Pubname_offset_map cu_pubname_map_;
  182. Pubname_offset_map cu_pubtype_map_;
  183. // Scan the given pubtable and build a map of the various dies it
  184. // refers to, so we can process the entries when we encounter the
  185. // die.
  186. void
  187. map_pubtable_to_dies(Dwarf_pubnames_table* table,
  188. Pubname_offset_map* map);
  189. // Tables to store the pubnames section of the current object.
  190. Dwarf_pubnames_table* pubnames_table_;
  191. Dwarf_pubnames_table* pubtypes_table_;
  192. // The .gdb_index section.
  193. Output_section* gdb_index_section_;
  194. // The list of DWARF compilation units.
  195. std::vector<Comp_unit> comp_units_;
  196. // The list of DWARF type units.
  197. std::vector<Type_unit> type_units_;
  198. // The list of address ranges.
  199. std::vector<Per_cu_range_list> ranges_;
  200. // The symbol table.
  201. Gdb_hashtab<Gdb_symbol>* gdb_symtab_;
  202. // The CU vector portion of the constant pool.
  203. std::vector<Cu_vector*> cu_vector_list_;
  204. // An array to map from a CU vector index to an offset to the constant pool.
  205. off_t* cu_vector_offsets_;
  206. // The string portion of the constant pool.
  207. Stringpool stringpool_;
  208. // Offsets of the various pieces of the .gdb_index section.
  209. off_t tu_offset_;
  210. off_t addr_offset_;
  211. off_t symtab_offset_;
  212. off_t cu_pool_offset_;
  213. off_t stringpool_offset_;
  214. // Object, stmt list offset of the CUs and TUs associated with the
  215. // last read pubnames and pubtypes sections.
  216. const Relobj* pubnames_object_;
  217. off_t stmt_list_offset_;
  218. };
  219. } // End namespace gold.
  220. #endif // !defined(GOLD_GDB_INDEX_H)