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.
29 #include <sys/types.h>
46 static void mk_data(const struct iodesc *i, const struct iodesc *,
47 struct kgz_hdr *, size_t);
48 static int ld_elf(const struct iodesc *, const struct iodesc *,
49 struct kgz_hdr *, const Elf32_Ehdr *);
50 static int ld_aout(const struct iodesc *, const struct iodesc *,
51 struct kgz_hdr *, const struct exec *);
54 * Compress executable and output it in relocatable object format.
57 kgzcmp(struct kgz_hdr *kh, const char *f1, const char *f2)
59 struct iodesc idi, ido;
61 if ((idi.fd = open(idi.fname = f1, O_RDONLY)) == -1)
62 err(1, "%s", idi.fname);
63 if ((ido.fd = open(ido.fname = f2, O_CREAT | O_TRUNC | O_WRONLY,
65 err(1, "%s", ido.fname);
66 kh->ident[0] = KGZ_ID0;
67 kh->ident[1] = KGZ_ID1;
68 kh->ident[2] = KGZ_ID2;
69 kh->ident[3] = KGZ_ID3;
70 mk_data(&idi, &ido, kh,
71 (format == F_AOUT ? sizeof(struct kgz_aouthdr0) :
72 sizeof(struct kgz_elfhdr)) +
73 sizeof(struct kgz_hdr));
74 kh->dload &= 0xffffff;
75 kh->entry &= 0xffffff;
76 if (format == F_AOUT) {
77 struct kgz_aouthdr0 ahdr0 = aouthdr0;
78 struct kgz_aouthdr1 ahdr1 = aouthdr1;
79 unsigned x = (sizeof(struct kgz_hdr) + kh->nsize) & (16 - 1);
84 xwrite(&ido, &ahdr1, sizeof(ahdr1));
85 ahdr0.a.a_data += kh->nsize + x;
87 xwrite(&ido, &ahdr0, sizeof(ahdr0));
89 struct kgz_elfhdr ehdr = elfhdr;
90 ehdr.st[KGZ_ST_KGZ_NDATA].st_size = kh->nsize;
91 ehdr.sh[KGZ_SH_DATA].sh_size += kh->nsize;
93 xwrite(&ido, &ehdr, sizeof(ehdr));
95 xwrite(&ido, kh, sizeof(*kh));
101 * Make encoded (compressed) data.
104 mk_data(const struct iodesc * idi, const struct iodesc * ido,
105 struct kgz_hdr * kh, size_t off)
118 n = xread(idi, &hdr, sizeof(hdr), 0);
120 if (n >= sizeof(hdr.ee) && IS_ELF(hdr.ee))
122 else if (n >= sizeof(hdr.ex) && N_GETMAGIC(hdr.ex) == ZMAGIC)
125 errx(1, "%s: Format not supported", idi->fname);
129 switch (pid = fork()) {
134 dup2(fd[0], STDIN_FILENO);
137 dup2(ido->fd, STDOUT_FILENO);
139 execlp("gzip", "gzip", "-9", NULL);
144 idp.fname = "(pipe)";
146 e = fmt == F_ELF ? ld_elf(idi, &idp, kh, &hdr.ee) :
147 fmt == F_AOUT ? ld_aout(idi, &idp, kh, &hdr.ex) : -1;
149 if ((pid = waitpid(pid, &status, 0)) == -1)
151 if (WIFSIGNALED(status) || WEXITSTATUS(status))
155 errx(1, "%s: Invalid format", idi->fname);
156 if (fstat(ido->fd, &sb))
157 err(1, "%s", ido->fname);
158 kh->nsize = sb.st_size - off;
162 * "Load" an ELF-format executable.
165 ld_elf(const struct iodesc * idi, const struct iodesc * ido,
166 struct kgz_hdr * kh, const Elf32_Ehdr * e)
169 size_t load, addr, n;
173 for (x = i = 0; i < e->e_phnum; i++) {
174 if (xread(idi, &p, sizeof(p),
175 e->e_phoff + i * e->e_phentsize) != e->e_phentsize)
177 if (p.p_type != PT_LOAD)
180 load = addr = p.p_vaddr;
182 if (p.p_vaddr < addr)
184 n = p.p_vaddr - addr;
190 if (p.p_memsz < p.p_filesz)
192 n = p.p_memsz - p.p_filesz;
193 xcopy(idi, ido, p.p_filesz, p.p_offset);
200 kh->dsize = addr - load;
201 kh->isize = kh->dsize + n;
202 kh->entry = e->e_entry;
207 * "Load" an a.out-format executable.
210 ld_aout(const struct iodesc * idi, const struct iodesc * ido,
211 struct kgz_hdr * kh, const struct exec * a)
215 load = addr = N_TXTADDR(*a);
216 xcopy(idi, ido, a->a_text, N_TXTOFF(*a));
218 if (N_DATADDR(*a) != addr)
220 xcopy(idi, ido, a->a_data, N_DATOFF(*a));
223 kh->dsize = addr - load;
224 kh->isize = kh->dsize + a->a_bss;
225 kh->entry = a->a_entry;