hex2.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* -*- c-file-style: "linux";indent-tabs-mode:t -*- */
  2. /* Copyright (C) 2017 Jeremiah Orians
  3. * Copyright (C) 2017 Jan Nieuwenhuizen <janneke@gnu.org>
  4. * This file is part of mescc-tools
  5. *
  6. * mescc-tools is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * mescc-tools is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with mescc-tools. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "hex2_globals.h"
  20. /* The essential functions */
  21. void first_pass(struct input_files* input);
  22. void second_pass(struct input_files* input);
  23. void WordFirstPass(struct input_files* input);
  24. void WordSecondPass(struct input_files* input);
  25. /* Standard C main program */
  26. int main(int argc, char **argv)
  27. {
  28. int InsaneArchitecture = FALSE;
  29. ALIGNED = FALSE;
  30. BigEndian = TRUE;
  31. jump_tables = calloc(65537, sizeof(struct entry*));
  32. require(NULL != jump_tables, "Failed to allocate our jump_tables\n");
  33. Architecture = KNIGHT;
  34. Base_Address = 0;
  35. struct input_files* input = NULL;
  36. output = stdout;
  37. char* output_file = "";
  38. exec_enable = TRUE;
  39. ByteMode = HEX;
  40. scratch = calloc(max_string + 1, sizeof(char));
  41. require(NULL != scratch, "failed to allocate our scratch buffer\n");
  42. char* arch;
  43. struct input_files* temp;
  44. int option_index = 1;
  45. while(option_index <= argc)
  46. {
  47. if(NULL == argv[option_index])
  48. {
  49. option_index = option_index + 1;
  50. }
  51. else if(match(argv[option_index], "--big-endian"))
  52. {
  53. BigEndian = TRUE;
  54. option_index = option_index + 1;
  55. }
  56. else if(match(argv[option_index], "--little-endian"))
  57. {
  58. BigEndian = FALSE;
  59. option_index = option_index + 1;
  60. }
  61. else if(match(argv[option_index], "--non-executable"))
  62. {
  63. exec_enable = FALSE;
  64. option_index = option_index + 1;
  65. }
  66. else if(match(argv[option_index], "-A") || match(argv[option_index], "--architecture"))
  67. {
  68. arch = argv[option_index + 1];
  69. if(match("knight-native", arch) || match("knight-posix", arch)) Architecture = KNIGHT;
  70. else if(match("x86", arch)) Architecture = X86;
  71. else if(match("amd64", arch)) Architecture = AMD64;
  72. else if(match("armv7l", arch)) Architecture = ARMV7L;
  73. else if(match("aarch64", arch)) Architecture = AARM64;
  74. else if(match("ppc64le", arch)) Architecture = PPC64LE;
  75. else if(match("riscv32", arch)) Architecture = RISCV32;
  76. else if(match("riscv64", arch)) Architecture = RISCV64;
  77. else
  78. {
  79. fputs("Unknown architecture: ", stderr);
  80. fputs(arch, stderr);
  81. fputs(" know values are: knight-native, knight-posix, x86, amd64, armv7l, riscv32 and riscv64", stderr);
  82. }
  83. option_index = option_index + 2;
  84. }
  85. else if(match(argv[option_index], "-b") || match(argv[option_index], "--binary"))
  86. {
  87. ByteMode = BINARY;
  88. option_index = option_index + 1;
  89. }
  90. else if(match(argv[option_index], "-B") || match(argv[option_index], "--base-address"))
  91. {
  92. Base_Address = strtoint(argv[option_index + 1]);
  93. option_index = option_index + 2;
  94. }
  95. else if(match(argv[option_index], "-h") || match(argv[option_index], "--help"))
  96. {
  97. fputs("Usage: ", stderr);
  98. fputs(argv[0], stderr);
  99. fputs(" --file FILENAME1 {-f FILENAME2} (--big-endian|--little-endian)", stderr);
  100. fputs(" [--base-address 0x12345] [--architecture name]\nArchitecture:", stderr);
  101. fputs(" knight-native, knight-posix, x86, amd64, armv7l, aarch64, riscv32 and riscv64\n", stderr);
  102. fputs("To leverage octal or binary input: --octal, --binary\n", stderr);
  103. exit(EXIT_SUCCESS);
  104. }
  105. else if(match(argv[option_index], "-f") || match(argv[option_index], "--file"))
  106. {
  107. temp = calloc(1, sizeof(struct input_files));
  108. require(NULL != temp, "failed to allocate file for processing\n");
  109. temp->filename = argv[option_index + 1];
  110. temp->next = input;
  111. input = temp;
  112. option_index = option_index + 2;
  113. }
  114. else if(match(argv[option_index], "-o") || match(argv[option_index], "--output"))
  115. {
  116. output_file = argv[option_index + 1];
  117. output = fopen(output_file, "w");
  118. if(NULL == output)
  119. {
  120. fputs("The file: ", stderr);
  121. fputs(argv[option_index + 1], stderr);
  122. fputs(" can not be opened!\n", stderr);
  123. exit(EXIT_FAILURE);
  124. }
  125. option_index = option_index + 2;
  126. }
  127. else if(match(argv[option_index], "-O") || match(argv[option_index], "--octal"))
  128. {
  129. ByteMode = OCTAL;
  130. option_index = option_index + 1;
  131. }
  132. else if(match(argv[option_index], "-V") || match(argv[option_index], "--version"))
  133. {
  134. fputs("hex2 1.5.0\n", stdout);
  135. exit(EXIT_SUCCESS);
  136. }
  137. else
  138. {
  139. fputs("Unknown option\n", stderr);
  140. exit(EXIT_FAILURE);
  141. }
  142. }
  143. if((Architecture == RISCV32) || (Architecture == RISCV64))
  144. {
  145. /* Forcing me to use words instead of just byting into the problem */
  146. InsaneArchitecture = TRUE;
  147. }
  148. /* Catch a common mistake */
  149. if((KNIGHT != Architecture) && (0 == Base_Address))
  150. {
  151. fputs(">> WARNING <<\n>> WARNING <<\n>> WARNING <<\n", stderr);
  152. fputs("If you are not generating a ROM image this binary will likely not work\n", stderr);
  153. }
  154. /* Catch implicitly false assumptions */
  155. if(BigEndian && ((X86 == Architecture) || ( AMD64 == Architecture) || (ARMV7L == Architecture) || (AARM64 == Architecture) || (RISCV32 == Architecture) || (RISCV64 == Architecture)))
  156. {
  157. fputs(">> WARNING <<\n>> WARNING <<\n>> WARNING <<\n", stderr);
  158. fputs("You have specified big endian output on likely a little endian processor\n", stderr);
  159. fputs("if this is a mistake please pass --little-endian next time\n", stderr);
  160. }
  161. /* Make sure we have a program tape to run */
  162. if (NULL == input)
  163. {
  164. return EXIT_FAILURE;
  165. }
  166. /* Get all of the labels */
  167. ip = Base_Address;
  168. if(InsaneArchitecture) WordFirstPass(input);
  169. else first_pass(input);
  170. /* Fix all the references*/
  171. ip = Base_Address;
  172. if(InsaneArchitecture) WordSecondPass(input);
  173. else second_pass(input);
  174. /* flush all writes */
  175. fflush(output);
  176. /* Set file as executable */
  177. if(exec_enable && (output != stdout))
  178. {
  179. /* Close output file */
  180. fclose(output);
  181. if(0 != chmod(output_file, 0750))
  182. {
  183. fputs("Unable to change permissions\n", stderr);
  184. exit(EXIT_FAILURE);
  185. }
  186. }
  187. return EXIT_SUCCESS;
  188. }