1 static char sccsid[] = "@(#)ld.c 4.5 03/31/82"; 2 3 /* 4 * ld - string table version for VAX 5 */ 6 7 #include <sys/types.h> 8 #include <signal.h> 9 #include <stdio.h> 10 #include <ctype.h> 11 #include <ar.h> 12 #include <a.out.h> 13 #include <ranlib.h> 14 #include <stat.h> 15 #include <pagsiz.h> 16 17 /* 18 * Basic strategy: 19 * 20 * The loader takes a number of files and libraries as arguments. 21 * A first pass examines each file in turn. Normal files are 22 * unconditionally loaded, and the (external) symbols they define and require 23 * are noted in the symbol table. Libraries are searched, and the 24 * library members which define needed symbols are remembered 25 * in a special data structure so they can be selected on the second 26 * pass. Symbols defined and required by library members are also 27 * recorded. 28 * 29 * After the first pass, the loader knows the size of the basic text 30 * data, and bss segments from the sum of the sizes of the modules which 31 * were required. It has computed, for each ``common'' symbol, the 32 * maximum size of any reference to it, and these symbols are then assigned 33 * storage locations after their sizes are appropriately rounded. 34 * The loader now knows all sizes for the eventual output file, and 35 * can determine the final locations of external symbols before it 36 * begins a second pass. 37 * 38 * On the second pass each normal file and required library member 39 * is processed again. The symbol table for each such file is 40 * reread and relevant parts of it are placed in the output. The offsets 41 * in the local symbol table for externally defined symbols are recorded 42 * since relocation information refers to symbols in this way. 43 * Armed with all necessary information, the text and data segments 44 * are relocated and the result is placed in the output file, which 45 * is pasted together, ``in place'', by writing to it in several 46 * different places concurrently. 47 */ 48 49 /* 50 * Internal data structures 51 * 52 * All internal data structures are segmented and dynamically extended. 53 * The basic structures hold 1103 (NSYM) symbols, ~~200 (NROUT) 54 * referenced library members, and 100 (NSYMPR) private (local) symbols 55 * per object module. For large programs and/or modules, these structures 56 * expand to be up to 40 (NSEG) times as large as this as necessary. 57 */ 58 #define NSEG 40 /* Number of segments, each data structure */ 59 #define NSYM 1103 /* Number of symbols per segment */ 60 #define NROUT 250 /* Number of library references per segment */ 61 #define NSYMPR 100 /* Number of private symbols per segment */ 62 63 /* 64 * Structure describing each symbol table segment. 65 * Each segment has its own hash table. We record the first 66 * address in and first address beyond both the symbol and hash 67 * tables, for use in the routine symx and the lookup routine respectively. 68 * The symfree routine also understands this structure well as it used 69 * to back out symbols from modules we decide that we don't need in pass 1. 70 * 71 * Csymseg points to the current symbol table segment; 72 * csymseg->sy_first[csymseg->sy_used] is the next symbol slot to be allocated, 73 * (unless csymseg->sy_used == NSYM in which case we will allocate another 74 * symbol table segment first.) 75 */ 76 struct symseg { 77 struct nlist *sy_first; /* base of this alloc'ed segment */ 78 struct nlist *sy_last; /* end of this segment, for n_strx */ 79 int sy_used; /* symbols used in this seg */ 80 struct nlist **sy_hfirst; /* base of hash table, this seg */ 81 struct nlist **sy_hlast; /* end of hash table, this seg */ 82 } symseg[NSEG], *csymseg; 83 84 /* 85 * The lookup routine uses quadratic rehash. Since a quadratic rehash 86 * only probes 1/2 of the buckets in the table, and since the hash 87 * table is segmented the same way the symbol table is, we make the 88 * hash table have twice as many buckets as there are symbol table slots 89 * in the segment. This guarantees that the quadratic rehash will never 90 * fail to find an empty bucket if the segment is not full and the 91 * symbol is not there. 92 */ 93 #define HSIZE (NSYM*2) 94 95 /* 96 * Xsym converts symbol table indices (ala x) into symbol table pointers. 97 * Symx (harder, but never used in loops) inverts pointers into the symbol 98 * table into indices using the symseg[] structure. 99 */ 100 #define xsym(x) (symseg[(x)/NSYM].sy_first+((x)%NSYM)) 101 /* symx() is a function, defined below */ 102 103 struct nlist cursym; /* current symbol */ 104 struct nlist *lastsym; /* last symbol entered */ 105 struct nlist *nextsym; /* next available symbol table entry */ 106 struct nlist *addsym; /* first sym defined during incr load */ 107 int nsym; /* pass2: number of local symbols in a.out */ 108 /* nsym + symx(nextsym) is the symbol table size during pass2 */ 109 110 struct nlist **lookup(), **slookup(); 111 struct nlist *p_etext, *p_edata, *p_end, *entrypt; 112 113 /* 114 * Definitions of segmentation for library member table. 115 * For each library we encounter on pass 1 we record pointers to all 116 * members which we will load on pass 2. These are recorded as offsets 117 * into the archive in the library member table. Libraries are 118 * separated in the table by the special offset value -1. 119 */ 120 off_t li_init[NROUT]; 121 struct libseg { 122 off_t *li_first; 123 int li_used; 124 int li_used2; 125 } libseg[NSEG] = { 126 li_init, 0, 0, 127 }, *clibseg = libseg; 128 129 /* 130 * In processing each module on pass 2 we must relocate references 131 * relative to external symbols. These references are recorded 132 * in the relocation information as relative to local symbol numbers 133 * assigned to the external symbols when the module was created. 134 * Thus before relocating the module in pass 2 we create a table 135 * which maps these internal numbers to symbol table entries. 136 * A hash table is constructed, based on the local symbol table indices, 137 * for quick lookup of these symbols. 138 */ 139 #define LHSIZ 31 140 struct local { 141 int l_index; /* index to symbol in file */ 142 struct nlist *l_symbol; /* ptr to symbol table */ 143 struct local *l_link; /* hash link */ 144 } *lochash[LHSIZ], lhinit[NSYMPR]; 145 struct locseg { 146 struct local *lo_first; 147 int lo_used; 148 } locseg[NSEG] = { 149 lhinit, 0 150 }, *clocseg; 151 152 /* 153 * Libraries are typically built with a table of contents, 154 * which is the first member of a library with special file 155 * name __.SYMDEF and contains a list of symbol names 156 * and with each symbol the offset of the library member which defines 157 * it. The loader uses this table to quickly tell which library members 158 * are (potentially) useful. The alternative, examining the symbol 159 * table of each library member, is painfully slow for large archives. 160 * 161 * See <ranlib.h> for the definition of the ranlib structure and an 162 * explanation of the __.SYMDEF file format. 163 */ 164 int tnum; /* number of symbols in table of contents */ 165 int ssiz; /* size of string table for table of contents */ 166 struct ranlib *tab; /* the table of contents (dynamically allocated) */ 167 char *tabstr; /* string table for table of contents */ 168 169 /* 170 * We open each input file or library only once, but in pass2 we 171 * (historically) read from such a file at 2 different places at the 172 * same time. These structures are remnants from those days, 173 * and now serve only to catch ``Premature EOF''. 174 * In order to make I/O more efficient, we provide routines which 175 * work in hardware page sizes. The associated constants are defined 176 * as BLKSIZE, BLKSHIFT, and BLKMASK. 177 */ 178 #define BLKSIZE 1024 179 #define BLKSHIFT 10 180 #define BLKMASK (BLKSIZE - 1) 181 typedef struct { 182 short *fakeptr; 183 int bno; 184 int nibuf; 185 int nuser; 186 char buff[BLKSIZE]; 187 } PAGE; 188 189 PAGE page[2]; 190 191 struct { 192 short *fakeptr; 193 int bno; 194 int nibuf; 195 int nuser; 196 } fpage; 197 198 typedef struct { 199 char *ptr; 200 int bno; 201 int nibuf; 202 long size; 203 long pos; 204 PAGE *pno; 205 } STREAM; 206 207 STREAM text; 208 STREAM reloc; 209 210 /* 211 * Header from the a.out and the archive it is from (if any). 212 */ 213 struct exec filhdr; 214 struct ar_hdr archdr; 215 #define OARMAG 0177545 216 217 /* 218 * Options. 219 */ 220 int trace; 221 int xflag; /* discard local symbols */ 222 int Xflag; /* discard locals starting with 'L' */ 223 int Sflag; /* discard all except locals and globals*/ 224 int rflag; /* preserve relocation bits, don't define common */ 225 int arflag; /* original copy of rflag */ 226 int sflag; /* discard all symbols */ 227 int Mflag; /* print rudimentary load map */ 228 int nflag; /* pure procedure */ 229 int dflag; /* define common even with rflag */ 230 int zflag; /* demand paged */ 231 long hsize; /* size of hole at beginning of data to be squashed */ 232 int Aflag; /* doing incremental load */ 233 int Nflag; /* want impure a.out */ 234 int funding; /* reading fundamental file for incremental load */ 235 int yflag; /* number of symbols to be traced */ 236 char **ytab; /* the symbols */ 237 238 /* 239 * These are the cumulative sizes, set in pass 1, which 240 * appear in the a.out header when the loader is finished. 241 */ 242 off_t tsize, dsize, bsize, trsize, drsize, ssize; 243 244 /* 245 * Symbol relocation: c?rel is a scale factor which is 246 * added to an old relocation to convert it to new units; 247 * i.e. it is the difference between segment origins. 248 * (Thus if we are loading from a data segment which began at location 249 * 4 in a .o file into an a.out where it will be loaded starting at 250 * 1024, cdrel will be 1020.) 251 */ 252 long ctrel, cdrel, cbrel; 253 254 /* 255 * Textbase is the start address of all text, 0 unless given by -T. 256 * Database is the base of all data, computed before and used during pass2. 257 */ 258 long textbase, database; 259 260 /* 261 * The base addresses for the loaded text, data and bss from the 262 * current module during pass2 are given by torigin, dorigin and borigin. 263 */ 264 long torigin, dorigin, borigin; 265 266 /* 267 * Errlev is nonzero when errors have occured. 268 * Delarg is an implicit argument to the routine delexit 269 * which is called on error. We do ``delarg = errlev'' before normal 270 * exits, and only if delarg is 0 (i.e. errlev was 0) do we make the 271 * result file executable. 272 */ 273 int errlev; 274 int delarg = 4; 275 276 /* 277 * The biobuf structure and associated routines are used to write 278 * into one file at several places concurrently. Calling bopen 279 * with a biobuf structure sets it up to write ``biofd'' starting 280 * at the specified offset. You can then use ``bwrite'' and/or ``bputc'' 281 * to stuff characters in the stream, much like ``fwrite'' and ``fputc''. 282 * Calling bflush drains all the buffers and MUST be done before exit. 283 */ 284 struct biobuf { 285 short b_nleft; /* Number free spaces left in b_buf */ 286 /* Initialize to be less than BUFSIZ initially, to boundary align in file */ 287 char *b_ptr; /* Next place to stuff characters */ 288 char b_buf[BUFSIZ]; /* The buffer itself */ 289 off_t b_off; /* Current file offset */ 290 struct biobuf *b_link; /* Link in chain for bflush() */ 291 } *biobufs; 292 #define bputc(c,b) ((b)->b_nleft ? (--(b)->b_nleft, *(b)->b_ptr++ = (c)) \ 293 : bflushc(b, c)) 294 int biofd; 295 off_t boffset; 296 struct biobuf *tout, *dout, *trout, *drout, *sout, *strout; 297 298 /* 299 * Offset is the current offset in the string file. 300 * Its initial value reflects the fact that we will 301 * eventually stuff the size of the string table at the 302 * beginning of the string table (i.e. offset itself!). 303 */ 304 off_t offset = sizeof (off_t); 305 306 int ofilfnd; /* -o given; otherwise move l.out to a.out */ 307 char *ofilename = "l.out"; 308 int ofilemode; /* respect umask even for unsucessful ld's */ 309 int infil; /* current input file descriptor */ 310 char *filname; /* and its name */ 311 312 /* 313 * Base of the string table of the current module (pass1 and pass2). 314 */ 315 char *curstr; 316 317 char get(); 318 int delexit(); 319 char *savestr(); 320 321 main(argc, argv) 322 char **argv; 323 { 324 register int c, i; 325 int num; 326 register char *ap, **p; 327 char save; 328 329 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 330 signal(SIGINT, delexit); 331 signal(SIGTERM, delexit); 332 } 333 if (argc == 1) 334 exit(4); 335 p = argv+1; 336 337 /* 338 * Scan files once to find where symbols are defined. 339 */ 340 for (c=1; c<argc; c++) { 341 if (trace) 342 printf("%s:\n", *p); 343 filname = 0; 344 ap = *p++; 345 if (*ap != '-') { 346 load1arg(ap); 347 continue; 348 } 349 for (i=1; ap[i]; i++) switch (ap[i]) { 350 351 case 'o': 352 if (++c >= argc) 353 error(1, "-o where?"); 354 ofilename = *p++; 355 ofilfnd++; 356 continue; 357 case 'u': 358 case 'e': 359 if (++c >= argc) 360 error(1, "-u or -c: arg missing"); 361 enter(slookup(*p++)); 362 if (ap[i]=='e') 363 entrypt = lastsym; 364 continue; 365 case 'H': 366 if (++c >= argc) 367 error(1, "-H: arg missing"); 368 if (tsize!=0) 369 error(1, "-H: too late, some text already loaded"); 370 hsize = atoi(*p++); 371 continue; 372 case 'A': 373 if (++c >= argc) 374 error(1, "-A: arg missing"); 375 if (Aflag) 376 error(1, "-A: only one base file allowed"); 377 Aflag = 1; 378 nflag = 0; 379 funding = 1; 380 load1arg(*p++); 381 trsize = drsize = tsize = dsize = bsize = 0; 382 ctrel = cdrel = cbrel = 0; 383 funding = 0; 384 addsym = nextsym; 385 continue; 386 case 'D': 387 if (++c >= argc) 388 error(1, "-D: arg missing"); 389 num = htoi(*p++); 390 if (dsize > num) 391 error(1, "-D: too small"); 392 dsize = num; 393 continue; 394 case 'T': 395 if (++c >= argc) 396 error(1, "-T: arg missing"); 397 if (tsize!=0) 398 error(1, "-T: too late, some text already loaded"); 399 textbase = htoi(*p++); 400 continue; 401 case 'l': 402 save = ap[--i]; 403 ap[i]='-'; 404 load1arg(&ap[i]); 405 ap[i]=save; 406 goto next; 407 case 'M': 408 Mflag++; 409 continue; 410 case 'x': 411 xflag++; 412 continue; 413 case 'X': 414 Xflag++; 415 continue; 416 case 'S': 417 Sflag++; 418 continue; 419 case 'r': 420 rflag++; 421 arflag++; 422 continue; 423 case 's': 424 sflag++; 425 xflag++; 426 continue; 427 case 'n': 428 nflag++; 429 Nflag = zflag = 0; 430 continue; 431 case 'N': 432 Nflag++; 433 nflag = zflag = 0; 434 continue; 435 case 'd': 436 dflag++; 437 continue; 438 case 'i': 439 printf("ld: -i ignored\n"); 440 continue; 441 case 't': 442 trace++; 443 continue; 444 case 'y': 445 if (ap[i+1] == 0) 446 error(1, "-y: symbol name missing"); 447 if (yflag == 0) { 448 ytab = (char **)calloc(argc, sizeof (char **)); 449 if (ytab == 0) 450 error(1, "ran out of memory (-y)"); 451 } 452 ytab[yflag++] = &ap[i+1]; 453 goto next; 454 case 'z': 455 zflag++; 456 Nflag = nflag = 0; 457 continue; 458 default: 459 filname = savestr("-x"); /* kludge */ 460 filname[1] = ap[i]; /* kludge */ 461 archdr.ar_name[0] = 0; /* kludge */ 462 error(1, "bad flag"); 463 } 464 next: 465 ; 466 } 467 if (rflag == 0 && Nflag == 0 && nflag == 0) 468 zflag++; 469 endload(argc, argv); 470 exit(0); 471 } 472 473 /* 474 * Convert a ascii string which is a hex number. 475 * Used by -T and -D options. 476 */ 477 htoi(p) 478 register char *p; 479 { 480 register int c, n; 481 482 n = 0; 483 while (c = *p++) { 484 n <<= 4; 485 if (isdigit(c)) 486 n += c - '0'; 487 else if (c >= 'a' && c <= 'f') 488 n += 10 + (c - 'a'); 489 else if (c >= 'A' && c <= 'F') 490 n += 10 + (c - 'A'); 491 else 492 error(1, "badly formed hex number"); 493 } 494 return (n); 495 } 496 497 delexit() 498 { 499 500 bflush(); 501 unlink("l.out"); 502 if (delarg==0 && Aflag==0) 503 chmod(ofilename, ofilemode); 504 exit (delarg); 505 } 506 507 endload(argc, argv) 508 int argc; 509 char **argv; 510 { 511 register int c, i; 512 long dnum; 513 register char *ap, **p; 514 515 clibseg = libseg; 516 filname = 0; 517 middle(); 518 setupout(); 519 p = argv+1; 520 for (c=1; c<argc; c++) { 521 ap = *p++; 522 if (trace) 523 printf("%s:\n", ap); 524 if (*ap != '-') { 525 load2arg(ap); 526 continue; 527 } 528 for (i=1; ap[i]; i++) switch (ap[i]) { 529 530 case 'D': 531 dnum = htoi(*p); 532 if (dorigin < dnum) 533 while (dorigin < dnum) 534 bputc(0, dout), dorigin++; 535 /* fall into ... */ 536 case 'T': 537 case 'u': 538 case 'e': 539 case 'o': 540 case 'H': 541 ++c; 542 ++p; 543 /* fall into ... */ 544 default: 545 continue; 546 case 'A': 547 funding = 1; 548 load2arg(*p++); 549 funding = 0; 550 c++; 551 continue; 552 case 'y': 553 goto next; 554 case 'l': 555 ap[--i]='-'; 556 load2arg(&ap[i]); 557 goto next; 558 } 559 next: 560 ; 561 } 562 finishout(); 563 } 564 565 /* 566 * Scan file to find defined symbols. 567 */ 568 load1arg(cp) 569 register char *cp; 570 { 571 register struct ranlib *tp; 572 off_t nloc; 573 int kind; 574 575 kind = getfile(cp); 576 if (Mflag) 577 printf("%s\n", filname); 578 switch (kind) { 579 580 /* 581 * Plain file. 582 */ 583 case 0: 584 load1(0, 0L); 585 break; 586 587 /* 588 * Archive without table of contents. 589 * (Slowly) process each member. 590 */ 591 case 1: 592 error(-1, 593 "warning: archive has no table of contents; add one using ranlib(1)"); 594 nloc = SARMAG; 595 while (step(nloc)) 596 nloc += sizeof(archdr) + 597 round(atol(archdr.ar_size), sizeof (short)); 598 break; 599 600 /* 601 * Archive with table of contents. 602 * Read the table of contents and its associated string table. 603 * Pass through the library resolving symbols until nothing changes 604 * for an entire pass (i.e. you can get away with backward references 605 * when there is a table of contents!) 606 */ 607 case 2: 608 nloc = SARMAG + sizeof (archdr); 609 dseek(&text, nloc, sizeof (tnum)); 610 mget((char *)&tnum, sizeof (tnum), &text); 611 nloc += sizeof (tnum); 612 tab = (struct ranlib *)malloc(tnum); 613 if (tab == 0) 614 error(1, "ran out of memory (toc)"); 615 dseek(&text, nloc, tnum); 616 mget((char *)tab, tnum, &text); 617 nloc += tnum; 618 tnum /= sizeof (struct ranlib); 619 dseek(&text, nloc, sizeof (ssiz)); 620 mget((char *)&ssiz, sizeof (ssiz), &text); 621 nloc += sizeof (ssiz); 622 tabstr = (char *)malloc(ssiz); 623 if (tabstr == 0) 624 error(1, "ran out of memory (tocstr)"); 625 dseek(&text, nloc, ssiz); 626 mget((char *)tabstr, ssiz, &text); 627 for (tp = &tab[tnum]; --tp >= tab;) { 628 if (tp->ran_un.ran_strx < 0 || 629 tp->ran_un.ran_strx >= ssiz) 630 error(1, "mangled archive table of contents"); 631 tp->ran_un.ran_name = tabstr + tp->ran_un.ran_strx; 632 } 633 while (ldrand()) 634 continue; 635 cfree((char *)tab); 636 cfree(tabstr); 637 nextlibp(-1); 638 break; 639 640 /* 641 * Table of contents is out of date, so search 642 * as a normal library (but skip the __.SYMDEF file). 643 */ 644 case 3: 645 error(-1, 646 "warning: table of contents for archive is out of date; rerun ranlib(1)"); 647 nloc = SARMAG; 648 do 649 nloc += sizeof(archdr) + 650 round(atol(archdr.ar_size), sizeof(short)); 651 while (step(nloc)); 652 break; 653 } 654 close(infil); 655 } 656 657 /* 658 * Advance to the next archive member, which 659 * is at offset nloc in the archive. If the member 660 * is useful, record its location in the liblist structure 661 * for use in pass2. Mark the end of the archive in libilst with a -1. 662 */ 663 step(nloc) 664 off_t nloc; 665 { 666 667 dseek(&text, nloc, (long) sizeof archdr); 668 if (text.size <= 0) { 669 nextlibp(-1); 670 return (0); 671 } 672 getarhdr(); 673 if (load1(1, nloc + (sizeof archdr))) 674 nextlibp(nloc); 675 return (1); 676 } 677 678 /* 679 * Record the location of a useful archive member. 680 * Recording -1 marks the end of files from an archive. 681 * The liblist data structure is dynamically extended here. 682 */ 683 nextlibp(val) 684 off_t val; 685 { 686 687 if (clibseg->li_used == NROUT) { 688 if (++clibseg == &libseg[NSEG]) 689 error(1, "too many files loaded from libraries"); 690 clibseg->li_first = (off_t *)malloc(NROUT * sizeof (off_t)); 691 if (clibseg->li_first == 0) 692 error(1, "ran out of memory (nextlibp)"); 693 } 694 clibseg->li_first[clibseg->li_used++] = val; 695 if (val != -1 && Mflag) 696 printf("\t%s\n", archdr.ar_name); 697 } 698 699 /* 700 * One pass over an archive with a table of contents. 701 * Remember the number of symbols currently defined, 702 * then call step on members which look promising (i.e. 703 * that define a symbol which is currently externally undefined). 704 * Indicate to our caller whether this process netted any more symbols. 705 */ 706 ldrand() 707 { 708 register struct nlist *sp, **hp; 709 register struct ranlib *tp, *tplast; 710 off_t loc; 711 int nsymt = symx(nextsym); 712 713 tplast = &tab[tnum-1]; 714 for (tp = tab; tp <= tplast; tp++) { 715 if ((hp = slookup(tp->ran_un.ran_name)) == 0) 716 continue; 717 sp = *hp; 718 if (sp->n_type != N_EXT+N_UNDF) 719 continue; 720 step(tp->ran_off); 721 loc = tp->ran_off; 722 while (tp < tplast && (tp+1)->ran_off == loc) 723 tp++; 724 } 725 return (symx(nextsym) != nsymt); 726 } 727 728 /* 729 * Examine a single file or archive member on pass 1. 730 */ 731 load1(libflg, loc) 732 off_t loc; 733 { 734 register struct nlist *sp; 735 struct nlist *savnext; 736 int ndef, nlocal, type, size, nsymt; 737 register int i; 738 off_t maxoff; 739 struct stat stb; 740 741 readhdr(loc); 742 if (filhdr.a_syms == 0) { 743 if (filhdr.a_text+filhdr.a_data == 0) 744 return (0); 745 error(1, "no namelist"); 746 } 747 if (libflg) 748 maxoff = atol(archdr.ar_size); 749 else { 750 fstat(infil, &stb); 751 maxoff = stb.st_size; 752 } 753 if (N_STROFF(filhdr) + sizeof (off_t) >= maxoff) 754 error(1, "too small (old format .o?)"); 755 ctrel = tsize; cdrel += dsize; cbrel += bsize; 756 ndef = 0; 757 nlocal = sizeof(cursym); 758 savnext = nextsym; 759 loc += N_SYMOFF(filhdr); 760 dseek(&text, loc, filhdr.a_syms); 761 dseek(&reloc, loc + filhdr.a_syms, sizeof(off_t)); 762 mget(&size, sizeof (size), &reloc); 763 dseek(&reloc, loc + filhdr.a_syms+sizeof (off_t), size-sizeof (off_t)); 764 curstr = (char *)malloc(size); 765 if (curstr == NULL) 766 error(1, "no space for string table"); 767 mget(curstr+sizeof(off_t), size-sizeof(off_t), &reloc); 768 while (text.size > 0) { 769 mget((char *)&cursym, sizeof(struct nlist), &text); 770 if (cursym.n_un.n_strx) { 771 if (cursym.n_un.n_strx<sizeof(size) || 772 cursym.n_un.n_strx>=size) 773 error(1, "bad string table index (pass 1)"); 774 cursym.n_un.n_name = curstr + cursym.n_un.n_strx; 775 } 776 type = cursym.n_type; 777 if ((type&N_EXT)==0) { 778 if (Xflag==0 || cursym.n_un.n_name[0]!='L' || 779 type & N_STAB) 780 nlocal += sizeof cursym; 781 continue; 782 } 783 symreloc(); 784 if (enter(lookup())) 785 continue; 786 if ((sp = lastsym)->n_type != N_EXT+N_UNDF) 787 continue; 788 if (cursym.n_type == N_EXT+N_UNDF) { 789 if (cursym.n_value > sp->n_value) 790 sp->n_value = cursym.n_value; 791 continue; 792 } 793 if (sp->n_value != 0 && cursym.n_type == N_EXT+N_TEXT) 794 continue; 795 ndef++; 796 sp->n_type = cursym.n_type; 797 sp->n_value = cursym.n_value; 798 } 799 if (libflg==0 || ndef) { 800 tsize += filhdr.a_text; 801 dsize += round(filhdr.a_data, sizeof (long)); 802 bsize += round(filhdr.a_bss, sizeof (long)); 803 ssize += nlocal; 804 trsize += filhdr.a_trsize; 805 drsize += filhdr.a_drsize; 806 if (funding) 807 textbase = (*slookup("_end"))->n_value; 808 nsymt = symx(nextsym); 809 for (i = symx(savnext); i < nsymt; i++) { 810 sp = xsym(i); 811 sp->n_un.n_name = savestr(sp->n_un.n_name); 812 } 813 free(curstr); 814 return (1); 815 } 816 /* 817 * No symbols defined by this library member. 818 * Rip out the hash table entries and reset the symbol table. 819 */ 820 symfree(savnext); 821 free(curstr); 822 return(0); 823 } 824 825 middle() 826 { 827 register struct nlist *sp; 828 long csize, t, corigin, ocsize; 829 int nund, rnd; 830 char s; 831 register int i; 832 int nsymt; 833 834 torigin = 0; 835 dorigin = 0; 836 borigin = 0; 837 838 p_etext = *slookup("_etext"); 839 p_edata = *slookup("_edata"); 840 p_end = *slookup("_end"); 841 /* 842 * If there are any undefined symbols, save the relocation bits. 843 */ 844 nsymt = symx(nextsym); 845 if (rflag==0) { 846 for (i = 0; i < nsymt; i++) { 847 sp = xsym(i); 848 if (sp->n_type==N_EXT+N_UNDF && sp->n_value==0 && 849 sp!=p_end && sp!=p_edata && sp!=p_etext) { 850 rflag++; 851 dflag = 0; 852 break; 853 } 854 } 855 } 856 if (rflag) 857 sflag = zflag = 0; 858 /* 859 * Assign common locations. 860 */ 861 csize = 0; 862 if (!Aflag) 863 addsym = symseg[0].sy_first; 864 database = round(tsize+textbase, 865 (nflag||zflag? PAGSIZ : sizeof (long))); 866 database += hsize; 867 if (dflag || rflag==0) { 868 ldrsym(p_etext, tsize, N_EXT+N_TEXT); 869 ldrsym(p_edata, dsize, N_EXT+N_DATA); 870 ldrsym(p_end, bsize, N_EXT+N_BSS); 871 for (i = symx(addsym); i < nsymt; i++) { 872 sp = xsym(i); 873 if ((s=sp->n_type)==N_EXT+N_UNDF && 874 (t = sp->n_value)!=0) { 875 if (t >= sizeof (double)) 876 rnd = sizeof (double); 877 else if (t >= sizeof (long)) 878 rnd = sizeof (long); 879 else 880 rnd = sizeof (short); 881 csize = round(csize, rnd); 882 sp->n_value = csize; 883 sp->n_type = N_EXT+N_COMM; 884 ocsize = csize; 885 csize += t; 886 } 887 if (s&N_EXT && (s&N_TYPE)==N_UNDF && s&N_STAB) { 888 sp->n_value = ocsize; 889 sp->n_type = (s&N_STAB) | (N_EXT+N_COMM); 890 } 891 } 892 } 893 /* 894 * Now set symbols to their final value 895 */ 896 csize = round(csize, sizeof (long)); 897 torigin = textbase; 898 dorigin = database; 899 corigin = dorigin + dsize; 900 borigin = corigin + csize; 901 nund = 0; 902 nsymt = symx(nextsym); 903 for (i = symx(addsym); i<nsymt; i++) { 904 sp = xsym(i); 905 switch (sp->n_type & (N_TYPE+N_EXT)) { 906 907 case N_EXT+N_UNDF: 908 if (arflag == 0) 909 errlev |= 01; 910 if ((arflag==0 || dflag) && sp->n_value==0) { 911 if (sp==p_end || sp==p_etext || sp==p_edata) 912 continue; 913 if (nund==0) 914 printf("Undefined:\n"); 915 nund++; 916 printf("%s\n", sp->n_un.n_name); 917 } 918 continue; 919 case N_EXT+N_ABS: 920 default: 921 continue; 922 case N_EXT+N_TEXT: 923 sp->n_value += torigin; 924 continue; 925 case N_EXT+N_DATA: 926 sp->n_value += dorigin; 927 continue; 928 case N_EXT+N_BSS: 929 sp->n_value += borigin; 930 continue; 931 case N_EXT+N_COMM: 932 sp->n_type = (sp->n_type & N_STAB) | (N_EXT+N_BSS); 933 sp->n_value += corigin; 934 continue; 935 } 936 } 937 if (sflag || xflag) 938 ssize = 0; 939 bsize += csize; 940 nsym = ssize / (sizeof cursym); 941 if (Aflag) { 942 fixspec(p_etext,torigin); 943 fixspec(p_edata,dorigin); 944 fixspec(p_end,borigin); 945 } 946 } 947 948 fixspec(sym,offset) 949 struct nlist *sym; 950 long offset; 951 { 952 953 if(symx(sym) < symx(addsym) && sym!=0) 954 sym->n_value += offset; 955 } 956 957 ldrsym(sp, val, type) 958 register struct nlist *sp; 959 long val; 960 { 961 962 if (sp == 0) 963 return; 964 if ((sp->n_type != N_EXT+N_UNDF || sp->n_value) && !Aflag) { 965 printf("%s: ", sp->n_un.n_name); 966 error(0, "user attempt to redfine loader-defined symbol"); 967 return; 968 } 969 sp->n_type = type; 970 sp->n_value = val; 971 } 972 973 off_t wroff; 974 struct biobuf toutb; 975 976 setupout() 977 { 978 int bss; 979 extern char *sys_errlist[]; 980 extern int errno; 981 982 ofilemode = 0777 & ~umask(0); 983 biofd = creat(ofilename, 0666 & ofilemode); 984 if (biofd < 0) { 985 filname = ofilename; /* kludge */ 986 archdr.ar_name[0] = 0; /* kludge */ 987 error(1, sys_errlist[errno]); /* kludge */ 988 } else { 989 struct stat mybuf; /* kls kludge */ 990 fstat(biofd, &mybuf); /* suppose file exists, wrong*/ 991 if(mybuf.st_mode & 0111) { /* mode, ld fails? */ 992 chmod(ofilename, mybuf.st_mode & 0666); 993 ofilemode = mybuf.st_mode; 994 } 995 } 996 tout = &toutb; 997 bopen(tout, 0); 998 filhdr.a_magic = nflag ? NMAGIC : (zflag ? ZMAGIC : OMAGIC); 999 filhdr.a_text = nflag ? tsize : 1000 round(tsize, zflag ? PAGSIZ : sizeof (long)); 1001 filhdr.a_data = zflag ? round(dsize, PAGSIZ) : dsize; 1002 bss = bsize - (filhdr.a_data - dsize); 1003 if (bss < 0) 1004 bss = 0; 1005 filhdr.a_bss = bss; 1006 filhdr.a_trsize = trsize; 1007 filhdr.a_drsize = drsize; 1008 filhdr.a_syms = sflag? 0: (ssize + (sizeof cursym)*symx(nextsym)); 1009 if (entrypt) { 1010 if (entrypt->n_type!=N_EXT+N_TEXT) 1011 error(0, "entry point not in text"); 1012 else 1013 filhdr.a_entry = entrypt->n_value; 1014 } else 1015 filhdr.a_entry = 0; 1016 filhdr.a_trsize = (rflag ? trsize:0); 1017 filhdr.a_drsize = (rflag ? drsize:0); 1018 bwrite((char *)&filhdr, sizeof (filhdr), tout); 1019 if (zflag) { 1020 bflush1(tout); 1021 biobufs = 0; 1022 bopen(tout, PAGSIZ); 1023 } 1024 wroff = N_TXTOFF(filhdr) + filhdr.a_text; 1025 outb(&dout, filhdr.a_data); 1026 if (rflag) { 1027 outb(&trout, filhdr.a_trsize); 1028 outb(&drout, filhdr.a_drsize); 1029 } 1030 if (sflag==0 || xflag==0) { 1031 outb(&sout, filhdr.a_syms); 1032 wroff += sizeof (offset); 1033 outb(&strout, 0); 1034 } 1035 } 1036 1037 outb(bp, inc) 1038 register struct biobuf **bp; 1039 { 1040 1041 *bp = (struct biobuf *)malloc(sizeof (struct biobuf)); 1042 if (*bp == 0) 1043 error(1, "ran out of memory (outb)"); 1044 bopen(*bp, wroff); 1045 wroff += inc; 1046 } 1047 1048 load2arg(acp) 1049 char *acp; 1050 { 1051 register char *cp; 1052 off_t loc; 1053 1054 cp = acp; 1055 if (getfile(cp) == 0) { 1056 while (*cp) 1057 cp++; 1058 while (cp >= acp && *--cp != '/'); 1059 mkfsym(++cp); 1060 load2(0L); 1061 } else { /* scan archive members referenced */ 1062 for (;;) { 1063 if (clibseg->li_used2 == clibseg->li_used) { 1064 if (clibseg->li_used < NROUT) 1065 error(1, "libseg botch"); 1066 clibseg++; 1067 } 1068 loc = clibseg->li_first[clibseg->li_used2++]; 1069 if (loc == -1) 1070 break; 1071 dseek(&text, loc, (long)sizeof(archdr)); 1072 getarhdr(); 1073 mkfsym(archdr.ar_name); 1074 load2(loc + (long)sizeof(archdr)); 1075 } 1076 } 1077 close(infil); 1078 } 1079 1080 load2(loc) 1081 long loc; 1082 { 1083 int size; 1084 register struct nlist *sp; 1085 register struct local *lp; 1086 register int symno, i; 1087 int type; 1088 1089 readhdr(loc); 1090 if (!funding) { 1091 ctrel = torigin; 1092 cdrel += dorigin; 1093 cbrel += borigin; 1094 } 1095 /* 1096 * Reread the symbol table, recording the numbering 1097 * of symbols for fixing external references. 1098 */ 1099 for (i = 0; i < LHSIZ; i++) 1100 lochash[i] = 0; 1101 clocseg = locseg; 1102 clocseg->lo_used = 0; 1103 symno = -1; 1104 loc += N_TXTOFF(filhdr); 1105 dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1106 filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms, sizeof(off_t)); 1107 mget(&size, sizeof(size), &text); 1108 dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1109 filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms+sizeof(off_t), 1110 size - sizeof(off_t)); 1111 curstr = (char *)malloc(size); 1112 if (curstr == NULL) 1113 error(1, "out of space reading string table (pass 2)"); 1114 mget(curstr+sizeof(off_t), size-sizeof(off_t), &text); 1115 dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1116 filhdr.a_trsize+filhdr.a_drsize, filhdr.a_syms); 1117 while (text.size > 0) { 1118 symno++; 1119 mget((char *)&cursym, sizeof(struct nlist), &text); 1120 if (cursym.n_un.n_strx) { 1121 if (cursym.n_un.n_strx<sizeof(size) || 1122 cursym.n_un.n_strx>=size) 1123 error(1, "bad string table index (pass 2)"); 1124 cursym.n_un.n_name = curstr + cursym.n_un.n_strx; 1125 } 1126 /* inline expansion of symreloc() */ 1127 switch (cursym.n_type & 017) { 1128 1129 case N_TEXT: 1130 case N_EXT+N_TEXT: 1131 cursym.n_value += ctrel; 1132 break; 1133 case N_DATA: 1134 case N_EXT+N_DATA: 1135 cursym.n_value += cdrel; 1136 break; 1137 case N_BSS: 1138 case N_EXT+N_BSS: 1139 cursym.n_value += cbrel; 1140 break; 1141 case N_EXT+N_UNDF: 1142 break; 1143 default: 1144 if (cursym.n_type&N_EXT) 1145 cursym.n_type = N_EXT+N_ABS; 1146 } 1147 /* end inline expansion of symreloc() */ 1148 type = cursym.n_type; 1149 if (yflag && cursym.n_un.n_name) 1150 for (i = 0; i < yflag; i++) 1151 /* fast check for 2d character! */ 1152 if (ytab[i][1] == cursym.n_un.n_name[1] && 1153 !strcmp(ytab[i], cursym.n_un.n_name)) { 1154 tracesym(); 1155 break; 1156 } 1157 if ((type&N_EXT) == 0) { 1158 if (!sflag&&!xflag&& 1159 (!Xflag||cursym.n_un.n_name[0]!='L'||type&N_STAB)) 1160 symwrite(&cursym, sout); 1161 continue; 1162 } 1163 if (funding) 1164 continue; 1165 if ((sp = *lookup()) == 0) 1166 error(1, "internal error: symbol not found"); 1167 if (cursym.n_type == N_EXT+N_UNDF) { 1168 if (clocseg->lo_used == NSYMPR) { 1169 if (++clocseg == &locseg[NSEG]) 1170 error(1, "local symbol overflow"); 1171 clocseg->lo_used = 0; 1172 } 1173 if (clocseg->lo_first == 0) { 1174 clocseg->lo_first = (struct local *) 1175 malloc(NSYMPR * sizeof (struct local)); 1176 if (clocseg->lo_first == 0) 1177 error(1, "out of memory (clocseg)"); 1178 } 1179 lp = &clocseg->lo_first[clocseg->lo_used++]; 1180 lp->l_index = symno; 1181 lp->l_symbol = sp; 1182 lp->l_link = lochash[symno % LHSIZ]; 1183 lochash[symno % LHSIZ] = lp; 1184 continue; 1185 } 1186 if (cursym.n_type & N_STAB) 1187 continue; 1188 if (cursym.n_type!=sp->n_type || cursym.n_value!=sp->n_value) { 1189 printf("%s: ", cursym.n_un.n_name); 1190 error(0, "multiply defined"); 1191 } 1192 } 1193 if (funding) 1194 return; 1195 dseek(&text, loc, filhdr.a_text); 1196 dseek(&reloc, loc+filhdr.a_text+filhdr.a_data, filhdr.a_trsize); 1197 load2td(ctrel, torigin - textbase, tout, trout); 1198 dseek(&text, loc+filhdr.a_text, filhdr.a_data); 1199 dseek(&reloc, loc+filhdr.a_text+filhdr.a_data+filhdr.a_trsize, 1200 filhdr.a_drsize); 1201 load2td(cdrel, dorigin - database, dout, drout); 1202 while (filhdr.a_data & (sizeof(long)-1)) { 1203 bputc(0, dout); 1204 filhdr.a_data++; 1205 } 1206 torigin += filhdr.a_text; 1207 dorigin += round(filhdr.a_data, sizeof (long)); 1208 borigin += round(filhdr.a_bss, sizeof (long)); 1209 free(curstr); 1210 } 1211 1212 struct tynames { 1213 int ty_value; 1214 char *ty_name; 1215 } tynames[] = { 1216 N_UNDF, "undefined", 1217 N_ABS, "absolute", 1218 N_TEXT, "text", 1219 N_DATA, "data", 1220 N_BSS, "bss", 1221 N_COMM, "common", 1222 0, 0, 1223 }; 1224 1225 tracesym() 1226 { 1227 register struct tynames *tp; 1228 1229 if (cursym.n_type & N_STAB) 1230 return; 1231 printf("%s", filname); 1232 if (archdr.ar_name[0]) 1233 printf("(%s)", archdr.ar_name); 1234 printf(": "); 1235 if ((cursym.n_type&N_TYPE) == N_UNDF && cursym.n_value) { 1236 printf("definition of common %s size %d\n", 1237 cursym.n_un.n_name, cursym.n_value); 1238 return; 1239 } 1240 for (tp = tynames; tp->ty_name; tp++) 1241 if (tp->ty_value == (cursym.n_type&N_TYPE)) 1242 break; 1243 printf((cursym.n_type&N_TYPE) ? "definition of" : "reference to"); 1244 if (cursym.n_type&N_EXT) 1245 printf(" external"); 1246 if (tp->ty_name) 1247 printf(" %s", tp->ty_name); 1248 printf(" %s\n", cursym.n_un.n_name); 1249 } 1250 1251 /* 1252 * This routine relocates the single text or data segment argument. 1253 * Offsets from external symbols are resolved by adding the value 1254 * of the external symbols. Non-external reference are updated to account 1255 * for the relative motion of the segments (ctrel, cdrel, ...). If 1256 * a relocation was pc-relative, then we update it to reflect the 1257 * change in the positioning of the segments by adding the displacement 1258 * of the referenced segment and subtracting the displacement of the 1259 * current segment (creloc). 1260 * 1261 * If we are saving the relocation information, then we increase 1262 * each relocation datum address by our base position in the new segment. 1263 */ 1264 load2td(creloc, position, b1, b2) 1265 long creloc, offset; 1266 struct biobuf *b1, *b2; 1267 { 1268 register struct nlist *sp; 1269 register struct local *lp; 1270 long tw; 1271 register struct relocation_info *rp, *rpend; 1272 struct relocation_info *relp; 1273 char *codep; 1274 register char *cp; 1275 int relsz, codesz; 1276 1277 relsz = reloc.size; 1278 relp = (struct relocation_info *)malloc(relsz); 1279 codesz = text.size; 1280 codep = (char *)malloc(codesz); 1281 if (relp == 0 || codep == 0) 1282 error(1, "out of memory (load2td)"); 1283 mget((char *)relp, relsz, &reloc); 1284 rpend = &relp[relsz / sizeof (struct relocation_info)]; 1285 mget(codep, codesz, &text); 1286 for (rp = relp; rp < rpend; rp++) { 1287 cp = codep + rp->r_address; 1288 /* 1289 * Pick up previous value at location to be relocated. 1290 */ 1291 switch (rp->r_length) { 1292 1293 case 0: /* byte */ 1294 tw = *cp; 1295 break; 1296 1297 case 1: /* word */ 1298 tw = *(short *)cp; 1299 break; 1300 1301 case 2: /* long */ 1302 tw = *(long *)cp; 1303 break; 1304 1305 default: 1306 error(1, "load2td botch: bad length"); 1307 } 1308 /* 1309 * If relative to an external which is defined, 1310 * resolve to a simpler kind of reference in the 1311 * result file. If the external is undefined, just 1312 * convert the symbol number to the number of the 1313 * symbol in the result file and leave it undefined. 1314 */ 1315 if (rp->r_extern) { 1316 /* 1317 * Search the hash table which maps local 1318 * symbol numbers to symbol tables entries 1319 * in the new a.out file. 1320 */ 1321 lp = lochash[rp->r_symbolnum % LHSIZ]; 1322 while (lp->l_index != rp->r_symbolnum) { 1323 lp = lp->l_link; 1324 if (lp == 0) 1325 error(1, "local symbol botch"); 1326 } 1327 sp = lp->l_symbol; 1328 if (sp->n_type == N_EXT+N_UNDF) 1329 rp->r_symbolnum = nsym+symx(sp); 1330 else { 1331 rp->r_symbolnum = sp->n_type & N_TYPE; 1332 tw += sp->n_value; 1333 rp->r_extern = 0; 1334 } 1335 } else switch (rp->r_symbolnum & N_TYPE) { 1336 /* 1337 * Relocation is relative to the loaded position 1338 * of another segment. Update by the change in position 1339 * of that segment. 1340 */ 1341 case N_TEXT: 1342 tw += ctrel; 1343 break; 1344 case N_DATA: 1345 tw += cdrel; 1346 break; 1347 case N_BSS: 1348 tw += cbrel; 1349 break; 1350 case N_ABS: 1351 break; 1352 default: 1353 error(1, "relocation format botch (symbol type))"); 1354 } 1355 /* 1356 * Relocation is pc relative, so decrease the relocation 1357 * by the amount the current segment is displaced. 1358 * (E.g if we are a relative reference to a text location 1359 * from data space, we added the increase in the text address 1360 * above, and subtract the increase in our (data) address 1361 * here, leaving the net change the relative change in the 1362 * positioning of our text and data segments.) 1363 */ 1364 if (rp->r_pcrel) 1365 tw -= creloc; 1366 /* 1367 * Put the value back in the segment, 1368 * while checking for overflow. 1369 */ 1370 switch (rp->r_length) { 1371 1372 case 0: /* byte */ 1373 if (tw < -128 || tw > 127) 1374 error(0, "byte displacement overflow"); 1375 *cp = tw; 1376 break; 1377 case 1: /* word */ 1378 if (tw < -32768 || tw > 32767) 1379 error(0, "word displacement overflow"); 1380 *(short *)cp = tw; 1381 break; 1382 case 2: /* long */ 1383 *(long *)cp = tw; 1384 break; 1385 } 1386 /* 1387 * If we are saving relocation information, 1388 * we must convert the address in the segment from 1389 * the old .o file into an address in the segment in 1390 * the new a.out, by adding the position of our 1391 * segment in the new larger segment. 1392 */ 1393 if (rflag) 1394 rp->r_address += position; 1395 } 1396 bwrite(codep, codesz, b1); 1397 if (rflag) 1398 bwrite(relp, relsz, b2); 1399 cfree((char *)relp); 1400 cfree(codep); 1401 } 1402 1403 finishout() 1404 { 1405 register int i; 1406 int nsymt; 1407 1408 if (sflag==0) { 1409 nsymt = symx(nextsym); 1410 for (i = 0; i < nsymt; i++) 1411 symwrite(xsym(i), sout); 1412 bwrite(&offset, sizeof offset, sout); 1413 } 1414 if (!ofilfnd) { 1415 unlink("a.out"); 1416 if (link("l.out", "a.out") < 0) 1417 error(1, "cannot move l.out to a.out"); 1418 ofilename = "a.out"; 1419 } 1420 delarg = errlev; 1421 delexit(); 1422 } 1423 1424 mkfsym(s) 1425 char *s; 1426 { 1427 1428 if (sflag || xflag) 1429 return; 1430 cursym.n_un.n_name = s; 1431 cursym.n_type = N_TEXT; 1432 cursym.n_value = torigin; 1433 symwrite(&cursym, sout); 1434 } 1435 1436 getarhdr() 1437 { 1438 register char *cp; 1439 1440 mget((char *)&archdr, sizeof archdr, &text); 1441 for (cp=archdr.ar_name; cp<&archdr.ar_name[sizeof(archdr.ar_name)];) 1442 if (*cp++ == ' ') { 1443 cp[-1] = 0; 1444 return; 1445 } 1446 } 1447 1448 mget(loc, n, sp) 1449 register STREAM *sp; 1450 register char *loc; 1451 { 1452 register char *p; 1453 register int take; 1454 1455 top: 1456 if (n == 0) 1457 return; 1458 if (sp->size && sp->nibuf) { 1459 p = sp->ptr; 1460 take = sp->size; 1461 if (take > sp->nibuf) 1462 take = sp->nibuf; 1463 if (take > n) 1464 take = n; 1465 n -= take; 1466 sp->size -= take; 1467 sp->nibuf -= take; 1468 sp->pos += take; 1469 do 1470 *loc++ = *p++; 1471 while (--take > 0); 1472 sp->ptr = p; 1473 goto top; 1474 } 1475 if (n > BUFSIZ) { 1476 take = n - n % BLKSIZE; 1477 lseek(infil, (sp->bno+1)*BLKSIZE, 0); 1478 if (take > sp->size || read(infil, loc, take) != take) 1479 error(1, "premature EOF"); 1480 loc += take; 1481 n -= take; 1482 sp->size -= take; 1483 sp->pos += take; 1484 dseek(sp, (sp->bno+1+take/BLKSIZE)*BLKSIZE, -1); 1485 goto top; 1486 } 1487 *loc++ = get(sp); 1488 --n; 1489 goto top; 1490 } 1491 1492 symwrite(sp, bp) 1493 struct nlist *sp; 1494 struct biobuf *bp; 1495 { 1496 register int len; 1497 register char *str; 1498 1499 str = sp->n_un.n_name; 1500 if (str) { 1501 sp->n_un.n_strx = offset; 1502 len = strlen(str) + 1; 1503 bwrite(str, len, strout); 1504 offset += len; 1505 } 1506 bwrite(sp, sizeof (*sp), bp); 1507 sp->n_un.n_name = str; 1508 } 1509 1510 dseek(sp, loc, s) 1511 register STREAM *sp; 1512 long loc, s; 1513 { 1514 register PAGE *p; 1515 register b, o; 1516 int n; 1517 1518 b = loc>>BLKSHIFT; 1519 o = loc&BLKMASK; 1520 if (o&01) 1521 error(1, "loader error; odd offset"); 1522 --sp->pno->nuser; 1523 if ((p = &page[0])->bno!=b && (p = &page[1])->bno!=b) 1524 if (p->nuser==0 || (p = &page[0])->nuser==0) { 1525 if (page[0].nuser==0 && page[1].nuser==0) 1526 if (page[0].bno < page[1].bno) 1527 p = &page[0]; 1528 p->bno = b; 1529 lseek(infil, loc & ~(long)BLKMASK, 0); 1530 if ((n = read(infil, p->buff, sizeof(p->buff))) < 0) 1531 n = 0; 1532 p->nibuf = n; 1533 } else 1534 error(1, "botch: no pages"); 1535 ++p->nuser; 1536 sp->bno = b; 1537 sp->pno = p; 1538 if (s != -1) {sp->size = s; sp->pos = 0;} 1539 sp->ptr = (char *)(p->buff + o); 1540 if ((sp->nibuf = p->nibuf-o) <= 0) 1541 sp->size = 0; 1542 } 1543 1544 char 1545 get(asp) 1546 STREAM *asp; 1547 { 1548 register STREAM *sp; 1549 1550 sp = asp; 1551 if ((sp->nibuf -= sizeof(char)) < 0) { 1552 dseek(sp, ((long)(sp->bno+1)<<BLKSHIFT), (long)-1); 1553 sp->nibuf -= sizeof(char); 1554 } 1555 if ((sp->size -= sizeof(char)) <= 0) { 1556 if (sp->size < 0) 1557 error(1, "premature EOF"); 1558 ++fpage.nuser; 1559 --sp->pno->nuser; 1560 sp->pno = (PAGE *) &fpage; 1561 } 1562 sp->pos += sizeof(char); 1563 return(*sp->ptr++); 1564 } 1565 1566 getfile(acp) 1567 char *acp; 1568 { 1569 register char *cp; 1570 register int c; 1571 char arcmag[SARMAG+1]; 1572 struct stat stb; 1573 1574 cp = acp; 1575 infil = -1; 1576 archdr.ar_name[0] = '\0'; 1577 filname = cp; 1578 if (cp[0]=='-' && cp[1]=='l') { 1579 char *locfilname = "/usr/local/lib/libxxxxxxxxxxxxxxx"; 1580 if(cp[2] == '\0') 1581 cp = "-la"; 1582 filname = "/usr/lib/libxxxxxxxxxxxxxxx"; 1583 for(c=0; cp[c+2]; c++) { 1584 filname[c+12] = cp[c+2]; 1585 locfilname[c+18] = cp[c+2]; 1586 } 1587 filname[c+12] = locfilname[c+18] = '.'; 1588 filname[c+13] = locfilname[c+19] = 'a'; 1589 filname[c+14] = locfilname[c+20] = '\0'; 1590 if ((infil = open(filname+4, 0)) >= 0) { 1591 filname += 4; 1592 } else if ((infil = open(filname, 0)) < 0) { 1593 filname = locfilname; 1594 } 1595 } 1596 if (infil == -1 && (infil = open(filname, 0)) < 0) 1597 error(1, "cannot open"); 1598 page[0].bno = page[1].bno = -1; 1599 page[0].nuser = page[1].nuser = 0; 1600 text.pno = reloc.pno = (PAGE *) &fpage; 1601 fpage.nuser = 2; 1602 dseek(&text, 0L, SARMAG); 1603 if (text.size <= 0) 1604 error(1, "premature EOF"); 1605 mget((char *)arcmag, SARMAG, &text); 1606 arcmag[SARMAG] = 0; 1607 if (strcmp(arcmag, ARMAG)) 1608 return (0); 1609 dseek(&text, SARMAG, sizeof archdr); 1610 if(text.size <= 0) 1611 return (1); 1612 getarhdr(); 1613 if (strncmp(archdr.ar_name, "__.SYMDEF", sizeof(archdr.ar_name)) != 0) 1614 return (1); 1615 fstat(infil, &stb); 1616 return (stb.st_mtime > atol(archdr.ar_date) ? 3 : 2); 1617 } 1618 1619 struct nlist ** 1620 lookup() 1621 { 1622 register int sh; 1623 register struct nlist **hp; 1624 register char *cp, *cp1; 1625 register struct symseg *gp; 1626 register int i; 1627 1628 sh = 0; 1629 for (cp = cursym.n_un.n_name; *cp;) 1630 sh = (sh<<1) + *cp++; 1631 sh = (sh & 0x7fffffff) % HSIZE; 1632 for (gp = symseg; gp < &symseg[NSEG]; gp++) { 1633 if (gp->sy_first == 0) { 1634 gp->sy_first = (struct nlist *) 1635 calloc(NSYM, sizeof (struct nlist)); 1636 gp->sy_hfirst = (struct nlist **) 1637 calloc(HSIZE, sizeof (struct nlist *)); 1638 if (gp->sy_first == 0 || gp->sy_hfirst == 0) 1639 error(1, "ran out of space for symbol table"); 1640 gp->sy_last = gp->sy_first + NSYM; 1641 gp->sy_hlast = gp->sy_hfirst + HSIZE; 1642 } 1643 if (gp > csymseg) 1644 csymseg = gp; 1645 hp = gp->sy_hfirst + sh; 1646 i = 1; 1647 do { 1648 if (*hp == 0) { 1649 if (gp->sy_used == NSYM) 1650 break; 1651 return (hp); 1652 } 1653 cp1 = (*hp)->n_un.n_name; 1654 for (cp = cursym.n_un.n_name; *cp == *cp1++;) 1655 if (*cp++ == 0) 1656 return (hp); 1657 hp += i; 1658 i += 2; 1659 if (hp >= gp->sy_hlast) 1660 hp -= HSIZE; 1661 } while (i < HSIZE); 1662 if (i > HSIZE) 1663 error(1, "hash table botch"); 1664 } 1665 error(1, "symbol table overflow"); 1666 /*NOTREACHED*/ 1667 } 1668 1669 symfree(saved) 1670 struct nlist *saved; 1671 { 1672 register struct symseg *gp; 1673 register struct nlist *sp; 1674 1675 for (gp = csymseg; gp >= symseg; gp--, csymseg--) { 1676 sp = gp->sy_first + gp->sy_used; 1677 if (sp == saved) { 1678 nextsym = sp; 1679 return; 1680 } 1681 for (sp--; sp >= gp->sy_first; sp--) { 1682 gp->sy_hfirst[sp->n_hash] = 0; 1683 gp->sy_used--; 1684 if (sp == saved) { 1685 nextsym = sp; 1686 return; 1687 } 1688 } 1689 } 1690 if (saved == 0) 1691 return; 1692 error(1, "symfree botch"); 1693 } 1694 1695 struct nlist ** 1696 slookup(s) 1697 char *s; 1698 { 1699 1700 cursym.n_un.n_name = s; 1701 cursym.n_type = N_EXT+N_UNDF; 1702 cursym.n_value = 0; 1703 return (lookup()); 1704 } 1705 1706 enter(hp) 1707 register struct nlist **hp; 1708 { 1709 register struct nlist *sp; 1710 1711 if (*hp==0) { 1712 if (hp < csymseg->sy_hfirst || hp >= csymseg->sy_hlast) 1713 error(1, "enter botch"); 1714 *hp = lastsym = sp = csymseg->sy_first + csymseg->sy_used; 1715 csymseg->sy_used++; 1716 sp->n_un.n_name = cursym.n_un.n_name; 1717 sp->n_type = cursym.n_type; 1718 sp->n_hash = hp - csymseg->sy_hfirst; 1719 sp->n_value = cursym.n_value; 1720 nextsym = lastsym + 1; 1721 return(1); 1722 } else { 1723 lastsym = *hp; 1724 return(0); 1725 } 1726 } 1727 1728 symx(sp) 1729 struct nlist *sp; 1730 { 1731 register struct symseg *gp; 1732 1733 if (sp == 0) 1734 return (0); 1735 for (gp = csymseg; gp >= symseg; gp--) 1736 /* <= is sloppy so nextsym will always work */ 1737 if (sp >= gp->sy_first && sp <= gp->sy_last) 1738 return ((gp - symseg) * NSYM + sp - gp->sy_first); 1739 error(1, "symx botch"); 1740 /*NOTREACHED*/ 1741 } 1742 1743 symreloc() 1744 { 1745 if(funding) return; 1746 switch (cursym.n_type & 017) { 1747 1748 case N_TEXT: 1749 case N_EXT+N_TEXT: 1750 cursym.n_value += ctrel; 1751 return; 1752 1753 case N_DATA: 1754 case N_EXT+N_DATA: 1755 cursym.n_value += cdrel; 1756 return; 1757 1758 case N_BSS: 1759 case N_EXT+N_BSS: 1760 cursym.n_value += cbrel; 1761 return; 1762 1763 case N_EXT+N_UNDF: 1764 return; 1765 1766 default: 1767 if (cursym.n_type&N_EXT) 1768 cursym.n_type = N_EXT+N_ABS; 1769 return; 1770 } 1771 } 1772 1773 error(n, s) 1774 char *s; 1775 { 1776 1777 if (errlev==0) 1778 printf("ld:"); 1779 if (filname) { 1780 printf("%s", filname); 1781 if (n != -1 && archdr.ar_name[0]) 1782 printf("(%s)", archdr.ar_name); 1783 printf(": "); 1784 } 1785 printf("%s\n", s); 1786 if (n == -1) 1787 return; 1788 if (n) 1789 delexit(); 1790 errlev = 2; 1791 } 1792 1793 readhdr(loc) 1794 off_t loc; 1795 { 1796 1797 dseek(&text, loc, (long)sizeof(filhdr)); 1798 mget((short *)&filhdr, sizeof(filhdr), &text); 1799 if (N_BADMAG(filhdr)) { 1800 if (filhdr.a_magic == OARMAG) 1801 error(1, "old archive"); 1802 error(1, "bad magic number"); 1803 } 1804 if (filhdr.a_text&01 || filhdr.a_data&01) 1805 error(1, "text/data size odd"); 1806 if (filhdr.a_magic == NMAGIC || filhdr.a_magic == ZMAGIC) { 1807 cdrel = -round(filhdr.a_text, PAGSIZ); 1808 cbrel = cdrel - filhdr.a_data; 1809 } else if (filhdr.a_magic == OMAGIC) { 1810 cdrel = -filhdr.a_text; 1811 cbrel = cdrel - filhdr.a_data; 1812 } else 1813 error(1, "bad format"); 1814 } 1815 1816 round(v, r) 1817 int v; 1818 u_long r; 1819 { 1820 1821 r--; 1822 v += r; 1823 v &= ~(long)r; 1824 return(v); 1825 } 1826 1827 #define NSAVETAB 8192 1828 char *savetab; 1829 int saveleft; 1830 1831 char * 1832 savestr(cp) 1833 register char *cp; 1834 { 1835 register int len; 1836 1837 len = strlen(cp) + 1; 1838 if (len > saveleft) { 1839 saveleft = NSAVETAB; 1840 if (len > saveleft) 1841 saveleft = len; 1842 savetab = (char *)malloc(saveleft); 1843 if (savetab == 0) 1844 error(1, "ran out of memory (savestr)"); 1845 } 1846 strncpy(savetab, cp, len); 1847 cp = savetab; 1848 savetab += len; 1849 saveleft -= len; 1850 return (cp); 1851 } 1852 1853 bopen(bp, off) 1854 struct biobuf *bp; 1855 { 1856 1857 bp->b_ptr = bp->b_buf; 1858 bp->b_nleft = BUFSIZ - off % BUFSIZ; 1859 bp->b_off = off; 1860 bp->b_link = biobufs; 1861 biobufs = bp; 1862 } 1863 1864 int bwrerror; 1865 1866 bwrite(p, cnt, bp) 1867 register char *p; 1868 register int cnt; 1869 register struct biobuf *bp; 1870 { 1871 register int put; 1872 register char *to; 1873 1874 top: 1875 if (cnt == 0) 1876 return; 1877 if (bp->b_nleft) { 1878 put = bp->b_nleft; 1879 if (put > cnt) 1880 put = cnt; 1881 bp->b_nleft -= put; 1882 to = bp->b_ptr; 1883 asm("movc3 r8,(r11),(r7)"); 1884 bp->b_ptr += put; 1885 p += put; 1886 cnt -= put; 1887 goto top; 1888 } 1889 if (cnt >= BUFSIZ) { 1890 if (bp->b_ptr != bp->b_buf) 1891 bflush1(bp); 1892 put = cnt - cnt % BUFSIZ; 1893 if (boffset != bp->b_off) 1894 lseek(biofd, bp->b_off, 0); 1895 if (write(biofd, p, put) != put) { 1896 bwrerror = 1; 1897 error(1, "output write error"); 1898 } 1899 bp->b_off += put; 1900 boffset = bp->b_off; 1901 p += put; 1902 cnt -= put; 1903 goto top; 1904 } 1905 bflush1(bp); 1906 goto top; 1907 } 1908 1909 bflush() 1910 { 1911 register struct biobuf *bp; 1912 1913 if (bwrerror) 1914 return; 1915 for (bp = biobufs; bp; bp = bp->b_link) 1916 bflush1(bp); 1917 } 1918 1919 bflush1(bp) 1920 register struct biobuf *bp; 1921 { 1922 register int cnt = bp->b_ptr - bp->b_buf; 1923 1924 if (cnt == 0) 1925 return; 1926 if (boffset != bp->b_off) 1927 lseek(biofd, bp->b_off, 0); 1928 if (write(biofd, bp->b_buf, cnt) != cnt) { 1929 bwrerror = 1; 1930 error(1, "output write error"); 1931 } 1932 bp->b_off += cnt; 1933 boffset = bp->b_off; 1934 bp->b_ptr = bp->b_buf; 1935 bp->b_nleft = BUFSIZ; 1936 } 1937 1938 bflushc(bp, c) 1939 register struct biobuf *bp; 1940 { 1941 1942 bflush1(bp); 1943 bputc(c, bp); 1944 } 1945