helper: Print /modules when verbose >= 2
[febootstrap.git] / helper / bin2s.pl
1 #!/usr/bin/perl
2
3 # This script creates a source file for the GNU assembler which shuold
4 # result in an object file equivalent to that of
5 #
6 # objcopy -I binary -B $(DEFAULT_ARCH) -O $(ELF_DEFAULT_ARCH) <in> <out>
7
8 use strict;
9 use warnings;
10
11 die "usage: $0 <in> <out>\n" if @ARGV != 2;
12
13 my ($infile, $outfile) = @ARGV;
14 my ($buf, $i, $sz);
15 open my $ifh, '<', $infile or die "open $infile: $!";
16 open my $ofh, '>', $outfile or die "open $outfile: $!";
17
18 print $ofh <<"EOF";
19 /* This file has been automatically generated from $infile by $0 */
20
21 \t.globl\t_binary_${infile}_start
22 \t.globl\t_binary_${infile}_end
23 \t.globl\t_binary_${infile}_size
24
25 \t.section\t.data
26 _binary_${infile}_start:
27 EOF
28
29 $sz = 0;
30 while ( $i = read $ifh, $buf, 12 ) {
31     print $ofh "\t.byte\t"
32       . join( ',', map { sprintf '0x%02x', ord $_ } split //, $buf ) . "\n";
33     $sz += $i;
34 }
35 die "read $infile (at offset $sz): $!\n" if not defined $i;
36 close $ifh;
37
38 print $ofh <<"EOF";
39
40 _binary_${infile}_end:
41
42 \t.equ _binary_${infile}_size, $sz
43 EOF
44
45 close $ofh;