Annotation of src/usr.bin/ranlib/build.c, Revision 1.6
1.6 ! espie 1: /* $OpenBSD: build.c,v 1.5 1997/11/07 03:42:47 deraadt Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*-
4: * Copyright (c) 1990 The Regents of the University of California.
5: * All rights reserved.
6: *
7: * This code is derived from software contributed to Berkeley by
8: * Hugh Smith at The University of Guelph.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the University of
21: * California, Berkeley and its contributors.
22: * 4. Neither the name of the University nor the names of its contributors
23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
39: #ifndef lint
40: /*static char sccsid[] = "from: @(#)build.c 5.3 (Berkeley) 3/12/91";*/
1.6 ! espie 41: static char rcsid[] = "$OpenBSD: build.c,v 1.5 1997/11/07 03:42:47 deraadt Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: #include <sys/types.h>
45: #include <sys/stat.h>
46: #include <errno.h>
47: #include <fcntl.h>
48: #include <a.out.h>
49: #include <dirent.h>
50: #include <unistd.h>
51: #include <ar.h>
1.5 deraadt 52: #include <limits.h>
1.1 deraadt 53: #include <ranlib.h>
54: #include <stdio.h>
55: #include <string.h>
56: #include <archive.h>
1.6 ! espie 57: #include "byte.c"
! 58:
1.1 deraadt 59:
60: extern CHDR chdr; /* converted header */
61: extern char *archive; /* archive name */
62: extern char *tname; /* temporary file "name" */
63:
64: typedef struct _rlib {
65: struct _rlib *next; /* next structure */
66: off_t pos; /* offset of defining archive file */
67: char *sym; /* symbol */
68: int symlen; /* strlen(sym) */
69: } RLIB;
70: RLIB *rhead, **pnext;
71:
72: static FILE *fp;
73: static long symcnt; /* symbol count */
74: static long tsymlen; /* total string length */
75:
1.6 ! espie 76: static int rexec();
! 77: static void symobj();
1.1 deraadt 78: extern void *emalloc();
79:
80: build()
81: {
82: CF cf;
83: int afd, tfd;
1.6 ! espie 84: int check_mid;
! 85: int current_mid;
1.1 deraadt 86: off_t size;
87:
1.6 ! espie 88: check_mid = 0;
1.1 deraadt 89: afd = open_archive(O_RDWR);
90: fp = fdopen(afd, "r+");
91: tfd = tmp();
92:
93: SETCF(afd, archive, tfd, tname, RPAD|WPAD);
94:
95: /* Read through the archive, creating list of symbols. */
96: symcnt = tsymlen = 0;
97: pnext = &rhead;
98: while(get_arobj(afd)) {
1.6 ! espie 99: int new_mid;
! 100:
1.1 deraadt 101: if (!strcmp(chdr.name, RANLIBMAG)) {
102: skip_arobj(afd);
103: continue;
104: }
1.6 ! espie 105: new_mid = rexec(afd, tfd);
! 106: if (check_mid && new_mid != current_mid)
! 107: errx(1, "Mixed object format archive: %d / %d",
! 108: new_mid, current_mid);
! 109: current_mid = new_mid;
! 110: check_mid = 1;
1.1 deraadt 111: put_arobj(&cf, (struct stat *)NULL);
112: }
113: *pnext = NULL;
114:
1.6 ! espie 115: /* Create the symbol table. Endianess the same as last mid seen */
! 116: symobj(current_mid);
1.1 deraadt 117:
118: /* Copy the saved objects into the archive. */
119: size = lseek(tfd, (off_t)0, SEEK_CUR);
120: (void)lseek(tfd, (off_t)0, SEEK_SET);
121: SETCF(tfd, tname, afd, archive, NOPAD);
122: copy_ar(&cf, size);
123: (void)ftruncate(afd, lseek(afd, (off_t)0, SEEK_CUR));
124: (void)close(tfd);
125:
126: /* Set the time. */
127: settime(afd);
128: close_archive(afd);
129: return(0);
130: }
131:
132: /*
133: * rexec
134: * Read the exec structure; ignore any files that don't look
1.6 ! espie 135: * exactly right. Return MID.
1.1 deraadt 136: */
1.6 ! espie 137: static int
1.1 deraadt 138: rexec(rfd, wfd)
139: register int rfd;
140: int wfd;
141: {
142: register RLIB *rp;
143: register long nsyms;
144: register int nr, symlen;
145: register char *strtab, *sym;
146: struct exec ebuf;
147: struct nlist nl;
148: off_t r_off, w_off;
149: long strsize;
150:
151: /* Get current offsets for original and tmp files. */
152: r_off = lseek(rfd, (off_t)0, SEEK_CUR);
153: w_off = lseek(wfd, (off_t)0, SEEK_CUR);
154:
155: /* Read in exec structure. */
156: nr = read(rfd, (char *)&ebuf, sizeof(struct exec));
157: if (nr != sizeof(struct exec))
158: goto badread;
159:
160: /* Check magic number and symbol count. */
1.6 ! espie 161: if (BAD_OBJECT(ebuf) || ebuf.a_syms == 0)
1.1 deraadt 162: goto bad1;
1.6 ! espie 163: fix_header_order(&ebuf);
1.1 deraadt 164:
165: /* Seek to string table. */
166: if (lseek(rfd, N_STROFF(ebuf) + r_off, SEEK_SET) == (off_t)-1)
167: error(archive);
168:
169: /* Read in size of the string table. */
170: nr = read(rfd, (char *)&strsize, sizeof(strsize));
171: if (nr != sizeof(strsize))
172: goto badread;
173:
1.6 ! espie 174: strsize = fix_long_order(strsize, N_GETMID(ebuf));
1.1 deraadt 175: /* Read in the string table. */
176: strsize -= sizeof(strsize);
177: strtab = (char *)emalloc(strsize);
178: nr = read(rfd, strtab, strsize);
179: if (nr != strsize) {
180: badread: if (nr < 0)
181: error(archive);
182: goto bad2;
183: }
184:
185: /* Seek to symbol table. */
186: if (fseek(fp, N_SYMOFF(ebuf) + r_off, SEEK_SET) == (off_t)-1)
187: goto bad2;
188:
189: /* For each symbol read the nlist entry and save it as necessary. */
190: nsyms = ebuf.a_syms / sizeof(struct nlist);
191: while (nsyms--) {
192: if (!fread((char *)&nl, sizeof(struct nlist), 1, fp)) {
193: if (feof(fp))
194: badfmt();
195: error(archive);
196: }
1.6 ! espie 197: fix_nlist_order(&nl, N_GETMID(ebuf));
1.1 deraadt 198:
199: /* Ignore if no name or local. */
200: if (!nl.n_un.n_strx || !(nl.n_type & N_EXT))
201: continue;
202:
203: /*
204: * If the symbol is an undefined external and the n_value
205: * field is non-zero, keep it.
206: */
207: if ((nl.n_type & N_TYPE) == N_UNDF && !nl.n_value)
208: continue;
209:
210: /* First four bytes are the table size. */
211: sym = strtab + nl.n_un.n_strx - sizeof(long);
212: symlen = strlen(sym) + 1;
213:
214: rp = (RLIB *)emalloc(sizeof(RLIB));
215: rp->sym = (char *)emalloc(symlen);
216: bcopy(sym, rp->sym, symlen);
217: rp->symlen = symlen;
218: rp->pos = w_off;
219:
220: /* Build in forward order for "ar -m" command. */
221: *pnext = rp;
222: pnext = &rp->next;
223:
224: ++symcnt;
225: tsymlen += symlen;
226: }
227:
228: bad2: free(strtab);
229: bad1: (void)lseek(rfd, (off_t)r_off, SEEK_SET);
1.6 ! espie 230: return N_GETMID(ebuf);
1.1 deraadt 231: }
232:
233: /*
234: * symobj --
235: * Write the symbol table into the archive, computing offsets as
1.6 ! espie 236: * writing. Use the right format depending on mid.
1.1 deraadt 237: */
238: static void
1.6 ! espie 239: symobj(mid)
! 240: int mid;
1.1 deraadt 241: {
242: register RLIB *rp, *rnext;
243: struct ranlib rn;
244: char hb[sizeof(struct ar_hdr) + 1], pad;
245: long ransize, size, stroff;
1.3 deraadt 246: uid_t uid;
247: gid_t gid;
1.1 deraadt 248:
249: /* Rewind the archive, leaving the magic number. */
250: if (fseek(fp, (off_t)SARMAG, SEEK_SET) == (off_t)-1)
251: error(archive);
252:
253: /* Size of the ranlib archive file, pad if necessary. */
254: ransize = sizeof(long) +
255: symcnt * sizeof(struct ranlib) + sizeof(long) + tsymlen;
256: if (ransize & 01) {
257: ++ransize;
258: pad = '\n';
259: } else
260: pad = '\0';
261:
1.3 deraadt 262: uid = getuid();
263: if (uid > USHRT_MAX) {
1.4 deraadt 264: warnx("warning: uid %u truncated to %u", uid, USHRT_MAX);
1.3 deraadt 265: uid = USHRT_MAX;
266: }
267: gid = getgid();
268: if (gid > USHRT_MAX) {
1.4 deraadt 269: warnx("warning: gid %u truncated to %u", gid, USHRT_MAX);
1.3 deraadt 270: gid = USHRT_MAX;
271: }
272:
1.1 deraadt 273: /* Put out the ranlib archive file header. */
274: #define DEFMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
1.3 deraadt 275: (void)sprintf(hb, HDR2, RANLIBMAG, 0L, uid, gid,
1.1 deraadt 276: DEFMODE & ~umask(0), (off_t)ransize, ARFMAG);
277: if (!fwrite(hb, sizeof(struct ar_hdr), 1, fp))
278: error(tname);
279:
280: /* First long is the size of the ranlib structure section. */
1.6 ! espie 281: size = fix_long_order(symcnt * sizeof(struct ranlib), mid);
1.1 deraadt 282: if (!fwrite((char *)&size, sizeof(size), 1, fp))
283: error(tname);
284:
285: /* Offset of the first archive file. */
286: size = SARMAG + sizeof(struct ar_hdr) + ransize;
287:
288: /*
289: * Write out the ranlib structures. The offset into the string
290: * table is cumulative, the offset into the archive is the value
291: * set in rexec() plus the offset to the first archive file.
292: */
293: for (rp = rhead, stroff = 0; rp; rp = rp->next) {
294: rn.ran_un.ran_strx = stroff;
295: stroff += rp->symlen;
296: rn.ran_off = size + rp->pos;
1.6 ! espie 297: fix_ranlib_order(&rn, mid);
1.1 deraadt 298: if (!fwrite((char *)&rn, sizeof(struct ranlib), 1, fp))
299: error(archive);
300: }
301:
302: /* Second long is the size of the string table. */
1.6 ! espie 303:
! 304: size = fix_long_order(tsymlen, mid);
! 305: if (!fwrite((char *)&size, sizeof(size), 1, fp))
1.1 deraadt 306: error(tname);
307:
308: /* Write out the string table. */
309: for (rp = rhead; rp; rp = rnext) {
310: if (!fwrite(rp->sym, rp->symlen, 1, fp))
311: error(tname);
312: rnext = rp->next;
313: free(rp);
314: }
315: rhead = NULL;
316:
317: if (pad && !fwrite(&pad, sizeof(pad), 1, fp))
318: error(tname);
319:
320: (void)fflush(fp);
321: }