aelflod.c 11 KB

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