aelflod.c 11 KB

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