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.

compile.h 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef ASMSH_COMPILE_H
  14. #define ASMSH_COMPILE_H
  15. #include "config.h"
  16. #include <alloca.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <signal.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <sys/select.h>
  25. #include <sys/signalfd.h>
  26. #include <sys/types.h>
  27. #include <sys/wait.h>
  28. #include "logger.h"
  29. /** Define the default assembler (GNU as) */
  30. #define ASMSH_COMPILE_AS "as"
  31. #define ASMSH_COMPILE_ARGS {"", "-O2", "-f", "-ad", "-o", NULL, "-c", "-", NULL}
  32. #define ASMSH_COMPILE_OBJ "/tmp/asmsh_jit_XXXXXX"
  33. typedef struct asmsh_asmc_ctx_s asmsh_asmc_ctx_t;
  34. typedef struct asmsh_asmc_child_s asmsh_asmc_child_t;
  35. typedef struct asmsh_bytecode_s asmsh_bytecode_t;
  36. struct asmsh_asmc_child_s
  37. {
  38. /** Compiler subprocess PID (waiting code on piped STDIN) */
  39. pid_t pid;
  40. /** Write endpoint of subprocess's piped STDIN */
  41. int pipe_stdin;
  42. /** Read endpoint of subprocess's piped STDOUT & STDERR */
  43. int pipe_stdout[2];
  44. };
  45. struct asmsh_asmc_ctx_s
  46. {
  47. /** NULL terminated list of args for child subprocess,
  48. * arg[0] is progname */
  49. char **args;
  50. /** Points on args[0] */
  51. char *progname;
  52. /** Points on the result path argument (value of -o) */
  53. char *respath;
  54. /** Compilation timeout */
  55. struct timeval ctimeout;
  56. /** A child, that can be spawn in advance */
  57. asmsh_asmc_child_t child;
  58. /** If true pre spawn childs */
  59. char pre_spawn;
  60. };
  61. /** Compilation result, 1 instruction bytecode */
  62. struct asmsh_bytecode_s
  63. {
  64. /** Instruction bytes */
  65. unsigned char bytes[15];
  66. /** Instruction size */
  67. char size;
  68. };
  69. /**
  70. * Check if :
  71. * - contains no ';' nor '#' nor '\n'
  72. * - only contains char in range [0x20 .. 0x7E]
  73. * - do not begins by '.'
  74. *
  75. * @param char* The instruction to pre-check
  76. * @param char** If not null contains a reason on failure (and must be freed)
  77. * @return 0 if instr has a valid syntax else -1
  78. */
  79. int asmsh_asmc_syntax(const char* instr, char **reason);
  80. /** Return a default compilation context */
  81. asmsh_asmc_ctx_t* asmsh_asmc_ctx_default();
  82. /** Return a new compilation context
  83. * @param char* The assembler name/path
  84. * @param char* The result path for the compiled obj as mkstemp(3) template
  85. * @param char*[] The assembler list of arguments, without the argv[0] progname and with NULL as a placeholder for the result path
  86. * @param int If 0 no child are pre-spawned, else a child is spawned in returned context
  87. * @param struct timeval* Compilation timeout
  88. */
  89. asmsh_asmc_ctx_t* asmsh_asmc_ctx(const char *progname, \
  90. const char *result_path, char* const args[], int pre_spawn,
  91. struct timeval *ctimeout);
  92. /** Free all compile context resources */
  93. void asmsh_asmc_ctx_free(asmsh_asmc_ctx_t *ctx);
  94. /** Compile an instruction
  95. * @param asmsh_asmc_ctx_t* The context
  96. * @param const char* The instruction to compile
  97. * @param asmsh_bytecode_t* Pointer on the result struct
  98. * @return 0 if ok else -1 on failure with errno set
  99. */
  100. int asmsh_asmc_compile(asmsh_asmc_ctx_t *ctx, const char *instr, asmsh_bytecode_t *res);
  101. int asmsh_asmc_compile_unsafe(asmsh_asmc_ctx_t *ctx, const char *_instr, asmsh_bytecode_t *res);
  102. /** Spawn an assembler process child process
  103. * @return 0 if no error else -1
  104. */
  105. int asmsh_asmc_spawn(asmsh_asmc_ctx_t *ctx);
  106. /**Child process main function
  107. * @param int[2][2] The pipes for stdin and stderr in this order
  108. * @param *char The path/name of the assembler executable
  109. * @param *char[] The NULL terminated list of arguments for the assembler
  110. * @return NEVER
  111. */
  112. void asmsh_asmc_child(const int std_pipes[3][2], const char *progname, char* const args[]);
  113. /** Given an object file, extract the "bytecode"
  114. *
  115. * Copy the .text segment of an ELF file
  116. * @param const char* the object file path
  117. * @param asmsh_bytecode_t* The result bytecode
  118. *
  119. * @return 0 if no error else -1
  120. */
  121. int asmh_asmc_bytecode_from_obj(const char *objfile, asmsh_bytecode_t *bytecode);
  122. /** Forge compiler arguments.
  123. *
  124. * The constructed arguments list will be allocated in args_o.
  125. * Put progname as first argument, result_tpl will replace the first NULL encountered
  126. * in the args_in array. args_in is an array of argument, that will be copied in
  127. * args_o.
  128. * @param const char* The compiler name/path
  129. * @param const char* An mkstemp() file template
  130. * @param char *const[] The NULL terminated list of arguments, the first will
  131. * be replaced by the progname value. And the first encountered NULL will
  132. * be affected to the temporary result file (from result_tpl)
  133. * @param char** The resulting args
  134. * @return 0 if no error else -1
  135. */
  136. int asmsh_asmc_buildarg(const char* progname, const char *result_tpl, \
  137. char *const args_in[],
  138. char ***args_o, char **progname_o, char **respath_o);
  139. #endif