File: bin2s.pl

package info (click to toggle)
febootstrap 3.17-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,116 kB
  • sloc: ansic: 25,034; sh: 4,118; ml: 967; makefile: 143; perl: 28
file content (45 lines) | stat: -rwxr-xr-x 1,022 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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;