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

1.12    ! mickey      1: /*     $OpenBSD: build.c,v 1.11 2003/06/12 20:58:10 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.12    ! mickey     37: static char rcsid[] = "$OpenBSD: build.c,v 1.11 2003/06/12 20:58:10 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.11      deraadt    77: build(void)
1.1       deraadt    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.11      deraadt   139: rexec(int rfd, int wfd)
1.1       deraadt   140: {
1.8       mpech     141:        RLIB *rp;
                    142:        long nsyms;
                    143:        int nr, symlen;
                    144:        char *strtab = 0;
1.7       espie     145:        char *sym;
1.1       deraadt   146:        struct exec ebuf;
                    147:        struct nlist nl;
                    148:        off_t r_off, w_off;
                    149:        long strsize;
1.7       espie     150:        int result = -1;
1.1       deraadt   151:
                    152:        /* Get current offsets for original and tmp files. */
                    153:        r_off = lseek(rfd, (off_t)0, SEEK_CUR);
                    154:        w_off = lseek(wfd, (off_t)0, SEEK_CUR);
                    155:
                    156:        /* Read in exec structure. */
                    157:        nr = read(rfd, (char *)&ebuf, sizeof(struct exec));
                    158:        if (nr != sizeof(struct exec))
1.7       espie     159:                goto bad;
1.1       deraadt   160:
                    161:        /* Check magic number and symbol count. */
1.6       espie     162:        if (BAD_OBJECT(ebuf) || ebuf.a_syms == 0)
1.7       espie     163:                goto bad;
1.6       espie     164:        fix_header_order(&ebuf);
1.1       deraadt   165:
                    166:        /* Seek to string table. */
1.7       espie     167:        if (lseek(rfd, N_STROFF(ebuf) + r_off, SEEK_SET) == (off_t)-1) {
                    168:                if (errno == EINVAL)
                    169:                        goto bad;
                    170:                else
                    171:                        error(archive);
                    172:        }
1.1       deraadt   173:
                    174:        /* Read in size of the string table. */
                    175:        nr = read(rfd, (char *)&strsize, sizeof(strsize));
                    176:        if (nr != sizeof(strsize))
1.7       espie     177:                goto bad;
1.1       deraadt   178:
1.12    ! mickey    179:        strsize = fix_32_order(strsize, N_GETMID(ebuf));
1.7       espie     180:
1.1       deraadt   181:        /* Read in the string table. */
                    182:        strsize -= sizeof(strsize);
                    183:        strtab = (char *)emalloc(strsize);
                    184:        nr = read(rfd, strtab, strsize);
1.7       espie     185:        if (nr != strsize)
                    186:                goto bad;
1.1       deraadt   187:
                    188:        /* Seek to symbol table. */
                    189:        if (fseek(fp, N_SYMOFF(ebuf) + r_off, SEEK_SET) == (off_t)-1)
1.7       espie     190:                goto bad;
1.1       deraadt   191:
1.7       espie     192:        result = N_GETMID(ebuf);
1.1       deraadt   193:        /* For each symbol read the nlist entry and save it as necessary. */
                    194:        nsyms = ebuf.a_syms / sizeof(struct nlist);
                    195:        while (nsyms--) {
                    196:                if (!fread((char *)&nl, sizeof(struct nlist), 1, fp)) {
                    197:                        if (feof(fp))
                    198:                                badfmt();
                    199:                        error(archive);
                    200:                }
1.7       espie     201:                fix_nlist_order(&nl, N_GETMID(ebuf));
1.1       deraadt   202:
                    203:                /* Ignore if no name or local. */
                    204:                if (!nl.n_un.n_strx || !(nl.n_type & N_EXT))
                    205:                        continue;
                    206:
                    207:                /*
                    208:                 * If the symbol is an undefined external and the n_value
                    209:                 * field is non-zero, keep it.
                    210:                 */
                    211:                if ((nl.n_type & N_TYPE) == N_UNDF && !nl.n_value)
                    212:                        continue;
                    213:
                    214:                /* First four bytes are the table size. */
                    215:                sym = strtab + nl.n_un.n_strx - sizeof(long);
                    216:                symlen = strlen(sym) + 1;
                    217:
                    218:                rp = (RLIB *)emalloc(sizeof(RLIB));
                    219:                rp->sym = (char *)emalloc(symlen);
                    220:                bcopy(sym, rp->sym, symlen);
                    221:                rp->symlen = symlen;
                    222:                rp->pos = w_off;
                    223:
                    224:                /* Build in forward order for "ar -m" command. */
                    225:                *pnext = rp;
                    226:                pnext = &rp->next;
                    227:
                    228:                ++symcnt;
                    229:                tsymlen += symlen;
                    230:        }
                    231:
1.7       espie     232: bad:   if (nr < 0)
                    233:                error(archive);
                    234:        free(strtab);
                    235:        (void)lseek(rfd, (off_t)r_off, SEEK_SET);
                    236:        return result;
1.1       deraadt   237: }
                    238:
                    239: /*
                    240:  * symobj --
                    241:  *     Write the symbol table into the archive, computing offsets as
1.6       espie     242:  *     writing.  Use the right format depending on mid.
1.1       deraadt   243:  */
                    244: static void
1.11      deraadt   245: symobj(int mid)
1.1       deraadt   246: {
1.8       mpech     247:        RLIB *rp, *rnext;
1.1       deraadt   248:        struct ranlib rn;
                    249:        char hb[sizeof(struct ar_hdr) + 1], pad;
                    250:        long ransize, size, stroff;
1.3       deraadt   251:        uid_t uid;
                    252:        gid_t gid;
1.1       deraadt   253:
                    254:        /* Rewind the archive, leaving the magic number. */
                    255:        if (fseek(fp, (off_t)SARMAG, SEEK_SET) == (off_t)-1)
                    256:                error(archive);
                    257:
                    258:        /* Size of the ranlib archive file, pad if necessary. */
                    259:        ransize = sizeof(long) +
                    260:            symcnt * sizeof(struct ranlib) + sizeof(long) + tsymlen;
                    261:        if (ransize & 01) {
                    262:                ++ransize;
                    263:                pad = '\n';
                    264:        } else
                    265:                pad = '\0';
                    266:
1.3       deraadt   267:        uid = getuid();
                    268:        if (uid > USHRT_MAX) {
1.4       deraadt   269:                warnx("warning: uid %u truncated to %u", uid, USHRT_MAX);
1.3       deraadt   270:                uid = USHRT_MAX;
                    271:        }
                    272:        gid = getgid();
                    273:        if (gid > USHRT_MAX) {
1.4       deraadt   274:                warnx("warning: gid %u truncated to %u", gid, USHRT_MAX);
1.3       deraadt   275:                gid = USHRT_MAX;
                    276:        }
                    277:
1.1       deraadt   278:        /* Put out the ranlib archive file header. */
                    279: #define        DEFMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
1.9       deraadt   280:        (void)snprintf(hb, sizeof hb, HDR2, RANLIBMAG, 0L, uid, gid,
1.1       deraadt   281:            DEFMODE & ~umask(0), (off_t)ransize, ARFMAG);
                    282:        if (!fwrite(hb, sizeof(struct ar_hdr), 1, fp))
                    283:                error(tname);
                    284:
                    285:        /* First long is the size of the ranlib structure section. */
1.12    ! mickey    286:        size = fix_32_order(symcnt * sizeof(struct ranlib), mid);
1.1       deraadt   287:        if (!fwrite((char *)&size, sizeof(size), 1, fp))
                    288:                error(tname);
                    289:
                    290:        /* Offset of the first archive file. */
                    291:        size = SARMAG + sizeof(struct ar_hdr) + ransize;
                    292:
                    293:        /*
                    294:         * Write out the ranlib structures.  The offset into the string
                    295:         * table is cumulative, the offset into the archive is the value
                    296:         * set in rexec() plus the offset to the first archive file.
                    297:         */
                    298:        for (rp = rhead, stroff = 0; rp; rp = rp->next) {
                    299:                rn.ran_un.ran_strx = stroff;
                    300:                stroff += rp->symlen;
                    301:                rn.ran_off = size + rp->pos;
1.6       espie     302:                fix_ranlib_order(&rn, mid);
1.1       deraadt   303:                if (!fwrite((char *)&rn, sizeof(struct ranlib), 1, fp))
                    304:                        error(archive);
                    305:        }
                    306:
                    307:        /* Second long is the size of the string table. */
1.6       espie     308:
1.12    ! mickey    309:        size = fix_32_order(tsymlen, mid);
1.6       espie     310:        if (!fwrite((char *)&size, sizeof(size), 1, fp))
1.1       deraadt   311:                error(tname);
                    312:
                    313:        /* Write out the string table. */
                    314:        for (rp = rhead; rp; rp = rnext) {
                    315:                if (!fwrite(rp->sym, rp->symlen, 1, fp))
                    316:                        error(tname);
                    317:                rnext = rp->next;
                    318:                free(rp);
                    319:        }
                    320:        rhead = NULL;
                    321:
                    322:        if (pad && !fwrite(&pad, sizeof(pad), 1, fp))
                    323:                error(tname);
                    324:
                    325:        (void)fflush(fp);
                    326: }