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