[Libguestfs] [PATCH] febootstrap-supermin-helper: Replace objcopy call for embedding init binary

Hilko Bengen bengen at hilluzination.de
Wed Aug 24 15:16:51 UTC 2011


objcopy needs "output-target" and "binary-architecture" parameters
which makes it necessary to keep a list of known architectures.

The bin2s.pl script generates input for the GNU assembler which should
produce an object file that is equivalent to that produced by objcopy.

I have successfully tested the change on an amd64 Debian/unstable system.
---
 helper/Makefile.am      |   10 ++++++++--
 helper/bin2s.pl         |   45 +++++++++++++++++++++++++++++++++++++++++++++
 helper/elf-default-arch |   32 --------------------------------
 3 files changed, 53 insertions(+), 34 deletions(-)
 create mode 100755 helper/bin2s.pl
 delete mode 100755 helper/elf-default-arch

diff --git a/helper/Makefile.am b/helper/Makefile.am
index 01a6af1..ab7f692 100644
--- a/helper/Makefile.am
+++ b/helper/Makefile.am
@@ -45,11 +45,17 @@ init_LDFLAGS = -static
 # http://www.doof.me.uk/2010/05/07/cute-objcopy-hack/
 ELF_DEFAULT_ARCH = $(shell $(srcdir)/elf-default-arch | gawk '{ print $$1 }')
 DEFAULT_ARCH = $(shell $(srcdir)/elf-default-arch | gawk '{ print $$2 }')
-ext2init.o: init
+
+CLEANFILES = ext2init.S
+
+ext2init.o: ext2init.S
+	$(CC) -o $@ -c $<
+
+ext2init.S: init
 	strip --strip-all $<
 	@file $< | grep -isq static || \
 	  (echo "*** error: init is not staticly linked"; exit 1)
-	objcopy -I binary -B $(DEFAULT_ARCH) -O $(ELF_DEFAULT_ARCH) $< $@
+	./bin2s.pl $< $@
 
 man_MANS = \
 	febootstrap-supermin-helper.8
diff --git a/helper/bin2s.pl b/helper/bin2s.pl
new file mode 100755
index 0000000..2c78b5e
--- /dev/null
+++ b/helper/bin2s.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+
+# This script creates a source file for the GNU assembler which shuold
+# result in an object file equivalent to that of
+#
+# objcopy -I binary -B $(DEFAULT_ARCH) -O $(ELF_DEFAULT_ARCH) <in> <out>
+
+use strict;
+use warnings;
+
+die "usage: $0 <in> <out>\n" if @ARGV != 2;
+
+my ($infile, $outfile) = @ARGV;
+my ($buf, $i, $sz);
+open my $ifh, '<', $infile or die "open $infile: $!";
+open my $ofh, '>', $outfile or die "open $outfile: $!";
+
+print $ofh <<"EOF";
+/* This file has been automatically generated from $infile by $0 */
+
+\t.globl\t_binary_${infile}_start
+\t.globl\t_binary_${infile}_end
+\t.globl\t_binary_${infile}_size
+
+\t.section\t.data
+_binary_${infile}_start:
+EOF
+
+$sz = 0;
+while ( $i = read $ifh, $buf, 12 ) {
+    print $ofh "\t.byte\t"
+      . join( ',', map { sprintf '0x%02x', ord $_ } split //, $buf ) . "\n";
+    $sz += $i;
+}
+die "read $infile (at offset $sz): $!\n" if not defined $i;
+close $ifh;
+
+print $ofh <<"EOF";
+
+_binary_${infile}_end:
+
+\t.equ _binary_${infile}_size, $sz
+EOF
+
+close $ofh;
diff --git a/helper/elf-default-arch b/helper/elf-default-arch
deleted file mode 100755
index 54af14d..0000000
--- a/helper/elf-default-arch
+++ /dev/null
@@ -1,32 +0,0 @@
-#!/bin/sh -
-# Copyright (C) 2010 Red Hat Inc.
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-# Calculate the default ELF object architecture for this format.
-# There doesn't seem to be an easy way to derive this from binutils,
-# so instead we hard code it.
-
-case $(uname -m) in
-    i[3456]86) echo "elf32-i386 i386" ;;
-    x86_64) echo "elf64-x86-64 i386" ;;
-    s390) echo "elf32-s390 s390:31-bit" ;;
-    s390x) echo "elf64-s390 s390:31-bit" ;;
-    ppc) echo "elf32-powerpc powerpc" ;;
-    ppc64) echo "elf64-powerpc powerpc" ;;
-    *)
-        echo "This architecture is not recognized.  Please update helper/elf-default-arch."
-        exit 1
-esac
-- 
1.7.5.4




More information about the Libguestfs mailing list