1 /*
2 * qobj.c - identify and parse a PowerPC object file
3 * forsyth@terzarima.net
4 */
5 #include <u.h>
6 #include <libc.h>
7 #include <bio.h>
8 #include <mach.h>
9 #include "qc/q.out.h"
10 #include "obj.h"
11
12 typedef struct Addr Addr;
13 struct Addr
14 {
15 char type;
16 char sym;
17 char name;
18 };
19 static Addr addr(Biobuf*);
20 static char type2char(int);
21 static void skip(Biobuf*, int);
22
23 int
_isq(char * s)24 _isq(char *s)
25 {
26 return (s[0]&0377) == ANAME /* ANAME */
27 && (s[1]&0377) == ANAME>>8
28 && s[2] == D_FILE /* type */
29 && s[3] == 1 /* sym */
30 && s[4] == '<'; /* name of file */
31 }
32
33 int
_readq(Biobuf * bp,Prog * p)34 _readq(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 n = Bgetc(bp); /* reg and flag */
78 skip(bp, 4); /* lineno(4) */
79 a = addr(bp);
80 if(n & 0x40)
81 addr(bp);
82 addr(bp);
83 if(a.type != D_OREG || a.name != D_STATIC && a.name != D_EXTERN)
84 p->kind = aNone;
85 p->sym = a.sym;
86 return 1;
87 }
88
89 static Addr
addr(Biobuf * bp)90 addr(Biobuf *bp)
91 {
92 Addr a;
93 long off;
94
95 a.type = Bgetc(bp); /* a.type */
96 skip(bp,1); /* reg */
97 a.sym = Bgetc(bp); /* sym index */
98 a.name = Bgetc(bp); /* sym type */
99 switch(a.type){
100 default:
101 case D_NONE: case D_REG: case D_FREG: case D_CREG:
102 case D_FPSCR: case D_MSR: case D_SREG:
103 break;
104 case D_SPR:
105 case D_OREG:
106 case D_DCR:
107 case D_CONST:
108 case D_BRANCH:
109 off = Bgetc(bp);
110 off |= Bgetc(bp) << 8;
111 off |= Bgetc(bp) << 16;
112 off |= Bgetc(bp) << 24;
113 if(off < 0)
114 off = -off;
115 if(a.sym && (a.name==D_PARAM || a.name==D_AUTO))
116 _offset(a.sym, off);
117 break;
118 case D_SCONST:
119 skip(bp, NSNAME);
120 break;
121 case D_FCONST:
122 skip(bp, 8);
123 break;
124 }
125 return a;
126 }
127
128 static char
type2char(int t)129 type2char(int t)
130 {
131 switch(t){
132 case D_EXTERN: return 'U';
133 case D_STATIC: return 'b';
134 case D_AUTO: return 'a';
135 case D_PARAM: return 'p';
136 default: return UNKNOWN;
137 }
138 }
139
140 static void
skip(Biobuf * bp,int n)141 skip(Biobuf *bp, int n)
142 {
143 while (n-- > 0)
144 Bgetc(bp);
145 }
146