From patchwork Thu Jun 8 21:40:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matheus Branco Borella X-Patchwork-Id: 70801 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B715A3856DC7 for ; Thu, 8 Jun 2023 21:44:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B715A3856DC7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686260681; bh=YI5cMq7Z3MFKYWEiNBCafBM0lFn1bYC1OmgzHLcQ6iM=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=yVkZ/mC0FehgOuuLlXa9m7fUmRfmLfw0lYerK8UhGfmHKgeSdECPMVUIG1gmnqGqu iPH17hvFQRt+pOwgKovT47Vacl6aT1uu0CAYnLNnv0KTqxHufSzs3s1fv5QLFJq24W 1JQC5tYUhIKVRq+8M9xv6GjVaB2kJ/ytdMocqN1Y= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-oa1-x31.google.com (mail-oa1-x31.google.com [IPv6:2001:4860:4864:20::31]) by sourceware.org (Postfix) with ESMTPS id 566EA3858C2F for ; Thu, 8 Jun 2023 21:44:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 566EA3858C2F Received: by mail-oa1-x31.google.com with SMTP id 586e51a60fabf-19f6f8c8283so132089fac.3 for ; Thu, 08 Jun 2023 14:44:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686260655; x=1688852655; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YI5cMq7Z3MFKYWEiNBCafBM0lFn1bYC1OmgzHLcQ6iM=; b=MuN8oLtRFWEhgJCAzmen42mnEQ1LhxHYQRhAeKWyDbtKK3L0EwAhLiVaps2iL8/UTs bS/kSDiextePedJ1wQWMNtBNhY32oVJfwjZ9g5ZFGspY9GtBcqqBdAElffdrWhTeZNEc /0DyEtz54g0kaNHyI8+E4yaA6kvE4G4us67v+0Z/TnKjgtq5+t8P7a81g+vlE30u3SLL CckLIaD1WyjIIXWRX+NCMyrcpi0J+nJU+TWKKEqeeDmIlEUtmnSQO0E1hpl3iDPvFbA9 F+hKEacfh+E2IX8bEnhUImDOoC1Gjo5tDOgQik2WVzbzxuLiBFbTTgGIiwhWO5HpqXVj z9GA== X-Gm-Message-State: AC+VfDwaxvN3yHxpuSkv17LzI9JrTBsPDcO6EGMY3RtK9qHdcKnKaUm6 /z7xbZhsOEWr++6dAYhWRa9Opgk/BiaynQ== X-Google-Smtp-Source: ACHHUZ6MP7oV90wi3NJiJOAADc+HWmaF8ILJn+IUjGaB5RjQ1cwAqBgwwVmRA5jcivS5jNPRCX9qiw== X-Received: by 2002:a05:6870:344:b0:19f:9ab7:26a8 with SMTP id n4-20020a056870034400b0019f9ab726a8mr8717778oaf.54.1686260654813; Thu, 08 Jun 2023 14:44:14 -0700 (PDT) Received: from localhost.localdomain ([177.200.73.226]) by smtp.gmail.com with ESMTPSA id m18-20020a9d7ad2000000b006af731d100fsm803517otn.75.2023.06.08.14.44.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 14:44:14 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Matheus Branco Borella Subject: [PATCH] Add name_of_main and language_of_main to the DWARF index Date: Thu, 8 Jun 2023 18:40:13 -0300 Message-Id: <20230608214012.1561-1-dark.ryu.550@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Matheus Branco Borella via Gdb-patches From: Matheus Branco Borella Reply-To: Matheus Branco Borella Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This patch adds a new section to the DWARF index containing the name and the language of the main function symbol, gathered from `cooked_index::get_main`, if available. Currently, for lack of a better name, this section is called the "shortcut table" (suggestions for a better name are appreciated). The way this name is both saved and applied upon an index being loaded in mirrors how it is done in `cooked_index_functions`, more specifically, the full name of the main function symbol is saved and `set_objfile_main_name` is used to apply it after it is loaded. The main use case for this patch is in improving startup times when dealing with large binaries. Currently, when an index is used, GDB has to expand symtabs until it finds out what the language of the main function symbol is. For some large executables, this may take a considerable amount of time to complete, slowing down startup. This patch bypasses that operation by having both the name and language of the main function symbol be provided ahead of time by the index. In my testing (a binary with about 1.8GB worth of DWARF data) this change brings startup time down from about 34 seconds to about 1.5 seconds. (I feel like I might've changed too much about the index format by adding a breaking change. If there's a better way to do this it'd be glad to hear about it.) --- gdb/dwarf2/index-write.c | 47 +++++++++++++++++++++++++++++++++---- gdb/dwarf2/read-gdb-index.c | 44 +++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 6 deletions(-) diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c index 62c2cc6ac7..4554b5bc49 100644 --- a/gdb/dwarf2/index-write.c +++ b/gdb/dwarf2/index-write.c @@ -1080,14 +1080,15 @@ write_gdbindex_1 (FILE *out_file, const data_buf &types_cu_list, const data_buf &addr_vec, const data_buf &symtab_vec, - const data_buf &constant_pool) + const data_buf &constant_pool, + const data_buf &shortcuts) { data_buf contents; - const offset_type size_of_header = 6 * sizeof (offset_type); + const offset_type size_of_header = 7 * sizeof (offset_type); offset_type total_len = size_of_header; /* The version number. */ - contents.append_offset (8); + contents.append_offset (9); /* The offset of the CU list from the start of the file. */ contents.append_offset (total_len); @@ -1105,6 +1106,10 @@ write_gdbindex_1 (FILE *out_file, contents.append_offset (total_len); total_len += symtab_vec.size (); + /* The offset of the shortcut table from the start of the file. */ + contents.append_offset (total_len); + total_len += shortcuts.size (); + /* The offset of the constant pool from the start of the file. */ contents.append_offset (total_len); total_len += constant_pool.size (); @@ -1116,6 +1121,7 @@ write_gdbindex_1 (FILE *out_file, types_cu_list.file_write (out_file); addr_vec.file_write (out_file); symtab_vec.file_write (out_file); + shortcuts.file_write (out_file); constant_pool.file_write (out_file); assert_file_size (out_file, total_len); @@ -1193,6 +1199,34 @@ write_cooked_index (cooked_index *table, } } +/* Write shortcut information. */ + +static void +write_shortcuts_table (cooked_index *table, data_buf& shortcuts, + data_buf& cpool) +{ + const auto main_info = table->get_main (); + size_t main_name_offset = 0; + language lang = language_unknown; + + if (main_info != nullptr) + { + lang = main_info->per_cu->lang (); + + if (lang != language_unknown) + { + auto_obstack obstack; + const auto main_name = main_info->full_name (&obstack, true); + + main_name_offset = cpool.size (); + cpool.append_cstr0 (main_name); + } + } + + shortcuts.append_uint (4, BFD_ENDIAN_LITTLE, lang); + shortcuts.append_offset (main_name_offset); +} + /* 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 @@ -1270,11 +1304,14 @@ write_gdbindex (dwarf2_per_bfd *per_bfd, cooked_index *table, write_hash_table (&symtab, symtab_vec, constant_pool); + data_buf shortcuts; + write_shortcuts_table (table, shortcuts, constant_pool); + write_gdbindex_1(out_file, objfile_cu_list, types_cu_list, addr_vec, - symtab_vec, constant_pool); + symtab_vec, constant_pool, shortcuts); if (dwz_out_file != NULL) - write_gdbindex_1 (dwz_out_file, dwz_cu_list, {}, {}, {}, {}); + write_gdbindex_1 (dwz_out_file, dwz_cu_list, {}, {}, {}, {}, {}); else gdb_assert (dwz_cu_list.empty ()); } diff --git a/gdb/dwarf2/read-gdb-index.c b/gdb/dwarf2/read-gdb-index.c index 1006386cb2..f590c7fb99 100644 --- a/gdb/dwarf2/read-gdb-index.c +++ b/gdb/dwarf2/read-gdb-index.c @@ -88,6 +88,9 @@ struct mapped_gdb_index final : public mapped_index_base /* A pointer to the constant pool. */ gdb::array_view constant_pool; + /* The shortcut table data. */ + gdb::array_view shortcut_table; + /* Return the index into the constant pool of the name of the IDXth symbol in the symbol table. */ offset_type symbol_name_index (offset_type idx) const @@ -166,6 +169,7 @@ dwarf2_gdb_index::dump (struct objfile *objfile) mapped_gdb_index *index = (gdb::checked_static_cast (per_objfile->per_bfd->index_table.get ())); + gdb_printf (".gdb_index: version %d\n", index->version); gdb_printf ("\n"); } @@ -583,7 +587,7 @@ to use the section anyway."), /* Indexes with higher version than the one supported by GDB may be no longer backward compatible. */ - if (version > 8) + if (version > 9) return 0; map->version = version; @@ -608,6 +612,12 @@ to use the section anyway."), map->symbol_table = offset_view (gdb::array_view (symbol_table, symbol_table_end)); + ++i; + + const gdb_byte *shortcut_table = addr + metadata[i]; + const gdb_byte *shortcut_table_end = addr + metadata[i + 1]; + map->shortcut_table + = gdb::array_view (shortcut_table, shortcut_table_end); ++i; map->constant_pool = buffer.slice (metadata[i]); @@ -763,6 +773,36 @@ create_addrmap_from_gdb_index (dwarf2_per_objfile *per_objfile, = new (&per_bfd->obstack) addrmap_fixed (&per_bfd->obstack, &mutable_map); } +/* Sets the name and language of the main function from the shortcut table. */ + +static void +set_main_name_from_gdb_index (dwarf2_per_objfile *per_objfile, + mapped_gdb_index *index) +{ + auto ptr = index->shortcut_table.data (); + const auto lang = extract_unsigned_integer (ptr, 4, BFD_ENDIAN_LITTLE); + if (lang >= nr_languages) + { + complaint (_(".gdb_index shortcut table has invalid main language %u"), + (unsigned) lang); + return; + } + if (lang == language_unknown) + { + /* Don't bother if the language for the main symbol was not known or if + * there was no main symbol at all when the index was built. */ + return; + } + ptr += 4; + + const auto name_offset = extract_unsigned_integer (ptr, + sizeof (offset_type), + BFD_ENDIAN_LITTLE); + const auto name = (const char*) (index->constant_pool.data () + name_offset); + + set_objfile_main_name (per_objfile->objfile, name, (enum language) lang); +} + /* See read-gdb-index.h. */ int @@ -848,6 +888,8 @@ dwarf2_read_gdb_index create_addrmap_from_gdb_index (per_objfile, map.get ()); + set_main_name_from_gdb_index (per_objfile, map.get ()); + per_bfd->index_table = std::move (map); per_bfd->quick_file_names_table = create_quick_file_names_table (per_bfd->all_units.size ());