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