elf-linux-core.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* Definitions for PRPSINFO structures under ELF on GNU/Linux.
  2. Copyright (C) 2013-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #ifndef ELF_LINUX_CORE_H
  17. #define ELF_LINUX_CORE_H
  18. /* External 32-bit structure for PRPSINFO. This structure is
  19. ABI-defined, thus we choose to use char arrays here in order to
  20. avoid dealing with different types in different architectures.
  21. This is the variant for targets which use a 32-bit data type for
  22. UID and GID, as all modern Linux ports do. Some older ports use
  23. a 16-bit data type instead; see below for the alternative variant.
  24. This structure will ultimately be written in the corefile's note
  25. section, as the PRPSINFO. */
  26. struct elf_external_linux_prpsinfo32_ugid32
  27. {
  28. char pr_state; /* Numeric process state. */
  29. char pr_sname; /* Char for pr_state. */
  30. char pr_zomb; /* Zombie. */
  31. char pr_nice; /* Nice val. */
  32. char pr_flag[4]; /* Flags. */
  33. char pr_uid[4];
  34. char pr_gid[4];
  35. char pr_pid[4];
  36. char pr_ppid[4];
  37. char pr_pgrp[4];
  38. char pr_sid[4];
  39. char pr_fname[16] ATTRIBUTE_NONSTRING; /* Filename of executable. */
  40. char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list. */
  41. };
  42. /* Helper function to copy an elf_internal_linux_prpsinfo in host
  43. endian to an elf_external_linux_prpsinfo32_ugid32 in target endian. */
  44. static inline void
  45. swap_linux_prpsinfo32_ugid32_out
  46. (bfd *obfd,
  47. const struct elf_internal_linux_prpsinfo *from,
  48. struct elf_external_linux_prpsinfo32_ugid32 *to)
  49. {
  50. bfd_put_8 (obfd, from->pr_state, &to->pr_state);
  51. bfd_put_8 (obfd, from->pr_sname, &to->pr_sname);
  52. bfd_put_8 (obfd, from->pr_zomb, &to->pr_zomb);
  53. bfd_put_8 (obfd, from->pr_nice, &to->pr_nice);
  54. bfd_put_32 (obfd, from->pr_flag, to->pr_flag);
  55. bfd_put_32 (obfd, from->pr_uid, to->pr_uid);
  56. bfd_put_32 (obfd, from->pr_gid, to->pr_gid);
  57. bfd_put_32 (obfd, from->pr_pid, to->pr_pid);
  58. bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
  59. bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
  60. bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
  61. strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
  62. strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
  63. }
  64. /* External 32-bit structure for PRPSINFO. This structure is
  65. ABI-defined, thus we choose to use char arrays here in order to
  66. avoid dealing with different types in different architectures.
  67. This is the variant for targets which use a 16-bit data type for
  68. UID and GID, as some older Linux ports do. All modern ports use
  69. a 32-bit data type instead; see above for the alternative variant.
  70. This structure will ultimately be written in the corefile's note
  71. section, as the PRPSINFO. */
  72. struct elf_external_linux_prpsinfo32_ugid16
  73. {
  74. char pr_state; /* Numeric process state. */
  75. char pr_sname; /* Char for pr_state. */
  76. char pr_zomb; /* Zombie. */
  77. char pr_nice; /* Nice val. */
  78. char pr_flag[4]; /* Flags. */
  79. char pr_uid[2];
  80. char pr_gid[2];
  81. char pr_pid[4];
  82. char pr_ppid[4];
  83. char pr_pgrp[4];
  84. char pr_sid[4];
  85. char pr_fname[16] ATTRIBUTE_NONSTRING; /* Filename of executable. */
  86. char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list. */
  87. };
  88. /* Helper function to copy an elf_internal_linux_prpsinfo in host
  89. endian to an elf_external_linux_prpsinfo32_ugid16 in target endian. */
  90. static inline void
  91. swap_linux_prpsinfo32_ugid16_out
  92. (bfd *obfd,
  93. const struct elf_internal_linux_prpsinfo *from,
  94. struct elf_external_linux_prpsinfo32_ugid16 *to)
  95. {
  96. bfd_put_8 (obfd, from->pr_state, &to->pr_state);
  97. bfd_put_8 (obfd, from->pr_sname, &to->pr_sname);
  98. bfd_put_8 (obfd, from->pr_zomb, &to->pr_zomb);
  99. bfd_put_8 (obfd, from->pr_nice, &to->pr_nice);
  100. bfd_put_32 (obfd, from->pr_flag, to->pr_flag);
  101. bfd_put_16 (obfd, from->pr_uid, to->pr_uid);
  102. bfd_put_16 (obfd, from->pr_gid, to->pr_gid);
  103. bfd_put_32 (obfd, from->pr_pid, to->pr_pid);
  104. bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
  105. bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
  106. bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
  107. strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
  108. strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
  109. }
  110. /* External 64-bit structure for PRPSINFO. This structure is
  111. ABI-defined, thus we choose to use char arrays here in order to
  112. avoid dealing with different types in different architectures.
  113. This is the variant for targets which use a 32-bit data type for
  114. UID and GID, as most Linux ports do. The SH64 port uses a 16-bit
  115. data type instead; see below for the alternative variant.
  116. This structure will ultimately be written in the corefile's note
  117. section, as the PRPSINFO. */
  118. struct elf_external_linux_prpsinfo64_ugid32
  119. {
  120. char pr_state; /* Numeric process state. */
  121. char pr_sname; /* Char for pr_state. */
  122. char pr_zomb; /* Zombie. */
  123. char pr_nice; /* Nice val. */
  124. char gap[4];
  125. char pr_flag[8]; /* Flags. */
  126. char pr_uid[4];
  127. char pr_gid[4];
  128. char pr_pid[4];
  129. char pr_ppid[4];
  130. char pr_pgrp[4];
  131. char pr_sid[4];
  132. char pr_fname[16] ATTRIBUTE_NONSTRING; /* Filename of executable. */
  133. char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list. */
  134. };
  135. /* Helper function to copy an elf_internal_linux_prpsinfo in host
  136. endian to an elf_external_linux_prpsinfo64_ugid32 in target endian. */
  137. static inline void
  138. swap_linux_prpsinfo64_ugid32_out
  139. (bfd *obfd,
  140. const struct elf_internal_linux_prpsinfo *from,
  141. struct elf_external_linux_prpsinfo64_ugid32 *to)
  142. {
  143. bfd_put_8 (obfd, from->pr_state, &to->pr_state);
  144. bfd_put_8 (obfd, from->pr_sname, &to->pr_sname);
  145. bfd_put_8 (obfd, from->pr_zomb, &to->pr_zomb);
  146. bfd_put_8 (obfd, from->pr_nice, &to->pr_nice);
  147. bfd_put_64 (obfd, from->pr_flag, to->pr_flag);
  148. bfd_put_32 (obfd, from->pr_uid, to->pr_uid);
  149. bfd_put_32 (obfd, from->pr_gid, to->pr_gid);
  150. bfd_put_32 (obfd, from->pr_pid, to->pr_pid);
  151. bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
  152. bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
  153. bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
  154. strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
  155. strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
  156. }
  157. /* External 64-bit structure for PRPSINFO. This structure is
  158. ABI-defined, thus we choose to use char arrays here in order to
  159. avoid dealing with different types in different architectures.
  160. This is the variant for the SH64 port which uses a 16-bit data
  161. type for UID and GID. Most Linux ports use a 32-bit data type
  162. instead; see above for the alternative variant.
  163. This structure will ultimately be written in the corefile's note
  164. section, as the PRPSINFO. */
  165. struct elf_external_linux_prpsinfo64_ugid16
  166. {
  167. char pr_state; /* Numeric process state. */
  168. char pr_sname; /* Char for pr_state. */
  169. char pr_zomb; /* Zombie. */
  170. char pr_nice; /* Nice val. */
  171. char gap[4];
  172. char pr_flag[8]; /* Flags. */
  173. char pr_uid[2];
  174. char pr_gid[2];
  175. char pr_pid[4];
  176. char pr_ppid[4];
  177. char pr_pgrp[4];
  178. char pr_sid[4];
  179. char pr_fname[16] ATTRIBUTE_NONSTRING; /* Filename of executable. */
  180. char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list. */
  181. };
  182. /* Helper function to copy an elf_internal_linux_prpsinfo in host
  183. endian to an elf_external_linux_prpsinfo64_ugid16 in target endian. */
  184. static inline void
  185. swap_linux_prpsinfo64_ugid16_out
  186. (bfd *obfd,
  187. const struct elf_internal_linux_prpsinfo *from,
  188. struct elf_external_linux_prpsinfo64_ugid16 *to)
  189. {
  190. bfd_put_8 (obfd, from->pr_state, &to->pr_state);
  191. bfd_put_8 (obfd, from->pr_sname, &to->pr_sname);
  192. bfd_put_8 (obfd, from->pr_zomb, &to->pr_zomb);
  193. bfd_put_8 (obfd, from->pr_nice, &to->pr_nice);
  194. bfd_put_64 (obfd, from->pr_flag, to->pr_flag);
  195. bfd_put_16 (obfd, from->pr_uid, to->pr_uid);
  196. bfd_put_16 (obfd, from->pr_gid, to->pr_gid);
  197. bfd_put_32 (obfd, from->pr_pid, to->pr_pid);
  198. bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
  199. bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
  200. bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
  201. strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
  202. strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
  203. }
  204. #endif