Commit | Line | Data |
---|---|---|
cd4fb1b2 SM |
1 | /* DWARF index writing support for GDB. |
2 | ||
3 | Copyright (C) 1994-2018 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
21 | ||
22 | #include "addrmap.h" | |
23 | #include "cli/cli-decode.h" | |
24 | #include "common/byte-vector.h" | |
25 | #include "common/filestuff.h" | |
26 | #include "common/gdb_unlinker.h" | |
87d6a7aa | 27 | #include "common/scoped_fd.h" |
cd4fb1b2 SM |
28 | #include "complaints.h" |
29 | #include "dwarf-index-common.h" | |
30 | #include "dwarf2.h" | |
31 | #include "dwarf2read.h" | |
32 | #include "gdb/gdb-index.h" | |
33 | #include "gdbcmd.h" | |
34 | #include "objfiles.h" | |
35 | #include "psympriv.h" | |
36 | ||
37 | #include <algorithm> | |
608219fb | 38 | #include <cmath> |
cd4fb1b2 SM |
39 | #include <set> |
40 | #include <unordered_map> | |
41 | #include <unordered_set> | |
42 | ||
cd4fb1b2 SM |
43 | /* Ensure only legit values are used. */ |
44 | #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \ | |
45 | do { \ | |
46 | gdb_assert ((unsigned int) (value) <= 1); \ | |
47 | GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \ | |
48 | } while (0) | |
49 | ||
50 | /* Ensure only legit values are used. */ | |
51 | #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \ | |
52 | do { \ | |
53 | gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \ | |
54 | && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \ | |
55 | GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \ | |
56 | } while (0) | |
57 | ||
58 | /* Ensure we don't use more than the alloted nuber of bits for the CU. */ | |
59 | #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \ | |
60 | do { \ | |
61 | gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \ | |
62 | GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \ | |
63 | } while (0) | |
64 | ||
65 | /* The "save gdb-index" command. */ | |
66 | ||
67 | /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with | |
68 | error checking. */ | |
69 | ||
70 | static void | |
71 | file_write (FILE *file, const void *data, size_t size) | |
72 | { | |
73 | if (fwrite (data, 1, size, file) != size) | |
74 | error (_("couldn't data write to file")); | |
75 | } | |
76 | ||
77 | /* Write the contents of VEC to FILE, with error checking. */ | |
78 | ||
79 | template<typename Elem, typename Alloc> | |
80 | static void | |
81 | file_write (FILE *file, const std::vector<Elem, Alloc> &vec) | |
82 | { | |
83 | file_write (file, vec.data (), vec.size () * sizeof (vec[0])); | |
84 | } | |
85 | ||
86 | /* In-memory buffer to prepare data to be written later to a file. */ | |
87 | class data_buf | |
88 | { | |
89 | public: | |
90 | /* Copy DATA to the end of the buffer. */ | |
91 | template<typename T> | |
92 | void append_data (const T &data) | |
93 | { | |
94 | std::copy (reinterpret_cast<const gdb_byte *> (&data), | |
95 | reinterpret_cast<const gdb_byte *> (&data + 1), | |
96 | grow (sizeof (data))); | |
97 | } | |
98 | ||
99 | /* Copy CSTR (a zero-terminated string) to the end of buffer. The | |
100 | terminating zero is appended too. */ | |
101 | void append_cstr0 (const char *cstr) | |
102 | { | |
103 | const size_t size = strlen (cstr) + 1; | |
104 | std::copy (cstr, cstr + size, grow (size)); | |
105 | } | |
106 | ||
107 | /* Store INPUT as ULEB128 to the end of buffer. */ | |
108 | void append_unsigned_leb128 (ULONGEST input) | |
109 | { | |
110 | for (;;) | |
111 | { | |
112 | gdb_byte output = input & 0x7f; | |
113 | input >>= 7; | |
114 | if (input) | |
115 | output |= 0x80; | |
116 | append_data (output); | |
117 | if (input == 0) | |
118 | break; | |
119 | } | |
120 | } | |
121 | ||
122 | /* Accept a host-format integer in VAL and append it to the buffer | |
123 | as a target-format integer which is LEN bytes long. */ | |
124 | void append_uint (size_t len, bfd_endian byte_order, ULONGEST val) | |
125 | { | |
126 | ::store_unsigned_integer (grow (len), len, byte_order, val); | |
127 | } | |
128 | ||
129 | /* Return the size of the buffer. */ | |
130 | size_t size () const | |
131 | { | |
132 | return m_vec.size (); | |
133 | } | |
134 | ||
135 | /* Return true iff the buffer is empty. */ | |
136 | bool empty () const | |
137 | { | |
138 | return m_vec.empty (); | |
139 | } | |
140 | ||
141 | /* Write the buffer to FILE. */ | |
142 | void file_write (FILE *file) const | |
143 | { | |
144 | ::file_write (file, m_vec); | |
145 | } | |
146 | ||
147 | private: | |
148 | /* Grow SIZE bytes at the end of the buffer. Returns a pointer to | |
149 | the start of the new block. */ | |
150 | gdb_byte *grow (size_t size) | |
151 | { | |
152 | m_vec.resize (m_vec.size () + size); | |
b4be9bfd | 153 | return &*(m_vec.end () - size); |
cd4fb1b2 SM |
154 | } |
155 | ||
156 | gdb::byte_vector m_vec; | |
157 | }; | |
158 | ||
159 | /* An entry in the symbol table. */ | |
160 | struct symtab_index_entry | |
161 | { | |
162 | /* The name of the symbol. */ | |
163 | const char *name; | |
164 | /* The offset of the name in the constant pool. */ | |
165 | offset_type index_offset; | |
166 | /* A sorted vector of the indices of all the CUs that hold an object | |
167 | of this name. */ | |
168 | std::vector<offset_type> cu_indices; | |
169 | }; | |
170 | ||
171 | /* The symbol table. This is a power-of-2-sized hash table. */ | |
172 | struct mapped_symtab | |
173 | { | |
174 | mapped_symtab () | |
175 | { | |
176 | data.resize (1024); | |
177 | } | |
178 | ||
179 | offset_type n_elements = 0; | |
180 | std::vector<symtab_index_entry> data; | |
181 | }; | |
182 | ||
183 | /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to | |
184 | the slot. | |
185 | ||
186 | Function is used only during write_hash_table so no index format backward | |
187 | compatibility is needed. */ | |
188 | ||
189 | static symtab_index_entry & | |
190 | find_slot (struct mapped_symtab *symtab, const char *name) | |
191 | { | |
192 | offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name); | |
193 | ||
194 | index = hash & (symtab->data.size () - 1); | |
195 | step = ((hash * 17) & (symtab->data.size () - 1)) | 1; | |
196 | ||
197 | for (;;) | |
198 | { | |
199 | if (symtab->data[index].name == NULL | |
200 | || strcmp (name, symtab->data[index].name) == 0) | |
201 | return symtab->data[index]; | |
202 | index = (index + step) & (symtab->data.size () - 1); | |
203 | } | |
204 | } | |
205 | ||
206 | /* Expand SYMTAB's hash table. */ | |
207 | ||
208 | static void | |
209 | hash_expand (struct mapped_symtab *symtab) | |
210 | { | |
211 | auto old_entries = std::move (symtab->data); | |
212 | ||
213 | symtab->data.clear (); | |
214 | symtab->data.resize (old_entries.size () * 2); | |
215 | ||
216 | for (auto &it : old_entries) | |
217 | if (it.name != NULL) | |
218 | { | |
219 | auto &ref = find_slot (symtab, it.name); | |
220 | ref = std::move (it); | |
221 | } | |
222 | } | |
223 | ||
224 | /* Add an entry to SYMTAB. NAME is the name of the symbol. | |
225 | CU_INDEX is the index of the CU in which the symbol appears. | |
226 | IS_STATIC is one if the symbol is static, otherwise zero (global). */ | |
227 | ||
228 | static void | |
229 | add_index_entry (struct mapped_symtab *symtab, const char *name, | |
230 | int is_static, gdb_index_symbol_kind kind, | |
231 | offset_type cu_index) | |
232 | { | |
233 | offset_type cu_index_and_attrs; | |
234 | ||
235 | ++symtab->n_elements; | |
236 | if (4 * symtab->n_elements / 3 >= symtab->data.size ()) | |
237 | hash_expand (symtab); | |
238 | ||
239 | symtab_index_entry &slot = find_slot (symtab, name); | |
240 | if (slot.name == NULL) | |
241 | { | |
242 | slot.name = name; | |
243 | /* index_offset is set later. */ | |
244 | } | |
245 | ||
246 | cu_index_and_attrs = 0; | |
247 | DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index); | |
248 | DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static); | |
249 | DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind); | |
250 | ||
251 | /* We don't want to record an index value twice as we want to avoid the | |
252 | duplication. | |
253 | We process all global symbols and then all static symbols | |
254 | (which would allow us to avoid the duplication by only having to check | |
255 | the last entry pushed), but a symbol could have multiple kinds in one CU. | |
256 | To keep things simple we don't worry about the duplication here and | |
257 | sort and uniqufy the list after we've processed all symbols. */ | |
258 | slot.cu_indices.push_back (cu_index_and_attrs); | |
259 | } | |
260 | ||
261 | /* Sort and remove duplicates of all symbols' cu_indices lists. */ | |
262 | ||
263 | static void | |
264 | uniquify_cu_indices (struct mapped_symtab *symtab) | |
265 | { | |
266 | for (auto &entry : symtab->data) | |
267 | { | |
268 | if (entry.name != NULL && !entry.cu_indices.empty ()) | |
269 | { | |
270 | auto &cu_indices = entry.cu_indices; | |
271 | std::sort (cu_indices.begin (), cu_indices.end ()); | |
272 | auto from = std::unique (cu_indices.begin (), cu_indices.end ()); | |
273 | cu_indices.erase (from, cu_indices.end ()); | |
274 | } | |
275 | } | |
276 | } | |
277 | ||
278 | /* A form of 'const char *' suitable for container keys. Only the | |
279 | pointer is stored. The strings themselves are compared, not the | |
280 | pointers. */ | |
281 | class c_str_view | |
282 | { | |
283 | public: | |
284 | c_str_view (const char *cstr) | |
285 | : m_cstr (cstr) | |
286 | {} | |
287 | ||
288 | bool operator== (const c_str_view &other) const | |
289 | { | |
290 | return strcmp (m_cstr, other.m_cstr) == 0; | |
291 | } | |
292 | ||
293 | /* Return the underlying C string. Note, the returned string is | |
294 | only a reference with lifetime of this object. */ | |
295 | const char *c_str () const | |
296 | { | |
297 | return m_cstr; | |
298 | } | |
299 | ||
300 | private: | |
301 | friend class c_str_view_hasher; | |
302 | const char *const m_cstr; | |
303 | }; | |
304 | ||
305 | /* A std::unordered_map::hasher for c_str_view that uses the right | |
306 | hash function for strings in a mapped index. */ | |
307 | class c_str_view_hasher | |
308 | { | |
309 | public: | |
310 | size_t operator () (const c_str_view &x) const | |
311 | { | |
312 | return mapped_index_string_hash (INT_MAX, x.m_cstr); | |
313 | } | |
314 | }; | |
315 | ||
316 | /* A std::unordered_map::hasher for std::vector<>. */ | |
317 | template<typename T> | |
318 | class vector_hasher | |
319 | { | |
320 | public: | |
321 | size_t operator () (const std::vector<T> &key) const | |
322 | { | |
323 | return iterative_hash (key.data (), | |
324 | sizeof (key.front ()) * key.size (), 0); | |
325 | } | |
326 | }; | |
327 | ||
328 | /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with | |
329 | constant pool entries going into the data buffer CPOOL. */ | |
330 | ||
331 | static void | |
332 | write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool) | |
333 | { | |
334 | { | |
335 | /* Elements are sorted vectors of the indices of all the CUs that | |
336 | hold an object of this name. */ | |
337 | std::unordered_map<std::vector<offset_type>, offset_type, | |
338 | vector_hasher<offset_type>> | |
339 | symbol_hash_table; | |
340 | ||
341 | /* We add all the index vectors to the constant pool first, to | |
342 | ensure alignment is ok. */ | |
343 | for (symtab_index_entry &entry : symtab->data) | |
344 | { | |
345 | if (entry.name == NULL) | |
346 | continue; | |
347 | gdb_assert (entry.index_offset == 0); | |
348 | ||
349 | /* Finding before inserting is faster than always trying to | |
350 | insert, because inserting always allocates a node, does the | |
351 | lookup, and then destroys the new node if another node | |
352 | already had the same key. C++17 try_emplace will avoid | |
353 | this. */ | |
354 | const auto found | |
355 | = symbol_hash_table.find (entry.cu_indices); | |
356 | if (found != symbol_hash_table.end ()) | |
357 | { | |
358 | entry.index_offset = found->second; | |
359 | continue; | |
360 | } | |
361 | ||
362 | symbol_hash_table.emplace (entry.cu_indices, cpool.size ()); | |
363 | entry.index_offset = cpool.size (); | |
364 | cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ())); | |
365 | for (const auto index : entry.cu_indices) | |
366 | cpool.append_data (MAYBE_SWAP (index)); | |
367 | } | |
368 | } | |
369 | ||
370 | /* Now write out the hash table. */ | |
371 | std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table; | |
372 | for (const auto &entry : symtab->data) | |
373 | { | |
374 | offset_type str_off, vec_off; | |
375 | ||
376 | if (entry.name != NULL) | |
377 | { | |
378 | const auto insertpair = str_table.emplace (entry.name, cpool.size ()); | |
379 | if (insertpair.second) | |
380 | cpool.append_cstr0 (entry.name); | |
381 | str_off = insertpair.first->second; | |
382 | vec_off = entry.index_offset; | |
383 | } | |
384 | else | |
385 | { | |
386 | /* While 0 is a valid constant pool index, it is not valid | |
387 | to have 0 for both offsets. */ | |
388 | str_off = 0; | |
389 | vec_off = 0; | |
390 | } | |
391 | ||
392 | output.append_data (MAYBE_SWAP (str_off)); | |
393 | output.append_data (MAYBE_SWAP (vec_off)); | |
394 | } | |
395 | } | |
396 | ||
397 | typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map; | |
398 | ||
399 | /* Helper struct for building the address table. */ | |
400 | struct addrmap_index_data | |
401 | { | |
402 | addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_) | |
403 | : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_) | |
404 | {} | |
405 | ||
406 | struct objfile *objfile; | |
407 | data_buf &addr_vec; | |
408 | psym_index_map &cu_index_htab; | |
409 | ||
410 | /* Non-zero if the previous_* fields are valid. | |
411 | We can't write an entry until we see the next entry (since it is only then | |
412 | that we know the end of the entry). */ | |
413 | int previous_valid; | |
414 | /* Index of the CU in the table of all CUs in the index file. */ | |
415 | unsigned int previous_cu_index; | |
416 | /* Start address of the CU. */ | |
417 | CORE_ADDR previous_cu_start; | |
418 | }; | |
419 | ||
420 | /* Write an address entry to ADDR_VEC. */ | |
421 | ||
422 | static void | |
423 | add_address_entry (struct objfile *objfile, data_buf &addr_vec, | |
424 | CORE_ADDR start, CORE_ADDR end, unsigned int cu_index) | |
425 | { | |
79748972 TT |
426 | addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start); |
427 | addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end); | |
cd4fb1b2 SM |
428 | addr_vec.append_data (MAYBE_SWAP (cu_index)); |
429 | } | |
430 | ||
431 | /* Worker function for traversing an addrmap to build the address table. */ | |
432 | ||
433 | static int | |
434 | add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj) | |
435 | { | |
436 | struct addrmap_index_data *data = (struct addrmap_index_data *) datap; | |
437 | struct partial_symtab *pst = (struct partial_symtab *) obj; | |
438 | ||
439 | if (data->previous_valid) | |
440 | add_address_entry (data->objfile, data->addr_vec, | |
441 | data->previous_cu_start, start_addr, | |
442 | data->previous_cu_index); | |
443 | ||
444 | data->previous_cu_start = start_addr; | |
445 | if (pst != NULL) | |
446 | { | |
447 | const auto it = data->cu_index_htab.find (pst); | |
448 | gdb_assert (it != data->cu_index_htab.cend ()); | |
449 | data->previous_cu_index = it->second; | |
450 | data->previous_valid = 1; | |
451 | } | |
452 | else | |
453 | data->previous_valid = 0; | |
454 | ||
455 | return 0; | |
456 | } | |
457 | ||
458 | /* Write OBJFILE's address map to ADDR_VEC. | |
459 | CU_INDEX_HTAB is used to map addrmap entries to their CU indices | |
460 | in the index file. */ | |
461 | ||
462 | static void | |
463 | write_address_map (struct objfile *objfile, data_buf &addr_vec, | |
464 | psym_index_map &cu_index_htab) | |
465 | { | |
466 | struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab); | |
467 | ||
468 | /* When writing the address table, we have to cope with the fact that | |
469 | the addrmap iterator only provides the start of a region; we have to | |
470 | wait until the next invocation to get the start of the next region. */ | |
471 | ||
472 | addrmap_index_data.objfile = objfile; | |
473 | addrmap_index_data.previous_valid = 0; | |
474 | ||
475 | addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker, | |
476 | &addrmap_index_data); | |
477 | ||
478 | /* It's highly unlikely the last entry (end address = 0xff...ff) | |
479 | is valid, but we should still handle it. | |
480 | The end address is recorded as the start of the next region, but that | |
481 | doesn't work here. To cope we pass 0xff...ff, this is a rare situation | |
482 | anyway. */ | |
483 | if (addrmap_index_data.previous_valid) | |
484 | add_address_entry (objfile, addr_vec, | |
485 | addrmap_index_data.previous_cu_start, (CORE_ADDR) -1, | |
486 | addrmap_index_data.previous_cu_index); | |
487 | } | |
488 | ||
489 | /* Return the symbol kind of PSYM. */ | |
490 | ||
491 | static gdb_index_symbol_kind | |
492 | symbol_kind (struct partial_symbol *psym) | |
493 | { | |
8a6d4234 TT |
494 | domain_enum domain = psym->domain; |
495 | enum address_class aclass = psym->aclass; | |
cd4fb1b2 SM |
496 | |
497 | switch (domain) | |
498 | { | |
499 | case VAR_DOMAIN: | |
500 | switch (aclass) | |
501 | { | |
502 | case LOC_BLOCK: | |
503 | return GDB_INDEX_SYMBOL_KIND_FUNCTION; | |
504 | case LOC_TYPEDEF: | |
505 | return GDB_INDEX_SYMBOL_KIND_TYPE; | |
506 | case LOC_COMPUTED: | |
507 | case LOC_CONST_BYTES: | |
508 | case LOC_OPTIMIZED_OUT: | |
509 | case LOC_STATIC: | |
510 | return GDB_INDEX_SYMBOL_KIND_VARIABLE; | |
511 | case LOC_CONST: | |
512 | /* Note: It's currently impossible to recognize psyms as enum values | |
513 | short of reading the type info. For now punt. */ | |
514 | return GDB_INDEX_SYMBOL_KIND_VARIABLE; | |
515 | default: | |
516 | /* There are other LOC_FOO values that one might want to classify | |
517 | as variables, but dwarf2read.c doesn't currently use them. */ | |
518 | return GDB_INDEX_SYMBOL_KIND_OTHER; | |
519 | } | |
520 | case STRUCT_DOMAIN: | |
521 | return GDB_INDEX_SYMBOL_KIND_TYPE; | |
522 | default: | |
523 | return GDB_INDEX_SYMBOL_KIND_OTHER; | |
524 | } | |
525 | } | |
526 | ||
527 | /* Add a list of partial symbols to SYMTAB. */ | |
528 | ||
529 | static void | |
530 | write_psymbols (struct mapped_symtab *symtab, | |
531 | std::unordered_set<partial_symbol *> &psyms_seen, | |
532 | struct partial_symbol **psymp, | |
533 | int count, | |
534 | offset_type cu_index, | |
535 | int is_static) | |
536 | { | |
537 | for (; count-- > 0; ++psymp) | |
538 | { | |
539 | struct partial_symbol *psym = *psymp; | |
540 | ||
8a6d4234 | 541 | if (psym->language == language_ada) |
cd4fb1b2 SM |
542 | error (_("Ada is not currently supported by the index")); |
543 | ||
544 | /* Only add a given psymbol once. */ | |
545 | if (psyms_seen.insert (psym).second) | |
546 | { | |
547 | gdb_index_symbol_kind kind = symbol_kind (psym); | |
548 | ||
8a6d4234 | 549 | add_index_entry (symtab, symbol_search_name (psym), |
cd4fb1b2 SM |
550 | is_static, kind, cu_index); |
551 | } | |
552 | } | |
553 | } | |
554 | ||
555 | /* A helper struct used when iterating over debug_types. */ | |
556 | struct signatured_type_index_data | |
557 | { | |
558 | signatured_type_index_data (data_buf &types_list_, | |
559 | std::unordered_set<partial_symbol *> &psyms_seen_) | |
560 | : types_list (types_list_), psyms_seen (psyms_seen_) | |
561 | {} | |
562 | ||
563 | struct objfile *objfile; | |
564 | struct mapped_symtab *symtab; | |
565 | data_buf &types_list; | |
566 | std::unordered_set<partial_symbol *> &psyms_seen; | |
567 | int cu_index; | |
568 | }; | |
569 | ||
570 | /* A helper function that writes a single signatured_type to an | |
571 | obstack. */ | |
572 | ||
573 | static int | |
574 | write_one_signatured_type (void **slot, void *d) | |
575 | { | |
576 | struct signatured_type_index_data *info | |
577 | = (struct signatured_type_index_data *) d; | |
578 | struct signatured_type *entry = (struct signatured_type *) *slot; | |
579 | struct partial_symtab *psymtab = entry->per_cu.v.psymtab; | |
580 | ||
581 | write_psymbols (info->symtab, | |
582 | info->psyms_seen, | |
b4be9bfd JK |
583 | (info->objfile->global_psymbols.data () |
584 | + psymtab->globals_offset), | |
cd4fb1b2 SM |
585 | psymtab->n_global_syms, info->cu_index, |
586 | 0); | |
587 | write_psymbols (info->symtab, | |
588 | info->psyms_seen, | |
b4be9bfd JK |
589 | (info->objfile->static_psymbols.data () |
590 | + psymtab->statics_offset), | |
cd4fb1b2 SM |
591 | psymtab->n_static_syms, info->cu_index, |
592 | 1); | |
593 | ||
594 | info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, | |
595 | to_underlying (entry->per_cu.sect_off)); | |
596 | info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, | |
597 | to_underlying (entry->type_offset_in_tu)); | |
598 | info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature); | |
599 | ||
600 | ++info->cu_index; | |
601 | ||
602 | return 1; | |
603 | } | |
604 | ||
605 | /* Recurse into all "included" dependencies and count their symbols as | |
606 | if they appeared in this psymtab. */ | |
607 | ||
608 | static void | |
609 | recursively_count_psymbols (struct partial_symtab *psymtab, | |
610 | size_t &psyms_seen) | |
611 | { | |
612 | for (int i = 0; i < psymtab->number_of_dependencies; ++i) | |
613 | if (psymtab->dependencies[i]->user != NULL) | |
614 | recursively_count_psymbols (psymtab->dependencies[i], | |
615 | psyms_seen); | |
616 | ||
617 | psyms_seen += psymtab->n_global_syms; | |
618 | psyms_seen += psymtab->n_static_syms; | |
619 | } | |
620 | ||
621 | /* Recurse into all "included" dependencies and write their symbols as | |
622 | if they appeared in this psymtab. */ | |
623 | ||
624 | static void | |
625 | recursively_write_psymbols (struct objfile *objfile, | |
626 | struct partial_symtab *psymtab, | |
627 | struct mapped_symtab *symtab, | |
628 | std::unordered_set<partial_symbol *> &psyms_seen, | |
629 | offset_type cu_index) | |
630 | { | |
631 | int i; | |
632 | ||
633 | for (i = 0; i < psymtab->number_of_dependencies; ++i) | |
634 | if (psymtab->dependencies[i]->user != NULL) | |
635 | recursively_write_psymbols (objfile, psymtab->dependencies[i], | |
636 | symtab, psyms_seen, cu_index); | |
637 | ||
638 | write_psymbols (symtab, | |
639 | psyms_seen, | |
b4be9bfd | 640 | objfile->global_psymbols.data () + psymtab->globals_offset, |
cd4fb1b2 SM |
641 | psymtab->n_global_syms, cu_index, |
642 | 0); | |
643 | write_psymbols (symtab, | |
644 | psyms_seen, | |
b4be9bfd | 645 | objfile->static_psymbols.data () + psymtab->statics_offset, |
cd4fb1b2 SM |
646 | psymtab->n_static_syms, cu_index, |
647 | 1); | |
648 | } | |
649 | ||
650 | /* DWARF-5 .debug_names builder. */ | |
651 | class debug_names | |
652 | { | |
653 | public: | |
654 | debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64, | |
655 | bfd_endian dwarf5_byte_order) | |
656 | : m_dwarf5_byte_order (dwarf5_byte_order), | |
657 | m_dwarf32 (dwarf5_byte_order), | |
658 | m_dwarf64 (dwarf5_byte_order), | |
659 | m_dwarf (is_dwarf64 | |
660 | ? static_cast<dwarf &> (m_dwarf64) | |
661 | : static_cast<dwarf &> (m_dwarf32)), | |
662 | m_name_table_string_offs (m_dwarf.name_table_string_offs), | |
663 | m_name_table_entry_offs (m_dwarf.name_table_entry_offs), | |
664 | m_debugstrlookup (dwarf2_per_objfile) | |
665 | {} | |
666 | ||
667 | int dwarf5_offset_size () const | |
668 | { | |
669 | const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64; | |
670 | return dwarf5_is_dwarf64 ? 8 : 4; | |
671 | } | |
672 | ||
673 | /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */ | |
674 | enum class unit_kind { cu, tu }; | |
675 | ||
676 | /* Insert one symbol. */ | |
677 | void insert (const partial_symbol *psym, int cu_index, bool is_static, | |
678 | unit_kind kind) | |
679 | { | |
680 | const int dwarf_tag = psymbol_tag (psym); | |
681 | if (dwarf_tag == 0) | |
682 | return; | |
8a6d4234 | 683 | const char *const name = symbol_search_name (psym); |
cd4fb1b2 SM |
684 | const auto insertpair |
685 | = m_name_to_value_set.emplace (c_str_view (name), | |
686 | std::set<symbol_value> ()); | |
687 | std::set<symbol_value> &value_set = insertpair.first->second; | |
688 | value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind)); | |
689 | } | |
690 | ||
691 | /* Build all the tables. All symbols must be already inserted. | |
692 | This function does not call file_write, caller has to do it | |
693 | afterwards. */ | |
694 | void build () | |
695 | { | |
696 | /* Verify the build method has not be called twice. */ | |
697 | gdb_assert (m_abbrev_table.empty ()); | |
698 | const size_t name_count = m_name_to_value_set.size (); | |
699 | m_bucket_table.resize | |
700 | (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3)))); | |
701 | m_hash_table.reserve (name_count); | |
702 | m_name_table_string_offs.reserve (name_count); | |
703 | m_name_table_entry_offs.reserve (name_count); | |
704 | ||
705 | /* Map each hash of symbol to its name and value. */ | |
706 | struct hash_it_pair | |
707 | { | |
708 | uint32_t hash; | |
709 | decltype (m_name_to_value_set)::const_iterator it; | |
710 | }; | |
711 | std::vector<std::forward_list<hash_it_pair>> bucket_hash; | |
712 | bucket_hash.resize (m_bucket_table.size ()); | |
713 | for (decltype (m_name_to_value_set)::const_iterator it | |
714 | = m_name_to_value_set.cbegin (); | |
715 | it != m_name_to_value_set.cend (); | |
716 | ++it) | |
717 | { | |
718 | const char *const name = it->first.c_str (); | |
719 | const uint32_t hash = dwarf5_djb_hash (name); | |
720 | hash_it_pair hashitpair; | |
721 | hashitpair.hash = hash; | |
722 | hashitpair.it = it; | |
723 | auto &slot = bucket_hash[hash % bucket_hash.size()]; | |
724 | slot.push_front (std::move (hashitpair)); | |
725 | } | |
726 | for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix) | |
727 | { | |
728 | const std::forward_list<hash_it_pair> &hashitlist | |
729 | = bucket_hash[bucket_ix]; | |
730 | if (hashitlist.empty ()) | |
731 | continue; | |
732 | uint32_t &bucket_slot = m_bucket_table[bucket_ix]; | |
733 | /* The hashes array is indexed starting at 1. */ | |
734 | store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot), | |
735 | sizeof (bucket_slot), m_dwarf5_byte_order, | |
736 | m_hash_table.size () + 1); | |
737 | for (const hash_it_pair &hashitpair : hashitlist) | |
738 | { | |
739 | m_hash_table.push_back (0); | |
740 | store_unsigned_integer (reinterpret_cast<gdb_byte *> | |
741 | (&m_hash_table.back ()), | |
742 | sizeof (m_hash_table.back ()), | |
743 | m_dwarf5_byte_order, hashitpair.hash); | |
744 | const c_str_view &name = hashitpair.it->first; | |
745 | const std::set<symbol_value> &value_set = hashitpair.it->second; | |
746 | m_name_table_string_offs.push_back_reorder | |
747 | (m_debugstrlookup.lookup (name.c_str ())); | |
748 | m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ()); | |
749 | gdb_assert (!value_set.empty ()); | |
750 | for (const symbol_value &value : value_set) | |
751 | { | |
752 | int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag, | |
753 | value.is_static, | |
754 | value.kind)]; | |
755 | if (idx == 0) | |
756 | { | |
757 | idx = m_idx_next++; | |
758 | m_abbrev_table.append_unsigned_leb128 (idx); | |
759 | m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag); | |
760 | m_abbrev_table.append_unsigned_leb128 | |
761 | (value.kind == unit_kind::cu ? DW_IDX_compile_unit | |
762 | : DW_IDX_type_unit); | |
763 | m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata); | |
764 | m_abbrev_table.append_unsigned_leb128 (value.is_static | |
765 | ? DW_IDX_GNU_internal | |
766 | : DW_IDX_GNU_external); | |
767 | m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present); | |
768 | ||
769 | /* Terminate attributes list. */ | |
770 | m_abbrev_table.append_unsigned_leb128 (0); | |
771 | m_abbrev_table.append_unsigned_leb128 (0); | |
772 | } | |
773 | ||
774 | m_entry_pool.append_unsigned_leb128 (idx); | |
775 | m_entry_pool.append_unsigned_leb128 (value.cu_index); | |
776 | } | |
777 | ||
778 | /* Terminate the list of CUs. */ | |
779 | m_entry_pool.append_unsigned_leb128 (0); | |
780 | } | |
781 | } | |
782 | gdb_assert (m_hash_table.size () == name_count); | |
783 | ||
784 | /* Terminate tags list. */ | |
785 | m_abbrev_table.append_unsigned_leb128 (0); | |
786 | } | |
787 | ||
788 | /* Return .debug_names bucket count. This must be called only after | |
789 | calling the build method. */ | |
790 | uint32_t bucket_count () const | |
791 | { | |
792 | /* Verify the build method has been already called. */ | |
793 | gdb_assert (!m_abbrev_table.empty ()); | |
794 | const uint32_t retval = m_bucket_table.size (); | |
795 | ||
796 | /* Check for overflow. */ | |
797 | gdb_assert (retval == m_bucket_table.size ()); | |
798 | return retval; | |
799 | } | |
800 | ||
801 | /* Return .debug_names names count. This must be called only after | |
802 | calling the build method. */ | |
803 | uint32_t name_count () const | |
804 | { | |
805 | /* Verify the build method has been already called. */ | |
806 | gdb_assert (!m_abbrev_table.empty ()); | |
807 | const uint32_t retval = m_hash_table.size (); | |
808 | ||
809 | /* Check for overflow. */ | |
810 | gdb_assert (retval == m_hash_table.size ()); | |
811 | return retval; | |
812 | } | |
813 | ||
814 | /* Return number of bytes of .debug_names abbreviation table. This | |
815 | must be called only after calling the build method. */ | |
816 | uint32_t abbrev_table_bytes () const | |
817 | { | |
818 | gdb_assert (!m_abbrev_table.empty ()); | |
819 | return m_abbrev_table.size (); | |
820 | } | |
821 | ||
822 | /* Recurse into all "included" dependencies and store their symbols | |
823 | as if they appeared in this psymtab. */ | |
824 | void recursively_write_psymbols | |
825 | (struct objfile *objfile, | |
826 | struct partial_symtab *psymtab, | |
827 | std::unordered_set<partial_symbol *> &psyms_seen, | |
828 | int cu_index) | |
829 | { | |
830 | for (int i = 0; i < psymtab->number_of_dependencies; ++i) | |
831 | if (psymtab->dependencies[i]->user != NULL) | |
832 | recursively_write_psymbols (objfile, psymtab->dependencies[i], | |
833 | psyms_seen, cu_index); | |
834 | ||
835 | write_psymbols (psyms_seen, | |
b4be9bfd | 836 | objfile->global_psymbols.data () + psymtab->globals_offset, |
cd4fb1b2 SM |
837 | psymtab->n_global_syms, cu_index, false, unit_kind::cu); |
838 | write_psymbols (psyms_seen, | |
b4be9bfd | 839 | objfile->static_psymbols.data () + psymtab->statics_offset, |
cd4fb1b2 SM |
840 | psymtab->n_static_syms, cu_index, true, unit_kind::cu); |
841 | } | |
842 | ||
843 | /* Return number of bytes the .debug_names section will have. This | |
844 | must be called only after calling the build method. */ | |
845 | size_t bytes () const | |
846 | { | |
847 | /* Verify the build method has been already called. */ | |
848 | gdb_assert (!m_abbrev_table.empty ()); | |
849 | size_t expected_bytes = 0; | |
850 | expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]); | |
851 | expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]); | |
852 | expected_bytes += m_name_table_string_offs.bytes (); | |
853 | expected_bytes += m_name_table_entry_offs.bytes (); | |
854 | expected_bytes += m_abbrev_table.size (); | |
855 | expected_bytes += m_entry_pool.size (); | |
856 | return expected_bytes; | |
857 | } | |
858 | ||
859 | /* Write .debug_names to FILE_NAMES and .debug_str addition to | |
860 | FILE_STR. This must be called only after calling the build | |
861 | method. */ | |
862 | void file_write (FILE *file_names, FILE *file_str) const | |
863 | { | |
864 | /* Verify the build method has been already called. */ | |
865 | gdb_assert (!m_abbrev_table.empty ()); | |
866 | ::file_write (file_names, m_bucket_table); | |
867 | ::file_write (file_names, m_hash_table); | |
868 | m_name_table_string_offs.file_write (file_names); | |
869 | m_name_table_entry_offs.file_write (file_names); | |
870 | m_abbrev_table.file_write (file_names); | |
871 | m_entry_pool.file_write (file_names); | |
872 | m_debugstrlookup.file_write (file_str); | |
873 | } | |
874 | ||
875 | /* A helper user data for write_one_signatured_type. */ | |
876 | class write_one_signatured_type_data | |
877 | { | |
878 | public: | |
879 | write_one_signatured_type_data (debug_names &nametable_, | |
880 | signatured_type_index_data &&info_) | |
881 | : nametable (nametable_), info (std::move (info_)) | |
882 | {} | |
883 | debug_names &nametable; | |
884 | struct signatured_type_index_data info; | |
885 | }; | |
886 | ||
887 | /* A helper function to pass write_one_signatured_type to | |
888 | htab_traverse_noresize. */ | |
889 | static int | |
890 | write_one_signatured_type (void **slot, void *d) | |
891 | { | |
892 | write_one_signatured_type_data *data = (write_one_signatured_type_data *) d; | |
893 | struct signatured_type_index_data *info = &data->info; | |
894 | struct signatured_type *entry = (struct signatured_type *) *slot; | |
895 | ||
896 | data->nametable.write_one_signatured_type (entry, info); | |
897 | ||
898 | return 1; | |
899 | } | |
900 | ||
901 | private: | |
902 | ||
903 | /* Storage for symbol names mapping them to their .debug_str section | |
904 | offsets. */ | |
905 | class debug_str_lookup | |
906 | { | |
907 | public: | |
908 | ||
909 | /* Object costructor to be called for current DWARF2_PER_OBJFILE. | |
910 | All .debug_str section strings are automatically stored. */ | |
911 | debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile) | |
912 | : m_abfd (dwarf2_per_objfile->objfile->obfd), | |
913 | m_dwarf2_per_objfile (dwarf2_per_objfile) | |
914 | { | |
915 | dwarf2_read_section (dwarf2_per_objfile->objfile, | |
916 | &dwarf2_per_objfile->str); | |
917 | if (dwarf2_per_objfile->str.buffer == NULL) | |
918 | return; | |
919 | for (const gdb_byte *data = dwarf2_per_objfile->str.buffer; | |
920 | data < (dwarf2_per_objfile->str.buffer | |
921 | + dwarf2_per_objfile->str.size);) | |
922 | { | |
923 | const char *const s = reinterpret_cast<const char *> (data); | |
924 | const auto insertpair | |
925 | = m_str_table.emplace (c_str_view (s), | |
926 | data - dwarf2_per_objfile->str.buffer); | |
927 | if (!insertpair.second) | |
b98664d3 | 928 | complaint (_("Duplicate string \"%s\" in " |
cd4fb1b2 SM |
929 | ".debug_str section [in module %s]"), |
930 | s, bfd_get_filename (m_abfd)); | |
931 | data += strlen (s) + 1; | |
932 | } | |
933 | } | |
934 | ||
935 | /* Return offset of symbol name S in the .debug_str section. Add | |
936 | such symbol to the section's end if it does not exist there | |
937 | yet. */ | |
938 | size_t lookup (const char *s) | |
939 | { | |
940 | const auto it = m_str_table.find (c_str_view (s)); | |
941 | if (it != m_str_table.end ()) | |
942 | return it->second; | |
943 | const size_t offset = (m_dwarf2_per_objfile->str.size | |
944 | + m_str_add_buf.size ()); | |
945 | m_str_table.emplace (c_str_view (s), offset); | |
946 | m_str_add_buf.append_cstr0 (s); | |
947 | return offset; | |
948 | } | |
949 | ||
950 | /* Append the end of the .debug_str section to FILE. */ | |
951 | void file_write (FILE *file) const | |
952 | { | |
953 | m_str_add_buf.file_write (file); | |
954 | } | |
955 | ||
956 | private: | |
957 | std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table; | |
958 | bfd *const m_abfd; | |
959 | struct dwarf2_per_objfile *m_dwarf2_per_objfile; | |
960 | ||
961 | /* Data to add at the end of .debug_str for new needed symbol names. */ | |
962 | data_buf m_str_add_buf; | |
963 | }; | |
964 | ||
965 | /* Container to map used DWARF tags to their .debug_names abbreviation | |
966 | tags. */ | |
967 | class index_key | |
968 | { | |
969 | public: | |
970 | index_key (int dwarf_tag_, bool is_static_, unit_kind kind_) | |
971 | : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_) | |
972 | { | |
973 | } | |
974 | ||
975 | bool | |
976 | operator== (const index_key &other) const | |
977 | { | |
978 | return (dwarf_tag == other.dwarf_tag && is_static == other.is_static | |
979 | && kind == other.kind); | |
980 | } | |
981 | ||
982 | const int dwarf_tag; | |
983 | const bool is_static; | |
984 | const unit_kind kind; | |
985 | }; | |
986 | ||
987 | /* Provide std::unordered_map::hasher for index_key. */ | |
988 | class index_key_hasher | |
989 | { | |
990 | public: | |
991 | size_t | |
992 | operator () (const index_key &key) const | |
993 | { | |
994 | return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static; | |
995 | } | |
996 | }; | |
997 | ||
998 | /* Parameters of one symbol entry. */ | |
999 | class symbol_value | |
1000 | { | |
1001 | public: | |
1002 | const int dwarf_tag, cu_index; | |
1003 | const bool is_static; | |
1004 | const unit_kind kind; | |
1005 | ||
1006 | symbol_value (int dwarf_tag_, int cu_index_, bool is_static_, | |
1007 | unit_kind kind_) | |
1008 | : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_), | |
1009 | kind (kind_) | |
1010 | {} | |
1011 | ||
1012 | bool | |
1013 | operator< (const symbol_value &other) const | |
1014 | { | |
1015 | #define X(n) \ | |
1016 | do \ | |
1017 | { \ | |
1018 | if (n < other.n) \ | |
1019 | return true; \ | |
1020 | if (n > other.n) \ | |
1021 | return false; \ | |
1022 | } \ | |
1023 | while (0) | |
1024 | X (dwarf_tag); | |
1025 | X (is_static); | |
1026 | X (kind); | |
1027 | X (cu_index); | |
1028 | #undef X | |
1029 | return false; | |
1030 | } | |
1031 | }; | |
1032 | ||
1033 | /* Abstract base class to unify DWARF-32 and DWARF-64 name table | |
1034 | output. */ | |
1035 | class offset_vec | |
1036 | { | |
1037 | protected: | |
1038 | const bfd_endian dwarf5_byte_order; | |
1039 | public: | |
1040 | explicit offset_vec (bfd_endian dwarf5_byte_order_) | |
1041 | : dwarf5_byte_order (dwarf5_byte_order_) | |
1042 | {} | |
1043 | ||
1044 | /* Call std::vector::reserve for NELEM elements. */ | |
1045 | virtual void reserve (size_t nelem) = 0; | |
1046 | ||
1047 | /* Call std::vector::push_back with store_unsigned_integer byte | |
1048 | reordering for ELEM. */ | |
1049 | virtual void push_back_reorder (size_t elem) = 0; | |
1050 | ||
1051 | /* Return expected output size in bytes. */ | |
1052 | virtual size_t bytes () const = 0; | |
1053 | ||
1054 | /* Write name table to FILE. */ | |
1055 | virtual void file_write (FILE *file) const = 0; | |
1056 | }; | |
1057 | ||
1058 | /* Template to unify DWARF-32 and DWARF-64 output. */ | |
1059 | template<typename OffsetSize> | |
1060 | class offset_vec_tmpl : public offset_vec | |
1061 | { | |
1062 | public: | |
1063 | explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_) | |
1064 | : offset_vec (dwarf5_byte_order_) | |
1065 | {} | |
1066 | ||
1067 | /* Implement offset_vec::reserve. */ | |
1068 | void reserve (size_t nelem) override | |
1069 | { | |
1070 | m_vec.reserve (nelem); | |
1071 | } | |
1072 | ||
1073 | /* Implement offset_vec::push_back_reorder. */ | |
1074 | void push_back_reorder (size_t elem) override | |
1075 | { | |
1076 | m_vec.push_back (elem); | |
1077 | /* Check for overflow. */ | |
1078 | gdb_assert (m_vec.back () == elem); | |
1079 | store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()), | |
1080 | sizeof (m_vec.back ()), dwarf5_byte_order, elem); | |
1081 | } | |
1082 | ||
1083 | /* Implement offset_vec::bytes. */ | |
1084 | size_t bytes () const override | |
1085 | { | |
1086 | return m_vec.size () * sizeof (m_vec[0]); | |
1087 | } | |
1088 | ||
1089 | /* Implement offset_vec::file_write. */ | |
1090 | void file_write (FILE *file) const override | |
1091 | { | |
1092 | ::file_write (file, m_vec); | |
1093 | } | |
1094 | ||
1095 | private: | |
1096 | std::vector<OffsetSize> m_vec; | |
1097 | }; | |
1098 | ||
1099 | /* Base class to unify DWARF-32 and DWARF-64 .debug_names output | |
1100 | respecting name table width. */ | |
1101 | class dwarf | |
1102 | { | |
1103 | public: | |
1104 | offset_vec &name_table_string_offs, &name_table_entry_offs; | |
1105 | ||
1106 | dwarf (offset_vec &name_table_string_offs_, | |
1107 | offset_vec &name_table_entry_offs_) | |
1108 | : name_table_string_offs (name_table_string_offs_), | |
1109 | name_table_entry_offs (name_table_entry_offs_) | |
1110 | { | |
1111 | } | |
1112 | }; | |
1113 | ||
1114 | /* Template to unify DWARF-32 and DWARF-64 .debug_names output | |
1115 | respecting name table width. */ | |
1116 | template<typename OffsetSize> | |
1117 | class dwarf_tmpl : public dwarf | |
1118 | { | |
1119 | public: | |
1120 | explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_) | |
1121 | : dwarf (m_name_table_string_offs, m_name_table_entry_offs), | |
1122 | m_name_table_string_offs (dwarf5_byte_order_), | |
1123 | m_name_table_entry_offs (dwarf5_byte_order_) | |
1124 | {} | |
1125 | ||
1126 | private: | |
1127 | offset_vec_tmpl<OffsetSize> m_name_table_string_offs; | |
1128 | offset_vec_tmpl<OffsetSize> m_name_table_entry_offs; | |
1129 | }; | |
1130 | ||
1131 | /* Try to reconstruct original DWARF tag for given partial_symbol. | |
1132 | This function is not DWARF-5 compliant but it is sufficient for | |
1133 | GDB as a DWARF-5 index consumer. */ | |
1134 | static int psymbol_tag (const struct partial_symbol *psym) | |
1135 | { | |
8a6d4234 TT |
1136 | domain_enum domain = psym->domain; |
1137 | enum address_class aclass = psym->aclass; | |
cd4fb1b2 SM |
1138 | |
1139 | switch (domain) | |
1140 | { | |
1141 | case VAR_DOMAIN: | |
1142 | switch (aclass) | |
1143 | { | |
1144 | case LOC_BLOCK: | |
1145 | return DW_TAG_subprogram; | |
1146 | case LOC_TYPEDEF: | |
1147 | return DW_TAG_typedef; | |
1148 | case LOC_COMPUTED: | |
1149 | case LOC_CONST_BYTES: | |
1150 | case LOC_OPTIMIZED_OUT: | |
1151 | case LOC_STATIC: | |
1152 | return DW_TAG_variable; | |
1153 | case LOC_CONST: | |
1154 | /* Note: It's currently impossible to recognize psyms as enum values | |
1155 | short of reading the type info. For now punt. */ | |
1156 | return DW_TAG_variable; | |
1157 | default: | |
1158 | /* There are other LOC_FOO values that one might want to classify | |
1159 | as variables, but dwarf2read.c doesn't currently use them. */ | |
1160 | return DW_TAG_variable; | |
1161 | } | |
1162 | case STRUCT_DOMAIN: | |
1163 | return DW_TAG_structure_type; | |
1164 | default: | |
1165 | return 0; | |
1166 | } | |
1167 | } | |
1168 | ||
1169 | /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */ | |
1170 | void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen, | |
1171 | struct partial_symbol **psymp, int count, int cu_index, | |
1172 | bool is_static, unit_kind kind) | |
1173 | { | |
1174 | for (; count-- > 0; ++psymp) | |
1175 | { | |
1176 | struct partial_symbol *psym = *psymp; | |
1177 | ||
8a6d4234 | 1178 | if (psym->language == language_ada) |
cd4fb1b2 SM |
1179 | error (_("Ada is not currently supported by the index")); |
1180 | ||
1181 | /* Only add a given psymbol once. */ | |
1182 | if (psyms_seen.insert (psym).second) | |
1183 | insert (psym, cu_index, is_static, kind); | |
1184 | } | |
1185 | } | |
1186 | ||
1187 | /* A helper function that writes a single signatured_type | |
1188 | to a debug_names. */ | |
1189 | void | |
1190 | write_one_signatured_type (struct signatured_type *entry, | |
1191 | struct signatured_type_index_data *info) | |
1192 | { | |
1193 | struct partial_symtab *psymtab = entry->per_cu.v.psymtab; | |
1194 | ||
1195 | write_psymbols (info->psyms_seen, | |
b4be9bfd JK |
1196 | (info->objfile->global_psymbols.data () |
1197 | + psymtab->globals_offset), | |
cd4fb1b2 SM |
1198 | psymtab->n_global_syms, info->cu_index, false, |
1199 | unit_kind::tu); | |
1200 | write_psymbols (info->psyms_seen, | |
b4be9bfd JK |
1201 | (info->objfile->static_psymbols.data () |
1202 | + psymtab->statics_offset), | |
cd4fb1b2 SM |
1203 | psymtab->n_static_syms, info->cu_index, true, |
1204 | unit_kind::tu); | |
1205 | ||
1206 | info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order, | |
1207 | to_underlying (entry->per_cu.sect_off)); | |
1208 | ||
1209 | ++info->cu_index; | |
1210 | } | |
1211 | ||
1212 | /* Store value of each symbol. */ | |
1213 | std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher> | |
1214 | m_name_to_value_set; | |
1215 | ||
1216 | /* Tables of DWARF-5 .debug_names. They are in object file byte | |
1217 | order. */ | |
1218 | std::vector<uint32_t> m_bucket_table; | |
1219 | std::vector<uint32_t> m_hash_table; | |
1220 | ||
1221 | const bfd_endian m_dwarf5_byte_order; | |
1222 | dwarf_tmpl<uint32_t> m_dwarf32; | |
1223 | dwarf_tmpl<uint64_t> m_dwarf64; | |
1224 | dwarf &m_dwarf; | |
1225 | offset_vec &m_name_table_string_offs, &m_name_table_entry_offs; | |
1226 | debug_str_lookup m_debugstrlookup; | |
1227 | ||
1228 | /* Map each used .debug_names abbreviation tag parameter to its | |
1229 | index value. */ | |
1230 | std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx; | |
1231 | ||
1232 | /* Next unused .debug_names abbreviation tag for | |
1233 | m_indexkey_to_idx. */ | |
1234 | int m_idx_next = 1; | |
1235 | ||
1236 | /* .debug_names abbreviation table. */ | |
1237 | data_buf m_abbrev_table; | |
1238 | ||
1239 | /* .debug_names entry pool. */ | |
1240 | data_buf m_entry_pool; | |
1241 | }; | |
1242 | ||
1243 | /* Return iff any of the needed offsets does not fit into 32-bit | |
1244 | .debug_names section. */ | |
1245 | ||
1246 | static bool | |
1247 | check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile) | |
1248 | { | |
b76e467d | 1249 | for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units) |
cd4fb1b2 | 1250 | { |
b76e467d | 1251 | if (to_underlying (per_cu->sect_off) >= (static_cast<uint64_t> (1) << 32)) |
cd4fb1b2 SM |
1252 | return true; |
1253 | } | |
b2bdb8cf | 1254 | for (const signatured_type *sigtype : dwarf2_per_objfile->all_type_units) |
cd4fb1b2 | 1255 | { |
b2bdb8cf | 1256 | const dwarf2_per_cu_data &per_cu = sigtype->per_cu; |
cd4fb1b2 SM |
1257 | |
1258 | if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32)) | |
1259 | return true; | |
1260 | } | |
1261 | return false; | |
1262 | } | |
1263 | ||
1264 | /* The psyms_seen set is potentially going to be largish (~40k | |
1265 | elements when indexing a -g3 build of GDB itself). Estimate the | |
1266 | number of elements in order to avoid too many rehashes, which | |
1267 | require rebuilding buckets and thus many trips to | |
1268 | malloc/free. */ | |
1269 | ||
1270 | static size_t | |
1271 | psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile) | |
1272 | { | |
1273 | size_t psyms_count = 0; | |
b76e467d | 1274 | for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units) |
cd4fb1b2 | 1275 | { |
cd4fb1b2 SM |
1276 | struct partial_symtab *psymtab = per_cu->v.psymtab; |
1277 | ||
1278 | if (psymtab != NULL && psymtab->user == NULL) | |
1279 | recursively_count_psymbols (psymtab, psyms_count); | |
1280 | } | |
1281 | /* Generating an index for gdb itself shows a ratio of | |
1282 | TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */ | |
1283 | return psyms_count / 4; | |
1284 | } | |
1285 | ||
1286 | /* Write new .gdb_index section for OBJFILE into OUT_FILE. | |
1287 | Return how many bytes were expected to be written into OUT_FILE. */ | |
1288 | ||
1289 | static size_t | |
1290 | write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file) | |
1291 | { | |
1292 | struct objfile *objfile = dwarf2_per_objfile->objfile; | |
1293 | mapped_symtab symtab; | |
1294 | data_buf cu_list; | |
1295 | ||
1296 | /* While we're scanning CU's create a table that maps a psymtab pointer | |
1297 | (which is what addrmap records) to its index (which is what is recorded | |
1298 | in the index file). This will later be needed to write the address | |
1299 | table. */ | |
1300 | psym_index_map cu_index_htab; | |
b76e467d | 1301 | cu_index_htab.reserve (dwarf2_per_objfile->all_comp_units.size ()); |
cd4fb1b2 SM |
1302 | |
1303 | /* The CU list is already sorted, so we don't need to do additional | |
1304 | work here. Also, the debug_types entries do not appear in | |
1305 | all_comp_units, but only in their own hash table. */ | |
1306 | ||
1307 | std::unordered_set<partial_symbol *> psyms_seen | |
1308 | (psyms_seen_size (dwarf2_per_objfile)); | |
b76e467d | 1309 | for (int i = 0; i < dwarf2_per_objfile->all_comp_units.size (); ++i) |
cd4fb1b2 SM |
1310 | { |
1311 | struct dwarf2_per_cu_data *per_cu | |
1312 | = dwarf2_per_objfile->all_comp_units[i]; | |
1313 | struct partial_symtab *psymtab = per_cu->v.psymtab; | |
1314 | ||
1315 | /* CU of a shared file from 'dwz -m' may be unused by this main file. | |
1316 | It may be referenced from a local scope but in such case it does not | |
1317 | need to be present in .gdb_index. */ | |
1318 | if (psymtab == NULL) | |
1319 | continue; | |
1320 | ||
1321 | if (psymtab->user == NULL) | |
1322 | recursively_write_psymbols (objfile, psymtab, &symtab, | |
1323 | psyms_seen, i); | |
1324 | ||
1325 | const auto insertpair = cu_index_htab.emplace (psymtab, i); | |
1326 | gdb_assert (insertpair.second); | |
1327 | ||
1328 | cu_list.append_uint (8, BFD_ENDIAN_LITTLE, | |
1329 | to_underlying (per_cu->sect_off)); | |
1330 | cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length); | |
1331 | } | |
1332 | ||
1333 | /* Dump the address map. */ | |
1334 | data_buf addr_vec; | |
1335 | write_address_map (objfile, addr_vec, cu_index_htab); | |
1336 | ||
1337 | /* Write out the .debug_type entries, if any. */ | |
1338 | data_buf types_cu_list; | |
1339 | if (dwarf2_per_objfile->signatured_types) | |
1340 | { | |
1341 | signatured_type_index_data sig_data (types_cu_list, | |
1342 | psyms_seen); | |
1343 | ||
1344 | sig_data.objfile = objfile; | |
1345 | sig_data.symtab = &symtab; | |
b76e467d | 1346 | sig_data.cu_index = dwarf2_per_objfile->all_comp_units.size (); |
cd4fb1b2 SM |
1347 | htab_traverse_noresize (dwarf2_per_objfile->signatured_types, |
1348 | write_one_signatured_type, &sig_data); | |
1349 | } | |
1350 | ||
1351 | /* Now that we've processed all symbols we can shrink their cu_indices | |
1352 | lists. */ | |
1353 | uniquify_cu_indices (&symtab); | |
1354 | ||
1355 | data_buf symtab_vec, constant_pool; | |
1356 | write_hash_table (&symtab, symtab_vec, constant_pool); | |
1357 | ||
1358 | data_buf contents; | |
1359 | const offset_type size_of_contents = 6 * sizeof (offset_type); | |
1360 | offset_type total_len = size_of_contents; | |
1361 | ||
1362 | /* The version number. */ | |
1363 | contents.append_data (MAYBE_SWAP (8)); | |
1364 | ||
1365 | /* The offset of the CU list from the start of the file. */ | |
1366 | contents.append_data (MAYBE_SWAP (total_len)); | |
1367 | total_len += cu_list.size (); | |
1368 | ||
1369 | /* The offset of the types CU list from the start of the file. */ | |
1370 | contents.append_data (MAYBE_SWAP (total_len)); | |
1371 | total_len += types_cu_list.size (); | |
1372 | ||
1373 | /* The offset of the address table from the start of the file. */ | |
1374 | contents.append_data (MAYBE_SWAP (total_len)); | |
1375 | total_len += addr_vec.size (); | |
1376 | ||
1377 | /* The offset of the symbol table from the start of the file. */ | |
1378 | contents.append_data (MAYBE_SWAP (total_len)); | |
1379 | total_len += symtab_vec.size (); | |
1380 | ||
1381 | /* The offset of the constant pool from the start of the file. */ | |
1382 | contents.append_data (MAYBE_SWAP (total_len)); | |
1383 | total_len += constant_pool.size (); | |
1384 | ||
1385 | gdb_assert (contents.size () == size_of_contents); | |
1386 | ||
1387 | contents.file_write (out_file); | |
1388 | cu_list.file_write (out_file); | |
1389 | types_cu_list.file_write (out_file); | |
1390 | addr_vec.file_write (out_file); | |
1391 | symtab_vec.file_write (out_file); | |
1392 | constant_pool.file_write (out_file); | |
1393 | ||
1394 | return total_len; | |
1395 | } | |
1396 | ||
1397 | /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */ | |
1398 | static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 }; | |
1399 | ||
1400 | /* Write a new .debug_names section for OBJFILE into OUT_FILE, write | |
1401 | needed addition to .debug_str section to OUT_FILE_STR. Return how | |
1402 | many bytes were expected to be written into OUT_FILE. */ | |
1403 | ||
1404 | static size_t | |
1405 | write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, | |
1406 | FILE *out_file, FILE *out_file_str) | |
1407 | { | |
1408 | const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile); | |
1409 | struct objfile *objfile = dwarf2_per_objfile->objfile; | |
1410 | const enum bfd_endian dwarf5_byte_order | |
1411 | = gdbarch_byte_order (get_objfile_arch (objfile)); | |
1412 | ||
1413 | /* The CU list is already sorted, so we don't need to do additional | |
1414 | work here. Also, the debug_types entries do not appear in | |
1415 | all_comp_units, but only in their own hash table. */ | |
1416 | data_buf cu_list; | |
1417 | debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64, | |
1418 | dwarf5_byte_order); | |
1419 | std::unordered_set<partial_symbol *> | |
1420 | psyms_seen (psyms_seen_size (dwarf2_per_objfile)); | |
b76e467d | 1421 | for (int i = 0; i < dwarf2_per_objfile->all_comp_units.size (); ++i) |
cd4fb1b2 SM |
1422 | { |
1423 | const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i]; | |
1424 | partial_symtab *psymtab = per_cu->v.psymtab; | |
1425 | ||
1426 | /* CU of a shared file from 'dwz -m' may be unused by this main | |
1427 | file. It may be referenced from a local scope but in such | |
1428 | case it does not need to be present in .debug_names. */ | |
1429 | if (psymtab == NULL) | |
1430 | continue; | |
1431 | ||
1432 | if (psymtab->user == NULL) | |
1433 | nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i); | |
1434 | ||
1435 | cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order, | |
1436 | to_underlying (per_cu->sect_off)); | |
1437 | } | |
1438 | ||
1439 | /* Write out the .debug_type entries, if any. */ | |
1440 | data_buf types_cu_list; | |
1441 | if (dwarf2_per_objfile->signatured_types) | |
1442 | { | |
1443 | debug_names::write_one_signatured_type_data sig_data (nametable, | |
1444 | signatured_type_index_data (types_cu_list, psyms_seen)); | |
1445 | ||
1446 | sig_data.info.objfile = objfile; | |
1447 | /* It is used only for gdb_index. */ | |
1448 | sig_data.info.symtab = nullptr; | |
1449 | sig_data.info.cu_index = 0; | |
1450 | htab_traverse_noresize (dwarf2_per_objfile->signatured_types, | |
1451 | debug_names::write_one_signatured_type, | |
1452 | &sig_data); | |
1453 | } | |
1454 | ||
1455 | nametable.build (); | |
1456 | ||
1457 | /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */ | |
1458 | ||
1459 | const offset_type bytes_of_header | |
1460 | = ((dwarf5_is_dwarf64 ? 12 : 4) | |
1461 | + 2 + 2 + 7 * 4 | |
1462 | + sizeof (dwarf5_gdb_augmentation)); | |
1463 | size_t expected_bytes = 0; | |
1464 | expected_bytes += bytes_of_header; | |
1465 | expected_bytes += cu_list.size (); | |
1466 | expected_bytes += types_cu_list.size (); | |
1467 | expected_bytes += nametable.bytes (); | |
1468 | data_buf header; | |
1469 | ||
1470 | if (!dwarf5_is_dwarf64) | |
1471 | { | |
1472 | const uint64_t size64 = expected_bytes - 4; | |
1473 | gdb_assert (size64 < 0xfffffff0); | |
1474 | header.append_uint (4, dwarf5_byte_order, size64); | |
1475 | } | |
1476 | else | |
1477 | { | |
1478 | header.append_uint (4, dwarf5_byte_order, 0xffffffff); | |
1479 | header.append_uint (8, dwarf5_byte_order, expected_bytes - 12); | |
1480 | } | |
1481 | ||
1482 | /* The version number. */ | |
1483 | header.append_uint (2, dwarf5_byte_order, 5); | |
1484 | ||
1485 | /* Padding. */ | |
1486 | header.append_uint (2, dwarf5_byte_order, 0); | |
1487 | ||
1488 | /* comp_unit_count - The number of CUs in the CU list. */ | |
b76e467d SM |
1489 | header.append_uint (4, dwarf5_byte_order, |
1490 | dwarf2_per_objfile->all_comp_units.size ()); | |
cd4fb1b2 SM |
1491 | |
1492 | /* local_type_unit_count - The number of TUs in the local TU | |
1493 | list. */ | |
b2bdb8cf SM |
1494 | header.append_uint (4, dwarf5_byte_order, |
1495 | dwarf2_per_objfile->all_type_units.size ()); | |
cd4fb1b2 SM |
1496 | |
1497 | /* foreign_type_unit_count - The number of TUs in the foreign TU | |
1498 | list. */ | |
1499 | header.append_uint (4, dwarf5_byte_order, 0); | |
1500 | ||
1501 | /* bucket_count - The number of hash buckets in the hash lookup | |
1502 | table. */ | |
1503 | header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ()); | |
1504 | ||
1505 | /* name_count - The number of unique names in the index. */ | |
1506 | header.append_uint (4, dwarf5_byte_order, nametable.name_count ()); | |
1507 | ||
1508 | /* abbrev_table_size - The size in bytes of the abbreviations | |
1509 | table. */ | |
1510 | header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ()); | |
1511 | ||
1512 | /* augmentation_string_size - The size in bytes of the augmentation | |
1513 | string. This value is rounded up to a multiple of 4. */ | |
1514 | static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, ""); | |
1515 | header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation)); | |
1516 | header.append_data (dwarf5_gdb_augmentation); | |
1517 | ||
1518 | gdb_assert (header.size () == bytes_of_header); | |
1519 | ||
1520 | header.file_write (out_file); | |
1521 | cu_list.file_write (out_file); | |
1522 | types_cu_list.file_write (out_file); | |
1523 | nametable.file_write (out_file, out_file_str); | |
1524 | ||
1525 | return expected_bytes; | |
1526 | } | |
1527 | ||
1528 | /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek | |
1529 | position is at the end of the file. */ | |
1530 | ||
1531 | static void | |
1532 | assert_file_size (FILE *file, const char *filename, size_t expected_size) | |
1533 | { | |
1534 | const auto file_size = ftell (file); | |
1535 | if (file_size == -1) | |
1536 | error (_("Can't get `%s' size"), filename); | |
1537 | gdb_assert (file_size == expected_size); | |
1538 | } | |
1539 | ||
87d6a7aa | 1540 | /* See dwarf-index-write.h. */ |
cd4fb1b2 | 1541 | |
87d6a7aa | 1542 | void |
cd4fb1b2 | 1543 | write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile, |
87d6a7aa | 1544 | const char *dir, const char *basename, |
cd4fb1b2 SM |
1545 | dw_index_kind index_kind) |
1546 | { | |
1547 | struct objfile *objfile = dwarf2_per_objfile->objfile; | |
1548 | ||
1549 | if (dwarf2_per_objfile->using_index) | |
1550 | error (_("Cannot use an index to create the index")); | |
1551 | ||
1552 | if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1) | |
1553 | error (_("Cannot make an index when the file has multiple .debug_types sections")); | |
1554 | ||
1555 | if (!objfile->psymtabs || !objfile->psymtabs_addrmap) | |
1556 | return; | |
1557 | ||
1558 | struct stat st; | |
1559 | if (stat (objfile_name (objfile), &st) < 0) | |
1560 | perror_with_name (objfile_name (objfile)); | |
1561 | ||
87d6a7aa SM |
1562 | /* Make a filename suitable to pass to mkstemp based on F (e.g. |
1563 | /tmp/foo -> /tmp/foo-XXXXXX). */ | |
1564 | auto make_temp_filename = [] (const std::string &f) -> gdb::char_vector | |
1565 | { | |
1566 | gdb::char_vector filename_temp (f.length () + 8); | |
1567 | strcpy (filename_temp.data (), f.c_str ()); | |
1568 | strcat (filename_temp.data () + f.size (), "-XXXXXX"); | |
1569 | return filename_temp; | |
1570 | }; | |
1571 | ||
1572 | std::string filename (std::string (dir) + SLASH_STRING + basename | |
cd4fb1b2 SM |
1573 | + (index_kind == dw_index_kind::DEBUG_NAMES |
1574 | ? INDEX5_SUFFIX : INDEX4_SUFFIX)); | |
87d6a7aa | 1575 | gdb::char_vector filename_temp = make_temp_filename (filename); |
cd4fb1b2 | 1576 | |
87d6a7aa SM |
1577 | gdb::optional<scoped_fd> out_file_fd |
1578 | (gdb::in_place, mkstemp (filename_temp.data ())); | |
1579 | if (out_file_fd->get () == -1) | |
83c8d318 | 1580 | perror_with_name (("mkstemp")); |
cd4fb1b2 | 1581 | |
87d6a7aa SM |
1582 | FILE *out_file = gdb_fopen_cloexec (filename_temp.data (), "wb").release (); |
1583 | if (out_file == nullptr) | |
1584 | error (_("Can't open `%s' for writing"), filename_temp.data ()); | |
1585 | ||
1586 | /* Order matters here; we want FILE to be closed before FILENAME_TEMP is | |
cd4fb1b2 SM |
1587 | unlinked, because on MS-Windows one cannot delete a file that is |
1588 | still open. (Don't call anything here that might throw until | |
87d6a7aa SM |
1589 | file_closer is created.) We don't need OUT_FILE_FD anymore, so we might |
1590 | as well close it now. */ | |
1591 | out_file_fd.reset (); | |
1592 | gdb::unlinker unlink_file (filename_temp.data ()); | |
cd4fb1b2 SM |
1593 | gdb_file_up close_out_file (out_file); |
1594 | ||
1595 | if (index_kind == dw_index_kind::DEBUG_NAMES) | |
1596 | { | |
1597 | std::string filename_str (std::string (dir) + SLASH_STRING | |
87d6a7aa SM |
1598 | + basename + DEBUG_STR_SUFFIX); |
1599 | gdb::char_vector filename_str_temp = make_temp_filename (filename_str); | |
1600 | ||
1601 | gdb::optional<scoped_fd> out_file_str_fd | |
1602 | (gdb::in_place, mkstemp (filename_str_temp.data ())); | |
1603 | if (out_file_str_fd->get () == -1) | |
83c8d318 | 1604 | perror_with_name (("mkstemp")); |
87d6a7aa | 1605 | |
cd4fb1b2 | 1606 | FILE *out_file_str |
87d6a7aa SM |
1607 | = gdb_fopen_cloexec (filename_str_temp.data (), "wb").release (); |
1608 | if (out_file_str == nullptr) | |
1609 | error (_("Can't open `%s' for writing"), filename_str_temp.data ()); | |
1610 | ||
1611 | out_file_str_fd.reset (); | |
1612 | gdb::unlinker unlink_file_str (filename_str_temp.data ()); | |
cd4fb1b2 SM |
1613 | gdb_file_up close_out_file_str (out_file_str); |
1614 | ||
1615 | const size_t total_len | |
1616 | = write_debug_names (dwarf2_per_objfile, out_file, out_file_str); | |
87d6a7aa | 1617 | assert_file_size (out_file, filename_temp.data (), total_len); |
cd4fb1b2 SM |
1618 | |
1619 | /* We want to keep the file .debug_str file too. */ | |
1620 | unlink_file_str.keep (); | |
87d6a7aa SM |
1621 | |
1622 | /* Close and move the str file in place. */ | |
1623 | close_out_file_str.reset (); | |
1624 | if (rename (filename_str_temp.data (), filename_str.c_str ()) != 0) | |
83c8d318 | 1625 | perror_with_name (("rename")); |
cd4fb1b2 SM |
1626 | } |
1627 | else | |
1628 | { | |
1629 | const size_t total_len | |
1630 | = write_gdbindex (dwarf2_per_objfile, out_file); | |
87d6a7aa | 1631 | assert_file_size (out_file, filename_temp.data (), total_len); |
cd4fb1b2 SM |
1632 | } |
1633 | ||
1634 | /* We want to keep the file. */ | |
1635 | unlink_file.keep (); | |
87d6a7aa SM |
1636 | |
1637 | /* Close and move the file in place. */ | |
1638 | close_out_file.reset (); | |
1639 | if (rename (filename_temp.data (), filename.c_str ()) != 0) | |
83c8d318 | 1640 | perror_with_name (("rename")); |
cd4fb1b2 SM |
1641 | } |
1642 | ||
1643 | /* Implementation of the `save gdb-index' command. | |
1644 | ||
1645 | Note that the .gdb_index file format used by this command is | |
1646 | documented in the GDB manual. Any changes here must be documented | |
1647 | there. */ | |
1648 | ||
1649 | static void | |
1650 | save_gdb_index_command (const char *arg, int from_tty) | |
1651 | { | |
1652 | struct objfile *objfile; | |
1653 | const char dwarf5space[] = "-dwarf-5 "; | |
1654 | dw_index_kind index_kind = dw_index_kind::GDB_INDEX; | |
1655 | ||
1656 | if (!arg) | |
1657 | arg = ""; | |
1658 | ||
1659 | arg = skip_spaces (arg); | |
1660 | if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0) | |
1661 | { | |
1662 | index_kind = dw_index_kind::DEBUG_NAMES; | |
1663 | arg += strlen (dwarf5space); | |
1664 | arg = skip_spaces (arg); | |
1665 | } | |
1666 | ||
1667 | if (!*arg) | |
1668 | error (_("usage: save gdb-index [-dwarf-5] DIRECTORY")); | |
1669 | ||
1670 | ALL_OBJFILES (objfile) | |
1671 | { | |
1672 | struct stat st; | |
1673 | ||
1674 | /* If the objfile does not correspond to an actual file, skip it. */ | |
1675 | if (stat (objfile_name (objfile), &st) < 0) | |
1676 | continue; | |
1677 | ||
1678 | struct dwarf2_per_objfile *dwarf2_per_objfile | |
1679 | = get_dwarf2_per_objfile (objfile); | |
1680 | ||
1681 | if (dwarf2_per_objfile != NULL) | |
1682 | { | |
1683 | TRY | |
1684 | { | |
87d6a7aa SM |
1685 | const char *basename = lbasename (objfile_name (objfile)); |
1686 | write_psymtabs_to_index (dwarf2_per_objfile, arg, basename, | |
1687 | index_kind); | |
cd4fb1b2 SM |
1688 | } |
1689 | CATCH (except, RETURN_MASK_ERROR) | |
1690 | { | |
1691 | exception_fprintf (gdb_stderr, except, | |
1692 | _("Error while writing index for `%s': "), | |
1693 | objfile_name (objfile)); | |
1694 | } | |
1695 | END_CATCH | |
1696 | } | |
1697 | ||
1698 | } | |
1699 | } | |
1700 | ||
1701 | void | |
1702 | _initialize_dwarf_index_write () | |
1703 | { | |
1704 | cmd_list_element *c = add_cmd ("gdb-index", class_files, | |
1705 | save_gdb_index_command, _("\ | |
1706 | Save a gdb-index file.\n\ | |
1707 | Usage: save gdb-index [-dwarf-5] DIRECTORY\n\ | |
1708 | \n\ | |
1709 | No options create one file with .gdb-index extension for pre-DWARF-5\n\ | |
1710 | compatible .gdb_index section. With -dwarf-5 creates two files with\n\ | |
1711 | extension .debug_names and .debug_str for DWARF-5 .debug_names section."), | |
1712 | &save_cmdlist); | |
1713 | set_cmd_completer (c, filename_completer); | |
1714 | } |