1 /* Copyright (C) 2001-2021 Red Hat, Inc.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 Copyright (C) 2019-2021 SUSE LLC.
4 Written by Jakub Jelinek <jakub@redhat.com>, 2012.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA. */
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <getopt.h>
25 #include <setjmp.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdbool.h>
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <inttypes.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <sys/mman.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <sys/times.h>
38
39 #include "obstack.h"
40
41 #include <gelf.h>
42 #include "dwarf2.h"
43 #include "hashtab.h"
44 #include "sha1.h"
45
46 #ifndef SHF_COMPRESSED
47 /* Glibc elf.h contains SHF_COMPRESSED starting v2.22. Libelf libelf.h has
48 a fallback definition starting v0.166. Define a fallback definition here
49 for the case of both pre-v2.22 glibc and pre-v0.166 libelf. */
50 # define SHF_COMPRESSED (1 << 11) /* Section with compressed data. */
51 #endif
52
53 /* Theory of operation:
54 The DWZ tool can either optimize debug sections of a single
55 executable or shared library at a time, or, when -m option
56 is used, optimize debug sections even in between different
57 executables or shared libraries by constructing a new ET_REL
58 ELF object containing debug sections to which the individual
59 debug sections in the various executables or shared libraries
60 can refer. As debug info can be pretty large, the multifile
61 optimization is done in several phases in order to decrease
62 memory usage of the tool, which can still be quite high.
63
64 The dwz () function optimizes a single file, and at the end,
65 after processing that single file, it frees all allocated memory.
66 Without -m, the dwz () function is called once on each argument.
67
68 When -m has been passed, the dwz () function is first called on
69 each argument, and during it in addition to performing the same
70 optimizations as dwz tool does without -m it also may append
71 data to several temporary files (one for each .debug_* section
72 that is needed for the multifile optimization). During
73 preparation of the additions to the temporary files (write_multifile
74 function), wr_multifile flag is true.
75
76 Next phase (optimize_multifile) is that all these temporary files
77 are mmapped, it is determined what DIEs, strings, .debug_macro
78 sequences etc. might be beneficial to have in the common debug
79 sections and finally a new ET_REL ELF object is written. During
80 this phase the op_multifile flag is true. This is often very
81 memory consuming phase, so extra hacks are used to decrease
82 the memory usage during it.
83
84 Next phase (read_multifile) is where the previously written ET_REL
85 ELF object is parsed again and needed hash tables and other data
86 structures filled in. The rd_multifile flag is set during this
87 phase. The reason why this phase is separate from the previous one,
88 as opposed to just populating the hash tables right away in
89 optimize_multifile, is that the memory consumption during that phase
90 can be very big and keeping malloced data around would mean the address
91 space would be unnecessarily fragmented. read_multifile usually needs
92 to allocate only small fragment of the memory optimize_multifile
93 needs, on the other side that memory needs to be kept around until
94 the end of the last phase.
95
96 During the last phase, the dwz () function is called second
97 time on each argument, with fi_multifile flag set. During this
98 phase duplicates in the common debug sections are referenced
99 from the local debug sections where possible.
100
101 If some executable or shared library has too large debug information
102 (number of DIEs in .debug_info section) that there would be
103 risk of too high memory consumption, that file isn't multifile
104 optimized, instead it is processed by dwz () in a low-memory mode
105 with low_mem flag set. This can decrease memory consumption to
106 half in some very large cases. */
107
108 #ifndef NT_GNU_BUILD_ID
109 # define NT_GNU_BUILD_ID 3
110 #endif
111
112 #if defined __GNUC__ && __GNUC__ >= 3
113 # define likely(x) __builtin_expect (!!(x), 1)
114 # define unlikely(x) __builtin_expect (!!(x), 0)
115 #else
116 # define likely(x) (x)
117 # define unlikely(x) (x)
118 #endif
119
120 #if defined __GNUC__
121 # define FORCE_INLINE __attribute__((always_inline))
122 # define UNUSED __attribute__((unused))
123 # define USED __attribute__((used))
124 #else
125 # define FORCE_INLINE
126 # define UNUSED
127 # define USED
128 #endif
129
130 /* Utility macro. */
131 #define IMPLIES(A, B) (!((A) && !(B)))
132 #define MAX(A, B) ((A) > (B) ? (A) : (B))
133 #define MIN(A, B) ((A) < (B) ? (A) : (B))
134
135
136 static void
report_progress(void)137 report_progress (void)
138 {
139 static struct tms current;
140 static struct tms prev;
141 static bool first = true;
142 static long ticks_per_second = 0;
143
144 if (!first)
145 prev = current;
146
147 times (¤t);
148
149 if (first)
150 {
151 ticks_per_second = sysconf (_SC_CLK_TCK);
152 first = false;
153 return;
154 }
155
156 clock_t user = current.tms_utime - prev.tms_utime;
157 clock_t sys = current.tms_stime - prev.tms_stime;
158 fprintf (stderr, "user: %.2f\n", (float)user / (float)ticks_per_second);
159 fprintf (stderr, "sys : %.2f\n", (float)sys / (float)ticks_per_second);
160 }
161
162 #define obstack_chunk_alloc malloc
163 #define obstack_chunk_free free
164
165 /* Where to longjmp on OOM. */
166 static jmp_buf oom_buf;
167
168 /* error () wrapper based on the Linux manual page at
169 http://man7.org/linux/man-pages/man3/error.3.html. */
170 static void
error(int status,int errnum,const char * fmt,...)171 error (int status, int errnum, const char *fmt, ...)
172 {
173 va_list ap;
174
175 fprintf (stderr, "%s: ", getprogname());
176 if (fmt != NULL) {
177 va_start (ap, fmt);
178 vfprintf (stderr, fmt, ap);
179 va_end (ap);
180 }
181
182 if (errnum != 0)
183 fprintf (stderr, ": %s", strerror(errnum));
184
185 fputc ('\n', stderr);
186
187 if (status != 0)
188 exit (status);
189 }
190
191 /* Handle OOM situation. If handling more than one file, we might
192 just fail to handle some large file due to OOM, but could very well
193 handle other smaller files after it. */
194 static void
dwz_oom(void)195 dwz_oom (void)
196 {
197 longjmp (oom_buf, 1);
198 }
199
200 /* General obstack for struct dw_cu, dw_die, also used for temporary
201 vectors. */
202 static struct obstack ob;
203 /* Short lived obstack, global only to free it on allocation failures. */
204 static struct obstack ob2;
205
206 /* After read_multifile ob and ob2 are moved over to these variables
207 and restored during final cleanup. */
208 static struct obstack alt_ob, alt_ob2;
209
210 #if DEVEL
211 static int tracing;
212 static int ignore_size;
213 static int ignore_locus;
214 static int dump_checksum_p;
215 static int dump_dies_p;
216 static int dump_dups_p;
217 static int dump_pus_p;
218 static int verify_dups_p;
219 static int verify_edge_freelist;
220 static int stats_p;
221 #else
222 #define tracing 0
223 #define ignore_size 0
224 #define ignore_locus 0
225 #define dump_checksum_p 0
226 #define dump_dies_p 0
227 #define dump_dups_p 0
228 #define dump_pus_p 0
229 #define verify_dups_p 0
230 #define stats_p 0
231 #endif
232 static int unoptimized_multifile;
233 static int save_temps = 0;
234 static int verify_edges_p = 0;
235 static int dump_edges_p = 0;
236 static int partition_dups_opt;
237 static int progress_p;
238 static int import_opt_p = 1;
239 static int force_p = 0;
240 enum deduplication_mode
241 {
242 dm_none,
243 dm_intra_cu,
244 dm_inter_cu
245 };
246 static enum deduplication_mode deduplication_mode = dm_inter_cu;
247 static int uni_lang_p = 0;
248 static int gen_cu_p = 0;
249 enum die_count_methods
250 {
251 none,
252 estimate
253 };
254 static enum die_count_methods die_count_method = estimate;
255
256 int odr = 0;
257 enum odr_mode { ODR_BASIC, ODR_LINK };
258 enum odr_mode odr_mode = ODR_LINK;
259 int odr_mode_parsed = 0;
260 bool odr_active_p = false;
261
262 /* Struct to gather statistics. */
263 struct stats
264 {
265 const char *file;
266 unsigned int root_cnt;
267 unsigned int namespace_cnt;
268 unsigned int lower_toplevel;
269 unsigned int die_count;
270 unsigned int lower_toplevel_with_checksum;
271 unsigned int dup_cnt;
272 unsigned int dup_chain_cnt;
273 unsigned int dup_chain_max_length;
274 unsigned int part_cnt;
275 unsigned int pu_ph1_cnt;
276 unsigned int pu_ph2_cnt;
277 unsigned int pu_toplevel_die_cnt;
278 };
279 struct stats *stats;
280
281 /* Initialize stats struct. */
282 static void
init_stats(const char * file)283 init_stats (const char *file)
284 {
285 if (stats == NULL)
286 stats = (struct stats *)malloc (sizeof (*stats));
287 memset (stats, 0, sizeof (*stats));
288 stats->file = file;
289 }
290
291 /* Print stats struct, parsing statistics. */
292 static void
print_parse_stats(void)293 print_parse_stats (void)
294 {
295 if (stats == NULL || stats->file == NULL)
296 return;
297
298 fprintf (stderr, "Parse statistics for %s\n", stats->file);
299
300 fprintf (stderr, "root_count : %10u\n",
301 stats->root_cnt);
302 fprintf (stderr, "namespace_count : %10u\n",
303 stats->namespace_cnt);
304 unsigned int upper_toplevel = stats->root_cnt + stats->namespace_cnt;
305 fprintf (stderr, "upper_toplevel : %10u\n",
306 upper_toplevel);
307 unsigned lower_toplevel
308 = stats->lower_toplevel + stats->lower_toplevel_with_checksum;
309 fprintf (stderr, "lower_toplevel : %10u\n",
310 lower_toplevel);
311 unsigned int toplevel = upper_toplevel + lower_toplevel;
312 fprintf (stderr, "toplevel : %10u\n",
313 toplevel);
314 unsigned non_toplevel = stats->die_count - toplevel;
315 fprintf (stderr, "non_toplevel : %10u\n",
316 non_toplevel);
317 fprintf (stderr, "die_count : %10u\n",
318 stats->die_count);
319 }
320
321 /* Print stats struct, dups statistics. */
322 static void
print_dups_stats(void)323 print_dups_stats (void)
324 {
325 if (stats == NULL || stats->file == NULL)
326 return;
327
328 fprintf (stderr, "Duplicate statistics for %s\n", stats->file);
329
330 fprintf (stderr, "lower_toplevel with checksum : %10u\n",
331 stats->lower_toplevel_with_checksum);
332 fprintf (stderr, "dup_cnt : %10u\n",
333 stats->dup_cnt);
334 fprintf (stderr, "dup_chain_cnt : %10u\n",
335 stats->dup_chain_cnt);
336 fprintf (stderr, "average dup_chain length : %10.2f\n",
337 (double)stats->dup_cnt / (double)stats->dup_chain_cnt);
338 fprintf (stderr, "max dup_chain length : %10u\n",
339 stats->dup_chain_max_length);
340 }
341
342 static void
print_part_stats(void)343 print_part_stats (void)
344 {
345 if (stats == NULL || stats->file == NULL)
346 return;
347
348 fprintf (stderr, "Partition statistics for %s\n", stats->file);
349
350 fprintf (stderr, "part_cnt : %10u\n", stats->part_cnt);
351 fprintf (stderr, "pu_ph1_cnt : %10u\n",
352 stats->pu_ph1_cnt);
353 fprintf (stderr, "pu_ph2_cnt : %10u\n",
354 stats->pu_ph2_cnt);
355 fprintf (stderr, "pu_cnt : %10u\n",
356 stats->pu_ph1_cnt + stats->pu_ph2_cnt);
357 fprintf (stderr, "pu_toplevel_die_cnt : %10u\n",
358 stats->pu_toplevel_die_cnt);
359 }
360
361 typedef struct
362 {
363 Elf *elf;
364 GElf_Ehdr ehdr;
365 Elf_Scn **scn;
366 const char *filename;
367 int lastscn;
368 GElf_Shdr shdr[0];
369 } DSO;
370
371 /* Macro to parse an uleb128 number, return it and
372 update ptr to the end of the uleb128 at the same time. */
373 #define read_uleb128(ptr) ({ \
374 uint64_t ret = 0; \
375 uint64_t c; \
376 int shift = 0; \
377 do \
378 { \
379 c = *ptr++; \
380 ret |= (c & 0x7f) << shift; \
381 shift += 7; \
382 } while (c & 0x80); \
383 \
384 if (shift >= 70) \
385 ret = ~(uint64_t) 0; \
386 ret; \
387 })
388
389 /* Macro to parse a sleb128 number, return it and
390 update ptr to the end of the sleb128 at the same time. */
391 #define read_sleb128(ptr) ({ \
392 uint64_t ret = 0; \
393 uint64_t c; \
394 int shift = 0; \
395 do \
396 { \
397 c = *ptr++; \
398 ret |= (c & 0x7f) << shift; \
399 shift += 7; \
400 } while (c & 0x80); \
401 \
402 if (shift >= 70) \
403 ret = ~(uint64_t) 0; \
404 else if (c & 0x40) \
405 ret |= (-(uint64_t) 1) << shift; \
406 ret; \
407 })
408
409 /* Macro to store an uleb128 number to ptr and update
410 ptr to point after it. */
411 #define write_uleb128(ptr, val) \
412 do \
413 { \
414 uint64_t valv = (val); \
415 do \
416 { \
417 unsigned char c = valv & 0x7f;\
418 valv >>= 7; \
419 if (valv) \
420 c |= 0x80; \
421 *ptr++ = c; \
422 } \
423 while (valv); \
424 } \
425 while (0)
426
427 #define write_sleb128(ptr, val) \
428 do \
429 { \
430 int64_t valv = (val); \
431 bool more; \
432 do \
433 { \
434 unsigned char c = valv & 0x7f; \
435 valv >>= 7; \
436 more = ((valv != 0 || (c & 0x40) != 0) \
437 && (valv != -1 || (c & 0x40) == 0)); \
438 if (more) \
439 c |= 0x80; \
440 *ptr++ = c; \
441 } \
442 while (more); \
443 } \
444 while (0)
445
446 /* Macro to skip a uleb128 or sleb128 number and update ptr to the end of the
447 number. */
448 #define skip_leb128(ptr) \
449 do {} while ((*ptr++) & 0x80)
450
451 /* Macro to parse a uint16_t value represented using form, return it and
452 update ptr to the end of the value at the same time. If the value doesn't
453 fit, assign true to error_p. */
454 #define read_u16(ptr, form, error_p) \
455 ({ \
456 uint16_t ret = 0; \
457 switch (form) \
458 { \
459 case DW_FORM_data1: \
460 ret = read_8 (ptr); \
461 break; \
462 case DW_FORM_data2: \
463 ret = read_16 (ptr); \
464 break; \
465 case DW_FORM_data4: \
466 { \
467 uint32_t res = read_32 (ptr); \
468 ret = (uint16_t)res; \
469 if ((uint32_t)ret != res) \
470 error_p = true; \
471 break; \
472 } \
473 case DW_FORM_data8: \
474 { \
475 uint64_t res = read_64 (ptr); \
476 ret = (uint16_t)res; \
477 if ((uint64_t)ret != res) \
478 error_p = true; \
479 break; \
480 } \
481 case DW_FORM_udata: \
482 { \
483 uint64_t res = read_uleb128 (ptr); \
484 ret = (uint16_t)res; \
485 if ((uint64_t)ret != res) \
486 error_p = true; \
487 break; \
488 } \
489 case DW_FORM_sdata: \
490 { \
491 union { \
492 uint64_t u; \
493 int64_t i; \
494 } res; \
495 res.u = read_sleb128 (ptr); \
496 ret = (uint16_t)res.u; \
497 if (res.i < 0 || (uint64_t)ret != res.u) \
498 error_p = true; \
499 break; \
500 } \
501 default: \
502 error_p = true; \
503 break; \
504 } \
505 ret; \
506 })
507
508 /* Pointer size in the debug info, in bytes. Only debug info
509 with a single pointer size are handled. */
510 static int ptr_size;
511
512 /* Lowest debug_line version we have seen. When writing out the multi
513 file .debug_line we'll only use a DWARF5 version when there is no
514 lower line table seen (since the debug_line dir and file table is
515 shared between all CUs). */
516 static unsigned int lowest_line_version = 5;
517
518 /* Utility functions and macros for reading/writing values in
519 given ELF endianity, which might be different from host endianity.
520 No specific alignment is expected. */
521 static uint16_t (*do_read_16) (unsigned char *ptr);
522 static uint32_t (*do_read_32) (unsigned char *ptr);
523 static uint64_t (*do_read_64) (unsigned char *ptr);
524 static void (*do_write_16) (unsigned char *ptr, unsigned short val);
525 static void (*do_write_32) (unsigned char *ptr, unsigned int val);
526 static void (*do_write_64) (unsigned char *ptr, uint64_t val);
527
528 static inline uint16_t
buf_read_ule16(unsigned char * data)529 buf_read_ule16 (unsigned char *data)
530 {
531 return data[0] | (data[1] << 8);
532 }
533
534 static inline uint16_t
buf_read_ube16(unsigned char * data)535 buf_read_ube16 (unsigned char *data)
536 {
537 return data[1] | (data[0] << 8);
538 }
539
540 static inline uint32_t
buf_read_ule32(unsigned char * data)541 buf_read_ule32 (unsigned char *data)
542 {
543 return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
544 }
545
546 static inline uint32_t
buf_read_ube32(unsigned char * data)547 buf_read_ube32 (unsigned char *data)
548 {
549 return data[3] | (data[2] << 8) | (data[1] << 16) | (data[0] << 24);
550 }
551
552 static inline uint64_t
buf_read_ule64(unsigned char * data)553 buf_read_ule64 (unsigned char *data)
554 {
555 return buf_read_ule32 (data)
556 | (((uint64_t) buf_read_ule32 (data + 4)) << 32);
557 }
558
559 static inline uint64_t
buf_read_ube64(unsigned char * data)560 buf_read_ube64 (unsigned char *data)
561 {
562 return (((uint64_t) buf_read_ube32 (data)) << 32)
563 | buf_read_ube32 (data + 4);
564 }
565
566 #define read_8(ptr) *ptr++
567
568 #define read_16(ptr) ({ \
569 uint16_t ret = do_read_16 (ptr); \
570 ptr += 2; \
571 ret; \
572 })
573
574 #define read_32(ptr) ({ \
575 uint32_t ret = do_read_32 (ptr); \
576 ptr += 4; \
577 ret; \
578 })
579
580 #define read_64(ptr) ({ \
581 uint64_t ret = do_read_64 (ptr); \
582 ptr += 8; \
583 ret; \
584 })
585
586 #define write_8(ptr, val) \
587 do \
588 *ptr++ = (val); \
589 while (0)
590
591 #define write_16(ptr, val) \
592 do \
593 { \
594 do_write_16 (ptr, val); \
595 ptr += 2; \
596 } \
597 while (0)
598
599 #define write_32(ptr, val) \
600 do \
601 { \
602 do_write_32 (ptr, val); \
603 ptr += 4; \
604 } \
605 while (0)
606
607 #define write_64(ptr, val) \
608 do \
609 { \
610 do_write_64 (ptr, val); \
611 ptr += 8; \
612 } \
613 while (0)
614
615 static uint64_t
read_size(unsigned char * p,int size)616 read_size (unsigned char *p, int size)
617 {
618 switch (size)
619 {
620 case 1: return read_8 (p);
621 case 2: return read_16 (p);
622 case 4: return read_32 (p);
623 case 8: return read_64 (p);
624 default: abort ();
625 }
626 }
627
628 static void
write_size(unsigned char * p,int size,uint64_t val)629 write_size (unsigned char *p, int size, uint64_t val)
630 {
631 switch (size)
632 {
633 case 1: write_8 (p, val); break;
634 case 2: write_16 (p, val); break;
635 case 4: write_32 (p, val); break;
636 case 8: write_64 (p, val); break;
637 default: abort ();
638 }
639 }
640
641 static void
buf_write_le16(unsigned char * p,unsigned short v)642 buf_write_le16 (unsigned char *p, unsigned short v)
643 {
644 p[0] = v;
645 p[1] = v >> 8;
646 }
647
648 static void
buf_write_be16(unsigned char * p,unsigned short v)649 buf_write_be16 (unsigned char *p, unsigned short v)
650 {
651 p[1] = v;
652 p[0] = v >> 8;
653 }
654
655 static void
buf_write_le32(unsigned char * p,unsigned int v)656 buf_write_le32 (unsigned char *p, unsigned int v)
657 {
658 p[0] = v;
659 p[1] = v >> 8;
660 p[2] = v >> 16;
661 p[3] = v >> 24;
662 }
663
664 static void
buf_write_be32(unsigned char * p,unsigned int v)665 buf_write_be32 (unsigned char *p, unsigned int v)
666 {
667 p[3] = v;
668 p[2] = v >> 8;
669 p[1] = v >> 16;
670 p[0] = v >> 24;
671 }
672
673 static void
buf_write_le64(unsigned char * data,uint64_t v)674 buf_write_le64 (unsigned char *data, uint64_t v)
675 {
676 buf_write_le32 (data, v);
677 buf_write_le32 (data + 4, v >> 32);
678 }
679
680 static void
buf_write_be64(unsigned char * data,uint64_t v)681 buf_write_be64 (unsigned char *data, uint64_t v)
682 {
683 buf_write_be32 (data, v >> 32);
684 buf_write_be32 (data + 4, v);
685 }
686
687 /* Return a DW_FORM_* name. */
688 static const char *
get_DW_FORM_str(unsigned int form)689 get_DW_FORM_str (unsigned int form)
690 {
691 const char *name = get_DW_FORM_name (form);
692 static char buf[9 + 3 * sizeof (int)];
693 if (name)
694 return name;
695 sprintf (buf, "DW_FORM_%u", form);
696 return buf;
697 }
698
699 /* Return a DW_OP_* name. */
700 static const char *
get_DW_OP_str(unsigned int op)701 get_DW_OP_str (unsigned int op)
702 {
703 const char *name = get_DW_OP_name (op);
704 static char buf[7 + 3 * sizeof (int)];
705 if (name)
706 return name;
707 sprintf (buf, "DW_OP_%u", op);
708 return buf;
709 }
710
711 /* Return a DW_AT_* name. */
712 static const char *
get_DW_AT_str(unsigned int at)713 get_DW_AT_str (unsigned int at)
714 {
715 const char *name = get_DW_AT_name (at);
716 static char buf[7 + 3 * sizeof (int)];
717 if (name)
718 return name;
719 sprintf (buf, "DW_AT_%u", at);
720 return buf;
721 }
722
723 /* Return a DW_UT_* name. */
724 static const char *
get_DW_UT_str(unsigned int ut)725 get_DW_UT_str (unsigned int ut)
726 {
727 const char *name = get_DW_UT_name (ut);
728 static char buf[7 + 3 * sizeof (int)];
729 if (name)
730 return name;
731 sprintf (buf, "DW_UT_%u", ut);
732 return buf;
733 }
734
735 /* Retrun a DW_LNCT_* name. */
736 static const char *
get_DW_LNCT_str(unsigned int lnct)737 get_DW_LNCT_str (unsigned int lnct)
738 {
739 const char *name;
740 static char buf[9 + 3 * sizeof (int)];
741 switch (lnct)
742 {
743 case DW_LNCT_path: name = "DW_LNCT_path"; break;
744 case DW_LNCT_directory_index: name = "DW_LNCT_directory_index"; break;
745 case DW_LNCT_timestamp: name = "DW_LNCT_timestamp"; break;
746 case DW_LNCT_size: name = "DW_LNCT_size"; break;
747 case DW_LNCT_MD5: name = "DW_LNCT_MD5"; break;
748
749 default: name = 0; break;
750 }
751 if (name)
752 return name;
753 sprintf (buf, "DW_LNCT_%u", lnct);
754 return buf;
755 }
756
757 /* This must match the debug_sections array content
758 below. */
759 enum debug_section_kind
760 {
761 DEBUG_INFO,
762 DEBUG_ABBREV,
763 DEBUG_LINE,
764 DEBUG_STR,
765 DEBUG_MACRO,
766 DEBUG_TYPES,
767 DEBUG_ARANGES,
768 DEBUG_PUBNAMES,
769 DEBUG_PUBTYPES,
770 DEBUG_GNU_PUBNAMES,
771 DEBUG_GNU_PUBTYPES,
772 DEBUG_MACINFO,
773 DEBUG_LOC,
774 DEBUG_LOCLISTS,
775 DEBUG_FRAME,
776 DEBUG_RANGES,
777 DEBUG_RNGLISTS,
778 DEBUG_LINE_STR,
779 DEBUG_SUP,
780 DEBUG_GDB_SCRIPTS,
781 GDB_INDEX,
782 GNU_DEBUGALTLINK,
783 SECTION_COUNT,
784 SAVED_SECTIONS = DEBUG_TYPES + 1
785 };
786
787 /* Details about standard DWARF sections. */
788 static struct
789 {
790 const char *name;
791 unsigned char *data;
792 unsigned char *new_data;
793 size_t size;
794 size_t new_size;
795 int sec;
796 } debug_sections[] =
797 {
798 { ".debug_info", NULL, NULL, 0, 0, 0 },
799 { ".debug_abbrev", NULL, NULL, 0, 0, 0 },
800 { ".debug_line", NULL, NULL, 0, 0, 0 },
801 { ".debug_str", NULL, NULL, 0, 0, 0 },
802 { ".debug_macro", NULL, NULL, 0, 0, 0 },
803 { ".debug_types", NULL, NULL, 0, 0, 0 },
804 { ".debug_aranges", NULL, NULL, 0, 0, 0 },
805 { ".debug_pubnames", NULL, NULL, 0, 0, 0 },
806 { ".debug_pubtypes", NULL, NULL, 0, 0, 0 },
807 { ".debug_gnu_pubnames", NULL, NULL, 0, 0, 0 },
808 { ".debug_gnu_pubtypes", NULL, NULL, 0, 0, 0 },
809 { ".debug_macinfo", NULL, NULL, 0, 0, 0 },
810 { ".debug_loc", NULL, NULL, 0, 0, 0 },
811 { ".debug_loclists", NULL, NULL, 0, 0, 0 },
812 { ".debug_frame", NULL, NULL, 0, 0, 0 },
813 { ".debug_ranges", NULL, NULL, 0, 0, 0 },
814 { ".debug_rnglists", NULL, NULL, 0, 0, 0 },
815 { ".debug_line_str", NULL, NULL, 0, 0, 0 },
816 { ".debug_sup", NULL, NULL, 0, 0, 0 },
817 { ".debug_gdb_scripts", NULL, NULL, 0, 0, 0 },
818 { ".gdb_index", NULL, NULL, 0, 0, 0 },
819 { ".gnu_debugaltlink", NULL, NULL, 0, 0, 0 },
820 { NULL, NULL, NULL, 0, 0, 0 }
821 };
822
823 /* Copies of .new_data fields during write_multifile. */
824 static unsigned char *saved_new_data[SAVED_SECTIONS];
825 /* Copies of .new_size fields during write_multifile. */
826 static size_t saved_new_size[SAVED_SECTIONS];
827
828 /* Copies of .data fields after read_multifile. */
829 static unsigned char *alt_data[SAVED_SECTIONS];
830 /* Copies of .size fields after read_multifile. */
831 static size_t alt_size[SAVED_SECTIONS];
832
833 /* How many bytes of each of /tmp/dwz.debug_*.XXXXXX have we written
834 already. */
835 static unsigned int multi_info_off, multi_abbrev_off;
836 static unsigned int multi_line_off, multi_str_off;
837 static unsigned int multi_macro_off;
838 /* And corresponding file descriptors. */
839 static int multi_info_fd = -1, multi_abbrev_fd = -1;
840 static int multi_line_fd = -1, multi_str_fd = -1;
841 static int multi_macro_fd = -1;
842
843 /* Copy of one of the input file's ehdr. */
844 static GElf_Ehdr multi_ehdr;
845
846 /* Pointer size of all debug info sources accumulated during
847 write_multifile. */
848 static int multi_ptr_size;
849 /* And their endianity. */
850 static int multi_endian;
851 /* Highest .gdb_index version seen. */
852 static unsigned int multi_gdb_index_ver;
853
854 /* Number of DIEs, above which dwz retries processing
855 in low_mem mode (and give up on multifile optimizing
856 the file in question). */
857 static unsigned int low_mem_die_limit = 10000000;
858
859 /* Number of DIEs, above which dwz gives up processing
860 input altogether. */
861 static unsigned int max_die_limit = 50000000;
862
863 /* Phase of multifile handling. */
864 static unsigned char multifile_mode;
865
866 enum multifile_mode_kind
867 {
868 MULTIFILE_MODE_WR = 1,
869 MULTIFILE_MODE_OP = 2,
870 MULTIFILE_MODE_RD = 4,
871 MULTIFILE_MODE_FI = 8,
872 MULTIFILE_MODE_LOW_MEM = 16
873 };
874
875 /* True while in write_multifile. */
876 #define wr_multifile (multifile_mode & MULTIFILE_MODE_WR)
877
878 /* True while in optimize_multifile. */
879 #define op_multifile (multifile_mode & MULTIFILE_MODE_OP)
880
881 /* True while in read_multifile. */
882 #define rd_multifile (multifile_mode & MULTIFILE_MODE_RD)
883
884 /* True while in finalize_multifile. */
885 #define fi_multifile (multifile_mode & MULTIFILE_MODE_FI)
886
887 /* True if running in low_mem mode. */
888 #define low_mem (multifile_mode & MULTIFILE_MODE_LOW_MEM)
889
890 /* Filename if inter-file size optimization should be performed. */
891 static const char *multifile;
892
893 /* Argument of -M option, i.e. preferred name that should be stored
894 into the .gnu_debugaltlink or .debug_sup section. */
895 static const char *multifile_name;
896
897 /* True if -r option is present, i.e. .gnu_debugaltlink or .debug_sup section
898 should contain a filename relative to the directory in which
899 the particular file is present. */
900 static bool multifile_relative;
901
902 /* SHA1 checksum (build-id) of the common file. */
903 static unsigned char multifile_sha1[0x14];
904
905 /* True if DWARF 5 .debug_sup and DW_FORM_ref_sup4 / DW_FORM_strp_sup
906 should be used instead of the GNU extensions .gnu_debugaltlink
907 and DW_FORM_GNU_ref_alt / DW_FORM_GNU_strp_alt etc. */
908 static bool dwarf_5;
909
910 /* True if -q option has been passed. */
911 static bool quiet;
912
913 /* A single attribute in abbreviations. */
914 struct abbrev_attr
915 {
916 /* DW_AT_* attribute code. */
917 unsigned int attr;
918 /* DW_FORM_* form code. */
919 unsigned int form;
920 };
921
922 /* Internal structure for .debug_abbrev entries. */
923 struct abbrev_tag
924 {
925 /* Abbreviation number. */
926 unsigned int entry;
927 /* Hash value, in first abbrev hash tables it is
928 the same as entry, in cu->cu_new_abbrev hash tables
929 iterative hash of all the relevant values. */
930 hashval_t hash;
931 /* DW_TAG_* code. */
932 unsigned int tag;
933 /* Number of attributes. */
934 unsigned int nattr;
935 /* How many DIEs refer to this abbreviation (unused
936 in first abbrev hash tables). */
937 unsigned int nusers;
938 /* True if DIEs with this abbreviation have children. */
939 bool children;
940 /* True if any typed DWARF opcodes refer to this. */
941 bool op_type_referenced;
942 /* The values of DW_FORM_implicit_const attribute forms. */
943 int64_t *values;
944 /* Attribute/form pairs. */
945 struct abbrev_attr attr[0];
946 };
947
948 typedef struct dw_die *dw_die_ref;
949 typedef struct dw_cu *dw_cu_ref;
950 struct import_cu;
951
952 /* An entry from .debug_line file table. */
953 struct dw_file
954 {
955 char *dir;
956 char *file;
957 uint64_t time, size;
958 unsigned int file_angle_brackets_encapsulated_no_slash : 1;
959 };
960
961 /* Internal representation of a compilation (or partial)
962 unit. */
963 struct dw_cu
964 {
965 /* Cached entries from .debug_line file table. */
966 struct dw_file *cu_files;
967 unsigned int cu_nfiles;
968 /* Kind of CU, normal (present in .debug_info), newly created
969 partial unit, .debug_types unit or .debug_info partial unit
970 from the common file. */
971 enum { CU_NORMAL, CU_PU, CU_TYPES, CU_ALT } cu_kind;
972 /* CUs linked from first_cu through this chain. */
973 dw_cu_ref cu_next;
974 /* Offset in original .debug_info if CU_NORMAL or .debug_types
975 if CU_TYPES, otherwise a unique index of the newly created
976 partial CU. */
977 unsigned int cu_offset;
978 /* DWARF version of the CU. */
979 unsigned int cu_version;
980 /* Cached DW_AT_comp_dir value from DW_TAG_*_unit cu_die,
981 or NULL if that attribute is not present. */
982 char *cu_comp_dir;
983 /* Pointer to the DW_TAG_*_unit inside of the .debug_info
984 chunk. */
985 dw_die_ref cu_die;
986 /* The original abbreviation hash table. */
987 htab_t cu_abbrev;
988 /* New abbreviation hash table. */
989 htab_t cu_new_abbrev;
990 union dw_cu_u1
991 {
992 /* Pointer to another struct dw_cu that owns
993 cu_new_abbrev for this CU. */
994 dw_cu_ref cu_new_abbrev_owner;
995 /* Pointer used during create_import_tree. */
996 struct import_cu *cu_icu;
997 } u1;
998 union dw_cu_u2
999 {
1000 /* Offset into the new .debug_abbrev section. */
1001 unsigned int cu_new_abbrev_offset;
1002 /* Highest ->entry value in the new abbrev table
1003 For abbrevs computed by this tool it is always
1004 equal to the number of abbreviations, but if
1005 abbrevs are read for .debug_types section which
1006 is not rewritten, there might be holes. */
1007 unsigned int cu_largest_entry;
1008 } u2;
1009 /* Offset into the new .debug_info section. */
1010 unsigned int cu_new_offset;
1011 /* When op_multifile, record which object this came from here,
1012 otherwise it is the index of the CU. */
1013 unsigned int cu_chunk;
1014 /* Form chosen for intra-cu references. */
1015 enum dwarf_form cu_intracu_form;
1016 /* Intracusize argument to init_new_die_offsets. Set in compute_abbrevs,
1017 used in recompute_abbrevs. */
1018 unsigned int initial_intracusize;
1019 enum dwarf_source_language lang;
1020 };
1021
1022 /* Internal representation of a debugging information entry (DIE).
1023 This structure should be kept as small as possible,
1024 there are .debug_info sections with tens of millions of DIEs
1025 in them and this structure is allocated for each of them. */
1026 struct dw_die
1027 {
1028 /* Offset in old .debug_info from the start of the .debug_info section,
1029 -1U for newly created DIEs. */
1030 unsigned int die_offset;
1031 /* Cached copy of die_abbrev->tag. */
1032 enum dwarf_tag die_tag : 16;
1033 /* State of checksum computation. Not computed yet, computed and
1034 suitable for moving into partial units, currently being computed
1035 and finally determined unsuitable for moving into partial units. */
1036 enum { CK_UNKNOWN, CK_KNOWN, CK_BEING_COMPUTED, CK_BAD } die_ck_state : 2;
1037 /* Set if any DW_OP_call2 opcode refers to this DIE. */
1038 unsigned int die_op_call2_referenced : 1;
1039 /* Set if any DW_OP_GNU_{{regval,deref,const}_type,convert,reinterpret}
1040 opcode refers to this DIE. Only DW_TAG_base_type DIEs should be
1041 referenced. As those opcodes refer to them using uleb128, we need to try
1042 hard to have those DIEs with low enough offsets that the uleb128 will
1043 fit. */
1044 unsigned int die_op_type_referenced : 1;
1045 /* Set in DW_TAG_namespace or DW_TAG_module with DW_AT_name that is
1046 either a child of DW_TAG_*_unit, or a child of another
1047 die_named_namespace DIE. */
1048 unsigned int die_named_namespace : 1;
1049 /* Set if u.p1.die_ref_hash is valid. */
1050 unsigned int die_ref_hash_computed : 1;
1051 /* Set if die_dup and die_nextdup fields are after this structure.
1052 True for DW_TAG_*_unit DIEs, die_named_namespace DIEs and their
1053 immediate children. */
1054 unsigned int die_toplevel : 1;
1055 /* Set if we want to remove this DIE from its containing CU. */
1056 unsigned int die_remove : 1;
1057 /* Set if DIE is unsuitable for moving into alternate .debug_info. */
1058 unsigned int die_no_multifile : 1;
1059 /* Set if DIE is referenced using DW_FORM_ref*. So far only used during
1060 optimize_multifile and low_mem. */
1061 unsigned int die_referenced : 1;
1062 /* Set if DIE is referenced using DW_FORM_ref_addr. So far used only
1063 during low_mem. */
1064 unsigned int die_intercu_referenced : 1;
1065 /* Set if DIE has its children collapsed. Only used during
1066 optimize_multifile. */
1067 unsigned int die_collapsed_children : 1;
1068 /* Set on collapsed child DIE that is referenced. In that case, die_tag
1069 is reused for die_enter difference from parent and no fields after
1070 die_parent are allocated. */
1071 unsigned int die_collapsed_child : 1;
1072 /* Set if die_parent field is reused for struct dw_cu pointer. */
1073 unsigned int die_root : 1;
1074 /* State for ODR optimization. */
1075 enum { ODR_UNKNOWN, ODR_NONE, ODR_DEF, ODR_DECL } die_odr_state : 2;
1076 /* Tree pointer to parent. */
1077 dw_die_ref die_parent;
1078
1079 /* The remaining fields are present only if die_collapsed_child is
1080 0. */
1081
1082 /* Tree pointers, to first child and pointer to next sibling. */
1083 dw_die_ref die_child, die_sib;
1084 /* Pointer to the old .debug_abbrev entry's internal representation. */
1085 struct abbrev_tag *die_abbrev;
1086 /* Size of the DIE (abbrev number + attributes), not including children.
1087 In later phases this holds the new size as opposed to the old one. */
1088 unsigned int die_size;
1089 /* Index into the dw_die_ref vector used in checksum_ref_die function.
1090 While this is only phase 1 field, we get better packing by having it
1091 here instead of u.p1. */
1092 unsigned int die_ref_seen;
1093 union dw_die_phase
1094 {
1095 /* Fields used in the first phase (read_debug_info and partition_dups
1096 and functions they call). */
1097 struct dw_die_p1
1098 {
1099 /* Iterative hash value of the tag, attributes other than
1100 references or DW_FORM_ref_addr references or references
1101 within the subtree of ultimate parent's die_toplevel DIE's
1102 children. Computed by checksum_die function. */
1103 hashval_t die_hash;
1104 /* Iterative hash of other references. Computed by
1105 checksum_ref_die function. */
1106 hashval_t die_ref_hash;
1107 /* For ODR phase 1, we change die_hash for ODR_DEF and ODR_DECL DIEs
1108 to only hash in the tag and the name, to be able to construct
1109 maximal duplicate chains. But during ODR phase 2, we want to
1110 compare ODR_DEF DIEs in the normal way, for which we need the
1111 unchanged die_hash, which we store here in die_hash2. */
1112 hashval_t die_hash2;
1113 /* Tick count of entering and leaving a DIE during depth first
1114 traversal of the CU, used to quickly find if a subtree is
1115 referenced. */
1116 unsigned int die_enter, die_exit;
1117 } p1;
1118 /* Fields used only after the first phase (from compute_abbrevs
1119 till the end). */
1120 struct dw_die_p2
1121 {
1122 /* Pointer to internal representation of new .debug_abbrev
1123 entry for this DIE. */
1124 struct abbrev_tag *die_new_abbrev;
1125 /* Offset within the new .debug_info CU. Unlike die_offset
1126 this one is CU relative, so die_cu (die)->cu_new_offset needs
1127 to be added to it to get .debug_info offset. */
1128 unsigned int die_new_offset;
1129 /* Used during compute_abbrevs DW_FORM_ref_udata optimization. */
1130 unsigned int die_intracu_udata_size;
1131 } p2;
1132 } u;
1133
1134 /* The remaining fields are present only if die_toplevel is
1135 1. */
1136
1137 /* Pointer to a duplicate DIE. */
1138 dw_die_ref die_dup;
1139 /* Chain of duplicate DIEs. If die_dup is NULL, but die_nextdup
1140 is non-NULL, this is the reference DIE of the duplicates.
1141 All DIEs in the die->nextdup linked list have die_dup pointing
1142 to this node. The reference DIE is initially just a DIE in the
1143 lowest CU that has the matching DIE, later on it is a DIE in
1144 the newly created partial unit CU. */
1145 dw_die_ref die_nextdup;
1146 };
1147
1148 #include "iterators.h"
1149
1150 /* Return CU structure pointer for a DIE. In order to save memory,
1151 individual DIEs don't have a dw_cu_ref field, and the pointer can
1152 be only found by overriding the die_parent pointer in a
1153 DW_TAG_{compile,partial}_unit descriptor, which has no parent. */
1154 static inline dw_cu_ref
die_cu(dw_die_ref die)1155 die_cu (dw_die_ref die)
1156 {
1157 while (!die->die_root)
1158 die = die->die_parent;
1159 return (dw_cu_ref) die->die_parent;
1160 }
1161
1162 /* Given a toplevel die DIE, return the first (that is, the reference die) in
1163 the duplicate chain. */
1164 #define first_dup(die) \
1165 (die->die_dup \
1166 ? die->die_dup \
1167 : (die->die_nextdup \
1168 ? die \
1169 : NULL))
1170
1171 /* Safe variant that check die_toplevel. Can't be used on LHS. */
1172 #define die_safe_dup(die) \
1173 ((die)->die_toplevel ? (die)->die_dup : (dw_die_ref) NULL)
1174 #define die_safe_nextdup(die) \
1175 ((die)->die_toplevel ? (die)->die_nextdup : (dw_die_ref) NULL)
1176
1177 #ifdef __GNUC__
1178 # define ALIGN_STRUCT(name)
1179 #else
1180 # define ALIGN_STRUCT(name) struct align_##name { char c; struct name s; };
1181 #endif
1182 ALIGN_STRUCT (abbrev_tag)
1183 ALIGN_STRUCT (dw_file)
1184 ALIGN_STRUCT (dw_cu)
1185 ALIGN_STRUCT (dw_die)
1186
1187 /* Big pool allocator. obstack isn't efficient, because it aligns everything
1188 too much, and allocates too small chunks. All these objects are only freed
1189 together. */
1190
1191 /* Pointer to the start of the current pool chunk, current first free byte
1192 in the chunk and byte after the end of the current pool chunk. */
1193 static unsigned char *pool, *pool_next, *pool_limit;
1194
1195 /* After read_multifile, pool variable is moved over to this variable
1196 as the pool from read_multifile needs to be around for subsequent dwz
1197 calls. Freed only during the final cleanup at the very end. */
1198 static unsigned char *alt_pool;
1199
1200 /* Allocate SIZE bytes with ALIGN bytes alignment from the pool. */
1201 static void *
pool_alloc_1(unsigned int align,unsigned int size)1202 pool_alloc_1 (unsigned int align, unsigned int size)
1203 {
1204 void *ret;
1205 if (pool == NULL
1206 || (size_t) (pool_limit - pool_next) < (size_t) align + size)
1207 {
1208 size_t new_size = (size_t) align + size;
1209 unsigned char *new_pool;
1210 new_size += sizeof (void *);
1211 if (new_size < 16384 * 1024 - 64)
1212 new_size = 16384 * 1024 - 64;
1213 new_pool = (unsigned char *) malloc (new_size);
1214 if (new_pool == NULL)
1215 dwz_oom ();
1216 *(unsigned char **) new_pool = pool;
1217 pool_next = new_pool + sizeof (unsigned char *);
1218 pool_limit = new_pool + new_size;
1219 pool = new_pool;
1220 }
1221 pool_next = (unsigned char *) (((uintptr_t) pool_next + align - 1)
1222 & ~(uintptr_t) (align - 1));
1223 ret = pool_next;
1224 pool_next += size;
1225 return ret;
1226 }
1227
1228 /* Free the whole pool. */
1229 static void
pool_destroy(void)1230 pool_destroy (void)
1231 {
1232 pool_next = NULL;
1233 pool_limit = NULL;
1234 while (pool)
1235 {
1236 void *p = (void *) pool;
1237 pool = *(unsigned char **) pool;
1238 free (p);
1239 }
1240 }
1241
1242 #ifdef __GNUC__
1243 # define pool_alloc(name, size) \
1244 (struct name *) pool_alloc_1 (__alignof__ (struct name), size)
1245 #else
1246 # define pool_alloc(name, size) \
1247 (struct name *) pool_alloc_1 (offsetof (struct align_##name, s), size)
1248 #endif
1249
1250 static struct abbrev_tag *
pool_clone_abbrev(struct abbrev_tag * t)1251 pool_clone_abbrev (struct abbrev_tag *t)
1252 {
1253 struct abbrev_tag *newt;
1254 size_t newt_size;
1255 unsigned nvalue = 0;
1256 if (t->values != NULL)
1257 {
1258 unsigned i;
1259 for (i = 0; i < t->nattr; i++)
1260 if (t->attr[i].form == DW_FORM_implicit_const)
1261 nvalue = i + 1;
1262 }
1263 newt_size = (sizeof (*newt)
1264 + t->nattr * sizeof (struct abbrev_attr)
1265 + nvalue * sizeof (int64_t));
1266 newt = pool_alloc (abbrev_tag, newt_size);
1267 memcpy (newt, t, newt_size - (nvalue * sizeof (int64_t)));
1268 if (nvalue == 0)
1269 newt->values = NULL;
1270 else
1271 {
1272 newt->values = (int64_t *) &newt->attr[newt->nattr];
1273 memcpy (newt->values, t->values, nvalue * sizeof (int64_t));
1274 }
1275 return newt;
1276 }
1277
1278 /* Hash function in first abbrev hash table as well as cu->cu_new_abbrev
1279 htab. */
1280 static hashval_t
abbrev_hash(const void * p)1281 abbrev_hash (const void *p)
1282 {
1283 struct abbrev_tag *t = (struct abbrev_tag *)p;
1284
1285 return t->hash;
1286 }
1287
1288 /* Equality function in first abbrev htab. */
1289 static int
abbrev_eq(const void * p,const void * q)1290 abbrev_eq (const void *p, const void *q)
1291 {
1292 struct abbrev_tag *t1 = (struct abbrev_tag *)p;
1293 struct abbrev_tag *t2 = (struct abbrev_tag *)q;
1294
1295 return t1->entry == t2->entry;
1296 }
1297
1298 /* Equality function in cu->cu_new_abbrev htab. */
1299 static int
abbrev_eq2(const void * p,const void * q)1300 abbrev_eq2 (const void *p, const void *q)
1301 {
1302 struct abbrev_tag *t1 = (struct abbrev_tag *)p;
1303 struct abbrev_tag *t2 = (struct abbrev_tag *)q;
1304 unsigned int i;
1305
1306 if (t1->hash != t2->hash
1307 || t1->tag != t2->tag
1308 || t1->nattr != t2->nattr
1309 || t1->children != t2->children)
1310 return 0;
1311 for (i = 0; i < t1->nattr; i++)
1312 if (t1->attr[i].attr != t2->attr[i].attr
1313 || t1->attr[i].form != t2->attr[i].form
1314 || (t1->attr[i].form == DW_FORM_implicit_const
1315 && t1->values[i] != t2->values[i]))
1316 return 0;
1317 return 1;
1318 }
1319
1320 /* Helper function to compute abbrev entry iterative hash value. */
1321 static void
compute_abbrev_hash(struct abbrev_tag * t)1322 compute_abbrev_hash (struct abbrev_tag *t)
1323 {
1324 unsigned int i;
1325
1326 t->hash = iterative_hash_object (t->tag, 0);
1327 t->hash = iterative_hash_object (t->nattr, t->hash);
1328 t->hash = iterative_hash_object (t->children, t->hash);
1329 for (i = 0; i < t->nattr; i++)
1330 {
1331 t->hash = iterative_hash_object (t->attr[i].attr, t->hash);
1332 t->hash = iterative_hash_object (t->attr[i].form, t->hash);
1333 if (t->attr[i].form == DW_FORM_implicit_const)
1334 t->hash = iterative_hash_object (t->values[i], t->hash);
1335 }
1336 }
1337
1338 /* Maximum number of attributes in a DIE. */
1339 static unsigned int max_nattr;
1340
1341 /* Parse a .debug_abbrev entry at PTR. */
1342 static htab_t
read_abbrev(DSO * dso,unsigned char * ptr)1343 read_abbrev (DSO *dso, unsigned char *ptr)
1344 {
1345 htab_t h;
1346 unsigned int attr, form;
1347 struct abbrev_tag *t;
1348 void **slot;
1349
1350 h = htab_try_create (50, abbrev_hash, abbrev_eq, NULL);
1351 if (h == NULL)
1352 dwz_oom ();
1353
1354 while ((attr = read_uleb128 (ptr)) != 0)
1355 {
1356 int highest_implicit_value_ndx = -1;
1357 unsigned int nattr = 0;
1358 unsigned char *p = ptr;
1359
1360 skip_leb128 (p);
1361 p++;
1362 while (read_uleb128 (p) != 0)
1363 {
1364 nattr++;
1365 form = read_uleb128 (p);
1366 if (form == DW_FORM_implicit_const)
1367 {
1368 skip_leb128 (p);
1369 highest_implicit_value_ndx = nattr - 1;
1370 }
1371 else if (form == 2
1372 || (form > DW_FORM_flag_present
1373 && !(form == DW_FORM_ref_sig8
1374 || form == DW_FORM_data16
1375 || form == DW_FORM_line_strp)))
1376 {
1377 error (0, 0, "%s: Unknown DWARF %s",
1378 dso->filename, get_DW_FORM_str (form));
1379 htab_delete (h);
1380 return NULL;
1381 }
1382 }
1383 if (read_uleb128 (p) != 0)
1384 {
1385 error (0, 0, "%s: DWARF abbreviation does not end with 2 zeros",
1386 dso->filename);
1387 htab_delete (h);
1388 return NULL;
1389 }
1390
1391 t = pool_alloc (abbrev_tag,
1392 sizeof (*t) + nattr * sizeof (struct abbrev_attr)
1393 + sizeof (int64_t) * (highest_implicit_value_ndx + 1));
1394 t->entry = attr;
1395 t->hash = attr;
1396 t->nattr = nattr;
1397 t->nusers = 0;
1398 t->tag = read_uleb128 (ptr);
1399 t->children = *ptr++ == DW_CHILDREN_yes;
1400 t->op_type_referenced = false;
1401 t->values = (highest_implicit_value_ndx >= 0
1402 ? (int64_t *) &t->attr[nattr] : NULL);
1403 nattr = 0;
1404 while ((attr = read_uleb128 (ptr)) != 0)
1405 {
1406 form = read_uleb128 (ptr);
1407 if (form == DW_FORM_implicit_const)
1408 t->values[nattr] = read_sleb128 (ptr);
1409 t->attr[nattr].attr = attr;
1410 t->attr[nattr++].form = form;
1411 }
1412 skip_leb128 (ptr);
1413 if (t->nattr > max_nattr)
1414 max_nattr = t->nattr;
1415 slot = htab_find_slot_with_hash (h, t, t->hash, INSERT);
1416 if (slot == NULL)
1417 {
1418 htab_delete (h);
1419 dwz_oom ();
1420 }
1421 if (*slot != NULL)
1422 {
1423 error (0, 0, "%s: Duplicate DWARF abbreviation %d", dso->filename,
1424 t->entry);
1425 htab_delete (h);
1426 return NULL;
1427 }
1428 *slot = t;
1429 }
1430
1431 return h;
1432 }
1433
1434 /* For a die attribute with form FORM starting at PTR, with the die in CU,
1435 return the pointer after the attribute, assuming FORM is not
1436 dw_form_indirect. */
1437 static inline unsigned char * FORCE_INLINE
skip_attr_no_dw_form_indirect(unsigned int cu_version,uint32_t form,unsigned char * ptr)1438 skip_attr_no_dw_form_indirect (unsigned int cu_version, uint32_t form,
1439 unsigned char *ptr)
1440 {
1441 size_t len = 0;
1442
1443 switch (form)
1444 {
1445 case DW_FORM_ref_addr:
1446 ptr += cu_version == 2 ? ptr_size : 4;
1447 break;
1448 case DW_FORM_addr:
1449 ptr += ptr_size;
1450 break;
1451 case DW_FORM_flag_present:
1452 case DW_FORM_implicit_const:
1453 break;
1454 case DW_FORM_ref1:
1455 case DW_FORM_flag:
1456 case DW_FORM_data1:
1457 ++ptr;
1458 break;
1459 case DW_FORM_ref2:
1460 case DW_FORM_data2:
1461 ptr += 2;
1462 break;
1463 case DW_FORM_ref4:
1464 case DW_FORM_data4:
1465 case DW_FORM_sec_offset:
1466 case DW_FORM_strp:
1467 case DW_FORM_line_strp:
1468 ptr += 4;
1469 break;
1470 case DW_FORM_ref8:
1471 case DW_FORM_data8:
1472 case DW_FORM_ref_sig8:
1473 ptr += 8;
1474 break;
1475 case DW_FORM_data16:
1476 ptr += 16;
1477 break;
1478 case DW_FORM_sdata:
1479 case DW_FORM_ref_udata:
1480 case DW_FORM_udata:
1481 skip_leb128 (ptr);
1482 break;
1483 case DW_FORM_string:
1484 ptr = (unsigned char *) strchr ((char *)ptr, '\0') + 1;
1485 break;
1486 case DW_FORM_indirect:
1487 abort ();
1488 case DW_FORM_block1:
1489 len = *ptr++;
1490 break;
1491 case DW_FORM_block2:
1492 len = read_16 (ptr);
1493 form = DW_FORM_block1;
1494 break;
1495 case DW_FORM_block4:
1496 len = read_32 (ptr);
1497 form = DW_FORM_block1;
1498 break;
1499 case DW_FORM_block:
1500 case DW_FORM_exprloc:
1501 len = read_uleb128 (ptr);
1502 form = DW_FORM_block1;
1503 break;
1504 default:
1505 abort ();
1506 }
1507
1508 if (form == DW_FORM_block1)
1509 ptr += len;
1510
1511 return ptr;
1512 }
1513
1514 /* Read the directory and file table from .debug_line offset OFF,
1515 record it in CU. */
1516 static int
read_debug_line(DSO * dso,dw_cu_ref cu,uint32_t off)1517 read_debug_line (DSO *dso, dw_cu_ref cu, uint32_t off)
1518 {
1519 unsigned char *ptr = debug_sections[DEBUG_LINE].data, *dir, *file;
1520 unsigned char **dirt;
1521 unsigned char *endsec = ptr + debug_sections[DEBUG_LINE].size;
1522 unsigned char *endcu, *endprol;
1523 unsigned char opcode_base;
1524 unsigned int culen;
1525 uint32_t value, version, ndirs, nfiles, dirt_cnt, file_cnt;
1526 /* DWARF5 has a dynamic table of elements in possible different
1527 forms. But we are only interested in the known elements (path,
1528 dir index, time, size and possibly later md5). */
1529 unsigned char n, nelems = 0;
1530 int path_ndx = -1;
1531 int dir_ndx = -1;
1532 int time_ndx = -1;
1533 int size_ndx = -1;
1534 uint16_t elems[256];
1535
1536 if (off >= debug_sections[DEBUG_LINE].size - 4)
1537 {
1538 error (0, 0, "%s: .debug_line reference above end of section",
1539 dso->filename);
1540 return 1;
1541 }
1542
1543 ptr += off;
1544
1545 endcu = ptr + 4;
1546 culen = read_32 (ptr);
1547 if (culen >= 0xfffffff0)
1548 {
1549 error (0, 0, "%s: 64-bit DWARF not supported", dso->filename);
1550 return 1;
1551 }
1552 endcu += culen;
1553
1554 if (endcu > endsec)
1555 {
1556 error (0, 0, "%s: .debug_line CU does not fit into section",
1557 dso->filename);
1558 return 1;
1559 }
1560
1561 value = read_16 (ptr);
1562 if (value < 2 || value > 5)
1563 {
1564 error (0, 0, "%s: DWARF version %d in .debug_line unhandled",
1565 dso->filename, value);
1566 return 1;
1567 }
1568 version = value;
1569
1570 if (version < lowest_line_version)
1571 lowest_line_version = version;
1572
1573 if (version >= 5)
1574 {
1575 int addr_size, seg_size;
1576 if (ptr + 2 > endcu)
1577 {
1578 error (0, 0, "%s: .debug_line header too short", dso->filename);
1579 return 1;
1580 }
1581 addr_size = *ptr++;
1582 seg_size = *ptr++;
1583 if (addr_size != ptr_size)
1584 {
1585 error (0, 0, "%s: .debug_line address size differs from CU ptr size",
1586 dso->filename);
1587 return 1;
1588 }
1589 if (seg_size != 0)
1590 {
1591 error (0, 0, "%s: .debug_line non-zero segment selector size",
1592 dso->filename);
1593 return 1;
1594 }
1595 }
1596
1597 endprol = ptr + 4;
1598 endprol += read_32 (ptr);
1599 if (endprol > endcu)
1600 {
1601 error (0, 0, "%s: .debug_line CU prologue does not fit into CU",
1602 dso->filename);
1603 return 1;
1604 }
1605
1606 opcode_base = ptr[4 + (version >= 4)];
1607 ptr = dir = ptr + 4 + (version >= 4) + opcode_base;
1608
1609 /* dir table: */
1610 if (version < 5)
1611 {
1612 value = 1;
1613 while (*ptr != 0)
1614 {
1615 ptr = (unsigned char *) strchr ((char *)ptr, 0) + 1;
1616 ++value;
1617 }
1618 ndirs = value;
1619 }
1620 else
1621 {
1622 nelems = *ptr++;
1623 for (n = 0; n < nelems; n++)
1624 {
1625 uint16_t lnct = read_uleb128 (ptr);
1626 uint16_t form = read_uleb128 (ptr);
1627 if (lnct == DW_LNCT_path)
1628 {
1629 if (path_ndx != -1)
1630 {
1631 error (0, 0, "%s: .debug_line duplicate dir path elements",
1632 dso->filename);
1633 return 1;
1634 }
1635 path_ndx = n;
1636 }
1637 else
1638 {
1639 error (0, 0, "%s: .debug_line unhandled dir element %s",
1640 dso->filename, get_DW_LNCT_str (lnct));
1641 return 1;
1642 }
1643
1644 if (form != DW_FORM_string
1645 && form != DW_FORM_strp
1646 && form != DW_FORM_line_strp)
1647 {
1648 error (0, 0, "%s: .debug_line unhandled form %s for dir path",
1649 dso->filename, get_DW_FORM_str (form));
1650 return 1;
1651 }
1652
1653 elems[n] = form;
1654 }
1655
1656 ndirs = read_uleb128 (ptr);
1657 }
1658
1659 dirt = (unsigned char **) alloca (ndirs * sizeof (unsigned char *));
1660 if (version < 5)
1661 {
1662 dirt[0] = NULL;
1663 dirt_cnt = 1;
1664 ptr = dir;
1665 while (*ptr != 0)
1666 {
1667 dirt[dirt_cnt++] = ptr;
1668 ptr = (unsigned char *) strchr ((char *)ptr, 0) + 1;
1669 }
1670 ptr++;
1671 }
1672 else
1673 {
1674 for (dirt_cnt = 0; dirt_cnt < ndirs; dirt_cnt++)
1675 {
1676 for (n = 0; n < nelems; n++)
1677 {
1678 uint32_t form = elems[n];
1679 if (n == path_ndx)
1680 {
1681 unsigned char *d;
1682 switch (form)
1683 {
1684 case DW_FORM_string:
1685 d = (unsigned char *) ptr;
1686 break;
1687 case DW_FORM_strp:
1688 {
1689 unsigned int strp = do_read_32 (ptr);
1690 if (strp >= debug_sections[DEBUG_STR].size)
1691 d = NULL;
1692 else
1693 d = ((unsigned char *)
1694 debug_sections[DEBUG_STR].data
1695 + strp);
1696 }
1697 break;
1698 case DW_FORM_line_strp:
1699 {
1700 unsigned int line_strp = do_read_32 (ptr);
1701 if (line_strp >= debug_sections[DEBUG_LINE_STR].size)
1702 d = NULL;
1703 else
1704 d = ((unsigned char *)
1705 debug_sections[DEBUG_LINE_STR].data
1706 + line_strp);
1707 }
1708 break;
1709 default:
1710 d = NULL;
1711 break;
1712 }
1713
1714 if (d == NULL)
1715 {
1716 error (0, 0, "%s: .debug_line bad dir path",
1717 dso->filename);
1718 return 1;
1719 }
1720
1721 /* Note we do this even for the zero entry, which is
1722 marked as NULL for pre-DWARF5 line tables. This
1723 is important for when we merge file entries
1724 together for a multifile because the zero dir
1725 entry could differ. It is should be equivalent
1726 to the CU DIE comp_dir attribute, but we don't
1727 track that all CUs referring to the (same) line
1728 table share identical an DW_AT_comp_dir value. */
1729 dirt[dirt_cnt] = d;
1730 }
1731 ptr = skip_attr_no_dw_form_indirect (cu->cu_version, form, ptr);
1732 }
1733 }
1734 }
1735
1736 /* file table: */
1737 file = ptr;
1738 if (version < 5)
1739 {
1740 file_cnt = 0;
1741 while (*ptr != 0)
1742 {
1743 ptr = (unsigned char *) strchr ((char *)ptr, 0) + 1;
1744 value = read_uleb128 (ptr);
1745
1746 if (value >= dirt_cnt)
1747 {
1748 error (0, 0, "%s: Wrong directory table index %u",
1749 dso->filename, value);
1750 return 1;
1751 }
1752
1753 skip_leb128 (ptr);
1754 skip_leb128 (ptr);
1755 file_cnt++;
1756 }
1757 nfiles = file_cnt;
1758 }
1759 else
1760 {
1761 nelems = *ptr++;
1762 path_ndx = -1;
1763 for (n = 0; n < nelems; n++)
1764 {
1765 uint16_t lnct = read_uleb128 (ptr);
1766 uint16_t form = read_uleb128 (ptr);
1767 switch (lnct)
1768 {
1769 case DW_LNCT_path:
1770 if (path_ndx != -1)
1771 {
1772 error (0, 0,
1773 "%s: .debug_line duplicate file path elements",
1774 dso->filename);
1775 return 1;
1776 }
1777 path_ndx = n;
1778
1779 /* Currently we only handle two string form which always
1780 stay... */
1781 if (form != DW_FORM_string && form != DW_FORM_line_strp)
1782 {
1783 error (0, 0,
1784 "%s: .debug_line unhandled form %s for file path",
1785 dso->filename, get_DW_FORM_str (form));
1786 return 1;
1787 }
1788 break;
1789
1790 case DW_LNCT_directory_index:
1791 if (dir_ndx != -1)
1792 {
1793 error (0, 0,
1794 "%s: .debug_line duplicate file dir elements",
1795 dso->filename);
1796 return 1;
1797 }
1798 dir_ndx = n;
1799
1800 if (form != DW_FORM_data1
1801 && form != DW_FORM_data2
1802 && form != DW_FORM_udata)
1803 {
1804 error (0, 0,
1805 "%s: .debug_line unhandled form %s for dir index",
1806 dso->filename, get_DW_FORM_str (form));
1807 return 1;
1808 }
1809 break;
1810
1811 case DW_LNCT_timestamp:
1812 if (time_ndx != -1)
1813 {
1814 error (0, 0,
1815 "%s: .debug_line duplicate file time elements",
1816 dso->filename);
1817 return 1;
1818 }
1819 time_ndx = n;
1820
1821 if (form != DW_FORM_udata
1822 && form != DW_FORM_data4
1823 && form != DW_FORM_data8)
1824 {
1825 error (0, 0,
1826 "%s: .debug_line unhandled form %s for file time",
1827 dso->filename, get_DW_FORM_str (form));
1828 return 1;
1829 }
1830 break;
1831
1832 case DW_LNCT_size:
1833 if (size_ndx != -1)
1834 {
1835 error (0, 0,
1836 "%s: .debug_line duplicate file size elements",
1837 dso->filename);
1838 return 1;
1839 }
1840 size_ndx = n;
1841
1842 if (form != DW_FORM_udata
1843 && form != DW_FORM_data1
1844 && form != DW_FORM_data2
1845 && form != DW_FORM_data4
1846 && form != DW_FORM_data8)
1847 {
1848 error (0, 0,
1849 "%s: .debug_line unhandled form %s for file size",
1850 dso->filename, get_DW_FORM_str (form));
1851 return 1;
1852 }
1853 break;
1854
1855 default:
1856 error (0, 0, "%s: .debug_line unhandled file element %s",
1857 dso->filename, get_DW_LNCT_str (lnct));
1858 return 1;
1859 }
1860 elems[n] = form;
1861 }
1862
1863 nfiles = read_uleb128 (ptr);
1864 if (nfiles > 0)
1865 nfiles--; /* We will skip the first (zero) entry. */
1866 }
1867
1868 cu->cu_nfiles = nfiles;
1869 cu->cu_files = pool_alloc (dw_file, nfiles * sizeof (struct dw_file));
1870 memset (cu->cu_files, 0, nfiles * sizeof (struct dw_file));
1871
1872 if (version < 5)
1873 ptr = file;
1874
1875 for (file_cnt = 0; file_cnt < nfiles; file_cnt++)
1876 {
1877 char *f = NULL;
1878 char *end = NULL;
1879 uint32_t d = 0;
1880 uint64_t time = 0;
1881 uint64_t size = 0;
1882 if (version < 5)
1883 {
1884 f = (char *) ptr;
1885 ptr = (unsigned char *) strchr ((char *)ptr, 0) + 1;
1886 end = (char *) ptr;
1887 d = read_uleb128 (ptr);
1888 time = read_uleb128 (ptr);
1889 size = read_uleb128 (ptr);
1890 }
1891 else
1892 {
1893 /* Skip zero entry. */
1894 if (file_cnt == 0)
1895 for (n = 0; n < nelems; n++)
1896 ptr = skip_attr_no_dw_form_indirect (cu->cu_version,
1897 elems[n], ptr);
1898
1899 for (n = 0; n < nelems; n++)
1900 {
1901 uint32_t form = elems[n];
1902 if (n == path_ndx)
1903 {
1904 switch (form)
1905 {
1906 case DW_FORM_string:
1907 f = (char *) ptr;
1908 end = strchr ((char *)ptr, 0) + 1;;
1909 break;
1910 case DW_FORM_strp:
1911 {
1912 unsigned int strp = do_read_32 (ptr);
1913 if (strp >= debug_sections[DEBUG_STR].size)
1914 f = NULL;
1915 else
1916 {
1917 f = ((char *) debug_sections[DEBUG_STR].data
1918 + strp);
1919 end = f + strlen (f) + 1;
1920 }
1921 }
1922 break;
1923 case DW_FORM_line_strp:
1924 {
1925 unsigned int line_strp = do_read_32 (ptr);
1926 if (line_strp >= debug_sections[DEBUG_LINE_STR].size)
1927 f = NULL;
1928 else
1929 {
1930 f = ((char *) debug_sections[DEBUG_LINE_STR].data
1931 + line_strp);
1932 end = f + strlen (f) + 1;
1933 }
1934 }
1935 break;
1936 default:
1937 f = NULL;
1938 break;
1939 }
1940
1941 if (f == NULL)
1942 {
1943 error (0, 0, "%s: .debug_line bad file path",
1944 dso->filename);
1945 return 1;
1946 }
1947 }
1948 else if (n == dir_ndx)
1949 {
1950 switch (form)
1951 {
1952 case DW_FORM_data1:
1953 d = *ptr;
1954 break;
1955 case DW_FORM_data2:
1956 d = do_read_16 (ptr);
1957 break;
1958 case DW_FORM_udata:
1959 {
1960 unsigned char *p = ptr;
1961 d = read_uleb128 (p);
1962 }
1963 break;
1964 }
1965 }
1966 ptr = skip_attr_no_dw_form_indirect (cu->cu_version, form, ptr);
1967 }
1968 }
1969
1970 cu->cu_files[file_cnt].file = f;
1971 if (d >= dirt_cnt)
1972 {
1973 error (0, 0, "%s: Wrong directory table index %u",
1974 dso->filename, value);
1975 return 1;
1976 }
1977
1978 cu->cu_files[file_cnt].dir = (char *) dirt[d];
1979 cu->cu_files[file_cnt].time = time;
1980 cu->cu_files[file_cnt].size = size;
1981 size_t file_len = (char *) end - f;
1982 size_t strlen_file = file_len - 1;
1983 bool file_has_slash = false;
1984 if (cu->cu_files[file_cnt].file[0] != '/'
1985 && cu->cu_files[file_cnt].dir != NULL)
1986 {
1987 size_t dir_len = strlen (cu->cu_files[file_cnt].dir);
1988 if (dir_len)
1989 {
1990 obstack_grow (&ob, cu->cu_files[file_cnt].dir,
1991 dir_len);
1992 strlen_file += dir_len;
1993 if (cu->cu_files[file_cnt].dir[dir_len - 1] != '/')
1994 {
1995 obstack_1grow (&ob, '/');
1996 strlen_file++;
1997 }
1998 file_has_slash = true;
1999 obstack_grow (&ob, cu->cu_files[file_cnt].file,
2000 file_len);
2001 cu->cu_files[file_cnt].file
2002 = (char *) obstack_finish (&ob);
2003 cu->cu_files[file_cnt].dir = NULL;
2004 }
2005 }
2006 cu->cu_files[file_cnt].file_angle_brackets_encapsulated_no_slash
2007 = (!file_has_slash
2008 && cu->cu_files[file_cnt].file[0] == '<'
2009 && cu->cu_files[file_cnt].file[strlen_file - 1] == '>'
2010 && strchr (cu->cu_files[file_cnt].file, '/') == NULL);
2011 }
2012
2013 return 0;
2014 }
2015
2016 /* Estimate the amount of DIEs in the .debug_info section, based on the size
2017 of that section. */
2018 static unsigned int UNUSED
estimate_nr_dies(void)2019 estimate_nr_dies (void)
2020 {
2021 unsigned int average_die_size = 11;
2022 unsigned int nr_dies = debug_sections[DEBUG_INFO].size / average_die_size;
2023 return nr_dies;
2024 }
2025
2026 static size_t UNUSED
emulate_htab(size_t initial,size_t final_nr_elements)2027 emulate_htab (size_t initial, size_t final_nr_elements)
2028 {
2029 size_t size = initial;
2030
2031 /* Emulate creation. */
2032 size = higher_prime_number (size);
2033
2034 /* Emulate growing till htab contains find_nr_elements. */
2035 while (1)
2036 {
2037 /* Emulate expansion trigger. */
2038 size_t nr_elements = size * 3 / 4;
2039 while (!(size * 3 <= nr_elements * 4))
2040 nr_elements++;
2041
2042 if (nr_elements > final_nr_elements)
2043 {
2044 nr_elements = final_nr_elements;
2045 break;
2046 }
2047
2048 /* Emulate expansion. */
2049 size = size * 2;
2050 size = higher_prime_number (size);
2051 }
2052
2053 return size;
2054 }
2055
2056 /* Print hash table statistics for hash table HTAB with message string MSG. */
2057 static void
htab_report(htab_t htab,const char * msg)2058 htab_report (htab_t htab, const char *msg)
2059 {
2060 double collisions = htab_collisions (htab);
2061 unsigned int searches = htab->searches;
2062 size_t elements = htab->n_elements;
2063 size_t deleted = htab->n_deleted;
2064 size_t adjusted_elements = elements - deleted;
2065 size_t size = htab->size;
2066 double occupancy = (double)elements / (double)size;
2067 double adjusted_occupancy = (double)adjusted_elements / (double)size;
2068 /* Indent unconditional fprintfs similar to conditional fprintfs to
2069 left-align literal strings. */
2070 if (1)
2071 fprintf (stderr, "htab: %s\n", msg);
2072 if (1)
2073 fprintf (stderr, " size: %zu\n", size);
2074 if (elements > 0 && deleted == 0)
2075 fprintf (stderr, " elements: %zu, occupancy: %f\n", elements,
2076 occupancy);
2077 if (deleted > 0)
2078 fprintf (stderr, " elements (incl. deleted): %zu, occupancy: %f\n",
2079 elements, occupancy);
2080 if (deleted > 0)
2081 fprintf (stderr, " elements (excl. deleted): %zu, occupancy: %f\n",
2082 adjusted_elements, adjusted_occupancy);
2083 if (elements > 0)
2084 fprintf (stderr, " searches: %u, collisions: %f\n", searches,
2085 collisions);
2086 }
2087
2088 /* Hash function for off_htab hash table. */
2089 static hashval_t
off_hash(const void * p)2090 off_hash (const void *p)
2091 {
2092 dw_die_ref die = (dw_die_ref) p;
2093
2094 return die->die_offset / 6;
2095 }
2096
2097 /* Equality function for off_htab hash table. */
2098 static int
off_eq(const void * p,const void * q)2099 off_eq (const void *p, const void *q)
2100 {
2101 return ((dw_die_ref) p)->die_offset == ((dw_die_ref) q)->die_offset;
2102 }
2103
2104 /* Hash table to map die_offset values to struct dw_die pointers. */
2105 static htab_t off_htab;
2106
2107 /* After read_multifile off_htab is copied over to this variable.
2108 Offsets in the alternate .debug_info are found using this hash table. */
2109 static htab_t alt_off_htab;
2110
2111 /* Offset hash table for .debug_types section. */
2112 static htab_t types_off_htab;
2113
2114 /* Function to add DIE into the hash table (and create the hash table
2115 when not already created). */
2116 static void
off_htab_add_die(dw_cu_ref cu,dw_die_ref die,unsigned int * die_count)2117 off_htab_add_die (dw_cu_ref cu, dw_die_ref die, unsigned int *die_count)
2118 {
2119 void **slot;
2120
2121 if (unlikely (cu->cu_kind == CU_TYPES))
2122 {
2123 if (types_off_htab == NULL)
2124 {
2125 types_off_htab = htab_try_create (100000, off_hash, off_eq, NULL);
2126 if (types_off_htab == NULL)
2127 dwz_oom ();
2128 }
2129
2130 slot = htab_find_slot (types_off_htab, die, INSERT);
2131 if (slot == NULL)
2132 dwz_oom ();
2133 assert (*slot == NULL);
2134 *slot = die;
2135 return;
2136 }
2137
2138 if (off_htab == NULL)
2139 {
2140 unsigned int estimated_nr_dies = estimate_nr_dies ();
2141 size_t default_initial_size = 100000;
2142 size_t initial_size;
2143 if (low_mem
2144 || op_multifile
2145 || (multifile_mode == 0
2146 && die_count_method == estimate
2147 && (estimated_nr_dies >= low_mem_die_limit
2148 || estimated_nr_dies >= max_die_limit)))
2149 initial_size = default_initial_size;
2150 else
2151 {
2152 size_t nr_dies;
2153 if (die_count && *die_count != 0)
2154 {
2155 nr_dies = *die_count;
2156 if (tracing)
2157 fprintf (stderr, "Using die count %zu for off_htab"
2158 " allocation\n", nr_dies);
2159 }
2160 else if (die_count_method == none)
2161 nr_dies = 0;
2162 else if (die_count_method == estimate)
2163 {
2164 nr_dies = estimated_nr_dies;
2165 if (tracing)
2166 fprintf (stderr, "Using die count estimate %zu for off_htab"
2167 " allocation\n", nr_dies);
2168 }
2169 else
2170 assert (false);
2171
2172 if (nr_dies != 0)
2173 {
2174 size_t final_hashtab_size
2175 = emulate_htab (default_initial_size, nr_dies);
2176 initial_size = final_hashtab_size;
2177 }
2178 else
2179 initial_size = default_initial_size;
2180 }
2181 off_htab = htab_try_create (initial_size, off_hash, off_eq, NULL);
2182 if (tracing)
2183 htab_report (off_htab, "off_htab allocation");
2184 if (off_htab == NULL)
2185 dwz_oom ();
2186 if (rd_multifile)
2187 alt_off_htab = off_htab;
2188 }
2189
2190 slot = htab_find_slot_with_hash (off_htab, die, off_hash (die), INSERT);
2191 if (slot == NULL)
2192 dwz_oom ();
2193 assert (*slot == NULL);
2194 *slot = die;
2195 }
2196
2197 /* For DIE_OFFSET return dw_die_ref whose die_offset field is equal
2198 to that value. Return NULL if no DIE is at that position (buggy
2199 DWARF input?). */
2200 static dw_die_ref
off_htab_lookup(dw_cu_ref cu,unsigned int die_offset)2201 off_htab_lookup (dw_cu_ref cu, unsigned int die_offset)
2202 {
2203 struct dw_die die;
2204 die.die_offset = die_offset;
2205 if (cu == NULL)
2206 return (dw_die_ref) htab_find_with_hash (off_htab, &die, off_hash (&die));
2207 if (unlikely (cu->cu_kind == CU_ALT))
2208 return (dw_die_ref) htab_find_with_hash (alt_off_htab, &die,
2209 off_hash (&die));
2210 if (unlikely (cu->cu_kind == CU_TYPES))
2211 return (dw_die_ref) htab_find_with_hash (types_off_htab, &die,
2212 off_hash (&die));
2213 return (dw_die_ref) htab_find_with_hash (off_htab, &die, off_hash (&die));
2214 }
2215
2216 /* For a die attribute ATTR starting at PTR, with the die in CU, return the
2217 pointer after the attribute. */
2218 static inline unsigned char * FORCE_INLINE
skip_attr(unsigned int cu_version,struct abbrev_attr * attr,unsigned char * ptr)2219 skip_attr (unsigned int cu_version, struct abbrev_attr *attr,
2220 unsigned char *ptr)
2221 {
2222 uint32_t form = attr->form;
2223
2224 while (form == DW_FORM_indirect)
2225 form = read_uleb128 (ptr);
2226 return skip_attr_no_dw_form_indirect (cu_version, form, ptr);
2227 }
2228
2229 /* Return a pointer at which DIE's attribute AT is encoded, and fill in
2230 its form into *FORMP. Return NULL if the attribute is not present. */
2231 static unsigned char *
get_AT(dw_die_ref die,enum dwarf_attribute at,enum dwarf_form * formp)2232 get_AT (dw_die_ref die, enum dwarf_attribute at, enum dwarf_form *formp)
2233 {
2234 struct abbrev_tag *t = die->die_abbrev;
2235 unsigned int i;
2236 unsigned char *ptr;
2237 dw_cu_ref cu = die_cu (die);
2238 if (unlikely (fi_multifile) && cu->cu_kind == CU_ALT)
2239 ptr = alt_data[DEBUG_INFO];
2240 else if (cu->cu_kind == CU_TYPES)
2241 ptr = debug_sections[DEBUG_TYPES].data;
2242 else
2243 ptr = debug_sections[DEBUG_INFO].data;
2244 ptr += die->die_offset;
2245 skip_leb128 (ptr);
2246 for (i = 0; i < t->nattr; ++i)
2247 {
2248 uint32_t form = t->attr[i].form;
2249
2250 while (form == DW_FORM_indirect)
2251 form = read_uleb128 (ptr);
2252 if (t->attr[i].attr == at)
2253 {
2254 *formp = form;
2255 if (form == DW_FORM_implicit_const)
2256 return (unsigned char *) &t->values[i];
2257 return ptr;
2258 }
2259
2260 ptr = skip_attr_no_dw_form_indirect (cu->cu_version, form, ptr);
2261 }
2262 return NULL;
2263 }
2264
2265 /* Return an integer attribute AT of DIE. Set *PRESENT to true
2266 if found. */
2267 static uint64_t
get_AT_int(dw_die_ref die,enum dwarf_attribute at,bool * present,enum dwarf_form * formp)2268 get_AT_int (dw_die_ref die, enum dwarf_attribute at, bool *present,
2269 enum dwarf_form *formp)
2270 {
2271 unsigned char *ptr;
2272 ptr = get_AT (die, at, formp);
2273 *present = false;
2274 if (ptr == NULL)
2275 return 0;
2276 *present = true;
2277 switch (*formp)
2278 {
2279 case DW_FORM_ref_addr:
2280 return read_size (ptr, die_cu (die)->cu_version == 2 ? ptr_size : 4);
2281 case DW_FORM_addr:
2282 return read_size (ptr, ptr_size);
2283 case DW_FORM_flag_present:
2284 return 1;
2285 case DW_FORM_ref1:
2286 case DW_FORM_flag:
2287 case DW_FORM_data1:
2288 return read_8 (ptr);
2289 case DW_FORM_ref2:
2290 case DW_FORM_data2:
2291 return read_16 (ptr);
2292 case DW_FORM_ref4:
2293 case DW_FORM_data4:
2294 case DW_FORM_sec_offset:
2295 return read_32 (ptr);
2296 case DW_FORM_ref8:
2297 case DW_FORM_data8:
2298 case DW_FORM_ref_sig8:
2299 return read_64 (ptr);
2300 case DW_FORM_sdata:
2301 return read_sleb128 (ptr);
2302 case DW_FORM_ref_udata:
2303 case DW_FORM_udata:
2304 return read_uleb128 (ptr);
2305 case DW_FORM_implicit_const:
2306 return *(uint64_t *)ptr; /* See get_AT. */
2307 default:
2308 *present = false;
2309 return 0;
2310 }
2311 }
2312
2313 /* Return a pointer to string attribute AT in DIE, or NULL
2314 if the attribute is not present. */
2315 static char *
get_AT_string(dw_die_ref die,enum dwarf_attribute at)2316 get_AT_string (dw_die_ref die, enum dwarf_attribute at)
2317 {
2318 enum dwarf_form form;
2319 unsigned char *ptr;
2320 ptr = get_AT (die, at, &form);
2321 if (ptr == NULL)
2322 return NULL;
2323 switch (form)
2324 {
2325 case DW_FORM_string:
2326 return (char *) ptr;
2327 case DW_FORM_strp:
2328 {
2329 unsigned int strp = read_32 (ptr);
2330 if (unlikely (fi_multifile) && die_cu (die)->cu_kind == CU_ALT)
2331 {
2332 if (strp >= alt_size[DEBUG_STR])
2333 return NULL;
2334 return (char *) alt_data[DEBUG_STR] + strp;
2335 }
2336 if (strp >= debug_sections[DEBUG_STR].size)
2337 return NULL;
2338 return (char *) debug_sections[DEBUG_STR].data + strp;
2339 }
2340 case DW_FORM_line_strp:
2341 {
2342 unsigned int line_strp = read_32 (ptr);
2343 if (line_strp >= debug_sections[DEBUG_LINE_STR].size)
2344 return NULL;
2345 else
2346 return (char *) debug_sections[DEBUG_LINE_STR].data + line_strp;
2347 }
2348 default:
2349 return NULL;
2350 }
2351 }
2352
2353 /* Parse DWARF expression referenced or stored in DIE, starting at
2354 PTR with LEN bytes. Return non-zero on error. If NEED_ADJUST
2355 is non-NULL, set *NEED_ADJUST to true if it contains DIE references
2356 that will need adjusting. Some opcodes cause DIE or referenced
2357 DIEs as unsuitable for moving into partial units, or limit their
2358 location. */
2359 static int
read_exprloc(DSO * dso,dw_die_ref die,unsigned char * ptr,size_t len,bool * need_adjust)2360 read_exprloc (DSO *dso, dw_die_ref die, unsigned char *ptr, size_t len,
2361 bool *need_adjust)
2362 {
2363 unsigned char *end = ptr + len;
2364 unsigned char op;
2365 GElf_Addr addr;
2366 dw_die_ref ref;
2367 dw_cu_ref cu;
2368
2369 while (ptr < end)
2370 {
2371 op = *ptr++;
2372 switch (op)
2373 {
2374 case DW_OP_addr:
2375 die->die_no_multifile = 1;
2376 ptr += ptr_size;
2377 break;
2378 case DW_OP_deref:
2379 case DW_OP_dup:
2380 case DW_OP_drop:
2381 case DW_OP_over:
2382 case DW_OP_swap:
2383 case DW_OP_rot:
2384 case DW_OP_xderef:
2385 case DW_OP_abs:
2386 case DW_OP_and:
2387 case DW_OP_div:
2388 case DW_OP_minus:
2389 case DW_OP_mod:
2390 case DW_OP_mul:
2391 case DW_OP_neg:
2392 case DW_OP_not:
2393 case DW_OP_or:
2394 case DW_OP_plus:
2395 case DW_OP_shl:
2396 case DW_OP_shr:
2397 case DW_OP_shra:
2398 case DW_OP_xor:
2399 case DW_OP_eq:
2400 case DW_OP_ge:
2401 case DW_OP_gt:
2402 case DW_OP_le:
2403 case DW_OP_lt:
2404 case DW_OP_ne:
2405 case DW_OP_lit0 ... DW_OP_lit31:
2406 case DW_OP_reg0 ... DW_OP_reg31:
2407 case DW_OP_nop:
2408 case DW_OP_push_object_address:
2409 case DW_OP_form_tls_address:
2410 case DW_OP_call_frame_cfa:
2411 case DW_OP_stack_value:
2412 case DW_OP_GNU_push_tls_address:
2413 case DW_OP_GNU_uninit:
2414 break;
2415 case DW_OP_const1u:
2416 case DW_OP_pick:
2417 case DW_OP_deref_size:
2418 case DW_OP_xderef_size:
2419 case DW_OP_const1s:
2420 ++ptr;
2421 break;
2422 case DW_OP_const2u:
2423 case DW_OP_const2s:
2424 case DW_OP_skip:
2425 case DW_OP_bra:
2426 ptr += 2;
2427 break;
2428 case DW_OP_call2:
2429 case DW_OP_call4:
2430 case DW_OP_GNU_parameter_ref:
2431 if (op == DW_OP_call2)
2432 addr = read_16 (ptr);
2433 else
2434 addr = read_32 (ptr);
2435 cu = die_cu (die);
2436 ref = off_htab_lookup (cu, cu->cu_offset + addr);
2437 if (ref == NULL)
2438 {
2439 error (0, 0, "%s: Couldn't find DIE at [%" PRIx64 "] "
2440 "referenced by %s from DIE at [%x]",
2441 dso->filename, cu->cu_offset + addr,
2442 get_DW_OP_str (op), die->die_offset);
2443 return 1;
2444 }
2445 if (op == DW_OP_call2)
2446 ref->die_op_call2_referenced = 1;
2447 if (ref->die_ck_state == CK_KNOWN)
2448 {
2449 dw_die_ref d;
2450 ref->die_ck_state = CK_BAD;
2451
2452 d = ref;
2453 while (!d->die_root
2454 && d->die_parent->die_ck_state == CK_KNOWN)
2455 {
2456 d = d->die_parent;
2457 d->die_ck_state = CK_BAD;
2458 }
2459 }
2460 else
2461 ref->die_ck_state = CK_BAD;
2462 if (unlikely (low_mem))
2463 {
2464 ref->die_referenced = 1;
2465 /* As .debug_loc adjustment is done after
2466 write_info finishes, we need to keep the referenced
2467 DIEs around uncollapsed. */
2468 if (need_adjust)
2469 ref->die_intercu_referenced = 1;
2470 }
2471 die->die_ck_state = CK_BAD;
2472 if (need_adjust)
2473 *need_adjust = true;
2474 break;
2475 case DW_OP_const4u:
2476 case DW_OP_const4s:
2477 ptr += 4;
2478 break;
2479 case DW_OP_call_ref:
2480 case DW_OP_GNU_implicit_pointer:
2481 case DW_OP_implicit_pointer:
2482 case DW_OP_GNU_variable_value:
2483 cu = die_cu (die);
2484 addr = read_size (ptr, cu->cu_version == 2 ? ptr_size : 4);
2485 if (cu->cu_version == 2)
2486 ptr += ptr_size;
2487 else
2488 ptr += 4;
2489 ref = off_htab_lookup (NULL, addr);
2490 if (ref == NULL || (unlikely (low_mem) && ref->die_tag == 0))
2491 {
2492 error (0, 0, "%s: Couldn't find DIE at [%" PRIx64 "] "
2493 "referenced by %s from DIE at [%x]",
2494 dso->filename, addr, get_DW_OP_str (op), die->die_offset);
2495 return 1;
2496 }
2497 ref->die_no_multifile = 1;
2498 if (unlikely (low_mem))
2499 {
2500 ref->die_referenced = 1;
2501 /* As .debug_loc adjustment is done after
2502 write_info finishes, we need to keep the referenced
2503 DIEs around uncollapsed. */
2504 if (die_cu (ref) != cu || need_adjust)
2505 ref->die_intercu_referenced = 1;
2506 }
2507 die->die_ck_state = CK_BAD;
2508 if (need_adjust)
2509 *need_adjust = true;
2510 if (op == DW_OP_GNU_implicit_pointer || op == DW_OP_implicit_pointer)
2511 skip_leb128 (ptr);
2512 break;
2513 case DW_OP_const8u:
2514 case DW_OP_const8s:
2515 ptr += 8;
2516 break;
2517 case DW_OP_constu:
2518 case DW_OP_plus_uconst:
2519 case DW_OP_regx:
2520 case DW_OP_piece:
2521 case DW_OP_consts:
2522 case DW_OP_breg0 ... DW_OP_breg31:
2523 case DW_OP_fbreg:
2524 skip_leb128 (ptr);
2525 break;
2526 case DW_OP_bregx:
2527 case DW_OP_bit_piece:
2528 skip_leb128 (ptr);
2529 skip_leb128 (ptr);
2530 break;
2531 case DW_OP_implicit_value:
2532 {
2533 uint32_t leni = read_uleb128 (ptr);
2534 ptr += leni;
2535 }
2536 break;
2537 case DW_OP_GNU_entry_value:
2538 case DW_OP_entry_value:
2539 {
2540 uint32_t leni = read_uleb128 (ptr);
2541 if ((uint64_t) (end - ptr) < leni)
2542 {
2543 error (0, 0, "%s: %s with too large length",
2544 get_DW_OP_str (op), dso->filename);
2545 return 1;
2546 }
2547 if (read_exprloc (dso, die, ptr, leni, need_adjust))
2548 return 1;
2549 ptr += leni;
2550 }
2551 break;
2552 case DW_OP_GNU_convert:
2553 case DW_OP_convert:
2554 case DW_OP_GNU_reinterpret:
2555 case DW_OP_reinterpret:
2556 addr = read_uleb128 (ptr);
2557 if (addr == 0)
2558 break;
2559 goto typed_dwarf;
2560 case DW_OP_GNU_regval_type:
2561 case DW_OP_regval_type:
2562 skip_leb128 (ptr);
2563 addr = read_uleb128 (ptr);
2564 goto typed_dwarf;
2565 case DW_OP_GNU_const_type:
2566 case DW_OP_const_type:
2567 addr = read_uleb128 (ptr);
2568 ptr += *ptr + 1;
2569 goto typed_dwarf;
2570 case DW_OP_GNU_deref_type:
2571 case DW_OP_deref_type:
2572 ++ptr;
2573 addr = read_uleb128 (ptr);
2574 typed_dwarf:
2575 cu = die_cu (die);
2576 ref = off_htab_lookup (cu, cu->cu_offset + addr);
2577 if (ref == NULL)
2578 {
2579 error (0, 0, "%s: Couldn't find DIE at [%" PRIx64 "] "
2580 "referenced by %s from DIE at [%x]",
2581 dso->filename, cu->cu_offset + addr,
2582 get_DW_OP_str (op), die->die_offset);
2583 return 1;
2584 }
2585 if (unlikely (low_mem))
2586 {
2587 ref->die_referenced = 1;
2588 /* As .debug_loc adjustment is done after
2589 write_info finishes, we need to keep the referenced
2590 DIEs around uncollapsed. */
2591 if (need_adjust)
2592 ref->die_intercu_referenced = 1;
2593 }
2594 ref->die_op_type_referenced = 1;
2595 die->die_ck_state = CK_BAD;
2596 if (need_adjust)
2597 *need_adjust = true;
2598 break;
2599 default:
2600 error (0, 0, "%s: Unknown DWARF %s",
2601 dso->filename, get_DW_OP_str (op));
2602 return 1;
2603 }
2604 }
2605 if (die->die_ck_state != CK_BAD)
2606 die->u.p1.die_hash = iterative_hash (end - len, len, die->u.p1.die_hash);
2607 return 0;
2608 }
2609
2610 /* Add dummy die in CU at OFFSET. */
2611 static inline void FORCE_INLINE
add_dummy_die(dw_cu_ref cu,unsigned int offset)2612 add_dummy_die (dw_cu_ref cu, unsigned int offset)
2613 {
2614 dw_die_ref ref;
2615 struct dw_die ref_buf;
2616 void **slot;
2617
2618 memset (&ref_buf, '\0', offsetof (struct dw_die, die_child));
2619 ref_buf.die_offset = offset;
2620 ref_buf.die_collapsed_child = 1;
2621 ref_buf.die_referenced = 1;
2622 ref_buf.die_intercu_referenced = 1;
2623 if (off_htab == NULL)
2624 {
2625 ref = pool_alloc (dw_die, offsetof (struct dw_die, die_child));
2626 memcpy (ref, &ref_buf, offsetof (struct dw_die, die_child));
2627 off_htab_add_die (cu, ref, NULL);
2628 return;
2629 }
2630
2631 slot
2632 = htab_find_slot_with_hash (off_htab, &ref_buf, off_hash (&ref_buf),
2633 INSERT);
2634 if (slot == NULL)
2635 dwz_oom ();
2636 if (*slot != NULL)
2637 return;
2638
2639 ref = pool_alloc (dw_die, offsetof (struct dw_die, die_child));
2640 memcpy (ref, &ref_buf, offsetof (struct dw_die, die_child));
2641 *slot = (void *) ref;
2642 }
2643
2644 /* Add dummy DIEs for expr_loc at PTR. */
2645 static int
read_exprloc_low_mem_phase1(DSO * dso,dw_die_ref die,unsigned char * ptr,size_t len)2646 read_exprloc_low_mem_phase1 (DSO *dso, dw_die_ref die, unsigned char *ptr,
2647 size_t len)
2648 {
2649 unsigned char *end = ptr + len;
2650 unsigned char op;
2651 GElf_Addr addr;
2652 dw_cu_ref cu;
2653
2654 while (ptr < end)
2655 {
2656 op = *ptr++;
2657 switch (op)
2658 {
2659 case DW_OP_addr:
2660 ptr += ptr_size;
2661 break;
2662 case DW_OP_deref:
2663 case DW_OP_dup:
2664 case DW_OP_drop:
2665 case DW_OP_over:
2666 case DW_OP_swap:
2667 case DW_OP_rot:
2668 case DW_OP_xderef:
2669 case DW_OP_abs:
2670 case DW_OP_and:
2671 case DW_OP_div:
2672 case DW_OP_minus:
2673 case DW_OP_mod:
2674 case DW_OP_mul:
2675 case DW_OP_neg:
2676 case DW_OP_not:
2677 case DW_OP_or:
2678 case DW_OP_plus:
2679 case DW_OP_shl:
2680 case DW_OP_shr:
2681 case DW_OP_shra:
2682 case DW_OP_xor:
2683 case DW_OP_eq:
2684 case DW_OP_ge:
2685 case DW_OP_gt:
2686 case DW_OP_le:
2687 case DW_OP_lt:
2688 case DW_OP_ne:
2689 case DW_OP_lit0 ... DW_OP_lit31:
2690 case DW_OP_reg0 ... DW_OP_reg31:
2691 case DW_OP_nop:
2692 case DW_OP_push_object_address:
2693 case DW_OP_form_tls_address:
2694 case DW_OP_call_frame_cfa:
2695 case DW_OP_stack_value:
2696 case DW_OP_GNU_push_tls_address:
2697 case DW_OP_GNU_uninit:
2698 break;
2699 case DW_OP_const1u:
2700 case DW_OP_pick:
2701 case DW_OP_deref_size:
2702 case DW_OP_xderef_size:
2703 case DW_OP_const1s:
2704 ++ptr;
2705 break;
2706 case DW_OP_const2u:
2707 case DW_OP_const2s:
2708 case DW_OP_skip:
2709 case DW_OP_bra:
2710 ptr += 2;
2711 break;
2712 case DW_OP_call2:
2713 case DW_OP_call4:
2714 case DW_OP_GNU_parameter_ref:
2715 if (op == DW_OP_call2)
2716 read_16 (ptr);
2717 else
2718 read_32 (ptr);
2719 break;
2720 case DW_OP_const4u:
2721 case DW_OP_const4s:
2722 ptr += 4;
2723 break;
2724 case DW_OP_call_ref:
2725 case DW_OP_GNU_implicit_pointer:
2726 case DW_OP_implicit_pointer:
2727 case DW_OP_GNU_variable_value:
2728 cu = die_cu (die);
2729 addr = read_size (ptr, cu->cu_version == 2 ? ptr_size : 4);
2730 if (cu->cu_version == 2)
2731 ptr += ptr_size;
2732 else
2733 ptr += 4;
2734 /* Adding a dummy DIE ref to mark an intercu reference is only
2735 necessary if die_cu (ref) != cu, but we don't track cu's during
2736 low-mem phase1. */
2737 add_dummy_die (cu, addr);
2738 if (op == DW_OP_GNU_implicit_pointer || op == DW_OP_implicit_pointer)
2739 skip_leb128 (ptr);
2740 break;
2741 case DW_OP_const8u:
2742 case DW_OP_const8s:
2743 ptr += 8;
2744 break;
2745 case DW_OP_constu:
2746 case DW_OP_plus_uconst:
2747 case DW_OP_regx:
2748 case DW_OP_piece:
2749 case DW_OP_consts:
2750 case DW_OP_breg0 ... DW_OP_breg31:
2751 case DW_OP_fbreg:
2752 skip_leb128 (ptr);
2753 break;
2754 case DW_OP_bregx:
2755 case DW_OP_bit_piece:
2756 skip_leb128 (ptr);
2757 skip_leb128 (ptr);
2758 break;
2759 case DW_OP_implicit_value:
2760 {
2761 uint32_t leni = read_uleb128 (ptr);
2762 ptr += leni;
2763 }
2764 break;
2765 case DW_OP_GNU_entry_value:
2766 case DW_OP_entry_value:
2767 {
2768 uint32_t leni = read_uleb128 (ptr);
2769 if ((uint64_t) (end - ptr) < leni)
2770 {
2771 error (0, 0, "%s: %s with too large length",
2772 get_DW_OP_str (op), dso->filename);
2773 return 1;
2774 }
2775 if (read_exprloc_low_mem_phase1 (dso, die, ptr, leni))
2776 return 1;
2777 ptr += leni;
2778 }
2779 break;
2780 case DW_OP_GNU_convert:
2781 case DW_OP_convert:
2782 case DW_OP_GNU_reinterpret:
2783 case DW_OP_reinterpret:
2784 skip_leb128 (ptr);
2785 break;
2786 case DW_OP_GNU_regval_type:
2787 case DW_OP_regval_type:
2788 skip_leb128 (ptr);
2789 skip_leb128 (ptr);
2790 break;
2791 case DW_OP_GNU_const_type:
2792 case DW_OP_const_type:
2793 read_uleb128 (ptr);
2794 ptr += *ptr + 1;
2795 break;
2796 case DW_OP_GNU_deref_type:
2797 case DW_OP_deref_type:
2798 ++ptr;
2799 skip_leb128 (ptr);
2800 break;
2801 default:
2802 error (0, 0, "%s: Unknown DWARF %s",
2803 dso->filename, get_DW_OP_str (op));
2804 return 1;
2805 }
2806 }
2807
2808 return 0;
2809 }
2810
2811 /* Add dummy DIEs for loclist at OFFSET. */
2812 static int
read_loclist_low_mem_phase1(DSO * dso,dw_cu_ref cu,dw_die_ref die,GElf_Addr offset)2813 read_loclist_low_mem_phase1 (DSO *dso, dw_cu_ref cu, dw_die_ref die,
2814 GElf_Addr offset)
2815 {
2816 unsigned char *ptr, *endsec;
2817 GElf_Addr low, high;
2818 size_t len = 0;
2819 int sec;
2820
2821 sec = cu->cu_version < 5 ? DEBUG_LOC : DEBUG_LOCLISTS;
2822 ptr = debug_sections[sec].data;
2823 if (ptr == NULL)
2824 {
2825 error (0, 0, "%s: loclistptr attribute, yet no %s section",
2826 dso->filename, debug_sections[sec].name);
2827 return 1;
2828 }
2829 if (offset >= debug_sections[sec].size)
2830 {
2831 error (0, 0,
2832 "%s: loclistptr offset %Ld outside of %s section",
2833 dso->filename, (long long) offset, debug_sections[sec].name);
2834 return 1;
2835 }
2836 endsec = ptr + debug_sections[sec].size;
2837 ptr += offset;
2838 again:
2839 while (ptr < endsec)
2840 {
2841 if (sec == DEBUG_LOC)
2842 {
2843 low = read_size (ptr, ptr_size);
2844 high = read_size (ptr + ptr_size, ptr_size);
2845 ptr += 2 * ptr_size;
2846 if (low == 0 && high == 0)
2847 break;
2848
2849 if (low == ~ (GElf_Addr) 0 || (ptr_size == 4 && low == 0xffffffff))
2850 continue;
2851
2852 len = read_16 (ptr);
2853 }
2854 else
2855 {
2856 uint8_t lle = *ptr++;
2857 switch (lle)
2858 {
2859 case DW_LLE_end_of_list:
2860 goto done;
2861
2862 case DW_LLE_base_addressx:
2863 skip_leb128 (ptr);
2864 goto again;
2865
2866 case DW_LLE_startx_endx:
2867 skip_leb128 (ptr);
2868 skip_leb128 (ptr);
2869 len = read_uleb128 (ptr);
2870 break;
2871
2872 case DW_LLE_startx_length:
2873 skip_leb128 (ptr);
2874 skip_leb128 (ptr);
2875 len = read_uleb128 (ptr);
2876 break;
2877
2878 case DW_LLE_offset_pair:
2879 skip_leb128 (ptr);
2880 skip_leb128 (ptr);
2881 len = read_uleb128 (ptr);
2882 break;
2883
2884 case DW_LLE_default_location:
2885 len = read_uleb128 (ptr);
2886 break;
2887
2888 case DW_LLE_base_address:
2889 ptr += ptr_size;
2890 goto again;
2891
2892 case DW_LLE_start_end:
2893 ptr += 2 * ptr_size;
2894 len = read_uleb128 (ptr);
2895 break;
2896
2897 case DW_LLE_start_length:
2898 ptr += ptr_size;
2899 skip_leb128 (ptr);
2900 len = read_uleb128 (ptr);
2901 break;
2902
2903 case DW_LLE_GNU_view_pair:
2904 if (cu->cu_version != 5)
2905 error (0, 0,
2906 "%s: DW_LLE_GNU_view_pair used with DWARF version %u",
2907 dso->filename, cu->cu_version);
2908 skip_leb128 (ptr);
2909 skip_leb128 (ptr);
2910 goto again;
2911
2912 default:
2913 error (0, 0,
2914 "%s: unhandled location list entry 0x%x in %s section",
2915 dso->filename, lle, debug_sections[sec].name);
2916 return 1;
2917 }
2918 }
2919
2920 if (unlikely (!(ptr + len <= endsec)))
2921 {
2922 error (0, 0,
2923 "%s: locexpr length 0x%Lx exceeds %s section",
2924 dso->filename, (long long) len, debug_sections[sec].name);
2925 return 1;
2926 }
2927
2928 if (len > 0)
2929 if (read_exprloc_low_mem_phase1 (dso, die, ptr, len))
2930 return 1;
2931
2932 ptr += len;
2933 }
2934
2935 done:
2936 return 0;
2937 }
2938
2939 /* Add dummy dies for loc_exprs and loc_lists referenced from DIE. */
2940 static int
add_locexpr_dummy_dies(DSO * dso,dw_cu_ref cu,dw_die_ref die,unsigned char * ptr,uint32_t form,unsigned int attr,size_t len)2941 add_locexpr_dummy_dies (DSO *dso, dw_cu_ref cu, dw_die_ref die,
2942 unsigned char *ptr, uint32_t form, unsigned int attr,
2943 size_t len)
2944 {
2945 if (form == DW_FORM_block1 && cu->cu_version < 4)
2946 {
2947 /* Old DWARF uses blocks instead of exprlocs. */
2948 switch (attr)
2949 {
2950 case DW_AT_frame_base:
2951 case DW_AT_location:
2952 case DW_AT_data_member_location:
2953 case DW_AT_vtable_elem_location:
2954 case DW_AT_byte_size:
2955 case DW_AT_bit_offset:
2956 case DW_AT_bit_size:
2957 case DW_AT_string_length:
2958 case DW_AT_lower_bound:
2959 case DW_AT_return_addr:
2960 case DW_AT_bit_stride:
2961 case DW_AT_upper_bound:
2962 case DW_AT_count:
2963 case DW_AT_segment:
2964 case DW_AT_static_link:
2965 case DW_AT_use_location:
2966 case DW_AT_allocated:
2967 case DW_AT_associated:
2968 case DW_AT_data_location:
2969 case DW_AT_byte_stride:
2970 case DW_AT_rank:
2971 case DW_AT_call_value:
2972 case DW_AT_call_target:
2973 case DW_AT_call_target_clobbered:
2974 case DW_AT_call_data_location:
2975 case DW_AT_call_data_value:
2976 case DW_AT_GNU_call_site_value:
2977 case DW_AT_GNU_call_site_data_value:
2978 case DW_AT_GNU_call_site_target:
2979 case DW_AT_GNU_call_site_target_clobbered:
2980 if (read_exprloc_low_mem_phase1 (dso, die, ptr, len))
2981 return 1;
2982 default:
2983 break;
2984 }
2985
2986 return 0;
2987 }
2988
2989 if (form == DW_FORM_exprloc)
2990 return read_exprloc_low_mem_phase1 (dso, die, ptr, len);
2991
2992 switch (attr)
2993 {
2994 case DW_AT_location:
2995 case DW_AT_string_length:
2996 case DW_AT_return_addr:
2997 case DW_AT_data_member_location:
2998 case DW_AT_frame_base:
2999 case DW_AT_segment:
3000 case DW_AT_static_link:
3001 case DW_AT_use_location:
3002 case DW_AT_vtable_elem_location:
3003 if ((cu->cu_version < 4 && form == DW_FORM_data4)
3004 || form == DW_FORM_sec_offset)
3005 {
3006 if (read_loclist_low_mem_phase1 (dso, cu, die, do_read_32 (ptr)))
3007 return 1;
3008 break;
3009 }
3010 else if (cu->cu_version < 4 && form == DW_FORM_data8)
3011 {
3012 if (read_loclist_low_mem_phase1 (dso, cu, die, do_read_64 (ptr)))
3013 return 1;
3014 break;
3015 }
3016 break;
3017 default:
3018 break;
3019 }
3020
3021 return 0;
3022 }
3023
3024 /* Structure recording a portion of .debug_loc section that will need
3025 adjusting. */
3026 struct debug_loc_adjust
3027 {
3028 /* Starting offset in .debug_loc that needs adjusting. */
3029 unsigned int start_offset;
3030 /* End offset. This is used for hashing, as in theory some DIE
3031 might be referencing a middle of a .debug_loc sequence (the address
3032 part of it) referenced by a different DIE. */
3033 unsigned int end_offset;
3034 /* Owning CU. We give up if the same .debug_loc part that needs adjusting
3035 is owned by more than one CU. */
3036 dw_cu_ref cu;
3037 };
3038 ALIGN_STRUCT (debug_loc_adjust)
3039
3040 /* Hash table and obstack for recording .debug_loc and .debug_loclists
3041 adjustment ranges. */
3042 static htab_t loc_htab;
3043 static htab_t loclists_htab;
3044
3045 /* Hash function for loc[lists]_htab. */
3046 static hashval_t
loc_hash(const void * p)3047 loc_hash (const void *p)
3048 {
3049 struct debug_loc_adjust *a = (struct debug_loc_adjust *)p;
3050
3051 return a->end_offset;
3052 }
3053
3054 /* Equality function for loc[lists]_htab. */
3055 static int
loc_eq(const void * p,const void * q)3056 loc_eq (const void *p, const void *q)
3057 {
3058 struct debug_loc_adjust *t1 = (struct debug_loc_adjust *)p;
3059 struct debug_loc_adjust *t2 = (struct debug_loc_adjust *)q;
3060
3061 return t1->end_offset == t2->end_offset;
3062 }
3063
3064 /* Parse .debug_loc portion starting at OFFSET, referenced by
3065 DIE. Call read_exprloc on each of the DWARF expressions
3066 contained in it. */
3067 static int
read_loclist(DSO * dso,dw_cu_ref cu,dw_die_ref die,GElf_Addr offset)3068 read_loclist (DSO *dso, dw_cu_ref cu, dw_die_ref die, GElf_Addr offset)
3069 {
3070 unsigned char *ptr, *endsec;
3071 GElf_Addr low, high;
3072 size_t len;
3073 int sec;
3074 bool need_adjust = false;
3075
3076 die->die_ck_state = CK_BAD;
3077 sec = cu->cu_version < 5 ? DEBUG_LOC : DEBUG_LOCLISTS;
3078 ptr = debug_sections[sec].data;
3079 if (ptr == NULL)
3080 {
3081 error (0, 0, "%s: loclistptr attribute, yet no %s section",
3082 dso->filename, debug_sections[sec].name);
3083 return 1;
3084 }
3085 if (offset >= debug_sections[sec].size)
3086 {
3087 error (0, 0,
3088 "%s: loclistptr offset %Ld outside of %s section",
3089 dso->filename, (long long) offset, debug_sections[sec].name);
3090 return 1;
3091 }
3092 endsec = ptr + debug_sections[sec].size;
3093 ptr += offset;
3094 again:
3095 while (ptr < endsec)
3096 {
3097 if (cu->cu_version < 5)
3098 {
3099 low = read_size (ptr, ptr_size);
3100 high = read_size (ptr + ptr_size, ptr_size);
3101 ptr += 2 * ptr_size;
3102 if (low == 0 && high == 0)
3103 break;
3104
3105 if (low == ~ (GElf_Addr) 0 || (ptr_size == 4 && low == 0xffffffff))
3106 continue;
3107
3108 len = read_16 (ptr);
3109 }
3110 else
3111 {
3112 uint8_t lle = *ptr++;
3113 switch (lle)
3114 {
3115 case DW_LLE_end_of_list:
3116 goto done;
3117
3118 case DW_LLE_base_addressx:
3119 skip_leb128 (ptr);
3120 goto again;
3121
3122 case DW_LLE_startx_endx:
3123 skip_leb128 (ptr);
3124 skip_leb128 (ptr);
3125 len = read_uleb128 (ptr);
3126 break;
3127
3128 case DW_LLE_startx_length:
3129 skip_leb128 (ptr);
3130 skip_leb128 (ptr);
3131 len = read_uleb128 (ptr);
3132 break;
3133
3134 case DW_LLE_offset_pair:
3135 skip_leb128 (ptr);
3136 skip_leb128 (ptr);
3137 len = read_uleb128 (ptr);
3138 break;
3139
3140 case DW_LLE_default_location:
3141 len = read_uleb128 (ptr);
3142 break;
3143
3144 case DW_LLE_base_address:
3145 ptr += ptr_size;
3146 goto again;
3147
3148 case DW_LLE_start_end:
3149 ptr += 2 * ptr_size;
3150 len = read_uleb128 (ptr);
3151 break;
3152
3153 case DW_LLE_start_length:
3154 ptr += ptr_size;
3155 skip_leb128 (ptr);
3156 len = read_uleb128 (ptr);
3157 break;
3158
3159 case DW_LLE_GNU_view_pair:
3160 if (cu->cu_version != 5)
3161 error (0, 0,
3162 "%s: DW_LLE_GNU_view_pair used with DWARF version %u",
3163 dso->filename, cu->cu_version);
3164 skip_leb128 (ptr);
3165 skip_leb128 (ptr);
3166 goto again;
3167
3168 default:
3169 error (0, 0,
3170 "%s: unhandled location list entry 0x%x in %s section",
3171 dso->filename, lle, debug_sections[sec].name);
3172 return 1;
3173 }
3174 }
3175
3176 if (unlikely (!(ptr + len <= endsec)))
3177 {
3178 error (0, 0,
3179 "%s: locexpr length 0x%Lx exceeds %s section",
3180 dso->filename, (long long) len, debug_sections[sec].name);
3181 return 1;
3182 }
3183
3184 if (read_exprloc (dso, die, ptr, len, &need_adjust))
3185 return 1;
3186
3187 ptr += len;
3188 }
3189
3190 done:
3191 if (need_adjust)
3192 {
3193 struct debug_loc_adjust adj, *a;
3194 void **slot;
3195
3196 adj.start_offset = offset;
3197 adj.end_offset = ptr - debug_sections[sec].data;
3198 adj.cu = cu;
3199 if (sec == DEBUG_LOC)
3200 {
3201 if (loc_htab == NULL)
3202 {
3203 loc_htab = htab_try_create (50, loc_hash, loc_eq, NULL);
3204 if (loc_htab == NULL)
3205 dwz_oom ();
3206 }
3207 slot = htab_find_slot_with_hash (loc_htab, &adj, adj.end_offset,
3208 INSERT);
3209 }
3210 else
3211 {
3212 if (loclists_htab == NULL)
3213 {
3214 loclists_htab = htab_try_create (50, loc_hash, loc_eq, NULL);
3215 if (loclists_htab == NULL)
3216 dwz_oom ();
3217 }
3218 slot = htab_find_slot_with_hash (loclists_htab, &adj, adj.end_offset,
3219 INSERT);
3220 }
3221 if (slot == NULL)
3222 dwz_oom ();
3223 if (*slot == NULL)
3224 {
3225 a = pool_alloc (debug_loc_adjust, sizeof (*a));
3226 *a = adj;
3227 *slot = (void *) a;
3228 }
3229 else if (((struct debug_loc_adjust *)*slot)->cu != adj.cu)
3230 {
3231 error (0, 0, "%s: can't adjust %s section because multiple "
3232 "CUs refer to it", dso->filename, debug_sections[sec].name);
3233 return 1;
3234 }
3235 else if (((struct debug_loc_adjust *)*slot)->start_offset > offset)
3236 ((struct debug_loc_adjust *)*slot)->start_offset = offset;
3237 }
3238
3239 return 0;
3240 }
3241
3242 /* Initialize die_odr_state field for DIE with CU. */
3243 static void
set_die_odr_state(dw_cu_ref cu,dw_die_ref die)3244 set_die_odr_state (dw_cu_ref cu, dw_die_ref die)
3245 {
3246 unsigned char *ptr;
3247 struct abbrev_tag *t;
3248 unsigned int i;
3249 bool decl_p;
3250 bool name_p;
3251 bool other_p;
3252
3253 assert (die->die_odr_state == ODR_UNKNOWN);
3254 die->die_odr_state = ODR_NONE;
3255
3256 if (low_mem)
3257 /* Todo: allow low-mem mode. */
3258 return;
3259
3260 if (multifile_mode == 0)
3261 /* We're in regular mode, enable the ODR optimization. */
3262 ;
3263 else
3264 /* One definition rule does not hold across executables and shared
3265 libraries, so disable. */
3266 return;
3267
3268 if (!die->die_toplevel)
3269 /* A nested struct is not uniquely identified by its name. There may be a
3270 different type with the same name nested in a different struct. */
3271 return;
3272
3273 switch (cu->lang)
3274 {
3275 case DW_LANG_C_plus_plus:
3276 case DW_LANG_C_plus_plus_03:
3277 case DW_LANG_C_plus_plus_11:
3278 case DW_LANG_C_plus_plus_14:
3279 /* c++ defines one-definition-rule. */
3280 if (die->die_tag == DW_TAG_structure_type
3281 || die->die_tag == DW_TAG_class_type
3282 || die->die_tag == DW_TAG_union_type)
3283 /* ODR holds for all types, but we limit the optimization to these
3284 tags, which are the ones likely to profit from it. */
3285 ;
3286 else
3287 return;
3288 break;
3289 default:
3290 return;
3291 }
3292
3293 ptr = debug_sections[DEBUG_INFO].data + die->die_offset;
3294 skip_leb128 (ptr);
3295
3296 t = die->die_abbrev;
3297
3298 decl_p = false;
3299 name_p = false;
3300 other_p = false;
3301 for (i = 0; i < t->nattr; ++i)
3302 {
3303 if (t->attr[i].attr == DW_AT_name)
3304 {
3305 name_p = true;
3306 continue;
3307 }
3308
3309 if (t->attr[i].attr == DW_AT_declaration)
3310 {
3311 decl_p = true;
3312 continue;
3313 }
3314
3315 other_p = true;
3316 }
3317
3318 if (!name_p)
3319 /* Ignore anonymous types. */
3320 return;
3321
3322 odr_active_p = true;
3323
3324 if (decl_p && !other_p && die->die_child == NULL)
3325 {
3326 /* Detected a declaration with no attributes other than DW_AT_name and
3327 DW_AT_declaration, and no children. */
3328 die->die_odr_state = ODR_DECL;
3329 return;
3330 }
3331
3332 die->die_odr_state = ODR_DEF;
3333 }
3334
3335 /* Return the initialized die_odr_state field for DIE with CU. */
3336 static unsigned int UNUSED
die_odr_state(dw_die_ref die)3337 die_odr_state (dw_die_ref die)
3338 {
3339 assert (die->die_odr_state != ODR_UNKNOWN);
3340 return die->die_odr_state;
3341 }
3342
3343 /* This function computes u.p1.die_hash and die_ck_state of DIE.
3344 The field u.p1.die_hash is an iterative hash of:
3345 - the die_tag,
3346 - for all attributes except DW_AT_sibling: the attribute code,
3347 - for non-reference class attributes: the value of the attribute (magic
3348 for DW_AT_*_file),
3349 - for DW_FORM_ref_addr attributes: the value of the attribute,
3350 - for reference class attributes that point into the subtree of TOP_DIE
3351 (note, other references are intentionally ignored here):
3352 ref->u.p1.die_enter - top_die->u.p1.die_enter,
3353 - for all children: their hashes.
3354 The field die_ck_state is set to CK_BAD if the die is unsuitable for
3355 moving into a partial unit (contains code references or other reasons).
3356 TOP_DIE is initially NULL when DW_TAG_*_unit or die_named_namespace dies
3357 are walked. */
3358 static int
checksum_die(DSO * dso,dw_cu_ref cu,dw_die_ref top_die,dw_die_ref die)3359 checksum_die (DSO *dso, dw_cu_ref cu, dw_die_ref top_die, dw_die_ref die)
3360 {
3361 unsigned short s;
3362 struct abbrev_tag *t;
3363 unsigned int i;
3364 unsigned char *ptr;
3365 dw_die_ref child;
3366 bool only_hash_name_p;
3367 hashval_t die_hash2;
3368
3369 switch (die->die_ck_state)
3370 {
3371 case CK_UNKNOWN:
3372 break;
3373 case CK_KNOWN:
3374 case CK_BAD:
3375 return 0;
3376 case CK_BEING_COMPUTED:
3377 die->die_ck_state = CK_BAD;
3378 return 0;
3379 }
3380 die->die_ck_state = CK_BEING_COMPUTED;
3381 die->u.p1.die_hash = 0;
3382 if (die->die_tag == DW_TAG_compile_unit
3383 || die->die_tag == DW_TAG_partial_unit
3384 || die->die_tag == DW_TAG_namespace
3385 || die->die_tag == DW_TAG_module
3386 || die->die_tag == DW_TAG_imported_unit)
3387 die->die_ck_state = CK_BAD;
3388 t = die->die_abbrev;
3389 ptr = debug_sections[DEBUG_INFO].data + die->die_offset;
3390 skip_leb128 (ptr);
3391 s = die->die_tag;
3392 die->u.p1.die_hash = iterative_hash_object (s, die->u.p1.die_hash);
3393 if (dump_checksum_p)
3394 fprintf (stderr, "DIE %x, hash: %x, tag\n", die->die_offset,
3395 die->u.p1.die_hash);
3396 if (uni_lang_p && die == top_die)
3397 {
3398 die->u.p1.die_hash
3399 = iterative_hash_object (cu->lang, die->u.p1.die_hash);
3400 if (dump_checksum_p)
3401 fprintf (stderr, "DIE %x, hash: %x, lang\n", die->die_offset,
3402 die->u.p1.die_hash);
3403 }
3404
3405 if (odr && die->die_odr_state == ODR_UNKNOWN)
3406 set_die_odr_state (die_cu (die), die);
3407 only_hash_name_p = odr && die_odr_state (die) != ODR_NONE;
3408 die_hash2 = 0;
3409 if (only_hash_name_p)
3410 die_hash2 = die->u.p1.die_hash;
3411 for (i = 0; i < t->nattr; ++i)
3412 {
3413 uint32_t form = t->attr[i].form;
3414 size_t len = 0;
3415 unsigned char *old_ptr;
3416 bool handled = false;
3417 uint64_t value;
3418
3419 while (form == DW_FORM_indirect)
3420 form = read_uleb128 (ptr);
3421 old_ptr = ptr;
3422
3423 switch (t->attr[i].attr)
3424 {
3425 /* Ignore DW_AT_sibling attribute. */
3426 case DW_AT_sibling:
3427 handled = true;
3428 break;
3429 /* These attributes reference code, prevent moving
3430 DIEs with them. */
3431 case DW_AT_low_pc:
3432 case DW_AT_high_pc:
3433 case DW_AT_entry_pc:
3434 case DW_AT_ranges:
3435 case DW_AT_call_return_pc:
3436 case DW_AT_call_pc:
3437 die->die_ck_state = CK_BAD;
3438 break;
3439 case DW_AT_start_scope:
3440 if (form == DW_FORM_sec_offset)
3441 die->die_ck_state = CK_BAD;
3442 break;
3443 /* These attributes reference other sections, they
3444 can't be moved to other files easily. */
3445 case DW_AT_stmt_list:
3446 case DW_AT_macro_info:
3447 case DW_AT_macros:
3448 case DW_AT_GNU_macros:
3449 if (!die->die_root)
3450 die->die_no_multifile = 1;
3451 break;
3452 /* loclistptr attributes. */
3453 case DW_AT_location:
3454 case DW_AT_string_length:
3455 case DW_AT_return_addr:
3456 case DW_AT_data_member_location:
3457 case DW_AT_frame_base:
3458 case DW_AT_segment:
3459 case DW_AT_static_link:
3460 case DW_AT_use_location:
3461 case DW_AT_vtable_elem_location:
3462 if ((cu->cu_version < 4 && form == DW_FORM_data4)
3463 || form == DW_FORM_sec_offset)
3464 {
3465 if (read_loclist (dso, cu, die, read_32 (ptr)))
3466 return 1;
3467 ptr = old_ptr;
3468 break;
3469 }
3470 else if (cu->cu_version < 4 && form == DW_FORM_data8)
3471 {
3472 if (read_loclist (dso, cu, die, read_64 (ptr)))
3473 return 1;
3474 ptr = old_ptr;
3475 break;
3476 }
3477 break;
3478 case DW_AT_decl_file:
3479 case DW_AT_call_file:
3480 switch (form)
3481 {
3482 case DW_FORM_data1: value = read_8 (ptr); handled = true; break;
3483 case DW_FORM_data2: value = read_16 (ptr); handled = true; break;
3484 case DW_FORM_data4: value = read_32 (ptr); handled = true; break;
3485 case DW_FORM_data8: value = read_64 (ptr); handled = true; break;
3486 case DW_FORM_udata:
3487 value = read_uleb128 (ptr); handled = true; break;
3488 case DW_FORM_sdata:
3489 {
3490 int64_t svalue = read_sleb128 (ptr);
3491 if (svalue >= 0)
3492 {
3493 value = svalue;
3494 handled = true;
3495 break;
3496 }
3497 else
3498 {
3499 negative:
3500 error (0, 0, "%s: negative value %" PRId64 " for %s",
3501 dso->filename, svalue,
3502 get_DW_AT_str (t->attr[i].attr));
3503 return 1;
3504 }
3505 }
3506 case DW_FORM_implicit_const:
3507 {
3508 if (t->values[i] >= 0)
3509 {
3510 value = t->values[i];
3511 handled = true;
3512 break;
3513 }
3514 else
3515 goto negative;
3516 }
3517 default:
3518 error (0, 0, "%s: Unhandled %s for %s",
3519 dso->filename, get_DW_FORM_str (form),
3520 get_DW_AT_str (t->attr[i].attr));
3521 return 1;
3522 }
3523 if (handled)
3524 {
3525 unsigned char *new_ptr = ptr;
3526 ptr = old_ptr;
3527 if (value > cu->cu_nfiles)
3528 {
3529 error (0, 0, "%s: Invalid %s file number %d",
3530 dso->filename, get_DW_AT_str (t->attr[i].attr),
3531 (int) value);
3532 return 1;
3533 }
3534 if (value == 0)
3535 handled = false;
3536 else if (!ignore_locus && die->die_ck_state != CK_BAD)
3537 {
3538 struct dw_file *cu_file = &cu->cu_files[value - 1];
3539 size_t file_len = strlen (cu_file->file);
3540 s = t->attr[i].attr;
3541 die->u.p1.die_hash
3542 = iterative_hash_object (s, die->u.p1.die_hash);
3543 die->u.p1.die_hash
3544 = iterative_hash_object (cu_file->time,
3545 die->u.p1.die_hash);
3546 die->u.p1.die_hash
3547 = iterative_hash_object (cu_file->size,
3548 die->u.p1.die_hash);
3549 die->u.p1.die_hash
3550 = iterative_hash (cu_file->file, file_len + 1,
3551 die->u.p1.die_hash);
3552 if (cu_file->dir)
3553 die->u.p1.die_hash
3554 = iterative_hash (cu_file->dir,
3555 strlen (cu_file->dir) + 1,
3556 die->u.p1.die_hash);
3557 /* Ignore DW_AT_comp_dir for DW_AT_*_file <built-in>
3558 etc. if immediately followed by DW_AT_*_line 0. */
3559 else if (cu_file->file_angle_brackets_encapsulated_no_slash
3560 && i + 1 < t->nattr
3561 && t->attr[i + 1].attr
3562 == (t->attr[i].attr == DW_AT_decl_file
3563 ? DW_AT_decl_line : DW_AT_call_line)
3564 && t->attr[i + 1].form == DW_FORM_data1
3565 && *new_ptr == 0)
3566 break;
3567 if (cu->cu_comp_dir
3568 && (cu_file->dir ? cu_file->dir[0]
3569 : cu_file->file[0]) != '/')
3570 die->u.p1.die_hash
3571 = iterative_hash (cu->cu_comp_dir,
3572 strlen (cu->cu_comp_dir) + 1,
3573 die->u.p1.die_hash);
3574 }
3575 }
3576 break;
3577 case DW_AT_decl_line:
3578 case DW_AT_decl_column:
3579 case DW_AT_call_line:
3580 case DW_AT_call_column:
3581 if (ignore_locus)
3582 {
3583 handled = true;
3584 break;
3585 }
3586 switch (form)
3587 {
3588 case DW_FORM_data1: value = read_8 (ptr); handled = true; break;
3589 case DW_FORM_data2: value = read_16 (ptr); handled = true; break;
3590 case DW_FORM_data4: value = read_32 (ptr); handled = true; break;
3591 case DW_FORM_data8: value = read_64 (ptr); handled = true; break;
3592 case DW_FORM_udata:
3593 value = read_uleb128 (ptr); handled = true; break;
3594 case DW_FORM_sdata:
3595 {
3596 int64_t svalue = read_sleb128 (ptr);
3597 if (svalue >= 0)
3598 {
3599 value = svalue;
3600 handled = true;
3601 break;
3602 }
3603 else
3604 goto negative;
3605 }
3606 case DW_FORM_implicit_const:
3607 if (t->values[i] >= 0)
3608 {
3609 value = t->values[i];
3610 handled = true;
3611 break;
3612 }
3613 else
3614 goto negative;
3615 default:
3616 error (0, 0, "%s: Unhandled %s for %s",
3617 dso->filename, get_DW_FORM_str (form),
3618 get_DW_AT_str (t->attr[i].attr));
3619 return 1;
3620 }
3621 if (handled)
3622 {
3623 ptr = old_ptr;
3624 s = t->attr[i].attr;
3625 die->u.p1.die_hash
3626 = iterative_hash_object (s, die->u.p1.die_hash);
3627 die->u.p1.die_hash
3628 = iterative_hash_object (value, die->u.p1.die_hash);
3629 }
3630 break;
3631 default:
3632 break;
3633 }
3634
3635 switch (form)
3636 {
3637 case DW_FORM_ref_addr:
3638 if (unlikely (op_multifile || rd_multifile || fi_multifile))
3639 {
3640 dw_die_ref ref;
3641
3642 value = read_size (ptr, cu->cu_version == 2
3643 ? ptr_size : 4);
3644 ptr += cu->cu_version == 2 ? ptr_size : 4;
3645 if (die->die_ck_state != CK_BAD)
3646 {
3647 s = t->attr[i].attr;
3648 die->u.p1.die_hash
3649 = iterative_hash_object (s, die->u.p1.die_hash);
3650 }
3651 ref = off_htab_lookup (cu, value);
3652 if (ref == NULL)
3653 {
3654 error (0, 0, "%s: Couldn't find DIE at [%" PRIx64 "] "
3655 "referenced by %s from DIE at [%x]",
3656 dso->filename, value,
3657 get_DW_AT_str (t->attr[i].attr), die->die_offset);
3658 return 1;
3659 }
3660 if (unlikely (op_multifile) && ref->die_collapsed_child)
3661 ref = ref->die_parent;
3662 if (cu == die_cu (ref))
3663 {
3664 /* The reference was encoded using a section-relative
3665 encoding, while if it could have been encoded using
3666 CU-relative encoding. Typically, the latter is used,
3667 because:
3668 - it's potentially smaller, and
3669 - it doesn't require a link-time relocation. */
3670
3671 /* Assert that the multifile only contains section-relative
3672 encoding when necessary. */
3673 assert (!op_multifile && !rd_multifile);
3674
3675 if (fi_multifile)
3676 {
3677 /* It's possible that the input DWARF contains this
3678 sub-optimal reference. We currently don't optimize
3679 this during single-file optimization, so it will still
3680 be there during finalize_multifile. Bail out to handle
3681 this conservatively. */
3682 die->die_ck_state = CK_BAD;
3683 return 0;
3684 }
3685 }
3686 /* Assert that during op_multifile, die belongs to the same object
3687 as ref. */
3688 assert (!op_multifile || cu->cu_chunk == die_cu (ref)->cu_chunk);
3689 handled = true;
3690 break;
3691 }
3692 die->die_no_multifile = 1;
3693 ptr += cu->cu_version == 2 ? ptr_size : 4;
3694 break;
3695 case DW_FORM_addr:
3696 die->die_no_multifile = 1;
3697 ptr += ptr_size;
3698 break;
3699 case DW_FORM_flag_present:
3700 break;
3701 case DW_FORM_implicit_const:
3702 if (!handled && die->die_ck_state != CK_BAD)
3703 {
3704 handled = true;
3705 s = t->attr[i].attr;
3706 die->u.p1.die_hash
3707 = iterative_hash_object (s, die->u.p1.die_hash);
3708 die->u.p1.die_hash
3709 = iterative_hash_object (t->values[i], die->u.p1.die_hash);
3710 }
3711 break;
3712 case DW_FORM_flag:
3713 case DW_FORM_data1:
3714 ++ptr;
3715 break;
3716 case DW_FORM_data2:
3717 ptr += 2;
3718 break;
3719 case DW_FORM_data4:
3720 case DW_FORM_sec_offset:
3721 ptr += 4;
3722 break;
3723 case DW_FORM_data8:
3724 ptr += 8;
3725 break;
3726 case DW_FORM_data16:
3727 ptr += 16;
3728 break;
3729 case DW_FORM_ref_sig8:
3730 die->die_no_multifile = 1;
3731 ptr += 8;
3732 break;
3733 case DW_FORM_sdata:
3734 case DW_FORM_udata:
3735 skip_leb128 (ptr);
3736 break;
3737 case DW_FORM_ref_udata:
3738 case DW_FORM_ref1:
3739 case DW_FORM_ref2:
3740 case DW_FORM_ref4:
3741 case DW_FORM_ref8:
3742 switch (form)
3743 {
3744 case DW_FORM_ref_udata: value = read_uleb128 (ptr); break;
3745 case DW_FORM_ref1: value = read_8 (ptr); break;
3746 case DW_FORM_ref2: value = read_16 (ptr); break;
3747 case DW_FORM_ref4: value = read_32 (ptr); break;
3748 case DW_FORM_ref8: value = read_64 (ptr); break;
3749 default: abort ();
3750 }
3751 if (!handled)
3752 {
3753 dw_die_ref ref
3754 = off_htab_lookup (cu, cu->cu_offset + value);
3755 if (ref == NULL)
3756 {
3757 error (0, 0, "%s: Couldn't find DIE at [%" PRIx64 "] "
3758 "referenced by %s from DIE at [%x]",
3759 dso->filename, cu->cu_offset + value,
3760 get_DW_AT_str (t->attr[i].attr), die->die_offset);
3761 return 1;
3762 }
3763 if (die->die_ck_state != CK_BAD)
3764 {
3765 s = t->attr[i].attr;
3766 die->u.p1.die_hash
3767 = iterative_hash_object (s, die->u.p1.die_hash);
3768 }
3769 if (top_die
3770 && !ref->die_collapsed_child
3771 && ref->u.p1.die_enter >= top_die->u.p1.die_enter
3772 && ref->u.p1.die_exit <= top_die->u.p1.die_exit)
3773 {
3774 if (die->die_ck_state != CK_BAD)
3775 {
3776 unsigned int val
3777 = ref->u.p1.die_enter - top_die->u.p1.die_enter;
3778 die->u.p1.die_hash
3779 = iterative_hash_object (val, die->u.p1.die_hash);
3780 }
3781 }
3782 handled = true;
3783 }
3784 break;
3785 case DW_FORM_strp:
3786 if (unlikely (op_multifile || rd_multifile || fi_multifile)
3787 && die->die_ck_state != CK_BAD)
3788 {
3789 value = read_32 (ptr);
3790 if (value >= debug_sections[DEBUG_STR].size)
3791 die->die_ck_state = CK_BAD;
3792 else
3793 {
3794 unsigned char *p = debug_sections[DEBUG_STR].data + value;
3795 unsigned int l = strlen ((char *) p) + 1;
3796 s = t->attr[i].attr;
3797 die->u.p1.die_hash
3798 = iterative_hash_object (s, die->u.p1.die_hash);
3799 die->u.p1.die_hash
3800 = iterative_hash (p, l, die->u.p1.die_hash);
3801 handled = true;
3802 }
3803 }
3804 else
3805 {
3806 ptr += 4;
3807 if (only_hash_name_p && t->attr[i].attr == DW_AT_name)
3808 {
3809 s = t->attr[i].attr;
3810 die_hash2 = iterative_hash_object (s, die_hash2);
3811 die_hash2
3812 = iterative_hash (old_ptr, ptr - old_ptr, die_hash2);
3813 }
3814 }
3815 break;
3816 case DW_FORM_line_strp:
3817 /* There is no .debug_line_str in the alt file, so we cannot
3818 move this DIE unless we change the string reference.
3819 This is not that bad because DW_FORM_line_strp is often
3820 only used in the CU DIE for file name and comp_dir and we
3821 don't move the CU DIE anyway. */
3822 die->die_ck_state = CK_BAD;
3823 break;
3824 case DW_FORM_string:
3825 ptr = (unsigned char *) strchr ((char *)ptr, '\0') + 1;
3826 if (only_hash_name_p && t->attr[i].attr == DW_AT_name)
3827 {
3828 s = t->attr[i].attr;
3829 die_hash2 = iterative_hash_object (s, die_hash2);
3830 die_hash2
3831 = iterative_hash (old_ptr, ptr - old_ptr, die_hash2);
3832 }
3833 break;
3834 case DW_FORM_indirect:
3835 abort ();
3836 case DW_FORM_block1:
3837 len = *ptr++;
3838 break;
3839 case DW_FORM_block2:
3840 len = read_16 (ptr);
3841 form = DW_FORM_block1;
3842 break;
3843 case DW_FORM_block4:
3844 len = read_32 (ptr);
3845 form = DW_FORM_block1;
3846 break;
3847 case DW_FORM_block:
3848 len = read_uleb128 (ptr);
3849 form = DW_FORM_block1;
3850 break;
3851 case DW_FORM_exprloc:
3852 len = read_uleb128 (ptr);
3853 break;
3854 default:
3855 abort ();
3856 }
3857
3858 if (form == DW_FORM_block1 && cu->cu_version < 4)
3859 {
3860 /* Old DWARF uses blocks instead of exprlocs. */
3861 switch (t->attr[i].attr)
3862 {
3863 case DW_AT_frame_base:
3864 case DW_AT_location:
3865 case DW_AT_data_member_location:
3866 case DW_AT_vtable_elem_location:
3867 case DW_AT_byte_size:
3868 case DW_AT_bit_offset:
3869 case DW_AT_bit_size:
3870 case DW_AT_string_length:
3871 case DW_AT_lower_bound:
3872 case DW_AT_return_addr:
3873 case DW_AT_bit_stride:
3874 case DW_AT_upper_bound:
3875 case DW_AT_count:
3876 case DW_AT_segment:
3877 case DW_AT_static_link:
3878 case DW_AT_use_location:
3879 case DW_AT_allocated:
3880 case DW_AT_associated:
3881 case DW_AT_data_location:
3882 case DW_AT_byte_stride:
3883 case DW_AT_rank:
3884 case DW_AT_call_value:
3885 case DW_AT_call_target:
3886 case DW_AT_call_target_clobbered:
3887 case DW_AT_call_data_location:
3888 case DW_AT_call_data_value:
3889 case DW_AT_GNU_call_site_value:
3890 case DW_AT_GNU_call_site_data_value:
3891 case DW_AT_GNU_call_site_target:
3892 case DW_AT_GNU_call_site_target_clobbered:
3893 if (die->die_ck_state != CK_BAD)
3894 {
3895 s = t->attr[i].attr;
3896 die->u.p1.die_hash
3897 = iterative_hash_object (s, die->u.p1.die_hash);
3898 }
3899 if (read_exprloc (dso, die, ptr, len, NULL))
3900 return 1;
3901 handled = true;
3902 default:
3903 break;
3904 }
3905 }
3906 else if (form == DW_FORM_exprloc)
3907 {
3908 if (die->die_ck_state != CK_BAD)
3909 {
3910 s = t->attr[i].attr;
3911 die->u.p1.die_hash
3912 = iterative_hash_object (s, die->u.p1.die_hash);
3913 }
3914 if (read_exprloc (dso, die, ptr, len, NULL))
3915 return 1;
3916 handled = true;
3917 }
3918 ptr += len; /* Skip expr/blocks. */
3919 if (!handled && die->die_ck_state != CK_BAD)
3920 {
3921 s = t->attr[i].attr;
3922 die->u.p1.die_hash = iterative_hash_object (s, die->u.p1.die_hash);
3923 die->u.p1.die_hash
3924 = iterative_hash (old_ptr, ptr - old_ptr, die->u.p1.die_hash);
3925 }
3926
3927 if (dump_checksum_p)
3928 fprintf (stderr, "DIE %x, hash: %x, attr (%d)\n", die->die_offset,
3929 die->u.p1.die_hash, i);
3930 }
3931
3932 for (child = die->die_child, i = 0; child; child = child->die_sib, ++i)
3933 if (checksum_die (dso, cu,
3934 top_die ? top_die
3935 : child->die_named_namespace
3936 ? NULL : child, child))
3937 return 1;
3938 else if (die->die_ck_state != CK_BAD)
3939 {
3940 if (child->die_ck_state == CK_KNOWN)
3941 {
3942 die->u.p1.die_hash
3943 = iterative_hash_object (child->u.p1.die_hash,
3944 die->u.p1.die_hash);
3945 if (dump_checksum_p)
3946 fprintf (stderr, "DIE %x, hash: %x, child (%i)\n",
3947 die->die_offset, die->u.p1.die_hash, i);
3948 die->die_no_multifile
3949 |= child->die_no_multifile;
3950 }
3951 else
3952 die->die_ck_state = CK_BAD;
3953 }
3954 if (die->die_ck_state == CK_BEING_COMPUTED)
3955 die->die_ck_state = CK_KNOWN;
3956
3957 if (dump_checksum_p)
3958 fprintf (stderr, "DIE %x, hash: %x, final\n", die->die_offset,
3959 die->u.p1.die_hash);
3960
3961 if (only_hash_name_p)
3962 {
3963 unsigned int tmp = die->u.p1.die_hash;
3964 die->u.p1.die_hash = die_hash2;
3965 die->u.p1.die_hash2 = tmp;
3966 }
3967
3968 return 0;
3969 }
3970
3971 /* Helper function for checksum_ref_die to sort DIE pointers
3972 by increasing u.p1.die_hash. */
3973 static int
checksum_ref_die_cmp(const void * p,const void * q)3974 checksum_ref_die_cmp (const void *p, const void *q)
3975 {
3976 dw_die_ref die1 = *(dw_die_ref *)p;
3977 dw_die_ref die2 = *(dw_die_ref *)q;
3978 if (die1->u.p1.die_hash < die2->u.p1.die_hash)
3979 return -1;
3980 if (die1->u.p1.die_hash > die2->u.p1.die_hash)
3981 return 1;
3982 /* The rest is just to keep the sort stable. If there is more than
3983 one DIE with the same hash, we don't consider any of them as suitable
3984 starting point for the walk. */
3985 if (die1->die_offset < die2->die_offset)
3986 return -1;
3987 if (die1->die_offset > die2->die_offset)
3988 return 1;
3989 return 0;
3990 }
3991
3992 /* This function is the second phase of hash computation, which computes
3993 u.p1.die_ref_hash after u.p1.die_hash has been computed.
3994 u.p1.die_ref_hash is an iterative hash of the references (other than
3995 those checksummed already into u.p1.die_hash by checksum_die).
3996 u.p1.die_ref_hash is only computed for the toplevel DIEs, i.e. children
3997 of DW_TAG_*_unit or die_named_namespace DIEs. So, in the graph
3998 containing DIEs as nodes and parent<->child and referrer<->referree edges
3999 we virtually coalesce all children of toplevel DIEs into the
4000 corresponding toplevel DIE ultimate parent node. The function has 4
4001 modes of operation:
4002
4003 The first one is when TOP_DIE, SECOND_IDX and SECOND_HASH are all NULL,
4004 this is when we walk through DW_TAG_*_unit and die_named_namespace DIEs
4005 to reach their children.
4006
4007 The second mode of operation is with TOP_DIE != NULL and both SECOND_IDX
4008 and SECOND_HASH NULL. In this mode we optimistically assume there are no
4009 cycles in the graph, first hash into TOP_DIE's u.p1.die_ref_hash its
4010 u.p1.die_hash, push the TOP_DIE into a vector (in OB obstack), for each
4011 reference if the referree isn't already toplevel DIE find its
4012 (grand)*parent that is a toplevel DIE, recurse on that and if it computed
4013 the referree toplevel DIE's u.p1.die_ref_hash (i.e. no cycle),
4014 iteratively hash in the referree toplevel DIE's u.p1.die_ref_hash (and,
4015 if referree isn't toplevel, before that also its relative position in the
4016 subtree). When existing, remove the TOP_DIE from the vector and set
4017 die_ref_hash_computed to note that it is computed and doesn't have to be
4018 computed again. If there are no cycles, the return value of the function
4019 is 0. If a cycle is found, things are more complicated. We can't just
4020 not walk into DIEs we've already seen and compute u.p1.die_ref_hash for
4021 toplevel DIEs on the cycle(s), because in different CUs matching cycles
4022 might be starting computation of the hash from different nodes on the
4023 cycle (the order of children of DW_TAG_*_unit or DW_TAG_namespace is
4024 usually not significant in DWARF). So, if a cycle is found, the return
4025 value is a minimum of the die_ref_seen indexes (positions in the
4026 vector); at that point it makes no sense to further compute
4027 u.p1.die_ref_hash of the toplevel DIEs on the cycle, but for references
4028 to acyclic subgraphs we still continue computing their u.p1.die_ref_hash.
4029 For DIEs on the cycle(s) pointers to them aren't immediately removed from
4030 the vector and everything is handled only after reaching the TOP_DIE with
4031 die_ref_seen equal to the minimum vector index (i.e. the first of
4032 the DIEs on the cycle(s) we've seen). At this point in the vector
4033 starting with that index should be a list of DIEs on the cycle, and all
4034 references to (toplevel) DIEs not on that list from those DIEs should
4035 have die_ref_hash_computed already set. If the cycle has matches in
4036 different CUs, we should have the same set of u.p1.die_hash values in the
4037 list in between all those CUs, but the order might be different. At this
4038 point we try to find a DIE from which to start walk using third mode of
4039 operation of this function. We can't base that decision on e.g.
4040 die_offset, as it may be not just different between the CUs, but the
4041 matching DIEs might be in different relative orders. So we look if there
4042 is just a single DIE with lowest u.p1.die_hash value and use that in that
4043 case, (and if not, look if there is just a single DIE with second lowest
4044 u.p1.die_hash and so on up to 20th). If none is found, the more
4045 expensive 4th mode of operation is used instead.
4046
4047 The third mode of operation is when TOP_DIE, SECOND_IDX and SECOND_HASH
4048 are all non-NULL. This mode is used when the initial TOP_DIE is
4049 a uniquely chosen DIE on the cycle (same in each CU that has
4050 matching cycle). In this mode into each TOP_DIE's u.p1.die_ref_hash
4051 we hash in *SECOND_IDX (counter when in that walk the DIE has been
4052 seen first) and relative referree positions in subtree, and hash in
4053 referree toplevel u.p1.die_ref_hash into *SECOND_HASH, and finally
4054 when the walk finishes, the caller will compute the final
4055 u.p1.die_ref_hash for DIEs on the cycle(s) from their intermediate
4056 u.p1.die_ref_hash and *SECOND_HASH.
4057
4058 The last mode of operation is when TOP_DIE and SECOND_HASH
4059 are non-NULL, but SECOND_IDX is NULL. This mode is used when no
4060 suitable DIE from which to start walking the cycle has been discovered.
4061 In that case we for each DIE on the cycle walk everything that hasn't
4062 die_ref_hash_computed yet, for DIEs seen that have die_ref_hash_computed
4063 hash in their u.p1.die_ref_hash, otherwise (DIEs on the current cycle(s))
4064 hash in their u.p1.die_hash. */
4065 static unsigned int
checksum_ref_die(dw_cu_ref cu,dw_die_ref top_die,dw_die_ref die,unsigned int * second_idx,hashval_t * second_hash)4066 checksum_ref_die (dw_cu_ref cu, dw_die_ref top_die, dw_die_ref die,
4067 unsigned int *second_idx, hashval_t *second_hash)
4068 {
4069 struct abbrev_tag *t;
4070 unsigned int i, ret = 0;
4071 unsigned char *ptr;
4072 dw_die_ref child;
4073
4074 if (top_die == die)
4075 {
4076 if (die->die_ref_hash_computed)
4077 return 0;
4078 if (die->die_ck_state != CK_KNOWN)
4079 return 0;
4080 if (die->die_ref_seen)
4081 return second_hash != NULL ? 0 : die->die_ref_seen;
4082 if (second_hash != NULL)
4083 {
4084 die->die_ref_seen = 1;
4085 if (second_idx != NULL)
4086 {
4087 die->u.p1.die_ref_hash
4088 = iterative_hash_object (*second_idx, die->u.p1.die_hash);
4089 (*second_idx)++;
4090 }
4091 }
4092 else
4093 {
4094 die->die_ref_seen
4095 = obstack_object_size (&ob) / sizeof (void *) + 1;
4096 obstack_ptr_grow (&ob, die);
4097 die->u.p1.die_ref_hash = die->u.p1.die_hash;
4098 }
4099 }
4100 else
4101 assert (top_die == NULL || die->die_ck_state == CK_KNOWN);
4102 t = die->die_abbrev;
4103 for (i = 0; i < t->nattr; ++i)
4104 if (t->attr[i].attr != DW_AT_sibling)
4105 switch (t->attr[i].form)
4106 {
4107 case DW_FORM_ref_addr:
4108 if (unlikely (op_multifile || rd_multifile || fi_multifile))
4109 i = -2U;
4110 break;
4111 case DW_FORM_ref_udata:
4112 case DW_FORM_ref1:
4113 case DW_FORM_ref2:
4114 case DW_FORM_ref4:
4115 case DW_FORM_ref8:
4116 case DW_FORM_indirect:
4117 i = -2U;
4118 break;
4119 }
4120 if (i == -1U)
4121 {
4122 ptr = debug_sections[DEBUG_INFO].data + die->die_offset;
4123 skip_leb128 (ptr);
4124 for (i = 0; i < t->nattr; ++i)
4125 {
4126 uint32_t form = t->attr[i].form;
4127 size_t len = 0;
4128 uint64_t value;
4129 dw_die_ref ref, reft;
4130
4131 while (form == DW_FORM_indirect)
4132 form = read_uleb128 (ptr);
4133
4134 switch (form)
4135 {
4136 case DW_FORM_ref_addr:
4137 if (unlikely (op_multifile || rd_multifile || fi_multifile))
4138 {
4139 value = read_size (ptr, cu->cu_version == 2 ? ptr_size : 4);
4140 ptr += cu->cu_version == 2 ? ptr_size : 4;
4141 assert (t->attr[i].attr != DW_AT_sibling);
4142 if (top_die == NULL)
4143 break;
4144 ref = off_htab_lookup (cu, value);
4145 goto finish_ref;
4146 }
4147 ptr += cu->cu_version == 2 ? ptr_size : 4;
4148 break;
4149 case DW_FORM_addr:
4150 ptr += ptr_size;
4151 break;
4152 case DW_FORM_flag_present:
4153 case DW_FORM_implicit_const:
4154 break;
4155 case DW_FORM_flag:
4156 case DW_FORM_data1:
4157 ++ptr;
4158 break;
4159 case DW_FORM_data2:
4160 ptr += 2;
4161 break;
4162 case DW_FORM_data4:
4163 case DW_FORM_sec_offset:
4164 case DW_FORM_strp:
4165 case DW_FORM_line_strp:
4166 ptr += 4;
4167 break;
4168 case DW_FORM_data8:
4169 case DW_FORM_ref_sig8:
4170 ptr += 8;
4171 break;
4172 case DW_FORM_data16:
4173 ptr += 16;
4174 break;
4175 case DW_FORM_sdata:
4176 case DW_FORM_udata:
4177 skip_leb128 (ptr);
4178 break;
4179 case DW_FORM_ref_udata:
4180 case DW_FORM_ref1:
4181 case DW_FORM_ref2:
4182 case DW_FORM_ref4:
4183 case DW_FORM_ref8:
4184 switch (form)
4185 {
4186 case DW_FORM_ref_udata: value = read_uleb128 (ptr); break;
4187 case DW_FORM_ref1: value = read_8 (ptr); break;
4188 case DW_FORM_ref2: value = read_16 (ptr); break;
4189 case DW_FORM_ref4: value = read_32 (ptr); break;
4190 case DW_FORM_ref8: value = read_64 (ptr); break;
4191 default: abort ();
4192 }
4193 if (t->attr[i].attr == DW_AT_sibling || top_die == NULL)
4194 break;
4195 ref = off_htab_lookup (cu, cu->cu_offset + value);
4196 if (ref->u.p1.die_enter >= top_die->u.p1.die_enter
4197 && ref->u.p1.die_exit <= top_die->u.p1.die_exit)
4198 break;
4199 finish_ref:
4200 reft = ref;
4201 while (!reft->die_root
4202 && reft->die_parent->die_tag != DW_TAG_compile_unit
4203 && reft->die_parent->die_tag != DW_TAG_partial_unit
4204 && !reft->die_parent->die_named_namespace)
4205 reft = reft->die_parent;
4206 if (reft->die_ck_state != CK_KNOWN || reft->die_root)
4207 top_die->die_ck_state = CK_BAD;
4208 else
4209 {
4210 unsigned int r = checksum_ref_die (die_cu (reft), reft, reft,
4211 second_idx, second_hash);
4212 if (ret == 0 || (r && r < ret))
4213 ret = r;
4214 if (reft->die_ck_state != CK_KNOWN)
4215 top_die->die_ck_state = CK_BAD;
4216 else
4217 top_die->die_no_multifile |= reft->die_no_multifile;
4218 }
4219 if (top_die->die_ck_state == CK_BAD)
4220 {
4221 if (top_die != die)
4222 return ret;
4223 i = t->nattr - 1;
4224 break;
4225 }
4226 if (ret)
4227 break;
4228 if (reft != ref)
4229 {
4230 unsigned int val
4231 = ref->u.p1.die_enter - reft->u.p1.die_enter;
4232 if (second_hash != NULL && second_idx == NULL)
4233 *second_hash
4234 = iterative_hash_object (val, *second_hash);
4235 else
4236 top_die->u.p1.die_ref_hash
4237 = iterative_hash_object (val,
4238 top_die->u.p1.die_ref_hash);
4239 }
4240 if (second_hash)
4241 {
4242 if (second_idx == NULL && !reft->die_ref_hash_computed)
4243 *second_hash
4244 = iterative_hash_object (reft->u.p1.die_hash,
4245 *second_hash);
4246 else
4247 *second_hash
4248 = iterative_hash_object (reft->u.p1.die_ref_hash,
4249 *second_hash);
4250 }
4251 else
4252 top_die->u.p1.die_ref_hash
4253 = iterative_hash_object (reft->u.p1.die_ref_hash,
4254 top_die->u.p1.die_ref_hash);
4255 break;
4256 case DW_FORM_string:
4257 ptr = (unsigned char *) strchr ((char *)ptr, '\0') + 1;
4258 break;
4259 case DW_FORM_indirect:
4260 abort ();
4261 case DW_FORM_block1:
4262 len = *ptr++;
4263 break;
4264 case DW_FORM_block2:
4265 len = read_16 (ptr);
4266 form = DW_FORM_block1;
4267 break;
4268 case DW_FORM_block4:
4269 len = read_32 (ptr);
4270 form = DW_FORM_block1;
4271 break;
4272 case DW_FORM_block:
4273 case DW_FORM_exprloc:
4274 len = read_uleb128 (ptr);
4275 form = DW_FORM_block1;
4276 break;
4277 default:
4278 abort ();
4279 }
4280
4281 if (form == DW_FORM_block1)
4282 ptr += len;
4283 }
4284 }
4285
4286 if (top_die == NULL || top_die->die_ck_state != CK_BAD)
4287 {
4288 for (child = die->die_child; child; child = child->die_sib)
4289 {
4290 unsigned int r
4291 = checksum_ref_die (cu,
4292 top_die ? top_die
4293 : child->die_named_namespace
4294 ? NULL : child, child,
4295 second_idx, second_hash);
4296 if (top_die == NULL)
4297 assert (r == 0 && obstack_object_size (&ob) == 0);
4298
4299 if (ret == 0 || (r && r < ret))
4300 ret = r;
4301 if (top_die && top_die->die_ck_state == CK_BAD)
4302 break;
4303 }
4304 }
4305
4306 if (top_die == die)
4307 {
4308 if (ret == 0)
4309 {
4310 if (second_hash != NULL)
4311 return 0;
4312 die->die_ref_seen = 0;
4313 die->die_ref_hash_computed = 1;
4314 obstack_blank_fast (&ob, -(int) sizeof (void *));
4315 return 0;
4316 }
4317 assert (ret <= die->die_ref_seen);
4318 if (ret == die->die_ref_seen)
4319 {
4320 unsigned int first = die->die_ref_seen - 1;
4321 dw_die_ref *arr;
4322 unsigned int count
4323 = obstack_object_size (&ob) / sizeof (void *) - first;
4324 unsigned int idx, minidx;
4325 hashval_t ref_hash = 0;
4326 bool bad = false;
4327 bool no_multifile = false;
4328
4329 arr = (dw_die_ref *) obstack_base (&ob) + first;
4330 for (i = 0; i < count; i++)
4331 {
4332 arr[i]->die_ref_seen = 0;
4333 if (arr[i]->die_ck_state == CK_BAD)
4334 bad = true;
4335 else if (arr[i]->die_no_multifile)
4336 no_multifile = true;
4337 }
4338 if (bad)
4339 {
4340 for (i = 0; i < count; i++)
4341 arr[i]->die_ck_state = CK_BAD;
4342 obstack_blank_fast (&ob, -(int) (count * sizeof (void *)));
4343 return 0;
4344 }
4345 /* Find the DIE in the array with the smallest u.p1.die_hash. */
4346 for (i = 0, minidx = -1U, bad = true; i < count; i++)
4347 {
4348 if (no_multifile)
4349 arr[i]->die_no_multifile = 1;
4350 if (minidx == -1U
4351 || arr[i]->u.p1.die_hash < arr[minidx]->u.p1.die_hash)
4352 {
4353 minidx = i;
4354 bad = false;
4355 }
4356 else if (arr[i]->u.p1.die_hash == arr[minidx]->u.p1.die_hash)
4357 bad = true;
4358 }
4359 if (bad)
4360 {
4361 unsigned int iter, limv;
4362 /* If there is more than one smallest u.p1.die_hash,
4363 look for second (up to 6th) smallest u.p1.die_hash
4364 if there is just one of that value. */
4365 for (iter = 0; iter < 5; iter++)
4366 {
4367 limv = arr[minidx]->u.p1.die_hash;
4368 for (i = 0, minidx = -1U, bad = true; i < count; i++)
4369 if (arr[i]->u.p1.die_hash <= limv)
4370 continue;
4371 else if (minidx == -1U
4372 || arr[i]->u.p1.die_hash
4373 < arr[minidx]->u.p1.die_hash)
4374 {
4375 minidx = i;
4376 bad = false;
4377 }
4378 else if (arr[i]->u.p1.die_hash
4379 == arr[minidx]->u.p1.die_hash)
4380 bad = true;
4381 if (minidx == -1U || !bad)
4382 break;
4383 }
4384 /* If all of 1st to 6th smallest u.p1.die_hash has more than
4385 one DIE with that u.p1.die_hash, sort the array and find
4386 the smallest u.p1.die_hash that only a single DIE has. */
4387 if (minidx != -1U && iter == 5)
4388 {
4389 unsigned int j;
4390 qsort (arr, count, sizeof (void *), checksum_ref_die_cmp);
4391 for (i = 0, minidx = -1U; i < count; i = j)
4392 {
4393 if (i + 1 == count
4394 || arr[i + 1]->u.p1.die_hash
4395 != arr[i]->u.p1.die_hash)
4396 {
4397 minidx = i;
4398 break;
4399 }
4400 for (j = i + 1; j < count; j++)
4401 if (arr[j]->u.p1.die_hash != arr[i]->u.p1.die_hash)
4402 break;
4403 }
4404 }
4405 }
4406 if (minidx != -1U)
4407 {
4408 idx = 0;
4409 checksum_ref_die (die_cu (arr[minidx]), arr[minidx],
4410 arr[minidx], &idx, &ref_hash);
4411 assert (arr == (dw_die_ref *) obstack_base (&ob) + first);
4412 for (i = 0; i < count; i++)
4413 {
4414 arr[i]->u.p1.die_ref_hash
4415 = iterative_hash_object (arr[i]->u.p1.die_ref_hash,
4416 ref_hash);
4417 arr[i]->die_ref_hash_computed = 1;
4418 arr[i]->die_ref_seen = 0;
4419 }
4420 }
4421 else
4422 {
4423 /* If we get here, all u.p1.die_hash values in the arr array
4424 are used by more than one DIE. Do the more expensive
4425 computation as fallback. */
4426 for (i = 0; i < count; i++)
4427 {
4428 unsigned int j;
4429 arr[i]->u.p1.die_ref_hash = arr[i]->u.p1.die_hash;
4430 checksum_ref_die (die_cu (arr[i]), arr[i], arr[i], NULL,
4431 &arr[i]->u.p1.die_ref_hash);
4432 assert (arr == (dw_die_ref *) obstack_base (&ob) + first);
4433 for (j = 0; j < count; j++)
4434 arr[j]->die_ref_seen = 0;
4435 }
4436 for (i = 0; i < count; i++)
4437 arr[i]->die_ref_hash_computed = 1;
4438 }
4439 obstack_blank_fast (&ob, -(int) (count * sizeof (void *)));
4440 return 0;
4441 }
4442 }
4443 return ret;
4444 }
4445
4446 /* Hash function for dup_htab. u.p1.die_ref_hash should have u.p1.die_hash
4447 iteratively hashed into it already. */
4448 static hashval_t
die_hash(const void * p)4449 die_hash (const void *p)
4450 {
4451 dw_die_ref die = (dw_die_ref) p;
4452
4453 return die->u.p1.die_ref_hash;
4454 }
4455
4456 /* Freelist of !die->die_toplevel DIEs, chained through die_sib fields. */
4457 static dw_die_ref die_nontoplevel_freelist;
4458 /* Freelist of die->die_collapsed_child DIEs, chained through die_parent
4459 fields. */
4460 static dw_die_ref die_collapsed_child_freelist;
4461
4462 /* Return pointer after the attributes of a DIE from a cu with CU_VERSION
4463 which uses abbrevs T and starts at PTR. */
4464 static unsigned char *
skip_attrs_1(unsigned int cu_version,struct abbrev_tag * t,unsigned char * ptr)4465 skip_attrs_1 (unsigned int cu_version, struct abbrev_tag *t, unsigned char *ptr)
4466 {
4467 unsigned int i;
4468 for (i = 0; i < t->nattr; ++i)
4469 ptr = skip_attr (cu_version, &t->attr[i], ptr);
4470
4471 return ptr;
4472 }
4473
4474 /* Return pointer after the attributes of a DIE from CU which uses abbrevs
4475 T and starts at PTR. */
4476 static unsigned char *
skip_attrs(dw_cu_ref cu,struct abbrev_tag * t,unsigned char * ptr)4477 skip_attrs (dw_cu_ref cu, struct abbrev_tag *t, unsigned char *ptr)
4478 {
4479 return skip_attrs_1 (cu->cu_version, t, ptr);
4480 }
4481
4482 /* Expand children of TOP_DIE that have been collapsed by
4483 collapse_child. CHECKSUM is true if checksum should be
4484 computed - expansion is performed during read_debug_info
4485 when duplicates are looked for - or false, if the expansion
4486 is performed late (e.g. during compute_abbrevs or write_{info,types}. */
4487 static void
expand_child(dw_die_ref top_die,bool checksum)4488 expand_child (dw_die_ref top_die, bool checksum)
4489 {
4490 dw_cu_ref cu = die_cu (top_die);
4491 dw_die_ref *diep = &top_die->die_child;
4492 dw_die_ref parent = top_die, child;
4493 unsigned char *ptr, *base;
4494 struct abbrev_tag tag, *t;
4495 dw_die_ref die;
4496 unsigned int tick = checksum ? top_die->u.p1.die_enter + 1 : 0;
4497
4498 if (unlikely (cu->cu_kind == CU_TYPES))
4499 base = debug_sections[DEBUG_TYPES].data;
4500 else
4501 base = debug_sections[DEBUG_INFO].data;
4502 ptr = base + top_die->die_offset;
4503 if (likely (checksum))
4504 ptr += top_die->die_size;
4505 else
4506 {
4507 t = top_die->die_abbrev;
4508 skip_leb128 (ptr);
4509 ptr = skip_attrs (cu, t, ptr);
4510 }
4511
4512 while (1)
4513 {
4514 unsigned int die_offset = ptr - base;
4515 void **slot;
4516 struct dw_die diebuf;
4517 dw_die_ref collapsed;
4518
4519 tag.entry = read_uleb128 (ptr);
4520 if (tag.entry == 0)
4521 {
4522 if (parent == top_die)
4523 break;
4524 diep = &parent->die_sib;
4525 if (checksum)
4526 parent->u.p1.die_exit = tick++;
4527 parent = parent->die_parent;
4528 continue;
4529 }
4530
4531 diebuf.die_offset = die_offset;
4532 slot = htab_find_slot_with_hash (cu->cu_kind == CU_TYPES
4533 ? types_off_htab : off_htab,
4534 &diebuf, off_hash (&diebuf), NO_INSERT);
4535 if (slot == NULL)
4536 die = NULL;
4537 else
4538 die = (dw_die_ref) *slot;
4539 if (die != NULL && !die->die_collapsed_child)
4540 {
4541 *diep = die;
4542 die->die_parent = parent;
4543 die->die_ck_state = CK_UNKNOWN;
4544 die->die_ref_seen = 0;
4545 assert (!checksum || die->u.p1.die_enter == tick);
4546 if (die->die_abbrev->children)
4547 {
4548 diep = &die->die_child;
4549 parent = die;
4550 }
4551 else
4552 {
4553 diep = &die->die_sib;
4554 assert (!checksum || die->u.p1.die_exit == tick);
4555 }
4556 tick++;
4557 if (checksum)
4558 ptr = base + die_offset + die->die_size;
4559 else
4560 ptr = skip_attrs (cu, die->die_abbrev, ptr);
4561 continue;
4562 }
4563
4564 collapsed = die;
4565 t = htab_find_with_hash (cu->cu_abbrev, &tag, tag.entry);
4566 if (die_nontoplevel_freelist)
4567 {
4568 die = die_nontoplevel_freelist;
4569 die_nontoplevel_freelist = die->die_sib;
4570 }
4571 else
4572 die = pool_alloc (dw_die, offsetof (struct dw_die, die_dup));
4573 memset (die, '\0', offsetof (struct dw_die, die_dup));
4574 *diep = die;
4575 die->die_tag = t->tag;
4576 die->die_abbrev = t;
4577 die->die_offset = die_offset;
4578 die->die_parent = parent;
4579 if (checksum)
4580 {
4581 die->u.p1.die_enter = tick;
4582 die->u.p1.die_exit = tick++;
4583 }
4584 if (t->children)
4585 {
4586 diep = &die->die_child;
4587 parent = die;
4588 }
4589 else
4590 diep = &die->die_sib;
4591
4592 ptr = skip_attrs (cu, t, ptr);
4593 die->die_size = (ptr - base) - die_offset;
4594 if (collapsed != NULL)
4595 {
4596 die->die_referenced = collapsed->die_referenced;
4597 *slot = (void *) die;
4598 memset (collapsed, '\0', offsetof (struct dw_die, die_child));
4599 collapsed->die_parent = die_collapsed_child_freelist;
4600 die_collapsed_child_freelist = collapsed;
4601 }
4602 }
4603 assert (!checksum || top_die->u.p1.die_exit == tick);
4604 top_die->die_collapsed_children = 0;
4605 if (checksum && likely (cu->cu_kind != CU_TYPES))
4606 for (child = top_die->die_child; child; child = child->die_sib)
4607 checksum_die (NULL, cu, top_die, child);
4608 }
4609
4610 /* Call expand_child on all collapsed toplevel children DIEs. */
4611 static bool
expand_children(dw_die_ref die)4612 expand_children (dw_die_ref die)
4613 {
4614 dw_die_ref child;
4615 bool ret = false;
4616 for (child = die->die_child; child; child = child->die_sib)
4617 if (child->die_named_namespace)
4618 ret |= expand_children (child);
4619 else if (child->die_collapsed_children)
4620 {
4621 expand_child (child, false);
4622 ret = true;
4623 }
4624 return ret;
4625 }
4626
4627 static unsigned odr_phase;
4628
4629 /* Return 1 if DIE1 and DIE2 match. TOP_DIE1 and TOP_DIE2
4630 is the corresponding ultimate parent with die_toplevel
4631 set. u.p1.die_hash and u.p1.die_ref_hash hashes should
4632 hopefully ensure that in most cases this function actually
4633 just verifies matching. */
4634 static int
die_eq_1(dw_cu_ref cu1,dw_cu_ref cu2,dw_die_ref top_die1,dw_die_ref top_die2,dw_die_ref die1,dw_die_ref die2)4635 die_eq_1 (dw_cu_ref cu1, dw_cu_ref cu2,
4636 dw_die_ref top_die1, dw_die_ref top_die2,
4637 dw_die_ref die1, dw_die_ref die2)
4638 {
4639 struct abbrev_tag *t1, *t2;
4640 unsigned int i, j;
4641 unsigned char *ptr1, *ptr2;
4642 dw_die_ref ref1, ref2;
4643 dw_die_ref child1, child2;
4644 bool only_compare_name_p;
4645
4646 #define FAIL goto fail
4647 if (die1 == die2 || die_safe_dup (die2) == die1)
4648 return 1;
4649 if (die1->u.p1.die_hash != die2->u.p1.die_hash
4650 || die1->u.p1.die_ref_hash != die2->u.p1.die_ref_hash
4651 || die1->die_tag != die2->die_tag
4652 || (!odr && (die1->u.p1.die_exit - die1->u.p1.die_enter
4653 != die2->u.p1.die_exit - die2->u.p1.die_enter))
4654 || die_safe_dup (die2) != NULL
4655 || die1->die_ck_state != CK_KNOWN
4656 || die2->die_ck_state != CK_KNOWN
4657 || die1->die_toplevel != die2->die_toplevel)
4658 return 0;
4659 assert (!die1->die_root && !die2->die_root);
4660
4661 if (uni_lang_p && die1 == top_die1 && die2 == top_die2
4662 && cu1->lang != cu2->lang)
4663 return 0;
4664
4665 only_compare_name_p
4666 = odr && die1->die_odr_state != ODR_NONE && die2->die_odr_state != ODR_NONE;
4667
4668 if (only_compare_name_p && odr_phase == 1)
4669 {
4670 const char *name1 = get_AT_string (die1, DW_AT_name);
4671 const char *name2 = get_AT_string (die2, DW_AT_name);
4672 // TODO: Handle DW_AT_linkage_name?
4673 if (name1 == NULL || name2 == NULL)
4674 return 0;
4675 if (strcmp (name1, name2) != 0)
4676 return 0;
4677 }
4678 else if (die1->u.p1.die_exit - die1->u.p1.die_enter
4679 != die2->u.p1.die_exit - die2->u.p1.die_enter)
4680 return 0;
4681
4682 if (only_compare_name_p && odr_phase == 2
4683 && die1->die_odr_state == ODR_DEF && die2->die_odr_state == ODR_DEF)
4684 {
4685 if (die1->u.p1.die_hash2 != die2->u.p1.die_hash2)
4686 return 0;
4687 }
4688
4689 t1 = die1->die_abbrev;
4690 t2 = die2->die_abbrev;
4691 if (likely (!fi_multifile))
4692 {
4693 ptr1 = debug_sections[DEBUG_INFO].data + die1->die_offset;
4694 ptr2 = debug_sections[DEBUG_INFO].data + die2->die_offset;
4695 }
4696 else
4697 {
4698 if (cu1->cu_kind == CU_ALT)
4699 ptr1 = alt_data[DEBUG_INFO];
4700 else
4701 ptr1 = debug_sections[DEBUG_INFO].data;
4702 ptr1 += die1->die_offset;
4703 if (cu2->cu_kind == CU_ALT)
4704 ptr2 = alt_data[DEBUG_INFO];
4705 else
4706 ptr2 = debug_sections[DEBUG_INFO].data;
4707 ptr2 += die2->die_offset;
4708 }
4709 skip_leb128 (ptr1);
4710 skip_leb128 (ptr2);
4711 i = 0;
4712 j = 0;
4713 if (die1->die_toplevel)
4714 {
4715 for (ref1 = die1->die_parent, ref2 = die2->die_parent; ref1 && ref2; )
4716 {
4717 const char *name1, *name2;
4718 if ((ref1->die_tag == DW_TAG_compile_unit
4719 || ref1->die_tag == DW_TAG_partial_unit)
4720 && (ref2->die_tag == DW_TAG_compile_unit
4721 || ref2->die_tag == DW_TAG_partial_unit))
4722 break;
4723 if (ref1->die_tag != ref2->die_tag)
4724 return 0;
4725 if (!ref1->die_named_namespace || !ref2->die_named_namespace)
4726 return 0;
4727 name1 = get_AT_string (ref1, DW_AT_name);
4728 name2 = get_AT_string (ref2, DW_AT_name);
4729 if (strcmp (name1, name2))
4730 return 0;
4731 ref1 = ref1->die_root ? NULL : ref1->die_parent;
4732 ref2 = ref2->die_root ? NULL : ref2->die_parent;
4733 }
4734 if (ref1 == NULL || ref2 == NULL)
4735 return 0;
4736 /* For each toplevel die seen, record optimistically
4737 that we expect them to match, to avoid recursing
4738 on it again. If non-match is determined later,
4739 die_eq wrapper undoes this (which is why the DIE
4740 pointer is added to the vector). */
4741 if (!die2->die_op_type_referenced)
4742 die2->die_remove = 1;
4743 obstack_ptr_grow (&ob, die2);
4744 if (likely (die2->die_nextdup == NULL))
4745 {
4746 die2->die_dup = die1;
4747 die2->die_nextdup = die1->die_nextdup;
4748 obstack_ptr_grow (&ob, NULL);
4749 }
4750 else
4751 {
4752 dw_die_ref next;
4753 for (next = die2; next->die_nextdup; next = next->die_nextdup)
4754 next->die_dup = die1;
4755 next->die_dup = die1;
4756 next->die_nextdup = die1->die_nextdup;
4757 obstack_ptr_grow (&ob, next);
4758 }
4759 die1->die_nextdup = die2;
4760 }
4761
4762 if (only_compare_name_p && odr_phase == 1)
4763 return 1;
4764
4765 while (1)
4766 {
4767 uint32_t form1, form2;
4768 size_t len = 0;
4769 unsigned char *old_ptr1;
4770 unsigned char *old_ptr2;
4771 uint64_t value1, value2;
4772
4773 while (i < t1->nattr && t1->attr[i].attr == DW_AT_sibling)
4774 {
4775 form1 = t1->attr[i].form;
4776 while (form1 == DW_FORM_indirect)
4777 form1 = read_uleb128 (ptr1);
4778 switch (form1)
4779 {
4780 case DW_FORM_ref_udata: skip_leb128 (ptr1); break;
4781 case DW_FORM_ref1: ptr1++; break;
4782 case DW_FORM_ref2: read_16 (ptr1); break;
4783 case DW_FORM_ref4: read_32 (ptr1); break;
4784 case DW_FORM_ref8: read_64 (ptr1); break;
4785 default: FAIL;
4786 }
4787 i++;
4788 }
4789 while (j < t2->nattr && t2->attr[j].attr == DW_AT_sibling)
4790 {
4791 form2 = t2->attr[j].form;
4792 while (form2 == DW_FORM_indirect)
4793 form2 = read_uleb128 (ptr2);
4794 switch (form2)
4795 {
4796 case DW_FORM_ref_udata: skip_leb128 (ptr2); break;
4797 case DW_FORM_ref1: ptr2++; break;
4798 case DW_FORM_ref2: read_16 (ptr2); break;
4799 case DW_FORM_ref4: read_32 (ptr2); break;
4800 case DW_FORM_ref8: read_64 (ptr2); break;
4801 default: FAIL;
4802 }
4803 j++;
4804 }
4805 if (i == t1->nattr)
4806 {
4807 if (j != t2->nattr)
4808 FAIL;
4809 break;
4810 }
4811 if (j == t2->nattr)
4812 FAIL;
4813
4814 if (t1->attr[i].attr != t2->attr[j].attr)
4815 FAIL;
4816
4817 form1 = t1->attr[i].form;
4818 while (form1 == DW_FORM_indirect)
4819 form1 = read_uleb128 (ptr1);
4820 form2 = t2->attr[j].form;
4821 while (form2 == DW_FORM_indirect)
4822 form2 = read_uleb128 (ptr2);
4823 old_ptr1 = ptr1;
4824 old_ptr2 = ptr2;
4825
4826 switch (t1->attr[i].attr)
4827 {
4828 case DW_AT_sibling:
4829 case DW_AT_low_pc:
4830 case DW_AT_high_pc:
4831 case DW_AT_entry_pc:
4832 case DW_AT_ranges:
4833 case DW_AT_call_return_pc:
4834 case DW_AT_call_pc:
4835 /* We shouldn't be hitting DIEs with attributes referencing
4836 addresses and we should have removed DW_AT_subling. */
4837 abort ();
4838 case DW_AT_decl_file:
4839 case DW_AT_call_file:
4840 switch (form1)
4841 {
4842 case DW_FORM_data1: value1 = read_8 (ptr1); break;
4843 case DW_FORM_data2: value1 = read_16 (ptr1); break;
4844 case DW_FORM_data4: value1 = read_32 (ptr1); break;
4845 case DW_FORM_data8: value1 = read_64 (ptr1); break;
4846 case DW_FORM_udata: value1 = read_uleb128 (ptr1); break;
4847 case DW_FORM_sdata: value1 = read_sleb128 (ptr1); break;
4848 case DW_FORM_implicit_const: value1 = t1->values[i]; break;
4849 default: abort ();
4850 }
4851 switch (form2)
4852 {
4853 case DW_FORM_data1: value2 = read_8 (ptr2); break;
4854 case DW_FORM_data2: value2 = read_16 (ptr2); break;
4855 case DW_FORM_data4: value2 = read_32 (ptr2); break;
4856 case DW_FORM_data8: value2 = read_64 (ptr2); break;
4857 case DW_FORM_udata: value2 = read_uleb128 (ptr2); break;
4858 case DW_FORM_sdata: value2 = read_sleb128 (ptr2); break;
4859 case DW_FORM_implicit_const: value2 = t2->values[j]; break;
4860 default: abort ();
4861 }
4862 if (ignore_locus)
4863 {
4864 i++;
4865 j++;
4866 continue;
4867 }
4868 if ((value1 == 0) ^ (value2 == 0))
4869 FAIL;
4870 if (value1 != 0)
4871 {
4872 struct dw_file *cu_file1
4873 = &cu1->cu_files[value1 - 1];
4874 struct dw_file *cu_file2
4875 = &cu2->cu_files[value2 - 1];
4876
4877 if (cu_file1->time != cu_file2->time
4878 || cu_file1->size != cu_file2->size
4879 || strcmp (cu_file1->file, cu_file2->file))
4880 FAIL;
4881
4882 if (cu_file1->dir != NULL)
4883 {
4884 if (cu_file2->dir == NULL
4885 || strcmp (cu_file1->dir, cu_file2->dir))
4886 FAIL;
4887 }
4888 else if (cu_file2->dir != NULL)
4889 FAIL;
4890 /* Ignore DW_AT_comp_dir for DW_AT_*_file <built-in>
4891 etc. if immediately followed by DW_AT_*_line 0. */
4892 else if (cu_file1->file_angle_brackets_encapsulated_no_slash
4893 && i + 1 < t1->nattr
4894 && j + 1 < t2->nattr
4895 && t1->attr[i + 1].attr
4896 == (t1->attr[i].attr == DW_AT_decl_file
4897 ? DW_AT_decl_line : DW_AT_call_line)
4898 && t1->attr[i + 1].form == DW_FORM_data1
4899 && t1->attr[i + 1].attr == t2->attr[j + 1].attr
4900 && t2->attr[j + 1].form == DW_FORM_data1
4901 && *ptr1 == 0
4902 && *ptr2 == 0)
4903 {
4904 i++;
4905 j++;
4906 continue;
4907 }
4908
4909 if ((cu_file1->dir ? cu_file1->dir[0] : cu_file1->file[0])
4910 != '/')
4911 {
4912 if (cu1->cu_comp_dir != NULL)
4913 {
4914 if (cu2->cu_comp_dir == NULL
4915 || strcmp (cu1->cu_comp_dir, cu2->cu_comp_dir))
4916 FAIL;
4917 }
4918 else if (cu2->cu_comp_dir != NULL)
4919 FAIL;
4920 }
4921 }
4922 i++;
4923 j++;
4924 continue;
4925 case DW_AT_decl_line:
4926 case DW_AT_decl_column:
4927 case DW_AT_call_line:
4928 case DW_AT_call_column:
4929 if (ignore_locus)
4930 {
4931 old_ptr1 = NULL;
4932 break;
4933 }
4934 switch (form1)
4935 {
4936 case DW_FORM_data1: value1 = read_8 (ptr1); break;
4937 case DW_FORM_data2: value1 = read_16 (ptr1); break;
4938 case DW_FORM_data4: value1 = read_32 (ptr1); break;
4939 case DW_FORM_data8: value1 = read_64 (ptr1); break;
4940 case DW_FORM_udata: value1 = read_uleb128 (ptr1); break;
4941 case DW_FORM_sdata: value1 = read_sleb128 (ptr1); break;
4942 case DW_FORM_implicit_const: value1 = t1->values[i]; break;
4943 default: abort ();
4944 }
4945 switch (form2)
4946 {
4947 case DW_FORM_data1: value2 = read_8 (ptr2); break;
4948 case DW_FORM_data2: value2 = read_16 (ptr2); break;
4949 case DW_FORM_data4: value2 = read_32 (ptr2); break;
4950 case DW_FORM_data8: value2 = read_64 (ptr2); break;
4951 case DW_FORM_udata: value2 = read_uleb128 (ptr2); break;
4952 case DW_FORM_sdata: value2 = read_sleb128 (ptr2); break;
4953 case DW_FORM_implicit_const: value2 = t2->values[j]; break;
4954 default: abort ();
4955 }
4956 if (value1 != value2)
4957 FAIL;
4958 i++;
4959 j++;
4960 continue;
4961 default:
4962 break;
4963 }
4964
4965 switch (form1)
4966 {
4967 case DW_FORM_ref_addr:
4968 if (likely (!op_multifile && !rd_multifile && !fi_multifile))
4969 {
4970 if (form1 != form2)
4971 FAIL;
4972 break;
4973 }
4974 /* FALLTHRU */
4975 case DW_FORM_ref_udata:
4976 case DW_FORM_ref1:
4977 case DW_FORM_ref2:
4978 case DW_FORM_ref4:
4979 case DW_FORM_ref8:
4980 switch (form2)
4981 {
4982 case DW_FORM_ref_addr:
4983 if (likely (!op_multifile && !rd_multifile && !fi_multifile))
4984 FAIL;
4985 break;
4986 case DW_FORM_ref_udata:
4987 case DW_FORM_ref1:
4988 case DW_FORM_ref2:
4989 case DW_FORM_ref4:
4990 case DW_FORM_ref8:
4991 break;
4992 default:
4993 FAIL;
4994 }
4995 break;
4996 default:
4997 if (form1 != form2)
4998 FAIL;
4999 break;
5000 }
5001
5002 switch (form1)
5003 {
5004 case DW_FORM_addr:
5005 ptr1 += ptr_size;
5006 ptr2 += ptr_size;
5007 break;
5008 case DW_FORM_flag_present:
5009 break;
5010 case DW_FORM_implicit_const:
5011 if ((!ignore_locus || old_ptr1) && t1->values[i] != t2->values[j])
5012 FAIL;
5013 break;
5014 case DW_FORM_flag:
5015 case DW_FORM_data1:
5016 ++ptr1;
5017 ++ptr2;
5018 break;
5019 case DW_FORM_data2:
5020 ptr1 += 2;
5021 ptr2 += 2;
5022 break;
5023 case DW_FORM_data4:
5024 case DW_FORM_sec_offset:
5025 ptr1 += 4;
5026 ptr2 += 4;
5027 break;
5028 case DW_FORM_data8:
5029 case DW_FORM_ref_sig8:
5030 ptr1 += 8;
5031 ptr2 += 8;
5032 break;
5033 case DW_FORM_data16:
5034 ptr1 += 16;
5035 ptr2 += 16;
5036 break;
5037 case DW_FORM_sdata:
5038 case DW_FORM_udata:
5039 skip_leb128 (ptr1);
5040 skip_leb128 (ptr2);
5041 break;
5042 case DW_FORM_strp:
5043 if (unlikely (op_multifile || rd_multifile || fi_multifile))
5044 {
5045 value1 = read_32 (ptr1);
5046 value2 = read_32 (ptr2);
5047 if (fi_multifile)
5048 {
5049 if (strcmp ((char *) (cu1->cu_kind == CU_ALT
5050 ? alt_data[DEBUG_STR]
5051 : debug_sections[DEBUG_STR].data)
5052 + value1,
5053 (char *) (cu2->cu_kind == CU_ALT
5054 ? alt_data[DEBUG_STR]
5055 : debug_sections[DEBUG_STR].data)
5056 + value2) != 0)
5057 FAIL;
5058 i++;
5059 j++;
5060 continue;
5061 }
5062 if (strcmp ((char *) debug_sections[DEBUG_STR].data + value1,
5063 (char *) debug_sections[DEBUG_STR].data + value2)
5064 != 0)
5065 FAIL;
5066 i++;
5067 j++;
5068 continue;
5069 }
5070 ptr1 += 4;
5071 ptr2 += 4;
5072 break;
5073 case DW_FORM_line_strp:
5074 ptr1 += 4;
5075 ptr2 += 4;
5076 break;
5077 case DW_FORM_string:
5078 ptr1 = (unsigned char *) strchr ((char *)ptr1, '\0') + 1;
5079 ptr2 = (unsigned char *) strchr ((char *)ptr2, '\0') + 1;
5080 break;
5081 case DW_FORM_indirect:
5082 abort ();
5083 case DW_FORM_block1:
5084 len = *ptr1++;
5085 ptr1 += len;
5086 len = *ptr2++;
5087 ptr2 += len;
5088 break;
5089 case DW_FORM_block2:
5090 len = read_16 (ptr1);
5091 ptr1 += len;
5092 len = read_16 (ptr2);
5093 ptr2 += len;
5094 break;
5095 case DW_FORM_block4:
5096 len = read_32 (ptr1);
5097 ptr1 += len;
5098 len = read_32 (ptr2);
5099 ptr2 += len;
5100 break;
5101 case DW_FORM_block:
5102 case DW_FORM_exprloc:
5103 len = read_uleb128 (ptr1);
5104 ptr1 += len;
5105 len = read_uleb128 (ptr2);
5106 ptr2 += len;
5107 break;
5108 case DW_FORM_ref_addr:
5109 if (likely (!op_multifile && !rd_multifile && !fi_multifile))
5110 {
5111 ptr1 += cu1->cu_version == 2 ? ptr_size : 4;
5112 ptr2 += cu2->cu_version == 2 ? ptr_size : 4;
5113 break;
5114 }
5115 /* FALLTHRU */
5116 case DW_FORM_ref_udata:
5117 case DW_FORM_ref1:
5118 case DW_FORM_ref2:
5119 case DW_FORM_ref4:
5120 case DW_FORM_ref8:
5121 switch (form1)
5122 {
5123 case DW_FORM_ref_addr:
5124 value1 = read_size (ptr1, cu1->cu_version == 2
5125 ? ptr_size : 4)
5126 - cu1->cu_offset;
5127 ptr1 += cu1->cu_version == 2 ? ptr_size : 4;
5128 break;
5129 case DW_FORM_ref_udata:
5130 value1 = read_uleb128 (ptr1);
5131 break;
5132 case DW_FORM_ref1:
5133 value1 = read_8 (ptr1);
5134 break;
5135 case DW_FORM_ref2:
5136 value1 = read_16 (ptr1);
5137 break;
5138 case DW_FORM_ref4:
5139 value1 = read_32 (ptr1);
5140 break;
5141 case DW_FORM_ref8:
5142 value1 = read_64 (ptr1);
5143 break;
5144 default: abort ();
5145 }
5146 ref1 = off_htab_lookup (cu1, cu1->cu_offset + value1);
5147 switch (form2)
5148 {
5149 case DW_FORM_ref_addr:
5150 value2 = read_size (ptr2, cu2->cu_version == 2
5151 ? ptr_size : 4)
5152 - cu2->cu_offset;
5153 ptr2 += cu2->cu_version == 2 ? ptr_size : 4;
5154 break;
5155 case DW_FORM_ref_udata:
5156 value2 = read_uleb128 (ptr2);
5157 break;
5158 case DW_FORM_ref1:
5159 value2 = read_8 (ptr2);
5160 break;
5161 case DW_FORM_ref2:
5162 value2 = read_16 (ptr2);
5163 break;
5164 case DW_FORM_ref4:
5165 value2 = read_32 (ptr2);
5166 break;
5167 case DW_FORM_ref8:
5168 value2 = read_64 (ptr2);
5169 break;
5170 default: abort ();
5171 }
5172 ref2 = off_htab_lookup (cu2, cu2->cu_offset + value2);
5173 assert (ref1 != NULL && ref2 != NULL);
5174 if (unlikely (op_multifile || low_mem))
5175 {
5176 if (die1->die_collapsed_children && ref1->die_collapsed_child)
5177 {
5178 expand_child (die1, true);
5179 ref1 = off_htab_lookup (cu1, cu1->cu_offset + value1);
5180 }
5181 assert (ref2->die_collapsed_child == 0);
5182 }
5183 if (likely (!ref1->die_collapsed_child)
5184 && die_cu (ref1) == cu1
5185 && ref1->u.p1.die_enter >= top_die1->u.p1.die_enter
5186 && ref1->u.p1.die_exit <= top_die1->u.p1.die_exit)
5187 {
5188 /* A reference into a subdie of the DIE being compared. */
5189 if (die_cu (ref2) != cu2
5190 || ref1->u.p1.die_enter - top_die1->u.p1.die_enter
5191 != ref2->u.p1.die_enter - top_die2->u.p1.die_enter
5192 || top_die1->u.p1.die_exit - ref1->u.p1.die_exit
5193 != top_die2->u.p1.die_exit - ref2->u.p1.die_exit)
5194 FAIL;
5195 }
5196 else
5197 {
5198 dw_die_ref reft1 = ref1, reft2 = ref2;
5199 dw_cu_ref refcu1, refcu2;
5200 while (reft1->die_toplevel == 0)
5201 reft1 = reft1->die_parent;
5202 while (reft2->die_toplevel == 0)
5203 reft2 = reft2->die_parent;
5204 if (unlikely (ref1->die_collapsed_child))
5205 {
5206 if (ref1->die_tag
5207 != ref2->u.p1.die_enter - reft2->u.p1.die_enter)
5208 FAIL;
5209 }
5210 else if (ref1->u.p1.die_enter - reft1->u.p1.die_enter
5211 != ref2->u.p1.die_enter - reft2->u.p1.die_enter)
5212 FAIL;
5213 refcu1 = die_cu (reft1);
5214 refcu2 = die_cu (reft2);
5215 if (unlikely (refcu1->cu_chunk == refcu2->cu_chunk)
5216 && likely (!fi_multifile))
5217 {
5218 if (reft1->die_dup
5219 && die_cu (reft1->die_dup)->cu_chunk
5220 == refcu1->cu_chunk)
5221 reft1 = reft1->die_dup;
5222 if (reft2->die_dup
5223 && die_cu (reft2->die_dup)->cu_chunk
5224 == refcu2->cu_chunk)
5225 reft2 = reft2->die_dup;
5226 if (reft2->die_offset < reft1->die_offset)
5227 {
5228 dw_die_ref tem = reft1;
5229 reft1 = reft2;
5230 reft2 = tem;
5231 }
5232 if (reft1->die_dup == NULL && reft2->die_dup != NULL)
5233 {
5234 dw_die_ref tem = reft1;
5235 reft1 = reft2;
5236 reft2 = tem;
5237 }
5238 }
5239 /* If reft1 (die1 or whatever refers to it is already
5240 in the hash table) already has a dup, follow to that
5241 dup. Don't do the same for reft2, {{top_,}die,reft,child}2
5242 should always be from the current CU. */
5243 if (reft1->die_dup)
5244 reft1 = reft1->die_dup;
5245 refcu1 = die_cu (reft1);
5246 refcu2 = die_cu (reft2);
5247 if (die_eq_1 (refcu1, refcu2, reft1, reft2, reft1, reft2) == 0)
5248 FAIL;
5249 }
5250 i++;
5251 j++;
5252 continue;
5253 default:
5254 abort ();
5255 }
5256
5257 if ((!ignore_locus || old_ptr1)
5258 && (ptr1 - old_ptr1 != ptr2 - old_ptr2
5259 || memcmp (old_ptr1, old_ptr2, ptr1 - old_ptr1)))
5260 FAIL;
5261 i++;
5262 j++;
5263 }
5264
5265 if (unlikely (op_multifile || low_mem))
5266 {
5267 if (die1->die_collapsed_children)
5268 expand_child (die1, true);
5269 assert (die2->die_collapsed_children == 0);
5270 }
5271
5272 for (child1 = die1->die_child, child2 = die2->die_child;
5273 child1 && child2;
5274 child1 = child1->die_sib, child2 = child2->die_sib)
5275 if (die_eq_1 (cu1, cu2, top_die1, top_die2, child1, child2) == 0)
5276 FAIL;
5277
5278 if (child1 || child2)
5279 {
5280 fail:
5281 return 0;
5282 }
5283
5284 if (unlikely (fi_multifile))
5285 assert (cu1->cu_kind == CU_ALT && cu2->cu_kind != CU_ALT);
5286 return 1;
5287 }
5288
5289 /* Wrapper around die_eq_1, used as equality function in
5290 dup_htab hash table. If zero (non-match) is returned and
5291 any toplevel DIEs have been pushed to the vector in ob obstack,
5292 undo the optimistic assignment of die_dup and die_nextdup. */
5293 static int
die_eq(const void * p,const void * q)5294 die_eq (const void *p, const void *q)
5295 {
5296 dw_die_ref die1 = (dw_die_ref) p;
5297 dw_die_ref die2 = (dw_die_ref) q;
5298 dw_die_ref *arr;
5299 unsigned int i, count;
5300 int ret;
5301
5302 if (die1->u.p1.die_hash != die2->u.p1.die_hash
5303 || die1->u.p1.die_ref_hash != die2->u.p1.die_ref_hash)
5304 return 0;
5305 ret = die_eq_1 (die_cu (die1), die_cu (die2), die1, die2, die1, die2);
5306 count = obstack_object_size (&ob) / sizeof (void *);
5307 arr = (dw_die_ref *) obstack_finish (&ob);
5308 if (!ret)
5309 for (i = count; i;)
5310 {
5311 dw_die_ref die;
5312 i -= 2;
5313 die = arr[i]->die_dup;
5314 if (likely (arr[i + 1] == NULL))
5315 {
5316 die->die_nextdup = arr[i]->die_nextdup;
5317 arr[i]->die_nextdup = NULL;
5318 arr[i]->die_dup = NULL;
5319 }
5320 else
5321 {
5322 dw_die_ref next;
5323
5324 assert (die->die_nextdup == arr[i]);
5325 for (next = arr[i]->die_nextdup;
5326 next != arr[i + 1];
5327 next = next->die_nextdup)
5328 {
5329 assert (next->die_dup == die);
5330 next->die_dup = arr[i];
5331 }
5332 assert (next->die_dup == die);
5333 next->die_dup = arr[i];
5334 die->die_nextdup = next->die_nextdup;
5335 next->die_nextdup = NULL;
5336 arr[i]->die_dup = NULL;
5337 }
5338 arr[i]->die_remove = 0;
5339 }
5340 obstack_free (&ob, (void *) arr);
5341 return ret;
5342 }
5343
5344 /* Hash table for finding of matching toplevel DIEs (and all
5345 its children together with it). */
5346 static htab_t dup_htab;
5347
5348 /* After read_multifile dup_htab is moved to this variable. */
5349 static htab_t alt_dup_htab;
5350
5351 /* First CU, start of the linked list of CUs, and the tail
5352 of that list. Initially this contains just the original
5353 CUs, later on newly created partial units are added
5354 to the beginning of the list and optionally .debug_types
5355 CUs are added to its tail. */
5356 static dw_cu_ref first_cu, last_cu;
5357
5358 /* After read_multifile first_cu is copied to this variable. */
5359 static dw_cu_ref alt_first_cu;
5360
5361 /* Compute approximate size of DIE and all its children together. */
5362 static unsigned long
calc_sizes(dw_die_ref die)5363 calc_sizes (dw_die_ref die)
5364 {
5365 unsigned long ret = die->die_size;
5366 dw_die_ref child;
5367 if (wr_multifile ? die->die_no_multifile : die->die_remove)
5368 return 0;
5369 for (child = die->die_child; child; child = child->die_sib)
5370 ret += calc_sizes (child);
5371 return ret;
5372 }
5373
5374 /* Verify the duplicate chains starting at DIE. If ORDERED, also check that
5375 the duplicate chain is in the correct order. */
5376 static void
verify_dups(dw_die_ref die,bool ordered)5377 verify_dups (dw_die_ref die, bool ordered)
5378 {
5379 dw_die_ref d, prev;
5380
5381 assert (die->die_dup == NULL);
5382 assert (die->die_collapsed_children == 0);
5383 assert (die->die_remove == 0);
5384
5385 for (prev = die, d = prev->die_nextdup;
5386 d;
5387 prev = d, d = prev->die_nextdup)
5388 {
5389 if (ordered)
5390 assert (die_cu (prev)->cu_chunk <= die_cu (d)->cu_chunk);
5391 assert (d->die_offset != -1U);
5392 assert (d->die_dup == die);
5393 assert (d->die_remove != d->die_op_type_referenced);
5394 assert (d->die_tag == die->die_tag);
5395 }
5396 }
5397
5398 /* Walk toplevel DIEs in tree rooted by PARENT, and see if they
5399 match previously processed DIEs. */
5400 static int
find_dups(dw_die_ref parent)5401 find_dups (dw_die_ref parent)
5402 {
5403 void **slot;
5404 dw_die_ref child;
5405
5406 if (stats_p && parent->die_root)
5407 stats->root_cnt++;
5408
5409 for (child = parent->die_child; child; child = child->die_sib)
5410 {
5411 if (child->die_ck_state == CK_KNOWN)
5412 {
5413 if (stats_p)
5414 stats->lower_toplevel_with_checksum++;
5415 if (child->die_dup != NULL)
5416 continue;
5417 slot = htab_find_slot_with_hash (dup_htab, child,
5418 child->u.p1.die_ref_hash,
5419 INSERT);
5420 if (slot == NULL)
5421 dwz_oom ();
5422 if (*slot == NULL)
5423 *slot = child;
5424 }
5425 else if (child->die_named_namespace)
5426 {
5427 if (stats_p)
5428 stats->namespace_cnt++;
5429 if (find_dups (child))
5430 return 1;
5431 }
5432 else if (stats_p)
5433 stats->lower_toplevel++;
5434 }
5435 return 0;
5436 }
5437
5438 /* Like find_dups, but for the last multifile optimization phase,
5439 where it only looks at duplicates in the common .debug_info
5440 section. */
5441 static int
find_dups_fi(dw_die_ref parent)5442 find_dups_fi (dw_die_ref parent)
5443 {
5444 dw_die_ref child;
5445
5446 for (child = parent->die_child; child; child = child->die_sib)
5447 {
5448 if (child->die_ck_state == CK_KNOWN)
5449 {
5450 if (child->die_dup != NULL)
5451 continue;
5452 htab_find_with_hash (alt_dup_htab, child, child->u.p1.die_ref_hash);
5453 }
5454 else if (child->die_named_namespace)
5455 if (find_dups_fi (child))
5456 return 1;
5457 }
5458 return 0;
5459 }
5460
5461 /* Return value of DW_AT_name for DIE, or for its specification or abstract
5462 origin. */
5463 static const char *
get_name(dw_die_ref die)5464 get_name (dw_die_ref die)
5465 {
5466 if (die->die_collapsed_child)
5467 return NULL;
5468 const char *name = get_AT_string (die, DW_AT_name);
5469 if (name)
5470 return name;
5471 dw_cu_ref cu = die_cu (die);
5472 bool present;
5473 enum dwarf_form form;
5474 unsigned int value = get_AT_int (die, DW_AT_specification, &present, &form);
5475 if (present)
5476 {
5477 dw_die_ref ref;
5478 if (form != DW_FORM_ref_addr)
5479 value = cu->cu_offset + value;
5480 ref = off_htab_lookup (cu, value);
5481 if (ref)
5482 return get_name (ref);
5483 }
5484 value = get_AT_int (die, DW_AT_abstract_origin, &present, &form);
5485 if (present)
5486 {
5487 dw_die_ref ref;
5488 if (form != DW_FORM_ref_addr)
5489 value = cu->cu_offset + value;
5490 ref = off_htab_lookup (die_cu (die), value);
5491 if (ref)
5492 return get_name (ref);
5493 }
5494 return NULL;
5495 }
5496
5497 /* Dump type of DIE to stderr. */
5498 static void
dump_type(dw_die_ref die)5499 dump_type (dw_die_ref die)
5500 {
5501 bool present;
5502 enum dwarf_form form;
5503 if (die->die_collapsed_child)
5504 return;
5505 unsigned int value = get_AT_int (die, DW_AT_type, &present, &form);
5506 if (!present)
5507 return;
5508
5509 dw_cu_ref cu = die_cu (die);
5510 if (cu == NULL)
5511 return;
5512
5513 dw_die_ref ref;
5514 if (form != DW_FORM_ref_addr)
5515 value = cu->cu_offset + value;
5516 fprintf (stderr, " (type: %x", value);
5517 ref = off_htab_lookup (cu, value);
5518 if (ref != NULL && !ref->die_collapsed_child)
5519 {
5520 const char *type_name = get_name (ref);
5521 if (type_name)
5522 fprintf (stderr, " %s", type_name);
5523 fprintf (stderr, " %s", get_DW_TAG_name (ref->die_tag) + 7);
5524 dump_type (ref);
5525 }
5526 fprintf (stderr, ")");
5527 }
5528
5529 /* Dump DIE to stderr with INDENT. */
5530 static void
dump_die_with_indent(int indent,dw_die_ref die)5531 dump_die_with_indent (int indent, dw_die_ref die)
5532 {
5533 if (die == NULL)
5534 fprintf (stderr, "%*s null", indent, "");
5535 else if (die->die_offset == -1U)
5536 {
5537 fprintf (stderr, "%*s -1 %s", indent, "",
5538 get_DW_TAG_name (die->die_tag) + 7);
5539 dw_die_ref d = die->die_nextdup;
5540 while (d)
5541 {
5542 const char *name = get_name (d);
5543 fprintf (stderr, " -> %x %s %s", d->die_offset, name ? name : "",
5544 get_DW_TAG_name (d->die_tag) + 7);
5545 d = d->die_nextdup;
5546 }
5547 }
5548 else if (die->die_collapsed_child)
5549 {
5550 fprintf (stderr, "%*s %x %c", indent, "", die->die_offset,
5551 die->die_ck_state == CK_KNOWN ? 'O' : 'X');
5552 }
5553 else
5554 {
5555 const char *name = get_name (die);
5556 fprintf (stderr, "%*s %x %c %x", indent, "", die->die_offset,
5557 die->die_ck_state == CK_KNOWN ? 'O' : 'X',
5558 (unsigned) die->u.p1.die_hash);
5559 if (odr && die->die_odr_state != ODR_NONE
5560 && die->die_odr_state != ODR_UNKNOWN)
5561 fprintf (stderr, "(%x)", (unsigned) die->u.p1.die_hash2);
5562 fprintf (stderr, " %x %s %s", (unsigned) die->u.p1.die_ref_hash,
5563 name ? name : "", get_DW_TAG_name (die->die_tag) + 7);
5564 dump_type (die);
5565 }
5566 fprintf (stderr, "\n");
5567 }
5568
5569 /* Dump DIE to stderr. */
5570 void USED
dump_die(dw_die_ref die)5571 dump_die (dw_die_ref die)
5572 {
5573 dump_die_with_indent (0, die);
5574 }
5575
5576 static void
dump_dups(dw_die_ref die)5577 dump_dups (dw_die_ref die)
5578 {
5579 dw_die_ref d;
5580 for (d = die; d; d = d->die_nextdup)
5581 dump_die (d);
5582 }
5583
5584 /* Dump DIE tree at tree depth DEPTH. */
5585 static void
dump_dies(int depth,dw_die_ref die)5586 dump_dies (int depth, dw_die_ref die)
5587 {
5588 dw_die_ref child;
5589 dump_die_with_indent (depth, die);
5590 for (child = die->die_child; child; child = child->die_sib)
5591 dump_dies (depth + 1, child);
5592 }
5593
5594 /* Hash table for .debug_str. Depending on multifile optimization
5595 phase this hash table has 3 different hash/equality functions.
5596 The first set is used to record tail optimized strings, during
5597 write_multifile the whole .debug_str section is written as is,
5598 plus then all the strings which are just suffixes of other
5599 strings. E.g. if .debug_str section contains "foobar" string
5600 and .debug_info section refers to the whole "foobar" string
5601 as well as "bar" by referring to "foobar" + 3.
5602 The second set is used during op_multifile and fi_multifile,
5603 noting each string and in addition to that how many times it
5604 has been seen (0, 1 or more than 1). If 0 then it isn't present
5605 in the hash table, 1 has lowest bit of new_off clear, more than 1
5606 the LSB of new_off is set.
5607 The final set is used during finalize_strp and afterwards, it is
5608 then used to map strings to their location in the new .debug_str
5609 section. */
5610 static htab_t strp_htab;
5611 /* Current offset in .debug_str when adding the tail optimized strings.
5612 This is initially the size of .debug_str section in the object,
5613 and as unique tail optimized strings are found, this is increased
5614 each time. */
5615 static unsigned int max_strp_off;
5616
5617 /* At the end of read_multifile strp_htab is moved to this variable,
5618 which is used to find strings in the shared .debug_str section. */
5619 static htab_t alt_strp_htab;
5620
5621 /* Structure describing strings in strp_htab. */
5622 struct strp_entry
5623 {
5624 /* Original .debug_str offset. */
5625 unsigned int off;
5626 /* New .debug_str offset, or when using strp_{hash,eq}2
5627 this is initially iterative hash of the string with the
5628 LSB bit used for whether the string has been seen just once
5629 or more than once. */
5630 unsigned int new_off;
5631 };
ALIGN_STRUCT(strp_entry)5632 ALIGN_STRUCT (strp_entry)
5633
5634 /* Hash function in strp_htab used for discovery of tail optimized
5635 strings. */
5636 static hashval_t
5637 strp_hash (const void *p)
5638 {
5639 struct strp_entry *s = (struct strp_entry *)p;
5640
5641 return s->off;
5642 }
5643
5644 /* Corresponding equality function in strp_htab. */
5645 static int
strp_eq(const void * p,const void * q)5646 strp_eq (const void *p, const void *q)
5647 {
5648 struct strp_entry *s1 = (struct strp_entry *)p;
5649 struct strp_entry *s2 = (struct strp_entry *)q;
5650
5651 return s1->off == s2->off;
5652 }
5653
5654 /* Hash function in strp_htab used to find what strings are
5655 used by more than one object. */
5656 static hashval_t
strp_hash2(const void * p)5657 strp_hash2 (const void *p)
5658 {
5659 struct strp_entry *s = (struct strp_entry *)p;
5660
5661 return s->new_off & ~1U;
5662 }
5663
5664 /* Corresponding equality function in strp_htab. */
5665 static int
strp_eq2(const void * p,const void * q)5666 strp_eq2 (const void *p, const void *q)
5667 {
5668 struct strp_entry *s1 = (struct strp_entry *)p;
5669 struct strp_entry *s2 = (struct strp_entry *)q;
5670
5671 return strcmp ((char *) debug_sections[DEBUG_STR].data + s1->off,
5672 (char *) debug_sections[DEBUG_STR].data + s2->off) == 0;
5673 }
5674
5675 /* Hash function in strp_htab used from finalize_strp onwards,
5676 mapping strings into strings in the new .debug_str section. */
5677 static hashval_t
strp_hash3(const void * p)5678 strp_hash3 (const void *p)
5679 {
5680 return iterative_hash (p, strlen (p), 0);
5681 }
5682
5683 /* Corresponding equality function in strp_htab. */
5684 static int
strp_eq3(const void * p,const void * q)5685 strp_eq3 (const void *p, const void *q)
5686 {
5687 return strcmp (p, q) == 0;
5688 }
5689
5690 /* Called for each DW_FORM_strp offset seen during initial
5691 .debug_{info,types,macro} parsing. Before fi_multifile phase
5692 this records just tail optimized strings, during fi_multifile
5693 it checks whether the string is already in the shared .debug_str
5694 section and if not, notes that it will need to be added to the
5695 new local .debug_str section. */
5696 static void
note_strp_offset(unsigned int off)5697 note_strp_offset (unsigned int off)
5698 {
5699 void **slot;
5700 struct strp_entry se;
5701
5702 if (unlikely (fi_multifile))
5703 {
5704 unsigned char *p;
5705 unsigned int len;
5706 hashval_t hash;
5707
5708 p = debug_sections[DEBUG_STR].data + off;
5709 len = strlen ((char *) p);
5710 hash = iterative_hash (p, len, 0);
5711 if (alt_strp_htab)
5712 {
5713 if (htab_find_with_hash (alt_strp_htab, p, hash))
5714 return;
5715 }
5716 if (strp_htab == NULL)
5717 {
5718 unsigned int strp_count = debug_sections[DEBUG_STR].size / 64;
5719
5720 if (strp_count < 100)
5721 strp_count = 100;
5722 strp_htab = htab_try_create (strp_count, strp_hash2, strp_eq2, NULL);
5723 if (strp_htab == NULL)
5724 dwz_oom ();
5725 }
5726
5727 se.off = off;
5728 se.new_off = hash | 1;
5729 slot = htab_find_slot_with_hash (strp_htab, &se, se.new_off & ~1U, INSERT);
5730 if (slot == NULL)
5731 dwz_oom ();
5732 if (*slot == NULL)
5733 {
5734 struct strp_entry *s = pool_alloc (strp_entry, sizeof (*s));
5735 *s = se;
5736 *slot = (void *) s;
5737 }
5738 return;
5739 }
5740 if (off >= debug_sections[DEBUG_STR].size || off == 0)
5741 return;
5742 if (debug_sections[DEBUG_STR].data[off - 1] == '\0')
5743 return;
5744 if (strp_htab == NULL)
5745 {
5746 if (multifile == NULL)
5747 return;
5748 strp_htab = htab_try_create (50, strp_hash, strp_eq, NULL);
5749 if (strp_htab == NULL)
5750 dwz_oom ();
5751 max_strp_off = debug_sections[DEBUG_STR].size;
5752 }
5753 se.off = off;
5754 slot = htab_find_slot_with_hash (strp_htab, &se, off, INSERT);
5755 if (slot == NULL)
5756 dwz_oom ();
5757 if (*slot == NULL)
5758 {
5759 struct strp_entry *s = pool_alloc (strp_entry, sizeof (*s));
5760 s->off = off;
5761 s->new_off = max_strp_off;
5762 max_strp_off += strlen ((char *) debug_sections[DEBUG_STR].data
5763 + off) + 1;
5764 if (max_strp_off < s->new_off)
5765 {
5766 htab_delete (strp_htab);
5767 strp_htab = NULL;
5768 max_strp_off = 0;
5769 multifile = NULL;
5770 error (0, 0, ".debug_str too large for multi-file optimization");
5771 }
5772 *slot = (void *) s;
5773 }
5774 }
5775
5776 /* Map offset in original .debug_str section into
5777 offset in new .debug_str, either the shared .debug_str
5778 or new local .debug_str. */
5779 static unsigned
lookup_strp_offset(unsigned int off)5780 lookup_strp_offset (unsigned int off)
5781 {
5782 struct strp_entry *s, se;
5783
5784 if (unlikely (op_multifile || fi_multifile))
5785 {
5786 unsigned char *p;
5787 unsigned int len;
5788 hashval_t hash;
5789
5790 p = debug_sections[DEBUG_STR].data + off;
5791 len = strlen ((char *) p);
5792 hash = iterative_hash (p, len, 0);
5793 if (alt_strp_htab)
5794 {
5795 unsigned char *q = (unsigned char *)
5796 htab_find_with_hash (alt_strp_htab, p, hash);
5797 if (q != NULL)
5798 return q - alt_data[DEBUG_STR];
5799 }
5800 assert (strp_htab);
5801 p = (unsigned char *) htab_find_with_hash (strp_htab, p, hash);
5802 assert (p != NULL);
5803 return p - debug_sections[DEBUG_STR].new_data;
5804 }
5805 if (off >= debug_sections[DEBUG_STR].size || off == 0)
5806 return off + multi_str_off;
5807 if (debug_sections[DEBUG_STR].data[off - 1] == '\0')
5808 return off + multi_str_off;
5809 se.off = off;
5810 s = (struct strp_entry *) htab_find_with_hash (strp_htab, &se, off);
5811 return s->new_off + multi_str_off;
5812 }
5813
5814 /* Note .debug_str offset during write_macro or compute_abbrevs,
5815 return either DW_FORM_strp if the string will be in the local
5816 .debug_str section, or DW_FORM_strp_sup / DW_FORM_GNU_strp_alt if it
5817 will be in the shared .debug_str section. */
5818 static enum dwarf_form
note_strp_offset2(unsigned int off)5819 note_strp_offset2 (unsigned int off)
5820 {
5821 hashval_t hash;
5822 struct strp_entry se;
5823 unsigned char *p, *q;
5824
5825 if (likely (fi_multifile))
5826 {
5827 unsigned int len;
5828
5829 if (alt_strp_htab)
5830 {
5831 p = debug_sections[DEBUG_STR].data + off;
5832 len = strlen ((char *) p);
5833 hash = iterative_hash (p, len, 0);
5834 if (htab_find_with_hash (alt_strp_htab, p, hash))
5835 return dwarf_5 ? DW_FORM_strp_sup : DW_FORM_GNU_strp_alt;
5836 }
5837 return DW_FORM_strp;
5838 }
5839 if (off >= debug_sections[DEBUG_STR].size)
5840 return DW_FORM_strp;
5841 p = debug_sections[DEBUG_STR].data + off;
5842 q = (unsigned char *) strchr ((char *) p, '\0');
5843 hash = iterative_hash (p, q - p, 0);
5844 se.off = off;
5845 se.new_off = hash & ~1U;
5846 struct strp_entry *s = (struct strp_entry *)
5847 htab_find_with_hash (strp_htab, &se, se.new_off);
5848 assert (s != NULL);
5849 s->new_off |= 1;
5850 return DW_FORM_strp;
5851 }
5852
5853 /* Helper to record all strp_entry entries from strp_htab.
5854 Called through htab_traverse. */
5855 static int
list_strp_entries(void ** slot,void * data)5856 list_strp_entries (void **slot, void *data)
5857 {
5858 struct strp_entry ***end = (struct strp_entry ***) data;
5859 **end = (struct strp_entry *) *slot;
5860 (*end)++;
5861 return 1;
5862 }
5863
5864 /* Adapted from bfd/merge.c strrevcmp. */
5865 static int
strrevcmp(const void * p,const void * q)5866 strrevcmp (const void *p, const void *q)
5867 {
5868 struct strp_entry *s1 = *(struct strp_entry **)p;
5869 struct strp_entry *s2 = *(struct strp_entry **)q;
5870 unsigned int len1 = s1->new_off & ~1U;
5871 unsigned int len2 = s2->new_off & ~1U;
5872 unsigned int len;
5873 unsigned char *p1 = debug_sections[DEBUG_STR].data + s1->off;
5874 unsigned char *p2 = debug_sections[DEBUG_STR].data + s2->off;
5875
5876 if (p1[len1])
5877 len1++;
5878 if (p2[len2])
5879 len2++;
5880 p1 += len1;
5881 p2 += len2;
5882 len = len1;
5883 if (len2 < len)
5884 len = len2;
5885 while (len)
5886 {
5887 p1--;
5888 p2--;
5889 if (*p1 != *p2)
5890 {
5891 if (*p1 < *p2)
5892 return -1;
5893 return 1;
5894 }
5895 len--;
5896 }
5897 if (len1 < len2)
5898 return 1;
5899 if (len1 > len2)
5900 return -1;
5901 assert (s1->off == s2->off);
5902 return 0;
5903 }
5904
5905 /* Compute new .debug_str section, from strp_htab content,
5906 replace strp_htab hash table with a new one, which maps strings
5907 to new .debug_str locations. */
5908 static unsigned int *
finalize_strp(bool build_tail_offset_list)5909 finalize_strp (bool build_tail_offset_list)
5910 {
5911 unsigned int count, new_count, i, *tail_offset_list = NULL;
5912 unsigned int strp_index = 0, tail_offset_list_count = 0, k;
5913 struct strp_entry **arr, **end;
5914 unsigned char *p;
5915
5916 if (strp_htab == NULL)
5917 {
5918 debug_sections[DEBUG_STR].new_data = NULL;
5919 debug_sections[DEBUG_STR].new_size = 0;
5920 return NULL;
5921 }
5922 count = htab_elements (strp_htab);
5923 arr = (struct strp_entry **)
5924 obstack_alloc (&ob, count * sizeof (struct strp_entry *));
5925 end = arr;
5926 htab_traverse (strp_htab, list_strp_entries, (void *) &end);
5927 for (i = 0; i < count; i++)
5928 {
5929 unsigned int len = strlen ((char *) debug_sections[DEBUG_STR].data
5930 + arr[i]->off);
5931 arr[i]->new_off = (len & ~1U) | (arr[i]->new_off & 1);
5932 }
5933 qsort (arr, count, sizeof (struct strp_entry *), strrevcmp);
5934 htab_delete (strp_htab);
5935 strp_htab = NULL;
5936 new_count = count;
5937 for (i = 0; i < count; i++)
5938 if ((arr[i]->new_off & 1) == 0)
5939 {
5940 arr[i]->off = -1U;
5941 arr[i]->new_off = -1U;
5942 new_count--;
5943 }
5944 else
5945 {
5946 unsigned int len1, len2, lastlen, j;
5947 unsigned char *p1, *p2;
5948 len1 = arr[i]->new_off & ~1U;
5949 p1 = debug_sections[DEBUG_STR].data + arr[i]->off;
5950 if (p1[len1])
5951 len1++;
5952 lastlen = len1;
5953 arr[i]->new_off = strp_index;
5954 strp_index += len1 + 1;
5955 for (j = i + 1; j < count; j++)
5956 {
5957 len2 = arr[j]->new_off & ~1U;
5958 p2 = debug_sections[DEBUG_STR].data + arr[j]->off;
5959 if (p2[len2])
5960 len2++;
5961 if (len2 >= lastlen)
5962 break;
5963 if (memcmp (p1 + len1 - len2, p2, len2 + 1) != 0)
5964 break;
5965 arr[j]->new_off = arr[i]->new_off + len1 - len2;
5966 lastlen = len2;
5967 tail_offset_list_count++;
5968 }
5969 i = j - 1;
5970 }
5971 debug_sections[DEBUG_STR].new_data = malloc (strp_index);
5972 if (debug_sections[DEBUG_STR].new_data == NULL)
5973 dwz_oom ();
5974 debug_sections[DEBUG_STR].new_size = strp_index;
5975 strp_htab = htab_try_create (new_count < 32 ? 32 : new_count,
5976 strp_hash3, strp_eq3, NULL);
5977 if (strp_htab == NULL)
5978 dwz_oom ();
5979 if (build_tail_offset_list && tail_offset_list_count++ != 0)
5980 {
5981 tail_offset_list
5982 = mmap (NULL, tail_offset_list_count * sizeof (int),
5983 PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
5984 if (tail_offset_list == MAP_FAILED)
5985 dwz_oom ();
5986 }
5987 for (i = 0, k = 0, p = debug_sections[DEBUG_STR].new_data; i < count; i++)
5988 if (arr[i]->off == -1U && arr[i]->new_off == -1U)
5989 continue;
5990 else
5991 {
5992 unsigned int len = strlen ((char *) debug_sections[DEBUG_STR].data
5993 + arr[i]->off) + 1;
5994 unsigned int j;
5995 void **slot;
5996
5997 memcpy (p, debug_sections[DEBUG_STR].data + arr[i]->off, len);
5998 slot = htab_find_slot_with_hash (strp_htab, p,
5999 iterative_hash (p, len - 1, 0),
6000 INSERT);
6001 if (slot == NULL)
6002 dwz_oom ();
6003 assert (*slot == NULL);
6004 *slot = (void *) p;
6005 for (j = i + 1; j < count; j++)
6006 if (arr[j]->new_off >= arr[i]->new_off + len)
6007 break;
6008 else
6009 {
6010 unsigned char *q = p + arr[j]->new_off - arr[i]->new_off;
6011 unsigned int l = len + arr[i]->new_off - arr[j]->new_off;
6012 if (tail_offset_list != NULL)
6013 tail_offset_list[k++] = arr[j]->new_off;
6014 slot = htab_find_slot_with_hash (strp_htab, q,
6015 iterative_hash (q, l - 1, 0),
6016 INSERT);
6017 if (slot == NULL)
6018 dwz_oom ();
6019 assert (*slot == NULL);
6020 *slot = (void *) q;
6021 }
6022 p += len;
6023 i = j - 1;
6024 }
6025 assert (p == debug_sections[DEBUG_STR].new_data + strp_index);
6026 if (tail_offset_list != NULL)
6027 {
6028 tail_offset_list[k++] = 0;
6029 assert (k == tail_offset_list_count);
6030 }
6031 obstack_free (&ob, (void *) arr);
6032 return tail_offset_list;
6033 }
6034
6035 enum mark_refs_mode
6036 {
6037 MARK_REFS_FOLLOW_DUPS = 1,
6038 MARK_REFS_RETURN_VAL = 2,
6039 MARK_REFS_REFERENCED = 4
6040 };
6041
6042 /* Mark all DIEs referenced from DIE by setting die_ref_seen to 1,
6043 unless already marked. */
6044 static bool
mark_refs(dw_cu_ref cu,dw_die_ref top_die,dw_die_ref die,int mode)6045 mark_refs (dw_cu_ref cu, dw_die_ref top_die, dw_die_ref die, int mode)
6046 {
6047 struct abbrev_tag *t;
6048 unsigned int i;
6049 unsigned char *ptr;
6050 dw_die_ref child;
6051
6052 t = die->die_abbrev;
6053 for (i = 0; i < t->nattr; ++i)
6054 if (t->attr[i].attr != DW_AT_sibling)
6055 switch (t->attr[i].form)
6056 {
6057 case DW_FORM_ref_addr:
6058 if (unlikely (op_multifile))
6059 i = -2U;
6060 break;
6061 case DW_FORM_ref_udata:
6062 case DW_FORM_ref1:
6063 case DW_FORM_ref2:
6064 case DW_FORM_ref4:
6065 case DW_FORM_ref8:
6066 case DW_FORM_indirect:
6067 i = -2U;
6068 break;
6069 }
6070 if (i == -1U)
6071 {
6072 if (unlikely (cu->cu_kind == CU_TYPES))
6073 ptr = debug_sections[DEBUG_TYPES].data;
6074 else
6075 ptr = debug_sections[DEBUG_INFO].data;
6076 ptr += die->die_offset;
6077 skip_leb128 (ptr);
6078 for (i = 0; i < t->nattr; ++i)
6079 {
6080 uint32_t form = t->attr[i].form;
6081 size_t len = 0;
6082 uint64_t value;
6083 dw_die_ref ref, reft;
6084
6085 while (form == DW_FORM_indirect)
6086 form = read_uleb128 (ptr);
6087
6088 switch (form)
6089 {
6090 case DW_FORM_ref_addr:
6091 if (unlikely (op_multifile))
6092 {
6093 value = read_size (ptr, cu->cu_version == 2
6094 ? ptr_size : 4);
6095 ptr += cu->cu_version == 2 ? ptr_size : 4;
6096 assert (t->attr[i].attr != DW_AT_sibling);
6097 ref = off_htab_lookup (cu, value);
6098 if ((mode & MARK_REFS_REFERENCED) != 0)
6099 ref->die_referenced = 1;
6100 goto finish_ref;
6101 }
6102 ptr += cu->cu_version == 2 ? ptr_size : 4;
6103 break;
6104 case DW_FORM_addr:
6105 ptr += ptr_size;
6106 break;
6107 case DW_FORM_flag_present:
6108 case DW_FORM_implicit_const:
6109 break;
6110 case DW_FORM_flag:
6111 case DW_FORM_data1:
6112 ++ptr;
6113 break;
6114 case DW_FORM_data2:
6115 ptr += 2;
6116 break;
6117 case DW_FORM_data4:
6118 case DW_FORM_sec_offset:
6119 case DW_FORM_strp:
6120 case DW_FORM_line_strp:
6121 ptr += 4;
6122 break;
6123 case DW_FORM_data8:
6124 case DW_FORM_ref_sig8:
6125 ptr += 8;
6126 break;
6127 case DW_FORM_data16:
6128 ptr += 16;
6129 break;
6130 case DW_FORM_sdata:
6131 case DW_FORM_udata:
6132 skip_leb128 (ptr);
6133 break;
6134 case DW_FORM_ref_udata:
6135 case DW_FORM_ref1:
6136 case DW_FORM_ref2:
6137 case DW_FORM_ref4:
6138 case DW_FORM_ref8:
6139 switch (form)
6140 {
6141 case DW_FORM_ref_udata: value = read_uleb128 (ptr); break;
6142 case DW_FORM_ref1: value = read_8 (ptr); break;
6143 case DW_FORM_ref2: value = read_16 (ptr); break;
6144 case DW_FORM_ref4: value = read_32 (ptr); break;
6145 case DW_FORM_ref8: value = read_64 (ptr); break;
6146 default: abort ();
6147 }
6148 if (t->attr[i].attr == DW_AT_sibling)
6149 break;
6150 ref = off_htab_lookup (cu, cu->cu_offset + value);
6151 if ((mode & MARK_REFS_REFERENCED) != 0)
6152 ref->die_referenced = 1;
6153 if (!ref->die_collapsed_child
6154 && ref->u.p1.die_enter >= top_die->u.p1.die_enter
6155 && ref->u.p1.die_exit <= top_die->u.p1.die_exit)
6156 break;
6157 finish_ref:
6158 reft = ref;
6159 while (!reft->die_root
6160 && reft->die_parent->die_tag != DW_TAG_compile_unit
6161 && reft->die_parent->die_tag != DW_TAG_partial_unit
6162 && !reft->die_parent->die_named_namespace)
6163 reft = reft->die_parent;
6164 if ((mode & MARK_REFS_FOLLOW_DUPS) && reft->die_dup != NULL)
6165 {
6166 reft = reft->die_dup;
6167 if (die_cu (reft)->cu_kind == CU_PU)
6168 break;
6169 }
6170 if (reft->die_ref_seen == 0)
6171 {
6172 if ((mode & MARK_REFS_RETURN_VAL))
6173 return false;
6174 reft->die_ref_seen = 1;
6175 mark_refs (die_cu (reft), reft, reft, mode);
6176 }
6177 break;
6178 case DW_FORM_string:
6179 ptr = (unsigned char *) strchr ((char *)ptr, '\0') + 1;
6180 break;
6181 case DW_FORM_indirect:
6182 abort ();
6183 case DW_FORM_block1:
6184 len = *ptr++;
6185 break;
6186 case DW_FORM_block2:
6187 len = read_16 (ptr);
6188 form = DW_FORM_block1;
6189 break;
6190 case DW_FORM_block4:
6191 len = read_32 (ptr);
6192 form = DW_FORM_block1;
6193 break;
6194 case DW_FORM_block:
6195 case DW_FORM_exprloc:
6196 len = read_uleb128 (ptr);
6197 form = DW_FORM_block1;
6198 break;
6199 default:
6200 abort ();
6201 }
6202
6203 if (form == DW_FORM_block1)
6204 ptr += len;
6205 }
6206 }
6207
6208 for (child = die->die_child; child; child = child->die_sib)
6209 if (!mark_refs (cu, top_die, child, mode))
6210 return false;
6211 return true;
6212 }
6213
6214 /* Remove completely unneeded children of DIE and remove unreferenced DIEs
6215 from offset hash tables. */
6216 static void
remove_dies(dw_cu_ref cu,dw_die_ref die,bool remove)6217 remove_dies (dw_cu_ref cu, dw_die_ref die, bool remove)
6218 {
6219 dw_die_ref child, next;
6220 if (die->die_toplevel && die->die_ref_seen == 0 && !low_mem)
6221 remove = true;
6222 for (child = die->die_child; child; child = next)
6223 {
6224 next = child->die_sib;
6225 remove_dies (cu, child, remove);
6226 }
6227 if (die->die_referenced == 0)
6228 {
6229 htab_t h = cu->cu_kind == CU_TYPES ? types_off_htab : off_htab;
6230 void **slot = htab_find_slot_with_hash (h, die, off_hash (die),
6231 NO_INSERT);
6232 if (slot != NULL)
6233 htab_clear_slot (h, slot);
6234 }
6235 if (!remove)
6236 return;
6237 if (die->die_toplevel == 0)
6238 {
6239 memset (die, '\0', offsetof (struct dw_die, die_dup));
6240 die->die_sib = die_nontoplevel_freelist;
6241 die_nontoplevel_freelist = die;
6242 }
6243 else
6244 die->die_child = NULL;
6245 }
6246
6247 /* Remove unneeded children DIEs. During phase 0 die_ref_seen
6248 of toplevel DIEs is computed, during phase 1 mark_refs is called
6249 to find referenced DIEs, during phase 2 unneeded children DIEs
6250 are removed. */
6251 static void
remove_unneeded(dw_cu_ref cu,dw_die_ref die,unsigned int phase)6252 remove_unneeded (dw_cu_ref cu, dw_die_ref die, unsigned int phase)
6253 {
6254 dw_die_ref child;
6255 for (child = die->die_child; child; child = child->die_sib)
6256 {
6257 if (child->die_named_namespace)
6258 {
6259 remove_unneeded (cu, child, phase);
6260 if (phase == 2)
6261 child->die_ref_seen = 0;
6262 }
6263 else
6264 switch (phase)
6265 {
6266 case 0:
6267 child->die_ref_seen = child->die_dup == NULL;
6268 break;
6269 case 1:
6270 if (child->die_dup == NULL || low_mem)
6271 mark_refs (cu, child, child, MARK_REFS_REFERENCED);
6272 break;
6273 case 2:
6274 remove_dies (cu, child, false);
6275 child->die_ref_seen = 0;
6276 break;
6277 }
6278 }
6279 }
6280
6281 /* Entries in meta_abbrev_htab, mapping .debug_abbrev section offsets
6282 to abbrev hash tables. */
6283 struct meta_abbrev_entry
6284 {
6285 /* .debug_abbrev offset. */
6286 unsigned int abbrev_off;
6287 /* Corresponding hash table. */
6288 htab_t abbrev_htab;
6289 };
6290
6291 /* Hash table for mapping of .debug_abbrev section offsets to
6292 abbrev hash tables. */
6293 static htab_t meta_abbrev_htab;
6294 /* Dummy entry used during OOM handling. */
6295 static struct meta_abbrev_entry meta_abbrev_fallback;
6296
6297 /* Hash function for meta_abbrev_htab. */
6298 static hashval_t
meta_abbrev_hash(const void * p)6299 meta_abbrev_hash (const void *p)
6300 {
6301 struct meta_abbrev_entry *m = (struct meta_abbrev_entry *)p;
6302
6303 return m->abbrev_off;
6304 }
6305
6306 /* Equality function for meta_abbrev_htab. */
6307 static int
meta_abbrev_eq(const void * p,const void * q)6308 meta_abbrev_eq (const void *p, const void *q)
6309 {
6310 struct meta_abbrev_entry *m1 = (struct meta_abbrev_entry *)p;
6311 struct meta_abbrev_entry *m2 = (struct meta_abbrev_entry *)q;
6312
6313 return m1->abbrev_off == m2->abbrev_off;
6314 }
6315
6316 /* Delete function for meta_abbrev_htab. */
6317 static void
meta_abbrev_del(void * p)6318 meta_abbrev_del (void *p)
6319 {
6320 struct meta_abbrev_entry *m = (struct meta_abbrev_entry *)p;
6321
6322 if (m->abbrev_htab != NULL)
6323 htab_delete (m->abbrev_htab);
6324 }
6325
6326 /* Collapse children of TOP_DIE to decrease memory usage. */
6327 static void
collapse_child(dw_cu_ref cu,dw_die_ref top_die,dw_die_ref die,unsigned int * tick)6328 collapse_child (dw_cu_ref cu, dw_die_ref top_die, dw_die_ref die,
6329 unsigned int *tick)
6330 {
6331 dw_die_ref child, next;
6332 bool has_children = die->die_child != NULL;
6333 unsigned int tick_diff = *tick;
6334 for (child = die->die_child; child; child = next)
6335 {
6336 next = child->die_sib;
6337 (*tick)++;
6338 collapse_child (cu, top_die, child, tick);
6339 }
6340 if (has_children)
6341 (*tick)++;
6342 if (top_die == die)
6343 {
6344 die->die_child = NULL;
6345 die->die_collapsed_children = 1;
6346 }
6347 else if (die->die_referenced)
6348 {
6349 die->die_parent = top_die;
6350 if (tick_diff <= 0xffff && !die->die_intercu_referenced)
6351 {
6352 dw_die_ref ref;
6353 void **slot;
6354 if (die_collapsed_child_freelist)
6355 {
6356 ref = die_collapsed_child_freelist;
6357 die_collapsed_child_freelist = ref->die_parent;
6358 }
6359 else
6360 ref = pool_alloc (dw_die, offsetof (struct dw_die, die_child));
6361 memcpy (ref, die, offsetof (struct dw_die, die_child));
6362 ref->die_collapsed_child = 1;
6363 ref->die_tag = tick_diff;
6364 slot = htab_find_slot_with_hash (cu->cu_kind == CU_TYPES
6365 ? types_off_htab : off_htab,
6366 ref, off_hash (ref), NO_INSERT);
6367 assert (slot != NULL);
6368 *slot = (void *) ref;
6369 memset (die, '\0', offsetof (struct dw_die, die_dup));
6370 die->die_sib = die_nontoplevel_freelist;
6371 die_nontoplevel_freelist = die;
6372 }
6373 else
6374 {
6375 die->die_child = NULL;
6376 die->die_sib = NULL;
6377 die->die_ref_seen = tick_diff;
6378 }
6379 }
6380 else
6381 {
6382 memset (die, '\0', offsetof (struct dw_die, die_dup));
6383 die->die_sib = die_nontoplevel_freelist;
6384 die_nontoplevel_freelist = die;
6385 }
6386 }
6387
6388 /* Collapse children of all toplevel DIEs that can be collapsed. */
6389 static void
collapse_children(dw_cu_ref cu,dw_die_ref die)6390 collapse_children (dw_cu_ref cu, dw_die_ref die)
6391 {
6392 dw_die_ref child;
6393 for (child = die->die_child; child; child = child->die_sib)
6394 if (child->die_named_namespace)
6395 collapse_children (cu, child);
6396 else if (child->die_child == NULL)
6397 continue;
6398 else if (child->die_nextdup == NULL
6399 || (child->die_dup != NULL
6400 && (die_cu (child->die_dup)->cu_kind != CU_PU
6401 || child->die_dup->die_nextdup != child)))
6402 {
6403 unsigned int tick = 0;
6404 collapse_child (cu, child, child, &tick);
6405 }
6406 }
6407
6408 /* Count the number of DIEs in the .debug_info section, and see if we run into
6409 some limit. */
6410 static int
try_debug_info(DSO * dso)6411 try_debug_info (DSO *dso)
6412 {
6413 unsigned char *ptr, *endcu, *endsec;
6414 unsigned int value;
6415 htab_t abbrev = NULL;
6416 unsigned int last_abbrev_offset = 0;
6417 struct abbrev_tag tag, *t;
6418 unsigned int ndies;
6419 unsigned ret = 1;
6420 int kind = DEBUG_INFO;
6421 bool low_mem_die_limit_hit = false;
6422
6423 if (unlikely (progress_p))
6424 {
6425 report_progress ();
6426 fprintf (stderr, "try_debug_info\n");
6427 }
6428
6429 if (tracing)
6430 fprintf (stderr, "Counting DIEs\n");
6431
6432 ndies = 0;
6433 ptr = debug_sections[kind].data;
6434 endsec = ptr + debug_sections[kind].size;
6435 while (ptr < endsec)
6436 {
6437 unsigned int culen;
6438 int cu_version;
6439
6440 /* Note header is one bigger with DWARF version 5. */
6441 if (ptr + (kind == DEBUG_TYPES ? 23 : 11) > endsec)
6442 {
6443 error (0, 0, "%s: %s CU header too small", dso->filename,
6444 debug_sections[kind].name);
6445 goto fail;
6446 }
6447
6448 endcu = ptr + 4;
6449 culen = read_32 (ptr);
6450 if (culen >= 0xfffffff0)
6451 {
6452 error (0, 0, "%s: 64-bit DWARF not supported", dso->filename);
6453 goto fail;
6454 }
6455 endcu += culen;
6456
6457 if (endcu > endsec)
6458 {
6459 error (0, 0, "%s: %s too small", dso->filename,
6460 debug_sections[kind].name);
6461 goto fail;
6462 }
6463
6464 cu_version = read_16 (ptr);
6465 if (kind == DEBUG_TYPES && (cu_version < 2 || cu_version > 4))
6466 {
6467 error (0, 0, "%s: DWARF version %d in .debug_types unhandled",
6468 dso->filename, cu_version);
6469 goto fail;
6470 }
6471 else if (cu_version < 2 || cu_version > 5)
6472 {
6473 error (0, 0, "%s: DWARF version %d in .debug_info unhandled",
6474 dso->filename, cu_version);
6475 goto fail;
6476 }
6477
6478 if (cu_version == 5)
6479 {
6480 value = read_8 (ptr);
6481 if (value != DW_UT_compile && value != DW_UT_partial)
6482 error (0, 0, "%s: DWARF CU type %s unhandled", dso->filename,
6483 get_DW_UT_str (value));
6484 }
6485 else
6486 {
6487 value = read_32 (ptr);
6488 if (value >= debug_sections[DEBUG_ABBREV].size)
6489 {
6490 if (debug_sections[DEBUG_ABBREV].data == NULL)
6491 error (0, 0, "%s: .debug_abbrev not present", dso->filename);
6492 else
6493 error (0, 0, "%s: DWARF CU abbrev offset too large",
6494 dso->filename);
6495 goto fail;
6496 }
6497 }
6498
6499 if (ptr_size == 0)
6500 {
6501 ptr_size = read_8 (ptr);
6502 if (ptr_size != 4 && ptr_size != 8)
6503 {
6504 error (0, 0, "%s: Invalid DWARF pointer size %d",
6505 dso->filename, ptr_size);
6506 goto fail;
6507 }
6508 }
6509 else if (read_8 (ptr) != ptr_size)
6510 {
6511 error (0, 0, "%s: DWARF pointer size differs between CUs",
6512 dso->filename);
6513 goto fail;
6514 }
6515
6516 if (cu_version == 5)
6517 {
6518 /* Above we only checked for the smaller version 4 header size. */
6519 if (ptr + 4 > endsec)
6520 {
6521 error (0, 0, "%s: %s CU version 5 header too small",
6522 dso->filename, debug_sections[kind].name);
6523 goto fail;
6524 }
6525 value = read_32 (ptr);
6526 if (value >= debug_sections[DEBUG_ABBREV].size)
6527 {
6528 if (debug_sections[DEBUG_ABBREV].data == NULL)
6529 error (0, 0, "%s: .debug_abbrev not present", dso->filename);
6530 else
6531 error (0, 0, "%s: DWARF CU abbrev offset too large",
6532 dso->filename);
6533 goto fail;
6534 }
6535 }
6536
6537 if (abbrev == NULL || value != last_abbrev_offset)
6538 {
6539 if (abbrev)
6540 htab_delete (abbrev);
6541 abbrev
6542 = read_abbrev (dso, debug_sections[DEBUG_ABBREV].data + value);
6543 if (abbrev == NULL)
6544 {
6545 error (0, 0, "%s: Couldn't read abbrev at offset 0x%x",
6546 dso->filename, value);
6547 goto fail;
6548 }
6549 }
6550 last_abbrev_offset = value;
6551
6552 while (ptr < endcu)
6553 {
6554 tag.entry = read_uleb128 (ptr);
6555 if (tag.entry == 0)
6556 continue;
6557 if (ndies == max_die_limit)
6558 {
6559 error (0, 0, "%s: Too many DIEs, not optimizing",
6560 dso->filename);
6561 goto fail;
6562 }
6563 /* If we reach the DIE limit, signal the dwz caller that it
6564 should retry with low_mem. */
6565 if (likely (!low_mem) && ndies == low_mem_die_limit)
6566 {
6567 if (tracing)
6568 fprintf (stderr, "Hit low-mem die-limit\n");
6569 if (estimate_nr_dies () > max_die_limit)
6570 /* Keep going, we still might hit the max die-limit. */
6571 low_mem_die_limit_hit = true;
6572 else
6573 {
6574 ret = 2;
6575 goto fail;
6576 }
6577 }
6578 ndies++;
6579 t = htab_find_with_hash (abbrev, &tag, tag.entry);
6580 if (t == NULL)
6581 {
6582 error (0, 0, "%s: Could not find DWARF abbreviation %d",
6583 dso->filename, tag.entry);
6584 goto fail;
6585 }
6586 ptr = skip_attrs_1 (cu_version, t, ptr);
6587 }
6588 }
6589
6590 if (low_mem_die_limit_hit)
6591 ret = 2;
6592 else
6593 ret = 0;
6594
6595 fail:
6596 if (abbrev)
6597 htab_delete (abbrev);
6598
6599 return ret;
6600 }
6601
6602 /* Read language attribute encoded using FORM from location PTR, return
6603 pointer to location after the attribute. Assign the attribute value
6604 to *LANG. */
6605 static unsigned char *
read_lang(unsigned char * ptr,enum dwarf_form form,enum dwarf_source_language * lang)6606 read_lang (unsigned char *ptr, enum dwarf_form form,
6607 enum dwarf_source_language *lang)
6608 {
6609 bool error_p = false;
6610 *lang = read_u16 (ptr, form, error_p);
6611 if (unlikely (error_p))
6612 {
6613 *lang = 0;
6614 error (0, 0, "Invalid DW_AT_language attribute, ignoring");
6615 }
6616 return ptr;
6617 }
6618
6619 /* First phase of the DWARF compression. Parse .debug_info section
6620 (for kind == DEBUG_INFO) or .debug_types section (for kind == DEBUG_TYPES)
6621 for each CU in it construct internal representation for the CU
6622 and its DIE tree, compute checksums of DIEs and look for duplicates. */
6623 static int
read_debug_info(DSO * dso,int kind,unsigned int * die_count)6624 read_debug_info (DSO *dso, int kind, unsigned int *die_count)
6625 {
6626 unsigned char *ptr, *endcu, *endsec;
6627 unsigned int value;
6628 htab_t abbrev = NULL;
6629 unsigned int last_abbrev_offset = 0;
6630 unsigned int last_debug_line_off = 0;
6631 struct dw_file *cu_files = NULL;
6632 unsigned int cu_nfiles = 0;
6633 bool note_strp_forms = multifile != NULL && !op_multifile
6634 && !rd_multifile && !low_mem;
6635 struct abbrev_tag tag, *t;
6636 unsigned int cu_chunk = 0;
6637 dw_cu_ref cu_tail = NULL, cu_collapse = NULL;
6638 unsigned int cu_kind = rd_multifile ? CU_ALT
6639 : kind == DEBUG_TYPES ? CU_TYPES : CU_NORMAL;
6640 void *to_free = NULL;
6641 int ret = 1;
6642 unsigned int ndies;
6643 bool low_mem_phase1 = low_mem && kind == DEBUG_INFO;
6644 struct dw_cu cu_buf;
6645 struct dw_die die_buf;
6646 bool lang_p = odr || uni_lang_p;
6647
6648 odr_active_p = false;
6649 if (odr)
6650 odr_phase = 1;
6651
6652 unsigned int estimated_nr_dies = estimate_nr_dies ();
6653 if (kind == DEBUG_INFO
6654 && multifile_mode == 0
6655 && die_count_method == estimate)
6656 {
6657 bool do_count = (estimated_nr_dies > max_die_limit
6658 || estimated_nr_dies > low_mem_die_limit);
6659 if (tracing)
6660 fprintf (stderr, "Using die count estimate %u to decide whether to"
6661 " count DIEs: %s\n", estimated_nr_dies,
6662 do_count ? "yes" : "no");
6663 if (do_count)
6664 {
6665 int try_ret = try_debug_info (dso);
6666 if (try_ret != 0)
6667 return try_ret;
6668 }
6669 }
6670
6671 if (unlikely (progress_p))
6672 {
6673 report_progress ();
6674 fprintf (stderr, "read_debug_info %s%s\n",
6675 kind == DEBUG_INFO ? ".debug_info" : ".debug_types",
6676 low_mem && kind == DEBUG_INFO ? " (low-mem)" : "");
6677 }
6678
6679 if (likely (!fi_multifile && kind != DEBUG_TYPES))
6680 {
6681 dup_htab = htab_try_create (100000, die_hash, die_eq, NULL);
6682 if (dup_htab == NULL)
6683 dwz_oom ();
6684 }
6685 if (unlikely (op_multifile || rd_multifile || fi_multifile || low_mem))
6686 {
6687 meta_abbrev_htab
6688 = htab_try_create (500, meta_abbrev_hash, meta_abbrev_eq,
6689 meta_abbrev_del);
6690 if (meta_abbrev_htab == NULL)
6691 dwz_oom ();
6692 to_free = obstack_alloc (&ob2, 1);
6693 }
6694
6695 low_mem_phase2:
6696 ndies = 0;
6697 ptr = debug_sections[kind].data;
6698 endsec = ptr + debug_sections[kind].size;
6699 while (ptr < endsec)
6700 {
6701 unsigned int cu_offset = ptr - debug_sections[kind].data;
6702 unsigned int tick = 0, culen;
6703 int cu_version;
6704 dw_cu_ref cu;
6705 dw_die_ref *diep, parent, die;
6706 bool present;
6707 unsigned int debug_line_off;
6708 unsigned int type_offset = 0;
6709
6710 /* Note header is one bigger with DWARF version 5. */
6711 if (ptr + (kind == DEBUG_TYPES ? 23 : 11) > endsec)
6712 {
6713 error (0, 0, "%s: %s CU header too small", dso->filename,
6714 debug_sections[kind].name);
6715 goto fail;
6716 }
6717
6718 endcu = ptr + 4;
6719 culen = read_32 (ptr);
6720 if (culen >= 0xfffffff0)
6721 {
6722 error (0, 0, "%s: 64-bit DWARF not supported", dso->filename);
6723 goto fail;
6724 }
6725 endcu += culen;
6726
6727 if (endcu > endsec)
6728 {
6729 error (0, 0, "%s: %s too small", dso->filename,
6730 debug_sections[kind].name);
6731 goto fail;
6732 }
6733
6734 cu_version = read_16 (ptr);
6735 if (kind == DEBUG_TYPES && (cu_version < 2 || cu_version > 4))
6736 {
6737 error (0, 0, "%s: DWARF version %d in .debug_types unhandled",
6738 dso->filename, cu_version);
6739 goto fail;
6740 }
6741 else if (cu_version < 2 || cu_version > 5)
6742 {
6743 error (0, 0, "%s: DWARF version %d in .debug_info unhandled",
6744 dso->filename, cu_version);
6745 goto fail;
6746 }
6747
6748 if (cu_version == 5)
6749 {
6750 value = read_8 (ptr);
6751 if (value != DW_UT_compile && value != DW_UT_partial)
6752 {
6753 error (0, 0, "%s: DWARF CU type %s unhandled", dso->filename,
6754 get_DW_UT_str (value));
6755 goto fail;
6756 }
6757 }
6758 else
6759 {
6760 value = read_32 (ptr);
6761 if (value >= debug_sections[DEBUG_ABBREV].size)
6762 {
6763 if (debug_sections[DEBUG_ABBREV].data == NULL)
6764 error (0, 0, "%s: .debug_abbrev not present", dso->filename);
6765 else
6766 error (0, 0, "%s: DWARF CU abbrev offset too large",
6767 dso->filename);
6768 goto fail;
6769 }
6770 }
6771
6772 if (ptr_size == 0)
6773 {
6774 ptr_size = read_8 (ptr);
6775 if (ptr_size != 4 && ptr_size != 8)
6776 {
6777 error (0, 0, "%s: Invalid DWARF pointer size %d",
6778 dso->filename, ptr_size);
6779 goto fail;
6780 }
6781 }
6782 else if (read_8 (ptr) != ptr_size)
6783 {
6784 error (0, 0, "%s: DWARF pointer size differs between CUs",
6785 dso->filename);
6786 goto fail;
6787 }
6788
6789 if (cu_version == 5)
6790 {
6791 /* Above we only checked for the smaller version 4 header size. */
6792 if (ptr + 4 > endsec)
6793 {
6794 error (0, 0, "%s: %s CU version 5 header too small",
6795 dso->filename, debug_sections[kind].name);
6796 goto fail;
6797 }
6798 value = read_32 (ptr);
6799 if (value >= debug_sections[DEBUG_ABBREV].size)
6800 {
6801 if (debug_sections[DEBUG_ABBREV].data == NULL)
6802 error (0, 0, "%s: .debug_abbrev not present", dso->filename);
6803 else
6804 error (0, 0, "%s: DWARF CU abbrev offset too large",
6805 dso->filename);
6806 goto fail;
6807 }
6808 }
6809
6810 if (unlikely (op_multifile))
6811 {
6812 if (ptr == endcu)
6813 {
6814 dw_cu_ref cuf = cu_tail ? cu_tail->cu_next : first_cu;
6815 /* Inside of optimize_multifile, DIE hashes are computed
6816 only after all the CUs from a particular DSO or
6817 executable have been parsed, as we follow
6818 DW_FORM_ref_addr then. */
6819 for (cu = cuf; cu; cu = cu->cu_next)
6820 if (checksum_die (dso, cu, NULL, cu->cu_die))
6821 goto fail;
6822
6823 for (cu = cuf; cu; cu = cu->cu_next)
6824 checksum_ref_die (cu, NULL, cu->cu_die, NULL, NULL);
6825
6826 if (dump_dies_p)
6827 for (cu = cuf; cu; cu = cu->cu_next)
6828 dump_dies (0, cu->cu_die);
6829
6830 for (cu = cuf; cu; cu = cu->cu_next)
6831 if (find_dups (cu->cu_die))
6832 goto fail;
6833
6834 for (cu = cuf; cu; cu = cu->cu_next)
6835 remove_unneeded (cu, cu->cu_die, 0);
6836 for (cu = cuf; cu; cu = cu->cu_next)
6837 remove_unneeded (cu, cu->cu_die, 1);
6838 for (cu = cuf; cu; cu = cu->cu_next)
6839 remove_unneeded (cu, cu->cu_die, 2);
6840
6841 if (cu_collapse == NULL)
6842 cu_collapse = first_cu;
6843 while (cu_collapse->cu_chunk < cu_chunk)
6844 {
6845 collapse_children (cu_collapse, cu_collapse->cu_die);
6846 cu_collapse = cu_collapse->cu_next;
6847 }
6848
6849 cu_tail = last_cu;
6850 cu_chunk++;
6851 continue;
6852 }
6853 }
6854 else
6855 cu_chunk++;
6856
6857 if (unlikely (meta_abbrev_htab != NULL))
6858 {
6859 struct meta_abbrev_entry m, *mp;
6860 void **slot;
6861 m.abbrev_off = value;
6862 slot = htab_find_slot_with_hash (meta_abbrev_htab, &m,
6863 m.abbrev_off, INSERT);
6864 if (slot == NULL)
6865 dwz_oom ();
6866 else if (*slot != NULL)
6867 abbrev = ((struct meta_abbrev_entry *) *slot)->abbrev_htab;
6868 else
6869 {
6870 *slot = (void *) &meta_abbrev_fallback;
6871 abbrev
6872 = read_abbrev (dso, debug_sections[DEBUG_ABBREV].data + value);
6873 if (abbrev == NULL)
6874 goto fail;
6875 mp = (struct meta_abbrev_entry *)
6876 obstack_alloc (&ob2, sizeof (*mp));
6877 mp->abbrev_off = value;
6878 mp->abbrev_htab = abbrev;
6879 *slot = (void *) mp;
6880 }
6881 }
6882 else if (abbrev == NULL || value != last_abbrev_offset)
6883 {
6884 if (abbrev)
6885 htab_delete (abbrev);
6886 abbrev
6887 = read_abbrev (dso, debug_sections[DEBUG_ABBREV].data + value);
6888 if (abbrev == NULL)
6889 {
6890 error (0, 0, "%s: Couldn't read abbrev at offset 0x%x",
6891 dso->filename, value);
6892
6893 goto fail;
6894 }
6895 }
6896 last_abbrev_offset = value;
6897
6898 if (unlikely (kind == DEBUG_TYPES))
6899 {
6900 ptr += 8;
6901 type_offset = read_32 (ptr);
6902 }
6903
6904 if (unlikely (low_mem_phase1))
6905 cu = &cu_buf;
6906 else
6907 cu = pool_alloc (dw_cu, sizeof (struct dw_cu));
6908 memset (cu, '\0', sizeof (*cu));
6909 cu->cu_kind = cu_kind;
6910 cu->cu_offset = cu_offset;
6911 cu->cu_version = cu_version;
6912 cu->cu_chunk = cu_chunk;
6913 if (unlikely (op_multifile || low_mem))
6914 cu->cu_abbrev = abbrev;
6915 diep = &cu->cu_die;
6916 parent = NULL;
6917 if (unlikely (low_mem_phase1))
6918 ;
6919 else if (first_cu == NULL)
6920 first_cu = last_cu = cu;
6921 else
6922 {
6923 last_cu->cu_next = cu;
6924 last_cu = cu;
6925 }
6926
6927 while (ptr < endcu)
6928 {
6929 unsigned int i;
6930 unsigned int die_offset = ptr - debug_sections[kind].data;
6931
6932 tag.entry = read_uleb128 (ptr);
6933 if (tag.entry == 0)
6934 {
6935 if (unlikely (low_mem_phase1))
6936 continue;
6937 if (parent)
6938 {
6939 diep = &parent->die_sib;
6940 parent->u.p1.die_exit = tick++;
6941 if (parent->die_root == 0)
6942 parent = parent->die_parent;
6943 else
6944 parent = NULL;
6945 }
6946 else
6947 diep = NULL;
6948 continue;
6949 }
6950 if (diep == NULL)
6951 {
6952 error (0, 0, "%s: Wrong %s DIE tree", dso->filename,
6953 debug_sections[kind].name);
6954 goto fail;
6955 }
6956 t = htab_find_with_hash (abbrev, &tag, tag.entry);
6957 if (t == NULL)
6958 {
6959 error (0, 0, "%s: Could not find DWARF abbreviation %d",
6960 dso->filename, tag.entry);
6961 goto fail;
6962 }
6963 if (likely (!op_multifile && !rd_multifile && !fi_multifile)
6964 && likely (kind == DEBUG_INFO))
6965 {
6966 if (ndies == max_die_limit)
6967 {
6968 error (0, 0, "%s: Too many DIEs, not optimizing",
6969 dso->filename);
6970 goto fail;
6971 }
6972 /* If we reach the DIE limit, silently signal the dwz
6973 caller that it should retry with low_mem. */
6974 if (likely (!low_mem) && ndies == low_mem_die_limit)
6975 {
6976 if (tracing)
6977 fprintf (stderr, "Hit low-mem die-limit\n");
6978 ret = 2;
6979 goto fail;
6980 }
6981 }
6982 ndies++;
6983 if (unlikely (low_mem_phase1))
6984 die = &die_buf;
6985 else if (parent == NULL
6986 || parent->die_root
6987 || parent->die_named_namespace)
6988 {
6989 die = pool_alloc (dw_die, sizeof (struct dw_die));
6990 memset (die, '\0', sizeof (struct dw_die));
6991 die->die_toplevel = 1;
6992 }
6993 else
6994 {
6995 if (die_nontoplevel_freelist)
6996 {
6997 die = die_nontoplevel_freelist;
6998 die_nontoplevel_freelist = die->die_sib;
6999 }
7000 else
7001 die = pool_alloc (dw_die, offsetof (struct dw_die, die_dup));
7002 memset (die, '\0', offsetof (struct dw_die, die_dup));
7003 }
7004 *diep = die;
7005 die->die_tag = t->tag;
7006 die->die_abbrev = t;
7007 die->die_offset = die_offset;
7008 if (parent)
7009 die->die_parent = parent;
7010 else
7011 {
7012 die->die_root = 1;
7013 die->die_parent = (dw_die_ref) cu;
7014 }
7015 die->u.p1.die_enter = tick;
7016 die->u.p1.die_exit = tick++;
7017 if (likely (!low_mem_phase1))
7018 {
7019 if (t->children)
7020 {
7021 diep = &die->die_child;
7022 parent = die;
7023 }
7024 else
7025 diep = &die->die_sib;
7026 }
7027 for (i = 0; i < t->nattr; ++i)
7028 {
7029 uint32_t form = t->attr[i].form;
7030 size_t len = 0;
7031
7032 while (form == DW_FORM_indirect)
7033 {
7034 form = read_uleb128 (ptr);
7035 if (ptr > endcu)
7036 {
7037 error (0, 0, "%s: Attributes extend beyond end of CU",
7038 dso->filename);
7039 goto fail;
7040 }
7041 }
7042
7043 /* Get length of expr/blocks first. Canonicalize all,
7044 except exprloc, to DW_FORM_block1. */
7045 switch (form)
7046 {
7047 case DW_FORM_block1:
7048 len = *ptr++;
7049 break;
7050 case DW_FORM_block2:
7051 len = read_16 (ptr);
7052 form = DW_FORM_block1;
7053 break;
7054 case DW_FORM_block4:
7055 len = read_32 (ptr);
7056 form = DW_FORM_block1;
7057 break;
7058 case DW_FORM_block:
7059 len = read_uleb128 (ptr);
7060 form = DW_FORM_block1;
7061 break;
7062 case DW_FORM_exprloc:
7063 len = read_uleb128 (ptr);
7064 break;
7065 default:
7066 break;
7067 }
7068
7069 if (unlikely (low_mem_phase1)
7070 && add_locexpr_dummy_dies (dso, cu, die, ptr, form,
7071 t->attr[i].attr, len))
7072 goto fail;
7073
7074 switch (form)
7075 {
7076 case DW_FORM_ref_addr:
7077 if (unlikely (low_mem_phase1))
7078 {
7079 unsigned int offset
7080 = read_size (ptr, cu->cu_version == 2 ? ptr_size : 4);
7081 add_dummy_die (cu, offset);
7082 }
7083 ptr += cu->cu_version == 2 ? ptr_size : 4;
7084 break;
7085 case DW_FORM_addr:
7086 ptr += ptr_size;
7087 break;
7088 case DW_FORM_flag_present:
7089 break;
7090 case DW_FORM_implicit_const:
7091 if (lang_p
7092 && (die->die_tag == DW_TAG_compile_unit
7093 || die->die_tag == DW_TAG_partial_unit)
7094 && t->attr[i].attr == DW_AT_language)
7095 cu->lang = t->values[i];
7096 break;
7097 case DW_FORM_data1:
7098 if (lang_p
7099 && (die->die_tag == DW_TAG_compile_unit
7100 || die->die_tag == DW_TAG_partial_unit)
7101 && t->attr[i].attr == DW_AT_language)
7102 cu->lang = *ptr;
7103 /* FALLTHRU */
7104 case DW_FORM_ref1:
7105 case DW_FORM_flag:
7106 ++ptr;
7107 break;
7108 case DW_FORM_data2:
7109 if (lang_p
7110 && (die->die_tag == DW_TAG_compile_unit
7111 || die->die_tag == DW_TAG_partial_unit)
7112 && t->attr[i].attr == DW_AT_language)
7113 cu->lang = do_read_16 (ptr);
7114 /* FALLTHRU */
7115 case DW_FORM_ref2:
7116 ptr += 2;
7117 break;
7118 case DW_FORM_data4:
7119 if (lang_p
7120 && (die->die_tag == DW_TAG_compile_unit
7121 || die->die_tag == DW_TAG_partial_unit)
7122 && t->attr[i].attr == DW_AT_language)
7123 read_lang (ptr, form, &cu->lang);
7124 /* FALLTHRU */
7125 case DW_FORM_ref4:
7126 case DW_FORM_sec_offset:
7127 ptr += 4;
7128 break;
7129 case DW_FORM_data8:
7130 if (lang_p
7131 && (die->die_tag == DW_TAG_compile_unit
7132 || die->die_tag == DW_TAG_partial_unit)
7133 && t->attr[i].attr == DW_AT_language)
7134 read_lang (ptr, form, &cu->lang);
7135 /* FALLTHRU */
7136 case DW_FORM_ref8:
7137 case DW_FORM_ref_sig8:
7138 ptr += 8;
7139 break;
7140 case DW_FORM_data16:
7141 ptr += 16;
7142 break;
7143 case DW_FORM_sdata:
7144 case DW_FORM_udata:
7145 if (lang_p
7146 && (die->die_tag == DW_TAG_compile_unit
7147 || die->die_tag == DW_TAG_partial_unit)
7148 && t->attr[i].attr == DW_AT_language)
7149 {
7150 ptr = read_lang (ptr, form, &cu->lang);
7151 break;
7152 }
7153 /* FALLTHRU */
7154 case DW_FORM_ref_udata:
7155 skip_leb128 (ptr);
7156 break;
7157 case DW_FORM_strp:
7158 if (t->attr[i].attr == DW_AT_name
7159 && (die->die_tag == DW_TAG_namespace
7160 || die->die_tag == DW_TAG_module)
7161 && !die->die_root
7162 && (die->die_parent->die_root
7163 || die->die_parent->die_named_namespace))
7164 die->die_named_namespace = 1;
7165 if (note_strp_forms)
7166 note_strp_offset (read_32 (ptr));
7167 else
7168 ptr += 4;
7169 break;
7170 case DW_FORM_line_strp:
7171 if (t->attr[i].attr == DW_AT_name
7172 && (die->die_tag == DW_TAG_namespace
7173 || die->die_tag == DW_TAG_module)
7174 && !die->die_root
7175 && (die->die_parent->die_root
7176 || die->die_parent->die_named_namespace))
7177 die->die_named_namespace = 1;
7178 /* Don't note strp, different string table. */
7179 ptr += 4;
7180 break;
7181 case DW_FORM_string:
7182 ptr = (unsigned char *) strchr ((char *)ptr, '\0') + 1;
7183 if (t->attr[i].attr == DW_AT_name
7184 && (die->die_tag == DW_TAG_namespace
7185 || die->die_tag == DW_TAG_module)
7186 && !die->die_root
7187 && (die->die_parent->die_root
7188 || die->die_parent->die_named_namespace))
7189 die->die_named_namespace = 1;
7190 break;
7191 case DW_FORM_indirect:
7192 abort ();
7193 /* All expr/blocks lengths already handled above.
7194 Just canonicalize exprloc to block1 too. */
7195 case DW_FORM_exprloc:
7196 form = DW_FORM_block1;
7197 break;
7198 case DW_FORM_block1:
7199 break;
7200 case DW_FORM_block2:
7201 case DW_FORM_block4:
7202 case DW_FORM_block:
7203 abort ();
7204 default:
7205 error (0, 0, "%s: Unknown DWARF %s",
7206 dso->filename, get_DW_FORM_str (form));
7207 goto fail;
7208 }
7209
7210 if (ptr > endcu)
7211 {
7212 error (0, 0, "%s: Attributes extend beyond end of CU",
7213 dso->filename);
7214 goto fail;
7215 }
7216
7217 if (form == DW_FORM_block1)
7218 {
7219 if (len >= (size_t) (endcu - ptr))
7220 {
7221 error (0, 0, "%s: Attributes extend beyond end of CU",
7222 dso->filename);
7223 goto fail;
7224 }
7225
7226 if (t->attr[i].attr > DW_AT_loclists_base
7227 && (t->attr[i].attr < DW_AT_MIPS_fde
7228 || t->attr[i].attr > DW_AT_MIPS_has_inlines)
7229 && (t->attr[i].attr < DW_AT_sf_names
7230 || t->attr[i].attr > DW_AT_body_end)
7231 && (t->attr[i].attr < DW_AT_GNU_call_site_value
7232 || t->attr[i].attr
7233 > DW_AT_GNU_call_site_target_clobbered))
7234 {
7235 error (0, 0, "%s: Unknown DWARF %s with "
7236 "block DW_FORM",
7237 dso->filename, get_DW_AT_str (t->attr[i].attr));
7238 goto fail;
7239 }
7240
7241 ptr += len;
7242 }
7243 }
7244 die->die_size = (ptr - debug_sections[kind].data)
7245 - die_offset;
7246 if (unlikely (low_mem))
7247 {
7248 if (low_mem_phase1)
7249 continue;
7250 if (off_htab != NULL && kind == DEBUG_INFO)
7251 {
7252 void **slot
7253 = htab_find_slot_with_hash (off_htab, die, off_hash (die),
7254 INSERT);
7255 if (slot == NULL)
7256 dwz_oom ();
7257 if (*slot != NULL)
7258 {
7259 dw_die_ref ref = (dw_die_ref) *slot;
7260 assert (ref->die_collapsed_child);
7261 die->die_referenced = 1;
7262 die->die_intercu_referenced = 1;
7263 memset (ref, '\0', offsetof (struct dw_die, die_child));
7264 ref->die_parent = die_collapsed_child_freelist;
7265 die_collapsed_child_freelist = ref;
7266 }
7267 *slot = (void *) die;
7268 continue;
7269 }
7270 }
7271
7272 off_htab_add_die (cu, die, die_count);
7273 }
7274
7275 if (unlikely (low_mem_phase1))
7276 continue;
7277
7278 if (cu->cu_die == NULL
7279 || (cu->cu_die->die_tag != DW_TAG_compile_unit
7280 && cu->cu_die->die_tag != DW_TAG_partial_unit
7281 && cu->cu_die->die_tag != DW_TAG_type_unit)
7282 || cu->cu_die->die_sib != NULL)
7283 {
7284 error (0, 0, "%s: %s section chunk doesn't contain a single"
7285 " compile_unit or partial_unit", dso->filename,
7286 debug_sections[kind].name);
7287 goto fail;
7288 }
7289
7290 cu->cu_comp_dir = get_AT_string (cu->cu_die, DW_AT_comp_dir);
7291 enum dwarf_form form;
7292 debug_line_off
7293 = get_AT_int (cu->cu_die, DW_AT_stmt_list, &present, &form);
7294 if (present)
7295 {
7296 if (!(form == DW_FORM_sec_offset || form == DW_FORM_data4))
7297 {
7298 error (0, 0, "%s: DW_AT_stmt_list not DW_FORM_sec_offset or"
7299 " DW_FORM_data4", dso->filename);
7300 goto fail;
7301 }
7302
7303 if (cu_files != NULL && last_debug_line_off == debug_line_off)
7304 {
7305 cu->cu_nfiles = cu_nfiles;
7306 cu->cu_files = cu_files;
7307 }
7308 else
7309 {
7310 if (read_debug_line (dso, cu, debug_line_off))
7311 goto fail;
7312 cu_nfiles = cu->cu_nfiles;
7313 cu_files = cu->cu_files;
7314 last_debug_line_off = debug_line_off;
7315 }
7316 }
7317
7318 if (likely (!op_multifile && !rd_multifile && !fi_multifile)
7319 && likely (kind == DEBUG_INFO))
7320 {
7321 if (checksum_die (dso, cu, NULL, cu->cu_die))
7322 goto fail;
7323 checksum_ref_die (cu, NULL, cu->cu_die, NULL, NULL);
7324 if (odr)
7325 {
7326 dw_die_ref die;
7327 FOREACH_LOW_TOPLEVEL_DIE_IN_CU (die, cu)
7328 {
7329 if (die->die_ck_state != CK_KNOWN)
7330 continue;
7331 if (die_odr_state (die) != ODR_NONE)
7332 die->u.p1.die_ref_hash = die->u.p1.die_hash;
7333 else
7334 die->die_ref_hash_computed = 0;
7335 }
7336 checksum_ref_die (cu, NULL, cu->cu_die, NULL, NULL);
7337 }
7338
7339 if (dump_dies_p)
7340 dump_dies (0, cu->cu_die);
7341
7342 if (find_dups (cu->cu_die))
7343 goto fail;
7344 }
7345 if (unlikely (kind == DEBUG_TYPES))
7346 {
7347 dw_die_ref ref = off_htab_lookup (cu, cu->cu_offset + type_offset);
7348 if (ref == NULL)
7349 {
7350 error (0, 0, "%s: Couldn't find DIE at [%x] "
7351 "referenced by type_offset from cu DIE at [%x]",
7352 dso->filename, cu->cu_offset + type_offset,
7353 cu->cu_die->die_offset);
7354 goto fail;
7355 }
7356 if (unlikely (low_mem))
7357 {
7358 ref->die_referenced = 1;
7359 ref->die_intercu_referenced = 1;
7360 }
7361 }
7362 if (unlikely (low_mem))
7363 {
7364 remove_unneeded (cu, cu->cu_die, 1);
7365 remove_unneeded (cu, cu->cu_die, 2);
7366 collapse_children (cu, cu->cu_die);
7367 }
7368 }
7369
7370 if (unlikely (low_mem_phase1))
7371 {
7372 low_mem_phase1 = false;
7373 cu_chunk = 0;
7374 goto low_mem_phase2;
7375 }
7376
7377 if (unlikely (low_mem))
7378 ;
7379 else if (unlikely (meta_abbrev_htab != NULL))
7380 {
7381 dw_cu_ref cu;
7382
7383 if (unlikely (op_multifile))
7384 for (cu = first_cu; cu; cu = cu->cu_next)
7385 cu->cu_abbrev = NULL;
7386 htab_delete (meta_abbrev_htab);
7387 meta_abbrev_htab = NULL;
7388 obstack_free (&ob2, to_free);
7389 abbrev = NULL;
7390 }
7391 else if (abbrev)
7392 htab_delete (abbrev);
7393
7394 if (unlikely (kind == DEBUG_TYPES))
7395 return 0;
7396
7397 if (unlikely (rd_multifile || fi_multifile))
7398 {
7399 dw_cu_ref cu;
7400
7401 /* Inside of read_multifile, DIE hashes are computed
7402 only after all the PUs are parsed, as we follow
7403 DW_FORM_ref_addr then. */
7404 for (cu = first_cu; cu; cu = cu->cu_next)
7405 if (checksum_die (dso, cu, NULL, cu->cu_die))
7406 goto fail;
7407
7408 for (cu = first_cu; cu; cu = cu->cu_next)
7409 checksum_ref_die (cu, NULL, cu->cu_die, NULL, NULL);
7410
7411 if (dump_dies_p)
7412 for (cu = first_cu; cu; cu = cu->cu_next)
7413 dump_dies (0, cu->cu_die);
7414
7415 if (rd_multifile)
7416 {
7417 for (cu = first_cu; cu; cu = cu->cu_next)
7418 if (find_dups (cu->cu_die))
7419 goto fail;
7420 }
7421 else
7422 for (cu = first_cu; cu; cu = cu->cu_next)
7423 if (find_dups_fi (cu->cu_die))
7424 goto fail;
7425
7426 return 0;
7427 }
7428
7429 if (tracing)
7430 {
7431 if (op_multifile)
7432 fprintf (stderr, "Die count: %u\n", ndies);
7433 else
7434 fprintf (stderr, "Die count: %u, %.2f%% of estimate\n", ndies,
7435 (double)ndies / ((double)estimated_nr_dies / 100));
7436 }
7437 if (tracing)
7438 htab_report (off_htab, "off_htab post-parsing");
7439 if (stats_p)
7440 stats->die_count = ndies;
7441 if (die_count)
7442 *die_count = ndies;
7443 htab_delete (dup_htab);
7444 dup_htab = NULL;
7445 return 0;
7446 fail:
7447 if (unlikely (meta_abbrev_htab != NULL))
7448 {
7449 dw_cu_ref cu;
7450
7451 for (cu = first_cu; cu; cu = cu->cu_next)
7452 cu->cu_abbrev = NULL;
7453 htab_delete (meta_abbrev_htab);
7454 meta_abbrev_htab = NULL;
7455 obstack_free (&ob2, to_free);
7456 }
7457 else if (abbrev)
7458 htab_delete (abbrev);
7459 if (dup_htab && kind == DEBUG_INFO)
7460 {
7461 htab_delete (dup_htab);
7462 dup_htab = NULL;
7463 }
7464 return ret;
7465 }
7466
7467 /* Compare function called from qsort, which should ensure that
7468 dup candidate dies with the same set of referrer CUs are
7469 adjacent. */
7470 static int
partition_cmp(const void * p,const void * q)7471 partition_cmp (const void *p, const void *q)
7472 {
7473 dw_die_ref die1 = *(dw_die_ref *) p;
7474 dw_die_ref die2 = *(dw_die_ref *) q;
7475 dw_die_ref ref1, ref2;
7476 dw_cu_ref last_cu1 = NULL, last_cu2 = NULL;
7477 ref1 = die1;
7478 ref2 = die2;
7479 if (odr_active_p && odr_mode != ODR_BASIC)
7480 {
7481 while (ref1 && die_odr_state (ref1) == ODR_DECL)
7482 ref1 = ref1->die_nextdup;
7483 if (ref1 == NULL)
7484 ref1 = die1;
7485 while (ref2 && die_odr_state (ref2) == ODR_DECL)
7486 ref2 = ref2->die_nextdup;
7487 if (ref2 == NULL)
7488 ref2 = die2;
7489 }
7490 for (;; ref1 = ref1->die_nextdup, ref2 = ref2->die_nextdup)
7491 {
7492 dw_cu_ref ref1cu = NULL;
7493 dw_cu_ref ref2cu = NULL;
7494 while (ref1 && (ref1cu = die_cu (ref1)) == last_cu1)
7495 ref1 = ref1->die_nextdup;
7496 while (ref2 && (ref2cu = die_cu (ref2)) == last_cu2)
7497 ref2 = ref2->die_nextdup;
7498 if (ref1 == NULL || ref2 == NULL)
7499 break;
7500 last_cu1 = ref1cu;
7501 last_cu2 = ref2cu;
7502 if (last_cu1->cu_offset < last_cu2->cu_offset)
7503 return -1;
7504 else if (last_cu1->cu_offset > last_cu2->cu_offset)
7505 return 1;
7506 }
7507 if (ref1)
7508 return -1;
7509 if (ref2)
7510 return 1;
7511 /* The rest is just to keep sort stable. */
7512 if (die1->die_offset < die2->die_offset)
7513 return -1;
7514 if (die1->die_offset > die2->die_offset)
7515 return 1;
7516 return 0;
7517 }
7518
7519 /* Add duplicate chain for DIE to VEC. */
7520 static void
partition_found_dups(dw_die_ref die,struct obstack * vec)7521 partition_found_dups (dw_die_ref die, struct obstack *vec)
7522 {
7523 assert (die->die_ck_state == CK_KNOWN);
7524 obstack_ptr_grow (vec, die);
7525 if (unlikely (verify_dups_p))
7526 verify_dups (die, true);
7527
7528 if (stats_p)
7529 {
7530 uint64_t length = 0;
7531 for (; die; die = die->die_nextdup)
7532 length++;
7533 stats->dup_cnt += length;
7534 stats->dup_chain_max_length = MAX (stats->dup_chain_max_length, length);
7535 stats->dup_chain_cnt++;
7536 }
7537 }
7538
7539 /* Sort duplication chain for HEAD, assuming the chain was formed by
7540 die_eq. */
7541 static void
sort_dups(dw_die_ref head)7542 sort_dups (dw_die_ref head)
7543 {
7544 dw_die_ref prev = NULL, die, next;
7545 /* Sort the die_nextdup list by increasing die_cu ()->cu_chunk.
7546 When it is originally added, child has the lowest
7547 cu_offset, then the DIEs are sorted in the linked list
7548 from highest cu_offset down to lowest or second lowest. */
7549 for (die = head->die_nextdup; die; prev = die, die = next)
7550 {
7551 next = die->die_nextdup;
7552 die->die_nextdup = prev;
7553 }
7554 head->die_nextdup = prev;
7555 }
7556
7557 /* Merge duplicate chains D and D2, and return the head of the merged
7558 chain. */
7559 static dw_die_ref
merge_dups(dw_die_ref d,dw_die_ref d2)7560 merge_dups (dw_die_ref d, dw_die_ref d2)
7561 {
7562 if (d == NULL)
7563 return d2;
7564 if (d2 == NULL)
7565 return d;
7566
7567 dw_die_ref tail = NULL;
7568 dw_die_ref head = NULL;
7569 while (true)
7570 {
7571 dw_die_ref next;
7572 if (d && d2)
7573 {
7574 if (d->die_offset < d2->die_offset)
7575 {
7576 next = d;
7577 d = d->die_nextdup;
7578 }
7579 else
7580 {
7581 next = d2;
7582 d2 = d2->die_nextdup;
7583 }
7584 }
7585 else if (d)
7586 {
7587 next = d;
7588 d = d->die_nextdup;
7589 }
7590 else if (d2)
7591 {
7592 next = d2;
7593 d2 = d2->die_nextdup;
7594 }
7595 else
7596 break;
7597 if (!head)
7598 head = next;
7599 if (tail)
7600 tail->die_nextdup = next;
7601 tail = next;
7602 }
7603
7604 for (d = head; d; d = d->die_nextdup)
7605 if (d == head)
7606 d->die_dup = NULL;
7607 else
7608 d->die_dup = head;
7609
7610 if (unlikely (verify_dups_p))
7611 verify_dups (head, true);
7612 return head;
7613 }
7614
7615 static void
mark_singletons(dw_cu_ref cu,dw_die_ref top_die,dw_die_ref die,struct obstack * vec)7616 mark_singletons (dw_cu_ref cu, dw_die_ref top_die, dw_die_ref die,
7617 struct obstack *vec)
7618 {
7619 struct abbrev_tag *t;
7620 unsigned int i;
7621 unsigned char *ptr;
7622 dw_die_ref child;
7623 bool found;
7624
7625 t = die->die_abbrev;
7626
7627 found = false;
7628 for (i = 0; i < t->nattr; ++i)
7629 {
7630 struct abbrev_attr *attr = &t->attr[i];
7631 if (attr->attr == DW_AT_sibling)
7632 continue;
7633 switch (attr->form)
7634 {
7635 case DW_FORM_ref_udata:
7636 case DW_FORM_ref1:
7637 case DW_FORM_ref2:
7638 case DW_FORM_ref4:
7639 case DW_FORM_ref8:
7640 case DW_FORM_indirect:
7641 found = true;
7642 break;
7643 }
7644 }
7645 if (!found)
7646 goto handle_children;
7647
7648 if (unlikely (cu->cu_kind == CU_TYPES))
7649 ptr = debug_sections[DEBUG_TYPES].data;
7650 else
7651 ptr = debug_sections[DEBUG_INFO].data;
7652 ptr += die->die_offset;
7653 skip_leb128 (ptr);
7654 for (i = 0; i < t->nattr; ++i)
7655 {
7656 struct abbrev_attr *attr = &t->attr[i];
7657 uint32_t form = attr->form;
7658 uint64_t value;
7659 dw_die_ref ref, reft;
7660
7661 while (form == DW_FORM_indirect)
7662 form = read_uleb128 (ptr);
7663
7664 switch (form)
7665 {
7666 case DW_FORM_ref_udata:
7667 case DW_FORM_ref1:
7668 case DW_FORM_ref2:
7669 case DW_FORM_ref4:
7670 case DW_FORM_ref8:
7671 switch (form)
7672 {
7673 case DW_FORM_ref_udata: value = read_uleb128 (ptr); break;
7674 case DW_FORM_ref1: value = read_8 (ptr); break;
7675 case DW_FORM_ref2: value = read_16 (ptr); break;
7676 case DW_FORM_ref4: value = read_32 (ptr); break;
7677 case DW_FORM_ref8: value = read_64 (ptr); break;
7678 default: abort ();
7679 }
7680 if (t->attr[i].attr == DW_AT_sibling)
7681 break;
7682 ref = off_htab_lookup (cu, cu->cu_offset + value);
7683 if (!ref->die_collapsed_child
7684 && ref->u.p1.die_enter >= top_die->u.p1.die_enter
7685 && ref->u.p1.die_exit <= top_die->u.p1.die_exit)
7686 break;
7687 reft = ref;
7688 while (!reft->die_root
7689 && reft->die_parent->die_tag != DW_TAG_compile_unit
7690 && reft->die_parent->die_tag != DW_TAG_partial_unit
7691 && !reft->die_parent->die_named_namespace)
7692 reft = reft->die_parent;
7693 if (reft->die_dup != NULL || reft->die_nextdup != NULL)
7694 break;
7695 if (reft->die_ref_seen)
7696 break;
7697 reft->die_ref_seen = 1;
7698 partition_found_dups (reft, vec);
7699 mark_singletons (die_cu (reft), reft, reft, vec);
7700 break;
7701 default:
7702 ptr = skip_attr_no_dw_form_indirect (cu->cu_version, form, ptr);
7703 break;
7704 }
7705 }
7706
7707 handle_children:
7708 for (child = die->die_child; child; child = child->die_sib)
7709 mark_singletons (cu, top_die, child, vec);
7710 }
7711
7712 /* Split maximal duplicate chain DIE into smaller chains that contain
7713 structurally equal defs, and combine the decls with one of those.
7714 Return the first chain, and call partition_found_dups for the others. */
7715 static dw_die_ref
split_dups(dw_die_ref die,struct obstack * vec)7716 split_dups (dw_die_ref die, struct obstack *vec)
7717 {
7718 dw_die_ref res = NULL;
7719
7720 /* Count the DIEs in the duplicate chain. */
7721 unsigned count = 0;
7722 dw_die_ref d;
7723 for (d = die; d; d = d->die_nextdup)
7724 count++;
7725 assert (count >= 2);
7726
7727 /* Break up the duplicate chain. */
7728 dw_die_ref arr[count];
7729 unsigned int i;
7730 for (d = die, i = 0; d; d = d->die_nextdup, i++)
7731 arr[i] = d;
7732 for (i = 0; i < count; i++)
7733 {
7734 d = arr[i];
7735 d->die_nextdup = NULL;
7736 d->die_dup = NULL;
7737 }
7738
7739 /* Build decls duplicate chain. */
7740 dw_die_ref tail = NULL;
7741 dw_die_ref head = NULL;
7742 for (i = 0; i < count; i++)
7743 {
7744 d = arr[i];
7745 if (die_odr_state (d) != ODR_DECL)
7746 continue;
7747 if (!head)
7748 head = d;
7749 if (tail)
7750 tail->die_nextdup = d;
7751 if (d != head)
7752 d->die_dup = head;
7753 tail = d;
7754 }
7755 dw_die_ref decls = head;
7756
7757 /* Build def duplicate chains. */
7758 unsigned int j;
7759 dw_die_ref d2;
7760 for (i = 0; i < count; i++)
7761 {
7762 d = arr[i];
7763 if (d->die_dup || d->die_nextdup
7764 || die_odr_state (d) == ODR_DECL)
7765 continue;
7766 tail = d;
7767 for (j = i + 1; j < count; j++)
7768 {
7769 d2 = arr[j];
7770 if (d2->die_dup || d2->die_nextdup
7771 || die_odr_state (d2) == ODR_DECL)
7772 continue;
7773 die_eq (d, d2);
7774 }
7775 sort_dups (d);
7776 }
7777
7778 if (odr_mode != ODR_BASIC)
7779 {
7780 /* Find the first duplicate chain containing defs. */
7781 dw_die_ref def = NULL;
7782 for (i = 0; i < count; i++)
7783 {
7784 d = arr[i];
7785 if (die_odr_state (d) == ODR_DECL
7786 || d->die_dup != NULL)
7787 continue;
7788 def = d;
7789 break;
7790 }
7791
7792 /* Merge the def chain with the decls. */
7793 merge_dups (def, decls);
7794 }
7795
7796 for (i = 0; i < count; i++)
7797 {
7798 d = arr[i];
7799 if (d->die_dup == NULL)
7800 /* If DIE is now either:
7801 - no longer part of a duplicate chain, or
7802 - head of a duplicate chain,
7803 don't remove it. */
7804 d->die_remove = 0;
7805 }
7806
7807 /* Notice the new duplicate chains. */
7808 for (i = 0; i < count; i++)
7809 {
7810 d = arr[i];
7811 if (d->die_dup != NULL)
7812 continue;
7813 if (res == NULL)
7814 res = d;
7815 else
7816 partition_found_dups (d, vec);
7817 }
7818 return res;
7819 }
7820
7821 /* Search for duplicate removal reference DIE candidates
7822 in tree rooted by PARENT. */
7823 static void
partition_find_dups(struct obstack * vec,dw_die_ref parent)7824 partition_find_dups (struct obstack *vec, dw_die_ref parent)
7825 {
7826 dw_die_ref child;
7827 for (child = parent->die_child; child; child = child->die_sib)
7828 {
7829 if (child->die_nextdup != NULL
7830 && child->die_dup == NULL
7831 && child->die_offset != -1U)
7832 {
7833 dw_die_ref die;
7834
7835 if (unlikely (op_multifile))
7836 {
7837 /* If all the dups are from the same DSO or executable,
7838 there is nothing in it to optimize in between different
7839 objects. */
7840 unsigned int cu_chunk = die_cu (child)->cu_chunk;
7841 for (die = child->die_nextdup; die; die = die->die_nextdup)
7842 if (die_cu (die)->cu_chunk != cu_chunk)
7843 break;
7844 if (die == NULL)
7845 continue;
7846 }
7847 sort_dups (child);
7848 partition_found_dups (child, vec);
7849 }
7850 else if (child->die_named_namespace)
7851 partition_find_dups (vec, child);
7852 }
7853 }
7854
7855 /* Reorder duplicate chain DIE to make sure it doesn't start with a decl. */
7856 static dw_die_ref
reorder_dups(dw_die_ref die)7857 reorder_dups (dw_die_ref die)
7858 {
7859 unsigned decl_count = 0;
7860 unsigned def_count = 0;
7861 dw_die_ref d;
7862
7863 if (die_odr_state (die) == ODR_NONE)
7864 return die;
7865
7866 for (d = die; d; d = d->die_nextdup)
7867 {
7868 if (die_odr_state (d) == ODR_DECL)
7869 decl_count++;
7870 else
7871 def_count++;
7872 }
7873 if (def_count == 0 || decl_count == 0)
7874 return die;
7875
7876 if (die_odr_state (die) != ODR_DECL)
7877 return die;
7878
7879 dw_die_ref def = NULL;
7880 dw_die_ref prev = NULL;
7881 for (d = die; d; prev = d, d = d->die_nextdup)
7882 {
7883 if (die_odr_state (d) == ODR_DECL)
7884 continue;
7885 def = d;
7886 break;
7887 }
7888
7889 assert (!die->die_remove);
7890 assert (def->die_remove);
7891 def->die_remove = 0;
7892 die->die_remove = 1;
7893 def->die_ref_seen = die->die_ref_seen;
7894 dw_die_ref next = def->die_nextdup;
7895 if (prev)
7896 prev->die_nextdup = next;
7897 def->die_nextdup = die;
7898 for (d = def; d; prev = d, d = d->die_nextdup)
7899 {
7900 if (d == def)
7901 d->die_dup = NULL;
7902 else
7903 d->die_dup = def;
7904 }
7905
7906 if (unlikely (verify_dups_p))
7907 verify_dups (def, false);
7908 return def;
7909 }
7910
7911 /* Copy DIE tree of DIE, as children of new DIE PARENT. */
7912 static dw_die_ref
copy_die_tree(dw_die_ref parent,dw_die_ref die)7913 copy_die_tree (dw_die_ref parent, dw_die_ref die)
7914 {
7915 dw_die_ref child, new_child, *diep;
7916 dw_die_ref new_die;
7917 if (die->die_toplevel)
7918 {
7919 new_die = pool_alloc (dw_die, sizeof (struct dw_die));
7920 memset (new_die, '\0', sizeof (*new_die));
7921 new_die->die_toplevel = 1;
7922 die->die_dup = new_die;
7923 new_die->die_nextdup = die;
7924 if (!die->die_op_type_referenced)
7925 die->die_remove = 1;
7926 }
7927 else
7928 {
7929 new_die = pool_alloc (dw_die, offsetof (struct dw_die, die_dup));
7930 memset (new_die, '\0', offsetof (struct dw_die, die_dup));
7931 }
7932 new_die->die_parent = parent;
7933 new_die->die_tag = die->die_tag;
7934 new_die->die_offset = -1U;
7935 new_die->die_size = die->die_size;
7936 diep = &new_die->die_child;
7937 for (child = die->die_child; child; child = child->die_sib)
7938 {
7939 new_child = copy_die_tree (new_die, child);
7940 *diep = new_child;
7941 diep = &new_child->die_sib;
7942 }
7943 return new_die;
7944 }
7945
7946 /* If all DIEs in the duplication chain DIE are in CUs with the same
7947 language, return that language. Otherwise, return 0. */
7948 static enum dwarf_source_language
partition_lang(dw_die_ref die)7949 partition_lang (dw_die_ref die)
7950 {
7951 enum dwarf_source_language lang;
7952 dw_die_ref d;
7953
7954 if (die == NULL)
7955 return 0;
7956
7957 lang = die_cu (die)->lang;
7958 switch (lang)
7959 {
7960 case DW_LANG_C_plus_plus:
7961 case DW_LANG_C_plus_plus_03:
7962 case DW_LANG_C_plus_plus_11:
7963 case DW_LANG_C_plus_plus_14:
7964 break;
7965 default:
7966 return 0;
7967 }
7968
7969 for (d = die->die_nextdup; d; d = d->die_nextdup)
7970 if (die_cu (d)->lang != lang)
7971 return 0;
7972
7973 return lang;
7974 }
7975
7976 /* Return how many bytes we need to encode VAL. */
7977 static unsigned int
nr_bytes_for(uint64_t val)7978 nr_bytes_for (uint64_t val)
7979 {
7980 unsigned int n;
7981
7982 if (val == 0)
7983 return 1;
7984
7985 for (n = 0; val > 0; n++)
7986 val = val >> 8;
7987
7988 return n;
7989 }
7990
7991 /* Return true if duplicate chains REF1 and REF2 have the same set of
7992 referrer CUs. If so, return the number of unique referrer CUs
7993 in *CNT. */
7994 static inline unsigned int FORCE_INLINE
same_ref_cus_p(dw_die_ref ref1,dw_die_ref ref2,size_t * cnt)7995 same_ref_cus_p (dw_die_ref ref1, dw_die_ref ref2, size_t *cnt)
7996 {
7997 dw_cu_ref last_cu1 = NULL, last_cu2 = NULL;
7998
7999 *cnt = 0;
8000
8001 if (odr_active_p && odr_mode != ODR_BASIC)
8002 {
8003 dw_die_ref orig_ref1 = ref1, orig_ref2 = ref2;
8004 while (ref1 && die_odr_state (ref1) == ODR_DECL)
8005 ref1 = ref1->die_nextdup;
8006 if (ref1 == NULL)
8007 ref1 = orig_ref1;
8008 while (ref2 && die_odr_state (ref2) == ODR_DECL)
8009 ref2 = ref2->die_nextdup;
8010 if (ref2 == NULL)
8011 ref2 = orig_ref2;
8012 }
8013 for (;; ref1 = ref1->die_nextdup, ref2 = ref2->die_nextdup)
8014 {
8015 dw_cu_ref ref1cu = NULL;
8016 dw_cu_ref ref2cu = NULL;
8017
8018 while (ref1 && (ref1cu = die_cu (ref1)) == last_cu1)
8019 ref1 = ref1->die_nextdup;
8020 while (ref2 && (ref2cu = die_cu (ref2)) == last_cu2)
8021 ref2 = ref2->die_nextdup;
8022 if (ref1 == NULL || ref2 == NULL)
8023 break;
8024
8025 last_cu1 = ref1cu;
8026 last_cu2 = ref2cu;
8027
8028 if (last_cu1 != last_cu2)
8029 break;
8030 else
8031 (*cnt)++;
8032 }
8033
8034 if (ref1 || ref2)
8035 return false;
8036
8037 return true;
8038 }
8039
8040 /* Return the number of unique referrer CUs in duplicate chain REF. */
8041 static inline size_t FORCE_INLINE
cnt_ref_cus(dw_die_ref ref)8042 cnt_ref_cus (dw_die_ref ref)
8043 {
8044 dw_cu_ref last_cu1 = NULL;
8045 size_t cnt = 0;
8046
8047 for (;; ref = ref->die_nextdup)
8048 {
8049 dw_cu_ref refcu = NULL;
8050 while (ref && (refcu = die_cu (ref)) == last_cu1)
8051 ref = ref->die_nextdup;
8052 if (ref == NULL)
8053 break;
8054 last_cu1 = refcu;
8055 cnt++;
8056 }
8057
8058 return cnt;
8059 }
8060
8061 /* Helper function of partition_dups_1. Decide what DIEs matching in
8062 multiple CUs might be worthwhile to be moved into partial units,
8063 construct those partial units. */
8064 static bool
partition_dups_1(dw_die_ref * arr,size_t nr_partitions,size_t * partitions,dw_cu_ref * first_partial_cu,dw_cu_ref * last_partial_cu,bool second_phase)8065 partition_dups_1 (dw_die_ref *arr, size_t nr_partitions, size_t *partitions,
8066 dw_cu_ref *first_partial_cu,
8067 dw_cu_ref *last_partial_cu,
8068 bool second_phase)
8069 {
8070 size_t i, j, cnt;
8071 bool ret = false;
8072 size_t idx = 0;
8073 for (idx = 0; idx < nr_partitions * 2; idx += 2)
8074 {
8075 i = partitions[idx];
8076 cnt = partitions[idx + 1];
8077 j = partitions[idx + 2];
8078
8079 if (arr[i]->die_dup != NULL)
8080 continue;
8081
8082 dw_die_ref ref;
8083 size_t size = 0, k, orig_size, new_size, namespaces = 0;
8084 unsigned int force = 0;
8085 enum dwarf_source_language part_lang
8086 = gen_cu_p ? partition_lang (arr[i]) : 0;
8087 for (k = i; k < j; k++)
8088 {
8089 if (second_phase && arr[k]->die_ref_seen)
8090 force++;
8091 size += calc_sizes (arr[k]);
8092 for (ref = arr[k]->die_parent;
8093 ref->die_named_namespace && ref->die_dup == NULL;
8094 ref = ref->die_parent)
8095 {
8096 ref->die_dup = arr[k];
8097 namespaces++;
8098 }
8099 }
8100 /* If during second_phase there are some DIEs we want to force
8101 into a partial unit because they are referenced from something
8102 already forced into a partial unit, but also some DIEs with
8103 the same set of referrers, try to see if we can put also those
8104 into the partial unit. They can be put there only if they
8105 don't refer to DIEs that won't be put into partial units. */
8106 if (unlikely (partition_dups_opt)
8107 && second_phase && force && force < j - i)
8108 {
8109 /* First optimistically assume all such DIEs can be put there,
8110 thus mark all such DIEs as going to be included, so that
8111 even if one of those DIEs references another one from those
8112 DIEs it can be included. */
8113 for (k = i; k < j; k++)
8114 {
8115 assert (arr[k]->die_ref_seen < 2);
8116 if (arr[k]->die_ref_seen == 0)
8117 arr[k]->die_ref_seen = 2;
8118 }
8119 for (k = i; k < j; k++)
8120 if (arr[k]->die_ref_seen == 2
8121 && !mark_refs (die_cu (arr[k]), arr[k], arr[k],
8122 (MARK_REFS_FOLLOW_DUPS | MARK_REFS_RETURN_VAL)))
8123 break;
8124 /* If that is not possible and some DIEs couldn't be included,
8125 fallback to assume other DIEs won't be included. */
8126 if (k < j)
8127 {
8128 for (k = i; k < j; k++)
8129 if (arr[k]->die_ref_seen == 2)
8130 arr[k]->die_ref_seen = 0;
8131 for (k = i; k < j; k++)
8132 if (arr[k]->die_ref_seen == 0)
8133 {
8134 arr[k]->die_ref_seen = 2;
8135 if (!mark_refs (die_cu (arr[k]), arr[k], arr[k],
8136 (MARK_REFS_FOLLOW_DUPS
8137 | MARK_REFS_RETURN_VAL)))
8138 arr[k]->die_ref_seen = 0;
8139 }
8140 }
8141 }
8142 if (namespaces)
8143 {
8144 for (k = i; k < j; k++)
8145 for (ref = arr[k]->die_parent; ref->die_named_namespace;
8146 ref = ref->die_parent)
8147 ref->die_dup = NULL;
8148 }
8149 orig_size = size * cnt;
8150 /* Estimated size of CU header and DW_TAG_partial_unit
8151 with DW_AT_stmt_list and DW_AT_comp_dir attributes
8152 21 (also child end byte). With DW_AT_language c++, 22. */
8153 size_t pu_size
8154 = (/* CU Header: unit length (initial length).
8155 32-bit DWARF: 4 bytes, 64-bit DWARF: 12 bytes. */
8156 4
8157 /* CU Header: version (uhalf).
8158 2 bytes. */
8159 + 2
8160 /* CU Header: debug_abbrev_offset (section offset).
8161 32-bit DWARF: 4 bytes, 64-bit DWARF: 8 bytes. */
8162 + 4
8163 /* CU Header: address_size (ubyte).
8164 1 byte. */
8165 + 1
8166 /* DWARF5 CU header: unit_type (ubyte). */
8167 + (die_cu (arr[i])->cu_version >= 5 ? 1 : 0)
8168 /* CU Root DIE: abbreviation code (unsigned LEB128).
8169 1 or more bytes. Optimistically assume 1. */
8170 + 1
8171 /* CU Root DIE: DW_AT_stmt_list (lineptr).
8172 32-bit DWARF: 4 bytes, 64-bit DWARF: 8 bytes. */
8173 + 4
8174 /* CU Root DIE: DW_AT_comp_dir (string).
8175 DW_FORM_strp: 32-bit DWARF: 4 bytes, 64-bit DWARF: 8 bytes.
8176 DW_FORM_string: 1 or more bytes.
8177 Assume 4 bytes. */
8178 + 4
8179 /* CU Root DIE: DW_AT_language (constant).
8180 1 or 2 bytes. */
8181 + ((uni_lang_p || part_lang)
8182 ? nr_bytes_for (die_cu (arr[i])->lang)
8183 : 0)
8184 /* CU root DIE children terminator: abbreviation code 0
8185 (unsigned LEB128).
8186 1 byte. */
8187 + 1);
8188 /* DW_TAG_imported_unit with DW_AT_import attribute
8189 (5 or 9 bytes (the latter for DWARF2 and ptr_size 8)). */
8190 size_t import_size
8191 = (die_cu (arr[i])->cu_version == 2 ? 1 + ptr_size : 5);
8192 /* For DW_TAG_namespace or DW_TAG_module needed as
8193 parents of the DIEs conservatively assume 10 bytes
8194 for the abbrev index, DW_AT_name attribute and
8195 DW_AT_sibling attribute and child end. */
8196 size_t namespace_size = 10;
8197 new_size = (/* Size of DIEs. */
8198 size
8199 /* Size of PU. */
8200 + pu_size
8201 /* Size of imports. */
8202 + (part_lang != 0 ? 0 : import_size * cnt)
8203 /* Size of namespace DIEs. */
8204 + namespace_size * namespaces);
8205 if (!second_phase)
8206 force = ((deduplication_mode == dm_inter_cu)
8207 && (ignore_size || orig_size > new_size));
8208 if (force)
8209 {
8210 dw_die_ref die, *diep;
8211 dw_cu_ref refcu = die_cu (arr[i]);
8212 dw_cu_ref partial_cu = pool_alloc (dw_cu, sizeof (struct dw_cu));
8213 memset (partial_cu, '\0', sizeof (*partial_cu));
8214 if (stats_p)
8215 {
8216 if (!second_phase)
8217 stats->pu_ph1_cnt++;
8218 else
8219 stats->pu_ph2_cnt++;
8220 }
8221 if (dump_pus_p)
8222 fprintf (stderr, "Partial unit (%s):\n",
8223 second_phase ? "phase two" : "phase one");
8224 partial_cu->cu_kind = CU_PU;
8225 partial_cu->cu_offset = *last_partial_cu == NULL
8226 ? 0 : (*last_partial_cu)->cu_offset + 1;
8227 partial_cu->cu_version = refcu->cu_version;
8228 if (uni_lang_p)
8229 partial_cu->lang = refcu->lang;
8230 if (*first_partial_cu == NULL)
8231 *first_partial_cu = *last_partial_cu = partial_cu;
8232 else
8233 {
8234 (*last_partial_cu)->cu_next = partial_cu;
8235 *last_partial_cu = partial_cu;
8236 }
8237 die = pool_alloc (dw_die, sizeof (struct dw_die));
8238 memset (die, '\0', sizeof (*die));
8239 die->die_toplevel = 1;
8240 partial_cu->cu_die = die;
8241 die->die_tag = DW_TAG_partial_unit;
8242 die->die_offset = -1U;
8243 die->die_root = 1;
8244 die->die_parent = (dw_die_ref) partial_cu;
8245 die->die_nextdup = refcu->cu_die;
8246 die->die_size = 9;
8247 diep = &die->die_child;
8248 for (k = i; k < j; k++)
8249 {
8250 dw_die_ref child;
8251 if (second_phase && !arr[k]->die_ref_seen)
8252 continue;
8253 if (dump_pus_p)
8254 dump_die (arr[k]);
8255 child = copy_die_tree (die, arr[k]);
8256 if (stats_p)
8257 stats->pu_toplevel_die_cnt++;
8258 for (ref = arr[k]->die_nextdup; ref; ref = ref->die_nextdup)
8259 ref->die_dup = child;
8260 if (unlikely (verify_dups_p))
8261 verify_dups (child, odr_mode == ODR_BASIC);
8262 if (part_lang != 0)
8263 {
8264 die->die_tag = DW_TAG_compile_unit;
8265 partial_cu->lang = part_lang;
8266 }
8267 if (namespaces)
8268 {
8269 for (ref = arr[k]->die_parent;
8270 ref->die_named_namespace && ref->die_dup == NULL;
8271 ref = ref->die_parent)
8272 {
8273 dw_die_ref namespc
8274 = pool_alloc (dw_die, sizeof (struct dw_die));
8275 memset (namespc, '\0', sizeof (struct dw_die));
8276 namespc->die_toplevel = 1;
8277 namespc->die_tag = ref->die_tag;
8278 namespc->die_offset = -1U;
8279 namespc->die_nextdup = ref;
8280 namespc->die_child = child;
8281 namespc->die_parent = die;
8282 namespc->die_size = 9;
8283 namespc->die_named_namespace = 1;
8284 child->die_parent = namespc;
8285 ref->die_dup = namespc;
8286 child = namespc;
8287 }
8288 if (ref->die_dup != NULL)
8289 {
8290 dw_die_ref *diep2;
8291 for (diep2 = &ref->die_dup->die_child->die_sib;
8292 *diep2; diep2 = &(*diep2)->die_sib)
8293 ;
8294 *diep2 = child;
8295 child->die_parent = ref->die_dup;
8296 continue;
8297 }
8298 }
8299 *diep = child;
8300 diep = &child->die_sib;
8301 }
8302 if (namespaces)
8303 {
8304 for (k = i; k < j; k++)
8305 {
8306 if (second_phase && !arr[k]->die_ref_seen)
8307 continue;
8308 for (ref = arr[k]->die_parent;
8309 ref->die_named_namespace; ref = ref->die_parent)
8310 ref->die_dup = NULL;
8311 }
8312 }
8313 }
8314 else if (!second_phase)
8315 ret = true;
8316 if (second_phase)
8317 {
8318 dw_die_ref next;
8319 for (k = i; k < j; k++)
8320 {
8321 if (arr[k]->die_dup != NULL)
8322 continue;
8323 for (ref = arr[k]; ref; ref = next)
8324 {
8325 dw_cu_ref refcu = die_cu (ref);
8326 next = ref->die_nextdup;
8327 ref->die_dup = NULL;
8328 ref->die_nextdup = NULL;
8329 ref->die_remove = 0;
8330 /* If there are dups within a single CU
8331 (arguably a bug in the DWARF producer),
8332 keep them linked together, but don't link
8333 DIEs across different CUs. */
8334 while (deduplication_mode != dm_none
8335 && next && refcu == die_cu (next))
8336 {
8337 dw_die_ref cur = next;
8338 next = cur->die_nextdup;
8339 cur->die_dup = ref;
8340 cur->die_nextdup = ref->die_nextdup;
8341 ref->die_nextdup = cur;
8342 }
8343 }
8344 }
8345 }
8346 }
8347 return ret;
8348 }
8349
8350 /* Partition the duplicate chains in array ARR with size VEC_SIZE, and store
8351 the partitions on obstack ob2, with for each partition two entries:
8352 the start and the number of unique reffer CUs. */
8353 static void
calculate_partitions(dw_die_ref * arr,size_t vec_size)8354 calculate_partitions (dw_die_ref *arr, size_t vec_size)
8355 {
8356 size_t i, j;
8357 for (i = 0; i < vec_size; i = j)
8358 {
8359 size_t cnt = 0;
8360 for (j = i + 1; j < vec_size; j++)
8361 {
8362 size_t this_cnt;
8363 if (!same_ref_cus_p (arr[i], arr[j], &this_cnt))
8364 break;
8365 cnt = this_cnt;
8366 }
8367 if (cnt == 0)
8368 cnt = cnt_ref_cus (arr[i]);
8369 obstack_grow (&ob2, &i, sizeof (size_t));
8370 obstack_grow (&ob2, &cnt, sizeof (size_t));
8371 }
8372
8373 /* Add element to mark end of partition list. This allows us to do
8374 'j = partitions[idx + 2]' for all partitions. */
8375 obstack_grow (&ob2, &j, sizeof (size_t));
8376 size_t zero = 0;
8377 obstack_grow (&ob2, &zero, sizeof (size_t));
8378 }
8379
8380 static inline void FORCE_INLINE
reset_die_ref_seen(void)8381 reset_die_ref_seen (void)
8382 {
8383 dw_die_ref die;
8384 dw_cu_ref cu;
8385 FOREACH_CU_NORMAL_LOW_TOPLEVEL_DIE (cu, die)
8386 die->die_ref_seen = 0;
8387 }
8388
8389 /* If the duplicate chain DIE consists of a singleton ODR_DEF die merged with
8390 the ODR_DECL chain, return the singleton ODR_DEF die. Otherwise, return
8391 NULL. */
8392 static inline dw_die_ref FORCE_INLINE
merged_singleton(dw_die_ref die)8393 merged_singleton (dw_die_ref die)
8394 {
8395 dw_die_ref res = NULL;
8396 dw_die_ref d;
8397 size_t decl_cnt = 0;
8398
8399 for (d = die; d; d = d->die_nextdup)
8400 switch (die_odr_state (d))
8401 {
8402 case ODR_DEF:
8403 if (res)
8404 return NULL;
8405 else
8406 res = d;
8407 break;
8408 case ODR_DECL:
8409 decl_cnt++;
8410 break;
8411 default:
8412 return NULL;
8413 }
8414
8415 if (decl_cnt == 0)
8416 return NULL;
8417
8418 return res;
8419 }
8420
8421 /* Decide what DIEs matching in multiple CUs might be worthwhile
8422 to be moved into partial units, construct those partial units. */
8423 static int
partition_dups(void)8424 partition_dups (void)
8425 {
8426 dw_cu_ref cu, first_partial_cu = NULL, last_partial_cu = NULL;
8427 size_t vec_size, i;
8428 unsigned char *to_free;
8429 dw_die_ref *arr;
8430
8431 if (unlikely (fi_multifile))
8432 return 0;
8433
8434 if (unlikely (progress_p))
8435 {
8436 report_progress ();
8437 fprintf (stderr, "partition_dups\n");
8438 }
8439
8440 to_free = obstack_alloc (&ob2, 1);
8441
8442 if (odr_active_p)
8443 odr_phase = 2;
8444
8445 for (cu = first_cu; cu; cu = cu->cu_next)
8446 partition_find_dups (&ob2, cu->cu_die);
8447 vec_size = obstack_object_size (&ob2) / sizeof (void *);
8448
8449 if (odr_active_p)
8450 {
8451 arr = (dw_die_ref *) obstack_base (&ob2);
8452 if (progress_p)
8453 {
8454 report_progress ();
8455 fprintf (stderr, "partition_dups split_dups\n");
8456 }
8457 for (i = 0; i < vec_size; i++)
8458 {
8459 dw_die_ref die = arr[i];
8460 if (die_odr_state (die) == ODR_NONE)
8461 continue;
8462 die = split_dups (die, &ob2);
8463 assert (die != NULL);
8464 if (unlikely (verify_dups_p))
8465 verify_dups (die, true);
8466 arr = (dw_die_ref *) obstack_base (&ob2);
8467 arr[i] = die;
8468 }
8469
8470 vec_size = obstack_object_size (&ob2) / sizeof (void *);
8471
8472 reset_die_ref_seen ();
8473 for (i = 0; i < vec_size; i++)
8474 {
8475 dw_die_ref die = arr[i];
8476 if (die->die_dup == NULL
8477 && die->die_nextdup == NULL)
8478 die->die_ref_seen = 1;
8479 }
8480 for (i = 0; i < vec_size; i++)
8481 {
8482 dw_die_ref die = arr[i];
8483 if (die->die_dup == NULL
8484 && die->die_nextdup == NULL)
8485 mark_singletons (die_cu (die), die, die, &ob2);
8486 else if (odr_mode != ODR_BASIC
8487 && die_odr_state (die) != ODR_NONE)
8488 {
8489 dw_die_ref s = merged_singleton (die);
8490 if (s)
8491 mark_singletons (die_cu (s), s, s, &ob2);
8492 }
8493 else if (cnt_ref_cus (die) == 1)
8494 mark_singletons (die_cu (die), die, die, &ob2);
8495
8496 arr = (dw_die_ref *) obstack_base (&ob2);
8497 }
8498
8499 vec_size = obstack_object_size (&ob2) / sizeof (void *);
8500 }
8501
8502 if (odr_active_p)
8503 odr_phase = 3;
8504
8505 if (stats_p)
8506 print_dups_stats ();
8507
8508 if (vec_size != 0)
8509 {
8510 arr = (dw_die_ref *) obstack_finish (&ob2);
8511 if (odr_active_p)
8512 for (i = 0; i < vec_size; ++i)
8513 {
8514 assert (arr[i] != NULL);
8515 if (unlikely (verify_dups_p))
8516 verify_dups (arr[i], true);
8517 }
8518 if (dump_dups_p)
8519 {
8520 for (i = 0; i < vec_size; ++i)
8521 {
8522 fprintf (stderr, "duplicate chain:\n");
8523 dump_dups (arr[i]);
8524 }
8525 }
8526 if (unlikely (progress_p))
8527 {
8528 report_progress ();
8529 fprintf (stderr, "partition_dups qsort\n");
8530 }
8531 qsort (arr, vec_size, sizeof (dw_die_ref), partition_cmp);
8532 if (unlikely (progress_p))
8533 {
8534 report_progress ();
8535 fprintf (stderr, "partition_dups after qsort\n");
8536 }
8537
8538 size_t *partitions = (size_t *) obstack_base (&ob2);
8539 calculate_partitions (arr, vec_size);
8540 size_t nr_partitions
8541 = (obstack_object_size (&ob2) / sizeof (size_t)) / 2 - 1;
8542 partitions = (size_t *) obstack_finish (&ob2);
8543 if (stats_p)
8544 stats->part_cnt += nr_partitions;
8545
8546 if (odr_active_p && odr_mode != ODR_BASIC)
8547 for (i = 0; i < vec_size; ++i)
8548 arr[i] = reorder_dups (arr[i]);
8549 if (partition_dups_1 (arr, nr_partitions, partitions, &first_partial_cu,
8550 &last_partial_cu, false))
8551 {
8552 for (i = 0; i < vec_size; i++)
8553 arr[i]->die_ref_seen = arr[i]->die_dup != NULL;
8554 for (i = 0; i < vec_size; i++)
8555 if (arr[i]->die_dup != NULL)
8556 mark_refs (die_cu (arr[i]), arr[i], arr[i],
8557 MARK_REFS_FOLLOW_DUPS);
8558 partition_dups_1 (arr, nr_partitions, partitions, &first_partial_cu,
8559 &last_partial_cu, true);
8560 for (i = 0; i < vec_size; i++)
8561 arr[i]->die_ref_seen = 0;
8562 }
8563 }
8564 if (first_partial_cu)
8565 {
8566 last_partial_cu->cu_next = first_cu;
8567 first_cu = first_partial_cu;
8568 }
8569 obstack_free (&ob2, to_free);
8570
8571 if (stats_p)
8572 print_part_stats ();
8573
8574 return 0;
8575 }
8576
8577 /* The create_import_tree function below and all its helper
8578 data structures and functions attempt to optimize the size of
8579 DW_TAG_imported_unit DIEs, from the initial assumption that
8580 each CU that needs to include some newly created DW_TAG_partial_unit
8581 will contain DW_TAG_imported_unit for each such partial unit (PU)
8582 (so basically a bipartite graph with CUs and PUs as nodes
8583 and DW_TAG_imported_unit DIEs as edges) into a tree, where some
8584 of the partial units may also include DW_TAG_imported_unit
8585 DIEs, or when beneficial new PUs are created to hold some
8586 DW_TAG_imported_unit DIEs. */
8587
8588 struct import_edge;
8589
8590 /* Structure describing details about a CU or PU (i.e. a node
8591 in the graph). */
8592 struct import_cu
8593 {
8594 /* Corresponding CU. CU->u1.cu_icu points back to this
8595 structure while in create_import_tree. */
8596 dw_cu_ref cu;
8597 /* Linked list of incoming resp. outgoing edges. */
8598 struct import_edge *incoming, *outgoing;
8599 /* The tail of the linked list of incoming edges. */
8600 struct import_edge *incoming_tail;
8601 /* Next import_cu (used to chain PUs together). */
8602 struct import_cu *next;
8603 /* Number of incoming resp. outgoing edges. */
8604 unsigned int incoming_count, outgoing_count;
8605 /* Index. Lowest indexes are assigned to partition_dups
8606 created PUs (sorted by decreasing number of incoming
8607 edges at the start), then referencing CUs
8608 (similarly, sorted by decreasing number of outgoing
8609 edges at the start), then optionally any PUs
8610 created by create_import_tree. */
8611 unsigned int idx;
8612 };
8613
8614 /* An edge in a linked list. */
8615 struct import_edge
8616 {
8617 struct import_cu *icu;
8618 struct import_edge *next;
8619 };
8620
8621 /* Called through qsort to sort an array of edges by decreasing
8622 incoming resp. outgoing_count (this is called when the graph
8623 is bipartite, so CUs only have non-zero outgoing_count
8624 and PUs only have non-zero incoming_count). */
8625 static int
import_edge_cmp(const void * p,const void * q)8626 import_edge_cmp (const void *p, const void *q)
8627 {
8628 struct import_edge *e1 = (struct import_edge *) p;
8629 struct import_edge *e2 = (struct import_edge *) q;
8630 if (e1->icu->incoming_count > e2->icu->incoming_count)
8631 return -1;
8632 if (e1->icu->incoming_count < e2->icu->incoming_count)
8633 return 1;
8634 if (e1->icu->outgoing_count > e2->icu->outgoing_count)
8635 return -1;
8636 if (e1->icu->outgoing_count < e2->icu->outgoing_count)
8637 return 1;
8638 /* The rest is just to keep qsort stable. */
8639 if (e1->icu->cu->cu_offset < e2->icu->cu->cu_offset)
8640 return -1;
8641 if (e1->icu->cu->cu_offset > e2->icu->cu->cu_offset)
8642 return 1;
8643 return 0;
8644 }
8645
8646 /* Called through qsort to sort an array of CUs/PUs by decreasing
8647 incoming resp. outgoing_count (this is called when the graph
8648 is bipartite, so CUs only have non-zero outgoing_count
8649 and PUs only have non-zero incoming_count). */
8650 static int
import_cu_cmp(const void * p,const void * q)8651 import_cu_cmp (const void *p, const void *q)
8652 {
8653 struct import_cu *c1 = *(struct import_cu **) p;
8654 struct import_cu *c2 = *(struct import_cu **) q;
8655 if (c1->incoming_count > c2->incoming_count)
8656 return -1;
8657 if (c1->incoming_count < c2->incoming_count)
8658 return 1;
8659 if (c1->outgoing_count > c2->outgoing_count)
8660 return -1;
8661 if (c1->outgoing_count < c2->outgoing_count)
8662 return 1;
8663 /* The rest is just to keep qsort stable. */
8664 if (c1->cu->cu_offset < c2->cu->cu_offset)
8665 return -1;
8666 if (c1->cu->cu_offset > c2->cu->cu_offset)
8667 return 1;
8668 return 0;
8669 }
8670
8671 /* Freelist for removed edges. */
8672 static struct import_edge *edge_freelist;
8673
8674 /* Prepare edge E to add to edge_freelist. */
8675 static inline void FORCE_INLINE
prepare_free_edge(struct import_edge * e UNUSED)8676 prepare_free_edge (struct import_edge *e UNUSED)
8677 {
8678 #if DEVEL
8679 e->icu = (void *)(uintptr_t)-1;
8680 #endif
8681 }
8682
8683 /* Add edge E to edge_freelist. */
8684 static inline void FORCE_INLINE
free_edge(struct import_edge * e)8685 free_edge (struct import_edge *e)
8686 {
8687 prepare_free_edge (e);
8688 e->next = edge_freelist;
8689 edge_freelist = e;
8690 }
8691
8692 /* Add edge list starting at HEAD and ending at TAIL to edge_freelist.
8693 Assume that prepare_free_edge has been called on all elements. */
8694 static inline void FORCE_INLINE
free_edges(struct import_edge * head,struct import_edge * tail)8695 free_edges (struct import_edge *head, struct import_edge *tail)
8696 {
8697 #if DEVEL
8698 if (verify_edge_freelist)
8699 {
8700 struct import_edge *e;
8701 for (e = head; e; e = e->next)
8702 {
8703 assert (e->icu == (void *)(uintptr_t)-1);
8704 if (e == tail)
8705 break;
8706 }
8707 assert (e != NULL);
8708 }
8709 #endif
8710 tail->next = edge_freelist;
8711 edge_freelist = head;
8712 }
8713
8714 /* Detach an edge from edge_freelist, and return it. */
8715 static inline struct import_edge * FORCE_INLINE
edge_from_freelist(void)8716 edge_from_freelist (void)
8717 {
8718 #if DEVEL
8719 assert (edge_freelist);
8720 #endif
8721 struct import_edge *e = edge_freelist;
8722 edge_freelist = edge_freelist->next;
8723 #if DEVEL
8724 e->next = (void *)(uintptr_t)-1;
8725 #endif
8726 return e;
8727 }
8728
8729 /* Return edge_freelist, and set it to NULL. */
8730 static inline struct import_edge * FORCE_INLINE
first_edge_from_freelist(void)8731 first_edge_from_freelist (void)
8732 {
8733 #if DEVEL
8734 assert (edge_freelist);
8735 #endif
8736 struct import_edge *e = edge_freelist;
8737 #if DEVEL
8738 edge_freelist = NULL;
8739 #endif
8740 return e;
8741 }
8742
8743 /* Set edge_freelist to TAIL->next and return HEAD. Assume HEAD was returned
8744 by first_edge_from_freelist, and TAIL is reachable from HEAD. */
8745 static inline struct import_edge * FORCE_INLINE
last_edge_from_freelist(struct import_edge * head,struct import_edge * tail)8746 last_edge_from_freelist (struct import_edge *head, struct import_edge *tail)
8747 {
8748 #if DEVEL
8749 assert (!edge_freelist);
8750 if (verify_edge_freelist)
8751 {
8752 struct import_edge *e;
8753 for (e = head; e; e = e->next)
8754 {
8755 if (e == tail)
8756 break;
8757 }
8758 assert (e != NULL);
8759 }
8760 #endif
8761 edge_freelist = tail->next;
8762 tail->next = NULL;
8763 return head;
8764 }
8765
8766 /* Remove edges in linked list EP that refer to CUS, which
8767 is an array of CUCOUNT CUs/PUs. If ADD is true, additionally
8768 add a new edge at the end of the linked list and return it. */
8769 static struct import_edge *
remove_import_edges(struct import_edge ** ep,struct import_edge ** ep_tail,struct import_cu ** cus,unsigned int cucount,bool add)8770 remove_import_edges (struct import_edge **ep, struct import_edge **ep_tail,
8771 struct import_cu **cus, unsigned int cucount, bool add)
8772 {
8773 unsigned int i = 0;
8774 struct import_edge *e, *efirst = NULL, *prev = NULL;
8775 while (*ep)
8776 if (i < cucount && (*ep)->icu == cus[i])
8777 {
8778 e = *ep;
8779 *ep = e->next;
8780 if (efirst == NULL)
8781 efirst = e;
8782 else
8783 free_edge (e);
8784 i++;
8785 if (ep_tail && *ep_tail == e)
8786 *ep_tail = prev;
8787 if (i == cucount && !add)
8788 return NULL;
8789 }
8790 else
8791 {
8792 if (ep_tail)
8793 prev = *ep;
8794 ep = &(*ep)->next;
8795 }
8796 assert (i == cucount);
8797 *ep = efirst;
8798 efirst->next = NULL;
8799 if (ep_tail)
8800 *ep_tail = efirst;
8801 return efirst;
8802 }
8803
8804 static void
dump_edges_1(struct import_cu * ipu)8805 dump_edges_1 (struct import_cu *ipu)
8806 {
8807 fprintf (stderr, "idx: %u\n", ipu->idx);
8808 fprintf (stderr, "cu: 0x%x\n", ipu->cu->cu_offset);
8809 struct import_edge *e1;
8810 for (e1 = ipu->incoming; e1; e1 = e1->next)
8811 fprintf (stderr, "incoming: %u\n", e1->icu->idx);
8812 for (e1 = ipu->outgoing; e1; e1 = e1->next)
8813 fprintf (stderr, "outgoing: %u\n", e1->icu->idx);
8814 }
8815
8816 static void
dump_edges(const char * msg,struct import_cu ** ipus,unsigned int npus,unsigned int ncus)8817 dump_edges (const char *msg, struct import_cu **ipus, unsigned int npus,
8818 unsigned int ncus)
8819 {
8820 struct import_cu *ipu;
8821 unsigned int i;
8822 fprintf (stderr, "PRINT_EDGES: %s\n", msg);
8823 fprintf (stderr, "PUs\n");
8824 for (ipu = ipus[0]; ipu; ipu = ipu->next)
8825 dump_edges_1 (ipu);
8826 fprintf (stderr, "CUs\n");
8827 for (i = 0; i < ncus; i++)
8828 dump_edges_1 (ipus[i + npus]);
8829 }
8830
8831 /* Enumerate the different kinds of nodes in the import_cu/import_edge
8832 graph. */
8833 enum node_kind { NODE_CU, NODE_PU_INITIAL, NODE_PU_NEW };
8834
8835 /* Return the node kind for node IDX, given that:
8836 - [0, NPUS - 1] are initial PUs,
8837 - [NPUS, NPUS + NCUS - 1] are CUs, and
8838 - [NPUS + NCUS, ] are new PUs. */
8839 static enum node_kind
get_node_kind(unsigned int idx,unsigned int npus,unsigned int ncus)8840 get_node_kind (unsigned int idx, unsigned int npus, unsigned int ncus)
8841 {
8842 if (idx < npus)
8843 return NODE_PU_INITIAL;
8844 if (idx < npus + ncus)
8845 return NODE_CU;
8846 return NODE_PU_NEW;
8847 }
8848
8849 /* Verify an edge from SRC to DEST during create_import_tree phase PHASE. */
8850 static void
verify_edge(enum node_kind src,enum node_kind dest,unsigned int phase)8851 verify_edge (enum node_kind src, enum node_kind dest, unsigned int phase)
8852 {
8853 if (phase == 1)
8854 {
8855 assert (src == NODE_CU && dest == NODE_PU_INITIAL);
8856 return;
8857 }
8858
8859 assert (IMPLIES (src == NODE_CU, dest != NODE_CU));
8860
8861 if (phase == 2)
8862 {
8863 assert (IMPLIES (src == NODE_PU_NEW, dest == NODE_PU_INITIAL));
8864 assert (src != NODE_PU_INITIAL);
8865 }
8866 else
8867 assert (IMPLIES (src == NODE_PU_NEW, dest != NODE_CU));
8868 }
8869
8870 /* Helper function for debugging create_import_tree. Verify
8871 various invariants for CU/PU IPU. */
8872 static void
verify_edges_1(struct import_cu * ipu,unsigned int * ic,unsigned int * oc,enum node_kind kind,unsigned int npus,unsigned int ncus,unsigned int phase)8873 verify_edges_1 (struct import_cu *ipu, unsigned int *ic, unsigned int *oc,
8874 enum node_kind kind, unsigned int npus, unsigned int ncus,
8875 unsigned int phase)
8876 {
8877 struct import_edge *e1, *e2;
8878 unsigned int last_idx, count;
8879 enum node_kind kind2;
8880
8881 for (last_idx = 0, count = 0, e1 = ipu->incoming;
8882 e1;
8883 last_idx = e1->icu->idx, count++, e1 = e1->next)
8884 {
8885 /* Verify that incoming edges are in ascending idx order. */
8886 assert (count == 0 || e1->icu->idx > last_idx);
8887
8888 /* Verify that each incoming edge has a corresponding outgoing edge. */
8889 for (e2 = e1->icu->outgoing; e2; e2 = e2->next)
8890 if (e2->icu == ipu)
8891 break;
8892 assert (e2);
8893
8894 kind2 = get_node_kind (e1->icu->idx, npus, ncus);
8895 verify_edge (kind2, kind, phase);
8896
8897 if (count == ipu->incoming_count - 1)
8898 assert (ipu->incoming_tail == e1);
8899 }
8900
8901 /* Verify the number of incoming edges. */
8902 assert (ipu->incoming_count == count);
8903
8904 for (last_idx = 0, count = 0, e1 = ipu->outgoing;
8905 e1;
8906 last_idx = e1->icu->idx, count++, e1 = e1->next)
8907 {
8908 /* Verify that outgoing edges are in ascending idx order. */
8909 assert (count == 0 || e1->icu->idx > last_idx);
8910
8911 /* Verify that each outgoing edge has a corresponding incoming edge. */
8912 for (e2 = e1->icu->incoming; e2; e2 = e2->next)
8913 if (e2->icu == ipu)
8914 break;
8915 assert (e2);
8916
8917 kind2 = get_node_kind (e1->icu->idx, npus, ncus);
8918 verify_edge (kind, kind2, phase);
8919 }
8920
8921 /* Verify the number of outgoing edges. */
8922 assert (ipu->outgoing_count == count);
8923
8924 *ic += ipu->incoming_count;
8925 *oc += ipu->outgoing_count;
8926 }
8927
8928 /* Helper function for debugging create_import_tree. Call verify_edges_1
8929 on all CUs and PUs. */
8930 void
verify_edges(struct import_cu ** ipus,unsigned int npus,unsigned int ncus,unsigned int phase)8931 verify_edges (struct import_cu **ipus, unsigned int npus, unsigned int ncus,
8932 unsigned int phase)
8933 {
8934 struct import_cu *ipu;
8935 unsigned int i, ic, oc;
8936
8937 ic = 0;
8938 oc = 0;
8939
8940 /* Verify initial PUs. */
8941 ipu = NULL;
8942 for (i = 0; i < npus; ++i)
8943 {
8944 ipu = ipus[i];
8945 assert (ipu->cu != NULL);
8946 if (i < npus - 1)
8947 assert (ipu->next == ipus[i + 1]);
8948 assert (ipu->incoming != NULL);
8949 if (phase <= 2)
8950 assert (ipu->outgoing == NULL);
8951 verify_edges_1 (ipu, &ic, &oc, NODE_PU_INITIAL, npus, ncus, phase);
8952 }
8953
8954 /* Verify new PUs. */
8955 assert (ipu != NULL);
8956 for (ipu = ipu->next; ipu; ipu = ipu->next)
8957 {
8958 assert (phase != 1);
8959 assert (ipu->cu == NULL);
8960 assert (ipu->incoming != NULL);
8961 assert (ipu->outgoing != NULL);
8962 verify_edges_1 (ipu, &ic, &oc, NODE_PU_NEW, npus, ncus, phase);
8963 }
8964
8965 /* Verify CUs. */
8966 for (i = 0; i < ncus; i++)
8967 {
8968 ipu = ipus[npus + i];
8969 assert (ipu->cu != NULL);
8970 assert (ipu->next == NULL);
8971 assert (ipu->incoming == NULL);
8972 assert (ipu->outgoing != NULL);
8973 verify_edges_1 (ipu, &ic, &oc, NODE_CU, npus, ncus, phase);
8974 }
8975
8976 /* Verify that the overall number of incoming and outgoing edges is
8977 equal. */
8978 assert (ic == oc);
8979 }
8980
8981 #define BITVECTOR_TYPE unsigned int
8982
8983 /* Return a bitvector containing NBITS bits. */
8984 static inline BITVECTOR_TYPE *
bitvector_alloc(unsigned nbits)8985 bitvector_alloc (unsigned nbits)
8986 {
8987 size_t nbytes = (nbits / 8) + 1;
8988 size_t size = nbytes + sizeof (BITVECTOR_TYPE);
8989 BITVECTOR_TYPE *res = (BITVECTOR_TYPE *)malloc (size);
8990 if (res == NULL)
8991 dwz_oom ();
8992 memset (res, 0, size);
8993 return res;
8994 }
8995
8996 /* Set bit IDX in bitvector VECTOR. */
8997 static inline void FORCE_INLINE
bitvector_set_bit(BITVECTOR_TYPE * vector,unsigned idx)8998 bitvector_set_bit (BITVECTOR_TYPE *vector, unsigned idx)
8999 {
9000 unsigned div = idx / (sizeof (BITVECTOR_TYPE) * 8);
9001 unsigned mod = idx % (sizeof (BITVECTOR_TYPE) * 8);
9002 vector[div] |= (1 << mod);
9003 }
9004
9005 /* Test bit IDX in bitvector VECTOR. */
9006 static inline bool FORCE_INLINE
bitvector_bit_p(BITVECTOR_TYPE * vector,unsigned idx)9007 bitvector_bit_p (BITVECTOR_TYPE *vector, unsigned idx)
9008 {
9009 unsigned div = idx / (sizeof (BITVECTOR_TYPE) * 8);
9010 unsigned mod = idx % (sizeof (BITVECTOR_TYPE) * 8);
9011 return (vector[div] & (1 << mod)) != 0;
9012 }
9013
9014 /* Clear at least bits [A, B] in VECTOR, possibly more. */
9015 static inline void FORCE_INLINE
bitvector_clear_bits(BITVECTOR_TYPE * vector,unsigned int a,unsigned int b)9016 bitvector_clear_bits (BITVECTOR_TYPE *vector, unsigned int a, unsigned int b)
9017 {
9018 unsigned int range_min = a / (sizeof (BITVECTOR_TYPE) * 8);
9019 unsigned int range_max = b / (sizeof (BITVECTOR_TYPE) * 8);
9020 memset (&vector[range_min], 0,
9021 (range_max - range_min + 1) * sizeof (BITVECTOR_TYPE));
9022 }
9023
9024 /* Function to optimize the size of DW_TAG_imported_unit DIEs by
9025 creating an inclusion tree, instead of each CU importing all
9026 PUs it needs directly, by optionally creating new PUs or
9027 adding DW_TAG_imported_unit to the already created PUs.
9028 At the end this function constructs any new PUs needed, and
9029 adds DW_TAG_imported_unit DIEs to them as well as the CUs
9030 and partition_dups created PUs. */
9031 static int
create_import_tree(void)9032 create_import_tree (void)
9033 {
9034 dw_cu_ref pu, cu, last_partial_cu = NULL;
9035 unsigned int i, new_pu_version = 2, min_cu_version, npus, ncus;
9036 struct import_cu **ipus, *ipu, *icu;
9037 unsigned int cu_off;
9038 unsigned int puidx;
9039 struct import_cu *last_pu, *pu_freelist = NULL;
9040 unsigned char *to_free;
9041
9042 if (unlikely (progress_p))
9043 {
9044 report_progress ();
9045 fprintf (stderr, "create_import_tree phase 1\n");
9046 }
9047
9048 /* size doesn't count anything already created before this
9049 function (partial units etc.) or already preexisting, just
9050 initially the cumulative sizes of DW_TAG_imported_unit DIEs
9051 that would need to be added, and if some new DW_TAG_partial_unit
9052 CUs are going to be created as a result of this routine, that size
9053 too. DW_TAG_imported_unit has size 5 (for DWARF3+) or 1 + ptr_size
9054 (DWARF2), DW_TAG_partial_unit has size 13/14 (11 CU header + 1 byte
9055 abbrev number + 1 byte child end + 1 byte for DWARF5 unit_type). */
9056 unsigned int size = 0;
9057 /* Size of DW_TAG_imported_unit if the same everywhere, otherwise
9058 (mixing DWARF2 and DWARF3+ with ptr_size != 4) 0. */
9059 unsigned int edge_cost = 0;
9060 /* Number of bytes needed for outgoing edges of PUs created by
9061 this function (which all have DWARF version new_pu_version). */
9062 unsigned int new_edge_cost;
9063
9064 /* If no PUs were created, there is nothing to do here. */
9065 if (first_cu == NULL || (fi_multifile ? alt_first_cu == NULL
9066 : first_cu->cu_kind != CU_PU))
9067 return 0;
9068
9069 edge_freelist = NULL;
9070 to_free = obstack_alloc (&ob2, 1);
9071 min_cu_version = first_cu->cu_version;
9072 /* First construct a bipartite graph between CUs and PUs. */
9073 for (pu = fi_multifile ? alt_first_cu : first_cu, npus = 0;
9074 pu && pu->cu_kind != CU_NORMAL; pu = pu->cu_next)
9075 {
9076 dw_die_ref die, rdie;
9077 dw_cu_ref prev_cu;
9078
9079 if (pu->cu_die->die_tag == DW_TAG_compile_unit)
9080 continue;
9081
9082 last_partial_cu = pu;
9083 for (rdie = pu->cu_die->die_child;
9084 rdie->die_named_namespace; rdie = rdie->die_child)
9085 ;
9086 if (unlikely (fi_multifile) && rdie->die_nextdup == NULL)
9087 {
9088 pu->u1.cu_icu = NULL;
9089 continue;
9090 }
9091 npus++;
9092 if (pu->cu_version > new_pu_version)
9093 new_pu_version = pu->cu_version;
9094 if (pu->cu_version < min_cu_version)
9095 min_cu_version = pu->cu_version;
9096 ipu = (struct import_cu *) obstack_alloc (&ob2, sizeof (*ipu));
9097 memset (ipu, 0, sizeof (*ipu));
9098 ipu->cu = pu;
9099 pu->u1.cu_icu = ipu;
9100 assert (rdie->die_toplevel);
9101 for (die = rdie->die_nextdup, prev_cu = NULL;
9102 die; die = die->die_nextdup)
9103 {
9104 dw_cu_ref diecu = die_cu (die);
9105 if (diecu == prev_cu)
9106 continue;
9107 ipu->incoming_count++;
9108 size += 1 + (diecu->cu_version == 2 ? ptr_size : 4);
9109 prev_cu = diecu;
9110 }
9111 ipu->incoming = (struct import_edge *)
9112 obstack_alloc (&ob2,
9113 ipu->incoming_count
9114 * sizeof (*ipu->incoming));
9115 for (die = rdie->die_nextdup, i = 0, prev_cu = NULL;
9116 die; die = die->die_nextdup)
9117 {
9118 dw_cu_ref diecu = die_cu (die);
9119 if (diecu == prev_cu)
9120 continue;
9121 icu = diecu->u1.cu_icu;
9122 if (icu == NULL)
9123 {
9124 icu = (struct import_cu *)
9125 obstack_alloc (&ob2, sizeof (*ipu));
9126 memset (icu, 0, sizeof (*icu));
9127 icu->cu = diecu;
9128 diecu->u1.cu_icu = icu;
9129 }
9130 ipu->incoming[i++].icu = icu;
9131 icu->outgoing_count++;
9132 prev_cu = diecu;
9133 }
9134 ipu->incoming_tail = &ipu->incoming[ipu->incoming_count - 1];
9135 }
9136 if (npus == 0)
9137 {
9138 obstack_free (&ob2, to_free);
9139 return 0;
9140 }
9141 for (cu = fi_multifile ? first_cu : pu, ncus = 0; cu; cu = cu->cu_next)
9142 if (cu->u1.cu_icu)
9143 {
9144 ncus++;
9145 if (cu->cu_version > new_pu_version)
9146 new_pu_version = cu->cu_version;
9147 if (cu->cu_version < min_cu_version)
9148 min_cu_version = cu->cu_version;
9149 cu->u1.cu_icu->outgoing
9150 = (struct import_edge *)
9151 obstack_alloc (&ob2,
9152 cu->u1.cu_icu->outgoing_count
9153 * sizeof (*cu->u1.cu_icu->outgoing));
9154 cu->u1.cu_icu->outgoing_count = 0;
9155 }
9156 if (ptr_size == 4 || min_cu_version > 2)
9157 edge_cost = 5;
9158 else if (new_pu_version == 2)
9159 edge_cost = 1 + ptr_size;
9160 new_edge_cost = new_pu_version == 2 ? 1 + ptr_size : 5;
9161 for (pu = fi_multifile ? alt_first_cu : first_cu;
9162 pu && pu->cu_kind != CU_NORMAL; pu = pu->cu_next)
9163 {
9164 ipu = pu->u1.cu_icu;
9165 if (ipu == NULL)
9166 continue;
9167 for (i = 0; i < ipu->incoming_count; i++)
9168 {
9169 icu = ipu->incoming[i].icu;
9170 icu->outgoing[icu->outgoing_count++].icu = ipu;
9171 }
9172 }
9173 ipus = (struct import_cu **)
9174 obstack_alloc (&ob2, (npus + ncus) * sizeof (*ipus));
9175 for (pu = fi_multifile ? alt_first_cu : first_cu, npus = 0;
9176 pu && pu->cu_kind != CU_NORMAL; pu = pu->cu_next)
9177 {
9178 ipu = pu->u1.cu_icu;
9179 if (ipu == NULL)
9180 continue;
9181 qsort (ipu->incoming, ipu->incoming_count, sizeof (*ipu->incoming),
9182 import_edge_cmp);
9183 for (i = 0; i < ipu->incoming_count; i++)
9184 {
9185 ipu->incoming[i].next
9186 = i != ipu->incoming_count - 1 ? &ipu->incoming[i + 1] : NULL;
9187 }
9188 ipus[npus++] = ipu;
9189 }
9190 for (cu = fi_multifile ? first_cu : pu, ncus = 0; cu; cu = cu->cu_next)
9191 if (cu->u1.cu_icu)
9192 {
9193 icu = cu->u1.cu_icu;
9194 qsort (icu->outgoing, icu->outgoing_count, sizeof (*icu->outgoing),
9195 import_edge_cmp);
9196 for (i = 0; i < icu->outgoing_count - 1; i++)
9197 icu->outgoing[i].next = &icu->outgoing[i + 1];
9198 icu->outgoing[i].next = NULL;
9199 ipus[npus + ncus] = icu;
9200 ncus++;
9201 }
9202 qsort (ipus, npus, sizeof (*ipus), import_cu_cmp);
9203 qsort (ipus + npus, ncus, sizeof (*ipus), import_cu_cmp);
9204 for (puidx = 0; puidx < npus; puidx++)
9205 {
9206 ipus[puidx]->idx = puidx;
9207 if (puidx + 1 < npus)
9208 ipus[puidx]->next = ipus[puidx + 1];
9209 }
9210 for (; puidx < npus + ncus; puidx++)
9211 ipus[puidx]->idx = puidx;
9212 last_pu = ipus[npus - 1];
9213 if (unlikely (dump_edges_p))
9214 dump_edges ("phase 1", ipus, npus, ncus);
9215 if (unlikely (verify_edges_p))
9216 verify_edges (ipus, npus, ncus, 1);
9217 if (!import_opt_p)
9218 goto opt_done;
9219 if (unlikely (progress_p))
9220 {
9221 report_progress ();
9222 fprintf (stderr, "create_import_tree phase 2\n");
9223 }
9224 /* Now, for the above constructed bipartite graph, find K x,2 components
9225 where x >= 5 (for DWARF3 and above or ptr_size 4, for DWARF2 and
9226 ptr_size 8 it can be even x == 4) and add a new PU node, where all
9227 CUs from the component will point to the new PU node and that new PU
9228 will point to all the destination PUs. In theory with DWARF2
9229 and ptr_size 1 we could need x >= 9.
9230
9231 The example below demonstrates the type of transformation. The
9232 transformation is an optimization if the benefit of reducing the number
9233 of imports (in other words, edges) is bigger than the cost of adding an
9234 extra PU. OTOH, the transformation can be done in the presence of
9235 additional incoming edges for PU_3 and PU_4.
9236
9237 Before: After:
9238
9239 CU_1---------->PU_3 CU_1 PU_3
9240 \ ^ ^ \ ^
9241 \ / / \ /
9242 \ / / \ /
9243 x----o / \ /
9244 / \ / \ /
9245 / \ / \ /
9246 / \ / v /
9247 CU_2 x CU_2----->PU_5
9248 \ / \ ^ \
9249 \ / \ / \
9250 \ / \ / \
9251 x----o \ / \
9252 / \ \ / \
9253 / \ \ / \
9254 / v v / v
9255 CU_3---------->PU_4 CU_3 PU_4
9256 */
9257 for (i = 0; i < npus - 1; i++)
9258 {
9259 struct import_cu *pudst[2], *pusrc[10];
9260 struct import_edge *e1, *e2, *e3, *e4;
9261 struct import_edge *e1next, *e2next, *e3next;
9262 pudst[0] = ipus[i];
9263 for (e1 = pudst[0]->incoming; e1; e1 = e1next)
9264 {
9265 e1next = e1->next;
9266 if (e1->icu->cu == NULL)
9267 break;
9268 for (e2 = e1->icu->outgoing; e2; e2 = e2next)
9269 {
9270 unsigned int srccount, dstcount, cost;
9271 struct import_cu *npu = NULL;
9272 struct import_edge **ep = NULL;
9273
9274 e2next = e2->next;
9275 if (e2->icu->idx <= pudst[0]->idx)
9276 continue;
9277 if (e2->icu->cu == NULL)
9278 break;
9279
9280 pudst[1] = e2->icu;
9281 pusrc[0] = e1->icu;
9282 srccount = 1;
9283 cost = edge_cost;
9284 if (!edge_cost)
9285 cost = pusrc[0]->cu->cu_version == 2 ? 1 + ptr_size : 5;
9286 for (e3 = e1next; e3; e3 = e3next)
9287 {
9288 e3next = e3->next;
9289 if (e3->icu->cu == NULL)
9290 break;
9291 dstcount = 0;
9292 for (e4 = e3->icu->outgoing; e4; e4 = e4->next)
9293 {
9294 if (e4->icu == pudst[0])
9295 dstcount++;
9296 else if (e4->icu == pudst[1])
9297 {
9298 dstcount++;
9299 break;
9300 }
9301 else if (e4->icu->idx > pudst[1]->idx)
9302 break;
9303 }
9304 if (dstcount != 2)
9305 continue;
9306 if (npu == NULL)
9307 {
9308 unsigned int header_size;
9309 pusrc[srccount] = e3->icu;
9310 header_size = (pusrc[srccount]->cu->cu_version >= 5
9311 ? 14 : 13); /* DWARF5 unit_type byte. */
9312 cost += edge_cost;
9313 if (!edge_cost)
9314 cost += pusrc[srccount]->cu->cu_version == 2
9315 ? 1 + ptr_size : 5;
9316 srccount++;
9317 if (ignore_size || ((dstcount - 1) * cost
9318 > (header_size
9319 + dstcount * new_edge_cost)))
9320 {
9321 unsigned int j;
9322
9323 e2next = NULL;
9324 if (pu_freelist)
9325 {
9326 npu = pu_freelist;
9327 pu_freelist = pu_freelist->next;
9328 }
9329 else
9330 npu = (struct import_cu *)
9331 obstack_alloc (&ob2, sizeof (*npu));
9332 memset (npu, 0, sizeof (*npu));
9333 npu->incoming_count = srccount;
9334 npu->outgoing_count = dstcount;
9335 npu->idx = puidx++;
9336 last_pu->next = npu;
9337 last_pu = npu;
9338 for (j = 0; j < srccount; j++)
9339 {
9340 if (e1next && e1next->icu == pusrc[j])
9341 e1next = e1next->next;
9342 remove_import_edges (&pusrc[j]->outgoing, NULL,
9343 pudst, dstcount, true)->icu
9344 = npu;
9345 pusrc[j]->outgoing_count -= dstcount - 1;
9346 }
9347 for (j = 0; j < dstcount; j++)
9348 {
9349 remove_import_edges (&pudst[j]->incoming,
9350 &pudst[j]->incoming_tail,
9351 pusrc, srccount, true)->icu
9352 = npu;
9353 pudst[j]->incoming_count -= srccount - 1;
9354 }
9355 npu->incoming = first_edge_from_freelist ();
9356 for (j = 0, e4 = npu->incoming; j < srccount; j++)
9357 {
9358 e4->icu = pusrc[j];
9359 if (j == srccount - 1)
9360 {
9361 npu->incoming
9362 = last_edge_from_freelist (npu->incoming,
9363 e4);
9364 npu->incoming_tail = e4;
9365 ep = &e4->next;
9366 }
9367 else
9368 e4 = e4->next;
9369 }
9370 npu->outgoing = first_edge_from_freelist ();
9371 for (j = 0, e4 = npu->outgoing; j < dstcount; j++)
9372 {
9373 e4->icu = pudst[j];
9374 if (j == dstcount - 1)
9375 npu->outgoing
9376 = last_edge_from_freelist (npu->outgoing, e4);
9377 else
9378 e4 = e4->next;
9379 }
9380 size -= (dstcount - 1) * cost;
9381 size += 13 + dstcount * new_edge_cost;
9382 }
9383 }
9384 else
9385 {
9386 unsigned int j;
9387
9388 pusrc[srccount] = e3->icu;
9389 cost = edge_cost;
9390 if (!edge_cost)
9391 cost = pusrc[srccount]->cu->cu_version == 2
9392 ? 1 + ptr_size : 5;
9393 if (e1next && e1next->icu == pusrc[srccount])
9394 e1next = e1next->next;
9395 remove_import_edges (&pusrc[srccount]->outgoing, NULL,
9396 pudst, dstcount, true)->icu = npu;
9397 pusrc[srccount]->outgoing_count -= dstcount - 1;
9398 for (j = 0; j < dstcount; j++)
9399 {
9400 remove_import_edges (&pudst[j]->incoming,
9401 &pudst[j]->incoming_tail,
9402 pusrc + srccount, 1, false);
9403 pudst[j]->incoming_count--;
9404 }
9405 *ep = edge_from_freelist ();
9406 npu->incoming_count++;
9407 (*ep)->icu = pusrc[srccount];
9408 (*ep)->next = NULL;
9409 npu->incoming_tail = *ep;
9410 ep = &(*ep)->next;
9411 size -= (dstcount - 1) * cost;
9412 }
9413 }
9414 }
9415 }
9416 }
9417 if (unlikely (dump_edges_p))
9418 dump_edges ("phase 2", ipus, npus, ncus);
9419 if (unlikely (verify_edges_p))
9420 verify_edges (ipus, npus, ncus, 2);
9421 if (unlikely (progress_p))
9422 {
9423 report_progress ();
9424 fprintf (stderr, "create_import_tree phase 3\n");
9425 }
9426 /* Try to merge PUs which have the same set of referrers if
9427 beneficial.
9428
9429 The example below demonstrates the type of transformation. The
9430 transformation is an optimization because it reduces the number of import
9431 statements (in other words, edges) as well as the number of PUs. It can
9432 however not be done if PU_3 or PU_4 have additional incoming edges.
9433
9434 Before: After:
9435
9436 CU_1----->PU_3 CU_1
9437 \ ^ \
9438 \ / \
9439 \ / v
9440 x PU_3_4
9441 / \ ^
9442 / \ /
9443 / v /
9444 CU_2----->PU_4 CU_2
9445
9446 Or, if one PU has a subset of referrers of the other, attempt to replace
9447 all the incoming edges from the referrers intersection to the PU with
9448 larger number of incoming edges by an edge from the other PU.
9449
9450 The example below demonstrates the type of transformation. The
9451 transformation is an optimization because it reduces the number of import
9452 statements (in other words, edges). It can however not be done if PU_3
9453 has additional incoming edges.
9454
9455 Before: After:
9456
9457 CU_1----->PU_3 CU_1------>PU_3
9458 \ ^ ^ |
9459 \ / / |
9460 \ / / |
9461 x / |
9462 / \ / |
9463 / \ / |
9464 / \ / |
9465 CU_2 \ CU_2 o
9466 \ \ |
9467 \ o |
9468 \ | |
9469 \ | |
9470 \ | |
9471 \ | |
9472 v v v
9473 CU_3----->PU_4 CU_3------>PU_4
9474 */
9475 /* Flag used during PU merging, set for PUs already considered
9476 for merging for the given first PU. */
9477 BITVECTOR_TYPE *seen = bitvector_alloc (puidx);
9478 unsigned int min_seen = UINT_MAX;
9479 unsigned int max_seen = 0;
9480 for (ipu = ipus[0]; ipu; ipu = ipu->next)
9481 {
9482 struct import_edge *e1, *e2, *e3, *e4, **e1p, **ep, *prev;
9483 for (e1p = &ipu->incoming, e1 = *e1p;
9484 e1; e1 = *e1p != e1 ? *e1p : (e1p = &e1->next, e1->next))
9485 {
9486 for (e2 = e1->icu->outgoing; e2; e2 = e2->next)
9487 {
9488 unsigned int size_inc, size_dec;
9489 struct import_cu *ipu2 = e2->icu, *ipusub, *ipusup;
9490 /* True if IPU's src set might be a subset
9491 of IPU2's src set. */
9492 bool maybe_subset;
9493 /* True if IPU's src set might be a superset
9494 of IPU2's src set. */
9495 bool maybe_superset;
9496 unsigned int intersection;
9497
9498 if (ipu2->idx <= ipu->idx || bitvector_bit_p (seen, ipu2->idx))
9499 continue;
9500 bitvector_set_bit (seen, ipu2->idx);
9501 min_seen = MIN (min_seen, ipu2->idx);
9502 max_seen = MAX (max_seen, ipu2->idx);
9503 maybe_subset = (e1 == ipu->incoming
9504 && ipu->incoming_count <= ipu2->incoming_count);
9505 maybe_superset = ipu->incoming_count >= ipu2->incoming_count;
9506 if (maybe_superset)
9507 {
9508 /* If the referrer nodes of ipu are a superset of the
9509 referrer nodes of ipu2, then ipu's last referrer node
9510 should have index larger or equal to the last referrer
9511 node of ipu2. */
9512 maybe_superset
9513 = (ipu->incoming_tail->icu->idx
9514 >= ipu2->incoming_tail->icu->idx);
9515 }
9516 if (maybe_subset)
9517 {
9518 /* If the referrer nodes of ipu are a subset of the
9519 referrer nodes of ipu2, then ipu's last referrer node
9520 should have index smaller or equal to the last referrer
9521 node of ipu2. */
9522 maybe_subset
9523 = (ipu->incoming_tail->icu->idx
9524 <= ipu2->incoming_tail->icu->idx);
9525 }
9526 e3 = e1;
9527 e4 = ipu2->incoming;
9528 intersection = 0;
9529 while ((maybe_subset || maybe_superset) && e3 && e4)
9530 {
9531 if (e3->icu == e4->icu)
9532 {
9533 intersection++;
9534 e3 = e3->next;
9535 e4 = e4->next;
9536 continue;
9537 }
9538 if (e3->icu->idx < e4->icu->idx)
9539 {
9540 maybe_subset = false;
9541 e3 = e3->next;
9542 continue;
9543 }
9544 maybe_superset = false;
9545 e4 = e4->next;
9546 }
9547 if (e3)
9548 maybe_subset = false;
9549 if (e4)
9550 maybe_superset = false;
9551 if ((!maybe_superset && !maybe_subset) || intersection < 2)
9552 continue;
9553 if (maybe_superset && maybe_subset)
9554 {
9555 if (unlikely (fi_multifile) && ipu2->idx < npus + ncus)
9556 continue;
9557 if (odr_active_p && odr_mode != ODR_BASIC
9558 && ipu2->idx < npus + ncus)
9559 continue;
9560 /* If IPU and IPU2 have the same set of src nodes, then
9561 (if beneficial, with edge_cost != 0 always), merge
9562 IPU2 node into IPU, by removing all incoming edges
9563 of IPU2 and moving over all outgoing edges of IPU2
9564 to IPU. */
9565 assert (ipu2->idx >= npus + ncus);
9566 size_inc = 0;
9567 if (edge_cost)
9568 size_dec = 13 + ipu2->incoming_count * edge_cost;
9569 else
9570 {
9571 size_dec = 13;
9572 if (ipu->cu && ipu->cu->cu_version == 2)
9573 {
9574 if (ptr_size > 4)
9575 size_inc = ipu2->outgoing_count * (ptr_size - 4);
9576 else
9577 size_dec += ipu2->outgoing_count * (4 - ptr_size);
9578 }
9579 for (e4 = ipu2->incoming; e4; e4 = e4->next)
9580 size_dec += (e4->icu->cu
9581 && e4->icu->cu->cu_version == 2)
9582 ? 1 + ptr_size : 5;
9583 }
9584 if (!ignore_size || size_dec > size_inc)
9585 {
9586 struct import_cu **ipup;
9587 for (e4 = ipu2->incoming, e3 = NULL; e4; e4 = e4->next)
9588 {
9589 remove_import_edges (&e4->icu->outgoing, NULL, &ipu2,
9590 1, false);
9591 e4->icu->outgoing_count--;
9592 prepare_free_edge (e4);
9593 e3 = e4;
9594 }
9595 free_edges (ipu2->incoming, e3);
9596 for (e4 = ipu2->outgoing; e4; e4 = e4->next)
9597 {
9598 for (ep = &e4->icu->incoming; *ep; ep = &(*ep)->next)
9599 if ((*ep)->icu->idx >= ipu->idx)
9600 break;
9601 assert ((*ep)->icu != ipu);
9602 if ((*ep)->icu == ipu2)
9603 (*ep)->icu = ipu;
9604 else
9605 {
9606 struct import_edge **ep2;
9607 for (ep2 = &(*ep)->next;
9608 *ep2; ep2 = &(*ep2)->next)
9609 if ((*ep2)->icu == ipu2)
9610 break;
9611 e3 = *ep2;
9612 *ep2 = e3->next;
9613 e3->next = *ep;
9614 *ep = e3;
9615 e3->icu = ipu;
9616 while (e4->icu->incoming_tail->next != NULL)
9617 e4->icu->incoming_tail
9618 = e4->icu->incoming_tail->next;
9619 }
9620 }
9621 e3 = ipu->outgoing;
9622 ep = &ipu->outgoing;
9623 for (e4 = ipu2->outgoing; e3 && e4; )
9624 if (e3->icu->idx < e4->icu->idx)
9625 {
9626 *ep = e3;
9627 ep = &e3->next;
9628 e3 = e3->next;
9629 }
9630 else
9631 {
9632 assert (e3->icu != e4->icu);
9633 *ep = e4;
9634 ep = &e4->next;
9635 e4 = e4->next;
9636 }
9637 if (e3)
9638 *ep = e3;
9639 else if (e4)
9640 *ep = e4;
9641 else
9642 *ep = NULL;
9643 ipu->outgoing_count += ipu2->outgoing_count;
9644 size -= size_dec - size_inc;
9645 if (ipu->idx >= npus + ncus)
9646 ipup = &ipu->next;
9647 else
9648 ipup = &ipus[npus - 1]->next;
9649 while (*ipup != ipu2)
9650 ipup = &(*ipup)->next;
9651 *ipup = ipu2->next;
9652 ipu2->next = pu_freelist;
9653 pu_freelist = ipu2;
9654 continue;
9655 }
9656 }
9657 if (maybe_superset)
9658 {
9659 ipusup = ipu;
9660 ipusub = ipu2;
9661 }
9662 else
9663 {
9664 ipusub = ipu;
9665 ipusup = ipu2;
9666 }
9667 /* If IPUSUB's src set is a subset of IPUSUP's src set
9668 and intersection is at least 2, remove edges from
9669 IPUSUB's src set to IPUSUP node and instead add
9670 an edge from IPUSUB to IPUSUP. */
9671 size_inc = 0;
9672 if (edge_cost)
9673 size_dec = (ipusub->incoming_count - 1) * edge_cost;
9674 else
9675 {
9676 size_inc = ipusub->cu && ipusub->cu->cu_version == 2
9677 ? 1 + ptr_size : 5;
9678 size_dec = 0;
9679 for (e3 = ipusub->incoming; e3; e3 = e3->next)
9680 size_dec += (e3->icu->cu
9681 && e3->icu->cu->cu_version == 2)
9682 ? 1 + ptr_size : 5;
9683 }
9684 if (size_dec > size_inc
9685 && (!fi_multifile || ipusub->idx >= npus + ncus))
9686 {
9687 for (e3 = ipusub->incoming, ep = &ipusup->incoming,
9688 prev = NULL;
9689 e3; e3 = e3->next)
9690 {
9691 remove_import_edges (&e3->icu->outgoing, NULL, &ipusup, 1,
9692 false);
9693 e3->icu->outgoing_count--;
9694 while ((*ep)->icu != e3->icu)
9695 {
9696 prev = *ep;
9697 ep = &(*ep)->next;
9698 }
9699 e4 = *ep;
9700 *ep = e4->next;
9701 free_edge (e4);
9702 if (ipusup->incoming_tail == e4)
9703 ipusup->incoming_tail = prev;
9704 }
9705 for (ep = &ipusub->outgoing; *ep; ep = &(*ep)->next)
9706 if ((*ep)->icu->idx >= ipusup->idx)
9707 break;
9708 assert (*ep == NULL || (*ep)->icu != ipusup);
9709 e4 = edge_from_freelist ();
9710 e4->icu = ipusup;
9711 e4->next = *ep;
9712 *ep = e4;
9713 ipusub->outgoing_count++;
9714 for (ep = &ipusup->incoming; *ep; ep = &(*ep)->next)
9715 if ((*ep)->icu->idx >= ipusub->idx)
9716 break;
9717 assert (*ep == NULL || (*ep)->icu != ipusub);
9718 e4 = edge_from_freelist ();
9719 e4->icu = ipusub;
9720 e4->next = *ep;
9721 *ep = e4;
9722 if (ipusup->incoming_tail->next == e4)
9723 ipusup->incoming_tail = e4;
9724 ipusup->incoming_count -= ipusub->incoming_count - 1;
9725 size -= size_dec - size_inc;
9726 if (ipusup == ipu)
9727 break;
9728 }
9729 }
9730 }
9731 if (min_seen <= max_seen)
9732 {
9733 bitvector_clear_bits (seen, min_seen, max_seen);
9734 min_seen = UINT_MAX;
9735 max_seen = 0;
9736 }
9737 }
9738 free (seen);
9739 if (unlikely (dump_edges_p))
9740 dump_edges ("phase 3", ipus, npus, ncus);
9741 if (unlikely (verify_edges_p))
9742 verify_edges (ipus, npus, ncus, 3);
9743 opt_done:
9744 if (unlikely (progress_p))
9745 {
9746 report_progress ();
9747 fprintf (stderr, "create_import_tree phase 4 (create partial units)\n");
9748 }
9749 /* Create DW_TAG_partial_unit (and containing dw_cu structures). */
9750 if (fi_multifile)
9751 {
9752 cu_off = 0;
9753 last_partial_cu = NULL;
9754 }
9755 else
9756 cu_off = last_partial_cu->cu_offset + 1;
9757 for (ipu = ipus[npus - 1]->next; ipu; ipu = ipu->next)
9758 {
9759 dw_die_ref die;
9760 dw_cu_ref partial_cu = pool_alloc (dw_cu, sizeof (struct dw_cu));
9761 memset (partial_cu, '\0', sizeof (*partial_cu));
9762 partial_cu->cu_kind = CU_PU;
9763 partial_cu->cu_offset = cu_off++;
9764 partial_cu->cu_version = new_pu_version;
9765 partial_cu->u1.cu_icu = ipu;
9766 if (unlikely (last_partial_cu == NULL))
9767 {
9768 partial_cu->cu_next = first_cu;
9769 first_cu = partial_cu;
9770 }
9771 else
9772 {
9773 partial_cu->cu_next = last_partial_cu->cu_next;
9774 last_partial_cu->cu_next = partial_cu;
9775 }
9776 last_partial_cu = partial_cu;
9777 die = pool_alloc (dw_die, sizeof (struct dw_die));
9778 memset (die, '\0', sizeof (struct dw_die));
9779 die->die_toplevel = 1;
9780 partial_cu->cu_die = die;
9781 die->die_tag = DW_TAG_partial_unit;
9782 die->die_offset = -1U;
9783 die->die_root = 1;
9784 die->die_parent = (dw_die_ref) partial_cu;
9785 die->die_size = 1;
9786 ipu->cu = partial_cu;
9787 }
9788 /* Next add all needed DW_TAG_imported_unit DIEs. */
9789 for (cu = first_cu; cu; cu = cu->cu_next)
9790 {
9791 struct import_edge *e;
9792
9793 icu = cu->u1.cu_icu;
9794 if (icu == NULL)
9795 continue;
9796 for (e = icu->outgoing; e; e = e->next)
9797 {
9798 dw_die_ref *diep;
9799 dw_die_ref die = pool_alloc (dw_die, sizeof (struct dw_die));
9800 memset (die, '\0', sizeof (*die));
9801 die->die_toplevel = 1;
9802 die->die_tag = DW_TAG_imported_unit;
9803 die->die_offset = -1U;
9804 die->die_nextdup = e->icu->cu->cu_die;
9805 die->die_parent = cu->cu_die;
9806 assert (e->icu->cu->cu_die->die_tag == DW_TAG_partial_unit);
9807 die->die_size = (cu->cu_version == 2 ? 1 + ptr_size : 5);
9808 /* Put the new DW_TAG_imported_unit DIE after all typed DWARF
9809 stack referenced base types and after all previously added
9810 new DW_TAG_imported_unit DIEs. */
9811 for (diep = &die->die_parent->die_child;
9812 *diep; diep = &(*diep)->die_sib)
9813 if (!(*diep)->die_op_type_referenced
9814 && ((*diep)->die_tag != DW_TAG_imported_unit
9815 || (*diep)->die_offset != -1U))
9816 break;
9817 die->die_sib = *diep;
9818 *diep = die;
9819 }
9820 }
9821 for (cu = first_cu; cu; cu = cu->cu_next)
9822 cu->u1.cu_icu = NULL;
9823 if (unlikely (fi_multifile))
9824 for (cu = alt_first_cu; cu; cu = cu->cu_next)
9825 cu->u1.cu_icu = NULL;
9826 obstack_free (&ob2, to_free);
9827 return 0;
9828 }
9829
9830 /* Helper function for die_find_dup, when ORIG has collapsed children. */
9831 static dw_die_ref
die_find_collapsed_dup(dw_die_ref die,unsigned int * tick)9832 die_find_collapsed_dup (dw_die_ref die, unsigned int *tick)
9833 {
9834 dw_die_ref child, ret;
9835
9836 for (child = die->die_child; child; child = child->die_sib)
9837 if ((*tick)-- == 0)
9838 return child;
9839 else if (child->die_child == NULL)
9840 continue;
9841 else if ((ret = die_find_collapsed_dup (child, tick)) != NULL)
9842 return ret;
9843 (*tick)--;
9844 return NULL;
9845 }
9846
9847 /* If DIE is equal to ORIG, return DUP, otherwise if DIE is
9848 a child of ORIG, return corresponding child in DUP's subtree,
9849 or return NULL. */
9850 static dw_die_ref
die_find_dup(dw_die_ref orig,dw_die_ref dup,dw_die_ref die)9851 die_find_dup (dw_die_ref orig, dw_die_ref dup, dw_die_ref die)
9852 {
9853 dw_die_ref orig_child, dup_child;
9854 if (orig == die)
9855 return dup;
9856 if (orig->die_collapsed_children)
9857 {
9858 dw_die_ref ret;
9859 unsigned int tick;
9860 if (die->die_collapsed_child)
9861 tick = die->die_tag - 1;
9862 else
9863 tick = die->die_ref_seen - 1;
9864 assert (dup->die_collapsed_children == 0
9865 && die->die_parent == orig);
9866 ret = die_find_collapsed_dup (dup, &tick);
9867 assert (die->die_collapsed_child || ret->die_tag == die->die_tag);
9868 return ret;
9869 }
9870 for (orig_child = orig->die_child, dup_child = dup->die_child;
9871 orig_child;
9872 orig_child = orig_child->die_sib, dup_child = dup_child->die_sib)
9873 {
9874 dw_die_ref ret = die_find_dup (orig_child, dup_child, die);
9875 if (ret)
9876 return ret;
9877 }
9878 return NULL;
9879 }
9880
9881 /* Return number of bytes needed to encode VAL using
9882 uleb128. */
9883 static unsigned int
size_of_uleb128(uint64_t val)9884 size_of_uleb128 (uint64_t val)
9885 {
9886 unsigned int size;
9887 for (size = 1; (val >>= 7) != 0; size++)
9888 ;
9889 return size;
9890 }
9891
9892 /* Return number of bytes needed to encode VAL using
9893 sleb128. */
9894 static unsigned int
size_of_sleb128(int64_t val)9895 size_of_sleb128 (int64_t val)
9896 {
9897 unsigned int size = 0;
9898 unsigned char c;
9899 do
9900 {
9901 c = val & 0x7f;
9902 val >>= 7;
9903 size++;
9904 }
9905 while ((val != 0 || (c & 0x40) != 0)
9906 && (val != -1 || (c & 0x40) == 0));
9907 return size;
9908 }
9909
9910 /* Hash table mapping original file IDs to new ids. */
9911 static htab_t line_htab;
9912 /* Current new maximum file ID. */
9913 static unsigned int max_line_id;
9914
9915 struct line_entry
9916 {
9917 /* File pointer. */
9918 struct dw_file *file;
9919 /* Precomputed hash value. */
9920 unsigned int hash;
9921 /* Corresponding new file ID. */
9922 unsigned int new_id;
9923 };
ALIGN_STRUCT(line_entry)9924 ALIGN_STRUCT (line_entry)
9925
9926 /* Hash function in line_htab. */
9927 static hashval_t
9928 line_hash (const void *p)
9929 {
9930 struct line_entry *s = (struct line_entry *)p;
9931
9932 return s->hash;
9933 }
9934
9935 /* Equality function in line_htab. */
9936 static int
line_eq(const void * p,const void * q)9937 line_eq (const void *p, const void *q)
9938 {
9939 struct line_entry *s1 = (struct line_entry *)p;
9940 struct line_entry *s2 = (struct line_entry *)q;
9941
9942 if (s1->hash != s2->hash)
9943 return 0;
9944 if (s1->file == s2->file)
9945 return 1;
9946 if (strcmp (s1->file->file, s2->file->file) != 0)
9947 return 0;
9948 if ((s1->file->dir == NULL) ^ (s2->file->dir == NULL))
9949 return 0;
9950 if (s1->file->dir && strcmp (s1->file->dir, s2->file->dir) != 0)
9951 return 0;
9952 return s1->file->time == s2->file->time && s1->file->size == s2->file->size;
9953 }
9954
9955 /* Map original file ID to new file ID. */
9956 static unsigned int
line_htab_lookup(dw_cu_ref cu,unsigned int id)9957 line_htab_lookup (dw_cu_ref cu, unsigned int id)
9958 {
9959 void **slot;
9960 struct line_entry le;
9961 hashval_t h;
9962
9963 if (id == 0)
9964 return 0;
9965 assert (id <= cu->cu_nfiles);
9966 le.file = &cu->cu_files[id - 1];
9967 h = iterative_hash_object (le.file->time, 0);
9968 h = iterative_hash_object (le.file->size, h);
9969 h = iterative_hash (le.file->file, strlen (le.file->file) + 1, h);
9970 if (le.file->dir)
9971 h = iterative_hash (le.file->dir, strlen (le.file->dir) + 1, h);
9972 if (line_htab == NULL)
9973 {
9974 line_htab = htab_try_create (50, line_hash, line_eq, NULL);
9975 if (line_htab == NULL)
9976 dwz_oom ();
9977 max_line_id = 1;
9978 }
9979 le.hash = h;
9980 slot = htab_find_slot_with_hash (line_htab, &le, h, INSERT);
9981 if (slot == NULL)
9982 dwz_oom ();
9983 if (*slot == NULL)
9984 {
9985 struct line_entry *l = pool_alloc (line_entry, sizeof (*l));
9986 l->file = le.file;
9987 l->hash = h;
9988 l->new_id = max_line_id++;
9989 *slot = (void *) l;
9990 return l->new_id;
9991 }
9992 else
9993 return ((struct line_entry *) *slot)->new_id;
9994 }
9995
9996 /* Hash table for finding duplicate .debug_macro opcode sequences.
9997 This hash table is used with two different sets of hash/equality
9998 callbacks. One is used either within handle_macro function (from within
9999 optimize_multifile), or from handle_macro onwards (read_multifile).
10000 The second set is used from read_macro onwards during fi_multifile. */
10001 static htab_t macro_htab;
10002
10003 /* At the end of read_multifile macro_htab is copied to this variable. */
10004 static htab_t alt_macro_htab;
10005
10006 struct macro_entry
10007 {
10008 /* Start of the sequence. */
10009 unsigned char *ptr;
10010 /* Precomputed hash value. LSB bit is used for a flag whether
10011 a particular .debug_macro sequence is seen more than once. */
10012 unsigned int hash;
10013 /* And it's length or 0 if non-shareable. */
10014 unsigned int len;
10015 };
ALIGN_STRUCT(macro_entry)10016 ALIGN_STRUCT (macro_entry)
10017
10018 /* Hash function in macro_htab. */
10019 static hashval_t
10020 macro_hash (const void *p)
10021 {
10022 struct macro_entry *m = (struct macro_entry *)p;
10023
10024 return m->hash & ~1U;
10025 }
10026
10027 /* Equality function in macro_htab. */
10028 static int
macro_eq(const void * p,const void * q)10029 macro_eq (const void *p, const void *q)
10030 {
10031 struct macro_entry *m1 = (struct macro_entry *)p;
10032 struct macro_entry *m2 = (struct macro_entry *)q;
10033 unsigned char *p1, *p2, *s1, op;
10034 unsigned int strp1, strp2;
10035
10036 if (m1->hash != m2->hash || m1->len != m2->len)
10037 return 0;
10038 if (rd_multifile)
10039 return 0;
10040
10041 s1 = m1->ptr;
10042 p2 = m2->ptr;
10043 p1 = s1 + 3;
10044
10045 while (1)
10046 {
10047 op = read_8 (p1);
10048 if (op == 0)
10049 break;
10050
10051 switch (op)
10052 {
10053 case DW_MACRO_define:
10054 case DW_MACRO_undef:
10055 skip_leb128 (p1);
10056 p1 = (unsigned char *) strchr ((char *) p1, '\0') + 1;
10057 break;
10058 case DW_MACRO_define_strp:
10059 case DW_MACRO_undef_strp:
10060 skip_leb128 (p1);
10061 if (memcmp (s1, p2, p1 - s1) != 0)
10062 return 0;
10063 p2 += p1 - s1;
10064 strp1 = read_32 (p1);
10065 strp2 = read_32 (p2);
10066 if (op_multifile)
10067 {
10068 if (strcmp ((char *) debug_sections[DEBUG_STR].data + strp1,
10069 (char *) debug_sections[DEBUG_STR].data + strp2)
10070 != 0)
10071 return 0;
10072 }
10073 else if (lookup_strp_offset (strp2) != strp1)
10074 return 0;
10075 s1 = p1;
10076 break;
10077 default:
10078 abort ();
10079 }
10080 }
10081 return memcmp (s1, p2, p1 - s1) == 0;
10082 }
10083
10084 /* Hash function in macro_htab. */
10085 static hashval_t
macro_hash2(const void * p)10086 macro_hash2 (const void *p)
10087 {
10088 struct macro_entry *m = (struct macro_entry *)p;
10089
10090 return m->ptr - debug_sections[DEBUG_MACRO].data;
10091 }
10092
10093 /* Equality function in macro_htab. */
10094 static int
macro_eq2(const void * p,const void * q)10095 macro_eq2 (const void *p, const void *q)
10096 {
10097 struct macro_entry *m1 = (struct macro_entry *)p;
10098 struct macro_entry *m2 = (struct macro_entry *)q;
10099 return m1->ptr == m2->ptr;
10100 }
10101
10102 /* Parse .debug_macro section, either during write_multifile
10103 or during fi_multifile phase. During write_multifile it
10104 selects potentially shareable .debug_macro sequences and
10105 writes them into debug_sections[DEBUG_MACRO].new_data
10106 block it allocates. During fi_multifile it populates
10107 macro_htab. In both cases it calls note_strp_offset
10108 on DW_FORM_strp offsets. */
10109 static int
read_macro(DSO * dso)10110 read_macro (DSO *dso)
10111 {
10112 unsigned char *ptr, *endsec, *dst = NULL;
10113 unsigned int version, flags, op, strp;
10114 struct macro_entry me, *m;
10115
10116 ptr = debug_sections[DEBUG_MACRO].data;
10117 endsec = ptr + debug_sections[DEBUG_MACRO].size;
10118 debug_sections[DEBUG_MACRO].new_size = 0;
10119 if (!wr_multifile)
10120 {
10121 macro_htab = htab_try_create (50, macro_hash2, macro_eq2, NULL);
10122 if (macro_htab == NULL)
10123 dwz_oom ();
10124 }
10125
10126 while (ptr < endsec)
10127 {
10128 unsigned char *start = ptr, *s = ptr;
10129 bool can_share = true;
10130 hashval_t hash = 0;
10131 unsigned int strp;
10132 void **slot;
10133
10134 if (ptr + 4 > endsec)
10135 {
10136 error (0, 0, "%s: .debug_macro header too small", dso->filename);
10137 return 1;
10138 }
10139
10140 version = read_16 (ptr);
10141 bool supported_version_p = version >= 4 && version <= 5;
10142 if (!supported_version_p)
10143 {
10144 error (0, 0, "%s: Unhandled .debug_macro version %d", dso->filename,
10145 version);
10146 return 1;
10147 }
10148 flags = read_8 (ptr);
10149 if ((flags & ~2U) != 0)
10150 {
10151 error (0, 0, "%s: Unhandled .debug_macro flags %d", dso->filename,
10152 flags);
10153 return 1;
10154 }
10155 if ((flags & 2) != 0)
10156 {
10157 ptr += 4;
10158 can_share = false;
10159 }
10160 if (fi_multifile && alt_macro_htab == NULL)
10161 can_share = false;
10162
10163 op = -1U;
10164 while (ptr < endsec)
10165 {
10166 op = read_8 (ptr);
10167 if (op == 0)
10168 break;
10169
10170 switch (op)
10171 {
10172 case DW_MACRO_define:
10173 case DW_MACRO_undef:
10174 skip_leb128 (ptr);
10175 ptr = (unsigned char *) strchr ((char *) ptr, '\0') + 1;
10176 break;
10177 case DW_MACRO_start_file:
10178 skip_leb128 (ptr);
10179 skip_leb128 (ptr);
10180 can_share = false;
10181 break;
10182 case DW_MACRO_end_file:
10183 can_share = false;
10184 break;
10185 case DW_MACRO_define_strp:
10186 case DW_MACRO_undef_strp:
10187 skip_leb128 (ptr);
10188 strp = read_32 (ptr);
10189 note_strp_offset (strp);
10190 if (wr_multifile)
10191 break;
10192 if (can_share)
10193 hash = iterative_hash (s, ptr - 4 - s, hash);
10194 if (can_share)
10195 {
10196 unsigned char *p = debug_sections[DEBUG_STR].data + strp;
10197 unsigned int len = strlen ((char *) p);
10198 hash = iterative_hash (p, len, hash);
10199 s = ptr;
10200 }
10201 break;
10202 case DW_MACRO_import:
10203 ptr += 4;
10204 can_share = false;
10205 break;
10206 default:
10207 error (0, 0, "%s: Unhandled .debug_macro opcode 0x%x",
10208 dso->filename, op);
10209 return 1;
10210 }
10211 }
10212 if (op != 0)
10213 {
10214 error (0, 0, "%s: .debug_macro section not zero terminated",
10215 dso->filename);
10216 return 1;
10217 }
10218 if (wr_multifile)
10219 {
10220 if (can_share)
10221 debug_sections[DEBUG_MACRO].new_size += ptr - start;
10222 continue;
10223 }
10224
10225 me.ptr = start;
10226 if (can_share)
10227 {
10228 hash = iterative_hash (s, ptr - s, hash);
10229 me.hash = hash & ~1U;
10230 me.len = ptr - start;
10231 m = (struct macro_entry *)
10232 htab_find_with_hash (alt_macro_htab, &me, me.hash);
10233 if (m == NULL)
10234 can_share = false;
10235 else
10236 me.hash = m->ptr - alt_data[DEBUG_MACRO];
10237 }
10238 if (!can_share)
10239 {
10240 me.len = 0;
10241 me.hash = debug_sections[DEBUG_MACRO].new_size;
10242 debug_sections[DEBUG_MACRO].new_size += ptr - start;
10243 }
10244 slot
10245 = htab_find_slot_with_hash (macro_htab, &me,
10246 me.ptr - debug_sections[DEBUG_MACRO].data,
10247 INSERT);
10248 if (slot == NULL)
10249 dwz_oom ();
10250 else
10251 {
10252 assert (*slot == NULL);
10253 m = pool_alloc (macro_entry, sizeof (*m));
10254 *m = me;
10255 *slot = (void *) m;
10256 }
10257 }
10258
10259 if (!wr_multifile)
10260 return 0;
10261
10262 debug_sections[DEBUG_MACRO].new_data
10263 = (unsigned char *) malloc (debug_sections[DEBUG_MACRO].new_size);
10264 if (debug_sections[DEBUG_MACRO].new_data == NULL)
10265 dwz_oom ();
10266 dst = debug_sections[DEBUG_MACRO].new_data;
10267 for (ptr = debug_sections[DEBUG_MACRO].data; ptr < endsec; )
10268 {
10269 unsigned char *start = ptr;
10270 bool can_share = true;
10271
10272 ptr += 2;
10273 flags = read_8 (ptr);
10274 if ((flags & 2) != 0)
10275 {
10276 ptr += 4;
10277 can_share = false;
10278 }
10279
10280 while (1)
10281 {
10282 op = read_8 (ptr);
10283 if (op == 0)
10284 break;
10285
10286 switch (op)
10287 {
10288 case DW_MACRO_define:
10289 case DW_MACRO_undef:
10290 skip_leb128 (ptr);
10291 ptr = (unsigned char *) strchr ((char *) ptr, '\0') + 1;
10292 break;
10293 case DW_MACRO_start_file:
10294 skip_leb128 (ptr);
10295 skip_leb128 (ptr);
10296 can_share = false;
10297 break;
10298 case DW_MACRO_end_file:
10299 can_share = false;
10300 break;
10301 case DW_MACRO_define_strp:
10302 case DW_MACRO_undef_strp:
10303 skip_leb128 (ptr);
10304 ptr += 4;
10305 break;
10306 case DW_MACRO_import:
10307 ptr += 4;
10308 can_share = false;
10309 break;
10310 default:
10311 abort ();
10312 }
10313 }
10314 if (can_share)
10315 {
10316 ptr = start + 3;
10317
10318 while (1)
10319 {
10320 op = read_8 (ptr);
10321 if (op == 0)
10322 break;
10323
10324 switch (op)
10325 {
10326 case DW_MACRO_define:
10327 case DW_MACRO_undef:
10328 skip_leb128 (ptr);
10329 ptr = (unsigned char *) strchr ((char *) ptr, '\0') + 1;
10330 break;
10331 case DW_MACRO_define_strp:
10332 case DW_MACRO_undef_strp:
10333 skip_leb128 (ptr);
10334 memcpy (dst, start, ptr - start);
10335 dst += ptr - start;
10336 strp = lookup_strp_offset (read_32 (ptr));
10337 write_32 (dst, strp);
10338 start = ptr;
10339 break;
10340 default:
10341 abort ();
10342 }
10343 }
10344 memcpy (dst, start, ptr - start);
10345 dst += ptr - start;
10346 }
10347 }
10348 assert (dst == debug_sections[DEBUG_MACRO].new_data
10349 + debug_sections[DEBUG_MACRO].new_size);
10350
10351 return 0;
10352 }
10353
10354 /* Helper function for handle_macro, called through htab_traverse.
10355 Write .debug_macro opcode sequence seen by more than one
10356 executable or shared library. */
10357 static int
optimize_write_macro(void ** slot,void * data)10358 optimize_write_macro (void **slot, void *data)
10359 {
10360 struct macro_entry *m = (struct macro_entry *) *slot;
10361 unsigned char **pp = (unsigned char **) data;
10362 unsigned char *s = m->ptr;
10363 unsigned char *p = s + 3, *q, op;
10364 unsigned int strp;
10365
10366 if ((m->hash & 1) == 0)
10367 return 1;
10368 while (1)
10369 {
10370 op = read_8 (p);
10371 if (op == 0)
10372 break;
10373
10374 switch (op)
10375 {
10376 case DW_MACRO_define:
10377 case DW_MACRO_undef:
10378 skip_leb128 (p);
10379 p = (unsigned char *) strchr ((char *) p, '\0') + 1;
10380 break;
10381 case DW_MACRO_define_strp:
10382 case DW_MACRO_undef_strp:
10383 skip_leb128 (p);
10384 memcpy (*pp, s, p - s);
10385 *pp += p - s;
10386 strp = read_32 (p);
10387 q = *pp;
10388 write_32 (q, lookup_strp_offset (strp));
10389 *pp += 4;
10390 s = p;
10391 break;
10392 default:
10393 abort ();
10394 }
10395 }
10396 memcpy (*pp, s, p - s);
10397 *pp += p - s;
10398 return 1;
10399 }
10400
10401 /* Parse .debug_macro section, during optimize_multifile
10402 or during read_multifile. It parses .debug_macro written
10403 by write_multifile, so it only contains shareable sequences.
10404 Find duplicate sequences, during optimize_multifile write them
10405 into debug_sections[DEBUG_MACRO].new_data it allocates,
10406 during read_multifile just populates macro_htab (soon to be
10407 alt_macro_htab). */
10408 static void
handle_macro(void)10409 handle_macro (void)
10410 {
10411 unsigned char *ptr, *endsec, op;
10412 unsigned char *to_free = NULL;
10413 struct macro_entry me, *m;
10414
10415 macro_htab = htab_try_create (50, macro_hash, macro_eq, NULL);
10416 if (macro_htab == NULL)
10417 dwz_oom ();
10418
10419 endsec = debug_sections[DEBUG_MACRO].data + debug_sections[DEBUG_MACRO].size;
10420 if (op_multifile)
10421 {
10422 debug_sections[DEBUG_MACRO].new_size = 0;
10423 to_free = obstack_alloc (&ob, 1);
10424 }
10425
10426 for (ptr = debug_sections[DEBUG_MACRO].data; ptr < endsec; )
10427 {
10428 unsigned char *start = ptr, *s = ptr, *p;
10429 hashval_t hash = 0;
10430 unsigned int len;
10431 void **slot;
10432 bool can_share = true;
10433
10434 ptr += 3;
10435 while (1)
10436 {
10437 op = read_8 (ptr);
10438 if (op == 0)
10439 break;
10440
10441 switch (op)
10442 {
10443 case DW_MACRO_define:
10444 case DW_MACRO_undef:
10445 skip_leb128 (ptr);
10446 ptr = (unsigned char *) strchr ((char *) ptr, '\0') + 1;
10447 break;
10448 case DW_MACRO_define_strp:
10449 case DW_MACRO_undef_strp:
10450 skip_leb128 (ptr);
10451 hash = iterative_hash (s, ptr - s, hash);
10452 p = debug_sections[DEBUG_STR].data + read_32 (ptr);
10453 len = strlen ((char *) p);
10454 hash = iterative_hash (p, len, hash);
10455 if (op_multifile
10456 /* This should only happen if there were multiple
10457 same transparent units within a single object file. */
10458 && htab_find_with_hash (strp_htab, p,
10459 iterative_hash (p, len, 0)) == NULL)
10460 can_share = false;
10461 s = ptr;
10462 break;
10463 default:
10464 abort ();
10465 }
10466 }
10467 if (!can_share)
10468 continue;
10469 hash = iterative_hash (s, ptr - s, hash);
10470 me.ptr = start;
10471 me.hash = hash & ~1U;
10472 me.len = ptr - start;
10473 slot = htab_find_slot_with_hash (macro_htab, &me, me.hash, INSERT);
10474 if (slot == NULL)
10475 dwz_oom ();
10476 else if (*slot != NULL)
10477 {
10478 m = (struct macro_entry *) *slot;
10479 if (op_multifile && (m->hash & 1) == 0)
10480 {
10481 m->hash |= 1;
10482 debug_sections[DEBUG_MACRO].new_size += me.len;
10483 }
10484 }
10485 else if (op_multifile)
10486 {
10487 m = (struct macro_entry *) obstack_alloc (&ob, sizeof (*m));
10488 *m = me;
10489 *slot = (void *) m;
10490 }
10491 else
10492 {
10493 m = pool_alloc (macro_entry, sizeof (*m));
10494 *m = me;
10495 *slot = (void *) m;
10496 }
10497 }
10498
10499 if (op_multifile)
10500 {
10501 if (debug_sections[DEBUG_MACRO].new_size)
10502 {
10503 unsigned char *p;
10504 debug_sections[DEBUG_MACRO].new_data
10505 = malloc (debug_sections[DEBUG_MACRO].new_size);
10506 p = debug_sections[DEBUG_MACRO].new_data;
10507 htab_traverse (macro_htab, optimize_write_macro, &p);
10508 assert (p == debug_sections[DEBUG_MACRO].new_data
10509 + debug_sections[DEBUG_MACRO].new_size);
10510 htab_delete (macro_htab);
10511 macro_htab = NULL;
10512 }
10513 obstack_free (&ob, (void *) to_free);
10514 }
10515 }
10516
10517 /* Write new content of .debug_macro section during fi_multifile phase. */
10518 static void
write_macro(void)10519 write_macro (void)
10520 {
10521 unsigned char *ptr, *endsec, *dst;
10522 unsigned int op, strp;
10523 struct macro_entry me, *m;
10524
10525 endsec = debug_sections[DEBUG_MACRO].data + debug_sections[DEBUG_MACRO].size;
10526 debug_sections[DEBUG_MACRO].new_data
10527 = (unsigned char *) malloc (debug_sections[DEBUG_MACRO].new_size);
10528 if (debug_sections[DEBUG_MACRO].new_data == NULL)
10529 dwz_oom ();
10530 dst = debug_sections[DEBUG_MACRO].new_data;
10531 for (ptr = debug_sections[DEBUG_MACRO].data; ptr < endsec; )
10532 {
10533 unsigned char *s = ptr;
10534 unsigned char flags;
10535
10536 me.ptr = ptr;
10537 m = (struct macro_entry *)
10538 htab_find_with_hash (macro_htab, &me,
10539 me.ptr - debug_sections[DEBUG_MACRO].data);
10540 if (m->len)
10541 {
10542 ptr += m->len;
10543 continue;
10544 }
10545
10546 ptr += 2;
10547 flags = read_8 (ptr);
10548 if ((flags & 2) != 0)
10549 ptr += 4;
10550
10551 while (1)
10552 {
10553 op = read_8 (ptr);
10554 if (op == 0)
10555 break;
10556
10557 switch (op)
10558 {
10559 case DW_MACRO_define:
10560 case DW_MACRO_undef:
10561 skip_leb128 (ptr);
10562 ptr = (unsigned char *) strchr ((char *) ptr, '\0') + 1;
10563 break;
10564 case DW_MACRO_start_file:
10565 skip_leb128 (ptr);
10566 skip_leb128 (ptr);
10567 break;
10568 case DW_MACRO_end_file:
10569 break;
10570 case DW_MACRO_define_strp:
10571 case DW_MACRO_undef_strp:
10572 memcpy (dst, s, ptr - 1 - s);
10573 dst += ptr - 1 - s;
10574 s = ptr - 1;
10575 skip_leb128 (ptr);
10576 strp = read_32 (ptr);
10577 switch (note_strp_offset2 (strp))
10578 {
10579 case DW_FORM_GNU_strp_alt:
10580 case DW_FORM_strp_sup:
10581 *dst = op == DW_MACRO_define_strp
10582 ? DW_MACRO_define_sup
10583 : DW_MACRO_undef_sup;
10584 dst++;
10585 s++;
10586 break;
10587 default:
10588 break;
10589 }
10590 memcpy (dst, s, ptr - 4 - s);
10591 dst += ptr - 4 - s;
10592 write_32 (dst, lookup_strp_offset (strp));
10593 s = ptr;
10594 break;
10595 case DW_MACRO_import:
10596 memcpy (dst, s, ptr - 1 - s);
10597 dst += ptr - 1 - s;
10598 me.ptr = debug_sections[DEBUG_MACRO].data + read_32 (ptr);
10599 m = (struct macro_entry *)
10600 htab_find_with_hash (macro_htab, &me,
10601 me.ptr
10602 - debug_sections[DEBUG_MACRO].data);
10603 if (m->len)
10604 *dst = DW_MACRO_import_sup;
10605 else
10606 *dst = DW_MACRO_import;
10607 dst++;
10608 write_32 (dst, m->hash);
10609 s = ptr;
10610 break;
10611 default:
10612 abort ();
10613 }
10614 }
10615 memcpy (dst, s, ptr - s);
10616 dst += ptr - s;
10617 }
10618 assert (dst == debug_sections[DEBUG_MACRO].new_data
10619 + debug_sections[DEBUG_MACRO].new_size);
10620 }
10621
10622 /* Compute new abbreviations for DIE (with reference DIE REF).
10623 T is a temporary buffer. Fill in *NDIES - number of DIEs
10624 in the tree, and record pairs of referrer/referree DIEs for
10625 intra-CU references into obstack vector VEC. */
10626 static int
build_abbrevs_for_die(htab_t h,dw_cu_ref cu,dw_die_ref die,dw_cu_ref refcu,dw_die_ref ref,struct abbrev_tag * t,unsigned int * ndies,struct obstack * vec,bool recompute)10627 build_abbrevs_for_die (htab_t h, dw_cu_ref cu, dw_die_ref die,
10628 dw_cu_ref refcu, dw_die_ref ref,
10629 struct abbrev_tag *t, unsigned int *ndies,
10630 struct obstack *vec, bool recompute)
10631 {
10632 dw_die_ref child, ref_child, sib = NULL, origin = NULL;
10633 unsigned int i, j;
10634 uint64_t low_pc = 0;
10635 void **slot;
10636
10637 if (unlikely (recompute) && die->u.p2.die_new_abbrev != NULL)
10638 {
10639 if (cu->cu_intracu_form == DW_FORM_ref_udata)
10640 die->die_ref_seen = 1;
10641 else
10642 {
10643 die->die_size -= size_of_uleb128 (die->u.p2.die_new_abbrev->entry)
10644 + die->u.p2.die_intracu_udata_size;
10645 die->die_ref_seen = 0;
10646 }
10647 for (child = die->die_child; child; child = child->die_sib)
10648 if (build_abbrevs_for_die (h, cu, child, NULL, NULL, t, ndies, vec,
10649 true))
10650 return 1;
10651 return 0;
10652 }
10653
10654 die->u.p2.die_new_abbrev = NULL;
10655 die->u.p2.die_new_offset = 0;
10656 die->u.p2.die_intracu_udata_size = 0;
10657 die->die_ref_seen = 0;
10658
10659 if (wr_multifile ? die->die_no_multifile : die->die_remove)
10660 return 0;
10661 t->entry = 0;
10662 t->tag = die->die_tag;
10663 t->children = die->die_child != NULL;
10664 t->op_type_referenced = false;
10665 t->nusers = 1;
10666 if (die->die_offset == -1U)
10667 {
10668 if (ref != NULL)
10669 ;
10670 else if (die_safe_nextdup (die) && die->die_nextdup->die_dup == die)
10671 {
10672 ref = die->die_nextdup;
10673 if (ref != NULL)
10674 refcu = die_cu (ref);
10675 }
10676 if (ref == NULL)
10677 origin = die->die_nextdup;
10678 }
10679 else
10680 {
10681 ref = die;
10682 refcu = cu;
10683 if (wr_multifile
10684 && (die->die_root || die->die_named_namespace))
10685 origin = die;
10686 }
10687 if (die->die_child && die->die_sib)
10688 for (sib = die->die_sib; sib; sib = sib->die_sib)
10689 if (wr_multifile ? !sib->die_no_multifile : !sib->die_remove)
10690 break;
10691 if (ref != NULL && origin == NULL)
10692 {
10693 unsigned char *base
10694 = cu->cu_kind == CU_TYPES
10695 ? debug_sections[DEBUG_TYPES].data
10696 : debug_sections[DEBUG_INFO].data;
10697 unsigned char *ptr = base + ref->die_offset;
10698 struct abbrev_tag *reft = ref->die_abbrev;
10699
10700 skip_leb128 (ptr);
10701 /* No longer count the abbrev uleb128 size in die_size.
10702 We'll add it back after determining the new abbrevs. */
10703 if (unlikely (wr_multifile || op_multifile || fi_multifile)
10704 || unlikely (recompute))
10705 i = -1U;
10706 else
10707 for (i = 0; i < reft->nattr; i++)
10708 switch (reft->attr[i].form)
10709 {
10710 case DW_FORM_ref1:
10711 case DW_FORM_ref2:
10712 case DW_FORM_ref4:
10713 case DW_FORM_ref8:
10714 case DW_FORM_ref_udata:
10715 case DW_FORM_indirect:
10716 i = -2U;
10717 break;
10718 case DW_FORM_data4:
10719 case DW_FORM_data8:
10720 if (reft->attr[i].attr == DW_AT_high_pc)
10721 i = -2U;
10722 break;
10723 case DW_FORM_addr:
10724 if (reft->attr[i].attr == DW_AT_high_pc
10725 && cu->cu_version >= 4)
10726 i = -2U;
10727 break;
10728 default:
10729 break;
10730 }
10731 if (i != -1U)
10732 {
10733 die->die_size -= ptr - (base + ref->die_offset);
10734 /* If there are no references, size stays the same
10735 and no need to walk the actual attribute values. */
10736 for (i = 0; i < reft->nattr; i++)
10737 {
10738 t->attr[i].attr = reft->attr[i].attr;
10739 t->attr[i].form = reft->attr[i].form;
10740 if (t->attr[i].form == DW_FORM_implicit_const)
10741 t->values[i] = reft->values[i];
10742 }
10743 t->nattr = reft->nattr;
10744 }
10745 else
10746 {
10747 die->die_size = 0;
10748 /* Otherwise, we need to walk the actual attributes. */
10749 for (i = 0, j = 0; i < reft->nattr; ++i)
10750 {
10751 uint32_t form = reft->attr[i].form;
10752 size_t len = 0;
10753 dw_die_ref refd;
10754 uint64_t value = 0;
10755 unsigned char *orig_ptr = ptr;
10756
10757 while (form == DW_FORM_indirect)
10758 form = read_uleb128 (ptr);
10759
10760 if (unlikely (wr_multifile || op_multifile)
10761 && (reft->attr[i].attr == DW_AT_decl_file
10762 || reft->attr[i].attr == DW_AT_call_file))
10763 {
10764 switch (form)
10765 {
10766 case DW_FORM_data1: value = read_8 (ptr); break;
10767 case DW_FORM_data2: value = read_16 (ptr); break;
10768 case DW_FORM_data4: value = read_32 (ptr); break;
10769 case DW_FORM_data8: value = read_64 (ptr); break;
10770 case DW_FORM_udata: value = read_uleb128 (ptr); break;
10771 case DW_FORM_sdata: value = read_sleb128 (ptr); break;
10772 case DW_FORM_implicit_const:
10773 value = reft->values[i];
10774 break;
10775 default:
10776 error (0, 0, "Unhandled %s for %s",
10777 get_DW_FORM_str (form),
10778 get_DW_AT_str (reft->attr[i].attr));
10779 return 1;
10780 }
10781 value = line_htab_lookup (refcu, value);
10782 if (form != DW_FORM_implicit_const)
10783 {
10784 if (value <= 0xff)
10785 {
10786 form = DW_FORM_data1;
10787 die->die_size++;
10788 }
10789 else if (value <= 0xffff)
10790 {
10791 form = DW_FORM_data2;
10792 die->die_size += 2;
10793 }
10794 else if (value <= 0xffffffff)
10795 {
10796 form = DW_FORM_data4;
10797 die->die_size += 4;
10798 }
10799 else
10800 {
10801 form = DW_FORM_data8;
10802 die->die_size += 8;
10803 }
10804 }
10805 t->attr[j].attr = reft->attr[i].attr;
10806 t->attr[j].form = form;
10807 if (form == DW_FORM_implicit_const)
10808 t->values[j] = value;
10809 j++;
10810 continue;
10811 }
10812
10813 if (unlikely (fi_multifile)
10814 && (reft->attr[i].attr == DW_AT_GNU_macros
10815 || reft->attr[i].attr == DW_AT_macros)
10816 && alt_macro_htab != NULL)
10817 {
10818 struct macro_entry me, *m;
10819
10820 switch (form)
10821 {
10822 case DW_FORM_data4:
10823 case DW_FORM_sec_offset:
10824 value = read_32 (ptr);
10825 break;
10826 default:
10827 error (0, 0, "Unhandled %s for %s",
10828 get_DW_FORM_str (form),
10829 get_DW_AT_str (reft->attr[i].attr));
10830 return 1;
10831 }
10832 me.ptr = debug_sections[DEBUG_MACRO].data + value;
10833 m = (struct macro_entry *)
10834 htab_find_with_hash (macro_htab, &me, value);
10835 if (m->len)
10836 {
10837 error (0, 0, "%s referencing transparent include",
10838 get_DW_AT_str (reft->attr[i].attr));
10839 return 1;
10840 }
10841 ptr -= 4;
10842 }
10843
10844 switch (form)
10845 {
10846 case DW_FORM_ref_addr:
10847 if (unlikely (fi_multifile))
10848 {
10849 dw_die_ref refdt;
10850 value = read_size (ptr,
10851 refcu->cu_version == 2
10852 ? ptr_size : 4);
10853 ptr += refcu->cu_version == 2 ? ptr_size : 4;
10854 refd = off_htab_lookup (NULL, value);
10855 assert (refd != NULL);
10856 refdt = refd;
10857 while (refdt->die_toplevel == 0)
10858 refdt = refdt->die_parent;
10859 if (refdt->die_dup
10860 && !refdt->die_op_type_referenced
10861 && die_cu (refdt->die_dup)->cu_kind == CU_ALT)
10862 {
10863 t->attr[j].attr = reft->attr[i].attr;
10864 t->attr[j++].form
10865 = dwarf_5 ? DW_FORM_ref_sup4 : DW_FORM_GNU_ref_alt;
10866 die->die_size += 4;
10867 continue;
10868 }
10869 break;
10870 }
10871 ptr += refcu->cu_version == 2 ? ptr_size : 4;
10872 break;
10873 case DW_FORM_addr:
10874 ptr += ptr_size;
10875 if (reft->attr[i].attr == DW_AT_low_pc
10876 && cu->cu_version >= 4)
10877 low_pc = read_size (ptr - ptr_size, ptr_size);
10878 else if (reft->attr[i].attr == DW_AT_high_pc
10879 && low_pc)
10880 {
10881 uint64_t high_pc = read_size (ptr - ptr_size, ptr_size);
10882 /* If both DW_AT_low_pc and DW_AT_high_pc attributes
10883 are present and have DW_FORM_addr, attempt to shrink
10884 the DIE by using DW_FORM_udata or DW_FORM_data4
10885 form for the latter in DWARF4+. Don't try
10886 DW_FORM_data[12], that might increase .debug_abbrev
10887 size too much or increase the uleb128 size of too
10888 many abbrev numbers. */
10889 if (high_pc > low_pc)
10890 {
10891 unsigned int nform = 0;
10892 unsigned int sz = size_of_uleb128 (high_pc - low_pc);
10893 if (sz <= 4 && sz <= (unsigned) ptr_size)
10894 nform = DW_FORM_udata;
10895 else if (ptr_size > 4
10896 && high_pc - low_pc <= 0xffffffff)
10897 {
10898 nform = DW_FORM_data4;
10899 sz = 4;
10900 }
10901 else if (sz <= (unsigned) ptr_size)
10902 nform = DW_FORM_udata;
10903 if (nform)
10904 {
10905 t->attr[j].attr = reft->attr[i].attr;
10906 t->attr[j++].form = nform;
10907 die->die_size += sz;
10908 continue;
10909 }
10910 }
10911 }
10912 break;
10913 case DW_FORM_flag_present:
10914 case DW_FORM_implicit_const:
10915 break;
10916 case DW_FORM_flag:
10917 case DW_FORM_data1:
10918 ++ptr;
10919 break;
10920 case DW_FORM_data2:
10921 ptr += 2;
10922 break;
10923 case DW_FORM_data4:
10924 if (reft->attr[i].attr == DW_AT_high_pc)
10925 {
10926 uint32_t range_len = read_32 (ptr);
10927 unsigned int sz = size_of_uleb128 (range_len);
10928 if (sz <= 4)
10929 {
10930 t->attr[j].attr = reft->attr[i].attr;
10931 t->attr[j++].form = DW_FORM_udata;
10932 die->die_size += sz;
10933 continue;
10934 }
10935 break;
10936 }
10937 ptr += 4;
10938 break;
10939 case DW_FORM_sec_offset:
10940 ptr += 4;
10941 break;
10942 case DW_FORM_data8:
10943 if (reft->attr[i].attr == DW_AT_high_pc)
10944 {
10945 unsigned int nform = 0;
10946 uint64_t range_len = read_64 (ptr);
10947 unsigned int sz = size_of_uleb128 (range_len);
10948 if (sz <= 4)
10949 nform = DW_FORM_udata;
10950 else if (range_len <= 0xffffffff)
10951 {
10952 nform = DW_FORM_data4;
10953 sz = 4;
10954 }
10955 else if (sz <= 8)
10956 nform = DW_FORM_udata;
10957 if (nform)
10958 {
10959 t->attr[j].attr = reft->attr[i].attr;
10960 t->attr[j++].form = nform;
10961 die->die_size += sz;
10962 continue;
10963 }
10964 break;
10965 }
10966 ptr += 8;
10967 break;
10968 case DW_FORM_ref_sig8:
10969 ptr += 8;
10970 break;
10971 case DW_FORM_data16:
10972 ptr += 16;
10973 break;
10974 case DW_FORM_sdata:
10975 case DW_FORM_udata:
10976 skip_leb128 (ptr);
10977 break;
10978 case DW_FORM_strp:
10979 if (unlikely (op_multifile || fi_multifile))
10980 {
10981 form = note_strp_offset2 (read_32 (ptr));
10982 if (form != DW_FORM_strp)
10983 {
10984 t->attr[j].attr = reft->attr[i].attr;
10985 t->attr[j++].form = form;
10986 die->die_size += 4;
10987 continue;
10988 }
10989 }
10990 else
10991 ptr += 4;
10992 break;
10993 case DW_FORM_line_strp:
10994 /* Since we don't register the line_strp we cannot
10995 change the form in the case of multifile. */
10996 ptr += 4;
10997 break;
10998 case DW_FORM_string:
10999 ptr = (unsigned char *) strchr ((char *)ptr, '\0') + 1;
11000 break;
11001 case DW_FORM_indirect:
11002 abort ();
11003 case DW_FORM_block1:
11004 len = *ptr++;
11005 break;
11006 case DW_FORM_block2:
11007 len = read_16 (ptr);
11008 form = DW_FORM_block1;
11009 break;
11010 case DW_FORM_block4:
11011 len = read_32 (ptr);
11012 form = DW_FORM_block1;
11013 break;
11014 case DW_FORM_block:
11015 case DW_FORM_exprloc:
11016 len = read_uleb128 (ptr);
11017 form = DW_FORM_block1;
11018 break;
11019 case DW_FORM_ref1:
11020 case DW_FORM_ref2:
11021 case DW_FORM_ref4:
11022 case DW_FORM_ref8:
11023 case DW_FORM_ref_udata:
11024 switch (form)
11025 {
11026 case DW_FORM_ref1: value = read_8 (ptr); break;
11027 case DW_FORM_ref2: value = read_16 (ptr); break;
11028 case DW_FORM_ref4: value = read_32 (ptr); break;
11029 case DW_FORM_ref8: value = read_64 (ptr); break;
11030 case DW_FORM_ref_udata: value = read_uleb128 (ptr); break;
11031 default: abort ();
11032 }
11033 if (reft->attr[i].attr == DW_AT_sibling)
11034 {
11035 if (sib == NULL)
11036 continue;
11037 form = DW_FORM_ref4;
11038 refd = sib;
11039 }
11040 else
11041 {
11042 dw_die_ref refdt;
11043 refd = off_htab_lookup (refcu, refcu->cu_offset + value);
11044 assert (refd != NULL);
11045 refdt = refd;
11046 while (refdt->die_toplevel == 0)
11047 refdt = refdt->die_parent;
11048 if (refdt->die_dup && refdt->die_op_type_referenced)
11049 {
11050 if (cu == die_cu (refdt))
11051 form = DW_FORM_ref4;
11052 else if (cu == die_cu (refdt->die_dup))
11053 {
11054 form = DW_FORM_ref4;
11055 refd = die_find_dup (refdt, refdt->die_dup,
11056 refd);
11057 }
11058 else
11059 form = DW_FORM_ref_addr;
11060 }
11061 else
11062 {
11063 if (refdt->die_dup)
11064 refd = die_find_dup (refdt, refdt->die_dup, refd);
11065 if (cu == die_cu (refd))
11066 form = DW_FORM_ref4;
11067 else if (die_cu (refd)->cu_kind == CU_ALT)
11068 form = (dwarf_5
11069 ? DW_FORM_ref_sup4 : DW_FORM_GNU_ref_alt);
11070 else
11071 form = DW_FORM_ref_addr;
11072 }
11073 }
11074 if (form == DW_FORM_ref_addr)
11075 die->die_size += cu->cu_version == 2 ? ptr_size : 4;
11076 else if (form == DW_FORM_GNU_ref_alt
11077 || form == DW_FORM_ref_sup4)
11078 die->die_size += 4;
11079 else
11080 {
11081 if (unlikely (recompute))
11082 form = cu->cu_intracu_form;
11083 if (likely (!recompute) || form == DW_FORM_ref_udata)
11084 {
11085 obstack_ptr_grow (vec, die);
11086 obstack_ptr_grow (vec, refd);
11087 }
11088 }
11089 t->attr[j].attr = reft->attr[i].attr;
11090 t->attr[j++].form = form;
11091 continue;
11092 default:
11093 abort ();
11094 }
11095
11096 if (form == DW_FORM_block1)
11097 ptr += len;
11098 t->attr[j].attr = reft->attr[i].attr;
11099 t->attr[j].form = reft->attr[i].form;
11100 if (reft->attr[i].form == DW_FORM_implicit_const)
11101 t->values[j] = reft->values[i];
11102 j++;
11103 die->die_size += ptr - orig_ptr;
11104 }
11105 t->nattr = j;
11106 }
11107 }
11108 else
11109 switch (die->die_tag)
11110 {
11111 case DW_TAG_partial_unit:
11112 case DW_TAG_compile_unit:
11113 t->nattr = 0;
11114 die->die_size = 0;
11115 if (origin == NULL)
11116 break;
11117 refcu = die_cu (origin);
11118 if (refcu->cu_nfiles)
11119 {
11120 t->attr[0].attr = DW_AT_stmt_list;
11121 t->attr[0].form = cu->cu_version < 4
11122 ? DW_FORM_data4 : DW_FORM_sec_offset;
11123 die->die_size += 4;
11124 t->nattr++;
11125 }
11126 if (uni_lang_p || cu->cu_die->die_tag == DW_TAG_compile_unit)
11127 {
11128 unsigned int lang_size = nr_bytes_for (cu->lang);
11129 die->die_size += lang_size;
11130 t->attr[t->nattr].attr = DW_AT_language;
11131 switch (lang_size)
11132 {
11133 case 1:
11134 t->attr[t->nattr].form = DW_FORM_data1;
11135 break;
11136 case 2:
11137 t->attr[t->nattr].form = DW_FORM_data2;
11138 break;
11139 default:
11140 abort ();
11141 }
11142 t->nattr++;
11143 }
11144 if (refcu->cu_comp_dir)
11145 {
11146 enum dwarf_form form;
11147 unsigned char *ptr = get_AT (origin, DW_AT_comp_dir, &form);
11148 assert (ptr && (form == DW_FORM_string
11149 || form == DW_FORM_strp
11150 || form == DW_FORM_line_strp));
11151 if (form == DW_FORM_strp)
11152 {
11153 if (unlikely (op_multifile || fi_multifile))
11154 form = note_strp_offset2 (read_32 (ptr));
11155 die->die_size += 4;
11156 }
11157 else if (form == DW_FORM_line_strp)
11158 die->die_size += 4;
11159 else
11160 die->die_size
11161 += strlen (refcu->cu_comp_dir) + 1;
11162 t->attr[t->nattr].attr = DW_AT_comp_dir;
11163 t->attr[t->nattr].form = form;
11164 t->nattr++;
11165 }
11166 break;
11167 case DW_TAG_namespace:
11168 case DW_TAG_module:
11169 {
11170 enum dwarf_form form;
11171 unsigned char *ptr = get_AT (origin, DW_AT_name, &form);
11172 assert (ptr && (form == DW_FORM_string
11173 || form == DW_FORM_strp
11174 || form == DW_FORM_line_strp));
11175 if (form == DW_FORM_strp)
11176 {
11177 if (unlikely (op_multifile || fi_multifile))
11178 form = note_strp_offset2 (read_32 (ptr));
11179 die->die_size = 4;
11180 }
11181 else if (form == DW_FORM_line_strp)
11182 die->die_size += 4;
11183 else
11184 die->die_size = strlen ((char *) ptr) + 1;
11185 t->attr[0].attr = DW_AT_name;
11186 t->attr[0].form = form;
11187 t->nattr = 1;
11188 if (sib)
11189 {
11190 t->attr[1].attr = DW_AT_sibling;
11191 t->attr[1].form = DW_FORM_ref4;
11192 obstack_ptr_grow (vec, die);
11193 obstack_ptr_grow (vec, sib);
11194 t->nattr++;
11195 }
11196 break;
11197 }
11198 case DW_TAG_imported_unit:
11199 t->attr[0].attr = DW_AT_import;
11200 t->nattr = 1;
11201 if (die_cu (die->die_nextdup)->cu_kind == CU_ALT)
11202 {
11203 t->attr[0].form = dwarf_5 ? DW_FORM_ref_sup4 : DW_FORM_GNU_ref_alt;
11204 die->die_size = 4;
11205 }
11206 else
11207 {
11208 t->attr[0].form = DW_FORM_ref_addr;
11209 die->die_size = cu->cu_version == 2 ? ptr_size : 4;
11210 }
11211 break;
11212 default:
11213 abort ();
11214 }
11215 compute_abbrev_hash (t);
11216 slot = htab_find_slot_with_hash (h, t, t->hash,
11217 recompute ? NO_INSERT : INSERT);
11218 if (slot == NULL)
11219 dwz_oom ();
11220 if (unlikely (recompute))
11221 assert (*slot);
11222 if (*slot)
11223 {
11224 if (likely (!recompute))
11225 ((struct abbrev_tag *)*slot)->nusers++;
11226 die->u.p2.die_new_abbrev = (struct abbrev_tag *)*slot;
11227 }
11228 else
11229 {
11230 struct abbrev_tag *newt = pool_clone_abbrev (t);
11231 *slot = newt;
11232 die->u.p2.die_new_abbrev = newt;
11233 }
11234 (*ndies)++;
11235 if (ref != NULL && ref != die)
11236 {
11237 for (child = die->die_child, ref_child = ref->die_child;
11238 child; child = child->die_sib, ref_child = ref_child->die_sib)
11239 if (build_abbrevs_for_die (h, cu, child, refcu, ref_child,
11240 t, ndies, vec, recompute))
11241 return 1;
11242 }
11243 else
11244 for (child = die->die_child; child; child = child->die_sib)
11245 if (build_abbrevs_for_die (h, cu, child, NULL, NULL, t, ndies, vec,
11246 recompute))
11247 return 1;
11248 return 0;
11249 }
11250
11251 /* Build new abbreviations for CU. T, NDIES and VEC arguments like
11252 for build_abbrevs_for_die. */
11253 static int
build_abbrevs(dw_cu_ref cu,struct abbrev_tag * t,unsigned int * ndies,struct obstack * vec)11254 build_abbrevs (dw_cu_ref cu, struct abbrev_tag *t, unsigned int *ndies,
11255 struct obstack *vec)
11256 {
11257 htab_t h = htab_try_create (50, abbrev_hash, abbrev_eq2, NULL);
11258
11259 if (h == NULL)
11260 dwz_oom ();
11261
11262 if (build_abbrevs_for_die (h, cu, cu->cu_die, NULL, NULL, t, ndies, vec,
11263 false))
11264 return 1;
11265
11266 cu->cu_new_abbrev = h;
11267 return 0;
11268 }
11269
11270 /* Helper to record all abbrevs from the hash table into ob obstack
11271 vector. Called through htab_traverse. */
11272 static int
list_abbrevs(void ** slot,void * data)11273 list_abbrevs (void **slot, void *data)
11274 {
11275 struct obstack *obp = (struct obstack *) data;
11276 obstack_ptr_grow (obp, *slot);
11277 return 1;
11278 }
11279
11280 /* Comparison function for abbreviations. Used for CUs that
11281 need 128 or more abbreviations. Use lowest numbers (i.e. sort earlier)
11282 abbrevs used for typed DWARF stack referenced DIEs, then sort
11283 by decreasing number of users (abbrev numbers are uleb128 encoded,
11284 the bigger number of them that can be 1 byte encoded the better). */
11285 static int
abbrev_cmp(const void * p,const void * q)11286 abbrev_cmp (const void *p, const void *q)
11287 {
11288 struct abbrev_tag *t1 = *(struct abbrev_tag **)p;
11289 struct abbrev_tag *t2 = *(struct abbrev_tag **)q;
11290 unsigned int i;
11291
11292 if (t1->op_type_referenced && !t2->op_type_referenced)
11293 return -1;
11294 if (!t1->op_type_referenced && t2->op_type_referenced)
11295 return 1;
11296 if (t1->nusers > t2->nusers)
11297 return -1;
11298 if (t1->nusers < t2->nusers)
11299 return 1;
11300 /* The rest just so that we have a stable sort. */
11301 if (t1->tag < t2->tag)
11302 return -1;
11303 if (t1->tag > t2->tag)
11304 return 1;
11305 if (t1->nattr < t2->nattr)
11306 return -1;
11307 if (t1->nattr > t2->nattr)
11308 return 1;
11309 if (t1->children && !t2->children)
11310 return -1;
11311 if (!t1->children && t2->children)
11312 return 1;
11313 for (i = 0; i < t1->nattr; i++)
11314 {
11315 if (t1->attr[i].attr < t2->attr[i].attr)
11316 return -1;
11317 if (t1->attr[i].attr > t2->attr[i].attr)
11318 return 1;
11319 if (t1->attr[i].form < t2->attr[i].form)
11320 return -1;
11321 if (t1->attr[i].form > t2->attr[i].form)
11322 return 1;
11323 if (t1->attr[i].form == DW_FORM_implicit_const)
11324 {
11325 if (t1->values[i] < t2->values[i])
11326 return -1;
11327 if (t1->values[i] > t2->values[i])
11328 return 1;
11329 }
11330 }
11331 return 0;
11332 }
11333
11334 /* First phase of computation of u.p2.die_new_offset and
11335 new CU sizes. */
11336 static unsigned int
init_new_die_offsets(dw_die_ref die,unsigned int off,unsigned int intracusize)11337 init_new_die_offsets (dw_die_ref die, unsigned int off,
11338 unsigned int intracusize)
11339 {
11340 dw_die_ref child;
11341 unsigned int i;
11342 struct abbrev_tag *t = die->u.p2.die_new_abbrev;
11343 if (wr_multifile ? die->die_no_multifile : die->die_remove)
11344 return off;
11345 die->u.p2.die_new_offset = off;
11346 if (likely (die->die_ref_seen == 0))
11347 {
11348 die->die_size += size_of_uleb128 (die->u.p2.die_new_abbrev->entry);
11349 die->u.p2.die_intracu_udata_size = 0;
11350 for (i = 0; i < t->nattr; ++i)
11351 switch (t->attr[i].form)
11352 {
11353 case DW_FORM_ref1:
11354 case DW_FORM_ref2:
11355 case DW_FORM_ref4:
11356 case DW_FORM_ref_udata:
11357 die->u.p2.die_intracu_udata_size += intracusize;
11358 break;
11359 default:
11360 break;
11361 }
11362 die->die_size += die->u.p2.die_intracu_udata_size;
11363 }
11364 off += die->die_size;
11365 for (child = die->die_child; child; child = child->die_sib)
11366 off = init_new_die_offsets (child, off, intracusize);
11367 if (die->die_child)
11368 off++;
11369 return off;
11370 }
11371
11372 /* Second phase of computation of u.p2.die_new_offset and
11373 new CU sizes. This step is called possibly many times,
11374 for deciding if DW_FORM_ref_udata is worthwhile.
11375 init_new_die_offsets starts with assuming each uleb128 will
11376 need maximum number of bytes for the CU of the given size,
11377 each new invocation of this function (except the last)
11378 will shrink one or more uleb128s. Each shrinking can create
11379 new opportunities to shrink other uleb128s. */
11380 static unsigned int
update_new_die_offsets(dw_die_ref die,unsigned int off,dw_die_ref ** intracuvec)11381 update_new_die_offsets (dw_die_ref die, unsigned int off,
11382 dw_die_ref **intracuvec)
11383 {
11384 dw_die_ref child;
11385 if (wr_multifile ? die->die_no_multifile : die->die_remove)
11386 return off;
11387 assert (off <= die->u.p2.die_new_offset);
11388 die->u.p2.die_new_offset = off;
11389 if ((*intracuvec)[0] == die)
11390 {
11391 unsigned int intracu_udata_size = 0;
11392 assert (die->u.p2.die_intracu_udata_size);
11393 while ((*intracuvec)[0] == die)
11394 {
11395 intracu_udata_size
11396 += size_of_uleb128 ((*intracuvec)[1]->u.p2.die_new_offset);
11397 *intracuvec += 2;
11398 }
11399 assert (die->u.p2.die_intracu_udata_size >= intracu_udata_size);
11400 die->die_size -= die->u.p2.die_intracu_udata_size - intracu_udata_size;
11401 die->u.p2.die_intracu_udata_size = intracu_udata_size;
11402 }
11403 else
11404 assert (die->u.p2.die_intracu_udata_size == 0 || die->die_ref_seen);
11405 off += die->die_size;
11406 for (child = die->die_child; child; child = child->die_sib)
11407 off = update_new_die_offsets (child, off, intracuvec);
11408 if (die->die_child)
11409 off++;
11410 return off;
11411 }
11412
11413 /* Final phase of computation of u.p2.die_new_offset. Called when already
11414 decided what intra-CU form will be used. Can return -1U if
11415 a problem is detected and the tool should give up. */
11416 static unsigned int
finalize_new_die_offsets(dw_cu_ref cu,dw_die_ref die,unsigned int off,unsigned int intracusize,dw_die_ref ** intracuvec)11417 finalize_new_die_offsets (dw_cu_ref cu, dw_die_ref die, unsigned int off,
11418 unsigned int intracusize, dw_die_ref **intracuvec)
11419 {
11420 dw_die_ref child;
11421 unsigned int ref_seen = die->die_ref_seen;
11422 if (wr_multifile ? die->die_no_multifile : die->die_remove)
11423 return off;
11424 die->u.p2.die_new_offset = off;
11425 die->die_ref_seen = 0;
11426 /* As we aren't adjusting sizes of exprloc, if in the new layout
11427 a DIE referenced through DW_OP_call2 is placed after 64K into
11428 the CU, punt. */
11429 if (die->die_op_call2_referenced && off >= 65536)
11430 return -1U;
11431 /* Similarly punt if
11432 DW_OP_GNU_{{regval,const,deref}_type,convert,reinterpret}
11433 references a DIE that needs more uleb128 bytes to encode
11434 the new offset compared to uleb128 bytes to encode the old offset.
11435 GCC emits DW_TAG_base_type dies referenced that way at the
11436 beginning of the CU and we try to preserve that, so this shouldn't
11437 trigger for GCC generated code. */
11438 if (die->die_op_type_referenced
11439 && !wr_multifile
11440 && size_of_uleb128 (off)
11441 > size_of_uleb128 (die->die_offset - cu->cu_offset))
11442 return -1U;
11443 if ((*intracuvec)[0] == die)
11444 {
11445 unsigned int intracu_udata_size = 0;
11446 assert (die->u.p2.die_intracu_udata_size);
11447 while ((*intracuvec)[0] == die)
11448 {
11449 intracu_udata_size += intracusize;
11450 *intracuvec += 2;
11451 }
11452 if (intracusize != 0)
11453 {
11454 die->die_size
11455 -= die->u.p2.die_intracu_udata_size - intracu_udata_size;
11456 die->u.p2.die_intracu_udata_size = intracu_udata_size;
11457 }
11458 }
11459 else
11460 assert (die->u.p2.die_intracu_udata_size == 0 || ref_seen);
11461 off += die->die_size;
11462 for (child = die->die_child; child; child = child->die_sib)
11463 {
11464 off = finalize_new_die_offsets (cu, child, off, intracusize, intracuvec);
11465 if (off == -1U)
11466 return off;
11467 }
11468 if (die->die_child)
11469 off++;
11470 return off;
11471 }
11472
11473 /* Comparison function, called through qsort, to sort CUs
11474 by increasing number of needed new abbreviations. */
11475 static int
cu_abbrev_cmp(const void * p,const void * q)11476 cu_abbrev_cmp (const void *p, const void *q)
11477 {
11478 dw_cu_ref cu1 = *(dw_cu_ref *)p;
11479 dw_cu_ref cu2 = *(dw_cu_ref *)q;
11480 unsigned int nabbrevs1 = htab_elements (cu1->cu_new_abbrev);
11481 unsigned int nabbrevs2 = htab_elements (cu2->cu_new_abbrev);
11482
11483 if (nabbrevs1 < nabbrevs2)
11484 return -1;
11485 if (nabbrevs1 > nabbrevs2)
11486 return 1;
11487 /* The rest is just to get stable sort. */
11488 if (cu1->cu_kind != CU_PU && cu2->cu_kind == CU_PU)
11489 return -1;
11490 if (cu1->cu_kind == CU_PU && cu2->cu_kind != CU_PU)
11491 return 1;
11492 if (cu1->cu_offset < cu2->cu_offset)
11493 return -1;
11494 if (cu1->cu_offset > cu2->cu_offset)
11495 return 1;
11496 return 0;
11497 }
11498
11499 /* Compute new abbreviations for all CUs, size the new
11500 .debug_abbrev section and all new .debug_info CUs. */
11501 static int
compute_abbrevs(DSO * dso)11502 compute_abbrevs (DSO *dso)
11503 {
11504 unsigned long total_size = 0, types_size = 0, abbrev_size = 0;
11505 dw_cu_ref cu, *cuarr;
11506 struct abbrev_tag *t;
11507 unsigned int ncus, nlargeabbrevs = 0, i, laststart;
11508
11509 if (unlikely (progress_p))
11510 {
11511 report_progress ();
11512 fprintf (stderr, "compute_abbrevs\n");
11513 }
11514
11515 t = (struct abbrev_tag *)
11516 obstack_alloc (&ob2,
11517 sizeof (*t)
11518 + (max_nattr + 4) * sizeof (struct abbrev_attr)
11519 + (max_nattr + 4) * sizeof (int64_t));
11520 t->values = (int64_t *) &t->attr[max_nattr + 4];
11521 for (cu = first_cu, ncus = 0; cu; cu = cu->cu_next)
11522 {
11523 unsigned int intracu, ndies = 0, tagsize = 0, nchildren = 0;
11524 unsigned int nabbrevs, diesize, cusize, off, intracusize;
11525 struct abbrev_tag **arr;
11526 dw_die_ref *intracuarr, *intracuvec;
11527 enum dwarf_form intracuform = DW_FORM_ref4;
11528 dw_die_ref child, *lastotr, child_next, *last;
11529 unsigned int headersz = (cu->cu_kind == CU_TYPES
11530 ? 23 : (cu->cu_version >= 5 ? 12 : 11));
11531
11532 if (unlikely (fi_multifile) && cu->cu_die->die_remove)
11533 continue;
11534 if (unlikely (low_mem) && cu->cu_kind != CU_PU)
11535 expand_children (cu->cu_die);
11536 ncus++;
11537 if (build_abbrevs (cu, t, &ndies, &ob2))
11538 return 1;
11539 nabbrevs = htab_elements (cu->cu_new_abbrev);
11540 htab_traverse (cu->cu_new_abbrev, list_abbrevs, &ob);
11541 assert (obstack_object_size (&ob) == nabbrevs * sizeof (void *));
11542 arr = (struct abbrev_tag **) obstack_finish (&ob);
11543 intracu = obstack_object_size (&ob2) / sizeof (void *) / 2;
11544 obstack_ptr_grow (&ob2, NULL);
11545 intracuarr = (dw_die_ref *) obstack_finish (&ob2);
11546 if (nabbrevs >= 128)
11547 {
11548 unsigned int limit, uleb128_size;
11549
11550 for (child = cu->cu_die->die_child; child; child = child->die_sib)
11551 if (child->die_op_type_referenced && !wr_multifile)
11552 {
11553 child->u.p2.die_new_abbrev->op_type_referenced = 1;
11554 /* If the old offset was close to uleb128 boundary, ensure
11555 that DW_TAG_compile_unit gets small abbrev number
11556 as well. */
11557 if (size_of_uleb128 (child->die_offset - cu->cu_offset)
11558 < size_of_uleb128 (child->die_offset - cu->cu_offset + 1))
11559 cu->cu_die->u.p2.die_new_abbrev->op_type_referenced = 1;
11560 }
11561 qsort (arr, nabbrevs, sizeof (*arr), abbrev_cmp);
11562 for (i = 0, limit = 128, uleb128_size = 1; i < nabbrevs; i++)
11563 {
11564 if (i + 1 == limit)
11565 {
11566 limit <<= 7;
11567 uleb128_size++;
11568 }
11569 arr[i]->entry = i + 1;
11570 tagsize += arr[i]->nusers * uleb128_size;
11571 if (arr[i]->children)
11572 nchildren += arr[i]->nusers;
11573 }
11574 nlargeabbrevs++;
11575 }
11576 else
11577 {
11578 tagsize = ndies;
11579 for (i = 0; i < nabbrevs; i++)
11580 {
11581 arr[i]->entry = i + 1;
11582 if (arr[i]->children)
11583 nchildren += arr[i]->nusers;
11584 }
11585 }
11586
11587 /* Move all base types with die_op_type_reference
11588 to front, to increase the likelyhood that the offset
11589 will fit. */
11590 for (last = &cu->cu_die->die_child, lastotr = last, child = *last;
11591 child; child = child_next)
11592 {
11593 child_next = child->die_sib;
11594 if (child->die_op_type_referenced)
11595 {
11596 if (lastotr != last)
11597 {
11598 child->die_sib = *lastotr;
11599 *lastotr = child;
11600 lastotr = &child->die_sib;
11601 *last = child_next;
11602 continue;
11603 }
11604 lastotr = &child->die_sib;
11605 }
11606 last = &child->die_sib;
11607 }
11608
11609 cu->u2.cu_largest_entry = nabbrevs;
11610 diesize = calc_sizes (cu->cu_die);
11611 cusize = headersz + tagsize + diesize + nchildren;
11612 intracusize = size_of_uleb128 (cusize + intracu);
11613 do
11614 {
11615 i = size_of_uleb128 (cusize + intracu * intracusize);
11616 if (i == intracusize)
11617 break;
11618 intracusize = i;
11619 }
11620 while (1);
11621 cu->initial_intracusize = intracusize;
11622 off = init_new_die_offsets (cu->cu_die, headersz, intracusize);
11623 do
11624 {
11625 intracuvec = intracuarr;
11626 i = update_new_die_offsets (cu->cu_die, headersz, &intracuvec);
11627 assert (*intracuvec == NULL);
11628 if (i == off)
11629 break;
11630 assert (i < off);
11631 off = i;
11632 }
11633 while (1);
11634 if (cusize + intracu <= 256)
11635 {
11636 intracuform = DW_FORM_ref1;
11637 intracusize = 1;
11638 cusize += intracu;
11639 }
11640 else if (cusize + intracu * 2 <= 65536)
11641 {
11642 intracuform = DW_FORM_ref2;
11643 intracusize = 2;
11644 cusize += intracu * 2;
11645 }
11646 else
11647 {
11648 cusize += intracu * 4;
11649 intracusize = 4;
11650 }
11651 if (off <= cusize)
11652 {
11653 intracuform = DW_FORM_ref_udata;
11654 intracusize = 0;
11655 cusize = off;
11656 }
11657
11658 intracuvec = intracuarr;
11659 off = finalize_new_die_offsets (cu, cu->cu_die, headersz, intracusize,
11660 &intracuvec);
11661 if (off == -1U)
11662 {
11663 error (0, 0, "%s: DW_OP_call2 or typed DWARF stack referenced DIE"
11664 " layed out at too big offset", dso->filename);
11665 return 1;
11666 }
11667 assert (*intracuvec == NULL && off == cusize);
11668 cu->cu_intracu_form = intracuform;
11669
11670 if (intracuform != DW_FORM_ref4)
11671 {
11672 unsigned int j;
11673 htab_empty (cu->cu_new_abbrev);
11674 for (i = 0; i < nabbrevs; i++)
11675 {
11676 void **slot;
11677 for (j = 0; j < arr[i]->nattr; j++)
11678 if (arr[i]->attr[j].form == DW_FORM_ref4)
11679 arr[i]->attr[j].form = intracuform;
11680 compute_abbrev_hash (arr[i]);
11681 slot = htab_find_slot_with_hash (cu->cu_new_abbrev, arr[i],
11682 arr[i]->hash, INSERT);
11683 if (slot == NULL)
11684 dwz_oom ();
11685 assert (slot != NULL && *slot == NULL);
11686 *slot = arr[i];
11687 }
11688 }
11689 obstack_free (&ob, (void *) arr);
11690 obstack_free (&ob2, (void *) intracuarr);
11691 if (cu->cu_kind == CU_TYPES)
11692 {
11693 cu->cu_new_offset = types_size;
11694 types_size += cusize;
11695 }
11696 else
11697 {
11698 cu->cu_new_offset = (wr_multifile ? multi_info_off : 0) + total_size;
11699 total_size += cusize;
11700 }
11701
11702 if (unlikely (low_mem) && cu->cu_kind != CU_PU)
11703 collapse_children (cu, cu->cu_die);
11704 }
11705 if (wr_multifile)
11706 total_size += 11; /* See the end of write_info. */
11707 obstack_free (&ob2, (void *) t);
11708 cuarr = (dw_cu_ref *) obstack_alloc (&ob2, ncus * sizeof (dw_cu_ref));
11709 for (cu = first_cu, i = 0; cu; cu = cu->cu_next)
11710 if (cu->u1.cu_new_abbrev_owner == NULL
11711 && (likely (!fi_multifile)
11712 || cu->cu_kind != CU_NORMAL
11713 || !cu->cu_die->die_remove))
11714 cuarr[i++] = cu;
11715 assert (i == ncus);
11716 qsort (cuarr, ncus, sizeof (dw_cu_ref), cu_abbrev_cmp);
11717 /* For CUs with < 128 abbrevs, try to see if either all of the
11718 abbrevs are at < 128 positions in >= 128 abbrev CUs, or
11719 can be merged with some other small abbrev table to form
11720 a < 128 abbrev table. */
11721 laststart = ncus - nlargeabbrevs;
11722 for (i = ncus - 1; i != -1U; i--)
11723 {
11724 struct abbrev_tag **arr;
11725 unsigned int nabbrevs, j, k, nattempts;
11726
11727 if (cuarr[i]->u1.cu_new_abbrev_owner != NULL)
11728 continue;
11729 nabbrevs = htab_elements (cuarr[i]->cu_new_abbrev);
11730 htab_traverse (cuarr[i]->cu_new_abbrev, list_abbrevs, &ob2);
11731 assert (obstack_object_size (&ob2) == nabbrevs * sizeof (void *));
11732 arr = (struct abbrev_tag **) obstack_finish (&ob2);
11733 if (nabbrevs >= 128)
11734 {
11735 nattempts = 0;
11736 for (j = i + 1; j < ncus; j++)
11737 {
11738 unsigned int entry;
11739 if (cuarr[j]->u1.cu_new_abbrev_owner)
11740 continue;
11741 if (++nattempts == 100)
11742 break;
11743 entry = cuarr[j]->u2.cu_largest_entry;
11744 for (k = 0; k < nabbrevs; k++)
11745 {
11746 struct abbrev_tag *t
11747 = htab_find_with_hash (cuarr[j]->cu_new_abbrev,
11748 arr[k], arr[k]->hash);
11749 if (t == NULL)
11750 {
11751 ++entry;
11752 if (size_of_uleb128 (entry)
11753 != size_of_uleb128 (arr[k]->entry))
11754 break;
11755 }
11756 else if (size_of_uleb128 (t->entry)
11757 != size_of_uleb128 (arr[k]->entry))
11758 break;
11759 }
11760 if (k != nabbrevs)
11761 continue;
11762 entry = cuarr[j]->u2.cu_largest_entry;
11763 for (k = 0; k < nabbrevs; k++)
11764 {
11765 void **slot
11766 = htab_find_slot_with_hash (cuarr[j]->cu_new_abbrev,
11767 arr[k], arr[k]->hash,
11768 INSERT);
11769 if (slot == NULL)
11770 dwz_oom ();
11771 if (*slot != NULL)
11772 arr[k]->entry = ((struct abbrev_tag *) *slot)->entry;
11773 else
11774 {
11775 struct abbrev_tag *newt;
11776 arr[k]->entry = ++entry;
11777 newt = pool_clone_abbrev (arr[k]);
11778 *slot = newt;
11779 }
11780 }
11781 cuarr[j]->u2.cu_largest_entry = entry;
11782 cuarr[i]->u1.cu_new_abbrev_owner = cuarr[j];
11783 break;
11784 }
11785 obstack_free (&ob2, (void *) arr);
11786 continue;
11787 }
11788 /* Don't search all CUs, that might be too expensive. So just search
11789 100 of >= 128 abbrev tables, if there are more than 100, different
11790 set each time. We are looking for a full match (i.e. that
11791 cuarr[i] abbrevs are a subset of cuarr[j] abbrevs, and all of them
11792 are in the low positions. */
11793 for (j = laststart, nattempts = -1U; nlargeabbrevs; j++)
11794 {
11795 if (j == ncus)
11796 j -= nlargeabbrevs;
11797 if (nattempts != -1U && j == laststart)
11798 break;
11799 if (nattempts == -1U)
11800 nattempts = 0;
11801 if (cuarr[j]->u1.cu_new_abbrev_owner)
11802 continue;
11803 if (++nattempts == 100)
11804 break;
11805 for (k = 0; k < nabbrevs; k++)
11806 {
11807 struct abbrev_tag *t
11808 = htab_find_with_hash (cuarr[j]->cu_new_abbrev,
11809 arr[k], arr[k]->hash);
11810 if (t == NULL || t->entry >= 128)
11811 break;
11812 }
11813 if (k == nabbrevs)
11814 {
11815 for (k = 0; k < nabbrevs; k++)
11816 {
11817 struct abbrev_tag *t
11818 = htab_find_with_hash (cuarr[j]->cu_new_abbrev,
11819 arr[k], arr[k]->hash);
11820 arr[k]->entry = t->entry;
11821 }
11822 cuarr[i]->u1.cu_new_abbrev_owner = cuarr[j];
11823 break;
11824 }
11825 }
11826 if (nlargeabbrevs > 100)
11827 laststart = j;
11828 if (cuarr[i]->u1.cu_new_abbrev_owner == NULL)
11829 {
11830 unsigned int maxdups = 0, maxdupidx = 0;
11831 /* Next search up to 100 of small abbrev CUs, looking
11832 for best match. */
11833 nattempts = 0;
11834 for (j = i + 1; j < ncus - nlargeabbrevs; j++)
11835 {
11836 unsigned int curdups = 0;
11837 if (cuarr[j]->u1.cu_new_abbrev_owner)
11838 continue;
11839 if (++nattempts == 100)
11840 break;
11841 for (k = 0; k < nabbrevs; k++)
11842 {
11843 struct abbrev_tag *t
11844 = htab_find_with_hash (cuarr[j]->cu_new_abbrev,
11845 arr[k], arr[k]->hash);
11846 if (t != NULL)
11847 curdups++;
11848 }
11849 if (curdups > maxdups
11850 && cuarr[j]->u2.cu_largest_entry - curdups + nabbrevs < 128)
11851 {
11852 maxdups = curdups;
11853 maxdupidx = j;
11854 if (maxdups == nabbrevs)
11855 break;
11856 }
11857 }
11858 if (maxdups)
11859 {
11860 unsigned int entry = cuarr[maxdupidx]->u2.cu_largest_entry;
11861 j = maxdupidx;
11862 for (k = 0; k < nabbrevs; k++)
11863 {
11864 void **slot
11865 = htab_find_slot_with_hash (cuarr[j]->cu_new_abbrev,
11866 arr[k], arr[k]->hash,
11867 INSERT);
11868 if (slot == NULL)
11869 dwz_oom ();
11870 if (*slot != NULL)
11871 arr[k]->entry = ((struct abbrev_tag *) *slot)->entry;
11872 else
11873 {
11874 struct abbrev_tag *newt;
11875 arr[k]->entry = ++entry;
11876 newt = pool_clone_abbrev (arr[k]);
11877 *slot = newt;
11878 }
11879 }
11880 cuarr[j]->u2.cu_largest_entry = entry;
11881 cuarr[i]->u1.cu_new_abbrev_owner = cuarr[j];
11882 }
11883 }
11884 obstack_free (&ob2, (void *) arr);
11885 }
11886 obstack_free (&ob2, (void *) t);
11887 for (cu = first_cu; cu; cu = cu->cu_next)
11888 {
11889 struct abbrev_tag **arr;
11890 unsigned int nabbrevs, j;
11891
11892 if (unlikely (fi_multifile)
11893 && cu->cu_kind == CU_NORMAL
11894 && cu->cu_die->die_remove)
11895 continue;
11896 if (cu->u1.cu_new_abbrev_owner != NULL)
11897 {
11898 cu->u2.cu_new_abbrev_offset = -1U;
11899 if (cu->cu_new_abbrev)
11900 htab_delete (cu->cu_new_abbrev);
11901 cu->cu_new_abbrev = NULL;
11902 continue;
11903 }
11904 cu->u2.cu_new_abbrev_offset
11905 = (wr_multifile ? multi_abbrev_off : 0) + abbrev_size;
11906 nabbrevs = htab_elements (cu->cu_new_abbrev);
11907 htab_traverse (cu->cu_new_abbrev, list_abbrevs, &ob);
11908 assert (obstack_object_size (&ob) == nabbrevs * sizeof (void *));
11909 arr = (struct abbrev_tag **) obstack_finish (&ob);
11910 for (i = 0; i < nabbrevs; i++)
11911 {
11912 abbrev_size += size_of_uleb128 (arr[i]->entry);
11913 abbrev_size += size_of_uleb128 (arr[i]->tag);
11914 abbrev_size += 1;
11915 for (j = 0; j < arr[i]->nattr; j++)
11916 {
11917 abbrev_size += size_of_uleb128 (arr[i]->attr[j].attr);
11918 abbrev_size += size_of_uleb128 (arr[i]->attr[j].form);
11919 if (arr[i]->attr[j].form == DW_FORM_implicit_const)
11920 abbrev_size += size_of_sleb128 (arr[i]->values[j]);
11921 }
11922 abbrev_size += 2;
11923 }
11924 abbrev_size += 1;
11925 obstack_free (&ob, (void *) arr);
11926 }
11927 for (cu = first_cu; cu; cu = cu->cu_next)
11928 if (unlikely (fi_multifile)
11929 && cu->cu_kind == CU_NORMAL
11930 && cu->cu_die->die_remove)
11931 continue;
11932 else if (cu->u2.cu_new_abbrev_offset == -1U)
11933 {
11934 dw_cu_ref owner = cu;
11935 unsigned int cu_new_abbrev_offset;
11936 while (owner->u1.cu_new_abbrev_owner != NULL)
11937 owner = owner->u1.cu_new_abbrev_owner;
11938 cu_new_abbrev_offset = owner->u2.cu_new_abbrev_offset;
11939 owner = cu;
11940 while (owner->u1.cu_new_abbrev_owner != NULL)
11941 {
11942 owner->u2.cu_new_abbrev_offset = cu_new_abbrev_offset;
11943 owner = owner->u1.cu_new_abbrev_owner;
11944 }
11945 }
11946 debug_sections[DEBUG_INFO].new_size = total_size;
11947 debug_sections[DEBUG_ABBREV].new_size = abbrev_size;
11948 debug_sections[DEBUG_TYPES].new_size = types_size;
11949 return 0;
11950 }
11951
11952 /* Comparison function, sort abbreviations by increasing
11953 entry value. */
11954 static int
abbrev_entry_cmp(const void * p,const void * q)11955 abbrev_entry_cmp (const void *p, const void *q)
11956 {
11957 struct abbrev_tag *t1 = *(struct abbrev_tag **)p;
11958 struct abbrev_tag *t2 = *(struct abbrev_tag **)q;
11959
11960 if (t1->entry < t2->entry)
11961 return -1;
11962 if (t1->entry > t2->entry)
11963 return 1;
11964 return 0;
11965 }
11966
11967 /* Construct the new .debug_abbrev section
11968 in malloced memory, store it as debug_sections[DEBUG_ABBREV].new_data. */
11969 static void
write_abbrev(void)11970 write_abbrev (void)
11971 {
11972 dw_cu_ref cu;
11973 unsigned char *abbrev = malloc (debug_sections[DEBUG_ABBREV].new_size);
11974 unsigned char *ptr = abbrev;
11975
11976 if (unlikely (progress_p))
11977 {
11978 report_progress ();
11979 fprintf (stderr, "write_abbrev\n");
11980 }
11981
11982 if (abbrev == NULL)
11983 dwz_oom ();
11984
11985 debug_sections[DEBUG_ABBREV].new_data = abbrev;
11986 for (cu = first_cu; cu; cu = cu->cu_next)
11987 {
11988 struct abbrev_tag **arr;
11989 unsigned int nabbrevs, i, j;
11990
11991 if (unlikely (fi_multifile)
11992 && cu->cu_kind == CU_NORMAL
11993 && cu->cu_die->die_remove)
11994 continue;
11995 if (cu->u1.cu_new_abbrev_owner != NULL)
11996 continue;
11997 nabbrevs = htab_elements (cu->cu_new_abbrev);
11998 htab_traverse (cu->cu_new_abbrev, list_abbrevs, &ob);
11999 assert (obstack_object_size (&ob) == nabbrevs * sizeof (void *));
12000 arr = (struct abbrev_tag **) obstack_finish (&ob);
12001 qsort (arr, nabbrevs, sizeof (*arr), abbrev_entry_cmp);
12002 for (i = 0; i < nabbrevs; i++)
12003 {
12004 write_uleb128 (ptr, arr[i]->entry);
12005 write_uleb128 (ptr, arr[i]->tag);
12006 *ptr++ = arr[i]->children ? DW_CHILDREN_yes : DW_CHILDREN_no;
12007 for (j = 0; j < arr[i]->nattr; j++)
12008 {
12009 write_uleb128 (ptr, arr[i]->attr[j].attr);
12010 write_uleb128 (ptr, arr[i]->attr[j].form);
12011 if (arr[i]->attr[j].form == DW_FORM_implicit_const)
12012 write_sleb128 (ptr, arr[i]->values[j]);
12013 }
12014 *ptr++ = 0;
12015 *ptr++ = 0;
12016 }
12017 *ptr++ = 0;
12018 obstack_free (&ob, (void *) arr);
12019 if (likely (!low_mem))
12020 {
12021 htab_delete (cu->cu_new_abbrev);
12022 cu->cu_new_abbrev = NULL;
12023 }
12024 }
12025 assert (abbrev + debug_sections[DEBUG_ABBREV].new_size == ptr);
12026 }
12027
12028 /* Adjust DWARF expression starting at PTR, LEN bytes long, referenced by
12029 DIE, with REF being the original DIE. */
12030 static void
adjust_exprloc(dw_cu_ref cu,dw_die_ref die,dw_cu_ref refcu,dw_die_ref ref,unsigned char * ptr,size_t len)12031 adjust_exprloc (dw_cu_ref cu, dw_die_ref die, dw_cu_ref refcu,
12032 dw_die_ref ref, unsigned char *ptr, size_t len)
12033 {
12034 unsigned char *end = ptr + len, *orig_ptr = NULL;
12035 unsigned char op;
12036 uint32_t leni;
12037 GElf_Addr addr;
12038 dw_die_ref refd, refdt;
12039
12040 while (ptr < end)
12041 {
12042 op = *ptr++;
12043 switch (op)
12044 {
12045 case DW_OP_addr:
12046 ptr += ptr_size;
12047 break;
12048 case DW_OP_deref:
12049 case DW_OP_dup:
12050 case DW_OP_drop:
12051 case DW_OP_over:
12052 case DW_OP_swap:
12053 case DW_OP_rot:
12054 case DW_OP_xderef:
12055 case DW_OP_abs:
12056 case DW_OP_and:
12057 case DW_OP_div:
12058 case DW_OP_minus:
12059 case DW_OP_mod:
12060 case DW_OP_mul:
12061 case DW_OP_neg:
12062 case DW_OP_not:
12063 case DW_OP_or:
12064 case DW_OP_plus:
12065 case DW_OP_shl:
12066 case DW_OP_shr:
12067 case DW_OP_shra:
12068 case DW_OP_xor:
12069 case DW_OP_eq:
12070 case DW_OP_ge:
12071 case DW_OP_gt:
12072 case DW_OP_le:
12073 case DW_OP_lt:
12074 case DW_OP_ne:
12075 case DW_OP_lit0 ... DW_OP_lit31:
12076 case DW_OP_reg0 ... DW_OP_reg31:
12077 case DW_OP_nop:
12078 case DW_OP_push_object_address:
12079 case DW_OP_form_tls_address:
12080 case DW_OP_call_frame_cfa:
12081 case DW_OP_stack_value:
12082 case DW_OP_GNU_push_tls_address:
12083 case DW_OP_GNU_uninit:
12084 break;
12085 case DW_OP_const1u:
12086 case DW_OP_pick:
12087 case DW_OP_deref_size:
12088 case DW_OP_xderef_size:
12089 case DW_OP_const1s:
12090 ++ptr;
12091 break;
12092 case DW_OP_const2u:
12093 case DW_OP_const2s:
12094 case DW_OP_skip:
12095 case DW_OP_bra:
12096 ptr += 2;
12097 break;
12098 case DW_OP_call2:
12099 case DW_OP_call4:
12100 case DW_OP_GNU_parameter_ref:
12101 if (op == DW_OP_call2)
12102 addr = read_16 (ptr);
12103 else
12104 addr = read_32 (ptr);
12105 refd = off_htab_lookup (refcu, refcu->cu_offset + addr);
12106 assert (refd != NULL && !refd->die_remove);
12107 if (op == DW_OP_call2)
12108 {
12109 assert (refd->u.p2.die_new_offset <= 65535);
12110 ptr -= 2;
12111 write_16 (ptr, refd->u.p2.die_new_offset);
12112 }
12113 else
12114 {
12115 ptr -= 4;
12116 write_32 (ptr, refd->u.p2.die_new_offset);
12117 }
12118 break;
12119 case DW_OP_const4u:
12120 case DW_OP_const4s:
12121 ptr += 4;
12122 break;
12123 case DW_OP_call_ref:
12124 case DW_OP_GNU_implicit_pointer:
12125 case DW_OP_implicit_pointer:
12126 case DW_OP_GNU_variable_value:
12127 addr = read_size (ptr, refcu->cu_version == 2 ? ptr_size : 4);
12128 assert (cu->cu_version == refcu->cu_version);
12129 refd = off_htab_lookup (NULL, addr);
12130 assert (refd != NULL);
12131 refdt = refd;
12132 while (refdt->die_toplevel == 0)
12133 refdt = refdt->die_parent;
12134 if (refdt->die_dup && !refdt->die_op_type_referenced)
12135 refd = die_find_dup (refdt, refdt->die_dup, refd);
12136 write_size (ptr, cu->cu_version == 2 ? ptr_size : 4,
12137 die_cu (refd)->cu_new_offset
12138 + refd->u.p2.die_new_offset);
12139 if (cu->cu_version == 2)
12140 ptr += ptr_size;
12141 else
12142 ptr += 4;
12143 if (op == DW_OP_GNU_implicit_pointer || op == DW_OP_implicit_pointer)
12144 skip_leb128 (ptr);
12145 break;
12146 case DW_OP_const8u:
12147 case DW_OP_const8s:
12148 ptr += 8;
12149 break;
12150 case DW_OP_constu:
12151 case DW_OP_plus_uconst:
12152 case DW_OP_regx:
12153 case DW_OP_piece:
12154 case DW_OP_consts:
12155 case DW_OP_breg0 ... DW_OP_breg31:
12156 case DW_OP_fbreg:
12157 skip_leb128 (ptr);
12158 break;
12159 case DW_OP_bregx:
12160 case DW_OP_bit_piece:
12161 skip_leb128 (ptr);
12162 skip_leb128 (ptr);
12163 break;
12164 case DW_OP_implicit_value:
12165 leni = read_uleb128 (ptr);
12166 ptr += leni;
12167 break;
12168 case DW_OP_GNU_entry_value:
12169 case DW_OP_entry_value:
12170 leni = read_uleb128 (ptr);
12171 assert ((uint64_t) (end - ptr) >= leni);
12172 adjust_exprloc (cu, die, refcu, ref, ptr, leni);
12173 ptr += leni;
12174 break;
12175 case DW_OP_GNU_convert:
12176 case DW_OP_convert:
12177 case DW_OP_GNU_reinterpret:
12178 case DW_OP_reinterpret:
12179 orig_ptr = ptr;
12180 addr = read_uleb128 (ptr);
12181 if (addr == 0)
12182 break;
12183 goto typed_dwarf;
12184 case DW_OP_GNU_regval_type:
12185 case DW_OP_regval_type:
12186 skip_leb128 (ptr);
12187 orig_ptr = ptr;
12188 addr = read_uleb128 (ptr);
12189 goto typed_dwarf;
12190 case DW_OP_GNU_const_type:
12191 case DW_OP_const_type:
12192 orig_ptr = ptr;
12193 addr = read_uleb128 (ptr);
12194 goto typed_dwarf;
12195 case DW_OP_GNU_deref_type:
12196 case DW_OP_deref_type:
12197 ++ptr;
12198 orig_ptr = ptr;
12199 addr = read_uleb128 (ptr);
12200 typed_dwarf:
12201 refd = off_htab_lookup (refcu, refcu->cu_offset + addr);
12202 assert (refd != NULL && refd->die_op_type_referenced);
12203 leni = ptr - orig_ptr;
12204 assert (size_of_uleb128 (refd->u.p2.die_new_offset) <= leni);
12205 ptr = orig_ptr;
12206 write_uleb128 (ptr, refd->u.p2.die_new_offset);
12207 /* If the new offset ends up being shorter uleb128
12208 encoded than the old, pad it up to make it still valid,
12209 but not shortest, uleb128. Changing sizes of
12210 exprloc would be a nightmare. Another alternative would
12211 be to pad with DW_OP_nop after the op. */
12212 if (ptr < orig_ptr + leni)
12213 {
12214 ptr[-1] |= 0x80;
12215 while (ptr < orig_ptr + leni - 1)
12216 *ptr++ = 0x80;
12217 *ptr++ = 0;
12218 }
12219 if (op == DW_OP_GNU_const_type || op == DW_OP_const_type)
12220 ptr += *ptr + 1;
12221 break;
12222 default:
12223 abort ();
12224 }
12225 }
12226 }
12227
12228 /* Write DW_TAG_unit_* DIE (with ORIGIN being the corresponding original DIE) to
12229 memory starting at PTR, return pointer after the DIE. */
12230 static unsigned char *
write_unit_die(unsigned char * ptr,dw_die_ref die,dw_die_ref origin)12231 write_unit_die (unsigned char *ptr, dw_die_ref die, dw_die_ref origin)
12232 {
12233 struct abbrev_tag *t = die->u.p2.die_new_abbrev;
12234 unsigned int i;
12235
12236 for (i = 0; i < t->nattr; ++i)
12237 {
12238 struct abbrev_attr *attr = &t->attr[i];
12239 switch (attr->attr)
12240 {
12241 case DW_AT_stmt_list:
12242 {
12243 enum dwarf_form form;
12244 unsigned char *p = get_AT (origin, DW_AT_stmt_list, &form);
12245 assert (p && (form == DW_FORM_sec_offset
12246 || form == DW_FORM_data4));
12247 if (wr_multifile)
12248 write_32 (ptr, multi_line_off);
12249 else if (op_multifile)
12250 write_32 (ptr, 0);
12251 else
12252 {
12253 memcpy (ptr, p, 4);
12254 ptr += 4;
12255 }
12256 }
12257 break;
12258 case DW_AT_comp_dir:
12259 {
12260 enum dwarf_form form;
12261 unsigned char *p = get_AT (origin, DW_AT_comp_dir, &form);
12262 assert (p);
12263 assert (form == attr->form
12264 || (form == DW_FORM_strp
12265 && (attr->form == DW_FORM_GNU_strp_alt
12266 || attr->form == DW_FORM_strp_sup)));
12267 if (form == DW_FORM_strp)
12268 {
12269 if (unlikely (wr_multifile || op_multifile || fi_multifile))
12270 {
12271 unsigned int strp = lookup_strp_offset (read_32 (p));
12272 write_32 (ptr, strp);
12273 }
12274 else
12275 {
12276 memcpy (ptr, p, 4);
12277 ptr += 4;
12278 }
12279 }
12280 else if (form == DW_FORM_line_strp)
12281 {
12282 memcpy (ptr, p, 4);
12283 ptr += 4;
12284 }
12285 else
12286 {
12287 size_t len = strlen ((char *) p) + 1;
12288 memcpy (ptr, p, len);
12289 ptr += len;
12290 }
12291 }
12292 break;
12293 case DW_AT_language:
12294 {
12295 enum dwarf_source_language lang = die_cu (die)->lang;
12296 unsigned int lang_size = nr_bytes_for (lang);
12297 write_size (ptr, lang_size, lang);
12298 ptr += lang_size;
12299 }
12300 break;
12301 default:
12302 assert (false);
12303 break;
12304 }
12305 }
12306
12307 return ptr;
12308 }
12309
12310 /* Write DIE (with REF being the corresponding original DIE) to
12311 memory starting at PTR, return pointer after the DIE. */
12312 static unsigned char *
write_die(unsigned char * ptr,dw_cu_ref cu,dw_die_ref die,dw_cu_ref refcu,dw_die_ref ref,unsigned int * die_count)12313 write_die (unsigned char *ptr, dw_cu_ref cu, dw_die_ref die,
12314 dw_cu_ref refcu, dw_die_ref ref, unsigned int *die_count)
12315 {
12316 uint64_t low_pc = 0;
12317 dw_die_ref child, sib = NULL, origin = NULL;
12318 struct abbrev_tag *t;
12319
12320 if (wr_multifile ? die->die_no_multifile : die->die_remove)
12321 return ptr;
12322 if (die_count)
12323 (*die_count)++;
12324 if (die->die_offset == -1U)
12325 {
12326 if (ref != NULL)
12327 ;
12328 else if (die_safe_nextdup (die) && die->die_nextdup->die_dup == die)
12329 {
12330 ref = die->die_nextdup;
12331 refcu = die_cu (ref);
12332 }
12333 if (ref == NULL)
12334 origin = die->die_nextdup;
12335 }
12336 else
12337 {
12338 ref = die;
12339 refcu = cu;
12340 if (wr_multifile
12341 && (die->die_root || die->die_named_namespace))
12342 origin = die;
12343 }
12344 if (die->die_child && die->die_sib)
12345 for (sib = die->die_sib; sib; sib = sib->die_sib)
12346 if (wr_multifile ? !sib->die_no_multifile : !sib->die_remove)
12347 break;
12348 t = die->u.p2.die_new_abbrev;
12349 write_uleb128 (ptr, t->entry);
12350 if (ref != NULL && origin == NULL)
12351 {
12352 unsigned char *base
12353 = cu->cu_kind == CU_TYPES
12354 ? debug_sections[DEBUG_TYPES].data
12355 : debug_sections[DEBUG_INFO].data;
12356 unsigned char *inptr = base + ref->die_offset;
12357 struct abbrev_tag *reft = ref->die_abbrev;
12358 unsigned int i, j;
12359
12360 skip_leb128 (inptr);
12361 for (i = 0, j = 0; i < reft->nattr; ++i)
12362 {
12363 uint32_t form = reft->attr[i].form;
12364 size_t len = 0;
12365 uint64_t value;
12366 unsigned char *orig_ptr = inptr;
12367
12368 while (form == DW_FORM_indirect)
12369 form = read_uleb128 (inptr);
12370
12371 if (unlikely (wr_multifile || op_multifile)
12372 && (reft->attr[i].attr == DW_AT_decl_file
12373 || reft->attr[i].attr == DW_AT_call_file))
12374 {
12375 switch (form)
12376 {
12377 case DW_FORM_data1:
12378 value = read_8 (inptr);
12379 break;
12380 case DW_FORM_data2:
12381 value = read_16 (inptr);
12382 break;
12383 case DW_FORM_data4:
12384 value = read_32 (inptr);
12385 break;
12386 case DW_FORM_data8:
12387 value = read_64 (inptr);
12388 break;
12389 case DW_FORM_udata:
12390 value = read_uleb128 (inptr);
12391 break;
12392 case DW_FORM_sdata:
12393 value = read_sleb128 (inptr);
12394 break;
12395 case DW_FORM_implicit_const:
12396 /* DW_FORM_implicit_const should have been updated
12397 already when computing abbrevs. */
12398 j++;
12399 continue;
12400 default: abort ();
12401 }
12402 value = line_htab_lookup (refcu, value);
12403 switch (t->attr[j].form)
12404 {
12405 case DW_FORM_data1: write_8 (ptr, value); break;
12406 case DW_FORM_data2: write_16 (ptr, value); break;
12407 case DW_FORM_data4: write_32 (ptr, value); break;
12408 case DW_FORM_data8: write_64 (ptr, value); break;
12409 case DW_FORM_udata: write_uleb128 (ptr, value); break;
12410 case DW_FORM_sdata: write_sleb128 (ptr, value); break;
12411 default: abort ();
12412 }
12413 j++;
12414 continue;
12415 }
12416
12417 if (unlikely (fi_multifile)
12418 && (reft->attr[i].attr == DW_AT_GNU_macros
12419 || reft->attr[i].attr == DW_AT_macros)
12420 && alt_macro_htab != NULL)
12421 {
12422 struct macro_entry me, *m;
12423
12424 memcpy (ptr, orig_ptr, inptr - orig_ptr);
12425 ptr += inptr - orig_ptr;
12426 value = read_32 (inptr);
12427 me.ptr = debug_sections[DEBUG_MACRO].data + value;
12428 m = (struct macro_entry *)
12429 htab_find_with_hash (macro_htab, &me, value);
12430 write_32 (ptr, m->hash);
12431 j++;
12432 continue;
12433 }
12434
12435 switch (form)
12436 {
12437 case DW_FORM_ref_addr:
12438 {
12439 dw_die_ref refd, refdt;
12440 if (t->attr[j].form != DW_FORM_GNU_ref_alt
12441 && t->attr[j].form != DW_FORM_ref_sup4)
12442 {
12443 memcpy (ptr, orig_ptr, inptr - orig_ptr);
12444 ptr += inptr - orig_ptr;
12445 }
12446 value = read_size (inptr, refcu->cu_version == 2
12447 ? ptr_size : 4);
12448 inptr += refcu->cu_version == 2 ? ptr_size : 4;
12449 refd = off_htab_lookup (NULL, value);
12450 if (refd == NULL || refd->die_tag == 0)
12451 error (1, 0, "Couldn't find DIE at DW_FORM_ref_addr offset"
12452 " 0x%" PRIx64, value);
12453 assert (refd != NULL);
12454 refdt = refd;
12455 while (refdt->die_toplevel == 0)
12456 refdt = refdt->die_parent;
12457 if (refdt->die_dup && !refdt->die_op_type_referenced)
12458 {
12459 refd = die_find_dup (refdt, refdt->die_dup, refd);
12460 if (t->attr[j].form == DW_FORM_GNU_ref_alt
12461 || t->attr[j].form == DW_FORM_ref_sup4)
12462 {
12463 assert (die_cu (refd)->cu_kind == CU_ALT);
12464 write_32 (ptr, refd->die_offset);
12465 j++;
12466 continue;
12467 }
12468 }
12469 assert (refd->u.p2.die_new_offset
12470 && t->attr[j].form != DW_FORM_GNU_ref_alt
12471 && t->attr[j].form != DW_FORM_ref_sup4);
12472 value = die_cu (refd)->cu_new_offset
12473 + refd->u.p2.die_new_offset;
12474 write_size (ptr, cu->cu_version == 2 ? ptr_size : 4,
12475 value);
12476 ptr += cu->cu_version == 2 ? ptr_size : 4;
12477 if (unlikely (op_multifile))
12478 assert (die_cu (refd)->cu_kind == CU_PU);
12479 j++;
12480 continue;
12481 }
12482 case DW_FORM_addr:
12483 inptr += ptr_size;
12484 if (reft->attr[i].attr == DW_AT_low_pc)
12485 low_pc = read_size (inptr - ptr_size, ptr_size);
12486 if (reft->attr[i].attr == DW_AT_high_pc
12487 && t->attr[j].form != reft->attr[i].form)
12488 {
12489 uint64_t high_pc = read_size (inptr - ptr_size, ptr_size);
12490 switch (t->attr[j].form)
12491 {
12492 case DW_FORM_udata:
12493 write_uleb128 (ptr, high_pc - low_pc);
12494 break;
12495 case DW_FORM_data4:
12496 write_32 (ptr, high_pc - low_pc);
12497 break;
12498 default:
12499 abort ();
12500 }
12501 j++;
12502 continue;
12503 }
12504 break;
12505 case DW_FORM_flag_present:
12506 case DW_FORM_implicit_const:
12507 break;
12508 case DW_FORM_flag:
12509 case DW_FORM_data1:
12510 ++inptr;
12511 break;
12512 case DW_FORM_data2:
12513 inptr += 2;
12514 break;
12515 case DW_FORM_data4:
12516 if (reft->attr[i].attr == DW_AT_high_pc
12517 && t->attr[j].form != reft->attr[i].form)
12518 {
12519 uint32_t range_len = read_32 (inptr);
12520 switch (t->attr[j].form)
12521 {
12522 case DW_FORM_udata:
12523 write_uleb128 (ptr, range_len);
12524 break;
12525 default:
12526 abort ();
12527 }
12528 j++;
12529 continue;
12530 }
12531 inptr += 4;
12532 break;
12533 case DW_FORM_sec_offset:
12534 inptr += 4;
12535 break;
12536 case DW_FORM_data8:
12537 if (reft->attr[i].attr == DW_AT_high_pc
12538 && t->attr[j].form != reft->attr[i].form)
12539 {
12540 uint64_t range_len = read_64 (inptr);
12541 switch (t->attr[j].form)
12542 {
12543 case DW_FORM_udata:
12544 write_uleb128 (ptr, range_len);
12545 break;
12546 case DW_FORM_data4:
12547 write_32 (ptr, range_len);
12548 break;
12549 default:
12550 abort ();
12551 }
12552 j++;
12553 continue;
12554 }
12555 inptr += 8;
12556 break;
12557 case DW_FORM_ref_sig8:
12558 inptr += 8;
12559 break;
12560 case DW_FORM_data16:
12561 inptr += 16;
12562 break;
12563 case DW_FORM_sdata:
12564 case DW_FORM_udata:
12565 skip_leb128 (inptr);
12566 break;
12567 case DW_FORM_strp:
12568 if (unlikely (wr_multifile || op_multifile || fi_multifile))
12569 {
12570 unsigned int strp = lookup_strp_offset (read_32 (inptr));
12571 memcpy (ptr, orig_ptr, inptr - 4 - orig_ptr);
12572 ptr += inptr - 4 - orig_ptr;
12573 write_32 (ptr, strp);
12574 j++;
12575 continue;
12576 }
12577 inptr += 4;
12578 break;
12579 case DW_FORM_line_strp:
12580 inptr += 4;
12581 break;
12582 case DW_FORM_string:
12583 inptr = (unsigned char *) strchr ((char *)inptr, '\0') + 1;
12584 break;
12585 case DW_FORM_indirect:
12586 abort ();
12587 case DW_FORM_block1:
12588 len = *inptr++;
12589 break;
12590 case DW_FORM_block2:
12591 len = read_16 (inptr);
12592 form = DW_FORM_block1;
12593 break;
12594 case DW_FORM_block4:
12595 len = read_32 (inptr);
12596 form = DW_FORM_block1;
12597 break;
12598 case DW_FORM_block:
12599 len = read_uleb128 (inptr);
12600 form = DW_FORM_block1;
12601 break;
12602 case DW_FORM_exprloc:
12603 len = read_uleb128 (inptr);
12604 break;
12605 case DW_FORM_ref1:
12606 case DW_FORM_ref2:
12607 case DW_FORM_ref4:
12608 case DW_FORM_ref8:
12609 case DW_FORM_ref_udata:
12610 switch (form)
12611 {
12612 case DW_FORM_ref1: value = read_8 (inptr); break;
12613 case DW_FORM_ref2: value = read_16 (inptr); break;
12614 case DW_FORM_ref4: value = read_32 (inptr); break;
12615 case DW_FORM_ref8: value = read_64 (inptr); break;
12616 case DW_FORM_ref_udata: value = read_uleb128 (inptr); break;
12617 default: abort ();
12618 }
12619 if (reft->attr[i].attr == DW_AT_sibling)
12620 {
12621 if (j == t->nattr
12622 || t->attr[j].attr != DW_AT_sibling)
12623 continue;
12624 assert (sib);
12625 value = sib->u.p2.die_new_offset;
12626 }
12627 else
12628 {
12629 dw_die_ref refdt, refd
12630 = off_htab_lookup (refcu, refcu->cu_offset + value);
12631 assert (refd != NULL);
12632 refdt = refd;
12633 while (refdt->die_toplevel == 0)
12634 refdt = refdt->die_parent;
12635 if (refdt->die_dup && refdt->die_op_type_referenced
12636 && cu->cu_kind != CU_PU)
12637 {
12638 if (cu == die_cu (refdt->die_dup))
12639 refd = die_find_dup (refdt, refdt->die_dup, refd);
12640 }
12641 else if (refdt->die_dup)
12642 refd = die_find_dup (refdt, refdt->die_dup, refd);
12643 if (t->attr[j].form == DW_FORM_GNU_ref_alt
12644 || t->attr[j].form == DW_FORM_ref_sup4)
12645 {
12646 value = refd->die_offset;
12647 assert (die_cu (refd)->cu_kind == CU_ALT);
12648 }
12649 else
12650 {
12651 dw_cu_ref refdcu = die_cu (refd);
12652 value = refd->u.p2.die_new_offset;
12653 assert (IMPLIES (cu->cu_kind == CU_PU,
12654 die_cu (refd)->cu_kind == CU_PU));
12655 assert (value && refdcu->cu_kind != CU_ALT);
12656 if (t->attr[j].form == DW_FORM_ref_addr)
12657 {
12658 value += refdcu->cu_new_offset;
12659 if (unlikely (op_multifile))
12660 assert (refdcu->cu_kind == CU_PU);
12661 }
12662 else
12663 assert (refdcu == cu);
12664 }
12665 }
12666 switch (t->attr[j].form)
12667 {
12668 case DW_FORM_ref1: write_8 (ptr, value); break;
12669 case DW_FORM_ref2: write_16 (ptr, value); break;
12670 case DW_FORM_ref4: write_32 (ptr, value); break;
12671 case DW_FORM_ref_udata: write_uleb128 (ptr, value); break;
12672 case DW_FORM_ref_addr:
12673 write_size (ptr, cu->cu_version == 2 ? ptr_size : 4,
12674 value);
12675 ptr += cu->cu_version == 2 ? ptr_size : 4;
12676 break;
12677 case DW_FORM_GNU_ref_alt:
12678 case DW_FORM_ref_sup4:
12679 write_32 (ptr, value);
12680 break;
12681 case DW_FORM_ref_sup8:
12682 write_64 (ptr, value);
12683 break;
12684 default:
12685 abort ();
12686 }
12687 j++;
12688 continue;
12689 default:
12690 abort ();
12691 }
12692
12693 if (form == DW_FORM_block1 || form == DW_FORM_exprloc)
12694 inptr += len;
12695
12696 memcpy (ptr, orig_ptr, inptr - orig_ptr);
12697 ptr += inptr - orig_ptr;
12698
12699 /* Old DWARF uses blocks instead of exprlocs. */
12700 if (form == DW_FORM_block1 && cu->cu_version < 4)
12701 switch (reft->attr[i].attr)
12702 {
12703 case DW_AT_frame_base:
12704 case DW_AT_location:
12705 case DW_AT_data_member_location:
12706 case DW_AT_vtable_elem_location:
12707 case DW_AT_byte_size:
12708 case DW_AT_bit_offset:
12709 case DW_AT_bit_size:
12710 case DW_AT_string_length:
12711 case DW_AT_lower_bound:
12712 case DW_AT_return_addr:
12713 case DW_AT_bit_stride:
12714 case DW_AT_upper_bound:
12715 case DW_AT_count:
12716 case DW_AT_segment:
12717 case DW_AT_static_link:
12718 case DW_AT_use_location:
12719 case DW_AT_allocated:
12720 case DW_AT_associated:
12721 case DW_AT_data_location:
12722 case DW_AT_byte_stride:
12723 case DW_AT_rank:
12724 case DW_AT_call_value:
12725 case DW_AT_call_target:
12726 case DW_AT_call_target_clobbered:
12727 case DW_AT_call_data_location:
12728 case DW_AT_call_data_value:
12729 case DW_AT_GNU_call_site_value:
12730 case DW_AT_GNU_call_site_data_value:
12731 case DW_AT_GNU_call_site_target:
12732 case DW_AT_GNU_call_site_target_clobbered:
12733 adjust_exprloc (cu, die, refcu, ref, ptr - len, len);
12734 default:
12735 break;
12736 }
12737 else if (form == DW_FORM_exprloc)
12738 adjust_exprloc (cu, die, refcu, ref, ptr - len, len);
12739 j++;
12740 }
12741 assert (j == t->nattr);
12742 }
12743 else
12744 switch (die->die_tag)
12745 {
12746 case DW_TAG_partial_unit:
12747 case DW_TAG_compile_unit:
12748 ptr = write_unit_die (ptr, die, origin);
12749 break;
12750 case DW_TAG_namespace:
12751 case DW_TAG_module:
12752 {
12753 enum dwarf_form form;
12754 unsigned char *p = get_AT (origin, DW_AT_name, &form);
12755 assert (p && (form == t->attr[0].form
12756 || (form == DW_FORM_strp
12757 && (t->attr[0].form == DW_FORM_GNU_strp_alt
12758 || t->attr[0].form == DW_FORM_strp_sup))));
12759 if (form == DW_FORM_strp)
12760 {
12761 if (unlikely (wr_multifile || op_multifile || fi_multifile))
12762 {
12763 unsigned int strp = lookup_strp_offset (read_32 (p));
12764 write_32 (ptr, strp);
12765 }
12766 else
12767 {
12768 memcpy (ptr, p, 4);
12769 ptr += 4;
12770 }
12771 }
12772 else if (form == DW_FORM_line_strp)
12773 {
12774 memcpy (ptr, p, 4);
12775 ptr += 4;
12776 }
12777 else
12778 {
12779 size_t len = strlen ((char *) p) + 1;
12780 memcpy (ptr, p, len);
12781 ptr += len;
12782 }
12783 if (t->nattr > 1)
12784 {
12785 assert (sib);
12786 switch (t->attr[1].form)
12787 {
12788 case DW_FORM_ref1:
12789 write_8 (ptr, sib->u.p2.die_new_offset);
12790 break;
12791 case DW_FORM_ref2:
12792 write_16 (ptr, sib->u.p2.die_new_offset);
12793 break;
12794 case DW_FORM_ref4:
12795 write_32 (ptr, sib->u.p2.die_new_offset);
12796 break;
12797 case DW_FORM_ref_udata:
12798 write_uleb128 (ptr, sib->u.p2.die_new_offset);
12799 break;
12800 default:
12801 abort ();
12802 }
12803 }
12804 break;
12805 }
12806 case DW_TAG_imported_unit:
12807 refcu = die_cu (origin);
12808 if (t->attr[0].form == DW_FORM_GNU_ref_alt
12809 || t->attr[0].form == DW_FORM_ref_sup4)
12810 {
12811 assert (refcu->cu_kind == CU_ALT);
12812 write_32 (ptr, origin->die_offset);
12813 break;
12814 }
12815 assert (refcu->cu_kind != CU_ALT);
12816 write_size (ptr, cu->cu_version == 2 ? ptr_size : 4,
12817 refcu->cu_new_offset
12818 + origin->u.p2.die_new_offset);
12819 ptr += cu->cu_version == 2 ? ptr_size : 4;
12820 break;
12821 default:
12822 abort ();
12823 }
12824 if (ref != NULL && ref != die)
12825 {
12826 dw_die_ref ref_child;
12827 for (child = die->die_child, ref_child = ref->die_child;
12828 child; child = child->die_sib, ref_child = ref_child->die_sib)
12829 ptr = write_die (ptr, cu, child, refcu, ref_child, die_count);
12830 }
12831 else
12832 for (child = die->die_child; child; child = child->die_sib)
12833 ptr = write_die (ptr, cu, child, NULL, NULL, die_count);
12834 if (die->die_child)
12835 write_8 (ptr, 0);
12836 return ptr;
12837 }
12838
12839 /* Recompute abbrevs for CU. If any children were collapsed during
12840 compute_abbrevs, their ->u.p2.die_new_abbrev and ->u.p2.die_new_offset
12841 fields are no longer available and need to be computed again. */
12842 static void
recompute_abbrevs(dw_cu_ref cu,unsigned int cu_size)12843 recompute_abbrevs (dw_cu_ref cu, unsigned int cu_size)
12844 {
12845 unsigned int headersz = (cu->cu_kind == CU_TYPES
12846 ? 23 : (cu->cu_version >= 5 ? 12 : 11));
12847 struct abbrev_tag *t;
12848 unsigned int ndies = 0, intracusize, off, i;
12849 dw_die_ref *intracuarr, *intracuvec;
12850
12851 t = (struct abbrev_tag *)
12852 obstack_alloc (&ob2,
12853 sizeof (*t)
12854 + (max_nattr + 4) * sizeof (struct abbrev_attr)
12855 + (max_nattr + 4) * sizeof (int64_t));
12856 t->values = (int64_t *) &t->attr[max_nattr + 4];
12857
12858 build_abbrevs_for_die (cu->u1.cu_new_abbrev_owner
12859 ? cu->u1.cu_new_abbrev_owner->cu_new_abbrev
12860 : cu->cu_new_abbrev, cu, cu->cu_die, NULL, NULL, t,
12861 &ndies, &ob2, true);
12862
12863 obstack_ptr_grow (&ob2, NULL);
12864 intracuarr = (dw_die_ref *) obstack_finish (&ob2);
12865 if (cu->cu_intracu_form != DW_FORM_ref_udata)
12866 {
12867 switch (cu->cu_intracu_form)
12868 {
12869 case DW_FORM_ref1: intracusize = 1; break;
12870 case DW_FORM_ref2: intracusize = 2; break;
12871 case DW_FORM_ref4: intracusize = 4; break;
12872 default: abort ();
12873 }
12874 off = init_new_die_offsets (cu->cu_die, headersz, intracusize);
12875 }
12876 else
12877 {
12878 intracusize = cu->initial_intracusize;
12879
12880 off = init_new_die_offsets (cu->cu_die, headersz, intracusize);
12881 do
12882 {
12883 intracuvec = intracuarr;
12884 i = update_new_die_offsets (cu->cu_die, headersz, &intracuvec);
12885 assert (*intracuvec == NULL);
12886 if (i == off)
12887 break;
12888 assert (i < off);
12889 off = i;
12890 }
12891 while (1);
12892
12893 intracuvec = intracuarr;
12894 off = finalize_new_die_offsets (cu, cu->cu_die, headersz, 0,
12895 &intracuvec);
12896 assert (*intracuvec == NULL);
12897 }
12898 obstack_free (&ob2, (void *) t);
12899 assert (off == cu_size);
12900 }
12901
12902 /* Construct new .debug_info section in malloced memory,
12903 store it to debug_sections[DEBUG_INFO].new_data. */
12904 static void
write_info(unsigned int * die_count)12905 write_info (unsigned int *die_count)
12906 {
12907 dw_cu_ref cu, cu_next;
12908 unsigned char *info = malloc (debug_sections[DEBUG_INFO].new_size);
12909 unsigned char *ptr = info;
12910
12911 if (unlikely (progress_p))
12912 {
12913 report_progress ();
12914 fprintf (stderr, "write_info\n");
12915 }
12916
12917 if (info == NULL)
12918 dwz_oom ();
12919 if (die_count)
12920 *die_count = 0;
12921 debug_sections[DEBUG_INFO].new_data = info;
12922 cu = first_cu;
12923 if (unlikely (fi_multifile))
12924 while (cu
12925 && cu->cu_kind == CU_NORMAL
12926 && cu->cu_die->die_remove)
12927 cu = cu->cu_next;
12928 for (; cu; cu = cu_next)
12929 {
12930 unsigned long next_off = debug_sections[DEBUG_INFO].new_size;
12931 /* Ignore .debug_types CUs. */
12932 if (cu->cu_kind == CU_TYPES)
12933 break;
12934 cu_next = cu->cu_next;
12935 if (unlikely (fi_multifile))
12936 while (cu_next
12937 && cu_next->cu_kind == CU_NORMAL
12938 && cu_next->cu_die->die_remove)
12939 cu_next = cu_next->cu_next;
12940 if (cu_next && cu_next->cu_kind == CU_TYPES)
12941 cu_next = NULL;
12942 if (cu_next)
12943 next_off = cu_next->cu_new_offset;
12944 else if (wr_multifile)
12945 next_off += multi_info_off - 11L;
12946 if (unlikely (low_mem)
12947 && cu->cu_kind != CU_PU
12948 && expand_children (cu->cu_die))
12949 recompute_abbrevs (cu, next_off - cu->cu_new_offset);
12950 /* Write CU header. */
12951 write_32 (ptr, next_off - cu->cu_new_offset - 4);
12952 write_16 (ptr, cu->cu_version);
12953 if (cu->cu_version >= 5)
12954 {
12955 *ptr++ = (cu->cu_die->die_tag == DW_TAG_compile_unit
12956 ? DW_UT_compile : DW_UT_partial);
12957 write_8 (ptr, ptr_size);
12958 }
12959 write_32 (ptr, cu->u2.cu_new_abbrev_offset);
12960 if (cu->cu_version < 5)
12961 write_8 (ptr, ptr_size);
12962 ptr = write_die (ptr, cu, cu->cu_die, NULL, NULL, die_count);
12963 assert (info + (next_off - (wr_multifile ? multi_info_off : 0)) == ptr);
12964 if (unlikely (low_mem) && cu->cu_kind != CU_PU)
12965 collapse_children (cu, cu->cu_die);
12966 }
12967 if (wr_multifile)
12968 {
12969 /* And terminate the contribution by the current object file. */
12970 write_32 (ptr, 7);
12971 write_16 (ptr, 2);
12972 write_32 (ptr, 0);
12973 write_8 (ptr, ptr_size);
12974 }
12975 assert (info + debug_sections[DEBUG_INFO].new_size == ptr);
12976 }
12977
12978 /* Adjust .debug_loc range determined by *SLOT, called through
12979 htab_traverse. */
12980 static int
adjust_loclist(void ** slot,void * data)12981 adjust_loclist (void **slot, void *data)
12982 {
12983 struct debug_loc_adjust *adj = (struct debug_loc_adjust *) *slot;
12984 unsigned char *ptr, *endsec;
12985 GElf_Addr low, high;
12986 size_t len;
12987
12988 (void)data;
12989
12990 ptr = debug_sections[DEBUG_LOC].new_data + adj->start_offset;
12991 endsec = ptr + debug_sections[DEBUG_LOC].size;
12992 while (ptr < endsec)
12993 {
12994 low = read_size (ptr, ptr_size);
12995 high = read_size (ptr + ptr_size, ptr_size);
12996 ptr += 2 * ptr_size;
12997 if (low == 0 && high == 0)
12998 break;
12999
13000 if (low == ~ (GElf_Addr) 0 || (ptr_size == 4 && low == 0xffffffff))
13001 continue;
13002
13003 len = read_16 (ptr);
13004 assert (ptr + len <= endsec);
13005
13006 adjust_exprloc (adj->cu, adj->cu->cu_die, adj->cu, adj->cu->cu_die,
13007 ptr, len);
13008
13009 ptr += len;
13010 }
13011
13012 return 1;
13013 }
13014
13015 /* Adjust .debug_loclists range determined by *SLOT, called through
13016 htab_traverse. */
13017 static int
adjust_loclists(void ** slot,void * data)13018 adjust_loclists (void **slot, void *data)
13019 {
13020 struct debug_loc_adjust *adj = (struct debug_loc_adjust *) *slot;
13021 unsigned char *ptr, *endsec;
13022 size_t len = 0;
13023
13024 (void)data;
13025
13026 ptr = debug_sections[DEBUG_LOCLISTS].new_data + adj->start_offset;
13027 endsec = ptr + debug_sections[DEBUG_LOCLISTS].size;
13028
13029 again:
13030 while (ptr < endsec)
13031 {
13032 uint8_t lle = *ptr++;
13033 switch (lle)
13034 {
13035 case DW_LLE_end_of_list:
13036 goto done;
13037
13038 case DW_LLE_base_addressx:
13039 skip_leb128 (ptr);
13040 goto again;
13041
13042 case DW_LLE_startx_endx:
13043 skip_leb128 (ptr);
13044 skip_leb128 (ptr);
13045 len = read_uleb128 (ptr);
13046 break;
13047
13048 case DW_LLE_startx_length:
13049 skip_leb128 (ptr);
13050 skip_leb128 (ptr);
13051 len = read_uleb128 (ptr);
13052 break;
13053
13054 case DW_LLE_offset_pair:
13055 skip_leb128 (ptr);
13056 skip_leb128 (ptr);
13057 len = read_uleb128 (ptr);
13058 break;
13059
13060 case DW_LLE_default_location:
13061 len = read_uleb128 (ptr);
13062 break;
13063
13064 case DW_LLE_base_address:
13065 ptr += ptr_size;
13066 goto again;
13067
13068 case DW_LLE_start_end:
13069 ptr += 2 * ptr_size;
13070 len = read_uleb128 (ptr);
13071 break;
13072
13073 case DW_LLE_start_length:
13074 ptr += ptr_size;
13075 skip_leb128 (ptr);
13076 len = read_uleb128 (ptr);
13077 break;
13078
13079 case DW_LLE_GNU_view_pair:
13080 /* Note we cannot check CU version here, but we'll get a
13081 warning on the original parsing if CU version is not 5.*/
13082 skip_leb128 (ptr);
13083 skip_leb128 (ptr);
13084 goto again;
13085
13086 default:
13087 error (0, 0, "unhandled location list entry 0x%x", lle);
13088 return 1;
13089 }
13090
13091 assert (ptr + len <= endsec);
13092
13093 adjust_exprloc (adj->cu, adj->cu->cu_die, adj->cu, adj->cu->cu_die,
13094 ptr, len);
13095
13096 ptr += len;
13097 }
13098
13099 done:
13100 return 1;
13101 }
13102
13103 /* Create new .debug_loc section in malloced memory if .debug_loc
13104 needs to be adjusted. */
13105 static void
write_loc(void)13106 write_loc (void)
13107 {
13108 unsigned char *loc;
13109 if (loc_htab == NULL)
13110 return;
13111 loc = malloc (debug_sections[DEBUG_LOC].size);
13112 if (loc == NULL)
13113 dwz_oom ();
13114 memcpy (loc, debug_sections[DEBUG_LOC].data, debug_sections[DEBUG_LOC].size);
13115 debug_sections[DEBUG_LOC].new_data = loc;
13116 htab_traverse (loc_htab, adjust_loclist, NULL);
13117 }
13118
13119 /* Create new .debug_loclists section in malloced memory if .debug_loclists
13120 needs to be adjusted. */
13121 static void
write_loclists(void)13122 write_loclists (void)
13123 {
13124 unsigned char *loc;
13125 if (loclists_htab == NULL)
13126 return;
13127 loc = malloc (debug_sections[DEBUG_LOCLISTS].size);
13128 if (loc == NULL)
13129 dwz_oom ();
13130 memcpy (loc, debug_sections[DEBUG_LOCLISTS].data,
13131 debug_sections[DEBUG_LOCLISTS].size);
13132 debug_sections[DEBUG_LOCLISTS].new_data = loc;
13133 htab_traverse (loclists_htab, adjust_loclists, NULL);
13134 }
13135
13136 /* Create new .debug_types section in malloced memory. */
13137 static void
write_types(void)13138 write_types (void)
13139 {
13140 dw_cu_ref cu;
13141 unsigned char *types, *ptr, *inptr;
13142 dw_die_ref ref;
13143
13144 if (debug_sections[DEBUG_TYPES].data == NULL)
13145 return;
13146 types = malloc (debug_sections[DEBUG_TYPES].new_size);
13147 if (types == NULL)
13148 dwz_oom ();
13149 debug_sections[DEBUG_TYPES].new_data = types;
13150 ptr = types;
13151 for (cu = first_cu; cu; cu = cu->cu_next)
13152 {
13153 unsigned long next_off = debug_sections[DEBUG_TYPES].new_size;
13154 /* Ignore .debug_info CUs. */
13155 if (cu->cu_kind != CU_TYPES)
13156 continue;
13157 if (cu->cu_next)
13158 next_off = cu->cu_next->cu_new_offset;
13159 if (unlikely (low_mem)
13160 && expand_children (cu->cu_die))
13161 recompute_abbrevs (cu, next_off - cu->cu_new_offset);
13162 /* Write CU header. */
13163 write_32 (ptr, next_off - cu->cu_new_offset - 4);
13164 write_16 (ptr, cu->cu_version);
13165 write_32 (ptr, cu->u2.cu_new_abbrev_offset);
13166 write_8 (ptr, ptr_size);
13167 inptr = debug_sections[DEBUG_TYPES].data + cu->cu_offset + 19;
13168 memcpy (ptr, inptr - 8, 8);
13169 ptr += 8;
13170 ref = off_htab_lookup (cu, cu->cu_offset + read_32 (inptr));
13171 assert (ref && die_safe_dup(ref) == NULL);
13172 write_32 (ptr, ref->u.p2.die_new_offset);
13173 ptr = write_die (ptr, cu, cu->cu_die, NULL, NULL, NULL);
13174 assert (types + next_off == ptr);
13175 if (unlikely (low_mem))
13176 collapse_children (cu, cu->cu_die);
13177 }
13178 assert (types + debug_sections[DEBUG_TYPES].new_size == ptr);
13179 }
13180
13181 /* Construct new .debug_aranges section in malloced memory,
13182 store it to debug_sections[DEBUG_ARANGES].new_data. */
13183 static int
write_aranges(DSO * dso)13184 write_aranges (DSO *dso)
13185 {
13186 dw_cu_ref cu, cufirst = NULL, cucur;
13187 unsigned char *aranges, *ptr, *end;
13188
13189 if (debug_sections[DEBUG_ARANGES].data == NULL)
13190 return 0;
13191
13192 aranges = malloc (debug_sections[DEBUG_ARANGES].size);
13193 if (aranges == NULL)
13194 dwz_oom ();
13195 memcpy (aranges, debug_sections[DEBUG_ARANGES].data,
13196 debug_sections[DEBUG_ARANGES].size);
13197 debug_sections[DEBUG_ARANGES].new_data = aranges;
13198 ptr = aranges;
13199 end = aranges + debug_sections[DEBUG_ARANGES].size;
13200 for (cu = first_cu; cu; cu = cu->cu_next)
13201 if (cu->cu_kind != CU_PU)
13202 break;
13203 cufirst = cu;
13204 while (ptr < end)
13205 {
13206 unsigned int culen, value, cuoff;
13207
13208 if (end - ptr < 12)
13209 {
13210 error (0, 0, "%s: Corrupted .debug_aranges section",
13211 dso->filename);
13212 return 1;
13213 }
13214 culen = read_32 (ptr);
13215 if (culen >= 0xfffffff0)
13216 {
13217 error (0, 0, "%s: 64-bit DWARF not supported", dso->filename);
13218 return 1;
13219 }
13220
13221 value = read_16 (ptr);
13222 if (value != 2)
13223 {
13224 error (0, 0, "%s: DWARF version %d in .debug_aranges unhandled",
13225 dso->filename, value);
13226 return 1;
13227 }
13228
13229 cuoff = read_32 (ptr);
13230 cucur = cu;
13231 /* Optimistically assume that .debug_aranges CU offsets only increase,
13232 otherwise this might be too expensive and need a hash table. */
13233 for (; cu; cu = cu->cu_next)
13234 {
13235 if (cu->cu_kind == CU_TYPES)
13236 {
13237 cu = NULL;
13238 break;
13239 }
13240 else if (cu->cu_offset == cuoff)
13241 break;
13242 }
13243 if (cu == NULL)
13244 {
13245 for (cu = cufirst; cu != cucur; cu = cu->cu_next)
13246 if (cu->cu_offset == cuoff)
13247 break;
13248 if (cu == cucur)
13249 {
13250 error (0, 0, "%s: Couldn't find CU for .debug_aranges "
13251 "offset 0x%x", dso->filename, cuoff);
13252 return 1;
13253 }
13254 }
13255 if (unlikely (fi_multifile)
13256 && cu->cu_kind == CU_NORMAL
13257 && cu->cu_die->die_remove)
13258 {
13259 error (0, 0, "%s: Partial unit referenced in .debug_aranges",
13260 dso->filename);
13261 return 1;
13262 }
13263 ptr -= 4;
13264 write_32 (ptr, cu->cu_new_offset);
13265 ptr += culen - 6;
13266 }
13267 return 0;
13268 }
13269
13270 /* Helper function of write_gdb_index, called through qsort.
13271 Sort an array of unsigned integer pairs, by increasing
13272 first integer. The first integer is the TU offset
13273 in the .gdb_index TU table, the second is its index in
13274 the TU table from the start of that table. */
13275 static int
gdb_index_tu_cmp(const void * p,const void * q)13276 gdb_index_tu_cmp (const void *p, const void *q)
13277 {
13278 unsigned int *t1 = (unsigned int *) p;
13279 unsigned int *t2 = (unsigned int *) q;
13280
13281 if (t1[0] < t2[0])
13282 return -1;
13283 if (t1[0] > t2[0])
13284 return 1;
13285
13286 if (t1[1] < t2[1])
13287 return -1;
13288 if (t1[1] > t2[1])
13289 return 1;
13290 return 0;
13291 }
13292
13293 /* Construct new .gdb_index section in malloced memory
13294 if it needs adjustment. */
13295 static void
write_gdb_index(void)13296 write_gdb_index (void)
13297 {
13298 dw_cu_ref cu, cu_next, first_tu = NULL;
13299 unsigned char *gdb_index, *ptr, *inptr, *end;
13300 unsigned int ncus = 0, npus = 0, ntus = 0, ndelcus = 0, ver;
13301 unsigned int culistoff, cutypesoff, addressoff, symboloff, constoff;
13302 unsigned int *tuindices = NULL, tuidx = 0, *cumap = NULL, i, j, k;
13303 bool fail = false;
13304
13305 debug_sections[GDB_INDEX].new_size = 0;
13306 if (likely (!op_multifile)
13307 && (debug_sections[GDB_INDEX].data == NULL
13308 || debug_sections[GDB_INDEX].size < 0x18))
13309 return;
13310 inptr = (unsigned char *) debug_sections[GDB_INDEX].data;
13311 if (unlikely (op_multifile))
13312 ver = multi_gdb_index_ver;
13313 else
13314 ver = buf_read_ule32 (inptr);
13315 if (ver < 4 || ver > 8)
13316 return;
13317
13318 for (cu = first_cu; cu; cu = cu->cu_next)
13319 if (cu->cu_kind == CU_PU)
13320 npus++;
13321 else if (cu->cu_kind == CU_NORMAL)
13322 {
13323 ncus++;
13324 if (unlikely (fi_multifile) && cu->cu_die->die_remove)
13325 ndelcus++;
13326 }
13327 else if (cu->cu_kind == CU_TYPES)
13328 ntus++;
13329
13330 /* Starting with version 7 CU indexes are limited to 24 bits,
13331 so if we have more CUs, give up. */
13332 if (npus + ncus + ntus - ndelcus >= (1U << 24))
13333 return;
13334
13335 if (unlikely (op_multifile))
13336 {
13337 assert (ncus == 0 && ntus == 0);
13338 debug_sections[GDB_INDEX].new_size
13339 = 0x18 + npus * 16 + 16;
13340 gdb_index = malloc (debug_sections[GDB_INDEX].new_size);
13341 if (gdb_index == NULL)
13342 dwz_oom ();
13343 debug_sections[GDB_INDEX].new_data = gdb_index;
13344 /* Write new header. */
13345 buf_write_le32 (gdb_index + 0x00, ver);
13346 buf_write_le32 (gdb_index + 0x04, 0x18);
13347 buf_write_le32 (gdb_index + 0x08, 0x18 + npus * 16);
13348 buf_write_le32 (gdb_index + 0x0c, 0x18 + npus * 16);
13349 buf_write_le32 (gdb_index + 0x10, 0x18 + npus * 16);
13350 buf_write_le32 (gdb_index + 0x14, 0x18 + npus * 16 + 16);
13351 ptr = gdb_index + 0x18;
13352 /* Write new CU list. */
13353 for (cu = first_cu; cu; cu = cu->cu_next)
13354 {
13355 unsigned long next_off = debug_sections[DEBUG_INFO].new_size;
13356 if (cu->cu_next)
13357 next_off = cu->cu_next->cu_new_offset;
13358 buf_write_le64 (ptr, cu->cu_new_offset);
13359 buf_write_le64 (ptr + 8, next_off - cu->cu_new_offset);
13360 ptr += 16;
13361 }
13362 /* Write an empty hash table (with two entries). */
13363 memset (ptr, '\0', 16);
13364 return;
13365 }
13366
13367 culistoff = buf_read_ule32 (inptr + 0x04);
13368 cutypesoff = buf_read_ule32 (inptr + 0x08);
13369 addressoff = buf_read_ule32 (inptr + 0x0c);
13370 symboloff = buf_read_ule32 (inptr + 0x10);
13371 constoff = buf_read_ule32 (inptr + 0x14);
13372 if (culistoff != 0x18
13373 || cutypesoff != 0x18 + ncus * 16
13374 || addressoff != cutypesoff + ntus * 24
13375 || symboloff < addressoff
13376 || ((symboloff - addressoff) % 20) != 0
13377 || constoff < symboloff
13378 || ((constoff - symboloff) & (constoff - symboloff - 1)) != 0
13379 || ((constoff - symboloff) & 7) != 0
13380 || debug_sections[GDB_INDEX].size < constoff)
13381 return;
13382 inptr += 0x18;
13383 if (ndelcus)
13384 cumap = (unsigned int *)
13385 obstack_alloc (&ob2, ncus * sizeof (unsigned int));
13386 for (cu = first_cu, i = 0, j = 0; cu; cu = cu->cu_next)
13387 if (cu->cu_kind == CU_NORMAL)
13388 {
13389 if (buf_read_ule64 (inptr) != cu->cu_offset)
13390 {
13391 if (cumap)
13392 obstack_free (&ob2, (void *) cumap);
13393 return;
13394 }
13395 inptr += 16;
13396 if (cumap)
13397 {
13398 if (cu->cu_die->die_remove)
13399 cumap[i++] = -1U;
13400 else
13401 cumap[i++] = j++;
13402 }
13403 }
13404 else if (cu->cu_kind == CU_TYPES)
13405 {
13406 if (tuindices == NULL)
13407 {
13408 tuindices = (unsigned int *)
13409 obstack_alloc (&ob2, ntus * 2 * sizeof (unsigned int));
13410 first_tu = cu;
13411 }
13412 tuindices[2 * tuidx] = buf_read_ule64 (inptr);
13413 tuindices[2 * tuidx + 1] = tuidx * 24;
13414 tuidx++;
13415 inptr += 24;
13416 }
13417 if (ntus)
13418 {
13419 qsort (tuindices, ntus, 2 * sizeof (unsigned int), gdb_index_tu_cmp);
13420 for (tuidx = 0, cu = first_tu; tuidx < ntus; tuidx++, cu = cu->cu_next)
13421 if (tuindices[2 * tuidx] != cu->cu_offset)
13422 {
13423 if (cumap)
13424 obstack_free (&ob2, (void *) cumap);
13425 else
13426 obstack_free (&ob2, (void *) tuindices);
13427 return;
13428 }
13429 }
13430
13431 if (multifile
13432 && !fi_multifile
13433 && !low_mem
13434 && multi_gdb_index_ver < ver)
13435 multi_gdb_index_ver = ver;
13436
13437 debug_sections[GDB_INDEX].new_size
13438 = debug_sections[GDB_INDEX].size + npus * 16 - ndelcus * 16;
13439 gdb_index = malloc (debug_sections[GDB_INDEX].new_size);
13440 if (gdb_index == NULL)
13441 dwz_oom ();
13442 debug_sections[GDB_INDEX].new_data = gdb_index;
13443 /* Write new header. */
13444 buf_write_le32 (gdb_index + 0x00, ver);
13445 buf_write_le32 (gdb_index + 0x04, culistoff);
13446 buf_write_le32 (gdb_index + 0x08, cutypesoff + npus * 16 - ndelcus * 16);
13447 buf_write_le32 (gdb_index + 0x0c, addressoff + npus * 16 - ndelcus * 16);
13448 buf_write_le32 (gdb_index + 0x10, symboloff + npus * 16 - ndelcus * 16);
13449 buf_write_le32 (gdb_index + 0x14, constoff + npus * 16 - ndelcus * 16);
13450 ptr = gdb_index + 0x18;
13451 /* Write new CU list. */
13452 for (cu = first_cu; cu; cu = cu_next)
13453 {
13454 unsigned long next_off = debug_sections[DEBUG_INFO].new_size;
13455 if (cu->cu_kind == CU_TYPES)
13456 break;
13457 cu_next = cu->cu_next;
13458 if (unlikely (fi_multifile))
13459 {
13460 while (cu_next
13461 && cu_next->cu_kind == CU_NORMAL
13462 && cu_next->cu_die->die_remove)
13463 cu_next = cu_next->cu_next;
13464 if (cu->cu_die->die_remove)
13465 continue;
13466 }
13467 if (cu_next && cu_next->cu_kind != CU_TYPES)
13468 next_off = cu_next->cu_new_offset;
13469 buf_write_le64 (ptr, cu->cu_new_offset);
13470 buf_write_le64 (ptr + 8, next_off - cu->cu_new_offset);
13471 ptr += 16;
13472 }
13473 /* Write new TU list. */
13474 for (tuidx = 0; cu; cu = cu->cu_next, tuidx++)
13475 {
13476 unsigned char *p;
13477 unsigned int tuoff = tuindices[2 * tuidx + 1];
13478 dw_die_ref ref;
13479 assert (cu->cu_kind == CU_TYPES);
13480 buf_write_le64 (ptr + tuoff, cu->cu_new_offset);
13481 p = debug_sections[DEBUG_TYPES].data + cu->cu_offset + 19;
13482 ref = off_htab_lookup (cu, cu->cu_offset + read_32 (p));
13483 assert (ref && ref->die_dup == NULL);
13484 buf_write_le64 (ptr + tuoff + 8, ref->u.p2.die_new_offset);
13485 p -= 12;
13486 buf_write_le64 (ptr + tuoff + 16, read_64 (p));
13487 }
13488 ptr += ntus * 24;
13489 end = inptr + (symboloff - addressoff);
13490 /* Copy address area, adjusting all CU indexes. */
13491 while (inptr < end)
13492 {
13493 memcpy (ptr, inptr, 16);
13494 i = buf_read_ule32 (inptr + 16);
13495 if (cumap && i < ncus)
13496 {
13497 if (cumap[i] == -1U)
13498 fail = true;
13499 i = cumap[i] + npus;
13500 }
13501 else
13502 i += npus - ndelcus;
13503 buf_write_le32 (ptr + 16, i);
13504 ptr += 20;
13505 inptr += 20;
13506 }
13507 /* Copy the symbol hash table. */
13508 memcpy (ptr, inptr, constoff - symboloff);
13509 /* Clear the const pool initially. */
13510 memset (ptr + (constoff - symboloff), '\0',
13511 debug_sections[GDB_INDEX].size - constoff);
13512 ptr = ptr + (constoff - symboloff);
13513 end = inptr + (constoff - symboloff);
13514 /* Finally copy over const objects into the const pool, strings as is,
13515 CU vectors with CU indexes adjusted. */
13516 while (inptr < end)
13517 {
13518 unsigned int name = buf_read_ule32 (inptr);
13519 unsigned int cuvec = buf_read_ule32 (inptr + 4);
13520
13521 inptr += 8;
13522 if (name == 0 && cuvec == 0)
13523 continue;
13524 if (name > debug_sections[GDB_INDEX].size - constoff - 1
13525 || cuvec > debug_sections[GDB_INDEX].size - constoff - 4)
13526 {
13527 fail:
13528 free (gdb_index);
13529 debug_sections[GDB_INDEX].new_size = 0;
13530 return;
13531 }
13532 if (ptr[name] == '\0')
13533 {
13534 unsigned char *strend = end + name;
13535 while (*strend != '\0')
13536 {
13537 if (strend + 1
13538 == end + (debug_sections[GDB_INDEX].size - constoff))
13539 goto fail;
13540 strend++;
13541 }
13542 memcpy (ptr + name, end + name, strend + 1 - (end + name));
13543 }
13544 if (buf_read_ule32 (ptr + cuvec) == 0)
13545 {
13546 unsigned int count = buf_read_ule32 (end + cuvec);
13547 if (count * 4
13548 > debug_sections[GDB_INDEX].size - constoff - cuvec - 4)
13549 goto fail;
13550 buf_write_le32 (ptr + cuvec, count);
13551 for (i = 0; i < count; i++)
13552 {
13553 j = buf_read_ule32 (end + cuvec + (i + 1) * 4);
13554 if (ver >= 7)
13555 k = j & ((1U << 24) - 1);
13556 else
13557 k = j;
13558 if (cumap && k < ncus)
13559 {
13560 if (cumap[k] == -1U)
13561 fail = true;
13562 k = cumap[k] + npus;
13563 }
13564 else
13565 k += npus - ndelcus;
13566 if (ver >= 7)
13567 j = (j & (~0U << 24)) | k;
13568 else
13569 j = k;
13570 buf_write_le32 (ptr + cuvec + (i + 1) * 4, j);
13571 }
13572 }
13573 }
13574 if (cumap)
13575 obstack_free (&ob2, (void *) cumap);
13576 else if (tuindices)
13577 obstack_free (&ob2, (void *) tuindices);
13578 if (fail)
13579 {
13580 free (debug_sections[GDB_INDEX].new_data);
13581 debug_sections[GDB_INDEX].new_data = NULL;
13582 debug_sections[GDB_INDEX].new_size = 0;
13583 }
13584 }
13585
13586 /* Return a string from section SEC at offset OFFSET. */
13587 static const char *
strptr(DSO * dso,int sec,off_t offset)13588 strptr (DSO *dso, int sec, off_t offset)
13589 {
13590 Elf_Scn *scn;
13591 Elf_Data *data;
13592
13593 scn = dso->scn[sec];
13594 if (offset >= 0 && (GElf_Addr) offset < dso->shdr[sec].sh_size)
13595 {
13596 data = NULL;
13597 while ((data = elf_rawdata (scn, data)) != NULL)
13598 {
13599 if (data->d_buf
13600 && offset >= data->d_off
13601 && offset < (off_t) (data->d_off + data->d_size))
13602 return (const char *) data->d_buf + (offset - data->d_off);
13603 }
13604 }
13605
13606 return NULL;
13607 }
13608
13609 /* Initialize do_read_* and do_write_* callbacks based on
13610 ENDIANITY. */
13611 static void
init_endian(int endianity)13612 init_endian (int endianity)
13613 {
13614 if (endianity == ELFDATA2LSB)
13615 {
13616 do_read_16 = buf_read_ule16;
13617 do_read_32 = buf_read_ule32;
13618 do_read_64 = buf_read_ule64;
13619 do_write_16 = buf_write_le16;
13620 do_write_32 = buf_write_le32;
13621 do_write_64 = buf_write_le64;
13622 }
13623 else if (endianity == ELFDATA2MSB)
13624 {
13625 do_read_16 = buf_read_ube16;
13626 do_read_32 = buf_read_ube32;
13627 do_read_64 = buf_read_ube64;
13628 do_write_16 = buf_write_be16;
13629 do_write_32 = buf_write_be32;
13630 do_write_64 = buf_write_be64;
13631 }
13632 else
13633 abort ();
13634 }
13635
13636 /* Read DWARF sections from DSO. */
13637 static int
read_dwarf(DSO * dso,bool quieter,unsigned int * die_count)13638 read_dwarf (DSO *dso, bool quieter, unsigned int *die_count)
13639 {
13640 Elf_Data *data;
13641 Elf_Scn *scn;
13642 int i, j;
13643
13644 for (i = 1; i < dso->ehdr.e_shnum; ++i)
13645 if (! (dso->shdr[i].sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR))
13646 && dso->shdr[i].sh_size)
13647 {
13648 const char *name = strptr (dso, dso->ehdr.e_shstrndx,
13649 dso->shdr[i].sh_name);
13650
13651 if (strncmp (name, ".debug_", sizeof (".debug_") - 1) == 0
13652 || strcmp (name, ".gdb_index") == 0
13653 || strcmp (name, ".gnu_debugaltlink") == 0)
13654 {
13655 if (dso->shdr[i].sh_flags & SHF_COMPRESSED)
13656 {
13657 error (0, 0,
13658 "%s: Found compressed %s section, not attempting dwz"
13659 " compression",
13660 dso->filename, name);
13661 return 1;
13662 }
13663 for (j = 0; debug_sections[j].name; ++j)
13664 if (strcmp (name, debug_sections[j].name) == 0)
13665 {
13666 if (debug_sections[j].data)
13667 {
13668 error (0, 0, "%s: Found two copies of %s section",
13669 dso->filename, name);
13670 return 1;
13671 }
13672
13673 scn = dso->scn[i];
13674 data = elf_rawdata (scn, NULL);
13675 assert (data != NULL);
13676 if (data->d_buf == NULL)
13677 {
13678 error (0, 0, "%s: Found empty %s section, not attempting"
13679 " dwz compression", dso->filename, name);
13680 return 1;
13681 }
13682 assert (elf_rawdata (scn, data) == NULL);
13683 assert (data->d_off == 0);
13684 assert (data->d_size == dso->shdr[i].sh_size);
13685 debug_sections[j].data = data->d_buf;
13686 debug_sections[j].size = data->d_size;
13687 debug_sections[j].new_size = data->d_size;
13688 debug_sections[j].sec = i;
13689 break;
13690 }
13691
13692 if (debug_sections[j].name == NULL)
13693 {
13694 error (0, 0, "%s: Unknown debugging section %s",
13695 dso->filename, name);
13696 return 1;
13697 }
13698 }
13699 }
13700
13701 if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB
13702 || dso->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
13703 init_endian (dso->ehdr.e_ident[EI_DATA]);
13704 else
13705 {
13706 error (0, 0, "%s: Wrong ELF data encoding", dso->filename);
13707 return 1;
13708 }
13709
13710 if (debug_sections[DEBUG_INFO].data == NULL
13711 && !rd_multifile)
13712 {
13713 if (!quieter)
13714 error (0, 0, "%s: .debug_info section not present",
13715 dso->filename);
13716 return 3;
13717 }
13718
13719 if (debug_sections[DEBUG_SUP].data != NULL && !(dwarf_5 && rd_multifile))
13720 {
13721 error (0, 0, "%s: .debug_sup section already present",
13722 dso->filename);
13723 return 1;
13724 }
13725
13726 if (debug_sections[GNU_DEBUGALTLINK].data != NULL)
13727 {
13728 error (0, 0, "%s: .gnu_debugaltlink section already present",
13729 dso->filename);
13730 return 1;
13731 }
13732
13733 int res;
13734 res = read_debug_info (dso, DEBUG_INFO, die_count);
13735 if (res == 0 && stats_p)
13736 print_parse_stats ();
13737 return res;
13738 }
13739
13740 /* Open an ELF file NAME. */
13741 static DSO *
fdopen_dso(int fd,const char * name)13742 fdopen_dso (int fd, const char *name)
13743 {
13744 Elf *elf = NULL;
13745 GElf_Ehdr ehdr;
13746 int i;
13747 DSO *dso = NULL;
13748
13749 elf = elf_begin (fd, ELF_C_READ, NULL);
13750 if (elf == NULL)
13751 {
13752 error (0, 0, "cannot open ELF file: %s", elf_errmsg (-1));
13753 goto error_out;
13754 }
13755
13756 if (elf_kind (elf) != ELF_K_ELF)
13757 {
13758 error (0, 0, "\"%s\" is not an ELF file", name);
13759 goto error_out;
13760 }
13761
13762 if (gelf_getehdr (elf, &ehdr) == NULL)
13763 {
13764 error (0, 0, "cannot get the ELF header: %s",
13765 elf_errmsg (-1));
13766 goto error_out;
13767 }
13768
13769 if (ehdr.e_type != ET_DYN && ehdr.e_type != ET_EXEC
13770 && (!rd_multifile || ehdr.e_type != ET_REL))
13771 {
13772 error (0, 0, "\"%s\" is not a shared library", name);
13773 goto error_out;
13774 }
13775
13776 /* Allocate DSO structure. */
13777 dso = (DSO *)
13778 malloc (sizeof(DSO) + ehdr.e_shnum * sizeof(GElf_Shdr)
13779 + ehdr.e_shnum * sizeof(Elf_Scn *)
13780 + strlen (name) + 1);
13781 if (!dso)
13782 {
13783 error (0, ENOMEM, "Could not open DSO");
13784 goto error_out;
13785 }
13786
13787 elf_flagelf (elf, ELF_C_SET, ELF_F_LAYOUT);
13788
13789 memset (dso, 0, sizeof(DSO));
13790 dso->elf = elf;
13791 dso->ehdr = ehdr;
13792 dso->scn = (Elf_Scn **) &dso->shdr[ehdr.e_shnum];
13793
13794 for (i = 0; i < ehdr.e_shnum; ++i)
13795 {
13796 dso->scn[i] = elf_getscn (elf, i);
13797 gelf_getshdr (dso->scn[i], dso->shdr + i);
13798 }
13799
13800 dso->filename = (const char *) &dso->scn[ehdr.e_shnum];
13801 strcpy ((char *) &dso->scn[ehdr.e_shnum], name);
13802 return dso;
13803
13804 error_out:
13805 free (dso);
13806 if (elf)
13807 elf_end (elf);
13808 if (fd != -1)
13809 close (fd);
13810 return NULL;
13811 }
13812
13813 /* Implicit arg for compare_section_numbers. Could be passed in as explicit arg
13814 when using qsort_r instead. */
13815 static DSO *compare_section_numbers_implicit_arg;
13816
13817 /* Helper function for sort_section_numbers. */
13818 static int
compare_section_numbers(const void * p1,const void * p2)13819 compare_section_numbers (const void *p1, const void *p2)
13820 {
13821 DSO *dso = compare_section_numbers_implicit_arg;
13822 const int i1 = *(const int *)p1;
13823 const int i2 = *(const int *)p2;
13824 GElf_Off o1;
13825 GElf_Off o2;
13826
13827 /* Keep element 0 at 0. */
13828 if (i1 == 0 || i2 == 0)
13829 {
13830 if (i1 == i2)
13831 return 0;
13832 if (i1 == 0)
13833 return -1;
13834 if (i2 == 0)
13835 return 1;
13836 }
13837
13838 /* Get file offsets. */
13839 o1 = (i1 == dso->ehdr.e_shnum
13840 ? dso->ehdr.e_shoff
13841 : dso->shdr[i1].sh_offset);
13842 o2 = (i2 == dso->ehdr.e_shnum
13843 ? dso->ehdr.e_shoff
13844 : dso->shdr[i2].sh_offset);
13845
13846 /* Compare file offsets. */
13847 if (o1 < o2)
13848 return -1;
13849 if (o1 > o2)
13850 return 1;
13851
13852 /* In case file offset is the same, keep the original relative order. */
13853 if (i1 < i2)
13854 return -1;
13855 if (i1 > i2)
13856 return 1;
13857
13858 return 0;
13859 }
13860
13861 /* Sort SORTED_SECTION_NUMBERS in file offset order. */
13862 static void
sort_section_numbers(DSO * dso,unsigned int * sorted_section_numbers)13863 sort_section_numbers (DSO *dso, unsigned int *sorted_section_numbers)
13864 {
13865 unsigned int i;
13866 unsigned int nr_sections = dso->ehdr.e_shnum;
13867
13868 /* Treat section header table as another section, with index
13869 dso->ehdr.e_shnum. */
13870 nr_sections += 1;
13871
13872 for (i = 0; i < nr_sections; ++i)
13873 sorted_section_numbers[i] = i;
13874
13875 compare_section_numbers_implicit_arg = dso;
13876 qsort (sorted_section_numbers, nr_sections,
13877 sizeof (sorted_section_numbers[0]), compare_section_numbers);
13878 compare_section_numbers_implicit_arg = NULL;
13879
13880 assert (sorted_section_numbers[0] == 0);
13881 }
13882
13883 /* Verify file offset and size of sections and section header table. */
13884 static void
verify_sections(DSO * dso,unsigned int * sorted_section_numbers,GElf_Off * distance,int addsec,GElf_Off addsize,GElf_Ehdr ehdr)13885 verify_sections (DSO *dso, unsigned int *sorted_section_numbers,
13886 GElf_Off *distance, int addsec, GElf_Off addsize,
13887 GElf_Ehdr ehdr)
13888 {
13889 int i, j;
13890 int prev, update_prev;
13891 GElf_Off offset, prev_offset, prev_size;
13892 GElf_Off section_header_table_size
13893 = dso->ehdr.e_shentsize * ehdr.e_shnum;
13894
13895 prev = -1;
13896 for (i = 1; i < (dso->ehdr.e_shnum + 1); ++i, prev = update_prev)
13897 {
13898 j = sorted_section_numbers[i];
13899
13900 if (j != dso->ehdr.e_shnum && dso->shdr[j].sh_type == SHT_NOBITS)
13901 {
13902 update_prev = prev;
13903 continue;
13904 }
13905 update_prev = j;
13906
13907 if (prev == -1)
13908 continue;
13909
13910 offset = (j == dso->ehdr.e_shnum
13911 ? ehdr.e_shoff
13912 : dso->shdr[j].sh_offset);
13913
13914 prev_offset = (prev == dso->ehdr.e_shnum
13915 ? ehdr.e_shoff
13916 : dso->shdr[prev].sh_offset);
13917
13918 prev_size = (prev == dso->ehdr.e_shnum
13919 ? section_header_table_size
13920 : (dso->shdr[prev].sh_type == SHT_NOBITS
13921 ? 0
13922 : dso->shdr[prev].sh_size));
13923
13924 if (distance != NULL)
13925 assert ((prev_offset + prev_size + distance[prev]
13926 + (prev == addsec ? addsize : 0))
13927 == offset);
13928 else
13929 assert ((prev_offset + prev_size + (prev == addsec ? addsize : 0))
13930 <= offset);
13931 }
13932 }
13933
13934 /* Calculate distance between sections and section header table. */
13935 static int
calculate_section_distance(DSO * dso,unsigned int * sorted_section_numbers,GElf_Off * distance)13936 calculate_section_distance (DSO *dso, unsigned int *sorted_section_numbers,
13937 GElf_Off *distance)
13938 {
13939 int i, j;
13940 int prev, update_prev;
13941 GElf_Off offset, prev_offset, prev_size;
13942 GElf_Off section_header_table_size
13943 = dso->ehdr.e_shentsize * dso->ehdr.e_shnum;
13944
13945 prev = -1;
13946 for (i = 1; i < (dso->ehdr.e_shnum + 1); ++i, prev = update_prev)
13947 {
13948 j = sorted_section_numbers[i];
13949
13950 if (j != dso->ehdr.e_shnum && dso->shdr[j].sh_type == SHT_NOBITS)
13951 {
13952 update_prev = prev;
13953 continue;
13954 }
13955 update_prev = j;
13956
13957 if (prev == -1)
13958 continue;
13959
13960 offset = (j == dso->ehdr.e_shnum
13961 ? dso->ehdr.e_shoff
13962 : dso->shdr[j].sh_offset);
13963
13964 prev_offset = (prev == dso->ehdr.e_shnum
13965 ? dso->ehdr.e_shoff
13966 : dso->shdr[prev].sh_offset);
13967
13968 prev_size = (prev == dso->ehdr.e_shnum
13969 ? section_header_table_size
13970 : dso->shdr[prev].sh_size);
13971
13972 if (prev_offset + prev_size > offset)
13973 {
13974 error (0, 0, "Section overlap detected");
13975 if (prev == dso->ehdr.e_shnum)
13976 error (0, 0, "Section header table: [0x%llx, 0x%llx)",
13977 (unsigned long long)prev_offset,
13978 (unsigned long long)(prev_offset + prev_size));
13979 else
13980 error (0, 0, "Section %d: [0x%llx, 0x%llx)", j,
13981 (unsigned long long)prev_offset,
13982 (unsigned long long)(prev_offset + prev_size));
13983 if (j == dso->ehdr.e_shnum)
13984 error (0, 0, "Section header table: 0x%llx",
13985 (unsigned long long)offset);
13986 else
13987 error (0, 0, "Section %d: 0x%llx", j, (unsigned long long)offset);
13988 return 1;
13989 }
13990
13991 distance[prev] = offset - (prev_offset + prev_size);
13992 }
13993
13994 verify_sections (dso, sorted_section_numbers, distance, -1, 0, dso->ehdr);
13995
13996 return 0;
13997 }
13998
13999 /* Store new ELF into FILE. debug_sections array contains
14000 new_data/new_size pairs where needed. */
14001 static int
write_dso(DSO * dso,const char * file,struct stat * st,bool save_to_temp)14002 write_dso (DSO *dso, const char *file, struct stat *st, bool save_to_temp)
14003 {
14004 Elf *elf = NULL;
14005 GElf_Ehdr ehdr;
14006 GElf_Off min_shoff = ~(GElf_Off) 0;
14007 char *e_ident;
14008 int fd, i, j, addsec = -1, ret;
14009 GElf_Off off, diff, addsize = 0;
14010 char *filename = NULL;
14011 GElf_Word shstrtabadd = 0;
14012 char *shstrtab = NULL;
14013 bool remove_sections[SECTION_COUNT];
14014 GElf_Off distance[dso->ehdr.e_shnum + 1];
14015 /* Array of sections and section header table sorted by file offset. */
14016 unsigned int sorted_section_numbers[dso->ehdr.e_shnum + 1];
14017 GElf_Off old_sh_offset[dso->ehdr.e_shnum];
14018
14019 if (unlikely (progress_p))
14020 {
14021 report_progress ();
14022 fprintf (stderr, "write_dso\n");
14023 }
14024
14025 for (i = 1; i < dso->ehdr.e_shnum; ++i)
14026 old_sh_offset[i] = dso->shdr[i].sh_offset;
14027
14028 memset (remove_sections, '\0', sizeof (remove_sections));
14029 ehdr = dso->ehdr;
14030
14031 sort_section_numbers (dso, sorted_section_numbers);
14032 if (calculate_section_distance (dso, sorted_section_numbers, distance))
14033 return 1;
14034
14035 for (i = 0; debug_sections[i].name; i++)
14036 if (debug_sections[i].new_size != debug_sections[i].size)
14037 {
14038 if (debug_sections[i].size == 0
14039 && debug_sections[i].sec == 0)
14040 {
14041 unsigned int len;
14042 if (addsec == -1)
14043 for (j = 0; debug_sections[j].name; j++)
14044 if (debug_sections[j].new_size
14045 && debug_sections[j].sec
14046 && debug_sections[j].sec > addsec)
14047 addsec = debug_sections[j].sec;
14048 ehdr.e_shnum++;
14049 if (ehdr.e_shoff < min_shoff)
14050 min_shoff = ehdr.e_shoff;
14051 for (j = 1; j < dso->ehdr.e_shnum; ++j)
14052 {
14053 if (dso->shdr[j].sh_offset > ehdr.e_shoff)
14054 dso->shdr[j].sh_offset += ehdr.e_shentsize;
14055 if (dso->shdr[j].sh_link > (unsigned int) addsec)
14056 dso->shdr[j].sh_link++;
14057 if ((dso->shdr[j].sh_type == SHT_REL
14058 || dso->shdr[j].sh_type == SHT_RELA
14059 || (dso->shdr[j].sh_flags & SHF_INFO_LINK))
14060 && dso->shdr[j].sh_info
14061 > (unsigned int) addsec)
14062 dso->shdr[j].sh_info++;
14063 }
14064 if (dso->ehdr.e_shstrndx > addsec)
14065 ehdr.e_shstrndx++;
14066 len = strlen (debug_sections[i].name) + 1;
14067 dso->shdr[dso->ehdr.e_shstrndx].sh_size += len;
14068 if (dso->shdr[dso->ehdr.e_shstrndx].sh_offset < min_shoff)
14069 min_shoff = dso->shdr[dso->ehdr.e_shstrndx].sh_offset;
14070 for (j = 1; j < dso->ehdr.e_shnum; ++j)
14071 if (dso->shdr[j].sh_offset
14072 > dso->shdr[dso->ehdr.e_shstrndx].sh_offset)
14073 dso->shdr[j].sh_offset += len;
14074 if (ehdr.e_shoff > dso->shdr[dso->ehdr.e_shstrndx].sh_offset)
14075 ehdr.e_shoff += len;
14076 shstrtabadd += len;
14077 diff = debug_sections[i].new_size;
14078 addsize += diff;
14079 off = dso->shdr[addsec].sh_offset;
14080 }
14081 else
14082 {
14083 diff = (GElf_Off) debug_sections[i].new_size
14084 - (GElf_Off) dso->shdr[debug_sections[i].sec].sh_size;
14085 off = dso->shdr[debug_sections[i].sec].sh_offset;
14086 }
14087 if (off < min_shoff)
14088 min_shoff = off;
14089 for (j = 1; j < dso->ehdr.e_shnum; ++j)
14090 if (dso->shdr[j].sh_offset > off)
14091 dso->shdr[j].sh_offset += diff;
14092 if (ehdr.e_shoff > off)
14093 ehdr.e_shoff += diff;
14094 dso->shdr[debug_sections[i].sec].sh_size
14095 = debug_sections[i].new_size;
14096 if (debug_sections[i].new_size == 0)
14097 {
14098 remove_sections[i] = true;
14099 ehdr.e_shnum--;
14100 if (ehdr.e_shoff < min_shoff)
14101 min_shoff = ehdr.e_shoff;
14102 for (j = 1; j < dso->ehdr.e_shnum; ++j)
14103 {
14104 if (dso->shdr[j].sh_offset > ehdr.e_shoff)
14105 dso->shdr[j].sh_offset -= ehdr.e_shentsize;
14106 if (dso->shdr[j].sh_link
14107 > (unsigned int) debug_sections[i].sec)
14108 dso->shdr[j].sh_link--;
14109 if ((dso->shdr[j].sh_type == SHT_REL
14110 || dso->shdr[j].sh_type == SHT_RELA
14111 || (dso->shdr[j].sh_flags & SHF_INFO_LINK))
14112 && dso->shdr[j].sh_info
14113 > (unsigned int) debug_sections[i].sec)
14114 dso->shdr[j].sh_info--;
14115 }
14116 if (dso->ehdr.e_shstrndx > debug_sections[i].sec)
14117 ehdr.e_shstrndx--;
14118 }
14119 }
14120
14121 /* Verify that we did not change section layout, by checking that the
14122 distances between sections and section header table remained the same. */
14123 verify_sections (dso, sorted_section_numbers, distance, addsec, addsize,
14124 ehdr);
14125
14126 if (min_shoff != ~(GElf_Off) 0)
14127 {
14128 for (j = 1; j < dso->ehdr.e_shnum; ++j)
14129 if (dso->shdr[j].sh_offset >= min_shoff
14130 && dso->shdr[j].sh_addralign > 1
14131 && (dso->shdr[j].sh_offset & (dso->shdr[j].sh_addralign - 1)) != 0)
14132 break;
14133 if (j < dso->ehdr.e_shnum
14134 || (ehdr.e_shoff >= min_shoff
14135 && (ehdr.e_shoff & (ehdr.e_ident[EI_CLASS] == ELFCLASS64
14136 ? 7 : 3)) != 0))
14137 {
14138 /* Need to fix up sh_offset/e_shoff. Punt if all the sections
14139 >= min_shoff aren't non-ALLOC. */
14140 GElf_Off last_shoff = 0;
14141 int k = -1;
14142 int l;
14143 for (l = 1; l <= dso->ehdr.e_shnum; ++l)
14144 {
14145 j = sorted_section_numbers[l];
14146 if (j == dso->ehdr.e_shnum)
14147 continue;
14148 else if (!last_shoff
14149 && (dso->shdr[j].sh_offset < min_shoff
14150 || (dso->shdr[j].sh_offset == min_shoff
14151 && (dso->shdr[j].sh_size == 0
14152 || dso->shdr[j].sh_type == SHT_NOBITS))))
14153 continue;
14154 else if (dso->shdr[j].sh_type == SHT_NOBITS)
14155 continue;
14156 else if ((dso->shdr[j].sh_flags & SHF_ALLOC) != 0)
14157 {
14158 error (0, 0, "Allocatable section in %s after "
14159 "non-allocatable ones", dso->filename);
14160 return 1;
14161 }
14162 else
14163 {
14164 assert (dso->shdr[j].sh_offset >= last_shoff);
14165
14166 if (k == -1)
14167 k = l;
14168 last_shoff = dso->shdr[j].sh_offset + dso->shdr[j].sh_size;
14169 }
14170 }
14171 last_shoff = min_shoff;
14172 for (l = k; l <= dso->ehdr.e_shnum; ++l)
14173 {
14174 j = sorted_section_numbers[l];
14175 if (j == dso->ehdr.e_shnum)
14176 {
14177 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64)
14178 ehdr.e_shoff = (last_shoff + 7) & -8;
14179 else
14180 ehdr.e_shoff = (last_shoff + 3) & -4;
14181 last_shoff = ehdr.e_shoff + ehdr.e_shnum * ehdr.e_shentsize;
14182 continue;
14183 }
14184 /* Ignore SHT_NOBITS sections. */
14185 if (dso->shdr[j].sh_type == SHT_NOBITS)
14186 continue;
14187 dso->shdr[j].sh_offset = last_shoff;
14188 if (dso->shdr[j].sh_addralign > 1)
14189 dso->shdr[j].sh_offset
14190 = (last_shoff + dso->shdr[j].sh_addralign - 1)
14191 & ~(dso->shdr[j].sh_addralign - (GElf_Off) 1);
14192 last_shoff = dso->shdr[j].sh_offset + dso->shdr[j].sh_size;
14193 if (addsec != -1 && j == addsec)
14194 last_shoff += addsize;
14195 }
14196 }
14197 }
14198
14199 for (i = 1; i < dso->ehdr.e_shnum; ++i)
14200 if (dso->shdr[i].sh_type == SHT_NOBITS)
14201 dso->shdr[i].sh_offset = old_sh_offset[i];
14202
14203 verify_sections (dso, sorted_section_numbers, NULL, addsec, addsize,
14204 ehdr);
14205
14206 if (shstrtabadd != 0)
14207 {
14208 shstrtab = (char *) malloc (dso->shdr[dso->ehdr.e_shstrndx].sh_size);
14209 if (shstrtab == NULL)
14210 {
14211 error (0, ENOMEM, "Failed to adjust .shstrtab for %s",
14212 dso->filename);
14213 return 1;
14214 }
14215 }
14216
14217 if (file == NULL)
14218 {
14219 size_t len = strlen (dso->filename);
14220 filename = alloca (len + sizeof (".#dwz#.XXXXXX"));
14221 memcpy (filename, dso->filename, len);
14222 memcpy (filename + len, ".#dwz#.XXXXXX", sizeof (".#dwz#.XXXXXX"));
14223 fd = mkstemp (filename);
14224 file = (const char *) filename;
14225 if (fd == -1)
14226 {
14227 error (0, errno, "Failed to create temporary file for %s",
14228 dso->filename);
14229 free (shstrtab);
14230 return 1;
14231 }
14232 }
14233 else
14234 {
14235 fd = open (file, O_RDWR | O_CREAT, 0600);
14236 if (fd == -1)
14237 {
14238 error (0, errno, "Failed to open %s for writing", file);
14239 free (shstrtab);
14240 return 1;
14241 }
14242 }
14243
14244 elf = elf_begin (fd, ELF_C_WRITE, NULL);
14245 if (elf == NULL)
14246 {
14247 error (0, 0, "cannot open ELF file: %s", elf_errmsg (-1));
14248 unlink (file);
14249 close (fd);
14250 free (shstrtab);
14251 return 1;
14252 }
14253
14254 /* Some gelf_newehdr implementations don't return the resulting
14255 ElfNN_Ehdr, so we have to do it the hard way instead of:
14256 e_ident = (char *) gelf_newehdr (elf, gelf_getclass (dso->elf)); */
14257 switch (gelf_getclass (dso->elf))
14258 {
14259 case ELFCLASS32:
14260 e_ident = (char *) elf32_newehdr (elf);
14261 break;
14262 case ELFCLASS64:
14263 e_ident = (char *) elf64_newehdr (elf);
14264 break;
14265 default:
14266 e_ident = NULL;
14267 break;
14268 }
14269
14270 if (e_ident == NULL
14271 /* This is here just for the gelfx wrapper, so that gelf_update_ehdr
14272 already has the correct ELF class. */
14273 || memcpy (e_ident, dso->ehdr.e_ident, EI_NIDENT) == NULL
14274 || gelf_update_ehdr (elf, &ehdr) == 0
14275 || gelf_newphdr (elf, ehdr.e_phnum) == 0)
14276 {
14277 error (0, 0, "Could not create new ELF headers");
14278 unlink (file);
14279 elf_end (elf);
14280 close (fd);
14281 free (shstrtab);
14282 return 1;
14283 }
14284 elf_flagelf (elf, ELF_C_SET, ELF_F_LAYOUT);
14285 for (i = 0; i < ehdr.e_phnum; ++i)
14286 {
14287 GElf_Phdr *phdr, phdr_mem;
14288 phdr = gelf_getphdr (dso->elf, i, &phdr_mem);
14289 gelf_update_phdr (elf, i, phdr);
14290 }
14291
14292 for (i = 1; i < dso->ehdr.e_shnum; ++i)
14293 {
14294 Elf_Scn *scn;
14295 Elf_Data *data1, *data2;
14296
14297 for (j = 0; debug_sections[j].name; j++)
14298 if (i == debug_sections[j].sec)
14299 break;
14300 if (debug_sections[j].name && remove_sections[j])
14301 continue;
14302 scn = elf_newscn (elf);
14303 elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY);
14304 gelf_update_shdr (scn, &dso->shdr[i]);
14305 data1 = elf_getdata (dso->scn[i], NULL);
14306 data2 = elf_newdata (scn);
14307 memcpy (data2, data1, sizeof (*data1));
14308 if (debug_sections[j].name
14309 && debug_sections[j].new_data != NULL)
14310 {
14311 data2->d_buf = debug_sections[j].new_data;
14312 data2->d_size = dso->shdr[i].sh_size;
14313 }
14314 if (i == dso->ehdr.e_shstrndx && shstrtabadd)
14315 {
14316 memcpy (shstrtab, data1->d_buf,
14317 dso->shdr[dso->ehdr.e_shstrndx].sh_size
14318 - shstrtabadd);
14319 data2->d_buf = shstrtab;
14320 data2->d_size = dso->shdr[i].sh_size;
14321 }
14322 if (i == addsec)
14323 {
14324 GElf_Word sh_name = dso->shdr[dso->ehdr.e_shstrndx].sh_size
14325 - shstrtabadd;
14326 GElf_Shdr shdr;
14327
14328 off = dso->shdr[i].sh_offset + dso->shdr[i].sh_size;
14329 for (j = 0; debug_sections[j].name; j++)
14330 if (debug_sections[j].new_size
14331 && debug_sections[j].size == 0
14332 && debug_sections[j].sec == 0)
14333 {
14334 scn = elf_newscn (elf);
14335 elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY);
14336 memset (&shdr, '\0', sizeof (shdr));
14337 shdr.sh_name = sh_name;
14338 sh_name += strlen (debug_sections[j].name) + 1;
14339 strcpy (shstrtab + shdr.sh_name, debug_sections[j].name);
14340 shdr.sh_type = SHT_PROGBITS;
14341 shdr.sh_offset = off;
14342 shdr.sh_size = debug_sections[j].new_size;
14343 shdr.sh_addralign = 1;
14344 off += shdr.sh_size;
14345 gelf_update_shdr (scn, &shdr);
14346 data2 = elf_newdata (scn);
14347 data2->d_buf = debug_sections[j].new_data;
14348 data2->d_type = ELF_T_BYTE;
14349 data2->d_version = EV_CURRENT;
14350 data2->d_size = shdr.sh_size;
14351 data2->d_off = 0;
14352 data2->d_align = 1;
14353 }
14354 }
14355 }
14356
14357 if (elf_update (elf, ELF_C_WRITE) == -1)
14358 {
14359 error (0, 0, "%s: elf_update failed", dso->filename);
14360 unlink (file);
14361 elf_end (elf);
14362 close (fd);
14363 free (shstrtab);
14364 return 1;
14365 }
14366
14367 if (elf_end (elf) < 0)
14368 {
14369 error (0, 0, "elf_end failed: %s", elf_errmsg (elf_errno ()));
14370 unlink (file);
14371 elf_end (elf);
14372 close (fd);
14373 free (shstrtab);
14374 return 1;
14375 }
14376
14377 free (shstrtab);
14378 ret = fchown (fd, st->st_uid, st->st_gid);
14379 fchmod (fd, st->st_mode & 07777);
14380 close (fd);
14381
14382 if (filename != NULL && rename (filename, dso->filename))
14383 {
14384 error (0, errno, "Failed to rename temporary file over %s",
14385 dso->filename);
14386 unlink (file);
14387 /* | (ret & 1) to silence up __wur warning for fchown. */
14388 return 1 | (ret & 1);
14389 }
14390 if (save_to_temp)
14391 {
14392 const char *prefix = "dwz.";
14393 size_t buf_len = strlen (prefix) + strlen (dso->filename) + 1;
14394 char *buf = (char *)alloca (buf_len);
14395 size_t offset = 0;
14396 strcpy (&buf[offset], prefix);
14397 offset += strlen (prefix);
14398 strcpy (&buf[offset], dso->filename);
14399 offset += strlen (dso->filename);
14400 assert (offset == buf_len - 1);
14401 assert (buf[offset] == '\0');
14402 unlink (buf);
14403 if (link (dso->filename, buf) != 0)
14404 {
14405 error (0, errno, "Failed to link file: %s", dso->filename);
14406 return 1;
14407 }
14408 }
14409 return 0;
14410 }
14411
14412 /* Free memory and clear global variables. */
14413 static void
cleanup(void)14414 cleanup (void)
14415 {
14416 dw_cu_ref cu;
14417 unsigned int i;
14418
14419 for (cu = first_cu; cu; cu = cu->cu_next)
14420 {
14421 if (cu->cu_new_abbrev)
14422 htab_delete (cu->cu_new_abbrev);
14423 cu->cu_new_abbrev = NULL;
14424 }
14425 if (off_htab != NULL)
14426 {
14427 if (tracing)
14428 htab_report (off_htab, "off_htab final");
14429 htab_delete (off_htab);
14430 }
14431 off_htab = NULL;
14432 if (types_off_htab != NULL)
14433 htab_delete (types_off_htab);
14434 types_off_htab = NULL;
14435 if (loc_htab != NULL)
14436 htab_delete (loc_htab);
14437 loc_htab = NULL;
14438 if (loclists_htab != NULL)
14439 htab_delete (loclists_htab);
14440 loclists_htab = NULL;
14441 if (dup_htab != NULL)
14442 htab_delete (dup_htab);
14443 dup_htab = NULL;
14444 if (strp_htab != NULL)
14445 htab_delete (strp_htab);
14446 strp_htab = NULL;
14447 if (line_htab != NULL)
14448 htab_delete (line_htab);
14449 line_htab = NULL;
14450 if (macro_htab != NULL)
14451 htab_delete (macro_htab);
14452 macro_htab = NULL;
14453 if (meta_abbrev_htab != NULL)
14454 htab_delete (meta_abbrev_htab);
14455 meta_abbrev_htab = NULL;
14456
14457 for (i = 0; i < SAVED_SECTIONS; ++i)
14458 {
14459 free (saved_new_data[i]);
14460 saved_new_data[i] = NULL;
14461 }
14462
14463 obstack_free (&ob2, NULL);
14464 obstack_free (&ob, NULL);
14465 memset (&ob2, '\0', sizeof (ob2));
14466 memset (&ob, '\0', sizeof (ob2));
14467 die_nontoplevel_freelist = NULL;
14468 die_collapsed_child_freelist = NULL;
14469 pool_destroy ();
14470 first_cu = NULL;
14471 last_cu = NULL;
14472 ptr_size = 0;
14473 max_nattr = 0;
14474 do_read_16 = NULL;
14475 do_read_32 = NULL;
14476 do_read_64 = NULL;
14477 do_write_16 = NULL;
14478 do_write_32 = NULL;
14479 do_write_64 = NULL;
14480 edge_freelist = NULL;
14481 multifile_mode = 0;
14482 max_strp_off = 0;
14483 max_line_id = 0;
14484 }
14485
14486 /* Propagate the die_no_multifile property along the duplicate chain of which
14487 DIE is a member. If the property was changed on any die, set *CHANGED to
14488 true. */
14489 static void
propagate_multifile_duplicate_chain(dw_die_ref die,bool * changed)14490 propagate_multifile_duplicate_chain (dw_die_ref die, bool *changed)
14491 {
14492 dw_die_ref dup = first_dup (die);
14493 if (!dup)
14494 return;
14495
14496 while (dup && dup->die_offset == -1U)
14497 dup = dup->die_nextdup;
14498 if (dup != die)
14499 return;
14500
14501 bool any_no_multifile = false;
14502 bool any_multifile = false;
14503 bool prop_needed = false;
14504 dw_die_ref d;
14505 for (d = dup; d && !prop_needed; d = d->die_nextdup)
14506 {
14507 if (d->die_no_multifile)
14508 any_no_multifile = true;
14509 else
14510 any_multifile = true;
14511 prop_needed = any_no_multifile && any_multifile;
14512 }
14513 if (!prop_needed)
14514 return;
14515
14516 *changed = true;
14517
14518 for (d = dup; d; d = d->die_nextdup)
14519 d->die_no_multifile = 1;
14520 }
14521
14522 /* Propagate the die_no_multifile property backwards along the outgoing
14523 references of DIE, which is a member of CU and of the subtree of lower
14524 toplevel die TOP_DIE. If the property was changed on any die, set *CHANGED
14525 to true. */
14526 static void
propagate_multifile_refs_backward(dw_cu_ref cu,dw_die_ref top_die,dw_die_ref die,bool * changed)14527 propagate_multifile_refs_backward (dw_cu_ref cu, dw_die_ref top_die,
14528 dw_die_ref die, bool *changed)
14529 {
14530 struct abbrev_tag *t = die->die_abbrev;
14531 unsigned int i;
14532 unsigned char *ptr;
14533 dw_die_ref child;
14534
14535 if (die->die_offset == -1U)
14536 return;
14537
14538 ptr = debug_sections[DEBUG_INFO].data + die->die_offset;
14539 skip_leb128 (ptr);
14540 for (i = 0; i < t->nattr; ++i)
14541 {
14542 uint32_t form = t->attr[i].form;
14543 uint64_t value;
14544 dw_die_ref ref, reft;
14545
14546 while (form == DW_FORM_indirect)
14547 form = read_uleb128 (ptr);
14548
14549 switch (form)
14550 {
14551 case DW_FORM_ref_addr:
14552 value = read_size (ptr, cu->cu_version == 2 ? ptr_size : 4);
14553 ptr += cu->cu_version == 2 ? ptr_size : 4;
14554 ref = off_htab_lookup (cu, value);
14555 goto finish_ref;
14556 break;
14557 case DW_FORM_ref_udata:
14558 case DW_FORM_ref1:
14559 case DW_FORM_ref2:
14560 case DW_FORM_ref4:
14561 case DW_FORM_ref8:
14562 switch (form)
14563 {
14564 case DW_FORM_ref_udata: value = read_uleb128 (ptr); break;
14565 case DW_FORM_ref1: value = read_8 (ptr); break;
14566 case DW_FORM_ref2: value = read_16 (ptr); break;
14567 case DW_FORM_ref4: value = read_32 (ptr); break;
14568 case DW_FORM_ref8: value = read_64 (ptr); break;
14569 default: abort ();
14570 }
14571 if (t->attr[i].attr == DW_AT_sibling)
14572 break;
14573 ref = off_htab_lookup (cu, cu->cu_offset + value);
14574 finish_ref:
14575 reft = ref;
14576 while (!reft->die_root
14577 && reft->die_parent->die_tag != DW_TAG_compile_unit
14578 && reft->die_parent->die_tag != DW_TAG_partial_unit
14579 && !reft->die_parent->die_named_namespace)
14580 reft = reft->die_parent;
14581 if (reft->die_root)
14582 ;
14583 else if (reft->die_ck_state == CK_KNOWN
14584 && !top_die->die_no_multifile && reft->die_no_multifile)
14585 {
14586 top_die->die_no_multifile = 1;
14587 *changed = true;
14588 }
14589 break;
14590 default:
14591 ptr = skip_attr_no_dw_form_indirect (cu->cu_version, form, ptr);
14592 }
14593 }
14594
14595 for (child = die->die_child; child; child = child->die_sib)
14596 propagate_multifile_refs_backward (cu, top_die, child, changed);
14597 }
14598
14599 /* Do propagation of the die_no_multifile property that was not covered in
14600 checksum_die and checksum_ref_die. */
14601 static void
propagate_multifile(void)14602 propagate_multifile (void)
14603 {
14604 bool changed;
14605 dw_cu_ref cu;
14606 dw_die_ref die;
14607
14608 changed = false;
14609
14610 FOREACH_CU_NORMAL_LOW_TOPLEVEL_DIE (cu, die)
14611 propagate_multifile_duplicate_chain (die, &changed);
14612
14613 if (!changed)
14614 return;
14615
14616 do
14617 {
14618 changed = false;
14619
14620 FOREACH_CU_NORMAL_LOW_TOPLEVEL_DIE (cu, die)
14621 propagate_multifile_refs_backward (cu, die, die, &changed);
14622
14623 FOREACH_CU_NORMAL_LOW_TOPLEVEL_DIE (cu, die)
14624 propagate_multifile_duplicate_chain (die, &changed);
14625 }
14626 while (changed);
14627 }
14628
14629 /* Returns true if DIE contains any toplevel children that can be
14630 potentially shared between different executables or shared libraries. */
14631 static bool
check_multifile(dw_die_ref die)14632 check_multifile (dw_die_ref die)
14633 {
14634 dw_die_ref child;
14635
14636 die->die_no_multifile = 1;
14637 for (child = die->die_child; child; child = child->die_sib)
14638 if (child->die_named_namespace)
14639 {
14640 if (check_multifile (child))
14641 die->die_no_multifile = 0;
14642 }
14643 else if (child->die_offset == -1U)
14644 {
14645 if (child->die_nextdup && child->die_nextdup->die_dup == child)
14646 {
14647 if (child->die_nextdup->die_ck_state == CK_KNOWN
14648 && child->die_nextdup->die_no_multifile == 0)
14649 {
14650 child->die_no_multifile = 0;
14651 die->die_no_multifile = 0;
14652 }
14653 else
14654 child->die_no_multifile = 1;
14655 }
14656 else
14657 child->die_no_multifile = 1;
14658 }
14659 else
14660 {
14661 child->die_op_type_referenced = 0;
14662 if (child->die_dup == NULL
14663 && child->die_ck_state == CK_KNOWN
14664 && child->die_no_multifile == 0)
14665 die->die_no_multifile = 0;
14666 else
14667 child->die_no_multifile = 1;
14668 }
14669 return die->die_no_multifile == 0;
14670 }
14671
14672 /* Helper function for write_multifile_strp to sort strp_entry
14673 by increasing new_off. */
14674 static int
strp_off_cmp(const void * p,const void * q)14675 strp_off_cmp (const void *p, const void *q)
14676 {
14677 struct strp_entry *s1 = *(struct strp_entry **)p;
14678 struct strp_entry *s2 = *(struct strp_entry **)q;
14679 if (s1->new_off < s2->new_off)
14680 return -1;
14681 if (s1->new_off > s2->new_off)
14682 return 1;
14683 return 0;
14684 }
14685
14686 /* Write tail optimized strings into the temporary .debug_str file. */
14687 static int
write_multifile_strp(void)14688 write_multifile_strp (void)
14689 {
14690 unsigned int count = htab_elements (strp_htab), i, buf_alloc, buf_size;
14691 struct strp_entry **arr = (struct strp_entry **)
14692 obstack_alloc (&ob, count * sizeof (*arr));
14693 struct strp_entry **end = arr;
14694 unsigned char *buf;
14695 int ret = 0;
14696
14697 htab_traverse (strp_htab, list_strp_entries, (void *) &end);
14698 assert (arr + count == end);
14699 qsort (arr, count, sizeof (struct strp_entry *), strp_off_cmp);
14700 buf_alloc = max_strp_off - debug_sections[DEBUG_STR].size;
14701 if (buf_alloc > 131072)
14702 buf_alloc = 131072;
14703 buf = (unsigned char *) obstack_alloc (&ob, buf_alloc);
14704 buf_size = 0;
14705 for (i = 0; i < count; i++)
14706 {
14707 unsigned char *p = debug_sections[DEBUG_STR].data + arr[i]->off;
14708 unsigned int len = strlen ((char *) p) + 1;
14709 if (buf_alloc - buf_size < len)
14710 {
14711 if (buf_size
14712 && write (multi_str_fd, buf, buf_size) != (ssize_t) buf_size)
14713 {
14714 ret = 1;
14715 break;
14716 }
14717 buf_size = 0;
14718 if (buf_alloc < len)
14719 {
14720 if (write (multi_str_fd, p, len) != (ssize_t) len)
14721 {
14722 ret = 1;
14723 break;
14724 }
14725 continue;
14726 }
14727 }
14728 memcpy (buf + buf_size, p, len);
14729 buf_size += len;
14730 }
14731 if (buf_size
14732 && ret == 0
14733 && write (multi_str_fd, buf, buf_size) != (ssize_t) buf_size)
14734 ret = 1;
14735 obstack_free (&ob, (void *) arr);
14736 return ret;
14737 }
14738
14739 /* Hold some statistics on the line entries so we know whether to emit
14740 time and/or sizes. Used by list_line_entries used by
14741 write_multifile_line. */
14742 struct line_stats
14743 {
14744 struct line_entry ***end;
14745 bool has_time;
14746 bool has_size;
14747 };
14748
14749 /* Helper to find the end of the line_htab entries and other stats.
14750 Called through htab_traverse. */
14751 static int
list_line_entries(void ** slot,void * data)14752 list_line_entries (void **slot, void *data)
14753 {
14754 struct line_stats *stats = (struct line_stats *) data;
14755 struct line_entry *entry = (struct line_entry *) *slot;
14756 struct line_entry ***end = stats->end;
14757 **end = entry;
14758 (*end)++;
14759 if (entry->file->time != 0)
14760 stats->has_time = true;
14761 if (entry->file->size != 0)
14762 stats->has_size = true;
14763 return 1;
14764 }
14765
14766 /* Helper function for write_multifile_strp to sort line_entry
14767 by increasing new_id. */
14768 static int
line_id_cmp(const void * p,const void * q)14769 line_id_cmp (const void *p, const void *q)
14770 {
14771 struct line_entry *s1 = *(struct line_entry **)p;
14772 struct line_entry *s2 = *(struct line_entry **)q;
14773 if (s1->new_id < s2->new_id)
14774 return -1;
14775 if (s1->new_id > s2->new_id)
14776 return 1;
14777 return 0;
14778 }
14779
14780 /* Write a minimal .debug_line entry. If not op_multifile, write it
14781 into the temporary .debug_line file (if line_htab is non-NULL, fill
14782 its directory and file table from it, otherwise emit an entry
14783 with no directories or files), if op_multifile, store the entry
14784 into debug_sections[DEBUG_LINE].new_data which it allocates. */
14785 static int
write_multifile_line(void)14786 write_multifile_line (void)
14787 {
14788 unsigned int filecnt = 0, dircnt = 0, filetbllen = 0, dirtbllen = 0;
14789 unsigned int header_len, len, i, j;
14790 unsigned char *line, *ptr;
14791 struct line_entry **filearr = NULL;
14792 struct line_stats line_stats;
14793 unsigned int *diridx = NULL, *dirarr = NULL;
14794 unsigned char buf[45]; /* Max header_len, see below. */
14795 int ret = 0;
14796
14797 line_stats.has_time = line_stats.has_size = false;
14798 if (line_htab)
14799 {
14800 struct line_entry **end;
14801 filecnt = htab_elements (line_htab);
14802 filearr = (struct line_entry **)
14803 obstack_alloc (&ob, filecnt * sizeof (*filearr));
14804 end = filearr;
14805 line_stats.end = &end;
14806 htab_traverse (line_htab, list_line_entries, (void *) &line_stats);
14807 assert (filearr + filecnt == end);
14808 diridx = (unsigned int *)
14809 obstack_alloc (&ob, filecnt * sizeof (*diridx));
14810 qsort (filearr, filecnt, sizeof (struct line_entry *), line_id_cmp);
14811 for (i = 0; i < filecnt; i++)
14812 {
14813 unsigned int direntrylen = 0;
14814 const char *file = filearr[i]->file->file;
14815 if (filearr[i]->file->dir == NULL)
14816 {
14817 const char *r = strrchr (file, '/'), *s;
14818
14819 j = 0;
14820 direntrylen = r ? r - file : 0;
14821 while (direntrylen && file[direntrylen - 1] == '/')
14822 direntrylen--;
14823 if (direntrylen)
14824 {
14825 direntrylen++;
14826 for (j = 0; j < dircnt; j++)
14827 if (filearr[dirarr[j]]->file->dir == NULL
14828 && strncmp (filearr[dirarr[j]]->file->file,
14829 file, direntrylen) == 0)
14830 {
14831 s = filearr[dirarr[j]]->file->file + direntrylen;
14832 while (*s == '/')
14833 s++;
14834 if (strchr (s, '/'))
14835 continue;
14836 break;
14837 }
14838 j++;
14839 file = r + 1;
14840 }
14841 }
14842 else
14843 {
14844 for (j = 0; j < dircnt; j++)
14845 if (filearr[dirarr[j]]->file->dir
14846 && strcmp (filearr[dirarr[j]]->file->dir,
14847 filearr[i]->file->dir) == 0)
14848 break;
14849 j++;
14850 direntrylen = strlen (filearr[i]->file->dir) + 1;
14851 }
14852 if (j <= dircnt)
14853 diridx[i] = j;
14854 else
14855 {
14856 obstack_int_grow (&ob, i);
14857 diridx[i] = ++dircnt;
14858 dirarr = (unsigned int *) obstack_base (&ob);
14859 dirtbllen += direntrylen;
14860 }
14861 filetbllen += strlen (file) + 1;
14862 filetbllen += size_of_uleb128 (diridx[i]);
14863 if (lowest_line_version < 5 || line_stats.has_time)
14864 filetbllen += size_of_uleb128 (filearr[i]->file->time);
14865 if (lowest_line_version < 5 || line_stats.has_size)
14866 filetbllen += size_of_uleb128 (filearr[i]->file->size);
14867 }
14868 dirarr = (unsigned int *) obstack_finish (&ob);
14869 }
14870
14871 /* standard .debug_line "header" length (both version 2 and 5):
14872 unit_length (4) + version (2) + header_length (4) +
14873 min_instr_length (1) + default_is_stmt (1) + line_base (1) +
14874 line_range (1) + opcode_base (1) = 15
14875
14876 version 2 adds 2 bytes, one zero byte to terminate dir and file lists.
14877
14878 version 5 adds at least 11 bytes, max_ops_per_instr (1) +
14879 address_size (1) + segment_size (1) + dir_entry_format_cnt (1) +
14880 format_pair (2), file_entry_format_cnt (1) + file_format_pairs
14881 (4). Plus dircnt (uleb128) + format_pair (2) if has_time +
14882 format_pair (2) if has_size) + filecnt (uleb128).
14883
14884 version 5 also has 2 extra 6 byte "<dwz>" string entries for dir
14885 and file entry zero, plus one for the zero file entry dir idx.
14886 */
14887 header_len = 15;
14888 if (lowest_line_version < 5)
14889 header_len += 2;
14890 else
14891 {
14892 header_len += 11;
14893 header_len += size_of_uleb128 (dircnt + 1);
14894 header_len += size_of_uleb128 (filecnt + 1);
14895 if (line_stats.has_time)
14896 header_len += 2;
14897 if (line_stats.has_size)
14898 header_len += 2;
14899 header_len += 2 * 6 + 1;
14900 }
14901 len = header_len + filetbllen + dirtbllen;
14902 if (unlikely (op_multifile))
14903 {
14904 debug_sections[DEBUG_LINE].new_size = len;
14905 debug_sections[DEBUG_LINE].new_data = malloc (len);
14906 if (debug_sections[DEBUG_LINE].new_data == NULL)
14907 dwz_oom ();
14908 line = debug_sections[DEBUG_LINE].new_data;
14909 }
14910 else
14911 {
14912 if (multi_line_off + len < multi_line_off)
14913 {
14914 if (line_htab)
14915 obstack_free (&ob, (void *) filearr);
14916 return 1;
14917 }
14918
14919 if (len == header_len)
14920 {
14921 line = buf;
14922 assert (sizeof (buf) >= header_len);
14923 }
14924 else
14925 line = (unsigned char *) obstack_alloc (&ob, len);
14926 }
14927 ptr = line;
14928 write_32 (ptr, len - 4); /* Total length. */
14929 if (lowest_line_version < 5)
14930 write_16 (ptr, 2); /* DWARF version. */
14931 else
14932 {
14933 write_16 (ptr, 5); /* DWARF version. */
14934 write_8 (ptr, multi_ptr_size); /* Address size. */
14935 write_8 (ptr, 0); /* Segment size. */
14936 }
14937 write_32 (ptr, /* Header length. */
14938 len - (lowest_line_version < 5 ? 10 : 12));
14939 write_8 (ptr, 1); /* Minimum insn length. */
14940 if (lowest_line_version >= 5)
14941 write_8 (ptr, 1); /* Maximum ops per instr. */
14942 write_8 (ptr, 1); /* Default is_stmt. */
14943 write_8 (ptr, 0); /* Line base. */
14944 write_8 (ptr, 1); /* Line range. */
14945 write_8 (ptr, 1); /* Opcode base. */
14946
14947 if (lowest_line_version >= 5)
14948 {
14949 write_8 (ptr, 1); /* Dir entry format count. */
14950 write_uleb128 (ptr, DW_LNCT_path);
14951 write_uleb128 (ptr, DW_FORM_string);
14952 write_uleb128 (ptr, dircnt + 1); /* Dir cnt. */
14953 memcpy (ptr, "<dwz>", 6); /* Zero entry empty dir path. */
14954 ptr += 6;
14955 }
14956
14957 for (i = 0; i < dircnt; i++)
14958 {
14959 unsigned int l;
14960 if (filearr[dirarr[i]]->file->dir)
14961 {
14962 l = strlen (filearr[dirarr[i]]->file->dir) + 1;
14963 memcpy (ptr, filearr[dirarr[i]]->file->dir, l);
14964 }
14965 else
14966 {
14967 const char *file = filearr[dirarr[i]]->file->file;
14968 const char *r = strrchr (file, '/');
14969
14970 while (r && r > file && r[-1] == '/')
14971 r--;
14972 l = r - file + 1;
14973 memcpy (ptr, file, l - 1);
14974 ptr[l - 1] = '\0';
14975 }
14976 ptr += l;
14977 }
14978 if (lowest_line_version < 5)
14979 write_8 (ptr, 0); /* Terminate dir table. */
14980 else
14981 {
14982 unsigned int format_cnt = 2 + line_stats.has_size + line_stats.has_time;
14983 write_8 (ptr, format_cnt); /* File entry format count. */
14984 write_uleb128 (ptr, DW_LNCT_path);
14985 write_uleb128 (ptr, DW_FORM_string);
14986 write_uleb128 (ptr, DW_LNCT_directory_index);
14987 write_uleb128 (ptr, DW_FORM_udata);
14988 if (line_stats.has_time)
14989 {
14990 write_uleb128 (ptr, DW_LNCT_timestamp);
14991 write_uleb128 (ptr, DW_FORM_udata);
14992 }
14993 if (line_stats.has_size)
14994 {
14995 write_uleb128 (ptr, DW_LNCT_size);
14996 write_uleb128 (ptr, DW_FORM_udata);
14997 }
14998 write_uleb128 (ptr, filecnt + 1); /* File names cnt. */
14999 memcpy (ptr, "<dwz>", 6); /* Zero entry empty file path. */
15000 ptr += 6;
15001 write_8 (ptr, 0); /* Zero entry zero diridx. */
15002 if (line_stats.has_time)
15003 write_8 (ptr, 0);
15004 if (line_stats.has_size)
15005 write_8 (ptr, 0);
15006 }
15007
15008 for (i = 0; i < filecnt; i++)
15009 {
15010 const char *file = filearr[i]->file->file;
15011 unsigned int l;
15012 if (diridx[i] && filearr[i]->file->dir == NULL)
15013 file = strrchr (file, '/') + 1;
15014 l = strlen (file) + 1;
15015 memcpy (ptr, file, l);
15016 ptr += l;
15017 write_uleb128 (ptr, diridx[i]);
15018 if (lowest_line_version < 5 || line_stats.has_time)
15019 write_uleb128 (ptr, filearr[i]->file->time);
15020 if (lowest_line_version < 5 || line_stats.has_size)
15021 write_uleb128 (ptr, filearr[i]->file->size);
15022 }
15023 if (lowest_line_version < 5)
15024 write_8 (ptr, 0); /* Terminate file table. */
15025 assert (ptr == line + len);
15026
15027 if (likely (!op_multifile))
15028 {
15029 if (write (multi_line_fd, line, len) != (ssize_t) len)
15030 ret = 1;
15031 else
15032 multi_line_off += len;
15033 if (line_htab)
15034 obstack_free (&ob, (void *) filearr);
15035 else if (line != buf)
15036 obstack_free (&ob, (void *) line);
15037 }
15038 else if (line_htab)
15039 obstack_free (&ob, (void *) filearr);
15040 return ret;
15041 }
15042
15043 #if DEVEL
15044 /* In struct dw_die we have a union u with fields p1 and p2. The p1 field is
15045 used during phase 1, after which the space is reused for the p2 field
15046 during phase 2. Clear the p2 field to get rid of values stored to p1
15047 during phase 1. */
15048 static int
clear_p2_field(void)15049 clear_p2_field (void)
15050 {
15051 dw_cu_ref cu;
15052 dw_die_ref die;
15053
15054 FOREACH_DIE (cu, die)
15055 {
15056 assert (die->die_collapsed_child == 0);
15057 die->u.p2.die_new_abbrev = NULL;
15058 die->u.p2.die_new_offset = 0;
15059 die->u.p2.die_intracu_udata_size = 0;
15060 }
15061
15062 return 0;
15063 }
15064 #endif
15065
15066 /* Collect potentially shareable DIEs, strings and .debug_macro
15067 opcode sequences into temporary .debug_* files. */
15068 static int
write_multifile(DSO * dso)15069 write_multifile (DSO *dso)
15070 {
15071 dw_cu_ref cu;
15072 bool any_cus = false;
15073 unsigned int i;
15074 int ret = 0;
15075
15076 if (unlikely (progress_p))
15077 {
15078 report_progress ();
15079 fprintf (stderr, "write_multifile\n");
15080 }
15081
15082 if (multi_ehdr.e_ident[0] == '\0')
15083 multi_ehdr = dso->ehdr;
15084
15085 if ((multi_ptr_size && ptr_size != multi_ptr_size)
15086 || (multi_endian
15087 && multi_endian != (do_read_32 == buf_read_ule32
15088 ? ELFDATA2LSB : ELFDATA2MSB)))
15089 {
15090 error (0, 0, "Multi-file optimization not allowed for different"
15091 " pointer sizes or endianity");
15092 multifile = NULL;
15093 return 1;
15094 }
15095 multi_ptr_size = ptr_size;
15096 multi_endian = do_read_32 == buf_read_ule32 ? ELFDATA2LSB : ELFDATA2MSB;
15097
15098 #if DEVEL
15099 clear_p2_field ();
15100 #endif
15101
15102 for (i = 0; i < SAVED_SECTIONS; i++)
15103 {
15104 saved_new_data[i] = debug_sections[i].new_data;
15105 saved_new_size[i] = debug_sections[i].new_size;
15106 debug_sections[i].new_data = NULL;
15107 debug_sections[i].new_size = debug_sections[i].size;
15108 }
15109 propagate_multifile ();
15110 for (cu = first_cu; cu && cu->cu_kind != CU_TYPES; cu = cu->cu_next)
15111 {
15112 cu->u1.cu_new_abbrev_owner = NULL;
15113 cu->u2.cu_new_abbrev_offset = 0;
15114 cu->cu_new_offset = 0;
15115 any_cus |= check_multifile (cu->cu_die);
15116 }
15117 if (any_cus)
15118 {
15119 dw_cu_ref *cup;
15120
15121 for (cup = &first_cu; *cup && (*cup)->cu_kind != CU_TYPES; )
15122 if ((*cup)->cu_die->die_no_multifile == 0)
15123 cup = &(*cup)->cu_next;
15124 else
15125 *cup = (*cup)->cu_next;
15126 *cup = NULL;
15127 multifile_mode = MULTIFILE_MODE_WR;
15128 if (tracing)
15129 fprintf (stderr, "Write-multifile %s\n", dso->filename);
15130 if (compute_abbrevs (NULL))
15131 ret = 1;
15132 else if (debug_sections[DEBUG_MACRO].data && read_macro (dso))
15133 ret = 1;
15134 else if ((unsigned int) (multi_info_off
15135 + debug_sections[DEBUG_INFO].new_size)
15136 < multi_info_off
15137 || (unsigned int) (multi_abbrev_off
15138 + debug_sections[DEBUG_ABBREV].new_size)
15139 < multi_abbrev_off
15140 || (unsigned int) (multi_str_off
15141 + (max_strp_off ? max_strp_off
15142 : debug_sections[DEBUG_ABBREV].size))
15143 < multi_str_off
15144 || (unsigned int) (multi_macro_off
15145 + debug_sections[DEBUG_MACRO].new_size)
15146 < multi_macro_off)
15147 {
15148 error (0, 0, "Multifile temporary files too large");
15149 multifile = NULL;
15150 ret = 1;
15151 }
15152 else
15153 {
15154 const char *mfile;
15155 write_abbrev ();
15156 write_info (NULL);
15157 /* Any error in this is fatal for multifile handling of further
15158 files. */
15159 mfile = multifile;
15160 multifile = NULL;
15161 if (write (multi_abbrev_fd, debug_sections[DEBUG_ABBREV].new_data,
15162 debug_sections[DEBUG_ABBREV].new_size)
15163 != (ssize_t) debug_sections[DEBUG_ABBREV].new_size
15164 || write (multi_info_fd, debug_sections[DEBUG_INFO].new_data,
15165 debug_sections[DEBUG_INFO].new_size)
15166 != (ssize_t) debug_sections[DEBUG_INFO].new_size
15167 || write (multi_str_fd, debug_sections[DEBUG_STR].data,
15168 debug_sections[DEBUG_STR].size)
15169 != (ssize_t) debug_sections[DEBUG_STR].size
15170 || (debug_sections[DEBUG_MACRO].new_data
15171 && write (multi_macro_fd,
15172 debug_sections[DEBUG_MACRO].new_data,
15173 debug_sections[DEBUG_MACRO].new_size)
15174 != (ssize_t) debug_sections[DEBUG_MACRO].new_size)
15175 || (strp_htab != NULL && write_multifile_strp ())
15176 || write_multifile_line ())
15177 {
15178 error (0, 0, "Error writing multi-file temporary files");
15179 ret = 1;
15180 }
15181 else
15182 {
15183 multi_info_off += debug_sections[DEBUG_INFO].new_size;
15184 multi_abbrev_off += debug_sections[DEBUG_ABBREV].new_size;
15185 multi_str_off += max_strp_off ? max_strp_off
15186 : debug_sections[DEBUG_STR].size;
15187 multi_macro_off += debug_sections[DEBUG_MACRO].new_size;
15188 multifile = mfile;
15189 }
15190 }
15191 }
15192 multifile_mode = 0;
15193 for (i = 0; i < SAVED_SECTIONS; i++)
15194 {
15195 free (debug_sections[i].new_data);
15196 debug_sections[i].new_data = saved_new_data[i];
15197 debug_sections[i].new_size = saved_new_size[i];
15198 saved_new_data[i] = NULL;
15199 }
15200 return ret;
15201 }
15202
15203 /* During fi_multifile phase, see what DIEs in a partial unit
15204 contain no children worth keeping where all real DIEs have
15205 dups in the shared .debug_info section and what remains is
15206 just the DW_TAG_partial_unit, a single DW_TAG_imported_unit
15207 and perhaps some empty named namespaces. Then all the
15208 references to that partial unit can be replaced by references
15209 to the shared partial unit DW_TAG_import_unit has been importing. */
15210 static bool
remove_empty_pu(dw_die_ref die)15211 remove_empty_pu (dw_die_ref die)
15212 {
15213 dw_die_ref child = die->die_child, dup = NULL;
15214 if (!die->die_named_namespace)
15215 {
15216 if (die->die_tag != DW_TAG_partial_unit
15217 || child == NULL
15218 || child->die_tag != DW_TAG_imported_unit
15219 || child->die_offset != -1U)
15220 return false;
15221 if (die->die_abbrev->nattr > 2)
15222 return false;
15223 if (die->die_abbrev->nattr
15224 && die->die_abbrev->attr[0].attr != DW_AT_stmt_list)
15225 return false;
15226 if (die->die_abbrev->nattr == 2
15227 && die->die_abbrev->attr[1].attr != DW_AT_comp_dir)
15228 return false;
15229 dup = child->die_nextdup;
15230 child = child->die_sib;
15231 }
15232 else
15233 {
15234 if (die->die_abbrev->nattr > 2)
15235 return false;
15236 if (die->die_abbrev->nattr
15237 && die->die_abbrev->attr[0].attr != DW_AT_name)
15238 return false;
15239 if (die->die_abbrev->nattr == 2
15240 && die->die_abbrev->attr[1].attr != DW_AT_sibling)
15241 return false;
15242 }
15243 for (; child; child = child->die_sib)
15244 if (!child->die_named_namespace)
15245 {
15246 if (!child->die_remove)
15247 /* Signal that DIE can't be removed, but
15248 perhaps we could still remove_empty_pu
15249 some named namespaces that are children of DIE. */
15250 dup = die;
15251 if (dup == NULL && die->die_named_namespace)
15252 dup = child->die_dup->die_parent;
15253 }
15254 else if (!remove_empty_pu (child))
15255 return false;
15256 else if (dup == NULL && die->die_named_namespace)
15257 dup = child->die_dup->die_parent;
15258 if (dup == NULL || dup == die)
15259 return false;
15260 die->die_remove = 1;
15261 assert (dup->die_tag == die->die_tag);
15262 die->die_dup = dup;
15263 return true;
15264 }
15265
15266 /* Call remove_empty_pu on all partial units. */
15267 static int
remove_empty_pus(void)15268 remove_empty_pus (void)
15269 {
15270 dw_cu_ref cu;
15271 for (cu = first_cu; cu; cu = cu->cu_next)
15272 if (cu->cu_kind == CU_NORMAL
15273 && cu->cu_die->die_tag == DW_TAG_partial_unit)
15274 remove_empty_pu (cu->cu_die);
15275 return 0;
15276 }
15277
15278 /* Helper structure for hardlink discovery. */
15279 struct file_result
15280 {
15281 /* -2: Already processed under different name.
15282 -1: Ignore.
15283 0: Processed, changed.
15284 1: Processed, unchanged. */
15285 int res;
15286 dev_t dev;
15287 ino_t ino;
15288 nlink_t nlink;
15289 unsigned int die_count;
15290 };
15291
15292 /* Handle compression of a single file FILE. If OUTFILE is
15293 non-NULL, the result will be stored into that file, otherwise
15294 the result will be written into a temporary file that is renamed
15295 over FILE. */
15296 static int
dwz(const char * file,const char * outfile,struct file_result * res,struct file_result * resa,char ** files)15297 dwz (const char *file, const char *outfile, struct file_result *res,
15298 struct file_result *resa, char **files)
15299 {
15300 DSO *dso;
15301 int ret = 0, fd;
15302 unsigned int i;
15303 struct stat st;
15304
15305 res->res = -1;
15306 fd = open (file, O_RDONLY);
15307 if (fd < 0)
15308 {
15309 error (0, errno, "Failed to open input file %s", file);
15310 return 1;
15311 }
15312 if (fstat (fd, &st) < 0)
15313 {
15314 close (fd);
15315 error (0, errno, "Failed to stat input file %s", file);
15316 return 1;
15317 }
15318
15319 res->res = 1;
15320 res->dev = st.st_dev;
15321 res->ino = st.st_ino;
15322 res->nlink = st.st_nlink;
15323 /* Hardlink handling if requested. */
15324 if (resa != NULL)
15325 {
15326 size_t n;
15327 for (n = 0; &resa[n] != res; n++)
15328 if (resa[n].res >= 0
15329 && resa[n].nlink > 1
15330 && resa[n].dev == st.st_dev
15331 && resa[n].ino == st.st_ino)
15332 break;
15333 if (&resa[n] != res)
15334 {
15335 /* If a hardlink to this has been processed before
15336 and we didn't change it, just assume the same
15337 state. */
15338 if (resa[n].res == 1)
15339 {
15340 if (tracing)
15341 fprintf (stderr, "Skipping hardlink %s to unchanged file\n",
15342 file);
15343 close (fd);
15344 res->res = -2;
15345 return 0;
15346 }
15347 /* If it changed, try to hardlink it again. */
15348 if (resa[n].res == 0)
15349 {
15350 size_t len = strlen (file);
15351 char *filename = alloca (len + sizeof (".#dwz#.XXXXXX"));
15352 int fd2;
15353 if (tracing)
15354 fprintf (stderr, "Updating hardlink %s to changed file\n",
15355 file);
15356 memcpy (filename, file, len);
15357 memcpy (filename + len, ".#dwz#.XXXXXX",
15358 sizeof (".#dwz#.XXXXXX"));
15359 fd2 = mkstemp (filename);
15360 if (fd2 >= 0)
15361 {
15362 close (fd2);
15363 unlink (filename);
15364 if (link (files[n], filename) == 0)
15365 {
15366 if (rename (filename, file) == 0)
15367 {
15368 close (fd);
15369 res->res = -2;
15370 return 0;
15371 }
15372 unlink (filename);
15373 }
15374 }
15375 }
15376 }
15377 }
15378
15379 if (tracing)
15380 {
15381 fprintf (stderr, "Compressing %s", file);
15382 if (multifile_mode == 0)
15383 ;
15384 else if (low_mem)
15385 fprintf (stderr, " in low-mem mode");
15386 else if (fi_multifile)
15387 fprintf (stderr, " in finalize-multifile mode");
15388 else
15389 abort ();
15390 fprintf (stderr, "\n");
15391 }
15392
15393 dso = fdopen_dso (fd, file);
15394 if (dso == NULL)
15395 return 1;
15396
15397 obstack_alloc_failed_handler = dwz_oom;
15398 if (setjmp (oom_buf))
15399 {
15400 error (0, ENOMEM, "%s: Could not allocate memory", dso->filename);
15401
15402 cleanup ();
15403 ret = 1;
15404 }
15405 else
15406 {
15407 obstack_init (&ob);
15408 obstack_init (&ob2);
15409
15410 unsigned int *die_count = multifile ? &res->die_count : NULL;
15411 ret = read_dwarf (dso, quiet && outfile == NULL, die_count);
15412 if (ret)
15413 cleanup ();
15414 else if (partition_dups ()
15415 || create_import_tree ()
15416 || (unlikely (fi_multifile)
15417 && (remove_empty_pus ()
15418 || read_macro (dso)))
15419 || read_debug_info (dso, DEBUG_TYPES, NULL)
15420 #if DEVEL
15421 || clear_p2_field ()
15422 #endif
15423 || compute_abbrevs (dso)
15424 || (unlikely (fi_multifile) && (finalize_strp (false), 0)))
15425 {
15426 cleanup ();
15427 ret = 1;
15428 }
15429 else if (!(ignore_size || force_p)
15430 && ((debug_sections[DEBUG_INFO].new_size
15431 + debug_sections[DEBUG_ABBREV].new_size
15432 + debug_sections[DEBUG_STR].new_size
15433 + debug_sections[DEBUG_MACRO].new_size
15434 + debug_sections[DEBUG_TYPES].new_size)
15435 >= (debug_sections[DEBUG_INFO].size
15436 + debug_sections[DEBUG_ABBREV].size
15437 + debug_sections[DEBUG_STR].size
15438 + debug_sections[DEBUG_MACRO].size
15439 + debug_sections[DEBUG_TYPES].size)))
15440 {
15441 if (!quiet || outfile != NULL)
15442 error (0, 0, "%s: DWARF compression not beneficial "
15443 "- old size %ld new size %ld", dso->filename,
15444 (unsigned long) (debug_sections[DEBUG_INFO].size
15445 + debug_sections[DEBUG_ABBREV].size
15446 + debug_sections[DEBUG_STR].size
15447 + debug_sections[DEBUG_MACRO].size
15448 + debug_sections[DEBUG_TYPES].size),
15449 (unsigned long) (debug_sections[DEBUG_INFO].new_size
15450 + debug_sections[DEBUG_ABBREV].new_size
15451 + debug_sections[DEBUG_STR].new_size
15452 + debug_sections[DEBUG_MACRO].new_size
15453 + debug_sections[DEBUG_TYPES].new_size));
15454
15455 if (multifile && !fi_multifile && !low_mem)
15456 write_multifile (dso);
15457
15458 cleanup ();
15459 if (outfile != NULL)
15460 ret = 1;
15461 }
15462 else if (write_aranges (dso))
15463 {
15464 failure:
15465 cleanup ();
15466 ret = 1;
15467 }
15468 else
15469 {
15470 if (unlikely (fi_multifile))
15471 {
15472 size_t len;
15473 const char *name = multifile_name;
15474 enum debug_section_kind sec_kind;
15475 unsigned char *ptr;
15476 if (multifile_name == NULL)
15477 {
15478 if (!multifile_relative)
15479 name = multifile;
15480 else
15481 {
15482 char *p1 = realpath (file, NULL);
15483 char *p2 = realpath (multifile, NULL);
15484 char *p3, *p4, *p5, *p6;
15485 unsigned int dotdot = 0;
15486 if (p1 == NULL || p2 == NULL)
15487 {
15488 if (p1)
15489 free (p1);
15490 else if (p2)
15491 free (p2);
15492 error (0, 0, "Could not compute relative multifile "
15493 "pathname from %s to %s",
15494 file, multifile);
15495 goto failure;
15496 }
15497 p3 = p1;
15498 p4 = p2;
15499 do
15500 {
15501 p5 = strchr (p3, '/');
15502 p6 = strchr (p4, '/');
15503 if (p5 == NULL
15504 || p6 == NULL
15505 || p5 - p3 != p6 - p4
15506 || memcmp (p3, p4, p5 - p3) != 0)
15507 break;
15508 p3 = p5 + 1;
15509 p4 = p6 + 1;
15510 }
15511 while (1);
15512 while (p5 != NULL)
15513 {
15514 dotdot++;
15515 p5 = strchr (p5 + 1, '/');
15516 }
15517 len = strlen (p4);
15518 p3 = (char *) malloc (dotdot * 3 + len + 1);
15519 if (p3 == NULL)
15520 dwz_oom ();
15521 p5 = p3;
15522 while (dotdot)
15523 {
15524 memcpy (p5, "../", 3);
15525 p5 += 3;
15526 dotdot--;
15527 }
15528 memcpy (p5, p4, len + 1);
15529 free (p1);
15530 free (p2);
15531 name = p3;
15532 }
15533 }
15534 len = strlen (name) + 1;
15535 sec_kind = dwarf_5 ? DEBUG_SUP : GNU_DEBUGALTLINK;
15536 debug_sections[sec_kind].new_size
15537 = len + 0x14 + (dwarf_5 ? 4 : 0);
15538 debug_sections[sec_kind].new_data
15539 = malloc (debug_sections[sec_kind].new_size);
15540 if (debug_sections[sec_kind].new_data == NULL)
15541 dwz_oom ();
15542 ptr = debug_sections[sec_kind].new_data;
15543 if (dwarf_5)
15544 {
15545 write_16 (ptr, 5);
15546 write_8 (ptr, 0);
15547 }
15548 memcpy (ptr, name, len);
15549 ptr += len;
15550 if (dwarf_5)
15551 write_uleb128 (ptr, 0x14);
15552 memcpy (ptr, multifile_sha1, 0x14);
15553 if (name != multifile_name && name != multifile)
15554 free ((void *) name);
15555 write_macro ();
15556 }
15557 write_abbrev ();
15558 write_info (die_count);
15559 if (unlikely (progress_p))
15560 {
15561 report_progress ();
15562 fprintf (stderr, "write_loc\n");
15563 }
15564 write_loc ();
15565 if (unlikely (progress_p))
15566 {
15567 report_progress ();
15568 fprintf (stderr, "write_loclists\n");
15569 }
15570 write_loclists ();
15571 if (unlikely (progress_p))
15572 {
15573 report_progress ();
15574 fprintf (stderr, "write_types\n");
15575 }
15576 write_types ();
15577 if (unlikely (progress_p))
15578 {
15579 report_progress ();
15580 fprintf (stderr, "write_gdb_index\n");
15581 }
15582 write_gdb_index ();
15583 /* These sections are optional and it is unclear
15584 how to adjust them. Just remove them. */
15585 debug_sections[DEBUG_PUBNAMES].new_data = NULL;
15586 debug_sections[DEBUG_PUBNAMES].new_size = 0;
15587 debug_sections[DEBUG_PUBTYPES].new_data = NULL;
15588 debug_sections[DEBUG_PUBTYPES].new_size = 0;
15589 debug_sections[DEBUG_GNU_PUBNAMES].new_data = NULL;
15590 debug_sections[DEBUG_GNU_PUBNAMES].new_size = 0;
15591 debug_sections[DEBUG_GNU_PUBTYPES].new_data = NULL;
15592 debug_sections[DEBUG_GNU_PUBTYPES].new_size = 0;
15593
15594 if (multifile && !fi_multifile && !low_mem)
15595 write_multifile (dso);
15596
15597 bool save_to_temp = save_temps && multifile && multifile_mode == 0;
15598 cleanup ();
15599
15600 if (write_dso (dso, outfile, &st, save_to_temp))
15601 ret = 1;
15602 else
15603 res->res = 0;
15604
15605 if (unlikely (progress_p))
15606 report_progress ();
15607 }
15608 }
15609
15610 for (i = 0; debug_sections[i].name; ++i)
15611 {
15612 debug_sections[i].data = NULL;
15613 debug_sections[i].size = 0;
15614 free (debug_sections[i].new_data);
15615 debug_sections[i].new_data = NULL;
15616 debug_sections[i].new_size = 0;
15617 debug_sections[i].sec = 0;
15618 }
15619
15620 if (elf_end (dso->elf) < 0)
15621 {
15622 error (0, 0, "elf_end failed: %s", elf_errmsg (elf_errno ()));
15623 ret = 1;
15624 }
15625 close (fd);
15626
15627 free (dso);
15628 if (ret == 3)
15629 {
15630 ret = (outfile != NULL) ? 1 : 0;
15631 res->res = -1;
15632 }
15633 return ret;
15634 }
15635
15636 /* In order to free all malloced memory at the end of optimize_multifile,
15637 communicate .debug_str tail optimized offset list from optimize_multifile
15638 to read_multifile using an mmapped chunk of memory pointed by this
15639 variable. */
15640 static unsigned int *strp_tail_off_list;
15641
15642 /* Process temporary .debug_* files, see what can be beneficially shared
15643 and write a new ET_REL file, containing the shared .debug_* sections. */
15644 static int
optimize_multifile(unsigned int * die_count)15645 optimize_multifile (unsigned int *die_count)
15646 {
15647 DSO dsobuf, *dso;
15648 int fd = -1;
15649 volatile int vfd = -1;
15650 unsigned int i;
15651 Elf *elf = NULL;
15652 Elf *volatile velf = NULL;
15653 GElf_Shdr shdr;
15654 Elf_Scn *scn;
15655 Elf_Data *data;
15656 char *e_ident;
15657 const char shstrtab_gnu[]
15658 = "\0.shstrtab\0.note.gnu.build-id\0.gdb_index\0"
15659 ".debug_info\0.debug_abbrev\0.debug_line\0.debug_str\0.debug_macro";
15660 const char shstrtab_dwarf5[]
15661 = "\0.shstrtab\0.gdb_index\0"
15662 ".debug_info\0.debug_abbrev\0.debug_line\0.debug_str\0.debug_macro\0"
15663 ".debug_sup";
15664 const char *shstrtab;
15665 size_t shstrtab_len;
15666 const char *p;
15667 unsigned char note[0x24], *np, *supp;
15668 struct sha1_ctx ctx;
15669
15670 if (multi_ehdr.e_ident[0] == '\0'
15671 || multi_ptr_size == 0
15672 || multi_endian == 0)
15673 return -1;
15674
15675 if (multi_line_off == 0)
15676 {
15677 init_endian (multi_endian);
15678 if (write_multifile_line ())
15679 {
15680 error (0, 0, "Error writing multi-file temporary files");
15681 return -1;
15682 }
15683 }
15684
15685 if (unlikely (progress_p))
15686 {
15687 report_progress ();
15688 fprintf (stderr, "optimize_multifile\n");
15689 }
15690
15691 if (dwarf_5)
15692 {
15693 shstrtab = shstrtab_dwarf5;
15694 shstrtab_len = sizeof shstrtab_dwarf5;
15695 }
15696 else
15697 {
15698 shstrtab = shstrtab_gnu;
15699 shstrtab_len = sizeof shstrtab_gnu;
15700 }
15701 debug_sections[DEBUG_INFO].size = multi_info_off;
15702 debug_sections[DEBUG_INFO].data
15703 = (multi_info_off
15704 ? mmap (NULL, multi_info_off, PROT_READ, MAP_PRIVATE, multi_info_fd, 0)
15705 : NULL);
15706 debug_sections[DEBUG_ABBREV].size = multi_abbrev_off;
15707 debug_sections[DEBUG_ABBREV].data
15708 = (multi_abbrev_off
15709 ? mmap (NULL, multi_abbrev_off, PROT_READ, MAP_PRIVATE,
15710 multi_abbrev_fd, 0)
15711 : NULL);
15712 debug_sections[DEBUG_LINE].size = multi_line_off;
15713 debug_sections[DEBUG_LINE].data
15714 = mmap (NULL, multi_line_off, PROT_READ, MAP_PRIVATE, multi_line_fd, 0);
15715 debug_sections[DEBUG_STR].size = multi_str_off;
15716 debug_sections[DEBUG_STR].data
15717 = multi_str_off
15718 ? mmap (NULL, multi_str_off, PROT_READ, MAP_PRIVATE, multi_str_fd, 0)
15719 : NULL;
15720 debug_sections[DEBUG_MACRO].size = multi_macro_off;
15721 debug_sections[DEBUG_MACRO].data
15722 = multi_macro_off
15723 ? mmap (NULL, multi_macro_off, PROT_READ, MAP_PRIVATE, multi_macro_fd, 0)
15724 : NULL;
15725 if (debug_sections[DEBUG_INFO].data == MAP_FAILED
15726 || debug_sections[DEBUG_ABBREV].data == MAP_FAILED
15727 || debug_sections[DEBUG_LINE].data == MAP_FAILED
15728 || debug_sections[DEBUG_STR].data == MAP_FAILED
15729 || debug_sections[DEBUG_MACRO].data == MAP_FAILED)
15730 {
15731 error (0, 0, "Error mmapping multi-file temporary files");
15732 fail:
15733 cleanup ();
15734 if (velf)
15735 elf_end (velf);
15736 if (vfd != -1)
15737 {
15738 unlink (multifile);
15739 close (vfd);
15740 }
15741 if (debug_sections[DEBUG_INFO].data != MAP_FAILED)
15742 munmap (debug_sections[DEBUG_INFO].data,
15743 debug_sections[DEBUG_INFO].size);
15744 if (debug_sections[DEBUG_ABBREV].data != MAP_FAILED)
15745 munmap (debug_sections[DEBUG_ABBREV].data,
15746 debug_sections[DEBUG_ABBREV].size);
15747 if (debug_sections[DEBUG_LINE].data != MAP_FAILED)
15748 munmap (debug_sections[DEBUG_LINE].data,
15749 debug_sections[DEBUG_LINE].size);
15750 if (debug_sections[DEBUG_STR].data != MAP_FAILED
15751 && debug_sections[DEBUG_STR].data != NULL)
15752 munmap (debug_sections[DEBUG_STR].data,
15753 debug_sections[DEBUG_STR].size);
15754 if (debug_sections[DEBUG_MACRO].data != MAP_FAILED
15755 && debug_sections[DEBUG_MACRO].data != NULL)
15756 munmap (debug_sections[DEBUG_MACRO].data,
15757 debug_sections[DEBUG_MACRO].size);
15758 return -1;
15759 }
15760
15761 init_endian (multi_endian);
15762 ptr_size = multi_ptr_size;
15763 memset (&dsobuf, '\0', sizeof (dsobuf));
15764 dso = &dsobuf;
15765 dso->filename = multifile;
15766 if (tracing)
15767 fprintf (stderr, "Optimize-multifile\n");
15768 multifile_mode = MULTIFILE_MODE_OP;
15769
15770 obstack_alloc_failed_handler = dwz_oom;
15771 if (unoptimized_multifile)
15772 {
15773 for (i = 0; i < SAVED_SECTIONS; i++)
15774 {
15775 debug_sections[i].new_data = debug_sections[i].data;
15776 debug_sections[i].new_size = debug_sections[i].size;
15777 }
15778 }
15779 else if (setjmp (oom_buf))
15780 {
15781 error (0, ENOMEM, "%s: Could not allocate memory", dso->filename);
15782 goto fail;
15783 }
15784 else
15785 {
15786 dw_cu_ref *cup;
15787 unsigned char *p, *q;
15788 unsigned int strp_count;
15789
15790 obstack_init (&ob);
15791 obstack_init (&ob2);
15792
15793 if (read_debug_info (dso, DEBUG_INFO, NULL)
15794 || partition_dups ())
15795 goto fail;
15796
15797 #if DEVEL
15798 clear_p2_field ();
15799 #endif
15800
15801 for (cup = &first_cu; *cup && (*cup)->cu_kind == CU_PU;
15802 cup = &(*cup)->cu_next)
15803 ;
15804
15805 *cup = NULL;
15806
15807 strp_count = debug_sections[DEBUG_STR].size / 64;
15808 if (strp_count < 64)
15809 strp_count = 64;
15810 strp_htab = htab_try_create (strp_count,
15811 strp_hash2, strp_eq2, NULL);
15812 if (strp_htab == NULL)
15813 dwz_oom ();
15814
15815 for (p = debug_sections[DEBUG_STR].data;
15816 p < debug_sections[DEBUG_STR].data + debug_sections[DEBUG_STR].size;
15817 p = q + 1)
15818 {
15819 void **slot;
15820 struct strp_entry se;
15821 hashval_t hash;
15822
15823 q = (unsigned char *) strchr ((char *) p, '\0');
15824 hash = iterative_hash (p, q - p, 0);
15825 se.off = p - debug_sections[DEBUG_STR].data;
15826 se.new_off = hash & ~1U;
15827 slot = htab_find_slot_with_hash (strp_htab, &se, se.new_off, INSERT);
15828 if (slot == NULL)
15829 dwz_oom ();
15830 if (*slot == NULL)
15831 {
15832 struct strp_entry *s = pool_alloc (strp_entry, sizeof (*s));
15833 *s = se;
15834 *slot = (void *) s;
15835 }
15836 else
15837 ((struct strp_entry *) *slot)->new_off |= 1;
15838 }
15839
15840 if (first_cu != NULL)
15841 {
15842 if (compute_abbrevs (dso))
15843 goto fail;
15844
15845 strp_tail_off_list = finalize_strp (true);
15846
15847 write_abbrev ();
15848 write_info (die_count);
15849 write_gdb_index ();
15850 if (write_multifile_line ())
15851 goto fail;
15852 }
15853 else
15854 strp_tail_off_list = finalize_strp (true);
15855
15856 if (debug_sections[DEBUG_MACRO].data)
15857 handle_macro ();
15858 }
15859
15860 np = note;
15861 write_32 (np, sizeof ("GNU"));
15862 write_32 (np, 0x14);
15863 write_32 (np, NT_GNU_BUILD_ID);
15864
15865 supp = NULL;
15866 if (dwarf_5)
15867 {
15868 debug_sections[DEBUG_SUP].new_size = 0x14 + 5;
15869 debug_sections[DEBUG_SUP].new_data
15870 = malloc (debug_sections[DEBUG_SUP].new_size);
15871 if (debug_sections[DEBUG_SUP].new_data == NULL)
15872 dwz_oom ();
15873 supp = debug_sections[DEBUG_SUP].new_data;
15874 write_16 (supp, 5);
15875 write_8 (supp, 1);
15876 write_8 (supp, 0);
15877 write_uleb128 (supp, 0x14);
15878 }
15879
15880 cleanup ();
15881 fd = open (multifile, O_RDWR | O_CREAT, 0600);
15882 vfd = fd;
15883 if (fd < 0)
15884 {
15885 error (0, errno, "Failed to open multi-file common file %s", multifile);
15886 goto fail;
15887 }
15888
15889 elf = elf_begin (fd, ELF_C_WRITE, NULL);
15890 velf = elf;
15891 if (elf == NULL)
15892 {
15893 error (0, 0, "cannot open ELF file: %s", elf_errmsg (-1));
15894 goto fail;
15895 }
15896
15897 multi_ehdr.e_type = ET_REL;
15898 multi_ehdr.e_entry = 0;
15899 multi_ehdr.e_phoff = 0;
15900 multi_ehdr.e_phnum = 0;
15901 multi_ehdr.e_shoff = multi_ehdr.e_ehsize;
15902 multi_ehdr.e_shnum = 2;
15903 if (!dwarf_5)
15904 {
15905 multi_ehdr.e_shoff += 0x24;
15906 multi_ehdr.e_shnum++;
15907 }
15908 for (i = 0; debug_sections[i].name; i++)
15909 if (debug_sections[i].new_size)
15910 {
15911 multi_ehdr.e_shoff += debug_sections[i].new_size;
15912 multi_ehdr.e_shnum++;
15913 }
15914 multi_ehdr.e_shstrndx = multi_ehdr.e_shnum - 1;
15915
15916 /* Some gelf_newehdr implementations don't return the resulting
15917 ElfNN_Ehdr, so we have to do it the hard way instead of:
15918 e_ident = (char *) gelf_newehdr (elf, gelf_getclass (dso->elf)); */
15919 switch (multi_ehdr.e_ident[EI_CLASS])
15920 {
15921 case ELFCLASS32:
15922 e_ident = (char *) elf32_newehdr (elf);
15923 multi_ehdr.e_shoff = (multi_ehdr.e_shoff + 3) & -4;
15924 break;
15925 case ELFCLASS64:
15926 e_ident = (char *) elf64_newehdr (elf);
15927 multi_ehdr.e_shoff = (multi_ehdr.e_shoff + 7) & -8;
15928 break;
15929 default:
15930 e_ident = NULL;
15931 break;
15932 }
15933
15934 if (e_ident == NULL
15935 /* This is here just for the gelfx wrapper, so that gelf_update_ehdr
15936 already has the correct ELF class. */
15937 || memcpy (e_ident, multi_ehdr.e_ident, EI_NIDENT) == NULL
15938 || gelf_update_ehdr (elf, &multi_ehdr) == 0)
15939 {
15940 error (0, 0, "Could not create new ELF headers");
15941 goto fail;
15942 }
15943 elf_flagelf (elf, ELF_C_SET, ELF_F_LAYOUT);
15944
15945 sha1_init_ctx (&ctx);
15946 for (i = 0; debug_sections[i].name; i++)
15947 {
15948 if (debug_sections[i].new_size == 0)
15949 continue;
15950 sha1_process_bytes (debug_sections[i].new_data,
15951 debug_sections[i].new_size, &ctx);
15952 }
15953 sha1_finish_ctx (&ctx, multifile_sha1);
15954
15955 memcpy (np, "GNU", sizeof ("GNU"));
15956 memcpy (np + 4, multifile_sha1, 0x14);
15957
15958 if (dwarf_5)
15959 memcpy (supp, multifile_sha1, 0x14);
15960
15961 memset (&shdr, '\0', sizeof (shdr));
15962 shdr.sh_offset = multi_ehdr.e_ehsize;
15963 if (!dwarf_5)
15964 {
15965 shdr.sh_type = SHT_NOTE;
15966 shdr.sh_addralign = 4;
15967 shdr.sh_size = 0x24;
15968 scn = elf_newscn (elf);
15969 elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY);
15970 shdr.sh_name = (strchr (shstrtab + 1, '\0') + 1) - shstrtab;
15971 gelf_update_shdr (scn, &shdr);
15972 data = elf_newdata (scn);
15973 data->d_buf = (char *) note;
15974 data->d_type = ELF_T_BYTE;
15975 data->d_version = EV_CURRENT;
15976 data->d_size = shdr.sh_size;
15977 data->d_off = 0;
15978 data->d_align = 1;
15979 }
15980
15981 shdr.sh_type = SHT_PROGBITS;
15982 shdr.sh_offset += shdr.sh_size;
15983 shdr.sh_addralign = 1;
15984 for (i = 0; debug_sections[i].name; i++)
15985 {
15986 if (debug_sections[i].new_size == 0)
15987 continue;
15988 scn = elf_newscn (elf);
15989 elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY);
15990 for (p = shstrtab + 1; p < shstrtab + shstrtab_len;
15991 p = strchr (p, '\0') + 1)
15992 if (strcmp (p, debug_sections[i].name) == 0)
15993 {
15994 shdr.sh_name = p - shstrtab;
15995 break;
15996 }
15997 shdr.sh_size = debug_sections[i].new_size;
15998 if (i == DEBUG_STR)
15999 {
16000 shdr.sh_flags = SHF_MERGE | SHF_STRINGS;
16001 shdr.sh_entsize = 1;
16002 }
16003 else
16004 {
16005 shdr.sh_flags = 0;
16006 shdr.sh_entsize = 0;
16007 }
16008 gelf_update_shdr (scn, &shdr);
16009 data = elf_newdata (scn);
16010 data->d_buf = debug_sections[i].new_data;
16011 data->d_type = ELF_T_BYTE;
16012 data->d_version = EV_CURRENT;
16013 data->d_size = shdr.sh_size;
16014 data->d_off = 0;
16015 data->d_align = 1;
16016 shdr.sh_offset += shdr.sh_size;
16017 }
16018 scn = elf_newscn (elf);
16019 elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY);
16020 shdr.sh_name = 1;
16021 shdr.sh_offset = multi_ehdr.e_shoff
16022 + multi_ehdr.e_shnum * multi_ehdr.e_shentsize;
16023 shdr.sh_size = shstrtab_len;
16024 shdr.sh_type = SHT_STRTAB;
16025 shdr.sh_flags = 0;
16026 shdr.sh_entsize = 0;
16027 gelf_update_shdr (scn, &shdr);
16028 data = elf_newdata (scn);
16029 data->d_buf = (char *) shstrtab;
16030 data->d_type = ELF_T_BYTE;
16031 data->d_version = EV_CURRENT;
16032 data->d_size = shdr.sh_size;
16033 data->d_off = 0;
16034 data->d_align = 1;
16035
16036 if (elf_update (elf, ELF_C_WRITE) == -1)
16037 {
16038 error (0, 0, "%s: elf_update failed", multifile);
16039 goto fail;
16040 }
16041
16042 if (elf_end (elf) < 0)
16043 {
16044 error (0, 0, "elf_end failed: %s", elf_errmsg (elf_errno ()));
16045 goto fail;
16046 }
16047
16048 fchmod (fd, 0644);
16049
16050 if (dwarf_5)
16051 {
16052 free (debug_sections[DEBUG_SUP].new_data);
16053 debug_sections[DEBUG_SUP].new_data = NULL;
16054 }
16055 munmap (debug_sections[DEBUG_INFO].data, debug_sections[DEBUG_INFO].size);
16056 munmap (debug_sections[DEBUG_ABBREV].data,
16057 debug_sections[DEBUG_ABBREV].size);
16058 munmap (debug_sections[DEBUG_LINE].data, debug_sections[DEBUG_LINE].size);
16059 if (debug_sections[DEBUG_STR].data)
16060 munmap (debug_sections[DEBUG_STR].data, debug_sections[DEBUG_STR].size);
16061 if (debug_sections[DEBUG_MACRO].data)
16062 munmap (debug_sections[DEBUG_MACRO].data,
16063 debug_sections[DEBUG_MACRO].size);
16064
16065 for (i = 0; debug_sections[i].name; ++i)
16066 {
16067 debug_sections[i].data = NULL;
16068 debug_sections[i].size = 0;
16069 if (!unoptimized_multifile)
16070 free (debug_sections[i].new_data);
16071 debug_sections[i].new_data = NULL;
16072 debug_sections[i].new_size = 0;
16073 debug_sections[i].sec = 0;
16074 }
16075
16076 return fd;
16077 }
16078
16079 /* Parse the .debug_* sections from shared ET_REL file written
16080 by optimize_multifile into data structures for fi_multifile
16081 phase. */
16082 static DSO *
read_multifile(int fd,unsigned int die_count)16083 read_multifile (int fd, unsigned int die_count)
16084 {
16085 DSO *dso, *volatile ret;
16086 unsigned int i;
16087
16088 if (unlikely (progress_p))
16089 {
16090 report_progress ();
16091 fprintf (stderr, "read_multifile\n");
16092 }
16093
16094 if (tracing)
16095 fprintf (stderr, "Read-multifile\n");
16096
16097 multifile_mode = MULTIFILE_MODE_RD;
16098 dso = fdopen_dso (fd, multifile);
16099 if (dso == NULL)
16100 {
16101 multifile_mode = 0;
16102 return NULL;
16103 }
16104
16105 ret = dso;
16106 obstack_alloc_failed_handler = dwz_oom;
16107 if (setjmp (oom_buf))
16108 {
16109 error (0, ENOMEM, "%s: Could not allocate memory", dso->filename);
16110
16111 fail:
16112 elf_end (dso->elf);
16113 close (fd);
16114 free (dso);
16115 ret = NULL;
16116 alt_off_htab = NULL;
16117 }
16118 else
16119 {
16120 obstack_init (&ob);
16121 obstack_init (&ob2);
16122
16123 if (read_dwarf (dso, false, &die_count))
16124 goto fail;
16125
16126 if (debug_sections[DEBUG_STR].size)
16127 {
16128 unsigned char *p, *q;
16129 unsigned int strp_count = debug_sections[DEBUG_STR].size / 64;
16130 void **slot;
16131 unsigned int *pi;
16132
16133 if (strp_count < 100)
16134 strp_count = 100;
16135 strp_htab = htab_try_create (strp_count, strp_hash3, strp_eq3, NULL);
16136 if (strp_htab == NULL)
16137 dwz_oom ();
16138 for (p = debug_sections[DEBUG_STR].data;
16139 p < debug_sections[DEBUG_STR].data
16140 + debug_sections[DEBUG_STR].size; p = q)
16141 {
16142 q = (unsigned char *) strchr ((char *) p, '\0') + 1;
16143 slot = htab_find_slot_with_hash (strp_htab, p,
16144 iterative_hash (p, q - p - 1,
16145 0), INSERT);
16146 if (slot == NULL)
16147 dwz_oom ();
16148 assert (*slot == NULL);
16149 *slot = (void *) p;
16150 }
16151 if (strp_tail_off_list)
16152 {
16153 for (pi = strp_tail_off_list; *pi; pi++)
16154 {
16155 p = debug_sections[DEBUG_STR].data + *pi;
16156 q = (unsigned char *) strchr ((char *) p, '\0');
16157 slot = htab_find_slot_with_hash (strp_htab, p,
16158 iterative_hash (p, q - p,
16159 0), INSERT);
16160 if (slot == NULL)
16161 dwz_oom ();
16162 assert (*slot == NULL);
16163 *slot = (void *) p;
16164 }
16165 pi++;
16166 munmap (strp_tail_off_list,
16167 (char *) pi - (char *) strp_tail_off_list);
16168 }
16169 }
16170
16171 if (debug_sections[DEBUG_MACRO].data)
16172 handle_macro ();
16173
16174 alt_strp_htab = strp_htab;
16175 strp_htab = NULL;
16176 alt_off_htab = off_htab;
16177 off_htab = NULL;
16178 alt_dup_htab = dup_htab;
16179 dup_htab = NULL;
16180 alt_macro_htab = macro_htab;
16181 macro_htab = NULL;
16182 alt_first_cu = first_cu;
16183 alt_pool = pool;
16184 pool = NULL;
16185 pool_next = NULL;
16186 pool_limit = NULL;
16187 alt_ob = ob;
16188 alt_ob2 = ob2;
16189 memset (&ob, '\0', sizeof (ob));
16190 memset (&ob2, '\0', sizeof (ob2));
16191 for (i = 0; i < SAVED_SECTIONS; i++)
16192 {
16193 alt_data[i] = debug_sections[i].data;
16194 alt_size[i] = debug_sections[i].size;
16195 }
16196 }
16197
16198 cleanup ();
16199
16200 for (i = 0; debug_sections[i].name; ++i)
16201 {
16202 debug_sections[i].data = NULL;
16203 debug_sections[i].size = 0;
16204 debug_sections[i].new_data = NULL;
16205 debug_sections[i].new_size = 0;
16206 debug_sections[i].sec = 0;
16207 }
16208
16209 return ret;
16210 }
16211
16212 /* Clear all die_nextdup fields among toplevel children
16213 of DIE. */
16214 static void
alt_clear_dups(dw_die_ref die)16215 alt_clear_dups (dw_die_ref die)
16216 {
16217 dw_die_ref child;
16218 assert (die->die_dup == NULL);
16219 die->die_nextdup = NULL;
16220 for (child = die->die_child; child; child = child->die_sib)
16221 {
16222 assert (child->die_dup == NULL);
16223 child->die_nextdup = NULL;
16224 if (child->die_named_namespace)
16225 alt_clear_dups (child);
16226 }
16227 }
16228
16229 /* Create a temporary file using NAME. Return the corresponding file
16230 descriptor if successful, otherwise return -1. */
16231 static int
make_temp_file(const char * name)16232 make_temp_file (const char *name)
16233 {
16234 const char *tmpdir = "/tmp/";
16235 const char *template_suffix = ".XXXXXX";
16236 int fd;
16237 size_t buf_len, offset, name_len;
16238 char *buf;
16239
16240 if (save_temps)
16241 {
16242 FILE *f = fopen (name, "w+");
16243 if (f == NULL)
16244 fd = -1;
16245 else
16246 fd = fileno (f);
16247 return fd;
16248 }
16249
16250 name_len = strlen (name);
16251 buf_len = (strlen (tmpdir)
16252 + name_len
16253 + strlen (template_suffix)
16254 + 1);
16255 if (buf_len < name_len)
16256 return -1;
16257 buf = (char *)malloc (buf_len);
16258 if (buf == NULL)
16259 return -1;
16260 offset = 0;
16261
16262 strcpy (&buf[offset], tmpdir);
16263 offset += strlen (tmpdir);
16264
16265 strcpy (&buf[offset], name);
16266 offset += name_len;
16267
16268 strcpy (&buf[offset], template_suffix);
16269 offset += strlen (template_suffix);
16270
16271 assert (offset == buf_len - 1);
16272 assert (buf[offset] == '\0');
16273
16274 fd = mkstemp (buf);
16275 if (fd == -1)
16276 goto done;
16277
16278 /* Unlink the filename, such that the file is disposed of once the file
16279 descriptor is closed. */
16280 unlink (buf);
16281
16282 done:
16283 free (buf);
16284 return fd;
16285 }
16286
16287 int die_count_method_parsed;
16288 int deduplication_mode_parsed;
16289
16290 /* Options for getopt_long. */
16291 static struct option dwz_options[] =
16292 {
16293 { "help", no_argument, 0, '?' },
16294 { "output", required_argument, 0, 'o' },
16295 { "multifile", required_argument, 0, 'm' },
16296 { "quiet", no_argument, 0, 'q' },
16297 { "hardlink", no_argument, 0, 'h' },
16298 { "low-mem-die-limit", required_argument, 0, 'l' },
16299 { "max-die-limit", required_argument, 0, 'L' },
16300 { "multifile-name", required_argument, 0, 'M' },
16301 { "relative", no_argument, 0, 'r' },
16302 { "version", no_argument, 0, 'v' },
16303 { "import-optimize",
16304 no_argument, &import_opt_p, 1 },
16305 { "no-import-optimize",
16306 no_argument, &import_opt_p, 0 },
16307 { "dwarf-5", no_argument, 0, '5' },
16308 #if DEVEL
16309 { "devel-trace", no_argument, &tracing, 1 },
16310 { "devel-progress", no_argument, &progress_p, 1 },
16311 { "devel-ignore-size", no_argument, &ignore_size, 1 },
16312 { "devel-ignore-locus",no_argument, &ignore_locus, 1 },
16313 { "devel-force", no_argument, &force_p, 1 },
16314 { "devel-save-temps", no_argument, &save_temps, 1 },
16315 { "devel-dump-checksum",
16316 no_argument, &dump_checksum_p, 1 },
16317 { "devel-dump-dies", no_argument, &dump_dies_p, 1 },
16318 { "devel-dump-dups", no_argument, &dump_dups_p, 1 },
16319 { "devel-dump-pus", no_argument, &dump_pus_p, 1 },
16320 { "devel-unoptimized-multifile",
16321 no_argument, &unoptimized_multifile, 1 },
16322 { "devel-verify-edges",no_argument, &verify_edges_p, 1 },
16323 { "devel-verify-dups", no_argument, &verify_dups_p, 1 },
16324 { "devel-dump-edges", no_argument, &dump_edges_p, 1 },
16325 { "devel-partition-dups-opt",
16326 no_argument, &partition_dups_opt, 1 },
16327 { "devel-die-count-method",
16328 required_argument, &die_count_method_parsed, 1 },
16329 { "devel-stats", no_argument, &stats_p, 1 },
16330 { "devel-deduplication-mode",
16331 required_argument, &deduplication_mode_parsed, 1 },
16332 { "devel-uni-lang",
16333 no_argument, &uni_lang_p, 1 },
16334 { "devel-no-uni-lang",
16335 no_argument, &uni_lang_p, 0 },
16336 { "devel-gen-cu",
16337 no_argument, &gen_cu_p, 1 },
16338 { "devel-no-gen-cu",
16339 no_argument, &gen_cu_p, 0 },
16340 #endif
16341 { "odr", no_argument, &odr, 1 },
16342 { "no-odr", no_argument, &odr, 0 },
16343 { "odr-mode", required_argument, &odr_mode_parsed, 1 },
16344 { NULL, no_argument, 0, 0 }
16345 };
16346
16347 /* Struct describing various usage aspects of a command line option. */
16348 struct option_help
16349 {
16350 const char *short_name;
16351 const char *long_name;
16352 const char *argument;
16353 const char *default_value;
16354 const char *msg;
16355 };
16356
16357 /* Describe common command line options. */
16358 static struct option_help dwz_common_options_help[] =
16359 {
16360 { "q", "quiet", NULL, NULL,
16361 "Silence up the most common messages." },
16362 { "l", "low-mem-die-limit", "<COUNT|none>", "10 million DIEs",
16363 "Handle files larger than this limit using a slower and more memory"
16364 " usage friendly mode and don't optimize those files in multifile mode." },
16365 { "L", "max-die-limit", "<COUNT|none>", "50 million DIEs",
16366 "Don't optimize files larger than this limit." },
16367 { NULL, "odr", NULL, NULL,
16368 NULL },
16369 { NULL, "no-odr", NULL, "Disabled",
16370 "Enable/disable one definition rule optimization." },
16371 { NULL, "odr-mode", "<basic|link>", "link",
16372 "Set aggressiveness level of one definition rule optimization." },
16373 { NULL, "import-optimize", NULL, NULL,
16374 NULL },
16375 { NULL, "no-import-optimize", NULL, "Enabled",
16376 "Enable/disable optimization that reduces the number of"
16377 " DW_TAG_imported_unit DIEs." }
16378 };
16379
16380 /* Describe single-file command line options. */
16381 static struct option_help dwz_single_file_options_help[] =
16382 {
16383 { "o", "output", "OUTFILE", NULL,
16384 "Place the output in OUTFILE." }
16385 };
16386
16387 /* Describe mult-file command line options. */
16388 static struct option_help dwz_multi_file_options_help[] =
16389 {
16390 { "h", "hardlink", NULL, NULL,
16391 "Handle hardlinked files as one file." },
16392 { "m", "multifile", "COMMONFILE", NULL,
16393 "Enable multifile optimization, placing common DIEs in multifile"
16394 " COMMONFILE." },
16395 { "M", "multifile-name", "NAME", NULL,
16396 "Set .gnu_debugaltlink or .debug_sup in files to NAME." },
16397 { "r", "relative", NULL, NULL,
16398 "Set .gnu_debugaltlink in files to relative path from file directory"
16399 " to multifile." },
16400 { "5", "dwarf-5", NULL, NULL,
16401 "Emit DWARF 5 standardized supplementary object files instead of"
16402 " GNU extension .debug_altlink." }
16403 };
16404
16405 /* Describe misc command line options. */
16406 static struct option_help dwz_misc_options_help[] =
16407 {
16408 { "v", "version", NULL, NULL,
16409 "Display dwz version information." },
16410 { "?", "help", NULL, NULL,
16411 "Display this information." }
16412 };
16413
16414 /* Print LEN spaces to STREAM. */
16415 static void
do_indent(FILE * stream,unsigned int len)16416 do_indent (FILE *stream, unsigned int len)
16417 {
16418 unsigned int i;
16419
16420 for (i = 0; i < len; i++)
16421 fprintf (stream, " ");
16422 }
16423
16424 /* Print MSG to STREAM, indenting to INDENT and wrapping at LIMIT.
16425 Assume starting position is at INDENT. */
16426 static void
wrap(FILE * stream,unsigned int indent,unsigned int limit,const char * msg)16427 wrap (FILE *stream, unsigned int indent, unsigned int limit, const char *msg)
16428 {
16429 unsigned int len = indent;
16430 const char *s = msg;
16431 while (true)
16432 {
16433 const char *e = strchr (s, ' ');
16434 unsigned int word_len;
16435 if (e == NULL)
16436 word_len = strlen (s);
16437 else
16438 word_len = e - s;
16439 if (word_len == 0)
16440 return;
16441
16442 if (len + 1 /* space */ + word_len > limit)
16443 {
16444 fprintf (stream, "\n");
16445 do_indent (stream ,indent);
16446 len = indent;
16447 }
16448 else if (len > indent)
16449 {
16450 fprintf (stream, " ");
16451 len += 1;
16452 }
16453
16454 if (e != NULL)
16455 {
16456 const char *i;
16457 for (i = s; i < e; ++i)
16458 fprintf (stream, "%c", *i);
16459 }
16460 else
16461 fprintf (stream, "%s", s);
16462 len += word_len;
16463
16464 if (e == NULL)
16465 break;
16466
16467 s = e + 1;
16468 }
16469 }
16470
16471 /* Print OPTIONS_HELP of length H to STREAM, indenting to help message to
16472 INDENT an wrapping at LIMIT. */
16473 static void
print_options_help(FILE * stream,struct option_help * options_help,unsigned int n,unsigned int indent,unsigned int limit)16474 print_options_help (FILE *stream, struct option_help *options_help, unsigned int n,
16475 unsigned int indent, unsigned int limit)
16476 {
16477 unsigned len;
16478 const char *s;
16479 unsigned int i;
16480
16481 for (i = 0; i < n; ++i)
16482 {
16483 len = 0;
16484
16485 fprintf (stream, " ");
16486 len += 2;
16487
16488 s = options_help[i].short_name;
16489 if (s)
16490 {
16491 fprintf (stream, "-%s", s);
16492 len += 2;
16493 }
16494
16495 s = options_help[i].long_name;
16496 if (len == 4)
16497 {
16498 fprintf (stream, ", ");
16499 len += 2;
16500 }
16501 fprintf (stream, "--%s", s);
16502 len += 2 + strlen (s);
16503
16504 s = options_help[i].argument;
16505 if (s)
16506 {
16507 fprintf (stream, " %s", s);
16508 len += 1 + strlen (s);
16509 }
16510
16511 s = options_help[i].msg;
16512 if (s)
16513 {
16514 if (len > indent)
16515 {
16516 fprintf (stream, "\n");
16517 do_indent (stream, indent);
16518 }
16519 else
16520 do_indent (stream, indent - len);
16521 len = indent;
16522
16523 wrap (stream, indent, limit, s);
16524 }
16525 fprintf (stream, "\n");
16526
16527 s = options_help[i].default_value;
16528 if (s)
16529 {
16530 do_indent (stream, indent);
16531 fprintf (stream, "Default value: %s.\n", s);
16532 }
16533 }
16534 }
16535
16536 /* Print usage and exit. */
16537 static void
usage(const char * progname,int failing)16538 usage (const char *progname, int failing)
16539 {
16540 unsigned int n;
16541 unsigned int indent, limit;
16542 FILE *stream = failing ? stderr : stdout;
16543
16544 fprintf (stream,
16545 ("Usage:\n"
16546 " %s [common options] [-h] [-m COMMONFILE] [-M NAME | -r] [FILES]\n"
16547 " %s [common options] -o OUTFILE FILE\n"
16548 " %s [ -v | -? ]\n"),
16549 progname, progname, progname);
16550
16551 indent = 30;
16552 limit = 80;
16553 fprintf (stream, "Common options:\n");
16554 n = (sizeof (dwz_common_options_help)
16555 / sizeof (dwz_common_options_help[0]));
16556 print_options_help (stream, dwz_common_options_help, n, indent, limit);
16557
16558 fprintf (stream, "Single-file options:\n");
16559 n = (sizeof (dwz_single_file_options_help)
16560 / sizeof (dwz_single_file_options_help[0]));
16561 print_options_help (stream, dwz_single_file_options_help, n, indent, limit);
16562
16563 fprintf (stream, "Multi-file options:\n");
16564 n = (sizeof (dwz_multi_file_options_help)
16565 / sizeof (dwz_multi_file_options_help[0]));
16566 print_options_help (stream, dwz_multi_file_options_help, n, indent, limit);
16567
16568 fprintf (stream, "Miscellaneous options:\n");
16569 n = (sizeof (dwz_misc_options_help)
16570 / sizeof (dwz_misc_options_help[0]));
16571 print_options_help (stream, dwz_misc_options_help, n, indent, limit);
16572
16573 #if DEVEL
16574 fprintf (stream, "Development options:\n");
16575 fprintf (stream, "%s",
16576 (" --devel-trace\n"
16577 " --devel-progress\n"
16578 " --devel-stats\n"
16579 " --devel-ignore-size\n"
16580 " --devel-ignore-locus\n"
16581 " --devel-force\n"
16582 " --devel-save-temps\n"
16583 " --devel-dump-checksum\n"
16584 " --devel-dump-dies\n"
16585 " --devel-dump-dups\n"
16586 " --devel-dump-pus\n"
16587 " --devel-unoptimized-multifile\n"
16588 " --devel-verify-dups\n"
16589 " --devel-verify-edges\n"
16590 " --devel-dump-edges\n"
16591 " --devel-partition-dups-opt\n"
16592 " --devel-die-count-method\n"
16593 " --devel-deduplication-mode={none,intra-cu,inter-cu}\n"
16594 " --devel-uni-lang / --devel-no-uni-lang\n"
16595 " --devel-gen-cu / --devel-no-gen-cu\n"));
16596 #endif
16597
16598 exit (failing);
16599 }
16600
16601 /* Print version and exit. */
16602 static void
version(void)16603 version (void)
16604 {
16605 printf ("dwz version " DWZ_VERSION "\n"
16606 "Copyright (C) " RH_YEARS " Red Hat, Inc.\n"
16607 "Copyright (C) " FSF_YEARS " Free Software Foundation, Inc.\n"
16608 "Copyright (C) " SUSE_YEARS " SUSE LLC.\n"
16609 "This program is free software; you may redistribute it under the terms of\n"
16610 "the GNU General Public License version 3 or (at your option) any later version.\n"
16611 "This program has absolutely no warranty.\n");
16612 exit (0);
16613 }
16614
16615 int
main(int argc,char * argv[])16616 main (int argc, char *argv[])
16617 {
16618 const char *outfile = NULL;
16619 int ret = 0;
16620 int i;
16621 unsigned long l;
16622 char *end;
16623 struct file_result res;
16624 bool hardlink = false;
16625 const char *file;
16626
16627 if (elf_version (EV_CURRENT) == EV_NONE)
16628 error (1, 0, "library out of date");
16629
16630 while (1)
16631 {
16632 int option_index = -1;
16633 int c = getopt_long (argc, argv, "m:o:qhl:L:M:r?v5", dwz_options, &option_index);
16634 if (c == -1)
16635 break;
16636 switch (c)
16637 {
16638 default:
16639 case '?':
16640 usage (argv[0], option_index == -1);
16641 break;
16642
16643 case 0:
16644 /* Option handled by getopt_long. */
16645 if (die_count_method_parsed)
16646 {
16647 die_count_method_parsed = 0;
16648 if (strcmp (optarg, "none") == 0)
16649 {
16650 die_count_method = none;
16651 break;
16652 }
16653 if (strcmp (optarg, "estimate") == 0)
16654 {
16655 die_count_method = estimate;
16656 break;
16657 }
16658 error (1, 0, "invalid argument --devel-die-count-method %s",
16659 optarg);
16660 }
16661 if (deduplication_mode_parsed)
16662 {
16663 deduplication_mode_parsed = 0;
16664 if (strcmp (optarg, "none") == 0)
16665 {
16666 deduplication_mode = dm_none;
16667 break;
16668 }
16669 if (strcmp (optarg, "intra-cu") == 0)
16670 {
16671 deduplication_mode = dm_intra_cu;
16672 break;
16673 }
16674 if (strcmp (optarg, "inter-cu") == 0)
16675 {
16676 deduplication_mode = dm_inter_cu;
16677 break;
16678 }
16679 error (1, 0, "invalid argument --devel-deduplication-mode %s",
16680 optarg);
16681 }
16682 if (odr_mode_parsed)
16683 {
16684 odr_mode_parsed = 0;
16685 if (strcmp (optarg, "basic") == 0)
16686 {
16687 odr_mode = ODR_BASIC;
16688 break;
16689 }
16690 if (strcmp (optarg, "link") == 0)
16691 {
16692 odr_mode = ODR_LINK;
16693 break;
16694 }
16695 error (1, 0, "invalid argument --odr-mode %s",
16696 optarg);
16697 }
16698 break;
16699
16700 case 'o':
16701 outfile = optarg;
16702 break;
16703
16704 case 'm':
16705 multifile = optarg;
16706 break;
16707
16708 case 'q':
16709 quiet = true;
16710 break;
16711
16712 case 'h':
16713 hardlink = true;
16714 break;
16715
16716 case 'M':
16717 multifile_name = optarg;
16718 break;
16719
16720 case 'r':
16721 multifile_relative = true;
16722 break;
16723
16724 case 'l':
16725 if (strcmp (optarg, "none") == 0)
16726 {
16727 low_mem_die_limit = -1U;
16728 break;
16729 }
16730 l = strtoul (optarg, &end, 0);
16731 if (*end != '\0' || optarg == end || (unsigned int) l != l)
16732 error (1, 0, "invalid argument -l %s", optarg);
16733 low_mem_die_limit = l;
16734 break;
16735
16736 case 'L':
16737 if (strcmp (optarg, "none") == 0)
16738 {
16739 max_die_limit = -1U;
16740 break;
16741 }
16742 l = strtoul (optarg, &end, 0);
16743 if (*end != '\0' || optarg == end || (unsigned int) l != l)
16744 error (1, 0, "invalid argument -L %s", optarg);
16745 max_die_limit = l;
16746 break;
16747
16748 case '5':
16749 dwarf_5 = true;
16750 break;
16751
16752 case 'v':
16753 version ();
16754 break;
16755 }
16756 }
16757
16758 /* Specifying a low-mem die-limit that is larger than or equal to the
16759 max die-limit has the effect of disabling low-mem mode. Make this
16760 explicit by setting it to the 'none' value. */
16761 if (low_mem_die_limit != -1U
16762 && low_mem_die_limit >= max_die_limit)
16763 low_mem_die_limit = -1U;
16764
16765 if (multifile_relative && multifile_name)
16766 error (1, 0, "-M and -r options can't be specified together");
16767
16768 if (optind == argc || optind + 1 == argc)
16769 {
16770 file = optind == argc ? "a.out" : argv[optind];
16771 if (multifile != NULL)
16772 {
16773 error (0, 0, "Too few files for multifile optimization");
16774 multifile = NULL;
16775 }
16776 if (stats_p)
16777 init_stats (file);
16778 res.die_count = 0;
16779 ret = (low_mem_die_limit == 0
16780 ? 2
16781 : dwz (file, outfile, &res, NULL, NULL));
16782 if (ret == 2)
16783 {
16784 multifile_mode = MULTIFILE_MODE_LOW_MEM;
16785 ret = dwz (file, outfile, &res, NULL, NULL);
16786 }
16787 }
16788 else
16789 {
16790 int nr_files = argc - optind;
16791 struct file_result *resa
16792 = (struct file_result *) malloc ((nr_files) * sizeof (*resa));
16793 bool hardlinks = false;
16794 int successcount = 0;
16795
16796 for (i = 0; i < nr_files; ++i)
16797 resa[i].die_count = 0;
16798 if (resa == NULL)
16799 error (1, ENOMEM, "failed to allocate result array");
16800 if (outfile != NULL)
16801 error (1, 0, "-o option not allowed for multiple files");
16802 if (multifile)
16803 {
16804 multi_info_fd = make_temp_file ("dwz.debug_info");
16805 multi_abbrev_fd = make_temp_file ("dwz.debug_abbrev");
16806 multi_line_fd = make_temp_file ("dwz.debug_line");
16807 multi_str_fd = make_temp_file ("dwz.debug_str");
16808 multi_macro_fd = make_temp_file ("dwz.debug_macro");
16809 if (multi_info_fd == -1
16810 || multi_abbrev_fd == -1
16811 || multi_line_fd == -1
16812 || multi_str_fd == -1
16813 || multi_macro_fd == -1)
16814 {
16815 error (0, 0, "Could not create multifile temporary files");
16816 multifile = NULL;
16817 }
16818 }
16819 for (i = optind; i < argc; i++)
16820 {
16821 int thisret;
16822 file = argv[i];
16823 if (stats_p)
16824 init_stats (file);
16825 thisret = (low_mem_die_limit == 0
16826 ? 2
16827 : dwz (file, NULL, &resa[i - optind],
16828 hardlinks ? resa : NULL, &argv[optind]));
16829 if (thisret == 2)
16830 {
16831 multifile_mode = MULTIFILE_MODE_LOW_MEM;
16832 thisret = dwz (file, NULL, &resa[i - optind],
16833 hardlinks ? resa : NULL, &argv[optind]);
16834 }
16835 else if (thisret == 1)
16836 ret = 1;
16837 else if (resa[i - optind].res >= 0)
16838 successcount++;
16839 if (hardlink
16840 && resa[i - optind].res >= 0
16841 && resa[i - optind].nlink > 1)
16842 hardlinks = true;
16843 }
16844 if (multifile && successcount < 2)
16845 {
16846 error (0, 0, "Too few files for multifile optimization");
16847 multifile = NULL;
16848 }
16849 if (multifile
16850 && multi_info_off == 0 && multi_str_off == 0 && multi_macro_off == 0)
16851 {
16852 if (!quiet)
16853 error (0, 0, "No suitable DWARF found for multifile optimization");
16854 multifile = NULL;
16855 }
16856 if (multifile)
16857 {
16858 unsigned int multifile_die_count = 0;
16859 int multi_fd = optimize_multifile (&multifile_die_count);
16860 DSO *dso;
16861 if (multi_fd == -1)
16862 return 1;
16863 dso = read_multifile (multi_fd, multifile_die_count);
16864 if (dso == NULL)
16865 ret = 1;
16866 else
16867 {
16868 for (i = optind; i < argc; i++)
16869 {
16870 dw_cu_ref cu;
16871 file = argv[i];
16872 if (stats_p)
16873 init_stats (file);
16874 multifile_mode = MULTIFILE_MODE_FI;
16875 /* Don't process again files that couldn't
16876 be processed successfully. */
16877 if (resa[i - optind].res == -1)
16878 continue;
16879 for (cu = alt_first_cu; cu; cu = cu->cu_next)
16880 alt_clear_dups (cu->cu_die);
16881 ret |= dwz (file, NULL, &resa[i - optind],
16882 hardlinks ? resa : NULL, &argv[optind]);
16883 }
16884 elf_end (dso->elf);
16885 close (multi_fd);
16886 free (dso);
16887 }
16888 cleanup ();
16889 strp_htab = alt_strp_htab;
16890 off_htab = alt_off_htab;
16891 dup_htab = alt_dup_htab;
16892 macro_htab = alt_macro_htab;
16893 pool = alt_pool;
16894 ob = alt_ob;
16895 ob2 = alt_ob2;
16896 cleanup ();
16897 }
16898 free (resa);
16899 }
16900
16901 if (stats_p)
16902 free (stats);
16903
16904 return ret;
16905 }
16906