From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id 99B823858CDA for ; Mon, 26 Sep 2022 05:23:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 99B823858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-x532.google.com with SMTP id c7so5554365pgt.11 for ; Sun, 25 Sep 2022 22:23:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date; bh=W3En6ZM5i6kThmfKsjaeeJXzGcQJUSB7acQoa7MPBjo=; b=OuQKwv2gNPb5D1ZqWqgbhmVMh+OoAenwm0OGz7ESip86sjjuDnYmGlGTfsgM/tEFjZ LZmxHMtYTWmL7aXSdzTcXTXyHEjiNbwdsuNvlqxEkKNuizVSU7437xj/Rqo5WcnnR5JC 02ITK3wYaLluyZf3ISeDTLXPLuRae7e11fOHckB4EnwDvQE75+15EpVzUcv/NdNMdi36 wJx8m+/KW1uf0G14rhKdTN89VpfPUJ+Er1nqI0/5oI2EZdbW5M5esxvPLv8+Kc7Nho+3 WHHluZT3A5MOs8wzszYFjIYvh6liu+uGRMnFgLp9JqnUcM82FJ1pULV3EiEdtXOC7Lz4 batw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date; bh=W3En6ZM5i6kThmfKsjaeeJXzGcQJUSB7acQoa7MPBjo=; b=HJZUfPVHHn2qo49kdfYGOZwrt8zklxw2PxbDOXnXxwNJNkrOeIJT5bCRPEebAbBfcD JzbZGVI8BLrJGK26rEx4UU7PbwzK4W5a1W2GuQwZlHj8wlzXXtkbDqNy8Lg38fKu8pGb qMPWwstnl0NUQr1n3fwpGwmJzVGR+F8/vIfb8hi0Q32EFlCE39DL3YrIuFqBrc2WbKDh h+l+cAogtY5SlFpuuJEZzEeldHrNLz51gvEPTp2m9ijhUfLR2vpGevKDhCDRm0NxWdar Ga1EQQnVQSvGmKXTBF2OlarfzlUahzwLVaYQH7Z5g6QCK3b03ek9vextwh1TB+VsIQZk adVg== X-Gm-Message-State: ACrzQf3J8FESB/uqJCzNUmUxz5xNUd20quCpZOfvgSfdag+Tg9UBAk7m b7GC16ACQsmG+ffFNrfXjF9CCfQgi2s= X-Google-Smtp-Source: AMsMyM4B/vHZrCo9hgT1EZoGZE59b1PomNfk563D5nTPAPxSt1yCfK9eaUTgtpZAIzNcLzwVAAcuKA== X-Received: by 2002:a63:5141:0:b0:43c:1471:7eb5 with SMTP id r1-20020a635141000000b0043c14717eb5mr16922873pgl.587.1664169810358; Sun, 25 Sep 2022 22:23:30 -0700 (PDT) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:8141:7672:2122:dcb8]) by smtp.gmail.com with ESMTPSA id q1-20020a17090a68c100b001fdc88d206fsm5610493pjj.9.2022.09.25.22.23.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Sep 2022 22:23:30 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 4904A114076E; Mon, 26 Sep 2022 14:53:27 +0930 (ACST) Date: Mon, 26 Sep 2022 14:53:27 +0930 From: Alan Modra To: binutils@sourceware.org Subject: stab nearest_line bfd_malloc_and_get_section Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3035.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: bfd_malloc_and_get_section performs some sanity checks on the section size before allocating memory. This patch avails the stab nearest_line code of that sanity checking, and tidies up memory afterward. * coffgen.c (_bfd_coff_close_and_cleanup): Call _bfd_stab_cleanup. * elf.c (_bfd_elf_close_and_cleanup): Likewise. * syms.c (_bfd_stab_section_find_nearest_line): Set *pinfo earlier. Use bfd_malloc_and_get_section. Free malloc'd buffers on failure. Malloc indextable. (_bfd_stab_cleanup): New function. * libbfd-in.h (_bfd_stab_cleanup): Declare. * libbfd.h: Regnerate. diff --git a/bfd/coffgen.c b/bfd/coffgen.c index 8eb69af2ce2..3e0fbc6184f 100644 --- a/bfd/coffgen.c +++ b/bfd/coffgen.c @@ -3175,7 +3175,10 @@ _bfd_coff_close_and_cleanup (bfd *abfd) if (bfd_get_format (abfd) == bfd_object || bfd_get_format (abfd) == bfd_core) - _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); + { + _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); + _bfd_stab_cleanup (abfd, &tdata->line_info); + } } return _bfd_generic_close_and_cleanup (abfd); } diff --git a/bfd/elf.c b/bfd/elf.c index 16cea4f8aeb..9a0bc1d2740 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -9649,6 +9649,7 @@ _bfd_elf_close_and_cleanup (bfd *abfd) if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) _bfd_elf_strtab_free (elf_shstrtab (abfd)); _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); + _bfd_stab_cleanup (abfd, &tdata->line_info); } return _bfd_generic_close_and_cleanup (abfd); diff --git a/bfd/libbfd-in.h b/bfd/libbfd-in.h index 04f343b051b..6d9d15f3409 100644 --- a/bfd/libbfd-in.h +++ b/bfd/libbfd-in.h @@ -615,6 +615,9 @@ extern bool _bfd_dwarf2_slurp_debug_info extern void _bfd_dwarf2_cleanup_debug_info (bfd *, void **) ATTRIBUTE_HIDDEN; +extern void _bfd_stab_cleanup + (bfd *, void **) ATTRIBUTE_HIDDEN; + /* Create a new section entry. */ extern struct bfd_hash_entry *bfd_section_hash_newfunc (struct bfd_hash_entry *, struct bfd_hash_table *, const char *) diff --git a/bfd/libbfd.h b/bfd/libbfd.h index a17b98e8e30..a67bded0d18 100644 --- a/bfd/libbfd.h +++ b/bfd/libbfd.h @@ -620,6 +620,9 @@ extern bool _bfd_dwarf2_slurp_debug_info extern void _bfd_dwarf2_cleanup_debug_info (bfd *, void **) ATTRIBUTE_HIDDEN; +extern void _bfd_stab_cleanup + (bfd *, void **) ATTRIBUTE_HIDDEN; + /* Create a new section entry. */ extern struct bfd_hash_entry *bfd_section_hash_newfunc (struct bfd_hash_entry *, struct bfd_hash_table *, const char *) diff --git a/bfd/syms.c b/bfd/syms.c index 7a454c5d7e7..ec62f3457ca 100644 --- a/bfd/syms.c +++ b/bfd/syms.c @@ -978,7 +978,7 @@ _bfd_stab_section_find_nearest_line (bfd *abfd, { if (info->stabsec == NULL || info->strsec == NULL) { - /* No stabs debugging information. */ + /* No usable stabs debugging information. */ return true; } @@ -1000,6 +1000,7 @@ _bfd_stab_section_find_nearest_line (bfd *abfd, info = (struct stab_find_info *) bfd_zalloc (abfd, amt); if (info == NULL) return false; + *pinfo = info; /* FIXME: When using the linker --split-by-file or --split-by-reloc options, it is possible for the .stab and @@ -1015,12 +1016,7 @@ _bfd_stab_section_find_nearest_line (bfd *abfd, info->strsec = bfd_get_section_by_name (abfd, "$GDB_STRINGS$"); if (info->stabsec == NULL || info->strsec == NULL) - { - /* No stabs debugging information. Set *pinfo so that we - can return quickly in the info != NULL case above. */ - *pinfo = info; - return true; - } + return true; } stabsize = (info->stabsec->rawsize @@ -1031,16 +1027,10 @@ _bfd_stab_section_find_nearest_line (bfd *abfd, ? info->strsec->rawsize : info->strsec->size); - info->stabs = (bfd_byte *) bfd_alloc (abfd, stabsize); - info->strs = (bfd_byte *) bfd_alloc (abfd, strsize); - if (info->stabs == NULL || info->strs == NULL) - return false; - - if (! bfd_get_section_contents (abfd, info->stabsec, info->stabs, - 0, stabsize) - || ! bfd_get_section_contents (abfd, info->strsec, info->strs, - 0, strsize)) - return false; + if (!bfd_malloc_and_get_section (abfd, info->stabsec, &info->stabs)) + goto out; + if (!bfd_malloc_and_get_section (abfd, info->strsec, &info->strs)) + goto out1; /* Stab strings ought to be nul terminated. Ensure the last one is, to prevent running off the end of the buffer. */ @@ -1052,15 +1042,24 @@ _bfd_stab_section_find_nearest_line (bfd *abfd, this should be no big deal. */ reloc_size = bfd_get_reloc_upper_bound (abfd, info->stabsec); if (reloc_size < 0) - return false; + goto out2; reloc_vector = (arelent **) bfd_malloc (reloc_size); if (reloc_vector == NULL && reloc_size != 0) - return false; + goto out2; reloc_count = bfd_canonicalize_reloc (abfd, info->stabsec, reloc_vector, symbols); if (reloc_count < 0) { + out3: free (reloc_vector); + out2: + free (info->strs); + info->strs = NULL; + out1: + free (info->stabs); + info->stabs = NULL; + out: + info->stabsec = NULL; return false; } if (reloc_count > 0) @@ -1091,8 +1090,7 @@ _bfd_stab_section_find_nearest_line (bfd *abfd, _bfd_error_handler (_("unsupported .stab relocation")); bfd_set_error (bfd_error_invalid_operation); - free (reloc_vector); - return false; + goto out3; } val = bfd_get_32 (abfd, info->stabs + octets); @@ -1146,14 +1144,21 @@ _bfd_stab_section_find_nearest_line (bfd *abfd, ++info->indextablesize; if (info->indextablesize == 0) - return true; + { + free (info->strs); + info->strs = NULL; + free (info->stabs); + info->stabs = NULL; + info->stabsec = NULL; + return true; + } ++info->indextablesize; amt = info->indextablesize; amt *= sizeof (struct indexentry); - info->indextable = (struct indexentry *) bfd_alloc (abfd, amt); + info->indextable = (struct indexentry *) bfd_malloc (amt); if (info->indextable == NULL) - return false; + goto out3; file_name = NULL; directory_name = NULL; @@ -1280,8 +1285,6 @@ _bfd_stab_section_find_nearest_line (bfd *abfd, info->indextablesize = i; qsort (info->indextable, (size_t) i, sizeof (struct indexentry), cmpindexentry); - - *pinfo = info; } /* We are passed a section relative offset. The offsets in the @@ -1446,6 +1449,18 @@ _bfd_stab_section_find_nearest_line (bfd *abfd, return true; } +void +_bfd_stab_cleanup (bfd *abfd ATTRIBUTE_UNUSED, void **pinfo) +{ + struct stab_find_info *info = (struct stab_find_info *) *pinfo; + if (info == NULL) + return; + + free (info->indextable); + free (info->strs); + free (info->stabs); +} + long _bfd_nosymbols_canonicalize_symtab (bfd *abfd ATTRIBUTE_UNUSED, asymbol **location ATTRIBUTE_UNUSED) -- Alan Modra Australia Development Lab, IBM