public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* PR29613, use of uninitialized value in objcopy
@ 2022-09-26  9:55 Alan Modra
  2024-04-18 21:12 ` Andrew Pinski
  0 siblings, 1 reply; 3+ messages in thread
From: Alan Modra @ 2022-09-26  9:55 UTC (permalink / raw)
  To: binutils

	PR 29613
	* elf.c (_bfd_elf_write_secondary_reloc_section): Trim sh_size
	back to relocs written.  Use better types for vars.

diff --git a/bfd/elf.c b/bfd/elf.c
index 9a0bc1d2740..396b56db024 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -13482,9 +13482,9 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
 	{
 	  asymbol *    last_sym;
 	  int          last_sym_idx;
-	  unsigned int reloc_count;
-	  unsigned int idx;
-	  unsigned int entsize;
+	  size_t       reloc_count;
+	  size_t       idx;
+	  bfd_size_type entsize;
 	  arelent *    src_irel;
 	  bfd_byte *   dst_rela;
 
@@ -13525,7 +13525,8 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
 	    }
 
 	  reloc_count = hdr->sh_size / entsize;
-	  if (reloc_count <= 0)
+	  hdr->sh_size = entsize * reloc_count;
+	  if (reloc_count == 0)
 	    {
 	      _bfd_error_handler
 		/* xgettext:c-format */
@@ -13572,7 +13573,7 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
 		{
 		  _bfd_error_handler
 		    /* xgettext:c-format */
-		    (_("%pB(%pA): error: reloc table entry %u is empty"),
+		    (_("%pB(%pA): error: reloc table entry %zu is empty"),
 		     abfd, relsec, idx);
 		  bfd_set_error (bfd_error_bad_value);
 		  result = false;
@@ -13597,7 +13598,7 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
 			{
 			  _bfd_error_handler
 			    /* xgettext:c-format */
-			    (_("%pB(%pA): error: secondary reloc %u"
+			    (_("%pB(%pA): error: secondary reloc %zu"
 			       " references a missing symbol"),
 			     abfd, relsec, idx);
 			  bfd_set_error (bfd_error_bad_value);
@@ -13615,7 +13616,7 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
 		    {
 		      _bfd_error_handler
 			/* xgettext:c-format */
-			(_("%pB(%pA): error: secondary reloc %u"
+			(_("%pB(%pA): error: secondary reloc %zu"
 			   " references a deleted symbol"),
 			 abfd, relsec, idx);
 		      bfd_set_error (bfd_error_bad_value);
@@ -13629,7 +13630,7 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
 		{
 		  _bfd_error_handler
 		    /* xgettext:c-format */
-		    (_("%pB(%pA): error: secondary reloc %u"
+		    (_("%pB(%pA): error: secondary reloc %zu"
 		       " is of an unknown type"),
 		     abfd, relsec, idx);
 		  bfd_set_error (bfd_error_bad_value);

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: PR29613, use of uninitialized value in objcopy
  2022-09-26  9:55 PR29613, use of uninitialized value in objcopy Alan Modra
@ 2024-04-18 21:12 ` Andrew Pinski
  2024-04-19  0:25   ` Alan Modra
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Pinski @ 2024-04-18 21:12 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils

On Mon, Sep 26, 2022 at 2:56 AM Alan Modra via Binutils
<binutils@sourceware.org> wrote:
>
>         PR 29613
>         * elf.c (_bfd_elf_write_secondary_reloc_section): Trim sh_size
>         back to relocs written.  Use better types for vars.

Just FYI. I noticed that this adds the use of `%zu` to
_bfd_error_handler but _bfd_error_handler uses ATTRIBUTE_PRINTF_1
which uses just the printf format which on mingw is different from
other targets (it is an alias to ms_printf rather than an alias to
gnu_printf). So this and other uses `%zu` will cause a warning to show
up because of that.
This was reported to GCC via
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114775 .

Thanks,
Andrew

>
> diff --git a/bfd/elf.c b/bfd/elf.c
> index 9a0bc1d2740..396b56db024 100644
> --- a/bfd/elf.c
> +++ b/bfd/elf.c
> @@ -13482,9 +13482,9 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
>         {
>           asymbol *    last_sym;
>           int          last_sym_idx;
> -         unsigned int reloc_count;
> -         unsigned int idx;
> -         unsigned int entsize;
> +         size_t       reloc_count;
> +         size_t       idx;
> +         bfd_size_type entsize;
>           arelent *    src_irel;
>           bfd_byte *   dst_rela;
>
> @@ -13525,7 +13525,8 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
>             }
>
>           reloc_count = hdr->sh_size / entsize;
> -         if (reloc_count <= 0)
> +         hdr->sh_size = entsize * reloc_count;
> +         if (reloc_count == 0)
>             {
>               _bfd_error_handler
>                 /* xgettext:c-format */
> @@ -13572,7 +13573,7 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
>                 {
>                   _bfd_error_handler
>                     /* xgettext:c-format */
> -                   (_("%pB(%pA): error: reloc table entry %u is empty"),
> +                   (_("%pB(%pA): error: reloc table entry %zu is empty"),
>                      abfd, relsec, idx);
>                   bfd_set_error (bfd_error_bad_value);
>                   result = false;
> @@ -13597,7 +13598,7 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
>                         {
>                           _bfd_error_handler
>                             /* xgettext:c-format */
> -                           (_("%pB(%pA): error: secondary reloc %u"
> +                           (_("%pB(%pA): error: secondary reloc %zu"
>                                " references a missing symbol"),
>                              abfd, relsec, idx);
>                           bfd_set_error (bfd_error_bad_value);
> @@ -13615,7 +13616,7 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
>                     {
>                       _bfd_error_handler
>                         /* xgettext:c-format */
> -                       (_("%pB(%pA): error: secondary reloc %u"
> +                       (_("%pB(%pA): error: secondary reloc %zu"
>                            " references a deleted symbol"),
>                          abfd, relsec, idx);
>                       bfd_set_error (bfd_error_bad_value);
> @@ -13629,7 +13630,7 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
>                 {
>                   _bfd_error_handler
>                     /* xgettext:c-format */
> -                   (_("%pB(%pA): error: secondary reloc %u"
> +                   (_("%pB(%pA): error: secondary reloc %zu"
>                        " is of an unknown type"),
>                      abfd, relsec, idx);
>                   bfd_set_error (bfd_error_bad_value);
>
> --
> Alan Modra
> Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: PR29613, use of uninitialized value in objcopy
  2024-04-18 21:12 ` Andrew Pinski
@ 2024-04-19  0:25   ` Alan Modra
  0 siblings, 0 replies; 3+ messages in thread
From: Alan Modra @ 2024-04-19  0:25 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: binutils

On Thu, Apr 18, 2024 at 02:12:27PM -0700, Andrew Pinski wrote:
> On Mon, Sep 26, 2022 at 2:56 AM Alan Modra via Binutils
> <binutils@sourceware.org> wrote:
> >
> >         PR 29613
> >         * elf.c (_bfd_elf_write_secondary_reloc_section): Trim sh_size
> >         back to relocs written.  Use better types for vars.
> 
> Just FYI. I noticed that this adds the use of `%zu` to
> _bfd_error_handler but _bfd_error_handler uses ATTRIBUTE_PRINTF_1
> which uses just the printf format which on mingw is different from
> other targets (it is an alias to ms_printf rather than an alias to
> gnu_printf). So this and other uses `%zu` will cause a warning to show
> up because of that.
> This was reported to GCC via
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114775 .

From binutils/README:
To build binutils you will need a C99 compliant compiler and library.

Casting size_t to long in order to print values is just plain wrong,
particularly on LLP64 hosts where sizeof(long)==4 and
sizeof(size_t)==8.  In fact, many places in binutils that use long
variables, struct fields or function return values ought to be using
size_t or ssize_t.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-04-19  0:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-26  9:55 PR29613, use of uninitialized value in objcopy Alan Modra
2024-04-18 21:12 ` Andrew Pinski
2024-04-19  0:25   ` Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).