+ return TRUE;
+}
+
+/* Compare function for line sequences. */
+
+static int
+compare_sequences (const void* a, const void* b)
+{
+ const struct line_sequence* seq1 = a;
+ const struct line_sequence* seq2 = b;
+
+ /* Sort by low_pc as the primary key. */
+ if (seq1->low_pc < seq2->low_pc)
+ return -1;
+ if (seq1->low_pc > seq2->low_pc)
+ return 1;
+
+ /* If low_pc values are equal, sort in reverse order of
+ high_pc, so that the largest region comes first. */
+ if (seq1->last_line->address < seq2->last_line->address)
+ return 1;
+ if (seq1->last_line->address > seq2->last_line->address)
+ return -1;
+
+ if (seq1->last_line->op_index < seq2->last_line->op_index)
+ return 1;
+ if (seq1->last_line->op_index > seq2->last_line->op_index)
+ return -1;
+
+ /* num_lines is initially an index, to make the sort stable. */
+ if (seq1->num_lines < seq2->num_lines)
+ return -1;
+ if (seq1->num_lines > seq2->num_lines)
+ return 1;
+ return 0;
+}
+
+/* Construct the line information table for quick lookup. */
+
+static bfd_boolean
+build_line_info_table (struct line_info_table * table,
+ struct line_sequence * seq)
+{
+ bfd_size_type amt;
+ struct line_info** line_info_lookup;
+ struct line_info* each_line;
+ unsigned int num_lines;
+ unsigned int line_index;
+
+ if (seq->line_info_lookup != NULL)
+ return TRUE;
+
+ /* Count the number of line information entries. We could do this while
+ scanning the debug information, but some entries may be added via
+ lcl_head without having a sequence handy to increment the number of
+ lines. */
+ num_lines = 0;
+ for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
+ num_lines++;
+
+ seq->num_lines = num_lines;
+ if (num_lines == 0)
+ return TRUE;
+
+ /* Allocate space for the line information lookup table. */
+ amt = sizeof (struct line_info*) * num_lines;
+ line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
+ seq->line_info_lookup = line_info_lookup;
+ if (line_info_lookup == NULL)
+ return FALSE;
+
+ /* Create the line information lookup table. */
+ line_index = num_lines;
+ for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
+ line_info_lookup[--line_index] = each_line;
+
+ BFD_ASSERT (line_index == 0);
+ return TRUE;
+}
+
+/* Sort the line sequences for quick lookup. */
+
+static bfd_boolean
+sort_line_sequences (struct line_info_table* table)
+{
+ bfd_size_type amt;
+ struct line_sequence* sequences;
+ struct line_sequence* seq;
+ unsigned int n = 0;
+ unsigned int num_sequences = table->num_sequences;
+ bfd_vma last_high_pc;
+
+ if (num_sequences == 0)
+ return TRUE;
+
+ /* Allocate space for an array of sequences. */
+ amt = sizeof (struct line_sequence) * num_sequences;
+ sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
+ if (sequences == NULL)
+ return FALSE;
+
+ /* Copy the linked list into the array, freeing the original nodes. */
+ seq = table->sequences;
+ for (n = 0; n < num_sequences; n++)
+ {
+ struct line_sequence* last_seq = seq;
+
+ BFD_ASSERT (seq);
+ sequences[n].low_pc = seq->low_pc;
+ sequences[n].prev_sequence = NULL;
+ sequences[n].last_line = seq->last_line;
+ sequences[n].line_info_lookup = NULL;
+ sequences[n].num_lines = n;
+ seq = seq->prev_sequence;
+ free (last_seq);
+ }
+ BFD_ASSERT (seq == NULL);
+
+ qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
+
+ /* Make the list binary-searchable by trimming overlapping entries
+ and removing nested entries. */
+ num_sequences = 1;
+ last_high_pc = sequences[0].last_line->address;
+ for (n = 1; n < table->num_sequences; n++)
+ {
+ if (sequences[n].low_pc < last_high_pc)
+ {
+ if (sequences[n].last_line->address <= last_high_pc)
+ /* Skip nested entries. */
+ continue;
+
+ /* Trim overlapping entries. */
+ sequences[n].low_pc = last_high_pc;
+ }
+ last_high_pc = sequences[n].last_line->address;
+ if (n > num_sequences)
+ {
+ /* Close up the gap. */
+ sequences[num_sequences].low_pc = sequences[n].low_pc;
+ sequences[num_sequences].last_line = sequences[n].last_line;
+ }
+ num_sequences++;
+ }
+
+ table->sequences = sequences;
+ table->num_sequences = num_sequences;
+ return TRUE;
+}
+
+/* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
+
+static bfd_boolean
+line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
+{
+ if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
+ {
+ char **tmp;
+ bfd_size_type amt;
+
+ amt = table->num_dirs + DIR_ALLOC_CHUNK;
+ amt *= sizeof (char *);
+
+ tmp = (char **) bfd_realloc (table->dirs, amt);
+ if (tmp == NULL)
+ return FALSE;
+ table->dirs = tmp;
+ }
+
+ table->dirs[table->num_dirs++] = cur_dir;
+ return TRUE;
+}
+
+static bfd_boolean
+line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
+ unsigned int dir ATTRIBUTE_UNUSED,
+ unsigned int xtime ATTRIBUTE_UNUSED,
+ unsigned int size ATTRIBUTE_UNUSED)
+{
+ return line_info_add_include_dir (table, cur_dir);
+}
+
+/* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
+
+static bfd_boolean
+line_info_add_file_name (struct line_info_table *table, char *cur_file,
+ unsigned int dir, unsigned int xtime,
+ unsigned int size)
+{
+ if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
+ {
+ struct fileinfo *tmp;
+ bfd_size_type amt;
+
+ amt = table->num_files + FILE_ALLOC_CHUNK;
+ amt *= sizeof (struct fileinfo);
+
+ tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
+ if (tmp == NULL)
+ return FALSE;
+ table->files = tmp;
+ }
+
+ table->files[table->num_files].name = cur_file;
+ table->files[table->num_files].dir = dir;
+ table->files[table->num_files].time = xtime;
+ table->files[table->num_files].size = size;
+ table->num_files++;
+ return TRUE;
+}
+
+/* Read directory or file name entry format, starting with byte of
+ format count entries, ULEB128 pairs of entry formats, ULEB128 of
+ entries count and the entries themselves in the described entry
+ format. */
+
+static bfd_boolean
+read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
+ bfd_byte *buf_end, struct line_info_table *table,
+ bfd_boolean (*callback) (struct line_info_table *table,
+ char *cur_file,
+ unsigned int dir,
+ unsigned int time,
+ unsigned int size))
+{
+ bfd *abfd = unit->abfd;
+ bfd_byte format_count, formati;
+ bfd_vma data_count, datai;
+ bfd_byte *buf = *bufp;
+ bfd_byte *format_header_data;
+ unsigned int bytes_read;
+
+ format_count = read_1_byte (abfd, buf, buf_end);
+ buf += 1;
+ format_header_data = buf;
+ for (formati = 0; formati < format_count; formati++)
+ {
+ _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
+ buf += bytes_read;
+ _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
+ buf += bytes_read;
+ }
+
+ data_count = _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
+ buf += bytes_read;
+ if (format_count == 0 && data_count != 0)
+ {
+ _bfd_error_handler (_("DWARF error: zero format count"));
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+
+ /* PR 22210. Paranoia check. Don't bother running the loop
+ if we know that we are going to run out of buffer. */
+ if (data_count > (bfd_vma) (buf_end - buf))
+ {
+ _bfd_error_handler
+ (_("DWARF error: data count (%" PRIx64 ") larger than buffer size"),
+ (uint64_t) data_count);
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+
+ for (datai = 0; datai < data_count; datai++)
+ {
+ bfd_byte *format = format_header_data;
+ struct fileinfo fe;
+
+ memset (&fe, 0, sizeof fe);
+ for (formati = 0; formati < format_count; formati++)
+ {
+ bfd_vma content_type, form;
+ char *string_trash;
+ char **stringp = &string_trash;
+ unsigned int uint_trash, *uintp = &uint_trash;
+ struct attribute attr;
+
+ content_type = _bfd_safe_read_leb128 (abfd, format, &bytes_read,
+ FALSE, buf_end);
+ format += bytes_read;
+ switch (content_type)
+ {
+ case DW_LNCT_path:
+ stringp = &fe.name;
+ break;
+ case DW_LNCT_directory_index:
+ uintp = &fe.dir;
+ break;
+ case DW_LNCT_timestamp:
+ uintp = &fe.time;
+ break;
+ case DW_LNCT_size:
+ uintp = &fe.size;
+ break;
+ case DW_LNCT_MD5:
+ break;
+ default:
+ _bfd_error_handler
+ (_("DWARF error: unknown format content type %" PRIu64),
+ (uint64_t) content_type);
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+
+ form = _bfd_safe_read_leb128 (abfd, format, &bytes_read, FALSE,
+ buf_end);
+ format += bytes_read;
+
+ buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end);
+ if (buf == NULL)
+ return FALSE;
+ switch (form)
+ {
+ case DW_FORM_string:
+ case DW_FORM_line_strp:
+ *stringp = attr.u.str;
+ break;
+
+ case DW_FORM_data1:
+ case DW_FORM_data2:
+ case DW_FORM_data4:
+ case DW_FORM_data8:
+ case DW_FORM_udata:
+ *uintp = attr.u.val;
+ break;
+ }
+ }
+
+ if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
+ return FALSE;
+ }
+
+ *bufp = buf;
+ return TRUE;