1193240Ssam/* Definitions for PRPSINFO structures under ELF on GNU/Linux.
2193240Ssam   Copyright (C) 2013-2022 Free Software Foundation, Inc.
3193240Ssam
4193240Ssam   This file is part of BFD, the Binary File Descriptor library.
5193240Ssam
6193240Ssam   This program is free software; you can redistribute it and/or modify
7193240Ssam   it under the terms of the GNU General Public License as published by
8193240Ssam   the Free Software Foundation; either version 3 of the License, or
9193240Ssam   (at your option) any later version.
10193240Ssam
11193240Ssam   This program is distributed in the hope that it will be useful,
12193240Ssam   but WITHOUT ANY WARRANTY; without even the implied warranty of
13193240Ssam   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14193240Ssam   GNU General Public License for more details.
15193240Ssam
16193240Ssam   You should have received a copy of the GNU General Public License
17193240Ssam   along with this program; if not, write to the Free Software
18193240Ssam   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19193240Ssam   MA 02110-1301, USA.  */
20193240Ssam
21193240Ssam#ifndef ELF_LINUX_CORE_H
22193240Ssam#define ELF_LINUX_CORE_H
23193240Ssam
24193240Ssam/* External 32-bit structure for PRPSINFO.  This structure is
25193240Ssam   ABI-defined, thus we choose to use char arrays here in order to
26193240Ssam   avoid dealing with different types in different architectures.
27193240Ssam
28193240Ssam   This is the variant for targets which use a 32-bit data type for
29193240Ssam   UID and GID, as all modern Linux ports do.  Some older ports use
30193240Ssam   a 16-bit data type instead; see below for the alternative variant.
31193240Ssam
32193240Ssam   This structure will ultimately be written in the corefile's note
33193240Ssam   section, as the PRPSINFO.  */
34193240Ssam
35193240Ssamstruct elf_external_linux_prpsinfo32_ugid32
36193240Ssam  {
37193240Ssam    char pr_state;			/* Numeric process state.  */
38193240Ssam    char pr_sname;			/* Char for pr_state.  */
39193240Ssam    char pr_zomb;			/* Zombie.  */
40193240Ssam    char pr_nice;			/* Nice val.  */
41193240Ssam    char pr_flag[4];			/* Flags.  */
42193240Ssam    char pr_uid[4];
43193240Ssam    char pr_gid[4];
44193240Ssam    char pr_pid[4];
45193240Ssam    char pr_ppid[4];
46193240Ssam    char pr_pgrp[4];
47193240Ssam    char pr_sid[4];
48193240Ssam    char pr_fname[16] ATTRIBUTE_NONSTRING;  /* Filename of executable.  */
49193240Ssam    char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list.  */
50193240Ssam  };
51193240Ssam
52193240Ssam/* Helper function to copy an elf_internal_linux_prpsinfo in host
53193240Ssam   endian to an elf_external_linux_prpsinfo32_ugid32 in target endian.  */
54193240Ssam
55193240Ssamstatic inline void
56193240Ssamswap_linux_prpsinfo32_ugid32_out
57193240Ssam  (bfd *obfd,
58193240Ssam   const struct elf_internal_linux_prpsinfo *from,
59193240Ssam   struct elf_external_linux_prpsinfo32_ugid32 *to)
60193240Ssam{
61193240Ssam  bfd_put_8 (obfd, from->pr_state, &to->pr_state);
62193240Ssam  bfd_put_8 (obfd, from->pr_sname, &to->pr_sname);
63193240Ssam  bfd_put_8 (obfd, from->pr_zomb, &to->pr_zomb);
64193240Ssam  bfd_put_8 (obfd, from->pr_nice, &to->pr_nice);
65193240Ssam  bfd_put_32 (obfd, from->pr_flag, to->pr_flag);
66193240Ssam  bfd_put_32 (obfd, from->pr_uid, to->pr_uid);
67193240Ssam  bfd_put_32 (obfd, from->pr_gid, to->pr_gid);
68193240Ssam  bfd_put_32 (obfd, from->pr_pid, to->pr_pid);
69193240Ssam  bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
70193240Ssam  bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
71193240Ssam  bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
72193240Ssam  strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
73193240Ssam  strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
74193240Ssam}
75193240Ssam
76193240Ssam/* External 32-bit structure for PRPSINFO.  This structure is
77193240Ssam   ABI-defined, thus we choose to use char arrays here in order to
78193240Ssam   avoid dealing with different types in different architectures.
79193240Ssam
80193240Ssam   This is the variant for targets which use a 16-bit data type for
81193240Ssam   UID and GID, as some older Linux ports do.  All modern ports use
82193240Ssam   a 32-bit data type instead; see above for the alternative variant.
83193240Ssam
84193240Ssam   This structure will ultimately be written in the corefile's note
85193240Ssam   section, as the PRPSINFO.  */
86193240Ssam
87193240Ssamstruct elf_external_linux_prpsinfo32_ugid16
88193240Ssam  {
89193240Ssam    char pr_state;			/* Numeric process state.  */
90193240Ssam    char pr_sname;			/* Char for pr_state.  */
91193240Ssam    char pr_zomb;			/* Zombie.  */
92193240Ssam    char pr_nice;			/* Nice val.  */
93193240Ssam    char pr_flag[4];			/* Flags.  */
94193240Ssam    char pr_uid[2];
95193240Ssam    char pr_gid[2];
96193240Ssam    char pr_pid[4];
97193240Ssam    char pr_ppid[4];
98193240Ssam    char pr_pgrp[4];
99193240Ssam    char pr_sid[4];
100193240Ssam    char pr_fname[16] ATTRIBUTE_NONSTRING;  /* Filename of executable.  */
101193240Ssam    char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list.  */
102193240Ssam  };
103193240Ssam
104193240Ssam/* Helper function to copy an elf_internal_linux_prpsinfo in host
105193240Ssam   endian to an elf_external_linux_prpsinfo32_ugid16 in target endian.  */
106193240Ssam
107193240Ssamstatic inline void
108193240Ssamswap_linux_prpsinfo32_ugid16_out
109  (bfd *obfd,
110   const struct elf_internal_linux_prpsinfo *from,
111   struct elf_external_linux_prpsinfo32_ugid16 *to)
112{
113  bfd_put_8 (obfd, from->pr_state, &to->pr_state);
114  bfd_put_8 (obfd, from->pr_sname, &to->pr_sname);
115  bfd_put_8 (obfd, from->pr_zomb, &to->pr_zomb);
116  bfd_put_8 (obfd, from->pr_nice, &to->pr_nice);
117  bfd_put_32 (obfd, from->pr_flag, to->pr_flag);
118  bfd_put_16 (obfd, from->pr_uid, to->pr_uid);
119  bfd_put_16 (obfd, from->pr_gid, to->pr_gid);
120  bfd_put_32 (obfd, from->pr_pid, to->pr_pid);
121  bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
122  bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
123  bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
124  strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
125  strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
126}
127
128/* External 64-bit structure for PRPSINFO.  This structure is
129   ABI-defined, thus we choose to use char arrays here in order to
130   avoid dealing with different types in different architectures.
131
132   This is the variant for targets which use a 32-bit data type for
133   UID and GID, as most Linux ports do.  The SH64 port uses a 16-bit
134   data type instead; see below for the alternative variant.
135
136   This structure will ultimately be written in the corefile's note
137   section, as the PRPSINFO.  */
138
139struct elf_external_linux_prpsinfo64_ugid32
140  {
141    char pr_state;			/* Numeric process state.  */
142    char pr_sname;			/* Char for pr_state.  */
143    char pr_zomb;			/* Zombie.  */
144    char pr_nice;			/* Nice val.  */
145    char gap[4];
146    char pr_flag[8];			/* Flags.  */
147    char pr_uid[4];
148    char pr_gid[4];
149    char pr_pid[4];
150    char pr_ppid[4];
151    char pr_pgrp[4];
152    char pr_sid[4];
153    char pr_fname[16] ATTRIBUTE_NONSTRING;  /* Filename of executable.  */
154    char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list.  */
155  };
156
157/* Helper function to copy an elf_internal_linux_prpsinfo in host
158   endian to an elf_external_linux_prpsinfo64_ugid32 in target endian.  */
159
160static inline void
161swap_linux_prpsinfo64_ugid32_out
162  (bfd *obfd,
163   const struct elf_internal_linux_prpsinfo *from,
164   struct elf_external_linux_prpsinfo64_ugid32 *to)
165{
166  bfd_put_8 (obfd, from->pr_state, &to->pr_state);
167  bfd_put_8 (obfd, from->pr_sname, &to->pr_sname);
168  bfd_put_8 (obfd, from->pr_zomb, &to->pr_zomb);
169  bfd_put_8 (obfd, from->pr_nice, &to->pr_nice);
170  bfd_put_64 (obfd, from->pr_flag, to->pr_flag);
171  bfd_put_32 (obfd, from->pr_uid, to->pr_uid);
172  bfd_put_32 (obfd, from->pr_gid, to->pr_gid);
173  bfd_put_32 (obfd, from->pr_pid, to->pr_pid);
174  bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
175  bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
176  bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
177  strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
178  strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
179}
180
181/* External 64-bit structure for PRPSINFO.  This structure is
182   ABI-defined, thus we choose to use char arrays here in order to
183   avoid dealing with different types in different architectures.
184
185   This is the variant for the SH64 port which uses a 16-bit data
186   type for UID and GID.  Most Linux ports use a 32-bit data type
187   instead; see above for the alternative variant.
188
189   This structure will ultimately be written in the corefile's note
190   section, as the PRPSINFO.  */
191
192struct elf_external_linux_prpsinfo64_ugid16
193  {
194    char pr_state;			/* Numeric process state.  */
195    char pr_sname;			/* Char for pr_state.  */
196    char pr_zomb;			/* Zombie.  */
197    char pr_nice;			/* Nice val.  */
198    char gap[4];
199    char pr_flag[8];			/* Flags.  */
200    char pr_uid[2];
201    char pr_gid[2];
202    char pr_pid[4];
203    char pr_ppid[4];
204    char pr_pgrp[4];
205    char pr_sid[4];
206    char pr_fname[16] ATTRIBUTE_NONSTRING;  /* Filename of executable.  */
207    char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list.  */
208  };
209
210/* Helper function to copy an elf_internal_linux_prpsinfo in host
211   endian to an elf_external_linux_prpsinfo64_ugid16 in target endian.  */
212
213static inline void
214swap_linux_prpsinfo64_ugid16_out
215  (bfd *obfd,
216   const struct elf_internal_linux_prpsinfo *from,
217   struct elf_external_linux_prpsinfo64_ugid16 *to)
218{
219  bfd_put_8 (obfd, from->pr_state, &to->pr_state);
220  bfd_put_8 (obfd, from->pr_sname, &to->pr_sname);
221  bfd_put_8 (obfd, from->pr_zomb, &to->pr_zomb);
222  bfd_put_8 (obfd, from->pr_nice, &to->pr_nice);
223  bfd_put_64 (obfd, from->pr_flag, to->pr_flag);
224  bfd_put_16 (obfd, from->pr_uid, to->pr_uid);
225  bfd_put_16 (obfd, from->pr_gid, to->pr_gid);
226  bfd_put_32 (obfd, from->pr_pid, to->pr_pid);
227  bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
228  bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
229  bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
230  strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
231  strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
232}
233
234#endif
235