Revert "elf: Refuse to dlopen PIE objects [BZ #24323]"
[platform/upstream/glibc.git] / elf / dl-printf.c
1 /* printf implementation for the dynamic loader.
2    Copyright (C) 1997-2024 Free Software Foundation, Inc.
3    Copyright The GNU Toolchain Authors.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library 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 GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <https://www.gnu.org/licenses/>.  */
19
20 #include <string.h>
21 #if BUILD_PIE_DEFAULT
22 # pragma GCC visibility push(hidden)
23 #endif
24 #include <_itoa.h>
25 #include <assert.h>
26 #include <dl-writev.h>
27 #include <ldsodefs.h>
28 #include <limits.h>
29 #include <stdarg.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <sys/uio.h>
33 #include <unistd.h>
34 #include <intprops.h>
35
36 /* The function might be called before the process is self-relocated.  */
37 static size_t
38 _dl_debug_strlen (const char *s)
39 {
40   const char *p = s;
41   for (; *s != '\0'; s++);
42   return s - p;
43 }
44
45 /* Bare-bones printf implementation.  This function only knows about
46    the formats and flags needed and can handle only up to 64 stripes in
47    the output.  */
48 static void
49 _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
50 {
51 # define NIOVMAX 64
52   struct iovec iov[NIOVMAX];
53   /* Maximum size for 'd', 'u', and 'x' including padding.  */
54   enum { IFMTSIZE = INT_STRLEN_BOUND(void *) };
55   char ifmtbuf[NIOVMAX][IFMTSIZE];
56   int niov = 0;
57   pid_t pid = 0;
58   char pidbuf[12];
59
60   while (*fmt != '\0')
61     {
62       const char *startp = fmt;
63
64       if (tag_p > 0)
65         {
66           /* Generate the tag line once.  It consists of the PID and a
67              colon followed by a tab.  */
68           if (pid == 0)
69             {
70               char *p;
71               pid = __getpid ();
72               assert (pid >= 0 && sizeof (pid_t) <= 4);
73               p = _itoa (pid, &pidbuf[10], 10, 0);
74               while (p > pidbuf)
75                 *--p = ' ';
76               pidbuf[10] = ':';
77               pidbuf[11] = '\t';
78             }
79
80           /* Append to the output.  */
81           assert (niov < NIOVMAX);
82           iov[niov].iov_len = 12;
83           iov[niov++].iov_base = pidbuf;
84
85           /* No more tags until we see the next newline.  */
86           tag_p = -1;
87         }
88
89       /* Skip everything except % and \n (if tags are needed).  */
90       while (*fmt != '\0' && *fmt != '%' && (! tag_p || *fmt != '\n'))
91         ++fmt;
92
93       /* Append constant string.  */
94       assert (niov < NIOVMAX);
95       if ((iov[niov].iov_len = fmt - startp) != 0)
96         iov[niov++].iov_base = (char *) startp;
97
98       if (*fmt == '%')
99         {
100           /* It is a format specifier.  */
101           char fill = ' ';
102           int width = -1;
103           int prec = -1;
104 #if LONG_MAX != INT_MAX
105           int long_mod = 0;
106 #endif
107
108           /* Recognize zero-digit fill flag.  */
109           if (*++fmt == '0')
110             {
111               fill = '0';
112               ++fmt;
113             }
114
115           /* See whether with comes from a parameter.  Note that no other
116              way to specify the width is implemented.  */
117           if (*fmt == '*')
118             {
119               width = va_arg (arg, int);
120               /* The maximum padding accepted is up to pointer size.  */
121               assert (width < IFMTSIZE);
122               ++fmt;
123             }
124
125           /* Handle precision.  */
126           if (*fmt == '.' && fmt[1] == '*')
127             {
128               prec = va_arg (arg, int);
129               fmt += 2;
130             }
131
132           /* Recognize the l modifier.  It is only important on some
133              platforms where long and int have a different size.  We
134              can use the same code for size_t.  */
135           if (*fmt == 'l' || *fmt == 'z')
136             {
137 #if LONG_MAX != INT_MAX
138               long_mod = 1;
139 #endif
140               ++fmt;
141             }
142
143           switch (*fmt)
144             {
145               /* Integer formatting.  */
146             case 'd':
147             case 'u':
148             case 'x':
149               {
150                 /* We have to make a difference if long and int have a
151                    different size.  */
152 #if LONG_MAX != INT_MAX
153                 unsigned long int num = (long_mod
154                                          ? va_arg (arg, unsigned long int)
155                                          : va_arg (arg, unsigned int));
156 #else
157                 unsigned long int num = va_arg (arg, unsigned int);
158 #endif
159                 bool negative = false;
160                 if (*fmt == 'd')
161                   {
162 #if LONG_MAX != INT_MAX
163                     if (long_mod)
164                       {
165                         if ((long int) num < 0)
166                           {
167                             num = -num;
168                             negative = true;
169                           }
170                       }
171                     else
172                       {
173                         if ((int) num < 0)
174                           {
175                             num = -(unsigned int) num;
176                             negative = true;
177                           }
178                       }
179 #else
180                     if ((int) num < 0)
181                       {
182                         num = -num;
183                         negative = true;
184                       }
185 #endif
186                   }
187
188                 char *endp = &ifmtbuf[niov][IFMTSIZE];
189                 char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0);
190
191                 /* Pad to the width the user specified.  */
192                 if (width != -1)
193                   while (endp - cp < width)
194                     *--cp = fill;
195
196                 if (negative)
197                   *--cp = '-';
198
199                 iov[niov].iov_base = cp;
200                 iov[niov].iov_len = endp - cp;
201                 ++niov;
202               }
203               break;
204
205             case 's':
206               /* Get the string argument.  */
207               iov[niov].iov_base = va_arg (arg, char *);
208               iov[niov].iov_len = _dl_debug_strlen (iov[niov].iov_base);
209               if (prec != -1)
210                 iov[niov].iov_len = MIN ((size_t) prec, iov[niov].iov_len);
211               ++niov;
212               break;
213
214             case '%':
215               iov[niov].iov_base = (void *) fmt;
216               iov[niov].iov_len = 1;
217               ++niov;
218               break;
219
220             default:
221               assert (! "invalid format specifier");
222             }
223           ++fmt;
224         }
225       else if (*fmt == '\n')
226         {
227           /* See whether we have to print a single newline character.  */
228           if (fmt == startp)
229             {
230               iov[niov].iov_base = (char *) startp;
231               iov[niov++].iov_len = 1;
232             }
233           else
234             /* No, just add it to the rest of the string.  */
235             ++iov[niov - 1].iov_len;
236
237           /* Next line, print a tag again.  */
238           tag_p = 1;
239           ++fmt;
240         }
241     }
242
243   /* Finally write the result.  */
244   _dl_writev (fd, iov, niov);
245 }
246
247
248 /* Write to debug file.  */
249 void
250 _dl_debug_printf (const char *fmt, ...)
251 {
252   va_list arg;
253
254   va_start (arg, fmt);
255   _dl_debug_vdprintf (GLRO(dl_debug_fd), 1, fmt, arg);
256   va_end (arg);
257 }
258
259
260 /* Write to debug file but don't start with a tag.  */
261 void
262 _dl_debug_printf_c (const char *fmt, ...)
263 {
264   va_list arg;
265
266   va_start (arg, fmt);
267   _dl_debug_vdprintf (GLRO(dl_debug_fd), -1, fmt, arg);
268   va_end (arg);
269 }
270
271
272 /* Write the given file descriptor.  */
273 void
274 _dl_dprintf (int fd, const char *fmt, ...)
275 {
276   va_list arg;
277
278   va_start (arg, fmt);
279   _dl_debug_vdprintf (fd, 0, fmt, arg);
280   va_end (arg);
281 }
282
283 void
284 _dl_printf (const char *fmt, ...)
285 {
286   va_list arg;
287
288   va_start (arg, fmt);
289   _dl_debug_vdprintf (STDOUT_FILENO, 0, fmt, arg);
290   va_end (arg);
291 }
292
293 void
294 _dl_error_printf (const char *fmt, ...)
295 {
296   va_list arg;
297
298   va_start (arg, fmt);
299   _dl_debug_vdprintf (STDERR_FILENO, 0, fmt, arg);
300   va_end (arg);
301 }
302
303 void
304 _dl_fatal_printf (const char *fmt, ...)
305 {
306   va_list arg;
307
308   va_start (arg, fmt);
309   _dl_debug_vdprintf (STDERR_FILENO, 0, fmt, arg);
310   va_end (arg);
311   _exit (127);
312 }
313 rtld_hidden_def (_dl_fatal_printf)