xref: /inferno-os/utils/libmach/vobj.c (revision 7e00430948d8af545f880e82bb30cd3ee50deb04)
1 /*
2  * vobj.c - identify and parse a mips object file
3  */
4 #include <lib9.h>
5 #include <bio.h>
6 #include "vc/v.out.h"
7 #include "obj.h"
8 
9 typedef struct Addr	Addr;
10 struct Addr
11 {
12 	char	type;
13 	char	sym;
14 	char	name;
15 };
16 static Addr addr(Biobuf*);
17 static char type2char(int);
18 static void skip(Biobuf*, int);
19 
20 int
21 _isv(char *s)
22 {
23 	return  s[0] == ANAME				/* ANAME */
24 		&& s[1] == D_FILE			/* type */
25 		&& s[2] == 1				/* sym */
26 		&& s[3] == '<';				/* name of file */
27 }
28 
29 int
30 _readv(Biobuf *bp, Prog *p)
31 {
32 	int as, n;
33 	Addr a;
34 
35 	as = Bgetc(bp);			/* as */
36 	if(as < 0)
37 		return 0;
38 	p->kind = aNone;
39 	if(as == ANAME || as == ASIGNAME){
40 		if(as == ASIGNAME)
41 			skip(bp, 4);	/* signature */
42 		p->kind = aName;
43 		p->type = type2char(Bgetc(bp));		/* type */
44 		p->sym = Bgetc(bp);			/* sym */
45 		n = 0;
46 		for(;;) {
47 			as = Bgetc(bp);
48 			if(as < 0)
49 				return 0;
50 			n++;
51 			if(as == 0)
52 				break;
53 		}
54 		p->id = malloc(n);
55 		if(p->id == 0)
56 			return 0;
57 		Bseek(bp, -n, 1);
58 		if(Bread(bp, p->id, n) != n)
59 			return 0;
60 		return 1;
61 	}
62 	if(as == ATEXT)
63 		p->kind = aText;
64 	else if(as == AGLOBL)
65 		p->kind = aData;
66 	skip(bp, 5);		/* reg(1), lineno(4) */
67 	a = addr(bp);
68 	addr(bp);
69 	if(a.type != D_OREG || a.name != D_STATIC && a.name != D_EXTERN)
70 		p->kind = aNone;
71 	p->sym = a.sym;
72 	return 1;
73 }
74 
75 static Addr
76 addr(Biobuf *bp)
77 {
78 	Addr a;
79 	long off;
80 
81 	a.type = Bgetc(bp);	/* a.type */
82 	skip(bp,1);		/* reg */
83 	a.sym = Bgetc(bp);	/* sym index */
84 	a.name = Bgetc(bp);	/* sym type */
85 	switch(a.type){
86 	default:
87 	case D_NONE: case D_REG: case D_FREG: case D_MREG:
88 	case D_FCREG: case D_LO: case D_HI:
89 		break;
90 	case D_OREG:
91 	case D_CONST:
92 	case D_BRANCH:
93 		off = Bgetc(bp);
94 		off |= Bgetc(bp) << 8;
95 		off |= Bgetc(bp) << 16;
96 		off |= Bgetc(bp) << 24;
97 		if(off < 0)
98 			off = -off;
99 		if(a.sym && (a.name==D_PARAM || a.name==D_AUTO))
100 			_offset(a.sym, off);
101 		break;
102 	case D_SCONST:
103 		skip(bp, NSNAME);
104 		break;
105 	case D_FCONST:
106 		skip(bp, 8);
107 		break;
108 	}
109 	return a;
110 }
111 
112 static char
113 type2char(int t)
114 {
115 	switch(t){
116 	case D_EXTERN:		return 'U';
117 	case D_STATIC:		return 'b';
118 	case D_AUTO:		return 'a';
119 	case D_PARAM:		return 'p';
120 	default:		return UNKNOWN;
121 	}
122 }
123 
124 static void
125 skip(Biobuf *bp, int n)
126 {
127 	while (n-- > 0)
128 		Bgetc(bp);
129 }
130