Skip to content

Commit

Permalink
Move data from .text to .rodata on x86_64
Browse files Browse the repository at this point in the history
Various constants and strings identifying the authors are currently
misplaced in .text. This change allows using execute-only .text on
platforms that enforce it by default, such as OpenBSD.

Modify x86_64-xlate.pl to replace .rodata with __DATA,__const for macs.
Adapt the nasm/masm path to emit an .rdata segment with alignment of 8.
This last change is not strictly needed but makes things explicit.

Change-Id: If716b892c1faabd85c6c70bdd75e145304841f83
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/57445
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
(cherry picked from commit ebd43ef8dd3c062dccf6adf264c5332efda1f4b3)
  • Loading branch information
botovq authored and skmcgrail committed Apr 17, 2023
1 parent 05293a8 commit 9772b19
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions crypto/chacha/asm/chacha-x86_64.pl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
.extern OPENSSL_ia32cap_P
.section .rodata
.align 64
.Lzero:
.long 0,0,0,0
Expand Down Expand Up @@ -111,6 +112,7 @@
.Lsixteen:
.long 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
.asciz "ChaCha20 for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
.text
___

sub AUTOLOAD() # thunk [simplified] 32-bit style perlasm
Expand Down
2 changes: 1 addition & 1 deletion crypto/cipher_extra/asm/aes128gcmsiv-x86_64.pl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*STDOUT=*OUT;

$code.=<<___;
.data
.section .rodata
.align 16
one:
Expand Down
2 changes: 2 additions & 0 deletions crypto/cipher_extra/asm/chacha20_poly1305_x86_64.pl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
chacha20_poly1305_constants:
.section .rodata
.align 64
.Lchacha20_consts:
.byte 'e','x','p','a','n','d',' ','3','2','-','b','y','t','e',' ','k'
Expand Down Expand Up @@ -83,6 +84,7 @@
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
.text
___

my ($oup,$inp,$inl,$adp,$keyp,$itr1,$itr2,$adl)=("%rdi","%rsi","%rbx","%rcx","%r9","%rcx","%r8","%r8");
Expand Down
2 changes: 2 additions & 0 deletions crypto/fipsmodule/aes/asm/aesni-x86_64.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3781,6 +3781,7 @@ sub aesni_generate8 {
}

$code.=<<___;
.section .rodata
.align 64
.Lbswap_mask:
.byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
Expand All @@ -3803,6 +3804,7 @@ sub aesni_generate8 {
.asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
.align 64
.text
___

# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
Expand Down
2 changes: 2 additions & 0 deletions crypto/fipsmodule/aes/asm/vpaes-x86_64.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,7 @@
## ##
########################################################
.type _vpaes_consts,\@object
.section .rodata
.align 64
_vpaes_consts:
.Lk_inv: # inv, inva
Expand Down Expand Up @@ -1400,6 +1401,7 @@
.asciz "Vector Permutation AES for x86_64/SSSE3, Mike Hamburg (Stanford University)"
.align 64
.size _vpaes_consts,.-_vpaes_consts
.text
___

if ($win64) {
Expand Down
2 changes: 2 additions & 0 deletions crypto/fipsmodule/bn/asm/rsaz-avx2.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,7 @@
}

$code.=<<___;
.section .rodata
.align 64
.Land_mask:
.quad 0x1fffffff,0x1fffffff,0x1fffffff,0x1fffffff
Expand All @@ -1754,6 +1755,7 @@
.long 2,2,2,2, 3,3,3,3
.long 4,4,4,4, 4,4,4,4
.align 64
.text
___

if ($win64) {
Expand Down
2 changes: 2 additions & 0 deletions crypto/fipsmodule/bn/asm/x86_64-mont5.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3580,11 +3580,13 @@
___
}
$code.=<<___;
.section .rodata
.align 64
.Linc:
.long 0,0, 1,1
.long 2,2, 2,2
.asciz "Montgomery Multiplication with scatter/gather for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
.text
___

# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
Expand Down
2 changes: 2 additions & 0 deletions crypto/fipsmodule/ec/asm/p256-x86_64-asm.pl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
.extern OPENSSL_ia32cap_P
# The polynomial
.section .rodata
.align 64
.Lpoly:
.quad 0xffffffffffffffff, 0x00000000ffffffff, 0x0000000000000000, 0xffffffff00000001
Expand All @@ -84,6 +85,7 @@
.quad 0xf3b9cac2fc632551, 0xbce6faada7179e84, 0xffffffffffffffff, 0xffffffff00000000
.LordK:
.quad 0xccd1c8aaee00bc4f
.text
___

{
Expand Down
2 changes: 2 additions & 0 deletions crypto/fipsmodule/modes/asm/aesni-gcm-x86_64.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@
___

$code.=<<___;
.section .rodata
.align 64
.Lbswap_mask:
.byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
Expand All @@ -1067,6 +1068,7 @@
.byte 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
.asciz "AES-NI GCM module for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
.align 64
.text
___
}}} else {{{
$code=<<___; # assembler is too old
Expand Down
2 changes: 2 additions & 0 deletions crypto/fipsmodule/modes/asm/ghash-ssse3-x86_64.pl
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ sub process_rows {
.seh_endproc
.size gcm_ghash_ssse3,.-gcm_ghash_ssse3
.section .rodata
.align 16
# .Lreverse_bytes is a permutation which, if applied with pshufb, reverses the
# bytes in an XMM register.
Expand All @@ -342,6 +343,7 @@ sub process_rows {
# .Llow4_mask is an XMM mask which selects the low four bits of each byte.
.Llow4_mask:
.quad 0x0f0f0f0f0f0f0f0f, 0x0f0f0f0f0f0f0f0f
.text
____

print $code;
Expand Down
2 changes: 2 additions & 0 deletions crypto/fipsmodule/modes/asm/ghash-x86_64.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,7 @@ sub reduction_avx {
}

$code.=<<___;
.section .rodata
.align 64
.Lbswap_mask:
.byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
Expand All @@ -1314,6 +1315,7 @@ sub reduction_avx {
.asciz "GHASH for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
.align 64
.text
___

$code =~ s/\`([^\`]*)\`/eval($1)/gem;
Expand Down
2 changes: 2 additions & 0 deletions crypto/fipsmodule/sha/asm/sha1-x86_64.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1819,6 +1819,7 @@ ()
}
}
$code.=<<___;
.section .rodata
.align 64
K_XX_XX:
.long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
Expand All @@ -1837,6 +1838,7 @@ ()
$code.=<<___;
.asciz "SHA1 block transform for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
.align 64
.text
___

# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
Expand Down
4 changes: 4 additions & 0 deletions crypto/fipsmodule/sha/asm/sha512-x86_64.pl
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ ()

if ($SZ==4) {
$code.=<<___;
.section .rodata
.align 64
.type $TABLE,\@object
$TABLE:
Expand Down Expand Up @@ -451,9 +452,11 @@ ()
.long 0xffffffff,0xffffffff,0x03020100,0x0b0a0908
.long 0xffffffff,0xffffffff,0x03020100,0x0b0a0908
.asciz "SHA256 block transform for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
.text
___
} else {
$code.=<<___;
.section .rodata
.align 64
.type $TABLE,\@object
$TABLE:
Expand Down Expand Up @@ -541,6 +544,7 @@ ()
.quad 0x0001020304050607,0x08090a0b0c0d0e0f
.quad 0x0001020304050607,0x08090a0b0c0d0e0f
.asciz "SHA512 block transform for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
.text
___
}

Expand Down
8 changes: 6 additions & 2 deletions crypto/perlasm/x86_64-xlate.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,9 @@
$self->{value} = ".p2align\t" . (log($$line)/log(2));
} elsif ($dir eq ".section") {
$current_segment=$$line;
if (!$elf && $current_segment eq ".rodata") {
if ($flavour eq "macosx") { $self->{value} = ".section\t__DATA,__const"; }
}
if (!$elf && $current_segment eq ".init") {
if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; }
elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; }
Expand Down Expand Up @@ -1159,9 +1162,10 @@
/\.section/ && do { my $v=undef;
$$line =~ s/([^,]*).*/$1/;
$$line = ".CRT\$XCU" if ($$line eq ".init");
$$line = ".rdata" if ($$line eq ".rodata");
if ($nasm) {
$v="section $$line";
if ($$line=~/\.([px])data/) {
if ($$line=~/\.([prx])data/) {
$v.=" rdata align=";
$v.=$1 eq "p"? 4 : 8;
} elsif ($$line=~/\.CRT\$/i) {
Expand All @@ -1170,7 +1174,7 @@
} else {
$v="$current_segment\tENDS\n" if ($current_segment);
$v.="$$line\tSEGMENT";
if ($$line=~/\.([px])data/) {
if ($$line=~/\.([prx])data/) {
$v.=" READONLY";
$v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref);
} elsif ($$line=~/\.CRT\$/i) {
Expand Down

0 comments on commit 9772b19

Please sign in to comment.