1  /* Pointer obfuscation implenentation.  C-SKY version.
       2     Copyright (C) 2022-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       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.
       9  
      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.
      14  
      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/>.  */
      18  
      19  #ifndef POINTER_GUARD_H
      20  #define POINTER_GUARD_H
      21  
      22  #if (IS_IN (rtld) \
      23       || (!defined SHARED && (IS_IN (libc) || IS_IN (libpthread))))
      24  # ifdef __ASSEMBLER__
      25  #  define PTR_MANGLE(dst, src, guard)                   \
      26          grs     t0, 1f;                                 \
      27  1:                                                      \
      28          lrw     guard, 1b@GOTPC;                        \
      29          addu    t0, guard;                              \
      30          lrw     guard, __pointer_chk_guard_local@GOT;   \
      31          ldr.w   guard, (t0, guard << 0);                \
      32          ldw     guard, (guard, 0);                      \
      33          xor     dst, src, guard;
      34  #  define PTR_DEMANGLE(dst, src, guard) PTR_MANGLE (dst, src, guard)
      35  #  define PTR_MANGLE2(dst, src, guard) \
      36          xor     dst, src, guard
      37  #  define PTR_DEMANGLE2(dst, src, guard) PTR_MANGLE2 (dst, src, guard)
      38  # else
      39  extern uintptr_t __pointer_chk_guard_local;
      40  #  define PTR_MANGLE(var) \
      41    (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard_local)
      42  #  define PTR_DEMANGLE(var) PTR_MANGLE (var)
      43  # endif
      44  #else
      45  # ifdef __ASSEMBLER__
      46  #  define PTR_MANGLE(dst, src, guard)           \
      47          grs     t0, 1f;                         \
      48  1:                                              \
      49          lrw     guard, 1b@GOTPC;                \
      50          addu    t0, guard;                      \
      51          lrw     guard, __pointer_chk_guard@GOT; \
      52          ldr.w   guard, (t0, guard << 0);        \
      53          ldw     guard, (guard, 0);              \
      54          xor     dst, src, guard;
      55  #  define PTR_DEMANGLE(dst, src, guard) PTR_MANGLE (dst, src, guard)
      56  #  define PTR_MANGLE2(dst, src, guard) \
      57          xor     dst, src, guard
      58  #  define PTR_DEMANGLE2(dst, src, guard) PTR_MANGLE2 (dst, src, guard)
      59  # else
      60  # include <stdint.h>
      61  extern uintptr_t __pointer_chk_guard;
      62  #  define PTR_MANGLE(var) \
      63    (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard)
      64  #  define PTR_DEMANGLE(var) PTR_MANGLE (var)
      65  # endif
      66  #endif
      67  
      68  #endif /* POINTER_GUARD_H */