Implement C23 <stdbit.h>
[glibc.git] / sysdeps / unix / sysv / linux / loongarch / pointer_guard.h
blobb85c58d3c9e8553af8c9b28c23f7cb4ee966c44c
1 /* Pointer obfuscation implenentation. LoongArch version.
2 Copyright (C) 2022-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #ifndef POINTER_GUARD_H
20 #define POINTER_GUARD_H
22 /* Load a got-relative EXPR into register G. */
23 #define LD_GLOBAL(G, EXPR) \
24 la.global G, EXPR; \
25 REG_L G, G, 0;
27 /* Load a pc-relative EXPR into register G. */
28 #define LD_PCREL(G, EXPR) \
29 pcalau12i G, %pc_hi20(EXPR); \
30 REG_L G, G, %pc_lo12(EXPR);
32 #if (IS_IN (rtld) \
33 || (!defined SHARED && (IS_IN (libc) \
34 || IS_IN (libpthread))))
36 #ifdef __ASSEMBLER__
37 #define PTR_MANGLE(dst, src, guard) \
38 LD_PCREL (guard, __pointer_chk_guard_local); \
39 PTR_MANGLE2 (dst, src, guard);
40 #define PTR_DEMANGLE(dst, src, guard) \
41 LD_PCREL (guard, __pointer_chk_guard_local); \
42 PTR_DEMANGLE2 (dst, src, guard);
43 /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
44 #define PTR_MANGLE2(dst, src, guard) \
45 xor dst, src, guard;
46 #define PTR_DEMANGLE2(dst, src, guard) \
47 PTR_MANGLE2 (dst, src, guard);
48 #else
49 # include <stdint.h>
50 extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
51 #define PTR_MANGLE(var) \
52 (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard_local)
53 #define PTR_DEMANGLE(var) PTR_MANGLE (var)
54 #endif
56 #else
58 #ifdef __ASSEMBLER__
59 #define PTR_MANGLE(dst, src, guard) \
60 LD_GLOBAL (guard, __pointer_chk_guard); \
61 PTR_MANGLE2 (dst, src, guard);
62 #define PTR_DEMANGLE(dst, src, guard) \
63 LD_GLOBAL (guard, __pointer_chk_guard); \
64 PTR_DEMANGLE2 (dst, src, guard);
65 /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
66 #define PTR_MANGLE2(dst, src, guard) \
67 xor dst, src, guard;
68 #define PTR_DEMANGLE2(dst, src, guard) \
69 PTR_MANGLE2 (dst, src, guard);
70 #else
71 # include <stdint.h>
72 extern uintptr_t __pointer_chk_guard attribute_relro;
73 #define PTR_MANGLE(var) \
74 (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard)
75 #define PTR_DEMANGLE(var) PTR_MANGLE (var)
76 #endif
78 #endif
80 #endif /* POINTER_GUARD_H */