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