Annotation of researchv9/jerq/sgs/strip/buildfcns.c, revision 1.1

1.1     ! root        1: /* UNIX HEADER */
        !             2: #include       <stdio.h>
        !             3: 
        !             4: /* COMMON SGS HEADERS */
        !             5: #include       "filehdr.h"
        !             6: #include       "scnhdr.h"
        !             7: #include       "reloc.h"
        !             8: #include       "syms.h"
        !             9: #include       "ldfcn.h"
        !            10: #include       "linenum.h"
        !            11: 
        !            12: /* STRIP HEADER */
        !            13: #include       "defs.h"
        !            14: 
        !            15: /* SGS SPECIFIC HEADER */
        !            16: #include       "sgs.h"
        !            17: 
        !            18: #if !UNIX
        !            19: long   rel_skipped;
        !            20: long   reloc_ptrs[MAXSECTIONS];
        !            21: #endif 
        !            22: 
        !            23: #if !AR16WR
        !            24: static TEMPENT *basep = NULL;
        !            25: static TEMPENT *oldptr = NULL;
        !            26: static TEMPENT *entptr = NULL;
        !            27: #endif
        !            28: 
        !            29: long   start_reloc;
        !            30: int    havelnums = 0;
        !            31: #if FLEXNAMES
        !            32: long   string_size = 0L;
        !            33: #endif
        !            34: 
        !            35:     /*  buildfcns.c is a collection of functions that copy parts of an object
        !            36:      *  file to strip's temporary file
        !            37:      *
        !            38:      *  copyopthdr( )
        !            39:      *
        !            40:      *  copies the optional header to the temporary file
        !            41:      *
        !            42:      *  calls:
        !            43:      *      - docopy((long) HEADER(ldptr).f_opthdr) to do the copying
        !            44:      *        after positioning ldptr at the start of the optional header
        !            45:      *
        !            46:      *  returns SUCCESS or FAILURE depending on the success of the copying
        !            47:      */
        !            48: 
        !            49: int
        !            50: cpyopthdr( )
        !            51: 
        !            52: {
        !            53:     /* OBJECT FILE ACCESS ROUTINE CALLED */
        !            54:     extern int         ldohseek( );
        !            55: 
        !            56:     /* STRIP FUNCTION CALLED */
        !            57:     extern int         docopy( );
        !            58: 
        !            59:     /* EXTERNAL VARIABLE USED */
        !            60:     extern LDFILE      *ldptr;
        !            61: 
        !            62:     if (ldohseek(ldptr) == SUCCESS) {
        !            63:        if (docopy((long) HEADER(ldptr).f_opthdr) == SUCCESS) {
        !            64:            return(SUCCESS);
        !            65:        }
        !            66:     }
        !            67: 
        !            68:     return(FAILURE);
        !            69: }
        !            70: 
        !            71: 
        !            72: 
        !            73: 
        !            74:     /*  cpyscnhdrs( )
        !            75:      *
        !            76:      *  copies each section header to the temporary file
        !            77:      *  sets number of line numbers and line number pointer to zero
        !            78:      *
        !            79:      *  returns SUCCESS or FAILURE depending on the success of the copy
        !            80:      */
        !            81: 
        !            82: 
        !            83: int
        !            84: cpyscnhdrs( )
        !            85: 
        !            86: {
        !            87:     /* UNIX FUNCTIONS CALLED */
        !            88:     extern int         fwrite( );
        !            89: 
        !            90:     /* COMMON ACCESS ROUTINE CALLED */
        !            91:     extern int         ldshread( );
        !            92: 
        !            93:     /* EXTERNAL VARIABLES USED */
        !            94:     extern LDFILE      *ldptr;
        !            95:     extern FILE                *strp1;
        !            96:      extern    int     sflag;
        !            97:      extern    int     havelnums;
        !            98:        extern  long    start_reloc;
        !            99:        extern  int     fflag;
        !           100:        extern  int     rflag;
        !           101:        extern  int     lflag;
        !           102:        extern  int     xflag;
        !           103: #if !UNIX
        !           104:        extern  long    rel_skipped;
        !           105:        extern  long    reloc_ptrs[];
        !           106: #endif
        !           107: 
        !           108:     /* LOCAL VARIABLES */
        !           109:     SCNHDR             secthead;
        !           110:     unsigned short     section;
        !           111: 
        !           112: #if !UNIX
        !           113:        if (fflag) {
        !           114:                rel_skipped = 0L;
        !           115:                for (section = 1; section <= HEADER(ldptr).f_nscns; ++section) {
        !           116:                        if (ldshread( ldptr, section, &secthead ) != SUCCESS)
        !           117:                                return( FAILURE );
        !           118:                        if (secthead.s_flags & STYP_COPY)
        !           119:                                reloc_ptrs[section - 1] = secthead.s_relptr - rel_skipped;
        !           120:                        else {
        !           121:                                reloc_ptrs[section - 1] = 0L;
        !           122:                                rel_skipped += (long) secthead.s_nreloc * RELSZ;
        !           123:                        }
        !           124:                }
        !           125:        }
        !           126: #endif
        !           127: 
        !           128:        if (fflag || sflag || rflag || xflag || lflag)
        !           129:                start_reloc = HEADER(ldptr).f_symptr;
        !           130:        else
        !           131:                start_reloc = 0L;
        !           132: 
        !           133:        for (section = 1; section <= HEADER(ldptr).f_nscns; ++section) {
        !           134:                if (ldshread(ldptr, section, &secthead) != SUCCESS) {
        !           135:                        return(FAILURE);
        !           136:                }
        !           137: 
        !           138:                /*
        !           139:                 * Pick up the earliest section's relocation or line numbers,
        !           140:                 * so that a gap between the last section and the next
        !           141:                 * information will be handled correctly, since the next strip
        !           142:                 * routine will assume a properly seeked pointer to the 
        !           143:                 * temp file "strp1"
        !           144:                 */
        !           145:                if ((secthead.s_relptr != 0L) && (secthead.s_relptr < start_reloc))
        !           146:                        start_reloc = secthead.s_relptr;
        !           147:                if ((secthead.s_lnnoptr != 0L) && (secthead.s_lnnoptr < start_reloc))
        !           148:                        start_reloc = secthead.s_lnnoptr;
        !           149: 
        !           150: #if !UNIX
        !           151:                if (fflag) {
        !           152:                        secthead.s_relptr = reloc_ptrs[section - 1];
        !           153:                        if (secthead.s_relptr == 0L)
        !           154:                                secthead.s_nreloc = 0;
        !           155:                }
        !           156: #endif
        !           157:                if (!sflag) {
        !           158:                        secthead.s_lnnoptr = 0L;
        !           159:                        secthead.s_nlnno = 0;
        !           160:                } else {
        !           161:                        if ( secthead.s_nlnno != 0 ) {
        !           162:                                havelnums = 1;
        !           163: #if !UNIX
        !           164:                                if (fflag)
        !           165:                                        secthead.s_lnnoptr -= rel_skipped;
        !           166: #endif
        !           167:                        }
        !           168:                }
        !           169: 
        !           170:                if (fwrite(&secthead, SCNHSZ, 1, strp1) != 1) {
        !           171:                return(FAILURE);
        !           172:                }
        !           173:        } /* for loop */
        !           174: 
        !           175:        return(SUCCESS);
        !           176: }
        !           177: 
        !           178: 
        !           179: 
        !           180: 
        !           181:     /*  copyscns( )
        !           182:      *
        !           183:      *  copies the section contents to the temporary file
        !           184:      *  keeps track of the file address that might be the start of the symbol
        !           185:      *  table in a partially stripped file
        !           186:      *
        !           187:      *  calls:
        !           188:      *      - docopy(secthead.s_size) to do the copying after positioning 
        !           189:      *        ldptr at the start of a section's contents and after seeking
        !           190:      *        to the section content file address in strp1 (to account for
        !           191:      *        section content alignment in the object file)
        !           192:      *
        !           193:      *  returns the address to be used for the start of the symbol table
        !           194:      *  in a partially stripped file -- if everything goes okay
        !           195:      *  returns ERROR if anything went wrong
        !           196:      */
        !           197: 
        !           198: 
        !           199: long
        !           200: copyscns( )
        !           201: 
        !           202: {
        !           203:     /* UNIX FUNCTION CALLED */
        !           204:     extern int         fseek( );
        !           205:     extern int         fwrite( );
        !           206: 
        !           207:     /* OBJECT FILE ACCESS ROUTINES CALLED */
        !           208:     extern int         ldshread( ),
        !           209:                        ldsseek( );
        !           210: 
        !           211:     /* STRIP FUNCTION CALLED */
        !           212:     extern int         docopy( );
        !           213: 
        !           214:     /* EXTERNAL VARIABLES USED */
        !           215:     extern LDFILE      *ldptr;
        !           216:     extern FILE                *strp1;
        !           217:     extern int         sflag;
        !           218:     extern long                start_reloc;
        !           219: 
        !           220:     /* LOCAL VARIABLES */
        !           221:     SCNHDR             secthead;
        !           222:     unsigned short     section;
        !           223:     long               fileaddr;
        !           224:     long               gap;
        !           225:     char               buf[BUFSIZ];
        !           226:     register char      *p;
        !           227: 
        !           228:     p = buf;
        !           229:     while (p < (buf + BUFSIZ))
        !           230:        *p++ = '\0';
        !           231: 
        !           232:     fileaddr = (long) FILHSZ + HEADER(ldptr).f_opthdr +
        !           233:               SCNHSZ * HEADER(ldptr).f_nscns;
        !           234: 
        !           235:     for (section = 1; section <= HEADER(ldptr).f_nscns; ++section) {
        !           236:        if (ldshread(ldptr, section, &secthead) != SUCCESS) {
        !           237:            return(ERROR);
        !           238:        }
        !           239: 
        !           240:        if (secthead.s_scnptr != 0L) {
        !           241:            if (ldsseek(ldptr, section) != SUCCESS) {
        !           242:                return(ERROR);
        !           243:            }
        !           244: 
        !           245:            /* If loader directive BLOCK is used, have to seek to section
        !           246:             * address in strp1 to ensure that the section contents appear
        !           247:             * in the right place.  Note that the fileheader is not written
        !           248:             * to the temporary file so its size must be subtracted from the
        !           249:             * seek address. Under UNIX/RT, zeros must be written in the gap.
        !           250:             */
        !           251: 
        !           252: #if RT
        !           253:            while (fileaddr < secthead.s_scnptr) {
        !           254:                gap = secthead.s_scnptr - fileaddr;
        !           255:                if (gap > BUFSIZ)
        !           256:                    gap = BUFSIZ;
        !           257:                if (fwrite( buf, (int) gap, 1, strp1) != 1)
        !           258:                    return( ERROR );
        !           259:                fileaddr += gap;
        !           260:            }
        !           261: #else
        !           262:           if (fseek(strp1,secthead.s_scnptr - FILHSZ, BEGINNING) !=
        !           263:                OKFSEEK) {
        !           264:                return (ERROR);
        !           265:           }
        !           266: #endif
        !           267: 
        !           268: 
        !           269:            if (docopy(secthead.s_size) != SUCCESS) {
        !           270:                return(ERROR);
        !           271:            }
        !           272:            fileaddr = secthead.s_scnptr + secthead.s_size;
        !           273:        }
        !           274:     }
        !           275: 
        !           276:        /*
        !           277:         * If there is a hole in the object file between the last byte of
        !           278:         * sections contents and whatever comes next, adjust "fileaddr"
        !           279:         * and the strp1 pointer so that the next strip routine called
        !           280:         * after this one will start at the right place in the
        !           281:         * temporary file
        !           282:         * This is also needed for .m files
        !           283:         */
        !           284:        if ((start_reloc != 0L) && (fileaddr < start_reloc))
        !           285:                while (fileaddr < start_reloc) {
        !           286:                        gap = start_reloc - fileaddr;
        !           287:                        if (gap > BUFSIZ)
        !           288:                                gap = BUFSIZ;
        !           289:                        if (fwrite( buf, (int) gap, 1, strp1 ) != 1)
        !           290:                                return( ERROR );
        !           291:                        fileaddr += gap;
        !           292:                }
        !           293:     return(fileaddr);
        !           294: }
        !           295: 
        !           296: 
        !           297: 
        !           298:     /*  cpyreloc(fileaddr)
        !           299:      *
        !           300:      *  cpyreloc is called only if lflag is ON
        !           301:      *  or if the rflag is ON and local symbols have already been stripped
        !           302:      *  It copies the relocation information to the temporary file
        !           303:      *  and increments fileaddr, the address of the symbol table in a partially
        !           304:      *  stripped file.
        !           305:      *
        !           306:      *  calls:
        !           307:      *      - docopy((long) secthead.s_nreloc * RELSZ) to do the copying
        !           308:      *       after ldptr has been positioned at the start of the reloc info
        !           309:      *
        !           310:      *  returns the address to be used for the symbol table in a partially
        !           311:      *  stripped if copy is successful
        !           312:      *  returns ERROR otherwise.
        !           313:      */
        !           314: 
        !           315: 
        !           316: long
        !           317: cpyreloc(fileaddr)
        !           318: 
        !           319: long   fileaddr;
        !           320: 
        !           321: {
        !           322:     /* COMMON OBJECT FILE ACCESS ROUTINES CALLED */
        !           323:     extern int         ldshread( ),
        !           324:                        ldrseek( );
        !           325: 
        !           326:     /* STRIP FUNCTION CALLED */
        !           327:     extern int         docopy( );
        !           328: 
        !           329:     /* EXTERNAL VARIABLES USED */
        !           330:     extern LDFILE      *ldptr;
        !           331: #if !UNIX
        !           332:        extern  int     fflag;
        !           333: #endif
        !           334: 
        !           335:     /* LOCAL VARIABLES */
        !           336:     SCNHDR             secthead;
        !           337:     unsigned short     section;
        !           338: 
        !           339:     for (section = 1; section <= HEADER(ldptr).f_nscns; ++section) {
        !           340:        if (ldshread(ldptr, section, &secthead) != SUCCESS) {
        !           341:            return(ERROR);
        !           342:        }
        !           343: 
        !           344:        if (secthead.s_nreloc != 0) {
        !           345: #if !UNIX
        !           346:            if (fflag && !(secthead.s_flags & STYP_COPY))
        !           347:                continue;
        !           348: #endif
        !           349:            if (ldrseek(ldptr, section) != SUCCESS) {
        !           350:                return(ERROR);
        !           351:            }
        !           352: 
        !           353:            if (docopy((long) secthead.s_nreloc * RELSZ) != SUCCESS) {
        !           354:                return(ERROR);
        !           355:            }
        !           356: 
        !           357:            fileaddr += (long) secthead.s_nreloc * RELSZ;
        !           358:        }
        !           359:     }
        !           360: 
        !           361:     return(fileaddr);
        !           362: }
        !           363:        /*      cpylnums( )
        !           364:         *
        !           365:         * cpylnums is called only if sflag is ON
        !           366:         * it copies the line number information to the temporary file
        !           367:         * and resets the symbol table index to the correct index of the
        !           368:         * partially stripped symbol table.
        !           369:         *
        !           370:         * fileaddr will be incremented to point to the spot where the
        !           371:         * new symbol table will be. cpylnums returns this value or ERROR
        !           372:         */
        !           373: 
        !           374: long
        !           375: cpylnums(fileaddr)
        !           376: long   fileaddr;
        !           377: {
        !           378:        /* UNIX FUNCTIONS CALLED */
        !           379:        extern  int     fread( );
        !           380:        extern  int     fwrite( );
        !           381: 
        !           382:        /* COMMON OBJECT FILE ACCESS ROUTINES CALLED */
        !           383:        extern int      ldlseek();
        !           384:        extern int      ldshread();
        !           385: 
        !           386:        /* EXTERNAL VARIABLES USED */
        !           387:        extern  LDFILE  *ldptr;
        !           388:        extern  FILE    *strp1;
        !           389: 
        !           390:        /* STRIP FUNCTIONS CALLED */
        !           391:        extern  long    newndx( );
        !           392: 
        !           393:        /* LOCAL VARIABLES */
        !           394:        long            lindex;
        !           395:        unsigned short  section;
        !           396:        SCNHDR          secthead;
        !           397:        LINENO          lnum;
        !           398:        long            numlnums;
        !           399: 
        !           400:        for (section = 1; section <= HEADER(ldptr).f_nscns; ++section) {
        !           401:                if (ldshread( ldptr, section, &secthead) != SUCCESS) {
        !           402:                        fprintf( stderr, "cannot read section header for section %s\n", section );
        !           403:                        return(ERROR);
        !           404:                }
        !           405: 
        !           406:                if (secthead.s_nlnno != 0) {
        !           407:                        if (ldlseek(ldptr, section) != SUCCESS) {
        !           408:                                fprintf( stderr, "could not seek to line numbers\n" );
        !           409:                                return(ERROR);
        !           410:                        }
        !           411: 
        !           412:                        for (numlnums = 0L; numlnums < secthead.s_nlnno; ++numlnums) {
        !           413:                                if (FREAD( &lnum, LINESZ, 1, ldptr ) != 1) {
        !           414:                                        fprintf( stderr, "could not read line numbers\n" );
        !           415:                                        return( ERROR );
        !           416:                                }
        !           417:                                if (lnum.l_lnno == 0) {
        !           418:                                        lindex = lnum.l_addr.l_symndx;
        !           419:                                        if ((lindex = newndx(lindex)) == ERROR){
        !           420:                                                fprintf( stderr, "could not locate new symbol table index\n" );
        !           421:                                                return( ERROR );
        !           422:                                        }
        !           423:                                        lnum.l_addr.l_symndx = lindex;
        !           424:                                }
        !           425:                                if (fwrite( &lnum, LINESZ, 1, strp1) != 1) {
        !           426:                                        fprintf( stderr, "could not write line numbers\n" );
        !           427:                                        return( ERROR );
        !           428:                                }
        !           429:                        }
        !           430: 
        !           431:                        fileaddr += (long) secthead.s_nlnno * LINESZ;
        !           432:                }
        !           433:        }
        !           434: 
        !           435:        return(fileaddr);
        !           436: }
        !           437: 
        !           438: 
        !           439: 
        !           440:     /*  copysyms( )
        !           441:      *
        !           442:      *  copies the entire symbol table to the temporary file (if lflag is ON
        !           443:      *  or if the rflag is on and local symbols have already been stripped)
        !           444:      *  sets the line number pointer in function auxiliary entries to zero
        !           445:      *
        !           446:      *  returns SUCCESS or FAILURE
        !           447:      */
        !           448: 
        !           449: 
        !           450: int
        !           451: copysyms( )
        !           452: 
        !           453: {
        !           454:        /* UNIX FUNCTIONS CALLED */
        !           455:        extern  int     fread( ),
        !           456:                        fwrite( );
        !           457: 
        !           458:        /* COMMON OBJECT FILE ACCESS ROUTINES CALLED */
        !           459:        extern  int     ldtbseek( );
        !           460: #if FLEXNAMES
        !           461:        extern  int     ldahread( );
        !           462: #endif
        !           463: 
        !           464:        /* EXTERNAL VARIABLES USED */
        !           465:        extern  LDFILE  *ldptr;
        !           466:        extern  FILE    *strp1;
        !           467: #if !UNIX
        !           468:        extern  int     fflag;
        !           469:        extern  long    reloc_ptrs[];
        !           470: #endif
        !           471: 
        !           472:        /* LOCAL VARIABLES */
        !           473:        SYMENT          symbol;
        !           474:        long            symindx;
        !           475:        AUXENT          aux;
        !           476: #if FLEXNAMES
        !           477:        ARCHDR          arhead;
        !           478:        long            str_size;
        !           479:        long            size;
        !           480:        long            str_table;
        !           481: #endif
        !           482: 
        !           483:        if (ldtbseek(ldptr) != SUCCESS)
        !           484:                return(FAILURE);
        !           485: 
        !           486:        for (symindx = 0L; symindx < HEADER(ldptr).f_nsyms; ++symindx)
        !           487:        {
        !           488:                if (FREAD(&symbol, SYMESZ, 1, ldptr) != 1)
        !           489:                        return(FAILURE);
        !           490: 
        !           491:                if (fwrite(&symbol, SYMESZ, 1, strp1) != 1)
        !           492:                        return(FAILURE);
        !           493: 
        !           494:                if (symbol.n_numaux == 1)
        !           495:                {
        !           496:                        if (FREAD(&aux, AUXESZ, 1, ldptr) != 1)
        !           497:                                return(FAILURE);
        !           498:                        ++symindx;
        !           499: 
        !           500:                        if (ISFCN(symbol.n_type))
        !           501:                        {
        !           502:                                aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0L;
        !           503:                        }
        !           504: 
        !           505:                        if ((symbol.n_sclass == C_STAT) 
        !           506:                            && (symbol.n_zeroes != 0L)
        !           507:                            && (strncmp( _TEXT, symbol.n_name, 8 ) == 0))
        !           508:                        {
        !           509:                                aux.x_scn.x_nlinno = 0;
        !           510: #if !UNIX
        !           511:                                if (fflag && (reloc_ptrs[symbol.n_scnum - 1] == 0L))
        !           512:                                        aux.x_scn.x_nreloc = 0L;
        !           513: #endif
        !           514:                        }
        !           515: 
        !           516:                        if ((symbol.n_sclass == C_FCN) || 
        !           517:                            (symbol.n_sclass == C_BLOCK) ||
        !           518:                            (ISARY(symbol.n_type)))
        !           519:                                aux.x_sym.x_misc.x_lnsz.x_lnno = 0;
        !           520: 
        !           521:                        if (fwrite(&aux, AUXESZ, 1, strp1) != 1)
        !           522:                        {
        !           523:                                return(FAILURE);
        !           524:                        }
        !           525:                }
        !           526:        }
        !           527: 
        !           528: #if FLEXNAMES
        !           529:        /* copy the string table, if there is one */
        !           530:        str_table = STROFFSET(ldptr);
        !           531:        if (TYPE(ldptr) == USH_ARTYPE)
        !           532:        {
        !           533:                if (ldahread( ldptr, &arhead ) != SUCCESS)
        !           534:                {
        !           535:                        fprintf( stderr, "could not read archive header" );
        !           536:                        return( FAILURE );
        !           537:                }
        !           538: 
        !           539:                if (arhead.ar_size <= (str_table + 1))
        !           540:                        return( SUCCESS );
        !           541: 
        !           542:                if ((FSEEK( ldptr, str_table, BEGINNING ) != OKFSEEK)
        !           543:                    || (FREAD( &str_size, sizeof( long ), 1, ldptr ) != 1 ))
        !           544:                {
        !           545:                        fprintf( stderr, "could not read string table\n" );
        !           546:                        return( FAILURE );
        !           547:                }
        !           548:        }
        !           549:        else if ((FSEEK( ldptr, str_table, BEGINNING ) != OKFSEEK)
        !           550:                    || (FREAD( &str_size, sizeof( long ), 1, ldptr ) != 1 ))
        !           551:                return( SUCCESS );
        !           552: 
        !           553:        /* make it even so the archive won't get screwed up */
        !           554:        size =  str_size + (str_size & 01); 
        !           555:        if ((fwrite( &size, sizeof( long ), 1, strp1 ) != 1)
        !           556:                        || (docopy( str_size - 4 ) != SUCCESS ))
        !           557:                return( FAILURE );
        !           558: 
        !           559:        if (size != str_size)
        !           560:                putc( '\0', strp1 );
        !           561: #endif
        !           562: 
        !           563:        return(SUCCESS);
        !           564: }
        !           565: 
        !           566: 
        !           567: 
        !           568: 
        !           569: /* EXTERNAL VARIABLE DEFINED */
        !           570: LDFILE *fwdptr;
        !           571: 
        !           572: 
        !           573:     /*  copyexts(filename)
        !           574:      *
        !           575:      *  copies external, static and extdef symbols to the temporary file (xflag)
        !           576:      *  sets the line number pointer in function aux entries to zero
        !           577:      *  sets the forward file index in function aux entries appropriatly for
        !           578:      *  the stripped symbol table
        !           579:      *  sets the index of a structure, union or enumeration tag entry to zero
        !           580:      *  for any structure, union or enumeration types left in the table
        !           581:      *
        !           582:      *  defines:
        !           583:      *      - fwdptr = ldaopen(filename, ldptr) to read entries between a
        !           584:      *                function and a .ef entry to determine the value of the
        !           585:      *                forward file index in the function aux entry
        !           586:      *
        !           587:      *  calls:
        !           588:      *      - findndx(aux.x_sym.x_fcnary.x_fcn.x_endndx, oldndx, count) to determine the
        !           589:      *        new value of aux.x_sym.x_fcnary.x_fcn.x_endndx
        !           590:      *
        !           591:      *  returns the number of symbols left in the symbol table after partial
        !           592:      *  stripping if successful
        !           593:      *  returns ERROR otherwise
        !           594:      *
        !           595:      *  Note that TUS people would like strip to keep .bf and .ef entries around
        !           596:      *  when locals are stripped from the symbol table but this is not
        !           597:      *  currently done.
        !           598:      */
        !           599: 
        !           600: 
        !           601: long
        !           602: copyexts(filename)
        !           603: 
        !           604: char   *filename;
        !           605: 
        !           606: {
        !           607:        /* UNIX FUNCTIONS CALLED */
        !           608:        extern  int     fread( ),
        !           609:                        fwrite( );
        !           610: 
        !           611:        /* COMMON OBJECT FILE ACCESS ROUTINES CALLED */
        !           612:        extern  LDFILE  *ldaopen( );
        !           613:        extern  int     ldaclose( ),
        !           614:                        ldtbseek( );
        !           615: #if FLEXNAMES
        !           616:        extern  char    *ldgetname( );
        !           617: #endif
        !           618: 
        !           619:        /* STRIP FUNCTION CALLED */
        !           620:        extern  long    findndx( );
        !           621: #if !AR16WR
        !           622:        extern  TEMPENT *getempent( );
        !           623: #endif
        !           624: 
        !           625:        /* EXTERNAL VARIABLES USED */
        !           626:        extern  LDFILE  *ldptr,
        !           627:                        *fwdptr;
        !           628:        extern  FILE    *strp2;
        !           629: #if AR16WR
        !           630:        extern  FILE    *readtmp,
        !           631:                        *tempfil;
        !           632: #else
        !           633:        extern  TEMPENT *basep;
        !           634: #endif
        !           635:        extern  int     rflag,
        !           636:                        havelnums,
        !           637:                        bflag,
        !           638:                        sflag;
        !           639: #if !UNIX
        !           640:        extern  int     fflag;
        !           641:        extern  long    rel_skipped;
        !           642:        extern  long    reloc_ptrs[];
        !           643: #endif
        !           644: #if FLEXNAMES
        !           645:        extern  long    string_file;
        !           646:        extern  char    tmpnam5[];
        !           647: #endif
        !           648: 
        !           649:        /* LOCAL VARIABLES */
        !           650:        SYMENT          symbol;
        !           651: #if AR16WR
        !           652:        TEMPENT         temp;
        !           653: #else
        !           654:        TEMPENT         *temp;
        !           655:        TEMPENT         *tail;
        !           656: #endif
        !           657:        long            oldndx;
        !           658:        long            count;
        !           659:        AUXENT          aux;
        !           660: #if FLEXNAMES
        !           661:        FILE            *string_file;
        !           662:        char            *sym_name;
        !           663:        int             name_length;
        !           664: #endif
        !           665: 
        !           666: 
        !           667:        if ((fwdptr = ldaopen(filename, ldptr)) == NULL) {
        !           668:                return(ERROR);
        !           669:        }
        !           670: 
        !           671:        if (ldtbseek(ldptr) != SUCCESS) {
        !           672:                return(ERROR);
        !           673:        }
        !           674: 
        !           675: #if FLEXNAMES
        !           676:        string_size = 0L;
        !           677:        if ((string_file = fopen( tmpnam5, "w" )) == NULL )
        !           678:                return( ERROR );
        !           679: #endif
        !           680: 
        !           681:        count = 0L;
        !           682: #if !AR16WR
        !           683:        tail = NULL;
        !           684: #endif
        !           685: 
        !           686:        for (oldndx = 0L; oldndx < HEADER(ldptr).f_nsyms; ++oldndx)
        !           687:        {
        !           688:                if (FREAD(&symbol, SYMESZ, 1, ldptr) != 1)
        !           689:                {
        !           690:                        return(ERROR);
        !           691:                }
        !           692: 
        !           693:                if ((symbol.n_sclass == C_EXT) || (symbol.n_sclass == C_STAT) ||
        !           694:                                (bflag && ((symbol.n_sclass == C_BLOCK)  ||
        !           695:                                (symbol.n_sclass == C_FCN)))  ||
        !           696:                                (symbol.n_sclass == C_HIDDEN) ||
        !           697:                                (symbol.n_sclass == C_EXTDEF) || 
        !           698:                                (symbol.n_sclass == C_FILE))
        !           699:                {
        !           700: #if AR16WR
        !           701:                        temp.new = count;
        !           702:                        temp.old = oldndx;
        !           703:                        if (fwrite(&temp, TEMPSZ, 1, tempfil) != 1)
        !           704:                        {
        !           705:                                fprintf(stderr,"couldn't fwrite to tempfil\n");
        !           706:                                return(ERROR);
        !           707:                        }
        !           708:                        fflush(tempfil);
        !           709: #else
        !           710:                        temp = getempent( );
        !           711:                        temp->new = count;
        !           712:                        temp->old = oldndx;
        !           713:                        temp->link = NULL;
        !           714:                        if (tail)
        !           715:                                tail->link = temp;
        !           716:                        else
        !           717:                                basep = temp;
        !           718:                        tail = temp;
        !           719: #endif
        !           720:                        ++count;
        !           721: 
        !           722: #if FLEXNAMES
        !           723:                        if (symbol.n_zeroes == 0L)
        !           724:                        {
        !           725:                                if ((sym_name = ldgetname( ldptr, &symbol )) == NULL)
        !           726:                                        return( ERROR );
        !           727:        
        !           728:                                name_length = strlen( sym_name ) + 1;
        !           729:                                if (fwrite( sym_name, name_length, 1, string_file ) != 1)
        !           730:                                        return( ERROR );
        !           731: 
        !           732:                                symbol.n_offset = string_size + 4;
        !           733:                                string_size += name_length;
        !           734:                        }
        !           735: #endif
        !           736:                        if(symbol.n_sclass == C_FILE && symbol.n_value != 0L)
        !           737:                        {
        !           738:                                if((symbol.n_value = findndx(symbol.n_value, oldndx, count+symbol.n_numaux)) == ERROR)
        !           739:                                return(ERROR);
        !           740:                        }
        !           741: 
        !           742:                        if (fwrite(&symbol, SYMESZ, 1, strp2) != 1)
        !           743:                        {
        !           744:                                return(ERROR);
        !           745:                        }
        !           746: 
        !           747:                        if (symbol.n_numaux == 1)
        !           748:                        {
        !           749:                                if (FREAD(&aux, AUXESZ, 1, ldptr) != 1)
        !           750:                                {
        !           751:                                        return(ERROR);
        !           752:                                }
        !           753:                                ++oldndx;
        !           754:                                ++count;
        !           755: 
        !           756:                                if (ISFCN(symbol.n_type))
        !           757:                                {
        !           758:                                        if ( !sflag) 
        !           759:                                                aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0L;
        !           760: #if !UNIX
        !           761:                                        else if ((havelnums == 1) && fflag && (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0))
        !           762:                                                aux.x_sym.x_fcnary.x_fcn.x_lnnoptr -= rel_skipped;
        !           763: #endif
        !           764:                                }
        !           765: 
        !           766:                                if (ISFCN(symbol.n_type) || (bflag &&
        !           767:                                    (strncmp(symbol.n_name,".bb",3)==0)))
        !           768:                                {
        !           769:                                        if ((aux.x_sym.x_fcnary.x_fcn.x_endndx =
        !           770:                                                findndx(aux.x_sym.x_fcnary.x_fcn.x_endndx, oldndx, count)) == ERROR)
        !           771:                                                        return(ERROR);
        !           772:                                }
        !           773: 
        !           774:                                if (!sflag && ISARY(symbol.n_type))
        !           775:                                        aux.x_sym.x_misc.x_lnsz.x_lnno = 0;
        !           776: 
        !           777:                                if ((symbol.n_sclass == C_STAT) 
        !           778:                                    && (symbol.n_zeroes != 0)
        !           779:                                    && (strncmp(_TEXT, symbol.n_name, 8 ) == 0))
        !           780:                                {
        !           781:                                        if (!sflag)
        !           782:                                                aux.x_scn.x_nlinno = 0;
        !           783: #if !UNIX
        !           784:                                        if (fflag && (reloc_ptrs[symbol.n_scnum - 1] == 0L))
        !           785:                                                aux.x_scn.x_nreloc = 0L;
        !           786: #endif
        !           787:                                }
        !           788: 
        !           789:                                if (((symbol.n_type & N_BTMASK) == T_STRUCT) ||
        !           790:                                    ((symbol.n_type & N_BTMASK) == T_UNION) ||
        !           791:                                    ((symbol.n_type & N_BTMASK) == T_ENUM))
        !           792:                                        aux.x_sym.x_tagndx = 0L;
        !           793: 
        !           794:                                if (fwrite(&aux, AUXESZ, 1, strp2) != 1)
        !           795:                                        return(ERROR);
        !           796: 
        !           797:                        }
        !           798: 
        !           799:                }
        !           800:                else   /* NOT EXT, STAT, EXTDEF, OR FILE        */
        !           801:                {
        !           802:                        if (symbol.n_numaux == 1)
        !           803:                        {
        !           804:                                if (FREAD(&aux, AUXESZ, 1, ldptr) != 1)
        !           805:                                        return(ERROR);
        !           806:                        ++oldndx;
        !           807:                        }
        !           808:                }
        !           809:        }
        !           810: 
        !           811: #if AR16WR
        !           812:        fflush( tempfil );
        !           813: #endif
        !           814: #if FLEXNAMES
        !           815:        if (string_size > 0L)
        !           816:        {
        !           817:                if (string_size & 01)
        !           818:                {
        !           819:                        string_size += 1;
        !           820:                        putc( '\0', string_file );
        !           821:                }
        !           822:                string_size += 4;
        !           823:        }
        !           824:        fclose( string_file );
        !           825: #endif
        !           826: 
        !           827:        ldaclose(fwdptr);
        !           828:        fflush(strp2);
        !           829:        return(count);
        !           830: }
        !           831: 
        !           832: 
        !           833: 
        !           834: 
        !           835:     /*  findndx(fwdndx, oldndx, count)
        !           836:      *
        !           837:      *  determines the new index of the symbol that used to be at fwdndx
        !           838:      *  (the entry after a .ef entry)
        !           839:      *  simply counts the number of external, static or extdef symbols that
        !           840:      *  appear between the current entry and the symbol at fwdndx
        !           841:      *
        !           842:      *  returns new index of symbol that used to be at fwdndx if successful
        !           843:      *  returns ERROR if the symbol table is messed up
        !           844:      */
        !           845: 
        !           846: 
        !           847: long
        !           848: findndx(fwdndx, oldndx, count)
        !           849: 
        !           850: register long  fwdndx,
        !           851:                oldndx,
        !           852:                count;
        !           853: 
        !           854: {
        !           855:     /* COMMON OBJECT FILE ACCESS ROUTINE CALLED */
        !           856:     extern int         ldtbread( );
        !           857: 
        !           858:     /* EXTERNAL VARIABLES USED */
        !           859:     extern LDFILE      *fwdptr;
        !           860:     extern int         bflag;
        !           861: 
        !           862:     /* LOCAL VARIABLE */
        !           863:     SYMENT             symbol;
        !           864: 
        !           865: 
        !           866:     /*  fwdndx is the old symbol table index of the entry following a .ef entry
        !           867:      *  oldndx is the old symbol table index of the last symbol read
        !           868:      *  count is the new index of the last symbol read
        !           869:      *  the idea is to increment count for each symbol (up to fwdndx) that
        !           870:      *  will be written to the stripped symbol table:  thus returning the
        !           871:      *  new symbol table index of the symbol at old fwdndx
        !           872:      */
        !           873:     while (++oldndx < fwdndx) {
        !           874:        if (ldtbread(fwdptr, oldndx, &symbol) != SUCCESS) {
        !           875:            return(ERROR);
        !           876:        }
        !           877: 
        !           878:        if ((symbol.n_sclass == C_EXT) || (symbol.n_sclass == C_STAT) ||
        !           879:            (symbol.n_sclass == C_HIDDEN) || (symbol.n_sclass == C_EXTDEF) || 
        !           880:            (symbol.n_sclass == C_FILE) || 
        !           881:            (bflag && ((symbol.n_sclass == C_BLOCK) || 
        !           882:            (symbol.n_sclass == C_FCN)))) {
        !           883:            ++count;
        !           884:            if (symbol.n_numaux == 1) {
        !           885:                ++count;
        !           886:                ++oldndx;
        !           887:            }
        !           888:        } else {
        !           889:            if (symbol.n_numaux == 1) {
        !           890:                ++oldndx;
        !           891:            }
        !           892:        }
        !           893:     }
        !           894: 
        !           895:     return(count);
        !           896: }
        !           897: 
        !           898: 
        !           899: 
        !           900: 
        !           901:     /*  docopy(size)
        !           902:      *
        !           903:      *  copies size bytes from the original object file (positioned by calling
        !           904:      *  routine) to the temporary file (positioned by calling routine or by
        !           905:      *  default)
        !           906:      *
        !           907:      *  docopy simply reads into and out of buffer until copying is done
        !           908:      *
        !           909:      *  returns SUCCESS or FAILURE
        !           910:      */
        !           911: 
        !           912: 
        !           913: int
        !           914: docopy(size)
        !           915: 
        !           916: long   size;
        !           917: 
        !           918: {
        !           919:     /* UNIX FUNCTIONS CALLED */
        !           920:     extern int         fread( ),
        !           921:                        fwrite( );
        !           922: 
        !           923:     /* EXTERNAL VARIABLES USED */
        !           924:     extern LDFILE      *ldptr;
        !           925:     extern FILE                *strp1;
        !           926: 
        !           927:     /* LOCAL VARIABLES */
        !           928:     short              buffer[BSIZE];
        !           929:     long               index;
        !           930:     short              remaindr;
        !           931: 
        !           932: 
        !           933:     for (index = size/BSIZE; index > 0; --index) {
        !           934:        if (FREAD(buffer, BSIZE, 1, ldptr) != 1) {
        !           935:            return(FAILURE);
        !           936:        }
        !           937: 
        !           938:        if (fwrite(buffer, BSIZE, 1, strp1) != 1) {
        !           939:            return(FAILURE);
        !           940:        }
        !           941:     }
        !           942: 
        !           943:     if ((remaindr = size % BSIZE) != 0) {
        !           944:        if (FREAD(buffer, remaindr, 1, ldptr) != 1) {
        !           945:            return(FAILURE);
        !           946:        }
        !           947: 
        !           948:        if (fwrite(buffer, remaindr, 1, strp1) != 1) {
        !           949:            return(FAILURE);
        !           950:        }
        !           951:     }
        !           952: 
        !           953:     fflush(strp1);
        !           954:     return(SUCCESS);
        !           955: }
        !           956: 
        !           957: 
        !           958:        /*      newreloc(fileaddr)
        !           959:         *
        !           960:         *
        !           961:         * newreloc will copy each relocation entry to the temporary
        !           962:         * file and reset the symbol table index to the correct
        !           963:         * index of the partially stripped symbol table.
        !           964:         * fileaddr will be incremented to point to the spot where the
        !           965:         * new symbol table will be. returns this value.
        !           966:         *
        !           967:         *
        !           968:         */
        !           969: 
        !           970: 
        !           971: long
        !           972: newreloc(fileaddr)
        !           973: 
        !           974: long   fileaddr;
        !           975: 
        !           976: {
        !           977: 
        !           978:        /* UNIX FUNCTIONS CALLED */
        !           979:        extern  int             fread( ),
        !           980:                                fwrite( );
        !           981:        /* COMMON OBJECT FILE ACCESS ROUTINES CALLED */
        !           982:        extern int      ldshread( ),
        !           983:                        ldrseek( );
        !           984: 
        !           985:        /* STRIP FUNCTIONS CALLED */
        !           986:        extern  long    newndx( );
        !           987: 
        !           988:        /* EXTERNAL VARIABLES USED */
        !           989:        extern LDFILE   *ldptr;
        !           990:        extern  FILE    *strp1;
        !           991: #if !UNIX
        !           992:        extern  int     fflag;
        !           993: #endif
        !           994: 
        !           995:        /* LOCAL VARIABLES */
        !           996:        SCNHDR          secthead;
        !           997:        unsigned short  sect;
        !           998:        long            numreloc;
        !           999:        RELOC           rloc;
        !          1000: 
        !          1001:        for (sect = 1; sect <= HEADER(ldptr).f_nscns; ++sect) {
        !          1002:                if (ldshread(ldptr, sect, &secthead) != SUCCESS) {
        !          1003:                        fprintf(stderr,"Could not read reloc info for sect. %d\n",sect);
        !          1004:                        return(ERROR);
        !          1005:                }
        !          1006: 
        !          1007:                if (secthead.s_nreloc != 0) {
        !          1008: #if !UNIX
        !          1009:                        if (fflag && !(secthead.s_flags & STYP_COPY))
        !          1010:                                continue;
        !          1011: #endif
        !          1012:                        if (ldrseek(ldptr,sect) != SUCCESS) {
        !          1013:                                fprintf(stderr,"Could not seek to reloc entries in sect: %d\n",sect);
        !          1014:                                return(ERROR);
        !          1015:                        }
        !          1016: 
        !          1017:                        for(numreloc = 0L; numreloc < secthead.s_nreloc; ++numreloc) {
        !          1018:                                if(FREAD(&rloc, RELSZ, 1,ldptr) != 1) {
        !          1019:                                        fprintf( stderr, "could not read relocation\n" );
        !          1020:                                        return(ERROR);
        !          1021:                                }
        !          1022: 
        !          1023:                                if((rloc.r_symndx = newndx( rloc.r_symndx )) == ERROR)
        !          1024:                                        return(ERROR);
        !          1025: 
        !          1026:                                if (fwrite(&rloc, RELSZ, 1, strp1) != 1) {
        !          1027:                                        fprintf(stderr,"couldn't fwrite\n");
        !          1028:                                        return(ERROR);
        !          1029:                                }
        !          1030:                        } /* inner for loop */
        !          1031: 
        !          1032:                fileaddr += (long) secthead.s_nreloc * RELSZ;
        !          1033:                }
        !          1034:        } /* outer for loop */
        !          1035:        fflush(strp1);
        !          1036:        return(fileaddr);
        !          1037: }
        !          1038: 
        !          1039: 
        !          1040: 
        !          1041:        /* newndx(index)
        !          1042:         *
        !          1043:         *
        !          1044:         * newndx accepts as input, a symbol index, gotten from the old
        !          1045:         * symbol table in <fileindex>. This symbol name is then compared
        !          1046:         * to the symbol indexs from a partially stripped symbol table.
        !          1047:         * When a match is found, the index of the symbol is returned, else
        !          1048:         * ERROR
        !          1049:         *
        !          1050:         */
        !          1051: 
        !          1052: 
        !          1053: long
        !          1054: newndx(rind)
        !          1055: 
        !          1056: register long  rind;
        !          1057: 
        !          1058: {
        !          1059: 
        !          1060:        /* UNIX FUNCTIONS CALLED */
        !          1061:        extern  int             fseek( );
        !          1062:        extern  int             fread( );
        !          1063: 
        !          1064:        /* EXTERNAL VARIABLES USED */
        !          1065: #if AR16WR
        !          1066:        extern  FILE            *readtmp;
        !          1067:        extern  FILE            *tempfil;
        !          1068: #else
        !          1069:        extern  TEMPENT         *basep;
        !          1070: #endif
        !          1071:        extern  FILHDR          filehead;
        !          1072: 
        !          1073:        /* LOCAL VARIABLES */
        !          1074: #if AR16WR
        !          1075:        TEMPENT                 tmp;
        !          1076:        register long           count;
        !          1077: #else
        !          1078:        register TEMPENT        *temp;
        !          1079: #endif
        !          1080: 
        !          1081:        if (rind == -1L)
        !          1082:                return(-1L);
        !          1083: 
        !          1084: #if AR16WR
        !          1085:        count = 0L;
        !          1086:        if(fseek(readtmp,0L,0) != 0) {
        !          1087:                fprintf(stderr,"Could not seek to beginning of file.\n");
        !          1088:                return(ERROR);
        !          1089:        }
        !          1090:        for(count=0; count < filehead.f_nsyms; ++count) {
        !          1091:                if(fread(&tmp, TEMPSZ, 1, readtmp) != 1) {
        !          1092:                        fprintf(stderr,"Could not read index extry. Index: %ld\n",rind);
        !          1093:                        return(ERROR);
        !          1094:                }
        !          1095:                if( rind == tmp.old ) {
        !          1096:                        return((long) tmp.new);
        !          1097:                }
        !          1098:        }
        !          1099: #else
        !          1100:        for ( temp = basep; temp; temp = temp->link ) {
        !          1101:                if (temp->old == rind)
        !          1102:                        return( (long) temp->new );
        !          1103:        }
        !          1104: #endif
        !          1105: 
        !          1106:        fprintf(stderr,"No index found for relocation entry: %lo\n",rind);
        !          1107:        return(ERROR);
        !          1108: }
        !          1109: 
        !          1110: #if !AR16WR
        !          1111: /*
        !          1112:  * getempent will return an item for a TEMPENT linked list. If necessary,
        !          1113:  * malloc() will be called to get space for the item. getempent will
        !          1114:  * get space for multiple items whenever it calls malloc();
        !          1115:  */
        !          1116: 
        !          1117: TEMPENT *
        !          1118: getempent()
        !          1119: {
        !          1120:        /* UNIX FUNCTIONS CALLED */
        !          1121:        extern  char    *malloc( );
        !          1122: 
        !          1123:        /* EXTERNAL VARIABLES USED */
        !          1124:        extern  TEMPENT *entptr;
        !          1125:        extern  TEMPENT *oldptr;
        !          1126: 
        !          1127:        /* VARIABLES DEFINED */
        !          1128:        static  int     nument;
        !          1129: 
        !          1130: 
        !          1131:        if ((oldptr == NULL) || (nument >= MAXTEMPENTS)) {
        !          1132:                if ((entptr = (TEMPENT *) malloc( sizeof(TEMPENT) * MAXTEMPENTS )) == NULL) {
        !          1133:                        fprintf( stderr, "malloc failed, can't reset relocation\n");
        !          1134:                        exit(1);
        !          1135:                }
        !          1136: 
        !          1137:                entptr->link = oldptr;
        !          1138:                oldptr = entptr;
        !          1139:                nument = 1;
        !          1140:                entptr++;
        !          1141:        }
        !          1142:        nument++;
        !          1143:        return( entptr++ );
        !          1144: 
        !          1145: }
        !          1146: 
        !          1147: /*
        !          1148:  * freetempent() will free up the space used by the TEMPENT list
        !          1149:  */
        !          1150: 
        !          1151: freetempent( )
        !          1152: {
        !          1153:        /* EXTERNAL VARIABLES USED */
        !          1154:        extern  TEMPENT *oldptr;
        !          1155:        extern  TEMPENT *entptr;
        !          1156: 
        !          1157:        for ( ; oldptr; oldptr = oldptr->link ) {
        !          1158:                free( oldptr );
        !          1159:        }
        !          1160: 
        !          1161:        oldptr = entptr = NULL;
        !          1162: }
        !          1163: #endif
        !          1164: 
        !          1165: /*
        !          1166:  *     @(#)buildfcns.c 1.16 11/15/83;
        !          1167:  */

unix.superglobalmegacorp.com

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