Update.
[glibc.git] / elf / readlib.c
blobe6d061909112da48d9733d8a0d575c81fcaa93f5
1 /* Copyright (C) 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@suse.de>, 1999 and
4 Jakub Jelinek <jakub@redhat.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* The code in this file and in readelflib is a heavily simplified
22 version of the readelf program that's part of the current binutils
23 development version. Besides the simplification, it has also been
24 modified to read some other file formats. */
27 #include <a.out.h>
28 #include <elf.h>
29 #include <error.h>
30 #include <link.h>
31 #include <libintl.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include <sys/mman.h>
37 #include <sys/stat.h>
39 #include "ldconfig.h"
41 #define Elf32_CLASS ELFCLASS32
42 #define Elf64_CLASS ELFCLASS64
44 struct known_names
46 const char *soname;
47 int flag;
50 static struct known_names interpreters [] =
52 {"/lib/ld-linux.so.2", FLAG_ELF_LIBC6},
53 {"/lib/ld-linux.so.1", FLAG_ELF_LIBC5}
56 static struct known_names known_libs [] =
58 {"libc.so.6", FLAG_ELF_LIBC6},
59 {"libc.so.5", FLAG_ELF_LIBC5},
60 {"libm.so.6", FLAG_ELF_LIBC6},
61 {"libm.so.5", FLAG_ELF_LIBC5}
66 /* Returns 0 if everything is ok, != 0 in case of error. */
67 int
68 process_file (const char *file_name, const char *lib, int *flag, char **soname, int is_link)
70 FILE *file;
71 struct stat statbuf;
72 void *file_contents;
73 int ret;
75 ElfW(Ehdr) *elf_header;
76 struct exec *aout_header;
78 ret = 0;
79 *flag = FLAG_ANY;
80 *soname = NULL;
82 file = fopen (file_name, "rb");
83 if (file == NULL)
85 /* No error for stale symlink. */
86 if (is_link && strstr (file_name, ".so.") != NULL)
87 return 1;
88 error (0, 0, _("Input file %s not found.\n"), file_name);
89 return 1;
92 if (fstat (fileno (file), &statbuf) < 0)
94 error (0, 0, _("Cannot fstat file %s.\n"), file_name);
95 return 1;
98 file_contents = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, fileno (file), 0);
99 if (file_contents == MAP_FAILED)
101 error (0, 0, _("Cannot mmap file %s.\n"), file_name);
102 fclose (file);
103 return 1;
106 /* First check if this is an aout file. */
107 aout_header = (struct exec *) file_contents;
108 if (N_MAGIC (*aout_header) == ZMAGIC
109 || N_MAGIC (*aout_header) == QMAGIC)
111 /* Aout files don't have a soname, just return the the name
112 including the major number. */
113 char *copy, *major, *dot;
114 copy = xstrdup (lib);
115 major = strstr (copy, ".so.");
116 if (major)
118 dot = strstr (major + 4, ".");
119 if (dot)
120 *dot = '\0';
122 *soname = copy;
123 *flag = FLAG_LIBC4;
124 goto done;
127 elf_header = (ElfW(Ehdr) *) file_contents;
128 if (elf_header->e_ident [EI_MAG0] != ELFMAG0
129 || elf_header->e_ident [EI_MAG1] != ELFMAG1
130 || elf_header->e_ident [EI_MAG2] != ELFMAG2
131 || elf_header->e_ident [EI_MAG3] != ELFMAG3)
133 /* The file is neither ELF nor aout. Check if it's a linker script,
134 like libc.so - otherwise complain. */
135 int len = statbuf.st_size;
136 /* Only search the beginning of the file. */
137 if (len > 512)
138 len = 512;
139 if (memmem (file_contents, len, "GROUP", 5) == NULL
140 && memmem (file_contents, len, "GNU ld script", 13) == NULL)
141 error (0, 0, _("%s is not an ELF file - it has the wrong magic bytes at the start.\n"),
142 file_name);
143 ret = 1;
144 goto done;
147 if (process_elf_file (file_name, lib, flag, soname, file_contents))
148 ret = 1;
150 done:
151 /* Clean up allocated memory and resources. */
152 munmap (file_contents, statbuf.st_size);
153 fclose (file);
155 return ret;
158 /* Get architecture specific version of process_elf_file. */
159 #include "readelflib.c"