1*19842Sdist /* 2*19842Sdist * Copyright (c) 1980 Regents of the University of California. 3*19842Sdist * All rights reserved. The Berkeley software License Agreement 4*19842Sdist * specifies the terms and conditions for redistribution. 5*19842Sdist */ 6*19842Sdist 712671Ssam #ifndef lint 8*19842Sdist char copyright[] = 9*19842Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 10*19842Sdist All rights reserved.\n"; 11*19842Sdist #endif not lint 126414Smckusic 13*19842Sdist #ifndef lint 14*19842Sdist static char sccsid[] = "@(#)ld.c 5.1 (Berkeley) 04/30/85"; 15*19842Sdist #endif not lint 16*19842Sdist 17615Sbill /* 18898Sbill * ld - string table version for VAX 19615Sbill */ 20615Sbill 2117133Ssam #include <sys/param.h> 22615Sbill #include <signal.h> 23615Sbill #include <stdio.h> 24615Sbill #include <ctype.h> 25650Sbill #include <ar.h> 26650Sbill #include <a.out.h> 27615Sbill #include <ranlib.h> 2813591Swnj #include <sys/stat.h> 2917133Ssam #include <sys/file.h> 30615Sbill 31615Sbill /* 32615Sbill * Basic strategy: 33615Sbill * 34615Sbill * The loader takes a number of files and libraries as arguments. 35615Sbill * A first pass examines each file in turn. Normal files are 36615Sbill * unconditionally loaded, and the (external) symbols they define and require 37615Sbill * are noted in the symbol table. Libraries are searched, and the 38615Sbill * library members which define needed symbols are remembered 39615Sbill * in a special data structure so they can be selected on the second 40615Sbill * pass. Symbols defined and required by library members are also 41615Sbill * recorded. 42615Sbill * 43615Sbill * After the first pass, the loader knows the size of the basic text 44615Sbill * data, and bss segments from the sum of the sizes of the modules which 45615Sbill * were required. It has computed, for each ``common'' symbol, the 46615Sbill * maximum size of any reference to it, and these symbols are then assigned 47615Sbill * storage locations after their sizes are appropriately rounded. 48615Sbill * The loader now knows all sizes for the eventual output file, and 49615Sbill * can determine the final locations of external symbols before it 50615Sbill * begins a second pass. 51615Sbill * 52615Sbill * On the second pass each normal file and required library member 53615Sbill * is processed again. The symbol table for each such file is 54615Sbill * reread and relevant parts of it are placed in the output. The offsets 55615Sbill * in the local symbol table for externally defined symbols are recorded 56615Sbill * since relocation information refers to symbols in this way. 57615Sbill * Armed with all necessary information, the text and data segments 58615Sbill * are relocated and the result is placed in the output file, which 59615Sbill * is pasted together, ``in place'', by writing to it in several 60615Sbill * different places concurrently. 61615Sbill */ 62615Sbill 63615Sbill /* 64615Sbill * Internal data structures 65615Sbill * 66615Sbill * All internal data structures are segmented and dynamically extended. 67615Sbill * The basic structures hold 1103 (NSYM) symbols, ~~200 (NROUT) 68615Sbill * referenced library members, and 100 (NSYMPR) private (local) symbols 69615Sbill * per object module. For large programs and/or modules, these structures 70615Sbill * expand to be up to 40 (NSEG) times as large as this as necessary. 71615Sbill */ 72615Sbill #define NSEG 40 /* Number of segments, each data structure */ 73615Sbill #define NSYM 1103 /* Number of symbols per segment */ 74615Sbill #define NROUT 250 /* Number of library references per segment */ 75615Sbill #define NSYMPR 100 /* Number of private symbols per segment */ 76615Sbill 77615Sbill /* 78615Sbill * Structure describing each symbol table segment. 79615Sbill * Each segment has its own hash table. We record the first 80615Sbill * address in and first address beyond both the symbol and hash 81615Sbill * tables, for use in the routine symx and the lookup routine respectively. 82615Sbill * The symfree routine also understands this structure well as it used 83615Sbill * to back out symbols from modules we decide that we don't need in pass 1. 84615Sbill * 85615Sbill * Csymseg points to the current symbol table segment; 86615Sbill * csymseg->sy_first[csymseg->sy_used] is the next symbol slot to be allocated, 87615Sbill * (unless csymseg->sy_used == NSYM in which case we will allocate another 88615Sbill * symbol table segment first.) 89615Sbill */ 90615Sbill struct symseg { 91615Sbill struct nlist *sy_first; /* base of this alloc'ed segment */ 92615Sbill struct nlist *sy_last; /* end of this segment, for n_strx */ 93615Sbill int sy_used; /* symbols used in this seg */ 94615Sbill struct nlist **sy_hfirst; /* base of hash table, this seg */ 95615Sbill struct nlist **sy_hlast; /* end of hash table, this seg */ 96615Sbill } symseg[NSEG], *csymseg; 97615Sbill 98615Sbill /* 99615Sbill * The lookup routine uses quadratic rehash. Since a quadratic rehash 100615Sbill * only probes 1/2 of the buckets in the table, and since the hash 101615Sbill * table is segmented the same way the symbol table is, we make the 102615Sbill * hash table have twice as many buckets as there are symbol table slots 103615Sbill * in the segment. This guarantees that the quadratic rehash will never 104615Sbill * fail to find an empty bucket if the segment is not full and the 105615Sbill * symbol is not there. 106615Sbill */ 107615Sbill #define HSIZE (NSYM*2) 108615Sbill 109615Sbill /* 110615Sbill * Xsym converts symbol table indices (ala x) into symbol table pointers. 111615Sbill * Symx (harder, but never used in loops) inverts pointers into the symbol 112615Sbill * table into indices using the symseg[] structure. 113615Sbill */ 114615Sbill #define xsym(x) (symseg[(x)/NSYM].sy_first+((x)%NSYM)) 115615Sbill /* symx() is a function, defined below */ 116615Sbill 117615Sbill struct nlist cursym; /* current symbol */ 118615Sbill struct nlist *lastsym; /* last symbol entered */ 119615Sbill struct nlist *nextsym; /* next available symbol table entry */ 120615Sbill struct nlist *addsym; /* first sym defined during incr load */ 121615Sbill int nsym; /* pass2: number of local symbols in a.out */ 122615Sbill /* nsym + symx(nextsym) is the symbol table size during pass2 */ 123615Sbill 124615Sbill struct nlist **lookup(), **slookup(); 125650Sbill struct nlist *p_etext, *p_edata, *p_end, *entrypt; 126615Sbill 127615Sbill /* 128615Sbill * Definitions of segmentation for library member table. 129615Sbill * For each library we encounter on pass 1 we record pointers to all 130615Sbill * members which we will load on pass 2. These are recorded as offsets 131615Sbill * into the archive in the library member table. Libraries are 132615Sbill * separated in the table by the special offset value -1. 133615Sbill */ 134615Sbill off_t li_init[NROUT]; 135615Sbill struct libseg { 136615Sbill off_t *li_first; 137615Sbill int li_used; 138615Sbill int li_used2; 139615Sbill } libseg[NSEG] = { 140615Sbill li_init, 0, 0, 141615Sbill }, *clibseg = libseg; 142615Sbill 143615Sbill /* 144615Sbill * In processing each module on pass 2 we must relocate references 145615Sbill * relative to external symbols. These references are recorded 146615Sbill * in the relocation information as relative to local symbol numbers 147615Sbill * assigned to the external symbols when the module was created. 148615Sbill * Thus before relocating the module in pass 2 we create a table 149615Sbill * which maps these internal numbers to symbol table entries. 150615Sbill * A hash table is constructed, based on the local symbol table indices, 151615Sbill * for quick lookup of these symbols. 152615Sbill */ 153615Sbill #define LHSIZ 31 154615Sbill struct local { 155615Sbill int l_index; /* index to symbol in file */ 156615Sbill struct nlist *l_symbol; /* ptr to symbol table */ 157615Sbill struct local *l_link; /* hash link */ 158615Sbill } *lochash[LHSIZ], lhinit[NSYMPR]; 159615Sbill struct locseg { 160615Sbill struct local *lo_first; 161615Sbill int lo_used; 162615Sbill } locseg[NSEG] = { 163615Sbill lhinit, 0 164615Sbill }, *clocseg; 165615Sbill 166615Sbill /* 167615Sbill * Libraries are typically built with a table of contents, 168615Sbill * which is the first member of a library with special file 169615Sbill * name __.SYMDEF and contains a list of symbol names 170615Sbill * and with each symbol the offset of the library member which defines 171615Sbill * it. The loader uses this table to quickly tell which library members 172615Sbill * are (potentially) useful. The alternative, examining the symbol 173615Sbill * table of each library member, is painfully slow for large archives. 174615Sbill * 175615Sbill * See <ranlib.h> for the definition of the ranlib structure and an 176615Sbill * explanation of the __.SYMDEF file format. 177615Sbill */ 178615Sbill int tnum; /* number of symbols in table of contents */ 179615Sbill int ssiz; /* size of string table for table of contents */ 180615Sbill struct ranlib *tab; /* the table of contents (dynamically allocated) */ 181615Sbill char *tabstr; /* string table for table of contents */ 182615Sbill 183615Sbill /* 184615Sbill * We open each input file or library only once, but in pass2 we 185615Sbill * (historically) read from such a file at 2 different places at the 186615Sbill * same time. These structures are remnants from those days, 187650Sbill * and now serve only to catch ``Premature EOF''. 1886414Smckusic * In order to make I/O more efficient, we provide routines which 18916068Sralph * use the optimal block size returned by stat(). 190615Sbill */ 1916414Smckusic #define BLKSIZE 1024 192615Sbill typedef struct { 193615Sbill short *fakeptr; 194615Sbill int bno; 195615Sbill int nibuf; 196615Sbill int nuser; 19716068Sralph char *buff; 19816068Sralph int bufsize; 199615Sbill } PAGE; 200615Sbill 201615Sbill PAGE page[2]; 20216068Sralph int p_blksize; 20316068Sralph int p_blkshift; 20416068Sralph int p_blkmask; 205615Sbill 206615Sbill struct { 207615Sbill short *fakeptr; 208615Sbill int bno; 209615Sbill int nibuf; 210615Sbill int nuser; 211615Sbill } fpage; 212615Sbill 213615Sbill typedef struct { 214615Sbill char *ptr; 215615Sbill int bno; 216615Sbill int nibuf; 217615Sbill long size; 218615Sbill long pos; 219615Sbill PAGE *pno; 220615Sbill } STREAM; 221615Sbill 222615Sbill STREAM text; 223615Sbill STREAM reloc; 224615Sbill 225615Sbill /* 226615Sbill * Header from the a.out and the archive it is from (if any). 227615Sbill */ 228615Sbill struct exec filhdr; 229615Sbill struct ar_hdr archdr; 230615Sbill #define OARMAG 0177545 231615Sbill 232615Sbill /* 233615Sbill * Options. 234615Sbill */ 235615Sbill int trace; 236615Sbill int xflag; /* discard local symbols */ 237615Sbill int Xflag; /* discard locals starting with 'L' */ 238615Sbill int Sflag; /* discard all except locals and globals*/ 239615Sbill int rflag; /* preserve relocation bits, don't define common */ 240615Sbill int arflag; /* original copy of rflag */ 241615Sbill int sflag; /* discard all symbols */ 242898Sbill int Mflag; /* print rudimentary load map */ 243615Sbill int nflag; /* pure procedure */ 244615Sbill int dflag; /* define common even with rflag */ 245650Sbill int zflag; /* demand paged */ 246615Sbill long hsize; /* size of hole at beginning of data to be squashed */ 247615Sbill int Aflag; /* doing incremental load */ 248650Sbill int Nflag; /* want impure a.out */ 249615Sbill int funding; /* reading fundamental file for incremental load */ 250898Sbill int yflag; /* number of symbols to be traced */ 251898Sbill char **ytab; /* the symbols */ 252615Sbill 253615Sbill /* 254615Sbill * These are the cumulative sizes, set in pass 1, which 255615Sbill * appear in the a.out header when the loader is finished. 256615Sbill */ 257615Sbill off_t tsize, dsize, bsize, trsize, drsize, ssize; 258615Sbill 259615Sbill /* 260615Sbill * Symbol relocation: c?rel is a scale factor which is 261615Sbill * added to an old relocation to convert it to new units; 262615Sbill * i.e. it is the difference between segment origins. 263650Sbill * (Thus if we are loading from a data segment which began at location 264650Sbill * 4 in a .o file into an a.out where it will be loaded starting at 265650Sbill * 1024, cdrel will be 1020.) 266615Sbill */ 267615Sbill long ctrel, cdrel, cbrel; 268615Sbill 269615Sbill /* 270650Sbill * Textbase is the start address of all text, 0 unless given by -T. 271615Sbill * Database is the base of all data, computed before and used during pass2. 272650Sbill */ 273650Sbill long textbase, database; 274650Sbill 275650Sbill /* 276615Sbill * The base addresses for the loaded text, data and bss from the 277615Sbill * current module during pass2 are given by torigin, dorigin and borigin. 278615Sbill */ 279615Sbill long torigin, dorigin, borigin; 280615Sbill 281615Sbill /* 282615Sbill * Errlev is nonzero when errors have occured. 283615Sbill * Delarg is an implicit argument to the routine delexit 284615Sbill * which is called on error. We do ``delarg = errlev'' before normal 285615Sbill * exits, and only if delarg is 0 (i.e. errlev was 0) do we make the 286615Sbill * result file executable. 287615Sbill */ 288615Sbill int errlev; 289615Sbill int delarg = 4; 290615Sbill 291615Sbill /* 292615Sbill * The biobuf structure and associated routines are used to write 293615Sbill * into one file at several places concurrently. Calling bopen 294615Sbill * with a biobuf structure sets it up to write ``biofd'' starting 295615Sbill * at the specified offset. You can then use ``bwrite'' and/or ``bputc'' 296615Sbill * to stuff characters in the stream, much like ``fwrite'' and ``fputc''. 297615Sbill * Calling bflush drains all the buffers and MUST be done before exit. 298615Sbill */ 299615Sbill struct biobuf { 300615Sbill short b_nleft; /* Number free spaces left in b_buf */ 30116068Sralph /* Initialize to be less than b_bufsize initially, to boundary align in file */ 302615Sbill char *b_ptr; /* Next place to stuff characters */ 30316068Sralph char *b_buf; /* Pointer to the buffer */ 30416068Sralph int b_bufsize; /* Size of the buffer */ 305615Sbill off_t b_off; /* Current file offset */ 306615Sbill struct biobuf *b_link; /* Link in chain for bflush() */ 307615Sbill } *biobufs; 308615Sbill #define bputc(c,b) ((b)->b_nleft ? (--(b)->b_nleft, *(b)->b_ptr++ = (c)) \ 309615Sbill : bflushc(b, c)) 310615Sbill int biofd; 311615Sbill off_t boffset; 312615Sbill struct biobuf *tout, *dout, *trout, *drout, *sout, *strout; 313615Sbill 314615Sbill /* 315615Sbill * Offset is the current offset in the string file. 316615Sbill * Its initial value reflects the fact that we will 317615Sbill * eventually stuff the size of the string table at the 318615Sbill * beginning of the string table (i.e. offset itself!). 319615Sbill */ 320615Sbill off_t offset = sizeof (off_t); 321615Sbill 322615Sbill int ofilfnd; /* -o given; otherwise move l.out to a.out */ 323615Sbill char *ofilename = "l.out"; 3243606Ssklower int ofilemode; /* respect umask even for unsucessful ld's */ 325615Sbill int infil; /* current input file descriptor */ 326615Sbill char *filname; /* and its name */ 327615Sbill 32817133Ssam #define NDIRS 25 32917133Ssam char *dirs[NDIRS]; /* directories for library search */ 33017133Ssam int ndir; /* number of directories */ 33117133Ssam 332615Sbill /* 333615Sbill * Base of the string table of the current module (pass1 and pass2). 334615Sbill */ 335615Sbill char *curstr; 336615Sbill 33712671Ssam /* 33812671Ssam * System software page size, as returned by getpagesize. 33912671Ssam */ 34012671Ssam int pagesize; 34112671Ssam 342615Sbill char get(); 343615Sbill int delexit(); 344615Sbill char *savestr(); 34517133Ssam char *malloc(); 346615Sbill 347615Sbill main(argc, argv) 348615Sbill char **argv; 349615Sbill { 350615Sbill register int c, i; 351615Sbill int num; 352615Sbill register char *ap, **p; 353615Sbill char save; 354615Sbill 355650Sbill if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 356615Sbill signal(SIGINT, delexit); 357650Sbill signal(SIGTERM, delexit); 358650Sbill } 359615Sbill if (argc == 1) 360615Sbill exit(4); 36112671Ssam pagesize = getpagesize(); 362615Sbill 36317133Ssam /* 36417133Ssam * Pull out search directories. 36517133Ssam */ 36617133Ssam for (c = 1; c < argc; c++) { 36717133Ssam ap = argv[c]; 36817133Ssam if (ap[0] == '-' && ap[1] == 'L') { 36917133Ssam if (ap[2] == 0) 37017133Ssam error(1, "-L: pathname missing"); 37117133Ssam if (ndir >= NDIRS) 37217133Ssam error(1, "-L: too many directories"); 37317133Ssam dirs[ndir++] = &ap[2]; 37417133Ssam } 37517133Ssam } 37617133Ssam /* add default search directories */ 37717133Ssam dirs[ndir++] = "/lib"; 37817133Ssam dirs[ndir++] = "/usr/lib"; 37917133Ssam dirs[ndir++] = "/usr/local/lib"; 38017133Ssam 38117133Ssam p = argv+1; 382650Sbill /* 383650Sbill * Scan files once to find where symbols are defined. 384650Sbill */ 385615Sbill for (c=1; c<argc; c++) { 386615Sbill if (trace) 387615Sbill printf("%s:\n", *p); 388615Sbill filname = 0; 389615Sbill ap = *p++; 390615Sbill if (*ap != '-') { 391615Sbill load1arg(ap); 392615Sbill continue; 393615Sbill } 394615Sbill for (i=1; ap[i]; i++) switch (ap[i]) { 395615Sbill 396615Sbill case 'o': 397615Sbill if (++c >= argc) 398615Sbill error(1, "-o where?"); 399615Sbill ofilename = *p++; 400615Sbill ofilfnd++; 401615Sbill continue; 402615Sbill case 'u': 403615Sbill case 'e': 404615Sbill if (++c >= argc) 405615Sbill error(1, "-u or -c: arg missing"); 406615Sbill enter(slookup(*p++)); 407615Sbill if (ap[i]=='e') 408615Sbill entrypt = lastsym; 409615Sbill continue; 410615Sbill case 'H': 411615Sbill if (++c >= argc) 412615Sbill error(1, "-H: arg missing"); 413615Sbill if (tsize!=0) 414615Sbill error(1, "-H: too late, some text already loaded"); 415615Sbill hsize = atoi(*p++); 416615Sbill continue; 417615Sbill case 'A': 418615Sbill if (++c >= argc) 419615Sbill error(1, "-A: arg missing"); 420615Sbill if (Aflag) 421615Sbill error(1, "-A: only one base file allowed"); 422615Sbill Aflag = 1; 423615Sbill nflag = 0; 424615Sbill funding = 1; 425615Sbill load1arg(*p++); 426615Sbill trsize = drsize = tsize = dsize = bsize = 0; 427615Sbill ctrel = cdrel = cbrel = 0; 428615Sbill funding = 0; 429615Sbill addsym = nextsym; 430615Sbill continue; 431615Sbill case 'D': 432615Sbill if (++c >= argc) 433615Sbill error(1, "-D: arg missing"); 434615Sbill num = htoi(*p++); 435615Sbill if (dsize > num) 436615Sbill error(1, "-D: too small"); 437615Sbill dsize = num; 438615Sbill continue; 439615Sbill case 'T': 440615Sbill if (++c >= argc) 441615Sbill error(1, "-T: arg missing"); 442615Sbill if (tsize!=0) 443615Sbill error(1, "-T: too late, some text already loaded"); 444615Sbill textbase = htoi(*p++); 445615Sbill continue; 446615Sbill case 'l': 447615Sbill save = ap[--i]; 448615Sbill ap[i]='-'; 449615Sbill load1arg(&ap[i]); 450615Sbill ap[i]=save; 451615Sbill goto next; 452898Sbill case 'M': 453898Sbill Mflag++; 454898Sbill continue; 455615Sbill case 'x': 456615Sbill xflag++; 457615Sbill continue; 458615Sbill case 'X': 459615Sbill Xflag++; 460615Sbill continue; 461615Sbill case 'S': 462615Sbill Sflag++; 463615Sbill continue; 464615Sbill case 'r': 465615Sbill rflag++; 466615Sbill arflag++; 467615Sbill continue; 468615Sbill case 's': 469615Sbill sflag++; 470615Sbill xflag++; 471615Sbill continue; 472615Sbill case 'n': 473615Sbill nflag++; 474650Sbill Nflag = zflag = 0; 475615Sbill continue; 476615Sbill case 'N': 477650Sbill Nflag++; 478650Sbill nflag = zflag = 0; 479615Sbill continue; 480615Sbill case 'd': 481615Sbill dflag++; 482615Sbill continue; 483615Sbill case 'i': 484615Sbill printf("ld: -i ignored\n"); 485615Sbill continue; 486615Sbill case 't': 487615Sbill trace++; 488615Sbill continue; 489898Sbill case 'y': 490898Sbill if (ap[i+1] == 0) 491898Sbill error(1, "-y: symbol name missing"); 492898Sbill if (yflag == 0) { 493898Sbill ytab = (char **)calloc(argc, sizeof (char **)); 494898Sbill if (ytab == 0) 495898Sbill error(1, "ran out of memory (-y)"); 496898Sbill } 497898Sbill ytab[yflag++] = &ap[i+1]; 498898Sbill goto next; 499615Sbill case 'z': 500615Sbill zflag++; 501650Sbill Nflag = nflag = 0; 502615Sbill continue; 50317133Ssam case 'L': 50417133Ssam goto next; 505615Sbill default: 506615Sbill filname = savestr("-x"); /* kludge */ 507615Sbill filname[1] = ap[i]; /* kludge */ 508615Sbill archdr.ar_name[0] = 0; /* kludge */ 509615Sbill error(1, "bad flag"); 510615Sbill } 511615Sbill next: 512615Sbill ; 513615Sbill } 514650Sbill if (rflag == 0 && Nflag == 0 && nflag == 0) 515650Sbill zflag++; 516615Sbill endload(argc, argv); 517615Sbill exit(0); 518615Sbill } 519615Sbill 520615Sbill /* 521615Sbill * Convert a ascii string which is a hex number. 522615Sbill * Used by -T and -D options. 523615Sbill */ 524615Sbill htoi(p) 525615Sbill register char *p; 526615Sbill { 527615Sbill register int c, n; 528615Sbill 529615Sbill n = 0; 530615Sbill while (c = *p++) { 531615Sbill n <<= 4; 532615Sbill if (isdigit(c)) 533615Sbill n += c - '0'; 534615Sbill else if (c >= 'a' && c <= 'f') 535615Sbill n += 10 + (c - 'a'); 536615Sbill else if (c >= 'A' && c <= 'F') 537615Sbill n += 10 + (c - 'A'); 538615Sbill else 539615Sbill error(1, "badly formed hex number"); 540615Sbill } 541615Sbill return (n); 542615Sbill } 543615Sbill 544615Sbill delexit() 545615Sbill { 5469332Smckusick struct stat stbuf; 5479332Smckusick long size; 5489332Smckusick char c = 0; 549615Sbill 550615Sbill bflush(); 551615Sbill unlink("l.out"); 552615Sbill if (delarg==0 && Aflag==0) 5533606Ssklower chmod(ofilename, ofilemode); 5549332Smckusick /* 5559332Smckusick * We have to insure that the last block of the data segment 55616068Sralph * is allocated a full pagesize block. If the underlying 55716068Sralph * file system allocates frags that are smaller than pagesize, 55816068Sralph * a full zero filled pagesize block needs to be allocated so 5599332Smckusick * that when it is demand paged, the paged in block will be 5609332Smckusick * appropriately filled with zeros. 5619332Smckusick */ 5629332Smckusick fstat(biofd, &stbuf); 56316068Sralph size = round(stbuf.st_size, pagesize); 56410640Smckusick if (!rflag && size > stbuf.st_size) { 5659332Smckusick lseek(biofd, size - 1, 0); 5669332Smckusick write(biofd, &c, 1); 5679332Smckusick } 568615Sbill exit (delarg); 569615Sbill } 570615Sbill 571615Sbill endload(argc, argv) 572615Sbill int argc; 573615Sbill char **argv; 574615Sbill { 575615Sbill register int c, i; 576615Sbill long dnum; 577615Sbill register char *ap, **p; 578615Sbill 579615Sbill clibseg = libseg; 580615Sbill filname = 0; 581615Sbill middle(); 582615Sbill setupout(); 583615Sbill p = argv+1; 584615Sbill for (c=1; c<argc; c++) { 585615Sbill ap = *p++; 586615Sbill if (trace) 587615Sbill printf("%s:\n", ap); 588615Sbill if (*ap != '-') { 589615Sbill load2arg(ap); 590615Sbill continue; 591615Sbill } 592615Sbill for (i=1; ap[i]; i++) switch (ap[i]) { 593615Sbill 594615Sbill case 'D': 595615Sbill dnum = htoi(*p); 596615Sbill if (dorigin < dnum) 597615Sbill while (dorigin < dnum) 598615Sbill bputc(0, dout), dorigin++; 599615Sbill /* fall into ... */ 600615Sbill case 'T': 601615Sbill case 'u': 602615Sbill case 'e': 603615Sbill case 'o': 604615Sbill case 'H': 605615Sbill ++c; 606615Sbill ++p; 607615Sbill /* fall into ... */ 608615Sbill default: 609615Sbill continue; 610615Sbill case 'A': 611615Sbill funding = 1; 612615Sbill load2arg(*p++); 613615Sbill funding = 0; 614615Sbill c++; 615615Sbill continue; 616898Sbill case 'y': 61717133Ssam case 'L': 618898Sbill goto next; 619615Sbill case 'l': 620615Sbill ap[--i]='-'; 621615Sbill load2arg(&ap[i]); 622615Sbill goto next; 623615Sbill } 624615Sbill next: 625615Sbill ; 626615Sbill } 627615Sbill finishout(); 628615Sbill } 629615Sbill 630615Sbill /* 631615Sbill * Scan file to find defined symbols. 632615Sbill */ 633615Sbill load1arg(cp) 634615Sbill register char *cp; 635615Sbill { 636615Sbill register struct ranlib *tp; 637615Sbill off_t nloc; 638898Sbill int kind; 639615Sbill 640898Sbill kind = getfile(cp); 641898Sbill if (Mflag) 642898Sbill printf("%s\n", filname); 643898Sbill switch (kind) { 644615Sbill 645615Sbill /* 646615Sbill * Plain file. 647615Sbill */ 648615Sbill case 0: 649615Sbill load1(0, 0L); 650615Sbill break; 651615Sbill 652615Sbill /* 653615Sbill * Archive without table of contents. 654615Sbill * (Slowly) process each member. 655615Sbill */ 656615Sbill case 1: 657898Sbill error(-1, 658898Sbill "warning: archive has no table of contents; add one using ranlib(1)"); 659615Sbill nloc = SARMAG; 660615Sbill while (step(nloc)) 661615Sbill nloc += sizeof(archdr) + 662615Sbill round(atol(archdr.ar_size), sizeof (short)); 663615Sbill break; 664615Sbill 665615Sbill /* 666615Sbill * Archive with table of contents. 667615Sbill * Read the table of contents and its associated string table. 668615Sbill * Pass through the library resolving symbols until nothing changes 669615Sbill * for an entire pass (i.e. you can get away with backward references 670615Sbill * when there is a table of contents!) 671615Sbill */ 672615Sbill case 2: 673615Sbill nloc = SARMAG + sizeof (archdr); 674615Sbill dseek(&text, nloc, sizeof (tnum)); 675615Sbill mget((char *)&tnum, sizeof (tnum), &text); 676615Sbill nloc += sizeof (tnum); 677615Sbill tab = (struct ranlib *)malloc(tnum); 678615Sbill if (tab == 0) 679615Sbill error(1, "ran out of memory (toc)"); 680615Sbill dseek(&text, nloc, tnum); 681615Sbill mget((char *)tab, tnum, &text); 682615Sbill nloc += tnum; 683615Sbill tnum /= sizeof (struct ranlib); 684615Sbill dseek(&text, nloc, sizeof (ssiz)); 685615Sbill mget((char *)&ssiz, sizeof (ssiz), &text); 686615Sbill nloc += sizeof (ssiz); 687615Sbill tabstr = (char *)malloc(ssiz); 688615Sbill if (tabstr == 0) 689615Sbill error(1, "ran out of memory (tocstr)"); 690615Sbill dseek(&text, nloc, ssiz); 691615Sbill mget((char *)tabstr, ssiz, &text); 692615Sbill for (tp = &tab[tnum]; --tp >= tab;) { 693615Sbill if (tp->ran_un.ran_strx < 0 || 694615Sbill tp->ran_un.ran_strx >= ssiz) 695615Sbill error(1, "mangled archive table of contents"); 696615Sbill tp->ran_un.ran_name = tabstr + tp->ran_un.ran_strx; 697615Sbill } 698615Sbill while (ldrand()) 699615Sbill continue; 700615Sbill cfree((char *)tab); 701615Sbill cfree(tabstr); 702615Sbill nextlibp(-1); 703615Sbill break; 704615Sbill 705615Sbill /* 706615Sbill * Table of contents is out of date, so search 707615Sbill * as a normal library (but skip the __.SYMDEF file). 708615Sbill */ 709615Sbill case 3: 710898Sbill error(-1, 711898Sbill "warning: table of contents for archive is out of date; rerun ranlib(1)"); 712615Sbill nloc = SARMAG; 713615Sbill do 714615Sbill nloc += sizeof(archdr) + 715615Sbill round(atol(archdr.ar_size), sizeof(short)); 716615Sbill while (step(nloc)); 717615Sbill break; 718615Sbill } 719615Sbill close(infil); 720615Sbill } 721615Sbill 722615Sbill /* 723615Sbill * Advance to the next archive member, which 724615Sbill * is at offset nloc in the archive. If the member 725615Sbill * is useful, record its location in the liblist structure 726615Sbill * for use in pass2. Mark the end of the archive in libilst with a -1. 727615Sbill */ 728615Sbill step(nloc) 729615Sbill off_t nloc; 730615Sbill { 731615Sbill 732615Sbill dseek(&text, nloc, (long) sizeof archdr); 733615Sbill if (text.size <= 0) { 734615Sbill nextlibp(-1); 735615Sbill return (0); 736615Sbill } 737615Sbill getarhdr(); 738615Sbill if (load1(1, nloc + (sizeof archdr))) 739615Sbill nextlibp(nloc); 740615Sbill return (1); 741615Sbill } 742615Sbill 743615Sbill /* 744615Sbill * Record the location of a useful archive member. 745615Sbill * Recording -1 marks the end of files from an archive. 746615Sbill * The liblist data structure is dynamically extended here. 747615Sbill */ 748615Sbill nextlibp(val) 749615Sbill off_t val; 750615Sbill { 751615Sbill 752615Sbill if (clibseg->li_used == NROUT) { 753615Sbill if (++clibseg == &libseg[NSEG]) 754615Sbill error(1, "too many files loaded from libraries"); 755615Sbill clibseg->li_first = (off_t *)malloc(NROUT * sizeof (off_t)); 756615Sbill if (clibseg->li_first == 0) 757615Sbill error(1, "ran out of memory (nextlibp)"); 758615Sbill } 759615Sbill clibseg->li_first[clibseg->li_used++] = val; 760898Sbill if (val != -1 && Mflag) 761898Sbill printf("\t%s\n", archdr.ar_name); 762615Sbill } 763615Sbill 764615Sbill /* 765615Sbill * One pass over an archive with a table of contents. 766615Sbill * Remember the number of symbols currently defined, 767615Sbill * then call step on members which look promising (i.e. 768615Sbill * that define a symbol which is currently externally undefined). 769615Sbill * Indicate to our caller whether this process netted any more symbols. 770615Sbill */ 771615Sbill ldrand() 772615Sbill { 773615Sbill register struct nlist *sp, **hp; 774615Sbill register struct ranlib *tp, *tplast; 775615Sbill off_t loc; 776615Sbill int nsymt = symx(nextsym); 777615Sbill 778615Sbill tplast = &tab[tnum-1]; 779615Sbill for (tp = tab; tp <= tplast; tp++) { 780615Sbill if ((hp = slookup(tp->ran_un.ran_name)) == 0) 781615Sbill continue; 782615Sbill sp = *hp; 783615Sbill if (sp->n_type != N_EXT+N_UNDF) 784615Sbill continue; 785615Sbill step(tp->ran_off); 786615Sbill loc = tp->ran_off; 787615Sbill while (tp < tplast && (tp+1)->ran_off == loc) 788615Sbill tp++; 789615Sbill } 790615Sbill return (symx(nextsym) != nsymt); 791615Sbill } 792615Sbill 793615Sbill /* 794615Sbill * Examine a single file or archive member on pass 1. 795615Sbill */ 796615Sbill load1(libflg, loc) 797615Sbill off_t loc; 798615Sbill { 799615Sbill register struct nlist *sp; 800615Sbill struct nlist *savnext; 801615Sbill int ndef, nlocal, type, size, nsymt; 802615Sbill register int i; 803615Sbill off_t maxoff; 804615Sbill struct stat stb; 805615Sbill 806615Sbill readhdr(loc); 807615Sbill if (filhdr.a_syms == 0) { 808615Sbill if (filhdr.a_text+filhdr.a_data == 0) 809615Sbill return (0); 810615Sbill error(1, "no namelist"); 811615Sbill } 812615Sbill if (libflg) 813615Sbill maxoff = atol(archdr.ar_size); 814615Sbill else { 815615Sbill fstat(infil, &stb); 816615Sbill maxoff = stb.st_size; 817615Sbill } 818615Sbill if (N_STROFF(filhdr) + sizeof (off_t) >= maxoff) 819615Sbill error(1, "too small (old format .o?)"); 820615Sbill ctrel = tsize; cdrel += dsize; cbrel += bsize; 821615Sbill ndef = 0; 822615Sbill nlocal = sizeof(cursym); 823615Sbill savnext = nextsym; 824615Sbill loc += N_SYMOFF(filhdr); 825615Sbill dseek(&text, loc, filhdr.a_syms); 826615Sbill dseek(&reloc, loc + filhdr.a_syms, sizeof(off_t)); 827615Sbill mget(&size, sizeof (size), &reloc); 828615Sbill dseek(&reloc, loc + filhdr.a_syms+sizeof (off_t), size-sizeof (off_t)); 829615Sbill curstr = (char *)malloc(size); 830615Sbill if (curstr == NULL) 831615Sbill error(1, "no space for string table"); 832615Sbill mget(curstr+sizeof(off_t), size-sizeof(off_t), &reloc); 833615Sbill while (text.size > 0) { 834615Sbill mget((char *)&cursym, sizeof(struct nlist), &text); 835615Sbill if (cursym.n_un.n_strx) { 836615Sbill if (cursym.n_un.n_strx<sizeof(size) || 837615Sbill cursym.n_un.n_strx>=size) 838615Sbill error(1, "bad string table index (pass 1)"); 839615Sbill cursym.n_un.n_name = curstr + cursym.n_un.n_strx; 840615Sbill } 841615Sbill type = cursym.n_type; 842615Sbill if ((type&N_EXT)==0) { 843615Sbill if (Xflag==0 || cursym.n_un.n_name[0]!='L' || 844615Sbill type & N_STAB) 845615Sbill nlocal += sizeof cursym; 846615Sbill continue; 847615Sbill } 848615Sbill symreloc(); 849615Sbill if (enter(lookup())) 850615Sbill continue; 851615Sbill if ((sp = lastsym)->n_type != N_EXT+N_UNDF) 852615Sbill continue; 853615Sbill if (cursym.n_type == N_EXT+N_UNDF) { 854615Sbill if (cursym.n_value > sp->n_value) 855615Sbill sp->n_value = cursym.n_value; 856615Sbill continue; 857615Sbill } 858615Sbill if (sp->n_value != 0 && cursym.n_type == N_EXT+N_TEXT) 859615Sbill continue; 860615Sbill ndef++; 861615Sbill sp->n_type = cursym.n_type; 862615Sbill sp->n_value = cursym.n_value; 863615Sbill } 864615Sbill if (libflg==0 || ndef) { 865615Sbill tsize += filhdr.a_text; 866615Sbill dsize += round(filhdr.a_data, sizeof (long)); 867615Sbill bsize += round(filhdr.a_bss, sizeof (long)); 868615Sbill ssize += nlocal; 869615Sbill trsize += filhdr.a_trsize; 870615Sbill drsize += filhdr.a_drsize; 871615Sbill if (funding) 872615Sbill textbase = (*slookup("_end"))->n_value; 873615Sbill nsymt = symx(nextsym); 874615Sbill for (i = symx(savnext); i < nsymt; i++) { 875615Sbill sp = xsym(i); 876615Sbill sp->n_un.n_name = savestr(sp->n_un.n_name); 877615Sbill } 878615Sbill free(curstr); 879615Sbill return (1); 880615Sbill } 881615Sbill /* 882615Sbill * No symbols defined by this library member. 883615Sbill * Rip out the hash table entries and reset the symbol table. 884615Sbill */ 885615Sbill symfree(savnext); 886615Sbill free(curstr); 887615Sbill return(0); 888615Sbill } 889615Sbill 890615Sbill middle() 891615Sbill { 892615Sbill register struct nlist *sp; 893615Sbill long csize, t, corigin, ocsize; 894615Sbill int nund, rnd; 895615Sbill char s; 896615Sbill register int i; 897615Sbill int nsymt; 898615Sbill 899615Sbill torigin = 0; 900615Sbill dorigin = 0; 901615Sbill borigin = 0; 902615Sbill 903615Sbill p_etext = *slookup("_etext"); 904615Sbill p_edata = *slookup("_edata"); 905615Sbill p_end = *slookup("_end"); 906615Sbill /* 907615Sbill * If there are any undefined symbols, save the relocation bits. 908615Sbill */ 909615Sbill nsymt = symx(nextsym); 910615Sbill if (rflag==0) { 911615Sbill for (i = 0; i < nsymt; i++) { 912615Sbill sp = xsym(i); 913615Sbill if (sp->n_type==N_EXT+N_UNDF && sp->n_value==0 && 914650Sbill sp!=p_end && sp!=p_edata && sp!=p_etext) { 915615Sbill rflag++; 916615Sbill dflag = 0; 917615Sbill break; 918615Sbill } 919615Sbill } 920615Sbill } 921615Sbill if (rflag) 922615Sbill sflag = zflag = 0; 923615Sbill /* 924615Sbill * Assign common locations. 925615Sbill */ 926615Sbill csize = 0; 927615Sbill if (!Aflag) 928615Sbill addsym = symseg[0].sy_first; 929615Sbill database = round(tsize+textbase, 93012671Ssam (nflag||zflag? pagesize : sizeof (long))); 931615Sbill database += hsize; 932615Sbill if (dflag || rflag==0) { 933615Sbill ldrsym(p_etext, tsize, N_EXT+N_TEXT); 934615Sbill ldrsym(p_edata, dsize, N_EXT+N_DATA); 935615Sbill ldrsym(p_end, bsize, N_EXT+N_BSS); 936615Sbill for (i = symx(addsym); i < nsymt; i++) { 937615Sbill sp = xsym(i); 938615Sbill if ((s=sp->n_type)==N_EXT+N_UNDF && 939615Sbill (t = sp->n_value)!=0) { 940615Sbill if (t >= sizeof (double)) 941615Sbill rnd = sizeof (double); 942615Sbill else if (t >= sizeof (long)) 943615Sbill rnd = sizeof (long); 944615Sbill else 945615Sbill rnd = sizeof (short); 946615Sbill csize = round(csize, rnd); 947615Sbill sp->n_value = csize; 948615Sbill sp->n_type = N_EXT+N_COMM; 949615Sbill ocsize = csize; 950615Sbill csize += t; 951615Sbill } 952615Sbill if (s&N_EXT && (s&N_TYPE)==N_UNDF && s&N_STAB) { 953615Sbill sp->n_value = ocsize; 954615Sbill sp->n_type = (s&N_STAB) | (N_EXT+N_COMM); 955615Sbill } 956615Sbill } 957615Sbill } 958615Sbill /* 959615Sbill * Now set symbols to their final value 960615Sbill */ 961615Sbill csize = round(csize, sizeof (long)); 962615Sbill torigin = textbase; 963615Sbill dorigin = database; 964615Sbill corigin = dorigin + dsize; 965615Sbill borigin = corigin + csize; 966615Sbill nund = 0; 967615Sbill nsymt = symx(nextsym); 968615Sbill for (i = symx(addsym); i<nsymt; i++) { 969615Sbill sp = xsym(i); 970615Sbill switch (sp->n_type & (N_TYPE+N_EXT)) { 971615Sbill 972615Sbill case N_EXT+N_UNDF: 9732369Skre if (arflag == 0) 9742369Skre errlev |= 01; 975615Sbill if ((arflag==0 || dflag) && sp->n_value==0) { 976650Sbill if (sp==p_end || sp==p_etext || sp==p_edata) 977650Sbill continue; 978615Sbill if (nund==0) 979615Sbill printf("Undefined:\n"); 980615Sbill nund++; 981615Sbill printf("%s\n", sp->n_un.n_name); 982615Sbill } 983615Sbill continue; 984615Sbill case N_EXT+N_ABS: 985615Sbill default: 986615Sbill continue; 987615Sbill case N_EXT+N_TEXT: 988615Sbill sp->n_value += torigin; 989615Sbill continue; 990615Sbill case N_EXT+N_DATA: 991615Sbill sp->n_value += dorigin; 992615Sbill continue; 993615Sbill case N_EXT+N_BSS: 994615Sbill sp->n_value += borigin; 995615Sbill continue; 996615Sbill case N_EXT+N_COMM: 997615Sbill sp->n_type = (sp->n_type & N_STAB) | (N_EXT+N_BSS); 998615Sbill sp->n_value += corigin; 999615Sbill continue; 1000615Sbill } 1001615Sbill } 1002615Sbill if (sflag || xflag) 1003615Sbill ssize = 0; 1004615Sbill bsize += csize; 1005615Sbill nsym = ssize / (sizeof cursym); 1006615Sbill if (Aflag) { 1007615Sbill fixspec(p_etext,torigin); 1008615Sbill fixspec(p_edata,dorigin); 1009615Sbill fixspec(p_end,borigin); 1010615Sbill } 1011615Sbill } 1012615Sbill 1013615Sbill fixspec(sym,offset) 1014615Sbill struct nlist *sym; 1015615Sbill long offset; 1016615Sbill { 1017615Sbill 1018615Sbill if(symx(sym) < symx(addsym) && sym!=0) 1019615Sbill sym->n_value += offset; 1020615Sbill } 1021615Sbill 1022615Sbill ldrsym(sp, val, type) 1023615Sbill register struct nlist *sp; 1024615Sbill long val; 1025615Sbill { 1026615Sbill 1027615Sbill if (sp == 0) 1028615Sbill return; 1029615Sbill if ((sp->n_type != N_EXT+N_UNDF || sp->n_value) && !Aflag) { 1030615Sbill printf("%s: ", sp->n_un.n_name); 1031615Sbill error(0, "user attempt to redfine loader-defined symbol"); 1032615Sbill return; 1033615Sbill } 1034615Sbill sp->n_type = type; 1035615Sbill sp->n_value = val; 1036615Sbill } 1037615Sbill 1038615Sbill off_t wroff; 1039615Sbill struct biobuf toutb; 1040615Sbill 1041615Sbill setupout() 1042615Sbill { 1043615Sbill int bss; 104416068Sralph struct stat stbuf; 1045898Sbill extern char *sys_errlist[]; 1046898Sbill extern int errno; 1047615Sbill 10483606Ssklower ofilemode = 0777 & ~umask(0); 10493606Ssklower biofd = creat(ofilename, 0666 & ofilemode); 1050898Sbill if (biofd < 0) { 1051898Sbill filname = ofilename; /* kludge */ 1052898Sbill archdr.ar_name[0] = 0; /* kludge */ 1053898Sbill error(1, sys_errlist[errno]); /* kludge */ 1054898Sbill } 105516068Sralph fstat(biofd, &stbuf); /* suppose file exists, wrong*/ 105616068Sralph if (stbuf.st_mode & 0111) { /* mode, ld fails? */ 105716068Sralph chmod(ofilename, stbuf.st_mode & 0666); 105816068Sralph ofilemode = stbuf.st_mode; 105916068Sralph } 1060615Sbill filhdr.a_magic = nflag ? NMAGIC : (zflag ? ZMAGIC : OMAGIC); 1061615Sbill filhdr.a_text = nflag ? tsize : 106212671Ssam round(tsize, zflag ? pagesize : sizeof (long)); 106312671Ssam filhdr.a_data = zflag ? round(dsize, pagesize) : dsize; 1064615Sbill bss = bsize - (filhdr.a_data - dsize); 1065615Sbill if (bss < 0) 1066615Sbill bss = 0; 1067615Sbill filhdr.a_bss = bss; 1068615Sbill filhdr.a_trsize = trsize; 1069615Sbill filhdr.a_drsize = drsize; 1070615Sbill filhdr.a_syms = sflag? 0: (ssize + (sizeof cursym)*symx(nextsym)); 1071615Sbill if (entrypt) { 1072615Sbill if (entrypt->n_type!=N_EXT+N_TEXT) 1073615Sbill error(0, "entry point not in text"); 1074615Sbill else 1075615Sbill filhdr.a_entry = entrypt->n_value; 1076615Sbill } else 1077615Sbill filhdr.a_entry = 0; 1078615Sbill filhdr.a_trsize = (rflag ? trsize:0); 1079615Sbill filhdr.a_drsize = (rflag ? drsize:0); 108016068Sralph tout = &toutb; 108116068Sralph bopen(tout, 0, stbuf.st_blksize); 1082615Sbill bwrite((char *)&filhdr, sizeof (filhdr), tout); 108316068Sralph if (zflag) 108416068Sralph bseek(tout, pagesize); 1085615Sbill wroff = N_TXTOFF(filhdr) + filhdr.a_text; 108616068Sralph outb(&dout, filhdr.a_data, stbuf.st_blksize); 1087615Sbill if (rflag) { 108816068Sralph outb(&trout, filhdr.a_trsize, stbuf.st_blksize); 108916068Sralph outb(&drout, filhdr.a_drsize, stbuf.st_blksize); 1090615Sbill } 1091615Sbill if (sflag==0 || xflag==0) { 109216068Sralph outb(&sout, filhdr.a_syms, stbuf.st_blksize); 1093615Sbill wroff += sizeof (offset); 109416068Sralph outb(&strout, 0, stbuf.st_blksize); 1095615Sbill } 1096615Sbill } 1097615Sbill 109816068Sralph outb(bp, inc, bufsize) 1099615Sbill register struct biobuf **bp; 1100615Sbill { 1101615Sbill 1102615Sbill *bp = (struct biobuf *)malloc(sizeof (struct biobuf)); 1103615Sbill if (*bp == 0) 1104615Sbill error(1, "ran out of memory (outb)"); 110516068Sralph bopen(*bp, wroff, bufsize); 1106615Sbill wroff += inc; 1107615Sbill } 1108615Sbill 1109615Sbill load2arg(acp) 1110615Sbill char *acp; 1111615Sbill { 1112615Sbill register char *cp; 1113615Sbill off_t loc; 1114615Sbill 1115615Sbill cp = acp; 1116615Sbill if (getfile(cp) == 0) { 1117615Sbill while (*cp) 1118615Sbill cp++; 1119615Sbill while (cp >= acp && *--cp != '/'); 1120615Sbill mkfsym(++cp); 1121615Sbill load2(0L); 1122615Sbill } else { /* scan archive members referenced */ 1123615Sbill for (;;) { 1124615Sbill if (clibseg->li_used2 == clibseg->li_used) { 1125615Sbill if (clibseg->li_used < NROUT) 1126615Sbill error(1, "libseg botch"); 1127615Sbill clibseg++; 1128615Sbill } 1129615Sbill loc = clibseg->li_first[clibseg->li_used2++]; 1130615Sbill if (loc == -1) 1131615Sbill break; 1132615Sbill dseek(&text, loc, (long)sizeof(archdr)); 1133615Sbill getarhdr(); 1134615Sbill mkfsym(archdr.ar_name); 1135615Sbill load2(loc + (long)sizeof(archdr)); 1136615Sbill } 1137615Sbill } 1138615Sbill close(infil); 1139615Sbill } 1140615Sbill 1141615Sbill load2(loc) 1142615Sbill long loc; 1143615Sbill { 1144615Sbill int size; 1145615Sbill register struct nlist *sp; 1146615Sbill register struct local *lp; 1147615Sbill register int symno, i; 1148615Sbill int type; 1149615Sbill 1150615Sbill readhdr(loc); 1151650Sbill if (!funding) { 1152615Sbill ctrel = torigin; 1153615Sbill cdrel += dorigin; 1154615Sbill cbrel += borigin; 1155615Sbill } 1156615Sbill /* 1157615Sbill * Reread the symbol table, recording the numbering 1158615Sbill * of symbols for fixing external references. 1159615Sbill */ 1160615Sbill for (i = 0; i < LHSIZ; i++) 1161615Sbill lochash[i] = 0; 1162615Sbill clocseg = locseg; 1163615Sbill clocseg->lo_used = 0; 1164615Sbill symno = -1; 1165615Sbill loc += N_TXTOFF(filhdr); 1166615Sbill dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1167615Sbill filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms, sizeof(off_t)); 1168615Sbill mget(&size, sizeof(size), &text); 1169615Sbill dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1170615Sbill filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms+sizeof(off_t), 1171615Sbill size - sizeof(off_t)); 1172615Sbill curstr = (char *)malloc(size); 1173615Sbill if (curstr == NULL) 1174615Sbill error(1, "out of space reading string table (pass 2)"); 1175615Sbill mget(curstr+sizeof(off_t), size-sizeof(off_t), &text); 1176615Sbill dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1177615Sbill filhdr.a_trsize+filhdr.a_drsize, filhdr.a_syms); 1178615Sbill while (text.size > 0) { 1179615Sbill symno++; 1180615Sbill mget((char *)&cursym, sizeof(struct nlist), &text); 1181615Sbill if (cursym.n_un.n_strx) { 1182615Sbill if (cursym.n_un.n_strx<sizeof(size) || 1183615Sbill cursym.n_un.n_strx>=size) 1184615Sbill error(1, "bad string table index (pass 2)"); 1185615Sbill cursym.n_un.n_name = curstr + cursym.n_un.n_strx; 1186615Sbill } 1187615Sbill /* inline expansion of symreloc() */ 1188615Sbill switch (cursym.n_type & 017) { 1189615Sbill 1190615Sbill case N_TEXT: 1191615Sbill case N_EXT+N_TEXT: 1192615Sbill cursym.n_value += ctrel; 1193615Sbill break; 1194615Sbill case N_DATA: 1195615Sbill case N_EXT+N_DATA: 1196615Sbill cursym.n_value += cdrel; 1197615Sbill break; 1198615Sbill case N_BSS: 1199615Sbill case N_EXT+N_BSS: 1200615Sbill cursym.n_value += cbrel; 1201615Sbill break; 1202615Sbill case N_EXT+N_UNDF: 1203615Sbill break; 1204615Sbill default: 1205615Sbill if (cursym.n_type&N_EXT) 1206615Sbill cursym.n_type = N_EXT+N_ABS; 1207615Sbill } 1208615Sbill /* end inline expansion of symreloc() */ 1209615Sbill type = cursym.n_type; 1210898Sbill if (yflag && cursym.n_un.n_name) 1211898Sbill for (i = 0; i < yflag; i++) 1212898Sbill /* fast check for 2d character! */ 1213898Sbill if (ytab[i][1] == cursym.n_un.n_name[1] && 1214898Sbill !strcmp(ytab[i], cursym.n_un.n_name)) { 1215898Sbill tracesym(); 1216898Sbill break; 1217898Sbill } 1218615Sbill if ((type&N_EXT) == 0) { 1219615Sbill if (!sflag&&!xflag&& 1220615Sbill (!Xflag||cursym.n_un.n_name[0]!='L'||type&N_STAB)) 1221615Sbill symwrite(&cursym, sout); 1222615Sbill continue; 1223615Sbill } 1224615Sbill if (funding) 1225615Sbill continue; 1226615Sbill if ((sp = *lookup()) == 0) 1227615Sbill error(1, "internal error: symbol not found"); 1228615Sbill if (cursym.n_type == N_EXT+N_UNDF) { 1229615Sbill if (clocseg->lo_used == NSYMPR) { 1230615Sbill if (++clocseg == &locseg[NSEG]) 1231615Sbill error(1, "local symbol overflow"); 1232615Sbill clocseg->lo_used = 0; 1233615Sbill } 1234615Sbill if (clocseg->lo_first == 0) { 1235615Sbill clocseg->lo_first = (struct local *) 1236615Sbill malloc(NSYMPR * sizeof (struct local)); 1237615Sbill if (clocseg->lo_first == 0) 1238615Sbill error(1, "out of memory (clocseg)"); 1239615Sbill } 1240615Sbill lp = &clocseg->lo_first[clocseg->lo_used++]; 1241615Sbill lp->l_index = symno; 1242615Sbill lp->l_symbol = sp; 1243615Sbill lp->l_link = lochash[symno % LHSIZ]; 1244615Sbill lochash[symno % LHSIZ] = lp; 1245615Sbill continue; 1246615Sbill } 1247615Sbill if (cursym.n_type & N_STAB) 1248615Sbill continue; 1249615Sbill if (cursym.n_type!=sp->n_type || cursym.n_value!=sp->n_value) { 1250615Sbill printf("%s: ", cursym.n_un.n_name); 1251615Sbill error(0, "multiply defined"); 1252615Sbill } 1253615Sbill } 1254615Sbill if (funding) 1255615Sbill return; 1256615Sbill dseek(&text, loc, filhdr.a_text); 1257615Sbill dseek(&reloc, loc+filhdr.a_text+filhdr.a_data, filhdr.a_trsize); 1258650Sbill load2td(ctrel, torigin - textbase, tout, trout); 1259615Sbill dseek(&text, loc+filhdr.a_text, filhdr.a_data); 1260615Sbill dseek(&reloc, loc+filhdr.a_text+filhdr.a_data+filhdr.a_trsize, 1261615Sbill filhdr.a_drsize); 1262650Sbill load2td(cdrel, dorigin - database, dout, drout); 1263615Sbill while (filhdr.a_data & (sizeof(long)-1)) { 1264615Sbill bputc(0, dout); 1265615Sbill filhdr.a_data++; 1266615Sbill } 1267615Sbill torigin += filhdr.a_text; 12681752Sbill dorigin += round(filhdr.a_data, sizeof (long)); 12691752Sbill borigin += round(filhdr.a_bss, sizeof (long)); 1270615Sbill free(curstr); 1271615Sbill } 1272615Sbill 1273898Sbill struct tynames { 1274898Sbill int ty_value; 1275898Sbill char *ty_name; 1276898Sbill } tynames[] = { 1277898Sbill N_UNDF, "undefined", 1278898Sbill N_ABS, "absolute", 1279898Sbill N_TEXT, "text", 1280898Sbill N_DATA, "data", 1281898Sbill N_BSS, "bss", 1282898Sbill N_COMM, "common", 1283898Sbill 0, 0, 1284898Sbill }; 1285898Sbill 1286898Sbill tracesym() 1287898Sbill { 1288898Sbill register struct tynames *tp; 1289898Sbill 1290898Sbill if (cursym.n_type & N_STAB) 1291898Sbill return; 1292898Sbill printf("%s", filname); 1293898Sbill if (archdr.ar_name[0]) 1294898Sbill printf("(%s)", archdr.ar_name); 1295898Sbill printf(": "); 1296898Sbill if ((cursym.n_type&N_TYPE) == N_UNDF && cursym.n_value) { 1297898Sbill printf("definition of common %s size %d\n", 1298898Sbill cursym.n_un.n_name, cursym.n_value); 1299898Sbill return; 1300898Sbill } 1301898Sbill for (tp = tynames; tp->ty_name; tp++) 1302898Sbill if (tp->ty_value == (cursym.n_type&N_TYPE)) 1303898Sbill break; 1304898Sbill printf((cursym.n_type&N_TYPE) ? "definition of" : "reference to"); 1305898Sbill if (cursym.n_type&N_EXT) 1306898Sbill printf(" external"); 1307898Sbill if (tp->ty_name) 1308898Sbill printf(" %s", tp->ty_name); 1309898Sbill printf(" %s\n", cursym.n_un.n_name); 1310898Sbill } 1311898Sbill 1312650Sbill /* 1313650Sbill * This routine relocates the single text or data segment argument. 1314650Sbill * Offsets from external symbols are resolved by adding the value 1315650Sbill * of the external symbols. Non-external reference are updated to account 1316650Sbill * for the relative motion of the segments (ctrel, cdrel, ...). If 1317650Sbill * a relocation was pc-relative, then we update it to reflect the 1318650Sbill * change in the positioning of the segments by adding the displacement 1319650Sbill * of the referenced segment and subtracting the displacement of the 1320650Sbill * current segment (creloc). 1321650Sbill * 1322650Sbill * If we are saving the relocation information, then we increase 1323650Sbill * each relocation datum address by our base position in the new segment. 1324650Sbill */ 1325650Sbill load2td(creloc, position, b1, b2) 1326650Sbill long creloc, offset; 1327615Sbill struct biobuf *b1, *b2; 1328615Sbill { 1329615Sbill register struct nlist *sp; 1330615Sbill register struct local *lp; 1331615Sbill long tw; 1332615Sbill register struct relocation_info *rp, *rpend; 1333615Sbill struct relocation_info *relp; 1334615Sbill char *codep; 1335615Sbill register char *cp; 1336615Sbill int relsz, codesz; 1337615Sbill 1338615Sbill relsz = reloc.size; 1339615Sbill relp = (struct relocation_info *)malloc(relsz); 1340615Sbill codesz = text.size; 1341615Sbill codep = (char *)malloc(codesz); 1342615Sbill if (relp == 0 || codep == 0) 1343615Sbill error(1, "out of memory (load2td)"); 1344615Sbill mget((char *)relp, relsz, &reloc); 1345615Sbill rpend = &relp[relsz / sizeof (struct relocation_info)]; 1346615Sbill mget(codep, codesz, &text); 1347615Sbill for (rp = relp; rp < rpend; rp++) { 1348615Sbill cp = codep + rp->r_address; 1349650Sbill /* 1350650Sbill * Pick up previous value at location to be relocated. 1351650Sbill */ 1352615Sbill switch (rp->r_length) { 1353615Sbill 1354615Sbill case 0: /* byte */ 1355615Sbill tw = *cp; 1356615Sbill break; 1357615Sbill 1358615Sbill case 1: /* word */ 1359615Sbill tw = *(short *)cp; 1360615Sbill break; 1361615Sbill 1362615Sbill case 2: /* long */ 1363615Sbill tw = *(long *)cp; 1364615Sbill break; 1365615Sbill 1366615Sbill default: 1367615Sbill error(1, "load2td botch: bad length"); 1368615Sbill } 1369650Sbill /* 1370650Sbill * If relative to an external which is defined, 1371650Sbill * resolve to a simpler kind of reference in the 1372650Sbill * result file. If the external is undefined, just 1373650Sbill * convert the symbol number to the number of the 1374650Sbill * symbol in the result file and leave it undefined. 1375650Sbill */ 1376615Sbill if (rp->r_extern) { 1377650Sbill /* 1378650Sbill * Search the hash table which maps local 1379650Sbill * symbol numbers to symbol tables entries 1380650Sbill * in the new a.out file. 1381650Sbill */ 1382615Sbill lp = lochash[rp->r_symbolnum % LHSIZ]; 1383615Sbill while (lp->l_index != rp->r_symbolnum) { 1384615Sbill lp = lp->l_link; 1385615Sbill if (lp == 0) 1386615Sbill error(1, "local symbol botch"); 1387615Sbill } 1388615Sbill sp = lp->l_symbol; 1389615Sbill if (sp->n_type == N_EXT+N_UNDF) 1390615Sbill rp->r_symbolnum = nsym+symx(sp); 1391615Sbill else { 1392615Sbill rp->r_symbolnum = sp->n_type & N_TYPE; 1393615Sbill tw += sp->n_value; 1394615Sbill rp->r_extern = 0; 1395615Sbill } 1396615Sbill } else switch (rp->r_symbolnum & N_TYPE) { 1397650Sbill /* 1398650Sbill * Relocation is relative to the loaded position 1399650Sbill * of another segment. Update by the change in position 1400650Sbill * of that segment. 1401650Sbill */ 1402615Sbill case N_TEXT: 1403615Sbill tw += ctrel; 1404615Sbill break; 1405615Sbill case N_DATA: 1406615Sbill tw += cdrel; 1407615Sbill break; 1408615Sbill case N_BSS: 1409615Sbill tw += cbrel; 1410615Sbill break; 1411615Sbill case N_ABS: 1412615Sbill break; 1413615Sbill default: 1414615Sbill error(1, "relocation format botch (symbol type))"); 1415615Sbill } 1416650Sbill /* 1417650Sbill * Relocation is pc relative, so decrease the relocation 1418650Sbill * by the amount the current segment is displaced. 1419650Sbill * (E.g if we are a relative reference to a text location 1420650Sbill * from data space, we added the increase in the text address 1421650Sbill * above, and subtract the increase in our (data) address 1422650Sbill * here, leaving the net change the relative change in the 1423650Sbill * positioning of our text and data segments.) 1424650Sbill */ 1425615Sbill if (rp->r_pcrel) 1426615Sbill tw -= creloc; 1427650Sbill /* 1428650Sbill * Put the value back in the segment, 1429650Sbill * while checking for overflow. 1430650Sbill */ 1431615Sbill switch (rp->r_length) { 1432615Sbill 1433615Sbill case 0: /* byte */ 1434615Sbill if (tw < -128 || tw > 127) 1435615Sbill error(0, "byte displacement overflow"); 1436615Sbill *cp = tw; 1437615Sbill break; 1438615Sbill case 1: /* word */ 1439615Sbill if (tw < -32768 || tw > 32767) 1440615Sbill error(0, "word displacement overflow"); 1441615Sbill *(short *)cp = tw; 1442615Sbill break; 1443615Sbill case 2: /* long */ 1444615Sbill *(long *)cp = tw; 1445615Sbill break; 1446615Sbill } 1447650Sbill /* 1448650Sbill * If we are saving relocation information, 1449650Sbill * we must convert the address in the segment from 1450650Sbill * the old .o file into an address in the segment in 1451650Sbill * the new a.out, by adding the position of our 1452650Sbill * segment in the new larger segment. 1453650Sbill */ 1454615Sbill if (rflag) 1455650Sbill rp->r_address += position; 1456615Sbill } 1457615Sbill bwrite(codep, codesz, b1); 1458615Sbill if (rflag) 1459615Sbill bwrite(relp, relsz, b2); 1460615Sbill cfree((char *)relp); 1461615Sbill cfree(codep); 1462615Sbill } 1463615Sbill 1464615Sbill finishout() 1465615Sbill { 1466615Sbill register int i; 1467615Sbill int nsymt; 1468615Sbill 1469615Sbill if (sflag==0) { 1470615Sbill nsymt = symx(nextsym); 1471615Sbill for (i = 0; i < nsymt; i++) 1472615Sbill symwrite(xsym(i), sout); 1473615Sbill bwrite(&offset, sizeof offset, sout); 1474615Sbill } 1475615Sbill if (!ofilfnd) { 1476615Sbill unlink("a.out"); 1477898Sbill if (link("l.out", "a.out") < 0) 1478898Sbill error(1, "cannot move l.out to a.out"); 1479615Sbill ofilename = "a.out"; 1480615Sbill } 1481615Sbill delarg = errlev; 1482615Sbill delexit(); 1483615Sbill } 1484615Sbill 1485615Sbill mkfsym(s) 1486615Sbill char *s; 1487615Sbill { 1488615Sbill 1489615Sbill if (sflag || xflag) 1490615Sbill return; 1491615Sbill cursym.n_un.n_name = s; 1492615Sbill cursym.n_type = N_TEXT; 1493615Sbill cursym.n_value = torigin; 1494615Sbill symwrite(&cursym, sout); 1495615Sbill } 1496615Sbill 1497615Sbill getarhdr() 1498615Sbill { 1499615Sbill register char *cp; 1500615Sbill 1501615Sbill mget((char *)&archdr, sizeof archdr, &text); 1502615Sbill for (cp=archdr.ar_name; cp<&archdr.ar_name[sizeof(archdr.ar_name)];) 1503615Sbill if (*cp++ == ' ') { 1504615Sbill cp[-1] = 0; 1505615Sbill return; 1506615Sbill } 1507615Sbill } 1508615Sbill 1509615Sbill mget(loc, n, sp) 1510615Sbill register STREAM *sp; 1511615Sbill register char *loc; 1512615Sbill { 1513615Sbill register char *p; 1514615Sbill register int take; 1515615Sbill 1516615Sbill top: 1517615Sbill if (n == 0) 1518615Sbill return; 1519615Sbill if (sp->size && sp->nibuf) { 1520615Sbill p = sp->ptr; 1521615Sbill take = sp->size; 1522615Sbill if (take > sp->nibuf) 1523615Sbill take = sp->nibuf; 1524615Sbill if (take > n) 1525615Sbill take = n; 1526615Sbill n -= take; 1527615Sbill sp->size -= take; 1528615Sbill sp->nibuf -= take; 1529615Sbill sp->pos += take; 1530615Sbill do 1531615Sbill *loc++ = *p++; 1532615Sbill while (--take > 0); 1533615Sbill sp->ptr = p; 1534615Sbill goto top; 1535615Sbill } 153616068Sralph if (n > p_blksize) { 153716068Sralph take = n - n % p_blksize; 153816068Sralph lseek(infil, (sp->bno+1)<<p_blkshift, 0); 1539615Sbill if (take > sp->size || read(infil, loc, take) != take) 1540615Sbill error(1, "premature EOF"); 1541615Sbill loc += take; 1542615Sbill n -= take; 1543615Sbill sp->size -= take; 1544615Sbill sp->pos += take; 154516068Sralph dseek(sp, (sp->bno+1+(take>>p_blkshift))<<p_blkshift, -1); 1546615Sbill goto top; 1547615Sbill } 1548615Sbill *loc++ = get(sp); 1549615Sbill --n; 1550615Sbill goto top; 1551615Sbill } 1552615Sbill 1553615Sbill symwrite(sp, bp) 1554615Sbill struct nlist *sp; 1555615Sbill struct biobuf *bp; 1556615Sbill { 1557615Sbill register int len; 1558615Sbill register char *str; 1559615Sbill 1560615Sbill str = sp->n_un.n_name; 1561615Sbill if (str) { 1562615Sbill sp->n_un.n_strx = offset; 1563615Sbill len = strlen(str) + 1; 1564615Sbill bwrite(str, len, strout); 1565615Sbill offset += len; 1566615Sbill } 1567615Sbill bwrite(sp, sizeof (*sp), bp); 1568615Sbill sp->n_un.n_name = str; 1569615Sbill } 1570615Sbill 1571615Sbill dseek(sp, loc, s) 1572615Sbill register STREAM *sp; 1573615Sbill long loc, s; 1574615Sbill { 1575615Sbill register PAGE *p; 1576615Sbill register b, o; 1577615Sbill int n; 1578615Sbill 157916068Sralph b = loc>>p_blkshift; 158016068Sralph o = loc&p_blkmask; 1581615Sbill if (o&01) 1582615Sbill error(1, "loader error; odd offset"); 1583615Sbill --sp->pno->nuser; 1584615Sbill if ((p = &page[0])->bno!=b && (p = &page[1])->bno!=b) 1585615Sbill if (p->nuser==0 || (p = &page[0])->nuser==0) { 1586615Sbill if (page[0].nuser==0 && page[1].nuser==0) 1587615Sbill if (page[0].bno < page[1].bno) 1588615Sbill p = &page[0]; 1589615Sbill p->bno = b; 159016068Sralph lseek(infil, loc & ~(long)p_blkmask, 0); 159116068Sralph if ((n = read(infil, p->buff, p_blksize)) < 0) 1592615Sbill n = 0; 1593615Sbill p->nibuf = n; 159416068Sralph } else 159516068Sralph error(1, "botch: no pages"); 1596615Sbill ++p->nuser; 1597615Sbill sp->bno = b; 1598615Sbill sp->pno = p; 1599615Sbill if (s != -1) {sp->size = s; sp->pos = 0;} 1600615Sbill sp->ptr = (char *)(p->buff + o); 1601615Sbill if ((sp->nibuf = p->nibuf-o) <= 0) 1602615Sbill sp->size = 0; 1603615Sbill } 1604615Sbill 1605615Sbill char 1606615Sbill get(asp) 1607615Sbill STREAM *asp; 1608615Sbill { 1609615Sbill register STREAM *sp; 1610615Sbill 1611615Sbill sp = asp; 1612615Sbill if ((sp->nibuf -= sizeof(char)) < 0) { 161316068Sralph dseek(sp, ((long)(sp->bno+1)<<p_blkshift), (long)-1); 1614615Sbill sp->nibuf -= sizeof(char); 1615615Sbill } 1616615Sbill if ((sp->size -= sizeof(char)) <= 0) { 1617615Sbill if (sp->size < 0) 1618615Sbill error(1, "premature EOF"); 1619615Sbill ++fpage.nuser; 1620615Sbill --sp->pno->nuser; 1621615Sbill sp->pno = (PAGE *) &fpage; 1622615Sbill } 1623615Sbill sp->pos += sizeof(char); 1624615Sbill return(*sp->ptr++); 1625615Sbill } 1626615Sbill 1627615Sbill getfile(acp) 1628615Sbill char *acp; 1629615Sbill { 1630615Sbill register int c; 1631615Sbill char arcmag[SARMAG+1]; 1632615Sbill struct stat stb; 1633615Sbill 1634615Sbill archdr.ar_name[0] = '\0'; 163517133Ssam filname = acp; 163617133Ssam if (filname[0] == '-' && filname[1] == 'l') 163717133Ssam infil = libopen(filname + 2, O_RDONLY); 163817133Ssam else 163917133Ssam infil = open(filname, O_RDONLY); 164017133Ssam if (infil < 0) 1641615Sbill error(1, "cannot open"); 164216068Sralph fstat(infil, &stb); 1643615Sbill page[0].bno = page[1].bno = -1; 1644615Sbill page[0].nuser = page[1].nuser = 0; 164516068Sralph c = stb.st_blksize; 164616068Sralph if (c == 0 || (c & (c - 1)) != 0) { 164716068Sralph /* use default size if not a power of two */ 164816068Sralph c = BLKSIZE; 164916068Sralph } 165016068Sralph if (p_blksize != c) { 165116068Sralph p_blksize = c; 165216068Sralph p_blkmask = c - 1; 165316068Sralph for (p_blkshift = 0; c > 1 ; p_blkshift++) 165416068Sralph c >>= 1; 165516068Sralph if (page[0].buff != NULL) 165616068Sralph free(page[0].buff); 165716068Sralph page[0].buff = (char *)malloc(p_blksize); 165816068Sralph if (page[0].buff == NULL) 165916068Sralph error(1, "ran out of memory (getfile)"); 166016068Sralph if (page[1].buff != NULL) 166116068Sralph free(page[1].buff); 166216068Sralph page[1].buff = (char *)malloc(p_blksize); 166316068Sralph if (page[1].buff == NULL) 166416068Sralph error(1, "ran out of memory (getfile)"); 166516068Sralph } 1666615Sbill text.pno = reloc.pno = (PAGE *) &fpage; 1667615Sbill fpage.nuser = 2; 1668615Sbill dseek(&text, 0L, SARMAG); 1669615Sbill if (text.size <= 0) 1670615Sbill error(1, "premature EOF"); 1671615Sbill mget((char *)arcmag, SARMAG, &text); 1672615Sbill arcmag[SARMAG] = 0; 1673615Sbill if (strcmp(arcmag, ARMAG)) 1674615Sbill return (0); 1675615Sbill dseek(&text, SARMAG, sizeof archdr); 167617133Ssam if (text.size <= 0) 1677615Sbill return (1); 1678615Sbill getarhdr(); 1679615Sbill if (strncmp(archdr.ar_name, "__.SYMDEF", sizeof(archdr.ar_name)) != 0) 1680615Sbill return (1); 1681615Sbill return (stb.st_mtime > atol(archdr.ar_date) ? 3 : 2); 1682615Sbill } 1683615Sbill 168417133Ssam /* 168517133Ssam * Search for a library with given name 168617133Ssam * using the directory search array. 168717133Ssam */ 168817133Ssam libopen(name, oflags) 168917133Ssam char *name; 169017133Ssam int oflags; 169117133Ssam { 169217133Ssam register char *p, *cp; 169317133Ssam register int i; 169417133Ssam static char buf[MAXPATHLEN+1]; 169517133Ssam int fd = -1; 169617133Ssam 169717133Ssam if (*name == '\0') /* backwards compat */ 169817133Ssam name = "a"; 169917133Ssam for (i = 0; i < ndir && fd == -1; i++) { 170017133Ssam p = buf; 170117133Ssam for (cp = dirs[i]; *cp; *p++ = *cp++) 170217133Ssam ; 170317133Ssam *p++ = '/'; 170417133Ssam for (cp = "lib"; *cp; *p++ = *cp++) 170517133Ssam ; 170617133Ssam for (cp = name; *cp; *p++ = *cp++) 170717133Ssam ; 170817133Ssam cp = ".a"; 170917133Ssam while (*p++ = *cp++) 171017133Ssam ; 171117133Ssam fd = open(buf, oflags); 171217133Ssam } 171317133Ssam if (fd != -1) 171417133Ssam filname = buf; 171517133Ssam return (fd); 171617133Ssam } 171717133Ssam 1718615Sbill struct nlist ** 1719615Sbill lookup() 1720615Sbill { 1721615Sbill register int sh; 1722615Sbill register struct nlist **hp; 1723615Sbill register char *cp, *cp1; 1724615Sbill register struct symseg *gp; 1725615Sbill register int i; 1726615Sbill 1727615Sbill sh = 0; 1728615Sbill for (cp = cursym.n_un.n_name; *cp;) 1729615Sbill sh = (sh<<1) + *cp++; 1730615Sbill sh = (sh & 0x7fffffff) % HSIZE; 1731615Sbill for (gp = symseg; gp < &symseg[NSEG]; gp++) { 1732615Sbill if (gp->sy_first == 0) { 1733615Sbill gp->sy_first = (struct nlist *) 1734615Sbill calloc(NSYM, sizeof (struct nlist)); 1735615Sbill gp->sy_hfirst = (struct nlist **) 1736615Sbill calloc(HSIZE, sizeof (struct nlist *)); 1737615Sbill if (gp->sy_first == 0 || gp->sy_hfirst == 0) 1738615Sbill error(1, "ran out of space for symbol table"); 1739615Sbill gp->sy_last = gp->sy_first + NSYM; 1740615Sbill gp->sy_hlast = gp->sy_hfirst + HSIZE; 1741615Sbill } 1742615Sbill if (gp > csymseg) 1743615Sbill csymseg = gp; 1744615Sbill hp = gp->sy_hfirst + sh; 1745615Sbill i = 1; 1746615Sbill do { 1747615Sbill if (*hp == 0) { 1748615Sbill if (gp->sy_used == NSYM) 1749615Sbill break; 1750615Sbill return (hp); 1751615Sbill } 1752615Sbill cp1 = (*hp)->n_un.n_name; 1753615Sbill for (cp = cursym.n_un.n_name; *cp == *cp1++;) 1754615Sbill if (*cp++ == 0) 1755615Sbill return (hp); 1756615Sbill hp += i; 1757615Sbill i += 2; 1758615Sbill if (hp >= gp->sy_hlast) 1759615Sbill hp -= HSIZE; 1760615Sbill } while (i < HSIZE); 1761615Sbill if (i > HSIZE) 1762615Sbill error(1, "hash table botch"); 1763615Sbill } 1764615Sbill error(1, "symbol table overflow"); 1765615Sbill /*NOTREACHED*/ 1766615Sbill } 1767615Sbill 1768615Sbill symfree(saved) 1769615Sbill struct nlist *saved; 1770615Sbill { 1771615Sbill register struct symseg *gp; 1772615Sbill register struct nlist *sp; 1773615Sbill 1774615Sbill for (gp = csymseg; gp >= symseg; gp--, csymseg--) { 1775615Sbill sp = gp->sy_first + gp->sy_used; 1776615Sbill if (sp == saved) { 1777615Sbill nextsym = sp; 1778615Sbill return; 1779615Sbill } 1780615Sbill for (sp--; sp >= gp->sy_first; sp--) { 1781615Sbill gp->sy_hfirst[sp->n_hash] = 0; 1782615Sbill gp->sy_used--; 1783615Sbill if (sp == saved) { 1784615Sbill nextsym = sp; 1785615Sbill return; 1786615Sbill } 1787615Sbill } 1788615Sbill } 1789615Sbill if (saved == 0) 1790615Sbill return; 1791615Sbill error(1, "symfree botch"); 1792615Sbill } 1793615Sbill 1794615Sbill struct nlist ** 1795615Sbill slookup(s) 1796615Sbill char *s; 1797615Sbill { 1798615Sbill 1799615Sbill cursym.n_un.n_name = s; 1800615Sbill cursym.n_type = N_EXT+N_UNDF; 1801615Sbill cursym.n_value = 0; 1802615Sbill return (lookup()); 1803615Sbill } 1804615Sbill 1805615Sbill enter(hp) 1806615Sbill register struct nlist **hp; 1807615Sbill { 1808615Sbill register struct nlist *sp; 1809615Sbill 1810615Sbill if (*hp==0) { 1811615Sbill if (hp < csymseg->sy_hfirst || hp >= csymseg->sy_hlast) 1812615Sbill error(1, "enter botch"); 1813615Sbill *hp = lastsym = sp = csymseg->sy_first + csymseg->sy_used; 1814615Sbill csymseg->sy_used++; 1815615Sbill sp->n_un.n_name = cursym.n_un.n_name; 1816615Sbill sp->n_type = cursym.n_type; 1817615Sbill sp->n_hash = hp - csymseg->sy_hfirst; 1818615Sbill sp->n_value = cursym.n_value; 1819615Sbill nextsym = lastsym + 1; 1820615Sbill return(1); 1821615Sbill } else { 1822615Sbill lastsym = *hp; 1823615Sbill return(0); 1824615Sbill } 1825615Sbill } 1826615Sbill 1827615Sbill symx(sp) 1828615Sbill struct nlist *sp; 1829615Sbill { 1830615Sbill register struct symseg *gp; 1831615Sbill 1832615Sbill if (sp == 0) 1833615Sbill return (0); 1834615Sbill for (gp = csymseg; gp >= symseg; gp--) 1835615Sbill /* <= is sloppy so nextsym will always work */ 1836615Sbill if (sp >= gp->sy_first && sp <= gp->sy_last) 1837615Sbill return ((gp - symseg) * NSYM + sp - gp->sy_first); 1838615Sbill error(1, "symx botch"); 1839615Sbill /*NOTREACHED*/ 1840615Sbill } 1841615Sbill 1842615Sbill symreloc() 1843615Sbill { 1844615Sbill if(funding) return; 1845615Sbill switch (cursym.n_type & 017) { 1846615Sbill 1847615Sbill case N_TEXT: 1848615Sbill case N_EXT+N_TEXT: 1849615Sbill cursym.n_value += ctrel; 1850615Sbill return; 1851615Sbill 1852615Sbill case N_DATA: 1853615Sbill case N_EXT+N_DATA: 1854615Sbill cursym.n_value += cdrel; 1855615Sbill return; 1856615Sbill 1857615Sbill case N_BSS: 1858615Sbill case N_EXT+N_BSS: 1859615Sbill cursym.n_value += cbrel; 1860615Sbill return; 1861615Sbill 1862615Sbill case N_EXT+N_UNDF: 1863615Sbill return; 1864615Sbill 1865615Sbill default: 1866615Sbill if (cursym.n_type&N_EXT) 1867615Sbill cursym.n_type = N_EXT+N_ABS; 1868615Sbill return; 1869615Sbill } 1870615Sbill } 1871615Sbill 1872615Sbill error(n, s) 1873615Sbill char *s; 1874615Sbill { 1875898Sbill 1876615Sbill if (errlev==0) 1877615Sbill printf("ld:"); 1878615Sbill if (filname) { 1879615Sbill printf("%s", filname); 1880615Sbill if (n != -1 && archdr.ar_name[0]) 1881615Sbill printf("(%s)", archdr.ar_name); 1882615Sbill printf(": "); 1883615Sbill } 1884615Sbill printf("%s\n", s); 1885615Sbill if (n == -1) 1886615Sbill return; 1887615Sbill if (n) 1888615Sbill delexit(); 1889615Sbill errlev = 2; 1890615Sbill } 1891615Sbill 1892615Sbill readhdr(loc) 1893615Sbill off_t loc; 1894615Sbill { 1895615Sbill 1896615Sbill dseek(&text, loc, (long)sizeof(filhdr)); 1897615Sbill mget((short *)&filhdr, sizeof(filhdr), &text); 1898615Sbill if (N_BADMAG(filhdr)) { 1899615Sbill if (filhdr.a_magic == OARMAG) 1900615Sbill error(1, "old archive"); 1901615Sbill error(1, "bad magic number"); 1902615Sbill } 1903615Sbill if (filhdr.a_text&01 || filhdr.a_data&01) 1904615Sbill error(1, "text/data size odd"); 1905615Sbill if (filhdr.a_magic == NMAGIC || filhdr.a_magic == ZMAGIC) { 190612671Ssam cdrel = -round(filhdr.a_text, pagesize); 1907615Sbill cbrel = cdrel - filhdr.a_data; 1908615Sbill } else if (filhdr.a_magic == OMAGIC) { 1909615Sbill cdrel = -filhdr.a_text; 1910615Sbill cbrel = cdrel - filhdr.a_data; 1911615Sbill } else 1912615Sbill error(1, "bad format"); 1913615Sbill } 1914615Sbill 1915615Sbill round(v, r) 1916615Sbill int v; 1917615Sbill u_long r; 1918615Sbill { 1919615Sbill 1920615Sbill r--; 1921615Sbill v += r; 1922615Sbill v &= ~(long)r; 1923615Sbill return(v); 1924615Sbill } 1925615Sbill 1926615Sbill #define NSAVETAB 8192 1927615Sbill char *savetab; 1928615Sbill int saveleft; 1929615Sbill 1930615Sbill char * 1931615Sbill savestr(cp) 1932615Sbill register char *cp; 1933615Sbill { 1934615Sbill register int len; 1935615Sbill 1936615Sbill len = strlen(cp) + 1; 1937615Sbill if (len > saveleft) { 1938615Sbill saveleft = NSAVETAB; 1939615Sbill if (len > saveleft) 1940615Sbill saveleft = len; 194117133Ssam savetab = malloc(saveleft); 1942615Sbill if (savetab == 0) 1943615Sbill error(1, "ran out of memory (savestr)"); 1944615Sbill } 1945615Sbill strncpy(savetab, cp, len); 1946615Sbill cp = savetab; 1947615Sbill savetab += len; 1948615Sbill saveleft -= len; 1949615Sbill return (cp); 1950615Sbill } 1951615Sbill 195216068Sralph bopen(bp, off, bufsize) 195316068Sralph register struct biobuf *bp; 1954615Sbill { 1955615Sbill 195617133Ssam bp->b_ptr = bp->b_buf = malloc(bufsize); 195716068Sralph if (bp->b_ptr == (char *)0) 195816068Sralph error(1, "ran out of memory (bopen)"); 195916068Sralph bp->b_bufsize = bufsize; 196016068Sralph bp->b_nleft = bufsize - (off % bufsize); 1961615Sbill bp->b_off = off; 1962615Sbill bp->b_link = biobufs; 1963615Sbill biobufs = bp; 1964615Sbill } 1965615Sbill 1966615Sbill int bwrerror; 1967615Sbill 1968615Sbill bwrite(p, cnt, bp) 1969615Sbill register char *p; 1970615Sbill register int cnt; 1971615Sbill register struct biobuf *bp; 1972615Sbill { 1973615Sbill register int put; 1974615Sbill register char *to; 1975615Sbill 1976615Sbill top: 1977615Sbill if (cnt == 0) 1978615Sbill return; 1979615Sbill if (bp->b_nleft) { 1980615Sbill put = bp->b_nleft; 1981615Sbill if (put > cnt) 1982615Sbill put = cnt; 1983615Sbill bp->b_nleft -= put; 1984615Sbill to = bp->b_ptr; 1985615Sbill asm("movc3 r8,(r11),(r7)"); 1986615Sbill bp->b_ptr += put; 1987615Sbill p += put; 1988615Sbill cnt -= put; 1989615Sbill goto top; 1990615Sbill } 199116068Sralph if (cnt >= bp->b_bufsize) { 1992615Sbill if (bp->b_ptr != bp->b_buf) 1993615Sbill bflush1(bp); 199416068Sralph put = cnt - cnt % bp->b_bufsize; 1995615Sbill if (boffset != bp->b_off) 1996615Sbill lseek(biofd, bp->b_off, 0); 1997615Sbill if (write(biofd, p, put) != put) { 1998615Sbill bwrerror = 1; 1999615Sbill error(1, "output write error"); 2000615Sbill } 2001615Sbill bp->b_off += put; 2002615Sbill boffset = bp->b_off; 2003615Sbill p += put; 2004615Sbill cnt -= put; 2005615Sbill goto top; 2006615Sbill } 2007615Sbill bflush1(bp); 2008615Sbill goto top; 2009615Sbill } 2010615Sbill 2011615Sbill bflush() 2012615Sbill { 2013615Sbill register struct biobuf *bp; 2014615Sbill 2015615Sbill if (bwrerror) 2016615Sbill return; 2017615Sbill for (bp = biobufs; bp; bp = bp->b_link) 2018615Sbill bflush1(bp); 2019615Sbill } 2020615Sbill 2021615Sbill bflush1(bp) 2022615Sbill register struct biobuf *bp; 2023615Sbill { 2024615Sbill register int cnt = bp->b_ptr - bp->b_buf; 2025615Sbill 2026615Sbill if (cnt == 0) 2027615Sbill return; 2028615Sbill if (boffset != bp->b_off) 2029615Sbill lseek(biofd, bp->b_off, 0); 2030615Sbill if (write(biofd, bp->b_buf, cnt) != cnt) { 2031615Sbill bwrerror = 1; 2032615Sbill error(1, "output write error"); 2033615Sbill } 2034615Sbill bp->b_off += cnt; 2035615Sbill boffset = bp->b_off; 2036615Sbill bp->b_ptr = bp->b_buf; 203716068Sralph bp->b_nleft = bp->b_bufsize; 2038615Sbill } 2039615Sbill 2040615Sbill bflushc(bp, c) 2041615Sbill register struct biobuf *bp; 2042615Sbill { 2043615Sbill 2044615Sbill bflush1(bp); 2045615Sbill bputc(c, bp); 2046615Sbill } 204716068Sralph 204816068Sralph bseek(bp, off) 204916068Sralph register struct biobuf *bp; 205016068Sralph register off_t off; 205116068Sralph { 205216068Sralph bflush1(bp); 205316068Sralph 205416068Sralph bp->b_nleft = bp->b_bufsize - (off % bp->b_bufsize); 205516068Sralph bp->b_off = off; 205616068Sralph } 2057