aboutsummaryrefslogtreecommitdiff
path: root/bfd/elfnn-riscv.c
diff options
context:
space:
mode:
authorAlan Modra <amodra@gmail.com>2023-06-14 14:24:50 +0930
committerAlan Modra <amodra@gmail.com>2023-06-14 14:24:50 +0930
commit6f860418d556d4e5492b3da9e1a52e4b85a85f3e (patch)
tree7d8223edf575df45e809a9d2068b518ac67086d6 /bfd/elfnn-riscv.c
parent48375c36dc59e3c0e6b9bf38f17f4692bddb578f (diff)
downloadgdb-6f860418d556d4e5492b3da9e1a52e4b85a85f3e.zip
gdb-6f860418d556d4e5492b3da9e1a52e4b85a85f3e.tar.gz
gdb-6f860418d556d4e5492b3da9e1a52e4b85a85f3e.tar.bz2
asprintf memory leaks
A number of backends want to return bfd_reloc_dangerous messaqes from relocation special_function, and construct the message using asprintf. Such messages are not freed anywhere, leading to small memory leaks inside libbfd. To limit the leaks, I'd implemented a static buffer in the ppc backends that was freed before use in asprintf output. This patch extends that scheme to other backends using a shared static buffer and goes further in freeing the buffer on any bfd_close. The patch also fixes a few other cases where asprintf output was not freed after use. bfd/ * bfd.c (_input_error_msg): Make global and rename to.. (_bfd_error_buf): ..this. (bfd_asprintf): New function. (bfd_errmsg): Use bfd_asprintf. * opncls.c (bfd_close_all_done): Free _buf_error_buf. * elf32-arm.c (find_thumb_glue, find_arm_glue): Use bfd_asprintf. * elf32-nios2.c (nios2_elf32_relocate_section): Likewise. * elf32-ppc.c (ppc_elf_unhandled_reloc): Likewise. * elf64-ppc.c (ppc64_elf_unhandled_reloc): Likewise. * elfnn-riscv.c (riscv_resolve_pcrel_lo_relocs): Likewise. (riscv_elf_relocate_section): Likewise. * libbfd.h: Regenerate. gas/ * read.c (read_end): Free current_name and current_label. (do_s_func): Likewise on error path. strdup label. ld/ * pe-dll.c (make_head, make_tail, make_one), (make_singleton_name_thunk, make_import_fixup_entry), (make_runtime_pseudo_reloc), (pe_create_runtime_relocator_reference: Free oname after use.
Diffstat (limited to 'bfd/elfnn-riscv.c')
-rw-r--r--bfd/elfnn-riscv.c45
1 files changed, 18 insertions, 27 deletions
diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 30d2faa..09aa7be 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -2090,14 +2090,14 @@ riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs *p)
!= RISCV_CONST_HIGH_PART (entry->value + r->reloc->r_addend))
{
/* Check the overflow when adding reloc addend. */
- if (asprintf (&string,
- _("%%pcrel_lo overflow with an addend, the "
- "value of %%pcrel_hi is 0x%" PRIx64 " without "
- "any addend, but may be 0x%" PRIx64 " after "
- "adding the %%pcrel_lo addend"),
- (int64_t) RISCV_CONST_HIGH_PART (entry->value),
- (int64_t) RISCV_CONST_HIGH_PART
- (entry->value + r->reloc->r_addend)) == -1)
+ string = bfd_asprintf (_("%%pcrel_lo overflow with an addend,"
+ " the value of %%pcrel_hi is 0x%" PRIx64
+ " without any addend, but may be 0x%" PRIx64
+ " after adding the %%pcrel_lo addend"),
+ (int64_t) RISCV_CONST_HIGH_PART (entry->value),
+ (int64_t) RISCV_CONST_HIGH_PART
+ (entry->value + r->reloc->r_addend));
+ if (string == NULL)
string = _("%pcrel_lo overflow with an addend");
}
@@ -2184,7 +2184,6 @@ riscv_elf_relocate_section (bfd *output_bfd,
int r_type = ELFNN_R_TYPE (rel->r_info), tls_type;
reloc_howto_type *howto = riscv_elf_rtype_to_howto (input_bfd, r_type);
const char *msg = NULL;
- char *msg_buf = NULL;
bool resolved_to_zero;
if (howto == NULL)
@@ -2705,14 +2704,12 @@ riscv_elf_relocate_section (bfd *output_bfd,
Perhaps we also need the similar checks for the
R_RISCV_BRANCH and R_RISCV_RVC_BRANCH relocations. */
- if (asprintf (&msg_buf,
- _("%%X%%P: relocation %s against `%s' which "
- "may bind externally can not be used when "
- "making a shared object; recompile "
- "with -fPIC\n"),
- howto->name, h->root.root.string) == -1)
- msg_buf = NULL;
- msg = msg_buf;
+ msg = bfd_asprintf (_("%%X%%P: relocation %s against `%s'"
+ " which may bind externally"
+ " can not be used"
+ " when making a shared object;"
+ " recompile with -fPIC\n"),
+ howto->name, h->root.root.string);
r = bfd_reloc_notsupported;
}
}
@@ -2999,13 +2996,10 @@ riscv_elf_relocate_section (bfd *output_bfd,
&& _bfd_elf_section_offset (output_bfd, info, input_section,
rel->r_offset) != (bfd_vma) -1)
{
- if (asprintf (&msg_buf,
- _("%%X%%P: unresolvable %s relocation against "
- "symbol `%s'\n"),
- howto->name,
- h->root.root.string) == -1)
- msg_buf = NULL;
- msg = msg_buf;
+ msg = bfd_asprintf (_("%%X%%P: unresolvable %s relocation against "
+ "symbol `%s'\n"),
+ howto->name,
+ h->root.root.string);
r = bfd_reloc_notsupported;
}
@@ -3062,9 +3056,6 @@ riscv_elf_relocate_section (bfd *output_bfd,
if (msg && r != bfd_reloc_dangerous)
info->callbacks->einfo (msg);
- /* Free the unused `msg_buf`. */
- free (msg_buf);
-
/* We already reported the error via a callback, so don't try to report
it again by returning false. That leads to spurious errors. */
ret = true;