]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/ranlib/build.c
This commit was generated by cvs2svn to compensate for changes in r1935,
[FreeBSD/FreeBSD.git] / usr.bin / ranlib / build.c
1 /*-
2  * Copyright (c) 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Hugh Smith at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
23  *
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
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 static char sccsid[] = "@(#)build.c     8.1 (Berkeley) 6/6/93";
39 #endif /* not lint */
40
41 #include <sys/types.h>
42 #include <sys/errno.h>
43 #include <sys/stat.h>
44
45 #include <a.out.h>
46 #include <ar.h>
47 #include <dirent.h>
48 #include <fcntl.h>
49 #include <ranlib.h>
50 #include <stdio.h>
51 #include <unistd.h>
52
53 #include "archive.h"
54
55 extern CHDR chdr;                       /* converted header */
56 extern char *archive;                   /* archive name */
57 extern char *tname;                     /* temporary file "name" */
58
59 typedef struct _rlib {
60         struct _rlib *next;             /* next structure */
61         off_t pos;                      /* offset of defining archive file */
62         char *sym;                      /* symbol */
63         int symlen;                     /* strlen(sym) */
64 } RLIB;
65 RLIB *rhead, **pnext;
66
67 FILE *fp;
68
69 long symcnt;                            /* symbol count */
70 long tsymlen;                           /* total string length */
71
72 static void rexec __P((int, int));
73 static void symobj __P((void));
74
75 build()
76 {
77         CF cf;
78         int afd, tfd;
79         off_t size;
80
81         afd = open_archive(O_RDWR);
82         fp = fdopen(afd, "r+");
83         tfd = tmp();
84
85         SETCF(afd, archive, tfd, tname, RPAD|WPAD);
86
87         /* Read through the archive, creating list of symbols. */
88         pnext = &rhead;
89         symcnt = tsymlen = 0;
90         while(get_arobj(afd)) {
91                 if (!strcmp(chdr.name, RANLIBMAG)) {
92                         skip_arobj(afd);
93                         continue;
94                 }
95                 rexec(afd, tfd);
96                 put_arobj(&cf, (struct stat *)NULL);
97         }
98         *pnext = NULL;
99
100         /* Create the symbol table. */
101         symobj();
102
103         /* Copy the saved objects into the archive. */
104         size = lseek(tfd, (off_t)0, SEEK_CUR);
105         (void)lseek(tfd, (off_t)0, SEEK_SET);
106         SETCF(tfd, tname, afd, archive, RPAD|WPAD);
107         copy_ar(&cf, size);
108         (void)ftruncate(afd, lseek(afd, (off_t)0, SEEK_CUR));
109         (void)close(tfd);
110
111         /* Set the time. */
112         settime(afd);
113         close_archive(afd);
114         return(0);
115 }
116
117 /*
118  * rexec
119  *      Read the exec structure; ignore any files that don't look
120  *      exactly right.
121  */
122 static void
123 rexec(rfd, wfd)
124         register int rfd;
125         int wfd;
126 {
127         register RLIB *rp;
128         register long nsyms;
129         register int nr, symlen;
130         register char *strtab, *sym;
131         struct exec ebuf;
132         struct nlist nl;
133         off_t r_off, w_off;
134         long strsize;
135         void *emalloc();
136
137         /* Get current offsets for original and tmp files. */
138         r_off = lseek(rfd, (off_t)0, SEEK_CUR);
139         w_off = lseek(wfd, (off_t)0, SEEK_CUR);
140
141         /* Read in exec structure. */
142         nr = read(rfd, &ebuf, sizeof(struct exec));
143         if (nr != sizeof(struct exec))
144                 goto badread;
145
146         /* Check magic number and symbol count. */
147         if (N_BADMAG(ebuf) || ebuf.a_syms == 0)
148                 goto bad1;
149
150         /* Seek to string table. */
151         if (lseek(rfd, r_off + N_STROFF(ebuf), SEEK_SET) == (off_t)-1)
152                 error(archive);
153
154         /* Read in size of the string table. */
155         nr = read(rfd, &strsize, sizeof(strsize));
156         if (nr != sizeof(strsize))
157                 goto badread;
158
159         /* Read in the string table. */
160         strsize -= sizeof(strsize);
161         strtab = emalloc(strsize);
162         nr = read(rfd, strtab, strsize);
163         if (nr != strsize) {
164 badread:        if (nr < 0)
165                         error(archive);
166                 goto bad2;
167         }
168
169         /* Seek to symbol table. */
170         if (fseek(fp, (long)r_off + N_SYMOFF(ebuf), SEEK_SET))
171                 goto bad2;
172
173         /* For each symbol read the nlist entry and save it as necessary. */
174         nsyms = ebuf.a_syms / sizeof(struct nlist);
175         while (nsyms--) {
176                 if (!fread(&nl, sizeof(struct nlist), 1, fp)) {
177                         if (feof(fp))
178                                 badfmt();
179                         error(archive);
180                 }
181
182                 /* Ignore if no name or local. */
183                 if (!nl.n_un.n_strx || !(nl.n_type & N_EXT))
184                         continue;
185
186                 /*
187                  * If the symbol is an undefined external and the n_value
188                  * field is non-zero, keep it.
189                  */
190                 if ((nl.n_type & N_TYPE) == N_UNDF && !nl.n_value)
191                         continue;
192
193                 /* First four bytes are the table size. */
194                 sym = strtab + nl.n_un.n_strx - sizeof(long);
195                 symlen = strlen(sym) + 1;
196
197                 rp = (RLIB *)emalloc(sizeof(RLIB));
198                 rp->sym = (char *)emalloc(symlen);
199                 bcopy(sym, rp->sym, symlen);
200                 rp->symlen = symlen;
201                 rp->pos = w_off;
202
203                 /* Build in forward order for "ar -m" command. */
204                 *pnext = rp;
205                 pnext = &rp->next;
206
207                 ++symcnt;
208                 tsymlen += symlen;
209         }
210
211 bad2:   free(strtab);
212 bad1:   (void)lseek(rfd, r_off, SEEK_SET);
213 }
214
215 /*
216  * symobj --
217  *      Write the symbol table into the archive, computing offsets as
218  *      writing.
219  */
220 static void
221 symobj()
222 {
223         register RLIB *rp;
224         struct ranlib rn;
225         off_t ransize;
226         long size, stroff;
227         char hb[sizeof(struct ar_hdr) + 1], pad;
228
229         /* Rewind the archive, leaving the magic number. */
230         if (fseek(fp, (long)SARMAG, SEEK_SET))
231                 error(archive);
232
233         /* Size of the ranlib archive file, pad if necessary. */
234         ransize = sizeof(long) +
235             symcnt * sizeof(struct ranlib) + sizeof(long) + tsymlen;
236         if (ransize & 01) {
237                 ++ransize;
238                 pad = '\n';
239         } else
240                 pad = '\0';
241
242         /* Put out the ranlib archive file header. */
243 #define DEFMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
244         (void)sprintf(hb, HDR2, RANLIBMAG, 0L, getuid(), getgid(),
245             DEFMODE & ~umask(0), ransize, ARFMAG);
246         if (!fwrite(hb, sizeof(struct ar_hdr), 1, fp))
247                 error(tname);
248
249         /* First long is the size of the ranlib structure section. */
250         size = symcnt * sizeof(struct ranlib);
251         if (!fwrite(&size, sizeof(size), 1, fp))
252                 error(tname);
253
254         /* Offset of the first archive file. */
255         size = SARMAG + sizeof(struct ar_hdr) + ransize;
256
257         /*
258          * Write out the ranlib structures.  The offset into the string
259          * table is cumulative, the offset into the archive is the value
260          * set in rexec() plus the offset to the first archive file.
261          */
262         for (rp = rhead, stroff = 0; rp; rp = rp->next) {
263                 rn.ran_un.ran_strx = stroff;
264                 stroff += rp->symlen;
265                 rn.ran_off = size + rp->pos;
266                 if (!fwrite(&rn, sizeof(struct ranlib), 1, fp))
267                         error(archive);
268         }
269
270         /* Second long is the size of the string table. */
271         if (!fwrite(&tsymlen, sizeof(tsymlen), 1, fp))
272                 error(tname);
273
274         /* Write out the string table. */
275         for (rp = rhead; rp; rp = rp->next)
276                 if (!fwrite(rp->sym, rp->symlen, 1, fp))
277                         error(tname);
278
279         if (pad && !fwrite(&pad, sizeof(pad), 1, fp))
280                 error(tname);
281
282         (void)fflush(fp);
283 }