pointer_guard.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Pointer guard implementation. Arm version.
  2. Copyright (C) 2013-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef POINTER_GUARD_H
  16. #define POINTER_GUARD_H
  17. /* Pointer mangling support. */
  18. #if (IS_IN (rtld) \
  19. || (!defined SHARED && (IS_IN (libc) || IS_IN (libpthread))))
  20. # ifdef __ASSEMBLER__
  21. # define PTR_MANGLE_LOAD(guard, tmp) \
  22. LDR_HIDDEN (guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard_local), 0)
  23. # define PTR_MANGLE(dst, src, guard, tmp) \
  24. PTR_MANGLE_LOAD(guard, tmp); \
  25. PTR_MANGLE2(dst, src, guard)
  26. /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
  27. # define PTR_MANGLE2(dst, src, guard) \
  28. eor dst, src, guard
  29. # define PTR_DEMANGLE(dst, src, guard, tmp) \
  30. PTR_MANGLE (dst, src, guard, tmp)
  31. # define PTR_DEMANGLE2(dst, src, guard) \
  32. PTR_MANGLE2 (dst, src, guard)
  33. # else
  34. extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
  35. # define PTR_MANGLE(var) \
  36. (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard_local)
  37. # define PTR_DEMANGLE(var) PTR_MANGLE (var)
  38. # endif
  39. #else
  40. # ifdef __ASSEMBLER__
  41. # define PTR_MANGLE_LOAD(guard, tmp) \
  42. LDR_GLOBAL (guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard), 0);
  43. # define PTR_MANGLE(dst, src, guard, tmp) \
  44. PTR_MANGLE_LOAD(guard, tmp); \
  45. PTR_MANGLE2(dst, src, guard)
  46. /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
  47. # define PTR_MANGLE2(dst, src, guard) \
  48. eor dst, src, guard
  49. # define PTR_DEMANGLE(dst, src, guard, tmp) \
  50. PTR_MANGLE (dst, src, guard, tmp)
  51. # define PTR_DEMANGLE2(dst, src, guard) \
  52. PTR_MANGLE2 (dst, src, guard)
  53. # else
  54. # include <stdint.h>
  55. extern uintptr_t __pointer_chk_guard attribute_relro;
  56. # define PTR_MANGLE(var) \
  57. (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard)
  58. # define PTR_DEMANGLE(var) PTR_MANGLE (var)
  59. # endif
  60. #endif
  61. #endif /* POINTER_GUARD_H */