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