xref: /csrg-svn/old/ld/ld.c (revision 44293)
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*44293Sbostic static char sccsid[] = "@(#)ld.c	5.15 (Berkeley) 06/26/90";
1519842Sdist #endif not lint
1619842Sdist 
17615Sbill /*
18898Sbill  * ld - string table version for VAX
19615Sbill  */
20615Sbill 
2117133Ssam #include <sys/param.h>
2242415Sbostic #include <sys/stat.h>
2342415Sbostic #include <sys/file.h>
2442415Sbostic #include <sys/signal.h>
25650Sbill #include <ar.h>
26650Sbill #include <a.out.h>
27615Sbill #include <ranlib.h>
2842415Sbostic #include <stdio.h>
2942415Sbostic #include <ctype.h>
3042415Sbostic #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 {
105242415Sbostic 	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 */
106142415Sbostic 		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 	}
1068*44293Sbostic #ifdef hp300
1069*44293Sbostic 	filhdr.a_mid = (rflag ? MID_ZERO : MID_HP300);
1070*44293Sbostic #endif
1071615Sbill 	filhdr.a_magic = nflag ? NMAGIC : (zflag ? ZMAGIC : OMAGIC);
1072615Sbill 	filhdr.a_text = nflag ? tsize :
107312671Ssam 	    round(tsize, zflag ? pagesize : sizeof (long));
107412671Ssam 	filhdr.a_data = zflag ? round(dsize, pagesize) : dsize;
1075615Sbill 	bss = bsize - (filhdr.a_data - dsize);
1076615Sbill 	if (bss < 0)
1077615Sbill 		bss = 0;
1078615Sbill 	filhdr.a_bss = bss;
1079615Sbill 	filhdr.a_trsize = trsize;
1080615Sbill 	filhdr.a_drsize = drsize;
1081615Sbill 	filhdr.a_syms = sflag? 0: (ssize + (sizeof cursym)*symx(nextsym));
1082615Sbill 	if (entrypt) {
1083615Sbill 		if (entrypt->n_type!=N_EXT+N_TEXT)
1084615Sbill 			error(0, "entry point not in text");
1085615Sbill 		else
1086615Sbill 			filhdr.a_entry = entrypt->n_value;
1087615Sbill 	} else
1088615Sbill 		filhdr.a_entry = 0;
1089615Sbill 	filhdr.a_trsize = (rflag ? trsize:0);
1090615Sbill 	filhdr.a_drsize = (rflag ? drsize:0);
109116068Sralph 	tout = &toutb;
109216068Sralph 	bopen(tout, 0, stbuf.st_blksize);
1093615Sbill 	bwrite((char *)&filhdr, sizeof (filhdr), tout);
109416068Sralph 	if (zflag)
109516068Sralph 		bseek(tout, pagesize);
1096615Sbill 	wroff = N_TXTOFF(filhdr) + filhdr.a_text;
109716068Sralph 	outb(&dout, filhdr.a_data, stbuf.st_blksize);
1098615Sbill 	if (rflag) {
109916068Sralph 		outb(&trout, filhdr.a_trsize, stbuf.st_blksize);
110016068Sralph 		outb(&drout, filhdr.a_drsize, stbuf.st_blksize);
1101615Sbill 	}
1102615Sbill 	if (sflag==0 || xflag==0) {
110316068Sralph 		outb(&sout, filhdr.a_syms, stbuf.st_blksize);
1104615Sbill 		wroff += sizeof (offset);
110516068Sralph 		outb(&strout, 0, stbuf.st_blksize);
1106615Sbill 	}
1107615Sbill }
1108615Sbill 
110916068Sralph outb(bp, inc, bufsize)
1110615Sbill 	register struct biobuf **bp;
1111615Sbill {
1112615Sbill 
1113615Sbill 	*bp = (struct biobuf *)malloc(sizeof (struct biobuf));
1114615Sbill 	if (*bp == 0)
1115615Sbill 		error(1, "ran out of memory (outb)");
111616068Sralph 	bopen(*bp, wroff, bufsize);
1117615Sbill 	wroff += inc;
1118615Sbill }
1119615Sbill 
1120615Sbill load2arg(acp)
1121615Sbill char *acp;
1122615Sbill {
1123615Sbill 	register char *cp;
1124615Sbill 	off_t loc;
1125615Sbill 
1126615Sbill 	cp = acp;
1127615Sbill 	if (getfile(cp) == 0) {
1128615Sbill 		while (*cp)
1129615Sbill 			cp++;
1130615Sbill 		while (cp >= acp && *--cp != '/');
1131615Sbill 		mkfsym(++cp);
1132615Sbill 		load2(0L);
1133615Sbill 	} else {	/* scan archive members referenced */
1134615Sbill 		for (;;) {
1135615Sbill 			if (clibseg->li_used2 == clibseg->li_used) {
1136615Sbill 				if (clibseg->li_used < NROUT)
1137615Sbill 					error(1, "libseg botch");
1138615Sbill 				clibseg++;
1139615Sbill 			}
1140615Sbill 			loc = clibseg->li_first[clibseg->li_used2++];
1141615Sbill 			if (loc == -1)
1142615Sbill 				break;
1143615Sbill 			dseek(&text, loc, (long)sizeof(archdr));
1144615Sbill 			getarhdr();
1145615Sbill 			mkfsym(archdr.ar_name);
1146615Sbill 			load2(loc + (long)sizeof(archdr));
1147615Sbill 		}
1148615Sbill 	}
1149615Sbill 	close(infil);
1150615Sbill }
1151615Sbill 
1152615Sbill load2(loc)
1153615Sbill long loc;
1154615Sbill {
1155615Sbill 	int size;
1156615Sbill 	register struct nlist *sp;
1157615Sbill 	register struct local *lp;
1158615Sbill 	register int symno, i;
1159615Sbill 	int type;
1160615Sbill 
1161615Sbill 	readhdr(loc);
1162650Sbill 	if (!funding) {
1163615Sbill 		ctrel = torigin;
1164615Sbill 		cdrel += dorigin;
1165615Sbill 		cbrel += borigin;
1166615Sbill 	}
1167615Sbill 	/*
1168615Sbill 	 * Reread the symbol table, recording the numbering
1169615Sbill 	 * of symbols for fixing external references.
1170615Sbill 	 */
1171615Sbill 	for (i = 0; i < LHSIZ; i++)
1172615Sbill 		lochash[i] = 0;
1173615Sbill 	clocseg = locseg;
1174615Sbill 	clocseg->lo_used = 0;
1175615Sbill 	symno = -1;
1176615Sbill 	loc += N_TXTOFF(filhdr);
1177615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1178615Sbill 		filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms, sizeof(off_t));
1179615Sbill 	mget(&size, sizeof(size), &text);
1180615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1181615Sbill 		filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms+sizeof(off_t),
1182615Sbill 		size - sizeof(off_t));
1183615Sbill 	curstr = (char *)malloc(size);
1184615Sbill 	if (curstr == NULL)
1185615Sbill 		error(1, "out of space reading string table (pass 2)");
1186615Sbill 	mget(curstr+sizeof(off_t), size-sizeof(off_t), &text);
1187615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1188615Sbill 		filhdr.a_trsize+filhdr.a_drsize, filhdr.a_syms);
1189615Sbill 	while (text.size > 0) {
1190615Sbill 		symno++;
1191615Sbill 		mget((char *)&cursym, sizeof(struct nlist), &text);
1192615Sbill 		if (cursym.n_un.n_strx) {
1193615Sbill 			if (cursym.n_un.n_strx<sizeof(size) ||
1194615Sbill 			    cursym.n_un.n_strx>=size)
1195615Sbill 				error(1, "bad string table index (pass 2)");
1196615Sbill 			cursym.n_un.n_name = curstr + cursym.n_un.n_strx;
1197615Sbill 		}
1198615Sbill /* inline expansion of symreloc() */
1199615Sbill 		switch (cursym.n_type & 017) {
1200615Sbill 
1201615Sbill 		case N_TEXT:
1202615Sbill 		case N_EXT+N_TEXT:
1203615Sbill 			cursym.n_value += ctrel;
1204615Sbill 			break;
1205615Sbill 		case N_DATA:
1206615Sbill 		case N_EXT+N_DATA:
1207615Sbill 			cursym.n_value += cdrel;
1208615Sbill 			break;
1209615Sbill 		case N_BSS:
1210615Sbill 		case N_EXT+N_BSS:
1211615Sbill 			cursym.n_value += cbrel;
1212615Sbill 			break;
1213615Sbill 		case N_EXT+N_UNDF:
1214615Sbill 			break;
1215615Sbill 		default:
1216615Sbill 			if (cursym.n_type&N_EXT)
1217615Sbill 				cursym.n_type = N_EXT+N_ABS;
1218615Sbill 		}
1219615Sbill /* end inline expansion of symreloc() */
1220615Sbill 		type = cursym.n_type;
1221898Sbill 		if (yflag && cursym.n_un.n_name)
1222898Sbill 			for (i = 0; i < yflag; i++)
1223898Sbill 				/* fast check for 2d character! */
1224898Sbill 				if (ytab[i][1] == cursym.n_un.n_name[1] &&
1225898Sbill 				    !strcmp(ytab[i], cursym.n_un.n_name)) {
1226898Sbill 					tracesym();
1227898Sbill 					break;
1228898Sbill 				}
1229615Sbill 		if ((type&N_EXT) == 0) {
1230615Sbill 			if (!sflag&&!xflag&&
1231615Sbill 			    (!Xflag||cursym.n_un.n_name[0]!='L'||type&N_STAB))
1232615Sbill 				symwrite(&cursym, sout);
1233615Sbill 			continue;
1234615Sbill 		}
1235615Sbill 		if (funding)
1236615Sbill 			continue;
1237615Sbill 		if ((sp = *lookup()) == 0)
1238615Sbill 			error(1, "internal error: symbol not found");
1239615Sbill 		if (cursym.n_type == N_EXT+N_UNDF) {
1240615Sbill 			if (clocseg->lo_used == NSYMPR) {
1241615Sbill 				if (++clocseg == &locseg[NSEG])
1242615Sbill 					error(1, "local symbol overflow");
1243615Sbill 				clocseg->lo_used = 0;
1244615Sbill 			}
1245615Sbill 			if (clocseg->lo_first == 0) {
1246615Sbill 				clocseg->lo_first = (struct local *)
1247615Sbill 				    malloc(NSYMPR * sizeof (struct local));
1248615Sbill 				if (clocseg->lo_first == 0)
1249615Sbill 					error(1, "out of memory (clocseg)");
1250615Sbill 			}
1251615Sbill 			lp = &clocseg->lo_first[clocseg->lo_used++];
1252615Sbill 			lp->l_index = symno;
1253615Sbill 			lp->l_symbol = sp;
1254615Sbill 			lp->l_link = lochash[symno % LHSIZ];
1255615Sbill 			lochash[symno % LHSIZ] = lp;
1256615Sbill 			continue;
1257615Sbill 		}
1258615Sbill 		if (cursym.n_type & N_STAB)
1259615Sbill 			continue;
1260615Sbill 		if (cursym.n_type!=sp->n_type || cursym.n_value!=sp->n_value) {
1261615Sbill 			printf("%s: ", cursym.n_un.n_name);
1262615Sbill 			error(0, "multiply defined");
1263615Sbill 		}
1264615Sbill 	}
1265615Sbill 	if (funding)
1266615Sbill 		return;
1267615Sbill 	dseek(&text, loc, filhdr.a_text);
1268615Sbill 	dseek(&reloc, loc+filhdr.a_text+filhdr.a_data, filhdr.a_trsize);
1269650Sbill 	load2td(ctrel, torigin - textbase, tout, trout);
1270615Sbill 	dseek(&text, loc+filhdr.a_text, filhdr.a_data);
1271615Sbill 	dseek(&reloc, loc+filhdr.a_text+filhdr.a_data+filhdr.a_trsize,
1272615Sbill 	    filhdr.a_drsize);
1273650Sbill 	load2td(cdrel, dorigin - database, dout, drout);
1274615Sbill 	while (filhdr.a_data & (sizeof(long)-1)) {
1275615Sbill 		bputc(0, dout);
1276615Sbill 		filhdr.a_data++;
1277615Sbill 	}
1278615Sbill 	torigin += filhdr.a_text;
12791752Sbill 	dorigin += round(filhdr.a_data, sizeof (long));
12801752Sbill 	borigin += round(filhdr.a_bss, sizeof (long));
1281615Sbill 	free(curstr);
1282615Sbill }
1283615Sbill 
1284898Sbill struct tynames {
1285898Sbill 	int	ty_value;
1286898Sbill 	char	*ty_name;
1287898Sbill } tynames[] = {
1288898Sbill 	N_UNDF,	"undefined",
1289898Sbill 	N_ABS,	"absolute",
1290898Sbill 	N_TEXT,	"text",
1291898Sbill 	N_DATA,	"data",
1292898Sbill 	N_BSS,	"bss",
1293898Sbill 	N_COMM,	"common",
1294898Sbill 	0,	0,
1295898Sbill };
1296898Sbill 
1297898Sbill tracesym()
1298898Sbill {
1299898Sbill 	register struct tynames *tp;
1300898Sbill 
1301898Sbill 	if (cursym.n_type & N_STAB)
1302898Sbill 		return;
1303898Sbill 	printf("%s", filname);
1304898Sbill 	if (archdr.ar_name[0])
1305898Sbill 		printf("(%s)", archdr.ar_name);
1306898Sbill 	printf(": ");
1307898Sbill 	if ((cursym.n_type&N_TYPE) == N_UNDF && cursym.n_value) {
1308898Sbill 		printf("definition of common %s size %d\n",
1309898Sbill 		    cursym.n_un.n_name, cursym.n_value);
1310898Sbill 		return;
1311898Sbill 	}
1312898Sbill 	for (tp = tynames; tp->ty_name; tp++)
1313898Sbill 		if (tp->ty_value == (cursym.n_type&N_TYPE))
1314898Sbill 			break;
1315898Sbill 	printf((cursym.n_type&N_TYPE) ? "definition of" : "reference to");
1316898Sbill 	if (cursym.n_type&N_EXT)
1317898Sbill 		printf(" external");
1318898Sbill 	if (tp->ty_name)
1319898Sbill 		printf(" %s", tp->ty_name);
1320898Sbill 	printf(" %s\n", cursym.n_un.n_name);
1321898Sbill }
1322898Sbill 
132329845Ssam #if !defined(tahoe)
132429845Ssam /* for machines which allow arbitrarily aligned word and longword accesses */
132529845Ssam #define	getw(cp)	(*(short *)(cp))
132629845Ssam #define	getl(cp)	(*(long *)(cp))
132729845Ssam #define	putw(cp, w)	(*(short *)(cp) = (w))
132829845Ssam #define	putl(cp, l)	(*(long *)(cp) = (l))
132929845Ssam #else
133029845Ssam short
133129845Ssam getw(cp)
133229845Ssam 	char *cp;
133329845Ssam {
133429845Ssam 	union {
133529845Ssam 		short	w;
133629845Ssam 		char	c[2];
133729845Ssam 	} w;
133829845Ssam 
133929845Ssam 	w.c[0] = *cp++;
134029845Ssam 	w.c[1] = *cp++;
134129845Ssam 	return (w.w);
134229845Ssam }
134329845Ssam 
134429845Ssam getl(cp)
134529845Ssam 	char *cp;
134629845Ssam {
134729845Ssam 	union {
134829845Ssam 		long	l;
134929845Ssam 		char	c[4];
135029845Ssam 	} l;
135129845Ssam 
135229845Ssam 	l.c[0] = *cp++;
135329845Ssam 	l.c[1] = *cp++;
135429845Ssam 	l.c[2] = *cp++;
135529845Ssam 	l.c[3] = *cp++;
135629845Ssam 	return (l.l);
135729845Ssam }
135829845Ssam 
135929845Ssam putw(cp, v)
136029845Ssam 	char *cp;
136129845Ssam 	short v;
136229845Ssam {
136329845Ssam 	union {
136429845Ssam 		short	w;
136529845Ssam 		char	c[2];
136629845Ssam 	} w;
136729845Ssam 
136829845Ssam 	w.w = v;
136929845Ssam 	*cp++ = w.c[0];
137029845Ssam 	*cp++ = w.c[1];
137129845Ssam }
137229845Ssam 
137329845Ssam putl(cp, v)
137429845Ssam 	char *cp;
137529845Ssam 	long v;
137629845Ssam {
137729845Ssam 	union {
137829845Ssam 		long	l;
137929845Ssam 		char	c[4];
138029845Ssam 	} l;
138129845Ssam 
138229845Ssam 	l.l = v;
138329845Ssam 	*cp++ = l.c[0];
138429845Ssam 	*cp++ = l.c[1];
138529845Ssam 	*cp++ = l.c[2];
138629845Ssam 	*cp++ = l.c[3];
138729845Ssam }
138829845Ssam #endif
138929845Ssam 
1390650Sbill /*
1391650Sbill  * This routine relocates the single text or data segment argument.
1392650Sbill  * Offsets from external symbols are resolved by adding the value
1393650Sbill  * of the external symbols.  Non-external reference are updated to account
1394650Sbill  * for the relative motion of the segments (ctrel, cdrel, ...).  If
1395650Sbill  * a relocation was pc-relative, then we update it to reflect the
1396650Sbill  * change in the positioning of the segments by adding the displacement
1397650Sbill  * of the referenced segment and subtracting the displacement of the
1398650Sbill  * current segment (creloc).
1399650Sbill  *
1400650Sbill  * If we are saving the relocation information, then we increase
1401650Sbill  * each relocation datum address by our base position in the new segment.
1402650Sbill  */
1403650Sbill load2td(creloc, position, b1, b2)
140430647Slepreau 	long creloc, position;
1405615Sbill 	struct biobuf *b1, *b2;
1406615Sbill {
1407615Sbill 	register struct nlist *sp;
1408615Sbill 	register struct local *lp;
1409615Sbill 	long tw;
1410615Sbill 	register struct relocation_info *rp, *rpend;
1411615Sbill 	struct relocation_info *relp;
1412615Sbill 	char *codep;
1413615Sbill 	register char *cp;
1414615Sbill 	int relsz, codesz;
1415615Sbill 
1416615Sbill 	relsz = reloc.size;
1417615Sbill 	relp = (struct relocation_info *)malloc(relsz);
1418615Sbill 	codesz = text.size;
1419615Sbill 	codep = (char *)malloc(codesz);
1420615Sbill 	if (relp == 0 || codep == 0)
1421615Sbill 		error(1, "out of memory (load2td)");
1422615Sbill 	mget((char *)relp, relsz, &reloc);
1423615Sbill 	rpend = &relp[relsz / sizeof (struct relocation_info)];
1424615Sbill 	mget(codep, codesz, &text);
1425615Sbill 	for (rp = relp; rp < rpend; rp++) {
1426615Sbill 		cp = codep + rp->r_address;
1427650Sbill 		/*
1428650Sbill 		 * Pick up previous value at location to be relocated.
1429650Sbill 		 */
1430615Sbill 		switch (rp->r_length) {
1431615Sbill 
1432615Sbill 		case 0:		/* byte */
1433615Sbill 			tw = *cp;
1434615Sbill 			break;
1435615Sbill 
1436615Sbill 		case 1:		/* word */
143729845Ssam 			tw = getw(cp);
1438615Sbill 			break;
1439615Sbill 
1440615Sbill 		case 2:		/* long */
144129845Ssam 			tw = getl(cp);
1442615Sbill 			break;
1443615Sbill 
1444615Sbill 		default:
1445615Sbill 			error(1, "load2td botch: bad length");
1446615Sbill 		}
1447650Sbill 		/*
1448650Sbill 		 * If relative to an external which is defined,
1449650Sbill 		 * resolve to a simpler kind of reference in the
1450650Sbill 		 * result file.  If the external is undefined, just
1451650Sbill 		 * convert the symbol number to the number of the
1452650Sbill 		 * symbol in the result file and leave it undefined.
1453650Sbill 		 */
1454615Sbill 		if (rp->r_extern) {
1455650Sbill 			/*
1456650Sbill 			 * Search the hash table which maps local
1457650Sbill 			 * symbol numbers to symbol tables entries
1458650Sbill 			 * in the new a.out file.
1459650Sbill 			 */
1460615Sbill 			lp = lochash[rp->r_symbolnum % LHSIZ];
1461615Sbill 			while (lp->l_index != rp->r_symbolnum) {
1462615Sbill 				lp = lp->l_link;
1463615Sbill 				if (lp == 0)
1464615Sbill 					error(1, "local symbol botch");
1465615Sbill 			}
1466615Sbill 			sp = lp->l_symbol;
1467615Sbill 			if (sp->n_type == N_EXT+N_UNDF)
1468615Sbill 				rp->r_symbolnum = nsym+symx(sp);
1469615Sbill 			else {
1470615Sbill 				rp->r_symbolnum = sp->n_type & N_TYPE;
1471615Sbill 				tw += sp->n_value;
1472615Sbill 				rp->r_extern = 0;
1473615Sbill 			}
1474615Sbill 		} else switch (rp->r_symbolnum & N_TYPE) {
1475650Sbill 		/*
1476650Sbill 		 * Relocation is relative to the loaded position
1477650Sbill 		 * of another segment.  Update by the change in position
1478650Sbill 		 * of that segment.
1479650Sbill 		 */
1480615Sbill 		case N_TEXT:
1481615Sbill 			tw += ctrel;
1482615Sbill 			break;
1483615Sbill 		case N_DATA:
1484615Sbill 			tw += cdrel;
1485615Sbill 			break;
1486615Sbill 		case N_BSS:
1487615Sbill 			tw += cbrel;
1488615Sbill 			break;
1489615Sbill 		case N_ABS:
1490615Sbill 			break;
1491615Sbill 		default:
1492615Sbill 			error(1, "relocation format botch (symbol type))");
1493615Sbill 		}
1494650Sbill 		/*
1495650Sbill 		 * Relocation is pc relative, so decrease the relocation
1496650Sbill 		 * by the amount the current segment is displaced.
1497650Sbill 		 * (E.g if we are a relative reference to a text location
1498650Sbill 		 * from data space, we added the increase in the text address
1499650Sbill 		 * above, and subtract the increase in our (data) address
1500650Sbill 		 * here, leaving the net change the relative change in the
1501650Sbill 		 * positioning of our text and data segments.)
1502650Sbill 		 */
1503615Sbill 		if (rp->r_pcrel)
1504615Sbill 			tw -= creloc;
1505650Sbill 		/*
1506650Sbill 		 * Put the value back in the segment,
1507650Sbill 		 * while checking for overflow.
1508650Sbill 		 */
1509615Sbill 		switch (rp->r_length) {
1510615Sbill 
1511615Sbill 		case 0:		/* byte */
1512615Sbill 			if (tw < -128 || tw > 127)
1513615Sbill 				error(0, "byte displacement overflow");
1514615Sbill 			*cp = tw;
1515615Sbill 			break;
1516615Sbill 		case 1:		/* word */
1517615Sbill 			if (tw < -32768 || tw > 32767)
1518615Sbill 				error(0, "word displacement overflow");
151929845Ssam 			putw(cp, tw);
1520615Sbill 			break;
1521615Sbill 		case 2:		/* long */
152229845Ssam 			putl(cp, tw);
1523615Sbill 			break;
1524615Sbill 		}
1525650Sbill 		/*
1526650Sbill 		 * If we are saving relocation information,
1527650Sbill 		 * we must convert the address in the segment from
1528650Sbill 		 * the old .o file into an address in the segment in
1529650Sbill 		 * the new a.out, by adding the position of our
1530650Sbill 		 * segment in the new larger segment.
1531650Sbill 		 */
1532615Sbill 		if (rflag)
1533650Sbill 			rp->r_address += position;
1534615Sbill 	}
1535615Sbill 	bwrite(codep, codesz, b1);
1536615Sbill 	if (rflag)
1537615Sbill 		bwrite(relp, relsz, b2);
153825483Slepreau 	free((char *)relp);
153925483Slepreau 	free(codep);
1540615Sbill }
1541615Sbill 
1542615Sbill finishout()
1543615Sbill {
1544615Sbill 	register int i;
154540407Smckusick 	char *newname;
1546615Sbill 	int nsymt;
1547615Sbill 
1548615Sbill 	if (sflag==0) {
1549615Sbill 		nsymt = symx(nextsym);
1550615Sbill 		for (i = 0; i < nsymt; i++)
1551615Sbill 			symwrite(xsym(i), sout);
1552615Sbill 		bwrite(&offset, sizeof offset, sout);
1553615Sbill 	}
1554615Sbill 	if (!ofilfnd) {
155540407Smckusick 		newname = (char *)genbuildname("a.out");
155640407Smckusick 		unlink(newname);
155740407Smckusick 		if (link(defaultname, newname) < 0)
1558898Sbill 			error(1, "cannot move l.out to a.out");
155940407Smckusick 		ofilename = newname;
1560615Sbill 	}
1561615Sbill 	delarg = errlev;
1562615Sbill 	delexit();
1563615Sbill }
1564615Sbill 
1565615Sbill mkfsym(s)
1566615Sbill char *s;
1567615Sbill {
1568615Sbill 
1569615Sbill 	if (sflag || xflag)
1570615Sbill 		return;
1571615Sbill 	cursym.n_un.n_name = s;
157230836Sbostic 	cursym.n_type = N_EXT | N_FN;
1573615Sbill 	cursym.n_value = torigin;
1574615Sbill 	symwrite(&cursym, sout);
1575615Sbill }
1576615Sbill 
1577615Sbill getarhdr()
1578615Sbill {
1579615Sbill 	register char *cp;
1580615Sbill 
1581615Sbill 	mget((char *)&archdr, sizeof archdr, &text);
1582615Sbill 	for (cp=archdr.ar_name; cp<&archdr.ar_name[sizeof(archdr.ar_name)];)
1583615Sbill 		if (*cp++ == ' ') {
1584615Sbill 			cp[-1] = 0;
1585615Sbill 			return;
1586615Sbill 		}
1587615Sbill }
1588615Sbill 
1589615Sbill mget(loc, n, sp)
1590615Sbill register STREAM *sp;
1591615Sbill register char *loc;
1592615Sbill {
1593615Sbill 	register char *p;
1594615Sbill 	register int take;
1595615Sbill 
1596615Sbill top:
1597615Sbill 	if (n == 0)
1598615Sbill 		return;
1599615Sbill 	if (sp->size && sp->nibuf) {
1600615Sbill 		p = sp->ptr;
1601615Sbill 		take = sp->size;
1602615Sbill 		if (take > sp->nibuf)
1603615Sbill 			take = sp->nibuf;
1604615Sbill 		if (take > n)
1605615Sbill 			take = n;
1606615Sbill 		n -= take;
1607615Sbill 		sp->size -= take;
1608615Sbill 		sp->nibuf -= take;
1609615Sbill 		sp->pos += take;
1610615Sbill 		do
1611615Sbill 			*loc++ = *p++;
1612615Sbill 		while (--take > 0);
1613615Sbill 		sp->ptr = p;
1614615Sbill 		goto top;
1615615Sbill 	}
161616068Sralph 	if (n > p_blksize) {
161716068Sralph 		take = n - n % p_blksize;
161816068Sralph 		lseek(infil, (sp->bno+1)<<p_blkshift, 0);
1619615Sbill 		if (take > sp->size || read(infil, loc, take) != take)
1620615Sbill 			error(1, "premature EOF");
1621615Sbill 		loc += take;
1622615Sbill 		n -= take;
1623615Sbill 		sp->size -= take;
1624615Sbill 		sp->pos += take;
162516068Sralph 		dseek(sp, (sp->bno+1+(take>>p_blkshift))<<p_blkshift, -1);
1626615Sbill 		goto top;
1627615Sbill 	}
1628615Sbill 	*loc++ = get(sp);
1629615Sbill 	--n;
1630615Sbill 	goto top;
1631615Sbill }
1632615Sbill 
1633615Sbill symwrite(sp, bp)
1634615Sbill 	struct nlist *sp;
1635615Sbill 	struct biobuf *bp;
1636615Sbill {
1637615Sbill 	register int len;
1638615Sbill 	register char *str;
1639615Sbill 
1640615Sbill 	str = sp->n_un.n_name;
1641615Sbill 	if (str) {
1642615Sbill 		sp->n_un.n_strx = offset;
1643615Sbill 		len = strlen(str) + 1;
1644615Sbill 		bwrite(str, len, strout);
1645615Sbill 		offset += len;
1646615Sbill 	}
1647615Sbill 	bwrite(sp, sizeof (*sp), bp);
1648615Sbill 	sp->n_un.n_name = str;
1649615Sbill }
1650615Sbill 
1651615Sbill dseek(sp, loc, s)
1652615Sbill register STREAM *sp;
1653615Sbill long loc, s;
1654615Sbill {
1655615Sbill 	register PAGE *p;
1656615Sbill 	register b, o;
1657615Sbill 	int n;
1658615Sbill 
165916068Sralph 	b = loc>>p_blkshift;
166016068Sralph 	o = loc&p_blkmask;
1661615Sbill 	if (o&01)
1662615Sbill 		error(1, "loader error; odd offset");
1663615Sbill 	--sp->pno->nuser;
1664615Sbill 	if ((p = &page[0])->bno!=b && (p = &page[1])->bno!=b)
1665615Sbill 		if (p->nuser==0 || (p = &page[0])->nuser==0) {
1666615Sbill 			if (page[0].nuser==0 && page[1].nuser==0)
1667615Sbill 				if (page[0].bno < page[1].bno)
1668615Sbill 					p = &page[0];
1669615Sbill 			p->bno = b;
167016068Sralph 			lseek(infil, loc & ~(long)p_blkmask, 0);
167116068Sralph 			if ((n = read(infil, p->buff, p_blksize)) < 0)
1672615Sbill 				n = 0;
1673615Sbill 			p->nibuf = n;
167416068Sralph 		} else
167516068Sralph 			error(1, "botch: no pages");
1676615Sbill 	++p->nuser;
1677615Sbill 	sp->bno = b;
1678615Sbill 	sp->pno = p;
1679615Sbill 	if (s != -1) {sp->size = s; sp->pos = 0;}
1680615Sbill 	sp->ptr = (char *)(p->buff + o);
1681615Sbill 	if ((sp->nibuf = p->nibuf-o) <= 0)
1682615Sbill 		sp->size = 0;
1683615Sbill }
1684615Sbill 
1685615Sbill char
1686615Sbill get(asp)
1687615Sbill STREAM *asp;
1688615Sbill {
1689615Sbill 	register STREAM *sp;
1690615Sbill 
1691615Sbill 	sp = asp;
1692615Sbill 	if ((sp->nibuf -= sizeof(char)) < 0) {
169316068Sralph 		dseek(sp, ((long)(sp->bno+1)<<p_blkshift), (long)-1);
1694615Sbill 		sp->nibuf -= sizeof(char);
1695615Sbill 	}
1696615Sbill 	if ((sp->size -= sizeof(char)) <= 0) {
1697615Sbill 		if (sp->size < 0)
1698615Sbill 			error(1, "premature EOF");
1699615Sbill 		++fpage.nuser;
1700615Sbill 		--sp->pno->nuser;
1701615Sbill 		sp->pno = (PAGE *) &fpage;
1702615Sbill 	}
1703615Sbill 	sp->pos += sizeof(char);
1704615Sbill 	return(*sp->ptr++);
1705615Sbill }
1706615Sbill 
1707615Sbill getfile(acp)
1708615Sbill char *acp;
1709615Sbill {
1710615Sbill 	register int c;
1711615Sbill 	char arcmag[SARMAG+1];
1712615Sbill 	struct stat stb;
1713615Sbill 
1714615Sbill 	archdr.ar_name[0] = '\0';
171517133Ssam 	filname = acp;
171617133Ssam 	if (filname[0] == '-' && filname[1] == 'l')
171717133Ssam 		infil = libopen(filname + 2, O_RDONLY);
171817133Ssam 	else
171940407Smckusick 		infil = open((char *)genbuildname(filname), O_RDONLY);
172017133Ssam 	if (infil < 0)
1721615Sbill 		error(1, "cannot open");
172216068Sralph 	fstat(infil, &stb);
1723615Sbill 	page[0].bno = page[1].bno = -1;
1724615Sbill 	page[0].nuser = page[1].nuser = 0;
172516068Sralph 	c = stb.st_blksize;
172616068Sralph 	if (c == 0 || (c & (c - 1)) != 0) {
172716068Sralph 		/* use default size if not a power of two */
172816068Sralph 		c = BLKSIZE;
172916068Sralph 	}
173016068Sralph 	if (p_blksize != c) {
173116068Sralph 		p_blksize = c;
173216068Sralph 		p_blkmask = c - 1;
173316068Sralph 		for (p_blkshift = 0; c > 1 ; p_blkshift++)
173416068Sralph 			c >>= 1;
173516068Sralph 		if (page[0].buff != NULL)
173616068Sralph 			free(page[0].buff);
173716068Sralph 		page[0].buff = (char *)malloc(p_blksize);
173816068Sralph 		if (page[0].buff == NULL)
173916068Sralph 			error(1, "ran out of memory (getfile)");
174016068Sralph 		if (page[1].buff != NULL)
174116068Sralph 			free(page[1].buff);
174216068Sralph 		page[1].buff = (char *)malloc(p_blksize);
174316068Sralph 		if (page[1].buff == NULL)
174416068Sralph 			error(1, "ran out of memory (getfile)");
174516068Sralph 	}
1746615Sbill 	text.pno = reloc.pno = (PAGE *) &fpage;
1747615Sbill 	fpage.nuser = 2;
1748615Sbill 	dseek(&text, 0L, SARMAG);
1749615Sbill 	if (text.size <= 0)
1750615Sbill 		error(1, "premature EOF");
1751615Sbill 	mget((char *)arcmag, SARMAG, &text);
1752615Sbill 	arcmag[SARMAG] = 0;
1753615Sbill 	if (strcmp(arcmag, ARMAG))
1754615Sbill 		return (0);
1755615Sbill 	dseek(&text, SARMAG, sizeof archdr);
175617133Ssam 	if (text.size <= 0)
1757615Sbill 		return (1);
1758615Sbill 	getarhdr();
175930828Sbostic 	if (strncmp(archdr.ar_name, RANLIBMAG, sizeof(archdr.ar_name)) != 0)
1760615Sbill 		return (1);
1761615Sbill 	return (stb.st_mtime > atol(archdr.ar_date) ? 3 : 2);
1762615Sbill }
1763615Sbill 
176417133Ssam /*
176517133Ssam  * Search for a library with given name
176617133Ssam  * using the directory search array.
176717133Ssam  */
176817133Ssam libopen(name, oflags)
176917133Ssam 	char *name;
177017133Ssam 	int oflags;
177117133Ssam {
177217133Ssam 	register char *p, *cp;
177317133Ssam 	register int i;
177417133Ssam 	static char buf[MAXPATHLEN+1];
177517133Ssam 	int fd = -1;
177617133Ssam 
177717133Ssam 	if (*name == '\0')			/* backwards compat */
177817133Ssam 		name = "a";
177917133Ssam 	for (i = 0; i < ndir && fd == -1; i++) {
178017133Ssam 		p = buf;
178117133Ssam 		for (cp = dirs[i]; *cp; *p++ = *cp++)
178217133Ssam 			;
178317133Ssam 		*p++ = '/';
178417133Ssam 		for (cp = "lib"; *cp; *p++ = *cp++)
178517133Ssam 			;
178617133Ssam 		for (cp = name; *cp; *p++ = *cp++)
178717133Ssam 			;
178817133Ssam 		cp = ".a";
178917133Ssam 		while (*p++ = *cp++)
179017133Ssam 			;
179117133Ssam 		fd = open(buf, oflags);
179217133Ssam 	}
179317133Ssam 	if (fd != -1)
179417133Ssam 		filname = buf;
179517133Ssam 	return (fd);
179617133Ssam }
179717133Ssam 
1798615Sbill struct nlist **
1799615Sbill lookup()
1800615Sbill {
1801615Sbill 	register int sh;
1802615Sbill 	register struct nlist **hp;
1803615Sbill 	register char *cp, *cp1;
1804615Sbill 	register struct symseg *gp;
1805615Sbill 	register int i;
1806615Sbill 
1807615Sbill 	sh = 0;
1808615Sbill 	for (cp = cursym.n_un.n_name; *cp;)
1809615Sbill 		sh = (sh<<1) + *cp++;
1810615Sbill 	sh = (sh & 0x7fffffff) % HSIZE;
1811615Sbill 	for (gp = symseg; gp < &symseg[NSEG]; gp++) {
1812615Sbill 		if (gp->sy_first == 0) {
1813615Sbill 			gp->sy_first = (struct nlist *)
1814615Sbill 			    calloc(NSYM, sizeof (struct nlist));
1815615Sbill 			gp->sy_hfirst = (struct nlist **)
1816615Sbill 			    calloc(HSIZE, sizeof (struct nlist *));
1817615Sbill 			if (gp->sy_first == 0 || gp->sy_hfirst == 0)
1818615Sbill 				error(1, "ran out of space for symbol table");
1819615Sbill 			gp->sy_last = gp->sy_first + NSYM;
1820615Sbill 			gp->sy_hlast = gp->sy_hfirst + HSIZE;
1821615Sbill 		}
1822615Sbill 		if (gp > csymseg)
1823615Sbill 			csymseg = gp;
1824615Sbill 		hp = gp->sy_hfirst + sh;
1825615Sbill 		i = 1;
1826615Sbill 		do {
1827615Sbill 			if (*hp == 0) {
1828615Sbill 				if (gp->sy_used == NSYM)
1829615Sbill 					break;
1830615Sbill 				return (hp);
1831615Sbill 			}
1832615Sbill 			cp1 = (*hp)->n_un.n_name;
1833615Sbill 			for (cp = cursym.n_un.n_name; *cp == *cp1++;)
1834615Sbill 				if (*cp++ == 0)
1835615Sbill 					return (hp);
1836615Sbill 			hp += i;
1837615Sbill 			i += 2;
1838615Sbill 			if (hp >= gp->sy_hlast)
1839615Sbill 				hp -= HSIZE;
1840615Sbill 		} while (i < HSIZE);
1841615Sbill 		if (i > HSIZE)
1842615Sbill 			error(1, "hash table botch");
1843615Sbill 	}
1844615Sbill 	error(1, "symbol table overflow");
1845615Sbill 	/*NOTREACHED*/
1846615Sbill }
1847615Sbill 
1848615Sbill symfree(saved)
1849615Sbill 	struct nlist *saved;
1850615Sbill {
1851615Sbill 	register struct symseg *gp;
1852615Sbill 	register struct nlist *sp;
1853615Sbill 
1854615Sbill 	for (gp = csymseg; gp >= symseg; gp--, csymseg--) {
1855615Sbill 		sp = gp->sy_first + gp->sy_used;
1856615Sbill 		if (sp == saved) {
1857615Sbill 			nextsym = sp;
1858615Sbill 			return;
1859615Sbill 		}
1860615Sbill 		for (sp--; sp >= gp->sy_first; sp--) {
1861615Sbill 			gp->sy_hfirst[sp->n_hash] = 0;
1862615Sbill 			gp->sy_used--;
1863615Sbill 			if (sp == saved) {
1864615Sbill 				nextsym = sp;
1865615Sbill 				return;
1866615Sbill 			}
1867615Sbill 		}
1868615Sbill 	}
1869615Sbill 	if (saved == 0)
1870615Sbill 		return;
1871615Sbill 	error(1, "symfree botch");
1872615Sbill }
1873615Sbill 
1874615Sbill struct nlist **
1875615Sbill slookup(s)
1876615Sbill 	char *s;
1877615Sbill {
1878615Sbill 
1879615Sbill 	cursym.n_un.n_name = s;
1880615Sbill 	cursym.n_type = N_EXT+N_UNDF;
1881615Sbill 	cursym.n_value = 0;
1882615Sbill 	return (lookup());
1883615Sbill }
1884615Sbill 
1885615Sbill enter(hp)
1886615Sbill register struct nlist **hp;
1887615Sbill {
1888615Sbill 	register struct nlist *sp;
1889615Sbill 
1890615Sbill 	if (*hp==0) {
1891615Sbill 		if (hp < csymseg->sy_hfirst || hp >= csymseg->sy_hlast)
1892615Sbill 			error(1, "enter botch");
1893615Sbill 		*hp = lastsym = sp = csymseg->sy_first + csymseg->sy_used;
1894615Sbill 		csymseg->sy_used++;
1895615Sbill 		sp->n_un.n_name = cursym.n_un.n_name;
1896615Sbill 		sp->n_type = cursym.n_type;
1897615Sbill 		sp->n_hash = hp - csymseg->sy_hfirst;
1898615Sbill 		sp->n_value = cursym.n_value;
1899615Sbill 		nextsym = lastsym + 1;
1900615Sbill 		return(1);
1901615Sbill 	} else {
1902615Sbill 		lastsym = *hp;
1903615Sbill 		return(0);
1904615Sbill 	}
1905615Sbill }
1906615Sbill 
1907615Sbill symx(sp)
1908615Sbill 	struct nlist *sp;
1909615Sbill {
1910615Sbill 	register struct symseg *gp;
1911615Sbill 
1912615Sbill 	if (sp == 0)
1913615Sbill 		return (0);
1914615Sbill 	for (gp = csymseg; gp >= symseg; gp--)
1915615Sbill 		/* <= is sloppy so nextsym will always work */
1916615Sbill 		if (sp >= gp->sy_first && sp <= gp->sy_last)
1917615Sbill 			return ((gp - symseg) * NSYM + sp - gp->sy_first);
1918615Sbill 	error(1, "symx botch");
1919615Sbill 	/*NOTREACHED*/
1920615Sbill }
1921615Sbill 
1922615Sbill symreloc()
1923615Sbill {
1924615Sbill 	if(funding) return;
1925615Sbill 	switch (cursym.n_type & 017) {
1926615Sbill 
1927615Sbill 	case N_TEXT:
1928615Sbill 	case N_EXT+N_TEXT:
1929615Sbill 		cursym.n_value += ctrel;
1930615Sbill 		return;
1931615Sbill 
1932615Sbill 	case N_DATA:
1933615Sbill 	case N_EXT+N_DATA:
1934615Sbill 		cursym.n_value += cdrel;
1935615Sbill 		return;
1936615Sbill 
1937615Sbill 	case N_BSS:
1938615Sbill 	case N_EXT+N_BSS:
1939615Sbill 		cursym.n_value += cbrel;
1940615Sbill 		return;
1941615Sbill 
1942615Sbill 	case N_EXT+N_UNDF:
1943615Sbill 		return;
1944615Sbill 
1945615Sbill 	default:
1946615Sbill 		if (cursym.n_type&N_EXT)
1947615Sbill 			cursym.n_type = N_EXT+N_ABS;
1948615Sbill 		return;
1949615Sbill 	}
1950615Sbill }
1951615Sbill 
1952615Sbill error(n, s)
1953615Sbill char *s;
1954615Sbill {
1955898Sbill 
1956615Sbill 	if (errlev==0)
1957615Sbill 		printf("ld:");
1958615Sbill 	if (filname) {
1959615Sbill 		printf("%s", filname);
1960615Sbill 		if (n != -1 && archdr.ar_name[0])
1961615Sbill 			printf("(%s)", archdr.ar_name);
1962615Sbill 		printf(": ");
1963615Sbill 	}
1964615Sbill 	printf("%s\n", s);
1965615Sbill 	if (n == -1)
1966615Sbill 		return;
1967615Sbill 	if (n)
1968615Sbill 		delexit();
1969615Sbill 	errlev = 2;
1970615Sbill }
1971615Sbill 
1972615Sbill readhdr(loc)
1973615Sbill off_t loc;
1974615Sbill {
1975615Sbill 
1976615Sbill 	dseek(&text, loc, (long)sizeof(filhdr));
1977615Sbill 	mget((short *)&filhdr, sizeof(filhdr), &text);
1978615Sbill 	if (N_BADMAG(filhdr)) {
1979615Sbill 		if (filhdr.a_magic == OARMAG)
1980615Sbill 			error(1, "old archive");
1981615Sbill 		error(1, "bad magic number");
1982615Sbill 	}
1983615Sbill 	if (filhdr.a_text&01 || filhdr.a_data&01)
1984615Sbill 		error(1, "text/data size odd");
1985615Sbill 	if (filhdr.a_magic == NMAGIC || filhdr.a_magic == ZMAGIC) {
198612671Ssam 		cdrel = -round(filhdr.a_text, pagesize);
1987615Sbill 		cbrel = cdrel - filhdr.a_data;
1988615Sbill 	} else if (filhdr.a_magic == OMAGIC) {
1989615Sbill 		cdrel = -filhdr.a_text;
1990615Sbill 		cbrel = cdrel - filhdr.a_data;
1991615Sbill 	} else
1992615Sbill 		error(1, "bad format");
1993615Sbill }
1994615Sbill 
1995615Sbill round(v, r)
1996615Sbill 	int v;
1997615Sbill 	u_long r;
1998615Sbill {
1999615Sbill 
2000615Sbill 	r--;
2001615Sbill 	v += r;
2002615Sbill 	v &= ~(long)r;
2003615Sbill 	return(v);
2004615Sbill }
2005615Sbill 
2006615Sbill #define	NSAVETAB	8192
2007615Sbill char	*savetab;
2008615Sbill int	saveleft;
2009615Sbill 
2010615Sbill char *
2011615Sbill savestr(cp)
2012615Sbill 	register char *cp;
2013615Sbill {
2014615Sbill 	register int len;
2015615Sbill 
2016615Sbill 	len = strlen(cp) + 1;
2017615Sbill 	if (len > saveleft) {
2018615Sbill 		saveleft = NSAVETAB;
2019615Sbill 		if (len > saveleft)
2020615Sbill 			saveleft = len;
202117133Ssam 		savetab = malloc(saveleft);
2022615Sbill 		if (savetab == 0)
2023615Sbill 			error(1, "ran out of memory (savestr)");
2024615Sbill 	}
2025615Sbill 	strncpy(savetab, cp, len);
2026615Sbill 	cp = savetab;
2027615Sbill 	savetab += len;
2028615Sbill 	saveleft -= len;
2029615Sbill 	return (cp);
2030615Sbill }
2031615Sbill 
203216068Sralph bopen(bp, off, bufsize)
203316068Sralph 	register struct biobuf *bp;
2034615Sbill {
2035615Sbill 
203617133Ssam 	bp->b_ptr = bp->b_buf = malloc(bufsize);
203716068Sralph 	if (bp->b_ptr == (char *)0)
203816068Sralph 		error(1, "ran out of memory (bopen)");
203916068Sralph 	bp->b_bufsize = bufsize;
204016068Sralph 	bp->b_nleft = bufsize - (off % bufsize);
2041615Sbill 	bp->b_off = off;
2042615Sbill 	bp->b_link = biobufs;
2043615Sbill 	biobufs = bp;
2044615Sbill }
2045615Sbill 
2046615Sbill int	bwrerror;
2047615Sbill 
2048615Sbill bwrite(p, cnt, bp)
2049615Sbill 	register char *p;
2050615Sbill 	register int cnt;
2051615Sbill 	register struct biobuf *bp;
2052615Sbill {
2053615Sbill 	register int put;
2054615Sbill 	register char *to;
2055615Sbill 
2056615Sbill top:
2057615Sbill 	if (cnt == 0)
2058615Sbill 		return;
2059615Sbill 	if (bp->b_nleft) {
2060615Sbill 		put = bp->b_nleft;
2061615Sbill 		if (put > cnt)
2062615Sbill 			put = cnt;
2063615Sbill 		bp->b_nleft -= put;
2064615Sbill 		to = bp->b_ptr;
206525419Sbloom 		bcopy(p, to, put);
2066615Sbill 		bp->b_ptr += put;
2067615Sbill 		p += put;
2068615Sbill 		cnt -= put;
2069615Sbill 		goto top;
2070615Sbill 	}
207116068Sralph 	if (cnt >= bp->b_bufsize) {
2072615Sbill 		if (bp->b_ptr != bp->b_buf)
2073615Sbill 			bflush1(bp);
207416068Sralph 		put = cnt - cnt % bp->b_bufsize;
2075615Sbill 		if (boffset != bp->b_off)
2076615Sbill 			lseek(biofd, bp->b_off, 0);
2077615Sbill 		if (write(biofd, p, put) != put) {
2078615Sbill 			bwrerror = 1;
2079615Sbill 			error(1, "output write error");
2080615Sbill 		}
2081615Sbill 		bp->b_off += put;
2082615Sbill 		boffset = bp->b_off;
2083615Sbill 		p += put;
2084615Sbill 		cnt -= put;
2085615Sbill 		goto top;
2086615Sbill 	}
2087615Sbill 	bflush1(bp);
2088615Sbill 	goto top;
2089615Sbill }
2090615Sbill 
2091615Sbill bflush()
2092615Sbill {
2093615Sbill 	register struct biobuf *bp;
2094615Sbill 
2095615Sbill 	if (bwrerror)
2096615Sbill 		return;
2097615Sbill 	for (bp = biobufs; bp; bp = bp->b_link)
2098615Sbill 		bflush1(bp);
2099615Sbill }
2100615Sbill 
2101615Sbill bflush1(bp)
2102615Sbill 	register struct biobuf *bp;
2103615Sbill {
2104615Sbill 	register int cnt = bp->b_ptr - bp->b_buf;
2105615Sbill 
2106615Sbill 	if (cnt == 0)
2107615Sbill 		return;
2108615Sbill 	if (boffset != bp->b_off)
2109615Sbill 		lseek(biofd, bp->b_off, 0);
2110615Sbill 	if (write(biofd, bp->b_buf, cnt) != cnt) {
2111615Sbill 		bwrerror = 1;
2112615Sbill 		error(1, "output write error");
2113615Sbill 	}
2114615Sbill 	bp->b_off += cnt;
2115615Sbill 	boffset = bp->b_off;
2116615Sbill 	bp->b_ptr = bp->b_buf;
211716068Sralph 	bp->b_nleft = bp->b_bufsize;
2118615Sbill }
2119615Sbill 
2120615Sbill bflushc(bp, c)
2121615Sbill 	register struct biobuf *bp;
2122615Sbill {
2123615Sbill 
2124615Sbill 	bflush1(bp);
2125615Sbill 	bputc(c, bp);
2126615Sbill }
212716068Sralph 
212816068Sralph bseek(bp, off)
212916068Sralph 	register struct biobuf *bp;
213016068Sralph 	register off_t off;
213116068Sralph {
213216068Sralph 	bflush1(bp);
213316068Sralph 
213416068Sralph 	bp->b_nleft = bp->b_bufsize - (off % bp->b_bufsize);
213516068Sralph 	bp->b_off = off;
213616068Sralph }
2137