From patchwork Wed Oct 16 13:41:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Matz X-Patchwork-Id: 99008 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 939763857021 for ; Wed, 16 Oct 2024 13:42:12 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id 4038F3858C5F for ; Wed, 16 Oct 2024 13:41:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4038F3858C5F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4038F3858C5F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729086088; cv=none; b=otL2AUXEeZ2KiHTIETA2fBOd3NwMkY4y03xfCSfj79NMOodVXPPtl9j19JcW0e/huw7NtGvpfxh4WONQ9joo+T45y7jAXHTSo7fAgy+xyqfgir+1gkPNnwnMVimUTjaoioaywzDkWvrYFxF3U/Njrj8cpXWNAKWd2j6joyK+xWc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729086088; c=relaxed/simple; bh=YgDUclW3UFpfwvQWhykMq8AMdwpm5LMFuBUKgUQ6FVs=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:Message-ID:MIME-Version; b=bd7XBxOIFH7j87IvUCY2Fy2u+4MAovg3YEuBuRosLlzIyM2jXQu5nIriEJnF/+pr3Vod/wnP4tKqSscoaUpOGqWMT2OuH2Na0nMgk2LIGOx7Tb4e6GMflIxNeN0dPWKXohuMOmy7ELsaWv8+vu6wiyZw9YuLH9H+1wqHn2fADZY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from knuth.suse.de (unknown [IPv6:2a07:de40:a101:3:9249:faff:fe06:959]) by smtp-out1.suse.de (Postfix) with ESMTP id 1C29D21E90 for ; Wed, 16 Oct 2024 13:41:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1729086083; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=DBPZAwX06T6oAmwOd++wgH6hpYaZZ9XNb0X5qXtyt8M=; b=xF769gH4s75riEdM6BUbFbj3tKt11mW5bKD9Swefh+YfpFF5HyEXsZxcSiL11Yc9feW/fp Gvra02yOKmToEUZVZiOQ86aYoW2CIQHYyys58qPS+GQCgnB3JC1frrf4eAMCW6+Fq5wfwc +SpL3ntNz1W9CRFIVV3HOI560dp52uM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1729086083; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=DBPZAwX06T6oAmwOd++wgH6hpYaZZ9XNb0X5qXtyt8M=; b=JZNznjh02m3Q2US0Hp/QE13dcRAnppUaiKlxbFXcIVSZk1AbMG/TlL9J1S/78JX4qW7sYT a66SYoC2QJKv/3BQ== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=xF769gH4; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=JZNznjh0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1729086083; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=DBPZAwX06T6oAmwOd++wgH6hpYaZZ9XNb0X5qXtyt8M=; b=xF769gH4s75riEdM6BUbFbj3tKt11mW5bKD9Swefh+YfpFF5HyEXsZxcSiL11Yc9feW/fp Gvra02yOKmToEUZVZiOQ86aYoW2CIQHYyys58qPS+GQCgnB3JC1frrf4eAMCW6+Fq5wfwc +SpL3ntNz1W9CRFIVV3HOI560dp52uM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1729086083; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=DBPZAwX06T6oAmwOd++wgH6hpYaZZ9XNb0X5qXtyt8M=; b=JZNznjh02m3Q2US0Hp/QE13dcRAnppUaiKlxbFXcIVSZk1AbMG/TlL9J1S/78JX4qW7sYT a66SYoC2QJKv/3BQ== Received: by knuth.suse.de (Postfix, from userid 10510) id 075A4529437; Wed, 16 Oct 2024 15:41:22 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by knuth.suse.de (Postfix) with ESMTP id EB486529436 for ; Wed, 16 Oct 2024 15:41:22 +0200 (CEST) Date: Wed, 16 Oct 2024 15:41:22 +0200 (CEST) From: Michael Matz To: binutils@sourceware.org Subject: PR32260: Improve error handling on string merging Message-ID: <0515740f-e90d-5c84-0714-f46c4f830354@suse.de> MIME-Version: 1.0 X-Spamd-Result: default: False [1.39 / 50.00]; BAYES_HAM(-3.00)[99.99%]; HFILTER_HOSTNAME_UNKNOWN(2.50)[]; RDNS_NONE(2.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; HFILTER_HELO_IP_A(1.00)[knuth.suse.de]; HFILTER_HELO_NORES_A_OR_MX(0.30)[knuth.suse.de]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; RCVD_NO_TLS_LAST(0.10)[]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; RECEIVED_HELO_LOCALHOST(0.00)[]; ARC_NA(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DKIM_TRACE(0.00)[suse.de:+]; MISSING_XM_UA(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; PREVIOUSLY_DELIVERED(0.00)[binutils@sourceware.org]; TO_DN_NONE(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:dkim,suse.de:mid] X-Spam-Score: 1.39 X-Spamd-Bar: + X-Rspamd-Queue-Id: 1C29D21E90 X-Rspamd-Action: no action X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spam-Status: No, score=-9.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces~patchwork=sourceware.org@sourceware.org if the input sections are near the max supported size (4G) we might fail to enlarge the hash table. The error handling for this case didn't quite work. When this happens we can gracefully fall back to just not deduplicate this section (and continue with further mergable sections). We were mixing that with the case of not being able to even allocate a small structure (in which case we can as well error out completely), this disentables both cases. bfd/ PR ld/32260 * merge.c (sec_merge_maybe_resize): Check overflow in ultimate target type. (record_section): Return three-state, use new state when unable to enlarge hash table. (_bfd_merge_sections): Remove current section from merging consideration when hashtable can't be enlarged. --- Tested without regressions on Alans list of targets. Okay for master? bfd/merge.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/bfd/merge.c b/bfd/merge.c index 5c8e69bedd9..c811bc57eae 100644 --- a/bfd/merge.c +++ b/bfd/merge.c @@ -165,7 +165,7 @@ struct sec_merge_sec_info /* Given a merge hash table TABLE and a number of entries to be ADDED, possibly resize the table for this to fit without further - resizing. */ + resizing. Returns false if that can't be done for whatever reason. */ static bool sec_merge_maybe_resize (struct sec_merge_hash *table, unsigned added) @@ -174,17 +174,18 @@ sec_merge_maybe_resize (struct sec_merge_hash *table, unsigned added) if (NEEDS_RESIZE (bfdtab->count + added, table->nbuckets)) { unsigned i; - unsigned long newnb = table->nbuckets * 2; + unsigned long newnb = table->nbuckets; struct sec_merge_hash_entry **newv; uint64_t *newl; unsigned long alloc; - while (NEEDS_RESIZE (bfdtab->count + added, newnb)) + do { newnb *= 2; - if (!newnb) + if (!(unsigned int)newnb) return false; } + while (NEEDS_RESIZE (bfdtab->count + added, newnb)); alloc = newnb * sizeof (newl[0]); if (alloc / sizeof (newl[0]) != newnb) @@ -698,9 +699,12 @@ _bfd_add_merge_section (bfd *abfd, void **psinfo, asection *sec, } /* Record one whole input section (described by SECINFO) into the hash table - SINFO. */ + SINFO. Returns 0 on hard errors (no sense in continuing link), + 1 when section is completely recorded, and 2 when the section wasn't + recorded but we can continue (e.g. by simply not deduplicating this + section). */ -static bool +static int record_section (struct sec_merge_info *sinfo, struct sec_merge_sec_info *secinfo) { @@ -737,8 +741,8 @@ record_section (struct sec_merge_info *sinfo, merged into this area will make use of that as well. */ if (!sec_merge_maybe_resize (sinfo->htab, 1 + sec->size / 2)) { - bfd_set_error (bfd_error_no_memory); - goto error_return; + free (contents); + return 2; } /* Walk through the contents, calculate hashes and length of all @@ -788,14 +792,14 @@ record_section (struct sec_merge_info *sinfo, /*printf ("ZZZ %s:%s %u entries\n", sec->owner->filename, sec->name, (unsigned)secinfo->noffsetmap);*/ - return true; + return 1; error_return: free (contents); contents = NULL; for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next) *secinfo->psecinfo = NULL; - return false; + return 0; } /* qsort comparison function. Won't ever return zero as all entries @@ -991,9 +995,16 @@ _bfd_merge_sections (bfd *abfd, } else { - if (!record_section (sinfo, secinfo)) + int e = record_section (sinfo, secinfo); + if (e == 0) return false; - if (align) + if (e == 2) + { + *secinfo->psecinfo = NULL; + if (remove_hook) + (*remove_hook) (abfd, secinfo->sec); + } + else if (align) { unsigned int opb = bfd_octets_per_byte (abfd, secinfo->sec); @@ -1043,7 +1054,8 @@ _bfd_merge_sections (bfd *abfd, /* Finally remove all input sections which have not made it into the hash table at all. */ for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next) - if (secinfo->first_str == NULL) + if (secinfo->first_str == NULL + && secinfo->sec->sec_info_type == SEC_INFO_TYPE_MERGE) secinfo->sec->flags |= SEC_EXCLUDE | SEC_KEEP; }