1  /* Pointer obfuscation implenentation.  LoongArch 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  /* Load a got-relative EXPR into G, using T.
      23     Note G and T are register names.  */
      24  #define LD_GLOBAL(G, EXPR) \
      25    la.global G,  EXPR; \
      26    REG_L     G,  G,  0;
      27  
      28  /* Load a pc-relative EXPR into G, using T.
      29     Note G and T are register names.  */
      30  #define LD_PCREL(G, EXPR) \
      31    la.pcrel  G,  EXPR; \
      32    REG_L     G,  G,  0;
      33  
      34  #if (IS_IN (rtld) \
      35       || (!defined SHARED && (IS_IN (libc) \
      36       || IS_IN (libpthread))))
      37  
      38  #ifdef __ASSEMBLER__
      39  #define PTR_MANGLE(dst, src, guard) \
      40    LD_PCREL (guard, __pointer_chk_guard_local); \
      41    PTR_MANGLE2 (dst, src, guard);
      42  #define PTR_DEMANGLE(dst, src, guard) \
      43    LD_PCREL (guard, __pointer_chk_guard_local); \
      44    PTR_DEMANGLE2 (dst, src, guard);
      45  /* Use PTR_MANGLE2 for efficiency if guard is already loaded.  */
      46  #define PTR_MANGLE2(dst, src, guard) \
      47    xor  dst, src, guard;
      48  #define PTR_DEMANGLE2(dst, src, guard) \
      49    PTR_MANGLE2 (dst, src, guard);
      50  #else
      51  # include <stdint.h>
      52  extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
      53  #define PTR_MANGLE(var) \
      54    (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard_local)
      55  #define PTR_DEMANGLE(var) PTR_MANGLE (var)
      56  #endif
      57  
      58  #else
      59  
      60  #ifdef __ASSEMBLER__
      61  #define PTR_MANGLE(dst, src, guard) \
      62    LD_GLOBAL (guard, __pointer_chk_guard); \
      63    PTR_MANGLE2 (dst, src, guard);
      64  #define PTR_DEMANGLE(dst, src, guard) \
      65    LD_GLOBAL (guard, __pointer_chk_guard); \
      66    PTR_DEMANGLE2 (dst, src, guard);
      67  /* Use PTR_MANGLE2 for efficiency if guard is already loaded.  */
      68  #define PTR_MANGLE2(dst, src, guard) \
      69    xor dst, src, guard;
      70  #define PTR_DEMANGLE2(dst, src, guard) \
      71    PTR_MANGLE2 (dst, src, guard);
      72  #else
      73  # include <stdint.h>
      74  extern uintptr_t __pointer_chk_guard attribute_relro;
      75  #define PTR_MANGLE(var) \
      76    (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard)
      77  #define PTR_DEMANGLE(var) PTR_MANGLE (var)
      78  #endif
      79  
      80  #endif
      81  
      82  #endif /* POINTER_GUARD_H */