1  /* Pointer obfuscation implenentation.  x86-64 version.
       2     Copyright (C) 2005-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  #include <x86-lp_size.h>
      23  #include <tcb-offsets.h>
      24  
      25  #if IS_IN (rtld)
      26  /* We cannot use the thread descriptor because in ld.so we use setjmp
      27     earlier than the descriptor is initialized.  */
      28  # ifdef __ASSEMBLER__
      29  #  define PTR_MANGLE(reg)       xor __pointer_chk_guard_local(%rip), reg;    \
      30                                  rol $2*LP_SIZE+1, reg
      31  #  define PTR_DEMANGLE(reg)     ror $2*LP_SIZE+1, reg;                       \
      32                                  xor __pointer_chk_guard_local(%rip), reg
      33  # else
      34  #  define PTR_MANGLE(reg)       asm ("xor __pointer_chk_guard_local(%%rip), %0\n" \
      35                                       "rol $2*" LP_SIZE "+1, %0"                   \
      36                                       : "=r" (reg) : "0" (reg))
      37  #  define PTR_DEMANGLE(reg)     asm ("ror $2*" LP_SIZE "+1, %0\n"                 \
      38                                       "xor __pointer_chk_guard_local(%%rip), %0"   \
      39                                       : "=r" (reg) : "0" (reg))
      40  # endif
      41  #else
      42  # ifdef __ASSEMBLER__
      43  #  define PTR_MANGLE(reg)       xor %fs:POINTER_GUARD, reg;                   \
      44                                  rol $2*LP_SIZE+1, reg
      45  #  define PTR_DEMANGLE(reg)     ror $2*LP_SIZE+1, reg;                        \
      46                                  xor %fs:POINTER_GUARD, reg
      47  # else
      48  #  define PTR_MANGLE(var)       asm ("xor %%fs:%c2, %0\n"                     \
      49                                       "rol $2*" LP_SIZE "+1, %0"               \
      50                                       : "=r" (var)                             \
      51                                       : "0" (var),                             \
      52                                         "i" (POINTER_GUARD))
      53  #  define PTR_DEMANGLE(var)     asm ("ror $2*" LP_SIZE "+1, %0\n"             \
      54                                       "xor %%fs:%c2, %0"                       \
      55                                       : "=r" (var)                             \
      56                                       : "0" (var),                             \
      57                                         "i" (POINTER_GUARD))
      58  # endif
      59  #endif
      60  
      61  #endif /* POINTER_GUARD_H */