Branch data Line data Source code
1 : : /* Print the strings of printable characters in files.
2 : : Copyright (C) 2005-2010, 2012, 2014 Red Hat, Inc.
3 : : This file is part of elfutils.
4 : : Written by Ulrich Drepper <drepper@redhat.com>, 2005.
5 : :
6 : : This file 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 : : elfutils is distributed in the hope that it will be useful, but
12 : : 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, see <http://www.gnu.org/licenses/>. */
18 : :
19 : : #ifdef HAVE_CONFIG_H
20 : : # include <config.h>
21 : : #endif
22 : :
23 : : #include <argp.h>
24 : : #include <assert.h>
25 : : #include <ctype.h>
26 : : #include <endian.h>
27 : : #include <errno.h>
28 : : #include <fcntl.h>
29 : : #include <gelf.h>
30 : : #include <inttypes.h>
31 : : #include <libintl.h>
32 : : #include <locale.h>
33 : : #include <stdbool.h>
34 : : #include <stdio.h>
35 : : #include <stdio_ext.h>
36 : : #include <stdlib.h>
37 : : #include <string.h>
38 : : #include <unistd.h>
39 : : #include <sys/mman.h>
40 : : #include <sys/stat.h>
41 : :
42 : : #include <libeu.h>
43 : : #include <system.h>
44 : : #include <printversion.h>
45 : :
46 : : #ifndef MAP_POPULATE
47 : : # define MAP_POPULATE 0
48 : : #endif
49 : :
50 : :
51 : : /* Prototypes of local functions. */
52 : : static int read_fd (int fd, const char *fname, off_t fdlen);
53 : : static int read_elf (Elf *elf, int fd, const char *fname, off_t fdlen);
54 : :
55 : :
56 : : /* Name and version of program. */
57 : : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
58 : :
59 : : /* Bug report address. */
60 : : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
61 : :
62 : : /* Definitions of arguments for argp functions. */
63 : : static const struct argp_option options[] =
64 : : {
65 : : { NULL, 0, NULL, 0, N_("Output Selection:"), 0 },
66 : : { "all", 'a', NULL, 0, N_("Scan entire file, not only loaded sections"), 0 },
67 : : { "bytes", 'n', "MIN-LEN", 0,
68 : : N_("Only NUL-terminated sequences of MIN-LEN characters or more are printed"), 0 },
69 : : { "encoding", 'e', "SELECTOR", 0, N_("\
70 : : Select character size and endianness: s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit"),
71 : : 0},
72 : : { "print-file-name", 'f', NULL, 0,
73 : : N_("Print name of the file before each string."), 0 },
74 : : { "radix", 't', "{o,d,x}", 0,
75 : : N_("Print location of the string in base 8, 10, or 16 respectively."), 0 },
76 : : { NULL, 'o', NULL, 0, N_("Alias for --radix=o"), 0 },
77 : :
78 : : { NULL, 0, NULL, 0, N_("Miscellaneous:"), 0 },
79 : : { NULL, 0, NULL, 0, NULL, 0 }
80 : : };
81 : :
82 : : /* Short description of program. */
83 : : static const char doc[] = N_("\
84 : : Print the strings of printable characters in files.");
85 : :
86 : : /* Strings for arguments in help texts. */
87 : : static const char args_doc[] = N_("[FILE...]");
88 : :
89 : : /* Prototype for option handler. */
90 : : static error_t parse_opt (int key, char *arg, struct argp_state *state);
91 : :
92 : : /* Data structure to communicate with argp functions. */
93 : : static struct argp argp =
94 : : {
95 : : options, parse_opt, args_doc, doc, NULL, NULL, NULL
96 : : };
97 : :
98 : :
99 : : /* Global variables. */
100 : :
101 : : /* True if whole file and not only loaded sections are looked at. */
102 : : static bool entire_file;
103 : :
104 : : /* Minimum length of any sequence reported. */
105 : : static size_t min_len = 4;
106 : :
107 : : /* Number of bytes per character. */
108 : : static size_t bytes_per_char = 1;
109 : :
110 : : /* Minimum length of any sequence reported in bytes. */
111 : : static size_t min_len_bytes;
112 : :
113 : : /* True if multibyte characters are in big-endian order. */
114 : : static bool big_endian;
115 : :
116 : : /* True unless 7-bit ASCII are expected. */
117 : : static bool char_7bit;
118 : :
119 : : /* True if file names should be printed before strings. */
120 : : static bool print_file_name;
121 : :
122 : : /* Radix for printed numbers. */
123 : : static enum
124 : : {
125 : : radix_none = 0,
126 : : radix_decimal,
127 : : radix_hex,
128 : : radix_octal
129 : : } radix = radix_none;
130 : :
131 : :
132 : : /* Page size in use. */
133 : : static size_t ps;
134 : :
135 : :
136 : : /* Mapped parts of the ELF file. */
137 : : static unsigned char *elfmap;
138 : : static unsigned char *elfmap_base;
139 : : static size_t elfmap_size;
140 : : static off_t elfmap_off;
141 : :
142 : :
143 : : int
144 : 1 : main (int argc, char *argv[])
145 : : {
146 : : /* We use no threads. */
147 : 1 : __fsetlocking (stdin, FSETLOCKING_BYCALLER);
148 : 1 : __fsetlocking (stdout, FSETLOCKING_BYCALLER);
149 : :
150 : : /* Set locale. */
151 : 1 : (void) setlocale (LC_ALL, "");
152 : :
153 : : /* Make sure the message catalog can be found. */
154 : 1 : (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
155 : :
156 : : /* Initialize the message catalog. */
157 : 1 : (void) textdomain (PACKAGE_TARNAME);
158 : :
159 : : /* Parse and process arguments. */
160 : 1 : int remaining;
161 : 1 : (void) argp_parse (&argp, argc, argv, 0, &remaining, NULL);
162 : :
163 : : /* Tell the library which version we are expecting. */
164 : 1 : elf_version (EV_CURRENT);
165 : :
166 : : /* Determine the page size. We will likely need it a couple of times. */
167 : 1 : ps = sysconf (_SC_PAGESIZE);
168 : :
169 : 1 : struct stat st;
170 : 1 : int result = 0;
171 [ - + ]: 1 : if (remaining == argc)
172 : : /* We read from standard input. This we cannot do for a
173 : : structured file. */
174 : 0 : result = read_fd (STDIN_FILENO,
175 [ # # ]: 0 : print_file_name ? "{standard input}" : NULL,
176 [ # # # # ]: 0 : (fstat (STDIN_FILENO, &st) == 0 && S_ISREG (st.st_mode))
177 : : ? st.st_size : INT64_C (0x7fffffffffffffff));
178 : : else
179 : 9 : do
180 : : {
181 : 18 : int fd = (strcmp (argv[remaining], "-") == 0
182 [ + - ]: 9 : ? STDIN_FILENO : open (argv[remaining], O_RDONLY));
183 [ - + ]: 9 : if (unlikely (fd == -1))
184 : : {
185 : 0 : error (0, errno, _("cannot open '%s'"), argv[remaining]);
186 : 0 : result = 1;
187 : : }
188 : : else
189 : : {
190 [ + - ]: 9 : const char *fname = print_file_name ? argv[remaining] : NULL;
191 : 9 : int fstat_fail = fstat (fd, &st);
192 : 18 : off_t fdlen = (fstat_fail
193 [ + - ]: 9 : ? INT64_C (0x7fffffffffffffff) : st.st_size);
194 [ + - ]: 9 : if (fdlen > (off_t) min_len_bytes)
195 : : {
196 : 9 : Elf *elf = NULL;
197 [ + - ]: 9 : if (entire_file
198 [ + - ]: 9 : || fstat_fail
199 [ + - ]: 9 : || !S_ISREG (st.st_mode)
200 [ + - ]: 9 : || (elf = elf_begin (fd, ELF_C_READ, NULL)) == NULL
201 [ - + ]: 9 : || elf_kind (elf) != ELF_K_ELF)
202 : 0 : result |= read_fd (fd, fname, fdlen);
203 : : else
204 : 9 : result |= read_elf (elf, fd, fname, fdlen);
205 : :
206 : : /* This call will succeed even if ELF is NULL. */
207 : 9 : elf_end (elf);
208 : : }
209 : :
210 [ + - ]: 9 : if (strcmp (argv[remaining], "-") != 0)
211 : 9 : close (fd);
212 : : }
213 : :
214 [ - + ]: 9 : if (elfmap != NULL && elfmap != MAP_FAILED)
215 : 0 : munmap (elfmap, elfmap_size);
216 : 9 : elfmap = NULL;
217 : : }
218 [ + + ]: 9 : while (++remaining < argc);
219 : :
220 : 1 : return result;
221 : : }
222 : :
223 : :
224 : : /* Handle program arguments. */
225 : : static error_t
226 : 7 : parse_opt (int key, char *arg,
227 : : struct argp_state *state __attribute__ ((unused)))
228 : : {
229 [ - - + - : 7 : switch (key)
- + + + ]
230 : : {
231 : 0 : case 'a':
232 : 0 : entire_file = true;
233 : 0 : break;
234 : :
235 : 0 : case 'e':
236 : : /* We expect a string of one character. */
237 [ # # # # : 0 : switch (arg[1] != '\0' ? '\0' : arg[0])
# # ]
238 : : {
239 : 0 : case 's':
240 : : case 'S':
241 : 0 : char_7bit = arg[0] == 's';
242 : 0 : bytes_per_char = 1;
243 : 0 : break;
244 : :
245 : 0 : case 'b':
246 : : case 'B':
247 : 0 : big_endian = true;
248 : 0 : FALLTHROUGH;
249 : :
250 : 0 : case 'l':
251 : : case 'L':
252 [ # # ]: 0 : bytes_per_char = isupper (arg[0]) ? 4 : 2;
253 : 0 : break;
254 : :
255 : 0 : default:
256 : 0 : error (0, 0, _("invalid value '%s' for %s parameter"),
257 : : arg, "-e");
258 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE, "strings");
259 : 0 : return ARGP_ERR_UNKNOWN;
260 : : }
261 : : break;
262 : :
263 : 1 : case 'f':
264 : 1 : print_file_name = true;
265 : 1 : break;
266 : :
267 : : case 'n':
268 : 0 : min_len = atoi (arg);
269 : 0 : break;
270 : :
271 : 0 : case 'o':
272 : 0 : goto octfmt;
273 : :
274 : 1 : case 't':
275 [ - - + - ]: 1 : switch (arg[0])
276 : : {
277 : 0 : case 'd':
278 : 0 : radix = radix_decimal;
279 : 0 : break;
280 : :
281 : : case 'o':
282 : 0 : octfmt:
283 : 0 : radix = radix_octal;
284 : 0 : break;
285 : :
286 : 1 : case 'x':
287 : 1 : radix = radix_hex;
288 : 1 : break;
289 : :
290 : 0 : default:
291 : 0 : error (0, 0, _("invalid value '%s' for %s parameter"),
292 : : arg, "-t");
293 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE, "strings");
294 : 0 : return ARGP_ERR_UNKNOWN;
295 : : }
296 : : break;
297 : :
298 : 1 : case ARGP_KEY_FINI:
299 : : /* Compute the length in bytes of any match. */
300 [ + - - + ]: 1 : if (min_len <= 0 || min_len > INT_MAX / bytes_per_char)
301 : 0 : error_exit (0, _("invalid minimum length of matched string size"));
302 : 1 : min_len_bytes = min_len * bytes_per_char;
303 : 1 : break;
304 : :
305 : : default:
306 : : return ARGP_ERR_UNKNOWN;
307 : : }
308 : : return 0;
309 : : }
310 : :
311 : :
312 : : static void
313 : 0 : process_chunk_mb (const char *fname, const unsigned char *buf, off_t to,
314 : : size_t len, char **unprinted)
315 : : {
316 [ # # ]: 0 : size_t curlen = *unprinted == NULL ? 0 : strlen (*unprinted);
317 : : const unsigned char *start = buf;
318 [ # # ]: 0 : while (len >= bytes_per_char)
319 : : {
320 : 0 : uint32_t ch;
321 : :
322 [ # # ]: 0 : if (bytes_per_char == 2)
323 : : {
324 [ # # ]: 0 : if (big_endian)
325 : 0 : ch = buf[0] << 8 | buf[1];
326 : : else
327 : 0 : ch = buf[1] << 8 | buf[0];
328 : : }
329 : : else
330 : : {
331 [ # # ]: 0 : if (big_endian)
332 : 0 : ch = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
333 : : else
334 : 0 : ch = buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0];
335 : : }
336 : :
337 [ # # # # : 0 : if (ch <= 255 && (isprint (ch) || ch == '\t'))
# # ]
338 : : {
339 : 0 : ++buf;
340 : 0 : ++curlen;
341 : : }
342 : : else
343 : : {
344 [ # # ]: 0 : if (curlen >= min_len)
345 : : {
346 : : /* We found a match. */
347 [ # # ]: 0 : if (unlikely (fname != NULL))
348 : : {
349 : 0 : fputs_unlocked (fname, stdout);
350 : 0 : fputs_unlocked (": ", stdout);
351 : : }
352 : :
353 [ # # ]: 0 : if (unlikely (radix != radix_none))
354 [ # # # # ]: 0 : printf ((radix == radix_octal ? "%7" PRIo64 " "
355 : : : (radix == radix_decimal ? "%7" PRId64 " "
356 : : : "%7" PRIx64 " ")),
357 : 0 : (int64_t) to - len - (buf - start));
358 : :
359 [ # # ]: 0 : if (unlikely (*unprinted != NULL))
360 : : {
361 : 0 : fputs_unlocked (*unprinted, stdout);
362 : 0 : free (*unprinted);
363 : 0 : *unprinted = NULL;
364 : : }
365 : :
366 : : /* There is no sane way of printing the string. If we
367 : : assume the file data is encoded in UCS-2/UTF-16 or
368 : : UCS-4/UTF-32 respectively we could covert the string.
369 : : But there is no such guarantee. */
370 [ # # # # : 0 : fwrite_unlocked (start, 1, buf - start, stdout);
# # # # #
# # # #
# ]
371 [ # # ]: 0 : putc_unlocked ('\n', stdout);
372 : : }
373 : :
374 : 0 : start = ++buf;
375 : 0 : curlen = 0;
376 : :
377 [ # # ]: 0 : if (len <= min_len)
378 : : break;
379 : : }
380 : :
381 : 0 : --len;
382 : : }
383 : :
384 [ # # ]: 0 : if (curlen != 0)
385 : 0 : *unprinted = xstrndup ((const char *) start, curlen);
386 : 0 : }
387 : :
388 : :
389 : : static void
390 : 158 : process_chunk (const char *fname, const unsigned char *buf, off_t to,
391 : : size_t len, char **unprinted)
392 : : {
393 : : /* We are not going to slow the check down for the 2- and 4-byte
394 : : encodings. Handle them special. */
395 [ - + ]: 158 : if (unlikely (bytes_per_char != 1))
396 : : {
397 : 0 : process_chunk_mb (fname, buf, to, len, unprinted);
398 : 0 : return;
399 : : }
400 : :
401 [ - + ]: 158 : size_t curlen = *unprinted == NULL ? 0 : strlen (*unprinted);
402 : : const unsigned char *start = buf;
403 [ + + ]: 68044 : while (len > 0)
404 : : {
405 [ + + + + : 68042 : if ((isprint (*buf) || *buf == '\t') && (! char_7bit || *buf <= 127))
- + - - ]
406 : : {
407 : 18503 : ++buf;
408 : 18503 : ++curlen;
409 : : }
410 : : else
411 : : {
412 [ + + ]: 49539 : if (curlen >= min_len)
413 : : {
414 : : /* We found a match. */
415 [ + - ]: 443 : if (likely (fname != NULL))
416 : : {
417 : 443 : fputs_unlocked (fname, stdout);
418 : 443 : fputs_unlocked (": ", stdout);
419 : : }
420 : :
421 [ + - ]: 443 : if (likely (radix != radix_none))
422 [ + - + - ]: 443 : printf ((radix == radix_octal ? "%7" PRIo64 " "
423 : : : (radix == radix_decimal ? "%7" PRId64 " "
424 : : : "%7" PRIx64 " ")),
425 : 443 : (int64_t) to - len - (buf - start));
426 : :
427 [ - + ]: 443 : if (unlikely (*unprinted != NULL))
428 : : {
429 : 0 : fputs_unlocked (*unprinted, stdout);
430 : 0 : free (*unprinted);
431 : 0 : *unprinted = NULL;
432 : : }
433 [ - + - - : 443 : fwrite_unlocked (start, 1, buf - start, stdout);
- - - - -
- - + -
- ]
434 [ - + ]: 443 : putc_unlocked ('\n', stdout);
435 : : }
436 : :
437 : 49539 : start = ++buf;
438 : 49539 : curlen = 0;
439 : :
440 [ + + ]: 49539 : if (len <= min_len)
441 : : break;
442 : : }
443 : :
444 : 67886 : --len;
445 : : }
446 : :
447 [ - + ]: 158 : if (curlen != 0)
448 : 0 : *unprinted = xstrndup ((const char *) start, curlen);
449 : : }
450 : :
451 : :
452 : : /* Map a file in as large chunks as possible. */
453 : : static void *
454 : 9 : map_file (int fd, off_t start_off, off_t fdlen, size_t *map_sizep)
455 : : {
456 : : /* Maximum size we mmap. We use an #ifdef to avoid overflows on
457 : : 32-bit machines. 64-bit machines these days do not have usable
458 : : address spaces larger than about 43 bits. Not that any file
459 : : should be that large. */
460 : : # if SIZE_MAX > 0xffffffff
461 : 9 : const size_t mmap_max = 0x4000000000lu;
462 : : # else
463 : : const size_t mmap_max = 0x40000000lu;
464 : : # endif
465 : :
466 : : /* Try to mmap the file. */
467 : 9 : size_t map_size = MIN ((off_t) mmap_max, fdlen);
468 [ - + - - ]: 9 : const size_t map_size_min = MAX (MAX (SIZE_MAX / 16, 2 * ps),
469 : : roundup (2 * min_len_bytes + 1, ps));
470 : 9 : void *mem;
471 : 9 : while (1)
472 : : {
473 : : /* We map the memory for reading only here. Since we will
474 : : always look at every byte of the file it makes sense to
475 : : use MAP_POPULATE. */
476 : 9 : mem = mmap (NULL, map_size, PROT_READ, MAP_PRIVATE | MAP_POPULATE,
477 : : fd, start_off);
478 [ + - ]: 9 : if (mem != MAP_FAILED)
479 : : {
480 : : /* We will go through the mapping sequentially. */
481 : 9 : (void) posix_madvise (mem, map_size, POSIX_MADV_SEQUENTIAL);
482 : 9 : break;
483 : : }
484 [ # # ]: 0 : if (errno != EINVAL && errno != ENOMEM)
485 : : /* This is an error other than the lack of address space. */
486 : : break;
487 : :
488 : : /* Maybe the size of the mapping is too big. Try again. */
489 : 0 : map_size /= 2;
490 [ # # ]: 0 : if (map_size < map_size_min)
491 : : /* That size should have fit. */
492 : : break;
493 : : }
494 : :
495 : 9 : *map_sizep = map_size;
496 : 9 : return mem;
497 : : }
498 : :
499 : :
500 : : /* Read the file without mapping. */
501 : : static int
502 : 0 : read_block_no_mmap (int fd, const char *fname, off_t from, off_t fdlen)
503 : : {
504 : 0 : char *unprinted = NULL;
505 : : #define CHUNKSIZE 65536
506 : 0 : unsigned char *buf = xmalloc (CHUNKSIZE + min_len_bytes
507 : 0 : + bytes_per_char - 1);
508 : 0 : size_t ntrailer = 0;
509 : 0 : int result = 0;
510 [ # # ]: 0 : while (fdlen > 0)
511 : : {
512 [ # # # # : 0 : ssize_t n = TEMP_FAILURE_RETRY (read (fd, buf + ntrailer,
# # ]
513 : : MIN (fdlen, CHUNKSIZE)));
514 [ # # ]: 0 : if (n == 0)
515 : : {
516 : : /* There are less than MIN_LEN+1 bytes left so there cannot be
517 : : another match. */
518 [ # # ]: 0 : assert (unprinted == NULL || ntrailer == 0);
519 : : break;
520 : : }
521 [ # # ]: 0 : if (unlikely (n < 0))
522 : : {
523 : : /* Something went wrong. */
524 : : result = 1;
525 : : break;
526 : : }
527 : :
528 : : /* Account for the number of bytes read in this round. */
529 : 0 : fdlen -= n;
530 : :
531 : : /* Do not use the signed N value. Note that the addition cannot
532 : : overflow. */
533 : 0 : size_t nb = (size_t) n + ntrailer;
534 [ # # ]: 0 : if (nb >= min_len_bytes)
535 : : {
536 : : /* We only use complete characters. */
537 : 0 : nb &= ~(bytes_per_char - 1);
538 : :
539 : 0 : process_chunk (fname, buf, from + nb, nb, &unprinted);
540 : :
541 : : /* If the last bytes of the buffer (modulo the character
542 : : size) have been printed we are not copying them. */
543 [ # # ]: 0 : size_t to_keep = unprinted != NULL ? 0 : min_len_bytes;
544 : :
545 : 0 : memmove (buf, buf + nb - to_keep, to_keep);
546 : 0 : ntrailer = to_keep;
547 : 0 : from += nb;
548 : : }
549 : : else
550 : : ntrailer = nb;
551 : : }
552 : :
553 : 0 : free (buf);
554 : :
555 : : /* Don't print anything we collected so far. There is no
556 : : terminating NUL byte. */
557 : 0 : free (unprinted);
558 : :
559 : 0 : return result;
560 : : }
561 : :
562 : :
563 : : static int
564 : 158 : read_block (int fd, const char *fname, off_t fdlen, off_t from, off_t to)
565 : : {
566 [ + + ]: 158 : if (elfmap == NULL)
567 : : {
568 : : /* We need a completely new mapping. */
569 : 9 : elfmap_off = from & ~(ps - 1);
570 : 9 : elfmap_base = elfmap = map_file (fd, elfmap_off, fdlen, &elfmap_size);
571 : :
572 [ - + ]: 9 : if (unlikely (elfmap == MAP_FAILED))
573 : : /* Let the kernel know we are going to read everything in sequence. */
574 : 0 : (void) posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
575 : : }
576 : :
577 [ - + ]: 158 : if (unlikely (elfmap == MAP_FAILED))
578 : : {
579 : : /* Read from the file descriptor. For this we must position the
580 : : read pointer. */
581 : : // XXX Eventually add flag which avoids this if the position
582 : : // XXX is known to match.
583 [ # # # # ]: 0 : if (from != 0 && lseek (fd, from, SEEK_SET) != from)
584 : 0 : error_exit (errno, _("lseek failed"));
585 : :
586 : 0 : return read_block_no_mmap (fd, fname, from, to - from);
587 : : }
588 : :
589 [ - + ]: 158 : assert ((off_t) min_len_bytes < fdlen);
590 : :
591 [ + - - + ]: 158 : if (to < (off_t) elfmap_off || from > (off_t) (elfmap_off + elfmap_size))
592 : : {
593 : : /* The existing mapping cannot fit at all. Map the new area.
594 : : We always map the full range of ELFMAP_SIZE bytes even if
595 : : this extend beyond the end of the file. The Linux kernel
596 : : handles this OK if the access pages are not touched. */
597 : 0 : elfmap_off = from & ~(ps - 1);
598 [ # # ]: 0 : if (mmap (elfmap, elfmap_size, PROT_READ,
599 : : MAP_PRIVATE | MAP_POPULATE | MAP_FIXED, fd, from)
600 : : == MAP_FAILED)
601 : 0 : error_exit (errno, _("re-mmap failed"));
602 : 0 : elfmap_base = elfmap;
603 : : }
604 : :
605 : 158 : char *unprinted = NULL;
606 : :
607 : : /* Use the existing mapping as much as possible. If necessary, map
608 : : new pages. */
609 [ + - ]: 158 : if (from >= (off_t) elfmap_off
610 [ + - ]: 158 : && from < (off_t) (elfmap_off + elfmap_size))
611 : : /* There are at least a few bytes in this mapping which we can
612 : : use. */
613 : 158 : process_chunk (fname, elfmap_base + (from - elfmap_off),
614 : : MIN (to, (off_t) (elfmap_off + elfmap_size)),
615 : 158 : MIN (to, (off_t) (elfmap_off + elfmap_size)) - from,
616 : : &unprinted);
617 : :
618 [ - + ]: 158 : if (to > (off_t) (elfmap_off + elfmap_size))
619 : : {
620 : 0 : unsigned char *remap_base = elfmap_base;
621 : 0 : size_t read_now = elfmap_size - (elfmap_base - elfmap);
622 : :
623 [ # # # # ]: 0 : assert (from >= (off_t) elfmap_off
624 : : && from < (off_t) (elfmap_off + elfmap_size));
625 : 0 : off_t handled_to = elfmap_off + elfmap_size;
626 [ # # # # ]: 0 : assert (elfmap == elfmap_base
627 : : || (elfmap_base - elfmap
628 : : == (ptrdiff_t) ((min_len_bytes + ps - 1) & ~(ps - 1))));
629 [ # # ]: 0 : if (elfmap == elfmap_base)
630 : : {
631 : 0 : size_t keep_area = (min_len_bytes + ps - 1) & ~(ps - 1);
632 [ # # ]: 0 : assert (elfmap_size >= keep_area + ps);
633 : : /* The keep area is used for the content of the previous
634 : : buffer we have to keep. This means copying those bytes
635 : : and for this we have to make the data writable. */
636 [ # # ]: 0 : if (unlikely (mprotect (elfmap, keep_area, PROT_READ | PROT_WRITE)
637 : : != 0))
638 : 0 : error_exit (errno, _("mprotect failed"));
639 : :
640 : 0 : elfmap_base = elfmap + keep_area;
641 : : }
642 : :
643 : 0 : while (1)
644 : : {
645 : : /* Map the rest of the file, eventually again in pieces.
646 : : We speed things up with a nice Linux feature. Note
647 : : that we have at least two pages mapped. */
648 [ # # ]: 0 : size_t to_keep = unprinted != NULL ? 0 : min_len_bytes;
649 : :
650 [ # # ]: 0 : assert (read_now >= to_keep);
651 : 0 : memmove (elfmap_base - to_keep,
652 [ # # ]: 0 : remap_base + read_now - to_keep, to_keep);
653 : 0 : remap_base = elfmap_base;
654 : :
655 [ # # ]: 0 : assert ((elfmap_size - (elfmap_base - elfmap)) % bytes_per_char
656 : : == 0);
657 : 0 : read_now = MIN (to - handled_to,
658 : : (ptrdiff_t) elfmap_size - (elfmap_base - elfmap));
659 : :
660 [ # # ]: 0 : assert (handled_to % ps == 0);
661 [ # # ]: 0 : assert (handled_to % bytes_per_char == 0);
662 [ # # ]: 0 : if (mmap (remap_base, read_now, PROT_READ,
663 : : MAP_PRIVATE | MAP_POPULATE | MAP_FIXED, fd, handled_to)
664 : : == MAP_FAILED)
665 : 0 : error_exit (errno, _("re-mmap failed"));
666 : 0 : elfmap_off = handled_to;
667 : :
668 : 0 : process_chunk (fname, remap_base - to_keep,
669 : 0 : elfmap_off + (read_now & ~(bytes_per_char - 1)),
670 : : to_keep + (read_now & ~(bytes_per_char - 1)),
671 : : &unprinted);
672 : 0 : handled_to += read_now;
673 [ # # ]: 0 : if (handled_to >= to)
674 : : break;
675 : : }
676 : : }
677 : :
678 : : /* Don't print anything we collected so far. There is no
679 : : terminating NUL byte. */
680 : 158 : free (unprinted);
681 : :
682 : 158 : return 0;
683 : : }
684 : :
685 : :
686 : : static int
687 : 0 : read_fd (int fd, const char *fname, off_t fdlen)
688 : : {
689 : 0 : return read_block (fd, fname, fdlen, 0, fdlen);
690 : : }
691 : :
692 : :
693 : : static int
694 : 9 : read_elf (Elf *elf, int fd, const char *fname, off_t fdlen)
695 : : {
696 [ - + ]: 9 : assert (fdlen >= 0);
697 : :
698 : : /* We will look at each section separately. The ELF file is not
699 : : mmapped. The libelf implementation will load the needed parts on
700 : : demand. Since we only iterate over the section header table the
701 : : memory consumption at this stage is kept minimal. */
702 : 9 : Elf_Scn *scn = elf_nextscn (elf, NULL);
703 [ - + ]: 9 : if (scn == NULL)
704 : 0 : return read_fd (fd, fname, fdlen);
705 : :
706 : : int result = 0;
707 : 276 : do
708 : : {
709 : 276 : GElf_Shdr shdr_mem;
710 : 276 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
711 : :
712 : : /* Only look in sections which are loaded at runtime and
713 : : actually have content. */
714 [ + - + + ]: 276 : if (shdr != NULL && shdr->sh_type != SHT_NOBITS
715 [ + + ]: 265 : && (shdr->sh_flags & SHF_ALLOC) != 0)
716 : : {
717 [ + - ]: 158 : if (shdr->sh_offset > (Elf64_Off) fdlen
718 [ - + ]: 158 : || fdlen - shdr->sh_offset < shdr->sh_size)
719 : 0 : {
720 : 0 : size_t strndx = 0;
721 : 0 : const char *sname;
722 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (elf, &strndx) < 0))
723 : : sname = "<unknown>";
724 : : else
725 [ # # ]: 0 : sname = elf_strptr (elf, strndx, shdr->sh_name) ?: "<unknown>";
726 : 0 : error (0, 0,
727 : 0 : _("Skipping section %zd '%s' data outside file"),
728 : : elf_ndxscn (scn), sname);
729 : 0 : result = 1;
730 : : }
731 : : else
732 : 158 : result |= read_block (fd, fname, fdlen, shdr->sh_offset,
733 : 158 : shdr->sh_offset + shdr->sh_size);
734 : : }
735 : : }
736 [ + + ]: 276 : while ((scn = elf_nextscn (elf, scn)) != NULL);
737 : :
738 [ + - ]: 9 : if (elfmap != NULL && elfmap != MAP_FAILED)
739 : 9 : munmap (elfmap, elfmap_size);
740 : 9 : elfmap = NULL;
741 : :
742 : 9 : return result;
743 : : }
744 : :
745 : :
746 : : #include "debugpred.h"
|