hex2_linker.c 16 KB

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