1/* Support for reading ELF files.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19
20int process_elf32_file (const char *file_name, const char *lib,
21 int *flag, unsigned int *osversion,
22 unsigned int *isa_level, char **soname,
23 void *file_contents, size_t file_length);
24int process_elf64_file (const char *file_name, const char *lib,
25 int *flag, unsigned int *osversion,
26 unsigned int *isa_level, char **soname,
27 void *file_contents, size_t file_length);
28
29/* The ELF flags supported by our current glibc port:
30 - EF_RISCV_FLOAT_ABI: We support the soft and double ABIs.
31 - EF_RISCV_RVC: While the Linux ABI mandates the presence of the C
32 extension, we can still support libraries compiled without that extension
33 so we just ignore this flag.
34 - EF_RISCV_RVE: glibc (and Linux) don't support RV32E based systems.
35 - EF_RISCV_TSO: The TSO extension isn't supported, as doing so would require
36 some mechanism to ensure that the TSO extension is enabled which doesn't
37 currently exist. */
38#define SUPPORTED_ELF_FLAGS (EF_RISCV_FLOAT_ABI | EF_RISCV_RVC)
39
40/* Returns 0 if everything is ok, != 0 in case of error. */
41int
42process_elf_file (const char *file_name, const char *lib, int *flag,
43 unsigned int *osversion, unsigned int *isa_level,
44 char **soname, void *file_contents, size_t file_length)
45{
46 ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents;
47 Elf32_Ehdr *elf32_header = (Elf32_Ehdr *) elf_header;
48 Elf64_Ehdr *elf64_header = (Elf64_Ehdr *) elf_header;
49 int ret;
50 long flags;
51
52 /* RISC-V libraries are always libc.so.6+. */
53 *flag = FLAG_ELF_LIBC6;
54
55 if (elf_header->e_ident [EI_CLASS] == ELFCLASS32)
56 {
57 ret = process_elf32_file (file_name, lib, flag, osversion, isa_level,
58 soname, file_contents, file_length);
59 flags = elf32_header->e_flags;
60 }
61 else
62 {
63 ret = process_elf64_file (file_name, lib, flag, osversion, isa_level,
64 soname, file_contents, file_length);
65 flags = elf64_header->e_flags;
66 }
67
68 /* RISC-V linkers encode the floating point ABI as part of the ELF headers. */
69 switch (flags & EF_RISCV_FLOAT_ABI)
70 {
71 case EF_RISCV_FLOAT_ABI_SOFT:
72 *flag |= FLAG_RISCV_FLOAT_ABI_SOFT;
73 break;
74 case EF_RISCV_FLOAT_ABI_DOUBLE:
75 *flag |= FLAG_RISCV_FLOAT_ABI_DOUBLE;
76 break;
77 default:
78 return 1;
79 }
80
81 /* If there are any other ELF flags set then glibc doesn't support this
82 library. */
83 if (flags & ~SUPPORTED_ELF_FLAGS)
84 return 1;
85
86 return ret;
87}
88
89#undef __ELF_NATIVE_CLASS
90#undef process_elf_file
91#define process_elf_file process_elf32_file
92#define __ELF_NATIVE_CLASS 32
93#include "elf/readelflib.c"
94
95#undef __ELF_NATIVE_CLASS
96#undef process_elf_file
97#define process_elf_file process_elf64_file
98#define __ELF_NATIVE_CLASS 64
99#include "elf/readelflib.c"
100

source code of glibc/sysdeps/unix/sysv/linux/riscv/readelflib.c