hex2_linker.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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 <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/stat.h>
  24. #define max_string 4096
  25. //CONSTANT max_string 4096
  26. #define TRUE 1
  27. //CONSTANT TRUE 1
  28. #define FALSE 0
  29. //CONSTANT FALSE 0
  30. void file_print(char* s, FILE* f);
  31. int match(char* a, char* b);
  32. char* numerate_number(int a);
  33. int numerate_string(char *a);
  34. int in_set(int c, char* s);
  35. struct input_files
  36. {
  37. struct input_files* next;
  38. char* filename;
  39. };
  40. struct entry
  41. {
  42. struct entry* next;
  43. unsigned target;
  44. char* name;
  45. };
  46. FILE* output;
  47. struct entry* jump_table;
  48. int BigEndian;
  49. int Base_Address;
  50. int Architecture;
  51. int ByteMode;
  52. int exec_enable;
  53. int ip;
  54. char* scratch;
  55. char* filename;
  56. int linenumber;
  57. int ALIGNED;
  58. void line_error()
  59. {
  60. file_print(filename, stderr);
  61. file_print(":", stderr);
  62. file_print(numerate_number(linenumber), stderr);
  63. file_print(" :", stderr);
  64. }
  65. int consume_token(FILE* source_file)
  66. {
  67. int i = 0;
  68. int c = fgetc(source_file);
  69. while(!in_set(c, " \t\n>"))
  70. {
  71. scratch[i] = c;
  72. i = i + 1;
  73. c = fgetc(source_file);
  74. }
  75. return c;
  76. }
  77. int Throwaway_token(FILE* source_file)
  78. {
  79. int c;
  80. do
  81. {
  82. c = fgetc(source_file);
  83. } while(!in_set(c, " \t\n>"));
  84. return c;
  85. }
  86. int length(char* s)
  87. {
  88. int i = 0;
  89. while(0 != s[i]) i = i + 1;
  90. return i;
  91. }
  92. void Clear_Scratch(char* s)
  93. {
  94. do
  95. {
  96. s[0] = 0;
  97. s = s + 1;
  98. } while(0 != s[0]);
  99. }
  100. void Copy_String(char* a, char* b)
  101. {
  102. while(0 != a[0])
  103. {
  104. b[0] = a[0];
  105. a = a + 1;
  106. b = b + 1;
  107. }
  108. }
  109. unsigned GetTarget(char* c)
  110. {
  111. struct entry* i;
  112. for(i = jump_table; NULL != i; i = i->next)
  113. {
  114. if(match(c, i->name))
  115. {
  116. return i->target;
  117. }
  118. }
  119. file_print("Target label ", stderr);
  120. file_print(c, stderr);
  121. file_print(" is not valid\n", stderr);
  122. exit(EXIT_FAILURE);
  123. }
  124. int storeLabel(FILE* source_file, int ip)
  125. {
  126. struct entry* entry = calloc(1, sizeof(struct entry));
  127. /* Ensure we have target address */
  128. entry->target = ip;
  129. /* Prepend to list */
  130. entry->next = jump_table;
  131. jump_table = entry;
  132. /* Store string */
  133. int c = consume_token(source_file);
  134. entry->name = calloc(length(scratch) + 1, sizeof(char));
  135. Copy_String(scratch, entry->name);
  136. Clear_Scratch(scratch);
  137. return c;
  138. }
  139. void range_check(int displacement, int number_of_bytes)
  140. {
  141. if(4 == number_of_bytes) return;
  142. else if (3 == number_of_bytes)
  143. {
  144. if((8388607 < displacement) || (displacement < -8388608))
  145. {
  146. file_print("A displacement of ", stderr);
  147. file_print(numerate_number(displacement), stderr);
  148. file_print(" does not fit in 3 bytes\n", stderr);
  149. exit(EXIT_FAILURE);
  150. }
  151. return;
  152. }
  153. else if (2 == number_of_bytes)
  154. {
  155. if((32767 < displacement) || (displacement < -32768))
  156. {
  157. file_print("A displacement of ", stderr);
  158. file_print(numerate_number(displacement), stderr);
  159. file_print(" does not fit in 2 bytes\n", stderr);
  160. exit(EXIT_FAILURE);
  161. }
  162. return;
  163. }
  164. else if (1 == number_of_bytes)
  165. {
  166. if((127 < displacement) || (displacement < -128))
  167. {
  168. file_print("A displacement of ", stderr);
  169. file_print(numerate_number(displacement), stderr);
  170. file_print(" does not fit in 1 byte\n", stderr);
  171. exit(EXIT_FAILURE);
  172. }
  173. return;
  174. }
  175. file_print("Invalid number of bytes given\n", stderr);
  176. exit(EXIT_FAILURE);
  177. }
  178. void outputPointer(int displacement, int number_of_bytes)
  179. {
  180. unsigned value = displacement;
  181. /* HALT HARD if we are going to do something BAD*/
  182. range_check(displacement, number_of_bytes);
  183. if(BigEndian)
  184. { /* Deal with BigEndian */
  185. if(4 == number_of_bytes) fputc((value >> 24), output);
  186. if(3 <= number_of_bytes) fputc(((value >> 16)%256), output);
  187. if(2 <= number_of_bytes) fputc(((value >> 8)%256), output);
  188. if(1 <= number_of_bytes) fputc((value % 256), output);
  189. }
  190. else
  191. { /* Deal with LittleEndian */
  192. unsigned byte;
  193. while(number_of_bytes > 0)
  194. {
  195. byte = value % 256;
  196. value = value / 256;
  197. fputc(byte, output);
  198. number_of_bytes = number_of_bytes - 1;
  199. }
  200. }
  201. }
  202. int Architectural_displacement(int target, int base)
  203. {
  204. if(0 == Architecture) return (target - base);
  205. else if(1 == Architecture) return (target - base);
  206. else if(2 == Architecture) return (target - base);
  207. else if(ALIGNED && (40 == Architecture))
  208. {
  209. ALIGNED = FALSE;
  210. /* Note: Branch displacements on ARM are in number of instructions to skip, basically. */
  211. if (target & 3)
  212. {
  213. line_error();
  214. file_print("error: Unaligned branch target: ", stderr);
  215. file_print(scratch, stderr);
  216. file_print(", aborting\n", stderr);
  217. exit(EXIT_FAILURE);
  218. }
  219. /*
  220. * The "fetch" stage already moved forward by 8 from the
  221. * beginning of the instruction because it is already
  222. * prefetching the next instruction.
  223. * Compensate for it by subtracting the space for
  224. * two instructions (including the branch instruction).
  225. * and the size of the aligned immediate.
  226. */
  227. return (((target - base + (base & 3)) >> 2) - 2);
  228. }
  229. else if(40 == Architecture)
  230. {
  231. /*
  232. * The size of the offset is 8 according to the spec but that value is
  233. * based on the end of the immediate, which the documentation gets wrong
  234. * and needs to be adjusted to the size of the immediate.
  235. * Eg 1byte immediate => -8 + 1 = -7
  236. */
  237. return ((target - base) - 8 + (3 & base));
  238. }
  239. file_print("Unknown Architecture, aborting before harm is done\n", stderr);
  240. exit(EXIT_FAILURE);
  241. }
  242. void Update_Pointer(char ch)
  243. {
  244. /* Calculate pointer size*/
  245. if(in_set(ch, "%&")) ip = ip + 4; /* Deal with % and & */
  246. else if(in_set(ch, "@$")) ip = ip + 2; /* Deal with @ and $ */
  247. else if('~' == ch) ip = ip + 3; /* Deal with ~ */
  248. else if('!' == ch) ip = ip + 1; /* Deal with ! */
  249. else
  250. {
  251. line_error();
  252. file_print("storePointer given unknown\n", stderr);
  253. exit(EXIT_FAILURE);
  254. }
  255. }
  256. void storePointer(char ch, FILE* source_file)
  257. {
  258. /* Get string of pointer */
  259. Clear_Scratch(scratch);
  260. Update_Pointer(ch);
  261. int base_sep_p = consume_token(source_file);
  262. /* Lookup token */
  263. int target = GetTarget(scratch);
  264. int displacement;
  265. int base = ip;
  266. /* Change relative base address to :<base> */
  267. if ('>' == base_sep_p)
  268. {
  269. Clear_Scratch(scratch);
  270. consume_token (source_file);
  271. base = GetTarget (scratch);
  272. /* Force universality of behavior */
  273. displacement = (target - base);
  274. }
  275. else
  276. {
  277. displacement = Architectural_displacement(target, base);
  278. }
  279. /* output calculated difference */
  280. if('!' == ch) outputPointer(displacement, 1); /* Deal with ! */
  281. else if('$' == ch) outputPointer(target, 2); /* Deal with $ */
  282. else if('@' == ch) outputPointer(displacement, 2); /* Deal with @ */
  283. else if('~' == ch) outputPointer(displacement, 3); /* Deal with ~ */
  284. else if('&' == ch) outputPointer(target, 4); /* Deal with & */
  285. else if('%' == ch) outputPointer(displacement, 4); /* Deal with % */
  286. else
  287. {
  288. line_error();
  289. file_print("error: storePointer reached impossible case: ch=", stderr);
  290. fputc(ch, stderr);
  291. file_print("\n", stderr);
  292. exit(EXIT_FAILURE);
  293. }
  294. }
  295. void line_Comment(FILE* source_file)
  296. {
  297. int c = fgetc(source_file);
  298. while(!in_set(c, "\n\r"))
  299. {
  300. c = fgetc(source_file);
  301. }
  302. linenumber = linenumber + 1;
  303. }
  304. int hex(int c, FILE* source_file)
  305. {
  306. if (in_set(c, "0123456789")) return (c - 48);
  307. else if (in_set(c, "abcdef")) return (c - 87);
  308. else if (in_set(c, "ABCDEF")) return (c - 55);
  309. else if (in_set(c, "#;")) line_Comment(source_file);
  310. else if ('\n' == c) linenumber = linenumber + 1;
  311. return -1;
  312. }
  313. int octal(int c, FILE* source_file)
  314. {
  315. if (in_set(c, "01234567")) return (c - 48);
  316. else if (in_set(c, "#;")) line_Comment(source_file);
  317. else if ('\n' == c) linenumber = linenumber + 1;
  318. return -1;
  319. }
  320. int binary(int c, FILE* source_file)
  321. {
  322. if (in_set(c, "01")) return (c - 48);
  323. else if (in_set(c, "#;")) line_Comment(source_file);
  324. else if ('\n' == c) linenumber = linenumber + 1;
  325. return -1;
  326. }
  327. int hold;
  328. int toggle;
  329. void process_byte(char c, FILE* source_file, int write)
  330. {
  331. if(16 == ByteMode)
  332. {
  333. if(0 <= hex(c, source_file))
  334. {
  335. if(toggle)
  336. {
  337. if(write) fputc(((hold * 16)) + hex(c, source_file), output);
  338. ip = ip + 1;
  339. hold = 0;
  340. }
  341. else
  342. {
  343. hold = hex(c, source_file);
  344. }
  345. toggle = !toggle;
  346. }
  347. }
  348. else if(8 ==ByteMode)
  349. {
  350. if(0 <= octal(c, source_file))
  351. {
  352. if(2 == toggle)
  353. {
  354. if(write) fputc(((hold * 8)) + octal(c, source_file), output);
  355. ip = ip + 1;
  356. hold = 0;
  357. toggle = 0;
  358. }
  359. else if(1 == toggle)
  360. {
  361. hold = ((hold * 8) + octal(c, source_file));
  362. toggle = 2;
  363. }
  364. else
  365. {
  366. hold = octal(c, source_file);
  367. toggle = 1;
  368. }
  369. }
  370. }
  371. else if(2 == ByteMode)
  372. {
  373. if(0 <= binary(c, source_file))
  374. {
  375. if(7 == toggle)
  376. {
  377. if(write) fputc((hold * 2) + binary(c, source_file), output);
  378. ip = ip + 1;
  379. hold = 0;
  380. toggle = 0;
  381. }
  382. else
  383. {
  384. hold = ((hold * 2) + binary(c, source_file));
  385. toggle = toggle + 1;
  386. }
  387. }
  388. }
  389. }
  390. void pad_to_align(int write)
  391. {
  392. if(40 == Architecture)
  393. {
  394. if(1 == (ip & 0x1))
  395. {
  396. ip = ip + 1;
  397. if(write) fputc('\0', output);
  398. }
  399. if(2 == (ip & 0x2))
  400. {
  401. ip = ip + 2;
  402. if(write)
  403. {
  404. fputc('\0', output);
  405. fputc('\0', output);
  406. }
  407. }
  408. }
  409. }
  410. void first_pass(struct input_files* input)
  411. {
  412. if(NULL == input) return;
  413. first_pass(input->next);
  414. filename = input->filename;
  415. linenumber = 1;
  416. FILE* source_file = fopen(filename, "r");
  417. if(NULL == source_file)
  418. {
  419. file_print("The file: ", stderr);
  420. file_print(input->filename, stderr);
  421. file_print(" can not be opened!\n", stderr);
  422. exit(EXIT_FAILURE);
  423. }
  424. toggle = FALSE;
  425. int c;
  426. for(c = fgetc(source_file); EOF != c; c = fgetc(source_file))
  427. {
  428. /* Check for and deal with label */
  429. if(':' == c)
  430. {
  431. c = storeLabel(source_file, ip);
  432. }
  433. /* check for and deal with relative/absolute pointers to labels */
  434. if(in_set(c, "!@$~%&"))
  435. { /* deal with 1byte pointer !; 2byte pointers (@ and $); 3byte pointers ~; 4byte pointers (% and &) */
  436. Update_Pointer(c);
  437. c = Throwaway_token(source_file);
  438. if ('>' == c)
  439. { /* deal with label>base */
  440. c = Throwaway_token(source_file);
  441. }
  442. }
  443. else if('<' == c)
  444. {
  445. pad_to_align(FALSE);
  446. }
  447. else if('^' == c)
  448. {
  449. /* Just ignore */
  450. continue;
  451. }
  452. else process_byte(c, source_file, FALSE);
  453. }
  454. fclose(source_file);
  455. }
  456. void second_pass(struct input_files* input)
  457. {
  458. if(NULL == input) return;
  459. second_pass(input->next);
  460. filename = input->filename;
  461. linenumber = 1;
  462. FILE* source_file = fopen(filename, "r");
  463. /* Something that should never happen */
  464. if(NULL == source_file)
  465. {
  466. file_print("The file: ", stderr);
  467. file_print(input->filename, stderr);
  468. file_print(" can not be opened!\nWTF-pass2\n", stderr);
  469. exit(EXIT_FAILURE);
  470. }
  471. toggle = FALSE;
  472. hold = 0;
  473. int c;
  474. for(c = fgetc(source_file); EOF != c; c = fgetc(source_file))
  475. {
  476. if(':' == c) c = Throwaway_token(source_file); /* Deal with : */
  477. else if(in_set(c, "!@$~%&")) storePointer(c, source_file); /* Deal with !, @, $, ~, % and & */
  478. else if('<' == c) pad_to_align(TRUE);
  479. else if('^' == c) ALIGNED = TRUE;
  480. else process_byte(c, source_file, TRUE);
  481. }
  482. fclose(source_file);
  483. }
  484. /* Standard C main program */
  485. int main(int argc, char **argv)
  486. {
  487. ALIGNED = FALSE;
  488. BigEndian = TRUE;
  489. jump_table = NULL;
  490. Architecture = 0;
  491. Base_Address = 0;
  492. struct input_files* input = NULL;
  493. output = stdout;
  494. char* output_file = "";
  495. exec_enable = FALSE;
  496. ByteMode = 16;
  497. scratch = calloc(max_string + 1, sizeof(char));
  498. char* arch;
  499. struct input_files* temp;
  500. int option_index = 1;
  501. while(option_index <= argc)
  502. {
  503. if(NULL == argv[option_index])
  504. {
  505. option_index = option_index + 1;
  506. }
  507. else if(match(argv[option_index], "--BigEndian"))
  508. {
  509. BigEndian = TRUE;
  510. option_index = option_index + 1;
  511. }
  512. else if(match(argv[option_index], "--LittleEndian"))
  513. {
  514. BigEndian = FALSE;
  515. option_index = option_index + 1;
  516. }
  517. else if(match(argv[option_index], "--exec_enable"))
  518. {
  519. exec_enable = TRUE;
  520. option_index = option_index + 1;
  521. }
  522. else if(match(argv[option_index], "-A") || match(argv[option_index], "--architecture"))
  523. {
  524. arch = argv[option_index + 1];
  525. if(match("knight-native", arch) || match("knight-posix", arch)) Architecture = 0;
  526. else if(match("x86", arch)) Architecture = 1;
  527. else if(match("amd64", arch)) Architecture = 2;
  528. else if(match("armv7l", arch)) Architecture = 40;
  529. else
  530. {
  531. file_print("Unknown architecture: ", stderr);
  532. file_print(arch, stderr);
  533. file_print(" know values are: knight-native, knight-posix, x86, amd64 and armv7l", stderr);
  534. }
  535. option_index = option_index + 2;
  536. }
  537. else if(match(argv[option_index], "-b") || match(argv[option_index], "--binary"))
  538. {
  539. ByteMode = 2;
  540. option_index = option_index + 1;
  541. }
  542. else if(match(argv[option_index], "-B") || match(argv[option_index], "--BaseAddress"))
  543. {
  544. Base_Address = numerate_string(argv[option_index + 1]);
  545. option_index = option_index + 2;
  546. }
  547. else if(match(argv[option_index], "-h") || match(argv[option_index], "--help"))
  548. {
  549. file_print("Usage: ", stderr);
  550. file_print(argv[0], stderr);
  551. file_print(" -f FILENAME1 {-f FILENAME2} (--BigEndian|--LittleEndian)", stderr);
  552. file_print(" [--BaseAddress 12345] [--architecture name]\nArchitecture", stderr);
  553. file_print(" knight-native, knight-posix, x86, amd64 and armv7\n", stderr);
  554. file_print("To leverage octal or binary input: --octal, --binary\n", stderr);
  555. exit(EXIT_SUCCESS);
  556. }
  557. else if(match(argv[option_index], "-f") || match(argv[option_index], "--file"))
  558. {
  559. temp = calloc(1, sizeof(struct input_files));
  560. temp->filename = argv[option_index + 1];
  561. temp->next = input;
  562. input = temp;
  563. option_index = option_index + 2;
  564. }
  565. else if(match(argv[option_index], "-o") || match(argv[option_index], "--output"))
  566. {
  567. output_file = argv[option_index + 1];
  568. output = fopen(output_file, "w");
  569. if(NULL == output)
  570. {
  571. file_print("The file: ", stderr);
  572. file_print(argv[option_index + 1], stderr);
  573. file_print(" can not be opened!\n", stderr);
  574. exit(EXIT_FAILURE);
  575. }
  576. option_index = option_index + 2;
  577. }
  578. else if(match(argv[option_index], "-O") || match(argv[option_index], "--octal"))
  579. {
  580. ByteMode = 8;
  581. option_index = option_index + 1;
  582. }
  583. else if(match(argv[option_index], "-V") || match(argv[option_index], "--version"))
  584. {
  585. file_print("hex2 0.3\n", stdout);
  586. exit(EXIT_SUCCESS);
  587. }
  588. else
  589. {
  590. file_print("Unknown option\n", stderr);
  591. exit(EXIT_FAILURE);
  592. }
  593. }
  594. /* Make sure we have a program tape to run */
  595. if (NULL == input)
  596. {
  597. return EXIT_FAILURE;
  598. }
  599. /* Get all of the labels */
  600. ip = Base_Address;
  601. first_pass(input);
  602. /* Fix all the references*/
  603. ip = Base_Address;
  604. second_pass(input);
  605. /* Close output file */
  606. if (output != stdout) {
  607. fclose(output);
  608. }
  609. /* Set file as executable */
  610. if(exec_enable)
  611. {
  612. /* 488 = 750 in octal */
  613. if(0 != chmod(output_file, 488))
  614. {
  615. file_print("Unable to change permissions\n", stderr);
  616. exit(EXIT_FAILURE);
  617. }
  618. }
  619. return EXIT_SUCCESS;
  620. }