| 1 |
#! /usr/bin/env perl |
| 2 |
# Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
# SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
use strict; |
| 6 |
|
| 7 |
my $flavour = shift; |
| 8 |
my $output = shift; |
| 9 |
open STDOUT,">$output" || die "can't open $output: $!"; |
| 10 |
|
| 11 |
$flavour = "linux32" if (!$flavour or $flavour eq "void"); |
| 12 |
|
| 13 |
my %GLOBALS; |
| 14 |
my $dotinlocallabels=($flavour=~/linux/)?1:0; |
| 15 |
|
| 16 |
################################################################ |
| 17 |
# directives which need special treatment on different platforms |
| 18 |
################################################################ |
| 19 |
my $arch = sub { |
| 20 |
if ($flavour =~ /linux/) { ".arch\t".join(',',@_); } |
| 21 |
elsif ($flavour =~ /win64/) { ".arch\t".join(',',@_); } |
| 22 |
else { ""; } |
| 23 |
}; |
| 24 |
my $fpu = sub { |
| 25 |
if ($flavour =~ /linux/) { ".fpu\t".join(',',@_); } |
| 26 |
else { ""; } |
| 27 |
}; |
| 28 |
my $hidden = sub { |
| 29 |
if ($flavour =~ /ios/) { ".private_extern\t".join(',',@_); } |
| 30 |
elsif ($flavour =~ /win64/) { ""; } |
| 31 |
else { ".hidden\t".join(',',@_); } |
| 32 |
}; |
| 33 |
my $comm = sub { |
| 34 |
my @args = split(/,\s*/,shift); |
| 35 |
my $name = @args[0]; |
| 36 |
my $global = \$GLOBALS{$name}; |
| 37 |
my $ret; |
| 38 |
|
| 39 |
if ($flavour =~ /ios32/) { |
| 40 |
$ret = ".comm\t_$name,@args[1]\n"; |
| 41 |
$ret .= ".non_lazy_symbol_pointer\n"; |
| 42 |
$ret .= "$name:\n"; |
| 43 |
$ret .= ".indirect_symbol\t_$name\n"; |
| 44 |
$ret .= ".long\t0"; |
| 45 |
$name = "_$name"; |
| 46 |
} else { $ret = ".comm\t".join(',',@args); } |
| 47 |
|
| 48 |
$$global = $name; |
| 49 |
$ret; |
| 50 |
}; |
| 51 |
my $globl = sub { |
| 52 |
my $name = shift; |
| 53 |
my $global = \$GLOBALS{$name}; |
| 54 |
my $ret; |
| 55 |
|
| 56 |
SWITCH: for ($flavour) { |
| 57 |
/ios/ && do { $name = "_$name"; |
| 58 |
last; |
| 59 |
}; |
| 60 |
} |
| 61 |
|
| 62 |
$ret = ".globl $name\n"; |
| 63 |
# All symbols in assembly files are hidden. |
| 64 |
$ret .= &$hidden($name); |
| 65 |
$$global = $name; |
| 66 |
$ret; |
| 67 |
}; |
| 68 |
my $global = $globl; |
| 69 |
my $extern = sub { |
| 70 |
&$globl(@_); |
| 71 |
return; # return nothing |
| 72 |
}; |
| 73 |
my $type = sub { |
| 74 |
if ($flavour =~ /linux/) { ".type\t".join(',',@_); } |
| 75 |
elsif ($flavour =~ /ios32/) { if (join(',',@_) =~ /(\w+),%function/) { |
| 76 |
"#ifdef __thumb2__\n". |
| 77 |
".thumb_func $1\n". |
| 78 |
"#endif"; |
| 79 |
} |
| 80 |
} |
| 81 |
elsif ($flavour =~ /win64/) { if (join(',',@_) =~ /(\w+),%function/) { |
| 82 |
# See https://sourceware.org/binutils/docs/as/Pseudo-Ops.html |
| 83 |
# Per https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#coff-symbol-table, |
| 84 |
# the type for functions is 0x20, or 32. |
| 85 |
".def $1\n". |
| 86 |
" .type 32\n". |
| 87 |
".endef"; |
| 88 |
} |
| 89 |
} |
| 90 |
else { ""; } |
| 91 |
}; |
| 92 |
my $size = sub { |
| 93 |
if ($flavour =~ /linux/) { ".size\t".join(',',@_); } |
| 94 |
else { ""; } |
| 95 |
}; |
| 96 |
my $inst = sub { |
| 97 |
if ($flavour =~ /linux/) { ".inst\t".join(',',@_); } |
| 98 |
else { ".long\t".join(',',@_); } |
| 99 |
}; |
| 100 |
my $asciz = sub { |
| 101 |
my $line = join(",",@_); |
| 102 |
if ($line =~ /^"(.*)"$/) |
| 103 |
{ ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; } |
| 104 |
else |
| 105 |
{ ""; } |
| 106 |
}; |
| 107 |
my $section = sub { |
| 108 |
if ($flavour =~ /ios/) { |
| 109 |
if ($_[0] eq ".rodata") { |
| 110 |
return ".section\t__TEXT,__const"; |
| 111 |
} |
| 112 |
die "Unknown section name $_[0]"; |
| 113 |
} else { |
| 114 |
return ".section\t" . join(",", @_); |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
sub range { |
| 119 |
my ($r,$sfx,$start,$end) = @_; |
| 120 |
|
| 121 |
join(",",map("$r$_$sfx",($start..$end))); |
| 122 |
} |
| 123 |
|
| 124 |
sub expand_line { |
| 125 |
my $line = shift; |
| 126 |
my @ret = (); |
| 127 |
|
| 128 |
pos($line)=0; |
| 129 |
|
| 130 |
while ($line =~ m/\G[^@\/\{\"]*/g) { |
| 131 |
if ($line =~ m/\G(@|\/\/|$)/gc) { |
| 132 |
last; |
| 133 |
} |
| 134 |
elsif ($line =~ m/\G\{/gc) { |
| 135 |
my $saved_pos = pos($line); |
| 136 |
$line =~ s/\G([rdqv])([0-9]+)([^\-]*)\-\1([0-9]+)\3/range($1,$3,$2,$4)/e; |
| 137 |
pos($line) = $saved_pos; |
| 138 |
$line =~ m/\G[^\}]*\}/g; |
| 139 |
} |
| 140 |
elsif ($line =~ m/\G\"/gc) { |
| 141 |
$line =~ m/\G[^\"]*\"/g; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
$line =~ s/\b(\w+)/$GLOBALS{$1} or $1/ge; |
| 146 |
|
| 147 |
return $line; |
| 148 |
} |
| 149 |
|
| 150 |
my ($arch_defines, $target_defines); |
| 151 |
if ($flavour =~ /32/) { |
| 152 |
$arch_defines = "defined(OPENSSL_ARM)"; |
| 153 |
} elsif ($flavour =~ /64/) { |
| 154 |
$arch_defines = "defined(OPENSSL_AARCH64)"; |
| 155 |
} else { |
| 156 |
die "unknown architecture: $flavour"; |
| 157 |
} |
| 158 |
if ($flavour =~ /linux/) { |
| 159 |
# Although the flavour is specified as "linux", it is really used by all |
| 160 |
# ELF platforms. |
| 161 |
$target_defines = "defined(__ELF__)"; |
| 162 |
} elsif ($flavour =~ /ios/) { |
| 163 |
# Although the flavour is specified as "ios", it is really used by all Apple |
| 164 |
# platforms. |
| 165 |
$target_defines = "defined(__APPLE__)"; |
| 166 |
} elsif ($flavour =~ /win/) { |
| 167 |
$target_defines = "defined(_WIN32)"; |
| 168 |
} else { |
| 169 |
die "unknown target: $flavour"; |
| 170 |
} |
| 171 |
|
| 172 |
print <<___; |
| 173 |
// This file is generated from a similarly-named Perl script in the BoringSSL |
| 174 |
// source tree. Do not edit by hand. |
| 175 |
|
| 176 |
#include <openssl/asm_base.h> |
| 177 |
|
| 178 |
#if !defined(OPENSSL_NO_ASM) && $arch_defines && $target_defines |
| 179 |
___ |
| 180 |
|
| 181 |
while(my $line=<>) { |
| 182 |
|
| 183 |
if ($line =~ m/^\s*(#|@|\/\/)/) { print $line; next; } |
| 184 |
|
| 185 |
$line =~ s|/\*.*\*/||; # get rid of C-style comments... |
| 186 |
$line =~ s|^\s+||; # ... and skip white spaces in beginning... |
| 187 |
$line =~ s|\s+$||; # ... and at the end |
| 188 |
|
| 189 |
if ($flavour =~ /64/) { |
| 190 |
my $copy = $line; |
| 191 |
# Also remove line comments. |
| 192 |
$copy =~ s|//.*||; |
| 193 |
if ($copy =~ /\b[wx]18\b/) { |
| 194 |
die "r18 is reserved by the platform and may not be used."; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
{ |
| 199 |
$line =~ s|[\b\.]L(\w{2,})|L$1|g; # common denominator for Locallabel |
| 200 |
$line =~ s|\bL(\w{2,})|\.L$1|g if ($dotinlocallabels); |
| 201 |
} |
| 202 |
|
| 203 |
{ |
| 204 |
$line =~ s|(^[\.\w]+)\:\s*||; |
| 205 |
my $label = $1; |
| 206 |
if ($label) { |
| 207 |
printf "%s:",($GLOBALS{$label} or $label); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if ($line !~ m/^[#@]/) { |
| 212 |
$line =~ s|^\s*(\.?)(\S+)\s*||; |
| 213 |
my $c = $1; $c = "\t" if ($c eq ""); |
| 214 |
my $mnemonic = $2; |
| 215 |
my $opcode; |
| 216 |
if ($mnemonic =~ m/([^\.]+)\.([^\.]+)/) { |
| 217 |
$opcode = eval("\$$1_$2"); |
| 218 |
} else { |
| 219 |
$opcode = eval("\$$mnemonic"); |
| 220 |
} |
| 221 |
|
| 222 |
if ($flavour =~ /ios/) { |
| 223 |
# Mach-O and ELF use different syntax for these relocations. Note |
| 224 |
# that we require :pg_hi21: to be explicitly listed. It is normally |
| 225 |
# optional with adrp instructions. |
| 226 |
$line =~ s|:pg_hi21:(\w+)|\1\@PAGE|; |
| 227 |
$line =~ s|:lo12:(\w+)|\1\@PAGEOFF|; |
| 228 |
} else { |
| 229 |
# Clang's integrated assembly does not support the optional |
| 230 |
# :pg_hi21: markers, so erase them. |
| 231 |
$line =~ s|:pg_hi21:||; |
| 232 |
} |
| 233 |
|
| 234 |
my $arg=expand_line($line); |
| 235 |
|
| 236 |
if (ref($opcode) eq 'CODE') { |
| 237 |
$line = &$opcode($arg); |
| 238 |
} elsif ($mnemonic) { |
| 239 |
$line = $c.$mnemonic; |
| 240 |
$line.= "\t$arg" if ($arg ne ""); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
print $line if ($line); |
| 245 |
print "\n"; |
| 246 |
} |
| 247 |
|
| 248 |
print <<___; |
| 249 |
#endif // !OPENSSL_NO_ASM && $arch_defines && $target_defines |
| 250 |
___ |
| 251 |
|
| 252 |
close STDOUT or die "error closing STDOUT: $!"; |