From patchwork Thu Feb 23 03:48:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 60774 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp102150wrd; Wed, 22 Feb 2023 19:49:15 -0800 (PST) X-Google-Smtp-Source: AK7set/7tC2u+Zb7uNP1lKy1dBgzLB7Mu1lmtbfk6fJlalCvQA/lGKJfhb2+Sq/qctq+CRoiqV3j X-Received: by 2002:a17:906:7e06:b0:8aa:c2e1:6a64 with SMTP id e6-20020a1709067e0600b008aac2e16a64mr17368218ejr.61.1677124155411; Wed, 22 Feb 2023 19:49:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677124155; cv=none; d=google.com; s=arc-20160816; b=OgnSyW/d+PFC8XdWxluzd6VfhC3SBG+HPBEdiEkMPWyongVCqtMMKqehvysygDbZ8J PTS2GUEYjZB79z6q90zE6UN6byDftAl6ktLC56WOnD8GdRExyV+0CBYXEKwupxzdel1e GZ1mG2cNQmhuGFll76T7OgciA7Zz6+/lLVsjBsvXflB9qdoqgZGGHHjY3BprxMgDJpX8 k4SZZGApEsKorwwaRaDy+z/DLfrGp3ANX1LbENI+OPD0kAWACr/umNgr4CdkMjAatO0g 1trJfilshqd7W47iUj4oQfsB6l/gGSdqxIuOPYvi//LKKniwPJ5z1KrrGvm0ZLNaXTE+ k+0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=zqJ/i0ZzKhNrfERcV18AmcNqxus+0ueBhJpZr86IvOE=; b=WKg+0n4PvtxZffCkB4Pi/jOVTQ223fmqkml4JJ4ORqaxfvg6LDeI1HYNQ1FFt2o2HM bgDwEaCA0zpSIf8Lse4I58yFJqmZZZIT2cBtZhzAWUeBHS0sHb5zpLkt5QCopMsu7zwV q3iCp2PNBcfqigDqLlRJIGw/sRsgcjwatvHQ4D6r9sN2dxVjbgJMPwt5K7OJ6IH/sumQ rH/ZwNuWPBpZfpoBMY2BKvKJcgURPX8YtT5rokRTKdAn10w5rs7RZbDPGNsS5gKpJHR0 15n13W0WnAWwZ4hg3KyKqAN8mue+qGu1nhOHcEP4yOejjglLJxrCVIT7rTizBTnI3fY6 LVBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=ixnDBPH0; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id zv16-20020a170907719000b008e259a32ab2si5323679ejb.660.2023.02.22.19.49.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 19:49:15 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=ixnDBPH0; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 53611385842B for ; Thu, 23 Feb 2023 03:49:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 53611385842B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677124154; bh=zqJ/i0ZzKhNrfERcV18AmcNqxus+0ueBhJpZr86IvOE=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ixnDBPH0xqU2wVOjn0GEs1B0/UjhsEfDaPqjSwoJgzM3K/kW2SrrO/bgQ2jcbNT09 dE/IoVBckp0CPHlqRu3AwOU9b4olhPA9XS8q7G6KdYpld1ut1sZzP9fg/nPpvECHFw gLkLYL5wVieBE/eTHJVRp/go1/Odjl+pEIDCijWc= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by sourceware.org (Postfix) with ESMTPS id 835DC3858D33 for ; Thu, 23 Feb 2023 03:49:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 835DC3858D33 Received: by mail-pj1-x102f.google.com with SMTP id c23so6523136pjo.4 for ; Wed, 22 Feb 2023 19:49:03 -0800 (PST) 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:message-id:reply-to; bh=zqJ/i0ZzKhNrfERcV18AmcNqxus+0ueBhJpZr86IvOE=; b=rEKWwKSSXaUk06PDJLQm9GcT1Q2BbG9z4n1OIn02K8eqptnjb7RD7xZsNdlHH5qcVr IsqISFexHKygzMzHToJunGxEZNoIMW6xdoqRWBtoZN5N34xrv1q7UjmleQxaGNq7JGZa ahLz7b899p7zbS4B12IS4+OxNwzeSPgmVHlPBCCZp6jLIq3TGqtLZF7BwQaQSkvsfn8S XKfKWxFnkL1HDLEEpL4ii+Sybx1FBhifS1tHMZBIubDr41x/1QumBlu3TkYFMjY1UU3F vGwnK/bDerrAeKr3THyHvSVIiOIjBq1s/uX3dhVwp7FfaWiciZDWn4CdkECR9nFNEzM9 J//Q== X-Gm-Message-State: AO0yUKWhosKfp4icb6MoHcrvH8ttzLcirkQxfOQOXh89RpqFymsQ9fHR hFRHLOI9Cc8Yqk2+nHU9kOlI12SmPk4= X-Received: by 2002:a05:6a20:3ca0:b0:be:e0c3:5012 with SMTP id b32-20020a056a203ca000b000bee0c35012mr10425479pzj.1.1677124141876; Wed, 22 Feb 2023 19:49:01 -0800 (PST) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:8e1e:cc42:da50:5c93]) by smtp.gmail.com with ESMTPSA id j20-20020a62b614000000b00580d25a2bb2sm2380276pff.108.2023.02.22.19.49.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 19:49:01 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 67D251142769; Thu, 23 Feb 2023 14:18:58 +1030 (ACDT) Date: Thu, 23 Feb 2023 14:18:58 +1030 To: binutils@sourceware.org Subject: Test SEC_HAS_CONTENTS before reading section contents Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3034.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 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-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758592138499717540?= X-GMAIL-MSGID: =?utf-8?q?1758592138499717540?= bfd_malloc_and_get_section does size sanity checking before allocating memory and reading contents. These size checks are not done for bss style sections, because they typically don't occupy file space and thus can't be compared against file size. However, if you are expecting to look at something other than a whole lot of zeros, don't allow fuzzers to avoid the size checking. * cofflink.c (process_embedded_commands): Don't look at sections without SEC_HAS_CONTENTS set. * cpu-arm.c (bfd_arm_update_notes): Likewise. (bfd_arm_get_mach_from_notes): Likewise. * elf-eh-frame.c (_bfd_elf_parse_eh_frame): Likewise. * elf-hppa.h (elf_hppa_sort_unwind): Likewise. * elf-m10300.c (mn10300_elf_relax_section): Likewise. * elf-sframe.c (_bfd_elf_parse_sframe): Likewise. * elf.c (_bfd_elf_print_private_bfd_data): Likewise. * elf32-arm.c (bfd_elf32_arm_process_before_allocation): Likewise. * elf32-avr.c (avr_elf32_load_property_records): Likewise. * elf32-ppc.c (_bfd_elf_ppc_set_arch): Likewise. (ppc_elf_get_synthetic_symtab, ppc_elf_relax_section): Likewise. * elf64-ppc.c (ppc64_elf_get_synthetic_symtab): Likewise. (opd_entry_value, ppc64_elf_edit_opd, ppc64_elf_edit_toc): Likewise. * elf64-x86-64.c (elf_x86_64_get_synthetic_symtab): Likewise. * elflink.c (elf_link_add_object_symbols): Likewise. (bfd_elf_get_bfd_needed_list): Likewise. * elfnn-aarch64.c (get_plt_type): Likewise. * elfxx-mips.c (_bfd_mips_elf_get_synthetic_symtab): Likewise. * linker.c (_bfd_handle_already_linked): Likewise. * opncls.c (bfd_get_debug_link_info_1): Likewise. (bfd_get_alt_debug_link_info, get_build_id): Likewise. * peXXigen.c (pe_print_idata, pe_print_pdata): Likewise. (_bfd_XX_print_ce_compressed_pdata, pe_print_reloc): Likewise. * pei-x86_64.c (pex64_bfd_print_pdata_section): Likewise. * stabs.c (_bfd_link_section_stabs): Likewise. (_bfd_discard_section_stabs): Likewise. * xcofflink.c (_bfd_xcoff_get_dynamic_symtab_upper_bound): Likewise. (_bfd_xcoff_canonicalize_dynamic_symtab): Likewise. (_bfd_xcoff_get_dynamic_reloc_upper_bound): Likewise. (_bfd_xcoff_canonicalize_dynamic_reloc): Likewise. (xcoff_link_add_dynamic_symbols): Likewise. (xcoff_link_check_dynamic_ar_symbols): Likewise. (bfd_xcoff_build_dynamic_sections): Likewise. diff --git a/bfd/cofflink.c b/bfd/cofflink.c index 3174bd3aff1..0de2e0bc391 100644 --- a/bfd/cofflink.c +++ b/bfd/cofflink.c @@ -1213,7 +1213,7 @@ process_embedded_commands (bfd *output_bfd, char *e; bfd_byte *copy; - if (!sec) + if (sec == NULL || (sec->flags & SEC_HAS_CONTENTS) == 0) return 1; if (!bfd_malloc_and_get_section (abfd, sec, ©)) diff --git a/bfd/cpu-arm.c b/bfd/cpu-arm.c index 09bcf5bd27f..03482b0daa0 100644 --- a/bfd/cpu-arm.c +++ b/bfd/cpu-arm.c @@ -418,7 +418,8 @@ bfd_arm_update_notes (bfd *abfd, const char *note_section) different. */ arm_arch_section = bfd_get_section_by_name (abfd, note_section); - if (arm_arch_section == NULL) + if (arm_arch_section == NULL + || (arm_arch_section->flags & SEC_HAS_CONTENTS) == 0) return true; buffer_size = arm_arch_section->size; @@ -521,7 +522,8 @@ bfd_arm_get_mach_from_notes (bfd *abfd, const char *note_section) different. */ arm_arch_section = bfd_get_section_by_name (abfd, note_section); - if (arm_arch_section == NULL) + if (arm_arch_section == NULL + || (arm_arch_section->flags & SEC_HAS_CONTENTS) == 0) return bfd_mach_arm_unknown; buffer_size = arm_arch_section->size; diff --git a/bfd/elf-eh-frame.c b/bfd/elf-eh-frame.c index 90a5848e663..bf7a9902355 100644 --- a/bfd/elf-eh-frame.c +++ b/bfd/elf-eh-frame.c @@ -602,6 +602,7 @@ _bfd_elf_parse_eh_frame (bfd *abfd, struct bfd_link_info *info, hdr_info = &htab->eh_info; if (sec->size == 0 + || (sec->flags & SEC_HAS_CONTENTS) == 0 || sec->sec_info_type != SEC_INFO_TYPE_NONE) { /* This file does not contain .eh_frame information. */ diff --git a/bfd/elf-hppa.h b/bfd/elf-hppa.h index 7896c028099..747c5b1a18c 100644 --- a/bfd/elf-hppa.h +++ b/bfd/elf-hppa.h @@ -984,7 +984,8 @@ elf_hppa_sort_unwind (bfd *abfd) Consider what happens if someone inept creates a linker script that puts unwind information in .text. */ s = bfd_get_section_by_name (abfd, ".PARISC.unwind"); - if (s != NULL) + if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0) + { bfd_size_type size; bfd_byte *contents; diff --git a/bfd/elf-m10300.c b/bfd/elf-m10300.c index bc3daecba29..7e084e64b0f 100644 --- a/bfd/elf-m10300.c +++ b/bfd/elf-m10300.c @@ -2694,7 +2694,8 @@ mn10300_elf_relax_section (bfd *abfd, if (! ((section->flags & SEC_RELOC) != 0 && section->reloc_count != 0)) continue; - if ((section->flags & SEC_ALLOC) == 0) + if ((section->flags & SEC_ALLOC) == 0 + || (section->flags & SEC_HAS_CONTENTS) == 0) continue; /* Get cached copy of section contents if it exists. */ @@ -3034,7 +3035,9 @@ mn10300_elf_relax_section (bfd *abfd, unsigned int symcount; /* Skip non-code sections and empty sections. */ - if ((section->flags & SEC_CODE) == 0 || section->size == 0) + if ((section->flags & SEC_CODE) == 0 + || (section->flags & SEC_HAS_CONTENTS) == 0 + || section->size == 0) continue; if (section->reloc_count != 0) diff --git a/bfd/elf-sframe.c b/bfd/elf-sframe.c index d2954ba9193..57d67989bf9 100644 --- a/bfd/elf-sframe.c +++ b/bfd/elf-sframe.c @@ -193,6 +193,7 @@ _bfd_elf_parse_sframe (bfd *abfd, int decerr = 0; if (sec->size == 0 + || (sec->flags & SEC_HAS_CONTENTS) == 0 || sec->sec_info_type != SEC_INFO_TYPE_NONE) { /* This file does not contain .sframe information. */ diff --git a/bfd/elf.c b/bfd/elf.c index a82a1dd60a6..37f331b98cd 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -1689,7 +1689,7 @@ _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) } s = bfd_get_section_by_name (abfd, ".dynamic"); - if (s != NULL) + if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0) { unsigned int elfsec; unsigned long shlink; diff --git a/bfd/elf32-arm.c b/bfd/elf32-arm.c index a6d83b97c97..e07e12226a5 100644 --- a/bfd/elf32-arm.c +++ b/bfd/elf32-arm.c @@ -7882,7 +7882,8 @@ bfd_elf32_arm_process_before_allocation (bfd *abfd, if (sec->reloc_count == 0) continue; - if ((sec->flags & SEC_EXCLUDE) != 0) + if ((sec->flags & SEC_EXCLUDE) != 0 + || (sec->flags & SEC_HAS_CONTENTS) == 0) continue; symtab_hdr = & elf_symtab_hdr (abfd); diff --git a/bfd/elf32-avr.c b/bfd/elf32-avr.c index e39746823f4..c01355ac5b7 100644 --- a/bfd/elf32-avr.c +++ b/bfd/elf32-avr.c @@ -4216,7 +4216,7 @@ avr_elf32_load_property_records (bfd *abfd) /* Find the '.avr.prop' section and load the contents into memory. */ sec = bfd_get_section_by_name (abfd, AVR_PROPERTY_RECORD_SECTION_NAME); - if (sec == NULL) + if (sec == NULL || (sec->flags & SEC_HAS_CONTENTS) == 0) return NULL; return avr_elf32_load_records_from_section (abfd, sec); } diff --git a/bfd/elf32-ppc.c b/bfd/elf32-ppc.c index 833bc744563..bb77ba2d5c7 100644 --- a/bfd/elf32-ppc.c +++ b/bfd/elf32-ppc.c @@ -1087,6 +1087,7 @@ _bfd_elf_ppc_set_arch (bfd *abfd) s = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME); if (s != NULL && s->size >= 24 + && (s->flags & SEC_HAS_CONTENTS) != 0 && bfd_malloc_and_get_section (abfd, s, &contents)) { unsigned int apuinfo_size = bfd_get_32 (abfd, contents + 4); @@ -1840,7 +1841,8 @@ ppc_elf_get_synthetic_symtab (bfd *abfd, long symcount, asymbol **syms, /* If this object was prelinked, the prelinker stored the address of .glink at got[1]. If it wasn't prelinked, got[1] will be zero. */ dynamic = bfd_get_section_by_name (abfd, ".dynamic"); - if (dynamic != NULL) + if (dynamic != NULL + && (dynamic->flags & SEC_HAS_CONTENTS) != 0) { bfd_byte *dynbuf, *extdyn, *extdynend; size_t extdynsize; @@ -6106,6 +6108,7 @@ ppc_elf_relax_section (bfd *abfd, /* No need to do anything with non-alloc or non-code sections. */ if ((isec->flags & SEC_ALLOC) == 0 || (isec->flags & SEC_CODE) == 0 + || (isec->flags & SEC_HAS_CONTENTS) == 0 || (isec->flags & SEC_LINKER_CREATED) != 0 || isec->size < 4) return true; diff --git a/bfd/elf64-ppc.c b/bfd/elf64-ppc.c index 193e00bc88d..069bd758aec 100644 --- a/bfd/elf64-ppc.c +++ b/bfd/elf64-ppc.c @@ -2472,7 +2472,9 @@ ppc64_elf_get_synthetic_symtab (bfd *abfd, asection *dynamic, *glink = NULL, *relplt = NULL; arelent *p; - if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents)) + if (opd != NULL + && ((opd->flags & SEC_HAS_CONTENTS) == 0 + || !bfd_malloc_and_get_section (abfd, opd, &contents))) { free_contents_and_exit_err: count = -1; @@ -2507,7 +2509,8 @@ ppc64_elf_get_synthetic_symtab (bfd *abfd, size_t extdynsize; void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); - if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf)) + if ((dynamic->flags & SEC_HAS_CONTENTS) == 0 + || !bfd_malloc_and_get_section (abfd, dynamic, &dynbuf)) goto free_contents_and_exit_err; extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; @@ -5536,7 +5539,8 @@ opd_entry_value (asection *opd_sec, if (contents == NULL) { - if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents)) + if ((opd_sec->flags & SEC_HAS_CONTENTS) == 0 + || !bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents)) return (bfd_vma) -1; ppc64_elf_tdata (opd_bfd)->opd.contents = contents; } @@ -7361,7 +7365,9 @@ ppc64_elf_edit_opd (struct bfd_link_info *info) continue; sec = bfd_get_section_by_name (ibfd, ".opd"); - if (sec == NULL || sec->size == 0) + if (sec == NULL + || sec->size == 0 + || (sec->flags & SEC_HAS_CONTENTS) == 0) continue; if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) @@ -8922,6 +8928,7 @@ ppc64_elf_edit_toc (struct bfd_link_info *info) toc = bfd_get_section_by_name (ibfd, ".toc"); if (toc == NULL || toc->size == 0 + || (toc->flags & SEC_HAS_CONTENTS) == 0 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS || discarded_section (toc)) continue; diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c index 5f89190a6a0..ef0ebdd6967 100644 --- a/bfd/elf64-x86-64.c +++ b/bfd/elf64-x86-64.c @@ -4967,7 +4967,9 @@ elf_x86_64_get_synthetic_symtab (bfd *abfd, for (j = 0; plts[j].name != NULL; j++) { plt = bfd_get_section_by_name (abfd, plts[j].name); - if (plt == NULL || plt->size == 0) + if (plt == NULL + || plt->size == 0 + || (plt->flags & SEC_HAS_CONTENTS) == 0) continue; /* Get the PLT section contents. */ diff --git a/bfd/elflink.c b/bfd/elflink.c index 80e3a8d053d..bab1a36598e 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -4386,7 +4386,7 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | DYN_NO_NEEDED)) == 0; s = bfd_get_section_by_name (abfd, ".dynamic"); - if (s != NULL && s->size != 0) + if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0) { bfd_byte *dynbuf; bfd_byte *extdyn; @@ -8204,7 +8204,7 @@ bfd_elf_get_bfd_needed_list (bfd *abfd, return true; s = bfd_get_section_by_name (abfd, ".dynamic"); - if (s == NULL || s->size == 0) + if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0) return true; if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) diff --git a/bfd/elfnn-aarch64.c b/bfd/elfnn-aarch64.c index c109ccdd996..bf8f913a66d 100644 --- a/bfd/elfnn-aarch64.c +++ b/bfd/elfnn-aarch64.c @@ -9880,6 +9880,7 @@ get_plt_type (bfd *abfd) bfd_byte *contents, *extdyn, *extdynend; asection *sec = bfd_get_section_by_name (abfd, ".dynamic"); if (!sec + || (sec->flags & SEC_HAS_CONTENTS) == 0 || sec->size < sizeof (ElfNN_External_Dyn) || !bfd_malloc_and_get_section (abfd, sec, &contents)) return ret; diff --git a/bfd/elfxx-mips.c b/bfd/elfxx-mips.c index e9fb61ff9e7..5b66cb81c1b 100644 --- a/bfd/elfxx-mips.c +++ b/bfd/elfxx-mips.c @@ -16572,7 +16572,7 @@ _bfd_mips_elf_get_synthetic_symtab (bfd *abfd, return 0; plt = bfd_get_section_by_name (abfd, ".plt"); - if (plt == NULL) + if (plt == NULL || (plt->flags & SEC_HAS_CONTENTS) == 0) return 0; slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; diff --git a/bfd/linker.c b/bfd/linker.c index 4fb7a6674f9..e57c9ee0486 100644 --- a/bfd/linker.c +++ b/bfd/linker.c @@ -2880,27 +2880,38 @@ _bfd_handle_already_linked (asection *sec, sec->owner, sec); else if (sec->size != 0) { - bfd_byte *sec_contents, *l_sec_contents = NULL; - - if (!bfd_malloc_and_get_section (sec->owner, sec, &sec_contents)) + bfd_byte *sec_contents, *l_sec_contents; + + if ((sec->flags & SEC_HAS_CONTENTS) == 0 + && (l->sec->flags & SEC_HAS_CONTENTS) == 0) + ; + else if ((sec->flags & SEC_HAS_CONTENTS) == 0 + || !bfd_malloc_and_get_section (sec->owner, sec, + &sec_contents)) info->callbacks->einfo /* xgettext:c-format */ (_("%pB: could not read contents of section `%pA'\n"), sec->owner, sec); - else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, - &l_sec_contents)) - info->callbacks->einfo - /* xgettext:c-format */ - (_("%pB: could not read contents of section `%pA'\n"), - l->sec->owner, l->sec); - else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) - info->callbacks->einfo - /* xgettext:c-format */ - (_("%pB: duplicate section `%pA' has different contents\n"), - sec->owner, sec); - - free (sec_contents); - free (l_sec_contents); + else if ((l->sec->flags & SEC_HAS_CONTENTS) == 0 + || !bfd_malloc_and_get_section (l->sec->owner, l->sec, + &l_sec_contents)) + { + info->callbacks->einfo + /* xgettext:c-format */ + (_("%pB: could not read contents of section `%pA'\n"), + l->sec->owner, l->sec); + free (sec_contents); + } + else + { + if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) + info->callbacks->einfo + /* xgettext:c-format */ + (_("%pB: duplicate section `%pA' has different contents\n"), + sec->owner, sec); + free (l_sec_contents); + free (sec_contents); + } } break; } diff --git a/bfd/opncls.c b/bfd/opncls.c index cdf09b333fa..abea464baa4 100644 --- a/bfd/opncls.c +++ b/bfd/opncls.c @@ -1201,7 +1201,7 @@ bfd_get_debug_link_info_1 (bfd *abfd, void *crc32_out) sect = bfd_get_section_by_name (abfd, GNU_DEBUGLINK); - if (sect == NULL) + if (sect == NULL || (sect->flags & SEC_HAS_CONTENTS) == 0) return NULL; size = bfd_section_size (sect); @@ -1289,7 +1289,7 @@ bfd_get_alt_debug_link_info (bfd * abfd, bfd_size_type *buildid_len, sect = bfd_get_section_by_name (abfd, GNU_DEBUGALTLINK); - if (sect == NULL) + if (sect == NULL || (sect->flags & SEC_HAS_CONTENTS) == 0) return NULL; size = bfd_section_size (sect); @@ -1801,7 +1801,8 @@ get_build_id (bfd *abfd) return (struct bfd_build_id *) abfd->build_id; sect = bfd_get_section_by_name (abfd, ".note.gnu.build-id"); - if (sect == NULL) + if (sect == NULL + || (sect->flags & SEC_HAS_CONTENTS) == 0) { bfd_set_error (bfd_error_no_debug_section); return NULL; diff --git a/bfd/peXXigen.c b/bfd/peXXigen.c index fa2b4296e86..43f3a83743c 100644 --- a/bfd/peXXigen.c +++ b/bfd/peXXigen.c @@ -1288,7 +1288,7 @@ pe_print_idata (bfd * abfd, void * vfile) { /* Maybe the extra header isn't there. Look for the section. */ section = bfd_get_section_by_name (abfd, ".idata"); - if (section == NULL) + if (section == NULL || (section->flags & SEC_HAS_CONTENTS) == 0) return true; addr = section->vma; @@ -1845,6 +1845,7 @@ pe_print_pdata (bfd * abfd, void * vfile) int onaline = PDATA_ROW_SIZE; if (section == NULL + || (section->flags & SEC_HAS_CONTENTS) == 0 || coff_section_data (abfd, section) == NULL || pei_section_data (abfd, section) == NULL) return true; @@ -2014,6 +2015,7 @@ _bfd_XX_print_ce_compressed_pdata (bfd * abfd, void * vfile) struct sym_cache cache = {0, 0} ; if (section == NULL + || (section->flags & SEC_HAS_CONTENTS) == 0 || coff_section_data (abfd, section) == NULL || pei_section_data (abfd, section) == NULL) return true; @@ -2147,7 +2149,9 @@ pe_print_reloc (bfd * abfd, void * vfile) asection *section = bfd_get_section_by_name (abfd, ".reloc"); bfd_byte *p, *end; - if (section == NULL || section->size == 0 || !(section->flags & SEC_HAS_CONTENTS)) + if (section == NULL + || section->size == 0 + || (section->flags & SEC_HAS_CONTENTS) == 0) return true; fprintf (file, diff --git a/bfd/pei-x86_64.c b/bfd/pei-x86_64.c index c33d422a449..4d2ba71def9 100644 --- a/bfd/pei-x86_64.c +++ b/bfd/pei-x86_64.c @@ -555,6 +555,7 @@ pex64_bfd_print_pdata_section (bfd *abfd, void *vfile, asection *pdata_section) /* Sanity checks. */ if (pdata_section == NULL + || (pdata_section->flags & SEC_HAS_CONTENTS) == 0 || coff_section_data (abfd, pdata_section) == NULL || pei_section_data (abfd, pdata_section) == NULL) return true; @@ -699,6 +700,7 @@ pex64_bfd_print_pdata_section (bfd *abfd, void *vfile, asection *pdata_section) xdata_section = pex64_get_section_by_rva (abfd, xdata_base, ".text"); /* Transfer xdata section into xdata array. */ if (!xdata_section + || (xdata_section->flags & SEC_HAS_CONTENTS) == 0 || !bfd_malloc_and_get_section (abfd, xdata_section, &xdata)) goto done; diff --git a/bfd/stabs.c b/bfd/stabs.c index da0c61d6c2a..1cce2ae4f3f 100644 --- a/bfd/stabs.c +++ b/bfd/stabs.c @@ -162,7 +162,9 @@ _bfd_link_section_stabs (bfd *abfd, bfd_size_type *pstridx; if (stabsec->size == 0 - || stabstrsec->size == 0) + || stabstrsec->size == 0 + || (stabsec->flags & SEC_HAS_CONTENTS) == 0 + || (stabstrsec->flags & SEC_HAS_CONTENTS) == 0) /* This file does not contain stabs debugging information. */ return true; @@ -520,7 +522,7 @@ _bfd_discard_section_stabs (bfd *abfd, bfd_size_type *pstridx; int deleting; - if (stabsec->size == 0) + if (stabsec->size == 0 || (stabsec->flags & SEC_HAS_CONTENTS) == 0) /* This file does not contain stabs debugging information. */ return false; diff --git a/bfd/xcofflink.c b/bfd/xcofflink.c index b3ab78013ad..a67f24ba847 100644 --- a/bfd/xcofflink.c +++ b/bfd/xcofflink.c @@ -259,7 +259,7 @@ _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd) } lsec = bfd_get_section_by_name (abfd, ".loader"); - if (lsec == NULL) + if (lsec == NULL || (lsec->flags & SEC_HAS_CONTENTS) == 0) { bfd_set_error (bfd_error_no_symbols); return -1; @@ -293,7 +293,7 @@ _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms) } lsec = bfd_get_section_by_name (abfd, ".loader"); - if (lsec == NULL) + if (lsec == NULL || (lsec->flags & SEC_HAS_CONTENTS) == 0) { bfd_set_error (bfd_error_no_symbols); return -1; @@ -378,7 +378,7 @@ _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd) } lsec = bfd_get_section_by_name (abfd, ".loader"); - if (lsec == NULL) + if (lsec == NULL || (lsec->flags & SEC_HAS_CONTENTS) == 0) { bfd_set_error (bfd_error_no_symbols); return -1; @@ -413,7 +413,7 @@ _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd, } lsec = bfd_get_section_by_name (abfd, ".loader"); - if (lsec == NULL) + if (lsec == NULL || (lsec->flags & SEC_HAS_CONTENTS) == 0) { bfd_set_error (bfd_error_no_symbols); return -1; @@ -904,7 +904,7 @@ xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info) o_snloader field in the a.out header, rather than grabbing the section by name. */ lsec = bfd_get_section_by_name (abfd, ".loader"); - if (lsec == NULL) + if (lsec == NULL || (lsec->flags & SEC_HAS_CONTENTS) == 0) { _bfd_error_handler (_("%pB: dynamic object with no .loader section"), @@ -2373,7 +2373,7 @@ xcoff_link_check_dynamic_ar_symbols (bfd *abfd, *pneeded = false; lsec = bfd_get_section_by_name (abfd, ".loader"); - if (lsec == NULL) + if (lsec == NULL || (lsec->flags & SEC_HAS_CONTENTS) == 0) /* There are no symbols, so don't try to include it. */ return true; @@ -4128,7 +4128,9 @@ bfd_xcoff_build_dynamic_sections (bfd *output_bfd, { /* Grab the contents of SUB's .debug section, if any. */ subdeb = bfd_get_section_by_name (sub, ".debug"); - if (subdeb != NULL && subdeb->size > 0) + if (subdeb != NULL + && subdeb->size != 0 + && (subdeb->flags & SEC_HAS_CONTENTS) != 0) { /* We use malloc and copy the names into the debug stringtab, rather than bfd_alloc, because I expect