PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/hw/elf_ops.h

https://github.com/edgarigl/tlmu
C Header | 297 lines | 259 code | 30 blank | 8 comment | 65 complexity | 0bfb4bfcad987515570e05acf9f5fd46 MD5 | raw file
Possible License(s): GPL-2.0
  1. static void glue(bswap_ehdr, SZ)(struct elfhdr *ehdr)
  2. {
  3. bswap16s(&ehdr->e_type); /* Object file type */
  4. bswap16s(&ehdr->e_machine); /* Architecture */
  5. bswap32s(&ehdr->e_version); /* Object file version */
  6. bswapSZs(&ehdr->e_entry); /* Entry point virtual address */
  7. bswapSZs(&ehdr->e_phoff); /* Program header table file offset */
  8. bswapSZs(&ehdr->e_shoff); /* Section header table file offset */
  9. bswap32s(&ehdr->e_flags); /* Processor-specific flags */
  10. bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
  11. bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
  12. bswap16s(&ehdr->e_phnum); /* Program header table entry count */
  13. bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
  14. bswap16s(&ehdr->e_shnum); /* Section header table entry count */
  15. bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
  16. }
  17. static void glue(bswap_phdr, SZ)(struct elf_phdr *phdr)
  18. {
  19. bswap32s(&phdr->p_type); /* Segment type */
  20. bswapSZs(&phdr->p_offset); /* Segment file offset */
  21. bswapSZs(&phdr->p_vaddr); /* Segment virtual address */
  22. bswapSZs(&phdr->p_paddr); /* Segment physical address */
  23. bswapSZs(&phdr->p_filesz); /* Segment size in file */
  24. bswapSZs(&phdr->p_memsz); /* Segment size in memory */
  25. bswap32s(&phdr->p_flags); /* Segment flags */
  26. bswapSZs(&phdr->p_align); /* Segment alignment */
  27. }
  28. static void glue(bswap_shdr, SZ)(struct elf_shdr *shdr)
  29. {
  30. bswap32s(&shdr->sh_name);
  31. bswap32s(&shdr->sh_type);
  32. bswapSZs(&shdr->sh_flags);
  33. bswapSZs(&shdr->sh_addr);
  34. bswapSZs(&shdr->sh_offset);
  35. bswapSZs(&shdr->sh_size);
  36. bswap32s(&shdr->sh_link);
  37. bswap32s(&shdr->sh_info);
  38. bswapSZs(&shdr->sh_addralign);
  39. bswapSZs(&shdr->sh_entsize);
  40. }
  41. static void glue(bswap_sym, SZ)(struct elf_sym *sym)
  42. {
  43. bswap32s(&sym->st_name);
  44. bswapSZs(&sym->st_value);
  45. bswapSZs(&sym->st_size);
  46. bswap16s(&sym->st_shndx);
  47. }
  48. static struct elf_shdr *glue(find_section, SZ)(struct elf_shdr *shdr_table,
  49. int n, int type)
  50. {
  51. int i;
  52. for(i=0;i<n;i++) {
  53. if (shdr_table[i].sh_type == type)
  54. return shdr_table + i;
  55. }
  56. return NULL;
  57. }
  58. static int glue(symfind, SZ)(const void *s0, const void *s1)
  59. {
  60. struct elf_sym *key = (struct elf_sym *)s0;
  61. struct elf_sym *sym = (struct elf_sym *)s1;
  62. int result = 0;
  63. if (key->st_value < sym->st_value) {
  64. result = -1;
  65. } else if (key->st_value >= sym->st_value + sym->st_size) {
  66. result = 1;
  67. }
  68. return result;
  69. }
  70. static const char *glue(lookup_symbol, SZ)(struct syminfo *s,
  71. target_phys_addr_t orig_addr)
  72. {
  73. struct elf_sym *syms = glue(s->disas_symtab.elf, SZ);
  74. struct elf_sym key;
  75. struct elf_sym *sym;
  76. key.st_value = orig_addr;
  77. sym = bsearch(&key, syms, s->disas_num_syms, sizeof(*syms), glue(symfind, SZ));
  78. if (sym != NULL) {
  79. return s->disas_strtab + sym->st_name;
  80. }
  81. return "";
  82. }
  83. static int glue(symcmp, SZ)(const void *s0, const void *s1)
  84. {
  85. struct elf_sym *sym0 = (struct elf_sym *)s0;
  86. struct elf_sym *sym1 = (struct elf_sym *)s1;
  87. return (sym0->st_value < sym1->st_value)
  88. ? -1
  89. : ((sym0->st_value > sym1->st_value) ? 1 : 0);
  90. }
  91. static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab,
  92. int clear_lsb)
  93. {
  94. struct elf_shdr *symtab, *strtab, *shdr_table = NULL;
  95. struct elf_sym *syms = NULL;
  96. struct syminfo *s;
  97. int nsyms, i;
  98. char *str = NULL;
  99. shdr_table = load_at(fd, ehdr->e_shoff,
  100. sizeof(struct elf_shdr) * ehdr->e_shnum);
  101. if (!shdr_table)
  102. return -1;
  103. if (must_swab) {
  104. for (i = 0; i < ehdr->e_shnum; i++) {
  105. glue(bswap_shdr, SZ)(shdr_table + i);
  106. }
  107. }
  108. symtab = glue(find_section, SZ)(shdr_table, ehdr->e_shnum, SHT_SYMTAB);
  109. if (!symtab)
  110. goto fail;
  111. syms = load_at(fd, symtab->sh_offset, symtab->sh_size);
  112. if (!syms)
  113. goto fail;
  114. nsyms = symtab->sh_size / sizeof(struct elf_sym);
  115. i = 0;
  116. while (i < nsyms) {
  117. if (must_swab)
  118. glue(bswap_sym, SZ)(&syms[i]);
  119. /* We are only interested in function symbols.
  120. Throw everything else away. */
  121. if (syms[i].st_shndx == SHN_UNDEF ||
  122. syms[i].st_shndx >= SHN_LORESERVE ||
  123. ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
  124. nsyms--;
  125. if (i < nsyms) {
  126. syms[i] = syms[nsyms];
  127. }
  128. continue;
  129. }
  130. if (clear_lsb) {
  131. /* The bottom address bit marks a Thumb or MIPS16 symbol. */
  132. syms[i].st_value &= ~(glue(glue(Elf, SZ), _Addr))1;
  133. }
  134. i++;
  135. }
  136. if (nsyms) {
  137. syms = g_realloc(syms, nsyms * sizeof(*syms));
  138. qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ));
  139. for (i = 0; i < nsyms - 1; i++) {
  140. if (syms[i].st_size == 0) {
  141. syms[i].st_size = syms[i + 1].st_value - syms[i].st_value;
  142. }
  143. }
  144. } else {
  145. g_free(syms);
  146. syms = NULL;
  147. }
  148. /* String table */
  149. if (symtab->sh_link >= ehdr->e_shnum)
  150. goto fail;
  151. strtab = &shdr_table[symtab->sh_link];
  152. str = load_at(fd, strtab->sh_offset, strtab->sh_size);
  153. if (!str)
  154. goto fail;
  155. /* Commit */
  156. s = g_malloc0(sizeof(*s));
  157. s->lookup_symbol = glue(lookup_symbol, SZ);
  158. glue(s->disas_symtab.elf, SZ) = syms;
  159. s->disas_num_syms = nsyms;
  160. s->disas_strtab = str;
  161. s->next = syminfos;
  162. syminfos = s;
  163. g_free(shdr_table);
  164. return 0;
  165. fail:
  166. g_free(syms);
  167. g_free(str);
  168. g_free(shdr_table);
  169. return -1;
  170. }
  171. static int glue(load_elf, SZ)(const char *name, int fd,
  172. uint64_t (*translate_fn)(void *, uint64_t),
  173. void *translate_opaque,
  174. int must_swab, uint64_t *pentry,
  175. uint64_t *lowaddr, uint64_t *highaddr,
  176. int elf_machine, int clear_lsb)
  177. {
  178. struct elfhdr ehdr;
  179. struct elf_phdr *phdr = NULL, *ph;
  180. int size, i, total_size;
  181. elf_word mem_size;
  182. uint64_t addr, low = (uint64_t)-1, high = 0;
  183. uint8_t *data = NULL;
  184. char label[128];
  185. if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
  186. goto fail;
  187. if (must_swab) {
  188. glue(bswap_ehdr, SZ)(&ehdr);
  189. }
  190. switch (elf_machine) {
  191. case EM_PPC64:
  192. if (EM_PPC64 != ehdr.e_machine)
  193. if (EM_PPC != ehdr.e_machine)
  194. goto fail;
  195. break;
  196. case EM_X86_64:
  197. if (EM_X86_64 != ehdr.e_machine)
  198. if (EM_386 != ehdr.e_machine)
  199. goto fail;
  200. break;
  201. case EM_MICROBLAZE:
  202. if (EM_MICROBLAZE != ehdr.e_machine)
  203. if (EM_MICROBLAZE_OLD != ehdr.e_machine)
  204. goto fail;
  205. break;
  206. default:
  207. if (elf_machine != ehdr.e_machine)
  208. goto fail;
  209. }
  210. if (pentry)
  211. *pentry = (uint64_t)(elf_sword)ehdr.e_entry;
  212. glue(load_symbols, SZ)(&ehdr, fd, must_swab, clear_lsb);
  213. size = ehdr.e_phnum * sizeof(phdr[0]);
  214. lseek(fd, ehdr.e_phoff, SEEK_SET);
  215. phdr = g_malloc0(size);
  216. if (!phdr)
  217. goto fail;
  218. if (read(fd, phdr, size) != size)
  219. goto fail;
  220. if (must_swab) {
  221. for(i = 0; i < ehdr.e_phnum; i++) {
  222. ph = &phdr[i];
  223. glue(bswap_phdr, SZ)(ph);
  224. }
  225. }
  226. total_size = 0;
  227. for(i = 0; i < ehdr.e_phnum; i++) {
  228. ph = &phdr[i];
  229. if (ph->p_type == PT_LOAD && ph->p_memsz) {
  230. mem_size = ph->p_memsz;
  231. /* XXX: avoid allocating */
  232. data = g_malloc0(mem_size);
  233. if (ph->p_filesz > 0) {
  234. if (lseek(fd, ph->p_offset, SEEK_SET) < 0)
  235. goto fail;
  236. if (read(fd, data, ph->p_filesz) != ph->p_filesz)
  237. goto fail;
  238. }
  239. /* address_offset is hack for kernel images that are
  240. linked at the wrong physical address. */
  241. if (translate_fn) {
  242. addr = translate_fn(translate_opaque, ph->p_paddr);
  243. } else {
  244. addr = ph->p_paddr;
  245. }
  246. snprintf(label, sizeof(label), "phdr #%d: %s", i, name);
  247. rom_add_blob_fixed(label, data, mem_size, addr);
  248. total_size += mem_size;
  249. if (addr < low)
  250. low = addr;
  251. if ((addr + mem_size) > high)
  252. high = addr + mem_size;
  253. g_free(data);
  254. data = NULL;
  255. }
  256. }
  257. g_free(phdr);
  258. if (lowaddr)
  259. *lowaddr = (uint64_t)(elf_sword)low;
  260. if (highaddr)
  261. *highaddr = (uint64_t)(elf_sword)high;
  262. return total_size;
  263. fail:
  264. g_free(data);
  265. g_free(phdr);
  266. return -1;
  267. }