aelflod.c 11 KB

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