diff --git a/0003-LoongArch-fix-up-sanitizer.patch b/0003-LoongArch-fix-up-sanitizer.patch new file mode 100644 index 0000000000000000000000000000000000000000..45af2fd6b3008b58718d46e40f04198c9f751083 --- /dev/null +++ b/0003-LoongArch-fix-up-sanitizer.patch @@ -0,0 +1,294 @@ +diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc +index ad129be2f..a25b0b720 100644 +--- a/gcc/config/loongarch/loongarch.cc ++++ b/gcc/config/loongarch/loongarch.cc +@@ -6640,7 +6640,7 @@ loongarch_asan_shadow_offset (void) + { + /* We only have libsanitizer support for LOONGARCH64 at present. + This value is taken from the file libsanitizer/asan/asan_mapping.h. */ +- return TARGET_64BIT ? (HOST_WIDE_INT_1 << 46) : 0; ++ return TARGET_64BIT ? (HOST_WIDE_INT_1 << 37) : 0; + } + + static sbitmap +diff --git a/libsanitizer/asan/asan_mapping.h b/libsanitizer/asan/asan_mapping.h +index 4fcb82776..c851b749a 100644 +--- a/libsanitizer/asan/asan_mapping.h ++++ b/libsanitizer/asan/asan_mapping.h +@@ -72,12 +72,6 @@ + // || `[0x2000000000, 0x23ffffffff]` || LowShadow || + // || `[0x0000000000, 0x1fffffffff]` || LowMem || + // +-// Default Linux/LoongArch64 (47-bit VMA) mapping: +-// || `[0x500000000000, 0x7fffffffffff]` || HighMem || +-// || `[0x4a0000000000, 0x4fffffffffff]` || HighShadow || +-// || `[0x480000000000, 0x49ffffffffff]` || ShadowGap || +-// || `[0x400000000000, 0x47ffffffffff]` || LowShadow || +-// || `[0x000000000000, 0x3fffffffffff]` || LowMem || + // Default Linux/RISCV64 Sv39 mapping: + // || `[0x1555550000, 0x3fffffffff]` || HighMem || + // || `[0x0fffffa000, 0x1555555fff]` || HighShadow || +@@ -171,7 +165,7 @@ static const u64 kAArch64_ShadowOffset64 = 1ULL << 36; + static const u64 kRiscv64_ShadowOffset64 = 0xd55550000; + static const u64 kMIPS32_ShadowOffset32 = 0x0aaa0000; + static const u64 kMIPS64_ShadowOffset64 = 1ULL << 37; +-static const u64 kLoongArch64_ShadowOffset64 = 1ULL << 46; ++static const u64 kLoongArch64_ShadowOffset64 = 1ULL << 37; + static const u64 kPPC64_ShadowOffset64 = 1ULL << 41; + static const u64 kSystemZ_ShadowOffset64 = 1ULL << 52; + static const u64 kSPARC64_ShadowOffset64 = 1ULL << 43; // 0x80000000000 +@@ -222,7 +216,7 @@ static const u64 kWindowsShadowOffset32 = 3ULL << 28; // 0x30000000 + # define SHADOW_OFFSET kDefaultShadowOffset64 + # elif defined(__mips64) + # define SHADOW_OFFSET kMIPS64_ShadowOffset64 +-# elif SANITIZER_LOONGARCH64 ++# elif defined(__loongarch64) + # define SHADOW_OFFSET kLoongArch64_ShadowOffset64 + #elif defined(__sparc__) + #define SHADOW_OFFSET kSPARC64_ShadowOffset64 +diff --git a/libsanitizer/configure.tgt b/libsanitizer/configure.tgt +index 87d8a2c38..03a533d5c 100644 +--- a/libsanitizer/configure.tgt ++++ b/libsanitizer/configure.tgt +@@ -73,6 +73,11 @@ case "${target}" in + riscv64-*-linux*) + ;; + loongarch64-*-linux*) ++ if test x$ac_cv_sizeof_void_p = x8; then ++ TSAN_SUPPORTED=yes ++ LSAN_SUPPORTED=yes ++ TSAN_TARGET_DEPENDENT_OBJECTS=tsan_rtl_loongarch64.lo ++ fi + ;; + *) + UNSUPPORTED=1 +diff --git a/libsanitizer/lsan/lsan_allocator.cpp b/libsanitizer/lsan/lsan_allocator.cpp +index 91e34ebb3..8b0b34e7b 100644 +--- a/libsanitizer/lsan/lsan_allocator.cpp ++++ b/libsanitizer/lsan/lsan_allocator.cpp +@@ -28,7 +28,7 @@ extern "C" void *memset(void *ptr, int value, uptr num); + namespace __lsan { + #if defined(__i386__) || defined(__arm__) + static const uptr kMaxAllowedMallocSize = 1UL << 30; +-#elif defined(__mips64) || defined(__aarch64__) ++#elif defined(__mips64) || defined(__aarch64__) || defined(__loongarch64) + static const uptr kMaxAllowedMallocSize = 4UL << 30; + #else + static const uptr kMaxAllowedMallocSize = 8UL << 30; +diff --git a/libsanitizer/lsan/lsan_allocator.h b/libsanitizer/lsan/lsan_allocator.h +index 45c6ac406..1ce09e241 100644 +--- a/libsanitizer/lsan/lsan_allocator.h ++++ b/libsanitizer/lsan/lsan_allocator.h +@@ -50,7 +50,8 @@ struct ChunkMetadata { + }; + + #if defined(__mips64) || defined(__aarch64__) || defined(__i386__) || \ +- defined(__arm__) || SANITIZER_RISCV64 || defined(__hexagon__) ++ defined(__arm__) || SANITIZER_RISCV64 || defined(__hexagon__) || \ ++ defined(__loongarch64) + template + struct AP32 { + static const uptr kSpaceBeg = 0; +diff --git a/libsanitizer/lsan/lsan_common.cpp b/libsanitizer/lsan/lsan_common.cpp +index 6bd5b933e..c38755a94 100644 +--- a/libsanitizer/lsan/lsan_common.cpp ++++ b/libsanitizer/lsan/lsan_common.cpp +@@ -167,9 +167,8 @@ static inline bool CanBeAHeapPointer(uptr p) { + unsigned runtimeVMA = + (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1); + return ((p >> runtimeVMA) == 0); +-# elif defined(__loongarch_lp64) +- // Allow 47-bit user-space VMA at current. +- return ((p >> 47) == 0); ++# elif defined(__loongarch64) ++ return ((p >> 40) == 0); + #else + return true; + #endif +diff --git a/libsanitizer/sanitizer_common/sanitizer_linux.cpp b/libsanitizer/sanitizer_common/sanitizer_linux.cpp +index 5f745fc26..7acbb0220 100644 +--- a/libsanitizer/sanitizer_common/sanitizer_linux.cpp ++++ b/libsanitizer/sanitizer_common/sanitizer_linux.cpp +@@ -750,7 +750,7 @@ struct linux_dirent { + unsigned long d_off; + #endif + unsigned short d_reclen; +-#if defined(__aarch64__) || SANITIZER_RISCV64 ++#if defined(__aarch64__) || SANITIZER_RISCV64 || SANITIZER_LOONGARCH64 + unsigned char d_type; + #endif + char d_name[256]; +diff --git a/libsanitizer/sanitizer_common/sanitizer_platform.h b/libsanitizer/sanitizer_common/sanitizer_platform.h +index daf8b4ab0..27ae55e96 100644 +--- a/libsanitizer/sanitizer_common/sanitizer_platform.h ++++ b/libsanitizer/sanitizer_common/sanitizer_platform.h +@@ -233,7 +233,7 @@ + #ifndef SANITIZER_CAN_USE_ALLOCATOR64 + # if (SANITIZER_ANDROID && defined(__aarch64__)) || SANITIZER_FUCHSIA + # define SANITIZER_CAN_USE_ALLOCATOR64 1 +-# elif defined(__mips64) || defined(__aarch64__) ++# elif defined(__mips64) || defined(__aarch64__) || defined(__loongarch64) + # define SANITIZER_CAN_USE_ALLOCATOR64 0 + # else + # define SANITIZER_CAN_USE_ALLOCATOR64 (SANITIZER_WORDSIZE == 64) +@@ -243,7 +243,7 @@ + // The range of addresses which can be returned my mmap. + // FIXME: this value should be different on different platforms. Larger values + // will still work but will consume more memory for TwoLevelByteMap. +-#if defined(__mips__) ++#if defined(__mips__) || defined(__loongarch64) + #if SANITIZER_GO && defined(__mips64) + #define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47) + #else +diff --git a/libsanitizer/tsan/tsan_platform.h b/libsanitizer/tsan/tsan_platform.h +index c3fbda98f..09cb52966 100644 +--- a/libsanitizer/tsan/tsan_platform.h ++++ b/libsanitizer/tsan/tsan_platform.h +@@ -192,36 +192,41 @@ struct MappingAarch64_39 { + static const uptr kVdsoBeg = 0x7f00000000ull; + }; + +-/* C/C++ on linux/loongarch64 (47-bit VMA) +-0000 0000 4000 - 0080 0000 0000: main binary +-0080 0000 0000 - 0100 0000 0000: - +-0100 0000 0000 - 1000 0000 0000: shadow memory +-1000 0000 0000 - 3000 0000 0000: - +-3000 0000 0000 - 3400 0000 0000: metainfo +-3400 0000 0000 - 5555 0000 0000: - +-5555 0000 0000 - 5556 0000 0000: main binary (PIE) +-5556 0000 0000 - 7ffe 0000 0000: - +-7ffe 0000 0000 - 7fff 0000 0000: heap +-7fff 0000 0000 - 7fff 8000 0000: - +-7fff 8000 0000 - 8000 0000 0000: modules and main thread stack ++/* ++C/C++ on linux/loongarch64 (40-bit VMA) ++0000 0000 00 - 0100 0000 00: - (4 GB) ++0100 0000 00 - 0200 0000 00: main binary (4 GB) ++0200 0000 00 - 2000 0000 00: - (120 GB) ++2000 0000 00 - 4000 0000 00: shadow (128 GB) ++4000 0000 00 - 5000 0000 00: metainfo (memory blocks and sync objects) (64 GB) ++5000 0000 00 - aa00 0000 00: - (360 GB) ++aa00 0000 00 - ab00 0000 00: main binary (PIE) (4 GB) ++ab00 0000 00 - b000 0000 00: - (20 GB) ++b000 0000 00 - b200 0000 00: traces (8 GB) ++b200 0000 00 - fe00 0000 00: - (304 GB) ++fe00 0000 00 - ff00 0000 00: heap (4 GB) ++ff00 0000 00 - ff80 0000 00: - (2 GB) ++ff80 0000 00 - ffff ffff ff: modules and main thread stack (<2 GB) + */ +-struct MappingLoongArch64_47 { +- static const uptr kMetaShadowBeg = 0x300000000000ull; +- static const uptr kMetaShadowEnd = 0x340000000000ull; +- static const uptr kShadowBeg = 0x010000000000ull; +- static const uptr kShadowEnd = 0x100000000000ull; +- static const uptr kHeapMemBeg = 0x7ffe00000000ull; +- static const uptr kHeapMemEnd = 0x7fff00000000ull; +- static const uptr kLoAppMemBeg = 0x000000004000ull; +- static const uptr kLoAppMemEnd = 0x008000000000ull; +- static const uptr kMidAppMemBeg = 0x555500000000ull; +- static const uptr kMidAppMemEnd = 0x555600000000ull; +- static const uptr kHiAppMemBeg = 0x7fff80000000ull; +- static const uptr kHiAppMemEnd = 0x800000000000ull; +- static const uptr kShadowMsk = 0x780000000000ull; +- static const uptr kShadowXor = 0x040000000000ull; +- static const uptr kShadowAdd = 0x000000000000ull; +- static const uptr kVdsoBeg = 0x7fffffffc000ull; ++struct MappingLoongArch64_40 { ++ static const uptr kMetaShadowBeg = 0x4000000000ull; ++ static const uptr kMetaShadowEnd = 0x5000000000ull; ++ static const uptr kTraceMemBeg = 0xb000000000ull; ++ static const uptr kTraceMemEnd = 0xb200000000ull; ++ static const uptr kShadowBeg = 0x2000000000ull; ++ static const uptr kShadowEnd = 0x4000000000ull; ++ static const uptr kHeapMemBeg = 0xfe00000000ull; ++ static const uptr kHeapMemEnd = 0xff00000000ull; ++ static const uptr kLoAppMemBeg = 0x0100000000ull; ++ static const uptr kLoAppMemEnd = 0x0200000000ull; ++ static const uptr kMidAppMemBeg = 0xaa00000000ull; ++ static const uptr kMidAppMemEnd = 0xab00000000ull; ++ static const uptr kHiAppMemBeg = 0xff80000000ull; ++ static const uptr kHiAppMemEnd = 0xffffffffffull; ++ static const uptr kShadowMsk = 0xf800000000ull; ++ static const uptr kShadowXor = 0x0800000000ull; ++ static const uptr kShadowAdd = 0x0000000000ull; ++ static const uptr kVdsoBeg = 0xfffff00000ull; + }; + + /* +@@ -703,7 +708,7 @@ ALWAYS_INLINE auto SelectMapping(Arg arg) { + return Func::template Apply(arg); + } + # elif SANITIZER_LOONGARCH64 +- return Func::template Apply(arg); ++ return Func::template Apply(arg); + # elif defined(__mips64) + return Func::template Apply(arg); + # elif defined(__s390x__) +@@ -723,7 +728,7 @@ void ForEachMapping() { + Func::template Apply(); + Func::template Apply(); + Func::template Apply(); +- Func::template Apply(); ++ Func::template Apply(); + Func::template Apply(); + Func::template Apply(); + Func::template Apply(); +diff --git a/libsanitizer/tsan/tsan_platform_linux.cpp b/libsanitizer/tsan/tsan_platform_linux.cpp +index 3a51d3c36..cafb5f047 100644 +--- a/libsanitizer/tsan/tsan_platform_linux.cpp ++++ b/libsanitizer/tsan/tsan_platform_linux.cpp +@@ -245,9 +245,9 @@ void InitializePlatformEarly() { + #endif + #elif SANITIZER_LOONGARCH64 + # if !SANITIZER_GO +- if (vmaSize != 47) { ++ if (vmaSize != 40) { + Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); +- Printf("FATAL: Found %zd - Supported 47\n", vmaSize); ++ Printf("FATAL: Found %zd - Supported 40\n", vmaSize); + Die(); + } + # endif +@@ -311,7 +311,7 @@ void InitializePlatform() { + SetAddressSpaceUnlimited(); + reexec = true; + } +-#if SANITIZER_LINUX && (defined(__aarch64__) || defined(__loongarch_lp64)) ++#if SANITIZER_LINUX && (defined(__aarch64__) || defined(__loongarch64)) + // After patch "arm64: mm: support ARCH_MMAP_RND_BITS." is introduced in + // linux kernel, the random gap between stack and mapped area is increased + // from 128M to 36G on 39-bit aarch64. As it is almost impossible to cover +@@ -396,8 +396,9 @@ static uptr UnmangleLongJmpSp(uptr mangled_sp) { + # else + return mangled_sp; + # endif +-#elif defined(__loongarch_lp64) +- return mangled_sp ^ longjmp_xor_key; ++#elif defined(__loongarch64) ++ //return mangled_sp ^ longjmp_xor_key; ++ return mangled_sp; + #elif defined(__powerpc64__) + // Reverse of: + // ld r4, -28696(r13) +diff --git a/libsanitizer/tsan/tsan_rtl.cpp b/libsanitizer/tsan/tsan_rtl.cpp +index 46dec04b8..22586175f 100644 +--- a/libsanitizer/tsan/tsan_rtl.cpp ++++ b/libsanitizer/tsan/tsan_rtl.cpp +@@ -252,7 +252,7 @@ static void StartBackgroundThread() { + ctx->background_thread = internal_start_thread(&BackgroundThread, 0); + } + +-#ifndef __mips__ ++# if !(defined(__mips__) || defined(__loongarch__)) + static void StopBackgroundThread() { + atomic_store(&ctx->stop_background_thread, 1, memory_order_relaxed); + internal_join_thread(ctx->background_thread); +@@ -434,7 +434,7 @@ void MaybeSpawnBackgroundThread() { + // On MIPS, TSan initialization is run before + // __pthread_initialize_minimal_internal() is finished, so we can not spawn + // new threads. +-#if !SANITIZER_GO && !defined(__mips__) ++#if !SANITIZER_GO && !(defined(__mips__) || defined(__loongarch__)) + static atomic_uint32_t bg_thread = {}; + if (atomic_load(&bg_thread, memory_order_relaxed) == 0 && + atomic_exchange(&bg_thread, 1, memory_order_relaxed) == 0) { diff --git a/0004-LoongArch-add-missed-headers.patch b/0004-LoongArch-add-missed-headers.patch new file mode 100644 index 0000000000000000000000000000000000000000..33570ea7917194ef0319db1cf40740b37bc76ac9 --- /dev/null +++ b/0004-LoongArch-add-missed-headers.patch @@ -0,0 +1,40 @@ +diff --git a/gcc/config/loongarch/genopts/loongarch.opt.in b/gcc/config/loongarch/genopts/loongarch.opt.in +index a56a867ff..8f04c9514 100644 +--- a/gcc/config/loongarch/genopts/loongarch.opt.in ++++ b/gcc/config/loongarch/genopts/loongarch.opt.in +@@ -30,6 +30,15 @@ config/loongarch/loongarch-opts.h + HeaderInclude + config/loongarch/loongarch-str.h + ++HeaderInclude ++config/loongarch/loongarch-driver.h ++ ++HeaderInclude ++config/loongarch/loongarch-tune.h ++ ++HeaderInclude ++config/loongarch/loongarch-def.h ++ + Variable + HOST_WIDE_INT la_opt_switches = 0 + +diff --git a/gcc/config/loongarch/loongarch.opt b/gcc/config/loongarch/loongarch.opt +index e917c789e..4a2a1ae1e 100644 +--- a/gcc/config/loongarch/loongarch.opt ++++ b/gcc/config/loongarch/loongarch.opt +@@ -37,6 +37,15 @@ config/loongarch/loongarch-opts.h + HeaderInclude + config/loongarch/loongarch-str.h + ++HeaderInclude ++config/loongarch/loongarch-driver.h ++ ++HeaderInclude ++config/loongarch/loongarch-tune.h ++ ++HeaderInclude ++config/loongarch/loongarch-def.h ++ + Variable + HOST_WIDE_INT la_opt_switches = 0 + diff --git a/gcc.spec b/gcc.spec index 68864665959852116ff772bc33129df7af67956b..72f9d15133105d96288ae36270334fe37ccc1eea 100644 --- a/gcc.spec +++ b/gcc.spec @@ -1,4 +1,4 @@ -%define anolis_release 3 +%define anolis_release 4 %global DATE 20221121 %global gitrev b3f5a0d53b84ed27cf00cfa2b9c3e2c78935c07d @@ -39,6 +39,9 @@ %global build_annobin_plugin 1 %global _gnu %{nil} %global gcc_target_platform %{_target_platform} +%ifarch loongarch64 +%global _lib lib +%endif %undefine _hardened_build %undefine _auto_set_build_flags @@ -119,6 +122,8 @@ Patch103: gcc12-fortran-fdec-non-logical-if.patch %ifarch loongarch64 Patch104: 0001-LoongArch-backports-from-upstream.patch Patch105: 0002-libjccjit-do-not-link-objects-contained-same-element.patch +Patch106: 0003-LoongArch-fix-up-sanitizer.patch +Patch107: 0004-LoongArch-add-missed-headers.patch %endif %if %{build_go} @@ -636,6 +641,8 @@ The %{name}-doc package contains documentation files for %{name}. %ifarch loongarch64 %patch104 -p1 %patch105 -p1 +%patch106 -p1 +%patch107 -p1 %endif rm -f libphobos/testsuite/libphobos.gc/forkgc2.d @@ -1100,6 +1107,18 @@ if [ "%{_lib}" = "lib" ]; then %if %{build_libubsan} ln -sf ../../../libubsan.so.1.* libubsan.so %endif +%ifarch loongarch64 +%if %{build_libtsan} + rm -f libtsan.so + echo 'INPUT ( %{_prefix}/%{_lib}/'`echo ../../../../%{_lib}/libtsan.so.2.* | sed 's,^.*libt,libt,'`' )' > libtsan.so + mv ../../../../%{_lib}/libtsan_preinit.o libtsan_preinit.o +%endif +%if %{build_liblsan} + rm -f liblsan.so + echo 'INPUT ( %{_prefix}/%{_lib}/'`echo ../../../../%{_lib}/liblsan.so.0.* | sed 's,^.*libl,libl,'`' )' > liblsan.so + mv ../../../../%{_lib}/liblsan_preinit.o liblsan_preinit.o +%endif +%endif else %if %{build_objc} ln -sf ../../../../%{_lib}/libobjc.so.4 libobjc.so @@ -1594,6 +1613,15 @@ end %{_prefix}/lib/gcc/%{gcc_target_platform}/%{gcc_major}/include/arm_bf16.h %{_prefix}/lib/gcc/%{gcc_target_platform}/%{gcc_major}/include/arm_sve.h %endif +%ifarch loongarch64 +%{_prefix}/lib/gcc/%{gcc_target_platform}/%{gcc_major}/include/larchintrin.h +%{_prefix}/lib/gcc/%{gcc_target_platform}/%{gcc_major}/plugin/include/config/loongarch/loongarch-protos.h +%{_prefix}/lib/gcc/%{gcc_target_platform}/%{gcc_major}/plugin/include/config/loongarch/loongarch-opts.h +%{_prefix}/lib/gcc/%{gcc_target_platform}/%{gcc_major}/plugin/include/config/loongarch/loongarch-str.h +%{_prefix}/lib/gcc/%{gcc_target_platform}/%{gcc_major}/plugin/include/config/loongarch/loongarch-def.h +%{_prefix}/lib/gcc/%{gcc_target_platform}/%{gcc_major}/plugin/include/config/loongarch/loongarch-tune.h +%{_prefix}/lib/gcc/%{gcc_target_platform}/%{gcc_major}/plugin/include/config/loongarch/loongarch-driver.h +%endif %if %{build_libasan} %{_prefix}/lib/gcc/%{gcc_target_platform}/%{gcc_major}/include/sanitizer %endif @@ -2068,6 +2096,9 @@ end %changelog +* Tue Aug 8 2023 Peng Fan - 12.2.1-4 +- LoongArch: fix up for sanitizer and add missed headers + * Tue Aug 8 2023 Peng Fan - 12.2.1-3 - LoongArch: add new symbol type and backports. - Last upstream commit: 5430c86e71927492399129f3df80824c6c334ddf