UNIX PC COFF Disassembler (Part 1 of 2)

Lenny Tropiano lenny at icus.islp.ny.us
Tue Sep 27 10:34:21 AEST 1988


Here's an UNIX PC disassembler (68000 assembly) that understands COFF
(Common Object File Format).  It creates output that has the appropriate
symbols in commands like ("jsr printf") and understands the symbols
in the string table and creates .asciz "<string>" (directives).

I didn't write this, but I thought it would be useful to the entire
UNIX PC community.  Especially for reverse engineering UNIX :-)

Enjoy,
Lenny

-- cut here -- -- cut here -- -- cut here -- -- cut here -- -- cut here --
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 1 (of 2)."
# Contents:  Makefile README README.coff alloc.c conv.c doc.nr doc.out
#   file.c iset.c
# Wrapped by lenny at icus on Mon Sep 26 20:30:04 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(214 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
XCFLAGS=-O
XLDFLAGS=-s
XOBJS=alloc.o file.o libmtch.o robj.o iset.o prin.o heur.o main.o conv.o
XSHAREDLIB=/lib/shlib.ifile /lib/crt0s.o
X
Xunc:	$(OBJS)
X	$(LD) $(LDFLAGS) -o unc $(OBJS) $(SHAREDLIB) -lld
X
X$(OBJS):	unc.h
END_OF_FILE
if test 214 -ne `wc -c <'Makefile'`; then
    echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'README' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'README'\"
else
echo shar: Extracting \"'README'\" \(806 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
XI have gotten many many requests to email this or post it, because of
Xits size email'ing it screws up most mailers, so i am submitting it to
Xmod.sources to be posted.  Please note a major cavaet with this, it was
Xwritten under Unisoft's port of Unix so the a.out file that it uses
Xmore closely resembles the b.out.h file that most cross assemblers
X(e.g.  greenhills) use. For the obvious reasons i have not included that
Xfile with the posting. I did not write this nor do i make any claim to
Xthat effect.
X
X    turner at imagen.UUCP <talcott!topaz!Shasta!imagen!Jim.Turner>
X
X----------------------------
XThis is the 68000 disassembler mentioned on the net.
XIt is not my final version by any means, but I have found it extremely
Xuseful and it represents several weeks' work.
X
X    John Collins. <jmc at inset.UUCP>
END_OF_FILE
if test 806 -ne `wc -c <'README'`; then
    echo shar: \"'README'\" unpacked with wrong size!
fi
# end of 'README'
fi
if test -f 'README.coff' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'README.coff'\"
else
echo shar: Extracting \"'README.coff'\" \(2737 characters\)
sed "s/^X//" >'README.coff' <<'END_OF_FILE'
X--------------------------------------
XCOFF DISASSEMBLER FOR THE AT&T UNIX pc
X--------------------------------------
XThis file by Lenny Tropiano, ICUS Software Systems -- September 26, 1988
X
XA while back I asked the "net" if anyone had a 68000 disassembler that
Xunderstood the COFF format for executable images.   A few people had replied, 
Xand I did receive a few good leads and two programs.  Most of the people 
Xsaid to get the "unc" disassembler that was posted to comp.sources.unix and
Xtry to work from there.  The unc diassembler worked for 68000 cpu's but
Xdidn't know anything about COFF format.  Some people said to work with the
XSUN patches and then teach it COFF.  This seemed like a big task for someone
Xwho didn't have much time on his hands.  
X
XWell then Janet Walz <walz at mimsy.umd.edu>, replied.  She said she taught
Xthe unc disassembler about COFF format, and it worked fine on the AT&T 
Xmachines.  I then asked if she would send me the patches.   After patiently
Xwaiting she kindly supplied a sharkit that had the new sources (she claimed
Xthat the patches were as *big* as the sources [which it looked like it was]).
X
XI'm not AT&T 68000 assembler expert, but I think the format that the 
X"unc" disassembler prints is sun format, which is fine if you are just
Xtrying to figure out what something is doing.   Tagged along with 
XMichael "Ford" Ditto's "unstrip" program that was posted to unix-pc.sources
Xa while back, one can put the shared library symbols back into source and
Xthen disassemble it.  This program puts generated-labels where appropriate
Xto show looping structures.  It does show .ascii and .asciz string blocks.
XIt has some very nice features, and it much better than the other solution
XI got that was a UNIX PC implementation of the "dis(1)" command that is
Xfound on the 3B2's System V release.
X
XWarning the program is awefully slow, and it does create large output files. 
XHere are some statistics running on my UNIX PC with 3MB of memory (and 
Xfairly loaded) for the follow disassembly of:
X
X$ cat > simple.c
Xmain()
X{
X	printf("hello, world\n");
X}
X
X$ wc simple.c
X      4      5     38 simple.c
X$ make simple
X	cc -O -s simple.c -o simple
X$ time unc -v simple -lc > simple.dis
XScanning text
XScanning data
XScanning symbols
XScanning for relocation
XEnd of input
XText anal 1
XData anal 1
XData anal 2
X
Xreal	4m4.43s
Xuser	0m15.36s
Xsys	2m15.83s
X$ wc simple.dis
X   4787   8428  58070 simple.dis
X
XHmmm, 38 bytes to 58070.... now if I disassembler UNIX I should go from
X168915 bytes to ~6800000 bytes... And probably wait about 5 hours or so.
XIf anyone trys it let me know :-)
X
XAny enhancements (showing code in AT&T m68k assembly, so you can feed
Xback to "as") or anything, I would be interested in seeing it!
X
X-Lenny
X
END_OF_FILE
if test 2737 -ne `wc -c <'README.coff'`; then
    echo shar: \"'README.coff'\" unpacked with wrong size!
fi
# end of 'README.coff'
fi
if test -f 'alloc.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'alloc.c'\"
else
echo shar: Extracting \"'alloc.c'\" \(6403 characters\)
sed "s/^X//" >'alloc.c' <<'END_OF_FILE'
X/*
X *	SCCS:	@(#)alloc.c	1.2	11/2/84	14:17:20
X *	Allocate space etc.
X *
X ***********************************************************************
X *	This software is copyright of
X *
X *		John M Collins
X *		47 Cedarwood Drive
X *		St Albans
X *		Herts, AL4 0DN
X *		England			+44 727 57267
X *
X *	and is released into the public domain on the following conditions:
X *
X *		1.  No free maintenance will be guaranteed.
X *		2.  Nothing may be based on this software without
X *		    acknowledgement, including incorporation of this
X *		    notice.
X *
X *	Notwithstanding the above, the author welcomes correspondence and bug
X *	fixes.
X ***********************************************************************
X */
X
X#include <stdio.h>
X#include <a.out.h>
X#include <setjmp.h>
X#include <ldfcn.h>
X#include "unc.h"
X
X#define	STINC	10
X
Xchar	*malloc(), *realloc();
Xchar	*strncpy();
Xvoid	gette(), getde(), setde(), putte(), putde();
Xvoid	unimpl();
Xlong	gettw();
X
Xef_fids	mainfile;
X
X/*
X *	Oops! out of memory.....
X */
X
Xvoid	nomem()
X{
X	(void) fprintf(stderr, "Sorry - run out of memory\n");
X	exit(255);
X}
X
X/*
X *	Look up hash value of symbol.
X */
X
Xunsigned  shash(str)
Xregister  char	*str;
X{
X	register  unsigned  result = 0;
X	register  int	cnt = 0;
X	
X	while  (*str  &&  cnt < MAXCHARS)  {
X		result += *str++;
X		cnt++;
X	}
X	return  result % HASHMOD;
X}
X
X/*
X *	Look up hash value of symbol, possibly allocating a new symbol.
X */
X
Xsymbol	lookup(str)
Xchar	*str;
X{
X	register  symbol  res, *pp;
X	register  int	len;
X	
X	pp = &symbhash[shash(str)];
X	res = *pp;
X	while  (res != NULL)  {
X		if  (strncmp(res->s_name, str, MAXCHARS) == 0)
X			return	res;
X		pp = &res->s_next;
X		res = *pp;
X	}
X	for  (len = 0;  len < MAXCHARS;  len++)
X		if  (str[len] == '\0')
X			break;
X	len++;
X	res = (symbol) malloc(sizeof(struct symstr) + len);
X	if  (res == NULL)
X		nomem();
X	*pp = res;
X	res->s_next = NULL;
X	(void) strncpy(res->s_name, str, len);
X	res->s_name[len] = '\0';		/*  Null-terminate  */
X	res->s_newsym = 1;
X	res->s_glob = 0;
X	res->s_invent = 0;
X	res->s_link = NULL;
X	res->s_used = 0;
X	res->s_defs = 0;
X	res->s_lsymb = 0;
X	return  res;
X}
X
X/*
X *	Invent a symbol, making sure that we don't know it.
X */
X
Xsymbol	inventsymb(prefix)
Xchar	*prefix;
X{
X	static	int  nsymb = 0;
X	char	schars[10];
X	register  symbol  res;
X	
X	do	(void) sprintf(schars, "%s%d", prefix, ++nsymb);
X	while  (!(res = lookup(schars))->s_newsym);
X	
X	res->s_newsym = 0;
X	res->s_invent = 1;
X	return	res;
X}
X	 
X/*
X *	Reallocate symbol table.
X */
X
Xvoid	reallst(outf)
Xregister  ef_fid  outf;
X{
X	outf->ef_stmax += STINC;
X	if  (outf->ef_stvec == NULL)
X		outf->ef_stvec = (symbol *) malloc(outf->ef_stmax * sizeof(symbol));
X	else
X		outf->ef_stvec = (symbol *) realloc(outf->ef_stvec,
X					outf->ef_stmax * sizeof(symbol));
X	if  (outf->ef_stvec == NULL)
X		nomem();
X}
X
X/*
X *	Search through existing symbol table for symbol with given
X *	value.  Invent a new one if needed.
X */
X
Xsymbol	getnsymb(fid, seg, pos)
Xregister  ef_fid  fid;
Xunsigned  seg;
Xlong	pos;
X{
X	register  int	i;
X	register  symbol  res;
X	
X	/***********  MACHINE DEPENDENT  ******************************
X	 *	Convert relocation segment type (argument) to symbol type
X	 *	(as remembered in symbol table).  Don't ask me why they
X	 *	have to be different.....
X	 */
X	
X	/*
X	 *	See if the reference is to an external symbol.
X	 *	If so, use that.
X	 */
X	
X	for  (i = 0;  i < fid->ef_stcnt;  i++)  {
X		res = fid->ef_stvec[i];
X		if  (res->s_type == seg  &&  res->s_value == pos)
X			return	res;
X	}
X	
X	/*
X	 *	Invent a symbol and use that.
X	 */
X	
X	res = inventsymb("RS");
X	if  (fid->ef_stcnt >= fid->ef_stmax)
X		reallst(fid);
X	fid->ef_stvec[fid->ef_stcnt++] = res;
X	res->s_type = seg;
X	res->s_value = pos;
X	if  (seg == S_TEXT)  {
X		t_entry	tent;
X		gette(fid, pos, &tent);
X		tent.t_bdest = 1;
X		tent.t_lab = res;
X		putte(fid, pos, &tent);
X	}
X	else  if  (seg == S_DATA  ||  seg == S_BSS)  {
X		d_entry dent;
X		getde(fid, pos, &dent);
X		dent.d_lab = res;
X		putde(fid, pos, &dent);
X	}
X
X	return	res;
X}
X
X/*
X *	Assuming address given is in text segment, find its label, or invent
X *	one.  Also set where refered from.
X */
X
Xsymbol	textlab(loc, refpos)
Xlong	loc, refpos;
X{
X	t_entry	tent;
X
X	gette(&mainfile, loc, &tent);
X	if  (tent.t_type == T_CONT)
X		return	NULL;
X	if  (tent.t_lab == NULL)  {
X		tent.t_lab = inventsymb("TS");
X		tent.t_lab->s_type = S_TEXT;
X		tent.t_lab->s_value = loc;
X		tent.t_bdest = 1;
X		putte(&mainfile, loc, &tent);
X	}
X	else
X		tent.t_lab->s_used++;
X	if  (tent.t_refhi < refpos)  {
X		tent.t_refhi = refpos;
X		putte(&mainfile, loc, &tent);
X	}
X	if  (tent.t_reflo > refpos)  {
X		tent.t_reflo = refpos;
X		putte(&mainfile, loc, &tent);
X	}
X	return	tent.t_lab;
X}
X
X/*
X *	Note references to data.
X */
X
Xvoid	mkdref(tpos, size)
Xlong	tpos;
Xunsigned  size;
X{
X	t_entry	tent;
X	d_entry	dent;
X	register  symbol  ds;
X	int	dchng = 0;
X	int	wsize;
X	long	dpos;
X	
X	gette(&mainfile, tpos, &tent);
X	if  (tent.t_relsymb != NULL)
X		return;
X		
X	dpos = gettw(&mainfile, tpos, R_LONG);
X	if  (dpos < mainfile.ef_dbase  ||  dpos > mainfile.ef_end)
X		return;
X	
X	switch  (size)  {
X	default:
X		wsize = D_BYTE;
X		break;
X	case  2:
X		wsize = D_WORD;
X		break;
X	case  4:
X		wsize = D_LONG;
X		break;
X	}
X	
X	getde(&mainfile, dpos, &dent);
X	if  ((ds = dent.d_lab) == NULL)  {
X		if  (dpos >= mainfile.ef_bbase)  {
X			ds = inventsymb("BS");
X			ds->s_type = S_BSS;
X		}
X		else  {
X			ds = inventsymb("DS");
X			ds->s_type = S_DATA;
X		}
X		ds->s_value = dpos;
X		dent.d_lab = ds;
X		dchng++;
X	}
X	else
X		ds->s_used++;
X
X	if  (dent.d_type != D_BYTE)  {
X		if  (dent.d_type != wsize)  {
X			if  (dent.d_type == D_ADDR)  {
X				if  (wsize != D_LONG)
X					unimpl("Addr word usage");
X			}
X			else  if  (dent.d_type > wsize)  {
X				dchng++;
X				dent.d_type = wsize;
X				dent.d_lng = size;
X			}
X		}
X	}
X	else  {
X		dent.d_type = wsize;
X		dent.d_lng = size;
X		dchng++;
X	}
X	if  (dchng)  {
X		putde(&mainfile, dpos, &dent);
X		for  (dchng = 1;  dchng < size; dchng++)
X			setde(&mainfile, dpos+dchng, D_CONT, 1);
X	}
X		
X	tent.t_relsymb = ds;
X	putte(&mainfile, tpos, &tent);
X}
X
X/*
X *	Add item to common or abs list.
X */
X
X#define	COMINC	10
X
Xvoid	addit(cp, symb)
Xregister  struct  commit  *cp;
Xsymbol	symb;
X{
X	if  (cp->c_int >= cp->c_max)  {
X		cp->c_max += COMINC;
X		if  (cp->c_symb == NULL)
X			cp->c_symb = (symbol *) malloc(COMINC*sizeof(symbol));
X		else
X			cp->c_symb = (symbol *)
X					realloc(cp->c_symb,
X						cp->c_max * sizeof(symbol));
X		if  (cp->c_symb == NULL)
X			nomem();
X	}
X	cp->c_symb[cp->c_int++] = symb;
X}
END_OF_FILE
if test 6403 -ne `wc -c <'alloc.c'`; then
    echo shar: \"'alloc.c'\" unpacked with wrong size!
fi
# end of 'alloc.c'
fi
if test -f 'conv.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'conv.c'\"
else
echo shar: Extracting \"'conv.c'\" \(563 characters\)
sed "s/^X//" >'conv.c' <<'END_OF_FILE'
X#include <stdio.h>
X#include <a.out.h>
X#include <ldfcn.h>
X#include "unc.h"
X
Xunsigned convtosun(sym)
Xstruct syment *sym;
X{
X    unsigned suntype;
X
X    switch(sym->n_scnum) {
X    case N_UNDEF:
X    case N_DEBUG:	/* call things that don't map well "undefined" */
X    case N_TV:
X    case P_TV:
X	suntype = S_UNDF;
X	break;
X    case N_ABS:
X	suntype = S_ABS;
X	break;
X    case 1:
X	suntype = S_TEXT;
X	break;
X    case 2:
X	suntype = S_DATA;
X	break;
X    case 3:
X	suntype = S_BSS;
X	break;
X    }
X
X    if (sym->n_sclass == C_EXT)
X	suntype = suntype | S_EXT;
X
X    return(suntype);
X}
END_OF_FILE
if test 563 -ne `wc -c <'conv.c'`; then
    echo shar: \"'conv.c'\" unpacked with wrong size!
fi
# end of 'conv.c'
fi
if test -f 'doc.nr' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'doc.nr'\"
else
echo shar: Extracting \"'doc.nr'\" \(6445 characters\)
sed "s/^X//" >'doc.nr' <<'END_OF_FILE'
X.\"/*%	nroff -cm -rL72 %|epson|spr -f plain.a -h uncdoc -w
X.nr Hb 7
X.nr Hs 3
X.ds HF 3 3 3 3 3 3 3
X.nr Hu 5
X.nr Hc 1
X.SA 1
X.PH "''A Disassembler''"
X.PF "'Issue %I%'- Page \\\\nP -'%G%'"
X.H 1 "Introduction"
XThis document describes the first release of a disassembler for UNIX
Xexecutable files.
XThe key features are:
X.AL
X.LI
XFor object files the output can be assembled to generate the same
Xobject module, (apart from minor variations in symbol table ordering) as the
Xinput.
X.LI
XFor stripped executable files object modules and libraries may be scanned,
Xmodules in the main input identified and the appropriate names automatically
Xinserted into the output.
X.LI
XAn option is available to convert most non-global names into local symbols,
Xwhich cuts down the symbols in the generated assembler file.
X.LI
XThe disassembler copes reasonably with modules merged with the
X.B "-r"
Xoption to
X.B "ld" ,
Xgenerating a warning message as to the number of modules involved.
X.LE
X.P
XAt present this is available for certain Motorola 68000 ports of UNIX
XSystem III and System V. Dependencies on
X.AL a
X.LI
XInstruction set.
X.LI
XObject module format.
X.LI
XLibrary module format.
X.LI
XAssembler output format.
X.LE
X.P
Xare hopefully sufficiently localised to make the product useful as a
Xbasis for other disassemblers for other versions of UNIX.
X.P
XThe product is thus distributed in source form at present.
X.H 1 "Use"
XThe disassembler is run by entering:
X.DS I
Xunc mainfile lib1 lib2 ...
X.DE
X.P
XThe first named file is the file to be disassembled, which should be
Xa single file, either an object module, a (possibly stripped) executable
Xfile, or a library member. Library members are designated using a
Xparenthesis notation, thus:
X.DS I
Xunc '/lib/libc.a(printf.o)'
X.DE
X.P
XIt is usually necessary to escape the arguments in this case to prevent
Xmisinterpretation by the shell. Libraries in standard places such as
X.I "/lib"
Xand
X.I "/usr/lib"
Xmay be specified in the same way as to
X.B "ld" ,
Xthus
X.DS I
Xunc '-lc(printf.o)'
Xunc '-lcurses(wmove.o)'
X.DE
X.P
XAs an additional facility, the list of directories searched for
Xlibraries may be varied by setting the environment variable
X.B "LDPATH" ,
Xwhich is interpreted similarly to the shell
X.B "PATH"
Xvariable, and of course defaults to
X.DS I
XLDPATH=/lib:/usr/lib
X.DE
X.P
XAs a further facility, the insertion of
X.B "lib"
Xbefore and
X.B ".a"
Xafter the argument may be suppressed by using a capital
X.B "-L"
Xargument, thus to print out the assembler for
X.I "/lib/crt0.o" ,
Xthen the command
X.DS I
Xunc -Lcrt0.o
X.DE
X.P
Xshould have the desired effect.
X.P
XSecond and subsequent file arguments are only referenced for stripped
Xexecutable files, and may consist of single object files and library
Xmembers, using the same syntax as before, or whole libraries of object
Xfiles, thus:
X.DS I
Xunc strippedfile -Lcrt0.o -lcurses -ltermcap '-lm(sqrt.o)' -lc
X.DE
X.P
XIt is advisable to make some effort to put the libraries to be searched
Xin the order in which they were originally loaded. This is because the
Xsearch for each module starts where the previously matched module ended.
XHowever, no harm is done if this rule is not adhered to apart from
Xincreased execution time except in the rare cases where the disassembler
Xis confused by object modules which are very nearly similar.
X.H 1 "Additional options"
XThe following options are available to modify the behaviour of the
Xdisassembler.
X.VL 15 2
X.LI "-o file"
XCauses output to be sent to the specified file instead of the standard
Xoutput.
X.LI "-t prefix"
XCauses temporary files to be created with the given prefix. The default
Xprefix is
X.B "split" ,
Xthus causing two temporary files to be created with this prefix in the
Xcurrent directory. If it is desired, for example, to create the files as
X.B "/tmp/xx*" ,
Xthen the argument
X.B "-t /tmp/xx"
Xshould be given. Note that the temporary files may be very large as a
Xcomplete map of the text and data segments is generated.
X.LI "-a"
XSuppresses the generation of non-global absolute symbols from the
Xoutput. This saves output from C compilations without any obvious
Xproblems, but the symbols are by default included in the name of
Xproducing as nearly identical output as possible to the original source.
X.LI "-s"
XCauses an additional scan to take place where all possible labels are
Xreplaced by local symbols. The local symbols are inserted in strictly
Xascending order, starting at 1.
X.LI "-v"
XCauses a blow-by-blow account of activities to be output on the standard
Xerror.
X.LE
X.H 1 "Diagnostics etc"
XTruncated or garbled object and library files usually cause processing
Xto stop with an explanatory message.
X.P
XThe only other kinds of message are some passing warnings concerning
Xobscure constructs not handled, such as the relocation of byte fields,
Xor the relocation of overlapping fields. Occasionally a message
X.DS I
XLibrary clash: message
X.DE
X.P
Xmay appear and processing cease. This message is found where at a late
Xstage in processing libraries, the program discovers that due to the
Xextreme similarity of two or more library members, it has come to the
Xwrong conclusion about which one to use. The remedy here is to spell out
Xto the program which members to take in which order.
X.H 1 "Future development"
XIn the future it is hoped to devise ways of making the disassembler
Xindependent of all the above-mentioned version dependencies, by first
Xreading a files defining these things. This will probably be applied
Xafter the Common Object Format becomes more standard.
X.P
XIn the long term it would be desirable and useful to enhance the product
Xto produce compilable C in addition to assemblable assembler. Stages in
Xthe process are seen as follows:
X.AL
X.LI
XBetter identification of basic blocks in the code. Switch statements are
Xa major problem here, as are constant data held in the text segment.
X.LI
XMarrying of data to the corresponding text. It is in various places hard
Xto divorce static references "on the fly" (e.g. strings, and switch
Xlists in some implementations) from static at the head of a module. This
Xis part of the problem of identifying basic blocks.
X.LI
XCompilation of header files to work out structure references within the
Xtext. At this stage some interaction may be needed.
X.LE
X.P
XMeanwhile the product is one which is a useful tool to the author in its
Xpresent form. Comments and suggestions as to the most practical method
Xof improving the product in the ways suggested or in other ways would be
Xgratefully considered.
END_OF_FILE
if test 6445 -ne `wc -c <'doc.nr'`; then
    echo shar: \"'doc.nr'\" unpacked with wrong size!
fi
# end of 'doc.nr'
fi
if test -f 'doc.out' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'doc.out'\"
else
echo shar: Extracting \"'doc.out'\" \(7415 characters\)
sed "s/^X//" >'doc.out' <<'END_OF_FILE'
X
X
X
X                       A Disassembler
X
X
X
X                      1.  Introduction
X
XThis document describes the first release of a  disassembler
Xfor UNIX executable files.  The key features are:
X
X  1.  For object  files  the  output  can  be  assembled  to
X      generate  the  same  object  module, (apart from minor
X      variations in symbol table ordering) as the input.
X
X  2.  For  stripped  executable  files  object  modules  and
X      libraries  may  be  scanned, modules in the main input
X      identified and  the  appropriate  names  automatically
X      inserted into the output.
X
X  3.  An option is  available  to  convert  most  non-global
X      names  into local symbols, which cuts down the symbols
X      in the generated assembler file.
X
X  4.  The disassembler copes reasonably with modules  merged
X      with the -r option to ld, generating a warning message
X      as to the number of modules involved.
X
XAt present this is  available  for  certain  Motorola  68000
Xports of UNIX System III and System V. Dependencies on
X
X  a.  Instruction set.
X
X  b.  Object module format.
X
X  c.  Library module format.
X
X  d.  Assembler output format.
X
Xare hopefully sufficiently localised  to  make  the  product
Xuseful as a basis for other disassemblers for other versions
Xof UNIX.
X
XThe product is thus distributed in source form at present.
X
X
X                          2.  Use
X
XThe disassembler is run by entering:
X
X     unc mainfile lib1 lib2 ...
X
XThe first named file is the file to be  disassembled,  which
Xshould  be  a  single  file,  either  an  object  module,  a
X(possibly stripped) executable file, or  a  library  member.
XLibrary members are designated using a parenthesis notation,
Xthus:
X
X
X
X                                                      Page 1
X
X
X
X
X
X
X
X                       A Disassembler
X
X
X
X     unc '/lib/libc.a(printf.o)'
X
XIt is usually necessary to escape the arguments in this case
Xto  prevent  misinterpretation  by  the  shell. Libraries in
Xstandard places such as /lib and /usr/lib may  be  specified
Xin the same way as to ld, thus
X
X     unc '-lc(printf.o)'
X     unc '-lcurses(wmove.o)'
X
XAs an additional facility, the list of directories  searched
Xfor  libraries  may  be  varied  by  setting the environment
Xvariable LDPATH, which is interpreted similarly to the shell
XPATH variable, and of course defaults to
X
X     LDPATH=/lib:/usr/lib
X
XAs a further facility, the insertion of lib  before  and  .a
Xafter  the  argument may be suppressed by using a capital -L
Xargument, thus to print out the assembler  for  /lib/crt0.o,
Xthen the command
X
X     unc -Lcrt0.o
X
Xshould have the desired effect.
X
XSecond and subsequent file arguments are only referenced for
Xstripped  executable files, and may consist of single object
Xfiles and library members, using the same syntax as  before,
Xor whole libraries of object files, thus:
X
X     unc strippedfile -Lcrt0.o -lcurses -ltermcap '-lm(sqrt.o)' -lc
X
XIt is advisable to make some effort to put the libraries  to
Xbe  searched  in  the  order  in  which they were originally
Xloaded. This is because the search for  each  module  starts
Xwhere the previously matched module ended.  However, no harm
Xis done if this rule is not adhered to apart from  increased
Xexecution   time   except   in  the  rare  cases  where  the
Xdisassembler is confused by object modules  which  are  very
Xnearly similar.
X
X
X                   3.  Additional options
X
XThe following options are available to modify the  behaviour
Xof the disassembler.
X
X  -o file      Causes output to be  sent  to  the  specified
X               file instead of the standard output.
X
X
X
X
X                                                      Page 2
X
X
X
X
X
X
X
X                       A Disassembler
X
X
X
X  -t prefix    Causes temporary files to be created with the
X               given  prefix.  The  default prefix is split,
X               thus  causing  two  temporary  files  to   be
X               created  with  this  prefix  in  the  current
X               directory. If it is desired, for example,  to
X               create   the  files  as  /tmp/xx*,  then  the
X               argument -t /tmp/xx  should  be  given.  Note
X               that the temporary files may be very large as
X               a complete map of the text and data  segments
X               is generated.
X
X  -a           Suppresses  the  generation   of   non-global
X               absolute  symbols from the output. This saves
X               output  from  C  compilations   without   any
X               obvious  problems,  but  the  symbols  are by
X               default included in the name of producing  as
X               nearly  identical  output  as possible to the
X               original source.
X
X  -s           Causes an additional scan to take place where
X               all  possible  labels  are  replaced by local
X               symbols. The local symbols  are  inserted  in
X               strictly ascending order, starting at 1.
X
X  -v           Causes a blow-by-blow account  of  activities
X               to be output on the standard error.
X
X
X                    4.  Diagnostics etc
X
XTruncated or garbled object and library files usually  cause
Xprocessing to stop with an explanatory message.
X
XThe only other kinds of message are  some  passing  warnings
Xconcerning  obscure  constructs  not  handled,  such  as the
Xrelocation of byte fields, or the relocation of  overlapping
Xfields. Occasionally a message
X
X     Library clash: message
X
Xmay appear and processing cease. This message is found where
Xat  a  late  stage  in  processing  libraries,  the  program
Xdiscovers that due to the extreme similarity of two or  more
Xlibrary  members,  it has come to the wrong conclusion about
Xwhich one to use. The remedy here is to  spell  out  to  the
Xprogram which members to take in which order.
X
X
X
X
X
X
X
X
X                                                      Page 3
X
X
X
X
X
X
X
X                       A Disassembler
X
X
X
X                   5.  Future development
X
XIn the future it is hoped  to  devise  ways  of  making  the
Xdisassembler  independent of all the above-mentioned version
Xdependencies,  by  first  reading  a  files  defining  these
Xthings.  This  will  probably  be  applied  after the Common
XObject Format becomes more standard.
X
XIn the long term it would be desirable and useful to enhance
Xthe   product   to  produce  compilable  C  in  addition  to
Xassemblable assembler. Stages in the  process  are  seen  as
Xfollows:
X
X  1.  Better identification of basic  blocks  in  the  code.
X      Switch  statements  are  a  major problem here, as are
X      constant data held in the text segment.
X
X  2.  Marrying of data to the corresponding text. It  is  in
X      various  places  hard to divorce static references "on
X      the fly" (e.g.  strings,  and  switch  lists  in  some
X      implementations)  from static at the head of a module.
X      This is part  of  the  problem  of  identifying  basic
X      blocks.
X
X  3.  Compilation of header  files  to  work  out  structure
X      references   within  the  text.  At  this  stage  some
X      interaction may be needed.
X
XMeanwhile the product is one which is a useful tool  to  the
Xauthor  in  its present form. Comments and suggestions as to
Xthe most practical method of improving the  product  in  the
Xways   suggested  or  in  other  ways  would  be  gratefully
Xconsidered.
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X                                                      Page 4
X
X
X
X
END_OF_FILE
if test 7415 -ne `wc -c <'doc.out'`; then
    echo shar: \"'doc.out'\" unpacked with wrong size!
fi
# end of 'doc.out'
fi
if test -f 'file.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'file.c'\"
else
echo shar: Extracting \"'file.c'\" \(4203 characters\)
sed "s/^X//" >'file.c' <<'END_OF_FILE'
X/*
X *	SCCS:	@(#)file.c	1.2	11/2/84	14:17:35
X *	Various operations on files.
X *
X ***********************************************************************
X *	This software is copyright of
X *
X *		John M Collins
X *		47 Cedarwood Drive
X *		St Albans
X *		Herts, AL4 0DN
X *		England			+44 727 57267
X *
X *	and is released into the public domain on the following conditions:
X *
X *		1.  No free maintenance will be guaranteed.
X *		2.  Nothing may be based on this software without
X *		    acknowledgement, including incorporation of this
X *		    notice.
X *
X *	Notwithstanding the above, the author welcomes correspondence and bug
X *	fixes.
X ***********************************************************************
X */
X
X#include <stdio.h>
X#include <a.out.h>
X#include <ldfcn.h>
X#include "unc.h"
X	
Xlong	lseek();
Xvoid	unimpl();
X
X/*
X *	Validate addr and get text entry corresponding to it from the given
X *	file.
X */
X
Xvoid	gette(fid, addr, te)
Xregister  ef_fid  fid;
Xregister  long	addr;
Xt_entry	*te;
X{
X	addr -= fid->ef_tbase;
X	if  (addr < 0  ||  addr > fid->ef_tsize  || (addr & 1) != 0)  {
X		(void) fprintf(stderr, "Invalid text address %lx\n", addr);
X		exit(200);
X	}
X	(void) lseek(fid->ef_t, (long)(addr * sizeof(t_entry)/2), 0);
X	if  (read(fid->ef_t, (char *) te, sizeof(t_entry)) != sizeof(t_entry))  {
X		(void) fprintf(stderr, "Trouble reading text at %lx\n", addr);
X		exit(201);
X	}
X}
X
X/*
X *	Store a text entry.
X */
X
Xvoid	putte(fid, addr, te)
Xregister  ef_fid  fid;
Xregister  long	addr;
Xt_entry	*te;
X{
X	addr -= fid->ef_tbase;
X	if  (addr < 0  ||  addr > fid->ef_tsize  ||  (addr & 1) != 0)  {
X		(void) fprintf(stderr, "Invalid text address %lx\n", addr);
X		exit(200);
X	}
X	(void) lseek(fid->ef_t, (long)(addr * sizeof(t_entry)/2), 0);
X	(void) write(fid->ef_t, (char *) te, sizeof(t_entry));
X}
X
X/*
X *	Validate addr and get data entry corresponding to it from the given
X *	file.
X */
X
Xvoid	getde(fid, addr, de)
Xregister  ef_fid  fid;
Xregister  long	addr;
Xd_entry	*de;
X{
X	if  (addr < fid->ef_dbase  ||  addr > fid->ef_end)  {
X		(void) fprintf(stderr, "Invalid data address %lx\n", addr);
X		exit(200);
X	}
X	addr -= fid->ef_dbase;
X	(void) lseek(fid->ef_d, (long)(addr * sizeof(d_entry)), 0);
X	if  (read(fid->ef_d, (char *) de, sizeof(d_entry)) != sizeof(d_entry))  {
X		(void) fprintf(stderr, "Trouble reading data at %lx\n", addr);
X		exit(201);
X	}
X}
X
X/*
X *	Store a data entry.
X */
X
Xvoid	putde(fid, addr, de)
Xregister  ef_fid  fid;
Xregister  long	addr;
Xd_entry	*de;
X{
X	if  (addr < fid->ef_dbase  ||  addr > fid->ef_end)  {
X		(void) fprintf(stderr, "Invalid data address %lx\n", addr);
X		exit(200);
X	}
X	addr -= fid->ef_dbase;
X	(void) lseek(fid->ef_d, (long)(addr * sizeof(d_entry)), 0);
X	(void) write(fid->ef_d, (char *) de, sizeof(d_entry));
X}
X
X/*
X *	Set type and length of given data entry.
X */
X
Xvoid	setde(fid, addr, type, lng)
Xef_fid	fid;
Xlong	addr;
Xunsigned  type;
Xint	lng;
X{
X	d_entry	dat;
X
X	if  (addr > fid->ef_end)
X		return;
X	getde(fid, addr, &dat);
X	if  (type == D_CONT  &&  dat.d_reloc != R_NONE)  {
X		char	obuf[30];
X		(void) sprintf(obuf, "overlapped reloc 0x%x", addr);
X		unimpl(obuf);
X	}
X	dat.d_type = type;
X	dat.d_lng = lng;
X	putde(fid, addr, &dat);
X}
X	
X/*
X *	Get a word of data file, size as requested.
X */
X
Xlong	getdw(fid, pos, size)
Xregister  ef_fid  fid;
Xlong	pos;
Xint	size;
X{
X	d_entry	dat;
X	register  long	res;
X	register  int	i, lt;
X	
X	getde(fid, pos, &dat);
X	
X	switch  (size)  {
X	case  R_BYTE:
X		return	dat.d_contents;
X		
X	case  R_LONG:
X		lt = 4;
X		goto  rest;
X		
X	case  R_WORD:
X		lt = 2;
X	rest:
X		res = dat.d_contents;
X		for  (i = 1;  i < lt; i++)  {
X			getde(fid, pos+i, &dat);
X			res = (res << 8) + dat.d_contents;
X		}
X		return	res;
X		
X	default:
X		(void) fprintf(stderr, "Data word size error\n");
X		exit(20);
X	}
X	/*NOTREACHED*/
X}
X
X/*
X *	Get a word of text file.
X */
X
Xlong	gettw(fid, pos, size)
Xregister  ef_fid  fid;
Xlong	pos;
Xint	size;
X{
X	t_entry	tex;
X	long	res;
X	
X	gette(fid, pos, &tex);
X	
X	switch  (size)  {
X	case  R_BYTE:
X		return	tex.t_contents >> 8;
X		
X	case  R_WORD:
X		return	tex.t_contents;
X		
X	case  R_LONG:
X		res = tex.t_contents;
X		gette(fid, pos+2, &tex);
X		return	(res << 16) + tex.t_contents;
X	default:
X		(void) fprintf(stderr, "Text word size error\n");
X		exit(20);
X	}
X	/*NOTREACHED*/
X}
END_OF_FILE
if test 4203 -ne `wc -c <'file.c'`; then
    echo shar: \"'file.c'\" unpacked with wrong size!
fi
# end of 'file.c'
fi
if test -f 'iset.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'iset.c'\"
else
echo shar: Extracting \"'iset.c'\" \(20063 characters\)
sed "s/^X//" >'iset.c' <<'END_OF_FILE'
X/*
X *	SCCS:	@(#)iset.c	1.2	11/2/84	14:18:23
X *	Decode instructions.
X *
X ***********************************************************************
X *	This software is copyright of
X *
X *		John M Collins
X *		47 Cedarwood Drive
X *		St Albans
X *		Herts, AL4 0DN
X *		England			+44 727 57267
X *
X *	and is released into the public domain on the following conditions:
X *
X *		1.  No free maintenance will be guaranteed.
X *		2.  Nothing may be based on this software without
X *		    acknowledgement, including incorporation of this
X *		    notice.
X *
X *	Notwithstanding the above, the author welcomes correspondence and bug
X *	fixes.
X ***********************************************************************
X */
X
X#include <stdio.h>
X#include <a.out.h>
X#include <ldfcn.h>
X#include "unc.h"
X
Xef_fids	mainfile;
Xlong	endt;
X
Xvoid	gette(), putte();
Xvoid	mkdref();
Xlong	gettw();
Xsymbol	textlab();
X
Xint	l1(), l2(), el1(), lea(), lmove(), lcbch(), jj();
Xint	limed(), lsbit(), lmvml(), lone(), loone(), lonew(), lonel();
X
Xint	pmove(), pcbch(), pdbcc(), pscc(), pcs(), pmovc(), pstop(), pexg();
Xint	pimed(), pmovp(), psbit(), pdbit(), pcs2(), pone(), ppea();
Xint	plea(), pdreg(), pmvml(), ptrap(), plink(), pareg(), podreg();
Xint	pqu(), pmqu(), ptreg(), pcmpm(), pomode(), pmshf(), pshf();
X
Xstruct	opstr	{
X	unsigned  short	 mask;
X	unsigned  short  match;
X	int	(*opsize)();
X	int	(*opprin)();
X	char	*prarg;
X} optab[] = {
X	0xf000, 0x2000, lmove, pmove, "l",
X	0xf000, 0x3000, lmove, pmove, "w",
X	0xf000, 0x1000, lmove, pmove, "b",
X	0xf000, 0x6000, lcbch, pcbch, 0,
X	0xffbf, 0x003c, l2,    pcs,   "or",
X	0xff00, 0x0000, limed, pimed, "or",
X	0xffbf, 0x023c, l2,    pcs,   "and",
X	0xff00, 0x0200, limed, pimed, "and",
X	0xff00, 0x0400, limed, pimed, "sub",
X	0xff00, 0x0600, limed, pimed, "add",
X	0xffbf, 0x0a3c, l2,    pcs,   "eor",
X	0xff00, 0x0a00, limed, pimed, "eor",
X	0xff00, 0x0c00, limed, pimed, "cmp",
X	0xf138, 0x0108, l2,    pmovp, 0,
X	0xff00, 0x0800, lsbit, psbit, 0,
X	0xf100, 0x0100, lonew, pdbit, 0,
X	0xffc0, 0x40c0, lonew, pcs2,  "sr",
X	0xff00, 0x4000, lone,  pone,  "negx",
X	0xff00, 0x4200, lone,  pone,  "clr",
X	0xffc0, 0x44c0, lonew, pcs2,  "cc",
X	0xff00, 0x4400, lone,  pone,  "neg",
X	0xffc0, 0x46c0, lonew, pcs2,  "sr",
X	0xff00, 0x4600, lone,  pone,  "not",
X	0xffc0, 0x4800, lonew, ppea,  "nbcd",
X	0xfff8, 0x4840, l1,    pdreg, "swap",
X	0xffc0, 0x4840, lonel, ppea,  "pea",
X	0xfff8, 0x4880, l1,    pdreg, "extw",
X	0xfff8, 0x48c0, l1,    pdreg, "extl",
X	0xfb80, 0x4880, lmvml, pmvml, 0,
X	0xffc0, 0x4ac0, lonew, ppea,  "tas",
X	0xff00, 0x4a00, lone,  pone,  "tst",
X	0xfff0, 0x4e40, l1,    ptrap, 0,
X	0xfff8, 0x4e50, l2,    plink, 0,
X	0xfff8, 0x4e58, l1,    pareg, "unlk\t%s",
X	0xfff8, 0x4e60, l1,    pareg, "movl\t%s,usp",
X	0xfff8, 0x4e68, l1,    pareg, "movl\tusp,%s",
X	0xffff, 0x4e70, l1,    pareg, "reset",
X	0xffff, 0x4e71, l1,    pareg, "nop",
X	0xffff, 0x4e72, l2,    pstop, 0,
X	0xffff, 0x4e73, el1,   pareg, "rte",
X	0xffff, 0x4e75, el1,   pareg, "rts",
X	0xffff, 0x4e76, l1,    pareg, "trapv",
X	0xffff, 0x4e77, el1,   pareg, "rtr",
X	0xfffe, 0x4e7a, l2,    pmovc, 0,
X	0xffc0, 0x4e80, jj,    ppea,  "jsr",
X	0xffc0, 0x4ec0, jj,    ppea,  "jmp",
X	0xf1c0, 0x4180, lonew, podreg,"chk",
X	0xf1c0, 0x41c0, lonel, plea,  0,
X	0xf0f8, 0x50c8, lcbch, pdbcc, 0,
X	0xf0c0, 0x50c0, lonew, pscc,  0,
X	0xf100, 0x5000, lone,  pqu,   "add",
X	0xf100, 0x5100, lone,  pqu,   "sub",
X	0xf100, 0x7000, l1,    pmqu,  0,
X	0xf1c0, 0x80c0, lonew, podreg,"divu",
X	0xf1c0, 0x81c0, lonew, podreg,"divs",
X	0xf1f0, 0x8100, l1,    ptreg, "sbcd",
X	0xf000, 0x8000, loone, pomode,"or",
X	0xf1f0, 0x9100, l1,    ptreg, "subxb",
X	0xf1f0, 0x9140, l1,    ptreg, "subxw",
X	0xf1f0, 0x9180, l1,    ptreg, "subxl",
X	0xf000, 0x9000, loone, pomode,"sub",
X	0xf1f8, 0xb108, l1,    pcmpm, "cmpmb",
X	0xf1f8, 0xb148, l1,    pcmpm, "cmpmw",
X	0xf1f8, 0xb188, l1,    pcmpm, "cmpml",
X	0xf100, 0xb000, loone, pomode,"cmp",
X	0xf1c0, 0xb1c0, loone, pomode,"cmp",
X	0xf100, 0xb100, loone, pomode,"eor",
X	0xf1c0, 0xc0c0, lonew, podreg,"mulu",
X	0xf1c0, 0xc1c0, lonew, podreg,"muls",
X	0xf1f0, 0xc100, l1,    ptreg, "abcd",
X	0xf130, 0xc100, l1,    pexg,  0,
X	0xf000, 0xc000, loone, pomode,"and",
X	0xf1f0, 0xd100, l1,    ptreg, "addxb",
X	0xf1f0, 0xd140, l1,    ptreg, "addxw",
X	0xf1f0, 0xd180, l1,    ptreg, "addxl",
X	0xf000, 0xd000, loone, pomode,"add",
X	0xf8c0, 0xe0c0, lonew, pmshf,  0,
X	0xf000, 0xe000, l1,    pshf,   0,
X	0
X};
X
Xchar	*areg[] = { "a0", "a1", "a2", "a3", "a4", "a5", "a6", "sp"};
Xchar	*cclist[] = { "hi", "ls", "cc", "cs", "ne", "eq", "vc", "vs",
X			"pl", "mi", "ge", "lt", "gt", "le"};
X	
Xchar	*shtype[] = { "as", "ls", "rox", "ro" };
Xchar	*bittyp[] = { "tst", "chg", "clr", "set" };
X
Xchar	*creg[] = { "sfc", "dfc", "usp", "vbr" };
X
X/*
X *	Length functions.
X */
X
Xint	l1()
X{
X	return	1;
X}
X
Xint	l2()
X{
X	return	2;
X}
X
Xint	el1(te)
Xt_entry	*te;
X{
X	te->t_bchtyp = T_UNBR;
X	return	1;
X}
X
Xint	lea(instr, size, pos)
Xunsigned  instr, size;
Xlong	pos;
X{
X	switch  ((instr >> 3) & 0x7)  {
X	case  0:
X	case  1:
X	case  2:
X	case  3:
X	case  4:
X		return	1;
X	case  5:
X	case  6:
X		return	2;
X	default:
X		switch  (instr & 0x7)  {
X		case  0:
X		case  2:
X		case  3:
X			return	2;
X		case  1:
X			mkdref(pos, size);
X			return	3;
X		case  4:
X			if  (size > 2)
X				return	3;
X			return	2;
X		default:
X			return	0;
X		}
X	}
X}
X
X/*
X *	Lengths of move instructions.
X */
X
Xint	lmove(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	register  unsigned  tc  =  te->t_contents;
X	unsigned  sz  =  1;
X	int	lng, lng2;
X	
X	lng  = tc & 0xf000;
X	if  (lng == 0x3000)
X		sz = 2;
X	else  if  (lng == 0x2000)
X		sz = 4;
X	
X	if  ((lng = lea(tc, sz, pos+2)) <= 0)
X		return	0;
X	lng2 = lea(((tc>>3) & 0x38) | ((tc>>9) & 0x7), sz, pos+lng+lng);
X	if  (lng2 <= 0)
X		return	0;
X	return	lng + lng2 - 1;
X}
X
X/*
X *	Lengths for conditional branches and dbcc instructions.
X */
X
Xint	lcbch(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	long	dest  =  pos + 2;
X	int	res   =  2;
X	
X	if  ((tc & 0xf000) == 0x5000  ||  (tc & 0xff) == 0)
X		dest += (short)gettw(&mainfile, pos+2, R_WORD);
X	else  {
X		dest += (char) tc;
X		res = 1;
X	}
X 	if ( dest < mainfile.ef_tbase 
X 		|| dest >= mainfile.ef_tbase+mainfile.ef_tsize 
X 		|| (dest & 1) != 0 )
X 		return 0;		/* Illegal branch destination */
X	if  ((tc & 0xff00) == 0x6000)
X		te->t_bchtyp = T_UNBR;
X	else  if  ((tc & 0xff00) == 0x6100)
X		te->t_bchtyp = T_JSR;
X	else
X		te->t_bchtyp = T_CONDBR;
X
X 	if ( (te->t_relsymb = textlab(dest, pos)) == NULL ) {
X 		te->t_bchtyp = T_NOBR;/* Branch to a continuation */
X 		return 0;
X 	}
X	return	res;
X}
X
Xint	jj(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	t_entry	nextl;
X 	long dest;
X	
X	te->t_bchtyp = (tc & 0x40)? T_UNBR: T_JSR;
X	if  ((tc & 0x3f) == 0x39)  {
X		gette(&mainfile, pos+2, &nextl);
X		if  (nextl.t_relsymb == NULL)  {
X 			dest = gettw(&mainfile, pos + 2, R_LONG );
X 			if ( dest < mainfile.ef_tbase
X 				|| dest >= mainfile.ef_tbase+mainfile.ef_tsize
X 				|| (dest & 1) != 0 )
X 				return 0;	/* Illegal branch destination */
X 			if ( ( nextl.t_relsymb = textlab(dest, pos) ) == NULL )
X 			return 0;	/* Branch to a continuation */
X			putte(&mainfile, pos+2, &nextl);
X		}
X		te->t_relsymb = nextl.t_relsymb;	/*  Easy ref  */
X	}
X	return	lea(tc, 4, pos+2);
X}
X
Xint	limed(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	int	lng;
X	
X	/*
X	 *	Specifically exclude byte address register operands,
X	 *	and ones which have lengths of 3.
X	 */
X
X	if  ((tc & 0xf8) == 0x08)
X		return  0;
X	
X	if  ((tc & 0xc0) >= 0x80)  {
X		if  (tc & 0x40)
X			return  0;
X		lng = lea(tc, 4, pos+6);
X		if  (lng > 0)
X			lng += 2;
X	}
X	else  {
X		lng = lea(tc, (unsigned)((tc & 0xc0)?2:1), pos+4);
X		if  (lng > 0)
X			lng++;
X	}
X	return	lng;
X}
X
Xint	lsbit(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	int	lng = lea(te->t_contents, 1, pos+4);
X	
X	if  (lng > 0)
X		lng++;
X	return	lng;
X}
X
Xint	lmvml(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	int	lng = lea(te->t_contents,
X			(unsigned)(te->t_contents&0x40? 4:2), pos+4);
X	
X	if  (lng > 0)
X		lng++;
X	return	lng;
X}
X
X/*
X *	Length depends on bits 6 and 7 of instruction.
X */
X
Xint	lone(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	
X	return	lea(tc, 1 << ((tc >> 6) & 3), pos+2);
X}
X
X/*
X *	Length depends on bits 6-8 of instruction.
X */
X
Xint	loone(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	
X	switch  ((tc >> 6) & 7)  {
X	case  0:
X	case  4:
X		return	lea(tc, 1, pos+2);
X	case  1:
X	case  3:
X	case  5:
X		return  lea(tc, 2, pos+2);
X	case  2:
X	case  6:
X	case  7:
X		return	lea(tc, 4, pos+2);
X	}
X	/*NOTREACHED*/
X}
X
Xint	lonew(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	return	lea(te->t_contents, 2, pos+2);
X}
X
Xint	lonel(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	return	lea(te->t_contents, 4, pos+2);
X}
X
X/*
X *	Print routines.
X */
X
Xint	findleng(tc)
Xunsigned  tc;
X{
X	switch  ((tc >> 6) & 3)  {
X	case  0:
X		return	'b';
X	case  1:
X		return	'w';
X	default:
X		return	'l';
X	}
X}
X
Xvoid	piword(disp)
Xunsigned  disp;
X{
X	int	szc;
X	
X	(void) printf("@(0x%x,", disp & 0xff);
X	if  (disp & 0x8000)
X		(void) printf("%s", areg[(disp >> 12) & 0x7]);
X	else
X		(void) printf("d%d", (disp >> 12) & 0x7);
X	szc = 'w';
X	if  (disp & (1 << 10))
X		szc = 'l';
X	(void) printf(":%c)", szc);
X}
X
Xvoid	paddr(pos)
Xlong	pos;
X{
X	t_entry	tent;
X	symbol	symb;
X	
X	gette(&mainfile, pos, &tent);
X	if  (tent.t_relsymb != NULL)  {
X		symb = tent.t_relsymb;
X		if  (symb->s_lsymb != 0)
X			(void) printf("%u$", symb->s_lsymb);
X		else
X			(void) printf("%s", symb->s_name);
X		if  (tent.t_reldisp != 0)
X			(void) printf("+0x%x", tent.t_reldisp);
X		return;
X	}
X	(void) printf("0x%x", gettw(&mainfile, pos, R_LONG));
X}
X
Xint	prea(ea, pos, sz)
Xunsigned  ea, sz;
Xlong	pos;			/*  Address of previous word to extn  */
X{
X	unsigned  reg  =  ea & 0x7;
X	long	disp;
X	
X	pos += 2;
X	
X	switch  ((ea >> 3) & 0x7)  {
X	case  0:
X		(void) printf("d%d", reg);
X		return	0;
X	case  1:
X		(void) printf("%s", areg[reg]);
X		return	0;
X	case  2:
X		(void) printf("%s@", areg[reg]);
X		return	0;
X	case  3:
X		(void) printf("%s at +", areg[reg]);
X		return	0;
X	case  4:
X		(void) printf("%s at -", areg[reg]);
X		return	0;
X	case  5:
X		disp = gettw(&mainfile, pos, R_WORD);
X		(void) printf("%s@(0x%x)", areg[reg], disp);
X		return	2;
X	case  6:
X		(void) printf("%s", areg[reg]);
X		piword((unsigned) gettw(&mainfile, pos, R_WORD));
X		return	2;
X	default:
X		switch  (reg)  {
X		case  0:
X			disp = gettw(&mainfile, pos, R_WORD);
X			(void) printf("0x%x:w", disp);
X			return	2;
X		case  1:
X			paddr(pos);
X			return	4;
X		case  2:
X			disp = gettw(&mainfile, pos, R_WORD);
X			(void) printf("pc@(0x%x)", disp);
X			return	2;
X		case  3:
X			(void) printf("pc");
X			piword((unsigned) gettw(&mainfile, pos, R_WORD));
X			return	2;
X		case  4:
X			(void) printf("#");
X			if  (sz < 4)
X				(void) printf("0x%x", gettw(&mainfile, pos, R_WORD));
X			else
X				paddr(pos);
X			return	sz;
X		default:
X			(void) fprintf(stderr, "Funny mode\n");
X			exit(220);
X		}
X	}
X	/*NOTREACHED*/
X}
X	
Xint	pmove(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  sz  =  2;
X	unsigned  tc  =  te->t_contents;
X	
X	(void) printf("mov%s\t", optab[te->t_iindex].prarg);
X	
X	if  ((tc & 0xf000) == 0x2000)
X		sz = 4;
X	
X	pos += prea(tc, pos, sz);
X	putchar(',');
X	(void) prea(((tc >> 9) & 0x7) | ((tc >> 3) & 0x38), pos, sz);
X}
X
Xint	pcbch(te)
Xt_entry	*te;
X{
X	int	cc = ((te->t_contents >> 8) & 0xf) - 2;
X	char	*msg;
X	register  symbol  ts;
X	
X	if  (cc < 0)
X		msg = cc < -1? "ra": "sr";
X	else
X		msg = cclist[cc];
X	(void) printf("b%s", msg);
X	if  (te->t_lng < 2)
X		(void) printf("s");
X	ts = te->t_relsymb;
X	if  (ts->s_lsymb != 0)
X		(void) printf("\t%u$", ts->s_lsymb);
X	else
X		(void) printf("\t%s", ts->s_name);
X}
X
Xint	pdbcc(te)
Xt_entry	*te;
X{
X	unsigned  tc  =  te->t_contents;
X	int	cc = ((tc >> 8) & 0xf) - 2;
X	char	*msg;
X	register  symbol  ts;
X	
X	if  (cc < 0)
X		msg = cc < -1? "t": "f";
X	else
X		msg = cclist[cc];
X	ts = te->t_relsymb;
X	(void) printf("db%s\td%d,", msg, tc & 0x7);
X	if  (ts->s_lsymb)
X		(void) printf("%u$", ts->s_lsymb);
X	else
X		(void) printf("%s", ts->s_name);
X}
X
Xint	pscc(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	int	cc = ((tc >> 8) & 0xf) - 2;
X	char	*msg;
X	
X	if  (cc < 0)
X		msg = cc < -1? "t": "f";
X	else
X		msg = cclist[cc];
X	(void) printf("s%s\t", msg);
X	(void) prea(tc, pos, 1);
X}
X
Xint	pcs(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	long	disp  =  gettw(&mainfile, pos+2, R_WORD);
X	
X	(void) printf("%s", optab[te->t_iindex].prarg);
X	if  ((te->t_contents & 0xc0) == 0)
X		(void) printf("b\t#0x%x,cc", disp);
X	else
X		(void) printf("w\t#0x%x,sr", disp);
X}
X
Xint	pmovc(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	int	disp = gettw(&mainfile, pos+2, R_WORD);
X	int	ctrl = ((disp >> 10) & 2) | (disp & 1);
X
X	(void) printf("movec\t");
X	if  ((te->t_contents & 1) == 0)
X		(void) printf("%s,", creg[ctrl]);
X	if  (disp & 0x8000)
X		(void) printf("%s", areg[(disp >> 12) & 7]);
X	else
X		(void) printf("d%d", disp >> 12);
X	if  (te->t_contents & 1)
X		(void) printf(",%s", creg[ctrl]);
X}
X
Xint	pimed(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	int	sz = findleng(te->t_contents);
X	
X	(void) printf("%s%c\t#", optab[te->t_iindex].prarg, sz);
X	if  (sz == 'l')  {
X		paddr(pos+2);
X		putchar(',');
X		(void) prea(te->t_contents, pos+4, 4);
X	}
X	else  {
X		(void) printf("0x%x,", gettw(&mainfile, pos+2, R_WORD));
X		(void) prea(te->t_contents, pos+2, 2);
X	}
X}
X
Xint	pmovp(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	long	disp  =  gettw(&mainfile, pos+2, R_WORD);
X	int	dreg = tc >> 9;
X	char	*ar = areg[tc & 0x7];
X	
X	(void) printf("movep");
X	if  (tc & (1 << 6))
X		putchar('l');
X	else
X		putchar('w');
X
X	if  (tc & (1 << 7))
X		(void) printf("\td%d,%s@(0x%x)", dreg, ar, disp);
X	else
X		(void) printf("\t%s@(0x%x),d%d", ar, disp, dreg);
X}
X
Xint	psbit(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	
X	(void) printf("b%s\t#%d,", bittyp[(tc >> 6) & 0x3], gettw(&mainfile, pos+2, R_WORD));
X	(void) prea(tc, pos+2, 1);
X}
X
X/*ARGSUSED*/
Xint	pstop(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	(void) printf("stop\t#0x%x", gettw(&mainfile, pos+2, R_WORD));
X}
X
Xint	pdbit(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	
X	(void) printf("b%s\td%d,", bittyp[(tc >> 6) & 0x3], (tc >> 9) & 0x7);
X	(void) prea(tc, pos, 1);
X}
X
Xint	pcs2(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	
X	(void) printf("movw\t");
X	if  ((tc & 0xffc0) == 0x40c0)  {
X		(void) printf("sr,");
X		(void) prea(tc, pos, 2);
X	}
X	else  {
X		(void) prea(tc, pos, 2);
X		(void) printf(",%s", optab[te->t_iindex].prarg);
X	}
X}
X
Xint	pone(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	int	sz = findleng(tc);
X	
X	(void) printf("%s%c\t", optab[te->t_iindex].prarg, sz);
X	(void) prea(tc, pos, (unsigned)(sz == 'l'? 4: 2));
X}
X
Xint	ppea(te, pos)	/*  nbcd, pea, tas, jmp, jsr  */
Xt_entry	*te;
Xlong	pos;
X{
X	(void) printf("%s\t", optab[te->t_iindex].prarg);
X	(void) prea(te->t_contents, pos, (unsigned)(te->t_lng > 2? 4: 2));
X}
X
X
Xint	plea(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	(void) printf("lea\t");
X	(void) prea(te->t_contents, pos, 4);
X	(void) printf(",%s", areg[(te->t_contents >> 9) & 0x7]);
X}
X
Xint	pdreg(te)
Xt_entry	*te;
X{
X	(void) printf("%s\td%d", optab[te->t_iindex].prarg, te->t_contents & 7);
X}
X
X
Xint	pmvml(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	register  unsigned  dw  =  gettw(&mainfile, pos+2, R_WORD);
X	unsigned  sz = 4;
X	int	sc = 'l';
X	register  int	i;
X	register  unsigned  bit;
X	
X	(void) printf("movem");
X	if  ((tc & 0x40) == 0)  {
X		sc = 'w';
X		sz = 2;
X	}
X	
X	(void) printf("%c\t", sc);
X	
X	if  (tc & 0x400)  {
X		(void) prea(tc, pos+2, sz);
X		(void) printf(",#0x%x", dw);
X	}
X	else  {
X		(void) printf("#0x%x,", dw);
X		(void) prea(tc, pos+2, sz);
X	}
X	
X	(void) printf("\t|");
X	
X	if  ((tc & 0x38) == 0x20)  {
X		bit = 0x8000;
X		for  (i = 0;  i < 8;  i++)  {
X			if  (dw & bit)
X				(void) printf(" d%d", i);
X			bit >>= 1;
X		}
X		for  (i = 0;  i < 8;  i++)  {
X			if  (dw & bit)
X				(void) printf(" %s", areg[i]);
X			bit >>= 1;
X		}
X	}
X	else  {
X		bit = 1;
X		for  (i = 0;  i < 8;  i++)  {
X			if  (dw & bit)
X				(void) printf(" d%d", i);
X			bit <<= 1;
X		}
X		for  (i = 0;  i < 8;  i++)  {
X			if  (dw & bit)
X				(void) printf(" %s", areg[i]);
X			bit <<= 1;
X		}
X	}
X}
X
Xint	ptrap(te)
Xt_entry	*te;
X{
X	(void) printf("trap\t#0x%x", te->t_contents & 0xf);
X}
X
Xint	plink(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	(void) printf("link\t%s,#0x%x", areg[te->t_contents & 0x7],
X				gettw(&mainfile, pos+2, R_WORD));
X}
X
X
Xint	pareg(te)
Xt_entry	*te;
X{
X	(void) printf(optab[te->t_iindex].prarg, areg[te->t_contents & 0x7]);
X}
X
Xint	podreg(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	
X	(void) printf("%s\t", optab[te->t_iindex].prarg);
X	(void) prea(tc, pos, 2);
X	(void) printf(",d%d", (tc >> 9) & 0x7);
X}
X
Xint	pqu(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	int	sz  =  findleng(tc);
X	int	amt = (tc >> 9) & 0x7;
X	
X	if  (amt == 0)
X		amt = 8;
X	(void) printf("%sq%c\t#%d,", optab[te->t_iindex].prarg, sz, amt);
X	(void) prea(tc, pos, (unsigned)(sz == 'l'? 4: 2));
X}
X
Xint	pmqu(te)
Xt_entry	*te;
X{
X	unsigned  tc  =  te->t_contents;
X
X	(void) printf("moveq\t#0x%x,d%d", (char)tc, (tc >> 9) & 0x7);
X}
X
Xint	ptreg(te)
Xt_entry	*te;
X{
X	register  unsigned  tc  =  te->t_contents;
X	int	rx = (tc >> 9) & 0x7;
X	int	ry = tc & 0x7;
X
X	(void) printf("%s\t", optab[te->t_iindex].prarg);
X	if  (tc & 0x8)
X		(void) printf("%s at -,%s at -", areg[ry], areg[rx]);
X	else
X		(void) printf("d%d,d%d", ry, rx);
X}
X
Xint	pcmpm(te)
Xt_entry	*te;
X{
X	register  unsigned  tc  =  te->t_contents;
X
X	(void) printf("%s\t%s at +,%s at +", optab[te->t_iindex].prarg,
X		areg[tc & 7], areg[(tc >> 9) & 7]);
X}
X
Xint	pomode(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	char	bef[4], aft[4];
X	int	sz;
X	int	reg = (tc >> 9) & 7;
X
X	bef[0] = aft[0] = '\0';
X	
X	switch  ((tc >> 6) & 7)  {
X	case  0:
X		sz = 'b';
X		goto  toreg;
X	case  1:
X		sz = 'w';
X		goto  toreg;
X	case  2:
X		sz = 'l';
X	toreg:
X		(void) sprintf(aft, ",d%d", reg);
X		break;
X	case  3:
X		sz = 'w';
X		goto  toareg;
X	case  7:
X		sz = 'l';
X	toareg:
X		(void) sprintf(aft, ",%s", areg[reg]);
X		break;
X	case  4:
X		sz = 'b';
X		goto  frreg;
X	case  5:
X		sz = 'w';
X		goto  frreg;
X	case  6:
X		sz = 'l';
X	frreg:
X		(void) sprintf(bef, "d%d,", reg);
X		break;
X	}
X
X	(void) printf("%s%c\t%s", optab[te->t_iindex].prarg, sz, bef);
X	(void) prea(tc, pos, (unsigned)(sz == 'l'? 4: 2));
X	(void) printf(aft);
X}
X
Xint	pexg(te)
Xt_entry	*te;
X{
X	unsigned  tc  =  te->t_contents;
X	int	r1 = (tc >> 9) & 7, r2 = tc & 7;
X
X	(void) printf("exg\t");
X	
X	if  ((tc & 0x00f8) == 0x0048)
X		(void) printf("%s,", areg[r1]);
X	else
X		(void) printf("d%d,", r1);
X	if  (tc & 0x8)
X		(void) printf("%s", areg[r2]);
X	else
X		(void) printf("d%d", r2);
X}
X	
Xint	pmshf(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	unsigned  tc  =  te->t_contents;
X	
X	(void) printf("%s%cw\t", shtype[(tc >> 9) & 3], tc & 0x100? 'l': 'r');
X	(void) prea(tc, pos, 2);
X}
X
Xint	pshf(te)
Xt_entry	*te;
X{
X	unsigned  tc  =  te->t_contents;
X	int	sz  =  findleng(tc);
X	int	disp = (tc >> 9) & 7;
X
X	(void) printf("%s%c%c\t", shtype[(tc >> 3) & 3], tc & 0x100? 'l': 'r', sz);
X	if  (tc & 0x20)
X		(void) printf("d%d", disp);
X	else
X		(void) printf("#%d", disp == 0? 8: disp);
X	(void) printf(",d%d", tc & 7);
X}
X
X/*
X *	Find length etc of instruction.
X */
X
Xint	findinst(te, pos)
Xregister  t_entry  *te;
Xlong	pos;
X{
X	register  struct  opstr	*op;
X	unsigned  tc  =  te->t_contents;
X	int	lng = 0;
X	register  int	i;
X
X	te->t_type = T_BEGIN;
X	te->t_bchtyp = T_NOBR;
X	
X	for  (op = &optab[0];  op->mask;  op++)  {
X		if  ((tc & op->mask) == op->match)  {
X			te->t_iindex = op - optab;
X			lng = (op->opsize)(te, pos);
X			break;
X		}
X	}
X
X	for  (i = 1;  i < lng;  i++)  {
X		t_entry	ctent;
X		long	npos = pos+i+i;
X		
X		if  (npos >= endt)
X			goto  clearem;
X		gette(&mainfile, npos, &ctent);
X		if  (ctent.t_bdest || ctent.t_dref)  {
Xclearem:		for  (i--; i > 0; i--)  {
X				npos = pos + i + i;
X				gette(&mainfile, npos, &ctent);
X				ctent.t_type = T_UNKNOWN;
X				putte(&mainfile, npos, &ctent);
X			}
X			lng = 0;
X			goto  ginv;
X		}
X		ctent.t_type = T_CONT;
X		putte(&mainfile, npos, &ctent);
X	}
X	
X	if  (lng <= 0)  {
Xginv:		te->t_vins = 0;
X		te->t_lng = 1;
X		te->t_type = T_UNKNOWN;
X		te->t_bchtyp = T_NOBR;
X	}
X	else
X		te->t_lng = lng;
X	return	lng;
X}
X
X/*
X *	Print instruction.
X */
X
Xvoid	prinst(te, pos)
Xt_entry	*te;
Xlong	pos;
X{
X	putchar('\t');
X	(optab[te->t_iindex].opprin)(te, pos);
X	putchar('\n');
X}
END_OF_FILE
if test 20063 -ne `wc -c <'iset.c'`; then
    echo shar: \"'iset.c'\" unpacked with wrong size!
fi
# end of 'iset.c'
fi
echo shar: End of archive 1 \(of 2\).
cp /dev/null ark1isdone
MISSING=""
for I in 1 2 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have unpacked both archives.
    echo "Read the README* files for more info"
    rm -f ark[1-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0
-- 
Lenny Tropiano             ICUS Software Systems       [work] +1 (516) 582-5525
lenny at icus.islp.ny.us      Telex: 154232428 ICUS       [home] +1 (516) 968-8576
{talcott,boulder,hombre,pacbell,sbcs}!icus!lenny       attmail!icus!lenny
        ICUS Software Systems -- PO Box 1; Islip Terrace, NY  11752



More information about the Unix-pc.sources mailing list