Do not silently truncate files on perlasm errors
[oweals/openssl.git] / crypto / md5 / asm / md5-x86_64.pl
index bc46f1938728137f97869a19849f1bf67e69da8a..99cbbea7749cc63d069a6339b2e875b9b618a9e3 100755 (executable)
@@ -1,11 +1,13 @@
-#!/usr/bin/perl -w
-#
-# MD5 optimized for AMD64.
-#
+#! /usr/bin/env perl
 # Author: Marc Bevand <bevand_m (at) epita.fr>
-# Licence: I hereby disclaim the copyright on this code and place it
-# in the public domain.
+# Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
 #
+# Licensed under the Apache License 2.0 (the "License").  You may not use
+# this file except in compliance with the License.  You can obtain a copy
+# in the file LICENSE in the source distribution or at
+# https://www.openssl.org/source/license.html
+
+# MD5 optimized for AMD64.
 
 use strict;
 
@@ -47,8 +49,8 @@ sub round2_step
     $code .= " mov     %edx,           %r12d           /* (NEXT STEP) z' = %edx */\n" if ($pos == -1);
     $code .= <<EOF;
        not     %r11d                           /* not z */
-       lea     $T_i($dst,%r10d),$dst           /* Const + dst + ... */
        and     $x,             %r12d           /* x & z */
+       lea     $T_i($dst,%r10d),$dst           /* Const + dst + ... */
        and     $y,             %r11d           /* y & (not z) */
        mov     $k_next*4(%rsi),%r10d           /* (NEXT STEP) X[$k_next] */
        or      %r11d,          %r12d           /* (y & (not z)) | (x & z) */
@@ -65,6 +67,7 @@ EOF
 #   %r10d = X[k_next]
 #   %r11d = y' (copy of y for the next step)
 # Each round3_step() takes about 4.2 clocks (8 instructions, 1.9 IPC)
+{ my $round3_alter=0;
 sub round3_step
 {
     my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
@@ -75,10 +78,20 @@ sub round3_step
        mov     $k_next*4(%rsi),%r10d           /* (NEXT STEP) X[$k_next] */
        xor     $x,             %r11d           /* x ^ ... */
        add     %r11d,          $dst            /* dst += ... */
+EOF
+    $code .= <<EOF if ($round3_alter);
        rol     \$$s,           $dst            /* dst <<< s */
        mov     $x,             %r11d           /* (NEXT STEP) y' = $x */
+EOF
+    $code .= <<EOF if (!$round3_alter);
+       mov     $x,             %r11d           /* (NEXT STEP) y' = $x */
+       rol     \$$s,           $dst            /* dst <<< s */
+EOF
+    $code .= <<EOF;
        add     $x,             $dst            /* dst += x */
 EOF
+    $round3_alter^=1;
+}
 }
 
 # round4_step() does:
@@ -105,9 +118,11 @@ sub round4_step
 EOF
 }
 
-my $flavour = shift;
-my $output  = shift;
-if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
+no warnings qw(uninitialized);
+# $output is the last argument if it looks like a file (it has an extension)
+# $flavour is the first argument if it doesn't look like a file
+my $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
+my $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
 
 my $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
 
@@ -116,8 +131,9 @@ $0 =~ m/(.*[\/\\])[^\/\\]+$/; my $dir=$1; my $xlate;
 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
 die "can't locate x86_64-xlate.pl";
 
-no warnings qw(uninitialized);
-open STDOUT,"| $^X $xlate $flavour $output";
+open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
+    or die "can't call $xlate: $!";
+*STDOUT=*OUT;
 
 $code .= <<EOF;
 .text
@@ -126,11 +142,17 @@ $code .= <<EOF;
 .globl md5_block_asm_data_order
 .type md5_block_asm_data_order,\@function,3
 md5_block_asm_data_order:
+.cfi_startproc
        push    %rbp
+.cfi_push      %rbp
        push    %rbx
+.cfi_push      %rbx
        push    %r12
+.cfi_push      %r12
        push    %r14
+.cfi_push      %r14
        push    %r15
+.cfi_push      %r15
 .Lprologue:
 
        # rdi = arg #1 (ctx, MD5_CTX pointer)
@@ -247,13 +269,20 @@ $code .= <<EOF;
        mov     %edx,           3*4(%rbp)       # ctx->D = D
 
        mov     (%rsp),%r15
+.cfi_restore   %r15
        mov     8(%rsp),%r14
+.cfi_restore   %r14
        mov     16(%rsp),%r12
+.cfi_restore   %r12
        mov     24(%rsp),%rbx
+.cfi_restore   %rbx
        mov     32(%rsp),%rbp
+.cfi_restore   %rbp
        add     \$40,%rsp
+.cfi_adjust_cfa_offset -40
 .Lepilogue:
        ret
+.cfi_endproc
 .size md5_block_asm_data_order,.-md5_block_asm_data_order
 EOF
 
@@ -363,4 +392,4 @@ ___
 
 print $code;
 
-close STDOUT;
+close STDOUT or die "error closing STDOUT";