112671Ssam #ifndef lint 2*13591Swnj static char sccsid[] = "@(#)ld.c 4.9 07/01/83"; 312671Ssam #endif 46414Smckusic 5615Sbill /* 6898Sbill * ld - string table version for VAX 7615Sbill */ 8615Sbill 9615Sbill #include <sys/types.h> 10615Sbill #include <signal.h> 11615Sbill #include <stdio.h> 12615Sbill #include <ctype.h> 13650Sbill #include <ar.h> 14650Sbill #include <a.out.h> 15615Sbill #include <ranlib.h> 16*13591Swnj #include <sys/stat.h> 17615Sbill 18615Sbill /* 19615Sbill * Basic strategy: 20615Sbill * 21615Sbill * The loader takes a number of files and libraries as arguments. 22615Sbill * A first pass examines each file in turn. Normal files are 23615Sbill * unconditionally loaded, and the (external) symbols they define and require 24615Sbill * are noted in the symbol table. Libraries are searched, and the 25615Sbill * library members which define needed symbols are remembered 26615Sbill * in a special data structure so they can be selected on the second 27615Sbill * pass. Symbols defined and required by library members are also 28615Sbill * recorded. 29615Sbill * 30615Sbill * After the first pass, the loader knows the size of the basic text 31615Sbill * data, and bss segments from the sum of the sizes of the modules which 32615Sbill * were required. It has computed, for each ``common'' symbol, the 33615Sbill * maximum size of any reference to it, and these symbols are then assigned 34615Sbill * storage locations after their sizes are appropriately rounded. 35615Sbill * The loader now knows all sizes for the eventual output file, and 36615Sbill * can determine the final locations of external symbols before it 37615Sbill * begins a second pass. 38615Sbill * 39615Sbill * On the second pass each normal file and required library member 40615Sbill * is processed again. The symbol table for each such file is 41615Sbill * reread and relevant parts of it are placed in the output. The offsets 42615Sbill * in the local symbol table for externally defined symbols are recorded 43615Sbill * since relocation information refers to symbols in this way. 44615Sbill * Armed with all necessary information, the text and data segments 45615Sbill * are relocated and the result is placed in the output file, which 46615Sbill * is pasted together, ``in place'', by writing to it in several 47615Sbill * different places concurrently. 48615Sbill */ 49615Sbill 50615Sbill /* 51615Sbill * Internal data structures 52615Sbill * 53615Sbill * All internal data structures are segmented and dynamically extended. 54615Sbill * The basic structures hold 1103 (NSYM) symbols, ~~200 (NROUT) 55615Sbill * referenced library members, and 100 (NSYMPR) private (local) symbols 56615Sbill * per object module. For large programs and/or modules, these structures 57615Sbill * expand to be up to 40 (NSEG) times as large as this as necessary. 58615Sbill */ 59615Sbill #define NSEG 40 /* Number of segments, each data structure */ 60615Sbill #define NSYM 1103 /* Number of symbols per segment */ 61615Sbill #define NROUT 250 /* Number of library references per segment */ 62615Sbill #define NSYMPR 100 /* Number of private symbols per segment */ 63615Sbill 64615Sbill /* 65615Sbill * Structure describing each symbol table segment. 66615Sbill * Each segment has its own hash table. We record the first 67615Sbill * address in and first address beyond both the symbol and hash 68615Sbill * tables, for use in the routine symx and the lookup routine respectively. 69615Sbill * The symfree routine also understands this structure well as it used 70615Sbill * to back out symbols from modules we decide that we don't need in pass 1. 71615Sbill * 72615Sbill * Csymseg points to the current symbol table segment; 73615Sbill * csymseg->sy_first[csymseg->sy_used] is the next symbol slot to be allocated, 74615Sbill * (unless csymseg->sy_used == NSYM in which case we will allocate another 75615Sbill * symbol table segment first.) 76615Sbill */ 77615Sbill struct symseg { 78615Sbill struct nlist *sy_first; /* base of this alloc'ed segment */ 79615Sbill struct nlist *sy_last; /* end of this segment, for n_strx */ 80615Sbill int sy_used; /* symbols used in this seg */ 81615Sbill struct nlist **sy_hfirst; /* base of hash table, this seg */ 82615Sbill struct nlist **sy_hlast; /* end of hash table, this seg */ 83615Sbill } symseg[NSEG], *csymseg; 84615Sbill 85615Sbill /* 86615Sbill * The lookup routine uses quadratic rehash. Since a quadratic rehash 87615Sbill * only probes 1/2 of the buckets in the table, and since the hash 88615Sbill * table is segmented the same way the symbol table is, we make the 89615Sbill * hash table have twice as many buckets as there are symbol table slots 90615Sbill * in the segment. This guarantees that the quadratic rehash will never 91615Sbill * fail to find an empty bucket if the segment is not full and the 92615Sbill * symbol is not there. 93615Sbill */ 94615Sbill #define HSIZE (NSYM*2) 95615Sbill 96615Sbill /* 97615Sbill * Xsym converts symbol table indices (ala x) into symbol table pointers. 98615Sbill * Symx (harder, but never used in loops) inverts pointers into the symbol 99615Sbill * table into indices using the symseg[] structure. 100615Sbill */ 101615Sbill #define xsym(x) (symseg[(x)/NSYM].sy_first+((x)%NSYM)) 102615Sbill /* symx() is a function, defined below */ 103615Sbill 104615Sbill struct nlist cursym; /* current symbol */ 105615Sbill struct nlist *lastsym; /* last symbol entered */ 106615Sbill struct nlist *nextsym; /* next available symbol table entry */ 107615Sbill struct nlist *addsym; /* first sym defined during incr load */ 108615Sbill int nsym; /* pass2: number of local symbols in a.out */ 109615Sbill /* nsym + symx(nextsym) is the symbol table size during pass2 */ 110615Sbill 111615Sbill struct nlist **lookup(), **slookup(); 112650Sbill struct nlist *p_etext, *p_edata, *p_end, *entrypt; 113615Sbill 114615Sbill /* 115615Sbill * Definitions of segmentation for library member table. 116615Sbill * For each library we encounter on pass 1 we record pointers to all 117615Sbill * members which we will load on pass 2. These are recorded as offsets 118615Sbill * into the archive in the library member table. Libraries are 119615Sbill * separated in the table by the special offset value -1. 120615Sbill */ 121615Sbill off_t li_init[NROUT]; 122615Sbill struct libseg { 123615Sbill off_t *li_first; 124615Sbill int li_used; 125615Sbill int li_used2; 126615Sbill } libseg[NSEG] = { 127615Sbill li_init, 0, 0, 128615Sbill }, *clibseg = libseg; 129615Sbill 130615Sbill /* 131615Sbill * In processing each module on pass 2 we must relocate references 132615Sbill * relative to external symbols. These references are recorded 133615Sbill * in the relocation information as relative to local symbol numbers 134615Sbill * assigned to the external symbols when the module was created. 135615Sbill * Thus before relocating the module in pass 2 we create a table 136615Sbill * which maps these internal numbers to symbol table entries. 137615Sbill * A hash table is constructed, based on the local symbol table indices, 138615Sbill * for quick lookup of these symbols. 139615Sbill */ 140615Sbill #define LHSIZ 31 141615Sbill struct local { 142615Sbill int l_index; /* index to symbol in file */ 143615Sbill struct nlist *l_symbol; /* ptr to symbol table */ 144615Sbill struct local *l_link; /* hash link */ 145615Sbill } *lochash[LHSIZ], lhinit[NSYMPR]; 146615Sbill struct locseg { 147615Sbill struct local *lo_first; 148615Sbill int lo_used; 149615Sbill } locseg[NSEG] = { 150615Sbill lhinit, 0 151615Sbill }, *clocseg; 152615Sbill 153615Sbill /* 154615Sbill * Libraries are typically built with a table of contents, 155615Sbill * which is the first member of a library with special file 156615Sbill * name __.SYMDEF and contains a list of symbol names 157615Sbill * and with each symbol the offset of the library member which defines 158615Sbill * it. The loader uses this table to quickly tell which library members 159615Sbill * are (potentially) useful. The alternative, examining the symbol 160615Sbill * table of each library member, is painfully slow for large archives. 161615Sbill * 162615Sbill * See <ranlib.h> for the definition of the ranlib structure and an 163615Sbill * explanation of the __.SYMDEF file format. 164615Sbill */ 165615Sbill int tnum; /* number of symbols in table of contents */ 166615Sbill int ssiz; /* size of string table for table of contents */ 167615Sbill struct ranlib *tab; /* the table of contents (dynamically allocated) */ 168615Sbill char *tabstr; /* string table for table of contents */ 169615Sbill 170615Sbill /* 171615Sbill * We open each input file or library only once, but in pass2 we 172615Sbill * (historically) read from such a file at 2 different places at the 173615Sbill * same time. These structures are remnants from those days, 174650Sbill * and now serve only to catch ``Premature EOF''. 1756414Smckusic * In order to make I/O more efficient, we provide routines which 1766414Smckusic * work in hardware page sizes. The associated constants are defined 1776414Smckusic * as BLKSIZE, BLKSHIFT, and BLKMASK. 178615Sbill */ 1796414Smckusic #define BLKSIZE 1024 1806414Smckusic #define BLKSHIFT 10 1816414Smckusic #define BLKMASK (BLKSIZE - 1) 182615Sbill typedef struct { 183615Sbill short *fakeptr; 184615Sbill int bno; 185615Sbill int nibuf; 186615Sbill int nuser; 1876414Smckusic char buff[BLKSIZE]; 188615Sbill } PAGE; 189615Sbill 190615Sbill PAGE page[2]; 191615Sbill 192615Sbill struct { 193615Sbill short *fakeptr; 194615Sbill int bno; 195615Sbill int nibuf; 196615Sbill int nuser; 197615Sbill } fpage; 198615Sbill 199615Sbill typedef struct { 200615Sbill char *ptr; 201615Sbill int bno; 202615Sbill int nibuf; 203615Sbill long size; 204615Sbill long pos; 205615Sbill PAGE *pno; 206615Sbill } STREAM; 207615Sbill 208615Sbill STREAM text; 209615Sbill STREAM reloc; 210615Sbill 211615Sbill /* 212615Sbill * Header from the a.out and the archive it is from (if any). 213615Sbill */ 214615Sbill struct exec filhdr; 215615Sbill struct ar_hdr archdr; 216615Sbill #define OARMAG 0177545 217615Sbill 218615Sbill /* 219615Sbill * Options. 220615Sbill */ 221615Sbill int trace; 222615Sbill int xflag; /* discard local symbols */ 223615Sbill int Xflag; /* discard locals starting with 'L' */ 224615Sbill int Sflag; /* discard all except locals and globals*/ 225615Sbill int rflag; /* preserve relocation bits, don't define common */ 226615Sbill int arflag; /* original copy of rflag */ 227615Sbill int sflag; /* discard all symbols */ 228898Sbill int Mflag; /* print rudimentary load map */ 229615Sbill int nflag; /* pure procedure */ 230615Sbill int dflag; /* define common even with rflag */ 231650Sbill int zflag; /* demand paged */ 232615Sbill long hsize; /* size of hole at beginning of data to be squashed */ 233615Sbill int Aflag; /* doing incremental load */ 234650Sbill int Nflag; /* want impure a.out */ 235615Sbill int funding; /* reading fundamental file for incremental load */ 236898Sbill int yflag; /* number of symbols to be traced */ 237898Sbill char **ytab; /* the symbols */ 238615Sbill 239615Sbill /* 240615Sbill * These are the cumulative sizes, set in pass 1, which 241615Sbill * appear in the a.out header when the loader is finished. 242615Sbill */ 243615Sbill off_t tsize, dsize, bsize, trsize, drsize, ssize; 244615Sbill 245615Sbill /* 246615Sbill * Symbol relocation: c?rel is a scale factor which is 247615Sbill * added to an old relocation to convert it to new units; 248615Sbill * i.e. it is the difference between segment origins. 249650Sbill * (Thus if we are loading from a data segment which began at location 250650Sbill * 4 in a .o file into an a.out where it will be loaded starting at 251650Sbill * 1024, cdrel will be 1020.) 252615Sbill */ 253615Sbill long ctrel, cdrel, cbrel; 254615Sbill 255615Sbill /* 256650Sbill * Textbase is the start address of all text, 0 unless given by -T. 257615Sbill * Database is the base of all data, computed before and used during pass2. 258650Sbill */ 259650Sbill long textbase, database; 260650Sbill 261650Sbill /* 262615Sbill * The base addresses for the loaded text, data and bss from the 263615Sbill * current module during pass2 are given by torigin, dorigin and borigin. 264615Sbill */ 265615Sbill long torigin, dorigin, borigin; 266615Sbill 267615Sbill /* 268615Sbill * Errlev is nonzero when errors have occured. 269615Sbill * Delarg is an implicit argument to the routine delexit 270615Sbill * which is called on error. We do ``delarg = errlev'' before normal 271615Sbill * exits, and only if delarg is 0 (i.e. errlev was 0) do we make the 272615Sbill * result file executable. 273615Sbill */ 274615Sbill int errlev; 275615Sbill int delarg = 4; 276615Sbill 277615Sbill /* 278615Sbill * The biobuf structure and associated routines are used to write 279615Sbill * into one file at several places concurrently. Calling bopen 280615Sbill * with a biobuf structure sets it up to write ``biofd'' starting 281615Sbill * at the specified offset. You can then use ``bwrite'' and/or ``bputc'' 282615Sbill * to stuff characters in the stream, much like ``fwrite'' and ``fputc''. 283615Sbill * Calling bflush drains all the buffers and MUST be done before exit. 284615Sbill */ 285615Sbill struct biobuf { 286615Sbill short b_nleft; /* Number free spaces left in b_buf */ 287615Sbill /* Initialize to be less than BUFSIZ initially, to boundary align in file */ 288615Sbill char *b_ptr; /* Next place to stuff characters */ 289615Sbill char b_buf[BUFSIZ]; /* The buffer itself */ 290615Sbill off_t b_off; /* Current file offset */ 291615Sbill struct biobuf *b_link; /* Link in chain for bflush() */ 292615Sbill } *biobufs; 293615Sbill #define bputc(c,b) ((b)->b_nleft ? (--(b)->b_nleft, *(b)->b_ptr++ = (c)) \ 294615Sbill : bflushc(b, c)) 295615Sbill int biofd; 296615Sbill off_t boffset; 297615Sbill struct biobuf *tout, *dout, *trout, *drout, *sout, *strout; 298615Sbill 299615Sbill /* 300615Sbill * Offset is the current offset in the string file. 301615Sbill * Its initial value reflects the fact that we will 302615Sbill * eventually stuff the size of the string table at the 303615Sbill * beginning of the string table (i.e. offset itself!). 304615Sbill */ 305615Sbill off_t offset = sizeof (off_t); 306615Sbill 307615Sbill int ofilfnd; /* -o given; otherwise move l.out to a.out */ 308615Sbill char *ofilename = "l.out"; 3093606Ssklower int ofilemode; /* respect umask even for unsucessful ld's */ 310615Sbill int infil; /* current input file descriptor */ 311615Sbill char *filname; /* and its name */ 312615Sbill 313615Sbill /* 314615Sbill * Base of the string table of the current module (pass1 and pass2). 315615Sbill */ 316615Sbill char *curstr; 317615Sbill 31812671Ssam /* 31912671Ssam * System software page size, as returned by getpagesize. 32012671Ssam */ 32112671Ssam int pagesize; 32212671Ssam 323615Sbill char get(); 324615Sbill int delexit(); 325615Sbill char *savestr(); 326615Sbill 327615Sbill main(argc, argv) 328615Sbill char **argv; 329615Sbill { 330615Sbill register int c, i; 331615Sbill int num; 332615Sbill register char *ap, **p; 333615Sbill char save; 334615Sbill 335650Sbill if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 336615Sbill signal(SIGINT, delexit); 337650Sbill signal(SIGTERM, delexit); 338650Sbill } 339615Sbill if (argc == 1) 340615Sbill exit(4); 341615Sbill p = argv+1; 34212671Ssam pagesize = getpagesize(); 343615Sbill 344650Sbill /* 345650Sbill * Scan files once to find where symbols are defined. 346650Sbill */ 347615Sbill for (c=1; c<argc; c++) { 348615Sbill if (trace) 349615Sbill printf("%s:\n", *p); 350615Sbill filname = 0; 351615Sbill ap = *p++; 352615Sbill if (*ap != '-') { 353615Sbill load1arg(ap); 354615Sbill continue; 355615Sbill } 356615Sbill for (i=1; ap[i]; i++) switch (ap[i]) { 357615Sbill 358615Sbill case 'o': 359615Sbill if (++c >= argc) 360615Sbill error(1, "-o where?"); 361615Sbill ofilename = *p++; 362615Sbill ofilfnd++; 363615Sbill continue; 364615Sbill case 'u': 365615Sbill case 'e': 366615Sbill if (++c >= argc) 367615Sbill error(1, "-u or -c: arg missing"); 368615Sbill enter(slookup(*p++)); 369615Sbill if (ap[i]=='e') 370615Sbill entrypt = lastsym; 371615Sbill continue; 372615Sbill case 'H': 373615Sbill if (++c >= argc) 374615Sbill error(1, "-H: arg missing"); 375615Sbill if (tsize!=0) 376615Sbill error(1, "-H: too late, some text already loaded"); 377615Sbill hsize = atoi(*p++); 378615Sbill continue; 379615Sbill case 'A': 380615Sbill if (++c >= argc) 381615Sbill error(1, "-A: arg missing"); 382615Sbill if (Aflag) 383615Sbill error(1, "-A: only one base file allowed"); 384615Sbill Aflag = 1; 385615Sbill nflag = 0; 386615Sbill funding = 1; 387615Sbill load1arg(*p++); 388615Sbill trsize = drsize = tsize = dsize = bsize = 0; 389615Sbill ctrel = cdrel = cbrel = 0; 390615Sbill funding = 0; 391615Sbill addsym = nextsym; 392615Sbill continue; 393615Sbill case 'D': 394615Sbill if (++c >= argc) 395615Sbill error(1, "-D: arg missing"); 396615Sbill num = htoi(*p++); 397615Sbill if (dsize > num) 398615Sbill error(1, "-D: too small"); 399615Sbill dsize = num; 400615Sbill continue; 401615Sbill case 'T': 402615Sbill if (++c >= argc) 403615Sbill error(1, "-T: arg missing"); 404615Sbill if (tsize!=0) 405615Sbill error(1, "-T: too late, some text already loaded"); 406615Sbill textbase = htoi(*p++); 407615Sbill continue; 408615Sbill case 'l': 409615Sbill save = ap[--i]; 410615Sbill ap[i]='-'; 411615Sbill load1arg(&ap[i]); 412615Sbill ap[i]=save; 413615Sbill goto next; 414898Sbill case 'M': 415898Sbill Mflag++; 416898Sbill continue; 417615Sbill case 'x': 418615Sbill xflag++; 419615Sbill continue; 420615Sbill case 'X': 421615Sbill Xflag++; 422615Sbill continue; 423615Sbill case 'S': 424615Sbill Sflag++; 425615Sbill continue; 426615Sbill case 'r': 427615Sbill rflag++; 428615Sbill arflag++; 429615Sbill continue; 430615Sbill case 's': 431615Sbill sflag++; 432615Sbill xflag++; 433615Sbill continue; 434615Sbill case 'n': 435615Sbill nflag++; 436650Sbill Nflag = zflag = 0; 437615Sbill continue; 438615Sbill case 'N': 439650Sbill Nflag++; 440650Sbill nflag = zflag = 0; 441615Sbill continue; 442615Sbill case 'd': 443615Sbill dflag++; 444615Sbill continue; 445615Sbill case 'i': 446615Sbill printf("ld: -i ignored\n"); 447615Sbill continue; 448615Sbill case 't': 449615Sbill trace++; 450615Sbill continue; 451898Sbill case 'y': 452898Sbill if (ap[i+1] == 0) 453898Sbill error(1, "-y: symbol name missing"); 454898Sbill if (yflag == 0) { 455898Sbill ytab = (char **)calloc(argc, sizeof (char **)); 456898Sbill if (ytab == 0) 457898Sbill error(1, "ran out of memory (-y)"); 458898Sbill } 459898Sbill ytab[yflag++] = &ap[i+1]; 460898Sbill goto next; 461615Sbill case 'z': 462615Sbill zflag++; 463650Sbill Nflag = nflag = 0; 464615Sbill continue; 465615Sbill default: 466615Sbill filname = savestr("-x"); /* kludge */ 467615Sbill filname[1] = ap[i]; /* kludge */ 468615Sbill archdr.ar_name[0] = 0; /* kludge */ 469615Sbill error(1, "bad flag"); 470615Sbill } 471615Sbill next: 472615Sbill ; 473615Sbill } 474650Sbill if (rflag == 0 && Nflag == 0 && nflag == 0) 475650Sbill zflag++; 476615Sbill endload(argc, argv); 477615Sbill exit(0); 478615Sbill } 479615Sbill 480615Sbill /* 481615Sbill * Convert a ascii string which is a hex number. 482615Sbill * Used by -T and -D options. 483615Sbill */ 484615Sbill htoi(p) 485615Sbill register char *p; 486615Sbill { 487615Sbill register int c, n; 488615Sbill 489615Sbill n = 0; 490615Sbill while (c = *p++) { 491615Sbill n <<= 4; 492615Sbill if (isdigit(c)) 493615Sbill n += c - '0'; 494615Sbill else if (c >= 'a' && c <= 'f') 495615Sbill n += 10 + (c - 'a'); 496615Sbill else if (c >= 'A' && c <= 'F') 497615Sbill n += 10 + (c - 'A'); 498615Sbill else 499615Sbill error(1, "badly formed hex number"); 500615Sbill } 501615Sbill return (n); 502615Sbill } 503615Sbill 504615Sbill delexit() 505615Sbill { 5069332Smckusick struct stat stbuf; 5079332Smckusick long size; 5089332Smckusick char c = 0; 509615Sbill 510615Sbill bflush(); 511615Sbill unlink("l.out"); 512615Sbill if (delarg==0 && Aflag==0) 5133606Ssklower chmod(ofilename, ofilemode); 5149332Smckusick /* 5159332Smckusick * We have to insure that the last block of the data segment 5169332Smckusick * is allocated a full BLKSIZE block. If the underlying 5179332Smckusick * file system allocates frags that are smaller than BLKSIZE, 5189332Smckusick * a full zero filled BLKSIZE block needs to be allocated so 5199332Smckusick * that when it is demand paged, the paged in block will be 5209332Smckusick * appropriately filled with zeros. 5219332Smckusick */ 5229332Smckusick fstat(biofd, &stbuf); 5239332Smckusick size = round(stbuf.st_size, BLKSIZE); 52410640Smckusick if (!rflag && size > stbuf.st_size) { 5259332Smckusick lseek(biofd, size - 1, 0); 5269332Smckusick write(biofd, &c, 1); 5279332Smckusick } 528615Sbill exit (delarg); 529615Sbill } 530615Sbill 531615Sbill endload(argc, argv) 532615Sbill int argc; 533615Sbill char **argv; 534615Sbill { 535615Sbill register int c, i; 536615Sbill long dnum; 537615Sbill register char *ap, **p; 538615Sbill 539615Sbill clibseg = libseg; 540615Sbill filname = 0; 541615Sbill middle(); 542615Sbill setupout(); 543615Sbill p = argv+1; 544615Sbill for (c=1; c<argc; c++) { 545615Sbill ap = *p++; 546615Sbill if (trace) 547615Sbill printf("%s:\n", ap); 548615Sbill if (*ap != '-') { 549615Sbill load2arg(ap); 550615Sbill continue; 551615Sbill } 552615Sbill for (i=1; ap[i]; i++) switch (ap[i]) { 553615Sbill 554615Sbill case 'D': 555615Sbill dnum = htoi(*p); 556615Sbill if (dorigin < dnum) 557615Sbill while (dorigin < dnum) 558615Sbill bputc(0, dout), dorigin++; 559615Sbill /* fall into ... */ 560615Sbill case 'T': 561615Sbill case 'u': 562615Sbill case 'e': 563615Sbill case 'o': 564615Sbill case 'H': 565615Sbill ++c; 566615Sbill ++p; 567615Sbill /* fall into ... */ 568615Sbill default: 569615Sbill continue; 570615Sbill case 'A': 571615Sbill funding = 1; 572615Sbill load2arg(*p++); 573615Sbill funding = 0; 574615Sbill c++; 575615Sbill continue; 576898Sbill case 'y': 577898Sbill goto next; 578615Sbill case 'l': 579615Sbill ap[--i]='-'; 580615Sbill load2arg(&ap[i]); 581615Sbill goto next; 582615Sbill } 583615Sbill next: 584615Sbill ; 585615Sbill } 586615Sbill finishout(); 587615Sbill } 588615Sbill 589615Sbill /* 590615Sbill * Scan file to find defined symbols. 591615Sbill */ 592615Sbill load1arg(cp) 593615Sbill register char *cp; 594615Sbill { 595615Sbill register struct ranlib *tp; 596615Sbill off_t nloc; 597898Sbill int kind; 598615Sbill 599898Sbill kind = getfile(cp); 600898Sbill if (Mflag) 601898Sbill printf("%s\n", filname); 602898Sbill switch (kind) { 603615Sbill 604615Sbill /* 605615Sbill * Plain file. 606615Sbill */ 607615Sbill case 0: 608615Sbill load1(0, 0L); 609615Sbill break; 610615Sbill 611615Sbill /* 612615Sbill * Archive without table of contents. 613615Sbill * (Slowly) process each member. 614615Sbill */ 615615Sbill case 1: 616898Sbill error(-1, 617898Sbill "warning: archive has no table of contents; add one using ranlib(1)"); 618615Sbill nloc = SARMAG; 619615Sbill while (step(nloc)) 620615Sbill nloc += sizeof(archdr) + 621615Sbill round(atol(archdr.ar_size), sizeof (short)); 622615Sbill break; 623615Sbill 624615Sbill /* 625615Sbill * Archive with table of contents. 626615Sbill * Read the table of contents and its associated string table. 627615Sbill * Pass through the library resolving symbols until nothing changes 628615Sbill * for an entire pass (i.e. you can get away with backward references 629615Sbill * when there is a table of contents!) 630615Sbill */ 631615Sbill case 2: 632615Sbill nloc = SARMAG + sizeof (archdr); 633615Sbill dseek(&text, nloc, sizeof (tnum)); 634615Sbill mget((char *)&tnum, sizeof (tnum), &text); 635615Sbill nloc += sizeof (tnum); 636615Sbill tab = (struct ranlib *)malloc(tnum); 637615Sbill if (tab == 0) 638615Sbill error(1, "ran out of memory (toc)"); 639615Sbill dseek(&text, nloc, tnum); 640615Sbill mget((char *)tab, tnum, &text); 641615Sbill nloc += tnum; 642615Sbill tnum /= sizeof (struct ranlib); 643615Sbill dseek(&text, nloc, sizeof (ssiz)); 644615Sbill mget((char *)&ssiz, sizeof (ssiz), &text); 645615Sbill nloc += sizeof (ssiz); 646615Sbill tabstr = (char *)malloc(ssiz); 647615Sbill if (tabstr == 0) 648615Sbill error(1, "ran out of memory (tocstr)"); 649615Sbill dseek(&text, nloc, ssiz); 650615Sbill mget((char *)tabstr, ssiz, &text); 651615Sbill for (tp = &tab[tnum]; --tp >= tab;) { 652615Sbill if (tp->ran_un.ran_strx < 0 || 653615Sbill tp->ran_un.ran_strx >= ssiz) 654615Sbill error(1, "mangled archive table of contents"); 655615Sbill tp->ran_un.ran_name = tabstr + tp->ran_un.ran_strx; 656615Sbill } 657615Sbill while (ldrand()) 658615Sbill continue; 659615Sbill cfree((char *)tab); 660615Sbill cfree(tabstr); 661615Sbill nextlibp(-1); 662615Sbill break; 663615Sbill 664615Sbill /* 665615Sbill * Table of contents is out of date, so search 666615Sbill * as a normal library (but skip the __.SYMDEF file). 667615Sbill */ 668615Sbill case 3: 669898Sbill error(-1, 670898Sbill "warning: table of contents for archive is out of date; rerun ranlib(1)"); 671615Sbill nloc = SARMAG; 672615Sbill do 673615Sbill nloc += sizeof(archdr) + 674615Sbill round(atol(archdr.ar_size), sizeof(short)); 675615Sbill while (step(nloc)); 676615Sbill break; 677615Sbill } 678615Sbill close(infil); 679615Sbill } 680615Sbill 681615Sbill /* 682615Sbill * Advance to the next archive member, which 683615Sbill * is at offset nloc in the archive. If the member 684615Sbill * is useful, record its location in the liblist structure 685615Sbill * for use in pass2. Mark the end of the archive in libilst with a -1. 686615Sbill */ 687615Sbill step(nloc) 688615Sbill off_t nloc; 689615Sbill { 690615Sbill 691615Sbill dseek(&text, nloc, (long) sizeof archdr); 692615Sbill if (text.size <= 0) { 693615Sbill nextlibp(-1); 694615Sbill return (0); 695615Sbill } 696615Sbill getarhdr(); 697615Sbill if (load1(1, nloc + (sizeof archdr))) 698615Sbill nextlibp(nloc); 699615Sbill return (1); 700615Sbill } 701615Sbill 702615Sbill /* 703615Sbill * Record the location of a useful archive member. 704615Sbill * Recording -1 marks the end of files from an archive. 705615Sbill * The liblist data structure is dynamically extended here. 706615Sbill */ 707615Sbill nextlibp(val) 708615Sbill off_t val; 709615Sbill { 710615Sbill 711615Sbill if (clibseg->li_used == NROUT) { 712615Sbill if (++clibseg == &libseg[NSEG]) 713615Sbill error(1, "too many files loaded from libraries"); 714615Sbill clibseg->li_first = (off_t *)malloc(NROUT * sizeof (off_t)); 715615Sbill if (clibseg->li_first == 0) 716615Sbill error(1, "ran out of memory (nextlibp)"); 717615Sbill } 718615Sbill clibseg->li_first[clibseg->li_used++] = val; 719898Sbill if (val != -1 && Mflag) 720898Sbill printf("\t%s\n", archdr.ar_name); 721615Sbill } 722615Sbill 723615Sbill /* 724615Sbill * One pass over an archive with a table of contents. 725615Sbill * Remember the number of symbols currently defined, 726615Sbill * then call step on members which look promising (i.e. 727615Sbill * that define a symbol which is currently externally undefined). 728615Sbill * Indicate to our caller whether this process netted any more symbols. 729615Sbill */ 730615Sbill ldrand() 731615Sbill { 732615Sbill register struct nlist *sp, **hp; 733615Sbill register struct ranlib *tp, *tplast; 734615Sbill off_t loc; 735615Sbill int nsymt = symx(nextsym); 736615Sbill 737615Sbill tplast = &tab[tnum-1]; 738615Sbill for (tp = tab; tp <= tplast; tp++) { 739615Sbill if ((hp = slookup(tp->ran_un.ran_name)) == 0) 740615Sbill continue; 741615Sbill sp = *hp; 742615Sbill if (sp->n_type != N_EXT+N_UNDF) 743615Sbill continue; 744615Sbill step(tp->ran_off); 745615Sbill loc = tp->ran_off; 746615Sbill while (tp < tplast && (tp+1)->ran_off == loc) 747615Sbill tp++; 748615Sbill } 749615Sbill return (symx(nextsym) != nsymt); 750615Sbill } 751615Sbill 752615Sbill /* 753615Sbill * Examine a single file or archive member on pass 1. 754615Sbill */ 755615Sbill load1(libflg, loc) 756615Sbill off_t loc; 757615Sbill { 758615Sbill register struct nlist *sp; 759615Sbill struct nlist *savnext; 760615Sbill int ndef, nlocal, type, size, nsymt; 761615Sbill register int i; 762615Sbill off_t maxoff; 763615Sbill struct stat stb; 764615Sbill 765615Sbill readhdr(loc); 766615Sbill if (filhdr.a_syms == 0) { 767615Sbill if (filhdr.a_text+filhdr.a_data == 0) 768615Sbill return (0); 769615Sbill error(1, "no namelist"); 770615Sbill } 771615Sbill if (libflg) 772615Sbill maxoff = atol(archdr.ar_size); 773615Sbill else { 774615Sbill fstat(infil, &stb); 775615Sbill maxoff = stb.st_size; 776615Sbill } 777615Sbill if (N_STROFF(filhdr) + sizeof (off_t) >= maxoff) 778615Sbill error(1, "too small (old format .o?)"); 779615Sbill ctrel = tsize; cdrel += dsize; cbrel += bsize; 780615Sbill ndef = 0; 781615Sbill nlocal = sizeof(cursym); 782615Sbill savnext = nextsym; 783615Sbill loc += N_SYMOFF(filhdr); 784615Sbill dseek(&text, loc, filhdr.a_syms); 785615Sbill dseek(&reloc, loc + filhdr.a_syms, sizeof(off_t)); 786615Sbill mget(&size, sizeof (size), &reloc); 787615Sbill dseek(&reloc, loc + filhdr.a_syms+sizeof (off_t), size-sizeof (off_t)); 788615Sbill curstr = (char *)malloc(size); 789615Sbill if (curstr == NULL) 790615Sbill error(1, "no space for string table"); 791615Sbill mget(curstr+sizeof(off_t), size-sizeof(off_t), &reloc); 792615Sbill while (text.size > 0) { 793615Sbill mget((char *)&cursym, sizeof(struct nlist), &text); 794615Sbill if (cursym.n_un.n_strx) { 795615Sbill if (cursym.n_un.n_strx<sizeof(size) || 796615Sbill cursym.n_un.n_strx>=size) 797615Sbill error(1, "bad string table index (pass 1)"); 798615Sbill cursym.n_un.n_name = curstr + cursym.n_un.n_strx; 799615Sbill } 800615Sbill type = cursym.n_type; 801615Sbill if ((type&N_EXT)==0) { 802615Sbill if (Xflag==0 || cursym.n_un.n_name[0]!='L' || 803615Sbill type & N_STAB) 804615Sbill nlocal += sizeof cursym; 805615Sbill continue; 806615Sbill } 807615Sbill symreloc(); 808615Sbill if (enter(lookup())) 809615Sbill continue; 810615Sbill if ((sp = lastsym)->n_type != N_EXT+N_UNDF) 811615Sbill continue; 812615Sbill if (cursym.n_type == N_EXT+N_UNDF) { 813615Sbill if (cursym.n_value > sp->n_value) 814615Sbill sp->n_value = cursym.n_value; 815615Sbill continue; 816615Sbill } 817615Sbill if (sp->n_value != 0 && cursym.n_type == N_EXT+N_TEXT) 818615Sbill continue; 819615Sbill ndef++; 820615Sbill sp->n_type = cursym.n_type; 821615Sbill sp->n_value = cursym.n_value; 822615Sbill } 823615Sbill if (libflg==0 || ndef) { 824615Sbill tsize += filhdr.a_text; 825615Sbill dsize += round(filhdr.a_data, sizeof (long)); 826615Sbill bsize += round(filhdr.a_bss, sizeof (long)); 827615Sbill ssize += nlocal; 828615Sbill trsize += filhdr.a_trsize; 829615Sbill drsize += filhdr.a_drsize; 830615Sbill if (funding) 831615Sbill textbase = (*slookup("_end"))->n_value; 832615Sbill nsymt = symx(nextsym); 833615Sbill for (i = symx(savnext); i < nsymt; i++) { 834615Sbill sp = xsym(i); 835615Sbill sp->n_un.n_name = savestr(sp->n_un.n_name); 836615Sbill } 837615Sbill free(curstr); 838615Sbill return (1); 839615Sbill } 840615Sbill /* 841615Sbill * No symbols defined by this library member. 842615Sbill * Rip out the hash table entries and reset the symbol table. 843615Sbill */ 844615Sbill symfree(savnext); 845615Sbill free(curstr); 846615Sbill return(0); 847615Sbill } 848615Sbill 849615Sbill middle() 850615Sbill { 851615Sbill register struct nlist *sp; 852615Sbill long csize, t, corigin, ocsize; 853615Sbill int nund, rnd; 854615Sbill char s; 855615Sbill register int i; 856615Sbill int nsymt; 857615Sbill 858615Sbill torigin = 0; 859615Sbill dorigin = 0; 860615Sbill borigin = 0; 861615Sbill 862615Sbill p_etext = *slookup("_etext"); 863615Sbill p_edata = *slookup("_edata"); 864615Sbill p_end = *slookup("_end"); 865615Sbill /* 866615Sbill * If there are any undefined symbols, save the relocation bits. 867615Sbill */ 868615Sbill nsymt = symx(nextsym); 869615Sbill if (rflag==0) { 870615Sbill for (i = 0; i < nsymt; i++) { 871615Sbill sp = xsym(i); 872615Sbill if (sp->n_type==N_EXT+N_UNDF && sp->n_value==0 && 873650Sbill sp!=p_end && sp!=p_edata && sp!=p_etext) { 874615Sbill rflag++; 875615Sbill dflag = 0; 876615Sbill break; 877615Sbill } 878615Sbill } 879615Sbill } 880615Sbill if (rflag) 881615Sbill sflag = zflag = 0; 882615Sbill /* 883615Sbill * Assign common locations. 884615Sbill */ 885615Sbill csize = 0; 886615Sbill if (!Aflag) 887615Sbill addsym = symseg[0].sy_first; 888615Sbill database = round(tsize+textbase, 88912671Ssam (nflag||zflag? pagesize : sizeof (long))); 890615Sbill database += hsize; 891615Sbill if (dflag || rflag==0) { 892615Sbill ldrsym(p_etext, tsize, N_EXT+N_TEXT); 893615Sbill ldrsym(p_edata, dsize, N_EXT+N_DATA); 894615Sbill ldrsym(p_end, bsize, N_EXT+N_BSS); 895615Sbill for (i = symx(addsym); i < nsymt; i++) { 896615Sbill sp = xsym(i); 897615Sbill if ((s=sp->n_type)==N_EXT+N_UNDF && 898615Sbill (t = sp->n_value)!=0) { 899615Sbill if (t >= sizeof (double)) 900615Sbill rnd = sizeof (double); 901615Sbill else if (t >= sizeof (long)) 902615Sbill rnd = sizeof (long); 903615Sbill else 904615Sbill rnd = sizeof (short); 905615Sbill csize = round(csize, rnd); 906615Sbill sp->n_value = csize; 907615Sbill sp->n_type = N_EXT+N_COMM; 908615Sbill ocsize = csize; 909615Sbill csize += t; 910615Sbill } 911615Sbill if (s&N_EXT && (s&N_TYPE)==N_UNDF && s&N_STAB) { 912615Sbill sp->n_value = ocsize; 913615Sbill sp->n_type = (s&N_STAB) | (N_EXT+N_COMM); 914615Sbill } 915615Sbill } 916615Sbill } 917615Sbill /* 918615Sbill * Now set symbols to their final value 919615Sbill */ 920615Sbill csize = round(csize, sizeof (long)); 921615Sbill torigin = textbase; 922615Sbill dorigin = database; 923615Sbill corigin = dorigin + dsize; 924615Sbill borigin = corigin + csize; 925615Sbill nund = 0; 926615Sbill nsymt = symx(nextsym); 927615Sbill for (i = symx(addsym); i<nsymt; i++) { 928615Sbill sp = xsym(i); 929615Sbill switch (sp->n_type & (N_TYPE+N_EXT)) { 930615Sbill 931615Sbill case N_EXT+N_UNDF: 9322369Skre if (arflag == 0) 9332369Skre errlev |= 01; 934615Sbill if ((arflag==0 || dflag) && sp->n_value==0) { 935650Sbill if (sp==p_end || sp==p_etext || sp==p_edata) 936650Sbill continue; 937615Sbill if (nund==0) 938615Sbill printf("Undefined:\n"); 939615Sbill nund++; 940615Sbill printf("%s\n", sp->n_un.n_name); 941615Sbill } 942615Sbill continue; 943615Sbill case N_EXT+N_ABS: 944615Sbill default: 945615Sbill continue; 946615Sbill case N_EXT+N_TEXT: 947615Sbill sp->n_value += torigin; 948615Sbill continue; 949615Sbill case N_EXT+N_DATA: 950615Sbill sp->n_value += dorigin; 951615Sbill continue; 952615Sbill case N_EXT+N_BSS: 953615Sbill sp->n_value += borigin; 954615Sbill continue; 955615Sbill case N_EXT+N_COMM: 956615Sbill sp->n_type = (sp->n_type & N_STAB) | (N_EXT+N_BSS); 957615Sbill sp->n_value += corigin; 958615Sbill continue; 959615Sbill } 960615Sbill } 961615Sbill if (sflag || xflag) 962615Sbill ssize = 0; 963615Sbill bsize += csize; 964615Sbill nsym = ssize / (sizeof cursym); 965615Sbill if (Aflag) { 966615Sbill fixspec(p_etext,torigin); 967615Sbill fixspec(p_edata,dorigin); 968615Sbill fixspec(p_end,borigin); 969615Sbill } 970615Sbill } 971615Sbill 972615Sbill fixspec(sym,offset) 973615Sbill struct nlist *sym; 974615Sbill long offset; 975615Sbill { 976615Sbill 977615Sbill if(symx(sym) < symx(addsym) && sym!=0) 978615Sbill sym->n_value += offset; 979615Sbill } 980615Sbill 981615Sbill ldrsym(sp, val, type) 982615Sbill register struct nlist *sp; 983615Sbill long val; 984615Sbill { 985615Sbill 986615Sbill if (sp == 0) 987615Sbill return; 988615Sbill if ((sp->n_type != N_EXT+N_UNDF || sp->n_value) && !Aflag) { 989615Sbill printf("%s: ", sp->n_un.n_name); 990615Sbill error(0, "user attempt to redfine loader-defined symbol"); 991615Sbill return; 992615Sbill } 993615Sbill sp->n_type = type; 994615Sbill sp->n_value = val; 995615Sbill } 996615Sbill 997615Sbill off_t wroff; 998615Sbill struct biobuf toutb; 999615Sbill 1000615Sbill setupout() 1001615Sbill { 1002615Sbill int bss; 1003898Sbill extern char *sys_errlist[]; 1004898Sbill extern int errno; 1005615Sbill 10063606Ssklower ofilemode = 0777 & ~umask(0); 10073606Ssklower biofd = creat(ofilename, 0666 & ofilemode); 1008898Sbill if (biofd < 0) { 1009898Sbill filname = ofilename; /* kludge */ 1010898Sbill archdr.ar_name[0] = 0; /* kludge */ 1011898Sbill error(1, sys_errlist[errno]); /* kludge */ 10123606Ssklower } else { 10133606Ssklower struct stat mybuf; /* kls kludge */ 10143606Ssklower fstat(biofd, &mybuf); /* suppose file exists, wrong*/ 10153606Ssklower if(mybuf.st_mode & 0111) { /* mode, ld fails? */ 10163606Ssklower chmod(ofilename, mybuf.st_mode & 0666); 10173606Ssklower ofilemode = mybuf.st_mode; 10183606Ssklower } 1019898Sbill } 1020615Sbill tout = &toutb; 1021615Sbill bopen(tout, 0); 1022615Sbill filhdr.a_magic = nflag ? NMAGIC : (zflag ? ZMAGIC : OMAGIC); 1023615Sbill filhdr.a_text = nflag ? tsize : 102412671Ssam round(tsize, zflag ? pagesize : sizeof (long)); 102512671Ssam filhdr.a_data = zflag ? round(dsize, pagesize) : dsize; 1026615Sbill bss = bsize - (filhdr.a_data - dsize); 1027615Sbill if (bss < 0) 1028615Sbill bss = 0; 1029615Sbill filhdr.a_bss = bss; 1030615Sbill filhdr.a_trsize = trsize; 1031615Sbill filhdr.a_drsize = drsize; 1032615Sbill filhdr.a_syms = sflag? 0: (ssize + (sizeof cursym)*symx(nextsym)); 1033615Sbill if (entrypt) { 1034615Sbill if (entrypt->n_type!=N_EXT+N_TEXT) 1035615Sbill error(0, "entry point not in text"); 1036615Sbill else 1037615Sbill filhdr.a_entry = entrypt->n_value; 1038615Sbill } else 1039615Sbill filhdr.a_entry = 0; 1040615Sbill filhdr.a_trsize = (rflag ? trsize:0); 1041615Sbill filhdr.a_drsize = (rflag ? drsize:0); 1042615Sbill bwrite((char *)&filhdr, sizeof (filhdr), tout); 1043615Sbill if (zflag) { 1044615Sbill bflush1(tout); 1045615Sbill biobufs = 0; 104612671Ssam bopen(tout, pagesize); 1047615Sbill } 1048615Sbill wroff = N_TXTOFF(filhdr) + filhdr.a_text; 1049615Sbill outb(&dout, filhdr.a_data); 1050615Sbill if (rflag) { 1051615Sbill outb(&trout, filhdr.a_trsize); 1052615Sbill outb(&drout, filhdr.a_drsize); 1053615Sbill } 1054615Sbill if (sflag==0 || xflag==0) { 1055615Sbill outb(&sout, filhdr.a_syms); 1056615Sbill wroff += sizeof (offset); 1057615Sbill outb(&strout, 0); 1058615Sbill } 1059615Sbill } 1060615Sbill 1061615Sbill outb(bp, inc) 1062615Sbill register struct biobuf **bp; 1063615Sbill { 1064615Sbill 1065615Sbill *bp = (struct biobuf *)malloc(sizeof (struct biobuf)); 1066615Sbill if (*bp == 0) 1067615Sbill error(1, "ran out of memory (outb)"); 1068615Sbill bopen(*bp, wroff); 1069615Sbill wroff += inc; 1070615Sbill } 1071615Sbill 1072615Sbill load2arg(acp) 1073615Sbill char *acp; 1074615Sbill { 1075615Sbill register char *cp; 1076615Sbill off_t loc; 1077615Sbill 1078615Sbill cp = acp; 1079615Sbill if (getfile(cp) == 0) { 1080615Sbill while (*cp) 1081615Sbill cp++; 1082615Sbill while (cp >= acp && *--cp != '/'); 1083615Sbill mkfsym(++cp); 1084615Sbill load2(0L); 1085615Sbill } else { /* scan archive members referenced */ 1086615Sbill for (;;) { 1087615Sbill if (clibseg->li_used2 == clibseg->li_used) { 1088615Sbill if (clibseg->li_used < NROUT) 1089615Sbill error(1, "libseg botch"); 1090615Sbill clibseg++; 1091615Sbill } 1092615Sbill loc = clibseg->li_first[clibseg->li_used2++]; 1093615Sbill if (loc == -1) 1094615Sbill break; 1095615Sbill dseek(&text, loc, (long)sizeof(archdr)); 1096615Sbill getarhdr(); 1097615Sbill mkfsym(archdr.ar_name); 1098615Sbill load2(loc + (long)sizeof(archdr)); 1099615Sbill } 1100615Sbill } 1101615Sbill close(infil); 1102615Sbill } 1103615Sbill 1104615Sbill load2(loc) 1105615Sbill long loc; 1106615Sbill { 1107615Sbill int size; 1108615Sbill register struct nlist *sp; 1109615Sbill register struct local *lp; 1110615Sbill register int symno, i; 1111615Sbill int type; 1112615Sbill 1113615Sbill readhdr(loc); 1114650Sbill if (!funding) { 1115615Sbill ctrel = torigin; 1116615Sbill cdrel += dorigin; 1117615Sbill cbrel += borigin; 1118615Sbill } 1119615Sbill /* 1120615Sbill * Reread the symbol table, recording the numbering 1121615Sbill * of symbols for fixing external references. 1122615Sbill */ 1123615Sbill for (i = 0; i < LHSIZ; i++) 1124615Sbill lochash[i] = 0; 1125615Sbill clocseg = locseg; 1126615Sbill clocseg->lo_used = 0; 1127615Sbill symno = -1; 1128615Sbill loc += N_TXTOFF(filhdr); 1129615Sbill dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1130615Sbill filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms, sizeof(off_t)); 1131615Sbill mget(&size, sizeof(size), &text); 1132615Sbill dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1133615Sbill filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms+sizeof(off_t), 1134615Sbill size - sizeof(off_t)); 1135615Sbill curstr = (char *)malloc(size); 1136615Sbill if (curstr == NULL) 1137615Sbill error(1, "out of space reading string table (pass 2)"); 1138615Sbill mget(curstr+sizeof(off_t), size-sizeof(off_t), &text); 1139615Sbill dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1140615Sbill filhdr.a_trsize+filhdr.a_drsize, filhdr.a_syms); 1141615Sbill while (text.size > 0) { 1142615Sbill symno++; 1143615Sbill mget((char *)&cursym, sizeof(struct nlist), &text); 1144615Sbill if (cursym.n_un.n_strx) { 1145615Sbill if (cursym.n_un.n_strx<sizeof(size) || 1146615Sbill cursym.n_un.n_strx>=size) 1147615Sbill error(1, "bad string table index (pass 2)"); 1148615Sbill cursym.n_un.n_name = curstr + cursym.n_un.n_strx; 1149615Sbill } 1150615Sbill /* inline expansion of symreloc() */ 1151615Sbill switch (cursym.n_type & 017) { 1152615Sbill 1153615Sbill case N_TEXT: 1154615Sbill case N_EXT+N_TEXT: 1155615Sbill cursym.n_value += ctrel; 1156615Sbill break; 1157615Sbill case N_DATA: 1158615Sbill case N_EXT+N_DATA: 1159615Sbill cursym.n_value += cdrel; 1160615Sbill break; 1161615Sbill case N_BSS: 1162615Sbill case N_EXT+N_BSS: 1163615Sbill cursym.n_value += cbrel; 1164615Sbill break; 1165615Sbill case N_EXT+N_UNDF: 1166615Sbill break; 1167615Sbill default: 1168615Sbill if (cursym.n_type&N_EXT) 1169615Sbill cursym.n_type = N_EXT+N_ABS; 1170615Sbill } 1171615Sbill /* end inline expansion of symreloc() */ 1172615Sbill type = cursym.n_type; 1173898Sbill if (yflag && cursym.n_un.n_name) 1174898Sbill for (i = 0; i < yflag; i++) 1175898Sbill /* fast check for 2d character! */ 1176898Sbill if (ytab[i][1] == cursym.n_un.n_name[1] && 1177898Sbill !strcmp(ytab[i], cursym.n_un.n_name)) { 1178898Sbill tracesym(); 1179898Sbill break; 1180898Sbill } 1181615Sbill if ((type&N_EXT) == 0) { 1182615Sbill if (!sflag&&!xflag&& 1183615Sbill (!Xflag||cursym.n_un.n_name[0]!='L'||type&N_STAB)) 1184615Sbill symwrite(&cursym, sout); 1185615Sbill continue; 1186615Sbill } 1187615Sbill if (funding) 1188615Sbill continue; 1189615Sbill if ((sp = *lookup()) == 0) 1190615Sbill error(1, "internal error: symbol not found"); 1191615Sbill if (cursym.n_type == N_EXT+N_UNDF) { 1192615Sbill if (clocseg->lo_used == NSYMPR) { 1193615Sbill if (++clocseg == &locseg[NSEG]) 1194615Sbill error(1, "local symbol overflow"); 1195615Sbill clocseg->lo_used = 0; 1196615Sbill } 1197615Sbill if (clocseg->lo_first == 0) { 1198615Sbill clocseg->lo_first = (struct local *) 1199615Sbill malloc(NSYMPR * sizeof (struct local)); 1200615Sbill if (clocseg->lo_first == 0) 1201615Sbill error(1, "out of memory (clocseg)"); 1202615Sbill } 1203615Sbill lp = &clocseg->lo_first[clocseg->lo_used++]; 1204615Sbill lp->l_index = symno; 1205615Sbill lp->l_symbol = sp; 1206615Sbill lp->l_link = lochash[symno % LHSIZ]; 1207615Sbill lochash[symno % LHSIZ] = lp; 1208615Sbill continue; 1209615Sbill } 1210615Sbill if (cursym.n_type & N_STAB) 1211615Sbill continue; 1212615Sbill if (cursym.n_type!=sp->n_type || cursym.n_value!=sp->n_value) { 1213615Sbill printf("%s: ", cursym.n_un.n_name); 1214615Sbill error(0, "multiply defined"); 1215615Sbill } 1216615Sbill } 1217615Sbill if (funding) 1218615Sbill return; 1219615Sbill dseek(&text, loc, filhdr.a_text); 1220615Sbill dseek(&reloc, loc+filhdr.a_text+filhdr.a_data, filhdr.a_trsize); 1221650Sbill load2td(ctrel, torigin - textbase, tout, trout); 1222615Sbill dseek(&text, loc+filhdr.a_text, filhdr.a_data); 1223615Sbill dseek(&reloc, loc+filhdr.a_text+filhdr.a_data+filhdr.a_trsize, 1224615Sbill filhdr.a_drsize); 1225650Sbill load2td(cdrel, dorigin - database, dout, drout); 1226615Sbill while (filhdr.a_data & (sizeof(long)-1)) { 1227615Sbill bputc(0, dout); 1228615Sbill filhdr.a_data++; 1229615Sbill } 1230615Sbill torigin += filhdr.a_text; 12311752Sbill dorigin += round(filhdr.a_data, sizeof (long)); 12321752Sbill borigin += round(filhdr.a_bss, sizeof (long)); 1233615Sbill free(curstr); 1234615Sbill } 1235615Sbill 1236898Sbill struct tynames { 1237898Sbill int ty_value; 1238898Sbill char *ty_name; 1239898Sbill } tynames[] = { 1240898Sbill N_UNDF, "undefined", 1241898Sbill N_ABS, "absolute", 1242898Sbill N_TEXT, "text", 1243898Sbill N_DATA, "data", 1244898Sbill N_BSS, "bss", 1245898Sbill N_COMM, "common", 1246898Sbill 0, 0, 1247898Sbill }; 1248898Sbill 1249898Sbill tracesym() 1250898Sbill { 1251898Sbill register struct tynames *tp; 1252898Sbill 1253898Sbill if (cursym.n_type & N_STAB) 1254898Sbill return; 1255898Sbill printf("%s", filname); 1256898Sbill if (archdr.ar_name[0]) 1257898Sbill printf("(%s)", archdr.ar_name); 1258898Sbill printf(": "); 1259898Sbill if ((cursym.n_type&N_TYPE) == N_UNDF && cursym.n_value) { 1260898Sbill printf("definition of common %s size %d\n", 1261898Sbill cursym.n_un.n_name, cursym.n_value); 1262898Sbill return; 1263898Sbill } 1264898Sbill for (tp = tynames; tp->ty_name; tp++) 1265898Sbill if (tp->ty_value == (cursym.n_type&N_TYPE)) 1266898Sbill break; 1267898Sbill printf((cursym.n_type&N_TYPE) ? "definition of" : "reference to"); 1268898Sbill if (cursym.n_type&N_EXT) 1269898Sbill printf(" external"); 1270898Sbill if (tp->ty_name) 1271898Sbill printf(" %s", tp->ty_name); 1272898Sbill printf(" %s\n", cursym.n_un.n_name); 1273898Sbill } 1274898Sbill 1275650Sbill /* 1276650Sbill * This routine relocates the single text or data segment argument. 1277650Sbill * Offsets from external symbols are resolved by adding the value 1278650Sbill * of the external symbols. Non-external reference are updated to account 1279650Sbill * for the relative motion of the segments (ctrel, cdrel, ...). If 1280650Sbill * a relocation was pc-relative, then we update it to reflect the 1281650Sbill * change in the positioning of the segments by adding the displacement 1282650Sbill * of the referenced segment and subtracting the displacement of the 1283650Sbill * current segment (creloc). 1284650Sbill * 1285650Sbill * If we are saving the relocation information, then we increase 1286650Sbill * each relocation datum address by our base position in the new segment. 1287650Sbill */ 1288650Sbill load2td(creloc, position, b1, b2) 1289650Sbill long creloc, offset; 1290615Sbill struct biobuf *b1, *b2; 1291615Sbill { 1292615Sbill register struct nlist *sp; 1293615Sbill register struct local *lp; 1294615Sbill long tw; 1295615Sbill register struct relocation_info *rp, *rpend; 1296615Sbill struct relocation_info *relp; 1297615Sbill char *codep; 1298615Sbill register char *cp; 1299615Sbill int relsz, codesz; 1300615Sbill 1301615Sbill relsz = reloc.size; 1302615Sbill relp = (struct relocation_info *)malloc(relsz); 1303615Sbill codesz = text.size; 1304615Sbill codep = (char *)malloc(codesz); 1305615Sbill if (relp == 0 || codep == 0) 1306615Sbill error(1, "out of memory (load2td)"); 1307615Sbill mget((char *)relp, relsz, &reloc); 1308615Sbill rpend = &relp[relsz / sizeof (struct relocation_info)]; 1309615Sbill mget(codep, codesz, &text); 1310615Sbill for (rp = relp; rp < rpend; rp++) { 1311615Sbill cp = codep + rp->r_address; 1312650Sbill /* 1313650Sbill * Pick up previous value at location to be relocated. 1314650Sbill */ 1315615Sbill switch (rp->r_length) { 1316615Sbill 1317615Sbill case 0: /* byte */ 1318615Sbill tw = *cp; 1319615Sbill break; 1320615Sbill 1321615Sbill case 1: /* word */ 1322615Sbill tw = *(short *)cp; 1323615Sbill break; 1324615Sbill 1325615Sbill case 2: /* long */ 1326615Sbill tw = *(long *)cp; 1327615Sbill break; 1328615Sbill 1329615Sbill default: 1330615Sbill error(1, "load2td botch: bad length"); 1331615Sbill } 1332650Sbill /* 1333650Sbill * If relative to an external which is defined, 1334650Sbill * resolve to a simpler kind of reference in the 1335650Sbill * result file. If the external is undefined, just 1336650Sbill * convert the symbol number to the number of the 1337650Sbill * symbol in the result file and leave it undefined. 1338650Sbill */ 1339615Sbill if (rp->r_extern) { 1340650Sbill /* 1341650Sbill * Search the hash table which maps local 1342650Sbill * symbol numbers to symbol tables entries 1343650Sbill * in the new a.out file. 1344650Sbill */ 1345615Sbill lp = lochash[rp->r_symbolnum % LHSIZ]; 1346615Sbill while (lp->l_index != rp->r_symbolnum) { 1347615Sbill lp = lp->l_link; 1348615Sbill if (lp == 0) 1349615Sbill error(1, "local symbol botch"); 1350615Sbill } 1351615Sbill sp = lp->l_symbol; 1352615Sbill if (sp->n_type == N_EXT+N_UNDF) 1353615Sbill rp->r_symbolnum = nsym+symx(sp); 1354615Sbill else { 1355615Sbill rp->r_symbolnum = sp->n_type & N_TYPE; 1356615Sbill tw += sp->n_value; 1357615Sbill rp->r_extern = 0; 1358615Sbill } 1359615Sbill } else switch (rp->r_symbolnum & N_TYPE) { 1360650Sbill /* 1361650Sbill * Relocation is relative to the loaded position 1362650Sbill * of another segment. Update by the change in position 1363650Sbill * of that segment. 1364650Sbill */ 1365615Sbill case N_TEXT: 1366615Sbill tw += ctrel; 1367615Sbill break; 1368615Sbill case N_DATA: 1369615Sbill tw += cdrel; 1370615Sbill break; 1371615Sbill case N_BSS: 1372615Sbill tw += cbrel; 1373615Sbill break; 1374615Sbill case N_ABS: 1375615Sbill break; 1376615Sbill default: 1377615Sbill error(1, "relocation format botch (symbol type))"); 1378615Sbill } 1379650Sbill /* 1380650Sbill * Relocation is pc relative, so decrease the relocation 1381650Sbill * by the amount the current segment is displaced. 1382650Sbill * (E.g if we are a relative reference to a text location 1383650Sbill * from data space, we added the increase in the text address 1384650Sbill * above, and subtract the increase in our (data) address 1385650Sbill * here, leaving the net change the relative change in the 1386650Sbill * positioning of our text and data segments.) 1387650Sbill */ 1388615Sbill if (rp->r_pcrel) 1389615Sbill tw -= creloc; 1390650Sbill /* 1391650Sbill * Put the value back in the segment, 1392650Sbill * while checking for overflow. 1393650Sbill */ 1394615Sbill switch (rp->r_length) { 1395615Sbill 1396615Sbill case 0: /* byte */ 1397615Sbill if (tw < -128 || tw > 127) 1398615Sbill error(0, "byte displacement overflow"); 1399615Sbill *cp = tw; 1400615Sbill break; 1401615Sbill case 1: /* word */ 1402615Sbill if (tw < -32768 || tw > 32767) 1403615Sbill error(0, "word displacement overflow"); 1404615Sbill *(short *)cp = tw; 1405615Sbill break; 1406615Sbill case 2: /* long */ 1407615Sbill *(long *)cp = tw; 1408615Sbill break; 1409615Sbill } 1410650Sbill /* 1411650Sbill * If we are saving relocation information, 1412650Sbill * we must convert the address in the segment from 1413650Sbill * the old .o file into an address in the segment in 1414650Sbill * the new a.out, by adding the position of our 1415650Sbill * segment in the new larger segment. 1416650Sbill */ 1417615Sbill if (rflag) 1418650Sbill rp->r_address += position; 1419615Sbill } 1420615Sbill bwrite(codep, codesz, b1); 1421615Sbill if (rflag) 1422615Sbill bwrite(relp, relsz, b2); 1423615Sbill cfree((char *)relp); 1424615Sbill cfree(codep); 1425615Sbill } 1426615Sbill 1427615Sbill finishout() 1428615Sbill { 1429615Sbill register int i; 1430615Sbill int nsymt; 1431615Sbill 1432615Sbill if (sflag==0) { 1433615Sbill nsymt = symx(nextsym); 1434615Sbill for (i = 0; i < nsymt; i++) 1435615Sbill symwrite(xsym(i), sout); 1436615Sbill bwrite(&offset, sizeof offset, sout); 1437615Sbill } 1438615Sbill if (!ofilfnd) { 1439615Sbill unlink("a.out"); 1440898Sbill if (link("l.out", "a.out") < 0) 1441898Sbill error(1, "cannot move l.out to a.out"); 1442615Sbill ofilename = "a.out"; 1443615Sbill } 1444615Sbill delarg = errlev; 1445615Sbill delexit(); 1446615Sbill } 1447615Sbill 1448615Sbill mkfsym(s) 1449615Sbill char *s; 1450615Sbill { 1451615Sbill 1452615Sbill if (sflag || xflag) 1453615Sbill return; 1454615Sbill cursym.n_un.n_name = s; 1455615Sbill cursym.n_type = N_TEXT; 1456615Sbill cursym.n_value = torigin; 1457615Sbill symwrite(&cursym, sout); 1458615Sbill } 1459615Sbill 1460615Sbill getarhdr() 1461615Sbill { 1462615Sbill register char *cp; 1463615Sbill 1464615Sbill mget((char *)&archdr, sizeof archdr, &text); 1465615Sbill for (cp=archdr.ar_name; cp<&archdr.ar_name[sizeof(archdr.ar_name)];) 1466615Sbill if (*cp++ == ' ') { 1467615Sbill cp[-1] = 0; 1468615Sbill return; 1469615Sbill } 1470615Sbill } 1471615Sbill 1472615Sbill mget(loc, n, sp) 1473615Sbill register STREAM *sp; 1474615Sbill register char *loc; 1475615Sbill { 1476615Sbill register char *p; 1477615Sbill register int take; 1478615Sbill 1479615Sbill top: 1480615Sbill if (n == 0) 1481615Sbill return; 1482615Sbill if (sp->size && sp->nibuf) { 1483615Sbill p = sp->ptr; 1484615Sbill take = sp->size; 1485615Sbill if (take > sp->nibuf) 1486615Sbill take = sp->nibuf; 1487615Sbill if (take > n) 1488615Sbill take = n; 1489615Sbill n -= take; 1490615Sbill sp->size -= take; 1491615Sbill sp->nibuf -= take; 1492615Sbill sp->pos += take; 1493615Sbill do 1494615Sbill *loc++ = *p++; 1495615Sbill while (--take > 0); 1496615Sbill sp->ptr = p; 1497615Sbill goto top; 1498615Sbill } 1499615Sbill if (n > BUFSIZ) { 15006414Smckusic take = n - n % BLKSIZE; 15016414Smckusic lseek(infil, (sp->bno+1)*BLKSIZE, 0); 1502615Sbill if (take > sp->size || read(infil, loc, take) != take) 1503615Sbill error(1, "premature EOF"); 1504615Sbill loc += take; 1505615Sbill n -= take; 1506615Sbill sp->size -= take; 1507615Sbill sp->pos += take; 15086414Smckusic dseek(sp, (sp->bno+1+take/BLKSIZE)*BLKSIZE, -1); 1509615Sbill goto top; 1510615Sbill } 1511615Sbill *loc++ = get(sp); 1512615Sbill --n; 1513615Sbill goto top; 1514615Sbill } 1515615Sbill 1516615Sbill symwrite(sp, bp) 1517615Sbill struct nlist *sp; 1518615Sbill struct biobuf *bp; 1519615Sbill { 1520615Sbill register int len; 1521615Sbill register char *str; 1522615Sbill 1523615Sbill str = sp->n_un.n_name; 1524615Sbill if (str) { 1525615Sbill sp->n_un.n_strx = offset; 1526615Sbill len = strlen(str) + 1; 1527615Sbill bwrite(str, len, strout); 1528615Sbill offset += len; 1529615Sbill } 1530615Sbill bwrite(sp, sizeof (*sp), bp); 1531615Sbill sp->n_un.n_name = str; 1532615Sbill } 1533615Sbill 1534615Sbill dseek(sp, loc, s) 1535615Sbill register STREAM *sp; 1536615Sbill long loc, s; 1537615Sbill { 1538615Sbill register PAGE *p; 1539615Sbill register b, o; 1540615Sbill int n; 1541615Sbill 15426414Smckusic b = loc>>BLKSHIFT; 15436414Smckusic o = loc&BLKMASK; 1544615Sbill if (o&01) 1545615Sbill error(1, "loader error; odd offset"); 1546615Sbill --sp->pno->nuser; 1547615Sbill if ((p = &page[0])->bno!=b && (p = &page[1])->bno!=b) 1548615Sbill if (p->nuser==0 || (p = &page[0])->nuser==0) { 1549615Sbill if (page[0].nuser==0 && page[1].nuser==0) 1550615Sbill if (page[0].bno < page[1].bno) 1551615Sbill p = &page[0]; 1552615Sbill p->bno = b; 15536414Smckusic lseek(infil, loc & ~(long)BLKMASK, 0); 1554615Sbill if ((n = read(infil, p->buff, sizeof(p->buff))) < 0) 1555615Sbill n = 0; 1556615Sbill p->nibuf = n; 1557615Sbill } else 1558615Sbill error(1, "botch: no pages"); 1559615Sbill ++p->nuser; 1560615Sbill sp->bno = b; 1561615Sbill sp->pno = p; 1562615Sbill if (s != -1) {sp->size = s; sp->pos = 0;} 1563615Sbill sp->ptr = (char *)(p->buff + o); 1564615Sbill if ((sp->nibuf = p->nibuf-o) <= 0) 1565615Sbill sp->size = 0; 1566615Sbill } 1567615Sbill 1568615Sbill char 1569615Sbill get(asp) 1570615Sbill STREAM *asp; 1571615Sbill { 1572615Sbill register STREAM *sp; 1573615Sbill 1574615Sbill sp = asp; 1575615Sbill if ((sp->nibuf -= sizeof(char)) < 0) { 15766414Smckusic dseek(sp, ((long)(sp->bno+1)<<BLKSHIFT), (long)-1); 1577615Sbill sp->nibuf -= sizeof(char); 1578615Sbill } 1579615Sbill if ((sp->size -= sizeof(char)) <= 0) { 1580615Sbill if (sp->size < 0) 1581615Sbill error(1, "premature EOF"); 1582615Sbill ++fpage.nuser; 1583615Sbill --sp->pno->nuser; 1584615Sbill sp->pno = (PAGE *) &fpage; 1585615Sbill } 1586615Sbill sp->pos += sizeof(char); 1587615Sbill return(*sp->ptr++); 1588615Sbill } 1589615Sbill 1590615Sbill getfile(acp) 1591615Sbill char *acp; 1592615Sbill { 1593615Sbill register char *cp; 1594615Sbill register int c; 1595615Sbill char arcmag[SARMAG+1]; 1596615Sbill struct stat stb; 1597615Sbill 1598615Sbill cp = acp; 1599615Sbill infil = -1; 1600615Sbill archdr.ar_name[0] = '\0'; 1601615Sbill filname = cp; 1602615Sbill if (cp[0]=='-' && cp[1]=='l') { 1603898Sbill char *locfilname = "/usr/local/lib/libxxxxxxxxxxxxxxx"; 1604615Sbill if(cp[2] == '\0') 1605615Sbill cp = "-la"; 1606898Sbill filname = "/usr/lib/libxxxxxxxxxxxxxxx"; 1607615Sbill for(c=0; cp[c+2]; c++) { 1608615Sbill filname[c+12] = cp[c+2]; 1609615Sbill locfilname[c+18] = cp[c+2]; 1610615Sbill } 1611615Sbill filname[c+12] = locfilname[c+18] = '.'; 1612615Sbill filname[c+13] = locfilname[c+19] = 'a'; 1613615Sbill filname[c+14] = locfilname[c+20] = '\0'; 1614615Sbill if ((infil = open(filname+4, 0)) >= 0) { 1615615Sbill filname += 4; 1616615Sbill } else if ((infil = open(filname, 0)) < 0) { 1617615Sbill filname = locfilname; 1618615Sbill } 1619615Sbill } 1620615Sbill if (infil == -1 && (infil = open(filname, 0)) < 0) 1621615Sbill error(1, "cannot open"); 1622615Sbill page[0].bno = page[1].bno = -1; 1623615Sbill page[0].nuser = page[1].nuser = 0; 1624615Sbill text.pno = reloc.pno = (PAGE *) &fpage; 1625615Sbill fpage.nuser = 2; 1626615Sbill dseek(&text, 0L, SARMAG); 1627615Sbill if (text.size <= 0) 1628615Sbill error(1, "premature EOF"); 1629615Sbill mget((char *)arcmag, SARMAG, &text); 1630615Sbill arcmag[SARMAG] = 0; 1631615Sbill if (strcmp(arcmag, ARMAG)) 1632615Sbill return (0); 1633615Sbill dseek(&text, SARMAG, sizeof archdr); 1634615Sbill if(text.size <= 0) 1635615Sbill return (1); 1636615Sbill getarhdr(); 1637615Sbill if (strncmp(archdr.ar_name, "__.SYMDEF", sizeof(archdr.ar_name)) != 0) 1638615Sbill return (1); 1639615Sbill fstat(infil, &stb); 1640615Sbill return (stb.st_mtime > atol(archdr.ar_date) ? 3 : 2); 1641615Sbill } 1642615Sbill 1643615Sbill struct nlist ** 1644615Sbill lookup() 1645615Sbill { 1646615Sbill register int sh; 1647615Sbill register struct nlist **hp; 1648615Sbill register char *cp, *cp1; 1649615Sbill register struct symseg *gp; 1650615Sbill register int i; 1651615Sbill 1652615Sbill sh = 0; 1653615Sbill for (cp = cursym.n_un.n_name; *cp;) 1654615Sbill sh = (sh<<1) + *cp++; 1655615Sbill sh = (sh & 0x7fffffff) % HSIZE; 1656615Sbill for (gp = symseg; gp < &symseg[NSEG]; gp++) { 1657615Sbill if (gp->sy_first == 0) { 1658615Sbill gp->sy_first = (struct nlist *) 1659615Sbill calloc(NSYM, sizeof (struct nlist)); 1660615Sbill gp->sy_hfirst = (struct nlist **) 1661615Sbill calloc(HSIZE, sizeof (struct nlist *)); 1662615Sbill if (gp->sy_first == 0 || gp->sy_hfirst == 0) 1663615Sbill error(1, "ran out of space for symbol table"); 1664615Sbill gp->sy_last = gp->sy_first + NSYM; 1665615Sbill gp->sy_hlast = gp->sy_hfirst + HSIZE; 1666615Sbill } 1667615Sbill if (gp > csymseg) 1668615Sbill csymseg = gp; 1669615Sbill hp = gp->sy_hfirst + sh; 1670615Sbill i = 1; 1671615Sbill do { 1672615Sbill if (*hp == 0) { 1673615Sbill if (gp->sy_used == NSYM) 1674615Sbill break; 1675615Sbill return (hp); 1676615Sbill } 1677615Sbill cp1 = (*hp)->n_un.n_name; 1678615Sbill for (cp = cursym.n_un.n_name; *cp == *cp1++;) 1679615Sbill if (*cp++ == 0) 1680615Sbill return (hp); 1681615Sbill hp += i; 1682615Sbill i += 2; 1683615Sbill if (hp >= gp->sy_hlast) 1684615Sbill hp -= HSIZE; 1685615Sbill } while (i < HSIZE); 1686615Sbill if (i > HSIZE) 1687615Sbill error(1, "hash table botch"); 1688615Sbill } 1689615Sbill error(1, "symbol table overflow"); 1690615Sbill /*NOTREACHED*/ 1691615Sbill } 1692615Sbill 1693615Sbill symfree(saved) 1694615Sbill struct nlist *saved; 1695615Sbill { 1696615Sbill register struct symseg *gp; 1697615Sbill register struct nlist *sp; 1698615Sbill 1699615Sbill for (gp = csymseg; gp >= symseg; gp--, csymseg--) { 1700615Sbill sp = gp->sy_first + gp->sy_used; 1701615Sbill if (sp == saved) { 1702615Sbill nextsym = sp; 1703615Sbill return; 1704615Sbill } 1705615Sbill for (sp--; sp >= gp->sy_first; sp--) { 1706615Sbill gp->sy_hfirst[sp->n_hash] = 0; 1707615Sbill gp->sy_used--; 1708615Sbill if (sp == saved) { 1709615Sbill nextsym = sp; 1710615Sbill return; 1711615Sbill } 1712615Sbill } 1713615Sbill } 1714615Sbill if (saved == 0) 1715615Sbill return; 1716615Sbill error(1, "symfree botch"); 1717615Sbill } 1718615Sbill 1719615Sbill struct nlist ** 1720615Sbill slookup(s) 1721615Sbill char *s; 1722615Sbill { 1723615Sbill 1724615Sbill cursym.n_un.n_name = s; 1725615Sbill cursym.n_type = N_EXT+N_UNDF; 1726615Sbill cursym.n_value = 0; 1727615Sbill return (lookup()); 1728615Sbill } 1729615Sbill 1730615Sbill enter(hp) 1731615Sbill register struct nlist **hp; 1732615Sbill { 1733615Sbill register struct nlist *sp; 1734615Sbill 1735615Sbill if (*hp==0) { 1736615Sbill if (hp < csymseg->sy_hfirst || hp >= csymseg->sy_hlast) 1737615Sbill error(1, "enter botch"); 1738615Sbill *hp = lastsym = sp = csymseg->sy_first + csymseg->sy_used; 1739615Sbill csymseg->sy_used++; 1740615Sbill sp->n_un.n_name = cursym.n_un.n_name; 1741615Sbill sp->n_type = cursym.n_type; 1742615Sbill sp->n_hash = hp - csymseg->sy_hfirst; 1743615Sbill sp->n_value = cursym.n_value; 1744615Sbill nextsym = lastsym + 1; 1745615Sbill return(1); 1746615Sbill } else { 1747615Sbill lastsym = *hp; 1748615Sbill return(0); 1749615Sbill } 1750615Sbill } 1751615Sbill 1752615Sbill symx(sp) 1753615Sbill struct nlist *sp; 1754615Sbill { 1755615Sbill register struct symseg *gp; 1756615Sbill 1757615Sbill if (sp == 0) 1758615Sbill return (0); 1759615Sbill for (gp = csymseg; gp >= symseg; gp--) 1760615Sbill /* <= is sloppy so nextsym will always work */ 1761615Sbill if (sp >= gp->sy_first && sp <= gp->sy_last) 1762615Sbill return ((gp - symseg) * NSYM + sp - gp->sy_first); 1763615Sbill error(1, "symx botch"); 1764615Sbill /*NOTREACHED*/ 1765615Sbill } 1766615Sbill 1767615Sbill symreloc() 1768615Sbill { 1769615Sbill if(funding) return; 1770615Sbill switch (cursym.n_type & 017) { 1771615Sbill 1772615Sbill case N_TEXT: 1773615Sbill case N_EXT+N_TEXT: 1774615Sbill cursym.n_value += ctrel; 1775615Sbill return; 1776615Sbill 1777615Sbill case N_DATA: 1778615Sbill case N_EXT+N_DATA: 1779615Sbill cursym.n_value += cdrel; 1780615Sbill return; 1781615Sbill 1782615Sbill case N_BSS: 1783615Sbill case N_EXT+N_BSS: 1784615Sbill cursym.n_value += cbrel; 1785615Sbill return; 1786615Sbill 1787615Sbill case N_EXT+N_UNDF: 1788615Sbill return; 1789615Sbill 1790615Sbill default: 1791615Sbill if (cursym.n_type&N_EXT) 1792615Sbill cursym.n_type = N_EXT+N_ABS; 1793615Sbill return; 1794615Sbill } 1795615Sbill } 1796615Sbill 1797615Sbill error(n, s) 1798615Sbill char *s; 1799615Sbill { 1800898Sbill 1801615Sbill if (errlev==0) 1802615Sbill printf("ld:"); 1803615Sbill if (filname) { 1804615Sbill printf("%s", filname); 1805615Sbill if (n != -1 && archdr.ar_name[0]) 1806615Sbill printf("(%s)", archdr.ar_name); 1807615Sbill printf(": "); 1808615Sbill } 1809615Sbill printf("%s\n", s); 1810615Sbill if (n == -1) 1811615Sbill return; 1812615Sbill if (n) 1813615Sbill delexit(); 1814615Sbill errlev = 2; 1815615Sbill } 1816615Sbill 1817615Sbill readhdr(loc) 1818615Sbill off_t loc; 1819615Sbill { 1820615Sbill 1821615Sbill dseek(&text, loc, (long)sizeof(filhdr)); 1822615Sbill mget((short *)&filhdr, sizeof(filhdr), &text); 1823615Sbill if (N_BADMAG(filhdr)) { 1824615Sbill if (filhdr.a_magic == OARMAG) 1825615Sbill error(1, "old archive"); 1826615Sbill error(1, "bad magic number"); 1827615Sbill } 1828615Sbill if (filhdr.a_text&01 || filhdr.a_data&01) 1829615Sbill error(1, "text/data size odd"); 1830615Sbill if (filhdr.a_magic == NMAGIC || filhdr.a_magic == ZMAGIC) { 183112671Ssam cdrel = -round(filhdr.a_text, pagesize); 1832615Sbill cbrel = cdrel - filhdr.a_data; 1833615Sbill } else if (filhdr.a_magic == OMAGIC) { 1834615Sbill cdrel = -filhdr.a_text; 1835615Sbill cbrel = cdrel - filhdr.a_data; 1836615Sbill } else 1837615Sbill error(1, "bad format"); 1838615Sbill } 1839615Sbill 1840615Sbill round(v, r) 1841615Sbill int v; 1842615Sbill u_long r; 1843615Sbill { 1844615Sbill 1845615Sbill r--; 1846615Sbill v += r; 1847615Sbill v &= ~(long)r; 1848615Sbill return(v); 1849615Sbill } 1850615Sbill 1851615Sbill #define NSAVETAB 8192 1852615Sbill char *savetab; 1853615Sbill int saveleft; 1854615Sbill 1855615Sbill char * 1856615Sbill savestr(cp) 1857615Sbill register char *cp; 1858615Sbill { 1859615Sbill register int len; 1860615Sbill 1861615Sbill len = strlen(cp) + 1; 1862615Sbill if (len > saveleft) { 1863615Sbill saveleft = NSAVETAB; 1864615Sbill if (len > saveleft) 1865615Sbill saveleft = len; 1866615Sbill savetab = (char *)malloc(saveleft); 1867615Sbill if (savetab == 0) 1868615Sbill error(1, "ran out of memory (savestr)"); 1869615Sbill } 1870615Sbill strncpy(savetab, cp, len); 1871615Sbill cp = savetab; 1872615Sbill savetab += len; 1873615Sbill saveleft -= len; 1874615Sbill return (cp); 1875615Sbill } 1876615Sbill 1877615Sbill bopen(bp, off) 1878615Sbill struct biobuf *bp; 1879615Sbill { 1880615Sbill 1881615Sbill bp->b_ptr = bp->b_buf; 1882615Sbill bp->b_nleft = BUFSIZ - off % BUFSIZ; 1883615Sbill bp->b_off = off; 1884615Sbill bp->b_link = biobufs; 1885615Sbill biobufs = bp; 1886615Sbill } 1887615Sbill 1888615Sbill int bwrerror; 1889615Sbill 1890615Sbill bwrite(p, cnt, bp) 1891615Sbill register char *p; 1892615Sbill register int cnt; 1893615Sbill register struct biobuf *bp; 1894615Sbill { 1895615Sbill register int put; 1896615Sbill register char *to; 1897615Sbill 1898615Sbill top: 1899615Sbill if (cnt == 0) 1900615Sbill return; 1901615Sbill if (bp->b_nleft) { 1902615Sbill put = bp->b_nleft; 1903615Sbill if (put > cnt) 1904615Sbill put = cnt; 1905615Sbill bp->b_nleft -= put; 1906615Sbill to = bp->b_ptr; 1907615Sbill asm("movc3 r8,(r11),(r7)"); 1908615Sbill bp->b_ptr += put; 1909615Sbill p += put; 1910615Sbill cnt -= put; 1911615Sbill goto top; 1912615Sbill } 1913615Sbill if (cnt >= BUFSIZ) { 1914615Sbill if (bp->b_ptr != bp->b_buf) 1915615Sbill bflush1(bp); 1916615Sbill put = cnt - cnt % BUFSIZ; 1917615Sbill if (boffset != bp->b_off) 1918615Sbill lseek(biofd, bp->b_off, 0); 1919615Sbill if (write(biofd, p, put) != put) { 1920615Sbill bwrerror = 1; 1921615Sbill error(1, "output write error"); 1922615Sbill } 1923615Sbill bp->b_off += put; 1924615Sbill boffset = bp->b_off; 1925615Sbill p += put; 1926615Sbill cnt -= put; 1927615Sbill goto top; 1928615Sbill } 1929615Sbill bflush1(bp); 1930615Sbill goto top; 1931615Sbill } 1932615Sbill 1933615Sbill bflush() 1934615Sbill { 1935615Sbill register struct biobuf *bp; 1936615Sbill 1937615Sbill if (bwrerror) 1938615Sbill return; 1939615Sbill for (bp = biobufs; bp; bp = bp->b_link) 1940615Sbill bflush1(bp); 1941615Sbill } 1942615Sbill 1943615Sbill bflush1(bp) 1944615Sbill register struct biobuf *bp; 1945615Sbill { 1946615Sbill register int cnt = bp->b_ptr - bp->b_buf; 1947615Sbill 1948615Sbill if (cnt == 0) 1949615Sbill return; 1950615Sbill if (boffset != bp->b_off) 1951615Sbill lseek(biofd, bp->b_off, 0); 1952615Sbill if (write(biofd, bp->b_buf, cnt) != cnt) { 1953615Sbill bwrerror = 1; 1954615Sbill error(1, "output write error"); 1955615Sbill } 1956615Sbill bp->b_off += cnt; 1957615Sbill boffset = bp->b_off; 1958615Sbill bp->b_ptr = bp->b_buf; 1959615Sbill bp->b_nleft = BUFSIZ; 1960615Sbill } 1961615Sbill 1962615Sbill bflushc(bp, c) 1963615Sbill register struct biobuf *bp; 1964615Sbill { 1965615Sbill 1966615Sbill bflush1(bp); 1967615Sbill bputc(c, bp); 1968615Sbill } 1969