1 /* Definitions for PRPSINFO structures under ELF on GNU/Linux.
2    Copyright (C) 2013-2020 Free Software Foundation, Inc.
3 
4    This file is part of BFD, the Binary File Descriptor library.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 #ifndef ELF_LINUX_CORE_H
22 #define ELF_LINUX_CORE_H
23 
24 /* External 32-bit structure for PRPSINFO.  This structure is
25    ABI-defined, thus we choose to use char arrays here in order to
26    avoid dealing with different types in different architectures.
27 
28    This is the variant for targets which use a 32-bit data type for
29    UID and GID, as all modern Linux ports do.  Some older ports use
30    a 16-bit data type instead; see below for the alternative variant.
31 
32    This structure will ultimately be written in the corefile's note
33    section, as the PRPSINFO.  */
34 
35 struct elf_external_linux_prpsinfo32_ugid32
36   {
37     char pr_state;			/* Numeric process state.  */
38     char pr_sname;			/* Char for pr_state.  */
39     char pr_zomb;			/* Zombie.  */
40     char pr_nice;			/* Nice val.  */
41     char pr_flag[4];			/* Flags.  */
42     char pr_uid[4];
43     char pr_gid[4];
44     char pr_pid[4];
45     char pr_ppid[4];
46     char pr_pgrp[4];
47     char pr_sid[4];
48     char pr_fname[16] ATTRIBUTE_NONSTRING;  /* Filename of executable.  */
49     char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list.  */
50   };
51 
52 /* Helper function to copy an elf_internal_linux_prpsinfo in host
53    endian to an elf_external_linux_prpsinfo32_ugid32 in target endian.  */
54 
55 static inline void
56 swap_linux_prpsinfo32_ugid32_out
57   (bfd *obfd,
58    const struct elf_internal_linux_prpsinfo *from,
59    struct elf_external_linux_prpsinfo32_ugid32 *to)
60 {
61   bfd_put_8 (obfd, from->pr_state, &to->pr_state);
62   bfd_put_8 (obfd, from->pr_sname, &to->pr_sname);
63   bfd_put_8 (obfd, from->pr_zomb, &to->pr_zomb);
64   bfd_put_8 (obfd, from->pr_nice, &to->pr_nice);
65   bfd_put_32 (obfd, from->pr_flag, to->pr_flag);
66   bfd_put_32 (obfd, from->pr_uid, to->pr_uid);
67   bfd_put_32 (obfd, from->pr_gid, to->pr_gid);
68   bfd_put_32 (obfd, from->pr_pid, to->pr_pid);
69   bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
70   bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
71   bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
72   strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
73   strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
74 }
75 
76 /* External 32-bit structure for PRPSINFO.  This structure is
77    ABI-defined, thus we choose to use char arrays here in order to
78    avoid dealing with different types in different architectures.
79 
80    This is the variant for targets which use a 16-bit data type for
81    UID and GID, as some older Linux ports do.  All modern ports use
82    a 32-bit data type instead; see above for the alternative variant.
83 
84    This structure will ultimately be written in the corefile's note
85    section, as the PRPSINFO.  */
86 
87 struct elf_external_linux_prpsinfo32_ugid16
88   {
89     char pr_state;			/* Numeric process state.  */
90     char pr_sname;			/* Char for pr_state.  */
91     char pr_zomb;			/* Zombie.  */
92     char pr_nice;			/* Nice val.  */
93     char pr_flag[4];			/* Flags.  */
94     char pr_uid[2];
95     char pr_gid[2];
96     char pr_pid[4];
97     char pr_ppid[4];
98     char pr_pgrp[4];
99     char pr_sid[4];
100     char pr_fname[16] ATTRIBUTE_NONSTRING;  /* Filename of executable.  */
101     char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list.  */
102   };
103 
104 /* Helper function to copy an elf_internal_linux_prpsinfo in host
105    endian to an elf_external_linux_prpsinfo32_ugid16 in target endian.  */
106 
107 static inline void
108 swap_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 
139 struct 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 
160 static inline void
161 swap_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 
192 struct 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 
213 static inline void
214 swap_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