[BACK]Return to build.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ranlib

Annotation of src/usr.bin/ranlib/build.c, Revision 1.8

1.8     ! mpech       1: /*     $OpenBSD: build.c,v 1.7 1999/09/21 13:15:43 espie 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.8     ! mpech      41: static char rcsid[] = "$OpenBSD: build.c,v 1.7 1999/09/21 13:15:43 espie 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>
1.7       espie      55: #include <stdlib.h>
1.1       deraadt    56: #include <string.h>
                     57: #include <archive.h>
1.7       espie      58: #include <err.h>
1.6       espie      59: #include "byte.c"
1.7       espie      60: #include "extern.h"
1.6       espie      61:
1.1       deraadt    62:
                     63: extern CHDR chdr;                      /* converted header */
                     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: static FILE    *fp;
                     74: static long    symcnt;                 /* symbol count */
                     75: static long    tsymlen;                /* total string length */
                     76:
1.6       espie      77: static int rexec();
                     78: static void symobj();
1.1       deraadt    79:
1.7       espie      80: int
1.1       deraadt    81: build()
                     82: {
                     83:        CF cf;
                     84:        int afd, tfd;
1.6       espie      85:        int current_mid;
1.1       deraadt    86:        off_t size;
                     87:
1.7       espie      88:        current_mid = -1;
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);
1.7       espie     106:                if (new_mid != -1) {
                    107:                        if (current_mid == -1)
                    108:                                current_mid = new_mid;
                    109:                        else if (new_mid != current_mid)
                    110:                                errx(1, "Mixed object format archive: %d / %d",
                    111:                                        new_mid, current_mid);
                    112:                }
1.1       deraadt   113:                put_arobj(&cf, (struct stat *)NULL);
                    114:        }
                    115:        *pnext = NULL;
                    116:
1.6       espie     117:        /* Create the symbol table.  Endianess the same as last mid seen */
                    118:        symobj(current_mid);
1.1       deraadt   119:
                    120:        /* Copy the saved objects into the archive. */
                    121:        size = lseek(tfd, (off_t)0, SEEK_CUR);
                    122:        (void)lseek(tfd, (off_t)0, SEEK_SET);
                    123:        SETCF(tfd, tname, afd, archive, NOPAD);
                    124:        copy_ar(&cf, size);
                    125:        (void)ftruncate(afd, lseek(afd, (off_t)0, SEEK_CUR));
                    126:        (void)close(tfd);
                    127:
                    128:        /* Set the time. */
                    129:        settime(afd);
                    130:        close_archive(afd);
                    131:        return(0);
                    132: }
                    133:
                    134: /*
                    135:  * rexec
                    136:  *     Read the exec structure; ignore any files that don't look
1.6       espie     137:  *     exactly right. Return MID.
1.7       espie     138:  *     return -1 for files that don't look right.
                    139:  *     XXX it's hard to be sure when to ignore files, and when to error
                    140:  *     out.
1.1       deraadt   141:  */
1.6       espie     142: static int
1.1       deraadt   143: rexec(rfd, wfd)
1.8     ! mpech     144:        int rfd;
1.1       deraadt   145:        int wfd;
                    146: {
1.8     ! mpech     147:        RLIB *rp;
        !           148:        long nsyms;
        !           149:        int nr, symlen;
        !           150:        char *strtab = 0;
1.7       espie     151:        char *sym;
1.1       deraadt   152:        struct exec ebuf;
                    153:        struct nlist nl;
                    154:        off_t r_off, w_off;
                    155:        long strsize;
1.7       espie     156:        int result = -1;
1.1       deraadt   157:
                    158:        /* Get current offsets for original and tmp files. */
                    159:        r_off = lseek(rfd, (off_t)0, SEEK_CUR);
                    160:        w_off = lseek(wfd, (off_t)0, SEEK_CUR);
                    161:
                    162:        /* Read in exec structure. */
                    163:        nr = read(rfd, (char *)&ebuf, sizeof(struct exec));
                    164:        if (nr != sizeof(struct exec))
1.7       espie     165:                goto bad;
1.1       deraadt   166:
                    167:        /* Check magic number and symbol count. */
1.6       espie     168:        if (BAD_OBJECT(ebuf) || ebuf.a_syms == 0)
1.7       espie     169:                goto bad;
1.6       espie     170:        fix_header_order(&ebuf);
1.1       deraadt   171:
                    172:        /* Seek to string table. */
1.7       espie     173:        if (lseek(rfd, N_STROFF(ebuf) + r_off, SEEK_SET) == (off_t)-1) {
                    174:                if (errno == EINVAL)
                    175:                        goto bad;
                    176:                else
                    177:                        error(archive);
                    178:        }
1.1       deraadt   179:
                    180:        /* Read in size of the string table. */
                    181:        nr = read(rfd, (char *)&strsize, sizeof(strsize));
                    182:        if (nr != sizeof(strsize))
1.7       espie     183:                goto bad;
1.1       deraadt   184:
1.6       espie     185:        strsize = fix_long_order(strsize, N_GETMID(ebuf));
1.7       espie     186:
1.1       deraadt   187:        /* Read in the string table. */
                    188:        strsize -= sizeof(strsize);
                    189:        strtab = (char *)emalloc(strsize);
                    190:        nr = read(rfd, strtab, strsize);
1.7       espie     191:        if (nr != strsize)
                    192:                goto bad;
1.1       deraadt   193:
                    194:        /* Seek to symbol table. */
                    195:        if (fseek(fp, N_SYMOFF(ebuf) + r_off, SEEK_SET) == (off_t)-1)
1.7       espie     196:                goto bad;
1.1       deraadt   197:
1.7       espie     198:        result = N_GETMID(ebuf);
1.1       deraadt   199:        /* For each symbol read the nlist entry and save it as necessary. */
                    200:        nsyms = ebuf.a_syms / sizeof(struct nlist);
                    201:        while (nsyms--) {
                    202:                if (!fread((char *)&nl, sizeof(struct nlist), 1, fp)) {
                    203:                        if (feof(fp))
                    204:                                badfmt();
                    205:                        error(archive);
                    206:                }
1.7       espie     207:                fix_nlist_order(&nl, N_GETMID(ebuf));
1.1       deraadt   208:
                    209:                /* Ignore if no name or local. */
                    210:                if (!nl.n_un.n_strx || !(nl.n_type & N_EXT))
                    211:                        continue;
                    212:
                    213:                /*
                    214:                 * If the symbol is an undefined external and the n_value
                    215:                 * field is non-zero, keep it.
                    216:                 */
                    217:                if ((nl.n_type & N_TYPE) == N_UNDF && !nl.n_value)
                    218:                        continue;
                    219:
                    220:                /* First four bytes are the table size. */
                    221:                sym = strtab + nl.n_un.n_strx - sizeof(long);
                    222:                symlen = strlen(sym) + 1;
                    223:
                    224:                rp = (RLIB *)emalloc(sizeof(RLIB));
                    225:                rp->sym = (char *)emalloc(symlen);
                    226:                bcopy(sym, rp->sym, symlen);
                    227:                rp->symlen = symlen;
                    228:                rp->pos = w_off;
                    229:
                    230:                /* Build in forward order for "ar -m" command. */
                    231:                *pnext = rp;
                    232:                pnext = &rp->next;
                    233:
                    234:                ++symcnt;
                    235:                tsymlen += symlen;
                    236:        }
                    237:
1.7       espie     238: bad:   if (nr < 0)
                    239:                error(archive);
                    240:        free(strtab);
                    241:        (void)lseek(rfd, (off_t)r_off, SEEK_SET);
                    242:        return result;
1.1       deraadt   243: }
                    244:
                    245: /*
                    246:  * symobj --
                    247:  *     Write the symbol table into the archive, computing offsets as
1.6       espie     248:  *     writing.  Use the right format depending on mid.
1.1       deraadt   249:  */
                    250: static void
1.6       espie     251: symobj(mid)
                    252:        int mid;
1.1       deraadt   253: {
1.8     ! mpech     254:        RLIB *rp, *rnext;
1.1       deraadt   255:        struct ranlib rn;
                    256:        char hb[sizeof(struct ar_hdr) + 1], pad;
                    257:        long ransize, size, stroff;
1.3       deraadt   258:        uid_t uid;
                    259:        gid_t gid;
1.1       deraadt   260:
                    261:        /* Rewind the archive, leaving the magic number. */
                    262:        if (fseek(fp, (off_t)SARMAG, SEEK_SET) == (off_t)-1)
                    263:                error(archive);
                    264:
                    265:        /* Size of the ranlib archive file, pad if necessary. */
                    266:        ransize = sizeof(long) +
                    267:            symcnt * sizeof(struct ranlib) + sizeof(long) + tsymlen;
                    268:        if (ransize & 01) {
                    269:                ++ransize;
                    270:                pad = '\n';
                    271:        } else
                    272:                pad = '\0';
                    273:
1.3       deraadt   274:        uid = getuid();
                    275:        if (uid > USHRT_MAX) {
1.4       deraadt   276:                warnx("warning: uid %u truncated to %u", uid, USHRT_MAX);
1.3       deraadt   277:                uid = USHRT_MAX;
                    278:        }
                    279:        gid = getgid();
                    280:        if (gid > USHRT_MAX) {
1.4       deraadt   281:                warnx("warning: gid %u truncated to %u", gid, USHRT_MAX);
1.3       deraadt   282:                gid = USHRT_MAX;
                    283:        }
                    284:
1.1       deraadt   285:        /* Put out the ranlib archive file header. */
                    286: #define        DEFMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
1.3       deraadt   287:        (void)sprintf(hb, HDR2, RANLIBMAG, 0L, uid, gid,
1.1       deraadt   288:            DEFMODE & ~umask(0), (off_t)ransize, ARFMAG);
                    289:        if (!fwrite(hb, sizeof(struct ar_hdr), 1, fp))
                    290:                error(tname);
                    291:
                    292:        /* First long is the size of the ranlib structure section. */
1.6       espie     293:        size = fix_long_order(symcnt * sizeof(struct ranlib), mid);
1.1       deraadt   294:        if (!fwrite((char *)&size, sizeof(size), 1, fp))
                    295:                error(tname);
                    296:
                    297:        /* Offset of the first archive file. */
                    298:        size = SARMAG + sizeof(struct ar_hdr) + ransize;
                    299:
                    300:        /*
                    301:         * Write out the ranlib structures.  The offset into the string
                    302:         * table is cumulative, the offset into the archive is the value
                    303:         * set in rexec() plus the offset to the first archive file.
                    304:         */
                    305:        for (rp = rhead, stroff = 0; rp; rp = rp->next) {
                    306:                rn.ran_un.ran_strx = stroff;
                    307:                stroff += rp->symlen;
                    308:                rn.ran_off = size + rp->pos;
1.6       espie     309:                fix_ranlib_order(&rn, mid);
1.1       deraadt   310:                if (!fwrite((char *)&rn, sizeof(struct ranlib), 1, fp))
                    311:                        error(archive);
                    312:        }
                    313:
                    314:        /* Second long is the size of the string table. */
1.6       espie     315:
                    316:        size = fix_long_order(tsymlen, mid);
                    317:        if (!fwrite((char *)&size, sizeof(size), 1, fp))
1.1       deraadt   318:                error(tname);
                    319:
                    320:        /* Write out the string table. */
                    321:        for (rp = rhead; rp; rp = rnext) {
                    322:                if (!fwrite(rp->sym, rp->symlen, 1, fp))
                    323:                        error(tname);
                    324:                rnext = rp->next;
                    325:                free(rp);
                    326:        }
                    327:        rhead = NULL;
                    328:
                    329:        if (pad && !fwrite(&pad, sizeof(pad), 1, fp))
                    330:                error(tname);
                    331:
                    332:        (void)fflush(fp);
                    333: }