Skip to contentSkip to conversation

reduce magic numbers length to 16 bytes (qemu-binfmt-conf.sh)

PATCH In review Anderson Lizardo 4 participants 6 messages Mar 11, 2006 – Mar 12, 2006 exec and binfmt

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 reduce magic numbers length to 16 bytes (qemu-binfmt-conf.sh)

Hi,

Any reason why the magic numbers from qemu-binfmt-conf.sh are not just 16 bytes long as those reported by "readelf --file-header"? For example:

$ readelf --file-header /bin/true | grep Magic:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00

Using only the first 16 bytes avoids the need to have separate entries for i386 and i486, given that they are emulated by the same program (qemu-i386). The others still need separate entries because of endianess.

Regards,
--
Anderson Lizardo
Embedded Linux Lab - 10LE
Nokia Institute of Technology - INdT
Manaus - Brazil

qemu-binfmt-conf.sh renamed from qemu-binfmt-conf.sh.orig +7 -8
@@ -20,20 +20,19 @@
 
 # register the interpreter for each cpu except for the native one
 if [ $cpu != "i386" ] ; then
-    echo ':i386:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00:\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/local/bin/qemu-i386:' > /proc/sys/fs/binfmt_misc/register
-    echo ':i486:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x00:\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/local/bin/qemu-i386:' > /proc/sys/fs/binfmt_misc/register
+    echo ':i386:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00:\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff:/usr/local/bin/qemu-i386:' > /proc/sys/fs/binfmt_misc/register
 fi
 if [ $cpu != "arm" ] ; then
-    echo   ':arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/local/bin/qemu-arm:' > /proc/sys/fs/binfmt_misc/register
-    echo   ':armeb:M::\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff:/usr/local/bin/qemu-armeb:' > /proc/sys/fs/binfmt_misc/register
+    echo ':arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff:/usr/local/bin/qemu-arm:' > /proc/sys/fs/binfmt_misc/register
+    echo ':armeb:M::\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff:/usr/local/bin/qemu-armeb:' > /proc/sys/fs/binfmt_misc/register
 fi
 if [ $cpu != "sparc" ] ; then
-    echo   ':sparc:M::\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff:/usr/local/bin/qemu-sparc:' > /proc/sys/fs/binfmt_misc/register
+    echo ':sparc:M::\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff:/usr/local/bin/qemu-sparc:' > /proc/sys/fs/binfmt_misc/register
 fi
 if [ $cpu != "ppc" ] ; then
-    echo   ':ppc:M::\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff:/usr/local/bin/qemu-ppc:' > /proc/sys/fs/binfmt_misc/register
+    echo ':ppc:M::\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff:/usr/local/bin/qemu-ppc:' > /proc/sys/fs/binfmt_misc/register
 fi
 if [ $cpu != "mips" ] ; then
-    echo   ':mips:M::\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff:/usr/local/bin/qemu-mips:' > /proc/sys/fs/binfmt_misc/register
-    echo   ':mipsel:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/local/bin/qemu-mipsel:' > /proc/sys/fs/binfmt_misc/register
+    echo ':mips:M::\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff:/usr/local/bin/qemu-mips:' > /proc/sys/fs/binfmt_misc/register
+    echo ':mipsel:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff:/usr/local/bin/qemu-mipsel:' > /proc/sys/fs/binfmt_misc/register
 fi
#1 lore raw
Quoted 4 lines · Anderson Lizardo

Hi,

Any reason why the magic numbers from qemu-binfmt-conf.sh are not just 16 bytes long as those reported by "readelf --file-header"?

The first 16 bytes only tell you it's an ELF executable. ie. you've just removed the bits that tell you whether it's e.g an arm or a ppc executable.

You'll notice that after your patch all the be/le pattern matches are identical, which is obviously not going to work.

Paul

#2 lore raw
In reply to Anderson Lizardo
Quoted 10 lines · Anderson Lizardo

Any reason why the magic numbers from qemu-binfmt-conf.sh are not just 16 bytes long as those reported by "readelf --file-header"? For example:

$ readelf --file-header /bin/true | grep Magic:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00

Using only the first 16 bytes avoids the need to have separate entries for i386 and i486, given that they are emulated by the same program (qemu-i386).

How would you distinguish it from an arm or mipsel binary?

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#3 lore raw
Quoted 6 lines · Andreas Schwab
Quoted 4 lines · Anderson Lizardo

Any reason why the magic numbers from qemu-binfmt-conf.sh are not just 16 bytes long as those reported by "readelf --file-header"? For example:

Quoted 8 lines
Quoted 6 lines
$ readelf --file-header /bin/true | grep Magic:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00

Using only the first 16 bytes avoids the need to have separate entries for i386 and i486, given that they are emulated by the same program (qemu-i386).

How would you distinguish it from an arm or mipsel binary?

Now I realize my mistake, sorry for the noise :/

To not make this thread completely useless: I find /usr/share/misc/file/magic very useful to get machine patterns (as detected by "file"). Search there for "magic for ELF executables" and you will find a fairly complete list of machine magic numbers.

Regards,
--
Anderson Lizardo
Embedded Linux Lab - 10LE
Nokia Institute of Technology - INdT
Manaus - Brazil

#4 lore raw
Quoted 4 lines

To not make this thread completely useless: I find /usr/share/misc/file/magic very useful to get machine patterns (as detected by "file"). Search there for "magic for ELF executables" and you will find a fairly complete list of machine magic numbers.

Or you could just use the official list in the gABI spec:

http://www.thescogroup.com/developers/gabi/latest/ch4.eheader.html

Paul

#5 lore raw

Hi,

Quoted 8 lines · Paul Brook
Quoted 4 lines

To not make this thread completely useless: I find /usr/share/misc/file/magic very useful to get machine patterns (as detected by "file"). Search there for "magic for ELF executables" and you will find a fairly complete list of machine magic numbers.

Or you could just use the official list in the gABI spec:

http://www.thescogroup.com/developers/gabi/latest/ch4.eheader.html

... but be aware they might sue you for using it ;-)

Ciao,
Dscho