/* strings -- print the strings of printable characters in files
Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
- 2002, 2003, 2004 Free Software Foundation, Inc.
+ 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2, or (at your option)
+ the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- 02111-1307, USA. */
+ Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
+ 02110-1301, USA. */
\f
/* Usage: strings [options] file...
littleendian 32-bit.
--target=BFDNAME
+ -T {bfdname}
Specify a non-default object file format.
--help
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
+#include "sysdep.h"
#include "bfd.h"
-#include <stdio.h>
#include "getopt.h"
-#include <errno.h>
-#include "bucomm.h"
#include "libiberty.h"
#include "safe-ctype.h"
#include <sys/stat.h>
+#include "bucomm.h"
/* Some platforms need to put stdin into binary mode, to read
binary files. */
{NULL, 0, NULL, 0}
};
+/* Records the size of a named file so that we
+ do not repeatedly run bfd_stat() on it. */
+
+typedef struct
+{
+ const char * filename;
+ bfd_size_type filesize;
+} filename_and_size_t;
+
static void strings_a_section (bfd *, asection *, void *);
static bfd_boolean strings_object_file (const char *);
static bfd_boolean strings_file (char *file);
program_name = argv[0];
xmalloc_set_program_name (program_name);
+
+ expandargv (&argc, &argv);
+
string_min = -1;
print_addresses = FALSE;
print_filenames = FALSE;
target = NULL;
encoding = 's';
- while ((optc = getopt_long (argc, argv, "afhHn:ot:e:Vv0123456789",
+ while ((optc = getopt_long (argc, argv, "afhHn:ot:e:T:Vv0123456789",
long_options, (int *) 0)) != EOF)
{
switch (optc)
return (exit_status);
}
\f
-/* Scan section SECT of the file ABFD, whose printable name is FILE.
- If it contains initialized data,
- set `got_a_section' and print the strings in it. */
+/* Scan section SECT of the file ABFD, whose printable name is in
+ ARG->filename and whose size might be in ARG->filesize. If it
+ contains initialized data set `got_a_section' and print the
+ strings in it.
+
+ FIXME: We ought to be able to return error codes/messages for
+ certain conditions. */
static void
-strings_a_section (bfd *abfd, asection *sect, void *filearg)
+strings_a_section (bfd *abfd, asection *sect, void *arg)
{
- const char *file = (const char *) filearg;
+ filename_and_size_t * filename_and_sizep;
+ bfd_size_type *filesizep;
+ bfd_size_type sectsize;
+ void *mem;
+
+ if ((sect->flags & DATA_FLAGS) != DATA_FLAGS)
+ return;
+
+ sectsize = bfd_get_section_size (sect);
+
+ if (sectsize <= 0)
+ return;
+
+ /* Get the size of the file. This might have been cached for us. */
+ filename_and_sizep = (filename_and_size_t *) arg;
+ filesizep = & filename_and_sizep->filesize;
+
+ if (*filesizep == 0)
+ {
+ struct stat st;
+
+ if (bfd_stat (abfd, &st))
+ return;
- if ((sect->flags & DATA_FLAGS) == DATA_FLAGS)
+ /* Cache the result so that we do not repeatedly stat this file. */
+ *filesizep = st.st_size;
+ }
+
+ /* Compare the size of the section against the size of the file.
+ If the section is bigger then the file must be corrupt and
+ we should not try dumping it. */
+ if (sectsize >= *filesizep)
+ return;
+
+ mem = xmalloc (sectsize);
+
+ if (bfd_get_section_contents (abfd, sect, mem, (file_ptr) 0, sectsize))
{
- bfd_size_type sz = bfd_get_section_size (sect);
- void *mem = xmalloc (sz);
+ got_a_section = TRUE;
- if (bfd_get_section_contents (abfd, sect, mem, (file_ptr) 0, sz))
- {
- got_a_section = TRUE;
- print_strings (file, (FILE *) NULL, sect->filepos, 0, sz, mem);
- }
- free (mem);
+ print_strings (filename_and_sizep->filename, NULL, sect->filepos,
+ 0, sectsize, mem);
}
+
+ free (mem);
}
/* Scan all of the sections in FILE, and print the strings
static bfd_boolean
strings_object_file (const char *file)
{
- bfd *abfd = bfd_openr (file, target);
+ filename_and_size_t filename_and_size;
+ bfd *abfd;
+
+ abfd = bfd_openr (file, target);
if (abfd == NULL)
/* Treat the file as a non-object file. */
}
got_a_section = FALSE;
- bfd_map_over_sections (abfd, strings_a_section, (void *) file);
+ filename_and_size.filename = file;
+ filename_and_size.filesize = 0;
+ bfd_map_over_sections (abfd, strings_a_section, & filename_and_size);
if (!bfd_close (abfd))
{
{
if (stream == NULL)
return EOF;
-#ifdef HAVE_GETC_UNLOCKED
+
+ /* Only use getc_unlocked if we found a declaration for it.
+ Otherwise, libc is not thread safe by default, and we
+ should not use it. */
+
+#if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
c = getc_unlocked (stream);
#else
c = getc (stream);
-f --print-file-name Print the name of the file before each string\n\
-n --bytes=[number] Locate & print any NUL-terminated sequence of at\n\
-<number> least [number] characters (default 4).\n\
- -t --radix={o,x,d} Print the location of the string in base 8, 10 or 16\n\
+ -t --radix={o,d,x} Print the location of the string in base 8, 10 or 16\n\
-o An alias for --radix=o\n\
-T --target=<BFDNAME> Specify the binary file format\n\
-e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
+ @<file> Read options from <file>\n\
-h --help Display this information\n\
-v --version Print the program's version number\n"));
list_supported_targets (program_name, stream);
- if (status == 0)
+ if (REPORT_BUGS_TO[0] && status == 0)
fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
exit (status);
}