elf2ecoff.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * Copyright (c) 1995
  3. * Ted Lemon (hereinafter referred to as the author)
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /* elf2ecoff.c
  29. This program converts an elf executable to an ECOFF executable.
  30. No symbol table is retained. This is useful primarily in building
  31. net-bootable kernels for machines (e.g., DECstation and Alpha) which
  32. only support the ECOFF object file format. */
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include <sys/types.h>
  37. #include <fcntl.h>
  38. #include <unistd.h>
  39. #include <elf.h>
  40. #include <limits.h>
  41. #include <netinet/in.h>
  42. #include <stdlib.h>
  43. #include <stdint.h>
  44. #include <inttypes.h>
  45. #include "ecoff.h"
  46. /*
  47. * Some extra ELF definitions
  48. */
  49. #define PT_MIPS_REGINFO 0x70000000 /* Register usage information */
  50. #define PT_MIPS_ABIFLAGS 0x70000003 /* Records ABI related flags */
  51. /* -------------------------------------------------------------------- */
  52. struct sect {
  53. uint32_t vaddr;
  54. uint32_t len;
  55. };
  56. int *symTypeTable;
  57. int must_convert_endian;
  58. int format_bigendian;
  59. static void copy(int out, int in, off_t offset, off_t size)
  60. {
  61. char ibuf[4096];
  62. int remaining, cur, count;
  63. /* Go to the start of the ELF symbol table... */
  64. if (lseek(in, offset, SEEK_SET) < 0) {
  65. perror("copy: lseek");
  66. exit(1);
  67. }
  68. remaining = size;
  69. while (remaining) {
  70. cur = remaining;
  71. if (cur > sizeof ibuf)
  72. cur = sizeof ibuf;
  73. remaining -= cur;
  74. if ((count = read(in, ibuf, cur)) != cur) {
  75. fprintf(stderr, "copy: read: %s\n",
  76. count ? strerror(errno) :
  77. "premature end of file");
  78. exit(1);
  79. }
  80. if ((count = write(out, ibuf, cur)) != cur) {
  81. perror("copy: write");
  82. exit(1);
  83. }
  84. }
  85. }
  86. /*
  87. * Combine two segments, which must be contiguous. If pad is true, it's
  88. * okay for there to be padding between.
  89. */
  90. static void combine(struct sect *base, struct sect *new, int pad)
  91. {
  92. if (!base->len)
  93. *base = *new;
  94. else if (new->len) {
  95. if (base->vaddr + base->len != new->vaddr) {
  96. if (pad)
  97. base->len = new->vaddr - base->vaddr;
  98. else {
  99. fprintf(stderr,
  100. "Non-contiguous data can't be converted.\n");
  101. exit(1);
  102. }
  103. }
  104. base->len += new->len;
  105. }
  106. }
  107. static int phcmp(const void *v1, const void *v2)
  108. {
  109. const Elf32_Phdr *h1 = v1;
  110. const Elf32_Phdr *h2 = v2;
  111. if (h1->p_vaddr > h2->p_vaddr)
  112. return 1;
  113. else if (h1->p_vaddr < h2->p_vaddr)
  114. return -1;
  115. else
  116. return 0;
  117. }
  118. static char *saveRead(int file, off_t offset, off_t len, char *name)
  119. {
  120. char *tmp;
  121. int count;
  122. off_t off;
  123. if ((off = lseek(file, offset, SEEK_SET)) < 0) {
  124. fprintf(stderr, "%s: fseek: %s\n", name, strerror(errno));
  125. exit(1);
  126. }
  127. if (!(tmp = (char *) malloc(len))) {
  128. fprintf(stderr, "%s: Can't allocate %ld bytes.\n", name,
  129. len);
  130. exit(1);
  131. }
  132. count = read(file, tmp, len);
  133. if (count != len) {
  134. fprintf(stderr, "%s: read: %s.\n",
  135. name,
  136. count ? strerror(errno) : "End of file reached");
  137. exit(1);
  138. }
  139. return tmp;
  140. }
  141. #define swab16(x) \
  142. ((uint16_t)( \
  143. (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
  144. (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) ))
  145. #define swab32(x) \
  146. ((unsigned int)( \
  147. (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
  148. (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
  149. (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
  150. (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) ))
  151. static void convert_elf_hdr(Elf32_Ehdr * e)
  152. {
  153. e->e_type = swab16(e->e_type);
  154. e->e_machine = swab16(e->e_machine);
  155. e->e_version = swab32(e->e_version);
  156. e->e_entry = swab32(e->e_entry);
  157. e->e_phoff = swab32(e->e_phoff);
  158. e->e_shoff = swab32(e->e_shoff);
  159. e->e_flags = swab32(e->e_flags);
  160. e->e_ehsize = swab16(e->e_ehsize);
  161. e->e_phentsize = swab16(e->e_phentsize);
  162. e->e_phnum = swab16(e->e_phnum);
  163. e->e_shentsize = swab16(e->e_shentsize);
  164. e->e_shnum = swab16(e->e_shnum);
  165. e->e_shstrndx = swab16(e->e_shstrndx);
  166. }
  167. static void convert_elf_phdrs(Elf32_Phdr * p, int num)
  168. {
  169. int i;
  170. for (i = 0; i < num; i++, p++) {
  171. p->p_type = swab32(p->p_type);
  172. p->p_offset = swab32(p->p_offset);
  173. p->p_vaddr = swab32(p->p_vaddr);
  174. p->p_paddr = swab32(p->p_paddr);
  175. p->p_filesz = swab32(p->p_filesz);
  176. p->p_memsz = swab32(p->p_memsz);
  177. p->p_flags = swab32(p->p_flags);
  178. p->p_align = swab32(p->p_align);
  179. }
  180. }
  181. static void convert_elf_shdrs(Elf32_Shdr * s, int num)
  182. {
  183. int i;
  184. for (i = 0; i < num; i++, s++) {
  185. s->sh_name = swab32(s->sh_name);
  186. s->sh_type = swab32(s->sh_type);
  187. s->sh_flags = swab32(s->sh_flags);
  188. s->sh_addr = swab32(s->sh_addr);
  189. s->sh_offset = swab32(s->sh_offset);
  190. s->sh_size = swab32(s->sh_size);
  191. s->sh_link = swab32(s->sh_link);
  192. s->sh_info = swab32(s->sh_info);
  193. s->sh_addralign = swab32(s->sh_addralign);
  194. s->sh_entsize = swab32(s->sh_entsize);
  195. }
  196. }
  197. static void convert_ecoff_filehdr(struct filehdr *f)
  198. {
  199. f->f_magic = swab16(f->f_magic);
  200. f->f_nscns = swab16(f->f_nscns);
  201. f->f_timdat = swab32(f->f_timdat);
  202. f->f_symptr = swab32(f->f_symptr);
  203. f->f_nsyms = swab32(f->f_nsyms);
  204. f->f_opthdr = swab16(f->f_opthdr);
  205. f->f_flags = swab16(f->f_flags);
  206. }
  207. static void convert_ecoff_aouthdr(struct aouthdr *a)
  208. {
  209. a->magic = swab16(a->magic);
  210. a->vstamp = swab16(a->vstamp);
  211. a->tsize = swab32(a->tsize);
  212. a->dsize = swab32(a->dsize);
  213. a->bsize = swab32(a->bsize);
  214. a->entry = swab32(a->entry);
  215. a->text_start = swab32(a->text_start);
  216. a->data_start = swab32(a->data_start);
  217. a->bss_start = swab32(a->bss_start);
  218. a->gprmask = swab32(a->gprmask);
  219. a->cprmask[0] = swab32(a->cprmask[0]);
  220. a->cprmask[1] = swab32(a->cprmask[1]);
  221. a->cprmask[2] = swab32(a->cprmask[2]);
  222. a->cprmask[3] = swab32(a->cprmask[3]);
  223. a->gp_value = swab32(a->gp_value);
  224. }
  225. static void convert_ecoff_esecs(struct scnhdr *s, int num)
  226. {
  227. int i;
  228. for (i = 0; i < num; i++, s++) {
  229. s->s_paddr = swab32(s->s_paddr);
  230. s->s_vaddr = swab32(s->s_vaddr);
  231. s->s_size = swab32(s->s_size);
  232. s->s_scnptr = swab32(s->s_scnptr);
  233. s->s_relptr = swab32(s->s_relptr);
  234. s->s_lnnoptr = swab32(s->s_lnnoptr);
  235. s->s_nreloc = swab16(s->s_nreloc);
  236. s->s_nlnno = swab16(s->s_nlnno);
  237. s->s_flags = swab32(s->s_flags);
  238. }
  239. }
  240. int main(int argc, char *argv[])
  241. {
  242. Elf32_Ehdr ex;
  243. Elf32_Phdr *ph;
  244. Elf32_Shdr *sh;
  245. int i, pad;
  246. struct sect text, data, bss;
  247. struct filehdr efh;
  248. struct aouthdr eah;
  249. struct scnhdr esecs[6];
  250. int infile, outfile;
  251. uint32_t cur_vma = UINT32_MAX;
  252. int addflag = 0;
  253. int nosecs;
  254. text.len = data.len = bss.len = 0;
  255. text.vaddr = data.vaddr = bss.vaddr = 0;
  256. /* Check args... */
  257. if (argc < 3 || argc > 4) {
  258. usage:
  259. fprintf(stderr,
  260. "usage: elf2ecoff <elf executable> <ecoff executable> [-a]\n");
  261. exit(1);
  262. }
  263. if (argc == 4) {
  264. if (strcmp(argv[3], "-a"))
  265. goto usage;
  266. addflag = 1;
  267. }
  268. /* Try the input file... */
  269. if ((infile = open(argv[1], O_RDONLY)) < 0) {
  270. fprintf(stderr, "Can't open %s for read: %s\n",
  271. argv[1], strerror(errno));
  272. exit(1);
  273. }
  274. /* Read the header, which is at the beginning of the file... */
  275. i = read(infile, &ex, sizeof ex);
  276. if (i != sizeof ex) {
  277. fprintf(stderr, "ex: %s: %s.\n",
  278. argv[1],
  279. i ? strerror(errno) : "End of file reached");
  280. exit(1);
  281. }
  282. if (ex.e_ident[EI_DATA] == ELFDATA2MSB)
  283. format_bigendian = 1;
  284. if (ntohs(0xaa55) == 0xaa55) {
  285. if (!format_bigendian)
  286. must_convert_endian = 1;
  287. } else {
  288. if (format_bigendian)
  289. must_convert_endian = 1;
  290. }
  291. if (must_convert_endian)
  292. convert_elf_hdr(&ex);
  293. /* Read the program headers... */
  294. ph = (Elf32_Phdr *) saveRead(infile, ex.e_phoff,
  295. ex.e_phnum * sizeof(Elf32_Phdr),
  296. "ph");
  297. if (must_convert_endian)
  298. convert_elf_phdrs(ph, ex.e_phnum);
  299. /* Read the section headers... */
  300. sh = (Elf32_Shdr *) saveRead(infile, ex.e_shoff,
  301. ex.e_shnum * sizeof(Elf32_Shdr),
  302. "sh");
  303. if (must_convert_endian)
  304. convert_elf_shdrs(sh, ex.e_shnum);
  305. /* Figure out if we can cram the program header into an ECOFF
  306. header... Basically, we can't handle anything but loadable
  307. segments, but we can ignore some kinds of segments. We can't
  308. handle holes in the address space. Segments may be out of order,
  309. so we sort them first. */
  310. qsort(ph, ex.e_phnum, sizeof(Elf32_Phdr), phcmp);
  311. for (i = 0; i < ex.e_phnum; i++) {
  312. /* Section types we can ignore... */
  313. switch (ph[i].p_type) {
  314. case PT_NULL:
  315. case PT_NOTE:
  316. case PT_PHDR:
  317. case PT_MIPS_REGINFO:
  318. case PT_MIPS_ABIFLAGS:
  319. continue;
  320. case PT_LOAD:
  321. /* Writable (data) segment? */
  322. if (ph[i].p_flags & PF_W) {
  323. struct sect ndata, nbss;
  324. ndata.vaddr = ph[i].p_vaddr;
  325. ndata.len = ph[i].p_filesz;
  326. nbss.vaddr = ph[i].p_vaddr + ph[i].p_filesz;
  327. nbss.len = ph[i].p_memsz - ph[i].p_filesz;
  328. combine(&data, &ndata, 0);
  329. combine(&bss, &nbss, 1);
  330. } else {
  331. struct sect ntxt;
  332. ntxt.vaddr = ph[i].p_vaddr;
  333. ntxt.len = ph[i].p_filesz;
  334. combine(&text, &ntxt, 0);
  335. }
  336. /* Remember the lowest segment start address. */
  337. if (ph[i].p_vaddr < cur_vma)
  338. cur_vma = ph[i].p_vaddr;
  339. break;
  340. default:
  341. /* Section types we can't handle... */
  342. fprintf(stderr,
  343. "Program header %d type %d can't be converted.\n",
  344. ex.e_phnum, ph[i].p_type);
  345. exit(1);
  346. }
  347. }
  348. /* Sections must be in order to be converted... */
  349. if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
  350. text.vaddr + text.len > data.vaddr
  351. || data.vaddr + data.len > bss.vaddr) {
  352. fprintf(stderr,
  353. "Sections ordering prevents a.out conversion.\n");
  354. exit(1);
  355. }
  356. /* If there's a data section but no text section, then the loader
  357. combined everything into one section. That needs to be the
  358. text section, so just make the data section zero length following
  359. text. */
  360. if (data.len && !text.len) {
  361. text = data;
  362. data.vaddr = text.vaddr + text.len;
  363. data.len = 0;
  364. }
  365. /* If there is a gap between text and data, we'll fill it when we copy
  366. the data, so update the length of the text segment as represented in
  367. a.out to reflect that, since a.out doesn't allow gaps in the program
  368. address space. */
  369. if (text.vaddr + text.len < data.vaddr)
  370. text.len = data.vaddr - text.vaddr;
  371. /* We now have enough information to cons up an a.out header... */
  372. eah.magic = OMAGIC;
  373. eah.vstamp = 200;
  374. eah.tsize = text.len;
  375. eah.dsize = data.len;
  376. eah.bsize = bss.len;
  377. eah.entry = ex.e_entry;
  378. eah.text_start = text.vaddr;
  379. eah.data_start = data.vaddr;
  380. eah.bss_start = bss.vaddr;
  381. eah.gprmask = 0xf3fffffe;
  382. memset(&eah.cprmask, '\0', sizeof eah.cprmask);
  383. eah.gp_value = 0; /* unused. */
  384. if (format_bigendian)
  385. efh.f_magic = MIPSEBMAGIC;
  386. else
  387. efh.f_magic = MIPSELMAGIC;
  388. if (addflag)
  389. nosecs = 6;
  390. else
  391. nosecs = 3;
  392. efh.f_nscns = nosecs;
  393. efh.f_timdat = 0; /* bogus */
  394. efh.f_symptr = 0;
  395. efh.f_nsyms = 0;
  396. efh.f_opthdr = sizeof eah;
  397. efh.f_flags = 0x100f; /* Stripped, not sharable. */
  398. memset(esecs, 0, sizeof esecs);
  399. strcpy(esecs[0].s_name, ".text");
  400. strcpy(esecs[1].s_name, ".data");
  401. strcpy(esecs[2].s_name, ".bss");
  402. if (addflag) {
  403. strcpy(esecs[3].s_name, ".rdata");
  404. strcpy(esecs[4].s_name, ".sdata");
  405. strcpy(esecs[5].s_name, ".sbss");
  406. }
  407. esecs[0].s_paddr = esecs[0].s_vaddr = eah.text_start;
  408. esecs[1].s_paddr = esecs[1].s_vaddr = eah.data_start;
  409. esecs[2].s_paddr = esecs[2].s_vaddr = eah.bss_start;
  410. if (addflag) {
  411. esecs[3].s_paddr = esecs[3].s_vaddr = 0;
  412. esecs[4].s_paddr = esecs[4].s_vaddr = 0;
  413. esecs[5].s_paddr = esecs[5].s_vaddr = 0;
  414. }
  415. esecs[0].s_size = eah.tsize;
  416. esecs[1].s_size = eah.dsize;
  417. esecs[2].s_size = eah.bsize;
  418. if (addflag) {
  419. esecs[3].s_size = 0;
  420. esecs[4].s_size = 0;
  421. esecs[5].s_size = 0;
  422. }
  423. esecs[0].s_scnptr = N_TXTOFF(efh, eah);
  424. esecs[1].s_scnptr = N_DATOFF(efh, eah);
  425. #define ECOFF_SEGMENT_ALIGNMENT(a) 0x10
  426. #define ECOFF_ROUND(s, a) (((s)+(a)-1)&~((a)-1))
  427. esecs[2].s_scnptr = esecs[1].s_scnptr +
  428. ECOFF_ROUND(esecs[1].s_size, ECOFF_SEGMENT_ALIGNMENT(&eah));
  429. if (addflag) {
  430. esecs[3].s_scnptr = 0;
  431. esecs[4].s_scnptr = 0;
  432. esecs[5].s_scnptr = 0;
  433. }
  434. esecs[0].s_relptr = esecs[1].s_relptr = esecs[2].s_relptr = 0;
  435. esecs[0].s_lnnoptr = esecs[1].s_lnnoptr = esecs[2].s_lnnoptr = 0;
  436. esecs[0].s_nreloc = esecs[1].s_nreloc = esecs[2].s_nreloc = 0;
  437. esecs[0].s_nlnno = esecs[1].s_nlnno = esecs[2].s_nlnno = 0;
  438. if (addflag) {
  439. esecs[3].s_relptr = esecs[4].s_relptr
  440. = esecs[5].s_relptr = 0;
  441. esecs[3].s_lnnoptr = esecs[4].s_lnnoptr
  442. = esecs[5].s_lnnoptr = 0;
  443. esecs[3].s_nreloc = esecs[4].s_nreloc = esecs[5].s_nreloc =
  444. 0;
  445. esecs[3].s_nlnno = esecs[4].s_nlnno = esecs[5].s_nlnno = 0;
  446. }
  447. esecs[0].s_flags = 0x20;
  448. esecs[1].s_flags = 0x40;
  449. esecs[2].s_flags = 0x82;
  450. if (addflag) {
  451. esecs[3].s_flags = 0x100;
  452. esecs[4].s_flags = 0x200;
  453. esecs[5].s_flags = 0x400;
  454. }
  455. /* Make the output file... */
  456. if ((outfile = open(argv[2], O_WRONLY | O_CREAT, 0777)) < 0) {
  457. fprintf(stderr, "Unable to create %s: %s\n", argv[2],
  458. strerror(errno));
  459. exit(1);
  460. }
  461. if (must_convert_endian)
  462. convert_ecoff_filehdr(&efh);
  463. /* Write the headers... */
  464. i = write(outfile, &efh, sizeof efh);
  465. if (i != sizeof efh) {
  466. perror("efh: write");
  467. exit(1);
  468. for (i = 0; i < nosecs; i++) {
  469. printf
  470. ("Section %d: %s phys %"PRIx32" size %"PRIx32"\t file offset %"PRIx32"\n",
  471. i, esecs[i].s_name, esecs[i].s_paddr,
  472. esecs[i].s_size, esecs[i].s_scnptr);
  473. }
  474. }
  475. fprintf(stderr, "wrote %d byte file header.\n", i);
  476. if (must_convert_endian)
  477. convert_ecoff_aouthdr(&eah);
  478. i = write(outfile, &eah, sizeof eah);
  479. if (i != sizeof eah) {
  480. perror("eah: write");
  481. exit(1);
  482. }
  483. fprintf(stderr, "wrote %d byte a.out header.\n", i);
  484. if (must_convert_endian)
  485. convert_ecoff_esecs(&esecs[0], nosecs);
  486. i = write(outfile, &esecs, nosecs * sizeof(struct scnhdr));
  487. if (i != nosecs * sizeof(struct scnhdr)) {
  488. perror("esecs: write");
  489. exit(1);
  490. }
  491. fprintf(stderr, "wrote %d bytes of section headers.\n", i);
  492. pad = (sizeof(efh) + sizeof(eah) + nosecs * sizeof(struct scnhdr)) & 15;
  493. if (pad) {
  494. pad = 16 - pad;
  495. i = write(outfile, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0", pad);
  496. if (i < 0) {
  497. perror("ipad: write");
  498. exit(1);
  499. }
  500. fprintf(stderr, "wrote %d byte pad.\n", i);
  501. }
  502. /*
  503. * Copy the loadable sections. Zero-fill any gaps less than 64k;
  504. * complain about any zero-filling, and die if we're asked to zero-fill
  505. * more than 64k.
  506. */
  507. for (i = 0; i < ex.e_phnum; i++) {
  508. /* Unprocessable sections were handled above, so just verify that
  509. the section can be loaded before copying. */
  510. if (ph[i].p_type == PT_LOAD && ph[i].p_filesz) {
  511. if (cur_vma != ph[i].p_vaddr) {
  512. uint32_t gap = ph[i].p_vaddr - cur_vma;
  513. char obuf[1024];
  514. if (gap > 65536) {
  515. fprintf(stderr,
  516. "Intersegment gap (%"PRId32" bytes) too large.\n",
  517. gap);
  518. exit(1);
  519. }
  520. fprintf(stderr,
  521. "Warning: %d byte intersegment gap.\n",
  522. gap);
  523. memset(obuf, 0, sizeof obuf);
  524. while (gap) {
  525. int count =
  526. write(outfile, obuf,
  527. (gap >
  528. sizeof obuf ? sizeof
  529. obuf : gap));
  530. if (count < 0) {
  531. fprintf(stderr,
  532. "Error writing gap: %s\n",
  533. strerror(errno));
  534. exit(1);
  535. }
  536. gap -= count;
  537. }
  538. }
  539. fprintf(stderr, "writing %d bytes...\n",
  540. ph[i].p_filesz);
  541. copy(outfile, infile, ph[i].p_offset,
  542. ph[i].p_filesz);
  543. cur_vma = ph[i].p_vaddr + ph[i].p_filesz;
  544. }
  545. }
  546. /*
  547. * Write a page of padding for boot PROMS that read entire pages.
  548. * Without this, they may attempt to read past the end of the
  549. * data section, incur an error, and refuse to boot.
  550. */
  551. {
  552. char obuf[4096];
  553. memset(obuf, 0, sizeof obuf);
  554. if (write(outfile, obuf, sizeof(obuf)) != sizeof(obuf)) {
  555. fprintf(stderr, "Error writing PROM padding: %s\n",
  556. strerror(errno));
  557. exit(1);
  558. }
  559. }
  560. /* Looks like we won... */
  561. exit(0);
  562. }