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 4.6KB

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