From patchwork Sat May 20 01:18:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 96698 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp69388vqo; Fri, 19 May 2023 18:18:46 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5tj0eRqfmb6wonnSxKZ4pnqu2MtHwWopR+Vh5yg4/0J9KtRg4G5hklB7Gl4Cj5RjjopCY1 X-Received: by 2002:aa7:c716:0:b0:50b:c58a:a7ae with SMTP id i22-20020aa7c716000000b0050bc58aa7aemr3117137edq.4.1684545526186; Fri, 19 May 2023 18:18:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684545526; cv=none; d=google.com; s=arc-20160816; b=iBH7rd8RBmRk3BwDCiUfen3Gok09+CQVQYj18ssV1qSB+VEvmB3F1c0lkn0kT6fcZG Q0AZgB3zYwEHu3rXWIOHdAVdZH9bbSAEhY740LA2per0W5rK1DQDon+K7dxCOHYqDwDm /r/39RWm89VC7eBaKkLEz6o5r5H4fWylFHmS0EcRBZMAHQmDkKulmxkWiQzTtQOxqHDi 656vARD6FgvkgbPHPLbRXHwapnbm3zuQl9yImP4/E0h9OYh9U6y/Ql5v+jFT9X2MMu7M NOE2JEY3rfBGiF9IUmT4fs42ajAy5Lp4MGF9SpBOSfcTWyLo6Sv8GfMIo2dWBd825j1l HDvA== 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=aOGjEZByNGCWoRfWYJPbQEcPHhfYeble2dbaq3RXxtA=; b=ha8EjTTEWNcL+B8YYmIDrvBbOz2zjyAt/Xdy824hYcsFopVyN6AvnkslRdh208GFZS cCD0+wVuJjWSCWJjxqBltr1xhyohXy+LUvyuh2vQV2fwfkzdWIQHaFyvxvE5XE2yPj2W Ep/X6JsuCstLGBRiQHi0o0N48oqKmPylNA1wYNO2Jift9C02ITlcHFdfF1PjPlopQhjD ELrFBX/WIbNrwu72Pc8zw/Y3lpfEiPDvQndLSlvkOgsisL0s3pSH8EbBV7gY77BoXaAR H0QXNVK9wlTHS9MlVIn3k747L6acXNp0XF8Jfh7Q6Sts1vrOdzqn5d9sOYq1W0gs//q7 HSew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=IFA4cx8w; 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 bm21-20020a0564020b1500b0050d89daaa66si590011edb.272.2023.05.19.18.18.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 May 2023 18:18:46 -0700 (PDT) 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=IFA4cx8w; 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 0054B3857706 for ; Sat, 20 May 2023 01:18:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0054B3857706 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684545523; bh=aOGjEZByNGCWoRfWYJPbQEcPHhfYeble2dbaq3RXxtA=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IFA4cx8w2RJJIIMEQ1U1Ouw3p8vkpdnccRlwV9CwghEsaSbALDeNLD2hvSSk0Gw8u JnGgk0MjcPoMseqIPsNXb1DVJf3UFDyDGkdEE5Nrt+HYzJZsJCyTyom5xXB5x4CAE3 AbvvoTl3xcFD0zWFEL0WJBAyw09Om4yRVOKaElU8= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by sourceware.org (Postfix) with ESMTPS id 3D92D3858C5F for ; Sat, 20 May 2023 01:18:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3D92D3858C5F Received: by mail-pg1-x531.google.com with SMTP id 41be03b00d2f7-51f1b6e8179so2751255a12.3 for ; Fri, 19 May 2023 18:18:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684545513; x=1687137513; 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=aOGjEZByNGCWoRfWYJPbQEcPHhfYeble2dbaq3RXxtA=; b=h1iIlf4biQnD0674mVRB+qpQ9YJ3bt9UK3zaxj5Fo0kNoH9gqtipKSToUyRMhOOQBJ I+MXfMv6nN6DHOx1JjgF4QIa4QenVQ0xJ8AkLR07+2zXfXE5CFq7bgOEzvET9cF7+c9E BaH1y9nwzlB45aEuTy6NU3miEOjRiOdSLUxLjumACGyivBrKUiaQY0kNYRHqFLXwR2je PGU6TCT4N/Xjo3ciBzZukXsP3gKPOr7FdO8sXGpeSXlDJ9Rq0lNFhHl6QyTW40CDczLC UYy1hstwMBSsOFGWRq21OwSn1SMSSXPxCGUf2pTurPl3NCEy/1V8IbTYrEfCGBQEOBoY 7VLA== X-Gm-Message-State: AC+VfDz01wVMa/VG2iDBhTXrL5/1coht18J1Avq1OKXD7B3bafB59nK/ U3538TfkGYgVW4MeC6JE1fWKQgf0oI0= X-Received: by 2002:a17:903:2306:b0:1ac:656f:a697 with SMTP id d6-20020a170903230600b001ac656fa697mr4720125plh.21.1684545512606; Fri, 19 May 2023 18:18:32 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id jb14-20020a170903258e00b001ac8218ddb7sm257718plb.84.2023.05.19.18.18.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 May 2023 18:18:31 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 99BF21142BF8; Sat, 20 May 2023 10:48:29 +0930 (ACST) Date: Sat, 20 May 2023 10:48:29 +0930 To: binutils@sourceware.org Subject: coffcode.h handle_COMDAT tidy 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, 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-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?1766374009301842173?= X-GMAIL-MSGID: =?utf-8?q?1766374009301842173?= I started down the path of attempting to fix https://sourceware.org/pipermail/binutils/2023-April/127263.html but decided after a while that I didn't want to mess with this code.. This patch is a just a few things that I thought worth doing, the main one being reporting of errors up the call chain. The while loop to for loop change is shamelessly stolen from Oleg. * coffcode.h (handle_COMDAT): Return bool. Make sec_flags a flagword*, and adjust to suit. Replace while loop with for loop. Check isym.n_numaux before reading aux entries. Alloc coff_comdat_info and name in one call to bfd_alloc. Remove goto breakloop. (styp_to_sec_flags): Adjust handle_COMDAT call. diff --git a/bfd/coffcode.h b/bfd/coffcode.h index ab22ed092d6..e52d652616e 100644 --- a/bfd/coffcode.h +++ b/bfd/coffcode.h @@ -852,9 +852,9 @@ styp_to_sec_flags (bfd *abfd, #else /* COFF_WITH_PE */ -static flagword +static bool handle_COMDAT (bfd * abfd, - flagword sec_flags, + flagword *sec_flags, void * hdr, const char *name, asection *section) @@ -864,7 +864,7 @@ handle_COMDAT (bfd * abfd, int seen_state = 0; char *target_name = NULL; - sec_flags |= SEC_LINK_ONCE; + *sec_flags |= SEC_LINK_ONCE; /* Unfortunately, the PE format stores essential information in the symbol table, of all places. We need to extract that @@ -884,18 +884,19 @@ handle_COMDAT (bfd * abfd, rather messy. */ if (! _bfd_coff_get_external_symbols (abfd)) - return sec_flags; + return true; esymstart = esym = (bfd_byte *) obj_coff_external_syms (abfd); esymend = esym + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd); - while (esym < esymend) + for (struct internal_syment isym; + esym < esymend; + esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd)) { - struct internal_syment isym; char buf[SYMNMLEN + 1]; const char *symname; - bfd_coff_swap_sym_in (abfd, esym, & isym); + bfd_coff_swap_sym_in (abfd, esym, &isym); BFD_ASSERT (sizeof (internal_s->s_name) <= SYMNMLEN); @@ -933,7 +934,7 @@ handle_COMDAT (bfd * abfd, { _bfd_error_handler (_("%pB: unable to load COMDAT section name"), abfd); - break; + return false; } switch (seen_state) @@ -968,7 +969,7 @@ handle_COMDAT (bfd * abfd, cf PR 21781. */ _bfd_error_handler (_("%pB: error: unexpected symbol '%s' in COMDAT section"), abfd, symname); - goto breakloop; + return false; } /* FIXME LATER: MSVC generates section names @@ -982,22 +983,8 @@ handle_COMDAT (bfd * abfd, " does not match section name '%s'"), abfd, symname, name); - seen_state = 1; - - /* PR 17512: file: e2cfe54f. */ - if (esym + bfd_coff_symesz (abfd) >= esymend) - { - /* xgettext:c-format */ - _bfd_error_handler (_("%pB: warning: no symbol for" - " section '%s' found"), - abfd, symname); - break; - } /* This is the section symbol. */ - bfd_coff_swap_aux_in (abfd, (esym + bfd_coff_symesz (abfd)), - isym.n_type, isym.n_sclass, - 0, isym.n_numaux, & aux); - + seen_state = 1; target_name = strchr (name, '$'); if (target_name != NULL) { @@ -1007,6 +994,24 @@ handle_COMDAT (bfd * abfd, target_name += 1; } + if (isym.n_numaux == 0) + aux.x_scn.x_comdat = 0; + else + { + /* PR 17512: file: e2cfe54f. */ + if (esym + bfd_coff_symesz (abfd) >= esymend) + { + /* xgettext:c-format */ + _bfd_error_handler (_("%pB: warning: no symbol for" + " section '%s' found"), + abfd, symname); + break; + } + bfd_coff_swap_aux_in (abfd, esym + bfd_coff_symesz (abfd), + isym.n_type, isym.n_sclass, + 0, isym.n_numaux, &aux); + } + /* FIXME: Microsoft uses NODUPLICATES and ASSOCIATIVE, but gnu uses ANY and SAME_SIZE. Unfortunately, gnu doesn't do @@ -1027,23 +1032,23 @@ handle_COMDAT (bfd * abfd, { case IMAGE_COMDAT_SELECT_NODUPLICATES: #ifdef STRICT_PE_FORMAT - sec_flags |= SEC_LINK_DUPLICATES_ONE_ONLY; + *sec_flags |= SEC_LINK_DUPLICATES_ONE_ONLY; #else - sec_flags &= ~SEC_LINK_ONCE; + *sec_flags &= ~SEC_LINK_ONCE; #endif break; case IMAGE_COMDAT_SELECT_ANY: - sec_flags |= SEC_LINK_DUPLICATES_DISCARD; + *sec_flags |= SEC_LINK_DUPLICATES_DISCARD; break; case IMAGE_COMDAT_SELECT_SAME_SIZE: - sec_flags |= SEC_LINK_DUPLICATES_SAME_SIZE; + *sec_flags |= SEC_LINK_DUPLICATES_SAME_SIZE; break; case IMAGE_COMDAT_SELECT_EXACT_MATCH: /* Not yet fully implemented ??? */ - sec_flags |= SEC_LINK_DUPLICATES_SAME_CONTENTS; + *sec_flags |= SEC_LINK_DUPLICATES_SAME_CONTENTS; break; /* debug$S gets this case; other @@ -1056,16 +1061,16 @@ handle_COMDAT (bfd * abfd, case IMAGE_COMDAT_SELECT_ASSOCIATIVE: #ifdef STRICT_PE_FORMAT /* FIXME: This is not currently implemented. */ - sec_flags |= SEC_LINK_DUPLICATES_DISCARD; + *sec_flags |= SEC_LINK_DUPLICATES_DISCARD; #else - sec_flags &= ~SEC_LINK_ONCE; + *sec_flags &= ~SEC_LINK_ONCE; #endif break; default: /* 0 means "no symbol" */ /* debug$F gets this case; other implications ??? */ - sec_flags |= SEC_LINK_DUPLICATES_DISCARD; + *sec_flags |= SEC_LINK_DUPLICATES_DISCARD; break; } } @@ -1082,7 +1087,6 @@ handle_COMDAT (bfd * abfd, symname + (TARGET_UNDERSCORE ? 1 : 0)) != 0) { /* Not the name we're looking for */ - esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd); continue; } /* Fall through. */ @@ -1090,42 +1094,30 @@ handle_COMDAT (bfd * abfd, /* MSVC mode: the lexically second symbol (or drop through from the above). */ { - char *newname; - size_t amt; - /* This must the second symbol with the section #. It is the actual symbol name. Intel puts the two adjacent, but Alpha (at least) spreads them out. */ - amt = sizeof (struct coff_comdat_info); - coff_section_data (abfd, section)->comdat - = (struct coff_comdat_info *) bfd_alloc (abfd, amt); - if (coff_section_data (abfd, section)->comdat == NULL) - abort (); - - coff_section_data (abfd, section)->comdat->symbol = - (esym - esymstart) / bfd_coff_symesz (abfd); + struct coff_comdat_info *comdat; + size_t len = strlen (symname) + 1; - amt = strlen (symname) + 1; - newname = (char *) bfd_alloc (abfd, amt); - if (newname == NULL) - abort (); + comdat = bfd_alloc (abfd, sizeof (*comdat) + len); + if (comdat == NULL) + return false; - strcpy (newname, symname); - coff_section_data (abfd, section)->comdat->name - = newname; + coff_section_data (abfd, section)->comdat = comdat; + comdat->symbol = (esym - esymstart) / bfd_coff_symesz (abfd); + char *newname = (char *) (comdat + 1); + comdat->name = newname; + memcpy (newname, symname, len); + return true; } - - goto breakloop; } } - - esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd); } - breakloop: - return sec_flags; + return true; } @@ -1276,7 +1268,8 @@ styp_to_sec_flags (bfd *abfd, break; case IMAGE_SCN_LNK_COMDAT: /* COMDAT gets very special treatment. */ - sec_flags = handle_COMDAT (abfd, sec_flags, hdr, name, section); + if (!handle_COMDAT (abfd, &sec_flags, hdr, name, section)) + result = false; break; default: /* Silently ignore for now. */