diff options
author | Alan Modra <amodra@gmail.com> | 2022-07-04 11:24:22 +0930 |
---|---|---|
committer | Alan Modra <amodra@gmail.com> | 2022-07-04 22:51:56 +0930 |
commit | 0772daccb3ebaf513badf4266e1948454b4455c1 (patch) | |
tree | 23032d204a77ac8f9c8cec3a0c48e0ee8b826548 /binutils/objcopy.c | |
parent | d8efadbdd94772562fed8fba9ce553587a62550f (diff) | |
download | gdb-0772daccb3ebaf513badf4266e1948454b4455c1.zip gdb-0772daccb3ebaf513badf4266e1948454b4455c1.tar.gz gdb-0772daccb3ebaf513badf4266e1948454b4455c1.tar.bz2 |
objcopy: bfd_alloc orelocation
This fixes an inconsequential objcopy memory leak. I'd normally
ignore reports of leaks like this one, that are merely one block or
fewer per section processed, since objcopy soon exits and frees all
memory. However I thought it worth providing support for allocating
memory on a bfd objalloc in objcopy and other utils.
PR 29233
* bucomm.c (bfd_xalloc): New function.
* bucomm.h (bfd_xalloc): Declare.
* objcopy.c (copy_relocations_in_section): Use it to allocate
array of reloc pointers. Rewrite code stripping relocs to do
without extra memory allocation.
Diffstat (limited to 'binutils/objcopy.c')
-rw-r--r-- | binutils/objcopy.c | 37 |
1 files changed, 13 insertions, 24 deletions
diff --git a/binutils/objcopy.c b/binutils/objcopy.c index df87712..3743573 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -4336,14 +4336,13 @@ copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg) } else { - relpp = (arelent **) xmalloc (relsize); + relpp = bfd_xalloc (obfd, relsize); relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp); if (relcount < 0) { status = 1; bfd_nonfatal_message (NULL, ibfd, isection, _("relocation count is negative")); - free (relpp); return; } } @@ -4352,34 +4351,24 @@ copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg) { /* Remove relocations which are not in keep_strip_specific_list. */ - arelent **temp_relpp; - long temp_relcount = 0; + arelent **w_relpp; long i; - temp_relpp = (arelent **) xmalloc (relsize); - for (i = 0; i < relcount; i++) - { - /* PR 17512: file: 9e907e0c. */ - if (relpp[i]->sym_ptr_ptr - /* PR 20096 */ - && * relpp[i]->sym_ptr_ptr) - if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr), - keep_specific_htab)) - temp_relpp [temp_relcount++] = relpp [i]; - } - relcount = temp_relcount; - if (relpp != isection->orelocation) - free (relpp); - relpp = temp_relpp; + for (w_relpp = relpp, i = 0; i < relcount; i++) + /* PR 17512: file: 9e907e0c. */ + if (relpp[i]->sym_ptr_ptr + /* PR 20096 */ + && *relpp[i]->sym_ptr_ptr + && is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr), + keep_specific_htab)) + *w_relpp++ = relpp[i]; + relcount = w_relpp - relpp; + *w_relpp = 0; } bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount); if (relcount == 0) - { - osection->flags &= ~SEC_RELOC; - if (relpp != isection->orelocation) - free (relpp); - } + osection->flags &= ~SEC_RELOC; } } |