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