1 /* strings -- print the strings of printable characters in files
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004 Free Software Foundation, Inc.
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)
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.
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
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 /* Usage: strings [options] file...
25 - Do not scan only the initialized data section of object files.
28 -f Print the name of the file before each string.
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.
36 -t {o,x,d} Print the offset within the file before each string,
39 -o Like -to. (Some other implementations have -o like -to,
40 others like -td. We chose one arbitrarily.)
42 --encoding={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,
49 Specify a non-default object file format.
52 -h Print the usage message on the standard output.
55 -v Print the program version number.
57 Written by Richard Stallman <rms@gnu.ai.mit.edu>
58 and David MacKenzie <djm@gnu.ai.mit.edu>. */
68 #include "libiberty.h"
69 #include "safe-ctype.h"
71 /* Some platforms need to put stdin into binary mode, to read
76 #define O_BINARY _O_BINARY
77 #define setmode _setmode
84 #define SET_BINARY(f) do { if (!isatty (f)) setmode (f,O_BINARY); } while (0)
88 #define STRING_ISGRAPHIC(c) \
91 && ((c) == '\t' || ISPRINT (c) || (encoding == 'S' && (c) > 127)))
97 /* The BFD section flags that identify an initialized data section. */
98 #define DATA_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)
101 typedef off64_t file_off
;
102 #define file_open(s,m) fopen64(s, m)
104 typedef off_t file_off
;
105 #define file_open(s,m) fopen(s, m)
108 /* Radix for printing addresses (must be 8, 10 or 16). */
109 static int address_radix
;
111 /* Minimum length of sequence of graphic chars to trigger output. */
112 static int string_min
;
114 /* TRUE means print address within file for each string. */
115 static bfd_boolean print_addresses
;
117 /* TRUE means print filename for each string. */
118 static bfd_boolean print_filenames
;
120 /* TRUE means for object files scan only the data section. */
121 static bfd_boolean datasection_only
;
123 /* TRUE if we found an initialized data section in the current file. */
124 static bfd_boolean got_a_section
;
126 /* The BFD object file format. */
129 /* The character encoding format. */
130 static char encoding
;
131 static int encoding_bytes
;
133 static struct option long_options
[] =
135 {"all", no_argument
, NULL
, 'a'},
136 {"print-file-name", no_argument
, NULL
, 'f'},
137 {"bytes", required_argument
, NULL
, 'n'},
138 {"radix", required_argument
, NULL
, 't'},
139 {"encoding", required_argument
, NULL
, 'e'},
140 {"target", required_argument
, NULL
, 'T'},
141 {"help", no_argument
, NULL
, 'h'},
142 {"version", no_argument
, NULL
, 'v'},
146 static void strings_a_section (bfd
*, asection
*, void *);
147 static bfd_boolean
strings_object_file (const char *);
148 static bfd_boolean
strings_file (char *file
);
149 static int integer_arg (char *s
);
150 static void print_strings (const char *, FILE *, file_off
, int, int, char *);
151 static void usage (FILE *, int);
152 static long get_char (FILE *, file_off
*, int *, char **);
154 int main (int, char **);
157 main (int argc
, char **argv
)
161 bfd_boolean files_given
= FALSE
;
163 #if defined (HAVE_SETLOCALE)
164 setlocale (LC_ALL
, "");
166 bindtextdomain (PACKAGE
, LOCALEDIR
);
167 textdomain (PACKAGE
);
169 program_name
= argv
[0];
170 xmalloc_set_program_name (program_name
);
172 print_addresses
= FALSE
;
173 print_filenames
= FALSE
;
174 datasection_only
= TRUE
;
178 while ((optc
= getopt_long (argc
, argv
, "afhHn:ot:e:Vv0123456789",
179 long_options
, (int *) 0)) != EOF
)
184 datasection_only
= FALSE
;
188 print_filenames
= TRUE
;
196 string_min
= integer_arg (optarg
);
198 fatal (_("invalid number %s"), optarg
);
202 print_addresses
= TRUE
;
207 print_addresses
= TRUE
;
208 if (optarg
[1] != '\0')
234 if (optarg
[1] != '\0')
236 encoding
= optarg
[0];
241 print_version ("strings");
249 string_min
= optc
- '0';
251 string_min
= string_min
* 10 + optc
- '0';
278 set_default_bfd_target ();
282 datasection_only
= FALSE
;
284 SET_BINARY (fileno (stdin
));
286 print_strings ("{standard input}", stdin
, 0, 0, 0, (char *) NULL
);
291 for (; optind
< argc
; ++optind
)
293 if (strcmp (argv
[optind
], "-") == 0)
294 datasection_only
= FALSE
;
298 exit_status
|= strings_file (argv
[optind
]) == FALSE
;
306 return (exit_status
);
309 /* Scan section SECT of the file ABFD, whose printable name is FILE.
310 If it contains initialized data,
311 set `got_a_section' and print the strings in it. */
314 strings_a_section (bfd
*abfd
, asection
*sect
, void *filearg
)
316 const char *file
= (const char *) filearg
;
318 if ((sect
->flags
& DATA_FLAGS
) == DATA_FLAGS
)
320 bfd_size_type sz
= bfd_get_section_size (sect
);
321 void *mem
= xmalloc (sz
);
323 if (bfd_get_section_contents (abfd
, sect
, mem
, (file_ptr
) 0, sz
))
325 got_a_section
= TRUE
;
326 print_strings (file
, (FILE *) NULL
, sect
->filepos
, 0, sz
, mem
);
332 /* Scan all of the sections in FILE, and print the strings
333 in the initialized data section(s).
335 Return TRUE if successful,
336 FALSE if not (such as if FILE is not an object file). */
339 strings_object_file (const char *file
)
341 bfd
*abfd
= bfd_openr (file
, target
);
344 /* Treat the file as a non-object file. */
347 /* This call is mainly for its side effect of reading in the sections.
348 We follow the traditional behavior of `strings' in that we don't
349 complain if we don't recognize a file to be an object file. */
350 if (!bfd_check_format (abfd
, bfd_object
))
356 got_a_section
= FALSE
;
357 bfd_map_over_sections (abfd
, strings_a_section
, (void *) file
);
359 if (!bfd_close (abfd
))
365 return got_a_section
;
368 /* Print the strings in FILE. Return TRUE if ok, FALSE if an error occurs. */
371 strings_file (char *file
)
373 if (get_file_size (file
) < 1)
376 /* If we weren't told to scan the whole file,
377 try to open it as an object file and only look at
378 initialized data sections. If that fails, fall back to the
380 if (!datasection_only
|| !strings_object_file (file
))
384 stream
= file_open (file
, FOPEN_RB
);
387 fprintf (stderr
, "%s: ", program_name
);
392 print_strings (file
, stream
, (file_off
) 0, 0, 0, (char *) 0);
394 if (fclose (stream
) == EOF
)
396 fprintf (stderr
, "%s: ", program_name
);
405 /* Read the next character, return EOF if none available.
406 Assume that STREAM is positioned so that the next byte read
407 is at address ADDRESS in the file.
409 If STREAM is NULL, do not read from it.
410 The caller can supply a buffer of characters
411 to be processed before the data in STREAM.
412 MAGIC is the address of the buffer and
413 MAGICCOUNT is how many characters are in it. */
416 get_char (FILE *stream
, file_off
*address
, int *magiccount
, char **magic
)
420 unsigned char buf
[4];
422 for (i
= 0; i
< encoding_bytes
; i
++)
433 #ifdef HAVE_GETC_UNLOCKED
434 c
= getc_unlocked (stream
);
453 r
= (buf
[0] << 8) | buf
[1];
456 r
= buf
[0] | (buf
[1] << 8);
459 r
= ((long) buf
[0] << 24) | ((long) buf
[1] << 16) |
460 ((long) buf
[2] << 8) | buf
[3];
463 r
= buf
[0] | ((long) buf
[1] << 8) | ((long) buf
[2] << 16) |
464 ((long) buf
[3] << 24);
474 /* Find the strings in file FILENAME, read from STREAM.
475 Assume that STREAM is positioned so that the next byte read
476 is at address ADDRESS in the file.
477 Stop reading at address STOP_POINT in the file, if nonzero.
479 If STREAM is NULL, do not read from it.
480 The caller can supply a buffer of characters
481 to be processed before the data in STREAM.
482 MAGIC is the address of the buffer and
483 MAGICCOUNT is how many characters are in it.
484 Those characters come at address ADDRESS and the data in STREAM follow. */
487 print_strings (const char *filename
, FILE *stream
, file_off address
,
488 int stop_point
, int magiccount
, char *magic
)
490 char *buf
= (char *) xmalloc (sizeof (char) * (string_min
+ 1));
498 /* See if the next `string_min' chars are all graphic chars. */
500 if (stop_point
&& address
>= stop_point
)
503 for (i
= 0; i
< string_min
; i
++)
505 c
= get_char (stream
, &address
, &magiccount
, &magic
);
508 if (! STRING_ISGRAPHIC (c
))
509 /* Found a non-graphic. Try again starting with next char. */
514 /* We found a run of `string_min' graphic characters. Print up
515 to the next non-graphic character. */
518 printf ("%s: ", filename
);
520 switch (address_radix
)
523 #if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
524 if (sizeof (start
) > sizeof (long))
525 printf ("%7Lo ", (unsigned long long) start
);
528 # if !BFD_HOST_64BIT_LONG
529 if (start
!= (unsigned long) start
)
530 printf ("++%7lo ", (unsigned long) start
);
534 printf ("%7lo ", (unsigned long) start
);
538 #if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
539 if (sizeof (start
) > sizeof (long))
540 printf ("%7Ld ", (unsigned long long) start
);
543 # if !BFD_HOST_64BIT_LONG
544 if (start
!= (unsigned long) start
)
545 printf ("++%7ld ", (unsigned long) start
);
549 printf ("%7ld ", (long) start
);
553 #if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
554 if (sizeof (start
) > sizeof (long))
555 printf ("%7Lx ", (unsigned long long) start
);
558 # if !BFD_HOST_64BIT_LONG
559 if (start
!= (unsigned long) start
)
560 printf ("%lx%8.8lx ", (unsigned long) (start
>> 32),
561 (unsigned long) (start
& 0xffffffff));
565 printf ("%7lx ", (unsigned long) start
);
574 c
= get_char (stream
, &address
, &magiccount
, &magic
);
577 if (! STRING_ISGRAPHIC (c
))
586 /* Parse string S as an integer, using decimal radix by default,
587 but allowing octal and hex numbers as in C. */
590 integer_arg (char *s
)
599 else if (*++p
== 'x')
608 while (((c
= *p
++) >= '0' && c
<= '9')
609 || (radix
== 16 && (c
& ~40) >= 'A' && (c
& ~40) <= 'Z'))
612 if (c
>= '0' && c
<= '9')
615 value
+= (c
& ~40) - 'A';
626 fatal (_("invalid integer argument %s"), s
);
632 usage (FILE *stream
, int status
)
634 fprintf (stream
, _("Usage: %s [option(s)] [file(s)]\n"), program_name
);
635 fprintf (stream
, _(" Display printable strings in [file(s)] (stdin by default)\n"));
636 fprintf (stream
, _(" The options are:\n\
637 -a - --all Scan the entire file, not just the data section\n\
638 -f --print-file-name Print the name of the file before each string\n\
639 -n --bytes=[number] Locate & print any NUL-terminated sequence of at\n\
640 -<number> least [number] characters (default 4).\n\
641 -t --radix={o,x,d} Print the location of the string in base 8, 10 or 16\n\
642 -o An alias for --radix=o\n\
643 -T --target=<BFDNAME> Specify the binary file format\n\
644 -e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
645 s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
646 -h --help Display this information\n\
647 -v --version Print the program's version number\n"));
648 list_supported_targets (program_name
, stream
);
650 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);