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 /* This code is a heavily simplified version of the readelf program
22 that's part of the current binutils development version. For architectures
23 which need to handle both 32bit and 64bit ELF libraries, this file is
24 included twice for each arch size. */
26 /* Returns 0 if everything is ok, != 0 in case of error. */
28 process_elf_file (const char *file_name
, const char *lib
, int *flag
, char **soname
,
34 unsigned int dynamic_addr
;
36 char *program_interpreter
;
38 ElfW(Ehdr
) *elf_header
;
39 ElfW(Phdr
) *elf_pheader
, *segment
;
40 ElfW(Dyn
) *dynamic_segment
, *dyn_entry
;
41 char *dynamic_strings
;
43 elf_header
= (ElfW(Ehdr
) *) file_contents
;
45 if (elf_header
->e_ident
[EI_CLASS
] != ElfW (CLASS
))
49 if (ElfW (CLASS
) == ELFCLASS32
)
50 error (0, 0, _("%s is a 32 bit ELF file.\n"), file_name
);
51 else if (ElfW (CLASS
) == ELFCLASS64
)
52 error (0, 0, _("%s is a 64 bit ELF file.\n"), file_name
);
54 error (0, 0, _("Unknown ELFCLASS in file %s.\n"), file_name
);
59 if (elf_header
->e_type
!= ET_DYN
)
61 error (0, 0, _("%s is not a shared object file (Type: %d).\n"), file_name
,
66 /* Get information from elf program header. */
67 elf_pheader
= (ElfW(Phdr
) *) (elf_header
->e_phoff
+ file_contents
);
69 /* The library is an elf library, now search for soname and
76 program_interpreter
= NULL
;
77 for (i
= 0, segment
= elf_pheader
;
78 i
< elf_header
->e_phnum
; i
++, segment
++)
80 switch (segment
->p_type
)
84 loadaddr
= (segment
->p_vaddr
& 0xfffff000)
85 - (segment
->p_offset
& 0xfffff000);
90 error (0, 0, _("more than one dynamic segment\n"));
92 dynamic_addr
= segment
->p_offset
;
93 dynamic_size
= segment
->p_filesz
;
96 program_interpreter
= (char *) (file_contents
+ segment
->p_offset
);
98 /* Check if this is enough to classify the binary. */
99 for (j
= 0; j
< sizeof (interpreters
) / sizeof (interpreters
[0]);
101 if (strcmp (program_interpreter
, interpreters
[j
].soname
) == 0)
103 *flag
= interpreters
[j
].flag
;
118 /* Now we can read the dynamic sections. */
119 if (dynamic_size
== 0)
122 dynamic_segment
= (ElfW(Dyn
) *) (file_contents
+ dynamic_addr
);
124 /* Find the string table. */
125 dynamic_strings
= NULL
;
126 for (dyn_entry
= dynamic_segment
; dyn_entry
->d_tag
!= DT_NULL
;
129 if (dyn_entry
->d_tag
== DT_STRTAB
)
131 dynamic_strings
= (char *) (file_contents
+ dyn_entry
->d_un
.d_val
- loadaddr
);
136 if (dynamic_strings
== NULL
)
139 /* Now read the DT_NEEDED and DT_SONAME entries. */
140 for (dyn_entry
= dynamic_segment
; dyn_entry
->d_tag
!= DT_NULL
;
143 if (dyn_entry
->d_tag
== DT_NEEDED
|| dyn_entry
->d_tag
== DT_SONAME
)
145 char *name
= dynamic_strings
+ dyn_entry
->d_un
.d_val
;
147 if (dyn_entry
->d_tag
== DT_NEEDED
)
150 if (*flag
== FLAG_ELF
)
152 /* Check if this is enough to classify the binary. */
154 j
< sizeof (known_libs
) / sizeof (known_libs
[0]);
156 if (strcmp (name
, known_libs
[j
].soname
) == 0)
158 *flag
= known_libs
[j
].flag
;
164 else if (dyn_entry
->d_tag
== DT_SONAME
)
165 *soname
= xstrdup (name
);
167 /* Do we have everything we need? */
168 if (*soname
&& *flag
!= FLAG_ELF
)
173 /* We reach this point only if the file doesn't contain a DT_SONAME
174 or if we can't classify the library. If it doesn't have a
175 soname, return the name of the library. */
177 *soname
= xstrdup (lib
);