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

source code of glibc/elf/dl-printf.c