]> git.ipfire.org Git - thirdparty/openssl.git/blame - ms/segrenam.pl
Copyright consolidation: perl files
[thirdparty/openssl.git] / ms / segrenam.pl
CommitLineData
e0a65194
RS
1#! /usr/bin/env perl
2# Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
3#
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
544c84b7
DSH
8
9my $quiet = 1;
10
11unpack("L",pack("N",1))!=1 || die "only little-endian hosts are supported";
12
13# first argument can specify custom suffix...
14$suffix=(@ARGV[0]=~/^\$/) ? shift(@ARGV) : "\$m";
15#################################################################
76c1183d
VS
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" ); #
544c84b7
DSH
21#################################################################
22
23# collect file list
24foreach (@ARGV) {
76c1183d
VS
25 if (/\*/) { push(@files,glob($_)); }
26 else { push(@files,$_); }
544c84b7
DSH
27}
28
29use Fcntl;
30use Fcntl ":seek";
31
32foreach (@files) {
33 $file=$_;
34 print "processing $file\n" unless $quiet;
35
36 sysopen(FD,$file,O_RDWR|O_BINARY) || die "sysopen($file): $!";
37
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") {
76c1183d
VS
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";
544c84b7 46 } elsif ($file =~ /\.obj$/i) {
76c1183d
VS
47 # .obj files have no IMAGE_DOS_HEADER
48 sysseek(FD,0,SEEK_SET) || die "unable to rewind $file";
544c84b7
DSH
49 } else { next; }
50
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);
56
57 # skip over IMAGE_OPTIONAL_HEADER
58 sysseek(FD,$SizeOfOptionalHeader,SEEK_CUR) || die "$file is too short";
59
60 # traverse IMAGE_SECTION_HEADER table
61 for($i=0;$i<$NumberOfSections;$i++) {
76c1183d
VS
62 sysread(FD,$SectionHeader,40)==40 || die "$file is too short";
63 ($Name,@opaque)=unpack("Z8C*",$SectionHeader);
64 if ($map{$Name}) {
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;
68 }
544c84b7
DSH
69 }
70 close(FD);
71}