From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id 1316738708B7 for ; Tue, 9 Apr 2024 23:01:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1316738708B7 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1316738708B7 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::430 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712703710; cv=none; b=D6IfDLS91iDUTT5qKHWHoTYn/iU2VSXF3jv+UyW+OXGAT3R2Bp1d2MGa2sXFfn/xBB4bI28mS22UDh5M3ad58zl0OEl+0x29yqTtbGwjH9k9yhTqcVGZzNJdAvpb30wx/0/zo7kjCD3DUzv2sKKGrux6hCTewNBZ5GYCEkFNjwU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712703710; c=relaxed/simple; bh=BgTsVF6LWIPNhScYGKThHuXJlefMQhipVmRazHrimpk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=l2VYcYhEqOkJJH4TEGzBcm7rUynREVRJ7NvPh08O1s+w54dA/VuxL9QrG4qxbCpUQwtCY6fYJzksBreoEiAESF4u+SebzOWhAODx3UYTm3HdxzyPSnFhQPvt1O0rDCuQDNsK34xVeAti4Q1XIMwb88nq9CEbp6sRYvHWwdZovgg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-6ed9fc77bbfso653105b3a.1 for ; Tue, 09 Apr 2024 16:01:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712703707; x=1713308507; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=VQ9JJCzjyiHkDTdhVmOUFU/rqIOl5bLBBk+btoxQ/Yg=; b=IWKaytbeKJQTL25JREJnHSN+X2j8l5ieA16LUyIK8AnKe5w+3EOkRyDgJ/teyUW9xm FW1Itt+g9SvQWqZi2rPwFNSca5IfFtf8IyrWRFDTCeVoBBV19djPK2TLkA157FUOTmbV r3XfzGyjBuh4cnI7OhyxVQ+/yzCEL69FsUYy7jUZ7rogDfTdUGgq2b7wKquJM0uErzLE CFhxmw9QIjP1i5sNZ/4BaPXbOszND1bVy4/IB2YD+O692kRwePh4i7qfBxDT3TvWP5pH o4uEj/cAiXEGe2hct+shtGJxBDZCGS12p2GqlX0juGS6bqLBY26+A39t9wld3c7XfRaQ Ipmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712703707; x=1713308507; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VQ9JJCzjyiHkDTdhVmOUFU/rqIOl5bLBBk+btoxQ/Yg=; b=ppgp+vJwecPhX8NExzOreSXIsdgQruSDVHZ6QCIoy9TkMBKOd94bsAt120ceanIm9h 57Z/vLyFo2egGFyc/5GO9wLBUTE1pRquwRbNbwp6P006tiXMjJua8lr/IisRaUK2PFsD lidkTGFZzf7+9o+BqfsWMZtHeANJKBhwWlUfcj/dt6VEWP5T+x8636nh15ctSxOH6sBF xVGLa9Sd5gqsbFNyJnkK910Uf1EJCZHOkl51PFxazNBPTFWASXTuT/PdlcoQ1e15eomD MSf04PtueTt9DVPJceXHLRwN8mbUV/C/G7dmdi88SOovIpsFDb3kBWFpPYyyIK9O8faa 32UQ== X-Gm-Message-State: AOJu0YxBVt+Ov1nhA1ZM1fCp3X4QjxjjZ9Gj/O3cAKv8v3f2ebeJdi1C deWX0uZ4xHK287y+3R/6Q6OSyhjBmygwJfDt4G9fs5T0As1NZun6G5/GS4Lz X-Google-Smtp-Source: AGHT+IGVJjvYlBWC3vF469XDTUHiQYVLScD/y3UoyJS+TVlsbT7tniwUv/hD2DmHiLbQsigBWA6fyw== X-Received: by 2002:a17:902:e5d0:b0:1e4:9ad5:a666 with SMTP id u16-20020a170902e5d000b001e49ad5a666mr1379644plf.25.1712703706619; Tue, 09 Apr 2024 16:01:46 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.56.169.48]) by smtp.gmail.com with ESMTPSA id e7-20020a170902784700b001e26ba8882fsm9648080pln.287.2024.04.09.16.01.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 16:01:46 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id D25F174019D for ; Tue, 9 Apr 2024 16:01:44 -0700 (PDT) From: "H.J. Lu" To: binutils@sourceware.org Subject: [PATCH] bfd: Add bfd_xalloc, bfd_xzalloc and bfd_xalloc_strdup Date: Tue, 9 Apr 2024 16:01:44 -0700 Message-ID: <20240409230144.2459604-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3020.3 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,RCVD_IN_SBL_CSS,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: Remove bfd_xalloc in binutils. Add bfd_xalloc, bfd_xzalloc and bfd_xalloc_strdup to bfd to fix memory leaks in read.c, write.c and obj-elf.c. bfd/ * libbfd.c (bfd_xalloc): New. (bfd_xzalloc): Likewise. (bfd_xalloc_strdup): Likewise. * bfd-in2.h: Regenerated. binutils/ * bucomm.c (bfd_xalloc): Removed. * bucomm.h (bfd_xalloc): Likewise. gas/ * read.c (s_reloc): Replace XNEW with bfd_xalloc. * write.c (write_relocs): Replace XCNEWVEC with bfd_xzalloc. * config/obj-elf.c (obj_elf_find_and_add_versioned_name): Replace xmalloc with bfd_xalloc. Replace xstrdup with bfd_xalloc_strdup. --- bfd/bfd-in2.h | 6 ++++ bfd/libbfd.c | 68 ++++++++++++++++++++++++++++++++++++++++++++ binutils/bucomm.c | 13 --------- binutils/bucomm.h | 2 -- gas/config/obj-elf.c | 4 +-- gas/read.c | 2 +- gas/write.c | 5 ++-- 7 files changed, 80 insertions(+), 20 deletions(-) diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index d107a22f27e..807a423dd75 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -172,6 +172,12 @@ void *bfd_alloc (bfd *abfd, bfd_size_type wanted); void *bfd_zalloc (bfd *abfd, bfd_size_type wanted); +void *bfd_xalloc (bfd *abfd, bfd_size_type wanted); + +void *bfd_xzalloc (bfd *abfd, bfd_size_type wanted); + +char *bfd_xalloc_strdup (bfd *abfd, const char *str); + void bfd_release (bfd *, void *); diff --git a/bfd/libbfd.c b/bfd/libbfd.c index 2f5ddcaf3b8..b92fa86d4ac 100644 --- a/bfd/libbfd.c +++ b/bfd/libbfd.c @@ -481,6 +481,74 @@ bfd_zalloc (bfd *abfd, bfd_size_type size) return res; } +/* +FUNCTION + bfd_xalloc + +SYNOPSIS + void *bfd_xalloc (bfd *abfd, bfd_size_type wanted); + +DESCRIPTION + Allocate a block of @var{wanted} bytes of memory attached to + <> and return a pointer to it. Call _bfd_error_handler + if memory allocation failed. +*/ + +void * +bfd_xalloc (bfd *abfd, bfd_size_type wanted) +{ + void *buf = bfd_alloc (abfd, wanted); + if (buf == NULL) + _bfd_error_handler + /* xgettext:c-format */ + (_("error: %pB: out of memory allocating %#" PRIx64 " bytes"), + abfd, (uint64_t) wanted); + return buf; +} + +/* +FUNCTION + bfd_xzalloc + +SYNOPSIS + void *bfd_xzalloc (bfd *abfd, bfd_size_type wanted); + +DESCRIPTION + Allocate a block of @var{wanted} bytes of memory attached to + <>, zero the memory block and return a pointer to it. + Call _bfd_error_handler if memory allocation failed. +*/ + +void * +bfd_xzalloc (bfd *abfd, bfd_size_type wanted) +{ + void *res = bfd_xalloc (abfd, wanted); + memset (res, 0, (size_t) wanted); + return res; +} + +/* +FUNCTION + bfd_xalloc_strdup + +SYNOPSIS + char *bfd_xalloc_strdup (bfd *abfd, const char *str); + +DESCRIPTION + Allocate a block of memory attached to <> and return a + copy of @var{str}. Call _bfd_error_handler if memory allocation + failed. +*/ + +char * +bfd_xalloc_strdup (bfd *abfd, const char *str) +{ + size_t len = strlen (str) + 1; + char *buf = bfd_xalloc (abfd, len); + memcpy (buf, str, len); + return buf; +} + /* FUNCTION bfd_release diff --git a/binutils/bucomm.c b/binutils/bucomm.c index d51d1349f12..5e5b410c688 100644 --- a/binutils/bucomm.c +++ b/binutils/bucomm.c @@ -142,19 +142,6 @@ non_fatal (const char *format, ...) va_end (args); } -/* Like xmalloc except that ABFD's objalloc memory is returned. - Use objalloc_free_block to free this memory and all more recently - allocated, or more usually, leave it to bfd_close to free. */ - -void * -bfd_xalloc (bfd *abfd, size_t size) -{ - void *ret = bfd_alloc (abfd, size); - if (ret == NULL) - bfd_fatal (NULL); - return ret; -} - /* Set the default BFD target based on the configured target. Doing this permits the binutils to be configured for a particular target, and linked against a shared BFD library which was configured for a diff --git a/binutils/bucomm.h b/binutils/bucomm.h index f45838ef592..8d9e4b15f31 100644 --- a/binutils/bucomm.h +++ b/binutils/bucomm.h @@ -39,8 +39,6 @@ void fatal (const char *, ...) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN; void non_fatal (const char *, ...) ATTRIBUTE_PRINTF_1; -void *bfd_xalloc (bfd *, size_t); - void set_default_bfd_target (void); void list_matching_formats (char **); diff --git a/gas/config/obj-elf.c b/gas/config/obj-elf.c index e28ba0a62d5..177d63d59b4 100644 --- a/gas/config/obj-elf.c +++ b/gas/config/obj-elf.c @@ -1851,8 +1851,8 @@ obj_elf_find_and_add_versioned_name (const char *version_name, /* Add this versioned name to the head of the list, */ versioned_name = (struct elf_versioned_name_list *) - xmalloc (sizeof (*versioned_name)); - versioned_name->name = xstrdup (version_name); + bfd_xalloc (stdoutput, sizeof (*versioned_name)); + versioned_name->name = bfd_xalloc_strdup (stdoutput, version_name); versioned_name->next = sy_obj->versioned_name; sy_obj->versioned_name = versioned_name; diff --git a/gas/read.c b/gas/read.c index 8026a6cdb65..547365eb6cb 100644 --- a/gas/read.c +++ b/gas/read.c @@ -4244,7 +4244,7 @@ s_reloc (int ignore ATTRIBUTE_UNUSED) { "64", BFD_RELOC_64 } }; - reloc = XNEW (struct reloc_list); + reloc = bfd_xalloc (stdoutput, sizeof (struct reloc_list)); if (flag_mri) stop = mri_comment_field (&stopc); diff --git a/gas/write.c b/gas/write.c index 18cf18fc830..1a3a84ec4f6 100644 --- a/gas/write.c +++ b/gas/write.c @@ -1244,7 +1244,7 @@ write_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, segment_info_type *seginfo = seg_info (sec); unsigned int n; struct reloc_list *my_reloc_list, **rp, *r; - arelent **relocs; + arelent **relocs = NULL; fixS *fixp; fragS *last_frag; @@ -1279,7 +1279,8 @@ write_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, rp = &r->next; } - relocs = XCNEWVEC (arelent *, n); + if (n != 0) + relocs = bfd_xzalloc (stdoutput, sizeof (arelent *) * n); n = 0; r = my_reloc_list; -- 2.44.0