]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - elf/readlib.c
Fix bad pointer / leak in regex code
[thirdparty/glibc.git] / elf / readlib.c
index 8df63a44a35d31049cc22f254544f776e88abb67..c9743e66929d49594dae9c870d04dfb4af7422fe 100644 (file)
@@ -1,11 +1,12 @@
-/* Copyright (C) 1999-2003, 2005 Free Software Foundation, Inc.
+/* Copyright (C) 1999-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Andreas Jaeger <aj@suse.de>, 1999 and
                  Jakub Jelinek <jakub@redhat.com>, 1999.
 
    This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License version 2 as
-   published by the Free Software Foundation.
+   it under the terms of the GNU General Public License as published
+   by the Free Software Foundation; version 2 of the License, or
+   (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -13,8 +14,7 @@
    GNU General Public License for more details.
 
    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.  */
+   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
 
 /* The code in this file and in readelflib is a heavily simplified
    version of the readelf program that's part of the current binutils
@@ -34,7 +34,7 @@
 #include <sys/stat.h>
 #include <gnu/lib-names.h>
 
-#include "ldconfig.h"
+#include <ldconfig.h>
 
 #define Elf32_CLASS ELFCLASS32
 #define Elf64_CLASS ELFCLASS64
@@ -63,12 +63,19 @@ static struct known_names known_libs[] =
 };
 
 
+/* Check if string corresponds to a GDB Python file.  */
+static bool
+is_gdb_python_file (const char *name)
+{
+  size_t len = strlen (name);
+  return len > 7 && strcmp (name + len - 7, "-gdb.py") == 0;
+}
 
 /* Returns 0 if everything is ok, != 0 in case of error.  */
 int
 process_file (const char *real_file_name, const char *file_name,
              const char *lib, int *flag, unsigned int *osversion,
-             char **soname, int is_link)
+             char **soname, int is_link, struct stat64 *stat_buf)
 {
   FILE *file;
   struct stat64 statbuf;
@@ -134,7 +141,7 @@ process_file (const char *real_file_name, const char *file_name,
       )
     {
       /* Aout files don't have a soname, just return the name
-         including the major number.  */
+        including the major number.  */
       char *copy, *major, *dot;
       copy = xstrdup (lib);
       major = strstr (copy, ".so.");
@@ -157,7 +164,8 @@ process_file (const char *real_file_name, const char *file_name,
         beginning of the file.  */
       size_t len = MIN (statbuf.st_size, 512);
       if (memmem (file_contents, len, "GROUP", 5) == NULL
-         && memmem (file_contents, len, "GNU ld script", 13) == NULL)
+         && memmem (file_contents, len, "GNU ld script", 13) == NULL
+         && !is_gdb_python_file (file_name))
        error (0, 0, _("%s is not an ELF file - it has the wrong magic bytes at the start.\n"),
               file_name);
       ret = 1;
@@ -174,8 +182,31 @@ process_file (const char *real_file_name, const char *file_name,
   munmap (file_contents, statbuf.st_size);
   fclose (file);
 
+  *stat_buf = statbuf;
   return ret;
 }
 
+/* Returns made up soname if lib doesn't have explicit DT_SONAME.  */
+
+char *
+implicit_soname (const char *lib, int flag)
+{
+  char *soname = xstrdup (lib);
+
+  if ((flag & FLAG_TYPE_MASK) != FLAG_LIBC4)
+    return soname;
+
+  /* Aout files don't have a soname, just return the name
+     including the major number.  */
+  char *major = strstr (soname, ".so.");
+  if (major)
+    {
+      char *dot = strstr (major + 4, ".");
+      if (dot)
+       *dot = '\0';
+    }
+  return soname;
+}
+
 /* Get architecture specific version of process_elf_file.  */
 #include <readelflib.c>