cleanup from Chris Torek
[unix-history] / usr / src / old / as.vax / assyms.c
index b4db319..166cff3 100644 (file)
@@ -1,8 +1,11 @@
 /*
 /*
- *     Copyright (c) 1982 Regents of the University of California
+ * Copyright (c) 1982 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
  */
  */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)assyms.c 4.12 %G%";
+static char sccsid[] = "@(#)assyms.c   5.2 (Berkeley) %G%";
 #endif not lint
 
 #include <stdio.h>
 #endif not lint
 
 #include <stdio.h>
@@ -92,7 +95,11 @@ syminstall()
        }
 }      /*end of syminstall*/
 
        }
 }      /*end of syminstall*/
 
-
+#define ISLABEL(sp) \
+       (   (!savelabels) \
+        && (sp->s_tag == LABELID) \
+        && (STRPLACE(sp) & STR_CORE) \
+        && (FETCHNAME(sp)[0] == 'L'))
 /*
  *     Assign final values to symbols,
  *     and overwrite the index field with its relative position in
 /*
  *     Assign final values to symbols,
  *     and overwrite the index field with its relative position in
@@ -129,16 +136,11 @@ freezesymtab()
                        hdr.a_bss += bs;
                }
           assignindex:
                        hdr.a_bss += bs;
                }
           assignindex:
-               if (    (FETCHNAME(sp)[0] != 'L')
-                    || (sp->s_tag != LABELID)
-                    || savelabels
-                    )                  /*then, we will write it later on*/
-                               sp->s_index = relpos++;
+               if (!ISLABEL(sp))
+                       sp->s_index = relpos++;
        }
 }
 
        }
 }
 
-
-
 /*
  *     For all of the stabs that had their final value undefined during pass 1
  *     and during pass 2 assign a final value.
 /*
  *     For all of the stabs that had their final value undefined during pass 1
  *     and during pass 2 assign a final value.
@@ -349,7 +351,7 @@ char *tagstring(tag)
                case OKTOBUMP:          return("oktobump");
                case ISET:              return("iset");
                case ILSYM:             return("ilsym");
                case OKTOBUMP:          return("oktobump");
                case ISET:              return("iset");
                case ILSYM:             return("ilsym");
-               default:                sprintf(tagbuff,"%d", tag);
+               default:                (void)sprintf(tagbuff,"%d", tag);
                                        return(tagbuff);
        }
 }
                                        return(tagbuff);
        }
 }
@@ -393,7 +395,6 @@ struct symtab **lookup(instflg)
        static  struct  symtab  **emptyslot;
        static  struct  hashdallop *emptyhd;
        static  struct  symtab  **hp_ub;
        static  struct  symtab  **emptyslot;
        static  struct  hashdallop *emptyhd;
        static  struct  symtab  **hp_ub;
-       static  struct  strdesc strdp;
 
        emptyslot = 0;
        for (nprobes = 0, from = yytext;
 
        emptyslot = 0;
        for (nprobes = 0, from = yytext;
@@ -443,44 +444,57 @@ struct symtab **lookup(instflg)
                hdallop->h_nused++;
                for (from = yytext, len = 0; *from++; len++)
                        continue;
                hdallop->h_nused++;
                for (from = yytext, len = 0; *from++; len++)
                        continue;
-               /*
-                *      save string and trailing null, both
-                *      internally, and in the string temporary file
-                */
-               strdp.sd_stroff = strfilepos;
-               strdp.sd_place = STR_BOTH;
-               strdp.sd_strlen = len + 1;      /* length and null */
-               fputs(yytext, strfile);         /* string */
-               putc(0, strfile);               /* null */
-               strfilepos += strdp.sd_strlen;
-               (*hp)->s_name = (char *)savestr(yytext, &strdp);
+               (*hp)->s_name = (char *)savestr(yytext, len + 1, STR_BOTH);
        }
        return(hp);
 }      /*end of lookup*/
 /*
        }
        return(hp);
 }      /*end of lookup*/
 /*
- *     save a string str, descriptor strdp, in the string pool
+ *     save a string str with len in the places indicated by place
  */
  */
-struct strdesc *savestr(str, strdp)
+struct strdesc *savestr(str, len, place)
        char    *str;
        char    *str;
-       struct  strdesc *strdp;
+       int     len;
+       int     place;
 {
        reg     struct  strdesc *res;
                int     tlen;
 {
        reg     struct  strdesc *res;
                int     tlen;
-
+       /*
+        *      Compute the total length of the record to live in core
+        */
        tlen = sizeof(struct strdesc) - sizeof(res->sd_string);
        tlen = sizeof(struct strdesc) - sizeof(res->sd_string);
-       if (strdp->sd_place & STR_FILE)
-               tlen += strdp->sd_strlen;
-
+       if (place & STR_CORE)
+               tlen += len;
+       /*
+        *      See if there is enough space for the record,
+        *      and allocate the record.
+        */
        if (tlen >= (STRPOOLDALLOP - strplhead->str_nalloc))
                strpoolalloc();
        res = (struct strdesc *)(strplhead->str_names + strplhead->str_nalloc);
        if (tlen >= (STRPOOLDALLOP - strplhead->str_nalloc))
                strpoolalloc();
        res = (struct strdesc *)(strplhead->str_names + strplhead->str_nalloc);
-       res[0] = *strdp;
-       if (strdp->sd_place & STR_FILE)
-               movestr(res[0].sd_string, str, strdp->sd_strlen);
+       /*
+        *      Save the string information that is always present
+        */
+       res->sd_stroff = strfilepos;
+       res->sd_strlen = len;
+       res->sd_place = place;
+       /*
+        *      Now, save the string itself.  If str is null, then
+        *      the characters have already been dumped to the file
+        */
+       if ((place & STR_CORE) && str)
+               movestr(res[0].sd_string, str, len);
+       if (place & STR_FILE){
+               if (str){
+                       fwrite(str, 1, len, strfile);
+               }
+               strfilepos += len;
+       }
+       /*
+        *      Adjust the in core string pool size
+        */
        strplhead->str_nalloc += tlen;
        return(res);
 }
        strplhead->str_nalloc += tlen;
        return(res);
 }
-
 /*
  *     The relocation information is saved internally in an array of
  *     lists of relocation buffers.  The relocation buffers are
 /*
  *     The relocation information is saved internally in an array of
  *     lists of relocation buffers.  The relocation buffers are
@@ -640,9 +654,7 @@ int symwrite(symfile)
                int     symsdesired = NOUTSYMS;
        reg     struct  symtab *sp, *ub;
                char    *name;                  /* temp to save the name */
                int     symsdesired = NOUTSYMS;
        reg     struct  symtab *sp, *ub;
                char    *name;                  /* temp to save the name */
-               int     nread;
-               char    rbuf[2048];
-               int     i;
+               int     totalstr;
        /*
         *      We use sp->s_index to hold the length of the
         *      name; it isn't used for anything else
        /*
         *      We use sp->s_index to hold the length of the
         *      name; it isn't used for anything else
@@ -650,25 +662,29 @@ int symwrite(symfile)
        register        struct  allocbox        *allocwalk;
 
        symsout = 0;
        register        struct  allocbox        *allocwalk;
 
        symsout = 0;
-       DECLITERATE(allocwalk, sp, ub)
-       {
+       totalstr = sizeof(totalstr);
+       DECLITERATE(allocwalk, sp, ub) {
                if (sp->s_tag >= IGNOREBOUND) 
                        continue;
                if (sp->s_tag >= IGNOREBOUND) 
                        continue;
-               if ((FETCHNAME(sp)[0] == 'L') && (sp->s_tag == LABELID) && !savelabels)
+               if (ISLABEL(sp))
                        continue;
                symsout++;
                        continue;
                symsout++;
-
                name = sp->s_name;              /* save pointer */
                /*
                 *      the length of the symbol table string
                name = sp->s_name;              /* save pointer */
                /*
                 *      the length of the symbol table string
-                *      always includes the trailing null
+                *      always includes the trailing null;
+                *      blast the pointer to its a.out value.
                 */
                if (sp->s_name && (sp->s_index = STRLEN(sp))){
                 */
                if (sp->s_name && (sp->s_index = STRLEN(sp))){
-                       sp->s_nmx = STROFF(sp); /* clobber */
+                       sp->s_nmx = totalstr;
+                       totalstr += sp->s_index;
                } else {
                        sp->s_nmx = 0;
                }
                } else {
                        sp->s_nmx = 0;
                }
-               sp->s_type = (sp->s_ptype != 0) ? sp->s_ptype : (sp->s_type & (~XFORW));
+               if (sp->s_ptype != 0)
+                       sp->s_type = sp->s_ptype;
+               else
+                       sp->s_type = (sp->s_type & (~XFORW));
                if (readonlydata && (sp->s_type&~N_EXT) == N_DATA)
                        sp->s_type = N_TEXT | (sp->s_type & N_EXT);
                bwrite((char *)&sp->s_nm, sizeof (struct nlist), symfile);
                if (readonlydata && (sp->s_type&~N_EXT) == N_DATA)
                        sp->s_type = N_TEXT | (sp->s_type & N_EXT);
                bwrite((char *)&sp->s_nm, sizeof (struct nlist), symfile);
@@ -678,17 +694,36 @@ int symwrite(symfile)
                yyerror("INTERNAL ERROR: Wrote %d symbols, wanted to write %d symbols\n",
                        symsout, symsdesired);
        /*
                yyerror("INTERNAL ERROR: Wrote %d symbols, wanted to write %d symbols\n",
                        symsout, symsdesired);
        /*
-        *      Copy the string temporary file to the symbol file,
-        *      copying all the strings and symbols we ever saw,
-        *      including labels, stabs strings, ascii strings, etc.
-        *      This is slightly wasteful.
+        *      Construct the string pool from the symbols that were written,
+        *      possibly fetching from the string file if the string
+        *      is not core resident.
         */
         */
-       i = 0;
-       while((nread = read(strfile->_file, rbuf, sizeof(rbuf))) > 0){
-               if (i == 0){
-                       ((int *)rbuf)[0] = strfilepos;
+       bwrite(&totalstr, sizeof(totalstr), symfile);
+       symsout = 0;
+       DECLITERATE(allocwalk, sp, ub) {
+               if (sp->s_tag >= IGNOREBOUND) 
+                       continue;
+               if (ISLABEL(sp))
+                       continue;
+               symsout++;
+               if (STRLEN(sp) > 0){
+                if (STRPLACE(sp) & STR_CORE){
+                       bwrite(FETCHNAME(sp), STRLEN(sp), symfile);
+                } else if (STRPLACE(sp) & STR_FILE){
+                       char    rbuf[2048];
+                       int     left, nread;
+                       fseek(strfile, STROFF(sp), 0);
+                       for (left = STRLEN(sp); left > 0; left -= nread){
+                               nread = fread(rbuf, sizeof(char),
+                                       min(sizeof(rbuf), left), strfile);
+                               if (nread == 0)
+                                       break;
+                               bwrite(rbuf, nread, symfile);
+                       }
+                }
                }
                }
-               bwrite(rbuf, nread, symfile);
-               i++;
        }
        }
+       if (symsout != symsdesired)
+               yyerror("INTERNAL ERROR: Wrote %d strings, wanted %d\n",
+                       symsout, symsdesired);
 }
 }