[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.10

1.10    ! millert     1: /*     $OpenBSD: build.c,v 1.9 2003/04/04 22:12:09 deraadt 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.
1.10    ! millert    18:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
                     35: #ifndef lint
                     36: /*static char sccsid[] = "from: @(#)build.c    5.3 (Berkeley) 3/12/91";*/
1.10    ! millert    37: static char rcsid[] = "$OpenBSD: build.c,v 1.9 2003/04/04 22:12:09 deraadt Exp $";
1.1       deraadt    38: #endif /* not lint */
                     39:
                     40: #include <sys/types.h>
                     41: #include <sys/stat.h>
                     42: #include <errno.h>
                     43: #include <fcntl.h>
                     44: #include <a.out.h>
                     45: #include <dirent.h>
                     46: #include <unistd.h>
                     47: #include <ar.h>
1.5       deraadt    48: #include <limits.h>
1.1       deraadt    49: #include <ranlib.h>
                     50: #include <stdio.h>
1.7       espie      51: #include <stdlib.h>
1.1       deraadt    52: #include <string.h>
                     53: #include <archive.h>
1.7       espie      54: #include <err.h>
1.6       espie      55: #include "byte.c"
1.7       espie      56: #include "extern.h"
1.6       espie      57:
1.1       deraadt    58:
                     59: extern CHDR chdr;                      /* converted header */
                     60:
                     61: typedef struct _rlib {
                     62:        struct _rlib *next;             /* next structure */
                     63:        off_t pos;                      /* offset of defining archive file */
                     64:        char *sym;                      /* symbol */
                     65:        int symlen;                     /* strlen(sym) */
                     66: } RLIB;
                     67: RLIB *rhead, **pnext;
                     68:
                     69: static FILE    *fp;
                     70: static long    symcnt;                 /* symbol count */
                     71: static long    tsymlen;                /* total string length */
                     72:
1.6       espie      73: static int rexec();
                     74: static void symobj();
1.1       deraadt    75:
1.7       espie      76: int
1.1       deraadt    77: build()
                     78: {
                     79:        CF cf;
                     80:        int afd, tfd;
1.6       espie      81:        int current_mid;
1.1       deraadt    82:        off_t size;
                     83:
1.7       espie      84:        current_mid = -1;
1.1       deraadt    85:        afd = open_archive(O_RDWR);
                     86:        fp = fdopen(afd, "r+");
                     87:        tfd = tmp();
                     88:
                     89:        SETCF(afd, archive, tfd, tname, RPAD|WPAD);
                     90:
                     91:        /* Read through the archive, creating list of symbols. */
                     92:        symcnt = tsymlen = 0;
                     93:        pnext = &rhead;
                     94:        while(get_arobj(afd)) {
1.6       espie      95:                int new_mid;
                     96:
1.1       deraadt    97:                if (!strcmp(chdr.name, RANLIBMAG)) {
                     98:                        skip_arobj(afd);
                     99:                        continue;
                    100:                }
1.6       espie     101:                new_mid = rexec(afd, tfd);
1.7       espie     102:                if (new_mid != -1) {
                    103:                        if (current_mid == -1)
                    104:                                current_mid = new_mid;
                    105:                        else if (new_mid != current_mid)
                    106:                                errx(1, "Mixed object format archive: %d / %d",
                    107:                                        new_mid, current_mid);
                    108:                }
1.1       deraadt   109:                put_arobj(&cf, (struct stat *)NULL);
                    110:        }
                    111:        *pnext = NULL;
                    112:
1.6       espie     113:        /* Create the symbol table.  Endianess the same as last mid seen */
                    114:        symobj(current_mid);
1.1       deraadt   115:
                    116:        /* Copy the saved objects into the archive. */
                    117:        size = lseek(tfd, (off_t)0, SEEK_CUR);
                    118:        (void)lseek(tfd, (off_t)0, SEEK_SET);
                    119:        SETCF(tfd, tname, afd, archive, NOPAD);
                    120:        copy_ar(&cf, size);
                    121:        (void)ftruncate(afd, lseek(afd, (off_t)0, SEEK_CUR));
                    122:        (void)close(tfd);
                    123:
                    124:        /* Set the time. */
                    125:        settime(afd);
                    126:        close_archive(afd);
                    127:        return(0);
                    128: }
                    129:
                    130: /*
                    131:  * rexec
                    132:  *     Read the exec structure; ignore any files that don't look
1.6       espie     133:  *     exactly right. Return MID.
1.7       espie     134:  *     return -1 for files that don't look right.
                    135:  *     XXX it's hard to be sure when to ignore files, and when to error
                    136:  *     out.
1.1       deraadt   137:  */
1.6       espie     138: static int
1.1       deraadt   139: rexec(rfd, wfd)
1.8       mpech     140:        int rfd;
1.1       deraadt   141:        int wfd;
                    142: {
1.8       mpech     143:        RLIB *rp;
                    144:        long nsyms;
                    145:        int nr, symlen;
                    146:        char *strtab = 0;
1.7       espie     147:        char *sym;
1.1       deraadt   148:        struct exec ebuf;
                    149:        struct nlist nl;
                    150:        off_t r_off, w_off;
                    151:        long strsize;
1.7       espie     152:        int result = -1;
1.1       deraadt   153:
                    154:        /* Get current offsets for original and tmp files. */
                    155:        r_off = lseek(rfd, (off_t)0, SEEK_CUR);
                    156:        w_off = lseek(wfd, (off_t)0, SEEK_CUR);
                    157:
                    158:        /* Read in exec structure. */
                    159:        nr = read(rfd, (char *)&ebuf, sizeof(struct exec));
                    160:        if (nr != sizeof(struct exec))
1.7       espie     161:                goto bad;
1.1       deraadt   162:
                    163:        /* Check magic number and symbol count. */
1.6       espie     164:        if (BAD_OBJECT(ebuf) || ebuf.a_syms == 0)
1.7       espie     165:                goto bad;
1.6       espie     166:        fix_header_order(&ebuf);
1.1       deraadt   167:
                    168:        /* Seek to string table. */
1.7       espie     169:        if (lseek(rfd, N_STROFF(ebuf) + r_off, SEEK_SET) == (off_t)-1) {
                    170:                if (errno == EINVAL)
                    171:                        goto bad;
                    172:                else
                    173:                        error(archive);
                    174:        }
1.1       deraadt   175:
                    176:        /* Read in size of the string table. */
                    177:        nr = read(rfd, (char *)&strsize, sizeof(strsize));
                    178:        if (nr != sizeof(strsize))
1.7       espie     179:                goto bad;
1.1       deraadt   180:
1.6       espie     181:        strsize = fix_long_order(strsize, N_GETMID(ebuf));
1.7       espie     182:
1.1       deraadt   183:        /* Read in the string table. */
                    184:        strsize -= sizeof(strsize);
                    185:        strtab = (char *)emalloc(strsize);
                    186:        nr = read(rfd, strtab, strsize);
1.7       espie     187:        if (nr != strsize)
                    188:                goto bad;
1.1       deraadt   189:
                    190:        /* Seek to symbol table. */
                    191:        if (fseek(fp, N_SYMOFF(ebuf) + r_off, SEEK_SET) == (off_t)-1)
1.7       espie     192:                goto bad;
1.1       deraadt   193:
1.7       espie     194:        result = N_GETMID(ebuf);
1.1       deraadt   195:        /* For each symbol read the nlist entry and save it as necessary. */
                    196:        nsyms = ebuf.a_syms / sizeof(struct nlist);
                    197:        while (nsyms--) {
                    198:                if (!fread((char *)&nl, sizeof(struct nlist), 1, fp)) {
                    199:                        if (feof(fp))
                    200:                                badfmt();
                    201:                        error(archive);
                    202:                }
1.7       espie     203:                fix_nlist_order(&nl, N_GETMID(ebuf));
1.1       deraadt   204:
                    205:                /* Ignore if no name or local. */
                    206:                if (!nl.n_un.n_strx || !(nl.n_type & N_EXT))
                    207:                        continue;
                    208:
                    209:                /*
                    210:                 * If the symbol is an undefined external and the n_value
                    211:                 * field is non-zero, keep it.
                    212:                 */
                    213:                if ((nl.n_type & N_TYPE) == N_UNDF && !nl.n_value)
                    214:                        continue;
                    215:
                    216:                /* First four bytes are the table size. */
                    217:                sym = strtab + nl.n_un.n_strx - sizeof(long);
                    218:                symlen = strlen(sym) + 1;
                    219:
                    220:                rp = (RLIB *)emalloc(sizeof(RLIB));
                    221:                rp->sym = (char *)emalloc(symlen);
                    222:                bcopy(sym, rp->sym, symlen);
                    223:                rp->symlen = symlen;
                    224:                rp->pos = w_off;
                    225:
                    226:                /* Build in forward order for "ar -m" command. */
                    227:                *pnext = rp;
                    228:                pnext = &rp->next;
                    229:
                    230:                ++symcnt;
                    231:                tsymlen += symlen;
                    232:        }
                    233:
1.7       espie     234: bad:   if (nr < 0)
                    235:                error(archive);
                    236:        free(strtab);
                    237:        (void)lseek(rfd, (off_t)r_off, SEEK_SET);
                    238:        return result;
1.1       deraadt   239: }
                    240:
                    241: /*
                    242:  * symobj --
                    243:  *     Write the symbol table into the archive, computing offsets as
1.6       espie     244:  *     writing.  Use the right format depending on mid.
1.1       deraadt   245:  */
                    246: static void
1.6       espie     247: symobj(mid)
                    248:        int mid;
1.1       deraadt   249: {
1.8       mpech     250:        RLIB *rp, *rnext;
1.1       deraadt   251:        struct ranlib rn;
                    252:        char hb[sizeof(struct ar_hdr) + 1], pad;
                    253:        long ransize, size, stroff;
1.3       deraadt   254:        uid_t uid;
                    255:        gid_t gid;
1.1       deraadt   256:
                    257:        /* Rewind the archive, leaving the magic number. */
                    258:        if (fseek(fp, (off_t)SARMAG, SEEK_SET) == (off_t)-1)
                    259:                error(archive);
                    260:
                    261:        /* Size of the ranlib archive file, pad if necessary. */
                    262:        ransize = sizeof(long) +
                    263:            symcnt * sizeof(struct ranlib) + sizeof(long) + tsymlen;
                    264:        if (ransize & 01) {
                    265:                ++ransize;
                    266:                pad = '\n';
                    267:        } else
                    268:                pad = '\0';
                    269:
1.3       deraadt   270:        uid = getuid();
                    271:        if (uid > USHRT_MAX) {
1.4       deraadt   272:                warnx("warning: uid %u truncated to %u", uid, USHRT_MAX);
1.3       deraadt   273:                uid = USHRT_MAX;
                    274:        }
                    275:        gid = getgid();
                    276:        if (gid > USHRT_MAX) {
1.4       deraadt   277:                warnx("warning: gid %u truncated to %u", gid, USHRT_MAX);
1.3       deraadt   278:                gid = USHRT_MAX;
                    279:        }
                    280:
1.1       deraadt   281:        /* Put out the ranlib archive file header. */
                    282: #define        DEFMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
1.9       deraadt   283:        (void)snprintf(hb, sizeof hb, HDR2, RANLIBMAG, 0L, uid, gid,
1.1       deraadt   284:            DEFMODE & ~umask(0), (off_t)ransize, ARFMAG);
                    285:        if (!fwrite(hb, sizeof(struct ar_hdr), 1, fp))
                    286:                error(tname);
                    287:
                    288:        /* First long is the size of the ranlib structure section. */
1.6       espie     289:        size = fix_long_order(symcnt * sizeof(struct ranlib), mid);
1.1       deraadt   290:        if (!fwrite((char *)&size, sizeof(size), 1, fp))
                    291:                error(tname);
                    292:
                    293:        /* Offset of the first archive file. */
                    294:        size = SARMAG + sizeof(struct ar_hdr) + ransize;
                    295:
                    296:        /*
                    297:         * Write out the ranlib structures.  The offset into the string
                    298:         * table is cumulative, the offset into the archive is the value
                    299:         * set in rexec() plus the offset to the first archive file.
                    300:         */
                    301:        for (rp = rhead, stroff = 0; rp; rp = rp->next) {
                    302:                rn.ran_un.ran_strx = stroff;
                    303:                stroff += rp->symlen;
                    304:                rn.ran_off = size + rp->pos;
1.6       espie     305:                fix_ranlib_order(&rn, mid);
1.1       deraadt   306:                if (!fwrite((char *)&rn, sizeof(struct ranlib), 1, fp))
                    307:                        error(archive);
                    308:        }
                    309:
                    310:        /* Second long is the size of the string table. */
1.6       espie     311:
                    312:        size = fix_long_order(tsymlen, mid);
                    313:        if (!fwrite((char *)&size, sizeof(size), 1, fp))
1.1       deraadt   314:                error(tname);
                    315:
                    316:        /* Write out the string table. */
                    317:        for (rp = rhead; rp; rp = rnext) {
                    318:                if (!fwrite(rp->sym, rp->symlen, 1, fp))
                    319:                        error(tname);
                    320:                rnext = rp->next;
                    321:                free(rp);
                    322:        }
                    323:        rhead = NULL;
                    324:
                    325:        if (pad && !fwrite(&pad, sizeof(pad), 1, fp))
                    326:                error(tname);
                    327:
                    328:        (void)fflush(fp);
                    329: }