diff --git a/CMakeLists.txt b/CMakeLists.txt index a851788034e..06adb5faa3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -144,16 +144,12 @@ if (USE_STATIC_LIBRARIES) list(REVERSE CMAKE_FIND_LIBRARY_SUFFIXES) endif () -if (CMAKE_LIBRARY_ARCHITECTURE MATCHES "amd64.*|x86_64.*|AMD64.*") +if (ARCH_AMD64) option (USE_INTERNAL_MEMCPY "Use internal implementation of 'memcpy' function instead of provided by libc. Only for x86_64." ON) - - if (OS_LINUX) - option (GLIBC_COMPATIBILITY "Set to TRUE to enable compatibility with older glibc libraries. Only for x86_64, Linux. Implies USE_INTERNAL_MEMCPY." ON) - endif() endif () -if (GLIBC_COMPATIBILITY) - set (USE_INTERNAL_MEMCPY ON) +if ((ARCH_AMD64 OR ARCH_AARCH64) AND OS_LINUX) + option (GLIBC_COMPATIBILITY "Set to TRUE to enable compatibility with older glibc libraries. Only for x86_64, Linux. Implies USE_INTERNAL_MEMCPY." ON) endif () option (PIPE "-pipe compiler option [less /tmp usage, more ram usage]" ON) diff --git a/libs/libglibc-compatibility/CMakeLists.txt b/libs/libglibc-compatibility/CMakeLists.txt index ff139425f70..c5360934e5f 100644 --- a/libs/libglibc-compatibility/CMakeLists.txt +++ b/libs/libglibc-compatibility/CMakeLists.txt @@ -14,20 +14,34 @@ enable_language (ASM) +if (ARCH_AARCH64) + list (APPEND glibc_compatibility_sources musl/aarch64/syscall.s musl/aarch64/longjmp.s) +elseif (ARCH_AMD64) + list (APPEND glibc_compatibility_sources musl/x86_64/syscall.s musl/x86_64/longjmp.s) +else () + message (FATAL_ERROR "glibc_compatibility can only be used on x86_64 or aarch64.") +endif () + add_library (glibc-compatibility glibc-compatibility.c musl/pipe2.c musl/fallocate.c -musl/longjmp.s musl/vasprintf.c musl/lgamma.c musl/posix_spawn.c musl/futimens.c -musl/syscall.s musl/syscall_ret.c -musl/sched_cpucount.c) +musl/sched_cpucount.c +${glibc_compatibility_sources}) if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - set_source_files_properties(musl/longjmp.s musl/syscall.s - PROPERTIES COMPILE_FLAGS -Wno-unused-command-line-argument) + if (ARCH_AARCH64) + set_source_files_properties(musl/aarch64/longjmp.s musl/aarch64/syscall.s + PROPERTIES COMPILE_FLAGS -Wno-unused-command-line-argument) + elseif (ARCH_AMD64) + set_source_files_properties(musl/x86_64/longjmp.s musl/x86_64/syscall.s + PROPERTIES COMPILE_FLAGS -Wno-unused-command-line-argument) + else () + message(FATAL_ERROR "glibc_compatibility can only be used on x86_64 or aarch64.") + endif () endif() add_subdirectory (tests EXCLUDE_FROM_ALL) diff --git a/libs/libglibc-compatibility/glibc-compatibility.c b/libs/libglibc-compatibility/glibc-compatibility.c index d15939e6bc0..869cc1c565e 100644 --- a/libs/libglibc-compatibility/glibc-compatibility.c +++ b/libs/libglibc-compatibility/glibc-compatibility.c @@ -24,9 +24,13 @@ extern "C" { #include +// We can not remove this function before manually designating the signal stack. +// However, the returned value could be set to 16KiB, so that we can save many VSZ. +// Morever, though we can use SIGSTKSZ to define the size of signal stack, +// it's too small to collect enough information. size_t __pthread_get_minstack(const pthread_attr_t * attr) { - return 1048576; /// This is a guess. Don't sure it is correct. + return 16384; } #include diff --git a/libs/libglibc-compatibility/musl/aarch64/longjmp.s b/libs/libglibc-compatibility/musl/aarch64/longjmp.s new file mode 100644 index 00000000000..f34170f0156 --- /dev/null +++ b/libs/libglibc-compatibility/musl/aarch64/longjmp.s @@ -0,0 +1,35 @@ +// Copyright 2022 PingCAP, Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +.global musl_glibc_longjmp +.type musl_glibc_longjmp,@function +musl_glibc_longjmp: + // IHI0055B_aapcs64.pdf 5.1.1, 5.1.2 callee saved registers + ldp x19, x20, [x0,#0] + ldp x21, x22, [x0,#16] + ldp x23, x24, [x0,#32] + ldp x25, x26, [x0,#48] + ldp x27, x28, [x0,#64] + ldp x29, x30, [x0,#80] + ldr x2, [x0,#104] + mov sp, x2 + ldp d8 , d9, [x0,#112] + ldp d10, d11, [x0,#128] + ldp d12, d13, [x0,#144] + ldp d14, d15, [x0,#160] + + mov x0, x1 + cbnz x1, 1f + mov x0, #1 +1: br x30 diff --git a/libs/libglibc-compatibility/musl/aarch64/syscall.s b/libs/libglibc-compatibility/musl/aarch64/syscall.s new file mode 100644 index 00000000000..184676724b6 --- /dev/null +++ b/libs/libglibc-compatibility/musl/aarch64/syscall.s @@ -0,0 +1,28 @@ +// Copyright 2022 PingCAP, Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +.global __syscall +.hidden __syscall +.type __syscall,%function +__syscall: + uxtw x8,w0 + mov x0,x1 + mov x1,x2 + mov x2,x3 + mov x3,x4 + mov x4,x5 + mov x5,x6 + mov x6,x7 + svc 0 + ret diff --git a/libs/libglibc-compatibility/musl/longjmp.s b/libs/libglibc-compatibility/musl/x86_64/longjmp.s similarity index 100% rename from libs/libglibc-compatibility/musl/longjmp.s rename to libs/libglibc-compatibility/musl/x86_64/longjmp.s diff --git a/libs/libglibc-compatibility/musl/syscall.s b/libs/libglibc-compatibility/musl/x86_64/syscall.s similarity index 100% rename from libs/libglibc-compatibility/musl/syscall.s rename to libs/libglibc-compatibility/musl/x86_64/syscall.s