Patchwork [libiberty/mach-o] fix byte-swapping of indices in wrapper sections.

login
register
mail settings
Submitter IainS
Date Feb. 26, 2012, 4:01 p.m.
Message ID <42C5221F-2437-4D2B-ADD7-202D86355E11@sandoe-acoustics.co.uk>
Download mbox | patch
Permalink /patch/143103/
State New
Headers show

Comments

IainS - Feb. 26, 2012, 4:01 p.m.
Found while testing x86 X ppc ...
.. I missed byte-swapping the indices when outputting the index of the  
GNU wrapper for LTO sections.

OK/When?
Iain

libiberty:

	* simple-object-mach-o.c (simple_object_mach_o_write_segment):
	Byte-swap indices when required.

        /* Write out the section names.
Ian Lance Taylor - Feb. 26, 2012, 5:39 p.m.
Iain Sandoe <developer@sandoe-acoustics.co.uk> writes:

> Found while testing x86 X ppc ...
> .. I missed byte-swapping the indices when outputting the index of the
> GNU wrapper for LTO sections.
>
> OK/When?
> Iain
>
> libiberty:
>
> 	* simple-object-mach-o.c (simple_object_mach_o_write_segment):
> 	Byte-swap indices when required.
>
> diff --git a/libiberty/simple-object-mach-o.c b/libiberty/simple-
> object-mach-o.c
> index af5e4f9..fbf6a3f 100644
> --- a/libiberty/simple-object-mach-o.c
> +++ b/libiberty/simple-object-mach-o.c
> @@ -1224,6 +1224,11 @@ simple_object_mach_o_write_segment
> (simple_object_write *sobj, int descriptor,
>  	index[4 * i] -= index[0];
>        index[0] = 0;
>
> +      /* Swap the indices, if required.  */
> +
> +      for (i = 0; i < (nsects_in * 4); ++i)
> +        set_32 (&index[i], index[i]);
> +
>        sechdr_offset += sechdrsize;
>
>        /* Write out the section names.


I think that is not the only way the code is broken.  When you pass the
array to simple_object_internal_write, you are assuming that the type
"unsigned int" is exactly 4 bytes long.  I would strongly recommend
changing the code to do something like

  index_bytes = XNEWVEC (unsigned char, nsects_in * 16);
  for (i = 0; i < nsects_in * 4; ++i)
    set_32 (index_bytes + i * 4, index[i]);
  if (!simple_object_internal_write (descriptor, offset, index_bytes,
                                     nsects_in * 16, errmsg, err))
    return 0;
  XFREEVEC (index_bytes);

If that works, I'll approve it.  I think this is OK in stage 4, as it is
a bug fix.

Ian

Patch

diff --git a/libiberty/simple-object-mach-o.c b/libiberty/simple- 
object-mach-o.c
index af5e4f9..fbf6a3f 100644
--- a/libiberty/simple-object-mach-o.c
+++ b/libiberty/simple-object-mach-o.c
@@ -1224,6 +1224,11 @@  simple_object_mach_o_write_segment  
(simple_object_write *sobj, int descriptor,
  	index[4 * i] -= index[0];
        index[0] = 0;

+      /* Swap the indices, if required.  */
+
+      for (i = 0; i < (nsects_in * 4); ++i)
+        set_32 (&index[i], index[i]);
+
        sechdr_offset += sechdrsize;