hex2_linker.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. /* Globals */
  21. FILE* output;
  22. struct entry** jump_tables;
  23. int BigEndian;
  24. int Base_Address;
  25. int Architecture;
  26. int ByteMode;
  27. int exec_enable;
  28. int ip;
  29. char* scratch;
  30. char* filename;
  31. int linenumber;
  32. int ALIGNED;
  33. /* For processing bytes */
  34. int hold;
  35. int toggle;
  36. void line_error()
  37. {
  38. fputs(filename, stderr);
  39. fputs(":", stderr);
  40. fputs(int2str(linenumber, 10, FALSE), stderr);
  41. fputs(" :", stderr);
  42. }
  43. int consume_token(FILE* source_file)
  44. {
  45. int i = 0;
  46. int c = fgetc(source_file);
  47. while(!in_set(c, " \t\n>"))
  48. {
  49. scratch[i] = c;
  50. i = i + 1;
  51. c = fgetc(source_file);
  52. require(max_string > i, "Consumed token exceeds length restriction\n");
  53. if(EOF == c) break;
  54. }
  55. return c;
  56. }
  57. int Throwaway_token(FILE* source_file)
  58. {
  59. int c;
  60. do
  61. {
  62. c = fgetc(source_file);
  63. if(EOF == c) break;
  64. } while(!in_set(c, " \t\n>"));
  65. return c;
  66. }
  67. int length(char* s)
  68. {
  69. int i = 0;
  70. while(0 != s[i]) i = i + 1;
  71. return i;
  72. }
  73. void Clear_Scratch(char* s)
  74. {
  75. do
  76. {
  77. s[0] = 0;
  78. s = s + 1;
  79. } while(0 != s[0]);
  80. }
  81. void Copy_String(char* a, char* b)
  82. {
  83. while(0 != a[0])
  84. {
  85. b[0] = a[0];
  86. a = a + 1;
  87. b = b + 1;
  88. }
  89. }
  90. int GetHash(char* s)
  91. {
  92. int i = 5381;
  93. while(0 != s[0])
  94. {
  95. i = i * 31 + s[0];
  96. s = s + 1;
  97. }
  98. return (i & 0xFFFF);
  99. }
  100. unsigned GetTarget(char* c)
  101. {
  102. struct entry* i;
  103. for(i = jump_tables[GetHash(c)]; NULL != i; i = i->next)
  104. {
  105. if(match(c, i->name))
  106. {
  107. return i->target;
  108. }
  109. }
  110. fputs("Target label ", stderr);
  111. fputs(c, stderr);
  112. fputs(" is not valid\n", stderr);
  113. exit(EXIT_FAILURE);
  114. }
  115. int storeLabel(FILE* source_file, int ip)
  116. {
  117. struct entry* entry = calloc(1, sizeof(struct entry));
  118. require(NULL != entry, "failed to allocate entry\n");
  119. /* Ensure we have target address */
  120. entry->target = ip;
  121. /* Store string */
  122. int c = consume_token(source_file);
  123. entry->name = calloc(length(scratch) + 1, sizeof(char));
  124. require(NULL != entry->name, "failed to allocate entry->name\n");
  125. Copy_String(scratch, entry->name);
  126. Clear_Scratch(scratch);
  127. /* Prepend to list */
  128. int h = GetHash(entry->name);
  129. entry->next = jump_tables[h];
  130. jump_tables[h] = entry;
  131. return c;
  132. }
  133. void range_check(int displacement, int number_of_bytes, int absolute)
  134. {
  135. if(4 == number_of_bytes) return;
  136. else if (absolute && (3 == number_of_bytes))
  137. {
  138. /* Deal with unsigned */
  139. if((16777215 < displacement) || (displacement < 0))
  140. {
  141. fputs("An absolute displacement of ", stderr);
  142. fputs(int2str(displacement, 10, TRUE), stderr);
  143. fputs(" does not fit in 3 bytes\n", stderr);
  144. exit(EXIT_FAILURE);
  145. }
  146. return;
  147. }
  148. else if (3 == number_of_bytes)
  149. {
  150. /* Deal with signed */
  151. if((8388607 < displacement) || (displacement < -8388608))
  152. {
  153. fputs("A relative displacement of ", stderr);
  154. fputs(int2str(displacement, 10, TRUE), stderr);
  155. fputs(" does not fit in 3 bytes\n", stderr);
  156. exit(EXIT_FAILURE);
  157. }
  158. return;
  159. }
  160. else if (absolute && (2 == number_of_bytes))
  161. {
  162. /* Deal with unsigned */
  163. if((65535 < displacement) || (displacement < 0))
  164. {
  165. fputs("An absolute displacement of ", stderr);
  166. fputs(int2str(displacement, 10, TRUE), stderr);
  167. fputs(" does not fit in 2 bytes\n", stderr);
  168. exit(EXIT_FAILURE);
  169. }
  170. return;
  171. }
  172. else if (2 == number_of_bytes)
  173. {
  174. /* Deal with signed */
  175. if((32767 < displacement) || (displacement < -32768))
  176. {
  177. fputs("A relative displacement of ", stderr);
  178. fputs(int2str(displacement, 10, TRUE), stderr);
  179. fputs(" does not fit in 2 bytes\n", stderr);
  180. exit(EXIT_FAILURE);
  181. }
  182. return;
  183. }
  184. else if (absolute && (1 == number_of_bytes))
  185. {
  186. /* Deal with unsigned */
  187. if((255 < displacement) || (displacement < 0))
  188. {
  189. fputs("An absolute displacement of ", stderr);
  190. fputs(int2str(displacement, 10, TRUE), stderr);
  191. fputs(" does not fit in 1 byte\n", stderr);
  192. exit(EXIT_FAILURE);
  193. }
  194. return;
  195. }
  196. else if (1 == number_of_bytes)
  197. {
  198. /* Deal with signed */
  199. if((127 < displacement) || (displacement < -128))
  200. {
  201. fputs("A relative displacement of ", stderr);
  202. fputs(int2str(displacement, 10, TRUE), stderr);
  203. fputs(" does not fit in 1 byte\n", stderr);
  204. exit(EXIT_FAILURE);
  205. }
  206. return;
  207. }
  208. fputs("Invalid number of bytes given\n", stderr);
  209. exit(EXIT_FAILURE);
  210. }
  211. void outputPointer(int displacement, int number_of_bytes, int absolute)
  212. {
  213. unsigned value = displacement;
  214. /* HALT HARD if we are going to do something BAD*/
  215. range_check(displacement, number_of_bytes, absolute);
  216. if(BigEndian)
  217. { /* Deal with BigEndian */
  218. if(4 == number_of_bytes) fputc((value >> 24), output);
  219. if(3 <= number_of_bytes) fputc(((value >> 16)%256), output);
  220. if(2 <= number_of_bytes) fputc(((value >> 8)%256), output);
  221. if(1 <= number_of_bytes) fputc((value % 256), output);
  222. }
  223. else
  224. { /* Deal with LittleEndian */
  225. unsigned byte;
  226. while(number_of_bytes > 0)
  227. {
  228. byte = value % 256;
  229. value = value / 256;
  230. fputc(byte, output);
  231. number_of_bytes = number_of_bytes - 1;
  232. }
  233. }
  234. }
  235. int Architectural_displacement(int target, int base)
  236. {
  237. if(KNIGHT == Architecture) return (target - base);
  238. else if(X86 == Architecture) return (target - base);
  239. else if(AMD64 == Architecture) return (target - base);
  240. else if(ALIGNED && (ARMV7L == Architecture))
  241. {
  242. ALIGNED = FALSE;
  243. /* Note: Branch displacements on ARM are in number of instructions to skip, basically. */
  244. if (target & 3)
  245. {
  246. line_error();
  247. fputs("error: Unaligned branch target: ", stderr);
  248. fputs(scratch, stderr);
  249. fputs(", aborting\n", stderr);
  250. exit(EXIT_FAILURE);
  251. }
  252. /*
  253. * The "fetch" stage already moved forward by 8 from the
  254. * beginning of the instruction because it is already
  255. * prefetching the next instruction.
  256. * Compensate for it by subtracting the space for
  257. * two instructions (including the branch instruction).
  258. * and the size of the aligned immediate.
  259. */
  260. return (((target - base + (base & 3)) >> 2) - 2);
  261. }
  262. else if(ARMV7L == Architecture)
  263. {
  264. /*
  265. * The size of the offset is 8 according to the spec but that value is
  266. * based on the end of the immediate, which the documentation gets wrong
  267. * and needs to be adjusted to the size of the immediate.
  268. * Eg 1byte immediate => -8 + 1 = -7
  269. */
  270. return ((target - base) - 8 + (3 & base));
  271. }
  272. else if(ALIGNED && (AARM64 == Architecture))
  273. {
  274. ALIGNED = FALSE;
  275. return (target - (~3 & base)) >> 2;
  276. }
  277. else if (AARM64 == Architecture)
  278. {
  279. return ((target - base) - 8 + (3 & base));
  280. }
  281. else if(ALIGNED && (PPC64LE == Architecture))
  282. {
  283. ALIGNED = FALSE;
  284. /* set Link register with branch */
  285. return (target - (base & 0xFFFFFFFC )) | 1;
  286. }
  287. else if(PPC64LE == Architecture)
  288. {
  289. /* DO *NOT* set link register with branch */
  290. return (target - (base & 0xFFFFFFFC));
  291. }
  292. else if(RISCV32 == Architecture || RISCV64 == Architecture) return (target - base);
  293. fputs("Unknown Architecture, aborting before harm is done\n", stderr);
  294. exit(EXIT_FAILURE);
  295. }
  296. void Update_Pointer(char ch)
  297. {
  298. /* Calculate pointer size*/
  299. if(in_set(ch, "%&")) ip = ip + 4; /* Deal with % and & */
  300. else if(in_set(ch, "@$")) ip = ip + 2; /* Deal with @ and $ */
  301. else if('~' == ch) ip = ip + 3; /* Deal with ~ */
  302. else if('!' == ch) ip = ip + 1; /* Deal with ! */
  303. else
  304. {
  305. line_error();
  306. fputs("storePointer given unknown\n", stderr);
  307. exit(EXIT_FAILURE);
  308. }
  309. }
  310. void storePointer(char ch, FILE* source_file)
  311. {
  312. /* Get string of pointer */
  313. Clear_Scratch(scratch);
  314. Update_Pointer(ch);
  315. int base_sep_p = consume_token(source_file);
  316. /* Lookup token */
  317. int target = GetTarget(scratch);
  318. int displacement;
  319. int base = ip;
  320. /* Change relative base address to :<base> */
  321. if ('>' == base_sep_p)
  322. {
  323. Clear_Scratch(scratch);
  324. consume_token (source_file);
  325. base = GetTarget (scratch);
  326. /* Force universality of behavior */
  327. displacement = (target - base);
  328. }
  329. else
  330. {
  331. displacement = Architectural_displacement(target, base);
  332. }
  333. /* output calculated difference */
  334. if('!' == ch) outputPointer(displacement, 1, FALSE); /* Deal with ! */
  335. else if('$' == ch) outputPointer(target, 2, TRUE); /* Deal with $ */
  336. else if('@' == ch) outputPointer(displacement, 2, FALSE); /* Deal with @ */
  337. else if('~' == ch) outputPointer(displacement, 3, FALSE); /* Deal with ~ */
  338. else if('&' == ch) outputPointer(target, 4, TRUE); /* Deal with & */
  339. else if('%' == ch) outputPointer(displacement, 4, FALSE); /* Deal with % */
  340. else
  341. {
  342. line_error();
  343. fputs("error: storePointer reached impossible case: ch=", stderr);
  344. fputc(ch, stderr);
  345. fputs("\n", stderr);
  346. exit(EXIT_FAILURE);
  347. }
  348. }
  349. void line_Comment(FILE* source_file)
  350. {
  351. int c = fgetc(source_file);
  352. while(!in_set(c, "\n\r"))
  353. {
  354. if(EOF == c) break;
  355. c = fgetc(source_file);
  356. }
  357. linenumber = linenumber + 1;
  358. }
  359. int hex(int c, FILE* source_file)
  360. {
  361. if (in_set(c, "0123456789")) return (c - 48);
  362. else if (in_set(c, "abcdef")) return (c - 87);
  363. else if (in_set(c, "ABCDEF")) return (c - 55);
  364. else if (in_set(c, "#;")) line_Comment(source_file);
  365. else if ('\n' == c) linenumber = linenumber + 1;
  366. return -1;
  367. }
  368. int octal(int c, FILE* source_file)
  369. {
  370. if (in_set(c, "01234567")) return (c - 48);
  371. else if (in_set(c, "#;")) line_Comment(source_file);
  372. else if ('\n' == c) linenumber = linenumber + 1;
  373. return -1;
  374. }
  375. int binary(int c, FILE* source_file)
  376. {
  377. if (in_set(c, "01")) return (c - 48);
  378. else if (in_set(c, "#;")) line_Comment(source_file);
  379. else if ('\n' == c) linenumber = linenumber + 1;
  380. return -1;
  381. }
  382. void process_byte(char c, FILE* source_file, int write)
  383. {
  384. if(HEX == ByteMode)
  385. {
  386. if(0 <= hex(c, source_file))
  387. {
  388. if(toggle)
  389. {
  390. if(write) fputc(((hold * 16)) + hex(c, source_file), output);
  391. ip = ip + 1;
  392. hold = 0;
  393. }
  394. else
  395. {
  396. hold = hex(c, source_file);
  397. }
  398. toggle = !toggle;
  399. }
  400. }
  401. else if(OCTAL ==ByteMode)
  402. {
  403. if(0 <= octal(c, source_file))
  404. {
  405. if(2 == toggle)
  406. {
  407. if(write) fputc(((hold * 8)) + octal(c, source_file), output);
  408. ip = ip + 1;
  409. hold = 0;
  410. toggle = 0;
  411. }
  412. else if(1 == toggle)
  413. {
  414. hold = ((hold * 8) + octal(c, source_file));
  415. toggle = 2;
  416. }
  417. else
  418. {
  419. hold = octal(c, source_file);
  420. toggle = 1;
  421. }
  422. }
  423. }
  424. else if(BINARY == ByteMode)
  425. {
  426. if(0 <= binary(c, source_file))
  427. {
  428. if(7 == toggle)
  429. {
  430. if(write) fputc((hold * 2) + binary(c, source_file), output);
  431. ip = ip + 1;
  432. hold = 0;
  433. toggle = 0;
  434. }
  435. else
  436. {
  437. hold = ((hold * 2) + binary(c, source_file));
  438. toggle = toggle + 1;
  439. }
  440. }
  441. }
  442. }
  443. void pad_to_align(int write)
  444. {
  445. if((ARMV7L == Architecture) || (AARM64 == Architecture) || (RISCV32 == Architecture) || (RISCV64 == Architecture))
  446. {
  447. if(1 == (ip & 0x1))
  448. {
  449. ip = ip + 1;
  450. if(write) fputc('\0', output);
  451. }
  452. if(2 == (ip & 0x2))
  453. {
  454. ip = ip + 2;
  455. if(write)
  456. {
  457. fputc('\0', output);
  458. fputc('\0', output);
  459. }
  460. }
  461. }
  462. }
  463. void first_pass(struct input_files* input)
  464. {
  465. if(NULL == input) return;
  466. first_pass(input->next);
  467. filename = input->filename;
  468. linenumber = 1;
  469. FILE* source_file = fopen(filename, "r");
  470. if(NULL == source_file)
  471. {
  472. fputs("The file: ", stderr);
  473. fputs(input->filename, stderr);
  474. fputs(" can not be opened!\n", stderr);
  475. exit(EXIT_FAILURE);
  476. }
  477. toggle = FALSE;
  478. int c;
  479. for(c = fgetc(source_file); EOF != c; c = fgetc(source_file))
  480. {
  481. /* Check for and deal with label */
  482. if(':' == c)
  483. {
  484. c = storeLabel(source_file, ip);
  485. }
  486. /* check for and deal with relative/absolute pointers to labels */
  487. if(in_set(c, "!@$~%&"))
  488. { /* deal with 1byte pointer !; 2byte pointers (@ and $); 3byte pointers ~; 4byte pointers (% and &) */
  489. Update_Pointer(c);
  490. c = Throwaway_token(source_file);
  491. if ('>' == c)
  492. { /* deal with label>base */
  493. c = Throwaway_token(source_file);
  494. }
  495. }
  496. else if('<' == c)
  497. {
  498. pad_to_align(FALSE);
  499. }
  500. else if('^' == c)
  501. {
  502. /* Just ignore */
  503. continue;
  504. }
  505. else process_byte(c, source_file, FALSE);
  506. }
  507. fclose(source_file);
  508. }
  509. void second_pass(struct input_files* input)
  510. {
  511. if(NULL == input) return;
  512. second_pass(input->next);
  513. filename = input->filename;
  514. linenumber = 1;
  515. FILE* source_file = fopen(filename, "r");
  516. /* Something that should never happen */
  517. if(NULL == source_file)
  518. {
  519. fputs("The file: ", stderr);
  520. fputs(input->filename, stderr);
  521. fputs(" can not be opened!\nWTF-pass2\n", stderr);
  522. exit(EXIT_FAILURE);
  523. }
  524. toggle = FALSE;
  525. hold = 0;
  526. int c;
  527. for(c = fgetc(source_file); EOF != c; c = fgetc(source_file))
  528. {
  529. if(':' == c) c = Throwaway_token(source_file); /* Deal with : */
  530. else if(in_set(c, "!@$~%&")) storePointer(c, source_file); /* Deal with !, @, $, ~, % and & */
  531. else if('<' == c) pad_to_align(TRUE);
  532. else if('^' == c) ALIGNED = TRUE;
  533. else process_byte(c, source_file, TRUE);
  534. }
  535. fclose(source_file);
  536. }