From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 2C522386C584 for ; Mon, 27 Jun 2022 10:48:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2C522386C584 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-167-R7s8h81PNHiMCWV1lA-OXQ-1; Mon, 27 Jun 2022 06:48:20 -0400 X-MC-Unique: R7s8h81PNHiMCWV1lA-OXQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6A04A2806AB5 for ; Mon, 27 Jun 2022 10:48:20 +0000 (UTC) Received: from comet.redhat.com (unknown [10.39.194.181]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E241E2026D64 for ; Mon, 27 Jun 2022 10:48:19 +0000 (UTC) From: Nick Clifton To: binutils@sourceware.org Subject: Commit: Fix indexed string handling in dwarf.c Date: Mon, 27 Jun 2022 11:48:16 +0100 Message-ID: <87o7yes7pr.fsf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="=-=-=" X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Jun 2022 10:48:24 -0000 --=-=-= Content-Type: text/plain Hi Guys, I am applying the attached patch to fix the fetch_indexed_string() function in binutils/dwarf.c so that it does not use the length of the first string table in the .debug_str section as the length for every string table in the section. Doing so removes bogus warnings about string indexes being too big. Cheers Nick binutils/ChangeLog 2022-06-27 Nick Clifton * dwarf.c (fetch_indexed_string): Do not use length of first table in string section as the length of every table in the section. * testsuite/binutils-all/pr26112.r: Update expected output. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=dwarf.c.patch diff --git a/binutils/dwarf.c b/binutils/dwarf.c index d9a1ee5a86b..bcabb61b871 100644 --- a/binutils/dwarf.c +++ b/binutils/dwarf.c @@ -703,8 +703,6 @@ fetch_indexed_string (dwarf_vma idx, dwarf_vma index_offset; dwarf_vma str_offset; const char * ret; - unsigned char *curr, *end; - dwarf_vma length; if (index_section->start == NULL) return (dwo ? _("") @@ -714,99 +712,46 @@ fetch_indexed_string (dwarf_vma idx, return (dwo ? _("") : _("")); - curr = index_section->start; - end = curr + index_section->size; - /* FIXME: We should cache the length... */ - SAFE_BYTE_GET_AND_INC (length, curr, 4, end); - if (length == 0xffffffff) - { - if (offset_size != 8) - warn (_("Expected offset size of 8 but given %s"), dwarf_vmatoa ("x", offset_size)); - SAFE_BYTE_GET_AND_INC (length, curr, 8, end); - } - else if (offset_size != 4) - { - warn (_("Expected offset size of 4 but given %s"), dwarf_vmatoa ("x", offset_size)); - } - - if (length == 0) - { - /* This is probably an old style .debug_str_offset section which - just contains offsets and no header (and the first offset is 0). */ - curr = index_section->start; - length = index_section->size; - } - else - { - /* Skip the version and padding bytes. - We assume that they are correct. */ - if (end - curr >= 4) - curr += 4; - else - curr = end; - if (length >= 4) - length -= 4; - else - length = 0; - - if (this_set != NULL - && this_set->section_sizes[DW_SECT_STR_OFFSETS] < length) - length = this_set->section_sizes[DW_SECT_STR_OFFSETS]; - - if (length > (dwarf_vma) (end - curr)) - { - warn (_("index table size too large for section %s vs %s\n"), - dwarf_vmatoa ("x", length), - dwarf_vmatoa ("x", index_section->size)); - length = end - curr; - } - - if (length < offset_size) - { - warn (_("index table size %s is too small\n"), - dwarf_vmatoa ("x", length)); - return _(""); - } - } - index_offset = idx * offset_size; if (this_set != NULL) index_offset += this_set->section_offsets [DW_SECT_STR_OFFSETS]; - if (index_offset >= length - || length - index_offset < offset_size) + index_offset += str_offsets_base; + + if (index_offset + offset_size > index_section->size) { - warn (_("DW_FORM_GNU_str_index offset too big: 0x%s vs 0x%s\n"), + warn (_("string index of %s converts to an offset of 0x%s which is too big for section %s"), + dwarf_vmatoa ("d", idx), dwarf_vmatoa ("x", index_offset), - dwarf_vmatoa ("x", length)); - return _(""); - } + str_section->name); - if (str_offsets_base > 0) - { - if (offset_size == 8) - str_offsets_base -= 16; - else - str_offsets_base -= 8; + return _(""); } - str_offset = byte_get (curr + index_offset + str_offsets_base, offset_size); + /* FIXME: If we are being paranoid then we should also check to see if + IDX references an entry beyond the end of the string table pointed to + by STR_OFFSETS_BASE. (Since there can be more than one string table + in a DWARF string section). */ + + str_offset = byte_get (index_section->start + index_offset, offset_size); + str_offset -= str_section->address; if (str_offset >= str_section->size) { - warn (_("DW_FORM_GNU_str_index indirect offset too big: 0x%s\n"), + warn (_("indirect offset too big: 0x%s\n"), dwarf_vmatoa ("x", str_offset)); return _(""); } ret = (const char *) str_section->start + str_offset; + /* Unfortunately we cannot rely upon str_section ending with a NUL byte. Since our caller is expecting to receive a well formed C string we test for the lack of a terminating byte here. */ if (strnlen (ret, str_section->size - str_offset) == str_section->size - str_offset) - ret = (const char *) _(""); + return _(""); return ret; } diff --git a/binutils/testsuite/binutils-all/pr26112.r b/binutils/testsuite/binutils-all/pr26112.r index b82b06025b1..f28441393e0 100644 --- a/binutils/testsuite/binutils-all/pr26112.r +++ b/binutils/testsuite/binutils-all/pr26112.r @@ -6,8 +6,10 @@ Contents of the .debug_macro section: Offset into .debug_line: 0x0 DW_MACRO_start_file - lineno: 0 filenum: 1 - DW_MACRO_define_strx lineno : 1 macro : FOO bar + DW_MACRO_define_strx lineno : 1 macro : - DW_MACRO_end_file + DW_MACRO_define_strx lineno : 0 macro : /home/tstellar/llvm-project/clang-build + DW_MACRO_define_strx lineno : 0 macro : FOO bar DW_MACRO_define_strx lineno : 0 macro : __llvm__ 1 DW_MACRO_define_strx lineno : 0 macro : __clang__ 1 DW_MACRO_define_strx lineno : 0 macro : __clang_major__ 11 @@ -346,5 +348,3 @@ Contents of the .debug_macro section: DW_MACRO_define_strx lineno : 0 macro : __STDC__ 1 DW_MACRO_define_strx lineno : 0 macro : __STDC_HOSTED__ 1 DW_MACRO_define_strx lineno : 0 macro : __STDC_VERSION__ 201710L - DW_MACRO_define_strx lineno : 0 macro : __STDC_UTF_16__ 1 - DW_MACRO_define_strx lineno : 0 macro : __STDC_UTF_32__ 1 --=-=-=--