elf.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Minimal ELF32 loader: parse PT_LOAD segments, allocate physical pages,
  2. * copy file bytes, zero BSS, and map them into a page directory as user pages. */
  3. #include "elf.h"
  4. #include "mm.h"
  5. #include "paging.h"
  6. #define ELF_MAGIC 0x464C457F /* "\x7fELF" little-endian */
  7. #define PT_LOAD 1
  8. #define EM_386 3
  9. struct elf32_hdr {
  10. unsigned char ident[16];
  11. unsigned short type;
  12. unsigned short machine;
  13. unsigned int version;
  14. unsigned int entry;
  15. unsigned int phoff;
  16. unsigned int shoff;
  17. unsigned int flags;
  18. unsigned short ehsize;
  19. unsigned short phentsize;
  20. unsigned short phnum;
  21. unsigned short shentsize;
  22. unsigned short shnum;
  23. unsigned short shstrndx;
  24. };
  25. struct elf32_phdr {
  26. unsigned int type;
  27. unsigned int offset;
  28. unsigned int vaddr;
  29. unsigned int paddr;
  30. unsigned int filesz;
  31. unsigned int memsz;
  32. unsigned int flags;
  33. unsigned int align;
  34. };
  35. static void memzero(void *dst, unsigned int n)
  36. {
  37. unsigned char *p = (unsigned char *)dst;
  38. while (n--)
  39. *p++ = 0;
  40. }
  41. static void memcopy(void *dst, const void *src, unsigned int n)
  42. {
  43. unsigned char *d = (unsigned char *)dst;
  44. const unsigned char *s = (const unsigned char *)src;
  45. while (n--)
  46. *d++ = *s++;
  47. }
  48. unsigned int elf_load(unsigned int cr3, const void *image, unsigned int size)
  49. {
  50. const struct elf32_hdr *h = (const struct elf32_hdr *)image;
  51. const struct elf32_phdr *ph;
  52. unsigned int i, p;
  53. if (size < sizeof(*h))
  54. return 0;
  55. if (*(const unsigned int *)h->ident != ELF_MAGIC)
  56. return 0;
  57. if (h->machine != EM_386)
  58. return 0;
  59. ph = (const struct elf32_phdr *)((const char *)image + h->phoff);
  60. for (i = 0; i < h->phnum; i++) {
  61. unsigned int va, memsz, filesz, off, pbase, npages;
  62. if (ph[i].type != PT_LOAD || ph[i].memsz == 0)
  63. continue;
  64. va = ph[i].vaddr;
  65. memsz = ph[i].memsz;
  66. filesz = ph[i].filesz;
  67. off = ph[i].offset;
  68. pbase = va & 0xFFFFF000u;
  69. npages = ((va - pbase) + memsz + 0xFFF) >> 12;
  70. for (p = 0; p < npages; p++) {
  71. unsigned int pva = pbase + p * 0x1000;
  72. unsigned int phys = paging_page_phys(cr3, pva);
  73. unsigned int start, end, dst, src, n;
  74. if (!phys) {
  75. phys = mm_alloc_page();
  76. if (!phys)
  77. return 0;
  78. memzero((void *)phys, 0x1000);
  79. if (!paging_map_page(cr3, pva, phys,
  80. PTE_USER | PTE_WRITABLE | PTE_PRESENT))
  81. return 0;
  82. }
  83. /* Copy the file-backed part that falls in this page. */
  84. start = (va + filesz > pva) ? ((va > pva) ? va : pva) : 0;
  85. end = va + filesz;
  86. if (end > pva + 0x1000)
  87. end = pva + 0x1000;
  88. if (start && end > start) {
  89. dst = start - pva;
  90. src = off + (start - va);
  91. n = end - start;
  92. memcopy((void *)(phys + dst),
  93. (const char *)image + src, n);
  94. }
  95. /* Zero the BSS tail of this segment in this page. */
  96. start = va + filesz;
  97. end = va + memsz;
  98. if (start < pva)
  99. start = pva;
  100. if (end > pva + 0x1000)
  101. end = pva + 0x1000;
  102. if (end > start)
  103. memzero((void *)(phys + (start - pva)),
  104. end - start);
  105. }
  106. }
  107. /* Flush the caches so the freshly-copied code is coherent. The Pentium
  108. * (P5) has split I/D caches with no hardware coherency for self-modifying
  109. * code, so the code we just wrote through the data cache is not visible to
  110. * the instruction cache until we wbinvd. QEMU doesn't model this. */
  111. __asm__ volatile ("wbinvd");
  112. return h->entry;
  113. }