]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ocaml/ocaml/contrib/tools/ocamlsize
Update
[l4.git] / l4 / pkg / ocaml / ocaml / contrib / tools / ocamlsize
1 #!/usr/bin/perl
2
3 foreach $f (@ARGV) {
4     open(FILE, $f) || die("Cannot open $f");
5     seek(FILE, -16, 2);
6     $num_sections = do read_int();
7     read(FILE, $magic, 12);
8     seek(FILE, -16 - 8 * $num_sections, 2);
9     @secname = ();
10     @seclength = ();
11     %length = ();
12     for ($i = 0; $i < $num_sections; $i++) {
13         read(FILE, $sec, 4);
14         $secname[$i] = $sec;
15         $seclength[$i] = do read_int();
16         $length{$sec} = $seclength[$i];
17     }
18     print $f, ":\n" if ($#ARGV > 0);
19     $path =
20         $length{'RNTM'} > 0 ?
21             do read_section('RNTM') :
22             "(default runtime)\n";
23     printf ("\tcode: %-7d data: %-7d symbols: %-7d debug: %-7d\n",
24             $length{'CODE'}, $length{'DATA'},
25             $length{'SYMB'}, $length{'DBUG'});
26     printf ("\tmagic number: %s  runtime system: %s",
27             $magic, $path);
28     close(FILE);
29 }
30
31 sub read_int {
32     read(FILE, $buff, 4) == 4 || die("Truncated bytecode file $f");
33     @int = unpack("C4", $buff);
34     return ($int[0] << 24) + ($int[1] << 16) + ($int[2] << 8) + $int[3];
35 }
36
37 sub read_section {
38     local ($sec) = @_;
39     local ($i, $ofs, $data);
40     for ($i = $num_sections - 1; $i >= 0; $i--) {
41         $ofs += $seclength[$i];
42         if ($secname[$i] eq $sec) {
43             seek(FILE, -16 - 8 * $num_sections - $ofs, 2);
44             read(FILE, $data, $seclength[$i]);
45             return $data;
46         }
47     }
48     return '';
49 }