elf_parser.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) 2021 Ant Group. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <assert.h>
  7. #include <fcntl.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <stdbool.h>
  12. #include "elf.h"
  13. #include "aot_runtime.h"
  14. #include "bh_log.h"
  15. #include "elf_parser.h"
  16. bool
  17. is_ELF(void *buf)
  18. {
  19. Elf32_Ehdr *eh = (Elf32_Ehdr *)buf;
  20. if (!strncmp((char *)eh->e_ident, "\177ELF", 4)) {
  21. LOG_VERBOSE("the buffer is ELF entry!");
  22. return true;
  23. }
  24. LOG_VERBOSE("the buffer is not ELF entry!");
  25. return false;
  26. }
  27. static bool
  28. is64Bit(Elf32_Ehdr *eh)
  29. {
  30. if (eh->e_ident[EI_CLASS] == ELFCLASS64)
  31. return true;
  32. else
  33. return false;
  34. }
  35. static bool
  36. is32Bit(Elf32_Ehdr *eh)
  37. {
  38. if (eh->e_ident[EI_CLASS] == ELFCLASS32)
  39. return true;
  40. else
  41. return false;
  42. }
  43. bool
  44. is_ELF64(void *buf)
  45. {
  46. Elf64_Ehdr *eh = (Elf64_Ehdr *)buf;
  47. if (!strncmp((char *)eh->e_ident, "\177ELF", 4)) {
  48. LOG_VERBOSE("the buffer is ELF entry!");
  49. return true;
  50. }
  51. LOG_VERBOSE("the buffer is not ELF entry!");
  52. return false;
  53. }
  54. static void
  55. read_section_header_table(Elf32_Ehdr *eh, Elf32_Shdr *sh_table[])
  56. {
  57. uint32_t i;
  58. char *buf = (char *)eh;
  59. buf += eh->e_shoff;
  60. LOG_VERBOSE("str index = %d count=%d", eh->e_shstrndx, eh->e_shnum);
  61. for (i = 0; i < eh->e_shnum; i++) {
  62. sh_table[i] = (Elf32_Shdr *)buf;
  63. buf += eh->e_shentsize;
  64. }
  65. }
  66. static void
  67. read_section_header_table64(Elf64_Ehdr *eh, Elf64_Shdr *sh_table[])
  68. {
  69. uint32_t i;
  70. char *buf = (char *)eh;
  71. buf += eh->e_shoff;
  72. for (i = 0; i < eh->e_shnum; i++) {
  73. sh_table[i] = (Elf64_Shdr *)buf;
  74. buf += eh->e_shentsize;
  75. }
  76. }
  77. static char *
  78. get_section(Elf32_Ehdr *eh, Elf32_Shdr *section_header)
  79. {
  80. char *buf = (char *)eh;
  81. return buf + section_header->sh_offset;
  82. }
  83. static char *
  84. get_section64(Elf64_Ehdr *eh, Elf64_Shdr *section_header)
  85. {
  86. char *buf = (char *)eh;
  87. return buf + section_header->sh_offset;
  88. }
  89. static bool
  90. is_text_section(const char *section_name)
  91. {
  92. return !strcmp(section_name, ".text") || !strcmp(section_name, ".ltext");
  93. }
  94. bool
  95. get_text_section(void *buf, uint64_t *offset, uint64_t *size)
  96. {
  97. bool ret = false;
  98. uint32 i;
  99. char *sh_str;
  100. /* Assumption: Only one of .text or .ltext is non-empty. */
  101. if (is64Bit(buf)) {
  102. Elf64_Ehdr *eh = (Elf64_Ehdr *)buf;
  103. Elf64_Shdr **sh_table =
  104. wasm_runtime_malloc(eh->e_shnum * sizeof(Elf64_Shdr *));
  105. if (sh_table) {
  106. read_section_header_table64(eh, sh_table);
  107. sh_str = get_section64(eh, sh_table[eh->e_shstrndx]);
  108. for (i = 0; i < eh->e_shnum; i++) {
  109. if (is_text_section(sh_str + sh_table[i]->sh_name)) {
  110. *offset = sh_table[i]->sh_offset;
  111. *size = sh_table[i]->sh_size;
  112. sh_table[i]->sh_addr =
  113. (Elf64_Addr)(uintptr_t)((char *)buf
  114. + sh_table[i]->sh_offset);
  115. ret = true;
  116. if (*size > 0) {
  117. break;
  118. }
  119. }
  120. }
  121. wasm_runtime_free(sh_table);
  122. }
  123. }
  124. else if (is32Bit(buf)) {
  125. Elf32_Ehdr *eh = (Elf32_Ehdr *)buf;
  126. Elf32_Shdr **sh_table =
  127. wasm_runtime_malloc(eh->e_shnum * sizeof(Elf32_Shdr *));
  128. if (sh_table) {
  129. read_section_header_table(eh, sh_table);
  130. sh_str = get_section(eh, sh_table[eh->e_shstrndx]);
  131. for (i = 0; i < eh->e_shnum; i++) {
  132. if (is_text_section(sh_str + sh_table[i]->sh_name)) {
  133. *offset = sh_table[i]->sh_offset;
  134. *size = sh_table[i]->sh_size;
  135. sh_table[i]->sh_addr =
  136. (Elf32_Addr)(uintptr_t)((char *)buf
  137. + sh_table[i]->sh_offset);
  138. ret = true;
  139. if (*size > 0) {
  140. break;
  141. }
  142. }
  143. }
  144. wasm_runtime_free(sh_table);
  145. }
  146. }
  147. return ret;
  148. }