From 0daba2e61ddef26a021b3f4b31db52559eaa11c3 Mon Sep 17 00:00:00 2001 From: Zhi Guan Date: Sat, 13 Apr 2024 17:37:47 +0800 Subject: [PATCH] Generate compiler compatible assembly symbols Typically when compiling a function `foo`, GCC will add a prefix `_` to the symbol, i.e., generate `_foo`. But on some platforms, the compiler will not add prefix. option `ENABLE_ASM_UNDERSCORE_PREFIX` change the default name of global symbols in assembly code. --- CMakeLists.txt | 6 ++++++ include/gmssl/asm.h | 20 ++++++++++++++++++++ src/gf128_aarch64.S | 9 +++++++-- src/sm4_aarch64.S | 15 +++++++++------ 4 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 include/gmssl/asm.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c07cc000..ba582043 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,9 +20,11 @@ option(ENABLE_SM4_CFB "Enable SM4 CFB mode" OFF) option(ENABLE_SM4_CBC_MAC "Enable SM4-CBC-MAC" OFF) option(ENABLE_SM4_CCM "Enable SM4 CCM mode" OFF) +option(ENABLE_ASM_UNDERSCORE_PREFIX "Add prefix `_` to assembly symbols" ON) option(ENABLE_GMUL_AARCH64 "Enable GF(2^128) Multiplication AArch64 assembly" OFF) + set(src src/version.c src/debug.c @@ -257,6 +259,10 @@ if (ENABLE_SM2_ALGOR_ID_ENCODE_NULL) add_definitions(-DENABLE_SM2_ALGOR_ID_ENCODE_NULL) endif() +if (ENABLE_ASM_UNDERSCORE_PREFIX) + message(STATUS "ENABLE_ASM_UNDERSCORE_PREFIX is ON") + add_definitions(-DENABLE_ASM_UNDERSCORE_PREFIX) +endif() if (ENABLE_GMUL_AARCH64) message(STATUS "ENABLE_GMUL_AARCH64 is ON") diff --git a/include/gmssl/asm.h b/include/gmssl/asm.h new file mode 100644 index 00000000..225883e5 --- /dev/null +++ b/include/gmssl/asm.h @@ -0,0 +1,20 @@ +/* + * Copyright 2014-2024 The GmSSL Project. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ + + +#ifndef GMSSL_ASM_H +#define GMSSL_ASM_H + +#ifdef ENABLE_ASM_UNDERSCORE_PREFIX +# define func(foo) _##foo +#else +# define func(foo) foo +#endif + +#endif diff --git a/src/gf128_aarch64.S b/src/gf128_aarch64.S index 939e401c..302347a5 100644 --- a/src/gf128_aarch64.S +++ b/src/gf128_aarch64.S @@ -8,6 +8,9 @@ */ +#include + + /* GF(2^128) defined by f(x) = x^128 + x^7 + x^2 + x + 1 f0 = x^128 = x^7 + x^2 + x + 1 @@ -29,9 +32,11 @@ = c + (e0 + w0) * x^64 + (d0 + w1) * f0 */ .text -.globl _gf128_mul + +.globl func(gf128_mul) .align 4 -_gf128_mul: + +func(gf128_mul): // load (a0, a1) ld1 {v1.2d},[x1] // load (b0, b1) diff --git a/src/sm4_aarch64.S b/src/sm4_aarch64.S index 2470062f..4fbd64ff 100644 --- a/src/sm4_aarch64.S +++ b/src/sm4_aarch64.S @@ -7,6 +7,7 @@ * http://www.apache.org/licenses/LICENSE-2.0 */ +#include .align 7 @@ -62,9 +63,10 @@ Llshift: .byte 4,5,6,7, 8,9,10,11, 12,13,14,15, 0,1,2,3 -.globl _sm4_set_encrypt_key +.globl func(sm4_set_encrypt_key) .align 4 -_sm4_set_encrypt_key: + +func(sm4_set_encrypt_key): // load const v16..v31 = SBox adr x3, LSBOX @@ -140,9 +142,10 @@ _sm4_set_encrypt_key: ret -.globl _sm4_set_decrypt_key +.globl func(sm4_set_decrypt_key) .align 4 -_sm4_set_decrypt_key: + +func(sm4_set_decrypt_key): // load const v16..v31 = SBox adr x3,LSBOX @@ -221,10 +224,10 @@ _sm4_set_decrypt_key: ret -.globl _sm4_encrypt +.globl func(sm4_encrypt) .align 5 -_sm4_encrypt: +func(sm4_encrypt): // load sbox adr x3, LSBOX