Automatic date update in version.in
[deliverable/binutils-gdb.git] / binutils / strings.c
CommitLineData
252b5132 1/* strings -- print the strings of printable characters in files
4b95cf5c 2 Copyright (C) 1993-2014 Free Software Foundation, Inc.
252b5132
RH
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
32866df7 6 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
b43b5d5f
NC
16 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
17 02110-1301, USA. */
252b5132
RH
18\f
19/* Usage: strings [options] file...
20
21 Options:
22 --all
23 -a
7fac9594
NC
24 - Scan each file in its entirety.
25
26 --data
27 -d Scan only the initialized data section(s) of object files.
252b5132
RH
28
29 --print-file-name
30 -f Print the name of the file before each string.
31
32 --bytes=min-len
33 -n min-len
34 -min-len Print graphic char sequences, MIN-LEN or more bytes long,
35 that are followed by a NUL or a newline. Default is 4.
36
37 --radix={o,x,d}
38 -t {o,x,d} Print the offset within the file before each string,
39 in octal/hex/decimal.
40
334ac421
EA
41 --include-all-whitespace
42 -w By default tab and space are the only whitepace included in graphic
43 char sequences. This option considers all of isspace() valid.
44
252b5132
RH
45 -o Like -to. (Some other implementations have -o like -to,
46 others like -td. We chose one arbitrarily.)
47
8745eafa
NC
48 --encoding={s,S,b,l,B,L}
49 -e {s,S,b,l,B,L}
50 Select character encoding: 7-bit-character, 8-bit-character,
51 bigendian 16-bit, littleendian 16-bit, bigendian 32-bit,
52 littleendian 32-bit.
d132876a 53
252b5132 54 --target=BFDNAME
3bf31ec9 55 -T {bfdname}
252b5132
RH
56 Specify a non-default object file format.
57
58 --help
59 -h Print the usage message on the standard output.
60
61 --version
ffbe5983 62 -V
252b5132
RH
63 -v Print the program version number.
64
65 Written by Richard Stallman <rms@gnu.ai.mit.edu>
66 and David MacKenzie <djm@gnu.ai.mit.edu>. */
67
3db64b00 68#include "sysdep.h"
252b5132 69#include "bfd.h"
e9792343 70#include "getopt.h"
252b5132 71#include "libiberty.h"
3882b010 72#include "safe-ctype.h"
3db64b00 73#include "bucomm.h"
252b5132 74
8745eafa
NC
75#define STRING_ISGRAPHIC(c) \
76 ( (c) >= 0 \
77 && (c) <= 255 \
334ac421
EA
78 && ((c) == '\t' || ISPRINT (c) || (encoding == 'S' && (c) > 127) \
79 || (include_all_whitespace == TRUE && ISSPACE (c))) \
80 )
252b5132
RH
81
82#ifndef errno
83extern int errno;
84#endif
85
86/* The BFD section flags that identify an initialized data section. */
87#define DATA_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)
88
89/* Radix for printing addresses (must be 8, 10 or 16). */
90static int address_radix;
91
92/* Minimum length of sequence of graphic chars to trigger output. */
93static int string_min;
94
334ac421
EA
95/* Whether or not we include all whitespace as a graphic char. */
96static bfd_boolean include_all_whitespace;
97
b34976b6
AM
98/* TRUE means print address within file for each string. */
99static bfd_boolean print_addresses;
252b5132 100
b34976b6
AM
101/* TRUE means print filename for each string. */
102static bfd_boolean print_filenames;
252b5132 103
b34976b6
AM
104/* TRUE means for object files scan only the data section. */
105static bfd_boolean datasection_only;
252b5132 106
b34976b6
AM
107/* TRUE if we found an initialized data section in the current file. */
108static bfd_boolean got_a_section;
252b5132
RH
109
110/* The BFD object file format. */
111static char *target;
112
d132876a
NC
113/* The character encoding format. */
114static char encoding;
115static int encoding_bytes;
116
252b5132
RH
117static struct option long_options[] =
118{
119 {"all", no_argument, NULL, 'a'},
7fac9594 120 {"data", no_argument, NULL, 'd'},
252b5132
RH
121 {"print-file-name", no_argument, NULL, 'f'},
122 {"bytes", required_argument, NULL, 'n'},
123 {"radix", required_argument, NULL, 't'},
334ac421 124 {"include-all-whitespace", required_argument, NULL, 'w'},
d132876a 125 {"encoding", required_argument, NULL, 'e'},
252b5132
RH
126 {"target", required_argument, NULL, 'T'},
127 {"help", no_argument, NULL, 'h'},
128 {"version", no_argument, NULL, 'v'},
129 {NULL, 0, NULL, 0}
130};
131
06803313
NC
132/* Records the size of a named file so that we
133 do not repeatedly run bfd_stat() on it. */
134
135typedef struct
136{
137 const char * filename;
138 bfd_size_type filesize;
139} filename_and_size_t;
140
2da42df6
AJ
141static void strings_a_section (bfd *, asection *, void *);
142static bfd_boolean strings_object_file (const char *);
7fac9594 143static bfd_boolean strings_file (char *);
ee2fb9eb 144static void print_strings (const char *, FILE *, file_ptr, int, int, char *);
2da42df6 145static void usage (FILE *, int);
ee2fb9eb 146static long get_char (FILE *, file_ptr *, int *, char **);
252b5132 147\f
2da42df6 148int main (int, char **);
65de42c0 149
252b5132 150int
2da42df6 151main (int argc, char **argv)
252b5132
RH
152{
153 int optc;
154 int exit_status = 0;
b34976b6 155 bfd_boolean files_given = FALSE;
508e676d 156 char *s;
e36aef42 157 int numeric_opt = 0;
252b5132 158
3882b010 159#if defined (HAVE_SETLOCALE)
1c529ca6 160 setlocale (LC_ALL, "");
252b5132
RH
161#endif
162 bindtextdomain (PACKAGE, LOCALEDIR);
163 textdomain (PACKAGE);
164
165 program_name = argv[0];
166 xmalloc_set_program_name (program_name);
869b9d07
MM
167
168 expandargv (&argc, &argv);
169
c904a764 170 string_min = 4;
334ac421 171 include_all_whitespace = FALSE;
b34976b6
AM
172 print_addresses = FALSE;
173 print_filenames = FALSE;
7fac9594
NC
174 if (DEFAULT_STRINGS_ALL)
175 datasection_only = FALSE;
176 else
177 datasection_only = TRUE;
252b5132 178 target = NULL;
d132876a 179 encoding = 's';
252b5132 180
7fac9594 181 while ((optc = getopt_long (argc, argv, "adfhHn:wot:e:T:Vv0123456789",
252b5132
RH
182 long_options, (int *) 0)) != EOF)
183 {
184 switch (optc)
185 {
186 case 'a':
b34976b6 187 datasection_only = FALSE;
252b5132
RH
188 break;
189
7fac9594
NC
190 case 'd':
191 datasection_only = TRUE;
192 break;
193
252b5132 194 case 'f':
b34976b6 195 print_filenames = TRUE;
252b5132
RH
196 break;
197
8b53311e 198 case 'H':
252b5132
RH
199 case 'h':
200 usage (stdout, 0);
201
202 case 'n':
508e676d
JK
203 string_min = (int) strtoul (optarg, &s, 0);
204 if (s != NULL && *s != 0)
205 fatal (_("invalid integer argument %s"), optarg);
252b5132
RH
206 break;
207
334ac421
EA
208 case 'w':
209 include_all_whitespace = TRUE;
210 break;
211
252b5132 212 case 'o':
b34976b6 213 print_addresses = TRUE;
252b5132
RH
214 address_radix = 8;
215 break;
216
217 case 't':
b34976b6 218 print_addresses = TRUE;
252b5132
RH
219 if (optarg[1] != '\0')
220 usage (stderr, 1);
221 switch (optarg[0])
222 {
223 case 'o':
224 address_radix = 8;
225 break;
226
227 case 'd':
228 address_radix = 10;
229 break;
230
231 case 'x':
232 address_radix = 16;
233 break;
234
235 default:
236 usage (stderr, 1);
237 }
238 break;
239
240 case 'T':
241 target = optarg;
242 break;
243
d132876a
NC
244 case 'e':
245 if (optarg[1] != '\0')
246 usage (stderr, 1);
247 encoding = optarg[0];
248 break;
249
8b53311e 250 case 'V':
252b5132
RH
251 case 'v':
252 print_version ("strings");
253 break;
254
255 case '?':
256 usage (stderr, 1);
257
258 default:
e36aef42 259 numeric_opt = optind;
252b5132
RH
260 break;
261 }
262 }
263
e36aef42
AM
264 if (numeric_opt != 0)
265 {
266 string_min = (int) strtoul (argv[numeric_opt - 1] + 1, &s, 0);
267 if (s != NULL && *s != 0)
268 fatal (_("invalid integer argument %s"), argv[numeric_opt - 1] + 1);
269 }
c904a764
NC
270 if (string_min < 1)
271 fatal (_("invalid minimum string length %d"), string_min);
252b5132 272
d132876a
NC
273 switch (encoding)
274 {
8745eafa 275 case 'S':
d132876a
NC
276 case 's':
277 encoding_bytes = 1;
278 break;
279 case 'b':
280 case 'l':
281 encoding_bytes = 2;
282 break;
283 case 'B':
284 case 'L':
285 encoding_bytes = 4;
286 break;
287 default:
288 usage (stderr, 1);
289 }
290
252b5132
RH
291 bfd_init ();
292 set_default_bfd_target ();
293
294 if (optind >= argc)
295 {
b34976b6 296 datasection_only = FALSE;
5af11cab 297 SET_BINARY (fileno (stdin));
252b5132 298 print_strings ("{standard input}", stdin, 0, 0, 0, (char *) NULL);
b34976b6 299 files_given = TRUE;
252b5132
RH
300 }
301 else
302 {
303 for (; optind < argc; ++optind)
304 {
305 if (strcmp (argv[optind], "-") == 0)
b34976b6 306 datasection_only = FALSE;
252b5132
RH
307 else
308 {
b34976b6
AM
309 files_given = TRUE;
310 exit_status |= strings_file (argv[optind]) == FALSE;
252b5132
RH
311 }
312 }
313 }
314
b34976b6 315 if (!files_given)
252b5132
RH
316 usage (stderr, 1);
317
318 return (exit_status);
319}
320\f
06803313
NC
321/* Scan section SECT of the file ABFD, whose printable name is in
322 ARG->filename and whose size might be in ARG->filesize. If it
323 contains initialized data set `got_a_section' and print the
324 strings in it.
325
326 FIXME: We ought to be able to return error codes/messages for
327 certain conditions. */
252b5132
RH
328
329static void
06803313 330strings_a_section (bfd *abfd, asection *sect, void *arg)
252b5132 331{
06803313
NC
332 filename_and_size_t * filename_and_sizep;
333 bfd_size_type *filesizep;
334 bfd_size_type sectsize;
335 void *mem;
336
337 if ((sect->flags & DATA_FLAGS) != DATA_FLAGS)
338 return;
339
340 sectsize = bfd_get_section_size (sect);
341
342 if (sectsize <= 0)
343 return;
344
345 /* Get the size of the file. This might have been cached for us. */
346 filename_and_sizep = (filename_and_size_t *) arg;
347 filesizep = & filename_and_sizep->filesize;
348
349 if (*filesizep == 0)
350 {
351 struct stat st;
352
353 if (bfd_stat (abfd, &st))
354 return;
355
356 /* Cache the result so that we do not repeatedly stat this file. */
357 *filesizep = st.st_size;
358 }
252b5132 359
06803313
NC
360 /* Compare the size of the section against the size of the file.
361 If the section is bigger then the file must be corrupt and
362 we should not try dumping it. */
363 if (sectsize >= *filesizep)
364 return;
365
366 mem = xmalloc (sectsize);
367
368 if (bfd_get_section_contents (abfd, sect, mem, (file_ptr) 0, sectsize))
252b5132 369 {
06803313 370 got_a_section = TRUE;
8745eafa 371
06803313 372 print_strings (filename_and_sizep->filename, NULL, sect->filepos,
3f5e193b 373 0, sectsize, (char *) mem);
252b5132 374 }
06803313
NC
375
376 free (mem);
252b5132
RH
377}
378
379/* Scan all of the sections in FILE, and print the strings
380 in the initialized data section(s).
381
b34976b6
AM
382 Return TRUE if successful,
383 FALSE if not (such as if FILE is not an object file). */
252b5132 384
b34976b6 385static bfd_boolean
2da42df6 386strings_object_file (const char *file)
252b5132 387{
06803313
NC
388 filename_and_size_t filename_and_size;
389 bfd *abfd;
390
391 abfd = bfd_openr (file, target);
252b5132
RH
392
393 if (abfd == NULL)
8745eafa
NC
394 /* Treat the file as a non-object file. */
395 return FALSE;
252b5132
RH
396
397 /* This call is mainly for its side effect of reading in the sections.
398 We follow the traditional behavior of `strings' in that we don't
399 complain if we don't recognize a file to be an object file. */
b34976b6 400 if (!bfd_check_format (abfd, bfd_object))
252b5132
RH
401 {
402 bfd_close (abfd);
b34976b6 403 return FALSE;
252b5132
RH
404 }
405
b34976b6 406 got_a_section = FALSE;
06803313
NC
407 filename_and_size.filename = file;
408 filename_and_size.filesize = 0;
409 bfd_map_over_sections (abfd, strings_a_section, & filename_and_size);
252b5132
RH
410
411 if (!bfd_close (abfd))
412 {
413 bfd_nonfatal (file);
b34976b6 414 return FALSE;
252b5132
RH
415 }
416
417 return got_a_section;
418}
419
b34976b6 420/* Print the strings in FILE. Return TRUE if ok, FALSE if an error occurs. */
252b5132 421
b34976b6 422static bfd_boolean
2da42df6 423strings_file (char *file)
252b5132 424{
ee2fb9eb
JK
425 struct stat st;
426
427 /* get_file_size does not support non-S_ISREG files. */
fb5b5478 428
ee2fb9eb 429 if (stat (file, &st) < 0)
fb5b5478
JJ
430 {
431 if (errno == ENOENT)
432 non_fatal (_("'%s': No such file"), file);
433 else
434 non_fatal (_("Warning: could not locate '%s'. reason: %s"),
435 file, strerror (errno));
436 return FALSE;
437 }
f24ddbdd 438
252b5132
RH
439 /* If we weren't told to scan the whole file,
440 try to open it as an object file and only look at
441 initialized data sections. If that fails, fall back to the
442 whole file. */
443 if (!datasection_only || !strings_object_file (file))
444 {
445 FILE *stream;
446
ee2fb9eb 447 stream = fopen (file, FOPEN_RB);
252b5132
RH
448 if (stream == NULL)
449 {
450 fprintf (stderr, "%s: ", program_name);
451 perror (file);
b34976b6 452 return FALSE;
252b5132
RH
453 }
454
ee2fb9eb 455 print_strings (file, stream, (file_ptr) 0, 0, 0, (char *) 0);
252b5132
RH
456
457 if (fclose (stream) == EOF)
458 {
459 fprintf (stderr, "%s: ", program_name);
460 perror (file);
b34976b6 461 return FALSE;
252b5132
RH
462 }
463 }
464
b34976b6 465 return TRUE;
252b5132
RH
466}
467\f
d132876a
NC
468/* Read the next character, return EOF if none available.
469 Assume that STREAM is positioned so that the next byte read
470 is at address ADDRESS in the file.
471
472 If STREAM is NULL, do not read from it.
473 The caller can supply a buffer of characters
474 to be processed before the data in STREAM.
475 MAGIC is the address of the buffer and
476 MAGICCOUNT is how many characters are in it. */
477
478static long
ee2fb9eb 479get_char (FILE *stream, file_ptr *address, int *magiccount, char **magic)
d132876a
NC
480{
481 int c, i;
c54e2ec1 482 long r = 0;
d132876a
NC
483
484 for (i = 0; i < encoding_bytes; i++)
485 {
486 if (*magiccount)
487 {
488 (*magiccount)--;
489 c = *(*magic)++;
490 }
491 else
492 {
493 if (stream == NULL)
494 return EOF;
b7d4af3a
JW
495
496 /* Only use getc_unlocked if we found a declaration for it.
497 Otherwise, libc is not thread safe by default, and we
498 should not use it. */
499
500#if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
cedd9a58
JJ
501 c = getc_unlocked (stream);
502#else
d132876a 503 c = getc (stream);
cedd9a58 504#endif
d132876a
NC
505 if (c == EOF)
506 return EOF;
507 }
508
509 (*address)++;
c54e2ec1 510 r = (r << 8) | (c & 0xff);
d132876a
NC
511 }
512
513 switch (encoding)
514 {
c54e2ec1 515 default:
d132876a
NC
516 break;
517 case 'l':
c54e2ec1 518 r = ((r & 0xff) << 8) | ((r & 0xff00) >> 8);
d132876a
NC
519 break;
520 case 'L':
c54e2ec1
AM
521 r = (((r & 0xff) << 24) | ((r & 0xff00) << 8)
522 | ((r & 0xff0000) >> 8) | ((r & 0xff000000) >> 24));
d132876a
NC
523 break;
524 }
525
d132876a
NC
526 return r;
527}
528\f
252b5132
RH
529/* Find the strings in file FILENAME, read from STREAM.
530 Assume that STREAM is positioned so that the next byte read
531 is at address ADDRESS in the file.
532 Stop reading at address STOP_POINT in the file, if nonzero.
533
534 If STREAM is NULL, do not read from it.
535 The caller can supply a buffer of characters
536 to be processed before the data in STREAM.
537 MAGIC is the address of the buffer and
538 MAGICCOUNT is how many characters are in it.
539 Those characters come at address ADDRESS and the data in STREAM follow. */
540
541static void
ee2fb9eb 542print_strings (const char *filename, FILE *stream, file_ptr address,
2da42df6 543 int stop_point, int magiccount, char *magic)
252b5132 544{
d132876a 545 char *buf = (char *) xmalloc (sizeof (char) * (string_min + 1));
252b5132
RH
546
547 while (1)
548 {
ee2fb9eb 549 file_ptr start;
252b5132 550 int i;
d132876a 551 long c;
252b5132
RH
552
553 /* See if the next `string_min' chars are all graphic chars. */
554 tryline:
555 if (stop_point && address >= stop_point)
556 break;
557 start = address;
558 for (i = 0; i < string_min; i++)
559 {
d132876a
NC
560 c = get_char (stream, &address, &magiccount, &magic);
561 if (c == EOF)
68187828
NC
562 {
563 free (buf);
564 return;
565 }
8745eafa 566 if (! STRING_ISGRAPHIC (c))
252b5132
RH
567 /* Found a non-graphic. Try again starting with next char. */
568 goto tryline;
569 buf[i] = c;
570 }
571
572 /* We found a run of `string_min' graphic characters. Print up
e9f87780 573 to the next non-graphic character. */
252b5132
RH
574
575 if (print_filenames)
576 printf ("%s: ", filename);
577 if (print_addresses)
578 switch (address_radix)
579 {
580 case 8:
cedd9a58
JJ
581#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
582 if (sizeof (start) > sizeof (long))
6e3d6dc1
NC
583 {
584#ifndef __MSVCRT__
585 printf ("%7llo ", (unsigned long long) start);
586#else
587 printf ("%7I64o ", (unsigned long long) start);
588#endif
589 }
cedd9a58 590 else
50e3244d 591#elif !BFD_HOST_64BIT_LONG
cedd9a58
JJ
592 if (start != (unsigned long) start)
593 printf ("++%7lo ", (unsigned long) start);
594 else
cedd9a58
JJ
595#endif
596 printf ("%7lo ", (unsigned long) start);
252b5132
RH
597 break;
598
599 case 10:
cedd9a58
JJ
600#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
601 if (sizeof (start) > sizeof (long))
6e3d6dc1
NC
602 {
603#ifndef __MSVCRT__
604 printf ("%7lld ", (unsigned long long) start);
605#else
606 printf ("%7I64d ", (unsigned long long) start);
607#endif
608 }
cedd9a58 609 else
50e3244d 610#elif !BFD_HOST_64BIT_LONG
cedd9a58
JJ
611 if (start != (unsigned long) start)
612 printf ("++%7ld ", (unsigned long) start);
613 else
cedd9a58
JJ
614#endif
615 printf ("%7ld ", (long) start);
252b5132
RH
616 break;
617
618 case 16:
cedd9a58
JJ
619#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
620 if (sizeof (start) > sizeof (long))
6e3d6dc1
NC
621 {
622#ifndef __MSVCRT__
623 printf ("%7llx ", (unsigned long long) start);
624#else
625 printf ("%7I64x ", (unsigned long long) start);
626#endif
627 }
cedd9a58 628 else
50e3244d 629#elif !BFD_HOST_64BIT_LONG
cedd9a58 630 if (start != (unsigned long) start)
e9f87780
AM
631 printf ("%lx%8.8lx ", (unsigned long) (start >> 32),
632 (unsigned long) (start & 0xffffffff));
cedd9a58 633 else
cedd9a58
JJ
634#endif
635 printf ("%7lx ", (unsigned long) start);
252b5132
RH
636 break;
637 }
638
639 buf[i] = '\0';
640 fputs (buf, stdout);
641
642 while (1)
643 {
d132876a
NC
644 c = get_char (stream, &address, &magiccount, &magic);
645 if (c == EOF)
646 break;
8745eafa 647 if (! STRING_ISGRAPHIC (c))
252b5132
RH
648 break;
649 putchar (c);
650 }
651
652 putchar ('\n');
653 }
68187828 654 free (buf);
252b5132
RH
655}
656\f
252b5132 657static void
2da42df6 658usage (FILE *stream, int status)
252b5132 659{
8b53311e
NC
660 fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
661 fprintf (stream, _(" Display printable strings in [file(s)] (stdin by default)\n"));
7fac9594
NC
662 fprintf (stream, _(" The options are:\n"));
663
664 if (DEFAULT_STRINGS_ALL)
665 fprintf (stream, _("\
666 -a - --all Scan the entire file, not just the data section [default]\n\
667 -d --data Only scan the data sections in the file\n"));
668 else
669 fprintf (stream, _("\
8b53311e 670 -a - --all Scan the entire file, not just the data section\n\
7fac9594
NC
671 -d --data Only scan the data sections in the file [default]\n"));
672
673 fprintf (stream, _("\
8b53311e
NC
674 -f --print-file-name Print the name of the file before each string\n\
675 -n --bytes=[number] Locate & print any NUL-terminated sequence of at\n\
c904a764 676 -<number> least [number] characters (default 4).\n\
d412a550 677 -t --radix={o,d,x} Print the location of the string in base 8, 10 or 16\n\
334ac421 678 -w --include-all-whitespace Include all whitespace as valid string characters\n\
8b53311e
NC
679 -o An alias for --radix=o\n\
680 -T --target=<BFDNAME> Specify the binary file format\n\
8745eafa
NC
681 -e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
682 s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
07012eee 683 @<file> Read options from <file>\n\
8b53311e 684 -h --help Display this information\n\
ffbe5983 685 -v -V --version Print the program's version number\n"));
252b5132 686 list_supported_targets (program_name, stream);
92f01d61 687 if (REPORT_BUGS_TO[0] && status == 0)
8ad3436c 688 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132
RH
689 exit (status);
690}
This page took 0.611299 seconds and 4 git commands to generate.