Re: [Libguestfs] [PATCH supermin] bin2s: make sure the data is aligned

Richard W.M. Jones
Wed, 23 Aug 2017 13:03:50 -0000

On Wed, Aug 23, 2017 at 01:12:13PM +0200, Pino Toscano
wrote:
> Not all the architectures can cope correctly with unaligned data, and
> thus the embedded init written is bogus (since it will contain random
> padding at the beginning, and truncated of the same amount at the end).
>
> To overcome that, make sure the rodata section with the embedded init
> is always aligned to 8 bytes, which should work fine for both 32bit and
> 64bit architectures.
> ---
>  src/bin2s.pl | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/src/bin2s.pl b/src/bin2s.pl
> index 6c70446..67ff1f2 100755
> --- a/src/bin2s.pl
> +++ b/src/bin2s.pl
> @@ -44,6 +44,7 @@ print $ofh <<"EOF";
>  \t.globl\t_binary_${infile_basename}_end
>
>  \t.section\t.rodata
> +\t.align 8
>  _binary_${infile_basename}_start:
>  EOF
>

It's unfortunate that ‘.align’ doesn't work the same way on all
architectures.  To quote from the gas info page:

  "The way the required alignment is specified varies from system to
  system. For the a29k, hppa, m68k, m88k, w65, sparc, and Hitachi SH,
  and i386 using ELF format, the first expression is the alignment
  request in bytes. For example `.align 8' advances the location
  counter until it is a multiple of 8. If the location counter is
  already a multiple of 8, no change is needed.

  For other systems, including the i386 using a.out format, it is the
  number of low-order zero bits the location counter must have after
  advancement. For example `.align 3' advances the location counter
  until it a multiple of 8. If the location counter is already a
  multiple of 8, no change is needed."
  [ftp://ftp.gnu.org/old-gnu/Manuals/gas/html_chapter/as_7.html]

The info page goes on to note that there is a ‘.balign’ directive
(which is gas-specific, but I don't know if that matters) which always
byte aligns.

It's possible we don't care about i386 + a.out format, and maybe this
only affects weird old architectures.

So, ACK, but if there's an easy way to fix the above mess then that
could be better.

Rich.

--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.