aelflod.c 11 KB

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