hex2_linker.c 15 KB

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