elf.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <vector>
  2. #include <assert.h>
  3. #include <kernel/errno.h>
  4. #include <kernel/mem.h>
  5. #include <kernel/process.hpp>
  6. #include <kernel/vfs.hpp>
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <types/elf.hpp>
  11. #include <types/string.hpp>
  12. #define align16_down(sp) (sp = ((char*)((uint32_t)(sp)&0xfffffff0)))
  13. template <typename T>
  14. inline void _user_push(char** sp, T d)
  15. {
  16. *sp -= sizeof(T);
  17. *(T*)*sp = d;
  18. }
  19. template <>
  20. inline void _user_push(char** sp, const char* str)
  21. {
  22. size_t len = strlen(str);
  23. *sp -= (len + 1);
  24. align16_down(*sp);
  25. memcpy(*sp, str, len + 1);
  26. }
  27. int types::elf::elf32_load(types::elf::elf32_load_data* d)
  28. {
  29. auto* ent_exec = d->exec_dent;
  30. if (!ent_exec) {
  31. d->errcode = ENOENT;
  32. return GB_FAILED;
  33. }
  34. // TODO: detect file format
  35. types::elf::elf32_header hdr {};
  36. auto n_read = fs::vfs_read(
  37. ent_exec->ind,
  38. (char*)&hdr,
  39. sizeof(types::elf::elf32_header),
  40. 0, sizeof(types::elf::elf32_header));
  41. if (n_read != sizeof(types::elf::elf32_header)) {
  42. d->errcode = EINVAL;
  43. return GB_FAILED;
  44. }
  45. size_t phents_size = hdr.phentsize * hdr.phnum;
  46. size_t shents_size = hdr.shentsize * hdr.shnum;
  47. std::vector<types::elf::elf32_program_header_entry> phents(hdr.phnum);
  48. n_read = fs::vfs_read(
  49. ent_exec->ind,
  50. (char*)phents.data(),
  51. phents_size,
  52. hdr.phoff, phents_size);
  53. // broken file or I/O error
  54. if (n_read != phents_size) {
  55. d->errcode = EINVAL;
  56. return GB_FAILED;
  57. }
  58. std::vector<types::elf::elf32_section_header_entry> shents(hdr.shnum);
  59. n_read = fs::vfs_read(
  60. ent_exec->ind,
  61. (char*)shents.data(),
  62. shents_size,
  63. hdr.shoff, shents_size);
  64. // broken file or I/O error
  65. if (n_read != shents_size) {
  66. d->errcode = EINVAL;
  67. return GB_FAILED;
  68. }
  69. // copy argv and envp
  70. std::vector<string<>> argv, envp;
  71. for (const char* const* p = d->argv; *p; ++p)
  72. argv.emplace_back(*p);
  73. for (const char* const* p = d->envp; *p; ++p)
  74. envp.emplace_back(*p);
  75. // from now on, caller process is recycled.
  76. // so we can't just simply return to it on error.
  77. current_process->mms.clear_user();
  78. uint32_t data_segment_end = 0;
  79. for (const auto& phent : phents) {
  80. if (phent.type != types::elf::elf32_program_header_entry::PT_LOAD)
  81. continue;
  82. auto vaddr = align_down<12>(phent.vaddr);
  83. auto vlen = align_up<12>(phent.vaddr + phent.memsz) - vaddr;
  84. auto flen = align_up<12>(phent.vaddr + phent.filesz) - vaddr;
  85. auto fileoff = align_down<12>(phent.offset);
  86. auto ret = mmap(
  87. (char*)vaddr,
  88. phent.filesz + (phent.vaddr & 0xfff),
  89. ent_exec->ind,
  90. fileoff,
  91. 1,
  92. d->system);
  93. if (ret != GB_OK)
  94. kill_current(-1);
  95. if (vlen > flen) {
  96. ret = mmap((char*)vaddr + flen, vlen - flen,
  97. nullptr, 0, true, d->system);
  98. if (ret != GB_OK)
  99. kill_current(-1);
  100. }
  101. if (vaddr + vlen > data_segment_end)
  102. data_segment_end = vaddr + vlen;
  103. }
  104. current_process->mms.register_brk((char*)data_segment_end + 0x10000);
  105. for (const auto& shent : shents) {
  106. if (shent.sh_type == elf32_section_header_entry::SHT_NOBITS)
  107. memset((char*)shent.sh_addr, 0x00, shent.sh_size);
  108. }
  109. // map stack area
  110. auto ret = mmap((void*)types::elf::ELF_STACK_TOP,
  111. types::elf::ELF_STACK_SIZE, nullptr, 0, true, false);
  112. assert(ret == GB_OK);
  113. d->eip = (void*)hdr.entry;
  114. d->sp = reinterpret_cast<uint32_t*>(types::elf::ELF_STACK_BOTTOM);
  115. auto* sp = (char**)&d->sp;
  116. // fill information block area
  117. std::vector<char*> args, envs;
  118. for (const auto& env : envp) {
  119. _user_push(sp, env.c_str());
  120. envs.push_back(*sp);
  121. }
  122. for (const auto& arg : argv) {
  123. _user_push(sp, arg.c_str());
  124. args.push_back(*sp);
  125. }
  126. // push null auxiliary vector entry
  127. _user_push(sp, 0);
  128. _user_push(sp, 0);
  129. // push 0 for envp
  130. _user_push(sp, 0);
  131. // push envp
  132. *sp -= sizeof(void*) * envs.size();
  133. memcpy(*sp, envs.data(), sizeof(void*) * envs.size());
  134. // push 0 for argv
  135. _user_push(sp, 0);
  136. // push argv
  137. *sp -= sizeof(void*) * args.size();
  138. memcpy(*sp, args.data(), sizeof(void*) * args.size());
  139. // push argc
  140. _user_push(sp, args.size());
  141. // rename current thread
  142. current_thread->name = ent_exec->name;
  143. return GB_OK;
  144. }