readelflib.c 3.4 KB

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