2 # Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
11 unpack("L",pack("N",1))!=1 || die "only little-endian hosts are supported";
13 # first argument can specify custom suffix...
14 $suffix=(@ARGV[0]=~/^\$/) ? shift(@ARGV) : "\$m";
15 #################################################################
16 # rename segments in COFF modules according to %map table below #
17 %map=( ".text" => "fipstx$suffix", #
18 ".text\$"=> "fipstx$suffix", #
19 ".rdata" => "fipsrd$suffix", #
20 ".data" => "fipsda$suffix" ); #
21 #################################################################
25 if (/\*/) { push(@files,glob($_)); }
26 else { push(@files,$_); }
34 print "processing $file\n" unless $quiet;
36 sysopen(FD,$file,O_RDWR|O_BINARY) || die "sysopen($file): $!";
38 # read IMAGE_DOS_HEADER
39 sysread(FD,$mz,64)==64 || die "$file is too short";
40 @dos_header=unpack("a2C58I",$mz);
41 if (@dos_header[0] eq "MZ") {
42 $e_lfanew=pop(@dos_header);
43 sysseek(FD,$e_lfanew,SEEK_SET) || die "$file is too short";
44 sysread(FD,$Magic,4)==4 || die "$file is too short";
45 unpack("I",$Magic)==0x4550 || die "$file is not COFF image";
46 } elsif ($file =~ /\.obj$/i) {
47 # .obj files have no IMAGE_DOS_HEADER
48 sysseek(FD,0,SEEK_SET) || die "unable to rewind $file";
51 # read IMAGE_FILE_HEADER
52 sysread(FD,$coff,20)==20 || die "$file is too short";
53 ($Machine,$NumberOfSections,$TimeDateStamp,
54 $PointerToSymbolTable,$NumberOfSysmbols,
55 $SizeOfOptionalHeader,$Characteristics)=unpack("SSIIISS",$coff);
57 # skip over IMAGE_OPTIONAL_HEADER
58 sysseek(FD,$SizeOfOptionalHeader,SEEK_CUR) || die "$file is too short";
60 # traverse IMAGE_SECTION_HEADER table
61 for($i=0;$i<$NumberOfSections;$i++) {
62 sysread(FD,$SectionHeader,40)==40 || die "$file is too short";
63 ($Name,@opaque)=unpack("Z8C*",$SectionHeader);
65 sysseek(FD,-40,SEEK_CUR) || die "unable to rewind $file";
66 syswrite(FD,pack("a8C*",$map{$Name},@opaque))==40 || die "syswrite failed: $!";
67 printf " %-8s -> %.8s\n",$Name,$map{$Name} unless $quiet;