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