diff options
author | Alan Modra <amodra@gmail.com> | 2024-10-21 12:16:31 +1030 |
---|---|---|
committer | Alan Modra <amodra@gmail.com> | 2024-10-24 17:58:00 +1030 |
commit | 86b26b453f65404b29ce035a2eb3d62671aa0612 (patch) | |
tree | 97309863b4b131ebbb0915a6bc943e9aad681ef4 /ld | |
parent | 8b5a212495371fbc9bea7f4ce6d632df3763723b (diff) | |
download | binutils-86b26b453f65404b29ce035a2eb3d62671aa0612.zip binutils-86b26b453f65404b29ce035a2eb3d62671aa0612.tar.gz binutils-86b26b453f65404b29ce035a2eb3d62671aa0612.tar.bz2 |
Replace uses of asprintf with xasprintf
xasprintf has a nicer interface and behaves like xmalloc as far as
memory is concerned, ie. no need to check a return status and the
program exits with an error on OOM.
binutils/
* dwarf.c (load_debug_sup_file): Replace asprintf with xasprintf.
* nm.c (get_elf_symbol_type, get_coff_symbol_type): Likewise.
* objdump.c (dump_ctf_indent_lines): Likewise.
* readelf.c (display_lto_symtab, dump_ctf_indent_lines): Likewise.
* windres.c (main): Likewise.
* configure.ac: Remove asprintf from AC_CHECK_DECLS.
* config.in: Regenerate.
* configure: Regenerate.
gas/
* config/tc-kvx.c (kvx_emit_single_noop): Simplify.
* config/tc-riscv.c (md_assemblef): Replace asprintf with xasprintf.
* read.c (s_nop, do_s_func): Likewise.
* stabs.c (stabs_generate_asm_func): Likewise.
(stabs_generate_asm_endfunc): Likewise.
* configure.ac: Remove asprintf from AC_CHECK_DECLS.
* config.in: Regenerate.
* configure: Regenerate.
ld/
* ldlang.c (lang_leave_overlay_section): Replace xmalloc+sprintf
with xasprintf. Localise vars.
* lexsup.c (parse_args): Replace asprintf with xasprintf.
* pe-dll.c (make_head, make_tail, make_one): Likewise.
(make_singleton_name_thunk, make_import_fixup_entry): Likewise.
(make_runtime_pseudo_reloc): Likewise.
(pe_create_runtime_relocator_reference): Likewise.
* configure.ac: Remove asprintf from AC_CHECK_DECLS.
* config.in: Regenerate.
* configure: Regenerate.
Diffstat (limited to 'ld')
-rw-r--r-- | ld/config.in | 4 | ||||
-rwxr-xr-x | ld/configure | 10 | ||||
-rw-r--r-- | ld/configure.ac | 2 | ||||
-rw-r--r-- | ld/ldlang.c | 19 | ||||
-rw-r--r-- | ld/lexsup.c | 23 | ||||
-rw-r--r-- | ld/pe-dll.c | 49 |
6 files changed, 21 insertions, 86 deletions
diff --git a/ld/config.in b/ld/config.in index f2aaf0a..633105a 100644 --- a/ld/config.in +++ b/ld/config.in @@ -95,10 +95,6 @@ */ #undef HAVE_DCGETTEXT -/* Define to 1 if you have the declaration of `asprintf', and to 0 if you - don't. */ -#undef HAVE_DECL_ASPRINTF - /* Define to 1 if you have the declaration of `environ', and to 0 if you don't. */ #undef HAVE_DECL_ENVIRON diff --git a/ld/configure b/ld/configure index d905f1c..0b4197d 100755 --- a/ld/configure +++ b/ld/configure @@ -18761,16 +18761,6 @@ $as_echo "#define USE_BINARY_FOPEN 1" >>confdefs.h ;; esac -ac_fn_c_check_decl "$LINENO" "asprintf" "ac_cv_have_decl_asprintf" "$ac_includes_default" -if test "x$ac_cv_have_decl_asprintf" = xyes; then : - ac_have_decl=1 -else - ac_have_decl=0 -fi - -cat >>confdefs.h <<_ACEOF -#define HAVE_DECL_ASPRINTF $ac_have_decl -_ACEOF ac_fn_c_check_decl "$LINENO" "environ" "ac_cv_have_decl_environ" "$ac_includes_default" if test "x$ac_cv_have_decl_environ" = xyes; then : ac_have_decl=1 diff --git a/ld/configure.ac b/ld/configure.ac index 5d10b38..3ac2b46 100644 --- a/ld/configure.ac +++ b/ld/configure.ac @@ -408,7 +408,7 @@ AC_CHECK_FUNCS(close glob lseek mkstemp open realpath waitpid) BFD_BINARY_FOPEN -AC_CHECK_DECLS([asprintf, environ, stpcpy]) +AC_CHECK_DECLS([environ, stpcpy]) GCC_AC_FUNC_MMAP diff --git a/ld/ldlang.c b/ld/ldlang.c index 00b2144..6335e1f 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -9195,12 +9195,7 @@ void lang_leave_overlay_section (fill_type *fill, lang_output_section_phdr_list *phdrs) { - const char *name; - char *clean, *s2; - const char *s1; - char *buf; - - name = current_section->name; + const char *name = current_section->name;; /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory region and that no load-time region has been specified. It doesn't @@ -9210,21 +9205,19 @@ lang_leave_overlay_section (fill_type *fill, /* Define the magic symbols. */ - clean = (char *) xmalloc (strlen (name) + 1); - s2 = clean; - for (s1 = name; *s1 != '\0'; s1++) + char *clean = xmalloc (strlen (name) + 1); + char *s2 = clean; + for (const char *s1 = name; *s1 != '\0'; s1++) if (ISALNUM (*s1) || *s1 == '_') *s2++ = *s1; *s2 = '\0'; - buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_"); - sprintf (buf, "__load_start_%s", clean); + char *buf = xasprintf ("__load_start_%s", clean); lang_add_assignment (exp_provide (buf, exp_nameop (LOADADDR, name), false)); - buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_"); - sprintf (buf, "__load_stop_%s", clean); + buf = xasprintf ("__load_stop_%s", clean); lang_add_assignment (exp_provide (buf, exp_binop ('+', exp_nameop (LOADADDR, name), diff --git a/ld/lexsup.c b/ld/lexsup.c index 8982073..92a12fe 100644 --- a/ld/lexsup.c +++ b/ld/lexsup.c @@ -1868,7 +1868,6 @@ parse_args (unsigned argc, char **argv) { char * new_name = NULL; char * percent; - int res = 0; if (config.map_filename[0] == 0) { @@ -1885,9 +1884,9 @@ parse_args (unsigned argc, char **argv) output filename. If the % character was the last character in the original map filename then add a .map extension. */ percent[0] = 0; - res = asprintf (&new_name, "%s%s%s", config.map_filename, - output_filename, - percent[1] ? percent + 1 : ".map"); + new_name = xasprintf ("%s%s%s", config.map_filename, + output_filename, + percent[1] ? percent + 1 : ".map"); /* FIXME: Should we ensure that any directory components in new_name exist ? */ } @@ -1905,10 +1904,9 @@ parse_args (unsigned argc, char **argv) else if (S_ISDIR (s.st_mode)) { char lastc = config.map_filename[strlen (config.map_filename) - 1]; - res = asprintf (&new_name, "%s%s%s.map", - config.map_filename, - IS_DIR_SEPARATOR (lastc) ? "" : "/", - lbasename (output_filename)); + new_name = xasprintf ("%s%s%s.map", config.map_filename, + IS_DIR_SEPARATOR (lastc) ? "" : "/", + lbasename (output_filename)); } else if (! S_ISREG (s.st_mode)) { @@ -1918,14 +1916,7 @@ parse_args (unsigned argc, char **argv) /* else FIXME: Check write permission ? */ } - if (res < 0) - { - /* If the asprintf failed then something is probably very - wrong. Better to halt now rather than continue on - into more problems. */ - einfo (_("%P%F: cannot create name for linker map file: %E\n")); - } - else if (new_name != NULL) + if (new_name != NULL) { /* This is a trivial memory leak. */ config.map_filename = new_name; diff --git a/ld/pe-dll.c b/ld/pe-dll.c index 93229e0..c2ccf98 100644 --- a/ld/pe-dll.c +++ b/ld/pe-dll.c @@ -2125,12 +2125,7 @@ make_head (bfd *parent) char *oname; bfd *abfd; - if (asprintf (&oname, "%s_d%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_d%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent); @@ -2219,12 +2214,7 @@ make_tail (bfd *parent) char *oname; bfd *abfd; - if (asprintf (&oname, "%s_d%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_d%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent); @@ -2412,12 +2402,7 @@ make_one (def_file_export *exp, bfd *parent, bool include_jmp_stub) } } - if (asprintf (&oname, "%s_d%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_d%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent); @@ -2600,12 +2585,7 @@ make_singleton_name_thunk (const char *import, bfd *parent) char *oname; bfd *abfd; - if (asprintf (&oname, "%s_nmth%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_nmth%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent); @@ -2681,12 +2661,7 @@ make_import_fixup_entry (const char *name, char *oname; bfd *abfd; - if (asprintf (&oname, "%s_fu%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_fu%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent); @@ -2740,12 +2715,7 @@ make_runtime_pseudo_reloc (const char *name ATTRIBUTE_UNUSED, bfd *abfd; bfd_size_type size; - if (asprintf (&oname, "%s_rtr%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_rtr%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent); @@ -2833,12 +2803,7 @@ pe_create_runtime_relocator_reference (bfd *parent) char *oname; bfd *abfd; - if (asprintf (&oname, "%s_ertr%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_ertr%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent); |