119842Sdist /* 219842Sdist * Copyright (c) 1980 Regents of the University of California. 319842Sdist * All rights reserved. The Berkeley software License Agreement 419842Sdist * specifies the terms and conditions for redistribution. 519842Sdist */ 619842Sdist 712671Ssam #ifndef lint 819842Sdist char copyright[] = 919842Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 1019842Sdist All rights reserved.\n"; 1119842Sdist #endif not lint 126414Smckusic 1319842Sdist #ifndef lint 14*42415Sbostic static char sccsid[] = "@(#)ld.c 5.14 (Berkeley) 05/28/90"; 1519842Sdist #endif not lint 1619842Sdist 17615Sbill /* 18898Sbill * ld - string table version for VAX 19615Sbill */ 20615Sbill 2117133Ssam #include <sys/param.h> 22*42415Sbostic #include <sys/stat.h> 23*42415Sbostic #include <sys/file.h> 24*42415Sbostic #include <sys/signal.h> 25650Sbill #include <ar.h> 26650Sbill #include <a.out.h> 27615Sbill #include <ranlib.h> 28*42415Sbostic #include <stdio.h> 29*42415Sbostic #include <ctype.h> 30*42415Sbostic #include <string.h> 3137031Sbostic #include "pathnames.h" 32615Sbill 33615Sbill /* 34615Sbill * Basic strategy: 35615Sbill * 36615Sbill * The loader takes a number of files and libraries as arguments. 37615Sbill * A first pass examines each file in turn. Normal files are 38615Sbill * unconditionally loaded, and the (external) symbols they define and require 39615Sbill * are noted in the symbol table. Libraries are searched, and the 40615Sbill * library members which define needed symbols are remembered 41615Sbill * in a special data structure so they can be selected on the second 42615Sbill * pass. Symbols defined and required by library members are also 43615Sbill * recorded. 44615Sbill * 45615Sbill * After the first pass, the loader knows the size of the basic text 46615Sbill * data, and bss segments from the sum of the sizes of the modules which 47615Sbill * were required. It has computed, for each ``common'' symbol, the 48615Sbill * maximum size of any reference to it, and these symbols are then assigned 49615Sbill * storage locations after their sizes are appropriately rounded. 50615Sbill * The loader now knows all sizes for the eventual output file, and 51615Sbill * can determine the final locations of external symbols before it 52615Sbill * begins a second pass. 53615Sbill * 54615Sbill * On the second pass each normal file and required library member 55615Sbill * is processed again. The symbol table for each such file is 56615Sbill * reread and relevant parts of it are placed in the output. The offsets 57615Sbill * in the local symbol table for externally defined symbols are recorded 58615Sbill * since relocation information refers to symbols in this way. 59615Sbill * Armed with all necessary information, the text and data segments 60615Sbill * are relocated and the result is placed in the output file, which 61615Sbill * is pasted together, ``in place'', by writing to it in several 62615Sbill * different places concurrently. 63615Sbill */ 64615Sbill 65615Sbill /* 66615Sbill * Internal data structures 67615Sbill * 68615Sbill * All internal data structures are segmented and dynamically extended. 69615Sbill * The basic structures hold 1103 (NSYM) symbols, ~~200 (NROUT) 70615Sbill * referenced library members, and 100 (NSYMPR) private (local) symbols 71615Sbill * per object module. For large programs and/or modules, these structures 72615Sbill * expand to be up to 40 (NSEG) times as large as this as necessary. 73615Sbill */ 74615Sbill #define NSEG 40 /* Number of segments, each data structure */ 75615Sbill #define NSYM 1103 /* Number of symbols per segment */ 76615Sbill #define NROUT 250 /* Number of library references per segment */ 77615Sbill #define NSYMPR 100 /* Number of private symbols per segment */ 78615Sbill 79615Sbill /* 80615Sbill * Structure describing each symbol table segment. 81615Sbill * Each segment has its own hash table. We record the first 82615Sbill * address in and first address beyond both the symbol and hash 83615Sbill * tables, for use in the routine symx and the lookup routine respectively. 84615Sbill * The symfree routine also understands this structure well as it used 85615Sbill * to back out symbols from modules we decide that we don't need in pass 1. 86615Sbill * 87615Sbill * Csymseg points to the current symbol table segment; 88615Sbill * csymseg->sy_first[csymseg->sy_used] is the next symbol slot to be allocated, 89615Sbill * (unless csymseg->sy_used == NSYM in which case we will allocate another 90615Sbill * symbol table segment first.) 91615Sbill */ 92615Sbill struct symseg { 93615Sbill struct nlist *sy_first; /* base of this alloc'ed segment */ 94615Sbill struct nlist *sy_last; /* end of this segment, for n_strx */ 95615Sbill int sy_used; /* symbols used in this seg */ 96615Sbill struct nlist **sy_hfirst; /* base of hash table, this seg */ 97615Sbill struct nlist **sy_hlast; /* end of hash table, this seg */ 98615Sbill } symseg[NSEG], *csymseg; 99615Sbill 100615Sbill /* 101615Sbill * The lookup routine uses quadratic rehash. Since a quadratic rehash 102615Sbill * only probes 1/2 of the buckets in the table, and since the hash 103615Sbill * table is segmented the same way the symbol table is, we make the 104615Sbill * hash table have twice as many buckets as there are symbol table slots 105615Sbill * in the segment. This guarantees that the quadratic rehash will never 106615Sbill * fail to find an empty bucket if the segment is not full and the 107615Sbill * symbol is not there. 108615Sbill */ 109615Sbill #define HSIZE (NSYM*2) 110615Sbill 111615Sbill /* 112615Sbill * Xsym converts symbol table indices (ala x) into symbol table pointers. 113615Sbill * Symx (harder, but never used in loops) inverts pointers into the symbol 114615Sbill * table into indices using the symseg[] structure. 115615Sbill */ 116615Sbill #define xsym(x) (symseg[(x)/NSYM].sy_first+((x)%NSYM)) 117615Sbill /* symx() is a function, defined below */ 118615Sbill 119615Sbill struct nlist cursym; /* current symbol */ 120615Sbill struct nlist *lastsym; /* last symbol entered */ 121615Sbill struct nlist *nextsym; /* next available symbol table entry */ 122615Sbill struct nlist *addsym; /* first sym defined during incr load */ 123615Sbill int nsym; /* pass2: number of local symbols in a.out */ 124615Sbill /* nsym + symx(nextsym) is the symbol table size during pass2 */ 125615Sbill 126615Sbill struct nlist **lookup(), **slookup(); 127650Sbill struct nlist *p_etext, *p_edata, *p_end, *entrypt; 128615Sbill 129615Sbill /* 130615Sbill * Definitions of segmentation for library member table. 131615Sbill * For each library we encounter on pass 1 we record pointers to all 132615Sbill * members which we will load on pass 2. These are recorded as offsets 133615Sbill * into the archive in the library member table. Libraries are 134615Sbill * separated in the table by the special offset value -1. 135615Sbill */ 136615Sbill off_t li_init[NROUT]; 137615Sbill struct libseg { 138615Sbill off_t *li_first; 139615Sbill int li_used; 140615Sbill int li_used2; 141615Sbill } libseg[NSEG] = { 142615Sbill li_init, 0, 0, 143615Sbill }, *clibseg = libseg; 144615Sbill 145615Sbill /* 146615Sbill * In processing each module on pass 2 we must relocate references 147615Sbill * relative to external symbols. These references are recorded 148615Sbill * in the relocation information as relative to local symbol numbers 149615Sbill * assigned to the external symbols when the module was created. 150615Sbill * Thus before relocating the module in pass 2 we create a table 151615Sbill * which maps these internal numbers to symbol table entries. 152615Sbill * A hash table is constructed, based on the local symbol table indices, 153615Sbill * for quick lookup of these symbols. 154615Sbill */ 155615Sbill #define LHSIZ 31 156615Sbill struct local { 157615Sbill int l_index; /* index to symbol in file */ 158615Sbill struct nlist *l_symbol; /* ptr to symbol table */ 159615Sbill struct local *l_link; /* hash link */ 160615Sbill } *lochash[LHSIZ], lhinit[NSYMPR]; 161615Sbill struct locseg { 162615Sbill struct local *lo_first; 163615Sbill int lo_used; 164615Sbill } locseg[NSEG] = { 165615Sbill lhinit, 0 166615Sbill }, *clocseg; 167615Sbill 168615Sbill /* 169615Sbill * Libraries are typically built with a table of contents, 170615Sbill * which is the first member of a library with special file 171615Sbill * name __.SYMDEF and contains a list of symbol names 172615Sbill * and with each symbol the offset of the library member which defines 173615Sbill * it. The loader uses this table to quickly tell which library members 174615Sbill * are (potentially) useful. The alternative, examining the symbol 175615Sbill * table of each library member, is painfully slow for large archives. 176615Sbill * 177615Sbill * See <ranlib.h> for the definition of the ranlib structure and an 178615Sbill * explanation of the __.SYMDEF file format. 179615Sbill */ 180615Sbill int tnum; /* number of symbols in table of contents */ 181615Sbill int ssiz; /* size of string table for table of contents */ 182615Sbill struct ranlib *tab; /* the table of contents (dynamically allocated) */ 183615Sbill char *tabstr; /* string table for table of contents */ 184615Sbill 185615Sbill /* 186615Sbill * We open each input file or library only once, but in pass2 we 187615Sbill * (historically) read from such a file at 2 different places at the 188615Sbill * same time. These structures are remnants from those days, 189650Sbill * and now serve only to catch ``Premature EOF''. 1906414Smckusic * In order to make I/O more efficient, we provide routines which 19116068Sralph * use the optimal block size returned by stat(). 192615Sbill */ 1936414Smckusic #define BLKSIZE 1024 194615Sbill typedef struct { 195615Sbill short *fakeptr; 196615Sbill int bno; 197615Sbill int nibuf; 198615Sbill int nuser; 19916068Sralph char *buff; 20016068Sralph int bufsize; 201615Sbill } PAGE; 202615Sbill 203615Sbill PAGE page[2]; 20416068Sralph int p_blksize; 20516068Sralph int p_blkshift; 20616068Sralph int p_blkmask; 207615Sbill 208615Sbill struct { 209615Sbill short *fakeptr; 210615Sbill int bno; 211615Sbill int nibuf; 212615Sbill int nuser; 213615Sbill } fpage; 214615Sbill 215615Sbill typedef struct { 216615Sbill char *ptr; 217615Sbill int bno; 218615Sbill int nibuf; 219615Sbill long size; 220615Sbill long pos; 221615Sbill PAGE *pno; 222615Sbill } STREAM; 223615Sbill 224615Sbill STREAM text; 225615Sbill STREAM reloc; 226615Sbill 227615Sbill /* 228615Sbill * Header from the a.out and the archive it is from (if any). 229615Sbill */ 230615Sbill struct exec filhdr; 231615Sbill struct ar_hdr archdr; 232615Sbill #define OARMAG 0177545 233615Sbill 234615Sbill /* 235615Sbill * Options. 236615Sbill */ 237615Sbill int trace; 238615Sbill int xflag; /* discard local symbols */ 239615Sbill int Xflag; /* discard locals starting with 'L' */ 240615Sbill int Sflag; /* discard all except locals and globals*/ 241615Sbill int rflag; /* preserve relocation bits, don't define common */ 242615Sbill int arflag; /* original copy of rflag */ 243615Sbill int sflag; /* discard all symbols */ 244898Sbill int Mflag; /* print rudimentary load map */ 245615Sbill int nflag; /* pure procedure */ 246615Sbill int dflag; /* define common even with rflag */ 247650Sbill int zflag; /* demand paged */ 248615Sbill long hsize; /* size of hole at beginning of data to be squashed */ 249615Sbill int Aflag; /* doing incremental load */ 250650Sbill int Nflag; /* want impure a.out */ 251615Sbill int funding; /* reading fundamental file for incremental load */ 252898Sbill int yflag; /* number of symbols to be traced */ 253898Sbill char **ytab; /* the symbols */ 254615Sbill 255615Sbill /* 256615Sbill * These are the cumulative sizes, set in pass 1, which 257615Sbill * appear in the a.out header when the loader is finished. 258615Sbill */ 259615Sbill off_t tsize, dsize, bsize, trsize, drsize, ssize; 260615Sbill 261615Sbill /* 262615Sbill * Symbol relocation: c?rel is a scale factor which is 263615Sbill * added to an old relocation to convert it to new units; 264615Sbill * i.e. it is the difference between segment origins. 265650Sbill * (Thus if we are loading from a data segment which began at location 266650Sbill * 4 in a .o file into an a.out where it will be loaded starting at 267650Sbill * 1024, cdrel will be 1020.) 268615Sbill */ 269615Sbill long ctrel, cdrel, cbrel; 270615Sbill 271615Sbill /* 272650Sbill * Textbase is the start address of all text, 0 unless given by -T. 273615Sbill * Database is the base of all data, computed before and used during pass2. 274650Sbill */ 275650Sbill long textbase, database; 276650Sbill 277650Sbill /* 278615Sbill * The base addresses for the loaded text, data and bss from the 279615Sbill * current module during pass2 are given by torigin, dorigin and borigin. 280615Sbill */ 281615Sbill long torigin, dorigin, borigin; 282615Sbill 283615Sbill /* 284615Sbill * Errlev is nonzero when errors have occured. 285615Sbill * Delarg is an implicit argument to the routine delexit 286615Sbill * which is called on error. We do ``delarg = errlev'' before normal 287615Sbill * exits, and only if delarg is 0 (i.e. errlev was 0) do we make the 288615Sbill * result file executable. 289615Sbill */ 290615Sbill int errlev; 291615Sbill int delarg = 4; 292615Sbill 293615Sbill /* 294615Sbill * The biobuf structure and associated routines are used to write 295615Sbill * into one file at several places concurrently. Calling bopen 296615Sbill * with a biobuf structure sets it up to write ``biofd'' starting 297615Sbill * at the specified offset. You can then use ``bwrite'' and/or ``bputc'' 298615Sbill * to stuff characters in the stream, much like ``fwrite'' and ``fputc''. 299615Sbill * Calling bflush drains all the buffers and MUST be done before exit. 300615Sbill */ 301615Sbill struct biobuf { 302615Sbill short b_nleft; /* Number free spaces left in b_buf */ 30316068Sralph /* Initialize to be less than b_bufsize initially, to boundary align in file */ 304615Sbill char *b_ptr; /* Next place to stuff characters */ 30516068Sralph char *b_buf; /* Pointer to the buffer */ 30616068Sralph int b_bufsize; /* Size of the buffer */ 307615Sbill off_t b_off; /* Current file offset */ 308615Sbill struct biobuf *b_link; /* Link in chain for bflush() */ 309615Sbill } *biobufs; 310615Sbill #define bputc(c,b) ((b)->b_nleft ? (--(b)->b_nleft, *(b)->b_ptr++ = (c)) \ 311615Sbill : bflushc(b, c)) 312615Sbill int biofd; 313615Sbill off_t boffset; 314615Sbill struct biobuf *tout, *dout, *trout, *drout, *sout, *strout; 315615Sbill 316615Sbill /* 317615Sbill * Offset is the current offset in the string file. 318615Sbill * Its initial value reflects the fact that we will 319615Sbill * eventually stuff the size of the string table at the 320615Sbill * beginning of the string table (i.e. offset itself!). 321615Sbill */ 322615Sbill off_t offset = sizeof (off_t); 323615Sbill 324615Sbill int ofilfnd; /* -o given; otherwise move l.out to a.out */ 32540407Smckusick char *defaultname; /* l.out */ 32640407Smckusick char *ofilename; /* name given to -o */ 3273606Ssklower int ofilemode; /* respect umask even for unsucessful ld's */ 328615Sbill int infil; /* current input file descriptor */ 329615Sbill char *filname; /* and its name */ 330615Sbill 33117133Ssam #define NDIRS 25 33225533Sbloom #define NDEFDIRS 3 /* number of default directories in dirs[] */ 33317133Ssam char *dirs[NDIRS]; /* directories for library search */ 33417133Ssam int ndir; /* number of directories */ 33517133Ssam 336615Sbill /* 337615Sbill * Base of the string table of the current module (pass1 and pass2). 338615Sbill */ 339615Sbill char *curstr; 340615Sbill 34112671Ssam /* 34212671Ssam * System software page size, as returned by getpagesize. 34312671Ssam */ 34412671Ssam int pagesize; 34512671Ssam 346615Sbill char get(); 347615Sbill int delexit(); 348615Sbill char *savestr(); 34917133Ssam char *malloc(); 350615Sbill 351615Sbill main(argc, argv) 352615Sbill char **argv; 353615Sbill { 354615Sbill register int c, i; 355615Sbill int num; 356615Sbill register char *ap, **p; 357615Sbill char save; 358615Sbill 359650Sbill if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 360615Sbill signal(SIGINT, delexit); 361650Sbill signal(SIGTERM, delexit); 362650Sbill } 363615Sbill if (argc == 1) 364615Sbill exit(4); 36540407Smckusick ofilename = defaultname = (char *)genbuildname("l.out"); 36612671Ssam pagesize = getpagesize(); 367615Sbill 36817133Ssam /* 36917133Ssam * Pull out search directories. 37017133Ssam */ 37117133Ssam for (c = 1; c < argc; c++) { 37217133Ssam ap = argv[c]; 37317133Ssam if (ap[0] == '-' && ap[1] == 'L') { 37417133Ssam if (ap[2] == 0) 37517133Ssam error(1, "-L: pathname missing"); 37625533Sbloom if (ndir >= NDIRS - NDEFDIRS) 37717133Ssam error(1, "-L: too many directories"); 37817133Ssam dirs[ndir++] = &ap[2]; 37917133Ssam } 38017133Ssam } 38117133Ssam /* add default search directories */ 38237031Sbostic dirs[ndir++] = _PATH_USRLIB; 38337031Sbostic dirs[ndir++] = _PATH_LOCALLIB; 38417133Ssam 38517133Ssam p = argv+1; 386650Sbill /* 387650Sbill * Scan files once to find where symbols are defined. 388650Sbill */ 389615Sbill for (c=1; c<argc; c++) { 390615Sbill if (trace) 391615Sbill printf("%s:\n", *p); 392615Sbill filname = 0; 393615Sbill ap = *p++; 394615Sbill if (*ap != '-') { 395615Sbill load1arg(ap); 396615Sbill continue; 397615Sbill } 398615Sbill for (i=1; ap[i]; i++) switch (ap[i]) { 399615Sbill 400615Sbill case 'o': 401615Sbill if (++c >= argc) 402615Sbill error(1, "-o where?"); 40340407Smckusick ofilename = (char *)genbuildname(*p++); 404615Sbill ofilfnd++; 405615Sbill continue; 406615Sbill case 'u': 407615Sbill case 'e': 408615Sbill if (++c >= argc) 40933997Sbostic error(1, " -u or -e: arg missing"); 410615Sbill enter(slookup(*p++)); 411615Sbill if (ap[i]=='e') 412615Sbill entrypt = lastsym; 413615Sbill continue; 414615Sbill case 'H': 415615Sbill if (++c >= argc) 416615Sbill error(1, "-H: arg missing"); 417615Sbill if (tsize!=0) 418615Sbill error(1, "-H: too late, some text already loaded"); 419615Sbill hsize = atoi(*p++); 420615Sbill continue; 421615Sbill case 'A': 422615Sbill if (++c >= argc) 423615Sbill error(1, "-A: arg missing"); 424615Sbill if (Aflag) 425615Sbill error(1, "-A: only one base file allowed"); 426615Sbill Aflag = 1; 427615Sbill nflag = 0; 428615Sbill funding = 1; 429615Sbill load1arg(*p++); 430615Sbill trsize = drsize = tsize = dsize = bsize = 0; 431615Sbill ctrel = cdrel = cbrel = 0; 432615Sbill funding = 0; 433615Sbill addsym = nextsym; 434615Sbill continue; 435615Sbill case 'D': 436615Sbill if (++c >= argc) 437615Sbill error(1, "-D: arg missing"); 438615Sbill num = htoi(*p++); 439615Sbill if (dsize > num) 440615Sbill error(1, "-D: too small"); 441615Sbill dsize = num; 442615Sbill continue; 443615Sbill case 'T': 444615Sbill if (++c >= argc) 445615Sbill error(1, "-T: arg missing"); 446615Sbill if (tsize!=0) 447615Sbill error(1, "-T: too late, some text already loaded"); 448615Sbill textbase = htoi(*p++); 449615Sbill continue; 450615Sbill case 'l': 451615Sbill save = ap[--i]; 452615Sbill ap[i]='-'; 453615Sbill load1arg(&ap[i]); 454615Sbill ap[i]=save; 455615Sbill goto next; 456898Sbill case 'M': 457898Sbill Mflag++; 458898Sbill continue; 459615Sbill case 'x': 460615Sbill xflag++; 461615Sbill continue; 462615Sbill case 'X': 463615Sbill Xflag++; 464615Sbill continue; 465615Sbill case 'S': 466615Sbill Sflag++; 467615Sbill continue; 468615Sbill case 'r': 469615Sbill rflag++; 470615Sbill arflag++; 471615Sbill continue; 472615Sbill case 's': 473615Sbill sflag++; 474615Sbill xflag++; 475615Sbill continue; 476615Sbill case 'n': 477615Sbill nflag++; 478650Sbill Nflag = zflag = 0; 479615Sbill continue; 480615Sbill case 'N': 481650Sbill Nflag++; 482650Sbill nflag = zflag = 0; 483615Sbill continue; 484615Sbill case 'd': 485615Sbill dflag++; 486615Sbill continue; 487615Sbill case 'i': 488615Sbill printf("ld: -i ignored\n"); 489615Sbill continue; 490615Sbill case 't': 491615Sbill trace++; 492615Sbill continue; 493898Sbill case 'y': 494898Sbill if (ap[i+1] == 0) 495898Sbill error(1, "-y: symbol name missing"); 496898Sbill if (yflag == 0) { 497898Sbill ytab = (char **)calloc(argc, sizeof (char **)); 498898Sbill if (ytab == 0) 499898Sbill error(1, "ran out of memory (-y)"); 500898Sbill } 501898Sbill ytab[yflag++] = &ap[i+1]; 502898Sbill goto next; 503615Sbill case 'z': 504615Sbill zflag++; 505650Sbill Nflag = nflag = 0; 506615Sbill continue; 50717133Ssam case 'L': 50817133Ssam goto next; 509615Sbill default: 510615Sbill filname = savestr("-x"); /* kludge */ 511615Sbill filname[1] = ap[i]; /* kludge */ 512615Sbill archdr.ar_name[0] = 0; /* kludge */ 513615Sbill error(1, "bad flag"); 514615Sbill } 515615Sbill next: 516615Sbill ; 517615Sbill } 518650Sbill if (rflag == 0 && Nflag == 0 && nflag == 0) 519650Sbill zflag++; 520615Sbill endload(argc, argv); 521615Sbill exit(0); 522615Sbill } 523615Sbill 524615Sbill /* 525615Sbill * Convert a ascii string which is a hex number. 526615Sbill * Used by -T and -D options. 527615Sbill */ 528615Sbill htoi(p) 529615Sbill register char *p; 530615Sbill { 531615Sbill register int c, n; 532615Sbill 533615Sbill n = 0; 534615Sbill while (c = *p++) { 535615Sbill n <<= 4; 536615Sbill if (isdigit(c)) 537615Sbill n += c - '0'; 538615Sbill else if (c >= 'a' && c <= 'f') 539615Sbill n += 10 + (c - 'a'); 540615Sbill else if (c >= 'A' && c <= 'F') 541615Sbill n += 10 + (c - 'A'); 542615Sbill else 543615Sbill error(1, "badly formed hex number"); 544615Sbill } 545615Sbill return (n); 546615Sbill } 547615Sbill 548615Sbill delexit() 549615Sbill { 5509332Smckusick struct stat stbuf; 5519332Smckusick long size; 5529332Smckusick char c = 0; 553615Sbill 554615Sbill bflush(); 55540407Smckusick unlink(defaultname); 5569332Smckusick /* 5579332Smckusick * We have to insure that the last block of the data segment 55816068Sralph * is allocated a full pagesize block. If the underlying 55916068Sralph * file system allocates frags that are smaller than pagesize, 56016068Sralph * a full zero filled pagesize block needs to be allocated so 5619332Smckusick * that when it is demand paged, the paged in block will be 5629332Smckusick * appropriately filled with zeros. 5639332Smckusick */ 5649332Smckusick fstat(biofd, &stbuf); 56516068Sralph size = round(stbuf.st_size, pagesize); 56610640Smckusick if (!rflag && size > stbuf.st_size) { 5679332Smckusick lseek(biofd, size - 1, 0); 56825419Sbloom if (write(biofd, &c, 1) != 1) 56925419Sbloom delarg |= 4; 5709332Smckusick } 57125419Sbloom if (delarg==0 && Aflag==0) 57225419Sbloom (void) chmod(ofilename, ofilemode); 573615Sbill exit (delarg); 574615Sbill } 575615Sbill 576615Sbill endload(argc, argv) 577615Sbill int argc; 578615Sbill char **argv; 579615Sbill { 580615Sbill register int c, i; 581615Sbill long dnum; 582615Sbill register char *ap, **p; 583615Sbill 584615Sbill clibseg = libseg; 585615Sbill filname = 0; 586615Sbill middle(); 587615Sbill setupout(); 588615Sbill p = argv+1; 589615Sbill for (c=1; c<argc; c++) { 590615Sbill ap = *p++; 591615Sbill if (trace) 592615Sbill printf("%s:\n", ap); 593615Sbill if (*ap != '-') { 594615Sbill load2arg(ap); 595615Sbill continue; 596615Sbill } 597615Sbill for (i=1; ap[i]; i++) switch (ap[i]) { 598615Sbill 599615Sbill case 'D': 600615Sbill dnum = htoi(*p); 601615Sbill if (dorigin < dnum) 602615Sbill while (dorigin < dnum) 603615Sbill bputc(0, dout), dorigin++; 604615Sbill /* fall into ... */ 605615Sbill case 'T': 606615Sbill case 'u': 607615Sbill case 'e': 608615Sbill case 'o': 609615Sbill case 'H': 610615Sbill ++c; 611615Sbill ++p; 612615Sbill /* fall into ... */ 613615Sbill default: 614615Sbill continue; 615615Sbill case 'A': 616615Sbill funding = 1; 617615Sbill load2arg(*p++); 618615Sbill funding = 0; 619615Sbill c++; 620615Sbill continue; 621898Sbill case 'y': 62217133Ssam case 'L': 623898Sbill goto next; 624615Sbill case 'l': 625615Sbill ap[--i]='-'; 626615Sbill load2arg(&ap[i]); 627615Sbill goto next; 628615Sbill } 629615Sbill next: 630615Sbill ; 631615Sbill } 632615Sbill finishout(); 633615Sbill } 634615Sbill 635615Sbill /* 636615Sbill * Scan file to find defined symbols. 637615Sbill */ 638615Sbill load1arg(cp) 639615Sbill register char *cp; 640615Sbill { 641615Sbill register struct ranlib *tp; 642615Sbill off_t nloc; 643898Sbill int kind; 644615Sbill 645898Sbill kind = getfile(cp); 646898Sbill if (Mflag) 647898Sbill printf("%s\n", filname); 648898Sbill switch (kind) { 649615Sbill 650615Sbill /* 651615Sbill * Plain file. 652615Sbill */ 653615Sbill case 0: 654615Sbill load1(0, 0L); 655615Sbill break; 656615Sbill 657615Sbill /* 658615Sbill * Archive without table of contents. 659615Sbill * (Slowly) process each member. 660615Sbill */ 661615Sbill case 1: 662898Sbill error(-1, 663898Sbill "warning: archive has no table of contents; add one using ranlib(1)"); 664615Sbill nloc = SARMAG; 665615Sbill while (step(nloc)) 666615Sbill nloc += sizeof(archdr) + 667615Sbill round(atol(archdr.ar_size), sizeof (short)); 668615Sbill break; 669615Sbill 670615Sbill /* 671615Sbill * Archive with table of contents. 672615Sbill * Read the table of contents and its associated string table. 673615Sbill * Pass through the library resolving symbols until nothing changes 674615Sbill * for an entire pass (i.e. you can get away with backward references 675615Sbill * when there is a table of contents!) 676615Sbill */ 677615Sbill case 2: 678615Sbill nloc = SARMAG + sizeof (archdr); 679615Sbill dseek(&text, nloc, sizeof (tnum)); 680615Sbill mget((char *)&tnum, sizeof (tnum), &text); 681615Sbill nloc += sizeof (tnum); 682615Sbill tab = (struct ranlib *)malloc(tnum); 683615Sbill if (tab == 0) 684615Sbill error(1, "ran out of memory (toc)"); 685615Sbill dseek(&text, nloc, tnum); 686615Sbill mget((char *)tab, tnum, &text); 687615Sbill nloc += tnum; 688615Sbill tnum /= sizeof (struct ranlib); 689615Sbill dseek(&text, nloc, sizeof (ssiz)); 690615Sbill mget((char *)&ssiz, sizeof (ssiz), &text); 691615Sbill nloc += sizeof (ssiz); 692615Sbill tabstr = (char *)malloc(ssiz); 693615Sbill if (tabstr == 0) 694615Sbill error(1, "ran out of memory (tocstr)"); 695615Sbill dseek(&text, nloc, ssiz); 696615Sbill mget((char *)tabstr, ssiz, &text); 697615Sbill for (tp = &tab[tnum]; --tp >= tab;) { 698615Sbill if (tp->ran_un.ran_strx < 0 || 699615Sbill tp->ran_un.ran_strx >= ssiz) 700615Sbill error(1, "mangled archive table of contents"); 701615Sbill tp->ran_un.ran_name = tabstr + tp->ran_un.ran_strx; 702615Sbill } 703615Sbill while (ldrand()) 704615Sbill continue; 70525483Slepreau free((char *)tab); 70625483Slepreau free(tabstr); 707615Sbill nextlibp(-1); 708615Sbill break; 709615Sbill 710615Sbill /* 711615Sbill * Table of contents is out of date, so search 712615Sbill * as a normal library (but skip the __.SYMDEF file). 713615Sbill */ 714615Sbill case 3: 715898Sbill error(-1, 716898Sbill "warning: table of contents for archive is out of date; rerun ranlib(1)"); 717615Sbill nloc = SARMAG; 718615Sbill do 719615Sbill nloc += sizeof(archdr) + 720615Sbill round(atol(archdr.ar_size), sizeof(short)); 721615Sbill while (step(nloc)); 722615Sbill break; 723615Sbill } 724615Sbill close(infil); 725615Sbill } 726615Sbill 727615Sbill /* 728615Sbill * Advance to the next archive member, which 729615Sbill * is at offset nloc in the archive. If the member 730615Sbill * is useful, record its location in the liblist structure 731615Sbill * for use in pass2. Mark the end of the archive in libilst with a -1. 732615Sbill */ 733615Sbill step(nloc) 734615Sbill off_t nloc; 735615Sbill { 736615Sbill 737615Sbill dseek(&text, nloc, (long) sizeof archdr); 738615Sbill if (text.size <= 0) { 739615Sbill nextlibp(-1); 740615Sbill return (0); 741615Sbill } 742615Sbill getarhdr(); 743615Sbill if (load1(1, nloc + (sizeof archdr))) 744615Sbill nextlibp(nloc); 745615Sbill return (1); 746615Sbill } 747615Sbill 748615Sbill /* 749615Sbill * Record the location of a useful archive member. 750615Sbill * Recording -1 marks the end of files from an archive. 751615Sbill * The liblist data structure is dynamically extended here. 752615Sbill */ 753615Sbill nextlibp(val) 754615Sbill off_t val; 755615Sbill { 756615Sbill 757615Sbill if (clibseg->li_used == NROUT) { 758615Sbill if (++clibseg == &libseg[NSEG]) 759615Sbill error(1, "too many files loaded from libraries"); 760615Sbill clibseg->li_first = (off_t *)malloc(NROUT * sizeof (off_t)); 761615Sbill if (clibseg->li_first == 0) 762615Sbill error(1, "ran out of memory (nextlibp)"); 763615Sbill } 764615Sbill clibseg->li_first[clibseg->li_used++] = val; 765898Sbill if (val != -1 && Mflag) 766898Sbill printf("\t%s\n", archdr.ar_name); 767615Sbill } 768615Sbill 769615Sbill /* 770615Sbill * One pass over an archive with a table of contents. 771615Sbill * Remember the number of symbols currently defined, 772615Sbill * then call step on members which look promising (i.e. 773615Sbill * that define a symbol which is currently externally undefined). 774615Sbill * Indicate to our caller whether this process netted any more symbols. 775615Sbill */ 776615Sbill ldrand() 777615Sbill { 778615Sbill register struct nlist *sp, **hp; 779615Sbill register struct ranlib *tp, *tplast; 780615Sbill off_t loc; 781615Sbill int nsymt = symx(nextsym); 782615Sbill 783615Sbill tplast = &tab[tnum-1]; 784615Sbill for (tp = tab; tp <= tplast; tp++) { 78525483Slepreau if ((hp = slookup(tp->ran_un.ran_name)) == 0 || *hp == 0) 786615Sbill continue; 787615Sbill sp = *hp; 788615Sbill if (sp->n_type != N_EXT+N_UNDF) 789615Sbill continue; 790615Sbill step(tp->ran_off); 791615Sbill loc = tp->ran_off; 792615Sbill while (tp < tplast && (tp+1)->ran_off == loc) 793615Sbill tp++; 794615Sbill } 795615Sbill return (symx(nextsym) != nsymt); 796615Sbill } 797615Sbill 798615Sbill /* 799615Sbill * Examine a single file or archive member on pass 1. 800615Sbill */ 801615Sbill load1(libflg, loc) 802615Sbill off_t loc; 803615Sbill { 804615Sbill register struct nlist *sp; 805615Sbill struct nlist *savnext; 806615Sbill int ndef, nlocal, type, size, nsymt; 807615Sbill register int i; 808615Sbill off_t maxoff; 809615Sbill struct stat stb; 810615Sbill 811615Sbill readhdr(loc); 812615Sbill if (filhdr.a_syms == 0) { 81329999Sbostic if (filhdr.a_text+filhdr.a_data == 0) { 81429999Sbostic /* load2() adds a symbol for the file name */ 81529999Sbostic if (!libflg) 81629999Sbostic ssize += sizeof (cursym); 817615Sbill return (0); 81829999Sbostic } 819615Sbill error(1, "no namelist"); 820615Sbill } 821615Sbill if (libflg) 822615Sbill maxoff = atol(archdr.ar_size); 823615Sbill else { 824615Sbill fstat(infil, &stb); 825615Sbill maxoff = stb.st_size; 826615Sbill } 827615Sbill if (N_STROFF(filhdr) + sizeof (off_t) >= maxoff) 828615Sbill error(1, "too small (old format .o?)"); 829615Sbill ctrel = tsize; cdrel += dsize; cbrel += bsize; 830615Sbill ndef = 0; 831615Sbill nlocal = sizeof(cursym); 832615Sbill savnext = nextsym; 833615Sbill loc += N_SYMOFF(filhdr); 834615Sbill dseek(&text, loc, filhdr.a_syms); 835615Sbill dseek(&reloc, loc + filhdr.a_syms, sizeof(off_t)); 836615Sbill mget(&size, sizeof (size), &reloc); 837615Sbill dseek(&reloc, loc + filhdr.a_syms+sizeof (off_t), size-sizeof (off_t)); 838615Sbill curstr = (char *)malloc(size); 839615Sbill if (curstr == NULL) 840615Sbill error(1, "no space for string table"); 841615Sbill mget(curstr+sizeof(off_t), size-sizeof(off_t), &reloc); 842615Sbill while (text.size > 0) { 843615Sbill mget((char *)&cursym, sizeof(struct nlist), &text); 844615Sbill if (cursym.n_un.n_strx) { 845615Sbill if (cursym.n_un.n_strx<sizeof(size) || 846615Sbill cursym.n_un.n_strx>=size) 847615Sbill error(1, "bad string table index (pass 1)"); 848615Sbill cursym.n_un.n_name = curstr + cursym.n_un.n_strx; 849615Sbill } 850615Sbill type = cursym.n_type; 851615Sbill if ((type&N_EXT)==0) { 852615Sbill if (Xflag==0 || cursym.n_un.n_name[0]!='L' || 853615Sbill type & N_STAB) 854615Sbill nlocal += sizeof cursym; 855615Sbill continue; 856615Sbill } 857615Sbill symreloc(); 858615Sbill if (enter(lookup())) 859615Sbill continue; 860615Sbill if ((sp = lastsym)->n_type != N_EXT+N_UNDF) 861615Sbill continue; 862615Sbill if (cursym.n_type == N_EXT+N_UNDF) { 863615Sbill if (cursym.n_value > sp->n_value) 864615Sbill sp->n_value = cursym.n_value; 865615Sbill continue; 866615Sbill } 867615Sbill if (sp->n_value != 0 && cursym.n_type == N_EXT+N_TEXT) 868615Sbill continue; 869615Sbill ndef++; 870615Sbill sp->n_type = cursym.n_type; 871615Sbill sp->n_value = cursym.n_value; 872615Sbill } 873615Sbill if (libflg==0 || ndef) { 874615Sbill tsize += filhdr.a_text; 875615Sbill dsize += round(filhdr.a_data, sizeof (long)); 876615Sbill bsize += round(filhdr.a_bss, sizeof (long)); 877615Sbill ssize += nlocal; 878615Sbill trsize += filhdr.a_trsize; 879615Sbill drsize += filhdr.a_drsize; 880615Sbill if (funding) 881615Sbill textbase = (*slookup("_end"))->n_value; 882615Sbill nsymt = symx(nextsym); 883615Sbill for (i = symx(savnext); i < nsymt; i++) { 884615Sbill sp = xsym(i); 885615Sbill sp->n_un.n_name = savestr(sp->n_un.n_name); 886615Sbill } 887615Sbill free(curstr); 888615Sbill return (1); 889615Sbill } 890615Sbill /* 891615Sbill * No symbols defined by this library member. 892615Sbill * Rip out the hash table entries and reset the symbol table. 893615Sbill */ 894615Sbill symfree(savnext); 895615Sbill free(curstr); 896615Sbill return(0); 897615Sbill } 898615Sbill 899615Sbill middle() 900615Sbill { 901615Sbill register struct nlist *sp; 902615Sbill long csize, t, corigin, ocsize; 903615Sbill int nund, rnd; 904615Sbill char s; 905615Sbill register int i; 906615Sbill int nsymt; 907615Sbill 908615Sbill torigin = 0; 909615Sbill dorigin = 0; 910615Sbill borigin = 0; 911615Sbill 912615Sbill p_etext = *slookup("_etext"); 913615Sbill p_edata = *slookup("_edata"); 914615Sbill p_end = *slookup("_end"); 915615Sbill /* 916615Sbill * If there are any undefined symbols, save the relocation bits. 917615Sbill */ 918615Sbill nsymt = symx(nextsym); 919615Sbill if (rflag==0) { 920615Sbill for (i = 0; i < nsymt; i++) { 921615Sbill sp = xsym(i); 922615Sbill if (sp->n_type==N_EXT+N_UNDF && sp->n_value==0 && 923650Sbill sp!=p_end && sp!=p_edata && sp!=p_etext) { 924615Sbill rflag++; 925615Sbill dflag = 0; 926615Sbill break; 927615Sbill } 928615Sbill } 929615Sbill } 930615Sbill if (rflag) 931615Sbill sflag = zflag = 0; 932615Sbill /* 933615Sbill * Assign common locations. 934615Sbill */ 935615Sbill csize = 0; 936615Sbill if (!Aflag) 937615Sbill addsym = symseg[0].sy_first; 938615Sbill database = round(tsize+textbase, 93912671Ssam (nflag||zflag? pagesize : sizeof (long))); 940615Sbill database += hsize; 941615Sbill if (dflag || rflag==0) { 942615Sbill ldrsym(p_etext, tsize, N_EXT+N_TEXT); 943615Sbill ldrsym(p_edata, dsize, N_EXT+N_DATA); 944615Sbill ldrsym(p_end, bsize, N_EXT+N_BSS); 945615Sbill for (i = symx(addsym); i < nsymt; i++) { 946615Sbill sp = xsym(i); 947615Sbill if ((s=sp->n_type)==N_EXT+N_UNDF && 948615Sbill (t = sp->n_value)!=0) { 949615Sbill if (t >= sizeof (double)) 950615Sbill rnd = sizeof (double); 951615Sbill else if (t >= sizeof (long)) 952615Sbill rnd = sizeof (long); 953615Sbill else 954615Sbill rnd = sizeof (short); 955615Sbill csize = round(csize, rnd); 956615Sbill sp->n_value = csize; 957615Sbill sp->n_type = N_EXT+N_COMM; 958615Sbill ocsize = csize; 959615Sbill csize += t; 960615Sbill } 961615Sbill if (s&N_EXT && (s&N_TYPE)==N_UNDF && s&N_STAB) { 962615Sbill sp->n_value = ocsize; 963615Sbill sp->n_type = (s&N_STAB) | (N_EXT+N_COMM); 964615Sbill } 965615Sbill } 966615Sbill } 967615Sbill /* 968615Sbill * Now set symbols to their final value 969615Sbill */ 970615Sbill csize = round(csize, sizeof (long)); 971615Sbill torigin = textbase; 972615Sbill dorigin = database; 973615Sbill corigin = dorigin + dsize; 974615Sbill borigin = corigin + csize; 975615Sbill nund = 0; 976615Sbill nsymt = symx(nextsym); 977615Sbill for (i = symx(addsym); i<nsymt; i++) { 978615Sbill sp = xsym(i); 979615Sbill switch (sp->n_type & (N_TYPE+N_EXT)) { 980615Sbill 981615Sbill case N_EXT+N_UNDF: 9822369Skre if (arflag == 0) 9832369Skre errlev |= 01; 984615Sbill if ((arflag==0 || dflag) && sp->n_value==0) { 985650Sbill if (sp==p_end || sp==p_etext || sp==p_edata) 986650Sbill continue; 987615Sbill if (nund==0) 988615Sbill printf("Undefined:\n"); 989615Sbill nund++; 990615Sbill printf("%s\n", sp->n_un.n_name); 991615Sbill } 992615Sbill continue; 993615Sbill case N_EXT+N_ABS: 994615Sbill default: 995615Sbill continue; 996615Sbill case N_EXT+N_TEXT: 997615Sbill sp->n_value += torigin; 998615Sbill continue; 999615Sbill case N_EXT+N_DATA: 1000615Sbill sp->n_value += dorigin; 1001615Sbill continue; 1002615Sbill case N_EXT+N_BSS: 1003615Sbill sp->n_value += borigin; 1004615Sbill continue; 1005615Sbill case N_EXT+N_COMM: 1006615Sbill sp->n_type = (sp->n_type & N_STAB) | (N_EXT+N_BSS); 1007615Sbill sp->n_value += corigin; 1008615Sbill continue; 1009615Sbill } 1010615Sbill } 1011615Sbill if (sflag || xflag) 1012615Sbill ssize = 0; 1013615Sbill bsize += csize; 1014615Sbill nsym = ssize / (sizeof cursym); 1015615Sbill if (Aflag) { 1016615Sbill fixspec(p_etext,torigin); 1017615Sbill fixspec(p_edata,dorigin); 1018615Sbill fixspec(p_end,borigin); 1019615Sbill } 1020615Sbill } 1021615Sbill 1022615Sbill fixspec(sym,offset) 1023615Sbill struct nlist *sym; 1024615Sbill long offset; 1025615Sbill { 1026615Sbill 1027615Sbill if(symx(sym) < symx(addsym) && sym!=0) 1028615Sbill sym->n_value += offset; 1029615Sbill } 1030615Sbill 1031615Sbill ldrsym(sp, val, type) 1032615Sbill register struct nlist *sp; 1033615Sbill long val; 1034615Sbill { 1035615Sbill 1036615Sbill if (sp == 0) 1037615Sbill return; 1038615Sbill if ((sp->n_type != N_EXT+N_UNDF || sp->n_value) && !Aflag) { 1039615Sbill printf("%s: ", sp->n_un.n_name); 1040615Sbill error(0, "user attempt to redfine loader-defined symbol"); 1041615Sbill return; 1042615Sbill } 1043615Sbill sp->n_type = type; 1044615Sbill sp->n_value = val; 1045615Sbill } 1046615Sbill 1047615Sbill off_t wroff; 1048615Sbill struct biobuf toutb; 1049615Sbill 1050615Sbill setupout() 1051615Sbill { 1052*42415Sbostic extern int errno; 1053615Sbill int bss; 105416068Sralph struct stat stbuf; 1055615Sbill 10563606Ssklower ofilemode = 0777 & ~umask(0); 10573606Ssklower biofd = creat(ofilename, 0666 & ofilemode); 1058898Sbill if (biofd < 0) { 1059898Sbill filname = ofilename; /* kludge */ 1060898Sbill archdr.ar_name[0] = 0; /* kludge */ 1061*42415Sbostic error(1, strerror(errno)); /* kludge */ 1062898Sbill } 106316068Sralph fstat(biofd, &stbuf); /* suppose file exists, wrong*/ 106416068Sralph if (stbuf.st_mode & 0111) { /* mode, ld fails? */ 106516068Sralph chmod(ofilename, stbuf.st_mode & 0666); 106616068Sralph ofilemode = stbuf.st_mode; 106716068Sralph } 1068615Sbill filhdr.a_magic = nflag ? NMAGIC : (zflag ? ZMAGIC : OMAGIC); 1069615Sbill filhdr.a_text = nflag ? tsize : 107012671Ssam round(tsize, zflag ? pagesize : sizeof (long)); 107112671Ssam filhdr.a_data = zflag ? round(dsize, pagesize) : dsize; 1072615Sbill bss = bsize - (filhdr.a_data - dsize); 1073615Sbill if (bss < 0) 1074615Sbill bss = 0; 1075615Sbill filhdr.a_bss = bss; 1076615Sbill filhdr.a_trsize = trsize; 1077615Sbill filhdr.a_drsize = drsize; 1078615Sbill filhdr.a_syms = sflag? 0: (ssize + (sizeof cursym)*symx(nextsym)); 1079615Sbill if (entrypt) { 1080615Sbill if (entrypt->n_type!=N_EXT+N_TEXT) 1081615Sbill error(0, "entry point not in text"); 1082615Sbill else 1083615Sbill filhdr.a_entry = entrypt->n_value; 1084615Sbill } else 1085615Sbill filhdr.a_entry = 0; 1086615Sbill filhdr.a_trsize = (rflag ? trsize:0); 1087615Sbill filhdr.a_drsize = (rflag ? drsize:0); 108816068Sralph tout = &toutb; 108916068Sralph bopen(tout, 0, stbuf.st_blksize); 1090615Sbill bwrite((char *)&filhdr, sizeof (filhdr), tout); 109116068Sralph if (zflag) 109216068Sralph bseek(tout, pagesize); 1093615Sbill wroff = N_TXTOFF(filhdr) + filhdr.a_text; 109416068Sralph outb(&dout, filhdr.a_data, stbuf.st_blksize); 1095615Sbill if (rflag) { 109616068Sralph outb(&trout, filhdr.a_trsize, stbuf.st_blksize); 109716068Sralph outb(&drout, filhdr.a_drsize, stbuf.st_blksize); 1098615Sbill } 1099615Sbill if (sflag==0 || xflag==0) { 110016068Sralph outb(&sout, filhdr.a_syms, stbuf.st_blksize); 1101615Sbill wroff += sizeof (offset); 110216068Sralph outb(&strout, 0, stbuf.st_blksize); 1103615Sbill } 1104615Sbill } 1105615Sbill 110616068Sralph outb(bp, inc, bufsize) 1107615Sbill register struct biobuf **bp; 1108615Sbill { 1109615Sbill 1110615Sbill *bp = (struct biobuf *)malloc(sizeof (struct biobuf)); 1111615Sbill if (*bp == 0) 1112615Sbill error(1, "ran out of memory (outb)"); 111316068Sralph bopen(*bp, wroff, bufsize); 1114615Sbill wroff += inc; 1115615Sbill } 1116615Sbill 1117615Sbill load2arg(acp) 1118615Sbill char *acp; 1119615Sbill { 1120615Sbill register char *cp; 1121615Sbill off_t loc; 1122615Sbill 1123615Sbill cp = acp; 1124615Sbill if (getfile(cp) == 0) { 1125615Sbill while (*cp) 1126615Sbill cp++; 1127615Sbill while (cp >= acp && *--cp != '/'); 1128615Sbill mkfsym(++cp); 1129615Sbill load2(0L); 1130615Sbill } else { /* scan archive members referenced */ 1131615Sbill for (;;) { 1132615Sbill if (clibseg->li_used2 == clibseg->li_used) { 1133615Sbill if (clibseg->li_used < NROUT) 1134615Sbill error(1, "libseg botch"); 1135615Sbill clibseg++; 1136615Sbill } 1137615Sbill loc = clibseg->li_first[clibseg->li_used2++]; 1138615Sbill if (loc == -1) 1139615Sbill break; 1140615Sbill dseek(&text, loc, (long)sizeof(archdr)); 1141615Sbill getarhdr(); 1142615Sbill mkfsym(archdr.ar_name); 1143615Sbill load2(loc + (long)sizeof(archdr)); 1144615Sbill } 1145615Sbill } 1146615Sbill close(infil); 1147615Sbill } 1148615Sbill 1149615Sbill load2(loc) 1150615Sbill long loc; 1151615Sbill { 1152615Sbill int size; 1153615Sbill register struct nlist *sp; 1154615Sbill register struct local *lp; 1155615Sbill register int symno, i; 1156615Sbill int type; 1157615Sbill 1158615Sbill readhdr(loc); 1159650Sbill if (!funding) { 1160615Sbill ctrel = torigin; 1161615Sbill cdrel += dorigin; 1162615Sbill cbrel += borigin; 1163615Sbill } 1164615Sbill /* 1165615Sbill * Reread the symbol table, recording the numbering 1166615Sbill * of symbols for fixing external references. 1167615Sbill */ 1168615Sbill for (i = 0; i < LHSIZ; i++) 1169615Sbill lochash[i] = 0; 1170615Sbill clocseg = locseg; 1171615Sbill clocseg->lo_used = 0; 1172615Sbill symno = -1; 1173615Sbill loc += N_TXTOFF(filhdr); 1174615Sbill dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1175615Sbill filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms, sizeof(off_t)); 1176615Sbill mget(&size, sizeof(size), &text); 1177615Sbill dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1178615Sbill filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms+sizeof(off_t), 1179615Sbill size - sizeof(off_t)); 1180615Sbill curstr = (char *)malloc(size); 1181615Sbill if (curstr == NULL) 1182615Sbill error(1, "out of space reading string table (pass 2)"); 1183615Sbill mget(curstr+sizeof(off_t), size-sizeof(off_t), &text); 1184615Sbill dseek(&text, loc+filhdr.a_text+filhdr.a_data+ 1185615Sbill filhdr.a_trsize+filhdr.a_drsize, filhdr.a_syms); 1186615Sbill while (text.size > 0) { 1187615Sbill symno++; 1188615Sbill mget((char *)&cursym, sizeof(struct nlist), &text); 1189615Sbill if (cursym.n_un.n_strx) { 1190615Sbill if (cursym.n_un.n_strx<sizeof(size) || 1191615Sbill cursym.n_un.n_strx>=size) 1192615Sbill error(1, "bad string table index (pass 2)"); 1193615Sbill cursym.n_un.n_name = curstr + cursym.n_un.n_strx; 1194615Sbill } 1195615Sbill /* inline expansion of symreloc() */ 1196615Sbill switch (cursym.n_type & 017) { 1197615Sbill 1198615Sbill case N_TEXT: 1199615Sbill case N_EXT+N_TEXT: 1200615Sbill cursym.n_value += ctrel; 1201615Sbill break; 1202615Sbill case N_DATA: 1203615Sbill case N_EXT+N_DATA: 1204615Sbill cursym.n_value += cdrel; 1205615Sbill break; 1206615Sbill case N_BSS: 1207615Sbill case N_EXT+N_BSS: 1208615Sbill cursym.n_value += cbrel; 1209615Sbill break; 1210615Sbill case N_EXT+N_UNDF: 1211615Sbill break; 1212615Sbill default: 1213615Sbill if (cursym.n_type&N_EXT) 1214615Sbill cursym.n_type = N_EXT+N_ABS; 1215615Sbill } 1216615Sbill /* end inline expansion of symreloc() */ 1217615Sbill type = cursym.n_type; 1218898Sbill if (yflag && cursym.n_un.n_name) 1219898Sbill for (i = 0; i < yflag; i++) 1220898Sbill /* fast check for 2d character! */ 1221898Sbill if (ytab[i][1] == cursym.n_un.n_name[1] && 1222898Sbill !strcmp(ytab[i], cursym.n_un.n_name)) { 1223898Sbill tracesym(); 1224898Sbill break; 1225898Sbill } 1226615Sbill if ((type&N_EXT) == 0) { 1227615Sbill if (!sflag&&!xflag&& 1228615Sbill (!Xflag||cursym.n_un.n_name[0]!='L'||type&N_STAB)) 1229615Sbill symwrite(&cursym, sout); 1230615Sbill continue; 1231615Sbill } 1232615Sbill if (funding) 1233615Sbill continue; 1234615Sbill if ((sp = *lookup()) == 0) 1235615Sbill error(1, "internal error: symbol not found"); 1236615Sbill if (cursym.n_type == N_EXT+N_UNDF) { 1237615Sbill if (clocseg->lo_used == NSYMPR) { 1238615Sbill if (++clocseg == &locseg[NSEG]) 1239615Sbill error(1, "local symbol overflow"); 1240615Sbill clocseg->lo_used = 0; 1241615Sbill } 1242615Sbill if (clocseg->lo_first == 0) { 1243615Sbill clocseg->lo_first = (struct local *) 1244615Sbill malloc(NSYMPR * sizeof (struct local)); 1245615Sbill if (clocseg->lo_first == 0) 1246615Sbill error(1, "out of memory (clocseg)"); 1247615Sbill } 1248615Sbill lp = &clocseg->lo_first[clocseg->lo_used++]; 1249615Sbill lp->l_index = symno; 1250615Sbill lp->l_symbol = sp; 1251615Sbill lp->l_link = lochash[symno % LHSIZ]; 1252615Sbill lochash[symno % LHSIZ] = lp; 1253615Sbill continue; 1254615Sbill } 1255615Sbill if (cursym.n_type & N_STAB) 1256615Sbill continue; 1257615Sbill if (cursym.n_type!=sp->n_type || cursym.n_value!=sp->n_value) { 1258615Sbill printf("%s: ", cursym.n_un.n_name); 1259615Sbill error(0, "multiply defined"); 1260615Sbill } 1261615Sbill } 1262615Sbill if (funding) 1263615Sbill return; 1264615Sbill dseek(&text, loc, filhdr.a_text); 1265615Sbill dseek(&reloc, loc+filhdr.a_text+filhdr.a_data, filhdr.a_trsize); 1266650Sbill load2td(ctrel, torigin - textbase, tout, trout); 1267615Sbill dseek(&text, loc+filhdr.a_text, filhdr.a_data); 1268615Sbill dseek(&reloc, loc+filhdr.a_text+filhdr.a_data+filhdr.a_trsize, 1269615Sbill filhdr.a_drsize); 1270650Sbill load2td(cdrel, dorigin - database, dout, drout); 1271615Sbill while (filhdr.a_data & (sizeof(long)-1)) { 1272615Sbill bputc(0, dout); 1273615Sbill filhdr.a_data++; 1274615Sbill } 1275615Sbill torigin += filhdr.a_text; 12761752Sbill dorigin += round(filhdr.a_data, sizeof (long)); 12771752Sbill borigin += round(filhdr.a_bss, sizeof (long)); 1278615Sbill free(curstr); 1279615Sbill } 1280615Sbill 1281898Sbill struct tynames { 1282898Sbill int ty_value; 1283898Sbill char *ty_name; 1284898Sbill } tynames[] = { 1285898Sbill N_UNDF, "undefined", 1286898Sbill N_ABS, "absolute", 1287898Sbill N_TEXT, "text", 1288898Sbill N_DATA, "data", 1289898Sbill N_BSS, "bss", 1290898Sbill N_COMM, "common", 1291898Sbill 0, 0, 1292898Sbill }; 1293898Sbill 1294898Sbill tracesym() 1295898Sbill { 1296898Sbill register struct tynames *tp; 1297898Sbill 1298898Sbill if (cursym.n_type & N_STAB) 1299898Sbill return; 1300898Sbill printf("%s", filname); 1301898Sbill if (archdr.ar_name[0]) 1302898Sbill printf("(%s)", archdr.ar_name); 1303898Sbill printf(": "); 1304898Sbill if ((cursym.n_type&N_TYPE) == N_UNDF && cursym.n_value) { 1305898Sbill printf("definition of common %s size %d\n", 1306898Sbill cursym.n_un.n_name, cursym.n_value); 1307898Sbill return; 1308898Sbill } 1309898Sbill for (tp = tynames; tp->ty_name; tp++) 1310898Sbill if (tp->ty_value == (cursym.n_type&N_TYPE)) 1311898Sbill break; 1312898Sbill printf((cursym.n_type&N_TYPE) ? "definition of" : "reference to"); 1313898Sbill if (cursym.n_type&N_EXT) 1314898Sbill printf(" external"); 1315898Sbill if (tp->ty_name) 1316898Sbill printf(" %s", tp->ty_name); 1317898Sbill printf(" %s\n", cursym.n_un.n_name); 1318898Sbill } 1319898Sbill 132029845Ssam #if !defined(tahoe) 132129845Ssam /* for machines which allow arbitrarily aligned word and longword accesses */ 132229845Ssam #define getw(cp) (*(short *)(cp)) 132329845Ssam #define getl(cp) (*(long *)(cp)) 132429845Ssam #define putw(cp, w) (*(short *)(cp) = (w)) 132529845Ssam #define putl(cp, l) (*(long *)(cp) = (l)) 132629845Ssam #else 132729845Ssam short 132829845Ssam getw(cp) 132929845Ssam char *cp; 133029845Ssam { 133129845Ssam union { 133229845Ssam short w; 133329845Ssam char c[2]; 133429845Ssam } w; 133529845Ssam 133629845Ssam w.c[0] = *cp++; 133729845Ssam w.c[1] = *cp++; 133829845Ssam return (w.w); 133929845Ssam } 134029845Ssam 134129845Ssam getl(cp) 134229845Ssam char *cp; 134329845Ssam { 134429845Ssam union { 134529845Ssam long l; 134629845Ssam char c[4]; 134729845Ssam } l; 134829845Ssam 134929845Ssam l.c[0] = *cp++; 135029845Ssam l.c[1] = *cp++; 135129845Ssam l.c[2] = *cp++; 135229845Ssam l.c[3] = *cp++; 135329845Ssam return (l.l); 135429845Ssam } 135529845Ssam 135629845Ssam putw(cp, v) 135729845Ssam char *cp; 135829845Ssam short v; 135929845Ssam { 136029845Ssam union { 136129845Ssam short w; 136229845Ssam char c[2]; 136329845Ssam } w; 136429845Ssam 136529845Ssam w.w = v; 136629845Ssam *cp++ = w.c[0]; 136729845Ssam *cp++ = w.c[1]; 136829845Ssam } 136929845Ssam 137029845Ssam putl(cp, v) 137129845Ssam char *cp; 137229845Ssam long v; 137329845Ssam { 137429845Ssam union { 137529845Ssam long l; 137629845Ssam char c[4]; 137729845Ssam } l; 137829845Ssam 137929845Ssam l.l = v; 138029845Ssam *cp++ = l.c[0]; 138129845Ssam *cp++ = l.c[1]; 138229845Ssam *cp++ = l.c[2]; 138329845Ssam *cp++ = l.c[3]; 138429845Ssam } 138529845Ssam #endif 138629845Ssam 1387650Sbill /* 1388650Sbill * This routine relocates the single text or data segment argument. 1389650Sbill * Offsets from external symbols are resolved by adding the value 1390650Sbill * of the external symbols. Non-external reference are updated to account 1391650Sbill * for the relative motion of the segments (ctrel, cdrel, ...). If 1392650Sbill * a relocation was pc-relative, then we update it to reflect the 1393650Sbill * change in the positioning of the segments by adding the displacement 1394650Sbill * of the referenced segment and subtracting the displacement of the 1395650Sbill * current segment (creloc). 1396650Sbill * 1397650Sbill * If we are saving the relocation information, then we increase 1398650Sbill * each relocation datum address by our base position in the new segment. 1399650Sbill */ 1400650Sbill load2td(creloc, position, b1, b2) 140130647Slepreau long creloc, position; 1402615Sbill struct biobuf *b1, *b2; 1403615Sbill { 1404615Sbill register struct nlist *sp; 1405615Sbill register struct local *lp; 1406615Sbill long tw; 1407615Sbill register struct relocation_info *rp, *rpend; 1408615Sbill struct relocation_info *relp; 1409615Sbill char *codep; 1410615Sbill register char *cp; 1411615Sbill int relsz, codesz; 1412615Sbill 1413615Sbill relsz = reloc.size; 1414615Sbill relp = (struct relocation_info *)malloc(relsz); 1415615Sbill codesz = text.size; 1416615Sbill codep = (char *)malloc(codesz); 1417615Sbill if (relp == 0 || codep == 0) 1418615Sbill error(1, "out of memory (load2td)"); 1419615Sbill mget((char *)relp, relsz, &reloc); 1420615Sbill rpend = &relp[relsz / sizeof (struct relocation_info)]; 1421615Sbill mget(codep, codesz, &text); 1422615Sbill for (rp = relp; rp < rpend; rp++) { 1423615Sbill cp = codep + rp->r_address; 1424650Sbill /* 1425650Sbill * Pick up previous value at location to be relocated. 1426650Sbill */ 1427615Sbill switch (rp->r_length) { 1428615Sbill 1429615Sbill case 0: /* byte */ 1430615Sbill tw = *cp; 1431615Sbill break; 1432615Sbill 1433615Sbill case 1: /* word */ 143429845Ssam tw = getw(cp); 1435615Sbill break; 1436615Sbill 1437615Sbill case 2: /* long */ 143829845Ssam tw = getl(cp); 1439615Sbill break; 1440615Sbill 1441615Sbill default: 1442615Sbill error(1, "load2td botch: bad length"); 1443615Sbill } 1444650Sbill /* 1445650Sbill * If relative to an external which is defined, 1446650Sbill * resolve to a simpler kind of reference in the 1447650Sbill * result file. If the external is undefined, just 1448650Sbill * convert the symbol number to the number of the 1449650Sbill * symbol in the result file and leave it undefined. 1450650Sbill */ 1451615Sbill if (rp->r_extern) { 1452650Sbill /* 1453650Sbill * Search the hash table which maps local 1454650Sbill * symbol numbers to symbol tables entries 1455650Sbill * in the new a.out file. 1456650Sbill */ 1457615Sbill lp = lochash[rp->r_symbolnum % LHSIZ]; 1458615Sbill while (lp->l_index != rp->r_symbolnum) { 1459615Sbill lp = lp->l_link; 1460615Sbill if (lp == 0) 1461615Sbill error(1, "local symbol botch"); 1462615Sbill } 1463615Sbill sp = lp->l_symbol; 1464615Sbill if (sp->n_type == N_EXT+N_UNDF) 1465615Sbill rp->r_symbolnum = nsym+symx(sp); 1466615Sbill else { 1467615Sbill rp->r_symbolnum = sp->n_type & N_TYPE; 1468615Sbill tw += sp->n_value; 1469615Sbill rp->r_extern = 0; 1470615Sbill } 1471615Sbill } else switch (rp->r_symbolnum & N_TYPE) { 1472650Sbill /* 1473650Sbill * Relocation is relative to the loaded position 1474650Sbill * of another segment. Update by the change in position 1475650Sbill * of that segment. 1476650Sbill */ 1477615Sbill case N_TEXT: 1478615Sbill tw += ctrel; 1479615Sbill break; 1480615Sbill case N_DATA: 1481615Sbill tw += cdrel; 1482615Sbill break; 1483615Sbill case N_BSS: 1484615Sbill tw += cbrel; 1485615Sbill break; 1486615Sbill case N_ABS: 1487615Sbill break; 1488615Sbill default: 1489615Sbill error(1, "relocation format botch (symbol type))"); 1490615Sbill } 1491650Sbill /* 1492650Sbill * Relocation is pc relative, so decrease the relocation 1493650Sbill * by the amount the current segment is displaced. 1494650Sbill * (E.g if we are a relative reference to a text location 1495650Sbill * from data space, we added the increase in the text address 1496650Sbill * above, and subtract the increase in our (data) address 1497650Sbill * here, leaving the net change the relative change in the 1498650Sbill * positioning of our text and data segments.) 1499650Sbill */ 1500615Sbill if (rp->r_pcrel) 1501615Sbill tw -= creloc; 1502650Sbill /* 1503650Sbill * Put the value back in the segment, 1504650Sbill * while checking for overflow. 1505650Sbill */ 1506615Sbill switch (rp->r_length) { 1507615Sbill 1508615Sbill case 0: /* byte */ 1509615Sbill if (tw < -128 || tw > 127) 1510615Sbill error(0, "byte displacement overflow"); 1511615Sbill *cp = tw; 1512615Sbill break; 1513615Sbill case 1: /* word */ 1514615Sbill if (tw < -32768 || tw > 32767) 1515615Sbill error(0, "word displacement overflow"); 151629845Ssam putw(cp, tw); 1517615Sbill break; 1518615Sbill case 2: /* long */ 151929845Ssam putl(cp, tw); 1520615Sbill break; 1521615Sbill } 1522650Sbill /* 1523650Sbill * If we are saving relocation information, 1524650Sbill * we must convert the address in the segment from 1525650Sbill * the old .o file into an address in the segment in 1526650Sbill * the new a.out, by adding the position of our 1527650Sbill * segment in the new larger segment. 1528650Sbill */ 1529615Sbill if (rflag) 1530650Sbill rp->r_address += position; 1531615Sbill } 1532615Sbill bwrite(codep, codesz, b1); 1533615Sbill if (rflag) 1534615Sbill bwrite(relp, relsz, b2); 153525483Slepreau free((char *)relp); 153625483Slepreau free(codep); 1537615Sbill } 1538615Sbill 1539615Sbill finishout() 1540615Sbill { 1541615Sbill register int i; 154240407Smckusick char *newname; 1543615Sbill int nsymt; 1544615Sbill 1545615Sbill if (sflag==0) { 1546615Sbill nsymt = symx(nextsym); 1547615Sbill for (i = 0; i < nsymt; i++) 1548615Sbill symwrite(xsym(i), sout); 1549615Sbill bwrite(&offset, sizeof offset, sout); 1550615Sbill } 1551615Sbill if (!ofilfnd) { 155240407Smckusick newname = (char *)genbuildname("a.out"); 155340407Smckusick unlink(newname); 155440407Smckusick if (link(defaultname, newname) < 0) 1555898Sbill error(1, "cannot move l.out to a.out"); 155640407Smckusick ofilename = newname; 1557615Sbill } 1558615Sbill delarg = errlev; 1559615Sbill delexit(); 1560615Sbill } 1561615Sbill 1562615Sbill mkfsym(s) 1563615Sbill char *s; 1564615Sbill { 1565615Sbill 1566615Sbill if (sflag || xflag) 1567615Sbill return; 1568615Sbill cursym.n_un.n_name = s; 156930836Sbostic cursym.n_type = N_EXT | N_FN; 1570615Sbill cursym.n_value = torigin; 1571615Sbill symwrite(&cursym, sout); 1572615Sbill } 1573615Sbill 1574615Sbill getarhdr() 1575615Sbill { 1576615Sbill register char *cp; 1577615Sbill 1578615Sbill mget((char *)&archdr, sizeof archdr, &text); 1579615Sbill for (cp=archdr.ar_name; cp<&archdr.ar_name[sizeof(archdr.ar_name)];) 1580615Sbill if (*cp++ == ' ') { 1581615Sbill cp[-1] = 0; 1582615Sbill return; 1583615Sbill } 1584615Sbill } 1585615Sbill 1586615Sbill mget(loc, n, sp) 1587615Sbill register STREAM *sp; 1588615Sbill register char *loc; 1589615Sbill { 1590615Sbill register char *p; 1591615Sbill register int take; 1592615Sbill 1593615Sbill top: 1594615Sbill if (n == 0) 1595615Sbill return; 1596615Sbill if (sp->size && sp->nibuf) { 1597615Sbill p = sp->ptr; 1598615Sbill take = sp->size; 1599615Sbill if (take > sp->nibuf) 1600615Sbill take = sp->nibuf; 1601615Sbill if (take > n) 1602615Sbill take = n; 1603615Sbill n -= take; 1604615Sbill sp->size -= take; 1605615Sbill sp->nibuf -= take; 1606615Sbill sp->pos += take; 1607615Sbill do 1608615Sbill *loc++ = *p++; 1609615Sbill while (--take > 0); 1610615Sbill sp->ptr = p; 1611615Sbill goto top; 1612615Sbill } 161316068Sralph if (n > p_blksize) { 161416068Sralph take = n - n % p_blksize; 161516068Sralph lseek(infil, (sp->bno+1)<<p_blkshift, 0); 1616615Sbill if (take > sp->size || read(infil, loc, take) != take) 1617615Sbill error(1, "premature EOF"); 1618615Sbill loc += take; 1619615Sbill n -= take; 1620615Sbill sp->size -= take; 1621615Sbill sp->pos += take; 162216068Sralph dseek(sp, (sp->bno+1+(take>>p_blkshift))<<p_blkshift, -1); 1623615Sbill goto top; 1624615Sbill } 1625615Sbill *loc++ = get(sp); 1626615Sbill --n; 1627615Sbill goto top; 1628615Sbill } 1629615Sbill 1630615Sbill symwrite(sp, bp) 1631615Sbill struct nlist *sp; 1632615Sbill struct biobuf *bp; 1633615Sbill { 1634615Sbill register int len; 1635615Sbill register char *str; 1636615Sbill 1637615Sbill str = sp->n_un.n_name; 1638615Sbill if (str) { 1639615Sbill sp->n_un.n_strx = offset; 1640615Sbill len = strlen(str) + 1; 1641615Sbill bwrite(str, len, strout); 1642615Sbill offset += len; 1643615Sbill } 1644615Sbill bwrite(sp, sizeof (*sp), bp); 1645615Sbill sp->n_un.n_name = str; 1646615Sbill } 1647615Sbill 1648615Sbill dseek(sp, loc, s) 1649615Sbill register STREAM *sp; 1650615Sbill long loc, s; 1651615Sbill { 1652615Sbill register PAGE *p; 1653615Sbill register b, o; 1654615Sbill int n; 1655615Sbill 165616068Sralph b = loc>>p_blkshift; 165716068Sralph o = loc&p_blkmask; 1658615Sbill if (o&01) 1659615Sbill error(1, "loader error; odd offset"); 1660615Sbill --sp->pno->nuser; 1661615Sbill if ((p = &page[0])->bno!=b && (p = &page[1])->bno!=b) 1662615Sbill if (p->nuser==0 || (p = &page[0])->nuser==0) { 1663615Sbill if (page[0].nuser==0 && page[1].nuser==0) 1664615Sbill if (page[0].bno < page[1].bno) 1665615Sbill p = &page[0]; 1666615Sbill p->bno = b; 166716068Sralph lseek(infil, loc & ~(long)p_blkmask, 0); 166816068Sralph if ((n = read(infil, p->buff, p_blksize)) < 0) 1669615Sbill n = 0; 1670615Sbill p->nibuf = n; 167116068Sralph } else 167216068Sralph error(1, "botch: no pages"); 1673615Sbill ++p->nuser; 1674615Sbill sp->bno = b; 1675615Sbill sp->pno = p; 1676615Sbill if (s != -1) {sp->size = s; sp->pos = 0;} 1677615Sbill sp->ptr = (char *)(p->buff + o); 1678615Sbill if ((sp->nibuf = p->nibuf-o) <= 0) 1679615Sbill sp->size = 0; 1680615Sbill } 1681615Sbill 1682615Sbill char 1683615Sbill get(asp) 1684615Sbill STREAM *asp; 1685615Sbill { 1686615Sbill register STREAM *sp; 1687615Sbill 1688615Sbill sp = asp; 1689615Sbill if ((sp->nibuf -= sizeof(char)) < 0) { 169016068Sralph dseek(sp, ((long)(sp->bno+1)<<p_blkshift), (long)-1); 1691615Sbill sp->nibuf -= sizeof(char); 1692615Sbill } 1693615Sbill if ((sp->size -= sizeof(char)) <= 0) { 1694615Sbill if (sp->size < 0) 1695615Sbill error(1, "premature EOF"); 1696615Sbill ++fpage.nuser; 1697615Sbill --sp->pno->nuser; 1698615Sbill sp->pno = (PAGE *) &fpage; 1699615Sbill } 1700615Sbill sp->pos += sizeof(char); 1701615Sbill return(*sp->ptr++); 1702615Sbill } 1703615Sbill 1704615Sbill getfile(acp) 1705615Sbill char *acp; 1706615Sbill { 1707615Sbill register int c; 1708615Sbill char arcmag[SARMAG+1]; 1709615Sbill struct stat stb; 1710615Sbill 1711615Sbill archdr.ar_name[0] = '\0'; 171217133Ssam filname = acp; 171317133Ssam if (filname[0] == '-' && filname[1] == 'l') 171417133Ssam infil = libopen(filname + 2, O_RDONLY); 171517133Ssam else 171640407Smckusick infil = open((char *)genbuildname(filname), O_RDONLY); 171717133Ssam if (infil < 0) 1718615Sbill error(1, "cannot open"); 171916068Sralph fstat(infil, &stb); 1720615Sbill page[0].bno = page[1].bno = -1; 1721615Sbill page[0].nuser = page[1].nuser = 0; 172216068Sralph c = stb.st_blksize; 172316068Sralph if (c == 0 || (c & (c - 1)) != 0) { 172416068Sralph /* use default size if not a power of two */ 172516068Sralph c = BLKSIZE; 172616068Sralph } 172716068Sralph if (p_blksize != c) { 172816068Sralph p_blksize = c; 172916068Sralph p_blkmask = c - 1; 173016068Sralph for (p_blkshift = 0; c > 1 ; p_blkshift++) 173116068Sralph c >>= 1; 173216068Sralph if (page[0].buff != NULL) 173316068Sralph free(page[0].buff); 173416068Sralph page[0].buff = (char *)malloc(p_blksize); 173516068Sralph if (page[0].buff == NULL) 173616068Sralph error(1, "ran out of memory (getfile)"); 173716068Sralph if (page[1].buff != NULL) 173816068Sralph free(page[1].buff); 173916068Sralph page[1].buff = (char *)malloc(p_blksize); 174016068Sralph if (page[1].buff == NULL) 174116068Sralph error(1, "ran out of memory (getfile)"); 174216068Sralph } 1743615Sbill text.pno = reloc.pno = (PAGE *) &fpage; 1744615Sbill fpage.nuser = 2; 1745615Sbill dseek(&text, 0L, SARMAG); 1746615Sbill if (text.size <= 0) 1747615Sbill error(1, "premature EOF"); 1748615Sbill mget((char *)arcmag, SARMAG, &text); 1749615Sbill arcmag[SARMAG] = 0; 1750615Sbill if (strcmp(arcmag, ARMAG)) 1751615Sbill return (0); 1752615Sbill dseek(&text, SARMAG, sizeof archdr); 175317133Ssam if (text.size <= 0) 1754615Sbill return (1); 1755615Sbill getarhdr(); 175630828Sbostic if (strncmp(archdr.ar_name, RANLIBMAG, sizeof(archdr.ar_name)) != 0) 1757615Sbill return (1); 1758615Sbill return (stb.st_mtime > atol(archdr.ar_date) ? 3 : 2); 1759615Sbill } 1760615Sbill 176117133Ssam /* 176217133Ssam * Search for a library with given name 176317133Ssam * using the directory search array. 176417133Ssam */ 176517133Ssam libopen(name, oflags) 176617133Ssam char *name; 176717133Ssam int oflags; 176817133Ssam { 176917133Ssam register char *p, *cp; 177017133Ssam register int i; 177117133Ssam static char buf[MAXPATHLEN+1]; 177217133Ssam int fd = -1; 177317133Ssam 177417133Ssam if (*name == '\0') /* backwards compat */ 177517133Ssam name = "a"; 177617133Ssam for (i = 0; i < ndir && fd == -1; i++) { 177717133Ssam p = buf; 177817133Ssam for (cp = dirs[i]; *cp; *p++ = *cp++) 177917133Ssam ; 178017133Ssam *p++ = '/'; 178117133Ssam for (cp = "lib"; *cp; *p++ = *cp++) 178217133Ssam ; 178317133Ssam for (cp = name; *cp; *p++ = *cp++) 178417133Ssam ; 178517133Ssam cp = ".a"; 178617133Ssam while (*p++ = *cp++) 178717133Ssam ; 178817133Ssam fd = open(buf, oflags); 178917133Ssam } 179017133Ssam if (fd != -1) 179117133Ssam filname = buf; 179217133Ssam return (fd); 179317133Ssam } 179417133Ssam 1795615Sbill struct nlist ** 1796615Sbill lookup() 1797615Sbill { 1798615Sbill register int sh; 1799615Sbill register struct nlist **hp; 1800615Sbill register char *cp, *cp1; 1801615Sbill register struct symseg *gp; 1802615Sbill register int i; 1803615Sbill 1804615Sbill sh = 0; 1805615Sbill for (cp = cursym.n_un.n_name; *cp;) 1806615Sbill sh = (sh<<1) + *cp++; 1807615Sbill sh = (sh & 0x7fffffff) % HSIZE; 1808615Sbill for (gp = symseg; gp < &symseg[NSEG]; gp++) { 1809615Sbill if (gp->sy_first == 0) { 1810615Sbill gp->sy_first = (struct nlist *) 1811615Sbill calloc(NSYM, sizeof (struct nlist)); 1812615Sbill gp->sy_hfirst = (struct nlist **) 1813615Sbill calloc(HSIZE, sizeof (struct nlist *)); 1814615Sbill if (gp->sy_first == 0 || gp->sy_hfirst == 0) 1815615Sbill error(1, "ran out of space for symbol table"); 1816615Sbill gp->sy_last = gp->sy_first + NSYM; 1817615Sbill gp->sy_hlast = gp->sy_hfirst + HSIZE; 1818615Sbill } 1819615Sbill if (gp > csymseg) 1820615Sbill csymseg = gp; 1821615Sbill hp = gp->sy_hfirst + sh; 1822615Sbill i = 1; 1823615Sbill do { 1824615Sbill if (*hp == 0) { 1825615Sbill if (gp->sy_used == NSYM) 1826615Sbill break; 1827615Sbill return (hp); 1828615Sbill } 1829615Sbill cp1 = (*hp)->n_un.n_name; 1830615Sbill for (cp = cursym.n_un.n_name; *cp == *cp1++;) 1831615Sbill if (*cp++ == 0) 1832615Sbill return (hp); 1833615Sbill hp += i; 1834615Sbill i += 2; 1835615Sbill if (hp >= gp->sy_hlast) 1836615Sbill hp -= HSIZE; 1837615Sbill } while (i < HSIZE); 1838615Sbill if (i > HSIZE) 1839615Sbill error(1, "hash table botch"); 1840615Sbill } 1841615Sbill error(1, "symbol table overflow"); 1842615Sbill /*NOTREACHED*/ 1843615Sbill } 1844615Sbill 1845615Sbill symfree(saved) 1846615Sbill struct nlist *saved; 1847615Sbill { 1848615Sbill register struct symseg *gp; 1849615Sbill register struct nlist *sp; 1850615Sbill 1851615Sbill for (gp = csymseg; gp >= symseg; gp--, csymseg--) { 1852615Sbill sp = gp->sy_first + gp->sy_used; 1853615Sbill if (sp == saved) { 1854615Sbill nextsym = sp; 1855615Sbill return; 1856615Sbill } 1857615Sbill for (sp--; sp >= gp->sy_first; sp--) { 1858615Sbill gp->sy_hfirst[sp->n_hash] = 0; 1859615Sbill gp->sy_used--; 1860615Sbill if (sp == saved) { 1861615Sbill nextsym = sp; 1862615Sbill return; 1863615Sbill } 1864615Sbill } 1865615Sbill } 1866615Sbill if (saved == 0) 1867615Sbill return; 1868615Sbill error(1, "symfree botch"); 1869615Sbill } 1870615Sbill 1871615Sbill struct nlist ** 1872615Sbill slookup(s) 1873615Sbill char *s; 1874615Sbill { 1875615Sbill 1876615Sbill cursym.n_un.n_name = s; 1877615Sbill cursym.n_type = N_EXT+N_UNDF; 1878615Sbill cursym.n_value = 0; 1879615Sbill return (lookup()); 1880615Sbill } 1881615Sbill 1882615Sbill enter(hp) 1883615Sbill register struct nlist **hp; 1884615Sbill { 1885615Sbill register struct nlist *sp; 1886615Sbill 1887615Sbill if (*hp==0) { 1888615Sbill if (hp < csymseg->sy_hfirst || hp >= csymseg->sy_hlast) 1889615Sbill error(1, "enter botch"); 1890615Sbill *hp = lastsym = sp = csymseg->sy_first + csymseg->sy_used; 1891615Sbill csymseg->sy_used++; 1892615Sbill sp->n_un.n_name = cursym.n_un.n_name; 1893615Sbill sp->n_type = cursym.n_type; 1894615Sbill sp->n_hash = hp - csymseg->sy_hfirst; 1895615Sbill sp->n_value = cursym.n_value; 1896615Sbill nextsym = lastsym + 1; 1897615Sbill return(1); 1898615Sbill } else { 1899615Sbill lastsym = *hp; 1900615Sbill return(0); 1901615Sbill } 1902615Sbill } 1903615Sbill 1904615Sbill symx(sp) 1905615Sbill struct nlist *sp; 1906615Sbill { 1907615Sbill register struct symseg *gp; 1908615Sbill 1909615Sbill if (sp == 0) 1910615Sbill return (0); 1911615Sbill for (gp = csymseg; gp >= symseg; gp--) 1912615Sbill /* <= is sloppy so nextsym will always work */ 1913615Sbill if (sp >= gp->sy_first && sp <= gp->sy_last) 1914615Sbill return ((gp - symseg) * NSYM + sp - gp->sy_first); 1915615Sbill error(1, "symx botch"); 1916615Sbill /*NOTREACHED*/ 1917615Sbill } 1918615Sbill 1919615Sbill symreloc() 1920615Sbill { 1921615Sbill if(funding) return; 1922615Sbill switch (cursym.n_type & 017) { 1923615Sbill 1924615Sbill case N_TEXT: 1925615Sbill case N_EXT+N_TEXT: 1926615Sbill cursym.n_value += ctrel; 1927615Sbill return; 1928615Sbill 1929615Sbill case N_DATA: 1930615Sbill case N_EXT+N_DATA: 1931615Sbill cursym.n_value += cdrel; 1932615Sbill return; 1933615Sbill 1934615Sbill case N_BSS: 1935615Sbill case N_EXT+N_BSS: 1936615Sbill cursym.n_value += cbrel; 1937615Sbill return; 1938615Sbill 1939615Sbill case N_EXT+N_UNDF: 1940615Sbill return; 1941615Sbill 1942615Sbill default: 1943615Sbill if (cursym.n_type&N_EXT) 1944615Sbill cursym.n_type = N_EXT+N_ABS; 1945615Sbill return; 1946615Sbill } 1947615Sbill } 1948615Sbill 1949615Sbill error(n, s) 1950615Sbill char *s; 1951615Sbill { 1952898Sbill 1953615Sbill if (errlev==0) 1954615Sbill printf("ld:"); 1955615Sbill if (filname) { 1956615Sbill printf("%s", filname); 1957615Sbill if (n != -1 && archdr.ar_name[0]) 1958615Sbill printf("(%s)", archdr.ar_name); 1959615Sbill printf(": "); 1960615Sbill } 1961615Sbill printf("%s\n", s); 1962615Sbill if (n == -1) 1963615Sbill return; 1964615Sbill if (n) 1965615Sbill delexit(); 1966615Sbill errlev = 2; 1967615Sbill } 1968615Sbill 1969615Sbill readhdr(loc) 1970615Sbill off_t loc; 1971615Sbill { 1972615Sbill 1973615Sbill dseek(&text, loc, (long)sizeof(filhdr)); 1974615Sbill mget((short *)&filhdr, sizeof(filhdr), &text); 1975615Sbill if (N_BADMAG(filhdr)) { 1976615Sbill if (filhdr.a_magic == OARMAG) 1977615Sbill error(1, "old archive"); 1978615Sbill error(1, "bad magic number"); 1979615Sbill } 1980615Sbill if (filhdr.a_text&01 || filhdr.a_data&01) 1981615Sbill error(1, "text/data size odd"); 1982615Sbill if (filhdr.a_magic == NMAGIC || filhdr.a_magic == ZMAGIC) { 198312671Ssam cdrel = -round(filhdr.a_text, pagesize); 1984615Sbill cbrel = cdrel - filhdr.a_data; 1985615Sbill } else if (filhdr.a_magic == OMAGIC) { 1986615Sbill cdrel = -filhdr.a_text; 1987615Sbill cbrel = cdrel - filhdr.a_data; 1988615Sbill } else 1989615Sbill error(1, "bad format"); 1990615Sbill } 1991615Sbill 1992615Sbill round(v, r) 1993615Sbill int v; 1994615Sbill u_long r; 1995615Sbill { 1996615Sbill 1997615Sbill r--; 1998615Sbill v += r; 1999615Sbill v &= ~(long)r; 2000615Sbill return(v); 2001615Sbill } 2002615Sbill 2003615Sbill #define NSAVETAB 8192 2004615Sbill char *savetab; 2005615Sbill int saveleft; 2006615Sbill 2007615Sbill char * 2008615Sbill savestr(cp) 2009615Sbill register char *cp; 2010615Sbill { 2011615Sbill register int len; 2012615Sbill 2013615Sbill len = strlen(cp) + 1; 2014615Sbill if (len > saveleft) { 2015615Sbill saveleft = NSAVETAB; 2016615Sbill if (len > saveleft) 2017615Sbill saveleft = len; 201817133Ssam savetab = malloc(saveleft); 2019615Sbill if (savetab == 0) 2020615Sbill error(1, "ran out of memory (savestr)"); 2021615Sbill } 2022615Sbill strncpy(savetab, cp, len); 2023615Sbill cp = savetab; 2024615Sbill savetab += len; 2025615Sbill saveleft -= len; 2026615Sbill return (cp); 2027615Sbill } 2028615Sbill 202916068Sralph bopen(bp, off, bufsize) 203016068Sralph register struct biobuf *bp; 2031615Sbill { 2032615Sbill 203317133Ssam bp->b_ptr = bp->b_buf = malloc(bufsize); 203416068Sralph if (bp->b_ptr == (char *)0) 203516068Sralph error(1, "ran out of memory (bopen)"); 203616068Sralph bp->b_bufsize = bufsize; 203716068Sralph bp->b_nleft = bufsize - (off % bufsize); 2038615Sbill bp->b_off = off; 2039615Sbill bp->b_link = biobufs; 2040615Sbill biobufs = bp; 2041615Sbill } 2042615Sbill 2043615Sbill int bwrerror; 2044615Sbill 2045615Sbill bwrite(p, cnt, bp) 2046615Sbill register char *p; 2047615Sbill register int cnt; 2048615Sbill register struct biobuf *bp; 2049615Sbill { 2050615Sbill register int put; 2051615Sbill register char *to; 2052615Sbill 2053615Sbill top: 2054615Sbill if (cnt == 0) 2055615Sbill return; 2056615Sbill if (bp->b_nleft) { 2057615Sbill put = bp->b_nleft; 2058615Sbill if (put > cnt) 2059615Sbill put = cnt; 2060615Sbill bp->b_nleft -= put; 2061615Sbill to = bp->b_ptr; 206225419Sbloom bcopy(p, to, put); 2063615Sbill bp->b_ptr += put; 2064615Sbill p += put; 2065615Sbill cnt -= put; 2066615Sbill goto top; 2067615Sbill } 206816068Sralph if (cnt >= bp->b_bufsize) { 2069615Sbill if (bp->b_ptr != bp->b_buf) 2070615Sbill bflush1(bp); 207116068Sralph put = cnt - cnt % bp->b_bufsize; 2072615Sbill if (boffset != bp->b_off) 2073615Sbill lseek(biofd, bp->b_off, 0); 2074615Sbill if (write(biofd, p, put) != put) { 2075615Sbill bwrerror = 1; 2076615Sbill error(1, "output write error"); 2077615Sbill } 2078615Sbill bp->b_off += put; 2079615Sbill boffset = bp->b_off; 2080615Sbill p += put; 2081615Sbill cnt -= put; 2082615Sbill goto top; 2083615Sbill } 2084615Sbill bflush1(bp); 2085615Sbill goto top; 2086615Sbill } 2087615Sbill 2088615Sbill bflush() 2089615Sbill { 2090615Sbill register struct biobuf *bp; 2091615Sbill 2092615Sbill if (bwrerror) 2093615Sbill return; 2094615Sbill for (bp = biobufs; bp; bp = bp->b_link) 2095615Sbill bflush1(bp); 2096615Sbill } 2097615Sbill 2098615Sbill bflush1(bp) 2099615Sbill register struct biobuf *bp; 2100615Sbill { 2101615Sbill register int cnt = bp->b_ptr - bp->b_buf; 2102615Sbill 2103615Sbill if (cnt == 0) 2104615Sbill return; 2105615Sbill if (boffset != bp->b_off) 2106615Sbill lseek(biofd, bp->b_off, 0); 2107615Sbill if (write(biofd, bp->b_buf, cnt) != cnt) { 2108615Sbill bwrerror = 1; 2109615Sbill error(1, "output write error"); 2110615Sbill } 2111615Sbill bp->b_off += cnt; 2112615Sbill boffset = bp->b_off; 2113615Sbill bp->b_ptr = bp->b_buf; 211416068Sralph bp->b_nleft = bp->b_bufsize; 2115615Sbill } 2116615Sbill 2117615Sbill bflushc(bp, c) 2118615Sbill register struct biobuf *bp; 2119615Sbill { 2120615Sbill 2121615Sbill bflush1(bp); 2122615Sbill bputc(c, bp); 2123615Sbill } 212416068Sralph 212516068Sralph bseek(bp, off) 212616068Sralph register struct biobuf *bp; 212716068Sralph register off_t off; 212816068Sralph { 212916068Sralph bflush1(bp); 213016068Sralph 213116068Sralph bp->b_nleft = bp->b_bufsize - (off % bp->b_bufsize); 213216068Sralph bp->b_off = off; 213316068Sralph } 2134