1*2369Skre static char sccsid[] = "@(#)ld.c 4.3 02/07/81"; 2615Sbill /* 3898Sbill * ld - string table version for VAX 4615Sbill */ 5615Sbill 6615Sbill #include <sys/types.h> 7615Sbill #include <signal.h> 8615Sbill #include <stdio.h> 9615Sbill #include <ctype.h> 10650Sbill #include <ar.h> 11650Sbill #include <a.out.h> 12615Sbill #include <ranlib.h> 13615Sbill #include <stat.h> 14615Sbill #include <pagsiz.h> 15615Sbill 16615Sbill /* 17615Sbill * Basic strategy: 18615Sbill * 19615Sbill * The loader takes a number of files and libraries as arguments. 20615Sbill * A first pass examines each file in turn. Normal files are 21615Sbill * unconditionally loaded, and the (external) symbols they define and require 22615Sbill * are noted in the symbol table. Libraries are searched, and the 23615Sbill * library members which define needed symbols are remembered 24615Sbill * in a special data structure so they can be selected on the second 25615Sbill * pass. Symbols defined and required by library members are also 26615Sbill * recorded. 27615Sbill * 28615Sbill * After the first pass, the loader knows the size of the basic text 29615Sbill * data, and bss segments from the sum of the sizes of the modules which 30615Sbill * were required. It has computed, for each ``common'' symbol, the 31615Sbill * maximum size of any reference to it, and these symbols are then assigned 32615Sbill * storage locations after their sizes are appropriately rounded. 33615Sbill * The loader now knows all sizes for the eventual output file, and 34615Sbill * can determine the final locations of external symbols before it 35615Sbill * begins a second pass. 36615Sbill * 37615Sbill * On the second pass each normal file and required library member 38615Sbill * is processed again. The symbol table for each such file is 39615Sbill * reread and relevant parts of it are placed in the output. The offsets 40615Sbill * in the local symbol table for externally defined symbols are recorded 41615Sbill * since relocation information refers to symbols in this way. 42615Sbill * Armed with all necessary information, the text and data segments 43615Sbill * are relocated and the result is placed in the output file, which 44615Sbill * is pasted together, ``in place'', by writing to it in several 45615Sbill * different places concurrently. 46615Sbill */ 47615Sbill 48615Sbill /* 49615Sbill * Internal data structures 50615Sbill * 51615Sbill * All internal data structures are segmented and dynamically extended. 52615Sbill * The basic structures hold 1103 (NSYM) symbols, ~~200 (NROUT) 53615Sbill * referenced library members, and 100 (NSYMPR) private (local) symbols 54615Sbill * per object module. For large programs and/or modules, these structures 55615Sbill * expand to be up to 40 (NSEG) times as large as this as necessary. 56615Sbill */ 57615Sbill #define NSEG 40 /* Number of segments, each data structure */ 58615Sbill #define NSYM 1103 /* Number of symbols per segment */ 59615Sbill #define NROUT 250 /* Number of library references per segment */ 60615Sbill #define NSYMPR 100 /* Number of private symbols per segment */ 61615Sbill 62615Sbill /* 63615Sbill * Structure describing each symbol table segment. 64615Sbill * Each segment has its own hash table. We record the first 65615Sbill * address in and first address beyond both the symbol and hash 66615Sbill * tables, for use in the routine symx and the lookup routine respectively. 67615Sbill * The symfree routine also understands this structure well as it used 68615Sbill * to back out symbols from modules we decide that we don't need in pass 1. 69615Sbill * 70615Sbill * Csymseg points to the current symbol table segment; 71615Sbill * csymseg->sy_first[csymseg->sy_used] is the next symbol slot to be allocated, 72615Sbill * (unless csymseg->sy_used == NSYM in which case we will allocate another 73615Sbill * symbol table segment first.) 74615Sbill */ 75615Sbill struct symseg { 76615Sbill struct nlist *sy_first; /* base of this alloc'ed segment */ 77615Sbill struct nlist *sy_last; /* end of this segment, for n_strx */ 78615Sbill int sy_used; /* symbols used in this seg */ 79615Sbill struct nlist **sy_hfirst; /* base of hash table, this seg */ 80615Sbill struct nlist **sy_hlast; /* end of hash table, this seg */ 81615Sbill } symseg[NSEG], *csymseg; 82615Sbill 83615Sbill /* 84615Sbill * The lookup routine uses quadratic rehash. Since a quadratic rehash 85615Sbill * only probes 1/2 of the buckets in the table, and since the hash 86615Sbill * table is segmented the same way the symbol table is, we make the 87615Sbill * hash table have twice as many buckets as there are symbol table slots 88615Sbill * in the segment. This guarantees that the quadratic rehash will never 89615Sbill * fail to find an empty bucket if the segment is not full and the 90615Sbill * symbol is not there. 91615Sbill */ 92615Sbill #define HSIZE (NSYM*2) 93615Sbill 94615Sbill /* 95615Sbill * Xsym converts symbol table indices (ala x) into symbol table pointers. 96615Sbill * Symx (harder, but never used in loops) inverts pointers into the symbol 97615Sbill * table into indices using the symseg[] structure. 98615Sbill */ 99615Sbill #define xsym(x) (symseg[(x)/NSYM].sy_first+((x)%NSYM)) 100615Sbill /* symx() is a function, defined below */ 101615Sbill 102615Sbill struct nlist cursym; /* current symbol */ 103615Sbill struct nlist *lastsym; /* last symbol entered */ 104615Sbill struct nlist *nextsym; /* next available symbol table entry */ 105615Sbill struct nlist *addsym; /* first sym defined during incr load */ 106615Sbill int nsym; /* pass2: number of local symbols in a.out */ 107615Sbill /* nsym + symx(nextsym) is the symbol table size during pass2 */ 108615Sbill 109615Sbill struct nlist **lookup(), **slookup(); 110650Sbill struct nlist *p_etext, *p_edata, *p_end, *entrypt; 111615Sbill 112615Sbill /* 113615Sbill * Definitions of segmentation for library member table. 114615Sbill * For each library we encounter on pass 1 we record pointers to all 115615Sbill * members which we will load on pass 2. These are recorded as offsets 116615Sbill * into the archive in the library member table. Libraries are 117615Sbill * separated in the table by the special offset value -1. 118615Sbill */ 119615Sbill off_t li_init[NROUT]; 120615Sbill struct libseg { 121615Sbill off_t *li_first; 122615Sbill int li_used; 123615Sbill int li_used2; 124615Sbill } libseg[NSEG] = { 125615Sbill li_init, 0, 0, 126615Sbill }, *clibseg = libseg; 127615Sbill 128615Sbill /* 129615Sbill * In processing each module on pass 2 we must relocate references 130615Sbill * relative to external symbols. These references are recorded 131615Sbill * in the relocation information as relative to local symbol numbers 132615Sbill * assigned to the external symbols when the module was created. 133615Sbill * Thus before relocating the module in pass 2 we create a table 134615Sbill * which maps these internal numbers to symbol table entries. 135615Sbill * A hash table is constructed, based on the local symbol table indices, 136615Sbill * for quick lookup of these symbols. 137615Sbill */ 138615Sbill #define LHSIZ 31 139615Sbill struct local { 140615Sbill int l_index; /* index to symbol in file */ 141615Sbill struct nlist *l_symbol; /* ptr to symbol table */ 142615Sbill struct local *l_link; /* hash link */ 143615Sbill } *lochash[LHSIZ], lhinit[NSYMPR]; 144615Sbill struct locseg { 145615Sbill struct local *lo_first; 146615Sbill int lo_used; 147615Sbill } locseg[NSEG] = { 148615Sbill lhinit, 0 149615Sbill }, *clocseg; 150615Sbill 151615Sbill /* 152615Sbill * Libraries are typically built with a table of contents, 153615Sbill * which is the first member of a library with special file 154615Sbill * name __.SYMDEF and contains a list of symbol names 155615Sbill * and with each symbol the offset of the library member which defines 156615Sbill * it. The loader uses this table to quickly tell which library members 157615Sbill * are (potentially) useful. The alternative, examining the symbol 158615Sbill * table of each library member, is painfully slow for large archives. 159615Sbill * 160615Sbill * See <ranlib.h> for the definition of the ranlib structure and an 161615Sbill * explanation of the __.SYMDEF file format. 162615Sbill */ 163615Sbill int tnum; /* number of symbols in table of contents */ 164615Sbill int ssiz; /* size of string table for table of contents */ 165615Sbill struct ranlib *tab; /* the table of contents (dynamically allocated) */ 166615Sbill char *tabstr; /* string table for table of contents */ 167615Sbill 168615Sbill /* 169615Sbill * We open each input file or library only once, but in pass2 we 170615Sbill * (historically) read from such a file at 2 different places at the 171615Sbill * same time. These structures are remnants from those days, 172650Sbill * and now serve only to catch ``Premature EOF''. 173615Sbill */ 174615Sbill typedef struct { 175615Sbill short *fakeptr; 176615Sbill int bno; 177615Sbill int nibuf; 178615Sbill int nuser; 179615Sbill char buff[BSIZE]; 180615Sbill } PAGE; 181615Sbill 182615Sbill PAGE page[2]; 183615Sbill 184615Sbill struct { 185615Sbill short *fakeptr; 186615Sbill int bno; 187615Sbill int nibuf; 188615Sbill int nuser; 189615Sbill } fpage; 190615Sbill 191615Sbill typedef struct { 192615Sbill char *ptr; 193615Sbill int bno; 194615Sbill int nibuf; 195615Sbill long size; 196615Sbill long pos; 197615Sbill PAGE *pno; 198615Sbill } STREAM; 199615Sbill 200615Sbill STREAM text; 201615Sbill STREAM reloc; 202615Sbill 203615Sbill /* 204615Sbill * Header from the a.out and the archive it is from (if any). 205615Sbill */ 206615Sbill struct exec filhdr; 207615Sbill struct ar_hdr archdr; 208615Sbill #define OARMAG 0177545 209615Sbill 210615Sbill /* 211615Sbill * Options. 212615Sbill */ 213615Sbill int trace; 214615Sbill int xflag; /* discard local symbols */ 215615Sbill int Xflag; /* discard locals starting with 'L' */ 216615Sbill int Sflag; /* discard all except locals and globals*/ 217615Sbill int rflag; /* preserve relocation bits, don't define common */ 218615Sbill int arflag; /* original copy of rflag */ 219615Sbill int sflag; /* discard all symbols */ 220898Sbill int Mflag; /* print rudimentary load map */ 221615Sbill int nflag; /* pure procedure */ 222615Sbill int dflag; /* define common even with rflag */ 223650Sbill int zflag; /* demand paged */ 224615Sbill long hsize; /* size of hole at beginning of data to be squashed */ 225615Sbill int Aflag; /* doing incremental load */ 226650Sbill int Nflag; /* want impure a.out */ 227615Sbill int funding; /* reading fundamental file for incremental load */ 228898Sbill int yflag; /* number of symbols to be traced */ 229898Sbill char **ytab; /* the symbols */ 230615Sbill 231615Sbill /* 232615Sbill * These are the cumulative sizes, set in pass 1, which 233615Sbill * appear in the a.out header when the loader is finished. 234615Sbill */ 235615Sbill off_t tsize, dsize, bsize, trsize, drsize, ssize; 236615Sbill 237615Sbill /* 238615Sbill * Symbol relocation: c?rel is a scale factor which is 239615Sbill * added to an old relocation to convert it to new units; 240615Sbill * i.e. it is the difference between segment origins. 241650Sbill * (Thus if we are loading from a data segment which began at location 242650Sbill * 4 in a .o file into an a.out where it will be loaded starting at 243650Sbill * 1024, cdrel will be 1020.) 244615Sbill */ 245615Sbill long ctrel, cdrel, cbrel; 246615Sbill 247615Sbill /* 248650Sbill * Textbase is the start address of all text, 0 unless given by -T. 249615Sbill * Database is the base of all data, computed before and used during pass2. 250650Sbill */ 251650Sbill long textbase, database; 252650Sbill 253650Sbill /* 254615Sbill * The base addresses for the loaded text, data and bss from the 255615Sbill * current module during pass2 are given by torigin, dorigin and borigin. 256615Sbill */ 257615Sbill long torigin, dorigin, borigin; 258615Sbill 259615Sbill /* 260615Sbill * Errlev is nonzero when errors have occured. 261615Sbill * Delarg is an implicit argument to the routine delexit 262615Sbill * which is called on error. We do ``delarg = errlev'' before normal 263615Sbill * exits, and only if delarg is 0 (i.e. errlev was 0) do we make the 264615Sbill * result file executable. 265615Sbill */ 266615Sbill int errlev; 267615Sbill int delarg = 4; 268615Sbill 269615Sbill /* 270615Sbill * The biobuf structure and associated routines are used to write 271615Sbill * into one file at several places concurrently. Calling bopen 272615Sbill * with a biobuf structure sets it up to write ``biofd'' starting 273615Sbill * at the specified offset. You can then use ``bwrite'' and/or ``bputc'' 274615Sbill * to stuff characters in the stream, much like ``fwrite'' and ``fputc''. 275615Sbill * Calling bflush drains all the buffers and MUST be done before exit. 276615Sbill */ 277615Sbill struct biobuf { 278615Sbill short b_nleft; /* Number free spaces left in b_buf */ 279615Sbill /* Initialize to be less than BUFSIZ initially, to boundary align in file */ 280615Sbill char *b_ptr; /* Next place to stuff characters */ 281615Sbill char b_buf[BUFSIZ]; /* The buffer itself */ 282615Sbill off_t b_off; /* Current file offset */ 283615Sbill struct biobuf *b_link; /* Link in chain for bflush() */ 284615Sbill } *biobufs; 285615Sbill #define bputc(c,b) ((b)->b_nleft ? (--(b)->b_nleft, *(b)->b_ptr++ = (c)) \ 286615Sbill : bflushc(b, c)) 287615Sbill int biofd; 288615Sbill off_t boffset; 289615Sbill struct biobuf *tout, *dout, *trout, *drout, *sout, *strout; 290615Sbill 291615Sbill /* 292615Sbill * Offset is the current offset in the string file. 293615Sbill * Its initial value reflects the fact that we will 294615Sbill * eventually stuff the size of the string table at the 295615Sbill * beginning of the string table (i.e. offset itself!). 296615Sbill */ 297615Sbill off_t offset = sizeof (off_t); 298615Sbill 299615Sbill int ofilfnd; /* -o given; otherwise move l.out to a.out */ 300615Sbill char *ofilename = "l.out"; 301615Sbill int infil; /* current input file descriptor */ 302615Sbill char *filname; /* and its name */ 303615Sbill 304615Sbill /* 305615Sbill * Base of the string table of the current module (pass1 and pass2). 306615Sbill */ 307615Sbill char *curstr; 308615Sbill 309615Sbill char get(); 310615Sbill int delexit(); 311615Sbill char *savestr(); 312615Sbill 313615Sbill main(argc, argv) 314615Sbill char **argv; 315615Sbill { 316615Sbill register int c, i; 317615Sbill int num; 318615Sbill register char *ap, **p; 319615Sbill char save; 320615Sbill 321650Sbill if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 322615Sbill signal(SIGINT, delexit); 323650Sbill signal(SIGTERM, delexit); 324650Sbill } 325615Sbill if (argc == 1) 326615Sbill exit(4); 327615Sbill p = argv+1; 328615Sbill 329650Sbill /* 330650Sbill * Scan files once to find where symbols are defined. 331650Sbill */ 332615Sbill for (c=1; c<argc; c++) { 333615Sbill if (trace) 334615Sbill printf("%s:\n", *p); 335615Sbill filname = 0; 336615Sbill ap = *p++; 337615Sbill if (*ap != '-') { 338615Sbill load1arg(ap); 339615Sbill continue; 340615Sbill } 341615Sbill for (i=1; ap[i]; i++) switch (ap[i]) { 342615Sbill 343615Sbill case 'o': 344615Sbill if (++c >= argc) 345615Sbill error(1, "-o where?"); 346615Sbill ofilename = *p++; 347615Sbill ofilfnd++; 348615Sbill continue; 349615Sbill case 'u': 350615Sbill case 'e': 351615Sbill if (++c >= argc) 352615Sbill error(1, "-u or -c: arg missing"); 353615Sbill enter(slookup(*p++)); 354615Sbill if (ap[i]=='e') 355615Sbill entrypt = lastsym; 356615Sbill continue; 357615Sbill case 'H': 358615Sbill if (++c >= argc) 359615Sbill error(1, "-H: arg missing"); 360615Sbill if (tsize!=0) 361615Sbill error(1, "-H: too late, some text already loaded"); 362615Sbill hsize = atoi(*p++); 363615Sbill continue; 364615Sbill case 'A': 365615Sbill if (++c >= argc) 366615Sbill error(1, "-A: arg missing"); 367615Sbill if (Aflag) 368615Sbill error(1, "-A: only one base file allowed"); 369615Sbill Aflag = 1; 370615Sbill nflag = 0; 371615Sbill funding = 1; 372615Sbill load1arg(*p++); 373615Sbill trsize = drsize = tsize = dsize = bsize = 0; 374615Sbill ctrel = cdrel = cbrel = 0; 375615Sbill funding = 0; 376615Sbill addsym = nextsym; 377615Sbill continue; 378615Sbill case 'D': 379615Sbill if (++c >= argc) 380615Sbill error(1, "-D: arg missing"); 381615Sbill num = htoi(*p++); 382615Sbill if (dsize > num) 383615Sbill error(1, "-D: too small"); 384615Sbill dsize = num; 385615Sbill continue; 386615Sbill case 'T': 387615Sbill if (++c >= argc) 388615Sbill error(1, "-T: arg missing"); 389615Sbill if (tsize!=0) 390615Sbill error(1, "-T: too late, some text already loaded"); 391615Sbill textbase = htoi(*p++); 392615Sbill continue; 393615Sbill case 'l': 394615Sbill save = ap[--i]; 395615Sbill ap[i]='-'; 396615Sbill load1arg(&ap[i]); 397615Sbill ap[i]=save; 398615Sbill goto next; 399898Sbill case 'M': 400898Sbill Mflag++; 401898Sbill continue; 402615Sbill case 'x': 403615Sbill xflag++; 404615Sbill continue; 405615Sbill case 'X': 406615Sbill Xflag++; 407615Sbill continue; 408615Sbill case 'S': 409615Sbill Sflag++; 410615Sbill continue; 411615Sbill case 'r': 412615Sbill rflag++; 413615Sbill arflag++; 414615Sbill continue; 415615Sbill case 's': 416615Sbill sflag++; 417615Sbill xflag++; 418615Sbill continue; 419615Sbill case 'n': 420615Sbill nflag++; 421650Sbill Nflag = zflag = 0; 422615Sbill continue; 423615Sbill case 'N': 424650Sbill Nflag++; 425650Sbill nflag = zflag = 0; 426615Sbill continue; 427615Sbill case 'd': 428615Sbill dflag++; 429615Sbill continue; 430615Sbill case 'i': 431615Sbill printf("ld: -i ignored\n"); 432615Sbill continue; 433615Sbill case 't': 434615Sbill trace++; 435615Sbill continue; 436898Sbill case 'y': 437898Sbill if (ap[i+1] == 0) 438898Sbill error(1, "-y: symbol name missing"); 439898Sbill if (yflag == 0) { 440898Sbill ytab = (char **)calloc(argc, sizeof (char **)); 441898Sbill if (ytab == 0) 442898Sbill error(1, "ran out of memory (-y)"); 443898Sbill } 444898Sbill ytab[yflag++] = &ap[i+1]; 445898Sbill goto next; 446615Sbill case 'z': 447615Sbill zflag++; 448650Sbill Nflag = nflag = 0; 449615Sbill continue; 450615Sbill default: 451615Sbill filname = savestr("-x"); /* kludge */ 452615Sbill filname[1] = ap[i]; /* kludge */ 453615Sbill archdr.ar_name[0] = 0; /* kludge */ 454615Sbill error(1, "bad flag"); 455615Sbill } 456615Sbill next: 457615Sbill ; 458615Sbill } 459650Sbill if (rflag == 0 && Nflag == 0 && nflag == 0) 460650Sbill zflag++; 461615Sbill endload(argc, argv); 462615Sbill exit(0); 463615Sbill } 464615Sbill 465615Sbill /* 466615Sbill * Convert a ascii string which is a hex number. 467615Sbill * Used by -T and -D options. 468615Sbill */ 469615Sbill htoi(p) 470615Sbill register char *p; 471615Sbill { 472615Sbill register int c, n; 473615Sbill 474615Sbill n = 0; 475615Sbill while (c = *p++) { 476615Sbill n <<= 4; 477615Sbill if (isdigit(c)) 478615Sbill n += c - '0'; 479615Sbill else if (c >= 'a' && c <= 'f') 480615Sbill n += 10 + (c - 'a'); 481615Sbill else if (c >= 'A' && c <= 'F') 482615Sbill n += 10 + (c - 'A'); 483615Sbill else 484615Sbill error(1, "badly formed hex number"); 485615Sbill } 486615Sbill return (n); 487615Sbill } 488615Sbill 489615Sbill delexit() 490615Sbill { 491615Sbill 492615Sbill bflush(); 493615Sbill unlink("l.out"); 494615Sbill if (delarg==0 && Aflag==0) 495615Sbill chmod(ofilename, 0777 &~ umask(0)); 496615Sbill exit (delarg); 497615Sbill } 498615Sbill 499615Sbill endload(argc, argv) 500615Sbill int argc; 501615Sbill char **argv; 502615Sbill { 503615Sbill register int c, i; 504615Sbill long dnum; 505615Sbill register char *ap, **p; 506615Sbill 507615Sbill clibseg = libseg; 508615Sbill filname = 0; 509615Sbill middle(); 510615Sbill setupout(); 511615Sbill p = argv+1; 512615Sbill for (c=1; c<argc; c++) { 513615Sbill ap = *p++; 514615Sbill if (trace) 515615Sbill printf("%s:\n", ap); 516615Sbill if (*ap != '-') { 517615Sbill load2arg(ap); 518615Sbill continue; 519615Sbill } 520615Sbill for (i=1; ap[i]; i++) switch (ap[i]) { 521615Sbill 522615Sbill case 'D': 523615Sbill dnum = htoi(*p); 524615Sbill if (dorigin < dnum) 525615Sbill while (dorigin < dnum) 526615Sbill bputc(0, dout), dorigin++; 527615Sbill /* fall into ... */ 528615Sbill case 'T': 529615Sbill case 'u': 530615Sbill case 'e': 531615Sbill case 'o': 532615Sbill case 'H': 533615Sbill ++c; 534615Sbill ++p; 535615Sbill /* fall into ... */ 536615Sbill default: 537615Sbill continue; 538615Sbill case 'A': 539615Sbill funding = 1; 540615Sbill load2arg(*p++); 541615Sbill funding = 0; 542615Sbill c++; 543615Sbill continue; 544898Sbill case 'y': 545898Sbill goto next; 546615Sbill case 'l': 547615Sbill ap[--i]='-'; 548615Sbill load2arg(&ap[i]); 549615Sbill goto next; 550615Sbill } 551615Sbill next: 552615Sbill ; 553615Sbill } 554615Sbill finishout(); 555615Sbill } 556615Sbill 557615Sbill /* 558615Sbill * Scan file to find defined symbols. 559615Sbill */ 560615Sbill load1arg(cp) 561615Sbill register char *cp; 562615Sbill { 563615Sbill register struct ranlib *tp; 564615Sbill off_t nloc; 565898Sbill int kind; 566615Sbill 567898Sbill kind = getfile(cp); 568898Sbill if (Mflag) 569898Sbill printf("%s\n", filname); 570898Sbill switch (kind) { 571615Sbill 572615Sbill /* 573615Sbill * Plain file. 574615Sbill */ 575615Sbill case 0: 576615Sbill load1(0, 0L); 577615Sbill break; 578615Sbill 579615Sbill /* 580615Sbill * Archive without table of contents. 581615Sbill * (Slowly) process each member. 582615Sbill */ 583615Sbill case 1: 584898Sbill error(-1, 585898Sbill "warning: archive has no table of contents; add one using ranlib(1)"); 586615Sbill nloc = SARMAG; 587615Sbill while (step(nloc)) 588615Sbill nloc += sizeof(archdr) + 589615Sbill round(atol(archdr.ar_size), sizeof (short)); 590615Sbill break; 591615Sbill 592615Sbill /* 593615Sbill * Archive with table of contents. 594615Sbill * Read the table of contents and its associated string table. 595615Sbill * Pass through the library resolving symbols until nothing changes 596615Sbill * for an entire pass (i.e. you can get away with backward references 597615Sbill * when there is a table of contents!) 598615Sbill */ 599615Sbill case 2: 600615Sbill nloc = SARMAG + sizeof (archdr); 601615Sbill dseek(&text, nloc, sizeof (tnum)); 602615Sbill mget((char *)&tnum, sizeof (tnum), &text); 603615Sbill nloc += sizeof (tnum); 604615Sbill tab = (struct ranlib *)malloc(tnum); 605615Sbill if (tab == 0) 606615Sbill error(1, "ran out of memory (toc)"); 607615Sbill dseek(&text, nloc, tnum); 608615Sbill mget((char *)tab, tnum, &text); 609615Sbill nloc += tnum; 610615Sbill tnum /= sizeof (struct ranlib); 611615Sbill dseek(&text, nloc, sizeof (ssiz)); 612615Sbill mget((char *)&ssiz, sizeof (ssiz), &text); 613615Sbill nloc += sizeof (ssiz); 614615Sbill tabstr = (char *)malloc(ssiz); 615615Sbill if (tabstr == 0) 616615Sbill error(1, "ran out of memory (tocstr)"); 617615Sbill dseek(&text, nloc, ssiz); 618615Sbill mget((char *)tabstr, ssiz, &text); 619615Sbill for (tp = &tab[tnum]; --tp >= tab;) { 620615Sbill if (tp->ran_un.ran_strx < 0 || 621615Sbill tp->ran_un.ran_strx >= ssiz) 622615Sbill error(1, "mangled archive table of contents"); 623615Sbill tp->ran_un.ran_name = tabstr + tp->ran_un.ran_strx; 624615Sbill } 625615Sbill while (ldrand()) 626615Sbill continue; 627615Sbill cfree((char *)tab); 628615Sbill cfree(tabstr); 629615Sbill nextlibp(-1); 630615Sbill break; 631615Sbill 632615Sbill /* 633615Sbill * Table of contents is out of date, so search 634615Sbill * as a normal library (but skip the __.SYMDEF file). 635615Sbill */ 636615Sbill case 3: 637898Sbill error(-1, 638898Sbill "warning: table of contents for archive is out of date; rerun ranlib(1)"); 639615Sbill nloc = SARMAG; 640615Sbill do 641615Sbill nloc += sizeof(archdr) + 642615Sbill round(atol(archdr.ar_size), sizeof(short)); 643615Sbill while (step(nloc)); 644615Sbill break; 645615Sbill } 646615Sbill close(infil); 647615Sbill } 648615Sbill 649615Sbill /* 650615Sbill * Advance to the next archive member, which 651615Sbill * is at offset nloc in the archive. If the member 652615Sbill * is useful, record its location in the liblist structure 653615Sbill * for use in pass2. Mark the end of the archive in libilst with a -1. 654615Sbill */ 655615Sbill step(nloc) 656615Sbill off_t nloc; 657615Sbill { 658615Sbill 659615Sbill dseek(&text, nloc, (long) sizeof archdr); 660615Sbill if (text.size <= 0) { 661615Sbill nextlibp(-1); 662615Sbill return (0); 663615Sbill } 664615Sbill getarhdr(); 665615Sbill if (load1(1, nloc + (sizeof archdr))) 666615Sbill nextlibp(nloc); 667615Sbill return (1); 668615Sbill } 669615Sbill 670615Sbill /* 671615Sbill * Record the location of a useful archive member. 672615Sbill * Recording -1 marks the end of files from an archive. 673615Sbill * The liblist data structure is dynamically extended here. 674615Sbill */ 675615Sbill nextlibp(val) 676615Sbill off_t val; 677615Sbill { 678615Sbill 679615Sbill if (clibseg->li_used == NROUT) { 680615Sbill if (++clibseg == &libseg[NSEG]) 681615Sbill error(1, "too many files loaded from libraries"); 682615Sbill clibseg->li_first = (off_t *)malloc(NROUT * sizeof (off_t)); 683615Sbill if (clibseg->li_first == 0) 684615Sbill error(1, "ran out of memory (nextlibp)"); 685615Sbill } 686615Sbill clibseg->li_first[clibseg->li_used++] = val; 687898Sbill if (val != -1 && Mflag) 688898Sbill printf("\t%s\n", archdr.ar_name); 689615Sbill } 690615Sbill 691615Sbill /* 692615Sbill * One pass over an archive with a table of contents. 693615Sbill * Remember the number of symbols currently defined, 694615Sbill * then call step on members which look promising (i.e. 695615Sbill * that define a symbol which is currently externally undefined). 696615Sbill * Indicate to our caller whether this process netted any more symbols. 697615Sbill */ 698615Sbill ldrand() 699615Sbill { 700615Sbill register struct nlist *sp, **hp; 701615Sbill register struct ranlib *tp, *tplast; 702615Sbill off_t loc; 703615Sbill int nsymt = symx(nextsym); 704615Sbill 705615Sbill tplast = &tab[tnum-1]; 706615Sbill for (tp = tab; tp <= tplast; tp++) { 707615Sbill if ((hp = slookup(tp->ran_un.ran_name)) == 0) 708615Sbill continue; 709615Sbill sp = *hp; 710615Sbill if (sp->n_type != N_EXT+N_UNDF) 711615Sbill continue; 712615Sbill step(tp->ran_off); 713615Sbill loc = tp->ran_off; 714615Sbill while (tp < tplast && (tp+1)->ran_off == loc) 715615Sbill tp++; 716615Sbill } 717615Sbill return (symx(nextsym) != nsymt); 718615Sbill } 719615Sbill 720615Sbill /* 721615Sbill * Examine a single file or archive member on pass 1. 722615Sbill */ 723615Sbill load1(libflg, loc) 724615Sbill off_t loc; 725615Sbill { 726615Sbill register struct nlist *sp; 727615Sbill struct nlist *savnext; 728615Sbill int ndef, nlocal, type, size, nsymt; 729615Sbill register int i; 730615Sbill off_t maxoff; 731615Sbill struct stat stb; 732615Sbill 733615Sbill readhdr(loc); 734615Sbill if (filhdr.a_syms == 0) { 735615Sbill if (filhdr.a_text+filhdr.a_data == 0) 736615Sbill return (0); 737615Sbill error(1, "no namelist"); 738615Sbill } 739615Sbill if (libflg) 740615Sbill maxoff = atol(archdr.ar_size); 741615Sbill else { 742615Sbill fstat(infil, &stb); 743615Sbill maxoff = stb.st_size; 744615Sbill } 745615Sbill if (N_STROFF(filhdr) + sizeof (off_t) >= maxoff) 746615Sbill error(1, "too small (old format .o?)"); 747615Sbill ctrel = tsize; cdrel += dsize; cbrel += bsize; 748615Sbill ndef = 0; 749615Sbill nlocal = sizeof(cursym); 750615Sbill savnext = nextsym; 751615Sbill loc += N_SYMOFF(filhdr); 752615Sbill dseek(&text, loc, filhdr.a_syms); 753615Sbill dseek(&reloc, loc + filhdr.a_syms, sizeof(off_t)); 754615Sbill mget(&size, sizeof (size), &reloc); 755615Sbill dseek(&reloc, loc + filhdr.a_syms+sizeof (off_t), size-sizeof (off_t)); 756615Sbill curstr = (char *)malloc(size); 757615Sbill if (curstr == NULL) 758615Sbill error(1, "no space for string table"); 759615Sbill mget(curstr+sizeof(off_t), size-sizeof(off_t), &reloc); 760615Sbill while (text.size > 0) { 761615Sbill mget((char *)&cursym, sizeof(struct nlist), &text); 762615Sbill if (cursym.n_un.n_strx) { 763615Sbill if (cursym.n_un.n_strx<sizeof(size) || 764615Sbill cursym.n_un.n_strx>=size) 765615Sbill error(1, "bad string table index (pass 1)"); 766615Sbill cursym.n_un.n_name = curstr + cursym.n_un.n_strx; 767615Sbill } 768615Sbill type = cursym.n_type; 769615Sbill if ((type&N_EXT)==0) { 770615Sbill if (Xflag==0 || cursym.n_un.n_name[0]!='L' || 771615Sbill type & N_STAB) 772615Sbill nlocal += sizeof cursym; 773615Sbill continue; 774615Sbill } 775615Sbill symreloc(); 776615Sbill if (enter(lookup())) 777615Sbill continue; 778615Sbill if ((sp = lastsym)->n_type != N_EXT+N_UNDF) 779615Sbill continue; 780615Sbill if (cursym.n_type == N_EXT+N_UNDF) { 781615Sbill if (cursym.n_value > sp->n_value) 782615Sbill sp->n_value = cursym.n_value; 783615Sbill continue; 784615Sbill } 785615Sbill if (sp->n_value != 0 && cursym.n_type == N_EXT+N_TEXT) 786615Sbill continue; 787615Sbill ndef++; 788615Sbill sp->n_type = cursym.n_type; 789615Sbill sp->n_value = cursym.n_value; 790615Sbill } 791615Sbill if (libflg==0 || ndef) { 792615Sbill tsize += filhdr.a_text; 793615Sbill dsize += round(filhdr.a_data, sizeof (long)); 794615Sbill bsize += round(filhdr.a_bss, sizeof (long)); 795615Sbill ssize += nlocal; 796615Sbill trsize += filhdr.a_trsize; 797615Sbill drsize += filhdr.a_drsize; 798615Sbill if (funding) 799615Sbill textbase = (*slookup("_end"))->n_value; 800615Sbill nsymt = symx(nextsym); 801615Sbill for (i = symx(savnext); i < nsymt; i++) { 802615Sbill sp = xsym(i); 803615Sbill sp->n_un.n_name = savestr(sp->n_un.n_name); 804615Sbill } 805615Sbill free(curstr); 806615Sbill return (1); 807615Sbill } 808615Sbill /* 809615Sbill * No symbols defined by this library member. 810615Sbill * Rip out the hash table entries and reset the symbol table. 811615Sbill */ 812615Sbill symfree(savnext); 813615Sbill free(curstr); 814615Sbill return(0); 815615Sbill } 816615Sbill 817615Sbill middle() 818615Sbill { 819615Sbill register struct nlist *sp; 820615Sbill long csize, t, corigin, ocsize; 821615Sbill int nund, rnd; 822615Sbill char s; 823615Sbill register int i; 824615Sbill int nsymt; 825615Sbill 826615Sbill torigin = 0; 827615Sbill dorigin = 0; 828615Sbill borigin = 0; 829615Sbill 830615Sbill p_etext = *slookup("_etext"); 831615Sbill p_edata = *slookup("_edata"); 832615Sbill p_end = *slookup("_end"); 833615Sbill /* 834615Sbill * If there are any undefined symbols, save the relocation bits. 835615Sbill */ 836615Sbill nsymt = symx(nextsym); 837615Sbill if (rflag==0) { 838615Sbill for (i = 0; i < nsymt; i++) { 839615Sbill sp = xsym(i); 840615Sbill if (sp->n_type==N_EXT+N_UNDF && sp->n_value==0 && 841650Sbill sp!=p_end && sp!=p_edata && sp!=p_etext) { 842615Sbill rflag++; 843615Sbill dflag = 0; 844615Sbill break; 845615Sbill } 846615Sbill } 847615Sbill } 848615Sbill if (rflag) 849615Sbill sflag = zflag = 0; 850615Sbill /* 851615Sbill * Assign common locations. 852615Sbill */ 853615Sbill csize = 0; 854615Sbill if (!Aflag) 855615Sbill addsym = symseg[0].sy_first; 856615Sbill database = round(tsize+textbase, 857615Sbill (nflag||zflag? PAGSIZ : sizeof (long))); 858615Sbill database += hsize; 859615Sbill if (dflag || rflag==0) { 860615Sbill ldrsym(p_etext, tsize, N_EXT+N_TEXT); 861615Sbill ldrsym(p_edata, dsize, N_EXT+N_DATA); 862615Sbill ldrsym(p_end, bsize, N_EXT+N_BSS); 863615Sbill for (i = symx(addsym); i < nsymt; i++) { 864615Sbill sp = xsym(i); 865615Sbill if ((s=sp->n_type)==N_EXT+N_UNDF && 866615Sbill (t = sp->n_value)!=0) { 867615Sbill if (t >= sizeof (double)) 868615Sbill rnd = sizeof (double); 869615Sbill else if (t >= sizeof (long)) 870615Sbill rnd = sizeof (long); 871615Sbill else 872615Sbill rnd = sizeof (short); 873615Sbill csize = round(csize, rnd); 874615Sbill sp->n_value = csize; 875615Sbill sp->n_type = N_EXT+N_COMM; 876615Sbill ocsize = csize; 877615Sbill csize += t; 878615Sbill } 879615Sbill if (s&N_EXT && (s&N_TYPE)==N_UNDF && s&N_STAB) { 880615Sbill sp->n_value = ocsize; 881615Sbill sp->n_type = (s&N_STAB) | (N_EXT+N_COMM); 882615Sbill } 883615Sbill } 884615Sbill } 885615Sbill /* 886615Sbill * Now set symbols to their final value 887615Sbill */ 888615Sbill csize = round(csize, sizeof (long)); 889615Sbill torigin = textbase; 890615Sbill dorigin = database; 891615Sbill corigin = dorigin + dsize; 892615Sbill borigin = corigin + csize; 893615Sbill nund = 0; 894615Sbill nsymt = symx(nextsym); 895615Sbill for (i = symx(addsym); i<nsymt; i++) { 896615Sbill sp = xsym(i); 897615Sbill switch (sp->n_type & (N_TYPE+N_EXT)) { 898615Sbill 899615Sbill case N_EXT+N_UNDF: 900*2369Skre if (arflag == 0) 901*2369Skre errlev |= 01; 902615Sbill if ((arflag==0 || dflag) && sp->n_value==0) { 903650Sbill if (sp==p_end || sp==p_etext || sp==p_edata) 904650Sbill continue; 905615Sbill if (nund==0) 906615Sbill printf("Undefined:\n"); 907615Sbill nund++; 908615Sbill printf("%s\n", sp->n_un.n_name); 909615Sbill } 910615Sbill continue; 911615Sbill case N_EXT+N_ABS: 912615Sbill default: 913615Sbill continue; 914615Sbill case N_EXT+N_TEXT: 915615Sbill sp->n_value += torigin; 916615Sbill continue; 917615Sbill case N_EXT+N_DATA: 918615Sbill sp->n_value += dorigin; 919615Sbill continue; 920615Sbill case N_EXT+N_BSS: 921615Sbill sp->n_value += borigin; 922615Sbill continue; 923615Sbill case N_EXT+N_COMM: 924615Sbill sp->n_type = (sp->n_type & N_STAB) | (N_EXT+N_BSS); 925615Sbill sp->n_value += corigin; 926615Sbill continue; 927615Sbill } 928615Sbill } 929615Sbill if (sflag || xflag) 930615Sbill ssize = 0; 931615Sbill bsize += csize; 932615Sbill nsym = ssize / (sizeof cursym); 933615Sbill if (Aflag) { 934615Sbill fixspec(p_etext,torigin); 935615Sbill fixspec(p_edata,dorigin); 936615Sbill fixspec(p_end,borigin); 937615Sbill } 938615Sbill } 939615Sbill 940615Sbill fixspec(sym,offset) 941615Sbill struct nlist *sym; 942615Sbill long offset; 943615Sbill { 944615Sbill 945615Sbill if(symx(sym) < symx(addsym) && sym!=0) 946615Sbill sym->n_value += offset; 947615Sbill } 948615Sbill 949615Sbill ldrsym(sp, val, type) 950615Sbill register struct nlist *sp; 951615Sbill long val; 952615Sbill { 953615Sbill 954615Sbill if (sp == 0) 955615Sbill return; 956615Sbill if ((sp->n_type != N_EXT+N_UNDF || sp->n_value) && !Aflag) { 957615Sbill printf("%s: ", sp->n_un.n_name); 958615Sbill error(0, "user attempt to redfine loader-defined symbol"); 959615Sbill return; 960615Sbill } 961615Sbill sp->n_type = type; 962615Sbill sp->n_value = val; 963615Sbill } 964615Sbill 965615Sbill off_t wroff; 966615Sbill struct biobuf toutb; 967615Sbill 968615Sbill setupout() 969615Sbill { 970615Sbill int bss; 971898Sbill extern char *sys_errlist[]; 972898Sbill extern int errno; 973615Sbill 974615Sbill biofd = creat(ofilename, 0666); 975898Sbill if (biofd < 0) { 976898Sbill filname = ofilename; /* kludge */ 977898Sbill archdr.ar_name[0] = 0; /* kludge */ 978898Sbill error(1, sys_errlist[errno]); /* kludge */ 979898Sbill } 980615Sbill tout = &toutb; 981615Sbill bopen(tout, 0); 982615Sbill filhdr.a_magic = nflag ? NMAGIC : (zflag ? ZMAGIC : OMAGIC); 983615Sbill filhdr.a_text = nflag ? tsize : 984615Sbill round(tsize, zflag ? PAGSIZ : sizeof (long)); 985615Sbill filhdr.a_data = zflag ? round(dsize, PAGSIZ) : dsize; 986615Sbill bss = bsize - (filhdr.a_data - dsize); 987615Sbill if (bss < 0) 988615Sbill bss = 0; 989615Sbill filhdr.a_bss = bss; 990615Sbill filhdr.a_trsize = trsize; 991615Sbill filhdr.a_drsize = drsize; 992615Sbill filhdr.a_syms = sflag? 0: (ssize + (sizeof cursym)*symx(nextsym)); 993615Sbill if (entrypt) { 994615Sbill if (entrypt->n_type!=N_EXT+N_TEXT) 995615Sbill error(0, "entry point not in text"); 996615Sbill else 997615Sbill filhdr.a_entry = entrypt->n_value; 998615Sbill } else 999615Sbill filhdr.a_entry = 0; 1000615Sbill filhdr.a_trsize = (rflag ? trsize:0); 1001615Sbill filhdr.a_drsize = (rflag ? drsize:0); 1002615Sbill bwrite((char *)&filhdr, sizeof (filhdr), tout); 1003615Sbill if (zflag) { 1004615Sbill bflush1(tout); 1005615Sbill biobufs = 0; 1006615Sbill bopen(tout, PAGSIZ); 1007615Sbill } 1008615Sbill wroff = N_TXTOFF(filhdr) + filhdr.a_text; 1009615Sbill outb(&dout, filhdr.a_data); 1010615Sbill if (rflag) { 1011615Sbill outb(&trout, filhdr.a_trsize); 1012615Sbill outb(&drout, filhdr.a_drsize); 1013615Sbill } 1014615Sbill if (sflag==0 || xflag==0) { 1015615Sbill outb(&sout, filhdr.a_syms); 1016615Sbill wroff += sizeof (offset); 1017615Sbill outb(&strout, 0); 1018615Sbill } 1019615Sbill } 1020615Sbill 1021615Sbill outb(bp, inc) 1022615Sbill register struct biobuf **bp; 1023615Sbill { 1024615Sbill 1025615Sbill *bp = (struct biobuf *)malloc(sizeof (struct biobuf)); 1026615Sbill if (*bp == 0) 1027615Sbill error(1, "ran out of memory (outb)"); 1028615Sbill bopen(*bp, wroff); 1029615Sbill wroff += inc; 1030615Sbill } 1031615Sbill 1032615Sbill load2arg(acp) 1033615Sbill char *acp; 1034615Sbill { 1035615Sbill register char *cp; 1036615Sbill off_t loc; 1037615Sbill 1038615Sbill cp = acp; 1039615Sbill if (getfile(cp) == 0) { 1040615Sbill while (*cp) 1041615Sbill cp++; 1042615Sbill while (cp >= acp && *--cp != '/'); 1043615Sbill mkfsym(++cp); 1044615Sbill load2(0L); 1045615Sbill } else { /* scan archive members referenced */ 1046615Sbill for (;;) { 1047615Sbill if (clibseg->li_used2 == clibseg->li_used) { 1048615Sbill if (clibseg->li_used < NROUT) 1049615Sbill error(1, "libseg botch"); 1050615Sbill clibseg++; 1051615Sbill } 1052615Sbill loc = clibseg->li_first[clibseg->li_used2++]; 1053615Sbill if (loc == -1) 1054615Sbill break; 1055615Sbill dseek(&text, loc, (long)sizeof(archdr)); 1056615Sbill getarhdr(); 1057615Sbill mkfsym(archdr.ar_name); 1058615Sbill load2(loc + (long)sizeof(archdr)); 1059615Sbill } 1060615Sbill } 1061615Sbill close(infil); 1062615Sbill } 1063615Sbill 1064615Sbill load2(loc) 1065615Sbill long loc; 1066615Sbill { 1067615Sbill int size; 1068615Sbill register struct nlist *sp; 1069615Sbill register struct local *lp; 1070615Sbill register int symno, i; 1071615Sbill int type; 1072615Sbill 1073615Sbill readhdr(loc); 1074650Sbill if (!funding) { 1075615Sbill ctrel = torigin; 1076615Sbill cdrel += dorigin; 1077615Sbill cbrel += borigin; 1078615Sbill } 1079615Sbill /* 1080615Sbill * Reread the symbol table, recording the numbering 1081615Sbill * of symbols for fixing external references. 1082615Sbill */ 1083615Sbill for (i = 0; i < LHSIZ; i++) 1084615Sbill lochash[i] = 0; 1085615Sbill clocseg = locseg; 1086615Sbill clocseg->lo_used = 0; 1087615Sbill symno = -1; 1088615Sbill loc += N_TXTOFF(filhdr); 1089615Sbill dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1090615Sbill filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms, sizeof(off_t)); 1091615Sbill mget(&size, sizeof(size), &text); 1092615Sbill dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1093615Sbill filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms+sizeof(off_t), 1094615Sbill size - sizeof(off_t)); 1095615Sbill curstr = (char *)malloc(size); 1096615Sbill if (curstr == NULL) 1097615Sbill error(1, "out of space reading string table (pass 2)"); 1098615Sbill mget(curstr+sizeof(off_t), size-sizeof(off_t), &text); 1099615Sbill dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1100615Sbill filhdr.a_trsize+filhdr.a_drsize, filhdr.a_syms); 1101615Sbill while (text.size > 0) { 1102615Sbill symno++; 1103615Sbill mget((char *)&cursym, sizeof(struct nlist), &text); 1104615Sbill if (cursym.n_un.n_strx) { 1105615Sbill if (cursym.n_un.n_strx<sizeof(size) || 1106615Sbill cursym.n_un.n_strx>=size) 1107615Sbill error(1, "bad string table index (pass 2)"); 1108615Sbill cursym.n_un.n_name = curstr + cursym.n_un.n_strx; 1109615Sbill } 1110615Sbill /* inline expansion of symreloc() */ 1111615Sbill switch (cursym.n_type & 017) { 1112615Sbill 1113615Sbill case N_TEXT: 1114615Sbill case N_EXT+N_TEXT: 1115615Sbill cursym.n_value += ctrel; 1116615Sbill break; 1117615Sbill case N_DATA: 1118615Sbill case N_EXT+N_DATA: 1119615Sbill cursym.n_value += cdrel; 1120615Sbill break; 1121615Sbill case N_BSS: 1122615Sbill case N_EXT+N_BSS: 1123615Sbill cursym.n_value += cbrel; 1124615Sbill break; 1125615Sbill case N_EXT+N_UNDF: 1126615Sbill break; 1127615Sbill default: 1128615Sbill if (cursym.n_type&N_EXT) 1129615Sbill cursym.n_type = N_EXT+N_ABS; 1130615Sbill } 1131615Sbill /* end inline expansion of symreloc() */ 1132615Sbill type = cursym.n_type; 1133898Sbill if (yflag && cursym.n_un.n_name) 1134898Sbill for (i = 0; i < yflag; i++) 1135898Sbill /* fast check for 2d character! */ 1136898Sbill if (ytab[i][1] == cursym.n_un.n_name[1] && 1137898Sbill !strcmp(ytab[i], cursym.n_un.n_name)) { 1138898Sbill tracesym(); 1139898Sbill break; 1140898Sbill } 1141615Sbill if ((type&N_EXT) == 0) { 1142615Sbill if (!sflag&&!xflag&& 1143615Sbill (!Xflag||cursym.n_un.n_name[0]!='L'||type&N_STAB)) 1144615Sbill symwrite(&cursym, sout); 1145615Sbill continue; 1146615Sbill } 1147615Sbill if (funding) 1148615Sbill continue; 1149615Sbill if ((sp = *lookup()) == 0) 1150615Sbill error(1, "internal error: symbol not found"); 1151615Sbill if (cursym.n_type == N_EXT+N_UNDF) { 1152615Sbill if (clocseg->lo_used == NSYMPR) { 1153615Sbill if (++clocseg == &locseg[NSEG]) 1154615Sbill error(1, "local symbol overflow"); 1155615Sbill clocseg->lo_used = 0; 1156615Sbill } 1157615Sbill if (clocseg->lo_first == 0) { 1158615Sbill clocseg->lo_first = (struct local *) 1159615Sbill malloc(NSYMPR * sizeof (struct local)); 1160615Sbill if (clocseg->lo_first == 0) 1161615Sbill error(1, "out of memory (clocseg)"); 1162615Sbill } 1163615Sbill lp = &clocseg->lo_first[clocseg->lo_used++]; 1164615Sbill lp->l_index = symno; 1165615Sbill lp->l_symbol = sp; 1166615Sbill lp->l_link = lochash[symno % LHSIZ]; 1167615Sbill lochash[symno % LHSIZ] = lp; 1168615Sbill continue; 1169615Sbill } 1170615Sbill if (cursym.n_type & N_STAB) 1171615Sbill continue; 1172615Sbill if (cursym.n_type!=sp->n_type || cursym.n_value!=sp->n_value) { 1173615Sbill printf("%s: ", cursym.n_un.n_name); 1174615Sbill error(0, "multiply defined"); 1175615Sbill } 1176615Sbill } 1177615Sbill if (funding) 1178615Sbill return; 1179615Sbill dseek(&text, loc, filhdr.a_text); 1180615Sbill dseek(&reloc, loc+filhdr.a_text+filhdr.a_data, filhdr.a_trsize); 1181650Sbill load2td(ctrel, torigin - textbase, tout, trout); 1182615Sbill dseek(&text, loc+filhdr.a_text, filhdr.a_data); 1183615Sbill dseek(&reloc, loc+filhdr.a_text+filhdr.a_data+filhdr.a_trsize, 1184615Sbill filhdr.a_drsize); 1185650Sbill load2td(cdrel, dorigin - database, dout, drout); 1186615Sbill while (filhdr.a_data & (sizeof(long)-1)) { 1187615Sbill bputc(0, dout); 1188615Sbill filhdr.a_data++; 1189615Sbill } 1190615Sbill torigin += filhdr.a_text; 11911752Sbill dorigin += round(filhdr.a_data, sizeof (long)); 11921752Sbill borigin += round(filhdr.a_bss, sizeof (long)); 1193615Sbill free(curstr); 1194615Sbill } 1195615Sbill 1196898Sbill struct tynames { 1197898Sbill int ty_value; 1198898Sbill char *ty_name; 1199898Sbill } tynames[] = { 1200898Sbill N_UNDF, "undefined", 1201898Sbill N_ABS, "absolute", 1202898Sbill N_TEXT, "text", 1203898Sbill N_DATA, "data", 1204898Sbill N_BSS, "bss", 1205898Sbill N_COMM, "common", 1206898Sbill 0, 0, 1207898Sbill }; 1208898Sbill 1209898Sbill tracesym() 1210898Sbill { 1211898Sbill register struct tynames *tp; 1212898Sbill 1213898Sbill if (cursym.n_type & N_STAB) 1214898Sbill return; 1215898Sbill printf("%s", filname); 1216898Sbill if (archdr.ar_name[0]) 1217898Sbill printf("(%s)", archdr.ar_name); 1218898Sbill printf(": "); 1219898Sbill if ((cursym.n_type&N_TYPE) == N_UNDF && cursym.n_value) { 1220898Sbill printf("definition of common %s size %d\n", 1221898Sbill cursym.n_un.n_name, cursym.n_value); 1222898Sbill return; 1223898Sbill } 1224898Sbill for (tp = tynames; tp->ty_name; tp++) 1225898Sbill if (tp->ty_value == (cursym.n_type&N_TYPE)) 1226898Sbill break; 1227898Sbill printf((cursym.n_type&N_TYPE) ? "definition of" : "reference to"); 1228898Sbill if (cursym.n_type&N_EXT) 1229898Sbill printf(" external"); 1230898Sbill if (tp->ty_name) 1231898Sbill printf(" %s", tp->ty_name); 1232898Sbill printf(" %s\n", cursym.n_un.n_name); 1233898Sbill } 1234898Sbill 1235650Sbill /* 1236650Sbill * This routine relocates the single text or data segment argument. 1237650Sbill * Offsets from external symbols are resolved by adding the value 1238650Sbill * of the external symbols. Non-external reference are updated to account 1239650Sbill * for the relative motion of the segments (ctrel, cdrel, ...). If 1240650Sbill * a relocation was pc-relative, then we update it to reflect the 1241650Sbill * change in the positioning of the segments by adding the displacement 1242650Sbill * of the referenced segment and subtracting the displacement of the 1243650Sbill * current segment (creloc). 1244650Sbill * 1245650Sbill * If we are saving the relocation information, then we increase 1246650Sbill * each relocation datum address by our base position in the new segment. 1247650Sbill */ 1248650Sbill load2td(creloc, position, b1, b2) 1249650Sbill long creloc, offset; 1250615Sbill struct biobuf *b1, *b2; 1251615Sbill { 1252615Sbill register struct nlist *sp; 1253615Sbill register struct local *lp; 1254615Sbill long tw; 1255615Sbill register struct relocation_info *rp, *rpend; 1256615Sbill struct relocation_info *relp; 1257615Sbill char *codep; 1258615Sbill register char *cp; 1259615Sbill int relsz, codesz; 1260615Sbill 1261615Sbill relsz = reloc.size; 1262615Sbill relp = (struct relocation_info *)malloc(relsz); 1263615Sbill codesz = text.size; 1264615Sbill codep = (char *)malloc(codesz); 1265615Sbill if (relp == 0 || codep == 0) 1266615Sbill error(1, "out of memory (load2td)"); 1267615Sbill mget((char *)relp, relsz, &reloc); 1268615Sbill rpend = &relp[relsz / sizeof (struct relocation_info)]; 1269615Sbill mget(codep, codesz, &text); 1270615Sbill for (rp = relp; rp < rpend; rp++) { 1271615Sbill cp = codep + rp->r_address; 1272650Sbill /* 1273650Sbill * Pick up previous value at location to be relocated. 1274650Sbill */ 1275615Sbill switch (rp->r_length) { 1276615Sbill 1277615Sbill case 0: /* byte */ 1278615Sbill tw = *cp; 1279615Sbill break; 1280615Sbill 1281615Sbill case 1: /* word */ 1282615Sbill tw = *(short *)cp; 1283615Sbill break; 1284615Sbill 1285615Sbill case 2: /* long */ 1286615Sbill tw = *(long *)cp; 1287615Sbill break; 1288615Sbill 1289615Sbill default: 1290615Sbill error(1, "load2td botch: bad length"); 1291615Sbill } 1292650Sbill /* 1293650Sbill * If relative to an external which is defined, 1294650Sbill * resolve to a simpler kind of reference in the 1295650Sbill * result file. If the external is undefined, just 1296650Sbill * convert the symbol number to the number of the 1297650Sbill * symbol in the result file and leave it undefined. 1298650Sbill */ 1299615Sbill if (rp->r_extern) { 1300650Sbill /* 1301650Sbill * Search the hash table which maps local 1302650Sbill * symbol numbers to symbol tables entries 1303650Sbill * in the new a.out file. 1304650Sbill */ 1305615Sbill lp = lochash[rp->r_symbolnum % LHSIZ]; 1306615Sbill while (lp->l_index != rp->r_symbolnum) { 1307615Sbill lp = lp->l_link; 1308615Sbill if (lp == 0) 1309615Sbill error(1, "local symbol botch"); 1310615Sbill } 1311615Sbill sp = lp->l_symbol; 1312615Sbill if (sp->n_type == N_EXT+N_UNDF) 1313615Sbill rp->r_symbolnum = nsym+symx(sp); 1314615Sbill else { 1315615Sbill rp->r_symbolnum = sp->n_type & N_TYPE; 1316615Sbill tw += sp->n_value; 1317615Sbill rp->r_extern = 0; 1318615Sbill } 1319615Sbill } else switch (rp->r_symbolnum & N_TYPE) { 1320650Sbill /* 1321650Sbill * Relocation is relative to the loaded position 1322650Sbill * of another segment. Update by the change in position 1323650Sbill * of that segment. 1324650Sbill */ 1325615Sbill case N_TEXT: 1326615Sbill tw += ctrel; 1327615Sbill break; 1328615Sbill case N_DATA: 1329615Sbill tw += cdrel; 1330615Sbill break; 1331615Sbill case N_BSS: 1332615Sbill tw += cbrel; 1333615Sbill break; 1334615Sbill case N_ABS: 1335615Sbill break; 1336615Sbill default: 1337615Sbill error(1, "relocation format botch (symbol type))"); 1338615Sbill } 1339650Sbill /* 1340650Sbill * Relocation is pc relative, so decrease the relocation 1341650Sbill * by the amount the current segment is displaced. 1342650Sbill * (E.g if we are a relative reference to a text location 1343650Sbill * from data space, we added the increase in the text address 1344650Sbill * above, and subtract the increase in our (data) address 1345650Sbill * here, leaving the net change the relative change in the 1346650Sbill * positioning of our text and data segments.) 1347650Sbill */ 1348615Sbill if (rp->r_pcrel) 1349615Sbill tw -= creloc; 1350650Sbill /* 1351650Sbill * Put the value back in the segment, 1352650Sbill * while checking for overflow. 1353650Sbill */ 1354615Sbill switch (rp->r_length) { 1355615Sbill 1356615Sbill case 0: /* byte */ 1357615Sbill if (tw < -128 || tw > 127) 1358615Sbill error(0, "byte displacement overflow"); 1359615Sbill *cp = tw; 1360615Sbill break; 1361615Sbill case 1: /* word */ 1362615Sbill if (tw < -32768 || tw > 32767) 1363615Sbill error(0, "word displacement overflow"); 1364615Sbill *(short *)cp = tw; 1365615Sbill break; 1366615Sbill case 2: /* long */ 1367615Sbill *(long *)cp = tw; 1368615Sbill break; 1369615Sbill } 1370650Sbill /* 1371650Sbill * If we are saving relocation information, 1372650Sbill * we must convert the address in the segment from 1373650Sbill * the old .o file into an address in the segment in 1374650Sbill * the new a.out, by adding the position of our 1375650Sbill * segment in the new larger segment. 1376650Sbill */ 1377615Sbill if (rflag) 1378650Sbill rp->r_address += position; 1379615Sbill } 1380615Sbill bwrite(codep, codesz, b1); 1381615Sbill if (rflag) 1382615Sbill bwrite(relp, relsz, b2); 1383615Sbill cfree((char *)relp); 1384615Sbill cfree(codep); 1385615Sbill } 1386615Sbill 1387615Sbill finishout() 1388615Sbill { 1389615Sbill register int i; 1390615Sbill int nsymt; 1391615Sbill 1392615Sbill if (sflag==0) { 1393615Sbill nsymt = symx(nextsym); 1394615Sbill for (i = 0; i < nsymt; i++) 1395615Sbill symwrite(xsym(i), sout); 1396615Sbill bwrite(&offset, sizeof offset, sout); 1397615Sbill } 1398615Sbill if (!ofilfnd) { 1399615Sbill unlink("a.out"); 1400898Sbill if (link("l.out", "a.out") < 0) 1401898Sbill error(1, "cannot move l.out to a.out"); 1402615Sbill ofilename = "a.out"; 1403615Sbill } 1404615Sbill delarg = errlev; 1405615Sbill delexit(); 1406615Sbill } 1407615Sbill 1408615Sbill mkfsym(s) 1409615Sbill char *s; 1410615Sbill { 1411615Sbill 1412615Sbill if (sflag || xflag) 1413615Sbill return; 1414615Sbill cursym.n_un.n_name = s; 1415615Sbill cursym.n_type = N_TEXT; 1416615Sbill cursym.n_value = torigin; 1417615Sbill symwrite(&cursym, sout); 1418615Sbill } 1419615Sbill 1420615Sbill getarhdr() 1421615Sbill { 1422615Sbill register char *cp; 1423615Sbill 1424615Sbill mget((char *)&archdr, sizeof archdr, &text); 1425615Sbill for (cp=archdr.ar_name; cp<&archdr.ar_name[sizeof(archdr.ar_name)];) 1426615Sbill if (*cp++ == ' ') { 1427615Sbill cp[-1] = 0; 1428615Sbill return; 1429615Sbill } 1430615Sbill } 1431615Sbill 1432615Sbill mget(loc, n, sp) 1433615Sbill register STREAM *sp; 1434615Sbill register char *loc; 1435615Sbill { 1436615Sbill register char *p; 1437615Sbill register int take; 1438615Sbill 1439615Sbill top: 1440615Sbill if (n == 0) 1441615Sbill return; 1442615Sbill if (sp->size && sp->nibuf) { 1443615Sbill p = sp->ptr; 1444615Sbill take = sp->size; 1445615Sbill if (take > sp->nibuf) 1446615Sbill take = sp->nibuf; 1447615Sbill if (take > n) 1448615Sbill take = n; 1449615Sbill n -= take; 1450615Sbill sp->size -= take; 1451615Sbill sp->nibuf -= take; 1452615Sbill sp->pos += take; 1453615Sbill do 1454615Sbill *loc++ = *p++; 1455615Sbill while (--take > 0); 1456615Sbill sp->ptr = p; 1457615Sbill goto top; 1458615Sbill } 1459615Sbill if (n > BUFSIZ) { 1460615Sbill take = n - n % BSIZE; 1461615Sbill lseek(infil, (sp->bno+1)*BSIZE, 0); 1462615Sbill if (take > sp->size || read(infil, loc, take) != take) 1463615Sbill error(1, "premature EOF"); 1464615Sbill loc += take; 1465615Sbill n -= take; 1466615Sbill sp->size -= take; 1467615Sbill sp->pos += take; 1468615Sbill dseek(sp, (sp->bno+1+take/BSIZE)*BSIZE, -1); 1469615Sbill goto top; 1470615Sbill } 1471615Sbill *loc++ = get(sp); 1472615Sbill --n; 1473615Sbill goto top; 1474615Sbill } 1475615Sbill 1476615Sbill symwrite(sp, bp) 1477615Sbill struct nlist *sp; 1478615Sbill struct biobuf *bp; 1479615Sbill { 1480615Sbill register int len; 1481615Sbill register char *str; 1482615Sbill 1483615Sbill str = sp->n_un.n_name; 1484615Sbill if (str) { 1485615Sbill sp->n_un.n_strx = offset; 1486615Sbill len = strlen(str) + 1; 1487615Sbill bwrite(str, len, strout); 1488615Sbill offset += len; 1489615Sbill } 1490615Sbill bwrite(sp, sizeof (*sp), bp); 1491615Sbill sp->n_un.n_name = str; 1492615Sbill } 1493615Sbill 1494615Sbill dseek(sp, loc, s) 1495615Sbill register STREAM *sp; 1496615Sbill long loc, s; 1497615Sbill { 1498615Sbill register PAGE *p; 1499615Sbill register b, o; 1500615Sbill int n; 1501615Sbill 1502615Sbill b = loc>>BSHIFT; 1503615Sbill o = loc&BMASK; 1504615Sbill if (o&01) 1505615Sbill error(1, "loader error; odd offset"); 1506615Sbill --sp->pno->nuser; 1507615Sbill if ((p = &page[0])->bno!=b && (p = &page[1])->bno!=b) 1508615Sbill if (p->nuser==0 || (p = &page[0])->nuser==0) { 1509615Sbill if (page[0].nuser==0 && page[1].nuser==0) 1510615Sbill if (page[0].bno < page[1].bno) 1511615Sbill p = &page[0]; 1512615Sbill p->bno = b; 1513615Sbill lseek(infil, loc & ~(long)BMASK, 0); 1514615Sbill if ((n = read(infil, p->buff, sizeof(p->buff))) < 0) 1515615Sbill n = 0; 1516615Sbill p->nibuf = n; 1517615Sbill } else 1518615Sbill error(1, "botch: no pages"); 1519615Sbill ++p->nuser; 1520615Sbill sp->bno = b; 1521615Sbill sp->pno = p; 1522615Sbill if (s != -1) {sp->size = s; sp->pos = 0;} 1523615Sbill sp->ptr = (char *)(p->buff + o); 1524615Sbill if ((sp->nibuf = p->nibuf-o) <= 0) 1525615Sbill sp->size = 0; 1526615Sbill } 1527615Sbill 1528615Sbill char 1529615Sbill get(asp) 1530615Sbill STREAM *asp; 1531615Sbill { 1532615Sbill register STREAM *sp; 1533615Sbill 1534615Sbill sp = asp; 1535615Sbill if ((sp->nibuf -= sizeof(char)) < 0) { 1536615Sbill dseek(sp, ((long)(sp->bno+1)<<BSHIFT), (long)-1); 1537615Sbill sp->nibuf -= sizeof(char); 1538615Sbill } 1539615Sbill if ((sp->size -= sizeof(char)) <= 0) { 1540615Sbill if (sp->size < 0) 1541615Sbill error(1, "premature EOF"); 1542615Sbill ++fpage.nuser; 1543615Sbill --sp->pno->nuser; 1544615Sbill sp->pno = (PAGE *) &fpage; 1545615Sbill } 1546615Sbill sp->pos += sizeof(char); 1547615Sbill return(*sp->ptr++); 1548615Sbill } 1549615Sbill 1550615Sbill getfile(acp) 1551615Sbill char *acp; 1552615Sbill { 1553615Sbill register char *cp; 1554615Sbill register int c; 1555615Sbill char arcmag[SARMAG+1]; 1556615Sbill struct stat stb; 1557615Sbill 1558615Sbill cp = acp; 1559615Sbill infil = -1; 1560615Sbill archdr.ar_name[0] = '\0'; 1561615Sbill filname = cp; 1562615Sbill if (cp[0]=='-' && cp[1]=='l') { 1563898Sbill char *locfilname = "/usr/local/lib/libxxxxxxxxxxxxxxx"; 1564615Sbill if(cp[2] == '\0') 1565615Sbill cp = "-la"; 1566898Sbill filname = "/usr/lib/libxxxxxxxxxxxxxxx"; 1567615Sbill for(c=0; cp[c+2]; c++) { 1568615Sbill filname[c+12] = cp[c+2]; 1569615Sbill locfilname[c+18] = cp[c+2]; 1570615Sbill } 1571615Sbill filname[c+12] = locfilname[c+18] = '.'; 1572615Sbill filname[c+13] = locfilname[c+19] = 'a'; 1573615Sbill filname[c+14] = locfilname[c+20] = '\0'; 1574615Sbill if ((infil = open(filname+4, 0)) >= 0) { 1575615Sbill filname += 4; 1576615Sbill } else if ((infil = open(filname, 0)) < 0) { 1577615Sbill filname = locfilname; 1578615Sbill } 1579615Sbill } 1580615Sbill if (infil == -1 && (infil = open(filname, 0)) < 0) 1581615Sbill error(1, "cannot open"); 1582615Sbill page[0].bno = page[1].bno = -1; 1583615Sbill page[0].nuser = page[1].nuser = 0; 1584615Sbill text.pno = reloc.pno = (PAGE *) &fpage; 1585615Sbill fpage.nuser = 2; 1586615Sbill dseek(&text, 0L, SARMAG); 1587615Sbill if (text.size <= 0) 1588615Sbill error(1, "premature EOF"); 1589615Sbill mget((char *)arcmag, SARMAG, &text); 1590615Sbill arcmag[SARMAG] = 0; 1591615Sbill if (strcmp(arcmag, ARMAG)) 1592615Sbill return (0); 1593615Sbill dseek(&text, SARMAG, sizeof archdr); 1594615Sbill if(text.size <= 0) 1595615Sbill return (1); 1596615Sbill getarhdr(); 1597615Sbill if (strncmp(archdr.ar_name, "__.SYMDEF", sizeof(archdr.ar_name)) != 0) 1598615Sbill return (1); 1599615Sbill fstat(infil, &stb); 1600615Sbill return (stb.st_mtime > atol(archdr.ar_date) ? 3 : 2); 1601615Sbill } 1602615Sbill 1603615Sbill struct nlist ** 1604615Sbill lookup() 1605615Sbill { 1606615Sbill register int sh; 1607615Sbill register struct nlist **hp; 1608615Sbill register char *cp, *cp1; 1609615Sbill register struct symseg *gp; 1610615Sbill register int i; 1611615Sbill 1612615Sbill sh = 0; 1613615Sbill for (cp = cursym.n_un.n_name; *cp;) 1614615Sbill sh = (sh<<1) + *cp++; 1615615Sbill sh = (sh & 0x7fffffff) % HSIZE; 1616615Sbill for (gp = symseg; gp < &symseg[NSEG]; gp++) { 1617615Sbill if (gp->sy_first == 0) { 1618615Sbill gp->sy_first = (struct nlist *) 1619615Sbill calloc(NSYM, sizeof (struct nlist)); 1620615Sbill gp->sy_hfirst = (struct nlist **) 1621615Sbill calloc(HSIZE, sizeof (struct nlist *)); 1622615Sbill if (gp->sy_first == 0 || gp->sy_hfirst == 0) 1623615Sbill error(1, "ran out of space for symbol table"); 1624615Sbill gp->sy_last = gp->sy_first + NSYM; 1625615Sbill gp->sy_hlast = gp->sy_hfirst + HSIZE; 1626615Sbill } 1627615Sbill if (gp > csymseg) 1628615Sbill csymseg = gp; 1629615Sbill hp = gp->sy_hfirst + sh; 1630615Sbill i = 1; 1631615Sbill do { 1632615Sbill if (*hp == 0) { 1633615Sbill if (gp->sy_used == NSYM) 1634615Sbill break; 1635615Sbill return (hp); 1636615Sbill } 1637615Sbill cp1 = (*hp)->n_un.n_name; 1638615Sbill for (cp = cursym.n_un.n_name; *cp == *cp1++;) 1639615Sbill if (*cp++ == 0) 1640615Sbill return (hp); 1641615Sbill hp += i; 1642615Sbill i += 2; 1643615Sbill if (hp >= gp->sy_hlast) 1644615Sbill hp -= HSIZE; 1645615Sbill } while (i < HSIZE); 1646615Sbill if (i > HSIZE) 1647615Sbill error(1, "hash table botch"); 1648615Sbill } 1649615Sbill error(1, "symbol table overflow"); 1650615Sbill /*NOTREACHED*/ 1651615Sbill } 1652615Sbill 1653615Sbill symfree(saved) 1654615Sbill struct nlist *saved; 1655615Sbill { 1656615Sbill register struct symseg *gp; 1657615Sbill register struct nlist *sp; 1658615Sbill 1659615Sbill for (gp = csymseg; gp >= symseg; gp--, csymseg--) { 1660615Sbill sp = gp->sy_first + gp->sy_used; 1661615Sbill if (sp == saved) { 1662615Sbill nextsym = sp; 1663615Sbill return; 1664615Sbill } 1665615Sbill for (sp--; sp >= gp->sy_first; sp--) { 1666615Sbill gp->sy_hfirst[sp->n_hash] = 0; 1667615Sbill gp->sy_used--; 1668615Sbill if (sp == saved) { 1669615Sbill nextsym = sp; 1670615Sbill return; 1671615Sbill } 1672615Sbill } 1673615Sbill } 1674615Sbill if (saved == 0) 1675615Sbill return; 1676615Sbill error(1, "symfree botch"); 1677615Sbill } 1678615Sbill 1679615Sbill struct nlist ** 1680615Sbill slookup(s) 1681615Sbill char *s; 1682615Sbill { 1683615Sbill 1684615Sbill cursym.n_un.n_name = s; 1685615Sbill cursym.n_type = N_EXT+N_UNDF; 1686615Sbill cursym.n_value = 0; 1687615Sbill return (lookup()); 1688615Sbill } 1689615Sbill 1690615Sbill enter(hp) 1691615Sbill register struct nlist **hp; 1692615Sbill { 1693615Sbill register struct nlist *sp; 1694615Sbill 1695615Sbill if (*hp==0) { 1696615Sbill if (hp < csymseg->sy_hfirst || hp >= csymseg->sy_hlast) 1697615Sbill error(1, "enter botch"); 1698615Sbill *hp = lastsym = sp = csymseg->sy_first + csymseg->sy_used; 1699615Sbill csymseg->sy_used++; 1700615Sbill sp->n_un.n_name = cursym.n_un.n_name; 1701615Sbill sp->n_type = cursym.n_type; 1702615Sbill sp->n_hash = hp - csymseg->sy_hfirst; 1703615Sbill sp->n_value = cursym.n_value; 1704615Sbill nextsym = lastsym + 1; 1705615Sbill return(1); 1706615Sbill } else { 1707615Sbill lastsym = *hp; 1708615Sbill return(0); 1709615Sbill } 1710615Sbill } 1711615Sbill 1712615Sbill symx(sp) 1713615Sbill struct nlist *sp; 1714615Sbill { 1715615Sbill register struct symseg *gp; 1716615Sbill 1717615Sbill if (sp == 0) 1718615Sbill return (0); 1719615Sbill for (gp = csymseg; gp >= symseg; gp--) 1720615Sbill /* <= is sloppy so nextsym will always work */ 1721615Sbill if (sp >= gp->sy_first && sp <= gp->sy_last) 1722615Sbill return ((gp - symseg) * NSYM + sp - gp->sy_first); 1723615Sbill error(1, "symx botch"); 1724615Sbill /*NOTREACHED*/ 1725615Sbill } 1726615Sbill 1727615Sbill symreloc() 1728615Sbill { 1729615Sbill if(funding) return; 1730615Sbill switch (cursym.n_type & 017) { 1731615Sbill 1732615Sbill case N_TEXT: 1733615Sbill case N_EXT+N_TEXT: 1734615Sbill cursym.n_value += ctrel; 1735615Sbill return; 1736615Sbill 1737615Sbill case N_DATA: 1738615Sbill case N_EXT+N_DATA: 1739615Sbill cursym.n_value += cdrel; 1740615Sbill return; 1741615Sbill 1742615Sbill case N_BSS: 1743615Sbill case N_EXT+N_BSS: 1744615Sbill cursym.n_value += cbrel; 1745615Sbill return; 1746615Sbill 1747615Sbill case N_EXT+N_UNDF: 1748615Sbill return; 1749615Sbill 1750615Sbill default: 1751615Sbill if (cursym.n_type&N_EXT) 1752615Sbill cursym.n_type = N_EXT+N_ABS; 1753615Sbill return; 1754615Sbill } 1755615Sbill } 1756615Sbill 1757615Sbill error(n, s) 1758615Sbill char *s; 1759615Sbill { 1760898Sbill 1761615Sbill if (errlev==0) 1762615Sbill printf("ld:"); 1763615Sbill if (filname) { 1764615Sbill printf("%s", filname); 1765615Sbill if (n != -1 && archdr.ar_name[0]) 1766615Sbill printf("(%s)", archdr.ar_name); 1767615Sbill printf(": "); 1768615Sbill } 1769615Sbill printf("%s\n", s); 1770615Sbill if (n == -1) 1771615Sbill return; 1772615Sbill if (n) 1773615Sbill delexit(); 1774615Sbill errlev = 2; 1775615Sbill } 1776615Sbill 1777615Sbill readhdr(loc) 1778615Sbill off_t loc; 1779615Sbill { 1780615Sbill 1781615Sbill dseek(&text, loc, (long)sizeof(filhdr)); 1782615Sbill mget((short *)&filhdr, sizeof(filhdr), &text); 1783615Sbill if (N_BADMAG(filhdr)) { 1784615Sbill if (filhdr.a_magic == OARMAG) 1785615Sbill error(1, "old archive"); 1786615Sbill error(1, "bad magic number"); 1787615Sbill } 1788615Sbill if (filhdr.a_text&01 || filhdr.a_data&01) 1789615Sbill error(1, "text/data size odd"); 1790615Sbill if (filhdr.a_magic == NMAGIC || filhdr.a_magic == ZMAGIC) { 1791615Sbill cdrel = -round(filhdr.a_text, PAGSIZ); 1792615Sbill cbrel = cdrel - filhdr.a_data; 1793615Sbill } else if (filhdr.a_magic == OMAGIC) { 1794615Sbill cdrel = -filhdr.a_text; 1795615Sbill cbrel = cdrel - filhdr.a_data; 1796615Sbill } else 1797615Sbill error(1, "bad format"); 1798615Sbill } 1799615Sbill 1800615Sbill round(v, r) 1801615Sbill int v; 1802615Sbill u_long r; 1803615Sbill { 1804615Sbill 1805615Sbill r--; 1806615Sbill v += r; 1807615Sbill v &= ~(long)r; 1808615Sbill return(v); 1809615Sbill } 1810615Sbill 1811615Sbill #define NSAVETAB 8192 1812615Sbill char *savetab; 1813615Sbill int saveleft; 1814615Sbill 1815615Sbill char * 1816615Sbill savestr(cp) 1817615Sbill register char *cp; 1818615Sbill { 1819615Sbill register int len; 1820615Sbill 1821615Sbill len = strlen(cp) + 1; 1822615Sbill if (len > saveleft) { 1823615Sbill saveleft = NSAVETAB; 1824615Sbill if (len > saveleft) 1825615Sbill saveleft = len; 1826615Sbill savetab = (char *)malloc(saveleft); 1827615Sbill if (savetab == 0) 1828615Sbill error(1, "ran out of memory (savestr)"); 1829615Sbill } 1830615Sbill strncpy(savetab, cp, len); 1831615Sbill cp = savetab; 1832615Sbill savetab += len; 1833615Sbill saveleft -= len; 1834615Sbill return (cp); 1835615Sbill } 1836615Sbill 1837615Sbill bopen(bp, off) 1838615Sbill struct biobuf *bp; 1839615Sbill { 1840615Sbill 1841615Sbill bp->b_ptr = bp->b_buf; 1842615Sbill bp->b_nleft = BUFSIZ - off % BUFSIZ; 1843615Sbill bp->b_off = off; 1844615Sbill bp->b_link = biobufs; 1845615Sbill biobufs = bp; 1846615Sbill } 1847615Sbill 1848615Sbill int bwrerror; 1849615Sbill 1850615Sbill bwrite(p, cnt, bp) 1851615Sbill register char *p; 1852615Sbill register int cnt; 1853615Sbill register struct biobuf *bp; 1854615Sbill { 1855615Sbill register int put; 1856615Sbill register char *to; 1857615Sbill 1858615Sbill top: 1859615Sbill if (cnt == 0) 1860615Sbill return; 1861615Sbill if (bp->b_nleft) { 1862615Sbill put = bp->b_nleft; 1863615Sbill if (put > cnt) 1864615Sbill put = cnt; 1865615Sbill bp->b_nleft -= put; 1866615Sbill to = bp->b_ptr; 1867615Sbill asm("movc3 r8,(r11),(r7)"); 1868615Sbill bp->b_ptr += put; 1869615Sbill p += put; 1870615Sbill cnt -= put; 1871615Sbill goto top; 1872615Sbill } 1873615Sbill if (cnt >= BUFSIZ) { 1874615Sbill if (bp->b_ptr != bp->b_buf) 1875615Sbill bflush1(bp); 1876615Sbill put = cnt - cnt % BUFSIZ; 1877615Sbill if (boffset != bp->b_off) 1878615Sbill lseek(biofd, bp->b_off, 0); 1879615Sbill if (write(biofd, p, put) != put) { 1880615Sbill bwrerror = 1; 1881615Sbill error(1, "output write error"); 1882615Sbill } 1883615Sbill bp->b_off += put; 1884615Sbill boffset = bp->b_off; 1885615Sbill p += put; 1886615Sbill cnt -= put; 1887615Sbill goto top; 1888615Sbill } 1889615Sbill bflush1(bp); 1890615Sbill goto top; 1891615Sbill } 1892615Sbill 1893615Sbill bflush() 1894615Sbill { 1895615Sbill register struct biobuf *bp; 1896615Sbill 1897615Sbill if (bwrerror) 1898615Sbill return; 1899615Sbill for (bp = biobufs; bp; bp = bp->b_link) 1900615Sbill bflush1(bp); 1901615Sbill } 1902615Sbill 1903615Sbill bflush1(bp) 1904615Sbill register struct biobuf *bp; 1905615Sbill { 1906615Sbill register int cnt = bp->b_ptr - bp->b_buf; 1907615Sbill 1908615Sbill if (cnt == 0) 1909615Sbill return; 1910615Sbill if (boffset != bp->b_off) 1911615Sbill lseek(biofd, bp->b_off, 0); 1912615Sbill if (write(biofd, bp->b_buf, cnt) != cnt) { 1913615Sbill bwrerror = 1; 1914615Sbill error(1, "output write error"); 1915615Sbill } 1916615Sbill bp->b_off += cnt; 1917615Sbill boffset = bp->b_off; 1918615Sbill bp->b_ptr = bp->b_buf; 1919615Sbill bp->b_nleft = BUFSIZ; 1920615Sbill } 1921615Sbill 1922615Sbill bflushc(bp, c) 1923615Sbill register struct biobuf *bp; 1924615Sbill { 1925615Sbill 1926615Sbill bflush1(bp); 1927615Sbill bputc(c, bp); 1928615Sbill } 1929