Annotation of researchv9/cmd/strip/shrink.c, revision 1.1

1.1     ! root        1: #include "strip.h"
        !             2: #include "hash.h"
        !             3: #define        DESCSYMBS
        !             4: #include "stab.h"
        !             5: 
        !             6: struct hn {
        !             7:        struct nlist *symbol;
        !             8:        int len;
        !             9: };
        !            10: 
        !            11: static int nglobs; static struct hn *tabstart, *tabend;
        !            12: 
        !            13: shrink(a)
        !            14: register struct Adotout *a;
        !            15: {
        !            16:        register struct nlist *sym, *ssym;
        !            17:        register char *str; register int n;
        !            18: 
        !            19:        hashinit();
        !            20:        nglobs = 0;
        !            21: 
        !            22:        for (sym=a->symtab; sym<a->symend; sym++)
        !            23:                if (str = sym->n_un.n_name) {
        !            24:                        if (sym->n_type == N_GSYM || sym->n_type == N_BSTR)
        !            25:                                nglobs++;
        !            26:                        if (*(str += (int)a->strtab) == '_')
        !            27:                                sym->n_un.n_name = hash(str);
        !            28:                        else
        !            29:                                sym->n_un.n_name = str;
        !            30:                }
        !            31: 
        !            32:        for (sym=a->symtab; sym<a->symend; sym++)
        !            33:                if ((str = sym->n_un.n_name) && *str != '_')
        !            34:                        sym->n_un.n_name = hash(str);
        !            35: 
        !            36:        if (prtflag) prthash();
        !            37: 
        !            38:        Free(a->strtab);
        !            39:        if ((tabstart = Calloc(struct hn, nglobs)) == 0)
        !            40:                fatal("cannot malloc hashed symbol table", "");
        !            41:        tabend = tabstart + nglobs;
        !            42: 
        !            43:        for (sym=ssym=a->symtab; sym<a->symend;) {
        !            44:                if (gflag && (sym->n_type == N_SLINE ||
        !            45:                              sym->n_type == N_LBRAC || sym->n_type == N_RBRAC)) {
        !            46:                        sym++;
        !            47:                } else if (sym->n_type != N_GSYM && sym->n_type != N_BSTR) {
        !            48:                        if (ssym != sym)
        !            49:                                *ssym++ = *sym++;
        !            50:                        else
        !            51:                                ssym++, sym++;
        !            52:                } else if ((n = lookup(sym, ssym)) < 0) {
        !            53:                        sym -= n;
        !            54:                } else if (ssym != sym) {
        !            55:                        while (--n >= 0)
        !            56:                                *ssym++ = *sym++;
        !            57:                } else {
        !            58:                        ssym += n, sym += n;
        !            59:                }
        !            60:        }
        !            61:        Free(tabstart);
        !            62:        a->symend = ssym;
        !            63:        a->nsymbols = ssym - a->symtab;
        !            64:        a->symtab = Realloc(struct nlist, a->symtab, a->nsymbols);
        !            65:        if (prtflag)
        !            66:                printf("number of symbols reduced to %d\n", a->nsymbols);
        !            67: }
        !            68: 
        !            69: lookup(sym, where)
        !            70: register struct nlist *sym; struct nlist *where;
        !            71: {
        !            72:        register struct nlist *t;
        !            73:        register struct hn *pt; struct hn *ptstart;
        !            74:        int lsym = symlen(sym);
        !            75: 
        !            76:        pt = ptstart = tabstart +
        !            77:            ((unsigned long)(sym->n_un.n_name)) % nglobs;
        !            78: 
        !            79:        do {
        !            80:                if ((t = pt->symbol) == 0) {
        !            81:                        pt->symbol = where;
        !            82:                        pt->len = lsym;
        !            83:                        return lsym;
        !            84:                }
        !            85:                if (t->n_un.n_name != sym->n_un.n_name ||
        !            86:                    t->n_type != sym->n_type) {
        !            87:                        continue;
        !            88:                }
        !            89:                if (pt->len == lsym && nlistcmp(t, sym, lsym) == 0) {
        !            90:                        return -lsym;
        !            91:                } else {
        !            92:                        return lsym;
        !            93:                }
        !            94:        } while ((++pt >= tabend ? pt = tabstart : pt) != ptstart);
        !            95: 
        !            96:        fatal("hashed symbol table full", "");
        !            97: }
        !            98: 
        !            99: symlen(sym)
        !           100: register struct nlist *sym;
        !           101: {
        !           102:        register len = 1, t, m;
        !           103:        if (sym[1].n_type == N_TYSIG)
        !           104:                ++len;
        !           105:        else if (sym->n_type == N_BSTR) {
        !           106:                do ++len; while ((++sym)->n_type != N_ESTR);
        !           107:        } else {
        !           108:                switch (BTYPE(t = sym->n_desc)) {
        !           109:                case STRTY:
        !           110:                case UNIONTY:
        !           111:                case ENUMTY:
        !           112:                        ++len;
        !           113:                }
        !           114:                for (; m = t&TMASK; t >>= TSHIFT)
        !           115:                        if (m == ARY) ++len;
        !           116:        }
        !           117:        return len;
        !           118: }
        !           119: 
        !           120: nlistcmp(a,b,n)
        !           121: register int *a, *b, n;
        !           122: {
        !           123:        n *= sizeof(struct nlist)/sizeof(int);
        !           124:        while (--n >= 0)
        !           125:                if (*a++ != *b++) return 1;
        !           126:        return 0;
        !           127: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.