1  /* Support for reading ELF files.
       2     Copyright (C) 1999-2023 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  
      20  int process_elf32_file (const char *file_name, const char *lib,
      21  			int *flag, unsigned int *isa_level, char **soname,
      22  			void *file_contents, size_t file_length);
      23  int process_elf64_file (const char *file_name, const char *lib,
      24  			int *flag, unsigned int *isa_level, char **soname,
      25  			void *file_contents, size_t file_length);
      26  
      27  /* The ELF flags supported by our current glibc port:
      28     - EF_RISCV_FLOAT_ABI: We support the soft and double ABIs.
      29     - EF_RISCV_RVC: While the Linux ABI mandates the presence of the C
      30       extension, we can still support libraries compiled without that extension
      31       so we just ignore this flag.
      32     - EF_RISCV_RVE: glibc (and Linux) don't support RV32E based systems.
      33     - EF_RISCV_TSO: The TSO extension isn't supported, as doing so would require
      34       some mechanism to ensure that the TSO extension is enabled which doesn't
      35       currently exist.  */
      36  #define SUPPORTED_ELF_FLAGS (EF_RISCV_FLOAT_ABI | EF_RISCV_RVC)
      37  
      38  /* Returns 0 if everything is ok, != 0 in case of error.  */
      39  int
      40  process_elf_file (const char *file_name, const char *lib, int *flag,
      41  		  unsigned int *isa_level, char **soname, void *file_contents,
      42  		  size_t file_length)
      43  {
      44    ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents;
      45    Elf32_Ehdr *elf32_header = (Elf32_Ehdr *) elf_header;
      46    Elf64_Ehdr *elf64_header = (Elf64_Ehdr *) elf_header;
      47    int ret;
      48    long flags;
      49  
      50    /* RISC-V libraries are always libc.so.6+.  */
      51    *flag = FLAG_ELF_LIBC6;
      52  
      53    if (elf_header->e_ident [EI_CLASS] == ELFCLASS32)
      54      {
      55        ret = process_elf32_file (file_name, lib, flag, isa_level, soname,
      56  				file_contents, file_length);
      57        flags = elf32_header->e_flags;
      58      }
      59    else
      60      {
      61        ret = process_elf64_file (file_name, lib, flag, isa_level, soname,
      62  				file_contents, file_length);
      63        flags = elf64_header->e_flags;
      64      }
      65  
      66    /* RISC-V linkers encode the floating point ABI as part of the ELF headers.  */
      67    switch (flags & EF_RISCV_FLOAT_ABI)
      68      {
      69        case EF_RISCV_FLOAT_ABI_SOFT:
      70          *flag |= FLAG_RISCV_FLOAT_ABI_SOFT;
      71  	break;
      72        case EF_RISCV_FLOAT_ABI_DOUBLE:
      73          *flag |= FLAG_RISCV_FLOAT_ABI_DOUBLE;
      74  	break;
      75        default:
      76          return 1;
      77      }
      78  
      79    /* If there are any other ELF flags set then glibc doesn't support this
      80       library.  */
      81    if (flags & ~SUPPORTED_ELF_FLAGS)
      82      return 1;
      83  
      84    return ret;
      85  }
      86  
      87  #undef __ELF_NATIVE_CLASS
      88  #undef process_elf_file
      89  #define process_elf_file process_elf32_file
      90  #define __ELF_NATIVE_CLASS 32
      91  #include "elf/readelflib.c"
      92  
      93  #undef __ELF_NATIVE_CLASS
      94  #undef process_elf_file
      95  #define process_elf_file process_elf64_file
      96  #define __ELF_NATIVE_CLASS 64
      97  #include "elf/readelflib.c"