From patchwork Wed May 3 06:03:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 89573 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1103310vqo; Tue, 2 May 2023 23:04:09 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7j5V2ggWxQ1P06tS7g/xvxZcXD75ONy9FpNP8IEMRVmfJ1FVoOml2KCZqk2GPd0l7ktekW X-Received: by 2002:a17:906:ee86:b0:94f:64ea:cdc9 with SMTP id wt6-20020a170906ee8600b0094f64eacdc9mr2057467ejb.6.1683093849628; Tue, 02 May 2023 23:04:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683093849; cv=none; d=google.com; s=arc-20160816; b=sWuQm6Fo2ZCZEH80wvJQ7uekVbMHwlaH6jk34RJQqkNZtMcugZxXg0rT0EFhtvvrpO KLd/AAKOy/R4MZDTZCBToa/m+5tNSd0iSITnFnjoUPGmA9BF1fII8zOjZCD/bU0rLJcF 4Y9TDJ0/hMVTx+Pykms2Pef/W4mSbuaNHXggMIMg2jRfS9Q9MjHmb1T/DDL8YtMXTxlK iF4uMUuhF5shx3iH8tXTZ1xLr9rMIJYoudEmPZ25GunfA9Pg1gB6zd/kw9k7HbN1vCLc 06vAaM9k0LlOMhwxHbWQBIyVDMIiA58EARMzHDGP+H60J08QyJl3zjL3zq5K72h5E//9 gnzA== 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=1LP/o9/dqCWrToAYJESCOrPEoeJMk30ds7A1IPApWa0=; b=v1VvRg7ONWIMQaePGTNTqUcFLBBt5ky4ANnbohwEs90kTDgO7a/r2kEeECps9ImSY8 gfEPEFetEWT2rRzs+/MJIB999b7v8PiBSvTpQSS7IiaoV4A4NGAdTs2sIfupIgh9+rRg IA+6ae/Id+ZARfe0sSak/nu2af66I9SBT9RBtfgABOykgtZZGJIDpw6FXFlPNsL9OcP9 CgKLa3Rj0z8IrHq82J1oBcTky49n+2Y685bjkr3l3kq77sgs6MzIirkdxnyPCw1RsQ7K dnAwJS91dw8Fhpqk8NpeQ869G86+WgEk2sWrDJ1oOtgxxM8LmIrInwEJ1OSzypSYZAWp LwSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=KWQAtvB2; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id go20-20020a1709070d9400b00959aba1c40fsi20221113ejc.49.2023.05.02.23.04.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 May 2023 23:04:09 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=KWQAtvB2; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 614EA38555A4 for ; Wed, 3 May 2023 06:03:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 614EA38555A4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1683093811; bh=1LP/o9/dqCWrToAYJESCOrPEoeJMk30ds7A1IPApWa0=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=KWQAtvB2QlQ7laAM8tPiHrBl2HDIKIDYRJEKFALCQ3WDdgJl1RgMq3TE0PmJOPBZ9 IS4lrGBNnF8hH4XE+MI1ZK2N9kgz11oG7BaYekMK4bmb++T3Jo33qIWWrWt3u0no+W eLCd9oYXlzrIvb5xR1JoO6VNbUvRKCpO1C1xZMy8= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by sourceware.org (Postfix) with ESMTPS id 328B93857437 for ; Wed, 3 May 2023 06:03:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 328B93857437 Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-52160f75920so3286946a12.2 for ; Tue, 02 May 2023 23:03:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683093802; x=1685685802; 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=1LP/o9/dqCWrToAYJESCOrPEoeJMk30ds7A1IPApWa0=; b=mE0HusuS9Rczp2vlKFe2Z/4D+l2IIbD56+TfNC/3YSK62eIns2/j6r3VJFIpwksHsk 0L+fYze771vgbeE3Tyd7Ixf7GlCg3gzTsVql/aEisHsGbfccdM81s0TTghYFiXen0e/g jmPI7HdOuWhpd/HulmKJC9bh3NWyP4MQR67j9dR7tccOjjKGlQwdW5yyxG0WDWKiq0M9 jVOVuyeD44Qzqr5sz7LVGadoAAylicpyifZTrXl4mFqYsejClPwg6hpBKuSfUHMRifLT ReXQatmvPbhO6NT2KPhgTOVhxTPhzZCTjOFKElXBbMNFwpHmHJv8tyFlGMtYCHxOIEPH YI3w== X-Gm-Message-State: AC+VfDx+8tHfQz9EvFX02HE18WPWPZqcsNeGWweXXbzgol1S5VKp1HuI 0AF51iOSavHnXqrNn0JRJxCuLq4Q0gc= X-Received: by 2002:a05:6a00:2e1e:b0:643:587:f48 with SMTP id fc30-20020a056a002e1e00b0064305870f48mr4277725pfb.3.1683093801603; Tue, 02 May 2023 23:03:21 -0700 (PDT) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:e289:4864:2d83:727f]) by smtp.gmail.com with ESMTPSA id ei28-20020a056a0080dc00b0063d24fcc2besm22720412pfb.125.2023.05.02.23.03.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 May 2023 23:03:20 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id ADC141142C83; Wed, 3 May 2023 15:33:18 +0930 (ACST) Date: Wed, 3 May 2023 15:33:18 +0930 To: binutils@sourceware.org Subject: Move bfd_alloc, bfd_zalloc and bfd_release to libbfd.c Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3034.4 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?1764851816519134700?= X-GMAIL-MSGID: =?utf-8?q?1764851816519134700?= These functions don't belong in opncls.c. * libbfd-in.h (bfd_release): Delete prototype. * opncls.c (bfd_alloc, bfd_zalloc, bfd_release): Move to.. * libbfd.c: ..here. Include objalloc.c and provide bfd_release with a FUNCTION comment. * bfd-in2.h: Regenerate. * libbfd.h: Regenerate. diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index 470a3cc9d3b..0c1844d5ca4 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -495,10 +495,6 @@ bool bfd_make_writable (bfd *abfd); bool bfd_make_readable (bfd *abfd); -void *bfd_alloc (bfd *abfd, bfd_size_type wanted); - -void *bfd_zalloc (bfd *abfd, bfd_size_type wanted); - uint32_t bfd_calc_gnu_debuglink_crc32 (uint32_t crc, const bfd_byte *buf, bfd_size_type len); @@ -523,6 +519,12 @@ char *bfd_follow_build_id_debuglink (bfd *abfd, const char *dir); const char *bfd_set_filename (bfd *abfd, const char *filename); /* Extracted from libbfd.c. */ +void *bfd_alloc (bfd *abfd, bfd_size_type wanted); + +void *bfd_zalloc (bfd *abfd, bfd_size_type wanted); + +void bfd_release (bfd *, void *); + /* Byte swapping macros for user section data. */ diff --git a/bfd/libbfd-in.h b/bfd/libbfd-in.h index 4305b8416ea..ca15b8b0a23 100644 --- a/bfd/libbfd-in.h +++ b/bfd/libbfd-in.h @@ -119,10 +119,6 @@ bfd_strdup (const char *str) memcpy (buf, str, len); return buf; } -/* These routines allocate and free things on the BFD's objalloc. */ - -extern void bfd_release - (bfd *, void *) ATTRIBUTE_HIDDEN; extern bfd * _bfd_create_empty_archive_element_shell (bfd *) ATTRIBUTE_HIDDEN; diff --git a/bfd/libbfd.c b/bfd/libbfd.c index e21cd988208..66ef32469fb 100644 --- a/bfd/libbfd.c +++ b/bfd/libbfd.c @@ -22,6 +22,7 @@ #include "sysdep.h" #include "bfd.h" #include "libbfd.h" +#include "objalloc.h" #ifndef HAVE_GETPAGESIZE #define getpagesize() 2048 @@ -416,6 +417,87 @@ bfd_zmalloc (bfd_size_type size) return ptr; } +/* +FUNCTION + bfd_alloc + +SYNOPSIS + void *bfd_alloc (bfd *abfd, bfd_size_type wanted); + +DESCRIPTION + Allocate a block of @var{wanted} bytes of memory attached to + <> and return a pointer to it. +*/ + +void * +bfd_alloc (bfd *abfd, bfd_size_type size) +{ + void *ret; + unsigned long ul_size = (unsigned long) size; + + if (size != ul_size + /* Note - although objalloc_alloc takes an unsigned long as its + argument, internally the size is treated as a signed long. This can + lead to problems where, for example, a request to allocate -1 bytes + can result in just 1 byte being allocated, rather than + ((unsigned long) -1) bytes. Also memory checkers will often + complain about attempts to allocate a negative amount of memory. + So to stop these problems we fail if the size is negative. */ + || ((signed long) ul_size) < 0) + { + bfd_set_error (bfd_error_no_memory); + return NULL; + } + + ret = objalloc_alloc ((struct objalloc *) abfd->memory, ul_size); + if (ret == NULL) + bfd_set_error (bfd_error_no_memory); + else + abfd->alloc_size += size; + return ret; +} + +/* +FUNCTION + bfd_zalloc + +SYNOPSIS + void *bfd_zalloc (bfd *abfd, bfd_size_type wanted); + +DESCRIPTION + Allocate a block of @var{wanted} bytes of zeroed memory + attached to <> and return a pointer to it. +*/ + +void * +bfd_zalloc (bfd *abfd, bfd_size_type size) +{ + void *res; + + res = bfd_alloc (abfd, size); + if (res) + memset (res, 0, (size_t) size); + return res; +} + +/* +FUNCTION + bfd_release + +SYNOPSIS + void bfd_release (bfd *, void *); + +DESCRIPTION + Free a block allocated for a BFD. + Note: Also frees all more recently allocated blocks! +*/ + +void +bfd_release (bfd *abfd, void *block) +{ + objalloc_free_block ((struct objalloc *) abfd->memory, block); +} + /* INTERNAL_FUNCTION bfd_write_bigendian_4byte_int diff --git a/bfd/libbfd.h b/bfd/libbfd.h index aceec4ab9c0..3bd79d2d3bd 100644 --- a/bfd/libbfd.h +++ b/bfd/libbfd.h @@ -125,10 +125,6 @@ bfd_strdup (const char *str) memcpy (buf, str, len); return buf; } -/* These routines allocate and free things on the BFD's objalloc. */ - -extern void bfd_release - (bfd *, void *) ATTRIBUTE_HIDDEN; extern bfd * _bfd_create_empty_archive_element_shell (bfd *) ATTRIBUTE_HIDDEN; diff --git a/bfd/opncls.c b/bfd/opncls.c index 602dc80a6c4..b7b9d8f2d0b 100644 --- a/bfd/opncls.c +++ b/bfd/opncls.c @@ -1042,79 +1042,6 @@ bfd_make_readable (bfd *abfd) return true; } -/* -FUNCTION - bfd_alloc - -SYNOPSIS - void *bfd_alloc (bfd *abfd, bfd_size_type wanted); - -DESCRIPTION - Allocate a block of @var{wanted} bytes of memory attached to - <> and return a pointer to it. -*/ - -void * -bfd_alloc (bfd *abfd, bfd_size_type size) -{ - void *ret; - unsigned long ul_size = (unsigned long) size; - - if (size != ul_size - /* Note - although objalloc_alloc takes an unsigned long as its - argument, internally the size is treated as a signed long. This can - lead to problems where, for example, a request to allocate -1 bytes - can result in just 1 byte being allocated, rather than - ((unsigned long) -1) bytes. Also memory checkers will often - complain about attempts to allocate a negative amount of memory. - So to stop these problems we fail if the size is negative. */ - || ((signed long) ul_size) < 0) - { - bfd_set_error (bfd_error_no_memory); - return NULL; - } - - ret = objalloc_alloc ((struct objalloc *) abfd->memory, ul_size); - if (ret == NULL) - bfd_set_error (bfd_error_no_memory); - else - abfd->alloc_size += size; - return ret; -} - -/* -FUNCTION - bfd_zalloc - -SYNOPSIS - void *bfd_zalloc (bfd *abfd, bfd_size_type wanted); - -DESCRIPTION - Allocate a block of @var{wanted} bytes of zeroed memory - attached to <> and return a pointer to it. -*/ - -void * -bfd_zalloc (bfd *abfd, bfd_size_type size) -{ - void *res; - - res = bfd_alloc (abfd, size); - if (res) - memset (res, 0, (size_t) size); - return res; -} - -/* Free a block allocated for a BFD. - Note: Also frees all more recently allocated blocks! */ - -void -bfd_release (bfd *abfd, void *block) -{ - objalloc_free_block ((struct objalloc *) abfd->memory, block); -} - - /* GNU Extension: separate debug-info files