1 /* Copyright (C) 1999-2021 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 int process_elf32_file (const char *file_name, const char *lib,
19 			int *flag, unsigned int *osversion,
20 			unsigned int *isa_level, char **soname,
21 			void *file_contents, size_t file_length);
22 int process_elf64_file (const char *file_name, const char *lib,
23 			int *flag, unsigned int *osversion,
24 			unsigned int *isa_level, char **soname,
25 			void *file_contents, size_t file_length);
26 
27 /* Returns 0 if everything is ok, != 0 in case of error.  */
28 int
process_elf_file(const char * file_name,const char * lib,int * flag,unsigned int * osversion,unsigned int * isa_level,char ** soname,void * file_contents,size_t file_length)29 process_elf_file (const char *file_name, const char *lib, int *flag,
30 		  unsigned int *osversion, unsigned int *isa_level,
31 		  char **soname, void *file_contents, size_t file_length)
32 {
33   ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents;
34   int ret, file_flag = 0;
35 
36   switch (elf_header->e_machine)
37     {
38     case EM_X86_64:
39       if (elf_header->e_ident[EI_CLASS] == ELFCLASS64)
40 	/* X86-64 64bit libraries are always libc.so.6+.  */
41 	file_flag = FLAG_X8664_LIB64|FLAG_ELF_LIBC6;
42       else
43 	/* X32 libraries are always libc.so.6+.  */
44 	file_flag = FLAG_X8664_LIBX32|FLAG_ELF_LIBC6;
45       break;
46 #ifndef __x86_64__
47     case EM_IA_64:
48       if (elf_header->e_ident[EI_CLASS] == ELFCLASS64)
49 	{
50 	  /* IA64 64bit libraries are always libc.so.6+.  */
51 	  file_flag = FLAG_IA64_LIB64|FLAG_ELF_LIBC6;
52 	  break;
53 	}
54       goto failed;
55 #endif
56     case EM_386:
57       if (elf_header->e_ident[EI_CLASS] == ELFCLASS32)
58 	break;
59       /* Fall through.  */
60     default:
61 #ifndef __x86_64__
62 failed:
63 #endif
64       error (0, 0, _("%s is for unknown machine %d.\n"),
65 	     file_name, elf_header->e_machine);
66       return 1;
67     }
68 
69   if (elf_header->e_ident[EI_CLASS] == ELFCLASS32)
70     ret = process_elf32_file (file_name, lib, flag, osversion, isa_level,
71 			      soname, file_contents, file_length);
72   else
73     ret = process_elf64_file (file_name, lib, flag, osversion, isa_level,
74 			      soname, file_contents, file_length);
75 
76   if (!ret && file_flag)
77     *flag = file_flag;
78 
79   return ret;
80 }
81 
82 #undef __ELF_NATIVE_CLASS
83 #undef process_elf_file
84 #define process_elf_file process_elf32_file
85 #define __ELF_NATIVE_CLASS 32
86 #include "elf/readelflib.c"
87 
88 #undef __ELF_NATIVE_CLASS
89 #undef process_elf_file
90 #define process_elf_file process_elf64_file
91 #define __ELF_NATIVE_CLASS 64
92 #include "elf/readelflib.c"
93