2 * Copyright (c) 1999 Global Technology Associates, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
19 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
21 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/param.h>
32 #include <sys/types.h>
48 static void mk_data(const struct iodesc *i, const struct iodesc *,
49 struct kgz_hdr *, size_t);
50 static int ld_elf(const struct iodesc *, const struct iodesc *,
51 struct kgz_hdr *, const Elf32_Ehdr *);
52 static int ld_aout(const struct iodesc *, const struct iodesc *,
53 struct kgz_hdr *, const struct exec *);
56 * Compress executable and output it in relocatable object format.
59 kgzcmp(struct kgz_hdr *kh, const char *f1, const char *f2)
61 struct iodesc idi, ido;
64 if ((idi.fd = open(idi.fname = f1, O_RDONLY)) == -1)
65 err(1, "%s", idi.fname);
66 if ((ido.fd = open(ido.fname = f2, O_CREAT | O_TRUNC | O_WRONLY,
68 err(1, "%s", ido.fname);
69 kh->ident[0] = KGZ_ID0;
70 kh->ident[1] = KGZ_ID1;
71 kh->ident[2] = KGZ_ID2;
72 kh->ident[3] = KGZ_ID3;
73 mk_data(&idi, &ido, kh,
74 (format == F_AOUT ? sizeof(struct kgz_aouthdr0) :
75 sizeof(struct kgz_elfhdr)) +
76 sizeof(struct kgz_hdr));
77 kh->dload &= 0xffffff;
78 kh->entry &= 0xffffff;
79 if (format == F_AOUT) {
80 struct kgz_aouthdr0 ahdr0 = aouthdr0;
81 struct kgz_aouthdr1 ahdr1 = aouthdr1;
82 unsigned x = (sizeof(struct kgz_hdr) + kh->nsize) & (16 - 1);
87 xwrite(&ido, &ahdr1, sizeof(ahdr1));
88 ahdr0.a.a_data += kh->nsize + x;
90 xwrite(&ido, &ahdr0, sizeof(ahdr0));
92 struct kgz_elfhdr ehdr = elfhdr;
93 ehdr.st[KGZ_ST_KGZ_NDATA].st_size = htole32(kh->nsize);
94 ehdr.sh[KGZ_SH_DATA].sh_size =
95 htole32(le32toh(ehdr.sh[KGZ_SH_DATA].sh_size) + kh->nsize);
97 xwrite(&ido, &ehdr, sizeof(ehdr));
100 khle.dload = htole32(khle.dload);
101 khle.dsize = htole32(khle.dsize);
102 khle.isize = htole32(khle.isize);
103 khle.entry = htole32(khle.entry);
104 khle.nsize = htole32(khle.nsize);
105 xwrite(&ido, &khle, sizeof(khle));
111 * Make encoded (compressed) data.
114 mk_data(const struct iodesc * idi, const struct iodesc * ido,
115 struct kgz_hdr * kh, size_t off)
128 n = xread(idi, &hdr, sizeof(hdr), 0);
130 if (n >= sizeof(hdr.ee) && IS_ELF(hdr.ee))
132 else if (n >= sizeof(hdr.ex) && N_GETMAGIC(hdr.ex) == ZMAGIC)
135 errx(1, "%s: Format not supported", idi->fname);
139 switch (pid = fork()) {
144 dup2(fd[0], STDIN_FILENO);
147 dup2(ido->fd, STDOUT_FILENO);
149 execlp("gzip", "gzip", "-9n", (char *)NULL);
154 idp.fname = "(pipe)";
156 e = fmt == F_ELF ? ld_elf(idi, &idp, kh, &hdr.ee) :
157 fmt == F_AOUT ? ld_aout(idi, &idp, kh, &hdr.ex) : -1;
159 if ((pid = waitpid(pid, &status, 0)) == -1)
161 if (WIFSIGNALED(status) || WEXITSTATUS(status))
165 errx(1, "%s: Invalid format", idi->fname);
166 if (fstat(ido->fd, &sb))
167 err(1, "%s", ido->fname);
168 kh->nsize = sb.st_size - off;
172 * "Load" an ELF-format executable.
175 ld_elf(const struct iodesc * idi, const struct iodesc * ido,
176 struct kgz_hdr * kh, const Elf32_Ehdr * e)
179 size_t load, addr, n;
183 for (x = i = 0; i < e->e_phnum; i++) {
184 if (xread(idi, &p, sizeof(p),
185 e->e_phoff + i * e->e_phentsize) != e->e_phentsize)
187 if (p.p_type != PT_LOAD)
190 load = addr = p.p_vaddr;
192 if (p.p_vaddr < addr)
194 n = p.p_vaddr - addr;
200 if (p.p_memsz < p.p_filesz)
202 n = p.p_memsz - p.p_filesz;
203 xcopy(idi, ido, p.p_filesz, p.p_offset);
210 kh->dsize = addr - load;
211 kh->isize = kh->dsize + n;
212 kh->entry = e->e_entry;
217 * "Load" an a.out-format executable.
220 ld_aout(const struct iodesc * idi, const struct iodesc * ido,
221 struct kgz_hdr * kh, const struct exec * a)
225 load = addr = N_TXTADDR(*a);
226 xcopy(idi, ido, le32toh(a->a_text), N_TXTOFF(*a));
227 addr += le32toh(a->a_text);
228 if (N_DATADDR(*a) != addr)
230 xcopy(idi, ido, le32toh(a->a_data), N_DATOFF(*a));
231 addr += le32toh(a->a_data);
233 kh->dsize = addr - load;
234 kh->isize = kh->dsize + le32toh(a->a_bss);
235 kh->entry = le32toh(a->a_entry);