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