A shell that runs x86_64 assembly
c
x86-64
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shell.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Copyright Yann Weber <asmsh@yannweb.net>
  2. This file is part of asmsh.
  3. asmsh is free software: you can redistribute it and/or modify it under the
  4. terms of the GNU General Public License as published by the Free Software
  5. Foundation, either version 3 of the License, or any later version.
  6. asmsh is distributed in the hope that it will be useful, but WITHOUT ANY
  7. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  9. details.
  10. You should have received a copy of the GNU General Public License along
  11. with asmsh. If not, see <https://www.gnu.org/licenses/>.
  12. */
  13. #include "shell.h"
  14. int asmsh_init(asmsh_t *sh, const char *child_path)
  15. {
  16. bzero(sh, sizeof(asmsh_t));
  17. if(!(sh->env = asmsh_env(child_path)))
  18. {
  19. asmsh_log_fatal("Unable to start child env");
  20. goto err;
  21. }
  22. if(!(sh->cctx = asmsh_asmc_ctx_default()))
  23. {
  24. asmsh_log_fatal("Unable to start compilation context");
  25. asmsh_env_free(sh->env);
  26. goto err;
  27. }
  28. sh->child_path = strdup(child_path); // TODO check error
  29. return 0;
  30. err:
  31. bzero(sh, sizeof(asmsh_t));
  32. return -1;
  33. }
  34. void asmsh_cleanup(asmsh_t *sh)
  35. {
  36. if(!sh){ return; }
  37. if(sh->cctx)
  38. {
  39. asmsh_asmc_ctx_free(sh->cctx);
  40. }
  41. if(sh->env)
  42. {
  43. asmsh_env_free(sh->env);
  44. }
  45. if(sh->child_path)
  46. {
  47. free(sh->child_path);
  48. }
  49. if(sh->last_instr)
  50. {
  51. free(sh->last_instr);
  52. }
  53. bzero(sh, sizeof(asmsh_t));
  54. }
  55. /** Attempt to compile and step if sucess */
  56. static int _compile_step(asmsh_t *sh, const char *cmd);
  57. /** Attempt to handle given shell internal command (starting with '.') */
  58. static int _handle_command(asmsh_t *sh, const char *cmd);
  59. int asmsh_exec(asmsh_t *sh, const char *cmd)
  60. {
  61. if(!cmd)
  62. {
  63. errno=EINVAL;
  64. return -1;
  65. }
  66. int clen, ret;
  67. //lstrip whitespace
  68. for(cmd; *cmd && (*cmd == ' ' || *cmd == '\t'); cmd++);
  69. if(!*cmd) { return 0; }
  70. switch(*cmd)
  71. {
  72. case '.':
  73. ret = _handle_command(sh, cmd);
  74. break;
  75. default:
  76. ret = _compile_step(sh, cmd);
  77. break;
  78. }
  79. if(!ret)
  80. {
  81. asmsh_env_update_regs(sh->env);
  82. }
  83. return ret;
  84. }
  85. static int _compile_step(asmsh_t *sh, const char *cmd)
  86. {
  87. int ret, status;
  88. asmsh_bytecode_t bcode;
  89. if(asmsh_asmc_compile(sh->cctx, cmd, &bcode) < 0)
  90. {
  91. return -1;
  92. }
  93. if(!bcode.size)
  94. {
  95. asmsh_log_error("No bytecode returned...");
  96. return -1;
  97. }
  98. sh->last_bcode = bcode;
  99. if(sh->last_instr) { free(sh->last_instr); }
  100. sh->last_instr = strdup(cmd);
  101. //char buf[256];
  102. //int _ret;
  103. //_ret = snprintf(buf, 255, "'%s' = 0x", cmd);
  104. //for(int _i=0; _i<bcode.size; _i++)
  105. //{
  106. // _ret += snprintf(buf+_ret, 255-_ret, "%02x", bcode.bytes[_i]);
  107. //}
  108. //_ret += snprintf(buf+_ret, 255-_ret, "(%d)", bcode.size);
  109. //buf[_ret] = '\0';
  110. //asmsh_log_info(buf);
  111. if(asmsh_env_write_code(sh->env, &bcode))
  112. {
  113. return -1;
  114. }
  115. status=0;
  116. ret = asmsh_env_step(sh->env, &status);
  117. if(ret < 0)
  118. {
  119. perror("Unable to step");
  120. asmsh_log_fatal("Error will child step");
  121. return -1;
  122. }
  123. else if(ret > 0)
  124. {
  125. asmsh_log_fatal("Child exited with non-zero status");
  126. return 1+WEXITSTATUS(status);
  127. }
  128. return 0;
  129. }
  130. static int _handle_command(asmsh_t *sh, const char *cmd)
  131. {
  132. const int clen = strlen(cmd);
  133. const asmsh_cmd_t *match;
  134. int ret;
  135. asmsh_cmd_args_t *args;
  136. args = asmsh_cmd_parse(cmd);
  137. if(!args)
  138. {
  139. asmsh_log_error("Parse error"); // TODO better error
  140. return -1;
  141. }
  142. match = asmsh_cmd_match(args->cmd, strlen(args->cmd));
  143. if(!match)
  144. {
  145. asmsh_log_error("Unknow command");
  146. return -1;
  147. }
  148. ret = match->cmd(sh, args);
  149. asmsh_cmd_args_free(args);
  150. return ret;
  151. }