2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Hugh Smith at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid[] = "@(#)build.c 8.1 (Berkeley) 6/6/93";
41 static const char rcsid[] =
42 "$FreeBSD: src/usr.bin/ranlib/build.c,v 1.7 1999/08/28 01:05:01 peter Exp $";
45 #include <sys/types.h>
60 extern int tmp __P(( void ));
61 extern void error __P(( char * ));
62 extern void badfmt __P(( void ));
63 extern void settime __P(( int ));
65 extern CHDR chdr; /* converted header */
66 extern char *archive; /* archive name */
67 extern char *tname; /* temporary file "name" */
69 typedef struct _rlib {
70 struct _rlib *next; /* next structure */
71 off_t pos; /* offset of defining archive file */
72 char *sym; /* symbol */
73 int symlen; /* strlen(sym) */
79 long symcnt; /* symbol count */
80 long tsymlen; /* total string length */
82 static void rexec __P((int, int));
83 static void symobj __P((void));
92 afd = open_archive(O_RDWR);
93 fp = fdopen(afd, "r+");
96 SETCF(afd, archive, tfd, tname, RPAD|WPAD);
98 /* Read through the archive, creating list of symbols. */
100 symcnt = tsymlen = 0;
101 while(get_arobj(afd)) {
102 if (!strcmp(chdr.name, RANLIBMAG)) {
107 put_arobj(&cf, (struct stat *)NULL);
111 /* Create the symbol table. */
114 /* Copy the saved objects into the archive. */
115 size = lseek(tfd, (off_t)0, SEEK_CUR);
116 (void)lseek(tfd, (off_t)0, SEEK_SET);
117 SETCF(tfd, tname, afd, archive, WPAD);
119 (void)ftruncate(afd, lseek(afd, (off_t)0, SEEK_CUR));
130 * Read the exec structure; ignore any files that don't look
140 register int nr, symlen;
141 register char *strtab = 0, *sym;
148 /* Get current offsets for original and tmp files. */
149 r_off = lseek(rfd, (off_t)0, SEEK_CUR);
150 w_off = lseek(wfd, (off_t)0, SEEK_CUR);
152 /* Read in exec structure. */
153 nr = read(rfd, &ebuf, sizeof(struct exec));
154 if (nr != sizeof(struct exec))
157 /* Check magic number and symbol count. */
158 if (N_BADMAG(ebuf) || ebuf.a_syms == 0)
161 /* Seek to string table. */
162 if (lseek(rfd, r_off + N_STROFF(ebuf), SEEK_SET) == (off_t)-1)
165 /* Read in size of the string table. */
166 nr = read(rfd, &strsize, sizeof(strsize));
167 if (nr != sizeof(strsize))
170 /* Read in the string table. */
171 strsize -= sizeof(strsize);
172 strtab = emalloc(strsize);
173 nr = read(rfd, strtab, strsize);
180 /* Seek to symbol table. */
181 if (fseek(fp, (long)r_off + N_SYMOFF(ebuf), SEEK_SET))
184 /* For each symbol read the nlist entry and save it as necessary. */
185 nsyms = ebuf.a_syms / sizeof(struct nlist);
187 if (!fread(&nl, sizeof(struct nlist), 1, fp)) {
193 /* Ignore if no name or local. */
194 if (!nl.n_un.n_strx || !(nl.n_type & N_EXT))
198 * If the symbol is an undefined external and the n_value
199 * field is non-zero, keep it.
201 if ((nl.n_type & N_TYPE) == N_UNDF && !nl.n_value)
204 /* First four bytes are the table size. */
205 sym = strtab + nl.n_un.n_strx - sizeof(long);
206 symlen = strlen(sym) + 1;
208 rp = (RLIB *)emalloc(sizeof(RLIB));
209 rp->sym = (char *)emalloc(symlen);
210 bcopy(sym, rp->sym, symlen);
214 /* Build in forward order for "ar -m" command. */
223 bad1: (void)lseek(rfd, r_off, SEEK_SET);
228 * Write the symbol table into the archive, computing offsets as
234 register RLIB *rp, *rpnext;
238 char hb[sizeof(struct ar_hdr) + 1], pad;
240 /* Rewind the archive, leaving the magic number. */
241 if (fseek(fp, (long)SARMAG, SEEK_SET))
244 /* Size of the ranlib archive file, pad if necessary. */
245 ransize = sizeof(long) +
246 symcnt * sizeof(struct ranlib) + sizeof(long) + tsymlen;
253 /* Put out the ranlib archive file header. */
254 #define DEFMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
255 (void)sprintf(hb, HDR2, RANLIBMAG, 0L, getuid(), getgid(),
256 DEFMODE & ~umask(0), ransize, ARFMAG);
257 if (!fwrite(hb, sizeof(struct ar_hdr), 1, fp))
260 /* First long is the size of the ranlib structure section. */
261 size = symcnt * sizeof(struct ranlib);
262 if (!fwrite(&size, sizeof(size), 1, fp))
265 /* Offset of the first archive file. */
266 size = SARMAG + sizeof(struct ar_hdr) + ransize;
269 * Write out the ranlib structures. The offset into the string
270 * table is cumulative, the offset into the archive is the value
271 * set in rexec() plus the offset to the first archive file.
273 for (rp = rhead, stroff = 0; rp; rp = rp->next) {
274 rn.ran_un.ran_strx = stroff;
275 stroff += rp->symlen;
276 rn.ran_off = size + rp->pos;
277 if (!fwrite(&rn, sizeof(struct ranlib), 1, fp))
281 /* Second long is the size of the string table. */
282 if (!fwrite(&tsymlen, sizeof(tsymlen), 1, fp))
285 /* Write out the string table. */
286 for (rp = rhead; rp; rp = rpnext) {
287 if (!fwrite(rp->sym, rp->symlen, 1, fp))
295 if (pad && !fwrite(&pad, sizeof(pad), 1, fp))