Annotation of researchv9/jerq/sgs/32reloc.c, revision 1.1

1.1     ! root        1: /* reloc for 5620's */
        !             2: 
        !             3: #include "stdio.h"
        !             4: #include "filehdr.h"
        !             5: #include "aouthdr.h"
        !             6: #include "reloc.h"
        !             7: #include "scnhdr.h"
        !             8: #include "sys/types.h"
        !             9: #include "sys/stat.h"
        !            10: extern char *malloc();
        !            11: 
        !            12: int textorg;
        !            13: struct stat stb;
        !            14: char *buf;
        !            15: struct scnhdr *ts, *ds, *bs;
        !            16: struct filehdr *fhdr;
        !            17: long swapb4();
        !            18: 
        !            19: #ifdef TEST
        !            20: #define DEBUG(X,Y)     printf(X,Y)
        !            21: #else
        !            22: #define DEBUG(X,Y)
        !            23: #endif
        !            24: 
        !            25: main(argc, argv)
        !            26: char **argv;
        !            27: {      int fd;
        !            28:        struct aouthdr *ahdr;
        !            29:        if(argc != 4) {
        !            30:                fprintf(stderr, "usage: %s -b addr file\n",argv[0]);
        !            31:                exit(1);
        !            32:        }
        !            33:        sscanf(argv[2], "%x", &textorg);
        !            34:        fd = open(argv[3], 0);
        !            35:        if(fd < 0) {
        !            36:                perror(argv[3]);
        !            37:                exit(1);
        !            38:        }
        !            39:        fstat(fd, &stb);
        !            40:        if(stb.st_size <= sizeof(struct filehdr)) {
        !            41:                perror("stdin");
        !            42:                exit(1);
        !            43:        }
        !            44:        buf = malloc(stb.st_size);
        !            45:        if(read(fd, buf, stb.st_size) != stb.st_size) {
        !            46:                perror("read");
        !            47:                exit(1);
        !            48:        }
        !            49:        fhdr = (struct filehdr *) buf;
        !            50:        if(fhdr->f_magic != FBOMAGIC) {
        !            51:                fprintf(stderr, "reloc: file magic was 0%o expected 0%o\n",
        !            52:                        fhdr->f_magic, FBOMAGIC);
        !            53:                exit(1);
        !            54:        }
        !            55:        if(fhdr->f_nscns != 3) {
        !            56:                fprintf(stderr, "reloc: file has %d sections instead of 3\n",
        !            57:                        fhdr->f_nscns);
        !            58:                exit(1);
        !            59:        }
        !            60: 
        !            61:        ts = (struct scnhdr *) (buf + sizeof(struct filehdr) + fhdr->f_opthdr);
        !            62:        ds = ts + 1;    /* is this arithmetic ok? */
        !            63:        bs = ds + 1;
        !            64:        reloc(ts);
        !            65:        reloc(ds);
        !            66:        reloc(bs);
        !            67:        fhdr->f_flags |= F_RELFLG;
        !            68: 
        !            69:        ahdr = (struct aouthdr *) (buf + sizeof(struct filehdr));
        !            70:        ahdr->entry += textorg;
        !            71:        ahdr->text_start += textorg;
        !            72:        ahdr->data_start += textorg;
        !            73:        ahdr->vstamp = 0;
        !            74:        fhdr->f_symptr = fhdr->f_nsyms = 0;
        !            75:        write(1, buf, stb.st_size);
        !            76:        exit(0);
        !            77: }
        !            78: 
        !            79: reloc(sp, org)
        !            80: struct scnhdr *sp;
        !            81: {      struct reloc *rp, *realrp, copyarea;
        !            82:        int i, j;
        !            83:        int space;
        !            84:        unsigned int addr;
        !            85:        struct syment *sym;
        !            86:        unsigned char *from, *to, *foo, *saveto;
        !            87:        realrp = (struct reloc *) (buf + sp->s_relptr);
        !            88:        rp = &copyarea;
        !            89:        memcpy(rp, realrp, RELSZ);
        !            90:        for(i = 0; i < sp->s_nreloc; i++) {
        !            91:                switch(rp->r_type) {
        !            92:                default:
        !            93:                        fprintf(stderr, "reloc type 0%o unknown\n", rp->r_type);
        !            94:                        exit(1);
        !            95:                case R_DIR32:
        !            96:                        DEBUG(" R_DIR32 (%d)",rp->r_type);
        !            97:                        to = (unsigned char *) (buf + sp->s_scnptr + rp->r_vaddr - sp->s_vaddr);
        !            98: #ifdef old
        !            99:                        /* swap bytes on the way in... */
        !           100:                        addr = to[0];
        !           101:                        for(j = 1; j<=3; j++)
        !           102:                                addr = (addr << 8) | to[j];
        !           103:                        addr += textorg;
        !           104:                        /* swap bytes on the way out... */
        !           105:                        from = (unsigned char *)&addr;
        !           106:                        for(j = 0; j < 4; j++)
        !           107:                                *to++ = from[3-j];
        !           108:                        break;
        !           109: #endif
        !           110:                        DEBUG(" to=%x, ",*to);
        !           111: #ifdef vax
        !           112:                        from = (unsigned char *) &space;
        !           113:                        saveto = to;
        !           114:                        for(j = 3; j >= 0 ; j--)
        !           115:                                *from++ = to[j];
        !           116:                        space += textorg;
        !           117:                        from = (unsigned char *) &space;
        !           118:                        to = saveto;
        !           119:                        for(j = 3; j >= 0 ; j--)
        !           120:                                to[j] = *from++;
        !           121: #else
        !           122:                        from = (char *) &space;
        !           123:                        saveto = to;
        !           124:                        for(j = 0; j < 4; j++)
        !           125:                                *from++ = to[j];
        !           126:                        space += textorg;
        !           127:                        from = (char *) &space;
        !           128:                        to = saveto;
        !           129:                        for(j = 0; j < 4; j++)
        !           130:                                to[j] = *from++;
        !           131: #endif vax
        !           132:                        DEBUG("space=%x\n",space);  
        !           133:                        break;
        !           134: 
        !           135:                case R_DIR32S:
        !           136:                        DEBUG(" R_DIR32S (%d)",rp->r_type); 
        !           137:                        to = (unsigned char *) (buf + sp->s_scnptr + rp->r_vaddr - sp->s_vaddr);
        !           138: #ifdef wannadoitfastonavax
        !           139:                        /* alignment and byte order? */ /* vax byte order */
        !           140:                        addr = *(int *)to;
        !           141:                /*      addr += textorg;        */
        !           142:                        addr = swapb4( swapb4(addr,1) + textorg, 1);
        !           143:                        *(int *)to = addr;
        !           144: #endif
        !           145:                        DEBUG(" to=%x, ",*to);
        !           146: #ifdef vax
        !           147:                        from = (unsigned char *) &space;
        !           148:                        saveto = to;
        !           149:                        for(j = 0; j < 4; j++)
        !           150:                                *from++ = to[j];
        !           151:                        space += textorg;
        !           152:                        from = (unsigned char *) &space;
        !           153:                        to = saveto;
        !           154:                        for(j = 0; j < 4; j++)
        !           155:                                to[j] = *from++;
        !           156: #else
        !           157:                        from = (char *) &space;
        !           158:                        saveto = to;
        !           159:                        for(j = 3; j >= 0 ; j--)
        !           160:                                *from++ = to[j];
        !           161:                        space += textorg;
        !           162:                        from = (char *) &space;
        !           163:                        to = saveto;
        !           164:                        for(j = 3; j >= 0 ; j--)
        !           165:                                to[j] = *from++;
        !           166: #endif
        !           167:                        DEBUG("space=%x\n",space);  
        !           168:                }
        !           169:                /* Alignment needed for (structure) pointers on some hosts */
        !           170:                realrp = (struct reloc *) ((char *)realrp +  RELSZ);
        !           171:                memcpy(rp, realrp, RELSZ);
        !           172:        }
        !           173:        sp->s_vaddr += textorg;
        !           174:        sp->s_paddr += textorg;
        !           175:        sp->s_relptr = sp->s_lnnoptr = 0;
        !           176:        sp->s_nreloc = sp->s_nlnno = 0;
        !           177: }

unix.superglobalmegacorp.com

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