]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/readlib.c
Update.
[thirdparty/glibc.git] / elf / readlib.c
CommitLineData
e7c036b3 1/* Copyright (C) 1999, 2000 Free Software Foundation, Inc.
591e1ffb
UD
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.
5
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.
10
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.
15
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. */
20
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. */
25
26
591e1ffb
UD
27#include <elf.h>
28#include <error.h>
29#include <link.h>
30#include <libintl.h>
31#include <stdio.h>
32#include <string.h>
33#include <unistd.h>
af1680f1 34#include <a.out.h>
591e1ffb
UD
35
36#include <sys/mman.h>
37#include <sys/stat.h>
dc95d158 38#include <gnu/lib-names.h>
591e1ffb
UD
39
40#include "ldconfig.h"
41
42#define Elf32_CLASS ELFCLASS32
43#define Elf64_CLASS ELFCLASS64
44
45struct known_names
46{
47 const char *soname;
48 int flag;
49};
50
51static struct known_names interpreters [] =
52{
9c145d90 53 {"/lib/" LD_SO, FLAG_ELF_LIBC6},
591e1ffb
UD
54 {"/lib/ld-linux.so.1", FLAG_ELF_LIBC5}
55};
56
57static struct known_names known_libs [] =
58{
dc95d158 59 /* Old names: */
591e1ffb 60 {"libc.so.5", FLAG_ELF_LIBC5},
dc95d158
AJ
61 {"libm.so.5", FLAG_ELF_LIBC5},
62 /* Current names: */
63 {LIBC_SO, FLAG_ELF_LIBC6},
64 {LIBM_SO, FLAG_ELF_LIBC6}
591e1ffb
UD
65};
66
67
68
69/* Returns 0 if everything is ok, != 0 in case of error. */
70int
b4a555d6
UD
71process_file (const char *real_file_name, const char *file_name,
72 const char *lib, int *flag, char **soname, int is_link)
591e1ffb
UD
73{
74 FILE *file;
b4a555d6 75 struct stat64 statbuf;
591e1ffb
UD
76 void *file_contents;
77 int ret;
af1680f1 78
591e1ffb
UD
79 ElfW(Ehdr) *elf_header;
80 struct exec *aout_header;
81
82 ret = 0;
83 *flag = FLAG_ANY;
84 *soname = NULL;
85
b4a555d6 86 file = fopen (real_file_name, "rb");
591e1ffb
UD
87 if (file == NULL)
88 {
89 /* No error for stale symlink. */
7ad9abc0 90 if (is_link && strstr (file_name, ".so") != NULL)
591e1ffb
UD
91 return 1;
92 error (0, 0, _("Input file %s not found.\n"), file_name);
93 return 1;
94 }
95
b4a555d6 96 if (fstat64 (fileno (file), &statbuf) < 0)
591e1ffb
UD
97 {
98 error (0, 0, _("Cannot fstat file %s.\n"), file_name);
e7c036b3
UD
99 fclose (file);
100 return 1;
101 }
102
103 /* Check that the file is large enough so that we can access the
104 information. We're only checking the size of the headers here. */
105 if (statbuf.st_size < sizeof (struct exec)
106 || statbuf.st_size < sizeof (ElfW(Ehdr)))
107 {
108 error (0, 0, _("File %s is too small, not checked."), file_name);
109 fclose (file);
591e1ffb
UD
110 return 1;
111 }
112
e7c036b3
UD
113 file_contents = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED,
114 fileno (file), 0);
591e1ffb
UD
115 if (file_contents == MAP_FAILED)
116 {
117 error (0, 0, _("Cannot mmap file %s.\n"), file_name);
118 fclose (file);
119 return 1;
120 }
121
122 /* First check if this is an aout file. */
123 aout_header = (struct exec *) file_contents;
124 if (N_MAGIC (*aout_header) == ZMAGIC
125 || N_MAGIC (*aout_header) == QMAGIC)
126 {
e7c036b3 127 /* Aout files don't have a soname, just return the name
591e1ffb
UD
128 including the major number. */
129 char *copy, *major, *dot;
130 copy = xstrdup (lib);
131 major = strstr (copy, ".so.");
132 if (major)
133 {
134 dot = strstr (major + 4, ".");
135 if (dot)
136 *dot = '\0';
137 }
138 *soname = copy;
139 *flag = FLAG_LIBC4;
140 goto done;
141 }
af1680f1 142
591e1ffb
UD
143 elf_header = (ElfW(Ehdr) *) file_contents;
144 if (elf_header->e_ident [EI_MAG0] != ELFMAG0
145 || elf_header->e_ident [EI_MAG1] != ELFMAG1
146 || elf_header->e_ident [EI_MAG2] != ELFMAG2
147 || elf_header->e_ident [EI_MAG3] != ELFMAG3)
148 {
149 /* The file is neither ELF nor aout. Check if it's a linker script,
150 like libc.so - otherwise complain. */
151 int len = statbuf.st_size;
152 /* Only search the beginning of the file. */
153 if (len > 512)
154 len = 512;
155 if (memmem (file_contents, len, "GROUP", 5) == NULL
156 && memmem (file_contents, len, "GNU ld script", 13) == NULL)
157 error (0, 0, _("%s is not an ELF file - it has the wrong magic bytes at the start.\n"),
158 file_name);
159 ret = 1;
160 goto done;
161 }
162
dc95d158
AJ
163 if (process_elf_file (file_name, lib, flag, soname, file_contents,
164 statbuf.st_size))
591e1ffb
UD
165 ret = 1;
166
167 done:
168 /* Clean up allocated memory and resources. */
169 munmap (file_contents, statbuf.st_size);
170 fclose (file);
171
172 return ret;
173}
174
175/* Get architecture specific version of process_elf_file. */
176#include "readelflib.c"