From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29334 invoked by alias); 15 Dec 2011 11:02:01 -0000 Received: (qmail 29318 invoked by uid 22791); 15 Dec 2011 11:02:00 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from mel.act-europe.fr (HELO mel.act-europe.fr) (194.98.77.210) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 15 Dec 2011 11:01:43 +0000 Received: from localhost (localhost [127.0.0.1]) by filtered-smtp.eu.adacore.com (Postfix) with ESMTP id 1FC9F290001; Thu, 15 Dec 2011 12:01:43 +0100 (CET) Received: from mel.act-europe.fr ([127.0.0.1]) by localhost (smtp.eu.adacore.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id YJFTYSspMwRK; Thu, 15 Dec 2011 12:01:43 +0100 (CET) Received: from ulanbator.act-europe.fr (ulanbator.act-europe.fr [10.10.1.67]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by mel.act-europe.fr (Postfix) with ESMTP id 0C482CB1377; Thu, 15 Dec 2011 12:01:43 +0100 (CET) Subject: Re: [PATCH] Mach-O: fix two memory leaks Mime-Version: 1.0 (Apple Message framework v1251.1) Content-Type: text/plain; charset=iso-8859-1 From: Tristan Gingold In-Reply-To: Date: Thu, 15 Dec 2011 11:02:00 -0000 Cc: binutils Development Content-Transfer-Encoding: quoted-printable Message-Id: <709CEC75-401C-4562-AED4-4679CF371108@adacore.com> References: <1449E8D9-EAEF-4A8E-A351-47EEE5CE524E@adacore.com> <7EC40D64-A3A4-4C61-89E6-5F0941C35BBA@adacore.com> <1DECD517-52B0-4915-8265-9F164BE68E9F@adacore.com> To: shinichiro hamaji X-IsSubscribed: yes Mailing-List: contact binutils-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: binutils-owner@sourceware.org X-SW-Source: 2011-12/txt/msg00211.txt.bz2 On Dec 14, 2011, at 3:23 PM, shinichiro hamaji wrote: > On Wed, Dec 14, 2011 at 11:03 PM, Tristan Gingold w= rote: >>>=20 >>> Thanks again for your quick response. Here is the updated patch: >>> http://shinh.skr.jp/t/mach-o-leaks-3.patch >>=20 >> Ok. >>=20 >> Should I commit it ? >=20 > Yes, please? Thanks! Committed. >=20 >>=20 >> Tristan. >>=20 >>>=20 >>> bfd/ >>> 2011-12-14 Shinichiro Hamaji >>>=20 >>> * mach-o.c (bfd_mach_o_canonicalize_reloc): Update relocation >>> table only when there isn't the cahce. >>> (bfd_mach_o_get_dynamic_reloc_upper_bound): Need one more space >>> for a pointer for the watchdog. >>> (bfd_mach_o_canonicalize_dynamic_reloc): Utilize cache like >>> bfd_mach_o_canonicalize_reloc. >>> (bfd_mach_o_close_and_cleanup): Call bfd_mach_o_free_cached_info. >>> (bfd_mach_o_free_cached_info): Free up cache data. >>> * mach-o.h (reloc_cache): A place to store cache of dynamic reloc= s. >>> (bfd_mach_o_free_cached_info): Add declaration. >>>=20 >>> diff --git a/bfd/mach-o.c b/bfd/mach-o.c >>> index c768689..e5da70b 100644 >>> --- a/bfd/mach-o.c >>> +++ b/bfd/mach-o.c >>> @@ -1024,21 +1024,25 @@ bfd_mach_o_canonicalize_reloc (bfd *abfd, >>> asection *asect, >>> if (bed->_bfd_mach_o_swap_reloc_in =3D=3D NULL) >>> return 0; >>>=20 >>> - res =3D bfd_malloc (asect->reloc_count * sizeof (arelent)); >>> - if (res =3D=3D NULL) >>> - return -1; >>> - >>> - if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos, >>> - asect->reloc_count, res, syms) <= 0) >>> + if (asect->relocation =3D=3D NULL) >>> { >>> - free (res); >>> - return -1; >>> + res =3D bfd_malloc (asect->reloc_count * sizeof (arelent)); >>> + if (res =3D=3D NULL) >>> + return -1; >>> + >>> + if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos, >>> + asect->reloc_count, res, sym= s) < 0) >>> + { >>> + free (res); >>> + return -1; >>> + } >>> + asect->relocation =3D res; >>> } >>>=20 >>> + res =3D asect->relocation; >>> for (i =3D 0; i < asect->reloc_count; i++) >>> rels[i] =3D &res[i]; >>> rels[i] =3D NULL; >>> - asect->relocation =3D res; >>>=20 >>> return i; >>> } >>> @@ -1050,7 +1054,7 @@ bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *ab= fd) >>>=20 >>> if (mdata->dysymtab =3D=3D NULL) >>> return 1; >>> - return (mdata->dysymtab->nextrel + mdata->dysymtab->nlocrel) >>> + return (mdata->dysymtab->nextrel + mdata->dysymtab->nlocrel + 1) >>> * sizeof (arelent *); >>> } >>>=20 >>> @@ -1073,25 +1077,32 @@ bfd_mach_o_canonicalize_dynamic_reloc (bfd >>> *abfd, arelent **rels, >>> if (bed->_bfd_mach_o_swap_reloc_in =3D=3D NULL) >>> return 0; >>>=20 >>> - res =3D bfd_malloc ((dysymtab->nextrel + dysymtab->nlocrel) * sizeof >>> (arelent)); >>> - if (res =3D=3D NULL) >>> - return -1; >>> - >>> - if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff, >>> - dysymtab->nextrel, res, syms) < = 0) >>> + if (mdata->dyn_reloc_cache =3D=3D NULL) >>> { >>> - free (res); >>> - return -1; >>> - } >>> + res =3D bfd_malloc ((dysymtab->nextrel + dysymtab->nlocrel) >>> + * sizeof (arelent)); >>> + if (res =3D=3D NULL) >>> + return -1; >>>=20 >>> - if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff, >>> - dysymtab->nlocrel, >>> - res + dysymtab->nextrel, syms) <= 0) >>> - { >>> - free (res); >>> - return -1; >>> + if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff, >>> + dysymtab->nextrel, res, syms= ) < 0) >>> + { >>> + free (res); >>> + return -1; >>> + } >>> + >>> + if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff, >>> + dysymtab->nlocrel, >>> + res + dysymtab->nextrel, sym= s) < 0) >>> + { >>> + free (res); >>> + return -1; >>> + } >>> + >>> + mdata->dyn_reloc_cache =3D res; >>> } >>>=20 >>> + res =3D mdata->dyn_reloc_cache; >>> for (i =3D 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++) >>> rels[i] =3D &res[i]; >>> rels[i] =3D NULL; >>> @@ -3740,9 +3751,26 @@ bfd_mach_o_close_and_cleanup (bfd *abfd) >>> if (bfd_get_format (abfd) =3D=3D bfd_object && mdata !=3D NULL) >>> _bfd_dwarf2_cleanup_debug_info (abfd, &mdata->dwarf2_find_line_info= ); >>>=20 >>> + bfd_mach_o_free_cached_info (abfd); >>> + >>> return _bfd_generic_close_and_cleanup (abfd); >>> } >>>=20 >>> +bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd) >>> +{ >>> + bfd_mach_o_data_struct *mdata =3D bfd_mach_o_get_data (abfd); >>> + asection *asect; >>> + free (mdata->dyn_reloc_cache); >>> + mdata->dyn_reloc_cache =3D NULL; >>> + for (asect =3D abfd->sections; asect !=3D NULL; asect =3D asect->nex= t) >>> + { >>> + free (asect->relocation); >>> + asect->relocation =3D NULL; >>> + } >>> + >>> + return TRUE; >>> +} >>> + >>> #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_l= ookup >>> #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_l= ookup >>>=20 >>> diff --git a/bfd/mach-o.h b/bfd/mach-o.h >>> index 0c6f4fd..07c6935 100644 >>> --- a/bfd/mach-o.h >>> +++ b/bfd/mach-o.h >>> @@ -519,6 +519,9 @@ typedef struct mach_o_data_struct >>>=20 >>> /* A place to stash dwarf2 info for this bfd. */ >>> void *dwarf2_find_line_info; >>> + >>> + /* Cache of dynamic relocs. */ >>> + arelent *dyn_reloc_cache; >>> } >>> bfd_mach_o_data_struct; >>>=20 >>> @@ -589,6 +592,7 @@ bfd_boolean bfd_mach_o_find_nearest_line (bfd *, >>> asection *, asymbol **, >>> bfd_vma, const char **, >>> const char **, unsigned int *= ); >>> bfd_boolean bfd_mach_o_close_and_cleanup (bfd *); >>> +bfd_boolean bfd_mach_o_free_cached_info (bfd *); >>>=20 >>> unsigned int bfd_mach_o_section_get_nbr_indirect (bfd *, bfd_mach_o_sec= tion *); >>> unsigned int bfd_mach_o_section_get_entry_size (bfd *, bfd_mach_o_secti= on *); >>=20