+static void
+assert_file_size (FILE *file, size_t expected_size)
+{
+ const auto file_size = ftell (file);
+ if (file_size == -1)
+ perror_with_name (("ftell"));
+ gdb_assert (file_size == expected_size);
+}
+
+/* Write a gdb index file to OUT_FILE from all the sections passed as
+ arguments. */
+
+static void
+write_gdbindex_1 (FILE *out_file,
+ const data_buf &cu_list,
+ const data_buf &types_cu_list,
+ const data_buf &addr_vec,
+ const data_buf &symtab_vec,
+ const data_buf &constant_pool)
+{
+ data_buf contents;
+ const offset_type size_of_header = 6 * sizeof (offset_type);
+ offset_type total_len = size_of_header;
+
+ /* The version number. */
+ contents.append_data (MAYBE_SWAP (8));
+
+ /* The offset of the CU list from the start of the file. */
+ contents.append_data (MAYBE_SWAP (total_len));
+ total_len += cu_list.size ();
+
+ /* The offset of the types CU list from the start of the file. */
+ contents.append_data (MAYBE_SWAP (total_len));
+ total_len += types_cu_list.size ();
+
+ /* The offset of the address table from the start of the file. */
+ contents.append_data (MAYBE_SWAP (total_len));
+ total_len += addr_vec.size ();
+
+ /* The offset of the symbol table from the start of the file. */
+ contents.append_data (MAYBE_SWAP (total_len));
+ total_len += symtab_vec.size ();
+
+ /* The offset of the constant pool from the start of the file. */
+ contents.append_data (MAYBE_SWAP (total_len));
+ total_len += constant_pool.size ();
+
+ gdb_assert (contents.size () == size_of_header);
+
+ contents.file_write (out_file);
+ cu_list.file_write (out_file);
+ types_cu_list.file_write (out_file);
+ addr_vec.file_write (out_file);
+ symtab_vec.file_write (out_file);
+ constant_pool.file_write (out_file);
+
+ assert_file_size (out_file, total_len);
+}
+
+/* Write contents of a .gdb_index section for OBJFILE into OUT_FILE.
+ If OBJFILE has an associated dwz file, write contents of a .gdb_index
+ section for that dwz file into DWZ_OUT_FILE. If OBJFILE does not have an
+ associated dwz file, DWZ_OUT_FILE must be NULL. */
+
+static void
+write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file,
+ FILE *dwz_out_file)