1 1.1 christos /* DWARF index writing support for GDB. 2 1.1 christos 3 1.1.1.3 christos Copyright (C) 1994-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This file is part of GDB. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 1.1 christos 20 1.1 christos 21 1.1 christos #include "dwarf2/index-write.h" 22 1.1 christos 23 1.1 christos #include "addrmap.h" 24 1.1 christos #include "cli/cli-decode.h" 25 1.1 christos #include "gdbsupport/byte-vector.h" 26 1.1 christos #include "gdbsupport/filestuff.h" 27 1.1 christos #include "gdbsupport/gdb_unlinker.h" 28 1.1 christos #include "gdbsupport/pathstuff.h" 29 1.1 christos #include "gdbsupport/scoped_fd.h" 30 1.1 christos #include "complaints.h" 31 1.1 christos #include "dwarf2/index-common.h" 32 1.1.1.3 christos #include "dwarf2/cooked-index.h" 33 1.1 christos #include "dwarf2.h" 34 1.1 christos #include "dwarf2/read.h" 35 1.1 christos #include "dwarf2/dwz.h" 36 1.1 christos #include "gdb/gdb-index.h" 37 1.1.1.3 christos #include "cli/cli-cmds.h" 38 1.1 christos #include "objfiles.h" 39 1.1 christos #include "ada-lang.h" 40 1.1.1.2 christos #include "dwarf2/tag.h" 41 1.1.1.3 christos #include "gdbsupport/gdb_tilde_expand.h" 42 1.1.1.3 christos #include "dwarf2/read-debug-names.h" 43 1.1 christos 44 1.1 christos #include <algorithm> 45 1.1 christos #include <cmath> 46 1.1 christos #include <forward_list> 47 1.1.1.3 christos #include <map> 48 1.1 christos #include <set> 49 1.1 christos #include <unordered_map> 50 1.1 christos #include <unordered_set> 51 1.1 christos 52 1.1 christos /* Ensure only legit values are used. */ 53 1.1 christos #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \ 54 1.1 christos do { \ 55 1.1 christos gdb_assert ((unsigned int) (value) <= 1); \ 56 1.1 christos GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \ 57 1.1 christos } while (0) 58 1.1 christos 59 1.1 christos /* Ensure only legit values are used. */ 60 1.1 christos #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \ 61 1.1 christos do { \ 62 1.1 christos gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \ 63 1.1.1.2 christos && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \ 64 1.1 christos GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \ 65 1.1 christos } while (0) 66 1.1 christos 67 1.1 christos /* Ensure we don't use more than the allotted number of bits for the CU. */ 68 1.1 christos #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \ 69 1.1 christos do { \ 70 1.1 christos gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \ 71 1.1 christos GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \ 72 1.1 christos } while (0) 73 1.1 christos 74 1.1 christos /* The "save gdb-index" command. */ 75 1.1 christos 76 1.1 christos /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with 77 1.1 christos error checking. */ 78 1.1 christos 79 1.1 christos static void 80 1.1 christos file_write (FILE *file, const void *data, size_t size) 81 1.1 christos { 82 1.1 christos if (fwrite (data, 1, size, file) != size) 83 1.1 christos error (_("couldn't data write to file")); 84 1.1 christos } 85 1.1 christos 86 1.1 christos /* Write the contents of VEC to FILE, with error checking. */ 87 1.1 christos 88 1.1 christos template<typename Elem, typename Alloc> 89 1.1 christos static void 90 1.1 christos file_write (FILE *file, const std::vector<Elem, Alloc> &vec) 91 1.1 christos { 92 1.1 christos if (!vec.empty ()) 93 1.1 christos file_write (file, vec.data (), vec.size () * sizeof (vec[0])); 94 1.1 christos } 95 1.1 christos 96 1.1 christos /* In-memory buffer to prepare data to be written later to a file. */ 97 1.1 christos class data_buf 98 1.1 christos { 99 1.1 christos public: 100 1.1.1.2 christos /* Copy ARRAY to the end of the buffer. */ 101 1.1.1.2 christos void append_array (gdb::array_view<const gdb_byte> array) 102 1.1.1.2 christos { 103 1.1.1.2 christos std::copy (array.begin (), array.end (), grow (array.size ())); 104 1.1 christos } 105 1.1 christos 106 1.1 christos /* Copy CSTR (a zero-terminated string) to the end of buffer. The 107 1.1 christos terminating zero is appended too. */ 108 1.1 christos void append_cstr0 (const char *cstr) 109 1.1 christos { 110 1.1 christos const size_t size = strlen (cstr) + 1; 111 1.1 christos std::copy (cstr, cstr + size, grow (size)); 112 1.1 christos } 113 1.1 christos 114 1.1 christos /* Store INPUT as ULEB128 to the end of buffer. */ 115 1.1 christos void append_unsigned_leb128 (ULONGEST input) 116 1.1 christos { 117 1.1 christos for (;;) 118 1.1 christos { 119 1.1 christos gdb_byte output = input & 0x7f; 120 1.1 christos input >>= 7; 121 1.1 christos if (input) 122 1.1 christos output |= 0x80; 123 1.1.1.2 christos m_vec.push_back (output); 124 1.1 christos if (input == 0) 125 1.1 christos break; 126 1.1 christos } 127 1.1 christos } 128 1.1 christos 129 1.1 christos /* Accept a host-format integer in VAL and append it to the buffer 130 1.1 christos as a target-format integer which is LEN bytes long. */ 131 1.1 christos void append_uint (size_t len, bfd_endian byte_order, ULONGEST val) 132 1.1 christos { 133 1.1 christos ::store_unsigned_integer (grow (len), len, byte_order, val); 134 1.1 christos } 135 1.1 christos 136 1.1.1.2 christos /* Copy VALUE to the end of the buffer, little-endian. */ 137 1.1.1.2 christos void append_offset (offset_type value) 138 1.1.1.2 christos { 139 1.1.1.2 christos append_uint (sizeof (value), BFD_ENDIAN_LITTLE, value); 140 1.1.1.2 christos } 141 1.1.1.2 christos 142 1.1 christos /* Return the size of the buffer. */ 143 1.1.1.3 christos virtual size_t size () const 144 1.1 christos { 145 1.1 christos return m_vec.size (); 146 1.1 christos } 147 1.1 christos 148 1.1 christos /* Return true iff the buffer is empty. */ 149 1.1 christos bool empty () const 150 1.1 christos { 151 1.1 christos return m_vec.empty (); 152 1.1 christos } 153 1.1 christos 154 1.1 christos /* Write the buffer to FILE. */ 155 1.1 christos void file_write (FILE *file) const 156 1.1 christos { 157 1.1 christos ::file_write (file, m_vec); 158 1.1 christos } 159 1.1 christos 160 1.1 christos private: 161 1.1 christos /* Grow SIZE bytes at the end of the buffer. Returns a pointer to 162 1.1 christos the start of the new block. */ 163 1.1 christos gdb_byte *grow (size_t size) 164 1.1 christos { 165 1.1 christos m_vec.resize (m_vec.size () + size); 166 1.1 christos return &*(m_vec.end () - size); 167 1.1 christos } 168 1.1 christos 169 1.1 christos gdb::byte_vector m_vec; 170 1.1 christos }; 171 1.1 christos 172 1.1 christos /* An entry in the symbol table. */ 173 1.1 christos struct symtab_index_entry 174 1.1 christos { 175 1.1 christos /* The name of the symbol. */ 176 1.1 christos const char *name; 177 1.1 christos /* The offset of the name in the constant pool. */ 178 1.1 christos offset_type index_offset; 179 1.1 christos /* A sorted vector of the indices of all the CUs that hold an object 180 1.1 christos of this name. */ 181 1.1 christos std::vector<offset_type> cu_indices; 182 1.1.1.2 christos 183 1.1.1.2 christos /* Minimize CU_INDICES, sorting them and removing duplicates as 184 1.1.1.2 christos appropriate. */ 185 1.1.1.2 christos void minimize (); 186 1.1 christos }; 187 1.1 christos 188 1.1 christos /* The symbol table. This is a power-of-2-sized hash table. */ 189 1.1 christos struct mapped_symtab 190 1.1 christos { 191 1.1 christos mapped_symtab () 192 1.1 christos { 193 1.1.1.3 christos m_data.resize (1024); 194 1.1 christos } 195 1.1 christos 196 1.1.1.3 christos /* If there are no elements in the symbol table, then reduce the table 197 1.1.1.3 christos size to zero. Otherwise call symtab_index_entry::minimize each entry 198 1.1.1.3 christos in the symbol table. */ 199 1.1.1.3 christos 200 1.1.1.2 christos void minimize () 201 1.1.1.2 christos { 202 1.1.1.3 christos if (m_element_count == 0) 203 1.1.1.3 christos m_data.resize (0); 204 1.1.1.3 christos 205 1.1.1.3 christos for (symtab_index_entry &item : m_data) 206 1.1.1.2 christos item.minimize (); 207 1.1.1.2 christos } 208 1.1.1.2 christos 209 1.1.1.3 christos /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX is 210 1.1.1.3 christos the index of the CU in which the symbol appears. IS_STATIC is one if 211 1.1.1.3 christos the symbol is static, otherwise zero (global). */ 212 1.1.1.3 christos 213 1.1.1.3 christos void add_index_entry (const char *name, int is_static, 214 1.1.1.3 christos gdb_index_symbol_kind kind, offset_type cu_index); 215 1.1.1.3 christos 216 1.1.1.3 christos /* When entries are originally added into the data hash the order will 217 1.1.1.3 christos vary based on the number of worker threads GDB is configured to use. 218 1.1.1.3 christos This function will rebuild the hash such that the final layout will be 219 1.1.1.3 christos deterministic regardless of the number of worker threads used. */ 220 1.1.1.3 christos 221 1.1.1.3 christos void sort (); 222 1.1.1.3 christos 223 1.1.1.3 christos /* Access the obstack. */ 224 1.1.1.3 christos struct obstack *obstack () 225 1.1.1.3 christos { return &m_string_obstack; } 226 1.1.1.3 christos 227 1.1.1.3 christos private: 228 1.1.1.3 christos 229 1.1.1.3 christos /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to 230 1.1.1.3 christos the slot. 231 1.1.1.3 christos 232 1.1.1.3 christos Function is used only during write_hash_table so no index format 233 1.1.1.3 christos backward compatibility is needed. */ 234 1.1.1.3 christos 235 1.1.1.3 christos symtab_index_entry &find_slot (const char *name); 236 1.1.1.3 christos 237 1.1.1.3 christos /* Expand SYMTAB's hash table. */ 238 1.1.1.3 christos 239 1.1.1.3 christos void hash_expand (); 240 1.1.1.3 christos 241 1.1.1.3 christos /* Return true if the hash table in data needs to grow. */ 242 1.1.1.3 christos 243 1.1.1.3 christos bool hash_needs_expanding () const 244 1.1.1.3 christos { return 4 * m_element_count / 3 >= m_data.size (); } 245 1.1.1.3 christos 246 1.1.1.3 christos /* A vector that is used as a hash table. */ 247 1.1.1.3 christos std::vector<symtab_index_entry> m_data; 248 1.1.1.3 christos 249 1.1.1.3 christos /* The number of elements stored in the m_data hash. */ 250 1.1.1.3 christos offset_type m_element_count = 0; 251 1.1 christos 252 1.1.1.2 christos /* Temporary storage for names. */ 253 1.1 christos auto_obstack m_string_obstack; 254 1.1 christos 255 1.1.1.3 christos public: 256 1.1.1.3 christos using iterator = decltype (m_data)::iterator; 257 1.1.1.3 christos using const_iterator = decltype (m_data)::const_iterator; 258 1.1.1.3 christos 259 1.1.1.3 christos iterator begin () 260 1.1.1.3 christos { return m_data.begin (); } 261 1.1.1.3 christos 262 1.1.1.3 christos iterator end () 263 1.1.1.3 christos { return m_data.end (); } 264 1.1.1.3 christos 265 1.1.1.3 christos const_iterator cbegin () 266 1.1.1.3 christos { return m_data.cbegin (); } 267 1.1.1.3 christos 268 1.1.1.3 christos const_iterator cend () 269 1.1.1.3 christos { return m_data.cend (); } 270 1.1.1.3 christos }; 271 1.1 christos 272 1.1.1.3 christos /* See class definition. */ 273 1.1 christos 274 1.1.1.3 christos symtab_index_entry & 275 1.1.1.3 christos mapped_symtab::find_slot (const char *name) 276 1.1 christos { 277 1.1 christos offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name); 278 1.1 christos 279 1.1.1.3 christos index = hash & (m_data.size () - 1); 280 1.1.1.3 christos step = ((hash * 17) & (m_data.size () - 1)) | 1; 281 1.1 christos 282 1.1 christos for (;;) 283 1.1 christos { 284 1.1.1.3 christos if (m_data[index].name == NULL 285 1.1.1.3 christos || strcmp (name, m_data[index].name) == 0) 286 1.1.1.3 christos return m_data[index]; 287 1.1.1.3 christos index = (index + step) & (m_data.size () - 1); 288 1.1 christos } 289 1.1 christos } 290 1.1 christos 291 1.1.1.3 christos /* See class definition. */ 292 1.1 christos 293 1.1.1.3 christos void 294 1.1.1.3 christos mapped_symtab::hash_expand () 295 1.1 christos { 296 1.1.1.3 christos auto old_entries = std::move (m_data); 297 1.1 christos 298 1.1.1.3 christos gdb_assert (m_data.size () == 0); 299 1.1.1.3 christos m_data.resize (old_entries.size () * 2); 300 1.1 christos 301 1.1 christos for (auto &it : old_entries) 302 1.1 christos if (it.name != NULL) 303 1.1 christos { 304 1.1.1.3 christos auto &ref = this->find_slot (it.name); 305 1.1 christos ref = std::move (it); 306 1.1 christos } 307 1.1 christos } 308 1.1 christos 309 1.1.1.3 christos /* See mapped_symtab class declaration. */ 310 1.1 christos 311 1.1.1.3 christos void mapped_symtab::sort () 312 1.1 christos { 313 1.1.1.3 christos /* Move contents out of this->data vector. */ 314 1.1.1.3 christos std::vector<symtab_index_entry> original_data = std::move (m_data); 315 1.1.1.3 christos 316 1.1.1.3 christos /* Restore the size of m_data, this will avoid having to expand the hash 317 1.1.1.3 christos table (and rehash all elements) when we reinsert after sorting. 318 1.1.1.3 christos However, we do reset the element count, this allows for some sanity 319 1.1.1.3 christos checking asserts during the reinsert phase. */ 320 1.1.1.3 christos gdb_assert (m_data.size () == 0); 321 1.1.1.3 christos m_data.resize (original_data.size ()); 322 1.1.1.3 christos m_element_count = 0; 323 1.1.1.3 christos 324 1.1.1.3 christos /* Remove empty entries from ORIGINAL_DATA, this makes sorting quicker. */ 325 1.1.1.3 christos auto it = std::remove_if (original_data.begin (), original_data.end (), 326 1.1.1.3 christos [] (const symtab_index_entry &entry) -> bool 327 1.1.1.3 christos { 328 1.1.1.3 christos return entry.name == nullptr; 329 1.1.1.3 christos }); 330 1.1.1.3 christos original_data.erase (it, original_data.end ()); 331 1.1.1.3 christos 332 1.1.1.3 christos /* Sort the existing contents. */ 333 1.1.1.3 christos std::sort (original_data.begin (), original_data.end (), 334 1.1.1.3 christos [] (const symtab_index_entry &a, 335 1.1.1.3 christos const symtab_index_entry &b) -> bool 336 1.1.1.3 christos { 337 1.1.1.3 christos /* Return true if A is before B. */ 338 1.1.1.3 christos gdb_assert (a.name != nullptr); 339 1.1.1.3 christos gdb_assert (b.name != nullptr); 340 1.1.1.3 christos 341 1.1.1.3 christos return strcmp (a.name, b.name) < 0; 342 1.1.1.3 christos }); 343 1.1.1.3 christos 344 1.1.1.3 christos /* Re-insert each item from the sorted list. */ 345 1.1.1.3 christos for (auto &entry : original_data) 346 1.1.1.3 christos { 347 1.1.1.3 christos /* We know that ORIGINAL_DATA contains no duplicates, this data was 348 1.1.1.3 christos taken from a hash table that de-duplicated entries for us, so 349 1.1.1.3 christos count this as a new item. 350 1.1.1.3 christos 351 1.1.1.3 christos As we retained the original size of m_data (see above) then we 352 1.1.1.3 christos should never need to grow m_data_ during this re-insertion phase, 353 1.1.1.3 christos assert that now. */ 354 1.1.1.3 christos ++m_element_count; 355 1.1.1.3 christos gdb_assert (!this->hash_needs_expanding ()); 356 1.1.1.3 christos 357 1.1.1.3 christos /* Lookup a slot. */ 358 1.1.1.3 christos symtab_index_entry &slot = this->find_slot (entry.name); 359 1.1 christos 360 1.1.1.3 christos /* As discussed above, we should not find duplicates. */ 361 1.1.1.3 christos gdb_assert (slot.name == nullptr); 362 1.1 christos 363 1.1.1.3 christos /* Move this item into the slot we found. */ 364 1.1.1.3 christos slot = std::move (entry); 365 1.1.1.3 christos } 366 1.1.1.3 christos } 367 1.1.1.3 christos 368 1.1.1.3 christos /* See class definition. */ 369 1.1.1.3 christos 370 1.1.1.3 christos void 371 1.1.1.3 christos mapped_symtab::add_index_entry (const char *name, int is_static, 372 1.1.1.3 christos gdb_index_symbol_kind kind, 373 1.1.1.3 christos offset_type cu_index) 374 1.1.1.3 christos { 375 1.1.1.3 christos symtab_index_entry *slot = &this->find_slot (name); 376 1.1.1.3 christos if (slot->name == NULL) 377 1.1 christos { 378 1.1.1.3 christos /* This is a new element in the hash table. */ 379 1.1.1.3 christos ++this->m_element_count; 380 1.1.1.3 christos 381 1.1.1.3 christos /* We might need to grow the hash table. */ 382 1.1.1.3 christos if (this->hash_needs_expanding ()) 383 1.1.1.3 christos { 384 1.1.1.3 christos this->hash_expand (); 385 1.1.1.3 christos 386 1.1.1.3 christos /* This element will have a different slot in the new table. */ 387 1.1.1.3 christos slot = &this->find_slot (name); 388 1.1.1.3 christos 389 1.1.1.3 christos /* But it should still be a new element in the hash table. */ 390 1.1.1.3 christos gdb_assert (slot->name == nullptr); 391 1.1.1.3 christos } 392 1.1.1.3 christos 393 1.1.1.3 christos slot->name = name; 394 1.1 christos /* index_offset is set later. */ 395 1.1 christos } 396 1.1 christos 397 1.1.1.3 christos offset_type cu_index_and_attrs = 0; 398 1.1 christos DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index); 399 1.1 christos DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static); 400 1.1 christos DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind); 401 1.1 christos 402 1.1 christos /* We don't want to record an index value twice as we want to avoid the 403 1.1 christos duplication. 404 1.1 christos We process all global symbols and then all static symbols 405 1.1 christos (which would allow us to avoid the duplication by only having to check 406 1.1 christos the last entry pushed), but a symbol could have multiple kinds in one CU. 407 1.1 christos To keep things simple we don't worry about the duplication here and 408 1.1 christos sort and uniquify the list after we've processed all symbols. */ 409 1.1.1.3 christos slot->cu_indices.push_back (cu_index_and_attrs); 410 1.1 christos } 411 1.1 christos 412 1.1.1.2 christos /* See symtab_index_entry. */ 413 1.1 christos 414 1.1.1.2 christos void 415 1.1.1.2 christos symtab_index_entry::minimize () 416 1.1 christos { 417 1.1.1.2 christos if (name == nullptr || cu_indices.empty ()) 418 1.1.1.2 christos return; 419 1.1.1.2 christos 420 1.1.1.2 christos std::sort (cu_indices.begin (), cu_indices.end ()); 421 1.1.1.2 christos auto from = std::unique (cu_indices.begin (), cu_indices.end ()); 422 1.1.1.2 christos cu_indices.erase (from, cu_indices.end ()); 423 1.1.1.2 christos 424 1.1.1.3 christos /* We don't want to enter a type more than once, so 425 1.1.1.2 christos remove any such duplicates from the list as well. When doing 426 1.1.1.2 christos this, we want to keep the entry from the first CU -- but this is 427 1.1.1.2 christos implicit due to the sort. This choice is done because it's 428 1.1.1.2 christos similar to what gdb historically did for partial symbols. */ 429 1.1.1.2 christos std::unordered_set<offset_type> seen; 430 1.1.1.2 christos from = std::remove_if (cu_indices.begin (), cu_indices.end (), 431 1.1.1.2 christos [&] (offset_type val) 432 1.1.1.2 christos { 433 1.1.1.2 christos gdb_index_symbol_kind kind = GDB_INDEX_SYMBOL_KIND_VALUE (val); 434 1.1.1.3 christos if (kind != GDB_INDEX_SYMBOL_KIND_TYPE) 435 1.1.1.2 christos return false; 436 1.1.1.2 christos 437 1.1.1.2 christos val &= ~GDB_INDEX_CU_MASK; 438 1.1.1.2 christos return !seen.insert (val).second; 439 1.1.1.2 christos }); 440 1.1.1.2 christos cu_indices.erase (from, cu_indices.end ()); 441 1.1 christos } 442 1.1 christos 443 1.1 christos /* A form of 'const char *' suitable for container keys. Only the 444 1.1 christos pointer is stored. The strings themselves are compared, not the 445 1.1 christos pointers. */ 446 1.1 christos class c_str_view 447 1.1 christos { 448 1.1 christos public: 449 1.1 christos c_str_view (const char *cstr) 450 1.1 christos : m_cstr (cstr) 451 1.1 christos {} 452 1.1 christos 453 1.1 christos bool operator== (const c_str_view &other) const 454 1.1 christos { 455 1.1 christos return strcmp (m_cstr, other.m_cstr) == 0; 456 1.1 christos } 457 1.1 christos 458 1.1.1.3 christos bool operator< (const c_str_view &other) const 459 1.1.1.3 christos { 460 1.1.1.3 christos return strcmp (m_cstr, other.m_cstr) < 0; 461 1.1.1.3 christos } 462 1.1.1.3 christos 463 1.1 christos /* Return the underlying C string. Note, the returned string is 464 1.1 christos only a reference with lifetime of this object. */ 465 1.1 christos const char *c_str () const 466 1.1 christos { 467 1.1 christos return m_cstr; 468 1.1 christos } 469 1.1 christos 470 1.1 christos private: 471 1.1 christos friend class c_str_view_hasher; 472 1.1 christos const char *const m_cstr; 473 1.1 christos }; 474 1.1 christos 475 1.1 christos /* A std::unordered_map::hasher for c_str_view that uses the right 476 1.1 christos hash function for strings in a mapped index. */ 477 1.1 christos class c_str_view_hasher 478 1.1 christos { 479 1.1 christos public: 480 1.1 christos size_t operator () (const c_str_view &x) const 481 1.1 christos { 482 1.1 christos return mapped_index_string_hash (INT_MAX, x.m_cstr); 483 1.1 christos } 484 1.1 christos }; 485 1.1 christos 486 1.1 christos /* A std::unordered_map::hasher for std::vector<>. */ 487 1.1 christos template<typename T> 488 1.1 christos class vector_hasher 489 1.1 christos { 490 1.1 christos public: 491 1.1 christos size_t operator () (const std::vector<T> &key) const 492 1.1 christos { 493 1.1 christos return iterative_hash (key.data (), 494 1.1 christos sizeof (key.front ()) * key.size (), 0); 495 1.1 christos } 496 1.1 christos }; 497 1.1 christos 498 1.1 christos /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with 499 1.1 christos constant pool entries going into the data buffer CPOOL. */ 500 1.1 christos 501 1.1 christos static void 502 1.1 christos write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool) 503 1.1 christos { 504 1.1 christos { 505 1.1 christos /* Elements are sorted vectors of the indices of all the CUs that 506 1.1 christos hold an object of this name. */ 507 1.1 christos std::unordered_map<std::vector<offset_type>, offset_type, 508 1.1 christos vector_hasher<offset_type>> 509 1.1 christos symbol_hash_table; 510 1.1 christos 511 1.1 christos /* We add all the index vectors to the constant pool first, to 512 1.1 christos ensure alignment is ok. */ 513 1.1.1.3 christos for (symtab_index_entry &entry : *symtab) 514 1.1 christos { 515 1.1 christos if (entry.name == NULL) 516 1.1 christos continue; 517 1.1 christos gdb_assert (entry.index_offset == 0); 518 1.1 christos 519 1.1.1.3 christos auto [iter, inserted] 520 1.1.1.3 christos = symbol_hash_table.try_emplace (entry.cu_indices, 521 1.1.1.3 christos cpool.size ()); 522 1.1.1.3 christos entry.index_offset = iter->second; 523 1.1.1.3 christos if (inserted) 524 1.1 christos { 525 1.1.1.3 christos /* Newly inserted. */ 526 1.1.1.3 christos cpool.append_offset (entry.cu_indices.size ()); 527 1.1.1.3 christos for (const auto index : entry.cu_indices) 528 1.1.1.3 christos cpool.append_offset (index); 529 1.1 christos } 530 1.1 christos } 531 1.1 christos } 532 1.1 christos 533 1.1 christos /* Now write out the hash table. */ 534 1.1 christos std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table; 535 1.1.1.3 christos for (const auto &entry : *symtab) 536 1.1 christos { 537 1.1 christos offset_type str_off, vec_off; 538 1.1 christos 539 1.1 christos if (entry.name != NULL) 540 1.1 christos { 541 1.1 christos const auto insertpair = str_table.emplace (entry.name, cpool.size ()); 542 1.1 christos if (insertpair.second) 543 1.1 christos cpool.append_cstr0 (entry.name); 544 1.1 christos str_off = insertpair.first->second; 545 1.1 christos vec_off = entry.index_offset; 546 1.1 christos } 547 1.1 christos else 548 1.1 christos { 549 1.1 christos /* While 0 is a valid constant pool index, it is not valid 550 1.1 christos to have 0 for both offsets. */ 551 1.1 christos str_off = 0; 552 1.1 christos vec_off = 0; 553 1.1 christos } 554 1.1 christos 555 1.1.1.2 christos output.append_offset (str_off); 556 1.1.1.2 christos output.append_offset (vec_off); 557 1.1 christos } 558 1.1 christos } 559 1.1 christos 560 1.1.1.3 christos using cu_index_map 561 1.1.1.3 christos = std::unordered_map<const dwarf2_per_cu_data *, unsigned int>; 562 1.1 christos 563 1.1 christos /* Helper struct for building the address table. */ 564 1.1 christos struct addrmap_index_data 565 1.1 christos { 566 1.1.1.2 christos addrmap_index_data (data_buf &addr_vec_, cu_index_map &cu_index_htab_) 567 1.1.1.2 christos : addr_vec (addr_vec_), 568 1.1.1.2 christos cu_index_htab (cu_index_htab_) 569 1.1 christos {} 570 1.1 christos 571 1.1 christos data_buf &addr_vec; 572 1.1.1.2 christos cu_index_map &cu_index_htab; 573 1.1.1.2 christos 574 1.1.1.3 christos int operator() (CORE_ADDR start_addr, const void *obj); 575 1.1 christos 576 1.1.1.2 christos /* True if the previous_* fields are valid. 577 1.1 christos We can't write an entry until we see the next entry (since it is only then 578 1.1 christos that we know the end of the entry). */ 579 1.1.1.2 christos bool previous_valid = false; 580 1.1 christos /* Index of the CU in the table of all CUs in the index file. */ 581 1.1.1.2 christos unsigned int previous_cu_index = 0; 582 1.1 christos /* Start address of the CU. */ 583 1.1.1.2 christos CORE_ADDR previous_cu_start = 0; 584 1.1 christos }; 585 1.1 christos 586 1.1 christos /* Write an address entry to ADDR_VEC. */ 587 1.1 christos 588 1.1 christos static void 589 1.1.1.2 christos add_address_entry (data_buf &addr_vec, 590 1.1 christos CORE_ADDR start, CORE_ADDR end, unsigned int cu_index) 591 1.1 christos { 592 1.1 christos addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start); 593 1.1 christos addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end); 594 1.1.1.2 christos addr_vec.append_offset (cu_index); 595 1.1 christos } 596 1.1 christos 597 1.1 christos /* Worker function for traversing an addrmap to build the address table. */ 598 1.1 christos 599 1.1.1.2 christos int 600 1.1.1.3 christos addrmap_index_data::operator() (CORE_ADDR start_addr, const void *obj) 601 1.1 christos { 602 1.1.1.3 christos const dwarf2_per_cu_data *per_cu 603 1.1.1.3 christos = static_cast<const dwarf2_per_cu_data *> (obj); 604 1.1 christos 605 1.1.1.2 christos if (previous_valid) 606 1.1.1.2 christos add_address_entry (addr_vec, 607 1.1.1.2 christos previous_cu_start, start_addr, 608 1.1.1.2 christos previous_cu_index); 609 1.1.1.2 christos 610 1.1.1.2 christos previous_cu_start = start_addr; 611 1.1.1.2 christos if (per_cu != NULL) 612 1.1.1.2 christos { 613 1.1.1.2 christos const auto it = cu_index_htab.find (per_cu); 614 1.1.1.2 christos gdb_assert (it != cu_index_htab.cend ()); 615 1.1.1.2 christos previous_cu_index = it->second; 616 1.1.1.2 christos previous_valid = true; 617 1.1 christos } 618 1.1 christos else 619 1.1.1.2 christos previous_valid = false; 620 1.1 christos 621 1.1 christos return 0; 622 1.1 christos } 623 1.1 christos 624 1.1.1.2 christos /* Write PER_BFD's address map to ADDR_VEC. 625 1.1 christos CU_INDEX_HTAB is used to map addrmap entries to their CU indices 626 1.1 christos in the index file. */ 627 1.1 christos 628 1.1 christos static void 629 1.1.1.3 christos write_address_map (const addrmap *addrmap, data_buf &addr_vec, 630 1.1.1.2 christos cu_index_map &cu_index_htab) 631 1.1 christos { 632 1.1 christos struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab); 633 1.1 christos 634 1.1.1.2 christos addrmap->foreach (addrmap_index_data); 635 1.1 christos 636 1.1 christos /* It's highly unlikely the last entry (end address = 0xff...ff) 637 1.1 christos is valid, but we should still handle it. 638 1.1 christos The end address is recorded as the start of the next region, but that 639 1.1 christos doesn't work here. To cope we pass 0xff...ff, this is a rare situation 640 1.1 christos anyway. */ 641 1.1 christos if (addrmap_index_data.previous_valid) 642 1.1.1.2 christos add_address_entry (addr_vec, 643 1.1 christos addrmap_index_data.previous_cu_start, (CORE_ADDR) -1, 644 1.1 christos addrmap_index_data.previous_cu_index); 645 1.1 christos } 646 1.1 christos 647 1.1 christos /* DWARF-5 .debug_names builder. */ 648 1.1 christos class debug_names 649 1.1 christos { 650 1.1 christos public: 651 1.1.1.3 christos debug_names (dwarf2_per_bfd *per_bfd, bool is_dwarf64, 652 1.1 christos bfd_endian dwarf5_byte_order) 653 1.1 christos : m_dwarf5_byte_order (dwarf5_byte_order), 654 1.1 christos m_dwarf32 (dwarf5_byte_order), 655 1.1 christos m_dwarf64 (dwarf5_byte_order), 656 1.1 christos m_dwarf (is_dwarf64 657 1.1 christos ? static_cast<dwarf &> (m_dwarf64) 658 1.1 christos : static_cast<dwarf &> (m_dwarf32)), 659 1.1 christos m_name_table_string_offs (m_dwarf.name_table_string_offs), 660 1.1 christos m_name_table_entry_offs (m_dwarf.name_table_entry_offs), 661 1.1.1.3 christos m_debugstrlookup (per_bfd) 662 1.1 christos {} 663 1.1 christos 664 1.1 christos int dwarf5_offset_size () const 665 1.1 christos { 666 1.1 christos const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64; 667 1.1 christos return dwarf5_is_dwarf64 ? 8 : 4; 668 1.1 christos } 669 1.1 christos 670 1.1 christos /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */ 671 1.1 christos enum class unit_kind { cu, tu }; 672 1.1 christos 673 1.1 christos /* Insert one symbol. */ 674 1.1.1.2 christos void insert (const cooked_index_entry *entry) 675 1.1 christos { 676 1.1.1.3 christos /* These entries are synthesized by the reader, and so should not 677 1.1.1.3 christos be written. */ 678 1.1.1.3 christos if (entry->lang == language_ada && entry->tag == DW_TAG_namespace) 679 1.1.1.3 christos return; 680 1.1 christos 681 1.1 christos const auto insertpair 682 1.1.1.3 christos = m_name_to_value_set.try_emplace (c_str_view (entry->name)); 683 1.1.1.3 christos entry_list &elist = insertpair.first->second; 684 1.1.1.3 christos elist.entries.push_back (entry); 685 1.1 christos } 686 1.1 christos 687 1.1 christos /* Build all the tables. All symbols must be already inserted. 688 1.1 christos This function does not call file_write, caller has to do it 689 1.1 christos afterwards. */ 690 1.1 christos void build () 691 1.1 christos { 692 1.1 christos /* Verify the build method has not be called twice. */ 693 1.1 christos gdb_assert (m_abbrev_table.empty ()); 694 1.1 christos const size_t name_count = m_name_to_value_set.size (); 695 1.1 christos m_name_table_string_offs.reserve (name_count); 696 1.1 christos m_name_table_entry_offs.reserve (name_count); 697 1.1 christos 698 1.1.1.3 christos /* The name table is indexed from 1. The numbers are needed here 699 1.1.1.3 christos so that parent entries can be handled correctly. */ 700 1.1.1.3 christos int next_name = 1; 701 1.1.1.3 christos for (auto &item : m_name_to_value_set) 702 1.1.1.3 christos item.second.index = next_name++; 703 1.1.1.3 christos 704 1.1.1.3 christos /* The next available abbrev number. */ 705 1.1.1.3 christos int next_abbrev = 1; 706 1.1.1.3 christos 707 1.1.1.3 christos for (auto &item : m_name_to_value_set) 708 1.1 christos { 709 1.1.1.3 christos const c_str_view &name = item.first; 710 1.1.1.3 christos entry_list &these_entries = item.second; 711 1.1.1.3 christos 712 1.1.1.3 christos /* Sort the items within each bucket. This ensures that the 713 1.1.1.3 christos generated index files will be the same no matter the order in 714 1.1.1.3 christos which symbols were added into the index. */ 715 1.1.1.3 christos std::sort (these_entries.entries.begin (), 716 1.1.1.3 christos these_entries.entries.end (), 717 1.1.1.3 christos [] (const cooked_index_entry *a, 718 1.1.1.3 christos const cooked_index_entry *b) 719 1.1.1.3 christos { 720 1.1.1.3 christos /* Sort first by CU. */ 721 1.1.1.3 christos if (a->per_cu->index != b->per_cu->index) 722 1.1.1.3 christos return a->per_cu->index < b->per_cu->index; 723 1.1.1.3 christos /* Then by DIE in the CU. */ 724 1.1.1.3 christos if (a->die_offset != b->die_offset) 725 1.1.1.3 christos return a->die_offset < b->die_offset; 726 1.1.1.3 christos /* We might have two entries for a DIE because 727 1.1.1.3 christos the linkage name is entered separately. So, 728 1.1.1.3 christos sort by flags. */ 729 1.1.1.3 christos return a->flags < b->flags; 730 1.1.1.3 christos }); 731 1.1.1.3 christos 732 1.1.1.3 christos m_name_table_string_offs.push_back_reorder 733 1.1.1.3 christos (m_debugstrlookup.lookup (name.c_str ())); /* ??? */ 734 1.1.1.3 christos m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ()); 735 1.1.1.3 christos 736 1.1.1.3 christos for (const cooked_index_entry *entry : these_entries.entries) 737 1.1 christos { 738 1.1.1.3 christos unit_kind kind = (entry->per_cu->is_debug_types 739 1.1.1.3 christos ? unit_kind::tu 740 1.1.1.3 christos : unit_kind::cu); 741 1.1.1.3 christos /* Currently Ada parentage is synthesized by the 742 1.1.1.3 christos reader and so must be ignored here. */ 743 1.1.1.3 christos const cooked_index_entry *parent = (entry->lang == language_ada 744 1.1.1.3 christos ? nullptr 745 1.1.1.3 christos : entry->get_parent ()); 746 1.1.1.3 christos 747 1.1.1.3 christos int &idx = m_indexkey_to_idx[index_key (entry->tag, 748 1.1.1.3 christos kind, 749 1.1.1.3 christos entry->flags, 750 1.1.1.3 christos entry->lang, 751 1.1.1.3 christos parent != nullptr)]; 752 1.1.1.3 christos if (idx == 0) 753 1.1 christos { 754 1.1.1.3 christos idx = next_abbrev++; 755 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (idx); 756 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (entry->tag); 757 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 758 1.1.1.3 christos (kind == unit_kind::cu 759 1.1.1.3 christos ? DW_IDX_compile_unit 760 1.1.1.3 christos : DW_IDX_type_unit); 761 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata); 762 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (DW_IDX_die_offset); 763 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (DW_FORM_ref_addr); 764 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (DW_IDX_GNU_language); 765 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata); 766 1.1.1.3 christos if ((entry->flags & IS_STATIC) != 0) 767 1.1 christos { 768 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (DW_IDX_GNU_internal); 769 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present); 770 1.1.1.3 christos } 771 1.1.1.3 christos if ((entry->flags & IS_MAIN) != 0) 772 1.1.1.3 christos { 773 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (DW_IDX_GNU_main); 774 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present); 775 1.1.1.3 christos } 776 1.1.1.3 christos if ((entry->flags & IS_LINKAGE) != 0) 777 1.1.1.3 christos { 778 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (DW_IDX_GNU_linkage_name); 779 1.1 christos m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present); 780 1.1.1.3 christos } 781 1.1.1.3 christos if (parent != nullptr) 782 1.1.1.3 christos { 783 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (DW_IDX_parent); 784 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata); 785 1.1 christos } 786 1.1 christos 787 1.1.1.3 christos /* Terminate attributes list. */ 788 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (0); 789 1.1.1.3 christos m_abbrev_table.append_unsigned_leb128 (0); 790 1.1 christos } 791 1.1 christos 792 1.1.1.3 christos m_entry_pool.append_unsigned_leb128 (idx); 793 1.1.1.3 christos 794 1.1.1.3 christos const auto it = m_cu_index_htab.find (entry->per_cu); 795 1.1.1.3 christos gdb_assert (it != m_cu_index_htab.cend ()); 796 1.1.1.3 christos m_entry_pool.append_unsigned_leb128 (it->second); 797 1.1.1.3 christos 798 1.1.1.3 christos m_entry_pool.append_uint (dwarf5_offset_size (), 799 1.1.1.3 christos m_dwarf5_byte_order, 800 1.1.1.3 christos to_underlying (entry->die_offset)); 801 1.1.1.3 christos 802 1.1.1.3 christos m_entry_pool.append_unsigned_leb128 (entry->per_cu->dw_lang ()); 803 1.1.1.3 christos 804 1.1.1.3 christos if (parent != nullptr) 805 1.1.1.3 christos { 806 1.1.1.3 christos c_str_view par_name (parent->name); 807 1.1.1.3 christos auto name_iter = m_name_to_value_set.find (par_name); 808 1.1.1.3 christos gdb_assert (name_iter != m_name_to_value_set.end ()); 809 1.1.1.3 christos gdb_assert (name_iter->second.index != 0); 810 1.1.1.3 christos m_entry_pool.append_unsigned_leb128 (name_iter->second.index); 811 1.1.1.3 christos } 812 1.1 christos } 813 1.1.1.3 christos 814 1.1.1.3 christos /* Terminate the list of entries. */ 815 1.1.1.3 christos m_entry_pool.append_unsigned_leb128 (0); 816 1.1 christos } 817 1.1 christos 818 1.1 christos /* Terminate tags list. */ 819 1.1 christos m_abbrev_table.append_unsigned_leb128 (0); 820 1.1 christos } 821 1.1 christos 822 1.1 christos /* Return .debug_names names count. This must be called only after 823 1.1 christos calling the build method. */ 824 1.1 christos uint32_t name_count () const 825 1.1 christos { 826 1.1 christos /* Verify the build method has been already called. */ 827 1.1 christos gdb_assert (!m_abbrev_table.empty ()); 828 1.1.1.3 christos return m_name_to_value_set.size (); 829 1.1 christos } 830 1.1 christos 831 1.1 christos /* Return number of bytes of .debug_names abbreviation table. This 832 1.1 christos must be called only after calling the build method. */ 833 1.1 christos uint32_t abbrev_table_bytes () const 834 1.1 christos { 835 1.1 christos gdb_assert (!m_abbrev_table.empty ()); 836 1.1 christos return m_abbrev_table.size (); 837 1.1 christos } 838 1.1 christos 839 1.1 christos /* Return number of bytes the .debug_names section will have. This 840 1.1 christos must be called only after calling the build method. */ 841 1.1 christos size_t bytes () const 842 1.1 christos { 843 1.1 christos /* Verify the build method has been already called. */ 844 1.1 christos gdb_assert (!m_abbrev_table.empty ()); 845 1.1 christos size_t expected_bytes = 0; 846 1.1 christos expected_bytes += m_name_table_string_offs.bytes (); 847 1.1 christos expected_bytes += m_name_table_entry_offs.bytes (); 848 1.1 christos expected_bytes += m_abbrev_table.size (); 849 1.1 christos expected_bytes += m_entry_pool.size (); 850 1.1 christos return expected_bytes; 851 1.1 christos } 852 1.1 christos 853 1.1 christos /* Write .debug_names to FILE_NAMES and .debug_str addition to 854 1.1 christos FILE_STR. This must be called only after calling the build 855 1.1 christos method. */ 856 1.1 christos void file_write (FILE *file_names, FILE *file_str) const 857 1.1 christos { 858 1.1 christos /* Verify the build method has been already called. */ 859 1.1 christos gdb_assert (!m_abbrev_table.empty ()); 860 1.1 christos m_name_table_string_offs.file_write (file_names); 861 1.1 christos m_name_table_entry_offs.file_write (file_names); 862 1.1 christos m_abbrev_table.file_write (file_names); 863 1.1 christos m_entry_pool.file_write (file_names); 864 1.1 christos m_debugstrlookup.file_write (file_str); 865 1.1 christos } 866 1.1 christos 867 1.1.1.2 christos void add_cu (dwarf2_per_cu_data *per_cu, offset_type index) 868 1.1 christos { 869 1.1.1.2 christos m_cu_index_htab.emplace (per_cu, index); 870 1.1 christos } 871 1.1 christos 872 1.1 christos private: 873 1.1 christos 874 1.1 christos /* Storage for symbol names mapping them to their .debug_str section 875 1.1 christos offsets. */ 876 1.1 christos class debug_str_lookup 877 1.1 christos { 878 1.1 christos public: 879 1.1 christos 880 1.1.1.3 christos /* Object constructor to be called for current DWARF2_PER_BFD. */ 881 1.1.1.3 christos debug_str_lookup (dwarf2_per_bfd *per_bfd) 882 1.1.1.3 christos : m_abfd (per_bfd->obfd), 883 1.1.1.3 christos m_per_bfd (per_bfd) 884 1.1.1.3 christos { 885 1.1 christos } 886 1.1 christos 887 1.1 christos /* Return offset of symbol name S in the .debug_str section. Add 888 1.1 christos such symbol to the section's end if it does not exist there 889 1.1 christos yet. */ 890 1.1 christos size_t lookup (const char *s) 891 1.1 christos { 892 1.1.1.3 christos /* Most strings will have come from the string table 893 1.1.1.3 christos already. */ 894 1.1.1.3 christos const gdb_byte *b = (const gdb_byte *) s; 895 1.1.1.3 christos if (b >= m_per_bfd->str.buffer 896 1.1.1.3 christos && b < m_per_bfd->str.buffer + m_per_bfd->str.size) 897 1.1.1.3 christos return b - m_per_bfd->str.buffer; 898 1.1.1.3 christos 899 1.1 christos const auto it = m_str_table.find (c_str_view (s)); 900 1.1 christos if (it != m_str_table.end ()) 901 1.1 christos return it->second; 902 1.1.1.3 christos const size_t offset = (m_per_bfd->str.size 903 1.1 christos + m_str_add_buf.size ()); 904 1.1 christos m_str_table.emplace (c_str_view (s), offset); 905 1.1 christos m_str_add_buf.append_cstr0 (s); 906 1.1 christos return offset; 907 1.1 christos } 908 1.1 christos 909 1.1 christos /* Append the end of the .debug_str section to FILE. */ 910 1.1 christos void file_write (FILE *file) const 911 1.1 christos { 912 1.1 christos m_str_add_buf.file_write (file); 913 1.1 christos } 914 1.1 christos 915 1.1 christos private: 916 1.1 christos std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table; 917 1.1 christos bfd *const m_abfd; 918 1.1.1.3 christos dwarf2_per_bfd *m_per_bfd; 919 1.1 christos 920 1.1 christos /* Data to add at the end of .debug_str for new needed symbol names. */ 921 1.1 christos data_buf m_str_add_buf; 922 1.1 christos }; 923 1.1 christos 924 1.1 christos /* Container to map used DWARF tags to their .debug_names abbreviation 925 1.1 christos tags. */ 926 1.1 christos class index_key 927 1.1 christos { 928 1.1 christos public: 929 1.1.1.3 christos index_key (dwarf_tag tag_, unit_kind kind_, cooked_index_flag flags_, 930 1.1.1.3 christos enum language lang_, bool has_parent_) 931 1.1.1.3 christos : tag (tag_), 932 1.1.1.3 christos kind (kind_), 933 1.1.1.3 christos flags (flags_ & ~IS_TYPE_DECLARATION), 934 1.1.1.3 christos lang (lang_), 935 1.1.1.3 christos has_parent (has_parent_) 936 1.1 christos { 937 1.1 christos } 938 1.1 christos 939 1.1.1.3 christos bool operator== (const index_key &other) const 940 1.1 christos { 941 1.1.1.3 christos return (tag == other.tag 942 1.1.1.3 christos && kind == other.kind 943 1.1.1.3 christos && flags == other.flags 944 1.1.1.3 christos && lang == other.lang 945 1.1.1.3 christos && has_parent == other.has_parent); 946 1.1 christos } 947 1.1 christos 948 1.1.1.3 christos const dwarf_tag tag; 949 1.1 christos const unit_kind kind; 950 1.1.1.3 christos const cooked_index_flag flags; 951 1.1.1.3 christos const enum language lang; 952 1.1.1.3 christos const bool has_parent; 953 1.1 christos }; 954 1.1 christos 955 1.1 christos /* Provide std::unordered_map::hasher for index_key. */ 956 1.1 christos class index_key_hasher 957 1.1 christos { 958 1.1 christos public: 959 1.1.1.3 christos size_t operator () (const index_key &key) const 960 1.1 christos { 961 1.1.1.3 christos return (std::hash<int>() (key.tag) 962 1.1.1.3 christos ^ std::hash<int>() (key.flags) 963 1.1.1.3 christos ^ std::hash<int>() (key.lang)); 964 1.1 christos } 965 1.1 christos }; 966 1.1 christos 967 1.1 christos /* Abstract base class to unify DWARF-32 and DWARF-64 name table 968 1.1 christos output. */ 969 1.1 christos class offset_vec 970 1.1 christos { 971 1.1 christos protected: 972 1.1 christos const bfd_endian dwarf5_byte_order; 973 1.1 christos public: 974 1.1 christos explicit offset_vec (bfd_endian dwarf5_byte_order_) 975 1.1 christos : dwarf5_byte_order (dwarf5_byte_order_) 976 1.1 christos {} 977 1.1 christos 978 1.1 christos /* Call std::vector::reserve for NELEM elements. */ 979 1.1 christos virtual void reserve (size_t nelem) = 0; 980 1.1 christos 981 1.1 christos /* Call std::vector::push_back with store_unsigned_integer byte 982 1.1 christos reordering for ELEM. */ 983 1.1 christos virtual void push_back_reorder (size_t elem) = 0; 984 1.1 christos 985 1.1 christos /* Return expected output size in bytes. */ 986 1.1 christos virtual size_t bytes () const = 0; 987 1.1 christos 988 1.1 christos /* Write name table to FILE. */ 989 1.1 christos virtual void file_write (FILE *file) const = 0; 990 1.1 christos }; 991 1.1 christos 992 1.1 christos /* Template to unify DWARF-32 and DWARF-64 output. */ 993 1.1 christos template<typename OffsetSize> 994 1.1 christos class offset_vec_tmpl : public offset_vec 995 1.1 christos { 996 1.1 christos public: 997 1.1 christos explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_) 998 1.1 christos : offset_vec (dwarf5_byte_order_) 999 1.1 christos {} 1000 1.1 christos 1001 1.1 christos /* Implement offset_vec::reserve. */ 1002 1.1 christos void reserve (size_t nelem) override 1003 1.1 christos { 1004 1.1 christos m_vec.reserve (nelem); 1005 1.1 christos } 1006 1.1 christos 1007 1.1 christos /* Implement offset_vec::push_back_reorder. */ 1008 1.1 christos void push_back_reorder (size_t elem) override 1009 1.1 christos { 1010 1.1 christos m_vec.push_back (elem); 1011 1.1 christos /* Check for overflow. */ 1012 1.1 christos gdb_assert (m_vec.back () == elem); 1013 1.1 christos store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()), 1014 1.1 christos sizeof (m_vec.back ()), dwarf5_byte_order, elem); 1015 1.1 christos } 1016 1.1 christos 1017 1.1 christos /* Implement offset_vec::bytes. */ 1018 1.1 christos size_t bytes () const override 1019 1.1 christos { 1020 1.1 christos return m_vec.size () * sizeof (m_vec[0]); 1021 1.1 christos } 1022 1.1 christos 1023 1.1 christos /* Implement offset_vec::file_write. */ 1024 1.1 christos void file_write (FILE *file) const override 1025 1.1 christos { 1026 1.1 christos ::file_write (file, m_vec); 1027 1.1 christos } 1028 1.1 christos 1029 1.1 christos private: 1030 1.1 christos std::vector<OffsetSize> m_vec; 1031 1.1 christos }; 1032 1.1 christos 1033 1.1 christos /* Base class to unify DWARF-32 and DWARF-64 .debug_names output 1034 1.1 christos respecting name table width. */ 1035 1.1 christos class dwarf 1036 1.1 christos { 1037 1.1 christos public: 1038 1.1 christos offset_vec &name_table_string_offs, &name_table_entry_offs; 1039 1.1 christos 1040 1.1 christos dwarf (offset_vec &name_table_string_offs_, 1041 1.1 christos offset_vec &name_table_entry_offs_) 1042 1.1 christos : name_table_string_offs (name_table_string_offs_), 1043 1.1 christos name_table_entry_offs (name_table_entry_offs_) 1044 1.1 christos { 1045 1.1 christos } 1046 1.1 christos }; 1047 1.1 christos 1048 1.1 christos /* Template to unify DWARF-32 and DWARF-64 .debug_names output 1049 1.1 christos respecting name table width. */ 1050 1.1 christos template<typename OffsetSize> 1051 1.1 christos class dwarf_tmpl : public dwarf 1052 1.1 christos { 1053 1.1 christos public: 1054 1.1 christos explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_) 1055 1.1 christos : dwarf (m_name_table_string_offs, m_name_table_entry_offs), 1056 1.1 christos m_name_table_string_offs (dwarf5_byte_order_), 1057 1.1 christos m_name_table_entry_offs (dwarf5_byte_order_) 1058 1.1 christos {} 1059 1.1 christos 1060 1.1 christos private: 1061 1.1 christos offset_vec_tmpl<OffsetSize> m_name_table_string_offs; 1062 1.1 christos offset_vec_tmpl<OffsetSize> m_name_table_entry_offs; 1063 1.1 christos }; 1064 1.1 christos 1065 1.1.1.3 christos struct entry_list 1066 1.1.1.3 christos { 1067 1.1.1.3 christos unsigned index = 0; 1068 1.1.1.3 christos std::vector<const cooked_index_entry *> entries; 1069 1.1.1.3 christos }; 1070 1.1.1.3 christos 1071 1.1.1.3 christos /* Store value of each symbol. Note that we rely on the sorting 1072 1.1.1.3 christos behavior of map to make the output stable. */ 1073 1.1.1.3 christos std::map<c_str_view, entry_list> m_name_to_value_set; 1074 1.1 christos 1075 1.1 christos const bfd_endian m_dwarf5_byte_order; 1076 1.1 christos dwarf_tmpl<uint32_t> m_dwarf32; 1077 1.1 christos dwarf_tmpl<uint64_t> m_dwarf64; 1078 1.1 christos dwarf &m_dwarf; 1079 1.1 christos offset_vec &m_name_table_string_offs, &m_name_table_entry_offs; 1080 1.1 christos debug_str_lookup m_debugstrlookup; 1081 1.1 christos 1082 1.1 christos /* Map each used .debug_names abbreviation tag parameter to its 1083 1.1 christos index value. */ 1084 1.1 christos std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx; 1085 1.1 christos 1086 1.1 christos /* .debug_names abbreviation table. */ 1087 1.1 christos data_buf m_abbrev_table; 1088 1.1 christos 1089 1.1 christos /* .debug_names entry pool. */ 1090 1.1 christos data_buf m_entry_pool; 1091 1.1 christos 1092 1.1 christos /* Temporary storage for Ada names. */ 1093 1.1 christos auto_obstack m_string_obstack; 1094 1.1.1.2 christos 1095 1.1.1.2 christos cu_index_map m_cu_index_htab; 1096 1.1 christos }; 1097 1.1 christos 1098 1.1 christos /* Return iff any of the needed offsets does not fit into 32-bit 1099 1.1 christos .debug_names section. */ 1100 1.1 christos 1101 1.1 christos static bool 1102 1.1.1.3 christos check_dwarf64_offsets (dwarf2_per_bfd *per_bfd) 1103 1.1 christos { 1104 1.1.1.3 christos for (const auto &per_cu : per_bfd->all_units) 1105 1.1 christos { 1106 1.1.1.2 christos if (to_underlying (per_cu->sect_off) 1107 1.1.1.2 christos >= (static_cast<uint64_t> (1) << 32)) 1108 1.1 christos return true; 1109 1.1 christos } 1110 1.1 christos return false; 1111 1.1 christos } 1112 1.1 christos 1113 1.1 christos /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek 1114 1.1 christos position is at the end of the file. */ 1115 1.1 christos 1116 1.1 christos static void 1117 1.1 christos assert_file_size (FILE *file, size_t expected_size) 1118 1.1 christos { 1119 1.1 christos const auto file_size = ftell (file); 1120 1.1 christos if (file_size == -1) 1121 1.1 christos perror_with_name (("ftell")); 1122 1.1 christos gdb_assert (file_size == expected_size); 1123 1.1 christos } 1124 1.1 christos 1125 1.1 christos /* Write a gdb index file to OUT_FILE from all the sections passed as 1126 1.1 christos arguments. */ 1127 1.1 christos 1128 1.1 christos static void 1129 1.1 christos write_gdbindex_1 (FILE *out_file, 1130 1.1 christos const data_buf &cu_list, 1131 1.1 christos const data_buf &types_cu_list, 1132 1.1 christos const data_buf &addr_vec, 1133 1.1 christos const data_buf &symtab_vec, 1134 1.1.1.3 christos const data_buf &constant_pool, 1135 1.1.1.3 christos const data_buf &shortcuts) 1136 1.1 christos { 1137 1.1 christos data_buf contents; 1138 1.1.1.3 christos const offset_type size_of_header = 7 * sizeof (offset_type); 1139 1.1.1.3 christos uint64_t total_len = size_of_header; 1140 1.1 christos 1141 1.1 christos /* The version number. */ 1142 1.1.1.3 christos contents.append_offset (9); 1143 1.1 christos 1144 1.1 christos /* The offset of the CU list from the start of the file. */ 1145 1.1.1.2 christos contents.append_offset (total_len); 1146 1.1 christos total_len += cu_list.size (); 1147 1.1 christos 1148 1.1 christos /* The offset of the types CU list from the start of the file. */ 1149 1.1.1.2 christos contents.append_offset (total_len); 1150 1.1 christos total_len += types_cu_list.size (); 1151 1.1 christos 1152 1.1 christos /* The offset of the address table from the start of the file. */ 1153 1.1.1.2 christos contents.append_offset (total_len); 1154 1.1 christos total_len += addr_vec.size (); 1155 1.1 christos 1156 1.1 christos /* The offset of the symbol table from the start of the file. */ 1157 1.1.1.2 christos contents.append_offset (total_len); 1158 1.1 christos total_len += symtab_vec.size (); 1159 1.1 christos 1160 1.1.1.3 christos /* The offset of the shortcut table from the start of the file. */ 1161 1.1.1.3 christos contents.append_offset (total_len); 1162 1.1.1.3 christos total_len += shortcuts.size (); 1163 1.1.1.3 christos 1164 1.1 christos /* The offset of the constant pool from the start of the file. */ 1165 1.1.1.2 christos contents.append_offset (total_len); 1166 1.1 christos total_len += constant_pool.size (); 1167 1.1 christos 1168 1.1 christos gdb_assert (contents.size () == size_of_header); 1169 1.1 christos 1170 1.1.1.3 christos /* The maximum size of an index file is limited by the maximum value 1171 1.1.1.3 christos capable of being represented by 'offset_type'. Throw an error if 1172 1.1.1.3 christos that length has been exceeded. */ 1173 1.1.1.3 christos size_t max_size = ~(offset_type) 0; 1174 1.1.1.3 christos if (total_len > max_size) 1175 1.1.1.3 christos error (_("gdb-index maximum file size of %zu exceeded"), max_size); 1176 1.1.1.3 christos 1177 1.1.1.3 christos if (out_file == nullptr) 1178 1.1.1.3 christos return; 1179 1.1.1.3 christos 1180 1.1 christos contents.file_write (out_file); 1181 1.1 christos cu_list.file_write (out_file); 1182 1.1 christos types_cu_list.file_write (out_file); 1183 1.1 christos addr_vec.file_write (out_file); 1184 1.1 christos symtab_vec.file_write (out_file); 1185 1.1.1.3 christos shortcuts.file_write (out_file); 1186 1.1 christos constant_pool.file_write (out_file); 1187 1.1 christos 1188 1.1 christos assert_file_size (out_file, total_len); 1189 1.1 christos } 1190 1.1 christos 1191 1.1.1.2 christos /* Write the contents of the internal "cooked" index. */ 1192 1.1.1.2 christos 1193 1.1.1.2 christos static void 1194 1.1.1.3 christos write_cooked_index (cooked_index *table, 1195 1.1.1.2 christos const cu_index_map &cu_index_htab, 1196 1.1.1.2 christos struct mapped_symtab *symtab) 1197 1.1.1.2 christos { 1198 1.1.1.2 christos for (const cooked_index_entry *entry : table->all_entries ()) 1199 1.1.1.2 christos { 1200 1.1.1.2 christos const auto it = cu_index_htab.find (entry->per_cu); 1201 1.1.1.2 christos gdb_assert (it != cu_index_htab.cend ()); 1202 1.1.1.2 christos 1203 1.1.1.3 christos const char *name = entry->full_name (symtab->obstack ()); 1204 1.1.1.2 christos 1205 1.1.1.3 christos if (entry->lang == language_ada) 1206 1.1.1.2 christos { 1207 1.1.1.3 christos /* In order for the index to work when read back into 1208 1.1.1.3 christos gdb, it has to use the encoded name, with any 1209 1.1.1.3 christos suffixes stripped. */ 1210 1.1.1.3 christos std::string encoded = ada_encode (name, false); 1211 1.1.1.3 christos name = obstack_strdup (symtab->obstack (), encoded.c_str ()); 1212 1.1.1.2 christos } 1213 1.1.1.3 christos else if (entry->lang == language_cplus 1214 1.1.1.2 christos && (entry->flags & IS_LINKAGE) != 0) 1215 1.1.1.2 christos { 1216 1.1.1.2 christos /* GDB never put C++ linkage names into .gdb_index. The 1217 1.1.1.2 christos theory here is that a linkage name will normally be in 1218 1.1.1.2 christos the minimal symbols anyway, so including it in the index 1219 1.1.1.2 christos is usually redundant -- and the cases where it would not 1220 1.1.1.2 christos be redundant are rare and not worth supporting. */ 1221 1.1.1.2 christos continue; 1222 1.1.1.2 christos } 1223 1.1.1.2 christos else if ((entry->flags & IS_TYPE_DECLARATION) != 0) 1224 1.1.1.2 christos { 1225 1.1.1.2 christos /* Don't add type declarations to the index. */ 1226 1.1.1.2 christos continue; 1227 1.1.1.2 christos } 1228 1.1.1.2 christos 1229 1.1.1.2 christos gdb_index_symbol_kind kind; 1230 1.1.1.3 christos if (entry->tag == DW_TAG_subprogram 1231 1.1.1.3 christos || entry->tag == DW_TAG_entry_point) 1232 1.1.1.2 christos kind = GDB_INDEX_SYMBOL_KIND_FUNCTION; 1233 1.1.1.2 christos else if (entry->tag == DW_TAG_variable 1234 1.1.1.2 christos || entry->tag == DW_TAG_constant 1235 1.1.1.2 christos || entry->tag == DW_TAG_enumerator) 1236 1.1.1.2 christos kind = GDB_INDEX_SYMBOL_KIND_VARIABLE; 1237 1.1.1.3 christos else if (tag_is_type (entry->tag)) 1238 1.1.1.2 christos kind = GDB_INDEX_SYMBOL_KIND_TYPE; 1239 1.1.1.3 christos else 1240 1.1.1.3 christos kind = GDB_INDEX_SYMBOL_KIND_OTHER; 1241 1.1.1.2 christos 1242 1.1.1.3 christos symtab->add_index_entry (name, (entry->flags & IS_STATIC) != 0, 1243 1.1.1.3 christos kind, it->second); 1244 1.1.1.2 christos } 1245 1.1.1.2 christos } 1246 1.1.1.2 christos 1247 1.1.1.3 christos /* Write shortcut information. */ 1248 1.1.1.3 christos 1249 1.1.1.3 christos static void 1250 1.1.1.3 christos write_shortcuts_table (cooked_index *table, data_buf &shortcuts, 1251 1.1.1.3 christos data_buf &cpool) 1252 1.1.1.3 christos { 1253 1.1.1.3 christos const auto main_info = table->get_main (); 1254 1.1.1.3 christos size_t main_name_offset = 0; 1255 1.1.1.3 christos dwarf_source_language dw_lang = (dwarf_source_language) 0; 1256 1.1.1.3 christos 1257 1.1.1.3 christos if (main_info != nullptr) 1258 1.1.1.3 christos { 1259 1.1.1.3 christos dw_lang = main_info->per_cu->dw_lang (); 1260 1.1.1.3 christos 1261 1.1.1.3 christos if (dw_lang != 0) 1262 1.1.1.3 christos { 1263 1.1.1.3 christos auto_obstack obstack; 1264 1.1.1.3 christos const auto main_name = main_info->full_name (&obstack, true); 1265 1.1.1.3 christos 1266 1.1.1.3 christos main_name_offset = cpool.size (); 1267 1.1.1.3 christos cpool.append_cstr0 (main_name); 1268 1.1.1.3 christos } 1269 1.1.1.3 christos } 1270 1.1.1.3 christos 1271 1.1.1.3 christos shortcuts.append_offset (dw_lang); 1272 1.1.1.3 christos shortcuts.append_offset (main_name_offset); 1273 1.1.1.3 christos } 1274 1.1.1.3 christos 1275 1.1 christos /* Write contents of a .gdb_index section for OBJFILE into OUT_FILE. 1276 1.1 christos If OBJFILE has an associated dwz file, write contents of a .gdb_index 1277 1.1 christos section for that dwz file into DWZ_OUT_FILE. If OBJFILE does not have an 1278 1.1 christos associated dwz file, DWZ_OUT_FILE must be NULL. */ 1279 1.1 christos 1280 1.1 christos static void 1281 1.1.1.3 christos write_gdbindex (dwarf2_per_bfd *per_bfd, cooked_index *table, 1282 1.1.1.2 christos FILE *out_file, FILE *dwz_out_file) 1283 1.1 christos { 1284 1.1 christos mapped_symtab symtab; 1285 1.1 christos data_buf objfile_cu_list; 1286 1.1 christos data_buf dwz_cu_list; 1287 1.1 christos 1288 1.1.1.2 christos /* While we're scanning CU's create a table that maps a dwarf2_per_cu_data 1289 1.1 christos (which is what addrmap records) to its index (which is what is recorded 1290 1.1 christos in the index file). This will later be needed to write the address 1291 1.1 christos table. */ 1292 1.1.1.2 christos cu_index_map cu_index_htab; 1293 1.1.1.3 christos cu_index_htab.reserve (per_bfd->all_units.size ()); 1294 1.1.1.2 christos 1295 1.1.1.2 christos /* Store out the .debug_type CUs, if any. */ 1296 1.1.1.2 christos data_buf types_cu_list; 1297 1.1 christos 1298 1.1 christos /* The CU list is already sorted, so we don't need to do additional 1299 1.1.1.3 christos work here. */ 1300 1.1 christos 1301 1.1.1.2 christos int counter = 0; 1302 1.1.1.3 christos for (int i = 0; i < per_bfd->all_units.size (); ++i) 1303 1.1 christos { 1304 1.1.1.3 christos dwarf2_per_cu_data *per_cu = per_bfd->all_units[i].get (); 1305 1.1 christos 1306 1.1.1.3 christos const auto insertpair = cu_index_htab.emplace (per_cu, counter); 1307 1.1.1.2 christos gdb_assert (insertpair.second); 1308 1.1 christos 1309 1.1.1.3 christos /* See enhancement PR symtab/30838. */ 1310 1.1.1.3 christos gdb_assert (!(per_cu->is_dwz && per_cu->is_debug_types)); 1311 1.1.1.3 christos 1312 1.1.1.2 christos /* The all_units list contains CUs read from the objfile as well as 1313 1.1 christos from the eventual dwz file. We need to place the entry in the 1314 1.1 christos corresponding index. */ 1315 1.1.1.2 christos data_buf &cu_list = (per_cu->is_debug_types 1316 1.1.1.2 christos ? types_cu_list 1317 1.1.1.2 christos : per_cu->is_dwz ? dwz_cu_list : objfile_cu_list); 1318 1.1 christos cu_list.append_uint (8, BFD_ENDIAN_LITTLE, 1319 1.1 christos to_underlying (per_cu->sect_off)); 1320 1.1.1.2 christos if (per_cu->is_debug_types) 1321 1.1.1.2 christos { 1322 1.1.1.2 christos signatured_type *sig_type = (signatured_type *) per_cu; 1323 1.1.1.2 christos cu_list.append_uint (8, BFD_ENDIAN_LITTLE, 1324 1.1.1.2 christos to_underlying (sig_type->type_offset_in_tu)); 1325 1.1.1.2 christos cu_list.append_uint (8, BFD_ENDIAN_LITTLE, 1326 1.1.1.2 christos sig_type->signature); 1327 1.1.1.2 christos } 1328 1.1.1.2 christos else 1329 1.1.1.2 christos cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length ()); 1330 1.1.1.2 christos 1331 1.1.1.3 christos ++counter; 1332 1.1 christos } 1333 1.1 christos 1334 1.1.1.2 christos write_cooked_index (table, cu_index_htab, &symtab); 1335 1.1.1.2 christos 1336 1.1 christos /* Dump the address map. */ 1337 1.1 christos data_buf addr_vec; 1338 1.1.1.2 christos for (auto map : table->get_addrmaps ()) 1339 1.1.1.2 christos write_address_map (map, addr_vec, cu_index_htab); 1340 1.1 christos 1341 1.1.1.3 christos /* Ensure symbol hash is built domestically. */ 1342 1.1.1.3 christos symtab.sort (); 1343 1.1.1.3 christos 1344 1.1 christos /* Now that we've processed all symbols we can shrink their cu_indices 1345 1.1 christos lists. */ 1346 1.1.1.2 christos symtab.minimize (); 1347 1.1 christos 1348 1.1 christos data_buf symtab_vec, constant_pool; 1349 1.1.1.2 christos 1350 1.1 christos write_hash_table (&symtab, symtab_vec, constant_pool); 1351 1.1 christos 1352 1.1.1.3 christos data_buf shortcuts; 1353 1.1.1.3 christos write_shortcuts_table (table, shortcuts, constant_pool); 1354 1.1.1.3 christos 1355 1.1.1.3 christos write_gdbindex_1 (out_file, objfile_cu_list, types_cu_list, addr_vec, 1356 1.1.1.3 christos symtab_vec, constant_pool, shortcuts); 1357 1.1 christos 1358 1.1 christos if (dwz_out_file != NULL) 1359 1.1.1.3 christos write_gdbindex_1 (dwz_out_file, dwz_cu_list, {}, {}, {}, {}, {}); 1360 1.1 christos else 1361 1.1 christos gdb_assert (dwz_cu_list.empty ()); 1362 1.1 christos } 1363 1.1 christos 1364 1.1 christos /* Write a new .debug_names section for OBJFILE into OUT_FILE, write 1365 1.1 christos needed addition to .debug_str section to OUT_FILE_STR. Return how 1366 1.1 christos many bytes were expected to be written into OUT_FILE. */ 1367 1.1 christos 1368 1.1 christos static void 1369 1.1.1.3 christos write_debug_names (dwarf2_per_bfd *per_bfd, cooked_index *table, 1370 1.1 christos FILE *out_file, FILE *out_file_str) 1371 1.1 christos { 1372 1.1.1.3 christos const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (per_bfd); 1373 1.1 christos const enum bfd_endian dwarf5_byte_order 1374 1.1.1.3 christos = bfd_big_endian (per_bfd->obfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE; 1375 1.1 christos 1376 1.1 christos /* The CU list is already sorted, so we don't need to do additional 1377 1.1 christos work here. Also, the debug_types entries do not appear in 1378 1.1.1.2 christos all_units, but only in their own hash table. */ 1379 1.1 christos data_buf cu_list; 1380 1.1.1.2 christos data_buf types_cu_list; 1381 1.1.1.3 christos debug_names nametable (per_bfd, dwarf5_is_dwarf64, dwarf5_byte_order); 1382 1.1.1.2 christos int counter = 0; 1383 1.1.1.2 christos int types_counter = 0; 1384 1.1.1.3 christos for (int i = 0; i < per_bfd->all_units.size (); ++i) 1385 1.1.1.2 christos { 1386 1.1.1.3 christos dwarf2_per_cu_data *per_cu = per_bfd->all_units[i].get (); 1387 1.1 christos 1388 1.1.1.2 christos int &this_counter = per_cu->is_debug_types ? types_counter : counter; 1389 1.1.1.2 christos data_buf &this_list = per_cu->is_debug_types ? types_cu_list : cu_list; 1390 1.1 christos 1391 1.1.1.2 christos nametable.add_cu (per_cu, this_counter); 1392 1.1.1.2 christos this_list.append_uint (nametable.dwarf5_offset_size (), 1393 1.1.1.2 christos dwarf5_byte_order, 1394 1.1.1.2 christos to_underlying (per_cu->sect_off)); 1395 1.1.1.2 christos ++this_counter; 1396 1.1 christos } 1397 1.1 christos 1398 1.1.1.2 christos /* Verify that all units are represented. */ 1399 1.1.1.3 christos gdb_assert (counter == per_bfd->all_comp_units.size ()); 1400 1.1.1.3 christos gdb_assert (types_counter == per_bfd->all_type_units.size ()); 1401 1.1 christos 1402 1.1.1.2 christos for (const cooked_index_entry *entry : table->all_entries ()) 1403 1.1.1.2 christos nametable.insert (entry); 1404 1.1 christos 1405 1.1 christos nametable.build (); 1406 1.1 christos 1407 1.1 christos /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */ 1408 1.1 christos 1409 1.1 christos const offset_type bytes_of_header 1410 1.1 christos = ((dwarf5_is_dwarf64 ? 12 : 4) 1411 1.1 christos + 2 + 2 + 7 * 4 1412 1.1.1.3 christos + sizeof (dwarf5_augmentation)); 1413 1.1 christos size_t expected_bytes = 0; 1414 1.1 christos expected_bytes += bytes_of_header; 1415 1.1 christos expected_bytes += cu_list.size (); 1416 1.1 christos expected_bytes += types_cu_list.size (); 1417 1.1 christos expected_bytes += nametable.bytes (); 1418 1.1 christos data_buf header; 1419 1.1 christos 1420 1.1 christos if (!dwarf5_is_dwarf64) 1421 1.1 christos { 1422 1.1 christos const uint64_t size64 = expected_bytes - 4; 1423 1.1 christos gdb_assert (size64 < 0xfffffff0); 1424 1.1 christos header.append_uint (4, dwarf5_byte_order, size64); 1425 1.1 christos } 1426 1.1 christos else 1427 1.1 christos { 1428 1.1 christos header.append_uint (4, dwarf5_byte_order, 0xffffffff); 1429 1.1 christos header.append_uint (8, dwarf5_byte_order, expected_bytes - 12); 1430 1.1 christos } 1431 1.1 christos 1432 1.1 christos /* The version number. */ 1433 1.1 christos header.append_uint (2, dwarf5_byte_order, 5); 1434 1.1 christos 1435 1.1 christos /* Padding. */ 1436 1.1 christos header.append_uint (2, dwarf5_byte_order, 0); 1437 1.1 christos 1438 1.1 christos /* comp_unit_count - The number of CUs in the CU list. */ 1439 1.1.1.2 christos header.append_uint (4, dwarf5_byte_order, counter); 1440 1.1 christos 1441 1.1 christos /* local_type_unit_count - The number of TUs in the local TU 1442 1.1 christos list. */ 1443 1.1.1.2 christos header.append_uint (4, dwarf5_byte_order, types_counter); 1444 1.1 christos 1445 1.1 christos /* foreign_type_unit_count - The number of TUs in the foreign TU 1446 1.1 christos list. */ 1447 1.1 christos header.append_uint (4, dwarf5_byte_order, 0); 1448 1.1 christos 1449 1.1 christos /* bucket_count - The number of hash buckets in the hash lookup 1450 1.1.1.3 christos table. GDB does not use the hash table, so there's also no need 1451 1.1.1.3 christos to write it -- plus, the hash table is broken as defined due to 1452 1.1.1.3 christos the lack of name canonicalization. */ 1453 1.1.1.3 christos header.append_uint (4, dwarf5_byte_order, 0); 1454 1.1 christos 1455 1.1 christos /* name_count - The number of unique names in the index. */ 1456 1.1 christos header.append_uint (4, dwarf5_byte_order, nametable.name_count ()); 1457 1.1 christos 1458 1.1 christos /* abbrev_table_size - The size in bytes of the abbreviations 1459 1.1 christos table. */ 1460 1.1 christos header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ()); 1461 1.1 christos 1462 1.1 christos /* augmentation_string_size - The size in bytes of the augmentation 1463 1.1 christos string. This value is rounded up to a multiple of 4. */ 1464 1.1.1.3 christos static_assert (sizeof (dwarf5_augmentation) % 4 == 0); 1465 1.1.1.3 christos header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_augmentation)); 1466 1.1.1.3 christos header.append_array (dwarf5_augmentation); 1467 1.1 christos 1468 1.1 christos gdb_assert (header.size () == bytes_of_header); 1469 1.1 christos 1470 1.1 christos header.file_write (out_file); 1471 1.1 christos cu_list.file_write (out_file); 1472 1.1 christos types_cu_list.file_write (out_file); 1473 1.1 christos nametable.file_write (out_file, out_file_str); 1474 1.1 christos 1475 1.1 christos assert_file_size (out_file, expected_bytes); 1476 1.1 christos } 1477 1.1 christos 1478 1.1 christos /* This represents an index file being written (work-in-progress). 1479 1.1 christos 1480 1.1 christos The data is initially written to a temporary file. When the finalize method 1481 1.1 christos is called, the file is closed and moved to its final location. 1482 1.1 christos 1483 1.1 christos On failure (if this object is being destroyed with having called finalize), 1484 1.1 christos the temporary file is closed and deleted. */ 1485 1.1 christos 1486 1.1 christos struct index_wip_file 1487 1.1 christos { 1488 1.1 christos index_wip_file (const char *dir, const char *basename, 1489 1.1 christos const char *suffix) 1490 1.1 christos { 1491 1.1.1.3 christos /* Validate DIR is a valid directory. */ 1492 1.1.1.3 christos struct stat buf; 1493 1.1.1.3 christos if (stat (dir, &buf) == -1) 1494 1.1.1.3 christos perror_with_name (string_printf (_("`%s'"), dir).c_str ()); 1495 1.1.1.3 christos if ((buf.st_mode & S_IFDIR) != S_IFDIR) 1496 1.1.1.3 christos error (_("`%s': Is not a directory."), dir); 1497 1.1.1.3 christos 1498 1.1 christos filename = (std::string (dir) + SLASH_STRING + basename 1499 1.1.1.2 christos + suffix); 1500 1.1 christos 1501 1.1 christos filename_temp = make_temp_filename (filename); 1502 1.1 christos 1503 1.1.1.2 christos scoped_fd out_file_fd = gdb_mkostemp_cloexec (filename_temp.data (), 1504 1.1.1.2 christos O_BINARY); 1505 1.1 christos if (out_file_fd.get () == -1) 1506 1.1.1.3 christos perror_with_name (string_printf (_("couldn't open `%s'"), 1507 1.1.1.3 christos filename_temp.data ()).c_str ()); 1508 1.1 christos 1509 1.1 christos out_file = out_file_fd.to_file ("wb"); 1510 1.1 christos 1511 1.1 christos if (out_file == nullptr) 1512 1.1 christos error (_("Can't open `%s' for writing"), filename_temp.data ()); 1513 1.1 christos 1514 1.1 christos unlink_file.emplace (filename_temp.data ()); 1515 1.1 christos } 1516 1.1 christos 1517 1.1 christos void finalize () 1518 1.1 christos { 1519 1.1 christos /* We want to keep the file. */ 1520 1.1 christos unlink_file->keep (); 1521 1.1 christos 1522 1.1 christos /* Close and move the str file in place. */ 1523 1.1 christos unlink_file.reset (); 1524 1.1 christos if (rename (filename_temp.data (), filename.c_str ()) != 0) 1525 1.1 christos perror_with_name (("rename")); 1526 1.1 christos } 1527 1.1 christos 1528 1.1 christos std::string filename; 1529 1.1 christos gdb::char_vector filename_temp; 1530 1.1 christos 1531 1.1 christos /* Order matters here; we want FILE to be closed before 1532 1.1 christos FILENAME_TEMP is unlinked, because on MS-Windows one cannot 1533 1.1 christos delete a file that is still open. So, we wrap the unlinker in an 1534 1.1 christos optional and emplace it once we know the file name. */ 1535 1.1.1.3 christos std::optional<gdb::unlinker> unlink_file; 1536 1.1 christos 1537 1.1 christos gdb_file_up out_file; 1538 1.1 christos }; 1539 1.1 christos 1540 1.1 christos /* See dwarf-index-write.h. */ 1541 1.1 christos 1542 1.1 christos void 1543 1.1.1.3 christos write_dwarf_index (dwarf2_per_bfd *per_bfd, const char *dir, 1544 1.1.1.2 christos const char *basename, const char *dwz_basename, 1545 1.1.1.2 christos dw_index_kind index_kind) 1546 1.1 christos { 1547 1.1.1.3 christos if (per_bfd->index_table == nullptr) 1548 1.1.1.2 christos error (_("No debugging symbols")); 1549 1.1.1.3 christos cooked_index *table = per_bfd->index_table->index_for_writing (); 1550 1.1.1.3 christos if (table == nullptr) 1551 1.1.1.3 christos error (_("Cannot use an index to create the index")); 1552 1.1 christos 1553 1.1.1.3 christos if (per_bfd->types.size () > 1) 1554 1.1 christos error (_("Cannot make an index when the file has multiple .debug_types sections")); 1555 1.1 christos 1556 1.1 christos const char *index_suffix = (index_kind == dw_index_kind::DEBUG_NAMES 1557 1.1 christos ? INDEX5_SUFFIX : INDEX4_SUFFIX); 1558 1.1 christos 1559 1.1 christos index_wip_file objfile_index_wip (dir, basename, index_suffix); 1560 1.1.1.3 christos std::optional<index_wip_file> dwz_index_wip; 1561 1.1 christos 1562 1.1 christos if (dwz_basename != NULL) 1563 1.1 christos dwz_index_wip.emplace (dir, dwz_basename, index_suffix); 1564 1.1 christos 1565 1.1 christos if (index_kind == dw_index_kind::DEBUG_NAMES) 1566 1.1 christos { 1567 1.1 christos index_wip_file str_wip_file (dir, basename, DEBUG_STR_SUFFIX); 1568 1.1 christos 1569 1.1.1.3 christos write_debug_names (per_bfd, table, objfile_index_wip.out_file.get (), 1570 1.1 christos str_wip_file.out_file.get ()); 1571 1.1 christos 1572 1.1 christos str_wip_file.finalize (); 1573 1.1 christos } 1574 1.1 christos else 1575 1.1.1.3 christos write_gdbindex (per_bfd, table, objfile_index_wip.out_file.get (), 1576 1.1 christos (dwz_index_wip.has_value () 1577 1.1 christos ? dwz_index_wip->out_file.get () : NULL)); 1578 1.1 christos 1579 1.1 christos objfile_index_wip.finalize (); 1580 1.1 christos 1581 1.1 christos if (dwz_index_wip.has_value ()) 1582 1.1 christos dwz_index_wip->finalize (); 1583 1.1 christos } 1584 1.1 christos 1585 1.1.1.3 christos /* Options structure for the 'save gdb-index' command. */ 1586 1.1.1.3 christos 1587 1.1.1.3 christos struct save_gdb_index_options 1588 1.1.1.3 christos { 1589 1.1.1.3 christos bool dwarf_5 = false; 1590 1.1.1.3 christos }; 1591 1.1.1.3 christos 1592 1.1.1.3 christos /* The option_def list for the 'save gdb-index' command. */ 1593 1.1.1.3 christos 1594 1.1.1.3 christos static const gdb::option::option_def save_gdb_index_options_defs[] = { 1595 1.1.1.3 christos gdb::option::boolean_option_def<save_gdb_index_options> { 1596 1.1.1.3 christos "dwarf-5", 1597 1.1.1.3 christos [] (save_gdb_index_options *opt) { return &opt->dwarf_5; }, 1598 1.1.1.3 christos nullptr, /* show_cmd_cb */ 1599 1.1.1.3 christos nullptr /* set_doc */ 1600 1.1.1.3 christos } 1601 1.1.1.3 christos }; 1602 1.1.1.3 christos 1603 1.1.1.3 christos /* Create an options_def_group for the 'save gdb-index' command. */ 1604 1.1.1.3 christos 1605 1.1.1.3 christos static gdb::option::option_def_group 1606 1.1.1.3 christos make_gdb_save_index_options_def_group (save_gdb_index_options *opts) 1607 1.1.1.3 christos { 1608 1.1.1.3 christos return {{save_gdb_index_options_defs}, opts}; 1609 1.1.1.3 christos } 1610 1.1.1.3 christos 1611 1.1.1.3 christos /* Completer for the "save gdb-index" command. */ 1612 1.1.1.3 christos 1613 1.1.1.3 christos static void 1614 1.1.1.3 christos gdb_save_index_cmd_completer (struct cmd_list_element *ignore, 1615 1.1.1.3 christos completion_tracker &tracker, 1616 1.1.1.3 christos const char *text, const char *word) 1617 1.1.1.3 christos { 1618 1.1.1.3 christos auto grp = make_gdb_save_index_options_def_group (nullptr); 1619 1.1.1.3 christos if (gdb::option::complete_options 1620 1.1.1.3 christos (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp)) 1621 1.1.1.3 christos return; 1622 1.1.1.3 christos 1623 1.1.1.3 christos word = advance_to_filename_complete_word_point (tracker, text); 1624 1.1.1.3 christos filename_completer (ignore, tracker, text, word); 1625 1.1.1.3 christos } 1626 1.1.1.3 christos 1627 1.1 christos /* Implementation of the `save gdb-index' command. 1628 1.1 christos 1629 1.1 christos Note that the .gdb_index file format used by this command is 1630 1.1 christos documented in the GDB manual. Any changes here must be documented 1631 1.1 christos there. */ 1632 1.1 christos 1633 1.1 christos static void 1634 1.1.1.3 christos save_gdb_index_command (const char *args, int from_tty) 1635 1.1 christos { 1636 1.1.1.3 christos save_gdb_index_options opts; 1637 1.1.1.3 christos const auto group = make_gdb_save_index_options_def_group (&opts); 1638 1.1.1.3 christos gdb::option::process_options 1639 1.1.1.3 christos (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group); 1640 1.1 christos 1641 1.1.1.3 christos if (args == nullptr || *args == '\0') 1642 1.1 christos error (_("usage: save gdb-index [-dwarf-5] DIRECTORY")); 1643 1.1 christos 1644 1.1.1.3 christos std::string directory (gdb_tilde_expand (args)); 1645 1.1.1.3 christos dw_index_kind index_kind 1646 1.1.1.3 christos = (opts.dwarf_5 ? dw_index_kind::DEBUG_NAMES : dw_index_kind::GDB_INDEX); 1647 1.1.1.3 christos 1648 1.1 christos for (objfile *objfile : current_program_space->objfiles ()) 1649 1.1 christos { 1650 1.1 christos /* If the objfile does not correspond to an actual file, skip it. */ 1651 1.1.1.2 christos if ((objfile->flags & OBJF_NOT_FILENAME) != 0) 1652 1.1 christos continue; 1653 1.1 christos 1654 1.1 christos dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile); 1655 1.1 christos 1656 1.1 christos if (per_objfile != NULL) 1657 1.1 christos { 1658 1.1 christos try 1659 1.1 christos { 1660 1.1 christos const char *basename = lbasename (objfile_name (objfile)); 1661 1.1 christos const dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd); 1662 1.1 christos const char *dwz_basename = NULL; 1663 1.1 christos 1664 1.1 christos if (dwz != NULL) 1665 1.1 christos dwz_basename = lbasename (dwz->filename ()); 1666 1.1 christos 1667 1.1.1.3 christos write_dwarf_index (per_objfile->per_bfd, directory.c_str (), 1668 1.1.1.3 christos basename, dwz_basename, index_kind); 1669 1.1 christos } 1670 1.1 christos catch (const gdb_exception_error &except) 1671 1.1 christos { 1672 1.1 christos exception_fprintf (gdb_stderr, except, 1673 1.1 christos _("Error while writing index for `%s': "), 1674 1.1 christos objfile_name (objfile)); 1675 1.1 christos } 1676 1.1.1.3 christos } 1677 1.1 christos 1678 1.1 christos } 1679 1.1 christos } 1680 1.1 christos 1681 1.1.1.3 christos #if GDB_SELF_TEST 1682 1.1.1.3 christos #include "gdbsupport/selftest.h" 1683 1.1.1.3 christos 1684 1.1.1.3 christos namespace selftests { 1685 1.1.1.3 christos 1686 1.1.1.3 christos class pretend_data_buf : public data_buf 1687 1.1.1.3 christos { 1688 1.1.1.3 christos public: 1689 1.1.1.3 christos /* Set the pretend size. */ 1690 1.1.1.3 christos void set_pretend_size (size_t s) { 1691 1.1.1.3 christos m_pretend_size = s; 1692 1.1.1.3 christos } 1693 1.1.1.3 christos 1694 1.1.1.3 christos /* Override size method of data_buf, returning the pretend size instead. */ 1695 1.1.1.3 christos size_t size () const override { 1696 1.1.1.3 christos return m_pretend_size; 1697 1.1.1.3 christos } 1698 1.1.1.3 christos 1699 1.1.1.3 christos private: 1700 1.1.1.3 christos size_t m_pretend_size = 0; 1701 1.1.1.3 christos }; 1702 1.1.1.3 christos 1703 1.1.1.3 christos static void 1704 1.1.1.3 christos gdb_index () 1705 1.1.1.3 christos { 1706 1.1.1.3 christos pretend_data_buf cu_list; 1707 1.1.1.3 christos pretend_data_buf types_cu_list; 1708 1.1.1.3 christos pretend_data_buf addr_vec; 1709 1.1.1.3 christos pretend_data_buf symtab_vec; 1710 1.1.1.3 christos pretend_data_buf constant_pool; 1711 1.1.1.3 christos pretend_data_buf short_cuts; 1712 1.1.1.3 christos 1713 1.1.1.3 christos const size_t size_of_header = 7 * sizeof (offset_type); 1714 1.1.1.3 christos 1715 1.1.1.3 christos /* Test that an overly large index will throw an error. */ 1716 1.1.1.3 christos symtab_vec.set_pretend_size (~(offset_type)0 - size_of_header); 1717 1.1.1.3 christos constant_pool.set_pretend_size (1); 1718 1.1.1.3 christos 1719 1.1.1.3 christos bool saw_exception = false; 1720 1.1.1.3 christos try 1721 1.1.1.3 christos { 1722 1.1.1.3 christos write_gdbindex_1 (nullptr, cu_list, types_cu_list, addr_vec, 1723 1.1.1.3 christos symtab_vec, constant_pool, short_cuts); 1724 1.1.1.3 christos } 1725 1.1.1.3 christos catch (const gdb_exception_error &e) 1726 1.1.1.3 christos { 1727 1.1.1.3 christos SELF_CHECK (e.reason == RETURN_ERROR); 1728 1.1.1.3 christos SELF_CHECK (e.error == GENERIC_ERROR); 1729 1.1.1.3 christos SELF_CHECK (e.message->find (_("gdb-index maximum file size of")) 1730 1.1.1.3 christos != std::string::npos); 1731 1.1.1.3 christos SELF_CHECK (e.message->find (_("exceeded")) != std::string::npos); 1732 1.1.1.3 christos saw_exception = true; 1733 1.1.1.3 christos } 1734 1.1.1.3 christos SELF_CHECK (saw_exception); 1735 1.1.1.3 christos 1736 1.1.1.3 christos /* Test that the largest possible index will not throw an error. */ 1737 1.1.1.3 christos constant_pool.set_pretend_size (0); 1738 1.1.1.3 christos 1739 1.1.1.3 christos saw_exception = false; 1740 1.1.1.3 christos try 1741 1.1.1.3 christos { 1742 1.1.1.3 christos write_gdbindex_1 (nullptr, cu_list, types_cu_list, addr_vec, 1743 1.1.1.3 christos symtab_vec, constant_pool, short_cuts); 1744 1.1.1.3 christos } 1745 1.1.1.3 christos catch (const gdb_exception_error &e) 1746 1.1.1.3 christos { 1747 1.1.1.3 christos saw_exception = true; 1748 1.1.1.3 christos } 1749 1.1.1.3 christos SELF_CHECK (!saw_exception); 1750 1.1.1.3 christos } 1751 1.1.1.3 christos 1752 1.1.1.3 christos } /* selftests namespace. */ 1753 1.1.1.3 christos #endif 1754 1.1.1.3 christos 1755 1.1 christos void _initialize_dwarf_index_write (); 1756 1.1 christos void 1757 1.1 christos _initialize_dwarf_index_write () 1758 1.1 christos { 1759 1.1.1.3 christos #if GDB_SELF_TEST 1760 1.1.1.3 christos selftests::register_test ("gdb_index", selftests::gdb_index); 1761 1.1.1.3 christos #endif 1762 1.1.1.3 christos 1763 1.1 christos cmd_list_element *c = add_cmd ("gdb-index", class_files, 1764 1.1 christos save_gdb_index_command, _("\ 1765 1.1 christos Save a gdb-index file.\n\ 1766 1.1 christos Usage: save gdb-index [-dwarf-5] DIRECTORY\n\ 1767 1.1 christos \n\ 1768 1.1 christos No options create one file with .gdb-index extension for pre-DWARF-5\n\ 1769 1.1 christos compatible .gdb_index section. With -dwarf-5 creates two files with\n\ 1770 1.1 christos extension .debug_names and .debug_str for DWARF-5 .debug_names section."), 1771 1.1 christos &save_cmdlist); 1772 1.1.1.3 christos set_cmd_completer_handle_brkchars (c, gdb_save_index_cmd_completer); 1773 1.1 christos } 1774