aelflod.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * $Source$
  3. * $State$
  4. *
  5. * Simple tool to produce an utterly basic ELF executable
  6. * from an absolute ack.out file. Suitable for operating
  7. * systems like Linux.
  8. *
  9. * This tool produces an executable with a program header
  10. * only and no section header.
  11. *
  12. * Mostly pinched from the ARM cv (and then rewritten in
  13. * ANSI C). Which, according to the comment, was pinched
  14. * from m68k2; therefore I am merely continuing a time-
  15. * honoured tradition.
  16. *
  17. * (I was 10 when the original for this was checked into
  18. * CVS...)
  19. *
  20. * dtrg, 2006-10-17
  21. */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include "../../modules/src/object/obj.h"
  29. #include <stdarg.h>
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include "out.h"
  33. #define ASSERT(x) switch (2) { case 0: case (x): ; }
  34. /* Global settings. */
  35. int bigendian = 0;
  36. int elfmachine;
  37. /* Header and section table of an ack object file. */
  38. struct outhead outhead;
  39. struct outsect outsect[S_MAX];
  40. char* stringarea;
  41. char* outputfile = NULL; /* Name of output file, or NULL */
  42. char* program; /* Name of current program: argv[0] */
  43. FILE* input; /* Input stream */
  44. FILE* output; /* Output stream */
  45. #define readf(a, b, c) fread((a), (b), (int)(c), input)
  46. #define writef(a, b, c) fwrite((a), (b), (int)(c), output)
  47. /* Header and program header table of an ELF object file. */
  48. #define ELF_HEADER_SIZE 0x34
  49. #define PROGRAM_HEADER_SIZE 0x20
  50. #define PROGRAM_HEADER_COUNT 1
  51. unsigned long codeoffset;
  52. const char elf_le_ident_string[] = {
  53. 0x7F, 'E', 'L', 'F'
  54. };
  55. /* Output file definitions and such */
  56. #define HDR_LENGTH 32
  57. char hdr[HDR_LENGTH] ;
  58. /* Segment numbers understood by aelflod. */
  59. enum {
  60. TEXT = 0,
  61. ROM,
  62. DATA,
  63. BSS,
  64. NUM_SEGMENTS
  65. };
  66. #define N_EXT 040
  67. #define N_UNDEF 00
  68. #define N_ABS 01
  69. #define N_TEXT 02
  70. #define N_DATA 03
  71. #define N_BSS 04
  72. #define N_FN 037
  73. /* Produce an error message and exit. */
  74. void fatal(const char* s, ...)
  75. {
  76. va_list ap;
  77. fprintf(stderr, "%s: ",program) ;
  78. va_start(ap, s);
  79. vfprintf(stderr, s, ap);
  80. va_end(ap);
  81. fprintf(stderr, "\n");
  82. if (outputfile)
  83. unlink(outputfile);
  84. exit(1);
  85. }
  86. /* Calculate the result of a aligned to b (rounding up if necessary).
  87. * b must be a power of two. */
  88. long align(long a, long b)
  89. {
  90. a += b - 1;
  91. return a & ~(b-1);
  92. }
  93. int follows(struct outsect* pa, struct outsect* pb)
  94. {
  95. /* return 1 if pa follows pb */
  96. return (pa->os_base == align(pb->os_base+pb->os_size, pa->os_lign));
  97. }
  98. /* Writes a byte. */
  99. void emit8(unsigned char value)
  100. {
  101. writef(&value, 1, 1);
  102. }
  103. /* Writes out 16 and 32 bit words in the appropriate endianness. */
  104. void emit16(unsigned short value)
  105. {
  106. unsigned char buffer[2];
  107. if (bigendian)
  108. {
  109. buffer[0] = (value >> 8) & 0xFF;
  110. buffer[1] = (value >> 0) & 0xFF;
  111. }
  112. else
  113. {
  114. buffer[1] = (value >> 8) & 0xFF;
  115. buffer[0] = (value >> 0) & 0xFF;
  116. }
  117. writef(buffer, 1, sizeof(buffer));
  118. }
  119. void emit32(unsigned long value)
  120. {
  121. unsigned char buffer[4];
  122. if (bigendian)
  123. {
  124. buffer[0] = (value >> 24) & 0xFF;
  125. buffer[1] = (value >> 16) & 0xFF;
  126. buffer[2] = (value >> 8) & 0xFF;
  127. buffer[3] = (value >> 0) & 0xFF;
  128. }
  129. else
  130. {
  131. buffer[3] = (value >> 24) & 0xFF;
  132. buffer[2] = (value >> 16) & 0xFF;
  133. buffer[1] = (value >> 8) & 0xFF;
  134. buffer[0] = (value >> 0) & 0xFF;
  135. }
  136. writef(buffer, 1, sizeof(buffer));
  137. }
  138. /* Copies the contents of a section from the input stream
  139. * to the output stream. */
  140. void emits(struct outsect* section)
  141. {
  142. char buffer[BUFSIZ];
  143. long n = section->os_flen;
  144. while (n > 0)
  145. {
  146. int blocksize = (n > BUFSIZ) ? BUFSIZ : n;
  147. readf(buffer, 1, blocksize);
  148. writef(buffer, 1, blocksize);
  149. n -= blocksize;
  150. }
  151. /* Zero fill any remaining space. */
  152. if (section->os_flen != section->os_size)
  153. {
  154. long n = section->os_size - section->os_flen;
  155. memset(buffer, 0, BUFSIZ);
  156. while (n > 0)
  157. {
  158. int blocksize = (n > BUFSIZ) ? BUFSIZ : n;
  159. writef(buffer, 1, blocksize);
  160. n -= blocksize;
  161. }
  162. }
  163. }
  164. /* Writes out an ELF program header. */
  165. void emitphdr(unsigned long address, unsigned long filesize,
  166. unsigned int memsize, unsigned int alignment, int flags)
  167. {
  168. static unsigned long fileoffset = 0;
  169. emit32(1); /* type = PT_LOAD */
  170. emit32(fileoffset); /* file offset */
  171. emit32(address); /* virtual address */
  172. emit32(0); /* physical address */
  173. emit32(filesize); /* file size */
  174. emit32(memsize); /* memory size */
  175. emit32(flags); /* executable, readable, writable */
  176. emit32(alignment); /* alignment */
  177. fileoffset += filesize;
  178. }
  179. /* Read the ack.out file header. */
  180. int rhead(FILE* f, struct outhead* head)
  181. {
  182. char buf[SZ_HEAD], *c;
  183. if (fread(buf, sizeof(buf), 1, f) != 1)
  184. return 0;
  185. c = buf;
  186. head->oh_magic = uget2(c); c += 2;
  187. head->oh_stamp = uget2(c); c += 2;
  188. head->oh_flags = uget2(c); c += 2;
  189. head->oh_nsect = uget2(c); c += 2;
  190. head->oh_nrelo = uget2(c); c += 2;
  191. head->oh_nname = uget2(c); c += 2;
  192. head->oh_nemit = get4(c); c += 4;
  193. head->oh_nchar = get4(c);
  194. return 1;
  195. }
  196. /* Read an ack.out section header. */
  197. int rsect(FILE* f, struct outsect* sect)
  198. {
  199. char buf[SZ_SECT], *c;
  200. if (fread(buf, sizeof(buf), 1, f) != 1)
  201. return 0;
  202. c = buf;
  203. sect->os_base = get4(c); c += 4;
  204. sect->os_size = get4(c); c += 4;
  205. sect->os_foff = get4(c); c += 4;
  206. sect->os_flen = get4(c); c += 4;
  207. sect->os_lign = get4(c);
  208. return 1 ;
  209. }
  210. int main(int argc, char* argv[])
  211. {
  212. /* General housecleaning and setup. */
  213. input = stdin;
  214. output = stdout;
  215. program = argv[0];
  216. /* Read in and process any flags. */
  217. while ((argc > 1) && (argv[1][0] == '-'))
  218. {
  219. switch (argv[1][1])
  220. {
  221. case 'h':
  222. fprintf(stderr, "%s: Syntax: aelflod [-h] <inputfile> <outputfile>\n",
  223. program);
  224. exit(0);
  225. default:
  226. syntaxerror:
  227. fatal("syntax error --- try -h for help");
  228. }
  229. argv++;
  230. argc--;
  231. }
  232. /* Process the rest of the arguments. */
  233. switch (argc)
  234. {
  235. case 1: /* No parameters --- read from stdin, write to stdout. */
  236. break;
  237. case 3: /* Both input and output files specified. */
  238. output = fopen(argv[2], "w");
  239. if (!output)
  240. fatal("unable to open output file.");
  241. outputfile = argv[2];
  242. /* fall through */
  243. case 2: /* Input file specified. */
  244. input = fopen(argv[1], "r");
  245. if (!input)
  246. fatal("unable to open input file.");
  247. break;
  248. default:
  249. goto syntaxerror;
  250. }
  251. /* Read and check the ack.out file header. */
  252. if (!rhead(input,&outhead))
  253. fatal("failed to read file header.");
  254. if (BADMAGIC(outhead))
  255. fatal("this isn't an ack object file.");
  256. if (outhead.oh_nrelo > 0)
  257. fprintf(stderr, "Warning: relocation information present.");
  258. if (!((outhead.oh_nsect == NUM_SEGMENTS) ||
  259. (outhead.oh_nsect == (NUM_SEGMENTS+1))))
  260. fatal("the input file must have %d sections, not %ld.",
  261. NUM_SEGMENTS, outhead.oh_nsect);
  262. /* Read in the section headers. */
  263. {
  264. int i;
  265. for (i=0; i<outhead.oh_nsect; i++)
  266. {
  267. if (!rsect(input, &outsect[i]))
  268. fatal("failed to read section header %d.", i);
  269. }
  270. }
  271. /* A few checks */
  272. if (outsect[BSS].os_flen != 0)
  273. fatal("the bss space contains initialized data.");
  274. if (!follows(&outsect[BSS], &outsect[DATA]))
  275. fatal("the bss segment must follow the data segment.");
  276. if (!follows(& outsect[ROM], &outsect[TEXT]))
  277. fatal("the rom segment must follow the text segment.");
  278. if (!follows(&outsect[DATA], &outsect[ROM]))
  279. fatal("the data segment must follow the rom segment.") ;
  280. /* Check for an optional end segment (which is otherwise
  281. * ignored). */
  282. if (outhead.oh_nsect == (NUM_SEGMENTS+1))
  283. {
  284. if (!follows(&outsect[NUM_SEGMENTS], &outsect[BSS]))
  285. fatal("end segment must follow bss");
  286. if ( outsect[NUM_SEGMENTS].os_size != 0 )
  287. fatal("end segment must be empty");
  288. }
  289. /* Ensure the base address doesn't overlap the file header. */
  290. codeoffset = outsect[TEXT].os_base & 0x1FFF;
  291. if (codeoffset < (ELF_HEADER_SIZE + PROGRAM_HEADER_SIZE*PROGRAM_HEADER_COUNT))
  292. fatal("base address too small --- overlaps ELF header");
  293. /* Rationalise the memory sizes. */
  294. outsect[TEXT].os_size = outsect[ROM ].os_base - outsect[TEXT].os_base;
  295. outsect[ROM ].os_size = outsect[DATA].os_base - outsect[ROM ].os_base;
  296. outsect[DATA].os_size = outsect[BSS ].os_base - outsect[DATA].os_base;
  297. outsect[BSS ].os_size = align(outsect[BSS].os_size, outsect[BSS].os_lign);
  298. /* Write out the ELF file header. */
  299. writef(elf_le_ident_string, 4, 1);
  300. emit8(1); /* class = ELFCLASS32 */
  301. emit8(bigendian ? 2 : 1); /* endianness */
  302. emit8(1); /* ELF version */
  303. emit8(3); /* ABI = Linux */
  304. emit8(0); /* ABI version */
  305. emit8(0); emit16(0); /* padding... */
  306. emit32(0); /* ...to offset 0x10 */
  307. emit16(2); /* type = ET_EXEC */
  308. emit16(3); /* machine = EM_386 */
  309. emit32(1); /* ELF version again */
  310. emit32(outsect[TEXT].os_base); /* entry point */
  311. emit32(ELF_HEADER_SIZE); /* program header offset */
  312. emit32(0); /* section header offset */
  313. emit32(0); /* flags */
  314. emit16(ELF_HEADER_SIZE); /* elf header size */
  315. emit16(PROGRAM_HEADER_SIZE); /* program header entry size */
  316. emit16(1); /* number of program header entries */
  317. emit16(0x28); /* section header entry size */
  318. emit16(0); /* number of section header entries */
  319. emit16(0); /* section header string table index = SHN_UNDEF */
  320. /* Write out a single rwx section for the entire program. */
  321. {
  322. unsigned long filelength = codeoffset +
  323. outsect[TEXT].os_size +
  324. outsect[ROM].os_size +
  325. outsect[DATA].os_size;
  326. unsigned long memlength = filelength +
  327. outsect[BSS].os_size;
  328. emitphdr(outsect[TEXT].os_base & ~0x1FFF, filelength, memlength,
  329. 0, 4|2|1);
  330. }
  331. /* Write padding until the code start. */
  332. fseek(output, codeoffset, SEEK_SET);
  333. /* Write out the actual data. */
  334. emits(&outsect[TEXT]);
  335. emits(&outsect[ROM]);
  336. emits(&outsect[DATA]);
  337. if (ferror(output))
  338. fatal("output write error");
  339. if (outputfile)
  340. chmod(outputfile, 0755);
  341. /* Summarise what we've done. */
  342. {
  343. long ss = 0;
  344. printf(" address length\n");
  345. printf(" ehdr : %08lX %08lX\n", outsect[TEXT].os_base & ~0x1FFF, codeoffset);
  346. printf(" text : %08lX %08lX\n", outsect[TEXT].os_base, outsect[TEXT].os_size);
  347. printf(" rom : %08lX %08lX\n", outsect[ROM].os_base, outsect[ROM].os_size);
  348. printf(" data : %08lX %08lX\n", outsect[DATA].os_base, outsect[DATA].os_size);
  349. printf(" bss : %08lX %08lX\n", outsect[BSS].os_base, outsect[BSS].os_size);
  350. ss += outsect[TEXT].os_size;
  351. ss += outsect[ROM].os_size;
  352. ss += outsect[DATA].os_size;
  353. ss += outsect[BSS].os_size;
  354. printf("TOTAL : %08lX\n", ss);
  355. }
  356. return 0;
  357. }