.}
.bfd_error_type;
.
+INTERNAL
+.{* A buffer that is freed on bfd_close. *}
+.extern char *_bfd_error_buf;
+.
*/
static bfd_error_type bfd_error;
static bfd_error_type input_error;
static bfd *input_bfd;
-static char *input_error_msg;
+char *_bfd_error_buf;
const char *const bfd_errmsgs[] =
{
/* This is an error that occurred during bfd_close when writing an
archive, but on one of the input files. */
bfd_error = bfd_error_on_input;
- free (input_error_msg);
- input_error_msg = NULL;
+ free (_bfd_error_buf);
+ _bfd_error_buf = NULL;
input_bfd = input;
input_error = error_tag;
if (input_error >= bfd_error_on_input)
if (error_tag == bfd_error_on_input)
{
const char *msg = bfd_errmsg (input_error);
-
- free (input_error_msg);
- input_error_msg = NULL;
- if (asprintf (&input_error_msg, _(bfd_errmsgs [error_tag]),
- bfd_get_filename (input_bfd), msg) != -1)
- return input_error_msg;
+ char *ret = bfd_asprintf (_(bfd_errmsgs[error_tag]),
+ bfd_get_filename (input_bfd), msg);
+ if (ret)
+ return ret;
/* Ick, what to do on out of memory? */
return msg;
if (error_tag > bfd_error_invalid_error_code)
error_tag = bfd_error_invalid_error_code; /* sanity check */
- return _(bfd_errmsgs [error_tag]);
+ return _(bfd_errmsgs[error_tag]);
}
/*
fflush (stderr);
}
+/*
+INTERNAL_FUNCTION
+ bfd_asprintf
+
+SYNOPSIS
+ char *bfd_asprintf (const char *fmt, ...);
+
+DESCRIPTION
+ Primarily for error reporting, this function is like
+ libiberty's xasprintf except that it can return NULL on no
+ memory and the returned string should not be freed. Uses a
+ single malloc'd buffer managed by libbfd, _bfd_error_buf.
+ Be aware that a call to this function frees the result of any
+ previous call. bfd_errmsg (bfd_error_on_input) also calls
+ this function.
+*/
+
+char *
+bfd_asprintf (const char *fmt, ...)
+{
+ free (_bfd_error_buf);
+ _bfd_error_buf = NULL;
+ va_list ap;
+ va_start (ap, fmt);
+ int count = vasprintf (&_bfd_error_buf, fmt, ap);
+ va_end (ap);
+ if (count == -1)
+ {
+ bfd_set_error (bfd_error_no_memory);
+ _bfd_error_buf = NULL;
+ }
+ return _bfd_error_buf;
+}
+
/*
SUBSECTION
BFD error handler
{
bfd_error = bfd_error_no_error;
input_bfd = NULL;
- free (input_error_msg);
- input_error_msg = NULL;
+ free (_bfd_error_buf);
+ _bfd_error_buf = NULL;
input_error = bfd_error_no_error;
_bfd_error_program_name = NULL;
_bfd_error_internal = error_handler_fprintf;
hash = elf_link_hash_lookup
(&(hash_table)->root, tmp_name, false, false, true);
- if (hash == NULL
- && asprintf (error_message, _("unable to find %s glue '%s' for '%s'"),
- "Thumb", tmp_name, name) == -1)
- *error_message = (char *) bfd_errmsg (bfd_error_system_call);
+ if (hash == NULL)
+ {
+ *error_message = bfd_asprintf (_("unable to find %s glue '%s' for '%s'"),
+ "Thumb", tmp_name, name);
+ if (*error_message == NULL)
+ *error_message = (char *) bfd_errmsg (bfd_error_system_call);
+ }
free (tmp_name);
myh = elf_link_hash_lookup
(&(hash_table)->root, tmp_name, false, false, true);
- if (myh == NULL
- && asprintf (error_message, _("unable to find %s glue '%s' for '%s'"),
- "ARM", tmp_name, name) == -1)
- *error_message = (char *) bfd_errmsg (bfd_error_system_call);
-
+ if (myh == NULL)
+ {
+ *error_message = bfd_asprintf (_("unable to find %s glue '%s' for '%s'"),
+ "ARM", tmp_name, name);
+ if (*error_message == NULL)
+ *error_message = (char *) bfd_errmsg (bfd_error_system_call);
+ }
free (tmp_name);
return myh;
const char *name = NULL;
int r_type;
const char *format;
- char *msgbuf = NULL;
char *msg = NULL;
bool unresolved_reloc;
bfd_vma off;
format = _("global pointer relative relocation at address "
"%#" PRIx64 " when _gp not defined\n");
- if (asprintf (&msgbuf, format,
- (uint64_t) reloc_address) == -1)
- msgbuf = NULL;
- msg = msgbuf;
+ msg = bfd_asprintf (format, (uint64_t) reloc_address);
r = bfd_reloc_dangerous;
}
else
"the global pointer (at %#" PRIx64 ") "
"because the offset (%" PRId64 ") is out of "
"the allowed range, -32678 to 32767\n" );
- if (asprintf (&msgbuf, format, name,
- (uint64_t) symbol_address, (uint64_t) gp,
- (int64_t) relocation) == -1)
- msgbuf = NULL;
- msg = msgbuf;
+ msg = bfd_asprintf (format, name,
+ (uint64_t) symbol_address,
+ (uint64_t) gp,
+ (int64_t) relocation);
r = bfd_reloc_outofrange;
}
else
{
(*info->callbacks->warning) (info, msg, name, input_bfd,
input_section, rel->r_offset);
- free (msgbuf);
return false;
}
}
input_section, output_bfd, error_message);
if (error_message != NULL)
- {
- static char *message;
- free (message);
- if (asprintf (&message, _("generic linker can't handle %s"),
- reloc_entry->howto->name) < 0)
- message = NULL;
- *error_message = message;
- }
+ *error_message = bfd_asprintf (_("generic linker can't handle %s"),
+ reloc_entry->howto->name);
return bfd_reloc_dangerous;
}
\f
input_section, output_bfd, error_message);
if (error_message != NULL)
- {
- static char *message;
- free (message);
- if (asprintf (&message, _("generic linker can't handle %s"),
- reloc_entry->howto->name) < 0)
- message = NULL;
- *error_message = message;
- }
+ *error_message = bfd_asprintf (_("generic linker can't handle %s"),
+ reloc_entry->howto->name);
return bfd_reloc_dangerous;
}
!= 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");
}
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)
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;
}
}
&& _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;
}
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;
unsigned int bfd_log2 (bfd_vma x) ATTRIBUTE_HIDDEN;
/* Extracted from bfd.c. */
+/* A buffer that is freed on bfd_close. */
+extern char *_bfd_error_buf;
+
+char *bfd_asprintf (const char *fmt, ...) ATTRIBUTE_HIDDEN;
+
bfd_error_handler_type _bfd_set_error_handler_caching (bfd *) ATTRIBUTE_HIDDEN;
const char *_bfd_get_error_program_name (void) ATTRIBUTE_HIDDEN;
}
_bfd_delete_bfd (abfd);
+ free (_bfd_error_buf);
+ _bfd_error_buf = NULL;
return ret;
}
stabs_end ();
poend ();
_obstack_free (&cond_obstack, NULL);
+ free (current_name);
+ free (current_label);
}
\f
#ifndef TC_ADDRESS_BYTES
if (debug_type == DEBUG_STABS)
stabs_generate_asm_endfunc (current_name, current_label);
+ free (current_name);
+ free (current_label);
current_name = current_label = NULL;
}
else /* ! end_p */
as_fatal ("%s", xstrerror (errno));
}
else
- label = name;
+ label = xstrdup (name);
}
}
else
tmp_seq++;
abfd = bfd_create (oname, parent);
+ free (oname);
bfd_find_target (pe_details->object_target, abfd);
bfd_make_writable (abfd);
tmp_seq++;
abfd = bfd_create (oname, parent);
+ free (oname);
bfd_find_target (pe_details->object_target, abfd);
bfd_make_writable (abfd);
tmp_seq++;
abfd = bfd_create (oname, parent);
+ free (oname);
bfd_find_target (pe_details->object_target, abfd);
bfd_make_writable (abfd);
tmp_seq++;
abfd = bfd_create (oname, parent);
+ free (oname);
bfd_find_target (pe_details->object_target, abfd);
bfd_make_writable (abfd);
tmp_seq++;
abfd = bfd_create (oname, parent);
+ free (oname);
bfd_find_target (pe_details->object_target, abfd);
bfd_make_writable (abfd);
tmp_seq++;
abfd = bfd_create (oname, parent);
+ free (oname);
bfd_find_target (pe_details->object_target, abfd);
bfd_make_writable (abfd);
tmp_seq++;
abfd = bfd_create (oname, parent);
+ free (oname);
bfd_find_target (pe_details->object_target, abfd);
bfd_make_writable (abfd);