elf2ecoff.c 17 KB

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