stackinfo.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Details about the machine's stack: wrapper header.
  2. Copyright (C) 2014-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 _INCLUDE_STACKINFO_H
  16. #define _INCLUDE_STACKINFO_H 1
  17. /* A sysdeps/.../stackinfo.h file defines details for the CPU.
  18. It is obliged to define either _STACK_GROWS_DOWN or _STACK_GROWS_UP. */
  19. #include_next <stackinfo.h>
  20. #if defined _STACK_GROWS_DOWN && _STACK_GROWS_DOWN
  21. # ifdef _STACK_GROWS_UP
  22. # error "stackinfo.h should not define both!"
  23. # else
  24. # define _STACK_GROWS_UP 0
  25. # endif
  26. #elif defined _STACK_GROWS_UP && _STACK_GROWS_UP
  27. # ifdef _STACK_GROWS_DOWN
  28. # error "stackinfo.h should not define both!"
  29. # else
  30. # define _STACK_GROWS_DOWN 0
  31. # endif
  32. #else
  33. # error "stackinfo.h must define _STACK_GROWS_UP or _STACK_GROWS_DOWN!"
  34. #endif
  35. #include <sys/mman.h>
  36. #include <link.h>
  37. /* ELF uses the PF_x macros to specify the segment permissions, mmap
  38. uses PROT_xxx. In most cases the three macros have the values 1, 2,
  39. and 4 but not in a matching order. The following macros allows
  40. converting from the PF_x values to PROT_xxx values. */
  41. #define PF_TO_PROT \
  42. ((PROT_READ << (PF_R * 4)) \
  43. | (PROT_WRITE << (PF_W * 4)) \
  44. | (PROT_EXEC << (PF_X * 4)) \
  45. | ((PROT_READ | PROT_WRITE) << ((PF_R | PF_W) * 4)) \
  46. | ((PROT_READ | PROT_EXEC) << ((PF_R | PF_X) * 4)) \
  47. | ((PROT_WRITE | PROT_EXEC) << (PF_W | PF_X) * 4) \
  48. | ((PROT_READ | PROT_WRITE | PROT_EXEC) << ((PF_R | PF_W | PF_X) * 4)))
  49. static inline int
  50. pf_to_prot (ElfW(Word) value)
  51. {
  52. #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
  53. return (PF_TO_PROT >> ((value & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
  54. #else
  55. ElfW(Word) ret = 0;
  56. if (value & PF_R)
  57. ret |= PROT_READ;
  58. if (value & PF_W)
  59. ret |= PROT_WRITE;
  60. if (value & PF_X)
  61. ret |= PROT_EXEC;
  62. return ret;
  63. #endif
  64. }
  65. #endif /* include/stackinfo.h */