1  /* Pointer guard implementation.  AArch64 version.
       2     Copyright (C) 2014-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  /* Pointer mangling is supported for AArch64.  */
      23  #if (IS_IN (rtld) \
      24       || (!defined SHARED && (IS_IN (libc) \
      25                               || IS_IN (libpthread))))
      26  # ifdef __ASSEMBLER__
      27  /* Note, dst, src, guard, and tmp are all register numbers rather than
      28     register names so they will work with both ILP32 and LP64. */
      29  #  define PTR_MANGLE(dst, src, guard, tmp)                                \
      30    LDST_PCREL (ldr, guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard_local)); \
      31    PTR_MANGLE2 (dst, src, guard)
      32  /* Use PTR_MANGLE2 for efficiency if guard is already loaded.  */
      33  #  define PTR_MANGLE2(dst, src, guard)\
      34    eor x##dst, x##src, x##guard
      35  #  define PTR_DEMANGLE(dst, src, guard, tmp)\
      36    PTR_MANGLE (dst, src, guard, tmp)
      37  #  define PTR_DEMANGLE2(dst, src, guard)\
      38    PTR_MANGLE2 (dst, src, guard)
      39  # else
      40  extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
      41  #  define PTR_MANGLE(var) \
      42    (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard_local)
      43  #  define PTR_DEMANGLE(var)     PTR_MANGLE (var)
      44  # endif
      45  #else
      46  # ifdef __ASSEMBLER__
      47  /* Note, dst, src, guard, and tmp are all register numbers rather than
      48     register names so they will work with both ILP32 and LP64. */
      49  #  define PTR_MANGLE(dst, src, guard, tmp)                             \
      50    LDST_GLOBAL (ldr, guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard));   \
      51    PTR_MANGLE2 (dst, src, guard)
      52  /* Use PTR_MANGLE2 for efficiency if guard is already loaded.  */
      53  #  define PTR_MANGLE2(dst, src, guard)\
      54    eor x##dst, x##src, x##guard
      55  #  define PTR_DEMANGLE(dst, src, guard, tmp)\
      56    PTR_MANGLE (dst, src, guard, tmp)
      57  #  define PTR_DEMANGLE2(dst, src, guard)\
      58    PTR_MANGLE2 (dst, src, guard)
      59  # else
      60  #  include <stdint.h>
      61  extern uintptr_t __pointer_chk_guard attribute_relro;
      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 */