xref: /plan9/sys/src/libmach/68020obj.c (revision 473d4f4b3fed09ca133e03643ec71c9150b33bcc)
1 /*
2  * 2obj.c - identify and parse a 68020 object file
3  */
4 #include <u.h>
5 #include <libc.h>
6 #include <bio.h>
7 #include <mach.h>
8 #include "68020/2.out.h"
9 #include "obj.h"
10 
11 typedef struct Addr	Addr;
12 struct Addr
13 {
14 	char	sym;
15 	char	flags;
16 };
17 static Addr addr(Biobuf *);
18 static char type2char(int);
19 static void skip(Biobuf*, int);
20 
21 int
_is2(char * t)22 _is2(char *t)
23 {
24 	uchar *s = (uchar *)t;
25 
26 	return  s[0] == (ANAME&0xff)			/* aslo = ANAME */
27 		&& s[1] == ((ANAME>>8)&0xff)		/* ashi = ANAME */
28 		&& s[2] == D_FILE			/* type */
29 		&& s[3] == 1				/* sym */
30 		&& s[4] == '<';				/* name of file */
31 }
32 
33 int
_read2(Biobuf * bp,Prog * p)34 _read2(Biobuf *bp, Prog *p)
35 {
36 	int as, n, c;
37 	Addr a;
38 
39 	as = Bgetc(bp);		/* as(low) */
40 	if(as < 0)
41 		return 0;
42 	c = Bgetc(bp);		/* as(high) */
43 	if(c < 0)
44 		return 0;
45 	as |= ((c & 0xff) << 8);
46 	p->kind = aNone;
47 	p->sig = 0;
48 	if(as == ANAME || as == ASIGNAME){
49 		if(as == ASIGNAME){
50 			Bread(bp, &p->sig, 4);
51 			p->sig = beswal(p->sig);
52 		}
53 		p->kind = aName;
54 		p->type = type2char(Bgetc(bp));		/* type */
55 		p->sym = Bgetc(bp);			/* sym */
56 		n = 0;
57 		for(;;) {
58 			as = Bgetc(bp);
59 			if(as < 0)
60 				return 0;
61 			n++;
62 			if(as == 0)
63 				break;
64 		}
65 		p->id = malloc(n);
66 		if(p->id == 0)
67 			return 0;
68 		Bseek(bp, -n, 1);
69 		if(Bread(bp, p->id, n) != n)
70 			return 0;
71 		return 1;
72 	}
73 	if(as == ATEXT)
74 		p->kind = aText;
75 	else if(as == AGLOBL)
76 		p->kind = aData;
77 	skip(bp, 4);		/*lineno: low, high, lowhigh, highigh*/
78 	a = addr(bp);
79 	addr(bp);
80 	if(!(a.flags & T_SYM))
81 		p->kind = aNone;
82 	p->sym = a.sym;
83 	return 1;
84 }
85 
86 static Addr
addr(Biobuf * bp)87 addr(Biobuf *bp)
88 {
89 	Addr a;
90 	int t;
91 	long off;
92 
93 	a.flags = Bgetc(bp);			/* flags */
94 	a.sym = -1;
95 	off = 0;
96 	if(a.flags & T_FIELD)
97 		skip(bp, 2);
98 	if(a.flags & T_INDEX)
99 		skip(bp, 7);
100 	if(a.flags & T_OFFSET){
101 		off = Bgetc(bp);
102 		off |= Bgetc(bp) << 8;
103 		off |= Bgetc(bp) << 16;
104 		off |= Bgetc(bp) << 24;
105 		if(off < 0)
106 			off = -off;
107 	}
108 	if(a.flags & T_SYM)
109 		a.sym = Bgetc(bp);
110 	if(a.flags & T_FCONST)
111 		skip(bp, 8);
112 	else if(a.flags & T_SCONST)
113 		skip(bp, NSNAME);
114 	else{
115 		t = Bgetc(bp);
116 		if(a.flags & T_TYPE)
117 			t |= Bgetc(bp) << 8;
118 		t &= D_MASK;
119 		if(a.sym > 0 && (t==D_PARAM || t==D_AUTO))
120 			_offset(a.sym, off);
121 	}
122 	return a;
123 }
124 
125 static char
type2char(int t)126 type2char(int t)
127 {
128 	switch(t){
129 	case D_EXTERN:		return 'U';
130 	case D_STATIC:		return 'b';
131 	case D_AUTO:		return 'a';
132 	case D_PARAM:		return 'p';
133 	default:		return UNKNOWN;
134 	}
135 }
136 
137 static void
skip(Biobuf * bp,int n)138 skip(Biobuf *bp, int n)
139 {
140 	while (n-- > 0)
141 		Bgetc(bp);
142 }
143